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