]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/network/netdev/tuntap.c
Add SPDX license identifiers to source files under the LGPL
[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 <fcntl.h>
22 #include <linux/if_tun.h>
23 #include <net/if.h>
24 #include <netinet/if_ether.h>
25 #include <sys/ioctl.h>
26 #include <sys/stat.h>
27 #include <sys/types.h>
28
29 #include "alloc-util.h"
30 #include "fd-util.h"
31 #include "netdev/tuntap.h"
32 #include "user-util.h"
33
34 #define TUN_DEV "/dev/net/tun"
35
36 static int netdev_fill_tuntap_message(NetDev *netdev, struct ifreq *ifr) {
37 TunTap *t;
38
39 assert(netdev);
40 assert(netdev->ifname);
41 assert(ifr);
42
43 if (netdev->kind == NETDEV_KIND_TAP) {
44 t = TAP(netdev);
45 ifr->ifr_flags |= IFF_TAP;
46 } else {
47 t = TUN(netdev);
48 ifr->ifr_flags |= IFF_TUN;
49 }
50
51 if (!t->packet_info)
52 ifr->ifr_flags |= IFF_NO_PI;
53
54 if (t->one_queue)
55 ifr->ifr_flags |= IFF_ONE_QUEUE;
56
57 if (t->multi_queue)
58 ifr->ifr_flags |= IFF_MULTI_QUEUE;
59
60 if (t->vnet_hdr)
61 ifr->ifr_flags |= IFF_VNET_HDR;
62
63 strncpy(ifr->ifr_name, netdev->ifname, IFNAMSIZ-1);
64
65 return 0;
66 }
67
68 static int netdev_tuntap_add(NetDev *netdev, struct ifreq *ifr) {
69 _cleanup_close_ int fd;
70 TunTap *t = NULL;
71 const char *user;
72 const char *group;
73 uid_t uid;
74 gid_t gid;
75 int r;
76
77 assert(netdev);
78 assert(ifr);
79
80 fd = open(TUN_DEV, O_RDWR);
81 if (fd < 0)
82 return log_netdev_error_errno(netdev, -errno, "Failed to open tun dev: %m");
83
84 r = ioctl(fd, TUNSETIFF, ifr);
85 if (r < 0)
86 return log_netdev_error_errno(netdev, -errno, "TUNSETIFF failed on tun dev: %m");
87
88 if (netdev->kind == NETDEV_KIND_TAP)
89 t = TAP(netdev);
90 else
91 t = TUN(netdev);
92
93 assert(t);
94
95 if (t->user_name) {
96
97 user = t->user_name;
98
99 r = get_user_creds(&user, &uid, NULL, NULL, NULL);
100 if (r < 0)
101 return log_netdev_error_errno(netdev, r, "Cannot resolve user name %s: %m", t->user_name);
102
103 r = ioctl(fd, TUNSETOWNER, uid);
104 if (r < 0)
105 return log_netdev_error_errno(netdev, -errno, "TUNSETOWNER failed on tun dev: %m");
106 }
107
108 if (t->group_name) {
109
110 group = t->group_name;
111
112 r = get_group_creds(&group, &gid);
113 if (r < 0)
114 return log_netdev_error_errno(netdev, r, "Cannot resolve group name %s: %m", t->group_name);
115
116 r = ioctl(fd, TUNSETGROUP, gid);
117 if (r < 0)
118 return log_netdev_error_errno(netdev, -errno, "TUNSETGROUP failed on tun dev: %m");
119
120 }
121
122 r = ioctl(fd, TUNSETPERSIST, 1);
123 if (r < 0)
124 return log_netdev_error_errno(netdev, -errno, "TUNSETPERSIST failed on tun dev: %m");
125
126 return 0;
127 }
128
129 static int netdev_create_tuntap(NetDev *netdev) {
130 struct ifreq ifr = {};
131 int r;
132
133 r = netdev_fill_tuntap_message(netdev, &ifr);
134 if (r < 0)
135 return r;
136
137 return netdev_tuntap_add(netdev, &ifr);
138 }
139
140 static void tuntap_done(NetDev *netdev) {
141 TunTap *t = NULL;
142
143 assert(netdev);
144
145 if (netdev->kind == NETDEV_KIND_TUN)
146 t = TUN(netdev);
147 else
148 t = TAP(netdev);
149
150 assert(t);
151
152 t->user_name = mfree(t->user_name);
153 t->group_name = mfree(t->group_name);
154 }
155
156 static int tuntap_verify(NetDev *netdev, const char *filename) {
157 assert(netdev);
158
159 if (netdev->mtu)
160 log_netdev_warning(netdev, "MTU configured for %s, ignoring", netdev_kind_to_string(netdev->kind));
161
162 if (netdev->mac)
163 log_netdev_warning(netdev, "MAC configured for %s, ignoring", netdev_kind_to_string(netdev->kind));
164
165 return 0;
166 }
167
168 const NetDevVTable tun_vtable = {
169 .object_size = sizeof(TunTap),
170 .sections = "Match\0NetDev\0Tun\0",
171 .config_verify = tuntap_verify,
172 .done = tuntap_done,
173 .create = netdev_create_tuntap,
174 .create_type = NETDEV_CREATE_INDEPENDENT,
175 };
176
177 const NetDevVTable tap_vtable = {
178 .object_size = sizeof(TunTap),
179 .sections = "Match\0NetDev\0Tap\0",
180 .config_verify = tuntap_verify,
181 .done = tuntap_done,
182 .create = netdev_create_tuntap,
183 .create_type = NETDEV_CREATE_INDEPENDENT,
184 };