1 /*
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>
21 #include <string.h>
22 #include <unistd.h>
23 #include <arpa/inet.h>
24 #include <netinet/in.h>
25
26 #include <cutils/properties.h>
27
28 static const char DAEMON_NAME[] = "dhcpcd";
29 static const char DAEMON_PROP_NAME[] = "init.svc.dhcpcd";
30 static const char HOSTNAME_PROP_NAME[] = "net.hostname";
31 static const char DHCP_PROP_NAME_PREFIX[] = "dhcp";
32 static const char DHCP_CONFIG_PATH[] = "/system/etc/dhcpcd/dhcpcd.conf";
33 static const int NAP_TIME = 200; /* wait for 200ms at a time */
34 /* when polling for property values */
35 static const char DAEMON_NAME_RENEW[] = "iprenew";
36 static char errmsg[100];
37 /* interface length for dhcpcd daemon start (dhcpcd_<interface> as defined in init.rc file)
38 * or for filling up system properties dhcpcd.<interface>.ipaddress, dhcpcd.<interface>.dns1
39 * and other properties on a successful bind
40 */
41 #define MAX_INTERFACE_LENGTH 25
42
43 /*
44 * P2p interface names increase sequentially p2p-p2p0-1, p2p-p2p0-2.. after
45 * group formation. This does not work well with system properties which can quickly
46 * exhaust or for specifiying a dhcp start target in init which requires
47 * interface to be pre-defined in init.rc file.
48 *
49 * This function returns a common string p2p for all p2p interfaces.
50 */
get_p2p_interface_replacement(const char * interface,char * p2p_interface)51 void get_p2p_interface_replacement(const char *interface, char *p2p_interface) {
52 /* Use p2p for any interface starting with p2p. */
53 if (strncmp(interface, "p2p",3) == 0) {
54 strncpy(p2p_interface, "p2p", MAX_INTERFACE_LENGTH);
55 } else {
56 strncpy(p2p_interface, interface, MAX_INTERFACE_LENGTH);
57 }
58 }
59
60 /*
61 * Wait for a system property to be assigned a specified value.
62 * If desired_value is NULL, then just wait for the property to
63 * be created with any value. maxwait is the maximum amount of
64 * time in seconds to wait before giving up.
65 */
wait_for_property(const char * name,const char * desired_value,int maxwait)66 static int wait_for_property(const char *name, const char *desired_value, int maxwait)
67 {
68 char value[PROPERTY_VALUE_MAX] = {'\0'};
69 int maxnaps = (maxwait * 1000) / NAP_TIME;
70
71 if (maxnaps < 1) {
72 maxnaps = 1;
73 }
74
75 while (maxnaps-- > 0) {
76 usleep(NAP_TIME * 1000);
77 if (property_get(name, value, NULL)) {
78 if (desired_value == NULL ||
79 strcmp(value, desired_value) == 0) {
80 return 0;
81 }
82 }
83 }
84 return -1; /* failure */
85 }
86
fill_ip_info(const char * interface,char * ipaddr,char * gateway,uint32_t * prefixLength,char * dns[],char * server,uint32_t * lease,char * vendorInfo,char * domain)87 static int fill_ip_info(const char *interface,
88 char *ipaddr,
89 char *gateway,
90 uint32_t *prefixLength,
91 char *dns[],
92 char *server,
93 uint32_t *lease,
94 char *vendorInfo,
95 char *domain)
96 {
97 char prop_name[PROPERTY_KEY_MAX];
98 char prop_value[PROPERTY_VALUE_MAX];
99 /* Interface name after converting p2p0-p2p0-X to p2p to reuse system properties */
100 char p2p_interface[MAX_INTERFACE_LENGTH];
101 int x;
102
103 get_p2p_interface_replacement(interface, p2p_interface);
104
105 snprintf(prop_name, sizeof(prop_name), "%s.%s.ipaddress", DHCP_PROP_NAME_PREFIX, p2p_interface);
106 property_get(prop_name, ipaddr, NULL);
107
108 snprintf(prop_name, sizeof(prop_name), "%s.%s.gateway", DHCP_PROP_NAME_PREFIX, p2p_interface);
109 property_get(prop_name, gateway, NULL);
110
111 snprintf(prop_name, sizeof(prop_name), "%s.%s.server", DHCP_PROP_NAME_PREFIX, p2p_interface);
112 property_get(prop_name, server, NULL);
113
114 //TODO: Handle IPv6 when we change system property usage
115 if (gateway[0] == '\0' || strncmp(gateway, "0.0.0.0", 7) == 0) {
116 //DHCP server is our best bet as gateway
117 strncpy(gateway, server, PROPERTY_VALUE_MAX);
118 }
119
120 snprintf(prop_name, sizeof(prop_name), "%s.%s.mask", DHCP_PROP_NAME_PREFIX, p2p_interface);
121 if (property_get(prop_name, prop_value, NULL)) {
122 int p;
123 // this conversion is v4 only, but this dhcp client is v4 only anyway
124 in_addr_t mask = ntohl(inet_addr(prop_value));
125 // Check netmask is a valid IP address. ntohl gives NONE response (all 1's) for
126 // non 255.255.255.255 inputs. if we get that value check if it is legit..
127 if (mask == INADDR_NONE && strcmp(prop_value, "255.255.255.255") != 0) {
128 snprintf(errmsg, sizeof(errmsg), "DHCP gave invalid net mask %s", prop_value);
129 return -1;
130 }
131 for (p = 0; p < 32; p++) {
132 if (mask == 0) break;
133 // check for non-contiguous netmask, e.g., 255.254.255.0
134 if ((mask & 0x80000000) == 0) {
135 snprintf(errmsg, sizeof(errmsg), "DHCP gave invalid net mask %s", prop_value);
136 return -1;
137 }
138 mask = mask << 1;
139 }
140 *prefixLength = p;
141 }
142
143 for (x=0; dns[x] != NULL; x++) {
144 snprintf(prop_name, sizeof(prop_name), "%s.%s.dns%d", DHCP_PROP_NAME_PREFIX, p2p_interface, x+1);
145 property_get(prop_name, dns[x], NULL);
146 }
147
148 snprintf(prop_name, sizeof(prop_name), "%s.%s.leasetime", DHCP_PROP_NAME_PREFIX, p2p_interface);
149 if (property_get(prop_name, prop_value, NULL)) {
150 *lease = atol(prop_value);
151 }
152
153 snprintf(prop_name, sizeof(prop_name), "%s.%s.vendorInfo", DHCP_PROP_NAME_PREFIX,
154 p2p_interface);
155 property_get(prop_name, vendorInfo, NULL);
156
157 snprintf(prop_name, sizeof(prop_name), "%s.%s.domain", DHCP_PROP_NAME_PREFIX,
158 p2p_interface);
159 property_get(prop_name, domain, NULL);
160
161 return 0;
162 }
163
ipaddr_to_string(in_addr_t addr)164 static const char *ipaddr_to_string(in_addr_t addr)
165 {
166 struct in_addr in_addr;
167
168 in_addr.s_addr = addr;
169 return inet_ntoa(in_addr);
170 }
171
172 /*
173 * Start the dhcp client daemon, and wait for it to finish
174 * configuring the interface.
175 *
176 * The device init.rc file needs a corresponding entry for this work.
177 *
178 * Example:
179 * service dhcpcd_<interface> /system/bin/dhcpcd -ABKL -f dhcpcd.conf
180 */
dhcp_do_request(const char * interface,char * ipaddr,char * gateway,uint32_t * prefixLength,char * dns[],char * server,uint32_t * lease,char * vendorInfo,char * domain)181 int dhcp_do_request(const char *interface,
182 char *ipaddr,
183 char *gateway,
184 uint32_t *prefixLength,
185 char *dns[],
186 char *server,
187 uint32_t *lease,
188 char *vendorInfo,
189 char *domain)
190 {
191 char result_prop_name[PROPERTY_KEY_MAX];
192 char daemon_prop_name[PROPERTY_KEY_MAX];
193 char prop_value[PROPERTY_VALUE_MAX] = {'\0'};
194 char daemon_cmd[PROPERTY_VALUE_MAX * 2 + sizeof(DHCP_CONFIG_PATH)];
195 const char *ctrl_prop = "ctl.start";
196 const char *desired_status = "running";
197 /* Interface name after converting p2p0-p2p0-X to p2p to reuse system properties */
198 char p2p_interface[MAX_INTERFACE_LENGTH];
199
200 get_p2p_interface_replacement(interface, p2p_interface);
201
202 snprintf(result_prop_name, sizeof(result_prop_name), "%s.%s.result",
203 DHCP_PROP_NAME_PREFIX,
204 p2p_interface);
205
206 snprintf(daemon_prop_name, sizeof(daemon_prop_name), "%s_%s",
207 DAEMON_PROP_NAME,
208 p2p_interface);
209
210 /* Erase any previous setting of the dhcp result property */
211 property_set(result_prop_name, "");
212
213 /* Start the daemon and wait until it's ready */
214 if (property_get(HOSTNAME_PROP_NAME, prop_value, NULL) && (prop_value[0] != '\0'))
215 snprintf(daemon_cmd, sizeof(daemon_cmd), "%s_%s:-f %s -h %s %s", DAEMON_NAME,
216 p2p_interface, DHCP_CONFIG_PATH, prop_value, interface);
217 else
218 snprintf(daemon_cmd, sizeof(daemon_cmd), "%s_%s:-f %s %s", DAEMON_NAME,
219 p2p_interface, DHCP_CONFIG_PATH, interface);
220 memset(prop_value, '\0', PROPERTY_VALUE_MAX);
221 property_set(ctrl_prop, daemon_cmd);
222 if (wait_for_property(daemon_prop_name, desired_status, 10) < 0) {
223 snprintf(errmsg, sizeof(errmsg), "%s", "Timed out waiting for dhcpcd to start");
224 return -1;
225 }
226
227 /* Wait for the daemon to return a result */
228 if (wait_for_property(result_prop_name, NULL, 30) < 0) {
229 snprintf(errmsg, sizeof(errmsg), "%s", "Timed out waiting for DHCP to finish");
230 return -1;
231 }
232
233 if (!property_get(result_prop_name, prop_value, NULL)) {
234 /* shouldn't ever happen, given the success of wait_for_property() */
235 snprintf(errmsg, sizeof(errmsg), "%s", "DHCP result property was not set");
236 return -1;
237 }
238 if (strcmp(prop_value, "ok") == 0) {
239 char dns_prop_name[PROPERTY_KEY_MAX];
240 if (fill_ip_info(interface, ipaddr, gateway, prefixLength, dns,
241 server, lease, vendorInfo, domain) == -1) {
242 return -1;
243 }
244 return 0;
245 } else {
246 snprintf(errmsg, sizeof(errmsg), "DHCP result was %s", prop_value);
247 return -1;
248 }
249 }
250
251 /**
252 * Stop the DHCP client daemon.
253 */
dhcp_stop(const char * interface)254 int dhcp_stop(const char *interface)
255 {
256 char result_prop_name[PROPERTY_KEY_MAX];
257 char daemon_prop_name[PROPERTY_KEY_MAX];
258 char daemon_cmd[PROPERTY_VALUE_MAX * 2];
259 const char *ctrl_prop = "ctl.stop";
260 const char *desired_status = "stopped";
261
262 char p2p_interface[MAX_INTERFACE_LENGTH];
263
264 get_p2p_interface_replacement(interface, p2p_interface);
265
266 snprintf(result_prop_name, sizeof(result_prop_name), "%s.%s.result",
267 DHCP_PROP_NAME_PREFIX,
268 p2p_interface);
269
270 snprintf(daemon_prop_name, sizeof(daemon_prop_name), "%s_%s",
271 DAEMON_PROP_NAME,
272 p2p_interface);
273
274 snprintf(daemon_cmd, sizeof(daemon_cmd), "%s_%s", DAEMON_NAME, p2p_interface);
275
276 /* Stop the daemon and wait until it's reported to be stopped */
277 property_set(ctrl_prop, daemon_cmd);
278 if (wait_for_property(daemon_prop_name, desired_status, 5) < 0) {
279 return -1;
280 }
281 property_set(result_prop_name, "failed");
282 return 0;
283 }
284
285 /**
286 * Release the current DHCP client lease.
287 */
dhcp_release_lease(const char * interface)288 int dhcp_release_lease(const char *interface)
289 {
290 char daemon_prop_name[PROPERTY_KEY_MAX];
291 char daemon_cmd[PROPERTY_VALUE_MAX * 2];
292 const char *ctrl_prop = "ctl.stop";
293 const char *desired_status = "stopped";
294
295 char p2p_interface[MAX_INTERFACE_LENGTH];
296
297 get_p2p_interface_replacement(interface, p2p_interface);
298
299 snprintf(daemon_prop_name, sizeof(daemon_prop_name), "%s_%s",
300 DAEMON_PROP_NAME,
301 p2p_interface);
302
303 snprintf(daemon_cmd, sizeof(daemon_cmd), "%s_%s", DAEMON_NAME, p2p_interface);
304
305 /* Stop the daemon and wait until it's reported to be stopped */
306 property_set(ctrl_prop, daemon_cmd);
307 if (wait_for_property(daemon_prop_name, desired_status, 5) < 0) {
308 return -1;
309 }
310 return 0;
311 }
312
dhcp_get_errmsg()313 char *dhcp_get_errmsg() {
314 return errmsg;
315 }
316
317 /**
318 * The device init.rc file needs a corresponding entry.
319 *
320 * Example:
321 * service iprenew_<interface> /system/bin/dhcpcd -n
322 *
323 */
dhcp_do_request_renew(const char * interface,char * ipaddr,char * gateway,uint32_t * prefixLength,char * dns[],char * server,uint32_t * lease,char * vendorInfo,char * domain)324 int dhcp_do_request_renew(const char *interface,
325 char *ipaddr,
326 char *gateway,
327 uint32_t *prefixLength,
328 char *dns[],
329 char *server,
330 uint32_t *lease,
331 char *vendorInfo,
332 char *domain)
333 {
334 char result_prop_name[PROPERTY_KEY_MAX];
335 char prop_value[PROPERTY_VALUE_MAX] = {'\0'};
336 char daemon_cmd[PROPERTY_VALUE_MAX * 2];
337 const char *ctrl_prop = "ctl.start";
338
339 char p2p_interface[MAX_INTERFACE_LENGTH];
340
341 get_p2p_interface_replacement(interface, p2p_interface);
342
343 snprintf(result_prop_name, sizeof(result_prop_name), "%s.%s.result",
344 DHCP_PROP_NAME_PREFIX,
345 p2p_interface);
346
347 /* Erase any previous setting of the dhcp result property */
348 property_set(result_prop_name, "");
349
350 /* Start the renew daemon and wait until it's ready */
351 snprintf(daemon_cmd, sizeof(daemon_cmd), "%s_%s:%s", DAEMON_NAME_RENEW,
352 p2p_interface, interface);
353 memset(prop_value, '\0', PROPERTY_VALUE_MAX);
354 property_set(ctrl_prop, daemon_cmd);
355
356 /* Wait for the daemon to return a result */
357 if (wait_for_property(result_prop_name, NULL, 30) < 0) {
358 snprintf(errmsg, sizeof(errmsg), "%s", "Timed out waiting for DHCP Renew to finish");
359 return -1;
360 }
361
362 if (!property_get(result_prop_name, prop_value, NULL)) {
363 /* shouldn't ever happen, given the success of wait_for_property() */
364 snprintf(errmsg, sizeof(errmsg), "%s", "DHCP Renew result property was not set");
365 return -1;
366 }
367 if (strcmp(prop_value, "ok") == 0) {
368 fill_ip_info(interface, ipaddr, gateway, prefixLength, dns,
369 server, lease, vendorInfo, domain);
370 return 0;
371 } else {
372 snprintf(errmsg, sizeof(errmsg), "DHCP Renew result was %s", prop_value);
373 return -1;
374 }
375 }
376