]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/libsystemd/sd-dhcp-client.c
sd-dhcp-client: minor style fixes
[thirdparty/systemd.git] / src / libsystemd / 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 <sys/param.h>
26
27 #include "util.h"
28 #include "list.h"
29
30 #include "dhcp-protocol.h"
31 #include "dhcp-internal.h"
32 #include "sd-dhcp-client.h"
33
34 #define DHCP_CLIENT_MIN_OPTIONS_SIZE 312
35
36 struct DHCPLease {
37 uint32_t t1;
38 uint32_t t2;
39 uint32_t lifetime;
40 be32_t address;
41 be32_t server_address;
42 be32_t subnet_mask;
43 be32_t router;
44 struct in_addr *dns;
45 size_t dns_size;
46 uint16_t mtu;
47 char *hostname;
48 };
49
50 typedef struct DHCPLease DHCPLease;
51
52 struct sd_dhcp_client {
53 DHCPState state;
54 sd_event *event;
55 sd_event_source *timeout_resend;
56 int index;
57 int fd;
58 union sockaddr_union link;
59 sd_event_source *receive_message;
60 uint8_t *req_opts;
61 size_t req_opts_allocated;
62 size_t req_opts_size;
63 be32_t last_addr;
64 struct ether_addr mac_addr;
65 uint32_t xid;
66 usec_t start_time;
67 unsigned int attempt;
68 usec_t request_sent;
69 sd_event_source *timeout_t1;
70 sd_event_source *timeout_t2;
71 sd_event_source *timeout_expire;
72 sd_dhcp_client_cb_t cb;
73 void *userdata;
74 DHCPLease *lease;
75 };
76
77 static const uint8_t default_req_opts[] = {
78 DHCP_OPTION_SUBNET_MASK,
79 DHCP_OPTION_ROUTER,
80 DHCP_OPTION_HOST_NAME,
81 DHCP_OPTION_DOMAIN_NAME,
82 DHCP_OPTION_DOMAIN_NAME_SERVER,
83 DHCP_OPTION_NTP_SERVER,
84 };
85
86 static int client_receive_message(sd_event_source *s, int fd,
87 uint32_t revents, void *userdata);
88
89 int sd_dhcp_client_set_callback(sd_dhcp_client *client, sd_dhcp_client_cb_t cb,
90 void *userdata) {
91 assert_return(client, -EINVAL);
92
93 client->cb = cb;
94 client->userdata = userdata;
95
96 return 0;
97 }
98
99 int sd_dhcp_client_set_request_option(sd_dhcp_client *client, uint8_t option) {
100 size_t i;
101
102 assert_return(client, -EINVAL);
103 assert_return (client->state == DHCP_STATE_INIT, -EBUSY);
104
105 switch(option) {
106 case DHCP_OPTION_PAD:
107 case DHCP_OPTION_OVERLOAD:
108 case DHCP_OPTION_MESSAGE_TYPE:
109 case DHCP_OPTION_PARAMETER_REQUEST_LIST:
110 case DHCP_OPTION_END:
111 return -EINVAL;
112
113 default:
114 break;
115 }
116
117 for (i = 0; i < client->req_opts_size; i++)
118 if (client->req_opts[i] == option)
119 return -EEXIST;
120
121 if (!GREEDY_REALLOC(client->req_opts, client->req_opts_allocated,
122 client->req_opts_size + 1))
123 return -ENOMEM;
124
125 client->req_opts[client->req_opts_size++] = option;
126
127 return 0;
128 }
129
130 int sd_dhcp_client_set_request_address(sd_dhcp_client *client,
131 const struct in_addr *last_addr) {
132 assert_return(client, -EINVAL);
133 assert_return(client->state == DHCP_STATE_INIT, -EBUSY);
134
135 if (last_addr)
136 client->last_addr = last_addr->s_addr;
137 else
138 client->last_addr = INADDR_ANY;
139
140 return 0;
141 }
142
143 int sd_dhcp_client_set_index(sd_dhcp_client *client, int interface_index) {
144 assert_return(client, -EINVAL);
145 assert_return(client->state == DHCP_STATE_INIT, -EBUSY);
146 assert_return(interface_index >= -1, -EINVAL);
147
148 client->index = interface_index;
149
150 return 0;
151 }
152
153 int sd_dhcp_client_set_mac(sd_dhcp_client *client,
154 const struct ether_addr *addr) {
155 assert_return(client, -EINVAL);
156 assert_return(client->state == DHCP_STATE_INIT, -EBUSY);
157
158 memcpy(&client->mac_addr, addr, ETH_ALEN);
159
160 return 0;
161 }
162
163 int sd_dhcp_client_get_address(sd_dhcp_client *client, struct in_addr *addr) {
164 assert_return(client, -EINVAL);
165 assert_return(addr, -EINVAL);
166
167 switch (client->state) {
168 case DHCP_STATE_INIT:
169 case DHCP_STATE_SELECTING:
170 case DHCP_STATE_INIT_REBOOT:
171 case DHCP_STATE_REBOOTING:
172 case DHCP_STATE_REQUESTING:
173 return -EADDRNOTAVAIL;
174
175 case DHCP_STATE_BOUND:
176 case DHCP_STATE_RENEWING:
177 case DHCP_STATE_REBINDING:
178 addr->s_addr = client->lease->address;
179
180 break;
181 }
182
183 return 0;
184 }
185
186 int sd_dhcp_client_get_mtu(sd_dhcp_client *client, uint16_t *mtu) {
187 assert_return(client, -EINVAL);
188 assert_return(mtu, -EINVAL);
189
190 switch (client->state) {
191 case DHCP_STATE_INIT:
192 case DHCP_STATE_SELECTING:
193 case DHCP_STATE_INIT_REBOOT:
194 case DHCP_STATE_REBOOTING:
195 case DHCP_STATE_REQUESTING:
196 return -EADDRNOTAVAIL;
197
198 case DHCP_STATE_BOUND:
199 case DHCP_STATE_RENEWING:
200 case DHCP_STATE_REBINDING:
201 if (client->lease->mtu)
202 *mtu = client->lease->mtu;
203 else
204 return -ENOENT;
205
206 break;
207 }
208
209 return 0;
210 }
211
212 int sd_dhcp_client_get_dns(sd_dhcp_client *client, struct in_addr **addr, size_t *addr_size) {
213 assert_return(client, -EINVAL);
214 assert_return(addr, -EINVAL);
215 assert_return(addr_size, -EINVAL);
216
217 switch (client->state) {
218 case DHCP_STATE_INIT:
219 case DHCP_STATE_SELECTING:
220 case DHCP_STATE_INIT_REBOOT:
221 case DHCP_STATE_REBOOTING:
222 case DHCP_STATE_REQUESTING:
223 return -EADDRNOTAVAIL;
224
225 case DHCP_STATE_BOUND:
226 case DHCP_STATE_RENEWING:
227 case DHCP_STATE_REBINDING:
228 if (client->lease->dns_size) {
229 *addr_size = client->lease->dns_size;
230 *addr = client->lease->dns;
231 } else
232 return -ENOENT;
233
234 break;
235 }
236
237 return 0;
238 }
239
240 int sd_dhcp_client_get_hostname(sd_dhcp_client *client, const char **hostname) {
241 assert_return(client, -EINVAL);
242 assert_return(hostname, -EINVAL);
243
244 switch (client->state) {
245 case DHCP_STATE_INIT:
246 case DHCP_STATE_SELECTING:
247 case DHCP_STATE_INIT_REBOOT:
248 case DHCP_STATE_REBOOTING:
249 case DHCP_STATE_REQUESTING:
250 return -EADDRNOTAVAIL;
251
252 case DHCP_STATE_BOUND:
253 case DHCP_STATE_RENEWING:
254 case DHCP_STATE_REBINDING:
255 if (client->lease->hostname)
256 *hostname = client->lease->hostname;
257 else
258 return -ENOENT;
259
260 break;
261 }
262
263 return 0;
264 }
265
266 int sd_dhcp_client_prefixlen(const struct in_addr *addr) {
267 int len = 0;
268 uint32_t mask;
269
270 assert_return(addr, -EADDRNOTAVAIL);
271
272 mask = be32toh(addr->s_addr);
273 while (mask) {
274 len++;
275 mask = mask << 1;
276 }
277
278 return len;
279 }
280
281 int sd_dhcp_client_get_router(sd_dhcp_client *client, struct in_addr *addr) {
282 assert_return(client, -EINVAL);
283 assert_return(addr, -EINVAL);
284
285 switch (client->state) {
286 case DHCP_STATE_INIT:
287 case DHCP_STATE_SELECTING:
288 case DHCP_STATE_INIT_REBOOT:
289 case DHCP_STATE_REBOOTING:
290 case DHCP_STATE_REQUESTING:
291 return -EADDRNOTAVAIL;
292
293 case DHCP_STATE_BOUND:
294 case DHCP_STATE_RENEWING:
295 case DHCP_STATE_REBINDING:
296 addr->s_addr = client->lease->router;
297
298 break;
299 }
300
301 return 0;
302 }
303
304 int sd_dhcp_client_get_netmask(sd_dhcp_client *client, struct in_addr *addr) {
305 assert_return(client, -EINVAL);
306 assert_return(addr, -EINVAL);
307
308 switch (client->state) {
309 case DHCP_STATE_INIT:
310 case DHCP_STATE_SELECTING:
311 case DHCP_STATE_INIT_REBOOT:
312 case DHCP_STATE_REBOOTING:
313 case DHCP_STATE_REQUESTING:
314 return -EADDRNOTAVAIL;
315
316 case DHCP_STATE_BOUND:
317 case DHCP_STATE_RENEWING:
318 case DHCP_STATE_REBINDING:
319 addr->s_addr = client->lease->subnet_mask;
320
321 break;
322 }
323
324 return 0;
325 }
326
327 static int client_notify(sd_dhcp_client *client, int event) {
328 if (client->cb)
329 client->cb(client, event, client->userdata);
330
331 return 0;
332 }
333
334 static void lease_free(DHCPLease *lease) {
335 if (!lease)
336 return;
337
338 free(lease->hostname);
339 free(lease->dns);
340 free(lease);
341 }
342
343 DEFINE_TRIVIAL_CLEANUP_FUNC(DHCPLease*, lease_free);
344 #define _cleanup_lease_free_ _cleanup_(lease_freep)
345
346 static int client_stop(sd_dhcp_client *client, int error) {
347 assert_return(client, -EINVAL);
348
349 client->receive_message =
350 sd_event_source_unref(client->receive_message);
351
352 if (client->fd >= 0)
353 close(client->fd);
354 client->fd = -1;
355
356 client->timeout_resend = sd_event_source_unref(client->timeout_resend);
357
358 client->timeout_t1 = sd_event_source_unref(client->timeout_t1);
359 client->timeout_t2 = sd_event_source_unref(client->timeout_t2);
360 client->timeout_expire = sd_event_source_unref(client->timeout_expire);
361
362 client->attempt = 1;
363
364 client_notify(client, error);
365
366 switch (client->state) {
367
368 case DHCP_STATE_INIT:
369 case DHCP_STATE_SELECTING:
370 case DHCP_STATE_REQUESTING:
371 case DHCP_STATE_BOUND:
372
373 client->start_time = 0;
374 client->state = DHCP_STATE_INIT;
375 break;
376
377 case DHCP_STATE_INIT_REBOOT:
378 case DHCP_STATE_REBOOTING:
379 case DHCP_STATE_RENEWING:
380 case DHCP_STATE_REBINDING:
381
382 break;
383 }
384
385 if (client->lease) {
386 lease_free(client->lease);
387 client->lease = NULL;
388 }
389
390 return 0;
391 }
392
393 static int client_packet_init(sd_dhcp_client *client, uint8_t type,
394 DHCPMessage *message, uint16_t secs,
395 uint8_t **opt, size_t *optlen) {
396 int err;
397 be16_t max_size;
398
399 *opt = (uint8_t *)(message + 1);
400
401 if (*optlen < 4)
402 return -ENOBUFS;
403 *optlen -= 4;
404
405 message->op = BOOTREQUEST;
406 message->htype = 1;
407 message->hlen = ETHER_ADDR_LEN;
408 message->xid = htobe32(client->xid);
409
410 /* Although 'secs' field is a SHOULD in RFC 2131, certain DHCP servers
411 refuse to issue an DHCP lease if 'secs' is set to zero */
412 message->secs = htobe16(secs);
413
414 if (client->state == DHCP_STATE_RENEWING ||
415 client->state == DHCP_STATE_REBINDING)
416 message->ciaddr = client->lease->address;
417
418 memcpy(&message->chaddr, &client->mac_addr, ETH_ALEN);
419 (*opt)[0] = 0x63;
420 (*opt)[1] = 0x82;
421 (*opt)[2] = 0x53;
422 (*opt)[3] = 0x63;
423
424 *opt += 4;
425
426 err = dhcp_option_append(opt, optlen, DHCP_OPTION_MESSAGE_TYPE, 1,
427 &type);
428 if (err < 0)
429 return err;
430
431 /* Some DHCP servers will refuse to issue an DHCP lease if the Cliient
432 Identifier option is not set */
433 err = dhcp_option_append(opt, optlen, DHCP_OPTION_CLIENT_IDENTIFIER,
434 ETH_ALEN, &client->mac_addr);
435 if (err < 0)
436 return err;
437
438 if (type == DHCP_DISCOVER || type == DHCP_REQUEST) {
439 err = dhcp_option_append(opt, optlen,
440 DHCP_OPTION_PARAMETER_REQUEST_LIST,
441 client->req_opts_size,
442 client->req_opts);
443 if (err < 0)
444 return err;
445
446 /* Some DHCP servers will send bigger DHCP packets than the
447 defined default size unless the Maximum Messge Size option
448 is explicitely set */
449 max_size = htobe16(DHCP_IP_UDP_SIZE + DHCP_MESSAGE_SIZE +
450 DHCP_CLIENT_MIN_OPTIONS_SIZE);
451 err = dhcp_option_append(opt, optlen,
452 DHCP_OPTION_MAXIMUM_MESSAGE_SIZE,
453 2, &max_size);
454 if (err < 0)
455 return err;
456 }
457
458 return 0;
459 }
460
461 static uint16_t client_checksum(void *buf, int len) {
462 uint32_t sum;
463 uint16_t *check;
464 int i;
465 uint8_t *odd;
466
467 sum = 0;
468 check = buf;
469
470 for (i = 0; i < len / 2 ; i++)
471 sum += check[i];
472
473 if (len & 0x01) {
474 odd = buf;
475 sum += odd[len - 1];
476 }
477
478 while (sum >> 16)
479 sum = (sum & 0xffff) + (sum >> 16);
480
481 return ~sum;
482 }
483
484 static void client_append_ip_headers(DHCPPacket *packet, uint16_t len) {
485 packet->ip.version = IPVERSION;
486 packet->ip.ihl = DHCP_IP_SIZE / 4;
487 packet->ip.tot_len = htobe16(len);
488
489 packet->ip.protocol = IPPROTO_UDP;
490 packet->ip.saddr = INADDR_ANY;
491 packet->ip.daddr = INADDR_BROADCAST;
492
493 packet->udp.source = htobe16(DHCP_PORT_CLIENT);
494 packet->udp.dest = htobe16(DHCP_PORT_SERVER);
495 packet->udp.len = htobe16(len - DHCP_IP_SIZE);
496
497 packet->ip.check = packet->udp.len;
498 packet->udp.check = client_checksum(&packet->ip.ttl, len - 8);
499
500 packet->ip.ttl = IPDEFTTL;
501 packet->ip.check = 0;
502 packet->ip.check = client_checksum(&packet->ip, DHCP_IP_SIZE);
503 }
504
505 static int client_send_discover(sd_dhcp_client *client, uint16_t secs) {
506 int err = 0;
507 _cleanup_free_ DHCPPacket *discover;
508 size_t optlen, len;
509 uint8_t *opt;
510
511 optlen = DHCP_CLIENT_MIN_OPTIONS_SIZE;
512 len = sizeof(DHCPPacket) + optlen;
513
514 discover = malloc0(len);
515
516 if (!discover)
517 return -ENOMEM;
518
519 err = client_packet_init(client, DHCP_DISCOVER, &discover->dhcp,
520 secs, &opt, &optlen);
521 if (err < 0)
522 return err;
523
524 if (client->last_addr != INADDR_ANY) {
525 err = dhcp_option_append(&opt, &optlen,
526 DHCP_OPTION_REQUESTED_IP_ADDRESS,
527 4, &client->last_addr);
528 if (err < 0)
529 return err;
530 }
531
532 err = dhcp_option_append(&opt, &optlen, DHCP_OPTION_END, 0, NULL);
533 if (err < 0)
534 return err;
535
536 client_append_ip_headers(discover, len);
537
538 err = dhcp_network_send_raw_socket(client->fd, &client->link,
539 discover, len);
540
541 return err;
542 }
543
544 static int client_send_request(sd_dhcp_client *client, uint16_t secs) {
545 _cleanup_free_ DHCPPacket *request;
546 size_t optlen, len;
547 int err;
548 uint8_t *opt;
549
550 optlen = DHCP_CLIENT_MIN_OPTIONS_SIZE;
551 len = DHCP_MESSAGE_SIZE + optlen;
552
553 request = malloc0(len);
554 if (!request)
555 return -ENOMEM;
556
557 err = client_packet_init(client, DHCP_REQUEST, &request->dhcp, secs,
558 &opt, &optlen);
559 if (err < 0)
560 return err;
561
562 if (client->state == DHCP_STATE_REQUESTING) {
563 err = dhcp_option_append(&opt, &optlen,
564 DHCP_OPTION_REQUESTED_IP_ADDRESS,
565 4, &client->lease->address);
566 if (err < 0)
567 return err;
568
569 err = dhcp_option_append(&opt, &optlen,
570 DHCP_OPTION_SERVER_IDENTIFIER,
571 4, &client->lease->server_address);
572 if (err < 0)
573 return err;
574 }
575
576 err = dhcp_option_append(&opt, &optlen, DHCP_OPTION_END, 0, NULL);
577 if (err < 0)
578 return err;
579
580 if (client->state == DHCP_STATE_RENEWING) {
581 err = dhcp_network_send_udp_socket(client->fd,
582 client->lease->server_address,
583 &request->dhcp,
584 len - DHCP_IP_UDP_SIZE);
585 } else {
586 client_append_ip_headers(request, len);
587
588 err = dhcp_network_send_raw_socket(client->fd, &client->link,
589 request, len);
590 }
591
592 return err;
593 }
594
595 static int client_timeout_resend(sd_event_source *s, uint64_t usec,
596 void *userdata) {
597 sd_dhcp_client *client = userdata;
598 usec_t next_timeout = 0;
599 uint32_t time_left;
600 uint16_t secs;
601 int err = 0;
602
603 switch (client->state) {
604 case DHCP_STATE_RENEWING:
605
606 time_left = (client->lease->t2 - client->lease->t1)/2;
607 if (time_left < 60)
608 time_left = 60;
609
610 next_timeout = usec + time_left * USEC_PER_SEC;
611
612 break;
613
614 case DHCP_STATE_REBINDING:
615
616 time_left = (client->lease->lifetime - client->lease->t2)/2;
617 if (time_left < 60)
618 time_left = 60;
619
620 next_timeout = usec + time_left * USEC_PER_SEC;
621 break;
622
623 case DHCP_STATE_INIT:
624 case DHCP_STATE_INIT_REBOOT:
625 case DHCP_STATE_REBOOTING:
626 case DHCP_STATE_SELECTING:
627 case DHCP_STATE_REQUESTING:
628 case DHCP_STATE_BOUND:
629
630 if (client->attempt < 64)
631 client->attempt *= 2;
632
633 next_timeout = usec + (client->attempt - 1) * USEC_PER_SEC;
634
635 break;
636 }
637
638 next_timeout += (random_u32() & 0x1fffff);
639
640 err = sd_event_add_monotonic(client->event, next_timeout,
641 10 * USEC_PER_MSEC,
642 client_timeout_resend, client,
643 &client->timeout_resend);
644 if (err < 0)
645 goto error;
646
647 secs = (usec - client->start_time) / USEC_PER_SEC;
648
649 switch (client->state) {
650 case DHCP_STATE_INIT:
651 err = client_send_discover(client, secs);
652 if (err >= 0) {
653 client->state = DHCP_STATE_SELECTING;
654 client->attempt = 1;
655 } else {
656 if (client->attempt >= 64)
657 goto error;
658 }
659
660 break;
661
662 case DHCP_STATE_SELECTING:
663 err = client_send_discover(client, secs);
664 if (err < 0 && client->attempt >= 64)
665 goto error;
666
667 break;
668
669 case DHCP_STATE_REQUESTING:
670 case DHCP_STATE_RENEWING:
671 case DHCP_STATE_REBINDING:
672 err = client_send_request(client, secs);
673 if (err < 0 && client->attempt >= 64)
674 goto error;
675
676 client->request_sent = usec;
677
678 break;
679
680 case DHCP_STATE_INIT_REBOOT:
681 case DHCP_STATE_REBOOTING:
682 case DHCP_STATE_BOUND:
683
684 break;
685 }
686
687 return 0;
688
689 error:
690 client_stop(client, err);
691
692 /* Errors were dealt with when stopping the client, don't spill
693 errors into the event loop handler */
694 return 0;
695 }
696
697 static int client_initialize_events(sd_dhcp_client *client, usec_t usec) {
698 int r;
699
700 r = sd_event_add_io(client->event, client->fd, EPOLLIN,
701 client_receive_message, client,
702 &client->receive_message);
703 if (r < 0)
704 goto error;
705
706 r = sd_event_add_monotonic(client->event, usec, 0,
707 client_timeout_resend, client,
708 &client->timeout_resend);
709
710 error:
711 if (r < 0)
712 client_stop(client, r);
713
714 return 0;
715
716 }
717
718 static int client_timeout_expire(sd_event_source *s, uint64_t usec,
719 void *userdata) {
720 sd_dhcp_client *client = userdata;
721
722 client_stop(client, DHCP_EVENT_EXPIRED);
723
724 return 0;
725 }
726
727 static int client_timeout_t2(sd_event_source *s, uint64_t usec, void *userdata) {
728 sd_dhcp_client *client = userdata;
729 int r;
730
731 if (client->fd >= 0) {
732 client->receive_message =
733 sd_event_source_unref(client->receive_message);
734 close(client->fd);
735 client->fd = -1;
736 }
737
738 client->state = DHCP_STATE_REBINDING;
739 client->attempt = 1;
740
741 r = dhcp_network_bind_raw_socket(client->index, &client->link);
742 if (r < 0) {
743 client_stop(client, r);
744 return 0;
745 }
746
747 client->fd = r;
748
749 return client_initialize_events(client, usec);
750 }
751
752 static int client_timeout_t1(sd_event_source *s, uint64_t usec, void *userdata) {
753 sd_dhcp_client *client = userdata;
754 int r;
755
756 client->state = DHCP_STATE_RENEWING;
757 client->attempt = 1;
758
759 r = dhcp_network_bind_udp_socket(client->index,
760 client->lease->address);
761 if (r < 0) {
762 client_stop(client, r);
763 return 0;
764 }
765
766 client->fd = r;
767
768 return client_initialize_events(client, usec);
769 }
770
771 static int client_parse_offer(uint8_t code, uint8_t len, const uint8_t *option,
772 void *user_data) {
773 DHCPLease *lease = user_data;
774 be32_t val;
775
776 switch(code) {
777
778 case DHCP_OPTION_IP_ADDRESS_LEASE_TIME:
779 if (len == 4) {
780 memcpy(&val, option, 4);
781 lease->lifetime = be32toh(val);
782 }
783
784 break;
785
786 case DHCP_OPTION_SERVER_IDENTIFIER:
787 if (len >= 4)
788 memcpy(&lease->server_address, option, 4);
789
790 break;
791
792 case DHCP_OPTION_SUBNET_MASK:
793 if (len >= 4)
794 memcpy(&lease->subnet_mask, option, 4);
795
796 break;
797
798 case DHCP_OPTION_ROUTER:
799 if (len >= 4)
800 memcpy(&lease->router, option, 4);
801
802 break;
803
804 case DHCP_OPTION_DOMAIN_NAME_SERVER:
805 if (len >= 4) {
806 unsigned i;
807
808 lease->dns_size = len / 4;
809
810 free(lease->dns);
811 lease->dns = new0(struct in_addr, lease->dns_size);
812 if (!lease->dns)
813 return -ENOMEM;
814
815 for (i = 0; i < lease->dns_size; i++) {
816 memcpy(&lease->dns[i].s_addr, option + 4 * i, 4);
817 }
818 }
819
820 break;
821
822 case DHCP_OPTION_INTERFACE_MTU:
823 if (len >= 2) {
824 be16_t mtu;
825
826 memcpy(&mtu, option, 2);
827 lease->mtu = be16toh(mtu);
828
829 if (lease->mtu < 68)
830 lease->mtu = 0;
831 }
832
833 break;
834
835 case DHCP_OPTION_HOST_NAME:
836 if (len >= 1) {
837 free(lease->hostname);
838 lease->hostname = strndup((const char *)option, len);
839 }
840
841 break;
842
843 case DHCP_OPTION_RENEWAL_T1_TIME:
844 if (len == 4) {
845 memcpy(&val, option, 4);
846 lease->t1 = be32toh(val);
847 }
848
849 break;
850
851 case DHCP_OPTION_REBINDING_T2_TIME:
852 if (len == 4) {
853 memcpy(&val, option, 4);
854 lease->t2 = be32toh(val);
855 }
856
857 break;
858 }
859
860 return 0;
861 }
862
863 static int client_verify_headers(sd_dhcp_client *client, DHCPPacket *message,
864 size_t len) {
865 size_t hdrlen;
866
867 if (len < (DHCP_IP_UDP_SIZE + DHCP_MESSAGE_SIZE))
868 return -EINVAL;
869
870 hdrlen = message->ip.ihl * 4;
871 if (hdrlen < 20 || hdrlen > len || client_checksum(&message->ip,
872 hdrlen))
873 return -EINVAL;
874
875 message->ip.check = message->udp.len;
876 message->ip.ttl = 0;
877
878 if (hdrlen + be16toh(message->udp.len) > len ||
879 client_checksum(&message->ip.ttl, be16toh(message->udp.len) + 12))
880 return -EINVAL;
881
882 if (be16toh(message->udp.source) != DHCP_PORT_SERVER ||
883 be16toh(message->udp.dest) != DHCP_PORT_CLIENT)
884 return -EINVAL;
885
886 if (message->dhcp.op != BOOTREPLY)
887 return -EINVAL;
888
889 if (be32toh(message->dhcp.xid) != client->xid)
890 return -EINVAL;
891
892 if (memcmp(&message->dhcp.chaddr[0], &client->mac_addr.ether_addr_octet,
893 ETHER_ADDR_LEN))
894 return -EINVAL;
895
896 return 0;
897 }
898
899 static int client_receive_offer(sd_dhcp_client *client, DHCPPacket *offer,
900 size_t len) {
901 _cleanup_lease_free_ DHCPLease *lease = NULL;
902 int r;
903
904 r = client_verify_headers(client, offer, len);
905 if (r < 0)
906 return r;
907
908 lease = new0(DHCPLease, 1);
909 if (!lease)
910 return -ENOMEM;
911
912 len = len - DHCP_IP_UDP_SIZE;
913 r = dhcp_option_parse(&offer->dhcp, len, client_parse_offer,
914 lease);
915 if (r != DHCP_OFFER)
916 return -ENOMSG;
917
918 lease->address = offer->dhcp.yiaddr;
919
920 if (lease->address == INADDR_ANY ||
921 lease->server_address == INADDR_ANY ||
922 lease->subnet_mask == INADDR_ANY ||
923 lease->lifetime == 0)
924 return -ENOMSG;
925
926 client->lease = lease;
927 lease = NULL;
928
929 return 0;
930 }
931
932 static int client_receive_ack(sd_dhcp_client *client, const uint8_t *buf,
933 size_t len) {
934 DHCPPacket *ack;
935 DHCPMessage *dhcp;
936 _cleanup_lease_free_ DHCPLease *lease = NULL;
937 int r;
938
939 if (client->state == DHCP_STATE_RENEWING) {
940 dhcp = (DHCPMessage *)buf;
941 } else {
942 ack = (DHCPPacket *)buf;
943
944 r = client_verify_headers(client, ack, len);
945 if (r < 0)
946 return r;
947
948 dhcp = &ack->dhcp;
949 len -= DHCP_IP_UDP_SIZE;
950 }
951
952 lease = new0(DHCPLease, 1);
953 if (!lease)
954 return -ENOMEM;
955
956 r = dhcp_option_parse(dhcp, len, client_parse_offer, lease);
957 if (r == DHCP_NAK)
958 return DHCP_EVENT_NO_LEASE;
959
960 if (r != DHCP_ACK)
961 return -ENOMSG;
962
963 lease->address = dhcp->yiaddr;
964
965 if (lease->address == INADDR_ANY ||
966 lease->server_address == INADDR_ANY ||
967 lease->subnet_mask == INADDR_ANY || lease->lifetime == 0)
968 return -ENOMSG;
969
970 r = DHCP_EVENT_IP_ACQUIRE;
971 if (client->lease) {
972 if (client->lease->address != lease->address ||
973 client->lease->subnet_mask != lease->subnet_mask ||
974 client->lease->router != lease->router) {
975 r = DHCP_EVENT_IP_CHANGE;
976 }
977
978 lease_free(client->lease);
979 }
980
981 client->lease = lease;
982 lease = NULL;
983
984 return r;
985 }
986
987 static uint64_t client_compute_timeout(uint64_t request_sent,
988 uint32_t lifetime) {
989 return request_sent + (lifetime - 3) * USEC_PER_SEC +
990 + (random_u32() & 0x1fffff);
991 }
992
993 static int client_set_lease_timeouts(sd_dhcp_client *client, uint64_t usec) {
994 uint64_t next_timeout;
995 int r;
996
997 if (client->lease->lifetime < 10)
998 return -EINVAL;
999
1000 client->timeout_t1 = sd_event_source_unref(client->timeout_t1);
1001 client->timeout_t2 = sd_event_source_unref(client->timeout_t2);
1002 client->timeout_expire = sd_event_source_unref(client->timeout_expire);
1003
1004 if (!client->lease->t1)
1005 client->lease->t1 = client->lease->lifetime / 2;
1006
1007 next_timeout = client_compute_timeout(client->request_sent,
1008 client->lease->t1);
1009 if (next_timeout < usec)
1010 return -EINVAL;
1011
1012 r = sd_event_add_monotonic(client->event, next_timeout,
1013 10 * USEC_PER_MSEC,
1014 client_timeout_t1, client,
1015 &client->timeout_t1);
1016 if (r < 0)
1017 return r;
1018
1019 if (!client->lease->t2)
1020 client->lease->t2 = client->lease->lifetime * 7 / 8;
1021
1022 if (client->lease->t2 < client->lease->t1)
1023 return -EINVAL;
1024
1025 if (client->lease->lifetime < client->lease->t2)
1026 return -EINVAL;
1027
1028 next_timeout = client_compute_timeout(client->request_sent,
1029 client->lease->t2);
1030 if (next_timeout < usec)
1031 return -EINVAL;
1032
1033 r = sd_event_add_monotonic(client->event, next_timeout,
1034 10 * USEC_PER_MSEC,
1035 client_timeout_t2, client,
1036 &client->timeout_t2);
1037 if (r < 0)
1038 return r;
1039
1040 next_timeout = client_compute_timeout(client->request_sent,
1041 client->lease->lifetime);
1042 if (next_timeout < usec)
1043 return -EINVAL;
1044
1045 r = sd_event_add_monotonic(client->event, next_timeout,
1046 10 * USEC_PER_MSEC,
1047 client_timeout_expire, client,
1048 &client->timeout_expire);
1049 if (r < 0)
1050 return r;
1051
1052 return 0;
1053 }
1054
1055 static int client_receive_message(sd_event_source *s, int fd,
1056 uint32_t revents, void *userdata) {
1057 sd_dhcp_client *client = userdata;
1058 uint8_t buf[sizeof(DHCPPacket) + DHCP_CLIENT_MIN_OPTIONS_SIZE];
1059 int buflen = sizeof(buf);
1060 int len, r = 0, notify_event = 0;
1061 DHCPPacket *message;
1062 usec_t time_now;
1063
1064 len = read(fd, &buf, buflen);
1065 if (len < 0)
1066 return 0;
1067
1068 r = sd_event_get_now_monotonic(client->event, &time_now);
1069 if (r < 0)
1070 goto error;
1071
1072 switch (client->state) {
1073 case DHCP_STATE_SELECTING:
1074
1075 message = (DHCPPacket *)&buf;
1076
1077 if (client_receive_offer(client, message, len) >= 0) {
1078
1079 client->timeout_resend =
1080 sd_event_source_unref(client->timeout_resend);
1081
1082 client->state = DHCP_STATE_REQUESTING;
1083 client->attempt = 1;
1084
1085 r = sd_event_add_monotonic(client->event, time_now, 0,
1086 client_timeout_resend,
1087 client,
1088 &client->timeout_resend);
1089 if (r < 0)
1090 goto error;
1091 }
1092
1093 break;
1094
1095 case DHCP_STATE_REQUESTING:
1096 case DHCP_STATE_RENEWING:
1097 case DHCP_STATE_REBINDING:
1098
1099 r = client_receive_ack(client, buf, len);
1100
1101 if (r == DHCP_EVENT_NO_LEASE)
1102 goto error;
1103
1104 if (r >= 0) {
1105 client->timeout_resend =
1106 sd_event_source_unref(client->timeout_resend);
1107
1108 if (client->state == DHCP_STATE_REQUESTING)
1109 notify_event = DHCP_EVENT_IP_ACQUIRE;
1110 else if (r != DHCP_EVENT_IP_ACQUIRE)
1111 notify_event = r;
1112
1113 client->state = DHCP_STATE_BOUND;
1114 client->attempt = 1;
1115
1116 client->last_addr = client->lease->address;
1117
1118 r = client_set_lease_timeouts(client, time_now);
1119 if (r < 0)
1120 goto error;
1121
1122 if (notify_event)
1123 client_notify(client, notify_event);
1124
1125 client->receive_message =
1126 sd_event_source_unref(client->receive_message);
1127 close(client->fd);
1128 client->fd = -1;
1129 }
1130
1131 r = 0;
1132
1133 break;
1134
1135 case DHCP_STATE_INIT:
1136 case DHCP_STATE_INIT_REBOOT:
1137 case DHCP_STATE_REBOOTING:
1138 case DHCP_STATE_BOUND:
1139
1140 break;
1141 }
1142
1143 error:
1144 if (r < 0 || r == DHCP_EVENT_NO_LEASE)
1145 return client_stop(client, r);
1146
1147 return 0;
1148 }
1149
1150 int sd_dhcp_client_start(sd_dhcp_client *client) {
1151 int r;
1152
1153 assert_return(client, -EINVAL);
1154 assert_return(client->index > 0, -EINVAL);
1155 assert_return(client->state == DHCP_STATE_INIT ||
1156 client->state == DHCP_STATE_INIT_REBOOT, -EBUSY);
1157
1158 client->xid = random_u32();
1159
1160 r = dhcp_network_bind_raw_socket(client->index, &client->link);
1161
1162 if (r < 0) {
1163 client_stop(client, r);
1164 return r;
1165 }
1166
1167 client->fd = r;
1168 client->start_time = now(CLOCK_MONOTONIC);
1169
1170 return client_initialize_events(client, client->start_time);
1171 }
1172
1173 int sd_dhcp_client_stop(sd_dhcp_client *client) {
1174 return client_stop(client, DHCP_EVENT_STOP);
1175 }
1176
1177 sd_dhcp_client *sd_dhcp_client_free(sd_dhcp_client *client) {
1178 assert_return(client, NULL);
1179
1180 sd_dhcp_client_stop(client);
1181
1182 sd_event_unref(client->event);
1183 free(client->req_opts);
1184 free(client);
1185
1186 return NULL;
1187 }
1188
1189 sd_dhcp_client *sd_dhcp_client_new(sd_event *event) {
1190 sd_dhcp_client *client;
1191
1192 assert_return(event, NULL);
1193
1194 client = new0(sd_dhcp_client, 1);
1195 if (!client)
1196 return NULL;
1197
1198 client->event = sd_event_ref(event);
1199 client->state = DHCP_STATE_INIT;
1200 client->index = -1;
1201 client->fd = -1;
1202 client->attempt = 1;
1203
1204 client->req_opts_size = ELEMENTSOF(default_req_opts);
1205
1206 client->req_opts = memdup(default_req_opts, client->req_opts_size);
1207 if (!client->req_opts) {
1208 free(client);
1209 return NULL;
1210 }
1211
1212 return client;
1213 }