Trim leading and trailing white space from the capture device in the
[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.98 2002/05/29 03:08:01 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 #ifdef HAVE_SYS_TYPES_H
31 # include <sys/types.h>
32 #endif
33
34 #include <glib.h>
35 #include <epan/packet.h>
36 #include "oui.h"
37 #include "xdlc.h"
38 #include "etypes.h"
39 #include "llcsaps.h"
40 #include "bridged_pids.h"
41 #include "ppptypes.h"
42 #include "packet-ip.h"
43 #include "packet-ipx.h"
44 #include "packet-netbios.h"
45 #include <epan/sna-utils.h>
46
47 #include "packet-llc.h"
48
49 #define UDP_PORT_LLC1   12000
50 #define UDP_PORT_LLC2   12001
51 #define UDP_PORT_LLC3   12002
52 #define UDP_PORT_LLC4   12003
53 #define UDP_PORT_LLC5   12004
54
55 static int proto_llc = -1;
56 static int hf_llc_dsap = -1;
57 static int hf_llc_ssap = -1;
58 static int hf_llc_dsap_ig = -1;
59 static int hf_llc_ssap_cr = -1;
60 static int hf_llc_ctrl = -1;
61 static int hf_llc_type = -1;
62 static int hf_llc_oui = -1;
63 static int hf_llc_pid = -1;
64
65 static gint ett_llc = -1;
66 static gint ett_llc_ctrl = -1;
67
68 static dissector_table_t subdissector_table;
69 static dissector_table_t cisco_subdissector_table;
70
71 static dissector_handle_t bpdu_handle;
72 static dissector_handle_t eth_handle;
73 static dissector_handle_t fddi_handle;
74 static dissector_handle_t tr_handle;
75 static dissector_handle_t data_handle;
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         { SAP_X25,            "ISO 8208 (X.25 over 802.2)" },
121         /*
122          * XXX - setting the group bit of SAP_X25 make 0x7F; is this just
123          * a group version of that?
124          */
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 /*
145  * See
146  *
147  * http://www.cisco.com/univercd/cc/td/doc/product/lan/trsrb/vlan.htm
148  *
149  * for the PIDs for VTP and DRiP that go with an OUI of OUI_CISCO.
150  */
151 const value_string oui_vals[] = {
152         { OUI_ENCAP_ETHER, "Encapsulated Ethernet" },
153 /*
154 http://www.cisco.com/univercd/cc/td/doc/product/software/ios113ed/113ed_cr/ibm_r/brprt1/brsrb.htm
155 */
156         { OUI_CISCO,       "Cisco" },
157         { OUI_CISCO_90,    "Cisco IOS 9.0 Compatible" },
158         { OUI_BRIDGED,     "Frame Relay or ATM bridged frames" },
159                                 /* RFC 2427, RFC 2684 */
160         { OUI_ATM_FORUM,   "ATM Forum" },
161         { OUI_CABLE_BPDU,  "DOCSIS Spanning Tree" }, /* DOCSIS spanning tree BPDU */
162         { OUI_APPLE_ATALK, "Apple (AppleTalk)" },
163         { 0,               NULL }
164 };
165
166 void
167 capture_llc(const u_char *pd, int offset, int len, packet_counts *ld) {
168
169         int             is_snap;
170         guint16         control;
171         int             llc_header_len;
172         guint32         oui;
173         guint16         etype;
174
175         if (!BYTES_ARE_IN_FRAME(offset, len, 2)) {
176                 ld->other++;
177                 return;
178         }
179         is_snap = (pd[offset] == SAP_SNAP) && (pd[offset+1] == SAP_SNAP);
180         llc_header_len = 2;     /* DSAP + SSAP */
181
182         /*
183          * XXX - the page referred to in the comment above about the
184          * Command/Response bit also implies that LLC Type 2 always
185          * uses extended operation, so we don't need to determine
186          * whether it's basic or extended operation; is that the case?
187          */
188         control = get_xdlc_control(pd, offset+2, pd[offset+1] & SSAP_CR_BIT);
189         llc_header_len += XDLC_CONTROL_LEN(control, TRUE);
190         if (is_snap)
191                 llc_header_len += 5;    /* 3 bytes of OUI, 2 bytes of protocol ID */
192         if (!BYTES_ARE_IN_FRAME(offset, len, llc_header_len)) {
193                 ld->other++;
194                 return;
195         }
196
197         if (is_snap) {
198                 oui = pd[offset+3] << 16 | pd[offset+4] << 8 | pd[offset+5];
199                 if (XDLC_IS_INFORMATION(control)) {
200                         etype = pntohs(&pd[offset+6]);
201                         switch (oui) {
202
203                         case OUI_ENCAP_ETHER:
204                         case OUI_CISCO_90:
205                         case OUI_APPLE_ATALK:
206                                 /* No, I have no idea why Apple used
207                                    one of their own OUIs, rather than
208                                    OUI_ENCAP_ETHER, and an Ethernet
209                                    packet type as protocol ID, for
210                                    AppleTalk data packets - but used
211                                    OUI_ENCAP_ETHER and an Ethernet
212                                    packet type for AARP packets. */
213                                 capture_ethertype(etype, pd, offset+8, len,
214                                     ld);
215                                 break;
216                         case OUI_CISCO:
217                                 capture_ethertype(etype, pd, offset + 8, len,
218                                     ld);
219                                 break;
220                         default:
221                                 ld->other++;
222                                 break;
223                         }
224                 }
225         }               
226         else {
227                 /* non-SNAP */
228                 if (XDLC_IS_INFORMATION(control)) {
229                         switch (pd[offset]) {
230
231                         case SAP_IP:
232                                 capture_ip(pd, offset + llc_header_len, len,
233                                     ld);
234                                 break;
235
236                         case SAP_NETWARE:
237                                 capture_ipx(ld);
238                                 break;
239
240                         case SAP_NETBIOS:
241                                 capture_netbios(ld);
242                                 break;
243
244                         default:
245                                 ld->other++;
246                                 break;
247                         }
248                 }
249         }
250 }
251
252 static void
253 dissect_llc(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
254 {
255         proto_tree      *llc_tree = NULL;
256         proto_item      *ti = NULL;
257         int             is_snap;
258         guint16         control;
259         int             llc_header_len;
260         guint8          dsap, ssap;
261         tvbuff_t        *next_tvb;
262
263         if (check_col(pinfo->cinfo, COL_PROTOCOL)) {
264                 col_set_str(pinfo->cinfo, COL_PROTOCOL, "LLC");
265         }
266         if (check_col(pinfo->cinfo, COL_INFO)) {
267                 col_clear(pinfo->cinfo, COL_INFO);
268         }
269
270         dsap = tvb_get_guint8(tvb, 0);
271         if (tree) {
272                 ti = proto_tree_add_item(tree, proto_llc, tvb, 0, -1, FALSE);
273                 llc_tree = proto_item_add_subtree(ti, ett_llc);
274                 proto_tree_add_uint(llc_tree, hf_llc_dsap, tvb, 0, 
275                         1, dsap & SAP_MASK);
276                 proto_tree_add_boolean(llc_tree, hf_llc_dsap_ig, tvb, 0, 
277                         1, dsap & DSAP_GI_BIT);
278         } else
279                 llc_tree = NULL;
280
281         ssap = tvb_get_guint8(tvb, 1);
282         if (tree) {
283                 proto_tree_add_uint(llc_tree, hf_llc_ssap, tvb, 1, 
284                         1, ssap & SAP_MASK);
285                 proto_tree_add_boolean(llc_tree, hf_llc_ssap_cr, tvb, 1, 
286                         1, ssap & SSAP_CR_BIT);
287         } else
288                 llc_tree = NULL;
289
290         is_snap = (dsap == SAP_SNAP) && (ssap == SAP_SNAP);
291         llc_header_len = 2;     /* DSAP + SSAP */
292
293         /*
294          * XXX - the page referred to in the comment above about the
295          * Command/Response bit also implies that LLC Type 2 always
296          * uses extended operation, so we don't need to determine
297          * whether it's basic or extended operation; is that the case?
298          */
299         control = dissect_xdlc_control(tvb, 2, pinfo, llc_tree,
300                                 hf_llc_ctrl, ett_llc_ctrl,
301                                 ssap & SSAP_CR_BIT, TRUE);
302         llc_header_len += XDLC_CONTROL_LEN(control, TRUE);
303         if (is_snap)
304                 llc_header_len += 5;    /* 3 bytes of OUI, 2 bytes of protocol ID */
305
306         if (tree)
307                 proto_item_set_len(ti, llc_header_len);
308
309         if (is_snap) {
310                 dissect_snap(tvb, 3, pinfo, tree, llc_tree, control,
311                     hf_llc_oui, hf_llc_type, hf_llc_pid, 2);
312         }
313         else {
314                 if (check_col(pinfo->cinfo, COL_INFO)) {
315                         col_append_fstr(pinfo->cinfo, COL_INFO, 
316                             "; DSAP %s %s, SSAP %s %s",
317                             val_to_str(dsap & SAP_MASK, sap_vals, "%02x"),
318                             dsap & DSAP_GI_BIT ?
319                               "Group" : "Individual",
320                             val_to_str(ssap & SAP_MASK, sap_vals, "%02x"),
321                             ssap & SSAP_CR_BIT ?
322                               "Response" : "Command"
323                         );
324                 }
325
326                 next_tvb = tvb_new_subset(tvb, llc_header_len, -1, -1);
327                 if (XDLC_IS_INFORMATION(control)) {
328                         /* non-SNAP */
329                         /* do lookup with the subdissector table */
330                         if (!dissector_try_port(subdissector_table, dsap,
331                             next_tvb, pinfo, tree)) {
332                                 call_dissector(data_handle,next_tvb, pinfo, tree);
333                         }
334                 } else {
335                         call_dissector(data_handle,next_tvb, pinfo, tree);
336                 }
337         }
338 }
339
340 /*
341  * Dissect SNAP header; used elsewhere, e.g. in the Frame Relay dissector.
342  */
343 void
344 dissect_snap(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree,
345     proto_tree *snap_tree, int control, int hf_oui, int hf_type, int hf_pid,
346     int bridge_pad)
347 {
348         guint32         oui;
349         guint16         etype;
350         tvbuff_t        *next_tvb;
351
352         oui =   tvb_get_ntoh24(tvb, offset);
353         etype = tvb_get_ntohs(tvb, offset+3);
354
355         if (check_col(pinfo->cinfo, COL_INFO)) {
356                 col_append_fstr(pinfo->cinfo, COL_INFO,
357                     "; SNAP, OUI 0x%06X (%s), PID 0x%04X",
358                     oui, val_to_str(oui, oui_vals, "Unknown"), etype);
359         }
360         if (tree) {
361                 proto_tree_add_uint(snap_tree, hf_oui, tvb, offset, 3, oui);
362         }
363
364         switch (oui) {
365
366         case OUI_ENCAP_ETHER:
367         case OUI_CISCO_90:
368         case OUI_APPLE_ATALK:
369                 /* No, I have no idea why Apple used
370                    one of their own OUIs, rather than
371                    OUI_ENCAP_ETHER, and an Ethernet
372                    packet type as protocol ID, for
373                    AppleTalk data packets - but used
374                    OUI_ENCAP_ETHER and an Ethernet
375                    packet type for AARP packets. */
376                 if (XDLC_IS_INFORMATION(control)) {
377                         ethertype(etype, tvb, offset+5,
378                             pinfo, tree, snap_tree, hf_type, -1);
379                 } else {
380                         next_tvb = tvb_new_subset(tvb, offset+5, -1, -1);
381                         call_dissector(data_handle,next_tvb, pinfo, tree);
382                 }
383                 break;
384
385         case OUI_BRIDGED:
386                 /*
387                  * MAC frames bridged over ATM (RFC 2684) or Frame Relay
388                  * (RFC 2427).
389                  *
390                  * We have to figure out how much padding to put
391                  * into the frame.  We were handed a "bridge_pad"
392                  * argument which should be 0 for Frame Relay and
393                  * 2 for ATM; we add to that the amount of padding
394                  * common to both bridging types.
395                  */
396                 if (tree) {
397                         proto_tree_add_uint(snap_tree, hf_pid, tvb, offset+3, 2,
398                             etype);
399                 }
400
401                 switch (etype) {
402
403                 case BPID_ETH_WITH_FCS:
404                 case BPID_ETH_WITHOUT_FCS:
405                         next_tvb = tvb_new_subset(tvb, offset+5+bridge_pad,
406                             -1, -1);
407                         call_dissector(eth_handle, next_tvb, pinfo, tree);
408                         break;
409
410                 case BPID_802_5_WITH_FCS:
411                 case BPID_802_5_WITHOUT_FCS:
412                         /*
413                          * We treat the last padding byte as the Access
414                          * Control byte, as that's what the Token
415                          * Ring dissector expects the first byte to
416                          * be.
417                          */
418                         next_tvb = tvb_new_subset(tvb, offset+5+bridge_pad,
419                             -1, -1);
420                         call_dissector(tr_handle, next_tvb, pinfo, tree);
421                         break;
422
423                 case BPID_FDDI_WITH_FCS:
424                 case BPID_FDDI_WITHOUT_FCS:
425                         next_tvb = tvb_new_subset(tvb, offset+5+1+bridge_pad,
426                             -1, -1);
427                         call_dissector(fddi_handle, next_tvb, pinfo, tree);
428                         break;
429
430                 case BPID_BPDU:
431                         next_tvb = tvb_new_subset(tvb, offset+5, -1, -1);
432                         call_dissector(bpdu_handle, next_tvb, pinfo, tree);
433                         break;
434
435                 default:
436                         next_tvb = tvb_new_subset(tvb, offset+5, -1, -1);
437                         call_dissector(data_handle,next_tvb, pinfo, tree);
438                         break;
439                 }
440                 break;
441                 
442         case OUI_CISCO:
443                 /* So are all CDP packets LLC packets
444                    with an OUI of OUI_CISCO and a
445                    protocol ID of 0x2000, or
446                    are some of them raw or encapsulated
447                    Ethernet? */
448                 if (tree) {
449                         proto_tree_add_uint(snap_tree, hf_pid, tvb, offset+3, 2,
450                             etype);
451                 }
452                 next_tvb = tvb_new_subset(tvb, offset+5, -1, -1);
453                 if (XDLC_IS_INFORMATION(control)) {
454                         /* do lookup with the subdissector table */
455                         /* for future reference, 0x0102 is Cisco DRIP */
456                         if (!dissector_try_port(cisco_subdissector_table,
457                             etype, next_tvb, pinfo, tree))
458                                 call_dissector(data_handle,next_tvb, pinfo, tree);
459                 } else
460                         call_dissector(data_handle,next_tvb, pinfo, tree);
461                 break;
462
463         case OUI_CABLE_BPDU:    /* DOCSIS cable modem spanning tree BPDU */
464                 if (tree) {
465                         proto_tree_add_uint(snap_tree, hf_pid, tvb, offset+3, 2,
466                             etype);
467                 }
468                 next_tvb = tvb_new_subset(tvb, offset+5, -1, -1);
469                 call_dissector(bpdu_handle, next_tvb, pinfo, tree);
470                 break;
471
472         default:
473                 if (tree) {
474                         proto_tree_add_uint(snap_tree, hf_pid, tvb, offset+3, 2,
475                             etype);
476                 }
477                 next_tvb = tvb_new_subset(tvb, offset+5, -1, -1);
478                 call_dissector(data_handle,next_tvb, pinfo, tree);
479                 break;
480         }
481 }
482
483 void
484 proto_register_llc(void)
485 {
486         static struct true_false_string ig_bit = { "Group", "Individual" };
487         static struct true_false_string cr_bit = { "Response", "Command" };
488
489         static hf_register_info hf[] = {
490                 { &hf_llc_dsap,
491                 { "DSAP",       "llc.dsap", FT_UINT8, BASE_HEX, 
492                         VALS(sap_vals), 0x0, "", HFILL }},
493
494                 { &hf_llc_dsap_ig,
495                 { "IG Bit",     "llc.dsap.ig", FT_BOOLEAN, BASE_HEX, 
496                         &ig_bit, 0x0, "Individual/Group", HFILL }},
497
498                 { &hf_llc_ssap,
499                 { "SSAP", "llc.ssap", FT_UINT8, BASE_HEX, 
500                         VALS(sap_vals), 0x0, "", HFILL }},
501
502                 { &hf_llc_ssap_cr,
503                 { "CR Bit", "llc.ssap.cr", FT_BOOLEAN, BASE_HEX, 
504                         &cr_bit, 0x0, "Command/Response", HFILL }},
505
506                 { &hf_llc_ctrl,
507                 { "Control", "llc.control", FT_UINT16, BASE_HEX, 
508                         NULL, 0x0, "", HFILL }},
509
510                 /* registered here but handled in ethertype.c */
511                 { &hf_llc_type,
512                 { "Type", "llc.type", FT_UINT16, BASE_HEX, 
513                         VALS(etype_vals), 0x0, "", HFILL }},
514
515                 { &hf_llc_oui,
516                 { "Organization Code",  "llc.oui", FT_UINT24, BASE_HEX, 
517                         VALS(oui_vals), 0x0, "", HFILL }},
518
519                 { &hf_llc_pid,
520                 { "Protocol ID", "llc.pid", FT_UINT16, BASE_HEX, 
521                         NULL, 0x0, "", HFILL }}
522         };
523         static gint *ett[] = {
524                 &ett_llc,
525                 &ett_llc_ctrl,
526         };
527
528         proto_llc = proto_register_protocol("Logical-Link Control", "LLC", "llc");
529         proto_register_field_array(proto_llc, hf, array_length(hf));
530         proto_register_subtree_array(ett, array_length(ett));
531
532 /* subdissector code */
533         subdissector_table = register_dissector_table("llc.dsap",
534           "LLC SAP", FT_UINT8, BASE_HEX);
535         cisco_subdissector_table = register_dissector_table("llc.cisco_pid",
536           "Cisco OUI PID", FT_UINT16, BASE_HEX);
537
538         register_dissector("llc", dissect_llc, proto_llc);
539 }
540
541 void
542 proto_reg_handoff_llc(void)
543 {
544         dissector_handle_t llc_handle;
545
546         /*
547          * Get handles for the BPDU, Ethernet, FDDI, and Token Ring
548          * dissectors.
549          */
550         bpdu_handle = find_dissector("bpdu");
551         eth_handle = find_dissector("eth");
552         fddi_handle = find_dissector("fddi");
553         tr_handle = find_dissector("tr");
554         data_handle = find_dissector("data");
555
556         llc_handle = find_dissector("llc");
557         dissector_add("wtap_encap", WTAP_ENCAP_ATM_RFC1483, llc_handle);
558         /* RFC 2043 */
559         dissector_add("ppp.protocol", PPP_LLC, llc_handle);
560         /* RFC 2353 */
561         dissector_add("udp.port", UDP_PORT_LLC1, llc_handle);
562         dissector_add("udp.port", UDP_PORT_LLC2, llc_handle);
563         dissector_add("udp.port", UDP_PORT_LLC3, llc_handle);
564         dissector_add("udp.port", UDP_PORT_LLC4, llc_handle);
565         dissector_add("udp.port", UDP_PORT_LLC5, llc_handle);
566 }