Make the capture routines take an additional argument giving the amount
[obnox/wireshark/wip.git] / packet-fddi.c
1 /* packet-fddi.c
2  * Routines for FDDI packet disassembly
3  *
4  * Laurent Deniel <deniel@worldnet.fr>
5  *
6  * $Id: packet-fddi.c,v 1.52 2001/11/20 21:59:12 guy Exp $
7  *
8  * Ethereal - Network traffic analyzer
9  * By Gerald Combs <gerald@ethereal.com>
10  * Copyright 1998 Gerald Combs
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 #ifdef HAVE_SYS_TYPES_H
32 # include <sys/types.h>
33 #endif
34
35 #include <stdio.h>
36 #include <string.h>
37 #include <glib.h>
38 #include "bitswap.h"
39 #include "packet.h"
40 #include "packet-fddi.h"
41 #include "packet-llc.h"
42 #include "resolv.h"
43
44 static int proto_fddi = -1;
45 static int hf_fddi_fc = -1;
46 static int hf_fddi_fc_clf = -1;
47 static int hf_fddi_fc_prio = -1;
48 static int hf_fddi_fc_smt_subtype = -1;
49 static int hf_fddi_fc_mac_subtype = -1;
50 static int hf_fddi_dst = -1;
51 static int hf_fddi_src = -1;
52 static int hf_fddi_addr = -1;
53
54 static gint ett_fddi = -1;
55 static gint ett_fddi_fc = -1;
56
57 /* FDDI Frame Control values */
58
59 #define FDDI_FC_VOID            0x00            /* Void frame */
60 #define FDDI_FC_NRT             0x80            /* Nonrestricted token */
61 #define FDDI_FC_RT              0xc0            /* Restricted token */
62 #define FDDI_FC_MAC             0xc0            /* MAC frame */
63 #define FDDI_FC_SMT             0x40            /* SMT frame */
64 #define FDDI_FC_SMT_INFO        0x41            /* SMT Info */
65 #define FDDI_FC_SMT_NSA         0x4F            /* SMT Next station adrs */
66 #define FDDI_FC_SMT_MIN         FDDI_FC_SMT_INFO
67 #define FDDI_FC_SMT_MAX         FDDI_FC_SMT_NSA
68 #define FDDI_FC_MAC_MIN         0xc1
69 #define FDDI_FC_MAC_BEACON      0xc2            /* MAC Beacon frame */
70 #define FDDI_FC_MAC_CLAIM       0xc3            /* MAC Claim frame */
71 #define FDDI_FC_MAC_MAX         0xcf
72 #define FDDI_FC_LLC_ASYNC       0x50            /* Async. LLC frame */
73 #define FDDI_FC_LLC_ASYNC_MIN   FDDI_FC_LLC_ASYNC
74 #define FDDI_FC_LLC_ASYNC_DEF   0x54
75 #define FDDI_FC_LLC_ASYNC_MAX   0x5f
76 #define FDDI_FC_LLC_SYNC        0xd0            /* Sync. LLC frame */
77 #define FDDI_FC_LLC_SYNC_MIN    FDDI_FC_LLC_SYNC
78 #define FDDI_FC_LLC_SYNC_MAX    0xd7
79 #define FDDI_FC_IMP_ASYNC       0x60            /* Implementor Async. */
80 #define FDDI_FC_IMP_ASYNC_MIN   FDDI_FC_IMP_ASYNC
81 #define FDDI_FC_IMP_ASYNC_MAX   0x6f
82 #define FDDI_FC_IMP_SYNC        0xe0            /* Implementor Synch. */
83
84 #define FDDI_FC_CLFF            0xF0            /* Class/Length/Format bits */
85 #define FDDI_FC_ZZZZ            0x0F            /* Control bits */
86
87 /*
88  * Async frame ZZZZ bits:
89  */
90 #define FDDI_FC_ASYNC_R         0x08            /* Reserved */
91 #define FDDI_FC_ASYNC_PRI       0x07            /* Priority */
92
93 #define CLFF_BITS(fc)   (((fc) & FDDI_FC_CLFF) >> 4)
94 #define ZZZZ_BITS(fc)   ((fc) & FDDI_FC_ZZZZ)
95
96 static const value_string clf_vals[] = {
97         { CLFF_BITS(FDDI_FC_VOID),      "Void" },
98         { CLFF_BITS(FDDI_FC_SMT),       "SMT" },
99         { CLFF_BITS(FDDI_FC_LLC_ASYNC), "Async LLC" },
100         { CLFF_BITS(FDDI_FC_IMP_ASYNC), "Implementor Async" },
101         { CLFF_BITS(FDDI_FC_NRT),       "Nonrestricted Token" },
102         { CLFF_BITS(FDDI_FC_MAC),       "MAC" },
103         { CLFF_BITS(FDDI_FC_LLC_SYNC),  "Sync LLC" },
104         { CLFF_BITS(FDDI_FC_IMP_SYNC),  "Implementor Sync" },
105         { 0,                            NULL }
106 };
107
108 static const value_string smt_subtype_vals[] = {
109         { ZZZZ_BITS(FDDI_FC_SMT_INFO), "Info" },
110         { ZZZZ_BITS(FDDI_FC_SMT_NSA),  "Next Station Address" },
111         { 0,                           NULL }
112 };
113
114 static const value_string mac_subtype_vals[] = {
115         { ZZZZ_BITS(FDDI_FC_MAC_BEACON), "Beacon" },
116         { ZZZZ_BITS(FDDI_FC_MAC_CLAIM),  "Claim" },
117         { 0,                             NULL }
118 };
119
120 #define FDDI_HEADER_SIZE        13
121
122 /* field positions */
123
124 #define FDDI_P_FC               0
125 #define FDDI_P_DHOST            1
126 #define FDDI_P_SHOST            7
127
128 static dissector_handle_t llc_handle;
129
130 static void
131 swap_mac_addr(u_char *swapped_addr, const u_char *orig_addr)
132 {
133         int i;
134
135         for (i = 0; i < 6; i++) {
136                 swapped_addr[i] = BIT_SWAP(orig_addr[i]);
137         }
138 }
139
140
141 void
142 capture_fddi(const u_char *pd, int len, packet_counts *ld)
143 {
144   int        offset = 0, fc;
145
146   if (!BYTES_ARE_IN_FRAME(0, len, FDDI_HEADER_SIZE)) {
147     ld->other++;
148     return;
149   }
150   offset = FDDI_HEADER_SIZE;
151
152   fc = (int) pd[FDDI_P_FC];
153
154   switch (fc) {
155
156     /* From now, only 802.2 SNAP (Async. LCC frame) is supported */
157
158     case FDDI_FC_LLC_ASYNC + 0  :
159     case FDDI_FC_LLC_ASYNC + 1  :
160     case FDDI_FC_LLC_ASYNC + 2  :
161     case FDDI_FC_LLC_ASYNC + 3  :
162     case FDDI_FC_LLC_ASYNC + 4  :
163     case FDDI_FC_LLC_ASYNC + 5  :
164     case FDDI_FC_LLC_ASYNC + 6  :
165     case FDDI_FC_LLC_ASYNC + 7  :
166     case FDDI_FC_LLC_ASYNC + 8  :
167     case FDDI_FC_LLC_ASYNC + 9  :
168     case FDDI_FC_LLC_ASYNC + 10 :
169     case FDDI_FC_LLC_ASYNC + 11 :
170     case FDDI_FC_LLC_ASYNC + 12 :
171     case FDDI_FC_LLC_ASYNC + 13 :
172     case FDDI_FC_LLC_ASYNC + 14 :
173     case FDDI_FC_LLC_ASYNC + 15 :
174       capture_llc(pd, offset, len, ld);
175       return;
176     default :
177       ld->other++;
178       return;
179
180   } /* fc */
181
182 } /* capture_fddi */
183
184 static gchar *
185 fddifc_to_str(int fc)
186 {
187   static gchar strbuf[128+1];
188
189   switch (fc) {
190
191   case FDDI_FC_VOID:                    /* Void frame */
192     return "Void frame";
193
194   case FDDI_FC_NRT:                     /* Nonrestricted token */
195     return "Nonrestricted token";
196
197   case FDDI_FC_RT:                      /* Restricted token */
198     return "Restricted token";
199
200   case FDDI_FC_SMT_INFO:                /* SMT Info */
201     return "SMT info";
202
203   case FDDI_FC_SMT_NSA:                 /* SMT Next station adrs */
204     return "SMT Next station address";
205
206   case FDDI_FC_MAC_BEACON:              /* MAC Beacon frame */
207     return "MAC beacon";
208
209   case FDDI_FC_MAC_CLAIM:               /* MAC Claim frame */
210     return "MAC claim token";
211
212   default:
213     switch (fc & FDDI_FC_CLFF) {
214
215     case FDDI_FC_MAC:
216       sprintf(strbuf, "MAC frame, control %x", fc & FDDI_FC_ZZZZ);
217       return strbuf;
218
219     case FDDI_FC_SMT:
220       sprintf(strbuf, "SMT frame, control %x", fc & FDDI_FC_ZZZZ);
221       return strbuf;
222
223     case FDDI_FC_LLC_ASYNC:
224       if (fc & FDDI_FC_ASYNC_R)
225         sprintf(strbuf, "Async LLC frame, control %x", fc & FDDI_FC_ZZZZ);
226       else
227         sprintf(strbuf, "Async LLC frame, priority %d",
228                         fc & FDDI_FC_ASYNC_PRI);
229       return strbuf;
230
231     case FDDI_FC_LLC_SYNC:
232       if (fc & FDDI_FC_ZZZZ) {
233         sprintf(strbuf, "Sync LLC frame, control %x", fc & FDDI_FC_ZZZZ);
234         return strbuf;
235       } else
236         return "Sync LLC frame";
237
238     case FDDI_FC_IMP_ASYNC:
239       sprintf(strbuf, "Implementor async frame, control %x",
240                         fc & FDDI_FC_ZZZZ);
241       return strbuf;
242
243     case FDDI_FC_IMP_SYNC:
244       sprintf(strbuf, "Implementor sync frame, control %x",
245                         fc & FDDI_FC_ZZZZ);
246       return strbuf;
247       break;
248
249     default:
250       return "Unknown frame type";
251     }
252   }
253 }
254
255
256 static void
257 dissect_fddi(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
258                 gboolean bitswapped)
259 {
260   int        fc;
261   proto_tree *fh_tree = NULL;
262   proto_item *ti;
263   gchar      *fc_str;
264   proto_tree *fc_tree;
265   static u_char src[6], dst[6];
266   u_char     src_swapped[6], dst_swapped[6];
267   tvbuff_t   *next_tvb;
268
269   if (check_col(pinfo->fd, COL_PROTOCOL))
270     col_set_str(pinfo->fd, COL_PROTOCOL, "FDDI");
271
272   fc = (int) tvb_get_guint8(tvb, FDDI_P_FC);
273   fc_str = fddifc_to_str(fc);
274
275   if (check_col(pinfo->fd, COL_INFO))
276     col_add_str(pinfo->fd, COL_INFO, fc_str);
277
278   if (tree) {
279     ti = proto_tree_add_protocol_format(tree, proto_fddi, tvb, 0, FDDI_HEADER_SIZE,
280                 "Fiber Distributed Data Interface, %s", fc_str);
281     fh_tree = proto_item_add_subtree(ti, ett_fddi);
282     ti = proto_tree_add_uint_format(fh_tree, hf_fddi_fc, tvb, FDDI_P_FC, 1, fc,
283         "Frame Control: 0x%02x (%s)", fc, fc_str);
284     fc_tree = proto_item_add_subtree(ti, ett_fddi_fc);
285     proto_tree_add_uint(fc_tree, hf_fddi_fc_clf, tvb, FDDI_P_FC, 1, fc);
286     switch (fc & FDDI_FC_CLFF) {
287
288     case FDDI_FC_SMT:
289       proto_tree_add_uint(fc_tree, hf_fddi_fc_smt_subtype, tvb, FDDI_P_FC, 1, fc);
290       break;
291
292     case FDDI_FC_MAC:
293       if (fc != FDDI_FC_RT)
294         proto_tree_add_uint(fc_tree, hf_fddi_fc_mac_subtype, tvb, FDDI_P_FC, 1, fc);
295       break;
296
297     case FDDI_FC_LLC_ASYNC:
298       if (!(fc & FDDI_FC_ASYNC_R))
299         proto_tree_add_uint(fc_tree, hf_fddi_fc_prio, tvb, FDDI_P_FC, 1, fc);
300       break;
301     }
302   }
303
304   /* Extract the destination address, possibly bit-swapping it. */
305   if (bitswapped)
306     swap_mac_addr(dst, (u_char *) tvb_get_ptr(tvb, FDDI_P_DHOST, 6));
307   else
308     memcpy(dst, (u_char *) tvb_get_ptr(tvb, FDDI_P_DHOST, 6), sizeof dst);
309   swap_mac_addr(dst_swapped, (u_char*) tvb_get_ptr(tvb, FDDI_P_DHOST, 6));
310
311   /* XXX - copy them to some buffer associated with "pi", rather than
312      just making "dst" static? */
313   SET_ADDRESS(&pinfo->dl_dst, AT_ETHER, 6, &dst[0]);
314   SET_ADDRESS(&pinfo->dst, AT_ETHER, 6, &dst[0]);
315
316   if (fh_tree) {
317     proto_tree_add_ether(fh_tree, hf_fddi_dst, tvb, FDDI_P_DHOST, 6, dst);
318     proto_tree_add_ether_hidden(fh_tree, hf_fddi_addr, tvb, FDDI_P_DHOST, 6, dst);
319
320     /* hide some bit-swapped mac address fields in the proto_tree, just in case */
321     proto_tree_add_ether_hidden(fh_tree, hf_fddi_dst, tvb, FDDI_P_DHOST, 6, dst_swapped);
322     proto_tree_add_ether_hidden(fh_tree, hf_fddi_addr, tvb, FDDI_P_DHOST, 6, dst_swapped);
323   }
324
325   /* Extract the source address, possibly bit-swapping it. */
326   if (bitswapped)
327     swap_mac_addr(src, (u_char *) tvb_get_ptr(tvb, FDDI_P_SHOST, 6));
328   else
329     memcpy(src, (u_char *) tvb_get_ptr(tvb, FDDI_P_SHOST, 6), sizeof src);
330   swap_mac_addr(src_swapped, (u_char*) tvb_get_ptr(tvb, FDDI_P_SHOST, 6));
331
332   /* XXX - copy them to some buffer associated with "pi", rather than
333      just making "src" static? */
334   SET_ADDRESS(&pinfo->dl_src, AT_ETHER, 6, &src[0]);
335   SET_ADDRESS(&pinfo->src, AT_ETHER, 6, &src[0]);
336
337   if (fh_tree) {
338       proto_tree_add_ether(fh_tree, hf_fddi_src, tvb, FDDI_P_SHOST, 6, src);
339       proto_tree_add_ether_hidden(fh_tree, hf_fddi_addr, tvb, FDDI_P_SHOST, 6, src);
340
341       /* hide some bit-swapped mac address fields in the proto_tree, just in case */
342       proto_tree_add_ether_hidden(fh_tree, hf_fddi_src, tvb, FDDI_P_SHOST, 6, src_swapped);
343       proto_tree_add_ether_hidden(fh_tree, hf_fddi_addr, tvb, FDDI_P_SHOST, 6, src_swapped);
344   }
345
346   next_tvb = tvb_new_subset(tvb, FDDI_HEADER_SIZE, -1, -1);
347
348   switch (fc) {
349
350     /* From now, only 802.2 SNAP (Async. LCC frame) is supported */
351
352     case FDDI_FC_LLC_ASYNC + 0  :
353     case FDDI_FC_LLC_ASYNC + 1  :
354     case FDDI_FC_LLC_ASYNC + 2  :
355     case FDDI_FC_LLC_ASYNC + 3  :
356     case FDDI_FC_LLC_ASYNC + 4  :
357     case FDDI_FC_LLC_ASYNC + 5  :
358     case FDDI_FC_LLC_ASYNC + 6  :
359     case FDDI_FC_LLC_ASYNC + 7  :
360     case FDDI_FC_LLC_ASYNC + 8  :
361     case FDDI_FC_LLC_ASYNC + 9  :
362     case FDDI_FC_LLC_ASYNC + 10 :
363     case FDDI_FC_LLC_ASYNC + 11 :
364     case FDDI_FC_LLC_ASYNC + 12 :
365     case FDDI_FC_LLC_ASYNC + 13 :
366     case FDDI_FC_LLC_ASYNC + 14 :
367     case FDDI_FC_LLC_ASYNC + 15 :
368       call_dissector(llc_handle, next_tvb, pinfo, tree);
369       return;
370       
371     default :
372       dissect_data(next_tvb, 0, pinfo, tree);
373       return;
374
375   } /* fc */
376 } /* dissect_fddi */
377
378         
379 static void
380 dissect_fddi_bitswapped(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
381 {
382         dissect_fddi(tvb, pinfo, tree, TRUE);
383 }
384
385 static void
386 dissect_fddi_not_bitswapped(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
387 {
388         dissect_fddi(tvb, pinfo, tree, FALSE);
389 }
390
391 void
392 proto_register_fddi(void)
393 {
394         static hf_register_info hf[] = {
395
396                 /*
397                  * XXX - we want this guy to have his own private formatting
398                  * routine, using "fc_to_str()"; if "fc_to_str()" returns
399                  * NULL, just show the hex value, else show the string.
400                  */
401                 { &hf_fddi_fc,
402                 { "Frame Control",      "fddi.fc", FT_UINT8, BASE_HEX, NULL, 0x0,
403                         "", HFILL }},
404
405                 { &hf_fddi_fc_clf,
406                 { "Class/Length/Format", "fddi.fc.clf", FT_UINT8, BASE_HEX, VALS(clf_vals), FDDI_FC_CLFF,
407                         "", HFILL }},
408
409                 { &hf_fddi_fc_prio,
410                 { "Priority", "fddi.fc.prio", FT_UINT8, BASE_DEC, NULL, FDDI_FC_ASYNC_PRI,
411                         "", HFILL }},
412
413                 { &hf_fddi_fc_smt_subtype,
414                 { "SMT Subtype", "fddi.fc.smt_subtype", FT_UINT8, BASE_DEC, VALS(smt_subtype_vals), FDDI_FC_ZZZZ,
415                         "", HFILL }},
416
417                 { &hf_fddi_fc_mac_subtype,
418                 { "MAC Subtype", "fddi.fc.mac_subtype", FT_UINT8, BASE_DEC, VALS(mac_subtype_vals), FDDI_FC_ZZZZ,
419                         "", HFILL }},
420
421                 { &hf_fddi_dst,
422                 { "Destination",        "fddi.dst", FT_ETHER, BASE_NONE, NULL, 0x0,
423                         "Destination Hardware Address", HFILL }},
424
425                 { &hf_fddi_src,
426                 { "Source",             "fddi.src", FT_ETHER, BASE_NONE, NULL, 0x0,
427                         "", HFILL }},
428
429                 { &hf_fddi_addr,
430                 { "Source or Destination Address", "fddi.addr", FT_ETHER, BASE_NONE, NULL, 0x0,
431                         "Source or Destination Hardware Address", HFILL }},
432
433         };
434         static gint *ett[] = {
435                 &ett_fddi,
436                 &ett_fddi_fc,
437         };
438
439         proto_fddi = proto_register_protocol("Fiber Distributed Data Interface",
440             "FDDI", "fddi");
441         proto_register_field_array(proto_fddi, hf, array_length(hf));
442         proto_register_subtree_array(ett, array_length(ett));
443
444         /*
445          * Called from various dissectors for encapsulated FDDI frames.
446          * We assume the MAC addresses in them aren't bitswapped.
447          */
448         register_dissector("fddi", dissect_fddi_not_bitswapped, proto_fddi);
449 }
450
451 void
452 proto_reg_handoff_fddi(void)
453 {
454         /*
455          * Get a handle for the LLC dissector.
456          */
457         llc_handle = find_dissector("llc");
458
459         dissector_add("wtap_encap", WTAP_ENCAP_FDDI,
460             dissect_fddi_not_bitswapped, proto_fddi);
461         dissector_add("wtap_encap", WTAP_ENCAP_FDDI_BITSWAPPED,
462             dissect_fddi_bitswapped, proto_fddi);
463 }