blob: 9b7bd8d0cd8a1ece13b1756c7af56e88976f6fa9 [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";
Dmitry Shmidt62d6f742012-07-23 17:39:30 -070032static const char DHCP_CONFIG_PATH[] = "/system/etc/dhcpcd/dhcpcd.conf";
Irfan Sheriffb1723b62010-12-21 10:31:05 -080033static const int NAP_TIME = 200; /* wait for 200ms at a time */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080034 /* when polling for property values */
TK MUN9d157872011-02-23 18:58:36 +090035static const char DAEMON_NAME_RENEW[] = "iprenew";
Erik Klinec2291a72015-02-19 19:15:34 +090036static char errmsg[100] = "";
Irfan Sheriff89f58cf2012-05-23 10:30:05 -070037/* 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 */
51void 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}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080059
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 */
66static int wait_for_property(const char *name, const char *desired_value, int maxwait)
67{
68 char value[PROPERTY_VALUE_MAX] = {'\0'};
Irfan Sheriffb1723b62010-12-21 10:31:05 -080069 int maxnaps = (maxwait * 1000) / NAP_TIME;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080070
71 if (maxnaps < 1) {
72 maxnaps = 1;
73 }
74
75 while (maxnaps-- > 0) {
Irfan Sheriffb1723b62010-12-21 10:31:05 -080076 usleep(NAP_TIME * 1000);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080077 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
Robert Greenwaltfaab26d2011-01-14 14:35:42 -080087static int fill_ip_info(const char *interface,
88 char *ipaddr,
89 char *gateway,
90 uint32_t *prefixLength,
Robert Greenwaltfdd57312013-01-09 16:22:12 -080091 char *dns[],
Robert Greenwaltfaab26d2011-01-14 14:35:42 -080092 char *server,
Jeff Sharkey0fb8ec82012-04-18 21:54:55 -070093 uint32_t *lease,
Robert Greenwalt6ecbdca2012-11-13 10:56:01 -080094 char *vendorInfo,
Dmitry Shmidtbe062102013-07-24 17:37:05 -070095 char *domain,
96 char *mtu)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080097{
98 char prop_name[PROPERTY_KEY_MAX];
99 char prop_value[PROPERTY_VALUE_MAX];
Irfan Sheriff89f58cf2012-05-23 10:30:05 -0700100 /* Interface name after converting p2p0-p2p0-X to p2p to reuse system properties */
101 char p2p_interface[MAX_INTERFACE_LENGTH];
Robert Greenwaltfdd57312013-01-09 16:22:12 -0800102 int x;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800103
Irfan Sheriff89f58cf2012-05-23 10:30:05 -0700104 get_p2p_interface_replacement(interface, p2p_interface);
105
106 snprintf(prop_name, sizeof(prop_name), "%s.%s.ipaddress", DHCP_PROP_NAME_PREFIX, p2p_interface);
Robert Greenwaltfaab26d2011-01-14 14:35:42 -0800107 property_get(prop_name, ipaddr, NULL);
108
Irfan Sheriff89f58cf2012-05-23 10:30:05 -0700109 snprintf(prop_name, sizeof(prop_name), "%s.%s.gateway", DHCP_PROP_NAME_PREFIX, p2p_interface);
Robert Greenwaltfaab26d2011-01-14 14:35:42 -0800110 property_get(prop_name, gateway, NULL);
111
Irfan Sheriff89f58cf2012-05-23 10:30:05 -0700112 snprintf(prop_name, sizeof(prop_name), "%s.%s.server", DHCP_PROP_NAME_PREFIX, p2p_interface);
Irfan Sheriffbdaaec12011-04-15 16:04:24 -0700113 property_get(prop_name, server, NULL);
114
115 //TODO: Handle IPv6 when we change system property usage
Irfan Sheriff94cecfc2012-12-07 10:36:29 -0800116 if (gateway[0] == '\0' || strncmp(gateway, "0.0.0.0", 7) == 0) {
Irfan Sheriffbdaaec12011-04-15 16:04:24 -0700117 //DHCP server is our best bet as gateway
118 strncpy(gateway, server, PROPERTY_VALUE_MAX);
119 }
120
Irfan Sheriff89f58cf2012-05-23 10:30:05 -0700121 snprintf(prop_name, sizeof(prop_name), "%s.%s.mask", DHCP_PROP_NAME_PREFIX, p2p_interface);
Robert Greenwaltfaab26d2011-01-14 14:35:42 -0800122 if (property_get(prop_name, prop_value, NULL)) {
123 int p;
124 // this conversion is v4 only, but this dhcp client is v4 only anyway
125 in_addr_t mask = ntohl(inet_addr(prop_value));
126 // Check netmask is a valid IP address. ntohl gives NONE response (all 1's) for
127 // non 255.255.255.255 inputs. if we get that value check if it is legit..
128 if (mask == INADDR_NONE && strcmp(prop_value, "255.255.255.255") != 0) {
129 snprintf(errmsg, sizeof(errmsg), "DHCP gave invalid net mask %s", prop_value);
130 return -1;
131 }
132 for (p = 0; p < 32; p++) {
133 if (mask == 0) break;
134 // check for non-contiguous netmask, e.g., 255.254.255.0
135 if ((mask & 0x80000000) == 0) {
136 snprintf(errmsg, sizeof(errmsg), "DHCP gave invalid net mask %s", prop_value);
137 return -1;
138 }
139 mask = mask << 1;
140 }
141 *prefixLength = p;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800142 }
Robert Greenwaltfaab26d2011-01-14 14:35:42 -0800143
Robert Greenwaltfdd57312013-01-09 16:22:12 -0800144 for (x=0; dns[x] != NULL; x++) {
145 snprintf(prop_name, sizeof(prop_name), "%s.%s.dns%d", DHCP_PROP_NAME_PREFIX, p2p_interface, x+1);
146 property_get(prop_name, dns[x], NULL);
147 }
Robert Greenwaltfaab26d2011-01-14 14:35:42 -0800148
Irfan Sheriff89f58cf2012-05-23 10:30:05 -0700149 snprintf(prop_name, sizeof(prop_name), "%s.%s.leasetime", DHCP_PROP_NAME_PREFIX, p2p_interface);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800150 if (property_get(prop_name, prop_value, NULL)) {
151 *lease = atol(prop_value);
152 }
Jeff Sharkey0fb8ec82012-04-18 21:54:55 -0700153
Irfan Sheriff89f58cf2012-05-23 10:30:05 -0700154 snprintf(prop_name, sizeof(prop_name), "%s.%s.vendorInfo", DHCP_PROP_NAME_PREFIX,
155 p2p_interface);
Jeff Sharkey0fb8ec82012-04-18 21:54:55 -0700156 property_get(prop_name, vendorInfo, NULL);
157
Robert Greenwalt6ecbdca2012-11-13 10:56:01 -0800158 snprintf(prop_name, sizeof(prop_name), "%s.%s.domain", DHCP_PROP_NAME_PREFIX,
159 p2p_interface);
160 property_get(prop_name, domain, NULL);
161
Dmitry Shmidtbe062102013-07-24 17:37:05 -0700162 snprintf(prop_name, sizeof(prop_name), "%s.%s.mtu", DHCP_PROP_NAME_PREFIX,
163 p2p_interface);
164 property_get(prop_name, mtu, NULL);
165
Robert Greenwaltfaab26d2011-01-14 14:35:42 -0800166 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800167}
168
Szymon Jakubczak8c85a002010-06-09 16:11:09 -0400169static const char *ipaddr_to_string(in_addr_t addr)
170{
171 struct in_addr in_addr;
172
173 in_addr.s_addr = addr;
174 return inet_ntoa(in_addr);
175}
176
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800177/*
Erik Klinec2291a72015-02-19 19:15:34 +0900178 * Get any available DHCP results.
179 */
180int dhcp_get_results(const char *interface,
181 char *ipaddr,
182 char *gateway,
183 uint32_t *prefixLength,
184 char *dns[],
185 char *server,
186 uint32_t *lease,
187 char *vendorInfo,
188 char *domain,
189 char *mtu)
190{
191 char result_prop_name[PROPERTY_KEY_MAX];
192 char prop_value[PROPERTY_VALUE_MAX];
193
194 /* Interface name after converting p2p0-p2p0-X to p2p to reuse system properties */
195 char p2p_interface[MAX_INTERFACE_LENGTH];
196 get_p2p_interface_replacement(interface, p2p_interface);
197 snprintf(result_prop_name, sizeof(result_prop_name), "%s.%s.result",
198 DHCP_PROP_NAME_PREFIX,
199 p2p_interface);
200
201 memset(prop_value, '\0', PROPERTY_VALUE_MAX);
202 if (!property_get(result_prop_name, prop_value, NULL)) {
203 snprintf(errmsg, sizeof(errmsg), "%s", "DHCP result property was not set");
204 return -1;
205 }
206 if (strcmp(prop_value, "ok") == 0) {
207 if (fill_ip_info(interface, ipaddr, gateway, prefixLength, dns,
208 server, lease, vendorInfo, domain, mtu) == -1) {
209 return -1;
210 }
211 return 0;
212 } else {
213 snprintf(errmsg, sizeof(errmsg), "DHCP result was %s", prop_value);
214 return -1;
215 }
216}
217
218/*
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800219 * Start the dhcp client daemon, and wait for it to finish
220 * configuring the interface.
Irfan Sheriff35c28602011-11-09 11:10:50 -0800221 *
222 * The device init.rc file needs a corresponding entry for this work.
223 *
224 * Example:
Dmitry Shmidt62d6f742012-07-23 17:39:30 -0700225 * service dhcpcd_<interface> /system/bin/dhcpcd -ABKL -f dhcpcd.conf
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800226 */
Erik Klinec2291a72015-02-19 19:15:34 +0900227int dhcp_start(const char *interface)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800228{
229 char result_prop_name[PROPERTY_KEY_MAX];
TK MUN9d157872011-02-23 18:58:36 +0900230 char daemon_prop_name[PROPERTY_KEY_MAX];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800231 char prop_value[PROPERTY_VALUE_MAX] = {'\0'};
Dmitry Shmidt62d6f742012-07-23 17:39:30 -0700232 char daemon_cmd[PROPERTY_VALUE_MAX * 2 + sizeof(DHCP_CONFIG_PATH)];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800233 const char *ctrl_prop = "ctl.start";
234 const char *desired_status = "running";
Irfan Sheriff89f58cf2012-05-23 10:30:05 -0700235 /* Interface name after converting p2p0-p2p0-X to p2p to reuse system properties */
236 char p2p_interface[MAX_INTERFACE_LENGTH];
repo synca329b422011-07-29 12:07:34 -0700237
Irfan Sheriff89f58cf2012-05-23 10:30:05 -0700238 get_p2p_interface_replacement(interface, p2p_interface);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800239
240 snprintf(result_prop_name, sizeof(result_prop_name), "%s.%s.result",
241 DHCP_PROP_NAME_PREFIX,
Irfan Sheriff89f58cf2012-05-23 10:30:05 -0700242 p2p_interface);
TK MUN9d157872011-02-23 18:58:36 +0900243
244 snprintf(daemon_prop_name, sizeof(daemon_prop_name), "%s_%s",
245 DAEMON_PROP_NAME,
Irfan Sheriff89f58cf2012-05-23 10:30:05 -0700246 p2p_interface);
TK MUN9d157872011-02-23 18:58:36 +0900247
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800248 /* Erase any previous setting of the dhcp result property */
249 property_set(result_prop_name, "");
250
251 /* Start the daemon and wait until it's ready */
Dmitry Shmidt9363b7d2009-12-14 15:41:54 -0800252 if (property_get(HOSTNAME_PROP_NAME, prop_value, NULL) && (prop_value[0] != '\0'))
Dmitry Shmidt62d6f742012-07-23 17:39:30 -0700253 snprintf(daemon_cmd, sizeof(daemon_cmd), "%s_%s:-f %s -h %s %s", DAEMON_NAME,
254 p2p_interface, DHCP_CONFIG_PATH, prop_value, interface);
Dmitry Shmidt9363b7d2009-12-14 15:41:54 -0800255 else
Dmitry Shmidt62d6f742012-07-23 17:39:30 -0700256 snprintf(daemon_cmd, sizeof(daemon_cmd), "%s_%s:-f %s %s", DAEMON_NAME,
Matt Gumbelba2ba5c2013-01-04 09:53:42 -0800257 p2p_interface, DHCP_CONFIG_PATH, interface);
Dmitry Shmidt9363b7d2009-12-14 15:41:54 -0800258 memset(prop_value, '\0', PROPERTY_VALUE_MAX);
259 property_set(ctrl_prop, daemon_cmd);
TK MUN9d157872011-02-23 18:58:36 +0900260 if (wait_for_property(daemon_prop_name, desired_status, 10) < 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800261 snprintf(errmsg, sizeof(errmsg), "%s", "Timed out waiting for dhcpcd to start");
262 return -1;
263 }
264
265 /* Wait for the daemon to return a result */
266 if (wait_for_property(result_prop_name, NULL, 30) < 0) {
267 snprintf(errmsg, sizeof(errmsg), "%s", "Timed out waiting for DHCP to finish");
268 return -1;
269 }
270
Erik Klinec2291a72015-02-19 19:15:34 +0900271 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800272}
273
274/**
275 * Stop the DHCP client daemon.
276 */
277int dhcp_stop(const char *interface)
278{
279 char result_prop_name[PROPERTY_KEY_MAX];
TK MUN9d157872011-02-23 18:58:36 +0900280 char daemon_prop_name[PROPERTY_KEY_MAX];
281 char daemon_cmd[PROPERTY_VALUE_MAX * 2];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800282 const char *ctrl_prop = "ctl.stop";
283 const char *desired_status = "stopped";
284
Irfan Sheriff89f58cf2012-05-23 10:30:05 -0700285 char p2p_interface[MAX_INTERFACE_LENGTH];
repo synca329b422011-07-29 12:07:34 -0700286
Irfan Sheriff89f58cf2012-05-23 10:30:05 -0700287 get_p2p_interface_replacement(interface, p2p_interface);
repo synca329b422011-07-29 12:07:34 -0700288
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800289 snprintf(result_prop_name, sizeof(result_prop_name), "%s.%s.result",
290 DHCP_PROP_NAME_PREFIX,
Irfan Sheriff89f58cf2012-05-23 10:30:05 -0700291 p2p_interface);
TK MUN9d157872011-02-23 18:58:36 +0900292
293 snprintf(daemon_prop_name, sizeof(daemon_prop_name), "%s_%s",
294 DAEMON_PROP_NAME,
Irfan Sheriff89f58cf2012-05-23 10:30:05 -0700295 p2p_interface);
TK MUN9d157872011-02-23 18:58:36 +0900296
Irfan Sheriff89f58cf2012-05-23 10:30:05 -0700297 snprintf(daemon_cmd, sizeof(daemon_cmd), "%s_%s", DAEMON_NAME, p2p_interface);
TK MUN9d157872011-02-23 18:58:36 +0900298
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800299 /* Stop the daemon and wait until it's reported to be stopped */
TK MUN9d157872011-02-23 18:58:36 +0900300 property_set(ctrl_prop, daemon_cmd);
301 if (wait_for_property(daemon_prop_name, desired_status, 5) < 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800302 return -1;
303 }
304 property_set(result_prop_name, "failed");
305 return 0;
306}
307
308/**
309 * Release the current DHCP client lease.
310 */
311int dhcp_release_lease(const char *interface)
312{
TK MUN9d157872011-02-23 18:58:36 +0900313 char daemon_prop_name[PROPERTY_KEY_MAX];
314 char daemon_cmd[PROPERTY_VALUE_MAX * 2];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800315 const char *ctrl_prop = "ctl.stop";
316 const char *desired_status = "stopped";
317
Irfan Sheriff89f58cf2012-05-23 10:30:05 -0700318 char p2p_interface[MAX_INTERFACE_LENGTH];
repo synca329b422011-07-29 12:07:34 -0700319
Irfan Sheriff89f58cf2012-05-23 10:30:05 -0700320 get_p2p_interface_replacement(interface, p2p_interface);
repo synca329b422011-07-29 12:07:34 -0700321
TK MUN9d157872011-02-23 18:58:36 +0900322 snprintf(daemon_prop_name, sizeof(daemon_prop_name), "%s_%s",
323 DAEMON_PROP_NAME,
Irfan Sheriff89f58cf2012-05-23 10:30:05 -0700324 p2p_interface);
TK MUN9d157872011-02-23 18:58:36 +0900325
Irfan Sheriff89f58cf2012-05-23 10:30:05 -0700326 snprintf(daemon_cmd, sizeof(daemon_cmd), "%s_%s", DAEMON_NAME, p2p_interface);
TK MUN9d157872011-02-23 18:58:36 +0900327
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800328 /* Stop the daemon and wait until it's reported to be stopped */
TK MUN9d157872011-02-23 18:58:36 +0900329 property_set(ctrl_prop, daemon_cmd);
330 if (wait_for_property(daemon_prop_name, desired_status, 5) < 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800331 return -1;
332 }
333 return 0;
334}
335
336char *dhcp_get_errmsg() {
337 return errmsg;
338}
TK MUN9d157872011-02-23 18:58:36 +0900339
340/**
Irfan Sheriff35c28602011-11-09 11:10:50 -0800341 * The device init.rc file needs a corresponding entry.
342 *
343 * Example:
344 * service iprenew_<interface> /system/bin/dhcpcd -n
345 *
TK MUN9d157872011-02-23 18:58:36 +0900346 */
Erik Klinec2291a72015-02-19 19:15:34 +0900347int dhcp_start_renew(const char *interface)
TK MUN9d157872011-02-23 18:58:36 +0900348{
349 char result_prop_name[PROPERTY_KEY_MAX];
350 char prop_value[PROPERTY_VALUE_MAX] = {'\0'};
351 char daemon_cmd[PROPERTY_VALUE_MAX * 2];
352 const char *ctrl_prop = "ctl.start";
353
Irfan Sheriff89f58cf2012-05-23 10:30:05 -0700354 char p2p_interface[MAX_INTERFACE_LENGTH];
repo synca329b422011-07-29 12:07:34 -0700355
Irfan Sheriff89f58cf2012-05-23 10:30:05 -0700356 get_p2p_interface_replacement(interface, p2p_interface);
repo synca329b422011-07-29 12:07:34 -0700357
TK MUN9d157872011-02-23 18:58:36 +0900358 snprintf(result_prop_name, sizeof(result_prop_name), "%s.%s.result",
359 DHCP_PROP_NAME_PREFIX,
Irfan Sheriff89f58cf2012-05-23 10:30:05 -0700360 p2p_interface);
TK MUN9d157872011-02-23 18:58:36 +0900361
362 /* Erase any previous setting of the dhcp result property */
363 property_set(result_prop_name, "");
364
365 /* Start the renew daemon and wait until it's ready */
repo synca329b422011-07-29 12:07:34 -0700366 snprintf(daemon_cmd, sizeof(daemon_cmd), "%s_%s:%s", DAEMON_NAME_RENEW,
Irfan Sheriff89f58cf2012-05-23 10:30:05 -0700367 p2p_interface, interface);
TK MUN9d157872011-02-23 18:58:36 +0900368 memset(prop_value, '\0', PROPERTY_VALUE_MAX);
369 property_set(ctrl_prop, daemon_cmd);
370
371 /* Wait for the daemon to return a result */
372 if (wait_for_property(result_prop_name, NULL, 30) < 0) {
373 snprintf(errmsg, sizeof(errmsg), "%s", "Timed out waiting for DHCP Renew to finish");
374 return -1;
375 }
376
Erik Klinec2291a72015-02-19 19:15:34 +0900377 return 0;
TK MUN9d157872011-02-23 18:58:36 +0900378}