1*0036d835Schristos /*
2*0036d835Schristos  * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 2000
3*0036d835Schristos  *	The Regents of the University of California.  All rights reserved.
4*0036d835Schristos  *
5*0036d835Schristos  * Redistribution and use in source and binary forms, with or without
6*0036d835Schristos  * modification, are permitted provided that: (1) source code distributions
7*0036d835Schristos  * retain the above copyright notice and this paragraph in its entirety, (2)
8*0036d835Schristos  * distributions including binary code include the above copyright notice and
9*0036d835Schristos  * this paragraph in its entirety in the documentation or other materials
10*0036d835Schristos  * provided with the distribution, and (3) all advertising materials mentioning
11*0036d835Schristos  * features or use of this software display the following acknowledgement:
12*0036d835Schristos  * ``This product includes software developed by the University of California,
13*0036d835Schristos  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14*0036d835Schristos  * the University nor the names of its contributors may be used to endorse
15*0036d835Schristos  * or promote products derived from this software without specific prior
16*0036d835Schristos  * written permission.
17*0036d835Schristos  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18*0036d835Schristos  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19*0036d835Schristos  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20*0036d835Schristos  */
21*0036d835Schristos 
22*0036d835Schristos #include "varattrs.h"
23*0036d835Schristos 
24*0036d835Schristos /*
25*0036d835Schristos  * This doesn't actually test libpcap itself; it tests whether
26*0036d835Schristos  * valgrind properly handles the APIs libpcap uses.  If it doesn't,
27*0036d835Schristos  * we end up getting patches submitted to "fix" references that
28*0036d835Schristos  * valgrind claims are being made to uninitialized data, when, in
29*0036d835Schristos  * fact, the OS isn't making any such references - or we get
30*0036d835Schristos  * valgrind *not* detecting *actual* incorrect references.
31*0036d835Schristos  *
32*0036d835Schristos  * Both BPF and Linux socket filters aren't handled correctly
33*0036d835Schristos  * by some versions of valgrind.  See valgrind bug 318203 for
34*0036d835Schristos  * Linux:
35*0036d835Schristos  *
36*0036d835Schristos  *	https://bugs.kde.org/show_bug.cgi?id=318203
37*0036d835Schristos  *
38*0036d835Schristos  * and valgrind bug 312989 for macOS:
39*0036d835Schristos  *
40*0036d835Schristos  *	https://bugs.kde.org/show_bug.cgi?id=312989
41*0036d835Schristos  *
42*0036d835Schristos  * The fixes for both of those are checked into the official valgrind
43*0036d835Schristos  * repository.
44*0036d835Schristos  *
45*0036d835Schristos  * The unofficial FreeBSD port has similar issues to the official macOS
46*0036d835Schristos  * port, for similar reasons.
47*0036d835Schristos  */
48*0036d835Schristos #ifndef lint
49*0036d835Schristos static const char copyright[] _U_ =
50*0036d835Schristos     "@(#) Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 2000\n\
51*0036d835Schristos The Regents of the University of California.  All rights reserved.\n";
52*0036d835Schristos #endif
53*0036d835Schristos 
54*0036d835Schristos #ifdef HAVE_CONFIG_H
55*0036d835Schristos #include <config.h>
56*0036d835Schristos #endif
57*0036d835Schristos 
58*0036d835Schristos #include <stdio.h>
59*0036d835Schristos #include <stdlib.h>
60*0036d835Schristos #include <string.h>
61*0036d835Schristos #include <stdarg.h>
62*0036d835Schristos #include <unistd.h>
63*0036d835Schristos #include <fcntl.h>
64*0036d835Schristos #include <errno.h>
65*0036d835Schristos #include <arpa/inet.h>
66*0036d835Schristos #include <sys/types.h>
67*0036d835Schristos #include <sys/stat.h>
68*0036d835Schristos 
69*0036d835Schristos #include "pcap/funcattrs.h"
70*0036d835Schristos 
71*0036d835Schristos #if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(_AIX) || defined(sun)
72*0036d835Schristos /* OS with BPF - use BPF */
73*0036d835Schristos #define USE_BPF
74*0036d835Schristos #elif defined(linux)
75*0036d835Schristos /* Linux - use socket filters */
76*0036d835Schristos #define USE_SOCKET_FILTERS
77*0036d835Schristos #else
78*0036d835Schristos #error "Unknown platform or platform that doesn't support Valgrind"
79*0036d835Schristos #endif
80*0036d835Schristos 
81*0036d835Schristos #if defined(USE_BPF)
82*0036d835Schristos 
83*0036d835Schristos #include <sys/ioctl.h>
84*0036d835Schristos #include <net/bpf.h>
85*0036d835Schristos 
86*0036d835Schristos /*
87*0036d835Schristos  * Make "pcap.h" not include "pcap/bpf.h"; we are going to include the
88*0036d835Schristos  * native OS version, as we're going to be doing our own ioctls to
89*0036d835Schristos  * make sure that, in the uninitialized-data tests, the filters aren't
90*0036d835Schristos  * checked by libpcap before being handed to BPF.
91*0036d835Schristos  */
92*0036d835Schristos #define PCAP_DONT_INCLUDE_PCAP_BPF_H
93*0036d835Schristos 
94*0036d835Schristos #elif defined(USE_SOCKET_FILTERS)
95*0036d835Schristos 
96*0036d835Schristos #include <sys/socket.h>
97*0036d835Schristos #include <linux/types.h>
98*0036d835Schristos #include <linux/filter.h>
99*0036d835Schristos 
100*0036d835Schristos #endif
101*0036d835Schristos 
102*0036d835Schristos #include <pcap.h>
103*0036d835Schristos 
104*0036d835Schristos static char *program_name;
105*0036d835Schristos 
106*0036d835Schristos /* Forwards */
107*0036d835Schristos static void PCAP_NORETURN usage(void);
108*0036d835Schristos static void PCAP_NORETURN error(const char *, ...) PCAP_PRINTFLIKE(1, 2);
109*0036d835Schristos static void warning(const char *, ...) PCAP_PRINTFLIKE(1, 2);
110*0036d835Schristos 
111*0036d835Schristos /*
112*0036d835Schristos  * On Windows, we need to open the file in binary mode, so that
113*0036d835Schristos  * we get all the bytes specified by the size we get from "fstat()".
114*0036d835Schristos  * On UNIX, that's not necessary.  O_BINARY is defined on Windows;
115*0036d835Schristos  * we define it as 0 if it's not defined, so it does nothing.
116*0036d835Schristos  */
117*0036d835Schristos #ifndef O_BINARY
118*0036d835Schristos #define O_BINARY	0
119*0036d835Schristos #endif
120*0036d835Schristos 
121*0036d835Schristos static char *
read_infile(char * fname)122*0036d835Schristos read_infile(char *fname)
123*0036d835Schristos {
124*0036d835Schristos 	register int i, fd, cc;
125*0036d835Schristos 	register char *cp;
126*0036d835Schristos 	struct stat buf;
127*0036d835Schristos 
128*0036d835Schristos 	fd = open(fname, O_RDONLY|O_BINARY);
129*0036d835Schristos 	if (fd < 0)
130*0036d835Schristos 		error("can't open %s: %s", fname, pcap_strerror(errno));
131*0036d835Schristos 
132*0036d835Schristos 	if (fstat(fd, &buf) < 0)
133*0036d835Schristos 		error("can't stat %s: %s", fname, pcap_strerror(errno));
134*0036d835Schristos 
135*0036d835Schristos 	cp = malloc((u_int)buf.st_size + 1);
136*0036d835Schristos 	if (cp == NULL)
137*0036d835Schristos 		error("malloc(%d) for %s: %s", (u_int)buf.st_size + 1,
138*0036d835Schristos 			fname, pcap_strerror(errno));
139*0036d835Schristos 	cc = read(fd, cp, (u_int)buf.st_size);
140*0036d835Schristos 	if (cc < 0)
141*0036d835Schristos 		error("read %s: %s", fname, pcap_strerror(errno));
142*0036d835Schristos 	if (cc != buf.st_size)
143*0036d835Schristos 		error("short read %s (%d != %d)", fname, cc, (int)buf.st_size);
144*0036d835Schristos 
145*0036d835Schristos 	close(fd);
146*0036d835Schristos 	/* replace "# comment" with spaces */
147*0036d835Schristos 	for (i = 0; i < cc; i++) {
148*0036d835Schristos 		if (cp[i] == '#')
149*0036d835Schristos 			while (i < cc && cp[i] != '\n')
150*0036d835Schristos 				cp[i++] = ' ';
151*0036d835Schristos 	}
152*0036d835Schristos 	cp[cc] = '\0';
153*0036d835Schristos 	return (cp);
154*0036d835Schristos }
155*0036d835Schristos 
156*0036d835Schristos /* VARARGS */
157*0036d835Schristos static void
error(const char * fmt,...)158*0036d835Schristos error(const char *fmt, ...)
159*0036d835Schristos {
160*0036d835Schristos 	va_list ap;
161*0036d835Schristos 
162*0036d835Schristos 	(void)fprintf(stderr, "%s: ", program_name);
163*0036d835Schristos 	va_start(ap, fmt);
164*0036d835Schristos 	(void)vfprintf(stderr, fmt, ap);
165*0036d835Schristos 	va_end(ap);
166*0036d835Schristos 	if (*fmt) {
167*0036d835Schristos 		fmt += strlen(fmt);
168*0036d835Schristos 		if (fmt[-1] != '\n')
169*0036d835Schristos 			(void)fputc('\n', stderr);
170*0036d835Schristos 	}
171*0036d835Schristos 	exit(1);
172*0036d835Schristos 	/* NOTREACHED */
173*0036d835Schristos }
174*0036d835Schristos 
175*0036d835Schristos /* VARARGS */
176*0036d835Schristos static void
warning(const char * fmt,...)177*0036d835Schristos warning(const char *fmt, ...)
178*0036d835Schristos {
179*0036d835Schristos 	va_list ap;
180*0036d835Schristos 
181*0036d835Schristos 	(void)fprintf(stderr, "%s: WARNING: ", program_name);
182*0036d835Schristos 	va_start(ap, fmt);
183*0036d835Schristos 	(void)vfprintf(stderr, fmt, ap);
184*0036d835Schristos 	va_end(ap);
185*0036d835Schristos 	if (*fmt) {
186*0036d835Schristos 		fmt += strlen(fmt);
187*0036d835Schristos 		if (fmt[-1] != '\n')
188*0036d835Schristos 			(void)fputc('\n', stderr);
189*0036d835Schristos 	}
190*0036d835Schristos }
191*0036d835Schristos 
192*0036d835Schristos /*
193*0036d835Schristos  * Copy arg vector into a new buffer, concatenating arguments with spaces.
194*0036d835Schristos  */
195*0036d835Schristos static char *
copy_argv(register char ** argv)196*0036d835Schristos copy_argv(register char **argv)
197*0036d835Schristos {
198*0036d835Schristos 	register char **p;
199*0036d835Schristos 	register u_int len = 0;
200*0036d835Schristos 	char *buf;
201*0036d835Schristos 	char *src, *dst;
202*0036d835Schristos 
203*0036d835Schristos 	p = argv;
204*0036d835Schristos 	if (*p == 0)
205*0036d835Schristos 		return 0;
206*0036d835Schristos 
207*0036d835Schristos 	while (*p)
208*0036d835Schristos 		len += strlen(*p++) + 1;
209*0036d835Schristos 
210*0036d835Schristos 	buf = (char *)malloc(len);
211*0036d835Schristos 	if (buf == NULL)
212*0036d835Schristos 		error("copy_argv: malloc");
213*0036d835Schristos 
214*0036d835Schristos 	p = argv;
215*0036d835Schristos 	dst = buf;
216*0036d835Schristos 	while ((src = *p++) != NULL) {
217*0036d835Schristos 		while ((*dst++ = *src++) != '\0')
218*0036d835Schristos 			;
219*0036d835Schristos 		dst[-1] = ' ';
220*0036d835Schristos 	}
221*0036d835Schristos 	dst[-1] = '\0';
222*0036d835Schristos 
223*0036d835Schristos 	return buf;
224*0036d835Schristos }
225*0036d835Schristos 
226*0036d835Schristos #define INSN_COUNT	17
227*0036d835Schristos 
228*0036d835Schristos int
main(int argc,char ** argv)229*0036d835Schristos main(int argc, char **argv)
230*0036d835Schristos {
231*0036d835Schristos 	char *cp, *device;
232*0036d835Schristos 	int op;
233*0036d835Schristos 	int dorfmon, useactivate;
234*0036d835Schristos 	char ebuf[PCAP_ERRBUF_SIZE];
235*0036d835Schristos 	char *infile;
236*0036d835Schristos 	const char *cmdbuf;
237*0036d835Schristos 	pcap_if_t *devlist;
238*0036d835Schristos 	pcap_t *pd;
239*0036d835Schristos 	int status = 0;
240*0036d835Schristos 	int pcap_fd;
241*0036d835Schristos #if defined(USE_BPF)
242*0036d835Schristos 	struct bpf_program bad_fcode;
243*0036d835Schristos 	struct bpf_insn uninitialized[INSN_COUNT];
244*0036d835Schristos #elif defined(USE_SOCKET_FILTERS)
245*0036d835Schristos 	struct sock_fprog bad_fcode;
246*0036d835Schristos 	struct sock_filter uninitialized[INSN_COUNT];
247*0036d835Schristos #endif
248*0036d835Schristos 	struct bpf_program fcode;
249*0036d835Schristos 
250*0036d835Schristos 	device = NULL;
251*0036d835Schristos 	dorfmon = 0;
252*0036d835Schristos 	useactivate = 0;
253*0036d835Schristos 	infile = NULL;
254*0036d835Schristos 
255*0036d835Schristos 	if ((cp = strrchr(argv[0], '/')) != NULL)
256*0036d835Schristos 		program_name = cp + 1;
257*0036d835Schristos 	else
258*0036d835Schristos 		program_name = argv[0];
259*0036d835Schristos 
260*0036d835Schristos 	opterr = 0;
261*0036d835Schristos 	while ((op = getopt(argc, argv, "aF:i:I")) != -1) {
262*0036d835Schristos 		switch (op) {
263*0036d835Schristos 
264*0036d835Schristos 		case 'a':
265*0036d835Schristos 			useactivate = 1;
266*0036d835Schristos 			break;
267*0036d835Schristos 
268*0036d835Schristos 		case 'F':
269*0036d835Schristos 			infile = optarg;
270*0036d835Schristos 			break;
271*0036d835Schristos 
272*0036d835Schristos 		case 'i':
273*0036d835Schristos 			device = optarg;
274*0036d835Schristos 			break;
275*0036d835Schristos 
276*0036d835Schristos 		case 'I':
277*0036d835Schristos 			dorfmon = 1;
278*0036d835Schristos 			useactivate = 1;	/* required for rfmon */
279*0036d835Schristos 			break;
280*0036d835Schristos 
281*0036d835Schristos 		default:
282*0036d835Schristos 			usage();
283*0036d835Schristos 			/* NOTREACHED */
284*0036d835Schristos 		}
285*0036d835Schristos 	}
286*0036d835Schristos 
287*0036d835Schristos 	if (device == NULL) {
288*0036d835Schristos 		/*
289*0036d835Schristos 		 * No interface specified; get whatever pcap_lookupdev()
290*0036d835Schristos 		 * finds.
291*0036d835Schristos 		 */
292*0036d835Schristos 		if (pcap_findalldevs(&devlist, ebuf) == -1)
293*0036d835Schristos 			error("%s", ebuf);
294*0036d835Schristos 		if (devlist == NULL)
295*0036d835Schristos 			error("no interfaces available for capture");
296*0036d835Schristos 		device = strdup(devlist->name);
297*0036d835Schristos 		pcap_freealldevs(devlist);
298*0036d835Schristos 	}
299*0036d835Schristos 
300*0036d835Schristos 	if (infile != NULL) {
301*0036d835Schristos 		/*
302*0036d835Schristos 		 * Filter specified with "-F" and a file containing
303*0036d835Schristos 		 * a filter.
304*0036d835Schristos 		 */
305*0036d835Schristos 		cmdbuf = read_infile(infile);
306*0036d835Schristos 	} else {
307*0036d835Schristos 		if (optind < argc) {
308*0036d835Schristos 			/*
309*0036d835Schristos 			 * Filter specified with arguments on the
310*0036d835Schristos 			 * command line.
311*0036d835Schristos 			 */
312*0036d835Schristos 			cmdbuf = copy_argv(&argv[optind+1]);
313*0036d835Schristos 		} else {
314*0036d835Schristos 			/*
315*0036d835Schristos 			 * No filter specified; use an empty string, which
316*0036d835Schristos 			 * compiles to an "accept all" filter.
317*0036d835Schristos 			 */
318*0036d835Schristos 			cmdbuf = "";
319*0036d835Schristos 		}
320*0036d835Schristos 	}
321*0036d835Schristos 
322*0036d835Schristos 	if (useactivate) {
323*0036d835Schristos 		pd = pcap_create(device, ebuf);
324*0036d835Schristos 		if (pd == NULL)
325*0036d835Schristos 			error("%s: pcap_create() failed: %s", device, ebuf);
326*0036d835Schristos 		status = pcap_set_snaplen(pd, 65535);
327*0036d835Schristos 		if (status != 0)
328*0036d835Schristos 			error("%s: pcap_set_snaplen failed: %s",
329*0036d835Schristos 			    device, pcap_statustostr(status));
330*0036d835Schristos 		status = pcap_set_promisc(pd, 1);
331*0036d835Schristos 		if (status != 0)
332*0036d835Schristos 			error("%s: pcap_set_promisc failed: %s",
333*0036d835Schristos 			    device, pcap_statustostr(status));
334*0036d835Schristos 		if (dorfmon) {
335*0036d835Schristos 			status = pcap_set_rfmon(pd, 1);
336*0036d835Schristos 			if (status != 0)
337*0036d835Schristos 				error("%s: pcap_set_rfmon failed: %s",
338*0036d835Schristos 				    device, pcap_statustostr(status));
339*0036d835Schristos 		}
340*0036d835Schristos 		status = pcap_set_timeout(pd, 1000);
341*0036d835Schristos 		if (status != 0)
342*0036d835Schristos 			error("%s: pcap_set_timeout failed: %s",
343*0036d835Schristos 			    device, pcap_statustostr(status));
344*0036d835Schristos 		status = pcap_activate(pd);
345*0036d835Schristos 		if (status < 0) {
346*0036d835Schristos 			/*
347*0036d835Schristos 			 * pcap_activate() failed.
348*0036d835Schristos 			 */
349*0036d835Schristos 			error("%s: %s\n(%s)", device,
350*0036d835Schristos 			    pcap_statustostr(status), pcap_geterr(pd));
351*0036d835Schristos 		} else if (status > 0) {
352*0036d835Schristos 			/*
353*0036d835Schristos 			 * pcap_activate() succeeded, but it's warning us
354*0036d835Schristos 			 * of a problem it had.
355*0036d835Schristos 			 */
356*0036d835Schristos 			warning("%s: %s\n(%s)", device,
357*0036d835Schristos 			    pcap_statustostr(status), pcap_geterr(pd));
358*0036d835Schristos 		}
359*0036d835Schristos 	} else {
360*0036d835Schristos 		*ebuf = '\0';
361*0036d835Schristos 		pd = pcap_open_live(device, 65535, 1, 1000, ebuf);
362*0036d835Schristos 		if (pd == NULL)
363*0036d835Schristos 			error("%s", ebuf);
364*0036d835Schristos 		else if (*ebuf)
365*0036d835Schristos 			warning("%s", ebuf);
366*0036d835Schristos 	}
367*0036d835Schristos 
368*0036d835Schristos 	pcap_fd = pcap_fileno(pd);
369*0036d835Schristos 
370*0036d835Schristos 	/*
371*0036d835Schristos 	 * Try setting a filter with an uninitialized bpf_program
372*0036d835Schristos 	 * structure.  This should cause valgrind to report a
373*0036d835Schristos 	 * problem.
374*0036d835Schristos 	 *
375*0036d835Schristos 	 * We don't check for errors, because it could get an
376*0036d835Schristos 	 * error due to a bad pointer or count.
377*0036d835Schristos 	 */
378*0036d835Schristos #if defined(USE_BPF)
379*0036d835Schristos 	ioctl(pcap_fd, BIOCSETF, &bad_fcode);
380*0036d835Schristos #elif defined(USE_SOCKET_FILTERS)
381*0036d835Schristos 	setsockopt(pcap_fd, SOL_SOCKET, SO_ATTACH_FILTER, &bad_fcode,
382*0036d835Schristos 	    sizeof(bad_fcode));
383*0036d835Schristos #endif
384*0036d835Schristos 
385*0036d835Schristos 	/*
386*0036d835Schristos 	 * Try setting a filter with an initialized bpf_program
387*0036d835Schristos 	 * structure that points to an uninitialized program.
388*0036d835Schristos 	 * That should also cause valgrind to report a problem.
389*0036d835Schristos 	 *
390*0036d835Schristos 	 * We don't check for errors, because it could get an
391*0036d835Schristos 	 * error due to a bad pointer or count.
392*0036d835Schristos 	 */
393*0036d835Schristos #if defined(USE_BPF)
394*0036d835Schristos 	bad_fcode.bf_len = INSN_COUNT;
395*0036d835Schristos 	bad_fcode.bf_insns = uninitialized;
396*0036d835Schristos 	ioctl(pcap_fd, BIOCSETF, &bad_fcode);
397*0036d835Schristos #elif defined(USE_SOCKET_FILTERS)
398*0036d835Schristos 	bad_fcode.len = INSN_COUNT;
399*0036d835Schristos 	bad_fcode.filter = uninitialized;
400*0036d835Schristos 	setsockopt(pcap_fd, SOL_SOCKET, SO_ATTACH_FILTER, &bad_fcode,
401*0036d835Schristos 	    sizeof(bad_fcode));
402*0036d835Schristos #endif
403*0036d835Schristos 
404*0036d835Schristos 	/*
405*0036d835Schristos 	 * Now compile a filter and set the filter with that.
406*0036d835Schristos 	 * That should *not* cause valgrind to report a
407*0036d835Schristos 	 * problem.
408*0036d835Schristos 	 */
409*0036d835Schristos 	if (pcap_compile(pd, &fcode, cmdbuf, 1, 0) < 0)
410*0036d835Schristos 		error("can't compile filter: %s", pcap_geterr(pd));
411*0036d835Schristos 	if (pcap_setfilter(pd, &fcode) < 0)
412*0036d835Schristos 		error("can't set filter: %s", pcap_geterr(pd));
413*0036d835Schristos 
414*0036d835Schristos 	pcap_close(pd);
415*0036d835Schristos 	exit(status < 0 ? 1 : 0);
416*0036d835Schristos }
417*0036d835Schristos 
418*0036d835Schristos static void
usage(void)419*0036d835Schristos usage(void)
420*0036d835Schristos {
421*0036d835Schristos 	(void)fprintf(stderr, "%s, with %s\n", program_name,
422*0036d835Schristos 	    pcap_lib_version());
423*0036d835Schristos 	(void)fprintf(stderr,
424*0036d835Schristos 	    "Usage: %s [-aI] [ -F file ] [ -I interface ] [ expression ]\n",
425*0036d835Schristos 	    program_name);
426*0036d835Schristos 	exit(1);
427*0036d835Schristos }
428