xref: /freebsd/contrib/tcpdump/print-llc.c (revision daf1cffce2e07931f27c6c6998652e90df6ba87e)
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[] =
29     "@(#) $Header: /tcpdump/master/tcpdump/print-llc.c,v 1.27 1999/12/22 06:27:21 itojun Exp $";
30 #endif
31 
32 #ifdef HAVE_CONFIG_H
33 #include "config.h"
34 #endif
35 
36 #include <sys/param.h>
37 #include <sys/time.h>
38 
39 #include <netinet/in.h>
40 
41 #include <ctype.h>
42 #include <netdb.h>
43 #include <stdio.h>
44 #include <string.h>
45 
46 #include "interface.h"
47 #include "addrtoname.h"
48 #include "extract.h"			/* must come after interface.h */
49 
50 #include "llc.h"
51 
52 static struct tok cmd2str[] = {
53 	{ LLC_UI,	"ui" },
54 	{ LLC_TEST,	"test" },
55 	{ LLC_XID,	"xid" },
56 	{ LLC_UA,	"ua" },
57 	{ LLC_DISC,	"disc" },
58 	{ LLC_DM,	"dm" },
59 	{ LLC_SABME,	"sabme" },
60 	{ LLC_FRMR,	"frmr" },
61 	{ 0,		NULL }
62 };
63 
64 /*
65  * Returns non-zero IFF it succeeds in printing the header
66  */
67 int
68 llc_print(const u_char *p, u_int length, u_int caplen,
69 	  const u_char *esrc, const u_char *edst)
70 {
71 	struct llc llc;
72 	register u_short et;
73 	register int ret;
74 
75 	if (caplen < 3) {
76 		(void)printf("[|llc]");
77 		default_print((u_char *)p, caplen);
78 		return(0);
79 	}
80 
81 	/* Watch out for possible alignment problems */
82 	memcpy((char *)&llc, (char *)p, min(caplen, sizeof(llc)));
83 
84 	if (llc.ssap == LLCSAP_GLOBAL && llc.dsap == LLCSAP_GLOBAL) {
85 		ipx_print(p, length);
86 		return (1);
87 	}
88 	if (llc.ssap == 0xf0 && llc.dsap == 0xf0) {
89 		/*
90 		 * we don't actually have a full netbeui parser yet, but the
91 		 * smb parser can handle many smb-in-netbeui packets, which
92 		 * is very useful, so we call that
93 		 */
94 		netbeui_print(p + 2, p + min(caplen, length));
95 		return (1);
96 	}
97 	if (llc.ssap == LLCSAP_ISONS && llc.dsap == LLCSAP_ISONS
98 	    && llc.llcui == LLC_UI) {
99 		isoclns_print(p + 3, length - 3, caplen - 3, esrc, edst);
100 		return (1);
101 	}
102 
103 	if (llc.ssap == LLCSAP_SNAP && llc.dsap == LLCSAP_SNAP
104 	    && llc.llcui == LLC_UI) {
105 		if (caplen < sizeof(llc)) {
106 			(void)printf("[|llc-snap]");
107 			default_print((u_char *)p, caplen);
108 			return (0);
109 		}
110 		if (vflag)
111 			(void)printf("snap %s ", protoid_string(llc.llcpi));
112 
113 		caplen -= sizeof(llc);
114 		length -= sizeof(llc);
115 		p += sizeof(llc);
116 
117 		/* This is an encapsulated Ethernet packet */
118 		et = EXTRACT_16BITS(&llc.ethertype[0]);
119 		ret = ether_encap_print(et, p, length, caplen);
120 		if (ret)
121 			return (ret);
122 	}
123 
124 	if ((llc.ssap & ~LLC_GSAP) == llc.dsap) {
125 		if (eflag)
126 			(void)printf("%s ", llcsap_string(llc.dsap));
127 		else
128 			(void)printf("%s > %s %s ",
129 					etheraddr_string(esrc),
130 					etheraddr_string(edst),
131 					llcsap_string(llc.dsap));
132 	} else {
133 		if (eflag)
134 			(void)printf("%s > %s ",
135 				llcsap_string(llc.ssap & ~LLC_GSAP),
136 				llcsap_string(llc.dsap));
137 		else
138 			(void)printf("%s %s > %s %s ",
139 				etheraddr_string(esrc),
140 				llcsap_string(llc.ssap & ~LLC_GSAP),
141 				etheraddr_string(edst),
142 				llcsap_string(llc.dsap));
143 	}
144 
145 	if ((llc.llcu & LLC_U_FMT) == LLC_U_FMT) {
146 		const char *m;
147 		char f;
148 		m = tok2str(cmd2str, "%02x", LLC_U_CMD(llc.llcu));
149 		switch ((llc.ssap & LLC_GSAP) | (llc.llcu & LLC_U_POLL)) {
150 			case 0:			f = 'C'; break;
151 			case LLC_GSAP:		f = 'R'; break;
152 			case LLC_U_POLL:	f = 'P'; break;
153 			case LLC_GSAP|LLC_U_POLL: f = 'F'; break;
154 			default:		f = '?'; break;
155 		}
156 
157 		printf("%s/%c", m, f);
158 
159 		p += 3;
160 		length -= 3;
161 		caplen -= 3;
162 
163 		if ((llc.llcu & ~LLC_U_POLL) == LLC_XID) {
164 			if (*p == LLC_XID_FI) {
165 				printf(": %02x %02x", p[1], p[2]);
166 				p += 3;
167 				length -= 3;
168 				caplen -= 3;
169 			}
170 		}
171 
172 		if (!strcmp(m,"ui") && f=='C') {
173 			/*
174 			 * we don't have a proper ipx decoder yet, but there
175 			 * is a partial one in the smb code
176 			 */
177 			ipx_netbios_print(p,p+min(caplen,length));
178 		}
179 
180 	} else {
181 		char f;
182 		llc.llcis = ntohs(llc.llcis);
183 		switch ((llc.ssap & LLC_GSAP) | (llc.llcu & LLC_U_POLL)) {
184 			case 0:			f = 'C'; break;
185 			case LLC_GSAP:		f = 'R'; break;
186 			case LLC_U_POLL:	f = 'P'; break;
187 			case LLC_GSAP|LLC_U_POLL: f = 'F'; break;
188 			default:		f = '?'; break;
189 		}
190 
191 		if ((llc.llcu & LLC_S_FMT) == LLC_S_FMT) {
192 			static char *llc_s[] = { "rr", "rej", "rnr", "03" };
193 			(void)printf("%s (r=%d,%c)",
194 				llc_s[LLC_S_CMD(llc.llcis)],
195 				LLC_IS_NR(llc.llcis),
196 				f);
197 		} else {
198 			(void)printf("I (s=%d,r=%d,%c)",
199 				LLC_I_NS(llc.llcis),
200 				LLC_IS_NR(llc.llcis),
201 				f);
202 		}
203 		p += 4;
204 		length -= 4;
205 		caplen -= 4;
206 	}
207 	(void)printf(" len=%d", length);
208 	if (caplen > 0 && !qflag) {
209 		default_print_unaligned(p, caplen);
210 	}
211 	return(1);
212 }
213