]> The Tcpdump Group git mirrors - tcpdump/blob - print-llc.c
06d0d296d003de476ca7caee4b2236194ae93d65
[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.64 2005-04-26 03:51:38 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 #include "oui.h"
46
47 static struct tok llc_values[] = {
48 { LLCSAP_NULL, "Null" },
49 { LLCSAP_GLOBAL, "Global" },
50 { LLCSAP_8021B_I, "802.1B I" },
51 { LLCSAP_8021B_G, "802.1B G" },
52 { LLCSAP_IP, "IP" },
53 { LLCSAP_PROWAYNM, "ProWay NM" },
54 { LLCSAP_8021D, "STP" },
55 { LLCSAP_RS511, "RS511" },
56 { LLCSAP_ISO8208, "ISO8208" },
57 { LLCSAP_PROWAY, "ProWay" },
58 { LLCSAP_SNAP, "SNAP" },
59 { LLCSAP_IPX, "IPX" },
60 { LLCSAP_NETBEUI, "NetBeui" },
61 { LLCSAP_ISONS, "OSI" },
62 { 0, NULL },
63 };
64
65 static struct tok cmd2str[] = {
66 { LLC_UI, "ui" },
67 { LLC_TEST, "test" },
68 { LLC_XID, "xid" },
69 { LLC_UA, "ua" },
70 { LLC_DISC, "disc" },
71 { LLC_DM, "dm" },
72 { LLC_SABME, "sabme" },
73 { LLC_FRMR, "frmr" },
74 { 0, NULL }
75 };
76
77 static const struct tok cisco_values[] = {
78 { PID_CISCO_CDP, "CDP" },
79 { 0, NULL }
80 };
81
82 static const struct tok bridged_values[] = {
83 { PID_RFC2684_ETH_FCS, "Ethernet + FCS" },
84 { PID_RFC2684_ETH_NOFCS, "Ethernet w/o FCS" },
85 { PID_RFC2684_802_4_FCS, "802.4 + FCS" },
86 { PID_RFC2684_802_4_NOFCS, "802.4 w/o FCS" },
87 { PID_RFC2684_802_5_FCS, "Token Ring + FCS" },
88 { PID_RFC2684_802_5_NOFCS, "Token Ring w/o FCS" },
89 { PID_RFC2684_FDDI_FCS, "FDDI + FCS" },
90 { PID_RFC2684_FDDI_NOFCS, "FDDI w/o FCS" },
91 { PID_RFC2684_802_6_FCS, "802.6 + FCS" },
92 { PID_RFC2684_802_6_NOFCS, "802.6 w/o FCS" },
93 { PID_RFC2684_BPDU, "BPDU" },
94 { 0, NULL },
95 };
96
97 struct oui_tok {
98 u_int32_t oui;
99 const struct tok *tok;
100 };
101
102 static const struct oui_tok oui_to_tok[] = {
103 { OUI_ENCAP_ETHER, ethertype_values },
104 { OUI_CISCO_90, ethertype_values }, /* uses some Ethertype values */
105 { OUI_APPLETALK, ethertype_values }, /* uses some Ethertype values */
106 { OUI_CISCO, cisco_values },
107 { OUI_RFC2684, bridged_values }, /* bridged, RFC 2427 FR or RFC 2864 ATM */
108 { 0, NULL }
109 };
110
111 /*
112 * Returns non-zero IFF it succeeds in printing the header
113 */
114 int
115 llc_print(const u_char *p, u_int length, u_int caplen,
116 const u_char *esrc, const u_char *edst, u_short *extracted_ethertype)
117 {
118 struct llc llc;
119 register u_short et;
120 u_int16_t control;
121 register int ret;
122
123 if (caplen < 3) {
124 (void)printf("[|llc]");
125 default_print((u_char *)p, caplen);
126 return(0);
127 }
128
129 /* Watch out for possible alignment problems */
130 memcpy((char *)&llc, (char *)p, min(caplen, sizeof(llc)));
131
132 if (llc.ssap == LLCSAP_GLOBAL && llc.dsap == LLCSAP_GLOBAL) {
133 /*
134 * This is an Ethernet_802.3 IPX frame; it has an
135 * 802.3 header (i.e., an Ethernet header where the
136 * type/length field is <= ETHERMTU, i.e. it's a length
137 * field, not a type field), but has no 802.2 header -
138 * the IPX packet starts right after the Ethernet header,
139 * with a signature of two bytes of 0xFF (which is
140 * LLCSAP_GLOBAL).
141 *
142 * (It might also have been an Ethernet_802.3 IPX at
143 * one time, but got bridged onto another network,
144 * such as an 802.11 network; this has appeared in at
145 * least one capture file.)
146 */
147
148 if (eflag)
149 printf("IPX-802.3: ");
150
151 ipx_print(p, length);
152 return (1);
153 }
154
155 if (eflag)
156 printf("LLC, dsap %s (0x%02x), ssap %s (0x%02x), cmd 0x%02x: ",
157 tok2str(llc_values,"Unknown",llc.dsap),
158 llc.dsap,
159 tok2str(llc_values,"Unknown",llc.ssap),
160 llc.ssap,
161 llc.llcu);
162
163 if (llc.ssap == LLCSAP_8021D && llc.dsap == LLCSAP_8021D) {
164 stp_print(p, length);
165 return (1);
166 }
167
168 if (llc.ssap == LLCSAP_IP && llc.dsap == LLCSAP_IP) {
169 ip_print(gndo, p+4, length-4);
170 return (1);
171 }
172
173 if (llc.ssap == LLCSAP_IPX && llc.dsap == LLCSAP_IPX &&
174 llc.llcui == LLC_UI) {
175 /*
176 * This is an Ethernet_802.2 IPX frame, with an 802.3
177 * header and an 802.2 LLC header with the source and
178 * destination SAPs being the IPX SAP.
179 *
180 * Skip DSAP, LSAP, and control field.
181 */
182 printf("(NOV-802.2) ");
183 p += 3;
184 length -= 3;
185 caplen -= 3;
186 ipx_print(p, length);
187 return (1);
188 }
189
190 #ifdef TCPDUMP_DO_SMB
191 if (llc.ssap == LLCSAP_NETBEUI && llc.dsap == LLCSAP_NETBEUI
192 && (!(llc.llcu & LLC_S_FMT) || llc.llcu == LLC_U_FMT)) {
193 /*
194 * we don't actually have a full netbeui parser yet, but the
195 * smb parser can handle many smb-in-netbeui packets, which
196 * is very useful, so we call that
197 *
198 * We don't call it for S frames, however, just I frames
199 * (which are frames that don't have the low-order bit,
200 * LLC_S_FMT, set in the first byte of the control field)
201 * and UI frames (whose control field is just 3, LLC_U_FMT).
202 */
203
204 /*
205 * Skip the DSAP and LSAP.
206 */
207 p += 2;
208 length -= 2;
209 caplen -= 2;
210
211 /*
212 * OK, what type of LLC frame is this? The length
213 * of the control field depends on that - I frames
214 * have a two-byte control field, and U frames have
215 * a one-byte control field.
216 */
217 if (llc.llcu == LLC_U_FMT) {
218 control = llc.llcu;
219 p += 1;
220 length -= 1;
221 caplen -= 1;
222 } else {
223 /*
224 * The control field in I and S frames is
225 * little-endian.
226 */
227 control = EXTRACT_LE_16BITS(&llc.llcu);
228 p += 2;
229 length -= 2;
230 caplen -= 2;
231 }
232 netbeui_print(control, p, length);
233 return (1);
234 }
235 #endif
236 if (llc.ssap == LLCSAP_ISONS && llc.dsap == LLCSAP_ISONS
237 && llc.llcui == LLC_UI) {
238 isoclns_print(p + 3, length - 3, caplen - 3);
239 return (1);
240 }
241
242 if (llc.ssap == LLCSAP_SNAP && llc.dsap == LLCSAP_SNAP
243 && llc.llcui == LLC_UI) {
244 u_int32_t orgcode;
245
246 if (caplen < sizeof(llc)) {
247 (void)printf("[|llc-snap]");
248 default_print((u_char *)p, caplen);
249 return (0);
250 }
251
252 caplen -= sizeof(llc);
253 length -= sizeof(llc);
254 p += sizeof(llc);
255
256 orgcode = EXTRACT_24BITS(&llc.llc_orgcode[0]);
257 et = EXTRACT_16BITS(&llc.llc_ethertype[0]);
258
259 if (eflag) {
260 const struct tok *tok = NULL;
261 const struct oui_tok *otp;
262
263 for (otp = &oui_to_tok[0]; otp->tok != NULL; otp++) {
264 if (otp->oui == orgcode) {
265 tok = otp->tok;
266 break;
267 }
268 }
269 (void)printf("oui %s (0x%06x), %s %s (0x%04x): ",
270 tok2str(oui_values, "Unknown", orgcode),
271 orgcode,
272 (orgcode == 0x000000 ? "ethertype" : "pid"),
273 tok2str(tok, "Unknown", et),
274 et);
275 }
276
277 /*
278 * XXX - what *is* the right bridge pad value here?
279 * Does anybody ever bridge one form of LAN traffic
280 * over a networking type that uses 802.2 LLC?
281 */
282 ret = snap_print(p, length, caplen, extracted_ethertype,
283 orgcode, et, 2);
284 if (ret)
285 return (ret);
286 }
287
288 if (!eflag) {
289 if ((llc.ssap & ~LLC_GSAP) == llc.dsap) {
290 if (esrc == NULL || edst == NULL)
291 (void)printf("%s ", llcsap_string(llc.dsap));
292 else
293 (void)printf("%s > %s %s ",
294 etheraddr_string(esrc),
295 etheraddr_string(edst),
296 llcsap_string(llc.dsap));
297 } else {
298 if (esrc == NULL || edst == NULL)
299 (void)printf("%s > %s ",
300 llcsap_string(llc.ssap & ~LLC_GSAP),
301 llcsap_string(llc.dsap));
302 else
303 (void)printf("%s %s > %s %s ",
304 etheraddr_string(esrc),
305 llcsap_string(llc.ssap & ~LLC_GSAP),
306 etheraddr_string(edst),
307 llcsap_string(llc.dsap));
308 }
309 }
310
311 if ((llc.llcu & LLC_U_FMT) == LLC_U_FMT) {
312 u_int16_t cmd;
313 const char *m;
314 char f;
315
316 cmd = LLC_U_CMD(llc.llcu);
317 m = tok2str(cmd2str, "%02x", cmd);
318 switch ((llc.ssap & LLC_GSAP) | (llc.llcu & LLC_U_POLL)) {
319 case 0: f = 'C'; break;
320 case LLC_GSAP: f = 'R'; break;
321 case LLC_U_POLL: f = 'P'; break;
322 case LLC_GSAP|LLC_U_POLL: f = 'F'; break;
323 default: f = '?'; break;
324 }
325
326 printf("%s/%c", m, f);
327
328 p += 3;
329 length -= 3;
330 caplen -= 3;
331
332 if ((llc.llcu & ~LLC_U_POLL) == LLC_XID) {
333 if (*p == LLC_XID_FI) {
334 printf(": %02x %02x", p[1], p[2]);
335 p += 3;
336 length -= 3;
337 caplen -= 3;
338 }
339 }
340 } else {
341 char f;
342
343 /*
344 * The control field in I and S frames is little-endian.
345 */
346 control = EXTRACT_LE_16BITS(&llc.llcu);
347 switch ((llc.ssap & LLC_GSAP) | (control & LLC_IS_POLL)) {
348 case 0: f = 'C'; break;
349 case LLC_GSAP: f = 'R'; break;
350 case LLC_IS_POLL: f = 'P'; break;
351 case LLC_GSAP|LLC_IS_POLL: f = 'F'; break;
352 default: f = '?'; break;
353 }
354
355 if ((control & LLC_S_FMT) == LLC_S_FMT) {
356 static const char *llc_s[] = { "rr", "rej", "rnr", "03" };
357 (void)printf("%s (r=%d,%c)",
358 llc_s[LLC_S_CMD(control)],
359 LLC_IS_NR(control),
360 f);
361 } else {
362 (void)printf("I (s=%d,r=%d,%c)",
363 LLC_I_NS(control),
364 LLC_IS_NR(control),
365 f);
366 }
367 p += 4;
368 length -= 4;
369 caplen -= 4;
370 }
371 return(1);
372 }
373
374 int
375 snap_print(const u_char *p, u_int length, u_int caplen,
376 u_short *extracted_ethertype, u_int32_t orgcode, u_short et,
377 u_int bridge_pad)
378 {
379 register int ret;
380
381 switch (orgcode) {
382 case OUI_ENCAP_ETHER:
383 case OUI_CISCO_90:
384 /*
385 * This is an encapsulated Ethernet packet,
386 * or a packet bridged by some piece of
387 * Cisco hardware; the protocol ID is
388 * an Ethernet protocol type.
389 */
390 ret = ether_encap_print(et, p, length, caplen,
391 extracted_ethertype);
392 if (ret)
393 return (ret);
394 break;
395
396 case OUI_APPLETALK:
397 if (et == ETHERTYPE_ATALK) {
398 /*
399 * No, I have no idea why Apple used one
400 * of their own OUIs, rather than
401 * 0x000000, and an Ethernet packet
402 * type, for Appletalk data packets,
403 * but used 0x000000 and an Ethernet
404 * packet type for AARP packets.
405 */
406 ret = ether_encap_print(et, p, length, caplen,
407 extracted_ethertype);
408 if (ret)
409 return (ret);
410 }
411 break;
412
413 case OUI_CISCO:
414 if (et == PID_CISCO_CDP) {
415 cdp_print(p, length, caplen);
416 return (1);
417 }
418 break;
419
420 case OUI_RFC2684:
421 switch (et) {
422
423 case PID_RFC2684_ETH_FCS:
424 case PID_RFC2684_ETH_NOFCS:
425 /*
426 * XXX - remove the last two bytes for
427 * PID_RFC2684_ETH_FCS?
428 */
429 /*
430 * Skip the padding.
431 */
432 caplen -= bridge_pad;
433 length -= bridge_pad;
434 p += bridge_pad;
435
436 /*
437 * What remains is an Ethernet packet.
438 */
439 ether_print(p, length, caplen);
440 return (1);
441
442 case PID_RFC2684_802_5_FCS:
443 case PID_RFC2684_802_5_NOFCS:
444 /*
445 * XXX - remove the last two bytes for
446 * PID_RFC2684_ETH_FCS?
447 */
448 /*
449 * Skip the padding, but not the Access
450 * Control field.
451 */
452 caplen -= bridge_pad;
453 length -= bridge_pad;
454 p += bridge_pad;
455
456 /*
457 * What remains is an 802.5 Token Ring
458 * packet.
459 */
460 token_print(p, length, caplen);
461 return (1);
462
463 case PID_RFC2684_FDDI_FCS:
464 case PID_RFC2684_FDDI_NOFCS:
465 /*
466 * XXX - remove the last two bytes for
467 * PID_RFC2684_ETH_FCS?
468 */
469 /*
470 * Skip the padding.
471 */
472 caplen -= bridge_pad + 1;
473 length -= bridge_pad + 1;
474 p += bridge_pad + 1;
475
476 /*
477 * What remains is an FDDI packet.
478 */
479 fddi_print(p, length, caplen);
480 return (1);
481
482 case PID_RFC2684_BPDU:
483 stp_print(p, length);
484 return (1);
485 }
486 }
487 return (0);
488 }
489
490
491 /*
492 * Local Variables:
493 * c-style: whitesmith
494 * c-basic-offset: 8
495 * End:
496 */