chiark / gitweb /
bd2882476e6955a8b2da8cc6de73106be2362a46
[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         int index;
38         uint8_t *req_opts;
39         size_t req_opts_size;
40         uint32_t last_addr;
41         struct ether_addr mac_addr;
42         uint32_t xid;
43 };
44
45 static const uint8_t default_req_opts[] = {
46         DHCP_OPTION_SUBNET_MASK,
47         DHCP_OPTION_ROUTER,
48         DHCP_OPTION_HOST_NAME,
49         DHCP_OPTION_DOMAIN_NAME,
50         DHCP_OPTION_DOMAIN_NAME_SERVER,
51         DHCP_OPTION_NTP_SERVER,
52 };
53
54 int sd_dhcp_client_set_request_option(sd_dhcp_client *client, uint8_t option)
55 {
56         size_t i;
57
58         assert_return(client, -EINVAL);
59         assert_return (client->state == DHCP_STATE_INIT, -EBUSY);
60
61         switch(option) {
62         case DHCP_OPTION_PAD:
63         case DHCP_OPTION_OVERLOAD:
64         case DHCP_OPTION_MESSAGE_TYPE:
65         case DHCP_OPTION_PARAMETER_REQUEST_LIST:
66         case DHCP_OPTION_END:
67                 return -EINVAL;
68
69         default:
70                 break;
71         }
72
73         for (i = 0; i < client->req_opts_size; i++)
74                 if (client->req_opts[i] == option)
75                         return -EEXIST;
76
77         if (!GREEDY_REALLOC(client->req_opts, client->req_opts_size,
78                             client->req_opts_size + 1))
79                 return -ENOMEM;
80
81         client->req_opts[client->req_opts_size - 1] = option;
82
83         return 0;
84 }
85
86 int sd_dhcp_client_set_request_address(sd_dhcp_client *client,
87                                        const struct in_addr *last_addr)
88 {
89         assert_return(client, -EINVAL);
90         assert_return(client->state == DHCP_STATE_INIT, -EBUSY);
91
92         if (last_addr)
93                 client->last_addr = last_addr->s_addr;
94         else
95                 client->last_addr = INADDR_ANY;
96
97         return 0;
98 }
99
100 int sd_dhcp_client_set_index(sd_dhcp_client *client, int interface_index)
101 {
102         assert_return(client, -EINVAL);
103         assert_return(client->state == DHCP_STATE_INIT, -EBUSY);
104         assert_return(interface_index >= -1, -EINVAL);
105
106         client->index = interface_index;
107
108         return 0;
109 }
110
111 int sd_dhcp_client_set_mac(sd_dhcp_client *client,
112                            const struct ether_addr *addr)
113 {
114         assert_return(client, -EINVAL);
115         assert_return(client->state == DHCP_STATE_INIT, -EBUSY);
116
117         memcpy(&client->mac_addr, addr, ETH_ALEN);
118
119         return 0;
120 }
121
122 static int client_stop(sd_dhcp_client *client, int error)
123 {
124         assert_return(client, -EINVAL);
125         assert_return(client->state != DHCP_STATE_INIT &&
126                       client->state != DHCP_STATE_INIT_REBOOT, -EALREADY);
127
128         switch (client->state) {
129
130         case DHCP_STATE_INIT:
131         case DHCP_STATE_SELECTING:
132
133                 client->state = DHCP_STATE_INIT;
134                 break;
135
136         case DHCP_STATE_INIT_REBOOT:
137         case DHCP_STATE_REBOOTING:
138         case DHCP_STATE_REQUESTING:
139         case DHCP_STATE_BOUND:
140         case DHCP_STATE_RENEWING:
141         case DHCP_STATE_REBINDING:
142
143                 break;
144         }
145
146         return 0;
147 }
148
149 static int client_packet_init(sd_dhcp_client *client, uint8_t type,
150                               DHCPMessage *message, uint16_t secs,
151                               uint8_t **opt, size_t *optlen)
152 {
153         int err;
154
155         *opt = (uint8_t *)(message + 1);
156
157         if (*optlen < 4)
158                 return -ENOBUFS;
159         *optlen -= 4;
160
161         message->op = BOOTREQUEST;
162         message->htype = 1;
163         message->hlen = ETHER_ADDR_LEN;
164         message->xid = htobe32(client->xid);
165
166         /* Although 'secs' field is a SHOULD in RFC 2131, certain DHCP servers
167            refuse to issue an DHCP lease if 'secs' is set to zero */
168         message->secs = htobe16(secs);
169
170         memcpy(&message->chaddr, &client->mac_addr, ETH_ALEN);
171         (*opt)[0] = 0x63;
172         (*opt)[1] = 0x82;
173         (*opt)[2] = 0x53;
174         (*opt)[3] = 0x63;
175
176         *opt += 4;
177
178         err = dhcp_option_append(opt, optlen, DHCP_OPTION_MESSAGE_TYPE, 1,
179                                  &type);
180         if (err < 0)
181                 return err;
182
183         /* Some DHCP servers will refuse to issue an DHCP lease if the Cliient
184            Identifier option is not set */
185         err = dhcp_option_append(opt, optlen, DHCP_OPTION_CLIENT_IDENTIFIER,
186                                  ETH_ALEN, &client->mac_addr);
187         if (err < 0)
188                 return err;
189
190         if (type == DHCP_DISCOVER || type == DHCP_REQUEST) {
191                 err = dhcp_option_append(opt, optlen,
192                                          DHCP_OPTION_PARAMETER_REQUEST_LIST,
193                                          client->req_opts_size,
194                                          client->req_opts);
195                 if (err < 0)
196                         return err;
197         }
198
199         return 0;
200 }
201
202 static uint16_t client_checksum(void *buf, int len)
203 {
204         uint32_t sum;
205         uint16_t *check;
206         int i;
207         uint8_t *odd;
208
209         sum = 0;
210         check = buf;
211
212         for (i = 0; i < len / 2 ; i++)
213                 sum += check[i];
214
215         if (len & 0x01) {
216                 odd = buf;
217                 sum += odd[len];
218         }
219
220         return ~((sum & 0xffff) + (sum >> 16));
221 }
222
223 static int client_send_discover(sd_dhcp_client *client, uint16_t secs)
224 {
225         int err = 0;
226         _cleanup_free_ DHCPPacket *discover;
227         size_t optlen, len;
228         uint8_t *opt;
229
230         optlen = DHCP_CLIENT_MIN_OPTIONS_SIZE;
231         len = sizeof(DHCPPacket) + optlen;
232
233         discover = malloc0(len);
234
235         if (!discover)
236                 return -ENOMEM;
237
238         err = client_packet_init(client, DHCP_DISCOVER, &discover->dhcp,
239                                  secs, &opt, &optlen);
240         if (err < 0)
241                 return err;
242
243         if (client->last_addr != INADDR_ANY) {
244                 err = dhcp_option_append(&opt, &optlen,
245                                          DHCP_OPTION_REQUESTED_IP_ADDRESS,
246                                          4, &client->last_addr);
247                 if (err < 0)
248                         return err;
249         }
250
251         err = dhcp_option_append(&opt, &optlen, DHCP_OPTION_END, 0, NULL);
252         if (err < 0)
253                 return err;
254
255         discover->ip.version = IPVERSION;
256         discover->ip.ihl = sizeof(discover->ip) >> 2;
257         discover->ip.tot_len = htobe16(len);
258
259         discover->ip.protocol = IPPROTO_UDP;
260         discover->ip.saddr = INADDR_ANY;
261         discover->ip.daddr = INADDR_BROADCAST;
262
263         discover->udp.source = htobe16(DHCP_PORT_CLIENT);
264         discover->udp.dest = htobe16(DHCP_PORT_SERVER);
265         discover->udp.len = htobe16(len - sizeof(discover->ip));
266
267         discover->ip.check = discover->udp.len;
268         discover->udp.check = client_checksum(&discover->ip.ttl,
269                                               len - 8);
270
271         discover->ip.ttl = IPDEFTTL;
272         discover->ip.check = 0;
273         discover->ip.check = client_checksum(&discover->ip,
274                                              sizeof(discover->ip));
275
276         err = dhcp_network_send_raw_packet(client->index, discover, len);
277
278         return 0;
279 }
280
281 int sd_dhcp_client_start(sd_dhcp_client *client)
282 {
283         assert_return(client, -EINVAL);
284         assert_return(client->index >= 0, -EINVAL);
285         assert_return(client->state == DHCP_STATE_INIT ||
286                       client->state == DHCP_STATE_INIT_REBOOT, -EBUSY);
287
288         client->xid = random_u();
289
290         return client_send_discover(client, 0);
291 }
292
293 int sd_dhcp_client_stop(sd_dhcp_client *client)
294 {
295         return client_stop(client, 0);
296 }
297
298 sd_dhcp_client *sd_dhcp_client_new(void)
299 {
300         sd_dhcp_client *client;
301
302         client = new0(sd_dhcp_client, 1);
303         if (!client)
304                 return NULL;
305
306         client->state = DHCP_STATE_INIT;
307         client->index = -1;
308
309         client->req_opts_size = ELEMENTSOF(default_req_opts);
310
311         client->req_opts = memdup(default_req_opts, client->req_opts_size);
312         if (!client->req_opts) {
313                 free(client);
314                 return NULL;
315         }
316
317         return client;
318 }