2 * Copyright (c) 2016 Fabien Siron <fabien.siron@epita.fr>
3 * Copyright (c) 2016 Dmitry V. Levin <ldv@altlinux.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include <sys/socket.h>
31 #include <linux/netlink.h>
32 #include "xlat/netlink_flags.h"
33 #include "xlat/netlink_types.h"
36 * Fetch a struct nlmsghdr from the given address.
39 fetch_nlmsghdr(struct tcb *const tcp, struct nlmsghdr *const nlmsghdr,
40 const kernel_ulong_t addr, const kernel_ulong_t len)
42 if (len < sizeof(struct nlmsghdr)) {
43 printstrn(tcp, addr, len);
47 if (umove_or_printaddr(tcp, addr, nlmsghdr))
54 print_nlmsghdr(struct tcb *tcp, const struct nlmsghdr *const nlmsghdr)
56 /* print the whole structure regardless of its nlmsg_len */
58 tprintf("{len=%u, type=", nlmsghdr->nlmsg_len);
60 printxval(netlink_types, nlmsghdr->nlmsg_type, "NLMSG_???");
63 printflags(netlink_flags, nlmsghdr->nlmsg_flags, "NLM_F_???");
65 tprintf(", seq=%u, pid=%u}", nlmsghdr->nlmsg_seq,
70 decode_nlmsghdr_with_payload(struct tcb *const tcp,
71 const struct nlmsghdr *const nlmsghdr,
72 const kernel_ulong_t addr,
73 const kernel_ulong_t len)
77 print_nlmsghdr(tcp, nlmsghdr);
79 unsigned int nlmsg_len =
80 nlmsghdr->nlmsg_len > len ? len : nlmsghdr->nlmsg_len;
81 if (nlmsg_len > sizeof(struct nlmsghdr)) {
84 printstrn(tcp, addr + sizeof(struct nlmsghdr),
85 nlmsg_len - sizeof(struct nlmsghdr));
92 decode_netlink(struct tcb *const tcp, kernel_ulong_t addr, kernel_ulong_t len)
94 struct nlmsghdr nlmsghdr;
95 bool print_array = false;
98 for (elt = 0; fetch_nlmsghdr(tcp, &nlmsghdr, addr, len); elt++) {
99 if (abbrev(tcp) && elt == max_strlen) {
104 unsigned int nlmsg_len = NLMSG_ALIGN(nlmsghdr.nlmsg_len);
105 kernel_ulong_t next_addr = 0;
106 kernel_ulong_t next_len = 0;
108 if (nlmsghdr.nlmsg_len >= sizeof(struct nlmsghdr)) {
109 next_len = (len >= nlmsg_len) ? len - nlmsg_len : 0;
111 if (next_len && addr + nlmsg_len > addr)
112 next_addr = addr + nlmsg_len;
115 if (!print_array && next_addr) {
120 decode_nlmsghdr_with_payload(tcp, &nlmsghdr, addr, len);