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