Christophe Tronche's BPDU dissector.
[metze/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.25 1999/10/08 20:50:38 guy Exp $
6  *
7  * Ethereal - Network traffic analyzer
8  * By Gerald Combs <gerald@unicom.net>
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 "xdlc.h"
38         
39 static int proto_llc = -1;
40 static int hf_llc_dsap = -1;
41 static int hf_llc_ssap = -1;
42 static int hf_llc_ctrl = -1;
43 static int hf_llc_type = -1;
44 static int hf_llc_oui = -1;
45
46 typedef void (capture_func_t)(const u_char *, int, guint32, packet_counts *);
47 typedef void (dissect_func_t)(const u_char *, int, frame_data *, proto_tree *);
48
49 /* The SAP info is split into two tables, one value_string table and one table of sap_info. This is
50  * so that the value_string can be used in the header field registration.
51  */
52 struct sap_info {
53         guint8  sap;
54         capture_func_t *capture_func;
55         dissect_func_t *dissect_func;
56 };
57
58 static const value_string sap_vals[] = {
59         { 0x00, "NULL LSAP" },
60         { 0x02, "LLC Sub-Layer Management Individual" },
61         { 0x03, "LLC Sub-Layer Management Group" },
62         { 0x04, "SNA Path Control Individual" },
63         { 0x05, "SNA Path Control Group" },
64         { 0x06, "TCP/IP" },
65         { 0x08, "SNA" },
66         { 0x0C, "SNA" },
67         { 0x42, "Spanning Tree BPDU" },
68         { 0x7F, "ISO 802.2" },
69         { 0x80, "XNS" },
70         { 0xAA, "SNAP" },
71         { 0xBA, "Banyan Vines" },
72         { 0xBC, "Banyan Vines" },
73         { 0xE0, "NetWare" },
74         { 0xF0, "NetBIOS" },
75         { 0xF4, "IBM Net Management Individual" },
76         { 0xF5, "IBM Net Management Group" },
77         { 0xF8, "Remote Program Load" },
78         { 0xFC, "Remote Program Load" },
79         { 0xFE, "ISO Network Layer" },
80         { 0xFF, "Global LSAP" },
81         { 0x00, NULL }
82 };
83
84 static struct sap_info  saps[] = {
85         { 0x00, NULL,           NULL },
86         { 0x02, NULL,           NULL },
87         { 0x03, NULL,           NULL },
88         { 0x04, NULL,           NULL },
89         { 0x05, NULL,           NULL },
90         { 0x06, capture_ip,     dissect_ip },
91         { 0x08, NULL,           NULL },
92         { 0x0C, NULL,           NULL },
93         { 0x42, NULL,           dissect_bpdu },
94         { 0x7F, NULL,           NULL },
95         { 0x80, NULL,           NULL },
96         { 0xAA, NULL,           NULL },
97         { 0xBA, NULL,           NULL },
98         { 0xBC, NULL,           NULL },
99         { 0xE0, NULL,           dissect_ipx },
100         { 0xF0, capture_netbios, dissect_netbios },
101         { 0xF4, NULL,           NULL },
102         { 0xF5, NULL,           NULL },
103         { 0xF8, NULL,           NULL },
104         { 0xFC, NULL,           NULL },
105         { 0xFE, NULL,           dissect_osi },
106         { 0xFF, NULL,           NULL },
107         { 0x00, NULL,           NULL}
108 };
109
110 static const value_string llc_ctrl_vals[] = {
111         { 0, "Information Transfer" },
112         { 1, "Supervisory" },
113         { 2, "Unknown" },
114         { 3, "Unnumbered Information" },
115         { 0, NULL }
116 };
117
118 static const value_string llc_oui_vals[] = {
119         { 0x000000, "Encapsulated Ethernet" },
120 /*
121 http://www.cisco.com/univercd/cc/td/doc/product/software/ios113ed/113ed_cr/ibm_r/brprt1/brsrb.htm
122 */
123         { 0x0000f8, "Cisco 90-Compatible" },
124         { 0x0000c0, "Cisco" },
125         { 0x0080c2, "Bridged Frame-Relay" }, /* RFC 2427 */
126         { 0,        NULL }
127 };
128
129 static capture_func_t *
130 sap_capture_func(u_char sap) {
131         int i=0;
132
133         /* look for the second record where sap == 0, which should
134          * be the last record
135          */
136         while (saps[i].sap > 0 || i == 0) {
137                 if (saps[i].sap == sap) {
138                         return saps[i].capture_func;
139                 }
140                 i++;
141         }
142         return NULL;
143 }
144
145 static dissect_func_t *
146 sap_dissect_func(u_char sap) {
147         int i=0;
148
149         /* look for the second record where sap == 0, which should
150          * be the last record
151          */
152         while (saps[i].sap > 0 || i == 0) {
153                 if (saps[i].sap == sap) {
154                         return saps[i].dissect_func;
155                 }
156                 i++;
157         }
158         return dissect_data;
159 }
160
161
162 void
163 capture_llc(const u_char *pd, int offset, guint32 cap_len, packet_counts *ld) {
164
165         int             is_snap;
166         guint16         control;
167         int             control_len;
168         guint16         etype;
169         capture_func_t  *capture;
170
171         is_snap = (pd[offset] == 0xAA) && (pd[offset+1] == 0xAA);
172
173         /*
174          * The low-order bit of the SSAP apparently determines whether this
175          * is a request or a response.  (RFC 1390, "Transmission of IP and
176          * ARP over FDDI Networks", says
177          *
178          *      Command frames are identified by having the low order
179          *      bit of the SSAP address reset to zero.  Response frames
180          *      have the low order bit of the SSAP address set to one.
181          *
182          * and a page I've seen seems to imply that's part of 802.2.)
183          *
184          * XXX - that page also implies that LLC Type 2 always uses
185          * extended operation, so we don't need to determine whether
186          * it's basic or extended operation; is that the case?
187          */
188         control = get_xdlc_control(pd, offset+2, pd[offset+1] & 0x01, TRUE);
189         control_len = XDLC_CONTROL_LEN(control, TRUE);
190         if (is_snap)
191                 control_len += 5;       /* 3 bytes of OUI, 2 bytes of ethertype */
192
193         if (is_snap) {
194                 if (XDLC_HAS_PAYLOAD(control)) {
195                         /*
196                          * This frame has a payload to be analyzed.
197                          */
198                         etype  = (pd[offset+6] << 8) | pd[offset+7];
199                         offset += control_len;
200                         capture_ethertype(etype, offset, pd, cap_len, ld);
201                 }
202         }               
203         else {
204                 if (XDLC_HAS_PAYLOAD(control)) {
205                         /*
206                          * This frame has a payload to be analyzed.
207                          */
208                         capture = sap_capture_func(pd[offset]);
209
210                         /* non-SNAP */
211                         offset += control_len;
212
213                         if (capture) {
214                                 capture(pd, offset, cap_len, ld);
215                         }
216                         else {
217                                 ld->other++;
218                         }
219                 }
220         }
221 }
222
223 void
224 dissect_llc(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
225
226         proto_tree      *llc_tree = NULL;
227         proto_item      *ti = NULL;
228         int             is_snap;
229         guint16         control;
230         int             control_len;
231         guint16         etype;
232         dissect_func_t  *dissect;
233
234         is_snap = (pd[offset] == 0xAA) && (pd[offset+1] == 0xAA);
235
236         if (check_col(fd, COL_PROTOCOL)) {
237                 col_add_str(fd, COL_PROTOCOL, "LLC");
238         }
239   
240         if (tree) {
241                 ti = proto_tree_add_item(tree, proto_llc, offset, 0, NULL);
242                 llc_tree = proto_item_add_subtree(ti, ETT_LLC);
243                 proto_tree_add_item(llc_tree, hf_llc_dsap, offset, 1, pd[offset]);
244                 proto_tree_add_item(llc_tree, hf_llc_ssap, offset+1, 1, pd[offset+1]);
245         } else
246                 llc_tree = NULL;
247
248         /*
249          * The low-order bit of the SSAP apparently determines whether this
250          * is a request or a response.  (RFC 1390, "Transmission of IP and
251          * ARP over FDDI Networks", says
252          *
253          *      Command frames are identified by having the low order
254          *      bit of the SSAP address reset to zero.  Response frames
255          *      have the low order bit of the SSAP address set to one.
256          *
257          * and a page I've seen seems to imply that's part of 802.2.)
258          *
259          * XXX - that page also implies that LLC Type 2 always uses
260          * extended operation, so we don't need to determine whether
261          * it's basic or extended operation; is that the case?
262          */
263         control = dissect_xdlc_control(pd, offset+2, fd, llc_tree,
264                                 hf_llc_ctrl, pd[offset+1] & 0x01, TRUE);
265         control_len = XDLC_CONTROL_LEN(control, TRUE);
266         if (is_snap)
267                 control_len += 5;       /* 3 bytes of OUI, 2 bytes of ethertype */
268         if (tree)
269                 proto_item_set_len(ti, control_len);
270
271         /*
272          * XXX - do we want to append the SAP information to the stuff
273          * "dissect_xdlc_control()" put in the COL_INFO column, rather
274          * than overwriting it?
275          */
276         if (is_snap) {
277                 if (check_col(fd, COL_INFO)) {
278                         col_add_str(fd, COL_INFO, "802.2 LLC (SNAP)");
279                 }
280                 if (tree) {
281                         proto_tree_add_item(llc_tree, hf_llc_oui, offset+3, 3,
282                                 pd[offset+3] << 16 | pd[offset+4] << 8 | pd[offset+5]);
283                 }
284                 if (XDLC_HAS_PAYLOAD(control)) {
285                         /*
286                          * This frame has a payload to be analyzed.
287                          */
288                         etype = pntohs(&pd[offset+6]);
289                         offset += control_len;
290                         /* w/o even checking, assume OUI is ethertype */
291                         ethertype(etype, offset, pd, fd, tree, llc_tree,
292                             hf_llc_type);
293                 }
294         }               
295         else {
296                 if (check_col(fd, COL_INFO)) {
297                         col_add_fstr(fd, COL_INFO, "802.2 LLC (%s)",
298                                 val_to_str(pd[offset], sap_vals, "%02x"));
299                 }
300
301                 if (XDLC_HAS_PAYLOAD(control)) {
302                         /*
303                          * This frame has a payload to be analyzed.
304                          */
305                         dissect = sap_dissect_func(pd[offset]);
306
307                         /* non-SNAP */
308                         offset += control_len;
309
310                         if (dissect) {
311                                 dissect(pd, offset, fd, tree);
312                         }
313                         else {
314                                 dissect_data(pd, offset, fd, tree);
315                         }
316                 }
317         }
318 }
319
320 void
321 proto_register_llc(void)
322 {
323         static hf_register_info hf[] = {
324                 { &hf_llc_dsap,
325                 { "DSAP",               "llc.dsap", FT_VALS_UINT8, VALS(sap_vals) }},
326
327                 { &hf_llc_ssap,
328                 { "SSAP",               "llc.ssap", FT_VALS_UINT8, VALS(sap_vals) }},
329
330                 { &hf_llc_ctrl,
331                 { "Control",            "llc.control", FT_VALS_UINT8, VALS(llc_ctrl_vals) }},
332
333                 /* registered here but handled in ethertype.c */
334                 { &hf_llc_type,
335                 { "Type",               "llc.type", FT_VALS_UINT16, VALS(etype_vals) }},
336
337                 { &hf_llc_oui,
338                 { "Organization Code",  "llc.oui", FT_VALS_UINT24, VALS(llc_oui_vals) }}
339         };
340
341         proto_llc = proto_register_protocol ("Logical-Link Control", "llc" );
342         proto_register_field_array(proto_llc, hf, array_length(hf));
343 }