ring: use xzmalloc_aligned
[netsniff-ng.git] / dissector.h
blob068924743789460b45af926ddb269f1bf73d763d
1 /*
2 * netsniff-ng - the packet sniffing beast
3 * Copyright 2009 - 2013 Daniel Borkmann.
4 * Subject to the GPL, version 2.
5 */
7 #ifndef DISSECTOR_H
8 #define DISSECTOR_H
10 #include <stdlib.h>
11 #include <stdint.h>
12 #include <sys/socket.h>
13 #include <linux/if.h>
14 #include <linux/netlink.h>
15 #include <linux/if_packet.h>
17 #include "ring.h"
18 #include "tprintf.h"
19 #include "linktype.h"
20 #include "vlan.h"
22 #define PRINT_NORM 0
23 #define PRINT_LESS 1
24 #define PRINT_HEX 2
25 #define PRINT_ASCII 3
26 #define PRINT_HEX_ASCII 4
27 #define PRINT_NONE 5
29 extern char *if_indextoname(unsigned ifindex, char *ifname);
31 static const char * const packet_types[256] = {
32 [PACKET_HOST] = "<", /* Incoming */
33 [PACKET_BROADCAST] = "B", /* Broadcast */
34 [PACKET_MULTICAST] = "M", /* Multicast */
35 [PACKET_OTHERHOST] = "P", /* Promisc */
36 [PACKET_OUTGOING] = ">", /* Outgoing */
37 [PACKET_USER] = "K->U", /* To Userspace */
38 [PACKET_KERNEL] = "U->K", /* To Kernelspace */
41 static inline const char *__show_ts_source(uint32_t status)
43 if (status & TP_STATUS_TS_RAW_HARDWARE)
44 return "(raw hw ts)";
45 else if (status & TP_STATUS_TS_SYS_HARDWARE)
46 return "(sys hw ts)";
47 else if (status & TP_STATUS_TS_SOFTWARE)
48 return "(sw ts)";
49 else
50 return "";
53 static inline void __show_frame_hdr(uint8_t *packet, size_t len, int linktype,
54 struct sockaddr_ll *s_ll, void *raw_hdr,
55 int mode, bool v3, unsigned long count)
57 char tmp[IFNAMSIZ];
58 union tpacket_uhdr hdr;
59 uint8_t pkttype = s_ll->sll_pkttype;
60 bool is_nl;
62 if (mode == PRINT_NONE)
63 return;
66 * If we're capturing on nlmon0, all packets will have sll_pkttype set
67 * to PACKET_OUTGOING, but we actually want PACKET_USER/PACKET_KERNEL as
68 * it originally was set in the kernel. Thus, use nlmsghdr->nlmsg_pid to
69 * restore the type.
71 is_nl = (linktype == LINKTYPE_NETLINK && len >= sizeof(struct nlmsghdr));
72 if (is_nl && pkttype == PACKET_OUTGOING) {
73 struct nlmsghdr *hdr = (struct nlmsghdr *) packet;
74 pkttype = hdr->nlmsg_pid == 0 ? PACKET_KERNEL : PACKET_USER;
77 hdr.raw = raw_hdr;
78 switch (mode) {
79 case PRINT_LESS:
80 tprintf("%s %s %u #%lu",
81 packet_types[pkttype] ? : "?",
82 if_indextoname(s_ll->sll_ifindex, tmp) ? : "?",
83 tpacket_uhdr(hdr, tp_len, v3),
84 count);
85 break;
86 default:
87 tprintf("%s %s %u %us.%uns #%lu %s\n",
88 packet_types[pkttype] ? : "?",
89 if_indextoname(s_ll->sll_ifindex, tmp) ? : "?",
90 tpacket_uhdr(hdr, tp_len, v3),
91 tpacket_uhdr(hdr, tp_sec, v3),
92 tpacket_uhdr(hdr, tp_nsec, v3),
93 count,
94 v3 ? "" : __show_ts_source(hdr.h2->tp_status));
96 if (tpacket_has_vlan_info(&hdr)) {
97 uint16_t tci = tpacket_uhdr_vlan_tci(&hdr, v3);
99 tprintf(" [ tpacketv3 VLAN ");
100 tprintf("Prio (%u), ", vlan_tci2prio(tci));
101 tprintf("CFI (%u), ", vlan_tci2cfi(tci));
102 tprintf("ID (%u), ", vlan_tci2vid(tci));
103 tprintf("Proto (0x%.4x)", tpacket_uhdr_vlan_proto(&hdr, v3));
104 tprintf(" ]\n");
106 break;
110 static inline void show_frame_hdr(uint8_t *packet, size_t len, int linktype,
111 struct frame_map *hdr, int mode,
112 unsigned long count)
114 __show_frame_hdr(packet, len, linktype, &hdr->s_ll, &hdr->tp_h, mode,
115 false, count);
118 extern void dissector_init_all(int fnttype);
119 extern void dissector_entry_point(uint8_t *packet, size_t len, int linktype,
120 int mode, struct sockaddr_ll *sll);
121 extern void dissector_cleanup_all(void);
122 extern int dissector_set_print_type(void *ptr, int type);
124 #endif /* DISSECTOR_H */