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