blob: 8076496909efdc33516b04297241e37c7a848002 [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
Szymon Jakubczakc88e09c2010-06-09 16:11:09 -040054static const char *ipaddr_to_string(in_addr_t addr)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080055{
56 struct in_addr in_addr;
57
58 in_addr.s_addr = addr;
59 return inet_ntoa(in_addr);
60}
61
62int ifc_init(void)
63{
64 if (ifc_ctl_sock == -1) {
65 ifc_ctl_sock = socket(AF_INET, SOCK_DGRAM, 0);
66 if (ifc_ctl_sock < 0) {
67 printerr("socket() failed: %s\n", strerror(errno));
68 }
69 }
70 return ifc_ctl_sock < 0 ? -1 : 0;
71}
72
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +053073int ifc_init6(void)
74{
75 if (ifc_ctl_sock6 == -1) {
76 ifc_ctl_sock6 = socket(AF_INET6, SOCK_DGRAM, 0);
77 if (ifc_ctl_sock6 < 0) {
78 printerr("socket() failed: %s\n", strerror(errno));
79 }
80 }
81 return ifc_ctl_sock6 < 0 ? -1 : 0;
82}
83
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080084void ifc_close(void)
85{
86 if (ifc_ctl_sock != -1) {
87 (void)close(ifc_ctl_sock);
88 ifc_ctl_sock = -1;
89 }
90}
91
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +053092void ifc_close6(void)
93{
94 if (ifc_ctl_sock6 != -1) {
95 (void)close(ifc_ctl_sock6);
96 ifc_ctl_sock6 = -1;
97 }
98}
99
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800100static void ifc_init_ifr(const char *name, struct ifreq *ifr)
101{
102 memset(ifr, 0, sizeof(struct ifreq));
103 strncpy(ifr->ifr_name, name, IFNAMSIZ);
104 ifr->ifr_name[IFNAMSIZ - 1] = 0;
105}
106
107int ifc_get_hwaddr(const char *name, void *ptr)
108{
109 int r;
110 struct ifreq ifr;
111 ifc_init_ifr(name, &ifr);
112
113 r = ioctl(ifc_ctl_sock, SIOCGIFHWADDR, &ifr);
114 if(r < 0) return -1;
115
Szymon Jakubczakc88e09c2010-06-09 16:11:09 -0400116 memcpy(ptr, &ifr.ifr_hwaddr.sa_data, ETH_ALEN);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800117 return 0;
118}
119
120int ifc_get_ifindex(const char *name, int *if_indexp)
121{
122 int r;
123 struct ifreq ifr;
124 ifc_init_ifr(name, &ifr);
125
126 r = ioctl(ifc_ctl_sock, SIOCGIFINDEX, &ifr);
127 if(r < 0) return -1;
128
129 *if_indexp = ifr.ifr_ifindex;
Dmitry Shmidt7d05a802011-01-24 17:10:30 -0800130 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800131}
132
133static int ifc_set_flags(const char *name, unsigned set, unsigned clr)
134{
135 struct ifreq ifr;
136 ifc_init_ifr(name, &ifr);
137
138 if(ioctl(ifc_ctl_sock, SIOCGIFFLAGS, &ifr) < 0) return -1;
139 ifr.ifr_flags = (ifr.ifr_flags & (~clr)) | set;
140 return ioctl(ifc_ctl_sock, SIOCSIFFLAGS, &ifr);
141}
142
143int ifc_up(const char *name)
144{
145 return ifc_set_flags(name, IFF_UP, 0);
146}
147
148int ifc_down(const char *name)
149{
150 return ifc_set_flags(name, 0, IFF_UP);
151}
152
153static void init_sockaddr_in(struct sockaddr *sa, in_addr_t addr)
154{
155 struct sockaddr_in *sin = (struct sockaddr_in *) sa;
156 sin->sin_family = AF_INET;
157 sin->sin_port = 0;
158 sin->sin_addr.s_addr = addr;
159}
160
161int ifc_set_addr(const char *name, in_addr_t addr)
162{
163 struct ifreq ifr;
164
165 ifc_init_ifr(name, &ifr);
166 init_sockaddr_in(&ifr.ifr_addr, addr);
Dmitry Shmidt7d05a802011-01-24 17:10:30 -0800167
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800168 return ioctl(ifc_ctl_sock, SIOCSIFADDR, &ifr);
169}
170
Szymon Jakubczakc88e09c2010-06-09 16:11:09 -0400171int ifc_set_hwaddr(const char *name, const void *ptr)
172{
173 int r;
174 struct ifreq ifr;
175 ifc_init_ifr(name, &ifr);
176
177 ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER;
178 memcpy(&ifr.ifr_hwaddr.sa_data, ptr, ETH_ALEN);
179 return ioctl(ifc_ctl_sock, SIOCSIFHWADDR, &ifr);
180}
181
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800182int ifc_set_mask(const char *name, in_addr_t mask)
183{
184 struct ifreq ifr;
185
186 ifc_init_ifr(name, &ifr);
187 init_sockaddr_in(&ifr.ifr_addr, mask);
Dmitry Shmidt7d05a802011-01-24 17:10:30 -0800188
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800189 return ioctl(ifc_ctl_sock, SIOCSIFNETMASK, &ifr);
190}
191
192int ifc_get_info(const char *name, in_addr_t *addr, in_addr_t *mask, unsigned *flags)
193{
194 struct ifreq ifr;
195 ifc_init_ifr(name, &ifr);
196
197 if (addr != NULL) {
198 if(ioctl(ifc_ctl_sock, SIOCGIFADDR, &ifr) < 0) {
199 *addr = 0;
200 } else {
201 *addr = ((struct sockaddr_in*) &ifr.ifr_addr)->sin_addr.s_addr;
202 }
203 }
Dmitry Shmidt7d05a802011-01-24 17:10:30 -0800204
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800205 if (mask != NULL) {
206 if(ioctl(ifc_ctl_sock, SIOCGIFNETMASK, &ifr) < 0) {
207 *mask = 0;
208 } else {
209 *mask = ((struct sockaddr_in*) &ifr.ifr_addr)->sin_addr.s_addr;
210 }
211 }
212
213 if (flags != NULL) {
214 if(ioctl(ifc_ctl_sock, SIOCGIFFLAGS, &ifr) < 0) {
215 *flags = 0;
216 } else {
217 *flags = ifr.ifr_flags;
218 }
219 }
220
221 return 0;
222}
223
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530224in_addr_t get_ipv4_netmask(int prefix_length)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800225{
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530226 in_addr_t mask = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800227
Stan Chesnutt36f93f02010-12-16 10:59:48 -0800228 // C99 (6.5.7): shifts of 32 bits have undefined results
229 if (prefix_length == 0) {
230 return 0;
231 }
232
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530233 mask = ~mask << (32 - prefix_length);
234 mask = htonl(mask);
235
236 return mask;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800237}
238
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530239int ifc_add_ipv4_route(const char *ifname, struct in_addr dst, int prefix_length,
240 struct in_addr gw)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800241{
242 struct rtentry rt;
243 int result;
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530244 in_addr_t netmask;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800245
246 memset(&rt, 0, sizeof(rt));
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530247
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800248 rt.rt_dst.sa_family = AF_INET;
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530249 rt.rt_dev = (void*) ifname;
250
251 netmask = get_ipv4_netmask(prefix_length);
252 init_sockaddr_in(&rt.rt_genmask, netmask);
253 init_sockaddr_in(&rt.rt_dst, dst.s_addr);
254 rt.rt_flags = RTF_UP;
255
256 if (prefix_length == 32) {
257 rt.rt_flags |= RTF_HOST;
258 }
259
260 if (gw.s_addr != 0) {
261 rt.rt_flags |= RTF_GATEWAY;
262 init_sockaddr_in(&rt.rt_gateway, gw.s_addr);
263 }
264
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800265 ifc_init();
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530266
267 if (ifc_ctl_sock < 0) {
268 return -errno;
269 }
270
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800271 result = ioctl(ifc_ctl_sock, SIOCADDRT, &rt);
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530272 if (result < 0) {
273 if (errno == EEXIST) {
274 result = 0;
275 } else {
276 result = -errno;
277 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800278 }
279 ifc_close();
280 return result;
281}
282
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530283int ifc_create_default_route(const char *name, in_addr_t gw)
284{
285 struct in_addr in_dst, in_gw;
286
287 in_dst.s_addr = 0;
288 in_gw.s_addr = gw;
289
290 return ifc_add_ipv4_route(name, in_dst, 0, in_gw);
291}
292
293int ifc_add_host_route(const char *name, in_addr_t dst)
294{
295 struct in_addr in_dst, in_gw;
296
297 in_dst.s_addr = dst;
298 in_gw.s_addr = 0;
299
300 return ifc_add_ipv4_route(name, in_dst, 32, in_gw);
301}
302
Mike Lockwoodfeb63e92009-07-10 17:21:17 -0400303int ifc_enable(const char *ifname)
304{
305 int result;
306
307 ifc_init();
308 result = ifc_up(ifname);
309 ifc_close();
310 return result;
311}
312
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800313int ifc_disable(const char *ifname)
314{
315 int result;
316
317 ifc_init();
318 result = ifc_down(ifname);
319 ifc_set_addr(ifname, 0);
320 ifc_close();
321 return result;
322}
323
324int ifc_reset_connections(const char *ifname)
325{
326#ifdef HAVE_ANDROID_OS
327 int result;
328 in_addr_t myaddr;
329 struct ifreq ifr;
330
331 ifc_init();
332 ifc_get_info(ifname, &myaddr, NULL, NULL);
333 ifc_init_ifr(ifname, &ifr);
334 init_sockaddr_in(&ifr.ifr_addr, myaddr);
335 result = ioctl(ifc_ctl_sock, SIOCKILLADDR, &ifr);
336 ifc_close();
Dmitry Shmidt7d05a802011-01-24 17:10:30 -0800337
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800338 return result;
339#else
340 return 0;
341#endif
342}
343
344/*
345 * Remove the routes associated with the named interface.
346 */
347int ifc_remove_host_routes(const char *name)
348{
349 char ifname[64];
350 in_addr_t dest, gway, mask;
351 int flags, refcnt, use, metric, mtu, win, irtt;
352 struct rtentry rt;
353 FILE *fp;
354 struct in_addr addr;
355
356 fp = fopen("/proc/net/route", "r");
357 if (fp == NULL)
358 return -1;
359 /* Skip the header line */
360 if (fscanf(fp, "%*[^\n]\n") < 0) {
361 fclose(fp);
362 return -1;
363 }
364 ifc_init();
365 for (;;) {
366 int nread = fscanf(fp, "%63s%X%X%X%d%d%d%X%d%d%d\n",
367 ifname, &dest, &gway, &flags, &refcnt, &use, &metric, &mask,
368 &mtu, &win, &irtt);
369 if (nread != 11) {
370 break;
371 }
372 if ((flags & (RTF_UP|RTF_HOST)) != (RTF_UP|RTF_HOST)
373 || strcmp(ifname, name) != 0) {
374 continue;
375 }
376 memset(&rt, 0, sizeof(rt));
377 rt.rt_dev = (void *)name;
378 init_sockaddr_in(&rt.rt_dst, dest);
379 init_sockaddr_in(&rt.rt_gateway, gway);
380 init_sockaddr_in(&rt.rt_genmask, mask);
381 addr.s_addr = dest;
382 if (ioctl(ifc_ctl_sock, SIOCDELRT, &rt) < 0) {
383 LOGD("failed to remove route for %s to %s: %s",
384 ifname, inet_ntoa(addr), strerror(errno));
385 }
386 }
387 fclose(fp);
388 ifc_close();
389 return 0;
390}
391
392/*
393 * Return the address of the default gateway
394 *
395 * TODO: factor out common code from this and remove_host_routes()
396 * so that we only scan /proc/net/route in one place.
397 */
398int ifc_get_default_route(const char *ifname)
399{
400 char name[64];
401 in_addr_t dest, gway, mask;
402 int flags, refcnt, use, metric, mtu, win, irtt;
403 int result;
404 FILE *fp;
405
406 fp = fopen("/proc/net/route", "r");
407 if (fp == NULL)
408 return 0;
409 /* Skip the header line */
410 if (fscanf(fp, "%*[^\n]\n") < 0) {
411 fclose(fp);
412 return 0;
413 }
414 ifc_init();
415 result = 0;
416 for (;;) {
417 int nread = fscanf(fp, "%63s%X%X%X%d%d%d%X%d%d%d\n",
418 name, &dest, &gway, &flags, &refcnt, &use, &metric, &mask,
419 &mtu, &win, &irtt);
420 if (nread != 11) {
421 break;
422 }
423 if ((flags & (RTF_UP|RTF_GATEWAY)) == (RTF_UP|RTF_GATEWAY)
424 && dest == 0
425 && strcmp(ifname, name) == 0) {
426 result = gway;
427 break;
428 }
429 }
430 fclose(fp);
431 ifc_close();
432 return result;
433}
434
435/*
436 * Sets the specified gateway as the default route for the named interface.
437 */
438int ifc_set_default_route(const char *ifname, in_addr_t gateway)
439{
440 struct in_addr addr;
441 int result;
442
443 ifc_init();
444 addr.s_addr = gateway;
445 if ((result = ifc_create_default_route(ifname, gateway)) < 0) {
446 LOGD("failed to add %s as default route for %s: %s",
447 inet_ntoa(addr), ifname, strerror(errno));
448 }
449 ifc_close();
450 return result;
451}
452
453/*
454 * Removes the default route for the named interface.
455 */
456int ifc_remove_default_route(const char *ifname)
457{
458 struct rtentry rt;
459 int result;
460
461 ifc_init();
462 memset(&rt, 0, sizeof(rt));
463 rt.rt_dev = (void *)ifname;
464 rt.rt_flags = RTF_UP|RTF_GATEWAY;
465 init_sockaddr_in(&rt.rt_dst, 0);
466 if ((result = ioctl(ifc_ctl_sock, SIOCDELRT, &rt)) < 0) {
467 LOGD("failed to remove default route for %s: %s", ifname, strerror(errno));
468 }
469 ifc_close();
470 return result;
471}
472
473int
474ifc_configure(const char *ifname,
475 in_addr_t address,
476 in_addr_t netmask,
477 in_addr_t gateway,
478 in_addr_t dns1,
479 in_addr_t dns2) {
480
481 char dns_prop_name[PROPERTY_KEY_MAX];
482
483 ifc_init();
484
485 if (ifc_up(ifname)) {
486 printerr("failed to turn on interface %s: %s\n", ifname, strerror(errno));
487 ifc_close();
488 return -1;
489 }
490 if (ifc_set_addr(ifname, address)) {
491 printerr("failed to set ipaddr %s: %s\n", ipaddr_to_string(address), strerror(errno));
492 ifc_close();
493 return -1;
494 }
495 if (ifc_set_mask(ifname, netmask)) {
496 printerr("failed to set netmask %s: %s\n", ipaddr_to_string(netmask), strerror(errno));
497 ifc_close();
498 return -1;
499 }
500 if (ifc_create_default_route(ifname, gateway)) {
501 printerr("failed to set default route %s: %s\n", ipaddr_to_string(gateway), strerror(errno));
502 ifc_close();
503 return -1;
504 }
505
506 ifc_close();
507
Szymon Jakubczakc88e09c2010-06-09 16:11:09 -0400508 snprintf(dns_prop_name, sizeof(dns_prop_name), "net.%s.dns1", ifname);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800509 property_set(dns_prop_name, dns1 ? ipaddr_to_string(dns1) : "");
Szymon Jakubczakc88e09c2010-06-09 16:11:09 -0400510 snprintf(dns_prop_name, sizeof(dns_prop_name), "net.%s.dns2", ifname);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800511 property_set(dns_prop_name, dns2 ? ipaddr_to_string(dns2) : "");
512
513 return 0;
514}
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530515
516int ifc_add_ipv6_route(const char *ifname, struct in6_addr dst, int prefix_length,
517 struct in6_addr gw)
518{
519 struct in6_rtmsg rtmsg;
520 int result;
521 int ifindex;
522
523 memset(&rtmsg, 0, sizeof(rtmsg));
524
525 ifindex = if_nametoindex(ifname);
526 if (ifindex == 0) {
527 printerr("if_nametoindex() failed: interface %s\n", ifname);
528 return -ENXIO;
529 }
530
531 rtmsg.rtmsg_ifindex = ifindex;
532 rtmsg.rtmsg_dst = dst;
533 rtmsg.rtmsg_dst_len = prefix_length;
534 rtmsg.rtmsg_flags = RTF_UP;
535
536 if (prefix_length == 128) {
537 rtmsg.rtmsg_flags |= RTF_HOST;
538 }
539
540 if (memcmp(&gw, &in6addr_any, sizeof(in6addr_any))) {
541 rtmsg.rtmsg_flags |= RTF_GATEWAY;
542 rtmsg.rtmsg_gateway = gw;
543 }
544
545 ifc_init6();
546
547 if (ifc_ctl_sock6 < 0) {
548 return -errno;
549 }
550
551 result = ioctl(ifc_ctl_sock6, SIOCADDRT, &rtmsg);
552 if (result < 0) {
553 if (errno == EEXIST) {
554 result = 0;
555 } else {
556 result = -errno;
557 }
558 }
559 ifc_close6();
560 return result;
561}
562
563int ifc_add_route(const char *ifname, const char *dst, int prefix_length,
564 const char *gw)
565{
566 int ret = 0;
567 struct sockaddr_in ipv4_dst, ipv4_gw;
568 struct sockaddr_in6 ipv6_dst, ipv6_gw;
569 struct addrinfo hints, *addr_ai, *gw_ai;
570
571 memset(&hints, 0, sizeof(hints));
572 hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */
573 hints.ai_flags = AI_NUMERICHOST;
574
575 ret = getaddrinfo(dst, NULL, &hints, &addr_ai);
576
577 if (ret != 0) {
578 printerr("getaddrinfo failed: invalid address %s\n", dst);
579 return -EINVAL;
580 }
581
582 if (gw == NULL) {
583 if (addr_ai->ai_family == AF_INET6) {
584 gw = "::";
585 } else if (addr_ai->ai_family == AF_INET) {
586 gw = "0.0.0.0";
587 }
588 }
589
590 ret = getaddrinfo(gw, NULL, &hints, &gw_ai);
591 if (ret != 0) {
592 printerr("getaddrinfo failed: invalid gateway %s\n", gw);
593 freeaddrinfo(addr_ai);
594 return -EINVAL;
595 }
596
597 if (addr_ai->ai_family != gw_ai->ai_family) {
598 printerr("ifc_add_route: different address families: %s and %s\n", dst, gw);
599 freeaddrinfo(addr_ai);
600 freeaddrinfo(gw_ai);
601 return -EINVAL;
602 }
603
604 if (addr_ai->ai_family == AF_INET6) {
605 memcpy(&ipv6_dst, addr_ai->ai_addr, sizeof(struct sockaddr_in6));
606 memcpy(&ipv6_gw, gw_ai->ai_addr, sizeof(struct sockaddr_in6));
607 ret = ifc_add_ipv6_route(ifname, ipv6_dst.sin6_addr, prefix_length,
608 ipv6_gw.sin6_addr);
609 } else if (addr_ai->ai_family == AF_INET) {
610 memcpy(&ipv4_dst, addr_ai->ai_addr, sizeof(struct sockaddr_in));
611 memcpy(&ipv4_gw, gw_ai->ai_addr, sizeof(struct sockaddr_in));
612 ret = ifc_add_ipv4_route(ifname, ipv4_dst.sin_addr, prefix_length,
613 ipv4_gw.sin_addr);
614 } else {
615 printerr("ifc_add_route: getaddrinfo returned un supported address family %d\n",
616 addr_ai->ai_family);
617 ret = -EAFNOSUPPORT;
618 }
619
620 freeaddrinfo(addr_ai);
621 freeaddrinfo(gw_ai);
622 return ret;
623}