xref: /openbsd-src/usr.sbin/tcpdump/print-null.c (revision 91f110e064cd7c194e59e019b83bb7496c1c84d4)
1 /*	$OpenBSD: print-null.c,v 1.19 2009/10/27 23:59:55 deraadt Exp $	*/
2 
3 /*
4  * Copyright (c) 1991, 1993, 1994, 1995, 1996, 1997
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that: (1) source code distributions
9  * retain the above copyright notice and this paragraph in its entirety, (2)
10  * distributions including binary code include the above copyright notice and
11  * this paragraph in its entirety in the documentation or other materials
12  * provided with the distribution, and (3) all advertising materials mentioning
13  * features or use of this software display the following acknowledgement:
14  * ``This product includes software developed by the University of California,
15  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
16  * the University nor the names of its contributors may be used to endorse
17  * or promote products derived from this software without specific prior
18  * written permission.
19  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
20  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
21  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
22  */
23 
24 #include <sys/param.h>
25 #include <sys/time.h>
26 #include <sys/socket.h>
27 #include <sys/file.h>
28 #include <sys/ioctl.h>
29 
30 struct mbuf;
31 struct rtentry;
32 #include <net/if.h>
33 
34 #include <netinet/in.h>
35 #include <netinet/in_systm.h>
36 #include <netinet/ip.h>
37 #include <netinet/if_ether.h>
38 #include <netinet/ip_var.h>
39 #include <netinet/udp.h>
40 #include <netinet/udp_var.h>
41 #include <netinet/tcp.h>
42 
43 #include <pcap.h>
44 #include <stdio.h>
45 #include <string.h>
46 
47 #ifdef INET6
48 #include <netinet/ip6.h>
49 #endif
50 
51 #include "interface.h"
52 #include "addrtoname.h"
53 
54 #ifndef AF_NS
55 #define AF_NS		6		/* XEROX NS protocols */
56 #endif
57 
58 /*
59  * The DLT_NULL packet header is 4 bytes long. It contains a host
60  * order 32 bit integer that specifies the family, e.g. AF_INET
61  */
62 #define	NULL_HDRLEN 4
63 
64 static void
65 null_print(const u_char *p, const struct ip *ip, u_int length)
66 {
67 	u_int family;
68 
69 	memcpy((char *)&family, (char *)p, sizeof(family));
70 
71 	if (nflag && family != AF_LINK) {
72 		/* XXX just dump the header */
73 		return;
74 	}
75 	switch (family) {
76 
77 	case AF_INET:
78 		printf("ip: ");
79 		break;
80 
81 #ifdef INET6
82 	case AF_INET6:
83 		printf("ip6: ");
84 		break;
85 #endif
86 
87 	case AF_NS:
88 		printf("ns: ");
89 		break;
90 
91 #ifdef __OpenBSD__
92 	case AF_LINK:
93 		ether_print(p + NULL_HDRLEN, length);
94 		break;
95 #endif
96 	case AF_MPLS:
97 		printf("mpls: ");
98 		break;
99 
100 	default:
101 		printf("AF %d: ", family);
102 		break;
103 	}
104 }
105 
106 void
107 loop_if_print(u_char *user, const struct pcap_pkthdr *h, const u_char *p)
108 {
109 	*(u_int *)p = ntohl(*(u_int *)p);
110 
111 	null_if_print(user, h, p);
112 }
113 
114 void
115 null_if_print(u_char *user, const struct pcap_pkthdr *h, const u_char *p)
116 {
117 	u_int length = h->len;
118 	u_int caplen = h->caplen;
119 	u_int family = *(u_int *)p;
120 
121 #ifdef __OpenBSD__
122 	struct ether_header *ep;
123 	u_short ether_type;
124 	extern u_short extracted_ethertype;
125 #endif
126 
127 	ts_print(&h->ts);
128 
129 	/*
130 	 * Some printers want to get back at the link level addresses,
131 	 * and/or check that they're not walking off the end of the packet.
132 	 * Rather than pass them all the way down, we set these globals.
133 	 */
134 	packetp = p;
135 	snapend = p + caplen;
136 
137 	length -= NULL_HDRLEN;
138 
139 	if (eflag)
140 		null_print(p, (struct ip *)(p + NULL_HDRLEN), length);
141 
142 	switch (family) {
143 	case AF_INET:
144 		ip_print(p + NULL_HDRLEN, length);
145 		break;
146 
147 #ifdef INET6
148 	case AF_INET6:
149 		ip6_print(p + NULL_HDRLEN, length);
150 		break;
151 #endif /*INET6*/
152 
153 	case AF_MPLS:
154 		mpls_print(p + NULL_HDRLEN, length);
155 		break;
156 
157 #ifdef __OpenBSD__
158 	case AF_LINK:
159 		if (caplen < sizeof(struct ether_header) + NULL_HDRLEN) {
160 			printf("[|ether]");
161 			goto out;
162 		}
163 
164 		length -= sizeof(struct ether_header);
165 		caplen -= sizeof(struct ether_header);
166 		ep = (struct ether_header *)(p + NULL_HDRLEN);
167 		p += NULL_HDRLEN + sizeof(struct ether_header);
168 		packetp += sizeof(struct ether_header);
169 		ether_type = ntohs(ep->ether_type);
170 
171 		extracted_ethertype = 0;
172 		if (ether_type <= ETHERMTU) {
173 			/* Try to print the LLC-layer header & higher layers */
174 			if (llc_print(p, length, caplen, ESRC(ep),
175 			    EDST(ep)) == 0) {
176 				/* ether_type not known, print raw packet */
177 				if (!eflag)
178 					ether_print((u_char *)ep, length);
179 				if (extracted_ethertype) {
180 					printf("(LLC %s) ",
181 					       etherproto_string(htons(extracted_ethertype)));
182 				}
183 				if (!xflag && !qflag)
184 					default_print(p, caplen);
185 			}
186 		} else if (ether_encap_print(ether_type, p, length,
187 		           caplen) == 0) {
188 			/* ether_type not known, print raw packet */
189 			if (!eflag)
190 				ether_print((u_char *)ep, length +
191 				    sizeof(*ep));
192 			if (!xflag && !qflag)
193 				default_print(p, caplen);
194 		}
195 		break;
196 #endif /* __OpenBSD__ */
197 	}
198 
199 	if (xflag)
200 		default_print((const u_char *)(packetp + NULL_HDRLEN),
201 		    caplen - NULL_HDRLEN);
202  out:
203 	putchar('\n');
204 }
205 
206