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 rta_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));
88 #define nla_fmt_nested(attr, fmt, ...) \
89 tprintf("[ "fmt, ##__VA_ARGS__); \
90 tprintf(", Len %d] ", NLA_LEN(attr));
92 #define nla_fmt_nested_start(attr, fmt, ...) \
93 tprintf("\t A: "fmt, ##__VA_ARGS__); \
94 tprintf(", Len %d ", NLA_LEN(attr));
96 #define nla_fmt_nested_end() tprintf("\n")
103 static const char *flags2str(struct flag_name
*tbl
, unsigned int flags
,
106 int bits_stay
= flags
;
110 for (; tbl
&& tbl
->name
; tbl
++) {
111 if (!(tbl
->flag
& flags
))
114 bits_stay
&= ~tbl
->flag
;
115 strncat(buf
, tbl
->name
, len
- strlen(buf
) - 1);
117 if (bits_stay
& flags
)
118 strncat(buf
, ",", len
- strlen(buf
) - 1);
124 static void nlmsg_print_raw(struct nlmsghdr
*hdr
)
126 u32 len
= hdr
->nlmsg_len
;
129 _ascii((uint8_t *) hdr
+ NLMSG_HDRLEN
, len
- NLMSG_HDRLEN
);
130 _hex((uint8_t *) hdr
+ NLMSG_HDRLEN
, len
- NLMSG_HDRLEN
);
134 static const char *nlmsg_family2str(uint16_t family
)
137 case NETLINK_ROUTE
: return "routing";
138 case NETLINK_UNUSED
: return "unused";
139 case NETLINK_USERSOCK
: return "user-mode socket";
140 case NETLINK_FIREWALL
: return "unused, formerly ip_queue";
141 /* NETLINK_INET_DIAG was renamed to NETLINK_SOCK_DIAG in Linux kernel 3.10 */
142 #if defined(NETLINK_SOCK_DIAG)
143 case NETLINK_SOCK_DIAG
: return "socket monitoring";
144 #elif defined(NETLINK_INET_DIAG)
145 case NETLINK_INET_DIAG
: return "INET socket monitoring";
147 case NETLINK_NFLOG
: return "netfilter ULOG";
148 case NETLINK_XFRM
: return "IPsec";
149 case NETLINK_SELINUX
: return "SELinux event notification";
150 case NETLINK_ISCSI
: return "Open-iSCSI";
151 case NETLINK_AUDIT
: return "auditing";
152 case NETLINK_FIB_LOOKUP
: return "FIB lookup";
153 case NETLINK_CONNECTOR
: return "Kernel connector";
154 case NETLINK_NETFILTER
: return "Netfilter";
155 case NETLINK_IP6_FW
: return "unused, formerly ip6_queue";
156 case NETLINK_DNRTMSG
: return "DECnet routing";
157 case NETLINK_KOBJECT_UEVENT
: return "Kernel messages";
158 case NETLINK_GENERIC
: return "Generic";
159 case NETLINK_SCSITRANSPORT
: return "SCSI transports";
160 case NETLINK_ECRYPTFS
: return "ecryptfs";
161 case NETLINK_RDMA
: return "RDMA";
162 #if defined(NETLINK_CRYPTO)
163 case NETLINK_CRYPTO
: return "Crypto layer";
165 default: return "Unknown";
169 static const char *nlmsg_rtnl_type2str(uint16_t type
)
172 case RTM_NEWLINK
: return "new link";
173 case RTM_DELLINK
: return "del link";
174 case RTM_GETLINK
: return "get link";
175 case RTM_SETLINK
: return "set link";
177 case RTM_NEWADDR
: return "new addr";
178 case RTM_DELADDR
: return "del addr";
179 case RTM_GETADDR
: return "get addr";
181 case RTM_NEWROUTE
: return "new route";
182 case RTM_DELROUTE
: return "del route";
183 case RTM_GETROUTE
: return "get route";
185 case RTM_NEWNEIGH
: return "new neigh";
186 case RTM_DELNEIGH
: return "del neigh";
187 case RTM_GETNEIGH
: return "get neigh";
189 case RTM_NEWRULE
: return "new rule";
190 case RTM_DELRULE
: return "del rule";
191 case RTM_GETRULE
: return "get rule";
193 case RTM_NEWQDISC
: return "new tc qdisc";
194 case RTM_DELQDISC
: return "del tc qdisc";
195 case RTM_GETQDISC
: return "get tc qdisc";
197 case RTM_NEWTCLASS
: return "new tc class";
198 case RTM_DELTCLASS
: return "del tc class";
199 case RTM_GETTCLASS
: return "get tc class";
201 case RTM_NEWTFILTER
: return "new tc filter";
202 case RTM_DELTFILTER
: return "del tc filter";
203 case RTM_GETTFILTER
: return "get tc filter";
205 case RTM_NEWACTION
: return "new tc action";
206 case RTM_DELACTION
: return "del tc action";
207 case RTM_GETACTION
: return "get tc action";
209 case RTM_NEWPREFIX
: return "new prefix";
211 case RTM_GETMULTICAST
: return "get mcast addr";
213 case RTM_GETANYCAST
: return "get anycast addr";
215 case RTM_NEWNEIGHTBL
: return "new neigh table";
216 case RTM_GETNEIGHTBL
: return "get neigh table";
217 case RTM_SETNEIGHTBL
: return "set neigh table";
219 case RTM_NEWNDUSEROPT
: return "new ndisc user option";
221 case RTM_NEWADDRLABEL
: return "new addr label";
222 case RTM_DELADDRLABEL
: return "del addr label";
223 case RTM_GETADDRLABEL
: return "get addr label";
225 case RTM_GETDCB
: return "get data-center-bridge";
226 case RTM_SETDCB
: return "set data-center-bridge";
228 #if defined(RTM_NEWNETCONF)
229 case RTM_NEWNETCONF
: return "new netconf";
230 case RTM_GETNETCONF
: return "get netconf";
233 #if defined(RTM_NEWMDB)
234 case RTM_NEWMDB
: return "new bridge mdb";
235 case RTM_DELMDB
: return "del bridge mdb";
236 case RTM_GETMDB
: return "get bridge mdb";
238 default: return NULL
;
242 static const char *nlmsg_genl_type2str(uint16_t type
)
245 case GENL_ID_GENERATE
: return "id gen";
246 case GENL_ID_CTRL
: return "id ctrl";
247 default: return NULL
;
251 static char *nlmsg_type2str(uint16_t proto
, uint16_t type
, char *buf
, int len
)
253 const char *name
= NULL
;
255 if (proto
== NETLINK_ROUTE
&& type
< RTM_MAX
)
256 name
= nlmsg_rtnl_type2str(type
);
257 else if (proto
== NETLINK_GENERIC
)
258 name
= nlmsg_genl_type2str(type
);
261 strncpy(buf
, name
, len
);
265 return nl_nlmsgtype2str(type
, buf
, len
);
268 static const char *addr_family2str(uint16_t family
)
271 case AF_INET
: return "ipv4";
272 case AF_INET6
: return "ipv6";
273 case AF_DECnet
: return "decnet";
274 case AF_IPX
: return "ipx";
275 default: return "Unknown";
279 static const char *addr2str(uint16_t af
, const void *addr
, char *buf
, int blen
)
281 if (af
== AF_INET
|| af
== AF_INET6
)
282 return inet_ntop(af
, addr
, buf
, blen
);
287 static const char *scope2str(uint8_t scope
)
290 case RT_SCOPE_UNIVERSE
: return "global";
291 case RT_SCOPE_LINK
: return "link";
292 case RT_SCOPE_HOST
: return "host";
293 case RT_SCOPE_NOWHERE
: return "nowhere";
295 default: return "Unknown";
299 static void rtnl_print_ifinfo(struct nlmsghdr
*hdr
)
301 struct ifinfomsg
*ifi
= NLMSG_DATA(hdr
);
302 struct rtattr
*attr
= IFLA_RTA(ifi
);
303 uint32_t attrs_len
= IFLA_PAYLOAD(hdr
);
305 char if_addr
[64] = {};
306 char *af_link
= "unknown";
308 if (hdr
->nlmsg_len
< NLMSG_LENGTH(sizeof(*ifi
)))
311 if (ifi
->ifi_family
== AF_UNSPEC
)
313 else if (ifi
->ifi_family
== AF_BRIDGE
)
316 tprintf(" [ Link Family %d (%s%s%s)", ifi
->ifi_family
,
317 colorize_start(bold
), af_link
, colorize_end());
318 tprintf(", Type %d (%s%s%s)", ifi
->ifi_type
,
319 colorize_start(bold
),
320 device_type2str(ifi
->ifi_type
),
322 tprintf(", Index %d", ifi
->ifi_index
);
323 tprintf(", Flags 0x%x (%s%s%s)", ifi
->ifi_flags
,
324 colorize_start(bold
),
325 rtnl_link_flags2str(ifi
->ifi_flags
, flags
,
328 tprintf(", Change 0x%x (%s%s%s) ]\n", ifi
->ifi_change
,
329 colorize_start(bold
),
330 rtnl_link_flags2str(ifi
->ifi_change
, flags
,
334 for (; RTA_OK(attr
, attrs_len
); attr
= RTA_NEXT(attr
, attrs_len
)) {
335 switch (attr
->rta_type
) {
337 rta_fmt(attr
, "Address %s",
338 device_addr2str(RTA_DATA(attr
),
339 RTA_LEN(attr
), ifi
->ifi_type
,
340 if_addr
, sizeof(if_addr
)));
343 rta_fmt(attr
, "Broadcast %s",
344 device_addr2str(RTA_DATA(attr
),
345 RTA_LEN(attr
), ifi
->ifi_type
,
346 if_addr
, sizeof(if_addr
)));
349 rta_fmt(attr
, "Name %s%s%s",
350 colorize_start(bold
), RTA_STR(attr
),
354 rta_fmt(attr
, "MTU %d", RTA_INT(attr
));
357 rta_fmt(attr
, "Link %d", RTA_INT(attr
));
360 rta_fmt(attr
, "QDisc %s", RTA_STR(attr
));
364 uint8_t st
= RTA_UINT8(attr
);
367 rta_fmt(attr
, "Operation state 0x%x (%s%s%s)",
369 colorize_start(bold
),
370 rtnl_link_operstate2str(st
,
371 states
, sizeof(states
)),
377 uint8_t mode
= RTA_UINT8(attr
);
380 rta_fmt(attr
, "Mode 0x%x (%s%s%s)", mode
,
381 colorize_start(bold
),
382 rtnl_link_mode2str(mode
, str
,
388 rta_fmt(attr
, "Group %d", RTA_INT(attr
));
391 rta_fmt(attr
, "Tx queue len %d", RTA_INT(attr
));
393 case IFLA_NET_NS_PID
:
394 rta_fmt(attr
, "Network namespace pid %d",
398 rta_fmt(attr
, "Network namespace fd %d", RTA_INT(attr
));
401 rta_fmt(attr
, "0x%x", attr
->rta_type
);
407 static void rtnl_print_ifaddr(struct nlmsghdr
*hdr
)
409 struct ifaddrmsg
*ifa
= NLMSG_DATA(hdr
);
410 uint32_t attrs_len
= IFA_PAYLOAD(hdr
);
411 struct rtattr
*attr
= IFA_RTA(ifa
);
412 struct ifa_cacheinfo
*ci
;
416 if (hdr
->nlmsg_len
< NLMSG_LENGTH(sizeof(*ifa
)))
419 tprintf(" [ Address Family %d (%s%s%s)", ifa
->ifa_family
,
420 colorize_start(bold
),
421 addr_family2str(ifa
->ifa_family
),
423 tprintf(", Prefix Len %d", ifa
->ifa_prefixlen
);
424 tprintf(", Flags %d (%s%s%s)", ifa
->ifa_flags
,
425 colorize_start(bold
),
426 rtnl_addr_flags2str(ifa
->ifa_flags
, flags
,
429 tprintf(", Scope %d (%s%s%s)", ifa
->ifa_scope
,
430 colorize_start(bold
),
431 scope2str(ifa
->ifa_scope
),
433 tprintf(", Link Index %d ]\n", ifa
->ifa_index
);
435 for (; RTA_OK(attr
, attrs_len
); attr
= RTA_NEXT(attr
, attrs_len
)) {
436 switch (attr
->rta_type
) {
438 rta_fmt(attr
, "Local %s", addr2str(ifa
->ifa_family
,
439 RTA_DATA(attr
), addr_str
, sizeof(addr_str
)));
442 rta_fmt(attr
, "Address %s", addr2str(ifa
->ifa_family
,
443 RTA_DATA(attr
), addr_str
, sizeof(addr_str
)));
446 rta_fmt(attr
, "Broadcast %s",
447 addr2str(ifa
->ifa_family
,
448 RTA_DATA(attr
), addr_str
,
452 rta_fmt(attr
, "Multicast %s",
453 addr2str(ifa
->ifa_family
,
454 RTA_DATA(attr
), addr_str
,
458 rta_fmt(attr
, "Anycast %s", addr2str(ifa
->ifa_family
,
459 RTA_DATA(attr
), addr_str
, sizeof(addr_str
)));
463 rta_fmt(attr
, "Flags %d (%s%s%s)", RTA_INT(attr
),
464 colorize_start(bold
),
465 rtnl_addr_flags2str(RTA_INT(attr
),
466 flags
, sizeof(flags
)),
471 rta_fmt(attr
, "Label %s", RTA_STR(attr
));
475 tprintf("\tA: Cache (");
477 if (ci
->ifa_valid
== INFINITY
)
478 tprintf("valid lft(forever)");
480 tprintf("valid lft(%us)", ci
->ifa_valid
);
482 if (ci
->ifa_prefered
== INFINITY
)
483 tprintf(", prefrd lft(forever)");
485 tprintf(", prefrd lft(%us)", ci
->ifa_prefered
);
487 tprintf(", created on(%.2fs)", (double)ci
->cstamp
/ 100);
488 tprintf(", updated on(%.2fs))", (double)ci
->cstamp
/ 100);
489 tprintf(", Len %d\n", RTA_LEN(attr
));
492 rta_fmt(attr
, "0x%x", attr
->rta_type
);
498 static const char *route_table2str(uint8_t table
)
501 case RT_TABLE_UNSPEC
: return "unspec";
502 case RT_TABLE_COMPAT
: return "compat";
503 case RT_TABLE_DEFAULT
: return "default";
504 case RT_TABLE_MAIN
: return "main";
505 case RT_TABLE_LOCAL
: return "local";
507 default: return "Unknown";
511 static const char *route_proto2str(uint8_t proto
)
514 case RTPROT_UNSPEC
: return "unspec";
515 case RTPROT_REDIRECT
: return "redirect";
516 case RTPROT_KERNEL
: return "kernel";
517 case RTPROT_BOOT
: return "boot";
518 case RTPROT_STATIC
: return "static";
519 case RTPROT_GATED
: return "gated";
520 case RTPROT_RA
: return "ra";
521 case RTPROT_MRT
: return "mrt";
522 case RTPROT_ZEBRA
: return "zebra";
523 case RTPROT_BIRD
: return "bird";
524 case RTPROT_DNROUTED
: return "DECnet";
525 case RTPROT_XORP
: return "xorp";
526 case RTPROT_NTK
: return "netsukuku";
527 case RTPROT_DHCP
: return "dhcpc";
528 #ifdef RTPROT_MROUTED
529 case RTPROT_MROUTED
: return "mrouted";
532 default: return "Unknown";
536 static const char *route_type2str(uint8_t type
)
539 case RTN_UNSPEC
: return "unspec";
540 case RTN_UNICAST
: return "unicast";
541 case RTN_LOCAL
: return "local";
542 case RTN_BROADCAST
: return "broadcast";
543 case RTN_ANYCAST
: return "anycast";
544 case RTN_MULTICAST
: return "multicast";
545 case RTN_BLACKHOLE
: return "blackhole";
546 case RTN_UNREACHABLE
: return "unreach";
547 case RTN_PROHIBIT
: return "prohibit";
548 case RTN_THROW
: return "throw";
549 case RTN_NAT
: return "nat";
550 case RTN_XRESOLVE
: return "xresolve";
551 default: return "Unknown";
555 static struct flag_name route_flags
[] = {
556 { "notify", RTM_F_NOTIFY
},
557 { "cloned", RTM_F_CLONED
},
558 { "equalize", RTM_F_EQUALIZE
},
559 { "prefix", RTM_F_PREFIX
},
560 { "dead", RTNH_F_DEAD
},
561 { "pervasive", RTNH_F_PERVASIVE
},
562 { "onlink", RTNH_F_ONLINK
},
566 static void rtnl_print_route(struct nlmsghdr
*hdr
)
568 struct rtmsg
*rtm
= NLMSG_DATA(hdr
);
569 uint32_t attrs_len
= RTM_PAYLOAD(hdr
);
570 struct rtattr
*attr
= RTM_RTA(rtm
);
571 struct rta_cacheinfo
*ci
;
572 int hz
= get_user_hz();
576 if (hdr
->nlmsg_len
< NLMSG_LENGTH(sizeof(*rtm
)))
579 tprintf(" [ Route Family %d (%s%s%s)", rtm
->rtm_family
,
580 colorize_start(bold
),
581 addr_family2str(rtm
->rtm_family
),
583 tprintf(", Dst Len %d", rtm
->rtm_dst_len
);
584 tprintf(", Src Len %d", rtm
->rtm_src_len
);
585 tprintf(", ToS %d", rtm
->rtm_tos
);
586 tprintf(", Table %d (%s%s%s)", rtm
->rtm_table
,
587 colorize_start(bold
),
588 route_table2str(rtm
->rtm_table
),
590 tprintf(", Proto %d (%s%s%s)", rtm
->rtm_protocol
,
591 colorize_start(bold
),
592 route_proto2str(rtm
->rtm_protocol
),
594 tprintf(", Scope %d (%s%s%s)", rtm
->rtm_scope
,
595 colorize_start(bold
),
596 scope2str(rtm
->rtm_scope
),
598 tprintf(", Type %d (%s%s%s)", rtm
->rtm_type
,
599 colorize_start(bold
),
600 route_type2str(rtm
->rtm_type
),
602 tprintf(", Flags 0x%x (%s%s%s) ]\n", rtm
->rtm_flags
,
603 colorize_start(bold
),
604 flags2str(route_flags
, rtm
->rtm_flags
, flags
,
608 for (; RTA_OK(attr
, attrs_len
); attr
= RTA_NEXT(attr
, attrs_len
)) {
609 switch (attr
->rta_type
) {
611 rta_fmt(attr
, "Dst %s", addr2str(rtm
->rtm_family
,
612 RTA_DATA(attr
), addr_str
, sizeof(addr_str
)));
615 rta_fmt(attr
, "Src %s", addr2str(rtm
->rtm_family
,
616 RTA_DATA(attr
), addr_str
, sizeof(addr_str
)));
619 rta_fmt(attr
, "Iif %d", RTA_INT(attr
));
622 rta_fmt(attr
, "Oif %d", RTA_INT(attr
));
625 rta_fmt(attr
, "Gateway %s", addr2str(rtm
->rtm_family
,
626 RTA_DATA(attr
), addr_str
, sizeof(addr_str
)));
629 rta_fmt(attr
, "Priority %u", RTA_UINT32(attr
));
632 rta_fmt(attr
, "Pref Src %s", addr2str(rtm
->rtm_family
,
633 RTA_DATA(attr
), addr_str
, sizeof(addr_str
)));
635 #if defined(RTA_MARK)
637 rta_fmt(attr
, "Mark 0x%x", RTA_UINT(attr
));
641 rta_fmt(attr
, "Flow 0x%x", RTA_UINT(attr
));
644 rta_fmt(attr
, "Table %d (%s%s%s)", RTA_UINT32(attr
),
645 colorize_start(bold
),
646 route_table2str(RTA_UINT32(attr
)),
651 tprintf("\tA: Cache (");
652 tprintf("expires(%ds)", ci
->rta_expires
/ hz
);
653 tprintf(", error(%d)", ci
->rta_error
);
654 tprintf(", users(%d)", ci
->rta_clntref
);
655 tprintf(", used(%d)", ci
->rta_used
);
656 tprintf(", last use(%ds)", ci
->rta_lastuse
/ hz
);
657 tprintf(", id(%d)", ci
->rta_id
);
658 tprintf(", ts(%d)", ci
->rta_ts
);
659 tprintf(", ts age(%ds))", ci
->rta_tsage
);
660 tprintf(", Len %d\n", RTA_LEN(attr
));
663 rta_fmt(attr
, "0x%x", attr
->rta_type
);
669 static struct flag_name neigh_states
[] = {
670 { "incomplete", NUD_INCOMPLETE
},
671 { "reachable", NUD_REACHABLE
},
672 { "stale", NUD_STALE
},
673 { "delay", NUD_DELAY
},
674 { "probe", NUD_PROBE
},
675 { "failed", NUD_FAILED
},
676 { "noarp", NUD_NOARP
},
677 { "permanent", NUD_PERMANENT
},
678 { "none", NUD_NONE
},
682 /* Copied from linux/neighbour.h */
684 # define NTF_USE 0x01
687 # define NTF_SELF 0x02
690 # define NTF_MASTER 0x04
693 # define NTF_PROXY 0x08
695 #ifndef NTF_EXT_LEARNED
696 # define NTF_EXT_LEARNED 0x10
699 # define NTF_ROUTER 0x80
702 static struct flag_name neigh_flags
[] = {
704 { "self", NTF_SELF
},
705 { "master", NTF_MASTER
},
706 { "proxy", NTF_PROXY
},
707 { "ext learned", NTF_EXT_LEARNED
},
708 { "router", NTF_ROUTER
},
712 static void rtnl_print_neigh(struct nlmsghdr
*hdr
)
714 struct ndmsg
*ndm
= NLMSG_DATA(hdr
);
715 uint32_t attrs_len
= NDA_PAYLOAD(hdr
);
716 struct rtattr
*attr
= NDA_RTA(ndm
);
717 struct nda_cacheinfo
*ci
;
718 int hz
= get_user_hz();
724 if (hdr
->nlmsg_len
< NLMSG_LENGTH(sizeof(*ndm
)))
727 tprintf(" [ Neigh Family %d (%s%s%s)", ndm
->ndm_family
,
728 colorize_start(bold
),
729 addr_family2str(ndm
->ndm_family
),
731 tprintf(", Link Index %d", ndm
->ndm_ifindex
);
732 tprintf(", State %d (%s%s%s)", ndm
->ndm_state
,
733 colorize_start(bold
),
734 flags2str(neigh_states
, ndm
->ndm_state
, states
,
737 tprintf(", Flags %d (%s%s%s)", ndm
->ndm_flags
,
738 colorize_start(bold
),
739 flags2str(neigh_flags
, ndm
->ndm_flags
, flags
,
742 tprintf(", Type %d (%s%s%s)", ndm
->ndm_type
,
743 colorize_start(bold
),
744 route_type2str(ndm
->ndm_type
),
748 for (; RTA_OK(attr
, attrs_len
); attr
= RTA_NEXT(attr
, attrs_len
)) {
749 switch (attr
->rta_type
) {
751 rta_fmt(attr
, "Address %s", addr2str(ndm
->ndm_family
,
752 RTA_DATA(attr
), addr_str
,
756 rta_fmt(attr
, "HW Address %s",
757 device_addr2str(RTA_DATA(attr
),
758 RTA_LEN(attr
), 0, hw_addr
,
762 rta_fmt(attr
, "Probes %d", RTA_UINT32(attr
));
766 tprintf("\tA: Cache (");
767 tprintf("confirmed(%ds)", ci
->ndm_confirmed
/ hz
);
768 tprintf(", used(%ds)", ci
->ndm_used
/ hz
);
769 tprintf(", updated(%ds)", ci
->ndm_updated
/ hz
);
770 tprintf(", refcnt(%d))", ci
->ndm_refcnt
);
771 tprintf(", Len %d\n", RTA_LEN(attr
));
774 rta_fmt(attr
, "0x%x", attr
->rta_type
);
780 static void rtnl_msg_print(struct nlmsghdr
*hdr
)
782 switch (hdr
->nlmsg_type
) {
787 rtnl_print_ifinfo(hdr
);
792 rtnl_print_ifaddr(hdr
);
797 rtnl_print_route(hdr
);
802 rtnl_print_neigh(hdr
);
807 static const char *genl_cmd2str(uint8_t table
)
810 case CTRL_CMD_UNSPEC
: return "unspec";
811 case CTRL_CMD_NEWFAMILY
: return "new family";
812 case CTRL_CMD_DELFAMILY
: return "del family";
813 case CTRL_CMD_GETFAMILY
: return "get family";
814 case CTRL_CMD_NEWOPS
: return "new ops";
815 case CTRL_CMD_DELOPS
: return "del ops";
816 case CTRL_CMD_GETOPS
: return "get ops";
817 case CTRL_CMD_NEWMCAST_GRP
: return "new mcast group";
818 case CTRL_CMD_DELMCAST_GRP
: return "del mcast group";
819 case CTRL_CMD_GETMCAST_GRP
: return "get mcast group";
821 default: return "Unknown";
825 static struct flag_name genl_ops_flags
[] = {
826 { "admin", GENL_ADMIN_PERM
},
827 { "doit", GENL_CMD_CAP_DO
},
828 { "dumpit", GENL_CMD_CAP_DUMP
},
829 { "policy", GENL_CMD_CAP_HASPOL
},
833 static void genl_print_ops_attr(struct nlattr
*attr
, uint32_t attr_len
)
838 for (; NLA_OK(attr
, attr_len
); attr
= NLA_NEXT(attr
, attr_len
)) {
839 switch (attr
->nla_type
) {
840 case CTRL_ATTR_OP_ID
:
841 nla_fmt_nested(attr
, "Id 0x%x", NLA_UINT32(attr
));
844 case CTRL_ATTR_OP_FLAGS
:
845 flags
= NLA_UINT32(attr
);
847 nla_fmt_nested(attr
, "Flags 0x%x (%s%s%s)", flags
,
848 colorize_start(bold
),
849 flags2str(genl_ops_flags
, flags
, str
, sizeof(str
)),
853 nla_fmt_nested(attr
, "0x%x", attr
->nla_type
);
859 static void genl_print_ops_list(struct nlattr
*attr
, uint32_t attr_len
)
861 for (; NLA_OK(attr
, attr_len
); attr
= NLA_NEXT(attr
, attr_len
)) {
862 nla_fmt_nested_start(attr
, "0x%x", attr
->nla_type
);
863 genl_print_ops_attr(NLA_DATA(attr
), NLA_LEN(attr
));
864 nla_fmt_nested_end();
868 static void genl_print_mcast_group(struct nlattr
*attr
, uint32_t attr_len
)
870 for (; NLA_OK(attr
, attr_len
); attr
= NLA_NEXT(attr
, attr_len
)) {
871 switch (attr
->nla_type
) {
872 case CTRL_ATTR_MCAST_GRP_ID
:
873 nla_fmt_nested(attr
, "Id 0x%x", NLA_UINT32(attr
));
876 case CTRL_ATTR_MCAST_GRP_NAME
:
877 nla_fmt_nested(attr
, "Name %s", NLA_STR(attr
));
880 nla_fmt_nested(attr
, "0x%x", attr
->nla_type
);
886 static void genl_print_mc_groups(struct nlattr
*attr
, uint32_t attr_len
)
888 for (; NLA_OK(attr
, attr_len
); attr
= NLA_NEXT(attr
, attr_len
)) {
889 nla_fmt_nested_start(attr
, "0x%x", attr
->nla_type
);
890 genl_print_mcast_group(NLA_DATA(attr
), NLA_LEN(attr
));
891 nla_fmt_nested_end();
895 static void genl_print_ctrl_attrs(struct nlmsghdr
*hdr
)
897 struct genlmsghdr
*genl
= NLMSG_DATA(hdr
);
898 struct nlattr
*attr
= GEN_NLA(genl
);
899 uint32_t attrs_len
= NLMSG_PAYLOAD(hdr
, sizeof(struct genlmsghdr
));
901 for (; NLA_OK(attr
, attrs_len
); attr
= NLA_NEXT(attr
, attrs_len
)) {
902 switch (attr
->nla_type
) {
903 case CTRL_ATTR_FAMILY_ID
:
904 nla_fmt(attr
, "Family Id 0x%x", NLA_UINT16(attr
));
906 case CTRL_ATTR_FAMILY_NAME
:
907 nla_fmt(attr
, "Family Name %s", NLA_STR(attr
));
909 case CTRL_ATTR_VERSION
:
910 nla_fmt(attr
, "Version %u", NLA_UINT32(attr
));
912 case CTRL_ATTR_HDRSIZE
:
913 nla_fmt(attr
, "Header size %u", NLA_UINT32(attr
));
915 case CTRL_ATTR_MAXATTR
:
916 nla_fmt(attr
, "Max attr value 0x%x", NLA_UINT32(attr
));
919 nla_fmt(attr
, "Ops list");
920 genl_print_ops_list(NLA_DATA(attr
), NLA_LEN(attr
));
922 case CTRL_ATTR_MCAST_GROUPS
:
923 nla_fmt(attr
, "Mcast groups");
924 genl_print_mc_groups(NLA_DATA(attr
), NLA_LEN(attr
));
927 nla_fmt(attr
, "0x%x", attr
->nla_type
);
933 static void genl_msg_print(struct nlmsghdr
*hdr
)
935 struct genlmsghdr
*genl
;
937 if (hdr
->nlmsg_type
!= GENL_ID_CTRL
) {
938 nlmsg_print_raw(hdr
);
942 genl
= NLMSG_DATA(hdr
);
944 tprintf(" [ Cmd %u (%s%s%s)", genl
->cmd
,
945 colorize_start(bold
), genl_cmd2str(genl
->cmd
), colorize_end());
946 tprintf(", Version %u", genl
->version
);
947 tprintf(", Reserved %u", genl
->reserved
);
950 genl_print_ctrl_attrs(hdr
);
953 static void nlmsg_print(uint16_t family
, struct nlmsghdr
*hdr
)
955 u16 nlmsg_flags
= hdr
->nlmsg_flags
;
958 char procname
[PATH_MAX
];
960 /* Look up the process name if message is not coming from the kernel.
962 * Note that the port id is not necessarily equal to the PID of the
963 * receiving process (e.g. if the application is multithreaded or using
964 * multiple sockets). In these cases we're not able to find a matching
965 * PID and the information will not be printed.
967 if (hdr
->nlmsg_pid
!= 0) {
968 if (proc_get_cmdline(hdr
->nlmsg_pid
, procname
, sizeof(procname
)) < 0)
969 snprintf(procname
, sizeof(procname
), "unknown process");
971 snprintf(procname
, sizeof(procname
), "kernel");
973 tprintf(" [ NLMSG ");
974 tprintf("Family %d (%s%s%s), ", family
,
975 colorize_start(bold
),
976 nlmsg_family2str(family
),
978 tprintf("Len %u, ", hdr
->nlmsg_len
);
979 tprintf("Type 0x%.4x (%s%s%s), ", hdr
->nlmsg_type
,
980 colorize_start(bold
),
981 nlmsg_type2str(family
, hdr
->nlmsg_type
, type
, sizeof(type
)),
983 tprintf("Flags 0x%.4x (%s%s%s), ", nlmsg_flags
,
984 colorize_start(bold
),
985 nlmsg_flags
? nl_nlmsg_flags2str(nlmsg_flags
, flags
, sizeof(flags
)) : "none",
987 tprintf("Seq-Nr %u, ", hdr
->nlmsg_seq
);
988 tprintf("PID %u", hdr
->nlmsg_pid
);
990 tprintf(" (%s%s%s)", colorize_start(bold
), basename(procname
),
998 case NETLINK_GENERIC
:
1002 nlmsg_print_raw(hdr
);
1006 static void nlmsg(struct pkt_buff
*pkt
)
1008 struct nlmsghdr
*hdr
= (struct nlmsghdr
*) pkt_pull(pkt
, NLMSG_HDRLEN
);
1009 unsigned int trim_len
= pkt_len(pkt
);
1012 trim_len
-= hdr
->nlmsg_len
;
1013 nlmsg_print(ntohs(pkt
->sll
->sll_protocol
), hdr
);
1015 if (!pkt_pull(pkt
, NLMSG_ALIGN(hdr
->nlmsg_len
) - NLMSG_HDRLEN
))
1018 hdr
= (struct nlmsghdr
*) pkt_pull(pkt
, NLMSG_HDRLEN
);
1021 if (hdr
->nlmsg_len
== 0)
1024 if (hdr
->nlmsg_type
!= NLMSG_DONE
&&
1025 (hdr
->nlmsg_flags
& NLM_F_MULTI
))
1029 /* mmaped packet? */
1030 if (hdr
&& hdr
->nlmsg_len
== 0)
1031 pkt_trim(pkt
, trim_len
);
1034 static void nlmsg_less(struct pkt_buff
*pkt
)
1036 struct nlmsghdr
*hdr
= (struct nlmsghdr
*) pkt_pull(pkt
, NLMSG_HDRLEN
);
1037 uint16_t family
= ntohs(pkt
->sll
->sll_protocol
);
1043 tprintf(" NLMSG Family %d (%s%s%s), ", family
,
1044 colorize_start(bold
),
1045 nlmsg_family2str(family
),
1047 tprintf("Type %u (%s%s%s)", hdr
->nlmsg_type
,
1048 colorize_start(bold
),
1049 nlmsg_type2str(family
, hdr
->nlmsg_type
, type
, sizeof(type
)),
1053 struct protocol nlmsg_ops
= {
1054 .print_full
= nlmsg
,
1055 .print_less
= nlmsg_less
,