blob: 8212eba29c0e226c5b287536d9905d8a7dea34c0 [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);
Lorenzo Colittic3d38962019-12-02 13:09:35 +0900116 } else {
117 // Getaddrinfo has its own error codes. Convert to negative errno.
118 // There, the only thing that can reasonably happen is that the passed-in string is invalid.
119 ret = (ret == EAI_SYSTEM) ? -errno : -EINVAL;
Lorenzo Colitti47ddb512011-09-26 10:49:41 -0700120 }
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;
Bjorn Andersson29299742015-12-29 11:17:05 -0800264 int saved_errno;
Lorenzo Colitti47ddb512011-09-26 10:49:41 -0700265 void *addr;
266 size_t addrlen;
267 struct {
268 struct nlmsghdr n;
269 struct ifaddrmsg r;
Lorenzo Colittia9027582019-12-02 17:25:28 +0900270 // Allow for IPv4 or IPv6 address, headers, IPv4 broadcast address and padding.
271 char attrbuf[NLMSG_ALIGN(sizeof(struct rtattr)) + NLMSG_ALIGN(INET6_ADDRLEN) +
272 NLMSG_ALIGN(sizeof(struct rtattr)) + NLMSG_ALIGN(INET_ADDRLEN)];
Lorenzo Colitti47ddb512011-09-26 10:49:41 -0700273 } req;
274 struct rtattr *rta;
275 struct nlmsghdr *nh;
276 struct nlmsgerr *err;
Lorenzo Colitti47ddb512011-09-26 10:49:41 -0700277
278 // Get interface ID.
279 ifindex = if_nametoindex(name);
280 if (ifindex == 0) {
281 return -errno;
282 }
283
284 // Convert string representation to sockaddr_storage.
285 ret = string_to_ip(address, &ss);
286 if (ret) {
287 return ret;
288 }
289
290 // Determine address type and length.
291 if (ss.ss_family == AF_INET) {
292 struct sockaddr_in *sin = (struct sockaddr_in *) &ss;
293 addr = &sin->sin_addr;
294 addrlen = INET_ADDRLEN;
295 } else if (ss.ss_family == AF_INET6) {
296 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) &ss;
297 addr = &sin6->sin6_addr;
298 addrlen = INET6_ADDRLEN;
299 } else {
300 return -EAFNOSUPPORT;
301 }
302
303 // Fill in netlink structures.
304 memset(&req, 0, sizeof(req));
305
306 // Netlink message header.
307 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(req.r));
308 req.n.nlmsg_type = action;
309 req.n.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
310 req.n.nlmsg_pid = getpid();
311
312 // Interface address message header.
313 req.r.ifa_family = ss.ss_family;
314 req.r.ifa_prefixlen = prefixlen;
315 req.r.ifa_index = ifindex;
316
317 // Routing attribute. Contains the actual IP address.
318 rta = (struct rtattr *) (((char *) &req) + NLMSG_ALIGN(req.n.nlmsg_len));
319 rta->rta_type = IFA_LOCAL;
320 rta->rta_len = RTA_LENGTH(addrlen);
321 req.n.nlmsg_len = NLMSG_ALIGN(req.n.nlmsg_len) + RTA_LENGTH(addrlen);
322 memcpy(RTA_DATA(rta), addr, addrlen);
323
Erik Kline06cb8e92016-03-30 18:57:13 +0900324 // Add an explicit IFA_BROADCAST for IPv4 RTM_NEWADDRs.
325 if (ss.ss_family == AF_INET && action == RTM_NEWADDR) {
326 rta = (struct rtattr *) (((char *) &req) + NLMSG_ALIGN(req.n.nlmsg_len));
327 rta->rta_type = IFA_BROADCAST;
328 rta->rta_len = RTA_LENGTH(addrlen);
329 req.n.nlmsg_len = NLMSG_ALIGN(req.n.nlmsg_len) + RTA_LENGTH(addrlen);
330 ((struct in_addr *)addr)->s_addr |= htonl((1<<(32-prefixlen))-1);
331 memcpy(RTA_DATA(rta), addr, addrlen);
332 }
333
Nick Kralevich6d3cddb2015-02-26 13:32:52 -0800334 s = socket(PF_NETLINK, SOCK_RAW | SOCK_CLOEXEC, NETLINK_ROUTE);
Bjorn Andersson29299742015-12-29 11:17:05 -0800335 if (s < 0) {
Lorenzo Colitti47ddb512011-09-26 10:49:41 -0700336 return -errno;
337 }
338
Bjorn Andersson29299742015-12-29 11:17:05 -0800339 if (send(s, &req, req.n.nlmsg_len, 0) < 0) {
340 saved_errno = errno;
341 close(s);
342 return -saved_errno;
343 }
344
Lorenzo Colittia9027582019-12-02 17:25:28 +0900345 char buf[NLMSG_ALIGN(sizeof(struct nlmsgerr)) + sizeof(req)];
Lorenzo Colitti47ddb512011-09-26 10:49:41 -0700346 len = recv(s, buf, sizeof(buf), 0);
Bjorn Andersson29299742015-12-29 11:17:05 -0800347 saved_errno = errno;
Lorenzo Colitti47ddb512011-09-26 10:49:41 -0700348 close(s);
349 if (len < 0) {
Bjorn Andersson29299742015-12-29 11:17:05 -0800350 return -saved_errno;
Lorenzo Colitti47ddb512011-09-26 10:49:41 -0700351 }
352
353 // Parse the acknowledgement to find the return code.
354 nh = (struct nlmsghdr *) buf;
355 if (!NLMSG_OK(nh, (unsigned) len) || nh->nlmsg_type != NLMSG_ERROR) {
356 return -EINVAL;
357 }
358 err = NLMSG_DATA(nh);
359
360 // Return code is negative errno.
361 return err->error;
362}
363
Maciej Żenczykowskif0d10122019-10-17 16:52:33 -0700364// Returns zero on success and negative errno on failure.
Lorenzo Colitti47ddb512011-09-26 10:49:41 -0700365int ifc_add_address(const char *name, const char *address, int prefixlen) {
366 return ifc_act_on_address(RTM_NEWADDR, name, address, prefixlen);
367}
368
Maciej Żenczykowskif0d10122019-10-17 16:52:33 -0700369// Returns zero on success and negative errno on failure.
Lorenzo Colitti47ddb512011-09-26 10:49:41 -0700370int ifc_del_address(const char *name, const char * address, int prefixlen) {
371 return ifc_act_on_address(RTM_DELADDR, name, address, prefixlen);
372}
373
374/*
375 * Clears IPv6 addresses on the specified interface.
376 */
377int ifc_clear_ipv6_addresses(const char *name) {
378 char rawaddrstr[INET6_ADDRSTRLEN], addrstr[INET6_ADDRSTRLEN];
379 unsigned int prefixlen;
380 int lasterror = 0, i, j, ret;
381 char ifname[64]; // Currently, IFNAMSIZ = 16.
382 FILE *f = fopen("/proc/net/if_inet6", "r");
383 if (!f) {
384 return -errno;
385 }
386
387 // Format:
388 // 20010db8000a0001fc446aa4b5b347ed 03 40 00 01 wlan0
389 while (fscanf(f, "%32s %*02x %02x %*02x %*02x %63s\n",
390 rawaddrstr, &prefixlen, ifname) == 3) {
391 // Is this the interface we're looking for?
392 if (strcmp(name, ifname)) {
393 continue;
394 }
395
396 // Put the colons back into the address.
397 for (i = 0, j = 0; i < 32; i++, j++) {
398 addrstr[j] = rawaddrstr[i];
399 if (i % 4 == 3) {
400 addrstr[++j] = ':';
401 }
402 }
403 addrstr[j - 1] = '\0';
404
405 // Don't delete the link-local address as well, or it will disable IPv6
406 // on the interface.
407 if (strncmp(addrstr, "fe80:", 5) == 0) {
408 continue;
409 }
410
411 ret = ifc_del_address(ifname, addrstr, prefixlen);
412 if (ret) {
Steve Block01dda202012-01-06 14:13:42 +0000413 ALOGE("Deleting address %s/%d on %s: %s", addrstr, prefixlen, ifname,
Lorenzo Colitti47ddb512011-09-26 10:49:41 -0700414 strerror(-ret));
415 lasterror = ret;
416 }
417 }
418
419 fclose(f);
420 return lasterror;
421}
422
423/*
424 * Clears IPv4 addresses on the specified interface.
425 */
426void ifc_clear_ipv4_addresses(const char *name) {
427 unsigned count, addr;
428 ifc_init();
429 for (count=0, addr=1;((addr != 0) && (count < 255)); count++) {
430 if (ifc_get_addr(name, &addr) < 0)
431 break;
432 if (addr)
433 ifc_set_addr(name, 0);
434 }
435 ifc_close();
436}
437
438/*
439 * Clears all IP addresses on the specified interface.
440 */
441int ifc_clear_addresses(const char *name) {
442 ifc_clear_ipv4_addresses(name);
443 return ifc_clear_ipv6_addresses(name);
444}
445
Szymon Jakubczakc88e09c2010-06-09 16:11:09 -0400446int ifc_set_hwaddr(const char *name, const void *ptr)
447{
Szymon Jakubczakc88e09c2010-06-09 16:11:09 -0400448 struct ifreq ifr;
449 ifc_init_ifr(name, &ifr);
450
451 ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER;
452 memcpy(&ifr.ifr_hwaddr.sa_data, ptr, ETH_ALEN);
453 return ioctl(ifc_ctl_sock, SIOCSIFHWADDR, &ifr);
454}
455
Jake Hamby6f49d5f2011-04-11 19:46:41 -0700456int ifc_set_mask(const char *name, in_addr_t mask)
457{
458 struct ifreq ifr;
Robert Greenwaltb6b48ae2011-05-24 21:12:51 -0700459 int ret;
Jake Hamby6f49d5f2011-04-11 19:46:41 -0700460
461 ifc_init_ifr(name, &ifr);
462 init_sockaddr_in(&ifr.ifr_addr, mask);
463
Robert Greenwaltb6b48ae2011-05-24 21:12:51 -0700464 ret = ioctl(ifc_ctl_sock, SIOCSIFNETMASK, &ifr);
465 if (DBG) printerr("ifc_set_mask(%s, xx) = %d", name, ret);
466 return ret;
Jake Hamby6f49d5f2011-04-11 19:46:41 -0700467}
468
Robert Greenwalt09dd8192011-02-01 15:21:21 -0800469int ifc_set_prefixLength(const char *name, int prefixLength)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800470{
471 struct ifreq ifr;
Robert Greenwalt09dd8192011-02-01 15:21:21 -0800472 // TODO - support ipv6
473 if (prefixLength > 32 || prefixLength < 0) return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800474
Robert Greenwalt09dd8192011-02-01 15:21:21 -0800475 in_addr_t mask = prefixLengthToIpv4Netmask(prefixLength);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800476 ifc_init_ifr(name, &ifr);
477 init_sockaddr_in(&ifr.ifr_addr, mask);
Dmitry Shmidt7d05a802011-01-24 17:10:30 -0800478
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800479 return ioctl(ifc_ctl_sock, SIOCSIFNETMASK, &ifr);
480}
481
Dmitry Shmidt9092b912011-01-27 14:16:20 -0800482int ifc_get_addr(const char *name, in_addr_t *addr)
483{
484 struct ifreq ifr;
485 int ret = 0;
486
487 ifc_init_ifr(name, &ifr);
488 if (addr != NULL) {
489 ret = ioctl(ifc_ctl_sock, SIOCGIFADDR, &ifr);
490 if (ret < 0) {
491 *addr = 0;
492 } else {
493 *addr = ((struct sockaddr_in*) &ifr.ifr_addr)->sin_addr.s_addr;
494 }
495 }
496 return ret;
497}
498
Robert Greenwalt09dd8192011-02-01 15:21:21 -0800499int ifc_get_info(const char *name, in_addr_t *addr, int *prefixLength, unsigned *flags)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800500{
501 struct ifreq ifr;
502 ifc_init_ifr(name, &ifr);
503
504 if (addr != NULL) {
505 if(ioctl(ifc_ctl_sock, SIOCGIFADDR, &ifr) < 0) {
506 *addr = 0;
507 } else {
508 *addr = ((struct sockaddr_in*) &ifr.ifr_addr)->sin_addr.s_addr;
509 }
510 }
Dmitry Shmidt7d05a802011-01-24 17:10:30 -0800511
Robert Greenwalt09dd8192011-02-01 15:21:21 -0800512 if (prefixLength != NULL) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800513 if(ioctl(ifc_ctl_sock, SIOCGIFNETMASK, &ifr) < 0) {
Robert Greenwalt09dd8192011-02-01 15:21:21 -0800514 *prefixLength = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800515 } else {
Chris Dearman6ee3ecc2011-06-17 17:07:46 -0700516 *prefixLength = ipv4NetmaskToPrefixLength(
Robert Greenwalt09dd8192011-02-01 15:21:21 -0800517 ((struct sockaddr_in*) &ifr.ifr_addr)->sin_addr.s_addr);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800518 }
519 }
520
521 if (flags != NULL) {
522 if(ioctl(ifc_ctl_sock, SIOCGIFFLAGS, &ifr) < 0) {
523 *flags = 0;
524 } else {
525 *flags = ifr.ifr_flags;
526 }
527 }
528
529 return 0;
530}
531
Robert Greenwalt021d0a22011-05-10 14:59:20 -0700532int 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 +0530533 struct in_addr gw)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800534{
535 struct rtentry rt;
536 int result;
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530537 in_addr_t netmask;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800538
539 memset(&rt, 0, sizeof(rt));
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530540
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800541 rt.rt_dst.sa_family = AF_INET;
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530542 rt.rt_dev = (void*) ifname;
543
Robert Greenwalt09dd8192011-02-01 15:21:21 -0800544 netmask = prefixLengthToIpv4Netmask(prefix_length);
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530545 init_sockaddr_in(&rt.rt_genmask, netmask);
546 init_sockaddr_in(&rt.rt_dst, dst.s_addr);
547 rt.rt_flags = RTF_UP;
548
549 if (prefix_length == 32) {
550 rt.rt_flags |= RTF_HOST;
551 }
552
553 if (gw.s_addr != 0) {
554 rt.rt_flags |= RTF_GATEWAY;
555 init_sockaddr_in(&rt.rt_gateway, gw.s_addr);
556 }
557
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800558 ifc_init();
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530559
560 if (ifc_ctl_sock < 0) {
Dmitry Shmidtcebcd452015-10-01 13:27:56 -0700561 ifc_close();
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530562 return -errno;
563 }
564
Robert Greenwalt021d0a22011-05-10 14:59:20 -0700565 result = ioctl(ifc_ctl_sock, action, &rt);
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530566 if (result < 0) {
567 if (errno == EEXIST) {
568 result = 0;
569 } else {
570 result = -errno;
571 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800572 }
573 ifc_close();
574 return result;
575}
576
Robert Greenwaltb6b48ae2011-05-24 21:12:51 -0700577/* deprecated - v4 only */
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530578int ifc_create_default_route(const char *name, in_addr_t gw)
579{
580 struct in_addr in_dst, in_gw;
581
582 in_dst.s_addr = 0;
583 in_gw.s_addr = gw;
584
Robert Greenwaltb6b48ae2011-05-24 21:12:51 -0700585 int ret = ifc_act_on_ipv4_route(SIOCADDRT, name, in_dst, 0, in_gw);
586 if (DBG) printerr("ifc_create_default_route(%s, %d) = %d", name, gw, ret);
587 return ret;
588}
589
Sreeram Ramachandraneec23262014-07-23 09:30:53 -0700590// Needed by code in hidden partner repositories / branches, so don't delete.
591int ifc_enable(const char *ifname)
592{
593 int result;
594
595 ifc_init();
596 result = ifc_up(ifname);
597 ifc_close();
598 return result;
599}
600
Sreeram Ramachandrane5ab3e32014-07-23 11:33:36 -0700601// Needed by code in hidden partner repositories / branches, so don't delete.
Sreeram Ramachandraneec23262014-07-23 09:30:53 -0700602int ifc_disable(const char *ifname)
603{
604 unsigned addr, count;
605 int result;
606
607 ifc_init();
608 result = ifc_down(ifname);
609
610 ifc_set_addr(ifname, 0);
611 for (count=0, addr=1;((addr != 0) && (count < 255)); count++) {
612 if (ifc_get_addr(ifname, &addr) < 0)
613 break;
614 if (addr)
615 ifc_set_addr(ifname, 0);
616 }
617
618 ifc_close();
619 return result;
620}
621
Wink Saville979203e2011-07-07 12:16:12 -0700622int ifc_reset_connections(const char *ifname, const int reset_mask)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800623{
Elliott Hughes9b828ad2015-07-30 08:47:35 -0700624#if defined(__ANDROID__)
Lorenzo Colitti6cf73ea2011-03-17 11:20:25 -0700625 int result, success;
Sreeram Ramachandran17622d02014-09-08 16:05:09 -0700626 in_addr_t myaddr = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800627 struct ifreq ifr;
Lorenzo Colitti6cf73ea2011-03-17 11:20:25 -0700628 struct in6_ifreq ifr6;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800629
Wink Saville979203e2011-07-07 12:16:12 -0700630 if (reset_mask & RESET_IPV4_ADDRESSES) {
631 /* IPv4. Clear connections on the IP address. */
632 ifc_init();
Sreeram Ramachandran17622d02014-09-08 16:05:09 -0700633 if (!(reset_mask & RESET_IGNORE_INTERFACE_ADDRESS)) {
634 ifc_get_info(ifname, &myaddr, NULL, NULL);
635 }
Wink Saville979203e2011-07-07 12:16:12 -0700636 ifc_init_ifr(ifname, &ifr);
637 init_sockaddr_in(&ifr.ifr_addr, myaddr);
638 result = ioctl(ifc_ctl_sock, SIOCKILLADDR, &ifr);
639 ifc_close();
640 } else {
641 result = 0;
Lorenzo Colitti6cf73ea2011-03-17 11:20:25 -0700642 }
Wink Saville979203e2011-07-07 12:16:12 -0700643
644 if (reset_mask & RESET_IPV6_ADDRESSES) {
645 /*
646 * IPv6. On Linux, when an interface goes down it loses all its IPv6
647 * addresses, so we don't know which connections belonged to that interface
648 * So we clear all unused IPv6 connections on the device by specifying an
649 * empty IPv6 address.
650 */
651 ifc_init6();
652 // This implicitly specifies an address of ::, i.e., kill all IPv6 sockets.
653 memset(&ifr6, 0, sizeof(ifr6));
654 success = ioctl(ifc_ctl_sock6, SIOCKILLADDR, &ifr6);
655 if (result == 0) {
656 result = success;
657 }
658 ifc_close6();
659 }
Lorenzo Colitti6cf73ea2011-03-17 11:20:25 -0700660
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800661 return result;
662#else
663 return 0;
664#endif
665}
666
Elliott Hughesc4630252015-02-03 19:56:35 +0000667/*
668 * Removes the default route for the named interface.
669 */
670int ifc_remove_default_route(const char *ifname)
671{
672 struct rtentry rt;
673 int result;
674
675 ifc_init();
676 memset(&rt, 0, sizeof(rt));
677 rt.rt_dev = (void *)ifname;
678 rt.rt_flags = RTF_UP|RTF_GATEWAY;
679 init_sockaddr_in(&rt.rt_dst, 0);
680 if ((result = ioctl(ifc_ctl_sock, SIOCDELRT, &rt)) < 0) {
681 ALOGD("failed to remove default route for %s: %s", ifname, strerror(errno));
682 }
683 ifc_close();
684 return result;
685}
686
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800687int
688ifc_configure(const char *ifname,
689 in_addr_t address,
Robert Greenwalt09dd8192011-02-01 15:21:21 -0800690 uint32_t prefixLength,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800691 in_addr_t gateway,
692 in_addr_t dns1,
693 in_addr_t dns2) {
694
695 char dns_prop_name[PROPERTY_KEY_MAX];
696
697 ifc_init();
698
699 if (ifc_up(ifname)) {
700 printerr("failed to turn on interface %s: %s\n", ifname, strerror(errno));
701 ifc_close();
702 return -1;
703 }
704 if (ifc_set_addr(ifname, address)) {
705 printerr("failed to set ipaddr %s: %s\n", ipaddr_to_string(address), strerror(errno));
706 ifc_close();
707 return -1;
708 }
Robert Greenwalt09dd8192011-02-01 15:21:21 -0800709 if (ifc_set_prefixLength(ifname, prefixLength)) {
710 printerr("failed to set prefixLength %d: %s\n", prefixLength, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800711 ifc_close();
712 return -1;
713 }
714 if (ifc_create_default_route(ifname, gateway)) {
715 printerr("failed to set default route %s: %s\n", ipaddr_to_string(gateway), strerror(errno));
716 ifc_close();
717 return -1;
718 }
719
720 ifc_close();
721
Szymon Jakubczakc88e09c2010-06-09 16:11:09 -0400722 snprintf(dns_prop_name, sizeof(dns_prop_name), "net.%s.dns1", ifname);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800723 property_set(dns_prop_name, dns1 ? ipaddr_to_string(dns1) : "");
Szymon Jakubczakc88e09c2010-06-09 16:11:09 -0400724 snprintf(dns_prop_name, sizeof(dns_prop_name), "net.%s.dns2", ifname);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800725 property_set(dns_prop_name, dns2 ? ipaddr_to_string(dns2) : "");
726
727 return 0;
728}