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