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