blob: f9f62f8cedd0005dffefd0ebbc4b11605a709f18 [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>
22
23#include <sys/socket.h>
24#include <sys/select.h>
25#include <sys/types.h>
26#include <netinet/in.h>
27#include <arpa/inet.h>
Dmitry Shmidt7d05a802011-01-24 17:10:30 -080028#include <net/if.h>
Lorenzo Colitti47ddb512011-09-26 10:49:41 -070029#include <netdb.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080030
31#include <linux/if.h>
Szymon Jakubczakc88e09c2010-06-09 16:11:09 -040032#include <linux/if_ether.h>
33#include <linux/if_arp.h>
Lorenzo Colitti47ddb512011-09-26 10:49:41 -070034#include <linux/netlink.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080035#include <linux/route.h>
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +053036#include <linux/ipv6_route.h>
Lorenzo Colitti47ddb512011-09-26 10:49:41 -070037#include <linux/rtnetlink.h>
38#include <linux/sockios.h>
39
40#include "netutils/ifc.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080041
42#ifdef ANDROID
43#define LOG_TAG "NetUtils"
44#include <cutils/log.h>
45#include <cutils/properties.h>
46#else
47#include <stdio.h>
48#include <string.h>
Steve Block8d66c492011-12-20 16:07:45 +000049#define ALOGD printf
Steve Blockae8b56c2012-01-05 22:25:38 +000050#define ALOGW printf
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080051#endif
52
Elliott Hughes9b828ad2015-07-30 08:47:35 -070053#if defined(__ANDROID__)
Elliott Hughes2d640c22013-11-12 13:05:01 -080054/* SIOCKILLADDR is an Android extension. */
55#define SIOCKILLADDR 0x8939
56#endif
57
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080058static int ifc_ctl_sock = -1;
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +053059static int ifc_ctl_sock6 = -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080060void printerr(char *fmt, ...);
61
Robert Greenwaltb6b48ae2011-05-24 21:12:51 -070062#define DBG 0
Lorenzo Colitti47ddb512011-09-26 10:49:41 -070063#define INET_ADDRLEN 4
64#define INET6_ADDRLEN 16
Robert Greenwaltb6b48ae2011-05-24 21:12:51 -070065
Robert Greenwalt09dd8192011-02-01 15:21:21 -080066in_addr_t prefixLengthToIpv4Netmask(int prefix_length)
67{
68 in_addr_t mask = 0;
69
70 // C99 (6.5.7): shifts of 32 bits have undefined results
71 if (prefix_length <= 0 || prefix_length > 32) {
72 return 0;
73 }
74
75 mask = ~mask << (32 - prefix_length);
76 mask = htonl(mask);
77
78 return mask;
79}
80
81int ipv4NetmaskToPrefixLength(in_addr_t mask)
82{
Robert Greenwalt09dd8192011-02-01 15:21:21 -080083 int prefixLength = 0;
Chris Dearman6ee3ecc2011-06-17 17:07:46 -070084 uint32_t m = (uint32_t)ntohl(mask);
Robert Greenwalt09dd8192011-02-01 15:21:21 -080085 while (m & 0x80000000) {
86 prefixLength++;
87 m = m << 1;
88 }
89 return prefixLength;
90}
91
Szymon Jakubczakc88e09c2010-06-09 16:11:09 -040092static const char *ipaddr_to_string(in_addr_t addr)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080093{
94 struct in_addr in_addr;
95
96 in_addr.s_addr = addr;
97 return inet_ntoa(in_addr);
98}
99
Lorenzo Colitti47ddb512011-09-26 10:49:41 -0700100int string_to_ip(const char *string, struct sockaddr_storage *ss) {
101 struct addrinfo hints, *ai;
102 int ret;
103
104 if (ss == NULL) {
105 return -EFAULT;
106 }
107
108 memset(&hints, 0, sizeof(hints));
109 hints.ai_family = AF_UNSPEC;
110 hints.ai_flags = AI_NUMERICHOST;
111 hints.ai_socktype = SOCK_DGRAM;
112
113 ret = getaddrinfo(string, NULL, &hints, &ai);
114 if (ret == 0) {
115 memcpy(ss, ai->ai_addr, ai->ai_addrlen);
116 freeaddrinfo(ai);
117 }
118
119 return ret;
120}
121
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800122int ifc_init(void)
123{
Robert Greenwaltb6b48ae2011-05-24 21:12:51 -0700124 int ret;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800125 if (ifc_ctl_sock == -1) {
Nick Kralevich6d3cddb2015-02-26 13:32:52 -0800126 ifc_ctl_sock = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800127 if (ifc_ctl_sock < 0) {
128 printerr("socket() failed: %s\n", strerror(errno));
129 }
130 }
Robert Greenwaltb6b48ae2011-05-24 21:12:51 -0700131
132 ret = ifc_ctl_sock < 0 ? -1 : 0;
133 if (DBG) printerr("ifc_init_returning %d", ret);
134 return ret;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800135}
136
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530137int ifc_init6(void)
138{
139 if (ifc_ctl_sock6 == -1) {
Nick Kralevich6d3cddb2015-02-26 13:32:52 -0800140 ifc_ctl_sock6 = socket(AF_INET6, SOCK_DGRAM | SOCK_CLOEXEC, 0);
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530141 if (ifc_ctl_sock6 < 0) {
142 printerr("socket() failed: %s\n", strerror(errno));
143 }
144 }
145 return ifc_ctl_sock6 < 0 ? -1 : 0;
146}
147
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800148void ifc_close(void)
149{
Robert Greenwaltb6b48ae2011-05-24 21:12:51 -0700150 if (DBG) printerr("ifc_close");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800151 if (ifc_ctl_sock != -1) {
152 (void)close(ifc_ctl_sock);
153 ifc_ctl_sock = -1;
154 }
155}
156
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530157void ifc_close6(void)
158{
159 if (ifc_ctl_sock6 != -1) {
160 (void)close(ifc_ctl_sock6);
161 ifc_ctl_sock6 = -1;
162 }
163}
164
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800165static void ifc_init_ifr(const char *name, struct ifreq *ifr)
166{
167 memset(ifr, 0, sizeof(struct ifreq));
168 strncpy(ifr->ifr_name, name, IFNAMSIZ);
169 ifr->ifr_name[IFNAMSIZ - 1] = 0;
170}
171
172int ifc_get_hwaddr(const char *name, void *ptr)
173{
174 int r;
175 struct ifreq ifr;
176 ifc_init_ifr(name, &ifr);
177
178 r = ioctl(ifc_ctl_sock, SIOCGIFHWADDR, &ifr);
179 if(r < 0) return -1;
180
Szymon Jakubczakc88e09c2010-06-09 16:11:09 -0400181 memcpy(ptr, &ifr.ifr_hwaddr.sa_data, ETH_ALEN);
Robert Greenwaltb6b48ae2011-05-24 21:12:51 -0700182 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800183}
184
185int ifc_get_ifindex(const char *name, int *if_indexp)
186{
187 int r;
188 struct ifreq ifr;
189 ifc_init_ifr(name, &ifr);
190
191 r = ioctl(ifc_ctl_sock, SIOCGIFINDEX, &ifr);
192 if(r < 0) return -1;
193
194 *if_indexp = ifr.ifr_ifindex;
Dmitry Shmidt7d05a802011-01-24 17:10:30 -0800195 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800196}
197
198static int ifc_set_flags(const char *name, unsigned set, unsigned clr)
199{
200 struct ifreq ifr;
201 ifc_init_ifr(name, &ifr);
202
203 if(ioctl(ifc_ctl_sock, SIOCGIFFLAGS, &ifr) < 0) return -1;
204 ifr.ifr_flags = (ifr.ifr_flags & (~clr)) | set;
205 return ioctl(ifc_ctl_sock, SIOCSIFFLAGS, &ifr);
206}
207
208int ifc_up(const char *name)
209{
Robert Greenwaltb6b48ae2011-05-24 21:12:51 -0700210 int ret = ifc_set_flags(name, IFF_UP, 0);
211 if (DBG) printerr("ifc_up(%s) = %d", name, ret);
212 return ret;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800213}
214
215int ifc_down(const char *name)
216{
Robert Greenwaltb6b48ae2011-05-24 21:12:51 -0700217 int ret = ifc_set_flags(name, 0, IFF_UP);
218 if (DBG) printerr("ifc_down(%s) = %d", name, ret);
219 return ret;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800220}
221
222static void init_sockaddr_in(struct sockaddr *sa, in_addr_t addr)
223{
224 struct sockaddr_in *sin = (struct sockaddr_in *) sa;
225 sin->sin_family = AF_INET;
226 sin->sin_port = 0;
227 sin->sin_addr.s_addr = addr;
228}
229
230int ifc_set_addr(const char *name, in_addr_t addr)
231{
232 struct ifreq ifr;
Robert Greenwaltb6b48ae2011-05-24 21:12:51 -0700233 int ret;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800234
235 ifc_init_ifr(name, &ifr);
236 init_sockaddr_in(&ifr.ifr_addr, addr);
Dmitry Shmidt7d05a802011-01-24 17:10:30 -0800237
Robert Greenwaltb6b48ae2011-05-24 21:12:51 -0700238 ret = ioctl(ifc_ctl_sock, SIOCSIFADDR, &ifr);
239 if (DBG) printerr("ifc_set_addr(%s, xx) = %d", name, ret);
240 return ret;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800241}
242
Lorenzo Colitti47ddb512011-09-26 10:49:41 -0700243/*
244 * Adds or deletes an IP address on an interface.
245 *
246 * Action is one of:
247 * - RTM_NEWADDR (to add a new address)
248 * - RTM_DELADDR (to delete an existing address)
249 *
250 * Returns zero on success and negative errno on failure.
251 */
252int ifc_act_on_address(int action, const char *name, const char *address,
253 int prefixlen) {
254 int ifindex, s, len, ret;
255 struct sockaddr_storage ss;
Bjorn Andersson29299742015-12-29 11:17:05 -0800256 int saved_errno;
Lorenzo Colitti47ddb512011-09-26 10:49:41 -0700257 void *addr;
258 size_t addrlen;
259 struct {
260 struct nlmsghdr n;
261 struct ifaddrmsg r;
Erik Kline06cb8e92016-03-30 18:57:13 +0900262 // Allow for IPv6 address, headers, IPv4 broadcast addr and padding.
Lorenzo Colitti47ddb512011-09-26 10:49:41 -0700263 char attrbuf[NLMSG_ALIGN(sizeof(struct nlmsghdr)) +
264 NLMSG_ALIGN(sizeof(struct rtattr)) +
Erik Kline06cb8e92016-03-30 18:57:13 +0900265 NLMSG_ALIGN(INET6_ADDRLEN) +
266 NLMSG_ALIGN(sizeof(struct rtattr)) +
267 NLMSG_ALIGN(INET_ADDRLEN)];
Lorenzo Colitti47ddb512011-09-26 10:49:41 -0700268 } req;
269 struct rtattr *rta;
270 struct nlmsghdr *nh;
271 struct nlmsgerr *err;
272 char buf[NLMSG_ALIGN(sizeof(struct nlmsghdr)) +
273 NLMSG_ALIGN(sizeof(struct nlmsgerr)) +
274 NLMSG_ALIGN(sizeof(struct nlmsghdr))];
275
276 // Get interface ID.
277 ifindex = if_nametoindex(name);
278 if (ifindex == 0) {
279 return -errno;
280 }
281
282 // Convert string representation to sockaddr_storage.
283 ret = string_to_ip(address, &ss);
284 if (ret) {
285 return ret;
286 }
287
288 // Determine address type and length.
289 if (ss.ss_family == AF_INET) {
290 struct sockaddr_in *sin = (struct sockaddr_in *) &ss;
291 addr = &sin->sin_addr;
292 addrlen = INET_ADDRLEN;
293 } else if (ss.ss_family == AF_INET6) {
294 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) &ss;
295 addr = &sin6->sin6_addr;
296 addrlen = INET6_ADDRLEN;
297 } else {
298 return -EAFNOSUPPORT;
299 }
300
301 // Fill in netlink structures.
302 memset(&req, 0, sizeof(req));
303
304 // Netlink message header.
305 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(req.r));
306 req.n.nlmsg_type = action;
307 req.n.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
308 req.n.nlmsg_pid = getpid();
309
310 // Interface address message header.
311 req.r.ifa_family = ss.ss_family;
312 req.r.ifa_prefixlen = prefixlen;
313 req.r.ifa_index = ifindex;
314
315 // Routing attribute. Contains the actual IP address.
316 rta = (struct rtattr *) (((char *) &req) + NLMSG_ALIGN(req.n.nlmsg_len));
317 rta->rta_type = IFA_LOCAL;
318 rta->rta_len = RTA_LENGTH(addrlen);
319 req.n.nlmsg_len = NLMSG_ALIGN(req.n.nlmsg_len) + RTA_LENGTH(addrlen);
320 memcpy(RTA_DATA(rta), addr, addrlen);
321
Erik Kline06cb8e92016-03-30 18:57:13 +0900322 // Add an explicit IFA_BROADCAST for IPv4 RTM_NEWADDRs.
323 if (ss.ss_family == AF_INET && action == RTM_NEWADDR) {
324 rta = (struct rtattr *) (((char *) &req) + NLMSG_ALIGN(req.n.nlmsg_len));
325 rta->rta_type = IFA_BROADCAST;
326 rta->rta_len = RTA_LENGTH(addrlen);
327 req.n.nlmsg_len = NLMSG_ALIGN(req.n.nlmsg_len) + RTA_LENGTH(addrlen);
328 ((struct in_addr *)addr)->s_addr |= htonl((1<<(32-prefixlen))-1);
329 memcpy(RTA_DATA(rta), addr, addrlen);
330 }
331
Nick Kralevich6d3cddb2015-02-26 13:32:52 -0800332 s = socket(PF_NETLINK, SOCK_RAW | SOCK_CLOEXEC, NETLINK_ROUTE);
Bjorn Andersson29299742015-12-29 11:17:05 -0800333 if (s < 0) {
Lorenzo Colitti47ddb512011-09-26 10:49:41 -0700334 return -errno;
335 }
336
Bjorn Andersson29299742015-12-29 11:17:05 -0800337 if (send(s, &req, req.n.nlmsg_len, 0) < 0) {
338 saved_errno = errno;
339 close(s);
340 return -saved_errno;
341 }
342
Lorenzo Colitti47ddb512011-09-26 10:49:41 -0700343 len = recv(s, buf, sizeof(buf), 0);
Bjorn Andersson29299742015-12-29 11:17:05 -0800344 saved_errno = errno;
Lorenzo Colitti47ddb512011-09-26 10:49:41 -0700345 close(s);
346 if (len < 0) {
Bjorn Andersson29299742015-12-29 11:17:05 -0800347 return -saved_errno;
Lorenzo Colitti47ddb512011-09-26 10:49:41 -0700348 }
349
350 // Parse the acknowledgement to find the return code.
351 nh = (struct nlmsghdr *) buf;
352 if (!NLMSG_OK(nh, (unsigned) len) || nh->nlmsg_type != NLMSG_ERROR) {
353 return -EINVAL;
354 }
355 err = NLMSG_DATA(nh);
356
357 // Return code is negative errno.
358 return err->error;
359}
360
361int ifc_add_address(const char *name, const char *address, int prefixlen) {
362 return ifc_act_on_address(RTM_NEWADDR, name, address, prefixlen);
363}
364
365int ifc_del_address(const char *name, const char * address, int prefixlen) {
366 return ifc_act_on_address(RTM_DELADDR, name, address, prefixlen);
367}
368
369/*
370 * Clears IPv6 addresses on the specified interface.
371 */
372int ifc_clear_ipv6_addresses(const char *name) {
373 char rawaddrstr[INET6_ADDRSTRLEN], addrstr[INET6_ADDRSTRLEN];
374 unsigned int prefixlen;
375 int lasterror = 0, i, j, ret;
376 char ifname[64]; // Currently, IFNAMSIZ = 16.
377 FILE *f = fopen("/proc/net/if_inet6", "r");
378 if (!f) {
379 return -errno;
380 }
381
382 // Format:
383 // 20010db8000a0001fc446aa4b5b347ed 03 40 00 01 wlan0
384 while (fscanf(f, "%32s %*02x %02x %*02x %*02x %63s\n",
385 rawaddrstr, &prefixlen, ifname) == 3) {
386 // Is this the interface we're looking for?
387 if (strcmp(name, ifname)) {
388 continue;
389 }
390
391 // Put the colons back into the address.
392 for (i = 0, j = 0; i < 32; i++, j++) {
393 addrstr[j] = rawaddrstr[i];
394 if (i % 4 == 3) {
395 addrstr[++j] = ':';
396 }
397 }
398 addrstr[j - 1] = '\0';
399
400 // Don't delete the link-local address as well, or it will disable IPv6
401 // on the interface.
402 if (strncmp(addrstr, "fe80:", 5) == 0) {
403 continue;
404 }
405
406 ret = ifc_del_address(ifname, addrstr, prefixlen);
407 if (ret) {
Steve Block01dda202012-01-06 14:13:42 +0000408 ALOGE("Deleting address %s/%d on %s: %s", addrstr, prefixlen, ifname,
Lorenzo Colitti47ddb512011-09-26 10:49:41 -0700409 strerror(-ret));
410 lasterror = ret;
411 }
412 }
413
414 fclose(f);
415 return lasterror;
416}
417
418/*
419 * Clears IPv4 addresses on the specified interface.
420 */
421void ifc_clear_ipv4_addresses(const char *name) {
422 unsigned count, addr;
423 ifc_init();
424 for (count=0, addr=1;((addr != 0) && (count < 255)); count++) {
425 if (ifc_get_addr(name, &addr) < 0)
426 break;
427 if (addr)
428 ifc_set_addr(name, 0);
429 }
430 ifc_close();
431}
432
433/*
434 * Clears all IP addresses on the specified interface.
435 */
436int ifc_clear_addresses(const char *name) {
437 ifc_clear_ipv4_addresses(name);
438 return ifc_clear_ipv6_addresses(name);
439}
440
Szymon Jakubczakc88e09c2010-06-09 16:11:09 -0400441int ifc_set_hwaddr(const char *name, const void *ptr)
442{
Szymon Jakubczakc88e09c2010-06-09 16:11:09 -0400443 struct ifreq ifr;
444 ifc_init_ifr(name, &ifr);
445
446 ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER;
447 memcpy(&ifr.ifr_hwaddr.sa_data, ptr, ETH_ALEN);
448 return ioctl(ifc_ctl_sock, SIOCSIFHWADDR, &ifr);
449}
450
Jake Hamby6f49d5f2011-04-11 19:46:41 -0700451int ifc_set_mask(const char *name, in_addr_t mask)
452{
453 struct ifreq ifr;
Robert Greenwaltb6b48ae2011-05-24 21:12:51 -0700454 int ret;
Jake Hamby6f49d5f2011-04-11 19:46:41 -0700455
456 ifc_init_ifr(name, &ifr);
457 init_sockaddr_in(&ifr.ifr_addr, mask);
458
Robert Greenwaltb6b48ae2011-05-24 21:12:51 -0700459 ret = ioctl(ifc_ctl_sock, SIOCSIFNETMASK, &ifr);
460 if (DBG) printerr("ifc_set_mask(%s, xx) = %d", name, ret);
461 return ret;
Jake Hamby6f49d5f2011-04-11 19:46:41 -0700462}
463
Robert Greenwalt09dd8192011-02-01 15:21:21 -0800464int ifc_set_prefixLength(const char *name, int prefixLength)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800465{
466 struct ifreq ifr;
Robert Greenwalt09dd8192011-02-01 15:21:21 -0800467 // TODO - support ipv6
468 if (prefixLength > 32 || prefixLength < 0) return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800469
Robert Greenwalt09dd8192011-02-01 15:21:21 -0800470 in_addr_t mask = prefixLengthToIpv4Netmask(prefixLength);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800471 ifc_init_ifr(name, &ifr);
472 init_sockaddr_in(&ifr.ifr_addr, mask);
Dmitry Shmidt7d05a802011-01-24 17:10:30 -0800473
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800474 return ioctl(ifc_ctl_sock, SIOCSIFNETMASK, &ifr);
475}
476
Dmitry Shmidt9092b912011-01-27 14:16:20 -0800477int ifc_get_addr(const char *name, in_addr_t *addr)
478{
479 struct ifreq ifr;
480 int ret = 0;
481
482 ifc_init_ifr(name, &ifr);
483 if (addr != NULL) {
484 ret = ioctl(ifc_ctl_sock, SIOCGIFADDR, &ifr);
485 if (ret < 0) {
486 *addr = 0;
487 } else {
488 *addr = ((struct sockaddr_in*) &ifr.ifr_addr)->sin_addr.s_addr;
489 }
490 }
491 return ret;
492}
493
Robert Greenwalt09dd8192011-02-01 15:21:21 -0800494int ifc_get_info(const char *name, in_addr_t *addr, int *prefixLength, unsigned *flags)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800495{
496 struct ifreq ifr;
497 ifc_init_ifr(name, &ifr);
498
499 if (addr != NULL) {
500 if(ioctl(ifc_ctl_sock, SIOCGIFADDR, &ifr) < 0) {
501 *addr = 0;
502 } else {
503 *addr = ((struct sockaddr_in*) &ifr.ifr_addr)->sin_addr.s_addr;
504 }
505 }
Dmitry Shmidt7d05a802011-01-24 17:10:30 -0800506
Robert Greenwalt09dd8192011-02-01 15:21:21 -0800507 if (prefixLength != NULL) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800508 if(ioctl(ifc_ctl_sock, SIOCGIFNETMASK, &ifr) < 0) {
Robert Greenwalt09dd8192011-02-01 15:21:21 -0800509 *prefixLength = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800510 } else {
Chris Dearman6ee3ecc2011-06-17 17:07:46 -0700511 *prefixLength = ipv4NetmaskToPrefixLength(
Robert Greenwalt09dd8192011-02-01 15:21:21 -0800512 ((struct sockaddr_in*) &ifr.ifr_addr)->sin_addr.s_addr);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800513 }
514 }
515
516 if (flags != NULL) {
517 if(ioctl(ifc_ctl_sock, SIOCGIFFLAGS, &ifr) < 0) {
518 *flags = 0;
519 } else {
520 *flags = ifr.ifr_flags;
521 }
522 }
523
524 return 0;
525}
526
Robert Greenwalt021d0a22011-05-10 14:59:20 -0700527int 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 +0530528 struct in_addr gw)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800529{
530 struct rtentry rt;
531 int result;
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530532 in_addr_t netmask;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800533
534 memset(&rt, 0, sizeof(rt));
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530535
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800536 rt.rt_dst.sa_family = AF_INET;
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530537 rt.rt_dev = (void*) ifname;
538
Robert Greenwalt09dd8192011-02-01 15:21:21 -0800539 netmask = prefixLengthToIpv4Netmask(prefix_length);
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530540 init_sockaddr_in(&rt.rt_genmask, netmask);
541 init_sockaddr_in(&rt.rt_dst, dst.s_addr);
542 rt.rt_flags = RTF_UP;
543
544 if (prefix_length == 32) {
545 rt.rt_flags |= RTF_HOST;
546 }
547
548 if (gw.s_addr != 0) {
549 rt.rt_flags |= RTF_GATEWAY;
550 init_sockaddr_in(&rt.rt_gateway, gw.s_addr);
551 }
552
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800553 ifc_init();
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530554
555 if (ifc_ctl_sock < 0) {
556 return -errno;
557 }
558
Robert Greenwalt021d0a22011-05-10 14:59:20 -0700559 result = ioctl(ifc_ctl_sock, action, &rt);
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530560 if (result < 0) {
561 if (errno == EEXIST) {
562 result = 0;
563 } else {
564 result = -errno;
565 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800566 }
567 ifc_close();
568 return result;
569}
570
Robert Greenwaltb6b48ae2011-05-24 21:12:51 -0700571/* deprecated - v4 only */
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530572int ifc_create_default_route(const char *name, in_addr_t gw)
573{
574 struct in_addr in_dst, in_gw;
575
576 in_dst.s_addr = 0;
577 in_gw.s_addr = gw;
578
Robert Greenwaltb6b48ae2011-05-24 21:12:51 -0700579 int ret = ifc_act_on_ipv4_route(SIOCADDRT, name, in_dst, 0, in_gw);
580 if (DBG) printerr("ifc_create_default_route(%s, %d) = %d", name, gw, ret);
581 return ret;
582}
583
Sreeram Ramachandraneec23262014-07-23 09:30:53 -0700584// Needed by code in hidden partner repositories / branches, so don't delete.
585int ifc_enable(const char *ifname)
586{
587 int result;
588
589 ifc_init();
590 result = ifc_up(ifname);
591 ifc_close();
592 return result;
593}
594
Sreeram Ramachandrane5ab3e32014-07-23 11:33:36 -0700595// Needed by code in hidden partner repositories / branches, so don't delete.
Sreeram Ramachandraneec23262014-07-23 09:30:53 -0700596int ifc_disable(const char *ifname)
597{
598 unsigned addr, count;
599 int result;
600
601 ifc_init();
602 result = ifc_down(ifname);
603
604 ifc_set_addr(ifname, 0);
605 for (count=0, addr=1;((addr != 0) && (count < 255)); count++) {
606 if (ifc_get_addr(ifname, &addr) < 0)
607 break;
608 if (addr)
609 ifc_set_addr(ifname, 0);
610 }
611
612 ifc_close();
613 return result;
614}
615
Wink Saville979203e2011-07-07 12:16:12 -0700616int ifc_reset_connections(const char *ifname, const int reset_mask)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800617{
Elliott Hughes9b828ad2015-07-30 08:47:35 -0700618#if defined(__ANDROID__)
Lorenzo Colitti6cf73ea2011-03-17 11:20:25 -0700619 int result, success;
Sreeram Ramachandran17622d02014-09-08 16:05:09 -0700620 in_addr_t myaddr = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800621 struct ifreq ifr;
Lorenzo Colitti6cf73ea2011-03-17 11:20:25 -0700622 struct in6_ifreq ifr6;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800623
Wink Saville979203e2011-07-07 12:16:12 -0700624 if (reset_mask & RESET_IPV4_ADDRESSES) {
625 /* IPv4. Clear connections on the IP address. */
626 ifc_init();
Sreeram Ramachandran17622d02014-09-08 16:05:09 -0700627 if (!(reset_mask & RESET_IGNORE_INTERFACE_ADDRESS)) {
628 ifc_get_info(ifname, &myaddr, NULL, NULL);
629 }
Wink Saville979203e2011-07-07 12:16:12 -0700630 ifc_init_ifr(ifname, &ifr);
631 init_sockaddr_in(&ifr.ifr_addr, myaddr);
632 result = ioctl(ifc_ctl_sock, SIOCKILLADDR, &ifr);
633 ifc_close();
634 } else {
635 result = 0;
Lorenzo Colitti6cf73ea2011-03-17 11:20:25 -0700636 }
Wink Saville979203e2011-07-07 12:16:12 -0700637
638 if (reset_mask & RESET_IPV6_ADDRESSES) {
639 /*
640 * IPv6. On Linux, when an interface goes down it loses all its IPv6
641 * addresses, so we don't know which connections belonged to that interface
642 * So we clear all unused IPv6 connections on the device by specifying an
643 * empty IPv6 address.
644 */
645 ifc_init6();
646 // This implicitly specifies an address of ::, i.e., kill all IPv6 sockets.
647 memset(&ifr6, 0, sizeof(ifr6));
648 success = ioctl(ifc_ctl_sock6, SIOCKILLADDR, &ifr6);
649 if (result == 0) {
650 result = success;
651 }
652 ifc_close6();
653 }
Lorenzo Colitti6cf73ea2011-03-17 11:20:25 -0700654
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800655 return result;
656#else
657 return 0;
658#endif
659}
660
Elliott Hughesc4630252015-02-03 19:56:35 +0000661/*
662 * Removes the default route for the named interface.
663 */
664int ifc_remove_default_route(const char *ifname)
665{
666 struct rtentry rt;
667 int result;
668
669 ifc_init();
670 memset(&rt, 0, sizeof(rt));
671 rt.rt_dev = (void *)ifname;
672 rt.rt_flags = RTF_UP|RTF_GATEWAY;
673 init_sockaddr_in(&rt.rt_dst, 0);
674 if ((result = ioctl(ifc_ctl_sock, SIOCDELRT, &rt)) < 0) {
675 ALOGD("failed to remove default route for %s: %s", ifname, strerror(errno));
676 }
677 ifc_close();
678 return result;
679}
680
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800681int
682ifc_configure(const char *ifname,
683 in_addr_t address,
Robert Greenwalt09dd8192011-02-01 15:21:21 -0800684 uint32_t prefixLength,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800685 in_addr_t gateway,
686 in_addr_t dns1,
687 in_addr_t dns2) {
688
689 char dns_prop_name[PROPERTY_KEY_MAX];
690
691 ifc_init();
692
693 if (ifc_up(ifname)) {
694 printerr("failed to turn on interface %s: %s\n", ifname, strerror(errno));
695 ifc_close();
696 return -1;
697 }
698 if (ifc_set_addr(ifname, address)) {
699 printerr("failed to set ipaddr %s: %s\n", ipaddr_to_string(address), strerror(errno));
700 ifc_close();
701 return -1;
702 }
Robert Greenwalt09dd8192011-02-01 15:21:21 -0800703 if (ifc_set_prefixLength(ifname, prefixLength)) {
704 printerr("failed to set prefixLength %d: %s\n", prefixLength, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800705 ifc_close();
706 return -1;
707 }
708 if (ifc_create_default_route(ifname, gateway)) {
709 printerr("failed to set default route %s: %s\n", ipaddr_to_string(gateway), strerror(errno));
710 ifc_close();
711 return -1;
712 }
713
714 ifc_close();
715
Szymon Jakubczakc88e09c2010-06-09 16:11:09 -0400716 snprintf(dns_prop_name, sizeof(dns_prop_name), "net.%s.dns1", ifname);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800717 property_set(dns_prop_name, dns1 ? ipaddr_to_string(dns1) : "");
Szymon Jakubczakc88e09c2010-06-09 16:11:09 -0400718 snprintf(dns_prop_name, sizeof(dns_prop_name), "net.%s.dns2", ifname);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800719 property_set(dns_prop_name, dns2 ? ipaddr_to_string(dns2) : "");
720
721 return 0;
722}