]> The Tcpdump Group git mirrors - tcpdump/blob - print-llc.c
avoid strcpy
[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.38 2001-06-04 05:47:14 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 #include "ethertype.h"
50
51 static struct tok cmd2str[] = {
52 { LLC_UI, "ui" },
53 { LLC_TEST, "test" },
54 { LLC_XID, "xid" },
55 { LLC_UA, "ua" },
56 { LLC_DISC, "disc" },
57 { LLC_DM, "dm" },
58 { LLC_SABME, "sabme" },
59 { LLC_FRMR, "frmr" },
60 { 0, NULL }
61 };
62
63 /*
64 * Returns non-zero IFF it succeeds in printing the header
65 */
66 int
67 llc_print(const u_char *p, u_int length, u_int caplen,
68 const u_char *esrc, const u_char *edst, u_short *extracted_ethertype)
69 {
70 struct llc llc;
71 register u_short et;
72 u_int16_t control;
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 /*
86 * This is an Ethernet_802.3 IPX frame; it has an
87 * 802.3 header (i.e., an Ethernet header where the
88 * type/length field is <= ETHERMTU, i.e. it's a length
89 * field, not a type field), but has no 802.2 header -
90 * the IPX packet starts right after the Ethernet header,
91 * with a signature of two bytes of 0xFF (which is
92 * LLCSAP_GLOBAL).
93 */
94 ipx_print(p, length);
95 return (1);
96 }
97
98 if (llc.ssap == LLCSAP_8021D && llc.dsap == LLCSAP_8021D) {
99 stp_print(p, length);
100 return (1);
101 }
102
103 if (llc.ssap == LLCSAP_IPX && llc.dsap == LLCSAP_IPX &&
104 llc.llcui == LLC_UI) {
105 /*
106 * This is an Ethernet_802.2 IPX frame, with an 802.3
107 * header and an 802.2 LLC header with the source and
108 * destination SAPs being the IPX SAP.
109 *
110 * Skip DSAP, LSAP, and control field.
111 */
112 p += 3;
113 length -= 3;
114 caplen -= 3;
115 ipx_print(p, length);
116 return (1);
117 }
118
119 if (llc.ssap == LLCSAP_NETBEUI && llc.dsap == LLCSAP_NETBEUI
120 && (!(llc.llcu & LLC_S_FMT) || llc.llcu == LLC_U_FMT)) {
121 /*
122 * we don't actually have a full netbeui parser yet, but the
123 * smb parser can handle many smb-in-netbeui packets, which
124 * is very useful, so we call that
125 *
126 * We don't call it for S frames, however, just I frames
127 * (which are frames that don't have the low-order bit,
128 * LLC_S_FMT, set in the first byte of the control field)
129 * and UI frames (whose control field is just 3, LLC_U_FMT).
130 */
131
132 /*
133 * Skip the DSAP and LSAP.
134 */
135 p += 2;
136 length -= 2;
137 caplen -= 2;
138
139 /*
140 * OK, what type of LLC frame is this? The length
141 * of the control field depends on that - I frames
142 * have a two-byte control field, and U frames have
143 * a one-byte control field.
144 */
145 if (llc.llcu == LLC_U_FMT) {
146 control = llc.llcu;
147 p += 1;
148 length -= 1;
149 caplen -= 1;
150 } else {
151 /*
152 * The control field in I and S frames is
153 * little-endian.
154 */
155 control = EXTRACT_LE_16BITS(&llc.llcu);
156 p += 2;
157 length -= 2;
158 caplen -= 2;
159 }
160 netbeui_print(control, p, length);
161 return (1);
162 }
163 if (llc.ssap == LLCSAP_ISONS && llc.dsap == LLCSAP_ISONS
164 && llc.llcui == LLC_UI) {
165 isoclns_print(p + 3, length - 3, caplen - 3, esrc, edst);
166 return (1);
167 }
168
169 if (llc.ssap == LLCSAP_SNAP && llc.dsap == LLCSAP_SNAP
170 && llc.llcui == LLC_UI) {
171 u_int32_t orgcode;
172
173 if (caplen < sizeof(llc)) {
174 (void)printf("[|llc-snap]");
175 default_print((u_char *)p, caplen);
176 return (0);
177 }
178 if (vflag)
179 (void)printf("snap %s ", protoid_string(llc.llcpi));
180
181 caplen -= sizeof(llc);
182 length -= sizeof(llc);
183 p += sizeof(llc);
184
185 orgcode = EXTRACT_24BITS(&llc.llc_orgcode[0]);
186 et = EXTRACT_16BITS(&llc.llc_ethertype[0]);
187 switch (orgcode) {
188 case OUI_ENCAP_ETHER:
189 case OUI_CISCO_90:
190 /*
191 * This is an encapsulated Ethernet packet,
192 * or a packet bridged by some piece of
193 * Cisco hardware; the protocol ID is
194 * an Ethernet protocol type.
195 */
196 ret = ether_encap_print(et, p, length, caplen,
197 extracted_ethertype);
198 if (ret)
199 return (ret);
200 break;
201
202 case OUI_APPLETALK:
203 if (et == ETHERTYPE_ATALK) {
204 /*
205 * No, I have no idea why Apple used one
206 * of their own OUIs, rather than
207 * 0x000000, and an Ethernet packet
208 * type, for Appletalk data packets,
209 * but used 0x000000 and an Ethernet
210 * packet type for AARP packets.
211 */
212 ret = ether_encap_print(et, p, length, caplen,
213 extracted_ethertype);
214 if (ret)
215 return (ret);
216 }
217 break;
218
219 case OUI_CISCO:
220 if (et == ETHERTYPE_CISCO_CDP) {
221 cdp_print( p, length, caplen, esrc, edst);
222 return 1;
223 }
224 break;
225 }
226 }
227
228 if ((llc.ssap & ~LLC_GSAP) == llc.dsap) {
229 if (eflag)
230 (void)printf("%s ", llcsap_string(llc.dsap));
231 else
232 (void)printf("%s > %s %s ",
233 etheraddr_string(esrc),
234 etheraddr_string(edst),
235 llcsap_string(llc.dsap));
236 } else {
237 if (eflag)
238 (void)printf("%s > %s ",
239 llcsap_string(llc.ssap & ~LLC_GSAP),
240 llcsap_string(llc.dsap));
241 else
242 (void)printf("%s %s > %s %s ",
243 etheraddr_string(esrc),
244 llcsap_string(llc.ssap & ~LLC_GSAP),
245 etheraddr_string(edst),
246 llcsap_string(llc.dsap));
247 }
248
249 if ((llc.llcu & LLC_U_FMT) == LLC_U_FMT) {
250 u_int16_t cmd;
251 const char *m;
252 char f;
253
254 cmd = LLC_U_CMD(llc.llcu);
255 m = tok2str(cmd2str, "%02x", cmd);
256 switch ((llc.ssap & LLC_GSAP) | (llc.llcu & LLC_U_POLL)) {
257 case 0: f = 'C'; break;
258 case LLC_GSAP: f = 'R'; break;
259 case LLC_U_POLL: f = 'P'; break;
260 case LLC_GSAP|LLC_U_POLL: f = 'F'; break;
261 default: f = '?'; break;
262 }
263
264 printf("%s/%c", m, f);
265
266 p += 3;
267 length -= 3;
268 caplen -= 3;
269
270 if ((llc.llcu & ~LLC_U_POLL) == LLC_XID) {
271 if (*p == LLC_XID_FI) {
272 printf(": %02x %02x", p[1], p[2]);
273 p += 3;
274 length -= 3;
275 caplen -= 3;
276 }
277 }
278 } else {
279 char f;
280
281 /*
282 * The control field in I and S frames is little-endian.
283 */
284 control = EXTRACT_LE_16BITS(&llc.llcu);
285 switch ((llc.ssap & LLC_GSAP) | (control & LLC_IS_POLL)) {
286 case 0: f = 'C'; break;
287 case LLC_GSAP: f = 'R'; break;
288 case LLC_IS_POLL: f = 'P'; break;
289 case LLC_GSAP|LLC_IS_POLL: f = 'F'; break;
290 default: f = '?'; break;
291 }
292
293 if ((control & LLC_S_FMT) == LLC_S_FMT) {
294 static char *llc_s[] = { "rr", "rej", "rnr", "03" };
295 (void)printf("%s (r=%d,%c)",
296 llc_s[LLC_S_CMD(control)],
297 LLC_IS_NR(control),
298 f);
299 } else {
300 (void)printf("I (s=%d,r=%d,%c)",
301 LLC_I_NS(control),
302 LLC_IS_NR(control),
303 f);
304 }
305 p += 4;
306 length -= 4;
307 caplen -= 4;
308 }
309 (void)printf(" len=%d", length);
310 return(1);
311 }