]> The Tcpdump Group git mirrors - tcpdump/blob - print-llc.c
bugfix: bogus SS codepoints, display ctrl field correct, added 'Poll' token for I...
[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.68 2005-12-01 17:45:45 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 #include "oui.h"
46
47 static struct tok llc_values[] = {
48 { LLCSAP_NULL, "Null" },
49 { LLCSAP_GLOBAL, "Global" },
50 { LLCSAP_8021B_I, "802.1B I" },
51 { LLCSAP_8021B_G, "802.1B G" },
52 { LLCSAP_IP, "IP" },
53 { LLCSAP_SNA, "SNA" },
54 { LLCSAP_PROWAYNM, "ProWay NM" },
55 { LLCSAP_8021D, "STP" },
56 { LLCSAP_RS511, "RS511" },
57 { LLCSAP_ISO8208, "ISO8208" },
58 { LLCSAP_PROWAY, "ProWay" },
59 { LLCSAP_SNAP, "SNAP" },
60 { LLCSAP_IPX, "IPX" },
61 { LLCSAP_NETBEUI, "NetBeui" },
62 { LLCSAP_ISONS, "OSI" },
63 { 0, NULL },
64 };
65
66 static struct tok llc_cmd_values[] = {
67 { LLC_UI, "ui" },
68 { LLC_TEST, "test" },
69 { LLC_XID, "xid" },
70 { LLC_UA, "ua" },
71 { LLC_DISC, "disc" },
72 { LLC_DM, "dm" },
73 { LLC_SABME, "sabme" },
74 { LLC_FRMR, "frmr" },
75 { 0, NULL }
76 };
77
78 static const struct tok llc_flag_values[] = {
79 { 0, "Command" },
80 { LLC_GSAP, "Response" },
81 { LLC_U_POLL, "Poll" },
82 { LLC_GSAP|LLC_U_POLL, "Final" },
83 { LLC_IS_POLL, "Poll" },
84 { LLC_GSAP|LLC_IS_POLL, "Final" },
85 { 0, NULL }
86 };
87
88
89 static const struct tok llc_supervisory_values[] = {
90 { 0, "Receiver Ready" },
91 { 1, "Receiver not Ready" },
92 { 2, "Reject" },
93 { 0, NULL }
94 };
95
96
97 static const struct tok cisco_values[] = {
98 { PID_CISCO_CDP, "CDP" },
99 { 0, NULL }
100 };
101
102 static const struct tok bridged_values[] = {
103 { PID_RFC2684_ETH_FCS, "Ethernet + FCS" },
104 { PID_RFC2684_ETH_NOFCS, "Ethernet w/o FCS" },
105 { PID_RFC2684_802_4_FCS, "802.4 + FCS" },
106 { PID_RFC2684_802_4_NOFCS, "802.4 w/o FCS" },
107 { PID_RFC2684_802_5_FCS, "Token Ring + FCS" },
108 { PID_RFC2684_802_5_NOFCS, "Token Ring w/o FCS" },
109 { PID_RFC2684_FDDI_FCS, "FDDI + FCS" },
110 { PID_RFC2684_FDDI_NOFCS, "FDDI w/o FCS" },
111 { PID_RFC2684_802_6_FCS, "802.6 + FCS" },
112 { PID_RFC2684_802_6_NOFCS, "802.6 w/o FCS" },
113 { PID_RFC2684_BPDU, "BPDU" },
114 { 0, NULL },
115 };
116
117 struct oui_tok {
118 u_int32_t oui;
119 const struct tok *tok;
120 };
121
122 static const struct oui_tok oui_to_tok[] = {
123 { OUI_ENCAP_ETHER, ethertype_values },
124 { OUI_CISCO_90, ethertype_values }, /* uses some Ethertype values */
125 { OUI_APPLETALK, ethertype_values }, /* uses some Ethertype values */
126 { OUI_CISCO, cisco_values },
127 { OUI_RFC2684, bridged_values }, /* bridged, RFC 2427 FR or RFC 2864 ATM */
128 { 0, NULL }
129 };
130
131 /*
132 * Returns non-zero IFF it succeeds in printing the header
133 */
134 int
135 llc_print(const u_char *p, u_int length, u_int caplen,
136 const u_char *esrc, const u_char *edst, u_short *extracted_ethertype)
137 {
138 u_int8_t dsap_field, dsap, ssap_field, ssap;
139 u_int16_t control;
140 int is_u;
141 register int ret;
142
143 *extracted_ethertype = 0;
144
145 if (caplen < 3) {
146 (void)printf("[|llc]");
147 default_print((u_char *)p, caplen);
148 return(0);
149 }
150
151 dsap_field = *p;
152 ssap_field = *(p + 1);
153
154 /*
155 * OK, what type of LLC frame is this? The length
156 * of the control field depends on that - I frames
157 * have a two-byte control field, and U frames have
158 * a one-byte control field.
159 */
160 control = *(p + 2);
161 if ((control & LLC_U_FMT) == LLC_U_FMT) {
162 /*
163 * U frame.
164 */
165 is_u = 1;
166 } else {
167 /*
168 * The control field in I and S frames is
169 * 2 bytes...
170 */
171 if (caplen < 4) {
172 (void)printf("[|llc]");
173 default_print((u_char *)p, caplen);
174 return(0);
175 }
176
177 /*
178 * ...and is little-endian.
179 */
180 control = EXTRACT_LE_16BITS(p + 2);
181 is_u = 0;
182 }
183
184 if (ssap_field == LLCSAP_GLOBAL && dsap_field == LLCSAP_GLOBAL) {
185 /*
186 * This is an Ethernet_802.3 IPX frame; it has an
187 * 802.3 header (i.e., an Ethernet header where the
188 * type/length field is <= ETHERMTU, i.e. it's a length
189 * field, not a type field), but has no 802.2 header -
190 * the IPX packet starts right after the Ethernet header,
191 * with a signature of two bytes of 0xFF (which is
192 * LLCSAP_GLOBAL).
193 *
194 * (It might also have been an Ethernet_802.3 IPX at
195 * one time, but got bridged onto another network,
196 * such as an 802.11 network; this has appeared in at
197 * least one capture file.)
198 */
199
200 if (eflag)
201 printf("IPX 802.3: ");
202
203 ipx_print(p, length);
204 return (1);
205 }
206
207 dsap = dsap_field & ~LLC_IG;
208 ssap = ssap_field & ~LLC_GSAP;
209
210 if (eflag) {
211 printf("LLC, dsap %s (0x%02x), ssap %s (0x%02x) %s",
212 tok2str(llc_values, "Unknown", dsap),
213 dsap,
214 tok2str(llc_values, "Unknown", ssap),
215 ssap,
216 tok2str(llc_flag_values, "Unknown", ssap_field & LLC_GSAP));
217
218 if (is_u) {
219 printf(", ctrl 0x%02x: ", control);
220 } else {
221 printf(", ctrl 0x%04x: ", control);
222 }
223 }
224
225 if (ssap == LLCSAP_8021D && dsap == LLCSAP_8021D &&
226 control == LLC_UI) {
227 stp_print(p+3, length-3);
228 return (1);
229 }
230
231 if (ssap == LLCSAP_IP && dsap == LLCSAP_IP &&
232 control == LLC_UI) {
233 ip_print(gndo, p+4, length-4);
234 return (1);
235 }
236
237 if (ssap == LLCSAP_IPX && dsap == LLCSAP_IPX &&
238 control == LLC_UI) {
239 /*
240 * This is an Ethernet_802.2 IPX frame, with an 802.3
241 * header and an 802.2 LLC header with the source and
242 * destination SAPs being the IPX SAP.
243 *
244 * Skip DSAP, LSAP, and control field.
245 */
246 if (eflag)
247 printf("IPX 802.2: ");
248
249 ipx_print(p+3, length-3);
250 return (1);
251 }
252
253 #ifdef TCPDUMP_DO_SMB
254 if (ssap == LLCSAP_NETBEUI && dsap == LLCSAP_NETBEUI
255 && (!(control & LLC_S_FMT) || control == LLC_U_FMT)) {
256 /*
257 * we don't actually have a full netbeui parser yet, but the
258 * smb parser can handle many smb-in-netbeui packets, which
259 * is very useful, so we call that
260 *
261 * We don't call it for S frames, however, just I frames
262 * (which are frames that don't have the low-order bit,
263 * LLC_S_FMT, set in the first byte of the control field)
264 * and UI frames (whose control field is just 3, LLC_U_FMT).
265 */
266
267 /*
268 * Skip the LLC header.
269 */
270 if (is_u) {
271 p += 3;
272 length -= 3;
273 caplen -= 3;
274 } else {
275 p += 4;
276 length -= 4;
277 caplen -= 4;
278 }
279 netbeui_print(control, p, length);
280 return (1);
281 }
282 #endif
283 if (ssap == LLCSAP_ISONS && dsap == LLCSAP_ISONS
284 && control == LLC_UI) {
285 isoclns_print(p + 3, length - 3, caplen - 3);
286 return (1);
287 }
288
289 if (ssap == LLCSAP_SNAP && dsap == LLCSAP_SNAP
290 && control == LLC_UI) {
291 /*
292 * XXX - what *is* the right bridge pad value here?
293 * Does anybody ever bridge one form of LAN traffic
294 * over a networking type that uses 802.2 LLC?
295 */
296 ret = snap_print(p+3, length-3, caplen-3, extracted_ethertype,
297 2);
298 if (ret)
299 return (ret);
300 }
301
302 if (!eflag) {
303 if (ssap == dsap) {
304 if (esrc == NULL || edst == NULL)
305 (void)printf("%s ", tok2str(llc_values, "Unknown DSAP 0x%02x", dsap));
306 else
307 (void)printf("%s > %s %s ",
308 etheraddr_string(esrc),
309 etheraddr_string(edst),
310 tok2str(llc_values, "Unknown DSAP 0x%02x", dsap));
311 } else {
312 if (esrc == NULL || edst == NULL)
313 (void)printf("%s > %s ",
314 tok2str(llc_values, "Unknown SSAP 0x%02x", ssap),
315 tok2str(llc_values, "Unknown DSAP 0x%02x", dsap));
316 else
317 (void)printf("%s %s > %s %s ",
318 etheraddr_string(esrc),
319 tok2str(llc_values, "Unknown SSAP 0x%02x", ssap),
320 etheraddr_string(edst),
321 tok2str(llc_values, "Unknown DSAP 0x%02x", dsap));
322 }
323 }
324
325 if (is_u) {
326 printf("Unnumbered, %s, Flags [%s], length %u",
327 tok2str(llc_cmd_values, "%02x", LLC_U_CMD(control)),
328 tok2str(llc_flag_values,"?",(ssap_field & LLC_GSAP) | (control & LLC_U_POLL)),
329 length);
330
331 p += 3;
332 length -= 3;
333 caplen -= 3;
334
335 if ((control & ~LLC_U_POLL) == LLC_XID) {
336 if (*p == LLC_XID_FI) {
337 printf(": %02x %02x", p[1], p[2]);
338 p += 3;
339 length -= 3;
340 caplen -= 3;
341 }
342 }
343 } else {
344 if ((control & LLC_S_FMT) == LLC_S_FMT) {
345 (void)printf("Supervisory, %s, rcv seq %u, Flags [%s], length %u",
346 tok2str(llc_supervisory_values,"?",LLC_S_CMD(control)),
347 LLC_IS_NR(control),
348 tok2str(llc_flag_values,"?",(ssap_field & LLC_GSAP) | (control & LLC_IS_POLL)),
349 length);
350 } else {
351 (void)printf("Information, send seq %u, rcv seq %u, Flags [%s], length %u",
352 LLC_I_NS(control),
353 LLC_IS_NR(control),
354 tok2str(llc_flag_values,"?",(ssap_field & LLC_GSAP) | (control & LLC_IS_POLL)),
355 length);
356 }
357 p += 4;
358 length -= 4;
359 caplen -= 4;
360 }
361 return(1);
362 }
363
364 int
365 snap_print(const u_char *p, u_int length, u_int caplen,
366 u_short *extracted_ethertype, u_int bridge_pad)
367 {
368 u_int32_t orgcode;
369 register u_short et;
370 register int ret;
371
372 TCHECK2(*p, 5);
373 orgcode = EXTRACT_24BITS(p);
374 et = EXTRACT_16BITS(p + 3);
375
376 if (eflag) {
377 const struct tok *tok = NULL;
378 const struct oui_tok *otp;
379
380 for (otp = &oui_to_tok[0]; otp->tok != NULL; otp++) {
381 if (otp->oui == orgcode) {
382 tok = otp->tok;
383 break;
384 }
385 }
386 (void)printf("oui %s (0x%06x), %s %s (0x%04x): ",
387 tok2str(oui_values, "Unknown", orgcode),
388 orgcode,
389 (orgcode == 0x000000 ? "ethertype" : "pid"),
390 tok2str(tok, "Unknown", et),
391 et);
392 }
393 p += 5;
394 length -= 5;
395 caplen -= 5;
396
397 switch (orgcode) {
398 case OUI_ENCAP_ETHER:
399 case OUI_CISCO_90:
400 /*
401 * This is an encapsulated Ethernet packet,
402 * or a packet bridged by some piece of
403 * Cisco hardware; the protocol ID is
404 * an Ethernet protocol type.
405 */
406 ret = ether_encap_print(et, p, length, caplen,
407 extracted_ethertype);
408 if (ret)
409 return (ret);
410 break;
411
412 case OUI_APPLETALK:
413 if (et == ETHERTYPE_ATALK) {
414 /*
415 * No, I have no idea why Apple used one
416 * of their own OUIs, rather than
417 * 0x000000, and an Ethernet packet
418 * type, for Appletalk data packets,
419 * but used 0x000000 and an Ethernet
420 * packet type for AARP packets.
421 */
422 ret = ether_encap_print(et, p, length, caplen,
423 extracted_ethertype);
424 if (ret)
425 return (ret);
426 }
427 break;
428
429 case OUI_CISCO:
430 if (et == PID_CISCO_CDP) {
431 cdp_print(p, length, caplen);
432 return (1);
433 }
434 break;
435
436 case OUI_RFC2684:
437 switch (et) {
438
439 case PID_RFC2684_ETH_FCS:
440 case PID_RFC2684_ETH_NOFCS:
441 /*
442 * XXX - remove the last two bytes for
443 * PID_RFC2684_ETH_FCS?
444 */
445 /*
446 * Skip the padding.
447 */
448 TCHECK2(*p, bridge_pad);
449 caplen -= bridge_pad;
450 length -= bridge_pad;
451 p += bridge_pad;
452
453 /*
454 * What remains is an Ethernet packet.
455 */
456 ether_print(p, length, caplen);
457 return (1);
458
459 case PID_RFC2684_802_5_FCS:
460 case PID_RFC2684_802_5_NOFCS:
461 /*
462 * XXX - remove the last two bytes for
463 * PID_RFC2684_ETH_FCS?
464 */
465 /*
466 * Skip the padding, but not the Access
467 * Control field.
468 */
469 TCHECK2(*p, bridge_pad);
470 caplen -= bridge_pad;
471 length -= bridge_pad;
472 p += bridge_pad;
473
474 /*
475 * What remains is an 802.5 Token Ring
476 * packet.
477 */
478 token_print(p, length, caplen);
479 return (1);
480
481 case PID_RFC2684_FDDI_FCS:
482 case PID_RFC2684_FDDI_NOFCS:
483 /*
484 * XXX - remove the last two bytes for
485 * PID_RFC2684_ETH_FCS?
486 */
487 /*
488 * Skip the padding.
489 */
490 TCHECK2(*p, bridge_pad + 1);
491 caplen -= bridge_pad + 1;
492 length -= bridge_pad + 1;
493 p += bridge_pad + 1;
494
495 /*
496 * What remains is an FDDI packet.
497 */
498 fddi_print(p, length, caplen);
499 return (1);
500
501 case PID_RFC2684_BPDU:
502 stp_print(p, length);
503 return (1);
504 }
505 }
506 return (0);
507
508 trunc:
509 (void)printf("[|snap]");
510 return (1);
511 }
512
513
514 /*
515 * Local Variables:
516 * c-style: whitesmith
517 * c-basic-offset: 8
518 * End:
519 */