From: Yu Watanabe Date: Sun, 20 Jun 2021 17:21:59 +0000 (+0900) Subject: arp-util: split out logic of setting BPF code into a function X-Git-Tag: v249-rc3~17^2~21 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d17ed573aae791ee440a1c84746868194896aa71;p=thirdparty%2Fsystemd.git arp-util: split out logic of setting BPF code into a function --- diff --git a/src/libsystemd-network/arp-util.c b/src/libsystemd-network/arp-util.c index cb15e51fd11..d8d94ab5cbd 100644 --- a/src/libsystemd-network/arp-util.c +++ b/src/libsystemd-network/arp-util.c @@ -14,7 +14,7 @@ #include "unaligned.h" #include "util.h" -int arp_network_bind_raw_socket(int ifindex, const struct in_addr *a, const struct ether_addr *eth_mac) { +int arp_update_filter(int fd, const struct in_addr *a, const struct ether_addr *eth_mac) { struct sock_filter filter[] = { BPF_STMT(BPF_LD + BPF_W + BPF_LEN, 0), /* A <- packet length */ BPF_JUMP(BPF_JMP + BPF_JGE + BPF_K, sizeof(struct ether_arp), 1, 0), /* packet >= arp packet ? */ @@ -66,6 +66,16 @@ int arp_network_bind_raw_socket(int ifindex, const struct in_addr *a, const stru .len = ELEMENTSOF(filter), .filter = (struct sock_filter*) filter, }; + + assert(fd >= 0); + + if (setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, &fprog, sizeof(fprog)) < 0) + return -errno; + + return 0; +} + +int arp_network_bind_raw_socket(int ifindex, const struct in_addr *a, const struct ether_addr *eth_mac) { union sockaddr_union link = { .ll.sll_family = AF_PACKET, .ll.sll_protocol = htobe16(ETH_P_ARP), @@ -74,6 +84,7 @@ int arp_network_bind_raw_socket(int ifindex, const struct in_addr *a, const stru .ll.sll_addr = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, }; _cleanup_close_ int s = -1; + int r; assert(ifindex > 0); @@ -81,8 +92,9 @@ int arp_network_bind_raw_socket(int ifindex, const struct in_addr *a, const stru if (s < 0) return -errno; - if (setsockopt(s, SOL_SOCKET, SO_ATTACH_FILTER, &fprog, sizeof(fprog)) < 0) - return -errno; + r = arp_update_filter(s, a, eth_mac); + if (r < 0) + return r; if (bind(s, &link.sa, sizeof(link.ll)) < 0) return -errno; diff --git a/src/libsystemd-network/arp-util.h b/src/libsystemd-network/arp-util.h index c711b573127..e8615a158d5 100644 --- a/src/libsystemd-network/arp-util.h +++ b/src/libsystemd-network/arp-util.h @@ -11,7 +11,8 @@ #include "socket-util.h" #include "sparse-endian.h" -int arp_network_bind_raw_socket(int index, const struct in_addr *a, const struct ether_addr *eth_mac); +int arp_update_filter(int fd, const struct in_addr *a, const struct ether_addr *eth_mac); +int arp_network_bind_raw_socket(int ifindex, const struct in_addr *a, const struct ether_addr *eth_mac); int arp_send_packet( int fd, diff --git a/src/libsystemd-network/test-ipv4ll.c b/src/libsystemd-network/test-ipv4ll.c index 1036b26412a..7985185b33d 100644 --- a/src/libsystemd-network/test-ipv4ll.c +++ b/src/libsystemd-network/test-ipv4ll.c @@ -62,6 +62,10 @@ int arp_send_packet( return 0; } +int arp_update_filter(int fd, const struct in_addr *a, const struct ether_addr *eth_mac) { + return 0; +} + int arp_network_bind_raw_socket(int ifindex, const struct in_addr *a, const struct ether_addr *eth_mac) { if (socketpair(AF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0, test_fd) < 0) return -errno;