2 * netsniff-ng - the packet sniffing beast
3 * Copyright 2014 Tobias Klauser.
4 * Subject to the GPL, version 2.
12 #include <netlink/msg.h>
13 #include <netlink/route/link.h>
14 #include <netlink/route/addr.h>
15 #include <linux/if_arp.h>
16 #include <arpa/inet.h>
25 #define INFINITY 0xFFFFFFFFU
27 #define RTA_LEN(attr) ((int)RTA_PAYLOAD(attr))
28 #define RTA_INT(attr) (*(int *)RTA_DATA(attr))
29 #define RTA_UINT(attr) (*(unsigned int *)RTA_DATA(attr))
30 #define RTA_UINT8(attr) (*(uint8_t *)RTA_DATA(attr))
31 #define RTA_UINT32(attr) (*(uint32_t *)RTA_DATA(attr))
32 #define RTA_STR(attr) ((char *)RTA_DATA(attr))
36 ((struct rtattr*)(((char*)(r)) + NLMSG_ALIGN(sizeof(struct ndmsg))))
40 #define NDA_PAYLOAD(n) NLMSG_PAYLOAD(n,sizeof(struct ndmsg))
44 #define NLA_LENGTH(len) (NLA_ALIGN(sizeof(struct nlattr)) + (len))
48 #define NLA_DATA(nla) ((void*)(((char*)(nla)) + NLA_LENGTH(0)))
52 #define NLA_PAYLOAD(nla) ((int)((nla)->nla_len) - NLA_LENGTH(0))
55 #define NLA_LEN(attr) ((int)NLA_PAYLOAD(attr))
56 #define NLA_INT(attr) (*(int *)NLA_DATA(attr))
57 #define NLA_UINT(attr) (*(unsigned int *)NLA_DATA(attr))
58 #define NLA_UINT8(attr) (*(uint8_t *)NLA_DATA(attr))
59 #define NLA_UINT16(attr) (*(uint16_t *)NLA_DATA(attr))
60 #define NLA_UINT32(attr) (*(uint32_t *)NLA_DATA(attr))
61 #define NLA_STR(attr) ((char *)NLA_DATA(attr))
64 #define GEN_NLA(n) ((struct nlattr*)(((char*)(n)) + GENL_HDRLEN))
68 #define NLA_OK(nla,len) \
69 ((len) >= (int)sizeof(struct nlattr) && \
70 (nla)->nla_len >= sizeof(struct nlattr) && \
71 (nla)->nla_len <= (len))
75 #define NLA_NEXT(nla,attrlen) \
76 ((attrlen) -= NLA_ALIGN((nla)->nla_len), \
77 (struct nlattr*)(((char*)(nla)) + NLA_ALIGN((nla)->nla_len)))
80 #define attr_fmt(attr, fmt, ...) \
81 tprintf("\tA: "fmt, ##__VA_ARGS__); \
82 tprintf(", Len %d\n", RTA_LEN(attr));
84 #define nla_fmt(attr, fmt, ...) \
85 tprintf("\tA: "fmt, ##__VA_ARGS__); \
86 tprintf(", Len %d\n", NLA_LEN(attr));
93 static const char *flags2str(struct flag_name
*tbl
, unsigned int flags
,
96 int bits_stay
= flags
;
100 for (; tbl
&& tbl
->name
; tbl
++) {
101 if (!(tbl
->flag
& flags
))
104 bits_stay
&= ~tbl
->flag
;
105 strncat(buf
, tbl
->name
, len
- strlen(buf
) - 1);
107 if (bits_stay
& flags
)
108 strncat(buf
, ",", len
- strlen(buf
) - 1);
114 static void nlmsg_print_raw(struct nlmsghdr
*hdr
)
116 u32 len
= hdr
->nlmsg_len
;
119 _ascii((uint8_t *) hdr
+ NLMSG_HDRLEN
, len
- NLMSG_HDRLEN
);
120 _hex((uint8_t *) hdr
+ NLMSG_HDRLEN
, len
- NLMSG_HDRLEN
);
124 static const char *nlmsg_family2str(uint16_t family
)
127 case NETLINK_ROUTE
: return "routing";
128 case NETLINK_UNUSED
: return "unused";
129 case NETLINK_USERSOCK
: return "user-mode socket";
130 case NETLINK_FIREWALL
: return "unused, formerly ip_queue";
131 /* NETLINK_INET_DIAG was renamed to NETLINK_SOCK_DIAG in Linux kernel 3.10 */
132 #if defined(NETLINK_SOCK_DIAG)
133 case NETLINK_SOCK_DIAG
: return "socket monitoring";
134 #elif defined(NETLINK_INET_DIAG)
135 case NETLINK_INET_DIAG
: return "INET socket monitoring";
137 case NETLINK_NFLOG
: return "netfilter ULOG";
138 case NETLINK_XFRM
: return "IPsec";
139 case NETLINK_SELINUX
: return "SELinux event notification";
140 case NETLINK_ISCSI
: return "Open-iSCSI";
141 case NETLINK_AUDIT
: return "auditing";
142 case NETLINK_FIB_LOOKUP
: return "FIB lookup";
143 case NETLINK_CONNECTOR
: return "Kernel connector";
144 case NETLINK_NETFILTER
: return "Netfilter";
145 case NETLINK_IP6_FW
: return "unused, formerly ip6_queue";
146 case NETLINK_DNRTMSG
: return "DECnet routing";
147 case NETLINK_KOBJECT_UEVENT
: return "Kernel messages";
148 case NETLINK_GENERIC
: return "Generic";
149 case NETLINK_SCSITRANSPORT
: return "SCSI transports";
150 case NETLINK_ECRYPTFS
: return "ecryptfs";
151 case NETLINK_RDMA
: return "RDMA";
152 case NETLINK_CRYPTO
: return "Crypto layer";
153 default: return "Unknown";
157 static const char *nlmsg_rtnl_type2str(uint16_t type
)
160 case RTM_NEWLINK
: return "new link";
161 case RTM_DELLINK
: return "del link";
162 case RTM_GETLINK
: return "get link";
163 case RTM_SETLINK
: return "set link";
165 case RTM_NEWADDR
: return "new addr";
166 case RTM_DELADDR
: return "del addr";
167 case RTM_GETADDR
: return "get addr";
169 case RTM_NEWROUTE
: return "new route";
170 case RTM_DELROUTE
: return "del route";
171 case RTM_GETROUTE
: return "get route";
173 case RTM_NEWNEIGH
: return "new neigh";
174 case RTM_DELNEIGH
: return "del neigh";
175 case RTM_GETNEIGH
: return "get neigh";
177 case RTM_NEWRULE
: return "new rule";
178 case RTM_DELRULE
: return "del rule";
179 case RTM_GETRULE
: return "get rule";
181 case RTM_NEWQDISC
: return "new tc qdisc";
182 case RTM_DELQDISC
: return "del tc qdisc";
183 case RTM_GETQDISC
: return "get tc qdisc";
185 case RTM_NEWTCLASS
: return "new tc class";
186 case RTM_DELTCLASS
: return "del tc class";
187 case RTM_GETTCLASS
: return "get tc class";
189 case RTM_NEWTFILTER
: return "new tc filter";
190 case RTM_DELTFILTER
: return "del tc filter";
191 case RTM_GETTFILTER
: return "get tc filter";
193 case RTM_NEWACTION
: return "new tc action";
194 case RTM_DELACTION
: return "del tc action";
195 case RTM_GETACTION
: return "get tc action";
197 case RTM_NEWPREFIX
: return "new prefix";
199 case RTM_GETMULTICAST
: return "get mcast addr";
201 case RTM_GETANYCAST
: return "get anycast addr";
203 case RTM_NEWNEIGHTBL
: return "new neigh table";
204 case RTM_GETNEIGHTBL
: return "get neigh table";
205 case RTM_SETNEIGHTBL
: return "set neigh table";
207 case RTM_NEWNDUSEROPT
: return "new ndisc user option";
209 case RTM_NEWADDRLABEL
: return "new addr label";
210 case RTM_DELADDRLABEL
: return "del addr label";
211 case RTM_GETADDRLABEL
: return "get addr label";
213 case RTM_GETDCB
: return "get data-center-bridge";
214 case RTM_SETDCB
: return "set data-center-bridge";
216 #if defined(RTM_NEWNETCONF)
217 case RTM_NEWNETCONF
: return "new netconf";
218 case RTM_GETNETCONF
: return "get netconf";
221 #if defined(RTM_NEWMDB)
222 case RTM_NEWMDB
: return "new bridge mdb";
223 case RTM_DELMDB
: return "del bridge mdb";
224 case RTM_GETMDB
: return "get bridge mdb";
226 default: return NULL
;
230 static const char *nlmsg_genl_type2str(uint16_t type
)
233 case GENL_ID_GENERATE
: return "id gen";
234 case GENL_ID_CTRL
: return "id ctrl";
235 default: return NULL
;
239 static char *nlmsg_type2str(uint16_t proto
, uint16_t type
, char *buf
, int len
)
241 const char *name
= NULL
;
243 if (proto
== NETLINK_ROUTE
&& type
< RTM_MAX
)
244 name
= nlmsg_rtnl_type2str(type
);
245 else if (proto
== NETLINK_GENERIC
)
246 name
= nlmsg_genl_type2str(type
);
249 strncpy(buf
, name
, len
);
253 return nl_nlmsgtype2str(type
, buf
, len
);
256 static const char *addr_family2str(uint16_t family
)
259 case AF_INET
: return "ipv4";
260 case AF_INET6
: return "ipv6";
261 case AF_DECnet
: return "decnet";
262 case AF_IPX
: return "ipx";
263 default: return "Unknown";
267 static const char *addr2str(uint16_t af
, const void *addr
, char *buf
, int blen
)
269 if (af
== AF_INET
|| af
== AF_INET6
)
270 return inet_ntop(af
, addr
, buf
, blen
);
275 static const char *scope2str(uint8_t scope
)
278 case RT_SCOPE_UNIVERSE
: return "global";
279 case RT_SCOPE_LINK
: return "link";
280 case RT_SCOPE_HOST
: return "host";
281 case RT_SCOPE_NOWHERE
: return "nowhere";
283 default: return "Unknown";
287 static void rtnl_print_ifinfo(struct nlmsghdr
*hdr
)
289 struct ifinfomsg
*ifi
= NLMSG_DATA(hdr
);
290 struct rtattr
*attr
= IFLA_RTA(ifi
);
291 uint32_t attrs_len
= IFLA_PAYLOAD(hdr
);
293 char if_addr
[64] = {};
294 char *af_link
= "unknown";
296 if (ifi
->ifi_family
== AF_UNSPEC
)
298 else if (ifi
->ifi_family
== AF_BRIDGE
)
301 tprintf(" [ Link Family %d (%s%s%s)", ifi
->ifi_family
,
302 colorize_start(bold
), af_link
, colorize_end());
303 tprintf(", Type %d (%s%s%s)", ifi
->ifi_type
,
304 colorize_start(bold
),
305 device_type2str(ifi
->ifi_type
),
307 tprintf(", Index %d", ifi
->ifi_index
);
308 tprintf(", Flags 0x%x (%s%s%s)", ifi
->ifi_flags
,
309 colorize_start(bold
),
310 rtnl_link_flags2str(ifi
->ifi_flags
, flags
,
313 tprintf(", Change 0x%x (%s%s%s) ]\n", ifi
->ifi_change
,
314 colorize_start(bold
),
315 rtnl_link_flags2str(ifi
->ifi_change
, flags
,
319 for (; RTA_OK(attr
, attrs_len
); attr
= RTA_NEXT(attr
, attrs_len
)) {
320 switch (attr
->rta_type
) {
322 attr_fmt(attr
, "Address %s",
323 device_addr2str(RTA_DATA(attr
),
324 RTA_LEN(attr
), ifi
->ifi_type
,
325 if_addr
, sizeof(if_addr
)));
328 attr_fmt(attr
, "Broadcast %s",
329 device_addr2str(RTA_DATA(attr
),
330 RTA_LEN(attr
), ifi
->ifi_type
,
331 if_addr
, sizeof(if_addr
)));
334 attr_fmt(attr
, "Name %s%s%s",
335 colorize_start(bold
), RTA_STR(attr
),
339 attr_fmt(attr
, "MTU %d", RTA_INT(attr
));
342 attr_fmt(attr
, "Link %d", RTA_INT(attr
));
345 attr_fmt(attr
, "QDisc %s", RTA_STR(attr
));
349 uint8_t st
= RTA_UINT8(attr
);
352 attr_fmt(attr
, "Operation state 0x%x (%s%s%s)",
354 colorize_start(bold
),
355 rtnl_link_operstate2str(st
,
356 states
, sizeof(states
)),
362 uint8_t mode
= RTA_UINT8(attr
);
365 attr_fmt(attr
, "Mode 0x%x (%s%s%s)", mode
,
366 colorize_start(bold
),
367 rtnl_link_mode2str(mode
, str
,
373 attr_fmt(attr
, "Group %d", RTA_INT(attr
));
376 attr_fmt(attr
, "Tx queue len %d", RTA_INT(attr
));
378 case IFLA_NET_NS_PID
:
379 attr_fmt(attr
, "Network namespace pid %d",
383 attr_fmt(attr
, "Network namespace fd %d",
387 attr_fmt(attr
, "0x%x", attr
->rta_type
);
393 static void rtnl_print_ifaddr(struct nlmsghdr
*hdr
)
395 struct ifaddrmsg
*ifa
= NLMSG_DATA(hdr
);
396 uint32_t attrs_len
= IFA_PAYLOAD(hdr
);
397 struct rtattr
*attr
= IFA_RTA(ifa
);
398 struct ifa_cacheinfo
*ci
;
402 tprintf(" [ Address Family %d (%s%s%s)", ifa
->ifa_family
,
403 colorize_start(bold
),
404 addr_family2str(ifa
->ifa_family
),
406 tprintf(", Prefix Len %d", ifa
->ifa_prefixlen
);
407 tprintf(", Flags %d (%s%s%s)", ifa
->ifa_flags
,
408 colorize_start(bold
),
409 rtnl_addr_flags2str(ifa
->ifa_flags
, flags
,
412 tprintf(", Scope %d (%s%s%s)", ifa
->ifa_scope
,
413 colorize_start(bold
),
414 scope2str(ifa
->ifa_scope
),
416 tprintf(", Link Index %d ]\n", ifa
->ifa_index
);
418 for (; RTA_OK(attr
, attrs_len
); attr
= RTA_NEXT(attr
, attrs_len
)) {
419 switch (attr
->rta_type
) {
421 attr_fmt(attr
, "Local %s", addr2str(ifa
->ifa_family
,
422 RTA_DATA(attr
), addr_str
, sizeof(addr_str
)));
425 attr_fmt(attr
, "Address %s", addr2str(ifa
->ifa_family
,
426 RTA_DATA(attr
), addr_str
, sizeof(addr_str
)));
429 attr_fmt(attr
, "Broadcast %s",
430 addr2str(ifa
->ifa_family
,
431 RTA_DATA(attr
), addr_str
,
435 attr_fmt(attr
, "Multicast %s",
436 addr2str(ifa
->ifa_family
,
437 RTA_DATA(attr
), addr_str
,
441 attr_fmt(attr
, "Anycast %s", addr2str(ifa
->ifa_family
,
442 RTA_DATA(attr
), addr_str
, sizeof(addr_str
)));
446 attr_fmt(attr
, "Flags %d (%s%s%s)", RTA_INT(attr
),
447 colorize_start(bold
),
448 rtnl_addr_flags2str(RTA_INT(attr
),
449 flags
, sizeof(flags
)),
454 attr_fmt(attr
, "Label %s", RTA_STR(attr
));
458 tprintf("\tA: Cache (");
460 if (ci
->ifa_valid
== INFINITY
)
461 tprintf("valid lft(forever)");
463 tprintf("valid lft(%us)", ci
->ifa_valid
);
465 if (ci
->ifa_prefered
== INFINITY
)
466 tprintf(", prefrd lft(forever)");
468 tprintf(", prefrd lft(%us)", ci
->ifa_prefered
);
470 tprintf(", created on(%.2fs)", (double)ci
->cstamp
/ 100);
471 tprintf(", updated on(%.2fs))", (double)ci
->cstamp
/ 100);
472 tprintf(", Len %d\n", RTA_LEN(attr
));
475 attr_fmt(attr
, "0x%x", attr
->rta_type
);
481 static const char *route_table2str(uint8_t table
)
484 case RT_TABLE_UNSPEC
: return "unspec";
485 case RT_TABLE_COMPAT
: return "compat";
486 case RT_TABLE_DEFAULT
: return "default";
487 case RT_TABLE_MAIN
: return "main";
488 case RT_TABLE_LOCAL
: return "local";
490 default: return "Unknown";
494 static const char *route_proto2str(uint8_t proto
)
497 case RTPROT_UNSPEC
: return "unspec";
498 case RTPROT_REDIRECT
: return "redirect";
499 case RTPROT_KERNEL
: return "kernel";
500 case RTPROT_BOOT
: return "boot";
501 case RTPROT_STATIC
: return "static";
502 case RTPROT_GATED
: return "gated";
503 case RTPROT_RA
: return "ra";
504 case RTPROT_MRT
: return "mrt";
505 case RTPROT_ZEBRA
: return "zebra";
506 case RTPROT_BIRD
: return "bird";
507 case RTPROT_DNROUTED
: return "DECnet";
508 case RTPROT_XORP
: return "xorp";
509 case RTPROT_NTK
: return "netsukuku";
510 case RTPROT_DHCP
: return "dhcpc";
511 #ifdef RTPROT_MROUTED
512 case RTPROT_MROUTED
: return "mrouted";
515 default: return "Unknown";
519 static const char *route_type2str(uint8_t type
)
522 case RTN_UNSPEC
: return "unspec";
523 case RTN_UNICAST
: return "unicast";
524 case RTN_LOCAL
: return "local";
525 case RTN_BROADCAST
: return "broadcast";
526 case RTN_ANYCAST
: return "anycast";
527 case RTN_MULTICAST
: return "multicast";
528 case RTN_BLACKHOLE
: return "blackhole";
529 case RTN_UNREACHABLE
: return "unreach";
530 case RTN_PROHIBIT
: return "prohibit";
531 case RTN_THROW
: return "throw";
532 case RTN_NAT
: return "nat";
533 case RTN_XRESOLVE
: return "xresolve";
534 default: return "Unknown";
538 static struct flag_name route_flags
[] = {
539 { "notify", RTM_F_NOTIFY
},
540 { "cloned", RTM_F_CLONED
},
541 { "equalize", RTM_F_EQUALIZE
},
542 { "prefix", RTM_F_PREFIX
},
543 { "dead", RTNH_F_DEAD
},
544 { "pervasive", RTNH_F_PERVASIVE
},
545 { "onlink", RTNH_F_ONLINK
},
549 static void rtnl_print_route(struct nlmsghdr
*hdr
)
551 struct rtmsg
*rtm
= NLMSG_DATA(hdr
);
552 uint32_t attrs_len
= RTM_PAYLOAD(hdr
);
553 struct rtattr
*attr
= RTM_RTA(rtm
);
554 struct rta_cacheinfo
*ci
;
555 int hz
= get_user_hz();
559 tprintf(" [ Route Family %d (%s%s%s)", rtm
->rtm_family
,
560 colorize_start(bold
),
561 addr_family2str(rtm
->rtm_family
),
563 tprintf(", Dst Len %d", rtm
->rtm_dst_len
);
564 tprintf(", Src Len %d", rtm
->rtm_src_len
);
565 tprintf(", ToS %d", rtm
->rtm_tos
);
566 tprintf(", Table %d (%s%s%s)", rtm
->rtm_table
,
567 colorize_start(bold
),
568 route_table2str(rtm
->rtm_table
),
570 tprintf(", Proto %d (%s%s%s)", rtm
->rtm_protocol
,
571 colorize_start(bold
),
572 route_proto2str(rtm
->rtm_protocol
),
574 tprintf(", Scope %d (%s%s%s)", rtm
->rtm_scope
,
575 colorize_start(bold
),
576 scope2str(rtm
->rtm_scope
),
578 tprintf(", Type %d (%s%s%s)", rtm
->rtm_type
,
579 colorize_start(bold
),
580 route_type2str(rtm
->rtm_type
),
582 tprintf(", Flags 0x%x (%s%s%s) ]\n", rtm
->rtm_flags
,
583 colorize_start(bold
),
584 flags2str(route_flags
, rtm
->rtm_flags
, flags
,
588 for (; RTA_OK(attr
, attrs_len
); attr
= RTA_NEXT(attr
, attrs_len
)) {
589 switch (attr
->rta_type
) {
591 attr_fmt(attr
, "Dst %s", addr2str(rtm
->rtm_family
,
592 RTA_DATA(attr
), addr_str
, sizeof(addr_str
)));
595 attr_fmt(attr
, "Src %s", addr2str(rtm
->rtm_family
,
596 RTA_DATA(attr
), addr_str
, sizeof(addr_str
)));
599 attr_fmt(attr
, "Iif %d", RTA_INT(attr
));
602 attr_fmt(attr
, "Oif %d", RTA_INT(attr
));
605 attr_fmt(attr
, "Gateway %s", addr2str(rtm
->rtm_family
,
606 RTA_DATA(attr
), addr_str
, sizeof(addr_str
)));
609 attr_fmt(attr
, "Priority %u", RTA_UINT32(attr
));
612 attr_fmt(attr
, "Pref Src %s", addr2str(rtm
->rtm_family
,
613 RTA_DATA(attr
), addr_str
, sizeof(addr_str
)));
616 attr_fmt(attr
, "Mark 0x%x", RTA_UINT(attr
));
619 attr_fmt(attr
, "Flow 0x%x", RTA_UINT(attr
));
622 attr_fmt(attr
, "Table %d (%s%s%s)", RTA_UINT32(attr
),
623 colorize_start(bold
),
624 route_table2str(RTA_UINT32(attr
)),
629 tprintf("\tA: Cache (");
630 tprintf("expires(%ds)", ci
->rta_expires
/ hz
);
631 tprintf(", error(%d)", ci
->rta_error
);
632 tprintf(", users(%d)", ci
->rta_clntref
);
633 tprintf(", used(%d)", ci
->rta_used
);
634 tprintf(", last use(%ds)", ci
->rta_lastuse
/ hz
);
635 tprintf(", id(%d)", ci
->rta_id
);
636 tprintf(", ts(%d)", ci
->rta_ts
);
637 tprintf(", ts age(%ds))", ci
->rta_tsage
);
638 tprintf(", Len %d\n", RTA_LEN(attr
));
641 attr_fmt(attr
, "0x%x", attr
->rta_type
);
647 static struct flag_name neigh_states
[] = {
648 { "incomplete", NUD_INCOMPLETE
},
649 { "reachable", NUD_REACHABLE
},
650 { "stale", NUD_STALE
},
651 { "delay", NUD_DELAY
},
652 { "probe", NUD_PROBE
},
653 { "failed", NUD_FAILED
},
654 { "noarp", NUD_NOARP
},
655 { "permanent", NUD_PERMANENT
},
656 { "none", NUD_NONE
},
660 /* Copied from linux/neighbour.h */
662 # define NTF_USE 0x01
665 # define NTF_SELF 0x02
668 # define NTF_MASTER 0x04
671 # define NTF_PROXY 0x08
673 #ifndef NTF_EXT_LEARNED
674 # define NTF_EXT_LEARNED 0x10
677 # define NTF_ROUTER 0x80
680 static struct flag_name neigh_flags
[] = {
682 { "self", NTF_SELF
},
683 { "master", NTF_MASTER
},
684 { "proxy", NTF_PROXY
},
685 { "ext learned", NTF_EXT_LEARNED
},
686 { "router", NTF_ROUTER
},
690 static void rtnl_print_neigh(struct nlmsghdr
*hdr
)
692 struct ndmsg
*ndm
= NLMSG_DATA(hdr
);
693 uint32_t attrs_len
= NDA_PAYLOAD(hdr
);
694 struct rtattr
*attr
= NDA_RTA(ndm
);
695 struct nda_cacheinfo
*ci
;
696 int hz
= get_user_hz();
702 tprintf(" [ Neigh Family %d (%s%s%s)", ndm
->ndm_family
,
703 colorize_start(bold
),
704 addr_family2str(ndm
->ndm_family
),
706 tprintf(", Link Index %d", ndm
->ndm_ifindex
);
707 tprintf(", State %d (%s%s%s)", ndm
->ndm_state
,
708 colorize_start(bold
),
709 flags2str(neigh_states
, ndm
->ndm_state
, states
,
712 tprintf(", Flags %d (%s%s%s)", ndm
->ndm_flags
,
713 colorize_start(bold
),
714 flags2str(neigh_flags
, ndm
->ndm_flags
, flags
,
717 tprintf(", Type %d (%s%s%s)", ndm
->ndm_type
,
718 colorize_start(bold
),
719 route_type2str(ndm
->ndm_type
),
723 for (; RTA_OK(attr
, attrs_len
); attr
= RTA_NEXT(attr
, attrs_len
)) {
724 switch (attr
->rta_type
) {
726 attr_fmt(attr
, "Address %s", addr2str(ndm
->ndm_family
,
727 RTA_DATA(attr
), addr_str
,
731 attr_fmt(attr
, "HW Address %s",
732 device_addr2str(RTA_DATA(attr
),
733 RTA_LEN(attr
), 0, hw_addr
,
737 attr_fmt(attr
, "Probes %d", RTA_UINT32(attr
));
741 tprintf("\tA: Cache (");
742 tprintf("confirmed(%ds)", ci
->ndm_confirmed
/ hz
);
743 tprintf(", used(%ds)", ci
->ndm_used
/ hz
);
744 tprintf(", updated(%ds)", ci
->ndm_updated
/ hz
);
745 tprintf(", refcnt(%d))", ci
->ndm_refcnt
);
746 tprintf(", Len %d\n", RTA_LEN(attr
));
749 attr_fmt(attr
, "0x%x", attr
->rta_type
);
755 static void rtnl_msg_print(struct nlmsghdr
*hdr
)
757 switch (hdr
->nlmsg_type
) {
762 rtnl_print_ifinfo(hdr
);
767 rtnl_print_ifaddr(hdr
);
772 rtnl_print_route(hdr
);
777 rtnl_print_neigh(hdr
);
782 static const char *genl_cmd2str(uint8_t table
)
785 case CTRL_CMD_UNSPEC
: return "unspec";
786 case CTRL_CMD_NEWFAMILY
: return "new family";
787 case CTRL_CMD_DELFAMILY
: return "del family";
788 case CTRL_CMD_GETFAMILY
: return "get family";
789 case CTRL_CMD_NEWOPS
: return "new ops";
790 case CTRL_CMD_DELOPS
: return "del ops";
791 case CTRL_CMD_GETOPS
: return "get ops";
792 case CTRL_CMD_NEWMCAST_GRP
: return "new mcast group";
793 case CTRL_CMD_DELMCAST_GRP
: return "del mcast group";
794 case CTRL_CMD_GETMCAST_GRP
: return "get mcast group";
796 default: return "Unknown";
800 static void genl_print_ctrl_family(struct nlmsghdr
*hdr
)
802 struct genlmsghdr
*genl
= NLMSG_DATA(hdr
);
803 struct nlattr
*attr
= GEN_NLA(genl
);
804 uint32_t attrs_len
= NLMSG_PAYLOAD(hdr
, sizeof(struct genlmsghdr
));
806 for (; NLA_OK(attr
, attrs_len
); attr
= NLA_NEXT(attr
, attrs_len
)) {
807 switch (attr
->nla_type
) {
808 case CTRL_ATTR_FAMILY_ID
:
809 nla_fmt(attr
, "Family Id 0x%x", NLA_UINT16(attr
));
812 case CTRL_ATTR_FAMILY_NAME
:
813 nla_fmt(attr
, "Family Name %s", NLA_STR(attr
));
816 case CTRL_ATTR_VERSION
:
817 nla_fmt(attr
, "Version %u", NLA_UINT32(attr
));
820 case CTRL_ATTR_HDRSIZE
:
821 nla_fmt(attr
, "Header size %u", NLA_UINT32(attr
));
824 case CTRL_ATTR_MAXATTR
:
825 nla_fmt(attr
, "Max attr value 0x%x", NLA_UINT32(attr
));
829 nla_fmt(attr
, "0x%x", attr
->nla_type
);
835 static void genl_print_ctrl(struct nlmsghdr
*hdr
)
837 struct genlmsghdr
*genl
= NLMSG_DATA(hdr
);
840 case CTRL_CMD_NEWFAMILY
:
841 case CTRL_CMD_DELFAMILY
:
842 case CTRL_CMD_GETFAMILY
:
843 genl_print_ctrl_family(hdr
);
847 static void genl_msg_print(struct nlmsghdr
*hdr
)
849 struct genlmsghdr
*genl
;
851 if (hdr
->nlmsg_type
!= GENL_ID_CTRL
) {
852 nlmsg_print_raw(hdr
);
856 genl
= NLMSG_DATA(hdr
);
858 tprintf(" [ Cmd %u (%s%s%s)", genl
->cmd
,
859 colorize_start(bold
), genl_cmd2str(genl
->cmd
), colorize_end());
860 tprintf(", Version %u", genl
->version
);
861 tprintf(", Reserved %u", genl
->reserved
);
864 genl_print_ctrl(hdr
);
867 static void nlmsg_print(uint16_t family
, struct nlmsghdr
*hdr
)
869 u16 nlmsg_flags
= hdr
->nlmsg_flags
;
872 char procname
[PATH_MAX
];
874 /* Look up the process name if message is not coming from the kernel.
876 * Note that the port id is not necessarily equal to the PID of the
877 * receiving process (e.g. if the application is multithreaded or using
878 * multiple sockets). In these cases we're not able to find a matching
879 * PID and the information will not be printed.
881 if (hdr
->nlmsg_pid
!= 0) {
882 if (proc_get_cmdline(hdr
->nlmsg_pid
, procname
, sizeof(procname
)) < 0)
883 snprintf(procname
, sizeof(procname
), "unknown process");
885 snprintf(procname
, sizeof(procname
), "kernel");
887 tprintf(" [ NLMSG ");
888 tprintf("Family %d (%s%s%s), ", family
,
889 colorize_start(bold
),
890 nlmsg_family2str(family
),
892 tprintf("Len %u, ", hdr
->nlmsg_len
);
893 tprintf("Type 0x%.4x (%s%s%s), ", hdr
->nlmsg_type
,
894 colorize_start(bold
),
895 nlmsg_type2str(family
, hdr
->nlmsg_type
, type
, sizeof(type
)),
897 tprintf("Flags 0x%.4x (%s%s%s), ", nlmsg_flags
,
898 colorize_start(bold
),
899 nlmsg_flags
? nl_nlmsg_flags2str(nlmsg_flags
, flags
, sizeof(flags
)) : "none",
901 tprintf("Seq-Nr %u, ", hdr
->nlmsg_seq
);
902 tprintf("PID %u", hdr
->nlmsg_pid
);
904 tprintf(" (%s%s%s)", colorize_start(bold
), basename(procname
),
912 case NETLINK_GENERIC
:
916 nlmsg_print_raw(hdr
);
920 static void nlmsg(struct pkt_buff
*pkt
)
922 struct nlmsghdr
*hdr
= (struct nlmsghdr
*) pkt_pull(pkt
, NLMSG_HDRLEN
);
923 unsigned int trim_len
= pkt_len(pkt
);
926 trim_len
-= hdr
->nlmsg_len
;
927 nlmsg_print(ntohs(pkt
->sll
->sll_protocol
), hdr
);
929 if (!pkt_pull(pkt
, NLMSG_ALIGN(hdr
->nlmsg_len
) - NLMSG_HDRLEN
))
932 hdr
= (struct nlmsghdr
*) pkt_pull(pkt
, NLMSG_HDRLEN
);
935 if (hdr
->nlmsg_len
== 0)
938 if (hdr
->nlmsg_type
!= NLMSG_DONE
&&
939 (hdr
->nlmsg_flags
& NLM_F_MULTI
))
944 if (hdr
&& hdr
->nlmsg_len
== 0)
945 pkt_trim(pkt
, trim_len
);
948 static void nlmsg_less(struct pkt_buff
*pkt
)
950 struct nlmsghdr
*hdr
= (struct nlmsghdr
*) pkt_pull(pkt
, NLMSG_HDRLEN
);
951 uint16_t family
= ntohs(pkt
->sll
->sll_protocol
);
957 tprintf(" NLMSG Family %d (%s%s%s), ", family
,
958 colorize_start(bold
),
959 nlmsg_family2str(family
),
961 tprintf("Type %u (%s%s%s)", hdr
->nlmsg_type
,
962 colorize_start(bold
),
963 nlmsg_type2str(family
, hdr
->nlmsg_type
, type
, sizeof(type
)),
967 struct protocol nlmsg_ops
= {
969 .print_less
= nlmsg_less
,