blob: e2f134239820305913714f453ae7ce5dff7c1301 [file] [log] [blame]
Lorenzo Colittiff6f7fe2014-12-08 11:27:41 +09001/*
2 * Copyright 2014 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.c - tun device functions
17 */
Lorenzo Colittiff6f7fe2014-12-08 11:27:41 +090018#include <arpa/inet.h>
junyulaic4e591a2018-11-26 22:36:10 +090019#include <fcntl.h>
Lorenzo Colittiff6f7fe2014-12-08 11:27:41 +090020#include <linux/if.h>
21#include <linux/if_tun.h>
junyulaic4e591a2018-11-26 22:36:10 +090022#include <string.h>
Lorenzo Colittiff6f7fe2014-12-08 11:27:41 +090023#include <sys/ioctl.h>
junyulaic4e591a2018-11-26 22:36:10 +090024#include <unistd.h>
Lorenzo Colitti6b2007a2014-12-08 12:53:05 +090025
Lorenzo Colittieb92f482019-01-04 14:59:11 +090026#include "common.h"
Lorenzo Colittiff6f7fe2014-12-08 11:27:41 +090027
28/* function: tun_open
Maciej Żenczykowski8addcc02019-04-08 17:46:48 -070029 * tries to open the tunnel device in non-blocking mode
Lorenzo Colittiff6f7fe2014-12-08 11:27:41 +090030 */
31int tun_open() {
32 int fd;
33
Maciej Żenczykowski8addcc02019-04-08 17:46:48 -070034 fd = open("/dev/tun", O_RDWR | O_NONBLOCK | O_CLOEXEC);
junyulaic4e591a2018-11-26 22:36:10 +090035 if (fd < 0) {
Maciej Żenczykowski8addcc02019-04-08 17:46:48 -070036 fd = open("/dev/net/tun", O_RDWR | O_NONBLOCK | O_CLOEXEC);
Lorenzo Colittiff6f7fe2014-12-08 11:27:41 +090037 }
38
39 return fd;
40}
41
42/* function: tun_alloc
43 * creates a tun interface and names it
44 * dev - the name for the new tun device
Lorenzo Colitti3297c7d2019-04-10 23:22:30 +090045 * fd - an open fd to the tun device node
46 * len - the length of the buffer pointed to by dev
Lorenzo Colittiff6f7fe2014-12-08 11:27:41 +090047 */
Lorenzo Colitti3297c7d2019-04-10 23:22:30 +090048int tun_alloc(char *dev, int fd, size_t len) {
Lorenzo Colittiff6f7fe2014-12-08 11:27:41 +090049 struct ifreq ifr;
50 int err;
51
52 memset(&ifr, 0, sizeof(ifr));
53
54 ifr.ifr_flags = IFF_TUN;
junyulaic4e591a2018-11-26 22:36:10 +090055 if (*dev) {
Lorenzo Colittiff6f7fe2014-12-08 11:27:41 +090056 strncpy(ifr.ifr_name, dev, IFNAMSIZ);
junyulaic4e591a2018-11-26 22:36:10 +090057 ifr.ifr_name[IFNAMSIZ - 1] = '\0';
Lorenzo Colittiff6f7fe2014-12-08 11:27:41 +090058 }
59
junyulaic4e591a2018-11-26 22:36:10 +090060 if ((err = ioctl(fd, TUNSETIFF, (void *)&ifr)) < 0) {
Lorenzo Colittiff6f7fe2014-12-08 11:27:41 +090061 close(fd);
62 return err;
63 }
Lorenzo Colitti3297c7d2019-04-10 23:22:30 +090064 strlcpy(dev, ifr.ifr_name, len);
Lorenzo Colittiff6f7fe2014-12-08 11:27:41 +090065 return 0;
66}