1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2014 Susant Sahani <susant@redhat.com>
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 #include <sys/ioctl.h>
24 #include <linux/if_tun.h>
26 #include "networkd-netdev-tuntap.h"
28 #define TUN_DEV "/dev/net/tun"
30 static int netdev_fill_tuntap_message(NetDev *netdev, struct ifreq *ifr) {
34 assert(netdev->ifname);
37 if (netdev->kind == NETDEV_KIND_TAP) {
39 ifr->ifr_flags |= IFF_TAP;
42 ifr->ifr_flags |= IFF_TUN;
46 ifr->ifr_flags |= IFF_NO_PI;
49 ifr->ifr_flags |= IFF_ONE_QUEUE;
52 ifr->ifr_flags |= IFF_MULTI_QUEUE;
54 strncpy(ifr->ifr_name, netdev->ifname, IFNAMSIZ-1);
59 static int netdev_tuntap_add(NetDev *netdev, struct ifreq *ifr) {
60 _cleanup_close_ int fd;
71 fd = open(TUN_DEV, O_RDWR);
73 log_netdev_error(netdev, "Failed to open tun dev: %m");
77 r = ioctl(fd, TUNSETIFF, ifr);
79 log_netdev_error(netdev,
80 "TUNSETIFF failed on tun dev: %s",
85 if (netdev->kind == NETDEV_KIND_TAP)
96 r = get_user_creds(&user, &uid, NULL, NULL, NULL);
98 log_error_errno(r, "Cannot resolve user name %s: %m",
103 r = ioctl(fd, TUNSETOWNER, uid);
105 log_netdev_error(netdev,
106 "TUNSETOWNER failed on tun dev: %s",
113 group = t->group_name;
115 r = get_group_creds(&group, &gid);
117 log_error_errno(r, "Cannot resolve group name %s: %m",
122 r = ioctl(fd, TUNSETGROUP, gid);
124 log_netdev_error(netdev,
125 "TUNSETGROUP failed on tun dev: %s",
132 r = ioctl(fd, TUNSETPERSIST, 1);
134 log_netdev_error(netdev,
135 "TUNSETPERSIST failed on tun dev: %s",
143 static int netdev_create_tuntap(NetDev *netdev) {
144 struct ifreq ifr = {};
147 r = netdev_fill_tuntap_message(netdev, &ifr);
151 return netdev_tuntap_add(netdev, &ifr);
154 static void tuntap_done(NetDev *netdev) {
159 if (netdev->kind == NETDEV_KIND_TUN)
170 t->group_name = NULL;
173 static int tuntap_verify(NetDev *netdev, const char *filename) {
177 log_warning_netdev(netdev, "MTU configured for %s, ignoring",
178 netdev_kind_to_string(netdev->kind));
182 log_warning_netdev(netdev, "MAC configured for %s, ignoring",
183 netdev_kind_to_string(netdev->kind));
189 const NetDevVTable tun_vtable = {
190 .object_size = sizeof(TunTap),
191 .sections = "Match\0NetDev\0Tun\0",
192 .config_verify = tuntap_verify,
194 .create = netdev_create_tuntap,
195 .create_type = NETDEV_CREATE_INDEPENDENT,
198 const NetDevVTable tap_vtable = {
199 .object_size = sizeof(TunTap),
200 .sections = "Match\0NetDev\0Tap\0",
201 .config_verify = tuntap_verify,
203 .create = netdev_create_tuntap,
204 .create_type = NETDEV_CREATE_INDEPENDENT,