2 * Copyright (c) 2016 Fabien Siron <fabien.siron@epita.fr>
3 * Copyright (c) 2016 Dmitry V. Levin <ldv@altlinux.org>
4 * Copyright (c) 2016-2017 The strace developers.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #include <sys/socket.h>
32 #include <linux/netlink.h>
33 #include "xlat/netlink_flags.h"
34 #include "xlat/netlink_types.h"
37 #define NLMSG_HDRLEN NLMSG_ALIGN(sizeof(struct nlmsghdr))
40 * Fetch a struct nlmsghdr from the given address.
43 fetch_nlmsghdr(struct tcb *const tcp, struct nlmsghdr *const nlmsghdr,
44 const kernel_ulong_t addr, const kernel_ulong_t len)
46 if (len < sizeof(struct nlmsghdr)) {
47 printstrn(tcp, addr, len);
51 if (umove_or_printaddr(tcp, addr, nlmsghdr))
58 print_nlmsghdr(struct tcb *tcp, const struct nlmsghdr *const nlmsghdr)
60 /* print the whole structure regardless of its nlmsg_len */
62 tprintf("{len=%u, type=", nlmsghdr->nlmsg_len);
64 printxval(netlink_types, nlmsghdr->nlmsg_type, "NLMSG_???");
67 printflags(netlink_flags, nlmsghdr->nlmsg_flags, "NLM_F_???");
69 tprintf(", seq=%u, pid=%u}", nlmsghdr->nlmsg_seq,
74 decode_nlmsghdr_with_payload(struct tcb *const tcp,
75 const struct nlmsghdr *const nlmsghdr,
76 const kernel_ulong_t addr,
77 const kernel_ulong_t len);
80 decode_nlmsgerr(struct tcb *const tcp,
86 if (len < sizeof(err.error)) {
87 printstrn(tcp, addr, len);
91 if (umove_or_printaddr(tcp, addr, &err.error))
95 if (err.error < 0 && (unsigned) -err.error < nerrnos) {
96 tprintf("-%s", errnoent[-err.error]);
98 tprintf("%d", err.error);
101 addr += offsetof(struct nlmsgerr, msg);
102 len -= offsetof(struct nlmsgerr, msg);
106 if (fetch_nlmsghdr(tcp, &err.msg, addr, len)) {
107 decode_nlmsghdr_with_payload(tcp, &err.msg, addr, len);
115 decode_payload(struct tcb *const tcp,
116 const struct nlmsghdr *const nlmsghdr,
117 const kernel_ulong_t addr,
118 const kernel_ulong_t len)
120 if (nlmsghdr->nlmsg_type == NLMSG_ERROR) {
121 decode_nlmsgerr(tcp, addr, len);
123 } else if (nlmsghdr->nlmsg_type == NLMSG_DONE && len == sizeof(int)) {
126 if (!umove_or_printaddr(tcp, addr, &num))
131 printstrn(tcp, addr, len);
135 decode_nlmsghdr_with_payload(struct tcb *const tcp,
136 const struct nlmsghdr *const nlmsghdr,
137 const kernel_ulong_t addr,
138 const kernel_ulong_t len)
142 print_nlmsghdr(tcp, nlmsghdr);
144 unsigned int nlmsg_len =
145 nlmsghdr->nlmsg_len > len ? len : nlmsghdr->nlmsg_len;
146 if (nlmsg_len > NLMSG_HDRLEN) {
148 decode_payload(tcp, nlmsghdr, addr + NLMSG_HDRLEN,
149 nlmsg_len - NLMSG_HDRLEN);
156 decode_netlink(struct tcb *const tcp, kernel_ulong_t addr, kernel_ulong_t len)
158 struct nlmsghdr nlmsghdr;
159 bool print_array = false;
162 for (elt = 0; fetch_nlmsghdr(tcp, &nlmsghdr, addr, len); elt++) {
163 if (abbrev(tcp) && elt == max_strlen) {
168 unsigned int nlmsg_len = NLMSG_ALIGN(nlmsghdr.nlmsg_len);
169 kernel_ulong_t next_addr = 0;
170 kernel_ulong_t next_len = 0;
172 if (nlmsghdr.nlmsg_len >= NLMSG_HDRLEN) {
173 next_len = (len >= nlmsg_len) ? len - nlmsg_len : 0;
175 if (next_len && addr + nlmsg_len > addr)
176 next_addr = addr + nlmsg_len;
179 if (!print_array && next_addr) {
184 decode_nlmsghdr_with_payload(tcp, &nlmsghdr, addr, len);