2 * Routines for roofnet dissection
3 * Copyright 2006, Sebastien Tandel (sebastien@tandel.be)
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
14 #include <epan/packet.h>
15 #include <epan/addr_resolv.h>
16 #include <epan/expert.h>
17 #include <epan/ptvcursor.h>
20 /* roofnet packet type constants */
21 #define ROOFNET_PT_QUERY 0x01
22 #define ROOFNET_PT_REPLY 0x02
23 #define ROOFNET_PT_DATA 0x04
24 #define ROOFNET_PT_GATEWAY 0x08
25 static const value_string roofnet_pt_vals
[] = {
26 { ROOFNET_PT_QUERY
, "Query" },
27 { ROOFNET_PT_REPLY
, "Reply" },
28 { ROOFNET_PT_DATA
, "Data" },
29 { ROOFNET_PT_GATEWAY
, "Gateway" },
33 /* roofnet flag bit masks */
34 #define ROOFNET_FLAG_ERROR (1<<0)
35 #define ROOFNET_FLAG_UPDATE (1<<1)
36 #define ROOFNET_FLAG_LAYER2 (1<<9)
37 #define ROOFNET_FLAG_RESERVED 0xFDFC
38 #define ROOFNET_FLAG_MASK (ROOFNET_FLAG_ERROR | ROOFNET_FLAG_UPDATE | ROOFNET_FLAG_LAYER2)
41 #define ROOFNET_HEADER_LENGTH 160
42 /* roofnet max length */
43 /* may change with time */
44 #define ROOFNET_MAX_LENGTH 400
45 /* Roofnet Link Description Length
46 * which is 6 fields of 4 bytes */
47 #define ROOFNET_LINK_DESCRIPTION_LENGTH 6*4
49 /* offset constants */
50 #define ROOFNET_OFFSET_TYPE 1
51 #define ROOFNET_OFFSET_NLINKS 2
52 #define ROOFNET_OFFSET_DATA_LENGTH 10
54 /* offset relative to a link section of roofnet */
55 #define ROOFNET_LINK_OFFSET_SRC 0
56 #define ROOFNET_LINK_OFFSET_DST 20
57 /* roofnet link fields length */
58 #define ROOFNET_LINK_LEN 24
60 /* forward reference */
61 void proto_register_roofnet(void);
62 void proto_reg_handoff_roofnet(void);
64 static dissector_handle_t roofnet_handle
;
65 static dissector_handle_t ip_handle
;
66 static dissector_handle_t eth_withoutfcs_handle
;
67 static int proto_roofnet
;
69 /* hf fields for the header of roofnet */
70 static int hf_roofnet_version
;
71 static int hf_roofnet_type
;
72 static int hf_roofnet_nlinks
;
73 static int hf_roofnet_next
;
74 static int hf_roofnet_ttl
;
75 static int hf_roofnet_cksum
;
76 static int hf_roofnet_flags
;
77 static int hf_roofnet_flags_error
;
78 static int hf_roofnet_flags_update
;
79 static int hf_roofnet_flags_layer2
;
80 static int hf_roofnet_flags_reserved
;
81 static int hf_roofnet_data_length
;
82 static int hf_roofnet_query_dst
;
83 static int hf_roofnet_seq
;
84 /* static int hf_roofnet_links; */
85 static int hf_roofnet_link_src
;
86 static int hf_roofnet_link_forward
;
87 static int hf_roofnet_link_rev
;
88 static int hf_roofnet_link_seq
;
89 static int hf_roofnet_link_age
;
90 static int hf_roofnet_link_dst
;
92 static int * const flag_list
[] = {
93 &hf_roofnet_flags_error
,
94 &hf_roofnet_flags_update
,
95 &hf_roofnet_flags_layer2
,
96 &hf_roofnet_flags_reserved
,
101 static int ett_roofnet
;
102 static int ett_roofnet_flags
;
103 static int ett_roofnet_link
;
105 static expert_field ei_roofnet_too_many_links
;
106 static expert_field ei_roofnet_too_much_data
;
109 * dissect the header of roofnet
111 static uint16_t dissect_roofnet_header(proto_tree
*tree
, packet_info
*pinfo
, tvbuff_t
*tvb
, unsigned *offset
)
114 ptvcursor_t
*cursor
= ptvcursor_new(pinfo
->pool
, tree
, tvb
, *offset
);
116 ptvcursor_add(cursor
, hf_roofnet_version
, 1, ENC_BIG_ENDIAN
);
117 ptvcursor_add(cursor
, hf_roofnet_type
, 1, ENC_BIG_ENDIAN
);
118 ptvcursor_add(cursor
, hf_roofnet_nlinks
, 1, ENC_BIG_ENDIAN
);
119 ptvcursor_add(cursor
, hf_roofnet_next
, 1, ENC_BIG_ENDIAN
);
120 ptvcursor_add(cursor
, hf_roofnet_ttl
, 2, ENC_BIG_ENDIAN
);
121 proto_tree_add_checksum(ptvcursor_tree(cursor
), ptvcursor_tvbuff(cursor
), ptvcursor_current_offset(cursor
),
122 hf_roofnet_cksum
, -1, NULL
, NULL
, 0, ENC_BIG_ENDIAN
, PROTO_CHECKSUM_NO_FLAGS
);
123 ptvcursor_advance(cursor
, 2);
124 flags
= tvb_get_ntohs(ptvcursor_tvbuff(cursor
), ptvcursor_current_offset(cursor
));
125 proto_tree_add_bitmask(ptvcursor_tree(cursor
), ptvcursor_tvbuff(cursor
), ptvcursor_current_offset(cursor
),
126 hf_roofnet_flags
, ett_roofnet_flags
, flag_list
, ENC_BIG_ENDIAN
);
127 ptvcursor_advance(cursor
, 2);
128 ptvcursor_add(cursor
, hf_roofnet_data_length
, 2, ENC_BIG_ENDIAN
);
129 ptvcursor_add(cursor
, hf_roofnet_query_dst
, 4, ENC_BIG_ENDIAN
);
130 ptvcursor_add(cursor
, hf_roofnet_seq
, 4, ENC_BIG_ENDIAN
);
132 *offset
= ptvcursor_current_offset(cursor
);
133 ptvcursor_free(cursor
);
139 * dissect the description of link in roofnet
141 static void dissect_roofnet_link(proto_tree
*tree
, packet_info
*pinfo
, tvbuff_t
*tvb
, unsigned *offset
, unsigned link
)
143 proto_tree
*subtree
= NULL
;
145 ptvcursor_t
*cursor
= NULL
;
147 uint32_t addr_src
= 0;
148 uint32_t addr_dst
= 0;
150 addr_src
= tvb_get_ipv4(tvb
, *offset
+ ROOFNET_LINK_OFFSET_SRC
);
151 addr_dst
= tvb_get_ipv4(tvb
, *offset
+ ROOFNET_LINK_OFFSET_DST
);
153 subtree
= proto_tree_add_subtree_format(tree
, tvb
, *offset
, ROOFNET_LINK_LEN
,
154 ett_roofnet_link
, NULL
, "link: %u, src: %s, dst: %s",
156 get_hostname(addr_src
),
157 get_hostname(addr_dst
));
159 proto_tree_add_ipv4(subtree
, hf_roofnet_link_src
, tvb
, *offset
, 4, addr_src
);
162 cursor
= ptvcursor_new(pinfo
->pool
, subtree
, tvb
, *offset
);
164 ptvcursor_add(cursor
, hf_roofnet_link_forward
, 4, ENC_BIG_ENDIAN
);
165 ptvcursor_add(cursor
, hf_roofnet_link_rev
, 4, ENC_BIG_ENDIAN
);
166 ptvcursor_add(cursor
, hf_roofnet_link_seq
, 4, ENC_BIG_ENDIAN
);
167 ptvcursor_add(cursor
, hf_roofnet_link_age
, 4, ENC_BIG_ENDIAN
);
169 *offset
= ptvcursor_current_offset(cursor
);
170 ptvcursor_free(cursor
);
172 proto_tree_add_ipv4(subtree
, hf_roofnet_link_dst
, tvb
, *offset
, 4, addr_dst
);
173 /* don't increment offset here because the dst of this link is the src of the next one */
177 * dissect the data in roofnet
179 static void dissect_roofnet_data(proto_tree
*tree
, tvbuff_t
*tvb
, packet_info
* pinfo
, int offset
, uint16_t flags
)
181 uint16_t roofnet_datalen
= 0;
182 uint16_t remaining_datalen
= 0;
184 roofnet_datalen
= tvb_get_ntohs(tvb
, ROOFNET_OFFSET_DATA_LENGTH
);
185 remaining_datalen
= tvb_reported_length_remaining(tvb
, offset
);
188 /* dissect on remaining_datalen */
189 if (roofnet_datalen
< remaining_datalen
)
190 proto_tree_add_expert_format(tree
, pinfo
, &ei_roofnet_too_much_data
, tvb
, offset
, roofnet_datalen
,
191 "[More payload data (%u) than told by Roofnet (%u)]",
192 remaining_datalen
, roofnet_datalen
);
194 if (roofnet_datalen
== 0)
197 /* dissect payload */
198 if (flags
& ROOFNET_FLAG_LAYER2
) {
199 /* ethernet frame is padded with 2 bytes at the start */
200 call_dissector(eth_withoutfcs_handle
, tvb_new_subset_remaining(tvb
, offset
+2), pinfo
, tree
);
202 call_dissector(ip_handle
, tvb_new_subset_remaining(tvb
, offset
), pinfo
, tree
);
208 * entry point of the roofnet dissector
210 static int dissect_roofnet(tvbuff_t
*tvb
, packet_info
*pinfo
, proto_tree
*tree
, void* data _U_
)
213 proto_tree
* roofnet_tree
;
216 uint8_t roofnet_msg_type
= 0;
217 uint8_t roofnet_nlinks
= 0;
221 col_set_str(pinfo
->cinfo
, COL_PROTOCOL
, "Roofnet");
223 roofnet_msg_type
= tvb_get_uint8(tvb
, ROOFNET_OFFSET_TYPE
);
224 /* Clear out stuff in the info column */
225 col_add_fstr(pinfo
->cinfo
, COL_INFO
, "Message Type: %s",
226 val_to_str(roofnet_msg_type
, roofnet_pt_vals
, "Unknown (%d)"));
228 it
= proto_tree_add_item(tree
, proto_roofnet
, tvb
, offset
, -1, ENC_NA
);
229 roofnet_tree
= proto_item_add_subtree(it
, ett_roofnet
);
231 flags
= dissect_roofnet_header(roofnet_tree
, pinfo
, tvb
, &offset
);
233 roofnet_nlinks
= tvb_get_uint8(tvb
, ROOFNET_OFFSET_NLINKS
);
234 /* Check that we do not have a malformed roofnet packet */
235 if ((roofnet_nlinks
*6*4)+ROOFNET_HEADER_LENGTH
> ROOFNET_MAX_LENGTH
) {
236 expert_add_info_format(pinfo
, it
, &ei_roofnet_too_many_links
, "Too many links (%u)", roofnet_nlinks
);
237 return tvb_captured_length(tvb
);
240 for (; roofnet_nlinks
> 0; roofnet_nlinks
--) {
241 /* Do we have enough buffer to decode the next link ? */
242 if (tvb_reported_length_remaining(tvb
, offset
) < ROOFNET_LINK_DESCRIPTION_LENGTH
)
244 dissect_roofnet_link(roofnet_tree
, pinfo
, tvb
, &offset
, nlink
++);
247 dissect_roofnet_data(tree
, tvb
, pinfo
, offset
+4, flags
);
248 return tvb_captured_length(tvb
);
251 void proto_register_roofnet(void)
253 static hf_register_info hf
[] = {
255 { &hf_roofnet_version
,
256 { "Version", "roofnet.version",
257 FT_UINT8
, BASE_DEC
, NULL
, 0x0, "Roofnet Version", HFILL
}
261 { "Type", "roofnet.type",
262 FT_UINT8
, BASE_DEC
, VALS(roofnet_pt_vals
), 0x0, "Roofnet Message Type", HFILL
}
265 { &hf_roofnet_nlinks
,
266 { "Number of Links", "roofnet.nlinks",
267 FT_UINT8
, BASE_DEC
, NULL
, 0x0, "Roofnet Number of Links", HFILL
}
271 { "Next Link", "roofnet.next",
272 FT_UINT8
, BASE_DEC
, NULL
, 0x0, "Roofnet Next Link to Use", HFILL
}
276 { "Time To Live", "roofnet.ttl",
277 FT_UINT16
, BASE_DEC
, NULL
, 0x0, "Roofnet Time to Live", HFILL
}
281 { "Checksum", "roofnet.cksum",
282 FT_UINT16
, BASE_DEC
, NULL
, 0x0, "Roofnet Header Checksum", HFILL
}
286 { "Flags", "roofnet.flags",
287 FT_UINT16
, BASE_HEX
, NULL
, 0x0, "Roofnet flags", HFILL
}
290 { &hf_roofnet_flags_error
,
291 { "Roofnet Error", "roofnet.flags.error",
292 FT_BOOLEAN
, 16, NULL
, ROOFNET_FLAG_ERROR
, NULL
, HFILL
}
295 { &hf_roofnet_flags_update
,
296 { "Roofnet Update", "roofnet.flags.update",
297 FT_BOOLEAN
, 16, NULL
, ROOFNET_FLAG_UPDATE
, NULL
, HFILL
}
300 { &hf_roofnet_flags_layer2
,
301 { "Roofnet Layer 2", "roofnet.flags.layer2",
302 FT_BOOLEAN
, 16, NULL
, ROOFNET_FLAG_LAYER2
, NULL
, HFILL
}
305 { &hf_roofnet_flags_reserved
,
306 { "Roofnet Reserved", "roofnet.flags.reserved",
307 FT_BOOLEAN
, 16, NULL
, ROOFNET_FLAG_RESERVED
, NULL
, HFILL
}
310 { &hf_roofnet_data_length
,
311 { "Data Length", "roofnet.datalength",
312 FT_UINT16
, BASE_DEC
, NULL
, 0x0, "Data Payload Length", HFILL
}
315 { &hf_roofnet_query_dst
,
316 { "Query Dst", "roofnet.querydst",
317 FT_IPv4
, BASE_NONE
, NULL
, 0x0, "Roofnet Query Destination", HFILL
}
321 { "Seq", "roofnet.seq",
322 FT_UINT32
, BASE_DEC
, NULL
, 0x0, "Roofnet Sequential Number", HFILL
}
327 { "Links", "roofnet.links",
328 FT_NONE
, BASE_NONE
, NULL
, 0x0, NULL
, HFILL
}
332 { &hf_roofnet_link_src
,
333 { "Source IP", "roofnet.link.src",
334 FT_IPv4
, BASE_NONE
, NULL
, 0x0, "Roofnet Message Source", HFILL
}
337 { &hf_roofnet_link_forward
,
338 { "Forward", "roofnet.link.forward",
339 FT_UINT32
, BASE_DEC
, NULL
, 0x0, NULL
, HFILL
}
342 { &hf_roofnet_link_rev
,
343 { "Rev", "roofnet.link.rev",
344 FT_UINT32
, BASE_DEC
, NULL
, 0x0, "Revision Number", HFILL
}
347 { &hf_roofnet_link_seq
,
348 { "Seq", "roofnet.link.seq",
349 FT_UINT32
, BASE_DEC
, NULL
, 0x0, "Link Sequential Number", HFILL
}
352 { &hf_roofnet_link_age
,
353 { "Age", "roofnet.link.age",
354 FT_UINT32
, BASE_DEC
, NULL
, 0x0, "Information Age", HFILL
}
357 { &hf_roofnet_link_dst
,
358 { "Dst IP", "roofnet.link.dst",
359 FT_IPv4
, BASE_NONE
, NULL
, 0x0, "Roofnet Message Destination", HFILL
}
363 /* setup protocol subtree array */
364 static int *ett
[] = {
370 static ei_register_info ei
[] = {
371 { &ei_roofnet_too_many_links
, { "roofnet.too_many_links", PI_MALFORMED
, PI_ERROR
, "Too many links", EXPFILL
}},
372 { &ei_roofnet_too_much_data
, { "roofnet.too_much_data", PI_MALFORMED
, PI_ERROR
, "More payload data than told by Roofnet", EXPFILL
}},
375 expert_module_t
* expert_roofnet
;
377 proto_roofnet
= proto_register_protocol("Roofnet Protocol", "Roofnet", "roofnet");
379 proto_register_field_array(proto_roofnet
, hf
, array_length(hf
));
380 proto_register_subtree_array(ett
, array_length(ett
));
381 expert_roofnet
= expert_register_protocol(proto_roofnet
);
382 expert_register_field_array(expert_roofnet
, ei
, array_length(ei
));
384 roofnet_handle
= register_dissector("roofnet", dissect_roofnet
, proto_roofnet
);
388 void proto_reg_handoff_roofnet(void)
390 /* Until now there is no other option than having an IPv4 payload (maybe
391 * extended one day to IPv6 or other?) */
392 ip_handle
= find_dissector_add_dependency("ip", proto_roofnet
);
393 eth_withoutfcs_handle
= find_dissector_add_dependency("eth_withoutfcs", proto_roofnet
);
394 /* I did not put the type numbers in the ethertypes.h as they only are
395 * experimental and not official */
396 dissector_add_uint("ethertype", 0x0641, roofnet_handle
);
397 dissector_add_uint("ethertype", 0x0643, roofnet_handle
);
398 dissector_add_uint("ethertype", 0x0644, roofnet_handle
);
399 dissector_add_uint("ethertype", 0x0645, roofnet_handle
);
403 * Editor modelines - https://www.wireshark.org/tools/modelines.html
408 * indent-tabs-mode: nil
411 * ex: set shiftwidth=2 tabstop=8 expandtab:
412 * :indentSize=2:tabSize=8:noTabs=true: