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#ifndef lint
26static const char rcsid[] _U_ =
27    "@(#) $Header: /tcpdump/master/tcpdump/print-llc.c,v 1.75 2007-04-13 09:43:11 hannes Exp $";
28#endif
29
30#ifdef HAVE_CONFIG_H
31#include "config.h"
32#endif
33
34#include <tcpdump-stdinc.h>
35
36#include <stdio.h>
37#include <string.h>
38
39#include "interface.h"
40#include "addrtoname.h"
41#include "extract.h"			/* must come after interface.h */
42
43#include "llc.h"
44#include "ethertype.h"
45#include "oui.h"
46
47static const struct tok llc_values[] = {
48        { LLCSAP_NULL,     "Null" },
49        { LLCSAP_GLOBAL,   "Global" },
50        { LLCSAP_8021B_I,  "802.1B I" },
51        { LLCSAP_8021B_G,  "802.1B G" },
52        { LLCSAP_IP,       "IP" },
53        { LLCSAP_SNA,      "SNA" },
54        { LLCSAP_PROWAYNM, "ProWay NM" },
55        { LLCSAP_8021D,    "STP" },
56        { LLCSAP_RS511,    "RS511" },
57        { LLCSAP_ISO8208,  "ISO8208" },
58        { LLCSAP_PROWAY,   "ProWay" },
59        { LLCSAP_SNAP,     "SNAP" },
60        { LLCSAP_IPX,      "IPX" },
61        { LLCSAP_NETBEUI,  "NetBeui" },
62        { LLCSAP_ISONS,    "OSI" },
63        { 0,               NULL },
64};
65
66static const struct tok llc_cmd_values[] = {
67	{ LLC_UI,	"ui" },
68	{ LLC_TEST,	"test" },
69	{ LLC_XID,	"xid" },
70	{ LLC_UA,	"ua" },
71	{ LLC_DISC,	"disc" },
72	{ LLC_DM,	"dm" },
73	{ LLC_SABME,	"sabme" },
74	{ LLC_FRMR,	"frmr" },
75	{ 0,		NULL }
76};
77
78static const struct tok llc_flag_values[] = {
79        { 0, "Command" },
80        { LLC_GSAP, "Response" },
81        { LLC_U_POLL, "Poll" },
82        { LLC_GSAP|LLC_U_POLL, "Final" },
83        { LLC_IS_POLL, "Poll" },
84        { LLC_GSAP|LLC_IS_POLL, "Final" },
85	{ 0, NULL }
86};
87
88
89static const struct tok llc_ig_flag_values[] = {
90        { 0, "Individual" },
91        { LLC_IG, "Group" },
92	{ 0, NULL }
93};
94
95
96static const struct tok llc_supervisory_values[] = {
97        { 0, "Receiver Ready" },
98        { 1, "Receiver not Ready" },
99        { 2, "Reject" },
100	{ 0,             NULL }
101};
102
103
104static const struct tok cisco_values[] = {
105	{ PID_CISCO_CDP, "CDP" },
106	{ PID_CISCO_VTP, "VTP" },
107	{ PID_CISCO_DTP, "DTP" },
108	{ PID_CISCO_UDLD, "UDLD" },
109	{ PID_CISCO_PVST, "PVST" },
110	{ PID_CISCO_VLANBRIDGE, "VLAN Bridge" },
111	{ 0,             NULL }
112};
113
114static const struct tok bridged_values[] = {
115	{ PID_RFC2684_ETH_FCS,     "Ethernet + FCS" },
116	{ PID_RFC2684_ETH_NOFCS,   "Ethernet w/o FCS" },
117	{ PID_RFC2684_802_4_FCS,   "802.4 + FCS" },
118	{ PID_RFC2684_802_4_NOFCS, "802.4 w/o FCS" },
119	{ PID_RFC2684_802_5_FCS,   "Token Ring + FCS" },
120	{ PID_RFC2684_802_5_NOFCS, "Token Ring w/o FCS" },
121	{ PID_RFC2684_FDDI_FCS,    "FDDI + FCS" },
122	{ PID_RFC2684_FDDI_NOFCS,  "FDDI w/o FCS" },
123	{ PID_RFC2684_802_6_FCS,   "802.6 + FCS" },
124	{ PID_RFC2684_802_6_NOFCS, "802.6 w/o FCS" },
125	{ PID_RFC2684_BPDU,        "BPDU" },
126	{ 0,                       NULL },
127};
128
129static const struct tok null_values[] = {
130	{ 0,             NULL }
131};
132
133struct oui_tok {
134	u_int32_t	oui;
135	const struct tok *tok;
136};
137
138static const struct oui_tok oui_to_tok[] = {
139	{ OUI_ENCAP_ETHER, ethertype_values },
140	{ OUI_CISCO_90, ethertype_values },	/* uses some Ethertype values */
141	{ OUI_APPLETALK, ethertype_values },	/* uses some Ethertype values */
142	{ OUI_CISCO, cisco_values },
143	{ OUI_RFC2684, bridged_values },	/* bridged, RFC 2427 FR or RFC 2864 ATM */
144	{ 0, NULL }
145};
146
147/*
148 * Returns non-zero IFF it succeeds in printing the header
149 */
150int
151llc_print(const u_char *p, u_int length, u_int caplen,
152	  const u_char *esrc, const u_char *edst, u_short *extracted_ethertype)
153{
154	u_int8_t dsap_field, dsap, ssap_field, ssap;
155	u_int16_t control;
156	int is_u;
157	register int ret;
158
159	*extracted_ethertype = 0;
160
161	if (caplen < 3) {
162		(void)printf("[|llc]");
163		default_print((u_char *)p, caplen);
164		return(0);
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	} else {
183		/*
184		 * The control field in I and S frames is
185		 * 2 bytes...
186		 */
187		if (caplen < 4) {
188			(void)printf("[|llc]");
189			default_print((u_char *)p, caplen);
190			return(0);
191		}
192
193		/*
194		 * ...and is little-endian.
195		 */
196		control = EXTRACT_LE_16BITS(p + 2);
197		is_u = 0;
198	}
199
200	if (ssap_field == LLCSAP_GLOBAL && dsap_field == LLCSAP_GLOBAL) {
201		/*
202		 * This is an Ethernet_802.3 IPX frame; it has an
203		 * 802.3 header (i.e., an Ethernet header where the
204		 * type/length field is <= ETHERMTU, i.e. it's a length
205		 * field, not a type field), but has no 802.2 header -
206		 * the IPX packet starts right after the Ethernet header,
207		 * with a signature of two bytes of 0xFF (which is
208		 * LLCSAP_GLOBAL).
209		 *
210		 * (It might also have been an Ethernet_802.3 IPX at
211		 * one time, but got bridged onto another network,
212		 * such as an 802.11 network; this has appeared in at
213		 * least one capture file.)
214		 */
215
216            if (eflag)
217		printf("IPX 802.3: ");
218
219            ipx_print(p, length);
220            return (1);
221	}
222
223	dsap = dsap_field & ~LLC_IG;
224	ssap = ssap_field & ~LLC_GSAP;
225
226	if (eflag) {
227                printf("LLC, dsap %s (0x%02x) %s, ssap %s (0x%02x) %s",
228                       tok2str(llc_values, "Unknown", dsap),
229                       dsap,
230                       tok2str(llc_ig_flag_values, "Unknown", dsap_field & LLC_IG),
231                       tok2str(llc_values, "Unknown", ssap),
232                       ssap,
233                       tok2str(llc_flag_values, "Unknown", ssap_field & LLC_GSAP));
234
235		if (is_u) {
236			printf(", ctrl 0x%02x: ", control);
237		} else {
238			printf(", ctrl 0x%04x: ", control);
239		}
240	}
241
242	if (ssap == LLCSAP_8021D && dsap == LLCSAP_8021D &&
243	    control == LLC_UI) {
244		stp_print(p+3, length-3);
245		return (1);
246	}
247
248	if (ssap == LLCSAP_IP && dsap == LLCSAP_IP &&
249	    control == LLC_UI) {
250		ip_print(gndo, p+4, length-4);
251		return (1);
252	}
253
254	if (ssap == LLCSAP_IPX && dsap == LLCSAP_IPX &&
255	    control == LLC_UI) {
256		/*
257		 * This is an Ethernet_802.2 IPX frame, with an 802.3
258		 * header and an 802.2 LLC header with the source and
259		 * destination SAPs being the IPX SAP.
260		 *
261		 * Skip DSAP, LSAP, and control field.
262		 */
263                if (eflag)
264                        printf("IPX 802.2: ");
265
266		ipx_print(p+3, length-3);
267		return (1);
268	}
269
270#ifdef TCPDUMP_DO_SMB
271	if (ssap == LLCSAP_NETBEUI && dsap == LLCSAP_NETBEUI
272	    && (!(control & LLC_S_FMT) || control == LLC_U_FMT)) {
273		/*
274		 * we don't actually have a full netbeui parser yet, but the
275		 * smb parser can handle many smb-in-netbeui packets, which
276		 * is very useful, so we call that
277		 *
278		 * We don't call it for S frames, however, just I frames
279		 * (which are frames that don't have the low-order bit,
280		 * LLC_S_FMT, set in the first byte of the control field)
281		 * and UI frames (whose control field is just 3, LLC_U_FMT).
282		 */
283
284		/*
285		 * Skip the LLC header.
286		 */
287		if (is_u) {
288			p += 3;
289			length -= 3;
290			caplen -= 3;
291		} else {
292			p += 4;
293			length -= 4;
294			caplen -= 4;
295		}
296		netbeui_print(control, p, length);
297		return (1);
298	}
299#endif
300	if (ssap == LLCSAP_ISONS && dsap == LLCSAP_ISONS
301	    && control == LLC_UI) {
302		isoclns_print(p + 3, length - 3, caplen - 3);
303		return (1);
304	}
305
306	if (ssap == LLCSAP_SNAP && dsap == LLCSAP_SNAP
307	    && control == LLC_UI) {
308		/*
309		 * XXX - what *is* the right bridge pad value here?
310		 * Does anybody ever bridge one form of LAN traffic
311		 * over a networking type that uses 802.2 LLC?
312		 */
313		ret = snap_print(p+3, length-3, caplen-3, 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
380int
381snap_print(const u_char *p, u_int length, u_int caplen, u_int bridge_pad)
382{
383	u_int32_t orgcode;
384	register u_short et;
385	register int ret;
386
387	TCHECK2(*p, 5);
388	orgcode = EXTRACT_24BITS(p);
389	et = EXTRACT_16BITS(p + 3);
390
391	if (eflag) {
392		const struct tok *tok = null_values;
393		const struct oui_tok *otp;
394
395		for (otp = &oui_to_tok[0]; otp->tok != NULL; otp++) {
396			if (otp->oui == orgcode) {
397				tok = otp->tok;
398				break;
399			}
400		}
401		(void)printf("oui %s (0x%06x), %s %s (0x%04x): ",
402		     tok2str(oui_values, "Unknown", orgcode),
403		     orgcode,
404		     (orgcode == 0x000000 ? "ethertype" : "pid"),
405		     tok2str(tok, "Unknown", et),
406		     et);
407	}
408	p += 5;
409	length -= 5;
410	caplen -= 5;
411
412	switch (orgcode) {
413	case OUI_ENCAP_ETHER:
414	case OUI_CISCO_90:
415		/*
416		 * This is an encapsulated Ethernet packet,
417		 * or a packet bridged by some piece of
418		 * Cisco hardware; the protocol ID is
419		 * an Ethernet protocol type.
420		 */
421		ret = ethertype_print(gndo, et, p, length, caplen);
422		if (ret)
423			return (ret);
424		break;
425
426	case OUI_APPLETALK:
427		if (et == ETHERTYPE_ATALK) {
428			/*
429			 * No, I have no idea why Apple used one
430			 * of their own OUIs, rather than
431			 * 0x000000, and an Ethernet packet
432			 * type, for Appletalk data packets,
433			 * but used 0x000000 and an Ethernet
434			 * packet type for AARP packets.
435			 */
436			ret = ethertype_print(gndo, et, p, length, caplen);
437			if (ret)
438				return (ret);
439		}
440		break;
441
442	case OUI_CISCO:
443                switch (et) {
444                case PID_CISCO_CDP:
445                        cdp_print(p, length, caplen);
446                        return (1);
447                case PID_CISCO_DTP:
448                        dtp_print(p, length);
449                        return (1);
450                case PID_CISCO_UDLD:
451                        udld_print(p, length);
452                        return (1);
453                case PID_CISCO_VTP:
454                        vtp_print(p, length);
455                        return (1);
456                case PID_CISCO_PVST:
457                case PID_CISCO_VLANBRIDGE:
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(gndo, 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
536trunc:
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