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