chiark / gitweb /
sd-dhcp: minimum options size is part of the protocol
[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 struct sd_dhcp_client {
36         DHCPState state;
37         sd_event *event;
38         int event_priority;
39         sd_event_source *timeout_resend;
40         int index;
41         int fd;
42         union sockaddr_union link;
43         sd_event_source *receive_message;
44         uint8_t *req_opts;
45         size_t req_opts_allocated;
46         size_t req_opts_size;
47         be32_t last_addr;
48         struct ether_addr mac_addr;
49         uint32_t xid;
50         usec_t start_time;
51         uint16_t secs;
52         unsigned int attempt;
53         usec_t request_sent;
54         sd_event_source *timeout_t1;
55         sd_event_source *timeout_t2;
56         sd_event_source *timeout_expire;
57         sd_dhcp_client_cb_t cb;
58         void *userdata;
59         sd_dhcp_lease *lease;
60 };
61
62 static const uint8_t default_req_opts[] = {
63         DHCP_OPTION_SUBNET_MASK,
64         DHCP_OPTION_ROUTER,
65         DHCP_OPTION_HOST_NAME,
66         DHCP_OPTION_DOMAIN_NAME,
67         DHCP_OPTION_DOMAIN_NAME_SERVER,
68         DHCP_OPTION_NTP_SERVER,
69 };
70
71 static int client_receive_message_raw(sd_event_source *s, int fd,
72                                       uint32_t revents, void *userdata);
73 static int client_receive_message_udp(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_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_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_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,
402                                          client->event_priority);
403         if (r < 0)
404                 goto error;
405
406         switch (client->state) {
407         case DHCP_STATE_INIT:
408
409                 client_update_secs(client, usec);
410
411                 r = client_send_discover(client, client->secs);
412                 if (r >= 0) {
413                         client->state = DHCP_STATE_SELECTING;
414                         client->attempt = 1;
415                 } else {
416                         if (client->attempt >= 64)
417                                 goto error;
418                 }
419
420                 break;
421
422         case DHCP_STATE_SELECTING:
423                 client_update_secs(client, usec);
424
425                 r = client_send_discover(client, client->secs);
426                 if (r < 0 && client->attempt >= 64)
427                         goto error;
428
429                 break;
430
431         case DHCP_STATE_REQUESTING:
432         case DHCP_STATE_RENEWING:
433         case DHCP_STATE_REBINDING:
434                 r = client_send_request(client, client->secs);
435                 if (r < 0 && client->attempt >= 64)
436                          goto error;
437
438                 client->request_sent = usec;
439
440                 break;
441
442         case DHCP_STATE_INIT_REBOOT:
443         case DHCP_STATE_REBOOTING:
444         case DHCP_STATE_BOUND:
445
446                 break;
447         }
448
449         return 0;
450
451 error:
452         client_stop(client, r);
453
454         /* Errors were dealt with when stopping the client, don't spill
455            errors into the event loop handler */
456         return 0;
457 }
458
459 static int client_initialize_events(sd_dhcp_client *client,
460                                     sd_event_io_handler_t io_callback,
461                                     usec_t usec) {
462         int r;
463
464         assert(client);
465         assert(client->event);
466
467         r = sd_event_add_io(client->event, client->fd, EPOLLIN, io_callback,
468                             client, &client->receive_message);
469         if (r < 0)
470                 goto error;
471
472         r = sd_event_source_set_priority(client->receive_message,
473                                          client->event_priority);
474         if (r < 0)
475                 goto error;
476
477         r = sd_event_add_monotonic(client->event, usec, 0,
478                                    client_timeout_resend, client,
479                                    &client->timeout_resend);
480         if (r < 0)
481                 goto error;
482
483         r = sd_event_source_set_priority(client->timeout_resend,
484                                          client->event_priority);
485
486 error:
487         if (r < 0)
488                 client_stop(client, r);
489
490         return 0;
491
492 }
493
494 static int client_timeout_expire(sd_event_source *s, uint64_t usec,
495                                  void *userdata) {
496         sd_dhcp_client *client = userdata;
497
498         client_stop(client, DHCP_EVENT_EXPIRED);
499
500         return 0;
501 }
502
503 static int client_timeout_t2(sd_event_source *s, uint64_t usec, void *userdata) {
504         sd_dhcp_client *client = userdata;
505         int r;
506
507         if (client->fd >= 0) {
508                 client->receive_message =
509                         sd_event_source_unref(client->receive_message);
510                 close(client->fd);
511                 client->fd = -1;
512         }
513
514         client->state = DHCP_STATE_REBINDING;
515         client->attempt = 1;
516
517         r = dhcp_network_bind_raw_socket(client->index, &client->link);
518         if (r < 0) {
519                 client_stop(client, r);
520                 return 0;
521         }
522
523         client->fd = r;
524
525         return client_initialize_events(client, client_receive_message_raw,
526                                         usec);
527 }
528
529 static int client_timeout_t1(sd_event_source *s, uint64_t usec,
530                              void *userdata) {
531         sd_dhcp_client *client = userdata;
532         int r;
533
534         client->state = DHCP_STATE_RENEWING;
535         client->attempt = 1;
536
537         r = dhcp_network_bind_udp_socket(client->index,
538                                          client->lease->address);
539         if (r < 0) {
540                 client_stop(client, r);
541                 return 0;
542         }
543
544         client->fd = r;
545
546         return client_initialize_events(client, client_receive_message_udp, usec);
547 }
548
549 static int client_handle_offer(sd_dhcp_client *client, DHCPMessage *offer,
550                                size_t len) {
551         _cleanup_dhcp_lease_unref_ sd_dhcp_lease *lease = NULL;
552         int r;
553
554         r = dhcp_lease_new(&lease);
555         if (r < 0)
556                 return r;
557
558         r = dhcp_option_parse(offer, len, dhcp_lease_parse_options, lease);
559         if (r != DHCP_OFFER)
560                 return -ENOMSG;
561
562         lease->address = offer->yiaddr;
563
564         if (lease->address == INADDR_ANY ||
565             lease->server_address == INADDR_ANY ||
566             lease->subnet_mask == INADDR_ANY ||
567             lease->lifetime == 0)
568                 return -ENOMSG;
569
570         client->lease = lease;
571         lease = NULL;
572
573         return 0;
574 }
575
576 static int client_handle_ack(sd_dhcp_client *client, DHCPMessage *ack,
577                              size_t len) {
578         _cleanup_dhcp_lease_unref_ sd_dhcp_lease *lease = NULL;
579         int r;
580
581         r = dhcp_lease_new(&lease);
582         if (r < 0)
583                 return r;
584
585         r = dhcp_option_parse(ack, len, dhcp_lease_parse_options, lease);
586         if (r == DHCP_NAK)
587                 return DHCP_EVENT_NO_LEASE;
588
589         if (r != DHCP_ACK)
590                 return -ENOMSG;
591
592         lease->address = ack->yiaddr;
593
594         if (lease->address == INADDR_ANY ||
595             lease->server_address == INADDR_ANY ||
596             lease->subnet_mask == INADDR_ANY || lease->lifetime == 0)
597                 return -ENOMSG;
598
599         r = DHCP_EVENT_IP_ACQUIRE;
600         if (client->lease) {
601                 if (client->lease->address != lease->address ||
602                     client->lease->subnet_mask != lease->subnet_mask ||
603                     client->lease->router != lease->router) {
604                         r = DHCP_EVENT_IP_CHANGE;
605                 }
606
607                 client->lease = sd_dhcp_lease_unref(client->lease);
608         }
609
610         client->lease = lease;
611         lease = NULL;
612
613         return r;
614 }
615
616 static uint64_t client_compute_timeout(uint64_t request_sent,
617                                        uint32_t lifetime) {
618         return request_sent + (lifetime - 3) * USEC_PER_SEC +
619                 + (random_u32() & 0x1fffff);
620 }
621
622 static int client_set_lease_timeouts(sd_dhcp_client *client, uint64_t usec) {
623         uint64_t next_timeout;
624         int r;
625
626         assert(client);
627         assert(client->event);
628
629         if (client->lease->lifetime < 10)
630                 return -EINVAL;
631
632         client->timeout_t1 = sd_event_source_unref(client->timeout_t1);
633         client->timeout_t2 = sd_event_source_unref(client->timeout_t2);
634         client->timeout_expire = sd_event_source_unref(client->timeout_expire);
635
636         if (!client->lease->t1)
637                 client->lease->t1 = client->lease->lifetime / 2;
638
639         next_timeout = client_compute_timeout(client->request_sent,
640                                               client->lease->t1);
641         if (next_timeout < usec)
642                 return -EINVAL;
643
644         r = sd_event_add_monotonic(client->event, next_timeout,
645                                      10 * USEC_PER_MSEC,
646                                      client_timeout_t1, client,
647                                      &client->timeout_t1);
648         if (r < 0)
649                 return r;
650
651         r = sd_event_source_set_priority(client->timeout_t1,
652                                          client->event_priority);
653         if (r < 0)
654                 return r;
655
656         if (!client->lease->t2)
657                 client->lease->t2 = client->lease->lifetime * 7 / 8;
658
659         if (client->lease->t2 < client->lease->t1)
660                 return -EINVAL;
661
662         if (client->lease->lifetime < client->lease->t2)
663                 return -EINVAL;
664
665         next_timeout = client_compute_timeout(client->request_sent,
666                                               client->lease->t2);
667         if (next_timeout < usec)
668                 return -EINVAL;
669
670         r = sd_event_add_monotonic(client->event, next_timeout,
671                                      10 * USEC_PER_MSEC,
672                                      client_timeout_t2, client,
673                                      &client->timeout_t2);
674         if (r < 0)
675                 return r;
676
677         r = sd_event_source_set_priority(client->timeout_t2,
678                                          client->event_priority);
679         if (r < 0)
680                 return r;
681
682         next_timeout = client_compute_timeout(client->request_sent,
683                                               client->lease->lifetime);
684         if (next_timeout < usec)
685                 return -EINVAL;
686
687         r = sd_event_add_monotonic(client->event, next_timeout,
688                                      10 * USEC_PER_MSEC,
689                                      client_timeout_expire, client,
690                                      &client->timeout_expire);
691         if (r < 0)
692                 return r;
693
694         r = sd_event_source_set_priority(client->timeout_expire,
695                                          client->event_priority);
696         if (r < 0)
697                 return r;
698
699         return 0;
700 }
701
702 static int client_handle_message(sd_dhcp_client *client, DHCPMessage *message,
703                                  int len, usec_t time_now) {
704         int r = 0, notify_event = 0;
705
706         assert(client);
707         assert(client->event);
708         assert(message);
709
710         if (be32toh(message->xid) != client->xid)
711                 return -EINVAL;
712
713         if (memcmp(&message->chaddr[0], &client->mac_addr.ether_addr_octet,
714                    ETHER_ADDR_LEN))
715                 return -EINVAL;
716
717         switch (client->state) {
718         case DHCP_STATE_SELECTING:
719
720                 r = client_handle_offer(client, message, len);
721                 if (r >= 0) {
722
723                         client->timeout_resend =
724                                 sd_event_source_unref(client->timeout_resend);
725
726                         client->state = DHCP_STATE_REQUESTING;
727                         client->attempt = 1;
728
729                         r = sd_event_add_monotonic(client->event, time_now, 0,
730                                                    client_timeout_resend,
731                                                    client,
732                                                    &client->timeout_resend);
733                         if (r < 0)
734                                 goto error;
735
736                         r = sd_event_source_set_priority(client->timeout_resend,
737                                                          client->event_priority);
738                         if (r < 0)
739                                 goto error;
740                 }
741
742                 break;
743
744         case DHCP_STATE_REQUESTING:
745         case DHCP_STATE_RENEWING:
746         case DHCP_STATE_REBINDING:
747
748                 r = client_handle_ack(client, message, len);
749
750                 if (r == DHCP_EVENT_NO_LEASE)
751                         goto error;
752
753                 if (r >= 0) {
754                         client->timeout_resend =
755                                 sd_event_source_unref(client->timeout_resend);
756
757                         if (client->state == DHCP_STATE_REQUESTING)
758                                 notify_event = DHCP_EVENT_IP_ACQUIRE;
759                         else if (r != DHCP_EVENT_IP_ACQUIRE)
760                                 notify_event = r;
761
762                         client->state = DHCP_STATE_BOUND;
763                         client->attempt = 1;
764
765                         client->last_addr = client->lease->address;
766
767                         r = client_set_lease_timeouts(client, time_now);
768                         if (r < 0)
769                                 goto error;
770
771                         if (notify_event)
772                                 client_notify(client, notify_event);
773
774                         client->receive_message =
775                                 sd_event_source_unref(client->receive_message);
776                         close(client->fd);
777                         client->fd = -1;
778                 }
779
780                 r = 0;
781
782                 break;
783
784         case DHCP_STATE_INIT:
785         case DHCP_STATE_INIT_REBOOT:
786         case DHCP_STATE_REBOOTING:
787         case DHCP_STATE_BOUND:
788
789                 break;
790         }
791
792 error:
793         if (r < 0 || r == DHCP_EVENT_NO_LEASE)
794                 return client_stop(client, r);
795
796         return 0;
797 }
798
799 static int client_receive_message_raw(sd_event_source *s, int fd,
800                                       uint32_t revents, void *userdata) {
801         sd_dhcp_client *client = userdata;
802         uint8_t buf[sizeof(DHCPMessage) + DHCP_MIN_OPTIONS_SIZE];
803         int buflen = sizeof(buf);
804         int len, r = 0;
805         usec_t time_now;
806
807         assert(s);
808         assert(client);
809         assert(client->event);
810
811         len = read(fd, &buf, buflen);
812         if (len < 0)
813                 return 0;
814
815         r = sd_event_get_now_monotonic(client->event, &time_now);
816         if (r < 0)
817                 return client_stop(client, r);
818
819         return client_handle_message(client, (DHCPMessage *) buf, len,
820                                      time_now);
821 }
822
823 static int client_receive_message_udp(sd_event_source *s, int fd,
824                                       uint32_t revents, void *userdata) {
825         sd_dhcp_client *client = userdata;
826         uint8_t buf[sizeof(DHCPPacket) + DHCP_MIN_OPTIONS_SIZE];
827         int buflen = sizeof(buf);
828         int len, r = 0;
829         DHCPPacket *packet;
830         usec_t time_now;
831
832         assert(s);
833         assert(client);
834         assert(client->event);
835
836         len = read(fd, &buf, buflen);
837         if (len < 0)
838                 return 0;
839
840         packet = (DHCPPacket *) buf;
841
842         r = dhcp_packet_verify_headers(packet, BOOTREPLY, len);
843         if (r < 0)
844                 return r;
845
846         len -= DHCP_IP_UDP_SIZE;
847
848         r = sd_event_get_now_monotonic(client->event, &time_now);
849         if (r < 0)
850                 return client_stop(client, r);
851
852         return client_handle_message(client, &packet->dhcp, len, time_now);
853 }
854
855 int sd_dhcp_client_start(sd_dhcp_client *client) {
856         int r;
857
858         assert_return(client, -EINVAL);
859         assert_return(client->event, -EINVAL);
860         assert_return(client->index > 0, -EINVAL);
861         assert_return(client->state == DHCP_STATE_INIT ||
862                       client->state == DHCP_STATE_INIT_REBOOT, -EBUSY);
863
864         client->xid = random_u32();
865
866         r = dhcp_network_bind_raw_socket(client->index, &client->link);
867
868         if (r < 0) {
869                 client_stop(client, r);
870                 return r;
871         }
872
873         client->fd = r;
874         client->start_time = now(CLOCK_MONOTONIC);
875         client->secs = 0;
876
877         return client_initialize_events(client, client_receive_message_udp,
878                                         client->start_time);
879 }
880
881 int sd_dhcp_client_stop(sd_dhcp_client *client) {
882         return client_stop(client, DHCP_EVENT_STOP);
883 }
884
885 int sd_dhcp_client_attach_event(sd_dhcp_client *client, sd_event *event,
886                                 int priority) {
887         int r;
888
889         assert_return(client, -EINVAL);
890         assert_return(!client->event, -EBUSY);
891
892         if (event)
893                 client->event = sd_event_ref(event);
894         else {
895                 r = sd_event_default(&client->event);
896                 if (r < 0)
897                         return 0;
898         }
899
900         client->event_priority = priority;
901
902         return 0;
903 }
904
905 int sd_dhcp_client_detach_event(sd_dhcp_client *client) {
906         assert_return(client, -EINVAL);
907
908         client->event = sd_event_unref(client->event);
909
910         return 0;
911 }
912
913 sd_event *sd_dhcp_client_get_event(sd_dhcp_client *client) {
914         if (!client)
915                 return NULL;
916
917         return client->event;
918 }
919
920 void sd_dhcp_client_free(sd_dhcp_client *client) {
921         if (!client)
922                 return;
923
924         sd_dhcp_client_stop(client);
925         sd_dhcp_client_detach_event(client);
926
927         free(client->req_opts);
928         free(client);
929 }
930
931 DEFINE_TRIVIAL_CLEANUP_FUNC(sd_dhcp_client*, sd_dhcp_client_free);
932 #define _cleanup_dhcp_client_free_ _cleanup_(sd_dhcp_client_freep)
933
934 int sd_dhcp_client_new(sd_dhcp_client **ret) {
935         _cleanup_dhcp_client_free_ sd_dhcp_client *client = NULL;
936
937         assert_return(ret, -EINVAL);
938
939         client = new0(sd_dhcp_client, 1);
940         if (!client)
941                 return -ENOMEM;
942
943         client->state = DHCP_STATE_INIT;
944         client->index = -1;
945         client->fd = -1;
946         client->attempt = 1;
947
948         client->req_opts_size = ELEMENTSOF(default_req_opts);
949
950         client->req_opts = memdup(default_req_opts, client->req_opts_size);
951         if (!client->req_opts)
952                 return -ENOMEM;
953
954         *ret = client;
955         client = NULL;
956
957         return 0;
958 }