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