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 int NAP_TIME = 200; /* wait for 200ms at a time */
33 /* when polling for property values */
34 static const char DAEMON_NAME_RENEW[] = "iprenew";
35 static char errmsg[100];
36 /* interface suffix on dhcpcd */
37 #define MAX_DAEMON_SUFFIX 25
38
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 */
wait_for_property(const char * name,const char * desired_value,int maxwait)45 static int wait_for_property(const char *name, const char *desired_value, int maxwait)
46 {
47 char value[PROPERTY_VALUE_MAX] = {'\0'};
48 int maxnaps = (maxwait * 1000) / NAP_TIME;
49
50 if (maxnaps < 1) {
51 maxnaps = 1;
52 }
53
54 while (maxnaps-- > 0) {
55 usleep(NAP_TIME * 1000);
56 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
fill_ip_info(const char * interface,char * ipaddr,char * gateway,uint32_t * prefixLength,char * dns1,char * dns2,char * server,uint32_t * lease)66 static 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,
73 uint32_t *lease)
74 {
75 char prop_name[PROPERTY_KEY_MAX];
76 char prop_value[PROPERTY_VALUE_MAX];
77
78 snprintf(prop_name, sizeof(prop_name), "%s.%s.ipaddress", DHCP_PROP_NAME_PREFIX, interface);
79 property_get(prop_name, ipaddr, NULL);
80
81 snprintf(prop_name, sizeof(prop_name), "%s.%s.gateway", DHCP_PROP_NAME_PREFIX, interface);
82 property_get(prop_name, gateway, NULL);
83
84 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
93 snprintf(prop_name, sizeof(prop_name), "%s.%s.mask", DHCP_PROP_NAME_PREFIX, interface);
94 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;
114 }
115 snprintf(prop_name, sizeof(prop_name), "%s.%s.dns1", DHCP_PROP_NAME_PREFIX, interface);
116 property_get(prop_name, dns1, NULL);
117
118 snprintf(prop_name, sizeof(prop_name), "%s.%s.dns2", DHCP_PROP_NAME_PREFIX, interface);
119 property_get(prop_name, dns2, NULL);
120
121 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 }
125 return 0;
126 }
127
ipaddr_to_string(in_addr_t addr)128 static 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
get_daemon_suffix(const char * interface,char * daemon_suffix)136 void 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
145 /*
146 * Start the dhcp client daemon, and wait for it to finish
147 * configuring the interface.
148 */
dhcp_do_request(const char * interface,char * ipaddr,char * gateway,uint32_t * prefixLength,char * dns1,char * dns2,char * server,uint32_t * lease)149 int dhcp_do_request(const char *interface,
150 char *ipaddr,
151 char *gateway,
152 uint32_t *prefixLength,
153 char *dns1,
154 char *dns2,
155 char *server,
156 uint32_t *lease)
157 {
158 char result_prop_name[PROPERTY_KEY_MAX];
159 char daemon_prop_name[PROPERTY_KEY_MAX];
160 char prop_value[PROPERTY_VALUE_MAX] = {'\0'};
161 char daemon_cmd[PROPERTY_VALUE_MAX * 2];
162 const char *ctrl_prop = "ctl.start";
163 const char *desired_status = "running";
164 char daemon_suffix[MAX_DAEMON_SUFFIX];
165
166 get_daemon_suffix(interface, daemon_suffix);
167
168 snprintf(result_prop_name, sizeof(result_prop_name), "%s.%s.result",
169 DHCP_PROP_NAME_PREFIX,
170 interface);
171
172 snprintf(daemon_prop_name, sizeof(daemon_prop_name), "%s_%s",
173 DAEMON_PROP_NAME,
174 daemon_suffix);
175
176 /* 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 */
180 if (property_get(HOSTNAME_PROP_NAME, prop_value, NULL) && (prop_value[0] != '\0'))
181 snprintf(daemon_cmd, sizeof(daemon_cmd), "%s_%s:-h %s %s", DAEMON_NAME, daemon_suffix,
182 prop_value, interface);
183 else
184 snprintf(daemon_cmd, sizeof(daemon_cmd), "%s_%s:%s", DAEMON_NAME, daemon_suffix, interface);
185 memset(prop_value, '\0', PROPERTY_VALUE_MAX);
186 property_set(ctrl_prop, daemon_cmd);
187 if (wait_for_property(daemon_prop_name, desired_status, 10) < 0) {
188 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) {
204 char dns_prop_name[PROPERTY_KEY_MAX];
205 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 */
212 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) : "");
216 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 */
dhcp_stop(const char * interface)226 int dhcp_stop(const char *interface)
227 {
228 char result_prop_name[PROPERTY_KEY_MAX];
229 char daemon_prop_name[PROPERTY_KEY_MAX];
230 char daemon_cmd[PROPERTY_VALUE_MAX * 2];
231 const char *ctrl_prop = "ctl.stop";
232 const char *desired_status = "stopped";
233
234 char daemon_suffix[MAX_DAEMON_SUFFIX];
235
236 get_daemon_suffix(interface, daemon_suffix);
237
238 snprintf(result_prop_name, sizeof(result_prop_name), "%s.%s.result",
239 DHCP_PROP_NAME_PREFIX,
240 interface);
241
242 snprintf(daemon_prop_name, sizeof(daemon_prop_name), "%s_%s",
243 DAEMON_PROP_NAME,
244 daemon_suffix);
245
246 snprintf(daemon_cmd, sizeof(daemon_cmd), "%s_%s", DAEMON_NAME, daemon_suffix);
247
248 /* Stop the daemon and wait until it's reported to be stopped */
249 property_set(ctrl_prop, daemon_cmd);
250 if (wait_for_property(daemon_prop_name, desired_status, 5) < 0) {
251 return -1;
252 }
253 property_set(result_prop_name, "failed");
254 return 0;
255 }
256
257 /**
258 * Release the current DHCP client lease.
259 */
dhcp_release_lease(const char * interface)260 int dhcp_release_lease(const char *interface)
261 {
262 char daemon_prop_name[PROPERTY_KEY_MAX];
263 char daemon_cmd[PROPERTY_VALUE_MAX * 2];
264 const char *ctrl_prop = "ctl.stop";
265 const char *desired_status = "stopped";
266
267 char daemon_suffix[MAX_DAEMON_SUFFIX];
268
269 get_daemon_suffix(interface, daemon_suffix);
270
271 snprintf(daemon_prop_name, sizeof(daemon_prop_name), "%s_%s",
272 DAEMON_PROP_NAME,
273 daemon_suffix);
274
275 snprintf(daemon_cmd, sizeof(daemon_cmd), "%s_%s", DAEMON_NAME, daemon_suffix);
276
277 /* Stop the daemon and wait until it's reported to be stopped */
278 property_set(ctrl_prop, daemon_cmd);
279 if (wait_for_property(daemon_prop_name, desired_status, 5) < 0) {
280 return -1;
281 }
282 return 0;
283 }
284
dhcp_get_errmsg()285 char *dhcp_get_errmsg() {
286 return errmsg;
287 }
288
289 /**
290 * Run WiMAX dhcp renew service.
291 * "wimax_renew" service shoud be included in init.rc.
292 */
dhcp_do_request_renew(const char * interface,in_addr_t * ipaddr,in_addr_t * gateway,in_addr_t * mask,in_addr_t * dns1,in_addr_t * dns2,in_addr_t * server,uint32_t * lease)293 int 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
307 char daemon_suffix[MAX_DAEMON_SUFFIX];
308
309 get_daemon_suffix(interface, daemon_suffix);
310
311 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 */
319 snprintf(daemon_cmd, sizeof(daemon_cmd), "%s_%s:%s", DAEMON_NAME_RENEW,
320 daemon_suffix, interface);
321 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 }
343