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> |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 24 | #include <unistd.h> |
Lorenzo Colitti | 6b2007a | 2014-12-08 12:53:05 +0900 | [diff] [blame] | 25 | |
Lorenzo Colitti | eb92f48 | 2019-01-04 14:59:11 +0900 | [diff] [blame] | 26 | #include "common.h" |
Lorenzo Colitti | ff6f7fe | 2014-12-08 11:27:41 +0900 | [diff] [blame] | 27 | |
| 28 | /* function: tun_open |
Maciej Żenczykowski | 8addcc0 | 2019-04-08 17:46:48 -0700 | [diff] [blame] | 29 | * tries to open the tunnel device in non-blocking mode |
Lorenzo Colitti | ff6f7fe | 2014-12-08 11:27:41 +0900 | [diff] [blame] | 30 | */ |
| 31 | int tun_open() { |
| 32 | int fd; |
| 33 | |
Maciej Żenczykowski | 8addcc0 | 2019-04-08 17:46:48 -0700 | [diff] [blame] | 34 | fd = open("/dev/tun", O_RDWR | O_NONBLOCK | O_CLOEXEC); |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 35 | if (fd < 0) { |
Maciej Żenczykowski | 8addcc0 | 2019-04-08 17:46:48 -0700 | [diff] [blame] | 36 | fd = open("/dev/net/tun", O_RDWR | O_NONBLOCK | O_CLOEXEC); |
Lorenzo Colitti | ff6f7fe | 2014-12-08 11:27:41 +0900 | [diff] [blame] | 37 | } |
| 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 Colitti | 3297c7d | 2019-04-10 23:22:30 +0900 | [diff] [blame] | 45 | * fd - an open fd to the tun device node |
| 46 | * len - the length of the buffer pointed to by dev |
Lorenzo Colitti | ff6f7fe | 2014-12-08 11:27:41 +0900 | [diff] [blame] | 47 | */ |
Lorenzo Colitti | 3297c7d | 2019-04-10 23:22:30 +0900 | [diff] [blame] | 48 | int tun_alloc(char *dev, int fd, size_t len) { |
Lorenzo Colitti | ff6f7fe | 2014-12-08 11:27:41 +0900 | [diff] [blame] | 49 | struct ifreq ifr; |
| 50 | int err; |
| 51 | |
| 52 | memset(&ifr, 0, sizeof(ifr)); |
| 53 | |
| 54 | ifr.ifr_flags = IFF_TUN; |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 55 | if (*dev) { |
Lorenzo Colitti | ff6f7fe | 2014-12-08 11:27:41 +0900 | [diff] [blame] | 56 | strncpy(ifr.ifr_name, dev, IFNAMSIZ); |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 57 | ifr.ifr_name[IFNAMSIZ - 1] = '\0'; |
Lorenzo Colitti | ff6f7fe | 2014-12-08 11:27:41 +0900 | [diff] [blame] | 58 | } |
| 59 | |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 60 | if ((err = ioctl(fd, TUNSETIFF, (void *)&ifr)) < 0) { |
Lorenzo Colitti | ff6f7fe | 2014-12-08 11:27:41 +0900 | [diff] [blame] | 61 | close(fd); |
| 62 | return err; |
| 63 | } |
Lorenzo Colitti | 3297c7d | 2019-04-10 23:22:30 +0900 | [diff] [blame] | 64 | strlcpy(dev, ifr.ifr_name, len); |
Lorenzo Colitti | ff6f7fe | 2014-12-08 11:27:41 +0900 | [diff] [blame] | 65 | return 0; |
| 66 | } |