blob: a220967814ab2919e772c7d5e8338acf784d2ecf [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,
Robert Greenwalt6ecbdca2012-11-13 10:56:01 -080095 char *vendorInfo,
96 char *domain)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080097{
98 char prop_name[PROPERTY_KEY_MAX];
99 char prop_value[PROPERTY_VALUE_MAX];
Irfan Sheriff89f58cf2012-05-23 10:30:05 -0700100 /* Interface name after converting p2p0-p2p0-X to p2p to reuse system properties */
101 char p2p_interface[MAX_INTERFACE_LENGTH];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800102
Irfan Sheriff89f58cf2012-05-23 10:30:05 -0700103 get_p2p_interface_replacement(interface, p2p_interface);
104
105 snprintf(prop_name, sizeof(prop_name), "%s.%s.ipaddress", DHCP_PROP_NAME_PREFIX, p2p_interface);
Robert Greenwaltfaab26d2011-01-14 14:35:42 -0800106 property_get(prop_name, ipaddr, NULL);
107
Irfan Sheriff89f58cf2012-05-23 10:30:05 -0700108 snprintf(prop_name, sizeof(prop_name), "%s.%s.gateway", DHCP_PROP_NAME_PREFIX, p2p_interface);
Robert Greenwaltfaab26d2011-01-14 14:35:42 -0800109 property_get(prop_name, gateway, NULL);
110
Irfan Sheriff89f58cf2012-05-23 10:30:05 -0700111 snprintf(prop_name, sizeof(prop_name), "%s.%s.server", DHCP_PROP_NAME_PREFIX, p2p_interface);
Irfan Sheriffbdaaec12011-04-15 16:04:24 -0700112 property_get(prop_name, server, NULL);
113
114 //TODO: Handle IPv6 when we change system property usage
115 if (strcmp(gateway, "0.0.0.0") == 0) {
116 //DHCP server is our best bet as gateway
117 strncpy(gateway, server, PROPERTY_VALUE_MAX);
118 }
119
Irfan Sheriff89f58cf2012-05-23 10:30:05 -0700120 snprintf(prop_name, sizeof(prop_name), "%s.%s.mask", DHCP_PROP_NAME_PREFIX, p2p_interface);
Robert Greenwaltfaab26d2011-01-14 14:35:42 -0800121 if (property_get(prop_name, prop_value, NULL)) {
122 int p;
123 // this conversion is v4 only, but this dhcp client is v4 only anyway
124 in_addr_t mask = ntohl(inet_addr(prop_value));
125 // Check netmask is a valid IP address. ntohl gives NONE response (all 1's) for
126 // non 255.255.255.255 inputs. if we get that value check if it is legit..
127 if (mask == INADDR_NONE && strcmp(prop_value, "255.255.255.255") != 0) {
128 snprintf(errmsg, sizeof(errmsg), "DHCP gave invalid net mask %s", prop_value);
129 return -1;
130 }
131 for (p = 0; p < 32; p++) {
132 if (mask == 0) break;
133 // check for non-contiguous netmask, e.g., 255.254.255.0
134 if ((mask & 0x80000000) == 0) {
135 snprintf(errmsg, sizeof(errmsg), "DHCP gave invalid net mask %s", prop_value);
136 return -1;
137 }
138 mask = mask << 1;
139 }
140 *prefixLength = p;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800141 }
Irfan Sheriff89f58cf2012-05-23 10:30:05 -0700142 snprintf(prop_name, sizeof(prop_name), "%s.%s.dns1", DHCP_PROP_NAME_PREFIX, p2p_interface);
Robert Greenwaltfaab26d2011-01-14 14:35:42 -0800143 property_get(prop_name, dns1, NULL);
144
Irfan Sheriff89f58cf2012-05-23 10:30:05 -0700145 snprintf(prop_name, sizeof(prop_name), "%s.%s.dns2", DHCP_PROP_NAME_PREFIX, p2p_interface);
Robert Greenwaltfaab26d2011-01-14 14:35:42 -0800146 property_get(prop_name, dns2, NULL);
147
Irfan Sheriff89f58cf2012-05-23 10:30:05 -0700148 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 -0800149 if (property_get(prop_name, prop_value, NULL)) {
150 *lease = atol(prop_value);
151 }
Jeff Sharkey0fb8ec82012-04-18 21:54:55 -0700152
Irfan Sheriff89f58cf2012-05-23 10:30:05 -0700153 snprintf(prop_name, sizeof(prop_name), "%s.%s.vendorInfo", DHCP_PROP_NAME_PREFIX,
154 p2p_interface);
Jeff Sharkey0fb8ec82012-04-18 21:54:55 -0700155 property_get(prop_name, vendorInfo, NULL);
156
Robert Greenwalt6ecbdca2012-11-13 10:56:01 -0800157 snprintf(prop_name, sizeof(prop_name), "%s.%s.domain", DHCP_PROP_NAME_PREFIX,
158 p2p_interface);
159 property_get(prop_name, domain, NULL);
160
Robert Greenwaltfaab26d2011-01-14 14:35:42 -0800161 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800162}
163
Szymon Jakubczak8c85a002010-06-09 16:11:09 -0400164static const char *ipaddr_to_string(in_addr_t addr)
165{
166 struct in_addr in_addr;
167
168 in_addr.s_addr = addr;
169 return inet_ntoa(in_addr);
170}
171
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800172/*
173 * Start the dhcp client daemon, and wait for it to finish
174 * configuring the interface.
Irfan Sheriff35c28602011-11-09 11:10:50 -0800175 *
176 * The device init.rc file needs a corresponding entry for this work.
177 *
178 * Example:
Dmitry Shmidt62d6f742012-07-23 17:39:30 -0700179 * service dhcpcd_<interface> /system/bin/dhcpcd -ABKL -f dhcpcd.conf
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800180 */
181int dhcp_do_request(const char *interface,
Robert Greenwaltfaab26d2011-01-14 14:35:42 -0800182 char *ipaddr,
183 char *gateway,
184 uint32_t *prefixLength,
185 char *dns1,
186 char *dns2,
187 char *server,
Jeff Sharkey0fb8ec82012-04-18 21:54:55 -0700188 uint32_t *lease,
Robert Greenwalt6ecbdca2012-11-13 10:56:01 -0800189 char *vendorInfo,
190 char *domain)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800191{
192 char result_prop_name[PROPERTY_KEY_MAX];
TK MUN9d157872011-02-23 18:58:36 +0900193 char daemon_prop_name[PROPERTY_KEY_MAX];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800194 char prop_value[PROPERTY_VALUE_MAX] = {'\0'};
Dmitry Shmidt62d6f742012-07-23 17:39:30 -0700195 char daemon_cmd[PROPERTY_VALUE_MAX * 2 + sizeof(DHCP_CONFIG_PATH)];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800196 const char *ctrl_prop = "ctl.start";
197 const char *desired_status = "running";
Irfan Sheriff89f58cf2012-05-23 10:30:05 -0700198 /* Interface name after converting p2p0-p2p0-X to p2p to reuse system properties */
199 char p2p_interface[MAX_INTERFACE_LENGTH];
repo synca329b422011-07-29 12:07:34 -0700200
Irfan Sheriff89f58cf2012-05-23 10:30:05 -0700201 get_p2p_interface_replacement(interface, p2p_interface);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800202
203 snprintf(result_prop_name, sizeof(result_prop_name), "%s.%s.result",
204 DHCP_PROP_NAME_PREFIX,
Irfan Sheriff89f58cf2012-05-23 10:30:05 -0700205 p2p_interface);
TK MUN9d157872011-02-23 18:58:36 +0900206
207 snprintf(daemon_prop_name, sizeof(daemon_prop_name), "%s_%s",
208 DAEMON_PROP_NAME,
Irfan Sheriff89f58cf2012-05-23 10:30:05 -0700209 p2p_interface);
TK MUN9d157872011-02-23 18:58:36 +0900210
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800211 /* Erase any previous setting of the dhcp result property */
212 property_set(result_prop_name, "");
213
214 /* Start the daemon and wait until it's ready */
Dmitry Shmidt9363b7d2009-12-14 15:41:54 -0800215 if (property_get(HOSTNAME_PROP_NAME, prop_value, NULL) && (prop_value[0] != '\0'))
Dmitry Shmidt62d6f742012-07-23 17:39:30 -0700216 snprintf(daemon_cmd, sizeof(daemon_cmd), "%s_%s:-f %s -h %s %s", DAEMON_NAME,
217 p2p_interface, DHCP_CONFIG_PATH, prop_value, interface);
Dmitry Shmidt9363b7d2009-12-14 15:41:54 -0800218 else
Dmitry Shmidt62d6f742012-07-23 17:39:30 -0700219 snprintf(daemon_cmd, sizeof(daemon_cmd), "%s_%s:-f %s %s", DAEMON_NAME,
220 DHCP_CONFIG_PATH, p2p_interface, interface);
Dmitry Shmidt9363b7d2009-12-14 15:41:54 -0800221 memset(prop_value, '\0', PROPERTY_VALUE_MAX);
222 property_set(ctrl_prop, daemon_cmd);
TK MUN9d157872011-02-23 18:58:36 +0900223 if (wait_for_property(daemon_prop_name, desired_status, 10) < 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800224 snprintf(errmsg, sizeof(errmsg), "%s", "Timed out waiting for dhcpcd to start");
225 return -1;
226 }
227
228 /* Wait for the daemon to return a result */
229 if (wait_for_property(result_prop_name, NULL, 30) < 0) {
230 snprintf(errmsg, sizeof(errmsg), "%s", "Timed out waiting for DHCP to finish");
231 return -1;
232 }
233
234 if (!property_get(result_prop_name, prop_value, NULL)) {
235 /* shouldn't ever happen, given the success of wait_for_property() */
236 snprintf(errmsg, sizeof(errmsg), "%s", "DHCP result property was not set");
237 return -1;
238 }
239 if (strcmp(prop_value, "ok") == 0) {
Szymon Jakubczak8c85a002010-06-09 16:11:09 -0400240 char dns_prop_name[PROPERTY_KEY_MAX];
Jeff Sharkey0fb8ec82012-04-18 21:54:55 -0700241 if (fill_ip_info(interface, ipaddr, gateway, prefixLength,
Robert Greenwalt6ecbdca2012-11-13 10:56:01 -0800242 dns1, dns2, server, lease, vendorInfo, domain) == -1) {
Robert Greenwaltfaab26d2011-01-14 14:35:42 -0800243 return -1;
244 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800245 return 0;
246 } else {
247 snprintf(errmsg, sizeof(errmsg), "DHCP result was %s", prop_value);
248 return -1;
249 }
250}
251
252/**
253 * Stop the DHCP client daemon.
254 */
255int dhcp_stop(const char *interface)
256{
257 char result_prop_name[PROPERTY_KEY_MAX];
TK MUN9d157872011-02-23 18:58:36 +0900258 char daemon_prop_name[PROPERTY_KEY_MAX];
259 char daemon_cmd[PROPERTY_VALUE_MAX * 2];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800260 const char *ctrl_prop = "ctl.stop";
261 const char *desired_status = "stopped";
262
Irfan Sheriff89f58cf2012-05-23 10:30:05 -0700263 char p2p_interface[MAX_INTERFACE_LENGTH];
repo synca329b422011-07-29 12:07:34 -0700264
Irfan Sheriff89f58cf2012-05-23 10:30:05 -0700265 get_p2p_interface_replacement(interface, p2p_interface);
repo synca329b422011-07-29 12:07:34 -0700266
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800267 snprintf(result_prop_name, sizeof(result_prop_name), "%s.%s.result",
268 DHCP_PROP_NAME_PREFIX,
Irfan Sheriff89f58cf2012-05-23 10:30:05 -0700269 p2p_interface);
TK MUN9d157872011-02-23 18:58:36 +0900270
271 snprintf(daemon_prop_name, sizeof(daemon_prop_name), "%s_%s",
272 DAEMON_PROP_NAME,
Irfan Sheriff89f58cf2012-05-23 10:30:05 -0700273 p2p_interface);
TK MUN9d157872011-02-23 18:58:36 +0900274
Irfan Sheriff89f58cf2012-05-23 10:30:05 -0700275 snprintf(daemon_cmd, sizeof(daemon_cmd), "%s_%s", DAEMON_NAME, p2p_interface);
TK MUN9d157872011-02-23 18:58:36 +0900276
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800277 /* Stop the daemon and wait until it's reported to be stopped */
TK MUN9d157872011-02-23 18:58:36 +0900278 property_set(ctrl_prop, daemon_cmd);
279 if (wait_for_property(daemon_prop_name, desired_status, 5) < 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800280 return -1;
281 }
282 property_set(result_prop_name, "failed");
283 return 0;
284}
285
286/**
287 * Release the current DHCP client lease.
288 */
289int dhcp_release_lease(const char *interface)
290{
TK MUN9d157872011-02-23 18:58:36 +0900291 char daemon_prop_name[PROPERTY_KEY_MAX];
292 char daemon_cmd[PROPERTY_VALUE_MAX * 2];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800293 const char *ctrl_prop = "ctl.stop";
294 const char *desired_status = "stopped";
295
Irfan Sheriff89f58cf2012-05-23 10:30:05 -0700296 char p2p_interface[MAX_INTERFACE_LENGTH];
repo synca329b422011-07-29 12:07:34 -0700297
Irfan Sheriff89f58cf2012-05-23 10:30:05 -0700298 get_p2p_interface_replacement(interface, p2p_interface);
repo synca329b422011-07-29 12:07:34 -0700299
TK MUN9d157872011-02-23 18:58:36 +0900300 snprintf(daemon_prop_name, sizeof(daemon_prop_name), "%s_%s",
301 DAEMON_PROP_NAME,
Irfan Sheriff89f58cf2012-05-23 10:30:05 -0700302 p2p_interface);
TK MUN9d157872011-02-23 18:58:36 +0900303
Irfan Sheriff89f58cf2012-05-23 10:30:05 -0700304 snprintf(daemon_cmd, sizeof(daemon_cmd), "%s_%s", DAEMON_NAME, p2p_interface);
TK MUN9d157872011-02-23 18:58:36 +0900305
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800306 /* Stop the daemon and wait until it's reported to be stopped */
TK MUN9d157872011-02-23 18:58:36 +0900307 property_set(ctrl_prop, daemon_cmd);
308 if (wait_for_property(daemon_prop_name, desired_status, 5) < 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800309 return -1;
310 }
311 return 0;
312}
313
314char *dhcp_get_errmsg() {
315 return errmsg;
316}
TK MUN9d157872011-02-23 18:58:36 +0900317
318/**
Irfan Sheriff35c28602011-11-09 11:10:50 -0800319 * The device init.rc file needs a corresponding entry.
320 *
321 * Example:
322 * service iprenew_<interface> /system/bin/dhcpcd -n
323 *
TK MUN9d157872011-02-23 18:58:36 +0900324 */
325int dhcp_do_request_renew(const char *interface,
Irfan Sheriff89f58cf2012-05-23 10:30:05 -0700326 char *ipaddr,
327 char *gateway,
tk.muned216332011-10-13 22:58:55 +0900328 uint32_t *prefixLength,
Irfan Sheriff89f58cf2012-05-23 10:30:05 -0700329 char *dns1,
330 char *dns2,
331 char *server,
Jeff Sharkey0fb8ec82012-04-18 21:54:55 -0700332 uint32_t *lease,
Robert Greenwalt6ecbdca2012-11-13 10:56:01 -0800333 char *vendorInfo,
334 char *domain)
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) {
Jeff Sharkey0fb8ec82012-04-18 21:54:55 -0700370 fill_ip_info(interface, ipaddr, gateway, prefixLength,
Robert Greenwalt6ecbdca2012-11-13 10:56:01 -0800371 dns1, dns2, server, lease, vendorInfo, domain);
TK MUN9d157872011-02-23 18:58:36 +0900372 return 0;
373 } else {
374 snprintf(errmsg, sizeof(errmsg), "DHCP Renew result was %s", prop_value);
375 return -1;
376 }
377}