Have "proto_register_protocol()" build a list of data structures for
[obnox/wireshark/wip.git] / packet-llc.c
1 /* packet-llc.c
2  * Routines for IEEE 802.2 LLC layer
3  * Gilbert Ramirez <gram@xiexie.org>
4  *
5  * $Id: packet-llc.c,v 1.74 2001/01/03 06:55:29 guy Exp $
6  *
7  * Ethereal - Network traffic analyzer
8  * By Gerald Combs <gerald@zing.org>
9  * Copyright 1998 Gerald Combs
10  *
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 <glib.h>
36 #include "packet.h"
37 #include "oui.h"
38 #include "xdlc.h"
39 #include "etypes.h"
40 #include "llcsaps.h"
41 #include "packet-ip.h"
42 #include "packet-ipx.h"
43 #include "packet-netbios.h"
44 #include "packet-osi.h"
45 #include "packet-sna.h"
46
47 static int proto_llc = -1;
48 static int hf_llc_dsap = -1;
49 static int hf_llc_ssap = -1;
50 static int hf_llc_dsap_ig = -1;
51 static int hf_llc_ssap_cr = -1;
52 static int hf_llc_ctrl = -1;
53 static int hf_llc_type = -1;
54 static int hf_llc_oui = -1;
55 static int hf_llc_pid = -1;
56
57 static gint ett_llc = -1;
58 static gint ett_llc_ctrl = -1;
59
60 static dissector_table_t subdissector_table;
61 static dissector_table_t cisco_subdissector_table;
62
63 static dissector_handle_t bpdu_handle;
64
65 typedef void (capture_func_t)(const u_char *, int, packet_counts *);
66
67 /* The SAP info is split into two tables, one value_string table and one
68  * table of sap_info. This is so that the value_string can be used in the
69  * header field registration.
70  */
71 struct sap_info {
72         guint8  sap;
73         capture_func_t *capture_func;
74 };
75
76 /*
77  * Group/Individual bit, in the DSAP.
78  */
79 #define DSAP_GI_BIT     0x01
80
81 /*
82  * Command/Response bit, in the SSAP.
83  *
84  * The low-order bit of the SSAP apparently determines whether this
85  * is a request or a response.  (RFC 1390, "Transmission of IP and
86  * ARP over FDDI Networks", says
87  *
88  *      Command frames are identified by having the low order
89  *      bit of the SSAP address reset to zero.  Response frames
90  *      have the low order bit of the SSAP address set to one.
91  *
92  * and a page I've seen seems to imply that's part of 802.2.)
93  */
94 #define SSAP_CR_BIT     0x01
95
96 /*
97  * Mask to extrace the SAP number from the DSAP or the SSAP.
98  */
99 #define SAP_MASK        0xFE
100
101 /*
102  * These are for SSAP and DSAP, wth last bit always zero.
103  * XXX - some DSAPs come in separate "individual" and "group" versions,
104  * with the last bit 0 and 1, respectively (e.g., LLC Sub-layer Management,
105  * IBM SNA Path Control, IBM Net Management), but, whilst 0xFE is
106  * the ISO Network Layer Protocol, 0xFF is the Global LSAP.
107  */
108 static const value_string sap_vals[] = {
109         { SAP_NULL,           "NULL LSAP" },
110         { SAP_LLC_SLMGMT,     "LLC Sub-Layer Management" },
111         { SAP_SNA_PATHCTRL,   "SNA Path Control" },
112         { SAP_IP,             "TCP/IP" },
113         { SAP_SNA1,           "SNA" },
114         { SAP_SNA2,           "SNA" },
115         { SAP_PROWAY_NM_INIT, "PROWAY (IEC955) Network Management and Initialization" },
116         { SAP_TI,             "Texas Instruments" },
117         { SAP_BPDU,           "Spanning Tree BPDU" },
118         { SAP_RS511,          "EIA RS-511 Manufacturing Message Service" },
119 #if 0
120         /* XXX - setting the group bit makes this 0x7F; is that just
121            a group version of this? */
122         { 0x7E,               "ISO 8208 (X.25 over 802.2 Type 2)" },
123 #endif
124         { 0x7F,               "ISO 802.2" },
125         { SAP_XNS,            "XNS" },
126         { SAP_NESTAR,         "Nestar" },
127         { SAP_PROWAY_ASLM,    "PROWAY (IEC955) Active Station List Maintenance" },
128         { SAP_ARP,            "ARP" },  /* XXX - hand to "dissect_arp()"? */
129         { SAP_SNAP,           "SNAP" },
130         { SAP_VINES1,         "Banyan Vines" },
131         { SAP_VINES2,         "Banyan Vines" },
132         { SAP_NETWARE,        "NetWare" },
133         { SAP_NETBIOS,        "NetBIOS" },
134         { SAP_IBMNM,          "IBM Net Management" },
135         { SAP_RPL1,           "Remote Program Load" },
136         { SAP_UB,             "Ungermann-Bass" },
137         { SAP_RPL2,           "Remote Program Load" },
138         { SAP_OSINL,          "ISO Network Layer" },
139         { SAP_GLOBAL,         "Global LSAP" },
140         { 0x00,               NULL }
141 };
142
143 static struct sap_info  saps[] = {
144         { SAP_IP,                       capture_ip },
145         { SAP_NETWARE,                  capture_ipx },
146         { SAP_NETBIOS,                  capture_netbios },
147         { 0x00,                         NULL}
148 };
149
150 /*
151  * See
152  *
153  * http://www.cisco.com/univercd/cc/td/doc/product/lan/trsrb/vlan.htm
154  *
155  * for the PIDs for VTP and DRiP that go with an OUI of OUI_CISCO.
156  */
157 const value_string oui_vals[] = {
158         { OUI_ENCAP_ETHER, "Encapsulated Ethernet" },
159 /*
160 http://www.cisco.com/univercd/cc/td/doc/product/software/ios113ed/113ed_cr/ibm_r/brprt1/brsrb.htm
161 */
162         { OUI_CISCO,       "Cisco" },
163         { OUI_CISCO_90,    "Cisco IOS 9.0 Compatible" },
164         { OUI_BFR,         "Bridged Frame-Relay" }, /* RFC 2427 */
165         { OUI_ATM_FORUM,   "ATM Forum" },
166         { OUI_APPLE_ATALK, "Apple (AppleTalk)" },
167         { OUI_CABLE_BPDU,  "DOCSIS Spanning Tree" }, /* DOCSIS spanning tree BPDU */
168         { 0,               NULL }
169 };
170
171 static capture_func_t *
172 sap_capture_func(u_char sap) {
173         int i=0;
174
175         /* look for the second record where sap == 0, which should
176          * be the last record
177          */
178         while (saps[i].sap > 0 || i == 0) {
179                 if (saps[i].sap == sap) {
180                         return saps[i].capture_func;
181                 }
182                 i++;
183         }
184         return NULL;
185 }
186
187 void
188 capture_llc(const u_char *pd, int offset, packet_counts *ld) {
189
190         int             is_snap;
191         guint16         control;
192         int             llc_header_len;
193         guint32         oui;
194         guint16         etype;
195         capture_func_t  *capture;
196
197         if (!BYTES_ARE_IN_FRAME(offset, 2)) {
198                 ld->other++;
199                 return;
200         }
201         is_snap = (pd[offset] == SAP_SNAP) && (pd[offset+1] == SAP_SNAP);
202         llc_header_len = 2;     /* DSAP + SSAP */
203
204         /*
205          * XXX - the page referred to in the comment above about the
206          * Command/Response bit also implies that LLC Type 2 always
207          * uses extended operation, so we don't need to determine
208          * whether it's basic or extended operation; is that the case?
209          */
210         control = get_xdlc_control(pd, offset+2, pd[offset+1] & SSAP_CR_BIT,
211             TRUE);
212         llc_header_len += XDLC_CONTROL_LEN(control, TRUE);
213         if (is_snap)
214                 llc_header_len += 5;    /* 3 bytes of OUI, 2 bytes of protocol ID */
215         if (!BYTES_ARE_IN_FRAME(offset, llc_header_len)) {
216                 ld->other++;
217                 return;
218         }
219
220         if (is_snap) {
221                 oui = pd[offset+3] << 16 | pd[offset+4] << 8 | pd[offset+5];
222                 if (XDLC_IS_INFORMATION(control)) {
223                         etype = pntohs(&pd[offset+6]);
224                         switch (oui) {
225
226                         case OUI_ENCAP_ETHER:
227                         case OUI_APPLE_ATALK:
228                                 /* No, I have no idea why Apple used
229                                    one of their own OUIs, rather than
230                                    OUI_ENCAP_ETHER, and an Ethernet
231                                    packet type as protocol ID, for
232                                    AppleTalk data packets - but used
233                                    OUI_ENCAP_ETHER and an Ethernet
234                                    packet type for AARP packets. */
235                                 capture_ethertype(etype, offset+8, pd,
236                                     ld);
237                                 break;
238                         case OUI_CISCO:
239                                 capture_ethertype(etype,
240                                                 offset + 8, pd, ld);
241                                 break;
242                         default:
243                                 ld->other++;
244                                 break;
245                         }
246                 }
247         }               
248         else {
249                 if (XDLC_IS_INFORMATION(control)) {
250                         capture = sap_capture_func(pd[offset]);
251
252                         /* non-SNAP */
253                         offset += llc_header_len;
254
255                         if (capture) {
256                                 capture(pd, offset, ld);
257                         }
258                         else {
259                                 ld->other++;
260                         }
261                 }
262         }
263 }
264
265 void
266 dissect_llc(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
267 {
268         proto_tree      *llc_tree = NULL;
269         proto_item      *ti = NULL;
270         int             is_snap;
271         guint16         control;
272         int             llc_header_len;
273         guint32         oui;
274         guint16         etype;
275         guint8          dsap, ssap;
276         tvbuff_t        *next_tvb;
277         const guint8    *pd;
278         int             offset;
279
280         CHECK_DISPLAY_AS_DATA(proto_llc, tvb, pinfo, tree);
281     
282         pinfo->current_proto = "LLC";
283
284         if (check_col(pinfo->fd, COL_PROTOCOL)) {
285                 col_set_str(pinfo->fd, COL_PROTOCOL, "LLC");
286         }
287
288         dsap = tvb_get_guint8(tvb, 0);
289         if (tree) {
290                 ti = proto_tree_add_item(tree, proto_llc, tvb, 0, 0, FALSE);
291                 llc_tree = proto_item_add_subtree(ti, ett_llc);
292                 proto_tree_add_uint(llc_tree, hf_llc_dsap, tvb, 0, 
293                         1, dsap & SAP_MASK);
294                 proto_tree_add_boolean(llc_tree, hf_llc_dsap_ig, tvb, 0, 
295                         1, dsap & DSAP_GI_BIT);
296         } else
297                 llc_tree = NULL;
298
299         ssap = tvb_get_guint8(tvb, 1);
300         if (tree) {
301                 proto_tree_add_uint(llc_tree, hf_llc_ssap, tvb, 1, 
302                         1, ssap & SAP_MASK);
303                 proto_tree_add_boolean(llc_tree, hf_llc_ssap_cr, tvb, 1, 
304                         1, ssap & SSAP_CR_BIT);
305         } else
306                 llc_tree = NULL;
307
308         is_snap = (dsap == SAP_SNAP) && (ssap == SAP_SNAP);
309         llc_header_len = 2;     /* DSAP + SSAP */
310
311         /*
312          * XXX - the page referred to in the comment above about the
313          * Command/Response bit also implies that LLC Type 2 always
314          * uses extended operation, so we don't need to determine
315          * whether it's basic or extended operation; is that the case?
316          */
317         control = dissect_xdlc_control(tvb, 2, pinfo, llc_tree,
318                                 hf_llc_ctrl, ett_llc_ctrl,
319                                 ssap & SSAP_CR_BIT, TRUE);
320         llc_header_len += XDLC_CONTROL_LEN(control, TRUE);
321         if (is_snap)
322                 llc_header_len += 5;    /* 3 bytes of OUI, 2 bytes of protocol ID */
323
324         if (tree)
325                 proto_item_set_len(ti, llc_header_len);
326
327         if (is_snap) {
328                 oui =   tvb_get_ntoh24(tvb, 3);
329                 etype = tvb_get_ntohs(tvb, 6);
330
331                 if (check_col(pinfo->fd, COL_INFO)) {
332                         col_append_fstr(pinfo->fd, COL_INFO, "; SNAP, OUI 0x%06X (%s), PID 0x%04X",
333                             oui, val_to_str(oui, oui_vals, "Unknown"),
334                             etype);
335                 }
336                 if (tree) {
337                         proto_tree_add_uint(llc_tree, hf_llc_oui, tvb, 3, 3,
338                                 oui);
339                 }
340
341                 next_tvb = tvb_new_subset(tvb, 8, -1, -1);
342                 tvb_compat(next_tvb, &pd, &offset);
343
344                 switch (oui) {
345
346                 case OUI_ENCAP_ETHER:
347                 case OUI_APPLE_ATALK:
348                         /* No, I have no idea why Apple used
349                            one of their own OUIs, rather than
350                            OUI_ENCAP_ETHER, and an Ethernet
351                            packet type as protocol ID, for
352                            AppleTalk data packets - but used
353                            OUI_ENCAP_ETHER and an Ethernet
354                            packet type for AARP packets. */
355                         if (XDLC_IS_INFORMATION(control)) {
356                                 ethertype(etype, tvb, 8,
357                                     pinfo, tree, llc_tree, hf_llc_type);
358                         } else
359                                 dissect_data(next_tvb, 0, pinfo, tree);
360                         break;
361
362                 case OUI_CISCO:
363                         /* So are all CDP packets LLC packets
364                            with an OUI of OUI_CISCO and a
365                            protocol ID of 0x2000, or
366                            are some of them raw or encapsulated
367                            Ethernet? */
368                         if (tree) {
369                                 proto_tree_add_uint(llc_tree,
370                                     hf_llc_pid, tvb, 6, 2, etype);
371                         }
372                         if (XDLC_IS_INFORMATION(control)) {
373                                 /* do lookup with the subdissector table */
374                                 /* for future reference, 0x0102 is Cisco DRIP */
375                                 if (!dissector_try_port(cisco_subdissector_table,
376                                     etype, next_tvb, pinfo, tree))
377                                         dissect_data(next_tvb, 0, pinfo, tree);
378                         } else
379                                 dissect_data(next_tvb, 0, pinfo, tree);
380                         break;
381
382                 case OUI_CABLE_BPDU:    /* DOCSIS cable modem spanning tree BPDU */
383                         if (tree) {
384                                 proto_tree_add_uint(llc_tree,
385                                     hf_llc_pid, tvb, 6, 2, etype);
386                         }
387                         call_dissector(bpdu_handle, next_tvb, pinfo, tree);
388                         break;
389
390                 default:
391                         if (tree) {
392                                 proto_tree_add_uint(llc_tree,
393                                     hf_llc_pid, tvb, 6, 2, etype);
394                         }
395                         dissect_data(next_tvb, 0, pinfo, tree);
396                         break;
397                 }
398         }
399         else {
400                 if (check_col(pinfo->fd, COL_INFO)) {
401                         col_append_fstr(pinfo->fd, COL_INFO, 
402                             "; DSAP %s %s, SSAP %s %s",
403                             val_to_str(dsap & SAP_MASK, sap_vals, "%02x"),
404                             dsap & DSAP_GI_BIT ?
405                               "Group" : "Individual",
406                             val_to_str(ssap & SAP_MASK, sap_vals, "%02x"),
407                             ssap & SSAP_CR_BIT ?
408                               "Response" : "Command"
409                         );
410                 }
411
412                 next_tvb = tvb_new_subset(tvb, llc_header_len, -1, -1);
413                 if (XDLC_IS_INFORMATION(control)) {
414                         /* non-SNAP */
415                         /* do lookup with the subdissector table */
416                         if (!dissector_try_port(subdissector_table, dsap,
417                             next_tvb, pinfo, tree)) {
418                                 dissect_data(next_tvb, 0, pinfo, tree);
419                         }
420                 } else {
421                         dissect_data(next_tvb, 0, pinfo, tree);
422                 }
423         }
424 }
425
426 void
427 proto_register_llc(void)
428 {
429         static struct true_false_string ig_bit = { "Group", "Individual" };
430         static struct true_false_string cr_bit = { "Response", "Command" };
431
432         static hf_register_info hf[] = {
433                 { &hf_llc_dsap,
434                 { "DSAP",       "llc.dsap", FT_UINT8, BASE_HEX, 
435                         VALS(sap_vals), 0x0, "" }},
436
437                 { &hf_llc_dsap_ig,
438                 { "IG Bit",     "llc.dsap.ig", FT_BOOLEAN, BASE_HEX, 
439                         &ig_bit, 0x0, "Individual/Group" }},
440
441                 { &hf_llc_ssap,
442                 { "SSAP", "llc.ssap", FT_UINT8, BASE_HEX, 
443                         VALS(sap_vals), 0x0, "" }},
444
445                 { &hf_llc_ssap_cr,
446                 { "CR Bit", "llc.ssap.cr", FT_BOOLEAN, BASE_HEX, 
447                         &cr_bit, 0x0, "Command/Response" }},
448
449                 { &hf_llc_ctrl,
450                 { "Control", "llc.control", FT_UINT16, BASE_HEX, 
451                         NULL, 0x0, "" }},
452
453                 /* registered here but handled in ethertype.c */
454                 { &hf_llc_type,
455                 { "Type", "llc.type", FT_UINT16, BASE_HEX, 
456                         VALS(etype_vals), 0x0, "" }},
457
458                 { &hf_llc_oui,
459                 { "Organization Code",  "llc.oui", FT_UINT24, BASE_HEX, 
460                         VALS(oui_vals), 0x0, ""}},
461
462                 { &hf_llc_pid,
463                 { "Protocol ID", "llc.pid", FT_UINT16, BASE_HEX, 
464                         NULL, 0x0, ""}}
465         };
466         static gint *ett[] = {
467                 &ett_llc,
468                 &ett_llc_ctrl,
469         };
470
471         proto_llc = proto_register_protocol("Logical-Link Control", "LLC", "llc");
472         proto_register_field_array(proto_llc, hf, array_length(hf));
473         proto_register_subtree_array(ett, array_length(ett));
474
475 /* subdissector code */
476         subdissector_table = register_dissector_table("llc.dsap");
477         cisco_subdissector_table = register_dissector_table("llc.cisco_pid");
478 }
479
480 void
481 proto_reg_handoff_llc(void)
482 {
483         /*
484          * Get a handle for the BPDU dissector.
485          */
486         bpdu_handle = find_dissector("bpdu");
487
488         dissector_add("wtap_encap", WTAP_ENCAP_ATM_RFC1483, dissect_llc);
489 }