]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/libsystemd-network/ipv4ll-network.c
dhcp6-option: Add helper function for uncompressed domain names
[thirdparty/systemd.git] / src / libsystemd-network / ipv4ll-network.c
CommitLineData
5c1d3fc9
UTL
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
298f77c6
TG
20#include <linux/filter.h>
21
5c1d3fc9
UTL
22#include "util.h"
23#include "ipv4ll-internal.h"
24
25int arp_network_send_raw_socket(int fd, const union sockaddr_union *link,
26 const struct ether_arp *arp) {
c3d2994b
TG
27 int r;
28
5c1d3fc9
UTL
29 assert(arp);
30 assert(link);
31 assert(fd >= 0);
32
c3d2994b
TG
33 r = sendto(fd, arp, sizeof(struct ether_arp), 0, &link->sa, sizeof(link->ll));
34 if (r < 0)
5c1d3fc9
UTL
35 return -errno;
36
37 return 0;
38}
39
088b6ba2
LP
40int 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 */
298f77c6
TG
58 };
59 struct sock_fprog fprog = {
088b6ba2
LP
60 .len = ELEMENTSOF(filter),
61 .filter = (struct sock_filter*) filter
298f77c6 62 };
c3d2994b
TG
63 _cleanup_close_ int s = -1;
64 int r;
5c1d3fc9 65
088b6ba2 66 assert(ifindex > 0);
5c1d3fc9
UTL
67 assert(link);
68
bcf3ce7b 69 s = socket(PF_PACKET, SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0);
5c1d3fc9
UTL
70 if (s < 0)
71 return -errno;
72
c3d2994b
TG
73 r = setsockopt(s, SOL_SOCKET, SO_ATTACH_FILTER, &fprog, sizeof(fprog));
74 if (r < 0)
298f77c6 75 return -errno;
298f77c6 76
5c1d3fc9 77 link->ll.sll_family = AF_PACKET;
5c1d3fc9 78 link->ll.sll_protocol = htons(ETH_P_ARP);
088b6ba2 79 link->ll.sll_ifindex = ifindex;
5c1d3fc9 80 link->ll.sll_halen = ETH_ALEN;
463b73e2 81 memset(link->ll.sll_addr, 0xff, ETH_ALEN);
5c1d3fc9 82
c3d2994b
TG
83 r = bind(s, &link->sa, sizeof(link->ll));
84 if (r < 0)
5c1d3fc9 85 return -errno;
5c1d3fc9 86
c3d2994b
TG
87 r = s;
88 s = -1;
89
90 return r;
5c1d3fc9 91}