]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
arp-util: split out logic of setting BPF code into a function
authorYu Watanabe <watanabe.yu+github@gmail.com>
Sun, 20 Jun 2021 17:21:59 +0000 (02:21 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 30 Jun 2021 15:49:02 +0000 (00:49 +0900)
src/libsystemd-network/arp-util.c
src/libsystemd-network/arp-util.h
src/libsystemd-network/test-ipv4ll.c

index cb15e51fd112153c44b3a63609d73ad140dc853b..d8d94ab5cbd1087ed2dcba86fa1b64d2ec2a2a0c 100644 (file)
@@ -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;
index c711b573127a71654c1b72081b7c552004fe2368..e8615a158d58cda742d24e152bef6d122dd5f414 100644 (file)
@@ -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,
index 1036b26412a67e888b184392a5acc316f61a9be3..7985185b33d563fbb76dc1394f39bb60dab29e71 100644 (file)
@@ -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;