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