]> The Tcpdump Group git mirrors - tcpdump/blob - print-llc.c
clean up the LCP Auth option printer, remove printf() chains and make use of tok2str...
[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[] _U_ =
27 "@(#) $Header: /tcpdump/master/tcpdump/print-llc.c,v 1.58 2004-05-01 10:06:00 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
46 static struct tok llc_values[] = {
47 { LLCSAP_NULL, "Null" },
48 { LLCSAP_GLOBAL, "Global" },
49 { LLCSAP_8021B_I, "802.1B I" },
50 { LLCSAP_8021B_G, "802.1B G" },
51 { LLCSAP_IP, "IP" },
52 { LLCSAP_PROWAYNM, "ProWay NM" },
53 { LLCSAP_8021D, "STP" },
54 { LLCSAP_RS511, "RS511" },
55 { LLCSAP_ISO8208, "ISO8208" },
56 { LLCSAP_PROWAY, "ProWay" },
57 { LLCSAP_SNAP, "SNAP" },
58 { LLCSAP_IPX, "IPX" },
59 { LLCSAP_NETBEUI, "NetBeui" },
60 { LLCSAP_ISONS, "OSI" },
61 };
62
63 static struct tok cmd2str[] = {
64 { LLC_UI, "ui" },
65 { LLC_TEST, "test" },
66 { LLC_XID, "xid" },
67 { LLC_UA, "ua" },
68 { LLC_DISC, "disc" },
69 { LLC_DM, "dm" },
70 { LLC_SABME, "sabme" },
71 { LLC_FRMR, "frmr" },
72 { 0, NULL }
73 };
74
75 /*
76 * Returns non-zero IFF it succeeds in printing the header
77 */
78 int
79 llc_print(const u_char *p, u_int length, u_int caplen,
80 const u_char *esrc, const u_char *edst, u_short *extracted_ethertype)
81 {
82 struct llc llc;
83 register u_short et;
84 u_int16_t control;
85 register int ret;
86
87 if (caplen < 3) {
88 (void)printf("[|llc]");
89 default_print((u_char *)p, caplen);
90 return(0);
91 }
92
93 /* Watch out for possible alignment problems */
94 memcpy((char *)&llc, (char *)p, min(caplen, sizeof(llc)));
95
96 if (eflag)
97 printf("LLC, dsap %s (0x%02x), ssap %s (0x%02x), cmd 0x%02x, ",
98 tok2str(llc_values,"Unknown",llc.dsap),
99 llc.dsap,
100 tok2str(llc_values,"Unknown",llc.ssap),
101 llc.ssap,
102 llc.llcu);
103
104 if (llc.ssap == LLCSAP_GLOBAL && llc.dsap == LLCSAP_GLOBAL) {
105 /*
106 * This is an Ethernet_802.3 IPX frame; it has an
107 * 802.3 header (i.e., an Ethernet header where the
108 * type/length field is <= ETHERMTU, i.e. it's a length
109 * field, not a type field), but has no 802.2 header -
110 * the IPX packet starts right after the Ethernet header,
111 * with a signature of two bytes of 0xFF (which is
112 * LLCSAP_GLOBAL).
113 *
114 * (It might also have been an Ethernet_802.3 IPX at
115 * one time, but got bridged onto another network,
116 * such as an 802.11 network; this has appeared in at
117 * least one capture file.)
118 */
119
120 if (eflag)
121 printf("IPX-802.3: ");
122
123 ipx_print(p, length);
124 return (1);
125 }
126
127 if (llc.ssap == LLCSAP_8021D && llc.dsap == LLCSAP_8021D) {
128 stp_print(p, length);
129 return (1);
130 }
131
132 if (llc.ssap == LLCSAP_IP && llc.dsap == LLCSAP_IP) {
133 ip_print(p+4, length-4);
134 return (1);
135 }
136
137 if (llc.ssap == LLCSAP_IPX && llc.dsap == LLCSAP_IPX &&
138 llc.llcui == LLC_UI) {
139 /*
140 * This is an Ethernet_802.2 IPX frame, with an 802.3
141 * header and an 802.2 LLC header with the source and
142 * destination SAPs being the IPX SAP.
143 *
144 * Skip DSAP, LSAP, and control field.
145 */
146 printf("(NOV-802.2) ");
147 p += 3;
148 length -= 3;
149 caplen -= 3;
150 ipx_print(p, length);
151 return (1);
152 }
153
154 #ifdef TCPDUMP_DO_SMB
155 if (llc.ssap == LLCSAP_NETBEUI && llc.dsap == LLCSAP_NETBEUI
156 && (!(llc.llcu & LLC_S_FMT) || llc.llcu == LLC_U_FMT)) {
157 /*
158 * we don't actually have a full netbeui parser yet, but the
159 * smb parser can handle many smb-in-netbeui packets, which
160 * is very useful, so we call that
161 *
162 * We don't call it for S frames, however, just I frames
163 * (which are frames that don't have the low-order bit,
164 * LLC_S_FMT, set in the first byte of the control field)
165 * and UI frames (whose control field is just 3, LLC_U_FMT).
166 */
167
168 /*
169 * Skip the DSAP and LSAP.
170 */
171 p += 2;
172 length -= 2;
173 caplen -= 2;
174
175 /*
176 * OK, what type of LLC frame is this? The length
177 * of the control field depends on that - I frames
178 * have a two-byte control field, and U frames have
179 * a one-byte control field.
180 */
181 if (llc.llcu == LLC_U_FMT) {
182 control = llc.llcu;
183 p += 1;
184 length -= 1;
185 caplen -= 1;
186 } else {
187 /*
188 * The control field in I and S frames is
189 * little-endian.
190 */
191 control = EXTRACT_LE_16BITS(&llc.llcu);
192 p += 2;
193 length -= 2;
194 caplen -= 2;
195 }
196 netbeui_print(control, p, length);
197 return (1);
198 }
199 #endif
200 if (llc.ssap == LLCSAP_ISONS && llc.dsap == LLCSAP_ISONS
201 && llc.llcui == LLC_UI) {
202 isoclns_print(p + 3, length - 3, caplen - 3);
203 return (1);
204 }
205
206 if (llc.ssap == LLCSAP_SNAP && llc.dsap == LLCSAP_SNAP
207 && llc.llcui == LLC_UI) {
208 u_int32_t orgcode;
209
210 if (caplen < sizeof(llc)) {
211 (void)printf("[|llc-snap]");
212 default_print((u_char *)p, caplen);
213 return (0);
214 }
215
216 caplen -= sizeof(llc);
217 length -= sizeof(llc);
218 p += sizeof(llc);
219
220 orgcode = EXTRACT_24BITS(&llc.llc_orgcode[0]);
221 et = EXTRACT_16BITS(&llc.llc_ethertype[0]);
222
223 if (eflag)
224 (void)printf("oui 0x%06x, ethertype %s (0x%04x): ",
225 orgcode,
226 tok2str(ethertype_values,"Unknown", et),
227 et);
228
229 /*
230 * XXX - what *is* the right bridge pad value here?
231 * Does anybody ever bridge one form of LAN traffic
232 * over a networking type that uses 802.2 LLC?
233 */
234 ret = snap_print(p, length, caplen, extracted_ethertype,
235 orgcode, et, 2);
236 if (ret)
237 return (ret);
238 }
239
240 if ((llc.ssap & ~LLC_GSAP) == llc.dsap) {
241 if (eflag || esrc == NULL || edst == NULL)
242 (void)printf("%s ", llcsap_string(llc.dsap));
243 else
244 (void)printf("%s > %s %s ",
245 etheraddr_string(esrc),
246 etheraddr_string(edst),
247 llcsap_string(llc.dsap));
248 } else {
249 if (eflag || esrc == NULL || edst == NULL)
250 (void)printf("%s > %s ",
251 llcsap_string(llc.ssap & ~LLC_GSAP),
252 llcsap_string(llc.dsap));
253 else
254 (void)printf("%s %s > %s %s ",
255 etheraddr_string(esrc),
256 llcsap_string(llc.ssap & ~LLC_GSAP),
257 etheraddr_string(edst),
258 llcsap_string(llc.dsap));
259 }
260
261 if ((llc.llcu & LLC_U_FMT) == LLC_U_FMT) {
262 u_int16_t cmd;
263 const char *m;
264 char f;
265
266 cmd = LLC_U_CMD(llc.llcu);
267 m = tok2str(cmd2str, "%02x", cmd);
268 switch ((llc.ssap & LLC_GSAP) | (llc.llcu & LLC_U_POLL)) {
269 case 0: f = 'C'; break;
270 case LLC_GSAP: f = 'R'; break;
271 case LLC_U_POLL: f = 'P'; break;
272 case LLC_GSAP|LLC_U_POLL: f = 'F'; break;
273 default: f = '?'; break;
274 }
275
276 printf("%s/%c", m, f);
277
278 p += 3;
279 length -= 3;
280 caplen -= 3;
281
282 if ((llc.llcu & ~LLC_U_POLL) == LLC_XID) {
283 if (*p == LLC_XID_FI) {
284 printf(": %02x %02x", p[1], p[2]);
285 p += 3;
286 length -= 3;
287 caplen -= 3;
288 }
289 }
290 } else {
291 char f;
292
293 /*
294 * The control field in I and S frames is little-endian.
295 */
296 control = EXTRACT_LE_16BITS(&llc.llcu);
297 switch ((llc.ssap & LLC_GSAP) | (control & LLC_IS_POLL)) {
298 case 0: f = 'C'; break;
299 case LLC_GSAP: f = 'R'; break;
300 case LLC_IS_POLL: f = 'P'; break;
301 case LLC_GSAP|LLC_IS_POLL: f = 'F'; break;
302 default: f = '?'; break;
303 }
304
305 if ((control & LLC_S_FMT) == LLC_S_FMT) {
306 static const char *llc_s[] = { "rr", "rej", "rnr", "03" };
307 (void)printf("%s (r=%d,%c)",
308 llc_s[LLC_S_CMD(control)],
309 LLC_IS_NR(control),
310 f);
311 } else {
312 (void)printf("I (s=%d,r=%d,%c)",
313 LLC_I_NS(control),
314 LLC_IS_NR(control),
315 f);
316 }
317 p += 4;
318 length -= 4;
319 caplen -= 4;
320 }
321 return(1);
322 }
323
324 int
325 snap_print(const u_char *p, u_int length, u_int caplen,
326 u_short *extracted_ethertype, u_int32_t orgcode, u_short et,
327 u_int bridge_pad)
328 {
329 register int ret;
330
331 switch (orgcode) {
332 case OUI_ENCAP_ETHER:
333 case OUI_CISCO_90:
334 /*
335 * This is an encapsulated Ethernet packet,
336 * or a packet bridged by some piece of
337 * Cisco hardware; the protocol ID is
338 * an Ethernet protocol type.
339 */
340 ret = ether_encap_print(et, p, length, caplen,
341 extracted_ethertype);
342 if (ret)
343 return (ret);
344 break;
345
346 case OUI_APPLETALK:
347 if (et == ETHERTYPE_ATALK) {
348 /*
349 * No, I have no idea why Apple used one
350 * of their own OUIs, rather than
351 * 0x000000, and an Ethernet packet
352 * type, for Appletalk data packets,
353 * but used 0x000000 and an Ethernet
354 * packet type for AARP packets.
355 */
356 ret = ether_encap_print(et, p, length, caplen,
357 extracted_ethertype);
358 if (ret)
359 return (ret);
360 }
361 break;
362
363 case OUI_CISCO:
364 if (et == PID_CISCO_CDP) {
365 cdp_print(p, length, caplen);
366 return (1);
367 }
368 break;
369
370 case OUI_RFC2684:
371 switch (et) {
372
373 case PID_RFC2684_ETH_FCS:
374 case PID_RFC2684_ETH_NOFCS:
375 /*
376 * XXX - remove the last two bytes for
377 * PID_RFC2684_ETH_FCS?
378 */
379 /*
380 * Skip the padding.
381 */
382 caplen -= bridge_pad;
383 length -= bridge_pad;
384 p += bridge_pad;
385
386 /*
387 * What remains is an Ethernet packet.
388 */
389 ether_print(p, length, caplen);
390 return (1);
391
392 case PID_RFC2684_802_5_FCS:
393 case PID_RFC2684_802_5_NOFCS:
394 /*
395 * XXX - remove the last two bytes for
396 * PID_RFC2684_ETH_FCS?
397 */
398 /*
399 * Skip the padding, but not the Access
400 * Control field.
401 */
402 caplen -= bridge_pad;
403 length -= bridge_pad;
404 p += bridge_pad;
405
406 /*
407 * What remains is an 802.5 Token Ring
408 * packet.
409 */
410 token_print(p, length, caplen);
411 return (1);
412
413 case PID_RFC2684_FDDI_FCS:
414 case PID_RFC2684_FDDI_NOFCS:
415 /*
416 * XXX - remove the last two bytes for
417 * PID_RFC2684_ETH_FCS?
418 */
419 /*
420 * Skip the padding.
421 */
422 caplen -= bridge_pad + 1;
423 length -= bridge_pad + 1;
424 p += bridge_pad + 1;
425
426 /*
427 * What remains is an FDDI packet.
428 */
429 fddi_print(p, length, caplen);
430 return (1);
431
432 case PID_RFC2684_BPDU:
433 stp_print(p, length);
434 return (1);
435 }
436 }
437 return (0);
438 }