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