From 74b2de364f3443fc2414d0160b0b942f347c6fd4 Mon Sep 17 00:00:00 2001 From: Guy Harris Date: Tue, 1 Dec 2009 19:07:11 -0800 Subject: [PATCH] When generating code for "ip broadcast", treat a netmask of 0xffffffff (255.255.255.255) be an indication that the netmask is unknown, and return an error. Document that as the way to tell pcap_compile() that the netmask is unknown. Have filtertest default to that as the netmask, and add a -m flag to let you specify the netmask. --- filtertest.c | 18 +++++++++++++++--- gencode.c | 6 ++++++ pcap_compile.3pcap.in | 6 +++--- 3 files changed, 24 insertions(+), 6 deletions(-) diff --git a/filtertest.c b/filtertest.c index 2424981..e4ecf2d 100644 --- a/filtertest.c +++ b/filtertest.c @@ -39,6 +39,7 @@ static const char rcsid[] _U_ = #include #include #include +#include #include #include @@ -165,6 +166,7 @@ main(int argc, char **argv) int Oflag; long snaplen; int dlt; + bpf_u_int32 netmask = 0xffffffff; char *cmdbuf; pcap_t *pd; struct bpf_program fcode; @@ -184,7 +186,7 @@ main(int argc, char **argv) program_name = argv[0]; opterr = 0; - while ((op = getopt(argc, argv, "dF:Os:")) != -1) { + while ((op = getopt(argc, argv, "dF:m:Os:")) != -1) { switch (op) { case 'd': @@ -199,6 +201,16 @@ main(int argc, char **argv) Oflag = 0; break; + case 'm': { + in_addr_t addr; + + addr = inet_addr(optarg); + if (addr == INADDR_NONE) + error("invalid netmask %s", optarg); + netmask = addr; + break; + } + case 's': { char *end; @@ -235,7 +247,7 @@ main(int argc, char **argv) if (pd == NULL) error("Can't open fake pcap_t"); - if (pcap_compile(pd, &fcode, cmdbuf, Oflag, 0) < 0) + if (pcap_compile(pd, &fcode, cmdbuf, Oflag, netmask) < 0) error("%s", pcap_geterr(pd)); bpf_dump(&fcode, dflag); pcap_close(pd); @@ -248,7 +260,7 @@ usage(void) (void)fprintf(stderr, "%s, with %s\n", program_name, pcap_lib_version()); (void)fprintf(stderr, - "Usage: %s [-dO] [ -F file ] [ -s snaplen ] dlt [ expression ]\n", + "Usage: %s [-dO] [ -F file ] [ -m netmask] [ -s snaplen ] dlt [ expression ]\n", program_name); exit(1); } diff --git a/gencode.c b/gencode.c index 5fe7d54..de57876 100644 --- a/gencode.c +++ b/gencode.c @@ -7118,6 +7118,12 @@ gen_broadcast(proto) break; case Q_IP: + /* + * We treat a netmask of 0xffffffff as an indication + * that we don't know the netmask, and fail. + */ + if (netmask == 0xffffffff) + bpf_error("netmask not known, so 'ip broadcast' not supported"); b0 = gen_linktype(ETHERTYPE_IP); hostmask = ~netmask; b1 = gen_mcmp(OR_NET, 16, BPF_W, (bpf_int32)0, hostmask); diff --git a/pcap_compile.3pcap.in b/pcap_compile.3pcap.in index e1ed513..2c101fa 100644 --- a/pcap_compile.3pcap.in +++ b/pcap_compile.3pcap.in @@ -54,9 +54,9 @@ captured; it is used only when checking for IPv4 broadcast addresses in the filter program. If the netmask of the network on which packets are being captured isn't known to the program, or if packets are being captured on the Linux "any" pseudo-interface that can capture on more -than one network, a value of 0 can be supplied; tests for IPv4 broadcast -addreses won't be done correctly, but all other tests in the filter -program will be OK. +than one network, a value of 0xffffffff can be supplied; tests for +IPv4 broadcast addreses will fail to compile, but all other tests in +the filter program will be OK. .SH RETURN VALUE .B pcap_compile() returns 0 on success and \-1 on failure.