Lorenzo Colitti | 1e299c6 | 2017-02-27 17:16:10 +0900 | [diff] [blame] | 1 | /* |
| 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 | * |
| 16 | * tun_interface.cpp - creates tun interfaces for testing purposes |
| 17 | */ |
| 18 | |
| 19 | #include <fcntl.h> |
| 20 | #include <netdb.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <unistd.h> |
Lorenzo Colitti | 54520a0 | 2018-02-09 18:39:16 +0900 | [diff] [blame] | 23 | #include <linux/if.h> |
| 24 | #include <linux/if_tun.h> |
| 25 | #include <net/if.h> |
| 26 | #include <netinet/in.h> |
Lorenzo Colitti | 1e299c6 | 2017-02-27 17:16:10 +0900 | [diff] [blame] | 27 | #include <sys/ioctl.h> |
| 28 | #include <sys/socket.h> |
| 29 | #include <sys/stat.h> |
| 30 | #include <sys/types.h> |
Lorenzo Colitti | 1e299c6 | 2017-02-27 17:16:10 +0900 | [diff] [blame] | 31 | |
| 32 | #include <android-base/stringprintf.h> |
| 33 | #include <android-base/strings.h> |
| 34 | #include <netutils/ifc.h> |
| 35 | |
| 36 | #include "tun_interface.h" |
| 37 | |
| 38 | #define TUN_DEV "/dev/tun" |
| 39 | |
| 40 | using android::base::StringPrintf; |
| 41 | |
| 42 | namespace android { |
| 43 | namespace net { |
| 44 | |
| 45 | int TunInterface::init() { |
| 46 | // Generate a random ULA address pair. |
| 47 | arc4random_buf(&mSrcAddr, sizeof(mSrcAddr)); |
| 48 | mSrcAddr.s6_addr[0] = 0xfd; |
| 49 | memcpy(&mDstAddr, &mSrcAddr, sizeof(mDstAddr)); |
| 50 | mDstAddr.s6_addr[15] ^= 1; |
| 51 | |
| 52 | // Convert the addresses to strings because that's what ifc_add_address takes. |
| 53 | char srcStr[INET6_ADDRSTRLEN], dstStr[INET6_ADDRSTRLEN]; |
| 54 | sockaddr_in6 src6 = { .sin6_family = AF_INET6, .sin6_addr = mSrcAddr, }; |
| 55 | sockaddr_in6 dst6 = { .sin6_family = AF_INET6, .sin6_addr = mDstAddr, }; |
| 56 | int flags = NI_NUMERICHOST; |
Yi Kong | bdfd57e | 2018-07-25 13:26:10 -0700 | [diff] [blame] | 57 | if (getnameinfo((sockaddr *) &src6, sizeof(src6), srcStr, sizeof(srcStr), nullptr, 0, flags) || |
| 58 | getnameinfo((sockaddr *) &dst6, sizeof(dst6), dstStr, sizeof(dstStr), nullptr, 0, flags)) { |
Lorenzo Colitti | 1e299c6 | 2017-02-27 17:16:10 +0900 | [diff] [blame] | 59 | return -EINVAL; |
| 60 | } |
| 61 | |
Luke Huang | 531f5d3 | 2018-08-03 15:19:05 +0800 | [diff] [blame^] | 62 | // Create a tun interface with a name based on a random number. |
| 63 | // In order to fit the size of interface alert name , resize ifname to 9 |
| 64 | // Alert name format in netd: ("%sAlert", ifname) |
| 65 | // Limitation in kernel: char name[15] in struct xt_quota_mtinfo2 |
| 66 | mIfName = StringPrintf("netd%x", arc4random()); |
| 67 | mIfName.resize(9); |
| 68 | |
Lorenzo Colitti | 1e299c6 | 2017-02-27 17:16:10 +0900 | [diff] [blame] | 69 | struct ifreq ifr = { |
| 70 | .ifr_ifru = { .ifru_flags = IFF_TUN }, |
| 71 | }; |
Lorenzo Colitti | 54520a0 | 2018-02-09 18:39:16 +0900 | [diff] [blame] | 72 | strlcpy(ifr.ifr_name, mIfName.c_str(), sizeof(ifr.ifr_name)); |
Lorenzo Colitti | 1e299c6 | 2017-02-27 17:16:10 +0900 | [diff] [blame] | 73 | |
| 74 | mFd = open(TUN_DEV, O_RDWR | O_NONBLOCK | O_CLOEXEC); |
| 75 | if (mFd == -1) return -errno; |
| 76 | |
| 77 | int ret = ioctl(mFd, TUNSETIFF, &ifr, sizeof(ifr)); |
| 78 | if (ret == -1) { |
| 79 | ret = -errno; |
| 80 | close(mFd); |
| 81 | return ret; |
| 82 | } |
| 83 | |
| 84 | if (ifc_add_address(ifr.ifr_name, srcStr, 64) || |
| 85 | ifc_add_address(ifr.ifr_name, dstStr, 64)) { |
| 86 | ret = -errno; |
| 87 | close(mFd); |
| 88 | return ret; |
| 89 | } |
| 90 | |
Lorenzo Colitti | 54520a0 | 2018-02-09 18:39:16 +0900 | [diff] [blame] | 91 | mIfIndex = if_nametoindex(ifr.ifr_name); |
| 92 | |
Luke Huang | 531f5d3 | 2018-08-03 15:19:05 +0800 | [diff] [blame^] | 93 | if (int ret = ifc_enable(ifr.ifr_name)) { |
| 94 | return ret; |
| 95 | } |
Lorenzo Colitti | 1e299c6 | 2017-02-27 17:16:10 +0900 | [diff] [blame] | 96 | return 0; |
| 97 | } |
| 98 | |
| 99 | void TunInterface::destroy() { |
| 100 | if (mFd != -1) { |
Luke Huang | 531f5d3 | 2018-08-03 15:19:05 +0800 | [diff] [blame^] | 101 | ifc_disable(mIfName.c_str()); |
Lorenzo Colitti | 1e299c6 | 2017-02-27 17:16:10 +0900 | [diff] [blame] | 102 | close(mFd); |
| 103 | mFd = -1; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | } // namespace net |
| 108 | } // namespace android |