print-sll.c revision 1.2.6.1 1 1.1 christos /*
2 1.1 christos * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
3 1.1 christos * The Regents of the University of California. All rights reserved.
4 1.1 christos *
5 1.1 christos * Redistribution and use in source and binary forms, with or without
6 1.1 christos * modification, are permitted provided that: (1) source code distributions
7 1.1 christos * retain the above copyright notice and this paragraph in its entirety, (2)
8 1.1 christos * distributions including binary code include the above copyright notice and
9 1.1 christos * this paragraph in its entirety in the documentation or other materials
10 1.1 christos * provided with the distribution, and (3) all advertising materials mentioning
11 1.1 christos * features or use of this software display the following acknowledgement:
12 1.1 christos * ``This product includes software developed by the University of California,
13 1.1 christos * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14 1.1 christos * the University nor the names of its contributors may be used to endorse
15 1.1 christos * or promote products derived from this software without specific prior
16 1.1 christos * written permission.
17 1.1 christos * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18 1.1 christos * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19 1.1 christos * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20 1.1 christos */
21 1.2 christos #include <sys/cdefs.h>
22 1.1 christos #ifndef lint
23 1.2 christos #if 0
24 1.1 christos static const char rcsid[] _U_ =
25 1.2.6.1 yamt "@(#) Header: /tcpdump/master/tcpdump/print-sll.c,v 1.19 2005-11-13 12:12:43 guy Exp (LBL)";
26 1.2 christos #else
27 1.2.6.1 yamt __RCSID("$NetBSD: print-sll.c,v 1.2.6.1 2014/05/22 15:51:20 yamt Exp $");
28 1.2 christos #endif
29 1.1 christos #endif
30 1.1 christos
31 1.1 christos #ifdef HAVE_CONFIG_H
32 1.1 christos #include "config.h"
33 1.1 christos #endif
34 1.1 christos
35 1.1 christos #include <tcpdump-stdinc.h>
36 1.1 christos
37 1.1 christos #include <stdio.h>
38 1.1 christos #include <string.h>
39 1.1 christos #include <pcap.h>
40 1.1 christos
41 1.1 christos #include "interface.h"
42 1.1 christos #include "addrtoname.h"
43 1.1 christos #include "ethertype.h"
44 1.1 christos #include "extract.h"
45 1.1 christos
46 1.1 christos #include "ether.h"
47 1.1 christos #include "sll.h"
48 1.1 christos
49 1.2.6.1 yamt static const struct tok sll_pkttype_values[] = {
50 1.1 christos { LINUX_SLL_HOST, "In" },
51 1.1 christos { LINUX_SLL_BROADCAST, "B" },
52 1.1 christos { LINUX_SLL_MULTICAST, "M" },
53 1.1 christos { LINUX_SLL_OTHERHOST, "P" },
54 1.1 christos { LINUX_SLL_OUTGOING, "Out" },
55 1.1 christos { 0, NULL}
56 1.1 christos };
57 1.1 christos
58 1.1 christos static inline void
59 1.1 christos sll_print(register const struct sll_header *sllp, u_int length)
60 1.1 christos {
61 1.1 christos u_short ether_type;
62 1.1 christos
63 1.1 christos printf("%3s ",tok2str(sll_pkttype_values,"?",EXTRACT_16BITS(&sllp->sll_pkttype)));
64 1.1 christos
65 1.1 christos /*
66 1.1 christos * XXX - check the link-layer address type value?
67 1.1 christos * For now, we just assume 6 means Ethernet.
68 1.1 christos * XXX - print others as strings of hex?
69 1.1 christos */
70 1.1 christos if (EXTRACT_16BITS(&sllp->sll_halen) == 6)
71 1.1 christos (void)printf("%s ", etheraddr_string(sllp->sll_addr));
72 1.1 christos
73 1.1 christos if (!qflag) {
74 1.1 christos ether_type = EXTRACT_16BITS(&sllp->sll_protocol);
75 1.1 christos
76 1.1 christos if (ether_type <= ETHERMTU) {
77 1.1 christos /*
78 1.1 christos * Not an Ethernet type; what type is it?
79 1.1 christos */
80 1.1 christos switch (ether_type) {
81 1.1 christos
82 1.1 christos case LINUX_SLL_P_802_3:
83 1.1 christos /*
84 1.1 christos * Ethernet_802.3 IPX frame.
85 1.1 christos */
86 1.1 christos (void)printf("802.3");
87 1.1 christos break;
88 1.1 christos
89 1.1 christos case LINUX_SLL_P_802_2:
90 1.1 christos /*
91 1.1 christos * 802.2.
92 1.1 christos */
93 1.1 christos (void)printf("802.2");
94 1.1 christos break;
95 1.1 christos
96 1.1 christos default:
97 1.1 christos /*
98 1.1 christos * What is it?
99 1.1 christos */
100 1.1 christos (void)printf("ethertype Unknown (0x%04x)",
101 1.1 christos ether_type);
102 1.1 christos break;
103 1.1 christos }
104 1.1 christos } else {
105 1.1 christos (void)printf("ethertype %s (0x%04x)",
106 1.1 christos tok2str(ethertype_values, "Unknown", ether_type),
107 1.1 christos ether_type);
108 1.1 christos }
109 1.1 christos (void)printf(", length %u: ", length);
110 1.1 christos }
111 1.1 christos }
112 1.1 christos
113 1.1 christos /*
114 1.1 christos * This is the top level routine of the printer. 'p' points to the
115 1.1 christos * Linux "cooked capture" header of the packet, 'h->ts' is the timestamp,
116 1.1 christos * 'h->len' is the length of the packet off the wire, and 'h->caplen'
117 1.1 christos * is the number of bytes actually captured.
118 1.1 christos */
119 1.1 christos u_int
120 1.1 christos sll_if_print(const struct pcap_pkthdr *h, const u_char *p)
121 1.1 christos {
122 1.1 christos u_int caplen = h->caplen;
123 1.1 christos u_int length = h->len;
124 1.1 christos register const struct sll_header *sllp;
125 1.1 christos u_short ether_type;
126 1.1 christos u_short extracted_ethertype;
127 1.1 christos
128 1.1 christos if (caplen < SLL_HDR_LEN) {
129 1.1 christos /*
130 1.1 christos * XXX - this "can't happen" because "pcap-linux.c" always
131 1.1 christos * adds this many bytes of header to every packet in a
132 1.1 christos * cooked socket capture.
133 1.1 christos */
134 1.1 christos printf("[|sll]");
135 1.1 christos return (caplen);
136 1.1 christos }
137 1.1 christos
138 1.1 christos sllp = (const struct sll_header *)p;
139 1.1 christos
140 1.1 christos if (eflag)
141 1.1 christos sll_print(sllp, length);
142 1.1 christos
143 1.1 christos /*
144 1.1 christos * Go past the cooked-mode header.
145 1.1 christos */
146 1.1 christos length -= SLL_HDR_LEN;
147 1.1 christos caplen -= SLL_HDR_LEN;
148 1.1 christos p += SLL_HDR_LEN;
149 1.1 christos
150 1.1 christos ether_type = EXTRACT_16BITS(&sllp->sll_protocol);
151 1.1 christos
152 1.1 christos recurse:
153 1.1 christos /*
154 1.1 christos * Is it (gag) an 802.3 encapsulation, or some non-Ethernet
155 1.1 christos * packet type?
156 1.1 christos */
157 1.1 christos if (ether_type <= ETHERMTU) {
158 1.1 christos /*
159 1.1 christos * Yes - what type is it?
160 1.1 christos */
161 1.1 christos switch (ether_type) {
162 1.1 christos
163 1.1 christos case LINUX_SLL_P_802_3:
164 1.1 christos /*
165 1.1 christos * Ethernet_802.3 IPX frame.
166 1.1 christos */
167 1.1 christos ipx_print(p, length);
168 1.1 christos break;
169 1.1 christos
170 1.1 christos case LINUX_SLL_P_802_2:
171 1.1 christos /*
172 1.1 christos * 802.2.
173 1.1 christos * Try to print the LLC-layer header & higher layers.
174 1.1 christos */
175 1.1 christos if (llc_print(p, length, caplen, NULL, NULL,
176 1.1 christos &extracted_ethertype) == 0)
177 1.1 christos goto unknown; /* unknown LLC type */
178 1.1 christos break;
179 1.1 christos
180 1.1 christos default:
181 1.1 christos extracted_ethertype = 0;
182 1.1 christos /*FALLTHROUGH*/
183 1.1 christos
184 1.1 christos unknown:
185 1.1 christos /* ether_type not known, print raw packet */
186 1.1 christos if (!eflag)
187 1.1 christos sll_print(sllp, length + SLL_HDR_LEN);
188 1.1 christos if (extracted_ethertype) {
189 1.1 christos printf("(LLC %s) ",
190 1.1 christos etherproto_string(htons(extracted_ethertype)));
191 1.1 christos }
192 1.1 christos if (!suppress_default_print)
193 1.1 christos default_print(p, caplen);
194 1.1 christos break;
195 1.1 christos }
196 1.1 christos } else if (ether_type == ETHERTYPE_8021Q) {
197 1.1 christos /*
198 1.1 christos * Print VLAN information, and then go back and process
199 1.1 christos * the enclosed type field.
200 1.1 christos */
201 1.1 christos if (caplen < 4 || length < 4) {
202 1.1 christos printf("[|vlan]");
203 1.1 christos return (SLL_HDR_LEN);
204 1.1 christos }
205 1.1 christos if (eflag) {
206 1.1 christos u_int16_t tag = EXTRACT_16BITS(p);
207 1.1 christos
208 1.1 christos printf("vlan %u, p %u%s, ",
209 1.1 christos tag & 0xfff,
210 1.1 christos tag >> 13,
211 1.1 christos (tag & 0x1000) ? ", CFI" : "");
212 1.1 christos }
213 1.1 christos
214 1.1 christos ether_type = EXTRACT_16BITS(p + 2);
215 1.1 christos if (ether_type <= ETHERMTU)
216 1.1 christos ether_type = LINUX_SLL_P_802_2;
217 1.1 christos if (!qflag) {
218 1.1 christos (void)printf("ethertype %s, ",
219 1.1 christos tok2str(ethertype_values, "Unknown", ether_type));
220 1.1 christos }
221 1.1 christos p += 4;
222 1.1 christos length -= 4;
223 1.1 christos caplen -= 4;
224 1.1 christos goto recurse;
225 1.1 christos } else {
226 1.2.6.1 yamt if (ethertype_print(gndo, ether_type, p, length, caplen) == 0) {
227 1.1 christos /* ether_type not known, print raw packet */
228 1.1 christos if (!eflag)
229 1.1 christos sll_print(sllp, length + SLL_HDR_LEN);
230 1.1 christos if (!suppress_default_print)
231 1.1 christos default_print(p, caplen);
232 1.1 christos }
233 1.1 christos }
234 1.1 christos
235 1.1 christos return (SLL_HDR_LEN);
236 1.1 christos }
237