Revert "TODO epan/dissectors/asn1/kerberos/packet-kerberos-template.c new GSS flags"
[wireshark-sm.git] / epan / dissectors / packet-aarp.c
bloba656ef3fdbcd3361e086b227ce6632bd35a816d1
1 /* packet-aarp.c
2 * Routines for Appletalk ARP packet disassembly
4 * Simon Wilkinson <sxw@dcs.ed.ac.uk>
6 * Wireshark - Network traffic analyzer
7 * By Gerald Combs <gerald@wireshark.org>
8 * Copyright 1998
10 * SPDX-License-Identifier: GPL-2.0-or-later
13 #include "config.h"
15 #include <epan/packet.h>
16 #include <epan/etypes.h>
17 #include <epan/expert.h>
18 #include <epan/to_str.h>
20 /* Forward declarations */
21 void proto_register_aarp(void);
22 void proto_reg_handoff_aarp(void);
24 static int proto_aarp;
25 static int hf_aarp_hard_type;
26 static int hf_aarp_proto_type;
27 static int hf_aarp_hard_size;
28 static int hf_aarp_proto_size;
29 static int hf_aarp_opcode;
30 static int hf_aarp_src_hw;
31 static int hf_aarp_src_hw_mac;
32 static int hf_aarp_src_proto;
33 static int hf_aarp_src_proto_id;
34 static int hf_aarp_dst_hw;
35 static int hf_aarp_dst_hw_mac;
36 static int hf_aarp_dst_proto;
37 static int hf_aarp_dst_proto_id;
39 static int ett_aarp;
41 static expert_field ei_aarp_length_invalid;
43 #ifndef AARP_REQUEST
44 #define AARP_REQUEST 0x0001
45 #endif
46 #ifndef AARP_REPLY
47 #define AARP_REPLY 0x0002
48 #endif
49 #ifndef AARP_PROBE
50 #define AARP_PROBE 0x0003
51 #endif
53 /* The following is screwed up shit to deal with the fact that
54 the linux kernel edits the packet inline. */
55 #define AARP_REQUEST_SWAPPED 0x0100
56 #define AARP_REPLY_SWAPPED 0x0200
57 #define AARP_PROBE_SWAPPED 0x0300
59 static const value_string op_vals[] = {
60 {AARP_REQUEST, "request" },
61 {AARP_REPLY, "reply" },
62 {AARP_PROBE, "probe" },
63 {AARP_REQUEST_SWAPPED, "request" },
64 {AARP_REPLY_SWAPPED, "reply" },
65 {AARP_PROBE_SWAPPED, "probe" },
66 {0, NULL } };
68 /* AARP protocol HARDWARE identifiers. */
69 #define AARPHRD_ETHER 1 /* Ethernet 10Mbps */
70 #define AARPHRD_TR 2 /* Token Ring */
72 static const value_string hrd_vals[] = {
73 {AARPHRD_ETHER, "Ethernet" },
74 {AARPHRD_TR, "Token Ring" },
75 {0, NULL } };
78 * Given the hardware address type and length, check whether an address
79 * is an Ethernet address - the address must be of type "Ethernet" or
80 * "Token Ring", and the length must be 6 bytes.
82 #define AARP_HW_IS_ETHER(ar_hrd, ar_hln) \
83 (((ar_hrd) == AARPHRD_ETHER || (ar_hrd) == AARPHRD_TR) \
84 && (ar_hln) == 6)
87 * Given the protocol address type and length, check whether an address
88 * is an Appletalk address - the address must be of type "Appletalk",
89 * and the length must be 4 bytes.
91 #define AARP_PRO_IS_ATALK(ar_pro, ar_pln) \
92 ((ar_pro) == ETHERTYPE_ATALK && (ar_pln) == 4)
94 static char *
95 tvb_atalkid_to_str(wmem_allocator_t *scope, tvbuff_t *tvb, int offset)
97 int node;
98 char *cur;
100 cur=(char *)wmem_alloc(scope, 16);
101 node=tvb_get_uint8(tvb, offset+1)<<8|tvb_get_uint8(tvb, offset+2);
102 snprintf(cur, 16, "%d.%d",node,tvb_get_uint8(tvb, offset+3));
103 return cur;
106 static const char *
107 tvb_aarphrdaddr_to_str(wmem_allocator_t *scope, tvbuff_t *tvb, int offset, int ad_len, uint16_t type)
109 if (AARP_HW_IS_ETHER(type, ad_len)) {
110 /* Ethernet address (or Token Ring address, which is the same type
111 of address). */
112 return tvb_ether_to_str(scope, tvb, offset);
114 return tvb_bytes_to_str(scope, tvb, offset, ad_len);
117 static char *
118 tvb_aarpproaddr_to_str(wmem_allocator_t *scope, tvbuff_t *tvb, int offset, int ad_len, uint16_t type)
120 if (AARP_PRO_IS_ATALK(type, ad_len)) {
121 /* Appletalk address. */
122 return tvb_atalkid_to_str(scope, tvb, offset);
124 return tvb_bytes_to_str(scope, tvb, offset, ad_len);
127 /* Offsets of fields within an AARP packet. */
128 #define AR_HRD 0
129 #define AR_PRO 2
130 #define AR_HLN 4
131 #define AR_PLN 5
132 #define AR_OP 6
133 #define MIN_AARP_HEADER_SIZE 8
135 static int
136 dissect_aarp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data _U_) {
137 uint16_t ar_hrd;
138 uint16_t ar_pro;
139 uint8_t ar_hln;
140 uint8_t ar_pln;
141 uint16_t ar_op;
142 proto_tree *aarp_tree;
143 proto_item *ti;
144 const char *op_str;
145 int sha_offset, spa_offset, tha_offset, tpa_offset;
146 const char *sha_str, *spa_str, /* *tha_str, */ *tpa_str;
148 col_set_str(pinfo->cinfo, COL_PROTOCOL, "AARP");
149 col_clear(pinfo->cinfo, COL_INFO);
151 ar_hrd = tvb_get_ntohs(tvb, AR_HRD);
152 ar_pro = tvb_get_ntohs(tvb, AR_PRO);
153 ar_hln = tvb_get_uint8(tvb, AR_HLN);
154 ar_pln = tvb_get_uint8(tvb, AR_PLN);
155 ar_op = tvb_get_ntohs(tvb, AR_OP);
157 /* Get the offsets of the addresses. */
158 sha_offset = MIN_AARP_HEADER_SIZE;
159 spa_offset = sha_offset + ar_hln;
160 tha_offset = spa_offset + ar_pln;
161 tpa_offset = tha_offset + ar_hln;
163 /* Extract the addresses. */
165 if (ar_hln < 1) {
166 expert_add_info_format(pinfo, tree, &ei_aarp_length_invalid,
167 "Invalid hardware address length: %d", ar_hln);
168 sha_str = "Unknown";
169 #if 0
170 tha_str = "Unknown";
171 #endif
172 } else {
173 sha_str = tvb_aarphrdaddr_to_str(pinfo->pool, tvb, sha_offset, ar_hln, ar_hrd);
174 #if 0
175 /* TODO: tha_str is currently not shown nor parsed */
176 tha_str = tvb_aarphrdaddr_to_str(pinfo->pool, tvb, tha_offset, ar_hln, ar_hrd);
177 #endif
180 if (ar_pln < 1) {
181 expert_add_info_format(pinfo, tree, &ei_aarp_length_invalid,
182 "Invalid protocol address length: %d", ar_pln);
183 spa_str = "Unknown";
184 tpa_str = "Unknown";
185 } else {
186 spa_str = tvb_aarpproaddr_to_str(pinfo->pool, tvb, spa_offset, ar_pln, ar_pro);
187 tpa_str = tvb_aarpproaddr_to_str(pinfo->pool, tvb, tpa_offset, ar_pln, ar_pro);
190 switch (ar_op) {
191 case AARP_REQUEST:
192 case AARP_REQUEST_SWAPPED:
193 col_add_fstr(pinfo->cinfo, COL_INFO, "Who has %s? Tell %s", tpa_str, spa_str);
194 break;
195 case AARP_REPLY:
196 case AARP_REPLY_SWAPPED:
197 col_add_fstr(pinfo->cinfo, COL_INFO, "%s is at %s", spa_str, sha_str);
198 break;
199 case AARP_PROBE:
200 case AARP_PROBE_SWAPPED:
201 col_add_fstr(pinfo->cinfo, COL_INFO, "Is there a %s", tpa_str);
202 break;
203 default:
204 col_add_fstr(pinfo->cinfo, COL_INFO, "Unknown AARP opcode 0x%04x", ar_op);
205 break;
208 if (tree) {
209 if ((op_str = try_val_to_str(ar_op, op_vals)))
210 ti = proto_tree_add_protocol_format(tree, proto_aarp, tvb, 0,
211 MIN_AARP_HEADER_SIZE + 2*ar_hln +
212 2*ar_pln, "AppleTalk Address Resolution Protocol (%s)", op_str);
213 else
214 ti = proto_tree_add_protocol_format(tree, proto_aarp, tvb, 0,
215 MIN_AARP_HEADER_SIZE + 2*ar_hln +
216 2*ar_pln,
217 "AppleTalk Address Resolution Protocol (opcode 0x%04x)", ar_op);
218 aarp_tree = proto_item_add_subtree(ti, ett_aarp);
219 proto_tree_add_uint(aarp_tree, hf_aarp_hard_type, tvb, AR_HRD, 2,
220 ar_hrd);
221 proto_tree_add_uint(aarp_tree, hf_aarp_proto_type, tvb, AR_PRO, 2,
222 ar_pro);
223 proto_tree_add_uint(aarp_tree, hf_aarp_hard_size, tvb, AR_HLN, 1,
224 ar_hln);
225 proto_tree_add_uint(aarp_tree, hf_aarp_proto_size, tvb, AR_PLN, 1,
226 ar_pln);
227 proto_tree_add_uint(aarp_tree, hf_aarp_opcode, tvb, AR_OP, 2,
228 ar_op);
229 if (ar_hln != 0) {
230 proto_tree_add_item(aarp_tree,
231 AARP_HW_IS_ETHER(ar_hrd, ar_hln) ? hf_aarp_src_hw_mac : hf_aarp_src_hw,
232 tvb, sha_offset, ar_hln, ENC_NA);
235 if (ar_pln != 0) {
236 if (AARP_PRO_IS_ATALK(ar_pro, ar_pln)) {
237 proto_tree_add_bytes_format_value(aarp_tree, hf_aarp_src_proto_id, tvb,
238 spa_offset, ar_pln, NULL,
239 "%s", spa_str);
240 } else {
241 proto_tree_add_bytes_format_value(aarp_tree, hf_aarp_src_proto, tvb,
242 spa_offset, ar_pln, NULL,
243 "%s", spa_str);
247 if (ar_hln != 0) {
248 proto_tree_add_item(aarp_tree,
249 AARP_HW_IS_ETHER(ar_hrd, ar_hln) ? hf_aarp_dst_hw_mac : hf_aarp_dst_hw,
250 tvb, tha_offset, ar_hln, ENC_NA);
253 if (ar_pln != 0) {
254 if (AARP_PRO_IS_ATALK(ar_pro, ar_pln)) {
255 proto_tree_add_bytes_format_value(aarp_tree, hf_aarp_dst_proto_id, tvb,
256 tpa_offset, ar_pln,
257 NULL, "%s", tpa_str);
258 } else {
259 proto_tree_add_bytes_format_value(aarp_tree, hf_aarp_dst_proto, tvb,
260 tpa_offset, ar_pln,
261 NULL, "%s", tpa_str);
265 return tvb_captured_length(tvb);
268 void
269 proto_register_aarp(void)
271 static hf_register_info hf[] = {
272 { &hf_aarp_hard_type,
273 { "Hardware type", "aarp.hard.type",
274 FT_UINT16, BASE_HEX, VALS(hrd_vals), 0x0,
275 NULL, HFILL }},
277 { &hf_aarp_proto_type,
278 { "Protocol type", "aarp.proto.type",
279 FT_UINT16, BASE_HEX, VALS(etype_vals), 0x0,
280 NULL, HFILL }},
282 { &hf_aarp_hard_size,
283 { "Hardware size", "aarp.hard.size",
284 FT_UINT8, BASE_DEC, NULL, 0x0,
285 NULL, HFILL }},
287 { &hf_aarp_proto_size,
288 { "Protocol size", "aarp.proto.size",
289 FT_UINT8, BASE_DEC, NULL, 0x0,
290 NULL, HFILL }},
292 { &hf_aarp_opcode,
293 { "Opcode", "aarp.opcode",
294 FT_UINT16, BASE_DEC, VALS(op_vals), 0x0,
295 NULL, HFILL }},
297 { &hf_aarp_src_hw,
298 { "Sender hardware address", "aarp.src.hw",
299 FT_BYTES, BASE_NONE, NULL, 0x0,
300 NULL, HFILL }},
302 { &hf_aarp_src_hw_mac,
303 { "Sender MAC address", "aarp.src.hw_mac",
304 FT_ETHER, BASE_NONE, NULL, 0x0,
305 NULL, HFILL }},
307 { &hf_aarp_src_proto,
308 { "Sender protocol address", "aarp.src.proto",
309 FT_BYTES, BASE_NONE, NULL, 0x0,
310 NULL, HFILL }},
312 { &hf_aarp_src_proto_id,
313 { "Sender ID", "aarp.src.proto_id",
314 FT_BYTES, BASE_NONE, NULL, 0x0,
315 NULL, HFILL }},
317 { &hf_aarp_dst_hw,
318 { "Target hardware address", "aarp.dst.hw",
319 FT_BYTES, BASE_NONE, NULL, 0x0,
320 NULL, HFILL }},
322 { &hf_aarp_dst_hw_mac,
323 { "Target MAC address", "aarp.dst.hw_mac",
324 FT_ETHER, BASE_NONE, NULL, 0x0,
325 NULL, HFILL }},
327 { &hf_aarp_dst_proto,
328 { "Target protocol address", "aarp.dst.proto",
329 FT_BYTES, BASE_NONE, NULL, 0x0,
330 NULL, HFILL }},
332 { &hf_aarp_dst_proto_id,
333 { "Target ID", "aarp.dst.proto_id",
334 FT_BYTES, BASE_NONE, NULL, 0x0,
335 NULL, HFILL }},
337 static int *ett[] = {
338 &ett_aarp,
341 static ei_register_info ei[] = {
342 { &ei_aarp_length_invalid, { "aarp.length.invalid", PI_PROTOCOL, PI_WARN, "Invalid length", EXPFILL }},
345 proto_aarp = proto_register_protocol("Appletalk Address Resolution Protocol",
346 "AARP",
347 "aarp");
348 register_dissector("aarp", dissect_aarp, proto_aarp);
349 proto_register_field_array(proto_aarp, hf, array_length(hf));
350 proto_register_subtree_array(ett, array_length(ett));
352 expert_module_t* expert_aarp = expert_register_protocol(proto_aarp);
353 expert_register_field_array(expert_aarp, ei, array_length(ei));
357 void
358 proto_reg_handoff_aarp(void)
360 dissector_handle_t aarp_handle = find_dissector("aarp");
362 dissector_add_uint("ethertype", ETHERTYPE_AARP, aarp_handle);
363 dissector_add_uint("chdlc.protocol", ETHERTYPE_AARP, aarp_handle);
367 * Editor modelines - https://www.wireshark.org/tools/modelines.html
369 * Local Variables:
370 * c-basic-offset: 2
371 * tab-width: 8
372 * indent-tabs-mode: nil
373 * End:
375 * ex: set shiftwidth=2 tabstop=8 expandtab:
376 * :indentSize=2:tabSize=8:noTabs=true: