br_fdb.c revision 31e8a49c161b00c648e960903512c9cbaee777b1
1/*
2 *	Forwarding database
3 *	Linux ethernet bridge
4 *
5 *	Authors:
6 *	Lennert Buytenhek		<buytenh@gnu.org>
7 *
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>
16#include <linux/rculist.h>
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>
22#include <linux/random.h>
23#include <linux/slab.h>
24#include <linux/atomic.h>
25#include <asm/unaligned.h>
26#include "br_private.h"
27
28static struct kmem_cache *br_fdb_cache __read_mostly;
29static int fdb_insert(struct net_bridge *br, struct net_bridge_port *source,
30		      const unsigned char *addr);
31static void fdb_notify(struct net_bridge *br,
32		       const struct net_bridge_fdb_entry *, int);
33
34static u32 fdb_salt __read_mostly;
35
36int __init br_fdb_init(void)
37{
38	br_fdb_cache = kmem_cache_create("bridge_fdb_cache",
39					 sizeof(struct net_bridge_fdb_entry),
40					 0,
41					 SLAB_HWCACHE_ALIGN, NULL);
42	if (!br_fdb_cache)
43		return -ENOMEM;
44
45	get_random_bytes(&fdb_salt, sizeof(fdb_salt));
46	return 0;
47}
48
49void br_fdb_fini(void)
50{
51	kmem_cache_destroy(br_fdb_cache);
52}
53
54
55/* if topology_changing then use forward_delay (default 15 sec)
56 * otherwise keep longer (default 5 minutes)
57 */
58static inline unsigned long hold_time(const struct net_bridge *br)
59{
60	return br->topology_change ? br->forward_delay : br->ageing_time;
61}
62
63static inline int has_expired(const struct net_bridge *br,
64				  const struct net_bridge_fdb_entry *fdb)
65{
66	return !fdb->is_static &&
67		time_before_eq(fdb->updated + hold_time(br), jiffies);
68}
69
70static inline int br_mac_hash(const unsigned char *mac)
71{
72	/* use 1 byte of OUI cnd 3 bytes of NIC */
73	u32 key = get_unaligned((u32 *)(mac + 2));
74	return jhash_1word(key, fdb_salt) & (BR_HASH_SIZE - 1);
75}
76
77static void fdb_rcu_free(struct rcu_head *head)
78{
79	struct net_bridge_fdb_entry *ent
80		= container_of(head, struct net_bridge_fdb_entry, rcu);
81	kmem_cache_free(br_fdb_cache, ent);
82}
83
84static void fdb_delete(struct net_bridge *br, struct net_bridge_fdb_entry *f)
85{
86	hlist_del_rcu(&f->hlist);
87	fdb_notify(br, f, RTM_DELNEIGH);
88	call_rcu(&f->rcu, fdb_rcu_free);
89}
90
91void br_fdb_changeaddr(struct net_bridge_port *p, const unsigned char *newaddr)
92{
93	struct net_bridge *br = p->br;
94	int i;
95
96	spin_lock_bh(&br->hash_lock);
97
98	/* Search all chains since old address/hash is unknown */
99	for (i = 0; i < BR_HASH_SIZE; i++) {
100		struct hlist_node *h;
101		hlist_for_each(h, &br->hash[i]) {
102			struct net_bridge_fdb_entry *f;
103
104			f = hlist_entry(h, struct net_bridge_fdb_entry, hlist);
105			if (f->dst == p && f->is_local) {
106				/* maybe another port has same hw addr? */
107				struct net_bridge_port *op;
108				list_for_each_entry(op, &br->port_list, list) {
109					if (op != p &&
110					    !compare_ether_addr(op->dev->dev_addr,
111								f->addr.addr)) {
112						f->dst = op;
113						goto insert;
114					}
115				}
116
117				/* delete old one */
118				fdb_delete(br, f);
119				goto insert;
120			}
121		}
122	}
123 insert:
124	/* insert new address,  may fail if invalid address or dup. */
125	fdb_insert(br, p, newaddr);
126
127	spin_unlock_bh(&br->hash_lock);
128}
129
130void br_fdb_cleanup(unsigned long _data)
131{
132	struct net_bridge *br = (struct net_bridge *)_data;
133	unsigned long delay = hold_time(br);
134	unsigned long next_timer = jiffies + br->ageing_time;
135	int i;
136
137	spin_lock_bh(&br->hash_lock);
138	for (i = 0; i < BR_HASH_SIZE; i++) {
139		struct net_bridge_fdb_entry *f;
140		struct hlist_node *h, *n;
141
142		hlist_for_each_entry_safe(f, h, n, &br->hash[i], hlist) {
143			unsigned long this_timer;
144			if (f->is_static)
145				continue;
146			this_timer = f->updated + delay;
147			if (time_before_eq(this_timer, jiffies))
148				fdb_delete(br, f);
149			else if (time_before(this_timer, next_timer))
150				next_timer = this_timer;
151		}
152	}
153	spin_unlock_bh(&br->hash_lock);
154
155	mod_timer(&br->gc_timer, round_jiffies_up(next_timer));
156}
157
158/* Completely flush all dynamic entries in forwarding database.*/
159void br_fdb_flush(struct net_bridge *br)
160{
161	int i;
162
163	spin_lock_bh(&br->hash_lock);
164	for (i = 0; i < BR_HASH_SIZE; i++) {
165		struct net_bridge_fdb_entry *f;
166		struct hlist_node *h, *n;
167		hlist_for_each_entry_safe(f, h, n, &br->hash[i], hlist) {
168			if (!f->is_static)
169				fdb_delete(br, f);
170		}
171	}
172	spin_unlock_bh(&br->hash_lock);
173}
174
175/* Flush all entries referring to a specific port.
176 * if do_all is set also flush static entries
177 */
178void br_fdb_delete_by_port(struct net_bridge *br,
179			   const struct net_bridge_port *p,
180			   int do_all)
181{
182	int i;
183
184	spin_lock_bh(&br->hash_lock);
185	for (i = 0; i < BR_HASH_SIZE; i++) {
186		struct hlist_node *h, *g;
187
188		hlist_for_each_safe(h, g, &br->hash[i]) {
189			struct net_bridge_fdb_entry *f
190				= hlist_entry(h, struct net_bridge_fdb_entry, hlist);
191			if (f->dst != p)
192				continue;
193
194			if (f->is_static && !do_all)
195				continue;
196			/*
197			 * if multiple ports all have the same device address
198			 * then when one port is deleted, assign
199			 * the local entry to other port
200			 */
201			if (f->is_local) {
202				struct net_bridge_port *op;
203				list_for_each_entry(op, &br->port_list, list) {
204					if (op != p &&
205					    !compare_ether_addr(op->dev->dev_addr,
206								f->addr.addr)) {
207						f->dst = op;
208						goto skip_delete;
209					}
210				}
211			}
212
213			fdb_delete(br, f);
214		skip_delete: ;
215		}
216	}
217	spin_unlock_bh(&br->hash_lock);
218}
219
220/* No locking or refcounting, assumes caller has rcu_read_lock */
221struct net_bridge_fdb_entry *__br_fdb_get(struct net_bridge *br,
222					  const unsigned char *addr)
223{
224	struct hlist_node *h;
225	struct net_bridge_fdb_entry *fdb;
226
227	hlist_for_each_entry_rcu(fdb, h, &br->hash[br_mac_hash(addr)], hlist) {
228		if (!compare_ether_addr(fdb->addr.addr, addr)) {
229			if (unlikely(has_expired(br, fdb)))
230				break;
231			return fdb;
232		}
233	}
234
235	return NULL;
236}
237
238#if defined(CONFIG_ATM_LANE) || defined(CONFIG_ATM_LANE_MODULE)
239/* Interface used by ATM LANE hook to test
240 * if an addr is on some other bridge port */
241int br_fdb_test_addr(struct net_device *dev, unsigned char *addr)
242{
243	struct net_bridge_fdb_entry *fdb;
244	struct net_bridge_port *port;
245	int ret;
246
247	rcu_read_lock();
248	port = br_port_get_rcu(dev);
249	if (!port)
250		ret = 0;
251	else {
252		fdb = __br_fdb_get(port->br, addr);
253		ret = fdb && fdb->dst->dev != dev &&
254			fdb->dst->state == BR_STATE_FORWARDING;
255	}
256	rcu_read_unlock();
257
258	return ret;
259}
260#endif /* CONFIG_ATM_LANE */
261
262/*
263 * Fill buffer with forwarding table records in
264 * the API format.
265 */
266int br_fdb_fillbuf(struct net_bridge *br, void *buf,
267		   unsigned long maxnum, unsigned long skip)
268{
269	struct __fdb_entry *fe = buf;
270	int i, num = 0;
271	struct hlist_node *h;
272	struct net_bridge_fdb_entry *f;
273
274	memset(buf, 0, maxnum*sizeof(struct __fdb_entry));
275
276	rcu_read_lock();
277	for (i = 0; i < BR_HASH_SIZE; i++) {
278		hlist_for_each_entry_rcu(f, h, &br->hash[i], hlist) {
279			if (num >= maxnum)
280				goto out;
281
282			if (has_expired(br, f))
283				continue;
284
285			if (skip) {
286				--skip;
287				continue;
288			}
289
290			/* convert from internal format to API */
291			memcpy(fe->mac_addr, f->addr.addr, ETH_ALEN);
292
293			/* due to ABI compat need to split into hi/lo */
294			fe->port_no = f->dst->port_no;
295			fe->port_hi = f->dst->port_no >> 8;
296
297			fe->is_local = f->is_local;
298			if (!f->is_static)
299				fe->ageing_timer_value = jiffies_to_clock_t(jiffies - f->updated);
300			++fe;
301			++num;
302		}
303	}
304
305 out:
306	rcu_read_unlock();
307
308	return num;
309}
310
311static struct net_bridge_fdb_entry *fdb_find(struct hlist_head *head,
312					     const unsigned char *addr)
313{
314	struct hlist_node *h;
315	struct net_bridge_fdb_entry *fdb;
316
317	hlist_for_each_entry(fdb, h, head, hlist) {
318		if (!compare_ether_addr(fdb->addr.addr, addr))
319			return fdb;
320	}
321	return NULL;
322}
323
324static struct net_bridge_fdb_entry *fdb_find_rcu(struct hlist_head *head,
325						 const unsigned char *addr)
326{
327	struct hlist_node *h;
328	struct net_bridge_fdb_entry *fdb;
329
330	hlist_for_each_entry_rcu(fdb, h, head, hlist) {
331		if (!compare_ether_addr(fdb->addr.addr, addr))
332			return fdb;
333	}
334	return NULL;
335}
336
337static struct net_bridge_fdb_entry *fdb_create(struct hlist_head *head,
338					       struct net_bridge_port *source,
339					       const unsigned char *addr)
340{
341	struct net_bridge_fdb_entry *fdb;
342
343	fdb = kmem_cache_alloc(br_fdb_cache, GFP_ATOMIC);
344	if (fdb) {
345		memcpy(fdb->addr.addr, addr, ETH_ALEN);
346		fdb->dst = source;
347		fdb->is_local = 0;
348		fdb->is_static = 0;
349		fdb->updated = fdb->used = jiffies;
350		hlist_add_head_rcu(&fdb->hlist, head);
351	}
352	return fdb;
353}
354
355static int fdb_insert(struct net_bridge *br, struct net_bridge_port *source,
356		  const unsigned char *addr)
357{
358	struct hlist_head *head = &br->hash[br_mac_hash(addr)];
359	struct net_bridge_fdb_entry *fdb;
360
361	if (!is_valid_ether_addr(addr))
362		return -EINVAL;
363
364	fdb = fdb_find(head, addr);
365	if (fdb) {
366		/* it is okay to have multiple ports with same
367		 * address, just use the first one.
368		 */
369		if (fdb->is_local)
370			return 0;
371		br_warn(br, "adding interface %s with same address "
372		       "as a received packet\n",
373		       source->dev->name);
374		fdb_delete(br, fdb);
375	}
376
377	fdb = fdb_create(head, source, addr);
378	if (!fdb)
379		return -ENOMEM;
380
381	fdb->is_local = fdb->is_static = 1;
382	fdb_notify(br, fdb, RTM_NEWNEIGH);
383	return 0;
384}
385
386/* Add entry for local address of interface */
387int br_fdb_insert(struct net_bridge *br, struct net_bridge_port *source,
388		  const unsigned char *addr)
389{
390	int ret;
391
392	spin_lock_bh(&br->hash_lock);
393	ret = fdb_insert(br, source, addr);
394	spin_unlock_bh(&br->hash_lock);
395	return ret;
396}
397
398void br_fdb_update(struct net_bridge *br, struct net_bridge_port *source,
399		   const unsigned char *addr)
400{
401	struct hlist_head *head = &br->hash[br_mac_hash(addr)];
402	struct net_bridge_fdb_entry *fdb;
403
404	/* some users want to always flood. */
405	if (hold_time(br) == 0)
406		return;
407
408	/* ignore packets unless we are using this port */
409	if (!(source->state == BR_STATE_LEARNING ||
410	      source->state == BR_STATE_FORWARDING))
411		return;
412
413	fdb = fdb_find_rcu(head, addr);
414	if (likely(fdb)) {
415		/* attempt to update an entry for a local interface */
416		if (unlikely(fdb->is_local)) {
417			if (net_ratelimit())
418				br_warn(br, "received packet on %s with "
419					"own address as source address\n",
420					source->dev->name);
421		} else {
422			/* fastpath: update of existing entry */
423			fdb->dst = source;
424			fdb->updated = jiffies;
425		}
426	} else {
427		spin_lock(&br->hash_lock);
428		if (likely(!fdb_find(head, addr))) {
429			fdb = fdb_create(head, source, addr);
430			if (fdb)
431				fdb_notify(br, fdb, RTM_NEWNEIGH);
432		}
433		/* else  we lose race and someone else inserts
434		 * it first, don't bother updating
435		 */
436		spin_unlock(&br->hash_lock);
437	}
438}
439
440static int fdb_to_nud(const struct net_bridge_fdb_entry *fdb)
441{
442	if (fdb->is_local)
443		return NUD_PERMANENT;
444	else if (fdb->is_static)
445		return NUD_NOARP;
446	else if (has_expired(fdb->dst->br, fdb))
447		return NUD_STALE;
448	else
449		return NUD_REACHABLE;
450}
451
452static int fdb_fill_info(struct sk_buff *skb, const struct net_bridge *br,
453			 const struct net_bridge_fdb_entry *fdb,
454			 u32 pid, u32 seq, int type, unsigned int flags)
455{
456	unsigned long now = jiffies;
457	struct nda_cacheinfo ci;
458	struct nlmsghdr *nlh;
459	struct ndmsg *ndm;
460
461	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), flags);
462	if (nlh == NULL)
463		return -EMSGSIZE;
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(struct net_bridge *br,
497		       const struct net_bridge_fdb_entry *fdb, int type)
498{
499	struct net *net = dev_net(br->dev);
500	struct sk_buff *skb;
501	int err = -ENOBUFS;
502
503	skb = nlmsg_new(fdb_nlmsg_size(), GFP_ATOMIC);
504	if (skb == NULL)
505		goto errout;
506
507	err = fdb_fill_info(skb, br, fdb, 0, 0, type, 0);
508	if (err < 0) {
509		/* -EMSGSIZE implies BUG in fdb_nlmsg_size() */
510		WARN_ON(err == -EMSGSIZE);
511		kfree_skb(skb);
512		goto errout;
513	}
514	rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
515	return;
516errout:
517	if (err < 0)
518		rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
519}
520
521/* Dump information about entries, in response to GETNEIGH */
522int br_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
523{
524	struct net *net = sock_net(skb->sk);
525	struct net_device *dev;
526	int idx = 0;
527
528	rcu_read_lock();
529	for_each_netdev_rcu(net, dev) {
530		struct net_bridge *br = netdev_priv(dev);
531		int i;
532
533		if (!(dev->priv_flags & IFF_EBRIDGE))
534			continue;
535
536		for (i = 0; i < BR_HASH_SIZE; i++) {
537			struct hlist_node *h;
538			struct net_bridge_fdb_entry *f;
539
540			hlist_for_each_entry_rcu(f, h, &br->hash[i], hlist) {
541				if (idx < cb->args[0])
542					goto skip;
543
544				if (fdb_fill_info(skb, br, f,
545						  NETLINK_CB(cb->skb).pid,
546						  cb->nlh->nlmsg_seq,
547						  RTM_NEWNEIGH,
548						  NLM_F_MULTI) < 0)
549					break;
550skip:
551				++idx;
552			}
553		}
554	}
555	rcu_read_unlock();
556
557	cb->args[0] = idx;
558
559	return skb->len;
560}
561
562/* Update (create or replace) forwarding database entry */
563static int fdb_add_entry(struct net_bridge_port *source, const __u8 *addr,
564			 __u16 state, __u16 flags)
565{
566	struct net_bridge *br = source->br;
567	struct hlist_head *head = &br->hash[br_mac_hash(addr)];
568	struct net_bridge_fdb_entry *fdb;
569
570	fdb = fdb_find(head, addr);
571	if (fdb == NULL) {
572		if (!(flags & NLM_F_CREATE))
573			return -ENOENT;
574
575		fdb = fdb_create(head, source, addr);
576		if (!fdb)
577			return -ENOMEM;
578		fdb_notify(br, fdb, RTM_NEWNEIGH);
579	} else {
580		if (flags & NLM_F_EXCL)
581			return -EEXIST;
582	}
583
584	if (fdb_to_nud(fdb) != state) {
585		if (state & NUD_PERMANENT)
586			fdb->is_local = fdb->is_static = 1;
587		else if (state & NUD_NOARP) {
588			fdb->is_local = 0;
589			fdb->is_static = 1;
590		} else
591			fdb->is_local = fdb->is_static = 0;
592
593		fdb->updated = fdb->used = jiffies;
594		fdb_notify(br, fdb, RTM_NEWNEIGH);
595	}
596
597	return 0;
598}
599
600/* Add new permanent fdb entry with RTM_NEWNEIGH */
601int br_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
602{
603	struct net *net = sock_net(skb->sk);
604	struct ndmsg *ndm;
605	struct nlattr *tb[NDA_MAX+1];
606	struct net_device *dev;
607	struct net_bridge_port *p;
608	const __u8 *addr;
609	int err;
610
611	ASSERT_RTNL();
612	err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL);
613	if (err < 0)
614		return err;
615
616	ndm = nlmsg_data(nlh);
617	if (ndm->ndm_ifindex == 0) {
618		pr_info("bridge: RTM_NEWNEIGH with invalid ifindex\n");
619		return -EINVAL;
620	}
621
622	dev = __dev_get_by_index(net, ndm->ndm_ifindex);
623	if (dev == NULL) {
624		pr_info("bridge: RTM_NEWNEIGH with unknown ifindex\n");
625		return -ENODEV;
626	}
627
628	if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
629		pr_info("bridge: RTM_NEWNEIGH with invalid address\n");
630		return -EINVAL;
631	}
632
633	addr = nla_data(tb[NDA_LLADDR]);
634	if (!is_valid_ether_addr(addr)) {
635		pr_info("bridge: RTM_NEWNEIGH with invalid ether address\n");
636		return -EINVAL;
637	}
638
639	if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_NOARP|NUD_REACHABLE))) {
640		pr_info("bridge: RTM_NEWNEIGH with invalid state %#x\n", ndm->ndm_state);
641		return -EINVAL;
642	}
643
644	p = br_port_get_rtnl(dev);
645	if (p == NULL) {
646		pr_info("bridge: RTM_NEWNEIGH %s not a bridge port\n",
647			dev->name);
648		return -EINVAL;
649	}
650
651	if (ndm->ndm_flags & NTF_USE) {
652		rcu_read_lock();
653		br_fdb_update(p->br, p, addr);
654		rcu_read_unlock();
655	} else {
656		spin_lock_bh(&p->br->hash_lock);
657		err = fdb_add_entry(p, addr, ndm->ndm_state, nlh->nlmsg_flags);
658		spin_unlock_bh(&p->br->hash_lock);
659	}
660
661	return err;
662}
663
664static int fdb_delete_by_addr(struct net_bridge_port *p, const u8 *addr)
665{
666	struct net_bridge *br = p->br;
667	struct hlist_head *head = &br->hash[br_mac_hash(addr)];
668	struct net_bridge_fdb_entry *fdb;
669
670	fdb = fdb_find(head, addr);
671	if (!fdb)
672		return -ENOENT;
673
674	fdb_delete(p->br, fdb);
675	return 0;
676}
677
678/* Remove neighbor entry with RTM_DELNEIGH */
679int br_fdb_delete(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
680{
681	struct net *net = sock_net(skb->sk);
682	struct ndmsg *ndm;
683	struct net_bridge_port *p;
684	struct nlattr *llattr;
685	const __u8 *addr;
686	struct net_device *dev;
687	int err;
688
689	ASSERT_RTNL();
690	if (nlmsg_len(nlh) < sizeof(*ndm))
691		return -EINVAL;
692
693	ndm = nlmsg_data(nlh);
694	if (ndm->ndm_ifindex == 0) {
695		pr_info("bridge: RTM_DELNEIGH with invalid ifindex\n");
696		return -EINVAL;
697	}
698
699	dev = __dev_get_by_index(net, ndm->ndm_ifindex);
700	if (dev == NULL) {
701		pr_info("bridge: RTM_DELNEIGH with unknown ifindex\n");
702		return -ENODEV;
703	}
704
705	llattr = nlmsg_find_attr(nlh, sizeof(*ndm), NDA_LLADDR);
706	if (llattr == NULL || nla_len(llattr) != ETH_ALEN) {
707		pr_info("bridge: RTM_DELNEIGH with invalid address\n");
708		return -EINVAL;
709	}
710
711	addr = nla_data(llattr);
712
713	p = br_port_get_rtnl(dev);
714	if (p == NULL) {
715		pr_info("bridge: RTM_DELNEIGH %s not a bridge port\n",
716			dev->name);
717		return -EINVAL;
718	}
719
720	spin_lock_bh(&p->br->hash_lock);
721	err = fdb_delete_by_addr(p, addr);
722	spin_unlock_bh(&p->br->hash_lock);
723
724	return err;
725}
726