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