]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/net/tun.c
tun: enable NAPI for TUN/TAP driver
[mirror_ubuntu-bionic-kernel.git] / drivers / net / tun.c
CommitLineData
1da177e4
LT
1/*
2 * TUN - Universal TUN/TAP device driver.
3 * Copyright (C) 1999-2002 Maxim Krasnyansky <maxk@qualcomm.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * $Id: tun.c,v 1.15 2002/03/01 02:44:24 maxk Exp $
16 */
17
18/*
19 * Changes:
20 *
ff4cc3ac
MK
21 * Mike Kershaw <dragorn@kismetwireless.net> 2005/08/14
22 * Add TUNSETLINK ioctl to set the link encapsulation
23 *
1da177e4 24 * Mark Smith <markzzzsmith@yahoo.com.au>
344dc8ed 25 * Use eth_random_addr() for tap MAC address.
1da177e4
LT
26 *
27 * Harald Roelle <harald.roelle@ifi.lmu.de> 2004/04/20
28 * Fixes in packet dropping, queue length setting and queue wakeup.
29 * Increased default tx queue length.
30 * Added ethtool API.
31 * Minor cleanups
32 *
33 * Daniel Podlejski <underley@underley.eu.org>
34 * Modifications for 2.3.99-pre5 kernel.
35 */
36
6b8a66ee
JP
37#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
38
1da177e4
LT
39#define DRV_NAME "tun"
40#define DRV_VERSION "1.6"
41#define DRV_DESCRIPTION "Universal TUN/TAP device driver"
42#define DRV_COPYRIGHT "(C) 1999-2004 Max Krasnyansky <maxk@qualcomm.com>"
43
1da177e4
LT
44#include <linux/module.h>
45#include <linux/errno.h>
46#include <linux/kernel.h>
174cd4b1 47#include <linux/sched/signal.h>
1da177e4
LT
48#include <linux/major.h>
49#include <linux/slab.h>
50#include <linux/poll.h>
51#include <linux/fcntl.h>
52#include <linux/init.h>
53#include <linux/skbuff.h>
54#include <linux/netdevice.h>
55#include <linux/etherdevice.h>
56#include <linux/miscdevice.h>
57#include <linux/ethtool.h>
58#include <linux/rtnetlink.h>
50857e2a 59#include <linux/compat.h>
1da177e4
LT
60#include <linux/if.h>
61#include <linux/if_arp.h>
62#include <linux/if_ether.h>
63#include <linux/if_tun.h>
6680ec68 64#include <linux/if_vlan.h>
1da177e4 65#include <linux/crc32.h>
d647a591 66#include <linux/nsproxy.h>
f43798c2 67#include <linux/virtio_net.h>
99405162 68#include <linux/rcupdate.h>
881d966b 69#include <net/net_namespace.h>
79d17604 70#include <net/netns/generic.h>
f019a7a5 71#include <net/rtnetlink.h>
33dccbb0 72#include <net/sock.h>
93e14b6d 73#include <linux/seq_file.h>
e0b46d0e 74#include <linux/uio.h>
1576d986 75#include <linux/skb_array.h>
761876c8
JW
76#include <linux/bpf.h>
77#include <linux/bpf_trace.h>
1da177e4 78
7c0f6ba6 79#include <linux/uaccess.h>
1da177e4 80
14daa021
RR
81/* Uncomment to enable debugging */
82/* #define TUN_DEBUG 1 */
83
1da177e4
LT
84#ifdef TUN_DEBUG
85static int debug;
14daa021 86
6b8a66ee
JP
87#define tun_debug(level, tun, fmt, args...) \
88do { \
89 if (tun->debug) \
90 netdev_printk(level, tun->dev, fmt, ##args); \
91} while (0)
92#define DBG1(level, fmt, args...) \
93do { \
94 if (debug == 2) \
95 printk(level fmt, ##args); \
96} while (0)
14daa021 97#else
6b8a66ee
JP
98#define tun_debug(level, tun, fmt, args...) \
99do { \
100 if (0) \
101 netdev_printk(level, tun->dev, fmt, ##args); \
102} while (0)
103#define DBG1(level, fmt, args...) \
104do { \
105 if (0) \
106 printk(level fmt, ##args); \
107} while (0)
14daa021
RR
108#endif
109
761876c8 110#define TUN_HEADROOM 256
7df13219 111#define TUN_RX_PAD (NET_IP_ALIGN + NET_SKB_PAD)
66ccbc9c 112
031f5e03
MT
113/* TUN device flags */
114
115/* IFF_ATTACH_QUEUE is never stored in device flags,
116 * overload it to mean fasync when stored there.
117 */
118#define TUN_FASYNC IFF_ATTACH_QUEUE
1cf8e410
MT
119/* High bits in flags field are unused. */
120#define TUN_VNET_LE 0x80000000
8b8e658b 121#define TUN_VNET_BE 0x40000000
031f5e03
MT
122
123#define TUN_FEATURES (IFF_NO_PI | IFF_ONE_QUEUE | IFF_VNET_HDR | \
94317099 124 IFF_MULTI_QUEUE | IFF_NAPI)
0690899b
MT
125#define GOODCOPY_LEN 128
126
f271b2cc
MK
127#define FLT_EXACT_COUNT 8
128struct tap_filter {
129 unsigned int count; /* Number of addrs. Zero means disabled */
130 u32 mask[2]; /* Mask of the hashed addrs */
131 unsigned char addr[FLT_EXACT_COUNT][ETH_ALEN];
132};
133
baf71c5c
PG
134/* MAX_TAP_QUEUES 256 is chosen to allow rx/tx queues to be equal
135 * to max number of VCPUs in guest. */
136#define MAX_TAP_QUEUES 256
b8732fb7 137#define MAX_TAP_FLOWS 4096
c8d68e6b 138
96442e42
JW
139#define TUN_FLOW_EXPIRE (3 * HZ)
140
608b9977
PA
141struct tun_pcpu_stats {
142 u64 rx_packets;
143 u64 rx_bytes;
144 u64 tx_packets;
145 u64 tx_bytes;
146 struct u64_stats_sync syncp;
147 u32 rx_dropped;
148 u32 tx_dropped;
149 u32 rx_frame_errors;
150};
151
54f968d6 152/* A tun_file connects an open character device to a tuntap netdevice. It
92d4ea6e 153 * also contains all socket related structures (except sock_fprog and tap_filter)
54f968d6
JW
154 * to serve as one transmit queue for tuntap device. The sock_fprog and
155 * tap_filter were kept in tun_struct since they were used for filtering for the
36fe8c09 156 * netdevice not for a specific queue (at least I didn't see the requirement for
54f968d6 157 * this).
6e914fc7
JW
158 *
159 * RCU usage:
36fe8c09 160 * The tun_file and tun_struct are loosely coupled, the pointer from one to the
6e914fc7 161 * other can only be read while rcu_read_lock or rtnl_lock is held.
54f968d6 162 */
631ab46b 163struct tun_file {
54f968d6
JW
164 struct sock sk;
165 struct socket socket;
166 struct socket_wq wq;
6e914fc7 167 struct tun_struct __rcu *tun;
54f968d6
JW
168 struct fasync_struct *fasync;
169 /* only used for fasnyc */
170 unsigned int flags;
fb7589a1
PE
171 union {
172 u16 queue_index;
173 unsigned int ifindex;
174 };
94317099 175 struct napi_struct napi;
4008e97f
JW
176 struct list_head next;
177 struct tun_struct *detached;
1576d986 178 struct skb_array tx_array;
631ab46b
EB
179};
180
96442e42
JW
181struct tun_flow_entry {
182 struct hlist_node hash_link;
183 struct rcu_head rcu;
184 struct tun_struct *tun;
185
186 u32 rxhash;
9bc88939 187 u32 rps_rxhash;
96442e42
JW
188 int queue_index;
189 unsigned long updated;
190};
191
192#define TUN_NUM_FLOW_ENTRIES 1024
193
54f968d6 194/* Since the socket were moved to tun_file, to preserve the behavior of persist
36fe8c09 195 * device, socket filter, sndbuf and vnet header size were restore when the
54f968d6
JW
196 * file were attached to a persist device.
197 */
14daa021 198struct tun_struct {
c8d68e6b
JW
199 struct tun_file __rcu *tfiles[MAX_TAP_QUEUES];
200 unsigned int numqueues;
f271b2cc 201 unsigned int flags;
0625c883
EB
202 kuid_t owner;
203 kgid_t group;
14daa021 204
14daa021 205 struct net_device *dev;
c8f44aff 206 netdev_features_t set_features;
88255375 207#define TUN_USER_FEATURES (NETIF_F_HW_CSUM|NETIF_F_TSO_ECN|NETIF_F_TSO| \
d591a1f3 208 NETIF_F_TSO6)
d9d52b51 209
eaea34b2 210 int align;
d9d52b51 211 int vnet_hdr_sz;
54f968d6
JW
212 int sndbuf;
213 struct tap_filter txflt;
214 struct sock_fprog fprog;
215 /* protected by rtnl lock */
216 bool filter_attached;
14daa021
RR
217#ifdef TUN_DEBUG
218 int debug;
1da177e4 219#endif
96442e42 220 spinlock_t lock;
96442e42
JW
221 struct hlist_head flows[TUN_NUM_FLOW_ENTRIES];
222 struct timer_list flow_gc_timer;
223 unsigned long ageing_time;
4008e97f
JW
224 unsigned int numdisabled;
225 struct list_head disabled;
5dbbaf2d 226 void *security;
b8732fb7 227 u32 flow_count;
5503fcec 228 u32 rx_batched;
608b9977 229 struct tun_pcpu_stats __percpu *pcpu_stats;
761876c8 230 struct bpf_prog __rcu *xdp_prog;
14daa021 231};
1da177e4 232
94317099
PP
233static int tun_napi_receive(struct napi_struct *napi, int budget)
234{
235 struct tun_file *tfile = container_of(napi, struct tun_file, napi);
236 struct sk_buff_head *queue = &tfile->sk.sk_write_queue;
237 struct sk_buff_head process_queue;
238 struct sk_buff *skb;
239 int received = 0;
240
241 __skb_queue_head_init(&process_queue);
242
243 spin_lock(&queue->lock);
244 skb_queue_splice_tail_init(queue, &process_queue);
245 spin_unlock(&queue->lock);
246
247 while (received < budget && (skb = __skb_dequeue(&process_queue))) {
248 napi_gro_receive(napi, skb);
249 ++received;
250 }
251
252 if (!skb_queue_empty(&process_queue)) {
253 spin_lock(&queue->lock);
254 skb_queue_splice(&process_queue, queue);
255 spin_unlock(&queue->lock);
256 }
257
258 return received;
259}
260
261static int tun_napi_poll(struct napi_struct *napi, int budget)
262{
263 unsigned int received;
264
265 received = tun_napi_receive(napi, budget);
266
267 if (received < budget)
268 napi_complete_done(napi, received);
269
270 return received;
271}
272
273static void tun_napi_init(struct tun_struct *tun, struct tun_file *tfile,
274 bool napi_en)
275{
276 if (napi_en) {
277 netif_napi_add(tun->dev, &tfile->napi, tun_napi_poll,
278 NAPI_POLL_WEIGHT);
279 napi_enable(&tfile->napi);
280 }
281}
282
283static void tun_napi_disable(struct tun_struct *tun, struct tun_file *tfile)
284{
285 if (tun->flags & IFF_NAPI)
286 napi_disable(&tfile->napi);
287}
288
289static void tun_napi_del(struct tun_struct *tun, struct tun_file *tfile)
290{
291 if (tun->flags & IFF_NAPI)
292 netif_napi_del(&tfile->napi);
293}
294
8b8e658b
GK
295#ifdef CONFIG_TUN_VNET_CROSS_LE
296static inline bool tun_legacy_is_little_endian(struct tun_struct *tun)
297{
298 return tun->flags & TUN_VNET_BE ? false :
299 virtio_legacy_is_little_endian();
300}
301
302static long tun_get_vnet_be(struct tun_struct *tun, int __user *argp)
303{
304 int be = !!(tun->flags & TUN_VNET_BE);
305
306 if (put_user(be, argp))
307 return -EFAULT;
308
309 return 0;
310}
311
312static long tun_set_vnet_be(struct tun_struct *tun, int __user *argp)
313{
314 int be;
315
316 if (get_user(be, argp))
317 return -EFAULT;
318
319 if (be)
320 tun->flags |= TUN_VNET_BE;
321 else
322 tun->flags &= ~TUN_VNET_BE;
323
324 return 0;
325}
326#else
327static inline bool tun_legacy_is_little_endian(struct tun_struct *tun)
328{
329 return virtio_legacy_is_little_endian();
330}
331
332static long tun_get_vnet_be(struct tun_struct *tun, int __user *argp)
333{
334 return -EINVAL;
335}
336
337static long tun_set_vnet_be(struct tun_struct *tun, int __user *argp)
338{
339 return -EINVAL;
340}
341#endif /* CONFIG_TUN_VNET_CROSS_LE */
342
25bd55bb
GK
343static inline bool tun_is_little_endian(struct tun_struct *tun)
344{
7d824109 345 return tun->flags & TUN_VNET_LE ||
8b8e658b 346 tun_legacy_is_little_endian(tun);
25bd55bb
GK
347}
348
56f0dcc5
MT
349static inline u16 tun16_to_cpu(struct tun_struct *tun, __virtio16 val)
350{
25bd55bb 351 return __virtio16_to_cpu(tun_is_little_endian(tun), val);
56f0dcc5
MT
352}
353
354static inline __virtio16 cpu_to_tun16(struct tun_struct *tun, u16 val)
355{
25bd55bb 356 return __cpu_to_virtio16(tun_is_little_endian(tun), val);
56f0dcc5
MT
357}
358
96442e42
JW
359static inline u32 tun_hashfn(u32 rxhash)
360{
361 return rxhash & 0x3ff;
362}
363
364static struct tun_flow_entry *tun_flow_find(struct hlist_head *head, u32 rxhash)
365{
366 struct tun_flow_entry *e;
96442e42 367
b67bfe0d 368 hlist_for_each_entry_rcu(e, head, hash_link) {
96442e42
JW
369 if (e->rxhash == rxhash)
370 return e;
371 }
372 return NULL;
373}
374
375static struct tun_flow_entry *tun_flow_create(struct tun_struct *tun,
376 struct hlist_head *head,
377 u32 rxhash, u16 queue_index)
378{
9fdc6bef
ED
379 struct tun_flow_entry *e = kmalloc(sizeof(*e), GFP_ATOMIC);
380
96442e42
JW
381 if (e) {
382 tun_debug(KERN_INFO, tun, "create flow: hash %u index %u\n",
383 rxhash, queue_index);
384 e->updated = jiffies;
385 e->rxhash = rxhash;
9bc88939 386 e->rps_rxhash = 0;
96442e42
JW
387 e->queue_index = queue_index;
388 e->tun = tun;
389 hlist_add_head_rcu(&e->hash_link, head);
b8732fb7 390 ++tun->flow_count;
96442e42
JW
391 }
392 return e;
393}
394
96442e42
JW
395static void tun_flow_delete(struct tun_struct *tun, struct tun_flow_entry *e)
396{
397 tun_debug(KERN_INFO, tun, "delete flow: hash %u index %u\n",
398 e->rxhash, e->queue_index);
399 hlist_del_rcu(&e->hash_link);
9fdc6bef 400 kfree_rcu(e, rcu);
b8732fb7 401 --tun->flow_count;
96442e42
JW
402}
403
404static void tun_flow_flush(struct tun_struct *tun)
405{
406 int i;
407
408 spin_lock_bh(&tun->lock);
409 for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) {
410 struct tun_flow_entry *e;
b67bfe0d 411 struct hlist_node *n;
96442e42 412
b67bfe0d 413 hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link)
96442e42
JW
414 tun_flow_delete(tun, e);
415 }
416 spin_unlock_bh(&tun->lock);
417}
418
419static void tun_flow_delete_by_queue(struct tun_struct *tun, u16 queue_index)
420{
421 int i;
422
423 spin_lock_bh(&tun->lock);
424 for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) {
425 struct tun_flow_entry *e;
b67bfe0d 426 struct hlist_node *n;
96442e42 427
b67bfe0d 428 hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link) {
96442e42
JW
429 if (e->queue_index == queue_index)
430 tun_flow_delete(tun, e);
431 }
432 }
433 spin_unlock_bh(&tun->lock);
434}
435
436static void tun_flow_cleanup(unsigned long data)
437{
438 struct tun_struct *tun = (struct tun_struct *)data;
439 unsigned long delay = tun->ageing_time;
440 unsigned long next_timer = jiffies + delay;
441 unsigned long count = 0;
442 int i;
443
444 tun_debug(KERN_INFO, tun, "tun_flow_cleanup\n");
445
446 spin_lock_bh(&tun->lock);
447 for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) {
448 struct tun_flow_entry *e;
b67bfe0d 449 struct hlist_node *n;
96442e42 450
b67bfe0d 451 hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link) {
96442e42
JW
452 unsigned long this_timer;
453 count++;
454 this_timer = e->updated + delay;
455 if (time_before_eq(this_timer, jiffies))
456 tun_flow_delete(tun, e);
457 else if (time_before(this_timer, next_timer))
458 next_timer = this_timer;
459 }
460 }
461
462 if (count)
463 mod_timer(&tun->flow_gc_timer, round_jiffies_up(next_timer));
464 spin_unlock_bh(&tun->lock);
465}
466
49974420 467static void tun_flow_update(struct tun_struct *tun, u32 rxhash,
9e85722d 468 struct tun_file *tfile)
96442e42
JW
469{
470 struct hlist_head *head;
471 struct tun_flow_entry *e;
472 unsigned long delay = tun->ageing_time;
9e85722d 473 u16 queue_index = tfile->queue_index;
96442e42
JW
474
475 if (!rxhash)
476 return;
477 else
478 head = &tun->flows[tun_hashfn(rxhash)];
479
480 rcu_read_lock();
481
9e85722d
JW
482 /* We may get a very small possibility of OOO during switching, not
483 * worth to optimize.*/
484 if (tun->numqueues == 1 || tfile->detached)
96442e42
JW
485 goto unlock;
486
487 e = tun_flow_find(head, rxhash);
488 if (likely(e)) {
489 /* TODO: keep queueing to old queue until it's empty? */
490 e->queue_index = queue_index;
491 e->updated = jiffies;
9bc88939 492 sock_rps_record_flow_hash(e->rps_rxhash);
96442e42
JW
493 } else {
494 spin_lock_bh(&tun->lock);
b8732fb7
JW
495 if (!tun_flow_find(head, rxhash) &&
496 tun->flow_count < MAX_TAP_FLOWS)
96442e42
JW
497 tun_flow_create(tun, head, rxhash, queue_index);
498
499 if (!timer_pending(&tun->flow_gc_timer))
500 mod_timer(&tun->flow_gc_timer,
501 round_jiffies_up(jiffies + delay));
502 spin_unlock_bh(&tun->lock);
503 }
504
505unlock:
506 rcu_read_unlock();
507}
508
9bc88939
TH
509/**
510 * Save the hash received in the stack receive path and update the
511 * flow_hash table accordingly.
512 */
513static inline void tun_flow_save_rps_rxhash(struct tun_flow_entry *e, u32 hash)
514{
567e4b79 515 if (unlikely(e->rps_rxhash != hash))
9bc88939 516 e->rps_rxhash = hash;
9bc88939
TH
517}
518
c8d68e6b 519/* We try to identify a flow through its rxhash first. The reason that
92d4ea6e 520 * we do not check rxq no. is because some cards(e.g 82599), chooses
c8d68e6b
JW
521 * the rxq based on the txq where the last packet of the flow comes. As
522 * the userspace application move between processors, we may get a
523 * different rxq no. here. If we could not get rxhash, then we would
524 * hope the rxq no. may help here.
525 */
f663dd9a 526static u16 tun_select_queue(struct net_device *dev, struct sk_buff *skb,
99932d4f 527 void *accel_priv, select_queue_fallback_t fallback)
c8d68e6b
JW
528{
529 struct tun_struct *tun = netdev_priv(dev);
96442e42 530 struct tun_flow_entry *e;
c8d68e6b
JW
531 u32 txq = 0;
532 u32 numqueues = 0;
533
534 rcu_read_lock();
92bb73ea 535 numqueues = ACCESS_ONCE(tun->numqueues);
c8d68e6b 536
feec084a 537 txq = __skb_get_hash_symmetric(skb);
c8d68e6b 538 if (txq) {
96442e42 539 e = tun_flow_find(&tun->flows[tun_hashfn(txq)], txq);
9bc88939 540 if (e) {
9bc88939 541 tun_flow_save_rps_rxhash(e, txq);
fbe4d456 542 txq = e->queue_index;
9bc88939 543 } else
96442e42
JW
544 /* use multiply and shift instead of expensive divide */
545 txq = ((u64)txq * numqueues) >> 32;
c8d68e6b
JW
546 } else if (likely(skb_rx_queue_recorded(skb))) {
547 txq = skb_get_rx_queue(skb);
548 while (unlikely(txq >= numqueues))
549 txq -= numqueues;
550 }
551
552 rcu_read_unlock();
553 return txq;
554}
555
cde8b15f
JW
556static inline bool tun_not_capable(struct tun_struct *tun)
557{
558 const struct cred *cred = current_cred();
c260b772 559 struct net *net = dev_net(tun->dev);
cde8b15f
JW
560
561 return ((uid_valid(tun->owner) && !uid_eq(cred->euid, tun->owner)) ||
562 (gid_valid(tun->group) && !in_egroup_p(tun->group))) &&
c260b772 563 !ns_capable(net->user_ns, CAP_NET_ADMIN);
cde8b15f
JW
564}
565
c8d68e6b
JW
566static void tun_set_real_num_queues(struct tun_struct *tun)
567{
568 netif_set_real_num_tx_queues(tun->dev, tun->numqueues);
569 netif_set_real_num_rx_queues(tun->dev, tun->numqueues);
570}
571
4008e97f
JW
572static void tun_disable_queue(struct tun_struct *tun, struct tun_file *tfile)
573{
574 tfile->detached = tun;
575 list_add_tail(&tfile->next, &tun->disabled);
576 ++tun->numdisabled;
577}
578
d32649d1 579static struct tun_struct *tun_enable_queue(struct tun_file *tfile)
4008e97f
JW
580{
581 struct tun_struct *tun = tfile->detached;
582
583 tfile->detached = NULL;
584 list_del_init(&tfile->next);
585 --tun->numdisabled;
586 return tun;
587}
588
4bfb0513
JW
589static void tun_queue_purge(struct tun_file *tfile)
590{
1576d986
JW
591 struct sk_buff *skb;
592
593 while ((skb = skb_array_consume(&tfile->tx_array)) != NULL)
594 kfree_skb(skb);
595
5503fcec 596 skb_queue_purge(&tfile->sk.sk_write_queue);
4bfb0513
JW
597 skb_queue_purge(&tfile->sk.sk_error_queue);
598}
599
c8d68e6b
JW
600static void __tun_detach(struct tun_file *tfile, bool clean)
601{
602 struct tun_file *ntfile;
603 struct tun_struct *tun;
c8d68e6b 604
b8deabd3
JW
605 tun = rtnl_dereference(tfile->tun);
606
94317099
PP
607 if (tun && clean) {
608 tun_napi_disable(tun, tfile);
609 tun_napi_del(tun, tfile);
610 }
611
9e85722d 612 if (tun && !tfile->detached) {
c8d68e6b
JW
613 u16 index = tfile->queue_index;
614 BUG_ON(index >= tun->numqueues);
c8d68e6b
JW
615
616 rcu_assign_pointer(tun->tfiles[index],
617 tun->tfiles[tun->numqueues - 1]);
b8deabd3 618 ntfile = rtnl_dereference(tun->tfiles[index]);
c8d68e6b
JW
619 ntfile->queue_index = index;
620
621 --tun->numqueues;
9e85722d 622 if (clean) {
c956674b 623 RCU_INIT_POINTER(tfile->tun, NULL);
4008e97f 624 sock_put(&tfile->sk);
9e85722d 625 } else
4008e97f 626 tun_disable_queue(tun, tfile);
c8d68e6b
JW
627
628 synchronize_net();
96442e42 629 tun_flow_delete_by_queue(tun, tun->numqueues + 1);
c8d68e6b 630 /* Drop read queue */
4bfb0513 631 tun_queue_purge(tfile);
c8d68e6b 632 tun_set_real_num_queues(tun);
dd38bd85 633 } else if (tfile->detached && clean) {
4008e97f 634 tun = tun_enable_queue(tfile);
dd38bd85
JW
635 sock_put(&tfile->sk);
636 }
c8d68e6b
JW
637
638 if (clean) {
af668b3c
MT
639 if (tun && tun->numqueues == 0 && tun->numdisabled == 0) {
640 netif_carrier_off(tun->dev);
641
40630b82 642 if (!(tun->flags & IFF_PERSIST) &&
af668b3c 643 tun->dev->reg_state == NETREG_REGISTERED)
4008e97f 644 unregister_netdevice(tun->dev);
af668b3c 645 }
1576d986
JW
646 if (tun)
647 skb_array_cleanup(&tfile->tx_array);
140e807d 648 sock_put(&tfile->sk);
c8d68e6b
JW
649 }
650}
651
652static void tun_detach(struct tun_file *tfile, bool clean)
653{
654 rtnl_lock();
655 __tun_detach(tfile, clean);
656 rtnl_unlock();
657}
658
659static void tun_detach_all(struct net_device *dev)
660{
661 struct tun_struct *tun = netdev_priv(dev);
761876c8 662 struct bpf_prog *xdp_prog = rtnl_dereference(tun->xdp_prog);
4008e97f 663 struct tun_file *tfile, *tmp;
c8d68e6b
JW
664 int i, n = tun->numqueues;
665
666 for (i = 0; i < n; i++) {
b8deabd3 667 tfile = rtnl_dereference(tun->tfiles[i]);
c8d68e6b 668 BUG_ON(!tfile);
94317099 669 tun_napi_disable(tun, tfile);
addf8fc4 670 tfile->socket.sk->sk_shutdown = RCV_SHUTDOWN;
9e641bdc 671 tfile->socket.sk->sk_data_ready(tfile->socket.sk);
c956674b 672 RCU_INIT_POINTER(tfile->tun, NULL);
c8d68e6b
JW
673 --tun->numqueues;
674 }
9e85722d 675 list_for_each_entry(tfile, &tun->disabled, next) {
addf8fc4 676 tfile->socket.sk->sk_shutdown = RCV_SHUTDOWN;
9e641bdc 677 tfile->socket.sk->sk_data_ready(tfile->socket.sk);
c956674b 678 RCU_INIT_POINTER(tfile->tun, NULL);
9e85722d 679 }
c8d68e6b
JW
680 BUG_ON(tun->numqueues != 0);
681
682 synchronize_net();
683 for (i = 0; i < n; i++) {
b8deabd3 684 tfile = rtnl_dereference(tun->tfiles[i]);
94317099 685 tun_napi_del(tun, tfile);
c8d68e6b 686 /* Drop read queue */
4bfb0513 687 tun_queue_purge(tfile);
c8d68e6b
JW
688 sock_put(&tfile->sk);
689 }
4008e97f
JW
690 list_for_each_entry_safe(tfile, tmp, &tun->disabled, next) {
691 tun_enable_queue(tfile);
4bfb0513 692 tun_queue_purge(tfile);
4008e97f
JW
693 sock_put(&tfile->sk);
694 }
695 BUG_ON(tun->numdisabled != 0);
dd38bd85 696
761876c8
JW
697 if (xdp_prog)
698 bpf_prog_put(xdp_prog);
699
40630b82 700 if (tun->flags & IFF_PERSIST)
dd38bd85 701 module_put(THIS_MODULE);
c8d68e6b
JW
702}
703
94317099
PP
704static int tun_attach(struct tun_struct *tun, struct file *file,
705 bool skip_filter, bool napi)
a7385ba2 706{
631ab46b 707 struct tun_file *tfile = file->private_data;
1576d986 708 struct net_device *dev = tun->dev;
38231b7a 709 int err;
a7385ba2 710
5dbbaf2d
PM
711 err = security_tun_dev_attach(tfile->socket.sk, tun->security);
712 if (err < 0)
713 goto out;
714
38231b7a 715 err = -EINVAL;
9e85722d 716 if (rtnl_dereference(tfile->tun) && !tfile->detached)
38231b7a
EB
717 goto out;
718
719 err = -EBUSY;
40630b82 720 if (!(tun->flags & IFF_MULTI_QUEUE) && tun->numqueues == 1)
c8d68e6b
JW
721 goto out;
722
723 err = -E2BIG;
4008e97f
JW
724 if (!tfile->detached &&
725 tun->numqueues + tun->numdisabled == MAX_TAP_QUEUES)
38231b7a
EB
726 goto out;
727
728 err = 0;
54f968d6 729
92d4ea6e 730 /* Re-attach the filter to persist device */
849c9b6f 731 if (!skip_filter && (tun->filter_attached == true)) {
8ced425e
HFS
732 lock_sock(tfile->socket.sk);
733 err = sk_attach_filter(&tun->fprog, tfile->socket.sk);
734 release_sock(tfile->socket.sk);
54f968d6
JW
735 if (!err)
736 goto out;
737 }
1576d986
JW
738
739 if (!tfile->detached &&
740 skb_array_init(&tfile->tx_array, dev->tx_queue_len, GFP_KERNEL)) {
741 err = -ENOMEM;
742 goto out;
743 }
744
c8d68e6b 745 tfile->queue_index = tun->numqueues;
addf8fc4 746 tfile->socket.sk->sk_shutdown &= ~RCV_SHUTDOWN;
6e914fc7 747 rcu_assign_pointer(tfile->tun, tun);
c8d68e6b 748 rcu_assign_pointer(tun->tfiles[tun->numqueues], tfile);
c8d68e6b 749 tun->numqueues++;
a7385ba2 750
94317099 751 if (tfile->detached) {
4008e97f 752 tun_enable_queue(tfile);
94317099 753 } else {
4008e97f 754 sock_hold(&tfile->sk);
94317099
PP
755 tun_napi_init(tun, tfile, napi);
756 }
4008e97f 757
c8d68e6b 758 tun_set_real_num_queues(tun);
a7385ba2 759
c8d68e6b
JW
760 /* device is allowed to go away first, so no need to hold extra
761 * refcnt.
762 */
763
764out:
765 return err;
631ab46b
EB
766}
767
768static struct tun_struct *__tun_get(struct tun_file *tfile)
769{
6e914fc7 770 struct tun_struct *tun;
c70f1829 771
6e914fc7
JW
772 rcu_read_lock();
773 tun = rcu_dereference(tfile->tun);
774 if (tun)
775 dev_hold(tun->dev);
776 rcu_read_unlock();
c70f1829
EB
777
778 return tun;
631ab46b
EB
779}
780
781static struct tun_struct *tun_get(struct file *file)
782{
783 return __tun_get(file->private_data);
784}
785
786static void tun_put(struct tun_struct *tun)
787{
6e914fc7 788 dev_put(tun->dev);
631ab46b
EB
789}
790
6b8a66ee 791/* TAP filtering */
f271b2cc
MK
792static void addr_hash_set(u32 *mask, const u8 *addr)
793{
794 int n = ether_crc(ETH_ALEN, addr) >> 26;
795 mask[n >> 5] |= (1 << (n & 31));
796}
797
798static unsigned int addr_hash_test(const u32 *mask, const u8 *addr)
799{
800 int n = ether_crc(ETH_ALEN, addr) >> 26;
801 return mask[n >> 5] & (1 << (n & 31));
802}
803
804static int update_filter(struct tap_filter *filter, void __user *arg)
805{
806 struct { u8 u[ETH_ALEN]; } *addr;
807 struct tun_filter uf;
808 int err, alen, n, nexact;
809
810 if (copy_from_user(&uf, arg, sizeof(uf)))
811 return -EFAULT;
812
813 if (!uf.count) {
814 /* Disabled */
815 filter->count = 0;
816 return 0;
817 }
818
819 alen = ETH_ALEN * uf.count;
28e8190d
ME
820 addr = memdup_user(arg + sizeof(uf), alen);
821 if (IS_ERR(addr))
822 return PTR_ERR(addr);
f271b2cc
MK
823
824 /* The filter is updated without holding any locks. Which is
825 * perfectly safe. We disable it first and in the worst
826 * case we'll accept a few undesired packets. */
827 filter->count = 0;
828 wmb();
829
830 /* Use first set of addresses as an exact filter */
831 for (n = 0; n < uf.count && n < FLT_EXACT_COUNT; n++)
832 memcpy(filter->addr[n], addr[n].u, ETH_ALEN);
833
834 nexact = n;
835
cfbf84fc
AW
836 /* Remaining multicast addresses are hashed,
837 * unicast will leave the filter disabled. */
f271b2cc 838 memset(filter->mask, 0, sizeof(filter->mask));
cfbf84fc
AW
839 for (; n < uf.count; n++) {
840 if (!is_multicast_ether_addr(addr[n].u)) {
841 err = 0; /* no filter */
3b8d2a69 842 goto free_addr;
cfbf84fc 843 }
f271b2cc 844 addr_hash_set(filter->mask, addr[n].u);
cfbf84fc 845 }
f271b2cc
MK
846
847 /* For ALLMULTI just set the mask to all ones.
848 * This overrides the mask populated above. */
849 if ((uf.flags & TUN_FLT_ALLMULTI))
850 memset(filter->mask, ~0, sizeof(filter->mask));
851
852 /* Now enable the filter */
853 wmb();
854 filter->count = nexact;
855
856 /* Return the number of exact filters */
857 err = nexact;
3b8d2a69 858free_addr:
f271b2cc
MK
859 kfree(addr);
860 return err;
861}
862
863/* Returns: 0 - drop, !=0 - accept */
864static int run_filter(struct tap_filter *filter, const struct sk_buff *skb)
865{
866 /* Cannot use eth_hdr(skb) here because skb_mac_hdr() is incorrect
867 * at this point. */
868 struct ethhdr *eh = (struct ethhdr *) skb->data;
869 int i;
870
871 /* Exact match */
872 for (i = 0; i < filter->count; i++)
2e42e474 873 if (ether_addr_equal(eh->h_dest, filter->addr[i]))
f271b2cc
MK
874 return 1;
875
876 /* Inexact match (multicast only) */
877 if (is_multicast_ether_addr(eh->h_dest))
878 return addr_hash_test(filter->mask, eh->h_dest);
879
880 return 0;
881}
882
883/*
884 * Checks whether the packet is accepted or not.
885 * Returns: 0 - drop, !=0 - accept
886 */
887static int check_filter(struct tap_filter *filter, const struct sk_buff *skb)
888{
889 if (!filter->count)
890 return 1;
891
892 return run_filter(filter, skb);
893}
894
1da177e4
LT
895/* Network device part of the driver */
896
7282d491 897static const struct ethtool_ops tun_ethtool_ops;
1da177e4 898
c70f1829
EB
899/* Net device detach from fd. */
900static void tun_net_uninit(struct net_device *dev)
901{
c8d68e6b 902 tun_detach_all(dev);
c70f1829
EB
903}
904
1da177e4
LT
905/* Net device open. */
906static int tun_net_open(struct net_device *dev)
907{
b20e2d54
HFS
908 struct tun_struct *tun = netdev_priv(dev);
909 int i;
910
c8d68e6b 911 netif_tx_start_all_queues(dev);
b20e2d54
HFS
912
913 for (i = 0; i < tun->numqueues; i++) {
914 struct tun_file *tfile;
915
916 tfile = rtnl_dereference(tun->tfiles[i]);
917 tfile->socket.sk->sk_write_space(tfile->socket.sk);
918 }
919
1da177e4
LT
920 return 0;
921}
922
923/* Net device close. */
924static int tun_net_close(struct net_device *dev)
925{
c8d68e6b 926 netif_tx_stop_all_queues(dev);
1da177e4
LT
927 return 0;
928}
929
930/* Net device start xmit */
424efe9c 931static netdev_tx_t tun_net_xmit(struct sk_buff *skb, struct net_device *dev)
1da177e4
LT
932{
933 struct tun_struct *tun = netdev_priv(dev);
c8d68e6b 934 int txq = skb->queue_mapping;
6e914fc7 935 struct tun_file *tfile;
fa35864e 936 u32 numqueues = 0;
1da177e4 937
6e914fc7 938 rcu_read_lock();
c8d68e6b 939 tfile = rcu_dereference(tun->tfiles[txq]);
fa35864e 940 numqueues = ACCESS_ONCE(tun->numqueues);
c8d68e6b 941
1da177e4 942 /* Drop packet if interface is not attached */
fa35864e 943 if (txq >= numqueues)
1da177e4
LT
944 goto drop;
945
3df97ba8
JW
946#ifdef CONFIG_RPS
947 if (numqueues == 1 && static_key_false(&rps_needed)) {
9bc88939
TH
948 /* Select queue was not called for the skbuff, so we extract the
949 * RPS hash and save it into the flow_table here.
950 */
951 __u32 rxhash;
952
feec084a 953 rxhash = __skb_get_hash_symmetric(skb);
9bc88939
TH
954 if (rxhash) {
955 struct tun_flow_entry *e;
956 e = tun_flow_find(&tun->flows[tun_hashfn(rxhash)],
957 rxhash);
958 if (e)
959 tun_flow_save_rps_rxhash(e, rxhash);
960 }
961 }
3df97ba8 962#endif
9bc88939 963
6e914fc7
JW
964 tun_debug(KERN_INFO, tun, "tun_net_xmit %d\n", skb->len);
965
c8d68e6b
JW
966 BUG_ON(!tfile);
967
f271b2cc
MK
968 /* Drop if the filter does not like it.
969 * This is a noop if the filter is disabled.
970 * Filter can be enabled only for the TAP devices. */
971 if (!check_filter(&tun->txflt, skb))
972 goto drop;
973
54f968d6
JW
974 if (tfile->socket.sk->sk_filter &&
975 sk_filter(tfile->socket.sk, skb))
99405162
MT
976 goto drop;
977
1f8b977a 978 if (unlikely(skb_orphan_frags_rx(skb, GFP_ATOMIC)))
7bf66305
JW
979 goto drop;
980
7b996243 981 skb_tx_timestamp(skb);
eda29772 982
0110d6f2 983 /* Orphan the skb - required as we might hang on to it
7bf66305
JW
984 * for indefinite time.
985 */
0110d6f2
MT
986 skb_orphan(skb);
987
f8af75f3
ED
988 nf_reset(skb);
989
1576d986
JW
990 if (skb_array_produce(&tfile->tx_array, skb))
991 goto drop;
1da177e4
LT
992
993 /* Notify and wake up reader process */
54f968d6
JW
994 if (tfile->flags & TUN_FASYNC)
995 kill_fasync(&tfile->fasync, SIGIO, POLL_IN);
9e641bdc 996 tfile->socket.sk->sk_data_ready(tfile->socket.sk);
6e914fc7
JW
997
998 rcu_read_unlock();
6ed10654 999 return NETDEV_TX_OK;
1da177e4
LT
1000
1001drop:
608b9977 1002 this_cpu_inc(tun->pcpu_stats->tx_dropped);
149d36f7 1003 skb_tx_error(skb);
1da177e4 1004 kfree_skb(skb);
6e914fc7 1005 rcu_read_unlock();
baeababb 1006 return NET_XMIT_DROP;
1da177e4
LT
1007}
1008
f271b2cc 1009static void tun_net_mclist(struct net_device *dev)
1da177e4 1010{
f271b2cc
MK
1011 /*
1012 * This callback is supposed to deal with mc filter in
1013 * _rx_ path and has nothing to do with the _tx_ path.
1014 * In rx path we always accept everything userspace gives us.
1015 */
1da177e4
LT
1016}
1017
c8f44aff
MM
1018static netdev_features_t tun_net_fix_features(struct net_device *dev,
1019 netdev_features_t features)
88255375
MM
1020{
1021 struct tun_struct *tun = netdev_priv(dev);
1022
1023 return (features & tun->set_features) | (features & ~TUN_USER_FEATURES);
1024}
bebd097a
NH
1025#ifdef CONFIG_NET_POLL_CONTROLLER
1026static void tun_poll_controller(struct net_device *dev)
1027{
1028 /*
1029 * Tun only receives frames when:
1030 * 1) the char device endpoint gets data from user space
1031 * 2) the tun socket gets a sendmsg call from user space
94317099
PP
1032 * If NAPI is not enabled, since both of those are synchronous
1033 * operations, we are guaranteed never to have pending data when we poll
1034 * for it so there is nothing to do here but return.
bebd097a
NH
1035 * We need this though so netpoll recognizes us as an interface that
1036 * supports polling, which enables bridge devices in virt setups to
1037 * still use netconsole
94317099
PP
1038 * If NAPI is enabled, however, we need to schedule polling for all
1039 * queues.
bebd097a 1040 */
94317099
PP
1041 struct tun_struct *tun = netdev_priv(dev);
1042
1043 if (tun->flags & IFF_NAPI) {
1044 struct tun_file *tfile;
1045 int i;
1046
1047 rcu_read_lock();
1048 for (i = 0; i < tun->numqueues; i++) {
1049 tfile = rcu_dereference(tun->tfiles[i]);
1050 napi_schedule(&tfile->napi);
1051 }
1052 rcu_read_unlock();
1053 }
bebd097a
NH
1054 return;
1055}
1056#endif
eaea34b2
PA
1057
1058static void tun_set_headroom(struct net_device *dev, int new_hr)
1059{
1060 struct tun_struct *tun = netdev_priv(dev);
1061
1062 if (new_hr < NET_SKB_PAD)
1063 new_hr = NET_SKB_PAD;
1064
1065 tun->align = new_hr;
1066}
1067
bc1f4470 1068static void
608b9977
PA
1069tun_net_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
1070{
1071 u32 rx_dropped = 0, tx_dropped = 0, rx_frame_errors = 0;
1072 struct tun_struct *tun = netdev_priv(dev);
1073 struct tun_pcpu_stats *p;
1074 int i;
1075
1076 for_each_possible_cpu(i) {
1077 u64 rxpackets, rxbytes, txpackets, txbytes;
1078 unsigned int start;
1079
1080 p = per_cpu_ptr(tun->pcpu_stats, i);
1081 do {
1082 start = u64_stats_fetch_begin(&p->syncp);
1083 rxpackets = p->rx_packets;
1084 rxbytes = p->rx_bytes;
1085 txpackets = p->tx_packets;
1086 txbytes = p->tx_bytes;
1087 } while (u64_stats_fetch_retry(&p->syncp, start));
1088
1089 stats->rx_packets += rxpackets;
1090 stats->rx_bytes += rxbytes;
1091 stats->tx_packets += txpackets;
1092 stats->tx_bytes += txbytes;
1093
1094 /* u32 counters */
1095 rx_dropped += p->rx_dropped;
1096 rx_frame_errors += p->rx_frame_errors;
1097 tx_dropped += p->tx_dropped;
1098 }
1099 stats->rx_dropped = rx_dropped;
1100 stats->rx_frame_errors = rx_frame_errors;
1101 stats->tx_dropped = tx_dropped;
608b9977
PA
1102}
1103
761876c8
JW
1104static int tun_xdp_set(struct net_device *dev, struct bpf_prog *prog,
1105 struct netlink_ext_ack *extack)
1106{
1107 struct tun_struct *tun = netdev_priv(dev);
1108 struct bpf_prog *old_prog;
1109
1110 old_prog = rtnl_dereference(tun->xdp_prog);
1111 rcu_assign_pointer(tun->xdp_prog, prog);
1112 if (old_prog)
1113 bpf_prog_put(old_prog);
1114
1115 return 0;
1116}
1117
1118static u32 tun_xdp_query(struct net_device *dev)
1119{
1120 struct tun_struct *tun = netdev_priv(dev);
1121 const struct bpf_prog *xdp_prog;
1122
1123 xdp_prog = rtnl_dereference(tun->xdp_prog);
1124 if (xdp_prog)
1125 return xdp_prog->aux->id;
1126
1127 return 0;
1128}
1129
1130static int tun_xdp(struct net_device *dev, struct netdev_xdp *xdp)
1131{
1132 switch (xdp->command) {
1133 case XDP_SETUP_PROG:
1134 return tun_xdp_set(dev, xdp->prog, xdp->extack);
1135 case XDP_QUERY_PROG:
1136 xdp->prog_id = tun_xdp_query(dev);
1137 xdp->prog_attached = !!xdp->prog_id;
1138 return 0;
1139 default:
1140 return -EINVAL;
1141 }
1142}
1143
758e43b7 1144static const struct net_device_ops tun_netdev_ops = {
c70f1829 1145 .ndo_uninit = tun_net_uninit,
758e43b7
SH
1146 .ndo_open = tun_net_open,
1147 .ndo_stop = tun_net_close,
00829823 1148 .ndo_start_xmit = tun_net_xmit,
88255375 1149 .ndo_fix_features = tun_net_fix_features,
c8d68e6b 1150 .ndo_select_queue = tun_select_queue,
bebd097a
NH
1151#ifdef CONFIG_NET_POLL_CONTROLLER
1152 .ndo_poll_controller = tun_poll_controller,
1153#endif
eaea34b2 1154 .ndo_set_rx_headroom = tun_set_headroom,
608b9977 1155 .ndo_get_stats64 = tun_net_get_stats64,
758e43b7
SH
1156};
1157
1158static const struct net_device_ops tap_netdev_ops = {
c70f1829 1159 .ndo_uninit = tun_net_uninit,
758e43b7
SH
1160 .ndo_open = tun_net_open,
1161 .ndo_stop = tun_net_close,
00829823 1162 .ndo_start_xmit = tun_net_xmit,
88255375 1163 .ndo_fix_features = tun_net_fix_features,
afc4b13d 1164 .ndo_set_rx_mode = tun_net_mclist,
758e43b7
SH
1165 .ndo_set_mac_address = eth_mac_addr,
1166 .ndo_validate_addr = eth_validate_addr,
c8d68e6b 1167 .ndo_select_queue = tun_select_queue,
bebd097a
NH
1168#ifdef CONFIG_NET_POLL_CONTROLLER
1169 .ndo_poll_controller = tun_poll_controller,
1170#endif
5e52796a 1171 .ndo_features_check = passthru_features_check,
eaea34b2 1172 .ndo_set_rx_headroom = tun_set_headroom,
608b9977 1173 .ndo_get_stats64 = tun_net_get_stats64,
761876c8 1174 .ndo_xdp = tun_xdp,
758e43b7
SH
1175};
1176
944a1376 1177static void tun_flow_init(struct tun_struct *tun)
96442e42
JW
1178{
1179 int i;
1180
96442e42
JW
1181 for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++)
1182 INIT_HLIST_HEAD(&tun->flows[i]);
1183
1184 tun->ageing_time = TUN_FLOW_EXPIRE;
1185 setup_timer(&tun->flow_gc_timer, tun_flow_cleanup, (unsigned long)tun);
1186 mod_timer(&tun->flow_gc_timer,
1187 round_jiffies_up(jiffies + tun->ageing_time));
96442e42
JW
1188}
1189
1190static void tun_flow_uninit(struct tun_struct *tun)
1191{
1192 del_timer_sync(&tun->flow_gc_timer);
1193 tun_flow_flush(tun);
96442e42
JW
1194}
1195
91572088
JW
1196#define MIN_MTU 68
1197#define MAX_MTU 65535
1198
1da177e4
LT
1199/* Initialize net device. */
1200static void tun_net_init(struct net_device *dev)
1201{
1202 struct tun_struct *tun = netdev_priv(dev);
6aa20a22 1203
1da177e4 1204 switch (tun->flags & TUN_TYPE_MASK) {
40630b82 1205 case IFF_TUN:
758e43b7
SH
1206 dev->netdev_ops = &tun_netdev_ops;
1207
1da177e4
LT
1208 /* Point-to-Point TUN Device */
1209 dev->hard_header_len = 0;
1210 dev->addr_len = 0;
1211 dev->mtu = 1500;
1212
1213 /* Zero header length */
6aa20a22 1214 dev->type = ARPHRD_NONE;
1da177e4 1215 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
1da177e4
LT
1216 break;
1217
40630b82 1218 case IFF_TAP:
7a0a9608 1219 dev->netdev_ops = &tap_netdev_ops;
1da177e4 1220 /* Ethernet TAP Device */
1da177e4 1221 ether_setup(dev);
550fd08c 1222 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
a676847b 1223 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
36226a8d 1224
f2cedb63 1225 eth_hw_addr_random(dev);
36226a8d 1226
1da177e4
LT
1227 break;
1228 }
91572088
JW
1229
1230 dev->min_mtu = MIN_MTU;
1231 dev->max_mtu = MAX_MTU - dev->hard_header_len;
1da177e4
LT
1232}
1233
1234/* Character device part */
1235
1236/* Poll */
c8d68e6b 1237static unsigned int tun_chr_poll(struct file *file, poll_table *wait)
6aa20a22 1238{
b2430de3
EB
1239 struct tun_file *tfile = file->private_data;
1240 struct tun_struct *tun = __tun_get(tfile);
3c8a9c63 1241 struct sock *sk;
33dccbb0 1242 unsigned int mask = 0;
1da177e4
LT
1243
1244 if (!tun)
eac9e902 1245 return POLLERR;
1da177e4 1246
54f968d6 1247 sk = tfile->socket.sk;
3c8a9c63 1248
6b8a66ee 1249 tun_debug(KERN_INFO, tun, "tun_chr_poll\n");
1da177e4 1250
9e641bdc 1251 poll_wait(file, sk_sleep(sk), wait);
6aa20a22 1252
1576d986 1253 if (!skb_array_empty(&tfile->tx_array))
1da177e4
LT
1254 mask |= POLLIN | POLLRDNORM;
1255
b20e2d54
HFS
1256 if (tun->dev->flags & IFF_UP &&
1257 (sock_writeable(sk) ||
1258 (!test_and_set_bit(SOCKWQ_ASYNC_NOSPACE, &sk->sk_socket->flags) &&
1259 sock_writeable(sk))))
33dccbb0
HX
1260 mask |= POLLOUT | POLLWRNORM;
1261
c70f1829
EB
1262 if (tun->dev->reg_state != NETREG_REGISTERED)
1263 mask = POLLERR;
1264
631ab46b 1265 tun_put(tun);
1da177e4
LT
1266 return mask;
1267}
1268
f42157cb
RR
1269/* prepad is the amount to reserve at front. len is length after that.
1270 * linear is a hint as to how much to copy (usually headers). */
54f968d6 1271static struct sk_buff *tun_alloc_skb(struct tun_file *tfile,
6f7c156c 1272 size_t prepad, size_t len,
1273 size_t linear, int noblock)
f42157cb 1274{
54f968d6 1275 struct sock *sk = tfile->socket.sk;
f42157cb 1276 struct sk_buff *skb;
33dccbb0 1277 int err;
f42157cb
RR
1278
1279 /* Under a page? Don't bother with paged skb. */
0eca93bc 1280 if (prepad + len < PAGE_SIZE || !linear)
33dccbb0 1281 linear = len;
f42157cb 1282
33dccbb0 1283 skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
28d64271 1284 &err, 0);
f42157cb 1285 if (!skb)
33dccbb0 1286 return ERR_PTR(err);
f42157cb
RR
1287
1288 skb_reserve(skb, prepad);
1289 skb_put(skb, linear);
33dccbb0
HX
1290 skb->data_len = len - linear;
1291 skb->len += len - linear;
f42157cb
RR
1292
1293 return skb;
1294}
1295
5503fcec
JW
1296static void tun_rx_batched(struct tun_struct *tun, struct tun_file *tfile,
1297 struct sk_buff *skb, int more)
1298{
1299 struct sk_buff_head *queue = &tfile->sk.sk_write_queue;
1300 struct sk_buff_head process_queue;
1301 u32 rx_batched = tun->rx_batched;
1302 bool rcv = false;
1303
1304 if (!rx_batched || (!more && skb_queue_empty(queue))) {
1305 local_bh_disable();
1306 netif_receive_skb(skb);
1307 local_bh_enable();
1308 return;
1309 }
1310
1311 spin_lock(&queue->lock);
1312 if (!more || skb_queue_len(queue) == rx_batched) {
1313 __skb_queue_head_init(&process_queue);
1314 skb_queue_splice_tail_init(queue, &process_queue);
1315 rcv = true;
1316 } else {
1317 __skb_queue_tail(queue, skb);
1318 }
1319 spin_unlock(&queue->lock);
1320
1321 if (rcv) {
1322 struct sk_buff *nskb;
1323
1324 local_bh_disable();
1325 while ((nskb = __skb_dequeue(&process_queue)))
1326 netif_receive_skb(nskb);
1327 netif_receive_skb(skb);
1328 local_bh_enable();
1329 }
1330}
1331
66ccbc9c
JW
1332static bool tun_can_build_skb(struct tun_struct *tun, struct tun_file *tfile,
1333 int len, int noblock, bool zerocopy)
1334{
1335 if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP)
1336 return false;
1337
1338 if (tfile->socket.sk->sk_sndbuf != INT_MAX)
1339 return false;
1340
1341 if (!noblock)
1342 return false;
1343
1344 if (zerocopy)
1345 return false;
1346
1347 if (SKB_DATA_ALIGN(len + TUN_RX_PAD) +
1348 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)) > PAGE_SIZE)
1349 return false;
1350
1351 return true;
1352}
1353
761876c8
JW
1354static struct sk_buff *tun_build_skb(struct tun_struct *tun,
1355 struct tun_file *tfile,
66ccbc9c 1356 struct iov_iter *from,
761876c8 1357 struct virtio_net_hdr *hdr,
1cfe6e93 1358 int len, int *skb_xdp)
66ccbc9c 1359{
0bbd7dad 1360 struct page_frag *alloc_frag = &current->task_frag;
66ccbc9c 1361 struct sk_buff *skb;
761876c8 1362 struct bpf_prog *xdp_prog;
7df13219 1363 int buflen = SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
761876c8 1364 unsigned int delta = 0;
66ccbc9c
JW
1365 char *buf;
1366 size_t copied;
761876c8 1367 bool xdp_xmit = false;
7df13219
JW
1368 int err, pad = TUN_RX_PAD;
1369
1370 rcu_read_lock();
1371 xdp_prog = rcu_dereference(tun->xdp_prog);
1372 if (xdp_prog)
1373 pad += TUN_HEADROOM;
1374 buflen += SKB_DATA_ALIGN(len + pad);
1375 rcu_read_unlock();
66ccbc9c
JW
1376
1377 if (unlikely(!skb_page_frag_refill(buflen, alloc_frag, GFP_KERNEL)))
1378 return ERR_PTR(-ENOMEM);
1379
1380 buf = (char *)page_address(alloc_frag->page) + alloc_frag->offset;
1381 copied = copy_page_from_iter(alloc_frag->page,
7df13219 1382 alloc_frag->offset + pad,
66ccbc9c
JW
1383 len, from);
1384 if (copied != len)
1385 return ERR_PTR(-EFAULT);
1386
7df13219
JW
1387 /* There's a small window that XDP may be set after the check
1388 * of xdp_prog above, this should be rare and for simplicity
1389 * we do XDP on skb in case the headroom is not enough.
1390 */
1391 if (hdr->gso_type || !xdp_prog)
1cfe6e93 1392 *skb_xdp = 1;
761876c8 1393 else
1cfe6e93 1394 *skb_xdp = 0;
761876c8
JW
1395
1396 rcu_read_lock();
1397 xdp_prog = rcu_dereference(tun->xdp_prog);
1cfe6e93 1398 if (xdp_prog && !*skb_xdp) {
761876c8
JW
1399 struct xdp_buff xdp;
1400 void *orig_data;
1401 u32 act;
1402
1403 xdp.data_hard_start = buf;
7df13219 1404 xdp.data = buf + pad;
761876c8
JW
1405 xdp.data_end = xdp.data + len;
1406 orig_data = xdp.data;
1407 act = bpf_prog_run_xdp(xdp_prog, &xdp);
1408
1409 switch (act) {
1410 case XDP_REDIRECT:
1411 get_page(alloc_frag->page);
1412 alloc_frag->offset += buflen;
1413 err = xdp_do_redirect(tun->dev, &xdp, xdp_prog);
1414 if (err)
1415 goto err_redirect;
1416 return NULL;
1417 case XDP_TX:
1418 xdp_xmit = true;
1419 /* fall through */
1420 case XDP_PASS:
1421 delta = orig_data - xdp.data;
1422 break;
1423 default:
1424 bpf_warn_invalid_xdp_action(act);
1425 /* fall through */
1426 case XDP_ABORTED:
1427 trace_xdp_exception(tun->dev, xdp_prog, act);
1428 /* fall through */
1429 case XDP_DROP:
1430 goto err_xdp;
1431 }
1432 }
1433
66ccbc9c 1434 skb = build_skb(buf, buflen);
761876c8
JW
1435 if (!skb) {
1436 rcu_read_unlock();
66ccbc9c 1437 return ERR_PTR(-ENOMEM);
761876c8 1438 }
66ccbc9c 1439
7df13219 1440 skb_reserve(skb, pad - delta);
761876c8 1441 skb_put(skb, len + delta);
66ccbc9c
JW
1442 get_page(alloc_frag->page);
1443 alloc_frag->offset += buflen;
1444
761876c8
JW
1445 if (xdp_xmit) {
1446 skb->dev = tun->dev;
1447 generic_xdp_tx(skb, xdp_prog);
1448 rcu_read_lock();
1449 return NULL;
1450 }
1451
1452 rcu_read_unlock();
1453
66ccbc9c 1454 return skb;
761876c8
JW
1455
1456err_redirect:
1457 put_page(alloc_frag->page);
1458err_xdp:
1459 rcu_read_unlock();
1460 this_cpu_inc(tun->pcpu_stats->rx_dropped);
1461 return NULL;
66ccbc9c
JW
1462}
1463
1da177e4 1464/* Get packet from user space buffer */
54f968d6 1465static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,
f5ff53b4 1466 void *msg_control, struct iov_iter *from,
5503fcec 1467 int noblock, bool more)
1da177e4 1468{
09640e63 1469 struct tun_pi pi = { 0, cpu_to_be16(ETH_P_IP) };
1da177e4 1470 struct sk_buff *skb;
f5ff53b4 1471 size_t total_len = iov_iter_count(from);
eaea34b2 1472 size_t len = total_len, align = tun->align, linear;
f43798c2 1473 struct virtio_net_hdr gso = { 0 };
608b9977 1474 struct tun_pcpu_stats *stats;
96f8d9ec 1475 int good_linear;
0690899b
MT
1476 int copylen;
1477 bool zerocopy = false;
1478 int err;
49974420 1479 u32 rxhash;
1cfe6e93 1480 int skb_xdp = 1;
1da177e4 1481
1bd4978a
ED
1482 if (!(tun->dev->flags & IFF_UP))
1483 return -EIO;
1484
40630b82 1485 if (!(tun->flags & IFF_NO_PI)) {
15718ea0 1486 if (len < sizeof(pi))
1da177e4 1487 return -EINVAL;
15718ea0 1488 len -= sizeof(pi);
1da177e4 1489
cbbd26b8 1490 if (!copy_from_iter_full(&pi, sizeof(pi), from))
1da177e4
LT
1491 return -EFAULT;
1492 }
1493
40630b82 1494 if (tun->flags & IFF_VNET_HDR) {
e1edab87
WB
1495 int vnet_hdr_sz = READ_ONCE(tun->vnet_hdr_sz);
1496
1497 if (len < vnet_hdr_sz)
f43798c2 1498 return -EINVAL;
e1edab87 1499 len -= vnet_hdr_sz;
f43798c2 1500
cbbd26b8 1501 if (!copy_from_iter_full(&gso, sizeof(gso), from))
f43798c2
RR
1502 return -EFAULT;
1503
4909122f 1504 if ((gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
56f0dcc5
MT
1505 tun16_to_cpu(tun, gso.csum_start) + tun16_to_cpu(tun, gso.csum_offset) + 2 > tun16_to_cpu(tun, gso.hdr_len))
1506 gso.hdr_len = cpu_to_tun16(tun, tun16_to_cpu(tun, gso.csum_start) + tun16_to_cpu(tun, gso.csum_offset) + 2);
4909122f 1507
56f0dcc5 1508 if (tun16_to_cpu(tun, gso.hdr_len) > len)
f43798c2 1509 return -EINVAL;
e1edab87 1510 iov_iter_advance(from, vnet_hdr_sz - sizeof(gso));
f43798c2
RR
1511 }
1512
40630b82 1513 if ((tun->flags & TUN_TYPE_MASK) == IFF_TAP) {
a504b86e 1514 align += NET_IP_ALIGN;
0eca93bc 1515 if (unlikely(len < ETH_HLEN ||
56f0dcc5 1516 (gso.hdr_len && tun16_to_cpu(tun, gso.hdr_len) < ETH_HLEN)))
e01bf1c8
RR
1517 return -EINVAL;
1518 }
6aa20a22 1519
96f8d9ec
JW
1520 good_linear = SKB_MAX_HEAD(align);
1521
88529176 1522 if (msg_control) {
f5ff53b4
AV
1523 struct iov_iter i = *from;
1524
88529176
JW
1525 /* There are 256 bytes to be copied in skb, so there is
1526 * enough room for skb expand head in case it is used.
0690899b
MT
1527 * The rest of the buffer is mapped from userspace.
1528 */
56f0dcc5 1529 copylen = gso.hdr_len ? tun16_to_cpu(tun, gso.hdr_len) : GOODCOPY_LEN;
96f8d9ec
JW
1530 if (copylen > good_linear)
1531 copylen = good_linear;
3dd5c330 1532 linear = copylen;
f5ff53b4
AV
1533 iov_iter_advance(&i, copylen);
1534 if (iov_iter_npages(&i, INT_MAX) <= MAX_SKB_FRAGS)
88529176
JW
1535 zerocopy = true;
1536 }
1537
66ccbc9c 1538 if (tun_can_build_skb(tun, tfile, len, noblock, zerocopy)) {
1cfe6e93
JW
1539 /* For the packet that is not easy to be processed
1540 * (e.g gso or jumbo packet), we will do it at after
1541 * skb was created with generic XDP routine.
1542 */
1543 skb = tun_build_skb(tun, tfile, from, &gso, len, &skb_xdp);
66ccbc9c 1544 if (IS_ERR(skb)) {
608b9977 1545 this_cpu_inc(tun->pcpu_stats->rx_dropped);
66ccbc9c
JW
1546 return PTR_ERR(skb);
1547 }
761876c8
JW
1548 if (!skb)
1549 return total_len;
66ccbc9c
JW
1550 } else {
1551 if (!zerocopy) {
1552 copylen = len;
1553 if (tun16_to_cpu(tun, gso.hdr_len) > good_linear)
1554 linear = good_linear;
1555 else
1556 linear = tun16_to_cpu(tun, gso.hdr_len);
1557 }
1da177e4 1558
66ccbc9c
JW
1559 skb = tun_alloc_skb(tfile, align, copylen, linear, noblock);
1560 if (IS_ERR(skb)) {
1561 if (PTR_ERR(skb) != -EAGAIN)
1562 this_cpu_inc(tun->pcpu_stats->rx_dropped);
1563 return PTR_ERR(skb);
1564 }
0690899b 1565
66ccbc9c
JW
1566 if (zerocopy)
1567 err = zerocopy_sg_from_iter(skb, from);
1568 else
1569 err = skb_copy_datagram_from_iter(skb, 0, from, len);
1570
1571 if (err) {
1572 this_cpu_inc(tun->pcpu_stats->rx_dropped);
1573 kfree_skb(skb);
1574 return -EFAULT;
1575 }
8f22757e 1576 }
1da177e4 1577
3e9e40e7 1578 if (virtio_net_hdr_to_skb(skb, &gso, tun_is_little_endian(tun))) {
df10db98
PA
1579 this_cpu_inc(tun->pcpu_stats->rx_frame_errors);
1580 kfree_skb(skb);
1581 return -EINVAL;
1582 }
1583
1da177e4 1584 switch (tun->flags & TUN_TYPE_MASK) {
40630b82
MT
1585 case IFF_TUN:
1586 if (tun->flags & IFF_NO_PI) {
f09f7ee2
AWC
1587 switch (skb->data[0] & 0xf0) {
1588 case 0x40:
1589 pi.proto = htons(ETH_P_IP);
1590 break;
1591 case 0x60:
1592 pi.proto = htons(ETH_P_IPV6);
1593 break;
1594 default:
608b9977 1595 this_cpu_inc(tun->pcpu_stats->rx_dropped);
f09f7ee2
AWC
1596 kfree_skb(skb);
1597 return -EINVAL;
1598 }
1599 }
1600
459a98ed 1601 skb_reset_mac_header(skb);
1da177e4 1602 skb->protocol = pi.proto;
4c13eb66 1603 skb->dev = tun->dev;
1da177e4 1604 break;
40630b82 1605 case IFF_TAP:
1da177e4
LT
1606 skb->protocol = eth_type_trans(skb, tun->dev);
1607 break;
6403eab1 1608 }
1da177e4 1609
0690899b
MT
1610 /* copy skb_ubuf_info for callback when skb has no error */
1611 if (zerocopy) {
1612 skb_shinfo(skb)->destructor_arg = msg_control;
1613 skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY;
c9af6db4 1614 skb_shinfo(skb)->tx_flags |= SKBTX_SHARED_FRAG;
af1cc7a2
JW
1615 } else if (msg_control) {
1616 struct ubuf_info *uarg = msg_control;
1617 uarg->callback(uarg, false);
0690899b
MT
1618 }
1619
72f65107 1620 skb_reset_network_header(skb);
40893fd0 1621 skb_probe_transport_header(skb, 0);
38502af7 1622
1cfe6e93 1623 if (skb_xdp) {
761876c8
JW
1624 struct bpf_prog *xdp_prog;
1625 int ret;
1626
1627 rcu_read_lock();
1628 xdp_prog = rcu_dereference(tun->xdp_prog);
1629 if (xdp_prog) {
1630 ret = do_xdp_generic(xdp_prog, skb);
1631 if (ret != XDP_PASS) {
1632 rcu_read_unlock();
1633 return total_len;
1634 }
1635 }
1636 rcu_read_unlock();
1637 }
1638
feec084a 1639 rxhash = __skb_get_hash_symmetric(skb);
94317099
PP
1640
1641 if (tun->flags & IFF_NAPI) {
1642 struct sk_buff_head *queue = &tfile->sk.sk_write_queue;
1643 int queue_len;
1644
1645 spin_lock_bh(&queue->lock);
1646 __skb_queue_tail(queue, skb);
1647 queue_len = skb_queue_len(queue);
1648 spin_unlock(&queue->lock);
1649
1650 if (!more || queue_len > NAPI_POLL_WEIGHT)
1651 napi_schedule(&tfile->napi);
1652
1653 local_bh_enable();
1654 } else if (!IS_ENABLED(CONFIG_4KSTACKS)) {
1655 tun_rx_batched(tun, tfile, skb, more);
1656 } else {
1657 netif_rx_ni(skb);
1658 }
6aa20a22 1659
608b9977
PA
1660 stats = get_cpu_ptr(tun->pcpu_stats);
1661 u64_stats_update_begin(&stats->syncp);
1662 stats->rx_packets++;
1663 stats->rx_bytes += len;
1664 u64_stats_update_end(&stats->syncp);
1665 put_cpu_ptr(stats);
1da177e4 1666
9e85722d 1667 tun_flow_update(tun, rxhash, tfile);
0690899b 1668 return total_len;
6aa20a22 1669}
1da177e4 1670
f5ff53b4 1671static ssize_t tun_chr_write_iter(struct kiocb *iocb, struct iov_iter *from)
1da177e4 1672{
33dccbb0 1673 struct file *file = iocb->ki_filp;
ab46d779 1674 struct tun_struct *tun = tun_get(file);
54f968d6 1675 struct tun_file *tfile = file->private_data;
631ab46b 1676 ssize_t result;
1da177e4
LT
1677
1678 if (!tun)
1679 return -EBADFD;
1680
5503fcec
JW
1681 result = tun_get_user(tun, tfile, NULL, from,
1682 file->f_flags & O_NONBLOCK, false);
631ab46b
EB
1683
1684 tun_put(tun);
1685 return result;
1da177e4
LT
1686}
1687
1da177e4 1688/* Put packet to the user space buffer */
6f7c156c 1689static ssize_t tun_put_user(struct tun_struct *tun,
54f968d6 1690 struct tun_file *tfile,
6f7c156c 1691 struct sk_buff *skb,
e0b46d0e 1692 struct iov_iter *iter)
1da177e4
LT
1693{
1694 struct tun_pi pi = { 0, skb->protocol };
608b9977 1695 struct tun_pcpu_stats *stats;
e0b46d0e 1696 ssize_t total;
8c847d25 1697 int vlan_offset = 0;
a8f9bfdf 1698 int vlan_hlen = 0;
2eb783c4 1699 int vnet_hdr_sz = 0;
a8f9bfdf 1700
df8a39de 1701 if (skb_vlan_tag_present(skb))
a8f9bfdf 1702 vlan_hlen = VLAN_HLEN;
1da177e4 1703
40630b82 1704 if (tun->flags & IFF_VNET_HDR)
e1edab87 1705 vnet_hdr_sz = READ_ONCE(tun->vnet_hdr_sz);
1da177e4 1706
e0b46d0e
HX
1707 total = skb->len + vlan_hlen + vnet_hdr_sz;
1708
40630b82 1709 if (!(tun->flags & IFF_NO_PI)) {
e0b46d0e 1710 if (iov_iter_count(iter) < sizeof(pi))
1da177e4
LT
1711 return -EINVAL;
1712
e0b46d0e
HX
1713 total += sizeof(pi);
1714 if (iov_iter_count(iter) < total) {
1da177e4
LT
1715 /* Packet will be striped */
1716 pi.flags |= TUN_PKT_STRIP;
1717 }
6aa20a22 1718
e0b46d0e 1719 if (copy_to_iter(&pi, sizeof(pi), iter) != sizeof(pi))
1da177e4 1720 return -EFAULT;
6aa20a22 1721 }
1da177e4 1722
2eb783c4 1723 if (vnet_hdr_sz) {
9403cd7c 1724 struct virtio_net_hdr gso;
34166093 1725
e0b46d0e 1726 if (iov_iter_count(iter) < vnet_hdr_sz)
f43798c2
RR
1727 return -EINVAL;
1728
3e9e40e7 1729 if (virtio_net_hdr_from_skb(skb, &gso,
6391a448 1730 tun_is_little_endian(tun), true)) {
f43798c2 1731 struct skb_shared_info *sinfo = skb_shinfo(skb);
34166093
MR
1732 pr_err("unexpected GSO type: "
1733 "0x%x, gso_size %d, hdr_len %d\n",
1734 sinfo->gso_type, tun16_to_cpu(tun, gso.gso_size),
1735 tun16_to_cpu(tun, gso.hdr_len));
1736 print_hex_dump(KERN_ERR, "tun: ",
1737 DUMP_PREFIX_NONE,
1738 16, 1, skb->head,
1739 min((int)tun16_to_cpu(tun, gso.hdr_len), 64), true);
1740 WARN_ON_ONCE(1);
1741 return -EINVAL;
1742 }
f43798c2 1743
e0b46d0e 1744 if (copy_to_iter(&gso, sizeof(gso), iter) != sizeof(gso))
f43798c2 1745 return -EFAULT;
8c847d25
JW
1746
1747 iov_iter_advance(iter, vnet_hdr_sz - sizeof(gso));
f43798c2
RR
1748 }
1749
a8f9bfdf 1750 if (vlan_hlen) {
e0b46d0e 1751 int ret;
6680ec68
JW
1752 struct {
1753 __be16 h_vlan_proto;
1754 __be16 h_vlan_TCI;
1755 } veth;
1756
1757 veth.h_vlan_proto = skb->vlan_proto;
df8a39de 1758 veth.h_vlan_TCI = htons(skb_vlan_tag_get(skb));
6680ec68
JW
1759
1760 vlan_offset = offsetof(struct vlan_ethhdr, h_vlan_proto);
6680ec68 1761
e0b46d0e
HX
1762 ret = skb_copy_datagram_iter(skb, 0, iter, vlan_offset);
1763 if (ret || !iov_iter_count(iter))
6680ec68
JW
1764 goto done;
1765
e0b46d0e
HX
1766 ret = copy_to_iter(&veth, sizeof(veth), iter);
1767 if (ret != sizeof(veth) || !iov_iter_count(iter))
6680ec68
JW
1768 goto done;
1769 }
1da177e4 1770
e0b46d0e 1771 skb_copy_datagram_iter(skb, vlan_offset, iter, skb->len - vlan_offset);
1da177e4 1772
6680ec68 1773done:
608b9977
PA
1774 /* caller is in process context, */
1775 stats = get_cpu_ptr(tun->pcpu_stats);
1776 u64_stats_update_begin(&stats->syncp);
1777 stats->tx_packets++;
1778 stats->tx_bytes += skb->len + vlan_hlen;
1779 u64_stats_update_end(&stats->syncp);
1780 put_cpu_ptr(tun->pcpu_stats);
1da177e4
LT
1781
1782 return total;
1783}
1784
1576d986
JW
1785static struct sk_buff *tun_ring_recv(struct tun_file *tfile, int noblock,
1786 int *err)
1787{
1788 DECLARE_WAITQUEUE(wait, current);
1789 struct sk_buff *skb = NULL;
f48cc6b2 1790 int error = 0;
1576d986
JW
1791
1792 skb = skb_array_consume(&tfile->tx_array);
1793 if (skb)
1794 goto out;
1795 if (noblock) {
f48cc6b2 1796 error = -EAGAIN;
1576d986
JW
1797 goto out;
1798 }
1799
1800 add_wait_queue(&tfile->wq.wait, &wait);
1801 current->state = TASK_INTERRUPTIBLE;
1802
1803 while (1) {
1804 skb = skb_array_consume(&tfile->tx_array);
1805 if (skb)
1806 break;
1807 if (signal_pending(current)) {
f48cc6b2 1808 error = -ERESTARTSYS;
1576d986
JW
1809 break;
1810 }
1811 if (tfile->socket.sk->sk_shutdown & RCV_SHUTDOWN) {
f48cc6b2 1812 error = -EFAULT;
1576d986
JW
1813 break;
1814 }
1815
1816 schedule();
1817 }
1818
1819 current->state = TASK_RUNNING;
1820 remove_wait_queue(&tfile->wq.wait, &wait);
1821
1822out:
f48cc6b2 1823 *err = error;
1576d986
JW
1824 return skb;
1825}
1826
54f968d6 1827static ssize_t tun_do_read(struct tun_struct *tun, struct tun_file *tfile,
9b067034 1828 struct iov_iter *to,
ac77cfd4 1829 int noblock, struct sk_buff *skb)
1da177e4 1830{
9b067034 1831 ssize_t ret;
1576d986 1832 int err;
1da177e4 1833
3872baf6 1834 tun_debug(KERN_INFO, tun, "tun_do_read\n");
1da177e4 1835
9b067034
AV
1836 if (!iov_iter_count(to))
1837 return 0;
1da177e4 1838
ac77cfd4
JW
1839 if (!skb) {
1840 /* Read frames from ring */
1841 skb = tun_ring_recv(tfile, noblock, &err);
1842 if (!skb)
1843 return err;
1844 }
e0b46d0e 1845
9b067034 1846 ret = tun_put_user(tun, tfile, skb, to);
f51a5e82 1847 if (unlikely(ret < 0))
f271b2cc 1848 kfree_skb(skb);
f51a5e82
JW
1849 else
1850 consume_skb(skb);
1da177e4 1851
05c2828c
MT
1852 return ret;
1853}
1854
9b067034 1855static ssize_t tun_chr_read_iter(struct kiocb *iocb, struct iov_iter *to)
05c2828c
MT
1856{
1857 struct file *file = iocb->ki_filp;
1858 struct tun_file *tfile = file->private_data;
1859 struct tun_struct *tun = __tun_get(tfile);
9b067034 1860 ssize_t len = iov_iter_count(to), ret;
05c2828c
MT
1861
1862 if (!tun)
1863 return -EBADFD;
ac77cfd4 1864 ret = tun_do_read(tun, tfile, to, file->f_flags & O_NONBLOCK, NULL);
42404c09 1865 ret = min_t(ssize_t, ret, len);
d0b7da8a
ZYW
1866 if (ret > 0)
1867 iocb->ki_pos = ret;
631ab46b 1868 tun_put(tun);
1da177e4
LT
1869 return ret;
1870}
1871
96442e42
JW
1872static void tun_free_netdev(struct net_device *dev)
1873{
1874 struct tun_struct *tun = netdev_priv(dev);
1875
4008e97f 1876 BUG_ON(!(list_empty(&tun->disabled)));
608b9977 1877 free_percpu(tun->pcpu_stats);
96442e42 1878 tun_flow_uninit(tun);
5dbbaf2d 1879 security_tun_dev_free_security(tun->security);
96442e42
JW
1880}
1881
1da177e4
LT
1882static void tun_setup(struct net_device *dev)
1883{
1884 struct tun_struct *tun = netdev_priv(dev);
1885
0625c883
EB
1886 tun->owner = INVALID_UID;
1887 tun->group = INVALID_GID;
1da177e4 1888
1da177e4 1889 dev->ethtool_ops = &tun_ethtool_ops;
cf124db5
DM
1890 dev->needs_free_netdev = true;
1891 dev->priv_destructor = tun_free_netdev;
016adb72
JW
1892 /* We prefer our own queue length */
1893 dev->tx_queue_len = TUN_READQ_SIZE;
1da177e4
LT
1894}
1895
f019a7a5
EB
1896/* Trivial set of netlink ops to allow deleting tun or tap
1897 * device with netlink.
1898 */
a8b8a889
MS
1899static int tun_validate(struct nlattr *tb[], struct nlattr *data[],
1900 struct netlink_ext_ack *extack)
f019a7a5
EB
1901{
1902 return -EINVAL;
1903}
1904
1905static struct rtnl_link_ops tun_link_ops __read_mostly = {
1906 .kind = DRV_NAME,
1907 .priv_size = sizeof(struct tun_struct),
1908 .setup = tun_setup,
1909 .validate = tun_validate,
1910};
1911
33dccbb0
HX
1912static void tun_sock_write_space(struct sock *sk)
1913{
54f968d6 1914 struct tun_file *tfile;
43815482 1915 wait_queue_head_t *wqueue;
33dccbb0
HX
1916
1917 if (!sock_writeable(sk))
1918 return;
1919
9cd3e072 1920 if (!test_and_clear_bit(SOCKWQ_ASYNC_NOSPACE, &sk->sk_socket->flags))
33dccbb0
HX
1921 return;
1922
43815482
ED
1923 wqueue = sk_sleep(sk);
1924 if (wqueue && waitqueue_active(wqueue))
1925 wake_up_interruptible_sync_poll(wqueue, POLLOUT |
05c2828c 1926 POLLWRNORM | POLLWRBAND);
c722c625 1927
54f968d6
JW
1928 tfile = container_of(sk, struct tun_file, sk);
1929 kill_fasync(&tfile->fasync, SIGIO, POLL_OUT);
33dccbb0
HX
1930}
1931
1b784140 1932static int tun_sendmsg(struct socket *sock, struct msghdr *m, size_t total_len)
05c2828c 1933{
54f968d6
JW
1934 int ret;
1935 struct tun_file *tfile = container_of(sock, struct tun_file, socket);
1936 struct tun_struct *tun = __tun_get(tfile);
1937
1938 if (!tun)
1939 return -EBADFD;
f5ff53b4 1940
c0371da6 1941 ret = tun_get_user(tun, tfile, m->msg_control, &m->msg_iter,
5503fcec
JW
1942 m->msg_flags & MSG_DONTWAIT,
1943 m->msg_flags & MSG_MORE);
54f968d6
JW
1944 tun_put(tun);
1945 return ret;
05c2828c
MT
1946}
1947
1b784140 1948static int tun_recvmsg(struct socket *sock, struct msghdr *m, size_t total_len,
05c2828c
MT
1949 int flags)
1950{
54f968d6
JW
1951 struct tun_file *tfile = container_of(sock, struct tun_file, socket);
1952 struct tun_struct *tun = __tun_get(tfile);
05c2828c 1953 int ret;
54f968d6
JW
1954
1955 if (!tun)
1956 return -EBADFD;
1957
eda29772 1958 if (flags & ~(MSG_DONTWAIT|MSG_TRUNC|MSG_ERRQUEUE)) {
3811ae76
G
1959 ret = -EINVAL;
1960 goto out;
1961 }
eda29772
RC
1962 if (flags & MSG_ERRQUEUE) {
1963 ret = sock_recv_errqueue(sock->sk, m, total_len,
1964 SOL_PACKET, TUN_TX_TIMESTAMP);
1965 goto out;
1966 }
ac77cfd4
JW
1967 ret = tun_do_read(tun, tfile, &m->msg_iter, flags & MSG_DONTWAIT,
1968 m->msg_control);
87897931 1969 if (ret > (ssize_t)total_len) {
42404c09
DM
1970 m->msg_flags |= MSG_TRUNC;
1971 ret = flags & MSG_TRUNC ? ret : total_len;
1972 }
3811ae76 1973out:
54f968d6 1974 tun_put(tun);
05c2828c
MT
1975 return ret;
1976}
1977
1576d986
JW
1978static int tun_peek_len(struct socket *sock)
1979{
1980 struct tun_file *tfile = container_of(sock, struct tun_file, socket);
1981 struct tun_struct *tun;
1982 int ret = 0;
1983
1984 tun = __tun_get(tfile);
1985 if (!tun)
1986 return 0;
1987
1988 ret = skb_array_peek_len(&tfile->tx_array);
1989 tun_put(tun);
1990
1991 return ret;
1992}
1993
05c2828c
MT
1994/* Ops structure to mimic raw sockets with tun */
1995static const struct proto_ops tun_socket_ops = {
1576d986 1996 .peek_len = tun_peek_len,
05c2828c
MT
1997 .sendmsg = tun_sendmsg,
1998 .recvmsg = tun_recvmsg,
1999};
2000
33dccbb0
HX
2001static struct proto tun_proto = {
2002 .name = "tun",
2003 .owner = THIS_MODULE,
54f968d6 2004 .obj_size = sizeof(struct tun_file),
33dccbb0 2005};
f019a7a5 2006
980c9e8c
DW
2007static int tun_flags(struct tun_struct *tun)
2008{
031f5e03 2009 return tun->flags & (TUN_FEATURES | IFF_PERSIST | IFF_TUN | IFF_TAP);
980c9e8c
DW
2010}
2011
2012static ssize_t tun_show_flags(struct device *dev, struct device_attribute *attr,
2013 char *buf)
2014{
2015 struct tun_struct *tun = netdev_priv(to_net_dev(dev));
2016 return sprintf(buf, "0x%x\n", tun_flags(tun));
2017}
2018
2019static ssize_t tun_show_owner(struct device *dev, struct device_attribute *attr,
2020 char *buf)
2021{
2022 struct tun_struct *tun = netdev_priv(to_net_dev(dev));
0625c883
EB
2023 return uid_valid(tun->owner)?
2024 sprintf(buf, "%u\n",
2025 from_kuid_munged(current_user_ns(), tun->owner)):
2026 sprintf(buf, "-1\n");
980c9e8c
DW
2027}
2028
2029static ssize_t tun_show_group(struct device *dev, struct device_attribute *attr,
2030 char *buf)
2031{
2032 struct tun_struct *tun = netdev_priv(to_net_dev(dev));
0625c883
EB
2033 return gid_valid(tun->group) ?
2034 sprintf(buf, "%u\n",
2035 from_kgid_munged(current_user_ns(), tun->group)):
2036 sprintf(buf, "-1\n");
980c9e8c
DW
2037}
2038
2039static DEVICE_ATTR(tun_flags, 0444, tun_show_flags, NULL);
2040static DEVICE_ATTR(owner, 0444, tun_show_owner, NULL);
2041static DEVICE_ATTR(group, 0444, tun_show_group, NULL);
2042
c4d33e24
TI
2043static struct attribute *tun_dev_attrs[] = {
2044 &dev_attr_tun_flags.attr,
2045 &dev_attr_owner.attr,
2046 &dev_attr_group.attr,
2047 NULL
2048};
2049
2050static const struct attribute_group tun_attr_group = {
2051 .attrs = tun_dev_attrs
2052};
2053
d647a591 2054static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
1da177e4
LT
2055{
2056 struct tun_struct *tun;
54f968d6 2057 struct tun_file *tfile = file->private_data;
1da177e4
LT
2058 struct net_device *dev;
2059 int err;
2060
7c0c3b1a
JW
2061 if (tfile->detached)
2062 return -EINVAL;
2063
74a3e5a7
EB
2064 dev = __dev_get_by_name(net, ifr->ifr_name);
2065 if (dev) {
f85ba780
DW
2066 if (ifr->ifr_flags & IFF_TUN_EXCL)
2067 return -EBUSY;
74a3e5a7
EB
2068 if ((ifr->ifr_flags & IFF_TUN) && dev->netdev_ops == &tun_netdev_ops)
2069 tun = netdev_priv(dev);
2070 else if ((ifr->ifr_flags & IFF_TAP) && dev->netdev_ops == &tap_netdev_ops)
2071 tun = netdev_priv(dev);
2072 else
2073 return -EINVAL;
2074
8e6d91ae 2075 if (!!(ifr->ifr_flags & IFF_MULTI_QUEUE) !=
40630b82 2076 !!(tun->flags & IFF_MULTI_QUEUE))
8e6d91ae
JW
2077 return -EINVAL;
2078
cde8b15f 2079 if (tun_not_capable(tun))
2b980dbd 2080 return -EPERM;
5dbbaf2d 2081 err = security_tun_dev_open(tun->security);
2b980dbd
PM
2082 if (err < 0)
2083 return err;
2084
94317099
PP
2085 err = tun_attach(tun, file, ifr->ifr_flags & IFF_NOFILTER,
2086 ifr->ifr_flags & IFF_NAPI);
a7385ba2
EB
2087 if (err < 0)
2088 return err;
4008e97f 2089
40630b82 2090 if (tun->flags & IFF_MULTI_QUEUE &&
e8dbad66
JW
2091 (tun->numqueues + tun->numdisabled > 1)) {
2092 /* One or more queue has already been attached, no need
2093 * to initialize the device again.
2094 */
2095 return 0;
2096 }
6aa20a22 2097 }
1da177e4
LT
2098 else {
2099 char *name;
2100 unsigned long flags = 0;
edfb6a14
JW
2101 int queues = ifr->ifr_flags & IFF_MULTI_QUEUE ?
2102 MAX_TAP_QUEUES : 1;
1da177e4 2103
c260b772 2104 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
ca6bb5d7 2105 return -EPERM;
2b980dbd
PM
2106 err = security_tun_dev_create();
2107 if (err < 0)
2108 return err;
ca6bb5d7 2109
1da177e4
LT
2110 /* Set dev type */
2111 if (ifr->ifr_flags & IFF_TUN) {
2112 /* TUN device */
40630b82 2113 flags |= IFF_TUN;
1da177e4
LT
2114 name = "tun%d";
2115 } else if (ifr->ifr_flags & IFF_TAP) {
2116 /* TAP device */
40630b82 2117 flags |= IFF_TAP;
1da177e4 2118 name = "tap%d";
6aa20a22 2119 } else
36989b90 2120 return -EINVAL;
6aa20a22 2121
1da177e4
LT
2122 if (*ifr->ifr_name)
2123 name = ifr->ifr_name;
2124
c8d68e6b 2125 dev = alloc_netdev_mqs(sizeof(struct tun_struct), name,
c835a677
TG
2126 NET_NAME_UNKNOWN, tun_setup, queues,
2127 queues);
edfb6a14 2128
1da177e4
LT
2129 if (!dev)
2130 return -ENOMEM;
2131
fc54c658 2132 dev_net_set(dev, net);
f019a7a5 2133 dev->rtnl_link_ops = &tun_link_ops;
fb7589a1 2134 dev->ifindex = tfile->ifindex;
c4d33e24 2135 dev->sysfs_groups[0] = &tun_attr_group;
758e43b7 2136
1da177e4
LT
2137 tun = netdev_priv(dev);
2138 tun->dev = dev;
2139 tun->flags = flags;
f271b2cc 2140 tun->txflt.count = 0;
d9d52b51 2141 tun->vnet_hdr_sz = sizeof(struct virtio_net_hdr);
33dccbb0 2142
eaea34b2 2143 tun->align = NET_SKB_PAD;
54f968d6
JW
2144 tun->filter_attached = false;
2145 tun->sndbuf = tfile->socket.sk->sk_sndbuf;
5503fcec 2146 tun->rx_batched = 0;
33dccbb0 2147
608b9977
PA
2148 tun->pcpu_stats = netdev_alloc_pcpu_stats(struct tun_pcpu_stats);
2149 if (!tun->pcpu_stats) {
2150 err = -ENOMEM;
2151 goto err_free_dev;
2152 }
2153
96442e42
JW
2154 spin_lock_init(&tun->lock);
2155
5dbbaf2d
PM
2156 err = security_tun_dev_alloc_security(&tun->security);
2157 if (err < 0)
608b9977 2158 goto err_free_stat;
2b980dbd 2159
1da177e4 2160 tun_net_init(dev);
944a1376 2161 tun_flow_init(tun);
96442e42 2162
88255375 2163 dev->hw_features = NETIF_F_SG | NETIF_F_FRAGLIST |
6680ec68
JW
2164 TUN_USER_FEATURES | NETIF_F_HW_VLAN_CTAG_TX |
2165 NETIF_F_HW_VLAN_STAG_TX;
2a2bbf17 2166 dev->features = dev->hw_features | NETIF_F_LLTX;
6671b224
FLVC
2167 dev->vlan_features = dev->features &
2168 ~(NETIF_F_HW_VLAN_CTAG_TX |
2169 NETIF_F_HW_VLAN_STAG_TX);
88255375 2170
4008e97f 2171 INIT_LIST_HEAD(&tun->disabled);
94317099 2172 err = tun_attach(tun, file, false, ifr->ifr_flags & IFF_NAPI);
eb0fb363 2173 if (err < 0)
662ca437 2174 goto err_free_flow;
eb0fb363 2175
1da177e4
LT
2176 err = register_netdevice(tun->dev);
2177 if (err < 0)
662ca437 2178 goto err_detach;
1da177e4
LT
2179 }
2180
af668b3c
MT
2181 netif_carrier_on(tun->dev);
2182
6b8a66ee 2183 tun_debug(KERN_INFO, tun, "tun_set_iff\n");
1da177e4 2184
031f5e03
MT
2185 tun->flags = (tun->flags & ~TUN_FEATURES) |
2186 (ifr->ifr_flags & TUN_FEATURES);
c8d68e6b 2187
e35259a9
MK
2188 /* Make sure persistent devices do not get stuck in
2189 * xoff state.
2190 */
2191 if (netif_running(tun->dev))
c8d68e6b 2192 netif_tx_wake_all_queues(tun->dev);
e35259a9 2193
1da177e4
LT
2194 strcpy(ifr->ifr_name, tun->dev->name);
2195 return 0;
2196
662ca437
JW
2197err_detach:
2198 tun_detach_all(dev);
ff244c6b
ED
2199 /* register_netdevice() already called tun_free_netdev() */
2200 goto err_free_dev;
2201
662ca437
JW
2202err_free_flow:
2203 tun_flow_uninit(tun);
2204 security_tun_dev_free_security(tun->security);
608b9977
PA
2205err_free_stat:
2206 free_percpu(tun->pcpu_stats);
662ca437 2207err_free_dev:
1da177e4 2208 free_netdev(dev);
1da177e4
LT
2209 return err;
2210}
2211
9ce99cf6 2212static void tun_get_iff(struct net *net, struct tun_struct *tun,
876bfd4d 2213 struct ifreq *ifr)
e3b99556 2214{
6b8a66ee 2215 tun_debug(KERN_INFO, tun, "tun_get_iff\n");
e3b99556
MM
2216
2217 strcpy(ifr->ifr_name, tun->dev->name);
2218
980c9e8c 2219 ifr->ifr_flags = tun_flags(tun);
e3b99556 2220
e3b99556
MM
2221}
2222
5228ddc9
RR
2223/* This is like a cut-down ethtool ops, except done via tun fd so no
2224 * privs required. */
88255375 2225static int set_offload(struct tun_struct *tun, unsigned long arg)
5228ddc9 2226{
c8f44aff 2227 netdev_features_t features = 0;
5228ddc9
RR
2228
2229 if (arg & TUN_F_CSUM) {
88255375 2230 features |= NETIF_F_HW_CSUM;
5228ddc9
RR
2231 arg &= ~TUN_F_CSUM;
2232
2233 if (arg & (TUN_F_TSO4|TUN_F_TSO6)) {
2234 if (arg & TUN_F_TSO_ECN) {
2235 features |= NETIF_F_TSO_ECN;
2236 arg &= ~TUN_F_TSO_ECN;
2237 }
2238 if (arg & TUN_F_TSO4)
2239 features |= NETIF_F_TSO;
2240 if (arg & TUN_F_TSO6)
2241 features |= NETIF_F_TSO6;
2242 arg &= ~(TUN_F_TSO4|TUN_F_TSO6);
2243 }
2244 }
2245
2246 /* This gives the user a way to test for new features in future by
2247 * trying to set them. */
2248 if (arg)
2249 return -EINVAL;
2250
88255375 2251 tun->set_features = features;
09050957
YI
2252 tun->dev->wanted_features &= ~TUN_USER_FEATURES;
2253 tun->dev->wanted_features |= features;
88255375 2254 netdev_update_features(tun->dev);
5228ddc9
RR
2255
2256 return 0;
2257}
2258
c8d68e6b
JW
2259static void tun_detach_filter(struct tun_struct *tun, int n)
2260{
2261 int i;
2262 struct tun_file *tfile;
2263
2264 for (i = 0; i < n; i++) {
b8deabd3 2265 tfile = rtnl_dereference(tun->tfiles[i]);
8ced425e
HFS
2266 lock_sock(tfile->socket.sk);
2267 sk_detach_filter(tfile->socket.sk);
2268 release_sock(tfile->socket.sk);
c8d68e6b
JW
2269 }
2270
2271 tun->filter_attached = false;
2272}
2273
2274static int tun_attach_filter(struct tun_struct *tun)
2275{
2276 int i, ret = 0;
2277 struct tun_file *tfile;
2278
2279 for (i = 0; i < tun->numqueues; i++) {
b8deabd3 2280 tfile = rtnl_dereference(tun->tfiles[i]);
8ced425e
HFS
2281 lock_sock(tfile->socket.sk);
2282 ret = sk_attach_filter(&tun->fprog, tfile->socket.sk);
2283 release_sock(tfile->socket.sk);
c8d68e6b
JW
2284 if (ret) {
2285 tun_detach_filter(tun, i);
2286 return ret;
2287 }
2288 }
2289
2290 tun->filter_attached = true;
2291 return ret;
2292}
2293
2294static void tun_set_sndbuf(struct tun_struct *tun)
2295{
2296 struct tun_file *tfile;
2297 int i;
2298
2299 for (i = 0; i < tun->numqueues; i++) {
b8deabd3 2300 tfile = rtnl_dereference(tun->tfiles[i]);
c8d68e6b
JW
2301 tfile->socket.sk->sk_sndbuf = tun->sndbuf;
2302 }
2303}
2304
cde8b15f
JW
2305static int tun_set_queue(struct file *file, struct ifreq *ifr)
2306{
2307 struct tun_file *tfile = file->private_data;
2308 struct tun_struct *tun;
cde8b15f
JW
2309 int ret = 0;
2310
2311 rtnl_lock();
2312
2313 if (ifr->ifr_flags & IFF_ATTACH_QUEUE) {
4008e97f 2314 tun = tfile->detached;
5dbbaf2d 2315 if (!tun) {
cde8b15f 2316 ret = -EINVAL;
5dbbaf2d
PM
2317 goto unlock;
2318 }
2319 ret = security_tun_dev_attach_queue(tun->security);
2320 if (ret < 0)
2321 goto unlock;
94317099 2322 ret = tun_attach(tun, file, false, tun->flags & IFF_NAPI);
4008e97f 2323 } else if (ifr->ifr_flags & IFF_DETACH_QUEUE) {
b8deabd3 2324 tun = rtnl_dereference(tfile->tun);
40630b82 2325 if (!tun || !(tun->flags & IFF_MULTI_QUEUE) || tfile->detached)
4008e97f
JW
2326 ret = -EINVAL;
2327 else
2328 __tun_detach(tfile, false);
2329 } else
cde8b15f
JW
2330 ret = -EINVAL;
2331
5dbbaf2d 2332unlock:
cde8b15f
JW
2333 rtnl_unlock();
2334 return ret;
2335}
2336
50857e2a
AB
2337static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
2338 unsigned long arg, int ifreq_len)
1da177e4 2339{
36b50bab 2340 struct tun_file *tfile = file->private_data;
631ab46b 2341 struct tun_struct *tun;
1da177e4
LT
2342 void __user* argp = (void __user*)arg;
2343 struct ifreq ifr;
0625c883
EB
2344 kuid_t owner;
2345 kgid_t group;
33dccbb0 2346 int sndbuf;
d9d52b51 2347 int vnet_hdr_sz;
fb7589a1 2348 unsigned int ifindex;
1cf8e410 2349 int le;
f271b2cc 2350 int ret;
1da177e4 2351
20861f26 2352 if (cmd == TUNSETIFF || cmd == TUNSETQUEUE || _IOC_TYPE(cmd) == SOCK_IOC_TYPE) {
50857e2a 2353 if (copy_from_user(&ifr, argp, ifreq_len))
1da177e4 2354 return -EFAULT;
8bbb1813 2355 } else {
a117dacd 2356 memset(&ifr, 0, sizeof(ifr));
8bbb1813 2357 }
631ab46b
EB
2358 if (cmd == TUNGETFEATURES) {
2359 /* Currently this just means: "what IFF flags are valid?".
2360 * This is needed because we never checked for invalid flags on
031f5e03
MT
2361 * TUNSETIFF.
2362 */
2363 return put_user(IFF_TUN | IFF_TAP | TUN_FEATURES,
631ab46b 2364 (unsigned int __user*)argp);
cde8b15f
JW
2365 } else if (cmd == TUNSETQUEUE)
2366 return tun_set_queue(file, &ifr);
631ab46b 2367
c8d68e6b 2368 ret = 0;
876bfd4d
HX
2369 rtnl_lock();
2370
36b50bab 2371 tun = __tun_get(tfile);
0f16bc13
GF
2372 if (cmd == TUNSETIFF) {
2373 ret = -EEXIST;
2374 if (tun)
2375 goto unlock;
2376
1da177e4
LT
2377 ifr.ifr_name[IFNAMSIZ-1] = '\0';
2378
140e807d 2379 ret = tun_set_iff(sock_net(&tfile->sk), file, &ifr);
1da177e4 2380
876bfd4d
HX
2381 if (ret)
2382 goto unlock;
1da177e4 2383
50857e2a 2384 if (copy_to_user(argp, &ifr, ifreq_len))
876bfd4d
HX
2385 ret = -EFAULT;
2386 goto unlock;
1da177e4 2387 }
fb7589a1
PE
2388 if (cmd == TUNSETIFINDEX) {
2389 ret = -EPERM;
2390 if (tun)
2391 goto unlock;
2392
2393 ret = -EFAULT;
2394 if (copy_from_user(&ifindex, argp, sizeof(ifindex)))
2395 goto unlock;
2396
2397 ret = 0;
2398 tfile->ifindex = ifindex;
2399 goto unlock;
2400 }
1da177e4 2401
876bfd4d 2402 ret = -EBADFD;
1da177e4 2403 if (!tun)
876bfd4d 2404 goto unlock;
1da177e4 2405
1e588338 2406 tun_debug(KERN_INFO, tun, "tun_chr_ioctl cmd %u\n", cmd);
1da177e4 2407
631ab46b 2408 ret = 0;
1da177e4 2409 switch (cmd) {
e3b99556 2410 case TUNGETIFF:
9ce99cf6 2411 tun_get_iff(current->nsproxy->net_ns, tun, &ifr);
e3b99556 2412
3d407a80
PE
2413 if (tfile->detached)
2414 ifr.ifr_flags |= IFF_DETACH_QUEUE;
849c9b6f
PE
2415 if (!tfile->socket.sk->sk_filter)
2416 ifr.ifr_flags |= IFF_NOFILTER;
3d407a80 2417
50857e2a 2418 if (copy_to_user(argp, &ifr, ifreq_len))
631ab46b 2419 ret = -EFAULT;
e3b99556
MM
2420 break;
2421
1da177e4
LT
2422 case TUNSETNOCSUM:
2423 /* Disable/Enable checksum */
1da177e4 2424
88255375
MM
2425 /* [unimplemented] */
2426 tun_debug(KERN_INFO, tun, "ignored: set checksum %s\n",
6b8a66ee 2427 arg ? "disabled" : "enabled");
1da177e4
LT
2428 break;
2429
2430 case TUNSETPERSIST:
54f968d6
JW
2431 /* Disable/Enable persist mode. Keep an extra reference to the
2432 * module to prevent the module being unprobed.
2433 */
40630b82
MT
2434 if (arg && !(tun->flags & IFF_PERSIST)) {
2435 tun->flags |= IFF_PERSIST;
54f968d6 2436 __module_get(THIS_MODULE);
dd38bd85 2437 }
40630b82
MT
2438 if (!arg && (tun->flags & IFF_PERSIST)) {
2439 tun->flags &= ~IFF_PERSIST;
54f968d6
JW
2440 module_put(THIS_MODULE);
2441 }
1da177e4 2442
6b8a66ee
JP
2443 tun_debug(KERN_INFO, tun, "persist %s\n",
2444 arg ? "enabled" : "disabled");
1da177e4
LT
2445 break;
2446
2447 case TUNSETOWNER:
2448 /* Set owner of the device */
0625c883
EB
2449 owner = make_kuid(current_user_ns(), arg);
2450 if (!uid_valid(owner)) {
2451 ret = -EINVAL;
2452 break;
2453 }
2454 tun->owner = owner;
1e588338 2455 tun_debug(KERN_INFO, tun, "owner set to %u\n",
0625c883 2456 from_kuid(&init_user_ns, tun->owner));
1da177e4
LT
2457 break;
2458
8c644623
GG
2459 case TUNSETGROUP:
2460 /* Set group of the device */
0625c883
EB
2461 group = make_kgid(current_user_ns(), arg);
2462 if (!gid_valid(group)) {
2463 ret = -EINVAL;
2464 break;
2465 }
2466 tun->group = group;
1e588338 2467 tun_debug(KERN_INFO, tun, "group set to %u\n",
0625c883 2468 from_kgid(&init_user_ns, tun->group));
8c644623
GG
2469 break;
2470
ff4cc3ac
MK
2471 case TUNSETLINK:
2472 /* Only allow setting the type when the interface is down */
2473 if (tun->dev->flags & IFF_UP) {
6b8a66ee
JP
2474 tun_debug(KERN_INFO, tun,
2475 "Linktype set failed because interface is up\n");
48abfe05 2476 ret = -EBUSY;
ff4cc3ac
MK
2477 } else {
2478 tun->dev->type = (int) arg;
6b8a66ee
JP
2479 tun_debug(KERN_INFO, tun, "linktype set to %d\n",
2480 tun->dev->type);
48abfe05 2481 ret = 0;
ff4cc3ac 2482 }
631ab46b 2483 break;
ff4cc3ac 2484
1da177e4
LT
2485#ifdef TUN_DEBUG
2486 case TUNSETDEBUG:
2487 tun->debug = arg;
2488 break;
2489#endif
5228ddc9 2490 case TUNSETOFFLOAD:
88255375 2491 ret = set_offload(tun, arg);
631ab46b 2492 break;
5228ddc9 2493
f271b2cc
MK
2494 case TUNSETTXFILTER:
2495 /* Can be set only for TAPs */
631ab46b 2496 ret = -EINVAL;
40630b82 2497 if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP)
631ab46b 2498 break;
c0e5a8c2 2499 ret = update_filter(&tun->txflt, (void __user *)arg);
631ab46b 2500 break;
1da177e4
LT
2501
2502 case SIOCGIFHWADDR:
b595076a 2503 /* Get hw address */
f271b2cc
MK
2504 memcpy(ifr.ifr_hwaddr.sa_data, tun->dev->dev_addr, ETH_ALEN);
2505 ifr.ifr_hwaddr.sa_family = tun->dev->type;
50857e2a 2506 if (copy_to_user(argp, &ifr, ifreq_len))
631ab46b
EB
2507 ret = -EFAULT;
2508 break;
1da177e4
LT
2509
2510 case SIOCSIFHWADDR:
f271b2cc 2511 /* Set hw address */
6b8a66ee
JP
2512 tun_debug(KERN_DEBUG, tun, "set hw address: %pM\n",
2513 ifr.ifr_hwaddr.sa_data);
40102371 2514
40102371 2515 ret = dev_set_mac_address(tun->dev, &ifr.ifr_hwaddr);
631ab46b 2516 break;
33dccbb0
HX
2517
2518 case TUNGETSNDBUF:
54f968d6 2519 sndbuf = tfile->socket.sk->sk_sndbuf;
33dccbb0
HX
2520 if (copy_to_user(argp, &sndbuf, sizeof(sndbuf)))
2521 ret = -EFAULT;
2522 break;
2523
2524 case TUNSETSNDBUF:
2525 if (copy_from_user(&sndbuf, argp, sizeof(sndbuf))) {
2526 ret = -EFAULT;
2527 break;
2528 }
2529
c8d68e6b
JW
2530 tun->sndbuf = sndbuf;
2531 tun_set_sndbuf(tun);
33dccbb0
HX
2532 break;
2533
d9d52b51
MT
2534 case TUNGETVNETHDRSZ:
2535 vnet_hdr_sz = tun->vnet_hdr_sz;
2536 if (copy_to_user(argp, &vnet_hdr_sz, sizeof(vnet_hdr_sz)))
2537 ret = -EFAULT;
2538 break;
2539
2540 case TUNSETVNETHDRSZ:
2541 if (copy_from_user(&vnet_hdr_sz, argp, sizeof(vnet_hdr_sz))) {
2542 ret = -EFAULT;
2543 break;
2544 }
2545 if (vnet_hdr_sz < (int)sizeof(struct virtio_net_hdr)) {
2546 ret = -EINVAL;
2547 break;
2548 }
2549
2550 tun->vnet_hdr_sz = vnet_hdr_sz;
2551 break;
2552
1cf8e410
MT
2553 case TUNGETVNETLE:
2554 le = !!(tun->flags & TUN_VNET_LE);
2555 if (put_user(le, (int __user *)argp))
2556 ret = -EFAULT;
2557 break;
2558
2559 case TUNSETVNETLE:
2560 if (get_user(le, (int __user *)argp)) {
2561 ret = -EFAULT;
2562 break;
2563 }
2564 if (le)
2565 tun->flags |= TUN_VNET_LE;
2566 else
2567 tun->flags &= ~TUN_VNET_LE;
2568 break;
2569
8b8e658b
GK
2570 case TUNGETVNETBE:
2571 ret = tun_get_vnet_be(tun, argp);
2572 break;
2573
2574 case TUNSETVNETBE:
2575 ret = tun_set_vnet_be(tun, argp);
2576 break;
2577
99405162
MT
2578 case TUNATTACHFILTER:
2579 /* Can be set only for TAPs */
2580 ret = -EINVAL;
40630b82 2581 if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP)
99405162
MT
2582 break;
2583 ret = -EFAULT;
54f968d6 2584 if (copy_from_user(&tun->fprog, argp, sizeof(tun->fprog)))
99405162
MT
2585 break;
2586
c8d68e6b 2587 ret = tun_attach_filter(tun);
99405162
MT
2588 break;
2589
2590 case TUNDETACHFILTER:
2591 /* Can be set only for TAPs */
2592 ret = -EINVAL;
40630b82 2593 if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP)
99405162 2594 break;
c8d68e6b
JW
2595 ret = 0;
2596 tun_detach_filter(tun, tun->numqueues);
99405162
MT
2597 break;
2598
76975e9c
PE
2599 case TUNGETFILTER:
2600 ret = -EINVAL;
40630b82 2601 if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP)
76975e9c
PE
2602 break;
2603 ret = -EFAULT;
2604 if (copy_to_user(argp, &tun->fprog, sizeof(tun->fprog)))
2605 break;
2606 ret = 0;
2607 break;
2608
1da177e4 2609 default:
631ab46b
EB
2610 ret = -EINVAL;
2611 break;
ee289b64 2612 }
1da177e4 2613
876bfd4d
HX
2614unlock:
2615 rtnl_unlock();
2616 if (tun)
2617 tun_put(tun);
631ab46b 2618 return ret;
1da177e4
LT
2619}
2620
50857e2a
AB
2621static long tun_chr_ioctl(struct file *file,
2622 unsigned int cmd, unsigned long arg)
2623{
2624 return __tun_chr_ioctl(file, cmd, arg, sizeof (struct ifreq));
2625}
2626
2627#ifdef CONFIG_COMPAT
2628static long tun_chr_compat_ioctl(struct file *file,
2629 unsigned int cmd, unsigned long arg)
2630{
2631 switch (cmd) {
2632 case TUNSETIFF:
2633 case TUNGETIFF:
2634 case TUNSETTXFILTER:
2635 case TUNGETSNDBUF:
2636 case TUNSETSNDBUF:
2637 case SIOCGIFHWADDR:
2638 case SIOCSIFHWADDR:
2639 arg = (unsigned long)compat_ptr(arg);
2640 break;
2641 default:
2642 arg = (compat_ulong_t)arg;
2643 break;
2644 }
2645
2646 /*
2647 * compat_ifreq is shorter than ifreq, so we must not access beyond
2648 * the end of that structure. All fields that are used in this
2649 * driver are compatible though, we don't need to convert the
2650 * contents.
2651 */
2652 return __tun_chr_ioctl(file, cmd, arg, sizeof(struct compat_ifreq));
2653}
2654#endif /* CONFIG_COMPAT */
2655
1da177e4
LT
2656static int tun_chr_fasync(int fd, struct file *file, int on)
2657{
54f968d6 2658 struct tun_file *tfile = file->private_data;
1da177e4
LT
2659 int ret;
2660
54f968d6 2661 if ((ret = fasync_helper(fd, file, on, &tfile->fasync)) < 0)
9d319522 2662 goto out;
6aa20a22 2663
1da177e4 2664 if (on) {
e0b93edd 2665 __f_setown(file, task_pid(current), PIDTYPE_PID, 0);
54f968d6 2666 tfile->flags |= TUN_FASYNC;
6aa20a22 2667 } else
54f968d6 2668 tfile->flags &= ~TUN_FASYNC;
9d319522
JC
2669 ret = 0;
2670out:
9d319522 2671 return ret;
1da177e4
LT
2672}
2673
2674static int tun_chr_open(struct inode *inode, struct file * file)
2675{
140e807d 2676 struct net *net = current->nsproxy->net_ns;
631ab46b 2677 struct tun_file *tfile;
deed49fb 2678
6b8a66ee 2679 DBG1(KERN_INFO, "tunX: tun_chr_open\n");
631ab46b 2680
140e807d 2681 tfile = (struct tun_file *)sk_alloc(net, AF_UNSPEC, GFP_KERNEL,
11aa9c28 2682 &tun_proto, 0);
631ab46b
EB
2683 if (!tfile)
2684 return -ENOMEM;
c956674b 2685 RCU_INIT_POINTER(tfile->tun, NULL);
54f968d6 2686 tfile->flags = 0;
fb7589a1 2687 tfile->ifindex = 0;
54f968d6 2688
54f968d6 2689 init_waitqueue_head(&tfile->wq.wait);
9e641bdc 2690 RCU_INIT_POINTER(tfile->socket.wq, &tfile->wq);
54f968d6
JW
2691
2692 tfile->socket.file = file;
2693 tfile->socket.ops = &tun_socket_ops;
2694
2695 sock_init_data(&tfile->socket, &tfile->sk);
54f968d6
JW
2696
2697 tfile->sk.sk_write_space = tun_sock_write_space;
2698 tfile->sk.sk_sndbuf = INT_MAX;
2699
631ab46b 2700 file->private_data = tfile;
4008e97f 2701 INIT_LIST_HEAD(&tfile->next);
54f968d6 2702
19a6afb2
JW
2703 sock_set_flag(&tfile->sk, SOCK_ZEROCOPY);
2704
1da177e4
LT
2705 return 0;
2706}
2707
2708static int tun_chr_close(struct inode *inode, struct file *file)
2709{
631ab46b 2710 struct tun_file *tfile = file->private_data;
1da177e4 2711
c8d68e6b 2712 tun_detach(tfile, true);
1da177e4
LT
2713
2714 return 0;
2715}
2716
93e14b6d 2717#ifdef CONFIG_PROC_FS
a3816ab0 2718static void tun_chr_show_fdinfo(struct seq_file *m, struct file *f)
93e14b6d
MY
2719{
2720 struct tun_struct *tun;
2721 struct ifreq ifr;
2722
2723 memset(&ifr, 0, sizeof(ifr));
2724
2725 rtnl_lock();
2726 tun = tun_get(f);
2727 if (tun)
2728 tun_get_iff(current->nsproxy->net_ns, tun, &ifr);
2729 rtnl_unlock();
2730
2731 if (tun)
2732 tun_put(tun);
2733
a3816ab0 2734 seq_printf(m, "iff:\t%s\n", ifr.ifr_name);
93e14b6d
MY
2735}
2736#endif
2737
d54b1fdb 2738static const struct file_operations tun_fops = {
6aa20a22 2739 .owner = THIS_MODULE,
1da177e4 2740 .llseek = no_llseek,
9b067034 2741 .read_iter = tun_chr_read_iter,
f5ff53b4 2742 .write_iter = tun_chr_write_iter,
1da177e4 2743 .poll = tun_chr_poll,
50857e2a
AB
2744 .unlocked_ioctl = tun_chr_ioctl,
2745#ifdef CONFIG_COMPAT
2746 .compat_ioctl = tun_chr_compat_ioctl,
2747#endif
1da177e4
LT
2748 .open = tun_chr_open,
2749 .release = tun_chr_close,
93e14b6d
MY
2750 .fasync = tun_chr_fasync,
2751#ifdef CONFIG_PROC_FS
2752 .show_fdinfo = tun_chr_show_fdinfo,
2753#endif
1da177e4
LT
2754};
2755
2756static struct miscdevice tun_miscdev = {
2757 .minor = TUN_MINOR,
2758 .name = "tun",
e454cea2 2759 .nodename = "net/tun",
1da177e4 2760 .fops = &tun_fops,
1da177e4
LT
2761};
2762
2763/* ethtool interface */
2764
29ccc49d
PR
2765static int tun_get_link_ksettings(struct net_device *dev,
2766 struct ethtool_link_ksettings *cmd)
2767{
2768 ethtool_link_ksettings_zero_link_mode(cmd, supported);
2769 ethtool_link_ksettings_zero_link_mode(cmd, advertising);
2770 cmd->base.speed = SPEED_10;
2771 cmd->base.duplex = DUPLEX_FULL;
2772 cmd->base.port = PORT_TP;
2773 cmd->base.phy_address = 0;
2774 cmd->base.autoneg = AUTONEG_DISABLE;
1da177e4
LT
2775 return 0;
2776}
2777
2778static void tun_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
2779{
2780 struct tun_struct *tun = netdev_priv(dev);
2781
33a5ba14
RJ
2782 strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
2783 strlcpy(info->version, DRV_VERSION, sizeof(info->version));
1da177e4
LT
2784
2785 switch (tun->flags & TUN_TYPE_MASK) {
40630b82 2786 case IFF_TUN:
33a5ba14 2787 strlcpy(info->bus_info, "tun", sizeof(info->bus_info));
1da177e4 2788 break;
40630b82 2789 case IFF_TAP:
33a5ba14 2790 strlcpy(info->bus_info, "tap", sizeof(info->bus_info));
1da177e4
LT
2791 break;
2792 }
2793}
2794
2795static u32 tun_get_msglevel(struct net_device *dev)
2796{
2797#ifdef TUN_DEBUG
2798 struct tun_struct *tun = netdev_priv(dev);
2799 return tun->debug;
2800#else
2801 return -EOPNOTSUPP;
2802#endif
2803}
2804
2805static void tun_set_msglevel(struct net_device *dev, u32 value)
2806{
2807#ifdef TUN_DEBUG
2808 struct tun_struct *tun = netdev_priv(dev);
2809 tun->debug = value;
2810#endif
2811}
2812
5503fcec
JW
2813static int tun_get_coalesce(struct net_device *dev,
2814 struct ethtool_coalesce *ec)
2815{
2816 struct tun_struct *tun = netdev_priv(dev);
2817
2818 ec->rx_max_coalesced_frames = tun->rx_batched;
2819
2820 return 0;
2821}
2822
2823static int tun_set_coalesce(struct net_device *dev,
2824 struct ethtool_coalesce *ec)
2825{
2826 struct tun_struct *tun = netdev_priv(dev);
2827
2828 if (ec->rx_max_coalesced_frames > NAPI_POLL_WEIGHT)
2829 tun->rx_batched = NAPI_POLL_WEIGHT;
2830 else
2831 tun->rx_batched = ec->rx_max_coalesced_frames;
2832
2833 return 0;
2834}
2835
7282d491 2836static const struct ethtool_ops tun_ethtool_ops = {
1da177e4
LT
2837 .get_drvinfo = tun_get_drvinfo,
2838 .get_msglevel = tun_get_msglevel,
2839 .set_msglevel = tun_set_msglevel,
bee31369 2840 .get_link = ethtool_op_get_link,
eda29772 2841 .get_ts_info = ethtool_op_get_ts_info,
5503fcec
JW
2842 .get_coalesce = tun_get_coalesce,
2843 .set_coalesce = tun_set_coalesce,
29ccc49d 2844 .get_link_ksettings = tun_get_link_ksettings,
1da177e4
LT
2845};
2846
1576d986
JW
2847static int tun_queue_resize(struct tun_struct *tun)
2848{
2849 struct net_device *dev = tun->dev;
2850 struct tun_file *tfile;
2851 struct skb_array **arrays;
2852 int n = tun->numqueues + tun->numdisabled;
2853 int ret, i;
2854
12039046 2855 arrays = kmalloc_array(n, sizeof(*arrays), GFP_KERNEL);
1576d986
JW
2856 if (!arrays)
2857 return -ENOMEM;
2858
2859 for (i = 0; i < tun->numqueues; i++) {
2860 tfile = rtnl_dereference(tun->tfiles[i]);
2861 arrays[i] = &tfile->tx_array;
2862 }
2863 list_for_each_entry(tfile, &tun->disabled, next)
2864 arrays[i++] = &tfile->tx_array;
2865
2866 ret = skb_array_resize_multiple(arrays, n,
2867 dev->tx_queue_len, GFP_KERNEL);
2868
2869 kfree(arrays);
2870 return ret;
2871}
2872
2873static int tun_device_event(struct notifier_block *unused,
2874 unsigned long event, void *ptr)
2875{
2876 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
2877 struct tun_struct *tun = netdev_priv(dev);
2878
86dfb4ac
CG
2879 if (dev->rtnl_link_ops != &tun_link_ops)
2880 return NOTIFY_DONE;
2881
1576d986
JW
2882 switch (event) {
2883 case NETDEV_CHANGE_TX_QUEUE_LEN:
2884 if (tun_queue_resize(tun))
2885 return NOTIFY_BAD;
2886 break;
2887 default:
2888 break;
2889 }
2890
2891 return NOTIFY_DONE;
2892}
2893
2894static struct notifier_block tun_notifier_block __read_mostly = {
2895 .notifier_call = tun_device_event,
2896};
79d17604 2897
1da177e4
LT
2898static int __init tun_init(void)
2899{
2900 int ret = 0;
2901
6b8a66ee 2902 pr_info("%s, %s\n", DRV_DESCRIPTION, DRV_VERSION);
1da177e4 2903
f019a7a5 2904 ret = rtnl_link_register(&tun_link_ops);
79d17604 2905 if (ret) {
6b8a66ee 2906 pr_err("Can't register link_ops\n");
f019a7a5 2907 goto err_linkops;
79d17604
PE
2908 }
2909
1da177e4 2910 ret = misc_register(&tun_miscdev);
79d17604 2911 if (ret) {
6b8a66ee 2912 pr_err("Can't register misc device %d\n", TUN_MINOR);
79d17604
PE
2913 goto err_misc;
2914 }
1576d986 2915
5edfbd3c
TZ
2916 ret = register_netdevice_notifier(&tun_notifier_block);
2917 if (ret) {
2918 pr_err("Can't register netdevice notifier\n");
2919 goto err_notifier;
2920 }
2921
f019a7a5 2922 return 0;
5edfbd3c
TZ
2923
2924err_notifier:
2925 misc_deregister(&tun_miscdev);
79d17604 2926err_misc:
f019a7a5
EB
2927 rtnl_link_unregister(&tun_link_ops);
2928err_linkops:
1da177e4
LT
2929 return ret;
2930}
2931
2932static void tun_cleanup(void)
2933{
6aa20a22 2934 misc_deregister(&tun_miscdev);
f019a7a5 2935 rtnl_link_unregister(&tun_link_ops);
1576d986 2936 unregister_netdevice_notifier(&tun_notifier_block);
1da177e4
LT
2937}
2938
05c2828c
MT
2939/* Get an underlying socket object from tun file. Returns error unless file is
2940 * attached to a device. The returned object works like a packet socket, it
2941 * can be used for sock_sendmsg/sock_recvmsg. The caller is responsible for
2942 * holding a reference to the file for as long as the socket is in use. */
2943struct socket *tun_get_socket(struct file *file)
2944{
6e914fc7 2945 struct tun_file *tfile;
05c2828c
MT
2946 if (file->f_op != &tun_fops)
2947 return ERR_PTR(-EINVAL);
6e914fc7
JW
2948 tfile = file->private_data;
2949 if (!tfile)
05c2828c 2950 return ERR_PTR(-EBADFD);
54f968d6 2951 return &tfile->socket;
05c2828c
MT
2952}
2953EXPORT_SYMBOL_GPL(tun_get_socket);
2954
83339c6b
JW
2955struct skb_array *tun_get_skb_array(struct file *file)
2956{
2957 struct tun_file *tfile;
2958
2959 if (file->f_op != &tun_fops)
2960 return ERR_PTR(-EINVAL);
2961 tfile = file->private_data;
2962 if (!tfile)
2963 return ERR_PTR(-EBADFD);
2964 return &tfile->tx_array;
2965}
2966EXPORT_SYMBOL_GPL(tun_get_skb_array);
2967
1da177e4
LT
2968module_init(tun_init);
2969module_exit(tun_cleanup);
2970MODULE_DESCRIPTION(DRV_DESCRIPTION);
2971MODULE_AUTHOR(DRV_COPYRIGHT);
2972MODULE_LICENSE("GPL");
2973MODULE_ALIAS_MISCDEV(TUN_MINOR);
578454ff 2974MODULE_ALIAS("devname:net/tun");