]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/network/netdev/tuntap.c
core/namespace: rework the return semantics of clone_device_node yet again
[thirdparty/systemd.git] / src / network / netdev / tuntap.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 This file is part of systemd.
4
5 Copyright 2014 Susant Sahani <susant@redhat.com>
6
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
11
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
19 ***/
20
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <linux/if_tun.h>
24 #include <net/if.h>
25 #include <netinet/if_ether.h>
26 #include <sys/ioctl.h>
27 #include <sys/stat.h>
28 #include <sys/types.h>
29
30 #include "alloc-util.h"
31 #include "fd-util.h"
32 #include "netdev/tuntap.h"
33 #include "user-util.h"
34
35 #define TUN_DEV "/dev/net/tun"
36
37 static int netdev_fill_tuntap_message(NetDev *netdev, struct ifreq *ifr) {
38 TunTap *t;
39
40 assert(netdev);
41 assert(netdev->ifname);
42 assert(ifr);
43
44 if (netdev->kind == NETDEV_KIND_TAP) {
45 t = TAP(netdev);
46 ifr->ifr_flags |= IFF_TAP;
47 } else {
48 t = TUN(netdev);
49 ifr->ifr_flags |= IFF_TUN;
50 }
51
52 if (!t->packet_info)
53 ifr->ifr_flags |= IFF_NO_PI;
54
55 if (t->one_queue)
56 ifr->ifr_flags |= IFF_ONE_QUEUE;
57
58 if (t->multi_queue)
59 ifr->ifr_flags |= IFF_MULTI_QUEUE;
60
61 if (t->vnet_hdr)
62 ifr->ifr_flags |= IFF_VNET_HDR;
63
64 strncpy(ifr->ifr_name, netdev->ifname, IFNAMSIZ-1);
65
66 return 0;
67 }
68
69 static int netdev_tuntap_add(NetDev *netdev, struct ifreq *ifr) {
70 _cleanup_close_ int fd;
71 TunTap *t = NULL;
72 const char *user;
73 const char *group;
74 uid_t uid;
75 gid_t gid;
76 int r;
77
78 assert(netdev);
79 assert(ifr);
80
81 fd = open(TUN_DEV, O_RDWR);
82 if (fd < 0)
83 return log_netdev_error_errno(netdev, -errno, "Failed to open tun dev: %m");
84
85 r = ioctl(fd, TUNSETIFF, ifr);
86 if (r < 0)
87 return log_netdev_error_errno(netdev, -errno, "TUNSETIFF failed on tun dev: %m");
88
89 if (netdev->kind == NETDEV_KIND_TAP)
90 t = TAP(netdev);
91 else
92 t = TUN(netdev);
93
94 assert(t);
95
96 if (t->user_name) {
97
98 user = t->user_name;
99
100 r = get_user_creds(&user, &uid, NULL, NULL, NULL);
101 if (r < 0)
102 return log_netdev_error_errno(netdev, r, "Cannot resolve user name %s: %m", t->user_name);
103
104 r = ioctl(fd, TUNSETOWNER, uid);
105 if (r < 0)
106 return log_netdev_error_errno(netdev, -errno, "TUNSETOWNER failed on tun dev: %m");
107 }
108
109 if (t->group_name) {
110
111 group = t->group_name;
112
113 r = get_group_creds(&group, &gid);
114 if (r < 0)
115 return log_netdev_error_errno(netdev, r, "Cannot resolve group name %s: %m", t->group_name);
116
117 r = ioctl(fd, TUNSETGROUP, gid);
118 if (r < 0)
119 return log_netdev_error_errno(netdev, -errno, "TUNSETGROUP failed on tun dev: %m");
120
121 }
122
123 r = ioctl(fd, TUNSETPERSIST, 1);
124 if (r < 0)
125 return log_netdev_error_errno(netdev, -errno, "TUNSETPERSIST failed on tun dev: %m");
126
127 return 0;
128 }
129
130 static int netdev_create_tuntap(NetDev *netdev) {
131 struct ifreq ifr = {};
132 int r;
133
134 r = netdev_fill_tuntap_message(netdev, &ifr);
135 if (r < 0)
136 return r;
137
138 return netdev_tuntap_add(netdev, &ifr);
139 }
140
141 static void tuntap_done(NetDev *netdev) {
142 TunTap *t = NULL;
143
144 assert(netdev);
145
146 if (netdev->kind == NETDEV_KIND_TUN)
147 t = TUN(netdev);
148 else
149 t = TAP(netdev);
150
151 assert(t);
152
153 t->user_name = mfree(t->user_name);
154 t->group_name = mfree(t->group_name);
155 }
156
157 static int tuntap_verify(NetDev *netdev, const char *filename) {
158 assert(netdev);
159
160 if (netdev->mtu)
161 log_netdev_warning(netdev, "MTU configured for %s, ignoring", netdev_kind_to_string(netdev->kind));
162
163 if (netdev->mac)
164 log_netdev_warning(netdev, "MAC configured for %s, ignoring", netdev_kind_to_string(netdev->kind));
165
166 return 0;
167 }
168
169 const NetDevVTable tun_vtable = {
170 .object_size = sizeof(TunTap),
171 .sections = "Match\0NetDev\0Tun\0",
172 .config_verify = tuntap_verify,
173 .done = tuntap_done,
174 .create = netdev_create_tuntap,
175 .create_type = NETDEV_CREATE_INDEPENDENT,
176 };
177
178 const NetDevVTable tap_vtable = {
179 .object_size = sizeof(TunTap),
180 .sections = "Match\0NetDev\0Tap\0",
181 .config_verify = tuntap_verify,
182 .done = tuntap_done,
183 .create = netdev_create_tuntap,
184 .create_type = NETDEV_CREATE_INDEPENDENT,
185 };