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