]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * Forwarding database | |
3 | * Linux ethernet bridge | |
4 | * | |
5 | * Authors: | |
6 | * Lennert Buytenhek <[email protected]> | |
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 | ||
14 | #include <linux/kernel.h> | |
15 | #include <linux/init.h> | |
82524746 | 16 | #include <linux/rculist.h> |
1da177e4 LT |
17 | #include <linux/spinlock.h> |
18 | #include <linux/times.h> | |
19 | #include <linux/netdevice.h> | |
20 | #include <linux/etherdevice.h> | |
21 | #include <linux/jhash.h> | |
3f890923 | 22 | #include <linux/random.h> |
5a0e3ad6 | 23 | #include <linux/slab.h> |
1da177e4 | 24 | #include <asm/atomic.h> |
3f890923 | 25 | #include <asm/unaligned.h> |
1da177e4 LT |
26 | #include "br_private.h" |
27 | ||
e18b890b | 28 | static struct kmem_cache *br_fdb_cache __read_mostly; |
1da177e4 LT |
29 | static int fdb_insert(struct net_bridge *br, struct net_bridge_port *source, |
30 | const unsigned char *addr); | |
31 | ||
3f890923 SH |
32 | static u32 fdb_salt __read_mostly; |
33 | ||
87a596e0 | 34 | int __init br_fdb_init(void) |
1da177e4 LT |
35 | { |
36 | br_fdb_cache = kmem_cache_create("bridge_fdb_cache", | |
37 | sizeof(struct net_bridge_fdb_entry), | |
38 | 0, | |
20c2df83 | 39 | SLAB_HWCACHE_ALIGN, NULL); |
87a596e0 AM |
40 | if (!br_fdb_cache) |
41 | return -ENOMEM; | |
42 | ||
3f890923 | 43 | get_random_bytes(&fdb_salt, sizeof(fdb_salt)); |
87a596e0 | 44 | return 0; |
1da177e4 LT |
45 | } |
46 | ||
73afc906 | 47 | void br_fdb_fini(void) |
1da177e4 LT |
48 | { |
49 | kmem_cache_destroy(br_fdb_cache); | |
50 | } | |
51 | ||
52 | ||
53 | /* if topology_changing then use forward_delay (default 15 sec) | |
54 | * otherwise keep longer (default 5 minutes) | |
55 | */ | |
3f890923 | 56 | static inline unsigned long hold_time(const struct net_bridge *br) |
1da177e4 LT |
57 | { |
58 | return br->topology_change ? br->forward_delay : br->ageing_time; | |
59 | } | |
60 | ||
3f890923 | 61 | static inline int has_expired(const struct net_bridge *br, |
1da177e4 LT |
62 | const struct net_bridge_fdb_entry *fdb) |
63 | { | |
f64f9e71 JP |
64 | return !fdb->is_static && |
65 | time_before_eq(fdb->ageing_timer + hold_time(br), jiffies); | |
1da177e4 LT |
66 | } |
67 | ||
3f890923 | 68 | static inline int br_mac_hash(const unsigned char *mac) |
1da177e4 | 69 | { |
3f890923 SH |
70 | /* use 1 byte of OUI cnd 3 bytes of NIC */ |
71 | u32 key = get_unaligned((u32 *)(mac + 2)); | |
72 | return jhash_1word(key, fdb_salt) & (BR_HASH_SIZE - 1); | |
1da177e4 LT |
73 | } |
74 | ||
da678292 MM |
75 | static void fdb_rcu_free(struct rcu_head *head) |
76 | { | |
77 | struct net_bridge_fdb_entry *ent | |
78 | = container_of(head, struct net_bridge_fdb_entry, rcu); | |
79 | kmem_cache_free(br_fdb_cache, ent); | |
80 | } | |
81 | ||
3f890923 | 82 | static inline void fdb_delete(struct net_bridge_fdb_entry *f) |
1da177e4 LT |
83 | { |
84 | hlist_del_rcu(&f->hlist); | |
da678292 | 85 | call_rcu(&f->rcu, fdb_rcu_free); |
1da177e4 LT |
86 | } |
87 | ||
88 | void br_fdb_changeaddr(struct net_bridge_port *p, const unsigned char *newaddr) | |
89 | { | |
90 | struct net_bridge *br = p->br; | |
91 | int i; | |
9d6f229f | 92 | |
1da177e4 LT |
93 | spin_lock_bh(&br->hash_lock); |
94 | ||
95 | /* Search all chains since old address/hash is unknown */ | |
96 | for (i = 0; i < BR_HASH_SIZE; i++) { | |
97 | struct hlist_node *h; | |
98 | hlist_for_each(h, &br->hash[i]) { | |
99 | struct net_bridge_fdb_entry *f; | |
100 | ||
101 | f = hlist_entry(h, struct net_bridge_fdb_entry, hlist); | |
102 | if (f->dst == p && f->is_local) { | |
103 | /* maybe another port has same hw addr? */ | |
104 | struct net_bridge_port *op; | |
105 | list_for_each_entry(op, &br->port_list, list) { | |
9d6f229f | 106 | if (op != p && |
6ede2463 SH |
107 | !compare_ether_addr(op->dev->dev_addr, |
108 | f->addr.addr)) { | |
1da177e4 LT |
109 | f->dst = op; |
110 | goto insert; | |
111 | } | |
112 | } | |
113 | ||
114 | /* delete old one */ | |
115 | fdb_delete(f); | |
116 | goto insert; | |
117 | } | |
118 | } | |
119 | } | |
120 | insert: | |
121 | /* insert new address, may fail if invalid address or dup. */ | |
122 | fdb_insert(br, p, newaddr); | |
123 | ||
124 | spin_unlock_bh(&br->hash_lock); | |
125 | } | |
126 | ||
127 | void br_fdb_cleanup(unsigned long _data) | |
128 | { | |
129 | struct net_bridge *br = (struct net_bridge *)_data; | |
130 | unsigned long delay = hold_time(br); | |
25442e06 | 131 | unsigned long next_timer = jiffies + br->ageing_time; |
1da177e4 LT |
132 | int i; |
133 | ||
134 | spin_lock_bh(&br->hash_lock); | |
135 | for (i = 0; i < BR_HASH_SIZE; i++) { | |
136 | struct net_bridge_fdb_entry *f; | |
137 | struct hlist_node *h, *n; | |
138 | ||
139 | hlist_for_each_entry_safe(f, h, n, &br->hash[i], hlist) { | |
071f7722 BE |
140 | unsigned long this_timer; |
141 | if (f->is_static) | |
142 | continue; | |
143 | this_timer = f->ageing_timer + delay; | |
144 | if (time_before_eq(this_timer, jiffies)) | |
1da177e4 | 145 | fdb_delete(f); |
2bec008c | 146 | else if (time_before(this_timer, next_timer)) |
071f7722 | 147 | next_timer = this_timer; |
1da177e4 LT |
148 | } |
149 | } | |
150 | spin_unlock_bh(&br->hash_lock); | |
151 | ||
25442e06 | 152 | mod_timer(&br->gc_timer, round_jiffies_up(next_timer)); |
1da177e4 LT |
153 | } |
154 | ||
9cf63747 SH |
155 | /* Completely flush all dynamic entries in forwarding database.*/ |
156 | void br_fdb_flush(struct net_bridge *br) | |
157 | { | |
158 | int i; | |
159 | ||
160 | spin_lock_bh(&br->hash_lock); | |
161 | for (i = 0; i < BR_HASH_SIZE; i++) { | |
162 | struct net_bridge_fdb_entry *f; | |
163 | struct hlist_node *h, *n; | |
164 | hlist_for_each_entry_safe(f, h, n, &br->hash[i], hlist) { | |
165 | if (!f->is_static) | |
166 | fdb_delete(f); | |
167 | } | |
168 | } | |
169 | spin_unlock_bh(&br->hash_lock); | |
170 | } | |
1a620698 | 171 | |
9cf63747 SH |
172 | /* Flush all entries refering to a specific port. |
173 | * if do_all is set also flush static entries | |
174 | */ | |
1a620698 SH |
175 | void br_fdb_delete_by_port(struct net_bridge *br, |
176 | const struct net_bridge_port *p, | |
177 | int do_all) | |
1da177e4 LT |
178 | { |
179 | int i; | |
180 | ||
181 | spin_lock_bh(&br->hash_lock); | |
182 | for (i = 0; i < BR_HASH_SIZE; i++) { | |
183 | struct hlist_node *h, *g; | |
9d6f229f | 184 | |
1da177e4 LT |
185 | hlist_for_each_safe(h, g, &br->hash[i]) { |
186 | struct net_bridge_fdb_entry *f | |
187 | = hlist_entry(h, struct net_bridge_fdb_entry, hlist); | |
9d6f229f | 188 | if (f->dst != p) |
1da177e4 LT |
189 | continue; |
190 | ||
1a620698 SH |
191 | if (f->is_static && !do_all) |
192 | continue; | |
1da177e4 LT |
193 | /* |
194 | * if multiple ports all have the same device address | |
195 | * then when one port is deleted, assign | |
196 | * the local entry to other port | |
197 | */ | |
198 | if (f->is_local) { | |
199 | struct net_bridge_port *op; | |
200 | list_for_each_entry(op, &br->port_list, list) { | |
9d6f229f | 201 | if (op != p && |
6ede2463 SH |
202 | !compare_ether_addr(op->dev->dev_addr, |
203 | f->addr.addr)) { | |
1da177e4 LT |
204 | f->dst = op; |
205 | goto skip_delete; | |
206 | } | |
207 | } | |
208 | } | |
209 | ||
210 | fdb_delete(f); | |
211 | skip_delete: ; | |
212 | } | |
213 | } | |
214 | spin_unlock_bh(&br->hash_lock); | |
215 | } | |
216 | ||
217 | /* No locking or refcounting, assumes caller has no preempt (rcu_read_lock) */ | |
218 | struct net_bridge_fdb_entry *__br_fdb_get(struct net_bridge *br, | |
219 | const unsigned char *addr) | |
220 | { | |
221 | struct hlist_node *h; | |
222 | struct net_bridge_fdb_entry *fdb; | |
223 | ||
224 | hlist_for_each_entry_rcu(fdb, h, &br->hash[br_mac_hash(addr)], hlist) { | |
6ede2463 | 225 | if (!compare_ether_addr(fdb->addr.addr, addr)) { |
1da177e4 LT |
226 | if (unlikely(has_expired(br, fdb))) |
227 | break; | |
228 | return fdb; | |
229 | } | |
230 | } | |
231 | ||
232 | return NULL; | |
233 | } | |
234 | ||
da678292 MM |
235 | #if defined(CONFIG_ATM_LANE) || defined(CONFIG_ATM_LANE_MODULE) |
236 | /* Interface used by ATM LANE hook to test | |
237 | * if an addr is on some other bridge port */ | |
238 | int br_fdb_test_addr(struct net_device *dev, unsigned char *addr) | |
1da177e4 LT |
239 | { |
240 | struct net_bridge_fdb_entry *fdb; | |
da678292 MM |
241 | int ret; |
242 | ||
243 | if (!dev->br_port) | |
244 | return 0; | |
1da177e4 LT |
245 | |
246 | rcu_read_lock(); | |
da678292 MM |
247 | fdb = __br_fdb_get(dev->br_port->br, addr); |
248 | ret = fdb && fdb->dst->dev != dev && | |
249 | fdb->dst->state == BR_STATE_FORWARDING; | |
1da177e4 | 250 | rcu_read_unlock(); |
1da177e4 | 251 | |
da678292 | 252 | return ret; |
1da177e4 | 253 | } |
da678292 | 254 | #endif /* CONFIG_ATM_LANE */ |
1da177e4 LT |
255 | |
256 | /* | |
9d6f229f | 257 | * Fill buffer with forwarding table records in |
1da177e4 LT |
258 | * the API format. |
259 | */ | |
260 | int br_fdb_fillbuf(struct net_bridge *br, void *buf, | |
261 | unsigned long maxnum, unsigned long skip) | |
262 | { | |
263 | struct __fdb_entry *fe = buf; | |
264 | int i, num = 0; | |
265 | struct hlist_node *h; | |
266 | struct net_bridge_fdb_entry *f; | |
267 | ||
268 | memset(buf, 0, maxnum*sizeof(struct __fdb_entry)); | |
269 | ||
270 | rcu_read_lock(); | |
271 | for (i = 0; i < BR_HASH_SIZE; i++) { | |
272 | hlist_for_each_entry_rcu(f, h, &br->hash[i], hlist) { | |
273 | if (num >= maxnum) | |
274 | goto out; | |
275 | ||
9d6f229f | 276 | if (has_expired(br, f)) |
1da177e4 LT |
277 | continue; |
278 | ||
279 | if (skip) { | |
280 | --skip; | |
281 | continue; | |
282 | } | |
283 | ||
284 | /* convert from internal format to API */ | |
285 | memcpy(fe->mac_addr, f->addr.addr, ETH_ALEN); | |
ae4f8fca SH |
286 | |
287 | /* due to ABI compat need to split into hi/lo */ | |
1da177e4 | 288 | fe->port_no = f->dst->port_no; |
ae4f8fca SH |
289 | fe->port_hi = f->dst->port_no >> 8; |
290 | ||
1da177e4 LT |
291 | fe->is_local = f->is_local; |
292 | if (!f->is_static) | |
293 | fe->ageing_timer_value = jiffies_to_clock_t(jiffies - f->ageing_timer); | |
294 | ++fe; | |
295 | ++num; | |
296 | } | |
297 | } | |
298 | ||
299 | out: | |
300 | rcu_read_unlock(); | |
301 | ||
302 | return num; | |
303 | } | |
304 | ||
305 | static inline struct net_bridge_fdb_entry *fdb_find(struct hlist_head *head, | |
306 | const unsigned char *addr) | |
307 | { | |
308 | struct hlist_node *h; | |
309 | struct net_bridge_fdb_entry *fdb; | |
310 | ||
311 | hlist_for_each_entry_rcu(fdb, h, head, hlist) { | |
6ede2463 | 312 | if (!compare_ether_addr(fdb->addr.addr, addr)) |
1da177e4 LT |
313 | return fdb; |
314 | } | |
315 | return NULL; | |
316 | } | |
317 | ||
318 | static struct net_bridge_fdb_entry *fdb_create(struct hlist_head *head, | |
319 | struct net_bridge_port *source, | |
9d6f229f | 320 | const unsigned char *addr, |
1da177e4 LT |
321 | int is_local) |
322 | { | |
323 | struct net_bridge_fdb_entry *fdb; | |
324 | ||
325 | fdb = kmem_cache_alloc(br_fdb_cache, GFP_ATOMIC); | |
326 | if (fdb) { | |
327 | memcpy(fdb->addr.addr, addr, ETH_ALEN); | |
1da177e4 LT |
328 | hlist_add_head_rcu(&fdb->hlist, head); |
329 | ||
330 | fdb->dst = source; | |
331 | fdb->is_local = is_local; | |
332 | fdb->is_static = is_local; | |
333 | fdb->ageing_timer = jiffies; | |
334 | } | |
335 | return fdb; | |
336 | } | |
337 | ||
338 | static int fdb_insert(struct net_bridge *br, struct net_bridge_port *source, | |
339 | const unsigned char *addr) | |
340 | { | |
341 | struct hlist_head *head = &br->hash[br_mac_hash(addr)]; | |
342 | struct net_bridge_fdb_entry *fdb; | |
343 | ||
344 | if (!is_valid_ether_addr(addr)) | |
345 | return -EINVAL; | |
346 | ||
347 | fdb = fdb_find(head, addr); | |
348 | if (fdb) { | |
9d6f229f | 349 | /* it is okay to have multiple ports with same |
1da177e4 LT |
350 | * address, just use the first one. |
351 | */ | |
9d6f229f | 352 | if (fdb->is_local) |
1da177e4 | 353 | return 0; |
28a16c97 | 354 | br_warn(br, "adding interface %s with same address " |
1da177e4 LT |
355 | "as a received packet\n", |
356 | source->dev->name); | |
357 | fdb_delete(fdb); | |
9d6f229f | 358 | } |
1da177e4 LT |
359 | |
360 | if (!fdb_create(head, source, addr, 1)) | |
361 | return -ENOMEM; | |
362 | ||
363 | return 0; | |
364 | } | |
365 | ||
366 | int br_fdb_insert(struct net_bridge *br, struct net_bridge_port *source, | |
367 | const unsigned char *addr) | |
368 | { | |
369 | int ret; | |
370 | ||
371 | spin_lock_bh(&br->hash_lock); | |
372 | ret = fdb_insert(br, source, addr); | |
373 | spin_unlock_bh(&br->hash_lock); | |
374 | return ret; | |
375 | } | |
376 | ||
377 | void br_fdb_update(struct net_bridge *br, struct net_bridge_port *source, | |
378 | const unsigned char *addr) | |
379 | { | |
380 | struct hlist_head *head = &br->hash[br_mac_hash(addr)]; | |
381 | struct net_bridge_fdb_entry *fdb; | |
382 | ||
383 | /* some users want to always flood. */ | |
384 | if (hold_time(br) == 0) | |
385 | return; | |
386 | ||
df1c0b84 SH |
387 | /* ignore packets unless we are using this port */ |
388 | if (!(source->state == BR_STATE_LEARNING || | |
389 | source->state == BR_STATE_FORWARDING)) | |
390 | return; | |
391 | ||
1da177e4 LT |
392 | fdb = fdb_find(head, addr); |
393 | if (likely(fdb)) { | |
394 | /* attempt to update an entry for a local interface */ | |
395 | if (unlikely(fdb->is_local)) { | |
9d6f229f | 396 | if (net_ratelimit()) |
28a16c97 | 397 | br_warn(br, "received packet on %s with " |
398 | "own address as source address\n", | |
399 | source->dev->name); | |
1da177e4 LT |
400 | } else { |
401 | /* fastpath: update of existing entry */ | |
402 | fdb->dst = source; | |
403 | fdb->ageing_timer = jiffies; | |
404 | } | |
405 | } else { | |
f8ae737d | 406 | spin_lock(&br->hash_lock); |
1da177e4 LT |
407 | if (!fdb_find(head, addr)) |
408 | fdb_create(head, source, addr, 0); | |
409 | /* else we lose race and someone else inserts | |
410 | * it first, don't bother updating | |
411 | */ | |
f8ae737d | 412 | spin_unlock(&br->hash_lock); |
1da177e4 | 413 | } |
1da177e4 | 414 | } |