blob: b9404537a1ce5ed118844628a85adb5c5df5f592 [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001/*
2 * Copyright 2008, The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/* Utilities for managing the dhcpcd DHCP client daemon */
18
19#include <stdio.h>
20#include <stdlib.h>
Olivier Baillyb93e5812010-11-17 11:47:23 -080021#include <string.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080022#include <unistd.h>
23#include <arpa/inet.h>
24#include <netinet/in.h>
25
26#include <cutils/properties.h>
27
Dmitry Shmidt9363b7d2009-12-14 15:41:54 -080028static const char DAEMON_NAME[] = "dhcpcd";
29static const char DAEMON_PROP_NAME[] = "init.svc.dhcpcd";
30static const char HOSTNAME_PROP_NAME[] = "net.hostname";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080031static const char DHCP_PROP_NAME_PREFIX[] = "dhcp";
Dmitry Shmidt62d6f742012-07-23 17:39:30 -070032static const char DHCP_CONFIG_PATH[] = "/system/etc/dhcpcd/dhcpcd.conf";
Irfan Sheriffb1723b62010-12-21 10:31:05 -080033static const int NAP_TIME = 200; /* wait for 200ms at a time */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080034 /* when polling for property values */
TK MUN9d157872011-02-23 18:58:36 +090035static const char DAEMON_NAME_RENEW[] = "iprenew";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080036static char errmsg[100];
Irfan Sheriff89f58cf2012-05-23 10:30:05 -070037/* interface length for dhcpcd daemon start (dhcpcd_<interface> as defined in init.rc file)
38 * or for filling up system properties dhcpcd.<interface>.ipaddress, dhcpcd.<interface>.dns1
39 * and other properties on a successful bind
40 */
41#define MAX_INTERFACE_LENGTH 25
42
43/*
44 * P2p interface names increase sequentially p2p-p2p0-1, p2p-p2p0-2.. after
45 * group formation. This does not work well with system properties which can quickly
46 * exhaust or for specifiying a dhcp start target in init which requires
47 * interface to be pre-defined in init.rc file.
48 *
49 * This function returns a common string p2p for all p2p interfaces.
50 */
51void get_p2p_interface_replacement(const char *interface, char *p2p_interface) {
52 /* Use p2p for any interface starting with p2p. */
53 if (strncmp(interface, "p2p",3) == 0) {
54 strncpy(p2p_interface, "p2p", MAX_INTERFACE_LENGTH);
55 } else {
56 strncpy(p2p_interface, interface, MAX_INTERFACE_LENGTH);
57 }
58}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080059
60/*
61 * Wait for a system property to be assigned a specified value.
62 * If desired_value is NULL, then just wait for the property to
63 * be created with any value. maxwait is the maximum amount of
64 * time in seconds to wait before giving up.
65 */
66static int wait_for_property(const char *name, const char *desired_value, int maxwait)
67{
68 char value[PROPERTY_VALUE_MAX] = {'\0'};
Irfan Sheriffb1723b62010-12-21 10:31:05 -080069 int maxnaps = (maxwait * 1000) / NAP_TIME;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080070
71 if (maxnaps < 1) {
72 maxnaps = 1;
73 }
74
75 while (maxnaps-- > 0) {
Irfan Sheriffb1723b62010-12-21 10:31:05 -080076 usleep(NAP_TIME * 1000);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080077 if (property_get(name, value, NULL)) {
78 if (desired_value == NULL ||
79 strcmp(value, desired_value) == 0) {
80 return 0;
81 }
82 }
83 }
84 return -1; /* failure */
85}
86
Robert Greenwaltfaab26d2011-01-14 14:35:42 -080087static int fill_ip_info(const char *interface,
88 char *ipaddr,
89 char *gateway,
90 uint32_t *prefixLength,
91 char *dns1,
92 char *dns2,
93 char *server,
Jeff Sharkey0fb8ec82012-04-18 21:54:55 -070094 uint32_t *lease,
95 char *vendorInfo)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080096{
97 char prop_name[PROPERTY_KEY_MAX];
98 char prop_value[PROPERTY_VALUE_MAX];
Irfan Sheriff89f58cf2012-05-23 10:30:05 -070099 /* Interface name after converting p2p0-p2p0-X to p2p to reuse system properties */
100 char p2p_interface[MAX_INTERFACE_LENGTH];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800101
Irfan Sheriff89f58cf2012-05-23 10:30:05 -0700102 get_p2p_interface_replacement(interface, p2p_interface);
103
104 snprintf(prop_name, sizeof(prop_name), "%s.%s.ipaddress", DHCP_PROP_NAME_PREFIX, p2p_interface);
Robert Greenwaltfaab26d2011-01-14 14:35:42 -0800105 property_get(prop_name, ipaddr, NULL);
106
Irfan Sheriff89f58cf2012-05-23 10:30:05 -0700107 snprintf(prop_name, sizeof(prop_name), "%s.%s.gateway", DHCP_PROP_NAME_PREFIX, p2p_interface);
Robert Greenwaltfaab26d2011-01-14 14:35:42 -0800108 property_get(prop_name, gateway, NULL);
109
Irfan Sheriff89f58cf2012-05-23 10:30:05 -0700110 snprintf(prop_name, sizeof(prop_name), "%s.%s.server", DHCP_PROP_NAME_PREFIX, p2p_interface);
Irfan Sheriffbdaaec12011-04-15 16:04:24 -0700111 property_get(prop_name, server, NULL);
112
113 //TODO: Handle IPv6 when we change system property usage
114 if (strcmp(gateway, "0.0.0.0") == 0) {
115 //DHCP server is our best bet as gateway
116 strncpy(gateway, server, PROPERTY_VALUE_MAX);
117 }
118
Irfan Sheriff89f58cf2012-05-23 10:30:05 -0700119 snprintf(prop_name, sizeof(prop_name), "%s.%s.mask", DHCP_PROP_NAME_PREFIX, p2p_interface);
Robert Greenwaltfaab26d2011-01-14 14:35:42 -0800120 if (property_get(prop_name, prop_value, NULL)) {
121 int p;
122 // this conversion is v4 only, but this dhcp client is v4 only anyway
123 in_addr_t mask = ntohl(inet_addr(prop_value));
124 // Check netmask is a valid IP address. ntohl gives NONE response (all 1's) for
125 // non 255.255.255.255 inputs. if we get that value check if it is legit..
126 if (mask == INADDR_NONE && strcmp(prop_value, "255.255.255.255") != 0) {
127 snprintf(errmsg, sizeof(errmsg), "DHCP gave invalid net mask %s", prop_value);
128 return -1;
129 }
130 for (p = 0; p < 32; p++) {
131 if (mask == 0) break;
132 // check for non-contiguous netmask, e.g., 255.254.255.0
133 if ((mask & 0x80000000) == 0) {
134 snprintf(errmsg, sizeof(errmsg), "DHCP gave invalid net mask %s", prop_value);
135 return -1;
136 }
137 mask = mask << 1;
138 }
139 *prefixLength = p;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800140 }
Irfan Sheriff89f58cf2012-05-23 10:30:05 -0700141 snprintf(prop_name, sizeof(prop_name), "%s.%s.dns1", DHCP_PROP_NAME_PREFIX, p2p_interface);
Robert Greenwaltfaab26d2011-01-14 14:35:42 -0800142 property_get(prop_name, dns1, NULL);
143
Irfan Sheriff89f58cf2012-05-23 10:30:05 -0700144 snprintf(prop_name, sizeof(prop_name), "%s.%s.dns2", DHCP_PROP_NAME_PREFIX, p2p_interface);
Robert Greenwaltfaab26d2011-01-14 14:35:42 -0800145 property_get(prop_name, dns2, NULL);
146
Irfan Sheriff89f58cf2012-05-23 10:30:05 -0700147 snprintf(prop_name, sizeof(prop_name), "%s.%s.leasetime", DHCP_PROP_NAME_PREFIX, p2p_interface);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800148 if (property_get(prop_name, prop_value, NULL)) {
149 *lease = atol(prop_value);
150 }
Jeff Sharkey0fb8ec82012-04-18 21:54:55 -0700151
Irfan Sheriff89f58cf2012-05-23 10:30:05 -0700152 snprintf(prop_name, sizeof(prop_name), "%s.%s.vendorInfo", DHCP_PROP_NAME_PREFIX,
153 p2p_interface);
Jeff Sharkey0fb8ec82012-04-18 21:54:55 -0700154 property_get(prop_name, vendorInfo, NULL);
155
Robert Greenwaltfaab26d2011-01-14 14:35:42 -0800156 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800157}
158
Szymon Jakubczak8c85a002010-06-09 16:11:09 -0400159static const char *ipaddr_to_string(in_addr_t addr)
160{
161 struct in_addr in_addr;
162
163 in_addr.s_addr = addr;
164 return inet_ntoa(in_addr);
165}
166
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800167/*
168 * Start the dhcp client daemon, and wait for it to finish
169 * configuring the interface.
Irfan Sheriff35c28602011-11-09 11:10:50 -0800170 *
171 * The device init.rc file needs a corresponding entry for this work.
172 *
173 * Example:
Dmitry Shmidt62d6f742012-07-23 17:39:30 -0700174 * service dhcpcd_<interface> /system/bin/dhcpcd -ABKL -f dhcpcd.conf
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800175 */
176int dhcp_do_request(const char *interface,
Robert Greenwaltfaab26d2011-01-14 14:35:42 -0800177 char *ipaddr,
178 char *gateway,
179 uint32_t *prefixLength,
180 char *dns1,
181 char *dns2,
182 char *server,
Jeff Sharkey0fb8ec82012-04-18 21:54:55 -0700183 uint32_t *lease,
184 char *vendorInfo)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800185{
186 char result_prop_name[PROPERTY_KEY_MAX];
TK MUN9d157872011-02-23 18:58:36 +0900187 char daemon_prop_name[PROPERTY_KEY_MAX];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800188 char prop_value[PROPERTY_VALUE_MAX] = {'\0'};
Dmitry Shmidt62d6f742012-07-23 17:39:30 -0700189 char daemon_cmd[PROPERTY_VALUE_MAX * 2 + sizeof(DHCP_CONFIG_PATH)];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800190 const char *ctrl_prop = "ctl.start";
191 const char *desired_status = "running";
Irfan Sheriff89f58cf2012-05-23 10:30:05 -0700192 /* Interface name after converting p2p0-p2p0-X to p2p to reuse system properties */
193 char p2p_interface[MAX_INTERFACE_LENGTH];
repo synca329b422011-07-29 12:07:34 -0700194
Irfan Sheriff89f58cf2012-05-23 10:30:05 -0700195 get_p2p_interface_replacement(interface, p2p_interface);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800196
197 snprintf(result_prop_name, sizeof(result_prop_name), "%s.%s.result",
198 DHCP_PROP_NAME_PREFIX,
Irfan Sheriff89f58cf2012-05-23 10:30:05 -0700199 p2p_interface);
TK MUN9d157872011-02-23 18:58:36 +0900200
201 snprintf(daemon_prop_name, sizeof(daemon_prop_name), "%s_%s",
202 DAEMON_PROP_NAME,
Irfan Sheriff89f58cf2012-05-23 10:30:05 -0700203 p2p_interface);
TK MUN9d157872011-02-23 18:58:36 +0900204
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800205 /* Erase any previous setting of the dhcp result property */
206 property_set(result_prop_name, "");
207
208 /* Start the daemon and wait until it's ready */
Dmitry Shmidt9363b7d2009-12-14 15:41:54 -0800209 if (property_get(HOSTNAME_PROP_NAME, prop_value, NULL) && (prop_value[0] != '\0'))
Dmitry Shmidt62d6f742012-07-23 17:39:30 -0700210 snprintf(daemon_cmd, sizeof(daemon_cmd), "%s_%s:-f %s -h %s %s", DAEMON_NAME,
211 p2p_interface, DHCP_CONFIG_PATH, prop_value, interface);
Dmitry Shmidt9363b7d2009-12-14 15:41:54 -0800212 else
Dmitry Shmidt62d6f742012-07-23 17:39:30 -0700213 snprintf(daemon_cmd, sizeof(daemon_cmd), "%s_%s:-f %s %s", DAEMON_NAME,
Matt Gumbelba2ba5c2013-01-04 09:53:42 -0800214 p2p_interface, DHCP_CONFIG_PATH, interface);
Dmitry Shmidt9363b7d2009-12-14 15:41:54 -0800215 memset(prop_value, '\0', PROPERTY_VALUE_MAX);
216 property_set(ctrl_prop, daemon_cmd);
TK MUN9d157872011-02-23 18:58:36 +0900217 if (wait_for_property(daemon_prop_name, desired_status, 10) < 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800218 snprintf(errmsg, sizeof(errmsg), "%s", "Timed out waiting for dhcpcd to start");
219 return -1;
220 }
221
222 /* Wait for the daemon to return a result */
223 if (wait_for_property(result_prop_name, NULL, 30) < 0) {
224 snprintf(errmsg, sizeof(errmsg), "%s", "Timed out waiting for DHCP to finish");
225 return -1;
226 }
227
228 if (!property_get(result_prop_name, prop_value, NULL)) {
229 /* shouldn't ever happen, given the success of wait_for_property() */
230 snprintf(errmsg, sizeof(errmsg), "%s", "DHCP result property was not set");
231 return -1;
232 }
233 if (strcmp(prop_value, "ok") == 0) {
Szymon Jakubczak8c85a002010-06-09 16:11:09 -0400234 char dns_prop_name[PROPERTY_KEY_MAX];
Jeff Sharkey0fb8ec82012-04-18 21:54:55 -0700235 if (fill_ip_info(interface, ipaddr, gateway, prefixLength,
236 dns1, dns2, server, lease, vendorInfo) == -1) {
Robert Greenwaltfaab26d2011-01-14 14:35:42 -0800237 return -1;
238 }
239
240 /* copy dns data to system properties - TODO - remove this after we have async
241 * notification of renewal's */
Szymon Jakubczak8c85a002010-06-09 16:11:09 -0400242 snprintf(dns_prop_name, sizeof(dns_prop_name), "net.%s.dns1", interface);
243 property_set(dns_prop_name, *dns1 ? ipaddr_to_string(*dns1) : "");
244 snprintf(dns_prop_name, sizeof(dns_prop_name), "net.%s.dns2", interface);
245 property_set(dns_prop_name, *dns2 ? ipaddr_to_string(*dns2) : "");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800246 return 0;
247 } else {
248 snprintf(errmsg, sizeof(errmsg), "DHCP result was %s", prop_value);
249 return -1;
250 }
251}
252
253/**
254 * Stop the DHCP client daemon.
255 */
256int dhcp_stop(const char *interface)
257{
258 char result_prop_name[PROPERTY_KEY_MAX];
TK MUN9d157872011-02-23 18:58:36 +0900259 char daemon_prop_name[PROPERTY_KEY_MAX];
260 char daemon_cmd[PROPERTY_VALUE_MAX * 2];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800261 const char *ctrl_prop = "ctl.stop";
262 const char *desired_status = "stopped";
263
Irfan Sheriff89f58cf2012-05-23 10:30:05 -0700264 char p2p_interface[MAX_INTERFACE_LENGTH];
repo synca329b422011-07-29 12:07:34 -0700265
Irfan Sheriff89f58cf2012-05-23 10:30:05 -0700266 get_p2p_interface_replacement(interface, p2p_interface);
repo synca329b422011-07-29 12:07:34 -0700267
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800268 snprintf(result_prop_name, sizeof(result_prop_name), "%s.%s.result",
269 DHCP_PROP_NAME_PREFIX,
Irfan Sheriff89f58cf2012-05-23 10:30:05 -0700270 p2p_interface);
TK MUN9d157872011-02-23 18:58:36 +0900271
272 snprintf(daemon_prop_name, sizeof(daemon_prop_name), "%s_%s",
273 DAEMON_PROP_NAME,
Irfan Sheriff89f58cf2012-05-23 10:30:05 -0700274 p2p_interface);
TK MUN9d157872011-02-23 18:58:36 +0900275
Irfan Sheriff89f58cf2012-05-23 10:30:05 -0700276 snprintf(daemon_cmd, sizeof(daemon_cmd), "%s_%s", DAEMON_NAME, p2p_interface);
TK MUN9d157872011-02-23 18:58:36 +0900277
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800278 /* Stop the daemon and wait until it's reported to be stopped */
TK MUN9d157872011-02-23 18:58:36 +0900279 property_set(ctrl_prop, daemon_cmd);
280 if (wait_for_property(daemon_prop_name, desired_status, 5) < 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800281 return -1;
282 }
283 property_set(result_prop_name, "failed");
284 return 0;
285}
286
287/**
288 * Release the current DHCP client lease.
289 */
290int dhcp_release_lease(const char *interface)
291{
TK MUN9d157872011-02-23 18:58:36 +0900292 char daemon_prop_name[PROPERTY_KEY_MAX];
293 char daemon_cmd[PROPERTY_VALUE_MAX * 2];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800294 const char *ctrl_prop = "ctl.stop";
295 const char *desired_status = "stopped";
296
Irfan Sheriff89f58cf2012-05-23 10:30:05 -0700297 char p2p_interface[MAX_INTERFACE_LENGTH];
repo synca329b422011-07-29 12:07:34 -0700298
Irfan Sheriff89f58cf2012-05-23 10:30:05 -0700299 get_p2p_interface_replacement(interface, p2p_interface);
repo synca329b422011-07-29 12:07:34 -0700300
TK MUN9d157872011-02-23 18:58:36 +0900301 snprintf(daemon_prop_name, sizeof(daemon_prop_name), "%s_%s",
302 DAEMON_PROP_NAME,
Irfan Sheriff89f58cf2012-05-23 10:30:05 -0700303 p2p_interface);
TK MUN9d157872011-02-23 18:58:36 +0900304
Irfan Sheriff89f58cf2012-05-23 10:30:05 -0700305 snprintf(daemon_cmd, sizeof(daemon_cmd), "%s_%s", DAEMON_NAME, p2p_interface);
TK MUN9d157872011-02-23 18:58:36 +0900306
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800307 /* Stop the daemon and wait until it's reported to be stopped */
TK MUN9d157872011-02-23 18:58:36 +0900308 property_set(ctrl_prop, daemon_cmd);
309 if (wait_for_property(daemon_prop_name, desired_status, 5) < 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800310 return -1;
311 }
312 return 0;
313}
314
315char *dhcp_get_errmsg() {
316 return errmsg;
317}
TK MUN9d157872011-02-23 18:58:36 +0900318
319/**
Irfan Sheriff35c28602011-11-09 11:10:50 -0800320 * The device init.rc file needs a corresponding entry.
321 *
322 * Example:
323 * service iprenew_<interface> /system/bin/dhcpcd -n
324 *
TK MUN9d157872011-02-23 18:58:36 +0900325 */
326int dhcp_do_request_renew(const char *interface,
Irfan Sheriff89f58cf2012-05-23 10:30:05 -0700327 char *ipaddr,
328 char *gateway,
tk.muned216332011-10-13 22:58:55 +0900329 uint32_t *prefixLength,
Irfan Sheriff89f58cf2012-05-23 10:30:05 -0700330 char *dns1,
331 char *dns2,
332 char *server,
Jeff Sharkey0fb8ec82012-04-18 21:54:55 -0700333 uint32_t *lease,
334 char *vendorInfo)
TK MUN9d157872011-02-23 18:58:36 +0900335{
336 char result_prop_name[PROPERTY_KEY_MAX];
337 char prop_value[PROPERTY_VALUE_MAX] = {'\0'};
338 char daemon_cmd[PROPERTY_VALUE_MAX * 2];
339 const char *ctrl_prop = "ctl.start";
340
Irfan Sheriff89f58cf2012-05-23 10:30:05 -0700341 char p2p_interface[MAX_INTERFACE_LENGTH];
repo synca329b422011-07-29 12:07:34 -0700342
Irfan Sheriff89f58cf2012-05-23 10:30:05 -0700343 get_p2p_interface_replacement(interface, p2p_interface);
repo synca329b422011-07-29 12:07:34 -0700344
TK MUN9d157872011-02-23 18:58:36 +0900345 snprintf(result_prop_name, sizeof(result_prop_name), "%s.%s.result",
346 DHCP_PROP_NAME_PREFIX,
Irfan Sheriff89f58cf2012-05-23 10:30:05 -0700347 p2p_interface);
TK MUN9d157872011-02-23 18:58:36 +0900348
349 /* Erase any previous setting of the dhcp result property */
350 property_set(result_prop_name, "");
351
352 /* Start the renew daemon and wait until it's ready */
repo synca329b422011-07-29 12:07:34 -0700353 snprintf(daemon_cmd, sizeof(daemon_cmd), "%s_%s:%s", DAEMON_NAME_RENEW,
Irfan Sheriff89f58cf2012-05-23 10:30:05 -0700354 p2p_interface, interface);
TK MUN9d157872011-02-23 18:58:36 +0900355 memset(prop_value, '\0', PROPERTY_VALUE_MAX);
356 property_set(ctrl_prop, daemon_cmd);
357
358 /* Wait for the daemon to return a result */
359 if (wait_for_property(result_prop_name, NULL, 30) < 0) {
360 snprintf(errmsg, sizeof(errmsg), "%s", "Timed out waiting for DHCP Renew to finish");
361 return -1;
362 }
363
364 if (!property_get(result_prop_name, prop_value, NULL)) {
365 /* shouldn't ever happen, given the success of wait_for_property() */
366 snprintf(errmsg, sizeof(errmsg), "%s", "DHCP Renew result property was not set");
367 return -1;
368 }
369 if (strcmp(prop_value, "ok") == 0) {
Michael Wu897df502013-06-05 08:42:22 +0800370 return fill_ip_info(interface, ipaddr, gateway, prefixLength,
Jeff Sharkey0fb8ec82012-04-18 21:54:55 -0700371 dns1, dns2, server, lease, vendorInfo);
TK MUN9d157872011-02-23 18:58:36 +0900372 } else {
373 snprintf(errmsg, sizeof(errmsg), "DHCP Renew result was %s", prop_value);
374 return -1;
375 }
376}