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