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_error_netdev(netdev,
74 "Failed to open tun dev: %s",
79 r = ioctl(fd, TUNSETIFF, ifr);
81 log_error_netdev(netdev,
82 "TUNSETIFF failed on tun dev: %s",
87 if (netdev->kind == NETDEV_KIND_TAP)
98 r = get_user_creds(&user, &uid, NULL, NULL, NULL);
100 log_error("Cannot resolve user name %s: %s",
101 t->user_name, strerror(-r));
105 r = ioctl(fd, TUNSETOWNER, uid);
107 log_error_netdev(netdev,
108 "TUNSETOWNER failed on tun dev: %s",
115 group = t->group_name;
117 r = get_group_creds(&group, &gid);
119 log_error("Cannot resolve group name %s: %s",
120 t->group_name, strerror(-r));
124 r = ioctl(fd, TUNSETGROUP, gid);
126 log_error_netdev(netdev,
127 "TUNSETGROUP failed on tun dev: %s",
134 r = ioctl(fd, TUNSETPERSIST, 1);
136 log_error_netdev(netdev,
137 "TUNSETPERSIST failed on tun dev: %s",
145 static int netdev_create_tuntap(NetDev *netdev) {
146 struct ifreq ifr = {};
149 r = netdev_fill_tuntap_message(netdev, &ifr);
153 return netdev_tuntap_add(netdev, &ifr);
156 static void tuntap_done(NetDev *netdev) {
161 if (netdev->kind == NETDEV_KIND_TUN)
172 t->group_name = NULL;
175 static int tuntap_verify(NetDev *netdev, const char *filename) {
180 if (netdev->kind == NETDEV_KIND_TUN)
188 log_warning_netdev(netdev, "MTU configured for %s, ignoring",
189 netdev_kind_to_string(netdev->kind));
193 log_warning_netdev(netdev, "MAC configured for %s, ignoring",
194 netdev_kind_to_string(netdev->kind));
200 const NetDevVTable tun_vtable = {
201 .object_size = sizeof(TunTap),
202 .sections = "Match\0NetDev\0Tun\0",
203 .config_verify = tuntap_verify,
205 .create = netdev_create_tuntap,
206 .create_type = NETDEV_CREATE_INDEPENDENT,
209 const NetDevVTable tap_vtable = {
210 .object_size = sizeof(TunTap),
211 .sections = "Match\0NetDev\0Tap\0",
212 .config_verify = tuntap_verify,
214 .create = netdev_create_tuntap,
215 .create_type = NETDEV_CREATE_INDEPENDENT,