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