]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/libsystemd-network/arp-util.c
Add SPDX license identifiers to source files under the LGPL
[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 "util.h"
28
29 int arp_network_bind_raw_socket(int ifindex, be32_t address, const struct ether_addr *eth_mac) {
30 struct sock_filter filter[] = {
31 BPF_STMT(BPF_LD + BPF_W + BPF_LEN, 0), /* A <- packet length */
32 BPF_JUMP(BPF_JMP + BPF_JGE + BPF_K, sizeof(struct ether_arp), 1, 0), /* packet >= arp packet ? */
33 BPF_STMT(BPF_RET + BPF_K, 0), /* ignore */
34 BPF_STMT(BPF_LD + BPF_H + BPF_ABS, offsetof(struct ether_arp, ea_hdr.ar_hrd)), /* A <- header */
35 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, ARPHRD_ETHER, 1, 0), /* header == ethernet ? */
36 BPF_STMT(BPF_RET + BPF_K, 0), /* ignore */
37 BPF_STMT(BPF_LD + BPF_H + BPF_ABS, offsetof(struct ether_arp, ea_hdr.ar_pro)), /* A <- protocol */
38 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, ETHERTYPE_IP, 1, 0), /* protocol == IP ? */
39 BPF_STMT(BPF_RET + BPF_K, 0), /* ignore */
40 BPF_STMT(BPF_LD + BPF_B + BPF_ABS, offsetof(struct ether_arp, ea_hdr.ar_hln)), /* A <- hardware address length */
41 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, sizeof(struct ether_addr), 1, 0), /* length == sizeof(ether_addr)? */
42 BPF_STMT(BPF_RET + BPF_K, 0), /* ignore */
43 BPF_STMT(BPF_LD + BPF_B + BPF_ABS, offsetof(struct ether_arp, ea_hdr.ar_pln)), /* A <- protocol address length */
44 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, sizeof(struct in_addr), 1, 0), /* length == sizeof(in_addr) ? */
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_op)), /* A <- operation */
47 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, ARPOP_REQUEST, 2, 0), /* protocol == request ? */
48 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, ARPOP_REPLY, 1, 0), /* protocol == reply ? */
49 BPF_STMT(BPF_RET + BPF_K, 0), /* ignore */
50 /* Sender Hardware Address must be different from our own */
51 BPF_STMT(BPF_LD + BPF_IMM, htobe32(*((uint32_t *) eth_mac))), /* A <- 4 bytes of client's MAC */
52 BPF_STMT(BPF_MISC + BPF_TAX, 0), /* X <- A */
53 BPF_STMT(BPF_LD + BPF_W + BPF_ABS, offsetof(struct ether_arp, arp_sha)), /* A <- 4 bytes of SHA */
54 BPF_STMT(BPF_ALU + BPF_XOR + BPF_X, 0), /* A xor X */
55 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, 0, 0, 6), /* A == 0 ? */
56 BPF_STMT(BPF_LD + BPF_IMM, htobe16(*((uint16_t *) (((char *) eth_mac) + 4)))), /* A <- remainder of client's MAC */
57 BPF_STMT(BPF_MISC + BPF_TAX, 0), /* X <- A */
58 BPF_STMT(BPF_LD + BPF_H + BPF_ABS, offsetof(struct ether_arp, arp_sha) + 4), /* A <- remainder of SHA */
59 BPF_STMT(BPF_ALU + BPF_XOR + BPF_X, 0), /* A xor X */
60 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, 0, 0, 1), /* A == 0 ? */
61 BPF_STMT(BPF_RET + BPF_K, 0), /* ignore */
62 /* Sender Protocol Address or Target Protocol Address must be equal to the one we care about */
63 BPF_STMT(BPF_LD + BPF_IMM, htobe32(address)), /* A <- clients IP */
64 BPF_STMT(BPF_MISC + BPF_TAX, 0), /* X <- A */
65 BPF_STMT(BPF_LD + BPF_W + BPF_ABS, offsetof(struct ether_arp, arp_spa)), /* A <- SPA */
66 BPF_STMT(BPF_ALU + BPF_XOR + BPF_X, 0), /* X xor A */
67 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, 0, 0, 1), /* A == 0 ? */
68 BPF_STMT(BPF_RET + BPF_K, 65535), /* return all */
69 BPF_STMT(BPF_LD + BPF_IMM, htobe32(address)), /* A <- clients IP */
70 BPF_STMT(BPF_MISC + BPF_TAX, 0), /* X <- A */
71 BPF_STMT(BPF_LD + BPF_W + BPF_ABS, offsetof(struct ether_arp, arp_tpa)), /* A <- TPA */
72 BPF_STMT(BPF_ALU + BPF_XOR + BPF_X, 0), /* X xor A */
73 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, 0, 0, 1), /* A == 0 ? */
74 BPF_STMT(BPF_RET + BPF_K, 65535), /* return all */
75 BPF_STMT(BPF_RET + BPF_K, 0), /* ignore */
76 };
77 struct sock_fprog fprog = {
78 .len = ELEMENTSOF(filter),
79 .filter = (struct sock_filter*) filter
80 };
81 union sockaddr_union link = {
82 .ll.sll_family = AF_PACKET,
83 .ll.sll_protocol = htobe16(ETH_P_ARP),
84 .ll.sll_ifindex = ifindex,
85 .ll.sll_halen = ETH_ALEN,
86 .ll.sll_addr = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff },
87 };
88 _cleanup_close_ int s = -1;
89 int r;
90
91 assert(ifindex > 0);
92
93 s = socket(PF_PACKET, SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0);
94 if (s < 0)
95 return -errno;
96
97 r = setsockopt(s, SOL_SOCKET, SO_ATTACH_FILTER, &fprog, sizeof(fprog));
98 if (r < 0)
99 return -errno;
100
101 r = bind(s, &link.sa, sizeof(link.ll));
102 if (r < 0)
103 return -errno;
104
105 r = s;
106 s = -1;
107
108 return r;
109 }
110
111 static int arp_send_packet(int fd, int ifindex,
112 be32_t pa, const struct ether_addr *ha,
113 bool announce) {
114 union sockaddr_union link = {
115 .ll.sll_family = AF_PACKET,
116 .ll.sll_protocol = htobe16(ETH_P_ARP),
117 .ll.sll_ifindex = ifindex,
118 .ll.sll_halen = ETH_ALEN,
119 .ll.sll_addr = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff },
120 };
121 struct ether_arp arp = {
122 .ea_hdr.ar_hrd = htobe16(ARPHRD_ETHER), /* HTYPE */
123 .ea_hdr.ar_pro = htobe16(ETHERTYPE_IP), /* PTYPE */
124 .ea_hdr.ar_hln = ETH_ALEN, /* HLEN */
125 .ea_hdr.ar_pln = sizeof(be32_t), /* PLEN */
126 .ea_hdr.ar_op = htobe16(ARPOP_REQUEST), /* REQUEST */
127 };
128 int r;
129
130 assert(fd >= 0);
131 assert(pa != 0);
132 assert(ha);
133
134 memcpy(&arp.arp_sha, ha, ETH_ALEN);
135 memcpy(&arp.arp_tpa, &pa, sizeof(pa));
136
137 if (announce)
138 memcpy(&arp.arp_spa, &pa, sizeof(pa));
139
140 r = sendto(fd, &arp, sizeof(struct ether_arp), 0, &link.sa, sizeof(link.ll));
141 if (r < 0)
142 return -errno;
143
144 return 0;
145 }
146
147 int arp_send_probe(int fd, int ifindex,
148 be32_t pa, const struct ether_addr *ha) {
149 return arp_send_packet(fd, ifindex, pa, ha, false);
150 }
151
152 int arp_send_announcement(int fd, int ifindex,
153 be32_t pa, const struct ether_addr *ha) {
154 return arp_send_packet(fd, ifindex, pa, ha, true);
155 }