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