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