blob: 179a9997b2b695906e61f2dcb0867a5eecf1340b [file] [log] [blame]
Fabien Siron2850f742016-07-06 15:49:22 +00001/*
2 * Copyright (c) 2016 Fabien Siron <fabien.siron@epita.fr>
Dmitry V. Levindeb0c182016-07-08 16:01:49 +00003 * Copyright (c) 2016 Dmitry V. Levin <ldv@altlinux.org>
Elliott Hughes28e98bc2018-06-14 16:59:04 -07004 * Copyright (c) 2016-2018 The strace developers.
Fabien Siron2850f742016-07-06 15:49:22 +00005 * 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"
Elliott Hughesdc75b012017-07-05 13:54:44 -070031#include "netlink.h"
Elliott Hughes77c3ff82017-09-08 17:11:00 -070032#include "nlattr.h"
Elliott Hughesdc75b012017-07-05 13:54:44 -070033#include <linux/audit.h>
34#include <linux/rtnetlink.h>
35#include <linux/xfrm.h>
Elliott Hughes77c3ff82017-09-08 17:11:00 -070036#include "xlat/netlink_ack_flags.h"
Elliott Hughesbbf97dc2017-11-15 16:45:52 -080037#include "xlat/netlink_delete_flags.h"
Fabien Siron2850f742016-07-06 15:49:22 +000038#include "xlat/netlink_flags.h"
Elliott Hughesdc75b012017-07-05 13:54:44 -070039#include "xlat/netlink_get_flags.h"
40#include "xlat/netlink_new_flags.h"
41#include "xlat/netlink_protocols.h"
Fabien Siron2850f742016-07-06 15:49:22 +000042#include "xlat/netlink_types.h"
Elliott Hughesbbf97dc2017-11-15 16:45:52 -080043#include "xlat/nf_acct_msg_types.h"
44#include "xlat/nf_cthelper_msg_types.h"
45#include "xlat/nf_ctnetlink_exp_msg_types.h"
46#include "xlat/nf_ctnetlink_msg_types.h"
47#include "xlat/nf_cttimeout_msg_types.h"
48#include "xlat/nf_ipset_msg_types.h"
49#include "xlat/nf_nft_compat_msg_types.h"
50#include "xlat/nf_nftables_msg_types.h"
51#include "xlat/nf_osf_msg_types.h"
52#include "xlat/nf_queue_msg_types.h"
53#include "xlat/nf_ulog_msg_types.h"
Elliott Hughesdc75b012017-07-05 13:54:44 -070054#include "xlat/nl_audit_types.h"
Elliott Hughes77c3ff82017-09-08 17:11:00 -070055#include "xlat/nl_crypto_types.h"
Elliott Hughesdc75b012017-07-05 13:54:44 -070056#include "xlat/nl_netfilter_subsys_ids.h"
Elliott Hughesdc75b012017-07-05 13:54:44 -070057#include "xlat/nl_selinux_types.h"
58#include "xlat/nl_sock_diag_types.h"
59#include "xlat/nl_xfrm_types.h"
Elliott Hughes77c3ff82017-09-08 17:11:00 -070060#include "xlat/nlmsgerr_attrs.h"
Elliott Hughes39bac052017-05-25 16:56:11 -070061
Dmitry V. Levindeb0c182016-07-08 16:01:49 +000062/*
63 * Fetch a struct nlmsghdr from the given address.
64 */
65static bool
Elliott Hughesd35df492017-02-15 15:19:05 -080066fetch_nlmsghdr(struct tcb *const tcp, struct nlmsghdr *const nlmsghdr,
Elliott Hughes03a418e2018-06-15 13:11:40 -070067 const kernel_ulong_t addr, const kernel_ulong_t len,
68 const bool in_array)
Fabien Siron2850f742016-07-06 15:49:22 +000069{
Dmitry V. Levindeb0c182016-07-08 16:01:49 +000070 if (len < sizeof(struct nlmsghdr)) {
Elliott Hughes77c3ff82017-09-08 17:11:00 -070071 printstr_ex(tcp, addr, len, QUOTE_FORCE_HEX);
Dmitry V. Levindeb0c182016-07-08 16:01:49 +000072 return false;
Fabien Siron2850f742016-07-06 15:49:22 +000073 }
Fabien Siron2850f742016-07-06 15:49:22 +000074
Elliott Hughes03a418e2018-06-15 13:11:40 -070075 if (tfetch_obj(tcp, addr, nlmsghdr))
76 return true;
Fabien Siron2850f742016-07-06 15:49:22 +000077
Elliott Hughes03a418e2018-06-15 13:11:40 -070078 if (in_array) {
79 tprints("...");
80 printaddr_comment(addr);
81 } else {
82 printaddr(addr);
83 }
84
85 return false;
Dmitry V. Levindeb0c182016-07-08 16:01:49 +000086}
87
Elliott Hughesdc75b012017-07-05 13:54:44 -070088static int
89get_fd_nl_family(struct tcb *const tcp, const int fd)
90{
91 const unsigned long inode = getfdinode(tcp, fd);
92 if (!inode)
Elliott Hughes77c3ff82017-09-08 17:11:00 -070093 return -1;
Elliott Hughesdc75b012017-07-05 13:54:44 -070094
95 const char *const details = get_sockaddr_by_inode(tcp, fd, inode);
96 if (!details)
Elliott Hughes77c3ff82017-09-08 17:11:00 -070097 return -1;
Elliott Hughesdc75b012017-07-05 13:54:44 -070098
99 const char *const nl_details = STR_STRIP_PREFIX(details, "NETLINK:[");
100 if (nl_details == details)
Elliott Hughes77c3ff82017-09-08 17:11:00 -0700101 return -1;
Elliott Hughesdc75b012017-07-05 13:54:44 -0700102
103 const struct xlat *xlats = netlink_protocols;
104 for (; xlats->str; ++xlats) {
105 const char *name = STR_STRIP_PREFIX(xlats->str, "NETLINK_");
106 if (!strncmp(nl_details, name, strlen(name)))
107 return xlats->val;
108 }
109
110 if (*nl_details >= '0' && *nl_details <= '9')
111 return atoi(nl_details);
112
Elliott Hughes77c3ff82017-09-08 17:11:00 -0700113 return -1;
Elliott Hughesdc75b012017-07-05 13:54:44 -0700114}
115
Dmitry V. Levindeb0c182016-07-08 16:01:49 +0000116static void
Elliott Hughesb7556142018-02-20 17:03:16 -0800117decode_nlmsg_type_default(struct tcb *tcp, const struct xlat *const xlat,
Elliott Hughesdc75b012017-07-05 13:54:44 -0700118 const uint16_t type,
119 const char *const dflt)
120{
121 printxval(xlat, type, dflt);
122}
123
124static void
Elliott Hughesb7556142018-02-20 17:03:16 -0800125decode_nlmsg_type_generic(struct tcb *tcp, const struct xlat *const xlat,
Elliott Hughesdc75b012017-07-05 13:54:44 -0700126 const uint16_t type,
127 const char *const dflt)
128{
Elliott Hughesb7556142018-02-20 17:03:16 -0800129 printxval(genl_families_xlat(tcp), type, dflt);
Elliott Hughesdc75b012017-07-05 13:54:44 -0700130}
131
Elliott Hughesbbf97dc2017-11-15 16:45:52 -0800132static const struct {
133 const struct xlat *const xlat;
134 const char *const dflt;
135} nf_nlmsg_types[] = {
136 [NFNL_SUBSYS_CTNETLINK] = {
137 nf_ctnetlink_msg_types,
138 "IPCTNL_MSG_CT_???"
139 },
140 [NFNL_SUBSYS_CTNETLINK_EXP] = {
141 nf_ctnetlink_exp_msg_types,
142 "IPCTNL_MSG_EXP_???"
143 },
144 [NFNL_SUBSYS_QUEUE] = { nf_queue_msg_types, "NFQNL_MSG_???" },
145 [NFNL_SUBSYS_ULOG] = { nf_ulog_msg_types, "NFULNL_MSG_???" },
146 [NFNL_SUBSYS_OSF] = { nf_osf_msg_types, "OSF_MSG_???" },
147 [NFNL_SUBSYS_IPSET] = { nf_ipset_msg_types, "IPSET_CMD_???" },
148 [NFNL_SUBSYS_ACCT] = { nf_acct_msg_types, "NFNL_MSG_ACCT_???" },
149 [NFNL_SUBSYS_CTNETLINK_TIMEOUT] = {
150 nf_cttimeout_msg_types,
151 "IPCTNL_MSG_TIMEOUT_???"
152 },
153 [NFNL_SUBSYS_CTHELPER] = {
154 nf_cthelper_msg_types,
155 "NFNL_MSG_CTHELPER_???"
156 },
157 [NFNL_SUBSYS_NFTABLES] = { nf_nftables_msg_types, "NFT_MSG_???" },
158 [NFNL_SUBSYS_NFT_COMPAT] = {
159 nf_nft_compat_msg_types,
160 "NFNL_MSG_COMPAT_???"
161 }
162};
163
Elliott Hughesdc75b012017-07-05 13:54:44 -0700164static void
Elliott Hughesb7556142018-02-20 17:03:16 -0800165decode_nlmsg_type_netfilter(struct tcb *tcp, const struct xlat *const xlat,
Elliott Hughesdc75b012017-07-05 13:54:44 -0700166 const uint16_t type,
167 const char *const dflt)
168{
169 /* Reserved control nfnetlink messages first. */
170 const char *const text = xlookup(nl_netfilter_msg_types, type);
171 if (text) {
Elliott Hughes03a418e2018-06-15 13:11:40 -0700172 print_xlat_ex(type, text, XLAT_STYLE_DEFAULT);
Elliott Hughesdc75b012017-07-05 13:54:44 -0700173 return;
174 }
175
176 /*
177 * Other netfilter message types are split
178 * in two pieces: 8 bits subsystem and 8 bits type.
179 */
180 const uint8_t subsys_id = (uint8_t) (type >> 8);
181 const uint8_t msg_type = (uint8_t) type;
182
183 printxval(xlat, subsys_id, dflt);
184
Elliott Hughesbbf97dc2017-11-15 16:45:52 -0800185 tprints("<<8|");
186 if (subsys_id < ARRAY_SIZE(nf_nlmsg_types))
187 printxval(nf_nlmsg_types[subsys_id].xlat,
188 msg_type, nf_nlmsg_types[subsys_id].dflt);
189 else
190 tprintf("%#x", msg_type);
Elliott Hughesdc75b012017-07-05 13:54:44 -0700191}
192
Elliott Hughesb7556142018-02-20 17:03:16 -0800193typedef void (*nlmsg_types_decoder_t)(struct tcb *, const struct xlat *,
Elliott Hughesdc75b012017-07-05 13:54:44 -0700194 uint16_t type,
195 const char *dflt);
196
197static const struct {
198 const nlmsg_types_decoder_t decoder;
199 const struct xlat *const xlat;
200 const char *const dflt;
201} nlmsg_types[] = {
202 [NETLINK_AUDIT] = { NULL, nl_audit_types, "AUDIT_???" },
Elliott Hughes77c3ff82017-09-08 17:11:00 -0700203 [NETLINK_CRYPTO] = { NULL, nl_crypto_types, "CRYPTO_MSG_???" },
Elliott Hughesdc75b012017-07-05 13:54:44 -0700204 [NETLINK_GENERIC] = {
205 decode_nlmsg_type_generic,
206 NULL,
207 "GENERIC_FAMILY_???"
208 },
209 [NETLINK_NETFILTER] = {
210 decode_nlmsg_type_netfilter,
211 nl_netfilter_subsys_ids,
212 "NFNL_SUBSYS_???"
213 },
214 [NETLINK_ROUTE] = { NULL, nl_route_types, "RTM_???" },
215 [NETLINK_SELINUX] = { NULL, nl_selinux_types, "SELNL_MSG_???" },
216 [NETLINK_SOCK_DIAG] = { NULL, nl_sock_diag_types, "SOCK_DIAG_???" },
217 [NETLINK_XFRM] = { NULL, nl_xfrm_types, "XFRM_MSG_???" }
218};
219
220/*
221 * As all valid netlink families are positive integers, use unsigned int
Elliott Hughes77c3ff82017-09-08 17:11:00 -0700222 * for family here to filter out -1.
Elliott Hughesdc75b012017-07-05 13:54:44 -0700223 */
224static void
Elliott Hughesb7556142018-02-20 17:03:16 -0800225decode_nlmsg_type(struct tcb *tcp, const uint16_t type,
226 const unsigned int family)
Elliott Hughesdc75b012017-07-05 13:54:44 -0700227{
228 nlmsg_types_decoder_t decoder = decode_nlmsg_type_default;
229 const struct xlat *xlat = netlink_types;
230 const char *dflt = "NLMSG_???";
231
Elliott Hughes77c3ff82017-09-08 17:11:00 -0700232 /*
233 * type < NLMSG_MIN_TYPE are reserved control messages
234 * that need no family-specific decoding.
235 */
236 if (type >= NLMSG_MIN_TYPE && family < ARRAY_SIZE(nlmsg_types)) {
Elliott Hughesdc75b012017-07-05 13:54:44 -0700237 if (nlmsg_types[family].decoder)
238 decoder = nlmsg_types[family].decoder;
239 if (nlmsg_types[family].xlat)
240 xlat = nlmsg_types[family].xlat;
241 if (nlmsg_types[family].dflt)
242 dflt = nlmsg_types[family].dflt;
243 }
244
Elliott Hughesb7556142018-02-20 17:03:16 -0800245 decoder(tcp, xlat, type, dflt);
Elliott Hughesdc75b012017-07-05 13:54:44 -0700246}
247
Elliott Hughesbbf97dc2017-11-15 16:45:52 -0800248static const struct xlat *
249decode_nlmsg_flags_crypto(const uint16_t type)
250{
251 switch (type) {
252 case CRYPTO_MSG_NEWALG:
253 return netlink_new_flags;
254 case CRYPTO_MSG_DELALG:
255 case CRYPTO_MSG_DELRNG:
256 return netlink_delete_flags;
257 case CRYPTO_MSG_GETALG:
258 return netlink_get_flags;
259 }
260
261 return NULL;
262}
263
264static const struct xlat *
265decode_nlmsg_flags_netfilter(const uint16_t type)
266{
267 const uint8_t subsys_id = (uint8_t) (type >> 8);
268 const uint8_t msg_type = (uint8_t) type;
269
270 switch (subsys_id) {
271 case NFNL_SUBSYS_CTNETLINK:
272 switch (msg_type) {
273 case IPCTNL_MSG_CT_NEW:
274 return netlink_new_flags;
275 case IPCTNL_MSG_CT_GET:
276 case IPCTNL_MSG_CT_GET_CTRZERO:
277 case IPCTNL_MSG_CT_GET_STATS_CPU:
278 case IPCTNL_MSG_CT_GET_STATS:
279 case IPCTNL_MSG_CT_GET_DYING:
280 case IPCTNL_MSG_CT_GET_UNCONFIRMED:
281 return netlink_get_flags;
282 case IPCTNL_MSG_CT_DELETE:
283 return netlink_delete_flags;
284 }
285 break;
286 case NFNL_SUBSYS_CTNETLINK_EXP:
287 switch (msg_type) {
288 case IPCTNL_MSG_EXP_NEW:
289 return netlink_new_flags;
290 case IPCTNL_MSG_EXP_GET:
291 case IPCTNL_MSG_EXP_GET_STATS_CPU:
292 return netlink_get_flags;
293 case IPCTNL_MSG_EXP_DELETE:
294 return netlink_delete_flags;
295 }
296 break;
297 case NFNL_SUBSYS_ACCT:
298 switch (msg_type) {
299 case NFNL_MSG_ACCT_NEW:
300 return netlink_new_flags;
301 case NFNL_MSG_ACCT_GET:
302 case NFNL_MSG_ACCT_GET_CTRZERO:
303 return netlink_get_flags;
304 case NFNL_MSG_ACCT_DEL:
305 return netlink_delete_flags;
306 }
307 break;
308 case NFNL_SUBSYS_CTNETLINK_TIMEOUT:
309 switch (msg_type) {
310 case IPCTNL_MSG_TIMEOUT_NEW:
311 return netlink_new_flags;
312 case IPCTNL_MSG_TIMEOUT_GET:
313 return netlink_get_flags;
314 case IPCTNL_MSG_TIMEOUT_DELETE:
315 return netlink_delete_flags;
316 }
317 break;
318 case NFNL_SUBSYS_CTHELPER:
319 switch (msg_type) {
320 case NFNL_MSG_CTHELPER_NEW:
321 return netlink_new_flags;
322 case NFNL_MSG_CTHELPER_GET:
323 return netlink_get_flags;
324 case NFNL_MSG_CTHELPER_DEL:
325 return netlink_delete_flags;
326 }
327 break;
328 case NFNL_SUBSYS_NFTABLES:
329 switch (msg_type) {
330 case NFT_MSG_NEWTABLE:
331 case NFT_MSG_NEWCHAIN:
332 case NFT_MSG_NEWRULE:
333 case NFT_MSG_NEWSET:
334 case NFT_MSG_NEWSETELEM:
335 case NFT_MSG_NEWGEN:
336 case NFT_MSG_NEWOBJ:
337 return netlink_new_flags;
338 case NFT_MSG_GETTABLE:
339 case NFT_MSG_GETCHAIN:
340 case NFT_MSG_GETRULE:
341 case NFT_MSG_GETSET:
342 case NFT_MSG_GETSETELEM:
343 case NFT_MSG_GETGEN:
344 case NFT_MSG_GETOBJ:
345 case NFT_MSG_GETOBJ_RESET:
346 return netlink_get_flags;
347 case NFT_MSG_DELTABLE:
348 case NFT_MSG_DELCHAIN:
349 case NFT_MSG_DELRULE:
350 case NFT_MSG_DELSET:
351 case NFT_MSG_DELSETELEM:
352 case NFT_MSG_DELOBJ:
353 return netlink_delete_flags;
354 }
355 break;
356 case NFNL_SUBSYS_NFT_COMPAT:
357 switch (msg_type) {
358 case NFNL_MSG_COMPAT_GET:
359 return netlink_get_flags;
360 }
361 break;
362 }
363
364 return NULL;
365}
366
367static const struct xlat *
368decode_nlmsg_flags_route(const uint16_t type)
369{
370 /* RTM_DELACTION uses NLM_F_ROOT flags */
371 if (type == RTM_DELACTION)
372 return netlink_get_flags;
373 switch (type & 3) {
374 case 0:
375 return netlink_new_flags;
376 case 1:
377 return netlink_delete_flags;
378 case 2:
379 return netlink_get_flags;
380 }
381
382 return NULL;
383}
384
385static const struct xlat *
386decode_nlmsg_flags_sock_diag(const uint16_t type)
387{
388 return netlink_get_flags;
389}
390
391static const struct xlat *
392decode_nlmsg_flags_xfrm(const uint16_t type)
393{
394 switch (type) {
395 case XFRM_MSG_NEWSA:
396 case XFRM_MSG_NEWPOLICY:
397 case XFRM_MSG_NEWAE:
398 case XFRM_MSG_NEWSADINFO:
399 case XFRM_MSG_NEWSPDINFO:
400 return netlink_new_flags;
401 case XFRM_MSG_DELSA:
402 case XFRM_MSG_DELPOLICY:
403 return netlink_delete_flags;
404 case XFRM_MSG_GETSA:
405 case XFRM_MSG_GETPOLICY:
406 case XFRM_MSG_GETAE:
407 case XFRM_MSG_GETSADINFO:
408 case XFRM_MSG_GETSPDINFO:
409 return netlink_get_flags;
410 }
411
412 return NULL;
413}
414
415typedef const struct xlat *(*nlmsg_flags_decoder_t)(const uint16_t type);
416
417static const nlmsg_flags_decoder_t nlmsg_flags[] = {
418 [NETLINK_CRYPTO] = decode_nlmsg_flags_crypto,
419 [NETLINK_NETFILTER] = decode_nlmsg_flags_netfilter,
420 [NETLINK_ROUTE] = decode_nlmsg_flags_route,
421 [NETLINK_SOCK_DIAG] = decode_nlmsg_flags_sock_diag,
422 [NETLINK_XFRM] = decode_nlmsg_flags_xfrm
423};
424
425/*
426 * As all valid netlink families are positive integers, use unsigned int
427 * for family here to filter out -1.
428 */
Elliott Hughesdc75b012017-07-05 13:54:44 -0700429static void
Elliott Hughesbbf97dc2017-11-15 16:45:52 -0800430decode_nlmsg_flags(const uint16_t flags, const uint16_t type,
431 const unsigned int family)
Elliott Hughesdc75b012017-07-05 13:54:44 -0700432{
433 const struct xlat *table = NULL;
434
Elliott Hughes77c3ff82017-09-08 17:11:00 -0700435 if (type < NLMSG_MIN_TYPE) {
436 if (type == NLMSG_ERROR)
437 table = netlink_ack_flags;
Elliott Hughesbbf97dc2017-11-15 16:45:52 -0800438 } else if (family < ARRAY_SIZE(nlmsg_flags) && nlmsg_flags[family])
439 table = nlmsg_flags[family](type);
Elliott Hughesdc75b012017-07-05 13:54:44 -0700440
Elliott Hughesc1873762018-12-19 15:13:36 -0800441 printflags_ex(flags, "NLM_F_???", XLAT_STYLE_DEFAULT,
Elliott Hughes03a418e2018-06-15 13:11:40 -0700442 netlink_flags, table, NULL);
Elliott Hughesdc75b012017-07-05 13:54:44 -0700443}
444
Elliott Hughes77c3ff82017-09-08 17:11:00 -0700445static void
Elliott Hughesdc75b012017-07-05 13:54:44 -0700446print_nlmsghdr(struct tcb *tcp,
447 const int fd,
Elliott Hughes77c3ff82017-09-08 17:11:00 -0700448 const int family,
Elliott Hughesdc75b012017-07-05 13:54:44 -0700449 const struct nlmsghdr *const nlmsghdr)
Dmitry V. Levindeb0c182016-07-08 16:01:49 +0000450{
451 /* print the whole structure regardless of its nlmsg_len */
452
453 tprintf("{len=%u, type=", nlmsghdr->nlmsg_len);
454
Elliott Hughesb7556142018-02-20 17:03:16 -0800455 decode_nlmsg_type(tcp, nlmsghdr->nlmsg_type, family);
Fabien Siron2850f742016-07-06 15:49:22 +0000456
457 tprints(", flags=");
Elliott Hughesdc75b012017-07-05 13:54:44 -0700458 decode_nlmsg_flags(nlmsghdr->nlmsg_flags,
Elliott Hughes77c3ff82017-09-08 17:11:00 -0700459 nlmsghdr->nlmsg_type, family);
Fabien Siron2850f742016-07-06 15:49:22 +0000460
Dmitry V. Levindeb0c182016-07-08 16:01:49 +0000461 tprintf(", seq=%u, pid=%u}", nlmsghdr->nlmsg_seq,
462 nlmsghdr->nlmsg_pid);
463}
Fabien Siron2850f742016-07-06 15:49:22 +0000464
Elliott Hughes77c3ff82017-09-08 17:11:00 -0700465static bool
466print_cookie(struct tcb *const tcp, void *const elem_buf,
467 const size_t elem_size, void *const opaque_data)
468{
469 tprintf("%" PRIu8, *(uint8_t *) elem_buf);
470
471 return true;
472}
473
474static bool
475decode_nlmsgerr_attr_cookie(struct tcb *const tcp,
476 const kernel_ulong_t addr,
477 const unsigned int len,
478 const void *const opaque_data)
479{
480 uint8_t cookie;
481 const size_t nmemb = len / sizeof(cookie);
482
483 print_array(tcp, addr, nmemb, &cookie, sizeof(cookie),
Elliott Hughes03a418e2018-06-15 13:11:40 -0700484 tfetch_mem, print_cookie, 0);
Elliott Hughes77c3ff82017-09-08 17:11:00 -0700485
486 return true;
487}
488
489static const nla_decoder_t nlmsgerr_nla_decoders[] = {
490 [NLMSGERR_ATTR_MSG] = decode_nla_str,
491 [NLMSGERR_ATTR_OFFS] = decode_nla_u32,
492 [NLMSGERR_ATTR_COOKIE] = decode_nlmsgerr_attr_cookie
493};
494
Dmitry V. Levindeb0c182016-07-08 16:01:49 +0000495static void
Elliott Hughesd35df492017-02-15 15:19:05 -0800496decode_nlmsghdr_with_payload(struct tcb *const tcp,
Elliott Hughesdc75b012017-07-05 13:54:44 -0700497 const int fd,
Elliott Hughes77c3ff82017-09-08 17:11:00 -0700498 const int family,
Dmitry V. Levindeb0c182016-07-08 16:01:49 +0000499 const struct nlmsghdr *const nlmsghdr,
Elliott Hughesd35df492017-02-15 15:19:05 -0800500 const kernel_ulong_t addr,
Elliott Hughes39bac052017-05-25 16:56:11 -0700501 const kernel_ulong_t len);
502
503static void
504decode_nlmsgerr(struct tcb *const tcp,
Elliott Hughesdc75b012017-07-05 13:54:44 -0700505 const int fd,
506 const int family,
507 kernel_ulong_t addr,
Elliott Hughes77c3ff82017-09-08 17:11:00 -0700508 unsigned int len,
509 const bool capped)
Elliott Hughes39bac052017-05-25 16:56:11 -0700510{
511 struct nlmsgerr err;
512
Elliott Hughesdc75b012017-07-05 13:54:44 -0700513 if (len < sizeof(err.error)) {
Elliott Hughes77c3ff82017-09-08 17:11:00 -0700514 printstr_ex(tcp, addr, len, QUOTE_FORCE_HEX);
Elliott Hughesdc75b012017-07-05 13:54:44 -0700515 return;
516 }
517
Elliott Hughes39bac052017-05-25 16:56:11 -0700518 if (umove_or_printaddr(tcp, addr, &err.error))
519 return;
520
521 tprints("{error=");
522 if (err.error < 0 && (unsigned) -err.error < nerrnos) {
523 tprintf("-%s", errnoent[-err.error]);
524 } else {
525 tprintf("%d", err.error);
526 }
527
528 addr += offsetof(struct nlmsgerr, msg);
529 len -= offsetof(struct nlmsgerr, msg);
530
531 if (len) {
532 tprints(", msg=");
Elliott Hughes03a418e2018-06-15 13:11:40 -0700533 if (fetch_nlmsghdr(tcp, &err.msg, addr, len, false)) {
Elliott Hughes77c3ff82017-09-08 17:11:00 -0700534 unsigned int payload =
535 capped ? sizeof(err.msg) : err.msg.nlmsg_len;
536 if (payload > len)
537 payload = len;
538
Elliott Hughesdc75b012017-07-05 13:54:44 -0700539 decode_nlmsghdr_with_payload(tcp, fd, family,
Elliott Hughes77c3ff82017-09-08 17:11:00 -0700540 &err.msg, addr, payload);
541 if (len > payload) {
542 tprints(", ");
543 decode_nlattr(tcp, addr + payload,
544 len - payload, nlmsgerr_attrs,
545 "NLMSGERR_ATTR_???",
546 nlmsgerr_nla_decoders,
547 ARRAY_SIZE(nlmsgerr_nla_decoders),
548 NULL);
549 }
Elliott Hughes39bac052017-05-25 16:56:11 -0700550 }
551 }
552
553 tprints("}");
554}
555
Elliott Hughesdc75b012017-07-05 13:54:44 -0700556static const netlink_decoder_t netlink_decoders[] = {
Elliott Hughes77c3ff82017-09-08 17:11:00 -0700557#ifdef HAVE_LINUX_CRYPTOUSER_H
558 [NETLINK_CRYPTO] = decode_netlink_crypto,
559#endif
Elliott Hughes28e98bc2018-06-14 16:59:04 -0700560#ifdef HAVE_LINUX_NETFILTER_NFNETLINK_H
561 [NETLINK_NETFILTER] = decode_netlink_netfilter,
562#endif
Elliott Hughes77c3ff82017-09-08 17:11:00 -0700563 [NETLINK_ROUTE] = decode_netlink_route,
564 [NETLINK_SELINUX] = decode_netlink_selinux,
Elliott Hughesdc75b012017-07-05 13:54:44 -0700565 [NETLINK_SOCK_DIAG] = decode_netlink_sock_diag
566};
567
Elliott Hughes39bac052017-05-25 16:56:11 -0700568static void
569decode_payload(struct tcb *const tcp,
Elliott Hughesdc75b012017-07-05 13:54:44 -0700570 const int fd,
571 const int family,
Elliott Hughes39bac052017-05-25 16:56:11 -0700572 const struct nlmsghdr *const nlmsghdr,
573 const kernel_ulong_t addr,
Elliott Hughes77c3ff82017-09-08 17:11:00 -0700574 const unsigned int len)
Elliott Hughes39bac052017-05-25 16:56:11 -0700575{
Elliott Hughesdc75b012017-07-05 13:54:44 -0700576 if (nlmsghdr->nlmsg_type == NLMSG_ERROR) {
Elliott Hughes77c3ff82017-09-08 17:11:00 -0700577 decode_nlmsgerr(tcp, fd, family, addr, len,
578 nlmsghdr->nlmsg_flags & NLM_F_CAPPED);
Elliott Hughesdc75b012017-07-05 13:54:44 -0700579 return;
Elliott Hughes39bac052017-05-25 16:56:11 -0700580 }
Elliott Hughesdc75b012017-07-05 13:54:44 -0700581
Elliott Hughes77c3ff82017-09-08 17:11:00 -0700582 /*
583 * While most of NLMSG_DONE messages indeed have payloads
584 * containing just a single integer, there are few exceptions,
585 * so pass payloads of NLMSG_DONE messages to family-specific
586 * netlink payload decoders.
587 *
588 * Other types of reserved control messages need no family-specific
589 * netlink payload decoding.
590 */
591 if ((nlmsghdr->nlmsg_type >= NLMSG_MIN_TYPE
592 || nlmsghdr->nlmsg_type == NLMSG_DONE)
593 && (unsigned int) family < ARRAY_SIZE(netlink_decoders)
Elliott Hughesdc75b012017-07-05 13:54:44 -0700594 && netlink_decoders[family]
595 && netlink_decoders[family](tcp, nlmsghdr, addr, len)) {
596 return;
597 }
598
599 if (nlmsghdr->nlmsg_type == NLMSG_DONE && len == sizeof(int)) {
600 int num;
601
602 if (!umove_or_printaddr(tcp, addr, &num))
603 tprintf("%d", num);
604 return;
605 }
606
Elliott Hughes77c3ff82017-09-08 17:11:00 -0700607 printstr_ex(tcp, addr, len, QUOTE_FORCE_HEX);
Elliott Hughes39bac052017-05-25 16:56:11 -0700608}
609
610static void
611decode_nlmsghdr_with_payload(struct tcb *const tcp,
Elliott Hughesdc75b012017-07-05 13:54:44 -0700612 const int fd,
Elliott Hughes77c3ff82017-09-08 17:11:00 -0700613 const int family,
Elliott Hughes39bac052017-05-25 16:56:11 -0700614 const struct nlmsghdr *const nlmsghdr,
615 const kernel_ulong_t addr,
Elliott Hughesd35df492017-02-15 15:19:05 -0800616 const kernel_ulong_t len)
Dmitry V. Levindeb0c182016-07-08 16:01:49 +0000617{
Elliott Hughes03a418e2018-06-15 13:11:40 -0700618 const unsigned int nlmsg_len = MIN(nlmsghdr->nlmsg_len, len);
Elliott Hughesdc75b012017-07-05 13:54:44 -0700619
620 if (nlmsg_len > NLMSG_HDRLEN)
621 tprints("{");
622
Elliott Hughes77c3ff82017-09-08 17:11:00 -0700623 print_nlmsghdr(tcp, fd, family, nlmsghdr);
Elliott Hughesdc75b012017-07-05 13:54:44 -0700624
Elliott Hughes39bac052017-05-25 16:56:11 -0700625 if (nlmsg_len > NLMSG_HDRLEN) {
Fabien Siron2850f742016-07-06 15:49:22 +0000626 tprints(", ");
Elliott Hughesdc75b012017-07-05 13:54:44 -0700627 decode_payload(tcp, fd, family, nlmsghdr, addr + NLMSG_HDRLEN,
628 nlmsg_len - NLMSG_HDRLEN);
629 tprints("}");
Fabien Siron2850f742016-07-06 15:49:22 +0000630 }
Fabien Siron2850f742016-07-06 15:49:22 +0000631}
Dmitry V. Levindeb0c182016-07-08 16:01:49 +0000632
633void
Elliott Hughesdc75b012017-07-05 13:54:44 -0700634decode_netlink(struct tcb *const tcp,
635 const int fd,
636 kernel_ulong_t addr,
637 kernel_ulong_t len)
Dmitry V. Levindeb0c182016-07-08 16:01:49 +0000638{
Elliott Hughes77c3ff82017-09-08 17:11:00 -0700639 const int family = get_fd_nl_family(tcp, fd);
640
641 if (family == NETLINK_KOBJECT_UEVENT) {
Elliott Hughes03a418e2018-06-15 13:11:40 -0700642 decode_netlink_kobject_uevent(tcp, addr, len);
Elliott Hughes77c3ff82017-09-08 17:11:00 -0700643 return;
644 }
645
Dmitry V. Levindeb0c182016-07-08 16:01:49 +0000646 struct nlmsghdr nlmsghdr;
Elliott Hughes03a418e2018-06-15 13:11:40 -0700647 bool is_array = false;
Dmitry V. Levindeb0c182016-07-08 16:01:49 +0000648 unsigned int elt;
649
Elliott Hughes03a418e2018-06-15 13:11:40 -0700650 for (elt = 0; fetch_nlmsghdr(tcp, &nlmsghdr, addr, len, is_array);
651 elt++) {
Dmitry V. Levindeb0c182016-07-08 16:01:49 +0000652 if (abbrev(tcp) && elt == max_strlen) {
653 tprints("...");
654 break;
655 }
656
Elliott Hughesd35df492017-02-15 15:19:05 -0800657 unsigned int nlmsg_len = NLMSG_ALIGN(nlmsghdr.nlmsg_len);
658 kernel_ulong_t next_addr = 0;
659 kernel_ulong_t next_len = 0;
Dmitry V. Levindeb0c182016-07-08 16:01:49 +0000660
Elliott Hughes39bac052017-05-25 16:56:11 -0700661 if (nlmsghdr.nlmsg_len >= NLMSG_HDRLEN) {
Dmitry V. Levindeb0c182016-07-08 16:01:49 +0000662 next_len = (len >= nlmsg_len) ? len - nlmsg_len : 0;
663
664 if (next_len && addr + nlmsg_len > addr)
665 next_addr = addr + nlmsg_len;
666 }
667
Elliott Hughes03a418e2018-06-15 13:11:40 -0700668 if (!is_array && next_addr) {
Dmitry V. Levindeb0c182016-07-08 16:01:49 +0000669 tprints("[");
Elliott Hughes03a418e2018-06-15 13:11:40 -0700670 is_array = true;
Dmitry V. Levindeb0c182016-07-08 16:01:49 +0000671 }
672
Elliott Hughes77c3ff82017-09-08 17:11:00 -0700673 decode_nlmsghdr_with_payload(tcp, fd, family,
Elliott Hughesdc75b012017-07-05 13:54:44 -0700674 &nlmsghdr, addr, len);
Dmitry V. Levindeb0c182016-07-08 16:01:49 +0000675
676 if (!next_addr)
677 break;
678
679 tprints(", ");
680 addr = next_addr;
681 len = next_len;
682 }
683
Elliott Hughes03a418e2018-06-15 13:11:40 -0700684 if (is_array) {
Dmitry V. Levindeb0c182016-07-08 16:01:49 +0000685 tprints("]");
686 }
687}