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.
23 * This doesn't actually test libpcap itself; it tests whether
24 * valgrind properly handles the APIs libpcap uses. If it doesn't,
25 * we end up getting patches submitted to "fix" references that
26 * valgrind claims are being made to uninitialized data, when, in
27 * fact, the OS isn't making any such references - or we get
28 * valgrind *not* detecting *actual* incorrect references.
30 * Both BPF and Linux socket filters aren't handled correctly
31 * by some versions of valgrind. See valgrind bug 318203 for
34 * https://bugs.kde.org/show_bug.cgi?id=318203
36 * and valgrind bug 312989 for OS X:
38 * https://bugs.kde.org/show_bug.cgi?id=312989
40 * The fixes for both of those are checked into the official valgrind
43 * The unofficial FreeBSD port has similar issues to the official OS X
44 * port, for similar reasons.
47 static const char copyright
[] _U_
=
48 "@(#) Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 2000\n\
49 The Regents of the University of California. All rights reserved.\n";
63 #include <arpa/inet.h>
64 #include <sys/types.h>
67 #include "pcap/funcattrs.h"
69 #if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
70 /* BSD-flavored OS - use BPF */
73 /* Linux - use socket filters */
74 #define USE_SOCKET_FILTERS
76 #error "Unknown platform or platform that doesn't support Valgrind"
81 #include <sys/ioctl.h>
85 * Make "pcap.h" not include "pcap/bpf.h"; we are going to include the
86 * native OS version, as we're going to be doing our own ioctls to
87 * make sure that, in the uninitialized-data tests, the filters aren't
88 * checked by libpcap before being handed to BPF.
90 #define PCAP_DONT_INCLUDE_PCAP_BPF_H
92 #elif defined(USE_SOCKET_FILTERS)
94 #include <sys/socket.h>
95 #include <linux/types.h>
96 #include <linux/filter.h>
102 static char *program_name
;
105 static void PCAP_NORETURN
usage(void);
106 static void PCAP_NORETURN
error(const char *, ...) PCAP_PRINTFLIKE(1, 2);
107 static void warning(const char *, ...) PCAP_PRINTFLIKE(1, 2);
110 * On Windows, we need to open the file in binary mode, so that
111 * we get all the bytes specified by the size we get from "fstat()".
112 * On UNIX, that's not necessary. O_BINARY is defined on Windows;
113 * we define it as 0 if it's not defined, so it does nothing.
120 read_infile(char *fname
)
122 register int i
, fd
, cc
;
126 fd
= open(fname
, O_RDONLY
|O_BINARY
);
128 error("can't open %s: %s", fname
, pcap_strerror(errno
));
130 if (fstat(fd
, &buf
) < 0)
131 error("can't stat %s: %s", fname
, pcap_strerror(errno
));
133 cp
= malloc((u_int
)buf
.st_size
+ 1);
135 error("malloc(%d) for %s: %s", (u_int
)buf
.st_size
+ 1,
136 fname
, pcap_strerror(errno
));
137 cc
= read(fd
, cp
, (u_int
)buf
.st_size
);
139 error("read %s: %s", fname
, pcap_strerror(errno
));
140 if (cc
!= buf
.st_size
)
141 error("short read %s (%d != %d)", fname
, cc
, (int)buf
.st_size
);
144 /* replace "# comment" with spaces */
145 for (i
= 0; i
< cc
; i
++) {
147 while (i
< cc
&& cp
[i
] != '\n')
156 error(const char *fmt
, ...)
160 (void)fprintf(stderr
, "%s: ", program_name
);
162 (void)vfprintf(stderr
, fmt
, ap
);
167 (void)fputc('\n', stderr
);
175 warning(const char *fmt
, ...)
179 (void)fprintf(stderr
, "%s: WARNING: ", program_name
);
181 (void)vfprintf(stderr
, fmt
, ap
);
186 (void)fputc('\n', stderr
);
191 * Copy arg vector into a new buffer, concatenating arguments with spaces.
194 copy_argv(register char **argv
)
197 register u_int len
= 0;
206 len
+= strlen(*p
++) + 1;
208 buf
= (char *)malloc(len
);
210 error("copy_argv: malloc");
214 while ((src
= *p
++) != NULL
) {
215 while ((*dst
++ = *src
++) != '\0')
224 #define INSN_COUNT 17
227 main(int argc
, char **argv
)
231 int dorfmon
, useactivate
;
232 char ebuf
[PCAP_ERRBUF_SIZE
];
239 struct bpf_program bad_fcode
;
240 struct bpf_insn uninitialized
[INSN_COUNT
];
241 #elif defined(USE_SOCKET_FILTERS)
242 struct sock_fprog bad_fcode
;
243 struct sock_filter uninitialized
[INSN_COUNT
];
245 struct bpf_program fcode
;
252 if ((cp
= strrchr(argv
[0], '/')) != NULL
)
253 program_name
= cp
+ 1;
255 program_name
= argv
[0];
258 while ((op
= getopt(argc
, argv
, "aF:i:I")) != -1) {
275 useactivate
= 1; /* required for rfmon */
284 if (device
== NULL
) {
286 * No interface specified; get whatever pcap_lookupdev()
289 device
= pcap_lookupdev(ebuf
);
290 if (device
== NULL
) {
291 error("couldn't find interface to use: %s",
296 if (infile
!= NULL
) {
298 * Filter specified with "-F" and a file containing
301 cmdbuf
= read_infile(infile
);
305 * Filter specified with arguments on the
308 cmdbuf
= copy_argv(&argv
[optind
+1]);
311 * No filter specified; use an empty string, which
312 * compiles to an "accept all" filter.
319 pd
= pcap_create(device
, ebuf
);
321 error("%s: pcap_create() failed: %s", device
, ebuf
);
322 status
= pcap_set_snaplen(pd
, 65535);
324 error("%s: pcap_set_snaplen failed: %s",
325 device
, pcap_statustostr(status
));
326 status
= pcap_set_promisc(pd
, 1);
328 error("%s: pcap_set_promisc failed: %s",
329 device
, pcap_statustostr(status
));
331 status
= pcap_set_rfmon(pd
, 1);
333 error("%s: pcap_set_rfmon failed: %s",
334 device
, pcap_statustostr(status
));
336 status
= pcap_set_timeout(pd
, 1000);
338 error("%s: pcap_set_timeout failed: %s",
339 device
, pcap_statustostr(status
));
340 status
= pcap_activate(pd
);
343 * pcap_activate() failed.
345 error("%s: %s\n(%s)", device
,
346 pcap_statustostr(status
), pcap_geterr(pd
));
347 } else if (status
> 0) {
349 * pcap_activate() succeeded, but it's warning us
350 * of a problem it had.
352 warning("%s: %s\n(%s)", device
,
353 pcap_statustostr(status
), pcap_geterr(pd
));
357 pd
= pcap_open_live(device
, 65535, 1, 1000, ebuf
);
364 pcap_fd
= pcap_fileno(pd
);
367 * Try setting a filter with an uninitialized bpf_program
368 * structure. This should cause valgrind to report a
371 * We don't check for errors, because it could get an
372 * error due to a bad pointer or count.
375 ioctl(pcap_fd
, BIOCSETF
, &bad_fcode
);
376 #elif defined(USE_SOCKET_FILTERS)
377 setsockopt(pcap_fd
, SOL_SOCKET
, SO_ATTACH_FILTER
, &bad_fcode
,
382 * Try setting a filter with an initialized bpf_program
383 * structure that points to an uninitialized program.
384 * That should also cause valgrind to report a problem.
386 * We don't check for errors, because it could get an
387 * error due to a bad pointer or count.
390 bad_fcode
.bf_len
= INSN_COUNT
;
391 bad_fcode
.bf_insns
= uninitialized
;
392 ioctl(pcap_fd
, BIOCSETF
, &bad_fcode
);
393 #elif defined(USE_SOCKET_FILTERS)
394 bad_fcode
.len
= INSN_COUNT
;
395 bad_fcode
.filter
= uninitialized
;
396 setsockopt(pcap_fd
, SOL_SOCKET
, SO_ATTACH_FILTER
, &bad_fcode
,
401 * Now compile a filter and set the filter with that.
402 * That should *not* cause valgrind to report a
405 if (pcap_compile(pd
, &fcode
, cmdbuf
, 1, 0) < 0)
406 error("can't compile filter: %s", pcap_geterr(pd
));
407 if (pcap_setfilter(pd
, &fcode
) < 0)
408 error("can't set filter: %s", pcap_geterr(pd
));
411 exit(status
< 0 ? 1 : 0);
417 (void)fprintf(stderr
, "%s, with %s\n", program_name
,
419 (void)fprintf(stderr
,
420 "Usage: %s [-aI] [ -F file ] [ -I interface ] [ expression ]\n",