blob: d9ca85b87971f2db1c073ab2810f1db7ee471344 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Forwarding database
3 * Linux ethernet bridge
4 *
5 * Authors:
6 * Lennert Buytenhek <buytenh@gnu.org>
7 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * 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-Huu82524742008-05-12 21:21:05 +020016#include <linux/rculist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#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 Hemminger3f890922007-03-21 13:42:33 -070022#include <linux/random.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
Arun Sharma60063492011-07-26 16:09:06 -070024#include <linux/atomic.h>
Stephen Hemminger3f890922007-03-21 13:42:33 -070025#include <asm/unaligned.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include "br_private.h"
27
Christoph Lametere18b8902006-12-06 20:33:20 -080028static struct kmem_cache *br_fdb_cache __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070029static int fdb_insert(struct net_bridge *br, struct net_bridge_port *source,
30 const unsigned char *addr);
stephen hemmingerb078f0d2011-04-04 14:03:30 +000031static void fdb_notify(const struct net_bridge_fdb_entry *, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Stephen Hemminger3f890922007-03-21 13:42:33 -070033static u32 fdb_salt __read_mostly;
34
Akinobu Mita87a596e2007-04-07 18:57:07 +090035int __init br_fdb_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070036{
37 br_fdb_cache = kmem_cache_create("bridge_fdb_cache",
38 sizeof(struct net_bridge_fdb_entry),
39 0,
Paul Mundt20c2df82007-07-20 10:11:58 +090040 SLAB_HWCACHE_ALIGN, NULL);
Akinobu Mita87a596e2007-04-07 18:57:07 +090041 if (!br_fdb_cache)
42 return -ENOMEM;
43
Stephen Hemminger3f890922007-03-21 13:42:33 -070044 get_random_bytes(&fdb_salt, sizeof(fdb_salt));
Akinobu Mita87a596e2007-04-07 18:57:07 +090045 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046}
47
Andrew Morton73afc902007-12-05 21:35:23 -080048void br_fdb_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049{
50 kmem_cache_destroy(br_fdb_cache);
51}
52
53
54/* if topology_changing then use forward_delay (default 15 sec)
55 * otherwise keep longer (default 5 minutes)
56 */
Stephen Hemminger3f890922007-03-21 13:42:33 -070057static inline unsigned long hold_time(const struct net_bridge *br)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058{
59 return br->topology_change ? br->forward_delay : br->ageing_time;
60}
61
Stephen Hemminger3f890922007-03-21 13:42:33 -070062static inline int has_expired(const struct net_bridge *br,
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 const struct net_bridge_fdb_entry *fdb)
64{
Joe Perchesf64f9e72009-11-29 16:55:45 -080065 return !fdb->is_static &&
stephen hemminger7cd88612011-04-04 14:03:28 +000066 time_before_eq(fdb->updated + hold_time(br), jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067}
68
Stephen Hemminger3f890922007-03-21 13:42:33 -070069static inline int br_mac_hash(const unsigned char *mac)
Linus Torvalds1da177e2005-04-16 15:20:36 -070070{
Stephen Hemminger3f890922007-03-21 13:42:33 -070071 /* use 1 byte of OUI cnd 3 bytes of NIC */
72 u32 key = get_unaligned((u32 *)(mac + 2));
73 return jhash_1word(key, fdb_salt) & (BR_HASH_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074}
75
Michał Mirosławda678292009-06-05 05:35:28 +000076static void fdb_rcu_free(struct rcu_head *head)
77{
78 struct net_bridge_fdb_entry *ent
79 = container_of(head, struct net_bridge_fdb_entry, rcu);
80 kmem_cache_free(br_fdb_cache, ent);
81}
82
Stephen Hemminger3f890922007-03-21 13:42:33 -070083static inline void fdb_delete(struct net_bridge_fdb_entry *f)
Linus Torvalds1da177e2005-04-16 15:20:36 -070084{
stephen hemmingerb078f0d2011-04-04 14:03:30 +000085 fdb_notify(f, RTM_DELNEIGH);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 hlist_del_rcu(&f->hlist);
Michał Mirosławda678292009-06-05 05:35:28 +000087 call_rcu(&f->rcu, fdb_rcu_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088}
89
90void br_fdb_changeaddr(struct net_bridge_port *p, const unsigned char *newaddr)
91{
92 struct net_bridge *br = p->br;
93 int i;
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +090094
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 spin_lock_bh(&br->hash_lock);
96
97 /* Search all chains since old address/hash is unknown */
98 for (i = 0; i < BR_HASH_SIZE; i++) {
99 struct hlist_node *h;
100 hlist_for_each(h, &br->hash[i]) {
101 struct net_bridge_fdb_entry *f;
102
103 f = hlist_entry(h, struct net_bridge_fdb_entry, hlist);
104 if (f->dst == p && f->is_local) {
105 /* maybe another port has same hw addr? */
106 struct net_bridge_port *op;
107 list_for_each_entry(op, &br->port_list, list) {
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900108 if (op != p &&
Stephen Hemminger6ede2462005-10-25 15:04:59 -0700109 !compare_ether_addr(op->dev->dev_addr,
110 f->addr.addr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 f->dst = op;
112 goto insert;
113 }
114 }
115
116 /* delete old one */
117 fdb_delete(f);
118 goto insert;
119 }
120 }
121 }
122 insert:
123 /* insert new address, may fail if invalid address or dup. */
124 fdb_insert(br, p, newaddr);
125
126 spin_unlock_bh(&br->hash_lock);
127}
128
129void br_fdb_cleanup(unsigned long _data)
130{
131 struct net_bridge *br = (struct net_bridge *)_data;
132 unsigned long delay = hold_time(br);
stephen hemminger25442e02010-06-15 06:14:12 +0000133 unsigned long next_timer = jiffies + br->ageing_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 int i;
135
136 spin_lock_bh(&br->hash_lock);
137 for (i = 0; i < BR_HASH_SIZE; i++) {
138 struct net_bridge_fdb_entry *f;
139 struct hlist_node *h, *n;
140
141 hlist_for_each_entry_safe(f, h, n, &br->hash[i], hlist) {
Baruch Even071f7722007-05-31 01:20:45 -0700142 unsigned long this_timer;
143 if (f->is_static)
144 continue;
stephen hemminger7cd88612011-04-04 14:03:28 +0000145 this_timer = f->updated + delay;
Baruch Even071f7722007-05-31 01:20:45 -0700146 if (time_before_eq(this_timer, jiffies))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 fdb_delete(f);
Fabio Checconi2bec0082008-03-20 15:54:58 -0700148 else if (time_before(this_timer, next_timer))
Baruch Even071f7722007-05-31 01:20:45 -0700149 next_timer = this_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 }
151 }
152 spin_unlock_bh(&br->hash_lock);
153
stephen hemminger25442e02010-06-15 06:14:12 +0000154 mod_timer(&br->gc_timer, round_jiffies_up(next_timer));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155}
156
Stephen Hemminger9cf63742007-04-09 12:57:54 -0700157/* Completely flush all dynamic entries in forwarding database.*/
158void br_fdb_flush(struct net_bridge *br)
159{
160 int i;
Stephen Hemminger1a620692006-10-12 14:45:38 -0700161
Stephen Hemminger9cf63742007-04-09 12:57:54 -0700162 spin_lock_bh(&br->hash_lock);
163 for (i = 0; i < BR_HASH_SIZE; i++) {
164 struct net_bridge_fdb_entry *f;
165 struct hlist_node *h, *n;
166 hlist_for_each_entry_safe(f, h, n, &br->hash[i], hlist) {
167 if (!f->is_static)
168 fdb_delete(f);
169 }
170 }
171 spin_unlock_bh(&br->hash_lock);
172}
173
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300174/* Flush all entries referring to a specific port.
Stephen Hemminger9cf63742007-04-09 12:57:54 -0700175 * if do_all is set also flush static entries
176 */
Stephen Hemminger1a620692006-10-12 14:45:38 -0700177void br_fdb_delete_by_port(struct net_bridge *br,
178 const struct net_bridge_port *p,
179 int do_all)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180{
181 int i;
182
183 spin_lock_bh(&br->hash_lock);
184 for (i = 0; i < BR_HASH_SIZE; i++) {
185 struct hlist_node *h, *g;
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 hlist_for_each_safe(h, g, &br->hash[i]) {
188 struct net_bridge_fdb_entry *f
189 = hlist_entry(h, struct net_bridge_fdb_entry, hlist);
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900190 if (f->dst != p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 continue;
192
Stephen Hemminger1a620692006-10-12 14:45:38 -0700193 if (f->is_static && !do_all)
194 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 /*
196 * if multiple ports all have the same device address
197 * then when one port is deleted, assign
198 * the local entry to other port
199 */
200 if (f->is_local) {
201 struct net_bridge_port *op;
202 list_for_each_entry(op, &br->port_list, list) {
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900203 if (op != p &&
Stephen Hemminger6ede2462005-10-25 15:04:59 -0700204 !compare_ether_addr(op->dev->dev_addr,
205 f->addr.addr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 f->dst = op;
207 goto skip_delete;
208 }
209 }
210 }
211
212 fdb_delete(f);
213 skip_delete: ;
214 }
215 }
216 spin_unlock_bh(&br->hash_lock);
217}
218
stephen hemmingereeaf61d2010-07-27 08:26:30 +0000219/* No locking or refcounting, assumes caller has rcu_read_lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220struct net_bridge_fdb_entry *__br_fdb_get(struct net_bridge *br,
221 const unsigned char *addr)
222{
223 struct hlist_node *h;
224 struct net_bridge_fdb_entry *fdb;
225
226 hlist_for_each_entry_rcu(fdb, h, &br->hash[br_mac_hash(addr)], hlist) {
Stephen Hemminger6ede2462005-10-25 15:04:59 -0700227 if (!compare_ether_addr(fdb->addr.addr, addr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 if (unlikely(has_expired(br, fdb)))
229 break;
230 return fdb;
231 }
232 }
233
234 return NULL;
235}
236
Michał Mirosławda678292009-06-05 05:35:28 +0000237#if defined(CONFIG_ATM_LANE) || defined(CONFIG_ATM_LANE_MODULE)
238/* Interface used by ATM LANE hook to test
239 * if an addr is on some other bridge port */
240int br_fdb_test_addr(struct net_device *dev, unsigned char *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241{
242 struct net_bridge_fdb_entry *fdb;
stephen hemmingerb5ed54e2010-11-15 06:38:13 +0000243 struct net_bridge_port *port;
Michał Mirosławda678292009-06-05 05:35:28 +0000244 int ret;
245
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 rcu_read_lock();
stephen hemmingerb5ed54e2010-11-15 06:38:13 +0000247 port = br_port_get_rcu(dev);
248 if (!port)
249 ret = 0;
250 else {
251 fdb = __br_fdb_get(port->br, addr);
252 ret = fdb && fdb->dst->dev != dev &&
253 fdb->dst->state == BR_STATE_FORWARDING;
254 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
Michał Mirosławda678292009-06-05 05:35:28 +0000257 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258}
Michał Mirosławda678292009-06-05 05:35:28 +0000259#endif /* CONFIG_ATM_LANE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
261/*
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900262 * Fill buffer with forwarding table records in
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 * the API format.
264 */
265int br_fdb_fillbuf(struct net_bridge *br, void *buf,
266 unsigned long maxnum, unsigned long skip)
267{
268 struct __fdb_entry *fe = buf;
269 int i, num = 0;
270 struct hlist_node *h;
271 struct net_bridge_fdb_entry *f;
272
273 memset(buf, 0, maxnum*sizeof(struct __fdb_entry));
274
275 rcu_read_lock();
276 for (i = 0; i < BR_HASH_SIZE; i++) {
277 hlist_for_each_entry_rcu(f, h, &br->hash[i], hlist) {
278 if (num >= maxnum)
279 goto out;
280
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900281 if (has_expired(br, f))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 continue;
283
284 if (skip) {
285 --skip;
286 continue;
287 }
288
289 /* convert from internal format to API */
290 memcpy(fe->mac_addr, f->addr.addr, ETH_ALEN);
Stephen Hemmingerae4f8fc2008-05-02 16:53:33 -0700291
292 /* due to ABI compat need to split into hi/lo */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 fe->port_no = f->dst->port_no;
Stephen Hemmingerae4f8fc2008-05-02 16:53:33 -0700294 fe->port_hi = f->dst->port_no >> 8;
295
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 fe->is_local = f->is_local;
297 if (!f->is_static)
stephen hemminger7cd88612011-04-04 14:03:28 +0000298 fe->ageing_timer_value = jiffies_to_clock_t(jiffies - f->updated);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 ++fe;
300 ++num;
301 }
302 }
303
304 out:
305 rcu_read_unlock();
306
307 return num;
308}
309
stephen hemminger664de482011-04-04 14:03:29 +0000310static struct net_bridge_fdb_entry *fdb_find(struct hlist_head *head,
311 const unsigned char *addr)
312{
313 struct hlist_node *h;
314 struct net_bridge_fdb_entry *fdb;
315
316 hlist_for_each_entry(fdb, h, head, hlist) {
317 if (!compare_ether_addr(fdb->addr.addr, addr))
318 return fdb;
319 }
320 return NULL;
321}
322
323static struct net_bridge_fdb_entry *fdb_find_rcu(struct hlist_head *head,
324 const unsigned char *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325{
326 struct hlist_node *h;
327 struct net_bridge_fdb_entry *fdb;
328
329 hlist_for_each_entry_rcu(fdb, h, head, hlist) {
Stephen Hemminger6ede2462005-10-25 15:04:59 -0700330 if (!compare_ether_addr(fdb->addr.addr, addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 return fdb;
332 }
333 return NULL;
334}
335
336static struct net_bridge_fdb_entry *fdb_create(struct hlist_head *head,
337 struct net_bridge_port *source,
stephen hemminger03e9b642011-04-04 14:03:27 +0000338 const unsigned char *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339{
340 struct net_bridge_fdb_entry *fdb;
341
342 fdb = kmem_cache_alloc(br_fdb_cache, GFP_ATOMIC);
343 if (fdb) {
344 memcpy(fdb->addr.addr, addr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 fdb->dst = source;
stephen hemminger03e9b642011-04-04 14:03:27 +0000346 fdb->is_local = 0;
347 fdb->is_static = 0;
stephen hemminger7cd88612011-04-04 14:03:28 +0000348 fdb->updated = fdb->used = jiffies;
Pavel Emelyanov1158f762011-02-04 13:02:36 -0800349 hlist_add_head_rcu(&fdb->hlist, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 }
351 return fdb;
352}
353
354static int fdb_insert(struct net_bridge *br, struct net_bridge_port *source,
355 const unsigned char *addr)
356{
357 struct hlist_head *head = &br->hash[br_mac_hash(addr)];
358 struct net_bridge_fdb_entry *fdb;
359
360 if (!is_valid_ether_addr(addr))
361 return -EINVAL;
362
363 fdb = fdb_find(head, addr);
364 if (fdb) {
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900365 /* it is okay to have multiple ports with same
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 * address, just use the first one.
367 */
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900368 if (fdb->is_local)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 return 0;
stephen hemminger28a16c92010-05-10 09:31:09 +0000370 br_warn(br, "adding interface %s with same address "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 "as a received packet\n",
372 source->dev->name);
373 fdb_delete(fdb);
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900374 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
stephen hemminger03e9b642011-04-04 14:03:27 +0000376 fdb = fdb_create(head, source, addr);
377 if (!fdb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 return -ENOMEM;
379
stephen hemminger03e9b642011-04-04 14:03:27 +0000380 fdb->is_local = fdb->is_static = 1;
stephen hemmingerf58ee4e2011-12-06 13:02:24 +0000381 fdb_notify(fdb, RTM_NEWNEIGH);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 return 0;
383}
384
stephen hemminger03e9b642011-04-04 14:03:27 +0000385/* Add entry for local address of interface */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386int br_fdb_insert(struct net_bridge *br, struct net_bridge_port *source,
387 const unsigned char *addr)
388{
389 int ret;
390
391 spin_lock_bh(&br->hash_lock);
392 ret = fdb_insert(br, source, addr);
393 spin_unlock_bh(&br->hash_lock);
394 return ret;
395}
396
397void br_fdb_update(struct net_bridge *br, struct net_bridge_port *source,
398 const unsigned char *addr)
399{
400 struct hlist_head *head = &br->hash[br_mac_hash(addr)];
401 struct net_bridge_fdb_entry *fdb;
402
403 /* some users want to always flood. */
404 if (hold_time(br) == 0)
405 return;
406
Stephen Hemmingerdf1c0b82007-08-30 22:15:35 -0700407 /* ignore packets unless we are using this port */
408 if (!(source->state == BR_STATE_LEARNING ||
409 source->state == BR_STATE_FORWARDING))
410 return;
411
stephen hemminger664de482011-04-04 14:03:29 +0000412 fdb = fdb_find_rcu(head, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 if (likely(fdb)) {
414 /* attempt to update an entry for a local interface */
415 if (unlikely(fdb->is_local)) {
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900416 if (net_ratelimit())
stephen hemminger28a16c92010-05-10 09:31:09 +0000417 br_warn(br, "received packet on %s with "
418 "own address as source address\n",
419 source->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 } else {
421 /* fastpath: update of existing entry */
422 fdb->dst = source;
stephen hemminger7cd88612011-04-04 14:03:28 +0000423 fdb->updated = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 }
425 } else {
Stephen Hemmingerf8ae7372006-03-20 22:58:36 -0800426 spin_lock(&br->hash_lock);
stephen hemmingerf58ee4e2011-12-06 13:02:24 +0000427 if (likely(!fdb_find(head, addr))) {
428 fdb = fdb_create(head, source, addr);
429 if (fdb)
430 fdb_notify(fdb, RTM_NEWNEIGH);
431 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 /* else we lose race and someone else inserts
433 * it first, don't bother updating
434 */
Stephen Hemmingerf8ae7372006-03-20 22:58:36 -0800435 spin_unlock(&br->hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437}
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000438
439static int fdb_to_nud(const struct net_bridge_fdb_entry *fdb)
440{
441 if (fdb->is_local)
442 return NUD_PERMANENT;
443 else if (fdb->is_static)
444 return NUD_NOARP;
445 else if (has_expired(fdb->dst->br, fdb))
446 return NUD_STALE;
447 else
448 return NUD_REACHABLE;
449}
450
451static int fdb_fill_info(struct sk_buff *skb,
452 const struct net_bridge_fdb_entry *fdb,
453 u32 pid, u32 seq, int type, unsigned int flags)
454{
455 unsigned long now = jiffies;
456 struct nda_cacheinfo ci;
457 struct nlmsghdr *nlh;
458 struct ndmsg *ndm;
459
460 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), flags);
461 if (nlh == NULL)
462 return -EMSGSIZE;
463
464
465 ndm = nlmsg_data(nlh);
466 ndm->ndm_family = AF_BRIDGE;
467 ndm->ndm_pad1 = 0;
468 ndm->ndm_pad2 = 0;
469 ndm->ndm_flags = 0;
470 ndm->ndm_type = 0;
471 ndm->ndm_ifindex = fdb->dst->dev->ifindex;
472 ndm->ndm_state = fdb_to_nud(fdb);
473
474 NLA_PUT(skb, NDA_LLADDR, ETH_ALEN, &fdb->addr);
475
476 ci.ndm_used = jiffies_to_clock_t(now - fdb->used);
477 ci.ndm_confirmed = 0;
478 ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated);
479 ci.ndm_refcnt = 0;
480 NLA_PUT(skb, NDA_CACHEINFO, sizeof(ci), &ci);
481
482 return nlmsg_end(skb, nlh);
483
484nla_put_failure:
485 nlmsg_cancel(skb, nlh);
486 return -EMSGSIZE;
487}
488
489static inline size_t fdb_nlmsg_size(void)
490{
491 return NLMSG_ALIGN(sizeof(struct ndmsg))
492 + nla_total_size(ETH_ALEN) /* NDA_LLADDR */
493 + nla_total_size(sizeof(struct nda_cacheinfo));
494}
495
496static void fdb_notify(const struct net_bridge_fdb_entry *fdb, int type)
497{
498 struct net *net = dev_net(fdb->dst->dev);
499 struct sk_buff *skb;
500 int err = -ENOBUFS;
501
502 skb = nlmsg_new(fdb_nlmsg_size(), GFP_ATOMIC);
503 if (skb == NULL)
504 goto errout;
505
506 err = fdb_fill_info(skb, fdb, 0, 0, type, 0);
507 if (err < 0) {
508 /* -EMSGSIZE implies BUG in fdb_nlmsg_size() */
509 WARN_ON(err == -EMSGSIZE);
510 kfree_skb(skb);
511 goto errout;
512 }
513 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
514 return;
515errout:
516 if (err < 0)
517 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
518}
519
520/* Dump information about entries, in response to GETNEIGH */
521int br_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
522{
523 struct net *net = sock_net(skb->sk);
524 struct net_device *dev;
525 int idx = 0;
526
527 rcu_read_lock();
528 for_each_netdev_rcu(net, dev) {
529 struct net_bridge *br = netdev_priv(dev);
530 int i;
531
532 if (!(dev->priv_flags & IFF_EBRIDGE))
533 continue;
534
535 for (i = 0; i < BR_HASH_SIZE; i++) {
536 struct hlist_node *h;
537 struct net_bridge_fdb_entry *f;
538
539 hlist_for_each_entry_rcu(f, h, &br->hash[i], hlist) {
540 if (idx < cb->args[0])
541 goto skip;
542
543 if (fdb_fill_info(skb, f,
544 NETLINK_CB(cb->skb).pid,
545 cb->nlh->nlmsg_seq,
546 RTM_NEWNEIGH,
547 NLM_F_MULTI) < 0)
548 break;
549skip:
550 ++idx;
551 }
552 }
553 }
554 rcu_read_unlock();
555
556 cb->args[0] = idx;
557
558 return skb->len;
559}
stephen hemminger36fd2b62011-04-04 14:03:31 +0000560
stephen hemminger292d1392011-11-09 18:30:08 +0000561/* Update (create or replace) forwarding database entry */
stephen hemminger36fd2b62011-04-04 14:03:31 +0000562static int fdb_add_entry(struct net_bridge_port *source, const __u8 *addr,
stephen hemminger64af1ba2011-09-30 14:37:27 +0000563 __u16 state, __u16 flags)
stephen hemminger36fd2b62011-04-04 14:03:31 +0000564{
565 struct net_bridge *br = source->br;
566 struct hlist_head *head = &br->hash[br_mac_hash(addr)];
567 struct net_bridge_fdb_entry *fdb;
568
569 fdb = fdb_find(head, addr);
stephen hemminger64af1ba2011-09-30 14:37:27 +0000570 if (fdb == NULL) {
571 if (!(flags & NLM_F_CREATE))
572 return -ENOENT;
stephen hemminger36fd2b62011-04-04 14:03:31 +0000573
stephen hemminger64af1ba2011-09-30 14:37:27 +0000574 fdb = fdb_create(head, source, addr);
575 if (!fdb)
576 return -ENOMEM;
stephen hemmingerf58ee4e2011-12-06 13:02:24 +0000577 fdb_notify(fdb, RTM_NEWNEIGH);
stephen hemminger64af1ba2011-09-30 14:37:27 +0000578 } else {
579 if (flags & NLM_F_EXCL)
580 return -EEXIST;
stephen hemminger64af1ba2011-09-30 14:37:27 +0000581 }
stephen hemminger36fd2b62011-04-04 14:03:31 +0000582
stephen hemminger292d1392011-11-09 18:30:08 +0000583 if (fdb_to_nud(fdb) != state) {
584 if (state & NUD_PERMANENT)
585 fdb->is_local = fdb->is_static = 1;
586 else if (state & NUD_NOARP) {
587 fdb->is_local = 0;
588 fdb->is_static = 1;
589 } else
590 fdb->is_local = fdb->is_static = 0;
591
592 fdb->updated = fdb->used = jiffies;
593 fdb_notify(fdb, RTM_NEWNEIGH);
594 }
595
stephen hemminger36fd2b62011-04-04 14:03:31 +0000596 return 0;
597}
598
599/* Add new permanent fdb entry with RTM_NEWNEIGH */
600int br_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
601{
602 struct net *net = sock_net(skb->sk);
603 struct ndmsg *ndm;
604 struct nlattr *tb[NDA_MAX+1];
605 struct net_device *dev;
606 struct net_bridge_port *p;
607 const __u8 *addr;
608 int err;
609
610 ASSERT_RTNL();
611 err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL);
612 if (err < 0)
613 return err;
614
615 ndm = nlmsg_data(nlh);
616 if (ndm->ndm_ifindex == 0) {
617 pr_info("bridge: RTM_NEWNEIGH with invalid ifindex\n");
618 return -EINVAL;
619 }
620
621 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
622 if (dev == NULL) {
623 pr_info("bridge: RTM_NEWNEIGH with unknown ifindex\n");
624 return -ENODEV;
625 }
626
627 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
628 pr_info("bridge: RTM_NEWNEIGH with invalid address\n");
629 return -EINVAL;
630 }
631
632 addr = nla_data(tb[NDA_LLADDR]);
633 if (!is_valid_ether_addr(addr)) {
634 pr_info("bridge: RTM_NEWNEIGH with invalid ether address\n");
635 return -EINVAL;
636 }
637
stephen hemminger292d1392011-11-09 18:30:08 +0000638 if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_NOARP|NUD_REACHABLE))) {
639 pr_info("bridge: RTM_NEWNEIGH with invalid state %#x\n", ndm->ndm_state);
640 return -EINVAL;
641 }
642
stephen hemminger36fd2b62011-04-04 14:03:31 +0000643 p = br_port_get_rtnl(dev);
644 if (p == NULL) {
645 pr_info("bridge: RTM_NEWNEIGH %s not a bridge port\n",
646 dev->name);
647 return -EINVAL;
648 }
649
stephen hemminger292d1392011-11-09 18:30:08 +0000650 if (ndm->ndm_flags & NTF_USE) {
651 rcu_read_lock();
652 br_fdb_update(p->br, p, addr);
653 rcu_read_unlock();
654 } else {
655 spin_lock_bh(&p->br->hash_lock);
656 err = fdb_add_entry(p, addr, ndm->ndm_state, nlh->nlmsg_flags);
657 spin_unlock_bh(&p->br->hash_lock);
658 }
stephen hemminger36fd2b62011-04-04 14:03:31 +0000659
660 return err;
661}
662
663static int fdb_delete_by_addr(struct net_bridge_port *p, const u8 *addr)
664{
665 struct net_bridge *br = p->br;
666 struct hlist_head *head = &br->hash[br_mac_hash(addr)];
667 struct net_bridge_fdb_entry *fdb;
668
669 fdb = fdb_find(head, addr);
670 if (!fdb)
671 return -ENOENT;
672
673 fdb_delete(fdb);
674 return 0;
675}
676
677/* Remove neighbor entry with RTM_DELNEIGH */
678int br_fdb_delete(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
679{
680 struct net *net = sock_net(skb->sk);
681 struct ndmsg *ndm;
682 struct net_bridge_port *p;
683 struct nlattr *llattr;
684 const __u8 *addr;
685 struct net_device *dev;
686 int err;
687
688 ASSERT_RTNL();
689 if (nlmsg_len(nlh) < sizeof(*ndm))
690 return -EINVAL;
691
692 ndm = nlmsg_data(nlh);
693 if (ndm->ndm_ifindex == 0) {
694 pr_info("bridge: RTM_DELNEIGH with invalid ifindex\n");
695 return -EINVAL;
696 }
697
698 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
699 if (dev == NULL) {
700 pr_info("bridge: RTM_DELNEIGH with unknown ifindex\n");
701 return -ENODEV;
702 }
703
704 llattr = nlmsg_find_attr(nlh, sizeof(*ndm), NDA_LLADDR);
705 if (llattr == NULL || nla_len(llattr) != ETH_ALEN) {
706 pr_info("bridge: RTM_DELNEIGH with invalid address\n");
707 return -EINVAL;
708 }
709
710 addr = nla_data(llattr);
711
712 p = br_port_get_rtnl(dev);
713 if (p == NULL) {
714 pr_info("bridge: RTM_DELNEIGH %s not a bridge port\n",
715 dev->name);
716 return -EINVAL;
717 }
718
719 spin_lock_bh(&p->br->hash_lock);
720 err = fdb_delete_by_addr(p, addr);
721 spin_unlock_bh(&p->br->hash_lock);
722
723 return err;
724}