netsniff-ng: dissector_sll: Fix potential NULL dereference
[netsniff-ng.git] / dissector_sll.c
blobc9202df01486d7e914dcb740820c9fd8df719478
1 /*
2 * netsniff-ng - the packet sniffing beast
3 * Subject to the GPL, version 2.
4 */
6 #include "oui.h"
7 #include "protos.h"
8 #include "pcap_io.h"
9 #include "pkt_buff.h"
10 #include "dissector.h"
11 #include "dissector_sll.h"
12 #include "dissector_eth.h"
14 static char *pkt_type2str(uint8_t pkttype)
16 switch (pkttype) {
17 case PACKET_HOST:
18 return "host";
19 case PACKET_BROADCAST:
20 return "broadcast";
21 case PACKET_MULTICAST:
22 return "multicast";
23 case PACKET_OTHERHOST:
24 return "other host";
25 case PACKET_OUTGOING:
26 return "outgoing";
27 case PACKET_USER:
28 return "user";
29 case PACKET_KERNEL:
30 return "kernel";
33 return "Unknown";
36 static void sll_print_full(struct pkt_buff *pkt)
38 struct sockaddr_ll *sll;
39 char addr_str[40] = {};
41 if (!pkt || !pkt->sll)
42 return;
44 sll = pkt->sll;
45 tprintf(" [ Linux \"cooked\"");
46 tprintf(" Pkt Type %d (%s)", sll->sll_pkttype,
47 pkt_type2str(sll->sll_pkttype));
48 tprintf(", If Type %d (%s)", sll->sll_hatype,
49 device_type2str(sll->sll_hatype));
50 tprintf(", Addr Len %d", sll->sll_halen);
51 tprintf(", Src (%s)", device_addr2str(sll->sll_addr, sll->sll_halen,
52 sll->sll_hatype, addr_str, sizeof(addr_str)));
53 tprintf(", Proto 0x%x", ntohs(sll->sll_protocol));
54 tprintf(" ]\n");
56 switch (pcap_devtype_to_linktype(sll->sll_hatype)) {
57 case LINKTYPE_EN10MB:
58 case ___constant_swab32(LINKTYPE_EN10MB):
59 pkt_set_dissector(pkt, &eth_lay2, ntohs(sll->sll_protocol));
60 break;
61 case LINKTYPE_NETLINK:
62 case ___constant_swab32(LINKTYPE_NETLINK):
63 pkt->dissector = &nlmsg_ops;
64 break;
65 default:
66 tprintf(" [ Uknown protocol ]\n");
70 static void sll_print_less(struct pkt_buff *pkt)
72 struct sockaddr_ll *sll;
73 char addr_str[40] = {};
75 if (!pkt || !pkt->sll)
76 return;
78 sll = pkt->sll;
79 tprintf(" Pkt Type %d (%s)", sll->sll_pkttype,
80 pkt_type2str(sll->sll_pkttype));
81 tprintf(", If Type %d (%s)", sll->sll_hatype,
82 device_type2str(sll->sll_hatype));
83 tprintf(", Addr Len %d", sll->sll_halen);
84 tprintf(", Src (%s)", device_addr2str(sll->sll_addr, sll->sll_halen,
85 sll->sll_hatype, addr_str, sizeof(addr_str)));
86 tprintf(", Proto 0x%x", ntohs(sll->sll_protocol));
89 struct protocol sll_ops = {
90 .key = 0,
91 .print_full = sll_print_full,
92 .print_less = sll_print_less,
95 struct protocol *dissector_get_sll_entry_point(void)
97 return &sll_ops;
100 struct protocol *dissector_get_sll_exit_point(void)
102 return &none_ops;
105 void dissector_init_sll(int fnttype)
107 dissector_set_print_type(&sll_ops, fnttype);
108 dissector_set_print_type(&none_ops, fnttype);
109 dissector_init_oui();
112 void dissector_cleanup_sll(void)
114 dissector_cleanup_oui();