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); |
| 107 | if (f->dst == p && f->is_local) { |
| 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; |
| 250 | goto skip_delete; |
| 251 | } |
| 252 | } |
| 253 | } |
| 254 | |
stephen hemminger | 31e8a49c | 2011-12-08 07:17:41 +0000 | [diff] [blame] | 255 | fdb_delete(br, f); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | skip_delete: ; |
| 257 | } |
| 258 | } |
| 259 | spin_unlock_bh(&br->hash_lock); |
| 260 | } |
| 261 | |
stephen hemminger | eeaf61d | 2010-07-27 08:26:30 +0000 | [diff] [blame] | 262 | /* No locking or refcounting, assumes caller has rcu_read_lock */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | struct net_bridge_fdb_entry *__br_fdb_get(struct net_bridge *br, |
Vlad Yasevich | 2ba071e | 2013-02-13 12:00:16 +0000 | [diff] [blame] | 264 | const unsigned char *addr, |
| 265 | __u16 vid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | struct net_bridge_fdb_entry *fdb; |
| 268 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 269 | hlist_for_each_entry_rcu(fdb, |
Vlad Yasevich | 2ba071e | 2013-02-13 12:00:16 +0000 | [diff] [blame] | 270 | &br->hash[br_mac_hash(addr, vid)], hlist) { |
| 271 | if (ether_addr_equal(fdb->addr.addr, addr) && |
| 272 | fdb->vlan_id == vid) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | if (unlikely(has_expired(br, fdb))) |
| 274 | break; |
| 275 | return fdb; |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | return NULL; |
| 280 | } |
| 281 | |
Igor Maravić | e6373c4 | 2011-12-12 02:58:25 +0000 | [diff] [blame] | 282 | #if IS_ENABLED(CONFIG_ATM_LANE) |
Michał Mirosław | da67829 | 2009-06-05 05:35:28 +0000 | [diff] [blame] | 283 | /* Interface used by ATM LANE hook to test |
| 284 | * if an addr is on some other bridge port */ |
| 285 | int br_fdb_test_addr(struct net_device *dev, unsigned char *addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | { |
| 287 | struct net_bridge_fdb_entry *fdb; |
stephen hemminger | b5ed54e | 2010-11-15 06:38:13 +0000 | [diff] [blame] | 288 | struct net_bridge_port *port; |
Michał Mirosław | da67829 | 2009-06-05 05:35:28 +0000 | [diff] [blame] | 289 | int ret; |
| 290 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | rcu_read_lock(); |
stephen hemminger | b5ed54e | 2010-11-15 06:38:13 +0000 | [diff] [blame] | 292 | port = br_port_get_rcu(dev); |
| 293 | if (!port) |
| 294 | ret = 0; |
| 295 | else { |
Vlad Yasevich | 2ba071e | 2013-02-13 12:00:16 +0000 | [diff] [blame] | 296 | fdb = __br_fdb_get(port->br, addr, 0); |
stephen hemminger | 4359881 | 2011-12-08 07:17:49 +0000 | [diff] [blame] | 297 | ret = fdb && fdb->dst && fdb->dst->dev != dev && |
stephen hemminger | b5ed54e | 2010-11-15 06:38:13 +0000 | [diff] [blame] | 298 | fdb->dst->state == BR_STATE_FORWARDING; |
| 299 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | |
Michał Mirosław | da67829 | 2009-06-05 05:35:28 +0000 | [diff] [blame] | 302 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | } |
Michał Mirosław | da67829 | 2009-06-05 05:35:28 +0000 | [diff] [blame] | 304 | #endif /* CONFIG_ATM_LANE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | |
| 306 | /* |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 307 | * Fill buffer with forwarding table records in |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | * the API format. |
| 309 | */ |
| 310 | int br_fdb_fillbuf(struct net_bridge *br, void *buf, |
| 311 | unsigned long maxnum, unsigned long skip) |
| 312 | { |
| 313 | struct __fdb_entry *fe = buf; |
| 314 | int i, num = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | struct net_bridge_fdb_entry *f; |
| 316 | |
| 317 | memset(buf, 0, maxnum*sizeof(struct __fdb_entry)); |
| 318 | |
| 319 | rcu_read_lock(); |
| 320 | for (i = 0; i < BR_HASH_SIZE; i++) { |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 321 | hlist_for_each_entry_rcu(f, &br->hash[i], hlist) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | if (num >= maxnum) |
| 323 | goto out; |
| 324 | |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 325 | if (has_expired(br, f)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | continue; |
| 327 | |
stephen hemminger | 4359881 | 2011-12-08 07:17:49 +0000 | [diff] [blame] | 328 | /* ignore pseudo entry for local MAC address */ |
| 329 | if (!f->dst) |
| 330 | continue; |
| 331 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | if (skip) { |
| 333 | --skip; |
| 334 | continue; |
| 335 | } |
| 336 | |
| 337 | /* convert from internal format to API */ |
| 338 | memcpy(fe->mac_addr, f->addr.addr, ETH_ALEN); |
Stephen Hemminger | ae4f8fc | 2008-05-02 16:53:33 -0700 | [diff] [blame] | 339 | |
| 340 | /* due to ABI compat need to split into hi/lo */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | fe->port_no = f->dst->port_no; |
Stephen Hemminger | ae4f8fc | 2008-05-02 16:53:33 -0700 | [diff] [blame] | 342 | fe->port_hi = f->dst->port_no >> 8; |
| 343 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | fe->is_local = f->is_local; |
| 345 | if (!f->is_static) |
Eric Dumazet | a399a80 | 2012-08-08 21:13:53 +0000 | [diff] [blame] | 346 | fe->ageing_timer_value = jiffies_delta_to_clock_t(jiffies - f->updated); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | ++fe; |
| 348 | ++num; |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | out: |
| 353 | rcu_read_unlock(); |
| 354 | |
| 355 | return num; |
| 356 | } |
| 357 | |
stephen hemminger | 664de48 | 2011-04-04 14:03:29 +0000 | [diff] [blame] | 358 | static struct net_bridge_fdb_entry *fdb_find(struct hlist_head *head, |
Vlad Yasevich | 2ba071e | 2013-02-13 12:00:16 +0000 | [diff] [blame] | 359 | const unsigned char *addr, |
| 360 | __u16 vid) |
stephen hemminger | 664de48 | 2011-04-04 14:03:29 +0000 | [diff] [blame] | 361 | { |
stephen hemminger | 664de48 | 2011-04-04 14:03:29 +0000 | [diff] [blame] | 362 | struct net_bridge_fdb_entry *fdb; |
| 363 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 364 | hlist_for_each_entry(fdb, head, hlist) { |
Vlad Yasevich | 2ba071e | 2013-02-13 12:00:16 +0000 | [diff] [blame] | 365 | if (ether_addr_equal(fdb->addr.addr, addr) && |
| 366 | fdb->vlan_id == vid) |
stephen hemminger | 664de48 | 2011-04-04 14:03:29 +0000 | [diff] [blame] | 367 | return fdb; |
| 368 | } |
| 369 | return NULL; |
| 370 | } |
| 371 | |
| 372 | 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] | 373 | const unsigned char *addr, |
| 374 | __u16 vid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | struct net_bridge_fdb_entry *fdb; |
| 377 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 378 | hlist_for_each_entry_rcu(fdb, head, hlist) { |
Vlad Yasevich | 2ba071e | 2013-02-13 12:00:16 +0000 | [diff] [blame] | 379 | if (ether_addr_equal(fdb->addr.addr, addr) && |
| 380 | fdb->vlan_id == vid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | return fdb; |
| 382 | } |
| 383 | return NULL; |
| 384 | } |
| 385 | |
| 386 | static struct net_bridge_fdb_entry *fdb_create(struct hlist_head *head, |
| 387 | struct net_bridge_port *source, |
Vlad Yasevich | 2ba071e | 2013-02-13 12:00:16 +0000 | [diff] [blame] | 388 | const unsigned char *addr, |
| 389 | __u16 vid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | { |
| 391 | struct net_bridge_fdb_entry *fdb; |
| 392 | |
| 393 | fdb = kmem_cache_alloc(br_fdb_cache, GFP_ATOMIC); |
| 394 | if (fdb) { |
| 395 | memcpy(fdb->addr.addr, addr, ETH_ALEN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | fdb->dst = source; |
Vlad Yasevich | 2ba071e | 2013-02-13 12:00:16 +0000 | [diff] [blame] | 397 | fdb->vlan_id = vid; |
stephen hemminger | 03e9b64 | 2011-04-04 14:03:27 +0000 | [diff] [blame] | 398 | fdb->is_local = 0; |
| 399 | fdb->is_static = 0; |
stephen hemminger | 7cd8861 | 2011-04-04 14:03:28 +0000 | [diff] [blame] | 400 | fdb->updated = fdb->used = jiffies; |
Pavel Emelyanov | 1158f76 | 2011-02-04 13:02:36 -0800 | [diff] [blame] | 401 | hlist_add_head_rcu(&fdb->hlist, head); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | } |
| 403 | return fdb; |
| 404 | } |
| 405 | |
| 406 | 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] | 407 | const unsigned char *addr, u16 vid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | { |
Vlad Yasevich | bc9a25d | 2013-02-13 12:00:19 +0000 | [diff] [blame] | 409 | struct hlist_head *head = &br->hash[br_mac_hash(addr, vid)]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | struct net_bridge_fdb_entry *fdb; |
| 411 | |
| 412 | if (!is_valid_ether_addr(addr)) |
| 413 | return -EINVAL; |
| 414 | |
Vlad Yasevich | bc9a25d | 2013-02-13 12:00:19 +0000 | [diff] [blame] | 415 | fdb = fdb_find(head, addr, vid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | if (fdb) { |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 417 | /* it is okay to have multiple ports with same |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | * address, just use the first one. |
| 419 | */ |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 420 | if (fdb->is_local) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | return 0; |
stephen hemminger | 28a16c9 | 2010-05-10 09:31:09 +0000 | [diff] [blame] | 422 | br_warn(br, "adding interface %s with same address " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | "as a received packet\n", |
Hong zhi guo | 9b46922 | 2013-03-23 02:27:50 +0000 | [diff] [blame] | 424 | source ? source->dev->name : br->dev->name); |
stephen hemminger | 31e8a49c | 2011-12-08 07:17:41 +0000 | [diff] [blame] | 425 | fdb_delete(br, fdb); |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 426 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | |
Vlad Yasevich | bc9a25d | 2013-02-13 12:00:19 +0000 | [diff] [blame] | 428 | fdb = fdb_create(head, source, addr, vid); |
stephen hemminger | 03e9b64 | 2011-04-04 14:03:27 +0000 | [diff] [blame] | 429 | if (!fdb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | return -ENOMEM; |
| 431 | |
stephen hemminger | 03e9b64 | 2011-04-04 14:03:27 +0000 | [diff] [blame] | 432 | fdb->is_local = fdb->is_static = 1; |
stephen hemminger | 31e8a49c | 2011-12-08 07:17:41 +0000 | [diff] [blame] | 433 | fdb_notify(br, fdb, RTM_NEWNEIGH); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | return 0; |
| 435 | } |
| 436 | |
stephen hemminger | 03e9b64 | 2011-04-04 14:03:27 +0000 | [diff] [blame] | 437 | /* Add entry for local address of interface */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | 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] | 439 | const unsigned char *addr, u16 vid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | { |
| 441 | int ret; |
| 442 | |
| 443 | spin_lock_bh(&br->hash_lock); |
Vlad Yasevich | bc9a25d | 2013-02-13 12:00:19 +0000 | [diff] [blame] | 444 | ret = fdb_insert(br, source, addr, vid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | spin_unlock_bh(&br->hash_lock); |
| 446 | return ret; |
| 447 | } |
| 448 | |
| 449 | void br_fdb_update(struct net_bridge *br, struct net_bridge_port *source, |
Vlad Yasevich | 2ba071e | 2013-02-13 12:00:16 +0000 | [diff] [blame] | 450 | const unsigned char *addr, u16 vid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | { |
Vlad Yasevich | 2ba071e | 2013-02-13 12:00:16 +0000 | [diff] [blame] | 452 | struct hlist_head *head = &br->hash[br_mac_hash(addr, vid)]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | struct net_bridge_fdb_entry *fdb; |
| 454 | |
| 455 | /* some users want to always flood. */ |
| 456 | if (hold_time(br) == 0) |
| 457 | return; |
| 458 | |
Stephen Hemminger | df1c0b8 | 2007-08-30 22:15:35 -0700 | [diff] [blame] | 459 | /* ignore packets unless we are using this port */ |
| 460 | if (!(source->state == BR_STATE_LEARNING || |
| 461 | source->state == BR_STATE_FORWARDING)) |
| 462 | return; |
| 463 | |
Vlad Yasevich | 2ba071e | 2013-02-13 12:00:16 +0000 | [diff] [blame] | 464 | fdb = fdb_find_rcu(head, addr, vid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | if (likely(fdb)) { |
| 466 | /* attempt to update an entry for a local interface */ |
| 467 | if (unlikely(fdb->is_local)) { |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 468 | if (net_ratelimit()) |
stephen hemminger | 28a16c9 | 2010-05-10 09:31:09 +0000 | [diff] [blame] | 469 | br_warn(br, "received packet on %s with " |
| 470 | "own address as source address\n", |
| 471 | source->dev->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | } else { |
| 473 | /* fastpath: update of existing entry */ |
| 474 | fdb->dst = source; |
stephen hemminger | 7cd8861 | 2011-04-04 14:03:28 +0000 | [diff] [blame] | 475 | fdb->updated = jiffies; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | } |
| 477 | } else { |
Stephen Hemminger | f8ae737 | 2006-03-20 22:58:36 -0800 | [diff] [blame] | 478 | spin_lock(&br->hash_lock); |
Vlad Yasevich | 2ba071e | 2013-02-13 12:00:16 +0000 | [diff] [blame] | 479 | if (likely(!fdb_find(head, addr, vid))) { |
| 480 | fdb = fdb_create(head, source, addr, vid); |
stephen hemminger | f58ee4e | 2011-12-06 13:02:24 +0000 | [diff] [blame] | 481 | if (fdb) |
stephen hemminger | 31e8a49c | 2011-12-08 07:17:41 +0000 | [diff] [blame] | 482 | fdb_notify(br, fdb, RTM_NEWNEIGH); |
stephen hemminger | f58ee4e | 2011-12-06 13:02:24 +0000 | [diff] [blame] | 483 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | /* else we lose race and someone else inserts |
| 485 | * it first, don't bother updating |
| 486 | */ |
Stephen Hemminger | f8ae737 | 2006-03-20 22:58:36 -0800 | [diff] [blame] | 487 | spin_unlock(&br->hash_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | } |
stephen hemminger | b078f0d | 2011-04-04 14:03:30 +0000 | [diff] [blame] | 490 | |
| 491 | static int fdb_to_nud(const struct net_bridge_fdb_entry *fdb) |
| 492 | { |
| 493 | if (fdb->is_local) |
| 494 | return NUD_PERMANENT; |
| 495 | else if (fdb->is_static) |
| 496 | return NUD_NOARP; |
| 497 | else if (has_expired(fdb->dst->br, fdb)) |
| 498 | return NUD_STALE; |
| 499 | else |
| 500 | return NUD_REACHABLE; |
| 501 | } |
| 502 | |
stephen hemminger | 31e8a49c | 2011-12-08 07:17:41 +0000 | [diff] [blame] | 503 | 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] | 504 | const struct net_bridge_fdb_entry *fdb, |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 505 | u32 portid, u32 seq, int type, unsigned int flags) |
stephen hemminger | b078f0d | 2011-04-04 14:03:30 +0000 | [diff] [blame] | 506 | { |
| 507 | unsigned long now = jiffies; |
| 508 | struct nda_cacheinfo ci; |
| 509 | struct nlmsghdr *nlh; |
| 510 | struct ndmsg *ndm; |
| 511 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 512 | nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags); |
stephen hemminger | b078f0d | 2011-04-04 14:03:30 +0000 | [diff] [blame] | 513 | if (nlh == NULL) |
| 514 | return -EMSGSIZE; |
| 515 | |
stephen hemminger | b078f0d | 2011-04-04 14:03:30 +0000 | [diff] [blame] | 516 | ndm = nlmsg_data(nlh); |
| 517 | ndm->ndm_family = AF_BRIDGE; |
| 518 | ndm->ndm_pad1 = 0; |
| 519 | ndm->ndm_pad2 = 0; |
| 520 | ndm->ndm_flags = 0; |
| 521 | ndm->ndm_type = 0; |
stephen hemminger | 4359881 | 2011-12-08 07:17:49 +0000 | [diff] [blame] | 522 | ndm->ndm_ifindex = fdb->dst ? fdb->dst->dev->ifindex : br->dev->ifindex; |
stephen hemminger | b078f0d | 2011-04-04 14:03:30 +0000 | [diff] [blame] | 523 | ndm->ndm_state = fdb_to_nud(fdb); |
| 524 | |
David S. Miller | 2eb812e | 2012-04-01 20:49:54 -0400 | [diff] [blame] | 525 | if (nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->addr)) |
| 526 | goto nla_put_failure; |
stephen hemminger | b078f0d | 2011-04-04 14:03:30 +0000 | [diff] [blame] | 527 | ci.ndm_used = jiffies_to_clock_t(now - fdb->used); |
| 528 | ci.ndm_confirmed = 0; |
| 529 | ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated); |
| 530 | ci.ndm_refcnt = 0; |
David S. Miller | 2eb812e | 2012-04-01 20:49:54 -0400 | [diff] [blame] | 531 | if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci)) |
| 532 | goto nla_put_failure; |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 533 | |
| 534 | if (nla_put(skb, NDA_VLAN, sizeof(u16), &fdb->vlan_id)) |
| 535 | goto nla_put_failure; |
| 536 | |
stephen hemminger | b078f0d | 2011-04-04 14:03:30 +0000 | [diff] [blame] | 537 | return nlmsg_end(skb, nlh); |
| 538 | |
| 539 | nla_put_failure: |
| 540 | nlmsg_cancel(skb, nlh); |
| 541 | return -EMSGSIZE; |
| 542 | } |
| 543 | |
| 544 | static inline size_t fdb_nlmsg_size(void) |
| 545 | { |
| 546 | return NLMSG_ALIGN(sizeof(struct ndmsg)) |
| 547 | + nla_total_size(ETH_ALEN) /* NDA_LLADDR */ |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 548 | + nla_total_size(sizeof(u16)) /* NDA_VLAN */ |
stephen hemminger | b078f0d | 2011-04-04 14:03:30 +0000 | [diff] [blame] | 549 | + nla_total_size(sizeof(struct nda_cacheinfo)); |
| 550 | } |
| 551 | |
stephen hemminger | 31e8a49c | 2011-12-08 07:17:41 +0000 | [diff] [blame] | 552 | static void fdb_notify(struct net_bridge *br, |
| 553 | const struct net_bridge_fdb_entry *fdb, int type) |
stephen hemminger | b078f0d | 2011-04-04 14:03:30 +0000 | [diff] [blame] | 554 | { |
stephen hemminger | 31e8a49c | 2011-12-08 07:17:41 +0000 | [diff] [blame] | 555 | struct net *net = dev_net(br->dev); |
stephen hemminger | b078f0d | 2011-04-04 14:03:30 +0000 | [diff] [blame] | 556 | struct sk_buff *skb; |
| 557 | int err = -ENOBUFS; |
| 558 | |
| 559 | skb = nlmsg_new(fdb_nlmsg_size(), GFP_ATOMIC); |
| 560 | if (skb == NULL) |
| 561 | goto errout; |
| 562 | |
stephen hemminger | 31e8a49c | 2011-12-08 07:17:41 +0000 | [diff] [blame] | 563 | err = fdb_fill_info(skb, br, fdb, 0, 0, type, 0); |
stephen hemminger | b078f0d | 2011-04-04 14:03:30 +0000 | [diff] [blame] | 564 | if (err < 0) { |
| 565 | /* -EMSGSIZE implies BUG in fdb_nlmsg_size() */ |
| 566 | WARN_ON(err == -EMSGSIZE); |
| 567 | kfree_skb(skb); |
| 568 | goto errout; |
| 569 | } |
| 570 | rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC); |
| 571 | return; |
| 572 | errout: |
tanxiaojun | 87e823b | 2013-12-19 13:28:10 +0800 | [diff] [blame] | 573 | rtnl_set_sk_err(net, RTNLGRP_NEIGH, err); |
stephen hemminger | b078f0d | 2011-04-04 14:03:30 +0000 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | /* Dump information about entries, in response to GETNEIGH */ |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 577 | int br_fdb_dump(struct sk_buff *skb, |
| 578 | struct netlink_callback *cb, |
| 579 | struct net_device *dev, |
| 580 | int idx) |
stephen hemminger | b078f0d | 2011-04-04 14:03:30 +0000 | [diff] [blame] | 581 | { |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 582 | struct net_bridge *br = netdev_priv(dev); |
| 583 | int i; |
stephen hemminger | b078f0d | 2011-04-04 14:03:30 +0000 | [diff] [blame] | 584 | |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 585 | if (!(dev->priv_flags & IFF_EBRIDGE)) |
| 586 | goto out; |
stephen hemminger | b078f0d | 2011-04-04 14:03:30 +0000 | [diff] [blame] | 587 | |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 588 | for (i = 0; i < BR_HASH_SIZE; i++) { |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 589 | struct net_bridge_fdb_entry *f; |
stephen hemminger | b078f0d | 2011-04-04 14:03:30 +0000 | [diff] [blame] | 590 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 591 | hlist_for_each_entry_rcu(f, &br->hash[i], hlist) { |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 592 | if (idx < cb->args[0]) |
| 593 | goto skip; |
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 | if (fdb_fill_info(skb, br, f, |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 596 | NETLINK_CB(cb->skb).portid, |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 597 | cb->nlh->nlmsg_seq, |
| 598 | RTM_NEWNEIGH, |
| 599 | NLM_F_MULTI) < 0) |
| 600 | break; |
stephen hemminger | b078f0d | 2011-04-04 14:03:30 +0000 | [diff] [blame] | 601 | skip: |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 602 | ++idx; |
stephen hemminger | b078f0d | 2011-04-04 14:03:30 +0000 | [diff] [blame] | 603 | } |
| 604 | } |
stephen hemminger | b078f0d | 2011-04-04 14:03:30 +0000 | [diff] [blame] | 605 | |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 606 | out: |
| 607 | return idx; |
stephen hemminger | b078f0d | 2011-04-04 14:03:30 +0000 | [diff] [blame] | 608 | } |
stephen hemminger | 36fd2b6 | 2011-04-04 14:03:31 +0000 | [diff] [blame] | 609 | |
stephen hemminger | 292d139 | 2011-11-09 18:30:08 +0000 | [diff] [blame] | 610 | /* Update (create or replace) forwarding database entry */ |
stephen hemminger | 36fd2b6 | 2011-04-04 14:03:31 +0000 | [diff] [blame] | 611 | 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] | 612 | __u16 state, __u16 flags, __u16 vid) |
stephen hemminger | 36fd2b6 | 2011-04-04 14:03:31 +0000 | [diff] [blame] | 613 | { |
| 614 | struct net_bridge *br = source->br; |
Vlad Yasevich | 2ba071e | 2013-02-13 12:00:16 +0000 | [diff] [blame] | 615 | struct hlist_head *head = &br->hash[br_mac_hash(addr, vid)]; |
stephen hemminger | 36fd2b6 | 2011-04-04 14:03:31 +0000 | [diff] [blame] | 616 | struct net_bridge_fdb_entry *fdb; |
roopa | b0a397f | 2013-04-22 12:56:49 +0000 | [diff] [blame] | 617 | bool modified = false; |
stephen hemminger | 36fd2b6 | 2011-04-04 14:03:31 +0000 | [diff] [blame] | 618 | |
Vlad Yasevich | 2ba071e | 2013-02-13 12:00:16 +0000 | [diff] [blame] | 619 | fdb = fdb_find(head, addr, vid); |
stephen hemminger | 64af1ba | 2011-09-30 14:37:27 +0000 | [diff] [blame] | 620 | if (fdb == NULL) { |
| 621 | if (!(flags & NLM_F_CREATE)) |
| 622 | return -ENOENT; |
stephen hemminger | 36fd2b6 | 2011-04-04 14:03:31 +0000 | [diff] [blame] | 623 | |
Vlad Yasevich | 2ba071e | 2013-02-13 12:00:16 +0000 | [diff] [blame] | 624 | fdb = fdb_create(head, source, addr, vid); |
stephen hemminger | 64af1ba | 2011-09-30 14:37:27 +0000 | [diff] [blame] | 625 | if (!fdb) |
| 626 | return -ENOMEM; |
roopa | b0a397f | 2013-04-22 12:56:49 +0000 | [diff] [blame] | 627 | |
| 628 | modified = true; |
stephen hemminger | 64af1ba | 2011-09-30 14:37:27 +0000 | [diff] [blame] | 629 | } else { |
| 630 | if (flags & NLM_F_EXCL) |
| 631 | return -EEXIST; |
roopa | b0a397f | 2013-04-22 12:56:49 +0000 | [diff] [blame] | 632 | |
| 633 | if (fdb->dst != source) { |
| 634 | fdb->dst = source; |
| 635 | modified = true; |
| 636 | } |
stephen hemminger | 64af1ba | 2011-09-30 14:37:27 +0000 | [diff] [blame] | 637 | } |
stephen hemminger | 36fd2b6 | 2011-04-04 14:03:31 +0000 | [diff] [blame] | 638 | |
stephen hemminger | 292d139 | 2011-11-09 18:30:08 +0000 | [diff] [blame] | 639 | if (fdb_to_nud(fdb) != state) { |
| 640 | if (state & NUD_PERMANENT) |
| 641 | fdb->is_local = fdb->is_static = 1; |
| 642 | else if (state & NUD_NOARP) { |
| 643 | fdb->is_local = 0; |
| 644 | fdb->is_static = 1; |
| 645 | } else |
| 646 | fdb->is_local = fdb->is_static = 0; |
| 647 | |
roopa | b0a397f | 2013-04-22 12:56:49 +0000 | [diff] [blame] | 648 | modified = true; |
| 649 | } |
| 650 | |
| 651 | fdb->used = jiffies; |
| 652 | if (modified) { |
| 653 | fdb->updated = jiffies; |
stephen hemminger | 31e8a49c | 2011-12-08 07:17:41 +0000 | [diff] [blame] | 654 | fdb_notify(br, fdb, RTM_NEWNEIGH); |
stephen hemminger | 292d139 | 2011-11-09 18:30:08 +0000 | [diff] [blame] | 655 | } |
| 656 | |
stephen hemminger | 36fd2b6 | 2011-04-04 14:03:31 +0000 | [diff] [blame] | 657 | return 0; |
| 658 | } |
| 659 | |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 660 | static int __br_fdb_add(struct ndmsg *ndm, struct net_bridge_port *p, |
| 661 | const unsigned char *addr, u16 nlh_flags, u16 vid) |
| 662 | { |
| 663 | int err = 0; |
| 664 | |
| 665 | if (ndm->ndm_flags & NTF_USE) { |
| 666 | rcu_read_lock(); |
| 667 | br_fdb_update(p->br, p, addr, vid); |
| 668 | rcu_read_unlock(); |
| 669 | } else { |
| 670 | spin_lock_bh(&p->br->hash_lock); |
| 671 | err = fdb_add_entry(p, addr, ndm->ndm_state, |
| 672 | nlh_flags, vid); |
| 673 | spin_unlock_bh(&p->br->hash_lock); |
| 674 | } |
| 675 | |
| 676 | return err; |
| 677 | } |
| 678 | |
stephen hemminger | 36fd2b6 | 2011-04-04 14:03:31 +0000 | [diff] [blame] | 679 | /* Add new permanent fdb entry with RTM_NEWNEIGH */ |
stephen hemminger | edc7d57 | 2012-10-01 12:32:33 +0000 | [diff] [blame] | 680 | int br_fdb_add(struct ndmsg *ndm, struct nlattr *tb[], |
| 681 | struct net_device *dev, |
stephen hemminger | 6b6e272 | 2012-09-17 10:03:26 +0000 | [diff] [blame] | 682 | const unsigned char *addr, u16 nlh_flags) |
stephen hemminger | 36fd2b6 | 2011-04-04 14:03:31 +0000 | [diff] [blame] | 683 | { |
stephen hemminger | 36fd2b6 | 2011-04-04 14:03:31 +0000 | [diff] [blame] | 684 | struct net_bridge_port *p; |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 685 | int err = 0; |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 686 | struct net_port_vlans *pv; |
| 687 | unsigned short vid = VLAN_N_VID; |
stephen hemminger | 36fd2b6 | 2011-04-04 14:03:31 +0000 | [diff] [blame] | 688 | |
stephen hemminger | 292d139 | 2011-11-09 18:30:08 +0000 | [diff] [blame] | 689 | if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_NOARP|NUD_REACHABLE))) { |
| 690 | pr_info("bridge: RTM_NEWNEIGH with invalid state %#x\n", ndm->ndm_state); |
| 691 | return -EINVAL; |
| 692 | } |
| 693 | |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 694 | if (tb[NDA_VLAN]) { |
| 695 | if (nla_len(tb[NDA_VLAN]) != sizeof(unsigned short)) { |
| 696 | pr_info("bridge: RTM_NEWNEIGH with invalid vlan\n"); |
| 697 | return -EINVAL; |
| 698 | } |
| 699 | |
| 700 | vid = nla_get_u16(tb[NDA_VLAN]); |
| 701 | |
Toshiaki Makita | 8adff41 | 2013-10-16 17:07:13 +0900 | [diff] [blame] | 702 | if (!vid || vid >= VLAN_VID_MASK) { |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 703 | pr_info("bridge: RTM_NEWNEIGH with invalid vlan id %d\n", |
| 704 | vid); |
| 705 | return -EINVAL; |
| 706 | } |
| 707 | } |
| 708 | |
Stephen Hemminger | 537f7f8 | 2013-06-25 09:34:36 -0700 | [diff] [blame] | 709 | if (is_zero_ether_addr(addr)) { |
| 710 | pr_info("bridge: RTM_NEWNEIGH with invalid ether address\n"); |
| 711 | return -EINVAL; |
| 712 | } |
| 713 | |
stephen hemminger | 36fd2b6 | 2011-04-04 14:03:31 +0000 | [diff] [blame] | 714 | p = br_port_get_rtnl(dev); |
| 715 | if (p == NULL) { |
| 716 | pr_info("bridge: RTM_NEWNEIGH %s not a bridge port\n", |
| 717 | dev->name); |
| 718 | return -EINVAL; |
| 719 | } |
| 720 | |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 721 | pv = nbp_get_vlan_info(p); |
| 722 | if (vid != VLAN_N_VID) { |
| 723 | if (!pv || !test_bit(vid, pv->vlan_bitmap)) { |
| 724 | pr_info("bridge: RTM_NEWNEIGH with unconfigured " |
| 725 | "vlan %d on port %s\n", vid, dev->name); |
| 726 | return -EINVAL; |
| 727 | } |
| 728 | |
| 729 | /* VID was specified, so use it. */ |
| 730 | err = __br_fdb_add(ndm, p, addr, nlh_flags, vid); |
stephen hemminger | 292d139 | 2011-11-09 18:30:08 +0000 | [diff] [blame] | 731 | } else { |
Toshiaki Makita | ef40b7e | 2013-08-20 17:10:18 +0900 | [diff] [blame] | 732 | if (!pv || bitmap_empty(pv->vlan_bitmap, VLAN_N_VID)) { |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 733 | err = __br_fdb_add(ndm, p, addr, nlh_flags, 0); |
| 734 | goto out; |
| 735 | } |
| 736 | |
| 737 | /* We have vlans configured on this port and user didn't |
| 738 | * specify a VLAN. To be nice, add/update entry for every |
| 739 | * vlan on this port. |
| 740 | */ |
Toshiaki Makita | ef40b7e | 2013-08-20 17:10:18 +0900 | [diff] [blame] | 741 | for_each_set_bit(vid, pv->vlan_bitmap, VLAN_N_VID) { |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 742 | err = __br_fdb_add(ndm, p, addr, nlh_flags, vid); |
| 743 | if (err) |
| 744 | goto out; |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 745 | } |
stephen hemminger | 292d139 | 2011-11-09 18:30:08 +0000 | [diff] [blame] | 746 | } |
stephen hemminger | 36fd2b6 | 2011-04-04 14:03:31 +0000 | [diff] [blame] | 747 | |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 748 | out: |
| 749 | return err; |
| 750 | } |
| 751 | |
Vlad Yasevich | bc9a25d | 2013-02-13 12:00:19 +0000 | [diff] [blame] | 752 | int fdb_delete_by_addr(struct net_bridge *br, const u8 *addr, |
| 753 | u16 vlan) |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 754 | { |
| 755 | struct hlist_head *head = &br->hash[br_mac_hash(addr, vlan)]; |
| 756 | struct net_bridge_fdb_entry *fdb; |
| 757 | |
| 758 | fdb = fdb_find(head, addr, vlan); |
| 759 | if (!fdb) |
| 760 | return -ENOENT; |
| 761 | |
| 762 | fdb_delete(br, fdb); |
| 763 | return 0; |
| 764 | } |
| 765 | |
| 766 | static int __br_fdb_delete(struct net_bridge_port *p, |
| 767 | const unsigned char *addr, u16 vid) |
| 768 | { |
| 769 | int err; |
| 770 | |
| 771 | spin_lock_bh(&p->br->hash_lock); |
| 772 | err = fdb_delete_by_addr(p->br, addr, vid); |
| 773 | spin_unlock_bh(&p->br->hash_lock); |
| 774 | |
stephen hemminger | 36fd2b6 | 2011-04-04 14:03:31 +0000 | [diff] [blame] | 775 | return err; |
| 776 | } |
| 777 | |
stephen hemminger | 36fd2b6 | 2011-04-04 14:03:31 +0000 | [diff] [blame] | 778 | /* Remove neighbor entry with RTM_DELNEIGH */ |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 779 | int br_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[], |
| 780 | struct net_device *dev, |
stephen hemminger | 6b6e272 | 2012-09-17 10:03:26 +0000 | [diff] [blame] | 781 | const unsigned char *addr) |
stephen hemminger | 36fd2b6 | 2011-04-04 14:03:31 +0000 | [diff] [blame] | 782 | { |
stephen hemminger | 36fd2b6 | 2011-04-04 14:03:31 +0000 | [diff] [blame] | 783 | struct net_bridge_port *p; |
stephen hemminger | 36fd2b6 | 2011-04-04 14:03:31 +0000 | [diff] [blame] | 784 | int err; |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 785 | struct net_port_vlans *pv; |
| 786 | unsigned short vid = VLAN_N_VID; |
stephen hemminger | 36fd2b6 | 2011-04-04 14:03:31 +0000 | [diff] [blame] | 787 | |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 788 | if (tb[NDA_VLAN]) { |
| 789 | if (nla_len(tb[NDA_VLAN]) != sizeof(unsigned short)) { |
| 790 | pr_info("bridge: RTM_NEWNEIGH with invalid vlan\n"); |
| 791 | return -EINVAL; |
| 792 | } |
| 793 | |
| 794 | vid = nla_get_u16(tb[NDA_VLAN]); |
| 795 | |
Toshiaki Makita | 8adff41 | 2013-10-16 17:07:13 +0900 | [diff] [blame] | 796 | if (!vid || vid >= VLAN_VID_MASK) { |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 797 | pr_info("bridge: RTM_NEWNEIGH with invalid vlan id %d\n", |
| 798 | vid); |
| 799 | return -EINVAL; |
| 800 | } |
| 801 | } |
stephen hemminger | 36fd2b6 | 2011-04-04 14:03:31 +0000 | [diff] [blame] | 802 | p = br_port_get_rtnl(dev); |
| 803 | if (p == NULL) { |
| 804 | pr_info("bridge: RTM_DELNEIGH %s not a bridge port\n", |
| 805 | dev->name); |
| 806 | return -EINVAL; |
| 807 | } |
| 808 | |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 809 | pv = nbp_get_vlan_info(p); |
| 810 | if (vid != VLAN_N_VID) { |
| 811 | if (!pv || !test_bit(vid, pv->vlan_bitmap)) { |
| 812 | pr_info("bridge: RTM_DELNEIGH with unconfigured " |
| 813 | "vlan %d on port %s\n", vid, dev->name); |
| 814 | return -EINVAL; |
| 815 | } |
stephen hemminger | 36fd2b6 | 2011-04-04 14:03:31 +0000 | [diff] [blame] | 816 | |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 817 | err = __br_fdb_delete(p, addr, vid); |
| 818 | } else { |
Toshiaki Makita | ef40b7e | 2013-08-20 17:10:18 +0900 | [diff] [blame] | 819 | if (!pv || bitmap_empty(pv->vlan_bitmap, VLAN_N_VID)) { |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 820 | err = __br_fdb_delete(p, addr, 0); |
| 821 | goto out; |
| 822 | } |
| 823 | |
| 824 | /* We have vlans configured on this port and user didn't |
| 825 | * specify a VLAN. To be nice, add/update entry for every |
| 826 | * vlan on this port. |
| 827 | */ |
| 828 | err = -ENOENT; |
Toshiaki Makita | ef40b7e | 2013-08-20 17:10:18 +0900 | [diff] [blame] | 829 | for_each_set_bit(vid, pv->vlan_bitmap, VLAN_N_VID) { |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 830 | err &= __br_fdb_delete(p, addr, vid); |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 831 | } |
| 832 | } |
| 833 | out: |
stephen hemminger | 36fd2b6 | 2011-04-04 14:03:31 +0000 | [diff] [blame] | 834 | return err; |
| 835 | } |