Debian Codegraph @

src/network/netdev/tuntap.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <fcntl.h>
4 #include <linux/if_tun.h>
5 #include <net/if.h>
6 #include <net/if_arp.h>
7 #include <sys/ioctl.h>
8
9 #include "sd-messages.h"
10
11 #include "alloc-util.h"
12 #include "daemon-util.h"
13 #include "fd-util.h"
14 #include "hashmap.h"
15 #include "networkd-manager.h"
16 #include "socket-util.h"
17 #include "string-util.h"
18 #include "tuntap.h"
19 #include "uid-classification.h"
20 #include "user-record.h"
21 #include "user-util.h"
22 #include "userdb.h"
23
24 #define TUN_DEV "/dev/net/tun"
25
26 static TunTap* TUNTAP(NetDev *netdev) {
27 assert(netdev);
28
29 switch (netdev->kind) {
30 case NETDEV_KIND_TAP:
31 return TAP(netdev);
32 case NETDEV_KIND_TUN:
33 return TUN(netdev);
34 default:
35 return NULL;
36 }
37 }
38
39 DEFINE_PRIVATE_HASH_OPS_FULL(named_fd_hash_ops, char, string_hash_func, string_compare_func, free, void, close_fd_ptr);
40
41 static int manager_add_tuntap_fd_impl(Manager *m, int fd, const char *name) {
42 _cleanup_free_ char *tuntap_name = NULL;
43 int r;
44
45 assert(m);
46 assert(fd >= 0);
47 assert(name);
48
49 tuntap_name = strdup(name);
50 if (!tuntap_name)
51 return log_oom_debug();
52
53 r = hashmap_ensure_put(&m->tuntap_fds_by_name, &named_fd_hash_ops, tuntap_name, FD_TO_PTR(fd));
54 if (r < 0)
55 return log_debug_errno(r, "Failed to store tuntap fd: %m");
56
57 TAKE_PTR(tuntap_name);
58 return 0;
59 }
60
61 int manager_add_tuntap_fd(Manager *m, int fd, const char *name) {
62 const char *p;
63
64 assert(m);
65 assert(fd >= 0);
66 assert(name);
67
68 p = startswith(name, "tuntap-");
69 if (!p)
70 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "Received unknown fd (%s).", name);
71
72 if (!ifname_valid(p))
73 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "Received tuntap fd with invalid name (%s).", p);
74
75 return manager_add_tuntap_fd_impl(m, fd, p);
76 }
77
78 static int netdev_take_tuntap_fd(Manager *m, const char *ifname) {
79 _unused_ _cleanup_free_ char *name = NULL;
80 void *p;
81
82 assert(m);
83 assert(ifname);
84
85 p = hashmap_remove2(m->tuntap_fds_by_name, ifname, (void**) &name);
86 if (!p)
87 return -EBADF;
88
89 return PTR_TO_FD(p);
90 }
91
92 static int netdev_push_tuntap_fd(NetDev *netdev, int fd) {
93 _unused_ _cleanup_close_ int fd_old = -EBADF;
94 int r;
95
96 assert(netdev->manager);
97
98 fd_old = netdev_take_tuntap_fd(netdev->manager, netdev->ifname);
99
100 if (!TUNTAP(netdev)->keep_fd)
101 return 0;
102
103 r = manager_add_tuntap_fd_impl(netdev->manager, fd, netdev->ifname);
104 if (r < 0)
105 return r;
106
107 (void) notify_push_fdf(fd, "tuntap-%s", netdev->ifname);
108 return 1; /* saved */
109 }
110
111 static void manager_close_and_notify_tuntap_fd(Manager *m, const char *ifname) {
112 assert(m);
113 assert(ifname);
114
115 /* netdev_take_tuntap_fd() may invalidate ifname. Hence, need to create fdname earlier. */
116 const char *fdname = strjoina("tuntap-", ifname);
117 close_and_notify_warn(netdev_take_tuntap_fd(m, ifname), fdname);
118 }
119
120 void manager_clear_unmanaged_tuntap_fds(Manager *m) {
121 const char *name;
122 void *p;
123
124 assert(m);
125
126 HASHMAP_FOREACH_KEY(p, name, m->tuntap_fds_by_name) {
127 NetDev *netdev;
128
129 if (netdev_get(m, name, &netdev) < 0 ||
130 !IN_SET(netdev->kind, NETDEV_KIND_TAP, NETDEV_KIND_TUN) ||
131 !TUNTAP(netdev)->keep_fd)
132 manager_close_and_notify_tuntap_fd(m, name);
133 }
134 }
135
136 static int netdev_create_tuntap(NetDev *netdev) {
137 _cleanup_close_ int fd = -EBADF;
138 struct ifreq ifr = {};
139 TunTap *t = TUNTAP(netdev);
140 int r;
141
142 assert(netdev->manager);
143
144 fd = open(TUN_DEV, O_RDWR|O_CLOEXEC);
145 if (fd < 0)
146 return log_netdev_error_errno(netdev, errno, "Failed to open " TUN_DEV ": %m");
147
148 if (netdev->kind == NETDEV_KIND_TAP)
149 ifr.ifr_flags |= IFF_TAP;
150 else
151 ifr.ifr_flags |= IFF_TUN;
152
153 if (!t->packet_info)
154 ifr.ifr_flags |= IFF_NO_PI;
155
156 if (t->multi_queue)
157 ifr.ifr_flags |= IFF_MULTI_QUEUE;
158
159 if (t->vnet_hdr)
160 ifr.ifr_flags |= IFF_VNET_HDR;
161
162 strncpy(ifr.ifr_name, netdev->ifname, IFNAMSIZ-1);
163
164 if (ioctl(fd, TUNSETIFF, &ifr) < 0)
165 return log_netdev_error_errno(netdev, errno, "TUNSETIFF failed: %m");
166
167 if (t->multi_queue) {
168 /* If we don't detach the queue, the kernel will send packets to our queue and they
169 * will be dropped because we never read them, which is especially important in case
170 * of KeepCarrier option which persists open FD. So detach our queue right after
171 * device create/attach to make kernel not send the packets to it. The option is
172 * available for multi-queue devices only.
173 *
174 * See https://github.com/systemd/systemd/pull/30504 for details. */
175 struct ifreq detach_request = { .ifr_flags = IFF_DETACH_QUEUE };
176 if (ioctl(fd, TUNSETQUEUE, &detach_request) < 0)
177 return log_netdev_error_errno(netdev, errno, "TUNSETQUEUE failed: %m");
178 }
179
180 if (uid_is_valid(t->uid))
181 if (ioctl(fd, TUNSETOWNER, t->uid) < 0)
182 return log_netdev_error_errno(netdev, errno, "TUNSETOWNER failed: %m");
183
184 if (gid_is_valid(t->gid))
185 if (ioctl(fd, TUNSETGROUP, t->gid) < 0)
186 return log_netdev_error_errno(netdev, errno, "TUNSETGROUP failed: %m");
187
188 if (ioctl(fd, TUNSETPERSIST, 1) < 0)
189 return log_netdev_error_errno(netdev, errno, "TUNSETPERSIST failed: %m");
190
191 r = netdev_push_tuntap_fd(netdev, fd);
192 if (r < 0)
193 return log_netdev_warning_errno(netdev, r, "Failed to save TUN/TAP fd: %m");
194 if (r > 0)
195 TAKE_FD(fd);
196
197 netdev_enter_ready(netdev);
198 return 0;
199 }
200
201 static void tuntap_drop(NetDev *netdev) {
202 assert(netdev);
203
204 manager_close_and_notify_tuntap_fd(netdev->manager, netdev->ifname);
205 }
206
207 static void tuntap_done(NetDev *netdev) {
208 TunTap *t = TUNTAP(netdev);
209
210 t->user_name = mfree(t->user_name);
211 t->group_name = mfree(t->group_name);
212 }
213
214 static void tuntap_init(NetDev *netdev) {
215 TunTap *t = TUNTAP(netdev);
216
217 t->uid = UID_INVALID;
218 t->gid = GID_INVALID;
219 }
220
221 static int tuntap_verify(NetDev *netdev, const char *filename) {
222 TunTap *t = TUNTAP(netdev);
223 int r;
224
225 assert(netdev);
226
227 if (netdev->mtu != 0)
228 log_netdev_warning(netdev,
229 "MTUBytes= configured for %s device in %s will be ignored.\n"
230 "Please set it in the corresponding .network file.",
231 netdev_kind_to_string(netdev->kind), filename);
232
233 if (netdev->hw_addr.length > 0)
234 log_netdev_warning(netdev,
235 "MACAddress= configured for %s device in %s will be ignored.\n"
236 "Please set it in the corresponding .network file.",
237 netdev_kind_to_string(netdev->kind), filename);
238
239 if (t->user_name) {
240 _cleanup_(user_record_unrefp) UserRecord *ur = NULL;
241
242 r = userdb_by_name(t->user_name, /* match = */ NULL,
243 USERDB_SUPPRESS_SHADOW | USERDB_PARSE_NUMERIC,
244 &ur);
245 if (r < 0)
246 log_netdev_warning_errno(netdev, r, "Cannot resolve user name '%s', ignoring: %s",
247 t->user_name, STRERROR_USER(r));
248 else {
249 if (!uid_is_system(ur->uid))
250 log_netdev_syntax(netdev, LOG_WARNING,
251 SD_MESSAGE_SYSTEM_ACCOUNT_REQUIRED_STR,
252 "User '%s' configured as owner is not a system user. "
253 "Support for device node ownership by non-system accounts is deprecated and will be removed in the future.",
254 t->user_name);
255
256 t->uid = ur->uid;
257 }
258 }
259
260 if (t->group_name) {
261 _cleanup_(group_record_unrefp) GroupRecord *gr = NULL;
262
263 r = groupdb_by_name(t->group_name, /* match = */ NULL,
264 USERDB_SUPPRESS_SHADOW | USERDB_PARSE_NUMERIC,
265 &gr);
266 if (r < 0)
267 log_netdev_warning_errno(netdev, r, "Cannot resolve group name '%s', ignoring: %s",
268 t->group_name, STRERROR_GROUP(r));
269 else {
270 if (!gid_is_system(gr->gid))
271 log_netdev_syntax(netdev, LOG_WARNING,
272 SD_MESSAGE_SYSTEM_ACCOUNT_REQUIRED_STR,
273 "Group '%s' configured as owner is not a system group. "
274 "Support for device node ownership by non-system accounts is deprecated and will be removed in the future.",
275 t->group_name);
276
277 t->gid = gr->gid;
278 }
279 }
280
281 return 0;
282 }
283
284 const NetDevVTable tun_vtable = {
285 .object_size = sizeof(TunTap),
286 .sections = NETDEV_COMMON_SECTIONS "Tun\0",
287 .init = tuntap_init,
288 .config_verify = tuntap_verify,
289 .drop = tuntap_drop,
290 .done = tuntap_done,
291 .create = netdev_create_tuntap,
292 .create_type = NETDEV_CREATE_INDEPENDENT,
293 .iftype = ARPHRD_NONE,
294 };
295
296 const NetDevVTable tap_vtable = {
297 .object_size = sizeof(TunTap),
298 .sections = NETDEV_COMMON_SECTIONS "Tap\0",
299 .init = tuntap_init,
300 .config_verify = tuntap_verify,
301 .drop = tuntap_drop,
302 .done = tuntap_done,
303 .create = netdev_create_tuntap,
304 .create_type = NETDEV_CREATE_INDEPENDENT,
305 .iftype = ARPHRD_ETHER,
306 };