xref: /netbsd/external/bsd/tcpdump/dist/print-sll.c (revision 6550d01e)
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 #include <sys/cdefs.h>
22 #ifndef lint
23 #if 0
24 static const char rcsid[] _U_ =
25     "@(#) Header: /tcpdump/master/tcpdump/print-sll.c,v 1.19 2005-11-13 12:12:43 guy Exp (LBL)";
26 #else
27 __RCSID("$NetBSD: print-sll.c,v 1.2 2010/12/05 05:11:30 christos Exp $");
28 #endif
29 #endif
30 
31 #ifdef HAVE_CONFIG_H
32 #include "config.h"
33 #endif
34 
35 #include <tcpdump-stdinc.h>
36 
37 #include <stdio.h>
38 #include <string.h>
39 #include <pcap.h>
40 
41 #include "interface.h"
42 #include "addrtoname.h"
43 #include "ethertype.h"
44 #include "extract.h"
45 
46 #include "ether.h"
47 #include "sll.h"
48 
49 const struct tok sll_pkttype_values[] = {
50     { LINUX_SLL_HOST, "In" },
51     { LINUX_SLL_BROADCAST, "B" },
52     { LINUX_SLL_MULTICAST, "M" },
53     { LINUX_SLL_OTHERHOST, "P" },
54     { LINUX_SLL_OUTGOING, "Out" },
55     { 0, NULL}
56 };
57 
58 static inline void
59 sll_print(register const struct sll_header *sllp, u_int length)
60 {
61 	u_short ether_type;
62 
63         printf("%3s ",tok2str(sll_pkttype_values,"?",EXTRACT_16BITS(&sllp->sll_pkttype)));
64 
65 	/*
66 	 * XXX - check the link-layer address type value?
67 	 * For now, we just assume 6 means Ethernet.
68 	 * XXX - print others as strings of hex?
69 	 */
70 	if (EXTRACT_16BITS(&sllp->sll_halen) == 6)
71 		(void)printf("%s ", etheraddr_string(sllp->sll_addr));
72 
73 	if (!qflag) {
74 		ether_type = EXTRACT_16BITS(&sllp->sll_protocol);
75 
76 		if (ether_type <= ETHERMTU) {
77 			/*
78 			 * Not an Ethernet type; what type is it?
79 			 */
80 			switch (ether_type) {
81 
82 			case LINUX_SLL_P_802_3:
83 				/*
84 				 * Ethernet_802.3 IPX frame.
85 				 */
86 				(void)printf("802.3");
87 				break;
88 
89 			case LINUX_SLL_P_802_2:
90 				/*
91 				 * 802.2.
92 				 */
93 				(void)printf("802.2");
94 				break;
95 
96 			default:
97 				/*
98 				 * What is it?
99 				 */
100 				(void)printf("ethertype Unknown (0x%04x)",
101 				    ether_type);
102 				break;
103 			}
104 		} else {
105 			(void)printf("ethertype %s (0x%04x)",
106 			    tok2str(ethertype_values, "Unknown", ether_type),
107 			    ether_type);
108 		}
109 		(void)printf(", length %u: ", length);
110 	}
111 }
112 
113 /*
114  * This is the top level routine of the printer.  'p' points to the
115  * Linux "cooked capture" header of the packet, 'h->ts' is the timestamp,
116  * 'h->len' is the length of the packet off the wire, and 'h->caplen'
117  * is the number of bytes actually captured.
118  */
119 u_int
120 sll_if_print(const struct pcap_pkthdr *h, const u_char *p)
121 {
122 	u_int caplen = h->caplen;
123 	u_int length = h->len;
124 	register const struct sll_header *sllp;
125 	u_short ether_type;
126 	u_short extracted_ethertype;
127 
128 	if (caplen < SLL_HDR_LEN) {
129 		/*
130 		 * XXX - this "can't happen" because "pcap-linux.c" always
131 		 * adds this many bytes of header to every packet in a
132 		 * cooked socket capture.
133 		 */
134 		printf("[|sll]");
135 		return (caplen);
136 	}
137 
138 	sllp = (const struct sll_header *)p;
139 
140 	if (eflag)
141 		sll_print(sllp, length);
142 
143 	/*
144 	 * Go past the cooked-mode header.
145 	 */
146 	length -= SLL_HDR_LEN;
147 	caplen -= SLL_HDR_LEN;
148 	p += SLL_HDR_LEN;
149 
150 	ether_type = EXTRACT_16BITS(&sllp->sll_protocol);
151 
152 recurse:
153 	/*
154 	 * Is it (gag) an 802.3 encapsulation, or some non-Ethernet
155 	 * packet type?
156 	 */
157 	if (ether_type <= ETHERMTU) {
158 		/*
159 		 * Yes - what type is it?
160 		 */
161 		switch (ether_type) {
162 
163 		case LINUX_SLL_P_802_3:
164 			/*
165 			 * Ethernet_802.3 IPX frame.
166 			 */
167 			ipx_print(p, length);
168 			break;
169 
170 		case LINUX_SLL_P_802_2:
171 			/*
172 			 * 802.2.
173 			 * Try to print the LLC-layer header & higher layers.
174 			 */
175 			if (llc_print(p, length, caplen, NULL, NULL,
176 			    &extracted_ethertype) == 0)
177 				goto unknown;	/* unknown LLC type */
178 			break;
179 
180 		default:
181 			extracted_ethertype = 0;
182 			/*FALLTHROUGH*/
183 
184 		unknown:
185 			/* ether_type not known, print raw packet */
186 			if (!eflag)
187 				sll_print(sllp, length + SLL_HDR_LEN);
188 			if (extracted_ethertype) {
189 				printf("(LLC %s) ",
190 			       etherproto_string(htons(extracted_ethertype)));
191 			}
192 			if (!suppress_default_print)
193 				default_print(p, caplen);
194 			break;
195 		}
196 	} else if (ether_type == ETHERTYPE_8021Q) {
197 		/*
198 		 * Print VLAN information, and then go back and process
199 		 * the enclosed type field.
200 		 */
201 		if (caplen < 4 || length < 4) {
202 			printf("[|vlan]");
203 			return (SLL_HDR_LEN);
204 		}
205 	        if (eflag) {
206 	        	u_int16_t tag = EXTRACT_16BITS(p);
207 
208 			printf("vlan %u, p %u%s, ",
209 			    tag & 0xfff,
210 			    tag >> 13,
211 			    (tag & 0x1000) ? ", CFI" : "");
212 		}
213 
214 		ether_type = EXTRACT_16BITS(p + 2);
215 		if (ether_type <= ETHERMTU)
216 			ether_type = LINUX_SLL_P_802_2;
217 		if (!qflag) {
218 			(void)printf("ethertype %s, ",
219 			    tok2str(ethertype_values, "Unknown", ether_type));
220 		}
221 		p += 4;
222 		length -= 4;
223 		caplen -= 4;
224 		goto recurse;
225 	} else {
226 		if (ethertype_print(ether_type, p, length, caplen) == 0) {
227 			/* ether_type not known, print raw packet */
228 			if (!eflag)
229 				sll_print(sllp, length + SLL_HDR_LEN);
230 			if (!suppress_default_print)
231 				default_print(p, caplen);
232 		}
233 	}
234 
235 	return (SLL_HDR_LEN);
236 }
237