xref: /freebsd/contrib/tcpdump/print-sll.c (revision 233f3abd968de36697eb5d7fecee1df6f163590e)
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.16 2004/10/28 00:34:29 hannes 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         printf("%3s ",tok2str(sll_pkttype_values,"?",EXTRACT_16BITS(&sllp->sll_pkttype)));
57 
58 	/*
59 	 * XXX - check the link-layer address type value?
60 	 * For now, we just assume 6 means Ethernet.
61 	 * XXX - print others as strings of hex?
62 	 */
63 	if (EXTRACT_16BITS(&sllp->sll_halen) == 6)
64 		(void)printf("%s ", etheraddr_string(sllp->sll_addr));
65 
66 	if (!qflag)
67                 (void)printf("ethertype %s (0x%04x), length %u: ",
68                          tok2str(ethertype_values,"Unknown", EXTRACT_16BITS(&sllp->sll_protocol)),
69                          EXTRACT_16BITS(&sllp->sll_protocol),
70                          length);
71 }
72 
73 /*
74  * This is the top level routine of the printer.  'p' points to the
75  * Linux "cooked capture" header of the packet, 'h->ts' is the timestamp,
76  * 'h->len' is the length of the packet off the wire, and 'h->caplen'
77  * is the number of bytes actually captured.
78  */
79 u_int
80 sll_if_print(const struct pcap_pkthdr *h, const u_char *p)
81 {
82 	u_int caplen = h->caplen;
83 	u_int length = h->len;
84 	register const struct sll_header *sllp;
85 	u_short ether_type;
86 	u_short extracted_ethertype;
87 
88 	if (caplen < SLL_HDR_LEN) {
89 		/*
90 		 * XXX - this "can't happen" because "pcap-linux.c" always
91 		 * adds this many bytes of header to every packet in a
92 		 * cooked socket capture.
93 		 */
94 		printf("[|sll]");
95 		return (caplen);
96 	}
97 
98 	sllp = (const struct sll_header *)p;
99 
100 	if (eflag)
101 		sll_print(sllp, length);
102 
103 	/*
104 	 * Go past the cooked-mode header.
105 	 */
106 	length -= SLL_HDR_LEN;
107 	caplen -= SLL_HDR_LEN;
108 	p += SLL_HDR_LEN;
109 
110 	ether_type = ntohs(sllp->sll_protocol);
111 
112 	/*
113 	 * Is it (gag) an 802.3 encapsulation, or some non-Ethernet
114 	 * packet type?
115 	 */
116 	extracted_ethertype = 0;
117 	if (ether_type <= ETHERMTU) {
118 		/*
119 		 * Yes - what type is it?
120 		 */
121 		switch (ether_type) {
122 
123 		case LINUX_SLL_P_802_3:
124 			/*
125 			 * Ethernet_802.3 IPX frame.
126 			 */
127 			ipx_print(p, length);
128 			break;
129 
130 		case LINUX_SLL_P_802_2:
131 			/*
132 			 * 802.2.
133 			 * Try to print the LLC-layer header & higher layers.
134 			 */
135 			if (llc_print(p, length, caplen, NULL, NULL,
136 			    &extracted_ethertype) == 0)
137 				goto unknown;	/* unknown LLC type */
138 			break;
139 
140 		default:
141 		unknown:
142 			/* ether_type not known, print raw packet */
143 			if (!eflag)
144 				sll_print(sllp, length + SLL_HDR_LEN);
145 			if (extracted_ethertype) {
146 				printf("(LLC %s) ",
147 			       etherproto_string(htons(extracted_ethertype)));
148 			}
149 			if (!xflag && !qflag)
150 				default_print(p, caplen);
151 			break;
152 		}
153 	} else if (ether_encap_print(ether_type, p, length, caplen,
154 	    &extracted_ethertype) == 0) {
155 		/* ether_type not known, print raw packet */
156 		if (!eflag)
157 			sll_print(sllp, length + SLL_HDR_LEN);
158 		if (!xflag && !qflag)
159 			default_print(p, caplen);
160 	}
161 
162 	return (SLL_HDR_LEN);
163 }
164