blob: f19cb41ddc4454ada988aff819f38f803ab56bbb [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 Block9786ec42011-12-20 16:07:45 +000049#define ALOGD printf
Steve Block4f07a1f2012-01-05 22:25:38 +000050#define ALOGW printf
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080051#endif
52
53static int ifc_ctl_sock = -1;
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +053054static int ifc_ctl_sock6 = -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080055void printerr(char *fmt, ...);
56
Robert Greenwaltb6b48ae2011-05-24 21:12:51 -070057#define DBG 0
Lorenzo Colitti47ddb512011-09-26 10:49:41 -070058#define INET_ADDRLEN 4
59#define INET6_ADDRLEN 16
Robert Greenwaltb6b48ae2011-05-24 21:12:51 -070060
Robert Greenwalt09dd8192011-02-01 15:21:21 -080061in_addr_t prefixLengthToIpv4Netmask(int prefix_length)
62{
63 in_addr_t mask = 0;
64
65 // C99 (6.5.7): shifts of 32 bits have undefined results
66 if (prefix_length <= 0 || prefix_length > 32) {
67 return 0;
68 }
69
70 mask = ~mask << (32 - prefix_length);
71 mask = htonl(mask);
72
73 return mask;
74}
75
76int ipv4NetmaskToPrefixLength(in_addr_t mask)
77{
Robert Greenwalt09dd8192011-02-01 15:21:21 -080078 int prefixLength = 0;
Chris Dearmanbf3a5b22011-06-17 17:07:46 -070079 uint32_t m = (uint32_t)ntohl(mask);
Robert Greenwalt09dd8192011-02-01 15:21:21 -080080 while (m & 0x80000000) {
81 prefixLength++;
82 m = m << 1;
83 }
84 return prefixLength;
85}
86
Szymon Jakubczakc88e09c2010-06-09 16:11:09 -040087static const char *ipaddr_to_string(in_addr_t addr)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080088{
89 struct in_addr in_addr;
90
91 in_addr.s_addr = addr;
92 return inet_ntoa(in_addr);
93}
94
Lorenzo Colitti47ddb512011-09-26 10:49:41 -070095int string_to_ip(const char *string, struct sockaddr_storage *ss) {
96 struct addrinfo hints, *ai;
97 int ret;
98
99 if (ss == NULL) {
100 return -EFAULT;
101 }
102
103 memset(&hints, 0, sizeof(hints));
104 hints.ai_family = AF_UNSPEC;
105 hints.ai_flags = AI_NUMERICHOST;
106 hints.ai_socktype = SOCK_DGRAM;
107
108 ret = getaddrinfo(string, NULL, &hints, &ai);
109 if (ret == 0) {
110 memcpy(ss, ai->ai_addr, ai->ai_addrlen);
111 freeaddrinfo(ai);
112 }
113
114 return ret;
115}
116
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800117int ifc_init(void)
118{
Robert Greenwaltb6b48ae2011-05-24 21:12:51 -0700119 int ret;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800120 if (ifc_ctl_sock == -1) {
Robert Greenwaltb6b48ae2011-05-24 21:12:51 -0700121 ifc_ctl_sock = socket(AF_INET, SOCK_DGRAM, 0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800122 if (ifc_ctl_sock < 0) {
123 printerr("socket() failed: %s\n", strerror(errno));
124 }
125 }
Robert Greenwaltb6b48ae2011-05-24 21:12:51 -0700126
127 ret = ifc_ctl_sock < 0 ? -1 : 0;
128 if (DBG) printerr("ifc_init_returning %d", ret);
129 return ret;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800130}
131
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530132int ifc_init6(void)
133{
134 if (ifc_ctl_sock6 == -1) {
135 ifc_ctl_sock6 = socket(AF_INET6, SOCK_DGRAM, 0);
136 if (ifc_ctl_sock6 < 0) {
137 printerr("socket() failed: %s\n", strerror(errno));
138 }
139 }
140 return ifc_ctl_sock6 < 0 ? -1 : 0;
141}
142
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800143void ifc_close(void)
144{
Robert Greenwaltb6b48ae2011-05-24 21:12:51 -0700145 if (DBG) printerr("ifc_close");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800146 if (ifc_ctl_sock != -1) {
147 (void)close(ifc_ctl_sock);
148 ifc_ctl_sock = -1;
149 }
150}
151
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530152void ifc_close6(void)
153{
154 if (ifc_ctl_sock6 != -1) {
155 (void)close(ifc_ctl_sock6);
156 ifc_ctl_sock6 = -1;
157 }
158}
159
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800160static void ifc_init_ifr(const char *name, struct ifreq *ifr)
161{
162 memset(ifr, 0, sizeof(struct ifreq));
163 strncpy(ifr->ifr_name, name, IFNAMSIZ);
164 ifr->ifr_name[IFNAMSIZ - 1] = 0;
165}
166
167int ifc_get_hwaddr(const char *name, void *ptr)
168{
169 int r;
170 struct ifreq ifr;
171 ifc_init_ifr(name, &ifr);
172
173 r = ioctl(ifc_ctl_sock, SIOCGIFHWADDR, &ifr);
174 if(r < 0) return -1;
175
Szymon Jakubczakc88e09c2010-06-09 16:11:09 -0400176 memcpy(ptr, &ifr.ifr_hwaddr.sa_data, ETH_ALEN);
Robert Greenwaltb6b48ae2011-05-24 21:12:51 -0700177 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800178}
179
180int ifc_get_ifindex(const char *name, int *if_indexp)
181{
182 int r;
183 struct ifreq ifr;
184 ifc_init_ifr(name, &ifr);
185
186 r = ioctl(ifc_ctl_sock, SIOCGIFINDEX, &ifr);
187 if(r < 0) return -1;
188
189 *if_indexp = ifr.ifr_ifindex;
Dmitry Shmidt7d05a802011-01-24 17:10:30 -0800190 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800191}
192
193static int ifc_set_flags(const char *name, unsigned set, unsigned clr)
194{
195 struct ifreq ifr;
196 ifc_init_ifr(name, &ifr);
197
198 if(ioctl(ifc_ctl_sock, SIOCGIFFLAGS, &ifr) < 0) return -1;
199 ifr.ifr_flags = (ifr.ifr_flags & (~clr)) | set;
200 return ioctl(ifc_ctl_sock, SIOCSIFFLAGS, &ifr);
201}
202
203int ifc_up(const char *name)
204{
Robert Greenwaltb6b48ae2011-05-24 21:12:51 -0700205 int ret = ifc_set_flags(name, IFF_UP, 0);
206 if (DBG) printerr("ifc_up(%s) = %d", name, ret);
207 return ret;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800208}
209
210int ifc_down(const char *name)
211{
Robert Greenwaltb6b48ae2011-05-24 21:12:51 -0700212 int ret = ifc_set_flags(name, 0, IFF_UP);
213 if (DBG) printerr("ifc_down(%s) = %d", name, ret);
214 return ret;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800215}
216
217static void init_sockaddr_in(struct sockaddr *sa, in_addr_t addr)
218{
219 struct sockaddr_in *sin = (struct sockaddr_in *) sa;
220 sin->sin_family = AF_INET;
221 sin->sin_port = 0;
222 sin->sin_addr.s_addr = addr;
223}
224
225int ifc_set_addr(const char *name, in_addr_t addr)
226{
227 struct ifreq ifr;
Robert Greenwaltb6b48ae2011-05-24 21:12:51 -0700228 int ret;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800229
230 ifc_init_ifr(name, &ifr);
231 init_sockaddr_in(&ifr.ifr_addr, addr);
Dmitry Shmidt7d05a802011-01-24 17:10:30 -0800232
Robert Greenwaltb6b48ae2011-05-24 21:12:51 -0700233 ret = ioctl(ifc_ctl_sock, SIOCSIFADDR, &ifr);
234 if (DBG) printerr("ifc_set_addr(%s, xx) = %d", name, ret);
235 return ret;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800236}
237
Lorenzo Colitti47ddb512011-09-26 10:49:41 -0700238/*
239 * Adds or deletes an IP address on an interface.
240 *
241 * Action is one of:
242 * - RTM_NEWADDR (to add a new address)
243 * - RTM_DELADDR (to delete an existing address)
244 *
245 * Returns zero on success and negative errno on failure.
246 */
247int ifc_act_on_address(int action, const char *name, const char *address,
248 int prefixlen) {
249 int ifindex, s, len, ret;
250 struct sockaddr_storage ss;
251 void *addr;
252 size_t addrlen;
253 struct {
254 struct nlmsghdr n;
255 struct ifaddrmsg r;
256 // Allow for IPv6 address, headers, and padding.
257 char attrbuf[NLMSG_ALIGN(sizeof(struct nlmsghdr)) +
258 NLMSG_ALIGN(sizeof(struct rtattr)) +
259 NLMSG_ALIGN(INET6_ADDRLEN)];
260 } req;
261 struct rtattr *rta;
262 struct nlmsghdr *nh;
263 struct nlmsgerr *err;
264 char buf[NLMSG_ALIGN(sizeof(struct nlmsghdr)) +
265 NLMSG_ALIGN(sizeof(struct nlmsgerr)) +
266 NLMSG_ALIGN(sizeof(struct nlmsghdr))];
267
268 // Get interface ID.
269 ifindex = if_nametoindex(name);
270 if (ifindex == 0) {
271 return -errno;
272 }
273
274 // Convert string representation to sockaddr_storage.
275 ret = string_to_ip(address, &ss);
276 if (ret) {
277 return ret;
278 }
279
280 // Determine address type and length.
281 if (ss.ss_family == AF_INET) {
282 struct sockaddr_in *sin = (struct sockaddr_in *) &ss;
283 addr = &sin->sin_addr;
284 addrlen = INET_ADDRLEN;
285 } else if (ss.ss_family == AF_INET6) {
286 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) &ss;
287 addr = &sin6->sin6_addr;
288 addrlen = INET6_ADDRLEN;
289 } else {
290 return -EAFNOSUPPORT;
291 }
292
293 // Fill in netlink structures.
294 memset(&req, 0, sizeof(req));
295
296 // Netlink message header.
297 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(req.r));
298 req.n.nlmsg_type = action;
299 req.n.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
300 req.n.nlmsg_pid = getpid();
301
302 // Interface address message header.
303 req.r.ifa_family = ss.ss_family;
304 req.r.ifa_prefixlen = prefixlen;
305 req.r.ifa_index = ifindex;
306
307 // Routing attribute. Contains the actual IP address.
308 rta = (struct rtattr *) (((char *) &req) + NLMSG_ALIGN(req.n.nlmsg_len));
309 rta->rta_type = IFA_LOCAL;
310 rta->rta_len = RTA_LENGTH(addrlen);
311 req.n.nlmsg_len = NLMSG_ALIGN(req.n.nlmsg_len) + RTA_LENGTH(addrlen);
312 memcpy(RTA_DATA(rta), addr, addrlen);
313
314 s = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
315 if (send(s, &req, req.n.nlmsg_len, 0) < 0) {
316 close(s);
317 return -errno;
318 }
319
320 len = recv(s, buf, sizeof(buf), 0);
321 close(s);
322 if (len < 0) {
323 return -errno;
324 }
325
326 // Parse the acknowledgement to find the return code.
327 nh = (struct nlmsghdr *) buf;
328 if (!NLMSG_OK(nh, (unsigned) len) || nh->nlmsg_type != NLMSG_ERROR) {
329 return -EINVAL;
330 }
331 err = NLMSG_DATA(nh);
332
333 // Return code is negative errno.
334 return err->error;
335}
336
337int ifc_add_address(const char *name, const char *address, int prefixlen) {
338 return ifc_act_on_address(RTM_NEWADDR, name, address, prefixlen);
339}
340
341int ifc_del_address(const char *name, const char * address, int prefixlen) {
342 return ifc_act_on_address(RTM_DELADDR, name, address, prefixlen);
343}
344
345/*
346 * Clears IPv6 addresses on the specified interface.
347 */
348int ifc_clear_ipv6_addresses(const char *name) {
349 char rawaddrstr[INET6_ADDRSTRLEN], addrstr[INET6_ADDRSTRLEN];
350 unsigned int prefixlen;
351 int lasterror = 0, i, j, ret;
352 char ifname[64]; // Currently, IFNAMSIZ = 16.
353 FILE *f = fopen("/proc/net/if_inet6", "r");
354 if (!f) {
355 return -errno;
356 }
357
358 // Format:
359 // 20010db8000a0001fc446aa4b5b347ed 03 40 00 01 wlan0
360 while (fscanf(f, "%32s %*02x %02x %*02x %*02x %63s\n",
361 rawaddrstr, &prefixlen, ifname) == 3) {
362 // Is this the interface we're looking for?
363 if (strcmp(name, ifname)) {
364 continue;
365 }
366
367 // Put the colons back into the address.
368 for (i = 0, j = 0; i < 32; i++, j++) {
369 addrstr[j] = rawaddrstr[i];
370 if (i % 4 == 3) {
371 addrstr[++j] = ':';
372 }
373 }
374 addrstr[j - 1] = '\0';
375
376 // Don't delete the link-local address as well, or it will disable IPv6
377 // on the interface.
378 if (strncmp(addrstr, "fe80:", 5) == 0) {
379 continue;
380 }
381
382 ret = ifc_del_address(ifname, addrstr, prefixlen);
383 if (ret) {
Steve Block8aeb6e22012-01-06 14:13:42 +0000384 ALOGE("Deleting address %s/%d on %s: %s", addrstr, prefixlen, ifname,
Lorenzo Colitti47ddb512011-09-26 10:49:41 -0700385 strerror(-ret));
386 lasterror = ret;
387 }
388 }
389
390 fclose(f);
391 return lasterror;
392}
393
394/*
395 * Clears IPv4 addresses on the specified interface.
396 */
397void ifc_clear_ipv4_addresses(const char *name) {
398 unsigned count, addr;
399 ifc_init();
400 for (count=0, addr=1;((addr != 0) && (count < 255)); count++) {
401 if (ifc_get_addr(name, &addr) < 0)
402 break;
403 if (addr)
404 ifc_set_addr(name, 0);
405 }
406 ifc_close();
407}
408
409/*
410 * Clears all IP addresses on the specified interface.
411 */
412int ifc_clear_addresses(const char *name) {
413 ifc_clear_ipv4_addresses(name);
414 return ifc_clear_ipv6_addresses(name);
415}
416
Szymon Jakubczakc88e09c2010-06-09 16:11:09 -0400417int ifc_set_hwaddr(const char *name, const void *ptr)
418{
419 int r;
420 struct ifreq ifr;
421 ifc_init_ifr(name, &ifr);
422
423 ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER;
424 memcpy(&ifr.ifr_hwaddr.sa_data, ptr, ETH_ALEN);
425 return ioctl(ifc_ctl_sock, SIOCSIFHWADDR, &ifr);
426}
427
Jake Hamby6f49d5f2011-04-11 19:46:41 -0700428int ifc_set_mask(const char *name, in_addr_t mask)
429{
430 struct ifreq ifr;
Robert Greenwaltb6b48ae2011-05-24 21:12:51 -0700431 int ret;
Jake Hamby6f49d5f2011-04-11 19:46:41 -0700432
433 ifc_init_ifr(name, &ifr);
434 init_sockaddr_in(&ifr.ifr_addr, mask);
435
Robert Greenwaltb6b48ae2011-05-24 21:12:51 -0700436 ret = ioctl(ifc_ctl_sock, SIOCSIFNETMASK, &ifr);
437 if (DBG) printerr("ifc_set_mask(%s, xx) = %d", name, ret);
438 return ret;
Jake Hamby6f49d5f2011-04-11 19:46:41 -0700439}
440
Robert Greenwalt09dd8192011-02-01 15:21:21 -0800441int ifc_set_prefixLength(const char *name, int prefixLength)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800442{
443 struct ifreq ifr;
Robert Greenwalt09dd8192011-02-01 15:21:21 -0800444 // TODO - support ipv6
445 if (prefixLength > 32 || prefixLength < 0) return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800446
Robert Greenwalt09dd8192011-02-01 15:21:21 -0800447 in_addr_t mask = prefixLengthToIpv4Netmask(prefixLength);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800448 ifc_init_ifr(name, &ifr);
449 init_sockaddr_in(&ifr.ifr_addr, mask);
Dmitry Shmidt7d05a802011-01-24 17:10:30 -0800450
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800451 return ioctl(ifc_ctl_sock, SIOCSIFNETMASK, &ifr);
452}
453
Dmitry Shmidt9092b912011-01-27 14:16:20 -0800454int ifc_get_addr(const char *name, in_addr_t *addr)
455{
456 struct ifreq ifr;
457 int ret = 0;
458
459 ifc_init_ifr(name, &ifr);
460 if (addr != NULL) {
461 ret = ioctl(ifc_ctl_sock, SIOCGIFADDR, &ifr);
462 if (ret < 0) {
463 *addr = 0;
464 } else {
465 *addr = ((struct sockaddr_in*) &ifr.ifr_addr)->sin_addr.s_addr;
466 }
467 }
468 return ret;
469}
470
Robert Greenwalt09dd8192011-02-01 15:21:21 -0800471int ifc_get_info(const char *name, in_addr_t *addr, int *prefixLength, unsigned *flags)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800472{
473 struct ifreq ifr;
474 ifc_init_ifr(name, &ifr);
475
476 if (addr != NULL) {
477 if(ioctl(ifc_ctl_sock, SIOCGIFADDR, &ifr) < 0) {
478 *addr = 0;
479 } else {
480 *addr = ((struct sockaddr_in*) &ifr.ifr_addr)->sin_addr.s_addr;
481 }
482 }
Dmitry Shmidt7d05a802011-01-24 17:10:30 -0800483
Robert Greenwalt09dd8192011-02-01 15:21:21 -0800484 if (prefixLength != NULL) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800485 if(ioctl(ifc_ctl_sock, SIOCGIFNETMASK, &ifr) < 0) {
Robert Greenwalt09dd8192011-02-01 15:21:21 -0800486 *prefixLength = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800487 } else {
Chris Dearmanbf3a5b22011-06-17 17:07:46 -0700488 *prefixLength = ipv4NetmaskToPrefixLength(
Robert Greenwalt09dd8192011-02-01 15:21:21 -0800489 ((struct sockaddr_in*) &ifr.ifr_addr)->sin_addr.s_addr);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800490 }
491 }
492
493 if (flags != NULL) {
494 if(ioctl(ifc_ctl_sock, SIOCGIFFLAGS, &ifr) < 0) {
495 *flags = 0;
496 } else {
497 *flags = ifr.ifr_flags;
498 }
499 }
500
501 return 0;
502}
503
Robert Greenwalt021d0a22011-05-10 14:59:20 -0700504int 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 +0530505 struct in_addr gw)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800506{
507 struct rtentry rt;
508 int result;
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530509 in_addr_t netmask;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800510
511 memset(&rt, 0, sizeof(rt));
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530512
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800513 rt.rt_dst.sa_family = AF_INET;
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530514 rt.rt_dev = (void*) ifname;
515
Robert Greenwalt09dd8192011-02-01 15:21:21 -0800516 netmask = prefixLengthToIpv4Netmask(prefix_length);
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530517 init_sockaddr_in(&rt.rt_genmask, netmask);
518 init_sockaddr_in(&rt.rt_dst, dst.s_addr);
519 rt.rt_flags = RTF_UP;
520
521 if (prefix_length == 32) {
522 rt.rt_flags |= RTF_HOST;
523 }
524
525 if (gw.s_addr != 0) {
526 rt.rt_flags |= RTF_GATEWAY;
527 init_sockaddr_in(&rt.rt_gateway, gw.s_addr);
528 }
529
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800530 ifc_init();
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530531
532 if (ifc_ctl_sock < 0) {
533 return -errno;
534 }
535
Robert Greenwalt021d0a22011-05-10 14:59:20 -0700536 result = ioctl(ifc_ctl_sock, action, &rt);
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530537 if (result < 0) {
538 if (errno == EEXIST) {
539 result = 0;
540 } else {
541 result = -errno;
542 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800543 }
544 ifc_close();
545 return result;
546}
547
Robert Greenwaltb6b48ae2011-05-24 21:12:51 -0700548/* deprecated - v4 only */
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530549int ifc_create_default_route(const char *name, in_addr_t gw)
550{
551 struct in_addr in_dst, in_gw;
552
553 in_dst.s_addr = 0;
554 in_gw.s_addr = gw;
555
Robert Greenwaltb6b48ae2011-05-24 21:12:51 -0700556 int ret = ifc_act_on_ipv4_route(SIOCADDRT, name, in_dst, 0, in_gw);
557 if (DBG) printerr("ifc_create_default_route(%s, %d) = %d", name, gw, ret);
558 return ret;
559}
560
561/* deprecated v4-only */
562int ifc_add_host_route(const char *name, in_addr_t dst)
563{
564 struct in_addr in_dst, in_gw;
565
566 in_dst.s_addr = dst;
567 in_gw.s_addr = 0;
568
569 return ifc_act_on_ipv4_route(SIOCADDRT, name, in_dst, 32, in_gw);
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530570}
571
Mike Lockwoodfeb63e92009-07-10 17:21:17 -0400572int ifc_enable(const char *ifname)
573{
574 int result;
575
576 ifc_init();
577 result = ifc_up(ifname);
578 ifc_close();
579 return result;
580}
581
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800582int ifc_disable(const char *ifname)
583{
Dmitry Shmidt9092b912011-01-27 14:16:20 -0800584 unsigned addr, count;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800585 int result;
586
587 ifc_init();
588 result = ifc_down(ifname);
Dmitry Shmidt9092b912011-01-27 14:16:20 -0800589
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800590 ifc_set_addr(ifname, 0);
Dmitry Shmidt9092b912011-01-27 14:16:20 -0800591 for (count=0, addr=1;((addr != 0) && (count < 255)); count++) {
592 if (ifc_get_addr(ifname, &addr) < 0)
593 break;
594 if (addr)
595 ifc_set_addr(ifname, 0);
596 }
597
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800598 ifc_close();
599 return result;
600}
601
Wink Saville979203e2011-07-07 12:16:12 -0700602#define RESET_IPV4_ADDRESSES 0x01
603#define RESET_IPV6_ADDRESSES 0x02
604#define RESET_ALL_ADDRESSES (RESET_IPV4_ADDRESSES | RESET_IPV6_ADDRESSES)
605
606int ifc_reset_connections(const char *ifname, const int reset_mask)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800607{
608#ifdef HAVE_ANDROID_OS
Lorenzo Colitti6cf73ea2011-03-17 11:20:25 -0700609 int result, success;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800610 in_addr_t myaddr;
611 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();
617 ifc_get_info(ifname, &myaddr, NULL, NULL);
618 ifc_init_ifr(ifname, &ifr);
619 init_sockaddr_in(&ifr.ifr_addr, myaddr);
620 result = ioctl(ifc_ctl_sock, SIOCKILLADDR, &ifr);
621 ifc_close();
622 } else {
623 result = 0;
Lorenzo Colitti6cf73ea2011-03-17 11:20:25 -0700624 }
Wink Saville979203e2011-07-07 12:16:12 -0700625
626 if (reset_mask & RESET_IPV6_ADDRESSES) {
627 /*
628 * IPv6. On Linux, when an interface goes down it loses all its IPv6
629 * addresses, so we don't know which connections belonged to that interface
630 * So we clear all unused IPv6 connections on the device by specifying an
631 * empty IPv6 address.
632 */
633 ifc_init6();
634 // This implicitly specifies an address of ::, i.e., kill all IPv6 sockets.
635 memset(&ifr6, 0, sizeof(ifr6));
636 success = ioctl(ifc_ctl_sock6, SIOCKILLADDR, &ifr6);
637 if (result == 0) {
638 result = success;
639 }
640 ifc_close6();
641 }
Lorenzo Colitti6cf73ea2011-03-17 11:20:25 -0700642
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800643 return result;
644#else
645 return 0;
646#endif
647}
648
649/*
650 * Remove the routes associated with the named interface.
651 */
652int ifc_remove_host_routes(const char *name)
653{
654 char ifname[64];
655 in_addr_t dest, gway, mask;
656 int flags, refcnt, use, metric, mtu, win, irtt;
657 struct rtentry rt;
658 FILE *fp;
659 struct in_addr addr;
660
661 fp = fopen("/proc/net/route", "r");
662 if (fp == NULL)
663 return -1;
664 /* Skip the header line */
665 if (fscanf(fp, "%*[^\n]\n") < 0) {
666 fclose(fp);
667 return -1;
668 }
669 ifc_init();
670 for (;;) {
671 int nread = fscanf(fp, "%63s%X%X%X%d%d%d%X%d%d%d\n",
672 ifname, &dest, &gway, &flags, &refcnt, &use, &metric, &mask,
673 &mtu, &win, &irtt);
674 if (nread != 11) {
675 break;
676 }
677 if ((flags & (RTF_UP|RTF_HOST)) != (RTF_UP|RTF_HOST)
678 || strcmp(ifname, name) != 0) {
679 continue;
680 }
681 memset(&rt, 0, sizeof(rt));
682 rt.rt_dev = (void *)name;
683 init_sockaddr_in(&rt.rt_dst, dest);
684 init_sockaddr_in(&rt.rt_gateway, gway);
685 init_sockaddr_in(&rt.rt_genmask, mask);
686 addr.s_addr = dest;
687 if (ioctl(ifc_ctl_sock, SIOCDELRT, &rt) < 0) {
Steve Block9786ec42011-12-20 16:07:45 +0000688 ALOGD("failed to remove route for %s to %s: %s",
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800689 ifname, inet_ntoa(addr), strerror(errno));
690 }
691 }
692 fclose(fp);
693 ifc_close();
694 return 0;
695}
696
697/*
Robert Greenwaltb6b48ae2011-05-24 21:12:51 -0700698 * Return the address of the default gateway
699 *
700 * TODO: factor out common code from this and remove_host_routes()
701 * so that we only scan /proc/net/route in one place.
702 *
703 * DEPRECATED
704 */
705int ifc_get_default_route(const char *ifname)
706{
707 char name[64];
708 in_addr_t dest, gway, mask;
709 int flags, refcnt, use, metric, mtu, win, irtt;
710 int result;
711 FILE *fp;
712
713 fp = fopen("/proc/net/route", "r");
714 if (fp == NULL)
715 return 0;
716 /* Skip the header line */
717 if (fscanf(fp, "%*[^\n]\n") < 0) {
718 fclose(fp);
719 return 0;
720 }
721 ifc_init();
722 result = 0;
723 for (;;) {
724 int nread = fscanf(fp, "%63s%X%X%X%d%d%d%X%d%d%d\n",
725 name, &dest, &gway, &flags, &refcnt, &use, &metric, &mask,
726 &mtu, &win, &irtt);
727 if (nread != 11) {
728 break;
729 }
730 if ((flags & (RTF_UP|RTF_GATEWAY)) == (RTF_UP|RTF_GATEWAY)
731 && dest == 0
732 && strcmp(ifname, name) == 0) {
733 result = gway;
734 break;
735 }
736 }
737 fclose(fp);
738 ifc_close();
739 return result;
740}
741
742/*
743 * Sets the specified gateway as the default route for the named interface.
744 * DEPRECATED
745 */
746int ifc_set_default_route(const char *ifname, in_addr_t gateway)
747{
748 struct in_addr addr;
749 int result;
750
751 ifc_init();
752 addr.s_addr = gateway;
753 if ((result = ifc_create_default_route(ifname, gateway)) < 0) {
Steve Block9786ec42011-12-20 16:07:45 +0000754 ALOGD("failed to add %s as default route for %s: %s",
Robert Greenwaltb6b48ae2011-05-24 21:12:51 -0700755 inet_ntoa(addr), ifname, strerror(errno));
756 }
757 ifc_close();
758 return result;
759}
760
761/*
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800762 * Removes the default route for the named interface.
763 */
764int ifc_remove_default_route(const char *ifname)
765{
766 struct rtentry rt;
767 int result;
768
769 ifc_init();
770 memset(&rt, 0, sizeof(rt));
771 rt.rt_dev = (void *)ifname;
772 rt.rt_flags = RTF_UP|RTF_GATEWAY;
773 init_sockaddr_in(&rt.rt_dst, 0);
774 if ((result = ioctl(ifc_ctl_sock, SIOCDELRT, &rt)) < 0) {
Steve Block9786ec42011-12-20 16:07:45 +0000775 ALOGD("failed to remove default route for %s: %s", ifname, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800776 }
777 ifc_close();
778 return result;
779}
780
781int
782ifc_configure(const char *ifname,
783 in_addr_t address,
Robert Greenwalt09dd8192011-02-01 15:21:21 -0800784 uint32_t prefixLength,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800785 in_addr_t gateway,
786 in_addr_t dns1,
787 in_addr_t dns2) {
788
789 char dns_prop_name[PROPERTY_KEY_MAX];
790
791 ifc_init();
792
793 if (ifc_up(ifname)) {
794 printerr("failed to turn on interface %s: %s\n", ifname, strerror(errno));
795 ifc_close();
796 return -1;
797 }
798 if (ifc_set_addr(ifname, address)) {
799 printerr("failed to set ipaddr %s: %s\n", ipaddr_to_string(address), strerror(errno));
800 ifc_close();
801 return -1;
802 }
Robert Greenwalt09dd8192011-02-01 15:21:21 -0800803 if (ifc_set_prefixLength(ifname, prefixLength)) {
804 printerr("failed to set prefixLength %d: %s\n", prefixLength, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800805 ifc_close();
806 return -1;
807 }
808 if (ifc_create_default_route(ifname, gateway)) {
809 printerr("failed to set default route %s: %s\n", ipaddr_to_string(gateway), strerror(errno));
810 ifc_close();
811 return -1;
812 }
813
814 ifc_close();
815
Szymon Jakubczakc88e09c2010-06-09 16:11:09 -0400816 snprintf(dns_prop_name, sizeof(dns_prop_name), "net.%s.dns1", ifname);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800817 property_set(dns_prop_name, dns1 ? ipaddr_to_string(dns1) : "");
Szymon Jakubczakc88e09c2010-06-09 16:11:09 -0400818 snprintf(dns_prop_name, sizeof(dns_prop_name), "net.%s.dns2", ifname);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800819 property_set(dns_prop_name, dns2 ? ipaddr_to_string(dns2) : "");
820
821 return 0;
822}
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530823
Robert Greenwalt021d0a22011-05-10 14:59:20 -0700824int ifc_act_on_ipv6_route(int action, const char *ifname, struct in6_addr dst, int prefix_length,
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530825 struct in6_addr gw)
826{
827 struct in6_rtmsg rtmsg;
828 int result;
829 int ifindex;
830
831 memset(&rtmsg, 0, sizeof(rtmsg));
832
833 ifindex = if_nametoindex(ifname);
834 if (ifindex == 0) {
835 printerr("if_nametoindex() failed: interface %s\n", ifname);
836 return -ENXIO;
837 }
838
839 rtmsg.rtmsg_ifindex = ifindex;
840 rtmsg.rtmsg_dst = dst;
841 rtmsg.rtmsg_dst_len = prefix_length;
842 rtmsg.rtmsg_flags = RTF_UP;
843
844 if (prefix_length == 128) {
845 rtmsg.rtmsg_flags |= RTF_HOST;
846 }
847
848 if (memcmp(&gw, &in6addr_any, sizeof(in6addr_any))) {
849 rtmsg.rtmsg_flags |= RTF_GATEWAY;
850 rtmsg.rtmsg_gateway = gw;
851 }
852
853 ifc_init6();
854
855 if (ifc_ctl_sock6 < 0) {
856 return -errno;
857 }
858
Robert Greenwalt021d0a22011-05-10 14:59:20 -0700859 result = ioctl(ifc_ctl_sock6, action, &rtmsg);
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530860 if (result < 0) {
861 if (errno == EEXIST) {
862 result = 0;
863 } else {
864 result = -errno;
865 }
866 }
867 ifc_close6();
868 return result;
869}
870
Robert Greenwalt021d0a22011-05-10 14:59:20 -0700871int ifc_act_on_route(int action, const char *ifname, const char *dst, int prefix_length,
872 const char *gw)
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530873{
874 int ret = 0;
875 struct sockaddr_in ipv4_dst, ipv4_gw;
876 struct sockaddr_in6 ipv6_dst, ipv6_gw;
877 struct addrinfo hints, *addr_ai, *gw_ai;
878
879 memset(&hints, 0, sizeof(hints));
880 hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */
881 hints.ai_flags = AI_NUMERICHOST;
882
883 ret = getaddrinfo(dst, NULL, &hints, &addr_ai);
884
885 if (ret != 0) {
886 printerr("getaddrinfo failed: invalid address %s\n", dst);
887 return -EINVAL;
888 }
889
Robert Greenwalt021d0a22011-05-10 14:59:20 -0700890 if (gw == NULL || (strlen(gw) == 0)) {
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530891 if (addr_ai->ai_family == AF_INET6) {
892 gw = "::";
893 } else if (addr_ai->ai_family == AF_INET) {
894 gw = "0.0.0.0";
895 }
896 }
897
Robert Greenwalt021d0a22011-05-10 14:59:20 -0700898 if (((addr_ai->ai_family == AF_INET6) && (prefix_length < 0 || prefix_length > 128)) ||
899 ((addr_ai->ai_family == AF_INET) && (prefix_length < 0 || prefix_length > 32))) {
900 printerr("ifc_add_route: invalid prefix length");
901 freeaddrinfo(addr_ai);
902 return -EINVAL;
903 }
904
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530905 ret = getaddrinfo(gw, NULL, &hints, &gw_ai);
906 if (ret != 0) {
907 printerr("getaddrinfo failed: invalid gateway %s\n", gw);
908 freeaddrinfo(addr_ai);
909 return -EINVAL;
910 }
911
912 if (addr_ai->ai_family != gw_ai->ai_family) {
913 printerr("ifc_add_route: different address families: %s and %s\n", dst, gw);
914 freeaddrinfo(addr_ai);
915 freeaddrinfo(gw_ai);
916 return -EINVAL;
917 }
918
919 if (addr_ai->ai_family == AF_INET6) {
920 memcpy(&ipv6_dst, addr_ai->ai_addr, sizeof(struct sockaddr_in6));
921 memcpy(&ipv6_gw, gw_ai->ai_addr, sizeof(struct sockaddr_in6));
Robert Greenwalt021d0a22011-05-10 14:59:20 -0700922 ret = ifc_act_on_ipv6_route(action, ifname, ipv6_dst.sin6_addr,
923 prefix_length, ipv6_gw.sin6_addr);
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530924 } else if (addr_ai->ai_family == AF_INET) {
925 memcpy(&ipv4_dst, addr_ai->ai_addr, sizeof(struct sockaddr_in));
926 memcpy(&ipv4_gw, gw_ai->ai_addr, sizeof(struct sockaddr_in));
Robert Greenwalt021d0a22011-05-10 14:59:20 -0700927 ret = ifc_act_on_ipv4_route(action, ifname, ipv4_dst.sin_addr,
928 prefix_length, ipv4_gw.sin_addr);
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530929 } else {
930 printerr("ifc_add_route: getaddrinfo returned un supported address family %d\n",
931 addr_ai->ai_family);
932 ret = -EAFNOSUPPORT;
933 }
934
935 freeaddrinfo(addr_ai);
936 freeaddrinfo(gw_ai);
937 return ret;
938}
Robert Greenwalt021d0a22011-05-10 14:59:20 -0700939
Robert Greenwaltb6b48ae2011-05-24 21:12:51 -0700940/*
941 * DEPRECATED
942 */
943int ifc_add_ipv4_route(const char *ifname, struct in_addr dst, int prefix_length,
944 struct in_addr gw)
945{
946 int i =ifc_act_on_ipv4_route(SIOCADDRT, ifname, dst, prefix_length, gw);
Wink Saville0c613612011-09-20 15:53:23 -0700947 if (DBG) printerr("ifc_add_ipv4_route(%s, xx, %d, xx) = %d", ifname, prefix_length, i);
Robert Greenwaltb6b48ae2011-05-24 21:12:51 -0700948 return i;
949}
950
951/*
952 * DEPRECATED
953 */
954int ifc_add_ipv6_route(const char *ifname, struct in6_addr dst, int prefix_length,
955 struct in6_addr gw)
956{
957 return ifc_act_on_ipv6_route(SIOCADDRT, ifname, dst, prefix_length, gw);
958}
959
Robert Greenwalt021d0a22011-05-10 14:59:20 -0700960int ifc_add_route(const char *ifname, const char *dst, int prefix_length, const char *gw)
961{
Robert Greenwaltb6b48ae2011-05-24 21:12:51 -0700962 int i = ifc_act_on_route(SIOCADDRT, ifname, dst, prefix_length, gw);
Wink Saville0c613612011-09-20 15:53:23 -0700963 if (DBG) printerr("ifc_add_route(%s, %s, %d, %s) = %d", ifname, dst, prefix_length, gw, i);
Robert Greenwaltb6b48ae2011-05-24 21:12:51 -0700964 return i;
Robert Greenwalt021d0a22011-05-10 14:59:20 -0700965}
966
967int ifc_remove_route(const char *ifname, const char*dst, int prefix_length, const char *gw)
968{
969 return ifc_act_on_route(SIOCDELRT, ifname, dst, prefix_length, gw);
970}