The only LLC frame types that should be dissected based on their SAP or,
[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.20 1999/08/23 22:47:13 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,           NULL },
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         int             control;
167         guint16         etype;
168         capture_func_t  *capture;
169
170         is_snap = (pd[offset] == 0xAA) && (pd[offset+1] == 0xAA);
171
172         /*
173          * The low-order bit of the SSAP apparently determines whether this
174          * is a request or a response.  (RFC 1390, "Transmission of IP and
175          * ARP over FDDI Networks", says
176          *
177          *      Command frames are identified by having the low order
178          *      bit of the SSAP address reset to zero.  Response frames
179          *      have the low order bit of the SSAP address set to one.
180          *
181          * and a page I've seen seems to imply that's part of 802.2.)
182          *
183          * XXX - that page also implies that LLC Type 2 always uses
184          * extended operation, so we don't need to determine whether
185          * it's basic or extended operation; is that the case?
186          */
187         control = get_xdlc_control(pd, offset+2, pd[offset+1] & 0x01, TRUE);
188
189         if (is_snap) {
190                 if (control == XDLC_I || control == (XDLC_U|XDLC_UI)) {
191                         /*
192                          * Unnumbered Information - analyze it based on
193                          * the Ethernet packet type.
194                          */
195                         etype  = (pd[offset+6] << 8) | pd[offset+7];
196                         offset += 8;
197                         capture_ethertype(etype, offset, pd, cap_len, ld);
198                 }
199         }               
200         else {
201                 if (control == XDLC_I || control == (XDLC_U|XDLC_UI)) {
202                         /*
203                          * Unnumbered Information - analyze it based on
204                          * the DSAP.
205                          */
206                         capture = sap_capture_func(pd[offset]);
207
208                         /* non-SNAP */
209                         offset += 3;
210
211                         if (capture) {
212                                 capture(pd, offset, cap_len, ld);
213                         }
214                         else {
215                                 ld->other++;
216                         }
217                 }
218         }
219 }
220
221 void
222 dissect_llc(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
223
224         proto_tree      *llc_tree = NULL;
225         proto_item      *ti;
226         int             is_snap;
227         int             control;
228         guint16         etype;
229         dissect_func_t  *dissect;
230
231         is_snap = (pd[offset] == 0xAA) && (pd[offset+1] == 0xAA);
232
233         if (check_col(fd, COL_PROTOCOL)) {
234                 col_add_str(fd, COL_PROTOCOL, "LLC");
235         }
236   
237         if (tree) {
238                 ti = proto_tree_add_item(tree, proto_llc, offset, (is_snap ? 8 : 3), NULL);
239                 llc_tree = proto_item_add_subtree(ti, ETT_LLC);
240                 proto_tree_add_item(llc_tree, hf_llc_dsap, offset, 1, pd[offset]);
241                 proto_tree_add_item(llc_tree, hf_llc_ssap, offset+1, 1, pd[offset+1]);
242         } else
243                 llc_tree = NULL;
244
245         /*
246          * The low-order bit of the SSAP apparently determines whether this
247          * is a request or a response.  (RFC 1390, "Transmission of IP and
248          * ARP over FDDI Networks", says
249          *
250          *      Command frames are identified by having the low order
251          *      bit of the SSAP address reset to zero.  Response frames
252          *      have the low order bit of the SSAP address set to one.
253          *
254          * and a page I've seen seems to imply that's part of 802.2.)
255          *
256          * XXX - that page also implies that LLC Type 2 always uses
257          * extended operation, so we don't need to determine whether
258          * it's basic or extended operation; is that the case?
259          */
260         control = dissect_xdlc_control(pd, offset+2, fd, llc_tree, hf_llc_ctrl,
261             pd[offset+1] & 0x01, TRUE);
262
263         /*
264          * XXX - do we want to append the SAP information to the stuff
265          * "dissect_xdlc_control()" put in the COL_INFO column, rather
266          * than overwriting it?
267          */
268         if (is_snap) {
269                 if (check_col(fd, COL_INFO)) {
270                         col_add_str(fd, COL_INFO, "802.2 LLC (SNAP)");
271                 }
272                 if (tree) {
273                         proto_tree_add_item(llc_tree, hf_llc_oui, offset+3, 3,
274                                 pd[offset+3] << 16 | pd[offset+4] << 8 | pd[offset+5]);
275                 }
276                 if (control == (XDLC_U|XDLC_UI)) {
277                         /*
278                          * Unnumbered Information - dissect it based on
279                          * the Ethernet packet type.
280                          */
281                         etype = pntohs(&pd[offset+6]);
282                         offset += 8;
283                         /* w/o even checking, assume OUI is ethertype */
284                         ethertype(etype, offset, pd, fd, tree, llc_tree,
285                             hf_llc_type);
286                 }
287         }               
288         else {
289                 if (check_col(fd, COL_INFO)) {
290                         col_add_fstr(fd, COL_INFO, "802.2 LLC (%s)",
291                                 val_to_str(pd[offset], sap_vals, "%02x"));
292                 }
293
294                 if (control == (XDLC_U|XDLC_UI)) {
295                         /*
296                          * Unnumbered Information - dissect it based on
297                          * the DSAP.
298                          */
299                         dissect = sap_dissect_func(pd[offset]);
300
301                         /* non-SNAP */
302                         offset += 3;
303
304                         if (dissect) {
305                                 dissect(pd, offset, fd, tree);
306                         }
307                         else {
308                                 dissect_data(pd, offset, fd, tree);
309                         }
310                 }
311         }
312 }
313
314 void
315 proto_register_llc(void)
316 {
317         static hf_register_info hf[] = {
318                 { &hf_llc_dsap,
319                 { "DSAP",               "llc.dsap", FT_VALS_UINT8, VALS(sap_vals) }},
320
321                 { &hf_llc_ssap,
322                 { "SSAP",               "llc.ssap", FT_VALS_UINT8, VALS(sap_vals) }},
323
324                 { &hf_llc_ctrl,
325                 { "Control",            "llc.control", FT_VALS_UINT8, VALS(llc_ctrl_vals) }},
326
327                 /* registered here but handled in ethertype.c */
328                 { &hf_llc_type,
329                 { "Type",               "llc.type", FT_VALS_UINT16, VALS(etype_vals) }},
330
331                 { &hf_llc_oui,
332                 { "Organization Code",  "llc.oui", FT_VALS_UINT24, VALS(llc_oui_vals) }}
333         };
334
335         proto_llc = proto_register_protocol ("Logical-Link Control", "llc" );
336         proto_register_field_array(proto_llc, hf, array_length(hf));
337 }