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 | #include <stdio.h> |
| 18 | #include <stdlib.h> |
| 19 | #include <unistd.h> |
| 20 | #include <string.h> |
| 21 | #include <errno.h> |
| 22 | |
| 23 | #include <sys/socket.h> |
| 24 | #include <sys/select.h> |
| 25 | #include <sys/types.h> |
| 26 | #include <netinet/in.h> |
| 27 | #include <arpa/inet.h> |
| 28 | |
| 29 | #include <linux/if.h> |
Szymon Jakubczak | c88e09c | 2010-06-09 16:11:09 -0400 | [diff] [blame^] | 30 | #include <linux/if_ether.h> |
| 31 | #include <linux/if_arp.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 32 | #include <linux/sockios.h> |
| 33 | #include <linux/route.h> |
| 34 | #include <linux/wireless.h> |
| 35 | |
| 36 | #ifdef ANDROID |
| 37 | #define LOG_TAG "NetUtils" |
| 38 | #include <cutils/log.h> |
| 39 | #include <cutils/properties.h> |
| 40 | #else |
| 41 | #include <stdio.h> |
| 42 | #include <string.h> |
| 43 | #define LOGD printf |
| 44 | #define LOGW printf |
| 45 | #endif |
| 46 | |
| 47 | static int ifc_ctl_sock = -1; |
| 48 | void printerr(char *fmt, ...); |
| 49 | |
Szymon Jakubczak | c88e09c | 2010-06-09 16:11:09 -0400 | [diff] [blame^] | 50 | static const char *ipaddr_to_string(in_addr_t addr) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 51 | { |
| 52 | struct in_addr in_addr; |
| 53 | |
| 54 | in_addr.s_addr = addr; |
| 55 | return inet_ntoa(in_addr); |
| 56 | } |
| 57 | |
| 58 | int ifc_init(void) |
| 59 | { |
| 60 | if (ifc_ctl_sock == -1) { |
| 61 | ifc_ctl_sock = socket(AF_INET, SOCK_DGRAM, 0); |
| 62 | if (ifc_ctl_sock < 0) { |
| 63 | printerr("socket() failed: %s\n", strerror(errno)); |
| 64 | } |
| 65 | } |
| 66 | return ifc_ctl_sock < 0 ? -1 : 0; |
| 67 | } |
| 68 | |
| 69 | void ifc_close(void) |
| 70 | { |
| 71 | if (ifc_ctl_sock != -1) { |
| 72 | (void)close(ifc_ctl_sock); |
| 73 | ifc_ctl_sock = -1; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | static void ifc_init_ifr(const char *name, struct ifreq *ifr) |
| 78 | { |
| 79 | memset(ifr, 0, sizeof(struct ifreq)); |
| 80 | strncpy(ifr->ifr_name, name, IFNAMSIZ); |
| 81 | ifr->ifr_name[IFNAMSIZ - 1] = 0; |
| 82 | } |
| 83 | |
| 84 | int ifc_get_hwaddr(const char *name, void *ptr) |
| 85 | { |
| 86 | int r; |
| 87 | struct ifreq ifr; |
| 88 | ifc_init_ifr(name, &ifr); |
| 89 | |
| 90 | r = ioctl(ifc_ctl_sock, SIOCGIFHWADDR, &ifr); |
| 91 | if(r < 0) return -1; |
| 92 | |
Szymon Jakubczak | c88e09c | 2010-06-09 16:11:09 -0400 | [diff] [blame^] | 93 | memcpy(ptr, &ifr.ifr_hwaddr.sa_data, ETH_ALEN); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 94 | return 0; |
| 95 | } |
| 96 | |
| 97 | int ifc_get_ifindex(const char *name, int *if_indexp) |
| 98 | { |
| 99 | int r; |
| 100 | struct ifreq ifr; |
| 101 | ifc_init_ifr(name, &ifr); |
| 102 | |
| 103 | r = ioctl(ifc_ctl_sock, SIOCGIFINDEX, &ifr); |
| 104 | if(r < 0) return -1; |
| 105 | |
| 106 | *if_indexp = ifr.ifr_ifindex; |
| 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | static int ifc_set_flags(const char *name, unsigned set, unsigned clr) |
| 111 | { |
| 112 | struct ifreq ifr; |
| 113 | ifc_init_ifr(name, &ifr); |
| 114 | |
| 115 | if(ioctl(ifc_ctl_sock, SIOCGIFFLAGS, &ifr) < 0) return -1; |
| 116 | ifr.ifr_flags = (ifr.ifr_flags & (~clr)) | set; |
| 117 | return ioctl(ifc_ctl_sock, SIOCSIFFLAGS, &ifr); |
| 118 | } |
| 119 | |
| 120 | int ifc_up(const char *name) |
| 121 | { |
| 122 | return ifc_set_flags(name, IFF_UP, 0); |
| 123 | } |
| 124 | |
| 125 | int ifc_down(const char *name) |
| 126 | { |
| 127 | return ifc_set_flags(name, 0, IFF_UP); |
| 128 | } |
| 129 | |
| 130 | static void init_sockaddr_in(struct sockaddr *sa, in_addr_t addr) |
| 131 | { |
| 132 | struct sockaddr_in *sin = (struct sockaddr_in *) sa; |
| 133 | sin->sin_family = AF_INET; |
| 134 | sin->sin_port = 0; |
| 135 | sin->sin_addr.s_addr = addr; |
| 136 | } |
| 137 | |
| 138 | int ifc_set_addr(const char *name, in_addr_t addr) |
| 139 | { |
| 140 | struct ifreq ifr; |
| 141 | |
| 142 | ifc_init_ifr(name, &ifr); |
| 143 | init_sockaddr_in(&ifr.ifr_addr, addr); |
| 144 | |
| 145 | return ioctl(ifc_ctl_sock, SIOCSIFADDR, &ifr); |
| 146 | } |
| 147 | |
Szymon Jakubczak | c88e09c | 2010-06-09 16:11:09 -0400 | [diff] [blame^] | 148 | int ifc_set_hwaddr(const char *name, const void *ptr) |
| 149 | { |
| 150 | int r; |
| 151 | struct ifreq ifr; |
| 152 | ifc_init_ifr(name, &ifr); |
| 153 | |
| 154 | ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER; |
| 155 | memcpy(&ifr.ifr_hwaddr.sa_data, ptr, ETH_ALEN); |
| 156 | return ioctl(ifc_ctl_sock, SIOCSIFHWADDR, &ifr); |
| 157 | } |
| 158 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 159 | int ifc_set_mask(const char *name, in_addr_t mask) |
| 160 | { |
| 161 | struct ifreq ifr; |
| 162 | |
| 163 | ifc_init_ifr(name, &ifr); |
| 164 | init_sockaddr_in(&ifr.ifr_addr, mask); |
| 165 | |
| 166 | return ioctl(ifc_ctl_sock, SIOCSIFNETMASK, &ifr); |
| 167 | } |
| 168 | |
| 169 | int ifc_get_info(const char *name, in_addr_t *addr, in_addr_t *mask, unsigned *flags) |
| 170 | { |
| 171 | struct ifreq ifr; |
| 172 | ifc_init_ifr(name, &ifr); |
| 173 | |
| 174 | if (addr != NULL) { |
| 175 | if(ioctl(ifc_ctl_sock, SIOCGIFADDR, &ifr) < 0) { |
| 176 | *addr = 0; |
| 177 | } else { |
| 178 | *addr = ((struct sockaddr_in*) &ifr.ifr_addr)->sin_addr.s_addr; |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | if (mask != NULL) { |
| 183 | if(ioctl(ifc_ctl_sock, SIOCGIFNETMASK, &ifr) < 0) { |
| 184 | *mask = 0; |
| 185 | } else { |
| 186 | *mask = ((struct sockaddr_in*) &ifr.ifr_addr)->sin_addr.s_addr; |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | if (flags != NULL) { |
| 191 | if(ioctl(ifc_ctl_sock, SIOCGIFFLAGS, &ifr) < 0) { |
| 192 | *flags = 0; |
| 193 | } else { |
| 194 | *flags = ifr.ifr_flags; |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | return 0; |
| 199 | } |
| 200 | |
| 201 | |
| 202 | int ifc_create_default_route(const char *name, in_addr_t addr) |
| 203 | { |
| 204 | struct rtentry rt; |
| 205 | |
| 206 | memset(&rt, 0, sizeof(rt)); |
| 207 | |
| 208 | rt.rt_dst.sa_family = AF_INET; |
| 209 | rt.rt_flags = RTF_UP | RTF_GATEWAY; |
| 210 | rt.rt_dev = (void*) name; |
| 211 | init_sockaddr_in(&rt.rt_genmask, 0); |
| 212 | init_sockaddr_in(&rt.rt_gateway, addr); |
| 213 | |
| 214 | return ioctl(ifc_ctl_sock, SIOCADDRT, &rt); |
| 215 | } |
| 216 | |
| 217 | int ifc_add_host_route(const char *name, in_addr_t addr) |
| 218 | { |
| 219 | struct rtentry rt; |
| 220 | int result; |
| 221 | |
| 222 | memset(&rt, 0, sizeof(rt)); |
| 223 | |
| 224 | rt.rt_dst.sa_family = AF_INET; |
| 225 | rt.rt_flags = RTF_UP | RTF_HOST; |
| 226 | rt.rt_dev = (void*) name; |
| 227 | init_sockaddr_in(&rt.rt_dst, addr); |
| 228 | init_sockaddr_in(&rt.rt_genmask, 0); |
| 229 | init_sockaddr_in(&rt.rt_gateway, 0); |
| 230 | |
| 231 | ifc_init(); |
| 232 | result = ioctl(ifc_ctl_sock, SIOCADDRT, &rt); |
| 233 | if (result < 0 && errno == EEXIST) { |
| 234 | result = 0; |
| 235 | } |
| 236 | ifc_close(); |
| 237 | return result; |
| 238 | } |
| 239 | |
Mike Lockwood | feb63e9 | 2009-07-10 17:21:17 -0400 | [diff] [blame] | 240 | int ifc_enable(const char *ifname) |
| 241 | { |
| 242 | int result; |
| 243 | |
| 244 | ifc_init(); |
| 245 | result = ifc_up(ifname); |
| 246 | ifc_close(); |
| 247 | return result; |
| 248 | } |
| 249 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 250 | int ifc_disable(const char *ifname) |
| 251 | { |
| 252 | int result; |
| 253 | |
| 254 | ifc_init(); |
| 255 | result = ifc_down(ifname); |
| 256 | ifc_set_addr(ifname, 0); |
| 257 | ifc_close(); |
| 258 | return result; |
| 259 | } |
| 260 | |
| 261 | int ifc_reset_connections(const char *ifname) |
| 262 | { |
| 263 | #ifdef HAVE_ANDROID_OS |
| 264 | int result; |
| 265 | in_addr_t myaddr; |
| 266 | struct ifreq ifr; |
| 267 | |
| 268 | ifc_init(); |
| 269 | ifc_get_info(ifname, &myaddr, NULL, NULL); |
| 270 | ifc_init_ifr(ifname, &ifr); |
| 271 | init_sockaddr_in(&ifr.ifr_addr, myaddr); |
| 272 | result = ioctl(ifc_ctl_sock, SIOCKILLADDR, &ifr); |
| 273 | ifc_close(); |
| 274 | |
| 275 | return result; |
| 276 | #else |
| 277 | return 0; |
| 278 | #endif |
| 279 | } |
| 280 | |
| 281 | /* |
| 282 | * Remove the routes associated with the named interface. |
| 283 | */ |
| 284 | int ifc_remove_host_routes(const char *name) |
| 285 | { |
| 286 | char ifname[64]; |
| 287 | in_addr_t dest, gway, mask; |
| 288 | int flags, refcnt, use, metric, mtu, win, irtt; |
| 289 | struct rtentry rt; |
| 290 | FILE *fp; |
| 291 | struct in_addr addr; |
| 292 | |
| 293 | fp = fopen("/proc/net/route", "r"); |
| 294 | if (fp == NULL) |
| 295 | return -1; |
| 296 | /* Skip the header line */ |
| 297 | if (fscanf(fp, "%*[^\n]\n") < 0) { |
| 298 | fclose(fp); |
| 299 | return -1; |
| 300 | } |
| 301 | ifc_init(); |
| 302 | for (;;) { |
| 303 | int nread = fscanf(fp, "%63s%X%X%X%d%d%d%X%d%d%d\n", |
| 304 | ifname, &dest, &gway, &flags, &refcnt, &use, &metric, &mask, |
| 305 | &mtu, &win, &irtt); |
| 306 | if (nread != 11) { |
| 307 | break; |
| 308 | } |
| 309 | if ((flags & (RTF_UP|RTF_HOST)) != (RTF_UP|RTF_HOST) |
| 310 | || strcmp(ifname, name) != 0) { |
| 311 | continue; |
| 312 | } |
| 313 | memset(&rt, 0, sizeof(rt)); |
| 314 | rt.rt_dev = (void *)name; |
| 315 | init_sockaddr_in(&rt.rt_dst, dest); |
| 316 | init_sockaddr_in(&rt.rt_gateway, gway); |
| 317 | init_sockaddr_in(&rt.rt_genmask, mask); |
| 318 | addr.s_addr = dest; |
| 319 | if (ioctl(ifc_ctl_sock, SIOCDELRT, &rt) < 0) { |
| 320 | LOGD("failed to remove route for %s to %s: %s", |
| 321 | ifname, inet_ntoa(addr), strerror(errno)); |
| 322 | } |
| 323 | } |
| 324 | fclose(fp); |
| 325 | ifc_close(); |
| 326 | return 0; |
| 327 | } |
| 328 | |
| 329 | /* |
| 330 | * Return the address of the default gateway |
| 331 | * |
| 332 | * TODO: factor out common code from this and remove_host_routes() |
| 333 | * so that we only scan /proc/net/route in one place. |
| 334 | */ |
| 335 | int ifc_get_default_route(const char *ifname) |
| 336 | { |
| 337 | char name[64]; |
| 338 | in_addr_t dest, gway, mask; |
| 339 | int flags, refcnt, use, metric, mtu, win, irtt; |
| 340 | int result; |
| 341 | FILE *fp; |
| 342 | |
| 343 | fp = fopen("/proc/net/route", "r"); |
| 344 | if (fp == NULL) |
| 345 | return 0; |
| 346 | /* Skip the header line */ |
| 347 | if (fscanf(fp, "%*[^\n]\n") < 0) { |
| 348 | fclose(fp); |
| 349 | return 0; |
| 350 | } |
| 351 | ifc_init(); |
| 352 | result = 0; |
| 353 | for (;;) { |
| 354 | int nread = fscanf(fp, "%63s%X%X%X%d%d%d%X%d%d%d\n", |
| 355 | name, &dest, &gway, &flags, &refcnt, &use, &metric, &mask, |
| 356 | &mtu, &win, &irtt); |
| 357 | if (nread != 11) { |
| 358 | break; |
| 359 | } |
| 360 | if ((flags & (RTF_UP|RTF_GATEWAY)) == (RTF_UP|RTF_GATEWAY) |
| 361 | && dest == 0 |
| 362 | && strcmp(ifname, name) == 0) { |
| 363 | result = gway; |
| 364 | break; |
| 365 | } |
| 366 | } |
| 367 | fclose(fp); |
| 368 | ifc_close(); |
| 369 | return result; |
| 370 | } |
| 371 | |
| 372 | /* |
| 373 | * Sets the specified gateway as the default route for the named interface. |
| 374 | */ |
| 375 | int ifc_set_default_route(const char *ifname, in_addr_t gateway) |
| 376 | { |
| 377 | struct in_addr addr; |
| 378 | int result; |
| 379 | |
| 380 | ifc_init(); |
| 381 | addr.s_addr = gateway; |
| 382 | if ((result = ifc_create_default_route(ifname, gateway)) < 0) { |
| 383 | LOGD("failed to add %s as default route for %s: %s", |
| 384 | inet_ntoa(addr), ifname, strerror(errno)); |
| 385 | } |
| 386 | ifc_close(); |
| 387 | return result; |
| 388 | } |
| 389 | |
| 390 | /* |
| 391 | * Removes the default route for the named interface. |
| 392 | */ |
| 393 | int ifc_remove_default_route(const char *ifname) |
| 394 | { |
| 395 | struct rtentry rt; |
| 396 | int result; |
| 397 | |
| 398 | ifc_init(); |
| 399 | memset(&rt, 0, sizeof(rt)); |
| 400 | rt.rt_dev = (void *)ifname; |
| 401 | rt.rt_flags = RTF_UP|RTF_GATEWAY; |
| 402 | init_sockaddr_in(&rt.rt_dst, 0); |
| 403 | if ((result = ioctl(ifc_ctl_sock, SIOCDELRT, &rt)) < 0) { |
| 404 | LOGD("failed to remove default route for %s: %s", ifname, strerror(errno)); |
| 405 | } |
| 406 | ifc_close(); |
| 407 | return result; |
| 408 | } |
| 409 | |
| 410 | int |
| 411 | ifc_configure(const char *ifname, |
| 412 | in_addr_t address, |
| 413 | in_addr_t netmask, |
| 414 | in_addr_t gateway, |
| 415 | in_addr_t dns1, |
| 416 | in_addr_t dns2) { |
| 417 | |
| 418 | char dns_prop_name[PROPERTY_KEY_MAX]; |
| 419 | |
| 420 | ifc_init(); |
| 421 | |
| 422 | if (ifc_up(ifname)) { |
| 423 | printerr("failed to turn on interface %s: %s\n", ifname, strerror(errno)); |
| 424 | ifc_close(); |
| 425 | return -1; |
| 426 | } |
| 427 | if (ifc_set_addr(ifname, address)) { |
| 428 | printerr("failed to set ipaddr %s: %s\n", ipaddr_to_string(address), strerror(errno)); |
| 429 | ifc_close(); |
| 430 | return -1; |
| 431 | } |
| 432 | if (ifc_set_mask(ifname, netmask)) { |
| 433 | printerr("failed to set netmask %s: %s\n", ipaddr_to_string(netmask), strerror(errno)); |
| 434 | ifc_close(); |
| 435 | return -1; |
| 436 | } |
| 437 | if (ifc_create_default_route(ifname, gateway)) { |
| 438 | printerr("failed to set default route %s: %s\n", ipaddr_to_string(gateway), strerror(errno)); |
| 439 | ifc_close(); |
| 440 | return -1; |
| 441 | } |
| 442 | |
| 443 | ifc_close(); |
| 444 | |
Szymon Jakubczak | c88e09c | 2010-06-09 16:11:09 -0400 | [diff] [blame^] | 445 | snprintf(dns_prop_name, sizeof(dns_prop_name), "net.%s.dns1", ifname); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 446 | property_set(dns_prop_name, dns1 ? ipaddr_to_string(dns1) : ""); |
Szymon Jakubczak | c88e09c | 2010-06-09 16:11:09 -0400 | [diff] [blame^] | 447 | snprintf(dns_prop_name, sizeof(dns_prop_name), "net.%s.dns2", ifname); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 448 | property_set(dns_prop_name, dns2 ? ipaddr_to_string(dns2) : ""); |
| 449 | |
| 450 | return 0; |
| 451 | } |