blob: 398d9c46410d1df7289d43f6635ad3da54eafa72 [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,
Jeff Sharkey0fb8ec82012-04-18 21:54:55 -070073 uint32_t *lease,
74 char *vendorInfo)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080075{
76 char prop_name[PROPERTY_KEY_MAX];
77 char prop_value[PROPERTY_VALUE_MAX];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080078
79 snprintf(prop_name, sizeof(prop_name), "%s.%s.ipaddress", DHCP_PROP_NAME_PREFIX, interface);
Robert Greenwaltfaab26d2011-01-14 14:35:42 -080080 property_get(prop_name, ipaddr, NULL);
81
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080082 snprintf(prop_name, sizeof(prop_name), "%s.%s.gateway", DHCP_PROP_NAME_PREFIX, interface);
Robert Greenwaltfaab26d2011-01-14 14:35:42 -080083 property_get(prop_name, gateway, NULL);
84
Irfan Sheriffbdaaec12011-04-15 16:04:24 -070085 snprintf(prop_name, sizeof(prop_name), "%s.%s.server", DHCP_PROP_NAME_PREFIX, interface);
86 property_get(prop_name, server, NULL);
87
88 //TODO: Handle IPv6 when we change system property usage
89 if (strcmp(gateway, "0.0.0.0") == 0) {
90 //DHCP server is our best bet as gateway
91 strncpy(gateway, server, PROPERTY_VALUE_MAX);
92 }
93
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080094 snprintf(prop_name, sizeof(prop_name), "%s.%s.mask", DHCP_PROP_NAME_PREFIX, interface);
Robert Greenwaltfaab26d2011-01-14 14:35:42 -080095 if (property_get(prop_name, prop_value, NULL)) {
96 int p;
97 // this conversion is v4 only, but this dhcp client is v4 only anyway
98 in_addr_t mask = ntohl(inet_addr(prop_value));
99 // Check netmask is a valid IP address. ntohl gives NONE response (all 1's) for
100 // non 255.255.255.255 inputs. if we get that value check if it is legit..
101 if (mask == INADDR_NONE && strcmp(prop_value, "255.255.255.255") != 0) {
102 snprintf(errmsg, sizeof(errmsg), "DHCP gave invalid net mask %s", prop_value);
103 return -1;
104 }
105 for (p = 0; p < 32; p++) {
106 if (mask == 0) break;
107 // check for non-contiguous netmask, e.g., 255.254.255.0
108 if ((mask & 0x80000000) == 0) {
109 snprintf(errmsg, sizeof(errmsg), "DHCP gave invalid net mask %s", prop_value);
110 return -1;
111 }
112 mask = mask << 1;
113 }
114 *prefixLength = p;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800115 }
116 snprintf(prop_name, sizeof(prop_name), "%s.%s.dns1", DHCP_PROP_NAME_PREFIX, interface);
Robert Greenwaltfaab26d2011-01-14 14:35:42 -0800117 property_get(prop_name, dns1, NULL);
118
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800119 snprintf(prop_name, sizeof(prop_name), "%s.%s.dns2", DHCP_PROP_NAME_PREFIX, interface);
Robert Greenwaltfaab26d2011-01-14 14:35:42 -0800120 property_get(prop_name, dns2, NULL);
121
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800122 snprintf(prop_name, sizeof(prop_name), "%s.%s.leasetime", DHCP_PROP_NAME_PREFIX, interface);
123 if (property_get(prop_name, prop_value, NULL)) {
124 *lease = atol(prop_value);
125 }
Jeff Sharkey0fb8ec82012-04-18 21:54:55 -0700126
127 snprintf(prop_name, sizeof(prop_name), "%s.%s.vendorInfo", DHCP_PROP_NAME_PREFIX, interface);
128 property_get(prop_name, vendorInfo, NULL);
129
Robert Greenwaltfaab26d2011-01-14 14:35:42 -0800130 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800131}
132
Szymon Jakubczak8c85a002010-06-09 16:11:09 -0400133static const char *ipaddr_to_string(in_addr_t addr)
134{
135 struct in_addr in_addr;
136
137 in_addr.s_addr = addr;
138 return inet_ntoa(in_addr);
139}
140
repo synca329b422011-07-29 12:07:34 -0700141void get_daemon_suffix(const char *interface, char *daemon_suffix) {
142 /* Use p2p suffix for any p2p interface. */
143 if (strncmp(interface, "p2p",3) == 0) {
144 sprintf(daemon_suffix, "p2p");
145 } else {
146 snprintf(daemon_suffix, MAX_DAEMON_SUFFIX, "%s", interface);
147 }
148}
149
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800150/*
151 * Start the dhcp client daemon, and wait for it to finish
152 * configuring the interface.
Irfan Sheriff35c28602011-11-09 11:10:50 -0800153 *
154 * The device init.rc file needs a corresponding entry for this work.
155 *
156 * Example:
157 * service dhcpcd_<interface> /system/bin/dhcpcd -ABKL
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800158 */
159int dhcp_do_request(const char *interface,
Robert Greenwaltfaab26d2011-01-14 14:35:42 -0800160 char *ipaddr,
161 char *gateway,
162 uint32_t *prefixLength,
163 char *dns1,
164 char *dns2,
165 char *server,
Jeff Sharkey0fb8ec82012-04-18 21:54:55 -0700166 uint32_t *lease,
167 char *vendorInfo)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800168{
169 char result_prop_name[PROPERTY_KEY_MAX];
TK MUN9d157872011-02-23 18:58:36 +0900170 char daemon_prop_name[PROPERTY_KEY_MAX];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800171 char prop_value[PROPERTY_VALUE_MAX] = {'\0'};
Dmitry Shmidt9363b7d2009-12-14 15:41:54 -0800172 char daemon_cmd[PROPERTY_VALUE_MAX * 2];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800173 const char *ctrl_prop = "ctl.start";
174 const char *desired_status = "running";
repo synca329b422011-07-29 12:07:34 -0700175 char daemon_suffix[MAX_DAEMON_SUFFIX];
176
177 get_daemon_suffix(interface, daemon_suffix);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800178
179 snprintf(result_prop_name, sizeof(result_prop_name), "%s.%s.result",
180 DHCP_PROP_NAME_PREFIX,
181 interface);
TK MUN9d157872011-02-23 18:58:36 +0900182
183 snprintf(daemon_prop_name, sizeof(daemon_prop_name), "%s_%s",
184 DAEMON_PROP_NAME,
repo synca329b422011-07-29 12:07:34 -0700185 daemon_suffix);
TK MUN9d157872011-02-23 18:58:36 +0900186
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800187 /* Erase any previous setting of the dhcp result property */
188 property_set(result_prop_name, "");
189
190 /* Start the daemon and wait until it's ready */
Dmitry Shmidt9363b7d2009-12-14 15:41:54 -0800191 if (property_get(HOSTNAME_PROP_NAME, prop_value, NULL) && (prop_value[0] != '\0'))
repo synca329b422011-07-29 12:07:34 -0700192 snprintf(daemon_cmd, sizeof(daemon_cmd), "%s_%s:-h %s %s", DAEMON_NAME, daemon_suffix,
Dmitry Shmidt9363b7d2009-12-14 15:41:54 -0800193 prop_value, interface);
194 else
repo synca329b422011-07-29 12:07:34 -0700195 snprintf(daemon_cmd, sizeof(daemon_cmd), "%s_%s:%s", DAEMON_NAME, daemon_suffix, interface);
Dmitry Shmidt9363b7d2009-12-14 15:41:54 -0800196 memset(prop_value, '\0', PROPERTY_VALUE_MAX);
197 property_set(ctrl_prop, daemon_cmd);
TK MUN9d157872011-02-23 18:58:36 +0900198 if (wait_for_property(daemon_prop_name, desired_status, 10) < 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800199 snprintf(errmsg, sizeof(errmsg), "%s", "Timed out waiting for dhcpcd to start");
200 return -1;
201 }
202
203 /* Wait for the daemon to return a result */
204 if (wait_for_property(result_prop_name, NULL, 30) < 0) {
205 snprintf(errmsg, sizeof(errmsg), "%s", "Timed out waiting for DHCP to finish");
206 return -1;
207 }
208
209 if (!property_get(result_prop_name, prop_value, NULL)) {
210 /* shouldn't ever happen, given the success of wait_for_property() */
211 snprintf(errmsg, sizeof(errmsg), "%s", "DHCP result property was not set");
212 return -1;
213 }
214 if (strcmp(prop_value, "ok") == 0) {
Szymon Jakubczak8c85a002010-06-09 16:11:09 -0400215 char dns_prop_name[PROPERTY_KEY_MAX];
Jeff Sharkey0fb8ec82012-04-18 21:54:55 -0700216 if (fill_ip_info(interface, ipaddr, gateway, prefixLength,
217 dns1, dns2, server, lease, vendorInfo) == -1) {
Robert Greenwaltfaab26d2011-01-14 14:35:42 -0800218 return -1;
219 }
220
221 /* copy dns data to system properties - TODO - remove this after we have async
222 * notification of renewal's */
Szymon Jakubczak8c85a002010-06-09 16:11:09 -0400223 snprintf(dns_prop_name, sizeof(dns_prop_name), "net.%s.dns1", interface);
224 property_set(dns_prop_name, *dns1 ? ipaddr_to_string(*dns1) : "");
225 snprintf(dns_prop_name, sizeof(dns_prop_name), "net.%s.dns2", interface);
226 property_set(dns_prop_name, *dns2 ? ipaddr_to_string(*dns2) : "");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800227 return 0;
228 } else {
229 snprintf(errmsg, sizeof(errmsg), "DHCP result was %s", prop_value);
230 return -1;
231 }
232}
233
234/**
235 * Stop the DHCP client daemon.
236 */
237int dhcp_stop(const char *interface)
238{
239 char result_prop_name[PROPERTY_KEY_MAX];
TK MUN9d157872011-02-23 18:58:36 +0900240 char daemon_prop_name[PROPERTY_KEY_MAX];
241 char daemon_cmd[PROPERTY_VALUE_MAX * 2];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800242 const char *ctrl_prop = "ctl.stop";
243 const char *desired_status = "stopped";
244
repo synca329b422011-07-29 12:07:34 -0700245 char daemon_suffix[MAX_DAEMON_SUFFIX];
246
247 get_daemon_suffix(interface, daemon_suffix);
248
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800249 snprintf(result_prop_name, sizeof(result_prop_name), "%s.%s.result",
250 DHCP_PROP_NAME_PREFIX,
251 interface);
TK MUN9d157872011-02-23 18:58:36 +0900252
253 snprintf(daemon_prop_name, sizeof(daemon_prop_name), "%s_%s",
254 DAEMON_PROP_NAME,
repo synca329b422011-07-29 12:07:34 -0700255 daemon_suffix);
TK MUN9d157872011-02-23 18:58:36 +0900256
repo synca329b422011-07-29 12:07:34 -0700257 snprintf(daemon_cmd, sizeof(daemon_cmd), "%s_%s", DAEMON_NAME, daemon_suffix);
TK MUN9d157872011-02-23 18:58:36 +0900258
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800259 /* Stop the daemon and wait until it's reported to be stopped */
TK MUN9d157872011-02-23 18:58:36 +0900260 property_set(ctrl_prop, daemon_cmd);
261 if (wait_for_property(daemon_prop_name, desired_status, 5) < 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800262 return -1;
263 }
264 property_set(result_prop_name, "failed");
265 return 0;
266}
267
268/**
269 * Release the current DHCP client lease.
270 */
271int dhcp_release_lease(const char *interface)
272{
TK MUN9d157872011-02-23 18:58:36 +0900273 char daemon_prop_name[PROPERTY_KEY_MAX];
274 char daemon_cmd[PROPERTY_VALUE_MAX * 2];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800275 const char *ctrl_prop = "ctl.stop";
276 const char *desired_status = "stopped";
277
repo synca329b422011-07-29 12:07:34 -0700278 char daemon_suffix[MAX_DAEMON_SUFFIX];
279
280 get_daemon_suffix(interface, daemon_suffix);
281
TK MUN9d157872011-02-23 18:58:36 +0900282 snprintf(daemon_prop_name, sizeof(daemon_prop_name), "%s_%s",
283 DAEMON_PROP_NAME,
repo synca329b422011-07-29 12:07:34 -0700284 daemon_suffix);
TK MUN9d157872011-02-23 18:58:36 +0900285
repo synca329b422011-07-29 12:07:34 -0700286 snprintf(daemon_cmd, sizeof(daemon_cmd), "%s_%s", DAEMON_NAME, daemon_suffix);
TK MUN9d157872011-02-23 18:58:36 +0900287
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800288 /* Stop the daemon and wait until it's reported to be stopped */
TK MUN9d157872011-02-23 18:58:36 +0900289 property_set(ctrl_prop, daemon_cmd);
290 if (wait_for_property(daemon_prop_name, desired_status, 5) < 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800291 return -1;
292 }
293 return 0;
294}
295
296char *dhcp_get_errmsg() {
297 return errmsg;
298}
TK MUN9d157872011-02-23 18:58:36 +0900299
300/**
Irfan Sheriff35c28602011-11-09 11:10:50 -0800301 * The device init.rc file needs a corresponding entry.
302 *
303 * Example:
304 * service iprenew_<interface> /system/bin/dhcpcd -n
305 *
TK MUN9d157872011-02-23 18:58:36 +0900306 */
307int dhcp_do_request_renew(const char *interface,
308 in_addr_t *ipaddr,
309 in_addr_t *gateway,
tk.muned216332011-10-13 22:58:55 +0900310 uint32_t *prefixLength,
TK MUN9d157872011-02-23 18:58:36 +0900311 in_addr_t *dns1,
312 in_addr_t *dns2,
313 in_addr_t *server,
Jeff Sharkey0fb8ec82012-04-18 21:54:55 -0700314 uint32_t *lease,
315 char *vendorInfo)
TK MUN9d157872011-02-23 18:58:36 +0900316{
317 char result_prop_name[PROPERTY_KEY_MAX];
318 char prop_value[PROPERTY_VALUE_MAX] = {'\0'};
319 char daemon_cmd[PROPERTY_VALUE_MAX * 2];
320 const char *ctrl_prop = "ctl.start";
321
repo synca329b422011-07-29 12:07:34 -0700322 char daemon_suffix[MAX_DAEMON_SUFFIX];
323
324 get_daemon_suffix(interface, daemon_suffix);
325
TK MUN9d157872011-02-23 18:58:36 +0900326 snprintf(result_prop_name, sizeof(result_prop_name), "%s.%s.result",
327 DHCP_PROP_NAME_PREFIX,
328 interface);
329
330 /* Erase any previous setting of the dhcp result property */
331 property_set(result_prop_name, "");
332
333 /* Start the renew daemon and wait until it's ready */
repo synca329b422011-07-29 12:07:34 -0700334 snprintf(daemon_cmd, sizeof(daemon_cmd), "%s_%s:%s", DAEMON_NAME_RENEW,
335 daemon_suffix, interface);
TK MUN9d157872011-02-23 18:58:36 +0900336 memset(prop_value, '\0', PROPERTY_VALUE_MAX);
337 property_set(ctrl_prop, daemon_cmd);
338
339 /* Wait for the daemon to return a result */
340 if (wait_for_property(result_prop_name, NULL, 30) < 0) {
341 snprintf(errmsg, sizeof(errmsg), "%s", "Timed out waiting for DHCP Renew to finish");
342 return -1;
343 }
344
345 if (!property_get(result_prop_name, prop_value, NULL)) {
346 /* shouldn't ever happen, given the success of wait_for_property() */
347 snprintf(errmsg, sizeof(errmsg), "%s", "DHCP Renew result property was not set");
348 return -1;
349 }
350 if (strcmp(prop_value, "ok") == 0) {
Jeff Sharkey0fb8ec82012-04-18 21:54:55 -0700351 fill_ip_info(interface, ipaddr, gateway, prefixLength,
352 dns1, dns2, server, lease, vendorInfo);
TK MUN9d157872011-02-23 18:58:36 +0900353 return 0;
354 } else {
355 snprintf(errmsg, sizeof(errmsg), "DHCP Renew result was %s", prop_value);
356 return -1;
357 }
358}