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