blob: af82a3c63082c5c3002317db32556bc54d2219a9 [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];
36
37/*
38 * Wait for a system property to be assigned a specified value.
39 * If desired_value is NULL, then just wait for the property to
40 * be created with any value. maxwait is the maximum amount of
41 * time in seconds to wait before giving up.
42 */
43static int wait_for_property(const char *name, const char *desired_value, int maxwait)
44{
45 char value[PROPERTY_VALUE_MAX] = {'\0'};
Irfan Sheriffb1723b62010-12-21 10:31:05 -080046 int maxnaps = (maxwait * 1000) / NAP_TIME;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080047
48 if (maxnaps < 1) {
49 maxnaps = 1;
50 }
51
52 while (maxnaps-- > 0) {
Irfan Sheriffb1723b62010-12-21 10:31:05 -080053 usleep(NAP_TIME * 1000);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080054 if (property_get(name, value, NULL)) {
55 if (desired_value == NULL ||
56 strcmp(value, desired_value) == 0) {
57 return 0;
58 }
59 }
60 }
61 return -1; /* failure */
62}
63
Robert Greenwaltfaab26d2011-01-14 14:35:42 -080064static int fill_ip_info(const char *interface,
65 char *ipaddr,
66 char *gateway,
67 uint32_t *prefixLength,
68 char *dns1,
69 char *dns2,
70 char *server,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080071 uint32_t *lease)
72{
73 char prop_name[PROPERTY_KEY_MAX];
74 char prop_value[PROPERTY_VALUE_MAX];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080075
76 snprintf(prop_name, sizeof(prop_name), "%s.%s.ipaddress", DHCP_PROP_NAME_PREFIX, interface);
Robert Greenwaltfaab26d2011-01-14 14:35:42 -080077 property_get(prop_name, ipaddr, NULL);
78
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080079 snprintf(prop_name, sizeof(prop_name), "%s.%s.gateway", DHCP_PROP_NAME_PREFIX, interface);
Robert Greenwaltfaab26d2011-01-14 14:35:42 -080080 property_get(prop_name, gateway, NULL);
81
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080082 snprintf(prop_name, sizeof(prop_name), "%s.%s.mask", DHCP_PROP_NAME_PREFIX, interface);
Robert Greenwaltfaab26d2011-01-14 14:35:42 -080083 if (property_get(prop_name, prop_value, NULL)) {
84 int p;
85 // this conversion is v4 only, but this dhcp client is v4 only anyway
86 in_addr_t mask = ntohl(inet_addr(prop_value));
87 // Check netmask is a valid IP address. ntohl gives NONE response (all 1's) for
88 // non 255.255.255.255 inputs. if we get that value check if it is legit..
89 if (mask == INADDR_NONE && strcmp(prop_value, "255.255.255.255") != 0) {
90 snprintf(errmsg, sizeof(errmsg), "DHCP gave invalid net mask %s", prop_value);
91 return -1;
92 }
93 for (p = 0; p < 32; p++) {
94 if (mask == 0) break;
95 // check for non-contiguous netmask, e.g., 255.254.255.0
96 if ((mask & 0x80000000) == 0) {
97 snprintf(errmsg, sizeof(errmsg), "DHCP gave invalid net mask %s", prop_value);
98 return -1;
99 }
100 mask = mask << 1;
101 }
102 *prefixLength = p;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800103 }
104 snprintf(prop_name, sizeof(prop_name), "%s.%s.dns1", DHCP_PROP_NAME_PREFIX, interface);
Robert Greenwaltfaab26d2011-01-14 14:35:42 -0800105 property_get(prop_name, dns1, NULL);
106
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800107 snprintf(prop_name, sizeof(prop_name), "%s.%s.dns2", DHCP_PROP_NAME_PREFIX, interface);
Robert Greenwaltfaab26d2011-01-14 14:35:42 -0800108 property_get(prop_name, dns2, NULL);
109
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800110 snprintf(prop_name, sizeof(prop_name), "%s.%s.server", DHCP_PROP_NAME_PREFIX, interface);
Robert Greenwaltfaab26d2011-01-14 14:35:42 -0800111 property_get(prop_name, server, NULL);
112
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800113 snprintf(prop_name, sizeof(prop_name), "%s.%s.leasetime", DHCP_PROP_NAME_PREFIX, interface);
114 if (property_get(prop_name, prop_value, NULL)) {
115 *lease = atol(prop_value);
116 }
Robert Greenwaltfaab26d2011-01-14 14:35:42 -0800117 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800118}
119
Szymon Jakubczak8c85a002010-06-09 16:11:09 -0400120static const char *ipaddr_to_string(in_addr_t addr)
121{
122 struct in_addr in_addr;
123
124 in_addr.s_addr = addr;
125 return inet_ntoa(in_addr);
126}
127
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800128/*
129 * Start the dhcp client daemon, and wait for it to finish
130 * configuring the interface.
131 */
132int dhcp_do_request(const char *interface,
Robert Greenwaltfaab26d2011-01-14 14:35:42 -0800133 char *ipaddr,
134 char *gateway,
135 uint32_t *prefixLength,
136 char *dns1,
137 char *dns2,
138 char *server,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800139 uint32_t *lease)
140{
141 char result_prop_name[PROPERTY_KEY_MAX];
TK MUN9d157872011-02-23 18:58:36 +0900142 char daemon_prop_name[PROPERTY_KEY_MAX];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800143 char prop_value[PROPERTY_VALUE_MAX] = {'\0'};
Dmitry Shmidt9363b7d2009-12-14 15:41:54 -0800144 char daemon_cmd[PROPERTY_VALUE_MAX * 2];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800145 const char *ctrl_prop = "ctl.start";
146 const char *desired_status = "running";
147
148 snprintf(result_prop_name, sizeof(result_prop_name), "%s.%s.result",
149 DHCP_PROP_NAME_PREFIX,
150 interface);
TK MUN9d157872011-02-23 18:58:36 +0900151
152 snprintf(daemon_prop_name, sizeof(daemon_prop_name), "%s_%s",
153 DAEMON_PROP_NAME,
154 interface);
155
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800156 /* Erase any previous setting of the dhcp result property */
157 property_set(result_prop_name, "");
158
159 /* Start the daemon and wait until it's ready */
Dmitry Shmidt9363b7d2009-12-14 15:41:54 -0800160 if (property_get(HOSTNAME_PROP_NAME, prop_value, NULL) && (prop_value[0] != '\0'))
TK MUN9d157872011-02-23 18:58:36 +0900161 snprintf(daemon_cmd, sizeof(daemon_cmd), "%s_%s:-h %s %s", DAEMON_NAME, interface,
Dmitry Shmidt9363b7d2009-12-14 15:41:54 -0800162 prop_value, interface);
163 else
TK MUN9d157872011-02-23 18:58:36 +0900164 snprintf(daemon_cmd, sizeof(daemon_cmd), "%s_%s:%s", DAEMON_NAME, interface, interface);
Dmitry Shmidt9363b7d2009-12-14 15:41:54 -0800165 memset(prop_value, '\0', PROPERTY_VALUE_MAX);
166 property_set(ctrl_prop, daemon_cmd);
TK MUN9d157872011-02-23 18:58:36 +0900167 if (wait_for_property(daemon_prop_name, desired_status, 10) < 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800168 snprintf(errmsg, sizeof(errmsg), "%s", "Timed out waiting for dhcpcd to start");
169 return -1;
170 }
171
172 /* Wait for the daemon to return a result */
173 if (wait_for_property(result_prop_name, NULL, 30) < 0) {
174 snprintf(errmsg, sizeof(errmsg), "%s", "Timed out waiting for DHCP to finish");
175 return -1;
176 }
177
178 if (!property_get(result_prop_name, prop_value, NULL)) {
179 /* shouldn't ever happen, given the success of wait_for_property() */
180 snprintf(errmsg, sizeof(errmsg), "%s", "DHCP result property was not set");
181 return -1;
182 }
183 if (strcmp(prop_value, "ok") == 0) {
Szymon Jakubczak8c85a002010-06-09 16:11:09 -0400184 char dns_prop_name[PROPERTY_KEY_MAX];
Robert Greenwaltfaab26d2011-01-14 14:35:42 -0800185 if (fill_ip_info(interface, ipaddr, gateway, prefixLength, dns1, dns2, server, lease)
186 == -1) {
187 return -1;
188 }
189
190 /* copy dns data to system properties - TODO - remove this after we have async
191 * notification of renewal's */
Szymon Jakubczak8c85a002010-06-09 16:11:09 -0400192 snprintf(dns_prop_name, sizeof(dns_prop_name), "net.%s.dns1", interface);
193 property_set(dns_prop_name, *dns1 ? ipaddr_to_string(*dns1) : "");
194 snprintf(dns_prop_name, sizeof(dns_prop_name), "net.%s.dns2", interface);
195 property_set(dns_prop_name, *dns2 ? ipaddr_to_string(*dns2) : "");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800196 return 0;
197 } else {
198 snprintf(errmsg, sizeof(errmsg), "DHCP result was %s", prop_value);
199 return -1;
200 }
201}
202
203/**
204 * Stop the DHCP client daemon.
205 */
206int dhcp_stop(const char *interface)
207{
208 char result_prop_name[PROPERTY_KEY_MAX];
TK MUN9d157872011-02-23 18:58:36 +0900209 char daemon_prop_name[PROPERTY_KEY_MAX];
210 char daemon_cmd[PROPERTY_VALUE_MAX * 2];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800211 const char *ctrl_prop = "ctl.stop";
212 const char *desired_status = "stopped";
213
214 snprintf(result_prop_name, sizeof(result_prop_name), "%s.%s.result",
215 DHCP_PROP_NAME_PREFIX,
216 interface);
TK MUN9d157872011-02-23 18:58:36 +0900217
218 snprintf(daemon_prop_name, sizeof(daemon_prop_name), "%s_%s",
219 DAEMON_PROP_NAME,
220 interface);
221
222 snprintf(daemon_cmd, sizeof(daemon_cmd), "%s_%s", DAEMON_NAME, interface);
223
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800224 /* Stop the daemon and wait until it's reported to be stopped */
TK MUN9d157872011-02-23 18:58:36 +0900225 property_set(ctrl_prop, daemon_cmd);
226 if (wait_for_property(daemon_prop_name, desired_status, 5) < 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800227 return -1;
228 }
229 property_set(result_prop_name, "failed");
230 return 0;
231}
232
233/**
234 * Release the current DHCP client lease.
235 */
236int dhcp_release_lease(const char *interface)
237{
TK MUN9d157872011-02-23 18:58:36 +0900238 char daemon_prop_name[PROPERTY_KEY_MAX];
239 char daemon_cmd[PROPERTY_VALUE_MAX * 2];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800240 const char *ctrl_prop = "ctl.stop";
241 const char *desired_status = "stopped";
242
TK MUN9d157872011-02-23 18:58:36 +0900243 snprintf(daemon_prop_name, sizeof(daemon_prop_name), "%s_%s",
244 DAEMON_PROP_NAME,
245 interface);
246
247 snprintf(daemon_cmd, sizeof(daemon_cmd), "%s_%s", DAEMON_NAME, interface);
248
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800249 /* Stop the daemon and wait until it's reported to be stopped */
TK MUN9d157872011-02-23 18:58:36 +0900250 property_set(ctrl_prop, daemon_cmd);
251 if (wait_for_property(daemon_prop_name, desired_status, 5) < 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800252 return -1;
253 }
254 return 0;
255}
256
257char *dhcp_get_errmsg() {
258 return errmsg;
259}
TK MUN9d157872011-02-23 18:58:36 +0900260
261/**
262 * Run WiMAX dhcp renew service.
263 * "wimax_renew" service shoud be included in init.rc.
264 */
265int dhcp_do_request_renew(const char *interface,
266 in_addr_t *ipaddr,
267 in_addr_t *gateway,
268 in_addr_t *mask,
269 in_addr_t *dns1,
270 in_addr_t *dns2,
271 in_addr_t *server,
272 uint32_t *lease)
273{
274 char result_prop_name[PROPERTY_KEY_MAX];
275 char prop_value[PROPERTY_VALUE_MAX] = {'\0'};
276 char daemon_cmd[PROPERTY_VALUE_MAX * 2];
277 const char *ctrl_prop = "ctl.start";
278
279 snprintf(result_prop_name, sizeof(result_prop_name), "%s.%s.result",
280 DHCP_PROP_NAME_PREFIX,
281 interface);
282
283 /* Erase any previous setting of the dhcp result property */
284 property_set(result_prop_name, "");
285
286 /* Start the renew daemon and wait until it's ready */
287 snprintf(daemon_cmd, sizeof(daemon_cmd), "%s_%s:%s", DAEMON_NAME_RENEW, interface, interface);
288 memset(prop_value, '\0', PROPERTY_VALUE_MAX);
289 property_set(ctrl_prop, daemon_cmd);
290
291 /* Wait for the daemon to return a result */
292 if (wait_for_property(result_prop_name, NULL, 30) < 0) {
293 snprintf(errmsg, sizeof(errmsg), "%s", "Timed out waiting for DHCP Renew to finish");
294 return -1;
295 }
296
297 if (!property_get(result_prop_name, prop_value, NULL)) {
298 /* shouldn't ever happen, given the success of wait_for_property() */
299 snprintf(errmsg, sizeof(errmsg), "%s", "DHCP Renew result property was not set");
300 return -1;
301 }
302 if (strcmp(prop_value, "ok") == 0) {
303 fill_ip_info(interface, ipaddr, gateway, mask, dns1, dns2, server, lease);
304 return 0;
305 } else {
306 snprintf(errmsg, sizeof(errmsg), "DHCP Renew result was %s", prop_value);
307 return -1;
308 }
309}