2 This file is part of systemd.
4 Copyright (C) 2013 Intel Corporation. All rights reserved.
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.
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.
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/>.
24 #include <net/ethernet.h>
29 #include "dhcp-protocol.h"
30 #include "dhcp-internal.h"
31 #include "sd-dhcp-client.h"
33 #define DHCP_CLIENT_MIN_OPTIONS_SIZE 312
35 struct sd_dhcp_client {
41 struct ether_addr mac_addr;
45 static const uint8_t default_req_opts[] = {
46 DHCP_OPTION_SUBNET_MASK,
48 DHCP_OPTION_HOST_NAME,
49 DHCP_OPTION_DOMAIN_NAME,
50 DHCP_OPTION_DOMAIN_NAME_SERVER,
51 DHCP_OPTION_NTP_SERVER,
54 int sd_dhcp_client_set_request_option(sd_dhcp_client *client, uint8_t option)
58 assert_return(client, -EINVAL);
59 assert_return (client->state == DHCP_STATE_INIT, -EBUSY);
63 case DHCP_OPTION_OVERLOAD:
64 case DHCP_OPTION_MESSAGE_TYPE:
65 case DHCP_OPTION_PARAMETER_REQUEST_LIST:
73 for (i = 0; i < client->req_opts_size; i++)
74 if (client->req_opts[i] == option)
77 if (!GREEDY_REALLOC(client->req_opts, client->req_opts_size,
78 client->req_opts_size + 1))
81 client->req_opts[client->req_opts_size - 1] = option;
86 int sd_dhcp_client_set_request_address(sd_dhcp_client *client,
87 const struct in_addr *last_addr)
89 assert_return(client, -EINVAL);
90 assert_return(client->state == DHCP_STATE_INIT, -EBUSY);
93 client->last_addr = last_addr->s_addr;
95 client->last_addr = INADDR_ANY;
100 int sd_dhcp_client_set_index(sd_dhcp_client *client, int interface_index)
102 assert_return(client, -EINVAL);
103 assert_return(client->state == DHCP_STATE_INIT, -EBUSY);
104 assert_return(interface_index >= -1, -EINVAL);
106 client->index = interface_index;
111 int sd_dhcp_client_set_mac(sd_dhcp_client *client,
112 const struct ether_addr *addr)
114 assert_return(client, -EINVAL);
115 assert_return(client->state == DHCP_STATE_INIT, -EBUSY);
117 memcpy(&client->mac_addr, addr, ETH_ALEN);
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)
128 *opt = (uint8_t *)(message + 1);
134 message->op = BOOTREQUEST;
136 message->hlen = ETHER_ADDR_LEN;
137 message->xid = htobe32(client->xid);
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);
143 memcpy(&message->chaddr, &client->mac_addr, ETH_ALEN);
151 err = dhcp_option_append(opt, optlen, DHCP_OPTION_MESSAGE_TYPE, 1,
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);
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,
175 static uint16_t client_checksum(void *buf, int len)
185 for (i = 0; i < len / 2 ; i++)
193 return ~((sum & 0xffff) + (sum >> 16));
196 static int client_send_discover(sd_dhcp_client *client, uint16_t secs)
199 _cleanup_free_ DHCPPacket *discover;
203 optlen = DHCP_CLIENT_MIN_OPTIONS_SIZE;
204 len = sizeof(DHCPPacket) + optlen;
206 discover = malloc0(len);
211 err = client_packet_init(client, DHCP_DISCOVER, &discover->dhcp,
212 secs, &opt, &optlen);
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);
224 err = dhcp_option_append(&opt, &optlen, DHCP_OPTION_END, 0, NULL);
228 discover->ip.version = IPVERSION;
229 discover->ip.ihl = sizeof(discover->ip) >> 2;
230 discover->ip.tot_len = htobe16(len);
232 discover->ip.protocol = IPPROTO_UDP;
233 discover->ip.saddr = INADDR_ANY;
234 discover->ip.daddr = INADDR_BROADCAST;
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));
240 discover->ip.check = discover->udp.len;
241 discover->udp.check = client_checksum(&discover->ip.ttl,
244 discover->ip.ttl = IPDEFTTL;
245 discover->ip.check = 0;
246 discover->ip.check = client_checksum(&discover->ip,
247 sizeof(discover->ip));
249 err = dhcp_network_send_raw_packet(client->index, discover, len);
254 int sd_dhcp_client_start(sd_dhcp_client *client)
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);
261 client->xid = random_u();
263 return client_send_discover(client, 0);
266 sd_dhcp_client *sd_dhcp_client_new(void)
268 sd_dhcp_client *client;
270 client = new0(sd_dhcp_client, 1);
274 client->state = DHCP_STATE_INIT;
277 client->req_opts_size = ELEMENTSOF(default_req_opts);
279 client->req_opts = memdup(default_req_opts, client->req_opts_size);
280 if (!client->req_opts) {