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