diff options
author | Eric W. Biederman <ebiederm@xmission.com> | 2007-09-17 14:56:21 -0400 |
---|---|---|
committer | David S. Miller <davem@sunset.davemloft.net> | 2007-10-10 19:49:10 -0400 |
commit | 881d966b48b035ab3f3aeaae0f3d3f9b584f45b2 (patch) | |
tree | c579d59a4107cbbe9e2b85939bc0d496b815c887 /net/core/dev.c | |
parent | b4b510290b056b86611757ce1175a230f1080f53 (diff) |
[NET]: Make the device list and device lookups per namespace.
This patch makes most of the generic device layer network
namespace safe. This patch makes dev_base_head a
network namespace variable, and then it picks up
a few associated variables. The functions:
dev_getbyhwaddr
dev_getfirsthwbytype
dev_get_by_flags
dev_get_by_name
__dev_get_by_name
dev_get_by_index
__dev_get_by_index
dev_ioctl
dev_ethtool
dev_load
wireless_process_ioctl
were modified to take a network namespace argument, and
deal with it.
vlan_ioctl_set and brioctl_set were modified so their
hooks will receive a network namespace argument.
So basically anthing in the core of the network stack that was
affected to by the change of dev_base was modified to handle
multiple network namespaces. The rest of the network stack was
simply modified to explicitly use &init_net the initial network
namespace. This can be fixed when those components of the network
stack are modified to handle multiple network namespaces.
For now the ifindex generator is left global.
Fundametally ifindex numbers are per namespace, or else
we will have corner case problems with migration when
we get that far.
At the same time there are assumptions in the network stack
that the ifindex of a network device won't change. Making
the ifindex number global seems a good compromise until
the network stack can cope with ifindex changes when
you change namespaces, and the like.
Signed-off-by: Eric W. Biederman <ebiederm@xmission.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
Diffstat (limited to 'net/core/dev.c')
-rw-r--r-- | net/core/dev.c | 271 |
1 files changed, 178 insertions, 93 deletions
diff --git a/net/core/dev.c b/net/core/dev.c index 40fd66fbe4e1..3a3d5ee73909 100644 --- a/net/core/dev.c +++ b/net/core/dev.c | |||
@@ -190,25 +190,22 @@ static struct net_dma net_dma = { | |||
190 | * unregister_netdevice(), which must be called with the rtnl | 190 | * unregister_netdevice(), which must be called with the rtnl |
191 | * semaphore held. | 191 | * semaphore held. |
192 | */ | 192 | */ |
193 | LIST_HEAD(dev_base_head); | ||
194 | DEFINE_RWLOCK(dev_base_lock); | 193 | DEFINE_RWLOCK(dev_base_lock); |
195 | 194 | ||
196 | EXPORT_SYMBOL(dev_base_head); | ||
197 | EXPORT_SYMBOL(dev_base_lock); | 195 | EXPORT_SYMBOL(dev_base_lock); |
198 | 196 | ||
199 | #define NETDEV_HASHBITS 8 | 197 | #define NETDEV_HASHBITS 8 |
200 | static struct hlist_head dev_name_head[1<<NETDEV_HASHBITS]; | 198 | #define NETDEV_HASHENTRIES (1 << NETDEV_HASHBITS) |
201 | static struct hlist_head dev_index_head[1<<NETDEV_HASHBITS]; | ||
202 | 199 | ||
203 | static inline struct hlist_head *dev_name_hash(const char *name) | 200 | static inline struct hlist_head *dev_name_hash(struct net *net, const char *name) |
204 | { | 201 | { |
205 | unsigned hash = full_name_hash(name, strnlen(name, IFNAMSIZ)); | 202 | unsigned hash = full_name_hash(name, strnlen(name, IFNAMSIZ)); |
206 | return &dev_name_head[hash & ((1<<NETDEV_HASHBITS)-1)]; | 203 | return &net->dev_name_head[hash & ((1 << NETDEV_HASHBITS) - 1)]; |
207 | } | 204 | } |
208 | 205 | ||
209 | static inline struct hlist_head *dev_index_hash(int ifindex) | 206 | static inline struct hlist_head *dev_index_hash(struct net *net, int ifindex) |
210 | { | 207 | { |
211 | return &dev_index_head[ifindex & ((1<<NETDEV_HASHBITS)-1)]; | 208 | return &net->dev_index_head[ifindex & ((1 << NETDEV_HASHBITS) - 1)]; |
212 | } | 209 | } |
213 | 210 | ||
214 | /* | 211 | /* |
@@ -492,7 +489,7 @@ unsigned long netdev_boot_base(const char *prefix, int unit) | |||
492 | * If device already registered then return base of 1 | 489 | * If device already registered then return base of 1 |
493 | * to indicate not to probe for this interface | 490 | * to indicate not to probe for this interface |
494 | */ | 491 | */ |
495 | if (__dev_get_by_name(name)) | 492 | if (__dev_get_by_name(&init_net, name)) |
496 | return 1; | 493 | return 1; |
497 | 494 | ||
498 | for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) | 495 | for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) |
@@ -547,11 +544,11 @@ __setup("netdev=", netdev_boot_setup); | |||
547 | * careful with locks. | 544 | * careful with locks. |
548 | */ | 545 | */ |
549 | 546 | ||
550 | struct net_device *__dev_get_by_name(const char *name) | 547 | struct net_device *__dev_get_by_name(struct net *net, const char *name) |
551 | { | 548 | { |
552 | struct hlist_node *p; | 549 | struct hlist_node *p; |
553 | 550 | ||
554 | hlist_for_each(p, dev_name_hash(name)) { | 551 | hlist_for_each(p, dev_name_hash(net, name)) { |
555 | struct net_device *dev | 552 | struct net_device *dev |
556 | = hlist_entry(p, struct net_device, name_hlist); | 553 | = hlist_entry(p, struct net_device, name_hlist); |
557 | if (!strncmp(dev->name, name, IFNAMSIZ)) | 554 | if (!strncmp(dev->name, name, IFNAMSIZ)) |
@@ -571,12 +568,12 @@ struct net_device *__dev_get_by_name(const char *name) | |||
571 | * matching device is found. | 568 | * matching device is found. |
572 | */ | 569 | */ |
573 | 570 | ||
574 | struct net_device *dev_get_by_name(const char *name) | 571 | struct net_device *dev_get_by_name(struct net *net, const char *name) |
575 | { | 572 | { |
576 | struct net_device *dev; | 573 | struct net_device *dev; |
577 | 574 | ||
578 | read_lock(&dev_base_lock); | 575 | read_lock(&dev_base_lock); |
579 | dev = __dev_get_by_name(name); | 576 | dev = __dev_get_by_name(net, name); |
580 | if (dev) | 577 | if (dev) |
581 | dev_hold(dev); | 578 | dev_hold(dev); |
582 | read_unlock(&dev_base_lock); | 579 | read_unlock(&dev_base_lock); |
@@ -594,11 +591,11 @@ struct net_device *dev_get_by_name(const char *name) | |||
594 | * or @dev_base_lock. | 591 | * or @dev_base_lock. |
595 | */ | 592 | */ |
596 | 593 | ||
597 | struct net_device *__dev_get_by_index(int ifindex) | 594 | struct net_device *__dev_get_by_index(struct net *net, int ifindex) |
598 | { | 595 | { |
599 | struct hlist_node *p; | 596 | struct hlist_node *p; |
600 | 597 | ||
601 | hlist_for_each(p, dev_index_hash(ifindex)) { | 598 | hlist_for_each(p, dev_index_hash(net, ifindex)) { |
602 | struct net_device *dev | 599 | struct net_device *dev |
603 | = hlist_entry(p, struct net_device, index_hlist); | 600 | = hlist_entry(p, struct net_device, index_hlist); |
604 | if (dev->ifindex == ifindex) | 601 | if (dev->ifindex == ifindex) |
@@ -618,12 +615,12 @@ struct net_device *__dev_get_by_index(int ifindex) | |||
618 | * dev_put to indicate they have finished with it. | 615 | * dev_put to indicate they have finished with it. |
619 | */ | 616 | */ |
620 | 617 | ||
621 | struct net_device *dev_get_by_index(int ifindex) | 618 | struct net_device *dev_get_by_index(struct net *net, int ifindex) |
622 | { | 619 | { |
623 | struct net_device *dev; | 620 | struct net_device *dev; |
624 | 621 | ||
625 | read_lock(&dev_base_lock); | 622 | read_lock(&dev_base_lock); |
626 | dev = __dev_get_by_index(ifindex); | 623 | dev = __dev_get_by_index(net, ifindex); |
627 | if (dev) | 624 | if (dev) |
628 | dev_hold(dev); | 625 | dev_hold(dev); |
629 | read_unlock(&dev_base_lock); | 626 | read_unlock(&dev_base_lock); |
@@ -644,13 +641,13 @@ struct net_device *dev_get_by_index(int ifindex) | |||
644 | * If the API was consistent this would be __dev_get_by_hwaddr | 641 | * If the API was consistent this would be __dev_get_by_hwaddr |
645 | */ | 642 | */ |
646 | 643 | ||
647 | struct net_device *dev_getbyhwaddr(unsigned short type, char *ha) | 644 | struct net_device *dev_getbyhwaddr(struct net *net, unsigned short type, char *ha) |
648 | { | 645 | { |
649 | struct net_device *dev; | 646 | struct net_device *dev; |
650 | 647 | ||
651 | ASSERT_RTNL(); | 648 | ASSERT_RTNL(); |
652 | 649 | ||
653 | for_each_netdev(dev) | 650 | for_each_netdev(&init_net, dev) |
654 | if (dev->type == type && | 651 | if (dev->type == type && |
655 | !memcmp(dev->dev_addr, ha, dev->addr_len)) | 652 | !memcmp(dev->dev_addr, ha, dev->addr_len)) |
656 | return dev; | 653 | return dev; |
@@ -660,12 +657,12 @@ struct net_device *dev_getbyhwaddr(unsigned short type, char *ha) | |||
660 | 657 | ||
661 | EXPORT_SYMBOL(dev_getbyhwaddr); | 658 | EXPORT_SYMBOL(dev_getbyhwaddr); |
662 | 659 | ||
663 | struct net_device *__dev_getfirstbyhwtype(unsigned short type) | 660 | struct net_device *__dev_getfirstbyhwtype(struct net *net, unsigned short type) |
664 | { | 661 | { |
665 | struct net_device *dev; | 662 | struct net_device *dev; |
666 | 663 | ||
667 | ASSERT_RTNL(); | 664 | ASSERT_RTNL(); |
668 | for_each_netdev(dev) | 665 | for_each_netdev(net, dev) |
669 | if (dev->type == type) | 666 | if (dev->type == type) |
670 | return dev; | 667 | return dev; |
671 | 668 | ||
@@ -674,12 +671,12 @@ struct net_device *__dev_getfirstbyhwtype(unsigned short type) | |||
674 | 671 | ||
675 | EXPORT_SYMBOL(__dev_getfirstbyhwtype); | 672 | EXPORT_SYMBOL(__dev_getfirstbyhwtype); |
676 | 673 | ||
677 | struct net_device *dev_getfirstbyhwtype(unsigned short type) | 674 | struct net_device *dev_getfirstbyhwtype(struct net *net, unsigned short type) |
678 | { | 675 | { |
679 | struct net_device *dev; | 676 | struct net_device *dev; |
680 | 677 | ||
681 | rtnl_lock(); | 678 | rtnl_lock(); |
682 | dev = __dev_getfirstbyhwtype(type); | 679 | dev = __dev_getfirstbyhwtype(net, type); |
683 | if (dev) | 680 | if (dev) |
684 | dev_hold(dev); | 681 | dev_hold(dev); |
685 | rtnl_unlock(); | 682 | rtnl_unlock(); |
@@ -699,13 +696,13 @@ EXPORT_SYMBOL(dev_getfirstbyhwtype); | |||
699 | * dev_put to indicate they have finished with it. | 696 | * dev_put to indicate they have finished with it. |
700 | */ | 697 | */ |
701 | 698 | ||
702 | struct net_device * dev_get_by_flags(unsigned short if_flags, unsigned short mask) | 699 | struct net_device * dev_get_by_flags(struct net *net, unsigned short if_flags, unsigned short mask) |
703 | { | 700 | { |
704 | struct net_device *dev, *ret; | 701 | struct net_device *dev, *ret; |
705 | 702 | ||
706 | ret = NULL; | 703 | ret = NULL; |
707 | read_lock(&dev_base_lock); | 704 | read_lock(&dev_base_lock); |
708 | for_each_netdev(dev) { | 705 | for_each_netdev(net, dev) { |
709 | if (((dev->flags ^ if_flags) & mask) == 0) { | 706 | if (((dev->flags ^ if_flags) & mask) == 0) { |
710 | dev_hold(dev); | 707 | dev_hold(dev); |
711 | ret = dev; | 708 | ret = dev; |
@@ -763,6 +760,10 @@ int dev_alloc_name(struct net_device *dev, const char *name) | |||
763 | const int max_netdevices = 8*PAGE_SIZE; | 760 | const int max_netdevices = 8*PAGE_SIZE; |
764 | long *inuse; | 761 | long *inuse; |
765 | struct net_device *d; | 762 | struct net_device *d; |
763 | struct net *net; | ||
764 | |||
765 | BUG_ON(!dev->nd_net); | ||
766 | net = dev->nd_net; | ||
766 | 767 | ||
767 | p = strnchr(name, IFNAMSIZ-1, '%'); | 768 | p = strnchr(name, IFNAMSIZ-1, '%'); |
768 | if (p) { | 769 | if (p) { |
@@ -779,7 +780,7 @@ int dev_alloc_name(struct net_device *dev, const char *name) | |||
779 | if (!inuse) | 780 | if (!inuse) |
780 | return -ENOMEM; | 781 | return -ENOMEM; |
781 | 782 | ||
782 | for_each_netdev(d) { | 783 | for_each_netdev(net, d) { |
783 | if (!sscanf(d->name, name, &i)) | 784 | if (!sscanf(d->name, name, &i)) |
784 | continue; | 785 | continue; |
785 | if (i < 0 || i >= max_netdevices) | 786 | if (i < 0 || i >= max_netdevices) |
@@ -796,7 +797,7 @@ int dev_alloc_name(struct net_device *dev, const char *name) | |||
796 | } | 797 | } |
797 | 798 | ||
798 | snprintf(buf, sizeof(buf), name, i); | 799 | snprintf(buf, sizeof(buf), name, i); |
799 | if (!__dev_get_by_name(buf)) { | 800 | if (!__dev_get_by_name(net, buf)) { |
800 | strlcpy(dev->name, buf, IFNAMSIZ); | 801 | strlcpy(dev->name, buf, IFNAMSIZ); |
801 | return i; | 802 | return i; |
802 | } | 803 | } |
@@ -822,9 +823,12 @@ int dev_change_name(struct net_device *dev, char *newname) | |||
822 | char oldname[IFNAMSIZ]; | 823 | char oldname[IFNAMSIZ]; |
823 | int err = 0; | 824 | int err = 0; |
824 | int ret; | 825 | int ret; |
826 | struct net *net; | ||
825 | 827 | ||
826 | ASSERT_RTNL(); | 828 | ASSERT_RTNL(); |
829 | BUG_ON(!dev->nd_net); | ||
827 | 830 | ||
831 | net = dev->nd_net; | ||
828 | if (dev->flags & IFF_UP) | 832 | if (dev->flags & IFF_UP) |
829 | return -EBUSY; | 833 | return -EBUSY; |
830 | 834 | ||
@@ -839,7 +843,7 @@ int dev_change_name(struct net_device *dev, char *newname) | |||
839 | return err; | 843 | return err; |
840 | strcpy(newname, dev->name); | 844 | strcpy(newname, dev->name); |
841 | } | 845 | } |
842 | else if (__dev_get_by_name(newname)) | 846 | else if (__dev_get_by_name(net, newname)) |
843 | return -EEXIST; | 847 | return -EEXIST; |
844 | else | 848 | else |
845 | strlcpy(dev->name, newname, IFNAMSIZ); | 849 | strlcpy(dev->name, newname, IFNAMSIZ); |
@@ -849,7 +853,7 @@ rollback: | |||
849 | 853 | ||
850 | write_lock_bh(&dev_base_lock); | 854 | write_lock_bh(&dev_base_lock); |
851 | hlist_del(&dev->name_hlist); | 855 | hlist_del(&dev->name_hlist); |
852 | hlist_add_head(&dev->name_hlist, dev_name_hash(dev->name)); | 856 | hlist_add_head(&dev->name_hlist, dev_name_hash(net, dev->name)); |
853 | write_unlock_bh(&dev_base_lock); | 857 | write_unlock_bh(&dev_base_lock); |
854 | 858 | ||
855 | ret = raw_notifier_call_chain(&netdev_chain, NETDEV_CHANGENAME, dev); | 859 | ret = raw_notifier_call_chain(&netdev_chain, NETDEV_CHANGENAME, dev); |
@@ -908,12 +912,12 @@ void netdev_state_change(struct net_device *dev) | |||
908 | * available in this kernel then it becomes a nop. | 912 | * available in this kernel then it becomes a nop. |
909 | */ | 913 | */ |
910 | 914 | ||
911 | void dev_load(const char *name) | 915 | void dev_load(struct net *net, const char *name) |
912 | { | 916 | { |
913 | struct net_device *dev; | 917 | struct net_device *dev; |
914 | 918 | ||
915 | read_lock(&dev_base_lock); | 919 | read_lock(&dev_base_lock); |
916 | dev = __dev_get_by_name(name); | 920 | dev = __dev_get_by_name(net, name); |
917 | read_unlock(&dev_base_lock); | 921 | read_unlock(&dev_base_lock); |
918 | 922 | ||
919 | if (!dev && capable(CAP_SYS_MODULE)) | 923 | if (!dev && capable(CAP_SYS_MODULE)) |
@@ -1052,6 +1056,8 @@ int dev_close(struct net_device *dev) | |||
1052 | } | 1056 | } |
1053 | 1057 | ||
1054 | 1058 | ||
1059 | static int dev_boot_phase = 1; | ||
1060 | |||
1055 | /* | 1061 | /* |
1056 | * Device change register/unregister. These are not inline or static | 1062 | * Device change register/unregister. These are not inline or static |
1057 | * as we export them to the world. | 1063 | * as we export them to the world. |
@@ -1075,23 +1081,27 @@ int register_netdevice_notifier(struct notifier_block *nb) | |||
1075 | { | 1081 | { |
1076 | struct net_device *dev; | 1082 | struct net_device *dev; |
1077 | struct net_device *last; | 1083 | struct net_device *last; |
1084 | struct net *net; | ||
1078 | int err; | 1085 | int err; |
1079 | 1086 | ||
1080 | rtnl_lock(); | 1087 | rtnl_lock(); |
1081 | err = raw_notifier_chain_register(&netdev_chain, nb); | 1088 | err = raw_notifier_chain_register(&netdev_chain, nb); |
1082 | if (err) | 1089 | if (err) |
1083 | goto unlock; | 1090 | goto unlock; |
1091 | if (dev_boot_phase) | ||
1092 | goto unlock; | ||
1093 | for_each_net(net) { | ||
1094 | for_each_netdev(net, dev) { | ||
1095 | err = nb->notifier_call(nb, NETDEV_REGISTER, dev); | ||
1096 | err = notifier_to_errno(err); | ||
1097 | if (err) | ||
1098 | goto rollback; | ||
1099 | |||
1100 | if (!(dev->flags & IFF_UP)) | ||
1101 | continue; | ||
1084 | 1102 | ||
1085 | for_each_netdev(dev) { | 1103 | nb->notifier_call(nb, NETDEV_UP, dev); |
1086 | err = nb->notifier_call(nb, NETDEV_REGISTER, dev); | 1104 | } |
1087 | err = notifier_to_errno(err); | ||
1088 | if (err) | ||
1089 | goto rollback; | ||
1090 | |||
1091 | if (!(dev->flags & IFF_UP)) | ||
1092 | continue; | ||
1093 | |||
1094 | nb->notifier_call(nb, NETDEV_UP, dev); | ||
1095 | } | 1105 | } |
1096 | 1106 | ||
1097 | unlock: | 1107 | unlock: |
@@ -1100,15 +1110,17 @@ unlock: | |||
1100 | 1110 | ||
1101 | rollback: | 1111 | rollback: |
1102 | last = dev; | 1112 | last = dev; |
1103 | for_each_netdev(dev) { | 1113 | for_each_net(net) { |
1104 | if (dev == last) | 1114 | for_each_netdev(net, dev) { |
1105 | break; | 1115 | if (dev == last) |
1116 | break; | ||
1106 | 1117 | ||
1107 | if (dev->flags & IFF_UP) { | 1118 | if (dev->flags & IFF_UP) { |
1108 | nb->notifier_call(nb, NETDEV_GOING_DOWN, dev); | 1119 | nb->notifier_call(nb, NETDEV_GOING_DOWN, dev); |
1109 | nb->notifier_call(nb, NETDEV_DOWN, dev); | 1120 | nb->notifier_call(nb, NETDEV_DOWN, dev); |
1121 | } | ||
1122 | nb->notifier_call(nb, NETDEV_UNREGISTER, dev); | ||
1110 | } | 1123 | } |
1111 | nb->notifier_call(nb, NETDEV_UNREGISTER, dev); | ||
1112 | } | 1124 | } |
1113 | goto unlock; | 1125 | goto unlock; |
1114 | } | 1126 | } |
@@ -2187,7 +2199,7 @@ int register_gifconf(unsigned int family, gifconf_func_t * gifconf) | |||
2187 | * match. --pb | 2199 | * match. --pb |
2188 | */ | 2200 | */ |
2189 | 2201 | ||
2190 | static int dev_ifname(struct ifreq __user *arg) | 2202 | static int dev_ifname(struct net *net, struct ifreq __user *arg) |
2191 | { | 2203 | { |
2192 | struct net_device *dev; | 2204 | struct net_device *dev; |
2193 | struct ifreq ifr; | 2205 | struct ifreq ifr; |
@@ -2200,7 +2212,7 @@ static int dev_ifname(struct ifreq __user *arg) | |||
2200 | return -EFAULT; | 2212 | return -EFAULT; |
2201 | 2213 | ||
2202 | read_lock(&dev_base_lock); | 2214 | read_lock(&dev_base_lock); |
2203 | dev = __dev_get_by_index(ifr.ifr_ifindex); | 2215 | dev = __dev_get_by_index(net, ifr.ifr_ifindex); |
2204 | if (!dev) { | 2216 | if (!dev) { |
2205 | read_unlock(&dev_base_lock); | 2217 | read_unlock(&dev_base_lock); |
2206 | return -ENODEV; | 2218 | return -ENODEV; |
@@ -2220,7 +2232,7 @@ static int dev_ifname(struct ifreq __user *arg) | |||
2220 | * Thus we will need a 'compatibility mode'. | 2232 | * Thus we will need a 'compatibility mode'. |
2221 | */ | 2233 | */ |
2222 | 2234 | ||
2223 | static int dev_ifconf(char __user *arg) | 2235 | static int dev_ifconf(struct net *net, char __user *arg) |
2224 | { | 2236 | { |
2225 | struct ifconf ifc; | 2237 | struct ifconf ifc; |
2226 | struct net_device *dev; | 2238 | struct net_device *dev; |
@@ -2244,7 +2256,7 @@ static int dev_ifconf(char __user *arg) | |||
2244 | */ | 2256 | */ |
2245 | 2257 | ||
2246 | total = 0; | 2258 | total = 0; |
2247 | for_each_netdev(dev) { | 2259 | for_each_netdev(net, dev) { |
2248 | for (i = 0; i < NPROTO; i++) { | 2260 | for (i = 0; i < NPROTO; i++) { |
2249 | if (gifconf_list[i]) { | 2261 | if (gifconf_list[i]) { |
2250 | int done; | 2262 | int done; |
@@ -2278,6 +2290,7 @@ static int dev_ifconf(char __user *arg) | |||
2278 | */ | 2290 | */ |
2279 | void *dev_seq_start(struct seq_file *seq, loff_t *pos) | 2291 | void *dev_seq_start(struct seq_file *seq, loff_t *pos) |
2280 | { | 2292 | { |
2293 | struct net *net = seq->private; | ||
2281 | loff_t off; | 2294 | loff_t off; |
2282 | struct net_device *dev; | 2295 | struct net_device *dev; |
2283 | 2296 | ||
@@ -2286,7 +2299,7 @@ void *dev_seq_start(struct seq_file *seq, loff_t *pos) | |||
2286 | return SEQ_START_TOKEN; | 2299 | return SEQ_START_TOKEN; |
2287 | 2300 | ||
2288 | off = 1; | 2301 | off = 1; |
2289 | for_each_netdev(dev) | 2302 | for_each_netdev(net, dev) |
2290 | if (off++ == *pos) | 2303 | if (off++ == *pos) |
2291 | return dev; | 2304 | return dev; |
2292 | 2305 | ||
@@ -2295,9 +2308,10 @@ void *dev_seq_start(struct seq_file *seq, loff_t *pos) | |||
2295 | 2308 | ||
2296 | void *dev_seq_next(struct seq_file *seq, void *v, loff_t *pos) | 2309 | void *dev_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
2297 | { | 2310 | { |
2311 | struct net *net = seq->private; | ||
2298 | ++*pos; | 2312 | ++*pos; |
2299 | return v == SEQ_START_TOKEN ? | 2313 | return v == SEQ_START_TOKEN ? |
2300 | first_net_device() : next_net_device((struct net_device *)v); | 2314 | first_net_device(net) : next_net_device((struct net_device *)v); |
2301 | } | 2315 | } |
2302 | 2316 | ||
2303 | void dev_seq_stop(struct seq_file *seq, void *v) | 2317 | void dev_seq_stop(struct seq_file *seq, void *v) |
@@ -2393,7 +2407,22 @@ static const struct seq_operations dev_seq_ops = { | |||
2393 | 2407 | ||
2394 | static int dev_seq_open(struct inode *inode, struct file *file) | 2408 | static int dev_seq_open(struct inode *inode, struct file *file) |
2395 | { | 2409 | { |
2396 | return seq_open(file, &dev_seq_ops); | 2410 | struct seq_file *seq; |
2411 | int res; | ||
2412 | res = seq_open(file, &dev_seq_ops); | ||
2413 | if (!res) { | ||
2414 | seq = file->private_data; | ||
2415 | seq->private = get_net(PROC_NET(inode)); | ||
2416 | } | ||
2417 | return res; | ||
2418 | } | ||
2419 | |||
2420 | static int dev_seq_release(struct inode *inode, struct file *file) | ||
2421 | { | ||
2422 | struct seq_file *seq = file->private_data; | ||
2423 | struct net *net = seq->private; | ||
2424 | put_net(net); | ||
2425 | return seq_release(inode, file); | ||
2397 | } | 2426 | } |
2398 | 2427 | ||
2399 | static const struct file_operations dev_seq_fops = { | 2428 | static const struct file_operations dev_seq_fops = { |
@@ -2401,7 +2430,7 @@ static const struct file_operations dev_seq_fops = { | |||
2401 | .open = dev_seq_open, | 2430 | .open = dev_seq_open, |
2402 | .read = seq_read, | 2431 | .read = seq_read, |
2403 | .llseek = seq_lseek, | 2432 | .llseek = seq_lseek, |
2404 | .release = seq_release, | 2433 | .release = dev_seq_release, |
2405 | }; | 2434 | }; |
2406 | 2435 | ||
2407 | static const struct seq_operations softnet_seq_ops = { | 2436 | static const struct seq_operations softnet_seq_ops = { |
@@ -2553,30 +2582,49 @@ static const struct file_operations ptype_seq_fops = { | |||
2553 | }; | 2582 | }; |
2554 | 2583 | ||
2555 | 2584 | ||
2556 | static int __init dev_proc_init(void) | 2585 | static int dev_proc_net_init(struct net *net) |
2557 | { | 2586 | { |
2558 | int rc = -ENOMEM; | 2587 | int rc = -ENOMEM; |
2559 | 2588 | ||
2560 | if (!proc_net_fops_create(&init_net, "dev", S_IRUGO, &dev_seq_fops)) | 2589 | if (!proc_net_fops_create(net, "dev", S_IRUGO, &dev_seq_fops)) |
2561 | goto out; | 2590 | goto out; |
2562 | if (!proc_net_fops_create(&init_net, "softnet_stat", S_IRUGO, &softnet_seq_fops)) | 2591 | if (!proc_net_fops_create(net, "softnet_stat", S_IRUGO, &softnet_seq_fops)) |
2563 | goto out_dev; | 2592 | goto out_dev; |
2564 | if (!proc_net_fops_create(&init_net, "ptype", S_IRUGO, &ptype_seq_fops)) | 2593 | if (!proc_net_fops_create(net, "ptype", S_IRUGO, &ptype_seq_fops)) |
2565 | goto out_softnet; | 2594 | goto out_softnet; |
2566 | 2595 | ||
2567 | if (wext_proc_init()) | 2596 | if (wext_proc_init(net)) |
2568 | goto out_ptype; | 2597 | goto out_ptype; |
2569 | rc = 0; | 2598 | rc = 0; |
2570 | out: | 2599 | out: |
2571 | return rc; | 2600 | return rc; |
2572 | out_ptype: | 2601 | out_ptype: |
2573 | proc_net_remove(&init_net, "ptype"); | 2602 | proc_net_remove(net, "ptype"); |
2574 | out_softnet: | 2603 | out_softnet: |
2575 | proc_net_remove(&init_net, "softnet_stat"); | 2604 | proc_net_remove(net, "softnet_stat"); |
2576 | out_dev: | 2605 | out_dev: |
2577 | proc_net_remove(&init_net, "dev"); | 2606 | proc_net_remove(net, "dev"); |
2578 | goto out; | 2607 | goto out; |
2579 | } | 2608 | } |
2609 | |||
2610 | static void dev_proc_net_exit(struct net *net) | ||
2611 | { | ||
2612 | wext_proc_exit(net); | ||
2613 | |||
2614 | proc_net_remove(net, "ptype"); | ||
2615 | proc_net_remove(net, "softnet_stat"); | ||
2616 | proc_net_remove(net, "dev"); | ||
2617 | } | ||
2618 | |||
2619 | static struct pernet_operations dev_proc_ops = { | ||
2620 | .init = dev_proc_net_init, | ||
2621 | .exit = dev_proc_net_exit, | ||
2622 | }; | ||
2623 | |||
2624 | static int __init dev_proc_init(void) | ||
2625 | { | ||
2626 | return register_pernet_subsys(&dev_proc_ops); | ||
2627 | } | ||
2580 | #else | 2628 | #else |
2581 | #define dev_proc_init() 0 | 2629 | #define dev_proc_init() 0 |
2582 | #endif /* CONFIG_PROC_FS */ | 2630 | #endif /* CONFIG_PROC_FS */ |
@@ -3011,10 +3059,10 @@ int dev_set_mac_address(struct net_device *dev, struct sockaddr *sa) | |||
3011 | /* | 3059 | /* |
3012 | * Perform the SIOCxIFxxx calls. | 3060 | * Perform the SIOCxIFxxx calls. |
3013 | */ | 3061 | */ |
3014 | static int dev_ifsioc(struct ifreq *ifr, unsigned int cmd) | 3062 | static int dev_ifsioc(struct net *net, struct ifreq *ifr, unsigned int cmd) |
3015 | { | 3063 | { |
3016 | int err; | 3064 | int err; |
3017 | struct net_device *dev = __dev_get_by_name(ifr->ifr_name); | 3065 | struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name); |
3018 | 3066 | ||
3019 | if (!dev) | 3067 | if (!dev) |
3020 | return -ENODEV; | 3068 | return -ENODEV; |
@@ -3167,7 +3215,7 @@ static int dev_ifsioc(struct ifreq *ifr, unsigned int cmd) | |||
3167 | * positive or a negative errno code on error. | 3215 | * positive or a negative errno code on error. |
3168 | */ | 3216 | */ |
3169 | 3217 | ||
3170 | int dev_ioctl(unsigned int cmd, void __user *arg) | 3218 | int dev_ioctl(struct net *net, unsigned int cmd, void __user *arg) |
3171 | { | 3219 | { |
3172 | struct ifreq ifr; | 3220 | struct ifreq ifr; |
3173 | int ret; | 3221 | int ret; |
@@ -3180,12 +3228,12 @@ int dev_ioctl(unsigned int cmd, void __user *arg) | |||
3180 | 3228 | ||
3181 | if (cmd == SIOCGIFCONF) { | 3229 | if (cmd == SIOCGIFCONF) { |
3182 | rtnl_lock(); | 3230 | rtnl_lock(); |
3183 | ret = dev_ifconf((char __user *) arg); | 3231 | ret = dev_ifconf(net, (char __user *) arg); |
3184 | rtnl_unlock(); | 3232 | rtnl_unlock(); |
3185 | return ret; | 3233 | return ret; |
3186 | } | 3234 | } |
3187 | if (cmd == SIOCGIFNAME) | 3235 | if (cmd == SIOCGIFNAME) |
3188 | return dev_ifname((struct ifreq __user *)arg); | 3236 | return dev_ifname(net, (struct ifreq __user *)arg); |
3189 | 3237 | ||
3190 | if (copy_from_user(&ifr, arg, sizeof(struct ifreq))) | 3238 | if (copy_from_user(&ifr, arg, sizeof(struct ifreq))) |
3191 | return -EFAULT; | 3239 | return -EFAULT; |
@@ -3215,9 +3263,9 @@ int dev_ioctl(unsigned int cmd, void __user *arg) | |||
3215 | case SIOCGIFMAP: | 3263 | case SIOCGIFMAP: |
3216 | case SIOCGIFINDEX: | 3264 | case SIOCGIFINDEX: |
3217 | case SIOCGIFTXQLEN: | 3265 | case SIOCGIFTXQLEN: |
3218 | dev_load(ifr.ifr_name); | 3266 | dev_load(net, ifr.ifr_name); |
3219 | read_lock(&dev_base_lock); | 3267 | read_lock(&dev_base_lock); |
3220 | ret = dev_ifsioc(&ifr, cmd); | 3268 | ret = dev_ifsioc(net, &ifr, cmd); |
3221 | read_unlock(&dev_base_lock); | 3269 | read_unlock(&dev_base_lock); |
3222 | if (!ret) { | 3270 | if (!ret) { |
3223 | if (colon) | 3271 | if (colon) |
@@ -3229,9 +3277,9 @@ int dev_ioctl(unsigned int cmd, void __user *arg) | |||
3229 | return ret; | 3277 | return ret; |
3230 | 3278 | ||
3231 | case SIOCETHTOOL: | 3279 | case SIOCETHTOOL: |
3232 | dev_load(ifr.ifr_name); | 3280 | dev_load(net, ifr.ifr_name); |
3233 | rtnl_lock(); | 3281 | rtnl_lock(); |
3234 | ret = dev_ethtool(&ifr); | 3282 | ret = dev_ethtool(net, &ifr); |
3235 | rtnl_unlock(); | 3283 | rtnl_unlock(); |
3236 | if (!ret) { | 3284 | if (!ret) { |
3237 | if (colon) | 3285 | if (colon) |
@@ -3253,9 +3301,9 @@ int dev_ioctl(unsigned int cmd, void __user *arg) | |||
3253 | case SIOCSIFNAME: | 3301 | case SIOCSIFNAME: |
3254 | if (!capable(CAP_NET_ADMIN)) | 3302 | if (!capable(CAP_NET_ADMIN)) |
3255 | return -EPERM; | 3303 | return -EPERM; |
3256 | dev_load(ifr.ifr_name); | 3304 | dev_load(net, ifr.ifr_name); |
3257 | rtnl_lock(); | 3305 | rtnl_lock(); |
3258 | ret = dev_ifsioc(&ifr, cmd); | 3306 | ret = dev_ifsioc(net, &ifr, cmd); |
3259 | rtnl_unlock(); | 3307 | rtnl_unlock(); |
3260 | if (!ret) { | 3308 | if (!ret) { |
3261 | if (colon) | 3309 | if (colon) |
@@ -3294,9 +3342,9 @@ int dev_ioctl(unsigned int cmd, void __user *arg) | |||
3294 | /* fall through */ | 3342 | /* fall through */ |
3295 | case SIOCBONDSLAVEINFOQUERY: | 3343 | case SIOCBONDSLAVEINFOQUERY: |
3296 | case SIOCBONDINFOQUERY: | 3344 | case SIOCBONDINFOQUERY: |
3297 | dev_load(ifr.ifr_name); | 3345 | dev_load(net, ifr.ifr_name); |
3298 | rtnl_lock(); | 3346 | rtnl_lock(); |
3299 | ret = dev_ifsioc(&ifr, cmd); | 3347 | ret = dev_ifsioc(net, &ifr, cmd); |
3300 | rtnl_unlock(); | 3348 | rtnl_unlock(); |
3301 | return ret; | 3349 | return ret; |
3302 | 3350 | ||
@@ -3316,9 +3364,9 @@ int dev_ioctl(unsigned int cmd, void __user *arg) | |||
3316 | if (cmd == SIOCWANDEV || | 3364 | if (cmd == SIOCWANDEV || |
3317 | (cmd >= SIOCDEVPRIVATE && | 3365 | (cmd >= SIOCDEVPRIVATE && |
3318 | cmd <= SIOCDEVPRIVATE + 15)) { | 3366 | cmd <= SIOCDEVPRIVATE + 15)) { |
3319 | dev_load(ifr.ifr_name); | 3367 | dev_load(net, ifr.ifr_name); |
3320 | rtnl_lock(); | 3368 | rtnl_lock(); |
3321 | ret = dev_ifsioc(&ifr, cmd); | 3369 | ret = dev_ifsioc(net, &ifr, cmd); |
3322 | rtnl_unlock(); | 3370 | rtnl_unlock(); |
3323 | if (!ret && copy_to_user(arg, &ifr, | 3371 | if (!ret && copy_to_user(arg, &ifr, |
3324 | sizeof(struct ifreq))) | 3372 | sizeof(struct ifreq))) |
@@ -3327,7 +3375,7 @@ int dev_ioctl(unsigned int cmd, void __user *arg) | |||
3327 | } | 3375 | } |
3328 | /* Take care of Wireless Extensions */ | 3376 | /* Take care of Wireless Extensions */ |
3329 | if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) | 3377 | if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) |
3330 | return wext_handle_ioctl(&ifr, cmd, arg); | 3378 | return wext_handle_ioctl(net, &ifr, cmd, arg); |
3331 | return -EINVAL; | 3379 | return -EINVAL; |
3332 | } | 3380 | } |
3333 | } | 3381 | } |
@@ -3340,19 +3388,17 @@ int dev_ioctl(unsigned int cmd, void __user *arg) | |||
3340 | * number. The caller must hold the rtnl semaphore or the | 3388 | * number. The caller must hold the rtnl semaphore or the |
3341 | * dev_base_lock to be sure it remains unique. | 3389 | * dev_base_lock to be sure it remains unique. |
3342 | */ | 3390 | */ |
3343 | static int dev_new_index(void) | 3391 | static int dev_new_index(struct net *net) |
3344 | { | 3392 | { |
3345 | static int ifindex; | 3393 | static int ifindex; |
3346 | for (;;) { | 3394 | for (;;) { |
3347 | if (++ifindex <= 0) | 3395 | if (++ifindex <= 0) |
3348 | ifindex = 1; | 3396 | ifindex = 1; |
3349 | if (!__dev_get_by_index(ifindex)) | 3397 | if (!__dev_get_by_index(net, ifindex)) |
3350 | return ifindex; | 3398 | return ifindex; |
3351 | } | 3399 | } |
3352 | } | 3400 | } |
3353 | 3401 | ||
3354 | static int dev_boot_phase = 1; | ||
3355 | |||
3356 | /* Delayed registration/unregisteration */ | 3402 | /* Delayed registration/unregisteration */ |
3357 | static DEFINE_SPINLOCK(net_todo_list_lock); | 3403 | static DEFINE_SPINLOCK(net_todo_list_lock); |
3358 | static struct list_head net_todo_list = LIST_HEAD_INIT(net_todo_list); | 3404 | static struct list_head net_todo_list = LIST_HEAD_INIT(net_todo_list); |
@@ -3386,6 +3432,7 @@ int register_netdevice(struct net_device *dev) | |||
3386 | struct hlist_head *head; | 3432 | struct hlist_head *head; |
3387 | struct hlist_node *p; | 3433 | struct hlist_node *p; |
3388 | int ret; | 3434 | int ret; |
3435 | struct net *net; | ||
3389 | 3436 | ||
3390 | BUG_ON(dev_boot_phase); | 3437 | BUG_ON(dev_boot_phase); |
3391 | ASSERT_RTNL(); | 3438 | ASSERT_RTNL(); |
@@ -3394,6 +3441,8 @@ int register_netdevice(struct net_device *dev) | |||
3394 | 3441 | ||
3395 | /* When net_device's are persistent, this will be fatal. */ | 3442 | /* When net_device's are persistent, this will be fatal. */ |
3396 | BUG_ON(dev->reg_state != NETREG_UNINITIALIZED); | 3443 | BUG_ON(dev->reg_state != NETREG_UNINITIALIZED); |
3444 | BUG_ON(!dev->nd_net); | ||
3445 | net = dev->nd_net; | ||
3397 | 3446 | ||
3398 | spin_lock_init(&dev->queue_lock); | 3447 | spin_lock_init(&dev->queue_lock); |
3399 | spin_lock_init(&dev->_xmit_lock); | 3448 | spin_lock_init(&dev->_xmit_lock); |
@@ -3418,12 +3467,12 @@ int register_netdevice(struct net_device *dev) | |||
3418 | goto err_uninit; | 3467 | goto err_uninit; |
3419 | } | 3468 | } |
3420 | 3469 | ||
3421 | dev->ifindex = dev_new_index(); | 3470 | dev->ifindex = dev_new_index(net); |
3422 | if (dev->iflink == -1) | 3471 | if (dev->iflink == -1) |
3423 | dev->iflink = dev->ifindex; | 3472 | dev->iflink = dev->ifindex; |
3424 | 3473 | ||
3425 | /* Check for existence of name */ | 3474 | /* Check for existence of name */ |
3426 | head = dev_name_hash(dev->name); | 3475 | head = dev_name_hash(net, dev->name); |
3427 | hlist_for_each(p, head) { | 3476 | hlist_for_each(p, head) { |
3428 | struct net_device *d | 3477 | struct net_device *d |
3429 | = hlist_entry(p, struct net_device, name_hlist); | 3478 | = hlist_entry(p, struct net_device, name_hlist); |
@@ -3501,9 +3550,9 @@ int register_netdevice(struct net_device *dev) | |||
3501 | 3550 | ||
3502 | dev_init_scheduler(dev); | 3551 | dev_init_scheduler(dev); |
3503 | write_lock_bh(&dev_base_lock); | 3552 | write_lock_bh(&dev_base_lock); |
3504 | list_add_tail(&dev->dev_list, &dev_base_head); | 3553 | list_add_tail(&dev->dev_list, &net->dev_base_head); |
3505 | hlist_add_head(&dev->name_hlist, head); | 3554 | hlist_add_head(&dev->name_hlist, head); |
3506 | hlist_add_head(&dev->index_hlist, dev_index_hash(dev->ifindex)); | 3555 | hlist_add_head(&dev->index_hlist, dev_index_hash(net, dev->ifindex)); |
3507 | dev_hold(dev); | 3556 | dev_hold(dev); |
3508 | write_unlock_bh(&dev_base_lock); | 3557 | write_unlock_bh(&dev_base_lock); |
3509 | 3558 | ||
@@ -4067,6 +4116,45 @@ int netdev_compute_features(unsigned long all, unsigned long one) | |||
4067 | } | 4116 | } |
4068 | EXPORT_SYMBOL(netdev_compute_features); | 4117 | EXPORT_SYMBOL(netdev_compute_features); |
4069 | 4118 | ||
4119 | /* Initialize per network namespace state */ | ||
4120 | static int netdev_init(struct net *net) | ||
4121 | { | ||
4122 | int i; | ||
4123 | INIT_LIST_HEAD(&net->dev_base_head); | ||
4124 | rwlock_init(&dev_base_lock); | ||
4125 | |||
4126 | net->dev_name_head = kmalloc( | ||
4127 | sizeof(*net->dev_name_head)*NETDEV_HASHENTRIES, GFP_KERNEL); | ||
4128 | if (!net->dev_name_head) | ||
4129 | return -ENOMEM; | ||
4130 | |||
4131 | net->dev_index_head = kmalloc( | ||
4132 | sizeof(*net->dev_index_head)*NETDEV_HASHENTRIES, GFP_KERNEL); | ||
4133 | if (!net->dev_index_head) { | ||
4134 | kfree(net->dev_name_head); | ||
4135 | return -ENOMEM; | ||
4136 | } | ||
4137 | |||
4138 | for (i = 0; i < NETDEV_HASHENTRIES; i++) | ||
4139 | INIT_HLIST_HEAD(&net->dev_name_head[i]); | ||
4140 | |||
4141 | for (i = 0; i < NETDEV_HASHENTRIES; i++) | ||
4142 | INIT_HLIST_HEAD(&net->dev_index_head[i]); | ||
4143 | |||
4144 | return 0; | ||
4145 | } | ||
4146 | |||
4147 | static void netdev_exit(struct net *net) | ||
4148 | { | ||
4149 | kfree(net->dev_name_head); | ||
4150 | kfree(net->dev_index_head); | ||
4151 | } | ||
4152 | |||
4153 | static struct pernet_operations netdev_net_ops = { | ||
4154 | .init = netdev_init, | ||
4155 | .exit = netdev_exit, | ||
4156 | }; | ||
4157 | |||
4070 | /* | 4158 | /* |
4071 | * Initialize the DEV module. At boot time this walks the device list and | 4159 | * Initialize the DEV module. At boot time this walks the device list and |
4072 | * unhooks any devices that fail to initialise (normally hardware not | 4160 | * unhooks any devices that fail to initialise (normally hardware not |
@@ -4094,11 +4182,8 @@ static int __init net_dev_init(void) | |||
4094 | for (i = 0; i < 16; i++) | 4182 | for (i = 0; i < 16; i++) |
4095 | INIT_LIST_HEAD(&ptype_base[i]); | 4183 | INIT_LIST_HEAD(&ptype_base[i]); |
4096 | 4184 | ||
4097 | for (i = 0; i < ARRAY_SIZE(dev_name_head); i++) | 4185 | if (register_pernet_subsys(&netdev_net_ops)) |
4098 | INIT_HLIST_HEAD(&dev_name_head[i]); | 4186 | goto out; |
4099 | |||
4100 | for (i = 0; i < ARRAY_SIZE(dev_index_head); i++) | ||
4101 | INIT_HLIST_HEAD(&dev_index_head[i]); | ||
4102 | 4187 | ||
4103 | /* | 4188 | /* |
4104 | * Initialise the packet receive queues. | 4189 | * Initialise the packet receive queues. |