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