3409 lines · 82.4 KB · C
RawDownload
// SPDX-License-Identifier: GPL-2.0-or-later/* *	IP multicast routing support for mrouted 3.6/3.8 * *		(c) 1995 Alan Cox, <alan@lxorguk.ukuu.org.uk> *	  Linux Consultancy and Custom Driver Development * *	Fixes: *	Michael Chastain	:	Incorrect size of copying. *	Alan Cox		:	Added the cache manager code *	Alan Cox		:	Fixed the clone/copy bug and device race. *	Mike McLagan		:	Routing by source *	Malcolm Beattie		:	Buffer handling fixes. *	Alexey Kuznetsov	:	Double buffer free and other fixes. *	SVR Anand		:	Fixed several multicast bugs and problems. *	Alexey Kuznetsov	:	Status, optimisations and more. *	Brad Parker		:	Better behaviour on mrouted upcall *					overflow. *      Carlos Picoto           :       PIMv1 Support *	Pavlin Ivanov Radoslavov:	PIMv2 Registers must checksum only PIM header *					Relax this requirement to work with older peers. */ #include <linux/uaccess.h>#include <linux/types.h>#include <linux/cache.h>#include <linux/capability.h>#include <linux/errno.h>#include <linux/mm.h>#include <linux/kernel.h>#include <linux/fcntl.h>#include <linux/stat.h>#include <linux/socket.h>#include <linux/in.h>#include <linux/inet.h>#include <linux/netdevice.h>#include <linux/inetdevice.h>#include <linux/igmp.h>#include <linux/proc_fs.h>#include <linux/seq_file.h>#include <linux/mroute.h>#include <linux/init.h>#include <linux/if_ether.h>#include <linux/slab.h>#include <net/flow.h>#include <net/net_namespace.h>#include <net/ip.h>#include <net/protocol.h>#include <linux/skbuff.h>#include <net/route.h>#include <net/icmp.h>#include <net/udp.h>#include <net/raw.h>#include <linux/notifier.h>#include <linux/if_arp.h>#include <linux/netfilter_ipv4.h>#include <linux/compat.h>#include <linux/export.h>#include <linux/rhashtable.h>#include <net/ip_tunnels.h>#include <net/checksum.h>#include <net/netlink.h>#include <net/fib_rules.h>#include <linux/netconf.h>#include <net/rtnh.h>#include <net/inet_dscp.h> #include <linux/nospec.h> struct ipmr_rule {	struct fib_rule		common;}; struct ipmr_result {	struct mr_table		*mrt;}; /* Big lock, protecting vif table, mrt cache and mroute socket state. * Note that the changes are semaphored via rtnl_lock. */ static DEFINE_SPINLOCK(mrt_lock); static struct net_device *vif_dev_read(const struct vif_device *vif){	return rcu_dereference(vif->dev);} /* Multicast router control variables */ /* Special spinlock for queue of unresolved entries */static DEFINE_SPINLOCK(mfc_unres_lock); /* We return to original Alan's scheme. Hash table of resolved * entries is changed only in process context and protected * with weak lock mrt_lock. Queue of unresolved entries is protected * with strong spinlock mfc_unres_lock. * * In this case data path is free of exclusive locks at all. */ static struct kmem_cache *mrt_cachep __ro_after_init; static struct mr_table *ipmr_new_table(struct net *net, u32 id);static void ipmr_free_table(struct mr_table *mrt,			    struct list_head *dev_kill_list); static void ip_mr_forward(struct net *net, struct mr_table *mrt,			  struct net_device *dev, struct sk_buff *skb,			  struct mfc_cache *cache, int local);static int ipmr_cache_report(const struct mr_table *mrt,			     struct sk_buff *pkt, vifi_t vifi, int assert);static void mroute_netlink_event(struct mr_table *mrt, struct mfc_cache *mfc,				 int cmd);static void igmpmsg_netlink_event(const struct mr_table *mrt, struct sk_buff *pkt);static void mroute_clean_tables(struct mr_table *mrt, int flags,				struct list_head *dev_kill_list);static void ipmr_expire_process(struct timer_list *t); #ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES#define ipmr_for_each_table(mrt, net)					\	list_for_each_entry_rcu(mrt, &net->ipv4.mr_tables, list,	\				lockdep_rtnl_is_held() ||		\				list_empty(&net->ipv4.mr_tables)) static struct mr_table *ipmr_mr_table_iter(struct net *net,					   struct mr_table *mrt){	struct mr_table *ret; 	if (!mrt)		ret = list_entry_rcu(net->ipv4.mr_tables.next,				     struct mr_table, list);	else		ret = list_entry_rcu(mrt->list.next,				     struct mr_table, list); 	if (&ret->list == &net->ipv4.mr_tables)		return NULL;	return ret;} static struct mr_table *__ipmr_get_table(struct net *net, u32 id){	struct mr_table *mrt; 	ipmr_for_each_table(mrt, net) {		if (mrt->id == id)			return mrt;	}	return NULL;} static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4,			   struct mr_table **mrt){	int err;	struct ipmr_result res;	struct fib_lookup_arg arg = {		.result = &res,		.flags = FIB_LOOKUP_NOREF,	}; 	/* update flow if oif or iif point to device enslaved to l3mdev */	l3mdev_update_flow(net, flowi4_to_flowi(flp4)); 	err = fib_rules_lookup(net->ipv4.mr_rules_ops,			       flowi4_to_flowi(flp4), 0, &arg);	if (err < 0)		return err;	*mrt = res.mrt;	return 0;} static int ipmr_rule_action(struct fib_rule *rule, struct flowi *flp,			    int flags, struct fib_lookup_arg *arg){	struct ipmr_result *res = arg->result;	struct mr_table *mrt; 	switch (rule->action) {	case FR_ACT_TO_TBL:		break;	case FR_ACT_UNREACHABLE:		return -ENETUNREACH;	case FR_ACT_PROHIBIT:		return -EACCES;	case FR_ACT_BLACKHOLE:	default:		return -EINVAL;	} 	arg->table = fib_rule_get_table(rule, arg); 	mrt = __ipmr_get_table(rule->fr_net, arg->table);	if (!mrt)		return -EAGAIN;	res->mrt = mrt;	return 0;} static int ipmr_rule_match(struct fib_rule *rule, struct flowi *fl, int flags){	return 1;} static int ipmr_rule_configure(struct fib_rule *rule, struct sk_buff *skb,			       struct fib_rule_hdr *frh, struct nlattr **tb,			       struct netlink_ext_ack *extack){	return 0;} static int ipmr_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,			     struct nlattr **tb){	return 1;} static int ipmr_rule_fill(struct fib_rule *rule, struct sk_buff *skb,			  struct fib_rule_hdr *frh){	frh->dst_len = 0;	frh->src_len = 0;	frh->tos     = 0;	return 0;} static const struct fib_rules_ops __net_initconst ipmr_rules_ops_template = {	.family		= RTNL_FAMILY_IPMR,	.rule_size	= sizeof(struct ipmr_rule),	.addr_size	= sizeof(u32),	.action		= ipmr_rule_action,	.match		= ipmr_rule_match,	.configure	= ipmr_rule_configure,	.compare	= ipmr_rule_compare,	.fill		= ipmr_rule_fill,	.nlgroup	= RTNLGRP_IPV4_RULE,	.owner		= THIS_MODULE,}; static int __net_init ipmr_rules_init(struct net *net){	struct fib_rules_ops *ops;	LIST_HEAD(dev_kill_list);	struct mr_table *mrt;	int err; 	ops = fib_rules_register(&ipmr_rules_ops_template, net);	if (IS_ERR(ops))		return PTR_ERR(ops); 	INIT_LIST_HEAD(&net->ipv4.mr_tables); 	mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);	if (IS_ERR(mrt)) {		err = PTR_ERR(mrt);		goto err1;	} 	err = fib_default_rule_add(ops, 0x7fff, RT_TABLE_DEFAULT);	if (err < 0)		goto err2; 	net->ipv4.mr_rules_ops = ops;	return 0; err2:	ipmr_free_table(mrt, &dev_kill_list);err1:	fib_rules_unregister(ops);	return err;} static void __net_exit ipmr_rules_exit(struct net *net){	fib_rules_unregister(net->ipv4.mr_rules_ops);} static void __net_exit ipmr_rules_exit_rtnl(struct net *net,					    struct list_head *dev_kill_list){	struct mr_table *mrt, *next; 	list_for_each_entry_safe(mrt, next, &net->ipv4.mr_tables, list) {		list_del_rcu(&mrt->list);		ipmr_free_table(mrt, dev_kill_list);	}} static int ipmr_rules_dump(struct net *net, struct notifier_block *nb,			   struct netlink_ext_ack *extack){	return fib_rules_dump(net, nb, RTNL_FAMILY_IPMR, extack);} static unsigned int ipmr_rules_seq_read(const struct net *net){	return fib_rules_seq_read(net, RTNL_FAMILY_IPMR);} bool ipmr_rule_default(const struct fib_rule *rule){	return fib_rule_matchall(rule) && rule->table == RT_TABLE_DEFAULT;}EXPORT_SYMBOL(ipmr_rule_default);#elsestatic struct mr_table *ipmr_mr_table_iter(struct net *net,					   struct mr_table *mrt){	if (!mrt)		return rcu_dereference(net->ipv4.mrt);	return NULL;} static struct mr_table *__ipmr_get_table(struct net *net, u32 id){	return rcu_dereference_check(net->ipv4.mrt,				     lockdep_rtnl_is_held() ||				     !rcu_access_pointer(net->ipv4.mrt));} #define ipmr_for_each_table(mrt, net)				\	for (mrt = __ipmr_get_table(net, 0); mrt; mrt = NULL) static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4,			   struct mr_table **mrt){	*mrt = rcu_dereference(net->ipv4.mrt);	if (!*mrt)		return -EAGAIN;	return 0;} static int __net_init ipmr_rules_init(struct net *net){	struct mr_table *mrt; 	mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);	if (IS_ERR(mrt))		return PTR_ERR(mrt); 	rcu_assign_pointer(net->ipv4.mrt, mrt);	return 0;} static void __net_exit ipmr_rules_exit(struct net *net){} static void __net_exit ipmr_rules_exit_rtnl(struct net *net,					    struct list_head *dev_kill_list){	struct mr_table *mrt = rcu_dereference_protected(net->ipv4.mrt, 1); 	RCU_INIT_POINTER(net->ipv4.mrt, NULL);	ipmr_free_table(mrt, dev_kill_list);} static int ipmr_rules_dump(struct net *net, struct notifier_block *nb,			   struct netlink_ext_ack *extack){	return 0;} static unsigned int ipmr_rules_seq_read(const struct net *net){	return 0;} bool ipmr_rule_default(const struct fib_rule *rule){	return true;}EXPORT_SYMBOL(ipmr_rule_default);#endif static struct mr_table *ipmr_get_table(struct net *net, u32 id){	struct mr_table *mrt; 	rcu_read_lock();	mrt = __ipmr_get_table(net, id);	rcu_read_unlock(); 	return mrt;} static inline int ipmr_hash_cmp(struct rhashtable_compare_arg *arg,				const void *ptr){	const struct mfc_cache_cmp_arg *cmparg = arg->key;	const struct mfc_cache *c = ptr; 	return cmparg->mfc_mcastgrp != c->mfc_mcastgrp ||	       cmparg->mfc_origin != c->mfc_origin;} static const struct rhashtable_params ipmr_rht_params = {	.head_offset = offsetof(struct mr_mfc, mnode),	.key_offset = offsetof(struct mfc_cache, cmparg),	.key_len = sizeof(struct mfc_cache_cmp_arg),	.nelem_hint = 3,	.obj_cmpfn = ipmr_hash_cmp,	.automatic_shrinking = true,}; static void ipmr_new_table_set(struct mr_table *mrt,			       struct net *net){#ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES	list_add_tail_rcu(&mrt->list, &net->ipv4.mr_tables);#endif} static struct mfc_cache_cmp_arg ipmr_mr_table_ops_cmparg_any = {	.mfc_mcastgrp = htonl(INADDR_ANY),	.mfc_origin = htonl(INADDR_ANY),}; static struct mr_table_ops ipmr_mr_table_ops = {	.rht_params = &ipmr_rht_params,	.cmparg_any = &ipmr_mr_table_ops_cmparg_any,}; static struct mr_table *ipmr_new_table(struct net *net, u32 id){	struct mr_table *mrt; 	/* "pimreg%u" should not exceed 16 bytes (IFNAMSIZ) */	if (id != RT_TABLE_DEFAULT && id >= 1000000000)		return ERR_PTR(-EINVAL); 	mrt = __ipmr_get_table(net, id);	if (mrt)		return mrt; 	return mr_table_alloc(net, id, &ipmr_mr_table_ops,			      ipmr_expire_process, ipmr_new_table_set);} static void ipmr_free_table(struct mr_table *mrt, struct list_head *dev_kill_list){	struct net *net = read_pnet(&mrt->net);	LIST_HEAD(ipmr_dev_kill_list); 	WARN_ON_ONCE(!mr_can_free_table(net)); 	mroute_clean_tables(mrt, MRT_FLUSH_VIFS | MRT_FLUSH_VIFS_STATIC |			    MRT_FLUSH_MFC | MRT_FLUSH_MFC_STATIC,			    &ipmr_dev_kill_list);	timer_shutdown_sync(&mrt->ipmr_expire_timer);	mr_table_free(mrt); 	WARN_ON_ONCE(!net_initialized(net) && !list_empty(&ipmr_dev_kill_list));	list_splice(&ipmr_dev_kill_list, dev_kill_list);} /* Service routines creating virtual interfaces: DVMRP tunnels and PIMREG */ /* Initialize ipmr pimreg/tunnel in_device */static bool ipmr_init_vif_indev(const struct net_device *dev){	struct in_device *in_dev; 	ASSERT_RTNL(); 	in_dev = __in_dev_get_rtnl(dev);	if (!in_dev)		return false;	ipv4_devconf_setall(in_dev);	neigh_parms_data_state_setall(in_dev->arp_parms);	IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0; 	return true;} static struct net_device *ipmr_new_tunnel(struct net *net, struct vifctl *v){	struct net_device *tunnel_dev, *new_dev;	struct ip_tunnel_parm_kern p = { };	int err; 	tunnel_dev = __dev_get_by_name(net, "tunl0");	if (!tunnel_dev)		goto out; 	p.iph.daddr = v->vifc_rmt_addr.s_addr;	p.iph.saddr = v->vifc_lcl_addr.s_addr;	p.iph.version = 4;	p.iph.ihl = 5;	p.iph.protocol = IPPROTO_IPIP;	sprintf(p.name, "dvmrp%d", v->vifc_vifi); 	if (!tunnel_dev->netdev_ops->ndo_tunnel_ctl)		goto out;	err = tunnel_dev->netdev_ops->ndo_tunnel_ctl(tunnel_dev, &p,			SIOCADDTUNNEL);	if (err)		goto out;

Lines 1–500 of 3409.