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