blob: 93b669fbbf8ca1ba4c31a4e384ccb1de004776e8 [file] [log] [blame]
Lorenzo Colitti1e299c62017-02-27 17:16:10 +09001/*
2 * Copyright 2017 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 *
Lorenzo Colitti48dec382020-05-29 21:34:47 +090016 * tun_interface.cpp - creates tun or tap interfaces for testing purposes
Lorenzo Colitti1e299c62017-02-27 17:16:10 +090017 */
18
Lorenzo Colitti8a9f1ad2019-02-26 00:30:18 +090019#include <string>
20
Lorenzo Colitti1e299c62017-02-27 17:16:10 +090021#include <fcntl.h>
Lorenzo Colitti54520a02018-02-09 18:39:16 +090022#include <linux/if.h>
23#include <linux/if_tun.h>
Lorenzo Colitti8a9f1ad2019-02-26 00:30:18 +090024#include <linux/netlink.h>
25#include <linux/rtnetlink.h>
Lorenzo Colitti54520a02018-02-09 18:39:16 +090026#include <net/if.h>
Lorenzo Colitti8a9f1ad2019-02-26 00:30:18 +090027#include <netdb.h>
Lorenzo Colitti54520a02018-02-09 18:39:16 +090028#include <netinet/in.h>
Lorenzo Colitti8a9f1ad2019-02-26 00:30:18 +090029#include <stdlib.h>
Lorenzo Colitti1e299c62017-02-27 17:16:10 +090030#include <sys/ioctl.h>
31#include <sys/socket.h>
32#include <sys/stat.h>
33#include <sys/types.h>
Lorenzo Colitti8a9f1ad2019-02-26 00:30:18 +090034#include <unistd.h>
Lorenzo Colitti1e299c62017-02-27 17:16:10 +090035
36#include <android-base/stringprintf.h>
37#include <android-base/strings.h>
Lorenzo Colitti8a9f1ad2019-02-26 00:30:18 +090038#include <android-base/unique_fd.h>
Lorenzo Colitti1e299c62017-02-27 17:16:10 +090039#include <netutils/ifc.h>
40
41#include "tun_interface.h"
42
Maciej Żenczykowski94db6582020-01-27 21:47:06 -080043#define TUN_DEV "/dev/net/tun"
Lorenzo Colitti1e299c62017-02-27 17:16:10 +090044
45using android::base::StringPrintf;
Lorenzo Colitti8a9f1ad2019-02-26 00:30:18 +090046using android::base::unique_fd;
Lorenzo Colitti1e299c62017-02-27 17:16:10 +090047
48namespace android {
49namespace net {
50
Lorenzo Colitti48dec382020-05-29 21:34:47 +090051int TunInterface::init(const std::string& ifName, bool isTap) {
Lorenzo Colitti1e299c62017-02-27 17:16:10 +090052 // Generate a random ULA address pair.
53 arc4random_buf(&mSrcAddr, sizeof(mSrcAddr));
54 mSrcAddr.s6_addr[0] = 0xfd;
55 memcpy(&mDstAddr, &mSrcAddr, sizeof(mDstAddr));
56 mDstAddr.s6_addr[15] ^= 1;
57
58 // Convert the addresses to strings because that's what ifc_add_address takes.
59 char srcStr[INET6_ADDRSTRLEN], dstStr[INET6_ADDRSTRLEN];
60 sockaddr_in6 src6 = { .sin6_family = AF_INET6, .sin6_addr = mSrcAddr, };
61 sockaddr_in6 dst6 = { .sin6_family = AF_INET6, .sin6_addr = mDstAddr, };
62 int flags = NI_NUMERICHOST;
Yi Kongbdfd57e2018-07-25 13:26:10 -070063 if (getnameinfo((sockaddr *) &src6, sizeof(src6), srcStr, sizeof(srcStr), nullptr, 0, flags) ||
64 getnameinfo((sockaddr *) &dst6, sizeof(dst6), dstStr, sizeof(dstStr), nullptr, 0, flags)) {
Lorenzo Colitti1e299c62017-02-27 17:16:10 +090065 return -EINVAL;
66 }
67
Luke Huang531f5d32018-08-03 15:19:05 +080068 // Create a tun interface with a name based on a random number.
69 // In order to fit the size of interface alert name , resize ifname to 9
70 // Alert name format in netd: ("%sAlert", ifname)
71 // Limitation in kernel: char name[15] in struct xt_quota_mtinfo2
Luke Huang19b49c52018-10-22 12:12:05 +090072
73 // Note that this form of alert doesn't actually appear to be used for interface alerts.
74 // It can only be created by BandwidthController::setInterfaceAlert, but that appears to have no
75 // actual callers in the framework, because mActiveAlerts is always empty.
76 // TODO: remove setInterfaceAlert and use a longer interface name.
Luke Huang528af602018-08-29 19:06:05 +080077 mIfName = ifName;
78 if (mIfName.empty()) {
79 mIfName = StringPrintf("netd%x", arc4random());
80 }
Luke Huang531f5d32018-08-03 15:19:05 +080081 mIfName.resize(9);
82
Lorenzo Colitti48dec382020-05-29 21:34:47 +090083 flags = IFF_NO_PI | (isTap ? IFF_TAP : IFF_TUN);
Lorenzo Colitti1e299c62017-02-27 17:16:10 +090084 struct ifreq ifr = {
Lorenzo Colitti48dec382020-05-29 21:34:47 +090085 .ifr_ifru = {.ifru_flags = static_cast<short>(flags)},
Lorenzo Colitti1e299c62017-02-27 17:16:10 +090086 };
Lorenzo Colitti54520a02018-02-09 18:39:16 +090087 strlcpy(ifr.ifr_name, mIfName.c_str(), sizeof(ifr.ifr_name));
Lorenzo Colitti1e299c62017-02-27 17:16:10 +090088
89 mFd = open(TUN_DEV, O_RDWR | O_NONBLOCK | O_CLOEXEC);
90 if (mFd == -1) return -errno;
91
92 int ret = ioctl(mFd, TUNSETIFF, &ifr, sizeof(ifr));
93 if (ret == -1) {
94 ret = -errno;
95 close(mFd);
96 return ret;
97 }
98
Lorenzo Colitti8a9f1ad2019-02-26 00:30:18 +090099 mIfIndex = if_nametoindex(ifr.ifr_name);
100
101 if (addAddress(srcStr, 64) || addAddress(dstStr, 64)) {
Lorenzo Colitti1e299c62017-02-27 17:16:10 +0900102 ret = -errno;
103 close(mFd);
104 return ret;
105 }
106
Luke Huang531f5d32018-08-03 15:19:05 +0800107 if (int ret = ifc_enable(ifr.ifr_name)) {
108 return ret;
109 }
Lorenzo Colitti48dec382020-05-29 21:34:47 +0900110
Lorenzo Colitti1e299c62017-02-27 17:16:10 +0900111 return 0;
112}
113
114void TunInterface::destroy() {
115 if (mFd != -1) {
Luke Huang531f5d32018-08-03 15:19:05 +0800116 ifc_disable(mIfName.c_str());
Lorenzo Colitti1e299c62017-02-27 17:16:10 +0900117 close(mFd);
118 mFd = -1;
119 }
120}
121
Lorenzo Colitti8a9f1ad2019-02-26 00:30:18 +0900122int TunInterface::addAddress(const std::string& addr, int prefixlen) {
123 // Wait for an RTM_NEWADDR indicating that the address has been created.
124 // This is because IPv6 addresses, even addresses that are optimistic or created with
125 // IFA_F_NODAD, are not immediately usable when the netlink ACK returns.
126 // This is not generally necessary in device code because the framework hears about IP addresses
127 // asynchronously via netlink, but it is necessary to ensure tests aren't flaky.
128 unique_fd s(socket(AF_NETLINK, SOCK_DGRAM | SOCK_CLOEXEC, 0));
129 if (s == -1) return -errno;
130
131 sockaddr_nl groups = {.nl_family = AF_NETLINK,
132 .nl_groups = RTMGRP_IPV4_IFADDR | RTMGRP_IPV6_IFADDR};
133 if (bind(s, reinterpret_cast<sockaddr*>(&groups), sizeof(groups)) == -1) return -errno;
134
135 sockaddr_nl kernel = {.nl_family = AF_NETLINK};
136 if (connect(s, reinterpret_cast<sockaddr*>(&kernel), sizeof(kernel)) == -1) return -errno;
137
138 // Wait up to 200ms for address to arrive.
139 timeval timeout = {.tv_usec = 200 * 1000};
140 if (setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)) == -1) return -errno;
141
Maciej Żenczykowskid2c761d2020-01-21 23:49:48 -0800142 if (int ret = ifc_act_on_address(RTM_NEWADDR, mIfName.c_str(), addr.c_str(), prefixlen,
143 /*nodad*/ true))
144 return ret;
Lorenzo Colitti8a9f1ad2019-02-26 00:30:18 +0900145
146 int family;
147 size_t addrlen;
148 union {
149 in_addr ip4;
150 in6_addr ip6;
151 } ip;
Maciej Żenczykowskibe437472019-04-02 16:21:30 -0700152 if (addr.find(':') != std::string::npos) {
Lorenzo Colitti8a9f1ad2019-02-26 00:30:18 +0900153 family = AF_INET6;
154 inet_pton(AF_INET6, addr.c_str(), &ip.ip6);
155 addrlen = sizeof(ip.ip6);
156 } else {
157 family = AF_INET;
158 inet_pton(AF_INET, addr.c_str(), &ip.ip4);
159 addrlen = sizeof(ip.ip4);
160 }
161
162 while (1) {
163 char buf[4096];
164 ssize_t len = recv(s, buf, sizeof(buf), 0);
165
166 if (len == -1) break;
167 if (len < static_cast<ssize_t>(NLMSG_SPACE(sizeof(ifaddrmsg)))) continue;
168
169 nlmsghdr* nlmsg = reinterpret_cast<nlmsghdr*>(buf);
170 if (nlmsg->nlmsg_type != RTM_NEWADDR) continue;
171
172 ifaddrmsg* ifaddr = reinterpret_cast<ifaddrmsg*>(NLMSG_DATA(nlmsg));
173 if (ifaddr->ifa_family != family) continue;
174 if (ifaddr->ifa_prefixlen != prefixlen) continue;
175 if (ifaddr->ifa_index != static_cast<uint32_t>(mIfIndex)) continue;
176
177 int ifalen = IFA_PAYLOAD(nlmsg);
178 for (rtattr* rta = IFA_RTA(ifaddr); RTA_OK(rta, ifalen); rta = RTA_NEXT(rta, ifalen)) {
179 if (rta->rta_type != IFA_LOCAL && rta->rta_type != IFA_ADDRESS) continue;
180 if (RTA_PAYLOAD(rta) != addrlen) continue;
181 if (!memcmp(RTA_DATA(rta), &ip, addrlen)) {
182 return 0;
183 }
184 }
185 }
186
187 return -errno;
188}
189
Lorenzo Colitti1e299c62017-02-27 17:16:10 +0900190} // namespace net
191} // namespace android