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 length for dhcpcd daemon start (dhcpcd_<interface> as defined in init.rc file) 37 * or for filling up system properties dhcpcd.<interface>.ipaddress, dhcpcd.<interface>.dns1 38 * and other properties on a successful bind 39 */ 40 #define MAX_INTERFACE_LENGTH 25 41 42 /* 43 * P2p interface names increase sequentially p2p-p2p0-1, p2p-p2p0-2.. after 44 * group formation. This does not work well with system properties which can quickly 45 * exhaust or for specifiying a dhcp start target in init which requires 46 * interface to be pre-defined in init.rc file. 47 * 48 * This function returns a common string p2p for all p2p interfaces. 49 */ 50 void get_p2p_interface_replacement(const char *interface, char *p2p_interface) { 51 /* Use p2p for any interface starting with p2p. */ 52 if (strncmp(interface, "p2p",3) == 0) { 53 strncpy(p2p_interface, "p2p", MAX_INTERFACE_LENGTH); 54 } else { 55 strncpy(p2p_interface, interface, MAX_INTERFACE_LENGTH); 56 } 57 } 58 59 /* 60 * Wait for a system property to be assigned a specified value. 61 * If desired_value is NULL, then just wait for the property to 62 * be created with any value. maxwait is the maximum amount of 63 * time in seconds to wait before giving up. 64 */ 65 static int wait_for_property(const char *name, const char *desired_value, int maxwait) 66 { 67 char value[PROPERTY_VALUE_MAX] = {'\0'}; 68 int maxnaps = (maxwait * 1000) / NAP_TIME; 69 70 if (maxnaps < 1) { 71 maxnaps = 1; 72 } 73 74 while (maxnaps-- > 0) { 75 usleep(NAP_TIME * 1000); 76 if (property_get(name, value, NULL)) { 77 if (desired_value == NULL || 78 strcmp(value, desired_value) == 0) { 79 return 0; 80 } 81 } 82 } 83 return -1; /* failure */ 84 } 85 86 static int fill_ip_info(const char *interface, 87 char *ipaddr, 88 char *gateway, 89 uint32_t *prefixLength, 90 char *dns1, 91 char *dns2, 92 char *server, 93 uint32_t *lease, 94 char *vendorInfo) 95 { 96 char prop_name[PROPERTY_KEY_MAX]; 97 char prop_value[PROPERTY_VALUE_MAX]; 98 /* Interface name after converting p2p0-p2p0-X to p2p to reuse system properties */ 99 char p2p_interface[MAX_INTERFACE_LENGTH]; 100 101 get_p2p_interface_replacement(interface, p2p_interface); 102 103 snprintf(prop_name, sizeof(prop_name), "%s.%s.ipaddress", DHCP_PROP_NAME_PREFIX, p2p_interface); 104 property_get(prop_name, ipaddr, NULL); 105 106 snprintf(prop_name, sizeof(prop_name), "%s.%s.gateway", DHCP_PROP_NAME_PREFIX, p2p_interface); 107 property_get(prop_name, gateway, NULL); 108 109 snprintf(prop_name, sizeof(prop_name), "%s.%s.server", DHCP_PROP_NAME_PREFIX, p2p_interface); 110 property_get(prop_name, server, NULL); 111 112 //TODO: Handle IPv6 when we change system property usage 113 if (strcmp(gateway, "0.0.0.0") == 0) { 114 //DHCP server is our best bet as gateway 115 strncpy(gateway, server, PROPERTY_VALUE_MAX); 116 } 117 118 snprintf(prop_name, sizeof(prop_name), "%s.%s.mask", DHCP_PROP_NAME_PREFIX, p2p_interface); 119 if (property_get(prop_name, prop_value, NULL)) { 120 int p; 121 // this conversion is v4 only, but this dhcp client is v4 only anyway 122 in_addr_t mask = ntohl(inet_addr(prop_value)); 123 // Check netmask is a valid IP address. ntohl gives NONE response (all 1's) for 124 // non 255.255.255.255 inputs. if we get that value check if it is legit.. 125 if (mask == INADDR_NONE && strcmp(prop_value, "255.255.255.255") != 0) { 126 snprintf(errmsg, sizeof(errmsg), "DHCP gave invalid net mask %s", prop_value); 127 return -1; 128 } 129 for (p = 0; p < 32; p++) { 130 if (mask == 0) break; 131 // check for non-contiguous netmask, e.g., 255.254.255.0 132 if ((mask & 0x80000000) == 0) { 133 snprintf(errmsg, sizeof(errmsg), "DHCP gave invalid net mask %s", prop_value); 134 return -1; 135 } 136 mask = mask << 1; 137 } 138 *prefixLength = p; 139 } 140 snprintf(prop_name, sizeof(prop_name), "%s.%s.dns1", DHCP_PROP_NAME_PREFIX, p2p_interface); 141 property_get(prop_name, dns1, NULL); 142 143 snprintf(prop_name, sizeof(prop_name), "%s.%s.dns2", DHCP_PROP_NAME_PREFIX, p2p_interface); 144 property_get(prop_name, dns2, NULL); 145 146 snprintf(prop_name, sizeof(prop_name), "%s.%s.leasetime", DHCP_PROP_NAME_PREFIX, p2p_interface); 147 if (property_get(prop_name, prop_value, NULL)) { 148 *lease = atol(prop_value); 149 } 150 151 snprintf(prop_name, sizeof(prop_name), "%s.%s.vendorInfo", DHCP_PROP_NAME_PREFIX, 152 p2p_interface); 153 property_get(prop_name, vendorInfo, NULL); 154 155 return 0; 156 } 157 158 static const char *ipaddr_to_string(in_addr_t addr) 159 { 160 struct in_addr in_addr; 161 162 in_addr.s_addr = addr; 163 return inet_ntoa(in_addr); 164 } 165 166 /* 167 * Start the dhcp client daemon, and wait for it to finish 168 * configuring the interface. 169 * 170 * The device init.rc file needs a corresponding entry for this work. 171 * 172 * Example: 173 * service dhcpcd_<interface> /system/bin/dhcpcd -ABKL 174 */ 175 int dhcp_do_request(const char *interface, 176 char *ipaddr, 177 char *gateway, 178 uint32_t *prefixLength, 179 char *dns1, 180 char *dns2, 181 char *server, 182 uint32_t *lease, 183 char *vendorInfo) 184 { 185 char result_prop_name[PROPERTY_KEY_MAX]; 186 char daemon_prop_name[PROPERTY_KEY_MAX]; 187 char prop_value[PROPERTY_VALUE_MAX] = {'\0'}; 188 char daemon_cmd[PROPERTY_VALUE_MAX * 2]; 189 const char *ctrl_prop = "ctl.start"; 190 const char *desired_status = "running"; 191 /* Interface name after converting p2p0-p2p0-X to p2p to reuse system properties */ 192 char p2p_interface[MAX_INTERFACE_LENGTH]; 193 194 get_p2p_interface_replacement(interface, p2p_interface); 195 196 snprintf(result_prop_name, sizeof(result_prop_name), "%s.%s.result", 197 DHCP_PROP_NAME_PREFIX, 198 p2p_interface); 199 200 snprintf(daemon_prop_name, sizeof(daemon_prop_name), "%s_%s", 201 DAEMON_PROP_NAME, 202 p2p_interface); 203 204 /* Erase any previous setting of the dhcp result property */ 205 property_set(result_prop_name, ""); 206 207 /* Start the daemon and wait until it's ready */ 208 if (property_get(HOSTNAME_PROP_NAME, prop_value, NULL) && (prop_value[0] != '\0')) 209 snprintf(daemon_cmd, sizeof(daemon_cmd), "%s_%s:-h %s %s", DAEMON_NAME, p2p_interface, 210 prop_value, interface); 211 else 212 snprintf(daemon_cmd, sizeof(daemon_cmd), "%s_%s:%s", DAEMON_NAME, p2p_interface, interface); 213 memset(prop_value, '\0', PROPERTY_VALUE_MAX); 214 property_set(ctrl_prop, daemon_cmd); 215 if (wait_for_property(daemon_prop_name, desired_status, 10) < 0) { 216 snprintf(errmsg, sizeof(errmsg), "%s", "Timed out waiting for dhcpcd to start"); 217 return -1; 218 } 219 220 /* Wait for the daemon to return a result */ 221 if (wait_for_property(result_prop_name, NULL, 30) < 0) { 222 snprintf(errmsg, sizeof(errmsg), "%s", "Timed out waiting for DHCP to finish"); 223 return -1; 224 } 225 226 if (!property_get(result_prop_name, prop_value, NULL)) { 227 /* shouldn't ever happen, given the success of wait_for_property() */ 228 snprintf(errmsg, sizeof(errmsg), "%s", "DHCP result property was not set"); 229 return -1; 230 } 231 if (strcmp(prop_value, "ok") == 0) { 232 char dns_prop_name[PROPERTY_KEY_MAX]; 233 if (fill_ip_info(interface, ipaddr, gateway, prefixLength, 234 dns1, dns2, server, lease, vendorInfo) == -1) { 235 return -1; 236 } 237 238 /* copy dns data to system properties - TODO - remove this after we have async 239 * notification of renewal's */ 240 snprintf(dns_prop_name, sizeof(dns_prop_name), "net.%s.dns1", interface); 241 property_set(dns_prop_name, *dns1 ? ipaddr_to_string(*dns1) : ""); 242 snprintf(dns_prop_name, sizeof(dns_prop_name), "net.%s.dns2", interface); 243 property_set(dns_prop_name, *dns2 ? ipaddr_to_string(*dns2) : ""); 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 */ 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 */ 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 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 */ 324 int dhcp_do_request_renew(const char *interface, 325 char *ipaddr, 326 char *gateway, 327 uint32_t *prefixLength, 328 char *dns1, 329 char *dns2, 330 char *server, 331 uint32_t *lease, 332 char *vendorInfo) 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, 369 dns1, dns2, server, lease, vendorInfo); 370 return 0; 371 } else { 372 snprintf(errmsg, sizeof(errmsg), "DHCP Renew result was %s", prop_value); 373 return -1; 374 } 375 } 376