]>
Commit | Line | Data |
---|---|---|
30ae9dfd SS |
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.h" | |
27 | ||
28 | #define TUN_DEV "/dev/net/tun" | |
29 | ||
30 | ||
31 | static int netdev_fill_tuntap_message(NetDev *netdev, struct ifreq *ifr) { | |
32 | ||
33 | assert(netdev); | |
34 | assert(ifr); | |
35 | ||
36 | memset(ifr, 0, sizeof(*ifr)); | |
37 | ||
38 | if (netdev->kind != NETDEV_KIND_TAP) | |
39 | ifr->ifr_flags |= IFF_TUN; | |
40 | else | |
41 | ifr->ifr_flags |= IFF_TAP; | |
42 | ||
43 | if (netdev->packet_info) | |
44 | ifr->ifr_flags &= ~IFF_NO_PI; | |
45 | else | |
46 | ifr->ifr_flags |= IFF_NO_PI; | |
47 | ||
48 | if (netdev->one_queue) | |
49 | ifr->ifr_flags |= IFF_ONE_QUEUE; | |
50 | ||
51 | if (netdev->multi_queue) | |
52 | ifr->ifr_flags |= IFF_MULTI_QUEUE; | |
53 | ||
54 | strncpy(ifr->ifr_name, netdev->ifname, IFNAMSIZ-1); | |
55 | ||
56 | return 0; | |
57 | } | |
58 | ||
59 | static int netdev_tuntap_add(NetDev *netdev, struct ifreq *ifr) { | |
60 | _cleanup_close_ int fd; | |
61 | const char *user; | |
62 | const char *group; | |
63 | uid_t uid; | |
64 | gid_t gid; | |
65 | int r = 0; | |
66 | ||
67 | fd = open(TUN_DEV, O_RDWR); | |
68 | if (fd < 0) { | |
69 | log_error_netdev(netdev, | |
70 | "Failed to open tun dev: %s", | |
71 | strerror(-r)); | |
72 | return r; | |
73 | } | |
74 | ||
75 | r = ioctl(fd, TUNSETIFF, ifr); | |
76 | if (r < 0) { | |
77 | log_error_netdev(netdev, | |
78 | "TUNSETIFF failed on tun dev: %s", | |
79 | strerror(-r)); | |
80 | return r; | |
81 | } | |
82 | ||
83 | if(netdev->user_name) { | |
84 | ||
85 | user = netdev->user_name; | |
86 | ||
87 | r = get_user_creds(&user, &uid, NULL, NULL, NULL); | |
88 | if (r < 0) { | |
89 | log_error("Cannot resolve user name %s: %s", | |
90 | netdev->user_name, strerror(-r)); | |
91 | return 0; | |
92 | } | |
93 | ||
94 | r = ioctl(fd, TUNSETOWNER, uid); | |
95 | if ( r < 0) { | |
96 | log_error_netdev(netdev, | |
97 | "TUNSETOWNER failed on tun dev: %s", | |
98 | strerror(-r)); | |
99 | } | |
100 | } | |
101 | ||
102 | if(netdev->group_name) { | |
103 | ||
104 | group = netdev->group_name; | |
105 | ||
106 | r = get_group_creds(&group, &gid); | |
107 | if (r < 0) { | |
108 | log_error("Cannot resolve group name %s: %s", | |
109 | netdev->group_name, strerror(-r)); | |
110 | return 0; | |
111 | } | |
112 | ||
113 | r = ioctl(fd, TUNSETGROUP, gid); | |
114 | if( r < 0) { | |
115 | log_error_netdev(netdev, | |
116 | "TUNSETGROUP failed on tun dev: %s", | |
117 | strerror(-r)); | |
118 | return r; | |
119 | } | |
120 | ||
121 | } | |
122 | ||
123 | r = ioctl(fd, TUNSETPERSIST, 1); | |
124 | if (r < 0) { | |
125 | log_error_netdev(netdev, | |
126 | "TUNSETPERSIST failed on tun dev: %s", | |
127 | strerror(-r)); | |
128 | return r; | |
129 | } | |
130 | ||
131 | return r; | |
132 | } | |
133 | ||
134 | int netdev_create_tuntap(NetDev *netdev) { | |
135 | struct ifreq ifr; | |
136 | int r; | |
137 | ||
138 | assert(netdev); | |
139 | assert(netdev->ifname); | |
140 | ||
141 | switch(netdev->kind) { | |
142 | case NETDEV_KIND_TUN: | |
143 | case NETDEV_KIND_TAP: | |
144 | break; | |
145 | default: | |
146 | return -ENOTSUP; | |
147 | } | |
148 | ||
149 | r = netdev_fill_tuntap_message(netdev, &ifr); | |
150 | if(r < 0) | |
151 | return r; | |
152 | ||
153 | log_debug_netdev(netdev, "Creating tuntap netdev: %s", | |
154 | netdev_kind_to_string(netdev->kind)); | |
155 | ||
156 | return netdev_tuntap_add(netdev, &ifr); | |
157 | } |