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> |
Lorenzo Colitti | 47ddb51 | 2011-09-26 10:49:41 -0700 | [diff] [blame] | 29 | #include <netdb.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 30 | |
| 31 | #include <linux/if.h> |
Szymon Jakubczak | c88e09c | 2010-06-09 16:11:09 -0400 | [diff] [blame] | 32 | #include <linux/if_ether.h> |
| 33 | #include <linux/if_arp.h> |
Lorenzo Colitti | 47ddb51 | 2011-09-26 10:49:41 -0700 | [diff] [blame] | 34 | #include <linux/netlink.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 35 | #include <linux/route.h> |
Banavathu, Srinivas Naik | 8984bb9 | 2010-08-11 01:23:54 +0530 | [diff] [blame] | 36 | #include <linux/ipv6_route.h> |
Lorenzo Colitti | 47ddb51 | 2011-09-26 10:49:41 -0700 | [diff] [blame] | 37 | #include <linux/rtnetlink.h> |
| 38 | #include <linux/sockios.h> |
| 39 | |
| 40 | #include "netutils/ifc.h" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 41 | |
| 42 | #ifdef ANDROID |
| 43 | #define LOG_TAG "NetUtils" |
| 44 | #include <cutils/log.h> |
| 45 | #include <cutils/properties.h> |
| 46 | #else |
| 47 | #include <stdio.h> |
| 48 | #include <string.h> |
Steve Block | 8d66c49 | 2011-12-20 16:07:45 +0000 | [diff] [blame] | 49 | #define ALOGD printf |
Steve Block | ae8b56c | 2012-01-05 22:25:38 +0000 | [diff] [blame] | 50 | #define ALOGW printf |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 51 | #endif |
| 52 | |
Elliott Hughes | 9b828ad | 2015-07-30 08:47:35 -0700 | [diff] [blame] | 53 | #if defined(__ANDROID__) |
Elliott Hughes | 2d640c2 | 2013-11-12 13:05:01 -0800 | [diff] [blame] | 54 | /* SIOCKILLADDR is an Android extension. */ |
| 55 | #define SIOCKILLADDR 0x8939 |
| 56 | #endif |
| 57 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 58 | static int ifc_ctl_sock = -1; |
Banavathu, Srinivas Naik | 8984bb9 | 2010-08-11 01:23:54 +0530 | [diff] [blame] | 59 | static int ifc_ctl_sock6 = -1; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 60 | void printerr(char *fmt, ...); |
| 61 | |
Robert Greenwalt | b6b48ae | 2011-05-24 21:12:51 -0700 | [diff] [blame] | 62 | #define DBG 0 |
Lorenzo Colitti | 47ddb51 | 2011-09-26 10:49:41 -0700 | [diff] [blame] | 63 | #define INET_ADDRLEN 4 |
| 64 | #define INET6_ADDRLEN 16 |
Robert Greenwalt | b6b48ae | 2011-05-24 21:12:51 -0700 | [diff] [blame] | 65 | |
Robert Greenwalt | 09dd819 | 2011-02-01 15:21:21 -0800 | [diff] [blame] | 66 | in_addr_t prefixLengthToIpv4Netmask(int prefix_length) |
| 67 | { |
| 68 | in_addr_t mask = 0; |
| 69 | |
| 70 | // C99 (6.5.7): shifts of 32 bits have undefined results |
| 71 | if (prefix_length <= 0 || prefix_length > 32) { |
| 72 | return 0; |
| 73 | } |
| 74 | |
| 75 | mask = ~mask << (32 - prefix_length); |
| 76 | mask = htonl(mask); |
| 77 | |
| 78 | return mask; |
| 79 | } |
| 80 | |
| 81 | int ipv4NetmaskToPrefixLength(in_addr_t mask) |
| 82 | { |
Robert Greenwalt | 09dd819 | 2011-02-01 15:21:21 -0800 | [diff] [blame] | 83 | int prefixLength = 0; |
Chris Dearman | 6ee3ecc | 2011-06-17 17:07:46 -0700 | [diff] [blame] | 84 | uint32_t m = (uint32_t)ntohl(mask); |
Robert Greenwalt | 09dd819 | 2011-02-01 15:21:21 -0800 | [diff] [blame] | 85 | while (m & 0x80000000) { |
| 86 | prefixLength++; |
| 87 | m = m << 1; |
| 88 | } |
| 89 | return prefixLength; |
| 90 | } |
| 91 | |
Szymon Jakubczak | c88e09c | 2010-06-09 16:11:09 -0400 | [diff] [blame] | 92 | 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] | 93 | { |
| 94 | struct in_addr in_addr; |
| 95 | |
| 96 | in_addr.s_addr = addr; |
| 97 | return inet_ntoa(in_addr); |
| 98 | } |
| 99 | |
Lorenzo Colitti | 47ddb51 | 2011-09-26 10:49:41 -0700 | [diff] [blame] | 100 | int string_to_ip(const char *string, struct sockaddr_storage *ss) { |
| 101 | struct addrinfo hints, *ai; |
| 102 | int ret; |
| 103 | |
| 104 | if (ss == NULL) { |
| 105 | return -EFAULT; |
| 106 | } |
| 107 | |
| 108 | memset(&hints, 0, sizeof(hints)); |
| 109 | hints.ai_family = AF_UNSPEC; |
| 110 | hints.ai_flags = AI_NUMERICHOST; |
| 111 | hints.ai_socktype = SOCK_DGRAM; |
| 112 | |
| 113 | ret = getaddrinfo(string, NULL, &hints, &ai); |
| 114 | if (ret == 0) { |
| 115 | memcpy(ss, ai->ai_addr, ai->ai_addrlen); |
| 116 | freeaddrinfo(ai); |
| 117 | } |
| 118 | |
| 119 | return ret; |
| 120 | } |
| 121 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 122 | int ifc_init(void) |
| 123 | { |
Robert Greenwalt | b6b48ae | 2011-05-24 21:12:51 -0700 | [diff] [blame] | 124 | int ret; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 125 | if (ifc_ctl_sock == -1) { |
Nick Kralevich | 6d3cddb | 2015-02-26 13:32:52 -0800 | [diff] [blame] | 126 | ifc_ctl_sock = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 127 | if (ifc_ctl_sock < 0) { |
| 128 | printerr("socket() failed: %s\n", strerror(errno)); |
| 129 | } |
| 130 | } |
Robert Greenwalt | b6b48ae | 2011-05-24 21:12:51 -0700 | [diff] [blame] | 131 | |
| 132 | ret = ifc_ctl_sock < 0 ? -1 : 0; |
| 133 | if (DBG) printerr("ifc_init_returning %d", ret); |
| 134 | return ret; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 135 | } |
| 136 | |
Banavathu, Srinivas Naik | 8984bb9 | 2010-08-11 01:23:54 +0530 | [diff] [blame] | 137 | int ifc_init6(void) |
| 138 | { |
| 139 | if (ifc_ctl_sock6 == -1) { |
Nick Kralevich | 6d3cddb | 2015-02-26 13:32:52 -0800 | [diff] [blame] | 140 | ifc_ctl_sock6 = socket(AF_INET6, SOCK_DGRAM | SOCK_CLOEXEC, 0); |
Banavathu, Srinivas Naik | 8984bb9 | 2010-08-11 01:23:54 +0530 | [diff] [blame] | 141 | if (ifc_ctl_sock6 < 0) { |
| 142 | printerr("socket() failed: %s\n", strerror(errno)); |
| 143 | } |
| 144 | } |
| 145 | return ifc_ctl_sock6 < 0 ? -1 : 0; |
| 146 | } |
| 147 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 148 | void ifc_close(void) |
| 149 | { |
Robert Greenwalt | b6b48ae | 2011-05-24 21:12:51 -0700 | [diff] [blame] | 150 | if (DBG) printerr("ifc_close"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 151 | if (ifc_ctl_sock != -1) { |
| 152 | (void)close(ifc_ctl_sock); |
| 153 | ifc_ctl_sock = -1; |
| 154 | } |
| 155 | } |
| 156 | |
Banavathu, Srinivas Naik | 8984bb9 | 2010-08-11 01:23:54 +0530 | [diff] [blame] | 157 | void ifc_close6(void) |
| 158 | { |
| 159 | if (ifc_ctl_sock6 != -1) { |
| 160 | (void)close(ifc_ctl_sock6); |
| 161 | ifc_ctl_sock6 = -1; |
| 162 | } |
| 163 | } |
| 164 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 165 | static void ifc_init_ifr(const char *name, struct ifreq *ifr) |
| 166 | { |
| 167 | memset(ifr, 0, sizeof(struct ifreq)); |
| 168 | strncpy(ifr->ifr_name, name, IFNAMSIZ); |
| 169 | ifr->ifr_name[IFNAMSIZ - 1] = 0; |
| 170 | } |
| 171 | |
| 172 | int ifc_get_hwaddr(const char *name, void *ptr) |
| 173 | { |
| 174 | int r; |
| 175 | struct ifreq ifr; |
| 176 | ifc_init_ifr(name, &ifr); |
| 177 | |
| 178 | r = ioctl(ifc_ctl_sock, SIOCGIFHWADDR, &ifr); |
| 179 | if(r < 0) return -1; |
| 180 | |
Szymon Jakubczak | c88e09c | 2010-06-09 16:11:09 -0400 | [diff] [blame] | 181 | memcpy(ptr, &ifr.ifr_hwaddr.sa_data, ETH_ALEN); |
Robert Greenwalt | b6b48ae | 2011-05-24 21:12:51 -0700 | [diff] [blame] | 182 | return 0; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | int ifc_get_ifindex(const char *name, int *if_indexp) |
| 186 | { |
| 187 | int r; |
| 188 | struct ifreq ifr; |
| 189 | ifc_init_ifr(name, &ifr); |
| 190 | |
| 191 | r = ioctl(ifc_ctl_sock, SIOCGIFINDEX, &ifr); |
| 192 | if(r < 0) return -1; |
| 193 | |
| 194 | *if_indexp = ifr.ifr_ifindex; |
Dmitry Shmidt | 7d05a80 | 2011-01-24 17:10:30 -0800 | [diff] [blame] | 195 | return 0; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | static int ifc_set_flags(const char *name, unsigned set, unsigned clr) |
| 199 | { |
| 200 | struct ifreq ifr; |
| 201 | ifc_init_ifr(name, &ifr); |
| 202 | |
| 203 | if(ioctl(ifc_ctl_sock, SIOCGIFFLAGS, &ifr) < 0) return -1; |
| 204 | ifr.ifr_flags = (ifr.ifr_flags & (~clr)) | set; |
| 205 | return ioctl(ifc_ctl_sock, SIOCSIFFLAGS, &ifr); |
| 206 | } |
| 207 | |
| 208 | int ifc_up(const char *name) |
| 209 | { |
Robert Greenwalt | b6b48ae | 2011-05-24 21:12:51 -0700 | [diff] [blame] | 210 | int ret = ifc_set_flags(name, IFF_UP, 0); |
| 211 | if (DBG) printerr("ifc_up(%s) = %d", name, ret); |
| 212 | return ret; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | int ifc_down(const char *name) |
| 216 | { |
Robert Greenwalt | b6b48ae | 2011-05-24 21:12:51 -0700 | [diff] [blame] | 217 | int ret = ifc_set_flags(name, 0, IFF_UP); |
| 218 | if (DBG) printerr("ifc_down(%s) = %d", name, ret); |
| 219 | return ret; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | static void init_sockaddr_in(struct sockaddr *sa, in_addr_t addr) |
| 223 | { |
| 224 | struct sockaddr_in *sin = (struct sockaddr_in *) sa; |
| 225 | sin->sin_family = AF_INET; |
| 226 | sin->sin_port = 0; |
| 227 | sin->sin_addr.s_addr = addr; |
| 228 | } |
| 229 | |
| 230 | int ifc_set_addr(const char *name, in_addr_t addr) |
| 231 | { |
| 232 | struct ifreq ifr; |
Robert Greenwalt | b6b48ae | 2011-05-24 21:12:51 -0700 | [diff] [blame] | 233 | int ret; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 234 | |
| 235 | ifc_init_ifr(name, &ifr); |
| 236 | init_sockaddr_in(&ifr.ifr_addr, addr); |
Dmitry Shmidt | 7d05a80 | 2011-01-24 17:10:30 -0800 | [diff] [blame] | 237 | |
Robert Greenwalt | b6b48ae | 2011-05-24 21:12:51 -0700 | [diff] [blame] | 238 | ret = ioctl(ifc_ctl_sock, SIOCSIFADDR, &ifr); |
| 239 | if (DBG) printerr("ifc_set_addr(%s, xx) = %d", name, ret); |
| 240 | return ret; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 241 | } |
| 242 | |
Lorenzo Colitti | 47ddb51 | 2011-09-26 10:49:41 -0700 | [diff] [blame] | 243 | /* |
| 244 | * Adds or deletes an IP address on an interface. |
| 245 | * |
| 246 | * Action is one of: |
| 247 | * - RTM_NEWADDR (to add a new address) |
| 248 | * - RTM_DELADDR (to delete an existing address) |
| 249 | * |
| 250 | * Returns zero on success and negative errno on failure. |
| 251 | */ |
| 252 | int ifc_act_on_address(int action, const char *name, const char *address, |
| 253 | int prefixlen) { |
| 254 | int ifindex, s, len, ret; |
| 255 | struct sockaddr_storage ss; |
| 256 | void *addr; |
| 257 | size_t addrlen; |
| 258 | struct { |
| 259 | struct nlmsghdr n; |
| 260 | struct ifaddrmsg r; |
| 261 | // Allow for IPv6 address, headers, and padding. |
| 262 | char attrbuf[NLMSG_ALIGN(sizeof(struct nlmsghdr)) + |
| 263 | NLMSG_ALIGN(sizeof(struct rtattr)) + |
| 264 | NLMSG_ALIGN(INET6_ADDRLEN)]; |
| 265 | } req; |
| 266 | struct rtattr *rta; |
| 267 | struct nlmsghdr *nh; |
| 268 | struct nlmsgerr *err; |
| 269 | char buf[NLMSG_ALIGN(sizeof(struct nlmsghdr)) + |
| 270 | NLMSG_ALIGN(sizeof(struct nlmsgerr)) + |
| 271 | NLMSG_ALIGN(sizeof(struct nlmsghdr))]; |
| 272 | |
| 273 | // Get interface ID. |
| 274 | ifindex = if_nametoindex(name); |
| 275 | if (ifindex == 0) { |
| 276 | return -errno; |
| 277 | } |
| 278 | |
| 279 | // Convert string representation to sockaddr_storage. |
| 280 | ret = string_to_ip(address, &ss); |
| 281 | if (ret) { |
| 282 | return ret; |
| 283 | } |
| 284 | |
| 285 | // Determine address type and length. |
| 286 | if (ss.ss_family == AF_INET) { |
| 287 | struct sockaddr_in *sin = (struct sockaddr_in *) &ss; |
| 288 | addr = &sin->sin_addr; |
| 289 | addrlen = INET_ADDRLEN; |
| 290 | } else if (ss.ss_family == AF_INET6) { |
| 291 | struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) &ss; |
| 292 | addr = &sin6->sin6_addr; |
| 293 | addrlen = INET6_ADDRLEN; |
| 294 | } else { |
| 295 | return -EAFNOSUPPORT; |
| 296 | } |
| 297 | |
| 298 | // Fill in netlink structures. |
| 299 | memset(&req, 0, sizeof(req)); |
| 300 | |
| 301 | // Netlink message header. |
| 302 | req.n.nlmsg_len = NLMSG_LENGTH(sizeof(req.r)); |
| 303 | req.n.nlmsg_type = action; |
| 304 | req.n.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK; |
| 305 | req.n.nlmsg_pid = getpid(); |
| 306 | |
| 307 | // Interface address message header. |
| 308 | req.r.ifa_family = ss.ss_family; |
| 309 | req.r.ifa_prefixlen = prefixlen; |
| 310 | req.r.ifa_index = ifindex; |
| 311 | |
| 312 | // Routing attribute. Contains the actual IP address. |
| 313 | rta = (struct rtattr *) (((char *) &req) + NLMSG_ALIGN(req.n.nlmsg_len)); |
| 314 | rta->rta_type = IFA_LOCAL; |
| 315 | rta->rta_len = RTA_LENGTH(addrlen); |
| 316 | req.n.nlmsg_len = NLMSG_ALIGN(req.n.nlmsg_len) + RTA_LENGTH(addrlen); |
| 317 | memcpy(RTA_DATA(rta), addr, addrlen); |
| 318 | |
Nick Kralevich | 6d3cddb | 2015-02-26 13:32:52 -0800 | [diff] [blame] | 319 | s = socket(PF_NETLINK, SOCK_RAW | SOCK_CLOEXEC, NETLINK_ROUTE); |
Lorenzo Colitti | 47ddb51 | 2011-09-26 10:49:41 -0700 | [diff] [blame] | 320 | if (send(s, &req, req.n.nlmsg_len, 0) < 0) { |
| 321 | close(s); |
| 322 | return -errno; |
| 323 | } |
| 324 | |
| 325 | len = recv(s, buf, sizeof(buf), 0); |
| 326 | close(s); |
| 327 | if (len < 0) { |
| 328 | return -errno; |
| 329 | } |
| 330 | |
| 331 | // Parse the acknowledgement to find the return code. |
| 332 | nh = (struct nlmsghdr *) buf; |
| 333 | if (!NLMSG_OK(nh, (unsigned) len) || nh->nlmsg_type != NLMSG_ERROR) { |
| 334 | return -EINVAL; |
| 335 | } |
| 336 | err = NLMSG_DATA(nh); |
| 337 | |
| 338 | // Return code is negative errno. |
| 339 | return err->error; |
| 340 | } |
| 341 | |
| 342 | int ifc_add_address(const char *name, const char *address, int prefixlen) { |
| 343 | return ifc_act_on_address(RTM_NEWADDR, name, address, prefixlen); |
| 344 | } |
| 345 | |
| 346 | int ifc_del_address(const char *name, const char * address, int prefixlen) { |
| 347 | return ifc_act_on_address(RTM_DELADDR, name, address, prefixlen); |
| 348 | } |
| 349 | |
| 350 | /* |
| 351 | * Clears IPv6 addresses on the specified interface. |
| 352 | */ |
| 353 | int ifc_clear_ipv6_addresses(const char *name) { |
| 354 | char rawaddrstr[INET6_ADDRSTRLEN], addrstr[INET6_ADDRSTRLEN]; |
| 355 | unsigned int prefixlen; |
| 356 | int lasterror = 0, i, j, ret; |
| 357 | char ifname[64]; // Currently, IFNAMSIZ = 16. |
| 358 | FILE *f = fopen("/proc/net/if_inet6", "r"); |
| 359 | if (!f) { |
| 360 | return -errno; |
| 361 | } |
| 362 | |
| 363 | // Format: |
| 364 | // 20010db8000a0001fc446aa4b5b347ed 03 40 00 01 wlan0 |
| 365 | while (fscanf(f, "%32s %*02x %02x %*02x %*02x %63s\n", |
| 366 | rawaddrstr, &prefixlen, ifname) == 3) { |
| 367 | // Is this the interface we're looking for? |
| 368 | if (strcmp(name, ifname)) { |
| 369 | continue; |
| 370 | } |
| 371 | |
| 372 | // Put the colons back into the address. |
| 373 | for (i = 0, j = 0; i < 32; i++, j++) { |
| 374 | addrstr[j] = rawaddrstr[i]; |
| 375 | if (i % 4 == 3) { |
| 376 | addrstr[++j] = ':'; |
| 377 | } |
| 378 | } |
| 379 | addrstr[j - 1] = '\0'; |
| 380 | |
| 381 | // Don't delete the link-local address as well, or it will disable IPv6 |
| 382 | // on the interface. |
| 383 | if (strncmp(addrstr, "fe80:", 5) == 0) { |
| 384 | continue; |
| 385 | } |
| 386 | |
| 387 | ret = ifc_del_address(ifname, addrstr, prefixlen); |
| 388 | if (ret) { |
Steve Block | 01dda20 | 2012-01-06 14:13:42 +0000 | [diff] [blame] | 389 | ALOGE("Deleting address %s/%d on %s: %s", addrstr, prefixlen, ifname, |
Lorenzo Colitti | 47ddb51 | 2011-09-26 10:49:41 -0700 | [diff] [blame] | 390 | strerror(-ret)); |
| 391 | lasterror = ret; |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | fclose(f); |
| 396 | return lasterror; |
| 397 | } |
| 398 | |
| 399 | /* |
| 400 | * Clears IPv4 addresses on the specified interface. |
| 401 | */ |
| 402 | void ifc_clear_ipv4_addresses(const char *name) { |
| 403 | unsigned count, addr; |
| 404 | ifc_init(); |
| 405 | for (count=0, addr=1;((addr != 0) && (count < 255)); count++) { |
| 406 | if (ifc_get_addr(name, &addr) < 0) |
| 407 | break; |
| 408 | if (addr) |
| 409 | ifc_set_addr(name, 0); |
| 410 | } |
| 411 | ifc_close(); |
| 412 | } |
| 413 | |
| 414 | /* |
| 415 | * Clears all IP addresses on the specified interface. |
| 416 | */ |
| 417 | int ifc_clear_addresses(const char *name) { |
| 418 | ifc_clear_ipv4_addresses(name); |
| 419 | return ifc_clear_ipv6_addresses(name); |
| 420 | } |
| 421 | |
Szymon Jakubczak | c88e09c | 2010-06-09 16:11:09 -0400 | [diff] [blame] | 422 | int ifc_set_hwaddr(const char *name, const void *ptr) |
| 423 | { |
Szymon Jakubczak | c88e09c | 2010-06-09 16:11:09 -0400 | [diff] [blame] | 424 | struct ifreq ifr; |
| 425 | ifc_init_ifr(name, &ifr); |
| 426 | |
| 427 | ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER; |
| 428 | memcpy(&ifr.ifr_hwaddr.sa_data, ptr, ETH_ALEN); |
| 429 | return ioctl(ifc_ctl_sock, SIOCSIFHWADDR, &ifr); |
| 430 | } |
| 431 | |
Jake Hamby | 6f49d5f | 2011-04-11 19:46:41 -0700 | [diff] [blame] | 432 | int ifc_set_mask(const char *name, in_addr_t mask) |
| 433 | { |
| 434 | struct ifreq ifr; |
Robert Greenwalt | b6b48ae | 2011-05-24 21:12:51 -0700 | [diff] [blame] | 435 | int ret; |
Jake Hamby | 6f49d5f | 2011-04-11 19:46:41 -0700 | [diff] [blame] | 436 | |
| 437 | ifc_init_ifr(name, &ifr); |
| 438 | init_sockaddr_in(&ifr.ifr_addr, mask); |
| 439 | |
Robert Greenwalt | b6b48ae | 2011-05-24 21:12:51 -0700 | [diff] [blame] | 440 | ret = ioctl(ifc_ctl_sock, SIOCSIFNETMASK, &ifr); |
| 441 | if (DBG) printerr("ifc_set_mask(%s, xx) = %d", name, ret); |
| 442 | return ret; |
Jake Hamby | 6f49d5f | 2011-04-11 19:46:41 -0700 | [diff] [blame] | 443 | } |
| 444 | |
Robert Greenwalt | 09dd819 | 2011-02-01 15:21:21 -0800 | [diff] [blame] | 445 | int ifc_set_prefixLength(const char *name, int prefixLength) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 446 | { |
| 447 | struct ifreq ifr; |
Robert Greenwalt | 09dd819 | 2011-02-01 15:21:21 -0800 | [diff] [blame] | 448 | // TODO - support ipv6 |
| 449 | if (prefixLength > 32 || prefixLength < 0) return -1; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 450 | |
Robert Greenwalt | 09dd819 | 2011-02-01 15:21:21 -0800 | [diff] [blame] | 451 | in_addr_t mask = prefixLengthToIpv4Netmask(prefixLength); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 452 | ifc_init_ifr(name, &ifr); |
| 453 | init_sockaddr_in(&ifr.ifr_addr, mask); |
Dmitry Shmidt | 7d05a80 | 2011-01-24 17:10:30 -0800 | [diff] [blame] | 454 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 455 | return ioctl(ifc_ctl_sock, SIOCSIFNETMASK, &ifr); |
| 456 | } |
| 457 | |
Dmitry Shmidt | 9092b91 | 2011-01-27 14:16:20 -0800 | [diff] [blame] | 458 | int ifc_get_addr(const char *name, in_addr_t *addr) |
| 459 | { |
| 460 | struct ifreq ifr; |
| 461 | int ret = 0; |
| 462 | |
| 463 | ifc_init_ifr(name, &ifr); |
| 464 | if (addr != NULL) { |
| 465 | ret = ioctl(ifc_ctl_sock, SIOCGIFADDR, &ifr); |
| 466 | if (ret < 0) { |
| 467 | *addr = 0; |
| 468 | } else { |
| 469 | *addr = ((struct sockaddr_in*) &ifr.ifr_addr)->sin_addr.s_addr; |
| 470 | } |
| 471 | } |
| 472 | return ret; |
| 473 | } |
| 474 | |
Robert Greenwalt | 09dd819 | 2011-02-01 15:21:21 -0800 | [diff] [blame] | 475 | 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] | 476 | { |
| 477 | struct ifreq ifr; |
| 478 | ifc_init_ifr(name, &ifr); |
| 479 | |
| 480 | if (addr != NULL) { |
| 481 | if(ioctl(ifc_ctl_sock, SIOCGIFADDR, &ifr) < 0) { |
| 482 | *addr = 0; |
| 483 | } else { |
| 484 | *addr = ((struct sockaddr_in*) &ifr.ifr_addr)->sin_addr.s_addr; |
| 485 | } |
| 486 | } |
Dmitry Shmidt | 7d05a80 | 2011-01-24 17:10:30 -0800 | [diff] [blame] | 487 | |
Robert Greenwalt | 09dd819 | 2011-02-01 15:21:21 -0800 | [diff] [blame] | 488 | if (prefixLength != NULL) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 489 | if(ioctl(ifc_ctl_sock, SIOCGIFNETMASK, &ifr) < 0) { |
Robert Greenwalt | 09dd819 | 2011-02-01 15:21:21 -0800 | [diff] [blame] | 490 | *prefixLength = 0; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 491 | } else { |
Chris Dearman | 6ee3ecc | 2011-06-17 17:07:46 -0700 | [diff] [blame] | 492 | *prefixLength = ipv4NetmaskToPrefixLength( |
Robert Greenwalt | 09dd819 | 2011-02-01 15:21:21 -0800 | [diff] [blame] | 493 | ((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] | 494 | } |
| 495 | } |
| 496 | |
| 497 | if (flags != NULL) { |
| 498 | if(ioctl(ifc_ctl_sock, SIOCGIFFLAGS, &ifr) < 0) { |
| 499 | *flags = 0; |
| 500 | } else { |
| 501 | *flags = ifr.ifr_flags; |
| 502 | } |
| 503 | } |
| 504 | |
| 505 | return 0; |
| 506 | } |
| 507 | |
Robert Greenwalt | 021d0a2 | 2011-05-10 14:59:20 -0700 | [diff] [blame] | 508 | 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] | 509 | struct in_addr gw) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 510 | { |
| 511 | struct rtentry rt; |
| 512 | int result; |
Banavathu, Srinivas Naik | 8984bb9 | 2010-08-11 01:23:54 +0530 | [diff] [blame] | 513 | in_addr_t netmask; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 514 | |
| 515 | memset(&rt, 0, sizeof(rt)); |
Banavathu, Srinivas Naik | 8984bb9 | 2010-08-11 01:23:54 +0530 | [diff] [blame] | 516 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 517 | rt.rt_dst.sa_family = AF_INET; |
Banavathu, Srinivas Naik | 8984bb9 | 2010-08-11 01:23:54 +0530 | [diff] [blame] | 518 | rt.rt_dev = (void*) ifname; |
| 519 | |
Robert Greenwalt | 09dd819 | 2011-02-01 15:21:21 -0800 | [diff] [blame] | 520 | netmask = prefixLengthToIpv4Netmask(prefix_length); |
Banavathu, Srinivas Naik | 8984bb9 | 2010-08-11 01:23:54 +0530 | [diff] [blame] | 521 | init_sockaddr_in(&rt.rt_genmask, netmask); |
| 522 | init_sockaddr_in(&rt.rt_dst, dst.s_addr); |
| 523 | rt.rt_flags = RTF_UP; |
| 524 | |
| 525 | if (prefix_length == 32) { |
| 526 | rt.rt_flags |= RTF_HOST; |
| 527 | } |
| 528 | |
| 529 | if (gw.s_addr != 0) { |
| 530 | rt.rt_flags |= RTF_GATEWAY; |
| 531 | init_sockaddr_in(&rt.rt_gateway, gw.s_addr); |
| 532 | } |
| 533 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 534 | ifc_init(); |
Banavathu, Srinivas Naik | 8984bb9 | 2010-08-11 01:23:54 +0530 | [diff] [blame] | 535 | |
| 536 | if (ifc_ctl_sock < 0) { |
| 537 | return -errno; |
| 538 | } |
| 539 | |
Robert Greenwalt | 021d0a2 | 2011-05-10 14:59:20 -0700 | [diff] [blame] | 540 | result = ioctl(ifc_ctl_sock, action, &rt); |
Banavathu, Srinivas Naik | 8984bb9 | 2010-08-11 01:23:54 +0530 | [diff] [blame] | 541 | if (result < 0) { |
| 542 | if (errno == EEXIST) { |
| 543 | result = 0; |
| 544 | } else { |
| 545 | result = -errno; |
| 546 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 547 | } |
| 548 | ifc_close(); |
| 549 | return result; |
| 550 | } |
| 551 | |
Robert Greenwalt | b6b48ae | 2011-05-24 21:12:51 -0700 | [diff] [blame] | 552 | /* deprecated - v4 only */ |
Banavathu, Srinivas Naik | 8984bb9 | 2010-08-11 01:23:54 +0530 | [diff] [blame] | 553 | int ifc_create_default_route(const char *name, in_addr_t gw) |
| 554 | { |
| 555 | struct in_addr in_dst, in_gw; |
| 556 | |
| 557 | in_dst.s_addr = 0; |
| 558 | in_gw.s_addr = gw; |
| 559 | |
Robert Greenwalt | b6b48ae | 2011-05-24 21:12:51 -0700 | [diff] [blame] | 560 | int ret = ifc_act_on_ipv4_route(SIOCADDRT, name, in_dst, 0, in_gw); |
| 561 | if (DBG) printerr("ifc_create_default_route(%s, %d) = %d", name, gw, ret); |
| 562 | return ret; |
| 563 | } |
| 564 | |
Sreeram Ramachandran | eec2326 | 2014-07-23 09:30:53 -0700 | [diff] [blame] | 565 | // Needed by code in hidden partner repositories / branches, so don't delete. |
| 566 | int ifc_enable(const char *ifname) |
| 567 | { |
| 568 | int result; |
| 569 | |
| 570 | ifc_init(); |
| 571 | result = ifc_up(ifname); |
| 572 | ifc_close(); |
| 573 | return result; |
| 574 | } |
| 575 | |
Sreeram Ramachandran | e5ab3e3 | 2014-07-23 11:33:36 -0700 | [diff] [blame] | 576 | // Needed by code in hidden partner repositories / branches, so don't delete. |
Sreeram Ramachandran | eec2326 | 2014-07-23 09:30:53 -0700 | [diff] [blame] | 577 | int ifc_disable(const char *ifname) |
| 578 | { |
| 579 | unsigned addr, count; |
| 580 | int result; |
| 581 | |
| 582 | ifc_init(); |
| 583 | result = ifc_down(ifname); |
| 584 | |
| 585 | ifc_set_addr(ifname, 0); |
| 586 | for (count=0, addr=1;((addr != 0) && (count < 255)); count++) { |
| 587 | if (ifc_get_addr(ifname, &addr) < 0) |
| 588 | break; |
| 589 | if (addr) |
| 590 | ifc_set_addr(ifname, 0); |
| 591 | } |
| 592 | |
| 593 | ifc_close(); |
| 594 | return result; |
| 595 | } |
| 596 | |
Wink Saville | 979203e | 2011-07-07 12:16:12 -0700 | [diff] [blame] | 597 | int ifc_reset_connections(const char *ifname, const int reset_mask) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 598 | { |
Elliott Hughes | 9b828ad | 2015-07-30 08:47:35 -0700 | [diff] [blame] | 599 | #if defined(__ANDROID__) |
Lorenzo Colitti | 6cf73ea | 2011-03-17 11:20:25 -0700 | [diff] [blame] | 600 | int result, success; |
Sreeram Ramachandran | 17622d0 | 2014-09-08 16:05:09 -0700 | [diff] [blame] | 601 | in_addr_t myaddr = 0; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 602 | struct ifreq ifr; |
Lorenzo Colitti | 6cf73ea | 2011-03-17 11:20:25 -0700 | [diff] [blame] | 603 | struct in6_ifreq ifr6; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 604 | |
Wink Saville | 979203e | 2011-07-07 12:16:12 -0700 | [diff] [blame] | 605 | if (reset_mask & RESET_IPV4_ADDRESSES) { |
| 606 | /* IPv4. Clear connections on the IP address. */ |
| 607 | ifc_init(); |
Sreeram Ramachandran | 17622d0 | 2014-09-08 16:05:09 -0700 | [diff] [blame] | 608 | if (!(reset_mask & RESET_IGNORE_INTERFACE_ADDRESS)) { |
| 609 | ifc_get_info(ifname, &myaddr, NULL, NULL); |
| 610 | } |
Wink Saville | 979203e | 2011-07-07 12:16:12 -0700 | [diff] [blame] | 611 | ifc_init_ifr(ifname, &ifr); |
| 612 | init_sockaddr_in(&ifr.ifr_addr, myaddr); |
| 613 | result = ioctl(ifc_ctl_sock, SIOCKILLADDR, &ifr); |
| 614 | ifc_close(); |
| 615 | } else { |
| 616 | result = 0; |
Lorenzo Colitti | 6cf73ea | 2011-03-17 11:20:25 -0700 | [diff] [blame] | 617 | } |
Wink Saville | 979203e | 2011-07-07 12:16:12 -0700 | [diff] [blame] | 618 | |
| 619 | if (reset_mask & RESET_IPV6_ADDRESSES) { |
| 620 | /* |
| 621 | * IPv6. On Linux, when an interface goes down it loses all its IPv6 |
| 622 | * addresses, so we don't know which connections belonged to that interface |
| 623 | * So we clear all unused IPv6 connections on the device by specifying an |
| 624 | * empty IPv6 address. |
| 625 | */ |
| 626 | ifc_init6(); |
| 627 | // This implicitly specifies an address of ::, i.e., kill all IPv6 sockets. |
| 628 | memset(&ifr6, 0, sizeof(ifr6)); |
| 629 | success = ioctl(ifc_ctl_sock6, SIOCKILLADDR, &ifr6); |
| 630 | if (result == 0) { |
| 631 | result = success; |
| 632 | } |
| 633 | ifc_close6(); |
| 634 | } |
Lorenzo Colitti | 6cf73ea | 2011-03-17 11:20:25 -0700 | [diff] [blame] | 635 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 636 | return result; |
| 637 | #else |
| 638 | return 0; |
| 639 | #endif |
| 640 | } |
| 641 | |
Elliott Hughes | c463025 | 2015-02-03 19:56:35 +0000 | [diff] [blame] | 642 | /* |
| 643 | * Removes the default route for the named interface. |
| 644 | */ |
| 645 | int ifc_remove_default_route(const char *ifname) |
| 646 | { |
| 647 | struct rtentry rt; |
| 648 | int result; |
| 649 | |
| 650 | ifc_init(); |
| 651 | memset(&rt, 0, sizeof(rt)); |
| 652 | rt.rt_dev = (void *)ifname; |
| 653 | rt.rt_flags = RTF_UP|RTF_GATEWAY; |
| 654 | init_sockaddr_in(&rt.rt_dst, 0); |
| 655 | if ((result = ioctl(ifc_ctl_sock, SIOCDELRT, &rt)) < 0) { |
| 656 | ALOGD("failed to remove default route for %s: %s", ifname, strerror(errno)); |
| 657 | } |
| 658 | ifc_close(); |
| 659 | return result; |
| 660 | } |
| 661 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 662 | int |
| 663 | ifc_configure(const char *ifname, |
| 664 | in_addr_t address, |
Robert Greenwalt | 09dd819 | 2011-02-01 15:21:21 -0800 | [diff] [blame] | 665 | uint32_t prefixLength, |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 666 | in_addr_t gateway, |
| 667 | in_addr_t dns1, |
| 668 | in_addr_t dns2) { |
| 669 | |
| 670 | char dns_prop_name[PROPERTY_KEY_MAX]; |
| 671 | |
| 672 | ifc_init(); |
| 673 | |
| 674 | if (ifc_up(ifname)) { |
| 675 | printerr("failed to turn on interface %s: %s\n", ifname, strerror(errno)); |
| 676 | ifc_close(); |
| 677 | return -1; |
| 678 | } |
| 679 | if (ifc_set_addr(ifname, address)) { |
| 680 | printerr("failed to set ipaddr %s: %s\n", ipaddr_to_string(address), strerror(errno)); |
| 681 | ifc_close(); |
| 682 | return -1; |
| 683 | } |
Robert Greenwalt | 09dd819 | 2011-02-01 15:21:21 -0800 | [diff] [blame] | 684 | if (ifc_set_prefixLength(ifname, prefixLength)) { |
| 685 | 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] | 686 | ifc_close(); |
| 687 | return -1; |
| 688 | } |
| 689 | if (ifc_create_default_route(ifname, gateway)) { |
| 690 | printerr("failed to set default route %s: %s\n", ipaddr_to_string(gateway), strerror(errno)); |
| 691 | ifc_close(); |
| 692 | return -1; |
| 693 | } |
| 694 | |
| 695 | ifc_close(); |
| 696 | |
Szymon Jakubczak | c88e09c | 2010-06-09 16:11:09 -0400 | [diff] [blame] | 697 | 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] | 698 | property_set(dns_prop_name, dns1 ? ipaddr_to_string(dns1) : ""); |
Szymon Jakubczak | c88e09c | 2010-06-09 16:11:09 -0400 | [diff] [blame] | 699 | 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] | 700 | property_set(dns_prop_name, dns2 ? ipaddr_to_string(dns2) : ""); |
| 701 | |
| 702 | return 0; |
| 703 | } |