]> git.ipfire.org Git - thirdparty/dhcpcd.git/commitdiff
Use constant BPF structures and defines to alter the data instead of functions. Small...
authorRoy Marples <roy@marples.name>
Thu, 22 May 2008 13:59:33 +0000 (13:59 +0000)
committerRoy Marples <roy@marples.name>
Thu, 22 May 2008 13:59:33 +0000 (13:59 +0000)
bpf-filter.h
dhcpcd.c
lpf.c
net.h

index 5911803937045fed469fa960dafa882c99b6b7d3..16930dd0eebd34817a7af09eac9d4fa23a6cfc47 100644 (file)
  * SUCH DAMAGE.
  */
 
-/* Credit where credit is due :)
- * The below BPF filter is taken from ISC DHCP */
-static struct bpf_insn dhcp_bpf_filter [] = {
+#ifndef BPF_ETHCOOK
+# define BPF_ETHCOOK 0
+#endif
+#ifndef BPF_WHOLEPACKET
+# define BPF_WHOLEPACKET ~0U
+#endif
+
+static const struct bpf_insn const dhcp_bpf_filter [] = {
+#ifndef BPF_SKIPTYPE
        /* Make sure this is an IP packet... */
-       BPF_STMT (BPF_LD + BPF_H + BPF_ABS, 12),
-       BPF_JUMP (BPF_JMP + BPF_JEQ + BPF_K, ETHERTYPE_IP, 0, 8),
+       BPF_STMT(BPF_LD + BPF_H + BPF_ABS, 12),
+       BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, ETHERTYPE_IP, 0, 8),
+#endif
 
        /* Make sure it's a UDP packet... */
-       BPF_STMT (BPF_LD + BPF_B + BPF_ABS, 23),
-       BPF_JUMP (BPF_JMP + BPF_JEQ + BPF_K, IPPROTO_UDP, 0, 6),
+       BPF_STMT(BPF_LD + BPF_B + BPF_ABS, 23 + BPF_ETHCOOK),
+       BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, IPPROTO_UDP, 0, 6),
 
        /* Make sure this isn't a fragment... */
-       BPF_STMT (BPF_LD + BPF_H + BPF_ABS, 20),
-       BPF_JUMP (BPF_JMP + BPF_JSET + BPF_K, 0x1fff, 4, 0),
+       BPF_STMT(BPF_LD + BPF_H + BPF_ABS, 20 + BPF_ETHCOOK),
+       BPF_JUMP(BPF_JMP + BPF_JSET + BPF_K, 0x1fff, 4, 0),
 
        /* Get the IP header length... */
-       BPF_STMT (BPF_LDX + BPF_B + BPF_MSH, 14),
+       BPF_STMT(BPF_LDX + BPF_B + BPF_MSH, 14 + BPF_ETHCOOK),
 
        /* Make sure it's to the right port... */
-       BPF_STMT (BPF_LD + BPF_H + BPF_IND, 16),
-       BPF_JUMP (BPF_JMP + BPF_JEQ + BPF_K, DHCP_CLIENT_PORT, 0, 1),
+       BPF_STMT(BPF_LD + BPF_H + BPF_IND, 16 + BPF_ETHCOOK),
+       BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, DHCP_CLIENT_PORT, 0, 1),
 
        /* If we passed all the tests, ask for the whole packet. */
-       BPF_STMT (BPF_RET + BPF_K, ~0U),
+       BPF_STMT(BPF_RET + BPF_K, BPF_WHOLEPACKET),
 
        /* Otherwise, drop it. */
-       BPF_STMT (BPF_RET + BPF_K, 0),
+       BPF_STMT(BPF_RET + BPF_K, 0),
 };
 static const size_t dhcp_bpf_filter_len =
     sizeof(dhcp_bpf_filter) / sizeof(dhcp_bpf_filter[0]);
 
-
-/* This, however, is mine */
-static struct bpf_insn arp_bpf_filter [] = {
+static const struct bpf_insn const arp_bpf_filter [] = {
+#ifndef BPF_SKIPTYPE
        /* Make sure this is an ARP packet... */
-       BPF_STMT (BPF_LD + BPF_H + BPF_ABS, 12),
-       BPF_JUMP (BPF_JMP + BPF_JEQ + BPF_K, ETHERTYPE_ARP, 0, 3),
+       BPF_STMT(BPF_LD + BPF_H + BPF_ABS, 12),
+       BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, ETHERTYPE_ARP, 0, 3),
+#endif
 
        /* Make sure this is an ARP REPLY... */
-       BPF_STMT (BPF_LD + BPF_H + BPF_ABS, 20),
-       BPF_JUMP (BPF_JMP + BPF_JEQ + BPF_K, ARPOP_REPLY, 0, 1),
+       BPF_STMT(BPF_LD + BPF_H + BPF_ABS, 20 + BPF_ETHCOOK),
+       BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, ARPOP_REPLY, 0, 1),
 
        /* If we passed all the tests, ask for the whole packet. */
-       BPF_STMT (BPF_RET + BPF_K, ~0U),
+       BPF_STMT(BPF_RET + BPF_K, BPF_WHOLEPACKET),
 
        /* Otherwise, drop it. */
-       BPF_STMT (BPF_RET + BPF_K, 0),
+       BPF_STMT(BPF_RET + BPF_K, 0),
 };
 static const size_t arp_bpf_filter_len =
     sizeof(arp_bpf_filter) / sizeof(arp_bpf_filter[0]);
index 2d843257de0f0dfae8367fffcd49718574e26493..4ca3dc17d1e0eae2104f44638ebebc62dc0eb93c 100644 (file)
--- a/dhcpcd.c
+++ b/dhcpcd.c
@@ -824,11 +824,6 @@ main(int argc, char **argv)
                logger(LOG_INFO, PACKAGE " " VERSION " starting");
        }
 
-#ifdef __linux__
-       /* Massage our filters per platform */
-       setup_packet_filters();
-#endif
-
        if (dhcp_run(options, &pidfd) == 0)
                retval = EXIT_SUCCESS;
 
diff --git a/lpf.c b/lpf.c
index 0e0b2cf7cc7a3d358a701bdc797abdcc8d4505be..4491e0be6f31857ebf27009891a01914d4e8d46b 100644 (file)
--- a/lpf.c
+++ b/lpf.c
@@ -38,6 +38,9 @@
 # include <linux/filter.h>
 # include <netpacket/packet.h>
 # define bpf_insn sock_filter
+# define BPF_SKIPTYPE
+# define BPF_ETHCOOK           -ETH_HLEN
+# define BPF_WHOLEPACKET       65535 /* work around buggy LPF filters */
 #endif
 
 #include <errno.h>
@@ -64,27 +67,6 @@ static const uint8_t ipv4_bcast_addr[] = {
        0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff
 };
 
-void
-setup_packet_filters(void)
-{
-#ifdef __linux__
-       /* We need to massage the filters for Linux cooked packets */
-       dhcp_bpf_filter[1].jf = 0; /* skip the IP packet type check */
-       dhcp_bpf_filter[2].k -= ETH_HLEN;
-       dhcp_bpf_filter[4].k -= ETH_HLEN;
-       dhcp_bpf_filter[6].k -= ETH_HLEN;
-       dhcp_bpf_filter[7].k -= ETH_HLEN;
-
-       arp_bpf_filter[1].jf = 0; /* skip the IP packet type check */
-       arp_bpf_filter[2].k -= ETH_HLEN;
-
-       /* Some buggy Linux kernels do not work with ~0U.
-        * 65536 should be enough for anyone ;) */
-       dhcp_bpf_filter[9].k = 65536;
-       arp_bpf_filter[5].k = 65536;
-#endif
-}
-
 int
 open_socket(struct interface *iface, int protocol)
 {
@@ -108,14 +90,13 @@ open_socket(struct interface *iface, int protocol)
                errno = ENOENT;
                goto eexit;
        }
-
        /* Install the DHCP filter */
        memset(&pf, 0, sizeof(pf));
        if (protocol == ETHERTYPE_ARP) {
-               pf.filter = arp_bpf_filter;
+               pf.filter = UNCONST(arp_bpf_filter);
                pf.len = arp_bpf_filter_len;
        } else {
-               pf.filter = dhcp_bpf_filter;
+               pf.filter = UNCONST(dhcp_bpf_filter);
                pf.len = dhcp_bpf_filter_len;
        }
        if (setsockopt(s, SOL_SOCKET, SO_ATTACH_FILTER, &pf, sizeof(pf)) != 0)
diff --git a/net.h b/net.h
index e7ed5941082760edae2cb27f37bbbcceb905c6ff..d821fdbd6367926fe2eb7f50ff8e7840756409f4 100644 (file)
--- a/net.h
+++ b/net.h
@@ -163,9 +163,6 @@ ssize_t make_udp_packet(uint8_t **, const uint8_t *, size_t,
 ssize_t get_udp_data(const uint8_t **, const uint8_t *);
 int valid_udp_packet(const uint8_t *);
 
-#ifdef __linux__
-void setup_packet_filters(void);
-#endif
 int open_socket(struct interface *, int);
 ssize_t send_packet(const struct interface *, struct in_addr, 
                    const uint8_t *, ssize_t);