Revert "TODO epan/dissectors/asn1/kerberos/packet-kerberos-template.c new GSS flags"
[wireshark-sm.git] / epan / dissectors / packet-bat.c
blob37f29cb8d085d0203f06a1b635f0d612dfc19307
1 /* packet-bat.c
2 * Routines for B.A.T.M.A.N. Layer 3 dissection
3 * Copyright 2008-2010 Sven Eckelmann <sven@narfation.org>
5 * Wireshark - Network traffic analyzer
6 * By Gerald Combs <gerald@wireshark.org>
7 * Copyright 1998 Gerald Combs
9 * SPDX-License-Identifier: GPL-2.0-or-later
12 #include "config.h"
14 #include <epan/packet.h>
15 #include <epan/tap.h>
16 #include <epan/addr_resolv.h>
17 #include <epan/tfs.h>
18 #include <epan/unit_strings.h>
20 void proto_register_bat(void);
21 void proto_reg_handoff_bat(void);
23 static dissector_handle_t batman_handle;
24 static dissector_handle_t gw_handle;
25 static dissector_handle_t vis_handle;
27 #define BAT_BATMAN_PORT 4305
28 #define BAT_GW_PORT 4306 /* Not IANA registered */
29 #define BAT_VIS_PORT 4307 /* Not IANA registered */
31 #define UNIDIRECTIONAL 0x80
32 #define DIRECTLINK 0x40
34 struct batman_packet_v5 {
35 uint8_t version; /* batman version field */
36 uint8_t flags; /* 0x80: UNIDIRECTIONAL link, 0x40: DIRECTLINK flag, ... */
37 uint8_t ttl;
38 uint8_t gwflags; /* flags related to gateway functions: gateway class */
39 uint16_t seqno;
40 uint16_t gwport;
41 address orig;
42 address old_orig;
43 uint8_t tq;
44 uint8_t hna_len;
46 #define BATMAN_PACKET_V5_SIZE 18
48 struct gw_packet {
49 uint8_t type;
51 #define GW_PACKET_SIZE 1
53 #define TUNNEL_DATA 0x01
54 #define TUNNEL_IP_REQUEST 0x02
55 #define TUNNEL_IP_INVALID 0x03
56 #define TUNNEL_KEEPALIVE_REQUEST 0x04
57 #define TUNNEL_KEEPALIVE_REPLY 0x05
59 #define DATA_TYPE_NEIGH 1
60 #define DATA_TYPE_SEC_IF 2
61 #define DATA_TYPE_HNA 3
63 struct vis_packet_v22 {
64 address sender_ip;
65 uint8_t version;
66 uint8_t gw_class;
67 uint16_t tq_max;
69 #define VIS_PACKET_V22_SIZE 8
71 struct vis_data_v22 {
72 uint8_t type;
73 uint16_t data;
74 address ip;
76 #define VIS_PACKET_V22_DATA_SIZE 7
78 struct vis_packet_v23 {
79 address sender_ip;
80 uint8_t version;
81 uint8_t gw_class;
82 uint8_t tq_max;
84 #define VIS_PACKET_V23_SIZE 7
86 struct vis_data_v23 {
87 uint8_t type;
88 uint8_t data;
89 address ip;
91 #define VIS_PACKET_V23_DATA_SIZE 6
92 /* End content from packet-bat.h */
94 /* trees */
95 static int ett_bat_batman;
96 static int ett_bat_batman_flags;
97 static int ett_bat_batman_gwflags;
98 static int ett_bat_batman_hna;
99 static int ett_bat_gw;
100 static int ett_bat_vis;
101 static int ett_bat_vis_entry;
103 /* hfs */
104 static int hf_bat_batman_version;
105 static int hf_bat_batman_flags;
106 static int hf_bat_batman_ttl;
107 static int hf_bat_batman_gwflags;
108 static int hf_bat_batman_gwflags_dl_speed;
109 static int hf_bat_batman_gwflags_ul_speed;
110 static int hf_bat_batman_seqno;
111 static int hf_bat_batman_gwport;
112 static int hf_bat_batman_orig;
113 static int hf_bat_batman_old_orig;
114 static int hf_bat_batman_tq;
115 static int hf_bat_batman_hna_len;
116 static int hf_bat_batman_hna_network;
117 static int hf_bat_batman_hna_netmask;
119 static int hf_bat_gw_type;
120 static int hf_bat_gw_ip;
122 static int hf_bat_vis_vis_orig;
123 static int hf_bat_vis_version;
124 static int hf_bat_vis_gwflags;
125 static int hf_bat_max_tq_v22;
126 static int hf_bat_max_tq_v23;
127 static int hf_bat_vis_data_type;
128 static int hf_bat_vis_netmask;
129 static int hf_bat_vis_tq_v22;
130 static int hf_bat_vis_tq_v23;
131 static int hf_bat_vis_data_ip;
133 /* flags */
134 static int hf_bat_batman_flags_unidirectional;
135 static int hf_bat_batman_flags_directlink;
137 static const value_string gw_packettypenames[] = {
138 { TUNNEL_DATA, "DATA" },
139 { TUNNEL_IP_REQUEST, "IP_REQUEST" },
140 { TUNNEL_IP_INVALID, "IP_INVALID" },
141 { TUNNEL_KEEPALIVE_REQUEST, "KEEPALIVE_REQUEST" },
142 { TUNNEL_KEEPALIVE_REPLY, "KEEPALIVE_REPLY" },
143 { 0, NULL }
146 static const value_string vis_packettypenames[] = {
147 { DATA_TYPE_NEIGH, "NEIGH" },
148 { DATA_TYPE_SEC_IF, "SEC_IF" },
149 { DATA_TYPE_HNA, "HNA" },
150 { 0, NULL }
153 /* supported packet dissectors */
154 static int dissect_bat_batman_v5(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree);
157 static void dissect_bat_vis_v22(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree);
158 static void dissect_vis_entry_v22(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree);
159 static void dissect_bat_vis_v23(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree);
160 static void dissect_vis_entry_v23(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree);
162 static void dissect_bat_hna(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree);
164 /* other dissectors */
165 static dissector_handle_t ip_handle;
167 static int proto_bat_plugin;
168 static int proto_bat_gw;
169 static int proto_bat_vis;
171 /* tap */
172 static int bat_tap;
173 static int bat_follow_tap;
175 static int dissect_bat_batman(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data _U_)
177 uint8_t version;
178 int offset = 0;
180 /* set protocol name */
181 col_set_str(pinfo->cinfo, COL_PROTOCOL, "BAT_BATMAN");
182 col_clear(pinfo->cinfo, COL_INFO);
184 version = tvb_get_uint8(tvb, 0);
185 switch (version) {
186 case 5:
187 while (tvb_reported_length_remaining(tvb, offset) > 0) {
188 offset = dissect_bat_batman_v5(tvb, offset, pinfo, tree);
190 break;
191 default:
192 col_add_fstr(pinfo->cinfo, COL_INFO, "Unsupported Version %d", version);
193 call_data_dissector(tvb, pinfo, tree);
194 break;
196 return tvb_captured_length(tvb);
199 static void dissect_bat_gwflags(tvbuff_t *tvb, uint8_t gwflags, int offset, proto_item *tgw)
201 proto_tree *gwflags_tree;
202 uint8_t s = (gwflags & 0x80) >> 7;
203 uint8_t downbits = (gwflags & 0x78) >> 3;
204 uint8_t upbits = (gwflags & 0x07);
205 unsigned down, up;
207 down = 32 * (s + 2) * (1 << downbits);
208 up = ((upbits + 1) * down) / 8;
210 gwflags_tree = proto_item_add_subtree(tgw, ett_bat_batman_gwflags);
211 proto_tree_add_uint(gwflags_tree, hf_bat_batman_gwflags_dl_speed, tvb, offset, 1, down);
212 proto_tree_add_uint(gwflags_tree, hf_bat_batman_gwflags_ul_speed, tvb, offset, 1, up);
216 static int dissect_bat_batman_v5(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree)
218 proto_item *tgw;
219 proto_tree *bat_batman_tree = NULL;
220 struct batman_packet_v5 *batman_packeth;
221 uint32_t old_orig, orig;
222 int i;
223 static int * const batman_flags[] = {
224 &hf_bat_batman_flags_unidirectional,
225 &hf_bat_batman_flags_directlink,
226 NULL
229 tvbuff_t *next_tvb;
231 batman_packeth = wmem_new(pinfo->pool, struct batman_packet_v5);
233 batman_packeth->version = tvb_get_uint8(tvb, offset+0);
234 batman_packeth->flags = tvb_get_uint8(tvb, offset+1);
235 batman_packeth->ttl = tvb_get_uint8(tvb, offset+2);
236 batman_packeth->gwflags = tvb_get_uint8(tvb, offset+3);
237 batman_packeth->seqno = tvb_get_ntohs(tvb, offset+4);
238 batman_packeth->gwport = tvb_get_ntohs(tvb, offset+6);
239 orig = tvb_get_ipv4(tvb, offset+8);
240 set_address_tvb(&batman_packeth->orig, AT_IPv4, 4, tvb, offset+8);
241 old_orig = tvb_get_ipv4(tvb, offset+12);
242 set_address_tvb(&batman_packeth->old_orig, AT_IPv4, 4, tvb, offset+12);
243 batman_packeth->tq = tvb_get_uint8(tvb, offset+16);
244 batman_packeth->hna_len = tvb_get_uint8(tvb, offset+17);
246 /* Set info column */
247 col_add_fstr(pinfo->cinfo, COL_INFO, "Seq=%u", batman_packeth->seqno);
249 /* Set tree info */
250 if (tree) {
251 proto_item *ti;
253 ti = proto_tree_add_protocol_format(tree, proto_bat_plugin, tvb, offset, BATMAN_PACKET_V5_SIZE,
254 "B.A.T.M.A.N., Orig: %s",
255 address_with_resolution_to_str(pinfo->pool, &batman_packeth->orig));
256 bat_batman_tree = proto_item_add_subtree(ti, ett_bat_batman);
259 /* items */
260 proto_tree_add_item(bat_batman_tree, hf_bat_batman_version, tvb, offset, 1, ENC_BIG_ENDIAN);
261 offset += 1;
263 proto_tree_add_bitmask(bat_batman_tree, tvb, offset, hf_bat_batman_flags,
264 ett_bat_batman_flags, batman_flags, ENC_NA);
265 offset += 1;
267 proto_tree_add_item(bat_batman_tree, hf_bat_batman_ttl, tvb, offset, 1, ENC_BIG_ENDIAN);
268 offset += 1;
270 tgw = proto_tree_add_item(bat_batman_tree, hf_bat_batman_gwflags, tvb, offset, 1, ENC_BIG_ENDIAN);
271 dissect_bat_gwflags(tvb, batman_packeth->gwflags, offset, tgw);
272 offset += 1;
274 proto_tree_add_item(bat_batman_tree, hf_bat_batman_seqno, tvb, offset, 2, ENC_BIG_ENDIAN);
275 offset += 2;
277 proto_tree_add_item(bat_batman_tree, hf_bat_batman_gwport, tvb, offset, 2, ENC_BIG_ENDIAN);
278 offset += 2;
280 proto_tree_add_ipv4(bat_batman_tree, hf_bat_batman_orig, tvb, offset, 4, orig);
281 offset += 4;
283 proto_tree_add_ipv4(bat_batman_tree, hf_bat_batman_old_orig, tvb, offset, 4, old_orig);
284 offset += 4;
286 proto_tree_add_item(bat_batman_tree, hf_bat_batman_tq, tvb, offset, 1, ENC_BIG_ENDIAN);
287 offset += 1;
289 proto_tree_add_item(bat_batman_tree, hf_bat_batman_hna_len, tvb, offset, 1, ENC_BIG_ENDIAN);
290 offset += 1;
292 tap_queue_packet(bat_tap, pinfo, batman_packeth);
294 for (i = 0; i < batman_packeth->hna_len; i++) {
295 next_tvb = tvb_new_subset_length(tvb, offset, 5);
297 if (have_tap_listener(bat_follow_tap)) {
298 tap_queue_packet(bat_follow_tap, pinfo, next_tvb);
301 dissect_bat_hna(next_tvb, pinfo, bat_batman_tree);
302 offset += 5;
305 return offset;
308 static void dissect_bat_hna(tvbuff_t *tvb, packet_info *pinfo _U_, proto_tree *tree)
310 uint32_t hna;
311 uint8_t hna_netmask;
313 hna = tvb_get_ipv4(tvb, 0);
314 hna_netmask = tvb_get_uint8(tvb, 4);
317 /* Set tree info */
318 if (tree) {
319 proto_item *ti;
320 proto_tree *bat_batman_hna_tree;
322 if (PTREE_DATA(tree)->visible) {
323 ti = proto_tree_add_protocol_format(tree, proto_bat_plugin, tvb, 0, 5,
324 "B.A.T.M.A.N. HNA: %s/%d",
325 tvb_ip_to_str(pinfo->pool, tvb, 0), hna_netmask);
326 } else {
327 ti = proto_tree_add_item(tree, proto_bat_plugin, tvb, 0, 5, ENC_NA);
329 bat_batman_hna_tree = proto_item_add_subtree(ti, ett_bat_batman_hna);
331 proto_tree_add_ipv4(bat_batman_hna_tree, hf_bat_batman_hna_network, tvb, 0, 4, hna);
332 proto_tree_add_item(bat_batman_hna_tree, hf_bat_batman_hna_netmask, tvb, 4, 1, ENC_BIG_ENDIAN);
337 static int dissect_bat_gw(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data _U_)
339 struct gw_packet *gw_packeth;
340 uint32_t ip;
341 int ip_pos;
343 tvbuff_t *next_tvb;
344 int length_remaining;
345 int offset = 0;
347 gw_packeth = wmem_new(pinfo->pool, struct gw_packet);
348 gw_packeth->type = tvb_get_uint8(tvb, 0);
350 switch (gw_packeth->type) {
351 case TUNNEL_IP_INVALID:
352 ip_pos = 13;
353 break;
354 default:
355 ip_pos = 1;
357 ip = tvb_get_ipv4(tvb, ip_pos);
359 /* set protocol name */
360 col_set_str(pinfo->cinfo, COL_PROTOCOL, "BAT_GW");
362 /* Set info column */
363 col_add_fstr(pinfo->cinfo, COL_INFO, "Type=%s",
364 val_to_str(gw_packeth->type, gw_packettypenames, "Unknown (0x%02x)"));
365 if (ip != 0) {
366 col_append_fstr(pinfo->cinfo, COL_INFO, " IP: %s",
367 tvb_address_with_resolution_to_str(pinfo->pool, tvb, AT_IPv4, ip_pos));
371 /* Set tree info */
372 if (tree) {
373 proto_item *ti;
374 proto_tree *bat_gw_entry_tree;
376 ti = proto_tree_add_protocol_format(tree, proto_bat_gw, tvb, 0, 1,
377 "B.A.T.M.A.N. GW [%s]",
378 val_to_str(gw_packeth->type, gw_packettypenames, "Unknown (0x%02x)"));
379 bat_gw_entry_tree = proto_item_add_subtree(ti, ett_bat_gw);
381 proto_tree_add_item(bat_gw_entry_tree, hf_bat_gw_type, tvb, offset, 1, ENC_BIG_ENDIAN);
382 /*offset += 1;*/
384 if (gw_packeth->type != TUNNEL_DATA && ip != 0) {
385 proto_tree_add_ipv4(bat_gw_entry_tree, hf_bat_gw_ip, tvb, ip_pos, 4, ip);
386 /*offset = ip_pos + 4;*/
390 /* Calculate offset even when we got no tree */
391 offset = 1;
392 if (gw_packeth->type != TUNNEL_DATA && ip != 0)
393 offset = ip_pos + 4;
395 length_remaining = tvb_reported_length_remaining(tvb, offset);
396 if (length_remaining > 0) {
397 next_tvb = tvb_new_subset_remaining(tvb, offset);
399 if (have_tap_listener(bat_follow_tap)) {
400 tap_queue_packet(bat_follow_tap, pinfo, next_tvb);
403 if (gw_packeth->type == TUNNEL_DATA) {
404 call_dissector(ip_handle, next_tvb, pinfo, tree);
405 } else {
406 call_data_dissector(next_tvb, pinfo, tree);
409 return tvb_captured_length(tvb);
412 static int dissect_bat_vis(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data _U_)
414 uint8_t version;
416 /* set protocol name */
417 col_set_str(pinfo->cinfo, COL_PROTOCOL, "BAT_VIS");
419 version = tvb_get_uint8(tvb, 4);
420 switch (version) {
421 case 22:
422 dissect_bat_vis_v22(tvb, pinfo, tree);
423 break;
424 case 23:
425 dissect_bat_vis_v23(tvb, pinfo, tree);
426 break;
427 default:
428 col_add_fstr(pinfo->cinfo, COL_INFO, "Unsupported Version %d", version);
429 call_data_dissector(tvb, pinfo, tree);
430 break;
432 return tvb_captured_length(tvb);
435 static void dissect_bat_vis_v22(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
437 struct vis_packet_v22 *vis_packeth;
438 uint32_t sender_ip;
439 proto_tree *bat_vis_tree = NULL;
441 tvbuff_t *next_tvb;
442 int length_remaining, i;
443 int offset = 0;
445 vis_packeth = wmem_new(pinfo->pool, struct vis_packet_v22);
447 sender_ip = tvb_get_ipv4(tvb, 0);
448 set_address_tvb(&vis_packeth->sender_ip, AT_IPv4, 4, tvb, 0);
449 vis_packeth->version = tvb_get_uint8(tvb, 4);
450 vis_packeth->gw_class = tvb_get_uint8(tvb, 5);
451 vis_packeth->tq_max = tvb_get_ntohs(tvb, 6);
453 /* set protocol name */
454 col_set_str(pinfo->cinfo, COL_PROTOCOL, "BAT_VIS");
456 /* Set info column */
457 col_add_fstr(pinfo->cinfo, COL_INFO, "Src: %s",
458 address_with_resolution_to_str(pinfo->pool, &vis_packeth->sender_ip));
460 /* Set tree info */
461 if (tree) {
462 proto_item *ti;
464 ti = proto_tree_add_protocol_format(tree, proto_bat_vis, tvb, 0, VIS_PACKET_V22_SIZE,
465 "B.A.T.M.A.N. Vis, Src: %s",
466 address_with_resolution_to_str(pinfo->pool, &vis_packeth->sender_ip));
467 bat_vis_tree = proto_item_add_subtree(ti, ett_bat_vis);
469 /* items */
470 proto_tree_add_ipv4(bat_vis_tree, hf_bat_vis_vis_orig, tvb, offset, 4, sender_ip);
471 offset += 4;
473 proto_tree_add_item(bat_vis_tree, hf_bat_vis_version, tvb, offset, 1, ENC_BIG_ENDIAN);
474 offset += 1;
476 proto_tree_add_item(bat_vis_tree, hf_bat_vis_gwflags, tvb, offset, 1, ENC_BIG_ENDIAN);
477 offset += 1;
479 proto_tree_add_item(bat_vis_tree, hf_bat_max_tq_v22, tvb, offset, 2, ENC_BIG_ENDIAN);
480 /*offset += 2;*/
483 /* Calculate offset even when we got no tree */
484 offset = VIS_PACKET_V22_SIZE;
486 tap_queue_packet(bat_tap, pinfo, vis_packeth);
488 length_remaining = tvb_reported_length_remaining(tvb, offset);
490 for (i = 0; i < length_remaining; i += VIS_PACKET_V22_DATA_SIZE) {
491 next_tvb = tvb_new_subset_length(tvb, offset, VIS_PACKET_V22_DATA_SIZE);
493 if (have_tap_listener(bat_follow_tap)) {
494 tap_queue_packet(bat_follow_tap, pinfo, next_tvb);
497 if (bat_vis_tree != NULL) {
498 dissect_vis_entry_v22(next_tvb, pinfo, tree);
501 offset += VIS_PACKET_V22_DATA_SIZE;
504 length_remaining = tvb_reported_length_remaining(tvb, offset);
505 if (length_remaining > 0) {
506 next_tvb = tvb_new_subset_remaining(tvb, offset);
508 if (have_tap_listener(bat_follow_tap)) {
509 tap_queue_packet(bat_follow_tap, pinfo, next_tvb);
512 call_data_dissector(next_tvb, pinfo, tree);
516 static void dissect_vis_entry_v22(tvbuff_t *tvb, packet_info *pinfo _U_, proto_tree *tree)
518 struct vis_data_v22 *vis_datah;
519 uint32_t ip;
521 vis_datah = wmem_new(pinfo->pool, struct vis_data_v22);
522 vis_datah->type = tvb_get_uint8(tvb, 0);
523 vis_datah->data = tvb_get_ntohs(tvb, 1);
524 ip = tvb_get_ipv4(tvb, 3);
525 set_address_tvb(&vis_datah->ip, AT_IPv4, 4, tvb, 3);
528 /* Set tree info */
529 if (tree) {
530 proto_item *ti;
531 proto_tree *bat_vis_entry_tree;
533 ti = proto_tree_add_protocol_format(tree, proto_bat_plugin, tvb, 0, 7,
534 "VIS Entry: [%s] %s",
535 val_to_str(vis_datah->type, vis_packettypenames, "Unknown (0x%02x)"),
536 address_with_resolution_to_str(pinfo->pool, &vis_datah->ip));
537 bat_vis_entry_tree = proto_item_add_subtree(ti, ett_bat_vis_entry);
539 proto_tree_add_item(bat_vis_entry_tree, hf_bat_vis_data_type, tvb, 0, 1, ENC_BIG_ENDIAN);
541 switch (vis_datah->type) {
542 case DATA_TYPE_NEIGH:
543 proto_tree_add_item(bat_vis_entry_tree, hf_bat_vis_tq_v22, tvb, 1, 2, ENC_BIG_ENDIAN);
544 break;
545 case DATA_TYPE_HNA:
546 proto_tree_add_item(bat_vis_entry_tree, hf_bat_vis_netmask, tvb, 1, 1, ENC_BIG_ENDIAN);
547 break;
548 case DATA_TYPE_SEC_IF:
549 default:
550 break;
552 proto_tree_add_ipv4(bat_vis_entry_tree, hf_bat_vis_data_ip, tvb, 3, 4, ip);
556 static void dissect_bat_vis_v23(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
558 struct vis_packet_v23 *vis_packeth;
559 uint32_t sender_ip;
560 proto_tree *bat_vis_tree = NULL;
562 tvbuff_t *next_tvb;
563 int length_remaining, i;
564 int offset = 0;
566 vis_packeth = wmem_new(pinfo->pool, struct vis_packet_v23);
568 sender_ip = tvb_get_ipv4(tvb, 0);
569 set_address_tvb(&vis_packeth->sender_ip, AT_IPv4, 4, tvb, 0);
570 vis_packeth->version = tvb_get_uint8(tvb, 4);
571 vis_packeth->gw_class = tvb_get_uint8(tvb, 5);
572 vis_packeth->tq_max = tvb_get_uint8(tvb, 6);
574 /* set protocol name */
575 col_set_str(pinfo->cinfo, COL_PROTOCOL, "BAT_VIS");
577 /* Set info column */
578 col_add_fstr(pinfo->cinfo, COL_INFO, "Src: %s",
579 address_with_resolution_to_str(pinfo->pool, &vis_packeth->sender_ip));
581 /* Set tree info */
582 if (tree) {
583 proto_item *ti;
585 ti = proto_tree_add_protocol_format(tree, proto_bat_vis, tvb, 0, VIS_PACKET_V23_SIZE,
586 "B.A.T.M.A.N. Vis, Src: %s",
587 address_with_resolution_to_str(pinfo->pool, &vis_packeth->sender_ip));
588 bat_vis_tree = proto_item_add_subtree(ti, ett_bat_vis);
590 /* items */
591 proto_tree_add_ipv4(bat_vis_tree, hf_bat_vis_vis_orig, tvb, offset, 4, sender_ip);
592 offset += 4;
594 proto_tree_add_item(bat_vis_tree, hf_bat_vis_version, tvb, offset, 1, ENC_BIG_ENDIAN);
595 offset += 1;
597 proto_tree_add_item(bat_vis_tree, hf_bat_vis_gwflags, tvb, offset, 1, ENC_BIG_ENDIAN);
598 offset += 1;
600 proto_tree_add_item(bat_vis_tree, hf_bat_max_tq_v23, tvb, offset, 1, ENC_BIG_ENDIAN);
601 /*offset += 1;*/
604 /* Calculate offset even when we got no tree */
605 offset = VIS_PACKET_V23_SIZE;
607 tap_queue_packet(bat_tap, pinfo, vis_packeth);
609 length_remaining = tvb_reported_length_remaining(tvb, offset);
611 for (i = 0; i < length_remaining; i += VIS_PACKET_V23_DATA_SIZE) {
612 next_tvb = tvb_new_subset_length(tvb, offset, VIS_PACKET_V23_DATA_SIZE);
614 if (have_tap_listener(bat_follow_tap)) {
615 tap_queue_packet(bat_follow_tap, pinfo, next_tvb);
618 if (bat_vis_tree != NULL) {
619 dissect_vis_entry_v23(next_tvb, pinfo, tree);
622 offset += VIS_PACKET_V23_DATA_SIZE;
625 length_remaining = tvb_reported_length_remaining(tvb, offset);
626 if (length_remaining > 0) {
627 next_tvb = tvb_new_subset_remaining(tvb, offset);
629 if (have_tap_listener(bat_follow_tap)) {
630 tap_queue_packet(bat_follow_tap, pinfo, next_tvb);
633 call_data_dissector(next_tvb, pinfo, tree);
637 static void dissect_vis_entry_v23(tvbuff_t *tvb, packet_info *pinfo _U_, proto_tree *tree)
639 struct vis_data_v23 *vis_datah;
640 uint32_t ip;
642 vis_datah = wmem_new(pinfo->pool, struct vis_data_v23);
643 vis_datah->type = tvb_get_uint8(tvb, 0);
644 vis_datah->data = tvb_get_uint8(tvb, 1);
645 ip = tvb_get_ipv4(tvb, 2);
646 set_address_tvb(&vis_datah->ip, AT_IPv4, 4, tvb, 2);
649 /* Set tree info */
650 if (tree) {
651 proto_item *ti;
652 proto_tree *bat_vis_entry_tree;
654 ti = proto_tree_add_protocol_format(tree, proto_bat_plugin, tvb, 0, 7,
655 "VIS Entry: [%s] %s",
656 val_to_str(vis_datah->type, vis_packettypenames, "Unknown (0x%02x)"),
657 address_with_resolution_to_str(pinfo->pool, &vis_datah->ip));
658 bat_vis_entry_tree = proto_item_add_subtree(ti, ett_bat_vis_entry);
660 proto_tree_add_item(bat_vis_entry_tree, hf_bat_vis_data_type, tvb, 0, 1, ENC_BIG_ENDIAN);
662 switch (vis_datah->type) {
663 case DATA_TYPE_NEIGH:
664 proto_tree_add_item(bat_vis_entry_tree, hf_bat_vis_tq_v23, tvb, 1, 1, ENC_BIG_ENDIAN);
665 break;
666 case DATA_TYPE_HNA:
667 proto_tree_add_item(bat_vis_entry_tree, hf_bat_vis_netmask, tvb, 1, 1, ENC_BIG_ENDIAN);
668 break;
669 case DATA_TYPE_SEC_IF:
670 default:
671 break;
673 proto_tree_add_ipv4(bat_vis_entry_tree, hf_bat_vis_data_ip, tvb, 2, 4, ip);
677 void proto_register_bat(void)
679 static hf_register_info hf[] = {
680 { &hf_bat_batman_version,
681 { "Version", "bat.batman.version",
682 FT_UINT8, BASE_DEC, NULL, 0x0,
683 NULL, HFILL }
685 { &hf_bat_batman_flags,
686 { "Flags", "bat.batman.flags",
687 FT_UINT8, BASE_HEX, NULL, 0x0,
688 NULL, HFILL }
690 { &hf_bat_batman_ttl,
691 { "Time to Live", "bat.batman.ttl",
692 FT_UINT8, BASE_DEC, NULL, 0x0,
693 NULL, HFILL }
695 { &hf_bat_batman_gwflags,
696 { "Gateway Flags", "bat.batman.gwflags",
697 FT_UINT8, BASE_HEX, NULL, 0x0,
698 NULL, HFILL }
700 { &hf_bat_batman_gwflags_dl_speed,
701 { "Download Speed", "bat.batman.gwflags.dl_speed",
702 FT_UINT32, BASE_DEC|BASE_UNIT_STRING, UNS(&units_kbit), 0x0,
703 NULL, HFILL }
705 { &hf_bat_batman_gwflags_ul_speed,
706 { "Upload Speed", "bat.batman.gwflags.ul_speed",
707 FT_UINT32, BASE_DEC|BASE_UNIT_STRING, UNS(&units_kbit), 0x0,
708 NULL, HFILL }
710 { &hf_bat_batman_seqno,
711 { "Sequence number", "bat.batman.seq",
712 FT_UINT16, BASE_DEC, NULL, 0x0,
713 NULL, HFILL }
715 { &hf_bat_batman_gwport,
716 { "Gateway Port", "bat.batman.gwport",
717 FT_UINT16, BASE_DEC, NULL, 0x0,
718 NULL, HFILL }
720 { &hf_bat_batman_orig,
721 { "Originator", "bat.batman.orig",
722 FT_IPv4, BASE_NONE, NULL, 0x0,
723 NULL, HFILL }
725 { &hf_bat_batman_old_orig,
726 { "Received from", "bat.batman.old_orig",
727 FT_IPv4, BASE_NONE, NULL, 0x0,
728 NULL, HFILL }
730 { &hf_bat_batman_tq,
731 { "Transmission Quality", "bat.batman.tq",
732 FT_UINT8, BASE_DEC, NULL, 0x0,
733 NULL, HFILL }
735 { &hf_bat_batman_hna_len,
736 { "Number of HNAs", "bat.batman.hna_len",
737 FT_UINT8, BASE_DEC, NULL, 0x0,
738 NULL, HFILL }
740 { &hf_bat_batman_flags_unidirectional,
741 { "Unidirectional", "bat.batman.flags.unidirectional",
742 FT_BOOLEAN, 8, TFS(&tfs_set_notset), 0x80,
743 NULL, HFILL }
745 { &hf_bat_batman_flags_directlink,
746 { "DirectLink", "bat.batman.flags.directlink",
747 FT_BOOLEAN, 8, TFS(&tfs_set_notset), 0x40,
748 NULL, HFILL }
750 { &hf_bat_batman_hna_network,
751 { "HNA Network", "bat.batman.hna_network",
752 FT_IPv4, BASE_NONE, NULL, 0x0,
753 NULL, HFILL }
755 { &hf_bat_batman_hna_netmask,
756 { "HNA Netmask", "bat.batman.hna_netmask",
757 FT_UINT8, BASE_DEC, NULL, 0x0,
758 NULL, HFILL }
760 { &hf_bat_gw_type,
761 { "Type", "bat.gw.type",
762 FT_UINT8, BASE_DEC, VALS(gw_packettypenames), 0x0,
763 NULL, HFILL }
765 { &hf_bat_gw_ip,
766 { "IP", "bat.gw.ip",
767 FT_IPv4, BASE_NONE, NULL, 0x0,
768 NULL, HFILL }
770 { &hf_bat_vis_vis_orig,
771 { "Originator", "bat.vis.sender_ip",
772 FT_IPv4, BASE_NONE, NULL, 0x0,
773 NULL, HFILL }
775 { &hf_bat_vis_version,
776 { "Version", "bat.vis.version",
777 FT_UINT8, BASE_DEC, NULL, 0x0,
778 NULL, HFILL }
780 { &hf_bat_vis_gwflags,
781 { "Gateway Flags", "bat.vis.gwflags",
782 FT_UINT8, BASE_HEX, NULL, 0x0,
783 NULL, HFILL }
785 { &hf_bat_max_tq_v22,
786 { "Maximum Transmission Quality", "bat.vis.tq_max",
787 FT_UINT16, BASE_DEC, NULL, 0x0,
788 NULL, HFILL }
790 { &hf_bat_max_tq_v23,
791 { "Maximum Transmission Quality", "bat.vis.tq_max",
792 FT_UINT8, BASE_DEC, NULL, 0x0,
793 NULL, HFILL }
795 { &hf_bat_vis_data_type,
796 { "Type", "bat.vis.data_type",
797 FT_UINT8, BASE_DEC, VALS(vis_packettypenames), 0x0,
798 NULL, HFILL }
800 { &hf_bat_vis_tq_v22,
801 { "Transmission Quality", "bat.vis.tq",
802 FT_UINT16, BASE_DEC, NULL, 0x0,
803 NULL, HFILL}
805 { &hf_bat_vis_tq_v23,
806 { "Transmission Quality", "bat.vis.tq",
807 FT_UINT8, BASE_DEC, NULL, 0x0,
808 NULL, HFILL}
810 { &hf_bat_vis_netmask,
811 { "Netmask", "bat.vis.netmask",
812 FT_UINT8, BASE_DEC, NULL, 0x0,
813 NULL, HFILL}
815 { &hf_bat_vis_data_ip,
816 { "IP", "bat.vis.data_ip",
817 FT_IPv4, BASE_NONE, NULL, 0x0,
818 NULL, HFILL }
822 /* Setup protocol subtree array */
823 static int *ett[] = {
824 &ett_bat_batman,
825 &ett_bat_batman_flags,
826 &ett_bat_batman_gwflags,
827 &ett_bat_batman_hna,
828 &ett_bat_gw,
829 &ett_bat_vis,
830 &ett_bat_vis_entry
833 proto_bat_plugin = proto_register_protocol("B.A.T.M.A.N. Layer 3 Protocol", "BAT", "bat");
834 proto_bat_gw = proto_register_protocol("B.A.T.M.A.N. GW", "BAT GW", "bat.gw");
835 proto_bat_vis = proto_register_protocol("B.A.T.M.A.N. Vis", "BAT VIS", "bat.vis");
837 batman_handle = register_dissector("bat", dissect_bat_batman, proto_bat_plugin);
838 gw_handle = register_dissector("bat.gw", dissect_bat_gw, proto_bat_gw);
839 vis_handle = register_dissector("bat.vis", dissect_bat_vis, proto_bat_vis);
841 proto_register_field_array(proto_bat_plugin, hf, array_length(hf));
842 proto_register_subtree_array(ett, array_length(ett));
844 bat_tap = register_tap("batman");
845 bat_follow_tap = register_tap("batman_follow");
848 void proto_reg_handoff_bat(void)
850 ip_handle = find_dissector_add_dependency("ip", proto_bat_gw);
852 dissector_add_uint_with_preference("udp.port", BAT_BATMAN_PORT, batman_handle);
853 dissector_add_uint_with_preference("udp.port", BAT_GW_PORT, gw_handle);
854 dissector_add_uint_with_preference("udp.port", BAT_VIS_PORT, vis_handle);
858 * Editor modelines - https://www.wireshark.org/tools/modelines.html
860 * Local variables:
861 * c-basic-offset: 8
862 * tab-width: 8
863 * indent-tabs-mode: t
864 * End:
866 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
867 * :indentSize=8:tabSize=8:noTabs=false: