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