2 * netsniff-ng - the packet sniffing beast
3 * Copyright 2014 Tobias Klauser.
4 * Subject to the GPL, version 2.
11 #include <netlink/msg.h>
17 static const char *nl_proto2str(uint16_t proto
)
20 case NETLINK_ROUTE
: return "RTNETLINK";
21 case NETLINK_UNUSED
: return "UNUSED";
22 case NETLINK_USERSOCK
: return "USERSOCK";
23 case NETLINK_FIREWALL
: return "FIREWALL";
24 /* NETLINK_INET_DIAG was renamed to NETLINK_SOCK_DIAG in Linux kernel 3.10 */
25 #if defined(NETLINK_SOCK_DIAG)
26 case NETLINK_SOCK_DIAG
: return "SOCK_DIAG";
27 #elif defined(NETLINK_INET_DIAG)
28 case NETLINK_INET_DIAG
: return "INET_DIAG";
30 case NETLINK_NFLOG
: return "NFLOG";
31 case NETLINK_XFRM
: return "XFRM";
32 case NETLINK_SELINUX
: return "SELINUX";
33 case NETLINK_ISCSI
: return "ISCSI";
34 case NETLINK_AUDIT
: return "AUDIT";
35 case NETLINK_FIB_LOOKUP
: return "FIB_LOOKUP";
36 case NETLINK_CONNECTOR
: return "CONNECTOR";
37 case NETLINK_NETFILTER
: return "NETFILTER";
38 case NETLINK_IP6_FW
: return "IP6_FW";
39 case NETLINK_DNRTMSG
: return "DNRTMSG";
40 case NETLINK_KOBJECT_UEVENT
: return "UEVENT";
41 case NETLINK_GENERIC
: return "GENERIC";
42 case NETLINK_SCSITRANSPORT
: return "SCSI";
43 case NETLINK_ECRYPTFS
: return "ECRYPTFS";
44 case NETLINK_RDMA
: return "RDMA";
45 case NETLINK_CRYPTO
: return "CRYPTO";
46 default: return "Unknown";
50 static void nlmsg(struct pkt_buff
*pkt
)
52 struct nlmsghdr
*hdr
= (struct nlmsghdr
*) pkt_pull(pkt
, sizeof(*hdr
));
55 char procname
[PATH_MAX
];
60 /* Look up the process name if message is not coming from the kernel.
62 * Note that the port id is not necessarily equal to the PID of the
63 * receiving process (e.g. if the application is multithreaded or using
64 * multiple sockets). In these cases we're not able to find a matching
65 * PID and the information will not be printed.
67 if (hdr
->nlmsg_pid
!= 0) {
71 snprintf(path
, sizeof(path
), "/proc/%u/exe", hdr
->nlmsg_pid
);
72 ret
= readlink(path
, procname
, sizeof(procname
) - 1);
77 snprintf(procname
, sizeof(procname
), "kernel");
80 tprintf("Proto %d (%s%s%s), ", ntohs(pkt
->proto
), colorize_start(bold
),
81 nl_proto2str(ntohs(pkt
->proto
)), colorize_end());
82 tprintf("Len %u, ", hdr
->nlmsg_len
);
83 tprintf("Type 0x%.4x (%s%s%s), ", hdr
->nlmsg_type
,
85 nl_nlmsgtype2str(hdr
->nlmsg_type
, type
, sizeof(type
)),
87 tprintf("Flags 0x%.4x (%s%s%s), ", hdr
->nlmsg_flags
,
89 nl_nlmsg_flags2str(hdr
->nlmsg_flags
, flags
, sizeof(flags
)),
91 tprintf("Seq-Nr %u, ", hdr
->nlmsg_seq
);
92 tprintf("PID %u", hdr
->nlmsg_pid
);
94 tprintf(" (%s%s%s)", colorize_start(bold
), basename(procname
),
99 static void nlmsg_less(struct pkt_buff
*pkt
)
101 struct nlmsghdr
*hdr
= (struct nlmsghdr
*) pkt_pull(pkt
, sizeof(*hdr
));
107 tprintf(" NLMSG %u (%s%s%s)", hdr
->nlmsg_type
, colorize_start(bold
),
108 nl_nlmsgtype2str(hdr
->nlmsg_type
, type
, sizeof(type
)),
112 struct protocol nlmsg_ops
= {
114 .print_less
= nlmsg_less
,