OSDN Git Service

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