blob: a098d596e00084f91b1020b80c1debbe60b8778f [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001/*
2 * Copyright 2008, The Android Open Source Project
3 *
Mark Salyzyn66ce3e02016-09-28 10:07:20 -07004 * 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
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08007 *
Mark Salyzyn66ce3e02016-09-28 10:07:20 -07008 * http://www.apache.org/licenses/LICENSE-2.0
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08009 *
Mark Salyzyn66ce3e02016-09-28 10:07:20 -070010 * 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
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080014 * limitations under the License.
15 */
16
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080017#include <arpa/inet.h>
Mark Salyzyn66ce3e02016-09-28 10:07:20 -070018#include <errno.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080019#include <linux/if.h>
Szymon Jakubczakc88e09c2010-06-09 16:11:09 -040020#include <linux/if_ether.h>
21#include <linux/if_arp.h>
Lorenzo Colitti47ddb512011-09-26 10:49:41 -070022#include <linux/netlink.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080023#include <linux/route.h>
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +053024#include <linux/ipv6_route.h>
Lorenzo Colitti47ddb512011-09-26 10:49:41 -070025#include <linux/rtnetlink.h>
26#include <linux/sockios.h>
Mark Salyzyn66ce3e02016-09-28 10:07:20 -070027#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>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080038
39#ifdef ANDROID
40#define LOG_TAG "NetUtils"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080041#include <cutils/properties.h>
Mark Salyzyn30f991f2017-01-10 13:19:54 -080042#include <log/log.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080043#else
Steve Block8d66c492011-12-20 16:07:45 +000044#define ALOGD printf
Steve Blockae8b56c2012-01-05 22:25:38 +000045#define ALOGW printf
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080046#endif
47
Mark Salyzyn66ce3e02016-09-28 10:07:20 -070048#include "netutils/ifc.h"
49
Elliott Hughes9b828ad2015-07-30 08:47:35 -070050#if defined(__ANDROID__)
Elliott Hughes2d640c22013-11-12 13:05:01 -080051/* SIOCKILLADDR is an Android extension. */
52#define SIOCKILLADDR 0x8939
53#endif
54
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080055static int ifc_ctl_sock = -1;
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +053056static int ifc_ctl_sock6 = -1;
Dmitry Shmidtcebcd452015-10-01 13:27:56 -070057static pthread_mutex_t ifc_sock_mutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
58static pthread_mutex_t ifc_sock6_mutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080059void printerr(char *fmt, ...);
60
Robert Greenwaltb6b48ae2011-05-24 21:12:51 -070061#define DBG 0
Lorenzo Colitti47ddb512011-09-26 10:49:41 -070062#define INET_ADDRLEN 4
63#define INET6_ADDRLEN 16
Robert Greenwaltb6b48ae2011-05-24 21:12:51 -070064
Robert Greenwalt09dd8192011-02-01 15:21:21 -080065in_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
80int ipv4NetmaskToPrefixLength(in_addr_t mask)
81{
Robert Greenwalt09dd8192011-02-01 15:21:21 -080082 int prefixLength = 0;
Chris Dearman6ee3ecc2011-06-17 17:07:46 -070083 uint32_t m = (uint32_t)ntohl(mask);
Robert Greenwalt09dd8192011-02-01 15:21:21 -080084 while (m & 0x80000000) {
85 prefixLength++;
86 m = m << 1;
87 }
88 return prefixLength;
89}
90
Szymon Jakubczakc88e09c2010-06-09 16:11:09 -040091static const char *ipaddr_to_string(in_addr_t addr)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080092{
93 struct in_addr in_addr;
94
95 in_addr.s_addr = addr;
96 return inet_ntoa(in_addr);
97}
98
Lorenzo Colitti47ddb512011-09-26 10:49:41 -070099int 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 }
117
118 return ret;
119}
120
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800121int ifc_init(void)
122{
Robert Greenwaltb6b48ae2011-05-24 21:12:51 -0700123 int ret;
Dmitry Shmidtcebcd452015-10-01 13:27:56 -0700124
125 pthread_mutex_lock(&ifc_sock_mutex);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800126 if (ifc_ctl_sock == -1) {
Nick Kralevich6d3cddb2015-02-26 13:32:52 -0800127 ifc_ctl_sock = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800128 if (ifc_ctl_sock < 0) {
129 printerr("socket() failed: %s\n", strerror(errno));
130 }
131 }
Robert Greenwaltb6b48ae2011-05-24 21:12:51 -0700132
133 ret = ifc_ctl_sock < 0 ? -1 : 0;
134 if (DBG) printerr("ifc_init_returning %d", ret);
135 return ret;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800136}
137
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530138int ifc_init6(void)
139{
Dmitry Shmidtcebcd452015-10-01 13:27:56 -0700140 pthread_mutex_lock(&ifc_sock6_mutex);
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530141 if (ifc_ctl_sock6 == -1) {
Nick Kralevich6d3cddb2015-02-26 13:32:52 -0800142 ifc_ctl_sock6 = socket(AF_INET6, SOCK_DGRAM | SOCK_CLOEXEC, 0);
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530143 if (ifc_ctl_sock6 < 0) {
144 printerr("socket() failed: %s\n", strerror(errno));
145 }
146 }
147 return ifc_ctl_sock6 < 0 ? -1 : 0;
148}
149
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800150void ifc_close(void)
151{
Robert Greenwaltb6b48ae2011-05-24 21:12:51 -0700152 if (DBG) printerr("ifc_close");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800153 if (ifc_ctl_sock != -1) {
154 (void)close(ifc_ctl_sock);
155 ifc_ctl_sock = -1;
156 }
Dmitry Shmidtcebcd452015-10-01 13:27:56 -0700157 pthread_mutex_unlock(&ifc_sock_mutex);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800158}
159
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530160void ifc_close6(void)
161{
162 if (ifc_ctl_sock6 != -1) {
163 (void)close(ifc_ctl_sock6);
164 ifc_ctl_sock6 = -1;
165 }
Dmitry Shmidtcebcd452015-10-01 13:27:56 -0700166 pthread_mutex_unlock(&ifc_sock6_mutex);
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530167}
168
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800169static void ifc_init_ifr(const char *name, struct ifreq *ifr)
170{
171 memset(ifr, 0, sizeof(struct ifreq));
172 strncpy(ifr->ifr_name, name, IFNAMSIZ);
173 ifr->ifr_name[IFNAMSIZ - 1] = 0;
174}
175
176int ifc_get_hwaddr(const char *name, void *ptr)
177{
178 int r;
179 struct ifreq ifr;
180 ifc_init_ifr(name, &ifr);
181
182 r = ioctl(ifc_ctl_sock, SIOCGIFHWADDR, &ifr);
183 if(r < 0) return -1;
184
Szymon Jakubczakc88e09c2010-06-09 16:11:09 -0400185 memcpy(ptr, &ifr.ifr_hwaddr.sa_data, ETH_ALEN);
Robert Greenwaltb6b48ae2011-05-24 21:12:51 -0700186 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800187}
188
189int ifc_get_ifindex(const char *name, int *if_indexp)
190{
191 int r;
192 struct ifreq ifr;
193 ifc_init_ifr(name, &ifr);
194
195 r = ioctl(ifc_ctl_sock, SIOCGIFINDEX, &ifr);
196 if(r < 0) return -1;
197
198 *if_indexp = ifr.ifr_ifindex;
Dmitry Shmidt7d05a802011-01-24 17:10:30 -0800199 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800200}
201
202static int ifc_set_flags(const char *name, unsigned set, unsigned clr)
203{
204 struct ifreq ifr;
205 ifc_init_ifr(name, &ifr);
206
207 if(ioctl(ifc_ctl_sock, SIOCGIFFLAGS, &ifr) < 0) return -1;
208 ifr.ifr_flags = (ifr.ifr_flags & (~clr)) | set;
209 return ioctl(ifc_ctl_sock, SIOCSIFFLAGS, &ifr);
210}
211
212int ifc_up(const char *name)
213{
Robert Greenwaltb6b48ae2011-05-24 21:12:51 -0700214 int ret = ifc_set_flags(name, IFF_UP, 0);
215 if (DBG) printerr("ifc_up(%s) = %d", name, ret);
216 return ret;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800217}
218
219int ifc_down(const char *name)
220{
Robert Greenwaltb6b48ae2011-05-24 21:12:51 -0700221 int ret = ifc_set_flags(name, 0, IFF_UP);
222 if (DBG) printerr("ifc_down(%s) = %d", name, ret);
223 return ret;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800224}
225
226static void init_sockaddr_in(struct sockaddr *sa, in_addr_t addr)
227{
228 struct sockaddr_in *sin = (struct sockaddr_in *) sa;
229 sin->sin_family = AF_INET;
230 sin->sin_port = 0;
231 sin->sin_addr.s_addr = addr;
232}
233
234int ifc_set_addr(const char *name, in_addr_t addr)
235{
236 struct ifreq ifr;
Robert Greenwaltb6b48ae2011-05-24 21:12:51 -0700237 int ret;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800238
239 ifc_init_ifr(name, &ifr);
240 init_sockaddr_in(&ifr.ifr_addr, addr);
Dmitry Shmidt7d05a802011-01-24 17:10:30 -0800241
Robert Greenwaltb6b48ae2011-05-24 21:12:51 -0700242 ret = ioctl(ifc_ctl_sock, SIOCSIFADDR, &ifr);
243 if (DBG) printerr("ifc_set_addr(%s, xx) = %d", name, ret);
244 return ret;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800245}
246
Lorenzo Colitti47ddb512011-09-26 10:49:41 -0700247/*
248 * Adds or deletes an IP address on an interface.
249 *
250 * Action is one of:
251 * - RTM_NEWADDR (to add a new address)
252 * - RTM_DELADDR (to delete an existing address)
253 *
254 * Returns zero on success and negative errno on failure.
255 */
256int ifc_act_on_address(int action, const char *name, const char *address,
257 int prefixlen) {
258 int ifindex, s, len, ret;
259 struct sockaddr_storage ss;
Bjorn Andersson29299742015-12-29 11:17:05 -0800260 int saved_errno;
Lorenzo Colitti47ddb512011-09-26 10:49:41 -0700261 void *addr;
262 size_t addrlen;
263 struct {
264 struct nlmsghdr n;
265 struct ifaddrmsg r;
Erik Kline06cb8e92016-03-30 18:57:13 +0900266 // Allow for IPv6 address, headers, IPv4 broadcast addr and padding.
Lorenzo Colitti47ddb512011-09-26 10:49:41 -0700267 char attrbuf[NLMSG_ALIGN(sizeof(struct nlmsghdr)) +
268 NLMSG_ALIGN(sizeof(struct rtattr)) +
Erik Kline06cb8e92016-03-30 18:57:13 +0900269 NLMSG_ALIGN(INET6_ADDRLEN) +
270 NLMSG_ALIGN(sizeof(struct rtattr)) +
271 NLMSG_ALIGN(INET_ADDRLEN)];
Lorenzo Colitti47ddb512011-09-26 10:49:41 -0700272 } req;
273 struct rtattr *rta;
274 struct nlmsghdr *nh;
275 struct nlmsgerr *err;
276 char buf[NLMSG_ALIGN(sizeof(struct nlmsghdr)) +
277 NLMSG_ALIGN(sizeof(struct nlmsgerr)) +
278 NLMSG_ALIGN(sizeof(struct nlmsghdr))];
279
280 // Get interface ID.
281 ifindex = if_nametoindex(name);
282 if (ifindex == 0) {
283 return -errno;
284 }
285
286 // Convert string representation to sockaddr_storage.
287 ret = string_to_ip(address, &ss);
288 if (ret) {
289 return ret;
290 }
291
292 // Determine address type and length.
293 if (ss.ss_family == AF_INET) {
294 struct sockaddr_in *sin = (struct sockaddr_in *) &ss;
295 addr = &sin->sin_addr;
296 addrlen = INET_ADDRLEN;
297 } else if (ss.ss_family == AF_INET6) {
298 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) &ss;
299 addr = &sin6->sin6_addr;
300 addrlen = INET6_ADDRLEN;
301 } else {
302 return -EAFNOSUPPORT;
303 }
304
305 // Fill in netlink structures.
306 memset(&req, 0, sizeof(req));
307
308 // Netlink message header.
309 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(req.r));
310 req.n.nlmsg_type = action;
311 req.n.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
312 req.n.nlmsg_pid = getpid();
313
314 // Interface address message header.
315 req.r.ifa_family = ss.ss_family;
316 req.r.ifa_prefixlen = prefixlen;
317 req.r.ifa_index = ifindex;
318
319 // Routing attribute. Contains the actual IP address.
320 rta = (struct rtattr *) (((char *) &req) + NLMSG_ALIGN(req.n.nlmsg_len));
321 rta->rta_type = IFA_LOCAL;
322 rta->rta_len = RTA_LENGTH(addrlen);
323 req.n.nlmsg_len = NLMSG_ALIGN(req.n.nlmsg_len) + RTA_LENGTH(addrlen);
324 memcpy(RTA_DATA(rta), addr, addrlen);
325
Erik Kline06cb8e92016-03-30 18:57:13 +0900326 // Add an explicit IFA_BROADCAST for IPv4 RTM_NEWADDRs.
327 if (ss.ss_family == AF_INET && action == RTM_NEWADDR) {
328 rta = (struct rtattr *) (((char *) &req) + NLMSG_ALIGN(req.n.nlmsg_len));
329 rta->rta_type = IFA_BROADCAST;
330 rta->rta_len = RTA_LENGTH(addrlen);
331 req.n.nlmsg_len = NLMSG_ALIGN(req.n.nlmsg_len) + RTA_LENGTH(addrlen);
332 ((struct in_addr *)addr)->s_addr |= htonl((1<<(32-prefixlen))-1);
333 memcpy(RTA_DATA(rta), addr, addrlen);
334 }
335
Nick Kralevich6d3cddb2015-02-26 13:32:52 -0800336 s = socket(PF_NETLINK, SOCK_RAW | SOCK_CLOEXEC, NETLINK_ROUTE);
Bjorn Andersson29299742015-12-29 11:17:05 -0800337 if (s < 0) {
Lorenzo Colitti47ddb512011-09-26 10:49:41 -0700338 return -errno;
339 }
340
Bjorn Andersson29299742015-12-29 11:17:05 -0800341 if (send(s, &req, req.n.nlmsg_len, 0) < 0) {
342 saved_errno = errno;
343 close(s);
344 return -saved_errno;
345 }
346
Lorenzo Colitti47ddb512011-09-26 10:49:41 -0700347 len = recv(s, buf, sizeof(buf), 0);
Bjorn Andersson29299742015-12-29 11:17:05 -0800348 saved_errno = errno;
Lorenzo Colitti47ddb512011-09-26 10:49:41 -0700349 close(s);
350 if (len < 0) {
Bjorn Andersson29299742015-12-29 11:17:05 -0800351 return -saved_errno;
Lorenzo Colitti47ddb512011-09-26 10:49:41 -0700352 }
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
365int ifc_add_address(const char *name, const char *address, int prefixlen) {
366 return ifc_act_on_address(RTM_NEWADDR, name, address, prefixlen);
367}
368
369int ifc_del_address(const char *name, const char * address, int prefixlen) {
370 return ifc_act_on_address(RTM_DELADDR, name, address, prefixlen);
371}
372
373/*
374 * Clears IPv6 addresses on the specified interface.
375 */
376int ifc_clear_ipv6_addresses(const char *name) {
377 char rawaddrstr[INET6_ADDRSTRLEN], addrstr[INET6_ADDRSTRLEN];
378 unsigned int prefixlen;
379 int lasterror = 0, i, j, ret;
380 char ifname[64]; // Currently, IFNAMSIZ = 16.
381 FILE *f = fopen("/proc/net/if_inet6", "r");
382 if (!f) {
383 return -errno;
384 }
385
386 // Format:
387 // 20010db8000a0001fc446aa4b5b347ed 03 40 00 01 wlan0
388 while (fscanf(f, "%32s %*02x %02x %*02x %*02x %63s\n",
389 rawaddrstr, &prefixlen, ifname) == 3) {
390 // Is this the interface we're looking for?
391 if (strcmp(name, ifname)) {
392 continue;
393 }
394
395 // Put the colons back into the address.
396 for (i = 0, j = 0; i < 32; i++, j++) {
397 addrstr[j] = rawaddrstr[i];
398 if (i % 4 == 3) {
399 addrstr[++j] = ':';
400 }
401 }
402 addrstr[j - 1] = '\0';
403
404 // Don't delete the link-local address as well, or it will disable IPv6
405 // on the interface.
406 if (strncmp(addrstr, "fe80:", 5) == 0) {
407 continue;
408 }
409
410 ret = ifc_del_address(ifname, addrstr, prefixlen);
411 if (ret) {
Steve Block01dda202012-01-06 14:13:42 +0000412 ALOGE("Deleting address %s/%d on %s: %s", addrstr, prefixlen, ifname,
Lorenzo Colitti47ddb512011-09-26 10:49:41 -0700413 strerror(-ret));
414 lasterror = ret;
415 }
416 }
417
418 fclose(f);
419 return lasterror;
420}
421
422/*
423 * Clears IPv4 addresses on the specified interface.
424 */
425void ifc_clear_ipv4_addresses(const char *name) {
426 unsigned count, addr;
427 ifc_init();
428 for (count=0, addr=1;((addr != 0) && (count < 255)); count++) {
429 if (ifc_get_addr(name, &addr) < 0)
430 break;
431 if (addr)
432 ifc_set_addr(name, 0);
433 }
434 ifc_close();
435}
436
437/*
438 * Clears all IP addresses on the specified interface.
439 */
440int ifc_clear_addresses(const char *name) {
441 ifc_clear_ipv4_addresses(name);
442 return ifc_clear_ipv6_addresses(name);
443}
444
Szymon Jakubczakc88e09c2010-06-09 16:11:09 -0400445int ifc_set_hwaddr(const char *name, const void *ptr)
446{
Szymon Jakubczakc88e09c2010-06-09 16:11:09 -0400447 struct ifreq ifr;
448 ifc_init_ifr(name, &ifr);
449
450 ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER;
451 memcpy(&ifr.ifr_hwaddr.sa_data, ptr, ETH_ALEN);
452 return ioctl(ifc_ctl_sock, SIOCSIFHWADDR, &ifr);
453}
454
Jake Hamby6f49d5f2011-04-11 19:46:41 -0700455int ifc_set_mask(const char *name, in_addr_t mask)
456{
457 struct ifreq ifr;
Robert Greenwaltb6b48ae2011-05-24 21:12:51 -0700458 int ret;
Jake Hamby6f49d5f2011-04-11 19:46:41 -0700459
460 ifc_init_ifr(name, &ifr);
461 init_sockaddr_in(&ifr.ifr_addr, mask);
462
Robert Greenwaltb6b48ae2011-05-24 21:12:51 -0700463 ret = ioctl(ifc_ctl_sock, SIOCSIFNETMASK, &ifr);
464 if (DBG) printerr("ifc_set_mask(%s, xx) = %d", name, ret);
465 return ret;
Jake Hamby6f49d5f2011-04-11 19:46:41 -0700466}
467
Robert Greenwalt09dd8192011-02-01 15:21:21 -0800468int ifc_set_prefixLength(const char *name, int prefixLength)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800469{
470 struct ifreq ifr;
Robert Greenwalt09dd8192011-02-01 15:21:21 -0800471 // TODO - support ipv6
472 if (prefixLength > 32 || prefixLength < 0) return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800473
Robert Greenwalt09dd8192011-02-01 15:21:21 -0800474 in_addr_t mask = prefixLengthToIpv4Netmask(prefixLength);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800475 ifc_init_ifr(name, &ifr);
476 init_sockaddr_in(&ifr.ifr_addr, mask);
Dmitry Shmidt7d05a802011-01-24 17:10:30 -0800477
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800478 return ioctl(ifc_ctl_sock, SIOCSIFNETMASK, &ifr);
479}
480
Dmitry Shmidt9092b912011-01-27 14:16:20 -0800481int ifc_get_addr(const char *name, in_addr_t *addr)
482{
483 struct ifreq ifr;
484 int ret = 0;
485
486 ifc_init_ifr(name, &ifr);
487 if (addr != NULL) {
488 ret = ioctl(ifc_ctl_sock, SIOCGIFADDR, &ifr);
489 if (ret < 0) {
490 *addr = 0;
491 } else {
492 *addr = ((struct sockaddr_in*) &ifr.ifr_addr)->sin_addr.s_addr;
493 }
494 }
495 return ret;
496}
497
Robert Greenwalt09dd8192011-02-01 15:21:21 -0800498int ifc_get_info(const char *name, in_addr_t *addr, int *prefixLength, unsigned *flags)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800499{
500 struct ifreq ifr;
501 ifc_init_ifr(name, &ifr);
502
503 if (addr != NULL) {
504 if(ioctl(ifc_ctl_sock, SIOCGIFADDR, &ifr) < 0) {
505 *addr = 0;
506 } else {
507 *addr = ((struct sockaddr_in*) &ifr.ifr_addr)->sin_addr.s_addr;
508 }
509 }
Dmitry Shmidt7d05a802011-01-24 17:10:30 -0800510
Robert Greenwalt09dd8192011-02-01 15:21:21 -0800511 if (prefixLength != NULL) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800512 if(ioctl(ifc_ctl_sock, SIOCGIFNETMASK, &ifr) < 0) {
Robert Greenwalt09dd8192011-02-01 15:21:21 -0800513 *prefixLength = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800514 } else {
Chris Dearman6ee3ecc2011-06-17 17:07:46 -0700515 *prefixLength = ipv4NetmaskToPrefixLength(
Robert Greenwalt09dd8192011-02-01 15:21:21 -0800516 ((struct sockaddr_in*) &ifr.ifr_addr)->sin_addr.s_addr);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800517 }
518 }
519
520 if (flags != NULL) {
521 if(ioctl(ifc_ctl_sock, SIOCGIFFLAGS, &ifr) < 0) {
522 *flags = 0;
523 } else {
524 *flags = ifr.ifr_flags;
525 }
526 }
527
528 return 0;
529}
530
Robert Greenwalt021d0a22011-05-10 14:59:20 -0700531int 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 +0530532 struct in_addr gw)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800533{
534 struct rtentry rt;
535 int result;
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530536 in_addr_t netmask;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800537
538 memset(&rt, 0, sizeof(rt));
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530539
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800540 rt.rt_dst.sa_family = AF_INET;
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530541 rt.rt_dev = (void*) ifname;
542
Robert Greenwalt09dd8192011-02-01 15:21:21 -0800543 netmask = prefixLengthToIpv4Netmask(prefix_length);
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530544 init_sockaddr_in(&rt.rt_genmask, netmask);
545 init_sockaddr_in(&rt.rt_dst, dst.s_addr);
546 rt.rt_flags = RTF_UP;
547
548 if (prefix_length == 32) {
549 rt.rt_flags |= RTF_HOST;
550 }
551
552 if (gw.s_addr != 0) {
553 rt.rt_flags |= RTF_GATEWAY;
554 init_sockaddr_in(&rt.rt_gateway, gw.s_addr);
555 }
556
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800557 ifc_init();
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530558
559 if (ifc_ctl_sock < 0) {
Dmitry Shmidtcebcd452015-10-01 13:27:56 -0700560 ifc_close();
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530561 return -errno;
562 }
563
Robert Greenwalt021d0a22011-05-10 14:59:20 -0700564 result = ioctl(ifc_ctl_sock, action, &rt);
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530565 if (result < 0) {
566 if (errno == EEXIST) {
567 result = 0;
568 } else {
569 result = -errno;
570 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800571 }
572 ifc_close();
573 return result;
574}
575
Robert Greenwaltb6b48ae2011-05-24 21:12:51 -0700576/* deprecated - v4 only */
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530577int ifc_create_default_route(const char *name, in_addr_t gw)
578{
579 struct in_addr in_dst, in_gw;
580
581 in_dst.s_addr = 0;
582 in_gw.s_addr = gw;
583
Robert Greenwaltb6b48ae2011-05-24 21:12:51 -0700584 int ret = ifc_act_on_ipv4_route(SIOCADDRT, name, in_dst, 0, in_gw);
585 if (DBG) printerr("ifc_create_default_route(%s, %d) = %d", name, gw, ret);
586 return ret;
587}
588
Sreeram Ramachandraneec23262014-07-23 09:30:53 -0700589// Needed by code in hidden partner repositories / branches, so don't delete.
590int ifc_enable(const char *ifname)
591{
592 int result;
593
594 ifc_init();
595 result = ifc_up(ifname);
596 ifc_close();
597 return result;
598}
599
Sreeram Ramachandrane5ab3e32014-07-23 11:33:36 -0700600// Needed by code in hidden partner repositories / branches, so don't delete.
Sreeram Ramachandraneec23262014-07-23 09:30:53 -0700601int ifc_disable(const char *ifname)
602{
603 unsigned addr, count;
604 int result;
605
606 ifc_init();
607 result = ifc_down(ifname);
608
609 ifc_set_addr(ifname, 0);
610 for (count=0, addr=1;((addr != 0) && (count < 255)); count++) {
611 if (ifc_get_addr(ifname, &addr) < 0)
612 break;
613 if (addr)
614 ifc_set_addr(ifname, 0);
615 }
616
617 ifc_close();
618 return result;
619}
620
Wink Saville979203e2011-07-07 12:16:12 -0700621int ifc_reset_connections(const char *ifname, const int reset_mask)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800622{
Elliott Hughes9b828ad2015-07-30 08:47:35 -0700623#if defined(__ANDROID__)
Lorenzo Colitti6cf73ea2011-03-17 11:20:25 -0700624 int result, success;
Sreeram Ramachandran17622d02014-09-08 16:05:09 -0700625 in_addr_t myaddr = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800626 struct ifreq ifr;
Lorenzo Colitti6cf73ea2011-03-17 11:20:25 -0700627 struct in6_ifreq ifr6;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800628
Wink Saville979203e2011-07-07 12:16:12 -0700629 if (reset_mask & RESET_IPV4_ADDRESSES) {
630 /* IPv4. Clear connections on the IP address. */
631 ifc_init();
Sreeram Ramachandran17622d02014-09-08 16:05:09 -0700632 if (!(reset_mask & RESET_IGNORE_INTERFACE_ADDRESS)) {
633 ifc_get_info(ifname, &myaddr, NULL, NULL);
634 }
Wink Saville979203e2011-07-07 12:16:12 -0700635 ifc_init_ifr(ifname, &ifr);
636 init_sockaddr_in(&ifr.ifr_addr, myaddr);
637 result = ioctl(ifc_ctl_sock, SIOCKILLADDR, &ifr);
638 ifc_close();
639 } else {
640 result = 0;
Lorenzo Colitti6cf73ea2011-03-17 11:20:25 -0700641 }
Wink Saville979203e2011-07-07 12:16:12 -0700642
643 if (reset_mask & RESET_IPV6_ADDRESSES) {
644 /*
645 * IPv6. On Linux, when an interface goes down it loses all its IPv6
646 * addresses, so we don't know which connections belonged to that interface
647 * So we clear all unused IPv6 connections on the device by specifying an
648 * empty IPv6 address.
649 */
650 ifc_init6();
651 // This implicitly specifies an address of ::, i.e., kill all IPv6 sockets.
652 memset(&ifr6, 0, sizeof(ifr6));
653 success = ioctl(ifc_ctl_sock6, SIOCKILLADDR, &ifr6);
654 if (result == 0) {
655 result = success;
656 }
657 ifc_close6();
658 }
Lorenzo Colitti6cf73ea2011-03-17 11:20:25 -0700659
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800660 return result;
661#else
662 return 0;
663#endif
664}
665
Elliott Hughesc4630252015-02-03 19:56:35 +0000666/*
667 * Removes the default route for the named interface.
668 */
669int ifc_remove_default_route(const char *ifname)
670{
671 struct rtentry rt;
672 int result;
673
674 ifc_init();
675 memset(&rt, 0, sizeof(rt));
676 rt.rt_dev = (void *)ifname;
677 rt.rt_flags = RTF_UP|RTF_GATEWAY;
678 init_sockaddr_in(&rt.rt_dst, 0);
679 if ((result = ioctl(ifc_ctl_sock, SIOCDELRT, &rt)) < 0) {
680 ALOGD("failed to remove default route for %s: %s", ifname, strerror(errno));
681 }
682 ifc_close();
683 return result;
684}
685
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800686int
687ifc_configure(const char *ifname,
688 in_addr_t address,
Robert Greenwalt09dd8192011-02-01 15:21:21 -0800689 uint32_t prefixLength,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800690 in_addr_t gateway,
691 in_addr_t dns1,
692 in_addr_t dns2) {
693
694 char dns_prop_name[PROPERTY_KEY_MAX];
695
696 ifc_init();
697
698 if (ifc_up(ifname)) {
699 printerr("failed to turn on interface %s: %s\n", ifname, strerror(errno));
700 ifc_close();
701 return -1;
702 }
703 if (ifc_set_addr(ifname, address)) {
704 printerr("failed to set ipaddr %s: %s\n", ipaddr_to_string(address), strerror(errno));
705 ifc_close();
706 return -1;
707 }
Robert Greenwalt09dd8192011-02-01 15:21:21 -0800708 if (ifc_set_prefixLength(ifname, prefixLength)) {
709 printerr("failed to set prefixLength %d: %s\n", prefixLength, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800710 ifc_close();
711 return -1;
712 }
713 if (ifc_create_default_route(ifname, gateway)) {
714 printerr("failed to set default route %s: %s\n", ipaddr_to_string(gateway), strerror(errno));
715 ifc_close();
716 return -1;
717 }
718
719 ifc_close();
720
Szymon Jakubczakc88e09c2010-06-09 16:11:09 -0400721 snprintf(dns_prop_name, sizeof(dns_prop_name), "net.%s.dns1", ifname);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800722 property_set(dns_prop_name, dns1 ? ipaddr_to_string(dns1) : "");
Szymon Jakubczakc88e09c2010-06-09 16:11:09 -0400723 snprintf(dns_prop_name, sizeof(dns_prop_name), "net.%s.dns2", ifname);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800724 property_set(dns_prop_name, dns2 ? ipaddr_to_string(dns2) : "");
725
726 return 0;
727}