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