blob: 4d004f6391d3849533f7d393fa89e5b3d2fa0988 [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001/*
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 Shmidt7d05a802011-01-24 17:10:30 -080028#include <net/if.h>
Lorenzo Colitti47ddb512011-09-26 10:49:41 -070029#include <netdb.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080030
31#include <linux/if.h>
Szymon Jakubczakc88e09c2010-06-09 16:11:09 -040032#include <linux/if_ether.h>
33#include <linux/if_arp.h>
Lorenzo Colitti47ddb512011-09-26 10:49:41 -070034#include <linux/netlink.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080035#include <linux/route.h>
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +053036#include <linux/ipv6_route.h>
Lorenzo Colitti47ddb512011-09-26 10:49:41 -070037#include <linux/rtnetlink.h>
38#include <linux/sockios.h>
39
40#include "netutils/ifc.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080041
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 Block8d66c492011-12-20 16:07:45 +000049#define ALOGD printf
Steve Blockae8b56c2012-01-05 22:25:38 +000050#define ALOGW printf
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080051#endif
52
Elliott Hughes2d640c22013-11-12 13:05:01 -080053#ifdef HAVE_ANDROID_OS
54/* SIOCKILLADDR is an Android extension. */
55#define SIOCKILLADDR 0x8939
56#endif
57
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080058static int ifc_ctl_sock = -1;
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +053059static int ifc_ctl_sock6 = -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080060void printerr(char *fmt, ...);
61
Robert Greenwaltb6b48ae2011-05-24 21:12:51 -070062#define DBG 0
Lorenzo Colitti47ddb512011-09-26 10:49:41 -070063#define INET_ADDRLEN 4
64#define INET6_ADDRLEN 16
Robert Greenwaltb6b48ae2011-05-24 21:12:51 -070065
Robert Greenwalt09dd8192011-02-01 15:21:21 -080066in_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
81int ipv4NetmaskToPrefixLength(in_addr_t mask)
82{
Robert Greenwalt09dd8192011-02-01 15:21:21 -080083 int prefixLength = 0;
Chris Dearman6ee3ecc2011-06-17 17:07:46 -070084 uint32_t m = (uint32_t)ntohl(mask);
Robert Greenwalt09dd8192011-02-01 15:21:21 -080085 while (m & 0x80000000) {
86 prefixLength++;
87 m = m << 1;
88 }
89 return prefixLength;
90}
91
Szymon Jakubczakc88e09c2010-06-09 16:11:09 -040092static const char *ipaddr_to_string(in_addr_t addr)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080093{
94 struct in_addr in_addr;
95
96 in_addr.s_addr = addr;
97 return inet_ntoa(in_addr);
98}
99
Lorenzo Colitti47ddb512011-09-26 10:49:41 -0700100int 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 Projectdd7bc332009-03-03 19:32:55 -0800122int ifc_init(void)
123{
Robert Greenwaltb6b48ae2011-05-24 21:12:51 -0700124 int ret;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800125 if (ifc_ctl_sock == -1) {
Robert Greenwaltb6b48ae2011-05-24 21:12:51 -0700126 ifc_ctl_sock = socket(AF_INET, SOCK_DGRAM, 0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800127 if (ifc_ctl_sock < 0) {
128 printerr("socket() failed: %s\n", strerror(errno));
129 }
130 }
Robert Greenwaltb6b48ae2011-05-24 21:12:51 -0700131
132 ret = ifc_ctl_sock < 0 ? -1 : 0;
133 if (DBG) printerr("ifc_init_returning %d", ret);
134 return ret;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800135}
136
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530137int ifc_init6(void)
138{
139 if (ifc_ctl_sock6 == -1) {
140 ifc_ctl_sock6 = socket(AF_INET6, SOCK_DGRAM, 0);
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 Projectdd7bc332009-03-03 19:32:55 -0800148void ifc_close(void)
149{
Robert Greenwaltb6b48ae2011-05-24 21:12:51 -0700150 if (DBG) printerr("ifc_close");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800151 if (ifc_ctl_sock != -1) {
152 (void)close(ifc_ctl_sock);
153 ifc_ctl_sock = -1;
154 }
155}
156
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530157void 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 Projectdd7bc332009-03-03 19:32:55 -0800165static 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
172int 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 Jakubczakc88e09c2010-06-09 16:11:09 -0400181 memcpy(ptr, &ifr.ifr_hwaddr.sa_data, ETH_ALEN);
Robert Greenwaltb6b48ae2011-05-24 21:12:51 -0700182 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800183}
184
185int 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 Shmidt7d05a802011-01-24 17:10:30 -0800195 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800196}
197
198static 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
208int ifc_up(const char *name)
209{
Robert Greenwaltb6b48ae2011-05-24 21:12:51 -0700210 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 Projectdd7bc332009-03-03 19:32:55 -0800213}
214
215int ifc_down(const char *name)
216{
Robert Greenwaltb6b48ae2011-05-24 21:12:51 -0700217 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 Projectdd7bc332009-03-03 19:32:55 -0800220}
221
222static 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
230int ifc_set_addr(const char *name, in_addr_t addr)
231{
232 struct ifreq ifr;
Robert Greenwaltb6b48ae2011-05-24 21:12:51 -0700233 int ret;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800234
235 ifc_init_ifr(name, &ifr);
236 init_sockaddr_in(&ifr.ifr_addr, addr);
Dmitry Shmidt7d05a802011-01-24 17:10:30 -0800237
Robert Greenwaltb6b48ae2011-05-24 21:12:51 -0700238 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 Projectdd7bc332009-03-03 19:32:55 -0800241}
242
Lorenzo Colitti47ddb512011-09-26 10:49:41 -0700243/*
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 */
252int 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
319 s = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
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
342int ifc_add_address(const char *name, const char *address, int prefixlen) {
343 return ifc_act_on_address(RTM_NEWADDR, name, address, prefixlen);
344}
345
346int 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 */
353int 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 Block01dda202012-01-06 14:13:42 +0000389 ALOGE("Deleting address %s/%d on %s: %s", addrstr, prefixlen, ifname,
Lorenzo Colitti47ddb512011-09-26 10:49:41 -0700390 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 */
402void 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 */
417int ifc_clear_addresses(const char *name) {
418 ifc_clear_ipv4_addresses(name);
419 return ifc_clear_ipv6_addresses(name);
420}
421
Szymon Jakubczakc88e09c2010-06-09 16:11:09 -0400422int ifc_set_hwaddr(const char *name, const void *ptr)
423{
424 int r;
425 struct ifreq ifr;
426 ifc_init_ifr(name, &ifr);
427
428 ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER;
429 memcpy(&ifr.ifr_hwaddr.sa_data, ptr, ETH_ALEN);
430 return ioctl(ifc_ctl_sock, SIOCSIFHWADDR, &ifr);
431}
432
Jake Hamby6f49d5f2011-04-11 19:46:41 -0700433int ifc_set_mask(const char *name, in_addr_t mask)
434{
435 struct ifreq ifr;
Robert Greenwaltb6b48ae2011-05-24 21:12:51 -0700436 int ret;
Jake Hamby6f49d5f2011-04-11 19:46:41 -0700437
438 ifc_init_ifr(name, &ifr);
439 init_sockaddr_in(&ifr.ifr_addr, mask);
440
Robert Greenwaltb6b48ae2011-05-24 21:12:51 -0700441 ret = ioctl(ifc_ctl_sock, SIOCSIFNETMASK, &ifr);
442 if (DBG) printerr("ifc_set_mask(%s, xx) = %d", name, ret);
443 return ret;
Jake Hamby6f49d5f2011-04-11 19:46:41 -0700444}
445
Robert Greenwalt09dd8192011-02-01 15:21:21 -0800446int ifc_set_prefixLength(const char *name, int prefixLength)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800447{
448 struct ifreq ifr;
Robert Greenwalt09dd8192011-02-01 15:21:21 -0800449 // TODO - support ipv6
450 if (prefixLength > 32 || prefixLength < 0) return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800451
Robert Greenwalt09dd8192011-02-01 15:21:21 -0800452 in_addr_t mask = prefixLengthToIpv4Netmask(prefixLength);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800453 ifc_init_ifr(name, &ifr);
454 init_sockaddr_in(&ifr.ifr_addr, mask);
Dmitry Shmidt7d05a802011-01-24 17:10:30 -0800455
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800456 return ioctl(ifc_ctl_sock, SIOCSIFNETMASK, &ifr);
457}
458
Dmitry Shmidt9092b912011-01-27 14:16:20 -0800459int ifc_get_addr(const char *name, in_addr_t *addr)
460{
461 struct ifreq ifr;
462 int ret = 0;
463
464 ifc_init_ifr(name, &ifr);
465 if (addr != NULL) {
466 ret = ioctl(ifc_ctl_sock, SIOCGIFADDR, &ifr);
467 if (ret < 0) {
468 *addr = 0;
469 } else {
470 *addr = ((struct sockaddr_in*) &ifr.ifr_addr)->sin_addr.s_addr;
471 }
472 }
473 return ret;
474}
475
Robert Greenwalt09dd8192011-02-01 15:21:21 -0800476int ifc_get_info(const char *name, in_addr_t *addr, int *prefixLength, unsigned *flags)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800477{
478 struct ifreq ifr;
479 ifc_init_ifr(name, &ifr);
480
481 if (addr != NULL) {
482 if(ioctl(ifc_ctl_sock, SIOCGIFADDR, &ifr) < 0) {
483 *addr = 0;
484 } else {
485 *addr = ((struct sockaddr_in*) &ifr.ifr_addr)->sin_addr.s_addr;
486 }
487 }
Dmitry Shmidt7d05a802011-01-24 17:10:30 -0800488
Robert Greenwalt09dd8192011-02-01 15:21:21 -0800489 if (prefixLength != NULL) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800490 if(ioctl(ifc_ctl_sock, SIOCGIFNETMASK, &ifr) < 0) {
Robert Greenwalt09dd8192011-02-01 15:21:21 -0800491 *prefixLength = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800492 } else {
Chris Dearman6ee3ecc2011-06-17 17:07:46 -0700493 *prefixLength = ipv4NetmaskToPrefixLength(
Robert Greenwalt09dd8192011-02-01 15:21:21 -0800494 ((struct sockaddr_in*) &ifr.ifr_addr)->sin_addr.s_addr);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800495 }
496 }
497
498 if (flags != NULL) {
499 if(ioctl(ifc_ctl_sock, SIOCGIFFLAGS, &ifr) < 0) {
500 *flags = 0;
501 } else {
502 *flags = ifr.ifr_flags;
503 }
504 }
505
506 return 0;
507}
508
Robert Greenwalt021d0a22011-05-10 14:59:20 -0700509int ifc_act_on_ipv4_route(int action, const char *ifname, struct in_addr dst, int prefix_length,
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530510 struct in_addr gw)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800511{
512 struct rtentry rt;
513 int result;
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530514 in_addr_t netmask;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800515
516 memset(&rt, 0, sizeof(rt));
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530517
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800518 rt.rt_dst.sa_family = AF_INET;
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530519 rt.rt_dev = (void*) ifname;
520
Robert Greenwalt09dd8192011-02-01 15:21:21 -0800521 netmask = prefixLengthToIpv4Netmask(prefix_length);
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530522 init_sockaddr_in(&rt.rt_genmask, netmask);
523 init_sockaddr_in(&rt.rt_dst, dst.s_addr);
524 rt.rt_flags = RTF_UP;
525
526 if (prefix_length == 32) {
527 rt.rt_flags |= RTF_HOST;
528 }
529
530 if (gw.s_addr != 0) {
531 rt.rt_flags |= RTF_GATEWAY;
532 init_sockaddr_in(&rt.rt_gateway, gw.s_addr);
533 }
534
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800535 ifc_init();
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530536
537 if (ifc_ctl_sock < 0) {
538 return -errno;
539 }
540
Robert Greenwalt021d0a22011-05-10 14:59:20 -0700541 result = ioctl(ifc_ctl_sock, action, &rt);
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530542 if (result < 0) {
543 if (errno == EEXIST) {
544 result = 0;
545 } else {
546 result = -errno;
547 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800548 }
549 ifc_close();
550 return result;
551}
552
Robert Greenwaltb6b48ae2011-05-24 21:12:51 -0700553/* deprecated - v4 only */
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530554int ifc_create_default_route(const char *name, in_addr_t gw)
555{
556 struct in_addr in_dst, in_gw;
557
558 in_dst.s_addr = 0;
559 in_gw.s_addr = gw;
560
Robert Greenwaltb6b48ae2011-05-24 21:12:51 -0700561 int ret = ifc_act_on_ipv4_route(SIOCADDRT, name, in_dst, 0, in_gw);
562 if (DBG) printerr("ifc_create_default_route(%s, %d) = %d", name, gw, ret);
563 return ret;
564}
565
566/* deprecated v4-only */
567int ifc_add_host_route(const char *name, in_addr_t dst)
568{
569 struct in_addr in_dst, in_gw;
570
571 in_dst.s_addr = dst;
572 in_gw.s_addr = 0;
573
574 return ifc_act_on_ipv4_route(SIOCADDRT, name, in_dst, 32, in_gw);
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530575}
576
Mike Lockwoodfeb63e92009-07-10 17:21:17 -0400577int ifc_enable(const char *ifname)
578{
579 int result;
580
581 ifc_init();
582 result = ifc_up(ifname);
583 ifc_close();
584 return result;
585}
586
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800587int ifc_disable(const char *ifname)
588{
Dmitry Shmidt9092b912011-01-27 14:16:20 -0800589 unsigned addr, count;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800590 int result;
591
592 ifc_init();
593 result = ifc_down(ifname);
Dmitry Shmidt9092b912011-01-27 14:16:20 -0800594
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800595 ifc_set_addr(ifname, 0);
Dmitry Shmidt9092b912011-01-27 14:16:20 -0800596 for (count=0, addr=1;((addr != 0) && (count < 255)); count++) {
597 if (ifc_get_addr(ifname, &addr) < 0)
598 break;
599 if (addr)
600 ifc_set_addr(ifname, 0);
601 }
602
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800603 ifc_close();
604 return result;
605}
606
Wink Saville979203e2011-07-07 12:16:12 -0700607int ifc_reset_connections(const char *ifname, const int reset_mask)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800608{
609#ifdef HAVE_ANDROID_OS
Lorenzo Colitti6cf73ea2011-03-17 11:20:25 -0700610 int result, success;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800611 in_addr_t myaddr;
612 struct ifreq ifr;
Lorenzo Colitti6cf73ea2011-03-17 11:20:25 -0700613 struct in6_ifreq ifr6;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800614
Wink Saville979203e2011-07-07 12:16:12 -0700615 if (reset_mask & RESET_IPV4_ADDRESSES) {
616 /* IPv4. Clear connections on the IP address. */
617 ifc_init();
618 ifc_get_info(ifname, &myaddr, NULL, NULL);
619 ifc_init_ifr(ifname, &ifr);
620 init_sockaddr_in(&ifr.ifr_addr, myaddr);
621 result = ioctl(ifc_ctl_sock, SIOCKILLADDR, &ifr);
622 ifc_close();
623 } else {
624 result = 0;
Lorenzo Colitti6cf73ea2011-03-17 11:20:25 -0700625 }
Wink Saville979203e2011-07-07 12:16:12 -0700626
627 if (reset_mask & RESET_IPV6_ADDRESSES) {
628 /*
629 * IPv6. On Linux, when an interface goes down it loses all its IPv6
630 * addresses, so we don't know which connections belonged to that interface
631 * So we clear all unused IPv6 connections on the device by specifying an
632 * empty IPv6 address.
633 */
634 ifc_init6();
635 // This implicitly specifies an address of ::, i.e., kill all IPv6 sockets.
636 memset(&ifr6, 0, sizeof(ifr6));
637 success = ioctl(ifc_ctl_sock6, SIOCKILLADDR, &ifr6);
638 if (result == 0) {
639 result = success;
640 }
641 ifc_close6();
642 }
Lorenzo Colitti6cf73ea2011-03-17 11:20:25 -0700643
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800644 return result;
645#else
646 return 0;
647#endif
648}
649
650/*
651 * Remove the routes associated with the named interface.
652 */
653int ifc_remove_host_routes(const char *name)
654{
655 char ifname[64];
656 in_addr_t dest, gway, mask;
657 int flags, refcnt, use, metric, mtu, win, irtt;
658 struct rtentry rt;
659 FILE *fp;
660 struct in_addr addr;
661
662 fp = fopen("/proc/net/route", "r");
663 if (fp == NULL)
664 return -1;
665 /* Skip the header line */
666 if (fscanf(fp, "%*[^\n]\n") < 0) {
667 fclose(fp);
668 return -1;
669 }
670 ifc_init();
671 for (;;) {
672 int nread = fscanf(fp, "%63s%X%X%X%d%d%d%X%d%d%d\n",
673 ifname, &dest, &gway, &flags, &refcnt, &use, &metric, &mask,
674 &mtu, &win, &irtt);
675 if (nread != 11) {
676 break;
677 }
678 if ((flags & (RTF_UP|RTF_HOST)) != (RTF_UP|RTF_HOST)
679 || strcmp(ifname, name) != 0) {
680 continue;
681 }
682 memset(&rt, 0, sizeof(rt));
683 rt.rt_dev = (void *)name;
684 init_sockaddr_in(&rt.rt_dst, dest);
685 init_sockaddr_in(&rt.rt_gateway, gway);
686 init_sockaddr_in(&rt.rt_genmask, mask);
687 addr.s_addr = dest;
688 if (ioctl(ifc_ctl_sock, SIOCDELRT, &rt) < 0) {
Steve Block8d66c492011-12-20 16:07:45 +0000689 ALOGD("failed to remove route for %s to %s: %s",
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800690 ifname, inet_ntoa(addr), strerror(errno));
691 }
692 }
693 fclose(fp);
694 ifc_close();
695 return 0;
696}
697
698/*
Robert Greenwaltb6b48ae2011-05-24 21:12:51 -0700699 * Return the address of the default gateway
700 *
701 * TODO: factor out common code from this and remove_host_routes()
702 * so that we only scan /proc/net/route in one place.
703 *
704 * DEPRECATED
705 */
706int ifc_get_default_route(const char *ifname)
707{
708 char name[64];
709 in_addr_t dest, gway, mask;
710 int flags, refcnt, use, metric, mtu, win, irtt;
711 int result;
712 FILE *fp;
713
714 fp = fopen("/proc/net/route", "r");
715 if (fp == NULL)
716 return 0;
717 /* Skip the header line */
718 if (fscanf(fp, "%*[^\n]\n") < 0) {
719 fclose(fp);
720 return 0;
721 }
722 ifc_init();
723 result = 0;
724 for (;;) {
725 int nread = fscanf(fp, "%63s%X%X%X%d%d%d%X%d%d%d\n",
726 name, &dest, &gway, &flags, &refcnt, &use, &metric, &mask,
727 &mtu, &win, &irtt);
728 if (nread != 11) {
729 break;
730 }
731 if ((flags & (RTF_UP|RTF_GATEWAY)) == (RTF_UP|RTF_GATEWAY)
732 && dest == 0
733 && strcmp(ifname, name) == 0) {
734 result = gway;
735 break;
736 }
737 }
738 fclose(fp);
739 ifc_close();
740 return result;
741}
742
743/*
744 * Sets the specified gateway as the default route for the named interface.
745 * DEPRECATED
746 */
747int ifc_set_default_route(const char *ifname, in_addr_t gateway)
748{
749 struct in_addr addr;
750 int result;
751
752 ifc_init();
753 addr.s_addr = gateway;
754 if ((result = ifc_create_default_route(ifname, gateway)) < 0) {
Steve Block8d66c492011-12-20 16:07:45 +0000755 ALOGD("failed to add %s as default route for %s: %s",
Robert Greenwaltb6b48ae2011-05-24 21:12:51 -0700756 inet_ntoa(addr), ifname, strerror(errno));
757 }
758 ifc_close();
759 return result;
760}
761
762/*
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800763 * Removes the default route for the named interface.
764 */
765int ifc_remove_default_route(const char *ifname)
766{
767 struct rtentry rt;
768 int result;
769
770 ifc_init();
771 memset(&rt, 0, sizeof(rt));
772 rt.rt_dev = (void *)ifname;
773 rt.rt_flags = RTF_UP|RTF_GATEWAY;
774 init_sockaddr_in(&rt.rt_dst, 0);
775 if ((result = ioctl(ifc_ctl_sock, SIOCDELRT, &rt)) < 0) {
Steve Block8d66c492011-12-20 16:07:45 +0000776 ALOGD("failed to remove default route for %s: %s", ifname, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800777 }
778 ifc_close();
779 return result;
780}
781
782int
783ifc_configure(const char *ifname,
784 in_addr_t address,
Robert Greenwalt09dd8192011-02-01 15:21:21 -0800785 uint32_t prefixLength,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800786 in_addr_t gateway,
787 in_addr_t dns1,
788 in_addr_t dns2) {
789
790 char dns_prop_name[PROPERTY_KEY_MAX];
791
792 ifc_init();
793
794 if (ifc_up(ifname)) {
795 printerr("failed to turn on interface %s: %s\n", ifname, strerror(errno));
796 ifc_close();
797 return -1;
798 }
799 if (ifc_set_addr(ifname, address)) {
800 printerr("failed to set ipaddr %s: %s\n", ipaddr_to_string(address), strerror(errno));
801 ifc_close();
802 return -1;
803 }
Robert Greenwalt09dd8192011-02-01 15:21:21 -0800804 if (ifc_set_prefixLength(ifname, prefixLength)) {
805 printerr("failed to set prefixLength %d: %s\n", prefixLength, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800806 ifc_close();
807 return -1;
808 }
809 if (ifc_create_default_route(ifname, gateway)) {
810 printerr("failed to set default route %s: %s\n", ipaddr_to_string(gateway), strerror(errno));
811 ifc_close();
812 return -1;
813 }
814
815 ifc_close();
816
Szymon Jakubczakc88e09c2010-06-09 16:11:09 -0400817 snprintf(dns_prop_name, sizeof(dns_prop_name), "net.%s.dns1", ifname);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800818 property_set(dns_prop_name, dns1 ? ipaddr_to_string(dns1) : "");
Szymon Jakubczakc88e09c2010-06-09 16:11:09 -0400819 snprintf(dns_prop_name, sizeof(dns_prop_name), "net.%s.dns2", ifname);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800820 property_set(dns_prop_name, dns2 ? ipaddr_to_string(dns2) : "");
821
822 return 0;
823}
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530824
Robert Greenwalt021d0a22011-05-10 14:59:20 -0700825int ifc_act_on_ipv6_route(int action, const char *ifname, struct in6_addr dst, int prefix_length,
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530826 struct in6_addr gw)
827{
828 struct in6_rtmsg rtmsg;
829 int result;
830 int ifindex;
831
832 memset(&rtmsg, 0, sizeof(rtmsg));
833
834 ifindex = if_nametoindex(ifname);
835 if (ifindex == 0) {
836 printerr("if_nametoindex() failed: interface %s\n", ifname);
837 return -ENXIO;
838 }
839
840 rtmsg.rtmsg_ifindex = ifindex;
841 rtmsg.rtmsg_dst = dst;
842 rtmsg.rtmsg_dst_len = prefix_length;
843 rtmsg.rtmsg_flags = RTF_UP;
844
845 if (prefix_length == 128) {
846 rtmsg.rtmsg_flags |= RTF_HOST;
847 }
848
849 if (memcmp(&gw, &in6addr_any, sizeof(in6addr_any))) {
850 rtmsg.rtmsg_flags |= RTF_GATEWAY;
851 rtmsg.rtmsg_gateway = gw;
852 }
853
854 ifc_init6();
855
856 if (ifc_ctl_sock6 < 0) {
857 return -errno;
858 }
859
Robert Greenwalt021d0a22011-05-10 14:59:20 -0700860 result = ioctl(ifc_ctl_sock6, action, &rtmsg);
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530861 if (result < 0) {
862 if (errno == EEXIST) {
863 result = 0;
864 } else {
865 result = -errno;
866 }
867 }
868 ifc_close6();
869 return result;
870}
871
Robert Greenwalt021d0a22011-05-10 14:59:20 -0700872int ifc_act_on_route(int action, const char *ifname, const char *dst, int prefix_length,
873 const char *gw)
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530874{
875 int ret = 0;
876 struct sockaddr_in ipv4_dst, ipv4_gw;
877 struct sockaddr_in6 ipv6_dst, ipv6_gw;
878 struct addrinfo hints, *addr_ai, *gw_ai;
879
880 memset(&hints, 0, sizeof(hints));
881 hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */
882 hints.ai_flags = AI_NUMERICHOST;
883
884 ret = getaddrinfo(dst, NULL, &hints, &addr_ai);
885
886 if (ret != 0) {
887 printerr("getaddrinfo failed: invalid address %s\n", dst);
888 return -EINVAL;
889 }
890
Robert Greenwalt021d0a22011-05-10 14:59:20 -0700891 if (gw == NULL || (strlen(gw) == 0)) {
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530892 if (addr_ai->ai_family == AF_INET6) {
893 gw = "::";
894 } else if (addr_ai->ai_family == AF_INET) {
895 gw = "0.0.0.0";
896 }
897 }
898
Robert Greenwalt021d0a22011-05-10 14:59:20 -0700899 if (((addr_ai->ai_family == AF_INET6) && (prefix_length < 0 || prefix_length > 128)) ||
900 ((addr_ai->ai_family == AF_INET) && (prefix_length < 0 || prefix_length > 32))) {
901 printerr("ifc_add_route: invalid prefix length");
902 freeaddrinfo(addr_ai);
903 return -EINVAL;
904 }
905
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530906 ret = getaddrinfo(gw, NULL, &hints, &gw_ai);
907 if (ret != 0) {
908 printerr("getaddrinfo failed: invalid gateway %s\n", gw);
909 freeaddrinfo(addr_ai);
910 return -EINVAL;
911 }
912
913 if (addr_ai->ai_family != gw_ai->ai_family) {
914 printerr("ifc_add_route: different address families: %s and %s\n", dst, gw);
915 freeaddrinfo(addr_ai);
916 freeaddrinfo(gw_ai);
917 return -EINVAL;
918 }
919
920 if (addr_ai->ai_family == AF_INET6) {
921 memcpy(&ipv6_dst, addr_ai->ai_addr, sizeof(struct sockaddr_in6));
922 memcpy(&ipv6_gw, gw_ai->ai_addr, sizeof(struct sockaddr_in6));
Robert Greenwalt021d0a22011-05-10 14:59:20 -0700923 ret = ifc_act_on_ipv6_route(action, ifname, ipv6_dst.sin6_addr,
924 prefix_length, ipv6_gw.sin6_addr);
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530925 } else if (addr_ai->ai_family == AF_INET) {
926 memcpy(&ipv4_dst, addr_ai->ai_addr, sizeof(struct sockaddr_in));
927 memcpy(&ipv4_gw, gw_ai->ai_addr, sizeof(struct sockaddr_in));
Robert Greenwalt021d0a22011-05-10 14:59:20 -0700928 ret = ifc_act_on_ipv4_route(action, ifname, ipv4_dst.sin_addr,
929 prefix_length, ipv4_gw.sin_addr);
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530930 } else {
931 printerr("ifc_add_route: getaddrinfo returned un supported address family %d\n",
932 addr_ai->ai_family);
933 ret = -EAFNOSUPPORT;
934 }
935
936 freeaddrinfo(addr_ai);
937 freeaddrinfo(gw_ai);
938 return ret;
939}
Robert Greenwalt021d0a22011-05-10 14:59:20 -0700940
Robert Greenwaltb6b48ae2011-05-24 21:12:51 -0700941/*
942 * DEPRECATED
943 */
944int ifc_add_ipv4_route(const char *ifname, struct in_addr dst, int prefix_length,
945 struct in_addr gw)
946{
947 int i =ifc_act_on_ipv4_route(SIOCADDRT, ifname, dst, prefix_length, gw);
Wink Saville0c613612011-09-20 15:53:23 -0700948 if (DBG) printerr("ifc_add_ipv4_route(%s, xx, %d, xx) = %d", ifname, prefix_length, i);
Robert Greenwaltb6b48ae2011-05-24 21:12:51 -0700949 return i;
950}
951
952/*
953 * DEPRECATED
954 */
955int ifc_add_ipv6_route(const char *ifname, struct in6_addr dst, int prefix_length,
956 struct in6_addr gw)
957{
958 return ifc_act_on_ipv6_route(SIOCADDRT, ifname, dst, prefix_length, gw);
959}
960
Robert Greenwalt021d0a22011-05-10 14:59:20 -0700961int ifc_add_route(const char *ifname, const char *dst, int prefix_length, const char *gw)
962{
Robert Greenwaltb6b48ae2011-05-24 21:12:51 -0700963 int i = ifc_act_on_route(SIOCADDRT, ifname, dst, prefix_length, gw);
Wink Saville0c613612011-09-20 15:53:23 -0700964 if (DBG) printerr("ifc_add_route(%s, %s, %d, %s) = %d", ifname, dst, prefix_length, gw, i);
Robert Greenwaltb6b48ae2011-05-24 21:12:51 -0700965 return i;
Robert Greenwalt021d0a22011-05-10 14:59:20 -0700966}
967
968int ifc_remove_route(const char *ifname, const char*dst, int prefix_length, const char *gw)
969{
970 return ifc_act_on_route(SIOCDELRT, ifname, dst, prefix_length, gw);
971}