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