Line data Source code
1 : /***
2 : This file is part of systemd.
3 :
4 : Copyright (C) 2014 Axis Communications AB. All rights reserved.
5 :
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.
10 :
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.
15 :
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/>.
18 : ***/
19 :
20 : #include <linux/filter.h>
21 :
22 : #include "util.h"
23 : #include "ipv4ll-internal.h"
24 :
25 0 : int arp_network_send_raw_socket(int fd, const union sockaddr_union *link,
26 : const struct ether_arp *arp) {
27 : int r;
28 :
29 0 : assert(arp);
30 0 : assert(link);
31 0 : assert(fd >= 0);
32 :
33 0 : r = sendto(fd, arp, sizeof(struct ether_arp), 0, &link->sa, sizeof(link->ll));
34 0 : if (r < 0)
35 0 : return -errno;
36 :
37 0 : return 0;
38 : }
39 :
40 0 : int arp_network_bind_raw_socket(int ifindex, union sockaddr_union *link) {
41 :
42 : static const struct sock_filter filter[] = {
43 : BPF_STMT(BPF_LD + BPF_W + BPF_LEN, 0), /* A <- packet length */
44 : BPF_JUMP(BPF_JMP + BPF_JGE + BPF_K, sizeof(struct ether_arp), 1, 0), /* packet >= arp packet ? */
45 : BPF_STMT(BPF_RET + BPF_K, 0), /* ignore */
46 : BPF_STMT(BPF_LD + BPF_H + BPF_ABS, offsetof(struct ether_arp, ea_hdr.ar_hrd)), /* A <- header */
47 : BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, ARPHRD_ETHER, 1, 0), /* header == ethernet ? */
48 : BPF_STMT(BPF_RET + BPF_K, 0), /* ignore */
49 : BPF_STMT(BPF_LD + BPF_H + BPF_ABS, offsetof(struct ether_arp, ea_hdr.ar_pro)), /* A <- protocol */
50 : BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, ETHERTYPE_IP, 1, 0), /* protocol == IP ? */
51 : BPF_STMT(BPF_RET + BPF_K, 0), /* ignore */
52 : BPF_STMT(BPF_LD + BPF_H + BPF_ABS, offsetof(struct ether_arp, ea_hdr.ar_op)), /* A <- operation */
53 : BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, ARPOP_REQUEST, 0, 1), /* protocol == request ? */
54 : BPF_STMT(BPF_RET + BPF_K, 65535), /* return all */
55 : BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, ARPOP_REPLY, 0, 1), /* protocol == reply ? */
56 : BPF_STMT(BPF_RET + BPF_K, 65535), /* return all */
57 : BPF_STMT(BPF_RET + BPF_K, 0), /* ignore */
58 : };
59 0 : struct sock_fprog fprog = {
60 : .len = ELEMENTSOF(filter),
61 : .filter = (struct sock_filter*) filter
62 : };
63 0 : _cleanup_close_ int s = -1;
64 : int r;
65 :
66 0 : assert(ifindex > 0);
67 0 : assert(link);
68 :
69 0 : s = socket(PF_PACKET, SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0);
70 0 : if (s < 0)
71 0 : return -errno;
72 :
73 0 : r = setsockopt(s, SOL_SOCKET, SO_ATTACH_FILTER, &fprog, sizeof(fprog));
74 0 : if (r < 0)
75 0 : return -errno;
76 :
77 0 : link->ll.sll_family = AF_PACKET;
78 0 : link->ll.sll_protocol = htons(ETH_P_ARP);
79 0 : link->ll.sll_ifindex = ifindex;
80 0 : link->ll.sll_halen = ETH_ALEN;
81 0 : memset(link->ll.sll_addr, 0xff, ETH_ALEN);
82 :
83 0 : r = bind(s, &link->sa, sizeof(link->ll));
84 0 : if (r < 0)
85 0 : return -errno;
86 :
87 0 : r = s;
88 0 : s = -1;
89 :
90 0 : return r;
91 : }
|