netsniff-ng: Move variable definition
[netsniff-ng.git] / proto_ethernet.c
blob98d40e51fb55f04e279974072a35444dcb316f9c
1 /*
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.
6 */
8 #include <stdio.h>
9 #include <stdint.h>
10 #include <netinet/in.h>
11 #include <linux/if_ether.h>
13 #include "proto.h"
14 #include "dissector_eth.h"
15 #include "lookup.h"
16 #include "pkt_buff.h"
17 #include "oui.h"
19 static inline bool is_multicast_ether_addr(const uint8_t *mac)
21 return mac[0] & 0x01;
24 static inline bool is_broadcast_ether_addr(const uint8_t *mac)
26 return (mac[0] & mac[1] & mac[2] & mac[3] & mac[4] & mac[5]) == 0xff;
29 static const char *ether_lookup_addr(uint8_t *mac)
31 if (is_multicast_ether_addr(mac)) {
32 if (is_broadcast_ether_addr(mac))
33 return "Broadcast";
34 else
35 return "Multicast";
38 /* found no matching address, so look up the vendor from OUI */
39 return lookup_vendor_str((mac[0] << 16) | (mac[1] << 8) | mac[2]);
42 static void ethernet(struct pkt_buff *pkt)
44 char *type;
45 uint8_t *src_mac, *dst_mac;
46 struct ethhdr *eth = (struct ethhdr *) pkt_pull(pkt, sizeof(*eth));
48 if (eth == NULL)
49 return;
51 src_mac = eth->h_source;
52 dst_mac = eth->h_dest;
54 tprintf(" [ Eth ");
55 tprintf("MAC (%.2x:%.2x:%.2x:%.2x:%.2x:%.2x => ",
56 src_mac[0], src_mac[1], src_mac[2],
57 src_mac[3], src_mac[4], src_mac[5]);
58 tprintf("%.2x:%.2x:%.2x:%.2x:%.2x:%.2x), ",
59 dst_mac[0], dst_mac[1], dst_mac[2],
60 dst_mac[3], dst_mac[4], dst_mac[5]);
61 tprintf("Proto (0x%.4x", ntohs(eth->h_proto));
63 type = lookup_ether_type(ntohs(eth->h_proto));
64 if (type)
65 tprintf(", %s%s%s", colorize_start(bold), type, colorize_end());
67 tprintf(") ]\n");
68 tprintf(" [ Vendor ");
69 tprintf("(%s => %s)", ether_lookup_addr(src_mac), ether_lookup_addr(dst_mac));
70 tprintf(" ]\n");
72 pkt_set_proto(pkt, &eth_lay2, ntohs(eth->h_proto));
75 static void ethernet_less(struct pkt_buff *pkt)
77 uint8_t *src_mac, *dst_mac;
78 struct ethhdr *eth = (struct ethhdr *) pkt_pull(pkt, sizeof(*eth));
80 if (eth == NULL)
81 return;
83 src_mac = eth->h_source;
84 dst_mac = eth->h_dest;
85 tprintf(" %s => %s ",
86 lookup_vendor_str((src_mac[0] << 16) | (src_mac[1] << 8) |
87 src_mac[2]),
88 lookup_vendor_str((dst_mac[0] << 16) | (dst_mac[1] << 8) |
89 dst_mac[2]));
90 tprintf("%s%s%s", colorize_start(bold),
91 lookup_ether_type(ntohs(eth->h_proto)), colorize_end());
93 pkt_set_proto(pkt, &eth_lay2, ntohs(eth->h_proto));
96 struct protocol ethernet_ops = {
97 .key = 0,
98 .print_full = ethernet,
99 .print_less = ethernet_less,