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