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