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