tizen 2.4 release
[external/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         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         TunTap *t = NULL;
62         const char *user;
63         const char *group;
64         uid_t uid;
65         gid_t gid;
66         int r = 0;
67
68         assert(netdev);
69         assert(ifr);
70
71         fd = open(TUN_DEV, O_RDWR);
72         if (fd < 0) {
73                 log_error_netdev(netdev,
74                                  "Failed to open tun dev: %s",
75                                  strerror(-r));
76                 return r;
77         }
78
79         r = ioctl(fd, TUNSETIFF, ifr);
80         if (r < 0) {
81                 log_error_netdev(netdev,
82                                  "TUNSETIFF failed on tun dev: %s",
83                                  strerror(-r));
84                 return r;
85         }
86
87         if (netdev->kind == NETDEV_KIND_TAP)
88                 t = TAP(netdev);
89         else
90                 t = TUN(netdev);
91
92         assert(t);
93
94         if(t->user_name) {
95
96                 user = t->user_name;
97
98                 r = get_user_creds(&user, &uid, NULL, NULL, NULL);
99                 if (r < 0) {
100                         log_error("Cannot resolve user name %s: %s",
101                                   t->user_name, strerror(-r));
102                         return 0;
103                 }
104
105                 r = ioctl(fd, TUNSETOWNER, uid);
106                 if ( r < 0) {
107                         log_error_netdev(netdev,
108                                          "TUNSETOWNER failed on tun dev: %s",
109                                          strerror(-r));
110                 }
111         }
112
113         if(t->group_name) {
114
115                 group = t->group_name;
116
117                 r = get_group_creds(&group, &gid);
118                 if (r < 0) {
119                         log_error("Cannot resolve group name %s: %s",
120                                   t->group_name, strerror(-r));
121                         return 0;
122                 }
123
124                 r = ioctl(fd, TUNSETGROUP, gid);
125                 if( r < 0) {
126                         log_error_netdev(netdev,
127                                          "TUNSETGROUP failed on tun dev: %s",
128                                          strerror(-r));
129                         return r;
130                 }
131
132         }
133
134         r = ioctl(fd, TUNSETPERSIST, 1);
135         if (r < 0) {
136                 log_error_netdev(netdev,
137                                  "TUNSETPERSIST failed on tun dev: %s",
138                                  strerror(-r));
139                 return r;
140         }
141
142         return r;
143 }
144
145 static int netdev_create_tuntap(NetDev *netdev) {
146         struct ifreq ifr = {};
147         int r;
148
149         r = netdev_fill_tuntap_message(netdev, &ifr);
150         if(r < 0)
151                 return r;
152
153         return netdev_tuntap_add(netdev, &ifr);
154 }
155
156 static void tuntap_done(NetDev *netdev) {
157         TunTap *t = NULL;
158
159         assert(netdev);
160
161         if (netdev->kind == NETDEV_KIND_TUN)
162                 t = TUN(netdev);
163         else
164                 t = TAP(netdev);
165
166         assert(t);
167
168         free(t->user_name);
169         t->user_name = NULL;
170
171         free(t->group_name);
172         t->group_name = NULL;
173 }
174
175 static int tuntap_verify(NetDev *netdev, const char *filename) {
176         TunTap *t = NULL;
177
178         assert(netdev);
179
180         if (netdev->kind == NETDEV_KIND_TUN)
181                 t = TUN(netdev);
182         else
183                 t = TAP(netdev);
184
185         assert(t);
186
187         if (netdev->mtu) {
188                 log_warning_netdev(netdev, "MTU configured for %s, ignoring",
189                                    netdev_kind_to_string(netdev->kind));
190         }
191
192         if (netdev->mac) {
193                 log_warning_netdev(netdev, "MAC configured for %s, ignoring",
194                                    netdev_kind_to_string(netdev->kind));
195         }
196
197         return 0;
198 }
199
200 const NetDevVTable tun_vtable = {
201         .object_size = sizeof(TunTap),
202         .sections = "Match\0NetDev\0Tun\0",
203         .config_verify = tuntap_verify,
204         .done = tuntap_done,
205         .create = netdev_create_tuntap,
206         .create_type = NETDEV_CREATE_INDEPENDENT,
207 };
208
209 const NetDevVTable tap_vtable = {
210         .object_size = sizeof(TunTap),
211         .sections = "Match\0NetDev\0Tap\0",
212         .config_verify = tuntap_verify,
213         .done = tuntap_done,
214         .create = netdev_create_tuntap,
215         .create_type = NETDEV_CREATE_INDEPENDENT,
216 };