xref: /freebsd/contrib/tcpdump/print-llc.c (revision 1669d8afc64812c8d2d1d147ae1fd42ff441e1b1)
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.61.2.10 2007/02/08 07:07:51 guy 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 	{ 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 	u_int32_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  * Returns non-zero IFF it succeeds in printing the header
148  */
149 int
150 llc_print(const u_char *p, u_int length, u_int caplen,
151 	  const u_char *esrc, const u_char *edst, u_short *extracted_ethertype)
152 {
153 	u_int8_t dsap_field, dsap, ssap_field, ssap;
154 	u_int16_t control;
155 	int is_u;
156 	register int ret;
157 
158 	*extracted_ethertype = 0;
159 
160 	if (caplen < 3) {
161 		(void)printf("[|llc]");
162 		default_print((u_char *)p, caplen);
163 		return(0);
164 	}
165 
166 	dsap_field = *p;
167 	ssap_field = *(p + 1);
168 
169 	/*
170 	 * OK, what type of LLC frame is this?  The length
171 	 * of the control field depends on that - I frames
172 	 * have a two-byte control field, and U frames have
173 	 * a one-byte control field.
174 	 */
175 	control = *(p + 2);
176 	if ((control & LLC_U_FMT) == LLC_U_FMT) {
177 		/*
178 		 * U frame.
179 		 */
180 		is_u = 1;
181 	} else {
182 		/*
183 		 * The control field in I and S frames is
184 		 * 2 bytes...
185 		 */
186 		if (caplen < 4) {
187 			(void)printf("[|llc]");
188 			default_print((u_char *)p, caplen);
189 			return(0);
190 		}
191 
192 		/*
193 		 * ...and is little-endian.
194 		 */
195 		control = EXTRACT_LE_16BITS(p + 2);
196 		is_u = 0;
197 	}
198 
199 	if (ssap_field == LLCSAP_GLOBAL && dsap_field == LLCSAP_GLOBAL) {
200 		/*
201 		 * This is an Ethernet_802.3 IPX frame; it has an
202 		 * 802.3 header (i.e., an Ethernet header where the
203 		 * type/length field is <= ETHERMTU, i.e. it's a length
204 		 * field, not a type field), but has no 802.2 header -
205 		 * the IPX packet starts right after the Ethernet header,
206 		 * with a signature of two bytes of 0xFF (which is
207 		 * LLCSAP_GLOBAL).
208 		 *
209 		 * (It might also have been an Ethernet_802.3 IPX at
210 		 * one time, but got bridged onto another network,
211 		 * such as an 802.11 network; this has appeared in at
212 		 * least one capture file.)
213 		 */
214 
215             if (eflag)
216 		printf("IPX 802.3: ");
217 
218             ipx_print(p, length);
219             return (1);
220 	}
221 
222 	dsap = dsap_field & ~LLC_IG;
223 	ssap = ssap_field & ~LLC_GSAP;
224 
225 	if (eflag) {
226                 printf("LLC, dsap %s (0x%02x) %s, ssap %s (0x%02x) %s",
227                        tok2str(llc_values, "Unknown", dsap),
228                        dsap,
229                        tok2str(llc_ig_flag_values, "Unknown", dsap_field & LLC_IG),
230                        tok2str(llc_values, "Unknown", ssap),
231                        ssap,
232                        tok2str(llc_flag_values, "Unknown", ssap_field & LLC_GSAP));
233 
234 		if (is_u) {
235 			printf(", ctrl 0x%02x: ", control);
236 		} else {
237 			printf(", ctrl 0x%04x: ", control);
238 		}
239 	}
240 
241 	if (ssap == LLCSAP_8021D && dsap == LLCSAP_8021D &&
242 	    control == LLC_UI) {
243 		stp_print(p+3, length-3);
244 		return (1);
245 	}
246 
247 	if (ssap == LLCSAP_IP && dsap == LLCSAP_IP &&
248 	    control == LLC_UI) {
249 		ip_print(gndo, p+4, length-4);
250 		return (1);
251 	}
252 
253 	if (ssap == LLCSAP_IPX && dsap == LLCSAP_IPX &&
254 	    control == LLC_UI) {
255 		/*
256 		 * This is an Ethernet_802.2 IPX frame, with an 802.3
257 		 * header and an 802.2 LLC header with the source and
258 		 * destination SAPs being the IPX SAP.
259 		 *
260 		 * Skip DSAP, LSAP, and control field.
261 		 */
262                 if (eflag)
263                         printf("IPX 802.2: ");
264 
265 		ipx_print(p+3, length-3);
266 		return (1);
267 	}
268 
269 #ifdef TCPDUMP_DO_SMB
270 	if (ssap == LLCSAP_NETBEUI && dsap == LLCSAP_NETBEUI
271 	    && (!(control & LLC_S_FMT) || control == LLC_U_FMT)) {
272 		/*
273 		 * we don't actually have a full netbeui parser yet, but the
274 		 * smb parser can handle many smb-in-netbeui packets, which
275 		 * is very useful, so we call that
276 		 *
277 		 * We don't call it for S frames, however, just I frames
278 		 * (which are frames that don't have the low-order bit,
279 		 * LLC_S_FMT, set in the first byte of the control field)
280 		 * and UI frames (whose control field is just 3, LLC_U_FMT).
281 		 */
282 
283 		/*
284 		 * Skip the LLC header.
285 		 */
286 		if (is_u) {
287 			p += 3;
288 			length -= 3;
289 			caplen -= 3;
290 		} else {
291 			p += 4;
292 			length -= 4;
293 			caplen -= 4;
294 		}
295 		netbeui_print(control, p, length);
296 		return (1);
297 	}
298 #endif
299 	if (ssap == LLCSAP_ISONS && dsap == LLCSAP_ISONS
300 	    && control == LLC_UI) {
301 		isoclns_print(p + 3, length - 3, caplen - 3);
302 		return (1);
303 	}
304 
305 	if (ssap == LLCSAP_SNAP && dsap == LLCSAP_SNAP
306 	    && control == LLC_UI) {
307 		/*
308 		 * XXX - what *is* the right bridge pad value here?
309 		 * Does anybody ever bridge one form of LAN traffic
310 		 * over a networking type that uses 802.2 LLC?
311 		 */
312 		ret = snap_print(p+3, length-3, caplen-3, extracted_ethertype,
313 		    2);
314 		if (ret)
315 			return (ret);
316 	}
317 
318 	if (!eflag) {
319 		if (ssap == dsap) {
320 			if (esrc == NULL || edst == NULL)
321 				(void)printf("%s ", tok2str(llc_values, "Unknown DSAP 0x%02x", dsap));
322 			else
323 				(void)printf("%s > %s %s ",
324 						etheraddr_string(esrc),
325 						etheraddr_string(edst),
326 						tok2str(llc_values, "Unknown DSAP 0x%02x", dsap));
327 		} else {
328 			if (esrc == NULL || edst == NULL)
329 				(void)printf("%s > %s ",
330                                         tok2str(llc_values, "Unknown SSAP 0x%02x", ssap),
331 					tok2str(llc_values, "Unknown DSAP 0x%02x", dsap));
332 			else
333 				(void)printf("%s %s > %s %s ",
334 					etheraddr_string(esrc),
335                                         tok2str(llc_values, "Unknown SSAP 0x%02x", ssap),
336 					etheraddr_string(edst),
337 					tok2str(llc_values, "Unknown DSAP 0x%02x", dsap));
338 		}
339 	}
340 
341 	if (is_u) {
342 		printf("Unnumbered, %s, Flags [%s], length %u",
343                        tok2str(llc_cmd_values, "%02x", LLC_U_CMD(control)),
344                        tok2str(llc_flag_values,"?",(ssap_field & LLC_GSAP) | (control & LLC_U_POLL)),
345                        length);
346 
347 		p += 3;
348 		length -= 3;
349 		caplen -= 3;
350 
351 		if ((control & ~LLC_U_POLL) == LLC_XID) {
352 			if (*p == LLC_XID_FI) {
353 				printf(": %02x %02x", p[1], p[2]);
354 				p += 3;
355 				length -= 3;
356 				caplen -= 3;
357 			}
358 		}
359 	} else {
360 		if ((control & LLC_S_FMT) == LLC_S_FMT) {
361 			(void)printf("Supervisory, %s, rcv seq %u, Flags [%s], length %u",
362 				tok2str(llc_supervisory_values,"?",LLC_S_CMD(control)),
363 				LLC_IS_NR(control),
364 				tok2str(llc_flag_values,"?",(ssap_field & LLC_GSAP) | (control & LLC_IS_POLL)),
365                                 length);
366 		} else {
367 			(void)printf("Information, send seq %u, rcv seq %u, Flags [%s], length %u",
368 				LLC_I_NS(control),
369 				LLC_IS_NR(control),
370 				tok2str(llc_flag_values,"?",(ssap_field & LLC_GSAP) | (control & LLC_IS_POLL)),
371                                 length);
372 		}
373 		p += 4;
374 		length -= 4;
375 		caplen -= 4;
376 	}
377 	return(1);
378 }
379 
380 int
381 snap_print(const u_char *p, u_int length, u_int caplen,
382     u_short *extracted_ethertype, 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 = ether_encap_print(et, p, length, caplen,
423 		    extracted_ethertype);
424 		if (ret)
425 			return (ret);
426 		break;
427 
428 	case OUI_APPLETALK:
429 		if (et == ETHERTYPE_ATALK) {
430 			/*
431 			 * No, I have no idea why Apple used one
432 			 * of their own OUIs, rather than
433 			 * 0x000000, and an Ethernet packet
434 			 * type, for Appletalk data packets,
435 			 * but used 0x000000 and an Ethernet
436 			 * packet type for AARP packets.
437 			 */
438 			ret = ether_encap_print(et, p, length, caplen,
439 			    extracted_ethertype);
440 			if (ret)
441 				return (ret);
442 		}
443 		break;
444 
445 	case OUI_CISCO:
446 		if (et == PID_CISCO_CDP) {
447 			cdp_print(p, length, caplen);
448 			return (1);
449 		}
450 		break;
451 
452 	case OUI_RFC2684:
453 		switch (et) {
454 
455 		case PID_RFC2684_ETH_FCS:
456 		case PID_RFC2684_ETH_NOFCS:
457 			/*
458 			 * XXX - remove the last two bytes for
459 			 * PID_RFC2684_ETH_FCS?
460 			 */
461 			/*
462 			 * Skip the padding.
463 			 */
464 			TCHECK2(*p, bridge_pad);
465 			caplen -= bridge_pad;
466 			length -= bridge_pad;
467 			p += bridge_pad;
468 
469 			/*
470 			 * What remains is an Ethernet packet.
471 			 */
472 			ether_print(p, length, caplen);
473 			return (1);
474 
475 		case PID_RFC2684_802_5_FCS:
476 		case PID_RFC2684_802_5_NOFCS:
477 			/*
478 			 * XXX - remove the last two bytes for
479 			 * PID_RFC2684_ETH_FCS?
480 			 */
481 			/*
482 			 * Skip the padding, but not the Access
483 			 * Control field.
484 			 */
485 			TCHECK2(*p, bridge_pad);
486 			caplen -= bridge_pad;
487 			length -= bridge_pad;
488 			p += bridge_pad;
489 
490 			/*
491 			 * What remains is an 802.5 Token Ring
492 			 * packet.
493 			 */
494 			token_print(p, length, caplen);
495 			return (1);
496 
497 		case PID_RFC2684_FDDI_FCS:
498 		case PID_RFC2684_FDDI_NOFCS:
499 			/*
500 			 * XXX - remove the last two bytes for
501 			 * PID_RFC2684_ETH_FCS?
502 			 */
503 			/*
504 			 * Skip the padding.
505 			 */
506 			TCHECK2(*p, bridge_pad + 1);
507 			caplen -= bridge_pad + 1;
508 			length -= bridge_pad + 1;
509 			p += bridge_pad + 1;
510 
511 			/*
512 			 * What remains is an FDDI packet.
513 			 */
514 			fddi_print(p, length, caplen);
515 			return (1);
516 
517 		case PID_RFC2684_BPDU:
518 			stp_print(p, length);
519 			return (1);
520 		}
521 	}
522 	return (0);
523 
524 trunc:
525 	(void)printf("[|snap]");
526 	return (1);
527 }
528 
529 
530 /*
531  * Local Variables:
532  * c-style: whitesmith
533  * c-basic-offset: 8
534  * End:
535  */
536