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