3 * Linux ethernet bridge
6 * Lennert Buytenhek <buytenh@gnu.org>
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.
14 #include <linux/capability.h>
15 #include <linux/kernel.h>
16 #include <linux/if_bridge.h>
17 #include <linux/netdevice.h>
18 #include <linux/times.h>
19 #include <net/net_namespace.h>
20 #include <asm/uaccess.h>
21 #include "br_private.h"
23 /* called with RTNL */
24 static int get_bridge_ifindices(struct net
*net
, int *indices
, int num
)
26 struct net_device
*dev
;
29 for_each_netdev(net
, dev
) {
32 if (dev
->priv_flags
& IFF_EBRIDGE
)
33 indices
[i
++] = dev
->ifindex
;
39 /* called with RTNL */
40 static void get_port_ifindices(struct net_bridge
*br
, int *ifindices
, int num
)
42 struct net_bridge_port
*p
;
44 list_for_each_entry(p
, &br
->port_list
, list
) {
46 ifindices
[p
->port_no
] = p
->dev
->ifindex
;
51 * Format up to a page worth of forwarding table entries
52 * userbuf -- where to copy result
53 * maxnum -- maximum number of entries desired
54 * (limited to a page for sanity)
55 * offset -- number of records to skip
57 static int get_fdb_entries(struct net_bridge
*br
, void __user
*userbuf
,
58 unsigned long maxnum
, unsigned long offset
)
64 /* Clamp size to PAGE_SIZE, test maxnum to avoid overflow */
65 if (maxnum
> PAGE_SIZE
/sizeof(struct __fdb_entry
))
66 maxnum
= PAGE_SIZE
/sizeof(struct __fdb_entry
);
68 size
= maxnum
* sizeof(struct __fdb_entry
);
70 buf
= kmalloc(size
, GFP_USER
);
74 num
= br_fdb_fillbuf(br
, buf
, maxnum
, offset
);
76 if (copy_to_user(userbuf
, buf
, num
*sizeof(struct __fdb_entry
)))
84 static int add_del_if(struct net_bridge
*br
, int ifindex
, int isadd
)
86 struct net_device
*dev
;
89 if (!capable(CAP_NET_ADMIN
))
92 dev
= dev_get_by_index(dev_net(br
->dev
), ifindex
);
97 ret
= br_add_if(br
, dev
);
99 ret
= br_del_if(br
, dev
);
106 * Legacy ioctl's through SIOCDEVPRIVATE
107 * This interface is deprecated because it was too difficult to
108 * to do the translation for 32/64bit ioctl compatability.
110 static int old_dev_ioctl(struct net_device
*dev
, struct ifreq
*rq
, int cmd
)
112 struct net_bridge
*br
= netdev_priv(dev
);
113 unsigned long args
[4];
115 if (copy_from_user(args
, rq
->ifr_data
, sizeof(args
)))
121 return add_del_if(br
, args
[1], args
[0] == BRCTL_ADD_IF
);
123 case BRCTL_GET_BRIDGE_INFO
:
125 struct __bridge_info b
;
127 memset(&b
, 0, sizeof(struct __bridge_info
));
129 memcpy(&b
.designated_root
, &br
->designated_root
, 8);
130 memcpy(&b
.bridge_id
, &br
->bridge_id
, 8);
131 b
.root_path_cost
= br
->root_path_cost
;
132 b
.max_age
= jiffies_to_clock_t(br
->max_age
);
133 b
.hello_time
= jiffies_to_clock_t(br
->hello_time
);
134 b
.forward_delay
= br
->forward_delay
;
135 b
.bridge_max_age
= br
->bridge_max_age
;
136 b
.bridge_hello_time
= br
->bridge_hello_time
;
137 b
.bridge_forward_delay
= jiffies_to_clock_t(br
->bridge_forward_delay
);
138 b
.topology_change
= br
->topology_change
;
139 b
.topology_change_detected
= br
->topology_change_detected
;
140 b
.root_port
= br
->root_port
;
142 b
.stp_enabled
= (br
->stp_enabled
!= BR_NO_STP
);
143 b
.ageing_time
= jiffies_to_clock_t(br
->ageing_time
);
144 b
.hello_timer_value
= br_timer_value(&br
->hello_timer
);
145 b
.tcn_timer_value
= br_timer_value(&br
->tcn_timer
);
146 b
.topology_change_timer_value
= br_timer_value(&br
->topology_change_timer
);
147 b
.gc_timer_value
= br_timer_value(&br
->gc_timer
);
150 if (copy_to_user((void __user
*)args
[1], &b
, sizeof(b
)))
156 case BRCTL_GET_PORT_LIST
:
165 if (num
> BR_MAX_PORTS
)
168 indices
= kcalloc(num
, sizeof(int), GFP_KERNEL
);
172 get_port_ifindices(br
, indices
, num
);
173 if (copy_to_user((void __user
*)args
[1], indices
, num
*sizeof(int)))
179 case BRCTL_SET_BRIDGE_FORWARD_DELAY
:
180 if (!capable(CAP_NET_ADMIN
))
183 spin_lock_bh(&br
->lock
);
184 br
->bridge_forward_delay
= clock_t_to_jiffies(args
[1]);
185 if (br_is_root_bridge(br
))
186 br
->forward_delay
= br
->bridge_forward_delay
;
187 spin_unlock_bh(&br
->lock
);
190 case BRCTL_SET_BRIDGE_HELLO_TIME
:
192 unsigned long t
= clock_t_to_jiffies(args
[1]);
193 if (!capable(CAP_NET_ADMIN
))
199 spin_lock_bh(&br
->lock
);
200 br
->bridge_hello_time
= t
;
201 if (br_is_root_bridge(br
))
202 br
->hello_time
= br
->bridge_hello_time
;
203 spin_unlock_bh(&br
->lock
);
207 case BRCTL_SET_BRIDGE_MAX_AGE
:
208 if (!capable(CAP_NET_ADMIN
))
211 spin_lock_bh(&br
->lock
);
212 br
->bridge_max_age
= clock_t_to_jiffies(args
[1]);
213 if (br_is_root_bridge(br
))
214 br
->max_age
= br
->bridge_max_age
;
215 spin_unlock_bh(&br
->lock
);
218 case BRCTL_SET_AGEING_TIME
:
219 if (!capable(CAP_NET_ADMIN
))
222 br
->ageing_time
= clock_t_to_jiffies(args
[1]);
225 case BRCTL_GET_PORT_INFO
:
227 struct __port_info p
;
228 struct net_bridge_port
*pt
;
231 if ((pt
= br_get_port(br
, args
[2])) == NULL
) {
236 memset(&p
, 0, sizeof(struct __port_info
));
237 memcpy(&p
.designated_root
, &pt
->designated_root
, 8);
238 memcpy(&p
.designated_bridge
, &pt
->designated_bridge
, 8);
239 p
.port_id
= pt
->port_id
;
240 p
.designated_port
= pt
->designated_port
;
241 p
.path_cost
= pt
->path_cost
;
242 p
.designated_cost
= pt
->designated_cost
;
244 p
.top_change_ack
= pt
->topology_change_ack
;
245 p
.config_pending
= pt
->config_pending
;
246 p
.message_age_timer_value
= br_timer_value(&pt
->message_age_timer
);
247 p
.forward_delay_timer_value
= br_timer_value(&pt
->forward_delay_timer
);
248 p
.hold_timer_value
= br_timer_value(&pt
->hold_timer
);
252 if (copy_to_user((void __user
*)args
[1], &p
, sizeof(p
)))
258 case BRCTL_SET_BRIDGE_STP_STATE
:
259 if (!capable(CAP_NET_ADMIN
))
262 br_stp_set_enabled(br
, args
[1]);
265 case BRCTL_SET_BRIDGE_PRIORITY
:
266 if (!capable(CAP_NET_ADMIN
))
269 spin_lock_bh(&br
->lock
);
270 br_stp_set_bridge_priority(br
, args
[1]);
271 spin_unlock_bh(&br
->lock
);
274 case BRCTL_SET_PORT_PRIORITY
:
276 struct net_bridge_port
*p
;
279 if (!capable(CAP_NET_ADMIN
))
282 if (args
[2] >= (1<<(16-BR_PORT_BITS
)))
285 spin_lock_bh(&br
->lock
);
286 if ((p
= br_get_port(br
, args
[1])) == NULL
)
289 br_stp_set_port_priority(p
, args
[2]);
290 spin_unlock_bh(&br
->lock
);
294 case BRCTL_SET_PATH_COST
:
296 struct net_bridge_port
*p
;
299 if (!capable(CAP_NET_ADMIN
))
302 if ((p
= br_get_port(br
, args
[1])) == NULL
)
305 br_stp_set_path_cost(p
, args
[2]);
310 case BRCTL_GET_FDB_ENTRIES
:
311 return get_fdb_entries(br
, (void __user
*)args
[1],
318 static int old_deviceless(struct net
*net
, void __user
*uarg
)
320 unsigned long args
[3];
322 if (copy_from_user(args
, uarg
, sizeof(args
)))
326 case BRCTL_GET_VERSION
:
327 return BRCTL_VERSION
;
329 case BRCTL_GET_BRIDGES
:
336 indices
= kcalloc(args
[2], sizeof(int), GFP_KERNEL
);
340 args
[2] = get_bridge_ifindices(net
, indices
, args
[2]);
342 ret
= copy_to_user((void __user
*)args
[1], indices
, args
[2]*sizeof(int))
349 case BRCTL_ADD_BRIDGE
:
350 case BRCTL_DEL_BRIDGE
:
354 if (!capable(CAP_NET_ADMIN
))
357 if (copy_from_user(buf
, (void __user
*)args
[1], IFNAMSIZ
))
362 if (args
[0] == BRCTL_ADD_BRIDGE
)
363 return br_add_bridge(net
, buf
);
365 return br_del_bridge(net
, buf
);
372 int br_ioctl_deviceless_stub(struct net
*net
, unsigned int cmd
, void __user
*uarg
)
377 return old_deviceless(net
, uarg
);
384 if (!capable(CAP_NET_ADMIN
))
387 if (copy_from_user(buf
, uarg
, IFNAMSIZ
))
391 if (cmd
== SIOCBRADDBR
)
392 return br_add_bridge(net
, buf
);
394 return br_del_bridge(net
, buf
);
400 int br_dev_ioctl(struct net_device
*dev
, struct ifreq
*rq
, int cmd
)
402 struct net_bridge
*br
= netdev_priv(dev
);
406 return old_dev_ioctl(dev
, rq
, cmd
);
410 return add_del_if(br
, rq
->ifr_ifindex
, cmd
== SIOCBRADDIF
);
414 pr_debug("Bridge does not support ioctl 0x%x\n", cmd
);