tuntap: introduce multiqueue flags
[linux-2.6-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>
63#include <linux/crc32.h>
d647a591 64#include <linux/nsproxy.h>
f43798c2 65#include <linux/virtio_net.h>
99405162 66#include <linux/rcupdate.h>
881d966b 67#include <net/net_namespace.h>
79d17604 68#include <net/netns/generic.h>
f019a7a5 69#include <net/rtnetlink.h>
33dccbb0 70#include <net/sock.h>
1da177e4 71
1da177e4
LT
72#include <asm/uaccess.h>
73
14daa021
RR
74/* Uncomment to enable debugging */
75/* #define TUN_DEBUG 1 */
76
1da177e4
LT
77#ifdef TUN_DEBUG
78static int debug;
14daa021 79
6b8a66ee
JP
80#define tun_debug(level, tun, fmt, args...) \
81do { \
82 if (tun->debug) \
83 netdev_printk(level, tun->dev, fmt, ##args); \
84} while (0)
85#define DBG1(level, fmt, args...) \
86do { \
87 if (debug == 2) \
88 printk(level fmt, ##args); \
89} while (0)
14daa021 90#else
6b8a66ee
JP
91#define tun_debug(level, tun, fmt, args...) \
92do { \
93 if (0) \
94 netdev_printk(level, tun->dev, fmt, ##args); \
95} while (0)
96#define DBG1(level, fmt, args...) \
97do { \
98 if (0) \
99 printk(level fmt, ##args); \
100} while (0)
14daa021
RR
101#endif
102
0690899b
MT
103#define GOODCOPY_LEN 128
104
f271b2cc
MK
105#define FLT_EXACT_COUNT 8
106struct tap_filter {
107 unsigned int count; /* Number of addrs. Zero means disabled */
108 u32 mask[2]; /* Mask of the hashed addrs */
109 unsigned char addr[FLT_EXACT_COUNT][ETH_ALEN];
110};
111
54f968d6
JW
112/* A tun_file connects an open character device to a tuntap netdevice. It
113 * also contains all socket related strctures (except sock_fprog and tap_filter)
114 * to serve as one transmit queue for tuntap device. The sock_fprog and
115 * tap_filter were kept in tun_struct since they were used for filtering for the
116 * netdevice not for a specific queue (at least I didn't see the reqirement for
117 * this).
6e914fc7
JW
118 *
119 * RCU usage:
120 * The tun_file and tun_struct are loosely coupled, the pointer from on to the
121 * other can only be read while rcu_read_lock or rtnl_lock is held.
54f968d6 122 */
631ab46b 123struct tun_file {
54f968d6
JW
124 struct sock sk;
125 struct socket socket;
126 struct socket_wq wq;
6e914fc7 127 struct tun_struct __rcu *tun;
36b50bab 128 struct net *net;
54f968d6
JW
129 struct fasync_struct *fasync;
130 /* only used for fasnyc */
131 unsigned int flags;
631ab46b
EB
132};
133
54f968d6
JW
134/* Since the socket were moved to tun_file, to preserve the behavior of persist
135 * device, socket fileter, sndbuf and vnet header size were restore when the
136 * file were attached to a persist device.
137 */
14daa021 138struct tun_struct {
6e914fc7 139 struct tun_file __rcu *tfile;
f271b2cc 140 unsigned int flags;
0625c883
EB
141 kuid_t owner;
142 kgid_t group;
14daa021 143
14daa021 144 struct net_device *dev;
c8f44aff 145 netdev_features_t set_features;
88255375
MM
146#define TUN_USER_FEATURES (NETIF_F_HW_CSUM|NETIF_F_TSO_ECN|NETIF_F_TSO| \
147 NETIF_F_TSO6|NETIF_F_UFO)
d9d52b51
MT
148
149 int vnet_hdr_sz;
54f968d6
JW
150 int sndbuf;
151 struct tap_filter txflt;
152 struct sock_fprog fprog;
153 /* protected by rtnl lock */
154 bool filter_attached;
14daa021
RR
155#ifdef TUN_DEBUG
156 int debug;
1da177e4 157#endif
14daa021 158};
1da177e4 159
a7385ba2
EB
160static int tun_attach(struct tun_struct *tun, struct file *file)
161{
631ab46b 162 struct tun_file *tfile = file->private_data;
38231b7a 163 int err;
a7385ba2
EB
164
165 ASSERT_RTNL();
166
38231b7a
EB
167 netif_tx_lock_bh(tun->dev);
168
169 err = -EINVAL;
170 if (tfile->tun)
171 goto out;
172
173 err = -EBUSY;
174 if (tun->tfile)
175 goto out;
176
177 err = 0;
54f968d6
JW
178
179 /* Re-attach filter when attaching to a persist device */
180 if (tun->filter_attached == true) {
181 err = sk_attach_filter(&tun->fprog, tfile->socket.sk);
182 if (!err)
183 goto out;
184 }
6e914fc7 185 rcu_assign_pointer(tfile->tun, tun);
54f968d6 186 tfile->socket.sk->sk_sndbuf = tun->sndbuf;
6e914fc7 187 rcu_assign_pointer(tun->tfile, tfile);
bee31369 188 netif_carrier_on(tun->dev);
54f968d6 189 sock_hold(&tfile->sk);
a7385ba2 190
38231b7a
EB
191out:
192 netif_tx_unlock_bh(tun->dev);
193 return err;
a7385ba2
EB
194}
195
631ab46b
EB
196static void __tun_detach(struct tun_struct *tun)
197{
6e914fc7
JW
198 struct tun_file *tfile = rcu_dereference_protected(tun->tfile,
199 lockdep_rtnl_is_held());
631ab46b 200 /* Detach from net device */
bee31369 201 netif_carrier_off(tun->dev);
6e914fc7
JW
202 rcu_assign_pointer(tun->tfile, NULL);
203 if (tfile) {
204 rcu_assign_pointer(tfile->tun, NULL);
c70f1829 205
6e914fc7
JW
206 synchronize_net();
207 /* Drop read queue */
208 skb_queue_purge(&tfile->socket.sk->sk_receive_queue);
209 }
631ab46b
EB
210}
211
212static struct tun_struct *__tun_get(struct tun_file *tfile)
213{
6e914fc7 214 struct tun_struct *tun;
c70f1829 215
6e914fc7
JW
216 rcu_read_lock();
217 tun = rcu_dereference(tfile->tun);
218 if (tun)
219 dev_hold(tun->dev);
220 rcu_read_unlock();
c70f1829
EB
221
222 return tun;
631ab46b
EB
223}
224
225static struct tun_struct *tun_get(struct file *file)
226{
227 return __tun_get(file->private_data);
228}
229
230static void tun_put(struct tun_struct *tun)
231{
6e914fc7 232 dev_put(tun->dev);
631ab46b
EB
233}
234
6b8a66ee 235/* TAP filtering */
f271b2cc
MK
236static void addr_hash_set(u32 *mask, const u8 *addr)
237{
238 int n = ether_crc(ETH_ALEN, addr) >> 26;
239 mask[n >> 5] |= (1 << (n & 31));
240}
241
242static unsigned int addr_hash_test(const u32 *mask, const u8 *addr)
243{
244 int n = ether_crc(ETH_ALEN, addr) >> 26;
245 return mask[n >> 5] & (1 << (n & 31));
246}
247
248static int update_filter(struct tap_filter *filter, void __user *arg)
249{
250 struct { u8 u[ETH_ALEN]; } *addr;
251 struct tun_filter uf;
252 int err, alen, n, nexact;
253
254 if (copy_from_user(&uf, arg, sizeof(uf)))
255 return -EFAULT;
256
257 if (!uf.count) {
258 /* Disabled */
259 filter->count = 0;
260 return 0;
261 }
262
263 alen = ETH_ALEN * uf.count;
264 addr = kmalloc(alen, GFP_KERNEL);
265 if (!addr)
266 return -ENOMEM;
267
268 if (copy_from_user(addr, arg + sizeof(uf), alen)) {
269 err = -EFAULT;
270 goto done;
271 }
272
273 /* The filter is updated without holding any locks. Which is
274 * perfectly safe. We disable it first and in the worst
275 * case we'll accept a few undesired packets. */
276 filter->count = 0;
277 wmb();
278
279 /* Use first set of addresses as an exact filter */
280 for (n = 0; n < uf.count && n < FLT_EXACT_COUNT; n++)
281 memcpy(filter->addr[n], addr[n].u, ETH_ALEN);
282
283 nexact = n;
284
cfbf84fc
AW
285 /* Remaining multicast addresses are hashed,
286 * unicast will leave the filter disabled. */
f271b2cc 287 memset(filter->mask, 0, sizeof(filter->mask));
cfbf84fc
AW
288 for (; n < uf.count; n++) {
289 if (!is_multicast_ether_addr(addr[n].u)) {
290 err = 0; /* no filter */
291 goto done;
292 }
f271b2cc 293 addr_hash_set(filter->mask, addr[n].u);
cfbf84fc 294 }
f271b2cc
MK
295
296 /* For ALLMULTI just set the mask to all ones.
297 * This overrides the mask populated above. */
298 if ((uf.flags & TUN_FLT_ALLMULTI))
299 memset(filter->mask, ~0, sizeof(filter->mask));
300
301 /* Now enable the filter */
302 wmb();
303 filter->count = nexact;
304
305 /* Return the number of exact filters */
306 err = nexact;
307
308done:
309 kfree(addr);
310 return err;
311}
312
313/* Returns: 0 - drop, !=0 - accept */
314static int run_filter(struct tap_filter *filter, const struct sk_buff *skb)
315{
316 /* Cannot use eth_hdr(skb) here because skb_mac_hdr() is incorrect
317 * at this point. */
318 struct ethhdr *eh = (struct ethhdr *) skb->data;
319 int i;
320
321 /* Exact match */
322 for (i = 0; i < filter->count; i++)
2e42e474 323 if (ether_addr_equal(eh->h_dest, filter->addr[i]))
f271b2cc
MK
324 return 1;
325
326 /* Inexact match (multicast only) */
327 if (is_multicast_ether_addr(eh->h_dest))
328 return addr_hash_test(filter->mask, eh->h_dest);
329
330 return 0;
331}
332
333/*
334 * Checks whether the packet is accepted or not.
335 * Returns: 0 - drop, !=0 - accept
336 */
337static int check_filter(struct tap_filter *filter, const struct sk_buff *skb)
338{
339 if (!filter->count)
340 return 1;
341
342 return run_filter(filter, skb);
343}
344
1da177e4
LT
345/* Network device part of the driver */
346
7282d491 347static const struct ethtool_ops tun_ethtool_ops;
1da177e4 348
c70f1829
EB
349/* Net device detach from fd. */
350static void tun_net_uninit(struct net_device *dev)
351{
352 struct tun_struct *tun = netdev_priv(dev);
6e914fc7
JW
353 struct tun_file *tfile = rcu_dereference_protected(tun->tfile,
354 lockdep_rtnl_is_held());
c70f1829
EB
355
356 /* Inform the methods they need to stop using the dev.
357 */
358 if (tfile) {
54f968d6 359 wake_up_all(&tfile->wq.wait);
6e914fc7
JW
360 __tun_detach(tun);
361 synchronize_net();
c70f1829
EB
362 }
363}
364
1da177e4
LT
365/* Net device open. */
366static int tun_net_open(struct net_device *dev)
367{
368 netif_start_queue(dev);
369 return 0;
370}
371
372/* Net device close. */
373static int tun_net_close(struct net_device *dev)
374{
375 netif_stop_queue(dev);
376 return 0;
377}
378
379/* Net device start xmit */
424efe9c 380static netdev_tx_t tun_net_xmit(struct sk_buff *skb, struct net_device *dev)
1da177e4
LT
381{
382 struct tun_struct *tun = netdev_priv(dev);
6e914fc7 383 struct tun_file *tfile;
1da177e4 384
6e914fc7
JW
385 rcu_read_lock();
386 tfile = rcu_dereference(tun->tfile);
1da177e4 387 /* Drop packet if interface is not attached */
54f968d6 388 if (!tfile)
1da177e4
LT
389 goto drop;
390
6e914fc7
JW
391 tun_debug(KERN_INFO, tun, "tun_net_xmit %d\n", skb->len);
392
f271b2cc
MK
393 /* Drop if the filter does not like it.
394 * This is a noop if the filter is disabled.
395 * Filter can be enabled only for the TAP devices. */
396 if (!check_filter(&tun->txflt, skb))
397 goto drop;
398
54f968d6
JW
399 if (tfile->socket.sk->sk_filter &&
400 sk_filter(tfile->socket.sk, skb))
99405162
MT
401 goto drop;
402
54f968d6
JW
403 if (skb_queue_len(&tfile->socket.sk->sk_receive_queue)
404 >= dev->tx_queue_len) {
1da177e4
LT
405 if (!(tun->flags & TUN_ONE_QUEUE)) {
406 /* Normal queueing mode. */
407 /* Packet scheduler handles dropping of further packets. */
408 netif_stop_queue(dev);
409
410 /* We won't see all dropped packets individually, so overrun
411 * error is more appropriate. */
09f75cd7 412 dev->stats.tx_fifo_errors++;
1da177e4
LT
413 } else {
414 /* Single queue mode.
415 * Driver handles dropping of all packets itself. */
416 goto drop;
417 }
418 }
419
0110d6f2
MT
420 /* Orphan the skb - required as we might hang on to it
421 * for indefinite time. */
868eefeb
MT
422 if (unlikely(skb_orphan_frags(skb, GFP_ATOMIC)))
423 goto drop;
0110d6f2
MT
424 skb_orphan(skb);
425
f271b2cc 426 /* Enqueue packet */
54f968d6 427 skb_queue_tail(&tfile->socket.sk->sk_receive_queue, skb);
1da177e4
LT
428
429 /* Notify and wake up reader process */
54f968d6
JW
430 if (tfile->flags & TUN_FASYNC)
431 kill_fasync(&tfile->fasync, SIGIO, POLL_IN);
432 wake_up_interruptible_poll(&tfile->wq.wait, POLLIN |
05c2828c 433 POLLRDNORM | POLLRDBAND);
6e914fc7
JW
434
435 rcu_read_unlock();
6ed10654 436 return NETDEV_TX_OK;
1da177e4
LT
437
438drop:
09f75cd7 439 dev->stats.tx_dropped++;
1da177e4 440 kfree_skb(skb);
6e914fc7 441 rcu_read_unlock();
6ed10654 442 return NETDEV_TX_OK;
1da177e4
LT
443}
444
f271b2cc 445static void tun_net_mclist(struct net_device *dev)
1da177e4 446{
f271b2cc
MK
447 /*
448 * This callback is supposed to deal with mc filter in
449 * _rx_ path and has nothing to do with the _tx_ path.
450 * In rx path we always accept everything userspace gives us.
451 */
1da177e4
LT
452}
453
4885a504
ES
454#define MIN_MTU 68
455#define MAX_MTU 65535
456
457static int
458tun_net_change_mtu(struct net_device *dev, int new_mtu)
459{
460 if (new_mtu < MIN_MTU || new_mtu + dev->hard_header_len > MAX_MTU)
461 return -EINVAL;
462 dev->mtu = new_mtu;
463 return 0;
464}
465
c8f44aff
MM
466static netdev_features_t tun_net_fix_features(struct net_device *dev,
467 netdev_features_t features)
88255375
MM
468{
469 struct tun_struct *tun = netdev_priv(dev);
470
471 return (features & tun->set_features) | (features & ~TUN_USER_FEATURES);
472}
bebd097a
NH
473#ifdef CONFIG_NET_POLL_CONTROLLER
474static void tun_poll_controller(struct net_device *dev)
475{
476 /*
477 * Tun only receives frames when:
478 * 1) the char device endpoint gets data from user space
479 * 2) the tun socket gets a sendmsg call from user space
480 * Since both of those are syncronous operations, we are guaranteed
481 * never to have pending data when we poll for it
482 * so theres nothing to do here but return.
483 * We need this though so netpoll recognizes us as an interface that
484 * supports polling, which enables bridge devices in virt setups to
485 * still use netconsole
486 */
487 return;
488}
489#endif
758e43b7 490static const struct net_device_ops tun_netdev_ops = {
c70f1829 491 .ndo_uninit = tun_net_uninit,
758e43b7
SH
492 .ndo_open = tun_net_open,
493 .ndo_stop = tun_net_close,
00829823 494 .ndo_start_xmit = tun_net_xmit,
758e43b7 495 .ndo_change_mtu = tun_net_change_mtu,
88255375 496 .ndo_fix_features = tun_net_fix_features,
bebd097a
NH
497#ifdef CONFIG_NET_POLL_CONTROLLER
498 .ndo_poll_controller = tun_poll_controller,
499#endif
758e43b7
SH
500};
501
502static const struct net_device_ops tap_netdev_ops = {
c70f1829 503 .ndo_uninit = tun_net_uninit,
758e43b7
SH
504 .ndo_open = tun_net_open,
505 .ndo_stop = tun_net_close,
00829823 506 .ndo_start_xmit = tun_net_xmit,
758e43b7 507 .ndo_change_mtu = tun_net_change_mtu,
88255375 508 .ndo_fix_features = tun_net_fix_features,
afc4b13d 509 .ndo_set_rx_mode = tun_net_mclist,
758e43b7
SH
510 .ndo_set_mac_address = eth_mac_addr,
511 .ndo_validate_addr = eth_validate_addr,
bebd097a
NH
512#ifdef CONFIG_NET_POLL_CONTROLLER
513 .ndo_poll_controller = tun_poll_controller,
514#endif
758e43b7
SH
515};
516
1da177e4
LT
517/* Initialize net device. */
518static void tun_net_init(struct net_device *dev)
519{
520 struct tun_struct *tun = netdev_priv(dev);
6aa20a22 521
1da177e4
LT
522 switch (tun->flags & TUN_TYPE_MASK) {
523 case TUN_TUN_DEV:
758e43b7
SH
524 dev->netdev_ops = &tun_netdev_ops;
525
1da177e4
LT
526 /* Point-to-Point TUN Device */
527 dev->hard_header_len = 0;
528 dev->addr_len = 0;
529 dev->mtu = 1500;
530
531 /* Zero header length */
6aa20a22 532 dev->type = ARPHRD_NONE;
1da177e4
LT
533 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
534 dev->tx_queue_len = TUN_READQ_SIZE; /* We prefer our own queue length */
535 break;
536
537 case TUN_TAP_DEV:
7a0a9608 538 dev->netdev_ops = &tap_netdev_ops;
1da177e4 539 /* Ethernet TAP Device */
1da177e4 540 ether_setup(dev);
550fd08c 541 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
36226a8d 542
f2cedb63 543 eth_hw_addr_random(dev);
36226a8d 544
1da177e4
LT
545 dev->tx_queue_len = TUN_READQ_SIZE; /* We prefer our own queue length */
546 break;
547 }
548}
549
550/* Character device part */
551
552/* Poll */
553static unsigned int tun_chr_poll(struct file *file, poll_table * wait)
6aa20a22 554{
b2430de3
EB
555 struct tun_file *tfile = file->private_data;
556 struct tun_struct *tun = __tun_get(tfile);
3c8a9c63 557 struct sock *sk;
33dccbb0 558 unsigned int mask = 0;
1da177e4
LT
559
560 if (!tun)
eac9e902 561 return POLLERR;
1da177e4 562
54f968d6 563 sk = tfile->socket.sk;
3c8a9c63 564
6b8a66ee 565 tun_debug(KERN_INFO, tun, "tun_chr_poll\n");
1da177e4 566
54f968d6 567 poll_wait(file, &tfile->wq.wait, wait);
6aa20a22 568
89f56d1e 569 if (!skb_queue_empty(&sk->sk_receive_queue))
1da177e4
LT
570 mask |= POLLIN | POLLRDNORM;
571
33dccbb0
HX
572 if (sock_writeable(sk) ||
573 (!test_and_set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags) &&
574 sock_writeable(sk)))
575 mask |= POLLOUT | POLLWRNORM;
576
c70f1829
EB
577 if (tun->dev->reg_state != NETREG_REGISTERED)
578 mask = POLLERR;
579
631ab46b 580 tun_put(tun);
1da177e4
LT
581 return mask;
582}
583
f42157cb
RR
584/* prepad is the amount to reserve at front. len is length after that.
585 * linear is a hint as to how much to copy (usually headers). */
54f968d6 586static struct sk_buff *tun_alloc_skb(struct tun_file *tfile,
6f7c156c 587 size_t prepad, size_t len,
588 size_t linear, int noblock)
f42157cb 589{
54f968d6 590 struct sock *sk = tfile->socket.sk;
f42157cb 591 struct sk_buff *skb;
33dccbb0 592 int err;
f42157cb
RR
593
594 /* Under a page? Don't bother with paged skb. */
0eca93bc 595 if (prepad + len < PAGE_SIZE || !linear)
33dccbb0 596 linear = len;
f42157cb 597
33dccbb0
HX
598 skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
599 &err);
f42157cb 600 if (!skb)
33dccbb0 601 return ERR_PTR(err);
f42157cb
RR
602
603 skb_reserve(skb, prepad);
604 skb_put(skb, linear);
33dccbb0
HX
605 skb->data_len = len - linear;
606 skb->len += len - linear;
f42157cb
RR
607
608 return skb;
609}
610
0690899b
MT
611/* set skb frags from iovec, this can move to core network code for reuse */
612static int zerocopy_sg_from_iovec(struct sk_buff *skb, const struct iovec *from,
613 int offset, size_t count)
614{
615 int len = iov_length(from, count) - offset;
616 int copy = skb_headlen(skb);
617 int size, offset1 = 0;
618 int i = 0;
619
620 /* Skip over from offset */
621 while (count && (offset >= from->iov_len)) {
622 offset -= from->iov_len;
623 ++from;
624 --count;
625 }
626
627 /* copy up to skb headlen */
628 while (count && (copy > 0)) {
629 size = min_t(unsigned int, copy, from->iov_len - offset);
630 if (copy_from_user(skb->data + offset1, from->iov_base + offset,
631 size))
632 return -EFAULT;
633 if (copy > size) {
634 ++from;
635 --count;
636 offset = 0;
637 } else
638 offset += size;
639 copy -= size;
640 offset1 += size;
641 }
642
643 if (len == offset1)
644 return 0;
645
646 while (count--) {
647 struct page *page[MAX_SKB_FRAGS];
648 int num_pages;
649 unsigned long base;
650 unsigned long truesize;
651
652 len = from->iov_len - offset;
653 if (!len) {
654 offset = 0;
655 ++from;
656 continue;
657 }
658 base = (unsigned long)from->iov_base + offset;
659 size = ((base & ~PAGE_MASK) + len + ~PAGE_MASK) >> PAGE_SHIFT;
660 if (i + size > MAX_SKB_FRAGS)
661 return -EMSGSIZE;
662 num_pages = get_user_pages_fast(base, size, 0, &page[i]);
663 if (num_pages != size) {
664 for (i = 0; i < num_pages; i++)
665 put_page(page[i]);
666 return -EFAULT;
667 }
668 truesize = size * PAGE_SIZE;
669 skb->data_len += len;
670 skb->len += len;
671 skb->truesize += truesize;
672 atomic_add(truesize, &skb->sk->sk_wmem_alloc);
673 while (len) {
674 int off = base & ~PAGE_MASK;
675 int size = min_t(int, len, PAGE_SIZE - off);
676 __skb_fill_page_desc(skb, i, page[i], off, size);
677 skb_shinfo(skb)->nr_frags++;
678 /* increase sk_wmem_alloc */
679 base += size;
680 len -= size;
681 i++;
682 }
683 offset = 0;
684 ++from;
685 }
686 return 0;
687}
688
1da177e4 689/* Get packet from user space buffer */
54f968d6
JW
690static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,
691 void *msg_control, const struct iovec *iv,
692 size_t total_len, size_t count, int noblock)
1da177e4 693{
09640e63 694 struct tun_pi pi = { 0, cpu_to_be16(ETH_P_IP) };
1da177e4 695 struct sk_buff *skb;
0690899b 696 size_t len = total_len, align = NET_SKB_PAD;
f43798c2 697 struct virtio_net_hdr gso = { 0 };
6f26c9a7 698 int offset = 0;
0690899b
MT
699 int copylen;
700 bool zerocopy = false;
701 int err;
1da177e4
LT
702
703 if (!(tun->flags & TUN_NO_PI)) {
0690899b 704 if ((len -= sizeof(pi)) > total_len)
1da177e4
LT
705 return -EINVAL;
706
6f26c9a7 707 if (memcpy_fromiovecend((void *)&pi, iv, 0, sizeof(pi)))
1da177e4 708 return -EFAULT;
6f26c9a7 709 offset += sizeof(pi);
1da177e4
LT
710 }
711
f43798c2 712 if (tun->flags & TUN_VNET_HDR) {
0690899b 713 if ((len -= tun->vnet_hdr_sz) > total_len)
f43798c2
RR
714 return -EINVAL;
715
6f26c9a7 716 if (memcpy_fromiovecend((void *)&gso, iv, offset, sizeof(gso)))
f43798c2
RR
717 return -EFAULT;
718
4909122f
HX
719 if ((gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
720 gso.csum_start + gso.csum_offset + 2 > gso.hdr_len)
721 gso.hdr_len = gso.csum_start + gso.csum_offset + 2;
722
f43798c2
RR
723 if (gso.hdr_len > len)
724 return -EINVAL;
d9d52b51 725 offset += tun->vnet_hdr_sz;
f43798c2
RR
726 }
727
e01bf1c8 728 if ((tun->flags & TUN_TYPE_MASK) == TUN_TAP_DEV) {
a504b86e 729 align += NET_IP_ALIGN;
0eca93bc
HX
730 if (unlikely(len < ETH_HLEN ||
731 (gso.hdr_len && gso.hdr_len < ETH_HLEN)))
e01bf1c8
RR
732 return -EINVAL;
733 }
6aa20a22 734
0690899b
MT
735 if (msg_control)
736 zerocopy = true;
737
738 if (zerocopy) {
739 /* Userspace may produce vectors with count greater than
740 * MAX_SKB_FRAGS, so we need to linearize parts of the skb
741 * to let the rest of data to be fit in the frags.
742 */
743 if (count > MAX_SKB_FRAGS) {
744 copylen = iov_length(iv, count - MAX_SKB_FRAGS);
745 if (copylen < offset)
746 copylen = 0;
747 else
748 copylen -= offset;
749 } else
750 copylen = 0;
751 /* There are 256 bytes to be copied in skb, so there is enough
752 * room for skb expand head in case it is used.
753 * The rest of the buffer is mapped from userspace.
754 */
755 if (copylen < gso.hdr_len)
756 copylen = gso.hdr_len;
757 if (!copylen)
758 copylen = GOODCOPY_LEN;
759 } else
760 copylen = len;
761
54f968d6 762 skb = tun_alloc_skb(tfile, align, copylen, gso.hdr_len, noblock);
33dccbb0
HX
763 if (IS_ERR(skb)) {
764 if (PTR_ERR(skb) != -EAGAIN)
765 tun->dev->stats.rx_dropped++;
766 return PTR_ERR(skb);
1da177e4
LT
767 }
768
0690899b
MT
769 if (zerocopy)
770 err = zerocopy_sg_from_iovec(skb, iv, offset, count);
771 else
772 err = skb_copy_datagram_from_iovec(skb, 0, iv, offset, len);
773
774 if (err) {
09f75cd7 775 tun->dev->stats.rx_dropped++;
8f22757e 776 kfree_skb(skb);
1da177e4 777 return -EFAULT;
8f22757e 778 }
1da177e4 779
f43798c2
RR
780 if (gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
781 if (!skb_partial_csum_set(skb, gso.csum_start,
782 gso.csum_offset)) {
783 tun->dev->stats.rx_frame_errors++;
784 kfree_skb(skb);
785 return -EINVAL;
786 }
88255375 787 }
f43798c2 788
1da177e4
LT
789 switch (tun->flags & TUN_TYPE_MASK) {
790 case TUN_TUN_DEV:
f09f7ee2
AWC
791 if (tun->flags & TUN_NO_PI) {
792 switch (skb->data[0] & 0xf0) {
793 case 0x40:
794 pi.proto = htons(ETH_P_IP);
795 break;
796 case 0x60:
797 pi.proto = htons(ETH_P_IPV6);
798 break;
799 default:
800 tun->dev->stats.rx_dropped++;
801 kfree_skb(skb);
802 return -EINVAL;
803 }
804 }
805
459a98ed 806 skb_reset_mac_header(skb);
1da177e4 807 skb->protocol = pi.proto;
4c13eb66 808 skb->dev = tun->dev;
1da177e4
LT
809 break;
810 case TUN_TAP_DEV:
811 skb->protocol = eth_type_trans(skb, tun->dev);
812 break;
6403eab1 813 }
1da177e4 814
f43798c2
RR
815 if (gso.gso_type != VIRTIO_NET_HDR_GSO_NONE) {
816 pr_debug("GSO!\n");
817 switch (gso.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
818 case VIRTIO_NET_HDR_GSO_TCPV4:
819 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
820 break;
821 case VIRTIO_NET_HDR_GSO_TCPV6:
822 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
823 break;
e36aa25a
SS
824 case VIRTIO_NET_HDR_GSO_UDP:
825 skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
826 break;
f43798c2
RR
827 default:
828 tun->dev->stats.rx_frame_errors++;
829 kfree_skb(skb);
830 return -EINVAL;
831 }
832
833 if (gso.gso_type & VIRTIO_NET_HDR_GSO_ECN)
834 skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
835
836 skb_shinfo(skb)->gso_size = gso.gso_size;
837 if (skb_shinfo(skb)->gso_size == 0) {
838 tun->dev->stats.rx_frame_errors++;
839 kfree_skb(skb);
840 return -EINVAL;
841 }
842
843 /* Header must be checked, and gso_segs computed. */
844 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
845 skb_shinfo(skb)->gso_segs = 0;
846 }
6aa20a22 847
0690899b
MT
848 /* copy skb_ubuf_info for callback when skb has no error */
849 if (zerocopy) {
850 skb_shinfo(skb)->destructor_arg = msg_control;
851 skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY;
852 }
853
1da177e4 854 netif_rx_ni(skb);
6aa20a22 855
09f75cd7
JG
856 tun->dev->stats.rx_packets++;
857 tun->dev->stats.rx_bytes += len;
1da177e4 858
0690899b 859 return total_len;
6aa20a22 860}
1da177e4 861
ee0b3e67
BP
862static ssize_t tun_chr_aio_write(struct kiocb *iocb, const struct iovec *iv,
863 unsigned long count, loff_t pos)
1da177e4 864{
33dccbb0 865 struct file *file = iocb->ki_filp;
ab46d779 866 struct tun_struct *tun = tun_get(file);
54f968d6 867 struct tun_file *tfile = file->private_data;
631ab46b 868 ssize_t result;
1da177e4
LT
869
870 if (!tun)
871 return -EBADFD;
872
6b8a66ee 873 tun_debug(KERN_INFO, tun, "tun_chr_write %ld\n", count);
1da177e4 874
54f968d6
JW
875 result = tun_get_user(tun, tfile, NULL, iv, iov_length(iv, count),
876 count, file->f_flags & O_NONBLOCK);
631ab46b
EB
877
878 tun_put(tun);
879 return result;
1da177e4
LT
880}
881
1da177e4 882/* Put packet to the user space buffer */
6f7c156c 883static ssize_t tun_put_user(struct tun_struct *tun,
54f968d6 884 struct tun_file *tfile,
6f7c156c 885 struct sk_buff *skb,
886 const struct iovec *iv, int len)
1da177e4
LT
887{
888 struct tun_pi pi = { 0, skb->protocol };
889 ssize_t total = 0;
890
891 if (!(tun->flags & TUN_NO_PI)) {
892 if ((len -= sizeof(pi)) < 0)
893 return -EINVAL;
894
895 if (len < skb->len) {
896 /* Packet will be striped */
897 pi.flags |= TUN_PKT_STRIP;
898 }
6aa20a22 899
43b39dcd 900 if (memcpy_toiovecend(iv, (void *) &pi, 0, sizeof(pi)))
1da177e4
LT
901 return -EFAULT;
902 total += sizeof(pi);
6aa20a22 903 }
1da177e4 904
f43798c2
RR
905 if (tun->flags & TUN_VNET_HDR) {
906 struct virtio_net_hdr gso = { 0 }; /* no info leak */
d9d52b51 907 if ((len -= tun->vnet_hdr_sz) < 0)
f43798c2
RR
908 return -EINVAL;
909
910 if (skb_is_gso(skb)) {
911 struct skb_shared_info *sinfo = skb_shinfo(skb);
912
913 /* This is a hint as to how much should be linear. */
914 gso.hdr_len = skb_headlen(skb);
915 gso.gso_size = sinfo->gso_size;
916 if (sinfo->gso_type & SKB_GSO_TCPV4)
917 gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
918 else if (sinfo->gso_type & SKB_GSO_TCPV6)
919 gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
e36aa25a
SS
920 else if (sinfo->gso_type & SKB_GSO_UDP)
921 gso.gso_type = VIRTIO_NET_HDR_GSO_UDP;
ef3db4a5 922 else {
6b8a66ee 923 pr_err("unexpected GSO type: "
ef3db4a5
MT
924 "0x%x, gso_size %d, hdr_len %d\n",
925 sinfo->gso_type, gso.gso_size,
926 gso.hdr_len);
927 print_hex_dump(KERN_ERR, "tun: ",
928 DUMP_PREFIX_NONE,
929 16, 1, skb->head,
930 min((int)gso.hdr_len, 64), true);
931 WARN_ON_ONCE(1);
932 return -EINVAL;
933 }
f43798c2
RR
934 if (sinfo->gso_type & SKB_GSO_TCP_ECN)
935 gso.gso_type |= VIRTIO_NET_HDR_GSO_ECN;
936 } else
937 gso.gso_type = VIRTIO_NET_HDR_GSO_NONE;
938
939 if (skb->ip_summed == CHECKSUM_PARTIAL) {
940 gso.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
55508d60 941 gso.csum_start = skb_checksum_start_offset(skb);
f43798c2 942 gso.csum_offset = skb->csum_offset;
10a8d94a
JW
943 } else if (skb->ip_summed == CHECKSUM_UNNECESSARY) {
944 gso.flags = VIRTIO_NET_HDR_F_DATA_VALID;
f43798c2
RR
945 } /* else everything is zero */
946
43b39dcd
MT
947 if (unlikely(memcpy_toiovecend(iv, (void *)&gso, total,
948 sizeof(gso))))
f43798c2 949 return -EFAULT;
d9d52b51 950 total += tun->vnet_hdr_sz;
f43798c2
RR
951 }
952
1da177e4
LT
953 len = min_t(int, skb->len, len);
954
43b39dcd 955 skb_copy_datagram_const_iovec(skb, 0, iv, total, len);
05c2828c 956 total += skb->len;
1da177e4 957
09f75cd7
JG
958 tun->dev->stats.tx_packets++;
959 tun->dev->stats.tx_bytes += len;
1da177e4
LT
960
961 return total;
962}
963
54f968d6 964static ssize_t tun_do_read(struct tun_struct *tun, struct tun_file *tfile,
05c2828c
MT
965 struct kiocb *iocb, const struct iovec *iv,
966 ssize_t len, int noblock)
1da177e4 967{
1da177e4
LT
968 DECLARE_WAITQUEUE(wait, current);
969 struct sk_buff *skb;
05c2828c 970 ssize_t ret = 0;
1da177e4 971
6b8a66ee 972 tun_debug(KERN_INFO, tun, "tun_chr_read\n");
1da177e4 973
61a5ff15 974 if (unlikely(!noblock))
54f968d6 975 add_wait_queue(&tfile->wq.wait, &wait);
1da177e4 976 while (len) {
1da177e4
LT
977 current->state = TASK_INTERRUPTIBLE;
978
979 /* Read frames from the queue */
54f968d6 980 if (!(skb = skb_dequeue(&tfile->socket.sk->sk_receive_queue))) {
05c2828c 981 if (noblock) {
1da177e4
LT
982 ret = -EAGAIN;
983 break;
984 }
985 if (signal_pending(current)) {
986 ret = -ERESTARTSYS;
987 break;
988 }
c70f1829
EB
989 if (tun->dev->reg_state != NETREG_REGISTERED) {
990 ret = -EIO;
991 break;
992 }
1da177e4
LT
993
994 /* Nothing to read, let's sleep */
995 schedule();
996 continue;
997 }
998 netif_wake_queue(tun->dev);
999
54f968d6 1000 ret = tun_put_user(tun, tfile, skb, iv, len);
f271b2cc
MK
1001 kfree_skb(skb);
1002 break;
1da177e4
LT
1003 }
1004
1005 current->state = TASK_RUNNING;
61a5ff15 1006 if (unlikely(!noblock))
54f968d6 1007 remove_wait_queue(&tfile->wq.wait, &wait);
1da177e4 1008
05c2828c
MT
1009 return ret;
1010}
1011
1012static ssize_t tun_chr_aio_read(struct kiocb *iocb, const struct iovec *iv,
1013 unsigned long count, loff_t pos)
1014{
1015 struct file *file = iocb->ki_filp;
1016 struct tun_file *tfile = file->private_data;
1017 struct tun_struct *tun = __tun_get(tfile);
1018 ssize_t len, ret;
1019
1020 if (!tun)
1021 return -EBADFD;
1022 len = iov_length(iv, count);
1023 if (len < 0) {
1024 ret = -EINVAL;
1025 goto out;
1026 }
1027
54f968d6
JW
1028 ret = tun_do_read(tun, tfile, iocb, iv, len,
1029 file->f_flags & O_NONBLOCK);
05c2828c 1030 ret = min_t(ssize_t, ret, len);
631ab46b
EB
1031out:
1032 tun_put(tun);
1da177e4
LT
1033 return ret;
1034}
1035
1da177e4
LT
1036static void tun_setup(struct net_device *dev)
1037{
1038 struct tun_struct *tun = netdev_priv(dev);
1039
0625c883
EB
1040 tun->owner = INVALID_UID;
1041 tun->group = INVALID_GID;
1da177e4 1042
1da177e4 1043 dev->ethtool_ops = &tun_ethtool_ops;
54f968d6 1044 dev->destructor = free_netdev;
1da177e4
LT
1045}
1046
f019a7a5
EB
1047/* Trivial set of netlink ops to allow deleting tun or tap
1048 * device with netlink.
1049 */
1050static int tun_validate(struct nlattr *tb[], struct nlattr *data[])
1051{
1052 return -EINVAL;
1053}
1054
1055static struct rtnl_link_ops tun_link_ops __read_mostly = {
1056 .kind = DRV_NAME,
1057 .priv_size = sizeof(struct tun_struct),
1058 .setup = tun_setup,
1059 .validate = tun_validate,
1060};
1061
33dccbb0
HX
1062static void tun_sock_write_space(struct sock *sk)
1063{
54f968d6 1064 struct tun_file *tfile;
43815482 1065 wait_queue_head_t *wqueue;
33dccbb0
HX
1066
1067 if (!sock_writeable(sk))
1068 return;
1069
33dccbb0
HX
1070 if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags))
1071 return;
1072
43815482
ED
1073 wqueue = sk_sleep(sk);
1074 if (wqueue && waitqueue_active(wqueue))
1075 wake_up_interruptible_sync_poll(wqueue, POLLOUT |
05c2828c 1076 POLLWRNORM | POLLWRBAND);
c722c625 1077
54f968d6
JW
1078 tfile = container_of(sk, struct tun_file, sk);
1079 kill_fasync(&tfile->fasync, SIGIO, POLL_OUT);
33dccbb0
HX
1080}
1081
05c2828c
MT
1082static int tun_sendmsg(struct kiocb *iocb, struct socket *sock,
1083 struct msghdr *m, size_t total_len)
1084{
54f968d6
JW
1085 int ret;
1086 struct tun_file *tfile = container_of(sock, struct tun_file, socket);
1087 struct tun_struct *tun = __tun_get(tfile);
1088
1089 if (!tun)
1090 return -EBADFD;
54f968d6
JW
1091 ret = tun_get_user(tun, tfile, m->msg_control, m->msg_iov, total_len,
1092 m->msg_iovlen, m->msg_flags & MSG_DONTWAIT);
1093 tun_put(tun);
1094 return ret;
05c2828c
MT
1095}
1096
54f968d6 1097
05c2828c
MT
1098static int tun_recvmsg(struct kiocb *iocb, struct socket *sock,
1099 struct msghdr *m, size_t total_len,
1100 int flags)
1101{
54f968d6
JW
1102 struct tun_file *tfile = container_of(sock, struct tun_file, socket);
1103 struct tun_struct *tun = __tun_get(tfile);
05c2828c 1104 int ret;
54f968d6
JW
1105
1106 if (!tun)
1107 return -EBADFD;
1108
05c2828c
MT
1109 if (flags & ~(MSG_DONTWAIT|MSG_TRUNC))
1110 return -EINVAL;
54f968d6 1111 ret = tun_do_read(tun, tfile, iocb, m->msg_iov, total_len,
05c2828c
MT
1112 flags & MSG_DONTWAIT);
1113 if (ret > total_len) {
1114 m->msg_flags |= MSG_TRUNC;
1115 ret = flags & MSG_TRUNC ? ret : total_len;
1116 }
54f968d6 1117 tun_put(tun);
05c2828c
MT
1118 return ret;
1119}
1120
1ab5ecb9
SK
1121static int tun_release(struct socket *sock)
1122{
1123 if (sock->sk)
1124 sock_put(sock->sk);
1125 return 0;
1126}
1127
05c2828c
MT
1128/* Ops structure to mimic raw sockets with tun */
1129static const struct proto_ops tun_socket_ops = {
1130 .sendmsg = tun_sendmsg,
1131 .recvmsg = tun_recvmsg,
1ab5ecb9 1132 .release = tun_release,
05c2828c
MT
1133};
1134
33dccbb0
HX
1135static struct proto tun_proto = {
1136 .name = "tun",
1137 .owner = THIS_MODULE,
54f968d6 1138 .obj_size = sizeof(struct tun_file),
33dccbb0 1139};
f019a7a5 1140
980c9e8c
DW
1141static int tun_flags(struct tun_struct *tun)
1142{
1143 int flags = 0;
1144
1145 if (tun->flags & TUN_TUN_DEV)
1146 flags |= IFF_TUN;
1147 else
1148 flags |= IFF_TAP;
1149
1150 if (tun->flags & TUN_NO_PI)
1151 flags |= IFF_NO_PI;
1152
1153 if (tun->flags & TUN_ONE_QUEUE)
1154 flags |= IFF_ONE_QUEUE;
1155
1156 if (tun->flags & TUN_VNET_HDR)
1157 flags |= IFF_VNET_HDR;
1158
1159 return flags;
1160}
1161
1162static ssize_t tun_show_flags(struct device *dev, struct device_attribute *attr,
1163 char *buf)
1164{
1165 struct tun_struct *tun = netdev_priv(to_net_dev(dev));
1166 return sprintf(buf, "0x%x\n", tun_flags(tun));
1167}
1168
1169static ssize_t tun_show_owner(struct device *dev, struct device_attribute *attr,
1170 char *buf)
1171{
1172 struct tun_struct *tun = netdev_priv(to_net_dev(dev));
0625c883
EB
1173 return uid_valid(tun->owner)?
1174 sprintf(buf, "%u\n",
1175 from_kuid_munged(current_user_ns(), tun->owner)):
1176 sprintf(buf, "-1\n");
980c9e8c
DW
1177}
1178
1179static ssize_t tun_show_group(struct device *dev, struct device_attribute *attr,
1180 char *buf)
1181{
1182 struct tun_struct *tun = netdev_priv(to_net_dev(dev));
0625c883
EB
1183 return gid_valid(tun->group) ?
1184 sprintf(buf, "%u\n",
1185 from_kgid_munged(current_user_ns(), tun->group)):
1186 sprintf(buf, "-1\n");
980c9e8c
DW
1187}
1188
1189static DEVICE_ATTR(tun_flags, 0444, tun_show_flags, NULL);
1190static DEVICE_ATTR(owner, 0444, tun_show_owner, NULL);
1191static DEVICE_ATTR(group, 0444, tun_show_group, NULL);
1192
d647a591 1193static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
1da177e4
LT
1194{
1195 struct tun_struct *tun;
54f968d6 1196 struct tun_file *tfile = file->private_data;
1da177e4
LT
1197 struct net_device *dev;
1198 int err;
1199
74a3e5a7
EB
1200 dev = __dev_get_by_name(net, ifr->ifr_name);
1201 if (dev) {
2b980dbd
PM
1202 const struct cred *cred = current_cred();
1203
f85ba780
DW
1204 if (ifr->ifr_flags & IFF_TUN_EXCL)
1205 return -EBUSY;
74a3e5a7
EB
1206 if ((ifr->ifr_flags & IFF_TUN) && dev->netdev_ops == &tun_netdev_ops)
1207 tun = netdev_priv(dev);
1208 else if ((ifr->ifr_flags & IFF_TAP) && dev->netdev_ops == &tap_netdev_ops)
1209 tun = netdev_priv(dev);
1210 else
1211 return -EINVAL;
1212
0625c883
EB
1213 if (((uid_valid(tun->owner) && !uid_eq(cred->euid, tun->owner)) ||
1214 (gid_valid(tun->group) && !in_egroup_p(tun->group))) &&
2b980dbd
PM
1215 !capable(CAP_NET_ADMIN))
1216 return -EPERM;
54f968d6 1217 err = security_tun_dev_attach(tfile->socket.sk);
2b980dbd
PM
1218 if (err < 0)
1219 return err;
1220
a7385ba2
EB
1221 err = tun_attach(tun, file);
1222 if (err < 0)
1223 return err;
6aa20a22 1224 }
1da177e4
LT
1225 else {
1226 char *name;
1227 unsigned long flags = 0;
1228
ca6bb5d7
DW
1229 if (!capable(CAP_NET_ADMIN))
1230 return -EPERM;
2b980dbd
PM
1231 err = security_tun_dev_create();
1232 if (err < 0)
1233 return err;
ca6bb5d7 1234
1da177e4
LT
1235 /* Set dev type */
1236 if (ifr->ifr_flags & IFF_TUN) {
1237 /* TUN device */
1238 flags |= TUN_TUN_DEV;
1239 name = "tun%d";
1240 } else if (ifr->ifr_flags & IFF_TAP) {
1241 /* TAP device */
1242 flags |= TUN_TAP_DEV;
1243 name = "tap%d";
6aa20a22 1244 } else
36989b90 1245 return -EINVAL;
6aa20a22 1246
1da177e4
LT
1247 if (*ifr->ifr_name)
1248 name = ifr->ifr_name;
1249
1250 dev = alloc_netdev(sizeof(struct tun_struct), name,
1251 tun_setup);
1252 if (!dev)
1253 return -ENOMEM;
1254
fc54c658 1255 dev_net_set(dev, net);
f019a7a5 1256 dev->rtnl_link_ops = &tun_link_ops;
758e43b7 1257
1da177e4
LT
1258 tun = netdev_priv(dev);
1259 tun->dev = dev;
1260 tun->flags = flags;
f271b2cc 1261 tun->txflt.count = 0;
d9d52b51 1262 tun->vnet_hdr_sz = sizeof(struct virtio_net_hdr);
33dccbb0 1263
54f968d6
JW
1264 tun->filter_attached = false;
1265 tun->sndbuf = tfile->socket.sk->sk_sndbuf;
33dccbb0 1266
54f968d6 1267 security_tun_dev_post_create(&tfile->sk);
2b980dbd 1268
1da177e4
LT
1269 tun_net_init(dev);
1270
88255375
MM
1271 dev->hw_features = NETIF_F_SG | NETIF_F_FRAGLIST |
1272 TUN_USER_FEATURES;
1273 dev->features = dev->hw_features;
1274
1da177e4
LT
1275 err = register_netdevice(tun->dev);
1276 if (err < 0)
54f968d6 1277 goto err_free_dev;
9c3fea6a 1278
980c9e8c
DW
1279 if (device_create_file(&tun->dev->dev, &dev_attr_tun_flags) ||
1280 device_create_file(&tun->dev->dev, &dev_attr_owner) ||
1281 device_create_file(&tun->dev->dev, &dev_attr_group))
6b8a66ee 1282 pr_err("Failed to create tun sysfs files\n");
980c9e8c 1283
a7385ba2
EB
1284 err = tun_attach(tun, file);
1285 if (err < 0)
9c3fea6a 1286 goto failed;
1da177e4
LT
1287 }
1288
6b8a66ee 1289 tun_debug(KERN_INFO, tun, "tun_set_iff\n");
1da177e4
LT
1290
1291 if (ifr->ifr_flags & IFF_NO_PI)
1292 tun->flags |= TUN_NO_PI;
a26af1e0
NF
1293 else
1294 tun->flags &= ~TUN_NO_PI;
1da177e4
LT
1295
1296 if (ifr->ifr_flags & IFF_ONE_QUEUE)
1297 tun->flags |= TUN_ONE_QUEUE;
a26af1e0
NF
1298 else
1299 tun->flags &= ~TUN_ONE_QUEUE;
1da177e4 1300
f43798c2
RR
1301 if (ifr->ifr_flags & IFF_VNET_HDR)
1302 tun->flags |= TUN_VNET_HDR;
1303 else
1304 tun->flags &= ~TUN_VNET_HDR;
1305
e35259a9
MK
1306 /* Make sure persistent devices do not get stuck in
1307 * xoff state.
1308 */
1309 if (netif_running(tun->dev))
1310 netif_wake_queue(tun->dev);
1311
1da177e4
LT
1312 strcpy(ifr->ifr_name, tun->dev->name);
1313 return 0;
1314
1315 err_free_dev:
1316 free_netdev(dev);
1317 failed:
1318 return err;
1319}
1320
876bfd4d
HX
1321static int tun_get_iff(struct net *net, struct tun_struct *tun,
1322 struct ifreq *ifr)
e3b99556 1323{
6b8a66ee 1324 tun_debug(KERN_INFO, tun, "tun_get_iff\n");
e3b99556
MM
1325
1326 strcpy(ifr->ifr_name, tun->dev->name);
1327
980c9e8c 1328 ifr->ifr_flags = tun_flags(tun);
e3b99556
MM
1329
1330 return 0;
1331}
1332
5228ddc9
RR
1333/* This is like a cut-down ethtool ops, except done via tun fd so no
1334 * privs required. */
88255375 1335static int set_offload(struct tun_struct *tun, unsigned long arg)
5228ddc9 1336{
c8f44aff 1337 netdev_features_t features = 0;
5228ddc9
RR
1338
1339 if (arg & TUN_F_CSUM) {
88255375 1340 features |= NETIF_F_HW_CSUM;
5228ddc9
RR
1341 arg &= ~TUN_F_CSUM;
1342
1343 if (arg & (TUN_F_TSO4|TUN_F_TSO6)) {
1344 if (arg & TUN_F_TSO_ECN) {
1345 features |= NETIF_F_TSO_ECN;
1346 arg &= ~TUN_F_TSO_ECN;
1347 }
1348 if (arg & TUN_F_TSO4)
1349 features |= NETIF_F_TSO;
1350 if (arg & TUN_F_TSO6)
1351 features |= NETIF_F_TSO6;
1352 arg &= ~(TUN_F_TSO4|TUN_F_TSO6);
1353 }
e36aa25a
SS
1354
1355 if (arg & TUN_F_UFO) {
1356 features |= NETIF_F_UFO;
1357 arg &= ~TUN_F_UFO;
1358 }
5228ddc9
RR
1359 }
1360
1361 /* This gives the user a way to test for new features in future by
1362 * trying to set them. */
1363 if (arg)
1364 return -EINVAL;
1365
88255375
MM
1366 tun->set_features = features;
1367 netdev_update_features(tun->dev);
5228ddc9
RR
1368
1369 return 0;
1370}
1371
50857e2a
AB
1372static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
1373 unsigned long arg, int ifreq_len)
1da177e4 1374{
36b50bab 1375 struct tun_file *tfile = file->private_data;
631ab46b 1376 struct tun_struct *tun;
1da177e4
LT
1377 void __user* argp = (void __user*)arg;
1378 struct ifreq ifr;
0625c883
EB
1379 kuid_t owner;
1380 kgid_t group;
33dccbb0 1381 int sndbuf;
d9d52b51 1382 int vnet_hdr_sz;
f271b2cc 1383 int ret;
1da177e4 1384
a117dacd 1385 if (cmd == TUNSETIFF || _IOC_TYPE(cmd) == 0x89) {
50857e2a 1386 if (copy_from_user(&ifr, argp, ifreq_len))
1da177e4 1387 return -EFAULT;
8bbb1813 1388 } else {
a117dacd 1389 memset(&ifr, 0, sizeof(ifr));
8bbb1813 1390 }
631ab46b
EB
1391 if (cmd == TUNGETFEATURES) {
1392 /* Currently this just means: "what IFF flags are valid?".
1393 * This is needed because we never checked for invalid flags on
1394 * TUNSETIFF. */
1395 return put_user(IFF_TUN | IFF_TAP | IFF_NO_PI | IFF_ONE_QUEUE |
1396 IFF_VNET_HDR,
1397 (unsigned int __user*)argp);
1398 }
1399
876bfd4d
HX
1400 rtnl_lock();
1401
36b50bab 1402 tun = __tun_get(tfile);
1da177e4 1403 if (cmd == TUNSETIFF && !tun) {
1da177e4
LT
1404 ifr.ifr_name[IFNAMSIZ-1] = '\0';
1405
876bfd4d 1406 ret = tun_set_iff(tfile->net, file, &ifr);
1da177e4 1407
876bfd4d
HX
1408 if (ret)
1409 goto unlock;
1da177e4 1410
50857e2a 1411 if (copy_to_user(argp, &ifr, ifreq_len))
876bfd4d
HX
1412 ret = -EFAULT;
1413 goto unlock;
1da177e4
LT
1414 }
1415
876bfd4d 1416 ret = -EBADFD;
1da177e4 1417 if (!tun)
876bfd4d 1418 goto unlock;
1da177e4 1419
1e588338 1420 tun_debug(KERN_INFO, tun, "tun_chr_ioctl cmd %u\n", cmd);
1da177e4 1421
631ab46b 1422 ret = 0;
1da177e4 1423 switch (cmd) {
e3b99556 1424 case TUNGETIFF:
876bfd4d 1425 ret = tun_get_iff(current->nsproxy->net_ns, tun, &ifr);
e3b99556 1426 if (ret)
631ab46b 1427 break;
e3b99556 1428
50857e2a 1429 if (copy_to_user(argp, &ifr, ifreq_len))
631ab46b 1430 ret = -EFAULT;
e3b99556
MM
1431 break;
1432
1da177e4
LT
1433 case TUNSETNOCSUM:
1434 /* Disable/Enable checksum */
1da177e4 1435
88255375
MM
1436 /* [unimplemented] */
1437 tun_debug(KERN_INFO, tun, "ignored: set checksum %s\n",
6b8a66ee 1438 arg ? "disabled" : "enabled");
1da177e4
LT
1439 break;
1440
1441 case TUNSETPERSIST:
54f968d6
JW
1442 /* Disable/Enable persist mode. Keep an extra reference to the
1443 * module to prevent the module being unprobed.
1444 */
1445 if (arg) {
1da177e4 1446 tun->flags |= TUN_PERSIST;
54f968d6
JW
1447 __module_get(THIS_MODULE);
1448 } else {
1da177e4 1449 tun->flags &= ~TUN_PERSIST;
54f968d6
JW
1450 module_put(THIS_MODULE);
1451 }
1da177e4 1452
6b8a66ee
JP
1453 tun_debug(KERN_INFO, tun, "persist %s\n",
1454 arg ? "enabled" : "disabled");
1da177e4
LT
1455 break;
1456
1457 case TUNSETOWNER:
1458 /* Set owner of the device */
0625c883
EB
1459 owner = make_kuid(current_user_ns(), arg);
1460 if (!uid_valid(owner)) {
1461 ret = -EINVAL;
1462 break;
1463 }
1464 tun->owner = owner;
1e588338 1465 tun_debug(KERN_INFO, tun, "owner set to %u\n",
0625c883 1466 from_kuid(&init_user_ns, tun->owner));
1da177e4
LT
1467 break;
1468
8c644623
GG
1469 case TUNSETGROUP:
1470 /* Set group of the device */
0625c883
EB
1471 group = make_kgid(current_user_ns(), arg);
1472 if (!gid_valid(group)) {
1473 ret = -EINVAL;
1474 break;
1475 }
1476 tun->group = group;
1e588338 1477 tun_debug(KERN_INFO, tun, "group set to %u\n",
0625c883 1478 from_kgid(&init_user_ns, tun->group));
8c644623
GG
1479 break;
1480
ff4cc3ac
MK
1481 case TUNSETLINK:
1482 /* Only allow setting the type when the interface is down */
1483 if (tun->dev->flags & IFF_UP) {
6b8a66ee
JP
1484 tun_debug(KERN_INFO, tun,
1485 "Linktype set failed because interface is up\n");
48abfe05 1486 ret = -EBUSY;
ff4cc3ac
MK
1487 } else {
1488 tun->dev->type = (int) arg;
6b8a66ee
JP
1489 tun_debug(KERN_INFO, tun, "linktype set to %d\n",
1490 tun->dev->type);
48abfe05 1491 ret = 0;
ff4cc3ac 1492 }
631ab46b 1493 break;
ff4cc3ac 1494
1da177e4
LT
1495#ifdef TUN_DEBUG
1496 case TUNSETDEBUG:
1497 tun->debug = arg;
1498 break;
1499#endif
5228ddc9 1500 case TUNSETOFFLOAD:
88255375 1501 ret = set_offload(tun, arg);
631ab46b 1502 break;
5228ddc9 1503
f271b2cc
MK
1504 case TUNSETTXFILTER:
1505 /* Can be set only for TAPs */
631ab46b 1506 ret = -EINVAL;
f271b2cc 1507 if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
631ab46b 1508 break;
c0e5a8c2 1509 ret = update_filter(&tun->txflt, (void __user *)arg);
631ab46b 1510 break;
1da177e4
LT
1511
1512 case SIOCGIFHWADDR:
b595076a 1513 /* Get hw address */
f271b2cc
MK
1514 memcpy(ifr.ifr_hwaddr.sa_data, tun->dev->dev_addr, ETH_ALEN);
1515 ifr.ifr_hwaddr.sa_family = tun->dev->type;
50857e2a 1516 if (copy_to_user(argp, &ifr, ifreq_len))
631ab46b
EB
1517 ret = -EFAULT;
1518 break;
1da177e4
LT
1519
1520 case SIOCSIFHWADDR:
f271b2cc 1521 /* Set hw address */
6b8a66ee
JP
1522 tun_debug(KERN_DEBUG, tun, "set hw address: %pM\n",
1523 ifr.ifr_hwaddr.sa_data);
40102371 1524
40102371 1525 ret = dev_set_mac_address(tun->dev, &ifr.ifr_hwaddr);
631ab46b 1526 break;
33dccbb0
HX
1527
1528 case TUNGETSNDBUF:
54f968d6 1529 sndbuf = tfile->socket.sk->sk_sndbuf;
33dccbb0
HX
1530 if (copy_to_user(argp, &sndbuf, sizeof(sndbuf)))
1531 ret = -EFAULT;
1532 break;
1533
1534 case TUNSETSNDBUF:
1535 if (copy_from_user(&sndbuf, argp, sizeof(sndbuf))) {
1536 ret = -EFAULT;
1537 break;
1538 }
1539
54f968d6 1540 tun->sndbuf = tfile->socket.sk->sk_sndbuf = sndbuf;
33dccbb0
HX
1541 break;
1542
d9d52b51
MT
1543 case TUNGETVNETHDRSZ:
1544 vnet_hdr_sz = tun->vnet_hdr_sz;
1545 if (copy_to_user(argp, &vnet_hdr_sz, sizeof(vnet_hdr_sz)))
1546 ret = -EFAULT;
1547 break;
1548
1549 case TUNSETVNETHDRSZ:
1550 if (copy_from_user(&vnet_hdr_sz, argp, sizeof(vnet_hdr_sz))) {
1551 ret = -EFAULT;
1552 break;
1553 }
1554 if (vnet_hdr_sz < (int)sizeof(struct virtio_net_hdr)) {
1555 ret = -EINVAL;
1556 break;
1557 }
1558
1559 tun->vnet_hdr_sz = vnet_hdr_sz;
1560 break;
1561
99405162
MT
1562 case TUNATTACHFILTER:
1563 /* Can be set only for TAPs */
1564 ret = -EINVAL;
1565 if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
1566 break;
1567 ret = -EFAULT;
54f968d6 1568 if (copy_from_user(&tun->fprog, argp, sizeof(tun->fprog)))
99405162
MT
1569 break;
1570
54f968d6
JW
1571 ret = sk_attach_filter(&tun->fprog, tfile->socket.sk);
1572 if (!ret)
1573 tun->filter_attached = true;
99405162
MT
1574 break;
1575
1576 case TUNDETACHFILTER:
1577 /* Can be set only for TAPs */
1578 ret = -EINVAL;
1579 if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
1580 break;
54f968d6
JW
1581 ret = sk_detach_filter(tfile->socket.sk);
1582 if (!ret)
1583 tun->filter_attached = false;
99405162
MT
1584 break;
1585
1da177e4 1586 default:
631ab46b
EB
1587 ret = -EINVAL;
1588 break;
ee289b64 1589 }
1da177e4 1590
876bfd4d
HX
1591unlock:
1592 rtnl_unlock();
1593 if (tun)
1594 tun_put(tun);
631ab46b 1595 return ret;
1da177e4
LT
1596}
1597
50857e2a
AB
1598static long tun_chr_ioctl(struct file *file,
1599 unsigned int cmd, unsigned long arg)
1600{
1601 return __tun_chr_ioctl(file, cmd, arg, sizeof (struct ifreq));
1602}
1603
1604#ifdef CONFIG_COMPAT
1605static long tun_chr_compat_ioctl(struct file *file,
1606 unsigned int cmd, unsigned long arg)
1607{
1608 switch (cmd) {
1609 case TUNSETIFF:
1610 case TUNGETIFF:
1611 case TUNSETTXFILTER:
1612 case TUNGETSNDBUF:
1613 case TUNSETSNDBUF:
1614 case SIOCGIFHWADDR:
1615 case SIOCSIFHWADDR:
1616 arg = (unsigned long)compat_ptr(arg);
1617 break;
1618 default:
1619 arg = (compat_ulong_t)arg;
1620 break;
1621 }
1622
1623 /*
1624 * compat_ifreq is shorter than ifreq, so we must not access beyond
1625 * the end of that structure. All fields that are used in this
1626 * driver are compatible though, we don't need to convert the
1627 * contents.
1628 */
1629 return __tun_chr_ioctl(file, cmd, arg, sizeof(struct compat_ifreq));
1630}
1631#endif /* CONFIG_COMPAT */
1632
1da177e4
LT
1633static int tun_chr_fasync(int fd, struct file *file, int on)
1634{
54f968d6 1635 struct tun_file *tfile = file->private_data;
1da177e4
LT
1636 int ret;
1637
54f968d6 1638 if ((ret = fasync_helper(fd, file, on, &tfile->fasync)) < 0)
9d319522 1639 goto out;
6aa20a22 1640
1da177e4 1641 if (on) {
609d7fa9 1642 ret = __f_setown(file, task_pid(current), PIDTYPE_PID, 0);
1da177e4 1643 if (ret)
9d319522 1644 goto out;
54f968d6 1645 tfile->flags |= TUN_FASYNC;
6aa20a22 1646 } else
54f968d6 1647 tfile->flags &= ~TUN_FASYNC;
9d319522
JC
1648 ret = 0;
1649out:
9d319522 1650 return ret;
1da177e4
LT
1651}
1652
1653static int tun_chr_open(struct inode *inode, struct file * file)
1654{
631ab46b 1655 struct tun_file *tfile;
deed49fb 1656
6b8a66ee 1657 DBG1(KERN_INFO, "tunX: tun_chr_open\n");
631ab46b 1658
54f968d6
JW
1659 tfile = (struct tun_file *)sk_alloc(&init_net, AF_UNSPEC, GFP_KERNEL,
1660 &tun_proto);
631ab46b
EB
1661 if (!tfile)
1662 return -ENOMEM;
6e914fc7 1663 rcu_assign_pointer(tfile->tun, NULL);
36b50bab 1664 tfile->net = get_net(current->nsproxy->net_ns);
54f968d6
JW
1665 tfile->flags = 0;
1666
1667 rcu_assign_pointer(tfile->socket.wq, &tfile->wq);
1668 init_waitqueue_head(&tfile->wq.wait);
1669
1670 tfile->socket.file = file;
1671 tfile->socket.ops = &tun_socket_ops;
1672
1673 sock_init_data(&tfile->socket, &tfile->sk);
1674 sk_change_net(&tfile->sk, tfile->net);
1675
1676 tfile->sk.sk_write_space = tun_sock_write_space;
1677 tfile->sk.sk_sndbuf = INT_MAX;
1678
631ab46b 1679 file->private_data = tfile;
54f968d6
JW
1680 set_bit(SOCK_EXTERNALLY_ALLOCATED, &tfile->socket.flags);
1681
1da177e4
LT
1682 return 0;
1683}
1684
1685static int tun_chr_close(struct inode *inode, struct file *file)
1686{
631ab46b 1687 struct tun_file *tfile = file->private_data;
f0a4d0e5 1688 struct tun_struct *tun;
54f968d6 1689 struct net *net = tfile->net;
1da177e4 1690
6e914fc7
JW
1691 rtnl_lock();
1692
1693 tun = rcu_dereference_protected(tfile->tun, lockdep_rtnl_is_held());
631ab46b 1694 if (tun) {
d23e4365
HX
1695 struct net_device *dev = tun->dev;
1696
6b8a66ee 1697 tun_debug(KERN_INFO, tun, "tun_chr_close\n");
1da177e4 1698
631ab46b 1699 __tun_detach(tun);
1da177e4 1700
6e914fc7
JW
1701 synchronize_net();
1702
3ad2f3fb 1703 /* If desirable, unregister the netdevice. */
d23e4365 1704 if (!(tun->flags & TUN_PERSIST)) {
d23e4365
HX
1705 if (dev->reg_state == NETREG_REGISTERED)
1706 unregister_netdevice(dev);
d23e4365 1707 }
1da177e4 1708
54f968d6
JW
1709 /* drop the reference that netdevice holds */
1710 sock_put(&tfile->sk);
1711 }
9c3fea6a 1712
6e914fc7
JW
1713 rtnl_unlock();
1714
54f968d6
JW
1715 /* drop the reference that file holds */
1716 BUG_ON(!test_bit(SOCK_EXTERNALLY_ALLOCATED,
1717 &tfile->socket.flags));
1718 sk_release_kernel(&tfile->sk);
1719 put_net(net);
1da177e4
LT
1720
1721 return 0;
1722}
1723
d54b1fdb 1724static const struct file_operations tun_fops = {
6aa20a22 1725 .owner = THIS_MODULE,
1da177e4 1726 .llseek = no_llseek,
ee0b3e67
BP
1727 .read = do_sync_read,
1728 .aio_read = tun_chr_aio_read,
1729 .write = do_sync_write,
1730 .aio_write = tun_chr_aio_write,
1da177e4 1731 .poll = tun_chr_poll,
50857e2a
AB
1732 .unlocked_ioctl = tun_chr_ioctl,
1733#ifdef CONFIG_COMPAT
1734 .compat_ioctl = tun_chr_compat_ioctl,
1735#endif
1da177e4
LT
1736 .open = tun_chr_open,
1737 .release = tun_chr_close,
6aa20a22 1738 .fasync = tun_chr_fasync
1da177e4
LT
1739};
1740
1741static struct miscdevice tun_miscdev = {
1742 .minor = TUN_MINOR,
1743 .name = "tun",
e454cea2 1744 .nodename = "net/tun",
1da177e4 1745 .fops = &tun_fops,
1da177e4
LT
1746};
1747
1748/* ethtool interface */
1749
1750static int tun_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1751{
1752 cmd->supported = 0;
1753 cmd->advertising = 0;
70739497 1754 ethtool_cmd_speed_set(cmd, SPEED_10);
1da177e4
LT
1755 cmd->duplex = DUPLEX_FULL;
1756 cmd->port = PORT_TP;
1757 cmd->phy_address = 0;
1758 cmd->transceiver = XCVR_INTERNAL;
1759 cmd->autoneg = AUTONEG_DISABLE;
1760 cmd->maxtxpkt = 0;
1761 cmd->maxrxpkt = 0;
1762 return 0;
1763}
1764
1765static void tun_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
1766{
1767 struct tun_struct *tun = netdev_priv(dev);
1768
33a5ba14
RJ
1769 strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
1770 strlcpy(info->version, DRV_VERSION, sizeof(info->version));
1da177e4
LT
1771
1772 switch (tun->flags & TUN_TYPE_MASK) {
1773 case TUN_TUN_DEV:
33a5ba14 1774 strlcpy(info->bus_info, "tun", sizeof(info->bus_info));
1da177e4
LT
1775 break;
1776 case TUN_TAP_DEV:
33a5ba14 1777 strlcpy(info->bus_info, "tap", sizeof(info->bus_info));
1da177e4
LT
1778 break;
1779 }
1780}
1781
1782static u32 tun_get_msglevel(struct net_device *dev)
1783{
1784#ifdef TUN_DEBUG
1785 struct tun_struct *tun = netdev_priv(dev);
1786 return tun->debug;
1787#else
1788 return -EOPNOTSUPP;
1789#endif
1790}
1791
1792static void tun_set_msglevel(struct net_device *dev, u32 value)
1793{
1794#ifdef TUN_DEBUG
1795 struct tun_struct *tun = netdev_priv(dev);
1796 tun->debug = value;
1797#endif
1798}
1799
7282d491 1800static const struct ethtool_ops tun_ethtool_ops = {
1da177e4
LT
1801 .get_settings = tun_get_settings,
1802 .get_drvinfo = tun_get_drvinfo,
1803 .get_msglevel = tun_get_msglevel,
1804 .set_msglevel = tun_set_msglevel,
bee31369 1805 .get_link = ethtool_op_get_link,
1da177e4
LT
1806};
1807
79d17604 1808
1da177e4
LT
1809static int __init tun_init(void)
1810{
1811 int ret = 0;
1812
6b8a66ee
JP
1813 pr_info("%s, %s\n", DRV_DESCRIPTION, DRV_VERSION);
1814 pr_info("%s\n", DRV_COPYRIGHT);
1da177e4 1815
f019a7a5 1816 ret = rtnl_link_register(&tun_link_ops);
79d17604 1817 if (ret) {
6b8a66ee 1818 pr_err("Can't register link_ops\n");
f019a7a5 1819 goto err_linkops;
79d17604
PE
1820 }
1821
1da177e4 1822 ret = misc_register(&tun_miscdev);
79d17604 1823 if (ret) {
6b8a66ee 1824 pr_err("Can't register misc device %d\n", TUN_MINOR);
79d17604
PE
1825 goto err_misc;
1826 }
f019a7a5 1827 return 0;
79d17604 1828err_misc:
f019a7a5
EB
1829 rtnl_link_unregister(&tun_link_ops);
1830err_linkops:
1da177e4
LT
1831 return ret;
1832}
1833
1834static void tun_cleanup(void)
1835{
6aa20a22 1836 misc_deregister(&tun_miscdev);
f019a7a5 1837 rtnl_link_unregister(&tun_link_ops);
1da177e4
LT
1838}
1839
05c2828c
MT
1840/* Get an underlying socket object from tun file. Returns error unless file is
1841 * attached to a device. The returned object works like a packet socket, it
1842 * can be used for sock_sendmsg/sock_recvmsg. The caller is responsible for
1843 * holding a reference to the file for as long as the socket is in use. */
1844struct socket *tun_get_socket(struct file *file)
1845{
6e914fc7 1846 struct tun_file *tfile;
05c2828c
MT
1847 if (file->f_op != &tun_fops)
1848 return ERR_PTR(-EINVAL);
6e914fc7
JW
1849 tfile = file->private_data;
1850 if (!tfile)
05c2828c 1851 return ERR_PTR(-EBADFD);
54f968d6 1852 return &tfile->socket;
05c2828c
MT
1853}
1854EXPORT_SYMBOL_GPL(tun_get_socket);
1855
1da177e4
LT
1856module_init(tun_init);
1857module_exit(tun_cleanup);
1858MODULE_DESCRIPTION(DRV_DESCRIPTION);
1859MODULE_AUTHOR(DRV_COPYRIGHT);
1860MODULE_LICENSE("GPL");
1861MODULE_ALIAS_MISCDEV(TUN_MINOR);
578454ff 1862MODULE_ALIAS("devname:net/tun");