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