1 /*
2 * Userspace interface
3 * Linux ethernet bridge
4 *
5 * Authors:
6 * Lennert Buytenhek <buytenh@gnu.org>
7 *
8 * $Id: br_if.c,v 1.6.2.1 2001/12/24 00:59:27 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_arp.h>
18 #include <linux/if_bridge.h>
19 #include <linux/inetdevice.h>
20 #include <linux/rtnetlink.h>
21 #include <linux/brlock.h>
22 #include <linux/etherdevice.h>
23 #include <asm/uaccess.h>
24 #include "br_private.h"
25
br_initial_port_cost(struct net_device * dev)26 static int br_initial_port_cost(struct net_device *dev)
27 {
28 if (!strncmp(dev->name, "lec", 3))
29 return 7;
30
31 if (!strncmp(dev->name, "eth", 3))
32 return 100; /* FIXME handle 100Mbps */
33
34 if (!strncmp(dev->name, "plip", 4))
35 return 2500;
36
37 return 100;
38 }
39
40 /* called under BR_NETPROTO_LOCK and bridge lock */
__br_del_if(struct net_bridge * br,struct net_device * dev)41 static int __br_del_if(struct net_bridge *br, struct net_device *dev)
42 {
43 struct net_bridge_port *p;
44 struct net_bridge_port **pptr;
45
46 if ((p = dev->br_port) == NULL || p->br != br)
47 return -EINVAL;
48
49 br_stp_disable_port(p);
50
51 dev_set_promiscuity(dev, -1);
52 dev->br_port = NULL;
53
54 pptr = &br->port_list;
55 while (*pptr != NULL) {
56 if (*pptr == p) {
57 *pptr = p->next;
58 break;
59 }
60
61 pptr = &((*pptr)->next);
62 }
63
64 br_fdb_delete_by_port(br, p);
65 kfree(p);
66 dev_put(dev);
67
68 return 0;
69 }
70
del_ifs(struct net_bridge * br)71 static void del_ifs(struct net_bridge *br)
72 {
73 br_write_lock_bh(BR_NETPROTO_LOCK);
74 write_lock(&br->lock);
75 while (br->port_list != NULL)
76 __br_del_if(br, br->port_list->dev);
77 write_unlock(&br->lock);
78 br_write_unlock_bh(BR_NETPROTO_LOCK);
79 }
80
new_nb(char * name)81 static struct net_bridge *new_nb(char *name)
82 {
83 struct net_bridge *br;
84 struct net_device *dev;
85
86 if ((br = kmalloc(sizeof(*br), GFP_KERNEL)) == NULL)
87 return NULL;
88
89 memset(br, 0, sizeof(*br));
90 dev = &br->dev;
91
92 strncpy(dev->name, name, IFNAMSIZ);
93 dev->priv = br;
94 ether_setup(dev);
95 br_dev_setup(dev);
96
97 br->lock = RW_LOCK_UNLOCKED;
98 br->hash_lock = RW_LOCK_UNLOCKED;
99
100 br->bridge_id.prio[0] = 0x80;
101 br->bridge_id.prio[1] = 0x00;
102 memset(br->bridge_id.addr, 0, ETH_ALEN);
103
104 br->stp_enabled = 0;
105 br->designated_root = br->bridge_id;
106 br->root_path_cost = 0;
107 br->root_port = 0;
108 br->bridge_max_age = br->max_age = 20 * HZ;
109 br->bridge_hello_time = br->hello_time = 2 * HZ;
110 br->bridge_forward_delay = br->forward_delay = 15 * HZ;
111 br->topology_change = 0;
112 br->topology_change_detected = 0;
113 br_timer_clear(&br->hello_timer);
114 br_timer_clear(&br->tcn_timer);
115 br_timer_clear(&br->topology_change_timer);
116
117 br->ageing_time = 300 * HZ;
118 br->gc_interval = 4 * HZ;
119
120 return br;
121 }
122
123 /* called under bridge lock */
new_nbp(struct net_bridge * br,struct net_device * dev)124 static struct net_bridge_port *new_nbp(struct net_bridge *br, struct net_device *dev)
125 {
126 int i;
127 struct net_bridge_port *p;
128
129 p = kmalloc(sizeof(*p), GFP_ATOMIC);
130 if (p == NULL)
131 return p;
132
133 memset(p, 0, sizeof(*p));
134 p->br = br;
135 p->dev = dev;
136 p->path_cost = br_initial_port_cost(dev);
137 p->priority = 0x80;
138
139 for (i=1;i<255;i++)
140 if (br_get_port(br, i) == NULL)
141 break;
142
143 if (i == 255) {
144 kfree(p);
145 return NULL;
146 }
147
148 dev->br_port = p;
149
150 p->port_no = i;
151 br_init_port(p);
152 p->state = BR_STATE_DISABLED;
153
154 p->next = br->port_list;
155 br->port_list = p;
156
157 return p;
158 }
159
br_add_bridge(char * name)160 int br_add_bridge(char *name)
161 {
162 struct net_bridge *br;
163 struct net_device *dev;
164 int err;
165
166 if ((br = new_nb(name)) == NULL)
167 return -ENOMEM;
168
169 dev = &br->dev;
170 if (strchr(dev->name, '%')) {
171 err = dev_alloc_name(dev, dev->name);
172 if (err < 0)
173 goto out;
174 }
175
176 err = register_netdevice(dev);
177 if (err == 0)
178 br_inc_use_count();
179
180 out:
181 return err;
182 }
183
br_del_bridge(char * name)184 int br_del_bridge(char *name)
185 {
186 struct net_device *dev;
187 struct net_bridge *br;
188
189 dev = __dev_get_by_name(name);
190 if (!dev)
191 return -ENXIO;
192
193 if (dev->hard_start_xmit != br_dev_xmit)
194 return -EPERM;
195
196 if (dev->flags & IFF_UP)
197 return -EBUSY;
198
199 br = dev->priv;
200 BUG_ON(&br->dev != dev);
201
202 del_ifs(br);
203
204 unregister_netdevice(dev);
205 kfree(br);
206 br_dec_use_count();
207
208 return 0;
209 }
210
br_add_if(struct net_bridge * br,struct net_device * dev)211 int br_add_if(struct net_bridge *br, struct net_device *dev)
212 {
213 struct net_bridge_port *p;
214
215 if (dev->br_port != NULL)
216 return -EBUSY;
217
218 if (dev->flags & IFF_LOOPBACK || dev->type != ARPHRD_ETHER)
219 return -EINVAL;
220
221 if (dev->hard_start_xmit == br_dev_xmit)
222 return -ELOOP;
223
224 if (!is_valid_ether_addr(dev->dev_addr))
225 return -EADDRNOTAVAIL;
226
227 dev_hold(dev);
228 write_lock_bh(&br->lock);
229 if ((p = new_nbp(br, dev)) == NULL) {
230 write_unlock_bh(&br->lock);
231 dev_put(dev);
232 return -EXFULL;
233 }
234
235 dev_set_promiscuity(dev, 1);
236
237 br_stp_recalculate_bridge_id(br);
238 br_fdb_insert(br, p, dev->dev_addr, 1);
239 if ((br->dev.flags & IFF_UP) && (dev->flags & IFF_UP))
240 br_stp_enable_port(p);
241 write_unlock_bh(&br->lock);
242
243 return 0;
244 }
245
br_del_if(struct net_bridge * br,struct net_device * dev)246 int br_del_if(struct net_bridge *br, struct net_device *dev)
247 {
248 int retval;
249
250 br_write_lock_bh(BR_NETPROTO_LOCK);
251 write_lock(&br->lock);
252 retval = __br_del_if(br, dev);
253 br_stp_recalculate_bridge_id(br);
254 write_unlock(&br->lock);
255 br_write_unlock_bh(BR_NETPROTO_LOCK);
256
257 return retval;
258 }
259
br_get_bridge_ifindices(int * indices,int num)260 int br_get_bridge_ifindices(int *indices, int num)
261 {
262 struct net_device *dev;
263 int i = 0;
264
265 for (dev = dev_base; i < num && dev != NULL; dev = dev->next) {
266 if (dev->hard_start_xmit == br_dev_xmit)
267 indices[i++] = dev->ifindex;
268 }
269
270 return i;
271 }
272
273 /* called under ioctl_lock */
br_get_port_ifindices(struct net_bridge * br,int * ifindices)274 void br_get_port_ifindices(struct net_bridge *br, int *ifindices)
275 {
276 struct net_bridge_port *p;
277
278 p = br->port_list;
279 while (p != NULL) {
280 ifindices[p->port_no] = p->dev->ifindex;
281 p = p->next;
282 }
283 }
284