2 * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 2000
3 * The Regents of the University of California. All rights reserved.
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
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.
25 * This doesn't actually test libpcap itself; it tests whether
26 * valgrind properly handles the APIs libpcap uses. If it doesn't,
27 * we end up getting patches submitted to "fix" references that
28 * valgrind claims are being made to uninitialized data, when, in
29 * fact, the OS isn't making any such references - or we get
30 * valgrind *not* detecting *actual* incorrect references.
32 * Both BPF and Linux socket filters aren't handled correctly
33 * by some versions of valgrind. See valgrind bug 318203 for
36 * https://bugs.kde.org/show_bug.cgi?id=318203
38 * and valgrind bug 312989 for macOS:
40 * https://bugs.kde.org/show_bug.cgi?id=312989
42 * The fixes for both of those are checked into the official valgrind
45 * The unofficial FreeBSD port has similar issues to the official macOS
46 * port, for similar reasons.
49 static const char copyright
[] _U_
=
50 "@(#) Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 2000\n\
51 The Regents of the University of California. All rights reserved.\n";
64 #include <arpa/inet.h>
65 #include <sys/types.h>
68 #include "pcap/funcattrs.h"
70 #if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(_AIX) || defined(sun)
71 /* OS with BPF - use BPF */
73 #elif defined(__linux__)
74 /* Linux - use socket filters */
75 #define USE_SOCKET_FILTERS
77 #error "Unknown platform or platform that doesn't support Valgrind"
82 #include <sys/ioctl.h>
86 * Make "pcap.h" not include "pcap/bpf.h"; we are going to include the
87 * native OS version, as we're going to be doing our own ioctls to
88 * make sure that, in the uninitialized-data tests, the filters aren't
89 * checked by libpcap before being handed to BPF.
91 #define PCAP_DONT_INCLUDE_PCAP_BPF_H
93 #elif defined(USE_SOCKET_FILTERS)
95 #include <sys/socket.h>
96 #include <linux/types.h>
97 #include <linux/filter.h>
104 * We include system headers to be able to directly set the filter to
105 * a program with uninitialized content, to make sure what we're testing
106 * is Valgrind's checking of the system call to set the filter, and we
107 * also include <pcap.h> to open the device in the first place, and that
108 * means that we may get collisions between their definitions of
109 * BPF_STMT and BPF_JUMP - and do, in fact, get them on Linux (the
110 * definitions may be semantically the same, but that's not sufficient to
111 * avoid the warnings, as the preprocessor doesn't know that u_short is
112 * just unsigned short).
114 * So we undefine BPF_STMT and BPF_JUMP to avoid the warning.
120 static char *program_name
;
123 static void PCAP_NORETURN
usage(void);
124 static void PCAP_NORETURN
error(const char *, ...) PCAP_PRINTFLIKE(1, 2);
125 static void warning(const char *, ...) PCAP_PRINTFLIKE(1, 2);
128 * On Windows, we need to open the file in binary mode, so that
129 * we get all the bytes specified by the size we get from "fstat()".
130 * On UNIX, that's not necessary. O_BINARY is defined on Windows;
131 * we define it as 0 if it's not defined, so it does nothing.
138 read_infile(char *fname
)
140 register int i
, fd
, cc
;
144 fd
= open(fname
, O_RDONLY
|O_BINARY
);
146 error("can't open %s: %s", fname
, pcap_strerror(errno
));
148 if (fstat(fd
, &buf
) < 0)
149 error("can't stat %s: %s", fname
, pcap_strerror(errno
));
152 * _read(), on Windows, has an unsigned int byte count and an
153 * int return value, so we can't handle a file bigger than
154 * INT_MAX - 1 bytes (and have no reason to do so; a filter *that*
155 * big will take forever to compile). (The -1 is for the '\0' at
156 * the end of the string.)
158 if (buf
.st_size
> INT_MAX
- 1)
159 error("%s is larger than %d bytes; that's too large", fname
,
161 cp
= malloc((u_int
)buf
.st_size
+ 1);
163 error("malloc(%d) for %s: %s", (u_int
)buf
.st_size
+ 1,
164 fname
, pcap_strerror(errno
));
165 cc
= (int)read(fd
, cp
, (u_int
)buf
.st_size
);
167 error("read %s: %s", fname
, pcap_strerror(errno
));
168 if (cc
!= buf
.st_size
)
169 error("short read %s (%d != %d)", fname
, cc
, (int)buf
.st_size
);
172 /* replace "# comment" with spaces */
173 for (i
= 0; i
< cc
; i
++) {
175 while (i
< cc
&& cp
[i
] != '\n')
184 error(const char *fmt
, ...)
188 (void)fprintf(stderr
, "%s: ", program_name
);
190 (void)vfprintf(stderr
, fmt
, ap
);
195 (void)fputc('\n', stderr
);
203 warning(const char *fmt
, ...)
207 (void)fprintf(stderr
, "%s: WARNING: ", program_name
);
209 (void)vfprintf(stderr
, fmt
, ap
);
214 (void)fputc('\n', stderr
);
219 * Copy arg vector into a new buffer, concatenating arguments with spaces.
222 copy_argv(register char **argv
)
225 register size_t len
= 0;
234 len
+= strlen(*p
++) + 1;
236 buf
= (char *)malloc(len
);
238 error("copy_argv: malloc");
242 while ((src
= *p
++) != NULL
) {
243 while ((*dst
++ = *src
++) != '\0')
252 #define INSN_COUNT 17
255 main(int argc
, char **argv
)
259 int dorfmon
, useactivate
;
260 char ebuf
[PCAP_ERRBUF_SIZE
];
268 struct bpf_program bad_fcode
;
269 struct bpf_insn uninitialized
[INSN_COUNT
];
270 #elif defined(USE_SOCKET_FILTERS)
271 struct sock_fprog bad_fcode
;
272 struct sock_filter uninitialized
[INSN_COUNT
];
274 struct bpf_program fcode
;
281 if ((cp
= strrchr(argv
[0], '/')) != NULL
)
282 program_name
= cp
+ 1;
284 program_name
= argv
[0];
287 while ((op
= getopt(argc
, argv
, "aF:i:I")) != -1) {
304 useactivate
= 1; /* required for rfmon */
313 if (device
== NULL
) {
315 * No interface specified; get whatever pcap_lookupdev()
318 if (pcap_findalldevs(&devlist
, ebuf
) == -1)
321 error("no interfaces available for capture");
322 device
= strdup(devlist
->name
);
323 pcap_freealldevs(devlist
);
326 if (infile
!= NULL
) {
328 * Filter specified with "-F" and a file containing
331 cmdbuf
= read_infile(infile
);
335 * Filter specified with arguments on the
338 cmdbuf
= copy_argv(&argv
[optind
+1]);
341 * No filter specified; use an empty string, which
342 * compiles to an "accept all" filter.
349 pd
= pcap_create(device
, ebuf
);
351 error("%s: pcap_create() failed: %s", device
, ebuf
);
352 status
= pcap_set_snaplen(pd
, 65535);
354 error("%s: pcap_set_snaplen failed: %s",
355 device
, pcap_statustostr(status
));
356 status
= pcap_set_promisc(pd
, 1);
358 error("%s: pcap_set_promisc failed: %s",
359 device
, pcap_statustostr(status
));
361 status
= pcap_set_rfmon(pd
, 1);
363 error("%s: pcap_set_rfmon failed: %s",
364 device
, pcap_statustostr(status
));
366 status
= pcap_set_timeout(pd
, 1000);
368 error("%s: pcap_set_timeout failed: %s",
369 device
, pcap_statustostr(status
));
370 status
= pcap_activate(pd
);
373 * pcap_activate() failed.
375 error("%s: %s\n(%s)", device
,
376 pcap_statustostr(status
), pcap_geterr(pd
));
377 } else if (status
> 0) {
379 * pcap_activate() succeeded, but it's warning us
380 * of a problem it had.
382 warning("%s: %s\n(%s)", device
,
383 pcap_statustostr(status
), pcap_geterr(pd
));
387 pd
= pcap_open_live(device
, 65535, 1, 1000, ebuf
);
394 pcap_fd
= pcap_fileno(pd
);
397 * Try setting a filter with an uninitialized bpf_program
398 * structure. This should cause valgrind to report a
401 * We don't check for errors, because it could get an
402 * error due to a bad pointer or count.
405 ioctl(pcap_fd
, BIOCSETF
, &bad_fcode
);
406 #elif defined(USE_SOCKET_FILTERS)
407 setsockopt(pcap_fd
, SOL_SOCKET
, SO_ATTACH_FILTER
, &bad_fcode
,
412 * Try setting a filter with an initialized bpf_program
413 * structure that points to an uninitialized program.
414 * That should also cause valgrind to report a problem.
416 * We don't check for errors, because it could get an
417 * error due to a bad pointer or count.
420 bad_fcode
.bf_len
= INSN_COUNT
;
421 bad_fcode
.bf_insns
= uninitialized
;
422 ioctl(pcap_fd
, BIOCSETF
, &bad_fcode
);
423 #elif defined(USE_SOCKET_FILTERS)
424 bad_fcode
.len
= INSN_COUNT
;
425 bad_fcode
.filter
= uninitialized
;
426 setsockopt(pcap_fd
, SOL_SOCKET
, SO_ATTACH_FILTER
, &bad_fcode
,
431 * Now compile a filter and set the filter with that.
432 * That should *not* cause valgrind to report a
435 if (pcap_compile(pd
, &fcode
, cmdbuf
, 1, 0) < 0)
436 error("can't compile filter: %s", pcap_geterr(pd
));
437 if (pcap_setfilter(pd
, &fcode
) < 0)
438 error("can't set filter: %s", pcap_geterr(pd
));
441 exit(status
< 0 ? 1 : 0);
447 (void)fprintf(stderr
, "%s, with %s\n", program_name
,
449 (void)fprintf(stderr
,
450 "Usage: %s [-aI] [ -F file ] [ -i interface ] [ expression ]\n",