dhcp_utils.c revision 9d157870345fa18ee7e639a85f6bcc4afb50c648
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 28static const char DAEMON_NAME[] = "dhcpcd"; 29static const char DAEMON_PROP_NAME[] = "init.svc.dhcpcd"; 30static const char HOSTNAME_PROP_NAME[] = "net.hostname"; 31static const char DHCP_PROP_NAME_PREFIX[] = "dhcp"; 32static const int NAP_TIME = 200; /* wait for 200ms at a time */ 33 /* when polling for property values */ 34static const char DAEMON_NAME_RENEW[] = "iprenew"; 35static 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'}; 46 int maxnaps = (maxwait * 1000) / NAP_TIME; 47 48 if (maxnaps < 1) { 49 maxnaps = 1; 50 } 51 52 while (maxnaps-- > 0) { 53 usleep(NAP_TIME * 1000); 54 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 64static 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, 71 uint32_t *lease) 72{ 73 char prop_name[PROPERTY_KEY_MAX]; 74 char prop_value[PROPERTY_VALUE_MAX]; 75 76 snprintf(prop_name, sizeof(prop_name), "%s.%s.ipaddress", DHCP_PROP_NAME_PREFIX, interface); 77 property_get(prop_name, ipaddr, NULL); 78 79 snprintf(prop_name, sizeof(prop_name), "%s.%s.gateway", DHCP_PROP_NAME_PREFIX, interface); 80 property_get(prop_name, gateway, NULL); 81 82 snprintf(prop_name, sizeof(prop_name), "%s.%s.mask", DHCP_PROP_NAME_PREFIX, interface); 83 if (property_get(prop_name, prop_value, NULL)) { 84 int p; 85 // this conversion is v4 only, but this dhcp client is v4 only anyway 86 in_addr_t mask = ntohl(inet_addr(prop_value)); 87 // Check netmask is a valid IP address. ntohl gives NONE response (all 1's) for 88 // non 255.255.255.255 inputs. if we get that value check if it is legit.. 89 if (mask == INADDR_NONE && strcmp(prop_value, "255.255.255.255") != 0) { 90 snprintf(errmsg, sizeof(errmsg), "DHCP gave invalid net mask %s", prop_value); 91 return -1; 92 } 93 for (p = 0; p < 32; p++) { 94 if (mask == 0) break; 95 // check for non-contiguous netmask, e.g., 255.254.255.0 96 if ((mask & 0x80000000) == 0) { 97 snprintf(errmsg, sizeof(errmsg), "DHCP gave invalid net mask %s", prop_value); 98 return -1; 99 } 100 mask = mask << 1; 101 } 102 *prefixLength = p; 103 } 104 snprintf(prop_name, sizeof(prop_name), "%s.%s.dns1", DHCP_PROP_NAME_PREFIX, interface); 105 property_get(prop_name, dns1, NULL); 106 107 snprintf(prop_name, sizeof(prop_name), "%s.%s.dns2", DHCP_PROP_NAME_PREFIX, interface); 108 property_get(prop_name, dns2, NULL); 109 110 snprintf(prop_name, sizeof(prop_name), "%s.%s.server", DHCP_PROP_NAME_PREFIX, interface); 111 property_get(prop_name, server, NULL); 112 113 snprintf(prop_name, sizeof(prop_name), "%s.%s.leasetime", DHCP_PROP_NAME_PREFIX, interface); 114 if (property_get(prop_name, prop_value, NULL)) { 115 *lease = atol(prop_value); 116 } 117 return 0; 118} 119 120static const char *ipaddr_to_string(in_addr_t addr) 121{ 122 struct in_addr in_addr; 123 124 in_addr.s_addr = addr; 125 return inet_ntoa(in_addr); 126} 127 128/* 129 * Start the dhcp client daemon, and wait for it to finish 130 * configuring the interface. 131 */ 132int dhcp_do_request(const char *interface, 133 char *ipaddr, 134 char *gateway, 135 uint32_t *prefixLength, 136 char *dns1, 137 char *dns2, 138 char *server, 139 uint32_t *lease) 140{ 141 char result_prop_name[PROPERTY_KEY_MAX]; 142 char daemon_prop_name[PROPERTY_KEY_MAX]; 143 char prop_value[PROPERTY_VALUE_MAX] = {'\0'}; 144 char daemon_cmd[PROPERTY_VALUE_MAX * 2]; 145 const char *ctrl_prop = "ctl.start"; 146 const char *desired_status = "running"; 147 148 snprintf(result_prop_name, sizeof(result_prop_name), "%s.%s.result", 149 DHCP_PROP_NAME_PREFIX, 150 interface); 151 152 snprintf(daemon_prop_name, sizeof(daemon_prop_name), "%s_%s", 153 DAEMON_PROP_NAME, 154 interface); 155 156 /* Erase any previous setting of the dhcp result property */ 157 property_set(result_prop_name, ""); 158 159 /* Start the daemon and wait until it's ready */ 160 if (property_get(HOSTNAME_PROP_NAME, prop_value, NULL) && (prop_value[0] != '\0')) 161 snprintf(daemon_cmd, sizeof(daemon_cmd), "%s_%s:-h %s %s", DAEMON_NAME, interface, 162 prop_value, interface); 163 else 164 snprintf(daemon_cmd, sizeof(daemon_cmd), "%s_%s:%s", DAEMON_NAME, interface, interface); 165 memset(prop_value, '\0', PROPERTY_VALUE_MAX); 166 property_set(ctrl_prop, daemon_cmd); 167 if (wait_for_property(daemon_prop_name, desired_status, 10) < 0) { 168 snprintf(errmsg, sizeof(errmsg), "%s", "Timed out waiting for dhcpcd to start"); 169 return -1; 170 } 171 172 /* Wait for the daemon to return a result */ 173 if (wait_for_property(result_prop_name, NULL, 30) < 0) { 174 snprintf(errmsg, sizeof(errmsg), "%s", "Timed out waiting for DHCP to finish"); 175 return -1; 176 } 177 178 if (!property_get(result_prop_name, prop_value, NULL)) { 179 /* shouldn't ever happen, given the success of wait_for_property() */ 180 snprintf(errmsg, sizeof(errmsg), "%s", "DHCP result property was not set"); 181 return -1; 182 } 183 if (strcmp(prop_value, "ok") == 0) { 184 char dns_prop_name[PROPERTY_KEY_MAX]; 185 if (fill_ip_info(interface, ipaddr, gateway, prefixLength, dns1, dns2, server, lease) 186 == -1) { 187 return -1; 188 } 189 190 /* copy dns data to system properties - TODO - remove this after we have async 191 * notification of renewal's */ 192 snprintf(dns_prop_name, sizeof(dns_prop_name), "net.%s.dns1", interface); 193 property_set(dns_prop_name, *dns1 ? ipaddr_to_string(*dns1) : ""); 194 snprintf(dns_prop_name, sizeof(dns_prop_name), "net.%s.dns2", interface); 195 property_set(dns_prop_name, *dns2 ? ipaddr_to_string(*dns2) : ""); 196 return 0; 197 } else { 198 snprintf(errmsg, sizeof(errmsg), "DHCP result was %s", prop_value); 199 return -1; 200 } 201} 202 203/** 204 * Stop the DHCP client daemon. 205 */ 206int dhcp_stop(const char *interface) 207{ 208 char result_prop_name[PROPERTY_KEY_MAX]; 209 char daemon_prop_name[PROPERTY_KEY_MAX]; 210 char daemon_cmd[PROPERTY_VALUE_MAX * 2]; 211 const char *ctrl_prop = "ctl.stop"; 212 const char *desired_status = "stopped"; 213 214 snprintf(result_prop_name, sizeof(result_prop_name), "%s.%s.result", 215 DHCP_PROP_NAME_PREFIX, 216 interface); 217 218 snprintf(daemon_prop_name, sizeof(daemon_prop_name), "%s_%s", 219 DAEMON_PROP_NAME, 220 interface); 221 222 snprintf(daemon_cmd, sizeof(daemon_cmd), "%s_%s", DAEMON_NAME, interface); 223 224 /* Stop the daemon and wait until it's reported to be stopped */ 225 property_set(ctrl_prop, daemon_cmd); 226 if (wait_for_property(daemon_prop_name, desired_status, 5) < 0) { 227 return -1; 228 } 229 property_set(result_prop_name, "failed"); 230 return 0; 231} 232 233/** 234 * Release the current DHCP client lease. 235 */ 236int dhcp_release_lease(const char *interface) 237{ 238 char daemon_prop_name[PROPERTY_KEY_MAX]; 239 char daemon_cmd[PROPERTY_VALUE_MAX * 2]; 240 const char *ctrl_prop = "ctl.stop"; 241 const char *desired_status = "stopped"; 242 243 snprintf(daemon_prop_name, sizeof(daemon_prop_name), "%s_%s", 244 DAEMON_PROP_NAME, 245 interface); 246 247 snprintf(daemon_cmd, sizeof(daemon_cmd), "%s_%s", DAEMON_NAME, interface); 248 249 /* Stop the daemon and wait until it's reported to be stopped */ 250 property_set(ctrl_prop, daemon_cmd); 251 if (wait_for_property(daemon_prop_name, desired_status, 5) < 0) { 252 return -1; 253 } 254 return 0; 255} 256 257char *dhcp_get_errmsg() { 258 return errmsg; 259} 260 261/** 262 * Run WiMAX dhcp renew service. 263 * "wimax_renew" service shoud be included in init.rc. 264 */ 265int dhcp_do_request_renew(const char *interface, 266 in_addr_t *ipaddr, 267 in_addr_t *gateway, 268 in_addr_t *mask, 269 in_addr_t *dns1, 270 in_addr_t *dns2, 271 in_addr_t *server, 272 uint32_t *lease) 273{ 274 char result_prop_name[PROPERTY_KEY_MAX]; 275 char prop_value[PROPERTY_VALUE_MAX] = {'\0'}; 276 char daemon_cmd[PROPERTY_VALUE_MAX * 2]; 277 const char *ctrl_prop = "ctl.start"; 278 279 snprintf(result_prop_name, sizeof(result_prop_name), "%s.%s.result", 280 DHCP_PROP_NAME_PREFIX, 281 interface); 282 283 /* Erase any previous setting of the dhcp result property */ 284 property_set(result_prop_name, ""); 285 286 /* Start the renew daemon and wait until it's ready */ 287 snprintf(daemon_cmd, sizeof(daemon_cmd), "%s_%s:%s", DAEMON_NAME_RENEW, interface, interface); 288 memset(prop_value, '\0', PROPERTY_VALUE_MAX); 289 property_set(ctrl_prop, daemon_cmd); 290 291 /* Wait for the daemon to return a result */ 292 if (wait_for_property(result_prop_name, NULL, 30) < 0) { 293 snprintf(errmsg, sizeof(errmsg), "%s", "Timed out waiting for DHCP Renew to finish"); 294 return -1; 295 } 296 297 if (!property_get(result_prop_name, prop_value, NULL)) { 298 /* shouldn't ever happen, given the success of wait_for_property() */ 299 snprintf(errmsg, sizeof(errmsg), "%s", "DHCP Renew result property was not set"); 300 return -1; 301 } 302 if (strcmp(prop_value, "ok") == 0) { 303 fill_ip_info(interface, ipaddr, gateway, mask, dns1, dns2, server, lease); 304 return 0; 305 } else { 306 snprintf(errmsg, sizeof(errmsg), "DHCP Renew result was %s", prop_value); 307 return -1; 308 } 309} 310