xref: /linux/net/bridge/br_ioctl.c (revision 561d8352818f2ca79c13471f245b992398902090)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  *	Ioctl handler
41da177e4SLinus Torvalds  *	Linux ethernet bridge
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  *	Authors:
71da177e4SLinus Torvalds  *	Lennert Buytenhek		<buytenh@gnu.org>
81da177e4SLinus Torvalds  */
91da177e4SLinus Torvalds 
104fc268d2SRandy Dunlap #include <linux/capability.h>
111da177e4SLinus Torvalds #include <linux/kernel.h>
121da177e4SLinus Torvalds #include <linux/if_bridge.h>
131da177e4SLinus Torvalds #include <linux/netdevice.h>
145a0e3ad6STejun Heo #include <linux/slab.h>
151da177e4SLinus Torvalds #include <linux/times.h>
16881d966bSEric W. Biederman #include <net/net_namespace.h>
177c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
181da177e4SLinus Torvalds #include "br_private.h"
191da177e4SLinus Torvalds 
204aa678baSAlexey Dobriyan static int get_bridge_ifindices(struct net *net, int *indices, int num)
211da177e4SLinus Torvalds {
221da177e4SLinus Torvalds 	struct net_device *dev;
231da177e4SLinus Torvalds 	int i = 0;
241da177e4SLinus Torvalds 
2531ca0458SNikolay Aleksandrov 	rcu_read_lock();
2631ca0458SNikolay Aleksandrov 	for_each_netdev_rcu(net, dev) {
277562f876SPavel Emelianov 		if (i >= num)
287562f876SPavel Emelianov 			break;
291da177e4SLinus Torvalds 		if (dev->priv_flags & IFF_EBRIDGE)
301da177e4SLinus Torvalds 			indices[i++] = dev->ifindex;
311da177e4SLinus Torvalds 	}
3231ca0458SNikolay Aleksandrov 	rcu_read_unlock();
331da177e4SLinus Torvalds 
341da177e4SLinus Torvalds 	return i;
351da177e4SLinus Torvalds }
361da177e4SLinus Torvalds 
371da177e4SLinus Torvalds /* called with RTNL */
381da177e4SLinus Torvalds static void get_port_ifindices(struct net_bridge *br, int *ifindices, int num)
391da177e4SLinus Torvalds {
401da177e4SLinus Torvalds 	struct net_bridge_port *p;
411da177e4SLinus Torvalds 
421da177e4SLinus Torvalds 	list_for_each_entry(p, &br->port_list, list) {
431da177e4SLinus Torvalds 		if (p->port_no < num)
441da177e4SLinus Torvalds 			ifindices[p->port_no] = p->dev->ifindex;
451da177e4SLinus Torvalds 	}
461da177e4SLinus Torvalds }
471da177e4SLinus Torvalds 
481da177e4SLinus Torvalds /*
491da177e4SLinus Torvalds  * Format up to a page worth of forwarding table entries
501da177e4SLinus Torvalds  * userbuf -- where to copy result
511da177e4SLinus Torvalds  * maxnum  -- maximum number of entries desired
521da177e4SLinus Torvalds  *            (limited to a page for sanity)
531da177e4SLinus Torvalds  * offset  -- number of records to skip
541da177e4SLinus Torvalds  */
551da177e4SLinus Torvalds static int get_fdb_entries(struct net_bridge *br, void __user *userbuf,
561da177e4SLinus Torvalds 			   unsigned long maxnum, unsigned long offset)
571da177e4SLinus Torvalds {
581da177e4SLinus Torvalds 	int num;
591da177e4SLinus Torvalds 	void *buf;
60ba8379b2SChris Wright 	size_t size;
611da177e4SLinus Torvalds 
62ba8379b2SChris Wright 	/* Clamp size to PAGE_SIZE, test maxnum to avoid overflow */
63ba8379b2SChris Wright 	if (maxnum > PAGE_SIZE/sizeof(struct __fdb_entry))
641da177e4SLinus Torvalds 		maxnum = PAGE_SIZE/sizeof(struct __fdb_entry);
65ba8379b2SChris Wright 
66ba8379b2SChris Wright 	size = maxnum * sizeof(struct __fdb_entry);
671da177e4SLinus Torvalds 
681da177e4SLinus Torvalds 	buf = kmalloc(size, GFP_USER);
691da177e4SLinus Torvalds 	if (!buf)
701da177e4SLinus Torvalds 		return -ENOMEM;
711da177e4SLinus Torvalds 
721da177e4SLinus Torvalds 	num = br_fdb_fillbuf(br, buf, maxnum, offset);
731da177e4SLinus Torvalds 	if (num > 0) {
741da177e4SLinus Torvalds 		if (copy_to_user(userbuf, buf, num*sizeof(struct __fdb_entry)))
751da177e4SLinus Torvalds 			num = -EFAULT;
761da177e4SLinus Torvalds 	}
771da177e4SLinus Torvalds 	kfree(buf);
781da177e4SLinus Torvalds 
791da177e4SLinus Torvalds 	return num;
801da177e4SLinus Torvalds }
811da177e4SLinus Torvalds 
8231ef30c7SEric Dumazet /* called with RTNL */
831da177e4SLinus Torvalds static int add_del_if(struct net_bridge *br, int ifindex, int isadd)
841da177e4SLinus Torvalds {
85cb990503SEric W. Biederman 	struct net *net = dev_net(br->dev);
861da177e4SLinus Torvalds 	struct net_device *dev;
871da177e4SLinus Torvalds 	int ret;
881da177e4SLinus Torvalds 
89cb990503SEric W. Biederman 	if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
901da177e4SLinus Torvalds 		return -EPERM;
911da177e4SLinus Torvalds 
92cb990503SEric W. Biederman 	dev = __dev_get_by_index(net, ifindex);
931da177e4SLinus Torvalds 	if (dev == NULL)
941da177e4SLinus Torvalds 		return -EINVAL;
951da177e4SLinus Torvalds 
961da177e4SLinus Torvalds 	if (isadd)
97ca752be0SDavid Ahern 		ret = br_add_if(br, dev, NULL);
98eccaa9e5SDavid S. Miller 	else
991da177e4SLinus Torvalds 		ret = br_del_if(br, dev);
1001da177e4SLinus Torvalds 
1011da177e4SLinus Torvalds 	return ret;
1021da177e4SLinus Torvalds }
1031da177e4SLinus Torvalds 
1041da177e4SLinus Torvalds /*
1051da177e4SLinus Torvalds  * Legacy ioctl's through SIOCDEVPRIVATE
1064bbd026cSRandy Dunlap  * This interface is deprecated because it was too difficult
10725985edcSLucas De Marchi  * to do the translation for 32/64bit ioctl compatibility.
1081da177e4SLinus Torvalds  */
109*561d8352SArnd Bergmann int br_dev_siocdevprivate(struct net_device *dev, struct ifreq *rq, void __user *data, int cmd)
1101da177e4SLinus Torvalds {
1111da177e4SLinus Torvalds 	struct net_bridge *br = netdev_priv(dev);
112bf871ad7SXin Long 	struct net_bridge_port *p = NULL;
1131da177e4SLinus Torvalds 	unsigned long args[4];
114*561d8352SArnd Bergmann 	void __user *argp;
115bf871ad7SXin Long 	int ret = -EOPNOTSUPP;
1161da177e4SLinus Torvalds 
117*561d8352SArnd Bergmann 	if (in_compat_syscall()) {
118*561d8352SArnd Bergmann 		unsigned int cargs[4];
119*561d8352SArnd Bergmann 
120*561d8352SArnd Bergmann 		if (copy_from_user(cargs, data, sizeof(cargs)))
1211da177e4SLinus Torvalds 			return -EFAULT;
1221da177e4SLinus Torvalds 
123*561d8352SArnd Bergmann 		args[0] = cargs[0];
124*561d8352SArnd Bergmann 		args[1] = cargs[1];
125*561d8352SArnd Bergmann 		args[2] = cargs[2];
126*561d8352SArnd Bergmann 		args[3] = cargs[3];
127*561d8352SArnd Bergmann 
128*561d8352SArnd Bergmann 		argp = compat_ptr(args[1]);
129*561d8352SArnd Bergmann 	} else {
130*561d8352SArnd Bergmann 		if (copy_from_user(args, data, sizeof(args)))
131*561d8352SArnd Bergmann 			return -EFAULT;
132*561d8352SArnd Bergmann 
133*561d8352SArnd Bergmann 		argp = (void __user *)args[1];
134*561d8352SArnd Bergmann 	}
135*561d8352SArnd Bergmann 
1361da177e4SLinus Torvalds 	switch (args[0]) {
1371da177e4SLinus Torvalds 	case BRCTL_ADD_IF:
1381da177e4SLinus Torvalds 	case BRCTL_DEL_IF:
1391da177e4SLinus Torvalds 		return add_del_if(br, args[1], args[0] == BRCTL_ADD_IF);
1401da177e4SLinus Torvalds 
1411da177e4SLinus Torvalds 	case BRCTL_GET_BRIDGE_INFO:
1421da177e4SLinus Torvalds 	{
1431da177e4SLinus Torvalds 		struct __bridge_info b;
1441da177e4SLinus Torvalds 
1451da177e4SLinus Torvalds 		memset(&b, 0, sizeof(struct __bridge_info));
1461da177e4SLinus Torvalds 		rcu_read_lock();
1471da177e4SLinus Torvalds 		memcpy(&b.designated_root, &br->designated_root, 8);
1481da177e4SLinus Torvalds 		memcpy(&b.bridge_id, &br->bridge_id, 8);
1491da177e4SLinus Torvalds 		b.root_path_cost = br->root_path_cost;
1501da177e4SLinus Torvalds 		b.max_age = jiffies_to_clock_t(br->max_age);
1511da177e4SLinus Torvalds 		b.hello_time = jiffies_to_clock_t(br->hello_time);
1521da177e4SLinus Torvalds 		b.forward_delay = br->forward_delay;
1531da177e4SLinus Torvalds 		b.bridge_max_age = br->bridge_max_age;
1541da177e4SLinus Torvalds 		b.bridge_hello_time = br->bridge_hello_time;
1551da177e4SLinus Torvalds 		b.bridge_forward_delay = jiffies_to_clock_t(br->bridge_forward_delay);
1561da177e4SLinus Torvalds 		b.topology_change = br->topology_change;
1571da177e4SLinus Torvalds 		b.topology_change_detected = br->topology_change_detected;
1581da177e4SLinus Torvalds 		b.root_port = br->root_port;
1599cde0708SStephen Hemminger 
1609cde0708SStephen Hemminger 		b.stp_enabled = (br->stp_enabled != BR_NO_STP);
1611da177e4SLinus Torvalds 		b.ageing_time = jiffies_to_clock_t(br->ageing_time);
1621da177e4SLinus Torvalds 		b.hello_timer_value = br_timer_value(&br->hello_timer);
1631da177e4SLinus Torvalds 		b.tcn_timer_value = br_timer_value(&br->tcn_timer);
1641da177e4SLinus Torvalds 		b.topology_change_timer_value = br_timer_value(&br->topology_change_timer);
165f7cdee8aSNikolay Aleksandrov 		b.gc_timer_value = br_timer_value(&br->gc_work.timer);
1661da177e4SLinus Torvalds 		rcu_read_unlock();
1671da177e4SLinus Torvalds 
1681da177e4SLinus Torvalds 		if (copy_to_user((void __user *)args[1], &b, sizeof(b)))
1691da177e4SLinus Torvalds 			return -EFAULT;
1701da177e4SLinus Torvalds 
1711da177e4SLinus Torvalds 		return 0;
1721da177e4SLinus Torvalds 	}
1731da177e4SLinus Torvalds 
1741da177e4SLinus Torvalds 	case BRCTL_GET_PORT_LIST:
1751da177e4SLinus Torvalds 	{
1761da177e4SLinus Torvalds 		int num, *indices;
1771da177e4SLinus Torvalds 
1781da177e4SLinus Torvalds 		num = args[2];
1791da177e4SLinus Torvalds 		if (num < 0)
1801da177e4SLinus Torvalds 			return -EINVAL;
1811da177e4SLinus Torvalds 		if (num == 0)
1821da177e4SLinus Torvalds 			num = 256;
1831da177e4SLinus Torvalds 		if (num > BR_MAX_PORTS)
1841da177e4SLinus Torvalds 			num = BR_MAX_PORTS;
1851da177e4SLinus Torvalds 
1860da974f4SPanagiotis Issaris 		indices = kcalloc(num, sizeof(int), GFP_KERNEL);
1871da177e4SLinus Torvalds 		if (indices == NULL)
1881da177e4SLinus Torvalds 			return -ENOMEM;
1891da177e4SLinus Torvalds 
1901da177e4SLinus Torvalds 		get_port_ifindices(br, indices, num);
191*561d8352SArnd Bergmann 		if (copy_to_user(argp, indices, num * sizeof(int)))
1921da177e4SLinus Torvalds 			num =  -EFAULT;
1931da177e4SLinus Torvalds 		kfree(indices);
1941da177e4SLinus Torvalds 		return num;
1951da177e4SLinus Torvalds 	}
1961da177e4SLinus Torvalds 
1971da177e4SLinus Torvalds 	case BRCTL_SET_BRIDGE_FORWARD_DELAY:
198cb990503SEric W. Biederman 		if (!ns_capable(dev_net(dev)->user_ns, CAP_NET_ADMIN))
1991da177e4SLinus Torvalds 			return -EPERM;
2001da177e4SLinus Torvalds 
201bf871ad7SXin Long 		ret = br_set_forward_delay(br, args[1]);
202bf871ad7SXin Long 		break;
2031da177e4SLinus Torvalds 
2041da177e4SLinus Torvalds 	case BRCTL_SET_BRIDGE_HELLO_TIME:
205cb990503SEric W. Biederman 		if (!ns_capable(dev_net(dev)->user_ns, CAP_NET_ADMIN))
2061da177e4SLinus Torvalds 			return -EPERM;
2071da177e4SLinus Torvalds 
208bf871ad7SXin Long 		ret = br_set_hello_time(br, args[1]);
209bf871ad7SXin Long 		break;
2101da177e4SLinus Torvalds 
2111da177e4SLinus Torvalds 	case BRCTL_SET_BRIDGE_MAX_AGE:
212cb990503SEric W. Biederman 		if (!ns_capable(dev_net(dev)->user_ns, CAP_NET_ADMIN))
2131da177e4SLinus Torvalds 			return -EPERM;
2141da177e4SLinus Torvalds 
215bf871ad7SXin Long 		ret = br_set_max_age(br, args[1]);
216bf871ad7SXin Long 		break;
2171da177e4SLinus Torvalds 
2181da177e4SLinus Torvalds 	case BRCTL_SET_AGEING_TIME:
219cb990503SEric W. Biederman 		if (!ns_capable(dev_net(dev)->user_ns, CAP_NET_ADMIN))
2201da177e4SLinus Torvalds 			return -EPERM;
2211da177e4SLinus Torvalds 
222bf871ad7SXin Long 		ret = br_set_ageing_time(br, args[1]);
223bf871ad7SXin Long 		break;
2241da177e4SLinus Torvalds 
2251da177e4SLinus Torvalds 	case BRCTL_GET_PORT_INFO:
2261da177e4SLinus Torvalds 	{
2271da177e4SLinus Torvalds 		struct __port_info p;
2281da177e4SLinus Torvalds 		struct net_bridge_port *pt;
2291da177e4SLinus Torvalds 
2301da177e4SLinus Torvalds 		rcu_read_lock();
2311da177e4SLinus Torvalds 		if ((pt = br_get_port(br, args[2])) == NULL) {
2321da177e4SLinus Torvalds 			rcu_read_unlock();
2331da177e4SLinus Torvalds 			return -EINVAL;
2341da177e4SLinus Torvalds 		}
2351da177e4SLinus Torvalds 
2361da177e4SLinus Torvalds 		memset(&p, 0, sizeof(struct __port_info));
2371da177e4SLinus Torvalds 		memcpy(&p.designated_root, &pt->designated_root, 8);
2381da177e4SLinus Torvalds 		memcpy(&p.designated_bridge, &pt->designated_bridge, 8);
2391da177e4SLinus Torvalds 		p.port_id = pt->port_id;
2401da177e4SLinus Torvalds 		p.designated_port = pt->designated_port;
2411da177e4SLinus Torvalds 		p.path_cost = pt->path_cost;
2421da177e4SLinus Torvalds 		p.designated_cost = pt->designated_cost;
2431da177e4SLinus Torvalds 		p.state = pt->state;
2441da177e4SLinus Torvalds 		p.top_change_ack = pt->topology_change_ack;
2451da177e4SLinus Torvalds 		p.config_pending = pt->config_pending;
2461da177e4SLinus Torvalds 		p.message_age_timer_value = br_timer_value(&pt->message_age_timer);
2471da177e4SLinus Torvalds 		p.forward_delay_timer_value = br_timer_value(&pt->forward_delay_timer);
2481da177e4SLinus Torvalds 		p.hold_timer_value = br_timer_value(&pt->hold_timer);
2491da177e4SLinus Torvalds 
2501da177e4SLinus Torvalds 		rcu_read_unlock();
2511da177e4SLinus Torvalds 
252*561d8352SArnd Bergmann 		if (copy_to_user(argp, &p, sizeof(p)))
2531da177e4SLinus Torvalds 			return -EFAULT;
2541da177e4SLinus Torvalds 
2551da177e4SLinus Torvalds 		return 0;
2561da177e4SLinus Torvalds 	}
2571da177e4SLinus Torvalds 
2581da177e4SLinus Torvalds 	case BRCTL_SET_BRIDGE_STP_STATE:
259cb990503SEric W. Biederman 		if (!ns_capable(dev_net(dev)->user_ns, CAP_NET_ADMIN))
2601da177e4SLinus Torvalds 			return -EPERM;
2611da177e4SLinus Torvalds 
262419dba8aSHoratiu Vultur 		ret = br_stp_set_enabled(br, args[1], NULL);
263bf871ad7SXin Long 		break;
2641da177e4SLinus Torvalds 
2651da177e4SLinus Torvalds 	case BRCTL_SET_BRIDGE_PRIORITY:
266cb990503SEric W. Biederman 		if (!ns_capable(dev_net(dev)->user_ns, CAP_NET_ADMIN))
2671da177e4SLinus Torvalds 			return -EPERM;
2681da177e4SLinus Torvalds 
2691da177e4SLinus Torvalds 		br_stp_set_bridge_priority(br, args[1]);
270bf871ad7SXin Long 		ret = 0;
271bf871ad7SXin Long 		break;
2721da177e4SLinus Torvalds 
2731da177e4SLinus Torvalds 	case BRCTL_SET_PORT_PRIORITY:
2741da177e4SLinus Torvalds 	{
275cb990503SEric W. Biederman 		if (!ns_capable(dev_net(dev)->user_ns, CAP_NET_ADMIN))
2761da177e4SLinus Torvalds 			return -EPERM;
2771da177e4SLinus Torvalds 
2781da177e4SLinus Torvalds 		spin_lock_bh(&br->lock);
2791da177e4SLinus Torvalds 		if ((p = br_get_port(br, args[1])) == NULL)
2801da177e4SLinus Torvalds 			ret = -EINVAL;
2811da177e4SLinus Torvalds 		else
28214f98f25Sstephen hemminger 			ret = br_stp_set_port_priority(p, args[2]);
2831da177e4SLinus Torvalds 		spin_unlock_bh(&br->lock);
284bf871ad7SXin Long 		break;
2851da177e4SLinus Torvalds 	}
2861da177e4SLinus Torvalds 
2871da177e4SLinus Torvalds 	case BRCTL_SET_PATH_COST:
2881da177e4SLinus Torvalds 	{
289cb990503SEric W. Biederman 		if (!ns_capable(dev_net(dev)->user_ns, CAP_NET_ADMIN))
2901da177e4SLinus Torvalds 			return -EPERM;
2911da177e4SLinus Torvalds 
29214f98f25Sstephen hemminger 		spin_lock_bh(&br->lock);
2931da177e4SLinus Torvalds 		if ((p = br_get_port(br, args[1])) == NULL)
2941da177e4SLinus Torvalds 			ret = -EINVAL;
2951da177e4SLinus Torvalds 		else
29614f98f25Sstephen hemminger 			ret = br_stp_set_path_cost(p, args[2]);
29714f98f25Sstephen hemminger 		spin_unlock_bh(&br->lock);
298bf871ad7SXin Long 		break;
2991da177e4SLinus Torvalds 	}
3001da177e4SLinus Torvalds 
3011da177e4SLinus Torvalds 	case BRCTL_GET_FDB_ENTRIES:
302*561d8352SArnd Bergmann 		return get_fdb_entries(br, argp, args[2], args[3]);
3031da177e4SLinus Torvalds 	}
3041da177e4SLinus Torvalds 
305bf871ad7SXin Long 	if (!ret) {
306bf871ad7SXin Long 		if (p)
30792899063SNikolay Aleksandrov 			br_ifinfo_notify(RTM_NEWLINK, NULL, p);
308bf871ad7SXin Long 		else
309bf871ad7SXin Long 			netdev_state_change(br->dev);
310bf871ad7SXin Long 	}
311bf871ad7SXin Long 
312bf871ad7SXin Long 	return ret;
3131da177e4SLinus Torvalds }
3141da177e4SLinus Torvalds 
3154aa678baSAlexey Dobriyan static int old_deviceless(struct net *net, void __user *uarg)
3161da177e4SLinus Torvalds {
3171da177e4SLinus Torvalds 	unsigned long args[3];
3181da177e4SLinus Torvalds 
3191da177e4SLinus Torvalds 	if (copy_from_user(args, uarg, sizeof(args)))
3201da177e4SLinus Torvalds 		return -EFAULT;
3211da177e4SLinus Torvalds 
3221da177e4SLinus Torvalds 	switch (args[0]) {
3231da177e4SLinus Torvalds 	case BRCTL_GET_VERSION:
3241da177e4SLinus Torvalds 		return BRCTL_VERSION;
3251da177e4SLinus Torvalds 
3261da177e4SLinus Torvalds 	case BRCTL_GET_BRIDGES:
3271da177e4SLinus Torvalds 	{
3281da177e4SLinus Torvalds 		int *indices;
3291da177e4SLinus Torvalds 		int ret = 0;
3301da177e4SLinus Torvalds 
3311da177e4SLinus Torvalds 		if (args[2] >= 2048)
3321da177e4SLinus Torvalds 			return -ENOMEM;
3330da974f4SPanagiotis Issaris 		indices = kcalloc(args[2], sizeof(int), GFP_KERNEL);
3341da177e4SLinus Torvalds 		if (indices == NULL)
3351da177e4SLinus Torvalds 			return -ENOMEM;
3361da177e4SLinus Torvalds 
3374aa678baSAlexey Dobriyan 		args[2] = get_bridge_ifindices(net, indices, args[2]);
3381da177e4SLinus Torvalds 
339*561d8352SArnd Bergmann 		ret = copy_to_user(uarg, indices, args[2]*sizeof(int))
3401da177e4SLinus Torvalds 			? -EFAULT : args[2];
3411da177e4SLinus Torvalds 
3421da177e4SLinus Torvalds 		kfree(indices);
3431da177e4SLinus Torvalds 		return ret;
3441da177e4SLinus Torvalds 	}
3451da177e4SLinus Torvalds 
3461da177e4SLinus Torvalds 	case BRCTL_ADD_BRIDGE:
3471da177e4SLinus Torvalds 	case BRCTL_DEL_BRIDGE:
3481da177e4SLinus Torvalds 	{
3491da177e4SLinus Torvalds 		char buf[IFNAMSIZ];
3501da177e4SLinus Torvalds 
351cb990503SEric W. Biederman 		if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
3521da177e4SLinus Torvalds 			return -EPERM;
3531da177e4SLinus Torvalds 
354*561d8352SArnd Bergmann 		if (copy_from_user(buf, uarg, IFNAMSIZ))
3551da177e4SLinus Torvalds 			return -EFAULT;
3561da177e4SLinus Torvalds 
3571da177e4SLinus Torvalds 		buf[IFNAMSIZ-1] = 0;
3581da177e4SLinus Torvalds 
3591da177e4SLinus Torvalds 		if (args[0] == BRCTL_ADD_BRIDGE)
3604aa678baSAlexey Dobriyan 			return br_add_bridge(net, buf);
3611da177e4SLinus Torvalds 
3624aa678baSAlexey Dobriyan 		return br_del_bridge(net, buf);
3631da177e4SLinus Torvalds 	}
3641da177e4SLinus Torvalds 	}
3651da177e4SLinus Torvalds 
3661da177e4SLinus Torvalds 	return -EOPNOTSUPP;
3671da177e4SLinus Torvalds }
3681da177e4SLinus Torvalds 
369881d966bSEric W. Biederman int br_ioctl_deviceless_stub(struct net *net, unsigned int cmd, void __user *uarg)
3701da177e4SLinus Torvalds {
3711da177e4SLinus Torvalds 	switch (cmd) {
3721da177e4SLinus Torvalds 	case SIOCGIFBR:
3731da177e4SLinus Torvalds 	case SIOCSIFBR:
3744aa678baSAlexey Dobriyan 		return old_deviceless(net, uarg);
3751da177e4SLinus Torvalds 
3761da177e4SLinus Torvalds 	case SIOCBRADDBR:
3771da177e4SLinus Torvalds 	case SIOCBRDELBR:
3781da177e4SLinus Torvalds 	{
3791da177e4SLinus Torvalds 		char buf[IFNAMSIZ];
3801da177e4SLinus Torvalds 
381cb990503SEric W. Biederman 		if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
3821da177e4SLinus Torvalds 			return -EPERM;
3831da177e4SLinus Torvalds 
3841da177e4SLinus Torvalds 		if (copy_from_user(buf, uarg, IFNAMSIZ))
3851da177e4SLinus Torvalds 			return -EFAULT;
3861da177e4SLinus Torvalds 
3871da177e4SLinus Torvalds 		buf[IFNAMSIZ-1] = 0;
3881da177e4SLinus Torvalds 		if (cmd == SIOCBRADDBR)
3894aa678baSAlexey Dobriyan 			return br_add_bridge(net, buf);
3901da177e4SLinus Torvalds 
3914aa678baSAlexey Dobriyan 		return br_del_bridge(net, buf);
3921da177e4SLinus Torvalds 	}
3931da177e4SLinus Torvalds 	}
3941da177e4SLinus Torvalds 	return -EOPNOTSUPP;
3951da177e4SLinus Torvalds }
3961da177e4SLinus Torvalds 
3971da177e4SLinus Torvalds int br_dev_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
3981da177e4SLinus Torvalds {
3991da177e4SLinus Torvalds 	struct net_bridge *br = netdev_priv(dev);
4001da177e4SLinus Torvalds 
4011da177e4SLinus Torvalds 	switch (cmd) {
4021da177e4SLinus Torvalds 	case SIOCBRADDIF:
4031da177e4SLinus Torvalds 	case SIOCBRDELIF:
4041da177e4SLinus Torvalds 		return add_del_if(br, rq->ifr_ifindex, cmd == SIOCBRADDIF);
4051da177e4SLinus Torvalds 
4061da177e4SLinus Torvalds 	}
4071da177e4SLinus Torvalds 
40828a16c97Sstephen hemminger 	br_debug(br, "Bridge does not support ioctl 0x%x\n", cmd);
4091da177e4SLinus Torvalds 	return -EOPNOTSUPP;
4101da177e4SLinus Torvalds }
411