xref: /openbsd-src/usr.sbin/tcpdump/print-llc.c (revision 2b0358df1d88d06ef4139321dd05bd5e05d91eaf)
1 /*	$OpenBSD: print-llc.c,v 1.17 2007/10/07 16:41:05 deraadt Exp $	*/
2 
3 /*
4  * Copyright (c) 1992, 1993, 1994, 1995, 1996, 1997
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that: (1) source code distributions
9  * retain the above copyright notice and this paragraph in its entirety, (2)
10  * distributions including binary code include the above copyright notice and
11  * this paragraph in its entirety in the documentation or other materials
12  * provided with the distribution, and (3) all advertising materials mentioning
13  * features or use of this software display the following acknowledgement:
14  * ``This product includes software developed by the University of California,
15  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
16  * the University nor the names of its contributors may be used to endorse
17  * or promote products derived from this software without specific prior
18  * written permission.
19  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
20  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
21  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
22  *
23  * Code by Matt Thomas, Digital Equipment Corporation
24  *	with an awful lot of hacking by Jeffrey Mogul, DECWRL
25  */
26 
27 #ifndef lint
28 static const char rcsid[] =
29     "@(#) $Id: print-llc.c,v 1.17 2007/10/07 16:41:05 deraadt Exp $";
30 #endif
31 
32 #include <sys/param.h>
33 #include <sys/time.h>
34 
35 #include <netinet/in.h>
36 
37 #include <ctype.h>
38 #include <netdb.h>
39 #include <signal.h>
40 #include <stdio.h>
41 #include <string.h>
42 
43 #include "interface.h"
44 #include "addrtoname.h"
45 #include "extract.h"			/* must come after interface.h */
46 #include "ethertype.h"
47 
48 #include "llc.h"
49 
50 static struct tok cmd2str[] = {
51 	{ LLC_UI,	"ui" },
52 	{ LLC_TEST,	"test" },
53 	{ LLC_XID,	"xid" },
54 	{ LLC_UA,	"ua" },
55 	{ LLC_DISC,	"disc" },
56 	{ LLC_DM,	"dm" },
57 	{ LLC_SABME,	"sabme" },
58 	{ LLC_FRMR,	"frmr" },
59 	{ 0,		NULL }
60 };
61 
62 /*
63  * Returns non-zero IFF it succeeds in printing the header
64  */
65 int
66 llc_print(const u_char *p, u_int length, u_int caplen,
67 	  const u_char *esrc, const u_char *edst)
68 {
69 	struct llc llc;
70 	register u_short et;
71 #if 0
72 	u_short control;
73 #endif
74 	register int ret;
75 
76 	if (caplen < 3) {
77 		(void)printf("[|llc]");
78 		default_print((u_char *)p, caplen);
79 		return(0);
80 	}
81 
82 	/* Watch out for possible alignment problems */
83 	memcpy((char *)&llc, (char *)p, min(caplen, sizeof(llc)));
84 
85 	if (llc.ssap == LLCSAP_GLOBAL && llc.dsap == LLCSAP_GLOBAL) {
86 		ipx_print(p, length);
87 		return (1);
88 	}
89 #ifdef notyet
90 	else if (p[0] == 0xf0 && p[1] == 0xf0)
91 		netbios_print(p, length);
92 #endif
93 	if (llc.ssap == LLCSAP_ISONS && llc.dsap == LLCSAP_ISONS
94 	    && llc.llcui == LLC_UI) {
95 		isoclns_print(p + 3, length - 3, caplen - 3, esrc, edst);
96 		return (1);
97 	}
98 
99 	if (llc.ssap == LLCSAP_SNAP && llc.dsap == LLCSAP_SNAP
100 	    && llc.llcui == LLC_UI) {
101 		if (caplen < sizeof(llc)) {
102 		    (void)printf("[|llc-snap]");
103 		    default_print((u_char *)p, caplen);
104 		    return (0);
105 		}
106 
107 		/* Cisco Discovery Protocol  - SNAP & ether type 0x2000 */
108 		if (llc.ethertype[0] == 0x20 && llc.ethertype[1] == 0x00) {
109 			cdp_print(p, length, caplen, esrc, edst);
110 			return (1);
111 		}
112 		/* Shared Spanning Tree Protocol - SNAP & ether type 0x010b */
113 		if (llc.ethertype[0] == 0x01 && llc.ethertype[1] == 0x0b) {
114 			stp_print(p, length);
115 			return (1);
116 		}
117 
118 		if (vflag)
119 			(void)printf("snap %s ", protoid_string(llc.llcpi));
120 
121 		caplen -= sizeof(llc);
122 		length -= sizeof(llc);
123 		p += sizeof(llc);
124 
125 		/* This is an encapsulated Ethernet packet */
126 		et = EXTRACT_16BITS(&llc.ethertype[0]);
127 
128 		/*
129 		 * Some protocols have special handling if they are 802.3
130 		 * SNAP encapsulated vs vers II encapsulated. Handle
131 		 * those special protocols here, and hand the rest to
132 		 * print-ether.c so we don't have to duplicate
133 		 * all that code here.
134 		 */
135 		switch (et) {
136 		case ETHERTYPE_ATALK:
137 			atalk_print(p, length);
138 			ret = 1;
139 			break;
140 		default:
141 			ret = ether_encap_print(et, p, length, caplen);
142 			break;
143 		}
144 
145 		if (ret)
146 			return (ret);
147 	}
148 
149 	if (llc.ssap == LLCSAP_8021D && llc.dsap == LLCSAP_8021D) {
150 		stp_print(p, length);
151 		return (1);
152 	}
153 
154 #if 0
155 	if (llc.ssap == 0xf0 && llc.dsap == 0xf0) {
156 		/*
157 		 * we don't actually have a full netbeui parser yet, but the
158 		 * smb parser can handle many smb-in-netbeui packets, which
159 		 * is very useful, so we call that
160 		 */
161 
162 		/*
163 		 * Skip the DSAP and LSAP.
164 		 */
165 		p += 2;
166 		length -= 2;
167 		caplen -= 2;
168 
169 		/*
170 		 * OK, what type of LLC frame is this?  The length
171 		 * of the control field depends on that - S or I
172 		 * frames have a two-byte control field, and U frames
173 		 * have a one-byte control field.
174 		 */
175 		if ((llc.llcu & LLC_U_FMT) == LLC_U_FMT) {
176 			control = llc.llcu;
177 			p += 1;
178 			length -= 1;
179 			caplen -= 1;
180 		} else {
181 			control = llc.llcis;
182 			p += 2;
183 			length -= 2;
184 			caplen -= 2;
185 		}
186 
187 		netbeui_print(control, p, p + min(caplen, length));
188 		return (1);
189 	}
190 #endif
191 
192 	if ((llc.ssap & ~LLC_GSAP) == llc.dsap) {
193 		if (eflag)
194 			(void)printf("%s ", llcsap_string(llc.dsap));
195 		else
196 			(void)printf("%s > %s %s ",
197 					etheraddr_string(esrc),
198 					etheraddr_string(edst),
199 					llcsap_string(llc.dsap));
200 	} else {
201 		if (eflag)
202 			(void)printf("%s > %s ",
203 				llcsap_string(llc.ssap & ~LLC_GSAP),
204 				llcsap_string(llc.dsap));
205 		else
206 			(void)printf("%s %s > %s %s ",
207 				etheraddr_string(esrc),
208 				llcsap_string(llc.ssap & ~LLC_GSAP),
209 				etheraddr_string(edst),
210 				llcsap_string(llc.dsap));
211 	}
212 
213 	if ((llc.llcu & LLC_U_FMT) == LLC_U_FMT) {
214 		const char *m;
215 		char f;
216 		m = tok2str(cmd2str, "%02x", LLC_U_CMD(llc.llcu));
217 		switch ((llc.ssap & LLC_GSAP) | (llc.llcu & LLC_U_POLL)) {
218 		    case 0:			f = 'C'; break;
219 		    case LLC_GSAP:		f = 'R'; break;
220 		    case LLC_U_POLL:		f = 'P'; break;
221 		    case LLC_GSAP|LLC_U_POLL:	f = 'F'; break;
222 		    default:			f = '?'; break;
223 		}
224 
225 		printf("%s/%c", m, f);
226 
227 		if (caplen < 6) {
228 			default_print_unaligned(p, caplen);
229 			return (0);
230 		}
231 		p += 3;
232 		length -= 3;
233 		caplen -= 3;
234 
235 		if ((llc.llcu & ~LLC_U_POLL) == LLC_XID) {
236 		    if (*p == LLC_XID_FI) {
237 			printf(": %02x %02x", p[1], p[2]);
238 			p += 3;
239 			length -= 3;
240 			caplen -= 3;
241 		    }
242 		}
243 
244 #if 0
245 		if (!strcmp(m,"ui") && f=='C') {
246 			/*
247 			 * we don't have a proper ipx decoder yet, but there
248 			 * is a partial one in the smb code
249 			 */
250 			ipx_netbios_print(p,p+min(caplen,length));
251 		}
252 #endif
253 
254 	} else {
255 		char f;
256 		if (caplen < 4) {
257 			default_print_unaligned(p, caplen);
258 			return (0);
259 		}
260 		llc.llcis = ntohs(llc.llcis);
261 		switch ((llc.ssap & LLC_GSAP) | (llc.llcu & LLC_U_POLL)) {
262 		    case 0:			f = 'C'; break;
263 		    case LLC_GSAP:		f = 'R'; break;
264 		    case LLC_U_POLL:		f = 'P'; break;
265 		    case LLC_GSAP|LLC_U_POLL:	f = 'F'; break;
266 		    default:			f = '?'; break;
267 		}
268 
269 		if ((llc.llcu & LLC_S_FMT) == LLC_S_FMT) {
270 			static char *llc_s[] = { "rr", "rej", "rnr", "03" };
271 			(void)printf("%s (r=%d,%c)",
272 				llc_s[LLC_S_CMD(llc.llcis)],
273 				LLC_IS_NR(llc.llcis),
274 				f);
275 		} else {
276 			(void)printf("I (s=%d,r=%d,%c)",
277 				LLC_I_NS(llc.llcis),
278 				LLC_IS_NR(llc.llcis),
279 				f);
280 		}
281 		p += 4;
282 		length -= 4;
283 		caplen -= 4;
284 	}
285 	(void)printf(" len=%d", length);
286 	return(1);
287 }
288