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