print-sll.c revision 356341
1139749Simp/*
2117632Sharti * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
3117632Sharti *	The Regents of the University of California.  All rights reserved.
4117632Sharti *
5117632Sharti * Redistribution and use in source and binary forms, with or without
6117632Sharti * modification, are permitted provided that: (1) source code distributions
7117632Sharti * retain the above copyright notice and this paragraph in its entirety, (2)
8117632Sharti * distributions including binary code include the above copyright notice and
9117632Sharti * this paragraph in its entirety in the documentation or other materials
10117632Sharti * provided with the distribution, and (3) all advertising materials mentioning
11117632Sharti * features or use of this software display the following acknowledgement:
12117632Sharti * ``This product includes software developed by the University of California,
13117632Sharti * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14117632Sharti * the University nor the names of its contributors may be used to endorse
15117632Sharti * or promote products derived from this software without specific prior
16117632Sharti * written permission.
17117632Sharti * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18117632Sharti * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19117632Sharti * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20117632Sharti */
21117632Sharti
22117632Sharti/* \summary: Linux cooked sockets capture printer */
23117632Sharti
24117632Sharti#ifdef HAVE_CONFIG_H
25117632Sharti#include "config.h"
26117632Sharti#endif
27117632Sharti
28117632Sharti#include <netdissect-stdinc.h>
29117632Sharti
30117632Sharti#include "netdissect.h"
31117632Sharti#include "addrtoname.h"
32117632Sharti#include "ethertype.h"
33117632Sharti#include "extract.h"
34117632Sharti
35117632Sharti#include "ether.h"
36117632Sharti
37117632Sharti/*
38117632Sharti * For captures on Linux cooked sockets, we construct a fake header
39119418Sobrien * that includes:
40117632Sharti *
41117632Sharti *	a 2-byte "packet type" which is one of:
42117632Sharti *
43117632Sharti *		LINUX_SLL_HOST		packet was sent to us
44117632Sharti *		LINUX_SLL_BROADCAST	packet was broadcast
45117632Sharti *		LINUX_SLL_MULTICAST	packet was multicast
46117632Sharti *		LINUX_SLL_OTHERHOST	packet was sent to somebody else
47117632Sharti *		LINUX_SLL_OUTGOING	packet was sent *by* us;
48117632Sharti *
49117632Sharti *	a 2-byte Ethernet protocol field;
50117632Sharti *
51117632Sharti *	a 2-byte link-layer type;
52117632Sharti *
53117632Sharti *	a 2-byte link-layer address length;
54117632Sharti *
55117632Sharti *	an 8-byte source link-layer address, whose actual length is
56117632Sharti *	specified by the previous value.
57117632Sharti *
58117632Sharti * All fields except for the link-layer address are in network byte order.
59117632Sharti *
60117632Sharti * DO NOT change the layout of this structure, or change any of the
61117632Sharti * LINUX_SLL_ values below.  If you must change the link-layer header
62117632Sharti * for a "cooked" Linux capture, introduce a new DLT_ type (ask
63117632Sharti * "tcpdump-workers@lists.tcpdump.org" for one, so that you don't give it
64117632Sharti * a value that collides with a value already being used), and use the
65117632Sharti * new header in captures of that type, so that programs that can
66117632Sharti * handle DLT_LINUX_SLL captures will continue to handle them correctly
67117632Sharti * without any change, and so that capture files with different headers
68117632Sharti * can be told apart and programs that read them can dissect the
69117632Sharti * packets in them.
70117632Sharti *
71117632Sharti * This structure, and the #defines below, must be the same in the
72117632Sharti * libpcap and tcpdump versions of "sll.h".
73117632Sharti */
74117632Sharti
75117632Sharti/*
76117632Sharti * A DLT_LINUX_SLL fake link-layer header.
77117632Sharti */
78117632Sharti#define SLL_HDR_LEN	16		/* total header length */
79117632Sharti#define SLL_ADDRLEN	8		/* length of address field */
80117632Sharti
81117632Shartistruct sll_header {
82117632Sharti	uint16_t	sll_pkttype;	/* packet type */
83117632Sharti	uint16_t	sll_hatype;	/* link-layer address type */
84117632Sharti	uint16_t	sll_halen;	/* link-layer address length */
85117632Sharti	uint8_t		sll_addr[SLL_ADDRLEN];	/* link-layer address */
86117632Sharti	uint16_t	sll_protocol;	/* protocol */
87117632Sharti};
88117632Sharti
89117632Sharti/*
90117632Sharti * The LINUX_SLL_ values for "sll_pkttype"; these correspond to the
91117632Sharti * PACKET_ values on Linux, but are defined here so that they're
92117632Sharti * available even on systems other than Linux, and so that they
93117632Sharti * don't change even if the PACKET_ values change.
94117632Sharti */
95117632Sharti#define LINUX_SLL_HOST		0
96117632Sharti#define LINUX_SLL_BROADCAST	1
97117632Sharti#define LINUX_SLL_MULTICAST	2
98117632Sharti#define LINUX_SLL_OTHERHOST	3
99117632Sharti#define LINUX_SLL_OUTGOING	4
100117632Sharti
101117632Sharti/*
102117632Sharti * The LINUX_SLL_ values for "sll_protocol"; these correspond to the
103117632Sharti * ETH_P_ values on Linux, but are defined here so that they're
104117632Sharti * available even on systems other than Linux.  We assume, for now,
105117632Sharti * that the ETH_P_ values won't change in Linux; if they do, then:
106117632Sharti *
107117632Sharti *	if we don't translate them in "pcap-linux.c", capture files
108117632Sharti *	won't necessarily be readable if captured on a system that
109117632Sharti *	defines ETH_P_ values that don't match these values;
110117632Sharti *
111117632Sharti *	if we do translate them in "pcap-linux.c", that makes life
112117632Sharti *	unpleasant for the BPF code generator, as the values you test
113117632Sharti *	for in the kernel aren't the values that you test for when
114117632Sharti *	reading a capture file, so the fixup code run on BPF programs
115117632Sharti *	handed to the kernel ends up having to do more work.
116117632Sharti *
117117632Sharti * Add other values here as necessary, for handling packet types that
118117632Sharti * might show up on non-Ethernet, non-802.x networks.  (Not all the ones
119117632Sharti * in the Linux "if_ether.h" will, I suspect, actually show up in
120117632Sharti * captures.)
121117632Sharti */
122117632Sharti#define LINUX_SLL_P_802_3	0x0001	/* Novell 802.3 frames without 802.2 LLC header */
123117632Sharti#define LINUX_SLL_P_802_2	0x0004	/* 802.2 frames (not D/I/X Ethernet) */
124117632Sharti
125117632Shartistatic const struct tok sll_pkttype_values[] = {
126117632Sharti    { LINUX_SLL_HOST, "In" },
127117632Sharti    { LINUX_SLL_BROADCAST, "B" },
128117632Sharti    { LINUX_SLL_MULTICAST, "M" },
129117632Sharti    { LINUX_SLL_OTHERHOST, "P" },
130117632Sharti    { LINUX_SLL_OUTGOING, "Out" },
131117632Sharti    { 0, NULL}
132117632Sharti};
133117632Sharti
134117632Shartistatic inline void
135117632Shartisll_print(netdissect_options *ndo, register const struct sll_header *sllp, u_int length)
136117632Sharti{
137117632Sharti	u_short ether_type;
138117632Sharti
139117632Sharti        ND_PRINT((ndo, "%3s ",tok2str(sll_pkttype_values,"?",EXTRACT_16BITS(&sllp->sll_pkttype))));
140117632Sharti
141117632Sharti	/*
142117632Sharti	 * XXX - check the link-layer address type value?
143117632Sharti	 * For now, we just assume 6 means Ethernet.
144117632Sharti	 * XXX - print others as strings of hex?
145117632Sharti	 */
146117632Sharti	if (EXTRACT_16BITS(&sllp->sll_halen) == 6)
147117632Sharti		ND_PRINT((ndo, "%s ", etheraddr_string(ndo, sllp->sll_addr)));
148117632Sharti
149117632Sharti	if (!ndo->ndo_qflag) {
150117632Sharti		ether_type = EXTRACT_16BITS(&sllp->sll_protocol);
151117632Sharti
152117632Sharti		if (ether_type <= ETHERMTU) {
153117632Sharti			/*
154117632Sharti			 * Not an Ethernet type; what type is it?
155117632Sharti			 */
156117632Sharti			switch (ether_type) {
157117632Sharti
158117632Sharti			case LINUX_SLL_P_802_3:
159117632Sharti				/*
160117632Sharti				 * Ethernet_802.3 IPX frame.
161117632Sharti				 */
162117632Sharti				ND_PRINT((ndo, "802.3"));
163117632Sharti				break;
164117632Sharti
165117632Sharti			case LINUX_SLL_P_802_2:
166117632Sharti				/*
167117632Sharti				 * 802.2.
168117632Sharti				 */
169117632Sharti				ND_PRINT((ndo, "802.2"));
170117632Sharti				break;
171117632Sharti
172117632Sharti			default:
173117632Sharti				/*
174117632Sharti				 * What is it?
175117632Sharti				 */
176117632Sharti				ND_PRINT((ndo, "ethertype Unknown (0x%04x)",
177117632Sharti				    ether_type));
178117632Sharti				break;
179117632Sharti			}
180117632Sharti		} else {
181117632Sharti			ND_PRINT((ndo, "ethertype %s (0x%04x)",
182117632Sharti			    tok2str(ethertype_values, "Unknown", ether_type),
183117632Sharti			    ether_type));
184117632Sharti		}
185117632Sharti		ND_PRINT((ndo, ", length %u: ", length));
186117632Sharti	}
187117632Sharti}
188117632Sharti
189117632Sharti/*
190117632Sharti * This is the top level routine of the printer.  'p' points to the
191117632Sharti * Linux "cooked capture" header of the packet, 'h->ts' is the timestamp,
192117632Sharti * 'h->len' is the length of the packet off the wire, and 'h->caplen'
193117632Sharti * is the number of bytes actually captured.
194117632Sharti */
195117632Shartiu_int
196117632Shartisll_if_print(netdissect_options *ndo, const struct pcap_pkthdr *h, const u_char *p)
197117632Sharti{
198117632Sharti	u_int caplen = h->caplen;
199117632Sharti	u_int length = h->len;
200117632Sharti	register const struct sll_header *sllp;
201117632Sharti	u_short hatype;
202117632Sharti	u_short ether_type;
203117632Sharti	int llc_hdrlen;
204117632Sharti	u_int hdrlen;
205117632Sharti
206117632Sharti	if (caplen < SLL_HDR_LEN) {
207117632Sharti		/*
208117632Sharti		 * XXX - this "can't happen" because "pcap-linux.c" always
209117632Sharti		 * adds this many bytes of header to every packet in a
210117632Sharti		 * cooked socket capture.
211117632Sharti		 */
212117632Sharti		ND_PRINT((ndo, "[|sll]"));
213117632Sharti		return (caplen);
214117632Sharti	}
215117632Sharti
216117632Sharti	sllp = (const struct sll_header *)p;
217117632Sharti
218117632Sharti	if (ndo->ndo_eflag)
219117632Sharti		sll_print(ndo, sllp, length);
220117632Sharti
221117632Sharti	/*
222117632Sharti	 * Go past the cooked-mode header.
223117632Sharti	 */
224117632Sharti	length -= SLL_HDR_LEN;
225117632Sharti	caplen -= SLL_HDR_LEN;
226117632Sharti	p += SLL_HDR_LEN;
227117632Sharti	hdrlen = SLL_HDR_LEN;
228117632Sharti
229117632Sharti	hatype = EXTRACT_16BITS(&sllp->sll_hatype);
230117632Sharti	switch (hatype) {
231117632Sharti
232117632Sharti	case 803:
233117632Sharti		/*
234117632Sharti		 * This is an packet with a radiotap header;
235117632Sharti		 * just dissect the payload as such.
236117632Sharti		 */
237117632Sharti		return (SLL_HDR_LEN + ieee802_11_radio_print(ndo, p, length, caplen));
238117632Sharti	}
239117632Sharti	ether_type = EXTRACT_16BITS(&sllp->sll_protocol);
240117632Sharti
241117632Shartirecurse:
242117632Sharti	/*
243117632Sharti	 * Is it (gag) an 802.3 encapsulation, or some non-Ethernet
244117632Sharti	 * packet type?
245117632Sharti	 */
246117632Sharti	if (ether_type <= ETHERMTU) {
247117632Sharti		/*
248117632Sharti		 * Yes - what type is it?
249117632Sharti		 */
250117632Sharti		switch (ether_type) {
251117632Sharti
252117632Sharti		case LINUX_SLL_P_802_3:
253117632Sharti			/*
254117632Sharti			 * Ethernet_802.3 IPX frame.
255117632Sharti			 */
256117632Sharti			ipx_print(ndo, p, length);
257117632Sharti			break;
258117632Sharti
259117632Sharti		case LINUX_SLL_P_802_2:
260117632Sharti			/*
261117632Sharti			 * 802.2.
262117632Sharti			 * Try to print the LLC-layer header & higher layers.
263117632Sharti			 */
264117632Sharti			llc_hdrlen = llc_print(ndo, p, length, caplen, NULL, NULL);
265117632Sharti			if (llc_hdrlen < 0)
266117632Sharti				goto unknown;	/* unknown LLC type */
267117632Sharti			hdrlen += llc_hdrlen;
268117632Sharti			break;
269117632Sharti
270117632Sharti		default:
271117632Sharti			/*FALLTHROUGH*/
272117632Sharti
273117632Sharti		unknown:
274117632Sharti			/* packet type not known, print raw packet */
275117632Sharti			if (!ndo->ndo_suppress_default_print)
276117632Sharti				ND_DEFAULTPRINT(p, caplen);
277117632Sharti			break;
278117632Sharti		}
279117632Sharti	} else if (ether_type == ETHERTYPE_8021Q) {
280117632Sharti		/*
281117632Sharti		 * Print VLAN information, and then go back and process
282117632Sharti		 * the enclosed type field.
283117632Sharti		 */
284117632Sharti		if (caplen < 4) {
285117632Sharti			ND_PRINT((ndo, "[|vlan]"));
286117632Sharti			return (hdrlen + caplen);
287117632Sharti		}
288117632Sharti		if (length < 4) {
289117632Sharti			ND_PRINT((ndo, "[|vlan]"));
290117632Sharti			return (hdrlen + length);
291117632Sharti		}
292117632Sharti	        if (ndo->ndo_eflag) {
293117632Sharti	        	uint16_t tag = EXTRACT_16BITS(p);
294117632Sharti
295117632Sharti			ND_PRINT((ndo, "%s, ", ieee8021q_tci_string(tag)));
296117632Sharti		}
297117632Sharti
298117632Sharti		ether_type = EXTRACT_16BITS(p + 2);
299117632Sharti		if (ether_type <= ETHERMTU)
300117632Sharti			ether_type = LINUX_SLL_P_802_2;
301117632Sharti		if (!ndo->ndo_qflag) {
302117632Sharti			ND_PRINT((ndo, "ethertype %s, ",
303117632Sharti			    tok2str(ethertype_values, "Unknown", ether_type)));
304117632Sharti		}
305117632Sharti		p += 4;
306117632Sharti		length -= 4;
307117632Sharti		caplen -= 4;
308117632Sharti		hdrlen += 4;
309117632Sharti		goto recurse;
310117632Sharti	} else {
311117632Sharti		if (ethertype_print(ndo, ether_type, p, length, caplen, NULL, NULL) == 0) {
312117632Sharti			/* ether_type not known, print raw packet */
313117632Sharti			if (!ndo->ndo_eflag)
314117632Sharti				sll_print(ndo, sllp, length + SLL_HDR_LEN);
315117632Sharti			if (!ndo->ndo_suppress_default_print)
316117632Sharti				ND_DEFAULTPRINT(p, caplen);
317117632Sharti		}
318117632Sharti	}
319117632Sharti
320117632Sharti	return (hdrlen);
321117632Sharti}
322117632Sharti