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