]> The Tcpdump Group git mirrors - tcpdump/blob - print-llc.c
The control field of an LLC frame is 1 byte for a U frame and 2 bytes
[tcpdump] / print-llc.c
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
26 static const char rcsid[] =
27 "@(#) $Header: /tcpdump/master/tcpdump/print-llc.c,v 1.30 2000-12-05 06:42:48 guy Exp $";
28 #endif
29
30 #ifdef HAVE_CONFIG_H
31 #include "config.h"
32 #endif
33
34 #include <sys/param.h>
35 #include <sys/time.h>
36
37 #include <netinet/in.h>
38
39 #include <ctype.h>
40 #include <netdb.h>
41 #include <stdio.h>
42 #include <string.h>
43
44 #include "interface.h"
45 #include "addrtoname.h"
46 #include "extract.h" /* must come after interface.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 u_short control;
72 register int ret;
73
74 if (caplen < 3) {
75 (void)printf("[|llc]");
76 default_print((u_char *)p, caplen);
77 return(0);
78 }
79
80 /* Watch out for possible alignment problems */
81 memcpy((char *)&llc, (char *)p, min(caplen, sizeof(llc)));
82
83 if (llc.ssap == LLCSAP_GLOBAL && llc.dsap == LLCSAP_GLOBAL) {
84 ipx_print(p, length);
85 return (1);
86 }
87
88 /* Cisco Discovery Protocol - SNAP & ether type 0x2000 */
89 if(llc.ssap == LLCSAP_SNAP && llc.dsap == LLCSAP_SNAP &&
90 llc.llcui == LLC_UI &&
91 llc.ethertype[0] == 0x20 && llc.ethertype[1] == 0x00 ) {
92 cdp_print( p, length, caplen, esrc, edst);
93 return (1);
94 }
95
96 if (llc.ssap == LLCSAP_8021D && llc.dsap == LLCSAP_8021D) {
97 stp_print(p, length);
98 return (1);
99 }
100 if (llc.ssap == 0xf0 && llc.dsap == 0xf0) {
101 /*
102 * we don't actually have a full netbeui parser yet, but the
103 * smb parser can handle many smb-in-netbeui packets, which
104 * is very useful, so we call that
105 */
106
107 /*
108 * Skip the DSAP and LSAP.
109 */
110 p += 2;
111 length -= 2;
112 caplen -= 2;
113
114 /*
115 * OK, what type of LLC frame is this? The length
116 * of the control field depends on that - S or I
117 * frames have a two-byte control field, and U frames
118 * have a one-byte control field.
119 */
120 if ((llc.llcu & LLC_U_FMT) == LLC_U_FMT) {
121 control = llc.llcu;
122 p += 1;
123 length -= 1;
124 caplen -= 1;
125 } else {
126 control = llc.llcis;
127 p += 2;
128 length -= 2;
129 caplen -= 2;
130 }
131 netbeui_print(control, p, p + min(caplen, length));
132 return (1);
133 }
134 if (llc.ssap == LLCSAP_ISONS && llc.dsap == LLCSAP_ISONS
135 && llc.llcui == LLC_UI) {
136 isoclns_print(p + 3, length - 3, caplen - 3, esrc, edst);
137 return (1);
138 }
139
140 if (llc.ssap == LLCSAP_SNAP && llc.dsap == LLCSAP_SNAP
141 && llc.llcui == LLC_UI) {
142 if (caplen < sizeof(llc)) {
143 (void)printf("[|llc-snap]");
144 default_print((u_char *)p, caplen);
145 return (0);
146 }
147 if (vflag)
148 (void)printf("snap %s ", protoid_string(llc.llcpi));
149
150 caplen -= sizeof(llc);
151 length -= sizeof(llc);
152 p += sizeof(llc);
153
154 /* This is an encapsulated Ethernet packet */
155 et = EXTRACT_16BITS(&llc.ethertype[0]);
156 ret = ether_encap_print(et, p, length, caplen);
157 if (ret)
158 return (ret);
159 }
160
161 if ((llc.ssap & ~LLC_GSAP) == llc.dsap) {
162 if (eflag)
163 (void)printf("%s ", llcsap_string(llc.dsap));
164 else
165 (void)printf("%s > %s %s ",
166 etheraddr_string(esrc),
167 etheraddr_string(edst),
168 llcsap_string(llc.dsap));
169 } else {
170 if (eflag)
171 (void)printf("%s > %s ",
172 llcsap_string(llc.ssap & ~LLC_GSAP),
173 llcsap_string(llc.dsap));
174 else
175 (void)printf("%s %s > %s %s ",
176 etheraddr_string(esrc),
177 llcsap_string(llc.ssap & ~LLC_GSAP),
178 etheraddr_string(edst),
179 llcsap_string(llc.dsap));
180 }
181
182 if ((llc.llcu & LLC_U_FMT) == LLC_U_FMT) {
183 const char *m;
184 char f;
185 m = tok2str(cmd2str, "%02x", LLC_U_CMD(llc.llcu));
186 switch ((llc.ssap & LLC_GSAP) | (llc.llcu & LLC_U_POLL)) {
187 case 0: f = 'C'; break;
188 case LLC_GSAP: f = 'R'; break;
189 case LLC_U_POLL: f = 'P'; break;
190 case LLC_GSAP|LLC_U_POLL: f = 'F'; break;
191 default: f = '?'; break;
192 }
193
194 printf("%s/%c", m, f);
195
196 p += 3;
197 length -= 3;
198 caplen -= 3;
199
200 if ((llc.llcu & ~LLC_U_POLL) == LLC_XID) {
201 if (*p == LLC_XID_FI) {
202 printf(": %02x %02x", p[1], p[2]);
203 p += 3;
204 length -= 3;
205 caplen -= 3;
206 }
207 }
208
209 if (!strcmp(m,"ui") && f=='C') {
210 /*
211 * we don't have a proper ipx decoder yet, but there
212 * is a partial one in the smb code
213 */
214 ipx_netbios_print(p,p+min(caplen,length));
215 }
216
217 } else {
218 char f;
219 llc.llcis = ntohs(llc.llcis);
220 switch ((llc.ssap & LLC_GSAP) | (llc.llcu & LLC_U_POLL)) {
221 case 0: f = 'C'; break;
222 case LLC_GSAP: f = 'R'; break;
223 case LLC_U_POLL: f = 'P'; break;
224 case LLC_GSAP|LLC_U_POLL: f = 'F'; break;
225 default: f = '?'; break;
226 }
227
228 if ((llc.llcu & LLC_S_FMT) == LLC_S_FMT) {
229 static char *llc_s[] = { "rr", "rej", "rnr", "03" };
230 (void)printf("%s (r=%d,%c)",
231 llc_s[LLC_S_CMD(llc.llcis)],
232 LLC_IS_NR(llc.llcis),
233 f);
234 } else {
235 (void)printf("I (s=%d,r=%d,%c)",
236 LLC_I_NS(llc.llcis),
237 LLC_IS_NR(llc.llcis),
238 f);
239 }
240 p += 4;
241 length -= 4;
242 caplen -= 4;
243 }
244 (void)printf(" len=%d", length);
245 return(1);
246 }