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> |
Scott Feldman | b4ad7ba | 2015-06-14 11:33:11 -0700 | [diff] [blame] | 27 | #include <net/switchdev.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include "br_private.h" |
| 29 | |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 30 | static struct kmem_cache *br_fdb_cache __read_mostly; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | 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] | 32 | const unsigned char *addr, u16 vid); |
stephen hemminger | 31e8a49c | 2011-12-08 07:17:41 +0000 | [diff] [blame] | 33 | static void fdb_notify(struct net_bridge *br, |
| 34 | const struct net_bridge_fdb_entry *, int); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | |
Stephen Hemminger | 3f89092 | 2007-03-21 13:42:33 -0700 | [diff] [blame] | 36 | static u32 fdb_salt __read_mostly; |
| 37 | |
Akinobu Mita | 87a596e | 2007-04-07 18:57:07 +0900 | [diff] [blame] | 38 | int __init br_fdb_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | { |
| 40 | br_fdb_cache = kmem_cache_create("bridge_fdb_cache", |
| 41 | sizeof(struct net_bridge_fdb_entry), |
| 42 | 0, |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 43 | SLAB_HWCACHE_ALIGN, NULL); |
Akinobu Mita | 87a596e | 2007-04-07 18:57:07 +0900 | [diff] [blame] | 44 | if (!br_fdb_cache) |
| 45 | return -ENOMEM; |
| 46 | |
Stephen Hemminger | 3f89092 | 2007-03-21 13:42:33 -0700 | [diff] [blame] | 47 | get_random_bytes(&fdb_salt, sizeof(fdb_salt)); |
Akinobu Mita | 87a596e | 2007-04-07 18:57:07 +0900 | [diff] [blame] | 48 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | } |
| 50 | |
Andrew Morton | 73afc90 | 2007-12-05 21:35:23 -0800 | [diff] [blame] | 51 | void br_fdb_fini(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | { |
| 53 | kmem_cache_destroy(br_fdb_cache); |
| 54 | } |
| 55 | |
| 56 | |
| 57 | /* if topology_changing then use forward_delay (default 15 sec) |
| 58 | * otherwise keep longer (default 5 minutes) |
| 59 | */ |
Stephen Hemminger | 3f89092 | 2007-03-21 13:42:33 -0700 | [diff] [blame] | 60 | static inline unsigned long hold_time(const struct net_bridge *br) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | { |
| 62 | return br->topology_change ? br->forward_delay : br->ageing_time; |
| 63 | } |
| 64 | |
Stephen Hemminger | 3f89092 | 2007-03-21 13:42:33 -0700 | [diff] [blame] | 65 | static inline int has_expired(const struct net_bridge *br, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | const struct net_bridge_fdb_entry *fdb) |
| 67 | { |
Roopa Prabhu | eda7a5e | 2017-02-16 13:38:04 -0800 | [diff] [blame] | 68 | return !fdb->is_static && !fdb->added_by_external_learn && |
stephen hemminger | 7cd8861 | 2011-04-04 14:03:28 +0000 | [diff] [blame] | 69 | time_before_eq(fdb->updated + hold_time(br), jiffies); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | } |
| 71 | |
Vlad Yasevich | 2ba071e | 2013-02-13 12:00:16 +0000 | [diff] [blame] | 72 | static inline int br_mac_hash(const unsigned char *mac, __u16 vid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | { |
Vlad Yasevich | 2ba071e | 2013-02-13 12:00:16 +0000 | [diff] [blame] | 74 | /* use 1 byte of OUI and 3 bytes of NIC */ |
Stephen Hemminger | 3f89092 | 2007-03-21 13:42:33 -0700 | [diff] [blame] | 75 | u32 key = get_unaligned((u32 *)(mac + 2)); |
Vlad Yasevich | 2ba071e | 2013-02-13 12:00:16 +0000 | [diff] [blame] | 76 | return jhash_2words(key, vid, fdb_salt) & (BR_HASH_SIZE - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | } |
| 78 | |
Michał Mirosław | da67829 | 2009-06-05 05:35:28 +0000 | [diff] [blame] | 79 | static void fdb_rcu_free(struct rcu_head *head) |
| 80 | { |
| 81 | struct net_bridge_fdb_entry *ent |
| 82 | = container_of(head, struct net_bridge_fdb_entry, rcu); |
| 83 | kmem_cache_free(br_fdb_cache, ent); |
| 84 | } |
| 85 | |
Nikolay Aleksandrov | bfd0aea | 2017-02-13 14:59:09 +0100 | [diff] [blame] | 86 | static struct net_bridge_fdb_entry *fdb_find_rcu(struct hlist_head *head, |
| 87 | const unsigned char *addr, |
| 88 | __u16 vid) |
| 89 | { |
| 90 | struct net_bridge_fdb_entry *f; |
| 91 | |
Nikolay Aleksandrov | 410b3d4 | 2017-02-13 14:59:10 +0100 | [diff] [blame] | 92 | WARN_ON_ONCE(!rcu_read_lock_held()); |
| 93 | |
Nikolay Aleksandrov | bfd0aea | 2017-02-13 14:59:09 +0100 | [diff] [blame] | 94 | hlist_for_each_entry_rcu(f, head, hlist) |
| 95 | if (ether_addr_equal(f->addr.addr, addr) && f->vlan_id == vid) |
| 96 | break; |
| 97 | |
| 98 | return f; |
| 99 | } |
| 100 | |
| 101 | /* requires bridge hash_lock */ |
| 102 | static struct net_bridge_fdb_entry *br_fdb_find(struct net_bridge *br, |
| 103 | const unsigned char *addr, |
| 104 | __u16 vid) |
| 105 | { |
| 106 | struct hlist_head *head = &br->hash[br_mac_hash(addr, vid)]; |
| 107 | struct net_bridge_fdb_entry *fdb; |
| 108 | |
WANG Cong | d12c917 | 2017-03-16 10:32:42 -0700 | [diff] [blame] | 109 | lockdep_assert_held_once(&br->hash_lock); |
Nikolay Aleksandrov | 410b3d4 | 2017-02-13 14:59:10 +0100 | [diff] [blame] | 110 | |
Nikolay Aleksandrov | bfd0aea | 2017-02-13 14:59:09 +0100 | [diff] [blame] | 111 | rcu_read_lock(); |
| 112 | fdb = fdb_find_rcu(head, addr, vid); |
| 113 | rcu_read_unlock(); |
| 114 | |
| 115 | return fdb; |
| 116 | } |
| 117 | |
| 118 | struct net_bridge_fdb_entry *br_fdb_find_rcu(struct net_bridge *br, |
| 119 | const unsigned char *addr, |
| 120 | __u16 vid) |
| 121 | { |
| 122 | struct hlist_head *head = &br->hash[br_mac_hash(addr, vid)]; |
| 123 | |
| 124 | return fdb_find_rcu(head, addr, vid); |
| 125 | } |
| 126 | |
Vlad Yasevich | 145beee | 2014-05-16 09:59:19 -0400 | [diff] [blame] | 127 | /* When a static FDB entry is added, the mac address from the entry is |
| 128 | * added to the bridge private HW address list and all required ports |
| 129 | * are then updated with the new information. |
| 130 | * Called under RTNL. |
| 131 | */ |
Jiri Pirko | 020ec6b | 2014-11-28 14:34:12 +0100 | [diff] [blame] | 132 | static void fdb_add_hw_addr(struct net_bridge *br, const unsigned char *addr) |
Vlad Yasevich | 145beee | 2014-05-16 09:59:19 -0400 | [diff] [blame] | 133 | { |
| 134 | int err; |
Li RongQing | a3f5ee7 | 2014-06-18 16:07:16 +0800 | [diff] [blame] | 135 | struct net_bridge_port *p; |
Vlad Yasevich | 145beee | 2014-05-16 09:59:19 -0400 | [diff] [blame] | 136 | |
| 137 | ASSERT_RTNL(); |
| 138 | |
| 139 | list_for_each_entry(p, &br->port_list, list) { |
| 140 | if (!br_promisc_port(p)) { |
| 141 | err = dev_uc_add(p->dev, addr); |
| 142 | if (err) |
| 143 | goto undo; |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | return; |
| 148 | undo: |
Li RongQing | a3f5ee7 | 2014-06-18 16:07:16 +0800 | [diff] [blame] | 149 | list_for_each_entry_continue_reverse(p, &br->port_list, list) { |
| 150 | if (!br_promisc_port(p)) |
| 151 | dev_uc_del(p->dev, addr); |
Vlad Yasevich | 145beee | 2014-05-16 09:59:19 -0400 | [diff] [blame] | 152 | } |
| 153 | } |
| 154 | |
| 155 | /* When a static FDB entry is deleted, the HW address from that entry is |
| 156 | * also removed from the bridge private HW address list and updates all |
| 157 | * the ports with needed information. |
| 158 | * Called under RTNL. |
| 159 | */ |
Jiri Pirko | 020ec6b | 2014-11-28 14:34:12 +0100 | [diff] [blame] | 160 | static void fdb_del_hw_addr(struct net_bridge *br, const unsigned char *addr) |
Vlad Yasevich | 145beee | 2014-05-16 09:59:19 -0400 | [diff] [blame] | 161 | { |
| 162 | struct net_bridge_port *p; |
| 163 | |
| 164 | ASSERT_RTNL(); |
| 165 | |
| 166 | list_for_each_entry(p, &br->port_list, list) { |
| 167 | if (!br_promisc_port(p)) |
| 168 | dev_uc_del(p->dev, addr); |
| 169 | } |
| 170 | } |
| 171 | |
Scott Feldman | b4ad7ba | 2015-06-14 11:33:11 -0700 | [diff] [blame] | 172 | static void fdb_del_external_learn(struct net_bridge_fdb_entry *f) |
| 173 | { |
Jiri Pirko | 52ba57c | 2015-10-01 11:03:44 +0200 | [diff] [blame] | 174 | struct switchdev_obj_port_fdb fdb = { |
Jiri Pirko | 5660738 | 2015-10-14 19:40:53 +0200 | [diff] [blame] | 175 | .obj = { |
Ido Schimmel | 6ff64f6 | 2015-12-15 16:03:35 +0100 | [diff] [blame] | 176 | .orig_dev = f->dst->dev, |
Jiri Pirko | 5660738 | 2015-10-14 19:40:53 +0200 | [diff] [blame] | 177 | .id = SWITCHDEV_OBJ_ID_PORT_FDB, |
| 178 | .flags = SWITCHDEV_F_DEFER, |
| 179 | }, |
Vivien Didelot | ab06900 | 2015-09-29 12:07:17 -0400 | [diff] [blame] | 180 | .vid = f->vlan_id, |
Scott Feldman | b4ad7ba | 2015-06-14 11:33:11 -0700 | [diff] [blame] | 181 | }; |
| 182 | |
Jiri Pirko | 850d0cb | 2015-10-14 19:40:51 +0200 | [diff] [blame] | 183 | ether_addr_copy(fdb.addr, f->addr.addr); |
Jiri Pirko | 9e8f4a5 | 2015-10-01 11:03:46 +0200 | [diff] [blame] | 184 | switchdev_port_obj_del(f->dst->dev, &fdb.obj); |
Scott Feldman | b4ad7ba | 2015-06-14 11:33:11 -0700 | [diff] [blame] | 185 | } |
| 186 | |
stephen hemminger | 31e8a49c | 2011-12-08 07:17:41 +0000 | [diff] [blame] | 187 | 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] | 188 | { |
Vlad Yasevich | 145beee | 2014-05-16 09:59:19 -0400 | [diff] [blame] | 189 | if (f->is_static) |
Jiri Pirko | 020ec6b | 2014-11-28 14:34:12 +0100 | [diff] [blame] | 190 | fdb_del_hw_addr(br, f->addr.addr); |
Vlad Yasevich | 145beee | 2014-05-16 09:59:19 -0400 | [diff] [blame] | 191 | |
Scott Feldman | b4ad7ba | 2015-06-14 11:33:11 -0700 | [diff] [blame] | 192 | if (f->added_by_external_learn) |
| 193 | fdb_del_external_learn(f); |
| 194 | |
Nikolay Aleksandrov | f7cdee8 | 2017-02-04 18:05:07 +0100 | [diff] [blame] | 195 | hlist_del_init_rcu(&f->hlist); |
stephen hemminger | 31e8a49c | 2011-12-08 07:17:41 +0000 | [diff] [blame] | 196 | fdb_notify(br, f, RTM_DELNEIGH); |
Michał Mirosław | da67829 | 2009-06-05 05:35:28 +0000 | [diff] [blame] | 197 | call_rcu(&f->rcu, fdb_rcu_free); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | } |
| 199 | |
Toshiaki Makita | 960b589 | 2014-02-07 16:48:23 +0900 | [diff] [blame] | 200 | /* Delete a local entry if no other port had the same address. */ |
| 201 | static void fdb_delete_local(struct net_bridge *br, |
| 202 | const struct net_bridge_port *p, |
| 203 | struct net_bridge_fdb_entry *f) |
| 204 | { |
| 205 | const unsigned char *addr = f->addr.addr; |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 206 | struct net_bridge_vlan_group *vg; |
| 207 | const struct net_bridge_vlan *v; |
Toshiaki Makita | 960b589 | 2014-02-07 16:48:23 +0900 | [diff] [blame] | 208 | struct net_bridge_port *op; |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 209 | u16 vid = f->vlan_id; |
Toshiaki Makita | 960b589 | 2014-02-07 16:48:23 +0900 | [diff] [blame] | 210 | |
| 211 | /* Maybe another port has same hw addr? */ |
| 212 | list_for_each_entry(op, &br->port_list, list) { |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 213 | vg = nbp_vlan_group(op); |
Toshiaki Makita | 960b589 | 2014-02-07 16:48:23 +0900 | [diff] [blame] | 214 | if (op != p && ether_addr_equal(op->dev->dev_addr, addr) && |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 215 | (!vid || br_vlan_find(vg, vid))) { |
Toshiaki Makita | 960b589 | 2014-02-07 16:48:23 +0900 | [diff] [blame] | 216 | f->dst = op; |
Toshiaki Makita | a778e6d | 2014-02-07 16:48:24 +0900 | [diff] [blame] | 217 | f->added_by_user = 0; |
Toshiaki Makita | 960b589 | 2014-02-07 16:48:23 +0900 | [diff] [blame] | 218 | return; |
| 219 | } |
| 220 | } |
| 221 | |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 222 | vg = br_vlan_group(br); |
| 223 | v = br_vlan_find(vg, vid); |
Toshiaki Makita | 960b589 | 2014-02-07 16:48:23 +0900 | [diff] [blame] | 224 | /* Maybe bridge device has same hw addr? */ |
| 225 | if (p && ether_addr_equal(br->dev->dev_addr, addr) && |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 226 | (!vid || (v && br_vlan_should_use(v)))) { |
Toshiaki Makita | 960b589 | 2014-02-07 16:48:23 +0900 | [diff] [blame] | 227 | f->dst = NULL; |
Toshiaki Makita | a778e6d | 2014-02-07 16:48:24 +0900 | [diff] [blame] | 228 | f->added_by_user = 0; |
Toshiaki Makita | 960b589 | 2014-02-07 16:48:23 +0900 | [diff] [blame] | 229 | return; |
| 230 | } |
| 231 | |
| 232 | fdb_delete(br, f); |
| 233 | } |
| 234 | |
Toshiaki Makita | 424bb9c | 2014-02-07 16:48:25 +0900 | [diff] [blame] | 235 | void br_fdb_find_delete_local(struct net_bridge *br, |
| 236 | const struct net_bridge_port *p, |
| 237 | const unsigned char *addr, u16 vid) |
| 238 | { |
Toshiaki Makita | 424bb9c | 2014-02-07 16:48:25 +0900 | [diff] [blame] | 239 | struct net_bridge_fdb_entry *f; |
| 240 | |
| 241 | spin_lock_bh(&br->hash_lock); |
Nikolay Aleksandrov | bfd0aea | 2017-02-13 14:59:09 +0100 | [diff] [blame] | 242 | f = br_fdb_find(br, addr, vid); |
Toshiaki Makita | 424bb9c | 2014-02-07 16:48:25 +0900 | [diff] [blame] | 243 | if (f && f->is_local && !f->added_by_user && f->dst == p) |
| 244 | fdb_delete_local(br, p, f); |
| 245 | spin_unlock_bh(&br->hash_lock); |
| 246 | } |
| 247 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | void br_fdb_changeaddr(struct net_bridge_port *p, const unsigned char *newaddr) |
| 249 | { |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 250 | struct net_bridge_vlan_group *vg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | struct net_bridge *br = p->br; |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 252 | struct net_bridge_vlan *v; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | int i; |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 254 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | spin_lock_bh(&br->hash_lock); |
| 256 | |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 257 | vg = nbp_vlan_group(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | /* Search all chains since old address/hash is unknown */ |
| 259 | for (i = 0; i < BR_HASH_SIZE; i++) { |
| 260 | struct hlist_node *h; |
| 261 | hlist_for_each(h, &br->hash[i]) { |
| 262 | struct net_bridge_fdb_entry *f; |
| 263 | |
| 264 | f = hlist_entry(h, struct net_bridge_fdb_entry, hlist); |
Toshiaki Makita | a5642ab | 2014-02-07 16:48:18 +0900 | [diff] [blame] | 265 | if (f->dst == p && f->is_local && !f->added_by_user) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | /* delete old one */ |
Toshiaki Makita | 960b589 | 2014-02-07 16:48:23 +0900 | [diff] [blame] | 267 | fdb_delete_local(br, p, f); |
| 268 | |
Vlad Yasevich | bc9a25d | 2013-02-13 12:00:19 +0000 | [diff] [blame] | 269 | /* if this port has no vlan information |
| 270 | * configured, we can safely be done at |
| 271 | * this point. |
| 272 | */ |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 273 | if (!vg || !vg->num_vlans) |
Toshiaki Makita | 2836882 | 2014-02-07 16:48:19 +0900 | [diff] [blame] | 274 | goto insert; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | } |
| 276 | } |
| 277 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | |
Toshiaki Makita | 2836882 | 2014-02-07 16:48:19 +0900 | [diff] [blame] | 279 | insert: |
| 280 | /* insert new address, may fail if invalid address or dup. */ |
| 281 | fdb_insert(br, p, newaddr, 0); |
| 282 | |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 283 | if (!vg || !vg->num_vlans) |
Toshiaki Makita | 2836882 | 2014-02-07 16:48:19 +0900 | [diff] [blame] | 284 | goto done; |
| 285 | |
| 286 | /* Now add entries for every VLAN configured on the port. |
| 287 | * This function runs under RTNL so the bitmap will not change |
| 288 | * from under us. |
| 289 | */ |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 290 | list_for_each_entry(v, &vg->vlan_list, vlist) |
| 291 | fdb_insert(br, p, newaddr, v->vid); |
Toshiaki Makita | 2836882 | 2014-02-07 16:48:19 +0900 | [diff] [blame] | 292 | |
Vlad Yasevich | bc9a25d | 2013-02-13 12:00:19 +0000 | [diff] [blame] | 293 | done: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | spin_unlock_bh(&br->hash_lock); |
| 295 | } |
| 296 | |
stephen hemminger | 4359881 | 2011-12-08 07:17:49 +0000 | [diff] [blame] | 297 | void br_fdb_change_mac_address(struct net_bridge *br, const u8 *newaddr) |
| 298 | { |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 299 | struct net_bridge_vlan_group *vg; |
stephen hemminger | 4359881 | 2011-12-08 07:17:49 +0000 | [diff] [blame] | 300 | struct net_bridge_fdb_entry *f; |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 301 | struct net_bridge_vlan *v; |
stephen hemminger | 4359881 | 2011-12-08 07:17:49 +0000 | [diff] [blame] | 302 | |
Toshiaki Makita | ac4c886 | 2014-02-07 16:48:26 +0900 | [diff] [blame] | 303 | spin_lock_bh(&br->hash_lock); |
| 304 | |
stephen hemminger | 4359881 | 2011-12-08 07:17:49 +0000 | [diff] [blame] | 305 | /* If old entry was unassociated with any port, then delete it. */ |
Nikolay Aleksandrov | bfd0aea | 2017-02-13 14:59:09 +0100 | [diff] [blame] | 306 | f = br_fdb_find(br, br->dev->dev_addr, 0); |
Toshiaki Makita | 7bb90c3 | 2016-08-04 11:11:19 +0900 | [diff] [blame] | 307 | if (f && f->is_local && !f->dst && !f->added_by_user) |
Toshiaki Makita | 960b589 | 2014-02-07 16:48:23 +0900 | [diff] [blame] | 308 | fdb_delete_local(br, NULL, f); |
stephen hemminger | 4359881 | 2011-12-08 07:17:49 +0000 | [diff] [blame] | 309 | |
Vlad Yasevich | bc9a25d | 2013-02-13 12:00:19 +0000 | [diff] [blame] | 310 | fdb_insert(br, NULL, newaddr, 0); |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 311 | vg = br_vlan_group(br); |
| 312 | if (!vg || !vg->num_vlans) |
| 313 | goto out; |
Vlad Yasevich | bc9a25d | 2013-02-13 12:00:19 +0000 | [diff] [blame] | 314 | /* Now remove and add entries for every VLAN configured on the |
| 315 | * bridge. This function runs under RTNL so the bitmap will not |
| 316 | * change from under us. |
| 317 | */ |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 318 | list_for_each_entry(v, &vg->vlan_list, vlist) { |
Toshiaki Makita | 0b148de | 2016-06-07 19:14:17 +0900 | [diff] [blame] | 319 | if (!br_vlan_should_use(v)) |
| 320 | continue; |
Nikolay Aleksandrov | bfd0aea | 2017-02-13 14:59:09 +0100 | [diff] [blame] | 321 | f = br_fdb_find(br, br->dev->dev_addr, v->vid); |
Toshiaki Makita | 7bb90c3 | 2016-08-04 11:11:19 +0900 | [diff] [blame] | 322 | if (f && f->is_local && !f->dst && !f->added_by_user) |
Toshiaki Makita | 960b589 | 2014-02-07 16:48:23 +0900 | [diff] [blame] | 323 | fdb_delete_local(br, NULL, f); |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 324 | fdb_insert(br, NULL, newaddr, v->vid); |
Vlad Yasevich | bc9a25d | 2013-02-13 12:00:19 +0000 | [diff] [blame] | 325 | } |
Toshiaki Makita | ac4c886 | 2014-02-07 16:48:26 +0900 | [diff] [blame] | 326 | out: |
| 327 | spin_unlock_bh(&br->hash_lock); |
stephen hemminger | 4359881 | 2011-12-08 07:17:49 +0000 | [diff] [blame] | 328 | } |
| 329 | |
Nikolay Aleksandrov | f7cdee8 | 2017-02-04 18:05:07 +0100 | [diff] [blame] | 330 | void br_fdb_cleanup(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | { |
Nikolay Aleksandrov | f7cdee8 | 2017-02-04 18:05:07 +0100 | [diff] [blame] | 332 | struct net_bridge *br = container_of(work, struct net_bridge, |
| 333 | gc_work.work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | unsigned long delay = hold_time(br); |
Nikolay Aleksandrov | f7cdee8 | 2017-02-04 18:05:07 +0100 | [diff] [blame] | 335 | unsigned long work_delay = delay; |
| 336 | unsigned long now = jiffies; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | int i; |
| 338 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | for (i = 0; i < BR_HASH_SIZE; i++) { |
| 340 | struct net_bridge_fdb_entry *f; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 341 | struct hlist_node *n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | |
Nikolay Aleksandrov | f7cdee8 | 2017-02-04 18:05:07 +0100 | [diff] [blame] | 343 | if (!br->hash[i].first) |
| 344 | continue; |
| 345 | |
| 346 | spin_lock_bh(&br->hash_lock); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 347 | hlist_for_each_entry_safe(f, n, &br->hash[i], hlist) { |
Baruch Even | 071f772 | 2007-05-31 01:20:45 -0700 | [diff] [blame] | 348 | unsigned long this_timer; |
Nikolay Aleksandrov | f7cdee8 | 2017-02-04 18:05:07 +0100 | [diff] [blame] | 349 | |
David S. Miller | 3fcf901 | 2015-02-04 23:52:44 -0800 | [diff] [blame] | 350 | if (f->is_static) |
Baruch Even | 071f772 | 2007-05-31 01:20:45 -0700 | [diff] [blame] | 351 | continue; |
Siva Mannem | dcd45e0 | 2015-09-23 08:39:19 -0700 | [diff] [blame] | 352 | if (f->added_by_external_learn) |
| 353 | continue; |
stephen hemminger | 7cd8861 | 2011-04-04 14:03:28 +0000 | [diff] [blame] | 354 | this_timer = f->updated + delay; |
Nikolay Aleksandrov | f7cdee8 | 2017-02-04 18:05:07 +0100 | [diff] [blame] | 355 | if (time_after(this_timer, now)) |
| 356 | work_delay = min(work_delay, this_timer - now); |
| 357 | else |
stephen hemminger | 31e8a49c | 2011-12-08 07:17:41 +0000 | [diff] [blame] | 358 | fdb_delete(br, f); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | } |
Nikolay Aleksandrov | f7cdee8 | 2017-02-04 18:05:07 +0100 | [diff] [blame] | 360 | spin_unlock_bh(&br->hash_lock); |
| 361 | cond_resched(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | |
Nikolay Aleksandrov | f7cdee8 | 2017-02-04 18:05:07 +0100 | [diff] [blame] | 364 | /* Cleanup minimum 10 milliseconds apart */ |
| 365 | work_delay = max_t(unsigned long, work_delay, msecs_to_jiffies(10)); |
| 366 | mod_delayed_work(system_long_wq, &br->gc_work, work_delay); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | } |
| 368 | |
Stephen Hemminger | 9cf6374 | 2007-04-09 12:57:54 -0700 | [diff] [blame] | 369 | /* Completely flush all dynamic entries in forwarding database.*/ |
| 370 | void br_fdb_flush(struct net_bridge *br) |
| 371 | { |
| 372 | int i; |
Stephen Hemminger | 1a62069 | 2006-10-12 14:45:38 -0700 | [diff] [blame] | 373 | |
Stephen Hemminger | 9cf6374 | 2007-04-09 12:57:54 -0700 | [diff] [blame] | 374 | spin_lock_bh(&br->hash_lock); |
| 375 | for (i = 0; i < BR_HASH_SIZE; i++) { |
| 376 | struct net_bridge_fdb_entry *f; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 377 | struct hlist_node *n; |
| 378 | hlist_for_each_entry_safe(f, n, &br->hash[i], hlist) { |
Stephen Hemminger | 9cf6374 | 2007-04-09 12:57:54 -0700 | [diff] [blame] | 379 | if (!f->is_static) |
stephen hemminger | 31e8a49c | 2011-12-08 07:17:41 +0000 | [diff] [blame] | 380 | fdb_delete(br, f); |
Stephen Hemminger | 9cf6374 | 2007-04-09 12:57:54 -0700 | [diff] [blame] | 381 | } |
| 382 | } |
| 383 | spin_unlock_bh(&br->hash_lock); |
| 384 | } |
| 385 | |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 386 | /* Flush all entries referring to a specific port. |
Stephen Hemminger | 9cf6374 | 2007-04-09 12:57:54 -0700 | [diff] [blame] | 387 | * if do_all is set also flush static entries |
Nikolay Aleksandrov | 1ea2d02 | 2015-06-23 05:28:16 -0700 | [diff] [blame] | 388 | * if vid is set delete all entries that match the vlan_id |
Stephen Hemminger | 9cf6374 | 2007-04-09 12:57:54 -0700 | [diff] [blame] | 389 | */ |
Stephen Hemminger | 1a62069 | 2006-10-12 14:45:38 -0700 | [diff] [blame] | 390 | void br_fdb_delete_by_port(struct net_bridge *br, |
| 391 | const struct net_bridge_port *p, |
Nikolay Aleksandrov | 1ea2d02 | 2015-06-23 05:28:16 -0700 | [diff] [blame] | 392 | u16 vid, |
Stephen Hemminger | 1a62069 | 2006-10-12 14:45:38 -0700 | [diff] [blame] | 393 | int do_all) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | { |
| 395 | int i; |
| 396 | |
| 397 | spin_lock_bh(&br->hash_lock); |
| 398 | for (i = 0; i < BR_HASH_SIZE; i++) { |
| 399 | struct hlist_node *h, *g; |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 400 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | hlist_for_each_safe(h, g, &br->hash[i]) { |
| 402 | struct net_bridge_fdb_entry *f |
| 403 | = hlist_entry(h, struct net_bridge_fdb_entry, hlist); |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 404 | if (f->dst != p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | continue; |
| 406 | |
Nikolay Aleksandrov | 1ea2d02 | 2015-06-23 05:28:16 -0700 | [diff] [blame] | 407 | if (!do_all) |
| 408 | if (f->is_static || (vid && f->vlan_id != vid)) |
| 409 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | |
Toshiaki Makita | a778e6d | 2014-02-07 16:48:24 +0900 | [diff] [blame] | 411 | if (f->is_local) |
| 412 | fdb_delete_local(br, p, f); |
| 413 | else |
| 414 | fdb_delete(br, f); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | } |
| 416 | } |
| 417 | spin_unlock_bh(&br->hash_lock); |
| 418 | } |
| 419 | |
Igor Maravić | e6373c4 | 2011-12-12 02:58:25 +0000 | [diff] [blame] | 420 | #if IS_ENABLED(CONFIG_ATM_LANE) |
Michał Mirosław | da67829 | 2009-06-05 05:35:28 +0000 | [diff] [blame] | 421 | /* Interface used by ATM LANE hook to test |
| 422 | * if an addr is on some other bridge port */ |
| 423 | int br_fdb_test_addr(struct net_device *dev, unsigned char *addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | { |
| 425 | struct net_bridge_fdb_entry *fdb; |
stephen hemminger | b5ed54e | 2010-11-15 06:38:13 +0000 | [diff] [blame] | 426 | struct net_bridge_port *port; |
Michał Mirosław | da67829 | 2009-06-05 05:35:28 +0000 | [diff] [blame] | 427 | int ret; |
| 428 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | rcu_read_lock(); |
stephen hemminger | b5ed54e | 2010-11-15 06:38:13 +0000 | [diff] [blame] | 430 | port = br_port_get_rcu(dev); |
| 431 | if (!port) |
| 432 | ret = 0; |
| 433 | else { |
Nikolay Aleksandrov | bfd0aea | 2017-02-13 14:59:09 +0100 | [diff] [blame] | 434 | fdb = br_fdb_find_rcu(port->br, addr, 0); |
stephen hemminger | 4359881 | 2011-12-08 07:17:49 +0000 | [diff] [blame] | 435 | ret = fdb && fdb->dst && fdb->dst->dev != dev && |
stephen hemminger | b5ed54e | 2010-11-15 06:38:13 +0000 | [diff] [blame] | 436 | fdb->dst->state == BR_STATE_FORWARDING; |
| 437 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | |
Michał Mirosław | da67829 | 2009-06-05 05:35:28 +0000 | [diff] [blame] | 440 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | } |
Michał Mirosław | da67829 | 2009-06-05 05:35:28 +0000 | [diff] [blame] | 442 | #endif /* CONFIG_ATM_LANE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | |
| 444 | /* |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 445 | * Fill buffer with forwarding table records in |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | * the API format. |
| 447 | */ |
| 448 | int br_fdb_fillbuf(struct net_bridge *br, void *buf, |
| 449 | unsigned long maxnum, unsigned long skip) |
| 450 | { |
| 451 | struct __fdb_entry *fe = buf; |
| 452 | int i, num = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | struct net_bridge_fdb_entry *f; |
| 454 | |
| 455 | memset(buf, 0, maxnum*sizeof(struct __fdb_entry)); |
| 456 | |
| 457 | rcu_read_lock(); |
| 458 | for (i = 0; i < BR_HASH_SIZE; i++) { |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 459 | hlist_for_each_entry_rcu(f, &br->hash[i], hlist) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | if (num >= maxnum) |
| 461 | goto out; |
| 462 | |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 463 | if (has_expired(br, f)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | continue; |
| 465 | |
stephen hemminger | 4359881 | 2011-12-08 07:17:49 +0000 | [diff] [blame] | 466 | /* ignore pseudo entry for local MAC address */ |
| 467 | if (!f->dst) |
| 468 | continue; |
| 469 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | if (skip) { |
| 471 | --skip; |
| 472 | continue; |
| 473 | } |
| 474 | |
| 475 | /* convert from internal format to API */ |
| 476 | memcpy(fe->mac_addr, f->addr.addr, ETH_ALEN); |
Stephen Hemminger | ae4f8fc | 2008-05-02 16:53:33 -0700 | [diff] [blame] | 477 | |
| 478 | /* due to ABI compat need to split into hi/lo */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | fe->port_no = f->dst->port_no; |
Stephen Hemminger | ae4f8fc | 2008-05-02 16:53:33 -0700 | [diff] [blame] | 480 | fe->port_hi = f->dst->port_no >> 8; |
| 481 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | fe->is_local = f->is_local; |
| 483 | if (!f->is_static) |
Eric Dumazet | a399a80 | 2012-08-08 21:13:53 +0000 | [diff] [blame] | 484 | fe->ageing_timer_value = jiffies_delta_to_clock_t(jiffies - f->updated); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | ++fe; |
| 486 | ++num; |
| 487 | } |
| 488 | } |
| 489 | |
| 490 | out: |
| 491 | rcu_read_unlock(); |
| 492 | |
| 493 | return num; |
| 494 | } |
| 495 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | static struct net_bridge_fdb_entry *fdb_create(struct hlist_head *head, |
| 497 | struct net_bridge_port *source, |
Vlad Yasevich | 2ba071e | 2013-02-13 12:00:16 +0000 | [diff] [blame] | 498 | const unsigned char *addr, |
Roopa Prabhu | b7af147 | 2015-10-27 07:52:56 -0700 | [diff] [blame] | 499 | __u16 vid, |
| 500 | unsigned char is_local, |
| 501 | unsigned char is_static) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | { |
| 503 | struct net_bridge_fdb_entry *fdb; |
| 504 | |
| 505 | fdb = kmem_cache_alloc(br_fdb_cache, GFP_ATOMIC); |
| 506 | if (fdb) { |
| 507 | memcpy(fdb->addr.addr, addr, ETH_ALEN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 508 | fdb->dst = source; |
Vlad Yasevich | 2ba071e | 2013-02-13 12:00:16 +0000 | [diff] [blame] | 509 | fdb->vlan_id = vid; |
Roopa Prabhu | b7af147 | 2015-10-27 07:52:56 -0700 | [diff] [blame] | 510 | fdb->is_local = is_local; |
| 511 | fdb->is_static = is_static; |
Toshiaki Makita | a5642ab | 2014-02-07 16:48:18 +0900 | [diff] [blame] | 512 | fdb->added_by_user = 0; |
Scott Feldman | cf6b8e1 | 2014-11-28 14:34:21 +0100 | [diff] [blame] | 513 | fdb->added_by_external_learn = 0; |
Arkadi Sharshevsky | 9fe8bce | 2017-06-08 08:44:15 +0200 | [diff] [blame^] | 514 | fdb->offloaded = 0; |
stephen hemminger | 7cd8861 | 2011-04-04 14:03:28 +0000 | [diff] [blame] | 515 | fdb->updated = fdb->used = jiffies; |
Pavel Emelyanov | 1158f76 | 2011-02-04 13:02:36 -0800 | [diff] [blame] | 516 | hlist_add_head_rcu(&fdb->hlist, head); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | } |
| 518 | return fdb; |
| 519 | } |
| 520 | |
| 521 | 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] | 522 | const unsigned char *addr, u16 vid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | { |
Vlad Yasevich | bc9a25d | 2013-02-13 12:00:19 +0000 | [diff] [blame] | 524 | struct hlist_head *head = &br->hash[br_mac_hash(addr, vid)]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | struct net_bridge_fdb_entry *fdb; |
| 526 | |
| 527 | if (!is_valid_ether_addr(addr)) |
| 528 | return -EINVAL; |
| 529 | |
Nikolay Aleksandrov | bfd0aea | 2017-02-13 14:59:09 +0100 | [diff] [blame] | 530 | fdb = br_fdb_find(br, addr, vid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | if (fdb) { |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 532 | /* it is okay to have multiple ports with same |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 533 | * address, just use the first one. |
| 534 | */ |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 535 | if (fdb->is_local) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | return 0; |
Roopa Prabhu | de1dfee | 2016-10-11 16:12:56 -0700 | [diff] [blame] | 537 | br_warn(br, "adding interface %s with same address as a received packet (addr:%pM, vlan:%u)\n", |
| 538 | source ? source->dev->name : br->dev->name, addr, vid); |
stephen hemminger | 31e8a49c | 2011-12-08 07:17:41 +0000 | [diff] [blame] | 539 | fdb_delete(br, fdb); |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 540 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | |
Roopa Prabhu | b7af147 | 2015-10-27 07:52:56 -0700 | [diff] [blame] | 542 | fdb = fdb_create(head, source, addr, vid, 1, 1); |
stephen hemminger | 03e9b64 | 2011-04-04 14:03:27 +0000 | [diff] [blame] | 543 | if (!fdb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | return -ENOMEM; |
| 545 | |
Jiri Pirko | 020ec6b | 2014-11-28 14:34:12 +0100 | [diff] [blame] | 546 | fdb_add_hw_addr(br, addr); |
stephen hemminger | 31e8a49c | 2011-12-08 07:17:41 +0000 | [diff] [blame] | 547 | fdb_notify(br, fdb, RTM_NEWNEIGH); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 548 | return 0; |
| 549 | } |
| 550 | |
stephen hemminger | 03e9b64 | 2011-04-04 14:03:27 +0000 | [diff] [blame] | 551 | /* Add entry for local address of interface */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | 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] | 553 | const unsigned char *addr, u16 vid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 554 | { |
| 555 | int ret; |
| 556 | |
| 557 | spin_lock_bh(&br->hash_lock); |
Vlad Yasevich | bc9a25d | 2013-02-13 12:00:19 +0000 | [diff] [blame] | 558 | ret = fdb_insert(br, source, addr, vid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 559 | spin_unlock_bh(&br->hash_lock); |
| 560 | return ret; |
| 561 | } |
| 562 | |
| 563 | 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] | 564 | const unsigned char *addr, u16 vid, bool added_by_user) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 | { |
Vlad Yasevich | 2ba071e | 2013-02-13 12:00:16 +0000 | [diff] [blame] | 566 | struct hlist_head *head = &br->hash[br_mac_hash(addr, vid)]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | struct net_bridge_fdb_entry *fdb; |
Jon Maxwell | c65c7a3 | 2014-05-29 17:27:16 +1000 | [diff] [blame] | 568 | bool fdb_modified = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | |
| 570 | /* some users want to always flood. */ |
| 571 | if (hold_time(br) == 0) |
| 572 | return; |
| 573 | |
Stephen Hemminger | df1c0b8 | 2007-08-30 22:15:35 -0700 | [diff] [blame] | 574 | /* ignore packets unless we are using this port */ |
| 575 | if (!(source->state == BR_STATE_LEARNING || |
| 576 | source->state == BR_STATE_FORWARDING)) |
| 577 | return; |
| 578 | |
Vlad Yasevich | 2ba071e | 2013-02-13 12:00:16 +0000 | [diff] [blame] | 579 | fdb = fdb_find_rcu(head, addr, vid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 580 | if (likely(fdb)) { |
| 581 | /* attempt to update an entry for a local interface */ |
| 582 | if (unlikely(fdb->is_local)) { |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 583 | if (net_ratelimit()) |
Roopa Prabhu | de1dfee | 2016-10-11 16:12:56 -0700 | [diff] [blame] | 584 | br_warn(br, "received packet on %s with own address as source address (addr:%pM, vlan:%u)\n", |
| 585 | source->dev->name, addr, vid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 586 | } else { |
stephen hemminger | ca6d448 | 2017-02-07 08:46:46 -0800 | [diff] [blame] | 587 | unsigned long now = jiffies; |
| 588 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 589 | /* fastpath: update of existing entry */ |
Jon Maxwell | c65c7a3 | 2014-05-29 17:27:16 +1000 | [diff] [blame] | 590 | if (unlikely(source != fdb->dst)) { |
| 591 | fdb->dst = source; |
| 592 | fdb_modified = true; |
Arkadi Sharshevsky | 58073b3 | 2017-04-28 22:39:07 +0300 | [diff] [blame] | 593 | /* Take over HW learned entry */ |
| 594 | if (unlikely(fdb->added_by_external_learn)) |
| 595 | fdb->added_by_external_learn = 0; |
Jon Maxwell | c65c7a3 | 2014-05-29 17:27:16 +1000 | [diff] [blame] | 596 | } |
stephen hemminger | ca6d448 | 2017-02-07 08:46:46 -0800 | [diff] [blame] | 597 | if (now != fdb->updated) |
| 598 | fdb->updated = now; |
Toshiaki Makita | a5642ab | 2014-02-07 16:48:18 +0900 | [diff] [blame] | 599 | if (unlikely(added_by_user)) |
| 600 | fdb->added_by_user = 1; |
Jon Maxwell | c65c7a3 | 2014-05-29 17:27:16 +1000 | [diff] [blame] | 601 | if (unlikely(fdb_modified)) |
| 602 | fdb_notify(br, fdb, RTM_NEWNEIGH); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 | } |
| 604 | } else { |
Stephen Hemminger | f8ae737 | 2006-03-20 22:58:36 -0800 | [diff] [blame] | 605 | spin_lock(&br->hash_lock); |
Nikolay Aleksandrov | bfd0aea | 2017-02-13 14:59:09 +0100 | [diff] [blame] | 606 | if (likely(!fdb_find_rcu(head, addr, vid))) { |
Roopa Prabhu | b7af147 | 2015-10-27 07:52:56 -0700 | [diff] [blame] | 607 | fdb = fdb_create(head, source, addr, vid, 0, 0); |
Toshiaki Makita | a5642ab | 2014-02-07 16:48:18 +0900 | [diff] [blame] | 608 | if (fdb) { |
| 609 | if (unlikely(added_by_user)) |
| 610 | fdb->added_by_user = 1; |
stephen hemminger | 31e8a49c | 2011-12-08 07:17:41 +0000 | [diff] [blame] | 611 | fdb_notify(br, fdb, RTM_NEWNEIGH); |
Toshiaki Makita | a5642ab | 2014-02-07 16:48:18 +0900 | [diff] [blame] | 612 | } |
stephen hemminger | f58ee4e | 2011-12-06 13:02:24 +0000 | [diff] [blame] | 613 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | /* else we lose race and someone else inserts |
| 615 | * it first, don't bother updating |
| 616 | */ |
Stephen Hemminger | f8ae737 | 2006-03-20 22:58:36 -0800 | [diff] [blame] | 617 | spin_unlock(&br->hash_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 619 | } |
stephen hemminger | b078f0d | 2011-04-04 14:03:30 +0000 | [diff] [blame] | 620 | |
Roopa Prabhu | 3741873 | 2015-10-08 10:38:52 -0700 | [diff] [blame] | 621 | static int fdb_to_nud(const struct net_bridge *br, |
| 622 | const struct net_bridge_fdb_entry *fdb) |
stephen hemminger | b078f0d | 2011-04-04 14:03:30 +0000 | [diff] [blame] | 623 | { |
| 624 | if (fdb->is_local) |
| 625 | return NUD_PERMANENT; |
| 626 | else if (fdb->is_static) |
| 627 | return NUD_NOARP; |
Roopa Prabhu | 3741873 | 2015-10-08 10:38:52 -0700 | [diff] [blame] | 628 | else if (has_expired(br, fdb)) |
stephen hemminger | b078f0d | 2011-04-04 14:03:30 +0000 | [diff] [blame] | 629 | return NUD_STALE; |
| 630 | else |
| 631 | return NUD_REACHABLE; |
| 632 | } |
| 633 | |
stephen hemminger | 31e8a49c | 2011-12-08 07:17:41 +0000 | [diff] [blame] | 634 | 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] | 635 | const struct net_bridge_fdb_entry *fdb, |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 636 | u32 portid, u32 seq, int type, unsigned int flags) |
stephen hemminger | b078f0d | 2011-04-04 14:03:30 +0000 | [diff] [blame] | 637 | { |
| 638 | unsigned long now = jiffies; |
| 639 | struct nda_cacheinfo ci; |
| 640 | struct nlmsghdr *nlh; |
| 641 | struct ndmsg *ndm; |
| 642 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 643 | nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags); |
stephen hemminger | b078f0d | 2011-04-04 14:03:30 +0000 | [diff] [blame] | 644 | if (nlh == NULL) |
| 645 | return -EMSGSIZE; |
| 646 | |
stephen hemminger | b078f0d | 2011-04-04 14:03:30 +0000 | [diff] [blame] | 647 | ndm = nlmsg_data(nlh); |
| 648 | ndm->ndm_family = AF_BRIDGE; |
| 649 | ndm->ndm_pad1 = 0; |
| 650 | ndm->ndm_pad2 = 0; |
Arkadi Sharshevsky | 9fe8bce | 2017-06-08 08:44:15 +0200 | [diff] [blame^] | 651 | ndm->ndm_flags = 0; |
stephen hemminger | b078f0d | 2011-04-04 14:03:30 +0000 | [diff] [blame] | 652 | ndm->ndm_type = 0; |
stephen hemminger | 4359881 | 2011-12-08 07:17:49 +0000 | [diff] [blame] | 653 | ndm->ndm_ifindex = fdb->dst ? fdb->dst->dev->ifindex : br->dev->ifindex; |
Roopa Prabhu | 3741873 | 2015-10-08 10:38:52 -0700 | [diff] [blame] | 654 | ndm->ndm_state = fdb_to_nud(br, fdb); |
stephen hemminger | b078f0d | 2011-04-04 14:03:30 +0000 | [diff] [blame] | 655 | |
Arkadi Sharshevsky | 9fe8bce | 2017-06-08 08:44:15 +0200 | [diff] [blame^] | 656 | if (fdb->offloaded) |
| 657 | ndm->ndm_flags |= NTF_OFFLOADED; |
| 658 | if (fdb->added_by_external_learn) |
| 659 | ndm->ndm_flags |= NTF_EXT_LEARNED; |
| 660 | |
David S. Miller | 2eb812e | 2012-04-01 20:49:54 -0400 | [diff] [blame] | 661 | if (nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->addr)) |
| 662 | goto nla_put_failure; |
Roopa Prabhu | 41c389d | 2014-05-27 22:39:37 -0700 | [diff] [blame] | 663 | if (nla_put_u32(skb, NDA_MASTER, br->dev->ifindex)) |
| 664 | goto nla_put_failure; |
stephen hemminger | b078f0d | 2011-04-04 14:03:30 +0000 | [diff] [blame] | 665 | ci.ndm_used = jiffies_to_clock_t(now - fdb->used); |
| 666 | ci.ndm_confirmed = 0; |
| 667 | ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated); |
| 668 | ci.ndm_refcnt = 0; |
David S. Miller | 2eb812e | 2012-04-01 20:49:54 -0400 | [diff] [blame] | 669 | if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci)) |
| 670 | goto nla_put_failure; |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 671 | |
Toshiaki Makita | 47fab41 | 2014-07-30 13:31:51 +0900 | [diff] [blame] | 672 | if (fdb->vlan_id && nla_put(skb, NDA_VLAN, sizeof(u16), &fdb->vlan_id)) |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 673 | goto nla_put_failure; |
| 674 | |
Johannes Berg | 053c095 | 2015-01-16 22:09:00 +0100 | [diff] [blame] | 675 | nlmsg_end(skb, nlh); |
| 676 | return 0; |
stephen hemminger | b078f0d | 2011-04-04 14:03:30 +0000 | [diff] [blame] | 677 | |
| 678 | nla_put_failure: |
| 679 | nlmsg_cancel(skb, nlh); |
| 680 | return -EMSGSIZE; |
| 681 | } |
| 682 | |
| 683 | static inline size_t fdb_nlmsg_size(void) |
| 684 | { |
| 685 | return NLMSG_ALIGN(sizeof(struct ndmsg)) |
| 686 | + nla_total_size(ETH_ALEN) /* NDA_LLADDR */ |
Roopa Prabhu | 41c389d | 2014-05-27 22:39:37 -0700 | [diff] [blame] | 687 | + nla_total_size(sizeof(u32)) /* NDA_MASTER */ |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 688 | + nla_total_size(sizeof(u16)) /* NDA_VLAN */ |
stephen hemminger | b078f0d | 2011-04-04 14:03:30 +0000 | [diff] [blame] | 689 | + nla_total_size(sizeof(struct nda_cacheinfo)); |
| 690 | } |
| 691 | |
stephen hemminger | 31e8a49c | 2011-12-08 07:17:41 +0000 | [diff] [blame] | 692 | static void fdb_notify(struct net_bridge *br, |
| 693 | const struct net_bridge_fdb_entry *fdb, int type) |
stephen hemminger | b078f0d | 2011-04-04 14:03:30 +0000 | [diff] [blame] | 694 | { |
stephen hemminger | 31e8a49c | 2011-12-08 07:17:41 +0000 | [diff] [blame] | 695 | struct net *net = dev_net(br->dev); |
stephen hemminger | b078f0d | 2011-04-04 14:03:30 +0000 | [diff] [blame] | 696 | struct sk_buff *skb; |
| 697 | int err = -ENOBUFS; |
| 698 | |
Arkadi Sharshevsky | 6b26b51 | 2017-06-08 08:44:14 +0200 | [diff] [blame] | 699 | br_switchdev_fdb_notify(fdb, type); |
| 700 | |
stephen hemminger | b078f0d | 2011-04-04 14:03:30 +0000 | [diff] [blame] | 701 | skb = nlmsg_new(fdb_nlmsg_size(), GFP_ATOMIC); |
| 702 | if (skb == NULL) |
| 703 | goto errout; |
| 704 | |
stephen hemminger | 31e8a49c | 2011-12-08 07:17:41 +0000 | [diff] [blame] | 705 | err = fdb_fill_info(skb, br, fdb, 0, 0, type, 0); |
stephen hemminger | b078f0d | 2011-04-04 14:03:30 +0000 | [diff] [blame] | 706 | if (err < 0) { |
| 707 | /* -EMSGSIZE implies BUG in fdb_nlmsg_size() */ |
| 708 | WARN_ON(err == -EMSGSIZE); |
| 709 | kfree_skb(skb); |
| 710 | goto errout; |
| 711 | } |
| 712 | rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC); |
| 713 | return; |
| 714 | errout: |
tanxiaojun | 87e823b | 2013-12-19 13:28:10 +0800 | [diff] [blame] | 715 | rtnl_set_sk_err(net, RTNLGRP_NEIGH, err); |
stephen hemminger | b078f0d | 2011-04-04 14:03:30 +0000 | [diff] [blame] | 716 | } |
| 717 | |
| 718 | /* Dump information about entries, in response to GETNEIGH */ |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 719 | int br_fdb_dump(struct sk_buff *skb, |
| 720 | struct netlink_callback *cb, |
| 721 | struct net_device *dev, |
Jamal Hadi Salim | 5d5eacb | 2014-07-10 07:01:58 -0400 | [diff] [blame] | 722 | struct net_device *filter_dev, |
Roopa Prabhu | d297653 | 2016-08-30 21:56:45 -0700 | [diff] [blame] | 723 | int *idx) |
stephen hemminger | b078f0d | 2011-04-04 14:03:30 +0000 | [diff] [blame] | 724 | { |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 725 | struct net_bridge *br = netdev_priv(dev); |
Roopa Prabhu | d297653 | 2016-08-30 21:56:45 -0700 | [diff] [blame] | 726 | int err = 0; |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 727 | int i; |
stephen hemminger | b078f0d | 2011-04-04 14:03:30 +0000 | [diff] [blame] | 728 | |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 729 | if (!(dev->priv_flags & IFF_EBRIDGE)) |
| 730 | goto out; |
stephen hemminger | b078f0d | 2011-04-04 14:03:30 +0000 | [diff] [blame] | 731 | |
Roopa Prabhu | d297653 | 2016-08-30 21:56:45 -0700 | [diff] [blame] | 732 | if (!filter_dev) { |
| 733 | err = ndo_dflt_fdb_dump(skb, cb, dev, NULL, idx); |
| 734 | if (err < 0) |
| 735 | goto out; |
| 736 | } |
Hubert Sokolowski | 6cb6974 | 2015-01-05 17:29:21 +0000 | [diff] [blame] | 737 | |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 738 | for (i = 0; i < BR_HASH_SIZE; i++) { |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 739 | struct net_bridge_fdb_entry *f; |
stephen hemminger | b078f0d | 2011-04-04 14:03:30 +0000 | [diff] [blame] | 740 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 741 | hlist_for_each_entry_rcu(f, &br->hash[i], hlist) { |
MINOURA Makoto / 箕浦 真 | 472681d | 2016-02-25 14:20:48 +0900 | [diff] [blame] | 742 | |
Roopa Prabhu | d297653 | 2016-08-30 21:56:45 -0700 | [diff] [blame] | 743 | if (*idx < cb->args[2]) |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 744 | goto skip; |
stephen hemminger | b078f0d | 2011-04-04 14:03:30 +0000 | [diff] [blame] | 745 | |
Jamal Hadi Salim | 5e6d243 | 2014-07-10 07:01:59 -0400 | [diff] [blame] | 746 | if (filter_dev && |
| 747 | (!f->dst || f->dst->dev != filter_dev)) { |
| 748 | if (filter_dev != dev) |
| 749 | goto skip; |
Hubert Sokolowski | 6cb6974 | 2015-01-05 17:29:21 +0000 | [diff] [blame] | 750 | /* !f->dst is a special case for bridge |
Jamal Hadi Salim | 5e6d243 | 2014-07-10 07:01:59 -0400 | [diff] [blame] | 751 | * It means the MAC belongs to the bridge |
| 752 | * Therefore need a little more filtering |
| 753 | * we only want to dump the !f->dst case |
| 754 | */ |
| 755 | if (f->dst) |
| 756 | goto skip; |
| 757 | } |
Hubert Sokolowski | 6cb6974 | 2015-01-05 17:29:21 +0000 | [diff] [blame] | 758 | if (!filter_dev && f->dst) |
| 759 | goto skip; |
Jamal Hadi Salim | 5d5eacb | 2014-07-10 07:01:58 -0400 | [diff] [blame] | 760 | |
MINOURA Makoto / 箕浦 真 | 472681d | 2016-02-25 14:20:48 +0900 | [diff] [blame] | 761 | err = fdb_fill_info(skb, br, f, |
| 762 | NETLINK_CB(cb->skb).portid, |
| 763 | cb->nlh->nlmsg_seq, |
| 764 | RTM_NEWNEIGH, |
| 765 | NLM_F_MULTI); |
Roopa Prabhu | d297653 | 2016-08-30 21:56:45 -0700 | [diff] [blame] | 766 | if (err < 0) |
| 767 | goto out; |
stephen hemminger | b078f0d | 2011-04-04 14:03:30 +0000 | [diff] [blame] | 768 | skip: |
Roopa Prabhu | d297653 | 2016-08-30 21:56:45 -0700 | [diff] [blame] | 769 | *idx += 1; |
stephen hemminger | b078f0d | 2011-04-04 14:03:30 +0000 | [diff] [blame] | 770 | } |
| 771 | } |
stephen hemminger | b078f0d | 2011-04-04 14:03:30 +0000 | [diff] [blame] | 772 | |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 773 | out: |
Roopa Prabhu | d297653 | 2016-08-30 21:56:45 -0700 | [diff] [blame] | 774 | return err; |
stephen hemminger | b078f0d | 2011-04-04 14:03:30 +0000 | [diff] [blame] | 775 | } |
stephen hemminger | 36fd2b6 | 2011-04-04 14:03:31 +0000 | [diff] [blame] | 776 | |
stephen hemminger | 292d139 | 2011-11-09 18:30:08 +0000 | [diff] [blame] | 777 | /* Update (create or replace) forwarding database entry */ |
Toshiaki Makita | 7bb90c3 | 2016-08-04 11:11:19 +0900 | [diff] [blame] | 778 | static int fdb_add_entry(struct net_bridge *br, struct net_bridge_port *source, |
| 779 | const __u8 *addr, __u16 state, __u16 flags, __u16 vid) |
stephen hemminger | 36fd2b6 | 2011-04-04 14:03:31 +0000 | [diff] [blame] | 780 | { |
Vlad Yasevich | 2ba071e | 2013-02-13 12:00:16 +0000 | [diff] [blame] | 781 | struct hlist_head *head = &br->hash[br_mac_hash(addr, vid)]; |
stephen hemminger | 36fd2b6 | 2011-04-04 14:03:31 +0000 | [diff] [blame] | 782 | struct net_bridge_fdb_entry *fdb; |
roopa | b0a397f | 2013-04-22 12:56:49 +0000 | [diff] [blame] | 783 | bool modified = false; |
stephen hemminger | 36fd2b6 | 2011-04-04 14:03:31 +0000 | [diff] [blame] | 784 | |
Wilson Kok | eb8d7ba | 2015-05-25 06:39:31 -0700 | [diff] [blame] | 785 | /* If the port cannot learn allow only local and static entries */ |
Toshiaki Makita | 7bb90c3 | 2016-08-04 11:11:19 +0900 | [diff] [blame] | 786 | if (source && !(state & NUD_PERMANENT) && !(state & NUD_NOARP) && |
Wilson Kok | eb8d7ba | 2015-05-25 06:39:31 -0700 | [diff] [blame] | 787 | !(source->state == BR_STATE_LEARNING || |
| 788 | source->state == BR_STATE_FORWARDING)) |
| 789 | return -EPERM; |
| 790 | |
Toshiaki Makita | 7bb90c3 | 2016-08-04 11:11:19 +0900 | [diff] [blame] | 791 | if (!source && !(state & NUD_PERMANENT)) { |
| 792 | pr_info("bridge: RTM_NEWNEIGH %s without NUD_PERMANENT\n", |
| 793 | br->dev->name); |
| 794 | return -EINVAL; |
| 795 | } |
| 796 | |
Nikolay Aleksandrov | bfd0aea | 2017-02-13 14:59:09 +0100 | [diff] [blame] | 797 | fdb = br_fdb_find(br, addr, vid); |
stephen hemminger | 64af1ba | 2011-09-30 14:37:27 +0000 | [diff] [blame] | 798 | if (fdb == NULL) { |
| 799 | if (!(flags & NLM_F_CREATE)) |
| 800 | return -ENOENT; |
stephen hemminger | 36fd2b6 | 2011-04-04 14:03:31 +0000 | [diff] [blame] | 801 | |
Roopa Prabhu | b7af147 | 2015-10-27 07:52:56 -0700 | [diff] [blame] | 802 | fdb = fdb_create(head, source, addr, vid, 0, 0); |
stephen hemminger | 64af1ba | 2011-09-30 14:37:27 +0000 | [diff] [blame] | 803 | if (!fdb) |
| 804 | return -ENOMEM; |
roopa | b0a397f | 2013-04-22 12:56:49 +0000 | [diff] [blame] | 805 | |
| 806 | modified = true; |
stephen hemminger | 64af1ba | 2011-09-30 14:37:27 +0000 | [diff] [blame] | 807 | } else { |
| 808 | if (flags & NLM_F_EXCL) |
| 809 | return -EEXIST; |
roopa | b0a397f | 2013-04-22 12:56:49 +0000 | [diff] [blame] | 810 | |
| 811 | if (fdb->dst != source) { |
| 812 | fdb->dst = source; |
| 813 | modified = true; |
| 814 | } |
stephen hemminger | 64af1ba | 2011-09-30 14:37:27 +0000 | [diff] [blame] | 815 | } |
stephen hemminger | 36fd2b6 | 2011-04-04 14:03:31 +0000 | [diff] [blame] | 816 | |
Roopa Prabhu | 3741873 | 2015-10-08 10:38:52 -0700 | [diff] [blame] | 817 | if (fdb_to_nud(br, fdb) != state) { |
Vlad Yasevich | 145beee | 2014-05-16 09:59:19 -0400 | [diff] [blame] | 818 | if (state & NUD_PERMANENT) { |
| 819 | fdb->is_local = 1; |
| 820 | if (!fdb->is_static) { |
| 821 | fdb->is_static = 1; |
Jiri Pirko | 020ec6b | 2014-11-28 14:34:12 +0100 | [diff] [blame] | 822 | fdb_add_hw_addr(br, addr); |
Vlad Yasevich | 145beee | 2014-05-16 09:59:19 -0400 | [diff] [blame] | 823 | } |
| 824 | } else if (state & NUD_NOARP) { |
stephen hemminger | 292d139 | 2011-11-09 18:30:08 +0000 | [diff] [blame] | 825 | fdb->is_local = 0; |
Vlad Yasevich | 145beee | 2014-05-16 09:59:19 -0400 | [diff] [blame] | 826 | if (!fdb->is_static) { |
| 827 | fdb->is_static = 1; |
Jiri Pirko | 020ec6b | 2014-11-28 14:34:12 +0100 | [diff] [blame] | 828 | fdb_add_hw_addr(br, addr); |
Vlad Yasevich | 145beee | 2014-05-16 09:59:19 -0400 | [diff] [blame] | 829 | } |
| 830 | } else { |
| 831 | fdb->is_local = 0; |
| 832 | if (fdb->is_static) { |
| 833 | fdb->is_static = 0; |
Jiri Pirko | 020ec6b | 2014-11-28 14:34:12 +0100 | [diff] [blame] | 834 | fdb_del_hw_addr(br, addr); |
Vlad Yasevich | 145beee | 2014-05-16 09:59:19 -0400 | [diff] [blame] | 835 | } |
| 836 | } |
stephen hemminger | 292d139 | 2011-11-09 18:30:08 +0000 | [diff] [blame] | 837 | |
roopa | b0a397f | 2013-04-22 12:56:49 +0000 | [diff] [blame] | 838 | modified = true; |
| 839 | } |
Toshiaki Makita | a5642ab | 2014-02-07 16:48:18 +0900 | [diff] [blame] | 840 | fdb->added_by_user = 1; |
roopa | b0a397f | 2013-04-22 12:56:49 +0000 | [diff] [blame] | 841 | |
| 842 | fdb->used = jiffies; |
| 843 | if (modified) { |
| 844 | fdb->updated = jiffies; |
stephen hemminger | 31e8a49c | 2011-12-08 07:17:41 +0000 | [diff] [blame] | 845 | fdb_notify(br, fdb, RTM_NEWNEIGH); |
stephen hemminger | 292d139 | 2011-11-09 18:30:08 +0000 | [diff] [blame] | 846 | } |
| 847 | |
stephen hemminger | 36fd2b6 | 2011-04-04 14:03:31 +0000 | [diff] [blame] | 848 | return 0; |
| 849 | } |
| 850 | |
Toshiaki Makita | 7bb90c3 | 2016-08-04 11:11:19 +0900 | [diff] [blame] | 851 | static int __br_fdb_add(struct ndmsg *ndm, struct net_bridge *br, |
| 852 | struct net_bridge_port *p, const unsigned char *addr, |
| 853 | u16 nlh_flags, u16 vid) |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 854 | { |
| 855 | int err = 0; |
| 856 | |
| 857 | if (ndm->ndm_flags & NTF_USE) { |
Toshiaki Makita | 7bb90c3 | 2016-08-04 11:11:19 +0900 | [diff] [blame] | 858 | if (!p) { |
| 859 | pr_info("bridge: RTM_NEWNEIGH %s with NTF_USE is not supported\n", |
| 860 | br->dev->name); |
| 861 | return -EINVAL; |
| 862 | } |
Nikolay Aleksandrov | c4c832f | 2015-06-06 06:49:00 -0700 | [diff] [blame] | 863 | local_bh_disable(); |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 864 | rcu_read_lock(); |
Toshiaki Makita | 7bb90c3 | 2016-08-04 11:11:19 +0900 | [diff] [blame] | 865 | br_fdb_update(br, p, addr, vid, true); |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 866 | rcu_read_unlock(); |
Nikolay Aleksandrov | c4c832f | 2015-06-06 06:49:00 -0700 | [diff] [blame] | 867 | local_bh_enable(); |
Nikolay Aleksandrov | eb100e0 | 2017-03-23 12:27:13 +0200 | [diff] [blame] | 868 | } else if (ndm->ndm_flags & NTF_EXT_LEARNED) { |
| 869 | err = br_fdb_external_learn_add(br, p, addr, vid); |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 870 | } else { |
Toshiaki Makita | 7bb90c3 | 2016-08-04 11:11:19 +0900 | [diff] [blame] | 871 | spin_lock_bh(&br->hash_lock); |
| 872 | err = fdb_add_entry(br, p, addr, ndm->ndm_state, |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 873 | nlh_flags, vid); |
Toshiaki Makita | 7bb90c3 | 2016-08-04 11:11:19 +0900 | [diff] [blame] | 874 | spin_unlock_bh(&br->hash_lock); |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 875 | } |
| 876 | |
| 877 | return err; |
| 878 | } |
| 879 | |
stephen hemminger | 36fd2b6 | 2011-04-04 14:03:31 +0000 | [diff] [blame] | 880 | /* Add new permanent fdb entry with RTM_NEWNEIGH */ |
stephen hemminger | edc7d57 | 2012-10-01 12:32:33 +0000 | [diff] [blame] | 881 | int br_fdb_add(struct ndmsg *ndm, struct nlattr *tb[], |
| 882 | struct net_device *dev, |
Jiri Pirko | f6f6424 | 2014-11-28 14:34:15 +0100 | [diff] [blame] | 883 | const unsigned char *addr, u16 vid, u16 nlh_flags) |
stephen hemminger | 36fd2b6 | 2011-04-04 14:03:31 +0000 | [diff] [blame] | 884 | { |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 885 | struct net_bridge_vlan_group *vg; |
Roopa Prabhu | 3741873 | 2015-10-08 10:38:52 -0700 | [diff] [blame] | 886 | struct net_bridge_port *p = NULL; |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 887 | struct net_bridge_vlan *v; |
Roopa Prabhu | 3741873 | 2015-10-08 10:38:52 -0700 | [diff] [blame] | 888 | struct net_bridge *br = NULL; |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 889 | int err = 0; |
stephen hemminger | 36fd2b6 | 2011-04-04 14:03:31 +0000 | [diff] [blame] | 890 | |
stephen hemminger | 292d139 | 2011-11-09 18:30:08 +0000 | [diff] [blame] | 891 | if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_NOARP|NUD_REACHABLE))) { |
| 892 | pr_info("bridge: RTM_NEWNEIGH with invalid state %#x\n", ndm->ndm_state); |
| 893 | return -EINVAL; |
| 894 | } |
| 895 | |
Stephen Hemminger | 537f7f8 | 2013-06-25 09:34:36 -0700 | [diff] [blame] | 896 | if (is_zero_ether_addr(addr)) { |
| 897 | pr_info("bridge: RTM_NEWNEIGH with invalid ether address\n"); |
| 898 | return -EINVAL; |
| 899 | } |
| 900 | |
Roopa Prabhu | 3741873 | 2015-10-08 10:38:52 -0700 | [diff] [blame] | 901 | if (dev->priv_flags & IFF_EBRIDGE) { |
| 902 | br = netdev_priv(dev); |
| 903 | vg = br_vlan_group(br); |
| 904 | } else { |
| 905 | p = br_port_get_rtnl(dev); |
| 906 | if (!p) { |
| 907 | pr_info("bridge: RTM_NEWNEIGH %s not a bridge port\n", |
| 908 | dev->name); |
| 909 | return -EINVAL; |
| 910 | } |
Toshiaki Makita | 7bb90c3 | 2016-08-04 11:11:19 +0900 | [diff] [blame] | 911 | br = p->br; |
Roopa Prabhu | 3741873 | 2015-10-08 10:38:52 -0700 | [diff] [blame] | 912 | vg = nbp_vlan_group(p); |
stephen hemminger | 36fd2b6 | 2011-04-04 14:03:31 +0000 | [diff] [blame] | 913 | } |
| 914 | |
Jiri Pirko | f6f6424 | 2014-11-28 14:34:15 +0100 | [diff] [blame] | 915 | if (vid) { |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 916 | v = br_vlan_find(vg, vid); |
Roopa Prabhu | 3741873 | 2015-10-08 10:38:52 -0700 | [diff] [blame] | 917 | if (!v || !br_vlan_should_use(v)) { |
| 918 | pr_info("bridge: RTM_NEWNEIGH with unconfigured vlan %d on %s\n", vid, dev->name); |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 919 | return -EINVAL; |
| 920 | } |
| 921 | |
| 922 | /* VID was specified, so use it. */ |
Toshiaki Makita | 7bb90c3 | 2016-08-04 11:11:19 +0900 | [diff] [blame] | 923 | err = __br_fdb_add(ndm, br, p, addr, nlh_flags, vid); |
stephen hemminger | 292d139 | 2011-11-09 18:30:08 +0000 | [diff] [blame] | 924 | } else { |
Toshiaki Makita | 7bb90c3 | 2016-08-04 11:11:19 +0900 | [diff] [blame] | 925 | err = __br_fdb_add(ndm, br, p, addr, nlh_flags, 0); |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 926 | if (err || !vg || !vg->num_vlans) |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 927 | goto out; |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 928 | |
| 929 | /* We have vlans configured on this port and user didn't |
| 930 | * specify a VLAN. To be nice, add/update entry for every |
| 931 | * vlan on this port. |
| 932 | */ |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 933 | list_for_each_entry(v, &vg->vlan_list, vlist) { |
Roopa Prabhu | 3741873 | 2015-10-08 10:38:52 -0700 | [diff] [blame] | 934 | if (!br_vlan_should_use(v)) |
| 935 | continue; |
Toshiaki Makita | 7bb90c3 | 2016-08-04 11:11:19 +0900 | [diff] [blame] | 936 | err = __br_fdb_add(ndm, br, p, addr, nlh_flags, v->vid); |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 937 | if (err) |
| 938 | goto out; |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 939 | } |
stephen hemminger | 292d139 | 2011-11-09 18:30:08 +0000 | [diff] [blame] | 940 | } |
stephen hemminger | 36fd2b6 | 2011-04-04 14:03:31 +0000 | [diff] [blame] | 941 | |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 942 | out: |
| 943 | return err; |
| 944 | } |
| 945 | |
Nikolay Aleksandrov | 5019ab5 | 2017-02-13 14:59:11 +0100 | [diff] [blame] | 946 | static int fdb_delete_by_addr_and_port(struct net_bridge *br, |
| 947 | const struct net_bridge_port *p, |
Nikolay Aleksandrov | 8c86f96 | 2015-06-09 03:34:13 -0700 | [diff] [blame] | 948 | const u8 *addr, u16 vlan) |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 949 | { |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 950 | struct net_bridge_fdb_entry *fdb; |
| 951 | |
Nikolay Aleksandrov | bfd0aea | 2017-02-13 14:59:09 +0100 | [diff] [blame] | 952 | fdb = br_fdb_find(br, addr, vlan); |
Nikolay Aleksandrov | 8c86f96 | 2015-06-09 03:34:13 -0700 | [diff] [blame] | 953 | if (!fdb || fdb->dst != p) |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 954 | return -ENOENT; |
| 955 | |
| 956 | fdb_delete(br, fdb); |
Nikolay Aleksandrov | 5019ab5 | 2017-02-13 14:59:11 +0100 | [diff] [blame] | 957 | |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 958 | return 0; |
| 959 | } |
| 960 | |
Nikolay Aleksandrov | 5019ab5 | 2017-02-13 14:59:11 +0100 | [diff] [blame] | 961 | static int __br_fdb_delete(struct net_bridge *br, |
| 962 | const struct net_bridge_port *p, |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 963 | const unsigned char *addr, u16 vid) |
| 964 | { |
| 965 | int err; |
| 966 | |
Nikolay Aleksandrov | 5019ab5 | 2017-02-13 14:59:11 +0100 | [diff] [blame] | 967 | spin_lock_bh(&br->hash_lock); |
| 968 | err = fdb_delete_by_addr_and_port(br, p, addr, vid); |
| 969 | spin_unlock_bh(&br->hash_lock); |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 970 | |
stephen hemminger | 36fd2b6 | 2011-04-04 14:03:31 +0000 | [diff] [blame] | 971 | return err; |
| 972 | } |
| 973 | |
stephen hemminger | 36fd2b6 | 2011-04-04 14:03:31 +0000 | [diff] [blame] | 974 | /* Remove neighbor entry with RTM_DELNEIGH */ |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 975 | int br_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[], |
| 976 | struct net_device *dev, |
Jiri Pirko | f6f6424 | 2014-11-28 14:34:15 +0100 | [diff] [blame] | 977 | const unsigned char *addr, u16 vid) |
stephen hemminger | 36fd2b6 | 2011-04-04 14:03:31 +0000 | [diff] [blame] | 978 | { |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 979 | struct net_bridge_vlan_group *vg; |
Roopa Prabhu | 3741873 | 2015-10-08 10:38:52 -0700 | [diff] [blame] | 980 | struct net_bridge_port *p = NULL; |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 981 | struct net_bridge_vlan *v; |
Nikolay Aleksandrov | 5019ab5 | 2017-02-13 14:59:11 +0100 | [diff] [blame] | 982 | struct net_bridge *br; |
stephen hemminger | 36fd2b6 | 2011-04-04 14:03:31 +0000 | [diff] [blame] | 983 | int err; |
| 984 | |
Roopa Prabhu | 3741873 | 2015-10-08 10:38:52 -0700 | [diff] [blame] | 985 | if (dev->priv_flags & IFF_EBRIDGE) { |
| 986 | br = netdev_priv(dev); |
| 987 | vg = br_vlan_group(br); |
| 988 | } else { |
| 989 | p = br_port_get_rtnl(dev); |
| 990 | if (!p) { |
| 991 | pr_info("bridge: RTM_DELNEIGH %s not a bridge port\n", |
| 992 | dev->name); |
| 993 | return -EINVAL; |
| 994 | } |
| 995 | vg = nbp_vlan_group(p); |
Nikolay Aleksandrov | 5019ab5 | 2017-02-13 14:59:11 +0100 | [diff] [blame] | 996 | br = p->br; |
stephen hemminger | 36fd2b6 | 2011-04-04 14:03:31 +0000 | [diff] [blame] | 997 | } |
| 998 | |
Jiri Pirko | f6f6424 | 2014-11-28 14:34:15 +0100 | [diff] [blame] | 999 | if (vid) { |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 1000 | v = br_vlan_find(vg, vid); |
| 1001 | if (!v) { |
Roopa Prabhu | 3741873 | 2015-10-08 10:38:52 -0700 | [diff] [blame] | 1002 | pr_info("bridge: RTM_DELNEIGH with unconfigured vlan %d on %s\n", vid, dev->name); |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 1003 | return -EINVAL; |
| 1004 | } |
stephen hemminger | 36fd2b6 | 2011-04-04 14:03:31 +0000 | [diff] [blame] | 1005 | |
Nikolay Aleksandrov | 5019ab5 | 2017-02-13 14:59:11 +0100 | [diff] [blame] | 1006 | err = __br_fdb_delete(br, p, addr, vid); |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 1007 | } else { |
Toshiaki Makita | 25d3b49 | 2015-02-09 20:16:17 +0900 | [diff] [blame] | 1008 | err = -ENOENT; |
Nikolay Aleksandrov | 5019ab5 | 2017-02-13 14:59:11 +0100 | [diff] [blame] | 1009 | err &= __br_fdb_delete(br, p, addr, 0); |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 1010 | if (!vg || !vg->num_vlans) |
Nikolay Aleksandrov | 5019ab5 | 2017-02-13 14:59:11 +0100 | [diff] [blame] | 1011 | return err; |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 1012 | |
Roopa Prabhu | 3741873 | 2015-10-08 10:38:52 -0700 | [diff] [blame] | 1013 | list_for_each_entry(v, &vg->vlan_list, vlist) { |
| 1014 | if (!br_vlan_should_use(v)) |
| 1015 | continue; |
Nikolay Aleksandrov | 5019ab5 | 2017-02-13 14:59:11 +0100 | [diff] [blame] | 1016 | err &= __br_fdb_delete(br, p, addr, v->vid); |
Roopa Prabhu | 3741873 | 2015-10-08 10:38:52 -0700 | [diff] [blame] | 1017 | } |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 1018 | } |
Nikolay Aleksandrov | 5019ab5 | 2017-02-13 14:59:11 +0100 | [diff] [blame] | 1019 | |
stephen hemminger | 36fd2b6 | 2011-04-04 14:03:31 +0000 | [diff] [blame] | 1020 | return err; |
| 1021 | } |
Vlad Yasevich | 8db24af | 2014-05-16 09:59:17 -0400 | [diff] [blame] | 1022 | |
| 1023 | int br_fdb_sync_static(struct net_bridge *br, struct net_bridge_port *p) |
| 1024 | { |
| 1025 | struct net_bridge_fdb_entry *fdb, *tmp; |
| 1026 | int i; |
| 1027 | int err; |
| 1028 | |
| 1029 | ASSERT_RTNL(); |
| 1030 | |
| 1031 | for (i = 0; i < BR_HASH_SIZE; i++) { |
| 1032 | hlist_for_each_entry(fdb, &br->hash[i], hlist) { |
| 1033 | /* We only care for static entries */ |
| 1034 | if (!fdb->is_static) |
| 1035 | continue; |
| 1036 | |
| 1037 | err = dev_uc_add(p->dev, fdb->addr.addr); |
| 1038 | if (err) |
| 1039 | goto rollback; |
| 1040 | } |
| 1041 | } |
| 1042 | return 0; |
| 1043 | |
| 1044 | rollback: |
| 1045 | for (i = 0; i < BR_HASH_SIZE; i++) { |
| 1046 | hlist_for_each_entry(tmp, &br->hash[i], hlist) { |
| 1047 | /* If we reached the fdb that failed, we can stop */ |
| 1048 | if (tmp == fdb) |
| 1049 | break; |
| 1050 | |
| 1051 | /* We only care for static entries */ |
| 1052 | if (!tmp->is_static) |
| 1053 | continue; |
| 1054 | |
| 1055 | dev_uc_del(p->dev, tmp->addr.addr); |
| 1056 | } |
| 1057 | } |
| 1058 | return err; |
| 1059 | } |
| 1060 | |
| 1061 | void br_fdb_unsync_static(struct net_bridge *br, struct net_bridge_port *p) |
| 1062 | { |
| 1063 | struct net_bridge_fdb_entry *fdb; |
| 1064 | int i; |
| 1065 | |
| 1066 | ASSERT_RTNL(); |
| 1067 | |
| 1068 | for (i = 0; i < BR_HASH_SIZE; i++) { |
| 1069 | hlist_for_each_entry_rcu(fdb, &br->hash[i], hlist) { |
| 1070 | /* We only care for static entries */ |
| 1071 | if (!fdb->is_static) |
| 1072 | continue; |
| 1073 | |
| 1074 | dev_uc_del(p->dev, fdb->addr.addr); |
| 1075 | } |
| 1076 | } |
| 1077 | } |
Scott Feldman | cf6b8e1 | 2014-11-28 14:34:21 +0100 | [diff] [blame] | 1078 | |
Jiri Pirko | 3aeb661 | 2015-01-15 23:49:37 +0100 | [diff] [blame] | 1079 | int br_fdb_external_learn_add(struct net_bridge *br, struct net_bridge_port *p, |
Scott Feldman | cf6b8e1 | 2014-11-28 14:34:21 +0100 | [diff] [blame] | 1080 | const unsigned char *addr, u16 vid) |
| 1081 | { |
Scott Feldman | cf6b8e1 | 2014-11-28 14:34:21 +0100 | [diff] [blame] | 1082 | struct hlist_head *head; |
| 1083 | struct net_bridge_fdb_entry *fdb; |
| 1084 | int err = 0; |
| 1085 | |
Scott Feldman | cf6b8e1 | 2014-11-28 14:34:21 +0100 | [diff] [blame] | 1086 | spin_lock_bh(&br->hash_lock); |
| 1087 | |
| 1088 | head = &br->hash[br_mac_hash(addr, vid)]; |
Nikolay Aleksandrov | bfd0aea | 2017-02-13 14:59:09 +0100 | [diff] [blame] | 1089 | fdb = br_fdb_find(br, addr, vid); |
Scott Feldman | cf6b8e1 | 2014-11-28 14:34:21 +0100 | [diff] [blame] | 1090 | if (!fdb) { |
Roopa Prabhu | b7af147 | 2015-10-27 07:52:56 -0700 | [diff] [blame] | 1091 | fdb = fdb_create(head, p, addr, vid, 0, 0); |
Scott Feldman | cf6b8e1 | 2014-11-28 14:34:21 +0100 | [diff] [blame] | 1092 | if (!fdb) { |
| 1093 | err = -ENOMEM; |
| 1094 | goto err_unlock; |
| 1095 | } |
| 1096 | fdb->added_by_external_learn = 1; |
| 1097 | fdb_notify(br, fdb, RTM_NEWNEIGH); |
| 1098 | } else if (fdb->added_by_external_learn) { |
| 1099 | /* Refresh entry */ |
| 1100 | fdb->updated = fdb->used = jiffies; |
| 1101 | } else if (!fdb->added_by_user) { |
| 1102 | /* Take over SW learned entry */ |
| 1103 | fdb->added_by_external_learn = 1; |
| 1104 | fdb->updated = jiffies; |
| 1105 | fdb_notify(br, fdb, RTM_NEWNEIGH); |
| 1106 | } |
| 1107 | |
| 1108 | err_unlock: |
| 1109 | spin_unlock_bh(&br->hash_lock); |
Scott Feldman | cf6b8e1 | 2014-11-28 14:34:21 +0100 | [diff] [blame] | 1110 | |
| 1111 | return err; |
| 1112 | } |
Scott Feldman | cf6b8e1 | 2014-11-28 14:34:21 +0100 | [diff] [blame] | 1113 | |
Jiri Pirko | 3aeb661 | 2015-01-15 23:49:37 +0100 | [diff] [blame] | 1114 | int br_fdb_external_learn_del(struct net_bridge *br, struct net_bridge_port *p, |
Scott Feldman | cf6b8e1 | 2014-11-28 14:34:21 +0100 | [diff] [blame] | 1115 | const unsigned char *addr, u16 vid) |
| 1116 | { |
Scott Feldman | cf6b8e1 | 2014-11-28 14:34:21 +0100 | [diff] [blame] | 1117 | struct net_bridge_fdb_entry *fdb; |
| 1118 | int err = 0; |
| 1119 | |
Scott Feldman | cf6b8e1 | 2014-11-28 14:34:21 +0100 | [diff] [blame] | 1120 | spin_lock_bh(&br->hash_lock); |
| 1121 | |
Nikolay Aleksandrov | bfd0aea | 2017-02-13 14:59:09 +0100 | [diff] [blame] | 1122 | fdb = br_fdb_find(br, addr, vid); |
Scott Feldman | cf6b8e1 | 2014-11-28 14:34:21 +0100 | [diff] [blame] | 1123 | if (fdb && fdb->added_by_external_learn) |
| 1124 | fdb_delete(br, fdb); |
| 1125 | else |
| 1126 | err = -ENOENT; |
| 1127 | |
| 1128 | spin_unlock_bh(&br->hash_lock); |
Scott Feldman | cf6b8e1 | 2014-11-28 14:34:21 +0100 | [diff] [blame] | 1129 | |
| 1130 | return err; |
| 1131 | } |
Arkadi Sharshevsky | 9fe8bce | 2017-06-08 08:44:15 +0200 | [diff] [blame^] | 1132 | |
| 1133 | void br_fdb_offloaded_set(struct net_bridge *br, struct net_bridge_port *p, |
| 1134 | const unsigned char *addr, u16 vid) |
| 1135 | { |
| 1136 | struct net_bridge_fdb_entry *fdb; |
| 1137 | |
| 1138 | spin_lock_bh(&br->hash_lock); |
| 1139 | |
| 1140 | fdb = br_fdb_find(br, addr, vid); |
| 1141 | if (fdb) |
| 1142 | fdb->offloaded = 1; |
| 1143 | |
| 1144 | spin_unlock_bh(&br->hash_lock); |
| 1145 | } |