]> granicus.if.org Git - strace/blob - netlink.c
netlink: introduce family specific decoder of NETLINK_ROUTE
[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 "netlink.h"
32 #include "nlattr.h"
33 #include <linux/audit.h>
34 #include <linux/rtnetlink.h>
35 #include <linux/xfrm.h>
36 #include "xlat/netlink_ack_flags.h"
37 #include "xlat/netlink_flags.h"
38 #include "xlat/netlink_get_flags.h"
39 #include "xlat/netlink_new_flags.h"
40 #include "xlat/netlink_protocols.h"
41 #include "xlat/netlink_types.h"
42 #include "xlat/nl_audit_types.h"
43 #include "xlat/nl_crypto_types.h"
44 #include "xlat/nl_netfilter_msg_types.h"
45 #include "xlat/nl_netfilter_subsys_ids.h"
46 #include "xlat/nl_route_types.h"
47 #include "xlat/nl_selinux_types.h"
48 #include "xlat/nl_sock_diag_types.h"
49 #include "xlat/nl_xfrm_types.h"
50 #include "xlat/nlmsgerr_attrs.h"
51
52 /*
53  * Fetch a struct nlmsghdr from the given address.
54  */
55 static bool
56 fetch_nlmsghdr(struct tcb *const tcp, struct nlmsghdr *const nlmsghdr,
57                const kernel_ulong_t addr, const kernel_ulong_t len)
58 {
59         if (len < sizeof(struct nlmsghdr)) {
60                 printstr_ex(tcp, addr, len, QUOTE_FORCE_HEX);
61                 return false;
62         }
63
64         if (umove_or_printaddr(tcp, addr, nlmsghdr))
65                 return false;
66
67         return true;
68 }
69
70 static int
71 get_fd_nl_family(struct tcb *const tcp, const int fd)
72 {
73         const unsigned long inode = getfdinode(tcp, fd);
74         if (!inode)
75                 return -1;
76
77         const char *const details = get_sockaddr_by_inode(tcp, fd, inode);
78         if (!details)
79                 return -1;
80
81         const char *const nl_details = STR_STRIP_PREFIX(details, "NETLINK:[");
82         if (nl_details == details)
83                 return -1;
84
85         const struct xlat *xlats = netlink_protocols;
86         for (; xlats->str; ++xlats) {
87                 const char *name = STR_STRIP_PREFIX(xlats->str, "NETLINK_");
88                 if (!strncmp(nl_details, name, strlen(name)))
89                         return xlats->val;
90         }
91
92         if (*nl_details >= '0' && *nl_details <= '9')
93                 return atoi(nl_details);
94
95         return -1;
96 }
97
98 static void
99 decode_nlmsg_type_default(const struct xlat *const xlat,
100                           const uint16_t type,
101                           const char *const dflt)
102 {
103         printxval(xlat, type, dflt);
104 }
105
106 static void
107 decode_nlmsg_type_generic(const struct xlat *const xlat,
108                           const uint16_t type,
109                           const char *const dflt)
110 {
111         printxval(genl_families_xlat(), type, dflt);
112 }
113
114 static void
115 decode_nlmsg_type_netfilter(const struct xlat *const xlat,
116                             const uint16_t type,
117                             const char *const dflt)
118 {
119         /* Reserved control nfnetlink messages first. */
120         const char *const text = xlookup(nl_netfilter_msg_types, type);
121         if (text) {
122                 tprints(text);
123                 return;
124         }
125
126         /*
127          * Other netfilter message types are split
128          * in two pieces: 8 bits subsystem and 8 bits type.
129          */
130         const uint8_t subsys_id = (uint8_t) (type >> 8);
131         const uint8_t msg_type = (uint8_t) type;
132
133         printxval(xlat, subsys_id, dflt);
134
135         /*
136          * The type is subsystem specific,
137          * print it in numeric format for now.
138          */
139         tprintf("<<8|%#x", msg_type);
140 }
141
142 typedef void (*nlmsg_types_decoder_t)(const struct xlat *,
143                                       uint16_t type,
144                                       const char *dflt);
145
146 static const struct {
147         const nlmsg_types_decoder_t decoder;
148         const struct xlat *const xlat;
149         const char *const dflt;
150 } nlmsg_types[] = {
151         [NETLINK_AUDIT] = { NULL, nl_audit_types, "AUDIT_???" },
152         [NETLINK_CRYPTO] = { NULL, nl_crypto_types, "CRYPTO_MSG_???" },
153         [NETLINK_GENERIC] = {
154                 decode_nlmsg_type_generic,
155                 NULL,
156                 "GENERIC_FAMILY_???"
157         },
158         [NETLINK_NETFILTER] = {
159                 decode_nlmsg_type_netfilter,
160                 nl_netfilter_subsys_ids,
161                 "NFNL_SUBSYS_???"
162         },
163         [NETLINK_ROUTE] = { NULL, nl_route_types, "RTM_???" },
164         [NETLINK_SELINUX] = { NULL, nl_selinux_types, "SELNL_MSG_???" },
165         [NETLINK_SOCK_DIAG] = { NULL, nl_sock_diag_types, "SOCK_DIAG_???" },
166         [NETLINK_XFRM] = { NULL, nl_xfrm_types, "XFRM_MSG_???" }
167 };
168
169 /*
170  * As all valid netlink families are positive integers, use unsigned int
171  * for family here to filter out -1.
172  */
173 static void
174 decode_nlmsg_type(const uint16_t type, const unsigned int family)
175 {
176         nlmsg_types_decoder_t decoder = decode_nlmsg_type_default;
177         const struct xlat *xlat = netlink_types;
178         const char *dflt = "NLMSG_???";
179
180         /*
181          * type < NLMSG_MIN_TYPE are reserved control messages
182          * that need no family-specific decoding.
183          */
184         if (type >= NLMSG_MIN_TYPE && family < ARRAY_SIZE(nlmsg_types)) {
185                 if (nlmsg_types[family].decoder)
186                         decoder = nlmsg_types[family].decoder;
187                 if (nlmsg_types[family].xlat)
188                         xlat = nlmsg_types[family].xlat;
189                 if (nlmsg_types[family].dflt)
190                         dflt = nlmsg_types[family].dflt;
191         }
192
193         decoder(xlat, type, dflt);
194 }
195
196 static void
197 decode_nlmsg_flags(const uint16_t flags, const uint16_t type, const int family)
198 {
199         const struct xlat *table = NULL;
200
201         if (type < NLMSG_MIN_TYPE) {
202                 if (type == NLMSG_ERROR)
203                         table = netlink_ack_flags;
204                 goto end;
205         }
206
207         switch (family) {
208         case NETLINK_CRYPTO:
209                 switch (type) {
210                 case CRYPTO_MSG_NEWALG:
211                         table = netlink_new_flags;
212                         break;
213                 case CRYPTO_MSG_GETALG:
214                         table = netlink_get_flags;
215                         break;
216                 }
217                 break;
218         case NETLINK_SOCK_DIAG:
219                 table = netlink_get_flags;
220                 break;
221         case NETLINK_ROUTE:
222                 if (type == RTM_DELACTION) {
223                         table = netlink_get_flags;
224                         break;
225                 }
226                 switch (type & 3) {
227                 case  0:
228                         table = netlink_new_flags;
229                         break;
230                 case  2:
231                         table = netlink_get_flags;
232                         break;
233                 }
234                 break;
235         case NETLINK_XFRM:
236                 switch (type) {
237                 case XFRM_MSG_NEWSA:
238                 case XFRM_MSG_NEWPOLICY:
239                 case XFRM_MSG_NEWAE:
240                 case XFRM_MSG_NEWSADINFO:
241                 case XFRM_MSG_NEWSPDINFO:
242                         table = netlink_new_flags;
243                         break;
244
245                 case XFRM_MSG_GETSA:
246                 case XFRM_MSG_GETPOLICY:
247                 case XFRM_MSG_GETAE:
248                 case XFRM_MSG_GETSADINFO:
249                 case XFRM_MSG_GETSPDINFO:
250                         table = netlink_get_flags;
251                         break;
252                 }
253                 break;
254         }
255
256 end:
257         printflags_ex(flags, "NLM_F_???", netlink_flags, table, NULL);
258 }
259
260 static void
261 print_nlmsghdr(struct tcb *tcp,
262                const int fd,
263                const int family,
264                const struct nlmsghdr *const nlmsghdr)
265 {
266         /* print the whole structure regardless of its nlmsg_len */
267
268         tprintf("{len=%u, type=", nlmsghdr->nlmsg_len);
269
270         decode_nlmsg_type(nlmsghdr->nlmsg_type, family);
271
272         tprints(", flags=");
273         decode_nlmsg_flags(nlmsghdr->nlmsg_flags,
274                            nlmsghdr->nlmsg_type, family);
275
276         tprintf(", seq=%u, pid=%u}", nlmsghdr->nlmsg_seq,
277                 nlmsghdr->nlmsg_pid);
278 }
279
280 static bool
281 print_cookie(struct tcb *const tcp, void *const elem_buf,
282              const size_t elem_size, void *const opaque_data)
283 {
284         tprintf("%" PRIu8, *(uint8_t *) elem_buf);
285
286         return true;
287 }
288
289 static bool
290 decode_nlmsgerr_attr_cookie(struct tcb *const tcp,
291                             const kernel_ulong_t addr,
292                             const unsigned int len,
293                             const void *const opaque_data)
294 {
295         uint8_t cookie;
296         const size_t nmemb = len / sizeof(cookie);
297
298         print_array(tcp, addr, nmemb, &cookie, sizeof(cookie),
299                     umoven_or_printaddr, print_cookie, 0);
300
301         return true;
302 }
303
304 static const nla_decoder_t nlmsgerr_nla_decoders[] = {
305         [NLMSGERR_ATTR_MSG]     = decode_nla_str,
306         [NLMSGERR_ATTR_OFFS]    = decode_nla_u32,
307         [NLMSGERR_ATTR_COOKIE]  = decode_nlmsgerr_attr_cookie
308 };
309
310 static void
311 decode_nlmsghdr_with_payload(struct tcb *const tcp,
312                              const int fd,
313                              const int family,
314                              const struct nlmsghdr *const nlmsghdr,
315                              const kernel_ulong_t addr,
316                              const kernel_ulong_t len);
317
318 static void
319 decode_nlmsgerr(struct tcb *const tcp,
320                 const int fd,
321                 const int family,
322                 kernel_ulong_t addr,
323                 unsigned int len,
324                 const bool capped)
325 {
326         struct nlmsgerr err;
327
328         if (len < sizeof(err.error)) {
329                 printstr_ex(tcp, addr, len, QUOTE_FORCE_HEX);
330                 return;
331         }
332
333         if (umove_or_printaddr(tcp, addr, &err.error))
334                 return;
335
336         tprints("{error=");
337         if (err.error < 0 && (unsigned) -err.error < nerrnos) {
338                 tprintf("-%s", errnoent[-err.error]);
339         } else {
340                 tprintf("%d", err.error);
341         }
342
343         addr += offsetof(struct nlmsgerr, msg);
344         len -= offsetof(struct nlmsgerr, msg);
345
346         if (len) {
347                 tprints(", msg=");
348                 if (fetch_nlmsghdr(tcp, &err.msg, addr, len)) {
349                         unsigned int payload =
350                                 capped ? sizeof(err.msg) : err.msg.nlmsg_len;
351                         if (payload > len)
352                                 payload = len;
353
354                         decode_nlmsghdr_with_payload(tcp, fd, family,
355                                                      &err.msg, addr, payload);
356                         if (len > payload) {
357                                 tprints(", ");
358                                 decode_nlattr(tcp, addr + payload,
359                                               len - payload, nlmsgerr_attrs,
360                                               "NLMSGERR_ATTR_???",
361                                               nlmsgerr_nla_decoders,
362                                               ARRAY_SIZE(nlmsgerr_nla_decoders),
363                                               NULL);
364                         }
365                 }
366         }
367
368         tprints("}");
369 }
370
371 static const netlink_decoder_t netlink_decoders[] = {
372 #ifdef HAVE_LINUX_CRYPTOUSER_H
373         [NETLINK_CRYPTO] = decode_netlink_crypto,
374 #endif
375         [NETLINK_ROUTE] = decode_netlink_route,
376         [NETLINK_SELINUX] = decode_netlink_selinux,
377         [NETLINK_SOCK_DIAG] = decode_netlink_sock_diag
378 };
379
380 static void
381 decode_payload(struct tcb *const tcp,
382                const int fd,
383                const int family,
384                const struct nlmsghdr *const nlmsghdr,
385                const kernel_ulong_t addr,
386                const unsigned int len)
387 {
388         if (nlmsghdr->nlmsg_type == NLMSG_ERROR) {
389                 decode_nlmsgerr(tcp, fd, family, addr, len,
390                                 nlmsghdr->nlmsg_flags & NLM_F_CAPPED);
391                 return;
392         }
393
394         /*
395          * While most of NLMSG_DONE messages indeed have payloads
396          * containing just a single integer, there are few exceptions,
397          * so pass payloads of NLMSG_DONE messages to family-specific
398          * netlink payload decoders.
399          *
400          * Other types of reserved control messages need no family-specific
401          * netlink payload decoding.
402          */
403         if ((nlmsghdr->nlmsg_type >= NLMSG_MIN_TYPE
404             || nlmsghdr->nlmsg_type == NLMSG_DONE)
405             && (unsigned int) family < ARRAY_SIZE(netlink_decoders)
406             && netlink_decoders[family]
407             && netlink_decoders[family](tcp, nlmsghdr, addr, len)) {
408                 return;
409         }
410
411         if (nlmsghdr->nlmsg_type == NLMSG_DONE && len == sizeof(int)) {
412                 int num;
413
414                 if (!umove_or_printaddr(tcp, addr, &num))
415                         tprintf("%d", num);
416                 return;
417         }
418
419         printstr_ex(tcp, addr, len, QUOTE_FORCE_HEX);
420 }
421
422 static void
423 decode_nlmsghdr_with_payload(struct tcb *const tcp,
424                              const int fd,
425                              const int family,
426                              const struct nlmsghdr *const nlmsghdr,
427                              const kernel_ulong_t addr,
428                              const kernel_ulong_t len)
429 {
430         const unsigned int nlmsg_len =
431                 nlmsghdr->nlmsg_len > len ? len : nlmsghdr->nlmsg_len;
432
433         if (nlmsg_len > NLMSG_HDRLEN)
434                 tprints("{");
435
436         print_nlmsghdr(tcp, fd, family, nlmsghdr);
437
438         if (nlmsg_len > NLMSG_HDRLEN) {
439                 tprints(", ");
440                 decode_payload(tcp, fd, family, nlmsghdr, addr + NLMSG_HDRLEN,
441                                                      nlmsg_len - NLMSG_HDRLEN);
442                 tprints("}");
443         }
444 }
445
446 void
447 decode_netlink(struct tcb *const tcp,
448                const int fd,
449                kernel_ulong_t addr,
450                kernel_ulong_t len)
451 {
452         const int family = get_fd_nl_family(tcp, fd);
453
454         if (family == NETLINK_KOBJECT_UEVENT) {
455                 printstrn(tcp, addr, len);
456                 return;
457         }
458
459         struct nlmsghdr nlmsghdr;
460         bool print_array = false;
461         unsigned int elt;
462
463         for (elt = 0; fetch_nlmsghdr(tcp, &nlmsghdr, addr, len); elt++) {
464                 if (abbrev(tcp) && elt == max_strlen) {
465                         tprints("...");
466                         break;
467                 }
468
469                 unsigned int nlmsg_len = NLMSG_ALIGN(nlmsghdr.nlmsg_len);
470                 kernel_ulong_t next_addr = 0;
471                 kernel_ulong_t next_len = 0;
472
473                 if (nlmsghdr.nlmsg_len >= NLMSG_HDRLEN) {
474                         next_len = (len >= nlmsg_len) ? len - nlmsg_len : 0;
475
476                         if (next_len && addr + nlmsg_len > addr)
477                                 next_addr = addr + nlmsg_len;
478                 }
479
480                 if (!print_array && next_addr) {
481                         tprints("[");
482                         print_array = true;
483                 }
484
485                 decode_nlmsghdr_with_payload(tcp, fd, family,
486                                              &nlmsghdr, addr, len);
487
488                 if (!next_addr)
489                         break;
490
491                 tprints(", ");
492                 addr = next_addr;
493                 len = next_len;
494         }
495
496         if (print_array) {
497                 tprints("]");
498         }
499 }