xref: /openbmc/linux/net/bridge/br_ioctl.c (revision 28efb0046512e8a13ed9f9bdf0d68d10bbfbe9cf)
1 /*
2  *	Ioctl handler
3  *	Linux ethernet bridge
4  *
5  *	Authors:
6  *	Lennert Buytenhek		<buytenh@gnu.org>
7  *
8  *	This program is free software; you can redistribute it and/or
9  *	modify it under the terms of the GNU General Public License
10  *	as published by the Free Software Foundation; either version
11  *	2 of the License, or (at your option) any later version.
12  */
13 
14 #include <linux/capability.h>
15 #include <linux/kernel.h>
16 #include <linux/if_bridge.h>
17 #include <linux/netdevice.h>
18 #include <linux/slab.h>
19 #include <linux/times.h>
20 #include <net/net_namespace.h>
21 #include <linux/uaccess.h>
22 #include "br_private.h"
23 
24 static int get_bridge_ifindices(struct net *net, int *indices, int num)
25 {
26 	struct net_device *dev;
27 	int i = 0;
28 
29 	rcu_read_lock();
30 	for_each_netdev_rcu(net, dev) {
31 		if (i >= num)
32 			break;
33 		if (dev->priv_flags & IFF_EBRIDGE)
34 			indices[i++] = dev->ifindex;
35 	}
36 	rcu_read_unlock();
37 
38 	return i;
39 }
40 
41 /* called with RTNL */
42 static void get_port_ifindices(struct net_bridge *br, int *ifindices, int num)
43 {
44 	struct net_bridge_port *p;
45 
46 	list_for_each_entry(p, &br->port_list, list) {
47 		if (p->port_no < num)
48 			ifindices[p->port_no] = p->dev->ifindex;
49 	}
50 }
51 
52 /*
53  * Format up to a page worth of forwarding table entries
54  * userbuf -- where to copy result
55  * maxnum  -- maximum number of entries desired
56  *            (limited to a page for sanity)
57  * offset  -- number of records to skip
58  */
59 static int get_fdb_entries(struct net_bridge *br, void __user *userbuf,
60 			   unsigned long maxnum, unsigned long offset)
61 {
62 	int num;
63 	void *buf;
64 	size_t size;
65 
66 	/* Clamp size to PAGE_SIZE, test maxnum to avoid overflow */
67 	if (maxnum > PAGE_SIZE/sizeof(struct __fdb_entry))
68 		maxnum = PAGE_SIZE/sizeof(struct __fdb_entry);
69 
70 	size = maxnum * sizeof(struct __fdb_entry);
71 
72 	buf = kmalloc(size, GFP_USER);
73 	if (!buf)
74 		return -ENOMEM;
75 
76 	num = br_fdb_fillbuf(br, buf, maxnum, offset);
77 	if (num > 0) {
78 		if (copy_to_user(userbuf, buf, num*sizeof(struct __fdb_entry)))
79 			num = -EFAULT;
80 	}
81 	kfree(buf);
82 
83 	return num;
84 }
85 
86 /* called with RTNL */
87 static int add_del_if(struct net_bridge *br, int ifindex, int isadd)
88 {
89 	struct net *net = dev_net(br->dev);
90 	struct net_device *dev;
91 	int ret;
92 
93 	if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
94 		return -EPERM;
95 
96 	dev = __dev_get_by_index(net, ifindex);
97 	if (dev == NULL)
98 		return -EINVAL;
99 
100 	if (isadd)
101 		ret = br_add_if(br, dev);
102 	else
103 		ret = br_del_if(br, dev);
104 
105 	if (!ret)
106 		rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_MASTER, GFP_KERNEL);
107 
108 	return ret;
109 }
110 
111 /*
112  * Legacy ioctl's through SIOCDEVPRIVATE
113  * This interface is deprecated because it was too difficult to
114  * to do the translation for 32/64bit ioctl compatibility.
115  */
116 static int old_dev_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
117 {
118 	struct net_bridge *br = netdev_priv(dev);
119 	struct net_bridge_port *p = NULL;
120 	unsigned long args[4];
121 	int ret = -EOPNOTSUPP;
122 
123 	if (copy_from_user(args, rq->ifr_data, sizeof(args)))
124 		return -EFAULT;
125 
126 	switch (args[0]) {
127 	case BRCTL_ADD_IF:
128 	case BRCTL_DEL_IF:
129 		return add_del_if(br, args[1], args[0] == BRCTL_ADD_IF);
130 
131 	case BRCTL_GET_BRIDGE_INFO:
132 	{
133 		struct __bridge_info b;
134 
135 		memset(&b, 0, sizeof(struct __bridge_info));
136 		rcu_read_lock();
137 		memcpy(&b.designated_root, &br->designated_root, 8);
138 		memcpy(&b.bridge_id, &br->bridge_id, 8);
139 		b.root_path_cost = br->root_path_cost;
140 		b.max_age = jiffies_to_clock_t(br->max_age);
141 		b.hello_time = jiffies_to_clock_t(br->hello_time);
142 		b.forward_delay = br->forward_delay;
143 		b.bridge_max_age = br->bridge_max_age;
144 		b.bridge_hello_time = br->bridge_hello_time;
145 		b.bridge_forward_delay = jiffies_to_clock_t(br->bridge_forward_delay);
146 		b.topology_change = br->topology_change;
147 		b.topology_change_detected = br->topology_change_detected;
148 		b.root_port = br->root_port;
149 
150 		b.stp_enabled = (br->stp_enabled != BR_NO_STP);
151 		b.ageing_time = jiffies_to_clock_t(br->ageing_time);
152 		b.hello_timer_value = br_timer_value(&br->hello_timer);
153 		b.tcn_timer_value = br_timer_value(&br->tcn_timer);
154 		b.topology_change_timer_value = br_timer_value(&br->topology_change_timer);
155 		b.gc_timer_value = br_timer_value(&br->gc_work.timer);
156 		rcu_read_unlock();
157 
158 		if (copy_to_user((void __user *)args[1], &b, sizeof(b)))
159 			return -EFAULT;
160 
161 		return 0;
162 	}
163 
164 	case BRCTL_GET_PORT_LIST:
165 	{
166 		int num, *indices;
167 
168 		num = args[2];
169 		if (num < 0)
170 			return -EINVAL;
171 		if (num == 0)
172 			num = 256;
173 		if (num > BR_MAX_PORTS)
174 			num = BR_MAX_PORTS;
175 
176 		indices = kcalloc(num, sizeof(int), GFP_KERNEL);
177 		if (indices == NULL)
178 			return -ENOMEM;
179 
180 		get_port_ifindices(br, indices, num);
181 		if (copy_to_user((void __user *)args[1], indices, num*sizeof(int)))
182 			num =  -EFAULT;
183 		kfree(indices);
184 		return num;
185 	}
186 
187 	case BRCTL_SET_BRIDGE_FORWARD_DELAY:
188 		if (!ns_capable(dev_net(dev)->user_ns, CAP_NET_ADMIN))
189 			return -EPERM;
190 
191 		ret = br_set_forward_delay(br, args[1]);
192 		break;
193 
194 	case BRCTL_SET_BRIDGE_HELLO_TIME:
195 		if (!ns_capable(dev_net(dev)->user_ns, CAP_NET_ADMIN))
196 			return -EPERM;
197 
198 		ret = br_set_hello_time(br, args[1]);
199 		break;
200 
201 	case BRCTL_SET_BRIDGE_MAX_AGE:
202 		if (!ns_capable(dev_net(dev)->user_ns, CAP_NET_ADMIN))
203 			return -EPERM;
204 
205 		ret = br_set_max_age(br, args[1]);
206 		break;
207 
208 	case BRCTL_SET_AGEING_TIME:
209 		if (!ns_capable(dev_net(dev)->user_ns, CAP_NET_ADMIN))
210 			return -EPERM;
211 
212 		ret = br_set_ageing_time(br, args[1]);
213 		break;
214 
215 	case BRCTL_GET_PORT_INFO:
216 	{
217 		struct __port_info p;
218 		struct net_bridge_port *pt;
219 
220 		rcu_read_lock();
221 		if ((pt = br_get_port(br, args[2])) == NULL) {
222 			rcu_read_unlock();
223 			return -EINVAL;
224 		}
225 
226 		memset(&p, 0, sizeof(struct __port_info));
227 		memcpy(&p.designated_root, &pt->designated_root, 8);
228 		memcpy(&p.designated_bridge, &pt->designated_bridge, 8);
229 		p.port_id = pt->port_id;
230 		p.designated_port = pt->designated_port;
231 		p.path_cost = pt->path_cost;
232 		p.designated_cost = pt->designated_cost;
233 		p.state = pt->state;
234 		p.top_change_ack = pt->topology_change_ack;
235 		p.config_pending = pt->config_pending;
236 		p.message_age_timer_value = br_timer_value(&pt->message_age_timer);
237 		p.forward_delay_timer_value = br_timer_value(&pt->forward_delay_timer);
238 		p.hold_timer_value = br_timer_value(&pt->hold_timer);
239 
240 		rcu_read_unlock();
241 
242 		if (copy_to_user((void __user *)args[1], &p, sizeof(p)))
243 			return -EFAULT;
244 
245 		return 0;
246 	}
247 
248 	case BRCTL_SET_BRIDGE_STP_STATE:
249 		if (!ns_capable(dev_net(dev)->user_ns, CAP_NET_ADMIN))
250 			return -EPERM;
251 
252 		br_stp_set_enabled(br, args[1]);
253 		ret = 0;
254 		break;
255 
256 	case BRCTL_SET_BRIDGE_PRIORITY:
257 		if (!ns_capable(dev_net(dev)->user_ns, CAP_NET_ADMIN))
258 			return -EPERM;
259 
260 		br_stp_set_bridge_priority(br, args[1]);
261 		ret = 0;
262 		break;
263 
264 	case BRCTL_SET_PORT_PRIORITY:
265 	{
266 		if (!ns_capable(dev_net(dev)->user_ns, CAP_NET_ADMIN))
267 			return -EPERM;
268 
269 		spin_lock_bh(&br->lock);
270 		if ((p = br_get_port(br, args[1])) == NULL)
271 			ret = -EINVAL;
272 		else
273 			ret = br_stp_set_port_priority(p, args[2]);
274 		spin_unlock_bh(&br->lock);
275 		break;
276 	}
277 
278 	case BRCTL_SET_PATH_COST:
279 	{
280 		if (!ns_capable(dev_net(dev)->user_ns, CAP_NET_ADMIN))
281 			return -EPERM;
282 
283 		spin_lock_bh(&br->lock);
284 		if ((p = br_get_port(br, args[1])) == NULL)
285 			ret = -EINVAL;
286 		else
287 			ret = br_stp_set_path_cost(p, args[2]);
288 		spin_unlock_bh(&br->lock);
289 		break;
290 	}
291 
292 	case BRCTL_GET_FDB_ENTRIES:
293 		return get_fdb_entries(br, (void __user *)args[1],
294 				       args[2], args[3]);
295 	}
296 
297 	if (!ret) {
298 		if (p)
299 			br_ifinfo_notify(RTM_NEWLINK, p);
300 		else
301 			netdev_state_change(br->dev);
302 	}
303 
304 	return ret;
305 }
306 
307 static int old_deviceless(struct net *net, void __user *uarg)
308 {
309 	unsigned long args[3];
310 
311 	if (copy_from_user(args, uarg, sizeof(args)))
312 		return -EFAULT;
313 
314 	switch (args[0]) {
315 	case BRCTL_GET_VERSION:
316 		return BRCTL_VERSION;
317 
318 	case BRCTL_GET_BRIDGES:
319 	{
320 		int *indices;
321 		int ret = 0;
322 
323 		if (args[2] >= 2048)
324 			return -ENOMEM;
325 		indices = kcalloc(args[2], sizeof(int), GFP_KERNEL);
326 		if (indices == NULL)
327 			return -ENOMEM;
328 
329 		args[2] = get_bridge_ifindices(net, indices, args[2]);
330 
331 		ret = copy_to_user((void __user *)args[1], indices, args[2]*sizeof(int))
332 			? -EFAULT : args[2];
333 
334 		kfree(indices);
335 		return ret;
336 	}
337 
338 	case BRCTL_ADD_BRIDGE:
339 	case BRCTL_DEL_BRIDGE:
340 	{
341 		char buf[IFNAMSIZ];
342 
343 		if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
344 			return -EPERM;
345 
346 		if (copy_from_user(buf, (void __user *)args[1], IFNAMSIZ))
347 			return -EFAULT;
348 
349 		buf[IFNAMSIZ-1] = 0;
350 
351 		if (args[0] == BRCTL_ADD_BRIDGE)
352 			return br_add_bridge(net, buf);
353 
354 		return br_del_bridge(net, buf);
355 	}
356 	}
357 
358 	return -EOPNOTSUPP;
359 }
360 
361 int br_ioctl_deviceless_stub(struct net *net, unsigned int cmd, void __user *uarg)
362 {
363 	switch (cmd) {
364 	case SIOCGIFBR:
365 	case SIOCSIFBR:
366 		return old_deviceless(net, uarg);
367 
368 	case SIOCBRADDBR:
369 	case SIOCBRDELBR:
370 	{
371 		char buf[IFNAMSIZ];
372 
373 		if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
374 			return -EPERM;
375 
376 		if (copy_from_user(buf, uarg, IFNAMSIZ))
377 			return -EFAULT;
378 
379 		buf[IFNAMSIZ-1] = 0;
380 		if (cmd == SIOCBRADDBR)
381 			return br_add_bridge(net, buf);
382 
383 		return br_del_bridge(net, buf);
384 	}
385 	}
386 	return -EOPNOTSUPP;
387 }
388 
389 int br_dev_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
390 {
391 	struct net_bridge *br = netdev_priv(dev);
392 
393 	switch (cmd) {
394 	case SIOCDEVPRIVATE:
395 		return old_dev_ioctl(dev, rq, cmd);
396 
397 	case SIOCBRADDIF:
398 	case SIOCBRDELIF:
399 		return add_del_if(br, rq->ifr_ifindex, cmd == SIOCBRADDIF);
400 
401 	}
402 
403 	br_debug(br, "Bridge does not support ioctl 0x%x\n", cmd);
404 	return -EOPNOTSUPP;
405 }
406