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> 23 #include <linux/if.h> 24 #include <linux/if_tun.h> 25 #include <net/if.h> 26 #include <netinet/in.h> 27 #include <sys/ioctl.h> 28 #include <sys/socket.h> 29 #include <sys/stat.h> 30 #include <sys/types.h> 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; 57 if (getnameinfo((sockaddr *) &src6, sizeof(src6), srcStr, sizeof(srcStr), NULL, 0, flags) || 58 getnameinfo((sockaddr *) &dst6, sizeof(dst6), dstStr, sizeof(dstStr), NULL, 0, flags)) { 59 return -EINVAL; 60 } 61 62 // Create a tun interface with a name based on our PID and some randomness. 63 // iptables will only accept interfaces whose name is up to IFNAMSIZ - 1 bytes long. 64 mIfName = StringPrintf("netd%u_%u", getpid(), arc4random()); 65 if (mIfName.size() >= IFNAMSIZ) { 66 mIfName.resize(IFNAMSIZ - 1); 67 } 68 struct ifreq ifr = { 69 .ifr_ifru = { .ifru_flags = IFF_TUN }, 70 }; 71 strlcpy(ifr.ifr_name, mIfName.c_str(), sizeof(ifr.ifr_name)); 72 73 mFd = open(TUN_DEV, O_RDWR | O_NONBLOCK | O_CLOEXEC); 74 if (mFd == -1) return -errno; 75 76 int ret = ioctl(mFd, TUNSETIFF, &ifr, sizeof(ifr)); 77 if (ret == -1) { 78 ret = -errno; 79 close(mFd); 80 return ret; 81 } 82 83 if (ifc_add_address(ifr.ifr_name, srcStr, 64) || 84 ifc_add_address(ifr.ifr_name, dstStr, 64)) { 85 ret = -errno; 86 close(mFd); 87 return ret; 88 } 89 90 mIfIndex = if_nametoindex(ifr.ifr_name); 91 92 return 0; 93 } 94 95 void TunInterface::destroy() { 96 if (mFd != -1) { 97 close(mFd); 98 mFd = -1; 99 } 100 } 101 102 } // namespace net 103 } // namespace android 104