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, uint8_t **opt,
128 *opt = (uint8_t *)(message + 1);
134 message->op = BOOTREQUEST;
136 message->hlen = ETHER_ADDR_LEN;
137 message->xid = htobe32(client->xid);
139 memcpy(&message->chaddr, &client->mac_addr, ETH_ALEN);
147 err = dhcp_option_append(opt, optlen, DHCP_OPTION_MESSAGE_TYPE, 1,
152 /* Some DHCP servers will refuse to issue an DHCP lease if the Cliient
153 Identifier option is not set */
154 err = dhcp_option_append(opt, optlen, DHCP_OPTION_CLIENT_IDENTIFIER,
155 ETH_ALEN, &client->mac_addr);
159 if (type == DHCP_DISCOVER || type == DHCP_REQUEST) {
160 err = dhcp_option_append(opt, optlen,
161 DHCP_OPTION_PARAMETER_REQUEST_LIST,
162 client->req_opts_size,
171 static uint16_t client_checksum(void *buf, int len)
181 for (i = 0; i < len / 2 ; i++)
189 return ~((sum & 0xffff) + (sum >> 16));
192 static int client_send_discover(sd_dhcp_client *client)
195 _cleanup_free_ DHCPPacket *discover;
199 optlen = DHCP_CLIENT_MIN_OPTIONS_SIZE;
200 len = sizeof(DHCPPacket) + optlen;
202 discover = malloc0(len);
207 err = client_packet_init(client, DHCP_DISCOVER, &discover->dhcp,
212 if (client->last_addr != INADDR_ANY) {
213 err = dhcp_option_append(&opt, &optlen,
214 DHCP_OPTION_REQUESTED_IP_ADDRESS,
215 4, &client->last_addr);
220 err = dhcp_option_append(&opt, &optlen, DHCP_OPTION_END, 0, NULL);
224 discover->ip.version = IPVERSION;
225 discover->ip.ihl = sizeof(discover->ip) >> 2;
226 discover->ip.tot_len = htobe16(len);
228 discover->ip.protocol = IPPROTO_UDP;
229 discover->ip.saddr = INADDR_ANY;
230 discover->ip.daddr = INADDR_BROADCAST;
232 discover->udp.source = htobe16(DHCP_PORT_CLIENT);
233 discover->udp.dest = htobe16(DHCP_PORT_SERVER);
234 discover->udp.len = htobe16(len - sizeof(discover->ip));
236 discover->ip.check = discover->udp.len;
237 discover->udp.check = client_checksum(&discover->ip.ttl,
240 discover->ip.ttl = IPDEFTTL;
241 discover->ip.check = 0;
242 discover->ip.check = client_checksum(&discover->ip,
243 sizeof(discover->ip));
245 err = dhcp_network_send_raw_packet(client->index, discover, len);
250 int sd_dhcp_client_start(sd_dhcp_client *client)
252 assert_return(client, -EINVAL);
253 assert_return(client->index >= 0, -EINVAL);
254 assert_return(client->state == DHCP_STATE_INIT ||
255 client->state == DHCP_STATE_INIT_REBOOT, -EBUSY);
257 client->xid = random_u();
259 return client_send_discover(client);
262 sd_dhcp_client *sd_dhcp_client_new(void)
264 sd_dhcp_client *client;
266 client = new0(sd_dhcp_client, 1);
270 client->state = DHCP_STATE_INIT;
273 client->req_opts_size = ELEMENTSOF(default_req_opts);
275 client->req_opts = memdup(default_req_opts, client->req_opts_size);
276 if (!client->req_opts) {