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