blob: 40f549564feb5fc42c837facb435ca0a801659b6 [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";
Irfan Sheriffb1723b62010-12-21 10:31:05 -080032static const int NAP_TIME = 200; /* wait for 200ms at a time */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080033 /* when polling for property values */
TK MUN9d157872011-02-23 18:58:36 +090034static const char DAEMON_NAME_RENEW[] = "iprenew";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080035static char errmsg[100];
repo synca329b422011-07-29 12:07:34 -070036/* interface suffix on dhcpcd */
37#define MAX_DAEMON_SUFFIX 25
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080038
39/*
40 * Wait for a system property to be assigned a specified value.
41 * If desired_value is NULL, then just wait for the property to
42 * be created with any value. maxwait is the maximum amount of
43 * time in seconds to wait before giving up.
44 */
45static int wait_for_property(const char *name, const char *desired_value, int maxwait)
46{
47 char value[PROPERTY_VALUE_MAX] = {'\0'};
Irfan Sheriffb1723b62010-12-21 10:31:05 -080048 int maxnaps = (maxwait * 1000) / NAP_TIME;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080049
50 if (maxnaps < 1) {
51 maxnaps = 1;
52 }
53
54 while (maxnaps-- > 0) {
Irfan Sheriffb1723b62010-12-21 10:31:05 -080055 usleep(NAP_TIME * 1000);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080056 if (property_get(name, value, NULL)) {
57 if (desired_value == NULL ||
58 strcmp(value, desired_value) == 0) {
59 return 0;
60 }
61 }
62 }
63 return -1; /* failure */
64}
65
Robert Greenwaltfaab26d2011-01-14 14:35:42 -080066static int fill_ip_info(const char *interface,
67 char *ipaddr,
68 char *gateway,
69 uint32_t *prefixLength,
70 char *dns1,
71 char *dns2,
72 char *server,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080073 uint32_t *lease)
74{
75 char prop_name[PROPERTY_KEY_MAX];
76 char prop_value[PROPERTY_VALUE_MAX];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080077
78 snprintf(prop_name, sizeof(prop_name), "%s.%s.ipaddress", DHCP_PROP_NAME_PREFIX, interface);
Robert Greenwaltfaab26d2011-01-14 14:35:42 -080079 property_get(prop_name, ipaddr, NULL);
80
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080081 snprintf(prop_name, sizeof(prop_name), "%s.%s.gateway", DHCP_PROP_NAME_PREFIX, interface);
Robert Greenwaltfaab26d2011-01-14 14:35:42 -080082 property_get(prop_name, gateway, NULL);
83
Irfan Sheriffbdaaec12011-04-15 16:04:24 -070084 snprintf(prop_name, sizeof(prop_name), "%s.%s.server", DHCP_PROP_NAME_PREFIX, interface);
85 property_get(prop_name, server, NULL);
86
87 //TODO: Handle IPv6 when we change system property usage
88 if (strcmp(gateway, "0.0.0.0") == 0) {
89 //DHCP server is our best bet as gateway
90 strncpy(gateway, server, PROPERTY_VALUE_MAX);
91 }
92
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080093 snprintf(prop_name, sizeof(prop_name), "%s.%s.mask", DHCP_PROP_NAME_PREFIX, interface);
Robert Greenwaltfaab26d2011-01-14 14:35:42 -080094 if (property_get(prop_name, prop_value, NULL)) {
95 int p;
96 // this conversion is v4 only, but this dhcp client is v4 only anyway
97 in_addr_t mask = ntohl(inet_addr(prop_value));
98 // Check netmask is a valid IP address. ntohl gives NONE response (all 1's) for
99 // non 255.255.255.255 inputs. if we get that value check if it is legit..
100 if (mask == INADDR_NONE && strcmp(prop_value, "255.255.255.255") != 0) {
101 snprintf(errmsg, sizeof(errmsg), "DHCP gave invalid net mask %s", prop_value);
102 return -1;
103 }
104 for (p = 0; p < 32; p++) {
105 if (mask == 0) break;
106 // check for non-contiguous netmask, e.g., 255.254.255.0
107 if ((mask & 0x80000000) == 0) {
108 snprintf(errmsg, sizeof(errmsg), "DHCP gave invalid net mask %s", prop_value);
109 return -1;
110 }
111 mask = mask << 1;
112 }
113 *prefixLength = p;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800114 }
115 snprintf(prop_name, sizeof(prop_name), "%s.%s.dns1", DHCP_PROP_NAME_PREFIX, interface);
Robert Greenwaltfaab26d2011-01-14 14:35:42 -0800116 property_get(prop_name, dns1, NULL);
117
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800118 snprintf(prop_name, sizeof(prop_name), "%s.%s.dns2", DHCP_PROP_NAME_PREFIX, interface);
Robert Greenwaltfaab26d2011-01-14 14:35:42 -0800119 property_get(prop_name, dns2, NULL);
120
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800121 snprintf(prop_name, sizeof(prop_name), "%s.%s.leasetime", DHCP_PROP_NAME_PREFIX, interface);
122 if (property_get(prop_name, prop_value, NULL)) {
123 *lease = atol(prop_value);
124 }
Robert Greenwaltfaab26d2011-01-14 14:35:42 -0800125 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800126}
127
Szymon Jakubczak8c85a002010-06-09 16:11:09 -0400128static const char *ipaddr_to_string(in_addr_t addr)
129{
130 struct in_addr in_addr;
131
132 in_addr.s_addr = addr;
133 return inet_ntoa(in_addr);
134}
135
repo synca329b422011-07-29 12:07:34 -0700136void get_daemon_suffix(const char *interface, char *daemon_suffix) {
137 /* Use p2p suffix for any p2p interface. */
138 if (strncmp(interface, "p2p",3) == 0) {
139 sprintf(daemon_suffix, "p2p");
140 } else {
141 snprintf(daemon_suffix, MAX_DAEMON_SUFFIX, "%s", interface);
142 }
143}
144
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800145/*
146 * Start the dhcp client daemon, and wait for it to finish
147 * configuring the interface.
Irfan Sheriff35c28602011-11-09 11:10:50 -0800148 *
149 * The device init.rc file needs a corresponding entry for this work.
150 *
151 * Example:
152 * service dhcpcd_<interface> /system/bin/dhcpcd -ABKL
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800153 */
154int dhcp_do_request(const char *interface,
Robert Greenwaltfaab26d2011-01-14 14:35:42 -0800155 char *ipaddr,
156 char *gateway,
157 uint32_t *prefixLength,
158 char *dns1,
159 char *dns2,
160 char *server,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800161 uint32_t *lease)
162{
163 char result_prop_name[PROPERTY_KEY_MAX];
TK MUN9d157872011-02-23 18:58:36 +0900164 char daemon_prop_name[PROPERTY_KEY_MAX];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800165 char prop_value[PROPERTY_VALUE_MAX] = {'\0'};
Dmitry Shmidt9363b7d2009-12-14 15:41:54 -0800166 char daemon_cmd[PROPERTY_VALUE_MAX * 2];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800167 const char *ctrl_prop = "ctl.start";
168 const char *desired_status = "running";
repo synca329b422011-07-29 12:07:34 -0700169 char daemon_suffix[MAX_DAEMON_SUFFIX];
170
171 get_daemon_suffix(interface, daemon_suffix);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800172
173 snprintf(result_prop_name, sizeof(result_prop_name), "%s.%s.result",
174 DHCP_PROP_NAME_PREFIX,
175 interface);
TK MUN9d157872011-02-23 18:58:36 +0900176
177 snprintf(daemon_prop_name, sizeof(daemon_prop_name), "%s_%s",
178 DAEMON_PROP_NAME,
repo synca329b422011-07-29 12:07:34 -0700179 daemon_suffix);
TK MUN9d157872011-02-23 18:58:36 +0900180
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800181 /* Erase any previous setting of the dhcp result property */
182 property_set(result_prop_name, "");
183
184 /* Start the daemon and wait until it's ready */
Dmitry Shmidt9363b7d2009-12-14 15:41:54 -0800185 if (property_get(HOSTNAME_PROP_NAME, prop_value, NULL) && (prop_value[0] != '\0'))
repo synca329b422011-07-29 12:07:34 -0700186 snprintf(daemon_cmd, sizeof(daemon_cmd), "%s_%s:-h %s %s", DAEMON_NAME, daemon_suffix,
Dmitry Shmidt9363b7d2009-12-14 15:41:54 -0800187 prop_value, interface);
188 else
repo synca329b422011-07-29 12:07:34 -0700189 snprintf(daemon_cmd, sizeof(daemon_cmd), "%s_%s:%s", DAEMON_NAME, daemon_suffix, interface);
Dmitry Shmidt9363b7d2009-12-14 15:41:54 -0800190 memset(prop_value, '\0', PROPERTY_VALUE_MAX);
191 property_set(ctrl_prop, daemon_cmd);
TK MUN9d157872011-02-23 18:58:36 +0900192 if (wait_for_property(daemon_prop_name, desired_status, 10) < 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800193 snprintf(errmsg, sizeof(errmsg), "%s", "Timed out waiting for dhcpcd to start");
194 return -1;
195 }
196
197 /* Wait for the daemon to return a result */
198 if (wait_for_property(result_prop_name, NULL, 30) < 0) {
199 snprintf(errmsg, sizeof(errmsg), "%s", "Timed out waiting for DHCP to finish");
200 return -1;
201 }
202
203 if (!property_get(result_prop_name, prop_value, NULL)) {
204 /* shouldn't ever happen, given the success of wait_for_property() */
205 snprintf(errmsg, sizeof(errmsg), "%s", "DHCP result property was not set");
206 return -1;
207 }
208 if (strcmp(prop_value, "ok") == 0) {
Szymon Jakubczak8c85a002010-06-09 16:11:09 -0400209 char dns_prop_name[PROPERTY_KEY_MAX];
Robert Greenwaltfaab26d2011-01-14 14:35:42 -0800210 if (fill_ip_info(interface, ipaddr, gateway, prefixLength, dns1, dns2, server, lease)
211 == -1) {
212 return -1;
213 }
214
215 /* copy dns data to system properties - TODO - remove this after we have async
216 * notification of renewal's */
Szymon Jakubczak8c85a002010-06-09 16:11:09 -0400217 snprintf(dns_prop_name, sizeof(dns_prop_name), "net.%s.dns1", interface);
218 property_set(dns_prop_name, *dns1 ? ipaddr_to_string(*dns1) : "");
219 snprintf(dns_prop_name, sizeof(dns_prop_name), "net.%s.dns2", interface);
220 property_set(dns_prop_name, *dns2 ? ipaddr_to_string(*dns2) : "");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800221 return 0;
222 } else {
223 snprintf(errmsg, sizeof(errmsg), "DHCP result was %s", prop_value);
224 return -1;
225 }
226}
227
228/**
229 * Stop the DHCP client daemon.
230 */
231int dhcp_stop(const char *interface)
232{
233 char result_prop_name[PROPERTY_KEY_MAX];
TK MUN9d157872011-02-23 18:58:36 +0900234 char daemon_prop_name[PROPERTY_KEY_MAX];
235 char daemon_cmd[PROPERTY_VALUE_MAX * 2];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800236 const char *ctrl_prop = "ctl.stop";
237 const char *desired_status = "stopped";
238
repo synca329b422011-07-29 12:07:34 -0700239 char daemon_suffix[MAX_DAEMON_SUFFIX];
240
241 get_daemon_suffix(interface, daemon_suffix);
242
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800243 snprintf(result_prop_name, sizeof(result_prop_name), "%s.%s.result",
244 DHCP_PROP_NAME_PREFIX,
245 interface);
TK MUN9d157872011-02-23 18:58:36 +0900246
247 snprintf(daemon_prop_name, sizeof(daemon_prop_name), "%s_%s",
248 DAEMON_PROP_NAME,
repo synca329b422011-07-29 12:07:34 -0700249 daemon_suffix);
TK MUN9d157872011-02-23 18:58:36 +0900250
repo synca329b422011-07-29 12:07:34 -0700251 snprintf(daemon_cmd, sizeof(daemon_cmd), "%s_%s", DAEMON_NAME, daemon_suffix);
TK MUN9d157872011-02-23 18:58:36 +0900252
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800253 /* Stop the daemon and wait until it's reported to be stopped */
TK MUN9d157872011-02-23 18:58:36 +0900254 property_set(ctrl_prop, daemon_cmd);
255 if (wait_for_property(daemon_prop_name, desired_status, 5) < 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800256 return -1;
257 }
258 property_set(result_prop_name, "failed");
259 return 0;
260}
261
262/**
263 * Release the current DHCP client lease.
264 */
265int dhcp_release_lease(const char *interface)
266{
TK MUN9d157872011-02-23 18:58:36 +0900267 char daemon_prop_name[PROPERTY_KEY_MAX];
268 char daemon_cmd[PROPERTY_VALUE_MAX * 2];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800269 const char *ctrl_prop = "ctl.stop";
270 const char *desired_status = "stopped";
271
repo synca329b422011-07-29 12:07:34 -0700272 char daemon_suffix[MAX_DAEMON_SUFFIX];
273
274 get_daemon_suffix(interface, daemon_suffix);
275
TK MUN9d157872011-02-23 18:58:36 +0900276 snprintf(daemon_prop_name, sizeof(daemon_prop_name), "%s_%s",
277 DAEMON_PROP_NAME,
repo synca329b422011-07-29 12:07:34 -0700278 daemon_suffix);
TK MUN9d157872011-02-23 18:58:36 +0900279
repo synca329b422011-07-29 12:07:34 -0700280 snprintf(daemon_cmd, sizeof(daemon_cmd), "%s_%s", DAEMON_NAME, daemon_suffix);
TK MUN9d157872011-02-23 18:58:36 +0900281
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800282 /* Stop the daemon and wait until it's reported to be stopped */
TK MUN9d157872011-02-23 18:58:36 +0900283 property_set(ctrl_prop, daemon_cmd);
284 if (wait_for_property(daemon_prop_name, desired_status, 5) < 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800285 return -1;
286 }
287 return 0;
288}
289
290char *dhcp_get_errmsg() {
291 return errmsg;
292}
TK MUN9d157872011-02-23 18:58:36 +0900293
294/**
Irfan Sheriff35c28602011-11-09 11:10:50 -0800295 * The device init.rc file needs a corresponding entry.
296 *
297 * Example:
298 * service iprenew_<interface> /system/bin/dhcpcd -n
299 *
TK MUN9d157872011-02-23 18:58:36 +0900300 */
301int dhcp_do_request_renew(const char *interface,
302 in_addr_t *ipaddr,
303 in_addr_t *gateway,
304 in_addr_t *mask,
305 in_addr_t *dns1,
306 in_addr_t *dns2,
307 in_addr_t *server,
308 uint32_t *lease)
309{
310 char result_prop_name[PROPERTY_KEY_MAX];
311 char prop_value[PROPERTY_VALUE_MAX] = {'\0'};
312 char daemon_cmd[PROPERTY_VALUE_MAX * 2];
313 const char *ctrl_prop = "ctl.start";
314
repo synca329b422011-07-29 12:07:34 -0700315 char daemon_suffix[MAX_DAEMON_SUFFIX];
316
317 get_daemon_suffix(interface, daemon_suffix);
318
TK MUN9d157872011-02-23 18:58:36 +0900319 snprintf(result_prop_name, sizeof(result_prop_name), "%s.%s.result",
320 DHCP_PROP_NAME_PREFIX,
321 interface);
322
323 /* Erase any previous setting of the dhcp result property */
324 property_set(result_prop_name, "");
325
326 /* Start the renew daemon and wait until it's ready */
repo synca329b422011-07-29 12:07:34 -0700327 snprintf(daemon_cmd, sizeof(daemon_cmd), "%s_%s:%s", DAEMON_NAME_RENEW,
328 daemon_suffix, interface);
TK MUN9d157872011-02-23 18:58:36 +0900329 memset(prop_value, '\0', PROPERTY_VALUE_MAX);
330 property_set(ctrl_prop, daemon_cmd);
331
332 /* Wait for the daemon to return a result */
333 if (wait_for_property(result_prop_name, NULL, 30) < 0) {
334 snprintf(errmsg, sizeof(errmsg), "%s", "Timed out waiting for DHCP Renew to finish");
335 return -1;
336 }
337
338 if (!property_get(result_prop_name, prop_value, NULL)) {
339 /* shouldn't ever happen, given the success of wait_for_property() */
340 snprintf(errmsg, sizeof(errmsg), "%s", "DHCP Renew result property was not set");
341 return -1;
342 }
343 if (strcmp(prop_value, "ok") == 0) {
344 fill_ip_info(interface, ipaddr, gateway, mask, dns1, dns2, server, lease);
345 return 0;
346 } else {
347 snprintf(errmsg, sizeof(errmsg), "DHCP Renew result was %s", prop_value);
348 return -1;
349 }
350}