From Kovarththanan Rajaratnam via bug 3548:
[obnox/wireshark/wip.git] / epan / dissectors / packet-aarp.c
1 /* packet-aarp.c
2  * Routines for Appletalk ARP packet disassembly
3  *
4  * $Id$
5  *
6  * Simon Wilkinson <sxw@dcs.ed.ac.uk>
7  *
8  * Wireshark - Network traffic analyzer
9  * By Gerald Combs <gerald@wireshark.org>
10  * Copyright 1998
11  *
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License
14  * as published by the Free Software Foundation; either version 2
15  * of the License, or (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
25  */
26
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <stdio.h>
32 #include <glib.h>
33 #include <epan/packet.h>
34 #include <epan/strutil.h>
35 #include <epan/emem.h>
36 #include <epan/etypes.h>
37
38 static int proto_aarp = -1;
39 static int hf_aarp_hard_type = -1;
40 static int hf_aarp_proto_type = -1;
41 static int hf_aarp_hard_size = -1;
42 static int hf_aarp_proto_size = -1;
43 static int hf_aarp_opcode = -1;
44 static int hf_aarp_src_hw = -1;
45 static int hf_aarp_src_hw_mac = -1;
46 static int hf_aarp_src_proto = -1;
47 static int hf_aarp_src_proto_id = -1;
48 static int hf_aarp_dst_hw = -1;
49 static int hf_aarp_dst_hw_mac = -1;
50 static int hf_aarp_dst_proto = -1;
51 static int hf_aarp_dst_proto_id = -1;
52
53 static gint ett_aarp = -1;
54
55 #ifndef AARP_REQUEST
56 #define AARP_REQUEST    0x0001
57 #endif
58 #ifndef AARP_REPLY
59 #define AARP_REPLY      0x0002
60 #endif
61 #ifndef AARP_PROBE
62 #define AARP_PROBE      0x0003
63 #endif
64
65 /* The following is screwed up shit to deal with the fact that
66    the linux kernel edits the packet inline. */
67 #define AARP_REQUEST_SWAPPED    0x0100
68 #define AARP_REPLY_SWAPPED  0x0200
69 #define AARP_PROBE_SWAPPED  0x0300
70
71 static const value_string op_vals[] = {
72   {AARP_REQUEST,  "request" },
73   {AARP_REPLY,    "reply"   },
74   {AARP_PROBE,    "probe"   },
75   {AARP_REQUEST_SWAPPED,  "request" },
76   {AARP_REPLY_SWAPPED,    "reply"   },
77   {AARP_PROBE_SWAPPED,    "probe"   },
78   {0,             NULL           } };
79
80 /* AARP protocol HARDWARE identifiers. */
81 #define AARPHRD_ETHER   1               /* Ethernet 10Mbps              */
82 #define AARPHRD_TR      2               /* Token Ring                   */
83
84 static const value_string hrd_vals[] = {
85   {AARPHRD_ETHER,   "Ethernet"       },
86   {AARPHRD_TR,      "Token Ring"     },
87   {0,               NULL             } };
88
89 /*
90  * Given the hardware address type and length, check whether an address
91  * is an Ethernet address - the address must be of type "Ethernet" or
92  * "Token Ring", and the length must be 6 bytes.
93  */
94 #define AARP_HW_IS_ETHER(ar_hrd, ar_hln) \
95         (((ar_hrd) == AARPHRD_ETHER || (ar_hrd) == AARPHRD_TR) \
96                                 && (ar_hln) == 6)
97
98 /*
99  * Given the protocol address type and length, check whether an address
100  * is an Appletalk address - the address must be of type "Appletalk",
101  * and the length must be 4 bytes.
102  */
103 #define AARP_PRO_IS_ATALK(ar_pro, ar_pln) \
104         ((ar_pro) == ETHERTYPE_ATALK && (ar_pln) == 4)
105
106 static gchar *
107 atalkid_to_str(const guint8 *ad) {
108   gint node;
109   gchar *cur;
110
111   cur=ep_alloc(16);
112   node=ad[1]<<8|ad[2];
113   g_snprintf(cur, 16, "%d.%d",node,ad[3]);
114   return cur;
115 }
116
117 static gchar *
118 aarphrdaddr_to_str(const guint8 *ad, int ad_len, guint16 type) {
119   if (AARP_HW_IS_ETHER(type, ad_len)) {
120     /* Ethernet address (or Token Ring address, which is the same type
121        of address). */
122     return ether_to_str(ad);
123   }
124   return bytes_to_str(ad, ad_len);
125 }
126
127 static gchar *
128 aarpproaddr_to_str(const guint8 *ad, int ad_len, guint16 type) {
129   if (AARP_PRO_IS_ATALK(type, ad_len)) {
130     /* Appletalk address.  */
131     return atalkid_to_str(ad);
132   }
133   return bytes_to_str(ad, ad_len);
134 }
135
136 /* Offsets of fields within an AARP packet. */
137 #define AR_HRD          0
138 #define AR_PRO          2
139 #define AR_HLN          4
140 #define AR_PLN          5
141 #define AR_OP           6
142 #define MIN_AARP_HEADER_SIZE    8
143
144 static void
145 dissect_aarp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree) {
146   guint16     ar_hrd;
147   guint16     ar_pro;
148   guint8      ar_hln;
149   guint8      ar_pln;
150   guint16     ar_op;
151   proto_tree  *aarp_tree;
152   proto_item  *ti;
153   const gchar *op_str;
154   int         sha_offset, spa_offset, tha_offset, tpa_offset;
155   const guint8      *sha_val, *spa_val, *tha_val, *tpa_val;
156   gchar       *sha_str, *spa_str, *tha_str, *tpa_str;
157
158   if(check_col(pinfo->cinfo, COL_PROTOCOL))
159     col_set_str(pinfo->cinfo, COL_PROTOCOL, "AARP");
160   if(check_col(pinfo->cinfo, COL_INFO))
161     col_clear(pinfo->cinfo, COL_INFO);
162
163   ar_hrd = tvb_get_ntohs(tvb, AR_HRD);
164   ar_pro = tvb_get_ntohs(tvb, AR_PRO);
165   ar_hln = tvb_get_guint8(tvb, AR_HLN);
166   ar_pln = tvb_get_guint8(tvb, AR_PLN);
167   ar_op  = tvb_get_ntohs(tvb, AR_OP);
168
169   /* Get the offsets of the addresses. */
170   sha_offset = MIN_AARP_HEADER_SIZE;
171   spa_offset = sha_offset + ar_hln;
172   tha_offset = spa_offset + ar_pln;
173   tpa_offset = tha_offset + ar_hln;
174
175   /* Extract the addresses.  */
176   sha_val = tvb_get_ptr(tvb, sha_offset, ar_hln);
177   sha_str = aarphrdaddr_to_str(sha_val, ar_hln, ar_hrd);
178
179   spa_val = tvb_get_ptr(tvb, spa_offset, ar_pln);
180   spa_str = aarpproaddr_to_str(spa_val, ar_pln, ar_pro);
181
182   tha_val = tvb_get_ptr(tvb, tha_offset, ar_hln);
183   tha_str = aarphrdaddr_to_str(tha_val, ar_hln, ar_hrd);
184
185   tpa_val = tvb_get_ptr(tvb, tpa_offset, ar_pln);
186   tpa_str = aarpproaddr_to_str(tpa_val, ar_pln, ar_pro);
187
188   if (check_col(pinfo->cinfo, COL_INFO)) {
189     switch (ar_op) {
190       case AARP_REQUEST:
191       case AARP_REQUEST_SWAPPED:
192         col_add_fstr(pinfo->cinfo, COL_INFO, "Who has %s?  Tell %s", tpa_str, spa_str);
193         break;
194       case AARP_REPLY:
195       case AARP_REPLY_SWAPPED:
196         col_add_fstr(pinfo->cinfo, COL_INFO, "%s is at %s", spa_str, sha_str);
197         break;
198       case AARP_PROBE:
199       case AARP_PROBE_SWAPPED:
200         col_add_fstr(pinfo->cinfo, COL_INFO, "Is there a %s", tpa_str);
201         break;
202       default:
203         col_add_fstr(pinfo->cinfo, COL_INFO, "Unknown AARP opcode 0x%04x", ar_op);
204         break;
205     }
206   }
207
208   if (tree) {
209     if ((op_str = match_strval(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, FALSE);
233     }
234
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,
239                                           spa_val,
240                                           "%s", spa_str);
241       } else {
242         proto_tree_add_bytes_format_value(aarp_tree, hf_aarp_src_proto, tvb,
243                                           spa_offset, ar_pln,
244                                           spa_val,
245                                           "%s", spa_str);
246       }
247     }
248
249     if (ar_hln != 0) {
250       proto_tree_add_item(aarp_tree,
251         AARP_HW_IS_ETHER(ar_hrd, ar_hln) ? hf_aarp_dst_hw_mac : hf_aarp_dst_hw,
252         tvb, tha_offset, ar_hln, FALSE);
253     }
254
255     if (ar_pln != 0) {
256       if (AARP_PRO_IS_ATALK(ar_pro, ar_pln)) {
257         proto_tree_add_bytes_format_value(aarp_tree, hf_aarp_dst_proto_id, tvb,
258                                           tpa_offset, ar_pln,
259                                           tpa_val,
260                                           "%s", tpa_str);
261       } else {
262         proto_tree_add_bytes_format_value(aarp_tree, hf_aarp_dst_proto, tvb,
263                                           tpa_offset, ar_pln,
264                                           tpa_val,
265                                           "%s", tpa_str);
266       }
267     }
268   }
269 }
270
271 void
272 proto_register_aarp(void)
273 {
274   static hf_register_info hf[] = {
275     { &hf_aarp_hard_type,
276       { "Hardware type",                "aarp.hard.type",
277         FT_UINT16,      BASE_HEX,       VALS(hrd_vals), 0x0,
278         NULL, HFILL }},
279
280     { &hf_aarp_proto_type,
281       { "Protocol type",                "aarp.proto.type",
282         FT_UINT16,      BASE_HEX,       VALS(etype_vals),       0x0,
283         NULL, HFILL }},
284
285     { &hf_aarp_hard_size,
286       { "Hardware size",                "aarp.hard.size",
287         FT_UINT8,       BASE_DEC,       NULL,   0x0,
288         NULL, HFILL }},
289
290     { &hf_aarp_proto_size,
291       { "Protocol size",                "aarp.proto.size",
292         FT_UINT8,       BASE_DEC,       NULL,   0x0,
293         NULL, HFILL }},
294
295     { &hf_aarp_opcode,
296       { "Opcode",                       "aarp.opcode",
297         FT_UINT16,      BASE_DEC,       VALS(op_vals),  0x0,
298         NULL, HFILL }},
299
300     { &hf_aarp_src_hw,
301       { "Sender hardware address",      "aarp.src.hw",
302         FT_BYTES,       BASE_NONE,      NULL,   0x0,
303         NULL, HFILL }},
304
305     { &hf_aarp_src_hw_mac,
306       { "Sender MAC address",           "aarp.src.hw_mac",
307         FT_ETHER,       BASE_NONE,      NULL,   0x0,
308         NULL, HFILL }},
309
310     { &hf_aarp_src_proto,
311       { "Sender protocol address",      "aarp.src.proto",
312         FT_BYTES,       BASE_NONE,      NULL,   0x0,
313         NULL, HFILL }},
314
315     { &hf_aarp_src_proto_id,
316       { "Sender ID",                    "aarp.src.proto_id",
317         FT_BYTES,       BASE_NONE,      NULL,   0x0,
318         NULL, HFILL }},
319
320     { &hf_aarp_dst_hw,
321       { "Target hardware address",      "aarp.dst.hw",
322         FT_BYTES,       BASE_NONE,      NULL,   0x0,
323         NULL, HFILL }},
324
325     { &hf_aarp_dst_hw_mac,
326       { "Target MAC address",           "aarp.dst.hw_mac",
327         FT_ETHER,       BASE_NONE,      NULL,   0x0,
328         NULL, HFILL }},
329
330     { &hf_aarp_dst_proto,
331       { "Target protocol address",      "aarp.dst.proto",
332         FT_BYTES,       BASE_NONE,      NULL,   0x0,
333       NULL, HFILL }},
334
335     { &hf_aarp_dst_proto_id,
336       { "Target ID",                    "aarp.dst.proto_id",
337         FT_BYTES,       BASE_NONE,      NULL,   0x0,
338         NULL, HFILL }},
339   };
340   static gint *ett[] = {
341     &ett_aarp,
342   };
343
344   proto_aarp = proto_register_protocol("Appletalk Address Resolution Protocol",
345                                        "AARP",
346                                        "aarp");
347   proto_register_field_array(proto_aarp, hf, array_length(hf));
348   proto_register_subtree_array(ett, array_length(ett));
349 }
350
351 void
352 proto_reg_handoff_aarp(void)
353 {
354   dissector_handle_t aarp_handle;
355
356   aarp_handle = create_dissector_handle(dissect_aarp, proto_aarp);
357   dissector_add("ethertype", ETHERTYPE_AARP, aarp_handle);
358   dissector_add("chdlctype", ETHERTYPE_AARP, aarp_handle);
359 }