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