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> |
Dmitry Shmidt | 7d05a80 | 2011-01-24 17:10:30 -0800 | [diff] [blame] | 28 | #include <net/if.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 29 | |
| 30 | #include <linux/if.h> |
Szymon Jakubczak | c88e09c | 2010-06-09 16:11:09 -0400 | [diff] [blame] | 31 | #include <linux/if_ether.h> |
| 32 | #include <linux/if_arp.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 33 | #include <linux/sockios.h> |
| 34 | #include <linux/route.h> |
Banavathu, Srinivas Naik | 8984bb9 | 2010-08-11 01:23:54 +0530 | [diff] [blame] | 35 | #include <linux/ipv6_route.h> |
| 36 | #include <netdb.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 37 | #include <linux/wireless.h> |
| 38 | |
| 39 | #ifdef ANDROID |
| 40 | #define LOG_TAG "NetUtils" |
| 41 | #include <cutils/log.h> |
| 42 | #include <cutils/properties.h> |
| 43 | #else |
| 44 | #include <stdio.h> |
| 45 | #include <string.h> |
| 46 | #define LOGD printf |
| 47 | #define LOGW printf |
| 48 | #endif |
| 49 | |
| 50 | static int ifc_ctl_sock = -1; |
Banavathu, Srinivas Naik | 8984bb9 | 2010-08-11 01:23:54 +0530 | [diff] [blame] | 51 | static int ifc_ctl_sock6 = -1; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 52 | void printerr(char *fmt, ...); |
| 53 | |
Robert Greenwalt | 09dd819 | 2011-02-01 15:21:21 -0800 | [diff] [blame] | 54 | in_addr_t prefixLengthToIpv4Netmask(int prefix_length) |
| 55 | { |
| 56 | in_addr_t mask = 0; |
| 57 | |
| 58 | // C99 (6.5.7): shifts of 32 bits have undefined results |
| 59 | if (prefix_length <= 0 || prefix_length > 32) { |
| 60 | return 0; |
| 61 | } |
| 62 | |
| 63 | mask = ~mask << (32 - prefix_length); |
| 64 | mask = htonl(mask); |
| 65 | |
| 66 | return mask; |
| 67 | } |
| 68 | |
| 69 | int ipv4NetmaskToPrefixLength(in_addr_t mask) |
| 70 | { |
| 71 | mask = ntohl(mask); |
| 72 | int prefixLength = 0; |
| 73 | uint32_t m = (uint32_t)mask; |
| 74 | while (m & 0x80000000) { |
| 75 | prefixLength++; |
| 76 | m = m << 1; |
| 77 | } |
| 78 | return prefixLength; |
| 79 | } |
| 80 | |
Szymon Jakubczak | c88e09c | 2010-06-09 16:11:09 -0400 | [diff] [blame] | 81 | 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] | 82 | { |
| 83 | struct in_addr in_addr; |
| 84 | |
| 85 | in_addr.s_addr = addr; |
| 86 | return inet_ntoa(in_addr); |
| 87 | } |
| 88 | |
| 89 | int ifc_init(void) |
| 90 | { |
| 91 | if (ifc_ctl_sock == -1) { |
| 92 | ifc_ctl_sock = socket(AF_INET, SOCK_DGRAM, 0); |
| 93 | if (ifc_ctl_sock < 0) { |
| 94 | printerr("socket() failed: %s\n", strerror(errno)); |
| 95 | } |
| 96 | } |
| 97 | return ifc_ctl_sock < 0 ? -1 : 0; |
| 98 | } |
| 99 | |
Banavathu, Srinivas Naik | 8984bb9 | 2010-08-11 01:23:54 +0530 | [diff] [blame] | 100 | int ifc_init6(void) |
| 101 | { |
| 102 | if (ifc_ctl_sock6 == -1) { |
| 103 | ifc_ctl_sock6 = socket(AF_INET6, SOCK_DGRAM, 0); |
| 104 | if (ifc_ctl_sock6 < 0) { |
| 105 | printerr("socket() failed: %s\n", strerror(errno)); |
| 106 | } |
| 107 | } |
| 108 | return ifc_ctl_sock6 < 0 ? -1 : 0; |
| 109 | } |
| 110 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 111 | void ifc_close(void) |
| 112 | { |
| 113 | if (ifc_ctl_sock != -1) { |
| 114 | (void)close(ifc_ctl_sock); |
| 115 | ifc_ctl_sock = -1; |
| 116 | } |
| 117 | } |
| 118 | |
Banavathu, Srinivas Naik | 8984bb9 | 2010-08-11 01:23:54 +0530 | [diff] [blame] | 119 | void ifc_close6(void) |
| 120 | { |
| 121 | if (ifc_ctl_sock6 != -1) { |
| 122 | (void)close(ifc_ctl_sock6); |
| 123 | ifc_ctl_sock6 = -1; |
| 124 | } |
| 125 | } |
| 126 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 127 | static void ifc_init_ifr(const char *name, struct ifreq *ifr) |
| 128 | { |
| 129 | memset(ifr, 0, sizeof(struct ifreq)); |
| 130 | strncpy(ifr->ifr_name, name, IFNAMSIZ); |
| 131 | ifr->ifr_name[IFNAMSIZ - 1] = 0; |
| 132 | } |
| 133 | |
| 134 | int ifc_get_hwaddr(const char *name, void *ptr) |
| 135 | { |
| 136 | int r; |
| 137 | struct ifreq ifr; |
| 138 | ifc_init_ifr(name, &ifr); |
| 139 | |
| 140 | r = ioctl(ifc_ctl_sock, SIOCGIFHWADDR, &ifr); |
| 141 | if(r < 0) return -1; |
| 142 | |
Szymon Jakubczak | c88e09c | 2010-06-09 16:11:09 -0400 | [diff] [blame] | 143 | memcpy(ptr, &ifr.ifr_hwaddr.sa_data, ETH_ALEN); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 144 | return 0; |
| 145 | } |
| 146 | |
| 147 | int ifc_get_ifindex(const char *name, int *if_indexp) |
| 148 | { |
| 149 | int r; |
| 150 | struct ifreq ifr; |
| 151 | ifc_init_ifr(name, &ifr); |
| 152 | |
| 153 | r = ioctl(ifc_ctl_sock, SIOCGIFINDEX, &ifr); |
| 154 | if(r < 0) return -1; |
| 155 | |
| 156 | *if_indexp = ifr.ifr_ifindex; |
Dmitry Shmidt | 7d05a80 | 2011-01-24 17:10:30 -0800 | [diff] [blame] | 157 | return 0; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | static int ifc_set_flags(const char *name, unsigned set, unsigned clr) |
| 161 | { |
| 162 | struct ifreq ifr; |
| 163 | ifc_init_ifr(name, &ifr); |
| 164 | |
| 165 | if(ioctl(ifc_ctl_sock, SIOCGIFFLAGS, &ifr) < 0) return -1; |
| 166 | ifr.ifr_flags = (ifr.ifr_flags & (~clr)) | set; |
| 167 | return ioctl(ifc_ctl_sock, SIOCSIFFLAGS, &ifr); |
| 168 | } |
| 169 | |
| 170 | int ifc_up(const char *name) |
| 171 | { |
| 172 | return ifc_set_flags(name, IFF_UP, 0); |
| 173 | } |
| 174 | |
| 175 | int ifc_down(const char *name) |
| 176 | { |
| 177 | return ifc_set_flags(name, 0, IFF_UP); |
| 178 | } |
| 179 | |
| 180 | static void init_sockaddr_in(struct sockaddr *sa, in_addr_t addr) |
| 181 | { |
| 182 | struct sockaddr_in *sin = (struct sockaddr_in *) sa; |
| 183 | sin->sin_family = AF_INET; |
| 184 | sin->sin_port = 0; |
| 185 | sin->sin_addr.s_addr = addr; |
| 186 | } |
| 187 | |
| 188 | int ifc_set_addr(const char *name, in_addr_t addr) |
| 189 | { |
| 190 | struct ifreq ifr; |
| 191 | |
| 192 | ifc_init_ifr(name, &ifr); |
| 193 | init_sockaddr_in(&ifr.ifr_addr, addr); |
Dmitry Shmidt | 7d05a80 | 2011-01-24 17:10:30 -0800 | [diff] [blame] | 194 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 195 | return ioctl(ifc_ctl_sock, SIOCSIFADDR, &ifr); |
| 196 | } |
| 197 | |
Szymon Jakubczak | c88e09c | 2010-06-09 16:11:09 -0400 | [diff] [blame] | 198 | int ifc_set_hwaddr(const char *name, const void *ptr) |
| 199 | { |
| 200 | int r; |
| 201 | struct ifreq ifr; |
| 202 | ifc_init_ifr(name, &ifr); |
| 203 | |
| 204 | ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER; |
| 205 | memcpy(&ifr.ifr_hwaddr.sa_data, ptr, ETH_ALEN); |
| 206 | return ioctl(ifc_ctl_sock, SIOCSIFHWADDR, &ifr); |
| 207 | } |
| 208 | |
Robert Greenwalt | 09dd819 | 2011-02-01 15:21:21 -0800 | [diff] [blame] | 209 | int ifc_set_prefixLength(const char *name, int prefixLength) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 210 | { |
| 211 | struct ifreq ifr; |
Robert Greenwalt | 09dd819 | 2011-02-01 15:21:21 -0800 | [diff] [blame] | 212 | // TODO - support ipv6 |
| 213 | if (prefixLength > 32 || prefixLength < 0) return -1; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 214 | |
Robert Greenwalt | 09dd819 | 2011-02-01 15:21:21 -0800 | [diff] [blame] | 215 | in_addr_t mask = prefixLengthToIpv4Netmask(prefixLength); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 216 | ifc_init_ifr(name, &ifr); |
| 217 | init_sockaddr_in(&ifr.ifr_addr, mask); |
Dmitry Shmidt | 7d05a80 | 2011-01-24 17:10:30 -0800 | [diff] [blame] | 218 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 219 | return ioctl(ifc_ctl_sock, SIOCSIFNETMASK, &ifr); |
| 220 | } |
| 221 | |
Dmitry Shmidt | 9092b91 | 2011-01-27 14:16:20 -0800 | [diff] [blame] | 222 | int ifc_get_addr(const char *name, in_addr_t *addr) |
| 223 | { |
| 224 | struct ifreq ifr; |
| 225 | int ret = 0; |
| 226 | |
| 227 | ifc_init_ifr(name, &ifr); |
| 228 | if (addr != NULL) { |
| 229 | ret = ioctl(ifc_ctl_sock, SIOCGIFADDR, &ifr); |
| 230 | if (ret < 0) { |
| 231 | *addr = 0; |
| 232 | } else { |
| 233 | *addr = ((struct sockaddr_in*) &ifr.ifr_addr)->sin_addr.s_addr; |
| 234 | } |
| 235 | } |
| 236 | return ret; |
| 237 | } |
| 238 | |
Robert Greenwalt | 09dd819 | 2011-02-01 15:21:21 -0800 | [diff] [blame] | 239 | int ifc_get_info(const char *name, in_addr_t *addr, int *prefixLength, unsigned *flags) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 240 | { |
| 241 | struct ifreq ifr; |
| 242 | ifc_init_ifr(name, &ifr); |
| 243 | |
| 244 | if (addr != NULL) { |
| 245 | if(ioctl(ifc_ctl_sock, SIOCGIFADDR, &ifr) < 0) { |
| 246 | *addr = 0; |
| 247 | } else { |
| 248 | *addr = ((struct sockaddr_in*) &ifr.ifr_addr)->sin_addr.s_addr; |
| 249 | } |
| 250 | } |
Dmitry Shmidt | 7d05a80 | 2011-01-24 17:10:30 -0800 | [diff] [blame] | 251 | |
Robert Greenwalt | 09dd819 | 2011-02-01 15:21:21 -0800 | [diff] [blame] | 252 | if (prefixLength != NULL) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 253 | if(ioctl(ifc_ctl_sock, SIOCGIFNETMASK, &ifr) < 0) { |
Robert Greenwalt | 09dd819 | 2011-02-01 15:21:21 -0800 | [diff] [blame] | 254 | *prefixLength = 0; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 255 | } else { |
Robert Greenwalt | 09dd819 | 2011-02-01 15:21:21 -0800 | [diff] [blame] | 256 | *prefixLength = ipv4NetmaskToPrefixLength((int) |
| 257 | ((struct sockaddr_in*) &ifr.ifr_addr)->sin_addr.s_addr); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 258 | } |
| 259 | } |
| 260 | |
| 261 | if (flags != NULL) { |
| 262 | if(ioctl(ifc_ctl_sock, SIOCGIFFLAGS, &ifr) < 0) { |
| 263 | *flags = 0; |
| 264 | } else { |
| 265 | *flags = ifr.ifr_flags; |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | return 0; |
| 270 | } |
| 271 | |
Robert Greenwalt | 021d0a2 | 2011-05-10 14:59:20 -0700 | [diff] [blame^] | 272 | int ifc_act_on_ipv4_route(int action, const char *ifname, struct in_addr dst, int prefix_length, |
Banavathu, Srinivas Naik | 8984bb9 | 2010-08-11 01:23:54 +0530 | [diff] [blame] | 273 | struct in_addr gw) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 274 | { |
| 275 | struct rtentry rt; |
| 276 | int result; |
Banavathu, Srinivas Naik | 8984bb9 | 2010-08-11 01:23:54 +0530 | [diff] [blame] | 277 | in_addr_t netmask; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 278 | |
| 279 | memset(&rt, 0, sizeof(rt)); |
Banavathu, Srinivas Naik | 8984bb9 | 2010-08-11 01:23:54 +0530 | [diff] [blame] | 280 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 281 | rt.rt_dst.sa_family = AF_INET; |
Banavathu, Srinivas Naik | 8984bb9 | 2010-08-11 01:23:54 +0530 | [diff] [blame] | 282 | rt.rt_dev = (void*) ifname; |
| 283 | |
Robert Greenwalt | 09dd819 | 2011-02-01 15:21:21 -0800 | [diff] [blame] | 284 | netmask = prefixLengthToIpv4Netmask(prefix_length); |
Banavathu, Srinivas Naik | 8984bb9 | 2010-08-11 01:23:54 +0530 | [diff] [blame] | 285 | init_sockaddr_in(&rt.rt_genmask, netmask); |
| 286 | init_sockaddr_in(&rt.rt_dst, dst.s_addr); |
| 287 | rt.rt_flags = RTF_UP; |
| 288 | |
| 289 | if (prefix_length == 32) { |
| 290 | rt.rt_flags |= RTF_HOST; |
| 291 | } |
| 292 | |
| 293 | if (gw.s_addr != 0) { |
| 294 | rt.rt_flags |= RTF_GATEWAY; |
| 295 | init_sockaddr_in(&rt.rt_gateway, gw.s_addr); |
| 296 | } |
| 297 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 298 | ifc_init(); |
Banavathu, Srinivas Naik | 8984bb9 | 2010-08-11 01:23:54 +0530 | [diff] [blame] | 299 | |
| 300 | if (ifc_ctl_sock < 0) { |
| 301 | return -errno; |
| 302 | } |
| 303 | |
Robert Greenwalt | 021d0a2 | 2011-05-10 14:59:20 -0700 | [diff] [blame^] | 304 | result = ioctl(ifc_ctl_sock, action, &rt); |
Banavathu, Srinivas Naik | 8984bb9 | 2010-08-11 01:23:54 +0530 | [diff] [blame] | 305 | if (result < 0) { |
| 306 | if (errno == EEXIST) { |
| 307 | result = 0; |
| 308 | } else { |
| 309 | result = -errno; |
| 310 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 311 | } |
| 312 | ifc_close(); |
| 313 | return result; |
| 314 | } |
| 315 | |
Banavathu, Srinivas Naik | 8984bb9 | 2010-08-11 01:23:54 +0530 | [diff] [blame] | 316 | int ifc_create_default_route(const char *name, in_addr_t gw) |
| 317 | { |
| 318 | struct in_addr in_dst, in_gw; |
| 319 | |
| 320 | in_dst.s_addr = 0; |
| 321 | in_gw.s_addr = gw; |
| 322 | |
Robert Greenwalt | 021d0a2 | 2011-05-10 14:59:20 -0700 | [diff] [blame^] | 323 | return ifc_act_on_route(SIOCADDRT, name, in_dst, 0, in_gw); |
Banavathu, Srinivas Naik | 8984bb9 | 2010-08-11 01:23:54 +0530 | [diff] [blame] | 324 | } |
| 325 | |
Mike Lockwood | feb63e9 | 2009-07-10 17:21:17 -0400 | [diff] [blame] | 326 | int ifc_enable(const char *ifname) |
| 327 | { |
| 328 | int result; |
| 329 | |
| 330 | ifc_init(); |
| 331 | result = ifc_up(ifname); |
| 332 | ifc_close(); |
| 333 | return result; |
| 334 | } |
| 335 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 336 | int ifc_disable(const char *ifname) |
| 337 | { |
Dmitry Shmidt | 9092b91 | 2011-01-27 14:16:20 -0800 | [diff] [blame] | 338 | unsigned addr, count; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 339 | int result; |
| 340 | |
| 341 | ifc_init(); |
| 342 | result = ifc_down(ifname); |
Dmitry Shmidt | 9092b91 | 2011-01-27 14:16:20 -0800 | [diff] [blame] | 343 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 344 | ifc_set_addr(ifname, 0); |
Dmitry Shmidt | 9092b91 | 2011-01-27 14:16:20 -0800 | [diff] [blame] | 345 | for (count=0, addr=1;((addr != 0) && (count < 255)); count++) { |
| 346 | if (ifc_get_addr(ifname, &addr) < 0) |
| 347 | break; |
| 348 | if (addr) |
| 349 | ifc_set_addr(ifname, 0); |
| 350 | } |
| 351 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 352 | ifc_close(); |
| 353 | return result; |
| 354 | } |
| 355 | |
| 356 | int ifc_reset_connections(const char *ifname) |
| 357 | { |
| 358 | #ifdef HAVE_ANDROID_OS |
Lorenzo Colitti | 6cf73ea | 2011-03-17 11:20:25 -0700 | [diff] [blame] | 359 | int result, success; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 360 | in_addr_t myaddr; |
| 361 | struct ifreq ifr; |
Lorenzo Colitti | 6cf73ea | 2011-03-17 11:20:25 -0700 | [diff] [blame] | 362 | struct in6_ifreq ifr6; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 363 | |
Lorenzo Colitti | 6cf73ea | 2011-03-17 11:20:25 -0700 | [diff] [blame] | 364 | /* IPv4. Clear connections on the IP address. */ |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 365 | ifc_init(); |
| 366 | ifc_get_info(ifname, &myaddr, NULL, NULL); |
| 367 | ifc_init_ifr(ifname, &ifr); |
| 368 | init_sockaddr_in(&ifr.ifr_addr, myaddr); |
| 369 | result = ioctl(ifc_ctl_sock, SIOCKILLADDR, &ifr); |
| 370 | ifc_close(); |
Dmitry Shmidt | 7d05a80 | 2011-01-24 17:10:30 -0800 | [diff] [blame] | 371 | |
Lorenzo Colitti | 6cf73ea | 2011-03-17 11:20:25 -0700 | [diff] [blame] | 372 | /* |
| 373 | * IPv6. On Linux, when an interface goes down it loses all its IPv6 |
| 374 | * addresses, so we don't know which connections belonged to that interface |
| 375 | * So we clear all unused IPv6 connections on the device by specifying an |
| 376 | * empty IPv6 address. |
| 377 | */ |
| 378 | ifc_init6(); |
| 379 | // This implicitly specifies an address of ::, i.e., kill all IPv6 sockets. |
| 380 | memset(&ifr6, 0, sizeof(ifr6)); |
| 381 | success = ioctl(ifc_ctl_sock6, SIOCKILLADDR, &ifr6); |
| 382 | if (result == 0) { |
| 383 | result = success; |
| 384 | } |
| 385 | ifc_close6(); |
| 386 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 387 | return result; |
| 388 | #else |
| 389 | return 0; |
| 390 | #endif |
| 391 | } |
| 392 | |
| 393 | /* |
| 394 | * Remove the routes associated with the named interface. |
| 395 | */ |
| 396 | int ifc_remove_host_routes(const char *name) |
| 397 | { |
| 398 | char ifname[64]; |
| 399 | in_addr_t dest, gway, mask; |
| 400 | int flags, refcnt, use, metric, mtu, win, irtt; |
| 401 | struct rtentry rt; |
| 402 | FILE *fp; |
| 403 | struct in_addr addr; |
| 404 | |
| 405 | fp = fopen("/proc/net/route", "r"); |
| 406 | if (fp == NULL) |
| 407 | return -1; |
| 408 | /* Skip the header line */ |
| 409 | if (fscanf(fp, "%*[^\n]\n") < 0) { |
| 410 | fclose(fp); |
| 411 | return -1; |
| 412 | } |
| 413 | ifc_init(); |
| 414 | for (;;) { |
| 415 | int nread = fscanf(fp, "%63s%X%X%X%d%d%d%X%d%d%d\n", |
| 416 | ifname, &dest, &gway, &flags, &refcnt, &use, &metric, &mask, |
| 417 | &mtu, &win, &irtt); |
| 418 | if (nread != 11) { |
| 419 | break; |
| 420 | } |
| 421 | if ((flags & (RTF_UP|RTF_HOST)) != (RTF_UP|RTF_HOST) |
| 422 | || strcmp(ifname, name) != 0) { |
| 423 | continue; |
| 424 | } |
| 425 | memset(&rt, 0, sizeof(rt)); |
| 426 | rt.rt_dev = (void *)name; |
| 427 | init_sockaddr_in(&rt.rt_dst, dest); |
| 428 | init_sockaddr_in(&rt.rt_gateway, gway); |
| 429 | init_sockaddr_in(&rt.rt_genmask, mask); |
| 430 | addr.s_addr = dest; |
| 431 | if (ioctl(ifc_ctl_sock, SIOCDELRT, &rt) < 0) { |
| 432 | LOGD("failed to remove route for %s to %s: %s", |
| 433 | ifname, inet_ntoa(addr), strerror(errno)); |
| 434 | } |
| 435 | } |
| 436 | fclose(fp); |
| 437 | ifc_close(); |
| 438 | return 0; |
| 439 | } |
| 440 | |
| 441 | /* |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 442 | * Removes the default route for the named interface. |
| 443 | */ |
| 444 | int ifc_remove_default_route(const char *ifname) |
| 445 | { |
| 446 | struct rtentry rt; |
| 447 | int result; |
| 448 | |
| 449 | ifc_init(); |
| 450 | memset(&rt, 0, sizeof(rt)); |
| 451 | rt.rt_dev = (void *)ifname; |
| 452 | rt.rt_flags = RTF_UP|RTF_GATEWAY; |
| 453 | init_sockaddr_in(&rt.rt_dst, 0); |
| 454 | if ((result = ioctl(ifc_ctl_sock, SIOCDELRT, &rt)) < 0) { |
| 455 | LOGD("failed to remove default route for %s: %s", ifname, strerror(errno)); |
| 456 | } |
| 457 | ifc_close(); |
| 458 | return result; |
| 459 | } |
| 460 | |
| 461 | int |
| 462 | ifc_configure(const char *ifname, |
| 463 | in_addr_t address, |
Robert Greenwalt | 09dd819 | 2011-02-01 15:21:21 -0800 | [diff] [blame] | 464 | uint32_t prefixLength, |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 465 | in_addr_t gateway, |
| 466 | in_addr_t dns1, |
| 467 | in_addr_t dns2) { |
| 468 | |
| 469 | char dns_prop_name[PROPERTY_KEY_MAX]; |
| 470 | |
| 471 | ifc_init(); |
| 472 | |
| 473 | if (ifc_up(ifname)) { |
| 474 | printerr("failed to turn on interface %s: %s\n", ifname, strerror(errno)); |
| 475 | ifc_close(); |
| 476 | return -1; |
| 477 | } |
| 478 | if (ifc_set_addr(ifname, address)) { |
| 479 | printerr("failed to set ipaddr %s: %s\n", ipaddr_to_string(address), strerror(errno)); |
| 480 | ifc_close(); |
| 481 | return -1; |
| 482 | } |
Robert Greenwalt | 09dd819 | 2011-02-01 15:21:21 -0800 | [diff] [blame] | 483 | if (ifc_set_prefixLength(ifname, prefixLength)) { |
| 484 | printerr("failed to set prefixLength %d: %s\n", prefixLength, strerror(errno)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 485 | ifc_close(); |
| 486 | return -1; |
| 487 | } |
| 488 | if (ifc_create_default_route(ifname, gateway)) { |
| 489 | printerr("failed to set default route %s: %s\n", ipaddr_to_string(gateway), strerror(errno)); |
| 490 | ifc_close(); |
| 491 | return -1; |
| 492 | } |
| 493 | |
| 494 | ifc_close(); |
| 495 | |
Szymon Jakubczak | c88e09c | 2010-06-09 16:11:09 -0400 | [diff] [blame] | 496 | 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] | 497 | property_set(dns_prop_name, dns1 ? ipaddr_to_string(dns1) : ""); |
Szymon Jakubczak | c88e09c | 2010-06-09 16:11:09 -0400 | [diff] [blame] | 498 | 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] | 499 | property_set(dns_prop_name, dns2 ? ipaddr_to_string(dns2) : ""); |
| 500 | |
| 501 | return 0; |
| 502 | } |
Banavathu, Srinivas Naik | 8984bb9 | 2010-08-11 01:23:54 +0530 | [diff] [blame] | 503 | |
Robert Greenwalt | 021d0a2 | 2011-05-10 14:59:20 -0700 | [diff] [blame^] | 504 | int ifc_act_on_ipv6_route(int action, const char *ifname, struct in6_addr dst, int prefix_length, |
Banavathu, Srinivas Naik | 8984bb9 | 2010-08-11 01:23:54 +0530 | [diff] [blame] | 505 | struct in6_addr gw) |
| 506 | { |
| 507 | struct in6_rtmsg rtmsg; |
| 508 | int result; |
| 509 | int ifindex; |
| 510 | |
| 511 | memset(&rtmsg, 0, sizeof(rtmsg)); |
| 512 | |
| 513 | ifindex = if_nametoindex(ifname); |
| 514 | if (ifindex == 0) { |
| 515 | printerr("if_nametoindex() failed: interface %s\n", ifname); |
| 516 | return -ENXIO; |
| 517 | } |
| 518 | |
| 519 | rtmsg.rtmsg_ifindex = ifindex; |
| 520 | rtmsg.rtmsg_dst = dst; |
| 521 | rtmsg.rtmsg_dst_len = prefix_length; |
| 522 | rtmsg.rtmsg_flags = RTF_UP; |
| 523 | |
| 524 | if (prefix_length == 128) { |
| 525 | rtmsg.rtmsg_flags |= RTF_HOST; |
| 526 | } |
| 527 | |
| 528 | if (memcmp(&gw, &in6addr_any, sizeof(in6addr_any))) { |
| 529 | rtmsg.rtmsg_flags |= RTF_GATEWAY; |
| 530 | rtmsg.rtmsg_gateway = gw; |
| 531 | } |
| 532 | |
| 533 | ifc_init6(); |
| 534 | |
| 535 | if (ifc_ctl_sock6 < 0) { |
| 536 | return -errno; |
| 537 | } |
| 538 | |
Robert Greenwalt | 021d0a2 | 2011-05-10 14:59:20 -0700 | [diff] [blame^] | 539 | result = ioctl(ifc_ctl_sock6, action, &rtmsg); |
Banavathu, Srinivas Naik | 8984bb9 | 2010-08-11 01:23:54 +0530 | [diff] [blame] | 540 | if (result < 0) { |
| 541 | if (errno == EEXIST) { |
| 542 | result = 0; |
| 543 | } else { |
| 544 | result = -errno; |
| 545 | } |
| 546 | } |
| 547 | ifc_close6(); |
| 548 | return result; |
| 549 | } |
| 550 | |
Robert Greenwalt | 021d0a2 | 2011-05-10 14:59:20 -0700 | [diff] [blame^] | 551 | int ifc_act_on_route(int action, const char *ifname, const char *dst, int prefix_length, |
| 552 | const char *gw) |
Banavathu, Srinivas Naik | 8984bb9 | 2010-08-11 01:23:54 +0530 | [diff] [blame] | 553 | { |
| 554 | int ret = 0; |
| 555 | struct sockaddr_in ipv4_dst, ipv4_gw; |
| 556 | struct sockaddr_in6 ipv6_dst, ipv6_gw; |
| 557 | struct addrinfo hints, *addr_ai, *gw_ai; |
| 558 | |
| 559 | memset(&hints, 0, sizeof(hints)); |
| 560 | hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */ |
| 561 | hints.ai_flags = AI_NUMERICHOST; |
| 562 | |
| 563 | ret = getaddrinfo(dst, NULL, &hints, &addr_ai); |
| 564 | |
| 565 | if (ret != 0) { |
| 566 | printerr("getaddrinfo failed: invalid address %s\n", dst); |
| 567 | return -EINVAL; |
| 568 | } |
| 569 | |
Robert Greenwalt | 021d0a2 | 2011-05-10 14:59:20 -0700 | [diff] [blame^] | 570 | if (gw == NULL || (strlen(gw) == 0)) { |
Banavathu, Srinivas Naik | 8984bb9 | 2010-08-11 01:23:54 +0530 | [diff] [blame] | 571 | if (addr_ai->ai_family == AF_INET6) { |
| 572 | gw = "::"; |
| 573 | } else if (addr_ai->ai_family == AF_INET) { |
| 574 | gw = "0.0.0.0"; |
| 575 | } |
| 576 | } |
| 577 | |
Robert Greenwalt | 021d0a2 | 2011-05-10 14:59:20 -0700 | [diff] [blame^] | 578 | if (((addr_ai->ai_family == AF_INET6) && (prefix_length < 0 || prefix_length > 128)) || |
| 579 | ((addr_ai->ai_family == AF_INET) && (prefix_length < 0 || prefix_length > 32))) { |
| 580 | printerr("ifc_add_route: invalid prefix length"); |
| 581 | freeaddrinfo(addr_ai); |
| 582 | return -EINVAL; |
| 583 | } |
| 584 | |
Banavathu, Srinivas Naik | 8984bb9 | 2010-08-11 01:23:54 +0530 | [diff] [blame] | 585 | ret = getaddrinfo(gw, NULL, &hints, &gw_ai); |
| 586 | if (ret != 0) { |
| 587 | printerr("getaddrinfo failed: invalid gateway %s\n", gw); |
| 588 | freeaddrinfo(addr_ai); |
| 589 | return -EINVAL; |
| 590 | } |
| 591 | |
| 592 | if (addr_ai->ai_family != gw_ai->ai_family) { |
| 593 | printerr("ifc_add_route: different address families: %s and %s\n", dst, gw); |
| 594 | freeaddrinfo(addr_ai); |
| 595 | freeaddrinfo(gw_ai); |
| 596 | return -EINVAL; |
| 597 | } |
| 598 | |
| 599 | if (addr_ai->ai_family == AF_INET6) { |
| 600 | memcpy(&ipv6_dst, addr_ai->ai_addr, sizeof(struct sockaddr_in6)); |
| 601 | memcpy(&ipv6_gw, gw_ai->ai_addr, sizeof(struct sockaddr_in6)); |
Robert Greenwalt | 021d0a2 | 2011-05-10 14:59:20 -0700 | [diff] [blame^] | 602 | ret = ifc_act_on_ipv6_route(action, ifname, ipv6_dst.sin6_addr, |
| 603 | prefix_length, ipv6_gw.sin6_addr); |
Banavathu, Srinivas Naik | 8984bb9 | 2010-08-11 01:23:54 +0530 | [diff] [blame] | 604 | } else if (addr_ai->ai_family == AF_INET) { |
| 605 | memcpy(&ipv4_dst, addr_ai->ai_addr, sizeof(struct sockaddr_in)); |
| 606 | memcpy(&ipv4_gw, gw_ai->ai_addr, sizeof(struct sockaddr_in)); |
Robert Greenwalt | 021d0a2 | 2011-05-10 14:59:20 -0700 | [diff] [blame^] | 607 | ret = ifc_act_on_ipv4_route(action, ifname, ipv4_dst.sin_addr, |
| 608 | prefix_length, ipv4_gw.sin_addr); |
Banavathu, Srinivas Naik | 8984bb9 | 2010-08-11 01:23:54 +0530 | [diff] [blame] | 609 | } else { |
| 610 | printerr("ifc_add_route: getaddrinfo returned un supported address family %d\n", |
| 611 | addr_ai->ai_family); |
| 612 | ret = -EAFNOSUPPORT; |
| 613 | } |
| 614 | |
| 615 | freeaddrinfo(addr_ai); |
| 616 | freeaddrinfo(gw_ai); |
| 617 | return ret; |
| 618 | } |
Robert Greenwalt | 021d0a2 | 2011-05-10 14:59:20 -0700 | [diff] [blame^] | 619 | |
| 620 | int ifc_add_route(const char *ifname, const char *dst, int prefix_length, const char *gw) |
| 621 | { |
| 622 | return ifc_act_on_route(SIOCADDRT, ifname, dst, prefix_length, gw); |
| 623 | } |
| 624 | |
| 625 | int ifc_remove_route(const char *ifname, const char*dst, int prefix_length, const char *gw) |
| 626 | { |
| 627 | return ifc_act_on_route(SIOCDELRT, ifname, dst, prefix_length, gw); |
| 628 | } |