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