]> The Tcpdump Group git mirrors - tcpdump/blob - print-llc.c
85fbb21db1cb638d2672a6da72e60fcc2270f673
[tcpdump] / print-llc.c
1 /*
2 * Copyright (c) 1992, 1993, 1994, 1995, 1996, 1997
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that: (1) source code distributions
7 * retain the above copyright notice and this paragraph in its entirety, (2)
8 * distributions including binary code include the above copyright notice and
9 * this paragraph in its entirety in the documentation or other materials
10 * provided with the distribution, and (3) all advertising materials mentioning
11 * features or use of this software display the following acknowledgement:
12 * ``This product includes software developed by the University of California,
13 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14 * the University nor the names of its contributors may be used to endorse
15 * or promote products derived from this software without specific prior
16 * written permission.
17 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20 *
21 * Code by Matt Thomas, Digital Equipment Corporation
22 * with an awful lot of hacking by Jeffrey Mogul, DECWRL
23 */
24
25 #ifndef lint
26 static const char rcsid[] _U_ =
27 "@(#) $Header: /tcpdump/master/tcpdump/print-llc.c,v 1.53.2.2 2003-11-16 08:51:32 guy Exp $";
28 #endif
29
30 #ifdef HAVE_CONFIG_H
31 #include "config.h"
32 #endif
33
34 #include <tcpdump-stdinc.h>
35
36 #include <stdio.h>
37 #include <string.h>
38
39 #include "interface.h"
40 #include "addrtoname.h"
41 #include "extract.h" /* must come after interface.h */
42
43 #include "llc.h"
44 #include "ethertype.h"
45
46 static struct tok cmd2str[] = {
47 { LLC_UI, "ui" },
48 { LLC_TEST, "test" },
49 { LLC_XID, "xid" },
50 { LLC_UA, "ua" },
51 { LLC_DISC, "disc" },
52 { LLC_DM, "dm" },
53 { LLC_SABME, "sabme" },
54 { LLC_FRMR, "frmr" },
55 { 0, NULL }
56 };
57
58 /*
59 * Returns non-zero IFF it succeeds in printing the header
60 */
61 int
62 llc_print(const u_char *p, u_int length, u_int caplen,
63 const u_char *esrc, const u_char *edst, u_short *extracted_ethertype)
64 {
65 struct llc llc;
66 register u_short et;
67 u_int16_t control;
68 register int ret;
69
70 if (caplen < 3) {
71 (void)printf("[|llc]");
72 default_print((u_char *)p, caplen);
73 return(0);
74 }
75
76 /* Watch out for possible alignment problems */
77 memcpy((char *)&llc, (char *)p, min(caplen, sizeof(llc)));
78
79 if (eflag)
80 printf("LLC, dsap 0x%02x, ssap 0x%02x, cmd 0x%02x, ",
81 llc.dsap,
82 llc.ssap,
83 llc.llcu);
84
85 if (llc.ssap == LLCSAP_GLOBAL && llc.dsap == LLCSAP_GLOBAL) {
86 /*
87 * This is an Ethernet_802.3 IPX frame; it has an
88 * 802.3 header (i.e., an Ethernet header where the
89 * type/length field is <= ETHERMTU, i.e. it's a length
90 * field, not a type field), but has no 802.2 header -
91 * the IPX packet starts right after the Ethernet header,
92 * with a signature of two bytes of 0xFF (which is
93 * LLCSAP_GLOBAL).
94 *
95 * (It might also have been an Ethernet_802.3 IPX at
96 * one time, but got bridged onto another network,
97 * such as an 802.11 network; this has appeared in at
98 * least one capture file.)
99 */
100 printf("(NOV-802.3) ");
101 ipx_print(p, length);
102 return (1);
103 }
104
105 if (llc.ssap == LLCSAP_8021D && llc.dsap == LLCSAP_8021D) {
106 stp_print(p, length);
107 return (1);
108 }
109
110 if (llc.ssap == LLCSAP_IP && llc.dsap == LLCSAP_IP) {
111 ip_print(p+4, length-4);
112 return (1);
113 }
114
115 if (llc.ssap == LLCSAP_IPX && llc.dsap == LLCSAP_IPX &&
116 llc.llcui == LLC_UI) {
117 /*
118 * This is an Ethernet_802.2 IPX frame, with an 802.3
119 * header and an 802.2 LLC header with the source and
120 * destination SAPs being the IPX SAP.
121 *
122 * Skip DSAP, LSAP, and control field.
123 */
124 printf("(NOV-802.2) ");
125 p += 3;
126 length -= 3;
127 caplen -= 3;
128 ipx_print(p, length);
129 return (1);
130 }
131
132 #ifdef TCPDUMP_DO_SMB
133 if (llc.ssap == LLCSAP_NETBEUI && llc.dsap == LLCSAP_NETBEUI
134 && (!(llc.llcu & LLC_S_FMT) || llc.llcu == LLC_U_FMT)) {
135 /*
136 * we don't actually have a full netbeui parser yet, but the
137 * smb parser can handle many smb-in-netbeui packets, which
138 * is very useful, so we call that
139 *
140 * We don't call it for S frames, however, just I frames
141 * (which are frames that don't have the low-order bit,
142 * LLC_S_FMT, set in the first byte of the control field)
143 * and UI frames (whose control field is just 3, LLC_U_FMT).
144 */
145
146 /*
147 * Skip the DSAP and LSAP.
148 */
149 p += 2;
150 length -= 2;
151 caplen -= 2;
152
153 /*
154 * OK, what type of LLC frame is this? The length
155 * of the control field depends on that - I frames
156 * have a two-byte control field, and U frames have
157 * a one-byte control field.
158 */
159 if (llc.llcu == LLC_U_FMT) {
160 control = llc.llcu;
161 p += 1;
162 length -= 1;
163 caplen -= 1;
164 } else {
165 /*
166 * The control field in I and S frames is
167 * little-endian.
168 */
169 control = EXTRACT_LE_16BITS(&llc.llcu);
170 p += 2;
171 length -= 2;
172 caplen -= 2;
173 }
174 netbeui_print(control, p, length);
175 return (1);
176 }
177 #endif
178 if (llc.ssap == LLCSAP_ISONS && llc.dsap == LLCSAP_ISONS
179 && llc.llcui == LLC_UI) {
180 isoclns_print(p + 3, length - 3, caplen - 3);
181 return (1);
182 }
183
184 if (llc.ssap == LLCSAP_SNAP && llc.dsap == LLCSAP_SNAP
185 && llc.llcui == LLC_UI) {
186 u_int32_t orgcode;
187
188 if (caplen < sizeof(llc)) {
189 (void)printf("[|llc-snap]");
190 default_print((u_char *)p, caplen);
191 return (0);
192 }
193 if (vflag)
194 (void)printf("snap %s ", protoid_string(llc.llcpi));
195
196 caplen -= sizeof(llc);
197 length -= sizeof(llc);
198 p += sizeof(llc);
199
200 orgcode = EXTRACT_24BITS(&llc.llc_orgcode[0]);
201 et = EXTRACT_16BITS(&llc.llc_ethertype[0]);
202 /*
203 * XXX - what *is* the right bridge pad value here?
204 * Does anybody ever bridge one form of LAN traffic
205 * over a networking type that uses 802.2 LLC?
206 */
207 ret = snap_print(p, length, caplen, extracted_ethertype,
208 orgcode, et, 2);
209 if (ret)
210 return (ret);
211 }
212
213 if ((llc.ssap & ~LLC_GSAP) == llc.dsap) {
214 if (eflag || esrc == NULL || edst == NULL)
215 (void)printf("%s ", llcsap_string(llc.dsap));
216 else
217 (void)printf("%s > %s %s ",
218 etheraddr_string(esrc),
219 etheraddr_string(edst),
220 llcsap_string(llc.dsap));
221 } else {
222 if (eflag || esrc == NULL || edst == NULL)
223 (void)printf("%s > %s ",
224 llcsap_string(llc.ssap & ~LLC_GSAP),
225 llcsap_string(llc.dsap));
226 else
227 (void)printf("%s %s > %s %s ",
228 etheraddr_string(esrc),
229 llcsap_string(llc.ssap & ~LLC_GSAP),
230 etheraddr_string(edst),
231 llcsap_string(llc.dsap));
232 }
233
234 if ((llc.llcu & LLC_U_FMT) == LLC_U_FMT) {
235 u_int16_t cmd;
236 const char *m;
237 char f;
238
239 cmd = LLC_U_CMD(llc.llcu);
240 m = tok2str(cmd2str, "%02x", cmd);
241 switch ((llc.ssap & LLC_GSAP) | (llc.llcu & LLC_U_POLL)) {
242 case 0: f = 'C'; break;
243 case LLC_GSAP: f = 'R'; break;
244 case LLC_U_POLL: f = 'P'; break;
245 case LLC_GSAP|LLC_U_POLL: f = 'F'; break;
246 default: f = '?'; break;
247 }
248
249 printf("%s/%c", m, f);
250
251 p += 3;
252 length -= 3;
253 caplen -= 3;
254
255 if ((llc.llcu & ~LLC_U_POLL) == LLC_XID) {
256 if (*p == LLC_XID_FI) {
257 printf(": %02x %02x", p[1], p[2]);
258 p += 3;
259 length -= 3;
260 caplen -= 3;
261 }
262 }
263 } else {
264 char f;
265
266 /*
267 * The control field in I and S frames is little-endian.
268 */
269 control = EXTRACT_LE_16BITS(&llc.llcu);
270 switch ((llc.ssap & LLC_GSAP) | (control & LLC_IS_POLL)) {
271 case 0: f = 'C'; break;
272 case LLC_GSAP: f = 'R'; break;
273 case LLC_IS_POLL: f = 'P'; break;
274 case LLC_GSAP|LLC_IS_POLL: f = 'F'; break;
275 default: f = '?'; break;
276 }
277
278 if ((control & LLC_S_FMT) == LLC_S_FMT) {
279 static const char *llc_s[] = { "rr", "rej", "rnr", "03" };
280 (void)printf("%s (r=%d,%c)",
281 llc_s[LLC_S_CMD(control)],
282 LLC_IS_NR(control),
283 f);
284 } else {
285 (void)printf("I (s=%d,r=%d,%c)",
286 LLC_I_NS(control),
287 LLC_IS_NR(control),
288 f);
289 }
290 p += 4;
291 length -= 4;
292 caplen -= 4;
293 }
294 (void)printf(" len=%d", length);
295 return(1);
296 }
297
298 int
299 snap_print(const u_char *p, u_int length, u_int caplen,
300 u_short *extracted_ethertype, u_int32_t orgcode, u_short et,
301 u_int bridge_pad)
302 {
303 register int ret;
304
305 switch (orgcode) {
306 case OUI_ENCAP_ETHER:
307 case OUI_CISCO_90:
308 /*
309 * This is an encapsulated Ethernet packet,
310 * or a packet bridged by some piece of
311 * Cisco hardware; the protocol ID is
312 * an Ethernet protocol type.
313 */
314 ret = ether_encap_print(et, p, length, caplen,
315 extracted_ethertype);
316 if (ret)
317 return (ret);
318 break;
319
320 case OUI_APPLETALK:
321 if (et == ETHERTYPE_ATALK) {
322 /*
323 * No, I have no idea why Apple used one
324 * of their own OUIs, rather than
325 * 0x000000, and an Ethernet packet
326 * type, for Appletalk data packets,
327 * but used 0x000000 and an Ethernet
328 * packet type for AARP packets.
329 */
330 ret = ether_encap_print(et, p, length, caplen,
331 extracted_ethertype);
332 if (ret)
333 return (ret);
334 }
335 break;
336
337 case OUI_CISCO:
338 if (et == PID_CISCO_CDP) {
339 cdp_print(p, length, caplen);
340 return (1);
341 }
342 break;
343
344 case OUI_RFC2684:
345 switch (et) {
346
347 case PID_RFC2684_ETH_FCS:
348 case PID_RFC2684_ETH_NOFCS:
349 /*
350 * XXX - remove the last two bytes for
351 * PID_RFC2684_ETH_FCS?
352 */
353 /*
354 * Skip the padding.
355 */
356 caplen -= bridge_pad;
357 length -= bridge_pad;
358 p += bridge_pad;
359
360 /*
361 * What remains is an Ethernet packet.
362 */
363 ether_print(p, length, caplen);
364 return (1);
365
366 case PID_RFC2684_802_5_FCS:
367 case PID_RFC2684_802_5_NOFCS:
368 /*
369 * XXX - remove the last two bytes for
370 * PID_RFC2684_ETH_FCS?
371 */
372 /*
373 * Skip the padding, but not the Access
374 * Control field.
375 */
376 caplen -= bridge_pad;
377 length -= bridge_pad;
378 p += bridge_pad;
379
380 /*
381 * What remains is an 802.5 Token Ring
382 * packet.
383 */
384 token_print(p, length, caplen);
385 return (1);
386
387 case PID_RFC2684_FDDI_FCS:
388 case PID_RFC2684_FDDI_NOFCS:
389 /*
390 * XXX - remove the last two bytes for
391 * PID_RFC2684_ETH_FCS?
392 */
393 /*
394 * Skip the padding.
395 */
396 caplen -= bridge_pad + 1;
397 length -= bridge_pad + 1;
398 p += bridge_pad + 1;
399
400 /*
401 * What remains is an FDDI packet.
402 */
403 fddi_print(p, length, caplen);
404 return (1);
405
406 case PID_RFC2684_BPDU:
407 stp_print(p, length);
408 return (1);
409 }
410 }
411 return (0);
412 }