For proto_tree_add_item(..., proto_xxx, ...)use ENC_NA as the encoding arg.
[obnox/wireshark/wip.git] / epan / dissectors / packet-arcnet.c
1 /* packet-arcnet.c
2  * Routines for arcnet dissection
3  * Copyright 2001-2002, Peter Fales <ethereal@fales-lorenz.net>
4  *
5  * $Id$
6  *
7  * Wireshark - Network traffic analyzer
8  * By Gerald Combs <gerald@wireshark.org>
9  * Copyright 1998 Gerald Combs
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24  */
25
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include <stdlib.h>
31
32 #include <glib.h>
33
34 #include <epan/packet.h>
35 #include "packet-arcnet.h"
36 #include <epan/arcnet_pids.h>
37 #include "packet-ip.h"
38
39 /* Initialize the protocol and registered fields */
40 static int proto_arcnet = -1;
41 static int hf_arcnet_src = -1;
42 static int hf_arcnet_dst = -1;
43 static int hf_arcnet_offset = -1;
44 static int hf_arcnet_protID = -1;
45 static int hf_arcnet_exception_flag = -1;
46 static int hf_arcnet_split_flag = -1;
47 static int hf_arcnet_sequence = -1;
48
49 /* Initialize the subtree pointers */
50 static gint ett_arcnet = -1;
51
52 static dissector_table_t arcnet_dissector_table;
53 static dissector_handle_t data_handle;
54
55 void
56 capture_arcnet (const guchar *pd, int len, packet_counts *ld,
57                 gboolean has_offset, gboolean has_exception)
58 {
59   int offset = has_offset ? 4 : 2;
60
61   if (!BYTES_ARE_IN_FRAME(offset, len, 1)) {
62     ld->other++;
63     return;
64   }
65
66   switch (pd[offset]) {
67
68   case ARCNET_PROTO_IP_1051:
69     /* No fragmentation stuff in the header */
70     capture_ip(pd, offset + 1, len, ld);
71     break;
72
73   case ARCNET_PROTO_IP_1201:
74     /*
75      * There's fragmentation stuff in the header.
76      *
77      * XXX - on at least some versions of NetBSD, it appears that we
78      * might we get ARCNET frames, not reassembled packets; we should
79      * perhaps bump "ld->other" for all but the first frame of a packet.
80      *
81      * XXX - but on FreeBSD it appears that we get reassembled packets
82      * on input (but apparently we get frames on output - or maybe
83      * we get the packet *and* all its frames!); how to tell the
84      * difference?  It looks from the FreeBSD reassembly code as if
85      * the reassembled packet arrives with the header for the first
86      * frame.  It also looks as if, on output, we first get the
87      * full packet, with a header containing none of the fragmentation
88      * stuff, and then get the frames.
89      *
90      * On Linux, we get only reassembled packets, and the exception
91      * frame stuff is hidden - there's a split flag and sequence
92      * number, but it appears that it will never have the exception
93      * frame stuff.
94      *
95      * XXX - what about OpenBSD?  And, for that matter, what about
96      * Windows?  (I suspect Windows supplies reassembled frames,
97      * as WinPcap, like PF_PACKET sockets, taps into the networking
98      * stack just as other protocols do.)
99      */
100     offset++;
101     if (!BYTES_ARE_IN_FRAME(offset, len, 1)) {
102       ld->other++;
103       return;
104     }
105     if (has_exception && pd[offset] == 0xff) {
106       /* This is an exception packet.  The flag value there is the
107          "this is an exception flag" packet; the next two bytes
108          after it are padding, and another copy of the packet
109          type appears after the padding. */
110       offset += 4;
111     }
112     capture_ip(pd, offset + 3, len, ld);
113     break;
114
115   case ARCNET_PROTO_ARP_1051:
116   case ARCNET_PROTO_ARP_1201:
117     /*
118      * XXX - do we have to worry about fragmentation for ARP?
119      */
120     ld->arp++;
121     break;
122
123   case ARCNET_PROTO_IPX:
124     ld->ipx++;
125     break;
126
127   default:
128     ld->other++;
129     break;
130   }
131 }
132
133 static void
134 dissect_arcnet_common (tvbuff_t * tvb, packet_info * pinfo, proto_tree * tree,
135                        gboolean has_offset, gboolean has_exception)
136 {
137   int offset = 0;
138   guint8 dst, src, protID, split_flag;
139   tvbuff_t *next_tvb;
140   proto_item *ti = NULL;
141   proto_tree *arcnet_tree = NULL;
142
143   col_set_str (pinfo->cinfo, COL_PROTOCOL, "ARCNET");
144
145   col_set_str(pinfo->cinfo, COL_INFO, "ARCNET");
146
147   src = tvb_get_guint8 (tvb, 0);
148   dst = tvb_get_guint8 (tvb, 1);
149   SET_ADDRESS(&pinfo->dl_src,   AT_ARCNET, 1, tvb_get_ptr(tvb, 0, 1));
150   SET_ADDRESS(&pinfo->src,      AT_ARCNET, 1, tvb_get_ptr(tvb, 0, 1));
151   SET_ADDRESS(&pinfo->dl_dst,   AT_ARCNET, 1, tvb_get_ptr(tvb, 1, 1));
152   SET_ADDRESS(&pinfo->dst,      AT_ARCNET, 1, tvb_get_ptr(tvb, 1, 1));
153
154   ti = proto_tree_add_item (tree, proto_arcnet, tvb, 0, -1, ENC_NA);
155
156   arcnet_tree = proto_item_add_subtree (ti, ett_arcnet);
157
158   proto_tree_add_uint (arcnet_tree, hf_arcnet_src, tvb, offset, 1, src);
159   offset++;
160
161   proto_tree_add_uint (arcnet_tree, hf_arcnet_dst, tvb, offset, 1, dst);
162   offset++;
163
164   if (has_offset) {
165     proto_tree_add_item (arcnet_tree, hf_arcnet_offset, tvb, offset, 2, ENC_NA);
166     offset += 2;
167   }
168
169   protID = tvb_get_guint8 (tvb, offset);
170   proto_tree_add_uint (arcnet_tree, hf_arcnet_protID, tvb, offset, 1, protID);
171   offset++;
172
173   switch (protID) {
174
175   case ARCNET_PROTO_IP_1051:
176   case ARCNET_PROTO_ARP_1051:
177   case ARCNET_PROTO_DIAGNOSE:
178   case ARCNET_PROTO_BACNET:     /* XXX - no fragmentation? */
179     /* No fragmentation stuff in the header */
180     break;
181
182   default:
183     /*
184      * Show the fragmentation stuff - flag and sequence ID.
185      *
186      * XXX - on at least some versions of NetBSD, it appears that
187      * we might get ARCNET frames, not reassembled packets; if so,
188      * we should reassemble them.
189      *
190      * XXX - but on FreeBSD it appears that we get reassembled packets
191      * on input (but apparently we get frames on output - or maybe
192      * we get the packet *and* all its frames!); how to tell the
193      * difference?  It looks from the FreeBSD reassembly code as if
194      * the reassembled packet arrives with the header for the first
195      * frame.  It also looks as if, on output, we first get the
196      * full packet, with a header containing none of the fragmentation
197      * stuff, and then get the frames.
198      *
199      * On Linux, we get only reassembled packets, and the exception
200      * frame stuff is hidden - there's a split flag and sequence
201      * number, but it appears that it will never have the exception
202      * frame stuff.
203      *
204      * XXX - what about OpenBSD?  And, for that matter, what about
205      * Windows?  (I suspect Windows supplies reassembled frames,
206      * as WinPcap, like PF_PACKET sockets, taps into the networking
207      * stack just as other protocols do.)
208      */
209     split_flag = tvb_get_guint8 (tvb, offset);
210     if (has_exception && split_flag == 0xff) {
211       /* This is an exception packet.  The flag value there is the
212          "this is an exception flag" packet; the next two bytes
213          after it are padding. */
214           proto_tree_add_uint (arcnet_tree, hf_arcnet_exception_flag, tvb, offset, 1,
215                              split_flag);
216       offset++;
217
218       proto_tree_add_text (arcnet_tree, tvb, offset, 2, "Padding");
219       offset += 2;
220
221       /* Another copy of the packet type appears after the padding. */
222       proto_tree_add_item (arcnet_tree, hf_arcnet_protID, tvb, offset, 1, ENC_BIG_ENDIAN);
223       offset++;
224
225       /* And after that comes the real split flag. */
226       split_flag = tvb_get_guint8 (tvb, offset);
227     }
228
229     proto_tree_add_uint (arcnet_tree, hf_arcnet_split_flag, tvb, offset, 1,
230                            split_flag);
231     offset++;
232
233     proto_tree_add_item (arcnet_tree, hf_arcnet_sequence, tvb, offset, 2, ENC_BIG_ENDIAN);
234     offset += 2;
235
236     break;
237   }
238
239   /* Set the length of the ARCNET header protocol tree item. */
240   proto_item_set_len(ti, offset);
241
242   next_tvb = tvb_new_subset_remaining (tvb, offset);
243
244   if (!dissector_try_uint (arcnet_dissector_table, protID,
245                            next_tvb, pinfo, tree))
246     {
247           col_add_fstr (pinfo->cinfo, COL_PROTOCOL, "0x%04x", protID);
248       call_dissector (data_handle, next_tvb, pinfo, tree);
249     }
250
251 }
252
253 /*
254  * BSD-style ARCNET headers - they don't have the offset field from the
255  * ARCNET hardware packet, but we might get an exception frame header.
256  */
257 static void
258 dissect_arcnet (tvbuff_t * tvb, packet_info * pinfo, proto_tree * tree)
259 {
260   dissect_arcnet_common (tvb, pinfo, tree, FALSE, TRUE);
261 }
262
263 /*
264  * Linux-style ARCNET headers - they *do* have the offset field from the
265  * ARCNET hardware packet, but we should never see an exception frame
266  * header.
267  */
268 static void
269 dissect_arcnet_linux (tvbuff_t * tvb, packet_info * pinfo, proto_tree * tree)
270 {
271   dissect_arcnet_common (tvb, pinfo, tree, TRUE, ENC_BIG_ENDIAN);
272 }
273
274 static const value_string arcnet_prot_id_vals[] = {
275   {ARCNET_PROTO_IP_1051,          "RFC 1051 IP"},
276   {ARCNET_PROTO_ARP_1051,         "RFC 1051 ARP"},
277   {ARCNET_PROTO_IP_1201,          "RFC 1201 IP"},
278   {ARCNET_PROTO_ARP_1201,         "RFC 1201 ARP"},
279   {ARCNET_PROTO_RARP_1201,        "RFC 1201 RARP"},
280   {ARCNET_PROTO_IPX,              "IPX"},
281   {ARCNET_PROTO_NOVELL_EC,        "Novell of some sort"},
282   {ARCNET_PROTO_IPv6,             "IPv6"},
283   {ARCNET_PROTO_ETHERNET,         "Encapsulated Ethernet"},
284   {ARCNET_PROTO_DATAPOINT_BOOT,   "Datapoint boot"},
285   {ARCNET_PROTO_DATAPOINT_MOUNT,  "Datapoint mount"},
286   {ARCNET_PROTO_POWERLAN_BEACON,  "PowerLAN beacon"},
287   {ARCNET_PROTO_POWERLAN_BEACON2, "PowerLAN beacon2"},
288   {ARCNET_PROTO_APPLETALK,        "Appletalk"},
289   {ARCNET_PROTO_BANYAN,           "Banyan VINES"},
290   {ARCNET_PROTO_DIAGNOSE,         "Diagnose"},
291   {ARCNET_PROTO_BACNET,           "BACnet"},
292   {0,                             NULL}
293 };
294
295 void
296 proto_register_arcnet (void)
297 {
298
299 /* Setup list of header fields  See Section 1.6.1 for details*/
300   static hf_register_info hf[] = {
301     {&hf_arcnet_src,
302      {"Source", "arcnet.src",
303       FT_UINT8, BASE_HEX, NULL, 0,
304       "Source ID", HFILL}
305      },
306     {&hf_arcnet_dst,
307      {"Dest", "arcnet.dst",
308       FT_UINT8, BASE_HEX, NULL, 0,
309       "Dest ID", HFILL}
310      },
311     {&hf_arcnet_offset,
312      {"Offset", "arcnet.offset",
313       FT_BYTES, BASE_NONE, NULL, 0,
314       NULL, HFILL}
315      },
316     {&hf_arcnet_protID,
317      {"Protocol ID", "arcnet.protID",
318       FT_UINT8, BASE_HEX, VALS(arcnet_prot_id_vals), 0,
319       "Proto type", HFILL}
320      },
321     {&hf_arcnet_split_flag,
322      {"Split Flag", "arcnet.split_flag",
323       FT_UINT8, BASE_DEC, NULL, 0,
324       NULL, HFILL}
325      },
326     {&hf_arcnet_exception_flag,
327      {"Exception Flag", "arcnet.exception_flag",
328       FT_UINT8, BASE_HEX, NULL, 0,
329       NULL, HFILL}
330      },
331     {&hf_arcnet_sequence,
332      {"Sequence", "arcnet.sequence",
333       FT_UINT16, BASE_DEC, NULL, 0,
334       "Sequence number", HFILL}
335      },
336   };
337
338 /* Setup protocol subtree array */
339   static gint *ett[] = {
340     &ett_arcnet,
341   };
342
343   arcnet_dissector_table = register_dissector_table ("arcnet.protocol_id",
344                                                      "ARCNET Protocol ID",
345                                                      FT_UINT8, BASE_HEX);
346
347 /* Register the protocol name and description */
348   proto_arcnet = proto_register_protocol ("ARCNET", "ARCNET", "arcnet");
349
350 /* Required function calls to register the header fields and subtrees used */
351   proto_register_field_array (proto_arcnet, hf, array_length (hf));
352   proto_register_subtree_array (ett, array_length (ett));
353 }
354
355
356 void
357 proto_reg_handoff_arcnet (void)
358 {
359   dissector_handle_t arcnet_handle, arcnet_linux_handle;
360
361   arcnet_handle = create_dissector_handle (dissect_arcnet, proto_arcnet);
362   dissector_add_uint ("wtap_encap", WTAP_ENCAP_ARCNET, arcnet_handle);
363
364   arcnet_linux_handle = create_dissector_handle (dissect_arcnet_linux,
365                                                  proto_arcnet);
366   dissector_add_uint ("wtap_encap", WTAP_ENCAP_ARCNET_LINUX, arcnet_linux_handle);
367   data_handle = find_dissector ("data");
368 }