chiark / gitweb /
nspawn: make socket(AF_NETLINK, *, NETLINK_AUDIT) fail with EAFNOTSUPPORT in containers
[elogind.git] / src / libsystemd-dhcp / 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-lease.h"
32 #include "dhcp-internal.h"
33 #include "sd-dhcp-client.h"
34
35 #define DHCP_CLIENT_MIN_OPTIONS_SIZE            312
36
37 struct sd_dhcp_client {
38         DHCPState state;
39         sd_event *event;
40         int event_priority;
41         sd_event_source *timeout_resend;
42         int index;
43         int fd;
44         union sockaddr_union link;
45         sd_event_source *receive_message;
46         uint8_t *req_opts;
47         size_t req_opts_allocated;
48         size_t req_opts_size;
49         be32_t last_addr;
50         struct ether_addr mac_addr;
51         uint32_t xid;
52         usec_t start_time;
53         uint16_t secs;
54         unsigned int attempt;
55         usec_t request_sent;
56         sd_event_source *timeout_t1;
57         sd_event_source *timeout_t2;
58         sd_event_source *timeout_expire;
59         sd_dhcp_client_cb_t cb;
60         void *userdata;
61         sd_dhcp_lease *lease;
62 };
63
64 static const uint8_t default_req_opts[] = {
65         DHCP_OPTION_SUBNET_MASK,
66         DHCP_OPTION_ROUTER,
67         DHCP_OPTION_HOST_NAME,
68         DHCP_OPTION_DOMAIN_NAME,
69         DHCP_OPTION_DOMAIN_NAME_SERVER,
70         DHCP_OPTION_NTP_SERVER,
71 };
72
73 static int client_receive_message(sd_event_source *s, int fd,
74                                   uint32_t revents, void *userdata);
75
76 int sd_dhcp_client_set_callback(sd_dhcp_client *client, sd_dhcp_client_cb_t cb,
77                                 void *userdata) {
78         assert_return(client, -EINVAL);
79
80         client->cb = cb;
81         client->userdata = userdata;
82
83         return 0;
84 }
85
86 int sd_dhcp_client_set_request_option(sd_dhcp_client *client, uint8_t option) {
87         size_t i;
88
89         assert_return(client, -EINVAL);
90         assert_return (client->state == DHCP_STATE_INIT, -EBUSY);
91
92         switch(option) {
93         case DHCP_OPTION_PAD:
94         case DHCP_OPTION_OVERLOAD:
95         case DHCP_OPTION_MESSAGE_TYPE:
96         case DHCP_OPTION_PARAMETER_REQUEST_LIST:
97         case DHCP_OPTION_END:
98                 return -EINVAL;
99
100         default:
101                 break;
102         }
103
104         for (i = 0; i < client->req_opts_size; i++)
105                 if (client->req_opts[i] == option)
106                         return -EEXIST;
107
108         if (!GREEDY_REALLOC(client->req_opts, client->req_opts_allocated,
109                             client->req_opts_size + 1))
110                 return -ENOMEM;
111
112         client->req_opts[client->req_opts_size++] = option;
113
114         return 0;
115 }
116
117 int sd_dhcp_client_set_request_address(sd_dhcp_client *client,
118                                        const struct in_addr *last_addr) {
119         assert_return(client, -EINVAL);
120         assert_return(client->state == DHCP_STATE_INIT, -EBUSY);
121
122         if (last_addr)
123                 client->last_addr = last_addr->s_addr;
124         else
125                 client->last_addr = INADDR_ANY;
126
127         return 0;
128 }
129
130 int sd_dhcp_client_set_index(sd_dhcp_client *client, int interface_index) {
131         assert_return(client, -EINVAL);
132         assert_return(client->state == DHCP_STATE_INIT, -EBUSY);
133         assert_return(interface_index >= -1, -EINVAL);
134
135         client->index = interface_index;
136
137         return 0;
138 }
139
140 int sd_dhcp_client_set_mac(sd_dhcp_client *client,
141                            const struct ether_addr *addr) {
142         assert_return(client, -EINVAL);
143         assert_return(client->state == DHCP_STATE_INIT, -EBUSY);
144
145         memcpy(&client->mac_addr, addr, ETH_ALEN);
146
147         return 0;
148 }
149
150 int sd_dhcp_client_get_lease(sd_dhcp_client *client, sd_dhcp_lease **ret) {
151         assert_return(client, -EINVAL);
152         assert_return(ret, -EINVAL);
153
154         if (client->state != DHCP_STATE_BOUND &&
155             client->state != DHCP_STATE_RENEWING &&
156             client->state != DHCP_STATE_REBINDING)
157                 return -EADDRNOTAVAIL;
158
159         *ret = sd_dhcp_lease_ref(client->lease);
160
161         return 0;
162 }
163
164 static int client_notify(sd_dhcp_client *client, int event) {
165         if (client->cb)
166                 client->cb(client, event, client->userdata);
167
168         return 0;
169 }
170
171 static int client_stop(sd_dhcp_client *client, int error) {
172         assert_return(client, -EINVAL);
173
174         client->receive_message =
175                 sd_event_source_unref(client->receive_message);
176
177         if (client->fd >= 0)
178                 close(client->fd);
179         client->fd = -1;
180
181         client->timeout_resend = sd_event_source_unref(client->timeout_resend);
182
183         client->timeout_t1 = sd_event_source_unref(client->timeout_t1);
184         client->timeout_t2 = sd_event_source_unref(client->timeout_t2);
185         client->timeout_expire = sd_event_source_unref(client->timeout_expire);
186
187         client->attempt = 1;
188
189         client_notify(client, error);
190
191         client->start_time = 0;
192         client->secs = 0;
193         client->state = DHCP_STATE_INIT;
194
195         if (client->lease)
196                 client->lease = sd_dhcp_lease_unref(client->lease);
197
198         return 0;
199 }
200
201 static int client_message_init(sd_dhcp_client *client, DHCPMessage *message,
202                                uint8_t type, uint16_t secs, uint8_t **opt,
203                                size_t *optlen) {
204         int r;
205
206         r = dhcp_message_init(message, BOOTREQUEST, client->xid, type,
207                               secs, opt, optlen);
208         if (r < 0)
209                 return r;
210
211         memcpy(&message->chaddr, &client->mac_addr, ETH_ALEN);
212
213         if (client->state == DHCP_STATE_RENEWING ||
214             client->state == DHCP_STATE_REBINDING)
215                 message->ciaddr = client->lease->address;
216
217         /* Some DHCP servers will refuse to issue an DHCP lease if the Cliient
218            Identifier option is not set */
219         r = dhcp_option_append(opt, optlen, DHCP_OPTION_CLIENT_IDENTIFIER,
220                                ETH_ALEN, &client->mac_addr);
221         if (r < 0)
222                 return r;
223
224         if (type == DHCP_DISCOVER || type == DHCP_REQUEST) {
225                 be16_t max_size;
226
227                 r = dhcp_option_append(opt, optlen,
228                                        DHCP_OPTION_PARAMETER_REQUEST_LIST,
229                                        client->req_opts_size,
230                                        client->req_opts);
231                 if (r < 0)
232                         return r;
233
234                 /* Some DHCP servers will send bigger DHCP packets than the
235                    defined default size unless the Maximum Messge Size option
236                    is explicitely set */
237                 max_size = htobe16(DHCP_IP_UDP_SIZE + DHCP_MESSAGE_SIZE +
238                                    DHCP_CLIENT_MIN_OPTIONS_SIZE);
239                 r = dhcp_option_append(opt, optlen,
240                                        DHCP_OPTION_MAXIMUM_MESSAGE_SIZE,
241                                        2, &max_size);
242                 if (r < 0)
243                         return r;
244         }
245
246         return 0;
247 }
248
249 static int client_send_discover(sd_dhcp_client *client, uint16_t secs) {
250         int err = 0;
251         _cleanup_free_ DHCPPacket *discover;
252         size_t optlen, len;
253         uint8_t *opt;
254
255         optlen = DHCP_CLIENT_MIN_OPTIONS_SIZE;
256         len = sizeof(DHCPPacket) + optlen;
257
258         discover = malloc0(len);
259
260         if (!discover)
261                 return -ENOMEM;
262
263         err = client_message_init(client, &discover->dhcp, DHCP_DISCOVER,
264                                   secs, &opt, &optlen);
265         if (err < 0)
266                 return err;
267
268         if (client->last_addr != INADDR_ANY) {
269                 err = dhcp_option_append(&opt, &optlen,
270                                          DHCP_OPTION_REQUESTED_IP_ADDRESS,
271                                          4, &client->last_addr);
272                 if (err < 0)
273                         return err;
274         }
275
276         err = dhcp_option_append(&opt, &optlen, DHCP_OPTION_END, 0, NULL);
277         if (err < 0)
278                 return err;
279
280         dhcp_packet_append_ip_headers(discover, BOOTREQUEST, len);
281
282         err = dhcp_network_send_raw_socket(client->fd, &client->link,
283                                            discover, len);
284
285         return err;
286 }
287
288 static int client_send_request(sd_dhcp_client *client, uint16_t secs) {
289         _cleanup_free_ DHCPPacket *request;
290         size_t optlen, len;
291         int err;
292         uint8_t *opt;
293
294         optlen = DHCP_CLIENT_MIN_OPTIONS_SIZE;
295         len = DHCP_MESSAGE_SIZE + optlen;
296
297         request = malloc0(len);
298         if (!request)
299                 return -ENOMEM;
300
301         err = client_message_init(client, &request->dhcp, DHCP_REQUEST, secs,
302                                   &opt, &optlen);
303         if (err < 0)
304                 return err;
305
306         if (client->state == DHCP_STATE_REQUESTING) {
307                 err = dhcp_option_append(&opt, &optlen,
308                                          DHCP_OPTION_REQUESTED_IP_ADDRESS,
309                                          4, &client->lease->address);
310                 if (err < 0)
311                         return err;
312
313                 err = dhcp_option_append(&opt, &optlen,
314                                          DHCP_OPTION_SERVER_IDENTIFIER,
315                                          4, &client->lease->server_address);
316                 if (err < 0)
317                         return err;
318         }
319
320         err = dhcp_option_append(&opt, &optlen, DHCP_OPTION_END, 0, NULL);
321         if (err < 0)
322                 return err;
323
324         if (client->state == DHCP_STATE_RENEWING) {
325                 err = dhcp_network_send_udp_socket(client->fd,
326                                                    client->lease->server_address,
327                                                    &request->dhcp,
328                                                    len - DHCP_IP_UDP_SIZE);
329         } else {
330                 dhcp_packet_append_ip_headers(request, BOOTREQUEST, len);
331
332                 err = dhcp_network_send_raw_socket(client->fd, &client->link,
333                                                    request, len);
334         }
335
336         return err;
337 }
338
339 static uint16_t client_update_secs(sd_dhcp_client *client, usec_t time_now)
340 {
341         client->secs = ((time_now - client->start_time) / USEC_PER_SEC) ? : 1;
342
343         return client->secs;
344 }
345
346 static int client_timeout_resend(sd_event_source *s, uint64_t usec,
347                                  void *userdata) {
348         sd_dhcp_client *client = userdata;
349         usec_t next_timeout = 0;
350         uint32_t time_left;
351         int r = 0;
352
353         assert(s);
354         assert(client);
355         assert(client->event);
356
357         switch (client->state) {
358         case DHCP_STATE_RENEWING:
359
360                 time_left = (client->lease->t2 - client->lease->t1)/2;
361                 if (time_left < 60)
362                         time_left = 60;
363
364                 next_timeout = usec + time_left * USEC_PER_SEC;
365
366                 break;
367
368         case DHCP_STATE_REBINDING:
369
370                 time_left = (client->lease->lifetime - client->lease->t2)/2;
371                 if (time_left < 60)
372                         time_left = 60;
373
374                 next_timeout = usec + time_left * USEC_PER_SEC;
375                 break;
376
377         case DHCP_STATE_INIT:
378         case DHCP_STATE_INIT_REBOOT:
379         case DHCP_STATE_REBOOTING:
380         case DHCP_STATE_SELECTING:
381         case DHCP_STATE_REQUESTING:
382         case DHCP_STATE_BOUND:
383
384                 if (client->attempt < 64)
385                         client->attempt *= 2;
386
387                 next_timeout = usec + (client->attempt - 1) * USEC_PER_SEC;
388
389                 break;
390         }
391
392         next_timeout += (random_u32() & 0x1fffff);
393
394         r = sd_event_add_monotonic(client->event, next_timeout,
395                                      10 * USEC_PER_MSEC,
396                                      client_timeout_resend, client,
397                                      &client->timeout_resend);
398         if (r < 0)
399                 goto error;
400
401         r = sd_event_source_set_priority(client->timeout_resend, client->event_priority);
402         if (r < 0)
403                 goto error;
404
405         switch (client->state) {
406         case DHCP_STATE_INIT:
407
408                 client_update_secs(client, usec);
409
410                 r = client_send_discover(client, client->secs);
411                 if (r >= 0) {
412                         client->state = DHCP_STATE_SELECTING;
413                         client->attempt = 1;
414                 } else {
415                         if (client->attempt >= 64)
416                                 goto error;
417                 }
418
419                 break;
420
421         case DHCP_STATE_SELECTING:
422                 client_update_secs(client, usec);
423
424                 r = client_send_discover(client, client->secs);
425                 if (r < 0 && client->attempt >= 64)
426                         goto error;
427
428                 break;
429
430         case DHCP_STATE_REQUESTING:
431         case DHCP_STATE_RENEWING:
432         case DHCP_STATE_REBINDING:
433                 r = client_send_request(client, client->secs);
434                 if (r < 0 && client->attempt >= 64)
435                          goto error;
436
437                 client->request_sent = usec;
438
439                 break;
440
441         case DHCP_STATE_INIT_REBOOT:
442         case DHCP_STATE_REBOOTING:
443         case DHCP_STATE_BOUND:
444
445                 break;
446         }
447
448         return 0;
449
450 error:
451         client_stop(client, r);
452
453         /* Errors were dealt with when stopping the client, don't spill
454            errors into the event loop handler */
455         return 0;
456 }
457
458 static int client_initialize_events(sd_dhcp_client *client, usec_t usec) {
459         int r;
460
461         assert(client);
462         assert(client->event);
463
464         r = sd_event_add_io(client->event, client->fd, EPOLLIN,
465                             client_receive_message, client,
466                             &client->receive_message);
467         if (r < 0)
468                 goto error;
469
470         r = sd_event_source_set_priority(client->receive_message, client->event_priority);
471         if (r < 0)
472                 goto error;
473
474         r = sd_event_add_monotonic(client->event, usec, 0,
475                                    client_timeout_resend, client,
476                                    &client->timeout_resend);
477         if (r < 0)
478                 goto error;
479
480         r = sd_event_source_set_priority(client->timeout_resend, client->event_priority);
481
482 error:
483         if (r < 0)
484                 client_stop(client, r);
485
486         return 0;
487
488 }
489
490 static int client_timeout_expire(sd_event_source *s, uint64_t usec,
491                                  void *userdata) {
492         sd_dhcp_client *client = userdata;
493
494         client_stop(client, DHCP_EVENT_EXPIRED);
495
496         return 0;
497 }
498
499 static int client_timeout_t2(sd_event_source *s, uint64_t usec, void *userdata) {
500         sd_dhcp_client *client = userdata;
501         int r;
502
503         if (client->fd >= 0) {
504                 client->receive_message =
505                         sd_event_source_unref(client->receive_message);
506                 close(client->fd);
507                 client->fd = -1;
508         }
509
510         client->state = DHCP_STATE_REBINDING;
511         client->attempt = 1;
512
513         r = dhcp_network_bind_raw_socket(client->index, &client->link);
514         if (r < 0) {
515                 client_stop(client, r);
516                 return 0;
517         }
518
519         client->fd = r;
520
521         return client_initialize_events(client, usec);
522 }
523
524 static int client_timeout_t1(sd_event_source *s, uint64_t usec, void *userdata) {
525         sd_dhcp_client *client = userdata;
526         int r;
527
528         client->state = DHCP_STATE_RENEWING;
529         client->attempt = 1;
530
531         r = dhcp_network_bind_udp_socket(client->index,
532                                          client->lease->address);
533         if (r < 0) {
534                 client_stop(client, r);
535                 return 0;
536         }
537
538         client->fd = r;
539
540         return client_initialize_events(client, usec);
541 }
542
543 static int client_verify_headers(sd_dhcp_client *client, DHCPPacket *message,
544                                  size_t len) {
545         int r;
546
547         r = dhcp_packet_verify_headers(message, BOOTREPLY, len);
548         if (r < 0)
549                 return r;
550
551         if (be32toh(message->dhcp.xid) != client->xid)
552                 return -EINVAL;
553
554         if (memcmp(&message->dhcp.chaddr[0], &client->mac_addr.ether_addr_octet,
555                     ETHER_ADDR_LEN))
556                 return -EINVAL;
557
558         return 0;
559 }
560
561 static int client_receive_offer(sd_dhcp_client *client, DHCPPacket *offer,
562                                 size_t len) {
563         _cleanup_dhcp_lease_unref_ sd_dhcp_lease *lease = NULL;
564         int r;
565
566         r = client_verify_headers(client, offer, len);
567         if (r < 0)
568                 return r;
569
570         r = dhcp_lease_new(&lease);
571         if (r < 0)
572                 return r;
573
574         len = len - DHCP_IP_UDP_SIZE;
575         r = dhcp_option_parse(&offer->dhcp, len, dhcp_lease_parse_options,
576                               lease);
577         if (r != DHCP_OFFER)
578                 return -ENOMSG;
579
580         lease->address = offer->dhcp.yiaddr;
581
582         if (lease->address == INADDR_ANY ||
583             lease->server_address == INADDR_ANY ||
584             lease->subnet_mask == INADDR_ANY ||
585             lease->lifetime == 0)
586                 return -ENOMSG;
587
588         client->lease = lease;
589         lease = NULL;
590
591         return 0;
592 }
593
594 static int client_receive_ack(sd_dhcp_client *client, const uint8_t *buf,
595                               size_t len) {
596         DHCPPacket *ack;
597         DHCPMessage *dhcp;
598         _cleanup_dhcp_lease_unref_ sd_dhcp_lease *lease = NULL;
599         int r;
600
601         if (client->state == DHCP_STATE_RENEWING) {
602                 dhcp = (DHCPMessage *)buf;
603         } else {
604                 ack = (DHCPPacket *)buf;
605
606                 r = client_verify_headers(client, ack, len);
607                 if (r < 0)
608                         return r;
609
610                 dhcp = &ack->dhcp;
611                 len -= DHCP_IP_UDP_SIZE;
612         }
613
614         r = dhcp_lease_new(&lease);
615         if (r < 0)
616                 return r;
617
618         r = dhcp_option_parse(dhcp, len, dhcp_lease_parse_options, lease);
619         if (r == DHCP_NAK)
620                 return DHCP_EVENT_NO_LEASE;
621
622         if (r != DHCP_ACK)
623                 return -ENOMSG;
624
625         lease->address = dhcp->yiaddr;
626
627         if (lease->address == INADDR_ANY ||
628             lease->server_address == INADDR_ANY ||
629             lease->subnet_mask == INADDR_ANY || lease->lifetime == 0)
630                 return -ENOMSG;
631
632         r = DHCP_EVENT_IP_ACQUIRE;
633         if (client->lease) {
634                 if (client->lease->address != lease->address ||
635                     client->lease->subnet_mask != lease->subnet_mask ||
636                     client->lease->router != lease->router) {
637                         r = DHCP_EVENT_IP_CHANGE;
638                 }
639
640                 client->lease = sd_dhcp_lease_unref(client->lease);
641         }
642
643         client->lease = lease;
644         lease = NULL;
645
646         return r;
647 }
648
649 static uint64_t client_compute_timeout(uint64_t request_sent,
650                                        uint32_t lifetime) {
651         return request_sent + (lifetime - 3) * USEC_PER_SEC +
652                 + (random_u32() & 0x1fffff);
653 }
654
655 static int client_set_lease_timeouts(sd_dhcp_client *client, uint64_t usec) {
656         uint64_t next_timeout;
657         int r;
658
659         assert(client);
660         assert(client->event);
661
662         if (client->lease->lifetime < 10)
663                 return -EINVAL;
664
665         client->timeout_t1 = sd_event_source_unref(client->timeout_t1);
666         client->timeout_t2 = sd_event_source_unref(client->timeout_t2);
667         client->timeout_expire = sd_event_source_unref(client->timeout_expire);
668
669         if (!client->lease->t1)
670                 client->lease->t1 = client->lease->lifetime / 2;
671
672         next_timeout = client_compute_timeout(client->request_sent,
673                                               client->lease->t1);
674         if (next_timeout < usec)
675                 return -EINVAL;
676
677         r = sd_event_add_monotonic(client->event, next_timeout,
678                                      10 * USEC_PER_MSEC,
679                                      client_timeout_t1, client,
680                                      &client->timeout_t1);
681         if (r < 0)
682                 return r;
683
684         r = sd_event_source_set_priority(client->timeout_t1, client->event_priority);
685         if (r < 0)
686                 return r;
687
688         if (!client->lease->t2)
689                 client->lease->t2 = client->lease->lifetime * 7 / 8;
690
691         if (client->lease->t2 < client->lease->t1)
692                 return -EINVAL;
693
694         if (client->lease->lifetime < client->lease->t2)
695                 return -EINVAL;
696
697         next_timeout = client_compute_timeout(client->request_sent,
698                                               client->lease->t2);
699         if (next_timeout < usec)
700                 return -EINVAL;
701
702         r = sd_event_add_monotonic(client->event, next_timeout,
703                                      10 * USEC_PER_MSEC,
704                                      client_timeout_t2, client,
705                                      &client->timeout_t2);
706         if (r < 0)
707                 return r;
708
709         r = sd_event_source_set_priority(client->timeout_t2, client->event_priority);
710         if (r < 0)
711                 return r;
712
713         next_timeout = client_compute_timeout(client->request_sent,
714                                               client->lease->lifetime);
715         if (next_timeout < usec)
716                 return -EINVAL;
717
718         r = sd_event_add_monotonic(client->event, next_timeout,
719                                      10 * USEC_PER_MSEC,
720                                      client_timeout_expire, client,
721                                      &client->timeout_expire);
722         if (r < 0)
723                 return r;
724
725         r = sd_event_source_set_priority(client->timeout_expire, client->event_priority);
726         if (r < 0)
727                 return r;
728
729         return 0;
730 }
731
732 static int client_receive_message(sd_event_source *s, int fd,
733                                   uint32_t revents, void *userdata) {
734         sd_dhcp_client *client = userdata;
735         uint8_t buf[sizeof(DHCPPacket) + DHCP_CLIENT_MIN_OPTIONS_SIZE];
736         int buflen = sizeof(buf);
737         int len, r = 0, notify_event = 0;
738         DHCPPacket *message;
739         usec_t time_now;
740
741         assert(s);
742         assert(client);
743         assert(client->event);
744
745         len = read(fd, &buf, buflen);
746         if (len < 0)
747                 return 0;
748
749         r = sd_event_get_now_monotonic(client->event, &time_now);
750         if (r < 0)
751                 goto error;
752
753         switch (client->state) {
754         case DHCP_STATE_SELECTING:
755
756                 message = (DHCPPacket *)&buf;
757
758                 if (client_receive_offer(client, message, len) >= 0) {
759
760                         client->timeout_resend =
761                                 sd_event_source_unref(client->timeout_resend);
762
763                         client->state = DHCP_STATE_REQUESTING;
764                         client->attempt = 1;
765
766                         r = sd_event_add_monotonic(client->event, time_now, 0,
767                                                    client_timeout_resend,
768                                                    client,
769                                                    &client->timeout_resend);
770                         if (r < 0)
771                                 goto error;
772
773                         r = sd_event_source_set_priority(client->timeout_resend, client->event_priority);
774                         if (r < 0)
775                                 goto error;
776                 }
777
778                 break;
779
780         case DHCP_STATE_REQUESTING:
781         case DHCP_STATE_RENEWING:
782         case DHCP_STATE_REBINDING:
783
784                 r = client_receive_ack(client, buf, len);
785
786                 if (r == DHCP_EVENT_NO_LEASE)
787                         goto error;
788
789                 if (r >= 0) {
790                         client->timeout_resend =
791                                 sd_event_source_unref(client->timeout_resend);
792
793                         if (client->state == DHCP_STATE_REQUESTING)
794                                 notify_event = DHCP_EVENT_IP_ACQUIRE;
795                         else if (r != DHCP_EVENT_IP_ACQUIRE)
796                                 notify_event = r;
797
798                         client->state = DHCP_STATE_BOUND;
799                         client->attempt = 1;
800
801                         client->last_addr = client->lease->address;
802
803                         r = client_set_lease_timeouts(client, time_now);
804                         if (r < 0)
805                                 goto error;
806
807                         if (notify_event)
808                                 client_notify(client, notify_event);
809
810                         client->receive_message =
811                                 sd_event_source_unref(client->receive_message);
812                         close(client->fd);
813                         client->fd = -1;
814                 }
815
816                 r = 0;
817
818                 break;
819
820         case DHCP_STATE_INIT:
821         case DHCP_STATE_INIT_REBOOT:
822         case DHCP_STATE_REBOOTING:
823         case DHCP_STATE_BOUND:
824
825                 break;
826         }
827
828 error:
829         if (r < 0 || r == DHCP_EVENT_NO_LEASE)
830                 return client_stop(client, r);
831
832         return 0;
833 }
834
835 int sd_dhcp_client_start(sd_dhcp_client *client) {
836         int r;
837
838         assert_return(client, -EINVAL);
839         assert_return(client->event, -EINVAL);
840         assert_return(client->index > 0, -EINVAL);
841         assert_return(client->state == DHCP_STATE_INIT ||
842                       client->state == DHCP_STATE_INIT_REBOOT, -EBUSY);
843
844         client->xid = random_u32();
845
846         r = dhcp_network_bind_raw_socket(client->index, &client->link);
847
848         if (r < 0) {
849                 client_stop(client, r);
850                 return r;
851         }
852
853         client->fd = r;
854         client->start_time = now(CLOCK_MONOTONIC);
855         client->secs = 0;
856
857         return client_initialize_events(client, client->start_time);
858 }
859
860 int sd_dhcp_client_stop(sd_dhcp_client *client) {
861         return client_stop(client, DHCP_EVENT_STOP);
862 }
863
864 int sd_dhcp_client_attach_event(sd_dhcp_client *client, sd_event *event, int priority) {
865         int r;
866
867         assert_return(client, -EINVAL);
868         assert_return(!client->event, -EBUSY);
869
870         if (event)
871                 client->event = sd_event_ref(event);
872         else {
873                 r = sd_event_default(&client->event);
874                 if (r < 0)
875                         return 0;
876         }
877
878         client->event_priority = priority;
879
880         return 0;
881 }
882
883 int sd_dhcp_client_detach_event(sd_dhcp_client *client) {
884         assert_return(client, -EINVAL);
885
886         client->event = sd_event_unref(client->event);
887
888         return 0;
889 }
890
891 sd_event *sd_dhcp_client_get_event(sd_dhcp_client *client) {
892         if (!client)
893                 return NULL;
894
895         return client->event;
896 }
897
898 void sd_dhcp_client_free(sd_dhcp_client *client) {
899         if (!client)
900                 return;
901
902         sd_dhcp_client_stop(client);
903         sd_dhcp_client_detach_event(client);
904
905         free(client->req_opts);
906         free(client);
907 }
908
909 DEFINE_TRIVIAL_CLEANUP_FUNC(sd_dhcp_client*, sd_dhcp_client_free);
910 #define _cleanup_dhcp_client_free_ _cleanup_(sd_dhcp_client_freep)
911
912 int sd_dhcp_client_new(sd_dhcp_client **ret) {
913         _cleanup_dhcp_client_free_ sd_dhcp_client *client = NULL;
914
915         assert_return(ret, -EINVAL);
916
917         client = new0(sd_dhcp_client, 1);
918         if (!client)
919                 return -ENOMEM;
920
921         client->state = DHCP_STATE_INIT;
922         client->index = -1;
923         client->fd = -1;
924         client->attempt = 1;
925
926         client->req_opts_size = ELEMENTSOF(default_req_opts);
927
928         client->req_opts = memdup(default_req_opts, client->req_opts_size);
929         if (!client->req_opts)
930                 return -ENOMEM;
931
932         *ret = client;
933         client = NULL;
934
935         return 0;
936 }