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