]> granicus.if.org Git - strace/blob - netlink.c
netlink: add decoding of NETLINK_ROUTE message types
[strace] / netlink.c
1 /*
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.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
17  *
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.
28  */
29
30 #include "defs.h"
31 #include <sys/socket.h>
32 #include <linux/audit.h>
33 #include <linux/netlink.h>
34 #include <linux/rtnetlink.h>
35 #include "xlat/netlink_flags.h"
36 #include "xlat/netlink_protocols.h"
37 #include "xlat/netlink_types.h"
38 #include "xlat/nl_audit_types.h"
39 #include "xlat/nl_route_types.h"
40 #include "xlat/nl_sock_diag_types.h"
41
42 #undef NLMSG_HDRLEN
43 #define NLMSG_HDRLEN NLMSG_ALIGN(sizeof(struct nlmsghdr))
44
45 /*
46  * Fetch a struct nlmsghdr from the given address.
47  */
48 static bool
49 fetch_nlmsghdr(struct tcb *const tcp, struct nlmsghdr *const nlmsghdr,
50                const kernel_ulong_t addr, const kernel_ulong_t len)
51 {
52         if (len < sizeof(struct nlmsghdr)) {
53                 printstrn(tcp, addr, len);
54                 return false;
55         }
56
57         if (umove_or_printaddr(tcp, addr, nlmsghdr))
58                 return false;
59
60         return true;
61 }
62
63 enum {
64         NL_FAMILY_ERROR = -1,
65         NL_FAMILY_DEFAULT = -2
66 };
67
68 static int
69 get_fd_nl_family(struct tcb *const tcp, const int fd)
70 {
71         const unsigned long inode = getfdinode(tcp, fd);
72         if (!inode)
73                 return NL_FAMILY_ERROR;
74
75         const char *const details = get_sockaddr_by_inode(tcp, fd, inode);
76         if (!details)
77                 return NL_FAMILY_ERROR;
78
79         const char *const nl_details = STR_STRIP_PREFIX(details, "NETLINK:[");
80         if (nl_details == details)
81                 return NL_FAMILY_ERROR;
82
83         const struct xlat *xlats = netlink_protocols;
84         for (; xlats->str; ++xlats) {
85                 const char *name = STR_STRIP_PREFIX(xlats->str, "NETLINK_");
86                 if (!strncmp(nl_details, name, strlen(name)))
87                         return xlats->val;
88         }
89
90         if (*nl_details >= '0' && *nl_details <= '9')
91                 return atoi(nl_details);
92
93         return NL_FAMILY_ERROR;
94 }
95
96 static const struct {
97         const struct xlat *const xlat;
98         const char *const dflt;
99 } nlmsg_types[] = {
100         [NETLINK_AUDIT] = { nl_audit_types, "AUDIT_???" },
101         [NETLINK_ROUTE] = { nl_route_types, "RTM_???" },
102         [NETLINK_SOCK_DIAG] = { nl_sock_diag_types, "SOCK_DIAG_???" }
103 };
104
105 /*
106  * As all valid netlink families are positive integers, use unsigned int
107  * for family here to filter out NL_FAMILY_ERROR and NL_FAMILY_DEFAULT.
108  */
109 static void
110 decode_nlmsg_type(const uint16_t type, const unsigned int family)
111 {
112         if (family < ARRAY_SIZE(nlmsg_types)
113             && nlmsg_types[family].xlat) {
114                 printxval(nlmsg_types[family].xlat, type, nlmsg_types[family].dflt);
115         } else {
116                 printxval(netlink_types, type, "NLMSG_???");
117         }
118 }
119
120 static int
121 print_nlmsghdr(struct tcb *tcp,
122                const int fd,
123                int family,
124                const struct nlmsghdr *const nlmsghdr)
125 {
126         /* print the whole structure regardless of its nlmsg_len */
127
128         tprintf("{len=%u, type=", nlmsghdr->nlmsg_len);
129
130         const int hdr_family = (nlmsghdr->nlmsg_type < NLMSG_MIN_TYPE)
131                                ? NL_FAMILY_DEFAULT
132                                : (family != NL_FAMILY_DEFAULT
133                                   ? family : get_fd_nl_family(tcp, fd));
134
135         decode_nlmsg_type(nlmsghdr->nlmsg_type, hdr_family);
136
137         tprints(", flags=");
138         printflags(netlink_flags, nlmsghdr->nlmsg_flags, "NLM_F_???");
139
140         tprintf(", seq=%u, pid=%u}", nlmsghdr->nlmsg_seq,
141                 nlmsghdr->nlmsg_pid);
142
143         return family != NL_FAMILY_DEFAULT ? family : hdr_family;
144 }
145
146 static void
147 decode_nlmsghdr_with_payload(struct tcb *const tcp,
148                              const int fd,
149                              int family,
150                              const struct nlmsghdr *const nlmsghdr,
151                              const kernel_ulong_t addr,
152                              const kernel_ulong_t len);
153
154 static void
155 decode_nlmsgerr(struct tcb *const tcp,
156                 const int fd,
157                 const int family,
158                 kernel_ulong_t addr,
159                 kernel_ulong_t len)
160 {
161         struct nlmsgerr err;
162
163         if (len < sizeof(err.error)) {
164                 printstrn(tcp, addr, len);
165                 return;
166         }
167
168         if (umove_or_printaddr(tcp, addr, &err.error))
169                 return;
170
171         tprints("{error=");
172         if (err.error < 0 && (unsigned) -err.error < nerrnos) {
173                 tprintf("-%s", errnoent[-err.error]);
174         } else {
175                 tprintf("%d", err.error);
176         }
177
178         addr += offsetof(struct nlmsgerr, msg);
179         len -= offsetof(struct nlmsgerr, msg);
180
181         if (len) {
182                 tprints(", msg=");
183                 if (fetch_nlmsghdr(tcp, &err.msg, addr, len)) {
184                         decode_nlmsghdr_with_payload(tcp, fd, family,
185                                                      &err.msg, addr, len);
186                 }
187         }
188
189         tprints("}");
190 }
191
192 static void
193 decode_payload(struct tcb *const tcp,
194                const int fd,
195                const int family,
196                const struct nlmsghdr *const nlmsghdr,
197                const kernel_ulong_t addr,
198                const kernel_ulong_t len)
199 {
200         if (nlmsghdr->nlmsg_type == NLMSG_ERROR) {
201                 decode_nlmsgerr(tcp, fd, family, addr, len);
202                 return;
203         } else if (nlmsghdr->nlmsg_type == NLMSG_DONE && len == sizeof(int)) {
204                 int num;
205
206                 if (!umove_or_printaddr(tcp, addr, &num))
207                         tprintf("%d", num);
208                 return;
209         }
210
211         printstrn(tcp, addr, len);
212 }
213
214 static void
215 decode_nlmsghdr_with_payload(struct tcb *const tcp,
216                              const int fd,
217                              int family,
218                              const struct nlmsghdr *const nlmsghdr,
219                              const kernel_ulong_t addr,
220                              const kernel_ulong_t len)
221 {
222         tprints("{");
223
224         family = print_nlmsghdr(tcp, fd, family, nlmsghdr);
225
226         unsigned int nlmsg_len =
227                 nlmsghdr->nlmsg_len > len ? len : nlmsghdr->nlmsg_len;
228         if (nlmsg_len > NLMSG_HDRLEN) {
229                 tprints(", ");
230                 decode_payload(tcp, fd, family, nlmsghdr, addr + NLMSG_HDRLEN,
231                                                      nlmsg_len - NLMSG_HDRLEN);
232         }
233
234         tprints("}");
235 }
236
237 void
238 decode_netlink(struct tcb *const tcp,
239                const int fd,
240                kernel_ulong_t addr,
241                kernel_ulong_t len)
242 {
243         struct nlmsghdr nlmsghdr;
244         bool print_array = false;
245         unsigned int elt;
246
247         for (elt = 0; fetch_nlmsghdr(tcp, &nlmsghdr, addr, len); elt++) {
248                 if (abbrev(tcp) && elt == max_strlen) {
249                         tprints("...");
250                         break;
251                 }
252
253                 unsigned int nlmsg_len = NLMSG_ALIGN(nlmsghdr.nlmsg_len);
254                 kernel_ulong_t next_addr = 0;
255                 kernel_ulong_t next_len = 0;
256
257                 if (nlmsghdr.nlmsg_len >= NLMSG_HDRLEN) {
258                         next_len = (len >= nlmsg_len) ? len - nlmsg_len : 0;
259
260                         if (next_len && addr + nlmsg_len > addr)
261                                 next_addr = addr + nlmsg_len;
262                 }
263
264                 if (!print_array && next_addr) {
265                         tprints("[");
266                         print_array = true;
267                 }
268
269                 decode_nlmsghdr_with_payload(tcp, fd, NL_FAMILY_DEFAULT,
270                                              &nlmsghdr, addr, len);
271
272                 if (!next_addr)
273                         break;
274
275                 tprints(", ");
276                 addr = next_addr;
277                 len = next_len;
278         }
279
280         if (print_array) {
281                 tprints("]");
282         }
283 }