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