]>
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> |
60063497 | 24 | #include <linux/atomic.h> |
3f890923 | 25 | #include <asm/unaligned.h> |
2ba071ec | 26 | #include <linux/if_vlan.h> |
1da177e4 LT |
27 | #include "br_private.h" |
28 | ||
e18b890b | 29 | static struct kmem_cache *br_fdb_cache __read_mostly; |
424bb9c9 TM |
30 | static struct net_bridge_fdb_entry *fdb_find(struct hlist_head *head, |
31 | const unsigned char *addr, | |
32 | __u16 vid); | |
1da177e4 | 33 | static int fdb_insert(struct net_bridge *br, struct net_bridge_port *source, |
bc9a25d2 | 34 | const unsigned char *addr, u16 vid); |
31e8a49c | 35 | static void fdb_notify(struct net_bridge *br, |
36 | const struct net_bridge_fdb_entry *, int); | |
1da177e4 | 37 | |
3f890923 SH |
38 | static u32 fdb_salt __read_mostly; |
39 | ||
87a596e0 | 40 | int __init br_fdb_init(void) |
1da177e4 LT |
41 | { |
42 | br_fdb_cache = kmem_cache_create("bridge_fdb_cache", | |
43 | sizeof(struct net_bridge_fdb_entry), | |
44 | 0, | |
20c2df83 | 45 | SLAB_HWCACHE_ALIGN, NULL); |
87a596e0 AM |
46 | if (!br_fdb_cache) |
47 | return -ENOMEM; | |
48 | ||
3f890923 | 49 | get_random_bytes(&fdb_salt, sizeof(fdb_salt)); |
87a596e0 | 50 | return 0; |
1da177e4 LT |
51 | } |
52 | ||
73afc906 | 53 | void br_fdb_fini(void) |
1da177e4 LT |
54 | { |
55 | kmem_cache_destroy(br_fdb_cache); | |
56 | } | |
57 | ||
58 | ||
59 | /* if topology_changing then use forward_delay (default 15 sec) | |
60 | * otherwise keep longer (default 5 minutes) | |
61 | */ | |
3f890923 | 62 | static inline unsigned long hold_time(const struct net_bridge *br) |
1da177e4 LT |
63 | { |
64 | return br->topology_change ? br->forward_delay : br->ageing_time; | |
65 | } | |
66 | ||
3f890923 | 67 | static inline int has_expired(const struct net_bridge *br, |
1da177e4 LT |
68 | const struct net_bridge_fdb_entry *fdb) |
69 | { | |
f64f9e71 | 70 | return !fdb->is_static && |
7cd8861a | 71 | time_before_eq(fdb->updated + hold_time(br), jiffies); |
1da177e4 LT |
72 | } |
73 | ||
2ba071ec | 74 | static inline int br_mac_hash(const unsigned char *mac, __u16 vid) |
1da177e4 | 75 | { |
2ba071ec | 76 | /* use 1 byte of OUI and 3 bytes of NIC */ |
3f890923 | 77 | u32 key = get_unaligned((u32 *)(mac + 2)); |
2ba071ec | 78 | return jhash_2words(key, vid, fdb_salt) & (BR_HASH_SIZE - 1); |
1da177e4 LT |
79 | } |
80 | ||
da678292 MM |
81 | static void fdb_rcu_free(struct rcu_head *head) |
82 | { | |
83 | struct net_bridge_fdb_entry *ent | |
84 | = container_of(head, struct net_bridge_fdb_entry, rcu); | |
85 | kmem_cache_free(br_fdb_cache, ent); | |
86 | } | |
87 | ||
31e8a49c | 88 | static void fdb_delete(struct net_bridge *br, struct net_bridge_fdb_entry *f) |
1da177e4 LT |
89 | { |
90 | hlist_del_rcu(&f->hlist); | |
31e8a49c | 91 | fdb_notify(br, f, RTM_DELNEIGH); |
da678292 | 92 | call_rcu(&f->rcu, fdb_rcu_free); |
1da177e4 LT |
93 | } |
94 | ||
960b589f TM |
95 | /* Delete a local entry if no other port had the same address. */ |
96 | static void fdb_delete_local(struct net_bridge *br, | |
97 | const struct net_bridge_port *p, | |
98 | struct net_bridge_fdb_entry *f) | |
99 | { | |
100 | const unsigned char *addr = f->addr.addr; | |
101 | u16 vid = f->vlan_id; | |
102 | struct net_bridge_port *op; | |
103 | ||
104 | /* Maybe another port has same hw addr? */ | |
105 | list_for_each_entry(op, &br->port_list, list) { | |
106 | if (op != p && ether_addr_equal(op->dev->dev_addr, addr) && | |
107 | (!vid || nbp_vlan_find(op, vid))) { | |
108 | f->dst = op; | |
a778e6d1 | 109 | f->added_by_user = 0; |
960b589f TM |
110 | return; |
111 | } | |
112 | } | |
113 | ||
114 | /* Maybe bridge device has same hw addr? */ | |
115 | if (p && ether_addr_equal(br->dev->dev_addr, addr) && | |
116 | (!vid || br_vlan_find(br, vid))) { | |
117 | f->dst = NULL; | |
a778e6d1 | 118 | f->added_by_user = 0; |
960b589f TM |
119 | return; |
120 | } | |
121 | ||
122 | fdb_delete(br, f); | |
123 | } | |
124 | ||
424bb9c9 TM |
125 | void br_fdb_find_delete_local(struct net_bridge *br, |
126 | const struct net_bridge_port *p, | |
127 | const unsigned char *addr, u16 vid) | |
128 | { | |
129 | struct hlist_head *head = &br->hash[br_mac_hash(addr, vid)]; | |
130 | struct net_bridge_fdb_entry *f; | |
131 | ||
132 | spin_lock_bh(&br->hash_lock); | |
133 | f = fdb_find(head, addr, vid); | |
134 | if (f && f->is_local && !f->added_by_user && f->dst == p) | |
135 | fdb_delete_local(br, p, f); | |
136 | spin_unlock_bh(&br->hash_lock); | |
137 | } | |
138 | ||
1da177e4 LT |
139 | void br_fdb_changeaddr(struct net_bridge_port *p, const unsigned char *newaddr) |
140 | { | |
141 | struct net_bridge *br = p->br; | |
2836882f TM |
142 | struct net_port_vlans *pv = nbp_get_vlan_info(p); |
143 | bool no_vlan = !pv; | |
1da177e4 | 144 | int i; |
2836882f | 145 | u16 vid; |
9d6f229f | 146 | |
1da177e4 LT |
147 | spin_lock_bh(&br->hash_lock); |
148 | ||
149 | /* Search all chains since old address/hash is unknown */ | |
150 | for (i = 0; i < BR_HASH_SIZE; i++) { | |
151 | struct hlist_node *h; | |
152 | hlist_for_each(h, &br->hash[i]) { | |
153 | struct net_bridge_fdb_entry *f; | |
154 | ||
155 | f = hlist_entry(h, struct net_bridge_fdb_entry, hlist); | |
a5642ab4 | 156 | if (f->dst == p && f->is_local && !f->added_by_user) { |
1da177e4 | 157 | /* delete old one */ |
960b589f TM |
158 | fdb_delete_local(br, p, f); |
159 | ||
bc9a25d2 VY |
160 | /* if this port has no vlan information |
161 | * configured, we can safely be done at | |
162 | * this point. | |
163 | */ | |
164 | if (no_vlan) | |
2836882f | 165 | goto insert; |
1da177e4 LT |
166 | } |
167 | } | |
168 | } | |
1da177e4 | 169 | |
2836882f TM |
170 | insert: |
171 | /* insert new address, may fail if invalid address or dup. */ | |
172 | fdb_insert(br, p, newaddr, 0); | |
173 | ||
174 | if (no_vlan) | |
175 | goto done; | |
176 | ||
177 | /* Now add entries for every VLAN configured on the port. | |
178 | * This function runs under RTNL so the bitmap will not change | |
179 | * from under us. | |
180 | */ | |
181 | for_each_set_bit(vid, pv->vlan_bitmap, VLAN_N_VID) | |
182 | fdb_insert(br, p, newaddr, vid); | |
183 | ||
bc9a25d2 | 184 | done: |
1da177e4 LT |
185 | spin_unlock_bh(&br->hash_lock); |
186 | } | |
187 | ||
43598813 | 188 | void br_fdb_change_mac_address(struct net_bridge *br, const u8 *newaddr) |
189 | { | |
190 | struct net_bridge_fdb_entry *f; | |
bc9a25d2 VY |
191 | struct net_port_vlans *pv; |
192 | u16 vid = 0; | |
43598813 | 193 | |
ac4c8868 TM |
194 | spin_lock_bh(&br->hash_lock); |
195 | ||
43598813 | 196 | /* If old entry was unassociated with any port, then delete it. */ |
2ba071ec | 197 | f = __br_fdb_get(br, br->dev->dev_addr, 0); |
43598813 | 198 | if (f && f->is_local && !f->dst) |
960b589f | 199 | fdb_delete_local(br, NULL, f); |
43598813 | 200 | |
bc9a25d2 VY |
201 | fdb_insert(br, NULL, newaddr, 0); |
202 | ||
203 | /* Now remove and add entries for every VLAN configured on the | |
204 | * bridge. This function runs under RTNL so the bitmap will not | |
205 | * change from under us. | |
206 | */ | |
207 | pv = br_get_vlan_info(br); | |
208 | if (!pv) | |
ac4c8868 | 209 | goto out; |
bc9a25d2 | 210 | |
ef40b7ef | 211 | for_each_set_bit_from(vid, pv->vlan_bitmap, VLAN_N_VID) { |
bc9a25d2 VY |
212 | f = __br_fdb_get(br, br->dev->dev_addr, vid); |
213 | if (f && f->is_local && !f->dst) | |
960b589f | 214 | fdb_delete_local(br, NULL, f); |
bc9a25d2 VY |
215 | fdb_insert(br, NULL, newaddr, vid); |
216 | } | |
ac4c8868 TM |
217 | out: |
218 | spin_unlock_bh(&br->hash_lock); | |
43598813 | 219 | } |
220 | ||
1da177e4 LT |
221 | void br_fdb_cleanup(unsigned long _data) |
222 | { | |
223 | struct net_bridge *br = (struct net_bridge *)_data; | |
224 | unsigned long delay = hold_time(br); | |
25442e06 | 225 | unsigned long next_timer = jiffies + br->ageing_time; |
1da177e4 LT |
226 | int i; |
227 | ||
27a42938 | 228 | spin_lock(&br->hash_lock); |
1da177e4 LT |
229 | for (i = 0; i < BR_HASH_SIZE; i++) { |
230 | struct net_bridge_fdb_entry *f; | |
b67bfe0d | 231 | struct hlist_node *n; |
1da177e4 | 232 | |
b67bfe0d | 233 | hlist_for_each_entry_safe(f, n, &br->hash[i], hlist) { |
071f7722 BE |
234 | unsigned long this_timer; |
235 | if (f->is_static) | |
236 | continue; | |
7cd8861a | 237 | this_timer = f->updated + delay; |
071f7722 | 238 | if (time_before_eq(this_timer, jiffies)) |
31e8a49c | 239 | fdb_delete(br, f); |
2bec008c | 240 | else if (time_before(this_timer, next_timer)) |
071f7722 | 241 | next_timer = this_timer; |
1da177e4 LT |
242 | } |
243 | } | |
27a42938 | 244 | spin_unlock(&br->hash_lock); |
1da177e4 | 245 | |
25442e06 | 246 | mod_timer(&br->gc_timer, round_jiffies_up(next_timer)); |
1da177e4 LT |
247 | } |
248 | ||
9cf63747 SH |
249 | /* Completely flush all dynamic entries in forwarding database.*/ |
250 | void br_fdb_flush(struct net_bridge *br) | |
251 | { | |
252 | int i; | |
253 | ||
254 | spin_lock_bh(&br->hash_lock); | |
255 | for (i = 0; i < BR_HASH_SIZE; i++) { | |
256 | struct net_bridge_fdb_entry *f; | |
b67bfe0d SL |
257 | struct hlist_node *n; |
258 | hlist_for_each_entry_safe(f, n, &br->hash[i], hlist) { | |
9cf63747 | 259 | if (!f->is_static) |
31e8a49c | 260 | fdb_delete(br, f); |
9cf63747 SH |
261 | } |
262 | } | |
263 | spin_unlock_bh(&br->hash_lock); | |
264 | } | |
1a620698 | 265 | |
25985edc | 266 | /* Flush all entries referring to a specific port. |
9cf63747 SH |
267 | * if do_all is set also flush static entries |
268 | */ | |
1a620698 SH |
269 | void br_fdb_delete_by_port(struct net_bridge *br, |
270 | const struct net_bridge_port *p, | |
271 | int do_all) | |
1da177e4 LT |
272 | { |
273 | int i; | |
274 | ||
275 | spin_lock_bh(&br->hash_lock); | |
276 | for (i = 0; i < BR_HASH_SIZE; i++) { | |
277 | struct hlist_node *h, *g; | |
9d6f229f | 278 | |
1da177e4 LT |
279 | hlist_for_each_safe(h, g, &br->hash[i]) { |
280 | struct net_bridge_fdb_entry *f | |
281 | = hlist_entry(h, struct net_bridge_fdb_entry, hlist); | |
9d6f229f | 282 | if (f->dst != p) |
1da177e4 LT |
283 | continue; |
284 | ||
1a620698 SH |
285 | if (f->is_static && !do_all) |
286 | continue; | |
1da177e4 | 287 | |
a778e6d1 TM |
288 | if (f->is_local) |
289 | fdb_delete_local(br, p, f); | |
290 | else | |
291 | fdb_delete(br, f); | |
1da177e4 LT |
292 | } |
293 | } | |
294 | spin_unlock_bh(&br->hash_lock); | |
295 | } | |
296 | ||
eeaf61d8 | 297 | /* No locking or refcounting, assumes caller has rcu_read_lock */ |
1da177e4 | 298 | struct net_bridge_fdb_entry *__br_fdb_get(struct net_bridge *br, |
2ba071ec VY |
299 | const unsigned char *addr, |
300 | __u16 vid) | |
1da177e4 | 301 | { |
1da177e4 LT |
302 | struct net_bridge_fdb_entry *fdb; |
303 | ||
b67bfe0d | 304 | hlist_for_each_entry_rcu(fdb, |
2ba071ec VY |
305 | &br->hash[br_mac_hash(addr, vid)], hlist) { |
306 | if (ether_addr_equal(fdb->addr.addr, addr) && | |
307 | fdb->vlan_id == vid) { | |
1da177e4 LT |
308 | if (unlikely(has_expired(br, fdb))) |
309 | break; | |
310 | return fdb; | |
311 | } | |
312 | } | |
313 | ||
314 | return NULL; | |
315 | } | |
316 | ||
e6373c4c | 317 | #if IS_ENABLED(CONFIG_ATM_LANE) |
da678292 MM |
318 | /* Interface used by ATM LANE hook to test |
319 | * if an addr is on some other bridge port */ | |
320 | int br_fdb_test_addr(struct net_device *dev, unsigned char *addr) | |
1da177e4 LT |
321 | { |
322 | struct net_bridge_fdb_entry *fdb; | |
b5ed54e9 | 323 | struct net_bridge_port *port; |
da678292 MM |
324 | int ret; |
325 | ||
1da177e4 | 326 | rcu_read_lock(); |
b5ed54e9 | 327 | port = br_port_get_rcu(dev); |
328 | if (!port) | |
329 | ret = 0; | |
330 | else { | |
2ba071ec | 331 | fdb = __br_fdb_get(port->br, addr, 0); |
43598813 | 332 | ret = fdb && fdb->dst && fdb->dst->dev != dev && |
b5ed54e9 | 333 | fdb->dst->state == BR_STATE_FORWARDING; |
334 | } | |
1da177e4 | 335 | rcu_read_unlock(); |
1da177e4 | 336 | |
da678292 | 337 | return ret; |
1da177e4 | 338 | } |
da678292 | 339 | #endif /* CONFIG_ATM_LANE */ |
1da177e4 LT |
340 | |
341 | /* | |
9d6f229f | 342 | * Fill buffer with forwarding table records in |
1da177e4 LT |
343 | * the API format. |
344 | */ | |
345 | int br_fdb_fillbuf(struct net_bridge *br, void *buf, | |
346 | unsigned long maxnum, unsigned long skip) | |
347 | { | |
348 | struct __fdb_entry *fe = buf; | |
349 | int i, num = 0; | |
1da177e4 LT |
350 | struct net_bridge_fdb_entry *f; |
351 | ||
352 | memset(buf, 0, maxnum*sizeof(struct __fdb_entry)); | |
353 | ||
354 | rcu_read_lock(); | |
355 | for (i = 0; i < BR_HASH_SIZE; i++) { | |
b67bfe0d | 356 | hlist_for_each_entry_rcu(f, &br->hash[i], hlist) { |
1da177e4 LT |
357 | if (num >= maxnum) |
358 | goto out; | |
359 | ||
9d6f229f | 360 | if (has_expired(br, f)) |
1da177e4 LT |
361 | continue; |
362 | ||
43598813 | 363 | /* ignore pseudo entry for local MAC address */ |
364 | if (!f->dst) | |
365 | continue; | |
366 | ||
1da177e4 LT |
367 | if (skip) { |
368 | --skip; | |
369 | continue; | |
370 | } | |
371 | ||
372 | /* convert from internal format to API */ | |
373 | memcpy(fe->mac_addr, f->addr.addr, ETH_ALEN); | |
ae4f8fca SH |
374 | |
375 | /* due to ABI compat need to split into hi/lo */ | |
1da177e4 | 376 | fe->port_no = f->dst->port_no; |
ae4f8fca SH |
377 | fe->port_hi = f->dst->port_no >> 8; |
378 | ||
1da177e4 LT |
379 | fe->is_local = f->is_local; |
380 | if (!f->is_static) | |
a399a805 | 381 | fe->ageing_timer_value = jiffies_delta_to_clock_t(jiffies - f->updated); |
1da177e4 LT |
382 | ++fe; |
383 | ++num; | |
384 | } | |
385 | } | |
386 | ||
387 | out: | |
388 | rcu_read_unlock(); | |
389 | ||
390 | return num; | |
391 | } | |
392 | ||
664de48b | 393 | static struct net_bridge_fdb_entry *fdb_find(struct hlist_head *head, |
2ba071ec VY |
394 | const unsigned char *addr, |
395 | __u16 vid) | |
664de48b | 396 | { |
664de48b | 397 | struct net_bridge_fdb_entry *fdb; |
398 | ||
b67bfe0d | 399 | hlist_for_each_entry(fdb, head, hlist) { |
2ba071ec VY |
400 | if (ether_addr_equal(fdb->addr.addr, addr) && |
401 | fdb->vlan_id == vid) | |
664de48b | 402 | return fdb; |
403 | } | |
404 | return NULL; | |
405 | } | |
406 | ||
407 | static struct net_bridge_fdb_entry *fdb_find_rcu(struct hlist_head *head, | |
2ba071ec VY |
408 | const unsigned char *addr, |
409 | __u16 vid) | |
1da177e4 | 410 | { |
1da177e4 LT |
411 | struct net_bridge_fdb_entry *fdb; |
412 | ||
b67bfe0d | 413 | hlist_for_each_entry_rcu(fdb, head, hlist) { |
2ba071ec VY |
414 | if (ether_addr_equal(fdb->addr.addr, addr) && |
415 | fdb->vlan_id == vid) | |
1da177e4 LT |
416 | return fdb; |
417 | } | |
418 | return NULL; | |
419 | } | |
420 | ||
421 | static struct net_bridge_fdb_entry *fdb_create(struct hlist_head *head, | |
422 | struct net_bridge_port *source, | |
2ba071ec VY |
423 | const unsigned char *addr, |
424 | __u16 vid) | |
1da177e4 LT |
425 | { |
426 | struct net_bridge_fdb_entry *fdb; | |
427 | ||
428 | fdb = kmem_cache_alloc(br_fdb_cache, GFP_ATOMIC); | |
429 | if (fdb) { | |
430 | memcpy(fdb->addr.addr, addr, ETH_ALEN); | |
1da177e4 | 431 | fdb->dst = source; |
2ba071ec | 432 | fdb->vlan_id = vid; |
03e9b64b | 433 | fdb->is_local = 0; |
434 | fdb->is_static = 0; | |
a5642ab4 | 435 | fdb->added_by_user = 0; |
7cd8861a | 436 | fdb->updated = fdb->used = jiffies; |
1158f762 | 437 | hlist_add_head_rcu(&fdb->hlist, head); |
1da177e4 LT |
438 | } |
439 | return fdb; | |
440 | } | |
441 | ||
442 | static int fdb_insert(struct net_bridge *br, struct net_bridge_port *source, | |
bc9a25d2 | 443 | const unsigned char *addr, u16 vid) |
1da177e4 | 444 | { |
bc9a25d2 | 445 | struct hlist_head *head = &br->hash[br_mac_hash(addr, vid)]; |
1da177e4 LT |
446 | struct net_bridge_fdb_entry *fdb; |
447 | ||
448 | if (!is_valid_ether_addr(addr)) | |
449 | return -EINVAL; | |
450 | ||
bc9a25d2 | 451 | fdb = fdb_find(head, addr, vid); |
1da177e4 | 452 | if (fdb) { |
9d6f229f | 453 | /* it is okay to have multiple ports with same |
1da177e4 LT |
454 | * address, just use the first one. |
455 | */ | |
9d6f229f | 456 | if (fdb->is_local) |
1da177e4 | 457 | return 0; |
28a16c97 | 458 | br_warn(br, "adding interface %s with same address " |
1da177e4 | 459 | "as a received packet\n", |
9b46922e | 460 | source ? source->dev->name : br->dev->name); |
31e8a49c | 461 | fdb_delete(br, fdb); |
9d6f229f | 462 | } |
1da177e4 | 463 | |
bc9a25d2 | 464 | fdb = fdb_create(head, source, addr, vid); |
03e9b64b | 465 | if (!fdb) |
1da177e4 LT |
466 | return -ENOMEM; |
467 | ||
03e9b64b | 468 | fdb->is_local = fdb->is_static = 1; |
31e8a49c | 469 | fdb_notify(br, fdb, RTM_NEWNEIGH); |
1da177e4 LT |
470 | return 0; |
471 | } | |
472 | ||
03e9b64b | 473 | /* Add entry for local address of interface */ |
1da177e4 | 474 | int br_fdb_insert(struct net_bridge *br, struct net_bridge_port *source, |
bc9a25d2 | 475 | const unsigned char *addr, u16 vid) |
1da177e4 LT |
476 | { |
477 | int ret; | |
478 | ||
479 | spin_lock_bh(&br->hash_lock); | |
bc9a25d2 | 480 | ret = fdb_insert(br, source, addr, vid); |
1da177e4 LT |
481 | spin_unlock_bh(&br->hash_lock); |
482 | return ret; | |
483 | } | |
484 | ||
485 | void br_fdb_update(struct net_bridge *br, struct net_bridge_port *source, | |
a5642ab4 | 486 | const unsigned char *addr, u16 vid, bool added_by_user) |
1da177e4 | 487 | { |
2ba071ec | 488 | struct hlist_head *head = &br->hash[br_mac_hash(addr, vid)]; |
1da177e4 LT |
489 | struct net_bridge_fdb_entry *fdb; |
490 | ||
491 | /* some users want to always flood. */ | |
492 | if (hold_time(br) == 0) | |
493 | return; | |
494 | ||
df1c0b84 SH |
495 | /* ignore packets unless we are using this port */ |
496 | if (!(source->state == BR_STATE_LEARNING || | |
497 | source->state == BR_STATE_FORWARDING)) | |
498 | return; | |
499 | ||
2ba071ec | 500 | fdb = fdb_find_rcu(head, addr, vid); |
1da177e4 LT |
501 | if (likely(fdb)) { |
502 | /* attempt to update an entry for a local interface */ | |
503 | if (unlikely(fdb->is_local)) { | |
9d6f229f | 504 | if (net_ratelimit()) |
28a16c97 | 505 | br_warn(br, "received packet on %s with " |
506 | "own address as source address\n", | |
507 | source->dev->name); | |
1da177e4 LT |
508 | } else { |
509 | /* fastpath: update of existing entry */ | |
510 | fdb->dst = source; | |
7cd8861a | 511 | fdb->updated = jiffies; |
a5642ab4 TM |
512 | if (unlikely(added_by_user)) |
513 | fdb->added_by_user = 1; | |
1da177e4 LT |
514 | } |
515 | } else { | |
f8ae737d | 516 | spin_lock(&br->hash_lock); |
2ba071ec VY |
517 | if (likely(!fdb_find(head, addr, vid))) { |
518 | fdb = fdb_create(head, source, addr, vid); | |
a5642ab4 TM |
519 | if (fdb) { |
520 | if (unlikely(added_by_user)) | |
521 | fdb->added_by_user = 1; | |
31e8a49c | 522 | fdb_notify(br, fdb, RTM_NEWNEIGH); |
a5642ab4 | 523 | } |
f58ee4e1 | 524 | } |
1da177e4 LT |
525 | /* else we lose race and someone else inserts |
526 | * it first, don't bother updating | |
527 | */ | |
f8ae737d | 528 | spin_unlock(&br->hash_lock); |
1da177e4 | 529 | } |
1da177e4 | 530 | } |
b078f0df | 531 | |
532 | static int fdb_to_nud(const struct net_bridge_fdb_entry *fdb) | |
533 | { | |
534 | if (fdb->is_local) | |
535 | return NUD_PERMANENT; | |
536 | else if (fdb->is_static) | |
537 | return NUD_NOARP; | |
538 | else if (has_expired(fdb->dst->br, fdb)) | |
539 | return NUD_STALE; | |
540 | else | |
541 | return NUD_REACHABLE; | |
542 | } | |
543 | ||
31e8a49c | 544 | static int fdb_fill_info(struct sk_buff *skb, const struct net_bridge *br, |
b078f0df | 545 | const struct net_bridge_fdb_entry *fdb, |
15e47304 | 546 | u32 portid, u32 seq, int type, unsigned int flags) |
b078f0df | 547 | { |
548 | unsigned long now = jiffies; | |
549 | struct nda_cacheinfo ci; | |
550 | struct nlmsghdr *nlh; | |
551 | struct ndmsg *ndm; | |
552 | ||
15e47304 | 553 | nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags); |
b078f0df | 554 | if (nlh == NULL) |
555 | return -EMSGSIZE; | |
556 | ||
b078f0df | 557 | ndm = nlmsg_data(nlh); |
558 | ndm->ndm_family = AF_BRIDGE; | |
559 | ndm->ndm_pad1 = 0; | |
560 | ndm->ndm_pad2 = 0; | |
561 | ndm->ndm_flags = 0; | |
562 | ndm->ndm_type = 0; | |
43598813 | 563 | ndm->ndm_ifindex = fdb->dst ? fdb->dst->dev->ifindex : br->dev->ifindex; |
b078f0df | 564 | ndm->ndm_state = fdb_to_nud(fdb); |
565 | ||
2eb812e6 DM |
566 | if (nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->addr)) |
567 | goto nla_put_failure; | |
b078f0df | 568 | ci.ndm_used = jiffies_to_clock_t(now - fdb->used); |
569 | ci.ndm_confirmed = 0; | |
570 | ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated); | |
571 | ci.ndm_refcnt = 0; | |
2eb812e6 DM |
572 | if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci)) |
573 | goto nla_put_failure; | |
1690be63 VY |
574 | |
575 | if (nla_put(skb, NDA_VLAN, sizeof(u16), &fdb->vlan_id)) | |
576 | goto nla_put_failure; | |
577 | ||
b078f0df | 578 | return nlmsg_end(skb, nlh); |
579 | ||
580 | nla_put_failure: | |
581 | nlmsg_cancel(skb, nlh); | |
582 | return -EMSGSIZE; | |
583 | } | |
584 | ||
585 | static inline size_t fdb_nlmsg_size(void) | |
586 | { | |
587 | return NLMSG_ALIGN(sizeof(struct ndmsg)) | |
588 | + nla_total_size(ETH_ALEN) /* NDA_LLADDR */ | |
1690be63 | 589 | + nla_total_size(sizeof(u16)) /* NDA_VLAN */ |
b078f0df | 590 | + nla_total_size(sizeof(struct nda_cacheinfo)); |
591 | } | |
592 | ||
31e8a49c | 593 | static void fdb_notify(struct net_bridge *br, |
594 | const struct net_bridge_fdb_entry *fdb, int type) | |
b078f0df | 595 | { |
31e8a49c | 596 | struct net *net = dev_net(br->dev); |
b078f0df | 597 | struct sk_buff *skb; |
598 | int err = -ENOBUFS; | |
599 | ||
600 | skb = nlmsg_new(fdb_nlmsg_size(), GFP_ATOMIC); | |
601 | if (skb == NULL) | |
602 | goto errout; | |
603 | ||
31e8a49c | 604 | err = fdb_fill_info(skb, br, fdb, 0, 0, type, 0); |
b078f0df | 605 | if (err < 0) { |
606 | /* -EMSGSIZE implies BUG in fdb_nlmsg_size() */ | |
607 | WARN_ON(err == -EMSGSIZE); | |
608 | kfree_skb(skb); | |
609 | goto errout; | |
610 | } | |
611 | rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC); | |
612 | return; | |
613 | errout: | |
87e823b3 | 614 | rtnl_set_sk_err(net, RTNLGRP_NEIGH, err); |
b078f0df | 615 | } |
616 | ||
617 | /* Dump information about entries, in response to GETNEIGH */ | |
77162022 JF |
618 | int br_fdb_dump(struct sk_buff *skb, |
619 | struct netlink_callback *cb, | |
620 | struct net_device *dev, | |
621 | int idx) | |
b078f0df | 622 | { |
77162022 JF |
623 | struct net_bridge *br = netdev_priv(dev); |
624 | int i; | |
b078f0df | 625 | |
77162022 JF |
626 | if (!(dev->priv_flags & IFF_EBRIDGE)) |
627 | goto out; | |
b078f0df | 628 | |
77162022 | 629 | for (i = 0; i < BR_HASH_SIZE; i++) { |
77162022 | 630 | struct net_bridge_fdb_entry *f; |
b078f0df | 631 | |
b67bfe0d | 632 | hlist_for_each_entry_rcu(f, &br->hash[i], hlist) { |
77162022 JF |
633 | if (idx < cb->args[0]) |
634 | goto skip; | |
635 | ||
636 | if (fdb_fill_info(skb, br, f, | |
15e47304 | 637 | NETLINK_CB(cb->skb).portid, |
77162022 JF |
638 | cb->nlh->nlmsg_seq, |
639 | RTM_NEWNEIGH, | |
640 | NLM_F_MULTI) < 0) | |
641 | break; | |
b078f0df | 642 | skip: |
77162022 | 643 | ++idx; |
b078f0df | 644 | } |
645 | } | |
b078f0df | 646 | |
77162022 JF |
647 | out: |
648 | return idx; | |
b078f0df | 649 | } |
36fd2b63 | 650 | |
292d1398 | 651 | /* Update (create or replace) forwarding database entry */ |
36fd2b63 | 652 | static int fdb_add_entry(struct net_bridge_port *source, const __u8 *addr, |
2ba071ec | 653 | __u16 state, __u16 flags, __u16 vid) |
36fd2b63 | 654 | { |
655 | struct net_bridge *br = source->br; | |
2ba071ec | 656 | struct hlist_head *head = &br->hash[br_mac_hash(addr, vid)]; |
36fd2b63 | 657 | struct net_bridge_fdb_entry *fdb; |
b0a397fb | 658 | bool modified = false; |
36fd2b63 | 659 | |
2ba071ec | 660 | fdb = fdb_find(head, addr, vid); |
64af1bac | 661 | if (fdb == NULL) { |
662 | if (!(flags & NLM_F_CREATE)) | |
663 | return -ENOENT; | |
36fd2b63 | 664 | |
2ba071ec | 665 | fdb = fdb_create(head, source, addr, vid); |
64af1bac | 666 | if (!fdb) |
667 | return -ENOMEM; | |
b0a397fb | 668 | |
669 | modified = true; | |
64af1bac | 670 | } else { |
671 | if (flags & NLM_F_EXCL) | |
672 | return -EEXIST; | |
b0a397fb | 673 | |
674 | if (fdb->dst != source) { | |
675 | fdb->dst = source; | |
676 | modified = true; | |
677 | } | |
292d1398 | 678 | } |
679 | ||
680 | if (fdb_to_nud(fdb) != state) { | |
681 | if (state & NUD_PERMANENT) | |
682 | fdb->is_local = fdb->is_static = 1; | |
683 | else if (state & NUD_NOARP) { | |
684 | fdb->is_local = 0; | |
685 | fdb->is_static = 1; | |
686 | } else | |
687 | fdb->is_local = fdb->is_static = 0; | |
64af1bac | 688 | |
b0a397fb | 689 | modified = true; |
690 | } | |
a5642ab4 | 691 | fdb->added_by_user = 1; |
b0a397fb | 692 | |
693 | fdb->used = jiffies; | |
694 | if (modified) { | |
695 | fdb->updated = jiffies; | |
31e8a49c | 696 | fdb_notify(br, fdb, RTM_NEWNEIGH); |
64af1bac | 697 | } |
36fd2b63 | 698 | |
36fd2b63 | 699 | return 0; |
700 | } | |
701 | ||
1690be63 VY |
702 | static int __br_fdb_add(struct ndmsg *ndm, struct net_bridge_port *p, |
703 | const unsigned char *addr, u16 nlh_flags, u16 vid) | |
704 | { | |
705 | int err = 0; | |
706 | ||
707 | if (ndm->ndm_flags & NTF_USE) { | |
708 | rcu_read_lock(); | |
a5642ab4 | 709 | br_fdb_update(p->br, p, addr, vid, true); |
1690be63 VY |
710 | rcu_read_unlock(); |
711 | } else { | |
712 | spin_lock_bh(&p->br->hash_lock); | |
713 | err = fdb_add_entry(p, addr, ndm->ndm_state, | |
714 | nlh_flags, vid); | |
715 | spin_unlock_bh(&p->br->hash_lock); | |
716 | } | |
717 | ||
718 | return err; | |
719 | } | |
720 | ||
36fd2b63 | 721 | /* Add new permanent fdb entry with RTM_NEWNEIGH */ |
edc7d573 | 722 | int br_fdb_add(struct ndmsg *ndm, struct nlattr *tb[], |
723 | struct net_device *dev, | |
6b6e2725 | 724 | const unsigned char *addr, u16 nlh_flags) |
36fd2b63 | 725 | { |
36fd2b63 | 726 | struct net_bridge_port *p; |
77162022 | 727 | int err = 0; |
1690be63 VY |
728 | struct net_port_vlans *pv; |
729 | unsigned short vid = VLAN_N_VID; | |
36fd2b63 | 730 | |
292d1398 | 731 | if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_NOARP|NUD_REACHABLE))) { |
732 | pr_info("bridge: RTM_NEWNEIGH with invalid state %#x\n", ndm->ndm_state); | |
733 | return -EINVAL; | |
734 | } | |
735 | ||
1690be63 VY |
736 | if (tb[NDA_VLAN]) { |
737 | if (nla_len(tb[NDA_VLAN]) != sizeof(unsigned short)) { | |
738 | pr_info("bridge: RTM_NEWNEIGH with invalid vlan\n"); | |
739 | return -EINVAL; | |
740 | } | |
741 | ||
742 | vid = nla_get_u16(tb[NDA_VLAN]); | |
743 | ||
8adff41c | 744 | if (!vid || vid >= VLAN_VID_MASK) { |
1690be63 VY |
745 | pr_info("bridge: RTM_NEWNEIGH with invalid vlan id %d\n", |
746 | vid); | |
747 | return -EINVAL; | |
748 | } | |
749 | } | |
750 | ||
537f7f84 SH |
751 | if (is_zero_ether_addr(addr)) { |
752 | pr_info("bridge: RTM_NEWNEIGH with invalid ether address\n"); | |
753 | return -EINVAL; | |
754 | } | |
755 | ||
36fd2b63 | 756 | p = br_port_get_rtnl(dev); |
757 | if (p == NULL) { | |
758 | pr_info("bridge: RTM_NEWNEIGH %s not a bridge port\n", | |
759 | dev->name); | |
760 | return -EINVAL; | |
761 | } | |
762 | ||
1690be63 VY |
763 | pv = nbp_get_vlan_info(p); |
764 | if (vid != VLAN_N_VID) { | |
765 | if (!pv || !test_bit(vid, pv->vlan_bitmap)) { | |
766 | pr_info("bridge: RTM_NEWNEIGH with unconfigured " | |
767 | "vlan %d on port %s\n", vid, dev->name); | |
768 | return -EINVAL; | |
769 | } | |
770 | ||
771 | /* VID was specified, so use it. */ | |
772 | err = __br_fdb_add(ndm, p, addr, nlh_flags, vid); | |
292d1398 | 773 | } else { |
ef40b7ef | 774 | if (!pv || bitmap_empty(pv->vlan_bitmap, VLAN_N_VID)) { |
1690be63 VY |
775 | err = __br_fdb_add(ndm, p, addr, nlh_flags, 0); |
776 | goto out; | |
777 | } | |
778 | ||
779 | /* We have vlans configured on this port and user didn't | |
780 | * specify a VLAN. To be nice, add/update entry for every | |
781 | * vlan on this port. | |
782 | */ | |
ef40b7ef | 783 | for_each_set_bit(vid, pv->vlan_bitmap, VLAN_N_VID) { |
1690be63 VY |
784 | err = __br_fdb_add(ndm, p, addr, nlh_flags, vid); |
785 | if (err) | |
786 | goto out; | |
1690be63 | 787 | } |
292d1398 | 788 | } |
36fd2b63 | 789 | |
1690be63 | 790 | out: |
36fd2b63 | 791 | return err; |
792 | } | |
793 | ||
424bb9c9 | 794 | static int fdb_delete_by_addr(struct net_bridge *br, const u8 *addr, u16 vlan) |
36fd2b63 | 795 | { |
1690be63 | 796 | struct hlist_head *head = &br->hash[br_mac_hash(addr, vlan)]; |
36fd2b63 | 797 | struct net_bridge_fdb_entry *fdb; |
798 | ||
1690be63 | 799 | fdb = fdb_find(head, addr, vlan); |
36fd2b63 | 800 | if (!fdb) |
801 | return -ENOENT; | |
802 | ||
1690be63 | 803 | fdb_delete(br, fdb); |
36fd2b63 | 804 | return 0; |
805 | } | |
806 | ||
1690be63 VY |
807 | static int __br_fdb_delete(struct net_bridge_port *p, |
808 | const unsigned char *addr, u16 vid) | |
809 | { | |
810 | int err; | |
811 | ||
812 | spin_lock_bh(&p->br->hash_lock); | |
813 | err = fdb_delete_by_addr(p->br, addr, vid); | |
814 | spin_unlock_bh(&p->br->hash_lock); | |
815 | ||
816 | return err; | |
817 | } | |
818 | ||
36fd2b63 | 819 | /* Remove neighbor entry with RTM_DELNEIGH */ |
1690be63 VY |
820 | int br_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[], |
821 | struct net_device *dev, | |
6b6e2725 | 822 | const unsigned char *addr) |
36fd2b63 | 823 | { |
36fd2b63 | 824 | struct net_bridge_port *p; |
36fd2b63 | 825 | int err; |
1690be63 VY |
826 | struct net_port_vlans *pv; |
827 | unsigned short vid = VLAN_N_VID; | |
36fd2b63 | 828 | |
1690be63 VY |
829 | if (tb[NDA_VLAN]) { |
830 | if (nla_len(tb[NDA_VLAN]) != sizeof(unsigned short)) { | |
831 | pr_info("bridge: RTM_NEWNEIGH with invalid vlan\n"); | |
832 | return -EINVAL; | |
833 | } | |
834 | ||
835 | vid = nla_get_u16(tb[NDA_VLAN]); | |
836 | ||
8adff41c | 837 | if (!vid || vid >= VLAN_VID_MASK) { |
1690be63 VY |
838 | pr_info("bridge: RTM_NEWNEIGH with invalid vlan id %d\n", |
839 | vid); | |
840 | return -EINVAL; | |
841 | } | |
842 | } | |
36fd2b63 | 843 | p = br_port_get_rtnl(dev); |
844 | if (p == NULL) { | |
845 | pr_info("bridge: RTM_DELNEIGH %s not a bridge port\n", | |
846 | dev->name); | |
847 | return -EINVAL; | |
848 | } | |
849 | ||
1690be63 VY |
850 | pv = nbp_get_vlan_info(p); |
851 | if (vid != VLAN_N_VID) { | |
852 | if (!pv || !test_bit(vid, pv->vlan_bitmap)) { | |
853 | pr_info("bridge: RTM_DELNEIGH with unconfigured " | |
854 | "vlan %d on port %s\n", vid, dev->name); | |
855 | return -EINVAL; | |
856 | } | |
36fd2b63 | 857 | |
1690be63 VY |
858 | err = __br_fdb_delete(p, addr, vid); |
859 | } else { | |
ef40b7ef | 860 | if (!pv || bitmap_empty(pv->vlan_bitmap, VLAN_N_VID)) { |
1690be63 VY |
861 | err = __br_fdb_delete(p, addr, 0); |
862 | goto out; | |
863 | } | |
864 | ||
865 | /* We have vlans configured on this port and user didn't | |
866 | * specify a VLAN. To be nice, add/update entry for every | |
867 | * vlan on this port. | |
868 | */ | |
869 | err = -ENOENT; | |
ef40b7ef | 870 | for_each_set_bit(vid, pv->vlan_bitmap, VLAN_N_VID) { |
1690be63 | 871 | err &= __br_fdb_delete(p, addr, vid); |
1690be63 VY |
872 | } |
873 | } | |
874 | out: | |
36fd2b63 | 875 | return err; |
876 | } |