]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/libsystemd-network/sd-dhcp-client.c
Merge pull request #164 from l10n-tw/master
[thirdparty/systemd.git] / src / libsystemd-network / sd-dhcp-client.c
1 /***
2 This file is part of systemd.
3
4 Copyright (C) 2013 Intel Corporation. All rights reserved.
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include <stdlib.h>
21 #include <errno.h>
22 #include <string.h>
23 #include <stdio.h>
24 #include <net/ethernet.h>
25 #include <net/if_arp.h>
26 #include <linux/if_infiniband.h>
27 #include <sys/ioctl.h>
28
29 #include "util.h"
30 #include "refcnt.h"
31 #include "random-util.h"
32 #include "async.h"
33
34 #include "dhcp-protocol.h"
35 #include "dhcp-internal.h"
36 #include "dhcp-lease-internal.h"
37 #include "dhcp-identifier.h"
38 #include "sd-dhcp-client.h"
39
40 #define MAX_CLIENT_ID_LEN (sizeof(uint32_t) + MAX_DUID_LEN) /* Arbitrary limit */
41 #define MAX_MAC_ADDR_LEN CONST_MAX(INFINIBAND_ALEN, ETH_ALEN)
42
43 struct sd_dhcp_client {
44 RefCount n_ref;
45
46 DHCPState state;
47 sd_event *event;
48 int event_priority;
49 sd_event_source *timeout_resend;
50 int index;
51 int fd;
52 union sockaddr_union link;
53 sd_event_source *receive_message;
54 bool request_broadcast;
55 uint8_t *req_opts;
56 size_t req_opts_allocated;
57 size_t req_opts_size;
58 be32_t last_addr;
59 uint8_t mac_addr[MAX_MAC_ADDR_LEN];
60 size_t mac_addr_len;
61 uint16_t arp_type;
62 struct {
63 uint8_t type;
64 union {
65 struct {
66 /* 0: Generic (non-LL) (RFC 2132) */
67 uint8_t data[MAX_CLIENT_ID_LEN];
68 } _packed_ gen;
69 struct {
70 /* 1: Ethernet Link-Layer (RFC 2132) */
71 uint8_t haddr[ETH_ALEN];
72 } _packed_ eth;
73 struct {
74 /* 2 - 254: ARP/Link-Layer (RFC 2132) */
75 uint8_t haddr[0];
76 } _packed_ ll;
77 struct {
78 /* 255: Node-specific (RFC 4361) */
79 uint32_t iaid;
80 struct duid duid;
81 } _packed_ ns;
82 struct {
83 uint8_t data[MAX_CLIENT_ID_LEN];
84 } _packed_ raw;
85 };
86 } _packed_ client_id;
87 size_t client_id_len;
88 char *hostname;
89 char *vendor_class_identifier;
90 uint32_t mtu;
91 uint32_t xid;
92 usec_t start_time;
93 unsigned int attempt;
94 usec_t request_sent;
95 sd_event_source *timeout_t1;
96 sd_event_source *timeout_t2;
97 sd_event_source *timeout_expire;
98 sd_dhcp_client_cb_t cb;
99 void *userdata;
100 sd_dhcp_lease *lease;
101 };
102
103 static const uint8_t default_req_opts[] = {
104 DHCP_OPTION_SUBNET_MASK,
105 DHCP_OPTION_ROUTER,
106 DHCP_OPTION_HOST_NAME,
107 DHCP_OPTION_DOMAIN_NAME,
108 DHCP_OPTION_DOMAIN_NAME_SERVER,
109 DHCP_OPTION_NTP_SERVER,
110 };
111
112 static int client_receive_message_raw(sd_event_source *s, int fd,
113 uint32_t revents, void *userdata);
114 static int client_receive_message_udp(sd_event_source *s, int fd,
115 uint32_t revents, void *userdata);
116 static void client_stop(sd_dhcp_client *client, int error);
117
118 int sd_dhcp_client_set_callback(sd_dhcp_client *client, sd_dhcp_client_cb_t cb,
119 void *userdata) {
120 assert_return(client, -EINVAL);
121
122 client->cb = cb;
123 client->userdata = userdata;
124
125 return 0;
126 }
127
128 int sd_dhcp_client_set_request_broadcast(sd_dhcp_client *client, int broadcast) {
129 assert_return(client, -EINVAL);
130
131 client->request_broadcast = !!broadcast;
132
133 return 0;
134 }
135
136 int sd_dhcp_client_set_request_option(sd_dhcp_client *client, uint8_t option) {
137 size_t i;
138
139 assert_return(client, -EINVAL);
140 assert_return (IN_SET(client->state, DHCP_STATE_INIT,
141 DHCP_STATE_STOPPED), -EBUSY);
142
143 switch(option) {
144 case DHCP_OPTION_PAD:
145 case DHCP_OPTION_OVERLOAD:
146 case DHCP_OPTION_MESSAGE_TYPE:
147 case DHCP_OPTION_PARAMETER_REQUEST_LIST:
148 case DHCP_OPTION_END:
149 return -EINVAL;
150
151 default:
152 break;
153 }
154
155 for (i = 0; i < client->req_opts_size; i++)
156 if (client->req_opts[i] == option)
157 return -EEXIST;
158
159 if (!GREEDY_REALLOC(client->req_opts, client->req_opts_allocated,
160 client->req_opts_size + 1))
161 return -ENOMEM;
162
163 client->req_opts[client->req_opts_size++] = option;
164
165 return 0;
166 }
167
168 int sd_dhcp_client_set_request_address(sd_dhcp_client *client,
169 const struct in_addr *last_addr) {
170 assert_return(client, -EINVAL);
171 assert_return (IN_SET(client->state, DHCP_STATE_INIT,
172 DHCP_STATE_STOPPED), -EBUSY);
173
174 if (last_addr)
175 client->last_addr = last_addr->s_addr;
176 else
177 client->last_addr = INADDR_ANY;
178
179 return 0;
180 }
181
182 int sd_dhcp_client_set_index(sd_dhcp_client *client, int interface_index) {
183 assert_return(client, -EINVAL);
184 assert_return (IN_SET(client->state, DHCP_STATE_INIT,
185 DHCP_STATE_STOPPED), -EBUSY);
186 assert_return(interface_index > 0, -EINVAL);
187
188 client->index = interface_index;
189
190 return 0;
191 }
192
193 int sd_dhcp_client_set_mac(sd_dhcp_client *client, const uint8_t *addr,
194 size_t addr_len, uint16_t arp_type) {
195 DHCP_CLIENT_DONT_DESTROY(client);
196 bool need_restart = false;
197
198 assert_return(client, -EINVAL);
199 assert_return(addr, -EINVAL);
200 assert_return(addr_len > 0 && addr_len <= MAX_MAC_ADDR_LEN, -EINVAL);
201 assert_return(arp_type > 0, -EINVAL);
202
203 if (arp_type == ARPHRD_ETHER)
204 assert_return(addr_len == ETH_ALEN, -EINVAL);
205 else if (arp_type == ARPHRD_INFINIBAND)
206 assert_return(addr_len == INFINIBAND_ALEN, -EINVAL);
207 else
208 return -EINVAL;
209
210 if (client->mac_addr_len == addr_len &&
211 memcmp(&client->mac_addr, addr, addr_len) == 0)
212 return 0;
213
214 if (!IN_SET(client->state, DHCP_STATE_INIT, DHCP_STATE_STOPPED)) {
215 log_dhcp_client(client, "Changing MAC address on running DHCP "
216 "client, restarting");
217 need_restart = true;
218 client_stop(client, DHCP_EVENT_STOP);
219 }
220
221 memcpy(&client->mac_addr, addr, addr_len);
222 client->mac_addr_len = addr_len;
223 client->arp_type = arp_type;
224
225 if (need_restart && client->state != DHCP_STATE_STOPPED)
226 sd_dhcp_client_start(client);
227
228 return 0;
229 }
230
231 int sd_dhcp_client_get_client_id(sd_dhcp_client *client, uint8_t *type,
232 const uint8_t **data, size_t *data_len) {
233
234 assert_return(client, -EINVAL);
235 assert_return(type, -EINVAL);
236 assert_return(data, -EINVAL);
237 assert_return(data_len, -EINVAL);
238
239 *type = 0;
240 *data = NULL;
241 *data_len = 0;
242 if (client->client_id_len) {
243 *type = client->client_id.type;
244 *data = client->client_id.raw.data;
245 *data_len = client->client_id_len - sizeof(client->client_id.type);
246 }
247
248 return 0;
249 }
250
251 int sd_dhcp_client_set_client_id(sd_dhcp_client *client, uint8_t type,
252 const uint8_t *data, size_t data_len) {
253 DHCP_CLIENT_DONT_DESTROY(client);
254 bool need_restart = false;
255
256 assert_return(client, -EINVAL);
257 assert_return(data, -EINVAL);
258 assert_return(data_len > 0 && data_len <= MAX_CLIENT_ID_LEN, -EINVAL);
259
260 switch (type) {
261 case ARPHRD_ETHER:
262 if (data_len != ETH_ALEN)
263 return -EINVAL;
264 break;
265 case ARPHRD_INFINIBAND:
266 if (data_len != INFINIBAND_ALEN)
267 return -EINVAL;
268 break;
269 default:
270 break;
271 }
272
273 if (client->client_id_len == data_len + sizeof(client->client_id.type) &&
274 client->client_id.type == type &&
275 memcmp(&client->client_id.raw.data, data, data_len) == 0)
276 return 0;
277
278 if (!IN_SET(client->state, DHCP_STATE_INIT, DHCP_STATE_STOPPED)) {
279 log_dhcp_client(client, "Changing client ID on running DHCP "
280 "client, restarting");
281 need_restart = true;
282 client_stop(client, DHCP_EVENT_STOP);
283 }
284
285 client->client_id.type = type;
286 memcpy(&client->client_id.raw.data, data, data_len);
287 client->client_id_len = data_len + sizeof (client->client_id.type);
288
289 if (need_restart && client->state != DHCP_STATE_STOPPED)
290 sd_dhcp_client_start(client);
291
292 return 0;
293 }
294
295 int sd_dhcp_client_set_hostname(sd_dhcp_client *client,
296 const char *hostname) {
297 char *new_hostname = NULL;
298
299 assert_return(client, -EINVAL);
300
301 if (streq_ptr(client->hostname, hostname))
302 return 0;
303
304 if (hostname) {
305 new_hostname = strdup(hostname);
306 if (!new_hostname)
307 return -ENOMEM;
308 }
309
310 free(client->hostname);
311 client->hostname = new_hostname;
312
313 return 0;
314 }
315
316 int sd_dhcp_client_set_vendor_class_identifier(sd_dhcp_client *client,
317 const char *vci) {
318 char *new_vci = NULL;
319
320 assert_return(client, -EINVAL);
321
322 new_vci = strdup(vci);
323 if (!new_vci)
324 return -ENOMEM;
325
326 free(client->vendor_class_identifier);
327
328 client->vendor_class_identifier = new_vci;
329
330 return 0;
331 }
332
333 int sd_dhcp_client_set_mtu(sd_dhcp_client *client, uint32_t mtu) {
334 assert_return(client, -EINVAL);
335 assert_return(mtu >= DHCP_DEFAULT_MIN_SIZE, -ERANGE);
336
337 client->mtu = mtu;
338
339 return 0;
340 }
341
342 int sd_dhcp_client_get_lease(sd_dhcp_client *client, sd_dhcp_lease **ret) {
343 assert_return(client, -EINVAL);
344 assert_return(ret, -EINVAL);
345
346 if (client->state != DHCP_STATE_BOUND &&
347 client->state != DHCP_STATE_RENEWING &&
348 client->state != DHCP_STATE_REBINDING)
349 return -EADDRNOTAVAIL;
350
351 *ret = sd_dhcp_lease_ref(client->lease);
352
353 return 0;
354 }
355
356 static void client_notify(sd_dhcp_client *client, int event) {
357 if (client->cb)
358 client->cb(client, event, client->userdata);
359 }
360
361 static int client_initialize(sd_dhcp_client *client) {
362 assert_return(client, -EINVAL);
363
364 client->receive_message =
365 sd_event_source_unref(client->receive_message);
366
367 client->fd = asynchronous_close(client->fd);
368
369 client->timeout_resend = sd_event_source_unref(client->timeout_resend);
370
371 client->timeout_t1 = sd_event_source_unref(client->timeout_t1);
372 client->timeout_t2 = sd_event_source_unref(client->timeout_t2);
373 client->timeout_expire = sd_event_source_unref(client->timeout_expire);
374
375 client->attempt = 1;
376
377 client->state = DHCP_STATE_INIT;
378 client->xid = 0;
379
380 if (client->lease)
381 client->lease = sd_dhcp_lease_unref(client->lease);
382
383 return 0;
384 }
385
386 static void client_stop(sd_dhcp_client *client, int error) {
387 assert(client);
388
389 if (error < 0)
390 log_dhcp_client(client, "STOPPED: %s", strerror(-error));
391 else if (error == DHCP_EVENT_STOP)
392 log_dhcp_client(client, "STOPPED");
393 else
394 log_dhcp_client(client, "STOPPED: Unknown event");
395
396 client_notify(client, error);
397
398 client_initialize(client);
399 }
400
401 static int client_message_init(sd_dhcp_client *client, DHCPPacket **ret,
402 uint8_t type, size_t *_optlen, size_t *_optoffset) {
403 _cleanup_free_ DHCPPacket *packet;
404 size_t optlen, optoffset, size;
405 be16_t max_size;
406 usec_t time_now;
407 uint16_t secs;
408 int r;
409
410 assert(client);
411 assert(client->start_time);
412 assert(ret);
413 assert(_optlen);
414 assert(_optoffset);
415 assert(type == DHCP_DISCOVER || type == DHCP_REQUEST);
416
417 optlen = DHCP_MIN_OPTIONS_SIZE;
418 size = sizeof(DHCPPacket) + optlen;
419
420 packet = malloc0(size);
421 if (!packet)
422 return -ENOMEM;
423
424 r = dhcp_message_init(&packet->dhcp, BOOTREQUEST, client->xid, type,
425 client->arp_type, optlen, &optoffset);
426 if (r < 0)
427 return r;
428
429 /* Although 'secs' field is a SHOULD in RFC 2131, certain DHCP servers
430 refuse to issue an DHCP lease if 'secs' is set to zero */
431 r = sd_event_now(client->event, clock_boottime_or_monotonic(), &time_now);
432 if (r < 0)
433 return r;
434 assert(time_now >= client->start_time);
435
436 /* seconds between sending first and last DISCOVER
437 * must always be strictly positive to deal with broken servers */
438 secs = ((time_now - client->start_time) / USEC_PER_SEC) ? : 1;
439 packet->dhcp.secs = htobe16(secs);
440
441 /* RFC2132 section 4.1
442 A client that cannot receive unicast IP datagrams until its protocol
443 software has been configured with an IP address SHOULD set the
444 BROADCAST bit in the 'flags' field to 1 in any DHCPDISCOVER or
445 DHCPREQUEST messages that client sends. The BROADCAST bit will
446 provide a hint to the DHCP server and BOOTP relay agent to broadcast
447 any messages to the client on the client's subnet.
448
449 Note: some interfaces needs this to be enabled, but some networks
450 needs this to be disabled as broadcasts are filteretd, so this
451 needs to be configurable */
452 if (client->request_broadcast || client->arp_type != ARPHRD_ETHER)
453 packet->dhcp.flags = htobe16(0x8000);
454
455 /* RFC2132 section 4.1.1:
456 The client MUST include its hardware address in the ’chaddr’ field, if
457 necessary for delivery of DHCP reply messages. Non-Ethernet
458 interfaces will leave 'chaddr' empty and use the client identifier
459 instead (eg, RFC 4390 section 2.1).
460 */
461 if (client->arp_type == ARPHRD_ETHER)
462 memcpy(&packet->dhcp.chaddr, &client->mac_addr, ETH_ALEN);
463
464 /* If no client identifier exists, construct an RFC 4361-compliant one */
465 if (client->client_id_len == 0) {
466 size_t duid_len;
467
468 client->client_id.type = 255;
469
470 r = dhcp_identifier_set_iaid(client->index, client->mac_addr, client->mac_addr_len, &client->client_id.ns.iaid);
471 if (r < 0)
472 return r;
473
474 r = dhcp_identifier_set_duid_en(&client->client_id.ns.duid, &duid_len);
475 if (r < 0)
476 return r;
477
478 client->client_id_len = sizeof(client->client_id.type) + sizeof(client->client_id.ns.iaid) + duid_len;
479 }
480
481 /* Some DHCP servers will refuse to issue an DHCP lease if the Client
482 Identifier option is not set */
483 if (client->client_id_len) {
484 r = dhcp_option_append(&packet->dhcp, optlen, &optoffset, 0,
485 DHCP_OPTION_CLIENT_IDENTIFIER,
486 client->client_id_len,
487 &client->client_id);
488 if (r < 0)
489 return r;
490 }
491
492 /* RFC2131 section 3.5:
493 in its initial DHCPDISCOVER or DHCPREQUEST message, a
494 client may provide the server with a list of specific
495 parameters the client is interested in. If the client
496 includes a list of parameters in a DHCPDISCOVER message,
497 it MUST include that list in any subsequent DHCPREQUEST
498 messages.
499 */
500 r = dhcp_option_append(&packet->dhcp, optlen, &optoffset, 0,
501 DHCP_OPTION_PARAMETER_REQUEST_LIST,
502 client->req_opts_size, client->req_opts);
503 if (r < 0)
504 return r;
505
506 /* RFC2131 section 3.5:
507 The client SHOULD include the ’maximum DHCP message size’ option to
508 let the server know how large the server may make its DHCP messages.
509
510 Note (from ConnMan): Some DHCP servers will send bigger DHCP packets
511 than the defined default size unless the Maximum Messge Size option
512 is explicitly set
513
514 RFC3442 "Requirements to Avoid Sizing Constraints":
515 Because a full routing table can be quite large, the standard 576
516 octet maximum size for a DHCP message may be too short to contain
517 some legitimate Classless Static Route options. Because of this,
518 clients implementing the Classless Static Route option SHOULD send a
519 Maximum DHCP Message Size [4] option if the DHCP client's TCP/IP
520 stack is capable of receiving larger IP datagrams. In this case, the
521 client SHOULD set the value of this option to at least the MTU of the
522 interface that the client is configuring. The client MAY set the
523 value of this option higher, up to the size of the largest UDP packet
524 it is prepared to accept. (Note that the value specified in the
525 Maximum DHCP Message Size option is the total maximum packet size,
526 including IP and UDP headers.)
527 */
528 max_size = htobe16(size);
529 r = dhcp_option_append(&packet->dhcp, client->mtu, &optoffset, 0,
530 DHCP_OPTION_MAXIMUM_MESSAGE_SIZE,
531 2, &max_size);
532 if (r < 0)
533 return r;
534
535 *_optlen = optlen;
536 *_optoffset = optoffset;
537 *ret = packet;
538 packet = NULL;
539
540 return 0;
541 }
542
543 static int dhcp_client_send_raw(sd_dhcp_client *client, DHCPPacket *packet,
544 size_t len) {
545 dhcp_packet_append_ip_headers(packet, INADDR_ANY, DHCP_PORT_CLIENT,
546 INADDR_BROADCAST, DHCP_PORT_SERVER, len);
547
548 return dhcp_network_send_raw_socket(client->fd, &client->link,
549 packet, len);
550 }
551
552 static int client_send_discover(sd_dhcp_client *client) {
553 _cleanup_free_ DHCPPacket *discover = NULL;
554 size_t optoffset, optlen;
555 int r;
556
557 assert(client);
558 assert(client->state == DHCP_STATE_INIT ||
559 client->state == DHCP_STATE_SELECTING);
560
561 r = client_message_init(client, &discover, DHCP_DISCOVER,
562 &optlen, &optoffset);
563 if (r < 0)
564 return r;
565
566 /* the client may suggest values for the network address
567 and lease time in the DHCPDISCOVER message. The client may include
568 the ’requested IP address’ option to suggest that a particular IP
569 address be assigned, and may include the ’IP address lease time’
570 option to suggest the lease time it would like.
571 */
572 if (client->last_addr != INADDR_ANY) {
573 r = dhcp_option_append(&discover->dhcp, optlen, &optoffset, 0,
574 DHCP_OPTION_REQUESTED_IP_ADDRESS,
575 4, &client->last_addr);
576 if (r < 0)
577 return r;
578 }
579
580 /* it is unclear from RFC 2131 if client should send hostname in
581 DHCPDISCOVER but dhclient does and so we do as well
582 */
583 if (client->hostname) {
584 r = dhcp_option_append(&discover->dhcp, optlen, &optoffset, 0,
585 DHCP_OPTION_HOST_NAME,
586 strlen(client->hostname), client->hostname);
587 if (r < 0)
588 return r;
589 }
590
591 if (client->vendor_class_identifier) {
592 r = dhcp_option_append(&discover->dhcp, optlen, &optoffset, 0,
593 DHCP_OPTION_VENDOR_CLASS_IDENTIFIER,
594 strlen(client->vendor_class_identifier),
595 client->vendor_class_identifier);
596 if (r < 0)
597 return r;
598 }
599
600 r = dhcp_option_append(&discover->dhcp, optlen, &optoffset, 0,
601 DHCP_OPTION_END, 0, NULL);
602 if (r < 0)
603 return r;
604
605 /* We currently ignore:
606 The client SHOULD wait a random time between one and ten seconds to
607 desynchronize the use of DHCP at startup.
608 */
609 r = dhcp_client_send_raw(client, discover, sizeof(DHCPPacket) + optoffset);
610 if (r < 0)
611 return r;
612
613 log_dhcp_client(client, "DISCOVER");
614
615 return 0;
616 }
617
618 static int client_send_request(sd_dhcp_client *client) {
619 _cleanup_free_ DHCPPacket *request = NULL;
620 size_t optoffset, optlen;
621 int r;
622
623 r = client_message_init(client, &request, DHCP_REQUEST,
624 &optlen, &optoffset);
625 if (r < 0)
626 return r;
627
628 switch (client->state) {
629 /* See RFC2131 section 4.3.2 (note that there is a typo in the RFC,
630 SELECTING should be REQUESTING)
631 */
632
633 case DHCP_STATE_REQUESTING:
634 /* Client inserts the address of the selected server in ’server
635 identifier’, ’ciaddr’ MUST be zero, ’requested IP address’ MUST be
636 filled in with the yiaddr value from the chosen DHCPOFFER.
637 */
638
639 r = dhcp_option_append(&request->dhcp, optlen, &optoffset, 0,
640 DHCP_OPTION_SERVER_IDENTIFIER,
641 4, &client->lease->server_address);
642 if (r < 0)
643 return r;
644
645 r = dhcp_option_append(&request->dhcp, optlen, &optoffset, 0,
646 DHCP_OPTION_REQUESTED_IP_ADDRESS,
647 4, &client->lease->address);
648 if (r < 0)
649 return r;
650
651 break;
652
653 case DHCP_STATE_INIT_REBOOT:
654 /* ’server identifier’ MUST NOT be filled in, ’requested IP address’
655 option MUST be filled in with client’s notion of its previously
656 assigned address. ’ciaddr’ MUST be zero.
657 */
658 r = dhcp_option_append(&request->dhcp, optlen, &optoffset, 0,
659 DHCP_OPTION_REQUESTED_IP_ADDRESS,
660 4, &client->last_addr);
661 if (r < 0)
662 return r;
663 break;
664
665 case DHCP_STATE_RENEWING:
666 /* ’server identifier’ MUST NOT be filled in, ’requested IP address’
667 option MUST NOT be filled in, ’ciaddr’ MUST be filled in with
668 client’s IP address.
669 */
670
671 /* fall through */
672 case DHCP_STATE_REBINDING:
673 /* ’server identifier’ MUST NOT be filled in, ’requested IP address’
674 option MUST NOT be filled in, ’ciaddr’ MUST be filled in with
675 client’s IP address.
676
677 This message MUST be broadcast to the 0xffffffff IP broadcast address.
678 */
679 request->dhcp.ciaddr = client->lease->address;
680
681 break;
682
683 case DHCP_STATE_INIT:
684 case DHCP_STATE_SELECTING:
685 case DHCP_STATE_REBOOTING:
686 case DHCP_STATE_BOUND:
687 case DHCP_STATE_STOPPED:
688 return -EINVAL;
689 }
690
691 if (client->hostname) {
692 r = dhcp_option_append(&request->dhcp, optlen, &optoffset, 0,
693 DHCP_OPTION_HOST_NAME,
694 strlen(client->hostname), client->hostname);
695 if (r < 0)
696 return r;
697 }
698
699 r = dhcp_option_append(&request->dhcp, optlen, &optoffset, 0,
700 DHCP_OPTION_END, 0, NULL);
701 if (r < 0)
702 return r;
703
704 if (client->state == DHCP_STATE_RENEWING) {
705 r = dhcp_network_send_udp_socket(client->fd,
706 client->lease->server_address,
707 DHCP_PORT_SERVER,
708 &request->dhcp,
709 sizeof(DHCPMessage) + optoffset);
710 } else {
711 r = dhcp_client_send_raw(client, request, sizeof(DHCPPacket) + optoffset);
712 }
713 if (r < 0)
714 return r;
715
716 switch (client->state) {
717 case DHCP_STATE_REQUESTING:
718 log_dhcp_client(client, "REQUEST (requesting)");
719 break;
720 case DHCP_STATE_INIT_REBOOT:
721 log_dhcp_client(client, "REQUEST (init-reboot)");
722 break;
723 case DHCP_STATE_RENEWING:
724 log_dhcp_client(client, "REQUEST (renewing)");
725 break;
726 case DHCP_STATE_REBINDING:
727 log_dhcp_client(client, "REQUEST (rebinding)");
728 break;
729 default:
730 log_dhcp_client(client, "REQUEST (invalid)");
731 break;
732 }
733
734 return 0;
735 }
736
737 static int client_start(sd_dhcp_client *client);
738
739 static int client_timeout_resend(sd_event_source *s, uint64_t usec,
740 void *userdata) {
741 sd_dhcp_client *client = userdata;
742 DHCP_CLIENT_DONT_DESTROY(client);
743 usec_t next_timeout = 0;
744 uint64_t time_now;
745 uint32_t time_left;
746 int r;
747
748 assert(s);
749 assert(client);
750 assert(client->event);
751
752 r = sd_event_now(client->event, clock_boottime_or_monotonic(), &time_now);
753 if (r < 0)
754 goto error;
755
756 switch (client->state) {
757 case DHCP_STATE_RENEWING:
758
759 time_left = (client->lease->t2 - client->lease->t1) / 2;
760 if (time_left < 60)
761 time_left = 60;
762
763 next_timeout = time_now + time_left * USEC_PER_SEC;
764
765 break;
766
767 case DHCP_STATE_REBINDING:
768
769 time_left = (client->lease->lifetime - client->lease->t2) / 2;
770 if (time_left < 60)
771 time_left = 60;
772
773 next_timeout = time_now + time_left * USEC_PER_SEC;
774 break;
775
776 case DHCP_STATE_REBOOTING:
777 /* start over as we did not receive a timely ack or nak */
778 r = client_initialize(client);
779 if (r < 0)
780 goto error;
781
782 r = client_start(client);
783 if (r < 0)
784 goto error;
785 else {
786 log_dhcp_client(client, "REBOOTED");
787 return 0;
788 }
789
790 case DHCP_STATE_INIT:
791 case DHCP_STATE_INIT_REBOOT:
792 case DHCP_STATE_SELECTING:
793 case DHCP_STATE_REQUESTING:
794 case DHCP_STATE_BOUND:
795
796 if (client->attempt < 64)
797 client->attempt *= 2;
798
799 next_timeout = time_now + (client->attempt - 1) * USEC_PER_SEC;
800
801 break;
802
803 case DHCP_STATE_STOPPED:
804 r = -EINVAL;
805 goto error;
806 }
807
808 next_timeout += (random_u32() & 0x1fffff);
809
810 client->timeout_resend = sd_event_source_unref(client->timeout_resend);
811
812 r = sd_event_add_time(client->event,
813 &client->timeout_resend,
814 clock_boottime_or_monotonic(),
815 next_timeout, 10 * USEC_PER_MSEC,
816 client_timeout_resend, client);
817 if (r < 0)
818 goto error;
819
820 r = sd_event_source_set_priority(client->timeout_resend,
821 client->event_priority);
822 if (r < 0)
823 goto error;
824
825 r = sd_event_source_set_description(client->timeout_resend, "dhcp4-resend-timer");
826 if (r < 0)
827 goto error;
828
829 switch (client->state) {
830 case DHCP_STATE_INIT:
831 r = client_send_discover(client);
832 if (r >= 0) {
833 client->state = DHCP_STATE_SELECTING;
834 client->attempt = 1;
835 } else {
836 if (client->attempt >= 64)
837 goto error;
838 }
839
840 break;
841
842 case DHCP_STATE_SELECTING:
843 r = client_send_discover(client);
844 if (r < 0 && client->attempt >= 64)
845 goto error;
846
847 break;
848
849 case DHCP_STATE_INIT_REBOOT:
850 case DHCP_STATE_REQUESTING:
851 case DHCP_STATE_RENEWING:
852 case DHCP_STATE_REBINDING:
853 r = client_send_request(client);
854 if (r < 0 && client->attempt >= 64)
855 goto error;
856
857 if (client->state == DHCP_STATE_INIT_REBOOT)
858 client->state = DHCP_STATE_REBOOTING;
859
860 client->request_sent = time_now;
861
862 break;
863
864 case DHCP_STATE_REBOOTING:
865 case DHCP_STATE_BOUND:
866
867 break;
868
869 case DHCP_STATE_STOPPED:
870 r = -EINVAL;
871 goto error;
872 }
873
874 return 0;
875
876 error:
877 client_stop(client, r);
878
879 /* Errors were dealt with when stopping the client, don't spill
880 errors into the event loop handler */
881 return 0;
882 }
883
884 static int client_initialize_io_events(sd_dhcp_client *client,
885 sd_event_io_handler_t io_callback) {
886 int r;
887
888 assert(client);
889 assert(client->event);
890
891 r = sd_event_add_io(client->event, &client->receive_message,
892 client->fd, EPOLLIN, io_callback,
893 client);
894 if (r < 0)
895 goto error;
896
897 r = sd_event_source_set_priority(client->receive_message,
898 client->event_priority);
899 if (r < 0)
900 goto error;
901
902 r = sd_event_source_set_description(client->receive_message, "dhcp4-receive-message");
903 if (r < 0)
904 goto error;
905
906 error:
907 if (r < 0)
908 client_stop(client, r);
909
910 return 0;
911 }
912
913 static int client_initialize_time_events(sd_dhcp_client *client) {
914 int r;
915
916 assert(client);
917 assert(client->event);
918
919 client->timeout_resend = sd_event_source_unref(client->timeout_resend);
920
921 r = sd_event_add_time(client->event,
922 &client->timeout_resend,
923 clock_boottime_or_monotonic(),
924 0, 0,
925 client_timeout_resend, client);
926 if (r < 0)
927 goto error;
928
929 r = sd_event_source_set_priority(client->timeout_resend,
930 client->event_priority);
931
932 r = sd_event_source_set_description(client->timeout_resend, "dhcp4-resend-timer");
933 if (r < 0)
934 goto error;
935
936 error:
937 if (r < 0)
938 client_stop(client, r);
939
940 return 0;
941
942 }
943
944 static int client_initialize_events(sd_dhcp_client *client,
945 sd_event_io_handler_t io_callback) {
946 client_initialize_io_events(client, io_callback);
947 client_initialize_time_events(client);
948
949 return 0;
950 }
951
952 static int client_start(sd_dhcp_client *client) {
953 int r;
954
955 assert_return(client, -EINVAL);
956 assert_return(client->event, -EINVAL);
957 assert_return(client->index > 0, -EINVAL);
958 assert_return(client->fd < 0, -EBUSY);
959 assert_return(client->xid == 0, -EINVAL);
960 assert_return(client->state == DHCP_STATE_INIT ||
961 client->state == DHCP_STATE_INIT_REBOOT, -EBUSY);
962
963 client->xid = random_u32();
964
965 r = dhcp_network_bind_raw_socket(client->index, &client->link,
966 client->xid, client->mac_addr,
967 client->mac_addr_len, client->arp_type);
968 if (r < 0) {
969 client_stop(client, r);
970 return r;
971 }
972 client->fd = r;
973
974 if (client->state == DHCP_STATE_INIT || client->state == DHCP_STATE_INIT_REBOOT)
975 client->start_time = now(clock_boottime_or_monotonic());
976
977 return client_initialize_events(client, client_receive_message_raw);
978 }
979
980 static int client_timeout_expire(sd_event_source *s, uint64_t usec,
981 void *userdata) {
982 sd_dhcp_client *client = userdata;
983 DHCP_CLIENT_DONT_DESTROY(client);
984
985 log_dhcp_client(client, "EXPIRED");
986
987 client_notify(client, DHCP_EVENT_EXPIRED);
988
989 /* lease was lost, start over if not freed or stopped in callback */
990 if (client->state != DHCP_STATE_STOPPED) {
991 client_initialize(client);
992 client_start(client);
993 }
994
995 return 0;
996 }
997
998 static int client_timeout_t2(sd_event_source *s, uint64_t usec, void *userdata) {
999 sd_dhcp_client *client = userdata;
1000 DHCP_CLIENT_DONT_DESTROY(client);
1001 int r;
1002
1003 client->receive_message = sd_event_source_unref(client->receive_message);
1004 client->fd = asynchronous_close(client->fd);
1005
1006 client->state = DHCP_STATE_REBINDING;
1007 client->attempt = 1;
1008
1009 r = dhcp_network_bind_raw_socket(client->index, &client->link,
1010 client->xid, client->mac_addr,
1011 client->mac_addr_len, client->arp_type);
1012 if (r < 0) {
1013 client_stop(client, r);
1014 return 0;
1015 }
1016 client->fd = r;
1017
1018 return client_initialize_events(client, client_receive_message_raw);
1019 }
1020
1021 static int client_timeout_t1(sd_event_source *s, uint64_t usec,
1022 void *userdata) {
1023 sd_dhcp_client *client = userdata;
1024 DHCP_CLIENT_DONT_DESTROY(client);
1025
1026 client->state = DHCP_STATE_RENEWING;
1027 client->attempt = 1;
1028
1029 return client_initialize_time_events(client);
1030 }
1031
1032 static int client_handle_offer(sd_dhcp_client *client, DHCPMessage *offer,
1033 size_t len) {
1034 _cleanup_dhcp_lease_unref_ sd_dhcp_lease *lease = NULL;
1035 int r;
1036
1037 r = dhcp_lease_new(&lease);
1038 if (r < 0)
1039 return r;
1040
1041 if (client->client_id_len) {
1042 r = dhcp_lease_set_client_id(lease,
1043 (uint8_t *) &client->client_id,
1044 client->client_id_len);
1045 if (r < 0)
1046 return r;
1047 }
1048
1049 r = dhcp_option_parse(offer, len, dhcp_lease_parse_options, lease);
1050 if (r != DHCP_OFFER) {
1051 log_dhcp_client(client, "received message was not an OFFER, ignoring");
1052 return -ENOMSG;
1053 }
1054
1055 lease->next_server = offer->siaddr;
1056
1057 lease->address = offer->yiaddr;
1058
1059 if (lease->address == INADDR_ANY ||
1060 lease->server_address == INADDR_ANY ||
1061 lease->lifetime == 0) {
1062 log_dhcp_client(client, "received lease lacks address, server "
1063 "address or lease lifetime, ignoring");
1064 return -ENOMSG;
1065 }
1066
1067 if (lease->subnet_mask == INADDR_ANY) {
1068 r = dhcp_lease_set_default_subnet_mask(lease);
1069 if (r < 0) {
1070 log_dhcp_client(client, "received lease lacks subnet "
1071 "mask, and a fallback one can not be "
1072 "generated, ignoring");
1073 return -ENOMSG;
1074 }
1075 }
1076
1077 sd_dhcp_lease_unref(client->lease);
1078 client->lease = lease;
1079 lease = NULL;
1080
1081 log_dhcp_client(client, "OFFER");
1082
1083 return 0;
1084 }
1085
1086 static int client_handle_forcerenew(sd_dhcp_client *client, DHCPMessage *force,
1087 size_t len) {
1088 int r;
1089
1090 r = dhcp_option_parse(force, len, NULL, NULL);
1091 if (r != DHCP_FORCERENEW)
1092 return -ENOMSG;
1093
1094 log_dhcp_client(client, "FORCERENEW");
1095
1096 return 0;
1097 }
1098
1099 static int client_handle_ack(sd_dhcp_client *client, DHCPMessage *ack,
1100 size_t len) {
1101 _cleanup_dhcp_lease_unref_ sd_dhcp_lease *lease = NULL;
1102 int r;
1103
1104 r = dhcp_lease_new(&lease);
1105 if (r < 0)
1106 return r;
1107
1108 if (client->client_id_len) {
1109 r = dhcp_lease_set_client_id(lease,
1110 (uint8_t *) &client->client_id,
1111 client->client_id_len);
1112 if (r < 0)
1113 return r;
1114 }
1115
1116 r = dhcp_option_parse(ack, len, dhcp_lease_parse_options, lease);
1117 if (r == DHCP_NAK) {
1118 log_dhcp_client(client, "NAK");
1119 return -EADDRNOTAVAIL;
1120 }
1121
1122 if (r != DHCP_ACK) {
1123 log_dhcp_client(client, "received message was not an ACK, ignoring");
1124 return -ENOMSG;
1125 }
1126
1127 lease->next_server = ack->siaddr;
1128
1129 lease->address = ack->yiaddr;
1130
1131 if (lease->address == INADDR_ANY ||
1132 lease->server_address == INADDR_ANY ||
1133 lease->lifetime == 0) {
1134 log_dhcp_client(client, "received lease lacks address, server "
1135 "address or lease lifetime, ignoring");
1136 return -ENOMSG;
1137 }
1138
1139 if (lease->subnet_mask == INADDR_ANY) {
1140 r = dhcp_lease_set_default_subnet_mask(lease);
1141 if (r < 0) {
1142 log_dhcp_client(client, "received lease lacks subnet "
1143 "mask, and a fallback one can not be "
1144 "generated, ignoring");
1145 return -ENOMSG;
1146 }
1147 }
1148
1149 r = DHCP_EVENT_IP_ACQUIRE;
1150 if (client->lease) {
1151 if (client->lease->address != lease->address ||
1152 client->lease->subnet_mask != lease->subnet_mask ||
1153 client->lease->router != lease->router) {
1154 r = DHCP_EVENT_IP_CHANGE;
1155 } else
1156 r = DHCP_EVENT_RENEW;
1157
1158 client->lease = sd_dhcp_lease_unref(client->lease);
1159 }
1160
1161 client->lease = lease;
1162 lease = NULL;
1163
1164 log_dhcp_client(client, "ACK");
1165
1166 return r;
1167 }
1168
1169 static uint64_t client_compute_timeout(sd_dhcp_client *client,
1170 uint32_t lifetime, double factor) {
1171 assert(client);
1172 assert(client->request_sent);
1173 assert(lifetime);
1174
1175 return client->request_sent + ((lifetime - 3) * USEC_PER_SEC * factor) +
1176 + (random_u32() & 0x1fffff);
1177 }
1178
1179 static int client_set_lease_timeouts(sd_dhcp_client *client) {
1180 usec_t time_now;
1181 uint64_t lifetime_timeout;
1182 uint64_t t2_timeout;
1183 uint64_t t1_timeout;
1184 char time_string[FORMAT_TIMESPAN_MAX];
1185 int r;
1186
1187 assert(client);
1188 assert(client->event);
1189 assert(client->lease);
1190 assert(client->lease->lifetime);
1191
1192 client->timeout_t1 = sd_event_source_unref(client->timeout_t1);
1193 client->timeout_t2 = sd_event_source_unref(client->timeout_t2);
1194 client->timeout_expire = sd_event_source_unref(client->timeout_expire);
1195
1196 /* don't set timers for infinite leases */
1197 if (client->lease->lifetime == 0xffffffff)
1198 return 0;
1199
1200 r = sd_event_now(client->event, clock_boottime_or_monotonic(), &time_now);
1201 if (r < 0)
1202 return r;
1203 assert(client->request_sent <= time_now);
1204
1205 /* convert the various timeouts from relative (secs) to absolute (usecs) */
1206 lifetime_timeout = client_compute_timeout(client, client->lease->lifetime, 1);
1207 if (client->lease->t1 && client->lease->t2) {
1208 /* both T1 and T2 are given */
1209 if (client->lease->t1 < client->lease->t2 &&
1210 client->lease->t2 < client->lease->lifetime) {
1211 /* they are both valid */
1212 t2_timeout = client_compute_timeout(client, client->lease->t2, 1);
1213 t1_timeout = client_compute_timeout(client, client->lease->t1, 1);
1214 } else {
1215 /* discard both */
1216 t2_timeout = client_compute_timeout(client, client->lease->lifetime, 7.0 / 8.0);
1217 client->lease->t2 = (client->lease->lifetime * 7) / 8;
1218 t1_timeout = client_compute_timeout(client, client->lease->lifetime, 0.5);
1219 client->lease->t1 = client->lease->lifetime / 2;
1220 }
1221 } else if (client->lease->t2 && client->lease->t2 < client->lease->lifetime) {
1222 /* only T2 is given, and it is valid */
1223 t2_timeout = client_compute_timeout(client, client->lease->t2, 1);
1224 t1_timeout = client_compute_timeout(client, client->lease->lifetime, 0.5);
1225 client->lease->t1 = client->lease->lifetime / 2;
1226 if (t2_timeout <= t1_timeout) {
1227 /* the computed T1 would be invalid, so discard T2 */
1228 t2_timeout = client_compute_timeout(client, client->lease->lifetime, 7.0 / 8.0);
1229 client->lease->t2 = (client->lease->lifetime * 7) / 8;
1230 }
1231 } else if (client->lease->t1 && client->lease->t1 < client->lease->lifetime) {
1232 /* only T1 is given, and it is valid */
1233 t1_timeout = client_compute_timeout(client, client->lease->t1, 1);
1234 t2_timeout = client_compute_timeout(client, client->lease->lifetime, 7.0 / 8.0);
1235 client->lease->t2 = (client->lease->lifetime * 7) / 8;
1236 if (t2_timeout <= t1_timeout) {
1237 /* the computed T2 would be invalid, so discard T1 */
1238 t2_timeout = client_compute_timeout(client, client->lease->lifetime, 0.5);
1239 client->lease->t2 = client->lease->lifetime / 2;
1240 }
1241 } else {
1242 /* fall back to the default timeouts */
1243 t1_timeout = client_compute_timeout(client, client->lease->lifetime, 0.5);
1244 client->lease->t1 = client->lease->lifetime / 2;
1245 t2_timeout = client_compute_timeout(client, client->lease->lifetime, 7.0 / 8.0);
1246 client->lease->t2 = (client->lease->lifetime * 7) / 8;
1247 }
1248
1249 /* arm lifetime timeout */
1250 r = sd_event_add_time(client->event, &client->timeout_expire,
1251 clock_boottime_or_monotonic(),
1252 lifetime_timeout, 10 * USEC_PER_MSEC,
1253 client_timeout_expire, client);
1254 if (r < 0)
1255 return r;
1256
1257 r = sd_event_source_set_priority(client->timeout_expire,
1258 client->event_priority);
1259 if (r < 0)
1260 return r;
1261
1262 r = sd_event_source_set_description(client->timeout_expire, "dhcp4-lifetime");
1263 if (r < 0)
1264 return r;
1265
1266 log_dhcp_client(client, "lease expires in %s",
1267 format_timespan(time_string, FORMAT_TIMESPAN_MAX,
1268 lifetime_timeout - time_now, 0));
1269
1270 /* don't arm earlier timeouts if this has already expired */
1271 if (lifetime_timeout <= time_now)
1272 return 0;
1273
1274 /* arm T2 timeout */
1275 r = sd_event_add_time(client->event,
1276 &client->timeout_t2,
1277 clock_boottime_or_monotonic(),
1278 t2_timeout,
1279 10 * USEC_PER_MSEC,
1280 client_timeout_t2, client);
1281 if (r < 0)
1282 return r;
1283
1284 r = sd_event_source_set_priority(client->timeout_t2,
1285 client->event_priority);
1286 if (r < 0)
1287 return r;
1288
1289 r = sd_event_source_set_description(client->timeout_t2, "dhcp4-t2-timeout");
1290 if (r < 0)
1291 return r;
1292
1293 log_dhcp_client(client, "T2 expires in %s",
1294 format_timespan(time_string, FORMAT_TIMESPAN_MAX,
1295 t2_timeout - time_now, 0));
1296
1297 /* don't arm earlier timeout if this has already expired */
1298 if (t2_timeout <= time_now)
1299 return 0;
1300
1301 /* arm T1 timeout */
1302 r = sd_event_add_time(client->event,
1303 &client->timeout_t1,
1304 clock_boottime_or_monotonic(),
1305 t1_timeout, 10 * USEC_PER_MSEC,
1306 client_timeout_t1, client);
1307 if (r < 0)
1308 return r;
1309
1310 r = sd_event_source_set_priority(client->timeout_t1,
1311 client->event_priority);
1312 if (r < 0)
1313 return r;
1314
1315 r = sd_event_source_set_description(client->timeout_t1, "dhcp4-t1-timer");
1316 if (r < 0)
1317 return r;
1318
1319 log_dhcp_client(client, "T1 expires in %s",
1320 format_timespan(time_string, FORMAT_TIMESPAN_MAX,
1321 t1_timeout - time_now, 0));
1322
1323 return 0;
1324 }
1325
1326 static int client_handle_message(sd_dhcp_client *client, DHCPMessage *message,
1327 int len) {
1328 DHCP_CLIENT_DONT_DESTROY(client);
1329 int r = 0, notify_event = 0;
1330
1331 assert(client);
1332 assert(client->event);
1333 assert(message);
1334
1335 switch (client->state) {
1336 case DHCP_STATE_SELECTING:
1337
1338 r = client_handle_offer(client, message, len);
1339 if (r >= 0) {
1340
1341 client->timeout_resend =
1342 sd_event_source_unref(client->timeout_resend);
1343
1344 client->state = DHCP_STATE_REQUESTING;
1345 client->attempt = 1;
1346
1347 r = sd_event_add_time(client->event,
1348 &client->timeout_resend,
1349 clock_boottime_or_monotonic(),
1350 0, 0,
1351 client_timeout_resend, client);
1352 if (r < 0)
1353 goto error;
1354
1355 r = sd_event_source_set_priority(client->timeout_resend,
1356 client->event_priority);
1357 if (r < 0)
1358 goto error;
1359
1360 r = sd_event_source_set_description(client->timeout_resend, "dhcp4-resend-timer");
1361 if (r < 0)
1362 goto error;
1363 } else if (r == -ENOMSG)
1364 /* invalid message, let's ignore it */
1365 return 0;
1366
1367 break;
1368
1369 case DHCP_STATE_REBOOTING:
1370 case DHCP_STATE_REQUESTING:
1371 case DHCP_STATE_RENEWING:
1372 case DHCP_STATE_REBINDING:
1373
1374 r = client_handle_ack(client, message, len);
1375 if (r >= 0) {
1376 client->timeout_resend =
1377 sd_event_source_unref(client->timeout_resend);
1378 client->receive_message =
1379 sd_event_source_unref(client->receive_message);
1380 client->fd = asynchronous_close(client->fd);
1381
1382 if (IN_SET(client->state, DHCP_STATE_REQUESTING,
1383 DHCP_STATE_REBOOTING))
1384 notify_event = DHCP_EVENT_IP_ACQUIRE;
1385 else if (r != DHCP_EVENT_IP_ACQUIRE)
1386 notify_event = r;
1387
1388 client->state = DHCP_STATE_BOUND;
1389 client->attempt = 1;
1390
1391 client->last_addr = client->lease->address;
1392
1393 r = client_set_lease_timeouts(client);
1394 if (r < 0) {
1395 log_dhcp_client(client, "could not set lease timeouts");
1396 goto error;
1397 }
1398
1399 r = dhcp_network_bind_udp_socket(client->lease->address,
1400 DHCP_PORT_CLIENT);
1401 if (r < 0) {
1402 log_dhcp_client(client, "could not bind UDP socket");
1403 goto error;
1404 }
1405
1406 client->fd = r;
1407
1408 client_initialize_io_events(client, client_receive_message_udp);
1409
1410 if (notify_event) {
1411 client_notify(client, notify_event);
1412 if (client->state == DHCP_STATE_STOPPED)
1413 return 0;
1414 }
1415
1416 } else if (r == -EADDRNOTAVAIL) {
1417 /* got a NAK, let's restart the client */
1418 client->timeout_resend =
1419 sd_event_source_unref(client->timeout_resend);
1420
1421 r = client_initialize(client);
1422 if (r < 0)
1423 goto error;
1424
1425 r = client_start(client);
1426 if (r < 0)
1427 goto error;
1428
1429 log_dhcp_client(client, "REBOOTED");
1430
1431 return 0;
1432 } else if (r == -ENOMSG)
1433 /* invalid message, let's ignore it */
1434 return 0;
1435
1436 break;
1437
1438 case DHCP_STATE_BOUND:
1439 r = client_handle_forcerenew(client, message, len);
1440 if (r >= 0) {
1441 r = client_timeout_t1(NULL, 0, client);
1442 if (r < 0)
1443 goto error;
1444 } else if (r == -ENOMSG)
1445 /* invalid message, let's ignore it */
1446 return 0;
1447
1448 break;
1449
1450 case DHCP_STATE_INIT:
1451 case DHCP_STATE_INIT_REBOOT:
1452
1453 break;
1454
1455 case DHCP_STATE_STOPPED:
1456 r = -EINVAL;
1457 goto error;
1458 }
1459
1460 error:
1461 if (r < 0)
1462 client_stop(client, r);
1463
1464 return r;
1465 }
1466
1467 static int client_receive_message_udp(sd_event_source *s, int fd,
1468 uint32_t revents, void *userdata) {
1469 sd_dhcp_client *client = userdata;
1470 _cleanup_free_ DHCPMessage *message = NULL;
1471 int buflen = 0, len, r;
1472 const struct ether_addr zero_mac = { { 0, 0, 0, 0, 0, 0 } };
1473 const struct ether_addr *expected_chaddr = NULL;
1474 uint8_t expected_hlen = 0;
1475
1476 assert(s);
1477 assert(client);
1478
1479 r = ioctl(fd, FIONREAD, &buflen);
1480 if (r < 0)
1481 return r;
1482
1483 if (buflen < 0)
1484 /* this can't be right */
1485 return -EIO;
1486
1487 message = malloc0(buflen);
1488 if (!message)
1489 return -ENOMEM;
1490
1491 len = read(fd, message, buflen);
1492 if (len < 0) {
1493 log_dhcp_client(client, "could not receive message from UDP "
1494 "socket: %m");
1495 return 0;
1496 } else if ((size_t)len < sizeof(DHCPMessage)) {
1497 log_dhcp_client(client, "too small to be a DHCP message: ignoring");
1498 return 0;
1499 }
1500
1501 if (be32toh(message->magic) != DHCP_MAGIC_COOKIE) {
1502 log_dhcp_client(client, "not a DHCP message: ignoring");
1503 return 0;
1504 }
1505
1506 if (message->op != BOOTREPLY) {
1507 log_dhcp_client(client, "not a BOOTREPLY message: ignoring");
1508 return 0;
1509 }
1510
1511 if (message->htype != client->arp_type) {
1512 log_dhcp_client(client, "packet type does not match client type");
1513 return 0;
1514 }
1515
1516 if (client->arp_type == ARPHRD_ETHER) {
1517 expected_hlen = ETH_ALEN;
1518 expected_chaddr = (const struct ether_addr *) &client->mac_addr;
1519 } else {
1520 /* Non-ethernet links expect zero chaddr */
1521 expected_hlen = 0;
1522 expected_chaddr = &zero_mac;
1523 }
1524
1525 if (message->hlen != expected_hlen) {
1526 log_dhcp_client(client, "unexpected packet hlen %d", message->hlen);
1527 return 0;
1528 }
1529
1530 if (memcmp(&message->chaddr[0], expected_chaddr, ETH_ALEN)) {
1531 log_dhcp_client(client, "received chaddr does not match "
1532 "expected: ignoring");
1533 return 0;
1534 }
1535
1536 if (client->state != DHCP_STATE_BOUND &&
1537 be32toh(message->xid) != client->xid) {
1538 /* in BOUND state, we may receive FORCERENEW with xid set by server,
1539 so ignore the xid in this case */
1540 log_dhcp_client(client, "received xid (%u) does not match "
1541 "expected (%u): ignoring",
1542 be32toh(message->xid), client->xid);
1543 return 0;
1544 }
1545
1546 return client_handle_message(client, message, len);
1547 }
1548
1549 static int client_receive_message_raw(sd_event_source *s, int fd,
1550 uint32_t revents, void *userdata) {
1551 sd_dhcp_client *client = userdata;
1552 _cleanup_free_ DHCPPacket *packet = NULL;
1553 uint8_t cmsgbuf[CMSG_LEN(sizeof(struct tpacket_auxdata))];
1554 struct iovec iov = {};
1555 struct msghdr msg = {
1556 .msg_iov = &iov,
1557 .msg_iovlen = 1,
1558 .msg_control = cmsgbuf,
1559 .msg_controllen = sizeof(cmsgbuf),
1560 };
1561 struct cmsghdr *cmsg;
1562 bool checksum = true;
1563 int buflen = 0, len, r;
1564
1565 assert(s);
1566 assert(client);
1567
1568 r = ioctl(fd, FIONREAD, &buflen);
1569 if (r < 0)
1570 return r;
1571
1572 if (buflen < 0)
1573 /* this can't be right */
1574 return -EIO;
1575
1576 packet = malloc0(buflen);
1577 if (!packet)
1578 return -ENOMEM;
1579
1580 iov.iov_base = packet;
1581 iov.iov_len = buflen;
1582
1583 len = recvmsg(fd, &msg, 0);
1584 if (len < 0) {
1585 log_dhcp_client(client, "could not receive message from raw "
1586 "socket: %m");
1587 return 0;
1588 } else if ((size_t)len < sizeof(DHCPPacket))
1589 return 0;
1590
1591 CMSG_FOREACH(cmsg, &msg) {
1592 if (cmsg->cmsg_level == SOL_PACKET &&
1593 cmsg->cmsg_type == PACKET_AUXDATA &&
1594 cmsg->cmsg_len == CMSG_LEN(sizeof(struct tpacket_auxdata))) {
1595 struct tpacket_auxdata *aux = (struct tpacket_auxdata*)CMSG_DATA(cmsg);
1596
1597 checksum = !(aux->tp_status & TP_STATUS_CSUMNOTREADY);
1598 break;
1599 }
1600 }
1601
1602 r = dhcp_packet_verify_headers(packet, len, checksum);
1603 if (r < 0)
1604 return 0;
1605
1606 len -= DHCP_IP_UDP_SIZE;
1607
1608 return client_handle_message(client, &packet->dhcp, len);
1609 }
1610
1611 int sd_dhcp_client_start(sd_dhcp_client *client) {
1612 int r;
1613
1614 assert_return(client, -EINVAL);
1615
1616 r = client_initialize(client);
1617 if (r < 0)
1618 return r;
1619
1620 if (client->last_addr)
1621 client->state = DHCP_STATE_INIT_REBOOT;
1622
1623 r = client_start(client);
1624 if (r >= 0)
1625 log_dhcp_client(client, "STARTED on ifindex %i", client->index);
1626
1627 return r;
1628 }
1629
1630 int sd_dhcp_client_stop(sd_dhcp_client *client) {
1631 DHCP_CLIENT_DONT_DESTROY(client);
1632
1633 assert_return(client, -EINVAL);
1634
1635 client_stop(client, DHCP_EVENT_STOP);
1636 client->state = DHCP_STATE_STOPPED;
1637
1638 return 0;
1639 }
1640
1641 int sd_dhcp_client_attach_event(sd_dhcp_client *client, sd_event *event,
1642 int priority) {
1643 int r;
1644
1645 assert_return(client, -EINVAL);
1646 assert_return(!client->event, -EBUSY);
1647
1648 if (event)
1649 client->event = sd_event_ref(event);
1650 else {
1651 r = sd_event_default(&client->event);
1652 if (r < 0)
1653 return 0;
1654 }
1655
1656 client->event_priority = priority;
1657
1658 return 0;
1659 }
1660
1661 int sd_dhcp_client_detach_event(sd_dhcp_client *client) {
1662 assert_return(client, -EINVAL);
1663
1664 client->event = sd_event_unref(client->event);
1665
1666 return 0;
1667 }
1668
1669 sd_event *sd_dhcp_client_get_event(sd_dhcp_client *client) {
1670 if (!client)
1671 return NULL;
1672
1673 return client->event;
1674 }
1675
1676 sd_dhcp_client *sd_dhcp_client_ref(sd_dhcp_client *client) {
1677 if (client)
1678 assert_se(REFCNT_INC(client->n_ref) >= 2);
1679
1680 return client;
1681 }
1682
1683 sd_dhcp_client *sd_dhcp_client_unref(sd_dhcp_client *client) {
1684 if (client && REFCNT_DEC(client->n_ref) == 0) {
1685 log_dhcp_client(client, "FREE");
1686
1687 client_initialize(client);
1688
1689 client->receive_message =
1690 sd_event_source_unref(client->receive_message);
1691
1692 sd_dhcp_client_detach_event(client);
1693
1694 sd_dhcp_lease_unref(client->lease);
1695
1696 free(client->req_opts);
1697 free(client->hostname);
1698 free(client->vendor_class_identifier);
1699 free(client);
1700 }
1701
1702 return NULL;
1703 }
1704
1705 int sd_dhcp_client_new(sd_dhcp_client **ret) {
1706 _cleanup_dhcp_client_unref_ sd_dhcp_client *client = NULL;
1707
1708 assert_return(ret, -EINVAL);
1709
1710 client = new0(sd_dhcp_client, 1);
1711 if (!client)
1712 return -ENOMEM;
1713
1714 client->n_ref = REFCNT_INIT;
1715 client->state = DHCP_STATE_INIT;
1716 client->index = -1;
1717 client->fd = -1;
1718 client->attempt = 1;
1719 client->mtu = DHCP_DEFAULT_MIN_SIZE;
1720
1721 client->req_opts_size = ELEMENTSOF(default_req_opts);
1722
1723 client->req_opts = memdup(default_req_opts, client->req_opts_size);
1724 if (!client->req_opts)
1725 return -ENOMEM;
1726
1727 *ret = client;
1728 client = NULL;
1729
1730 return 0;
1731 }