blob: aa18bc1f991cd84b2c2e25a6451731371de0a0b9 [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>
Dmitry Shmidtcebcd452015-10-01 13:27:56 -070022#include <pthread.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080023
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>
Dmitry Shmidt7d05a802011-01-24 17:10:30 -080029#include <net/if.h>
Lorenzo Colitti47ddb512011-09-26 10:49:41 -070030#include <netdb.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080031
32#include <linux/if.h>
Szymon Jakubczakc88e09c2010-06-09 16:11:09 -040033#include <linux/if_ether.h>
34#include <linux/if_arp.h>
Lorenzo Colitti47ddb512011-09-26 10:49:41 -070035#include <linux/netlink.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080036#include <linux/route.h>
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +053037#include <linux/ipv6_route.h>
Lorenzo Colitti47ddb512011-09-26 10:49:41 -070038#include <linux/rtnetlink.h>
39#include <linux/sockios.h>
40
41#include "netutils/ifc.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080042
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>
Steve Block8d66c492011-12-20 16:07:45 +000050#define ALOGD printf
Steve Blockae8b56c2012-01-05 22:25:38 +000051#define ALOGW printf
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080052#endif
53
Elliott Hughes9b828ad2015-07-30 08:47:35 -070054#if defined(__ANDROID__)
Elliott Hughes2d640c22013-11-12 13:05:01 -080055/* SIOCKILLADDR is an Android extension. */
56#define SIOCKILLADDR 0x8939
57#endif
58
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080059static int ifc_ctl_sock = -1;
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +053060static int ifc_ctl_sock6 = -1;
Dmitry Shmidtcebcd452015-10-01 13:27:56 -070061static pthread_mutex_t ifc_sock_mutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
62static pthread_mutex_t ifc_sock6_mutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080063void printerr(char *fmt, ...);
64
Robert Greenwaltb6b48ae2011-05-24 21:12:51 -070065#define DBG 0
Lorenzo Colitti47ddb512011-09-26 10:49:41 -070066#define INET_ADDRLEN 4
67#define INET6_ADDRLEN 16
Robert Greenwaltb6b48ae2011-05-24 21:12:51 -070068
Robert Greenwalt09dd8192011-02-01 15:21:21 -080069in_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
84int ipv4NetmaskToPrefixLength(in_addr_t mask)
85{
Robert Greenwalt09dd8192011-02-01 15:21:21 -080086 int prefixLength = 0;
Chris Dearman6ee3ecc2011-06-17 17:07:46 -070087 uint32_t m = (uint32_t)ntohl(mask);
Robert Greenwalt09dd8192011-02-01 15:21:21 -080088 while (m & 0x80000000) {
89 prefixLength++;
90 m = m << 1;
91 }
92 return prefixLength;
93}
94
Szymon Jakubczakc88e09c2010-06-09 16:11:09 -040095static const char *ipaddr_to_string(in_addr_t addr)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080096{
97 struct in_addr in_addr;
98
99 in_addr.s_addr = addr;
100 return inet_ntoa(in_addr);
101}
102
Lorenzo Colitti47ddb512011-09-26 10:49:41 -0700103int 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
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800125int ifc_init(void)
126{
Robert Greenwaltb6b48ae2011-05-24 21:12:51 -0700127 int ret;
Dmitry Shmidtcebcd452015-10-01 13:27:56 -0700128
129 pthread_mutex_lock(&ifc_sock_mutex);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800130 if (ifc_ctl_sock == -1) {
Nick Kralevich6d3cddb2015-02-26 13:32:52 -0800131 ifc_ctl_sock = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800132 if (ifc_ctl_sock < 0) {
133 printerr("socket() failed: %s\n", strerror(errno));
134 }
135 }
Robert Greenwaltb6b48ae2011-05-24 21:12:51 -0700136
137 ret = ifc_ctl_sock < 0 ? -1 : 0;
138 if (DBG) printerr("ifc_init_returning %d", ret);
139 return ret;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800140}
141
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530142int ifc_init6(void)
143{
Dmitry Shmidtcebcd452015-10-01 13:27:56 -0700144 pthread_mutex_lock(&ifc_sock6_mutex);
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530145 if (ifc_ctl_sock6 == -1) {
Nick Kralevich6d3cddb2015-02-26 13:32:52 -0800146 ifc_ctl_sock6 = socket(AF_INET6, SOCK_DGRAM | SOCK_CLOEXEC, 0);
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530147 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
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800154void ifc_close(void)
155{
Robert Greenwaltb6b48ae2011-05-24 21:12:51 -0700156 if (DBG) printerr("ifc_close");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800157 if (ifc_ctl_sock != -1) {
158 (void)close(ifc_ctl_sock);
159 ifc_ctl_sock = -1;
160 }
Dmitry Shmidtcebcd452015-10-01 13:27:56 -0700161 pthread_mutex_unlock(&ifc_sock_mutex);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800162}
163
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530164void ifc_close6(void)
165{
166 if (ifc_ctl_sock6 != -1) {
167 (void)close(ifc_ctl_sock6);
168 ifc_ctl_sock6 = -1;
169 }
Dmitry Shmidtcebcd452015-10-01 13:27:56 -0700170 pthread_mutex_unlock(&ifc_sock6_mutex);
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530171}
172
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800173static 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
180int 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
Szymon Jakubczakc88e09c2010-06-09 16:11:09 -0400189 memcpy(ptr, &ifr.ifr_hwaddr.sa_data, ETH_ALEN);
Robert Greenwaltb6b48ae2011-05-24 21:12:51 -0700190 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800191}
192
193int 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;
Dmitry Shmidt7d05a802011-01-24 17:10:30 -0800203 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800204}
205
206static 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
216int ifc_up(const char *name)
217{
Robert Greenwaltb6b48ae2011-05-24 21:12:51 -0700218 int ret = ifc_set_flags(name, IFF_UP, 0);
219 if (DBG) printerr("ifc_up(%s) = %d", name, ret);
220 return ret;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800221}
222
223int ifc_down(const char *name)
224{
Robert Greenwaltb6b48ae2011-05-24 21:12:51 -0700225 int ret = ifc_set_flags(name, 0, IFF_UP);
226 if (DBG) printerr("ifc_down(%s) = %d", name, ret);
227 return ret;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800228}
229
230static 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
238int ifc_set_addr(const char *name, in_addr_t addr)
239{
240 struct ifreq ifr;
Robert Greenwaltb6b48ae2011-05-24 21:12:51 -0700241 int ret;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800242
243 ifc_init_ifr(name, &ifr);
244 init_sockaddr_in(&ifr.ifr_addr, addr);
Dmitry Shmidt7d05a802011-01-24 17:10:30 -0800245
Robert Greenwaltb6b48ae2011-05-24 21:12:51 -0700246 ret = ioctl(ifc_ctl_sock, SIOCSIFADDR, &ifr);
247 if (DBG) printerr("ifc_set_addr(%s, xx) = %d", name, ret);
248 return ret;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800249}
250
Lorenzo Colitti47ddb512011-09-26 10:49:41 -0700251/*
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 */
260int 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 void *addr;
265 size_t addrlen;
266 struct {
267 struct nlmsghdr n;
268 struct ifaddrmsg r;
269 // Allow for IPv6 address, headers, and padding.
270 char attrbuf[NLMSG_ALIGN(sizeof(struct nlmsghdr)) +
271 NLMSG_ALIGN(sizeof(struct rtattr)) +
272 NLMSG_ALIGN(INET6_ADDRLEN)];
273 } req;
274 struct rtattr *rta;
275 struct nlmsghdr *nh;
276 struct nlmsgerr *err;
277 char buf[NLMSG_ALIGN(sizeof(struct nlmsghdr)) +
278 NLMSG_ALIGN(sizeof(struct nlmsgerr)) +
279 NLMSG_ALIGN(sizeof(struct nlmsghdr))];
280
281 // Get interface ID.
282 ifindex = if_nametoindex(name);
283 if (ifindex == 0) {
284 return -errno;
285 }
286
287 // Convert string representation to sockaddr_storage.
288 ret = string_to_ip(address, &ss);
289 if (ret) {
290 return ret;
291 }
292
293 // Determine address type and length.
294 if (ss.ss_family == AF_INET) {
295 struct sockaddr_in *sin = (struct sockaddr_in *) &ss;
296 addr = &sin->sin_addr;
297 addrlen = INET_ADDRLEN;
298 } else if (ss.ss_family == AF_INET6) {
299 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) &ss;
300 addr = &sin6->sin6_addr;
301 addrlen = INET6_ADDRLEN;
302 } else {
303 return -EAFNOSUPPORT;
304 }
305
306 // Fill in netlink structures.
307 memset(&req, 0, sizeof(req));
308
309 // Netlink message header.
310 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(req.r));
311 req.n.nlmsg_type = action;
312 req.n.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
313 req.n.nlmsg_pid = getpid();
314
315 // Interface address message header.
316 req.r.ifa_family = ss.ss_family;
317 req.r.ifa_prefixlen = prefixlen;
318 req.r.ifa_index = ifindex;
319
320 // Routing attribute. Contains the actual IP address.
321 rta = (struct rtattr *) (((char *) &req) + NLMSG_ALIGN(req.n.nlmsg_len));
322 rta->rta_type = IFA_LOCAL;
323 rta->rta_len = RTA_LENGTH(addrlen);
324 req.n.nlmsg_len = NLMSG_ALIGN(req.n.nlmsg_len) + RTA_LENGTH(addrlen);
325 memcpy(RTA_DATA(rta), addr, addrlen);
326
Nick Kralevich6d3cddb2015-02-26 13:32:52 -0800327 s = socket(PF_NETLINK, SOCK_RAW | SOCK_CLOEXEC, NETLINK_ROUTE);
Lorenzo Colitti47ddb512011-09-26 10:49:41 -0700328 if (send(s, &req, req.n.nlmsg_len, 0) < 0) {
329 close(s);
330 return -errno;
331 }
332
333 len = recv(s, buf, sizeof(buf), 0);
334 close(s);
335 if (len < 0) {
336 return -errno;
337 }
338
339 // Parse the acknowledgement to find the return code.
340 nh = (struct nlmsghdr *) buf;
341 if (!NLMSG_OK(nh, (unsigned) len) || nh->nlmsg_type != NLMSG_ERROR) {
342 return -EINVAL;
343 }
344 err = NLMSG_DATA(nh);
345
346 // Return code is negative errno.
347 return err->error;
348}
349
350int ifc_add_address(const char *name, const char *address, int prefixlen) {
351 return ifc_act_on_address(RTM_NEWADDR, name, address, prefixlen);
352}
353
354int ifc_del_address(const char *name, const char * address, int prefixlen) {
355 return ifc_act_on_address(RTM_DELADDR, name, address, prefixlen);
356}
357
358/*
359 * Clears IPv6 addresses on the specified interface.
360 */
361int ifc_clear_ipv6_addresses(const char *name) {
362 char rawaddrstr[INET6_ADDRSTRLEN], addrstr[INET6_ADDRSTRLEN];
363 unsigned int prefixlen;
364 int lasterror = 0, i, j, ret;
365 char ifname[64]; // Currently, IFNAMSIZ = 16.
366 FILE *f = fopen("/proc/net/if_inet6", "r");
367 if (!f) {
368 return -errno;
369 }
370
371 // Format:
372 // 20010db8000a0001fc446aa4b5b347ed 03 40 00 01 wlan0
373 while (fscanf(f, "%32s %*02x %02x %*02x %*02x %63s\n",
374 rawaddrstr, &prefixlen, ifname) == 3) {
375 // Is this the interface we're looking for?
376 if (strcmp(name, ifname)) {
377 continue;
378 }
379
380 // Put the colons back into the address.
381 for (i = 0, j = 0; i < 32; i++, j++) {
382 addrstr[j] = rawaddrstr[i];
383 if (i % 4 == 3) {
384 addrstr[++j] = ':';
385 }
386 }
387 addrstr[j - 1] = '\0';
388
389 // Don't delete the link-local address as well, or it will disable IPv6
390 // on the interface.
391 if (strncmp(addrstr, "fe80:", 5) == 0) {
392 continue;
393 }
394
395 ret = ifc_del_address(ifname, addrstr, prefixlen);
396 if (ret) {
Steve Block01dda202012-01-06 14:13:42 +0000397 ALOGE("Deleting address %s/%d on %s: %s", addrstr, prefixlen, ifname,
Lorenzo Colitti47ddb512011-09-26 10:49:41 -0700398 strerror(-ret));
399 lasterror = ret;
400 }
401 }
402
403 fclose(f);
404 return lasterror;
405}
406
407/*
408 * Clears IPv4 addresses on the specified interface.
409 */
410void ifc_clear_ipv4_addresses(const char *name) {
411 unsigned count, addr;
412 ifc_init();
413 for (count=0, addr=1;((addr != 0) && (count < 255)); count++) {
414 if (ifc_get_addr(name, &addr) < 0)
415 break;
416 if (addr)
417 ifc_set_addr(name, 0);
418 }
419 ifc_close();
420}
421
422/*
423 * Clears all IP addresses on the specified interface.
424 */
425int ifc_clear_addresses(const char *name) {
426 ifc_clear_ipv4_addresses(name);
427 return ifc_clear_ipv6_addresses(name);
428}
429
Szymon Jakubczakc88e09c2010-06-09 16:11:09 -0400430int ifc_set_hwaddr(const char *name, const void *ptr)
431{
Szymon Jakubczakc88e09c2010-06-09 16:11:09 -0400432 struct ifreq ifr;
433 ifc_init_ifr(name, &ifr);
434
435 ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER;
436 memcpy(&ifr.ifr_hwaddr.sa_data, ptr, ETH_ALEN);
437 return ioctl(ifc_ctl_sock, SIOCSIFHWADDR, &ifr);
438}
439
Jake Hamby6f49d5f2011-04-11 19:46:41 -0700440int ifc_set_mask(const char *name, in_addr_t mask)
441{
442 struct ifreq ifr;
Robert Greenwaltb6b48ae2011-05-24 21:12:51 -0700443 int ret;
Jake Hamby6f49d5f2011-04-11 19:46:41 -0700444
445 ifc_init_ifr(name, &ifr);
446 init_sockaddr_in(&ifr.ifr_addr, mask);
447
Robert Greenwaltb6b48ae2011-05-24 21:12:51 -0700448 ret = ioctl(ifc_ctl_sock, SIOCSIFNETMASK, &ifr);
449 if (DBG) printerr("ifc_set_mask(%s, xx) = %d", name, ret);
450 return ret;
Jake Hamby6f49d5f2011-04-11 19:46:41 -0700451}
452
Robert Greenwalt09dd8192011-02-01 15:21:21 -0800453int ifc_set_prefixLength(const char *name, int prefixLength)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800454{
455 struct ifreq ifr;
Robert Greenwalt09dd8192011-02-01 15:21:21 -0800456 // TODO - support ipv6
457 if (prefixLength > 32 || prefixLength < 0) return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800458
Robert Greenwalt09dd8192011-02-01 15:21:21 -0800459 in_addr_t mask = prefixLengthToIpv4Netmask(prefixLength);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800460 ifc_init_ifr(name, &ifr);
461 init_sockaddr_in(&ifr.ifr_addr, mask);
Dmitry Shmidt7d05a802011-01-24 17:10:30 -0800462
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800463 return ioctl(ifc_ctl_sock, SIOCSIFNETMASK, &ifr);
464}
465
Dmitry Shmidt9092b912011-01-27 14:16:20 -0800466int ifc_get_addr(const char *name, in_addr_t *addr)
467{
468 struct ifreq ifr;
469 int ret = 0;
470
471 ifc_init_ifr(name, &ifr);
472 if (addr != NULL) {
473 ret = ioctl(ifc_ctl_sock, SIOCGIFADDR, &ifr);
474 if (ret < 0) {
475 *addr = 0;
476 } else {
477 *addr = ((struct sockaddr_in*) &ifr.ifr_addr)->sin_addr.s_addr;
478 }
479 }
480 return ret;
481}
482
Robert Greenwalt09dd8192011-02-01 15:21:21 -0800483int ifc_get_info(const char *name, in_addr_t *addr, int *prefixLength, unsigned *flags)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800484{
485 struct ifreq ifr;
486 ifc_init_ifr(name, &ifr);
487
488 if (addr != NULL) {
489 if(ioctl(ifc_ctl_sock, SIOCGIFADDR, &ifr) < 0) {
490 *addr = 0;
491 } else {
492 *addr = ((struct sockaddr_in*) &ifr.ifr_addr)->sin_addr.s_addr;
493 }
494 }
Dmitry Shmidt7d05a802011-01-24 17:10:30 -0800495
Robert Greenwalt09dd8192011-02-01 15:21:21 -0800496 if (prefixLength != NULL) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800497 if(ioctl(ifc_ctl_sock, SIOCGIFNETMASK, &ifr) < 0) {
Robert Greenwalt09dd8192011-02-01 15:21:21 -0800498 *prefixLength = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800499 } else {
Chris Dearman6ee3ecc2011-06-17 17:07:46 -0700500 *prefixLength = ipv4NetmaskToPrefixLength(
Robert Greenwalt09dd8192011-02-01 15:21:21 -0800501 ((struct sockaddr_in*) &ifr.ifr_addr)->sin_addr.s_addr);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800502 }
503 }
504
505 if (flags != NULL) {
506 if(ioctl(ifc_ctl_sock, SIOCGIFFLAGS, &ifr) < 0) {
507 *flags = 0;
508 } else {
509 *flags = ifr.ifr_flags;
510 }
511 }
512
513 return 0;
514}
515
Robert Greenwalt021d0a22011-05-10 14:59:20 -0700516int 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 +0530517 struct in_addr gw)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800518{
519 struct rtentry rt;
520 int result;
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530521 in_addr_t netmask;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800522
523 memset(&rt, 0, sizeof(rt));
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530524
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800525 rt.rt_dst.sa_family = AF_INET;
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530526 rt.rt_dev = (void*) ifname;
527
Robert Greenwalt09dd8192011-02-01 15:21:21 -0800528 netmask = prefixLengthToIpv4Netmask(prefix_length);
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530529 init_sockaddr_in(&rt.rt_genmask, netmask);
530 init_sockaddr_in(&rt.rt_dst, dst.s_addr);
531 rt.rt_flags = RTF_UP;
532
533 if (prefix_length == 32) {
534 rt.rt_flags |= RTF_HOST;
535 }
536
537 if (gw.s_addr != 0) {
538 rt.rt_flags |= RTF_GATEWAY;
539 init_sockaddr_in(&rt.rt_gateway, gw.s_addr);
540 }
541
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800542 ifc_init();
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530543
544 if (ifc_ctl_sock < 0) {
Dmitry Shmidtcebcd452015-10-01 13:27:56 -0700545 ifc_close();
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530546 return -errno;
547 }
548
Robert Greenwalt021d0a22011-05-10 14:59:20 -0700549 result = ioctl(ifc_ctl_sock, action, &rt);
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530550 if (result < 0) {
551 if (errno == EEXIST) {
552 result = 0;
553 } else {
554 result = -errno;
555 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800556 }
557 ifc_close();
558 return result;
559}
560
Robert Greenwaltb6b48ae2011-05-24 21:12:51 -0700561/* deprecated - v4 only */
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530562int ifc_create_default_route(const char *name, in_addr_t gw)
563{
564 struct in_addr in_dst, in_gw;
565
566 in_dst.s_addr = 0;
567 in_gw.s_addr = gw;
568
Robert Greenwaltb6b48ae2011-05-24 21:12:51 -0700569 int ret = ifc_act_on_ipv4_route(SIOCADDRT, name, in_dst, 0, in_gw);
570 if (DBG) printerr("ifc_create_default_route(%s, %d) = %d", name, gw, ret);
571 return ret;
572}
573
Sreeram Ramachandraneec23262014-07-23 09:30:53 -0700574// Needed by code in hidden partner repositories / branches, so don't delete.
575int ifc_enable(const char *ifname)
576{
577 int result;
578
579 ifc_init();
580 result = ifc_up(ifname);
581 ifc_close();
582 return result;
583}
584
Sreeram Ramachandrane5ab3e32014-07-23 11:33:36 -0700585// Needed by code in hidden partner repositories / branches, so don't delete.
Sreeram Ramachandraneec23262014-07-23 09:30:53 -0700586int ifc_disable(const char *ifname)
587{
588 unsigned addr, count;
589 int result;
590
591 ifc_init();
592 result = ifc_down(ifname);
593
594 ifc_set_addr(ifname, 0);
595 for (count=0, addr=1;((addr != 0) && (count < 255)); count++) {
596 if (ifc_get_addr(ifname, &addr) < 0)
597 break;
598 if (addr)
599 ifc_set_addr(ifname, 0);
600 }
601
602 ifc_close();
603 return result;
604}
605
Wink Saville979203e2011-07-07 12:16:12 -0700606int ifc_reset_connections(const char *ifname, const int reset_mask)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800607{
Elliott Hughes9b828ad2015-07-30 08:47:35 -0700608#if defined(__ANDROID__)
Lorenzo Colitti6cf73ea2011-03-17 11:20:25 -0700609 int result, success;
Sreeram Ramachandran17622d02014-09-08 16:05:09 -0700610 in_addr_t myaddr = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800611 struct ifreq ifr;
Lorenzo Colitti6cf73ea2011-03-17 11:20:25 -0700612 struct in6_ifreq ifr6;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800613
Wink Saville979203e2011-07-07 12:16:12 -0700614 if (reset_mask & RESET_IPV4_ADDRESSES) {
615 /* IPv4. Clear connections on the IP address. */
616 ifc_init();
Sreeram Ramachandran17622d02014-09-08 16:05:09 -0700617 if (!(reset_mask & RESET_IGNORE_INTERFACE_ADDRESS)) {
618 ifc_get_info(ifname, &myaddr, NULL, NULL);
619 }
Wink Saville979203e2011-07-07 12:16:12 -0700620 ifc_init_ifr(ifname, &ifr);
621 init_sockaddr_in(&ifr.ifr_addr, myaddr);
622 result = ioctl(ifc_ctl_sock, SIOCKILLADDR, &ifr);
623 ifc_close();
624 } else {
625 result = 0;
Lorenzo Colitti6cf73ea2011-03-17 11:20:25 -0700626 }
Wink Saville979203e2011-07-07 12:16:12 -0700627
628 if (reset_mask & RESET_IPV6_ADDRESSES) {
629 /*
630 * IPv6. On Linux, when an interface goes down it loses all its IPv6
631 * addresses, so we don't know which connections belonged to that interface
632 * So we clear all unused IPv6 connections on the device by specifying an
633 * empty IPv6 address.
634 */
635 ifc_init6();
636 // This implicitly specifies an address of ::, i.e., kill all IPv6 sockets.
637 memset(&ifr6, 0, sizeof(ifr6));
638 success = ioctl(ifc_ctl_sock6, SIOCKILLADDR, &ifr6);
639 if (result == 0) {
640 result = success;
641 }
642 ifc_close6();
643 }
Lorenzo Colitti6cf73ea2011-03-17 11:20:25 -0700644
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800645 return result;
646#else
647 return 0;
648#endif
649}
650
Elliott Hughesc4630252015-02-03 19:56:35 +0000651/*
652 * Removes the default route for the named interface.
653 */
654int ifc_remove_default_route(const char *ifname)
655{
656 struct rtentry rt;
657 int result;
658
659 ifc_init();
660 memset(&rt, 0, sizeof(rt));
661 rt.rt_dev = (void *)ifname;
662 rt.rt_flags = RTF_UP|RTF_GATEWAY;
663 init_sockaddr_in(&rt.rt_dst, 0);
664 if ((result = ioctl(ifc_ctl_sock, SIOCDELRT, &rt)) < 0) {
665 ALOGD("failed to remove default route for %s: %s", ifname, strerror(errno));
666 }
667 ifc_close();
668 return result;
669}
670
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800671int
672ifc_configure(const char *ifname,
673 in_addr_t address,
Robert Greenwalt09dd8192011-02-01 15:21:21 -0800674 uint32_t prefixLength,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800675 in_addr_t gateway,
676 in_addr_t dns1,
677 in_addr_t dns2) {
678
679 char dns_prop_name[PROPERTY_KEY_MAX];
680
681 ifc_init();
682
683 if (ifc_up(ifname)) {
684 printerr("failed to turn on interface %s: %s\n", ifname, strerror(errno));
685 ifc_close();
686 return -1;
687 }
688 if (ifc_set_addr(ifname, address)) {
689 printerr("failed to set ipaddr %s: %s\n", ipaddr_to_string(address), strerror(errno));
690 ifc_close();
691 return -1;
692 }
Robert Greenwalt09dd8192011-02-01 15:21:21 -0800693 if (ifc_set_prefixLength(ifname, prefixLength)) {
694 printerr("failed to set prefixLength %d: %s\n", prefixLength, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800695 ifc_close();
696 return -1;
697 }
698 if (ifc_create_default_route(ifname, gateway)) {
699 printerr("failed to set default route %s: %s\n", ipaddr_to_string(gateway), strerror(errno));
700 ifc_close();
701 return -1;
702 }
703
704 ifc_close();
705
Szymon Jakubczakc88e09c2010-06-09 16:11:09 -0400706 snprintf(dns_prop_name, sizeof(dns_prop_name), "net.%s.dns1", ifname);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800707 property_set(dns_prop_name, dns1 ? ipaddr_to_string(dns1) : "");
Szymon Jakubczakc88e09c2010-06-09 16:11:09 -0400708 snprintf(dns_prop_name, sizeof(dns_prop_name), "net.%s.dns2", ifname);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800709 property_set(dns_prop_name, dns2 ? ipaddr_to_string(dns2) : "");
710
711 return 0;
712}