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