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.
This commit is contained in:
parent
8b52a90cd9
commit
74b2de364f
18
filtertest.c
18
filtertest.c
|
@ -39,6 +39,7 @@ static const char rcsid[] _U_ =
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
#include <arpa/inet.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
|
|
||||||
|
@ -165,6 +166,7 @@ main(int argc, char **argv)
|
||||||
int Oflag;
|
int Oflag;
|
||||||
long snaplen;
|
long snaplen;
|
||||||
int dlt;
|
int dlt;
|
||||||
|
bpf_u_int32 netmask = 0xffffffff;
|
||||||
char *cmdbuf;
|
char *cmdbuf;
|
||||||
pcap_t *pd;
|
pcap_t *pd;
|
||||||
struct bpf_program fcode;
|
struct bpf_program fcode;
|
||||||
|
@ -184,7 +186,7 @@ main(int argc, char **argv)
|
||||||
program_name = argv[0];
|
program_name = argv[0];
|
||||||
|
|
||||||
opterr = 0;
|
opterr = 0;
|
||||||
while ((op = getopt(argc, argv, "dF:Os:")) != -1) {
|
while ((op = getopt(argc, argv, "dF:m:Os:")) != -1) {
|
||||||
switch (op) {
|
switch (op) {
|
||||||
|
|
||||||
case 'd':
|
case 'd':
|
||||||
|
@ -199,6 +201,16 @@ main(int argc, char **argv)
|
||||||
Oflag = 0;
|
Oflag = 0;
|
||||||
break;
|
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': {
|
case 's': {
|
||||||
char *end;
|
char *end;
|
||||||
|
|
||||||
|
@ -235,7 +247,7 @@ main(int argc, char **argv)
|
||||||
if (pd == NULL)
|
if (pd == NULL)
|
||||||
error("Can't open fake pcap_t");
|
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));
|
error("%s", pcap_geterr(pd));
|
||||||
bpf_dump(&fcode, dflag);
|
bpf_dump(&fcode, dflag);
|
||||||
pcap_close(pd);
|
pcap_close(pd);
|
||||||
|
@ -248,7 +260,7 @@ usage(void)
|
||||||
(void)fprintf(stderr, "%s, with %s\n", program_name,
|
(void)fprintf(stderr, "%s, with %s\n", program_name,
|
||||||
pcap_lib_version());
|
pcap_lib_version());
|
||||||
(void)fprintf(stderr,
|
(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);
|
program_name);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
|
@ -7118,6 +7118,12 @@ gen_broadcast(proto)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case Q_IP:
|
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);
|
b0 = gen_linktype(ETHERTYPE_IP);
|
||||||
hostmask = ~netmask;
|
hostmask = ~netmask;
|
||||||
b1 = gen_mcmp(OR_NET, 16, BPF_W, (bpf_int32)0, hostmask);
|
b1 = gen_mcmp(OR_NET, 16, BPF_W, (bpf_int32)0, hostmask);
|
||||||
|
|
|
@ -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
|
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
|
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
|
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
|
than one network, a value of 0xffffffff can be supplied; tests for
|
||||||
addreses won't be done correctly, but all other tests in the filter
|
IPv4 broadcast addreses will fail to compile, but all other tests in
|
||||||
program will be OK.
|
the filter program will be OK.
|
||||||
.SH RETURN VALUE
|
.SH RETURN VALUE
|
||||||
.B pcap_compile()
|
.B pcap_compile()
|
||||||
returns 0 on success and \-1 on failure.
|
returns 0 on success and \-1 on failure.
|
||||||
|
|
Reference in New Issue