ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / net / bridge / br_ioctl.c
1 /*
2  *      Ioctl handler
3  *      Linux ethernet bridge
4  *
5  *      Authors:
6  *      Lennert Buytenhek               <buytenh@gnu.org>
7  *
8  *      $Id: br_ioctl.c,v 1.4 2000/11/08 05:16:40 davem Exp $
9  *
10  *      This program is free software; you can redistribute it and/or
11  *      modify it under the terms of the GNU General Public License
12  *      as published by the Free Software Foundation; either version
13  *      2 of the License, or (at your option) any later version.
14  */
15
16 #include <linux/kernel.h>
17 #include <linux/if_bridge.h>
18 #include <linux/netdevice.h>
19 #include <linux/times.h>
20 #include <asm/uaccess.h>
21 #include "br_private.h"
22
23 /* Report time remaining in user HZ  */
24 static unsigned long timer_residue(const struct timer_list *timer)
25 {
26         return timer_pending(timer) 
27                 ? jiffies_to_clock_t(timer->expires - jiffies) : 0;
28 }
29
30 int br_ioctl_device(struct net_bridge *br, unsigned int cmd,
31                    unsigned long arg0, unsigned long arg1, unsigned long arg2)
32 {
33         if (br == NULL)
34                 return -EINVAL;
35
36         switch (cmd)
37         {
38         case BRCTL_ADD_IF:
39         case BRCTL_DEL_IF:
40         {
41                 struct net_device *dev;
42                 int ret;
43
44                 if (!capable(CAP_NET_ADMIN))
45                         return -EPERM;
46
47                 dev = dev_get_by_index(arg0);
48                 if (dev == NULL)
49                         return -EINVAL;
50
51                 if (cmd == BRCTL_ADD_IF)
52                         ret = br_add_if(br, dev);
53                 else
54                         ret = br_del_if(br, dev);
55
56                 dev_put(dev);
57                 return ret;
58         }
59
60         case BRCTL_GET_BRIDGE_INFO:
61         {
62                 struct __bridge_info b;
63
64                 memset(&b, 0, sizeof(struct __bridge_info));
65                 rcu_read_lock();
66                 memcpy(&b.designated_root, &br->designated_root, 8);
67                 memcpy(&b.bridge_id, &br->bridge_id, 8);
68                 b.root_path_cost = br->root_path_cost;
69                 b.max_age = jiffies_to_clock_t(br->max_age);
70                 b.hello_time = jiffies_to_clock_t(br->hello_time);
71                 b.forward_delay = br->forward_delay;
72                 b.bridge_max_age = br->bridge_max_age;
73                 b.bridge_hello_time = br->bridge_hello_time;
74                 b.bridge_forward_delay = jiffies_to_clock_t(br->bridge_forward_delay);
75                 b.topology_change = br->topology_change;
76                 b.topology_change_detected = br->topology_change_detected;
77                 b.root_port = br->root_port;
78                 b.stp_enabled = br->stp_enabled;
79                 b.ageing_time = jiffies_to_clock_t(br->ageing_time);
80                 b.hello_timer_value = timer_residue(&br->hello_timer);
81                 b.tcn_timer_value = timer_residue(&br->tcn_timer);
82                 b.topology_change_timer_value = timer_residue(&br->topology_change_timer);
83                 b.gc_timer_value = timer_residue(&br->gc_timer);
84                 rcu_read_unlock();
85
86                 if (copy_to_user((void *)arg0, &b, sizeof(b)))
87                         return -EFAULT;
88
89                 return 0;
90         }
91
92         case BRCTL_GET_PORT_LIST:
93         {
94                 int num, *indices;
95
96                 num = arg1;
97                 if (num < 0)
98                         return -EINVAL;
99                 if (num == 0)
100                         num = 256;
101                 if (num > BR_MAX_PORTS)
102                         num = BR_MAX_PORTS;
103
104                 indices = kmalloc(num*sizeof(int), GFP_KERNEL);
105                 if (indices == NULL)
106                         return -ENOMEM;
107
108                 memset(indices, 0, num*sizeof(int));
109
110                 br_get_port_ifindices(br, indices, num);
111                 if (copy_to_user((void *)arg0, indices, num*sizeof(int)))
112                         num =  -EFAULT;
113                 kfree(indices);
114                 return num;
115         }
116
117         case BRCTL_SET_BRIDGE_FORWARD_DELAY:
118                 if (!capable(CAP_NET_ADMIN))
119                         return -EPERM;
120
121                 spin_lock_bh(&br->lock);
122                 br->bridge_forward_delay = clock_t_to_jiffies(arg0);
123                 if (br_is_root_bridge(br))
124                         br->forward_delay = br->bridge_forward_delay;
125                 spin_unlock_bh(&br->lock);
126                 return 0;
127
128         case BRCTL_SET_BRIDGE_HELLO_TIME:
129                 if (!capable(CAP_NET_ADMIN))
130                         return -EPERM;
131
132                 spin_lock_bh(&br->lock);
133                 br->bridge_hello_time = clock_t_to_jiffies(arg0);
134                 if (br_is_root_bridge(br))
135                         br->hello_time = br->bridge_hello_time;
136                 spin_unlock_bh(&br->lock);
137                 return 0;
138
139         case BRCTL_SET_BRIDGE_MAX_AGE:
140                 if (!capable(CAP_NET_ADMIN))
141                         return -EPERM;
142
143                 spin_lock_bh(&br->lock);
144                 br->bridge_max_age = clock_t_to_jiffies(arg0);
145                 if (br_is_root_bridge(br))
146                         br->max_age = br->bridge_max_age;
147                 spin_unlock_bh(&br->lock);
148                 return 0;
149
150         case BRCTL_SET_AGEING_TIME:
151                 if (!capable(CAP_NET_ADMIN))
152                         return -EPERM;
153
154                 br->ageing_time = clock_t_to_jiffies(arg0);
155                 return 0;
156
157         case BRCTL_GET_PORT_INFO:
158         {
159                 struct __port_info p;
160                 struct net_bridge_port *pt;
161
162                 rcu_read_lock();
163                 if ((pt = br_get_port(br, arg1)) == NULL) {
164                         rcu_read_unlock();
165                         return -EINVAL;
166                 }
167
168                 memset(&p, 0, sizeof(struct __port_info));
169                 memcpy(&p.designated_root, &pt->designated_root, 8);
170                 memcpy(&p.designated_bridge, &pt->designated_bridge, 8);
171                 p.port_id = pt->port_id;
172                 p.designated_port = pt->designated_port;
173                 p.path_cost = pt->path_cost;
174                 p.designated_cost = pt->designated_cost;
175                 p.state = pt->state;
176                 p.top_change_ack = pt->topology_change_ack;
177                 p.config_pending = pt->config_pending;
178                 p.message_age_timer_value = timer_residue(&pt->message_age_timer);
179                 p.forward_delay_timer_value = timer_residue(&pt->forward_delay_timer);
180                 p.hold_timer_value = timer_residue(&pt->hold_timer);
181
182                 rcu_read_unlock();
183
184                 if (copy_to_user((void *)arg0, &p, sizeof(p)))
185                         return -EFAULT;
186
187                 return 0;
188         }
189
190         case BRCTL_SET_BRIDGE_STP_STATE:
191                 if (!capable(CAP_NET_ADMIN))
192                         return -EPERM;
193
194                 br->stp_enabled = arg0?1:0;
195                 return 0;
196
197         case BRCTL_SET_BRIDGE_PRIORITY:
198                 if (!capable(CAP_NET_ADMIN))
199                         return -EPERM;
200
201                 spin_lock_bh(&br->lock);
202                 br_stp_set_bridge_priority(br, arg0);
203                 spin_unlock_bh(&br->lock);
204                 return 0;
205
206         case BRCTL_SET_PORT_PRIORITY:
207         {
208                 struct net_bridge_port *p;
209                 int ret = 0;
210
211                 if (!capable(CAP_NET_ADMIN))
212                         return -EPERM;
213
214                 if (arg1 >= (1<<(16-BR_PORT_BITS)))
215                         return -ERANGE;
216
217                 spin_lock_bh(&br->lock);
218                 if ((p = br_get_port(br, arg0)) == NULL) 
219                         ret = -EINVAL;
220                 else
221                         br_stp_set_port_priority(p, arg1);
222                 spin_unlock_bh(&br->lock);
223                 return ret;
224         }
225
226         case BRCTL_SET_PATH_COST:
227         {
228                 struct net_bridge_port *p;
229                 int ret = 0;
230
231                 if (!capable(CAP_NET_ADMIN))
232                         return -EPERM;
233
234                 spin_lock_bh(&br->lock);
235                 if ((p = br_get_port(br, arg0)) == NULL)
236                         ret = -EINVAL;
237                 else
238                         br_stp_set_path_cost(p, arg1);
239                 spin_unlock_bh(&br->lock);
240                 return ret;
241         }
242
243         case BRCTL_GET_FDB_ENTRIES:
244                 return br_fdb_get_entries(br, (void *)arg0, arg1, arg2);
245         }
246
247         return -EOPNOTSUPP;
248 }
249
250 static int br_ioctl_deviceless(unsigned int cmd,
251                                unsigned long arg0,
252                                unsigned long arg1)
253 {
254         switch (cmd)
255         {
256         case BRCTL_GET_VERSION:
257                 return BRCTL_VERSION;
258
259         case BRCTL_GET_BRIDGES:
260         {
261                 int *indices;
262                 int ret = 0;
263
264                 indices = kmalloc(arg1*sizeof(int), GFP_KERNEL);
265                 if (indices == NULL)
266                         return -ENOMEM;
267
268                 memset(indices, 0, arg1*sizeof(int));
269                 arg1 = br_get_bridge_ifindices(indices, arg1);
270
271                 ret = copy_to_user((void *)arg0, indices, arg1*sizeof(int))
272                         ? -EFAULT : arg1;
273
274                 kfree(indices);
275                 return ret;
276         }
277
278         case BRCTL_ADD_BRIDGE:
279         case BRCTL_DEL_BRIDGE:
280         {
281                 char buf[IFNAMSIZ];
282
283                 if (!capable(CAP_NET_ADMIN))
284                         return -EPERM;
285
286                 if (copy_from_user(buf, (void *)arg0, IFNAMSIZ))
287                         return -EFAULT;
288
289                 buf[IFNAMSIZ-1] = 0;
290
291                 if (cmd == BRCTL_ADD_BRIDGE)
292                         return br_add_bridge(buf);
293
294                 return br_del_bridge(buf);
295         }
296         }
297
298         return -EOPNOTSUPP;
299 }
300
301
302 int br_ioctl_deviceless_stub(unsigned long arg)
303 {
304         unsigned long i[3];
305
306         if (copy_from_user(i, (void *)arg, 3*sizeof(unsigned long)))
307                 return -EFAULT;
308
309         return br_ioctl_deviceless(i[0], i[1], i[2]);
310 }