blob: 3ab5d1bce6ff8e96ab8588747e28ae8f38832197 [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.
148 */
149int dhcp_do_request(const char *interface,
Robert Greenwaltfaab26d2011-01-14 14:35:42 -0800150 char *ipaddr,
151 char *gateway,
152 uint32_t *prefixLength,
153 char *dns1,
154 char *dns2,
155 char *server,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800156 uint32_t *lease)
157{
158 char result_prop_name[PROPERTY_KEY_MAX];
TK MUN9d157872011-02-23 18:58:36 +0900159 char daemon_prop_name[PROPERTY_KEY_MAX];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800160 char prop_value[PROPERTY_VALUE_MAX] = {'\0'};
Dmitry Shmidt9363b7d2009-12-14 15:41:54 -0800161 char daemon_cmd[PROPERTY_VALUE_MAX * 2];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800162 const char *ctrl_prop = "ctl.start";
163 const char *desired_status = "running";
repo synca329b422011-07-29 12:07:34 -0700164 char daemon_suffix[MAX_DAEMON_SUFFIX];
165
166 get_daemon_suffix(interface, daemon_suffix);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800167
168 snprintf(result_prop_name, sizeof(result_prop_name), "%s.%s.result",
169 DHCP_PROP_NAME_PREFIX,
170 interface);
TK MUN9d157872011-02-23 18:58:36 +0900171
172 snprintf(daemon_prop_name, sizeof(daemon_prop_name), "%s_%s",
173 DAEMON_PROP_NAME,
repo synca329b422011-07-29 12:07:34 -0700174 daemon_suffix);
TK MUN9d157872011-02-23 18:58:36 +0900175
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800176 /* Erase any previous setting of the dhcp result property */
177 property_set(result_prop_name, "");
178
179 /* Start the daemon and wait until it's ready */
Dmitry Shmidt9363b7d2009-12-14 15:41:54 -0800180 if (property_get(HOSTNAME_PROP_NAME, prop_value, NULL) && (prop_value[0] != '\0'))
repo synca329b422011-07-29 12:07:34 -0700181 snprintf(daemon_cmd, sizeof(daemon_cmd), "%s_%s:-h %s %s", DAEMON_NAME, daemon_suffix,
Dmitry Shmidt9363b7d2009-12-14 15:41:54 -0800182 prop_value, interface);
183 else
repo synca329b422011-07-29 12:07:34 -0700184 snprintf(daemon_cmd, sizeof(daemon_cmd), "%s_%s:%s", DAEMON_NAME, daemon_suffix, interface);
Dmitry Shmidt9363b7d2009-12-14 15:41:54 -0800185 memset(prop_value, '\0', PROPERTY_VALUE_MAX);
186 property_set(ctrl_prop, daemon_cmd);
TK MUN9d157872011-02-23 18:58:36 +0900187 if (wait_for_property(daemon_prop_name, desired_status, 10) < 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800188 snprintf(errmsg, sizeof(errmsg), "%s", "Timed out waiting for dhcpcd to start");
189 return -1;
190 }
191
192 /* Wait for the daemon to return a result */
193 if (wait_for_property(result_prop_name, NULL, 30) < 0) {
194 snprintf(errmsg, sizeof(errmsg), "%s", "Timed out waiting for DHCP to finish");
195 return -1;
196 }
197
198 if (!property_get(result_prop_name, prop_value, NULL)) {
199 /* shouldn't ever happen, given the success of wait_for_property() */
200 snprintf(errmsg, sizeof(errmsg), "%s", "DHCP result property was not set");
201 return -1;
202 }
203 if (strcmp(prop_value, "ok") == 0) {
Szymon Jakubczak8c85a002010-06-09 16:11:09 -0400204 char dns_prop_name[PROPERTY_KEY_MAX];
Robert Greenwaltfaab26d2011-01-14 14:35:42 -0800205 if (fill_ip_info(interface, ipaddr, gateway, prefixLength, dns1, dns2, server, lease)
206 == -1) {
207 return -1;
208 }
209
210 /* copy dns data to system properties - TODO - remove this after we have async
211 * notification of renewal's */
Szymon Jakubczak8c85a002010-06-09 16:11:09 -0400212 snprintf(dns_prop_name, sizeof(dns_prop_name), "net.%s.dns1", interface);
213 property_set(dns_prop_name, *dns1 ? ipaddr_to_string(*dns1) : "");
214 snprintf(dns_prop_name, sizeof(dns_prop_name), "net.%s.dns2", interface);
215 property_set(dns_prop_name, *dns2 ? ipaddr_to_string(*dns2) : "");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800216 return 0;
217 } else {
218 snprintf(errmsg, sizeof(errmsg), "DHCP result was %s", prop_value);
219 return -1;
220 }
221}
222
223/**
224 * Stop the DHCP client daemon.
225 */
226int dhcp_stop(const char *interface)
227{
228 char result_prop_name[PROPERTY_KEY_MAX];
TK MUN9d157872011-02-23 18:58:36 +0900229 char daemon_prop_name[PROPERTY_KEY_MAX];
230 char daemon_cmd[PROPERTY_VALUE_MAX * 2];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800231 const char *ctrl_prop = "ctl.stop";
232 const char *desired_status = "stopped";
233
repo synca329b422011-07-29 12:07:34 -0700234 char daemon_suffix[MAX_DAEMON_SUFFIX];
235
236 get_daemon_suffix(interface, daemon_suffix);
237
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800238 snprintf(result_prop_name, sizeof(result_prop_name), "%s.%s.result",
239 DHCP_PROP_NAME_PREFIX,
240 interface);
TK MUN9d157872011-02-23 18:58:36 +0900241
242 snprintf(daemon_prop_name, sizeof(daemon_prop_name), "%s_%s",
243 DAEMON_PROP_NAME,
repo synca329b422011-07-29 12:07:34 -0700244 daemon_suffix);
TK MUN9d157872011-02-23 18:58:36 +0900245
repo synca329b422011-07-29 12:07:34 -0700246 snprintf(daemon_cmd, sizeof(daemon_cmd), "%s_%s", DAEMON_NAME, daemon_suffix);
TK MUN9d157872011-02-23 18:58:36 +0900247
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800248 /* Stop the daemon and wait until it's reported to be stopped */
TK MUN9d157872011-02-23 18:58:36 +0900249 property_set(ctrl_prop, daemon_cmd);
250 if (wait_for_property(daemon_prop_name, desired_status, 5) < 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800251 return -1;
252 }
253 property_set(result_prop_name, "failed");
254 return 0;
255}
256
257/**
258 * Release the current DHCP client lease.
259 */
260int dhcp_release_lease(const char *interface)
261{
TK MUN9d157872011-02-23 18:58:36 +0900262 char daemon_prop_name[PROPERTY_KEY_MAX];
263 char daemon_cmd[PROPERTY_VALUE_MAX * 2];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800264 const char *ctrl_prop = "ctl.stop";
265 const char *desired_status = "stopped";
266
repo synca329b422011-07-29 12:07:34 -0700267 char daemon_suffix[MAX_DAEMON_SUFFIX];
268
269 get_daemon_suffix(interface, daemon_suffix);
270
TK MUN9d157872011-02-23 18:58:36 +0900271 snprintf(daemon_prop_name, sizeof(daemon_prop_name), "%s_%s",
272 DAEMON_PROP_NAME,
repo synca329b422011-07-29 12:07:34 -0700273 daemon_suffix);
TK MUN9d157872011-02-23 18:58:36 +0900274
repo synca329b422011-07-29 12:07:34 -0700275 snprintf(daemon_cmd, sizeof(daemon_cmd), "%s_%s", DAEMON_NAME, daemon_suffix);
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 return 0;
283}
284
285char *dhcp_get_errmsg() {
286 return errmsg;
287}
TK MUN9d157872011-02-23 18:58:36 +0900288
289/**
290 * Run WiMAX dhcp renew service.
291 * "wimax_renew" service shoud be included in init.rc.
292 */
293int dhcp_do_request_renew(const char *interface,
294 in_addr_t *ipaddr,
295 in_addr_t *gateway,
296 in_addr_t *mask,
297 in_addr_t *dns1,
298 in_addr_t *dns2,
299 in_addr_t *server,
300 uint32_t *lease)
301{
302 char result_prop_name[PROPERTY_KEY_MAX];
303 char prop_value[PROPERTY_VALUE_MAX] = {'\0'};
304 char daemon_cmd[PROPERTY_VALUE_MAX * 2];
305 const char *ctrl_prop = "ctl.start";
306
repo synca329b422011-07-29 12:07:34 -0700307 char daemon_suffix[MAX_DAEMON_SUFFIX];
308
309 get_daemon_suffix(interface, daemon_suffix);
310
TK MUN9d157872011-02-23 18:58:36 +0900311 snprintf(result_prop_name, sizeof(result_prop_name), "%s.%s.result",
312 DHCP_PROP_NAME_PREFIX,
313 interface);
314
315 /* Erase any previous setting of the dhcp result property */
316 property_set(result_prop_name, "");
317
318 /* Start the renew daemon and wait until it's ready */
repo synca329b422011-07-29 12:07:34 -0700319 snprintf(daemon_cmd, sizeof(daemon_cmd), "%s_%s:%s", DAEMON_NAME_RENEW,
320 daemon_suffix, interface);
TK MUN9d157872011-02-23 18:58:36 +0900321 memset(prop_value, '\0', PROPERTY_VALUE_MAX);
322 property_set(ctrl_prop, daemon_cmd);
323
324 /* Wait for the daemon to return a result */
325 if (wait_for_property(result_prop_name, NULL, 30) < 0) {
326 snprintf(errmsg, sizeof(errmsg), "%s", "Timed out waiting for DHCP Renew to finish");
327 return -1;
328 }
329
330 if (!property_get(result_prop_name, prop_value, NULL)) {
331 /* shouldn't ever happen, given the success of wait_for_property() */
332 snprintf(errmsg, sizeof(errmsg), "%s", "DHCP Renew result property was not set");
333 return -1;
334 }
335 if (strcmp(prop_value, "ok") == 0) {
336 fill_ip_info(interface, ipaddr, gateway, mask, dns1, dns2, server, lease);
337 return 0;
338 } else {
339 snprintf(errmsg, sizeof(errmsg), "DHCP Renew result was %s", prop_value);
340 return -1;
341 }
342}