]> The Tcpdump Group git mirrors - tcpdump/blob - print-ether.c
Clean up printing of LLC packets.
[tcpdump] / print-ether.c
1 /*
2 * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 2000
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
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25
26 #include <tcpdump-stdinc.h>
27
28 #include "interface.h"
29 #include "extract.h"
30 #include "addrtoname.h"
31 #include "ethertype.h"
32 #include "ether.h"
33
34 const struct tok ethertype_values[] = {
35 { ETHERTYPE_IP, "IPv4" },
36 { ETHERTYPE_MPLS, "MPLS unicast" },
37 { ETHERTYPE_MPLS_MULTI, "MPLS multicast" },
38 { ETHERTYPE_IPV6, "IPv6" },
39 { ETHERTYPE_8021Q, "802.1Q" },
40 { ETHERTYPE_8021Q9100, "802.1Q-9100" },
41 { ETHERTYPE_8021QinQ, "802.1Q-QinQ" },
42 { ETHERTYPE_8021Q9200, "802.1Q-9200" },
43 { ETHERTYPE_VMAN, "VMAN" },
44 { ETHERTYPE_PUP, "PUP" },
45 { ETHERTYPE_ARP, "ARP"},
46 { ETHERTYPE_REVARP, "Reverse ARP"},
47 { ETHERTYPE_NS, "NS" },
48 { ETHERTYPE_SPRITE, "Sprite" },
49 { ETHERTYPE_TRAIL, "Trail" },
50 { ETHERTYPE_MOPDL, "MOP DL" },
51 { ETHERTYPE_MOPRC, "MOP RC" },
52 { ETHERTYPE_DN, "DN" },
53 { ETHERTYPE_LAT, "LAT" },
54 { ETHERTYPE_SCA, "SCA" },
55 { ETHERTYPE_TEB, "TEB" },
56 { ETHERTYPE_LANBRIDGE, "Lanbridge" },
57 { ETHERTYPE_DECDNS, "DEC DNS" },
58 { ETHERTYPE_DECDTS, "DEC DTS" },
59 { ETHERTYPE_VEXP, "VEXP" },
60 { ETHERTYPE_VPROD, "VPROD" },
61 { ETHERTYPE_ATALK, "Appletalk" },
62 { ETHERTYPE_AARP, "Appletalk ARP" },
63 { ETHERTYPE_IPX, "IPX" },
64 { ETHERTYPE_PPP, "PPP" },
65 { ETHERTYPE_MPCP, "MPCP" },
66 { ETHERTYPE_SLOW, "Slow Protocols" },
67 { ETHERTYPE_PPPOED, "PPPoE D" },
68 { ETHERTYPE_PPPOES, "PPPoE S" },
69 { ETHERTYPE_EAPOL, "EAPOL" },
70 { ETHERTYPE_RRCP, "RRCP" },
71 { ETHERTYPE_MS_NLB_HB, "MS NLB heartbeat" },
72 { ETHERTYPE_JUMBO, "Jumbo" },
73 { ETHERTYPE_LOOPBACK, "Loopback" },
74 { ETHERTYPE_ISO, "OSI" },
75 { ETHERTYPE_GRE_ISO, "GRE-OSI" },
76 { ETHERTYPE_CFM_OLD, "CFM (old)" },
77 { ETHERTYPE_CFM, "CFM" },
78 { ETHERTYPE_IEEE1905_1, "IEEE1905.1" },
79 { ETHERTYPE_LLDP, "LLDP" },
80 { ETHERTYPE_TIPC, "TIPC"},
81 { ETHERTYPE_GEONET_OLD, "GeoNet (old)"},
82 { ETHERTYPE_GEONET, "GeoNet"},
83 { ETHERTYPE_CALM_FAST, "CALM FAST"},
84 { ETHERTYPE_AOE, "AoE" },
85 { 0, NULL}
86 };
87
88 static inline void
89 ether_hdr_print(netdissect_options *ndo,
90 const u_char *bp, u_int length)
91 {
92 register const struct ether_header *ep;
93 uint16_t ether_type;
94
95 ep = (const struct ether_header *)bp;
96
97 ND_PRINT((ndo, "%s > %s",
98 etheraddr_string(ndo, ESRC(ep)),
99 etheraddr_string(ndo, EDST(ep))));
100
101 ether_type = EXTRACT_16BITS(&ep->ether_type);
102 if (!ndo->ndo_qflag) {
103 if (ether_type <= ETHERMTU)
104 ND_PRINT((ndo, ", 802.3"));
105 else
106 ND_PRINT((ndo, ", ethertype %s (0x%04x)",
107 tok2str(ethertype_values,"Unknown", ether_type),
108 ether_type));
109 } else {
110 if (ether_type <= ETHERMTU)
111 ND_PRINT((ndo, ", 802.3"));
112 else
113 ND_PRINT((ndo, ", %s", tok2str(ethertype_values,"Unknown Ethertype (0x%04x)", ether_type)));
114 }
115
116 ND_PRINT((ndo, ", length %u: ", length));
117 }
118
119 /*
120 * Print an Ethernet frame.
121 * This might be encapsulated within another frame; we might be passed
122 * a pointer to a function that can print header information for that
123 * frame's protocol, and an argument to pass to that function.
124 */
125 void
126 ether_print(netdissect_options *ndo,
127 const u_char *p, u_int length, u_int caplen,
128 void (*print_encap_header)(netdissect_options *ndo, const u_char *), const u_char *encap_header_arg)
129 {
130 struct ether_header *ep;
131 u_int orig_length;
132 u_short ether_type;
133
134 if (caplen < ETHER_HDRLEN || length < ETHER_HDRLEN) {
135 ND_PRINT((ndo, "[|ether]"));
136 return;
137 }
138
139 if (ndo->ndo_eflag) {
140 if (print_encap_header != NULL)
141 (*print_encap_header)(ndo, encap_header_arg);
142 ether_hdr_print(ndo, p, length);
143 }
144 orig_length = length;
145
146 length -= ETHER_HDRLEN;
147 caplen -= ETHER_HDRLEN;
148 ep = (struct ether_header *)p;
149 p += ETHER_HDRLEN;
150
151 ether_type = EXTRACT_16BITS(&ep->ether_type);
152
153 recurse:
154 /*
155 * Is it (gag) an 802.3 encapsulation?
156 */
157 if (ether_type <= ETHERMTU) {
158 /* Try to print the LLC-layer header & higher layers */
159 if (llc_print(ndo, p, length, caplen, ESRC(ep), EDST(ep)) == 0) {
160 /* ether_type not known, print raw packet */
161 if (!ndo->ndo_suppress_default_print)
162 ND_DEFAULTPRINT(p, caplen);
163 }
164 } else if (ether_type == ETHERTYPE_8021Q ||
165 ether_type == ETHERTYPE_8021Q9100 ||
166 ether_type == ETHERTYPE_8021Q9200 ||
167 ether_type == ETHERTYPE_8021QinQ) {
168 /*
169 * Print VLAN information, and then go back and process
170 * the enclosed type field.
171 */
172 if (caplen < 4 || length < 4) {
173 ND_PRINT((ndo, "[|vlan]"));
174 return;
175 }
176 if (ndo->ndo_eflag) {
177 uint16_t tag = EXTRACT_16BITS(p);
178
179 ND_PRINT((ndo, "%s, ", ieee8021q_tci_string(tag)));
180 }
181
182 ether_type = EXTRACT_16BITS(p + 2);
183 if (ndo->ndo_eflag && ether_type > ETHERMTU)
184 ND_PRINT((ndo, "ethertype %s, ", tok2str(ethertype_values,"0x%04x", ether_type)));
185 p += 4;
186 length -= 4;
187 caplen -= 4;
188 goto recurse;
189 } else if (ether_type == ETHERTYPE_JUMBO) {
190 /*
191 * Alteon jumbo frames.
192 * See
193 *
194 * http://tools.ietf.org/html/draft-ietf-isis-ext-eth-01
195 *
196 * which indicates that, following the type field,
197 * there's an LLC header and payload.
198 */
199 /* Try to print the LLC-layer header & higher layers */
200 if (llc_print(ndo, p, length, caplen, ESRC(ep), EDST(ep)) == 0) {
201 /* ether_type not known, print raw packet */
202 if (!ndo->ndo_suppress_default_print)
203 ND_DEFAULTPRINT(p, caplen);
204 }
205 } else {
206 if (ethertype_print(ndo, ether_type, p, length, caplen) == 0) {
207 /* ether_type not known, print raw packet */
208 if (!ndo->ndo_eflag) {
209 if (print_encap_header != NULL)
210 (*print_encap_header)(ndo, encap_header_arg);
211 ether_hdr_print(ndo, (u_char *)ep, orig_length);
212 }
213
214 if (!ndo->ndo_suppress_default_print)
215 ND_DEFAULTPRINT(p, caplen);
216 }
217 }
218 }
219
220 /*
221 * This is the top level routine of the printer. 'p' points
222 * to the ether header of the packet, 'h->ts' is the timestamp,
223 * 'h->len' is the length of the packet off the wire, and 'h->caplen'
224 * is the number of bytes actually captured.
225 */
226 u_int
227 ether_if_print(netdissect_options *ndo, const struct pcap_pkthdr *h,
228 const u_char *p)
229 {
230 ether_print(ndo, p, h->len, h->caplen, NULL, NULL);
231
232 return (ETHER_HDRLEN);
233 }
234
235 /*
236 * This is the top level routine of the printer. 'p' points
237 * to the ether header of the packet, 'h->ts' is the timestamp,
238 * 'h->len' is the length of the packet off the wire, and 'h->caplen'
239 * is the number of bytes actually captured.
240 *
241 * This is for DLT_NETANALYZER, which has a 4-byte pseudo-header
242 * before the Ethernet header.
243 */
244 u_int
245 netanalyzer_if_print(netdissect_options *ndo, const struct pcap_pkthdr *h,
246 const u_char *p)
247 {
248 /*
249 * Fail if we don't have enough data for the Hilscher pseudo-header.
250 */
251 if (h->len < 4 || h->caplen < 4) {
252 ND_PRINT((ndo, "[|netanalyzer]"));
253 return (h->caplen);
254 }
255
256 /* Skip the pseudo-header. */
257 ether_print(ndo, p + 4, h->len - 4, h->caplen - 4, NULL, NULL);
258
259 return (4 + ETHER_HDRLEN);
260 }
261
262 /*
263 * This is the top level routine of the printer. 'p' points
264 * to the ether header of the packet, 'h->ts' is the timestamp,
265 * 'h->len' is the length of the packet off the wire, and 'h->caplen'
266 * is the number of bytes actually captured.
267 *
268 * This is for DLT_NETANALYZER_TRANSPARENT, which has a 4-byte
269 * pseudo-header, a 7-byte Ethernet preamble, and a 1-byte Ethernet SOF
270 * before the Ethernet header.
271 */
272 u_int
273 netanalyzer_transparent_if_print(netdissect_options *ndo,
274 const struct pcap_pkthdr *h,
275 const u_char *p)
276 {
277 /*
278 * Fail if we don't have enough data for the Hilscher pseudo-header,
279 * preamble, and SOF.
280 */
281 if (h->len < 12 || h->caplen < 12) {
282 ND_PRINT((ndo, "[|netanalyzer-transparent]"));
283 return (h->caplen);
284 }
285
286 /* Skip the pseudo-header, preamble, and SOF. */
287 ether_print(ndo, p + 12, h->len - 12, h->caplen - 12, NULL, NULL);
288
289 return (12 + ETHER_HDRLEN);
290 }
291
292 /*
293 * Prints the packet payload, given an Ethernet type code for the payload's
294 * protocol.
295 *
296 * Returns non-zero if it can do so, zero if the ethertype is unknown.
297 */
298
299 int
300 ethertype_print(netdissect_options *ndo,
301 u_short ether_type, const u_char *p,
302 u_int length, u_int caplen)
303 {
304 switch (ether_type) {
305
306 case ETHERTYPE_IP:
307 ip_print(ndo, p, length);
308 return (1);
309
310 case ETHERTYPE_IPV6:
311 ip6_print(ndo, p, length);
312 return (1);
313
314 case ETHERTYPE_ARP:
315 case ETHERTYPE_REVARP:
316 arp_print(ndo, p, length, caplen);
317 return (1);
318
319 case ETHERTYPE_DN:
320 decnet_print(ndo, p, length, caplen);
321 return (1);
322
323 case ETHERTYPE_ATALK:
324 if (ndo->ndo_vflag)
325 ND_PRINT((ndo, "et1 "));
326 atalk_print(ndo, p, length);
327 return (1);
328
329 case ETHERTYPE_AARP:
330 aarp_print(ndo, p, length);
331 return (1);
332
333 case ETHERTYPE_IPX:
334 ND_PRINT((ndo, "(NOV-ETHII) "));
335 ipx_print(ndo, p, length);
336 return (1);
337
338 case ETHERTYPE_ISO:
339 isoclns_print(ndo, p + 1, length - 1, length - 1);
340 return(1);
341
342 case ETHERTYPE_PPPOED:
343 case ETHERTYPE_PPPOES:
344 case ETHERTYPE_PPPOED2:
345 case ETHERTYPE_PPPOES2:
346 pppoe_print(ndo, p, length);
347 return (1);
348
349 case ETHERTYPE_EAPOL:
350 eap_print(ndo, p, length);
351 return (1);
352
353 case ETHERTYPE_RRCP:
354 rrcp_print(ndo, p - 14 , length + 14);
355 return (1);
356
357 case ETHERTYPE_PPP:
358 if (length) {
359 ND_PRINT((ndo, ": "));
360 ppp_print(ndo, p, length);
361 }
362 return (1);
363
364 case ETHERTYPE_MPCP:
365 mpcp_print(ndo, p, length);
366 return (1);
367
368 case ETHERTYPE_SLOW:
369 slow_print(ndo, p, length);
370 return (1);
371
372 case ETHERTYPE_CFM:
373 case ETHERTYPE_CFM_OLD:
374 cfm_print(ndo, p, length);
375 return (1);
376
377 case ETHERTYPE_LLDP:
378 lldp_print(ndo, p, length);
379 return (1);
380
381 case ETHERTYPE_LOOPBACK:
382 loopback_print(ndo, p, length);
383 return (1);
384
385 case ETHERTYPE_MPLS:
386 case ETHERTYPE_MPLS_MULTI:
387 mpls_print(ndo, p, length);
388 return (1);
389
390 case ETHERTYPE_TIPC:
391 tipc_print(ndo, p, length, caplen);
392 return (1);
393
394 case ETHERTYPE_MS_NLB_HB:
395 msnlb_print(ndo, p);
396 return (1);
397
398 case ETHERTYPE_GEONET_OLD:
399 case ETHERTYPE_GEONET:
400 geonet_print(ndo, p-14, p, length);
401 return (1);
402
403 case ETHERTYPE_CALM_FAST:
404 calm_fast_print(ndo, p-14, p, length);
405 return (1);
406
407 case ETHERTYPE_AOE:
408 aoe_print(ndo, p, length);
409 return (1);
410
411 case ETHERTYPE_LAT:
412 case ETHERTYPE_SCA:
413 case ETHERTYPE_MOPRC:
414 case ETHERTYPE_MOPDL:
415 case ETHERTYPE_IEEE1905_1:
416 /* default_print for now */
417 default:
418 return (0);
419 }
420 }
421
422
423 /*
424 * Local Variables:
425 * c-style: whitesmith
426 * c-basic-offset: 8
427 * End:
428 */
429