chiark / gitweb /
e9c4700081c0168b16bb28329193d2d705876767
[elogind.git] / src / libsystemd-dhcp / dhcp-client.c
1 /***
2   This file is part of systemd.
3
4   Copyright (C) 2013 Intel Corporation. All rights reserved.
5
6   systemd is free software; you can redistribute it and/or modify it
7   under the terms of the GNU Lesser General Public License as published by
8   the Free Software Foundation; either version 2.1 of the License, or
9   (at your option) any later version.
10
11   systemd is distributed in the hope that it will be useful, but
12   WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14   Lesser General Public License for more details.
15
16   You should have received a copy of the GNU Lesser General Public License
17   along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include <stdlib.h>
21 #include <errno.h>
22 #include <string.h>
23 #include <stdio.h>
24 #include <net/ethernet.h>
25
26 #include "util.h"
27 #include "list.h"
28
29 #include "dhcp-protocol.h"
30 #include "dhcp-internal.h"
31 #include "sd-dhcp-client.h"
32
33 #define DHCP_CLIENT_MIN_OPTIONS_SIZE            312
34
35 struct sd_dhcp_client {
36         DHCPState state;
37         sd_event *event;
38         sd_event_source *timeout_resend;
39         int index;
40         uint8_t *req_opts;
41         size_t req_opts_size;
42         uint32_t last_addr;
43         struct ether_addr mac_addr;
44         uint32_t xid;
45         usec_t start_time;
46 };
47
48 static const uint8_t default_req_opts[] = {
49         DHCP_OPTION_SUBNET_MASK,
50         DHCP_OPTION_ROUTER,
51         DHCP_OPTION_HOST_NAME,
52         DHCP_OPTION_DOMAIN_NAME,
53         DHCP_OPTION_DOMAIN_NAME_SERVER,
54         DHCP_OPTION_NTP_SERVER,
55 };
56
57 int sd_dhcp_client_set_request_option(sd_dhcp_client *client, uint8_t option)
58 {
59         size_t i;
60
61         assert_return(client, -EINVAL);
62         assert_return (client->state == DHCP_STATE_INIT, -EBUSY);
63
64         switch(option) {
65         case DHCP_OPTION_PAD:
66         case DHCP_OPTION_OVERLOAD:
67         case DHCP_OPTION_MESSAGE_TYPE:
68         case DHCP_OPTION_PARAMETER_REQUEST_LIST:
69         case DHCP_OPTION_END:
70                 return -EINVAL;
71
72         default:
73                 break;
74         }
75
76         for (i = 0; i < client->req_opts_size; i++)
77                 if (client->req_opts[i] == option)
78                         return -EEXIST;
79
80         if (!GREEDY_REALLOC(client->req_opts, client->req_opts_size,
81                             client->req_opts_size + 1))
82                 return -ENOMEM;
83
84         client->req_opts[client->req_opts_size - 1] = option;
85
86         return 0;
87 }
88
89 int sd_dhcp_client_set_request_address(sd_dhcp_client *client,
90                                        const struct in_addr *last_addr)
91 {
92         assert_return(client, -EINVAL);
93         assert_return(client->state == DHCP_STATE_INIT, -EBUSY);
94
95         if (last_addr)
96                 client->last_addr = last_addr->s_addr;
97         else
98                 client->last_addr = INADDR_ANY;
99
100         return 0;
101 }
102
103 int sd_dhcp_client_set_index(sd_dhcp_client *client, int interface_index)
104 {
105         assert_return(client, -EINVAL);
106         assert_return(client->state == DHCP_STATE_INIT, -EBUSY);
107         assert_return(interface_index >= -1, -EINVAL);
108
109         client->index = interface_index;
110
111         return 0;
112 }
113
114 int sd_dhcp_client_set_mac(sd_dhcp_client *client,
115                            const struct ether_addr *addr)
116 {
117         assert_return(client, -EINVAL);
118         assert_return(client->state == DHCP_STATE_INIT, -EBUSY);
119
120         memcpy(&client->mac_addr, addr, ETH_ALEN);
121
122         return 0;
123 }
124
125 static int client_stop(sd_dhcp_client *client, int error)
126 {
127         assert_return(client, -EINVAL);
128         assert_return(client->state != DHCP_STATE_INIT &&
129                       client->state != DHCP_STATE_INIT_REBOOT, -EALREADY);
130
131         client->timeout_resend = sd_event_source_unref(client->timeout_resend);
132
133         switch (client->state) {
134
135         case DHCP_STATE_INIT:
136         case DHCP_STATE_SELECTING:
137
138                 client->state = DHCP_STATE_INIT;
139                 break;
140
141         case DHCP_STATE_INIT_REBOOT:
142         case DHCP_STATE_REBOOTING:
143         case DHCP_STATE_REQUESTING:
144         case DHCP_STATE_BOUND:
145         case DHCP_STATE_RENEWING:
146         case DHCP_STATE_REBINDING:
147
148                 break;
149         }
150
151         return 0;
152 }
153
154 static int client_packet_init(sd_dhcp_client *client, uint8_t type,
155                               DHCPMessage *message, uint16_t secs,
156                               uint8_t **opt, size_t *optlen)
157 {
158         int err;
159
160         *opt = (uint8_t *)(message + 1);
161
162         if (*optlen < 4)
163                 return -ENOBUFS;
164         *optlen -= 4;
165
166         message->op = BOOTREQUEST;
167         message->htype = 1;
168         message->hlen = ETHER_ADDR_LEN;
169         message->xid = htobe32(client->xid);
170
171         /* Although 'secs' field is a SHOULD in RFC 2131, certain DHCP servers
172            refuse to issue an DHCP lease if 'secs' is set to zero */
173         message->secs = htobe16(secs);
174
175         memcpy(&message->chaddr, &client->mac_addr, ETH_ALEN);
176         (*opt)[0] = 0x63;
177         (*opt)[1] = 0x82;
178         (*opt)[2] = 0x53;
179         (*opt)[3] = 0x63;
180
181         *opt += 4;
182
183         err = dhcp_option_append(opt, optlen, DHCP_OPTION_MESSAGE_TYPE, 1,
184                                  &type);
185         if (err < 0)
186                 return err;
187
188         /* Some DHCP servers will refuse to issue an DHCP lease if the Cliient
189            Identifier option is not set */
190         err = dhcp_option_append(opt, optlen, DHCP_OPTION_CLIENT_IDENTIFIER,
191                                  ETH_ALEN, &client->mac_addr);
192         if (err < 0)
193                 return err;
194
195         if (type == DHCP_DISCOVER || type == DHCP_REQUEST) {
196                 err = dhcp_option_append(opt, optlen,
197                                          DHCP_OPTION_PARAMETER_REQUEST_LIST,
198                                          client->req_opts_size,
199                                          client->req_opts);
200                 if (err < 0)
201                         return err;
202         }
203
204         return 0;
205 }
206
207 static uint16_t client_checksum(void *buf, int len)
208 {
209         uint32_t sum;
210         uint16_t *check;
211         int i;
212         uint8_t *odd;
213
214         sum = 0;
215         check = buf;
216
217         for (i = 0; i < len / 2 ; i++)
218                 sum += check[i];
219
220         if (len & 0x01) {
221                 odd = buf;
222                 sum += odd[len];
223         }
224
225         return ~((sum & 0xffff) + (sum >> 16));
226 }
227
228 static int client_send_discover(sd_dhcp_client *client, uint16_t secs)
229 {
230         int err = 0;
231         _cleanup_free_ DHCPPacket *discover;
232         size_t optlen, len;
233         uint8_t *opt;
234
235         optlen = DHCP_CLIENT_MIN_OPTIONS_SIZE;
236         len = sizeof(DHCPPacket) + optlen;
237
238         discover = malloc0(len);
239
240         if (!discover)
241                 return -ENOMEM;
242
243         err = client_packet_init(client, DHCP_DISCOVER, &discover->dhcp,
244                                  secs, &opt, &optlen);
245         if (err < 0)
246                 return err;
247
248         if (client->last_addr != INADDR_ANY) {
249                 err = dhcp_option_append(&opt, &optlen,
250                                          DHCP_OPTION_REQUESTED_IP_ADDRESS,
251                                          4, &client->last_addr);
252                 if (err < 0)
253                         return err;
254         }
255
256         err = dhcp_option_append(&opt, &optlen, DHCP_OPTION_END, 0, NULL);
257         if (err < 0)
258                 return err;
259
260         discover->ip.version = IPVERSION;
261         discover->ip.ihl = sizeof(discover->ip) >> 2;
262         discover->ip.tot_len = htobe16(len);
263
264         discover->ip.protocol = IPPROTO_UDP;
265         discover->ip.saddr = INADDR_ANY;
266         discover->ip.daddr = INADDR_BROADCAST;
267
268         discover->udp.source = htobe16(DHCP_PORT_CLIENT);
269         discover->udp.dest = htobe16(DHCP_PORT_SERVER);
270         discover->udp.len = htobe16(len - sizeof(discover->ip));
271
272         discover->ip.check = discover->udp.len;
273         discover->udp.check = client_checksum(&discover->ip.ttl,
274                                               len - 8);
275
276         discover->ip.ttl = IPDEFTTL;
277         discover->ip.check = 0;
278         discover->ip.check = client_checksum(&discover->ip,
279                                              sizeof(discover->ip));
280
281         err = dhcp_network_send_raw_packet(client->index, discover, len);
282
283         return 0;
284 }
285
286 static int client_timeout_resend(sd_event_source *s, uint64_t usec,
287                                  void *userdata)
288 {
289         sd_dhcp_client *client = userdata;
290         usec_t next_timeout;
291         uint16_t secs;
292         int err = 0;
293
294         switch (client->state) {
295         case DHCP_STATE_INIT:
296         case DHCP_STATE_SELECTING:
297
298                 if (!client->start_time)
299                         client->start_time = usec;
300
301                 secs = (usec - client->start_time) / USEC_PER_SEC;
302
303                 next_timeout = usec + 2 * USEC_PER_SEC + (random() & 0x1fffff);
304
305                 err = sd_event_add_monotonic(client->event, next_timeout,
306                                              10 * USEC_PER_MSEC,
307                                              client_timeout_resend, client,
308                                              &client->timeout_resend);
309                 if (err < 0)
310                         goto error;
311
312                 if (client_send_discover(client, secs) >= 0)
313                         client->state = DHCP_STATE_SELECTING;
314
315                 break;
316
317         case DHCP_STATE_INIT_REBOOT:
318         case DHCP_STATE_REBOOTING:
319         case DHCP_STATE_REQUESTING:
320         case DHCP_STATE_BOUND:
321         case DHCP_STATE_RENEWING:
322         case DHCP_STATE_REBINDING:
323
324                 break;
325         }
326
327         return 0;
328
329 error:
330         client_stop(client, err);
331
332         /* Errors were dealt with when stopping the client, don't spill
333            errors into the event loop handler */
334         return 0;
335 }
336
337 int sd_dhcp_client_start(sd_dhcp_client *client)
338 {
339         int err;
340
341         assert_return(client, -EINVAL);
342         assert_return(client->index >= 0, -EINVAL);
343         assert_return(client->state == DHCP_STATE_INIT ||
344                       client->state == DHCP_STATE_INIT_REBOOT, -EBUSY);
345
346         client->xid = random_u();
347
348         err = sd_event_add_monotonic(client->event, now(CLOCK_MONOTONIC), 0,
349                                      client_timeout_resend, client,
350                                      &client->timeout_resend);
351         if (err < 0)
352                 goto error;
353
354         return 0;
355
356 error:
357         client_stop(client, err);
358
359         return err;
360 }
361
362 int sd_dhcp_client_stop(sd_dhcp_client *client)
363 {
364         return client_stop(client, 0);
365 }
366
367 sd_dhcp_client *sd_dhcp_client_new(sd_event *event)
368 {
369         sd_dhcp_client *client;
370
371         assert_return(event, NULL);
372
373         client = new0(sd_dhcp_client, 1);
374         if (!client)
375                 return NULL;
376
377         client->event = sd_event_ref(event);
378         client->state = DHCP_STATE_INIT;
379         client->index = -1;
380
381         client->req_opts_size = ELEMENTSOF(default_req_opts);
382
383         client->req_opts = memdup(default_req_opts, client->req_opts_size);
384         if (!client->req_opts) {
385                 free(client);
386                 return NULL;
387         }
388
389         return client;
390 }