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 #if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
68 /* BSD-flavored OS - use BPF */
71 /* Linux - use socket filters */
72 #define USE_SOCKET_FILTERS
74 #error "Unknown platform or platform that doesn't support Valgrind"
79 #include <sys/ioctl.h>
83 * Make "pcap.h" not include "pcap/bpf.h"; we are going to include the
84 * native OS version, as we're going to be doing our own ioctls to
85 * make sure that, in the uninitialized-data tests, the filters aren't
86 * checked by libpcap before being handed to BPF.
88 #define PCAP_DONT_INCLUDE_PCAP_BPF_H
90 #elif defined(USE_SOCKET_FILTERS)
92 #include <sys/socket.h>
93 #include <linux/types.h>
94 #include <linux/filter.h>
100 static char *program_name
;
103 * This was introduced by Clang:
105 * http://clang.llvm.org/docs/LanguageExtensions.html#has-attribute
107 * in some version (which version?); it has been picked up by GCC 5.0.
109 #ifndef __has_attribute
111 * It's a macro, so you can check whether it's defined to check
112 * whether it's supported.
114 * If it's not, define it to always return 0, so that we move on to
115 * the fallback checks.
117 #define __has_attribute(x) 0
120 #if __has_attribute(noreturn) \
121 || (defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) >= 205)) \
122 || (defined(__SUNPRO_C) && (__SUNPRO_C >= 0x590)) \
123 || (defined(__xlC__) && __xlC__ >= 0x0A01) \
124 || (defined(__HP_aCC) && __HP_aCC >= 61000)
126 * Compiler with support for it, or GCC 2.5 and later, or Solaris Studio 12
127 * (Sun C 5.9) and later, or IBM XL C 10.1 and later (do any earlier
128 * versions of XL C support this?), or HP aCC A.06.10 and later.
130 #define PCAP_NORETURN __attribute((noreturn))
131 #elif defined( _MSC_VER )
132 #define PCAP_NORETURN __declspec(noreturn)
134 #define PCAP_NORETURN
137 #if __has_attribute(__format__) \
138 || (defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) >= 203)) \
139 || (defined(__xlC__) && __xlC__ >= 0x0A01) \
140 || (defined(__HP_aCC) && __HP_aCC >= 61000)
142 * Compiler with support for it, or GCC 2.3 and later, or IBM XL C 10.1
143 * and later (do any earlier versions of XL C support this?),
144 * or HP aCC A.06.10 and later.
146 #define PCAP_PRINTFLIKE(x,y) __attribute__((__format__(__printf__,x,y)))
148 #define PCAP_PRINTFLIKE(x,y)
152 static void PCAP_NORETURN
usage(void);
153 static void PCAP_NORETURN
error(const char *, ...) PCAP_PRINTFLIKE(1, 2);
154 static void warning(const char *, ...) PCAP_PRINTFLIKE(1, 2);
157 * On Windows, we need to open the file in binary mode, so that
158 * we get all the bytes specified by the size we get from "fstat()".
159 * On UNIX, that's not necessary. O_BINARY is defined on Windows;
160 * we define it as 0 if it's not defined, so it does nothing.
167 read_infile(char *fname
)
169 register int i
, fd
, cc
;
173 fd
= open(fname
, O_RDONLY
|O_BINARY
);
175 error("can't open %s: %s", fname
, pcap_strerror(errno
));
177 if (fstat(fd
, &buf
) < 0)
178 error("can't stat %s: %s", fname
, pcap_strerror(errno
));
180 cp
= malloc((u_int
)buf
.st_size
+ 1);
182 error("malloc(%d) for %s: %s", (u_int
)buf
.st_size
+ 1,
183 fname
, pcap_strerror(errno
));
184 cc
= read(fd
, cp
, (u_int
)buf
.st_size
);
186 error("read %s: %s", fname
, pcap_strerror(errno
));
187 if (cc
!= buf
.st_size
)
188 error("short read %s (%d != %d)", fname
, cc
, (int)buf
.st_size
);
191 /* replace "# comment" with spaces */
192 for (i
= 0; i
< cc
; i
++) {
194 while (i
< cc
&& cp
[i
] != '\n')
203 error(const char *fmt
, ...)
207 (void)fprintf(stderr
, "%s: ", program_name
);
209 (void)vfprintf(stderr
, fmt
, ap
);
214 (void)fputc('\n', stderr
);
222 warning(const char *fmt
, ...)
226 (void)fprintf(stderr
, "%s: WARNING: ", program_name
);
228 (void)vfprintf(stderr
, fmt
, ap
);
233 (void)fputc('\n', stderr
);
238 * Copy arg vector into a new buffer, concatenating arguments with spaces.
241 copy_argv(register char **argv
)
244 register u_int len
= 0;
253 len
+= strlen(*p
++) + 1;
255 buf
= (char *)malloc(len
);
257 error("copy_argv: malloc");
261 while ((src
= *p
++) != NULL
) {
262 while ((*dst
++ = *src
++) != '\0')
271 #define INSN_COUNT 17
274 main(int argc
, char **argv
)
278 int dorfmon
, useactivate
;
279 char ebuf
[PCAP_ERRBUF_SIZE
];
286 struct bpf_program bad_fcode
;
287 struct bpf_insn uninitialized
[INSN_COUNT
];
288 #elif defined(USE_SOCKET_FILTERS)
289 struct sock_fprog bad_fcode
;
290 struct sock_filter uninitialized
[INSN_COUNT
];
292 struct bpf_program fcode
;
299 if ((cp
= strrchr(argv
[0], '/')) != NULL
)
300 program_name
= cp
+ 1;
302 program_name
= argv
[0];
305 while ((op
= getopt(argc
, argv
, "aF:i:I")) != -1) {
322 useactivate
= 1; /* required for rfmon */
331 if (device
== NULL
) {
333 * No interface specified; get whatever pcap_lookupdev()
336 device
= pcap_lookupdev(ebuf
);
337 if (device
== NULL
) {
338 error("couldn't find interface to use: %s",
343 if (infile
!= NULL
) {
345 * Filter specified with "-F" and a file containing
348 cmdbuf
= read_infile(infile
);
352 * Filter specified with arguments on the
355 cmdbuf
= copy_argv(&argv
[optind
+1]);
358 * No filter specified; use an empty string, which
359 * compiles to an "accept all" filter.
366 pd
= pcap_create(device
, ebuf
);
368 error("%s: pcap_create() failed: %s", device
, ebuf
);
369 status
= pcap_set_snaplen(pd
, 65535);
371 error("%s: pcap_set_snaplen failed: %s",
372 device
, pcap_statustostr(status
));
373 status
= pcap_set_promisc(pd
, 1);
375 error("%s: pcap_set_promisc failed: %s",
376 device
, pcap_statustostr(status
));
378 status
= pcap_set_rfmon(pd
, 1);
380 error("%s: pcap_set_rfmon failed: %s",
381 device
, pcap_statustostr(status
));
383 status
= pcap_set_timeout(pd
, 1000);
385 error("%s: pcap_set_timeout failed: %s",
386 device
, pcap_statustostr(status
));
387 status
= pcap_activate(pd
);
390 * pcap_activate() failed.
392 error("%s: %s\n(%s)", device
,
393 pcap_statustostr(status
), pcap_geterr(pd
));
394 } else if (status
> 0) {
396 * pcap_activate() succeeded, but it's warning us
397 * of a problem it had.
399 warning("%s: %s\n(%s)", device
,
400 pcap_statustostr(status
), pcap_geterr(pd
));
404 pd
= pcap_open_live(device
, 65535, 1, 1000, ebuf
);
411 pcap_fd
= pcap_fileno(pd
);
414 * Try setting a filter with an uninitialized bpf_program
415 * structure. This should cause valgrind to report a
418 * We don't check for errors, because it could get an
419 * error due to a bad pointer or count.
422 ioctl(pcap_fd
, BIOCSETF
, &bad_fcode
);
423 #elif defined(USE_SOCKET_FILTERS)
424 setsockopt(pcap_fd
, SOL_SOCKET
, SO_ATTACH_FILTER
, &bad_fcode
,
429 * Try setting a filter with an initialized bpf_program
430 * structure that points to an uninitialized program.
431 * That should also cause valgrind to report a problem.
433 * We don't check for errors, because it could get an
434 * error due to a bad pointer or count.
437 bad_fcode
.bf_len
= INSN_COUNT
;
438 bad_fcode
.bf_insns
= uninitialized
;
439 ioctl(pcap_fd
, BIOCSETF
, &bad_fcode
);
440 #elif defined(USE_SOCKET_FILTERS)
441 bad_fcode
.len
= INSN_COUNT
;
442 bad_fcode
.filter
= uninitialized
;
443 setsockopt(pcap_fd
, SOL_SOCKET
, SO_ATTACH_FILTER
, &bad_fcode
,
448 * Now compile a filter and set the filter with that.
449 * That should *not* cause valgrind to report a
452 if (pcap_compile(pd
, &fcode
, cmdbuf
, 1, 0) < 0)
453 error("can't compile filter: %s", pcap_geterr(pd
));
454 if (pcap_setfilter(pd
, &fcode
) < 0)
455 error("can't set filter: %s", pcap_geterr(pd
));
458 exit(status
< 0 ? 1 : 0);
464 (void)fprintf(stderr
, "%s, with %s\n", program_name
,
466 (void)fprintf(stderr
,
467 "Usage: %s [-aI] [ -F file ] [ -I interface ] [ expression ]\n",