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