Lorenzo Colitti | ff6f7fe | 2014-12-08 11:27:41 +0900 | [diff] [blame] | 1 | /* |
| 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 Colitti | ff6f7fe | 2014-12-08 11:27:41 +0900 | [diff] [blame] | 18 | #include <arpa/inet.h> |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 19 | #include <fcntl.h> |
Lorenzo Colitti | ff6f7fe | 2014-12-08 11:27:41 +0900 | [diff] [blame] | 20 | #include <linux/if.h> |
| 21 | #include <linux/if_tun.h> |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 22 | #include <string.h> |
Lorenzo Colitti | ff6f7fe | 2014-12-08 11:27:41 +0900 | [diff] [blame] | 23 | #include <sys/ioctl.h> |
Lorenzo Colitti | 6b2007a | 2014-12-08 12:53:05 +0900 | [diff] [blame] | 24 | #include <sys/uio.h> |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 25 | #include <unistd.h> |
Lorenzo Colitti | 6b2007a | 2014-12-08 12:53:05 +0900 | [diff] [blame] | 26 | |
Lorenzo Colitti | eb92f48 | 2019-01-04 14:59:11 +0900 | [diff] [blame] | 27 | #include "common.h" |
Lorenzo Colitti | ff6f7fe | 2014-12-08 11:27:41 +0900 | [diff] [blame] | 28 | |
| 29 | /* function: tun_open |
| 30 | * tries to open the tunnel device |
| 31 | */ |
| 32 | int tun_open() { |
| 33 | int fd; |
| 34 | |
| 35 | fd = open("/dev/tun", O_RDWR); |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 36 | if (fd < 0) { |
Lorenzo Colitti | ff6f7fe | 2014-12-08 11:27:41 +0900 | [diff] [blame] | 37 | fd = open("/dev/net/tun", O_RDWR); |
| 38 | } |
| 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 |
| 46 | */ |
| 47 | int tun_alloc(char *dev, int fd) { |
| 48 | struct ifreq ifr; |
| 49 | int err; |
| 50 | |
| 51 | memset(&ifr, 0, sizeof(ifr)); |
| 52 | |
| 53 | ifr.ifr_flags = IFF_TUN; |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 54 | if (*dev) { |
Lorenzo Colitti | ff6f7fe | 2014-12-08 11:27:41 +0900 | [diff] [blame] | 55 | strncpy(ifr.ifr_name, dev, IFNAMSIZ); |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 56 | ifr.ifr_name[IFNAMSIZ - 1] = '\0'; |
Lorenzo Colitti | ff6f7fe | 2014-12-08 11:27:41 +0900 | [diff] [blame] | 57 | } |
| 58 | |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 59 | if ((err = ioctl(fd, TUNSETIFF, (void *)&ifr)) < 0) { |
Lorenzo Colitti | ff6f7fe | 2014-12-08 11:27:41 +0900 | [diff] [blame] | 60 | close(fd); |
| 61 | return err; |
| 62 | } |
| 63 | strcpy(dev, ifr.ifr_name); |
| 64 | return 0; |
| 65 | } |
Lorenzo Colitti | 6b2007a | 2014-12-08 12:53:05 +0900 | [diff] [blame] | 66 | |
Lorenzo Colitti | b20719e | 2014-12-08 10:51:32 +0900 | [diff] [blame] | 67 | /* function: set_nonblocking |
| 68 | * sets a filedescriptor to non-blocking mode |
| 69 | * fd - the filedescriptor |
| 70 | * returns: 0 on success, -1 on failure |
| 71 | */ |
| 72 | int set_nonblocking(int fd) { |
| 73 | int flags = fcntl(fd, F_GETFL); |
| 74 | if (flags == -1) { |
| 75 | return flags; |
| 76 | } |
| 77 | return fcntl(fd, F_SETFL, flags | O_NONBLOCK); |
| 78 | } |
| 79 | |
| 80 | /* function: send_tun |
| 81 | * sends a clat_packet to a tun interface |
| 82 | * fd - the tun filedescriptor |
| 83 | * out - the packet to send |
| 84 | * iov_len - the number of entries in the clat_packet |
| 85 | * returns: number of bytes read on success, -1 on failure |
| 86 | */ |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 87 | int send_tun(int fd, clat_packet out, int iov_len) { return writev(fd, out, iov_len); } |