]> The Tcpdump Group git mirrors - libpcap/blob - tests/valgrindtest.c
Use raw ioctls or setsockopts to set the bad filters.
[libpcap] / tests / valgrindtest.c
1 /*
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.
4 *
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
16 * written permission.
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.
20 */
21
22 #ifndef lint
23 static const char copyright[] _U_ =
24 "@(#) Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 2000\n\
25 The Regents of the University of California. All rights reserved.\n";
26 static const char rcsid[] _U_ =
27 "@(#) $Header: /tcpdump/master/libpcap/filtertest.c,v 1.2 2005-08-08 17:50:13 guy Exp $ (LBL)";
28 #endif
29
30 #ifdef HAVE_CONFIG_H
31 #include "config.h"
32 #endif
33
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <stdarg.h>
38 #include <unistd.h>
39 #include <fcntl.h>
40 #include <errno.h>
41 #include <arpa/inet.h>
42 #include <sys/types.h>
43 #include <sys/stat.h>
44
45 #if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
46 /* BSD-flavored OS - use BPF */
47 #define USE_BPF
48 #elif defined(linux)
49 /* Linux - use socket filters */
50 #else
51 #error "Unknown platform or platform that doesn't support Valgrind"
52 #endif
53
54 #if defined(USE_BPF)
55 #include <sys/ioctl.h>
56 #include <net/bpf.h>
57 #elif defined(USE_SOCKET_FILTERS)
58 #include <sys/socket.h>
59 #include <linux/types.h>
60 #include <linux/filter.h>
61 #endif
62
63 /*
64 * Make "pcap.h" not include "pcap/bpf.h"; we are going to include the
65 * native OS version, as we're going to be doing our own ioctls to
66 * make sure that, in the uninitialized-data tests, the filters aren't
67 * checked by libpcap before being handed to BPF.
68 */
69 #define PCAP_DONT_INCLUDE_PCAP_BPF_H
70
71 #include <pcap.h>
72 #ifndef HAVE___ATTRIBUTE__
73 #define __attribute__(x)
74 #endif
75
76 static char *program_name;
77
78 /* Forwards */
79 static void usage(void) __attribute__((noreturn));
80 static void error(const char *, ...)
81 __attribute__((noreturn, format (printf, 1, 2)));
82 static void warning(const char *, ...)
83 __attribute__((format (printf, 1, 2)));
84
85 extern int optind;
86 extern int opterr;
87 extern char *optarg;
88
89 /*
90 * On Windows, we need to open the file in binary mode, so that
91 * we get all the bytes specified by the size we get from "fstat()".
92 * On UNIX, that's not necessary. O_BINARY is defined on Windows;
93 * we define it as 0 if it's not defined, so it does nothing.
94 */
95 #ifndef O_BINARY
96 #define O_BINARY 0
97 #endif
98
99 static char *
100 read_infile(char *fname)
101 {
102 register int i, fd, cc;
103 register char *cp;
104 struct stat buf;
105
106 fd = open(fname, O_RDONLY|O_BINARY);
107 if (fd < 0)
108 error("can't open %s: %s", fname, pcap_strerror(errno));
109
110 if (fstat(fd, &buf) < 0)
111 error("can't stat %s: %s", fname, pcap_strerror(errno));
112
113 cp = malloc((u_int)buf.st_size + 1);
114 if (cp == NULL)
115 error("malloc(%d) for %s: %s", (u_int)buf.st_size + 1,
116 fname, pcap_strerror(errno));
117 cc = read(fd, cp, (u_int)buf.st_size);
118 if (cc < 0)
119 error("read %s: %s", fname, pcap_strerror(errno));
120 if (cc != buf.st_size)
121 error("short read %s (%d != %d)", fname, cc, (int)buf.st_size);
122
123 close(fd);
124 /* replace "# comment" with spaces */
125 for (i = 0; i < cc; i++) {
126 if (cp[i] == '#')
127 while (i < cc && cp[i] != '\n')
128 cp[i++] = ' ';
129 }
130 cp[cc] = '\0';
131 return (cp);
132 }
133
134 /* VARARGS */
135 static void
136 error(const char *fmt, ...)
137 {
138 va_list ap;
139
140 (void)fprintf(stderr, "%s: ", program_name);
141 va_start(ap, fmt);
142 (void)vfprintf(stderr, fmt, ap);
143 va_end(ap);
144 if (*fmt) {
145 fmt += strlen(fmt);
146 if (fmt[-1] != '\n')
147 (void)fputc('\n', stderr);
148 }
149 exit(1);
150 /* NOTREACHED */
151 }
152
153 /* VARARGS */
154 static void
155 warning(const char *fmt, ...)
156 {
157 va_list ap;
158
159 (void)fprintf(stderr, "%s: WARNING: ", program_name);
160 va_start(ap, fmt);
161 (void)vfprintf(stderr, fmt, ap);
162 va_end(ap);
163 if (*fmt) {
164 fmt += strlen(fmt);
165 if (fmt[-1] != '\n')
166 (void)fputc('\n', stderr);
167 }
168 }
169
170 /*
171 * Copy arg vector into a new buffer, concatenating arguments with spaces.
172 */
173 static char *
174 copy_argv(register char **argv)
175 {
176 register char **p;
177 register u_int len = 0;
178 char *buf;
179 char *src, *dst;
180
181 p = argv;
182 if (*p == 0)
183 return 0;
184
185 while (*p)
186 len += strlen(*p++) + 1;
187
188 buf = (char *)malloc(len);
189 if (buf == NULL)
190 error("copy_argv: malloc");
191
192 p = argv;
193 dst = buf;
194 while ((src = *p++) != NULL) {
195 while ((*dst++ = *src++) != '\0')
196 ;
197 dst[-1] = ' ';
198 }
199 dst[-1] = '\0';
200
201 return buf;
202 }
203
204 #define INSN_COUNT 17
205
206 int
207 main(int argc, char **argv)
208 {
209 char *cp, *device;
210 int op;
211 int dorfmon, useactivate;
212 char ebuf[PCAP_ERRBUF_SIZE];
213 char *infile;
214 char *cmdbuf;
215 pcap_t *pd;
216 int status = 0;
217 int pcap_fd;
218 #if defined(USE_BPF)
219 struct bpf_program bad_fcode;
220 struct bpf_insn uninitialized[INSN_COUNT];
221 #elif define(USE_SOCKET_FILTERS)
222 struct sock_fprog bad_fcode;
223 struct sock_filter uninitialized[INSN_COUNT];
224 #endif
225 struct bpf_program fcode;
226
227 device = NULL;
228 dorfmon = 0;
229 useactivate = 0;
230 infile = NULL;
231
232 if ((cp = strrchr(argv[0], '/')) != NULL)
233 program_name = cp + 1;
234 else
235 program_name = argv[0];
236
237 opterr = 0;
238 while ((op = getopt(argc, argv, "aF:i:I")) != -1) {
239 switch (op) {
240
241 case 'a':
242 useactivate = 1;
243 break;
244
245 case 'F':
246 infile = optarg;
247 break;
248
249 case 'i':
250 device = optarg;
251 break;
252
253 case 'I':
254 dorfmon = 1;
255 useactivate = 1; /* required for rfmon */
256 break;
257
258 default:
259 usage();
260 /* NOTREACHED */
261 }
262 }
263
264 if (device == NULL) {
265 /*
266 * No interface specified; get whatever pcap_lookupdev()
267 * finds.
268 */
269 device = pcap_lookupdev(ebuf);
270 if (device == NULL) {
271 error("couldn't find interface to use: %s",
272 ebuf);
273 }
274 }
275
276 if (infile != NULL) {
277 /*
278 * Filter specified with "-F" and a file containing
279 * a filter.
280 */
281 cmdbuf = read_infile(infile);
282 } else {
283 if (optind < argc) {
284 /*
285 * Filter specified with arguments on the
286 * command line.
287 */
288 cmdbuf = copy_argv(&argv[optind+1]);
289 } else {
290 /*
291 * No filter specified; use an empty string, which
292 * compiles to an "accept all" filter.
293 */
294 cmdbuf = "";
295 }
296 }
297
298 if (useactivate) {
299 pd = pcap_create(device, ebuf);
300 if (pd == NULL)
301 error("%s: pcap_create() failed: %s", device, ebuf);
302 status = pcap_set_snaplen(pd, 65535);
303 if (status != 0)
304 error("%s: pcap_set_snaplen failed: %s",
305 device, pcap_statustostr(status));
306 status = pcap_set_promisc(pd, 1);
307 if (status != 0)
308 error("%s: pcap_set_promisc failed: %s",
309 device, pcap_statustostr(status));
310 if (dorfmon) {
311 status = pcap_set_rfmon(pd, 1);
312 if (status != 0)
313 error("%s: pcap_set_rfmon failed: %s",
314 device, pcap_statustostr(status));
315 }
316 status = pcap_set_timeout(pd, 1000);
317 if (status != 0)
318 error("%s: pcap_set_timeout failed: %s",
319 device, pcap_statustostr(status));
320 status = pcap_activate(pd);
321 if (status < 0) {
322 /*
323 * pcap_activate() failed.
324 */
325 error("%s: %s\n(%s)", device,
326 pcap_statustostr(status), pcap_geterr(pd));
327 } else if (status > 0) {
328 /*
329 * pcap_activate() succeeded, but it's warning us
330 * of a problem it had.
331 */
332 warning("%s: %s\n(%s)", device,
333 pcap_statustostr(status), pcap_geterr(pd));
334 }
335 } else {
336 *ebuf = '\0';
337 pd = pcap_open_live(device, 65535, 1, 1000, ebuf);
338 if (pd == NULL)
339 error("%s", ebuf);
340 else if (*ebuf)
341 warning("%s", ebuf);
342 }
343
344 pcap_fd = pcap_fileno(pd);
345
346 /*
347 * Try setting a filter with an uninitialized bpf_program
348 * structure. This should cause valgrind to report a
349 * problem.
350 *
351 * We don't check for errors, because it could get an
352 * error due to a bad pointer or count.
353 */
354 #if defined(USE_BPF)
355 ioctl(pcap_fd, BIOCSETF, &bad_fcode);
356 #elif defined(USE_SOCKET_FILTERS)
357 setsockopt(pcap_fd, SOL_SOCKET, SO_ATTACH_FILTER, &bad_fcode,
358 sizeof(bad_fcode));
359 #endif
360
361 /*
362 * Try setting a filter with an initialized bpf_program
363 * structure that points to an uninitialized program.
364 * That should also cause valgrind to report a problem.
365 *
366 * We don't check for errors, because it could get an
367 * error due to a bad pointer or count.
368 */
369 #if defined(USE_BPF)
370 bad_fcode.bf_len = INSN_COUNT;
371 bad_fcode.bf_insns = uninitialized;
372 ioctl(pcap_fd, BIOCSETF, &bad_fcode);
373 #elif defined(USE_SOCKET_FILTERS)
374 bad_fcode.len = INSN_COUNT;
375 bad_fcode.filter = uninitialized;
376 setsockopt(pcap_fd, SOL_SOCKET, SO_ATTACH_FILTER, &bad_fcode,
377 sizeof(bad_fcode));
378 #endif
379
380 /*
381 * Now compile a filter and set the filter with that.
382 * That should *not* cause valgrind to report a
383 * problem.
384 */
385 if (pcap_compile(pd, &fcode, cmdbuf, 1, 0) < 0)
386 error("can't compile filter: %s", pcap_geterr(pd));
387 if (pcap_setfilter(pd, &fcode) < 0)
388 error("can't set filter: %s", pcap_geterr(pd));
389
390 pcap_close(pd);
391 exit(status < 0 ? 1 : 0);
392 }
393
394 static void
395 usage(void)
396 {
397 (void)fprintf(stderr, "%s, with %s\n", program_name,
398 pcap_lib_version());
399 (void)fprintf(stderr,
400 "Usage: %s [-aI] [ -F file ] [ -I interface ] [ expression ]\n",
401 program_name);
402 exit(1);
403 }