]> granicus.if.org Git - strace/blob - netlink.c
Introduce generic STRINGIFY and STRINGIFY_VAL macros
[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 <linux/audit.h>
33 #include <linux/rtnetlink.h>
34 #include <linux/xfrm.h>
35 #include "xlat/netlink_flags.h"
36 #include "xlat/netlink_get_flags.h"
37 #include "xlat/netlink_new_flags.h"
38 #include "xlat/netlink_protocols.h"
39 #include "xlat/netlink_types.h"
40 #include "xlat/nl_audit_types.h"
41 #include "xlat/nl_netfilter_msg_types.h"
42 #include "xlat/nl_netfilter_subsys_ids.h"
43 #include "xlat/nl_route_types.h"
44 #include "xlat/nl_selinux_types.h"
45 #include "xlat/nl_sock_diag_types.h"
46 #include "xlat/nl_xfrm_types.h"
47
48 /*
49  * Fetch a struct nlmsghdr from the given address.
50  */
51 static bool
52 fetch_nlmsghdr(struct tcb *const tcp, struct nlmsghdr *const nlmsghdr,
53                const kernel_ulong_t addr, const kernel_ulong_t len)
54 {
55         if (len < sizeof(struct nlmsghdr)) {
56                 printstrn(tcp, addr, len);
57                 return false;
58         }
59
60         if (umove_or_printaddr(tcp, addr, nlmsghdr))
61                 return false;
62
63         return true;
64 }
65
66 enum {
67         NL_FAMILY_ERROR = -1,
68         NL_FAMILY_DEFAULT = -2
69 };
70
71 static int
72 get_fd_nl_family(struct tcb *const tcp, const int fd)
73 {
74         const unsigned long inode = getfdinode(tcp, fd);
75         if (!inode)
76                 return NL_FAMILY_ERROR;
77
78         const char *const details = get_sockaddr_by_inode(tcp, fd, inode);
79         if (!details)
80                 return NL_FAMILY_ERROR;
81
82         const char *const nl_details = STR_STRIP_PREFIX(details, "NETLINK:[");
83         if (nl_details == details)
84                 return NL_FAMILY_ERROR;
85
86         const struct xlat *xlats = netlink_protocols;
87         for (; xlats->str; ++xlats) {
88                 const char *name = STR_STRIP_PREFIX(xlats->str, "NETLINK_");
89                 if (!strncmp(nl_details, name, strlen(name)))
90                         return xlats->val;
91         }
92
93         if (*nl_details >= '0' && *nl_details <= '9')
94                 return atoi(nl_details);
95
96         return NL_FAMILY_ERROR;
97 }
98
99 static void
100 decode_nlmsg_type_default(const struct xlat *const xlat,
101                           const uint16_t type,
102                           const char *const dflt)
103 {
104         printxval(xlat, type, dflt);
105 }
106
107 static void
108 decode_nlmsg_type_generic(const struct xlat *const xlat,
109                           const uint16_t type,
110                           const char *const dflt)
111 {
112         printxval(genl_families_xlat(), type, dflt);
113 }
114
115 static void
116 decode_nlmsg_type_netfilter(const struct xlat *const xlat,
117                             const uint16_t type,
118                             const char *const dflt)
119 {
120         /* Reserved control nfnetlink messages first. */
121         const char *const text = xlookup(nl_netfilter_msg_types, type);
122         if (text) {
123                 tprints(text);
124                 return;
125         }
126
127         /*
128          * Other netfilter message types are split
129          * in two pieces: 8 bits subsystem and 8 bits type.
130          */
131         const uint8_t subsys_id = (uint8_t) (type >> 8);
132         const uint8_t msg_type = (uint8_t) type;
133
134         printxval(xlat, subsys_id, dflt);
135
136         /*
137          * The type is subsystem specific,
138          * print it in numeric format for now.
139          */
140         tprintf("<<8|%#x", msg_type);
141 }
142
143 typedef void (*nlmsg_types_decoder_t)(const struct xlat *,
144                                       uint16_t type,
145                                       const char *dflt);
146
147 static const struct {
148         const nlmsg_types_decoder_t decoder;
149         const struct xlat *const xlat;
150         const char *const dflt;
151 } nlmsg_types[] = {
152         [NETLINK_AUDIT] = { NULL, nl_audit_types, "AUDIT_???" },
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 NL_FAMILY_ERROR and NL_FAMILY_DEFAULT.
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         if (type != NLMSG_DONE && family < ARRAY_SIZE(nlmsg_types)) {
181                 if (nlmsg_types[family].decoder)
182                         decoder = nlmsg_types[family].decoder;
183                 if (nlmsg_types[family].xlat)
184                         xlat = nlmsg_types[family].xlat;
185                 if (nlmsg_types[family].dflt)
186                         dflt = nlmsg_types[family].dflt;
187         }
188
189         decoder(xlat, type, dflt);
190 }
191
192 static void
193 decode_nlmsg_flags(const uint16_t flags, const uint16_t type, const int family)
194 {
195         const struct xlat *table = NULL;
196
197         if (type == NLMSG_DONE)
198                 goto end;
199
200         switch (family) {
201         case NETLINK_SOCK_DIAG:
202                 table = netlink_get_flags;
203                 break;
204         case NETLINK_ROUTE:
205                 if (type == RTM_DELACTION) {
206                         table = netlink_get_flags;
207                         break;
208                 }
209                 switch (type & 3) {
210                 case  0:
211                         table = netlink_new_flags;
212                         break;
213                 case  2:
214                         table = netlink_get_flags;
215                         break;
216                 }
217                 break;
218         case NETLINK_XFRM:
219                 switch (type) {
220                 case XFRM_MSG_NEWSA:
221                 case XFRM_MSG_NEWPOLICY:
222                 case XFRM_MSG_NEWAE:
223                 case XFRM_MSG_NEWSADINFO:
224                 case XFRM_MSG_NEWSPDINFO:
225                         table = netlink_new_flags;
226                         break;
227
228                 case XFRM_MSG_GETSA:
229                 case XFRM_MSG_GETPOLICY:
230                 case XFRM_MSG_GETAE:
231                 case XFRM_MSG_GETSADINFO:
232                 case XFRM_MSG_GETSPDINFO:
233                         table = netlink_get_flags;
234                         break;
235                 }
236                 break;
237         }
238
239 end:
240         printflags_ex(flags, "NLM_F_???", netlink_flags, table, NULL);
241 }
242
243 static int
244 print_nlmsghdr(struct tcb *tcp,
245                const int fd,
246                int family,
247                const struct nlmsghdr *const nlmsghdr)
248 {
249         /* print the whole structure regardless of its nlmsg_len */
250
251         tprintf("{len=%u, type=", nlmsghdr->nlmsg_len);
252
253         const int hdr_family = (nlmsghdr->nlmsg_type < NLMSG_MIN_TYPE
254                                 && nlmsghdr->nlmsg_type != NLMSG_DONE)
255                                ? NL_FAMILY_DEFAULT
256                                : (family != NL_FAMILY_DEFAULT
257                                   ? family : get_fd_nl_family(tcp, fd));
258
259         decode_nlmsg_type(nlmsghdr->nlmsg_type, hdr_family);
260
261         tprints(", flags=");
262         decode_nlmsg_flags(nlmsghdr->nlmsg_flags,
263                            nlmsghdr->nlmsg_type, hdr_family);
264
265         tprintf(", seq=%u, pid=%u}", nlmsghdr->nlmsg_seq,
266                 nlmsghdr->nlmsg_pid);
267
268         return family != NL_FAMILY_DEFAULT ? family : hdr_family;
269 }
270
271 static void
272 decode_nlmsghdr_with_payload(struct tcb *const tcp,
273                              const int fd,
274                              int family,
275                              const struct nlmsghdr *const nlmsghdr,
276                              const kernel_ulong_t addr,
277                              const kernel_ulong_t len);
278
279 static void
280 decode_nlmsgerr(struct tcb *const tcp,
281                 const int fd,
282                 const int family,
283                 kernel_ulong_t addr,
284                 kernel_ulong_t len)
285 {
286         struct nlmsgerr err;
287
288         if (len < sizeof(err.error)) {
289                 printstrn(tcp, addr, len);
290                 return;
291         }
292
293         if (umove_or_printaddr(tcp, addr, &err.error))
294                 return;
295
296         tprints("{error=");
297         if (err.error < 0 && (unsigned) -err.error < nerrnos) {
298                 tprintf("-%s", errnoent[-err.error]);
299         } else {
300                 tprintf("%d", err.error);
301         }
302
303         addr += offsetof(struct nlmsgerr, msg);
304         len -= offsetof(struct nlmsgerr, msg);
305
306         if (len) {
307                 tprints(", msg=");
308                 if (fetch_nlmsghdr(tcp, &err.msg, addr, len)) {
309                         decode_nlmsghdr_with_payload(tcp, fd, family,
310                                                      &err.msg, addr, len);
311                 }
312         }
313
314         tprints("}");
315 }
316
317 static const netlink_decoder_t netlink_decoders[] = {
318         [NETLINK_SOCK_DIAG] = decode_netlink_sock_diag
319 };
320
321 static void
322 decode_payload(struct tcb *const tcp,
323                const int fd,
324                const int family,
325                const struct nlmsghdr *const nlmsghdr,
326                const kernel_ulong_t addr,
327                const kernel_ulong_t len)
328 {
329         if (nlmsghdr->nlmsg_type == NLMSG_ERROR) {
330                 decode_nlmsgerr(tcp, fd, family, addr, len);
331                 return;
332         }
333
334         if ((unsigned int) family < ARRAY_SIZE(netlink_decoders)
335             && netlink_decoders[family]
336             && netlink_decoders[family](tcp, nlmsghdr, addr, len)) {
337                 return;
338         }
339
340         if (nlmsghdr->nlmsg_type == NLMSG_DONE && len == sizeof(int)) {
341                 int num;
342
343                 if (!umove_or_printaddr(tcp, addr, &num))
344                         tprintf("%d", num);
345                 return;
346         }
347
348         printstrn(tcp, addr, len);
349 }
350
351 static void
352 decode_nlmsghdr_with_payload(struct tcb *const tcp,
353                              const int fd,
354                              int family,
355                              const struct nlmsghdr *const nlmsghdr,
356                              const kernel_ulong_t addr,
357                              const kernel_ulong_t len)
358 {
359         const unsigned int nlmsg_len =
360                 nlmsghdr->nlmsg_len > len ? len : nlmsghdr->nlmsg_len;
361
362         if (nlmsg_len > NLMSG_HDRLEN)
363                 tprints("{");
364
365         family = print_nlmsghdr(tcp, fd, family, nlmsghdr);
366
367         if (nlmsg_len > NLMSG_HDRLEN) {
368                 tprints(", ");
369                 decode_payload(tcp, fd, family, nlmsghdr, addr + NLMSG_HDRLEN,
370                                                      nlmsg_len - NLMSG_HDRLEN);
371                 tprints("}");
372         }
373 }
374
375 void
376 decode_netlink(struct tcb *const tcp,
377                const int fd,
378                kernel_ulong_t addr,
379                kernel_ulong_t len)
380 {
381         struct nlmsghdr nlmsghdr;
382         bool print_array = false;
383         unsigned int elt;
384
385         for (elt = 0; fetch_nlmsghdr(tcp, &nlmsghdr, addr, len); elt++) {
386                 if (abbrev(tcp) && elt == max_strlen) {
387                         tprints("...");
388                         break;
389                 }
390
391                 unsigned int nlmsg_len = NLMSG_ALIGN(nlmsghdr.nlmsg_len);
392                 kernel_ulong_t next_addr = 0;
393                 kernel_ulong_t next_len = 0;
394
395                 if (nlmsghdr.nlmsg_len >= NLMSG_HDRLEN) {
396                         next_len = (len >= nlmsg_len) ? len - nlmsg_len : 0;
397
398                         if (next_len && addr + nlmsg_len > addr)
399                                 next_addr = addr + nlmsg_len;
400                 }
401
402                 if (!print_array && next_addr) {
403                         tprints("[");
404                         print_array = true;
405                 }
406
407                 decode_nlmsghdr_with_payload(tcp, fd, NL_FAMILY_DEFAULT,
408                                              &nlmsghdr, addr, len);
409
410                 if (!next_addr)
411                         break;
412
413                 tprints(", ");
414                 addr = next_addr;
415                 len = next_len;
416         }
417
418         if (print_array) {
419                 tprints("]");
420         }
421 }