]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * TUN - Universal TUN/TAP device driver. | |
3 | * Copyright (C) 1999-2002 Maxim Krasnyansky <maxk@qualcomm.com> | |
4 | * | |
5 | * This program is free software; you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License as published by | |
7 | * the Free Software Foundation; either version 2 of the License, or | |
8 | * (at your option) any later version. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * | |
15 | * $Id: tun.c,v 1.15 2002/03/01 02:44:24 maxk Exp $ | |
16 | */ | |
17 | ||
18 | /* | |
19 | * Changes: | |
20 | * | |
ff4cc3ac MK |
21 | * Mike Kershaw <dragorn@kismetwireless.net> 2005/08/14 |
22 | * Add TUNSETLINK ioctl to set the link encapsulation | |
23 | * | |
1da177e4 | 24 | * Mark Smith <markzzzsmith@yahoo.com.au> |
344dc8ed | 25 | * Use eth_random_addr() for tap MAC address. |
1da177e4 LT |
26 | * |
27 | * Harald Roelle <harald.roelle@ifi.lmu.de> 2004/04/20 | |
28 | * Fixes in packet dropping, queue length setting and queue wakeup. | |
29 | * Increased default tx queue length. | |
30 | * Added ethtool API. | |
31 | * Minor cleanups | |
32 | * | |
33 | * Daniel Podlejski <underley@underley.eu.org> | |
34 | * Modifications for 2.3.99-pre5 kernel. | |
35 | */ | |
36 | ||
6b8a66ee JP |
37 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
38 | ||
1da177e4 LT |
39 | #define DRV_NAME "tun" |
40 | #define DRV_VERSION "1.6" | |
41 | #define DRV_DESCRIPTION "Universal TUN/TAP device driver" | |
42 | #define DRV_COPYRIGHT "(C) 1999-2004 Max Krasnyansky <maxk@qualcomm.com>" | |
43 | ||
1da177e4 LT |
44 | #include <linux/module.h> |
45 | #include <linux/errno.h> | |
46 | #include <linux/kernel.h> | |
47 | #include <linux/major.h> | |
48 | #include <linux/slab.h> | |
49 | #include <linux/poll.h> | |
50 | #include <linux/fcntl.h> | |
51 | #include <linux/init.h> | |
52 | #include <linux/skbuff.h> | |
53 | #include <linux/netdevice.h> | |
54 | #include <linux/etherdevice.h> | |
55 | #include <linux/miscdevice.h> | |
56 | #include <linux/ethtool.h> | |
57 | #include <linux/rtnetlink.h> | |
50857e2a | 58 | #include <linux/compat.h> |
1da177e4 LT |
59 | #include <linux/if.h> |
60 | #include <linux/if_arp.h> | |
61 | #include <linux/if_ether.h> | |
62 | #include <linux/if_tun.h> | |
6680ec68 | 63 | #include <linux/if_vlan.h> |
1da177e4 | 64 | #include <linux/crc32.h> |
d647a591 | 65 | #include <linux/nsproxy.h> |
f43798c2 | 66 | #include <linux/virtio_net.h> |
99405162 | 67 | #include <linux/rcupdate.h> |
881d966b | 68 | #include <net/net_namespace.h> |
79d17604 | 69 | #include <net/netns/generic.h> |
f019a7a5 | 70 | #include <net/rtnetlink.h> |
33dccbb0 | 71 | #include <net/sock.h> |
1da177e4 | 72 | |
1da177e4 LT |
73 | #include <asm/uaccess.h> |
74 | ||
14daa021 RR |
75 | /* Uncomment to enable debugging */ |
76 | /* #define TUN_DEBUG 1 */ | |
77 | ||
1da177e4 LT |
78 | #ifdef TUN_DEBUG |
79 | static int debug; | |
14daa021 | 80 | |
6b8a66ee JP |
81 | #define tun_debug(level, tun, fmt, args...) \ |
82 | do { \ | |
83 | if (tun->debug) \ | |
84 | netdev_printk(level, tun->dev, fmt, ##args); \ | |
85 | } while (0) | |
86 | #define DBG1(level, fmt, args...) \ | |
87 | do { \ | |
88 | if (debug == 2) \ | |
89 | printk(level fmt, ##args); \ | |
90 | } while (0) | |
14daa021 | 91 | #else |
6b8a66ee JP |
92 | #define tun_debug(level, tun, fmt, args...) \ |
93 | do { \ | |
94 | if (0) \ | |
95 | netdev_printk(level, tun->dev, fmt, ##args); \ | |
96 | } while (0) | |
97 | #define DBG1(level, fmt, args...) \ | |
98 | do { \ | |
99 | if (0) \ | |
100 | printk(level fmt, ##args); \ | |
101 | } while (0) | |
14daa021 RR |
102 | #endif |
103 | ||
0690899b MT |
104 | #define GOODCOPY_LEN 128 |
105 | ||
f271b2cc MK |
106 | #define FLT_EXACT_COUNT 8 |
107 | struct tap_filter { | |
108 | unsigned int count; /* Number of addrs. Zero means disabled */ | |
109 | u32 mask[2]; /* Mask of the hashed addrs */ | |
110 | unsigned char addr[FLT_EXACT_COUNT][ETH_ALEN]; | |
111 | }; | |
112 | ||
edfb6a14 JW |
113 | /* DEFAULT_MAX_NUM_RSS_QUEUES were choosed to let the rx/tx queues allocated for |
114 | * the netdevice to be fit in one page. So we can make sure the success of | |
115 | * memory allocation. TODO: increase the limit. */ | |
116 | #define MAX_TAP_QUEUES DEFAULT_MAX_NUM_RSS_QUEUES | |
b8732fb7 | 117 | #define MAX_TAP_FLOWS 4096 |
c8d68e6b | 118 | |
96442e42 JW |
119 | #define TUN_FLOW_EXPIRE (3 * HZ) |
120 | ||
54f968d6 JW |
121 | /* A tun_file connects an open character device to a tuntap netdevice. It |
122 | * also contains all socket related strctures (except sock_fprog and tap_filter) | |
123 | * to serve as one transmit queue for tuntap device. The sock_fprog and | |
124 | * tap_filter were kept in tun_struct since they were used for filtering for the | |
36fe8c09 | 125 | * netdevice not for a specific queue (at least I didn't see the requirement for |
54f968d6 | 126 | * this). |
6e914fc7 JW |
127 | * |
128 | * RCU usage: | |
36fe8c09 | 129 | * The tun_file and tun_struct are loosely coupled, the pointer from one to the |
6e914fc7 | 130 | * other can only be read while rcu_read_lock or rtnl_lock is held. |
54f968d6 | 131 | */ |
631ab46b | 132 | struct tun_file { |
54f968d6 JW |
133 | struct sock sk; |
134 | struct socket socket; | |
135 | struct socket_wq wq; | |
6e914fc7 | 136 | struct tun_struct __rcu *tun; |
36b50bab | 137 | struct net *net; |
54f968d6 JW |
138 | struct fasync_struct *fasync; |
139 | /* only used for fasnyc */ | |
140 | unsigned int flags; | |
fb7589a1 PE |
141 | union { |
142 | u16 queue_index; | |
143 | unsigned int ifindex; | |
144 | }; | |
4008e97f JW |
145 | struct list_head next; |
146 | struct tun_struct *detached; | |
631ab46b EB |
147 | }; |
148 | ||
96442e42 JW |
149 | struct tun_flow_entry { |
150 | struct hlist_node hash_link; | |
151 | struct rcu_head rcu; | |
152 | struct tun_struct *tun; | |
153 | ||
154 | u32 rxhash; | |
155 | int queue_index; | |
156 | unsigned long updated; | |
157 | }; | |
158 | ||
159 | #define TUN_NUM_FLOW_ENTRIES 1024 | |
160 | ||
54f968d6 | 161 | /* Since the socket were moved to tun_file, to preserve the behavior of persist |
36fe8c09 | 162 | * device, socket filter, sndbuf and vnet header size were restore when the |
54f968d6 JW |
163 | * file were attached to a persist device. |
164 | */ | |
14daa021 | 165 | struct tun_struct { |
c8d68e6b JW |
166 | struct tun_file __rcu *tfiles[MAX_TAP_QUEUES]; |
167 | unsigned int numqueues; | |
f271b2cc | 168 | unsigned int flags; |
0625c883 EB |
169 | kuid_t owner; |
170 | kgid_t group; | |
14daa021 | 171 | |
14daa021 | 172 | struct net_device *dev; |
c8f44aff | 173 | netdev_features_t set_features; |
88255375 MM |
174 | #define TUN_USER_FEATURES (NETIF_F_HW_CSUM|NETIF_F_TSO_ECN|NETIF_F_TSO| \ |
175 | NETIF_F_TSO6|NETIF_F_UFO) | |
d9d52b51 MT |
176 | |
177 | int vnet_hdr_sz; | |
54f968d6 JW |
178 | int sndbuf; |
179 | struct tap_filter txflt; | |
180 | struct sock_fprog fprog; | |
181 | /* protected by rtnl lock */ | |
182 | bool filter_attached; | |
14daa021 RR |
183 | #ifdef TUN_DEBUG |
184 | int debug; | |
1da177e4 | 185 | #endif |
96442e42 | 186 | spinlock_t lock; |
96442e42 JW |
187 | struct hlist_head flows[TUN_NUM_FLOW_ENTRIES]; |
188 | struct timer_list flow_gc_timer; | |
189 | unsigned long ageing_time; | |
4008e97f JW |
190 | unsigned int numdisabled; |
191 | struct list_head disabled; | |
5dbbaf2d | 192 | void *security; |
b8732fb7 | 193 | u32 flow_count; |
14daa021 | 194 | }; |
1da177e4 | 195 | |
96442e42 JW |
196 | static inline u32 tun_hashfn(u32 rxhash) |
197 | { | |
198 | return rxhash & 0x3ff; | |
199 | } | |
200 | ||
201 | static struct tun_flow_entry *tun_flow_find(struct hlist_head *head, u32 rxhash) | |
202 | { | |
203 | struct tun_flow_entry *e; | |
96442e42 | 204 | |
b67bfe0d | 205 | hlist_for_each_entry_rcu(e, head, hash_link) { |
96442e42 JW |
206 | if (e->rxhash == rxhash) |
207 | return e; | |
208 | } | |
209 | return NULL; | |
210 | } | |
211 | ||
212 | static struct tun_flow_entry *tun_flow_create(struct tun_struct *tun, | |
213 | struct hlist_head *head, | |
214 | u32 rxhash, u16 queue_index) | |
215 | { | |
9fdc6bef ED |
216 | struct tun_flow_entry *e = kmalloc(sizeof(*e), GFP_ATOMIC); |
217 | ||
96442e42 JW |
218 | if (e) { |
219 | tun_debug(KERN_INFO, tun, "create flow: hash %u index %u\n", | |
220 | rxhash, queue_index); | |
221 | e->updated = jiffies; | |
222 | e->rxhash = rxhash; | |
223 | e->queue_index = queue_index; | |
224 | e->tun = tun; | |
225 | hlist_add_head_rcu(&e->hash_link, head); | |
b8732fb7 | 226 | ++tun->flow_count; |
96442e42 JW |
227 | } |
228 | return e; | |
229 | } | |
230 | ||
96442e42 JW |
231 | static void tun_flow_delete(struct tun_struct *tun, struct tun_flow_entry *e) |
232 | { | |
233 | tun_debug(KERN_INFO, tun, "delete flow: hash %u index %u\n", | |
234 | e->rxhash, e->queue_index); | |
235 | hlist_del_rcu(&e->hash_link); | |
9fdc6bef | 236 | kfree_rcu(e, rcu); |
b8732fb7 | 237 | --tun->flow_count; |
96442e42 JW |
238 | } |
239 | ||
240 | static void tun_flow_flush(struct tun_struct *tun) | |
241 | { | |
242 | int i; | |
243 | ||
244 | spin_lock_bh(&tun->lock); | |
245 | for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) { | |
246 | struct tun_flow_entry *e; | |
b67bfe0d | 247 | struct hlist_node *n; |
96442e42 | 248 | |
b67bfe0d | 249 | hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link) |
96442e42 JW |
250 | tun_flow_delete(tun, e); |
251 | } | |
252 | spin_unlock_bh(&tun->lock); | |
253 | } | |
254 | ||
255 | static void tun_flow_delete_by_queue(struct tun_struct *tun, u16 queue_index) | |
256 | { | |
257 | int i; | |
258 | ||
259 | spin_lock_bh(&tun->lock); | |
260 | for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) { | |
261 | struct tun_flow_entry *e; | |
b67bfe0d | 262 | struct hlist_node *n; |
96442e42 | 263 | |
b67bfe0d | 264 | hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link) { |
96442e42 JW |
265 | if (e->queue_index == queue_index) |
266 | tun_flow_delete(tun, e); | |
267 | } | |
268 | } | |
269 | spin_unlock_bh(&tun->lock); | |
270 | } | |
271 | ||
272 | static void tun_flow_cleanup(unsigned long data) | |
273 | { | |
274 | struct tun_struct *tun = (struct tun_struct *)data; | |
275 | unsigned long delay = tun->ageing_time; | |
276 | unsigned long next_timer = jiffies + delay; | |
277 | unsigned long count = 0; | |
278 | int i; | |
279 | ||
280 | tun_debug(KERN_INFO, tun, "tun_flow_cleanup\n"); | |
281 | ||
282 | spin_lock_bh(&tun->lock); | |
283 | for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) { | |
284 | struct tun_flow_entry *e; | |
b67bfe0d | 285 | struct hlist_node *n; |
96442e42 | 286 | |
b67bfe0d | 287 | hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link) { |
96442e42 JW |
288 | unsigned long this_timer; |
289 | count++; | |
290 | this_timer = e->updated + delay; | |
291 | if (time_before_eq(this_timer, jiffies)) | |
292 | tun_flow_delete(tun, e); | |
293 | else if (time_before(this_timer, next_timer)) | |
294 | next_timer = this_timer; | |
295 | } | |
296 | } | |
297 | ||
298 | if (count) | |
299 | mod_timer(&tun->flow_gc_timer, round_jiffies_up(next_timer)); | |
300 | spin_unlock_bh(&tun->lock); | |
301 | } | |
302 | ||
49974420 | 303 | static void tun_flow_update(struct tun_struct *tun, u32 rxhash, |
9e85722d | 304 | struct tun_file *tfile) |
96442e42 JW |
305 | { |
306 | struct hlist_head *head; | |
307 | struct tun_flow_entry *e; | |
308 | unsigned long delay = tun->ageing_time; | |
9e85722d | 309 | u16 queue_index = tfile->queue_index; |
96442e42 JW |
310 | |
311 | if (!rxhash) | |
312 | return; | |
313 | else | |
314 | head = &tun->flows[tun_hashfn(rxhash)]; | |
315 | ||
316 | rcu_read_lock(); | |
317 | ||
9e85722d JW |
318 | /* We may get a very small possibility of OOO during switching, not |
319 | * worth to optimize.*/ | |
320 | if (tun->numqueues == 1 || tfile->detached) | |
96442e42 JW |
321 | goto unlock; |
322 | ||
323 | e = tun_flow_find(head, rxhash); | |
324 | if (likely(e)) { | |
325 | /* TODO: keep queueing to old queue until it's empty? */ | |
326 | e->queue_index = queue_index; | |
327 | e->updated = jiffies; | |
328 | } else { | |
329 | spin_lock_bh(&tun->lock); | |
b8732fb7 JW |
330 | if (!tun_flow_find(head, rxhash) && |
331 | tun->flow_count < MAX_TAP_FLOWS) | |
96442e42 JW |
332 | tun_flow_create(tun, head, rxhash, queue_index); |
333 | ||
334 | if (!timer_pending(&tun->flow_gc_timer)) | |
335 | mod_timer(&tun->flow_gc_timer, | |
336 | round_jiffies_up(jiffies + delay)); | |
337 | spin_unlock_bh(&tun->lock); | |
338 | } | |
339 | ||
340 | unlock: | |
341 | rcu_read_unlock(); | |
342 | } | |
343 | ||
c8d68e6b JW |
344 | /* We try to identify a flow through its rxhash first. The reason that |
345 | * we do not check rxq no. is becuase some cards(e.g 82599), chooses | |
346 | * the rxq based on the txq where the last packet of the flow comes. As | |
347 | * the userspace application move between processors, we may get a | |
348 | * different rxq no. here. If we could not get rxhash, then we would | |
349 | * hope the rxq no. may help here. | |
350 | */ | |
f663dd9a JW |
351 | static u16 tun_select_queue(struct net_device *dev, struct sk_buff *skb, |
352 | void *accel_priv) | |
c8d68e6b JW |
353 | { |
354 | struct tun_struct *tun = netdev_priv(dev); | |
96442e42 | 355 | struct tun_flow_entry *e; |
c8d68e6b JW |
356 | u32 txq = 0; |
357 | u32 numqueues = 0; | |
358 | ||
359 | rcu_read_lock(); | |
92bb73ea | 360 | numqueues = ACCESS_ONCE(tun->numqueues); |
c8d68e6b JW |
361 | |
362 | txq = skb_get_rxhash(skb); | |
363 | if (txq) { | |
96442e42 JW |
364 | e = tun_flow_find(&tun->flows[tun_hashfn(txq)], txq); |
365 | if (e) | |
366 | txq = e->queue_index; | |
367 | else | |
368 | /* use multiply and shift instead of expensive divide */ | |
369 | txq = ((u64)txq * numqueues) >> 32; | |
c8d68e6b JW |
370 | } else if (likely(skb_rx_queue_recorded(skb))) { |
371 | txq = skb_get_rx_queue(skb); | |
372 | while (unlikely(txq >= numqueues)) | |
373 | txq -= numqueues; | |
374 | } | |
375 | ||
376 | rcu_read_unlock(); | |
377 | return txq; | |
378 | } | |
379 | ||
cde8b15f JW |
380 | static inline bool tun_not_capable(struct tun_struct *tun) |
381 | { | |
382 | const struct cred *cred = current_cred(); | |
c260b772 | 383 | struct net *net = dev_net(tun->dev); |
cde8b15f JW |
384 | |
385 | return ((uid_valid(tun->owner) && !uid_eq(cred->euid, tun->owner)) || | |
386 | (gid_valid(tun->group) && !in_egroup_p(tun->group))) && | |
c260b772 | 387 | !ns_capable(net->user_ns, CAP_NET_ADMIN); |
cde8b15f JW |
388 | } |
389 | ||
c8d68e6b JW |
390 | static void tun_set_real_num_queues(struct tun_struct *tun) |
391 | { | |
392 | netif_set_real_num_tx_queues(tun->dev, tun->numqueues); | |
393 | netif_set_real_num_rx_queues(tun->dev, tun->numqueues); | |
394 | } | |
395 | ||
4008e97f JW |
396 | static void tun_disable_queue(struct tun_struct *tun, struct tun_file *tfile) |
397 | { | |
398 | tfile->detached = tun; | |
399 | list_add_tail(&tfile->next, &tun->disabled); | |
400 | ++tun->numdisabled; | |
401 | } | |
402 | ||
d32649d1 | 403 | static struct tun_struct *tun_enable_queue(struct tun_file *tfile) |
4008e97f JW |
404 | { |
405 | struct tun_struct *tun = tfile->detached; | |
406 | ||
407 | tfile->detached = NULL; | |
408 | list_del_init(&tfile->next); | |
409 | --tun->numdisabled; | |
410 | return tun; | |
411 | } | |
412 | ||
4bfb0513 JW |
413 | static void tun_queue_purge(struct tun_file *tfile) |
414 | { | |
415 | skb_queue_purge(&tfile->sk.sk_receive_queue); | |
416 | skb_queue_purge(&tfile->sk.sk_error_queue); | |
417 | } | |
418 | ||
c8d68e6b JW |
419 | static void __tun_detach(struct tun_file *tfile, bool clean) |
420 | { | |
421 | struct tun_file *ntfile; | |
422 | struct tun_struct *tun; | |
c8d68e6b | 423 | |
b8deabd3 JW |
424 | tun = rtnl_dereference(tfile->tun); |
425 | ||
9e85722d | 426 | if (tun && !tfile->detached) { |
c8d68e6b JW |
427 | u16 index = tfile->queue_index; |
428 | BUG_ON(index >= tun->numqueues); | |
c8d68e6b JW |
429 | |
430 | rcu_assign_pointer(tun->tfiles[index], | |
431 | tun->tfiles[tun->numqueues - 1]); | |
b8deabd3 | 432 | ntfile = rtnl_dereference(tun->tfiles[index]); |
c8d68e6b JW |
433 | ntfile->queue_index = index; |
434 | ||
435 | --tun->numqueues; | |
9e85722d JW |
436 | if (clean) { |
437 | rcu_assign_pointer(tfile->tun, NULL); | |
4008e97f | 438 | sock_put(&tfile->sk); |
9e85722d | 439 | } else |
4008e97f | 440 | tun_disable_queue(tun, tfile); |
c8d68e6b JW |
441 | |
442 | synchronize_net(); | |
96442e42 | 443 | tun_flow_delete_by_queue(tun, tun->numqueues + 1); |
c8d68e6b | 444 | /* Drop read queue */ |
4bfb0513 | 445 | tun_queue_purge(tfile); |
c8d68e6b | 446 | tun_set_real_num_queues(tun); |
dd38bd85 | 447 | } else if (tfile->detached && clean) { |
4008e97f | 448 | tun = tun_enable_queue(tfile); |
dd38bd85 JW |
449 | sock_put(&tfile->sk); |
450 | } | |
c8d68e6b JW |
451 | |
452 | if (clean) { | |
af668b3c MT |
453 | if (tun && tun->numqueues == 0 && tun->numdisabled == 0) { |
454 | netif_carrier_off(tun->dev); | |
455 | ||
456 | if (!(tun->flags & TUN_PERSIST) && | |
457 | tun->dev->reg_state == NETREG_REGISTERED) | |
4008e97f | 458 | unregister_netdevice(tun->dev); |
af668b3c | 459 | } |
4008e97f | 460 | |
c8d68e6b JW |
461 | BUG_ON(!test_bit(SOCK_EXTERNALLY_ALLOCATED, |
462 | &tfile->socket.flags)); | |
463 | sk_release_kernel(&tfile->sk); | |
464 | } | |
465 | } | |
466 | ||
467 | static void tun_detach(struct tun_file *tfile, bool clean) | |
468 | { | |
469 | rtnl_lock(); | |
470 | __tun_detach(tfile, clean); | |
471 | rtnl_unlock(); | |
472 | } | |
473 | ||
474 | static void tun_detach_all(struct net_device *dev) | |
475 | { | |
476 | struct tun_struct *tun = netdev_priv(dev); | |
4008e97f | 477 | struct tun_file *tfile, *tmp; |
c8d68e6b JW |
478 | int i, n = tun->numqueues; |
479 | ||
480 | for (i = 0; i < n; i++) { | |
b8deabd3 | 481 | tfile = rtnl_dereference(tun->tfiles[i]); |
c8d68e6b JW |
482 | BUG_ON(!tfile); |
483 | wake_up_all(&tfile->wq.wait); | |
484 | rcu_assign_pointer(tfile->tun, NULL); | |
485 | --tun->numqueues; | |
486 | } | |
9e85722d JW |
487 | list_for_each_entry(tfile, &tun->disabled, next) { |
488 | wake_up_all(&tfile->wq.wait); | |
489 | rcu_assign_pointer(tfile->tun, NULL); | |
490 | } | |
c8d68e6b JW |
491 | BUG_ON(tun->numqueues != 0); |
492 | ||
493 | synchronize_net(); | |
494 | for (i = 0; i < n; i++) { | |
b8deabd3 | 495 | tfile = rtnl_dereference(tun->tfiles[i]); |
c8d68e6b | 496 | /* Drop read queue */ |
4bfb0513 | 497 | tun_queue_purge(tfile); |
c8d68e6b JW |
498 | sock_put(&tfile->sk); |
499 | } | |
4008e97f JW |
500 | list_for_each_entry_safe(tfile, tmp, &tun->disabled, next) { |
501 | tun_enable_queue(tfile); | |
4bfb0513 | 502 | tun_queue_purge(tfile); |
4008e97f JW |
503 | sock_put(&tfile->sk); |
504 | } | |
505 | BUG_ON(tun->numdisabled != 0); | |
dd38bd85 JW |
506 | |
507 | if (tun->flags & TUN_PERSIST) | |
508 | module_put(THIS_MODULE); | |
c8d68e6b JW |
509 | } |
510 | ||
849c9b6f | 511 | static int tun_attach(struct tun_struct *tun, struct file *file, bool skip_filter) |
a7385ba2 | 512 | { |
631ab46b | 513 | struct tun_file *tfile = file->private_data; |
38231b7a | 514 | int err; |
a7385ba2 | 515 | |
5dbbaf2d PM |
516 | err = security_tun_dev_attach(tfile->socket.sk, tun->security); |
517 | if (err < 0) | |
518 | goto out; | |
519 | ||
38231b7a | 520 | err = -EINVAL; |
9e85722d | 521 | if (rtnl_dereference(tfile->tun) && !tfile->detached) |
38231b7a EB |
522 | goto out; |
523 | ||
524 | err = -EBUSY; | |
c8d68e6b JW |
525 | if (!(tun->flags & TUN_TAP_MQ) && tun->numqueues == 1) |
526 | goto out; | |
527 | ||
528 | err = -E2BIG; | |
4008e97f JW |
529 | if (!tfile->detached && |
530 | tun->numqueues + tun->numdisabled == MAX_TAP_QUEUES) | |
38231b7a EB |
531 | goto out; |
532 | ||
533 | err = 0; | |
54f968d6 | 534 | |
c8d68e6b | 535 | /* Re-attach the filter to presist device */ |
849c9b6f | 536 | if (!skip_filter && (tun->filter_attached == true)) { |
54f968d6 JW |
537 | err = sk_attach_filter(&tun->fprog, tfile->socket.sk); |
538 | if (!err) | |
539 | goto out; | |
540 | } | |
c8d68e6b | 541 | tfile->queue_index = tun->numqueues; |
6e914fc7 | 542 | rcu_assign_pointer(tfile->tun, tun); |
c8d68e6b | 543 | rcu_assign_pointer(tun->tfiles[tun->numqueues], tfile); |
c8d68e6b | 544 | tun->numqueues++; |
a7385ba2 | 545 | |
4008e97f JW |
546 | if (tfile->detached) |
547 | tun_enable_queue(tfile); | |
548 | else | |
549 | sock_hold(&tfile->sk); | |
550 | ||
c8d68e6b | 551 | tun_set_real_num_queues(tun); |
a7385ba2 | 552 | |
c8d68e6b JW |
553 | /* device is allowed to go away first, so no need to hold extra |
554 | * refcnt. | |
555 | */ | |
556 | ||
557 | out: | |
558 | return err; | |
631ab46b EB |
559 | } |
560 | ||
561 | static struct tun_struct *__tun_get(struct tun_file *tfile) | |
562 | { | |
6e914fc7 | 563 | struct tun_struct *tun; |
c70f1829 | 564 | |
6e914fc7 JW |
565 | rcu_read_lock(); |
566 | tun = rcu_dereference(tfile->tun); | |
567 | if (tun) | |
568 | dev_hold(tun->dev); | |
569 | rcu_read_unlock(); | |
c70f1829 EB |
570 | |
571 | return tun; | |
631ab46b EB |
572 | } |
573 | ||
574 | static struct tun_struct *tun_get(struct file *file) | |
575 | { | |
576 | return __tun_get(file->private_data); | |
577 | } | |
578 | ||
579 | static void tun_put(struct tun_struct *tun) | |
580 | { | |
6e914fc7 | 581 | dev_put(tun->dev); |
631ab46b EB |
582 | } |
583 | ||
6b8a66ee | 584 | /* TAP filtering */ |
f271b2cc MK |
585 | static void addr_hash_set(u32 *mask, const u8 *addr) |
586 | { | |
587 | int n = ether_crc(ETH_ALEN, addr) >> 26; | |
588 | mask[n >> 5] |= (1 << (n & 31)); | |
589 | } | |
590 | ||
591 | static unsigned int addr_hash_test(const u32 *mask, const u8 *addr) | |
592 | { | |
593 | int n = ether_crc(ETH_ALEN, addr) >> 26; | |
594 | return mask[n >> 5] & (1 << (n & 31)); | |
595 | } | |
596 | ||
597 | static int update_filter(struct tap_filter *filter, void __user *arg) | |
598 | { | |
599 | struct { u8 u[ETH_ALEN]; } *addr; | |
600 | struct tun_filter uf; | |
601 | int err, alen, n, nexact; | |
602 | ||
603 | if (copy_from_user(&uf, arg, sizeof(uf))) | |
604 | return -EFAULT; | |
605 | ||
606 | if (!uf.count) { | |
607 | /* Disabled */ | |
608 | filter->count = 0; | |
609 | return 0; | |
610 | } | |
611 | ||
612 | alen = ETH_ALEN * uf.count; | |
613 | addr = kmalloc(alen, GFP_KERNEL); | |
614 | if (!addr) | |
615 | return -ENOMEM; | |
616 | ||
617 | if (copy_from_user(addr, arg + sizeof(uf), alen)) { | |
618 | err = -EFAULT; | |
619 | goto done; | |
620 | } | |
621 | ||
622 | /* The filter is updated without holding any locks. Which is | |
623 | * perfectly safe. We disable it first and in the worst | |
624 | * case we'll accept a few undesired packets. */ | |
625 | filter->count = 0; | |
626 | wmb(); | |
627 | ||
628 | /* Use first set of addresses as an exact filter */ | |
629 | for (n = 0; n < uf.count && n < FLT_EXACT_COUNT; n++) | |
630 | memcpy(filter->addr[n], addr[n].u, ETH_ALEN); | |
631 | ||
632 | nexact = n; | |
633 | ||
cfbf84fc AW |
634 | /* Remaining multicast addresses are hashed, |
635 | * unicast will leave the filter disabled. */ | |
f271b2cc | 636 | memset(filter->mask, 0, sizeof(filter->mask)); |
cfbf84fc AW |
637 | for (; n < uf.count; n++) { |
638 | if (!is_multicast_ether_addr(addr[n].u)) { | |
639 | err = 0; /* no filter */ | |
640 | goto done; | |
641 | } | |
f271b2cc | 642 | addr_hash_set(filter->mask, addr[n].u); |
cfbf84fc | 643 | } |
f271b2cc MK |
644 | |
645 | /* For ALLMULTI just set the mask to all ones. | |
646 | * This overrides the mask populated above. */ | |
647 | if ((uf.flags & TUN_FLT_ALLMULTI)) | |
648 | memset(filter->mask, ~0, sizeof(filter->mask)); | |
649 | ||
650 | /* Now enable the filter */ | |
651 | wmb(); | |
652 | filter->count = nexact; | |
653 | ||
654 | /* Return the number of exact filters */ | |
655 | err = nexact; | |
656 | ||
657 | done: | |
658 | kfree(addr); | |
659 | return err; | |
660 | } | |
661 | ||
662 | /* Returns: 0 - drop, !=0 - accept */ | |
663 | static int run_filter(struct tap_filter *filter, const struct sk_buff *skb) | |
664 | { | |
665 | /* Cannot use eth_hdr(skb) here because skb_mac_hdr() is incorrect | |
666 | * at this point. */ | |
667 | struct ethhdr *eh = (struct ethhdr *) skb->data; | |
668 | int i; | |
669 | ||
670 | /* Exact match */ | |
671 | for (i = 0; i < filter->count; i++) | |
2e42e474 | 672 | if (ether_addr_equal(eh->h_dest, filter->addr[i])) |
f271b2cc MK |
673 | return 1; |
674 | ||
675 | /* Inexact match (multicast only) */ | |
676 | if (is_multicast_ether_addr(eh->h_dest)) | |
677 | return addr_hash_test(filter->mask, eh->h_dest); | |
678 | ||
679 | return 0; | |
680 | } | |
681 | ||
682 | /* | |
683 | * Checks whether the packet is accepted or not. | |
684 | * Returns: 0 - drop, !=0 - accept | |
685 | */ | |
686 | static int check_filter(struct tap_filter *filter, const struct sk_buff *skb) | |
687 | { | |
688 | if (!filter->count) | |
689 | return 1; | |
690 | ||
691 | return run_filter(filter, skb); | |
692 | } | |
693 | ||
1da177e4 LT |
694 | /* Network device part of the driver */ |
695 | ||
7282d491 | 696 | static const struct ethtool_ops tun_ethtool_ops; |
1da177e4 | 697 | |
c70f1829 EB |
698 | /* Net device detach from fd. */ |
699 | static void tun_net_uninit(struct net_device *dev) | |
700 | { | |
c8d68e6b | 701 | tun_detach_all(dev); |
c70f1829 EB |
702 | } |
703 | ||
1da177e4 LT |
704 | /* Net device open. */ |
705 | static int tun_net_open(struct net_device *dev) | |
706 | { | |
c8d68e6b | 707 | netif_tx_start_all_queues(dev); |
1da177e4 LT |
708 | return 0; |
709 | } | |
710 | ||
711 | /* Net device close. */ | |
712 | static int tun_net_close(struct net_device *dev) | |
713 | { | |
c8d68e6b | 714 | netif_tx_stop_all_queues(dev); |
1da177e4 LT |
715 | return 0; |
716 | } | |
717 | ||
718 | /* Net device start xmit */ | |
424efe9c | 719 | static netdev_tx_t tun_net_xmit(struct sk_buff *skb, struct net_device *dev) |
1da177e4 LT |
720 | { |
721 | struct tun_struct *tun = netdev_priv(dev); | |
c8d68e6b | 722 | int txq = skb->queue_mapping; |
6e914fc7 | 723 | struct tun_file *tfile; |
1da177e4 | 724 | |
6e914fc7 | 725 | rcu_read_lock(); |
c8d68e6b JW |
726 | tfile = rcu_dereference(tun->tfiles[txq]); |
727 | ||
1da177e4 | 728 | /* Drop packet if interface is not attached */ |
c8d68e6b | 729 | if (txq >= tun->numqueues) |
1da177e4 LT |
730 | goto drop; |
731 | ||
6e914fc7 JW |
732 | tun_debug(KERN_INFO, tun, "tun_net_xmit %d\n", skb->len); |
733 | ||
c8d68e6b JW |
734 | BUG_ON(!tfile); |
735 | ||
f271b2cc MK |
736 | /* Drop if the filter does not like it. |
737 | * This is a noop if the filter is disabled. | |
738 | * Filter can be enabled only for the TAP devices. */ | |
739 | if (!check_filter(&tun->txflt, skb)) | |
740 | goto drop; | |
741 | ||
54f968d6 JW |
742 | if (tfile->socket.sk->sk_filter && |
743 | sk_filter(tfile->socket.sk, skb)) | |
99405162 MT |
744 | goto drop; |
745 | ||
36fe8c09 | 746 | /* Limit the number of packets queued by dividing txq length with the |
c8d68e6b JW |
747 | * number of queues. |
748 | */ | |
54f968d6 | 749 | if (skb_queue_len(&tfile->socket.sk->sk_receive_queue) |
5d097109 MT |
750 | >= dev->tx_queue_len / tun->numqueues) |
751 | goto drop; | |
1da177e4 | 752 | |
7bf66305 JW |
753 | if (unlikely(skb_orphan_frags(skb, GFP_ATOMIC))) |
754 | goto drop; | |
755 | ||
eda29772 RC |
756 | if (skb->sk) { |
757 | sock_tx_timestamp(skb->sk, &skb_shinfo(skb)->tx_flags); | |
758 | sw_tx_timestamp(skb); | |
759 | } | |
760 | ||
0110d6f2 | 761 | /* Orphan the skb - required as we might hang on to it |
7bf66305 JW |
762 | * for indefinite time. |
763 | */ | |
0110d6f2 MT |
764 | skb_orphan(skb); |
765 | ||
f8af75f3 ED |
766 | nf_reset(skb); |
767 | ||
f271b2cc | 768 | /* Enqueue packet */ |
54f968d6 | 769 | skb_queue_tail(&tfile->socket.sk->sk_receive_queue, skb); |
1da177e4 LT |
770 | |
771 | /* Notify and wake up reader process */ | |
54f968d6 JW |
772 | if (tfile->flags & TUN_FASYNC) |
773 | kill_fasync(&tfile->fasync, SIGIO, POLL_IN); | |
774 | wake_up_interruptible_poll(&tfile->wq.wait, POLLIN | | |
05c2828c | 775 | POLLRDNORM | POLLRDBAND); |
6e914fc7 JW |
776 | |
777 | rcu_read_unlock(); | |
6ed10654 | 778 | return NETDEV_TX_OK; |
1da177e4 LT |
779 | |
780 | drop: | |
09f75cd7 | 781 | dev->stats.tx_dropped++; |
149d36f7 | 782 | skb_tx_error(skb); |
1da177e4 | 783 | kfree_skb(skb); |
6e914fc7 | 784 | rcu_read_unlock(); |
6ed10654 | 785 | return NETDEV_TX_OK; |
1da177e4 LT |
786 | } |
787 | ||
f271b2cc | 788 | static void tun_net_mclist(struct net_device *dev) |
1da177e4 | 789 | { |
f271b2cc MK |
790 | /* |
791 | * This callback is supposed to deal with mc filter in | |
792 | * _rx_ path and has nothing to do with the _tx_ path. | |
793 | * In rx path we always accept everything userspace gives us. | |
794 | */ | |
1da177e4 LT |
795 | } |
796 | ||
4885a504 ES |
797 | #define MIN_MTU 68 |
798 | #define MAX_MTU 65535 | |
799 | ||
800 | static int | |
801 | tun_net_change_mtu(struct net_device *dev, int new_mtu) | |
802 | { | |
803 | if (new_mtu < MIN_MTU || new_mtu + dev->hard_header_len > MAX_MTU) | |
804 | return -EINVAL; | |
805 | dev->mtu = new_mtu; | |
806 | return 0; | |
807 | } | |
808 | ||
c8f44aff MM |
809 | static netdev_features_t tun_net_fix_features(struct net_device *dev, |
810 | netdev_features_t features) | |
88255375 MM |
811 | { |
812 | struct tun_struct *tun = netdev_priv(dev); | |
813 | ||
814 | return (features & tun->set_features) | (features & ~TUN_USER_FEATURES); | |
815 | } | |
bebd097a NH |
816 | #ifdef CONFIG_NET_POLL_CONTROLLER |
817 | static void tun_poll_controller(struct net_device *dev) | |
818 | { | |
819 | /* | |
820 | * Tun only receives frames when: | |
821 | * 1) the char device endpoint gets data from user space | |
822 | * 2) the tun socket gets a sendmsg call from user space | |
823 | * Since both of those are syncronous operations, we are guaranteed | |
824 | * never to have pending data when we poll for it | |
825 | * so theres nothing to do here but return. | |
826 | * We need this though so netpoll recognizes us as an interface that | |
827 | * supports polling, which enables bridge devices in virt setups to | |
828 | * still use netconsole | |
829 | */ | |
830 | return; | |
831 | } | |
832 | #endif | |
758e43b7 | 833 | static const struct net_device_ops tun_netdev_ops = { |
c70f1829 | 834 | .ndo_uninit = tun_net_uninit, |
758e43b7 SH |
835 | .ndo_open = tun_net_open, |
836 | .ndo_stop = tun_net_close, | |
00829823 | 837 | .ndo_start_xmit = tun_net_xmit, |
758e43b7 | 838 | .ndo_change_mtu = tun_net_change_mtu, |
88255375 | 839 | .ndo_fix_features = tun_net_fix_features, |
c8d68e6b | 840 | .ndo_select_queue = tun_select_queue, |
bebd097a NH |
841 | #ifdef CONFIG_NET_POLL_CONTROLLER |
842 | .ndo_poll_controller = tun_poll_controller, | |
843 | #endif | |
758e43b7 SH |
844 | }; |
845 | ||
846 | static const struct net_device_ops tap_netdev_ops = { | |
c70f1829 | 847 | .ndo_uninit = tun_net_uninit, |
758e43b7 SH |
848 | .ndo_open = tun_net_open, |
849 | .ndo_stop = tun_net_close, | |
00829823 | 850 | .ndo_start_xmit = tun_net_xmit, |
758e43b7 | 851 | .ndo_change_mtu = tun_net_change_mtu, |
88255375 | 852 | .ndo_fix_features = tun_net_fix_features, |
afc4b13d | 853 | .ndo_set_rx_mode = tun_net_mclist, |
758e43b7 SH |
854 | .ndo_set_mac_address = eth_mac_addr, |
855 | .ndo_validate_addr = eth_validate_addr, | |
c8d68e6b | 856 | .ndo_select_queue = tun_select_queue, |
bebd097a NH |
857 | #ifdef CONFIG_NET_POLL_CONTROLLER |
858 | .ndo_poll_controller = tun_poll_controller, | |
859 | #endif | |
758e43b7 SH |
860 | }; |
861 | ||
944a1376 | 862 | static void tun_flow_init(struct tun_struct *tun) |
96442e42 JW |
863 | { |
864 | int i; | |
865 | ||
96442e42 JW |
866 | for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) |
867 | INIT_HLIST_HEAD(&tun->flows[i]); | |
868 | ||
869 | tun->ageing_time = TUN_FLOW_EXPIRE; | |
870 | setup_timer(&tun->flow_gc_timer, tun_flow_cleanup, (unsigned long)tun); | |
871 | mod_timer(&tun->flow_gc_timer, | |
872 | round_jiffies_up(jiffies + tun->ageing_time)); | |
96442e42 JW |
873 | } |
874 | ||
875 | static void tun_flow_uninit(struct tun_struct *tun) | |
876 | { | |
877 | del_timer_sync(&tun->flow_gc_timer); | |
878 | tun_flow_flush(tun); | |
96442e42 JW |
879 | } |
880 | ||
1da177e4 LT |
881 | /* Initialize net device. */ |
882 | static void tun_net_init(struct net_device *dev) | |
883 | { | |
884 | struct tun_struct *tun = netdev_priv(dev); | |
6aa20a22 | 885 | |
1da177e4 LT |
886 | switch (tun->flags & TUN_TYPE_MASK) { |
887 | case TUN_TUN_DEV: | |
758e43b7 SH |
888 | dev->netdev_ops = &tun_netdev_ops; |
889 | ||
1da177e4 LT |
890 | /* Point-to-Point TUN Device */ |
891 | dev->hard_header_len = 0; | |
892 | dev->addr_len = 0; | |
893 | dev->mtu = 1500; | |
894 | ||
895 | /* Zero header length */ | |
6aa20a22 | 896 | dev->type = ARPHRD_NONE; |
1da177e4 LT |
897 | dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST; |
898 | dev->tx_queue_len = TUN_READQ_SIZE; /* We prefer our own queue length */ | |
899 | break; | |
900 | ||
901 | case TUN_TAP_DEV: | |
7a0a9608 | 902 | dev->netdev_ops = &tap_netdev_ops; |
1da177e4 | 903 | /* Ethernet TAP Device */ |
1da177e4 | 904 | ether_setup(dev); |
550fd08c | 905 | dev->priv_flags &= ~IFF_TX_SKB_SHARING; |
a676847b | 906 | dev->priv_flags |= IFF_LIVE_ADDR_CHANGE; |
36226a8d | 907 | |
f2cedb63 | 908 | eth_hw_addr_random(dev); |
36226a8d | 909 | |
1da177e4 LT |
910 | dev->tx_queue_len = TUN_READQ_SIZE; /* We prefer our own queue length */ |
911 | break; | |
912 | } | |
913 | } | |
914 | ||
915 | /* Character device part */ | |
916 | ||
917 | /* Poll */ | |
c8d68e6b | 918 | static unsigned int tun_chr_poll(struct file *file, poll_table *wait) |
6aa20a22 | 919 | { |
b2430de3 EB |
920 | struct tun_file *tfile = file->private_data; |
921 | struct tun_struct *tun = __tun_get(tfile); | |
3c8a9c63 | 922 | struct sock *sk; |
33dccbb0 | 923 | unsigned int mask = 0; |
1da177e4 LT |
924 | |
925 | if (!tun) | |
eac9e902 | 926 | return POLLERR; |
1da177e4 | 927 | |
54f968d6 | 928 | sk = tfile->socket.sk; |
3c8a9c63 | 929 | |
6b8a66ee | 930 | tun_debug(KERN_INFO, tun, "tun_chr_poll\n"); |
1da177e4 | 931 | |
54f968d6 | 932 | poll_wait(file, &tfile->wq.wait, wait); |
6aa20a22 | 933 | |
89f56d1e | 934 | if (!skb_queue_empty(&sk->sk_receive_queue)) |
1da177e4 LT |
935 | mask |= POLLIN | POLLRDNORM; |
936 | ||
33dccbb0 HX |
937 | if (sock_writeable(sk) || |
938 | (!test_and_set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags) && | |
939 | sock_writeable(sk))) | |
940 | mask |= POLLOUT | POLLWRNORM; | |
941 | ||
c70f1829 EB |
942 | if (tun->dev->reg_state != NETREG_REGISTERED) |
943 | mask = POLLERR; | |
944 | ||
631ab46b | 945 | tun_put(tun); |
1da177e4 LT |
946 | return mask; |
947 | } | |
948 | ||
f42157cb RR |
949 | /* prepad is the amount to reserve at front. len is length after that. |
950 | * linear is a hint as to how much to copy (usually headers). */ | |
54f968d6 | 951 | static struct sk_buff *tun_alloc_skb(struct tun_file *tfile, |
6f7c156c | 952 | size_t prepad, size_t len, |
953 | size_t linear, int noblock) | |
f42157cb | 954 | { |
54f968d6 | 955 | struct sock *sk = tfile->socket.sk; |
f42157cb | 956 | struct sk_buff *skb; |
33dccbb0 | 957 | int err; |
f42157cb RR |
958 | |
959 | /* Under a page? Don't bother with paged skb. */ | |
0eca93bc | 960 | if (prepad + len < PAGE_SIZE || !linear) |
33dccbb0 | 961 | linear = len; |
f42157cb | 962 | |
33dccbb0 | 963 | skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock, |
28d64271 | 964 | &err, 0); |
f42157cb | 965 | if (!skb) |
33dccbb0 | 966 | return ERR_PTR(err); |
f42157cb RR |
967 | |
968 | skb_reserve(skb, prepad); | |
969 | skb_put(skb, linear); | |
33dccbb0 HX |
970 | skb->data_len = len - linear; |
971 | skb->len += len - linear; | |
f42157cb RR |
972 | |
973 | return skb; | |
974 | } | |
975 | ||
1da177e4 | 976 | /* Get packet from user space buffer */ |
54f968d6 JW |
977 | static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile, |
978 | void *msg_control, const struct iovec *iv, | |
979 | size_t total_len, size_t count, int noblock) | |
1da177e4 | 980 | { |
09640e63 | 981 | struct tun_pi pi = { 0, cpu_to_be16(ETH_P_IP) }; |
1da177e4 | 982 | struct sk_buff *skb; |
3dd5c330 | 983 | size_t len = total_len, align = NET_SKB_PAD, linear; |
f43798c2 | 984 | struct virtio_net_hdr gso = { 0 }; |
96f8d9ec | 985 | int good_linear; |
6f26c9a7 | 986 | int offset = 0; |
0690899b MT |
987 | int copylen; |
988 | bool zerocopy = false; | |
989 | int err; | |
49974420 | 990 | u32 rxhash; |
1da177e4 LT |
991 | |
992 | if (!(tun->flags & TUN_NO_PI)) { | |
15718ea0 | 993 | if (len < sizeof(pi)) |
1da177e4 | 994 | return -EINVAL; |
15718ea0 | 995 | len -= sizeof(pi); |
1da177e4 | 996 | |
6f26c9a7 | 997 | if (memcpy_fromiovecend((void *)&pi, iv, 0, sizeof(pi))) |
1da177e4 | 998 | return -EFAULT; |
6f26c9a7 | 999 | offset += sizeof(pi); |
1da177e4 LT |
1000 | } |
1001 | ||
f43798c2 | 1002 | if (tun->flags & TUN_VNET_HDR) { |
15718ea0 | 1003 | if (len < tun->vnet_hdr_sz) |
f43798c2 | 1004 | return -EINVAL; |
15718ea0 | 1005 | len -= tun->vnet_hdr_sz; |
f43798c2 | 1006 | |
6f26c9a7 | 1007 | if (memcpy_fromiovecend((void *)&gso, iv, offset, sizeof(gso))) |
f43798c2 RR |
1008 | return -EFAULT; |
1009 | ||
4909122f HX |
1010 | if ((gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) && |
1011 | gso.csum_start + gso.csum_offset + 2 > gso.hdr_len) | |
1012 | gso.hdr_len = gso.csum_start + gso.csum_offset + 2; | |
1013 | ||
f43798c2 RR |
1014 | if (gso.hdr_len > len) |
1015 | return -EINVAL; | |
d9d52b51 | 1016 | offset += tun->vnet_hdr_sz; |
f43798c2 RR |
1017 | } |
1018 | ||
e01bf1c8 | 1019 | if ((tun->flags & TUN_TYPE_MASK) == TUN_TAP_DEV) { |
a504b86e | 1020 | align += NET_IP_ALIGN; |
0eca93bc HX |
1021 | if (unlikely(len < ETH_HLEN || |
1022 | (gso.hdr_len && gso.hdr_len < ETH_HLEN))) | |
e01bf1c8 RR |
1023 | return -EINVAL; |
1024 | } | |
6aa20a22 | 1025 | |
96f8d9ec JW |
1026 | good_linear = SKB_MAX_HEAD(align); |
1027 | ||
88529176 JW |
1028 | if (msg_control) { |
1029 | /* There are 256 bytes to be copied in skb, so there is | |
1030 | * enough room for skb expand head in case it is used. | |
0690899b MT |
1031 | * The rest of the buffer is mapped from userspace. |
1032 | */ | |
88529176 | 1033 | copylen = gso.hdr_len ? gso.hdr_len : GOODCOPY_LEN; |
96f8d9ec JW |
1034 | if (copylen > good_linear) |
1035 | copylen = good_linear; | |
3dd5c330 | 1036 | linear = copylen; |
88529176 JW |
1037 | if (iov_pages(iv, offset + copylen, count) <= MAX_SKB_FRAGS) |
1038 | zerocopy = true; | |
1039 | } | |
1040 | ||
1041 | if (!zerocopy) { | |
0690899b | 1042 | copylen = len; |
96f8d9ec JW |
1043 | if (gso.hdr_len > good_linear) |
1044 | linear = good_linear; | |
1045 | else | |
1046 | linear = gso.hdr_len; | |
3dd5c330 | 1047 | } |
0690899b | 1048 | |
3dd5c330 | 1049 | skb = tun_alloc_skb(tfile, align, copylen, linear, noblock); |
33dccbb0 HX |
1050 | if (IS_ERR(skb)) { |
1051 | if (PTR_ERR(skb) != -EAGAIN) | |
1052 | tun->dev->stats.rx_dropped++; | |
1053 | return PTR_ERR(skb); | |
1da177e4 LT |
1054 | } |
1055 | ||
0690899b MT |
1056 | if (zerocopy) |
1057 | err = zerocopy_sg_from_iovec(skb, iv, offset, count); | |
88529176 | 1058 | else { |
0690899b | 1059 | err = skb_copy_datagram_from_iovec(skb, 0, iv, offset, len); |
88529176 JW |
1060 | if (!err && msg_control) { |
1061 | struct ubuf_info *uarg = msg_control; | |
1062 | uarg->callback(uarg, false); | |
1063 | } | |
1064 | } | |
0690899b MT |
1065 | |
1066 | if (err) { | |
09f75cd7 | 1067 | tun->dev->stats.rx_dropped++; |
8f22757e | 1068 | kfree_skb(skb); |
1da177e4 | 1069 | return -EFAULT; |
8f22757e | 1070 | } |
1da177e4 | 1071 | |
f43798c2 RR |
1072 | if (gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) { |
1073 | if (!skb_partial_csum_set(skb, gso.csum_start, | |
1074 | gso.csum_offset)) { | |
1075 | tun->dev->stats.rx_frame_errors++; | |
1076 | kfree_skb(skb); | |
1077 | return -EINVAL; | |
1078 | } | |
88255375 | 1079 | } |
f43798c2 | 1080 | |
1da177e4 LT |
1081 | switch (tun->flags & TUN_TYPE_MASK) { |
1082 | case TUN_TUN_DEV: | |
f09f7ee2 AWC |
1083 | if (tun->flags & TUN_NO_PI) { |
1084 | switch (skb->data[0] & 0xf0) { | |
1085 | case 0x40: | |
1086 | pi.proto = htons(ETH_P_IP); | |
1087 | break; | |
1088 | case 0x60: | |
1089 | pi.proto = htons(ETH_P_IPV6); | |
1090 | break; | |
1091 | default: | |
1092 | tun->dev->stats.rx_dropped++; | |
1093 | kfree_skb(skb); | |
1094 | return -EINVAL; | |
1095 | } | |
1096 | } | |
1097 | ||
459a98ed | 1098 | skb_reset_mac_header(skb); |
1da177e4 | 1099 | skb->protocol = pi.proto; |
4c13eb66 | 1100 | skb->dev = tun->dev; |
1da177e4 LT |
1101 | break; |
1102 | case TUN_TAP_DEV: | |
1103 | skb->protocol = eth_type_trans(skb, tun->dev); | |
1104 | break; | |
6403eab1 | 1105 | } |
1da177e4 | 1106 | |
f43798c2 RR |
1107 | if (gso.gso_type != VIRTIO_NET_HDR_GSO_NONE) { |
1108 | pr_debug("GSO!\n"); | |
1109 | switch (gso.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) { | |
1110 | case VIRTIO_NET_HDR_GSO_TCPV4: | |
c9af6db4 | 1111 | skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4; |
f43798c2 RR |
1112 | break; |
1113 | case VIRTIO_NET_HDR_GSO_TCPV6: | |
c9af6db4 | 1114 | skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6; |
f43798c2 | 1115 | break; |
e36aa25a | 1116 | case VIRTIO_NET_HDR_GSO_UDP: |
c9af6db4 | 1117 | skb_shinfo(skb)->gso_type = SKB_GSO_UDP; |
e36aa25a | 1118 | break; |
f43798c2 RR |
1119 | default: |
1120 | tun->dev->stats.rx_frame_errors++; | |
1121 | kfree_skb(skb); | |
1122 | return -EINVAL; | |
1123 | } | |
1124 | ||
1125 | if (gso.gso_type & VIRTIO_NET_HDR_GSO_ECN) | |
c9af6db4 | 1126 | skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN; |
f43798c2 RR |
1127 | |
1128 | skb_shinfo(skb)->gso_size = gso.gso_size; | |
1129 | if (skb_shinfo(skb)->gso_size == 0) { | |
1130 | tun->dev->stats.rx_frame_errors++; | |
1131 | kfree_skb(skb); | |
1132 | return -EINVAL; | |
1133 | } | |
1134 | ||
1135 | /* Header must be checked, and gso_segs computed. */ | |
1136 | skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY; | |
1137 | skb_shinfo(skb)->gso_segs = 0; | |
1138 | } | |
6aa20a22 | 1139 | |
0690899b MT |
1140 | /* copy skb_ubuf_info for callback when skb has no error */ |
1141 | if (zerocopy) { | |
1142 | skb_shinfo(skb)->destructor_arg = msg_control; | |
1143 | skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY; | |
c9af6db4 | 1144 | skb_shinfo(skb)->tx_flags |= SKBTX_SHARED_FRAG; |
0690899b MT |
1145 | } |
1146 | ||
76fe4581 | 1147 | skb_reset_network_header(skb); |
40893fd0 | 1148 | skb_probe_transport_header(skb, 0); |
38502af7 | 1149 | |
49974420 | 1150 | rxhash = skb_get_rxhash(skb); |
1da177e4 | 1151 | netif_rx_ni(skb); |
6aa20a22 | 1152 | |
09f75cd7 JG |
1153 | tun->dev->stats.rx_packets++; |
1154 | tun->dev->stats.rx_bytes += len; | |
1da177e4 | 1155 | |
9e85722d | 1156 | tun_flow_update(tun, rxhash, tfile); |
0690899b | 1157 | return total_len; |
6aa20a22 | 1158 | } |
1da177e4 | 1159 | |
ee0b3e67 BP |
1160 | static ssize_t tun_chr_aio_write(struct kiocb *iocb, const struct iovec *iv, |
1161 | unsigned long count, loff_t pos) | |
1da177e4 | 1162 | { |
33dccbb0 | 1163 | struct file *file = iocb->ki_filp; |
ab46d779 | 1164 | struct tun_struct *tun = tun_get(file); |
54f968d6 | 1165 | struct tun_file *tfile = file->private_data; |
631ab46b | 1166 | ssize_t result; |
1da177e4 LT |
1167 | |
1168 | if (!tun) | |
1169 | return -EBADFD; | |
1170 | ||
6b8a66ee | 1171 | tun_debug(KERN_INFO, tun, "tun_chr_write %ld\n", count); |
1da177e4 | 1172 | |
54f968d6 JW |
1173 | result = tun_get_user(tun, tfile, NULL, iv, iov_length(iv, count), |
1174 | count, file->f_flags & O_NONBLOCK); | |
631ab46b EB |
1175 | |
1176 | tun_put(tun); | |
1177 | return result; | |
1da177e4 LT |
1178 | } |
1179 | ||
1da177e4 | 1180 | /* Put packet to the user space buffer */ |
6f7c156c | 1181 | static ssize_t tun_put_user(struct tun_struct *tun, |
54f968d6 | 1182 | struct tun_file *tfile, |
6f7c156c | 1183 | struct sk_buff *skb, |
1184 | const struct iovec *iv, int len) | |
1da177e4 LT |
1185 | { |
1186 | struct tun_pi pi = { 0, skb->protocol }; | |
bbd37626 | 1187 | ssize_t total = 0; |
e6fd07c8 | 1188 | int vlan_offset = 0, copied; |
1da177e4 LT |
1189 | |
1190 | if (!(tun->flags & TUN_NO_PI)) { | |
1191 | if ((len -= sizeof(pi)) < 0) | |
1192 | return -EINVAL; | |
1193 | ||
1194 | if (len < skb->len) { | |
1195 | /* Packet will be striped */ | |
1196 | pi.flags |= TUN_PKT_STRIP; | |
1197 | } | |
6aa20a22 | 1198 | |
43b39dcd | 1199 | if (memcpy_toiovecend(iv, (void *) &pi, 0, sizeof(pi))) |
1da177e4 LT |
1200 | return -EFAULT; |
1201 | total += sizeof(pi); | |
6aa20a22 | 1202 | } |
1da177e4 | 1203 | |
f43798c2 RR |
1204 | if (tun->flags & TUN_VNET_HDR) { |
1205 | struct virtio_net_hdr gso = { 0 }; /* no info leak */ | |
d9d52b51 | 1206 | if ((len -= tun->vnet_hdr_sz) < 0) |
f43798c2 RR |
1207 | return -EINVAL; |
1208 | ||
1209 | if (skb_is_gso(skb)) { | |
1210 | struct skb_shared_info *sinfo = skb_shinfo(skb); | |
1211 | ||
1212 | /* This is a hint as to how much should be linear. */ | |
1213 | gso.hdr_len = skb_headlen(skb); | |
1214 | gso.gso_size = sinfo->gso_size; | |
1215 | if (sinfo->gso_type & SKB_GSO_TCPV4) | |
1216 | gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV4; | |
1217 | else if (sinfo->gso_type & SKB_GSO_TCPV6) | |
1218 | gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV6; | |
e36aa25a SS |
1219 | else if (sinfo->gso_type & SKB_GSO_UDP) |
1220 | gso.gso_type = VIRTIO_NET_HDR_GSO_UDP; | |
ef3db4a5 | 1221 | else { |
6b8a66ee | 1222 | pr_err("unexpected GSO type: " |
ef3db4a5 MT |
1223 | "0x%x, gso_size %d, hdr_len %d\n", |
1224 | sinfo->gso_type, gso.gso_size, | |
1225 | gso.hdr_len); | |
1226 | print_hex_dump(KERN_ERR, "tun: ", | |
1227 | DUMP_PREFIX_NONE, | |
1228 | 16, 1, skb->head, | |
1229 | min((int)gso.hdr_len, 64), true); | |
1230 | WARN_ON_ONCE(1); | |
1231 | return -EINVAL; | |
1232 | } | |
f43798c2 RR |
1233 | if (sinfo->gso_type & SKB_GSO_TCP_ECN) |
1234 | gso.gso_type |= VIRTIO_NET_HDR_GSO_ECN; | |
1235 | } else | |
1236 | gso.gso_type = VIRTIO_NET_HDR_GSO_NONE; | |
1237 | ||
1238 | if (skb->ip_summed == CHECKSUM_PARTIAL) { | |
1239 | gso.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM; | |
55508d60 | 1240 | gso.csum_start = skb_checksum_start_offset(skb); |
f43798c2 | 1241 | gso.csum_offset = skb->csum_offset; |
10a8d94a JW |
1242 | } else if (skb->ip_summed == CHECKSUM_UNNECESSARY) { |
1243 | gso.flags = VIRTIO_NET_HDR_F_DATA_VALID; | |
f43798c2 RR |
1244 | } /* else everything is zero */ |
1245 | ||
43b39dcd MT |
1246 | if (unlikely(memcpy_toiovecend(iv, (void *)&gso, total, |
1247 | sizeof(gso)))) | |
f43798c2 | 1248 | return -EFAULT; |
d9d52b51 | 1249 | total += tun->vnet_hdr_sz; |
f43798c2 RR |
1250 | } |
1251 | ||
e6fd07c8 JW |
1252 | copied = total; |
1253 | total += skb->len; | |
6680ec68 JW |
1254 | if (!vlan_tx_tag_present(skb)) { |
1255 | len = min_t(int, skb->len, len); | |
1256 | } else { | |
1257 | int copy, ret; | |
bbd37626 DM |
1258 | struct { |
1259 | __be16 h_vlan_proto; | |
1260 | __be16 h_vlan_TCI; | |
1261 | } veth; | |
6680ec68 JW |
1262 | |
1263 | veth.h_vlan_proto = skb->vlan_proto; | |
1264 | veth.h_vlan_TCI = htons(vlan_tx_tag_get(skb)); | |
1265 | ||
1266 | vlan_offset = offsetof(struct vlan_ethhdr, h_vlan_proto); | |
1267 | len = min_t(int, skb->len + VLAN_HLEN, len); | |
e6fd07c8 | 1268 | total += VLAN_HLEN; |
6680ec68 JW |
1269 | |
1270 | copy = min_t(int, vlan_offset, len); | |
e6fd07c8 | 1271 | ret = skb_copy_datagram_const_iovec(skb, 0, iv, copied, copy); |
6680ec68 | 1272 | len -= copy; |
e6fd07c8 | 1273 | copied += copy; |
6680ec68 JW |
1274 | if (ret || !len) |
1275 | goto done; | |
1276 | ||
1277 | copy = min_t(int, sizeof(veth), len); | |
e6fd07c8 | 1278 | ret = memcpy_toiovecend(iv, (void *)&veth, copied, copy); |
6680ec68 | 1279 | len -= copy; |
e6fd07c8 | 1280 | copied += copy; |
6680ec68 JW |
1281 | if (ret || !len) |
1282 | goto done; | |
1283 | } | |
1da177e4 | 1284 | |
e6fd07c8 | 1285 | skb_copy_datagram_const_iovec(skb, vlan_offset, iv, copied, len); |
1da177e4 | 1286 | |
6680ec68 | 1287 | done: |
09f75cd7 JG |
1288 | tun->dev->stats.tx_packets++; |
1289 | tun->dev->stats.tx_bytes += len; | |
1da177e4 LT |
1290 | |
1291 | return total; | |
1292 | } | |
1293 | ||
54f968d6 | 1294 | static ssize_t tun_do_read(struct tun_struct *tun, struct tun_file *tfile, |
05c2828c MT |
1295 | struct kiocb *iocb, const struct iovec *iv, |
1296 | ssize_t len, int noblock) | |
1da177e4 | 1297 | { |
1da177e4 LT |
1298 | DECLARE_WAITQUEUE(wait, current); |
1299 | struct sk_buff *skb; | |
05c2828c | 1300 | ssize_t ret = 0; |
1da177e4 | 1301 | |
3872baf6 | 1302 | tun_debug(KERN_INFO, tun, "tun_do_read\n"); |
1da177e4 | 1303 | |
61a5ff15 | 1304 | if (unlikely(!noblock)) |
54f968d6 | 1305 | add_wait_queue(&tfile->wq.wait, &wait); |
1da177e4 | 1306 | while (len) { |
5c0c52c9 MT |
1307 | if (unlikely(!noblock)) |
1308 | current->state = TASK_INTERRUPTIBLE; | |
1da177e4 LT |
1309 | |
1310 | /* Read frames from the queue */ | |
54f968d6 | 1311 | if (!(skb = skb_dequeue(&tfile->socket.sk->sk_receive_queue))) { |
05c2828c | 1312 | if (noblock) { |
1da177e4 LT |
1313 | ret = -EAGAIN; |
1314 | break; | |
1315 | } | |
1316 | if (signal_pending(current)) { | |
1317 | ret = -ERESTARTSYS; | |
1318 | break; | |
1319 | } | |
c70f1829 EB |
1320 | if (tun->dev->reg_state != NETREG_REGISTERED) { |
1321 | ret = -EIO; | |
1322 | break; | |
1323 | } | |
1da177e4 LT |
1324 | |
1325 | /* Nothing to read, let's sleep */ | |
1326 | schedule(); | |
1327 | continue; | |
1328 | } | |
1da177e4 | 1329 | |
54f968d6 | 1330 | ret = tun_put_user(tun, tfile, skb, iv, len); |
f271b2cc MK |
1331 | kfree_skb(skb); |
1332 | break; | |
1da177e4 LT |
1333 | } |
1334 | ||
5c0c52c9 MT |
1335 | if (unlikely(!noblock)) { |
1336 | current->state = TASK_RUNNING; | |
54f968d6 | 1337 | remove_wait_queue(&tfile->wq.wait, &wait); |
5c0c52c9 | 1338 | } |
1da177e4 | 1339 | |
05c2828c MT |
1340 | return ret; |
1341 | } | |
1342 | ||
1343 | static ssize_t tun_chr_aio_read(struct kiocb *iocb, const struct iovec *iv, | |
1344 | unsigned long count, loff_t pos) | |
1345 | { | |
1346 | struct file *file = iocb->ki_filp; | |
1347 | struct tun_file *tfile = file->private_data; | |
1348 | struct tun_struct *tun = __tun_get(tfile); | |
1349 | ssize_t len, ret; | |
1350 | ||
1351 | if (!tun) | |
1352 | return -EBADFD; | |
1353 | len = iov_length(iv, count); | |
1354 | if (len < 0) { | |
1355 | ret = -EINVAL; | |
1356 | goto out; | |
1357 | } | |
1358 | ||
54f968d6 JW |
1359 | ret = tun_do_read(tun, tfile, iocb, iv, len, |
1360 | file->f_flags & O_NONBLOCK); | |
05c2828c | 1361 | ret = min_t(ssize_t, ret, len); |
d0b7da8a ZYW |
1362 | if (ret > 0) |
1363 | iocb->ki_pos = ret; | |
631ab46b EB |
1364 | out: |
1365 | tun_put(tun); | |
1da177e4 LT |
1366 | return ret; |
1367 | } | |
1368 | ||
96442e42 JW |
1369 | static void tun_free_netdev(struct net_device *dev) |
1370 | { | |
1371 | struct tun_struct *tun = netdev_priv(dev); | |
1372 | ||
4008e97f | 1373 | BUG_ON(!(list_empty(&tun->disabled))); |
96442e42 | 1374 | tun_flow_uninit(tun); |
5dbbaf2d | 1375 | security_tun_dev_free_security(tun->security); |
96442e42 JW |
1376 | free_netdev(dev); |
1377 | } | |
1378 | ||
1da177e4 LT |
1379 | static void tun_setup(struct net_device *dev) |
1380 | { | |
1381 | struct tun_struct *tun = netdev_priv(dev); | |
1382 | ||
0625c883 EB |
1383 | tun->owner = INVALID_UID; |
1384 | tun->group = INVALID_GID; | |
1da177e4 | 1385 | |
1da177e4 | 1386 | dev->ethtool_ops = &tun_ethtool_ops; |
96442e42 | 1387 | dev->destructor = tun_free_netdev; |
1da177e4 LT |
1388 | } |
1389 | ||
f019a7a5 EB |
1390 | /* Trivial set of netlink ops to allow deleting tun or tap |
1391 | * device with netlink. | |
1392 | */ | |
1393 | static int tun_validate(struct nlattr *tb[], struct nlattr *data[]) | |
1394 | { | |
1395 | return -EINVAL; | |
1396 | } | |
1397 | ||
1398 | static struct rtnl_link_ops tun_link_ops __read_mostly = { | |
1399 | .kind = DRV_NAME, | |
1400 | .priv_size = sizeof(struct tun_struct), | |
1401 | .setup = tun_setup, | |
1402 | .validate = tun_validate, | |
1403 | }; | |
1404 | ||
33dccbb0 HX |
1405 | static void tun_sock_write_space(struct sock *sk) |
1406 | { | |
54f968d6 | 1407 | struct tun_file *tfile; |
43815482 | 1408 | wait_queue_head_t *wqueue; |
33dccbb0 HX |
1409 | |
1410 | if (!sock_writeable(sk)) | |
1411 | return; | |
1412 | ||
33dccbb0 HX |
1413 | if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags)) |
1414 | return; | |
1415 | ||
43815482 ED |
1416 | wqueue = sk_sleep(sk); |
1417 | if (wqueue && waitqueue_active(wqueue)) | |
1418 | wake_up_interruptible_sync_poll(wqueue, POLLOUT | | |
05c2828c | 1419 | POLLWRNORM | POLLWRBAND); |
c722c625 | 1420 | |
54f968d6 JW |
1421 | tfile = container_of(sk, struct tun_file, sk); |
1422 | kill_fasync(&tfile->fasync, SIGIO, POLL_OUT); | |
33dccbb0 HX |
1423 | } |
1424 | ||
05c2828c MT |
1425 | static int tun_sendmsg(struct kiocb *iocb, struct socket *sock, |
1426 | struct msghdr *m, size_t total_len) | |
1427 | { | |
54f968d6 JW |
1428 | int ret; |
1429 | struct tun_file *tfile = container_of(sock, struct tun_file, socket); | |
1430 | struct tun_struct *tun = __tun_get(tfile); | |
1431 | ||
1432 | if (!tun) | |
1433 | return -EBADFD; | |
54f968d6 JW |
1434 | ret = tun_get_user(tun, tfile, m->msg_control, m->msg_iov, total_len, |
1435 | m->msg_iovlen, m->msg_flags & MSG_DONTWAIT); | |
1436 | tun_put(tun); | |
1437 | return ret; | |
05c2828c MT |
1438 | } |
1439 | ||
1440 | static int tun_recvmsg(struct kiocb *iocb, struct socket *sock, | |
1441 | struct msghdr *m, size_t total_len, | |
1442 | int flags) | |
1443 | { | |
54f968d6 JW |
1444 | struct tun_file *tfile = container_of(sock, struct tun_file, socket); |
1445 | struct tun_struct *tun = __tun_get(tfile); | |
05c2828c | 1446 | int ret; |
54f968d6 JW |
1447 | |
1448 | if (!tun) | |
1449 | return -EBADFD; | |
1450 | ||
eda29772 | 1451 | if (flags & ~(MSG_DONTWAIT|MSG_TRUNC|MSG_ERRQUEUE)) { |
3811ae76 G |
1452 | ret = -EINVAL; |
1453 | goto out; | |
1454 | } | |
eda29772 RC |
1455 | if (flags & MSG_ERRQUEUE) { |
1456 | ret = sock_recv_errqueue(sock->sk, m, total_len, | |
1457 | SOL_PACKET, TUN_TX_TIMESTAMP); | |
1458 | goto out; | |
1459 | } | |
54f968d6 | 1460 | ret = tun_do_read(tun, tfile, iocb, m->msg_iov, total_len, |
05c2828c MT |
1461 | flags & MSG_DONTWAIT); |
1462 | if (ret > total_len) { | |
1463 | m->msg_flags |= MSG_TRUNC; | |
1464 | ret = flags & MSG_TRUNC ? ret : total_len; | |
1465 | } | |
3811ae76 | 1466 | out: |
54f968d6 | 1467 | tun_put(tun); |
05c2828c MT |
1468 | return ret; |
1469 | } | |
1470 | ||
1ab5ecb9 SK |
1471 | static int tun_release(struct socket *sock) |
1472 | { | |
1473 | if (sock->sk) | |
1474 | sock_put(sock->sk); | |
1475 | return 0; | |
1476 | } | |
1477 | ||
05c2828c MT |
1478 | /* Ops structure to mimic raw sockets with tun */ |
1479 | static const struct proto_ops tun_socket_ops = { | |
1480 | .sendmsg = tun_sendmsg, | |
1481 | .recvmsg = tun_recvmsg, | |
1ab5ecb9 | 1482 | .release = tun_release, |
05c2828c MT |
1483 | }; |
1484 | ||
33dccbb0 HX |
1485 | static struct proto tun_proto = { |
1486 | .name = "tun", | |
1487 | .owner = THIS_MODULE, | |
54f968d6 | 1488 | .obj_size = sizeof(struct tun_file), |
33dccbb0 | 1489 | }; |
f019a7a5 | 1490 | |
980c9e8c DW |
1491 | static int tun_flags(struct tun_struct *tun) |
1492 | { | |
1493 | int flags = 0; | |
1494 | ||
1495 | if (tun->flags & TUN_TUN_DEV) | |
1496 | flags |= IFF_TUN; | |
1497 | else | |
1498 | flags |= IFF_TAP; | |
1499 | ||
1500 | if (tun->flags & TUN_NO_PI) | |
1501 | flags |= IFF_NO_PI; | |
1502 | ||
5d097109 MT |
1503 | /* This flag has no real effect. We track the value for backwards |
1504 | * compatibility. | |
1505 | */ | |
980c9e8c DW |
1506 | if (tun->flags & TUN_ONE_QUEUE) |
1507 | flags |= IFF_ONE_QUEUE; | |
1508 | ||
1509 | if (tun->flags & TUN_VNET_HDR) | |
1510 | flags |= IFF_VNET_HDR; | |
1511 | ||
c8d68e6b JW |
1512 | if (tun->flags & TUN_TAP_MQ) |
1513 | flags |= IFF_MULTI_QUEUE; | |
1514 | ||
274038f8 PE |
1515 | if (tun->flags & TUN_PERSIST) |
1516 | flags |= IFF_PERSIST; | |
1517 | ||
980c9e8c DW |
1518 | return flags; |
1519 | } | |
1520 | ||
1521 | static ssize_t tun_show_flags(struct device *dev, struct device_attribute *attr, | |
1522 | char *buf) | |
1523 | { | |
1524 | struct tun_struct *tun = netdev_priv(to_net_dev(dev)); | |
1525 | return sprintf(buf, "0x%x\n", tun_flags(tun)); | |
1526 | } | |
1527 | ||
1528 | static ssize_t tun_show_owner(struct device *dev, struct device_attribute *attr, | |
1529 | char *buf) | |
1530 | { | |
1531 | struct tun_struct *tun = netdev_priv(to_net_dev(dev)); | |
0625c883 EB |
1532 | return uid_valid(tun->owner)? |
1533 | sprintf(buf, "%u\n", | |
1534 | from_kuid_munged(current_user_ns(), tun->owner)): | |
1535 | sprintf(buf, "-1\n"); | |
980c9e8c DW |
1536 | } |
1537 | ||
1538 | static ssize_t tun_show_group(struct device *dev, struct device_attribute *attr, | |
1539 | char *buf) | |
1540 | { | |
1541 | struct tun_struct *tun = netdev_priv(to_net_dev(dev)); | |
0625c883 EB |
1542 | return gid_valid(tun->group) ? |
1543 | sprintf(buf, "%u\n", | |
1544 | from_kgid_munged(current_user_ns(), tun->group)): | |
1545 | sprintf(buf, "-1\n"); | |
980c9e8c DW |
1546 | } |
1547 | ||
1548 | static DEVICE_ATTR(tun_flags, 0444, tun_show_flags, NULL); | |
1549 | static DEVICE_ATTR(owner, 0444, tun_show_owner, NULL); | |
1550 | static DEVICE_ATTR(group, 0444, tun_show_group, NULL); | |
1551 | ||
d647a591 | 1552 | static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr) |
1da177e4 LT |
1553 | { |
1554 | struct tun_struct *tun; | |
54f968d6 | 1555 | struct tun_file *tfile = file->private_data; |
1da177e4 LT |
1556 | struct net_device *dev; |
1557 | int err; | |
1558 | ||
7c0c3b1a JW |
1559 | if (tfile->detached) |
1560 | return -EINVAL; | |
1561 | ||
74a3e5a7 EB |
1562 | dev = __dev_get_by_name(net, ifr->ifr_name); |
1563 | if (dev) { | |
f85ba780 DW |
1564 | if (ifr->ifr_flags & IFF_TUN_EXCL) |
1565 | return -EBUSY; | |
74a3e5a7 EB |
1566 | if ((ifr->ifr_flags & IFF_TUN) && dev->netdev_ops == &tun_netdev_ops) |
1567 | tun = netdev_priv(dev); | |
1568 | else if ((ifr->ifr_flags & IFF_TAP) && dev->netdev_ops == &tap_netdev_ops) | |
1569 | tun = netdev_priv(dev); | |
1570 | else | |
1571 | return -EINVAL; | |
1572 | ||
8e6d91ae JW |
1573 | if (!!(ifr->ifr_flags & IFF_MULTI_QUEUE) != |
1574 | !!(tun->flags & TUN_TAP_MQ)) | |
1575 | return -EINVAL; | |
1576 | ||
cde8b15f | 1577 | if (tun_not_capable(tun)) |
2b980dbd | 1578 | return -EPERM; |
5dbbaf2d | 1579 | err = security_tun_dev_open(tun->security); |
2b980dbd PM |
1580 | if (err < 0) |
1581 | return err; | |
1582 | ||
849c9b6f | 1583 | err = tun_attach(tun, file, ifr->ifr_flags & IFF_NOFILTER); |
a7385ba2 EB |
1584 | if (err < 0) |
1585 | return err; | |
4008e97f JW |
1586 | |
1587 | if (tun->flags & TUN_TAP_MQ && | |
e8dbad66 JW |
1588 | (tun->numqueues + tun->numdisabled > 1)) { |
1589 | /* One or more queue has already been attached, no need | |
1590 | * to initialize the device again. | |
1591 | */ | |
1592 | return 0; | |
1593 | } | |
6aa20a22 | 1594 | } |
1da177e4 LT |
1595 | else { |
1596 | char *name; | |
1597 | unsigned long flags = 0; | |
edfb6a14 JW |
1598 | int queues = ifr->ifr_flags & IFF_MULTI_QUEUE ? |
1599 | MAX_TAP_QUEUES : 1; | |
1da177e4 | 1600 | |
c260b772 | 1601 | if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) |
ca6bb5d7 | 1602 | return -EPERM; |
2b980dbd PM |
1603 | err = security_tun_dev_create(); |
1604 | if (err < 0) | |
1605 | return err; | |
ca6bb5d7 | 1606 | |
1da177e4 LT |
1607 | /* Set dev type */ |
1608 | if (ifr->ifr_flags & IFF_TUN) { | |
1609 | /* TUN device */ | |
1610 | flags |= TUN_TUN_DEV; | |
1611 | name = "tun%d"; | |
1612 | } else if (ifr->ifr_flags & IFF_TAP) { | |
1613 | /* TAP device */ | |
1614 | flags |= TUN_TAP_DEV; | |
1615 | name = "tap%d"; | |
6aa20a22 | 1616 | } else |
36989b90 | 1617 | return -EINVAL; |
6aa20a22 | 1618 | |
1da177e4 LT |
1619 | if (*ifr->ifr_name) |
1620 | name = ifr->ifr_name; | |
1621 | ||
c8d68e6b | 1622 | dev = alloc_netdev_mqs(sizeof(struct tun_struct), name, |
edfb6a14 JW |
1623 | tun_setup, queues, queues); |
1624 | ||
1da177e4 LT |
1625 | if (!dev) |
1626 | return -ENOMEM; | |
1627 | ||
fc54c658 | 1628 | dev_net_set(dev, net); |
f019a7a5 | 1629 | dev->rtnl_link_ops = &tun_link_ops; |
fb7589a1 | 1630 | dev->ifindex = tfile->ifindex; |
758e43b7 | 1631 | |
1da177e4 LT |
1632 | tun = netdev_priv(dev); |
1633 | tun->dev = dev; | |
1634 | tun->flags = flags; | |
f271b2cc | 1635 | tun->txflt.count = 0; |
d9d52b51 | 1636 | tun->vnet_hdr_sz = sizeof(struct virtio_net_hdr); |
33dccbb0 | 1637 | |
54f968d6 JW |
1638 | tun->filter_attached = false; |
1639 | tun->sndbuf = tfile->socket.sk->sk_sndbuf; | |
33dccbb0 | 1640 | |
96442e42 JW |
1641 | spin_lock_init(&tun->lock); |
1642 | ||
5dbbaf2d PM |
1643 | err = security_tun_dev_alloc_security(&tun->security); |
1644 | if (err < 0) | |
1645 | goto err_free_dev; | |
2b980dbd | 1646 | |
1da177e4 | 1647 | tun_net_init(dev); |
944a1376 | 1648 | tun_flow_init(tun); |
96442e42 | 1649 | |
88255375 | 1650 | dev->hw_features = NETIF_F_SG | NETIF_F_FRAGLIST | |
6680ec68 JW |
1651 | TUN_USER_FEATURES | NETIF_F_HW_VLAN_CTAG_TX | |
1652 | NETIF_F_HW_VLAN_STAG_TX; | |
88255375 | 1653 | dev->features = dev->hw_features; |
c0317998 | 1654 | dev->vlan_features = dev->features; |
88255375 | 1655 | |
4008e97f | 1656 | INIT_LIST_HEAD(&tun->disabled); |
849c9b6f | 1657 | err = tun_attach(tun, file, false); |
eb0fb363 | 1658 | if (err < 0) |
662ca437 | 1659 | goto err_free_flow; |
eb0fb363 | 1660 | |
1da177e4 LT |
1661 | err = register_netdevice(tun->dev); |
1662 | if (err < 0) | |
662ca437 | 1663 | goto err_detach; |
9c3fea6a | 1664 | |
980c9e8c DW |
1665 | if (device_create_file(&tun->dev->dev, &dev_attr_tun_flags) || |
1666 | device_create_file(&tun->dev->dev, &dev_attr_owner) || | |
1667 | device_create_file(&tun->dev->dev, &dev_attr_group)) | |
6b8a66ee | 1668 | pr_err("Failed to create tun sysfs files\n"); |
1da177e4 LT |
1669 | } |
1670 | ||
af668b3c MT |
1671 | netif_carrier_on(tun->dev); |
1672 | ||
6b8a66ee | 1673 | tun_debug(KERN_INFO, tun, "tun_set_iff\n"); |
1da177e4 LT |
1674 | |
1675 | if (ifr->ifr_flags & IFF_NO_PI) | |
1676 | tun->flags |= TUN_NO_PI; | |
a26af1e0 NF |
1677 | else |
1678 | tun->flags &= ~TUN_NO_PI; | |
1da177e4 | 1679 | |
5d097109 MT |
1680 | /* This flag has no real effect. We track the value for backwards |
1681 | * compatibility. | |
1682 | */ | |
1da177e4 LT |
1683 | if (ifr->ifr_flags & IFF_ONE_QUEUE) |
1684 | tun->flags |= TUN_ONE_QUEUE; | |
a26af1e0 NF |
1685 | else |
1686 | tun->flags &= ~TUN_ONE_QUEUE; | |
1da177e4 | 1687 | |
f43798c2 RR |
1688 | if (ifr->ifr_flags & IFF_VNET_HDR) |
1689 | tun->flags |= TUN_VNET_HDR; | |
1690 | else | |
1691 | tun->flags &= ~TUN_VNET_HDR; | |
1692 | ||
c8d68e6b JW |
1693 | if (ifr->ifr_flags & IFF_MULTI_QUEUE) |
1694 | tun->flags |= TUN_TAP_MQ; | |
1695 | else | |
1696 | tun->flags &= ~TUN_TAP_MQ; | |
1697 | ||
e35259a9 MK |
1698 | /* Make sure persistent devices do not get stuck in |
1699 | * xoff state. | |
1700 | */ | |
1701 | if (netif_running(tun->dev)) | |
c8d68e6b | 1702 | netif_tx_wake_all_queues(tun->dev); |
e35259a9 | 1703 | |
1da177e4 LT |
1704 | strcpy(ifr->ifr_name, tun->dev->name); |
1705 | return 0; | |
1706 | ||
662ca437 JW |
1707 | err_detach: |
1708 | tun_detach_all(dev); | |
1709 | err_free_flow: | |
1710 | tun_flow_uninit(tun); | |
1711 | security_tun_dev_free_security(tun->security); | |
1712 | err_free_dev: | |
1da177e4 | 1713 | free_netdev(dev); |
1da177e4 LT |
1714 | return err; |
1715 | } | |
1716 | ||
9ce99cf6 | 1717 | static void tun_get_iff(struct net *net, struct tun_struct *tun, |
876bfd4d | 1718 | struct ifreq *ifr) |
e3b99556 | 1719 | { |
6b8a66ee | 1720 | tun_debug(KERN_INFO, tun, "tun_get_iff\n"); |
e3b99556 MM |
1721 | |
1722 | strcpy(ifr->ifr_name, tun->dev->name); | |
1723 | ||
980c9e8c | 1724 | ifr->ifr_flags = tun_flags(tun); |
e3b99556 | 1725 | |
e3b99556 MM |
1726 | } |
1727 | ||
5228ddc9 RR |
1728 | /* This is like a cut-down ethtool ops, except done via tun fd so no |
1729 | * privs required. */ | |
88255375 | 1730 | static int set_offload(struct tun_struct *tun, unsigned long arg) |
5228ddc9 | 1731 | { |
c8f44aff | 1732 | netdev_features_t features = 0; |
5228ddc9 RR |
1733 | |
1734 | if (arg & TUN_F_CSUM) { | |
88255375 | 1735 | features |= NETIF_F_HW_CSUM; |
5228ddc9 RR |
1736 | arg &= ~TUN_F_CSUM; |
1737 | ||
1738 | if (arg & (TUN_F_TSO4|TUN_F_TSO6)) { | |
1739 | if (arg & TUN_F_TSO_ECN) { | |
1740 | features |= NETIF_F_TSO_ECN; | |
1741 | arg &= ~TUN_F_TSO_ECN; | |
1742 | } | |
1743 | if (arg & TUN_F_TSO4) | |
1744 | features |= NETIF_F_TSO; | |
1745 | if (arg & TUN_F_TSO6) | |
1746 | features |= NETIF_F_TSO6; | |
1747 | arg &= ~(TUN_F_TSO4|TUN_F_TSO6); | |
1748 | } | |
e36aa25a SS |
1749 | |
1750 | if (arg & TUN_F_UFO) { | |
1751 | features |= NETIF_F_UFO; | |
1752 | arg &= ~TUN_F_UFO; | |
1753 | } | |
5228ddc9 RR |
1754 | } |
1755 | ||
1756 | /* This gives the user a way to test for new features in future by | |
1757 | * trying to set them. */ | |
1758 | if (arg) | |
1759 | return -EINVAL; | |
1760 | ||
88255375 MM |
1761 | tun->set_features = features; |
1762 | netdev_update_features(tun->dev); | |
5228ddc9 RR |
1763 | |
1764 | return 0; | |
1765 | } | |
1766 | ||
c8d68e6b JW |
1767 | static void tun_detach_filter(struct tun_struct *tun, int n) |
1768 | { | |
1769 | int i; | |
1770 | struct tun_file *tfile; | |
1771 | ||
1772 | for (i = 0; i < n; i++) { | |
b8deabd3 | 1773 | tfile = rtnl_dereference(tun->tfiles[i]); |
c8d68e6b JW |
1774 | sk_detach_filter(tfile->socket.sk); |
1775 | } | |
1776 | ||
1777 | tun->filter_attached = false; | |
1778 | } | |
1779 | ||
1780 | static int tun_attach_filter(struct tun_struct *tun) | |
1781 | { | |
1782 | int i, ret = 0; | |
1783 | struct tun_file *tfile; | |
1784 | ||
1785 | for (i = 0; i < tun->numqueues; i++) { | |
b8deabd3 | 1786 | tfile = rtnl_dereference(tun->tfiles[i]); |
c8d68e6b JW |
1787 | ret = sk_attach_filter(&tun->fprog, tfile->socket.sk); |
1788 | if (ret) { | |
1789 | tun_detach_filter(tun, i); | |
1790 | return ret; | |
1791 | } | |
1792 | } | |
1793 | ||
1794 | tun->filter_attached = true; | |
1795 | return ret; | |
1796 | } | |
1797 | ||
1798 | static void tun_set_sndbuf(struct tun_struct *tun) | |
1799 | { | |
1800 | struct tun_file *tfile; | |
1801 | int i; | |
1802 | ||
1803 | for (i = 0; i < tun->numqueues; i++) { | |
b8deabd3 | 1804 | tfile = rtnl_dereference(tun->tfiles[i]); |
c8d68e6b JW |
1805 | tfile->socket.sk->sk_sndbuf = tun->sndbuf; |
1806 | } | |
1807 | } | |
1808 | ||
cde8b15f JW |
1809 | static int tun_set_queue(struct file *file, struct ifreq *ifr) |
1810 | { | |
1811 | struct tun_file *tfile = file->private_data; | |
1812 | struct tun_struct *tun; | |
cde8b15f JW |
1813 | int ret = 0; |
1814 | ||
1815 | rtnl_lock(); | |
1816 | ||
1817 | if (ifr->ifr_flags & IFF_ATTACH_QUEUE) { | |
4008e97f | 1818 | tun = tfile->detached; |
5dbbaf2d | 1819 | if (!tun) { |
cde8b15f | 1820 | ret = -EINVAL; |
5dbbaf2d PM |
1821 | goto unlock; |
1822 | } | |
1823 | ret = security_tun_dev_attach_queue(tun->security); | |
1824 | if (ret < 0) | |
1825 | goto unlock; | |
849c9b6f | 1826 | ret = tun_attach(tun, file, false); |
4008e97f | 1827 | } else if (ifr->ifr_flags & IFF_DETACH_QUEUE) { |
b8deabd3 | 1828 | tun = rtnl_dereference(tfile->tun); |
9e85722d | 1829 | if (!tun || !(tun->flags & TUN_TAP_MQ) || tfile->detached) |
4008e97f JW |
1830 | ret = -EINVAL; |
1831 | else | |
1832 | __tun_detach(tfile, false); | |
1833 | } else | |
cde8b15f JW |
1834 | ret = -EINVAL; |
1835 | ||
5dbbaf2d | 1836 | unlock: |
cde8b15f JW |
1837 | rtnl_unlock(); |
1838 | return ret; | |
1839 | } | |
1840 | ||
50857e2a AB |
1841 | static long __tun_chr_ioctl(struct file *file, unsigned int cmd, |
1842 | unsigned long arg, int ifreq_len) | |
1da177e4 | 1843 | { |
36b50bab | 1844 | struct tun_file *tfile = file->private_data; |
631ab46b | 1845 | struct tun_struct *tun; |
1da177e4 LT |
1846 | void __user* argp = (void __user*)arg; |
1847 | struct ifreq ifr; | |
0625c883 EB |
1848 | kuid_t owner; |
1849 | kgid_t group; | |
33dccbb0 | 1850 | int sndbuf; |
d9d52b51 | 1851 | int vnet_hdr_sz; |
fb7589a1 | 1852 | unsigned int ifindex; |
f271b2cc | 1853 | int ret; |
1da177e4 | 1854 | |
cde8b15f | 1855 | if (cmd == TUNSETIFF || cmd == TUNSETQUEUE || _IOC_TYPE(cmd) == 0x89) { |
50857e2a | 1856 | if (copy_from_user(&ifr, argp, ifreq_len)) |
1da177e4 | 1857 | return -EFAULT; |
8bbb1813 | 1858 | } else { |
a117dacd | 1859 | memset(&ifr, 0, sizeof(ifr)); |
8bbb1813 | 1860 | } |
631ab46b EB |
1861 | if (cmd == TUNGETFEATURES) { |
1862 | /* Currently this just means: "what IFF flags are valid?". | |
1863 | * This is needed because we never checked for invalid flags on | |
1864 | * TUNSETIFF. */ | |
1865 | return put_user(IFF_TUN | IFF_TAP | IFF_NO_PI | IFF_ONE_QUEUE | | |
cde8b15f | 1866 | IFF_VNET_HDR | IFF_MULTI_QUEUE, |
631ab46b | 1867 | (unsigned int __user*)argp); |
cde8b15f JW |
1868 | } else if (cmd == TUNSETQUEUE) |
1869 | return tun_set_queue(file, &ifr); | |
631ab46b | 1870 | |
c8d68e6b | 1871 | ret = 0; |
876bfd4d HX |
1872 | rtnl_lock(); |
1873 | ||
36b50bab | 1874 | tun = __tun_get(tfile); |
1da177e4 | 1875 | if (cmd == TUNSETIFF && !tun) { |
1da177e4 LT |
1876 | ifr.ifr_name[IFNAMSIZ-1] = '\0'; |
1877 | ||
876bfd4d | 1878 | ret = tun_set_iff(tfile->net, file, &ifr); |
1da177e4 | 1879 | |
876bfd4d HX |
1880 | if (ret) |
1881 | goto unlock; | |
1da177e4 | 1882 | |
50857e2a | 1883 | if (copy_to_user(argp, &ifr, ifreq_len)) |
876bfd4d HX |
1884 | ret = -EFAULT; |
1885 | goto unlock; | |
1da177e4 | 1886 | } |
fb7589a1 PE |
1887 | if (cmd == TUNSETIFINDEX) { |
1888 | ret = -EPERM; | |
1889 | if (tun) | |
1890 | goto unlock; | |
1891 | ||
1892 | ret = -EFAULT; | |
1893 | if (copy_from_user(&ifindex, argp, sizeof(ifindex))) | |
1894 | goto unlock; | |
1895 | ||
1896 | ret = 0; | |
1897 | tfile->ifindex = ifindex; | |
1898 | goto unlock; | |
1899 | } | |
1da177e4 | 1900 | |
876bfd4d | 1901 | ret = -EBADFD; |
1da177e4 | 1902 | if (!tun) |
876bfd4d | 1903 | goto unlock; |
1da177e4 | 1904 | |
1e588338 | 1905 | tun_debug(KERN_INFO, tun, "tun_chr_ioctl cmd %u\n", cmd); |
1da177e4 | 1906 | |
631ab46b | 1907 | ret = 0; |
1da177e4 | 1908 | switch (cmd) { |
e3b99556 | 1909 | case TUNGETIFF: |
9ce99cf6 | 1910 | tun_get_iff(current->nsproxy->net_ns, tun, &ifr); |
e3b99556 | 1911 | |
3d407a80 PE |
1912 | if (tfile->detached) |
1913 | ifr.ifr_flags |= IFF_DETACH_QUEUE; | |
849c9b6f PE |
1914 | if (!tfile->socket.sk->sk_filter) |
1915 | ifr.ifr_flags |= IFF_NOFILTER; | |
3d407a80 | 1916 | |
50857e2a | 1917 | if (copy_to_user(argp, &ifr, ifreq_len)) |
631ab46b | 1918 | ret = -EFAULT; |
e3b99556 MM |
1919 | break; |
1920 | ||
1da177e4 LT |
1921 | case TUNSETNOCSUM: |
1922 | /* Disable/Enable checksum */ | |
1da177e4 | 1923 | |
88255375 MM |
1924 | /* [unimplemented] */ |
1925 | tun_debug(KERN_INFO, tun, "ignored: set checksum %s\n", | |
6b8a66ee | 1926 | arg ? "disabled" : "enabled"); |
1da177e4 LT |
1927 | break; |
1928 | ||
1929 | case TUNSETPERSIST: | |
54f968d6 JW |
1930 | /* Disable/Enable persist mode. Keep an extra reference to the |
1931 | * module to prevent the module being unprobed. | |
1932 | */ | |
dd38bd85 | 1933 | if (arg && !(tun->flags & TUN_PERSIST)) { |
1da177e4 | 1934 | tun->flags |= TUN_PERSIST; |
54f968d6 | 1935 | __module_get(THIS_MODULE); |
dd38bd85 JW |
1936 | } |
1937 | if (!arg && (tun->flags & TUN_PERSIST)) { | |
1da177e4 | 1938 | tun->flags &= ~TUN_PERSIST; |
54f968d6 JW |
1939 | module_put(THIS_MODULE); |
1940 | } | |
1da177e4 | 1941 | |
6b8a66ee JP |
1942 | tun_debug(KERN_INFO, tun, "persist %s\n", |
1943 | arg ? "enabled" : "disabled"); | |
1da177e4 LT |
1944 | break; |
1945 | ||
1946 | case TUNSETOWNER: | |
1947 | /* Set owner of the device */ | |
0625c883 EB |
1948 | owner = make_kuid(current_user_ns(), arg); |
1949 | if (!uid_valid(owner)) { | |
1950 | ret = -EINVAL; | |
1951 | break; | |
1952 | } | |
1953 | tun->owner = owner; | |
1e588338 | 1954 | tun_debug(KERN_INFO, tun, "owner set to %u\n", |
0625c883 | 1955 | from_kuid(&init_user_ns, tun->owner)); |
1da177e4 LT |
1956 | break; |
1957 | ||
8c644623 GG |
1958 | case TUNSETGROUP: |
1959 | /* Set group of the device */ | |
0625c883 EB |
1960 | group = make_kgid(current_user_ns(), arg); |
1961 | if (!gid_valid(group)) { | |
1962 | ret = -EINVAL; | |
1963 | break; | |
1964 | } | |
1965 | tun->group = group; | |
1e588338 | 1966 | tun_debug(KERN_INFO, tun, "group set to %u\n", |
0625c883 | 1967 | from_kgid(&init_user_ns, tun->group)); |
8c644623 GG |
1968 | break; |
1969 | ||
ff4cc3ac MK |
1970 | case TUNSETLINK: |
1971 | /* Only allow setting the type when the interface is down */ | |
1972 | if (tun->dev->flags & IFF_UP) { | |
6b8a66ee JP |
1973 | tun_debug(KERN_INFO, tun, |
1974 | "Linktype set failed because interface is up\n"); | |
48abfe05 | 1975 | ret = -EBUSY; |
ff4cc3ac MK |
1976 | } else { |
1977 | tun->dev->type = (int) arg; | |
6b8a66ee JP |
1978 | tun_debug(KERN_INFO, tun, "linktype set to %d\n", |
1979 | tun->dev->type); | |
48abfe05 | 1980 | ret = 0; |
ff4cc3ac | 1981 | } |
631ab46b | 1982 | break; |
ff4cc3ac | 1983 | |
1da177e4 LT |
1984 | #ifdef TUN_DEBUG |
1985 | case TUNSETDEBUG: | |
1986 | tun->debug = arg; | |
1987 | break; | |
1988 | #endif | |
5228ddc9 | 1989 | case TUNSETOFFLOAD: |
88255375 | 1990 | ret = set_offload(tun, arg); |
631ab46b | 1991 | break; |
5228ddc9 | 1992 | |
f271b2cc MK |
1993 | case TUNSETTXFILTER: |
1994 | /* Can be set only for TAPs */ | |
631ab46b | 1995 | ret = -EINVAL; |
f271b2cc | 1996 | if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV) |
631ab46b | 1997 | break; |
c0e5a8c2 | 1998 | ret = update_filter(&tun->txflt, (void __user *)arg); |
631ab46b | 1999 | break; |
1da177e4 LT |
2000 | |
2001 | case SIOCGIFHWADDR: | |
b595076a | 2002 | /* Get hw address */ |
f271b2cc MK |
2003 | memcpy(ifr.ifr_hwaddr.sa_data, tun->dev->dev_addr, ETH_ALEN); |
2004 | ifr.ifr_hwaddr.sa_family = tun->dev->type; | |
50857e2a | 2005 | if (copy_to_user(argp, &ifr, ifreq_len)) |
631ab46b EB |
2006 | ret = -EFAULT; |
2007 | break; | |
1da177e4 LT |
2008 | |
2009 | case SIOCSIFHWADDR: | |
f271b2cc | 2010 | /* Set hw address */ |
6b8a66ee JP |
2011 | tun_debug(KERN_DEBUG, tun, "set hw address: %pM\n", |
2012 | ifr.ifr_hwaddr.sa_data); | |
40102371 | 2013 | |
40102371 | 2014 | ret = dev_set_mac_address(tun->dev, &ifr.ifr_hwaddr); |
631ab46b | 2015 | break; |
33dccbb0 HX |
2016 | |
2017 | case TUNGETSNDBUF: | |
54f968d6 | 2018 | sndbuf = tfile->socket.sk->sk_sndbuf; |
33dccbb0 HX |
2019 | if (copy_to_user(argp, &sndbuf, sizeof(sndbuf))) |
2020 | ret = -EFAULT; | |
2021 | break; | |
2022 | ||
2023 | case TUNSETSNDBUF: | |
2024 | if (copy_from_user(&sndbuf, argp, sizeof(sndbuf))) { | |
2025 | ret = -EFAULT; | |
2026 | break; | |
2027 | } | |
2028 | ||
c8d68e6b JW |
2029 | tun->sndbuf = sndbuf; |
2030 | tun_set_sndbuf(tun); | |
33dccbb0 HX |
2031 | break; |
2032 | ||
d9d52b51 MT |
2033 | case TUNGETVNETHDRSZ: |
2034 | vnet_hdr_sz = tun->vnet_hdr_sz; | |
2035 | if (copy_to_user(argp, &vnet_hdr_sz, sizeof(vnet_hdr_sz))) | |
2036 | ret = -EFAULT; | |
2037 | break; | |
2038 | ||
2039 | case TUNSETVNETHDRSZ: | |
2040 | if (copy_from_user(&vnet_hdr_sz, argp, sizeof(vnet_hdr_sz))) { | |
2041 | ret = -EFAULT; | |
2042 | break; | |
2043 | } | |
2044 | if (vnet_hdr_sz < (int)sizeof(struct virtio_net_hdr)) { | |
2045 | ret = -EINVAL; | |
2046 | break; | |
2047 | } | |
2048 | ||
2049 | tun->vnet_hdr_sz = vnet_hdr_sz; | |
2050 | break; | |
2051 | ||
99405162 MT |
2052 | case TUNATTACHFILTER: |
2053 | /* Can be set only for TAPs */ | |
2054 | ret = -EINVAL; | |
2055 | if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV) | |
2056 | break; | |
2057 | ret = -EFAULT; | |
54f968d6 | 2058 | if (copy_from_user(&tun->fprog, argp, sizeof(tun->fprog))) |
99405162 MT |
2059 | break; |
2060 | ||
c8d68e6b | 2061 | ret = tun_attach_filter(tun); |
99405162 MT |
2062 | break; |
2063 | ||
2064 | case TUNDETACHFILTER: | |
2065 | /* Can be set only for TAPs */ | |
2066 | ret = -EINVAL; | |
2067 | if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV) | |
2068 | break; | |
c8d68e6b JW |
2069 | ret = 0; |
2070 | tun_detach_filter(tun, tun->numqueues); | |
99405162 MT |
2071 | break; |
2072 | ||
76975e9c PE |
2073 | case TUNGETFILTER: |
2074 | ret = -EINVAL; | |
2075 | if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV) | |
2076 | break; | |
2077 | ret = -EFAULT; | |
2078 | if (copy_to_user(argp, &tun->fprog, sizeof(tun->fprog))) | |
2079 | break; | |
2080 | ret = 0; | |
2081 | break; | |
2082 | ||
1da177e4 | 2083 | default: |
631ab46b EB |
2084 | ret = -EINVAL; |
2085 | break; | |
ee289b64 | 2086 | } |
1da177e4 | 2087 | |
876bfd4d HX |
2088 | unlock: |
2089 | rtnl_unlock(); | |
2090 | if (tun) | |
2091 | tun_put(tun); | |
631ab46b | 2092 | return ret; |
1da177e4 LT |
2093 | } |
2094 | ||
50857e2a AB |
2095 | static long tun_chr_ioctl(struct file *file, |
2096 | unsigned int cmd, unsigned long arg) | |
2097 | { | |
2098 | return __tun_chr_ioctl(file, cmd, arg, sizeof (struct ifreq)); | |
2099 | } | |
2100 | ||
2101 | #ifdef CONFIG_COMPAT | |
2102 | static long tun_chr_compat_ioctl(struct file *file, | |
2103 | unsigned int cmd, unsigned long arg) | |
2104 | { | |
2105 | switch (cmd) { | |
2106 | case TUNSETIFF: | |
2107 | case TUNGETIFF: | |
2108 | case TUNSETTXFILTER: | |
2109 | case TUNGETSNDBUF: | |
2110 | case TUNSETSNDBUF: | |
2111 | case SIOCGIFHWADDR: | |
2112 | case SIOCSIFHWADDR: | |
2113 | arg = (unsigned long)compat_ptr(arg); | |
2114 | break; | |
2115 | default: | |
2116 | arg = (compat_ulong_t)arg; | |
2117 | break; | |
2118 | } | |
2119 | ||
2120 | /* | |
2121 | * compat_ifreq is shorter than ifreq, so we must not access beyond | |
2122 | * the end of that structure. All fields that are used in this | |
2123 | * driver are compatible though, we don't need to convert the | |
2124 | * contents. | |
2125 | */ | |
2126 | return __tun_chr_ioctl(file, cmd, arg, sizeof(struct compat_ifreq)); | |
2127 | } | |
2128 | #endif /* CONFIG_COMPAT */ | |
2129 | ||
1da177e4 LT |
2130 | static int tun_chr_fasync(int fd, struct file *file, int on) |
2131 | { | |
54f968d6 | 2132 | struct tun_file *tfile = file->private_data; |
1da177e4 LT |
2133 | int ret; |
2134 | ||
54f968d6 | 2135 | if ((ret = fasync_helper(fd, file, on, &tfile->fasync)) < 0) |
9d319522 | 2136 | goto out; |
6aa20a22 | 2137 | |
1da177e4 | 2138 | if (on) { |
609d7fa9 | 2139 | ret = __f_setown(file, task_pid(current), PIDTYPE_PID, 0); |
1da177e4 | 2140 | if (ret) |
9d319522 | 2141 | goto out; |
54f968d6 | 2142 | tfile->flags |= TUN_FASYNC; |
6aa20a22 | 2143 | } else |
54f968d6 | 2144 | tfile->flags &= ~TUN_FASYNC; |
9d319522 JC |
2145 | ret = 0; |
2146 | out: | |
9d319522 | 2147 | return ret; |
1da177e4 LT |
2148 | } |
2149 | ||
2150 | static int tun_chr_open(struct inode *inode, struct file * file) | |
2151 | { | |
631ab46b | 2152 | struct tun_file *tfile; |
deed49fb | 2153 | |
6b8a66ee | 2154 | DBG1(KERN_INFO, "tunX: tun_chr_open\n"); |
631ab46b | 2155 | |
54f968d6 JW |
2156 | tfile = (struct tun_file *)sk_alloc(&init_net, AF_UNSPEC, GFP_KERNEL, |
2157 | &tun_proto); | |
631ab46b EB |
2158 | if (!tfile) |
2159 | return -ENOMEM; | |
6e914fc7 | 2160 | rcu_assign_pointer(tfile->tun, NULL); |
36b50bab | 2161 | tfile->net = get_net(current->nsproxy->net_ns); |
54f968d6 | 2162 | tfile->flags = 0; |
fb7589a1 | 2163 | tfile->ifindex = 0; |
54f968d6 JW |
2164 | |
2165 | rcu_assign_pointer(tfile->socket.wq, &tfile->wq); | |
2166 | init_waitqueue_head(&tfile->wq.wait); | |
2167 | ||
2168 | tfile->socket.file = file; | |
2169 | tfile->socket.ops = &tun_socket_ops; | |
2170 | ||
2171 | sock_init_data(&tfile->socket, &tfile->sk); | |
2172 | sk_change_net(&tfile->sk, tfile->net); | |
2173 | ||
2174 | tfile->sk.sk_write_space = tun_sock_write_space; | |
2175 | tfile->sk.sk_sndbuf = INT_MAX; | |
2176 | ||
631ab46b | 2177 | file->private_data = tfile; |
54f968d6 | 2178 | set_bit(SOCK_EXTERNALLY_ALLOCATED, &tfile->socket.flags); |
4008e97f | 2179 | INIT_LIST_HEAD(&tfile->next); |
54f968d6 | 2180 | |
19a6afb2 JW |
2181 | sock_set_flag(&tfile->sk, SOCK_ZEROCOPY); |
2182 | ||
1da177e4 LT |
2183 | return 0; |
2184 | } | |
2185 | ||
2186 | static int tun_chr_close(struct inode *inode, struct file *file) | |
2187 | { | |
631ab46b | 2188 | struct tun_file *tfile = file->private_data; |
54f968d6 | 2189 | struct net *net = tfile->net; |
1da177e4 | 2190 | |
c8d68e6b | 2191 | tun_detach(tfile, true); |
54f968d6 | 2192 | put_net(net); |
1da177e4 LT |
2193 | |
2194 | return 0; | |
2195 | } | |
2196 | ||
d54b1fdb | 2197 | static const struct file_operations tun_fops = { |
6aa20a22 | 2198 | .owner = THIS_MODULE, |
1da177e4 | 2199 | .llseek = no_llseek, |
ee0b3e67 BP |
2200 | .read = do_sync_read, |
2201 | .aio_read = tun_chr_aio_read, | |
2202 | .write = do_sync_write, | |
2203 | .aio_write = tun_chr_aio_write, | |
1da177e4 | 2204 | .poll = tun_chr_poll, |
50857e2a AB |
2205 | .unlocked_ioctl = tun_chr_ioctl, |
2206 | #ifdef CONFIG_COMPAT | |
2207 | .compat_ioctl = tun_chr_compat_ioctl, | |
2208 | #endif | |
1da177e4 LT |
2209 | .open = tun_chr_open, |
2210 | .release = tun_chr_close, | |
6aa20a22 | 2211 | .fasync = tun_chr_fasync |
1da177e4 LT |
2212 | }; |
2213 | ||
2214 | static struct miscdevice tun_miscdev = { | |
2215 | .minor = TUN_MINOR, | |
2216 | .name = "tun", | |
e454cea2 | 2217 | .nodename = "net/tun", |
1da177e4 | 2218 | .fops = &tun_fops, |
1da177e4 LT |
2219 | }; |
2220 | ||
2221 | /* ethtool interface */ | |
2222 | ||
2223 | static int tun_get_settings(struct net_device *dev, struct ethtool_cmd *cmd) | |
2224 | { | |
2225 | cmd->supported = 0; | |
2226 | cmd->advertising = 0; | |
70739497 | 2227 | ethtool_cmd_speed_set(cmd, SPEED_10); |
1da177e4 LT |
2228 | cmd->duplex = DUPLEX_FULL; |
2229 | cmd->port = PORT_TP; | |
2230 | cmd->phy_address = 0; | |
2231 | cmd->transceiver = XCVR_INTERNAL; | |
2232 | cmd->autoneg = AUTONEG_DISABLE; | |
2233 | cmd->maxtxpkt = 0; | |
2234 | cmd->maxrxpkt = 0; | |
2235 | return 0; | |
2236 | } | |
2237 | ||
2238 | static void tun_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info) | |
2239 | { | |
2240 | struct tun_struct *tun = netdev_priv(dev); | |
2241 | ||
33a5ba14 RJ |
2242 | strlcpy(info->driver, DRV_NAME, sizeof(info->driver)); |
2243 | strlcpy(info->version, DRV_VERSION, sizeof(info->version)); | |
1da177e4 LT |
2244 | |
2245 | switch (tun->flags & TUN_TYPE_MASK) { | |
2246 | case TUN_TUN_DEV: | |
33a5ba14 | 2247 | strlcpy(info->bus_info, "tun", sizeof(info->bus_info)); |
1da177e4 LT |
2248 | break; |
2249 | case TUN_TAP_DEV: | |
33a5ba14 | 2250 | strlcpy(info->bus_info, "tap", sizeof(info->bus_info)); |
1da177e4 LT |
2251 | break; |
2252 | } | |
2253 | } | |
2254 | ||
2255 | static u32 tun_get_msglevel(struct net_device *dev) | |
2256 | { | |
2257 | #ifdef TUN_DEBUG | |
2258 | struct tun_struct *tun = netdev_priv(dev); | |
2259 | return tun->debug; | |
2260 | #else | |
2261 | return -EOPNOTSUPP; | |
2262 | #endif | |
2263 | } | |
2264 | ||
2265 | static void tun_set_msglevel(struct net_device *dev, u32 value) | |
2266 | { | |
2267 | #ifdef TUN_DEBUG | |
2268 | struct tun_struct *tun = netdev_priv(dev); | |
2269 | tun->debug = value; | |
2270 | #endif | |
2271 | } | |
2272 | ||
7282d491 | 2273 | static const struct ethtool_ops tun_ethtool_ops = { |
1da177e4 LT |
2274 | .get_settings = tun_get_settings, |
2275 | .get_drvinfo = tun_get_drvinfo, | |
2276 | .get_msglevel = tun_get_msglevel, | |
2277 | .set_msglevel = tun_set_msglevel, | |
bee31369 | 2278 | .get_link = ethtool_op_get_link, |
eda29772 | 2279 | .get_ts_info = ethtool_op_get_ts_info, |
1da177e4 LT |
2280 | }; |
2281 | ||
79d17604 | 2282 | |
1da177e4 LT |
2283 | static int __init tun_init(void) |
2284 | { | |
2285 | int ret = 0; | |
2286 | ||
6b8a66ee JP |
2287 | pr_info("%s, %s\n", DRV_DESCRIPTION, DRV_VERSION); |
2288 | pr_info("%s\n", DRV_COPYRIGHT); | |
1da177e4 | 2289 | |
f019a7a5 | 2290 | ret = rtnl_link_register(&tun_link_ops); |
79d17604 | 2291 | if (ret) { |
6b8a66ee | 2292 | pr_err("Can't register link_ops\n"); |
f019a7a5 | 2293 | goto err_linkops; |
79d17604 PE |
2294 | } |
2295 | ||
1da177e4 | 2296 | ret = misc_register(&tun_miscdev); |
79d17604 | 2297 | if (ret) { |
6b8a66ee | 2298 | pr_err("Can't register misc device %d\n", TUN_MINOR); |
79d17604 PE |
2299 | goto err_misc; |
2300 | } | |
f019a7a5 | 2301 | return 0; |
79d17604 | 2302 | err_misc: |
f019a7a5 EB |
2303 | rtnl_link_unregister(&tun_link_ops); |
2304 | err_linkops: | |
1da177e4 LT |
2305 | return ret; |
2306 | } | |
2307 | ||
2308 | static void tun_cleanup(void) | |
2309 | { | |
6aa20a22 | 2310 | misc_deregister(&tun_miscdev); |
f019a7a5 | 2311 | rtnl_link_unregister(&tun_link_ops); |
1da177e4 LT |
2312 | } |
2313 | ||
05c2828c MT |
2314 | /* Get an underlying socket object from tun file. Returns error unless file is |
2315 | * attached to a device. The returned object works like a packet socket, it | |
2316 | * can be used for sock_sendmsg/sock_recvmsg. The caller is responsible for | |
2317 | * holding a reference to the file for as long as the socket is in use. */ | |
2318 | struct socket *tun_get_socket(struct file *file) | |
2319 | { | |
6e914fc7 | 2320 | struct tun_file *tfile; |
05c2828c MT |
2321 | if (file->f_op != &tun_fops) |
2322 | return ERR_PTR(-EINVAL); | |
6e914fc7 JW |
2323 | tfile = file->private_data; |
2324 | if (!tfile) | |
05c2828c | 2325 | return ERR_PTR(-EBADFD); |
54f968d6 | 2326 | return &tfile->socket; |
05c2828c MT |
2327 | } |
2328 | EXPORT_SYMBOL_GPL(tun_get_socket); | |
2329 | ||
1da177e4 LT |
2330 | module_init(tun_init); |
2331 | module_exit(tun_cleanup); | |
2332 | MODULE_DESCRIPTION(DRV_DESCRIPTION); | |
2333 | MODULE_AUTHOR(DRV_COPYRIGHT); | |
2334 | MODULE_LICENSE("GPL"); | |
2335 | MODULE_ALIAS_MISCDEV(TUN_MINOR); | |
578454ff | 2336 | MODULE_ALIAS("devname:net/tun"); |