When doing a capture, decode enough of the incoming packets to correctly
[obnox/wireshark/wip.git] / packet-llc.c
1 /* packet-llc.c
2  * Routines for IEEE 802.2 LLC layer
3  * Gilbert Ramirez <gram@verdict.uthscsa.edu>
4  *
5  * $Id: packet-llc.c,v 1.11 1999/02/09 00:35:37 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
36 #include <gtk/gtk.h>
37
38 #include <stdio.h>
39
40 #include "ethereal.h"
41 #include "packet.h"
42 #include "etypes.h"
43
44 struct sap_info {
45         guint8  sap;
46         void    (*capture_func) (const u_char *, int, guint32, packet_counts *);
47         void    (*dissect_func) (const u_char *, int, frame_data *, GtkTree *);
48         char    *text;
49 };
50
51 static struct sap_info  saps[] = {
52         { 0x00, NULL,           NULL,           "NULL LSAP" },
53         { 0x02, NULL,           NULL,           "LLC Sub-Layer Management Individual" },
54         { 0x03, NULL,           NULL,           "LLC Sub-Layer Management Group" },
55         { 0x04, NULL,           NULL,           "SNA Path Control Individual" },
56         { 0x05, NULL,           NULL,           "SNA Path Control Group" },
57         { 0x06, capture_ip,     dissect_ip,     "TCP/IP" },
58         { 0x08, NULL,           NULL,           "SNA" },
59         { 0x0C, NULL,           NULL,           "SNA" },
60         { 0x42, NULL,           NULL,           "Spanning Tree BPDU" },
61         { 0x7F, NULL,           NULL,           "ISO 802.2" },
62         { 0x80, NULL,           NULL,           "XNS" },
63         { 0xAA, NULL,           NULL,           "SNAP" },
64         /*{ 0xBA, NULL,         dissect_vines,  "Banyan Vines" },
65         { 0xBC, NULL,           dissect_vines,  "Banyan Vines" },*/
66         { 0xBA, NULL,           NULL,           "Banyan Vines" },
67         { 0xBC, NULL,           NULL,           "Banyan Vines" },
68         { 0xE0, NULL,           dissect_ipx,    "NetWare" },
69         { 0xF0, NULL,           NULL,           "NetBIOS" },
70         { 0xF4, NULL,           NULL,           "IBM Net Management Individual" },
71         { 0xF5, NULL,           NULL,           "IBM Net Management Group" },
72         { 0xF8, NULL,           NULL,           "Remote Program Load" },
73         { 0xFC, NULL,           NULL,           "Remote Program Load" },
74         { 0xFE, NULL,           dissect_osi,    "ISO Network Layer" },
75         { 0xFF, NULL,           NULL,           "Global LSAP" },
76         { 0x00, NULL,           NULL,           NULL }
77 };
78
79
80 static char*
81 sap_text(u_char sap) {
82         int i=0;
83
84         while (saps[i].text != NULL) {
85                 if (saps[i].sap == sap) {
86                         return saps[i].text;
87                 }
88                 i++;
89         }
90         return "Unknown";
91 }
92
93 static void*
94 sap_capture_func(u_char sap) {
95         int i=0;
96
97         while (saps[i].text != NULL) {
98                 if (saps[i].sap == sap) {
99                         return saps[i].capture_func;
100                 }
101                 i++;
102         }
103         return dissect_data;
104 }
105
106 static void*
107 sap_dissect_func(u_char sap) {
108         int i=0;
109
110         while (saps[i].text != NULL) {
111                 if (saps[i].sap == sap) {
112                         return saps[i].dissect_func;
113                 }
114                 i++;
115         }
116         return dissect_data;
117 }
118
119 static char*
120 llc_org(const u_char *ptr) {
121
122         unsigned long org = (ptr[0] << 16) | (ptr[1] << 8) | ptr[2];
123         char *llc_org[1] = {
124                 "Encapsulated Ethernet"};
125
126         if (org > 0) {
127                 return "Unknown";
128         }
129         else {
130                 return llc_org[org];
131         }
132 }
133
134 void
135 capture_llc(const u_char *pd, int offset, guint32 cap_len, packet_counts *ld) {
136
137         guint16         etype;
138         int             is_snap;
139         void            (*capture) (const u_char *, int, guint32, packet_counts *);
140
141         is_snap = (pd[offset] == 0xAA) && (pd[offset+1] == 0xAA);
142         if (is_snap) {
143                 etype  = (pd[offset+6] << 8) | pd[offset+7];
144                 offset += 8;
145                 capture_ethertype(etype, offset, pd, cap_len, ld);
146         }               
147         else {
148                 capture = sap_capture_func(pd[offset]);
149
150                 /* non-SNAP */
151                 offset += 3;
152
153                 if (capture) {
154                         capture(pd, offset, cap_len, ld);
155                 }
156                 else {
157                         ld->other++;
158                 }
159
160         }
161 }
162
163 void
164 dissect_llc(const u_char *pd, int offset, frame_data *fd, GtkTree *tree) {
165
166         GtkWidget       *llc_tree = NULL, *ti;
167         guint16         etype;
168         int             is_snap;
169         void            (*dissect) (const u_char *, int, frame_data *, GtkTree *);
170
171         /* LLC Strings */
172         char *llc_ctrl[4] = {
173                 "Information Transfer", "Supervisory",
174                 "", "Unnumbered Information" };
175
176         is_snap = (pd[offset] == 0xAA) && (pd[offset+1] == 0xAA);
177
178         if (check_col(fd, COL_PROTOCOL)) {
179                 col_add_str(fd, COL_PROTOCOL, "LLC");
180         }
181   
182         if (tree) {
183                 ti = add_item_to_tree(GTK_WIDGET(tree), offset, (is_snap ? 8 : 3),
184                         "Logical-Link Control");
185                 llc_tree = gtk_tree_new();
186                 add_subtree(ti, llc_tree, ETT_LLC);
187                 add_item_to_tree(llc_tree, offset,      1, "DSAP: %s (0x%02X)",
188                         sap_text(pd[offset]), pd[offset]);
189                 add_item_to_tree(llc_tree, offset+1,    1, "SSAP: %s (0x%02X)",
190                         sap_text(pd[offset+1]), pd[offset+1]);
191                 add_item_to_tree(llc_tree, offset+2,    1, "Control: %s",
192                         llc_ctrl[pd[offset+2] & 3]);
193         }
194
195         if (is_snap) {
196                 if (check_col(fd, COL_INFO)) {
197                         col_add_str(fd, COL_INFO, "802.2 LLC (SNAP)");
198                 }
199                 if (tree) {
200                         add_item_to_tree(llc_tree, offset+3,    3,
201                                 "Organization Code: %s (%02X-%02X-%02X)",
202                                 llc_org(&pd[offset+3]), 
203                                 pd[offset+3], pd[offset+4], pd[offset+5]);
204                 }
205                 etype  = (pd[offset+6] << 8) | pd[offset+7];
206                 offset += 8;
207                 ethertype(etype, offset, pd, fd, tree, llc_tree);
208         }               
209         else {
210                 if (check_col(fd, COL_INFO)) {
211                         col_add_fstr(fd, COL_INFO, "802.2 LLC (%s)", sap_text(pd[offset]));
212                 }
213
214                 dissect = sap_dissect_func(pd[offset]);
215
216                 /* non-SNAP */
217                 offset += 3;
218
219                 if (dissect) {
220                         dissect(pd, offset, fd, tree);
221                 }
222                 else {
223                         dissect_data(pd, offset, fd, tree);
224                 }
225
226         }
227 }