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