blob: 5991ea8c0db502179e41523d90c16955818d12db [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
Irfan Sheriffbdaaec12011-04-15 16:04:24 -070082 snprintf(prop_name, sizeof(prop_name), "%s.%s.server", DHCP_PROP_NAME_PREFIX, interface);
83 property_get(prop_name, server, NULL);
84
85 //TODO: Handle IPv6 when we change system property usage
86 if (strcmp(gateway, "0.0.0.0") == 0) {
87 //DHCP server is our best bet as gateway
88 strncpy(gateway, server, PROPERTY_VALUE_MAX);
89 }
90
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080091 snprintf(prop_name, sizeof(prop_name), "%s.%s.mask", DHCP_PROP_NAME_PREFIX, interface);
Robert Greenwaltfaab26d2011-01-14 14:35:42 -080092 if (property_get(prop_name, prop_value, NULL)) {
93 int p;
94 // this conversion is v4 only, but this dhcp client is v4 only anyway
95 in_addr_t mask = ntohl(inet_addr(prop_value));
96 // Check netmask is a valid IP address. ntohl gives NONE response (all 1's) for
97 // non 255.255.255.255 inputs. if we get that value check if it is legit..
98 if (mask == INADDR_NONE && strcmp(prop_value, "255.255.255.255") != 0) {
99 snprintf(errmsg, sizeof(errmsg), "DHCP gave invalid net mask %s", prop_value);
100 return -1;
101 }
102 for (p = 0; p < 32; p++) {
103 if (mask == 0) break;
104 // check for non-contiguous netmask, e.g., 255.254.255.0
105 if ((mask & 0x80000000) == 0) {
106 snprintf(errmsg, sizeof(errmsg), "DHCP gave invalid net mask %s", prop_value);
107 return -1;
108 }
109 mask = mask << 1;
110 }
111 *prefixLength = p;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800112 }
113 snprintf(prop_name, sizeof(prop_name), "%s.%s.dns1", DHCP_PROP_NAME_PREFIX, interface);
Robert Greenwaltfaab26d2011-01-14 14:35:42 -0800114 property_get(prop_name, dns1, NULL);
115
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800116 snprintf(prop_name, sizeof(prop_name), "%s.%s.dns2", DHCP_PROP_NAME_PREFIX, interface);
Robert Greenwaltfaab26d2011-01-14 14:35:42 -0800117 property_get(prop_name, dns2, NULL);
118
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800119 snprintf(prop_name, sizeof(prop_name), "%s.%s.leasetime", DHCP_PROP_NAME_PREFIX, interface);
120 if (property_get(prop_name, prop_value, NULL)) {
121 *lease = atol(prop_value);
122 }
Robert Greenwaltfaab26d2011-01-14 14:35:42 -0800123 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800124}
125
Szymon Jakubczak8c85a002010-06-09 16:11:09 -0400126static const char *ipaddr_to_string(in_addr_t addr)
127{
128 struct in_addr in_addr;
129
130 in_addr.s_addr = addr;
131 return inet_ntoa(in_addr);
132}
133
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800134/*
135 * Start the dhcp client daemon, and wait for it to finish
136 * configuring the interface.
137 */
138int dhcp_do_request(const char *interface,
Robert Greenwaltfaab26d2011-01-14 14:35:42 -0800139 char *ipaddr,
140 char *gateway,
141 uint32_t *prefixLength,
142 char *dns1,
143 char *dns2,
144 char *server,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800145 uint32_t *lease)
146{
147 char result_prop_name[PROPERTY_KEY_MAX];
TK MUN9d157872011-02-23 18:58:36 +0900148 char daemon_prop_name[PROPERTY_KEY_MAX];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800149 char prop_value[PROPERTY_VALUE_MAX] = {'\0'};
Dmitry Shmidt9363b7d2009-12-14 15:41:54 -0800150 char daemon_cmd[PROPERTY_VALUE_MAX * 2];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800151 const char *ctrl_prop = "ctl.start";
152 const char *desired_status = "running";
153
154 snprintf(result_prop_name, sizeof(result_prop_name), "%s.%s.result",
155 DHCP_PROP_NAME_PREFIX,
156 interface);
TK MUN9d157872011-02-23 18:58:36 +0900157
158 snprintf(daemon_prop_name, sizeof(daemon_prop_name), "%s_%s",
159 DAEMON_PROP_NAME,
160 interface);
161
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800162 /* Erase any previous setting of the dhcp result property */
163 property_set(result_prop_name, "");
164
165 /* Start the daemon and wait until it's ready */
Dmitry Shmidt9363b7d2009-12-14 15:41:54 -0800166 if (property_get(HOSTNAME_PROP_NAME, prop_value, NULL) && (prop_value[0] != '\0'))
TK MUN9d157872011-02-23 18:58:36 +0900167 snprintf(daemon_cmd, sizeof(daemon_cmd), "%s_%s:-h %s %s", DAEMON_NAME, interface,
Dmitry Shmidt9363b7d2009-12-14 15:41:54 -0800168 prop_value, interface);
169 else
TK MUN9d157872011-02-23 18:58:36 +0900170 snprintf(daemon_cmd, sizeof(daemon_cmd), "%s_%s:%s", DAEMON_NAME, interface, interface);
Dmitry Shmidt9363b7d2009-12-14 15:41:54 -0800171 memset(prop_value, '\0', PROPERTY_VALUE_MAX);
172 property_set(ctrl_prop, daemon_cmd);
TK MUN9d157872011-02-23 18:58:36 +0900173 if (wait_for_property(daemon_prop_name, desired_status, 10) < 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800174 snprintf(errmsg, sizeof(errmsg), "%s", "Timed out waiting for dhcpcd to start");
175 return -1;
176 }
177
178 /* Wait for the daemon to return a result */
179 if (wait_for_property(result_prop_name, NULL, 30) < 0) {
180 snprintf(errmsg, sizeof(errmsg), "%s", "Timed out waiting for DHCP to finish");
181 return -1;
182 }
183
184 if (!property_get(result_prop_name, prop_value, NULL)) {
185 /* shouldn't ever happen, given the success of wait_for_property() */
186 snprintf(errmsg, sizeof(errmsg), "%s", "DHCP result property was not set");
187 return -1;
188 }
189 if (strcmp(prop_value, "ok") == 0) {
Szymon Jakubczak8c85a002010-06-09 16:11:09 -0400190 char dns_prop_name[PROPERTY_KEY_MAX];
Robert Greenwaltfaab26d2011-01-14 14:35:42 -0800191 if (fill_ip_info(interface, ipaddr, gateway, prefixLength, dns1, dns2, server, lease)
192 == -1) {
193 return -1;
194 }
195
196 /* copy dns data to system properties - TODO - remove this after we have async
197 * notification of renewal's */
Szymon Jakubczak8c85a002010-06-09 16:11:09 -0400198 snprintf(dns_prop_name, sizeof(dns_prop_name), "net.%s.dns1", interface);
199 property_set(dns_prop_name, *dns1 ? ipaddr_to_string(*dns1) : "");
200 snprintf(dns_prop_name, sizeof(dns_prop_name), "net.%s.dns2", interface);
201 property_set(dns_prop_name, *dns2 ? ipaddr_to_string(*dns2) : "");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800202 return 0;
203 } else {
204 snprintf(errmsg, sizeof(errmsg), "DHCP result was %s", prop_value);
205 return -1;
206 }
207}
208
209/**
210 * Stop the DHCP client daemon.
211 */
212int dhcp_stop(const char *interface)
213{
214 char result_prop_name[PROPERTY_KEY_MAX];
TK MUN9d157872011-02-23 18:58:36 +0900215 char daemon_prop_name[PROPERTY_KEY_MAX];
216 char daemon_cmd[PROPERTY_VALUE_MAX * 2];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800217 const char *ctrl_prop = "ctl.stop";
218 const char *desired_status = "stopped";
219
220 snprintf(result_prop_name, sizeof(result_prop_name), "%s.%s.result",
221 DHCP_PROP_NAME_PREFIX,
222 interface);
TK MUN9d157872011-02-23 18:58:36 +0900223
224 snprintf(daemon_prop_name, sizeof(daemon_prop_name), "%s_%s",
225 DAEMON_PROP_NAME,
226 interface);
227
228 snprintf(daemon_cmd, sizeof(daemon_cmd), "%s_%s", DAEMON_NAME, interface);
229
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800230 /* Stop the daemon and wait until it's reported to be stopped */
TK MUN9d157872011-02-23 18:58:36 +0900231 property_set(ctrl_prop, daemon_cmd);
232 if (wait_for_property(daemon_prop_name, desired_status, 5) < 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800233 return -1;
234 }
235 property_set(result_prop_name, "failed");
236 return 0;
237}
238
239/**
240 * Release the current DHCP client lease.
241 */
242int dhcp_release_lease(const char *interface)
243{
TK MUN9d157872011-02-23 18:58:36 +0900244 char daemon_prop_name[PROPERTY_KEY_MAX];
245 char daemon_cmd[PROPERTY_VALUE_MAX * 2];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800246 const char *ctrl_prop = "ctl.stop";
247 const char *desired_status = "stopped";
248
TK MUN9d157872011-02-23 18:58:36 +0900249 snprintf(daemon_prop_name, sizeof(daemon_prop_name), "%s_%s",
250 DAEMON_PROP_NAME,
251 interface);
252
253 snprintf(daemon_cmd, sizeof(daemon_cmd), "%s_%s", DAEMON_NAME, interface);
254
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800255 /* Stop the daemon and wait until it's reported to be stopped */
TK MUN9d157872011-02-23 18:58:36 +0900256 property_set(ctrl_prop, daemon_cmd);
257 if (wait_for_property(daemon_prop_name, desired_status, 5) < 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800258 return -1;
259 }
260 return 0;
261}
262
263char *dhcp_get_errmsg() {
264 return errmsg;
265}
TK MUN9d157872011-02-23 18:58:36 +0900266
267/**
268 * Run WiMAX dhcp renew service.
269 * "wimax_renew" service shoud be included in init.rc.
270 */
271int dhcp_do_request_renew(const char *interface,
272 in_addr_t *ipaddr,
273 in_addr_t *gateway,
274 in_addr_t *mask,
275 in_addr_t *dns1,
276 in_addr_t *dns2,
277 in_addr_t *server,
278 uint32_t *lease)
279{
280 char result_prop_name[PROPERTY_KEY_MAX];
281 char prop_value[PROPERTY_VALUE_MAX] = {'\0'};
282 char daemon_cmd[PROPERTY_VALUE_MAX * 2];
283 const char *ctrl_prop = "ctl.start";
284
285 snprintf(result_prop_name, sizeof(result_prop_name), "%s.%s.result",
286 DHCP_PROP_NAME_PREFIX,
287 interface);
288
289 /* Erase any previous setting of the dhcp result property */
290 property_set(result_prop_name, "");
291
292 /* Start the renew daemon and wait until it's ready */
293 snprintf(daemon_cmd, sizeof(daemon_cmd), "%s_%s:%s", DAEMON_NAME_RENEW, interface, interface);
294 memset(prop_value, '\0', PROPERTY_VALUE_MAX);
295 property_set(ctrl_prop, daemon_cmd);
296
297 /* Wait for the daemon to return a result */
298 if (wait_for_property(result_prop_name, NULL, 30) < 0) {
299 snprintf(errmsg, sizeof(errmsg), "%s", "Timed out waiting for DHCP Renew to finish");
300 return -1;
301 }
302
303 if (!property_get(result_prop_name, prop_value, NULL)) {
304 /* shouldn't ever happen, given the success of wait_for_property() */
305 snprintf(errmsg, sizeof(errmsg), "%s", "DHCP Renew result property was not set");
306 return -1;
307 }
308 if (strcmp(prop_value, "ok") == 0) {
309 fill_ip_info(interface, ipaddr, gateway, mask, dns1, dns2, server, lease);
310 return 0;
311 } else {
312 snprintf(errmsg, sizeof(errmsg), "DHCP Renew result was %s", prop_value);
313 return -1;
314 }
315}