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