]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/libsystemd-network/arp-util.c
Merge pull request #7817 from medhefgo/systemd-boot
[thirdparty/systemd.git] / src / libsystemd-network / arp-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 This file is part of systemd.
4
5 Copyright (C) 2014 Axis Communications AB. All rights reserved.
6 Copyright (C) 2015 Tom Gundersen
7
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
12
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <linux/filter.h>
23 #include <arpa/inet.h>
24
25 #include "arp-util.h"
26 #include "fd-util.h"
27 #include "unaligned.h"
28 #include "util.h"
29
30 int arp_network_bind_raw_socket(int ifindex, be32_t address, const struct ether_addr *eth_mac) {
31 struct sock_filter filter[] = {
32 BPF_STMT(BPF_LD + BPF_W + BPF_LEN, 0), /* A <- packet length */
33 BPF_JUMP(BPF_JMP + BPF_JGE + BPF_K, sizeof(struct ether_arp), 1, 0), /* packet >= arp packet ? */
34 BPF_STMT(BPF_RET + BPF_K, 0), /* ignore */
35 BPF_STMT(BPF_LD + BPF_H + BPF_ABS, offsetof(struct ether_arp, ea_hdr.ar_hrd)), /* A <- header */
36 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, ARPHRD_ETHER, 1, 0), /* header == ethernet ? */
37 BPF_STMT(BPF_RET + BPF_K, 0), /* ignore */
38 BPF_STMT(BPF_LD + BPF_H + BPF_ABS, offsetof(struct ether_arp, ea_hdr.ar_pro)), /* A <- protocol */
39 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, ETHERTYPE_IP, 1, 0), /* protocol == IP ? */
40 BPF_STMT(BPF_RET + BPF_K, 0), /* ignore */
41 BPF_STMT(BPF_LD + BPF_B + BPF_ABS, offsetof(struct ether_arp, ea_hdr.ar_hln)), /* A <- hardware address length */
42 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, sizeof(struct ether_addr), 1, 0), /* length == sizeof(ether_addr)? */
43 BPF_STMT(BPF_RET + BPF_K, 0), /* ignore */
44 BPF_STMT(BPF_LD + BPF_B + BPF_ABS, offsetof(struct ether_arp, ea_hdr.ar_pln)), /* A <- protocol address length */
45 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, sizeof(struct in_addr), 1, 0), /* length == sizeof(in_addr) ? */
46 BPF_STMT(BPF_RET + BPF_K, 0), /* ignore */
47 BPF_STMT(BPF_LD + BPF_H + BPF_ABS, offsetof(struct ether_arp, ea_hdr.ar_op)), /* A <- operation */
48 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, ARPOP_REQUEST, 2, 0), /* protocol == request ? */
49 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, ARPOP_REPLY, 1, 0), /* protocol == reply ? */
50 BPF_STMT(BPF_RET + BPF_K, 0), /* ignore */
51 /* Sender Hardware Address must be different from our own */
52 BPF_STMT(BPF_LD + BPF_IMM, unaligned_read_be32(&eth_mac->ether_addr_octet[0])),/* A <- 4 bytes of client's MAC */
53 BPF_STMT(BPF_MISC + BPF_TAX, 0), /* X <- A */
54 BPF_STMT(BPF_LD + BPF_W + BPF_ABS, offsetof(struct ether_arp, arp_sha)), /* A <- 4 bytes of SHA */
55 BPF_STMT(BPF_ALU + BPF_XOR + BPF_X, 0), /* A xor X */
56 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, 0, 0, 6), /* A == 0 ? */
57 BPF_STMT(BPF_LD + BPF_IMM, unaligned_read_be16(&eth_mac->ether_addr_octet[4])),/* A <- remainder of client's MAC */
58 BPF_STMT(BPF_MISC + BPF_TAX, 0), /* X <- A */
59 BPF_STMT(BPF_LD + BPF_H + BPF_ABS, offsetof(struct ether_arp, arp_sha) + 4), /* A <- remainder of SHA */
60 BPF_STMT(BPF_ALU + BPF_XOR + BPF_X, 0), /* A xor X */
61 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, 0, 0, 1), /* A == 0 ? */
62 BPF_STMT(BPF_RET + BPF_K, 0), /* ignore */
63 /* Sender Protocol Address or Target Protocol Address must be equal to the one we care about */
64 BPF_STMT(BPF_LD + BPF_IMM, htobe32(address)), /* A <- clients IP */
65 BPF_STMT(BPF_MISC + BPF_TAX, 0), /* X <- A */
66 BPF_STMT(BPF_LD + BPF_W + BPF_ABS, offsetof(struct ether_arp, arp_spa)), /* A <- SPA */
67 BPF_STMT(BPF_ALU + BPF_XOR + BPF_X, 0), /* X xor A */
68 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, 0, 0, 1), /* A == 0 ? */
69 BPF_STMT(BPF_RET + BPF_K, 65535), /* return all */
70 BPF_STMT(BPF_LD + BPF_IMM, htobe32(address)), /* A <- clients IP */
71 BPF_STMT(BPF_MISC + BPF_TAX, 0), /* X <- A */
72 BPF_STMT(BPF_LD + BPF_W + BPF_ABS, offsetof(struct ether_arp, arp_tpa)), /* A <- TPA */
73 BPF_STMT(BPF_ALU + BPF_XOR + BPF_X, 0), /* X xor A */
74 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, 0, 0, 1), /* A == 0 ? */
75 BPF_STMT(BPF_RET + BPF_K, 65535), /* return all */
76 BPF_STMT(BPF_RET + BPF_K, 0), /* ignore */
77 };
78 struct sock_fprog fprog = {
79 .len = ELEMENTSOF(filter),
80 .filter = (struct sock_filter*) filter
81 };
82 union sockaddr_union link = {
83 .ll.sll_family = AF_PACKET,
84 .ll.sll_protocol = htobe16(ETH_P_ARP),
85 .ll.sll_ifindex = ifindex,
86 .ll.sll_halen = ETH_ALEN,
87 .ll.sll_addr = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff },
88 };
89 _cleanup_close_ int s = -1;
90 int r;
91
92 assert(ifindex > 0);
93
94 s = socket(PF_PACKET, SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0);
95 if (s < 0)
96 return -errno;
97
98 r = setsockopt(s, SOL_SOCKET, SO_ATTACH_FILTER, &fprog, sizeof(fprog));
99 if (r < 0)
100 return -errno;
101
102 r = bind(s, &link.sa, sizeof(link.ll));
103 if (r < 0)
104 return -errno;
105
106 r = s;
107 s = -1;
108
109 return r;
110 }
111
112 static int arp_send_packet(int fd, int ifindex,
113 be32_t pa, const struct ether_addr *ha,
114 bool announce) {
115 union sockaddr_union link = {
116 .ll.sll_family = AF_PACKET,
117 .ll.sll_protocol = htobe16(ETH_P_ARP),
118 .ll.sll_ifindex = ifindex,
119 .ll.sll_halen = ETH_ALEN,
120 .ll.sll_addr = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff },
121 };
122 struct ether_arp arp = {
123 .ea_hdr.ar_hrd = htobe16(ARPHRD_ETHER), /* HTYPE */
124 .ea_hdr.ar_pro = htobe16(ETHERTYPE_IP), /* PTYPE */
125 .ea_hdr.ar_hln = ETH_ALEN, /* HLEN */
126 .ea_hdr.ar_pln = sizeof(be32_t), /* PLEN */
127 .ea_hdr.ar_op = htobe16(ARPOP_REQUEST), /* REQUEST */
128 };
129 int r;
130
131 assert(fd >= 0);
132 assert(pa != 0);
133 assert(ha);
134
135 memcpy(&arp.arp_sha, ha, ETH_ALEN);
136 memcpy(&arp.arp_tpa, &pa, sizeof(pa));
137
138 if (announce)
139 memcpy(&arp.arp_spa, &pa, sizeof(pa));
140
141 r = sendto(fd, &arp, sizeof(struct ether_arp), 0, &link.sa, sizeof(link.ll));
142 if (r < 0)
143 return -errno;
144
145 return 0;
146 }
147
148 int arp_send_probe(int fd, int ifindex,
149 be32_t pa, const struct ether_addr *ha) {
150 return arp_send_packet(fd, ifindex, pa, ha, false);
151 }
152
153 int arp_send_announcement(int fd, int ifindex,
154 be32_t pa, const struct ether_addr *ha) {
155 return arp_send_packet(fd, ifindex, pa, ha, true);
156 }