Use "proto_tree_add_item()" to add the URI string to the protocol tree,
[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.56 2002/01/21 07:36:34 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 <epan/bitswap.h>
39 #include <epan/packet.h>
40 #include "packet-fddi.h"
41 #include "packet-llc.h"
42 #include <epan/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 static dissector_handle_t data_handle;
130
131 static void
132 swap_mac_addr(u_char *swapped_addr, const u_char *orig_addr)
133 {
134         int i;
135
136         for (i = 0; i < 6; i++) {
137                 swapped_addr[i] = BIT_SWAP(orig_addr[i]);
138         }
139 }
140
141
142 void
143 capture_fddi(const u_char *pd, int len, packet_counts *ld)
144 {
145   int        offset = 0, fc;
146
147   if (!BYTES_ARE_IN_FRAME(0, len, FDDI_HEADER_SIZE)) {
148     ld->other++;
149     return;
150   }
151   offset = FDDI_HEADER_SIZE;
152
153   fc = (int) pd[FDDI_P_FC];
154
155   switch (fc) {
156
157     /* From now, only 802.2 SNAP (Async. LCC frame) is supported */
158
159     case FDDI_FC_LLC_ASYNC + 0  :
160     case FDDI_FC_LLC_ASYNC + 1  :
161     case FDDI_FC_LLC_ASYNC + 2  :
162     case FDDI_FC_LLC_ASYNC + 3  :
163     case FDDI_FC_LLC_ASYNC + 4  :
164     case FDDI_FC_LLC_ASYNC + 5  :
165     case FDDI_FC_LLC_ASYNC + 6  :
166     case FDDI_FC_LLC_ASYNC + 7  :
167     case FDDI_FC_LLC_ASYNC + 8  :
168     case FDDI_FC_LLC_ASYNC + 9  :
169     case FDDI_FC_LLC_ASYNC + 10 :
170     case FDDI_FC_LLC_ASYNC + 11 :
171     case FDDI_FC_LLC_ASYNC + 12 :
172     case FDDI_FC_LLC_ASYNC + 13 :
173     case FDDI_FC_LLC_ASYNC + 14 :
174     case FDDI_FC_LLC_ASYNC + 15 :
175       capture_llc(pd, offset, len, ld);
176       return;
177     default :
178       ld->other++;
179       return;
180
181   } /* fc */
182
183 } /* capture_fddi */
184
185 static gchar *
186 fddifc_to_str(int fc)
187 {
188   static gchar strbuf[128+1];
189
190   switch (fc) {
191
192   case FDDI_FC_VOID:                    /* Void frame */
193     return "Void frame";
194
195   case FDDI_FC_NRT:                     /* Nonrestricted token */
196     return "Nonrestricted token";
197
198   case FDDI_FC_RT:                      /* Restricted token */
199     return "Restricted token";
200
201   case FDDI_FC_SMT_INFO:                /* SMT Info */
202     return "SMT info";
203
204   case FDDI_FC_SMT_NSA:                 /* SMT Next station adrs */
205     return "SMT Next station address";
206
207   case FDDI_FC_MAC_BEACON:              /* MAC Beacon frame */
208     return "MAC beacon";
209
210   case FDDI_FC_MAC_CLAIM:               /* MAC Claim frame */
211     return "MAC claim token";
212
213   default:
214     switch (fc & FDDI_FC_CLFF) {
215
216     case FDDI_FC_MAC:
217       sprintf(strbuf, "MAC frame, control %x", fc & FDDI_FC_ZZZZ);
218       return strbuf;
219
220     case FDDI_FC_SMT:
221       sprintf(strbuf, "SMT frame, control %x", fc & FDDI_FC_ZZZZ);
222       return strbuf;
223
224     case FDDI_FC_LLC_ASYNC:
225       if (fc & FDDI_FC_ASYNC_R)
226         sprintf(strbuf, "Async LLC frame, control %x", fc & FDDI_FC_ZZZZ);
227       else
228         sprintf(strbuf, "Async LLC frame, priority %d",
229                         fc & FDDI_FC_ASYNC_PRI);
230       return strbuf;
231
232     case FDDI_FC_LLC_SYNC:
233       if (fc & FDDI_FC_ZZZZ) {
234         sprintf(strbuf, "Sync LLC frame, control %x", fc & FDDI_FC_ZZZZ);
235         return strbuf;
236       } else
237         return "Sync LLC frame";
238
239     case FDDI_FC_IMP_ASYNC:
240       sprintf(strbuf, "Implementor async frame, control %x",
241                         fc & FDDI_FC_ZZZZ);
242       return strbuf;
243
244     case FDDI_FC_IMP_SYNC:
245       sprintf(strbuf, "Implementor sync frame, control %x",
246                         fc & FDDI_FC_ZZZZ);
247       return strbuf;
248       break;
249
250     default:
251       return "Unknown frame type";
252     }
253   }
254 }
255
256
257 static void
258 dissect_fddi(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
259                 gboolean bitswapped)
260 {
261   int        fc;
262   proto_tree *fh_tree = NULL;
263   proto_item *ti;
264   gchar      *fc_str;
265   proto_tree *fc_tree;
266   static u_char src[6], dst[6];
267   u_char     src_swapped[6], dst_swapped[6];
268   tvbuff_t   *next_tvb;
269
270   if (check_col(pinfo->cinfo, COL_PROTOCOL))
271     col_set_str(pinfo->cinfo, COL_PROTOCOL, "FDDI");
272
273   fc = (int) tvb_get_guint8(tvb, FDDI_P_FC);
274   fc_str = fddifc_to_str(fc);
275
276   if (check_col(pinfo->cinfo, COL_INFO))
277     col_add_str(pinfo->cinfo, COL_INFO, fc_str);
278
279   if (tree) {
280     ti = proto_tree_add_protocol_format(tree, proto_fddi, tvb, 0, FDDI_HEADER_SIZE,
281                 "Fiber Distributed Data Interface, %s", fc_str);
282     fh_tree = proto_item_add_subtree(ti, ett_fddi);
283     ti = proto_tree_add_uint_format(fh_tree, hf_fddi_fc, tvb, FDDI_P_FC, 1, fc,
284         "Frame Control: 0x%02x (%s)", fc, fc_str);
285     fc_tree = proto_item_add_subtree(ti, ett_fddi_fc);
286     proto_tree_add_uint(fc_tree, hf_fddi_fc_clf, tvb, FDDI_P_FC, 1, fc);
287     switch (fc & FDDI_FC_CLFF) {
288
289     case FDDI_FC_SMT:
290       proto_tree_add_uint(fc_tree, hf_fddi_fc_smt_subtype, tvb, FDDI_P_FC, 1, fc);
291       break;
292
293     case FDDI_FC_MAC:
294       if (fc != FDDI_FC_RT)
295         proto_tree_add_uint(fc_tree, hf_fddi_fc_mac_subtype, tvb, FDDI_P_FC, 1, fc);
296       break;
297
298     case FDDI_FC_LLC_ASYNC:
299       if (!(fc & FDDI_FC_ASYNC_R))
300         proto_tree_add_uint(fc_tree, hf_fddi_fc_prio, tvb, FDDI_P_FC, 1, fc);
301       break;
302     }
303   }
304
305   /* Extract the destination address, possibly bit-swapping it. */
306   if (bitswapped)
307     swap_mac_addr(dst, (u_char *) tvb_get_ptr(tvb, FDDI_P_DHOST, 6));
308   else
309     memcpy(dst, (u_char *) tvb_get_ptr(tvb, FDDI_P_DHOST, 6), sizeof dst);
310   swap_mac_addr(dst_swapped, (u_char*) tvb_get_ptr(tvb, FDDI_P_DHOST, 6));
311
312   /* XXX - copy them to some buffer associated with "pi", rather than
313      just making "dst" static? */
314   SET_ADDRESS(&pinfo->dl_dst, AT_ETHER, 6, &dst[0]);
315   SET_ADDRESS(&pinfo->dst, AT_ETHER, 6, &dst[0]);
316
317   if (fh_tree) {
318     proto_tree_add_ether(fh_tree, hf_fddi_dst, tvb, FDDI_P_DHOST, 6, dst);
319     proto_tree_add_ether_hidden(fh_tree, hf_fddi_addr, tvb, FDDI_P_DHOST, 6, dst);
320
321     /* hide some bit-swapped mac address fields in the proto_tree, just in case */
322     proto_tree_add_ether_hidden(fh_tree, hf_fddi_dst, tvb, FDDI_P_DHOST, 6, dst_swapped);
323     proto_tree_add_ether_hidden(fh_tree, hf_fddi_addr, tvb, FDDI_P_DHOST, 6, dst_swapped);
324   }
325
326   /* Extract the source address, possibly bit-swapping it. */
327   if (bitswapped)
328     swap_mac_addr(src, (u_char *) tvb_get_ptr(tvb, FDDI_P_SHOST, 6));
329   else
330     memcpy(src, (u_char *) tvb_get_ptr(tvb, FDDI_P_SHOST, 6), sizeof src);
331   swap_mac_addr(src_swapped, (u_char*) tvb_get_ptr(tvb, FDDI_P_SHOST, 6));
332
333   /* XXX - copy them to some buffer associated with "pi", rather than
334      just making "src" static? */
335   SET_ADDRESS(&pinfo->dl_src, AT_ETHER, 6, &src[0]);
336   SET_ADDRESS(&pinfo->src, AT_ETHER, 6, &src[0]);
337
338   if (fh_tree) {
339       proto_tree_add_ether(fh_tree, hf_fddi_src, tvb, FDDI_P_SHOST, 6, src);
340       proto_tree_add_ether_hidden(fh_tree, hf_fddi_addr, tvb, FDDI_P_SHOST, 6, src);
341
342       /* hide some bit-swapped mac address fields in the proto_tree, just in case */
343       proto_tree_add_ether_hidden(fh_tree, hf_fddi_src, tvb, FDDI_P_SHOST, 6, src_swapped);
344       proto_tree_add_ether_hidden(fh_tree, hf_fddi_addr, tvb, FDDI_P_SHOST, 6, src_swapped);
345   }
346
347   next_tvb = tvb_new_subset(tvb, FDDI_HEADER_SIZE, -1, -1);
348
349   switch (fc) {
350
351     /* From now, only 802.2 SNAP (Async. LCC frame) is supported */
352
353     case FDDI_FC_LLC_ASYNC + 0  :
354     case FDDI_FC_LLC_ASYNC + 1  :
355     case FDDI_FC_LLC_ASYNC + 2  :
356     case FDDI_FC_LLC_ASYNC + 3  :
357     case FDDI_FC_LLC_ASYNC + 4  :
358     case FDDI_FC_LLC_ASYNC + 5  :
359     case FDDI_FC_LLC_ASYNC + 6  :
360     case FDDI_FC_LLC_ASYNC + 7  :
361     case FDDI_FC_LLC_ASYNC + 8  :
362     case FDDI_FC_LLC_ASYNC + 9  :
363     case FDDI_FC_LLC_ASYNC + 10 :
364     case FDDI_FC_LLC_ASYNC + 11 :
365     case FDDI_FC_LLC_ASYNC + 12 :
366     case FDDI_FC_LLC_ASYNC + 13 :
367     case FDDI_FC_LLC_ASYNC + 14 :
368     case FDDI_FC_LLC_ASYNC + 15 :
369       call_dissector(llc_handle, next_tvb, pinfo, tree);
370       return;
371       
372     default :
373       call_dissector(data_handle,next_tvb, pinfo, tree);
374       return;
375
376   } /* fc */
377 } /* dissect_fddi */
378
379         
380 static void
381 dissect_fddi_bitswapped(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
382 {
383         dissect_fddi(tvb, pinfo, tree, TRUE);
384 }
385
386 static void
387 dissect_fddi_not_bitswapped(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
388 {
389         dissect_fddi(tvb, pinfo, tree, FALSE);
390 }
391
392 void
393 proto_register_fddi(void)
394 {
395         static hf_register_info hf[] = {
396
397                 /*
398                  * XXX - we want this guy to have his own private formatting
399                  * routine, using "fc_to_str()"; if "fc_to_str()" returns
400                  * NULL, just show the hex value, else show the string.
401                  */
402                 { &hf_fddi_fc,
403                 { "Frame Control",      "fddi.fc", FT_UINT8, BASE_HEX, NULL, 0x0,
404                         "", HFILL }},
405
406                 { &hf_fddi_fc_clf,
407                 { "Class/Length/Format", "fddi.fc.clf", FT_UINT8, BASE_HEX, VALS(clf_vals), FDDI_FC_CLFF,
408                         "", HFILL }},
409
410                 { &hf_fddi_fc_prio,
411                 { "Priority", "fddi.fc.prio", FT_UINT8, BASE_DEC, NULL, FDDI_FC_ASYNC_PRI,
412                         "", HFILL }},
413
414                 { &hf_fddi_fc_smt_subtype,
415                 { "SMT Subtype", "fddi.fc.smt_subtype", FT_UINT8, BASE_DEC, VALS(smt_subtype_vals), FDDI_FC_ZZZZ,
416                         "", HFILL }},
417
418                 { &hf_fddi_fc_mac_subtype,
419                 { "MAC Subtype", "fddi.fc.mac_subtype", FT_UINT8, BASE_DEC, VALS(mac_subtype_vals), FDDI_FC_ZZZZ,
420                         "", HFILL }},
421
422                 { &hf_fddi_dst,
423                 { "Destination",        "fddi.dst", FT_ETHER, BASE_NONE, NULL, 0x0,
424                         "Destination Hardware Address", HFILL }},
425
426                 { &hf_fddi_src,
427                 { "Source",             "fddi.src", FT_ETHER, BASE_NONE, NULL, 0x0,
428                         "", HFILL }},
429
430                 { &hf_fddi_addr,
431                 { "Source or Destination Address", "fddi.addr", FT_ETHER, BASE_NONE, NULL, 0x0,
432                         "Source or Destination Hardware Address", HFILL }},
433
434         };
435         static gint *ett[] = {
436                 &ett_fddi,
437                 &ett_fddi_fc,
438         };
439
440         proto_fddi = proto_register_protocol("Fiber Distributed Data Interface",
441             "FDDI", "fddi");
442         proto_register_field_array(proto_fddi, hf, array_length(hf));
443         proto_register_subtree_array(ett, array_length(ett));
444
445         /*
446          * Called from various dissectors for encapsulated FDDI frames.
447          * We assume the MAC addresses in them aren't bitswapped.
448          */
449         register_dissector("fddi", dissect_fddi_not_bitswapped, proto_fddi);
450 }
451
452 void
453 proto_reg_handoff_fddi(void)
454 {
455         dissector_handle_t fddi_handle, fddi_bitswapped_handle;
456
457         /*
458          * Get a handle for the LLC dissector.
459          */
460         llc_handle = find_dissector("llc");
461         data_handle = find_dissector("data");
462
463         fddi_handle = find_dissector("fddi");
464         dissector_add("wtap_encap", WTAP_ENCAP_FDDI, fddi_handle);
465         fddi_bitswapped_handle =
466             create_dissector_handle(dissect_fddi_bitswapped, proto_fddi);
467         dissector_add("wtap_encap", WTAP_ENCAP_FDDI_BITSWAPPED,
468             fddi_bitswapped_handle);
469 }