2 * netsniff-ng - the packet sniffing beast
3 * Copyright 2009, 2010 Daniel Borkmann.
4 * Copyright 2014 Tobias Klauser
5 * Subject to the GPL, version 2.
10 #include <netinet/in.h>
11 #include <linux/if_ether.h>
14 #include "dissector_eth.h"
18 static inline bool is_multicast_ether_addr(const uint8_t *mac
)
23 static inline bool is_broadcast_ether_addr(const uint8_t *mac
)
25 return (mac
[0] & mac
[1] & mac
[2] & mac
[3] & mac
[4] & mac
[5]) == 0xff;
28 static const char *ether_lookup_addr(uint8_t *mac
)
30 if (is_multicast_ether_addr(mac
)) {
31 if (is_broadcast_ether_addr(mac
))
37 /* found no matching address, so look up the vendor from OUI */
38 return lookup_vendor_str((mac
[0] << 16) | (mac
[1] << 8) | mac
[2]);
41 static void ethernet(struct pkt_buff
*pkt
)
44 uint8_t *src_mac
, *dst_mac
;
45 struct ethhdr
*eth
= (struct ethhdr
*) pkt_pull(pkt
, sizeof(*eth
));
50 src_mac
= eth
->h_source
;
51 dst_mac
= eth
->h_dest
;
54 tprintf("MAC (%.2x:%.2x:%.2x:%.2x:%.2x:%.2x => ",
55 src_mac
[0], src_mac
[1], src_mac
[2],
56 src_mac
[3], src_mac
[4], src_mac
[5]);
57 tprintf("%.2x:%.2x:%.2x:%.2x:%.2x:%.2x), ",
58 dst_mac
[0], dst_mac
[1], dst_mac
[2],
59 dst_mac
[3], dst_mac
[4], dst_mac
[5]);
60 tprintf("Proto (0x%.4x", ntohs(eth
->h_proto
));
62 type
= lookup_ether_type(ntohs(eth
->h_proto
));
64 tprintf(", %s%s%s", colorize_start(bold
), type
, colorize_end());
67 tprintf(" [ Vendor ");
68 tprintf("(%s => %s)", ether_lookup_addr(src_mac
), ether_lookup_addr(dst_mac
));
71 pkt_set_proto(pkt
, ð_lay2
, ntohs(eth
->h_proto
));
74 static void ethernet_less(struct pkt_buff
*pkt
)
76 uint8_t *src_mac
, *dst_mac
;
77 struct ethhdr
*eth
= (struct ethhdr
*) pkt_pull(pkt
, sizeof(*eth
));
82 src_mac
= eth
->h_source
;
83 dst_mac
= eth
->h_dest
;
85 lookup_vendor_str((src_mac
[0] << 16) | (src_mac
[1] << 8) |
87 lookup_vendor_str((dst_mac
[0] << 16) | (dst_mac
[1] << 8) |
89 tprintf("%s%s%s", colorize_start(bold
),
90 lookup_ether_type(ntohs(eth
->h_proto
)), colorize_end());
92 pkt_set_proto(pkt
, ð_lay2
, ntohs(eth
->h_proto
));
95 struct protocol ethernet_ops
= {
97 .print_full
= ethernet
,
98 .print_less
= ethernet_less
,