blob: c482736de3eddab1fbd06fcb7978903f67f1b6d5 [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>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080029
30#include <linux/if.h>
Szymon Jakubczakc88e09c2010-06-09 16:11:09 -040031#include <linux/if_ether.h>
32#include <linux/if_arp.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080033#include <linux/sockios.h>
34#include <linux/route.h>
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +053035#include <linux/ipv6_route.h>
36#include <netdb.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080037#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
50static int ifc_ctl_sock = -1;
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +053051static int ifc_ctl_sock6 = -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080052void printerr(char *fmt, ...);
53
Robert Greenwaltb6b48ae2011-05-24 21:12:51 -070054#define DBG 0
55
Robert Greenwalt09dd8192011-02-01 15:21:21 -080056in_addr_t prefixLengthToIpv4Netmask(int prefix_length)
57{
58 in_addr_t mask = 0;
59
60 // C99 (6.5.7): shifts of 32 bits have undefined results
61 if (prefix_length <= 0 || prefix_length > 32) {
62 return 0;
63 }
64
65 mask = ~mask << (32 - prefix_length);
66 mask = htonl(mask);
67
68 return mask;
69}
70
71int ipv4NetmaskToPrefixLength(in_addr_t mask)
72{
73 mask = ntohl(mask);
74 int prefixLength = 0;
75 uint32_t m = (uint32_t)mask;
76 while (m & 0x80000000) {
77 prefixLength++;
78 m = m << 1;
79 }
80 return prefixLength;
81}
82
Szymon Jakubczakc88e09c2010-06-09 16:11:09 -040083static const char *ipaddr_to_string(in_addr_t addr)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080084{
85 struct in_addr in_addr;
86
87 in_addr.s_addr = addr;
88 return inet_ntoa(in_addr);
89}
90
91int ifc_init(void)
92{
Robert Greenwaltb6b48ae2011-05-24 21:12:51 -070093 int ret;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080094 if (ifc_ctl_sock == -1) {
Robert Greenwaltb6b48ae2011-05-24 21:12:51 -070095 ifc_ctl_sock = socket(AF_INET, SOCK_DGRAM, 0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080096 if (ifc_ctl_sock < 0) {
97 printerr("socket() failed: %s\n", strerror(errno));
98 }
99 }
Robert Greenwaltb6b48ae2011-05-24 21:12:51 -0700100
101 ret = ifc_ctl_sock < 0 ? -1 : 0;
102 if (DBG) printerr("ifc_init_returning %d", ret);
103 return ret;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800104}
105
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530106int ifc_init6(void)
107{
108 if (ifc_ctl_sock6 == -1) {
109 ifc_ctl_sock6 = socket(AF_INET6, SOCK_DGRAM, 0);
110 if (ifc_ctl_sock6 < 0) {
111 printerr("socket() failed: %s\n", strerror(errno));
112 }
113 }
114 return ifc_ctl_sock6 < 0 ? -1 : 0;
115}
116
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800117void ifc_close(void)
118{
Robert Greenwaltb6b48ae2011-05-24 21:12:51 -0700119 if (DBG) printerr("ifc_close");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800120 if (ifc_ctl_sock != -1) {
121 (void)close(ifc_ctl_sock);
122 ifc_ctl_sock = -1;
123 }
124}
125
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530126void ifc_close6(void)
127{
128 if (ifc_ctl_sock6 != -1) {
129 (void)close(ifc_ctl_sock6);
130 ifc_ctl_sock6 = -1;
131 }
132}
133
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800134static void ifc_init_ifr(const char *name, struct ifreq *ifr)
135{
136 memset(ifr, 0, sizeof(struct ifreq));
137 strncpy(ifr->ifr_name, name, IFNAMSIZ);
138 ifr->ifr_name[IFNAMSIZ - 1] = 0;
139}
140
141int ifc_get_hwaddr(const char *name, void *ptr)
142{
143 int r;
144 struct ifreq ifr;
145 ifc_init_ifr(name, &ifr);
146
147 r = ioctl(ifc_ctl_sock, SIOCGIFHWADDR, &ifr);
148 if(r < 0) return -1;
149
Szymon Jakubczakc88e09c2010-06-09 16:11:09 -0400150 memcpy(ptr, &ifr.ifr_hwaddr.sa_data, ETH_ALEN);
Robert Greenwaltb6b48ae2011-05-24 21:12:51 -0700151 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800152}
153
154int ifc_get_ifindex(const char *name, int *if_indexp)
155{
156 int r;
157 struct ifreq ifr;
158 ifc_init_ifr(name, &ifr);
159
160 r = ioctl(ifc_ctl_sock, SIOCGIFINDEX, &ifr);
161 if(r < 0) return -1;
162
163 *if_indexp = ifr.ifr_ifindex;
Dmitry Shmidt7d05a802011-01-24 17:10:30 -0800164 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800165}
166
167static int ifc_set_flags(const char *name, unsigned set, unsigned clr)
168{
169 struct ifreq ifr;
170 ifc_init_ifr(name, &ifr);
171
172 if(ioctl(ifc_ctl_sock, SIOCGIFFLAGS, &ifr) < 0) return -1;
173 ifr.ifr_flags = (ifr.ifr_flags & (~clr)) | set;
174 return ioctl(ifc_ctl_sock, SIOCSIFFLAGS, &ifr);
175}
176
177int ifc_up(const char *name)
178{
Robert Greenwaltb6b48ae2011-05-24 21:12:51 -0700179 int ret = ifc_set_flags(name, IFF_UP, 0);
180 if (DBG) printerr("ifc_up(%s) = %d", name, ret);
181 return ret;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800182}
183
184int ifc_down(const char *name)
185{
Robert Greenwaltb6b48ae2011-05-24 21:12:51 -0700186 int ret = ifc_set_flags(name, 0, IFF_UP);
187 if (DBG) printerr("ifc_down(%s) = %d", name, ret);
188 return ret;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800189}
190
191static void init_sockaddr_in(struct sockaddr *sa, in_addr_t addr)
192{
193 struct sockaddr_in *sin = (struct sockaddr_in *) sa;
194 sin->sin_family = AF_INET;
195 sin->sin_port = 0;
196 sin->sin_addr.s_addr = addr;
197}
198
199int ifc_set_addr(const char *name, in_addr_t addr)
200{
201 struct ifreq ifr;
Robert Greenwaltb6b48ae2011-05-24 21:12:51 -0700202 int ret;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800203
204 ifc_init_ifr(name, &ifr);
205 init_sockaddr_in(&ifr.ifr_addr, addr);
Dmitry Shmidt7d05a802011-01-24 17:10:30 -0800206
Robert Greenwaltb6b48ae2011-05-24 21:12:51 -0700207 ret = ioctl(ifc_ctl_sock, SIOCSIFADDR, &ifr);
208 if (DBG) printerr("ifc_set_addr(%s, xx) = %d", name, ret);
209 return ret;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800210}
211
Szymon Jakubczakc88e09c2010-06-09 16:11:09 -0400212int ifc_set_hwaddr(const char *name, const void *ptr)
213{
214 int r;
215 struct ifreq ifr;
216 ifc_init_ifr(name, &ifr);
217
218 ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER;
219 memcpy(&ifr.ifr_hwaddr.sa_data, ptr, ETH_ALEN);
220 return ioctl(ifc_ctl_sock, SIOCSIFHWADDR, &ifr);
221}
222
Jake Hamby6f49d5f2011-04-11 19:46:41 -0700223int ifc_set_mask(const char *name, in_addr_t mask)
224{
225 struct ifreq ifr;
Robert Greenwaltb6b48ae2011-05-24 21:12:51 -0700226 int ret;
Jake Hamby6f49d5f2011-04-11 19:46:41 -0700227
228 ifc_init_ifr(name, &ifr);
229 init_sockaddr_in(&ifr.ifr_addr, mask);
230
Robert Greenwaltb6b48ae2011-05-24 21:12:51 -0700231 ret = ioctl(ifc_ctl_sock, SIOCSIFNETMASK, &ifr);
232 if (DBG) printerr("ifc_set_mask(%s, xx) = %d", name, ret);
233 return ret;
Jake Hamby6f49d5f2011-04-11 19:46:41 -0700234}
235
Robert Greenwalt09dd8192011-02-01 15:21:21 -0800236int ifc_set_prefixLength(const char *name, int prefixLength)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800237{
238 struct ifreq ifr;
Robert Greenwalt09dd8192011-02-01 15:21:21 -0800239 // TODO - support ipv6
240 if (prefixLength > 32 || prefixLength < 0) return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800241
Robert Greenwalt09dd8192011-02-01 15:21:21 -0800242 in_addr_t mask = prefixLengthToIpv4Netmask(prefixLength);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800243 ifc_init_ifr(name, &ifr);
244 init_sockaddr_in(&ifr.ifr_addr, mask);
Dmitry Shmidt7d05a802011-01-24 17:10:30 -0800245
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800246 return ioctl(ifc_ctl_sock, SIOCSIFNETMASK, &ifr);
247}
248
Dmitry Shmidt9092b912011-01-27 14:16:20 -0800249int ifc_get_addr(const char *name, in_addr_t *addr)
250{
251 struct ifreq ifr;
252 int ret = 0;
253
254 ifc_init_ifr(name, &ifr);
255 if (addr != NULL) {
256 ret = ioctl(ifc_ctl_sock, SIOCGIFADDR, &ifr);
257 if (ret < 0) {
258 *addr = 0;
259 } else {
260 *addr = ((struct sockaddr_in*) &ifr.ifr_addr)->sin_addr.s_addr;
261 }
262 }
263 return ret;
264}
265
Robert Greenwalt09dd8192011-02-01 15:21:21 -0800266int ifc_get_info(const char *name, in_addr_t *addr, int *prefixLength, unsigned *flags)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800267{
268 struct ifreq ifr;
269 ifc_init_ifr(name, &ifr);
270
271 if (addr != NULL) {
272 if(ioctl(ifc_ctl_sock, SIOCGIFADDR, &ifr) < 0) {
273 *addr = 0;
274 } else {
275 *addr = ((struct sockaddr_in*) &ifr.ifr_addr)->sin_addr.s_addr;
276 }
277 }
Dmitry Shmidt7d05a802011-01-24 17:10:30 -0800278
Robert Greenwalt09dd8192011-02-01 15:21:21 -0800279 if (prefixLength != NULL) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800280 if(ioctl(ifc_ctl_sock, SIOCGIFNETMASK, &ifr) < 0) {
Robert Greenwalt09dd8192011-02-01 15:21:21 -0800281 *prefixLength = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800282 } else {
Robert Greenwalt09dd8192011-02-01 15:21:21 -0800283 *prefixLength = ipv4NetmaskToPrefixLength((int)
284 ((struct sockaddr_in*) &ifr.ifr_addr)->sin_addr.s_addr);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800285 }
286 }
287
288 if (flags != NULL) {
289 if(ioctl(ifc_ctl_sock, SIOCGIFFLAGS, &ifr) < 0) {
290 *flags = 0;
291 } else {
292 *flags = ifr.ifr_flags;
293 }
294 }
295
296 return 0;
297}
298
Robert Greenwalt021d0a22011-05-10 14:59:20 -0700299int 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 +0530300 struct in_addr gw)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800301{
302 struct rtentry rt;
303 int result;
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530304 in_addr_t netmask;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800305
306 memset(&rt, 0, sizeof(rt));
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530307
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800308 rt.rt_dst.sa_family = AF_INET;
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530309 rt.rt_dev = (void*) ifname;
310
Robert Greenwalt09dd8192011-02-01 15:21:21 -0800311 netmask = prefixLengthToIpv4Netmask(prefix_length);
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530312 init_sockaddr_in(&rt.rt_genmask, netmask);
313 init_sockaddr_in(&rt.rt_dst, dst.s_addr);
314 rt.rt_flags = RTF_UP;
315
316 if (prefix_length == 32) {
317 rt.rt_flags |= RTF_HOST;
318 }
319
320 if (gw.s_addr != 0) {
321 rt.rt_flags |= RTF_GATEWAY;
322 init_sockaddr_in(&rt.rt_gateway, gw.s_addr);
323 }
324
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800325 ifc_init();
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530326
327 if (ifc_ctl_sock < 0) {
328 return -errno;
329 }
330
Robert Greenwalt021d0a22011-05-10 14:59:20 -0700331 result = ioctl(ifc_ctl_sock, action, &rt);
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530332 if (result < 0) {
333 if (errno == EEXIST) {
334 result = 0;
335 } else {
336 result = -errno;
337 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800338 }
339 ifc_close();
340 return result;
341}
342
Robert Greenwaltb6b48ae2011-05-24 21:12:51 -0700343/* deprecated - v4 only */
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530344int ifc_create_default_route(const char *name, in_addr_t gw)
345{
346 struct in_addr in_dst, in_gw;
347
348 in_dst.s_addr = 0;
349 in_gw.s_addr = gw;
350
Robert Greenwaltb6b48ae2011-05-24 21:12:51 -0700351 int ret = ifc_act_on_ipv4_route(SIOCADDRT, name, in_dst, 0, in_gw);
352 if (DBG) printerr("ifc_create_default_route(%s, %d) = %d", name, gw, ret);
353 return ret;
354}
355
356/* deprecated v4-only */
357int ifc_add_host_route(const char *name, in_addr_t dst)
358{
359 struct in_addr in_dst, in_gw;
360
361 in_dst.s_addr = dst;
362 in_gw.s_addr = 0;
363
364 return ifc_act_on_ipv4_route(SIOCADDRT, name, in_dst, 32, in_gw);
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530365}
366
Mike Lockwoodfeb63e92009-07-10 17:21:17 -0400367int ifc_enable(const char *ifname)
368{
369 int result;
370
371 ifc_init();
372 result = ifc_up(ifname);
373 ifc_close();
374 return result;
375}
376
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800377int ifc_disable(const char *ifname)
378{
Dmitry Shmidt9092b912011-01-27 14:16:20 -0800379 unsigned addr, count;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800380 int result;
381
382 ifc_init();
383 result = ifc_down(ifname);
Dmitry Shmidt9092b912011-01-27 14:16:20 -0800384
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800385 ifc_set_addr(ifname, 0);
Dmitry Shmidt9092b912011-01-27 14:16:20 -0800386 for (count=0, addr=1;((addr != 0) && (count < 255)); count++) {
387 if (ifc_get_addr(ifname, &addr) < 0)
388 break;
389 if (addr)
390 ifc_set_addr(ifname, 0);
391 }
392
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800393 ifc_close();
394 return result;
395}
396
Wink Saville979203e2011-07-07 12:16:12 -0700397#define RESET_IPV4_ADDRESSES 0x01
398#define RESET_IPV6_ADDRESSES 0x02
399#define RESET_ALL_ADDRESSES (RESET_IPV4_ADDRESSES | RESET_IPV6_ADDRESSES)
400
401int ifc_reset_connections(const char *ifname, const int reset_mask)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800402{
403#ifdef HAVE_ANDROID_OS
Lorenzo Colitti6cf73ea2011-03-17 11:20:25 -0700404 int result, success;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800405 in_addr_t myaddr;
406 struct ifreq ifr;
Lorenzo Colitti6cf73ea2011-03-17 11:20:25 -0700407 struct in6_ifreq ifr6;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800408
Wink Saville979203e2011-07-07 12:16:12 -0700409 if (reset_mask & RESET_IPV4_ADDRESSES) {
410 /* IPv4. Clear connections on the IP address. */
411 ifc_init();
412 ifc_get_info(ifname, &myaddr, NULL, NULL);
413 ifc_init_ifr(ifname, &ifr);
414 init_sockaddr_in(&ifr.ifr_addr, myaddr);
415 result = ioctl(ifc_ctl_sock, SIOCKILLADDR, &ifr);
416 ifc_close();
417 } else {
418 result = 0;
Lorenzo Colitti6cf73ea2011-03-17 11:20:25 -0700419 }
Wink Saville979203e2011-07-07 12:16:12 -0700420
421 if (reset_mask & RESET_IPV6_ADDRESSES) {
422 /*
423 * IPv6. On Linux, when an interface goes down it loses all its IPv6
424 * addresses, so we don't know which connections belonged to that interface
425 * So we clear all unused IPv6 connections on the device by specifying an
426 * empty IPv6 address.
427 */
428 ifc_init6();
429 // This implicitly specifies an address of ::, i.e., kill all IPv6 sockets.
430 memset(&ifr6, 0, sizeof(ifr6));
431 success = ioctl(ifc_ctl_sock6, SIOCKILLADDR, &ifr6);
432 if (result == 0) {
433 result = success;
434 }
435 ifc_close6();
436 }
Lorenzo Colitti6cf73ea2011-03-17 11:20:25 -0700437
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800438 return result;
439#else
440 return 0;
441#endif
442}
443
444/*
445 * Remove the routes associated with the named interface.
446 */
447int ifc_remove_host_routes(const char *name)
448{
449 char ifname[64];
450 in_addr_t dest, gway, mask;
451 int flags, refcnt, use, metric, mtu, win, irtt;
452 struct rtentry rt;
453 FILE *fp;
454 struct in_addr addr;
455
456 fp = fopen("/proc/net/route", "r");
457 if (fp == NULL)
458 return -1;
459 /* Skip the header line */
460 if (fscanf(fp, "%*[^\n]\n") < 0) {
461 fclose(fp);
462 return -1;
463 }
464 ifc_init();
465 for (;;) {
466 int nread = fscanf(fp, "%63s%X%X%X%d%d%d%X%d%d%d\n",
467 ifname, &dest, &gway, &flags, &refcnt, &use, &metric, &mask,
468 &mtu, &win, &irtt);
469 if (nread != 11) {
470 break;
471 }
472 if ((flags & (RTF_UP|RTF_HOST)) != (RTF_UP|RTF_HOST)
473 || strcmp(ifname, name) != 0) {
474 continue;
475 }
476 memset(&rt, 0, sizeof(rt));
477 rt.rt_dev = (void *)name;
478 init_sockaddr_in(&rt.rt_dst, dest);
479 init_sockaddr_in(&rt.rt_gateway, gway);
480 init_sockaddr_in(&rt.rt_genmask, mask);
481 addr.s_addr = dest;
482 if (ioctl(ifc_ctl_sock, SIOCDELRT, &rt) < 0) {
483 LOGD("failed to remove route for %s to %s: %s",
484 ifname, inet_ntoa(addr), strerror(errno));
485 }
486 }
487 fclose(fp);
488 ifc_close();
489 return 0;
490}
491
492/*
Robert Greenwaltb6b48ae2011-05-24 21:12:51 -0700493 * Return the address of the default gateway
494 *
495 * TODO: factor out common code from this and remove_host_routes()
496 * so that we only scan /proc/net/route in one place.
497 *
498 * DEPRECATED
499 */
500int ifc_get_default_route(const char *ifname)
501{
502 char name[64];
503 in_addr_t dest, gway, mask;
504 int flags, refcnt, use, metric, mtu, win, irtt;
505 int result;
506 FILE *fp;
507
508 fp = fopen("/proc/net/route", "r");
509 if (fp == NULL)
510 return 0;
511 /* Skip the header line */
512 if (fscanf(fp, "%*[^\n]\n") < 0) {
513 fclose(fp);
514 return 0;
515 }
516 ifc_init();
517 result = 0;
518 for (;;) {
519 int nread = fscanf(fp, "%63s%X%X%X%d%d%d%X%d%d%d\n",
520 name, &dest, &gway, &flags, &refcnt, &use, &metric, &mask,
521 &mtu, &win, &irtt);
522 if (nread != 11) {
523 break;
524 }
525 if ((flags & (RTF_UP|RTF_GATEWAY)) == (RTF_UP|RTF_GATEWAY)
526 && dest == 0
527 && strcmp(ifname, name) == 0) {
528 result = gway;
529 break;
530 }
531 }
532 fclose(fp);
533 ifc_close();
534 return result;
535}
536
537/*
538 * Sets the specified gateway as the default route for the named interface.
539 * DEPRECATED
540 */
541int ifc_set_default_route(const char *ifname, in_addr_t gateway)
542{
543 struct in_addr addr;
544 int result;
545
546 ifc_init();
547 addr.s_addr = gateway;
548 if ((result = ifc_create_default_route(ifname, gateway)) < 0) {
549 LOGD("failed to add %s as default route for %s: %s",
550 inet_ntoa(addr), ifname, strerror(errno));
551 }
552 ifc_close();
553 return result;
554}
555
556/*
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800557 * Removes the default route for the named interface.
558 */
559int ifc_remove_default_route(const char *ifname)
560{
561 struct rtentry rt;
562 int result;
563
564 ifc_init();
565 memset(&rt, 0, sizeof(rt));
566 rt.rt_dev = (void *)ifname;
567 rt.rt_flags = RTF_UP|RTF_GATEWAY;
568 init_sockaddr_in(&rt.rt_dst, 0);
569 if ((result = ioctl(ifc_ctl_sock, SIOCDELRT, &rt)) < 0) {
570 LOGD("failed to remove default route for %s: %s", ifname, strerror(errno));
571 }
572 ifc_close();
573 return result;
574}
575
576int
577ifc_configure(const char *ifname,
578 in_addr_t address,
Robert Greenwalt09dd8192011-02-01 15:21:21 -0800579 uint32_t prefixLength,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800580 in_addr_t gateway,
581 in_addr_t dns1,
582 in_addr_t dns2) {
583
584 char dns_prop_name[PROPERTY_KEY_MAX];
585
586 ifc_init();
587
588 if (ifc_up(ifname)) {
589 printerr("failed to turn on interface %s: %s\n", ifname, strerror(errno));
590 ifc_close();
591 return -1;
592 }
593 if (ifc_set_addr(ifname, address)) {
594 printerr("failed to set ipaddr %s: %s\n", ipaddr_to_string(address), strerror(errno));
595 ifc_close();
596 return -1;
597 }
Robert Greenwalt09dd8192011-02-01 15:21:21 -0800598 if (ifc_set_prefixLength(ifname, prefixLength)) {
599 printerr("failed to set prefixLength %d: %s\n", prefixLength, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800600 ifc_close();
601 return -1;
602 }
603 if (ifc_create_default_route(ifname, gateway)) {
604 printerr("failed to set default route %s: %s\n", ipaddr_to_string(gateway), strerror(errno));
605 ifc_close();
606 return -1;
607 }
608
609 ifc_close();
610
Szymon Jakubczakc88e09c2010-06-09 16:11:09 -0400611 snprintf(dns_prop_name, sizeof(dns_prop_name), "net.%s.dns1", ifname);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800612 property_set(dns_prop_name, dns1 ? ipaddr_to_string(dns1) : "");
Szymon Jakubczakc88e09c2010-06-09 16:11:09 -0400613 snprintf(dns_prop_name, sizeof(dns_prop_name), "net.%s.dns2", ifname);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800614 property_set(dns_prop_name, dns2 ? ipaddr_to_string(dns2) : "");
615
616 return 0;
617}
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530618
Robert Greenwalt021d0a22011-05-10 14:59:20 -0700619int 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 +0530620 struct in6_addr gw)
621{
622 struct in6_rtmsg rtmsg;
623 int result;
624 int ifindex;
625
626 memset(&rtmsg, 0, sizeof(rtmsg));
627
628 ifindex = if_nametoindex(ifname);
629 if (ifindex == 0) {
630 printerr("if_nametoindex() failed: interface %s\n", ifname);
631 return -ENXIO;
632 }
633
634 rtmsg.rtmsg_ifindex = ifindex;
635 rtmsg.rtmsg_dst = dst;
636 rtmsg.rtmsg_dst_len = prefix_length;
637 rtmsg.rtmsg_flags = RTF_UP;
638
639 if (prefix_length == 128) {
640 rtmsg.rtmsg_flags |= RTF_HOST;
641 }
642
643 if (memcmp(&gw, &in6addr_any, sizeof(in6addr_any))) {
644 rtmsg.rtmsg_flags |= RTF_GATEWAY;
645 rtmsg.rtmsg_gateway = gw;
646 }
647
648 ifc_init6();
649
650 if (ifc_ctl_sock6 < 0) {
651 return -errno;
652 }
653
Robert Greenwalt021d0a22011-05-10 14:59:20 -0700654 result = ioctl(ifc_ctl_sock6, action, &rtmsg);
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530655 if (result < 0) {
656 if (errno == EEXIST) {
657 result = 0;
658 } else {
659 result = -errno;
660 }
661 }
662 ifc_close6();
663 return result;
664}
665
Robert Greenwalt021d0a22011-05-10 14:59:20 -0700666int ifc_act_on_route(int action, const char *ifname, const char *dst, int prefix_length,
667 const char *gw)
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530668{
669 int ret = 0;
670 struct sockaddr_in ipv4_dst, ipv4_gw;
671 struct sockaddr_in6 ipv6_dst, ipv6_gw;
672 struct addrinfo hints, *addr_ai, *gw_ai;
673
674 memset(&hints, 0, sizeof(hints));
675 hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */
676 hints.ai_flags = AI_NUMERICHOST;
677
678 ret = getaddrinfo(dst, NULL, &hints, &addr_ai);
679
680 if (ret != 0) {
681 printerr("getaddrinfo failed: invalid address %s\n", dst);
682 return -EINVAL;
683 }
684
Robert Greenwalt021d0a22011-05-10 14:59:20 -0700685 if (gw == NULL || (strlen(gw) == 0)) {
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530686 if (addr_ai->ai_family == AF_INET6) {
687 gw = "::";
688 } else if (addr_ai->ai_family == AF_INET) {
689 gw = "0.0.0.0";
690 }
691 }
692
Robert Greenwalt021d0a22011-05-10 14:59:20 -0700693 if (((addr_ai->ai_family == AF_INET6) && (prefix_length < 0 || prefix_length > 128)) ||
694 ((addr_ai->ai_family == AF_INET) && (prefix_length < 0 || prefix_length > 32))) {
695 printerr("ifc_add_route: invalid prefix length");
696 freeaddrinfo(addr_ai);
697 return -EINVAL;
698 }
699
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530700 ret = getaddrinfo(gw, NULL, &hints, &gw_ai);
701 if (ret != 0) {
702 printerr("getaddrinfo failed: invalid gateway %s\n", gw);
703 freeaddrinfo(addr_ai);
704 return -EINVAL;
705 }
706
707 if (addr_ai->ai_family != gw_ai->ai_family) {
708 printerr("ifc_add_route: different address families: %s and %s\n", dst, gw);
709 freeaddrinfo(addr_ai);
710 freeaddrinfo(gw_ai);
711 return -EINVAL;
712 }
713
714 if (addr_ai->ai_family == AF_INET6) {
715 memcpy(&ipv6_dst, addr_ai->ai_addr, sizeof(struct sockaddr_in6));
716 memcpy(&ipv6_gw, gw_ai->ai_addr, sizeof(struct sockaddr_in6));
Robert Greenwalt021d0a22011-05-10 14:59:20 -0700717 ret = ifc_act_on_ipv6_route(action, ifname, ipv6_dst.sin6_addr,
718 prefix_length, ipv6_gw.sin6_addr);
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530719 } else if (addr_ai->ai_family == AF_INET) {
720 memcpy(&ipv4_dst, addr_ai->ai_addr, sizeof(struct sockaddr_in));
721 memcpy(&ipv4_gw, gw_ai->ai_addr, sizeof(struct sockaddr_in));
Robert Greenwalt021d0a22011-05-10 14:59:20 -0700722 ret = ifc_act_on_ipv4_route(action, ifname, ipv4_dst.sin_addr,
723 prefix_length, ipv4_gw.sin_addr);
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530724 } else {
725 printerr("ifc_add_route: getaddrinfo returned un supported address family %d\n",
726 addr_ai->ai_family);
727 ret = -EAFNOSUPPORT;
728 }
729
730 freeaddrinfo(addr_ai);
731 freeaddrinfo(gw_ai);
732 return ret;
733}
Robert Greenwalt021d0a22011-05-10 14:59:20 -0700734
Robert Greenwaltb6b48ae2011-05-24 21:12:51 -0700735/*
736 * DEPRECATED
737 */
738int ifc_add_ipv4_route(const char *ifname, struct in_addr dst, int prefix_length,
739 struct in_addr gw)
740{
741 int i =ifc_act_on_ipv4_route(SIOCADDRT, ifname, dst, prefix_length, gw);
Wink Saville0c613612011-09-20 15:53:23 -0700742 if (DBG) printerr("ifc_add_ipv4_route(%s, xx, %d, xx) = %d", ifname, prefix_length, i);
Robert Greenwaltb6b48ae2011-05-24 21:12:51 -0700743 return i;
744}
745
746/*
747 * DEPRECATED
748 */
749int ifc_add_ipv6_route(const char *ifname, struct in6_addr dst, int prefix_length,
750 struct in6_addr gw)
751{
752 return ifc_act_on_ipv6_route(SIOCADDRT, ifname, dst, prefix_length, gw);
753}
754
Robert Greenwalt021d0a22011-05-10 14:59:20 -0700755int ifc_add_route(const char *ifname, const char *dst, int prefix_length, const char *gw)
756{
Robert Greenwaltb6b48ae2011-05-24 21:12:51 -0700757 int i = ifc_act_on_route(SIOCADDRT, ifname, dst, prefix_length, gw);
Wink Saville0c613612011-09-20 15:53:23 -0700758 if (DBG) printerr("ifc_add_route(%s, %s, %d, %s) = %d", ifname, dst, prefix_length, gw, i);
Robert Greenwaltb6b48ae2011-05-24 21:12:51 -0700759 return i;
Robert Greenwalt021d0a22011-05-10 14:59:20 -0700760}
761
762int ifc_remove_route(const char *ifname, const char*dst, int prefix_length, const char *gw)
763{
764 return ifc_act_on_route(SIOCDELRT, ifname, dst, prefix_length, gw);
765}