tun.c (946e0f6ffcaa614012d646f4cf84efdd62628c8b) tun.c (86dfb4acb378cb3a4eede3db919604c583beaa7c)
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.

--- 57 unchanged lines hidden (view full) ---

66#include <linux/virtio_net.h>
67#include <linux/rcupdate.h>
68#include <net/net_namespace.h>
69#include <net/netns/generic.h>
70#include <net/rtnetlink.h>
71#include <net/sock.h>
72#include <linux/seq_file.h>
73#include <linux/uio.h>
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.

--- 57 unchanged lines hidden (view full) ---

66#include <linux/virtio_net.h>
67#include <linux/rcupdate.h>
68#include <net/net_namespace.h>
69#include <net/netns/generic.h>
70#include <net/rtnetlink.h>
71#include <net/sock.h>
72#include <linux/seq_file.h>
73#include <linux/uio.h>
74#include <linux/skb_array.h>
74
75#include <asm/uaccess.h>
76
77/* Uncomment to enable debugging */
78/* #define TUN_DEBUG 1 */
79
80#ifdef TUN_DEBUG
81static int debug;

--- 80 unchanged lines hidden (view full) ---

162 /* only used for fasnyc */
163 unsigned int flags;
164 union {
165 u16 queue_index;
166 unsigned int ifindex;
167 };
168 struct list_head next;
169 struct tun_struct *detached;
75
76#include <asm/uaccess.h>
77
78/* Uncomment to enable debugging */
79/* #define TUN_DEBUG 1 */
80
81#ifdef TUN_DEBUG
82static int debug;

--- 80 unchanged lines hidden (view full) ---

163 /* only used for fasnyc */
164 unsigned int flags;
165 union {
166 u16 queue_index;
167 unsigned int ifindex;
168 };
169 struct list_head next;
170 struct tun_struct *detached;
171 struct skb_array tx_array;
170};
171
172struct tun_flow_entry {
173 struct hlist_node hash_link;
174 struct rcu_head rcu;
175 struct tun_struct *tun;
176
177 u32 rxhash;

--- 332 unchanged lines hidden (view full) ---

510 tfile->detached = NULL;
511 list_del_init(&tfile->next);
512 --tun->numdisabled;
513 return tun;
514}
515
516static void tun_queue_purge(struct tun_file *tfile)
517{
172};
173
174struct tun_flow_entry {
175 struct hlist_node hash_link;
176 struct rcu_head rcu;
177 struct tun_struct *tun;
178
179 u32 rxhash;

--- 332 unchanged lines hidden (view full) ---

512 tfile->detached = NULL;
513 list_del_init(&tfile->next);
514 --tun->numdisabled;
515 return tun;
516}
517
518static void tun_queue_purge(struct tun_file *tfile)
519{
518 skb_queue_purge(&tfile->sk.sk_receive_queue);
520 struct sk_buff *skb;
521
522 while ((skb = skb_array_consume(&tfile->tx_array)) != NULL)
523 kfree_skb(skb);
524
519 skb_queue_purge(&tfile->sk.sk_error_queue);
520}
521
522static void __tun_detach(struct tun_file *tfile, bool clean)
523{
524 struct tun_file *ntfile;
525 struct tun_struct *tun;
526

--- 28 unchanged lines hidden (view full) ---

555 if (clean) {
556 if (tun && tun->numqueues == 0 && tun->numdisabled == 0) {
557 netif_carrier_off(tun->dev);
558
559 if (!(tun->flags & IFF_PERSIST) &&
560 tun->dev->reg_state == NETREG_REGISTERED)
561 unregister_netdevice(tun->dev);
562 }
525 skb_queue_purge(&tfile->sk.sk_error_queue);
526}
527
528static void __tun_detach(struct tun_file *tfile, bool clean)
529{
530 struct tun_file *ntfile;
531 struct tun_struct *tun;
532

--- 28 unchanged lines hidden (view full) ---

561 if (clean) {
562 if (tun && tun->numqueues == 0 && tun->numdisabled == 0) {
563 netif_carrier_off(tun->dev);
564
565 if (!(tun->flags & IFF_PERSIST) &&
566 tun->dev->reg_state == NETREG_REGISTERED)
567 unregister_netdevice(tun->dev);
568 }
569 if (tun)
570 skb_array_cleanup(&tfile->tx_array);
563 sock_put(&tfile->sk);
564 }
565}
566
567static void tun_detach(struct tun_file *tfile, bool clean)
568{
569 rtnl_lock();
570 __tun_detach(tfile, clean);

--- 37 unchanged lines hidden (view full) ---

608
609 if (tun->flags & IFF_PERSIST)
610 module_put(THIS_MODULE);
611}
612
613static int tun_attach(struct tun_struct *tun, struct file *file, bool skip_filter)
614{
615 struct tun_file *tfile = file->private_data;
571 sock_put(&tfile->sk);
572 }
573}
574
575static void tun_detach(struct tun_file *tfile, bool clean)
576{
577 rtnl_lock();
578 __tun_detach(tfile, clean);

--- 37 unchanged lines hidden (view full) ---

616
617 if (tun->flags & IFF_PERSIST)
618 module_put(THIS_MODULE);
619}
620
621static int tun_attach(struct tun_struct *tun, struct file *file, bool skip_filter)
622{
623 struct tun_file *tfile = file->private_data;
624 struct net_device *dev = tun->dev;
616 int err;
617
618 err = security_tun_dev_attach(tfile->socket.sk, tun->security);
619 if (err < 0)
620 goto out;
621
622 err = -EINVAL;
623 if (rtnl_dereference(tfile->tun) && !tfile->detached)

--- 13 unchanged lines hidden (view full) ---

637 /* Re-attach the filter to persist device */
638 if (!skip_filter && (tun->filter_attached == true)) {
639 lock_sock(tfile->socket.sk);
640 err = sk_attach_filter(&tun->fprog, tfile->socket.sk);
641 release_sock(tfile->socket.sk);
642 if (!err)
643 goto out;
644 }
625 int err;
626
627 err = security_tun_dev_attach(tfile->socket.sk, tun->security);
628 if (err < 0)
629 goto out;
630
631 err = -EINVAL;
632 if (rtnl_dereference(tfile->tun) && !tfile->detached)

--- 13 unchanged lines hidden (view full) ---

646 /* Re-attach the filter to persist device */
647 if (!skip_filter && (tun->filter_attached == true)) {
648 lock_sock(tfile->socket.sk);
649 err = sk_attach_filter(&tun->fprog, tfile->socket.sk);
650 release_sock(tfile->socket.sk);
651 if (!err)
652 goto out;
653 }
654
655 if (!tfile->detached &&
656 skb_array_init(&tfile->tx_array, dev->tx_queue_len, GFP_KERNEL)) {
657 err = -ENOMEM;
658 goto out;
659 }
660
645 tfile->queue_index = tun->numqueues;
646 tfile->socket.sk->sk_shutdown &= ~RCV_SHUTDOWN;
647 rcu_assign_pointer(tfile->tun, tun);
648 rcu_assign_pointer(tun->tfiles[tun->numqueues], tfile);
649 tun->numqueues++;
650
651 if (tfile->detached)
652 tun_enable_queue(tfile);

--- 233 unchanged lines hidden (view full) ---

886
887 /* Orphan the skb - required as we might hang on to it
888 * for indefinite time.
889 */
890 skb_orphan(skb);
891
892 nf_reset(skb);
893
661 tfile->queue_index = tun->numqueues;
662 tfile->socket.sk->sk_shutdown &= ~RCV_SHUTDOWN;
663 rcu_assign_pointer(tfile->tun, tun);
664 rcu_assign_pointer(tun->tfiles[tun->numqueues], tfile);
665 tun->numqueues++;
666
667 if (tfile->detached)
668 tun_enable_queue(tfile);

--- 233 unchanged lines hidden (view full) ---

902
903 /* Orphan the skb - required as we might hang on to it
904 * for indefinite time.
905 */
906 skb_orphan(skb);
907
908 nf_reset(skb);
909
894 /* Enqueue packet */
895 skb_queue_tail(&tfile->socket.sk->sk_receive_queue, skb);
910 if (skb_array_produce(&tfile->tx_array, skb))
911 goto drop;
896
897 /* Notify and wake up reader process */
898 if (tfile->flags & TUN_FASYNC)
899 kill_fasync(&tfile->fasync, SIGIO, POLL_IN);
900 tfile->socket.sk->sk_data_ready(tfile->socket.sk);
901
902 rcu_read_unlock();
903 return NETDEV_TX_OK;

--- 198 unchanged lines hidden (view full) ---

1102 return POLLERR;
1103
1104 sk = tfile->socket.sk;
1105
1106 tun_debug(KERN_INFO, tun, "tun_chr_poll\n");
1107
1108 poll_wait(file, sk_sleep(sk), wait);
1109
912
913 /* Notify and wake up reader process */
914 if (tfile->flags & TUN_FASYNC)
915 kill_fasync(&tfile->fasync, SIGIO, POLL_IN);
916 tfile->socket.sk->sk_data_ready(tfile->socket.sk);
917
918 rcu_read_unlock();
919 return NETDEV_TX_OK;

--- 198 unchanged lines hidden (view full) ---

1118 return POLLERR;
1119
1120 sk = tfile->socket.sk;
1121
1122 tun_debug(KERN_INFO, tun, "tun_chr_poll\n");
1123
1124 poll_wait(file, sk_sleep(sk), wait);
1125
1110 if (!skb_queue_empty(&sk->sk_receive_queue))
1126 if (!skb_array_empty(&tfile->tx_array))
1111 mask |= POLLIN | POLLRDNORM;
1112
1113 if (sock_writeable(sk) ||
1114 (!test_and_set_bit(SOCKWQ_ASYNC_NOSPACE, &sk->sk_socket->flags) &&
1115 sock_writeable(sk)))
1116 mask |= POLLOUT | POLLWRNORM;
1117
1118 if (tun->dev->reg_state != NETREG_REGISTERED)

--- 130 unchanged lines hidden (view full) ---

1249 }
1250
1251 if (err) {
1252 this_cpu_inc(tun->pcpu_stats->rx_dropped);
1253 kfree_skb(skb);
1254 return -EFAULT;
1255 }
1256
1127 mask |= POLLIN | POLLRDNORM;
1128
1129 if (sock_writeable(sk) ||
1130 (!test_and_set_bit(SOCKWQ_ASYNC_NOSPACE, &sk->sk_socket->flags) &&
1131 sock_writeable(sk)))
1132 mask |= POLLOUT | POLLWRNORM;
1133
1134 if (tun->dev->reg_state != NETREG_REGISTERED)

--- 130 unchanged lines hidden (view full) ---

1265 }
1266
1267 if (err) {
1268 this_cpu_inc(tun->pcpu_stats->rx_dropped);
1269 kfree_skb(skb);
1270 return -EFAULT;
1271 }
1272
1257 if (gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
1258 if (!skb_partial_csum_set(skb, tun16_to_cpu(tun, gso.csum_start),
1259 tun16_to_cpu(tun, gso.csum_offset))) {
1260 this_cpu_inc(tun->pcpu_stats->rx_frame_errors);
1261 kfree_skb(skb);
1262 return -EINVAL;
1263 }
1273 err = virtio_net_hdr_to_skb(skb, &gso, tun_is_little_endian(tun));
1274 if (err) {
1275 this_cpu_inc(tun->pcpu_stats->rx_frame_errors);
1276 kfree_skb(skb);
1277 return -EINVAL;
1264 }
1265
1266 switch (tun->flags & TUN_TYPE_MASK) {
1267 case IFF_TUN:
1268 if (tun->flags & IFF_NO_PI) {
1269 switch (skb->data[0] & 0xf0) {
1270 case 0x40:
1271 pi.proto = htons(ETH_P_IP);

--- 12 unchanged lines hidden (view full) ---

1284 skb->protocol = pi.proto;
1285 skb->dev = tun->dev;
1286 break;
1287 case IFF_TAP:
1288 skb->protocol = eth_type_trans(skb, tun->dev);
1289 break;
1290 }
1291
1278 }
1279
1280 switch (tun->flags & TUN_TYPE_MASK) {
1281 case IFF_TUN:
1282 if (tun->flags & IFF_NO_PI) {
1283 switch (skb->data[0] & 0xf0) {
1284 case 0x40:
1285 pi.proto = htons(ETH_P_IP);

--- 12 unchanged lines hidden (view full) ---

1298 skb->protocol = pi.proto;
1299 skb->dev = tun->dev;
1300 break;
1301 case IFF_TAP:
1302 skb->protocol = eth_type_trans(skb, tun->dev);
1303 break;
1304 }
1305
1292 if (gso.gso_type != VIRTIO_NET_HDR_GSO_NONE) {
1293 pr_debug("GSO!\n");
1294 switch (gso.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
1295 case VIRTIO_NET_HDR_GSO_TCPV4:
1296 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
1297 break;
1298 case VIRTIO_NET_HDR_GSO_TCPV6:
1299 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
1300 break;
1301 case VIRTIO_NET_HDR_GSO_UDP:
1302 skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
1303 break;
1304 default:
1305 this_cpu_inc(tun->pcpu_stats->rx_frame_errors);
1306 kfree_skb(skb);
1307 return -EINVAL;
1308 }
1309
1310 if (gso.gso_type & VIRTIO_NET_HDR_GSO_ECN)
1311 skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
1312
1313 skb_shinfo(skb)->gso_size = tun16_to_cpu(tun, gso.gso_size);
1314 if (skb_shinfo(skb)->gso_size == 0) {
1315 this_cpu_inc(tun->pcpu_stats->rx_frame_errors);
1316 kfree_skb(skb);
1317 return -EINVAL;
1318 }
1319
1320 /* Header must be checked, and gso_segs computed. */
1321 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
1322 skb_shinfo(skb)->gso_segs = 0;
1323 }
1324
1325 /* copy skb_ubuf_info for callback when skb has no error */
1326 if (zerocopy) {
1327 skb_shinfo(skb)->destructor_arg = msg_control;
1328 skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY;
1329 skb_shinfo(skb)->tx_flags |= SKBTX_SHARED_FRAG;
1330 }
1331
1332 skb_reset_network_header(skb);

--- 61 unchanged lines hidden (view full) ---

1394 }
1395
1396 if (copy_to_iter(&pi, sizeof(pi), iter) != sizeof(pi))
1397 return -EFAULT;
1398 }
1399
1400 if (vnet_hdr_sz) {
1401 struct virtio_net_hdr gso = { 0 }; /* no info leak */
1306 /* copy skb_ubuf_info for callback when skb has no error */
1307 if (zerocopy) {
1308 skb_shinfo(skb)->destructor_arg = msg_control;
1309 skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY;
1310 skb_shinfo(skb)->tx_flags |= SKBTX_SHARED_FRAG;
1311 }
1312
1313 skb_reset_network_header(skb);

--- 61 unchanged lines hidden (view full) ---

1375 }
1376
1377 if (copy_to_iter(&pi, sizeof(pi), iter) != sizeof(pi))
1378 return -EFAULT;
1379 }
1380
1381 if (vnet_hdr_sz) {
1382 struct virtio_net_hdr gso = { 0 }; /* no info leak */
1383 int ret;
1384
1402 if (iov_iter_count(iter) < vnet_hdr_sz)
1403 return -EINVAL;
1404
1385 if (iov_iter_count(iter) < vnet_hdr_sz)
1386 return -EINVAL;
1387
1405 if (skb_is_gso(skb)) {
1388 ret = virtio_net_hdr_from_skb(skb, &gso,
1389 tun_is_little_endian(tun));
1390 if (ret) {
1406 struct skb_shared_info *sinfo = skb_shinfo(skb);
1391 struct skb_shared_info *sinfo = skb_shinfo(skb);
1392 pr_err("unexpected GSO type: "
1393 "0x%x, gso_size %d, hdr_len %d\n",
1394 sinfo->gso_type, tun16_to_cpu(tun, gso.gso_size),
1395 tun16_to_cpu(tun, gso.hdr_len));
1396 print_hex_dump(KERN_ERR, "tun: ",
1397 DUMP_PREFIX_NONE,
1398 16, 1, skb->head,
1399 min((int)tun16_to_cpu(tun, gso.hdr_len), 64), true);
1400 WARN_ON_ONCE(1);
1401 return -EINVAL;
1402 }
1407
1403
1408 /* This is a hint as to how much should be linear. */
1409 gso.hdr_len = cpu_to_tun16(tun, skb_headlen(skb));
1410 gso.gso_size = cpu_to_tun16(tun, sinfo->gso_size);
1411 if (sinfo->gso_type & SKB_GSO_TCPV4)
1412 gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
1413 else if (sinfo->gso_type & SKB_GSO_TCPV6)
1414 gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
1415 else if (sinfo->gso_type & SKB_GSO_UDP)
1416 gso.gso_type = VIRTIO_NET_HDR_GSO_UDP;
1417 else {
1418 pr_err("unexpected GSO type: "
1419 "0x%x, gso_size %d, hdr_len %d\n",
1420 sinfo->gso_type, tun16_to_cpu(tun, gso.gso_size),
1421 tun16_to_cpu(tun, gso.hdr_len));
1422 print_hex_dump(KERN_ERR, "tun: ",
1423 DUMP_PREFIX_NONE,
1424 16, 1, skb->head,
1425 min((int)tun16_to_cpu(tun, gso.hdr_len), 64), true);
1426 WARN_ON_ONCE(1);
1427 return -EINVAL;
1428 }
1429 if (sinfo->gso_type & SKB_GSO_TCP_ECN)
1430 gso.gso_type |= VIRTIO_NET_HDR_GSO_ECN;
1431 } else
1432 gso.gso_type = VIRTIO_NET_HDR_GSO_NONE;
1433
1434 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1435 gso.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
1436 gso.csum_start = cpu_to_tun16(tun, skb_checksum_start_offset(skb) +
1437 vlan_hlen);
1438 gso.csum_offset = cpu_to_tun16(tun, skb->csum_offset);
1439 } else if (skb->ip_summed == CHECKSUM_UNNECESSARY) {
1440 gso.flags = VIRTIO_NET_HDR_F_DATA_VALID;
1441 } /* else everything is zero */
1442
1443 if (copy_to_iter(&gso, sizeof(gso), iter) != sizeof(gso))
1444 return -EFAULT;
1445
1446 iov_iter_advance(iter, vnet_hdr_sz - sizeof(gso));
1447 }
1448
1449 if (vlan_hlen) {
1450 int ret;

--- 25 unchanged lines hidden (view full) ---

1476 stats->tx_packets++;
1477 stats->tx_bytes += skb->len + vlan_hlen;
1478 u64_stats_update_end(&stats->syncp);
1479 put_cpu_ptr(tun->pcpu_stats);
1480
1481 return total;
1482}
1483
1404 if (copy_to_iter(&gso, sizeof(gso), iter) != sizeof(gso))
1405 return -EFAULT;
1406
1407 iov_iter_advance(iter, vnet_hdr_sz - sizeof(gso));
1408 }
1409
1410 if (vlan_hlen) {
1411 int ret;

--- 25 unchanged lines hidden (view full) ---

1437 stats->tx_packets++;
1438 stats->tx_bytes += skb->len + vlan_hlen;
1439 u64_stats_update_end(&stats->syncp);
1440 put_cpu_ptr(tun->pcpu_stats);
1441
1442 return total;
1443}
1444
1445static struct sk_buff *tun_ring_recv(struct tun_file *tfile, int noblock,
1446 int *err)
1447{
1448 DECLARE_WAITQUEUE(wait, current);
1449 struct sk_buff *skb = NULL;
1450 int error = 0;
1451
1452 skb = skb_array_consume(&tfile->tx_array);
1453 if (skb)
1454 goto out;
1455 if (noblock) {
1456 error = -EAGAIN;
1457 goto out;
1458 }
1459
1460 add_wait_queue(&tfile->wq.wait, &wait);
1461 current->state = TASK_INTERRUPTIBLE;
1462
1463 while (1) {
1464 skb = skb_array_consume(&tfile->tx_array);
1465 if (skb)
1466 break;
1467 if (signal_pending(current)) {
1468 error = -ERESTARTSYS;
1469 break;
1470 }
1471 if (tfile->socket.sk->sk_shutdown & RCV_SHUTDOWN) {
1472 error = -EFAULT;
1473 break;
1474 }
1475
1476 schedule();
1477 }
1478
1479 current->state = TASK_RUNNING;
1480 remove_wait_queue(&tfile->wq.wait, &wait);
1481
1482out:
1483 *err = error;
1484 return skb;
1485}
1486
1484static ssize_t tun_do_read(struct tun_struct *tun, struct tun_file *tfile,
1485 struct iov_iter *to,
1486 int noblock)
1487{
1488 struct sk_buff *skb;
1489 ssize_t ret;
1487static ssize_t tun_do_read(struct tun_struct *tun, struct tun_file *tfile,
1488 struct iov_iter *to,
1489 int noblock)
1490{
1491 struct sk_buff *skb;
1492 ssize_t ret;
1490 int peeked, err, off = 0;
1493 int err;
1491
1492 tun_debug(KERN_INFO, tun, "tun_do_read\n");
1493
1494 if (!iov_iter_count(to))
1495 return 0;
1496
1494
1495 tun_debug(KERN_INFO, tun, "tun_do_read\n");
1496
1497 if (!iov_iter_count(to))
1498 return 0;
1499
1497 /* Read frames from queue */
1498 skb = __skb_recv_datagram(tfile->socket.sk, noblock ? MSG_DONTWAIT : 0,
1499 &peeked, &off, &err);
1500 /* Read frames from ring */
1501 skb = tun_ring_recv(tfile, noblock, &err);
1500 if (!skb)
1501 return err;
1502
1503 ret = tun_put_user(tun, tfile, skb, to);
1504 if (unlikely(ret < 0))
1505 kfree_skb(skb);
1506 else
1507 consume_skb(skb);

--- 116 unchanged lines hidden (view full) ---

1624 m->msg_flags |= MSG_TRUNC;
1625 ret = flags & MSG_TRUNC ? ret : total_len;
1626 }
1627out:
1628 tun_put(tun);
1629 return ret;
1630}
1631
1502 if (!skb)
1503 return err;
1504
1505 ret = tun_put_user(tun, tfile, skb, to);
1506 if (unlikely(ret < 0))
1507 kfree_skb(skb);
1508 else
1509 consume_skb(skb);

--- 116 unchanged lines hidden (view full) ---

1626 m->msg_flags |= MSG_TRUNC;
1627 ret = flags & MSG_TRUNC ? ret : total_len;
1628 }
1629out:
1630 tun_put(tun);
1631 return ret;
1632}
1633
1634static int tun_peek_len(struct socket *sock)
1635{
1636 struct tun_file *tfile = container_of(sock, struct tun_file, socket);
1637 struct tun_struct *tun;
1638 int ret = 0;
1639
1640 tun = __tun_get(tfile);
1641 if (!tun)
1642 return 0;
1643
1644 ret = skb_array_peek_len(&tfile->tx_array);
1645 tun_put(tun);
1646
1647 return ret;
1648}
1649
1632/* Ops structure to mimic raw sockets with tun */
1633static const struct proto_ops tun_socket_ops = {
1650/* Ops structure to mimic raw sockets with tun */
1651static const struct proto_ops tun_socket_ops = {
1652 .peek_len = tun_peek_len,
1634 .sendmsg = tun_sendmsg,
1635 .recvmsg = tun_recvmsg,
1636};
1637
1638static struct proto tun_proto = {
1639 .name = "tun",
1640 .owner = THIS_MODULE,
1641 .obj_size = sizeof(struct tun_file),

--- 805 unchanged lines hidden (view full) ---

2447 .get_settings = tun_get_settings,
2448 .get_drvinfo = tun_get_drvinfo,
2449 .get_msglevel = tun_get_msglevel,
2450 .set_msglevel = tun_set_msglevel,
2451 .get_link = ethtool_op_get_link,
2452 .get_ts_info = ethtool_op_get_ts_info,
2453};
2454
1653 .sendmsg = tun_sendmsg,
1654 .recvmsg = tun_recvmsg,
1655};
1656
1657static struct proto tun_proto = {
1658 .name = "tun",
1659 .owner = THIS_MODULE,
1660 .obj_size = sizeof(struct tun_file),

--- 805 unchanged lines hidden (view full) ---

2466 .get_settings = tun_get_settings,
2467 .get_drvinfo = tun_get_drvinfo,
2468 .get_msglevel = tun_get_msglevel,
2469 .set_msglevel = tun_set_msglevel,
2470 .get_link = ethtool_op_get_link,
2471 .get_ts_info = ethtool_op_get_ts_info,
2472};
2473
2474static int tun_queue_resize(struct tun_struct *tun)
2475{
2476 struct net_device *dev = tun->dev;
2477 struct tun_file *tfile;
2478 struct skb_array **arrays;
2479 int n = tun->numqueues + tun->numdisabled;
2480 int ret, i;
2455
2481
2482 arrays = kmalloc(sizeof *arrays * n, GFP_KERNEL);
2483 if (!arrays)
2484 return -ENOMEM;
2485
2486 for (i = 0; i < tun->numqueues; i++) {
2487 tfile = rtnl_dereference(tun->tfiles[i]);
2488 arrays[i] = &tfile->tx_array;
2489 }
2490 list_for_each_entry(tfile, &tun->disabled, next)
2491 arrays[i++] = &tfile->tx_array;
2492
2493 ret = skb_array_resize_multiple(arrays, n,
2494 dev->tx_queue_len, GFP_KERNEL);
2495
2496 kfree(arrays);
2497 return ret;
2498}
2499
2500static int tun_device_event(struct notifier_block *unused,
2501 unsigned long event, void *ptr)
2502{
2503 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
2504 struct tun_struct *tun = netdev_priv(dev);
2505
2506 if (dev->rtnl_link_ops != &tun_link_ops)
2507 return NOTIFY_DONE;
2508
2509 switch (event) {
2510 case NETDEV_CHANGE_TX_QUEUE_LEN:
2511 if (tun_queue_resize(tun))
2512 return NOTIFY_BAD;
2513 break;
2514 default:
2515 break;
2516 }
2517
2518 return NOTIFY_DONE;
2519}
2520
2521static struct notifier_block tun_notifier_block __read_mostly = {
2522 .notifier_call = tun_device_event,
2523};
2524
2456static int __init tun_init(void)
2457{
2458 int ret = 0;
2459
2460 pr_info("%s, %s\n", DRV_DESCRIPTION, DRV_VERSION);
2461 pr_info("%s\n", DRV_COPYRIGHT);
2462
2463 ret = rtnl_link_register(&tun_link_ops);
2464 if (ret) {
2465 pr_err("Can't register link_ops\n");
2466 goto err_linkops;
2467 }
2468
2469 ret = misc_register(&tun_miscdev);
2470 if (ret) {
2471 pr_err("Can't register misc device %d\n", TUN_MINOR);
2472 goto err_misc;
2473 }
2525static int __init tun_init(void)
2526{
2527 int ret = 0;
2528
2529 pr_info("%s, %s\n", DRV_DESCRIPTION, DRV_VERSION);
2530 pr_info("%s\n", DRV_COPYRIGHT);
2531
2532 ret = rtnl_link_register(&tun_link_ops);
2533 if (ret) {
2534 pr_err("Can't register link_ops\n");
2535 goto err_linkops;
2536 }
2537
2538 ret = misc_register(&tun_miscdev);
2539 if (ret) {
2540 pr_err("Can't register misc device %d\n", TUN_MINOR);
2541 goto err_misc;
2542 }
2543
2544 register_netdevice_notifier(&tun_notifier_block);
2474 return 0;
2475err_misc:
2476 rtnl_link_unregister(&tun_link_ops);
2477err_linkops:
2478 return ret;
2479}
2480
2481static void tun_cleanup(void)
2482{
2483 misc_deregister(&tun_miscdev);
2484 rtnl_link_unregister(&tun_link_ops);
2545 return 0;
2546err_misc:
2547 rtnl_link_unregister(&tun_link_ops);
2548err_linkops:
2549 return ret;
2550}
2551
2552static void tun_cleanup(void)
2553{
2554 misc_deregister(&tun_miscdev);
2555 rtnl_link_unregister(&tun_link_ops);
2556 unregister_netdevice_notifier(&tun_notifier_block);
2485}
2486
2487/* Get an underlying socket object from tun file. Returns error unless file is
2488 * attached to a device. The returned object works like a packet socket, it
2489 * can be used for sock_sendmsg/sock_recvmsg. The caller is responsible for
2490 * holding a reference to the file for as long as the socket is in use. */
2491struct socket *tun_get_socket(struct file *file)
2492{

--- 17 unchanged lines hidden ---
2557}
2558
2559/* Get an underlying socket object from tun file. Returns error unless file is
2560 * attached to a device. The returned object works like a packet socket, it
2561 * can be used for sock_sendmsg/sock_recvmsg. The caller is responsible for
2562 * holding a reference to the file for as long as the socket is in use. */
2563struct socket *tun_get_socket(struct file *file)
2564{

--- 17 unchanged lines hidden ---