Include the "-D" flag in the usage message.
[obnox/wireshark/wip.git] / packet-llc.c
1 /* packet-llc.c
2  * Routines for IEEE 802.2 LLC layer
3  * Gilbert Ramirez <gramirez@tivoli.com>
4  *
5  * $Id: packet-llc.c,v 1.44 2000/01/24 02:44:52 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         
41 static int proto_llc = -1;
42 static int hf_llc_dsap = -1;
43 static int hf_llc_ssap = -1;
44 static int hf_llc_dsap_ig = -1;
45 static int hf_llc_ssap_cr = -1;
46 static int hf_llc_ctrl = -1;
47 static int hf_llc_type = -1;
48 static int hf_llc_oui = -1;
49 static int hf_llc_pid = -1;
50
51 static gint ett_llc = -1;
52 static gint ett_llc_ctrl = -1;
53
54 typedef void (capture_func_t)(const u_char *, int, packet_counts *);
55 typedef void (dissect_func_t)(const u_char *, int, frame_data *, proto_tree *);
56
57 /* The SAP info is split into two tables, one value_string table and one table of sap_info. This is
58  * so that the value_string can be used in the header field registration.
59  */
60 struct sap_info {
61         guint8  sap;
62         capture_func_t *capture_func;
63         dissect_func_t *dissect_func;
64 };
65
66 /*
67  * Group/Individual bit, in the DSAP.
68  */
69 #define DSAP_GI_BIT     0x01
70
71 /*
72  * Command/Response bit, in the SSAP.
73  *
74  * The low-order bit of the SSAP apparently determines whether this
75  * is a request or a response.  (RFC 1390, "Transmission of IP and
76  * ARP over FDDI Networks", says
77  *
78  *      Command frames are identified by having the low order
79  *      bit of the SSAP address reset to zero.  Response frames
80  *      have the low order bit of the SSAP address set to one.
81  *
82  * and a page I've seen seems to imply that's part of 802.2.)
83  */
84 #define SSAP_CR_BIT     0x01
85
86 /*
87  * Mask to extrace the SAP number from the DSAP or the SSAP.
88  */
89 #define SAP_MASK        0xFE
90
91 #define SAP_SNAP        0xAA
92
93 /*
94  * These are for SSAP and DSAP, wth last bit always zero.
95  * XXX - some DSAPs come in separate "individual" and "group" versions,
96  * with the last bit 0 and 1, respectively (e.g., LLC Sub-layer Management,
97  * IBM SNA Path Control, IBM Net Management), but, whilst 0xFE is
98  * the ISO Network Layer Protocol, 0xFF is the Global LSAP.
99  */
100 static const value_string sap_vals[] = {
101         { 0x00,     "NULL LSAP" },
102         { 0x02,     "LLC Sub-Layer Management" },
103         { 0x04,     "SNA Path Control" },
104         { 0x06,     "TCP/IP" },
105         { 0x08,     "SNA" },
106         { 0x0C,     "SNA" },
107         { 0x0E,     "PROWAY (IEC955) Network Management and Initialization" },
108         { 0x18,     "Texas Instruments" },
109         { 0x42,     "Spanning Tree BPDU" },
110         { 0x4E,     "EIA RS-511 Manufacturing Message Service" },
111 #if 0
112         /* XXX - setting the group bit makes this 0x7F; is that just
113            a group version of this? */
114         { 0x7E,     "ISO 8208 (X.25 over 802.2 Type 2)" },
115 #endif
116         { 0x7F,     "ISO 802.2" },
117         { 0x80,     "XNS" },
118         { 0x86,     "Nestar" },
119         { 0x8E,     "PROWAY (IEC955) Active Station List Maintenance" },
120         { 0x98,     "ARP" },    /* XXX - hand to "dissect_arp()"? */
121         { SAP_SNAP, "SNAP" },
122         { 0xBA,     "Banyan Vines" },
123         { 0xBC,     "Banyan Vines" },
124         { 0xE0,     "NetWare" },
125         { 0xF0,     "NetBIOS" },
126         { 0xF4,     "IBM Net Management" },
127         { 0xF8,     "Remote Program Load" },
128         { 0xFA,     "Ungermann-Bass" },
129         { 0xFC,     "Remote Program Load" },
130         { 0xFE,     "ISO Network Layer" },
131         { 0xFF,     "Global LSAP" },
132         { 0x00,     NULL }
133 };
134
135 static struct sap_info  saps[] = {
136         { 0x00,         NULL,           NULL },
137         { 0x02,         NULL,           NULL },
138         { 0x03,         NULL,           NULL },
139         { 0x04,         NULL,           dissect_sna },
140         { 0x05,         NULL,           NULL },
141         { 0x06,         capture_ip,     dissect_ip },
142         { 0x08,         NULL,           NULL },
143         { 0x0C,         NULL,           NULL },
144         { 0x42,         NULL,           dissect_bpdu },
145         { 0x7F,         NULL,           NULL },
146         { 0x80,         NULL,           NULL },
147         { SAP_SNAP,     NULL,           NULL },
148         { 0xBA,         NULL,           NULL },
149         { 0xBC,         NULL,           NULL },
150         { 0xE0,         capture_ipx,    dissect_ipx },
151         { 0xF0,         capture_netbios, dissect_netbios },
152         { 0xF4,         NULL,           NULL },
153         { 0xF5,         NULL,           NULL },
154         { 0xF8,         NULL,           NULL },
155         { 0xFC,         NULL,           NULL },
156         { 0xFE,         NULL,           dissect_osi },
157         { 0xFF,         NULL,           NULL },
158         { 0x00,         NULL,           NULL}
159 };
160
161 static const value_string llc_ctrl_vals[] = {
162         { 0, "Information Transfer" },
163         { 1, "Supervisory" },
164         { 2, "Unknown" },
165         { 3, "Unnumbered Information" },
166         { 0, NULL }
167 };
168
169 const value_string oui_vals[] = {
170         { OUI_ENCAP_ETHER, "Encapsulated Ethernet" },
171 /*
172 http://www.cisco.com/univercd/cc/td/doc/product/software/ios113ed/113ed_cr/ibm_r/brprt1/brsrb.htm
173 */
174         { OUI_CISCO,       "Cisco" },
175         { OUI_CISCO_90,    "Cisco IOS 9.0 Compatible" },
176         { OUI_BFR,         "Bridged Frame-Relay" }, /* RFC 2427 */
177         { OUI_ATM_FORUM,   "ATM Forum" },
178         { OUI_APPLE_ATALK, "Apple (AppleTalk)" },
179         { 0,               NULL }
180 };
181
182 static capture_func_t *
183 sap_capture_func(u_char sap) {
184         int i=0;
185
186         /* look for the second record where sap == 0, which should
187          * be the last record
188          */
189         while (saps[i].sap > 0 || i == 0) {
190                 if (saps[i].sap == sap) {
191                         return saps[i].capture_func;
192                 }
193                 i++;
194         }
195         return NULL;
196 }
197
198 static dissect_func_t *
199 sap_dissect_func(u_char sap) {
200         int i=0;
201
202         /* look for the second record where sap == 0, which should
203          * be the last record
204          */
205         while (saps[i].sap > 0 || i == 0) {
206                 if (saps[i].sap == sap) {
207                         return saps[i].dissect_func;
208                 }
209                 i++;
210         }
211         return dissect_data;
212 }
213
214 void
215 capture_llc(const u_char *pd, int offset, packet_counts *ld) {
216
217         int             is_snap;
218         guint16         control;
219         int             llc_header_len;
220         guint32         oui;
221         guint16         etype;
222         capture_func_t  *capture;
223
224         if (!BYTES_ARE_IN_FRAME(offset, 2)) {
225                 ld->other++;
226                 return;
227         }
228         is_snap = (pd[offset] == SAP_SNAP) && (pd[offset+1] == SAP_SNAP);
229         llc_header_len = 2;     /* DSAP + SSAP */
230
231         /*
232          * XXX - the page referred to in the comment above about the
233          * Command/Response bit also implies that LLC Type 2 always
234          * uses extended operation, so we don't need to determine
235          * whether it's basic or extended operation; is that the case?
236          */
237         control = get_xdlc_control(pd, offset+2, pd[offset+1] & SSAP_CR_BIT,
238             TRUE);
239         llc_header_len += XDLC_CONTROL_LEN(control, TRUE);
240         if (is_snap)
241                 llc_header_len += 5;    /* 3 bytes of OUI, 2 bytes of protocol ID */
242         if (!BYTES_ARE_IN_FRAME(offset, llc_header_len)) {
243                 ld->other++;
244                 return;
245         }
246
247         if (is_snap) {
248                 oui = pd[offset+3] << 16 | pd[offset+4] << 8 | pd[offset+5];
249                 if (XDLC_IS_INFORMATION(control)) {
250                         etype = pntohs(&pd[offset+6]);
251                         switch (oui) {
252
253                         case OUI_ENCAP_ETHER:
254                         case OUI_APPLE_ATALK:
255                                 /* No, I have no idea why Apple used
256                                    one of their own OUIs, rather than
257                                    OUI_ENCAP_ETHER, and an Ethernet
258                                    packet type as protocol ID, for
259                                    AppleTalk data packets - but used
260                                    OUI_ENCAP_ETHER and an Ethernet
261                                    packet type for AARP packets. */
262                                 capture_ethertype(etype, offset+8, pd,
263                                     ld);
264                                 break;
265                         case OUI_CISCO:
266                                 capture_ethertype(etype,
267                                                 offset + 8, pd, ld);
268                                 break;
269                         default:
270                                 ld->other++;
271                                 break;
272                         }
273                 }
274         }               
275         else {
276                 if (XDLC_IS_INFORMATION(control)) {
277                         capture = sap_capture_func(pd[offset]);
278
279                         /* non-SNAP */
280                         offset += llc_header_len;
281
282                         if (capture) {
283                                 capture(pd, offset, ld);
284                         }
285                         else {
286                                 ld->other++;
287                         }
288                 }
289         }
290 }
291
292 void
293 dissect_llc(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
294
295         proto_tree      *llc_tree = NULL;
296         proto_item      *ti = NULL;
297         int             is_snap;
298         guint16         control;
299         int             llc_header_len;
300         guint32         oui;
301         guint16         etype;
302         dissect_func_t  *dissect;
303
304         if (!BYTES_ARE_IN_FRAME(offset, 2)) {
305                 dissect_data(pd, offset, fd, tree);
306                 return;
307         }
308         is_snap = (pd[offset] == SAP_SNAP) && (pd[offset+1] == SAP_SNAP);
309         llc_header_len = 2;     /* DSAP + SSAP */
310
311         if (check_col(fd, COL_PROTOCOL)) {
312                 col_add_str(fd, COL_PROTOCOL, "LLC");
313         }
314
315         if (tree) {
316                 ti = proto_tree_add_item(tree, proto_llc, offset, 0, NULL);
317                 llc_tree = proto_item_add_subtree(ti, ett_llc);
318                 proto_tree_add_item(llc_tree, hf_llc_dsap, offset, 
319                         1, pd[offset] & SAP_MASK);
320                 proto_tree_add_item(llc_tree, hf_llc_dsap_ig, offset, 
321                         1, pd[offset] & DSAP_GI_BIT);
322                 proto_tree_add_item(llc_tree, hf_llc_ssap, offset+1, 
323                         1, pd[offset+1] & SAP_MASK);
324                 proto_tree_add_item(llc_tree, hf_llc_ssap_cr, offset+1, 
325                         1, pd[offset+1] & SSAP_CR_BIT);
326         } else
327                 llc_tree = NULL;
328
329         /*
330          * XXX - the page referred to in the comment above about the
331          * Command/Response bit also implies that LLC Type 2 always
332          * uses extended operation, so we don't need to determine
333          * whether it's basic or extended operation; is that the case?
334          */
335         control = dissect_xdlc_control(pd, offset+2, fd, llc_tree,
336                                 hf_llc_ctrl, ett_llc_ctrl,
337                                 pd[offset+1] & SSAP_CR_BIT, TRUE);
338         llc_header_len += XDLC_CONTROL_LEN(control, TRUE);
339         if (is_snap)
340                 llc_header_len += 5;    /* 3 bytes of OUI, 2 bytes of protocol ID */
341         if (!BYTES_ARE_IN_FRAME(offset, llc_header_len)) {
342                 dissect_data(pd, offset, fd, tree);
343                 return;
344         }
345         if (tree)
346                 proto_item_set_len(ti, llc_header_len);
347
348         /*
349          * XXX - do we want to append the SAP information to the stuff
350          * "dissect_xdlc_control()" put in the COL_INFO column, rather
351          * than overwriting it?
352          */
353         if (is_snap) {
354                 oui = pd[offset+3] << 16 | pd[offset+4] << 8 | pd[offset+5];
355                 etype = pntohs(&pd[offset+6]);
356                 if (check_col(fd, COL_INFO)) {
357                         col_add_fstr(fd, COL_INFO, "SNAP, OUI 0x%06X (%s), PID 0x%04X",
358                             oui, val_to_str(oui, oui_vals, "Unknown"),
359                             etype);
360                 }
361                 if (tree) {
362                         proto_tree_add_item(llc_tree, hf_llc_oui, offset+3, 3,
363                                 oui);
364                 }
365                 switch (oui) {
366
367                 case OUI_ENCAP_ETHER:
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, offset+8, pd,
378                                     fd, tree, llc_tree, hf_llc_type);
379                         } else
380                                 dissect_data(pd, offset+8, fd, tree);
381                         break;
382
383                 case OUI_CISCO:
384                         /* So are all CDP packets LLC packets
385                            with an OUI of OUI_CISCO and a
386                            protocol ID of 0x2000, or
387                            are some of them raw or encapsulated
388                            Ethernet? */
389                         if (tree) {
390                                 proto_tree_add_item(llc_tree,
391                                     hf_llc_pid, offset+6, 2, etype);
392                         }
393                         if (XDLC_IS_INFORMATION(control)) {
394                                 switch (etype) {
395
396                                 case 0x2000:
397                                         dissect_cdp(pd, offset+8, fd, tree);
398                                         break;
399
400                                 default:
401                                         dissect_data(pd, offset+8, fd, tree);
402                                         break;
403                                 }
404                         } else
405                                 dissect_data(pd, offset+8, fd, tree);
406                         break;
407
408                 default:
409                         if (tree) {
410                                 proto_tree_add_item(llc_tree,
411                                     hf_llc_pid, offset+6, 2, etype);
412                         }
413                         dissect_data(pd, offset+8, fd, tree);
414                         break;
415                 }
416         }               
417         else {
418                 if (check_col(fd, COL_INFO)) {
419                         col_add_fstr(fd, COL_INFO, 
420                             "DSAP %s %s, SSAP %s %s",
421                             val_to_str(pd[offset] & SAP_MASK, sap_vals, "%02x"),
422                             pd[offset] & DSAP_GI_BIT ?
423                               "Group" : "Individual",
424                             val_to_str(pd[offset+1] & SAP_MASK, sap_vals, "%02x"),
425                             pd[offset+1] & SSAP_CR_BIT ?
426                               "Command" : "Response"
427                         );
428                 }
429
430                 if (XDLC_IS_INFORMATION(control)) {
431                         dissect = sap_dissect_func(pd[offset]);
432
433                         /* non-SNAP */
434                         offset += llc_header_len;
435
436                         if (dissect) {
437                                 dissect(pd, offset, fd, tree);
438                         }
439                         else {
440                                 dissect_data(pd, offset, fd, tree);
441                         }
442                 } else
443                         dissect_data(pd, offset, fd, tree);
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_UINT8, BASE_HEX, 
472                         VALS(llc_ctrl_vals), 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 }