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