11e299c63fd42f02f23547690275d4f6f9cd5fcc4Lorenzo Colitti/*
21e299c63fd42f02f23547690275d4f6f9cd5fcc4Lorenzo Colitti * Copyright 2017 The Android Open Source Project
31e299c63fd42f02f23547690275d4f6f9cd5fcc4Lorenzo Colitti *
41e299c63fd42f02f23547690275d4f6f9cd5fcc4Lorenzo Colitti * Licensed under the Apache License, Version 2.0 (the "License");
51e299c63fd42f02f23547690275d4f6f9cd5fcc4Lorenzo Colitti * you may not use this file except in compliance with the License.
61e299c63fd42f02f23547690275d4f6f9cd5fcc4Lorenzo Colitti * You may obtain a copy of the License at
71e299c63fd42f02f23547690275d4f6f9cd5fcc4Lorenzo Colitti *
81e299c63fd42f02f23547690275d4f6f9cd5fcc4Lorenzo Colitti * http://www.apache.org/licenses/LICENSE-2.0
91e299c63fd42f02f23547690275d4f6f9cd5fcc4Lorenzo Colitti *
101e299c63fd42f02f23547690275d4f6f9cd5fcc4Lorenzo Colitti * Unless required by applicable law or agreed to in writing, software
111e299c63fd42f02f23547690275d4f6f9cd5fcc4Lorenzo Colitti * distributed under the License is distributed on an "AS IS" BASIS,
121e299c63fd42f02f23547690275d4f6f9cd5fcc4Lorenzo Colitti * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
131e299c63fd42f02f23547690275d4f6f9cd5fcc4Lorenzo Colitti * See the License for the specific language governing permissions and
141e299c63fd42f02f23547690275d4f6f9cd5fcc4Lorenzo Colitti * limitations under the License.
151e299c63fd42f02f23547690275d4f6f9cd5fcc4Lorenzo Colitti *
161e299c63fd42f02f23547690275d4f6f9cd5fcc4Lorenzo Colitti * tun_interface.cpp - creates tun interfaces for testing purposes
171e299c63fd42f02f23547690275d4f6f9cd5fcc4Lorenzo Colitti */
181e299c63fd42f02f23547690275d4f6f9cd5fcc4Lorenzo Colitti
191e299c63fd42f02f23547690275d4f6f9cd5fcc4Lorenzo Colitti#include <fcntl.h>
201e299c63fd42f02f23547690275d4f6f9cd5fcc4Lorenzo Colitti#include <netdb.h>
211e299c63fd42f02f23547690275d4f6f9cd5fcc4Lorenzo Colitti#include <stdlib.h>
221e299c63fd42f02f23547690275d4f6f9cd5fcc4Lorenzo Colitti#include <unistd.h>
2354520a0206f086fadda861fdabde3afbc4318960Lorenzo Colitti#include <linux/if.h>
2454520a0206f086fadda861fdabde3afbc4318960Lorenzo Colitti#include <linux/if_tun.h>
2554520a0206f086fadda861fdabde3afbc4318960Lorenzo Colitti#include <net/if.h>
2654520a0206f086fadda861fdabde3afbc4318960Lorenzo Colitti#include <netinet/in.h>
271e299c63fd42f02f23547690275d4f6f9cd5fcc4Lorenzo Colitti#include <sys/ioctl.h>
281e299c63fd42f02f23547690275d4f6f9cd5fcc4Lorenzo Colitti#include <sys/socket.h>
291e299c63fd42f02f23547690275d4f6f9cd5fcc4Lorenzo Colitti#include <sys/stat.h>
301e299c63fd42f02f23547690275d4f6f9cd5fcc4Lorenzo Colitti#include <sys/types.h>
311e299c63fd42f02f23547690275d4f6f9cd5fcc4Lorenzo Colitti
321e299c63fd42f02f23547690275d4f6f9cd5fcc4Lorenzo Colitti#include <android-base/stringprintf.h>
331e299c63fd42f02f23547690275d4f6f9cd5fcc4Lorenzo Colitti#include <android-base/strings.h>
341e299c63fd42f02f23547690275d4f6f9cd5fcc4Lorenzo Colitti#include <netutils/ifc.h>
351e299c63fd42f02f23547690275d4f6f9cd5fcc4Lorenzo Colitti
361e299c63fd42f02f23547690275d4f6f9cd5fcc4Lorenzo Colitti#include "tun_interface.h"
371e299c63fd42f02f23547690275d4f6f9cd5fcc4Lorenzo Colitti
381e299c63fd42f02f23547690275d4f6f9cd5fcc4Lorenzo Colitti#define TUN_DEV "/dev/tun"
391e299c63fd42f02f23547690275d4f6f9cd5fcc4Lorenzo Colitti
401e299c63fd42f02f23547690275d4f6f9cd5fcc4Lorenzo Colittiusing android::base::StringPrintf;
411e299c63fd42f02f23547690275d4f6f9cd5fcc4Lorenzo Colitti
421e299c63fd42f02f23547690275d4f6f9cd5fcc4Lorenzo Colittinamespace android {
431e299c63fd42f02f23547690275d4f6f9cd5fcc4Lorenzo Colittinamespace net {
441e299c63fd42f02f23547690275d4f6f9cd5fcc4Lorenzo Colitti
451e299c63fd42f02f23547690275d4f6f9cd5fcc4Lorenzo Colittiint TunInterface::init() {
461e299c63fd42f02f23547690275d4f6f9cd5fcc4Lorenzo Colitti    // Generate a random ULA address pair.
471e299c63fd42f02f23547690275d4f6f9cd5fcc4Lorenzo Colitti    arc4random_buf(&mSrcAddr, sizeof(mSrcAddr));
481e299c63fd42f02f23547690275d4f6f9cd5fcc4Lorenzo Colitti    mSrcAddr.s6_addr[0] = 0xfd;
491e299c63fd42f02f23547690275d4f6f9cd5fcc4Lorenzo Colitti    memcpy(&mDstAddr, &mSrcAddr, sizeof(mDstAddr));
501e299c63fd42f02f23547690275d4f6f9cd5fcc4Lorenzo Colitti    mDstAddr.s6_addr[15] ^= 1;
511e299c63fd42f02f23547690275d4f6f9cd5fcc4Lorenzo Colitti
521e299c63fd42f02f23547690275d4f6f9cd5fcc4Lorenzo Colitti    // Convert the addresses to strings because that's what ifc_add_address takes.
531e299c63fd42f02f23547690275d4f6f9cd5fcc4Lorenzo Colitti    char srcStr[INET6_ADDRSTRLEN], dstStr[INET6_ADDRSTRLEN];
541e299c63fd42f02f23547690275d4f6f9cd5fcc4Lorenzo Colitti    sockaddr_in6 src6 = { .sin6_family = AF_INET6, .sin6_addr = mSrcAddr, };
551e299c63fd42f02f23547690275d4f6f9cd5fcc4Lorenzo Colitti    sockaddr_in6 dst6 = { .sin6_family = AF_INET6, .sin6_addr = mDstAddr, };
561e299c63fd42f02f23547690275d4f6f9cd5fcc4Lorenzo Colitti    int flags = NI_NUMERICHOST;
571e299c63fd42f02f23547690275d4f6f9cd5fcc4Lorenzo Colitti    if (getnameinfo((sockaddr *) &src6, sizeof(src6), srcStr, sizeof(srcStr), NULL, 0, flags) ||
581e299c63fd42f02f23547690275d4f6f9cd5fcc4Lorenzo Colitti        getnameinfo((sockaddr *) &dst6, sizeof(dst6), dstStr, sizeof(dstStr), NULL, 0, flags)) {
591e299c63fd42f02f23547690275d4f6f9cd5fcc4Lorenzo Colitti        return -EINVAL;
601e299c63fd42f02f23547690275d4f6f9cd5fcc4Lorenzo Colitti    }
611e299c63fd42f02f23547690275d4f6f9cd5fcc4Lorenzo Colitti
6254520a0206f086fadda861fdabde3afbc4318960Lorenzo Colitti    // Create a tun interface with a name based on our PID and some randomness.
6354520a0206f086fadda861fdabde3afbc4318960Lorenzo Colitti    // iptables will only accept interfaces whose name is up to IFNAMSIZ - 1 bytes long.
6454520a0206f086fadda861fdabde3afbc4318960Lorenzo Colitti    mIfName = StringPrintf("netd%u_%u", getpid(), arc4random());
6554520a0206f086fadda861fdabde3afbc4318960Lorenzo Colitti    if (mIfName.size() >= IFNAMSIZ) {
6654520a0206f086fadda861fdabde3afbc4318960Lorenzo Colitti        mIfName.resize(IFNAMSIZ - 1);
6754520a0206f086fadda861fdabde3afbc4318960Lorenzo Colitti    }
681e299c63fd42f02f23547690275d4f6f9cd5fcc4Lorenzo Colitti    struct ifreq ifr = {
691e299c63fd42f02f23547690275d4f6f9cd5fcc4Lorenzo Colitti        .ifr_ifru = { .ifru_flags = IFF_TUN },
701e299c63fd42f02f23547690275d4f6f9cd5fcc4Lorenzo Colitti    };
7154520a0206f086fadda861fdabde3afbc4318960Lorenzo Colitti    strlcpy(ifr.ifr_name, mIfName.c_str(), sizeof(ifr.ifr_name));
721e299c63fd42f02f23547690275d4f6f9cd5fcc4Lorenzo Colitti
731e299c63fd42f02f23547690275d4f6f9cd5fcc4Lorenzo Colitti    mFd = open(TUN_DEV, O_RDWR | O_NONBLOCK | O_CLOEXEC);
741e299c63fd42f02f23547690275d4f6f9cd5fcc4Lorenzo Colitti    if (mFd == -1) return -errno;
751e299c63fd42f02f23547690275d4f6f9cd5fcc4Lorenzo Colitti
761e299c63fd42f02f23547690275d4f6f9cd5fcc4Lorenzo Colitti    int ret = ioctl(mFd, TUNSETIFF, &ifr, sizeof(ifr));
771e299c63fd42f02f23547690275d4f6f9cd5fcc4Lorenzo Colitti    if (ret == -1) {
781e299c63fd42f02f23547690275d4f6f9cd5fcc4Lorenzo Colitti        ret = -errno;
791e299c63fd42f02f23547690275d4f6f9cd5fcc4Lorenzo Colitti        close(mFd);
801e299c63fd42f02f23547690275d4f6f9cd5fcc4Lorenzo Colitti        return ret;
811e299c63fd42f02f23547690275d4f6f9cd5fcc4Lorenzo Colitti    }
821e299c63fd42f02f23547690275d4f6f9cd5fcc4Lorenzo Colitti
831e299c63fd42f02f23547690275d4f6f9cd5fcc4Lorenzo Colitti    if (ifc_add_address(ifr.ifr_name, srcStr, 64) ||
841e299c63fd42f02f23547690275d4f6f9cd5fcc4Lorenzo Colitti        ifc_add_address(ifr.ifr_name, dstStr, 64)) {
851e299c63fd42f02f23547690275d4f6f9cd5fcc4Lorenzo Colitti        ret = -errno;
861e299c63fd42f02f23547690275d4f6f9cd5fcc4Lorenzo Colitti        close(mFd);
871e299c63fd42f02f23547690275d4f6f9cd5fcc4Lorenzo Colitti        return ret;
881e299c63fd42f02f23547690275d4f6f9cd5fcc4Lorenzo Colitti    }
891e299c63fd42f02f23547690275d4f6f9cd5fcc4Lorenzo Colitti
9054520a0206f086fadda861fdabde3afbc4318960Lorenzo Colitti    mIfIndex = if_nametoindex(ifr.ifr_name);
9154520a0206f086fadda861fdabde3afbc4318960Lorenzo Colitti
921e299c63fd42f02f23547690275d4f6f9cd5fcc4Lorenzo Colitti    return 0;
931e299c63fd42f02f23547690275d4f6f9cd5fcc4Lorenzo Colitti}
941e299c63fd42f02f23547690275d4f6f9cd5fcc4Lorenzo Colitti
951e299c63fd42f02f23547690275d4f6f9cd5fcc4Lorenzo Colittivoid TunInterface::destroy() {
961e299c63fd42f02f23547690275d4f6f9cd5fcc4Lorenzo Colitti    if (mFd != -1) {
971e299c63fd42f02f23547690275d4f6f9cd5fcc4Lorenzo Colitti        close(mFd);
981e299c63fd42f02f23547690275d4f6f9cd5fcc4Lorenzo Colitti        mFd = -1;
991e299c63fd42f02f23547690275d4f6f9cd5fcc4Lorenzo Colitti    }
1001e299c63fd42f02f23547690275d4f6f9cd5fcc4Lorenzo Colitti}
1011e299c63fd42f02f23547690275d4f6f9cd5fcc4Lorenzo Colitti
1021e299c63fd42f02f23547690275d4f6f9cd5fcc4Lorenzo Colitti}  // namespace net
1031e299c63fd42f02f23547690275d4f6f9cd5fcc4Lorenzo Colitti}  // namespace android
104