blob: 7ecbf2c09075c69b4ab4a54a3af2d678536fd1b9 [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>
Lorenzo Colitti6b2007a2014-12-08 12:53:05 +090024#include <sys/uio.h>
junyulaic4e591a2018-11-26 22:36:10 +090025#include <unistd.h>
Lorenzo Colitti6b2007a2014-12-08 12:53:05 +090026
Lorenzo Colittieb92f482019-01-04 14:59:11 +090027#include "common.h"
Lorenzo Colittiff6f7fe2014-12-08 11:27:41 +090028
29/* function: tun_open
30 * tries to open the tunnel device
31 */
32int tun_open() {
33 int fd;
34
Lorenzo Colitti6a095df2019-04-10 23:22:30 +090035 fd = open("/dev/tun", O_RDWR | O_CLOEXEC);
junyulaic4e591a2018-11-26 22:36:10 +090036 if (fd < 0) {
Lorenzo Colitti6a095df2019-04-10 23:22:30 +090037 fd = open("/dev/net/tun", O_RDWR | O_CLOEXEC);
Lorenzo Colittiff6f7fe2014-12-08 11:27:41 +090038 }
39
40 return fd;
41}
42
43/* function: tun_alloc
44 * creates a tun interface and names it
45 * dev - the name for the new tun device
Lorenzo Colitti6a095df2019-04-10 23:22:30 +090046 * fd - an open fd to the tun device node
47 * len - the length of the buffer pointed to by dev
Lorenzo Colittiff6f7fe2014-12-08 11:27:41 +090048 */
Lorenzo Colitti6a095df2019-04-10 23:22:30 +090049int tun_alloc(char *dev, int fd, size_t len) {
Lorenzo Colittiff6f7fe2014-12-08 11:27:41 +090050 struct ifreq ifr;
51 int err;
52
53 memset(&ifr, 0, sizeof(ifr));
54
55 ifr.ifr_flags = IFF_TUN;
junyulaic4e591a2018-11-26 22:36:10 +090056 if (*dev) {
Lorenzo Colittiff6f7fe2014-12-08 11:27:41 +090057 strncpy(ifr.ifr_name, dev, IFNAMSIZ);
junyulaic4e591a2018-11-26 22:36:10 +090058 ifr.ifr_name[IFNAMSIZ - 1] = '\0';
Lorenzo Colittiff6f7fe2014-12-08 11:27:41 +090059 }
60
junyulaic4e591a2018-11-26 22:36:10 +090061 if ((err = ioctl(fd, TUNSETIFF, (void *)&ifr)) < 0) {
Lorenzo Colittiff6f7fe2014-12-08 11:27:41 +090062 close(fd);
63 return err;
64 }
Lorenzo Colitti6a095df2019-04-10 23:22:30 +090065 strlcpy(dev, ifr.ifr_name, len);
Lorenzo Colittiff6f7fe2014-12-08 11:27:41 +090066 return 0;
67}
Lorenzo Colitti6b2007a2014-12-08 12:53:05 +090068
Lorenzo Colittib20719e2014-12-08 10:51:32 +090069/* function: set_nonblocking
70 * sets a filedescriptor to non-blocking mode
71 * fd - the filedescriptor
72 * returns: 0 on success, -1 on failure
73 */
74int set_nonblocking(int fd) {
75 int flags = fcntl(fd, F_GETFL);
76 if (flags == -1) {
77 return flags;
78 }
79 return fcntl(fd, F_SETFL, flags | O_NONBLOCK);
80}
81
82/* function: send_tun
83 * sends a clat_packet to a tun interface
84 * fd - the tun filedescriptor
85 * out - the packet to send
86 * iov_len - the number of entries in the clat_packet
87 * returns: number of bytes read on success, -1 on failure
88 */
junyulaic4e591a2018-11-26 22:36:10 +090089int send_tun(int fd, clat_packet out, int iov_len) { return writev(fd, out, iov_len); }