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