2 * netsniff-ng - the packet sniffing beast
3 * By Daniel Borkmann <daniel@netsniff-ng.org>
4 * Copyright 2009, 2010 Daniel Borkmann.
5 * Subject to the GPL, version 2.
14 #include "dissector.h"
15 #include "dissector_eth.h"
19 struct hash_table eth_lay2
;
20 struct hash_table eth_lay3
;
22 static struct hash_table eth_ether_types
;
23 static struct hash_table eth_ports_udp
;
24 static struct hash_table eth_ports_tcp
;
29 struct port_tcp
*next
;
35 struct port_udp
*next
;
41 struct ether_type
*next
;
44 /* Note: this macro only applies to the lookup_* functions here in this file,
45 * mainly to remove redundand code. */
46 #define __do_lookup_inline(id, struct_name, hash_ptr, struct_member) \
48 struct struct_name *entry = lookup_hash(id, hash_ptr); \
49 while (entry && id != entry->id) \
50 entry = entry->next; \
51 (entry && id == entry->id ? entry->struct_member : 0);\
54 char *lookup_port_udp(unsigned int id
)
56 return __do_lookup_inline(id
, port_udp
, ð_ports_udp
, port
);
59 char *lookup_port_tcp(unsigned int id
)
61 return __do_lookup_inline(id
, port_tcp
, ð_ports_tcp
, port
);
64 char *lookup_ether_type(unsigned int id
)
66 return __do_lookup_inline(id
, ether_type
, ð_ether_types
, type
);
70 static inline void dissector_init_entry(int type
)
72 dissector_set_print_type(ðernet_ops
, type
);
75 static inline void dissector_init_exit(int type
)
77 dissector_set_print_type(&none_ops
, type
);
80 static void dissector_init_layer_2(int type
)
83 INSERT_HASH_PROTOS(arp_ops
, eth_lay2
);
84 INSERT_HASH_PROTOS(vlan_ops
, eth_lay2
);
85 INSERT_HASH_PROTOS(ipv4_ops
, eth_lay2
);
86 INSERT_HASH_PROTOS(ipv6_ops
, eth_lay2
);
87 INSERT_HASH_PROTOS(QinQ_ops
, eth_lay2
);
88 INSERT_HASH_PROTOS(mpls_uc_ops
, eth_lay2
);
89 for_each_hash_int(ð_lay2
, dissector_set_print_type
, type
);
92 static void dissector_init_layer_3(int type
)
95 INSERT_HASH_PROTOS(icmpv4_ops
, eth_lay3
);
96 INSERT_HASH_PROTOS(icmpv6_ops
, eth_lay3
);
97 INSERT_HASH_PROTOS(igmp_ops
, eth_lay3
);
98 INSERT_HASH_PROTOS(ip_auth_ops
, eth_lay3
);
99 INSERT_HASH_PROTOS(ip_esp_ops
, eth_lay3
);
100 INSERT_HASH_PROTOS(ipv6_dest_opts_ops
, eth_lay3
);
101 INSERT_HASH_PROTOS(ipv6_fragm_ops
, eth_lay3
);
102 INSERT_HASH_PROTOS(ipv6_hop_by_hop_ops
, eth_lay3
);
103 INSERT_HASH_PROTOS(ipv6_in_ipv4_ops
, eth_lay3
);
104 INSERT_HASH_PROTOS(ipv6_mobility_ops
, eth_lay3
);
105 INSERT_HASH_PROTOS(ipv6_no_next_header_ops
, eth_lay3
);
106 INSERT_HASH_PROTOS(ipv6_routing_ops
, eth_lay3
);
107 INSERT_HASH_PROTOS(tcp_ops
, eth_lay3
);
108 INSERT_HASH_PROTOS(udp_ops
, eth_lay3
);
109 for_each_hash_int(ð_lay3
, dissector_set_print_type
, type
);
112 static inline void dissector_init_entry(int type
) {}
113 static inline void dissector_init_exit(int type
) {}
114 static void dissector_init_layer_2(int type
) {}
115 static void dissector_init_layer_3(int type
) {}
116 #endif /* __WITH_PROTOS */
118 static void dissector_init_ports_udp(void)
121 char buff
[512], *ptr
;
122 struct port_udp
*pudp
;
124 fp
= fopen("/etc/netsniff-ng/udp.conf", "r");
126 panic("No /etc/netsniff-ng/udp.conf found!\n");
127 memset(buff
, 0, sizeof(buff
));
128 while (fgets(buff
, sizeof(buff
), fp
) != NULL
) {
129 buff
[sizeof(buff
) - 1] = 0;
130 pudp
= xmalloc(sizeof(*pudp
));
133 ptr
= getuint(ptr
, &pudp
->id
);
135 ptr
= skipchar(ptr
, ',');
137 ptr
= strtrim_right(ptr
, '\n');
138 ptr
= strtrim_right(ptr
, ' ');
139 pudp
->port
= xstrdup(ptr
);
141 pos
= insert_hash(pudp
->id
, pudp
, ð_ports_udp
);
146 memset(buff
, 0, sizeof(buff
));
151 static int dissector_cleanup_ports_udp(void *ptr
)
153 struct port_udp
*tmp
, *p
= ptr
;
156 while ((tmp
= p
->next
)) {
166 static void dissector_init_ports_tcp(void)
169 char buff
[512], *ptr
;
170 struct port_tcp
*ptcp
;
172 fp
= fopen("/etc/netsniff-ng/tcp.conf", "r");
174 panic("No /etc/netsniff-ng/tcp.conf found!\n");
175 memset(buff
, 0, sizeof(buff
));
176 while (fgets(buff
, sizeof(buff
), fp
) != NULL
) {
177 buff
[sizeof(buff
) - 1] = 0;
178 ptcp
= xmalloc(sizeof(*ptcp
));
181 ptr
= getuint(ptr
, &ptcp
->id
);
183 ptr
= skipchar(ptr
, ',');
185 ptr
= strtrim_right(ptr
, '\n');
186 ptr
= strtrim_right(ptr
, ' ');
187 ptcp
->port
= xstrdup(ptr
);
189 pos
= insert_hash(ptcp
->id
, ptcp
, ð_ports_tcp
);
194 memset(buff
, 0, sizeof(buff
));
199 static int dissector_cleanup_ports_tcp(void *ptr
)
201 struct port_tcp
*tmp
, *p
= ptr
;
204 while ((tmp
= p
->next
)) {
214 static void dissector_init_ether_types(void)
217 char buff
[512], *ptr
;
218 struct ether_type
*et
;
220 fp
= fopen("/etc/netsniff-ng/ether.conf", "r");
222 panic("No /etc/netsniff-ng/ether.conf found!\n");
223 memset(buff
, 0, sizeof(buff
));
224 while (fgets(buff
, sizeof(buff
), fp
) != NULL
) {
225 buff
[sizeof(buff
) - 1] = 0;
226 et
= xmalloc(sizeof(*et
));
229 ptr
= getuint(ptr
, &et
->id
);
231 ptr
= skipchar(ptr
, ',');
233 ptr
= strtrim_right(ptr
, '\n');
234 ptr
= strtrim_right(ptr
, ' ');
235 et
->type
= xstrdup(ptr
);
237 pos
= insert_hash(et
->id
, et
, ð_ether_types
);
242 memset(buff
, 0, sizeof(buff
));
247 static int dissector_cleanup_ether_types(void *ptr
)
249 struct ether_type
*tmp
, *p
= ptr
;
252 while ((tmp
= p
->next
)) {
262 void dissector_init_ethernet(int fnttype
)
264 dissector_init_entry(fnttype
);
265 dissector_init_layer_2(fnttype
);
266 dissector_init_layer_3(fnttype
);
267 dissector_init_exit(fnttype
);
269 dissector_init_oui();
271 dissector_init_ports_udp();
272 dissector_init_ports_tcp();
273 dissector_init_ether_types();
276 void dissector_cleanup_ethernet(void)
278 free_hash(ð_lay2
);
279 free_hash(ð_lay3
);
280 for_each_hash(ð_ether_types
, dissector_cleanup_ether_types
);
281 free_hash(ð_ether_types
);
282 for_each_hash(ð_ports_udp
, dissector_cleanup_ports_udp
);
283 free_hash(ð_ports_udp
);
284 for_each_hash(ð_ports_tcp
, dissector_cleanup_ports_tcp
);
285 free_hash(ð_ports_tcp
);
287 dissector_cleanup_oui();