Debian Codegraph @

src/libsystemd-network/test-dhcp-server.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 /***
3 Copyright © 2013 Intel Corporation. All rights reserved.
4 ***/
5
6 #include <net/if_arp.h>
7
8 #include "sd-dhcp-server.h"
9 #include "sd-event.h"
10
11 #include "dhcp-server-internal.h"
12 #include "dhcp-server-request.h"
13 #include "fd-util.h"
14 #include "iovec-util.h"
15 #include "iovec-wrapper.h"
16 #include "tests.h"
17
18 TEST(basic) {
19 struct in_addr address_lo = {
20 .s_addr = htobe32(INADDR_LOOPBACK),
21 };
22 struct in_addr address_any = {
23 .s_addr = htobe32(INADDR_ANY),
24 };
25
26 _cleanup_(sd_event_unrefp) sd_event *event = NULL;
27 ASSERT_OK(sd_event_new(&event));
28
29 _cleanup_(sd_dhcp_server_unrefp) sd_dhcp_server *server = NULL;
30 ASSERT_OK(sd_dhcp_server_new(&server, 4242));
31 ASSERT_NOT_NULL(server);
32
33 ASSERT_OK(sd_dhcp_server_attach_event(server, event, SD_EVENT_PRIORITY_NORMAL));
34 ASSERT_RETURN_EXPECTED(ASSERT_ERROR(sd_dhcp_server_attach_event(server, event, SD_EVENT_PRIORITY_NORMAL), EBUSY));
35 ASSERT_PTR_EQ(sd_dhcp_server_get_event(server), event);
36 ASSERT_OK(sd_dhcp_server_detach_event(server));
37 ASSERT_NULL(sd_dhcp_server_get_event(server));
38 ASSERT_OK(sd_dhcp_server_attach_event(server, NULL, SD_EVENT_PRIORITY_NORMAL));
39 ASSERT_RETURN_EXPECTED(ASSERT_ERROR(sd_dhcp_server_attach_event(server, NULL, SD_EVENT_PRIORITY_NORMAL), EBUSY));
40
41 ASSERT_TRUE(sd_dhcp_server_ref(server) == server);
42 ASSERT_NULL(sd_dhcp_server_unref(server));
43
44 ASSERT_RETURN_EXPECTED(ASSERT_ERROR(sd_dhcp_server_start(server), EUNATCH));
45
46 ASSERT_RETURN_EXPECTED(ASSERT_ERROR(sd_dhcp_server_configure_pool(server, &address_any, 28, 0, 0), EINVAL));
47 ASSERT_RETURN_EXPECTED(ASSERT_ERROR(sd_dhcp_server_configure_pool(server, &address_lo, 38, 0, 0), ERANGE));
48 ASSERT_OK(sd_dhcp_server_configure_pool(server, &address_lo, 8, 0, 0));
49 ASSERT_OK(sd_dhcp_server_configure_pool(server, &address_lo, 8, 0, 0));
50 ASSERT_RETURN_EXPECTED(ASSERT_ERROR(sd_dhcp_server_configure_pool(server, &address_any, 8, 0, 1), EINVAL));
51 ASSERT_OK(sd_dhcp_server_configure_pool(server, &address_lo, 8, 0, 1));
52
53 _cleanup_close_pair_ int socket_fd[2] = EBADF_PAIR;
54 ASSERT_OK_ERRNO(socketpair(AF_UNIX, SOCK_SEQPACKET | SOCK_CLOEXEC | SOCK_NONBLOCK, 0, socket_fd));
55
56 server->socket_fd = TAKE_FD(socket_fd[0]);
57
58 ASSERT_OK(sd_dhcp_server_start(server));
59 ASSERT_OK(sd_dhcp_server_start(server));
60 ASSERT_OK(sd_dhcp_server_stop(server));
61 ASSERT_OK(sd_dhcp_server_stop(server));
62 ASSERT_OK(sd_dhcp_server_start(server));
63 }
64
65 static int process_one(sd_dhcp_server *server, sd_dhcp_message *message) {
66 _cleanup_(iovw_done_free) struct iovec_wrapper iovw = {};
67 ASSERT_OK(dhcp_message_build(message, &iovw));
68
69 _cleanup_(iovec_done) struct iovec iov = {};
70 ASSERT_OK(iovw_concat(&iovw, &iov));
71 return dhcp_server_process_message(server, &iov, /* mh= */ NULL);
72 }
73
74 TEST(dhcp_server_process_message) {
75 static const struct hw_addr_data hw_addr = {
76 .length = ETH_ALEN,
77 .ether = {{ 'A', 'B', 'C', 'D', 'E', 'F' }},
78 };
79
80 _cleanup_(sd_dhcp_message_unrefp) sd_dhcp_message *m = NULL;
81 ASSERT_OK(dhcp_message_new(&m));
82 ASSERT_OK(dhcp_message_init_header(
83 m,
84 BOOTREQUEST,
85 0x12345678,
86 ARPHRD_ETHER,
87 &hw_addr));
88
89 ASSERT_OK(dhcp_message_append_option_hostname(m, /* flags= */ 0, /* is_client= */ false, "TESTHN"));
90
91 struct in_addr address_lo = {
92 .s_addr = htobe32(INADDR_LOOPBACK),
93 };
94 struct in_addr static_lease_address = {
95 .s_addr = htobe32(INADDR_LOOPBACK + 42),
96 };
97 static uint8_t static_lease_client_id[7] = {0x01, 'A', 'B', 'C', 'D', 'E', 'G' };
98
99 _cleanup_close_pair_ int socket_fd[2] = EBADF_PAIR;
100 ASSERT_OK_ERRNO(socketpair(AF_UNIX, SOCK_SEQPACKET | SOCK_CLOEXEC | SOCK_NONBLOCK, 0, socket_fd));
101
102 _cleanup_(sd_event_unrefp) sd_event *event = NULL;
103 ASSERT_OK(sd_event_new(&event));
104
105 _cleanup_(sd_dhcp_server_unrefp) sd_dhcp_server *server = NULL;
106 ASSERT_OK(sd_dhcp_server_new(&server, 4242));
107 ASSERT_OK(sd_dhcp_server_configure_pool(server, &address_lo, 8, 0, 0));
108 ASSERT_OK(sd_dhcp_server_set_static_lease(
109 server,
110 &static_lease_address,
111 static_lease_client_id,
112 ELEMENTSOF(static_lease_client_id),
113 /* hostname= */ NULL));
114 ASSERT_OK(sd_dhcp_server_attach_event(server, event, SD_EVENT_PRIORITY_NORMAL));
115 server->socket_fd = TAKE_FD(socket_fd[0]);
116 ASSERT_OK(sd_dhcp_server_start(server));
117
118 ASSERT_OK(dhcp_message_append_option_u8(m, SD_DHCP_OPTION_MESSAGE_TYPE, DHCP_DISCOVER));
119 ASSERT_OK_EQ(process_one(server, m), DHCP_OFFER);
120
121 /* Missing Message Type option */
122 dhcp_message_remove_option(m, SD_DHCP_OPTION_MESSAGE_TYPE);
123 ASSERT_ERROR(process_one(server, m), ENODATA);
124 ASSERT_OK(dhcp_message_append_option_u8(m, SD_DHCP_OPTION_MESSAGE_TYPE, DHCP_DISCOVER));
125
126 /* Invalid op */
127 m->header.op = 0;
128 ASSERT_ERROR(process_one(server, m), EBADMSG);
129 m->header.op = BOOTREQUEST;
130
131 /* Invalid htype */
132 m->header.htype = 0;
133 ASSERT_ERROR(process_one(server, m), EBADMSG);
134 m->header.htype = ARPHRD_ETHER;
135
136 /* Invalid hlen */
137 m->header.hlen = 0;
138 ASSERT_ERROR(process_one(server, m), EBADMSG);
139 m->header.hlen = ETHER_ADDR_LEN;
140
141 /* DHCPREQUEST (init-reboot) without Requested IP Address option */
142 dhcp_message_remove_option(m, SD_DHCP_OPTION_MESSAGE_TYPE);
143 ASSERT_OK(dhcp_message_append_option_u8(m, SD_DHCP_OPTION_MESSAGE_TYPE, DHCP_REQUEST));
144 ASSERT_ERROR(process_one(server, m), ENODATA);
145
146 /* DHCPREQUEST (init-reboot) with an invalid Requested IP Address option */
147 ASSERT_OK(dhcp_message_append_option_be32(m, SD_DHCP_OPTION_REQUESTED_IP_ADDRESS, htobe32(0x12345678)));
148 ASSERT_OK_EQ(process_one(server, m), DHCP_NAK);
149
150 /* DHCPREQUEST (selecting) with Requested IP address */
151 ASSERT_OK(dhcp_message_append_option_be32(m, SD_DHCP_OPTION_SERVER_IDENTIFIER, htobe32(INADDR_LOOPBACK)));
152 dhcp_message_remove_option(m, SD_DHCP_OPTION_REQUESTED_IP_ADDRESS);
153 ASSERT_OK(dhcp_message_append_option_be32(m, SD_DHCP_OPTION_REQUESTED_IP_ADDRESS, htobe32(INADDR_LOOPBACK + 3)));
154 ASSERT_OK_EQ(process_one(server, m), DHCP_ACK);
155
156 /* DHCPREQUEST (selecting) with unmatching server address (silently ignored). */
157 dhcp_message_remove_option(m, SD_DHCP_OPTION_SERVER_IDENTIFIER);
158 ASSERT_OK(dhcp_message_append_option_be32(m, SD_DHCP_OPTION_SERVER_IDENTIFIER, htobe32(0x12345678)));
159 ASSERT_OK_ZERO(process_one(server, m));
160 dhcp_message_remove_option(m, SD_DHCP_OPTION_SERVER_IDENTIFIER);
161 ASSERT_OK(dhcp_message_append_option_be32(m, SD_DHCP_OPTION_SERVER_IDENTIFIER, htobe32(INADDR_LOOPBACK)));
162
163 /* DHCPREQUEST (selecting) with another address */
164 dhcp_message_remove_option(m, SD_DHCP_OPTION_REQUESTED_IP_ADDRESS);
165 ASSERT_OK(dhcp_message_append_option_be32(m, SD_DHCP_OPTION_REQUESTED_IP_ADDRESS, htobe32(INADDR_LOOPBACK + 4)));
166 ASSERT_OK_EQ(process_one(server, m), DHCP_ACK);
167
168 /* Request the previous address again. */
169 dhcp_message_remove_option(m, SD_DHCP_OPTION_REQUESTED_IP_ADDRESS);
170 ASSERT_OK(dhcp_message_append_option_be32(m, SD_DHCP_OPTION_REQUESTED_IP_ADDRESS, htobe32(INADDR_LOOPBACK + 3)));
171 ASSERT_OK_EQ(process_one(server, m), DHCP_ACK);
172
173 /* With client ID */
174 struct sd_dhcp_client_id client_id = {
175 .size = 7,
176 .id.type = 1,
177 .id.eth.haddr = { 'A', 'B', 'C', 'D', 'E', 'F' },
178 };
179 ASSERT_OK(dhcp_message_append_option_client_id(m, &client_id));
180 ASSERT_OK_EQ(process_one(server, m), DHCP_ACK);
181
182 /* Request a different address with client ID */
183 dhcp_message_remove_option(m, SD_DHCP_OPTION_REQUESTED_IP_ADDRESS);
184 ASSERT_OK(dhcp_message_append_option_be32(m, SD_DHCP_OPTION_REQUESTED_IP_ADDRESS, htobe32(INADDR_LOOPBACK + 30)));
185 ASSERT_OK_EQ(process_one(server, m), DHCP_ACK);
186
187 /* add the static lease for the client ID */
188 ASSERT_OK(sd_dhcp_server_stop(server));
189 ASSERT_OK(sd_dhcp_server_set_static_lease(
190 server,
191 &(struct in_addr) { .s_addr = htobe32(INADDR_LOOPBACK + 31) },
192 (uint8_t[7]) { 0x01, 'A', 'B', 'C', 'D', 'E', 'F' },
193 7,
194 /* hostname= */ NULL));
195 ASSERT_OK(sd_dhcp_server_start(server));
196
197 /* discover */
198 dhcp_message_remove_option(m, SD_DHCP_OPTION_MESSAGE_TYPE);
199 ASSERT_OK(dhcp_message_append_option_u8(m, SD_DHCP_OPTION_MESSAGE_TYPE, DHCP_DISCOVER));
200 ASSERT_OK_EQ(process_one(server, m), DHCP_OFFER);
201
202 /* request neither bound nor static address */
203 dhcp_message_remove_option(m, SD_DHCP_OPTION_MESSAGE_TYPE);
204 ASSERT_OK(dhcp_message_append_option_u8(m, SD_DHCP_OPTION_MESSAGE_TYPE, DHCP_REQUEST));
205 dhcp_message_remove_option(m, SD_DHCP_OPTION_REQUESTED_IP_ADDRESS);
206 ASSERT_OK(dhcp_message_append_option_be32(m, SD_DHCP_OPTION_REQUESTED_IP_ADDRESS, htobe32(INADDR_LOOPBACK + 29)));
207 ASSERT_OK_EQ(process_one(server, m), DHCP_NAK);
208
209 /* request the currently assigned address */
210 dhcp_message_remove_option(m, SD_DHCP_OPTION_REQUESTED_IP_ADDRESS);
211 ASSERT_OK(dhcp_message_append_option_be32(m, SD_DHCP_OPTION_REQUESTED_IP_ADDRESS, htobe32(INADDR_LOOPBACK + 30)));
212 ASSERT_OK_EQ(process_one(server, m), DHCP_NAK);
213
214 /* request the new static address */
215 dhcp_message_remove_option(m, SD_DHCP_OPTION_REQUESTED_IP_ADDRESS);
216 ASSERT_OK(dhcp_message_append_option_be32(m, SD_DHCP_OPTION_REQUESTED_IP_ADDRESS, htobe32(INADDR_LOOPBACK + 31)));
217 ASSERT_OK_EQ(process_one(server, m), DHCP_ACK);
218
219 /* release the bound static lease */
220 m->header.ciaddr = htobe32(INADDR_LOOPBACK + 31);
221 dhcp_message_remove_option(m, SD_DHCP_OPTION_MESSAGE_TYPE);
222 ASSERT_OK(dhcp_message_append_option_u8(m, SD_DHCP_OPTION_MESSAGE_TYPE, DHCP_RELEASE));
223 ASSERT_OK_ZERO(process_one(server, m));
224 m->header.ciaddr = 0;
225 dhcp_message_remove_option(m, SD_DHCP_OPTION_MESSAGE_TYPE);
226 ASSERT_OK(dhcp_message_append_option_u8(m, SD_DHCP_OPTION_MESSAGE_TYPE, DHCP_REQUEST));
227
228 /* drop the static lease for the client ID */
229 ASSERT_OK(sd_dhcp_server_stop(server));
230 ASSERT_OK(sd_dhcp_server_set_static_lease(
231 server,
232 /* address= */ NULL,
233 (uint8_t[7]) { 0x01, 'A', 'B', 'C', 'D', 'E', 'F' },
234 7,
235 /* hostname= */ NULL));
236 ASSERT_OK(sd_dhcp_server_start(server));
237
238 /* request a new non-static address */
239 dhcp_message_remove_option(m, SD_DHCP_OPTION_REQUESTED_IP_ADDRESS);
240 ASSERT_OK(dhcp_message_append_option_be32(m, SD_DHCP_OPTION_REQUESTED_IP_ADDRESS, htobe32(INADDR_LOOPBACK + 29)));
241 ASSERT_OK_EQ(process_one(server, m), DHCP_ACK);
242
243 /* request address reserved for static lease (unmatching client ID) */
244 client_id.id.eth.haddr[5] = 'H';
245 dhcp_message_remove_option(m, SD_DHCP_OPTION_CLIENT_IDENTIFIER);
246 ASSERT_OK(dhcp_message_append_option_client_id(m, &client_id));
247 dhcp_message_remove_option(m, SD_DHCP_OPTION_REQUESTED_IP_ADDRESS);
248 ASSERT_OK(dhcp_message_append_option_be32(m, SD_DHCP_OPTION_REQUESTED_IP_ADDRESS, htobe32(INADDR_LOOPBACK + 42)));
249 ASSERT_OK_EQ(process_one(server, m), DHCP_NAK);
250
251 /* request unmatching address */
252 client_id.id.eth.haddr[5] = 'G';
253 dhcp_message_remove_option(m, SD_DHCP_OPTION_CLIENT_IDENTIFIER);
254 ASSERT_OK(dhcp_message_append_option_client_id(m, &client_id));
255 dhcp_message_remove_option(m, SD_DHCP_OPTION_REQUESTED_IP_ADDRESS);
256 ASSERT_OK(dhcp_message_append_option_be32(m, SD_DHCP_OPTION_REQUESTED_IP_ADDRESS, htobe32(INADDR_LOOPBACK + 41)));
257 ASSERT_OK_EQ(process_one(server, m), DHCP_NAK);
258
259 /* request matching address */
260 dhcp_message_remove_option(m, SD_DHCP_OPTION_REQUESTED_IP_ADDRESS);
261 ASSERT_OK(dhcp_message_append_option_be32(m, SD_DHCP_OPTION_REQUESTED_IP_ADDRESS, htobe32(INADDR_LOOPBACK + 42)));
262 ASSERT_OK_EQ(process_one(server, m), DHCP_ACK);
263
264 /* try again */
265 dhcp_message_remove_option(m, SD_DHCP_OPTION_REQUESTED_IP_ADDRESS);
266 ASSERT_OK(dhcp_message_append_option_be32(m, SD_DHCP_OPTION_REQUESTED_IP_ADDRESS, htobe32(INADDR_LOOPBACK + 42)));
267 ASSERT_OK_EQ(process_one(server, m), DHCP_ACK);
268 }
269
270 TEST(sd_dhcp_server_set_static_lease) {
271 _cleanup_(sd_dhcp_server_unrefp) sd_dhcp_server *server = NULL;
272 ASSERT_OK(sd_dhcp_server_new(&server, 4242));
273
274 ASSERT_OK(sd_dhcp_server_set_static_lease(
275 server,
276 &(struct in_addr) { .s_addr = 0x01020304 },
277 (uint8_t *) &(uint32_t) { 0x01020304 },
278 sizeof(uint32_t),
279 /* hostname= */ NULL));
280 /* Duplicated entry. */
281 ASSERT_ERROR(sd_dhcp_server_set_static_lease(
282 server,
283 &(struct in_addr) { .s_addr = 0x01020304 },
284 (uint8_t *) &(uint32_t) { 0x01020304 },
285 sizeof(uint32_t),
286 /* hostname= */ NULL),
287 EEXIST);
288 /* Address is conflicted. */
289 ASSERT_ERROR(sd_dhcp_server_set_static_lease(
290 server,
291 &(struct in_addr) { .s_addr = 0x01020304 },
292 (uint8_t *) &(uint32_t) { 0x01020305 },
293 sizeof(uint32_t),
294 /* hostname= */ NULL),
295 EEXIST);
296 /* Client ID is conflicted. */
297 ASSERT_ERROR(sd_dhcp_server_set_static_lease(
298 server,
299 &(struct in_addr) { .s_addr = 0x01020305 },
300 (uint8_t *) &(uint32_t) { 0x01020304 },
301 sizeof(uint32_t),
302 /* hostname= */ NULL),
303 EEXIST);
304
305 ASSERT_OK(sd_dhcp_server_set_static_lease(
306 server,
307 &(struct in_addr) { .s_addr = 0x01020305 },
308 (uint8_t *) &(uint32_t) { 0x01020305 },
309 sizeof(uint32_t),
310 /* hostname= */ NULL));
311 /* Remove the previous entry. */
312 ASSERT_OK(sd_dhcp_server_set_static_lease(
313 server,
314 &(struct in_addr) { .s_addr = 0x00000000 },
315 (uint8_t *) &(uint32_t) { 0x01020305 },
316 sizeof(uint32_t),
317 /* hostname= */ NULL));
318 /* Then, set a different address. */
319 ASSERT_OK(sd_dhcp_server_set_static_lease(
320 server,
321 &(struct in_addr) { .s_addr = 0x01020306 },
322 (uint8_t *) &(uint32_t) { 0x01020305 },
323 sizeof(uint32_t),
324 /* hostname= */ NULL));
325 /* Remove again. */
326 ASSERT_OK(sd_dhcp_server_set_static_lease(
327 server,
328 &(struct in_addr) { .s_addr = 0x00000000 },
329 (uint8_t *) &(uint32_t) { 0x01020305 },
330 sizeof(uint32_t),
331 /* hostname= */ NULL));
332 /* Try to remove non-existent entry. */
333 ASSERT_OK(sd_dhcp_server_set_static_lease(
334 server,
335 &(struct in_addr) { .s_addr = 0x00000000 },
336 (uint8_t *) &(uint32_t) { 0x01020305 },
337 sizeof(uint32_t),
338 /* hostname= */ NULL));
339 /* Try to remove non-existent entry. */
340 ASSERT_OK(sd_dhcp_server_set_static_lease(
341 server,
342 &(struct in_addr) { .s_addr = 0x00000000 },
343 (uint8_t *) &(uint32_t) { 0x01020306 },
344 sizeof(uint32_t),
345 /* hostname= */ NULL));
346 }
347
348 TEST(sd_dhcp_server_set_domain_name) {
349 _cleanup_(sd_dhcp_server_unrefp) sd_dhcp_server *server = NULL;
350 ASSERT_OK(sd_dhcp_server_new(&server, 4242));
351
352 /* Test setting domain name */
353 ASSERT_OK_POSITIVE(sd_dhcp_server_set_domain_name(server, "example.com"));
354
355 /* Test setting same domain name (should return 0 - no change) */
356 ASSERT_OK_ZERO(sd_dhcp_server_set_domain_name(server, "example.com"));
357
358 /* Test changing domain name */
359 ASSERT_OK_POSITIVE(sd_dhcp_server_set_domain_name(server, "test.local"));
360
361 /* Test clearing domain name */
362 ASSERT_OK_POSITIVE(sd_dhcp_server_set_domain_name(server, NULL));
363
364 /* Test clearing again (should return 0 - already cleared) */
365 ASSERT_OK_ZERO(sd_dhcp_server_set_domain_name(server, NULL));
366
367 /* Test invalid domain name */
368 ASSERT_ERROR(sd_dhcp_server_set_domain_name(server, "invalid..domain"), EINVAL);
369
370 /* Test empty string (treated differently from NULL) */
371 ASSERT_OK_POSITIVE(sd_dhcp_server_set_domain_name(server, ""));
372
373 /* Test clearing domain name with NULL */
374 ASSERT_OK_POSITIVE(sd_dhcp_server_set_domain_name(server, NULL));
375
376 /* Test valid domain with subdomain */
377 ASSERT_OK_POSITIVE(sd_dhcp_server_set_domain_name(server, "sub.example.com"));
378
379 /* Test single-label domain */
380 ASSERT_OK_POSITIVE(sd_dhcp_server_set_domain_name(server, "local"));
381 }
382
383 DEFINE_TEST_MAIN(LOG_DEBUG);