chiark / gitweb /
181c6f8348959bc9dbd43f72746a8bf6eadc1031
[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_packet_init(sd_dhcp_client *client, uint8_t type,
123                               DHCPMessage *message, uint16_t secs,
124                               uint8_t **opt, size_t *optlen)
125 {
126         int err;
127
128         *opt = (uint8_t *)(message + 1);
129
130         if (*optlen < 4)
131                 return -ENOBUFS;
132         *optlen -= 4;
133
134         message->op = BOOTREQUEST;
135         message->htype = 1;
136         message->hlen = ETHER_ADDR_LEN;
137         message->xid = htobe32(client->xid);
138
139         /* Although 'secs' field is a SHOULD in RFC 2131, certain DHCP servers
140            refuse to issue an DHCP lease if 'secs' is set to zero */
141         message->secs = htobe16(secs);
142
143         memcpy(&message->chaddr, &client->mac_addr, ETH_ALEN);
144         (*opt)[0] = 0x63;
145         (*opt)[1] = 0x82;
146         (*opt)[2] = 0x53;
147         (*opt)[3] = 0x63;
148
149         *opt += 4;
150
151         err = dhcp_option_append(opt, optlen, DHCP_OPTION_MESSAGE_TYPE, 1,
152                                  &type);
153         if (err < 0)
154                 return err;
155
156         /* Some DHCP servers will refuse to issue an DHCP lease if the Cliient
157            Identifier option is not set */
158         err = dhcp_option_append(opt, optlen, DHCP_OPTION_CLIENT_IDENTIFIER,
159                                  ETH_ALEN, &client->mac_addr);
160         if (err < 0)
161                 return err;
162
163         if (type == DHCP_DISCOVER || type == DHCP_REQUEST) {
164                 err = dhcp_option_append(opt, optlen,
165                                          DHCP_OPTION_PARAMETER_REQUEST_LIST,
166                                          client->req_opts_size,
167                                          client->req_opts);
168                 if (err < 0)
169                         return err;
170         }
171
172         return 0;
173 }
174
175 static uint16_t client_checksum(void *buf, int len)
176 {
177         uint32_t sum;
178         uint16_t *check;
179         int i;
180         uint8_t *odd;
181
182         sum = 0;
183         check = buf;
184
185         for (i = 0; i < len / 2 ; i++)
186                 sum += check[i];
187
188         if (len & 0x01) {
189                 odd = buf;
190                 sum += odd[len];
191         }
192
193         return ~((sum & 0xffff) + (sum >> 16));
194 }
195
196 static int client_send_discover(sd_dhcp_client *client, uint16_t secs)
197 {
198         int err = 0;
199         _cleanup_free_ DHCPPacket *discover;
200         size_t optlen, len;
201         uint8_t *opt;
202
203         optlen = DHCP_CLIENT_MIN_OPTIONS_SIZE;
204         len = sizeof(DHCPPacket) + optlen;
205
206         discover = malloc0(len);
207
208         if (!discover)
209                 return -ENOMEM;
210
211         err = client_packet_init(client, DHCP_DISCOVER, &discover->dhcp,
212                                  secs, &opt, &optlen);
213         if (err < 0)
214                 return err;
215
216         if (client->last_addr != INADDR_ANY) {
217                 err = dhcp_option_append(&opt, &optlen,
218                                          DHCP_OPTION_REQUESTED_IP_ADDRESS,
219                                          4, &client->last_addr);
220                 if (err < 0)
221                         return err;
222         }
223
224         err = dhcp_option_append(&opt, &optlen, DHCP_OPTION_END, 0, NULL);
225         if (err < 0)
226                 return err;
227
228         discover->ip.version = IPVERSION;
229         discover->ip.ihl = sizeof(discover->ip) >> 2;
230         discover->ip.tot_len = htobe16(len);
231
232         discover->ip.protocol = IPPROTO_UDP;
233         discover->ip.saddr = INADDR_ANY;
234         discover->ip.daddr = INADDR_BROADCAST;
235
236         discover->udp.source = htobe16(DHCP_PORT_CLIENT);
237         discover->udp.dest = htobe16(DHCP_PORT_SERVER);
238         discover->udp.len = htobe16(len - sizeof(discover->ip));
239
240         discover->ip.check = discover->udp.len;
241         discover->udp.check = client_checksum(&discover->ip.ttl,
242                                               len - 8);
243
244         discover->ip.ttl = IPDEFTTL;
245         discover->ip.check = 0;
246         discover->ip.check = client_checksum(&discover->ip,
247                                              sizeof(discover->ip));
248
249         err = dhcp_network_send_raw_packet(client->index, discover, len);
250
251         return 0;
252 }
253
254 int sd_dhcp_client_start(sd_dhcp_client *client)
255 {
256         assert_return(client, -EINVAL);
257         assert_return(client->index >= 0, -EINVAL);
258         assert_return(client->state == DHCP_STATE_INIT ||
259                       client->state == DHCP_STATE_INIT_REBOOT, -EBUSY);
260
261         client->xid = random_u();
262
263         return client_send_discover(client, 0);
264 }
265
266 sd_dhcp_client *sd_dhcp_client_new(void)
267 {
268         sd_dhcp_client *client;
269
270         client = new0(sd_dhcp_client, 1);
271         if (!client)
272                 return NULL;
273
274         client->state = DHCP_STATE_INIT;
275         client->index = -1;
276
277         client->req_opts_size = ELEMENTSOF(default_req_opts);
278
279         client->req_opts = memdup(default_req_opts, client->req_opts_size);
280         if (!client->req_opts) {
281                 free(client);
282                 return NULL;
283         }
284
285         return client;
286 }