2840 lines · 66.0 KB · C
RawDownload
// SPDX-License-Identifier: GPL-2.0-or-later/* *	Linux INET6 implementation *	Forwarding Information Database * *	Authors: *	Pedro Roque		<roque@di.fc.ul.pt> * *	Changes: *	Yuji SEKIYA @USAGI:	Support default route on router node; *				remove ip6_null_entry from the top of *				routing table. *	Ville Nuorvala:		Fixed routing subtrees. */ #define pr_fmt(fmt) "IPv6: " fmt #include <linux/bpf.h>#include <linux/errno.h>#include <linux/types.h>#include <linux/net.h>#include <linux/route.h>#include <linux/netdevice.h>#include <linux/in6.h>#include <linux/init.h>#include <linux/list.h>#include <linux/slab.h> #include <net/ip.h>#include <net/ipv6.h>#include <net/ndisc.h>#include <net/addrconf.h>#include <net/lwtunnel.h>#include <net/fib_notifier.h> #include <net/ip_fib.h>#include <net/ip6_fib.h>#include <net/ip6_route.h> static struct kmem_cache *fib6_node_kmem __read_mostly; struct fib6_cleaner {	struct fib6_walker w;	struct net *net;	int (*func)(struct fib6_info *, void *arg);	int sernum;	void *arg;	bool skip_notify;}; #ifdef CONFIG_IPV6_SUBTREES#define FWS_INIT FWS_S#else#define FWS_INIT FWS_L#endif static struct fib6_info *fib6_find_prefix(struct net *net,					 struct fib6_table *table,					 struct fib6_node *fn);static struct fib6_node *fib6_repair_tree(struct net *net,					  struct fib6_table *table,					  struct fib6_node *fn);static int fib6_walk(struct net *net, struct fib6_walker *w);static int fib6_walk_continue(struct fib6_walker *w); /* *	A routing update causes an increase of the serial number on the *	affected subtree. This allows for cached routes to be asynchronously *	tested when modifications are made to the destination cache as a *	result of redirects, path MTU changes, etc. */ static void fib6_gc_timer_cb(struct timer_list *t); #define FOR_WALKERS(net, w) \	list_for_each_entry(w, &(net)->ipv6.fib6_walkers, lh) static void fib6_walker_link(struct net *net, struct fib6_walker *w){	write_lock_bh(&net->ipv6.fib6_walker_lock);	list_add(&w->lh, &net->ipv6.fib6_walkers);	write_unlock_bh(&net->ipv6.fib6_walker_lock);} static void fib6_walker_unlink(struct net *net, struct fib6_walker *w){	write_lock_bh(&net->ipv6.fib6_walker_lock);	list_del_init(&w->lh);	write_unlock_bh(&net->ipv6.fib6_walker_lock);} static int fib6_new_sernum(struct net *net){	int new, old = atomic_read(&net->ipv6.fib6_sernum); 	do {		new = old < INT_MAX ? old + 1 : 1;	} while (!atomic_try_cmpxchg(&net->ipv6.fib6_sernum, &old, new)); 	return new;} enum {	FIB6_NO_SERNUM_CHANGE = 0,}; void fib6_update_sernum(struct net *net, struct fib6_info *f6i){	struct fib6_node *fn; 	fn = rcu_dereference_protected(f6i->fib6_node,			lockdep_is_held(&f6i->fib6_table->tb6_lock));	if (fn)		WRITE_ONCE(fn->fn_sernum, fib6_new_sernum(net));} /* *	Auxiliary address test functions for the radix tree. * *	These assume a 32bit processor (although it will work on *	64bit processors) */ /* *	test bit */#if defined(__LITTLE_ENDIAN)# define BITOP_BE32_SWIZZLE	(0x1F & ~7)#else# define BITOP_BE32_SWIZZLE	0#endif static __be32 addr_bit_set(const void *token, int fn_bit){	const __be32 *addr = token;	/*	 * Here,	 *	1 << ((~fn_bit ^ BITOP_BE32_SWIZZLE) & 0x1f)	 * is optimized version of	 *	htonl(1 << ((~fn_bit)&0x1F))	 * See include/asm-generic/bitops/le.h.	 */	return (__force __be32)(1 << ((~fn_bit ^ BITOP_BE32_SWIZZLE) & 0x1f)) &	       addr[fn_bit >> 5];} struct fib6_info *fib6_info_alloc(gfp_t gfp_flags, bool with_fib6_nh){	struct fib6_info *f6i;	size_t sz = sizeof(*f6i); 	if (with_fib6_nh)		sz += sizeof(struct fib6_nh); 	f6i = kzalloc(sz, gfp_flags);	if (!f6i)		return NULL; 	/* fib6_siblings is a union with nh_list, so this initializes both */	INIT_LIST_HEAD(&f6i->fib6_siblings);	refcount_set(&f6i->fib6_ref, 1); 	INIT_HLIST_NODE(&f6i->gc_link); 	return f6i;} void fib6_info_destroy_rcu(struct rcu_head *head){	struct fib6_info *f6i = container_of(head, struct fib6_info, rcu); 	WARN_ON(f6i->fib6_node); 	if (f6i->nh)		nexthop_put(f6i->nh);	else		fib6_nh_release(f6i->fib6_nh); 	ip_fib_metrics_put(f6i->fib6_metrics);	kfree(f6i);}EXPORT_SYMBOL_GPL(fib6_info_destroy_rcu); static struct fib6_node *node_alloc(struct net *net){	struct fib6_node *fn; 	fn = kmem_cache_zalloc(fib6_node_kmem, GFP_ATOMIC);	if (fn)		net->ipv6.rt6_stats->fib_nodes++; 	return fn;} static void node_free_immediate(struct net *net, struct fib6_node *fn){	kmem_cache_free(fib6_node_kmem, fn);	net->ipv6.rt6_stats->fib_nodes--;} static void node_free(struct net *net, struct fib6_node *fn){	kfree_rcu(fn, rcu);	net->ipv6.rt6_stats->fib_nodes--;} static void fib6_free_table(struct fib6_table *table){	inetpeer_invalidate_tree(&table->tb6_peers);	kfree(table);} static void fib6_link_table(struct net *net, struct fib6_table *tb){	unsigned int h; 	/*	 * Initialize table lock at a single place to give lockdep a key,	 * tables aren't visible prior to being linked to the list.	 */	spin_lock_init(&tb->tb6_lock);	h = tb->tb6_id & (FIB6_TABLE_HASHSZ - 1); 	/*	 * No protection necessary, this is the only list mutatation	 * operation, tables never disappear once they exist.	 */	hlist_add_head_rcu(&tb->tb6_hlist, &net->ipv6.fib_table_hash[h]);} #ifdef CONFIG_IPV6_MULTIPLE_TABLES static struct fib6_table *fib6_alloc_table(struct net *net, u32 id){	struct fib6_table *table; 	table = kzalloc_obj(*table, GFP_ATOMIC);	if (table) {		table->tb6_id = id;		rcu_assign_pointer(table->tb6_root.leaf,				   net->ipv6.fib6_null_entry);		table->tb6_root.fn_flags = RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;		inet_peer_base_init(&table->tb6_peers);		INIT_HLIST_HEAD(&table->tb6_gc_hlist);	} 	return table;} struct fib6_table *fib6_new_table(struct net *net, u32 id){	struct fib6_table *tb, *new_tb; 	if (id == 0)		id = RT6_TABLE_MAIN; 	tb = fib6_get_table(net, id);	if (tb)		return tb; 	new_tb = fib6_alloc_table(net, id);	if (!new_tb)		return NULL; 	spin_lock_bh(&net->ipv6.fib_table_hash_lock); 	tb = fib6_get_table(net, id);	if (unlikely(tb)) {		spin_unlock_bh(&net->ipv6.fib_table_hash_lock);		kfree(new_tb);		return tb;	} 	fib6_link_table(net, new_tb); 	spin_unlock_bh(&net->ipv6.fib_table_hash_lock); 	return new_tb;}EXPORT_SYMBOL_GPL(fib6_new_table); struct fib6_table *fib6_get_table(struct net *net, u32 id){	struct hlist_head *head;	struct fib6_table *tb; 	if (!id)		id = RT6_TABLE_MAIN; 	head = &net->ipv6.fib_table_hash[id & (FIB6_TABLE_HASHSZ - 1)]; 	/* See comment in fib6_link_table().  RCU is not required,	 * but rcu_dereference_raw() is used to avoid data-race.	 */	hlist_for_each_entry_rcu(tb, head, tb6_hlist, true)		if (tb->tb6_id == id)			return tb; 	return NULL;}EXPORT_SYMBOL_GPL(fib6_get_table); static void __net_init fib6_tables_init(struct net *net){	fib6_link_table(net, net->ipv6.fib6_main_tbl);	fib6_link_table(net, net->ipv6.fib6_local_tbl);}#else struct fib6_table *fib6_new_table(struct net *net, u32 id){	return fib6_get_table(net, id);} struct fib6_table *fib6_get_table(struct net *net, u32 id){	  return net->ipv6.fib6_main_tbl;} struct dst_entry *fib6_rule_lookup(struct net *net, struct flowi6 *fl6,				   const struct sk_buff *skb,				   int flags, pol_lookup_t lookup){	struct rt6_info *rt; 	rt = pol_lookup_func(lookup,			net, net->ipv6.fib6_main_tbl, fl6, skb, flags);	if (rt->dst.error == -EAGAIN) {		ip6_rt_put_flags(rt, flags);		rt = net->ipv6.ip6_null_entry;		if (!(flags & RT6_LOOKUP_F_DST_NOREF))			dst_hold(&rt->dst);	} 	return &rt->dst;} /* called with rcu lock held; no reference taken on fib6_info */int fib6_lookup(struct net *net, int oif, struct flowi6 *fl6,		struct fib6_result *res, int flags){	return fib6_table_lookup(net, net->ipv6.fib6_main_tbl, oif, fl6,				 res, flags);}#if IS_MODULE(CONFIG_NFT_FIB_IPV6)EXPORT_SYMBOL_GPL(fib6_lookup);#endif static void __net_init fib6_tables_init(struct net *net){	fib6_link_table(net, net->ipv6.fib6_main_tbl);} #endif unsigned int fib6_tables_seq_read(const struct net *net){	unsigned int h, fib_seq = 0; 	rcu_read_lock();	for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {		const struct hlist_head *head = &net->ipv6.fib_table_hash[h];		const struct fib6_table *tb; 		hlist_for_each_entry_rcu(tb, head, tb6_hlist)			fib_seq += READ_ONCE(tb->fib_seq);	}	rcu_read_unlock(); 	return fib_seq;} static int call_fib6_entry_notifier(struct notifier_block *nb,				    enum fib_event_type event_type,				    struct fib6_info *rt,				    struct netlink_ext_ack *extack){	struct fib6_entry_notifier_info info = {		.info.extack = extack,		.rt = rt,	}; 	return call_fib6_notifier(nb, event_type, &info.info);} static int call_fib6_multipath_entry_notifier(struct notifier_block *nb,					      enum fib_event_type event_type,					      struct fib6_info *rt,					      unsigned int nsiblings,					      struct netlink_ext_ack *extack){	struct fib6_entry_notifier_info info = {		.info.extack = extack,		.rt = rt,		.nsiblings = nsiblings,	}; 	return call_fib6_notifier(nb, event_type, &info.info);} int call_fib6_entry_notifiers(struct net *net,			      enum fib_event_type event_type,			      struct fib6_info *rt,			      struct netlink_ext_ack *extack){	struct fib6_entry_notifier_info info = {		.info.extack = extack,		.rt = rt,	}; 	WRITE_ONCE(rt->fib6_table->fib_seq, rt->fib6_table->fib_seq + 1);	return call_fib6_notifiers(net, event_type, &info.info);} int call_fib6_multipath_entry_notifiers(struct net *net,					enum fib_event_type event_type,					struct fib6_info *rt,					unsigned int nsiblings,					struct netlink_ext_ack *extack){	struct fib6_entry_notifier_info info = {		.info.extack = extack,		.rt = rt,		.nsiblings = nsiblings,	}; 	WRITE_ONCE(rt->fib6_table->fib_seq, rt->fib6_table->fib_seq + 1);	return call_fib6_notifiers(net, event_type, &info.info);} int call_fib6_entry_notifiers_replace(struct net *net, struct fib6_info *rt){	struct fib6_entry_notifier_info info = {		.rt = rt,		.nsiblings = rt->fib6_nsiblings,	}; 	WRITE_ONCE(rt->fib6_table->fib_seq, rt->fib6_table->fib_seq + 1);	return call_fib6_notifiers(net, FIB_EVENT_ENTRY_REPLACE, &info.info);} struct fib6_dump_arg {	struct net *net;	struct notifier_block *nb;	struct netlink_ext_ack *extack;}; static int fib6_rt_dump(struct fib6_info *rt, struct fib6_dump_arg *arg){	enum fib_event_type fib_event = FIB_EVENT_ENTRY_REPLACE;	unsigned int nsiblings;	int err; 	if (!rt || rt == arg->net->ipv6.fib6_null_entry)		return 0; 	nsiblings = READ_ONCE(rt->fib6_nsiblings);	if (nsiblings)		err = call_fib6_multipath_entry_notifier(arg->nb, fib_event,							 rt,							 nsiblings,							 arg->extack);	else		err = call_fib6_entry_notifier(arg->nb, fib_event, rt,					       arg->extack); 	return err;} static int fib6_node_dump(struct fib6_walker *w){	int err; 	err = fib6_rt_dump(w->leaf, w->args);	w->leaf = NULL;	return err;} static int fib6_table_dump(struct net *net, struct fib6_table *tb,			   struct fib6_walker *w){	int err; 	w->root = &tb->tb6_root;	spin_lock_bh(&tb->tb6_lock);	err = fib6_walk(net, w);	spin_unlock_bh(&tb->tb6_lock);	return err;} /* Called with rcu_read_lock() */int fib6_tables_dump(struct net *net, struct notifier_block *nb,		     struct netlink_ext_ack *extack){	struct fib6_dump_arg arg;	struct fib6_walker *w;	unsigned int h;	int err = 0; 	w = kzalloc_obj(*w, GFP_ATOMIC);

Lines 1–500 of 2840.

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t