print-ether.c revision 53146
1/*
2 * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
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 * $FreeBSD: head/contrib/tcpdump/print-ether.c 53146 1999-11-14 11:28:11Z brian $
22 */
23#ifndef lint
24static const char rcsid[] =
25    "@(#) $Header: print-ether.c,v 1.44 97/05/26 17:18:13 leres Exp $ (LBL)";
26#endif
27
28#include <sys/param.h>
29#include <sys/time.h>
30#include <sys/socket.h>
31
32#if __STDC__
33struct mbuf;
34struct rtentry;
35#endif
36#include <net/if.h>
37
38#include <netinet/in.h>
39#include <net/ethernet.h>
40#include <netinet/in_systm.h>
41#include <netinet/ip.h>
42#include <netinet/ip_var.h>
43#include <netinet/udp.h>
44#include <netinet/udp_var.h>
45#include <netinet/tcp.h>
46#include <netinet/tcpip.h>
47
48#include <stdio.h>
49#include <pcap.h>
50
51#include "interface.h"
52#include "addrtoname.h"
53#include "ethertype.h"
54
55const u_char *packetp;
56const u_char *snapend;
57
58static inline void
59ether_print(register const u_char *bp, u_int length)
60{
61	register const struct ether_header *ep;
62
63	ep = (const struct ether_header *)bp;
64	if (qflag)
65		(void)printf("%s %s %d: ",
66			     etheraddr_string(ESRC(ep)),
67			     etheraddr_string(EDST(ep)),
68			     length);
69	else
70		(void)printf("%s %s %s %d: ",
71			     etheraddr_string(ESRC(ep)),
72			     etheraddr_string(EDST(ep)),
73			     etherproto_string(ep->ether_type),
74			     length);
75}
76
77/*
78 * This is the top level routine of the printer.  'p' is the points
79 * to the ether header of the packet, 'tvp' is the timestamp,
80 * 'length' is the length of the packet off the wire, and 'caplen'
81 * is the number of bytes actually captured.
82 */
83void
84ether_if_print(u_char *user, const struct pcap_pkthdr *h, const u_char *p)
85{
86	u_int caplen = h->caplen;
87	u_int length = h->len;
88	struct ether_header *ep;
89	u_short ether_type;
90	extern u_short extracted_ethertype;
91
92	ts_print(&h->ts);
93
94	if (caplen < sizeof(struct ether_header)) {
95		printf("[|ether]");
96		goto out;
97	}
98
99	if (eflag)
100		ether_print(p, length);
101
102	/*
103	 * Some printers want to get back at the ethernet addresses,
104	 * and/or check that they're not walking off the end of the packet.
105	 * Rather than pass them all the way down, we set these globals.
106	 */
107	packetp = p;
108	snapend = p + caplen;
109
110	length -= sizeof(struct ether_header);
111	caplen -= sizeof(struct ether_header);
112	ep = (struct ether_header *)p;
113	p += sizeof(struct ether_header);
114
115	ether_type = ntohs(ep->ether_type);
116
117	/*
118	 * Is it (gag) an 802.3 encapsulation?
119	 */
120	extracted_ethertype = 0;
121	if (ether_type <= ETHERMTU) {
122		/* Try to print the LLC-layer header & higher layers */
123		if (llc_print(p, length, caplen, ESRC(ep), EDST(ep)) == 0) {
124			/* ether_type not known, print raw packet */
125			if (!eflag)
126				ether_print((u_char *)ep, length);
127			if (extracted_ethertype) {
128				printf("(LLC %s) ",
129			       etherproto_string(htons(extracted_ethertype)));
130			}
131			if (!xflag && !qflag)
132				default_print(p, caplen);
133		}
134	} else if (ether_encap_print(ether_type, p, length, caplen) == 0) {
135		/* ether_type not known, print raw packet */
136		if (!eflag)
137			ether_print((u_char *)ep, length + sizeof(*ep));
138		if (!xflag && !qflag)
139			default_print(p, caplen);
140	}
141	if (xflag)
142		default_print(p, caplen);
143 out:
144	putchar('\n');
145}
146
147/*
148 * Prints the packet encapsulated in an Ethernet data segment
149 * (or an equivalent encapsulation), given the Ethernet type code.
150 *
151 * Returns non-zero if it can do so, zero if the ethertype is unknown.
152 *
153 * Stuffs the ether type into a global for the benefit of lower layers
154 * that might want to know what it is.
155 */
156
157u_short	extracted_ethertype;
158
159int
160ether_encap_print(u_short ethertype, const u_char *p,
161    u_int length, u_int caplen)
162{
163	if (ethertype == 0x8100) { /* IEEE 802.1Q vlan tagging encapsulation */
164		printf ("[vlan %d] ", ntohs(*(u_short *)p) & 0x17ff);
165		ethertype = ntohs(((u_short *)p)[1]);
166		p += 4;
167		length -= 4;
168		caplen -= 4;
169	}
170	extracted_ethertype = ethertype;
171
172	switch (ethertype) {
173
174	case ETHERTYPE_IP:
175		ip_print(p, length);
176		return (1);
177
178	case ETHERTYPE_ARP:
179	case ETHERTYPE_REVARP:
180		arp_print(p, length, caplen);
181		return (1);
182
183	case ETHERTYPE_DN:
184		decnet_print(p, length, caplen);
185		return (1);
186
187	case ETHERTYPE_ATALK:
188		if (vflag)
189			fputs("et1 ", stdout);
190		atalk_print(p, length);
191		return (1);
192
193	case ETHERTYPE_AARP:
194		aarp_print(p, length);
195		return (1);
196
197	case ETHERTYPE_IPX:
198		ipx_print(p, length);
199		return (1);
200
201	case ETHERTYPE_PPPOE_DISC:
202	case ETHERTYPE_PPPOE_SESS:
203		pppoe_print(p, length);
204		return (1);
205
206	case ETHERTYPE_LAT:
207	case ETHERTYPE_SCA:
208	case ETHERTYPE_MOPRC:
209	case ETHERTYPE_MOPDL:
210		/* default_print for now */
211	default:
212		return (0);
213	}
214}
215