2 * netsniff-ng - the packet sniffing beast
3 * Copyright 2009 - 2013 Daniel Borkmann.
4 * Subject to the GPL, version 2.
12 #include <sys/socket.h>
13 #include <linux/if_packet.h>
15 #include <netlink/msg.h>
25 #define PRINT_HEX_ASCII 4
28 extern char *if_indextoname(unsigned ifindex
, char *ifname
);
30 static const char * const packet_types
[256] = {
31 [PACKET_HOST
] = "<", /* Incoming */
32 [PACKET_BROADCAST
] = "B", /* Broadcast */
33 [PACKET_MULTICAST
] = "M", /* Multicast */
34 [PACKET_OTHERHOST
] = "P", /* Promisc */
35 [PACKET_OUTGOING
] = ">", /* Outgoing */
36 [PACKET_USER
] = ">U", /* To Userspace */
37 [PACKET_KERNEL
] = ">K", /* To Kernelspace */
40 static inline const char *__show_ts_source(uint32_t status
)
42 if (status
& TP_STATUS_TS_RAW_HARDWARE
)
44 else if (status
& TP_STATUS_TS_SYS_HARDWARE
)
46 else if (status
& TP_STATUS_TS_SOFTWARE
)
52 static inline void __show_frame_hdr(uint8_t *packet
, size_t len
, int linktype
,
53 struct sockaddr_ll
*s_ll
, void *raw_hdr
,
54 int mode
, bool v3
, unsigned long count
)
57 union tpacket_uhdr hdr
;
58 uint8_t pkttype
= s_ll
->sll_pkttype
;
61 if (mode
== PRINT_NONE
)
65 * If we're capturing on nlmon0, all packets will have sll_pkttype set
66 * to PACKET_OUTGOING, but we actually want PACKET_USER/PACKET_KERNEL as
67 * it originally was set in the kernel. Thus, use nlmsghdr->nlmsg_pid to
70 is_nl
= (linktype
== LINKTYPE_NETLINK
&& len
>= sizeof(struct nlmsghdr
));
71 if (is_nl
&& pkttype
== PACKET_OUTGOING
) {
72 struct nlmsghdr
*hdr
= (struct nlmsghdr
*) packet
;
73 pkttype
= hdr
->nlmsg_pid
== 0 ? PACKET_KERNEL
: PACKET_USER
;
79 tprintf("%s %s %u #%lu",
80 packet_types
[pkttype
] ? : "?",
81 if_indextoname(s_ll
->sll_ifindex
, tmp
) ? : "?",
82 tpacket_uhdr(hdr
, tp_len
, v3
),
86 tprintf("%s %s %u %us.%uns #%lu %s\n",
87 packet_types
[pkttype
] ? : "?",
88 if_indextoname(s_ll
->sll_ifindex
, tmp
) ? : "?",
89 tpacket_uhdr(hdr
, tp_len
, v3
),
90 tpacket_uhdr(hdr
, tp_sec
, v3
),
91 tpacket_uhdr(hdr
, tp_nsec
, v3
),
93 v3
? "" : __show_ts_source(hdr
.h2
->tp_status
));
98 static inline void show_frame_hdr(uint8_t *packet
, size_t len
, int linktype
,
99 struct frame_map
*hdr
, int mode
,
102 __show_frame_hdr(packet
, len
, linktype
, &hdr
->s_ll
, &hdr
->tp_h
, mode
,
106 extern void dissector_init_all(int fnttype
);
107 extern void dissector_entry_point(uint8_t *packet
, size_t len
, int linktype
,
108 int mode
, struct sockaddr_ll
*sll
);
109 extern void dissector_cleanup_all(void);
110 extern int dissector_set_print_type(void *ptr
, int type
);
112 #endif /* DISSECTOR_H */