xref: /netbsd-src/external/bsd/tcpdump/dist/print-llc.c (revision ccd9df534e375a4366c5b55f23782053c7a98d82)
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.10 2023/08/17 20:19:40 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 	ndo->ndo_protocol = "llc";
164 	if (caplen < 3) {
165 		nd_print_trunc(ndo);
166 		ND_DEFAULTPRINT((const u_char *)p, caplen);
167 		return (caplen);
168 	}
169 	if (length < 3) {
170 		nd_print_trunc(ndo);
171 		ND_DEFAULTPRINT((const u_char *)p, caplen);
172 		return (length);
173 	}
174 
175 	dsap_field = GET_U_1(p);
176 	ssap_field = GET_U_1(p + 1);
177 
178 	/*
179 	 * OK, what type of LLC frame is this?  The length
180 	 * of the control field depends on that - I frames
181 	 * have a two-byte control field, and U frames have
182 	 * a one-byte control field.
183 	 */
184 	control = GET_U_1(p + 2);
185 	if ((control & LLC_U_FMT) == LLC_U_FMT) {
186 		/*
187 		 * U frame.
188 		 */
189 		is_u = 1;
190 		hdrlen = 3;	/* DSAP, SSAP, 1-byte control field */
191 	} else {
192 		/*
193 		 * The control field in I and S frames is
194 		 * 2 bytes...
195 		 */
196 		if (caplen < 4) {
197 			nd_print_trunc(ndo);
198 			ND_DEFAULTPRINT((const u_char *)p, caplen);
199 			return (caplen);
200 		}
201 		if (length < 4) {
202 			nd_print_trunc(ndo);
203 			ND_DEFAULTPRINT((const u_char *)p, caplen);
204 			return (length);
205 		}
206 
207 		/*
208 		 * ...and is little-endian.
209 		 */
210 		control = GET_LE_U_2(p + 2);
211 		is_u = 0;
212 		hdrlen = 4;	/* DSAP, SSAP, 2-byte control field */
213 	}
214 
215 	if (ssap_field == LLCSAP_GLOBAL && dsap_field == LLCSAP_GLOBAL) {
216 		/*
217 		 * This is an Ethernet_802.3 IPX frame; it has an
218 		 * 802.3 header (i.e., an Ethernet header where the
219 		 * type/length field is <= MAX_ETHERNET_LENGTH_VAL,
220 		 * i.e. it's a length field, not a type field), but
221 		 * has no 802.2 header - the IPX packet starts right
222 		 * after the Ethernet header, with a signature of two
223 		 * bytes of 0xFF (which is LLCSAP_GLOBAL).
224 		 *
225 		 * (It might also have been an Ethernet_802.3 IPX at
226 		 * one time, but got bridged onto another network,
227 		 * such as an 802.11 network; this has appeared in at
228 		 * least one capture file.)
229 		 */
230 
231             if (ndo->ndo_eflag)
232 		ND_PRINT("IPX 802.3: ");
233 
234             ipx_print(ndo, p, length);
235             return (0);		/* no LLC header */
236 	}
237 
238 	dsap = dsap_field & ~LLC_IG;
239 	ssap = ssap_field & ~LLC_GSAP;
240 
241 	if (ndo->ndo_eflag) {
242                 ND_PRINT("LLC, dsap %s (0x%02x) %s, ssap %s (0x%02x) %s",
243                        tok2str(llc_values, "Unknown", dsap),
244                        dsap,
245                        tok2str(llc_ig_flag_values, "Unknown", dsap_field & LLC_IG),
246                        tok2str(llc_values, "Unknown", ssap),
247                        ssap,
248                        tok2str(llc_flag_values, "Unknown", ssap_field & LLC_GSAP));
249 
250 		if (is_u) {
251 			ND_PRINT(", ctrl 0x%02x: ", control);
252 		} else {
253 			ND_PRINT(", ctrl 0x%04x: ", control);
254 		}
255 	}
256 
257 	/*
258 	 * Skip LLC header.
259 	 */
260 	p += hdrlen;
261 	length -= hdrlen;
262 	caplen -= hdrlen;
263 
264 	if (ssap == LLCSAP_SNAP && dsap == LLCSAP_SNAP
265 	    && control == LLC_UI) {
266 		/*
267 		 * XXX - what *is* the right bridge pad value here?
268 		 * Does anybody ever bridge one form of LAN traffic
269 		 * over a networking type that uses 802.2 LLC?
270 		 */
271 		if (!snap_print(ndo, p, length, caplen, src, dst, 2)) {
272 			/*
273 			 * Unknown packet type; tell our caller, by
274 			 * returning a negative value, so they
275 			 * can print the raw packet.
276 			 */
277 			return (-(hdrlen + 5));	/* include LLC and SNAP header */
278 		} else
279 			return (hdrlen + 5);	/* include LLC and SNAP header */
280 	}
281 
282 	if (ssap == LLCSAP_8021D && dsap == LLCSAP_8021D &&
283 	    control == LLC_UI) {
284 		stp_print(ndo, p, length);
285 		return (hdrlen);
286 	}
287 
288 	if (ssap == LLCSAP_IP && dsap == LLCSAP_IP &&
289 	    control == LLC_UI) {
290 		/*
291 		 * This is an RFC 948-style IP packet, with
292 		 * an 802.3 header and an 802.2 LLC header
293 		 * with the source and destination SAPs being
294 		 * the IP SAP.
295 		 */
296 		ip_print(ndo, p, length);
297 		return (hdrlen);
298 	}
299 
300 	if (ssap == LLCSAP_IPX && dsap == LLCSAP_IPX &&
301 	    control == LLC_UI) {
302 		/*
303 		 * This is an Ethernet_802.2 IPX frame, with an 802.3
304 		 * header and an 802.2 LLC header with the source and
305 		 * destination SAPs being the IPX SAP.
306 		 */
307                 if (ndo->ndo_eflag)
308                         ND_PRINT("IPX 802.2: ");
309 
310 		ipx_print(ndo, p, length);
311 		return (hdrlen);
312 	}
313 
314 #ifdef ENABLE_SMB
315 	if (ssap == LLCSAP_NETBEUI && dsap == LLCSAP_NETBEUI
316 	    && (!(control & LLC_S_FMT) || control == LLC_U_FMT)) {
317 		/*
318 		 * we don't actually have a full netbeui parser yet, but the
319 		 * smb parser can handle many smb-in-netbeui packets, which
320 		 * is very useful, so we call that
321 		 *
322 		 * We don't call it for S frames, however, just I frames
323 		 * (which are frames that don't have the low-order bit,
324 		 * LLC_S_FMT, set in the first byte of the control field)
325 		 * and UI frames (whose control field is just 3, LLC_U_FMT).
326 		 */
327 		netbeui_print(ndo, control, p, length);
328 		return (hdrlen);
329 	}
330 #endif
331 	if (ssap == LLCSAP_ISONS && dsap == LLCSAP_ISONS
332 	    && control == LLC_UI) {
333 		isoclns_print(ndo, p, length);
334 		return (hdrlen);
335 	}
336 
337 	if (!ndo->ndo_eflag) {
338 		if (ssap == dsap) {
339 			if (src == NULL || dst == NULL)
340 				ND_PRINT("%s ", tok2str(llc_values, "Unknown DSAP 0x%02x", dsap));
341 			else
342 				ND_PRINT("%s > %s %s ",
343 						(src->addr_string)(ndo, src->addr),
344 						(dst->addr_string)(ndo, dst->addr),
345 						tok2str(llc_values, "Unknown DSAP 0x%02x", dsap));
346 		} else {
347 			if (src == NULL || dst == NULL)
348 				ND_PRINT("%s > %s ",
349                                         tok2str(llc_values, "Unknown SSAP 0x%02x", ssap),
350 					tok2str(llc_values, "Unknown DSAP 0x%02x", dsap));
351 			else
352 				ND_PRINT("%s %s > %s %s ",
353 					(src->addr_string)(ndo, src->addr),
354                                         tok2str(llc_values, "Unknown SSAP 0x%02x", ssap),
355 					(dst->addr_string)(ndo, dst->addr),
356 					tok2str(llc_values, "Unknown DSAP 0x%02x", dsap));
357 		}
358 	}
359 
360 	if (is_u) {
361 		ND_PRINT("Unnumbered, %s, Flags [%s], length %u",
362                        tok2str(llc_cmd_values, "%02x", LLC_U_CMD(control)),
363                        tok2str(llc_flag_values,"?",(ssap_field & LLC_GSAP) | (control & LLC_U_POLL)),
364                        length + hdrlen);
365 
366 		if ((control & ~LLC_U_POLL) == LLC_XID) {
367 			if (length == 0) {
368 				/*
369 				 * XID with no payload.
370 				 * This could, for example, be an SNA
371 				 * "short form" XID.
372                                  */
373 				return (hdrlen);
374 			}
375 			if (caplen < 1) {
376 				nd_print_trunc(ndo);
377 				if (caplen > 0)
378 					ND_DEFAULTPRINT((const u_char *)p, caplen);
379 				return (hdrlen);
380 			}
381 			if (GET_U_1(p) == LLC_XID_FI) {
382 				if (caplen < 3 || length < 3) {
383 					nd_print_trunc(ndo);
384 					if (caplen > 0)
385 						ND_DEFAULTPRINT((const u_char *)p, caplen);
386 				} else
387 					ND_PRINT(": %02x %02x",
388 						  GET_U_1(p + 1),
389 						  GET_U_1(p + 2));
390 				return (hdrlen);
391 			}
392 		}
393 	} else {
394 		if ((control & LLC_S_FMT) == LLC_S_FMT) {
395 			ND_PRINT("Supervisory, %s, rcv seq %u, Flags [%s], length %u",
396 				tok2str(llc_supervisory_values,"?",LLC_S_CMD(control)),
397 				LLC_IS_NR(control),
398 				tok2str(llc_flag_values,"?",(ssap_field & LLC_GSAP) | (control & LLC_IS_POLL)),
399                                 length + hdrlen);
400 			return (hdrlen);	/* no payload to print */
401 		} else {
402 			ND_PRINT("Information, send seq %u, rcv seq %u, Flags [%s], length %u",
403 				LLC_I_NS(control),
404 				LLC_IS_NR(control),
405 				tok2str(llc_flag_values,"?",(ssap_field & LLC_GSAP) | (control & LLC_IS_POLL)),
406                                 length + hdrlen);
407 		}
408 	}
409 	return (-hdrlen);
410 }
411 
412 static const struct tok *
413 oui_to_struct_tok(uint32_t orgcode)
414 {
415 	const struct tok *tok = null_values;
416 	const struct oui_tok *otp;
417 
418 	for (otp = &oui_to_tok[0]; otp->tok != NULL; otp++) {
419 		if (otp->oui == orgcode) {
420 			tok = otp->tok;
421 			break;
422 		}
423 	}
424 	return (tok);
425 }
426 
427 int
428 snap_print(netdissect_options *ndo, const u_char *p, u_int length, u_int caplen,
429 	const struct lladdr_info *src, const struct lladdr_info *dst,
430 	u_int bridge_pad)
431 {
432 	uint32_t orgcode;
433 	u_short et;
434 	int ret;
435 
436 	ndo->ndo_protocol = "snap";
437 	ND_TCHECK_5(p);
438 	if (caplen < 5 || length < 5)
439 		goto trunc;
440 	orgcode = GET_BE_U_3(p);
441 	et = GET_BE_U_2(p + 3);
442 
443 	if (ndo->ndo_eflag) {
444 		/*
445 		 * Somebody's already printed the MAC addresses, if there
446 		 * are any, so just print the SNAP header, not the MAC
447 		 * addresses.
448 		 */
449 		ND_PRINT("oui %s (0x%06x), %s %s (0x%04x), length %u: ",
450 		     tok2str(oui_values, "Unknown", orgcode),
451 		     orgcode,
452 		     (orgcode == 0x000000 ? "ethertype" : "pid"),
453 		     tok2str(oui_to_struct_tok(orgcode), "Unknown", et),
454 		     et, length - 5);
455 	}
456 	p += 5;
457 	length -= 5;
458 	caplen -= 5;
459 
460 	switch (orgcode) {
461 	case OUI_ENCAP_ETHER:
462 	case OUI_CISCO_90:
463 		/*
464 		 * This is an encapsulated Ethernet packet,
465 		 * or a packet bridged by some piece of
466 		 * Cisco hardware; the protocol ID is
467 		 * an Ethernet protocol type.
468 		 */
469 		ret = ethertype_print(ndo, et, p, length, caplen, src, dst);
470 		if (ret)
471 			return (ret);
472 		break;
473 
474 	case OUI_APPLETALK:
475 		if (et == ETHERTYPE_ATALK) {
476 			/*
477 			 * No, I have no idea why Apple used one
478 			 * of their own OUIs, rather than
479 			 * 0x000000, and an Ethernet packet
480 			 * type, for Appletalk data packets,
481 			 * but used 0x000000 and an Ethernet
482 			 * packet type for AARP packets.
483 			 */
484 			ret = ethertype_print(ndo, et, p, length, caplen, src, dst);
485 			if (ret)
486 				return (ret);
487 		}
488 		break;
489 
490 	case OUI_CISCO:
491                 switch (et) {
492                 case PID_CISCO_CDP:
493                         cdp_print(ndo, p, length);
494                         return (1);
495                 case PID_CISCO_DTP:
496                         dtp_print(ndo, p, length);
497                         return (1);
498                 case PID_CISCO_UDLD:
499                         udld_print(ndo, p, length);
500                         return (1);
501                 case PID_CISCO_VTP:
502                         vtp_print(ndo, p, length);
503                         return (1);
504                 case PID_CISCO_PVST:
505                 case PID_CISCO_VLANBRIDGE:
506                         stp_print(ndo, p, length);
507                         return (1);
508                 default:
509                         break;
510                 }
511 		break;
512 
513 	case OUI_RFC2684:
514 		switch (et) {
515 
516 		case PID_RFC2684_ETH_FCS:
517 		case PID_RFC2684_ETH_NOFCS:
518 			/*
519 			 * XXX - remove the last two bytes for
520 			 * PID_RFC2684_ETH_FCS?
521 			 */
522 			/*
523 			 * Skip the padding.
524 			 */
525 			ND_TCHECK_LEN(p, bridge_pad);
526 			caplen -= bridge_pad;
527 			length -= bridge_pad;
528 			p += bridge_pad;
529 
530 			/*
531 			 * What remains is an Ethernet packet.
532 			 */
533 			ether_print(ndo, p, length, caplen, NULL, NULL);
534 			return (1);
535 
536 		case PID_RFC2684_802_5_FCS:
537 		case PID_RFC2684_802_5_NOFCS:
538 			/*
539 			 * XXX - remove the last two bytes for
540 			 * PID_RFC2684_ETH_FCS?
541 			 */
542 			/*
543 			 * Skip the padding, but not the Access
544 			 * Control field.
545 			 */
546 			ND_TCHECK_LEN(p, bridge_pad);
547 			caplen -= bridge_pad;
548 			length -= bridge_pad;
549 			p += bridge_pad;
550 
551 			/*
552 			 * What remains is an 802.5 Token Ring
553 			 * packet.
554 			 */
555 			token_print(ndo, p, length, caplen);
556 			return (1);
557 
558 		case PID_RFC2684_FDDI_FCS:
559 		case PID_RFC2684_FDDI_NOFCS:
560 			/*
561 			 * XXX - remove the last two bytes for
562 			 * PID_RFC2684_ETH_FCS?
563 			 */
564 			/*
565 			 * Skip the padding.
566 			 */
567 			ND_TCHECK_LEN(p, bridge_pad + 1);
568 			caplen -= bridge_pad + 1;
569 			length -= bridge_pad + 1;
570 			p += bridge_pad + 1;
571 
572 			/*
573 			 * What remains is an FDDI packet.
574 			 */
575 			fddi_print(ndo, p, length, caplen);
576 			return (1);
577 
578 		case PID_RFC2684_BPDU:
579 			stp_print(ndo, p, length);
580 			return (1);
581 		}
582 	}
583 	if (!ndo->ndo_eflag) {
584 		/*
585 		 * Nobody printed the link-layer addresses, so print them, if
586 		 * we have any.
587 		 */
588 		if (src != NULL && dst != NULL) {
589 			ND_PRINT("%s > %s ",
590 				(src->addr_string)(ndo, src->addr),
591 				(dst->addr_string)(ndo, dst->addr));
592 		}
593 		/*
594 		 * Print the SNAP header, but if the OUI is 000000, don't
595 		 * bother printing it, and report the PID as being an
596 		 * ethertype.
597 		 */
598 		if (orgcode == 0x000000) {
599 			ND_PRINT("SNAP, ethertype %s (0x%04x), length %u: ",
600 			     tok2str(ethertype_values, "Unknown", et),
601 			     et, length);
602 		} else {
603 			ND_PRINT("SNAP, oui %s (0x%06x), pid %s (0x%04x), length %u: ",
604 			     tok2str(oui_values, "Unknown", orgcode),
605 			     orgcode,
606 			     tok2str(oui_to_struct_tok(orgcode), "Unknown", et),
607 			     et, length);
608 		}
609 	}
610 	return (0);
611 
612 trunc:
613 	nd_print_trunc(ndo);
614 	return (1);
615 }
616