Debian Codegraph @

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

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <net/if_arp.h>
4
5 #include "sd-event.h"
6
7 #include "alloc-util.h"
8 #include "dhcp-message.h"
9 #include "dhcp-server-internal.h"
10 #include "dhcp-server-lease-internal.h"
11 #include "dhcp-server-request.h"
12 #include "dhcp-server-send.h"
13 #include "errno-util.h"
14 #include "fd-util.h"
15 #include "hashmap.h"
16 #include "iovec-util.h"
17 #include "ip-util.h"
18 #include "set.h"
19 #include "siphash24.h"
20 #include "socket-util.h"
21 #include "string-util.h"
22
23 static DHCPRequest* dhcp_request_free(DHCPRequest *req) {
24 if (!req)
25 return NULL;
26
27 sd_dhcp_message_unref(req->message);
28 set_free(req->parameter_request_list);
29 return mfree(req);
30 }
31
32 DEFINE_TRIVIAL_CLEANUP_FUNC(DHCPRequest*, dhcp_request_free);
33
34 static void dhcp_request_set_timestamp(DHCPRequest *req, const triple_timestamp *timestamp) {
35 assert(req);
36
37 if (timestamp && triple_timestamp_is_set(timestamp))
38 req->timestamp = *timestamp;
39 else
40 triple_timestamp_now(&req->timestamp);
41 }
42
43 int dhcp_request_get_lifetime_timestamp(DHCPRequest *req, clockid_t clock, usec_t *ret) {
44 assert(req);
45 assert(TRIPLE_TIMESTAMP_HAS_CLOCK(clock));
46 assert(clock_supported(clock));
47 assert(ret);
48
49 if (req->lifetime <= 0)
50 return -ENODATA;
51
52 if (!triple_timestamp_is_set(&req->timestamp))
53 return -ENODATA;
54
55 *ret = usec_add(triple_timestamp_by_clock(&req->timestamp, clock), req->lifetime);
56 return 0;
57 }
58
59 static int dhcp_request_set_client_id(DHCPRequest *req) {
60 assert(req);
61 assert(req->message);
62
63 /* Genuine client ID from Client Identifier option. The option may not be set. */
64 (void) dhcp_message_get_option_client_id(req->message, &req->client_id);
65
66 /* Fake client ID generated from the DHCP header.
67 * The client ID type 0 and 255 are special. So do not use if htype is 0 or 255.
68 * Note, Some hardware type (e.g. Infiniband) may not set chaddr field. */
69 if (!IN_SET(req->message->header.htype, 0, UINT8_MAX))
70 (void) sd_dhcp_client_id_set(
71 &req->client_id_by_header,
72 req->message->header.htype,
73 req->message->header.chaddr,
74 req->message->header.hlen);
75
76 /* If Client Identifier option is unspecified, use the generated one. */
77 if (!sd_dhcp_client_id_is_set(&req->client_id))
78 req->client_id = req->client_id_by_header;
79
80 /* We manage bound leases by client ID. Hence, at least one of them is necessary. */
81 if (!sd_dhcp_client_id_is_set(&req->client_id))
82 return -EBADMSG;
83
84 return 0;
85 }
86
87 static int dhcp_request_set_server_identifier(DHCPRequest *req) {
88 int r;
89
90 assert(req);
91 assert(req->message);
92
93 bool mandatory = IN_SET(req->type, DHCP_RELEASE, DHCP_DECLINE);
94
95 be32_t a;
96 r = dhcp_message_get_option_be32(req->message, SD_DHCP_OPTION_SERVER_IDENTIFIER, &a);
97 if (r < 0)
98 return mandatory ? r : 0;
99
100 req->server_address = a;
101 return 0;
102 }
103
104 static int dhcp_request_set_maximum_message_size(DHCPRequest *req) {
105 int r;
106
107 assert(req);
108 assert(req->message);
109
110 uint16_t sz;
111 r = dhcp_message_get_option_u16(req->message, SD_DHCP_OPTION_MAXIMUM_MESSAGE_SIZE, &sz);
112 if (r < 0)
113 return r;
114
115 /* RFC 2132 section 9.10:
116 * The minimum legal value is 576 octets. */
117 if (sz < IPV4_MIN_REASSEMBLY_SIZE)
118 return -EBADMSG;
119
120 req->max_message_size = sz;
121 return 0;
122 }
123
124 static int dhcp_request_set_lifetime(DHCPRequest *req, sd_dhcp_server *server) {
125 assert(req);
126 assert(req->message);
127 assert(server);
128
129 (void) dhcp_message_get_option_sec(
130 req->message,
131 SD_DHCP_OPTION_IP_ADDRESS_LEASE_TIME,
132 /* max_as_infinity= */ true,
133 &req->lifetime);
134
135 /* If unset (or zero is specified...), use the default lease time. */
136 if (req->lifetime <= 0)
137 req->lifetime = MAX(30 * USEC_PER_SEC, server->default_lease_time);
138
139 /* If the requested lifetime is too long, then cap it with the maximum lease time. */
140 if (server->max_lease_time > 0 && req->lifetime > server->max_lease_time)
141 req->lifetime = server->max_lease_time;
142
143 return 0;
144 }
145
146 static int dhcp_server_parse_message(sd_dhcp_server *server, const struct iovec *iov, DHCPRequest **ret) {
147 int r;
148
149 assert(server);
150 assert(iov);
151 assert(ret);
152
153 _cleanup_(sd_dhcp_message_unrefp) sd_dhcp_message *message = NULL;
154 r = dhcp_message_parse(
155 iov,
156 BOOTREQUEST,
157 /* xid= */ NULL,
158 ARPHRD_NONE,
159 /* hw_addr= */ NULL,
160 &message);
161 if (r < 0)
162 return r;
163
164 /* A DHCP relay agent is running on the interface with the same address??
165 * Should be malicious message. */
166 if (message->header.giaddr == server->address)
167 return -EBADMSG;
168
169 _cleanup_(dhcp_request_freep) DHCPRequest *req = new(DHCPRequest, 1);
170 if (!req)
171 return -ENOMEM;
172
173 *req = (DHCPRequest) {
174 .message = sd_dhcp_message_ref(message),
175
176 /* RFC 2132 section 9.10:
177 * The minimum legal value is 576 octets. */
178 .max_message_size = IPV4_MIN_REASSEMBLY_SIZE,
179 };
180
181 /* client hardware address
182 * Note, hlen and chaddr may not be set for non-ethernet interface.
183 * See RFC2131 section 4.1. */
184 r = dhcp_message_get_hw_addr(message, &req->hw_addr);
185 if (r < 0)
186 return r;
187
188 /* Message Type: mandatory */
189 r = dhcp_message_get_option_u8(message, SD_DHCP_OPTION_MESSAGE_TYPE, &req->type);
190 if (r < 0)
191 return r;
192
193 /* Client Identifier: Mandatory. If not set, fall back to use chaddr. */
194 r = dhcp_request_set_client_id(req);
195 if (r < 0)
196 return r;
197
198 /* Server Identifier */
199 r = dhcp_request_set_server_identifier(req);
200 if (r < 0)
201 return r;
202
203 /* Maximum Message Size: optional */
204 (void) dhcp_request_set_maximum_message_size(req);
205
206 /* Lifetime: optional */
207 (void) dhcp_request_set_lifetime(req, server);
208
209 /* Parameter Request List: optional */
210 (void) dhcp_message_get_option_parameter_request_list(message, &req->parameter_request_list);
211
212 *ret = TAKE_PTR(req);
213 return 0;
214 }
215
216 static int dhcp_server_ack(sd_dhcp_server *server, DHCPRequest *req) {
217 int r;
218
219 assert(server);
220 assert(req);
221 assert(req->address != INADDR_ANY);
222
223 r = dhcp_server_set_lease(server, req);
224 if (r < 0)
225 return r;
226
227 r = dhcp_server_send_reply(server, req, DHCP_ACK);
228 if (r < 0)
229 return r;
230
231 dhcp_server_on_lease_change(server);
232 return r;
233 }
234
235 static int dhcp_server_process_discover(sd_dhcp_server *server, DHCPRequest *req) {
236 assert(server);
237 assert(req);
238
239 sd_dhcp_server_lease
240 *existing_lease = hashmap_get(server->bound_leases_by_client_id, &req->client_id),
241 *static_lease = dhcp_server_get_static_lease(server, req);
242
243 log_dhcp_server(server, "DISCOVER (0x%x)", be32toh(req->message->header.xid));
244
245 /* for now pick a random free address from the pool */
246 if (static_lease) {
247 sd_dhcp_server_lease *l = hashmap_get(server->bound_leases_by_address, UINT32_TO_PTR(static_lease->address));
248 if (l && l != existing_lease)
249 /* The address is already assigned to another host. Refusing. */
250 return -EADDRINUSE;
251
252 /* Found a matching static lease. */
253 req->static_lease = static_lease;
254 req->address = static_lease->address;
255
256 } else if (existing_lease && dhcp_server_address_is_in_pool(server, existing_lease->address))
257 /* If we previously assigned an address to the host, then reuse it. */
258 req->address = existing_lease->address;
259
260 else {
261 struct siphash state;
262 uint64_t hash;
263
264 /* Even with no persistence of leases, we try to offer the same client the same IP address.
265 * We do this by using the hash of the client ID as the offset into the pool of leases when
266 * finding the next free one. */
267
268 #define HASH_KEY SD_ID128_MAKE(0d,1d,fe,bd,f1,24,bd,b3,47,f1,dd,6e,73,21,93,30)
269
270 siphash24_init(&state, HASH_KEY.bytes);
271 client_id_hash_func(&req->client_id, &state);
272 hash = htole64(siphash24_finalize(&state));
273
274 for (unsigned i = 0; i < server->pool_size; i++) {
275 be32_t a = server->subnet | htobe32(server->pool_offset + (hash + i) % server->pool_size);
276 if (dhcp_server_address_available(server, a)) {
277 req->address = a;
278 break;
279 }
280 }
281 }
282
283 if (req->address == INADDR_ANY)
284 return -EADDRNOTAVAIL; /* no free addresses left */
285
286 if (server->rapid_commit &&
287 dhcp_message_get_option_flag(req->message, SD_DHCP_OPTION_RAPID_COMMIT) >= 0)
288 return dhcp_server_ack(server, req);
289
290 return dhcp_server_send_reply(server, req, DHCP_OFFER);
291 }
292
293 static int server_guess_client_state(sd_dhcp_server *server, DHCPRequest *req) {
294 int r;
295
296 assert(server);
297 assert(req);
298 assert(req->message);
299
300 /* This guesses the client is in the renewing or rebinding state.
301 *
302 * If giaddr is zero, then there is no relay agent between the client and us. In that case, a
303 * DHCPREQUEST sent to a unicast destination address implies the client is in RENEWING state, while a
304 * broadcast destination address implies REBINDING state. Note, if packet destination information is
305 * unavailable, we conservatively assume REBINDING.
306 *
307 * If giaddr is non-zero, then there is a relay agent. The relay forwards packets to us using unicast
308 * transport, so the outer IPv4 destination address no longer reflects whether the original client
309 * message was broadcast or unicast. If the message contains the Relay Agent Information option with
310 * Relay Agent Flags suboption, then we can know if the original message is unicast or broadcast. If
311 * the message does not have the option, then we assume the client is in the rebinding state. */
312
313 if (req->message->header.giaddr == INADDR_ANY) {
314 if (!req->pktinfo)
315 return DHCP_STATE_REBINDING;
316
317 /* 255.255.255.255 ? */
318 if (req->pktinfo->ipi_addr.s_addr == INADDR_BROADCAST)
319 return DHCP_STATE_REBINDING;
320
321 /* subnet-directed broadcast, e.g., 192.0.2.0/24 -> 192.0.2.255 ? */
322 if (req->pktinfo->ipi_addr.s_addr == (server->subnet | ~server->netmask))
323 return DHCP_STATE_REBINDING;
324
325 /* unicast, hence renewing. */
326 return DHCP_STATE_RENEWING;
327 }
328
329 _cleanup_(tlv_unrefp) TLV *agent_info = NULL;
330 r = dhcp_message_get_option_sub_tlv(
331 req->message,
332 SD_DHCP_OPTION_RELAY_AGENT_INFORMATION,
333 TLV_DHCP4_SUBOPTION,
334 &agent_info);
335 if (r == -ENODATA)
336 return DHCP_STATE_REBINDING;
337 if (r < 0)
338 return r;
339
340 struct iovec iov = {};
341 r = tlv_get_full(agent_info, SD_DHCP_RELAY_AGENT_FLAGS, sizeof(uint8_t), &iov);
342 if (r == -ENODATA)
343 return DHCP_STATE_REBINDING;
344 if (r < 0)
345 return r;
346
347 assert(iov.iov_len == sizeof(uint8_t));
348 uint8_t flags = *(const uint8_t*) iov.iov_base;
349 return FLAGS_SET(flags, DHCP_RELAY_AGENT_FLAG_UNICAST) ? DHCP_STATE_RENEWING : DHCP_STATE_REBINDING;
350 }
351
352 static int dhcp_server_process_request(sd_dhcp_server *server, DHCPRequest *req) {
353 int r;
354
355 assert(server);
356 assert(req);
357
358 sd_dhcp_server_lease
359 *existing_lease = hashmap_get(server->bound_leases_by_client_id, &req->client_id),
360 *static_lease = dhcp_server_get_static_lease(server, req);
361
362 DHCPState state;
363 be32_t address;
364
365 /* see RFC 2131, section 4.3.2 */
366 if (req->server_address != INADDR_ANY) {
367 state = DHCP_STATE_SELECTING;
368
369 if (req->server_address != server->address)
370 return 0; /* The message is not for us. Let's silently ignore the packet. */
371
372 if (req->message->header.ciaddr != INADDR_ANY) /* this MUST be zero */
373 return -EBADMSG;
374
375 /* this must be filled in with the yiaddr from the chosen OFFER */
376 r = dhcp_message_get_option_be32(req->message, SD_DHCP_OPTION_REQUESTED_IP_ADDRESS, &address);
377 if (r < 0)
378 return r;
379
380 if (address == INADDR_ANY)
381 return -EBADMSG;
382
383 } else if (req->message->header.ciaddr != INADDR_ANY) {
384 r = server_guess_client_state(server, req);
385 if (r < 0)
386 return r;
387 state = r;
388
389 /* this must NOT be filled */
390 if (dhcp_message_get_option_be32(req->message, SD_DHCP_OPTION_REQUESTED_IP_ADDRESS, /* ret= */ NULL) >= 0)
391 return -EBADMSG;
392
393 address = req->message->header.ciaddr;
394
395 } else {
396 state = DHCP_STATE_INIT_REBOOT;
397
398 r = dhcp_message_get_option_be32(req->message, SD_DHCP_OPTION_REQUESTED_IP_ADDRESS, &address);
399 if (r < 0)
400 return r;
401
402 if (address == INADDR_ANY)
403 return -EBADMSG;
404 }
405
406 log_dhcp_server(server, "REQUEST (%s) (0x%x)", dhcp_state_to_string(state), be32toh(req->message->header.xid));
407
408 /* In the below, when we found some inconsistency with the bound lease or static lease, we send
409 * DHCPNAK if the client is in the selecting, renewing, or init-reboot state. Otherwise, i.e.,
410 * if in the rebinding state, we silently ignore the message. This is because, the network may have
411 * multiple DHCP servers, and the address may be managed by another server, and the request may be
412 * for that server.
413 *
414 * On selecting:
415 * We have already verified the message has matching server identifier, hence we have responsibility
416 * to manage the request, hence we should aggressively reply DHCPNAK.
417 *
418 * On renewing:
419 * The message is sent directly to us using unicast transport, so replying with DHCPNAK is unlikely
420 * to interfere with another server.
421 *
422 * On rebinding:
423 * The message is sent using broadcast delivery, hence if the network has multiple DHCP servers, then
424 * another server may reply DHCPACK. We should not disturb their process. Hence, silently ignore the
425 * message.
426 *
427 * On init-reboot:
428 * Even though the message is broadcast, for faster rebooting, we should aggressively reply DHCPNAK.
429 * Even if this is unnecessarily aggressive, the client will soon enter the usual DISCOVER -> REQUEST
430 * cycle, so that should not cause any big issues. */
431 bool send_nak = IN_SET(state, DHCP_STATE_SELECTING, DHCP_STATE_RENEWING, DHCP_STATE_INIT_REBOOT);
432
433 /* Check if the requested address is already assigned to another host.
434 * - if 'l' is NULL, then the address is not assigned to any host.
435 * - if 'l' is non-NULL, and equivalent to 'existing_lease', then the address is assigned to the host.
436 * - if 'l' is non-NULL, but different from 'existing_lease', then the address is already assigned to
437 * another host. In this case, we explicitly know that the address should not be used by the host. */
438 sd_dhcp_server_lease *l = hashmap_get(server->bound_leases_by_address, UINT32_TO_PTR(address));
439 if (l && l != existing_lease)
440 return send_nak ? dhcp_server_send_reply(server, req, DHCP_NAK) : 0;
441
442 /* Check if the request is consistent with the static lease. */
443 if (static_lease) {
444 /* Found a static lease for the client ID. In this case, the server is explicitly configured
445 * to manage the host. Hence, send NAK when the request is invalid. */
446
447 if (static_lease->address != address)
448 /* The client requested an address which is different from the static lease. Refusing. */
449 return send_nak ? dhcp_server_send_reply(server, req, DHCP_NAK) : 0;
450
451 req->static_lease = static_lease;
452 req->address = address;
453
454 return dhcp_server_ack(server, req);
455 }
456
457 if (dhcp_server_address_is_in_pool(server, address)) {
458 /* The requested address is in the pool. In the above, we have checked the address is free or
459 * already assigned to the host. Hence, ACK. */
460 req->address = address;
461
462 return dhcp_server_ack(server, req);
463 }
464
465 /* No static lease is configured for the host, and the requested address is not in our pool. Refusing. */
466 if (send_nak)
467 return dhcp_server_send_reply(server, req, DHCP_NAK);
468
469 return 0;
470 }
471
472 static int dhcp_server_process_decline(sd_dhcp_server *server, DHCPRequest *req) {
473 assert(server);
474 assert(req);
475
476 if (req->server_address != server->address)
477 return 0; /* The message is not for us. Let's silently ignore the packet. */
478
479 /* TODO: make sure we don't offer this address again for a while. */
480
481 _cleanup_free_ char *e = NULL;
482 (void) dhcp_message_get_option_string(req->message, SD_DHCP_OPTION_ERROR_MESSAGE, &e);
483 log_dhcp_server(server, "DECLINE (0x%x): %s", be32toh(req->message->header.xid), strna(e));
484 return 0;
485 }
486
487 static int dhcp_server_process_release(sd_dhcp_server *server, DHCPRequest *req) {
488 assert(server);
489 assert(req);
490
491 if (req->server_address != server->address)
492 return 0; /* The message is not for us. Let's silently ignore the packet. */
493
494 sd_dhcp_server_lease *existing_lease = hashmap_get(server->bound_leases_by_client_id, &req->client_id);
495 if (!existing_lease)
496 return 0;
497
498 if (existing_lease->address != req->message->header.ciaddr)
499 return -EBADMSG;
500
501 sd_dhcp_server_lease_unref(existing_lease);
502 dhcp_server_on_lease_change(server);
503
504 log_dhcp_server(server, "RELEASE (0x%x)", be32toh(req->message->header.xid));
505 return 0;
506 }
507
508 int dhcp_server_process_message(sd_dhcp_server *server, const struct iovec *iov, struct msghdr *mh) {
509 int r;
510
511 assert(server);
512 assert(iov);
513
514 _cleanup_(dhcp_request_freep) DHCPRequest *req = NULL;
515 r = dhcp_server_parse_message(server, iov, &req);
516 if (r < 0)
517 return r;
518
519 dhcp_request_set_timestamp(req, mh ? TRIPLE_TIMESTAMP_FROM_CMSG(mh) : NULL);
520 req->pktinfo = mh ? CMSG_FIND_DATA(mh, IPPROTO_IP, IP_PKTINFO, struct in_pktinfo) : NULL;
521
522 r = dhcp_server_cleanup_expired_leases(server);
523 if (r < 0)
524 return r;
525
526 switch (req->type) {
527 case DHCP_DISCOVER:
528 return dhcp_server_process_discover(server, req);
529 case DHCP_REQUEST:
530 return dhcp_server_process_request(server, req);
531 case DHCP_DECLINE:
532 return dhcp_server_process_decline(server, req);
533 case DHCP_RELEASE:
534 return dhcp_server_process_release(server, req);
535 default:
536 return 0; /* Unsupported DHCP message type? Ignore the message silently. */
537 }
538 }
539
540 static int server_receive_message(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
541 sd_dhcp_server *server = ASSERT_PTR(userdata);
542 int r;
543
544 ssize_t buflen = next_datagram_size_fd(fd);
545 if (ERRNO_IS_NEG_TRANSIENT(buflen) || ERRNO_IS_NEG_DISCONNECT(buflen))
546 return 0;
547 if (buflen < 0) {
548 log_dhcp_server_errno(server, buflen, "Failed to determine datagram size to read, ignoring: %m");
549 return 0;
550 }
551
552 _cleanup_free_ void *buf = malloc0(buflen);
553 if (!buf)
554 return -ENOMEM;
555
556 /* This needs to be initialized with zero. See #20741.
557 * The issue is fixed on glibc-2.35 (8fba672472ae0055387e9315fc2eddfa6775ca79). */
558 CMSG_BUFFER_TYPE(CMSG_SPACE_TIMEVAL +
559 CMSG_SPACE(sizeof(struct in_pktinfo))) control = {};
560 struct iovec iov = IOVEC_MAKE(buf, buflen);
561 struct msghdr msg = {
562 .msg_iov = &iov,
563 .msg_iovlen = 1,
564 .msg_control = &control,
565 .msg_controllen = sizeof(control),
566 };
567
568 ssize_t len = recvmsg_safe(fd, &msg, 0);
569 if (ERRNO_IS_NEG_TRANSIENT(len) || ERRNO_IS_NEG_DISCONNECT(len))
570 return 0;
571 if (len < 0) {
572 log_dhcp_server_errno(server, len, "Could not receive message, ignoring: %m");
573 return 0;
574 }
575
576 r = dhcp_server_process_message(server, &IOVEC_MAKE(buf, len), &msg);
577 if (r < 0)
578 log_dhcp_server_errno(server, r, "Couldn't process incoming message, ignoring: %m");
579
580 return 0;
581 }
582
583 static int server_open_socket(sd_dhcp_server *server) {
584 int r;
585
586 assert(server);
587
588 _cleanup_close_ int fd = RET_NERRNO(socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0));
589 if (fd < 0)
590 return fd;
591
592 r = socket_bind_to_ifindex(fd, server->ifindex);
593 if (r < 0)
594 return r;
595
596 r = setsockopt_int(fd, SOL_SOCKET, SO_TIMESTAMP, true);
597 if (r < 0)
598 return r;
599
600 r = setsockopt_int(fd, SOL_SOCKET, SO_REUSEADDR, true);
601 if (r < 0)
602 return r;
603
604 r = setsockopt_int(fd, SOL_SOCKET, SO_BROADCAST, true);
605 if (r < 0)
606 return r;
607
608 r = setsockopt_int(fd, SOL_SOCKET, SO_PRIORITY, tos_to_priority(server->ip_service_type));
609 if (r < 0)
610 return r;
611
612 r = setsockopt_int(fd, IPPROTO_IP, IP_TOS, server->ip_service_type);
613 if (r < 0)
614 return r;
615
616 r = setsockopt_int(fd, IPPROTO_IP, IP_PKTINFO, true);
617 if (r < 0)
618 return r;
619
620 union sockaddr_union sa = {
621 .in.sin_family = AF_INET,
622 .in.sin_port = htobe16(DHCP_PORT_SERVER),
623 .in.sin_addr.s_addr = htobe32(INADDR_ANY),
624 };
625
626 if (bind(fd, &sa.sa, sizeof(sa.in)) < 0)
627 return -errno;
628
629 return TAKE_FD(fd);
630 }
631
632 int dhcp_server_setup_io_event_source(sd_dhcp_server *server) {
633 int r;
634
635 assert(server);
636 assert(server->event);
637
638 _cleanup_close_ int fd_close = -EBADF;
639 int fd;
640 if (server->socket_fd >= 0)
641 /* When a socket fd is given externally, unconditionally use it and do not close the socket
642 * even if we fail to set up the event source. */
643 fd = server->socket_fd;
644 else {
645 fd = fd_close = server_open_socket(server);
646 if (fd < 0)
647 return fd;
648 }
649
650 _cleanup_(sd_event_source_unrefp) sd_event_source *s = NULL;
651 r = sd_event_add_io(server->event, &s, fd, EPOLLIN, server_receive_message, server);
652 if (r < 0)
653 return r;
654
655 r = sd_event_source_set_priority(s, server->event_priority);
656 if (r < 0)
657 return r;
658
659 (void) sd_event_source_set_description(s, "dhcp-server-io");
660
661 if (fd_close >= 0) {
662 r = sd_event_source_set_io_fd_own(s, true);
663 if (r < 0)
664 return r;
665 TAKE_FD(fd_close);
666 }
667
668 sd_event_source_disable_unref(server->io_event_source);
669 server->io_event_source = TAKE_PTR(s);
670 return 0;
671 }