print-sll.c revision 1.7 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
22 #include <sys/cdefs.h>
23 #ifndef lint
24 __RCSID("$NetBSD: print-sll.c,v 1.7 2017/01/24 23:29:14 christos Exp $");
25 #endif
26
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30
31 #include <netdissect-stdinc.h>
32
33 #include "netdissect.h"
34 #include "addrtoname.h"
35 #include "ethertype.h"
36 #include "extract.h"
37
38 #include "ether.h"
39
40 /*
41 * For captures on Linux cooked sockets, we construct a fake header
42 * that includes:
43 *
44 * a 2-byte "packet type" which is one of:
45 *
46 * LINUX_SLL_HOST packet was sent to us
47 * LINUX_SLL_BROADCAST packet was broadcast
48 * LINUX_SLL_MULTICAST packet was multicast
49 * LINUX_SLL_OTHERHOST packet was sent to somebody else
50 * LINUX_SLL_OUTGOING packet was sent *by* us;
51 *
52 * a 2-byte Ethernet protocol field;
53 *
54 * a 2-byte link-layer type;
55 *
56 * a 2-byte link-layer address length;
57 *
58 * an 8-byte source link-layer address, whose actual length is
59 * specified by the previous value.
60 *
61 * All fields except for the link-layer address are in network byte order.
62 *
63 * DO NOT change the layout of this structure, or change any of the
64 * LINUX_SLL_ values below. If you must change the link-layer header
65 * for a "cooked" Linux capture, introduce a new DLT_ type (ask
66 * "tcpdump-workers (at) lists.tcpdump.org" for one, so that you don't give it
67 * a value that collides with a value already being used), and use the
68 * new header in captures of that type, so that programs that can
69 * handle DLT_LINUX_SLL captures will continue to handle them correctly
70 * without any change, and so that capture files with different headers
71 * can be told apart and programs that read them can dissect the
72 * packets in them.
73 *
74 * This structure, and the #defines below, must be the same in the
75 * libpcap and tcpdump versions of "sll.h".
76 */
77
78 /*
79 * A DLT_LINUX_SLL fake link-layer header.
80 */
81 #define SLL_HDR_LEN 16 /* total header length */
82 #define SLL_ADDRLEN 8 /* length of address field */
83
84 struct sll_header {
85 uint16_t sll_pkttype; /* packet type */
86 uint16_t sll_hatype; /* link-layer address type */
87 uint16_t sll_halen; /* link-layer address length */
88 uint8_t sll_addr[SLL_ADDRLEN]; /* link-layer address */
89 uint16_t sll_protocol; /* protocol */
90 };
91
92 /*
93 * The LINUX_SLL_ values for "sll_pkttype"; these correspond to the
94 * PACKET_ values on Linux, but are defined here so that they're
95 * available even on systems other than Linux, and so that they
96 * don't change even if the PACKET_ values change.
97 */
98 #define LINUX_SLL_HOST 0
99 #define LINUX_SLL_BROADCAST 1
100 #define LINUX_SLL_MULTICAST 2
101 #define LINUX_SLL_OTHERHOST 3
102 #define LINUX_SLL_OUTGOING 4
103
104 /*
105 * The LINUX_SLL_ values for "sll_protocol"; these correspond to the
106 * ETH_P_ values on Linux, but are defined here so that they're
107 * available even on systems other than Linux. We assume, for now,
108 * that the ETH_P_ values won't change in Linux; if they do, then:
109 *
110 * if we don't translate them in "pcap-linux.c", capture files
111 * won't necessarily be readable if captured on a system that
112 * defines ETH_P_ values that don't match these values;
113 *
114 * if we do translate them in "pcap-linux.c", that makes life
115 * unpleasant for the BPF code generator, as the values you test
116 * for in the kernel aren't the values that you test for when
117 * reading a capture file, so the fixup code run on BPF programs
118 * handed to the kernel ends up having to do more work.
119 *
120 * Add other values here as necessary, for handling packet types that
121 * might show up on non-Ethernet, non-802.x networks. (Not all the ones
122 * in the Linux "if_ether.h" will, I suspect, actually show up in
123 * captures.)
124 */
125 #define LINUX_SLL_P_802_3 0x0001 /* Novell 802.3 frames without 802.2 LLC header */
126 #define LINUX_SLL_P_802_2 0x0004 /* 802.2 frames (not D/I/X Ethernet) */
127
128 static const struct tok sll_pkttype_values[] = {
129 { LINUX_SLL_HOST, "In" },
130 { LINUX_SLL_BROADCAST, "B" },
131 { LINUX_SLL_MULTICAST, "M" },
132 { LINUX_SLL_OTHERHOST, "P" },
133 { LINUX_SLL_OUTGOING, "Out" },
134 { 0, NULL}
135 };
136
137 static inline void
138 sll_print(netdissect_options *ndo, register const struct sll_header *sllp, u_int length)
139 {
140 u_short ether_type;
141
142 ND_PRINT((ndo, "%3s ",tok2str(sll_pkttype_values,"?",EXTRACT_16BITS(&sllp->sll_pkttype))));
143
144 /*
145 * XXX - check the link-layer address type value?
146 * For now, we just assume 6 means Ethernet.
147 * XXX - print others as strings of hex?
148 */
149 if (EXTRACT_16BITS(&sllp->sll_halen) == 6)
150 ND_PRINT((ndo, "%s ", etheraddr_string(ndo, sllp->sll_addr)));
151
152 if (!ndo->ndo_qflag) {
153 ether_type = EXTRACT_16BITS(&sllp->sll_protocol);
154
155 if (ether_type <= ETHERMTU) {
156 /*
157 * Not an Ethernet type; what type is it?
158 */
159 switch (ether_type) {
160
161 case LINUX_SLL_P_802_3:
162 /*
163 * Ethernet_802.3 IPX frame.
164 */
165 ND_PRINT((ndo, "802.3"));
166 break;
167
168 case LINUX_SLL_P_802_2:
169 /*
170 * 802.2.
171 */
172 ND_PRINT((ndo, "802.2"));
173 break;
174
175 default:
176 /*
177 * What is it?
178 */
179 ND_PRINT((ndo, "ethertype Unknown (0x%04x)",
180 ether_type));
181 break;
182 }
183 } else {
184 ND_PRINT((ndo, "ethertype %s (0x%04x)",
185 tok2str(ethertype_values, "Unknown", ether_type),
186 ether_type));
187 }
188 ND_PRINT((ndo, ", length %u: ", length));
189 }
190 }
191
192 /*
193 * This is the top level routine of the printer. 'p' points to the
194 * Linux "cooked capture" header of the packet, 'h->ts' is the timestamp,
195 * 'h->len' is the length of the packet off the wire, and 'h->caplen'
196 * is the number of bytes actually captured.
197 */
198 u_int
199 sll_if_print(netdissect_options *ndo, const struct pcap_pkthdr *h, const u_char *p)
200 {
201 u_int caplen = h->caplen;
202 u_int length = h->len;
203 register const struct sll_header *sllp;
204 u_short ether_type;
205 int llc_hdrlen;
206 u_int hdrlen;
207
208 if (caplen < SLL_HDR_LEN) {
209 /*
210 * XXX - this "can't happen" because "pcap-linux.c" always
211 * adds this many bytes of header to every packet in a
212 * cooked socket capture.
213 */
214 ND_PRINT((ndo, "[|sll]"));
215 return (caplen);
216 }
217
218 sllp = (const struct sll_header *)p;
219
220 if (ndo->ndo_eflag)
221 sll_print(ndo, sllp, length);
222
223 /*
224 * Go past the cooked-mode header.
225 */
226 length -= SLL_HDR_LEN;
227 caplen -= SLL_HDR_LEN;
228 p += SLL_HDR_LEN;
229 hdrlen = SLL_HDR_LEN;
230
231 ether_type = EXTRACT_16BITS(&sllp->sll_protocol);
232
233 recurse:
234 /*
235 * Is it (gag) an 802.3 encapsulation, or some non-Ethernet
236 * packet type?
237 */
238 if (ether_type <= ETHERMTU) {
239 /*
240 * Yes - what type is it?
241 */
242 switch (ether_type) {
243
244 case LINUX_SLL_P_802_3:
245 /*
246 * Ethernet_802.3 IPX frame.
247 */
248 ipx_print(ndo, p, length);
249 break;
250
251 case LINUX_SLL_P_802_2:
252 /*
253 * 802.2.
254 * Try to print the LLC-layer header & higher layers.
255 */
256 llc_hdrlen = llc_print(ndo, p, length, caplen, NULL, NULL);
257 if (llc_hdrlen < 0)
258 goto unknown; /* unknown LLC type */
259 hdrlen += llc_hdrlen;
260 break;
261
262 default:
263 /*FALLTHROUGH*/
264
265 unknown:
266 /* packet type not known, print raw packet */
267 if (!ndo->ndo_suppress_default_print)
268 ND_DEFAULTPRINT(p, caplen);
269 break;
270 }
271 } else if (ether_type == ETHERTYPE_8021Q) {
272 /*
273 * Print VLAN information, and then go back and process
274 * the enclosed type field.
275 */
276 if (caplen < 4) {
277 ND_PRINT((ndo, "[|vlan]"));
278 return (hdrlen + caplen);
279 }
280 if (length < 4) {
281 ND_PRINT((ndo, "[|vlan]"));
282 return (hdrlen + length);
283 }
284 if (ndo->ndo_eflag) {
285 uint16_t tag = EXTRACT_16BITS(p);
286
287 ND_PRINT((ndo, "%s, ", ieee8021q_tci_string(tag)));
288 }
289
290 ether_type = EXTRACT_16BITS(p + 2);
291 if (ether_type <= ETHERMTU)
292 ether_type = LINUX_SLL_P_802_2;
293 if (!ndo->ndo_qflag) {
294 ND_PRINT((ndo, "ethertype %s, ",
295 tok2str(ethertype_values, "Unknown", ether_type)));
296 }
297 p += 4;
298 length -= 4;
299 caplen -= 4;
300 hdrlen += 4;
301 goto recurse;
302 } else {
303 if (ethertype_print(ndo, ether_type, p, length, caplen) == 0) {
304 /* ether_type not known, print raw packet */
305 if (!ndo->ndo_eflag)
306 sll_print(ndo, sllp, length + SLL_HDR_LEN);
307 if (!ndo->ndo_suppress_default_print)
308 ND_DEFAULTPRINT(p, caplen);
309 }
310 }
311
312 return (hdrlen);
313 }
314