chiark / gitweb /
core: add new ConditionNeedsUpdate= unit condition
[elogind.git] / src / libsystemd-network / ipv4ll-network.c
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 int arp_network_send_raw_socket(int fd, const union sockaddr_union *link,
26                                         const struct ether_arp *arp) {
27         int r;
28
29         assert(arp);
30         assert(link);
31         assert(fd >= 0);
32
33         r = sendto(fd, arp, sizeof(struct ether_arp), 0, &link->sa, sizeof(link->ll));
34         if (r < 0)
35                 return -errno;
36
37         return 0;
38 }
39
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 */
57         };
58         struct sock_fprog fprog = {
59             .len = ELEMENTSOF(filter),
60             .filter = filter
61         };
62         _cleanup_close_ int s = -1;
63         int r;
64
65         assert(index > 0);
66         assert(link);
67
68         s = socket(PF_PACKET, SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0);
69         if (s < 0)
70                 return -errno;
71
72         r = setsockopt(s, SOL_SOCKET, SO_ATTACH_FILTER, &fprog, sizeof(fprog));
73         if (r < 0)
74                 return -errno;
75
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);
81
82         r = bind(s, &link->sa, sizeof(link->ll));
83         if (r < 0)
84                 return -errno;
85
86         r = s;
87         s = -1;
88
89         return r;
90 }