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