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