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