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