Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * Forwarding database | |
3 | * Linux ethernet bridge | |
4 | * | |
5 | * Authors: | |
6 | * Lennert Buytenhek <buytenh@gnu.org> | |
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> |
60063497 | 24 | #include <linux/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); | |
31e8a49c | 31 | static void fdb_notify(struct net_bridge *br, |
32 | const struct net_bridge_fdb_entry *, int); | |
1da177e4 | 33 | |
3f890923 SH |
34 | static u32 fdb_salt __read_mostly; |
35 | ||
87a596e0 | 36 | int __init br_fdb_init(void) |
1da177e4 LT |
37 | { |
38 | br_fdb_cache = kmem_cache_create("bridge_fdb_cache", | |
39 | sizeof(struct net_bridge_fdb_entry), | |
40 | 0, | |
20c2df83 | 41 | SLAB_HWCACHE_ALIGN, NULL); |
87a596e0 AM |
42 | if (!br_fdb_cache) |
43 | return -ENOMEM; | |
44 | ||
3f890923 | 45 | get_random_bytes(&fdb_salt, sizeof(fdb_salt)); |
87a596e0 | 46 | return 0; |
1da177e4 LT |
47 | } |
48 | ||
73afc906 | 49 | void br_fdb_fini(void) |
1da177e4 LT |
50 | { |
51 | kmem_cache_destroy(br_fdb_cache); | |
52 | } | |
53 | ||
54 | ||
55 | /* if topology_changing then use forward_delay (default 15 sec) | |
56 | * otherwise keep longer (default 5 minutes) | |
57 | */ | |
3f890923 | 58 | static inline unsigned long hold_time(const struct net_bridge *br) |
1da177e4 LT |
59 | { |
60 | return br->topology_change ? br->forward_delay : br->ageing_time; | |
61 | } | |
62 | ||
3f890923 | 63 | static inline int has_expired(const struct net_bridge *br, |
1da177e4 LT |
64 | const struct net_bridge_fdb_entry *fdb) |
65 | { | |
f64f9e71 | 66 | return !fdb->is_static && |
7cd8861a | 67 | time_before_eq(fdb->updated + hold_time(br), jiffies); |
1da177e4 LT |
68 | } |
69 | ||
3f890923 | 70 | static inline int br_mac_hash(const unsigned char *mac) |
1da177e4 | 71 | { |
3f890923 SH |
72 | /* use 1 byte of OUI cnd 3 bytes of NIC */ |
73 | u32 key = get_unaligned((u32 *)(mac + 2)); | |
74 | return jhash_1word(key, fdb_salt) & (BR_HASH_SIZE - 1); | |
1da177e4 LT |
75 | } |
76 | ||
da678292 MM |
77 | static void fdb_rcu_free(struct rcu_head *head) |
78 | { | |
79 | struct net_bridge_fdb_entry *ent | |
80 | = container_of(head, struct net_bridge_fdb_entry, rcu); | |
81 | kmem_cache_free(br_fdb_cache, ent); | |
82 | } | |
83 | ||
31e8a49c | 84 | static void fdb_delete(struct net_bridge *br, struct net_bridge_fdb_entry *f) |
1da177e4 LT |
85 | { |
86 | hlist_del_rcu(&f->hlist); | |
31e8a49c | 87 | fdb_notify(br, f, RTM_DELNEIGH); |
da678292 | 88 | call_rcu(&f->rcu, fdb_rcu_free); |
1da177e4 LT |
89 | } |
90 | ||
91 | void br_fdb_changeaddr(struct net_bridge_port *p, const unsigned char *newaddr) | |
92 | { | |
93 | struct net_bridge *br = p->br; | |
94 | int i; | |
9d6f229f | 95 | |
1da177e4 LT |
96 | spin_lock_bh(&br->hash_lock); |
97 | ||
98 | /* Search all chains since old address/hash is unknown */ | |
99 | for (i = 0; i < BR_HASH_SIZE; i++) { | |
100 | struct hlist_node *h; | |
101 | hlist_for_each(h, &br->hash[i]) { | |
102 | struct net_bridge_fdb_entry *f; | |
103 | ||
104 | f = hlist_entry(h, struct net_bridge_fdb_entry, hlist); | |
105 | if (f->dst == p && f->is_local) { | |
106 | /* maybe another port has same hw addr? */ | |
107 | struct net_bridge_port *op; | |
108 | list_for_each_entry(op, &br->port_list, list) { | |
9d6f229f | 109 | if (op != p && |
6ede2463 SH |
110 | !compare_ether_addr(op->dev->dev_addr, |
111 | f->addr.addr)) { | |
1da177e4 LT |
112 | f->dst = op; |
113 | goto insert; | |
114 | } | |
115 | } | |
116 | ||
117 | /* delete old one */ | |
31e8a49c | 118 | fdb_delete(br, f); |
1da177e4 LT |
119 | goto insert; |
120 | } | |
121 | } | |
122 | } | |
123 | insert: | |
124 | /* insert new address, may fail if invalid address or dup. */ | |
125 | fdb_insert(br, p, newaddr); | |
126 | ||
127 | spin_unlock_bh(&br->hash_lock); | |
128 | } | |
129 | ||
130 | void br_fdb_cleanup(unsigned long _data) | |
131 | { | |
132 | struct net_bridge *br = (struct net_bridge *)_data; | |
133 | unsigned long delay = hold_time(br); | |
25442e06 | 134 | unsigned long next_timer = jiffies + br->ageing_time; |
1da177e4 LT |
135 | int i; |
136 | ||
137 | spin_lock_bh(&br->hash_lock); | |
138 | for (i = 0; i < BR_HASH_SIZE; i++) { | |
139 | struct net_bridge_fdb_entry *f; | |
140 | struct hlist_node *h, *n; | |
141 | ||
142 | hlist_for_each_entry_safe(f, h, n, &br->hash[i], hlist) { | |
071f7722 BE |
143 | unsigned long this_timer; |
144 | if (f->is_static) | |
145 | continue; | |
7cd8861a | 146 | this_timer = f->updated + delay; |
071f7722 | 147 | if (time_before_eq(this_timer, jiffies)) |
31e8a49c | 148 | fdb_delete(br, f); |
2bec008c | 149 | else if (time_before(this_timer, next_timer)) |
071f7722 | 150 | next_timer = this_timer; |
1da177e4 LT |
151 | } |
152 | } | |
153 | spin_unlock_bh(&br->hash_lock); | |
154 | ||
25442e06 | 155 | mod_timer(&br->gc_timer, round_jiffies_up(next_timer)); |
1da177e4 LT |
156 | } |
157 | ||
9cf63747 SH |
158 | /* Completely flush all dynamic entries in forwarding database.*/ |
159 | void br_fdb_flush(struct net_bridge *br) | |
160 | { | |
161 | int i; | |
162 | ||
163 | spin_lock_bh(&br->hash_lock); | |
164 | for (i = 0; i < BR_HASH_SIZE; i++) { | |
165 | struct net_bridge_fdb_entry *f; | |
166 | struct hlist_node *h, *n; | |
167 | hlist_for_each_entry_safe(f, h, n, &br->hash[i], hlist) { | |
168 | if (!f->is_static) | |
31e8a49c | 169 | fdb_delete(br, f); |
9cf63747 SH |
170 | } |
171 | } | |
172 | spin_unlock_bh(&br->hash_lock); | |
173 | } | |
1a620698 | 174 | |
25985edc | 175 | /* Flush all entries referring to a specific port. |
9cf63747 SH |
176 | * if do_all is set also flush static entries |
177 | */ | |
1a620698 SH |
178 | void br_fdb_delete_by_port(struct net_bridge *br, |
179 | const struct net_bridge_port *p, | |
180 | int do_all) | |
1da177e4 LT |
181 | { |
182 | int i; | |
183 | ||
184 | spin_lock_bh(&br->hash_lock); | |
185 | for (i = 0; i < BR_HASH_SIZE; i++) { | |
186 | struct hlist_node *h, *g; | |
9d6f229f | 187 | |
1da177e4 LT |
188 | hlist_for_each_safe(h, g, &br->hash[i]) { |
189 | struct net_bridge_fdb_entry *f | |
190 | = hlist_entry(h, struct net_bridge_fdb_entry, hlist); | |
9d6f229f | 191 | if (f->dst != p) |
1da177e4 LT |
192 | continue; |
193 | ||
1a620698 SH |
194 | if (f->is_static && !do_all) |
195 | continue; | |
1da177e4 LT |
196 | /* |
197 | * if multiple ports all have the same device address | |
198 | * then when one port is deleted, assign | |
199 | * the local entry to other port | |
200 | */ | |
201 | if (f->is_local) { | |
202 | struct net_bridge_port *op; | |
203 | list_for_each_entry(op, &br->port_list, list) { | |
9d6f229f | 204 | if (op != p && |
6ede2463 SH |
205 | !compare_ether_addr(op->dev->dev_addr, |
206 | f->addr.addr)) { | |
1da177e4 LT |
207 | f->dst = op; |
208 | goto skip_delete; | |
209 | } | |
210 | } | |
211 | } | |
212 | ||
31e8a49c | 213 | fdb_delete(br, f); |
1da177e4 LT |
214 | skip_delete: ; |
215 | } | |
216 | } | |
217 | spin_unlock_bh(&br->hash_lock); | |
218 | } | |
219 | ||
eeaf61d8 | 220 | /* No locking or refcounting, assumes caller has rcu_read_lock */ |
1da177e4 LT |
221 | struct net_bridge_fdb_entry *__br_fdb_get(struct net_bridge *br, |
222 | const unsigned char *addr) | |
223 | { | |
224 | struct hlist_node *h; | |
225 | struct net_bridge_fdb_entry *fdb; | |
226 | ||
227 | hlist_for_each_entry_rcu(fdb, h, &br->hash[br_mac_hash(addr)], hlist) { | |
6ede2463 | 228 | if (!compare_ether_addr(fdb->addr.addr, addr)) { |
1da177e4 LT |
229 | if (unlikely(has_expired(br, fdb))) |
230 | break; | |
231 | return fdb; | |
232 | } | |
233 | } | |
234 | ||
235 | return NULL; | |
236 | } | |
237 | ||
da678292 MM |
238 | #if defined(CONFIG_ATM_LANE) || defined(CONFIG_ATM_LANE_MODULE) |
239 | /* Interface used by ATM LANE hook to test | |
240 | * if an addr is on some other bridge port */ | |
241 | int br_fdb_test_addr(struct net_device *dev, unsigned char *addr) | |
1da177e4 LT |
242 | { |
243 | struct net_bridge_fdb_entry *fdb; | |
b5ed54e9 | 244 | struct net_bridge_port *port; |
da678292 MM |
245 | int ret; |
246 | ||
1da177e4 | 247 | rcu_read_lock(); |
b5ed54e9 | 248 | port = br_port_get_rcu(dev); |
249 | if (!port) | |
250 | ret = 0; | |
251 | else { | |
252 | fdb = __br_fdb_get(port->br, addr); | |
253 | ret = fdb && fdb->dst->dev != dev && | |
254 | fdb->dst->state == BR_STATE_FORWARDING; | |
255 | } | |
1da177e4 | 256 | rcu_read_unlock(); |
1da177e4 | 257 | |
da678292 | 258 | return ret; |
1da177e4 | 259 | } |
da678292 | 260 | #endif /* CONFIG_ATM_LANE */ |
1da177e4 LT |
261 | |
262 | /* | |
9d6f229f | 263 | * Fill buffer with forwarding table records in |
1da177e4 LT |
264 | * the API format. |
265 | */ | |
266 | int br_fdb_fillbuf(struct net_bridge *br, void *buf, | |
267 | unsigned long maxnum, unsigned long skip) | |
268 | { | |
269 | struct __fdb_entry *fe = buf; | |
270 | int i, num = 0; | |
271 | struct hlist_node *h; | |
272 | struct net_bridge_fdb_entry *f; | |
273 | ||
274 | memset(buf, 0, maxnum*sizeof(struct __fdb_entry)); | |
275 | ||
276 | rcu_read_lock(); | |
277 | for (i = 0; i < BR_HASH_SIZE; i++) { | |
278 | hlist_for_each_entry_rcu(f, h, &br->hash[i], hlist) { | |
279 | if (num >= maxnum) | |
280 | goto out; | |
281 | ||
9d6f229f | 282 | if (has_expired(br, f)) |
1da177e4 LT |
283 | continue; |
284 | ||
285 | if (skip) { | |
286 | --skip; | |
287 | continue; | |
288 | } | |
289 | ||
290 | /* convert from internal format to API */ | |
291 | memcpy(fe->mac_addr, f->addr.addr, ETH_ALEN); | |
ae4f8fca SH |
292 | |
293 | /* due to ABI compat need to split into hi/lo */ | |
1da177e4 | 294 | fe->port_no = f->dst->port_no; |
ae4f8fca SH |
295 | fe->port_hi = f->dst->port_no >> 8; |
296 | ||
1da177e4 LT |
297 | fe->is_local = f->is_local; |
298 | if (!f->is_static) | |
7cd8861a | 299 | fe->ageing_timer_value = jiffies_to_clock_t(jiffies - f->updated); |
1da177e4 LT |
300 | ++fe; |
301 | ++num; | |
302 | } | |
303 | } | |
304 | ||
305 | out: | |
306 | rcu_read_unlock(); | |
307 | ||
308 | return num; | |
309 | } | |
310 | ||
664de48b | 311 | static struct net_bridge_fdb_entry *fdb_find(struct hlist_head *head, |
312 | const unsigned char *addr) | |
313 | { | |
314 | struct hlist_node *h; | |
315 | struct net_bridge_fdb_entry *fdb; | |
316 | ||
317 | hlist_for_each_entry(fdb, h, head, hlist) { | |
318 | if (!compare_ether_addr(fdb->addr.addr, addr)) | |
319 | return fdb; | |
320 | } | |
321 | return NULL; | |
322 | } | |
323 | ||
324 | static struct net_bridge_fdb_entry *fdb_find_rcu(struct hlist_head *head, | |
325 | const unsigned char *addr) | |
1da177e4 LT |
326 | { |
327 | struct hlist_node *h; | |
328 | struct net_bridge_fdb_entry *fdb; | |
329 | ||
330 | hlist_for_each_entry_rcu(fdb, h, head, hlist) { | |
6ede2463 | 331 | if (!compare_ether_addr(fdb->addr.addr, addr)) |
1da177e4 LT |
332 | return fdb; |
333 | } | |
334 | return NULL; | |
335 | } | |
336 | ||
337 | static struct net_bridge_fdb_entry *fdb_create(struct hlist_head *head, | |
338 | struct net_bridge_port *source, | |
03e9b64b | 339 | const unsigned char *addr) |
1da177e4 LT |
340 | { |
341 | struct net_bridge_fdb_entry *fdb; | |
342 | ||
343 | fdb = kmem_cache_alloc(br_fdb_cache, GFP_ATOMIC); | |
344 | if (fdb) { | |
345 | memcpy(fdb->addr.addr, addr, ETH_ALEN); | |
1da177e4 | 346 | fdb->dst = source; |
03e9b64b | 347 | fdb->is_local = 0; |
348 | fdb->is_static = 0; | |
7cd8861a | 349 | fdb->updated = fdb->used = jiffies; |
1158f762 | 350 | hlist_add_head_rcu(&fdb->hlist, head); |
1da177e4 LT |
351 | } |
352 | return fdb; | |
353 | } | |
354 | ||
355 | static int fdb_insert(struct net_bridge *br, struct net_bridge_port *source, | |
356 | const unsigned char *addr) | |
357 | { | |
358 | struct hlist_head *head = &br->hash[br_mac_hash(addr)]; | |
359 | struct net_bridge_fdb_entry *fdb; | |
360 | ||
361 | if (!is_valid_ether_addr(addr)) | |
362 | return -EINVAL; | |
363 | ||
364 | fdb = fdb_find(head, addr); | |
365 | if (fdb) { | |
9d6f229f | 366 | /* it is okay to have multiple ports with same |
1da177e4 LT |
367 | * address, just use the first one. |
368 | */ | |
9d6f229f | 369 | if (fdb->is_local) |
1da177e4 | 370 | return 0; |
28a16c97 | 371 | br_warn(br, "adding interface %s with same address " |
1da177e4 LT |
372 | "as a received packet\n", |
373 | source->dev->name); | |
31e8a49c | 374 | fdb_delete(br, fdb); |
9d6f229f | 375 | } |
1da177e4 | 376 | |
03e9b64b | 377 | fdb = fdb_create(head, source, addr); |
378 | if (!fdb) | |
1da177e4 LT |
379 | return -ENOMEM; |
380 | ||
03e9b64b | 381 | fdb->is_local = fdb->is_static = 1; |
31e8a49c | 382 | fdb_notify(br, fdb, RTM_NEWNEIGH); |
1da177e4 LT |
383 | return 0; |
384 | } | |
385 | ||
03e9b64b | 386 | /* Add entry for local address of interface */ |
1da177e4 LT |
387 | int br_fdb_insert(struct net_bridge *br, struct net_bridge_port *source, |
388 | const unsigned char *addr) | |
389 | { | |
390 | int ret; | |
391 | ||
392 | spin_lock_bh(&br->hash_lock); | |
393 | ret = fdb_insert(br, source, addr); | |
394 | spin_unlock_bh(&br->hash_lock); | |
395 | return ret; | |
396 | } | |
397 | ||
398 | void br_fdb_update(struct net_bridge *br, struct net_bridge_port *source, | |
399 | const unsigned char *addr) | |
400 | { | |
401 | struct hlist_head *head = &br->hash[br_mac_hash(addr)]; | |
402 | struct net_bridge_fdb_entry *fdb; | |
403 | ||
404 | /* some users want to always flood. */ | |
405 | if (hold_time(br) == 0) | |
406 | return; | |
407 | ||
df1c0b84 SH |
408 | /* ignore packets unless we are using this port */ |
409 | if (!(source->state == BR_STATE_LEARNING || | |
410 | source->state == BR_STATE_FORWARDING)) | |
411 | return; | |
412 | ||
664de48b | 413 | fdb = fdb_find_rcu(head, addr); |
1da177e4 LT |
414 | if (likely(fdb)) { |
415 | /* attempt to update an entry for a local interface */ | |
416 | if (unlikely(fdb->is_local)) { | |
9d6f229f | 417 | if (net_ratelimit()) |
28a16c97 | 418 | br_warn(br, "received packet on %s with " |
419 | "own address as source address\n", | |
420 | source->dev->name); | |
1da177e4 LT |
421 | } else { |
422 | /* fastpath: update of existing entry */ | |
423 | fdb->dst = source; | |
7cd8861a | 424 | fdb->updated = jiffies; |
1da177e4 LT |
425 | } |
426 | } else { | |
f8ae737d | 427 | spin_lock(&br->hash_lock); |
f58ee4e1 | 428 | if (likely(!fdb_find(head, addr))) { |
429 | fdb = fdb_create(head, source, addr); | |
430 | if (fdb) | |
31e8a49c | 431 | fdb_notify(br, fdb, RTM_NEWNEIGH); |
f58ee4e1 | 432 | } |
1da177e4 LT |
433 | /* else we lose race and someone else inserts |
434 | * it first, don't bother updating | |
435 | */ | |
f8ae737d | 436 | spin_unlock(&br->hash_lock); |
1da177e4 | 437 | } |
1da177e4 | 438 | } |
b078f0df | 439 | |
440 | static int fdb_to_nud(const struct net_bridge_fdb_entry *fdb) | |
441 | { | |
442 | if (fdb->is_local) | |
443 | return NUD_PERMANENT; | |
444 | else if (fdb->is_static) | |
445 | return NUD_NOARP; | |
446 | else if (has_expired(fdb->dst->br, fdb)) | |
447 | return NUD_STALE; | |
448 | else | |
449 | return NUD_REACHABLE; | |
450 | } | |
451 | ||
31e8a49c | 452 | static int fdb_fill_info(struct sk_buff *skb, const struct net_bridge *br, |
b078f0df | 453 | const struct net_bridge_fdb_entry *fdb, |
454 | u32 pid, u32 seq, int type, unsigned int flags) | |
455 | { | |
456 | unsigned long now = jiffies; | |
457 | struct nda_cacheinfo ci; | |
458 | struct nlmsghdr *nlh; | |
459 | struct ndmsg *ndm; | |
460 | ||
461 | nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), flags); | |
462 | if (nlh == NULL) | |
463 | return -EMSGSIZE; | |
464 | ||
b078f0df | 465 | ndm = nlmsg_data(nlh); |
466 | ndm->ndm_family = AF_BRIDGE; | |
467 | ndm->ndm_pad1 = 0; | |
468 | ndm->ndm_pad2 = 0; | |
469 | ndm->ndm_flags = 0; | |
470 | ndm->ndm_type = 0; | |
471 | ndm->ndm_ifindex = fdb->dst->dev->ifindex; | |
472 | ndm->ndm_state = fdb_to_nud(fdb); | |
473 | ||
474 | NLA_PUT(skb, NDA_LLADDR, ETH_ALEN, &fdb->addr); | |
475 | ||
476 | ci.ndm_used = jiffies_to_clock_t(now - fdb->used); | |
477 | ci.ndm_confirmed = 0; | |
478 | ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated); | |
479 | ci.ndm_refcnt = 0; | |
480 | NLA_PUT(skb, NDA_CACHEINFO, sizeof(ci), &ci); | |
481 | ||
482 | return nlmsg_end(skb, nlh); | |
483 | ||
484 | nla_put_failure: | |
485 | nlmsg_cancel(skb, nlh); | |
486 | return -EMSGSIZE; | |
487 | } | |
488 | ||
489 | static inline size_t fdb_nlmsg_size(void) | |
490 | { | |
491 | return NLMSG_ALIGN(sizeof(struct ndmsg)) | |
492 | + nla_total_size(ETH_ALEN) /* NDA_LLADDR */ | |
493 | + nla_total_size(sizeof(struct nda_cacheinfo)); | |
494 | } | |
495 | ||
31e8a49c | 496 | static void fdb_notify(struct net_bridge *br, |
497 | const struct net_bridge_fdb_entry *fdb, int type) | |
b078f0df | 498 | { |
31e8a49c | 499 | struct net *net = dev_net(br->dev); |
b078f0df | 500 | struct sk_buff *skb; |
501 | int err = -ENOBUFS; | |
502 | ||
503 | skb = nlmsg_new(fdb_nlmsg_size(), GFP_ATOMIC); | |
504 | if (skb == NULL) | |
505 | goto errout; | |
506 | ||
31e8a49c | 507 | err = fdb_fill_info(skb, br, fdb, 0, 0, type, 0); |
b078f0df | 508 | if (err < 0) { |
509 | /* -EMSGSIZE implies BUG in fdb_nlmsg_size() */ | |
510 | WARN_ON(err == -EMSGSIZE); | |
511 | kfree_skb(skb); | |
512 | goto errout; | |
513 | } | |
514 | rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC); | |
515 | return; | |
516 | errout: | |
517 | if (err < 0) | |
518 | rtnl_set_sk_err(net, RTNLGRP_NEIGH, err); | |
519 | } | |
520 | ||
521 | /* Dump information about entries, in response to GETNEIGH */ | |
522 | int br_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb) | |
523 | { | |
524 | struct net *net = sock_net(skb->sk); | |
525 | struct net_device *dev; | |
526 | int idx = 0; | |
527 | ||
528 | rcu_read_lock(); | |
529 | for_each_netdev_rcu(net, dev) { | |
530 | struct net_bridge *br = netdev_priv(dev); | |
531 | int i; | |
532 | ||
533 | if (!(dev->priv_flags & IFF_EBRIDGE)) | |
534 | continue; | |
535 | ||
536 | for (i = 0; i < BR_HASH_SIZE; i++) { | |
537 | struct hlist_node *h; | |
538 | struct net_bridge_fdb_entry *f; | |
539 | ||
540 | hlist_for_each_entry_rcu(f, h, &br->hash[i], hlist) { | |
541 | if (idx < cb->args[0]) | |
542 | goto skip; | |
543 | ||
31e8a49c | 544 | if (fdb_fill_info(skb, br, f, |
b078f0df | 545 | NETLINK_CB(cb->skb).pid, |
546 | cb->nlh->nlmsg_seq, | |
547 | RTM_NEWNEIGH, | |
548 | NLM_F_MULTI) < 0) | |
549 | break; | |
550 | skip: | |
551 | ++idx; | |
552 | } | |
553 | } | |
554 | } | |
555 | rcu_read_unlock(); | |
556 | ||
557 | cb->args[0] = idx; | |
558 | ||
559 | return skb->len; | |
560 | } | |
36fd2b63 | 561 | |
292d1398 | 562 | /* Update (create or replace) forwarding database entry */ |
36fd2b63 | 563 | static int fdb_add_entry(struct net_bridge_port *source, const __u8 *addr, |
64af1bac | 564 | __u16 state, __u16 flags) |
36fd2b63 | 565 | { |
566 | struct net_bridge *br = source->br; | |
567 | struct hlist_head *head = &br->hash[br_mac_hash(addr)]; | |
568 | struct net_bridge_fdb_entry *fdb; | |
569 | ||
570 | fdb = fdb_find(head, addr); | |
64af1bac | 571 | if (fdb == NULL) { |
572 | if (!(flags & NLM_F_CREATE)) | |
573 | return -ENOENT; | |
36fd2b63 | 574 | |
64af1bac | 575 | fdb = fdb_create(head, source, addr); |
576 | if (!fdb) | |
577 | return -ENOMEM; | |
31e8a49c | 578 | fdb_notify(br, fdb, RTM_NEWNEIGH); |
64af1bac | 579 | } else { |
580 | if (flags & NLM_F_EXCL) | |
581 | return -EEXIST; | |
292d1398 | 582 | } |
583 | ||
584 | if (fdb_to_nud(fdb) != state) { | |
585 | if (state & NUD_PERMANENT) | |
586 | fdb->is_local = fdb->is_static = 1; | |
587 | else if (state & NUD_NOARP) { | |
588 | fdb->is_local = 0; | |
589 | fdb->is_static = 1; | |
590 | } else | |
591 | fdb->is_local = fdb->is_static = 0; | |
64af1bac | 592 | |
292d1398 | 593 | fdb->updated = fdb->used = jiffies; |
31e8a49c | 594 | fdb_notify(br, fdb, RTM_NEWNEIGH); |
64af1bac | 595 | } |
36fd2b63 | 596 | |
36fd2b63 | 597 | return 0; |
598 | } | |
599 | ||
600 | /* Add new permanent fdb entry with RTM_NEWNEIGH */ | |
601 | int br_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg) | |
602 | { | |
603 | struct net *net = sock_net(skb->sk); | |
604 | struct ndmsg *ndm; | |
605 | struct nlattr *tb[NDA_MAX+1]; | |
606 | struct net_device *dev; | |
607 | struct net_bridge_port *p; | |
608 | const __u8 *addr; | |
609 | int err; | |
610 | ||
611 | ASSERT_RTNL(); | |
612 | err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL); | |
613 | if (err < 0) | |
614 | return err; | |
615 | ||
616 | ndm = nlmsg_data(nlh); | |
617 | if (ndm->ndm_ifindex == 0) { | |
618 | pr_info("bridge: RTM_NEWNEIGH with invalid ifindex\n"); | |
619 | return -EINVAL; | |
620 | } | |
621 | ||
622 | dev = __dev_get_by_index(net, ndm->ndm_ifindex); | |
623 | if (dev == NULL) { | |
624 | pr_info("bridge: RTM_NEWNEIGH with unknown ifindex\n"); | |
625 | return -ENODEV; | |
626 | } | |
627 | ||
628 | if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) { | |
629 | pr_info("bridge: RTM_NEWNEIGH with invalid address\n"); | |
630 | return -EINVAL; | |
631 | } | |
632 | ||
633 | addr = nla_data(tb[NDA_LLADDR]); | |
634 | if (!is_valid_ether_addr(addr)) { | |
635 | pr_info("bridge: RTM_NEWNEIGH with invalid ether address\n"); | |
636 | return -EINVAL; | |
637 | } | |
638 | ||
292d1398 | 639 | if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_NOARP|NUD_REACHABLE))) { |
640 | pr_info("bridge: RTM_NEWNEIGH with invalid state %#x\n", ndm->ndm_state); | |
641 | return -EINVAL; | |
642 | } | |
643 | ||
36fd2b63 | 644 | p = br_port_get_rtnl(dev); |
645 | if (p == NULL) { | |
646 | pr_info("bridge: RTM_NEWNEIGH %s not a bridge port\n", | |
647 | dev->name); | |
648 | return -EINVAL; | |
649 | } | |
650 | ||
292d1398 | 651 | if (ndm->ndm_flags & NTF_USE) { |
652 | rcu_read_lock(); | |
653 | br_fdb_update(p->br, p, addr); | |
654 | rcu_read_unlock(); | |
655 | } else { | |
656 | spin_lock_bh(&p->br->hash_lock); | |
657 | err = fdb_add_entry(p, addr, ndm->ndm_state, nlh->nlmsg_flags); | |
658 | spin_unlock_bh(&p->br->hash_lock); | |
659 | } | |
36fd2b63 | 660 | |
661 | return err; | |
662 | } | |
663 | ||
664 | static int fdb_delete_by_addr(struct net_bridge_port *p, const u8 *addr) | |
665 | { | |
666 | struct net_bridge *br = p->br; | |
667 | struct hlist_head *head = &br->hash[br_mac_hash(addr)]; | |
668 | struct net_bridge_fdb_entry *fdb; | |
669 | ||
670 | fdb = fdb_find(head, addr); | |
671 | if (!fdb) | |
672 | return -ENOENT; | |
673 | ||
31e8a49c | 674 | fdb_delete(p->br, fdb); |
36fd2b63 | 675 | return 0; |
676 | } | |
677 | ||
678 | /* Remove neighbor entry with RTM_DELNEIGH */ | |
679 | int br_fdb_delete(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg) | |
680 | { | |
681 | struct net *net = sock_net(skb->sk); | |
682 | struct ndmsg *ndm; | |
683 | struct net_bridge_port *p; | |
684 | struct nlattr *llattr; | |
685 | const __u8 *addr; | |
686 | struct net_device *dev; | |
687 | int err; | |
688 | ||
689 | ASSERT_RTNL(); | |
690 | if (nlmsg_len(nlh) < sizeof(*ndm)) | |
691 | return -EINVAL; | |
692 | ||
693 | ndm = nlmsg_data(nlh); | |
694 | if (ndm->ndm_ifindex == 0) { | |
695 | pr_info("bridge: RTM_DELNEIGH with invalid ifindex\n"); | |
696 | return -EINVAL; | |
697 | } | |
698 | ||
699 | dev = __dev_get_by_index(net, ndm->ndm_ifindex); | |
700 | if (dev == NULL) { | |
701 | pr_info("bridge: RTM_DELNEIGH with unknown ifindex\n"); | |
702 | return -ENODEV; | |
703 | } | |
704 | ||
705 | llattr = nlmsg_find_attr(nlh, sizeof(*ndm), NDA_LLADDR); | |
706 | if (llattr == NULL || nla_len(llattr) != ETH_ALEN) { | |
707 | pr_info("bridge: RTM_DELNEIGH with invalid address\n"); | |
708 | return -EINVAL; | |
709 | } | |
710 | ||
711 | addr = nla_data(llattr); | |
712 | ||
713 | p = br_port_get_rtnl(dev); | |
714 | if (p == NULL) { | |
715 | pr_info("bridge: RTM_DELNEIGH %s not a bridge port\n", | |
716 | dev->name); | |
717 | return -EINVAL; | |
718 | } | |
719 | ||
720 | spin_lock_bh(&p->br->hash_lock); | |
721 | err = fdb_delete_by_addr(p, addr); | |
722 | spin_unlock_bh(&p->br->hash_lock); | |
723 | ||
724 | return err; | |
725 | } |