bridge: change console message interface
[linux-2.6.git] / net / bridge / br_ioctl.c
index b8ce14b..cb43312 100644 (file)
@@ -5,29 +5,32 @@
  *     Authors:
  *     Lennert Buytenhek               <buytenh@gnu.org>
  *
- *     $Id: br_ioctl.c,v 1.4 2000/11/08 05:16:40 davem Exp $
- *
  *     This program is free software; you can redistribute it and/or
  *     modify it under the terms of the GNU General Public License
  *     as published by the Free Software Foundation; either version
  *     2 of the License, or (at your option) any later version.
  */
 
+#include <linux/capability.h>
 #include <linux/kernel.h>
 #include <linux/if_bridge.h>
 #include <linux/netdevice.h>
+#include <linux/slab.h>
 #include <linux/times.h>
+#include <net/net_namespace.h>
 #include <asm/uaccess.h>
 #include "br_private.h"
 
 /* called with RTNL */
-static int get_bridge_ifindices(int *indices, int num)
+static int get_bridge_ifindices(struct net *net, int *indices, int num)
 {
        struct net_device *dev;
        int i = 0;
 
-       for (dev = dev_base; dev && i < num; dev = dev->next) {
-               if (dev->priv_flags & IFF_EBRIDGE) 
+       for_each_netdev(net, dev) {
+               if (i >= num)
+                       break;
+               if (dev->priv_flags & IFF_EBRIDGE)
                        indices[i++] = dev->ifindex;
        }
 
@@ -52,22 +55,23 @@ static void get_port_ifindices(struct net_bridge *br, int *ifindices, int num)
  *            (limited to a page for sanity)
  * offset  -- number of records to skip
  */
-static int get_fdb_entries(struct net_bridge *br, void __user *userbuf, 
+static int get_fdb_entries(struct net_bridge *br, void __user *userbuf,
                           unsigned long maxnum, unsigned long offset)
 {
        int num;
        void *buf;
-       size_t size = maxnum * sizeof(struct __fdb_entry);
+       size_t size;
 
-       if (size > PAGE_SIZE) {
-               size = PAGE_SIZE;
+       /* Clamp size to PAGE_SIZE, test maxnum to avoid overflow */
+       if (maxnum > PAGE_SIZE/sizeof(struct __fdb_entry))
                maxnum = PAGE_SIZE/sizeof(struct __fdb_entry);
-       }
+
+       size = maxnum * sizeof(struct __fdb_entry);
 
        buf = kmalloc(size, GFP_USER);
        if (!buf)
                return -ENOMEM;
-       
+
        num = br_fdb_fillbuf(br, buf, maxnum, offset);
        if (num > 0) {
                if (copy_to_user(userbuf, buf, num*sizeof(struct __fdb_entry)))
@@ -78,6 +82,7 @@ static int get_fdb_entries(struct net_bridge *br, void __user *userbuf,
        return num;
 }
 
+/* called with RTNL */
 static int add_del_if(struct net_bridge *br, int ifindex, int isadd)
 {
        struct net_device *dev;
@@ -86,16 +91,15 @@ static int add_del_if(struct net_bridge *br, int ifindex, int isadd)
        if (!capable(CAP_NET_ADMIN))
                return -EPERM;
 
-       dev = dev_get_by_index(ifindex);
+       dev = __dev_get_by_index(dev_net(br->dev), ifindex);
        if (dev == NULL)
                return -EINVAL;
-       
+
        if (isadd)
                ret = br_add_if(br, dev);
        else
                ret = br_del_if(br, dev);
 
-       dev_put(dev);
        return ret;
 }
 
@@ -108,7 +112,7 @@ static int old_dev_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
 {
        struct net_bridge *br = netdev_priv(dev);
        unsigned long args[4];
-       
+
        if (copy_from_user(args, rq->ifr_data, sizeof(args)))
                return -EFAULT;
 
@@ -135,13 +139,14 @@ static int old_dev_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
                b.topology_change = br->topology_change;
                b.topology_change_detected = br->topology_change_detected;
                b.root_port = br->root_port;
-               b.stp_enabled = br->stp_enabled;
+
+               b.stp_enabled = (br->stp_enabled != BR_NO_STP);
                b.ageing_time = jiffies_to_clock_t(br->ageing_time);
                b.hello_timer_value = br_timer_value(&br->hello_timer);
                b.tcn_timer_value = br_timer_value(&br->tcn_timer);
                b.topology_change_timer_value = br_timer_value(&br->topology_change_timer);
                b.gc_timer_value = br_timer_value(&br->gc_timer);
-               rcu_read_unlock();
+               rcu_read_unlock();
 
                if (copy_to_user((void __user *)args[1], &b, sizeof(b)))
                        return -EFAULT;
@@ -161,12 +166,10 @@ static int old_dev_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
                if (num > BR_MAX_PORTS)
                        num = BR_MAX_PORTS;
 
-               indices = kmalloc(num*sizeof(int), GFP_KERNEL);
+               indices = kcalloc(num, sizeof(int), GFP_KERNEL);
                if (indices == NULL)
                        return -ENOMEM;
 
-               memset(indices, 0, num*sizeof(int));
-
                get_port_ifindices(br, indices, num);
                if (copy_to_user((void __user *)args[1], indices, num*sizeof(int)))
                        num =  -EFAULT;
@@ -186,15 +189,21 @@ static int old_dev_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
                return 0;
 
        case BRCTL_SET_BRIDGE_HELLO_TIME:
+       {
+               unsigned long t = clock_t_to_jiffies(args[1]);
                if (!capable(CAP_NET_ADMIN))
                        return -EPERM;
 
+               if (t < HZ)
+                       return -EINVAL;
+
                spin_lock_bh(&br->lock);
-               br->bridge_hello_time = clock_t_to_jiffies(args[1]);
+               br->bridge_hello_time = t;
                if (br_is_root_bridge(br))
                        br->hello_time = br->bridge_hello_time;
                spin_unlock_bh(&br->lock);
                return 0;
+       }
 
        case BRCTL_SET_BRIDGE_MAX_AGE:
                if (!capable(CAP_NET_ADMIN))
@@ -251,7 +260,7 @@ static int old_dev_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
                if (!capable(CAP_NET_ADMIN))
                        return -EPERM;
 
-               br->stp_enabled = args[1]?1:0;
+               br_stp_set_enabled(br, args[1]);
                return 0;
 
        case BRCTL_SET_BRIDGE_PRIORITY:
@@ -275,7 +284,7 @@ static int old_dev_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
                        return -ERANGE;
 
                spin_lock_bh(&br->lock);
-               if ((p = br_get_port(br, args[1])) == NULL) 
+               if ((p = br_get_port(br, args[1])) == NULL)
                        ret = -EINVAL;
                else
                        br_stp_set_port_priority(p, args[2]);
@@ -291,24 +300,23 @@ static int old_dev_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
                if (!capable(CAP_NET_ADMIN))
                        return -EPERM;
 
-               spin_lock_bh(&br->lock);
                if ((p = br_get_port(br, args[1])) == NULL)
                        ret = -EINVAL;
                else
                        br_stp_set_path_cost(p, args[2]);
-               spin_unlock_bh(&br->lock);
+
                return ret;
        }
 
        case BRCTL_GET_FDB_ENTRIES:
-               return get_fdb_entries(br, (void __user *)args[1], 
+               return get_fdb_entries(br, (void __user *)args[1],
                                       args[2], args[3]);
        }
 
        return -EOPNOTSUPP;
 }
 
-static int old_deviceless(void __user *uarg)
+static int old_deviceless(struct net *net, void __user *uarg)
 {
        unsigned long args[3];
 
@@ -326,12 +334,11 @@ static int old_deviceless(void __user *uarg)
 
                if (args[2] >= 2048)
                        return -ENOMEM;
-               indices = kmalloc(args[2]*sizeof(int), GFP_KERNEL);
+               indices = kcalloc(args[2], sizeof(int), GFP_KERNEL);
                if (indices == NULL)
                        return -ENOMEM;
 
-               memset(indices, 0, args[2]*sizeof(int));
-               args[2] = get_bridge_ifindices(indices, args[2]);
+               args[2] = get_bridge_ifindices(net, indices, args[2]);
 
                ret = copy_to_user((void __user *)args[1], indices, args[2]*sizeof(int))
                        ? -EFAULT : args[2];
@@ -354,22 +361,22 @@ static int old_deviceless(void __user *uarg)
                buf[IFNAMSIZ-1] = 0;
 
                if (args[0] == BRCTL_ADD_BRIDGE)
-                       return br_add_bridge(buf);
+                       return br_add_bridge(net, buf);
 
-               return br_del_bridge(buf);
+               return br_del_bridge(net, buf);
        }
        }
 
        return -EOPNOTSUPP;
 }
 
-int br_ioctl_deviceless_stub(unsigned int cmd, void __user *uarg)
+int br_ioctl_deviceless_stub(struct net *net, unsigned int cmd, void __user *uarg)
 {
        switch (cmd) {
        case SIOCGIFBR:
        case SIOCSIFBR:
-               return old_deviceless(uarg);
-               
+               return old_deviceless(net, uarg);
+
        case SIOCBRADDBR:
        case SIOCBRDELBR:
        {
@@ -383,9 +390,9 @@ int br_ioctl_deviceless_stub(unsigned int cmd, void __user *uarg)
 
                buf[IFNAMSIZ-1] = 0;
                if (cmd == SIOCBRADDBR)
-                       return br_add_bridge(buf);
+                       return br_add_bridge(net, buf);
 
-               return br_del_bridge(buf);
+               return br_del_bridge(net, buf);
        }
        }
        return -EOPNOTSUPP;
@@ -405,6 +412,6 @@ int br_dev_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
 
        }
 
-       pr_debug("Bridge does not support ioctl 0x%x\n", cmd);
+       br_debug(br, "Bridge does not support ioctl 0x%x\n", cmd);
        return -EOPNOTSUPP;
 }