2 This file is part of systemd.
4 Copyright (C) 2014 Axis Communications AB. All rights reserved.
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 #include <linux/filter.h>
23 #include "ipv4ll-internal.h"
25 int arp_network_send_raw_socket(int fd, const union sockaddr_union *link,
26 const struct ether_arp *arp) {
33 r = sendto(fd, arp, sizeof(struct ether_arp), 0, &link->sa, sizeof(link->ll));
40 int arp_network_bind_raw_socket(int index, union sockaddr_union *link) {
41 struct sock_filter filter[] = {
42 BPF_STMT(BPF_LD + BPF_W + BPF_LEN, 0), /* A <- packet length */
43 BPF_JUMP(BPF_JMP + BPF_JGE + BPF_K, sizeof(struct ether_arp), 1, 0), /* packet >= arp packet ? */
44 BPF_STMT(BPF_RET + BPF_K, 0), /* ignore */
45 BPF_STMT(BPF_LD + BPF_H + BPF_ABS, offsetof(struct ether_arp, ea_hdr.ar_hrd)), /* A <- header */
46 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, ARPHRD_ETHER, 1, 0), /* header == ethernet ? */
47 BPF_STMT(BPF_RET + BPF_K, 0), /* ignore */
48 BPF_STMT(BPF_LD + BPF_H + BPF_ABS, offsetof(struct ether_arp, ea_hdr.ar_pro)), /* A <- protocol */
49 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, ETHERTYPE_IP, 1, 0), /* protocol == IP ? */
50 BPF_STMT(BPF_RET + BPF_K, 0), /* ignore */
51 BPF_STMT(BPF_LD + BPF_H + BPF_ABS, offsetof(struct ether_arp, ea_hdr.ar_op)), /* A <- operation */
52 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, ARPOP_REQUEST, 0, 1), /* protocol == request ? */
53 BPF_STMT(BPF_RET + BPF_K, 65535), /* return all */
54 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, ARPOP_REPLY, 0, 1), /* protocol == reply ? */
55 BPF_STMT(BPF_RET + BPF_K, 65535), /* return all */
56 BPF_STMT(BPF_RET + BPF_K, 0), /* ignore */
58 struct sock_fprog fprog = {
59 .len = ELEMENTSOF(filter),
62 _cleanup_close_ int s = -1;
68 s = socket(PF_PACKET, SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0);
72 r = setsockopt(s, SOL_SOCKET, SO_ATTACH_FILTER, &fprog, sizeof(fprog));
76 link->ll.sll_family = AF_PACKET;
77 link->ll.sll_protocol = htons(ETH_P_ARP);
78 link->ll.sll_ifindex = index;
79 link->ll.sll_halen = ETH_ALEN;
80 memset(link->ll.sll_addr, 0xff, ETH_ALEN);
82 r = bind(s, &link->sa, sizeof(link->ll));