summaryrefslogtreecommitdiff
path: root/src/libsystemd-network/arp-util.c
diff options
context:
space:
mode:
authorYu Watanabe <watanabe.yu+github@gmail.com>2021-06-21 02:21:59 +0900
committerYu Watanabe <watanabe.yu+github@gmail.com>2021-07-01 00:49:02 +0900
commitd17ed573aae791ee440a1c84746868194896aa71 (patch)
treebbaecbe7511daa88adc93c72a24a16c76d8d0aa7 /src/libsystemd-network/arp-util.c
parentecad63f8503ea9f6264b34936b0ac8e7b6e718ac (diff)
downloadsystemd-d17ed573aae791ee440a1c84746868194896aa71.tar.gz
arp-util: split out logic of setting BPF code into a function
Diffstat (limited to 'src/libsystemd-network/arp-util.c')
-rw-r--r--src/libsystemd-network/arp-util.c18
1 files changed, 15 insertions, 3 deletions
diff --git a/src/libsystemd-network/arp-util.c b/src/libsystemd-network/arp-util.c
index cb15e51fd1..d8d94ab5cb 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;