]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/libsystemd-network/dhcp-network.c
dhcp: split out dhcp-network.h from dhcp-internal.h
[thirdparty/systemd.git] / src / libsystemd-network / dhcp-network.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 /***
3 Copyright © 2013 Intel Corporation. All rights reserved.
4 ***/
5
6 #include <errno.h>
7 #include <net/ethernet.h>
8 #include <net/if.h>
9 #include <net/if_arp.h>
10 #include <stdio.h>
11 #include <string.h>
12 #include <linux/filter.h>
13 #include <linux/if_infiniband.h>
14 #include <linux/if_packet.h>
15
16 #include "dhcp-network.h"
17 #include "dhcp-protocol.h"
18 #include "fd-util.h"
19 #include "unaligned.h"
20
21 static int _bind_raw_socket(
22 int ifindex,
23 union sockaddr_union *link,
24 uint32_t xid,
25 const struct hw_addr_data *hw_addr,
26 const struct hw_addr_data *bcast_addr,
27 uint16_t arp_type,
28 uint16_t port,
29 bool so_priority_set,
30 int so_priority) {
31
32 assert(ifindex > 0);
33 assert(link);
34 assert(hw_addr);
35 assert(bcast_addr);
36 assert(IN_SET(arp_type, ARPHRD_ETHER, ARPHRD_INFINIBAND));
37
38 switch (arp_type) {
39 case ARPHRD_ETHER:
40 assert(hw_addr->length == ETH_ALEN);
41 assert(bcast_addr->length == ETH_ALEN);
42 break;
43 case ARPHRD_INFINIBAND:
44 assert(hw_addr->length == 0);
45 assert(bcast_addr->length == INFINIBAND_ALEN);
46 break;
47 default:
48 assert_not_reached();
49 }
50
51 struct sock_filter filter[] = {
52 BPF_STMT(BPF_LD + BPF_W + BPF_LEN, 0), /* A <- packet length */
53 BPF_JUMP(BPF_JMP + BPF_JGE + BPF_K, sizeof(DHCPPacket), 1, 0), /* packet >= DHCPPacket ? */
54 BPF_STMT(BPF_RET + BPF_K, 0), /* ignore */
55 BPF_STMT(BPF_LD + BPF_B + BPF_ABS, offsetof(DHCPPacket, ip.protocol)), /* A <- IP protocol */
56 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, IPPROTO_UDP, 1, 0), /* IP protocol == UDP ? */
57 BPF_STMT(BPF_RET + BPF_K, 0), /* ignore */
58 BPF_STMT(BPF_LD + BPF_B + BPF_ABS, offsetof(DHCPPacket, ip.frag_off)), /* A <- Flags */
59 BPF_STMT(BPF_ALU + BPF_AND + BPF_K, 0x20), /* A <- A & 0x20 (More Fragments bit) */
60 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, 0, 1, 0), /* A == 0 ? */
61 BPF_STMT(BPF_RET + BPF_K, 0), /* ignore */
62 BPF_STMT(BPF_LD + BPF_H + BPF_ABS, offsetof(DHCPPacket, ip.frag_off)), /* A <- Flags + Fragment offset */
63 BPF_STMT(BPF_ALU + BPF_AND + BPF_K, 0x1fff), /* A <- A & 0x1fff (Fragment offset) */
64 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, 0, 1, 0), /* A == 0 ? */
65 BPF_STMT(BPF_RET + BPF_K, 0), /* ignore */
66 BPF_STMT(BPF_LD + BPF_H + BPF_ABS, offsetof(DHCPPacket, udp.dest)), /* A <- UDP destination port */
67 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, port, 1, 0), /* UDP destination port == DHCP client port ? */
68 BPF_STMT(BPF_RET + BPF_K, 0), /* ignore */
69 BPF_STMT(BPF_LD + BPF_B + BPF_ABS, offsetof(DHCPPacket, dhcp.op)), /* A <- DHCP op */
70 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, BOOTREPLY, 1, 0), /* op == BOOTREPLY ? */
71 BPF_STMT(BPF_RET + BPF_K, 0), /* ignore */
72 BPF_STMT(BPF_LD + BPF_B + BPF_ABS, offsetof(DHCPPacket, dhcp.htype)), /* A <- DHCP header type */
73 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, arp_type, 1, 0), /* header type == arp_type ? */
74 BPF_STMT(BPF_RET + BPF_K, 0), /* ignore */
75 BPF_STMT(BPF_LD + BPF_W + BPF_ABS, offsetof(DHCPPacket, dhcp.xid)), /* A <- client identifier */
76 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, xid, 1, 0), /* client identifier == xid ? */
77 BPF_STMT(BPF_RET + BPF_K, 0), /* ignore */
78 BPF_STMT(BPF_LD + BPF_B + BPF_ABS, offsetof(DHCPPacket, dhcp.hlen)), /* A <- MAC address length */
79 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, (uint8_t) hw_addr->length, 1, 0), /* address length == hw_addr->length ? */
80 BPF_STMT(BPF_RET + BPF_K, 0), /* ignore */
81
82 /* We only support MAC address length to be either 0 or 6 (ETH_ALEN). Optionally
83 * compare chaddr for ETH_ALEN bytes. */
84 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, ETH_ALEN, 0, 8), /* A (the MAC address length) == ETH_ALEN ? */
85 BPF_STMT(BPF_LDX + BPF_IMM, unaligned_read_be32(hw_addr->bytes)), /* X <- 4 bytes of client's MAC */
86 BPF_STMT(BPF_LD + BPF_W + BPF_ABS, offsetof(DHCPPacket, dhcp.chaddr)), /* A <- 4 bytes of MAC from dhcp.chaddr */
87 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_X, 0, 1, 0), /* A == X ? */
88 BPF_STMT(BPF_RET + BPF_K, 0), /* ignore */
89 BPF_STMT(BPF_LDX + BPF_IMM, unaligned_read_be16(hw_addr->bytes + 4)), /* X <- remainder of client's MAC */
90 BPF_STMT(BPF_LD + BPF_H + BPF_ABS, offsetof(DHCPPacket, dhcp.chaddr) + 4), /* A <- remainder of MAC from dhcp.chaddr */
91 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_X, 0, 1, 0), /* A == X ? */
92 BPF_STMT(BPF_RET + BPF_K, 0), /* ignore */
93
94 BPF_STMT(BPF_LD + BPF_W + BPF_ABS, offsetof(DHCPPacket, dhcp.magic)), /* A <- DHCP magic cookie */
95 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, DHCP_MAGIC_COOKIE, 1, 0), /* cookie == DHCP magic cookie ? */
96 BPF_STMT(BPF_RET + BPF_K, 0), /* ignore */
97 BPF_STMT(BPF_RET + BPF_K, UINT32_MAX), /* accept */
98 };
99 struct sock_fprog fprog = {
100 .len = ELEMENTSOF(filter),
101 .filter = filter
102 };
103 _cleanup_close_ int s = -EBADF;
104 int r;
105
106 s = socket(AF_PACKET, SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0);
107 if (s < 0)
108 return -errno;
109
110 r = setsockopt_int(s, SOL_PACKET, PACKET_AUXDATA, true);
111 if (r < 0)
112 return r;
113
114 r = setsockopt(s, SOL_SOCKET, SO_ATTACH_FILTER, &fprog, sizeof(fprog));
115 if (r < 0)
116 return -errno;
117
118 if (so_priority_set) {
119 r = setsockopt_int(s, SOL_SOCKET, SO_PRIORITY, so_priority);
120 if (r < 0)
121 return r;
122 }
123
124 link->ll = (struct sockaddr_ll) {
125 .sll_family = AF_PACKET,
126 .sll_protocol = htobe16(ETH_P_IP),
127 .sll_ifindex = ifindex,
128 .sll_hatype = htobe16(arp_type),
129 .sll_halen = bcast_addr->length,
130 };
131 /* We may overflow link->ll. link->ll_buffer ensures we have enough space. */
132 memcpy(link->ll.sll_addr, bcast_addr->bytes, bcast_addr->length);
133
134 r = bind(s, &link->sa, SOCKADDR_LL_LEN(link->ll));
135 if (r < 0)
136 return -errno;
137
138 return TAKE_FD(s);
139 }
140
141 int dhcp_network_bind_raw_socket(
142 int ifindex,
143 union sockaddr_union *link,
144 uint32_t xid,
145 const struct hw_addr_data *hw_addr,
146 const struct hw_addr_data *bcast_addr,
147 uint16_t arp_type,
148 uint16_t port,
149 bool so_priority_set,
150 int so_priority) {
151
152 static struct hw_addr_data default_eth_bcast = {
153 .length = ETH_ALEN,
154 .ether = {{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }},
155 }, default_ib_bcast = {
156 .length = INFINIBAND_ALEN,
157 .infiniband = {
158 0x00, 0xff, 0xff, 0xff, 0xff, 0x12, 0x40, 0x1b,
159 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
160 0xff, 0xff, 0xff, 0xff
161 },
162 };
163
164 assert(ifindex > 0);
165 assert(link);
166 assert(hw_addr);
167
168 switch (arp_type) {
169 case ARPHRD_ETHER:
170 return _bind_raw_socket(ifindex, link, xid,
171 hw_addr,
172 (bcast_addr && !hw_addr_is_null(bcast_addr)) ? bcast_addr : &default_eth_bcast,
173 arp_type, port, so_priority_set, so_priority);
174
175 case ARPHRD_INFINIBAND:
176 return _bind_raw_socket(ifindex, link, xid,
177 &HW_ADDR_NULL,
178 (bcast_addr && !hw_addr_is_null(bcast_addr)) ? bcast_addr : &default_ib_bcast,
179 arp_type, port, so_priority_set, so_priority);
180 default:
181 return -EINVAL;
182 }
183 }
184
185 int dhcp_network_bind_udp_socket(int ifindex, be32_t address, uint16_t port, int ip_service_type) {
186 union sockaddr_union src = {
187 .in.sin_family = AF_INET,
188 .in.sin_port = htobe16(port),
189 .in.sin_addr.s_addr = address,
190 };
191 _cleanup_close_ int s = -EBADF;
192 int r;
193
194 s = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0);
195 if (s < 0)
196 return -errno;
197
198 if (ip_service_type >= 0)
199 r = setsockopt_int(s, IPPROTO_IP, IP_TOS, ip_service_type);
200 else
201 r = setsockopt_int(s, IPPROTO_IP, IP_TOS, IPTOS_CLASS_CS6);
202 if (r < 0)
203 return r;
204
205 r = setsockopt_int(s, SOL_SOCKET, SO_REUSEADDR, true);
206 if (r < 0)
207 return r;
208
209 if (ifindex > 0) {
210 r = socket_bind_to_ifindex(s, ifindex);
211 if (r < 0)
212 return r;
213 }
214
215 if (port == DHCP_PORT_SERVER) {
216 r = setsockopt_int(s, SOL_SOCKET, SO_BROADCAST, true);
217 if (r < 0)
218 return r;
219 if (address == INADDR_ANY) {
220 /* IP_PKTINFO filter should not be applied when packets are
221 allowed to enter/leave through the interface other than
222 DHCP server sits on(BindToInterface option). */
223 r = setsockopt_int(s, IPPROTO_IP, IP_PKTINFO, true);
224 if (r < 0)
225 return r;
226 }
227 } else {
228 r = setsockopt_int(s, IPPROTO_IP, IP_FREEBIND, true);
229 if (r < 0)
230 return r;
231 }
232
233 if (bind(s, &src.sa, sizeof(src.in)) < 0)
234 return -errno;
235
236 return TAKE_FD(s);
237 }
238
239 int dhcp_network_send_raw_socket(
240 int s,
241 const union sockaddr_union *link,
242 const void *packet,
243 size_t len) {
244
245 /* Do not add assert(s >= 0) here, as this is called in fuzz-dhcp-server, and in that case this
246 * function should fail with negative errno. */
247
248 assert(link);
249 assert(packet);
250 assert(len > 0);
251
252 if (sendto(s, packet, len, 0, &link->sa, SOCKADDR_LL_LEN(link->ll)) < 0)
253 return -errno;
254
255 return 0;
256 }
257
258 int dhcp_network_send_udp_socket(
259 int s,
260 be32_t address,
261 uint16_t port,
262 const void *packet,
263 size_t len) {
264
265 union sockaddr_union dest = {
266 .in.sin_family = AF_INET,
267 .in.sin_port = htobe16(port),
268 .in.sin_addr.s_addr = address,
269 };
270
271 assert(s >= 0);
272 assert(packet);
273 assert(len > 0);
274
275 if (sendto(s, packet, len, 0, &dest.sa, sizeof(dest.in)) < 0)
276 return -errno;
277
278 return 0;
279 }