tun.c (74a3e5a71c9b54c63bff978e9cafbcef67600f0b) tun.c (a7385ba21102a90f902055f9f185ca02bf62fa43)
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.

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

101
102 struct tap_filter txflt;
103
104#ifdef TUN_DEBUG
105 int debug;
106#endif
107};
108
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.

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

101
102 struct tap_filter txflt;
103
104#ifdef TUN_DEBUG
105 int debug;
106#endif
107};
108
109static int tun_attach(struct tun_struct *tun, struct file *file)
110{
111 const struct cred *cred = current_cred();
112
113 ASSERT_RTNL();
114
115 if (file->private_data)
116 return -EINVAL;
117
118 if (tun->attached)
119 return -EBUSY;
120
121 /* Check permissions */
122 if (((tun->owner != -1 && cred->euid != tun->owner) ||
123 (tun->group != -1 && cred->egid != tun->group)) &&
124 !capable(CAP_NET_ADMIN))
125 return -EPERM;
126
127 file->private_data = tun;
128 tun->attached = 1;
129 get_net(dev_net(tun->dev));
130
131 return 0;
132}
133
109/* TAP filterting */
110static void addr_hash_set(u32 *mask, const u8 *addr)
111{
112 int n = ether_crc(ETH_ALEN, addr) >> 26;
113 mask[n >> 5] |= (1 << (n & 31));
114}
115
116static unsigned int addr_hash_test(const u32 *mask, const u8 *addr)

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

690 dev->destructor = free_netdev;
691 dev->features |= NETIF_F_NETNS_LOCAL;
692}
693
694static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
695{
696 struct tun_struct *tun;
697 struct net_device *dev;
134/* TAP filterting */
135static void addr_hash_set(u32 *mask, const u8 *addr)
136{
137 int n = ether_crc(ETH_ALEN, addr) >> 26;
138 mask[n >> 5] |= (1 << (n & 31));
139}
140
141static unsigned int addr_hash_test(const u32 *mask, const u8 *addr)

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

715 dev->destructor = free_netdev;
716 dev->features |= NETIF_F_NETNS_LOCAL;
717}
718
719static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
720{
721 struct tun_struct *tun;
722 struct net_device *dev;
698 const struct cred *cred = current_cred();
699 int err;
700
701 dev = __dev_get_by_name(net, ifr->ifr_name);
702 if (dev) {
703 if ((ifr->ifr_flags & IFF_TUN) && dev->netdev_ops == &tun_netdev_ops)
704 tun = netdev_priv(dev);
705 else if ((ifr->ifr_flags & IFF_TAP) && dev->netdev_ops == &tap_netdev_ops)
706 tun = netdev_priv(dev);
707 else
708 return -EINVAL;
709
723 int err;
724
725 dev = __dev_get_by_name(net, ifr->ifr_name);
726 if (dev) {
727 if ((ifr->ifr_flags & IFF_TUN) && dev->netdev_ops == &tun_netdev_ops)
728 tun = netdev_priv(dev);
729 else if ((ifr->ifr_flags & IFF_TAP) && dev->netdev_ops == &tap_netdev_ops)
730 tun = netdev_priv(dev);
731 else
732 return -EINVAL;
733
710 if (tun->attached)
711 return -EBUSY;
712
713 /* Check permissions */
714 if (((tun->owner != -1 &&
715 cred->euid != tun->owner) ||
716 (tun->group != -1 &&
717 cred->egid != tun->group)) &&
718 !capable(CAP_NET_ADMIN)) {
719 return -EPERM;
720 }
734 err = tun_attach(tun, file);
735 if (err < 0)
736 return err;
721 }
722 else {
723 char *name;
724 unsigned long flags = 0;
725
726 err = -EINVAL;
727
728 if (!capable(CAP_NET_ADMIN))

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

761 err = dev_alloc_name(dev, dev->name);
762 if (err < 0)
763 goto err_free_dev;
764 }
765
766 err = register_netdevice(tun->dev);
767 if (err < 0)
768 goto err_free_dev;
737 }
738 else {
739 char *name;
740 unsigned long flags = 0;
741
742 err = -EINVAL;
743
744 if (!capable(CAP_NET_ADMIN))

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

777 err = dev_alloc_name(dev, dev->name);
778 if (err < 0)
779 goto err_free_dev;
780 }
781
782 err = register_netdevice(tun->dev);
783 if (err < 0)
784 goto err_free_dev;
785
786 err = tun_attach(tun, file);
787 if (err < 0)
788 goto err_free_dev;
769 }
770
771 DBG(KERN_INFO "%s: tun_set_iff\n", tun->dev->name);
772
773 if (ifr->ifr_flags & IFF_NO_PI)
774 tun->flags |= TUN_NO_PI;
775 else
776 tun->flags &= ~TUN_NO_PI;
777
778 if (ifr->ifr_flags & IFF_ONE_QUEUE)
779 tun->flags |= TUN_ONE_QUEUE;
780 else
781 tun->flags &= ~TUN_ONE_QUEUE;
782
783 if (ifr->ifr_flags & IFF_VNET_HDR)
784 tun->flags |= TUN_VNET_HDR;
785 else
786 tun->flags &= ~TUN_VNET_HDR;
787
789 }
790
791 DBG(KERN_INFO "%s: tun_set_iff\n", tun->dev->name);
792
793 if (ifr->ifr_flags & IFF_NO_PI)
794 tun->flags |= TUN_NO_PI;
795 else
796 tun->flags &= ~TUN_NO_PI;
797
798 if (ifr->ifr_flags & IFF_ONE_QUEUE)
799 tun->flags |= TUN_ONE_QUEUE;
800 else
801 tun->flags &= ~TUN_ONE_QUEUE;
802
803 if (ifr->ifr_flags & IFF_VNET_HDR)
804 tun->flags |= TUN_VNET_HDR;
805 else
806 tun->flags &= ~TUN_VNET_HDR;
807
788 file->private_data = tun;
789 tun->attached = 1;
790 get_net(dev_net(tun->dev));
791
792 /* Make sure persistent devices do not get stuck in
793 * xoff state.
794 */
795 if (netif_running(tun->dev))
796 netif_wake_queue(tun->dev);
797
798 strcpy(ifr->ifr_name, tun->dev->name);
799 return 0;

--- 455 unchanged lines hidden ---
808 /* Make sure persistent devices do not get stuck in
809 * xoff state.
810 */
811 if (netif_running(tun->dev))
812 netif_wake_queue(tun->dev);
813
814 strcpy(ifr->ifr_name, tun->dev->name);
815 return 0;

--- 455 unchanged lines hidden ---