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