Linux Audio

Check our new training course

Linux debugging, profiling, tracing and performance analysis training

Mar 24-27, 2025, special US time zones
Register
Loading...
v5.4
   1// SPDX-License-Identifier: GPL-2.0-or-later
   2/*
   3 *	IP multicast routing support for mrouted 3.6/3.8
   4 *
   5 *		(c) 1995 Alan Cox, <alan@lxorguk.ukuu.org.uk>
   6 *	  Linux Consultancy and Custom Driver Development
   7 *
   8 *	Fixes:
   9 *	Michael Chastain	:	Incorrect size of copying.
  10 *	Alan Cox		:	Added the cache manager code
  11 *	Alan Cox		:	Fixed the clone/copy bug and device race.
  12 *	Mike McLagan		:	Routing by source
  13 *	Malcolm Beattie		:	Buffer handling fixes.
  14 *	Alexey Kuznetsov	:	Double buffer free and other fixes.
  15 *	SVR Anand		:	Fixed several multicast bugs and problems.
  16 *	Alexey Kuznetsov	:	Status, optimisations and more.
  17 *	Brad Parker		:	Better behaviour on mrouted upcall
  18 *					overflow.
  19 *      Carlos Picoto           :       PIMv1 Support
  20 *	Pavlin Ivanov Radoslavov:	PIMv2 Registers must checksum only PIM header
  21 *					Relax this requirement to work with older peers.
  22 */
  23
  24#include <linux/uaccess.h>
  25#include <linux/types.h>
  26#include <linux/cache.h>
  27#include <linux/capability.h>
  28#include <linux/errno.h>
  29#include <linux/mm.h>
  30#include <linux/kernel.h>
  31#include <linux/fcntl.h>
  32#include <linux/stat.h>
  33#include <linux/socket.h>
  34#include <linux/in.h>
  35#include <linux/inet.h>
  36#include <linux/netdevice.h>
  37#include <linux/inetdevice.h>
  38#include <linux/igmp.h>
  39#include <linux/proc_fs.h>
  40#include <linux/seq_file.h>
  41#include <linux/mroute.h>
  42#include <linux/init.h>
  43#include <linux/if_ether.h>
  44#include <linux/slab.h>
  45#include <net/net_namespace.h>
  46#include <net/ip.h>
  47#include <net/protocol.h>
  48#include <linux/skbuff.h>
  49#include <net/route.h>
  50#include <net/icmp.h>
  51#include <net/udp.h>
  52#include <net/raw.h>
  53#include <linux/notifier.h>
  54#include <linux/if_arp.h>
  55#include <linux/netfilter_ipv4.h>
  56#include <linux/compat.h>
  57#include <linux/export.h>
  58#include <linux/rhashtable.h>
  59#include <net/ip_tunnels.h>
  60#include <net/checksum.h>
  61#include <net/netlink.h>
  62#include <net/fib_rules.h>
  63#include <linux/netconf.h>
  64#include <net/rtnh.h>
 
  65
  66#include <linux/nospec.h>
  67
  68struct ipmr_rule {
  69	struct fib_rule		common;
  70};
  71
  72struct ipmr_result {
  73	struct mr_table		*mrt;
  74};
  75
  76/* Big lock, protecting vif table, mrt cache and mroute socket state.
  77 * Note that the changes are semaphored via rtnl_lock.
  78 */
  79
  80static DEFINE_RWLOCK(mrt_lock);
 
 
 
 
 
  81
  82/* Multicast router control variables */
  83
  84/* Special spinlock for queue of unresolved entries */
  85static DEFINE_SPINLOCK(mfc_unres_lock);
  86
  87/* We return to original Alan's scheme. Hash table of resolved
  88 * entries is changed only in process context and protected
  89 * with weak lock mrt_lock. Queue of unresolved entries is protected
  90 * with strong spinlock mfc_unres_lock.
  91 *
  92 * In this case data path is free of exclusive locks at all.
  93 */
  94
  95static struct kmem_cache *mrt_cachep __ro_after_init;
  96
  97static struct mr_table *ipmr_new_table(struct net *net, u32 id);
  98static void ipmr_free_table(struct mr_table *mrt);
  99
 100static void ip_mr_forward(struct net *net, struct mr_table *mrt,
 101			  struct net_device *dev, struct sk_buff *skb,
 102			  struct mfc_cache *cache, int local);
 103static int ipmr_cache_report(struct mr_table *mrt,
 104			     struct sk_buff *pkt, vifi_t vifi, int assert);
 105static void mroute_netlink_event(struct mr_table *mrt, struct mfc_cache *mfc,
 106				 int cmd);
 107static void igmpmsg_netlink_event(struct mr_table *mrt, struct sk_buff *pkt);
 108static void mroute_clean_tables(struct mr_table *mrt, int flags);
 109static void ipmr_expire_process(struct timer_list *t);
 110
 111#ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
 112#define ipmr_for_each_table(mrt, net) \
 113	list_for_each_entry_rcu(mrt, &net->ipv4.mr_tables, list)
 
 
 
 
 
 
 
 114
 115static struct mr_table *ipmr_mr_table_iter(struct net *net,
 116					   struct mr_table *mrt)
 117{
 118	struct mr_table *ret;
 119
 120	if (!mrt)
 121		ret = list_entry_rcu(net->ipv4.mr_tables.next,
 122				     struct mr_table, list);
 123	else
 124		ret = list_entry_rcu(mrt->list.next,
 125				     struct mr_table, list);
 126
 127	if (&ret->list == &net->ipv4.mr_tables)
 128		return NULL;
 129	return ret;
 130}
 131
 132static struct mr_table *ipmr_get_table(struct net *net, u32 id)
 133{
 134	struct mr_table *mrt;
 135
 136	ipmr_for_each_table(mrt, net) {
 137		if (mrt->id == id)
 138			return mrt;
 139	}
 140	return NULL;
 141}
 142
 
 
 
 
 
 
 
 
 
 
 143static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4,
 144			   struct mr_table **mrt)
 145{
 146	int err;
 147	struct ipmr_result res;
 148	struct fib_lookup_arg arg = {
 149		.result = &res,
 150		.flags = FIB_LOOKUP_NOREF,
 151	};
 152
 153	/* update flow if oif or iif point to device enslaved to l3mdev */
 154	l3mdev_update_flow(net, flowi4_to_flowi(flp4));
 155
 156	err = fib_rules_lookup(net->ipv4.mr_rules_ops,
 157			       flowi4_to_flowi(flp4), 0, &arg);
 158	if (err < 0)
 159		return err;
 160	*mrt = res.mrt;
 161	return 0;
 162}
 163
 164static int ipmr_rule_action(struct fib_rule *rule, struct flowi *flp,
 165			    int flags, struct fib_lookup_arg *arg)
 166{
 167	struct ipmr_result *res = arg->result;
 168	struct mr_table *mrt;
 169
 170	switch (rule->action) {
 171	case FR_ACT_TO_TBL:
 172		break;
 173	case FR_ACT_UNREACHABLE:
 174		return -ENETUNREACH;
 175	case FR_ACT_PROHIBIT:
 176		return -EACCES;
 177	case FR_ACT_BLACKHOLE:
 178	default:
 179		return -EINVAL;
 180	}
 181
 182	arg->table = fib_rule_get_table(rule, arg);
 183
 184	mrt = ipmr_get_table(rule->fr_net, arg->table);
 185	if (!mrt)
 186		return -EAGAIN;
 187	res->mrt = mrt;
 188	return 0;
 189}
 190
 191static int ipmr_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
 192{
 193	return 1;
 194}
 195
 196static const struct nla_policy ipmr_rule_policy[FRA_MAX + 1] = {
 197	FRA_GENERIC_POLICY,
 198};
 199
 200static int ipmr_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
 201			       struct fib_rule_hdr *frh, struct nlattr **tb,
 202			       struct netlink_ext_ack *extack)
 203{
 204	return 0;
 205}
 206
 207static int ipmr_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
 208			     struct nlattr **tb)
 209{
 210	return 1;
 211}
 212
 213static int ipmr_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
 214			  struct fib_rule_hdr *frh)
 215{
 216	frh->dst_len = 0;
 217	frh->src_len = 0;
 218	frh->tos     = 0;
 219	return 0;
 220}
 221
 222static const struct fib_rules_ops __net_initconst ipmr_rules_ops_template = {
 223	.family		= RTNL_FAMILY_IPMR,
 224	.rule_size	= sizeof(struct ipmr_rule),
 225	.addr_size	= sizeof(u32),
 226	.action		= ipmr_rule_action,
 227	.match		= ipmr_rule_match,
 228	.configure	= ipmr_rule_configure,
 229	.compare	= ipmr_rule_compare,
 230	.fill		= ipmr_rule_fill,
 231	.nlgroup	= RTNLGRP_IPV4_RULE,
 232	.policy		= ipmr_rule_policy,
 233	.owner		= THIS_MODULE,
 234};
 235
 236static int __net_init ipmr_rules_init(struct net *net)
 237{
 238	struct fib_rules_ops *ops;
 239	struct mr_table *mrt;
 240	int err;
 241
 242	ops = fib_rules_register(&ipmr_rules_ops_template, net);
 243	if (IS_ERR(ops))
 244		return PTR_ERR(ops);
 245
 246	INIT_LIST_HEAD(&net->ipv4.mr_tables);
 247
 248	mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
 249	if (IS_ERR(mrt)) {
 250		err = PTR_ERR(mrt);
 251		goto err1;
 252	}
 253
 254	err = fib_default_rule_add(ops, 0x7fff, RT_TABLE_DEFAULT, 0);
 255	if (err < 0)
 256		goto err2;
 257
 258	net->ipv4.mr_rules_ops = ops;
 259	return 0;
 260
 261err2:
 
 262	ipmr_free_table(mrt);
 
 263err1:
 264	fib_rules_unregister(ops);
 265	return err;
 266}
 267
 268static void __net_exit ipmr_rules_exit(struct net *net)
 269{
 270	struct mr_table *mrt, *next;
 271
 272	rtnl_lock();
 273	list_for_each_entry_safe(mrt, next, &net->ipv4.mr_tables, list) {
 274		list_del(&mrt->list);
 275		ipmr_free_table(mrt);
 276	}
 277	fib_rules_unregister(net->ipv4.mr_rules_ops);
 278	rtnl_unlock();
 279}
 280
 281static int ipmr_rules_dump(struct net *net, struct notifier_block *nb)
 
 282{
 283	return fib_rules_dump(net, nb, RTNL_FAMILY_IPMR);
 284}
 285
 286static unsigned int ipmr_rules_seq_read(struct net *net)
 287{
 288	return fib_rules_seq_read(net, RTNL_FAMILY_IPMR);
 289}
 290
 291bool ipmr_rule_default(const struct fib_rule *rule)
 292{
 293	return fib_rule_matchall(rule) && rule->table == RT_TABLE_DEFAULT;
 294}
 295EXPORT_SYMBOL(ipmr_rule_default);
 296#else
 297#define ipmr_for_each_table(mrt, net) \
 298	for (mrt = net->ipv4.mrt; mrt; mrt = NULL)
 299
 
 
 
 
 
 300static struct mr_table *ipmr_mr_table_iter(struct net *net,
 301					   struct mr_table *mrt)
 302{
 303	if (!mrt)
 304		return net->ipv4.mrt;
 305	return NULL;
 306}
 307
 308static struct mr_table *ipmr_get_table(struct net *net, u32 id)
 309{
 310	return net->ipv4.mrt;
 311}
 312
 
 
 313static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4,
 314			   struct mr_table **mrt)
 315{
 316	*mrt = net->ipv4.mrt;
 317	return 0;
 318}
 319
 320static int __net_init ipmr_rules_init(struct net *net)
 321{
 322	struct mr_table *mrt;
 323
 324	mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
 325	if (IS_ERR(mrt))
 326		return PTR_ERR(mrt);
 327	net->ipv4.mrt = mrt;
 328	return 0;
 329}
 330
 331static void __net_exit ipmr_rules_exit(struct net *net)
 332{
 333	rtnl_lock();
 334	ipmr_free_table(net->ipv4.mrt);
 335	net->ipv4.mrt = NULL;
 336	rtnl_unlock();
 337}
 338
 339static int ipmr_rules_dump(struct net *net, struct notifier_block *nb)
 
 340{
 341	return 0;
 342}
 343
 344static unsigned int ipmr_rules_seq_read(struct net *net)
 345{
 346	return 0;
 347}
 348
 349bool ipmr_rule_default(const struct fib_rule *rule)
 350{
 351	return true;
 352}
 353EXPORT_SYMBOL(ipmr_rule_default);
 354#endif
 355
 356static inline int ipmr_hash_cmp(struct rhashtable_compare_arg *arg,
 357				const void *ptr)
 358{
 359	const struct mfc_cache_cmp_arg *cmparg = arg->key;
 360	struct mfc_cache *c = (struct mfc_cache *)ptr;
 361
 362	return cmparg->mfc_mcastgrp != c->mfc_mcastgrp ||
 363	       cmparg->mfc_origin != c->mfc_origin;
 364}
 365
 366static const struct rhashtable_params ipmr_rht_params = {
 367	.head_offset = offsetof(struct mr_mfc, mnode),
 368	.key_offset = offsetof(struct mfc_cache, cmparg),
 369	.key_len = sizeof(struct mfc_cache_cmp_arg),
 370	.nelem_hint = 3,
 371	.obj_cmpfn = ipmr_hash_cmp,
 372	.automatic_shrinking = true,
 373};
 374
 375static void ipmr_new_table_set(struct mr_table *mrt,
 376			       struct net *net)
 377{
 378#ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
 379	list_add_tail_rcu(&mrt->list, &net->ipv4.mr_tables);
 380#endif
 381}
 382
 383static struct mfc_cache_cmp_arg ipmr_mr_table_ops_cmparg_any = {
 384	.mfc_mcastgrp = htonl(INADDR_ANY),
 385	.mfc_origin = htonl(INADDR_ANY),
 386};
 387
 388static struct mr_table_ops ipmr_mr_table_ops = {
 389	.rht_params = &ipmr_rht_params,
 390	.cmparg_any = &ipmr_mr_table_ops_cmparg_any,
 391};
 392
 393static struct mr_table *ipmr_new_table(struct net *net, u32 id)
 394{
 395	struct mr_table *mrt;
 396
 397	/* "pimreg%u" should not exceed 16 bytes (IFNAMSIZ) */
 398	if (id != RT_TABLE_DEFAULT && id >= 1000000000)
 399		return ERR_PTR(-EINVAL);
 400
 401	mrt = ipmr_get_table(net, id);
 402	if (mrt)
 403		return mrt;
 404
 405	return mr_table_alloc(net, id, &ipmr_mr_table_ops,
 406			      ipmr_expire_process, ipmr_new_table_set);
 407}
 408
 409static void ipmr_free_table(struct mr_table *mrt)
 410{
 411	del_timer_sync(&mrt->ipmr_expire_timer);
 
 
 
 
 412	mroute_clean_tables(mrt, MRT_FLUSH_VIFS | MRT_FLUSH_VIFS_STATIC |
 413				 MRT_FLUSH_MFC | MRT_FLUSH_MFC_STATIC);
 414	rhltable_destroy(&mrt->mfc_hash);
 415	kfree(mrt);
 416}
 417
 418/* Service routines creating virtual interfaces: DVMRP tunnels and PIMREG */
 419
 420static void ipmr_del_tunnel(struct net_device *dev, struct vifctl *v)
 421{
 422	struct net *net = dev_net(dev);
 423
 424	dev_close(dev);
 425
 426	dev = __dev_get_by_name(net, "tunl0");
 427	if (dev) {
 428		const struct net_device_ops *ops = dev->netdev_ops;
 429		struct ifreq ifr;
 430		struct ip_tunnel_parm p;
 431
 432		memset(&p, 0, sizeof(p));
 433		p.iph.daddr = v->vifc_rmt_addr.s_addr;
 434		p.iph.saddr = v->vifc_lcl_addr.s_addr;
 435		p.iph.version = 4;
 436		p.iph.ihl = 5;
 437		p.iph.protocol = IPPROTO_IPIP;
 438		sprintf(p.name, "dvmrp%d", v->vifc_vifi);
 439		ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
 440
 441		if (ops->ndo_do_ioctl) {
 442			mm_segment_t oldfs = get_fs();
 443
 444			set_fs(KERNEL_DS);
 445			ops->ndo_do_ioctl(dev, &ifr, SIOCDELTUNNEL);
 446			set_fs(oldfs);
 447		}
 448	}
 449}
 450
 451/* Initialize ipmr pimreg/tunnel in_device */
 452static bool ipmr_init_vif_indev(const struct net_device *dev)
 453{
 454	struct in_device *in_dev;
 455
 456	ASSERT_RTNL();
 457
 458	in_dev = __in_dev_get_rtnl(dev);
 459	if (!in_dev)
 460		return false;
 461	ipv4_devconf_setall(in_dev);
 462	neigh_parms_data_state_setall(in_dev->arp_parms);
 463	IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0;
 464
 465	return true;
 466}
 467
 468static struct net_device *ipmr_new_tunnel(struct net *net, struct vifctl *v)
 469{
 470	struct net_device  *dev;
 
 
 471
 472	dev = __dev_get_by_name(net, "tunl0");
 
 
 473
 474	if (dev) {
 475		const struct net_device_ops *ops = dev->netdev_ops;
 476		int err;
 477		struct ifreq ifr;
 478		struct ip_tunnel_parm p;
 479
 480		memset(&p, 0, sizeof(p));
 481		p.iph.daddr = v->vifc_rmt_addr.s_addr;
 482		p.iph.saddr = v->vifc_lcl_addr.s_addr;
 483		p.iph.version = 4;
 484		p.iph.ihl = 5;
 485		p.iph.protocol = IPPROTO_IPIP;
 486		sprintf(p.name, "dvmrp%d", v->vifc_vifi);
 487		ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
 488
 489		if (ops->ndo_do_ioctl) {
 490			mm_segment_t oldfs = get_fs();
 491
 492			set_fs(KERNEL_DS);
 493			err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
 494			set_fs(oldfs);
 495		} else {
 496			err = -EOPNOTSUPP;
 497		}
 498		dev = NULL;
 499
 500		if (err == 0 &&
 501		    (dev = __dev_get_by_name(net, p.name)) != NULL) {
 502			dev->flags |= IFF_MULTICAST;
 503			if (!ipmr_init_vif_indev(dev))
 504				goto failure;
 505			if (dev_open(dev, NULL))
 506				goto failure;
 507			dev_hold(dev);
 508		}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 509	}
 510	return dev;
 511
 512failure:
 513	unregister_netdevice(dev);
 514	return NULL;
 
 515}
 516
 517#if defined(CONFIG_IP_PIMSM_V1) || defined(CONFIG_IP_PIMSM_V2)
 518static netdev_tx_t reg_vif_xmit(struct sk_buff *skb, struct net_device *dev)
 519{
 520	struct net *net = dev_net(dev);
 521	struct mr_table *mrt;
 522	struct flowi4 fl4 = {
 523		.flowi4_oif	= dev->ifindex,
 524		.flowi4_iif	= skb->skb_iif ? : LOOPBACK_IFINDEX,
 525		.flowi4_mark	= skb->mark,
 526	};
 527	int err;
 528
 529	err = ipmr_fib_lookup(net, &fl4, &mrt);
 530	if (err < 0) {
 531		kfree_skb(skb);
 532		return err;
 533	}
 534
 535	read_lock(&mrt_lock);
 536	dev->stats.tx_bytes += skb->len;
 537	dev->stats.tx_packets++;
 538	ipmr_cache_report(mrt, skb, mrt->mroute_reg_vif_num, IGMPMSG_WHOLEPKT);
 539	read_unlock(&mrt_lock);
 
 
 
 
 540	kfree_skb(skb);
 541	return NETDEV_TX_OK;
 542}
 543
 544static int reg_vif_get_iflink(const struct net_device *dev)
 545{
 546	return 0;
 547}
 548
 549static const struct net_device_ops reg_vif_netdev_ops = {
 550	.ndo_start_xmit	= reg_vif_xmit,
 551	.ndo_get_iflink = reg_vif_get_iflink,
 552};
 553
 554static void reg_vif_setup(struct net_device *dev)
 555{
 556	dev->type		= ARPHRD_PIMREG;
 557	dev->mtu		= ETH_DATA_LEN - sizeof(struct iphdr) - 8;
 558	dev->flags		= IFF_NOARP;
 559	dev->netdev_ops		= &reg_vif_netdev_ops;
 560	dev->needs_free_netdev	= true;
 561	dev->features		|= NETIF_F_NETNS_LOCAL;
 562}
 563
 564static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt)
 565{
 566	struct net_device *dev;
 567	char name[IFNAMSIZ];
 568
 569	if (mrt->id == RT_TABLE_DEFAULT)
 570		sprintf(name, "pimreg");
 571	else
 572		sprintf(name, "pimreg%u", mrt->id);
 573
 574	dev = alloc_netdev(0, name, NET_NAME_UNKNOWN, reg_vif_setup);
 575
 576	if (!dev)
 577		return NULL;
 578
 579	dev_net_set(dev, net);
 580
 581	if (register_netdevice(dev)) {
 582		free_netdev(dev);
 583		return NULL;
 584	}
 585
 586	if (!ipmr_init_vif_indev(dev))
 587		goto failure;
 588	if (dev_open(dev, NULL))
 589		goto failure;
 590
 591	dev_hold(dev);
 592
 593	return dev;
 594
 595failure:
 596	unregister_netdevice(dev);
 597	return NULL;
 598}
 599
 600/* called with rcu_read_lock() */
 601static int __pim_rcv(struct mr_table *mrt, struct sk_buff *skb,
 602		     unsigned int pimlen)
 603{
 604	struct net_device *reg_dev = NULL;
 605	struct iphdr *encap;
 
 606
 607	encap = (struct iphdr *)(skb_transport_header(skb) + pimlen);
 608	/* Check that:
 609	 * a. packet is really sent to a multicast group
 610	 * b. packet is not a NULL-REGISTER
 611	 * c. packet is not truncated
 612	 */
 613	if (!ipv4_is_multicast(encap->daddr) ||
 614	    encap->tot_len == 0 ||
 615	    ntohs(encap->tot_len) + pimlen > skb->len)
 616		return 1;
 617
 618	read_lock(&mrt_lock);
 619	if (mrt->mroute_reg_vif_num >= 0)
 620		reg_dev = mrt->vif_table[mrt->mroute_reg_vif_num].dev;
 621	read_unlock(&mrt_lock);
 622
 623	if (!reg_dev)
 624		return 1;
 625
 626	skb->mac_header = skb->network_header;
 627	skb_pull(skb, (u8 *)encap - skb->data);
 628	skb_reset_network_header(skb);
 629	skb->protocol = htons(ETH_P_IP);
 630	skb->ip_summed = CHECKSUM_NONE;
 631
 632	skb_tunnel_rx(skb, reg_dev, dev_net(reg_dev));
 633
 634	netif_rx(skb);
 635
 636	return NET_RX_SUCCESS;
 637}
 638#else
 639static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt)
 640{
 641	return NULL;
 642}
 643#endif
 644
 645static int call_ipmr_vif_entry_notifiers(struct net *net,
 646					 enum fib_event_type event_type,
 647					 struct vif_device *vif,
 
 648					 vifi_t vif_index, u32 tb_id)
 649{
 650	return mr_call_vif_notifiers(net, RTNL_FAMILY_IPMR, event_type,
 651				     vif, vif_index, tb_id,
 652				     &net->ipv4.ipmr_seq);
 653}
 654
 655static int call_ipmr_mfc_entry_notifiers(struct net *net,
 656					 enum fib_event_type event_type,
 657					 struct mfc_cache *mfc, u32 tb_id)
 658{
 659	return mr_call_mfc_notifiers(net, RTNL_FAMILY_IPMR, event_type,
 660				     &mfc->_c, tb_id, &net->ipv4.ipmr_seq);
 661}
 662
 663/**
 664 *	vif_delete - Delete a VIF entry
 
 
 665 *	@notify: Set to 1, if the caller is a notifier_call
 
 666 */
 667static int vif_delete(struct mr_table *mrt, int vifi, int notify,
 668		      struct list_head *head)
 669{
 670	struct net *net = read_pnet(&mrt->net);
 671	struct vif_device *v;
 672	struct net_device *dev;
 673	struct in_device *in_dev;
 674
 675	if (vifi < 0 || vifi >= mrt->maxvif)
 676		return -EADDRNOTAVAIL;
 677
 678	v = &mrt->vif_table[vifi];
 679
 680	if (VIF_EXISTS(mrt, vifi))
 681		call_ipmr_vif_entry_notifiers(net, FIB_EVENT_VIF_DEL, v, vifi,
 682					      mrt->id);
 683
 684	write_lock_bh(&mrt_lock);
 685	dev = v->dev;
 686	v->dev = NULL;
 687
 688	if (!dev) {
 689		write_unlock_bh(&mrt_lock);
 690		return -EADDRNOTAVAIL;
 691	}
 692
 693	if (vifi == mrt->mroute_reg_vif_num)
 694		mrt->mroute_reg_vif_num = -1;
 695
 
 
 
 
 
 
 
 
 
 696	if (vifi + 1 == mrt->maxvif) {
 697		int tmp;
 698
 699		for (tmp = vifi - 1; tmp >= 0; tmp--) {
 700			if (VIF_EXISTS(mrt, tmp))
 701				break;
 702		}
 703		mrt->maxvif = tmp+1;
 704	}
 705
 706	write_unlock_bh(&mrt_lock);
 707
 708	dev_set_allmulti(dev, -1);
 709
 710	in_dev = __in_dev_get_rtnl(dev);
 711	if (in_dev) {
 712		IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)--;
 713		inet_netconf_notify_devconf(dev_net(dev), RTM_NEWNETCONF,
 714					    NETCONFA_MC_FORWARDING,
 715					    dev->ifindex, &in_dev->cnf);
 716		ip_rt_multicast_event(in_dev);
 717	}
 718
 719	if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER) && !notify)
 720		unregister_netdevice_queue(dev, head);
 721
 722	dev_put(dev);
 723	return 0;
 724}
 725
 726static void ipmr_cache_free_rcu(struct rcu_head *head)
 727{
 728	struct mr_mfc *c = container_of(head, struct mr_mfc, rcu);
 729
 730	kmem_cache_free(mrt_cachep, (struct mfc_cache *)c);
 731}
 732
 733static void ipmr_cache_free(struct mfc_cache *c)
 734{
 735	call_rcu(&c->_c.rcu, ipmr_cache_free_rcu);
 736}
 737
 738/* Destroy an unresolved cache entry, killing queued skbs
 739 * and reporting error to netlink readers.
 740 */
 741static void ipmr_destroy_unres(struct mr_table *mrt, struct mfc_cache *c)
 742{
 743	struct net *net = read_pnet(&mrt->net);
 744	struct sk_buff *skb;
 745	struct nlmsgerr *e;
 746
 747	atomic_dec(&mrt->cache_resolve_queue_len);
 748
 749	while ((skb = skb_dequeue(&c->_c.mfc_un.unres.unresolved))) {
 750		if (ip_hdr(skb)->version == 0) {
 751			struct nlmsghdr *nlh = skb_pull(skb,
 752							sizeof(struct iphdr));
 753			nlh->nlmsg_type = NLMSG_ERROR;
 754			nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr));
 755			skb_trim(skb, nlh->nlmsg_len);
 756			e = nlmsg_data(nlh);
 757			e->error = -ETIMEDOUT;
 758			memset(&e->msg, 0, sizeof(e->msg));
 759
 760			rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
 761		} else {
 762			kfree_skb(skb);
 763		}
 764	}
 765
 766	ipmr_cache_free(c);
 767}
 768
 769/* Timer process for the unresolved queue. */
 770static void ipmr_expire_process(struct timer_list *t)
 771{
 772	struct mr_table *mrt = from_timer(mrt, t, ipmr_expire_timer);
 773	struct mr_mfc *c, *next;
 774	unsigned long expires;
 775	unsigned long now;
 776
 777	if (!spin_trylock(&mfc_unres_lock)) {
 778		mod_timer(&mrt->ipmr_expire_timer, jiffies+HZ/10);
 779		return;
 780	}
 781
 782	if (list_empty(&mrt->mfc_unres_queue))
 783		goto out;
 784
 785	now = jiffies;
 786	expires = 10*HZ;
 787
 788	list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
 789		if (time_after(c->mfc_un.unres.expires, now)) {
 790			unsigned long interval = c->mfc_un.unres.expires - now;
 791			if (interval < expires)
 792				expires = interval;
 793			continue;
 794		}
 795
 796		list_del(&c->list);
 797		mroute_netlink_event(mrt, (struct mfc_cache *)c, RTM_DELROUTE);
 798		ipmr_destroy_unres(mrt, (struct mfc_cache *)c);
 799	}
 800
 801	if (!list_empty(&mrt->mfc_unres_queue))
 802		mod_timer(&mrt->ipmr_expire_timer, jiffies + expires);
 803
 804out:
 805	spin_unlock(&mfc_unres_lock);
 806}
 807
 808/* Fill oifs list. It is called under write locked mrt_lock. */
 809static void ipmr_update_thresholds(struct mr_table *mrt, struct mr_mfc *cache,
 810				   unsigned char *ttls)
 811{
 812	int vifi;
 813
 814	cache->mfc_un.res.minvif = MAXVIFS;
 815	cache->mfc_un.res.maxvif = 0;
 816	memset(cache->mfc_un.res.ttls, 255, MAXVIFS);
 817
 818	for (vifi = 0; vifi < mrt->maxvif; vifi++) {
 819		if (VIF_EXISTS(mrt, vifi) &&
 820		    ttls[vifi] && ttls[vifi] < 255) {
 821			cache->mfc_un.res.ttls[vifi] = ttls[vifi];
 822			if (cache->mfc_un.res.minvif > vifi)
 823				cache->mfc_un.res.minvif = vifi;
 824			if (cache->mfc_un.res.maxvif <= vifi)
 825				cache->mfc_un.res.maxvif = vifi + 1;
 826		}
 827	}
 828	cache->mfc_un.res.lastuse = jiffies;
 829}
 830
 831static int vif_add(struct net *net, struct mr_table *mrt,
 832		   struct vifctl *vifc, int mrtsock)
 833{
 834	struct netdev_phys_item_id ppid = { };
 835	int vifi = vifc->vifc_vifi;
 836	struct vif_device *v = &mrt->vif_table[vifi];
 837	struct net_device *dev;
 838	struct in_device *in_dev;
 839	int err;
 840
 841	/* Is vif busy ? */
 842	if (VIF_EXISTS(mrt, vifi))
 843		return -EADDRINUSE;
 844
 845	switch (vifc->vifc_flags) {
 846	case VIFF_REGISTER:
 847		if (!ipmr_pimsm_enabled())
 848			return -EINVAL;
 849		/* Special Purpose VIF in PIM
 850		 * All the packets will be sent to the daemon
 851		 */
 852		if (mrt->mroute_reg_vif_num >= 0)
 853			return -EADDRINUSE;
 854		dev = ipmr_reg_vif(net, mrt);
 855		if (!dev)
 856			return -ENOBUFS;
 857		err = dev_set_allmulti(dev, 1);
 858		if (err) {
 859			unregister_netdevice(dev);
 860			dev_put(dev);
 861			return err;
 862		}
 863		break;
 864	case VIFF_TUNNEL:
 865		dev = ipmr_new_tunnel(net, vifc);
 866		if (!dev)
 867			return -ENOBUFS;
 868		err = dev_set_allmulti(dev, 1);
 869		if (err) {
 870			ipmr_del_tunnel(dev, vifc);
 871			dev_put(dev);
 872			return err;
 873		}
 874		break;
 875	case VIFF_USE_IFINDEX:
 876	case 0:
 877		if (vifc->vifc_flags == VIFF_USE_IFINDEX) {
 878			dev = dev_get_by_index(net, vifc->vifc_lcl_ifindex);
 879			if (dev && !__in_dev_get_rtnl(dev)) {
 880				dev_put(dev);
 881				return -EADDRNOTAVAIL;
 882			}
 883		} else {
 884			dev = ip_dev_find(net, vifc->vifc_lcl_addr.s_addr);
 885		}
 886		if (!dev)
 887			return -EADDRNOTAVAIL;
 888		err = dev_set_allmulti(dev, 1);
 889		if (err) {
 890			dev_put(dev);
 891			return err;
 892		}
 893		break;
 894	default:
 895		return -EINVAL;
 896	}
 897
 898	in_dev = __in_dev_get_rtnl(dev);
 899	if (!in_dev) {
 900		dev_put(dev);
 901		return -EADDRNOTAVAIL;
 902	}
 903	IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)++;
 904	inet_netconf_notify_devconf(net, RTM_NEWNETCONF, NETCONFA_MC_FORWARDING,
 905				    dev->ifindex, &in_dev->cnf);
 906	ip_rt_multicast_event(in_dev);
 907
 908	/* Fill in the VIF structures */
 909	vif_device_init(v, dev, vifc->vifc_rate_limit,
 910			vifc->vifc_threshold,
 911			vifc->vifc_flags | (!mrtsock ? VIFF_STATIC : 0),
 912			(VIFF_TUNNEL | VIFF_REGISTER));
 913
 914	err = dev_get_port_parent_id(dev, &ppid, true);
 915	if (err == 0) {
 916		memcpy(v->dev_parent_id.id, ppid.id, ppid.id_len);
 917		v->dev_parent_id.id_len = ppid.id_len;
 918	} else {
 919		v->dev_parent_id.id_len = 0;
 920	}
 921
 922	v->local = vifc->vifc_lcl_addr.s_addr;
 923	v->remote = vifc->vifc_rmt_addr.s_addr;
 924
 925	/* And finish update writing critical data */
 926	write_lock_bh(&mrt_lock);
 927	v->dev = dev;
 928	if (v->flags & VIFF_REGISTER)
 929		mrt->mroute_reg_vif_num = vifi;
 
 
 
 930	if (vifi+1 > mrt->maxvif)
 931		mrt->maxvif = vifi+1;
 932	write_unlock_bh(&mrt_lock);
 933	call_ipmr_vif_entry_notifiers(net, FIB_EVENT_VIF_ADD, v, vifi, mrt->id);
 
 934	return 0;
 935}
 936
 937/* called with rcu_read_lock() */
 938static struct mfc_cache *ipmr_cache_find(struct mr_table *mrt,
 939					 __be32 origin,
 940					 __be32 mcastgrp)
 941{
 942	struct mfc_cache_cmp_arg arg = {
 943			.mfc_mcastgrp = mcastgrp,
 944			.mfc_origin = origin
 945	};
 946
 947	return mr_mfc_find(mrt, &arg);
 948}
 949
 950/* Look for a (*,G) entry */
 951static struct mfc_cache *ipmr_cache_find_any(struct mr_table *mrt,
 952					     __be32 mcastgrp, int vifi)
 953{
 954	struct mfc_cache_cmp_arg arg = {
 955			.mfc_mcastgrp = mcastgrp,
 956			.mfc_origin = htonl(INADDR_ANY)
 957	};
 958
 959	if (mcastgrp == htonl(INADDR_ANY))
 960		return mr_mfc_find_any_parent(mrt, vifi);
 961	return mr_mfc_find_any(mrt, vifi, &arg);
 962}
 963
 964/* Look for a (S,G,iif) entry if parent != -1 */
 965static struct mfc_cache *ipmr_cache_find_parent(struct mr_table *mrt,
 966						__be32 origin, __be32 mcastgrp,
 967						int parent)
 968{
 969	struct mfc_cache_cmp_arg arg = {
 970			.mfc_mcastgrp = mcastgrp,
 971			.mfc_origin = origin,
 972	};
 973
 974	return mr_mfc_find_parent(mrt, &arg, parent);
 975}
 976
 977/* Allocate a multicast cache entry */
 978static struct mfc_cache *ipmr_cache_alloc(void)
 979{
 980	struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_KERNEL);
 981
 982	if (c) {
 983		c->_c.mfc_un.res.last_assert = jiffies - MFC_ASSERT_THRESH - 1;
 984		c->_c.mfc_un.res.minvif = MAXVIFS;
 985		c->_c.free = ipmr_cache_free_rcu;
 986		refcount_set(&c->_c.mfc_un.res.refcount, 1);
 987	}
 988	return c;
 989}
 990
 991static struct mfc_cache *ipmr_cache_alloc_unres(void)
 992{
 993	struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_ATOMIC);
 994
 995	if (c) {
 996		skb_queue_head_init(&c->_c.mfc_un.unres.unresolved);
 997		c->_c.mfc_un.unres.expires = jiffies + 10 * HZ;
 998	}
 999	return c;
1000}
1001
1002/* A cache entry has gone into a resolved state from queued */
1003static void ipmr_cache_resolve(struct net *net, struct mr_table *mrt,
1004			       struct mfc_cache *uc, struct mfc_cache *c)
1005{
1006	struct sk_buff *skb;
1007	struct nlmsgerr *e;
1008
1009	/* Play the pending entries through our router */
1010	while ((skb = __skb_dequeue(&uc->_c.mfc_un.unres.unresolved))) {
1011		if (ip_hdr(skb)->version == 0) {
1012			struct nlmsghdr *nlh = skb_pull(skb,
1013							sizeof(struct iphdr));
1014
1015			if (mr_fill_mroute(mrt, skb, &c->_c,
1016					   nlmsg_data(nlh)) > 0) {
1017				nlh->nlmsg_len = skb_tail_pointer(skb) -
1018						 (u8 *)nlh;
1019			} else {
1020				nlh->nlmsg_type = NLMSG_ERROR;
1021				nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr));
1022				skb_trim(skb, nlh->nlmsg_len);
1023				e = nlmsg_data(nlh);
1024				e->error = -EMSGSIZE;
1025				memset(&e->msg, 0, sizeof(e->msg));
1026			}
1027
1028			rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
1029		} else {
 
1030			ip_mr_forward(net, mrt, skb->dev, skb, c, 0);
 
1031		}
1032	}
1033}
1034
1035/* Bounce a cache query up to mrouted and netlink.
1036 *
1037 * Called under mrt_lock.
1038 */
1039static int ipmr_cache_report(struct mr_table *mrt,
1040			     struct sk_buff *pkt, vifi_t vifi, int assert)
1041{
1042	const int ihl = ip_hdrlen(pkt);
1043	struct sock *mroute_sk;
1044	struct igmphdr *igmp;
1045	struct igmpmsg *msg;
1046	struct sk_buff *skb;
1047	int ret;
1048
 
 
 
 
1049	if (assert == IGMPMSG_WHOLEPKT || assert == IGMPMSG_WRVIFWHOLE)
1050		skb = skb_realloc_headroom(pkt, sizeof(struct iphdr));
1051	else
1052		skb = alloc_skb(128, GFP_ATOMIC);
1053
1054	if (!skb)
1055		return -ENOBUFS;
1056
1057	if (assert == IGMPMSG_WHOLEPKT || assert == IGMPMSG_WRVIFWHOLE) {
1058		/* Ugly, but we have no choice with this interface.
1059		 * Duplicate old header, fix ihl, length etc.
1060		 * And all this only to mangle msg->im_msgtype and
1061		 * to set msg->im_mbz to "mbz" :-)
1062		 */
1063		skb_push(skb, sizeof(struct iphdr));
1064		skb_reset_network_header(skb);
1065		skb_reset_transport_header(skb);
1066		msg = (struct igmpmsg *)skb_network_header(skb);
1067		memcpy(msg, skb_network_header(pkt), sizeof(struct iphdr));
1068		msg->im_msgtype = assert;
1069		msg->im_mbz = 0;
1070		if (assert == IGMPMSG_WRVIFWHOLE)
1071			msg->im_vif = vifi;
1072		else
1073			msg->im_vif = mrt->mroute_reg_vif_num;
 
 
 
 
 
 
1074		ip_hdr(skb)->ihl = sizeof(struct iphdr) >> 2;
1075		ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(pkt)->tot_len) +
1076					     sizeof(struct iphdr));
1077	} else {
1078		/* Copy the IP header */
1079		skb_set_network_header(skb, skb->len);
1080		skb_put(skb, ihl);
1081		skb_copy_to_linear_data(skb, pkt->data, ihl);
1082		/* Flag to the kernel this is a route add */
1083		ip_hdr(skb)->protocol = 0;
1084		msg = (struct igmpmsg *)skb_network_header(skb);
1085		msg->im_vif = vifi;
1086		skb_dst_set(skb, dst_clone(skb_dst(pkt)));
 
 
1087		/* Add our header */
1088		igmp = skb_put(skb, sizeof(struct igmphdr));
1089		igmp->type = assert;
1090		msg->im_msgtype = assert;
1091		igmp->code = 0;
1092		ip_hdr(skb)->tot_len = htons(skb->len);	/* Fix the length */
1093		skb->transport_header = skb->network_header;
1094	}
1095
1096	rcu_read_lock();
1097	mroute_sk = rcu_dereference(mrt->mroute_sk);
1098	if (!mroute_sk) {
1099		rcu_read_unlock();
1100		kfree_skb(skb);
1101		return -EINVAL;
1102	}
1103
1104	igmpmsg_netlink_event(mrt, skb);
1105
1106	/* Deliver to mrouted */
1107	ret = sock_queue_rcv_skb(mroute_sk, skb);
1108	rcu_read_unlock();
1109	if (ret < 0) {
1110		net_warn_ratelimited("mroute: pending queue full, dropping entries\n");
1111		kfree_skb(skb);
1112	}
1113
1114	return ret;
1115}
1116
1117/* Queue a packet for resolution. It gets locked cache entry! */
 
1118static int ipmr_cache_unresolved(struct mr_table *mrt, vifi_t vifi,
1119				 struct sk_buff *skb, struct net_device *dev)
1120{
1121	const struct iphdr *iph = ip_hdr(skb);
1122	struct mfc_cache *c;
1123	bool found = false;
1124	int err;
1125
1126	spin_lock_bh(&mfc_unres_lock);
1127	list_for_each_entry(c, &mrt->mfc_unres_queue, _c.list) {
1128		if (c->mfc_mcastgrp == iph->daddr &&
1129		    c->mfc_origin == iph->saddr) {
1130			found = true;
1131			break;
1132		}
1133	}
1134
1135	if (!found) {
1136		/* Create a new entry if allowable */
1137		c = ipmr_cache_alloc_unres();
1138		if (!c) {
1139			spin_unlock_bh(&mfc_unres_lock);
1140
1141			kfree_skb(skb);
1142			return -ENOBUFS;
1143		}
1144
1145		/* Fill in the new cache entry */
1146		c->_c.mfc_parent = -1;
1147		c->mfc_origin	= iph->saddr;
1148		c->mfc_mcastgrp	= iph->daddr;
1149
1150		/* Reflect first query at mrouted. */
1151		err = ipmr_cache_report(mrt, skb, vifi, IGMPMSG_NOCACHE);
1152
1153		if (err < 0) {
1154			/* If the report failed throw the cache entry
1155			   out - Brad Parker
1156			 */
1157			spin_unlock_bh(&mfc_unres_lock);
1158
1159			ipmr_cache_free(c);
1160			kfree_skb(skb);
1161			return err;
1162		}
1163
1164		atomic_inc(&mrt->cache_resolve_queue_len);
1165		list_add(&c->_c.list, &mrt->mfc_unres_queue);
1166		mroute_netlink_event(mrt, c, RTM_NEWROUTE);
1167
1168		if (atomic_read(&mrt->cache_resolve_queue_len) == 1)
1169			mod_timer(&mrt->ipmr_expire_timer,
1170				  c->_c.mfc_un.unres.expires);
1171	}
1172
1173	/* See if we can append the packet */
1174	if (c->_c.mfc_un.unres.unresolved.qlen > 3) {
1175		kfree_skb(skb);
1176		err = -ENOBUFS;
1177	} else {
1178		if (dev) {
1179			skb->dev = dev;
1180			skb->skb_iif = dev->ifindex;
1181		}
1182		skb_queue_tail(&c->_c.mfc_un.unres.unresolved, skb);
1183		err = 0;
1184	}
1185
1186	spin_unlock_bh(&mfc_unres_lock);
1187	return err;
1188}
1189
1190/* MFC cache manipulation by user space mroute daemon */
1191
1192static int ipmr_mfc_delete(struct mr_table *mrt, struct mfcctl *mfc, int parent)
1193{
1194	struct net *net = read_pnet(&mrt->net);
1195	struct mfc_cache *c;
1196
1197	/* The entries are added/deleted only under RTNL */
1198	rcu_read_lock();
1199	c = ipmr_cache_find_parent(mrt, mfc->mfcc_origin.s_addr,
1200				   mfc->mfcc_mcastgrp.s_addr, parent);
1201	rcu_read_unlock();
1202	if (!c)
1203		return -ENOENT;
1204	rhltable_remove(&mrt->mfc_hash, &c->_c.mnode, ipmr_rht_params);
1205	list_del_rcu(&c->_c.list);
1206	call_ipmr_mfc_entry_notifiers(net, FIB_EVENT_ENTRY_DEL, c, mrt->id);
1207	mroute_netlink_event(mrt, c, RTM_DELROUTE);
1208	mr_cache_put(&c->_c);
1209
1210	return 0;
1211}
1212
1213static int ipmr_mfc_add(struct net *net, struct mr_table *mrt,
1214			struct mfcctl *mfc, int mrtsock, int parent)
1215{
1216	struct mfc_cache *uc, *c;
1217	struct mr_mfc *_uc;
1218	bool found;
1219	int ret;
1220
1221	if (mfc->mfcc_parent >= MAXVIFS)
1222		return -ENFILE;
1223
1224	/* The entries are added/deleted only under RTNL */
1225	rcu_read_lock();
1226	c = ipmr_cache_find_parent(mrt, mfc->mfcc_origin.s_addr,
1227				   mfc->mfcc_mcastgrp.s_addr, parent);
1228	rcu_read_unlock();
1229	if (c) {
1230		write_lock_bh(&mrt_lock);
1231		c->_c.mfc_parent = mfc->mfcc_parent;
1232		ipmr_update_thresholds(mrt, &c->_c, mfc->mfcc_ttls);
1233		if (!mrtsock)
1234			c->_c.mfc_flags |= MFC_STATIC;
1235		write_unlock_bh(&mrt_lock);
1236		call_ipmr_mfc_entry_notifiers(net, FIB_EVENT_ENTRY_REPLACE, c,
1237					      mrt->id);
1238		mroute_netlink_event(mrt, c, RTM_NEWROUTE);
1239		return 0;
1240	}
1241
1242	if (mfc->mfcc_mcastgrp.s_addr != htonl(INADDR_ANY) &&
1243	    !ipv4_is_multicast(mfc->mfcc_mcastgrp.s_addr))
1244		return -EINVAL;
1245
1246	c = ipmr_cache_alloc();
1247	if (!c)
1248		return -ENOMEM;
1249
1250	c->mfc_origin = mfc->mfcc_origin.s_addr;
1251	c->mfc_mcastgrp = mfc->mfcc_mcastgrp.s_addr;
1252	c->_c.mfc_parent = mfc->mfcc_parent;
1253	ipmr_update_thresholds(mrt, &c->_c, mfc->mfcc_ttls);
1254	if (!mrtsock)
1255		c->_c.mfc_flags |= MFC_STATIC;
1256
1257	ret = rhltable_insert_key(&mrt->mfc_hash, &c->cmparg, &c->_c.mnode,
1258				  ipmr_rht_params);
1259	if (ret) {
1260		pr_err("ipmr: rhtable insert error %d\n", ret);
1261		ipmr_cache_free(c);
1262		return ret;
1263	}
1264	list_add_tail_rcu(&c->_c.list, &mrt->mfc_cache_list);
1265	/* Check to see if we resolved a queued list. If so we
1266	 * need to send on the frames and tidy up.
1267	 */
1268	found = false;
1269	spin_lock_bh(&mfc_unres_lock);
1270	list_for_each_entry(_uc, &mrt->mfc_unres_queue, list) {
1271		uc = (struct mfc_cache *)_uc;
1272		if (uc->mfc_origin == c->mfc_origin &&
1273		    uc->mfc_mcastgrp == c->mfc_mcastgrp) {
1274			list_del(&_uc->list);
1275			atomic_dec(&mrt->cache_resolve_queue_len);
1276			found = true;
1277			break;
1278		}
1279	}
1280	if (list_empty(&mrt->mfc_unres_queue))
1281		del_timer(&mrt->ipmr_expire_timer);
1282	spin_unlock_bh(&mfc_unres_lock);
1283
1284	if (found) {
1285		ipmr_cache_resolve(net, mrt, uc, c);
1286		ipmr_cache_free(uc);
1287	}
1288	call_ipmr_mfc_entry_notifiers(net, FIB_EVENT_ENTRY_ADD, c, mrt->id);
1289	mroute_netlink_event(mrt, c, RTM_NEWROUTE);
1290	return 0;
1291}
1292
1293/* Close the multicast socket, and clear the vif tables etc */
1294static void mroute_clean_tables(struct mr_table *mrt, int flags)
1295{
1296	struct net *net = read_pnet(&mrt->net);
1297	struct mr_mfc *c, *tmp;
1298	struct mfc_cache *cache;
1299	LIST_HEAD(list);
1300	int i;
1301
1302	/* Shut down all active vif entries */
1303	if (flags & (MRT_FLUSH_VIFS | MRT_FLUSH_VIFS_STATIC)) {
1304		for (i = 0; i < mrt->maxvif; i++) {
1305			if (((mrt->vif_table[i].flags & VIFF_STATIC) &&
1306			     !(flags & MRT_FLUSH_VIFS_STATIC)) ||
1307			    (!(mrt->vif_table[i].flags & VIFF_STATIC) && !(flags & MRT_FLUSH_VIFS)))
1308				continue;
1309			vif_delete(mrt, i, 0, &list);
1310		}
1311		unregister_netdevice_many(&list);
1312	}
1313
1314	/* Wipe the cache */
1315	if (flags & (MRT_FLUSH_MFC | MRT_FLUSH_MFC_STATIC)) {
1316		list_for_each_entry_safe(c, tmp, &mrt->mfc_cache_list, list) {
1317			if (((c->mfc_flags & MFC_STATIC) && !(flags & MRT_FLUSH_MFC_STATIC)) ||
1318			    (!(c->mfc_flags & MFC_STATIC) && !(flags & MRT_FLUSH_MFC)))
1319				continue;
1320			rhltable_remove(&mrt->mfc_hash, &c->mnode, ipmr_rht_params);
1321			list_del_rcu(&c->list);
1322			cache = (struct mfc_cache *)c;
1323			call_ipmr_mfc_entry_notifiers(net, FIB_EVENT_ENTRY_DEL, cache,
1324						      mrt->id);
1325			mroute_netlink_event(mrt, cache, RTM_DELROUTE);
1326			mr_cache_put(c);
1327		}
1328	}
1329
1330	if (flags & MRT_FLUSH_MFC) {
1331		if (atomic_read(&mrt->cache_resolve_queue_len) != 0) {
1332			spin_lock_bh(&mfc_unres_lock);
1333			list_for_each_entry_safe(c, tmp, &mrt->mfc_unres_queue, list) {
1334				list_del(&c->list);
1335				cache = (struct mfc_cache *)c;
1336				mroute_netlink_event(mrt, cache, RTM_DELROUTE);
1337				ipmr_destroy_unres(mrt, cache);
1338			}
1339			spin_unlock_bh(&mfc_unres_lock);
1340		}
1341	}
1342}
1343
1344/* called from ip_ra_control(), before an RCU grace period,
1345 * we dont need to call synchronize_rcu() here
1346 */
1347static void mrtsock_destruct(struct sock *sk)
1348{
1349	struct net *net = sock_net(sk);
1350	struct mr_table *mrt;
1351
1352	rtnl_lock();
1353	ipmr_for_each_table(mrt, net) {
1354		if (sk == rtnl_dereference(mrt->mroute_sk)) {
1355			IPV4_DEVCONF_ALL(net, MC_FORWARDING)--;
1356			inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
1357						    NETCONFA_MC_FORWARDING,
1358						    NETCONFA_IFINDEX_ALL,
1359						    net->ipv4.devconf_all);
1360			RCU_INIT_POINTER(mrt->mroute_sk, NULL);
1361			mroute_clean_tables(mrt, MRT_FLUSH_VIFS | MRT_FLUSH_MFC);
1362		}
1363	}
1364	rtnl_unlock();
1365}
1366
1367/* Socket options and virtual interface manipulation. The whole
1368 * virtual interface system is a complete heap, but unfortunately
1369 * that's how BSD mrouted happens to think. Maybe one day with a proper
1370 * MOSPF/PIM router set up we can clean this up.
1371 */
1372
1373int ip_mroute_setsockopt(struct sock *sk, int optname, char __user *optval,
1374			 unsigned int optlen)
1375{
1376	struct net *net = sock_net(sk);
1377	int val, ret = 0, parent = 0;
1378	struct mr_table *mrt;
1379	struct vifctl vif;
1380	struct mfcctl mfc;
1381	bool do_wrvifwhole;
1382	u32 uval;
1383
1384	/* There's one exception to the lock - MRT_DONE which needs to unlock */
1385	rtnl_lock();
1386	if (sk->sk_type != SOCK_RAW ||
1387	    inet_sk(sk)->inet_num != IPPROTO_IGMP) {
1388		ret = -EOPNOTSUPP;
1389		goto out_unlock;
1390	}
1391
1392	mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1393	if (!mrt) {
1394		ret = -ENOENT;
1395		goto out_unlock;
1396	}
1397	if (optname != MRT_INIT) {
1398		if (sk != rcu_access_pointer(mrt->mroute_sk) &&
1399		    !ns_capable(net->user_ns, CAP_NET_ADMIN)) {
1400			ret = -EACCES;
1401			goto out_unlock;
1402		}
1403	}
1404
1405	switch (optname) {
1406	case MRT_INIT:
1407		if (optlen != sizeof(int)) {
1408			ret = -EINVAL;
1409			break;
1410		}
1411		if (rtnl_dereference(mrt->mroute_sk)) {
1412			ret = -EADDRINUSE;
1413			break;
1414		}
1415
1416		ret = ip_ra_control(sk, 1, mrtsock_destruct);
1417		if (ret == 0) {
1418			rcu_assign_pointer(mrt->mroute_sk, sk);
1419			IPV4_DEVCONF_ALL(net, MC_FORWARDING)++;
1420			inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
1421						    NETCONFA_MC_FORWARDING,
1422						    NETCONFA_IFINDEX_ALL,
1423						    net->ipv4.devconf_all);
1424		}
1425		break;
1426	case MRT_DONE:
1427		if (sk != rcu_access_pointer(mrt->mroute_sk)) {
1428			ret = -EACCES;
1429		} else {
1430			/* We need to unlock here because mrtsock_destruct takes
1431			 * care of rtnl itself and we can't change that due to
1432			 * the IP_ROUTER_ALERT setsockopt which runs without it.
1433			 */
1434			rtnl_unlock();
1435			ret = ip_ra_control(sk, 0, NULL);
1436			goto out;
1437		}
1438		break;
1439	case MRT_ADD_VIF:
1440	case MRT_DEL_VIF:
1441		if (optlen != sizeof(vif)) {
1442			ret = -EINVAL;
1443			break;
1444		}
1445		if (copy_from_user(&vif, optval, sizeof(vif))) {
1446			ret = -EFAULT;
1447			break;
1448		}
1449		if (vif.vifc_vifi >= MAXVIFS) {
1450			ret = -ENFILE;
1451			break;
1452		}
1453		if (optname == MRT_ADD_VIF) {
1454			ret = vif_add(net, mrt, &vif,
1455				      sk == rtnl_dereference(mrt->mroute_sk));
1456		} else {
1457			ret = vif_delete(mrt, vif.vifc_vifi, 0, NULL);
1458		}
1459		break;
1460	/* Manipulate the forwarding caches. These live
1461	 * in a sort of kernel/user symbiosis.
1462	 */
1463	case MRT_ADD_MFC:
1464	case MRT_DEL_MFC:
1465		parent = -1;
1466		/* fall through */
1467	case MRT_ADD_MFC_PROXY:
1468	case MRT_DEL_MFC_PROXY:
1469		if (optlen != sizeof(mfc)) {
1470			ret = -EINVAL;
1471			break;
1472		}
1473		if (copy_from_user(&mfc, optval, sizeof(mfc))) {
1474			ret = -EFAULT;
1475			break;
1476		}
1477		if (parent == 0)
1478			parent = mfc.mfcc_parent;
1479		if (optname == MRT_DEL_MFC || optname == MRT_DEL_MFC_PROXY)
1480			ret = ipmr_mfc_delete(mrt, &mfc, parent);
1481		else
1482			ret = ipmr_mfc_add(net, mrt, &mfc,
1483					   sk == rtnl_dereference(mrt->mroute_sk),
1484					   parent);
1485		break;
1486	case MRT_FLUSH:
1487		if (optlen != sizeof(val)) {
1488			ret = -EINVAL;
1489			break;
1490		}
1491		if (get_user(val, (int __user *)optval)) {
1492			ret = -EFAULT;
1493			break;
1494		}
1495		mroute_clean_tables(mrt, val);
1496		break;
1497	/* Control PIM assert. */
1498	case MRT_ASSERT:
1499		if (optlen != sizeof(val)) {
1500			ret = -EINVAL;
1501			break;
1502		}
1503		if (get_user(val, (int __user *)optval)) {
1504			ret = -EFAULT;
1505			break;
1506		}
1507		mrt->mroute_do_assert = val;
1508		break;
1509	case MRT_PIM:
1510		if (!ipmr_pimsm_enabled()) {
1511			ret = -ENOPROTOOPT;
1512			break;
1513		}
1514		if (optlen != sizeof(val)) {
1515			ret = -EINVAL;
1516			break;
1517		}
1518		if (get_user(val, (int __user *)optval)) {
1519			ret = -EFAULT;
1520			break;
1521		}
1522
1523		do_wrvifwhole = (val == IGMPMSG_WRVIFWHOLE);
1524		val = !!val;
1525		if (val != mrt->mroute_do_pim) {
1526			mrt->mroute_do_pim = val;
1527			mrt->mroute_do_assert = val;
1528			mrt->mroute_do_wrvifwhole = do_wrvifwhole;
1529		}
1530		break;
1531	case MRT_TABLE:
1532		if (!IS_BUILTIN(CONFIG_IP_MROUTE_MULTIPLE_TABLES)) {
1533			ret = -ENOPROTOOPT;
1534			break;
1535		}
1536		if (optlen != sizeof(uval)) {
1537			ret = -EINVAL;
1538			break;
1539		}
1540		if (get_user(uval, (u32 __user *)optval)) {
1541			ret = -EFAULT;
1542			break;
1543		}
1544
1545		if (sk == rtnl_dereference(mrt->mroute_sk)) {
1546			ret = -EBUSY;
1547		} else {
1548			mrt = ipmr_new_table(net, uval);
1549			if (IS_ERR(mrt))
1550				ret = PTR_ERR(mrt);
1551			else
1552				raw_sk(sk)->ipmr_table = uval;
1553		}
1554		break;
1555	/* Spurious command, or MRT_VERSION which you cannot set. */
1556	default:
1557		ret = -ENOPROTOOPT;
1558	}
1559out_unlock:
1560	rtnl_unlock();
1561out:
1562	return ret;
1563}
1564
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1565/* Getsock opt support for the multicast routing system. */
1566int ip_mroute_getsockopt(struct sock *sk, int optname, char __user *optval, int __user *optlen)
 
1567{
1568	int olr;
1569	int val;
1570	struct net *net = sock_net(sk);
1571	struct mr_table *mrt;
1572
1573	if (sk->sk_type != SOCK_RAW ||
1574	    inet_sk(sk)->inet_num != IPPROTO_IGMP)
1575		return -EOPNOTSUPP;
1576
1577	mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1578	if (!mrt)
1579		return -ENOENT;
1580
1581	switch (optname) {
1582	case MRT_VERSION:
1583		val = 0x0305;
1584		break;
1585	case MRT_PIM:
1586		if (!ipmr_pimsm_enabled())
1587			return -ENOPROTOOPT;
1588		val = mrt->mroute_do_pim;
1589		break;
1590	case MRT_ASSERT:
1591		val = mrt->mroute_do_assert;
1592		break;
1593	default:
1594		return -ENOPROTOOPT;
1595	}
1596
1597	if (get_user(olr, optlen))
1598		return -EFAULT;
1599	olr = min_t(unsigned int, olr, sizeof(int));
1600	if (olr < 0)
1601		return -EINVAL;
1602	if (put_user(olr, optlen))
 
 
 
1603		return -EFAULT;
1604	if (copy_to_user(optval, &val, olr))
1605		return -EFAULT;
1606	return 0;
1607}
1608
1609/* The IP multicast ioctl support routines. */
1610int ipmr_ioctl(struct sock *sk, int cmd, void __user *arg)
1611{
1612	struct sioc_sg_req sr;
1613	struct sioc_vif_req vr;
1614	struct vif_device *vif;
1615	struct mfc_cache *c;
1616	struct net *net = sock_net(sk);
 
 
1617	struct mr_table *mrt;
1618
1619	mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1620	if (!mrt)
1621		return -ENOENT;
1622
1623	switch (cmd) {
1624	case SIOCGETVIFCNT:
1625		if (copy_from_user(&vr, arg, sizeof(vr)))
1626			return -EFAULT;
1627		if (vr.vifi >= mrt->maxvif)
1628			return -EINVAL;
1629		vr.vifi = array_index_nospec(vr.vifi, mrt->maxvif);
1630		read_lock(&mrt_lock);
1631		vif = &mrt->vif_table[vr.vifi];
1632		if (VIF_EXISTS(mrt, vr.vifi)) {
1633			vr.icount = vif->pkt_in;
1634			vr.ocount = vif->pkt_out;
1635			vr.ibytes = vif->bytes_in;
1636			vr.obytes = vif->bytes_out;
1637			read_unlock(&mrt_lock);
1638
1639			if (copy_to_user(arg, &vr, sizeof(vr)))
1640				return -EFAULT;
1641			return 0;
1642		}
1643		read_unlock(&mrt_lock);
1644		return -EADDRNOTAVAIL;
1645	case SIOCGETSGCNT:
1646		if (copy_from_user(&sr, arg, sizeof(sr)))
1647			return -EFAULT;
1648
1649		rcu_read_lock();
1650		c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
1651		if (c) {
1652			sr.pktcnt = c->_c.mfc_un.res.pkt;
1653			sr.bytecnt = c->_c.mfc_un.res.bytes;
1654			sr.wrong_if = c->_c.mfc_un.res.wrong_if;
1655			rcu_read_unlock();
1656
1657			if (copy_to_user(arg, &sr, sizeof(sr)))
1658				return -EFAULT;
1659			return 0;
1660		}
1661		rcu_read_unlock();
1662		return -EADDRNOTAVAIL;
1663	default:
1664		return -ENOIOCTLCMD;
1665	}
1666}
1667
1668#ifdef CONFIG_COMPAT
1669struct compat_sioc_sg_req {
1670	struct in_addr src;
1671	struct in_addr grp;
1672	compat_ulong_t pktcnt;
1673	compat_ulong_t bytecnt;
1674	compat_ulong_t wrong_if;
1675};
1676
1677struct compat_sioc_vif_req {
1678	vifi_t	vifi;		/* Which iface */
1679	compat_ulong_t icount;
1680	compat_ulong_t ocount;
1681	compat_ulong_t ibytes;
1682	compat_ulong_t obytes;
1683};
1684
1685int ipmr_compat_ioctl(struct sock *sk, unsigned int cmd, void __user *arg)
1686{
1687	struct compat_sioc_sg_req sr;
1688	struct compat_sioc_vif_req vr;
1689	struct vif_device *vif;
1690	struct mfc_cache *c;
1691	struct net *net = sock_net(sk);
1692	struct mr_table *mrt;
1693
1694	mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1695	if (!mrt)
1696		return -ENOENT;
1697
1698	switch (cmd) {
1699	case SIOCGETVIFCNT:
1700		if (copy_from_user(&vr, arg, sizeof(vr)))
1701			return -EFAULT;
1702		if (vr.vifi >= mrt->maxvif)
1703			return -EINVAL;
1704		vr.vifi = array_index_nospec(vr.vifi, mrt->maxvif);
1705		read_lock(&mrt_lock);
1706		vif = &mrt->vif_table[vr.vifi];
1707		if (VIF_EXISTS(mrt, vr.vifi)) {
1708			vr.icount = vif->pkt_in;
1709			vr.ocount = vif->pkt_out;
1710			vr.ibytes = vif->bytes_in;
1711			vr.obytes = vif->bytes_out;
1712			read_unlock(&mrt_lock);
1713
1714			if (copy_to_user(arg, &vr, sizeof(vr)))
1715				return -EFAULT;
1716			return 0;
1717		}
1718		read_unlock(&mrt_lock);
1719		return -EADDRNOTAVAIL;
1720	case SIOCGETSGCNT:
1721		if (copy_from_user(&sr, arg, sizeof(sr)))
1722			return -EFAULT;
1723
1724		rcu_read_lock();
1725		c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
1726		if (c) {
1727			sr.pktcnt = c->_c.mfc_un.res.pkt;
1728			sr.bytecnt = c->_c.mfc_un.res.bytes;
1729			sr.wrong_if = c->_c.mfc_un.res.wrong_if;
1730			rcu_read_unlock();
1731
1732			if (copy_to_user(arg, &sr, sizeof(sr)))
1733				return -EFAULT;
1734			return 0;
1735		}
1736		rcu_read_unlock();
1737		return -EADDRNOTAVAIL;
1738	default:
1739		return -ENOIOCTLCMD;
1740	}
1741}
1742#endif
1743
1744static int ipmr_device_event(struct notifier_block *this, unsigned long event, void *ptr)
1745{
1746	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1747	struct net *net = dev_net(dev);
1748	struct mr_table *mrt;
1749	struct vif_device *v;
1750	int ct;
1751
1752	if (event != NETDEV_UNREGISTER)
1753		return NOTIFY_DONE;
1754
1755	ipmr_for_each_table(mrt, net) {
1756		v = &mrt->vif_table[0];
1757		for (ct = 0; ct < mrt->maxvif; ct++, v++) {
1758			if (v->dev == dev)
1759				vif_delete(mrt, ct, 1, NULL);
1760		}
1761	}
1762	return NOTIFY_DONE;
1763}
1764
1765static struct notifier_block ip_mr_notifier = {
1766	.notifier_call = ipmr_device_event,
1767};
1768
1769/* Encapsulate a packet by attaching a valid IPIP header to it.
1770 * This avoids tunnel drivers and other mess and gives us the speed so
1771 * important for multicast video.
1772 */
1773static void ip_encap(struct net *net, struct sk_buff *skb,
1774		     __be32 saddr, __be32 daddr)
1775{
1776	struct iphdr *iph;
1777	const struct iphdr *old_iph = ip_hdr(skb);
1778
1779	skb_push(skb, sizeof(struct iphdr));
1780	skb->transport_header = skb->network_header;
1781	skb_reset_network_header(skb);
1782	iph = ip_hdr(skb);
1783
1784	iph->version	=	4;
1785	iph->tos	=	old_iph->tos;
1786	iph->ttl	=	old_iph->ttl;
1787	iph->frag_off	=	0;
1788	iph->daddr	=	daddr;
1789	iph->saddr	=	saddr;
1790	iph->protocol	=	IPPROTO_IPIP;
1791	iph->ihl	=	5;
1792	iph->tot_len	=	htons(skb->len);
1793	ip_select_ident(net, skb, NULL);
1794	ip_send_check(iph);
1795
1796	memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1797	nf_reset_ct(skb);
1798}
1799
1800static inline int ipmr_forward_finish(struct net *net, struct sock *sk,
1801				      struct sk_buff *skb)
1802{
1803	struct ip_options *opt = &(IPCB(skb)->opt);
1804
1805	IP_INC_STATS(net, IPSTATS_MIB_OUTFORWDATAGRAMS);
1806	IP_ADD_STATS(net, IPSTATS_MIB_OUTOCTETS, skb->len);
1807
1808	if (unlikely(opt->optlen))
1809		ip_forward_options(skb);
1810
1811	return dst_output(net, sk, skb);
1812}
1813
1814#ifdef CONFIG_NET_SWITCHDEV
1815static bool ipmr_forward_offloaded(struct sk_buff *skb, struct mr_table *mrt,
1816				   int in_vifi, int out_vifi)
1817{
1818	struct vif_device *out_vif = &mrt->vif_table[out_vifi];
1819	struct vif_device *in_vif = &mrt->vif_table[in_vifi];
1820
1821	if (!skb->offload_l3_fwd_mark)
1822		return false;
1823	if (!out_vif->dev_parent_id.id_len || !in_vif->dev_parent_id.id_len)
1824		return false;
1825	return netdev_phys_item_id_same(&out_vif->dev_parent_id,
1826					&in_vif->dev_parent_id);
1827}
1828#else
1829static bool ipmr_forward_offloaded(struct sk_buff *skb, struct mr_table *mrt,
1830				   int in_vifi, int out_vifi)
1831{
1832	return false;
1833}
1834#endif
1835
1836/* Processing handlers for ipmr_forward */
1837
1838static void ipmr_queue_xmit(struct net *net, struct mr_table *mrt,
1839			    int in_vifi, struct sk_buff *skb, int vifi)
1840{
1841	const struct iphdr *iph = ip_hdr(skb);
1842	struct vif_device *vif = &mrt->vif_table[vifi];
 
1843	struct net_device *dev;
1844	struct rtable *rt;
1845	struct flowi4 fl4;
1846	int    encap = 0;
1847
1848	if (!vif->dev)
 
1849		goto out_free;
1850
1851	if (vif->flags & VIFF_REGISTER) {
1852		vif->pkt_out++;
1853		vif->bytes_out += skb->len;
1854		vif->dev->stats.tx_bytes += skb->len;
1855		vif->dev->stats.tx_packets++;
1856		ipmr_cache_report(mrt, skb, vifi, IGMPMSG_WHOLEPKT);
1857		goto out_free;
1858	}
1859
1860	if (ipmr_forward_offloaded(skb, mrt, in_vifi, vifi))
1861		goto out_free;
1862
1863	if (vif->flags & VIFF_TUNNEL) {
1864		rt = ip_route_output_ports(net, &fl4, NULL,
1865					   vif->remote, vif->local,
1866					   0, 0,
1867					   IPPROTO_IPIP,
1868					   RT_TOS(iph->tos), vif->link);
1869		if (IS_ERR(rt))
1870			goto out_free;
1871		encap = sizeof(struct iphdr);
1872	} else {
1873		rt = ip_route_output_ports(net, &fl4, NULL, iph->daddr, 0,
1874					   0, 0,
1875					   IPPROTO_IPIP,
1876					   RT_TOS(iph->tos), vif->link);
1877		if (IS_ERR(rt))
1878			goto out_free;
1879	}
1880
1881	dev = rt->dst.dev;
1882
1883	if (skb->len+encap > dst_mtu(&rt->dst) && (ntohs(iph->frag_off) & IP_DF)) {
1884		/* Do not fragment multicasts. Alas, IPv4 does not
1885		 * allow to send ICMP, so that packets will disappear
1886		 * to blackhole.
1887		 */
1888		IP_INC_STATS(net, IPSTATS_MIB_FRAGFAILS);
1889		ip_rt_put(rt);
1890		goto out_free;
1891	}
1892
1893	encap += LL_RESERVED_SPACE(dev) + rt->dst.header_len;
1894
1895	if (skb_cow(skb, encap)) {
1896		ip_rt_put(rt);
1897		goto out_free;
1898	}
1899
1900	vif->pkt_out++;
1901	vif->bytes_out += skb->len;
1902
1903	skb_dst_drop(skb);
1904	skb_dst_set(skb, &rt->dst);
1905	ip_decrease_ttl(ip_hdr(skb));
1906
1907	/* FIXME: forward and output firewalls used to be called here.
1908	 * What do we do with netfilter? -- RR
1909	 */
1910	if (vif->flags & VIFF_TUNNEL) {
1911		ip_encap(net, skb, vif->local, vif->remote);
1912		/* FIXME: extra output firewall step used to be here. --RR */
1913		vif->dev->stats.tx_packets++;
1914		vif->dev->stats.tx_bytes += skb->len;
1915	}
1916
1917	IPCB(skb)->flags |= IPSKB_FORWARDED;
1918
1919	/* RFC1584 teaches, that DVMRP/PIM router must deliver packets locally
1920	 * not only before forwarding, but after forwarding on all output
1921	 * interfaces. It is clear, if mrouter runs a multicasting
1922	 * program, it should receive packets not depending to what interface
1923	 * program is joined.
1924	 * If we will not make it, the program will have to join on all
1925	 * interfaces. On the other hand, multihoming host (or router, but
1926	 * not mrouter) cannot join to more than one interface - it will
1927	 * result in receiving multiple packets.
1928	 */
1929	NF_HOOK(NFPROTO_IPV4, NF_INET_FORWARD,
1930		net, NULL, skb, skb->dev, dev,
1931		ipmr_forward_finish);
1932	return;
1933
1934out_free:
1935	kfree_skb(skb);
1936}
1937
1938static int ipmr_find_vif(struct mr_table *mrt, struct net_device *dev)
 
1939{
1940	int ct;
1941
1942	for (ct = mrt->maxvif-1; ct >= 0; ct--) {
1943		if (mrt->vif_table[ct].dev == dev)
1944			break;
1945	}
1946	return ct;
1947}
1948
1949/* "local" means that we should preserve one skb (for local delivery) */
 
1950static void ip_mr_forward(struct net *net, struct mr_table *mrt,
1951			  struct net_device *dev, struct sk_buff *skb,
1952			  struct mfc_cache *c, int local)
1953{
1954	int true_vifi = ipmr_find_vif(mrt, dev);
1955	int psend = -1;
1956	int vif, ct;
1957
1958	vif = c->_c.mfc_parent;
1959	c->_c.mfc_un.res.pkt++;
1960	c->_c.mfc_un.res.bytes += skb->len;
1961	c->_c.mfc_un.res.lastuse = jiffies;
1962
1963	if (c->mfc_origin == htonl(INADDR_ANY) && true_vifi >= 0) {
1964		struct mfc_cache *cache_proxy;
1965
1966		/* For an (*,G) entry, we only check that the incomming
1967		 * interface is part of the static tree.
1968		 */
1969		cache_proxy = mr_mfc_find_any_parent(mrt, vif);
1970		if (cache_proxy &&
1971		    cache_proxy->_c.mfc_un.res.ttls[true_vifi] < 255)
1972			goto forward;
1973	}
1974
1975	/* Wrong interface: drop packet and (maybe) send PIM assert. */
1976	if (mrt->vif_table[vif].dev != dev) {
1977		if (rt_is_output_route(skb_rtable(skb))) {
1978			/* It is our own packet, looped back.
1979			 * Very complicated situation...
1980			 *
1981			 * The best workaround until routing daemons will be
1982			 * fixed is not to redistribute packet, if it was
1983			 * send through wrong interface. It means, that
1984			 * multicast applications WILL NOT work for
1985			 * (S,G), which have default multicast route pointing
1986			 * to wrong oif. In any case, it is not a good
1987			 * idea to use multicasting applications on router.
1988			 */
1989			goto dont_forward;
1990		}
1991
1992		c->_c.mfc_un.res.wrong_if++;
1993
1994		if (true_vifi >= 0 && mrt->mroute_do_assert &&
1995		    /* pimsm uses asserts, when switching from RPT to SPT,
1996		     * so that we cannot check that packet arrived on an oif.
1997		     * It is bad, but otherwise we would need to move pretty
1998		     * large chunk of pimd to kernel. Ough... --ANK
1999		     */
2000		    (mrt->mroute_do_pim ||
2001		     c->_c.mfc_un.res.ttls[true_vifi] < 255) &&
2002		    time_after(jiffies,
2003			       c->_c.mfc_un.res.last_assert +
2004			       MFC_ASSERT_THRESH)) {
2005			c->_c.mfc_un.res.last_assert = jiffies;
2006			ipmr_cache_report(mrt, skb, true_vifi, IGMPMSG_WRONGVIF);
2007			if (mrt->mroute_do_wrvifwhole)
2008				ipmr_cache_report(mrt, skb, true_vifi,
2009						  IGMPMSG_WRVIFWHOLE);
2010		}
2011		goto dont_forward;
2012	}
2013
2014forward:
2015	mrt->vif_table[vif].pkt_in++;
2016	mrt->vif_table[vif].bytes_in += skb->len;
 
 
2017
2018	/* Forward the frame */
2019	if (c->mfc_origin == htonl(INADDR_ANY) &&
2020	    c->mfc_mcastgrp == htonl(INADDR_ANY)) {
2021		if (true_vifi >= 0 &&
2022		    true_vifi != c->_c.mfc_parent &&
2023		    ip_hdr(skb)->ttl >
2024				c->_c.mfc_un.res.ttls[c->_c.mfc_parent]) {
2025			/* It's an (*,*) entry and the packet is not coming from
2026			 * the upstream: forward the packet to the upstream
2027			 * only.
2028			 */
2029			psend = c->_c.mfc_parent;
2030			goto last_forward;
2031		}
2032		goto dont_forward;
2033	}
2034	for (ct = c->_c.mfc_un.res.maxvif - 1;
2035	     ct >= c->_c.mfc_un.res.minvif; ct--) {
2036		/* For (*,G) entry, don't forward to the incoming interface */
2037		if ((c->mfc_origin != htonl(INADDR_ANY) ||
2038		     ct != true_vifi) &&
2039		    ip_hdr(skb)->ttl > c->_c.mfc_un.res.ttls[ct]) {
2040			if (psend != -1) {
2041				struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
2042
2043				if (skb2)
2044					ipmr_queue_xmit(net, mrt, true_vifi,
2045							skb2, psend);
2046			}
2047			psend = ct;
2048		}
2049	}
2050last_forward:
2051	if (psend != -1) {
2052		if (local) {
2053			struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
2054
2055			if (skb2)
2056				ipmr_queue_xmit(net, mrt, true_vifi, skb2,
2057						psend);
2058		} else {
2059			ipmr_queue_xmit(net, mrt, true_vifi, skb, psend);
2060			return;
2061		}
2062	}
2063
2064dont_forward:
2065	if (!local)
2066		kfree_skb(skb);
2067}
2068
2069static struct mr_table *ipmr_rt_fib_lookup(struct net *net, struct sk_buff *skb)
2070{
2071	struct rtable *rt = skb_rtable(skb);
2072	struct iphdr *iph = ip_hdr(skb);
2073	struct flowi4 fl4 = {
2074		.daddr = iph->daddr,
2075		.saddr = iph->saddr,
2076		.flowi4_tos = RT_TOS(iph->tos),
2077		.flowi4_oif = (rt_is_output_route(rt) ?
2078			       skb->dev->ifindex : 0),
2079		.flowi4_iif = (rt_is_output_route(rt) ?
2080			       LOOPBACK_IFINDEX :
2081			       skb->dev->ifindex),
2082		.flowi4_mark = skb->mark,
2083	};
2084	struct mr_table *mrt;
2085	int err;
2086
2087	err = ipmr_fib_lookup(net, &fl4, &mrt);
2088	if (err)
2089		return ERR_PTR(err);
2090	return mrt;
2091}
2092
2093/* Multicast packets for forwarding arrive here
2094 * Called with rcu_read_lock();
2095 */
2096int ip_mr_input(struct sk_buff *skb)
2097{
2098	struct mfc_cache *cache;
2099	struct net *net = dev_net(skb->dev);
2100	int local = skb_rtable(skb)->rt_flags & RTCF_LOCAL;
2101	struct mr_table *mrt;
2102	struct net_device *dev;
2103
2104	/* skb->dev passed in is the loX master dev for vrfs.
2105	 * As there are no vifs associated with loopback devices,
2106	 * get the proper interface that does have a vif associated with it.
2107	 */
2108	dev = skb->dev;
2109	if (netif_is_l3_master(skb->dev)) {
2110		dev = dev_get_by_index_rcu(net, IPCB(skb)->iif);
2111		if (!dev) {
2112			kfree_skb(skb);
2113			return -ENODEV;
2114		}
2115	}
2116
2117	/* Packet is looped back after forward, it should not be
2118	 * forwarded second time, but still can be delivered locally.
2119	 */
2120	if (IPCB(skb)->flags & IPSKB_FORWARDED)
2121		goto dont_forward;
2122
2123	mrt = ipmr_rt_fib_lookup(net, skb);
2124	if (IS_ERR(mrt)) {
2125		kfree_skb(skb);
2126		return PTR_ERR(mrt);
2127	}
2128	if (!local) {
2129		if (IPCB(skb)->opt.router_alert) {
2130			if (ip_call_ra_chain(skb))
2131				return 0;
2132		} else if (ip_hdr(skb)->protocol == IPPROTO_IGMP) {
2133			/* IGMPv1 (and broken IGMPv2 implementations sort of
2134			 * Cisco IOS <= 11.2(8)) do not put router alert
2135			 * option to IGMP packets destined to routable
2136			 * groups. It is very bad, because it means
2137			 * that we can forward NO IGMP messages.
2138			 */
2139			struct sock *mroute_sk;
2140
2141			mroute_sk = rcu_dereference(mrt->mroute_sk);
2142			if (mroute_sk) {
2143				nf_reset_ct(skb);
2144				raw_rcv(mroute_sk, skb);
2145				return 0;
2146			}
2147		    }
2148	}
2149
2150	/* already under rcu_read_lock() */
2151	cache = ipmr_cache_find(mrt, ip_hdr(skb)->saddr, ip_hdr(skb)->daddr);
2152	if (!cache) {
2153		int vif = ipmr_find_vif(mrt, dev);
2154
2155		if (vif >= 0)
2156			cache = ipmr_cache_find_any(mrt, ip_hdr(skb)->daddr,
2157						    vif);
2158	}
2159
2160	/* No usable cache entry */
2161	if (!cache) {
2162		int vif;
2163
2164		if (local) {
2165			struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
2166			ip_local_deliver(skb);
2167			if (!skb2)
2168				return -ENOBUFS;
2169			skb = skb2;
2170		}
2171
2172		read_lock(&mrt_lock);
2173		vif = ipmr_find_vif(mrt, dev);
2174		if (vif >= 0) {
2175			int err2 = ipmr_cache_unresolved(mrt, vif, skb, dev);
2176			read_unlock(&mrt_lock);
2177
2178			return err2;
2179		}
2180		read_unlock(&mrt_lock);
2181		kfree_skb(skb);
2182		return -ENODEV;
2183	}
2184
2185	read_lock(&mrt_lock);
2186	ip_mr_forward(net, mrt, dev, skb, cache, local);
2187	read_unlock(&mrt_lock);
2188
2189	if (local)
2190		return ip_local_deliver(skb);
2191
2192	return 0;
2193
2194dont_forward:
2195	if (local)
2196		return ip_local_deliver(skb);
2197	kfree_skb(skb);
2198	return 0;
2199}
2200
2201#ifdef CONFIG_IP_PIMSM_V1
2202/* Handle IGMP messages of PIMv1 */
2203int pim_rcv_v1(struct sk_buff *skb)
2204{
2205	struct igmphdr *pim;
2206	struct net *net = dev_net(skb->dev);
2207	struct mr_table *mrt;
2208
2209	if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
2210		goto drop;
2211
2212	pim = igmp_hdr(skb);
2213
2214	mrt = ipmr_rt_fib_lookup(net, skb);
2215	if (IS_ERR(mrt))
2216		goto drop;
2217	if (!mrt->mroute_do_pim ||
2218	    pim->group != PIM_V1_VERSION || pim->code != PIM_V1_REGISTER)
2219		goto drop;
2220
2221	if (__pim_rcv(mrt, skb, sizeof(*pim))) {
2222drop:
2223		kfree_skb(skb);
2224	}
2225	return 0;
2226}
2227#endif
2228
2229#ifdef CONFIG_IP_PIMSM_V2
2230static int pim_rcv(struct sk_buff *skb)
2231{
2232	struct pimreghdr *pim;
2233	struct net *net = dev_net(skb->dev);
2234	struct mr_table *mrt;
2235
2236	if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
2237		goto drop;
2238
2239	pim = (struct pimreghdr *)skb_transport_header(skb);
2240	if (pim->type != ((PIM_VERSION << 4) | (PIM_TYPE_REGISTER)) ||
2241	    (pim->flags & PIM_NULL_REGISTER) ||
2242	    (ip_compute_csum((void *)pim, sizeof(*pim)) != 0 &&
2243	     csum_fold(skb_checksum(skb, 0, skb->len, 0))))
2244		goto drop;
2245
2246	mrt = ipmr_rt_fib_lookup(net, skb);
2247	if (IS_ERR(mrt))
2248		goto drop;
2249	if (__pim_rcv(mrt, skb, sizeof(*pim))) {
2250drop:
2251		kfree_skb(skb);
2252	}
2253	return 0;
2254}
2255#endif
2256
2257int ipmr_get_route(struct net *net, struct sk_buff *skb,
2258		   __be32 saddr, __be32 daddr,
2259		   struct rtmsg *rtm, u32 portid)
2260{
2261	struct mfc_cache *cache;
2262	struct mr_table *mrt;
2263	int err;
2264
2265	mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
2266	if (!mrt)
 
 
2267		return -ENOENT;
 
2268
2269	rcu_read_lock();
2270	cache = ipmr_cache_find(mrt, saddr, daddr);
2271	if (!cache && skb->dev) {
2272		int vif = ipmr_find_vif(mrt, skb->dev);
2273
2274		if (vif >= 0)
2275			cache = ipmr_cache_find_any(mrt, daddr, vif);
2276	}
2277	if (!cache) {
2278		struct sk_buff *skb2;
2279		struct iphdr *iph;
2280		struct net_device *dev;
2281		int vif = -1;
2282
2283		dev = skb->dev;
2284		read_lock(&mrt_lock);
2285		if (dev)
2286			vif = ipmr_find_vif(mrt, dev);
2287		if (vif < 0) {
2288			read_unlock(&mrt_lock);
2289			rcu_read_unlock();
2290			return -ENODEV;
2291		}
2292
2293		skb2 = skb_realloc_headroom(skb, sizeof(struct iphdr));
2294		if (!skb2) {
2295			read_unlock(&mrt_lock);
2296			rcu_read_unlock();
2297			return -ENOMEM;
2298		}
2299
2300		NETLINK_CB(skb2).portid = portid;
2301		skb_push(skb2, sizeof(struct iphdr));
2302		skb_reset_network_header(skb2);
2303		iph = ip_hdr(skb2);
2304		iph->ihl = sizeof(struct iphdr) >> 2;
2305		iph->saddr = saddr;
2306		iph->daddr = daddr;
2307		iph->version = 0;
2308		err = ipmr_cache_unresolved(mrt, vif, skb2, dev);
2309		read_unlock(&mrt_lock);
2310		rcu_read_unlock();
2311		return err;
2312	}
2313
2314	read_lock(&mrt_lock);
2315	err = mr_fill_mroute(mrt, skb, &cache->_c, rtm);
2316	read_unlock(&mrt_lock);
2317	rcu_read_unlock();
2318	return err;
2319}
2320
2321static int ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
2322			    u32 portid, u32 seq, struct mfc_cache *c, int cmd,
2323			    int flags)
2324{
2325	struct nlmsghdr *nlh;
2326	struct rtmsg *rtm;
2327	int err;
2328
2329	nlh = nlmsg_put(skb, portid, seq, cmd, sizeof(*rtm), flags);
2330	if (!nlh)
2331		return -EMSGSIZE;
2332
2333	rtm = nlmsg_data(nlh);
2334	rtm->rtm_family   = RTNL_FAMILY_IPMR;
2335	rtm->rtm_dst_len  = 32;
2336	rtm->rtm_src_len  = 32;
2337	rtm->rtm_tos      = 0;
2338	rtm->rtm_table    = mrt->id;
2339	if (nla_put_u32(skb, RTA_TABLE, mrt->id))
2340		goto nla_put_failure;
2341	rtm->rtm_type     = RTN_MULTICAST;
2342	rtm->rtm_scope    = RT_SCOPE_UNIVERSE;
2343	if (c->_c.mfc_flags & MFC_STATIC)
2344		rtm->rtm_protocol = RTPROT_STATIC;
2345	else
2346		rtm->rtm_protocol = RTPROT_MROUTED;
2347	rtm->rtm_flags    = 0;
2348
2349	if (nla_put_in_addr(skb, RTA_SRC, c->mfc_origin) ||
2350	    nla_put_in_addr(skb, RTA_DST, c->mfc_mcastgrp))
2351		goto nla_put_failure;
2352	err = mr_fill_mroute(mrt, skb, &c->_c, rtm);
2353	/* do not break the dump if cache is unresolved */
2354	if (err < 0 && err != -ENOENT)
2355		goto nla_put_failure;
2356
2357	nlmsg_end(skb, nlh);
2358	return 0;
2359
2360nla_put_failure:
2361	nlmsg_cancel(skb, nlh);
2362	return -EMSGSIZE;
2363}
2364
2365static int _ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
2366			     u32 portid, u32 seq, struct mr_mfc *c, int cmd,
2367			     int flags)
2368{
2369	return ipmr_fill_mroute(mrt, skb, portid, seq, (struct mfc_cache *)c,
2370				cmd, flags);
2371}
2372
2373static size_t mroute_msgsize(bool unresolved, int maxvif)
2374{
2375	size_t len =
2376		NLMSG_ALIGN(sizeof(struct rtmsg))
2377		+ nla_total_size(4)	/* RTA_TABLE */
2378		+ nla_total_size(4)	/* RTA_SRC */
2379		+ nla_total_size(4)	/* RTA_DST */
2380		;
2381
2382	if (!unresolved)
2383		len = len
2384		      + nla_total_size(4)	/* RTA_IIF */
2385		      + nla_total_size(0)	/* RTA_MULTIPATH */
2386		      + maxvif * NLA_ALIGN(sizeof(struct rtnexthop))
2387						/* RTA_MFC_STATS */
2388		      + nla_total_size_64bit(sizeof(struct rta_mfc_stats))
2389		;
2390
2391	return len;
2392}
2393
2394static void mroute_netlink_event(struct mr_table *mrt, struct mfc_cache *mfc,
2395				 int cmd)
2396{
2397	struct net *net = read_pnet(&mrt->net);
2398	struct sk_buff *skb;
2399	int err = -ENOBUFS;
2400
2401	skb = nlmsg_new(mroute_msgsize(mfc->_c.mfc_parent >= MAXVIFS,
2402				       mrt->maxvif),
2403			GFP_ATOMIC);
2404	if (!skb)
2405		goto errout;
2406
2407	err = ipmr_fill_mroute(mrt, skb, 0, 0, mfc, cmd, 0);
2408	if (err < 0)
2409		goto errout;
2410
2411	rtnl_notify(skb, net, 0, RTNLGRP_IPV4_MROUTE, NULL, GFP_ATOMIC);
2412	return;
2413
2414errout:
2415	kfree_skb(skb);
2416	if (err < 0)
2417		rtnl_set_sk_err(net, RTNLGRP_IPV4_MROUTE, err);
2418}
2419
2420static size_t igmpmsg_netlink_msgsize(size_t payloadlen)
2421{
2422	size_t len =
2423		NLMSG_ALIGN(sizeof(struct rtgenmsg))
2424		+ nla_total_size(1)	/* IPMRA_CREPORT_MSGTYPE */
2425		+ nla_total_size(4)	/* IPMRA_CREPORT_VIF_ID */
2426		+ nla_total_size(4)	/* IPMRA_CREPORT_SRC_ADDR */
2427		+ nla_total_size(4)	/* IPMRA_CREPORT_DST_ADDR */
 
2428					/* IPMRA_CREPORT_PKT */
2429		+ nla_total_size(payloadlen)
2430		;
2431
2432	return len;
2433}
2434
2435static void igmpmsg_netlink_event(struct mr_table *mrt, struct sk_buff *pkt)
2436{
2437	struct net *net = read_pnet(&mrt->net);
2438	struct nlmsghdr *nlh;
2439	struct rtgenmsg *rtgenm;
2440	struct igmpmsg *msg;
2441	struct sk_buff *skb;
2442	struct nlattr *nla;
2443	int payloadlen;
2444
2445	payloadlen = pkt->len - sizeof(struct igmpmsg);
2446	msg = (struct igmpmsg *)skb_network_header(pkt);
2447
2448	skb = nlmsg_new(igmpmsg_netlink_msgsize(payloadlen), GFP_ATOMIC);
2449	if (!skb)
2450		goto errout;
2451
2452	nlh = nlmsg_put(skb, 0, 0, RTM_NEWCACHEREPORT,
2453			sizeof(struct rtgenmsg), 0);
2454	if (!nlh)
2455		goto errout;
2456	rtgenm = nlmsg_data(nlh);
2457	rtgenm->rtgen_family = RTNL_FAMILY_IPMR;
2458	if (nla_put_u8(skb, IPMRA_CREPORT_MSGTYPE, msg->im_msgtype) ||
2459	    nla_put_u32(skb, IPMRA_CREPORT_VIF_ID, msg->im_vif) ||
2460	    nla_put_in_addr(skb, IPMRA_CREPORT_SRC_ADDR,
2461			    msg->im_src.s_addr) ||
2462	    nla_put_in_addr(skb, IPMRA_CREPORT_DST_ADDR,
2463			    msg->im_dst.s_addr))
 
2464		goto nla_put_failure;
2465
2466	nla = nla_reserve(skb, IPMRA_CREPORT_PKT, payloadlen);
2467	if (!nla || skb_copy_bits(pkt, sizeof(struct igmpmsg),
2468				  nla_data(nla), payloadlen))
2469		goto nla_put_failure;
2470
2471	nlmsg_end(skb, nlh);
2472
2473	rtnl_notify(skb, net, 0, RTNLGRP_IPV4_MROUTE_R, NULL, GFP_ATOMIC);
2474	return;
2475
2476nla_put_failure:
2477	nlmsg_cancel(skb, nlh);
2478errout:
2479	kfree_skb(skb);
2480	rtnl_set_sk_err(net, RTNLGRP_IPV4_MROUTE_R, -ENOBUFS);
2481}
2482
2483static int ipmr_rtm_valid_getroute_req(struct sk_buff *skb,
2484				       const struct nlmsghdr *nlh,
2485				       struct nlattr **tb,
2486				       struct netlink_ext_ack *extack)
2487{
2488	struct rtmsg *rtm;
2489	int i, err;
2490
2491	if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*rtm))) {
2492		NL_SET_ERR_MSG(extack, "ipv4: Invalid header for multicast route get request");
2493		return -EINVAL;
2494	}
2495
2496	if (!netlink_strict_get_check(skb))
2497		return nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX,
2498					      rtm_ipv4_policy, extack);
2499
2500	rtm = nlmsg_data(nlh);
2501	if ((rtm->rtm_src_len && rtm->rtm_src_len != 32) ||
2502	    (rtm->rtm_dst_len && rtm->rtm_dst_len != 32) ||
2503	    rtm->rtm_tos || rtm->rtm_table || rtm->rtm_protocol ||
2504	    rtm->rtm_scope || rtm->rtm_type || rtm->rtm_flags) {
2505		NL_SET_ERR_MSG(extack, "ipv4: Invalid values in header for multicast route get request");
2506		return -EINVAL;
2507	}
2508
2509	err = nlmsg_parse_deprecated_strict(nlh, sizeof(*rtm), tb, RTA_MAX,
2510					    rtm_ipv4_policy, extack);
2511	if (err)
2512		return err;
2513
2514	if ((tb[RTA_SRC] && !rtm->rtm_src_len) ||
2515	    (tb[RTA_DST] && !rtm->rtm_dst_len)) {
2516		NL_SET_ERR_MSG(extack, "ipv4: rtm_src_len and rtm_dst_len must be 32 for IPv4");
2517		return -EINVAL;
2518	}
2519
2520	for (i = 0; i <= RTA_MAX; i++) {
2521		if (!tb[i])
2522			continue;
2523
2524		switch (i) {
2525		case RTA_SRC:
2526		case RTA_DST:
2527		case RTA_TABLE:
2528			break;
2529		default:
2530			NL_SET_ERR_MSG(extack, "ipv4: Unsupported attribute in multicast route get request");
2531			return -EINVAL;
2532		}
2533	}
2534
2535	return 0;
2536}
2537
2538static int ipmr_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh,
2539			     struct netlink_ext_ack *extack)
2540{
2541	struct net *net = sock_net(in_skb->sk);
2542	struct nlattr *tb[RTA_MAX + 1];
2543	struct sk_buff *skb = NULL;
2544	struct mfc_cache *cache;
2545	struct mr_table *mrt;
2546	__be32 src, grp;
2547	u32 tableid;
2548	int err;
2549
2550	err = ipmr_rtm_valid_getroute_req(in_skb, nlh, tb, extack);
2551	if (err < 0)
2552		goto errout;
2553
2554	src = tb[RTA_SRC] ? nla_get_in_addr(tb[RTA_SRC]) : 0;
2555	grp = tb[RTA_DST] ? nla_get_in_addr(tb[RTA_DST]) : 0;
2556	tableid = tb[RTA_TABLE] ? nla_get_u32(tb[RTA_TABLE]) : 0;
2557
2558	mrt = ipmr_get_table(net, tableid ? tableid : RT_TABLE_DEFAULT);
2559	if (!mrt) {
2560		err = -ENOENT;
2561		goto errout_free;
2562	}
2563
2564	/* entries are added/deleted only under RTNL */
2565	rcu_read_lock();
2566	cache = ipmr_cache_find(mrt, src, grp);
2567	rcu_read_unlock();
2568	if (!cache) {
2569		err = -ENOENT;
2570		goto errout_free;
2571	}
2572
2573	skb = nlmsg_new(mroute_msgsize(false, mrt->maxvif), GFP_KERNEL);
2574	if (!skb) {
2575		err = -ENOBUFS;
2576		goto errout_free;
2577	}
2578
2579	err = ipmr_fill_mroute(mrt, skb, NETLINK_CB(in_skb).portid,
2580			       nlh->nlmsg_seq, cache,
2581			       RTM_NEWROUTE, 0);
2582	if (err < 0)
2583		goto errout_free;
2584
2585	err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
2586
2587errout:
2588	return err;
2589
2590errout_free:
2591	kfree_skb(skb);
2592	goto errout;
2593}
2594
2595static int ipmr_rtm_dumproute(struct sk_buff *skb, struct netlink_callback *cb)
2596{
2597	struct fib_dump_filter filter = {};
 
 
2598	int err;
2599
2600	if (cb->strict_check) {
2601		err = ip_valid_fib_dump_req(sock_net(skb->sk), cb->nlh,
2602					    &filter, cb);
2603		if (err < 0)
2604			return err;
2605	}
2606
2607	if (filter.table_id) {
2608		struct mr_table *mrt;
2609
2610		mrt = ipmr_get_table(sock_net(skb->sk), filter.table_id);
2611		if (!mrt) {
2612			if (filter.dump_all_families)
2613				return skb->len;
2614
2615			NL_SET_ERR_MSG(cb->extack, "ipv4: MR table does not exist");
2616			return -ENOENT;
2617		}
2618		err = mr_table_dump(mrt, skb, cb, _ipmr_fill_mroute,
2619				    &mfc_unres_lock, &filter);
2620		return skb->len ? : err;
2621	}
2622
2623	return mr_rtm_dumproute(skb, cb, ipmr_mr_table_iter,
2624				_ipmr_fill_mroute, &mfc_unres_lock, &filter);
2625}
2626
2627static const struct nla_policy rtm_ipmr_policy[RTA_MAX + 1] = {
2628	[RTA_SRC]	= { .type = NLA_U32 },
2629	[RTA_DST]	= { .type = NLA_U32 },
2630	[RTA_IIF]	= { .type = NLA_U32 },
2631	[RTA_TABLE]	= { .type = NLA_U32 },
2632	[RTA_MULTIPATH]	= { .len = sizeof(struct rtnexthop) },
2633};
2634
2635static bool ipmr_rtm_validate_proto(unsigned char rtm_protocol)
2636{
2637	switch (rtm_protocol) {
2638	case RTPROT_STATIC:
2639	case RTPROT_MROUTED:
2640		return true;
2641	}
2642	return false;
2643}
2644
2645static int ipmr_nla_get_ttls(const struct nlattr *nla, struct mfcctl *mfcc)
2646{
2647	struct rtnexthop *rtnh = nla_data(nla);
2648	int remaining = nla_len(nla), vifi = 0;
2649
2650	while (rtnh_ok(rtnh, remaining)) {
2651		mfcc->mfcc_ttls[vifi] = rtnh->rtnh_hops;
2652		if (++vifi == MAXVIFS)
2653			break;
2654		rtnh = rtnh_next(rtnh, &remaining);
2655	}
2656
2657	return remaining > 0 ? -EINVAL : vifi;
2658}
2659
2660/* returns < 0 on error, 0 for ADD_MFC and 1 for ADD_MFC_PROXY */
2661static int rtm_to_ipmr_mfcc(struct net *net, struct nlmsghdr *nlh,
2662			    struct mfcctl *mfcc, int *mrtsock,
2663			    struct mr_table **mrtret,
2664			    struct netlink_ext_ack *extack)
2665{
2666	struct net_device *dev = NULL;
2667	u32 tblid = RT_TABLE_DEFAULT;
2668	struct mr_table *mrt;
2669	struct nlattr *attr;
2670	struct rtmsg *rtm;
2671	int ret, rem;
2672
2673	ret = nlmsg_validate_deprecated(nlh, sizeof(*rtm), RTA_MAX,
2674					rtm_ipmr_policy, extack);
2675	if (ret < 0)
2676		goto out;
2677	rtm = nlmsg_data(nlh);
2678
2679	ret = -EINVAL;
2680	if (rtm->rtm_family != RTNL_FAMILY_IPMR || rtm->rtm_dst_len != 32 ||
2681	    rtm->rtm_type != RTN_MULTICAST ||
2682	    rtm->rtm_scope != RT_SCOPE_UNIVERSE ||
2683	    !ipmr_rtm_validate_proto(rtm->rtm_protocol))
2684		goto out;
2685
2686	memset(mfcc, 0, sizeof(*mfcc));
2687	mfcc->mfcc_parent = -1;
2688	ret = 0;
2689	nlmsg_for_each_attr(attr, nlh, sizeof(struct rtmsg), rem) {
2690		switch (nla_type(attr)) {
2691		case RTA_SRC:
2692			mfcc->mfcc_origin.s_addr = nla_get_be32(attr);
2693			break;
2694		case RTA_DST:
2695			mfcc->mfcc_mcastgrp.s_addr = nla_get_be32(attr);
2696			break;
2697		case RTA_IIF:
2698			dev = __dev_get_by_index(net, nla_get_u32(attr));
2699			if (!dev) {
2700				ret = -ENODEV;
2701				goto out;
2702			}
2703			break;
2704		case RTA_MULTIPATH:
2705			if (ipmr_nla_get_ttls(attr, mfcc) < 0) {
2706				ret = -EINVAL;
2707				goto out;
2708			}
2709			break;
2710		case RTA_PREFSRC:
2711			ret = 1;
2712			break;
2713		case RTA_TABLE:
2714			tblid = nla_get_u32(attr);
2715			break;
2716		}
2717	}
2718	mrt = ipmr_get_table(net, tblid);
2719	if (!mrt) {
2720		ret = -ENOENT;
2721		goto out;
2722	}
2723	*mrtret = mrt;
2724	*mrtsock = rtm->rtm_protocol == RTPROT_MROUTED ? 1 : 0;
2725	if (dev)
2726		mfcc->mfcc_parent = ipmr_find_vif(mrt, dev);
2727
2728out:
2729	return ret;
2730}
2731
2732/* takes care of both newroute and delroute */
2733static int ipmr_rtm_route(struct sk_buff *skb, struct nlmsghdr *nlh,
2734			  struct netlink_ext_ack *extack)
2735{
2736	struct net *net = sock_net(skb->sk);
2737	int ret, mrtsock, parent;
2738	struct mr_table *tbl;
2739	struct mfcctl mfcc;
2740
2741	mrtsock = 0;
2742	tbl = NULL;
2743	ret = rtm_to_ipmr_mfcc(net, nlh, &mfcc, &mrtsock, &tbl, extack);
2744	if (ret < 0)
2745		return ret;
2746
2747	parent = ret ? mfcc.mfcc_parent : -1;
2748	if (nlh->nlmsg_type == RTM_NEWROUTE)
2749		return ipmr_mfc_add(net, tbl, &mfcc, mrtsock, parent);
2750	else
2751		return ipmr_mfc_delete(tbl, &mfcc, parent);
2752}
2753
2754static bool ipmr_fill_table(struct mr_table *mrt, struct sk_buff *skb)
2755{
2756	u32 queue_len = atomic_read(&mrt->cache_resolve_queue_len);
2757
2758	if (nla_put_u32(skb, IPMRA_TABLE_ID, mrt->id) ||
2759	    nla_put_u32(skb, IPMRA_TABLE_CACHE_RES_QUEUE_LEN, queue_len) ||
2760	    nla_put_s32(skb, IPMRA_TABLE_MROUTE_REG_VIF_NUM,
2761			mrt->mroute_reg_vif_num) ||
2762	    nla_put_u8(skb, IPMRA_TABLE_MROUTE_DO_ASSERT,
2763		       mrt->mroute_do_assert) ||
2764	    nla_put_u8(skb, IPMRA_TABLE_MROUTE_DO_PIM, mrt->mroute_do_pim) ||
2765	    nla_put_u8(skb, IPMRA_TABLE_MROUTE_DO_WRVIFWHOLE,
2766		       mrt->mroute_do_wrvifwhole))
2767		return false;
2768
2769	return true;
2770}
2771
2772static bool ipmr_fill_vif(struct mr_table *mrt, u32 vifid, struct sk_buff *skb)
2773{
 
2774	struct nlattr *vif_nest;
2775	struct vif_device *vif;
2776
 
 
2777	/* if the VIF doesn't exist just continue */
2778	if (!VIF_EXISTS(mrt, vifid))
2779		return true;
2780
2781	vif = &mrt->vif_table[vifid];
2782	vif_nest = nla_nest_start_noflag(skb, IPMRA_VIF);
2783	if (!vif_nest)
2784		return false;
2785	if (nla_put_u32(skb, IPMRA_VIFA_IFINDEX, vif->dev->ifindex) ||
 
2786	    nla_put_u32(skb, IPMRA_VIFA_VIF_ID, vifid) ||
2787	    nla_put_u16(skb, IPMRA_VIFA_FLAGS, vif->flags) ||
2788	    nla_put_u64_64bit(skb, IPMRA_VIFA_BYTES_IN, vif->bytes_in,
2789			      IPMRA_VIFA_PAD) ||
2790	    nla_put_u64_64bit(skb, IPMRA_VIFA_BYTES_OUT, vif->bytes_out,
2791			      IPMRA_VIFA_PAD) ||
2792	    nla_put_u64_64bit(skb, IPMRA_VIFA_PACKETS_IN, vif->pkt_in,
2793			      IPMRA_VIFA_PAD) ||
2794	    nla_put_u64_64bit(skb, IPMRA_VIFA_PACKETS_OUT, vif->pkt_out,
2795			      IPMRA_VIFA_PAD) ||
2796	    nla_put_be32(skb, IPMRA_VIFA_LOCAL_ADDR, vif->local) ||
2797	    nla_put_be32(skb, IPMRA_VIFA_REMOTE_ADDR, vif->remote)) {
2798		nla_nest_cancel(skb, vif_nest);
2799		return false;
2800	}
2801	nla_nest_end(skb, vif_nest);
2802
2803	return true;
2804}
2805
2806static int ipmr_valid_dumplink(const struct nlmsghdr *nlh,
2807			       struct netlink_ext_ack *extack)
2808{
2809	struct ifinfomsg *ifm;
2810
2811	if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
2812		NL_SET_ERR_MSG(extack, "ipv4: Invalid header for ipmr link dump");
2813		return -EINVAL;
2814	}
2815
2816	if (nlmsg_attrlen(nlh, sizeof(*ifm))) {
2817		NL_SET_ERR_MSG(extack, "Invalid data after header in ipmr link dump");
2818		return -EINVAL;
2819	}
2820
2821	ifm = nlmsg_data(nlh);
2822	if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
2823	    ifm->ifi_change || ifm->ifi_index) {
2824		NL_SET_ERR_MSG(extack, "Invalid values in header for ipmr link dump request");
2825		return -EINVAL;
2826	}
2827
2828	return 0;
2829}
2830
2831static int ipmr_rtm_dumplink(struct sk_buff *skb, struct netlink_callback *cb)
2832{
2833	struct net *net = sock_net(skb->sk);
2834	struct nlmsghdr *nlh = NULL;
2835	unsigned int t = 0, s_t;
2836	unsigned int e = 0, s_e;
2837	struct mr_table *mrt;
2838
2839	if (cb->strict_check) {
2840		int err = ipmr_valid_dumplink(cb->nlh, cb->extack);
2841
2842		if (err < 0)
2843			return err;
2844	}
2845
2846	s_t = cb->args[0];
2847	s_e = cb->args[1];
2848
2849	ipmr_for_each_table(mrt, net) {
2850		struct nlattr *vifs, *af;
2851		struct ifinfomsg *hdr;
2852		u32 i;
2853
2854		if (t < s_t)
2855			goto skip_table;
2856		nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid,
2857				cb->nlh->nlmsg_seq, RTM_NEWLINK,
2858				sizeof(*hdr), NLM_F_MULTI);
2859		if (!nlh)
2860			break;
2861
2862		hdr = nlmsg_data(nlh);
2863		memset(hdr, 0, sizeof(*hdr));
2864		hdr->ifi_family = RTNL_FAMILY_IPMR;
2865
2866		af = nla_nest_start_noflag(skb, IFLA_AF_SPEC);
2867		if (!af) {
2868			nlmsg_cancel(skb, nlh);
2869			goto out;
2870		}
2871
2872		if (!ipmr_fill_table(mrt, skb)) {
2873			nlmsg_cancel(skb, nlh);
2874			goto out;
2875		}
2876
2877		vifs = nla_nest_start_noflag(skb, IPMRA_TABLE_VIFS);
2878		if (!vifs) {
2879			nla_nest_end(skb, af);
2880			nlmsg_end(skb, nlh);
2881			goto out;
2882		}
2883		for (i = 0; i < mrt->maxvif; i++) {
2884			if (e < s_e)
2885				goto skip_entry;
2886			if (!ipmr_fill_vif(mrt, i, skb)) {
2887				nla_nest_end(skb, vifs);
2888				nla_nest_end(skb, af);
2889				nlmsg_end(skb, nlh);
2890				goto out;
2891			}
2892skip_entry:
2893			e++;
2894		}
2895		s_e = 0;
2896		e = 0;
2897		nla_nest_end(skb, vifs);
2898		nla_nest_end(skb, af);
2899		nlmsg_end(skb, nlh);
2900skip_table:
2901		t++;
2902	}
2903
2904out:
2905	cb->args[1] = e;
2906	cb->args[0] = t;
2907
2908	return skb->len;
2909}
2910
2911#ifdef CONFIG_PROC_FS
2912/* The /proc interfaces to multicast routing :
2913 * /proc/net/ip_mr_cache & /proc/net/ip_mr_vif
2914 */
2915
2916static void *ipmr_vif_seq_start(struct seq_file *seq, loff_t *pos)
2917	__acquires(mrt_lock)
2918{
2919	struct mr_vif_iter *iter = seq->private;
2920	struct net *net = seq_file_net(seq);
2921	struct mr_table *mrt;
2922
2923	mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
2924	if (!mrt)
 
 
2925		return ERR_PTR(-ENOENT);
 
2926
2927	iter->mrt = mrt;
2928
2929	read_lock(&mrt_lock);
2930	return mr_vif_seq_start(seq, pos);
2931}
2932
2933static void ipmr_vif_seq_stop(struct seq_file *seq, void *v)
2934	__releases(mrt_lock)
2935{
2936	read_unlock(&mrt_lock);
2937}
2938
2939static int ipmr_vif_seq_show(struct seq_file *seq, void *v)
2940{
2941	struct mr_vif_iter *iter = seq->private;
2942	struct mr_table *mrt = iter->mrt;
2943
2944	if (v == SEQ_START_TOKEN) {
2945		seq_puts(seq,
2946			 "Interface      BytesIn  PktsIn  BytesOut PktsOut Flags Local    Remote\n");
2947	} else {
2948		const struct vif_device *vif = v;
2949		const char *name =  vif->dev ?
2950				    vif->dev->name : "none";
2951
 
 
2952		seq_printf(seq,
2953			   "%2td %-10s %8ld %7ld  %8ld %7ld %05X %08X %08X\n",
2954			   vif - mrt->vif_table,
2955			   name, vif->bytes_in, vif->pkt_in,
2956			   vif->bytes_out, vif->pkt_out,
2957			   vif->flags, vif->local, vif->remote);
2958	}
2959	return 0;
2960}
2961
2962static const struct seq_operations ipmr_vif_seq_ops = {
2963	.start = ipmr_vif_seq_start,
2964	.next  = mr_vif_seq_next,
2965	.stop  = ipmr_vif_seq_stop,
2966	.show  = ipmr_vif_seq_show,
2967};
2968
2969static void *ipmr_mfc_seq_start(struct seq_file *seq, loff_t *pos)
2970{
2971	struct net *net = seq_file_net(seq);
2972	struct mr_table *mrt;
2973
2974	mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
2975	if (!mrt)
2976		return ERR_PTR(-ENOENT);
2977
2978	return mr_mfc_seq_start(seq, pos, mrt, &mfc_unres_lock);
2979}
2980
2981static int ipmr_mfc_seq_show(struct seq_file *seq, void *v)
2982{
2983	int n;
2984
2985	if (v == SEQ_START_TOKEN) {
2986		seq_puts(seq,
2987		 "Group    Origin   Iif     Pkts    Bytes    Wrong Oifs\n");
2988	} else {
2989		const struct mfc_cache *mfc = v;
2990		const struct mr_mfc_iter *it = seq->private;
2991		const struct mr_table *mrt = it->mrt;
2992
2993		seq_printf(seq, "%08X %08X %-3hd",
2994			   (__force u32) mfc->mfc_mcastgrp,
2995			   (__force u32) mfc->mfc_origin,
2996			   mfc->_c.mfc_parent);
2997
2998		if (it->cache != &mrt->mfc_unres_queue) {
2999			seq_printf(seq, " %8lu %8lu %8lu",
3000				   mfc->_c.mfc_un.res.pkt,
3001				   mfc->_c.mfc_un.res.bytes,
3002				   mfc->_c.mfc_un.res.wrong_if);
3003			for (n = mfc->_c.mfc_un.res.minvif;
3004			     n < mfc->_c.mfc_un.res.maxvif; n++) {
3005				if (VIF_EXISTS(mrt, n) &&
3006				    mfc->_c.mfc_un.res.ttls[n] < 255)
3007					seq_printf(seq,
3008					   " %2d:%-3d",
3009					   n, mfc->_c.mfc_un.res.ttls[n]);
3010			}
3011		} else {
3012			/* unresolved mfc_caches don't contain
3013			 * pkt, bytes and wrong_if values
3014			 */
3015			seq_printf(seq, " %8lu %8lu %8lu", 0ul, 0ul, 0ul);
3016		}
3017		seq_putc(seq, '\n');
3018	}
3019	return 0;
3020}
3021
3022static const struct seq_operations ipmr_mfc_seq_ops = {
3023	.start = ipmr_mfc_seq_start,
3024	.next  = mr_mfc_seq_next,
3025	.stop  = mr_mfc_seq_stop,
3026	.show  = ipmr_mfc_seq_show,
3027};
3028#endif
3029
3030#ifdef CONFIG_IP_PIMSM_V2
3031static const struct net_protocol pim_protocol = {
3032	.handler	=	pim_rcv,
3033	.netns_ok	=	1,
3034};
3035#endif
3036
3037static unsigned int ipmr_seq_read(struct net *net)
3038{
3039	ASSERT_RTNL();
3040
3041	return net->ipv4.ipmr_seq + ipmr_rules_seq_read(net);
3042}
3043
3044static int ipmr_dump(struct net *net, struct notifier_block *nb)
 
3045{
3046	return mr_dump(net, nb, RTNL_FAMILY_IPMR, ipmr_rules_dump,
3047		       ipmr_mr_table_iter, &mrt_lock);
3048}
3049
3050static const struct fib_notifier_ops ipmr_notifier_ops_template = {
3051	.family		= RTNL_FAMILY_IPMR,
3052	.fib_seq_read	= ipmr_seq_read,
3053	.fib_dump	= ipmr_dump,
3054	.owner		= THIS_MODULE,
3055};
3056
3057static int __net_init ipmr_notifier_init(struct net *net)
3058{
3059	struct fib_notifier_ops *ops;
3060
3061	net->ipv4.ipmr_seq = 0;
3062
3063	ops = fib_notifier_ops_register(&ipmr_notifier_ops_template, net);
3064	if (IS_ERR(ops))
3065		return PTR_ERR(ops);
3066	net->ipv4.ipmr_notifier_ops = ops;
3067
3068	return 0;
3069}
3070
3071static void __net_exit ipmr_notifier_exit(struct net *net)
3072{
3073	fib_notifier_ops_unregister(net->ipv4.ipmr_notifier_ops);
3074	net->ipv4.ipmr_notifier_ops = NULL;
3075}
3076
3077/* Setup for IP multicast routing */
3078static int __net_init ipmr_net_init(struct net *net)
3079{
3080	int err;
3081
3082	err = ipmr_notifier_init(net);
3083	if (err)
3084		goto ipmr_notifier_fail;
3085
3086	err = ipmr_rules_init(net);
3087	if (err < 0)
3088		goto ipmr_rules_fail;
3089
3090#ifdef CONFIG_PROC_FS
3091	err = -ENOMEM;
3092	if (!proc_create_net("ip_mr_vif", 0, net->proc_net, &ipmr_vif_seq_ops,
3093			sizeof(struct mr_vif_iter)))
3094		goto proc_vif_fail;
3095	if (!proc_create_net("ip_mr_cache", 0, net->proc_net, &ipmr_mfc_seq_ops,
3096			sizeof(struct mr_mfc_iter)))
3097		goto proc_cache_fail;
3098#endif
3099	return 0;
3100
3101#ifdef CONFIG_PROC_FS
3102proc_cache_fail:
3103	remove_proc_entry("ip_mr_vif", net->proc_net);
3104proc_vif_fail:
 
3105	ipmr_rules_exit(net);
 
3106#endif
3107ipmr_rules_fail:
3108	ipmr_notifier_exit(net);
3109ipmr_notifier_fail:
3110	return err;
3111}
3112
3113static void __net_exit ipmr_net_exit(struct net *net)
3114{
3115#ifdef CONFIG_PROC_FS
3116	remove_proc_entry("ip_mr_cache", net->proc_net);
3117	remove_proc_entry("ip_mr_vif", net->proc_net);
3118#endif
3119	ipmr_notifier_exit(net);
3120	ipmr_rules_exit(net);
 
 
 
 
 
 
 
 
 
3121}
3122
3123static struct pernet_operations ipmr_net_ops = {
3124	.init = ipmr_net_init,
3125	.exit = ipmr_net_exit,
 
 
 
 
 
 
 
 
 
 
 
 
3126};
3127
3128int __init ip_mr_init(void)
3129{
3130	int err;
3131
3132	mrt_cachep = kmem_cache_create("ip_mrt_cache",
3133				       sizeof(struct mfc_cache),
3134				       0, SLAB_HWCACHE_ALIGN | SLAB_PANIC,
3135				       NULL);
3136
3137	err = register_pernet_subsys(&ipmr_net_ops);
3138	if (err)
3139		goto reg_pernet_fail;
3140
3141	err = register_netdevice_notifier(&ip_mr_notifier);
3142	if (err)
3143		goto reg_notif_fail;
3144#ifdef CONFIG_IP_PIMSM_V2
3145	if (inet_add_protocol(&pim_protocol, IPPROTO_PIM) < 0) {
3146		pr_err("%s: can't add PIM protocol\n", __func__);
3147		err = -EAGAIN;
3148		goto add_proto_fail;
3149	}
3150#endif
3151	rtnl_register(RTNL_FAMILY_IPMR, RTM_GETROUTE,
3152		      ipmr_rtm_getroute, ipmr_rtm_dumproute, 0);
3153	rtnl_register(RTNL_FAMILY_IPMR, RTM_NEWROUTE,
3154		      ipmr_rtm_route, NULL, 0);
3155	rtnl_register(RTNL_FAMILY_IPMR, RTM_DELROUTE,
3156		      ipmr_rtm_route, NULL, 0);
3157
3158	rtnl_register(RTNL_FAMILY_IPMR, RTM_GETLINK,
3159		      NULL, ipmr_rtm_dumplink, 0);
3160	return 0;
3161
3162#ifdef CONFIG_IP_PIMSM_V2
3163add_proto_fail:
3164	unregister_netdevice_notifier(&ip_mr_notifier);
3165#endif
3166reg_notif_fail:
3167	unregister_pernet_subsys(&ipmr_net_ops);
3168reg_pernet_fail:
3169	kmem_cache_destroy(mrt_cachep);
3170	return err;
3171}
v6.13.7
   1// SPDX-License-Identifier: GPL-2.0-or-later
   2/*
   3 *	IP multicast routing support for mrouted 3.6/3.8
   4 *
   5 *		(c) 1995 Alan Cox, <alan@lxorguk.ukuu.org.uk>
   6 *	  Linux Consultancy and Custom Driver Development
   7 *
   8 *	Fixes:
   9 *	Michael Chastain	:	Incorrect size of copying.
  10 *	Alan Cox		:	Added the cache manager code
  11 *	Alan Cox		:	Fixed the clone/copy bug and device race.
  12 *	Mike McLagan		:	Routing by source
  13 *	Malcolm Beattie		:	Buffer handling fixes.
  14 *	Alexey Kuznetsov	:	Double buffer free and other fixes.
  15 *	SVR Anand		:	Fixed several multicast bugs and problems.
  16 *	Alexey Kuznetsov	:	Status, optimisations and more.
  17 *	Brad Parker		:	Better behaviour on mrouted upcall
  18 *					overflow.
  19 *      Carlos Picoto           :       PIMv1 Support
  20 *	Pavlin Ivanov Radoslavov:	PIMv2 Registers must checksum only PIM header
  21 *					Relax this requirement to work with older peers.
  22 */
  23
  24#include <linux/uaccess.h>
  25#include <linux/types.h>
  26#include <linux/cache.h>
  27#include <linux/capability.h>
  28#include <linux/errno.h>
  29#include <linux/mm.h>
  30#include <linux/kernel.h>
  31#include <linux/fcntl.h>
  32#include <linux/stat.h>
  33#include <linux/socket.h>
  34#include <linux/in.h>
  35#include <linux/inet.h>
  36#include <linux/netdevice.h>
  37#include <linux/inetdevice.h>
  38#include <linux/igmp.h>
  39#include <linux/proc_fs.h>
  40#include <linux/seq_file.h>
  41#include <linux/mroute.h>
  42#include <linux/init.h>
  43#include <linux/if_ether.h>
  44#include <linux/slab.h>
  45#include <net/net_namespace.h>
  46#include <net/ip.h>
  47#include <net/protocol.h>
  48#include <linux/skbuff.h>
  49#include <net/route.h>
  50#include <net/icmp.h>
  51#include <net/udp.h>
  52#include <net/raw.h>
  53#include <linux/notifier.h>
  54#include <linux/if_arp.h>
  55#include <linux/netfilter_ipv4.h>
  56#include <linux/compat.h>
  57#include <linux/export.h>
  58#include <linux/rhashtable.h>
  59#include <net/ip_tunnels.h>
  60#include <net/checksum.h>
  61#include <net/netlink.h>
  62#include <net/fib_rules.h>
  63#include <linux/netconf.h>
  64#include <net/rtnh.h>
  65#include <net/inet_dscp.h>
  66
  67#include <linux/nospec.h>
  68
  69struct ipmr_rule {
  70	struct fib_rule		common;
  71};
  72
  73struct ipmr_result {
  74	struct mr_table		*mrt;
  75};
  76
  77/* Big lock, protecting vif table, mrt cache and mroute socket state.
  78 * Note that the changes are semaphored via rtnl_lock.
  79 */
  80
  81static DEFINE_SPINLOCK(mrt_lock);
  82
  83static struct net_device *vif_dev_read(const struct vif_device *vif)
  84{
  85	return rcu_dereference(vif->dev);
  86}
  87
  88/* Multicast router control variables */
  89
  90/* Special spinlock for queue of unresolved entries */
  91static DEFINE_SPINLOCK(mfc_unres_lock);
  92
  93/* We return to original Alan's scheme. Hash table of resolved
  94 * entries is changed only in process context and protected
  95 * with weak lock mrt_lock. Queue of unresolved entries is protected
  96 * with strong spinlock mfc_unres_lock.
  97 *
  98 * In this case data path is free of exclusive locks at all.
  99 */
 100
 101static struct kmem_cache *mrt_cachep __ro_after_init;
 102
 103static struct mr_table *ipmr_new_table(struct net *net, u32 id);
 104static void ipmr_free_table(struct mr_table *mrt);
 105
 106static void ip_mr_forward(struct net *net, struct mr_table *mrt,
 107			  struct net_device *dev, struct sk_buff *skb,
 108			  struct mfc_cache *cache, int local);
 109static int ipmr_cache_report(const struct mr_table *mrt,
 110			     struct sk_buff *pkt, vifi_t vifi, int assert);
 111static void mroute_netlink_event(struct mr_table *mrt, struct mfc_cache *mfc,
 112				 int cmd);
 113static void igmpmsg_netlink_event(const struct mr_table *mrt, struct sk_buff *pkt);
 114static void mroute_clean_tables(struct mr_table *mrt, int flags);
 115static void ipmr_expire_process(struct timer_list *t);
 116
 117#ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
 118#define ipmr_for_each_table(mrt, net)					\
 119	list_for_each_entry_rcu(mrt, &net->ipv4.mr_tables, list,	\
 120				lockdep_rtnl_is_held() ||		\
 121				list_empty(&net->ipv4.mr_tables))
 122
 123static bool ipmr_can_free_table(struct net *net)
 124{
 125	return !check_net(net) || !net_initialized(net);
 126}
 127
 128static struct mr_table *ipmr_mr_table_iter(struct net *net,
 129					   struct mr_table *mrt)
 130{
 131	struct mr_table *ret;
 132
 133	if (!mrt)
 134		ret = list_entry_rcu(net->ipv4.mr_tables.next,
 135				     struct mr_table, list);
 136	else
 137		ret = list_entry_rcu(mrt->list.next,
 138				     struct mr_table, list);
 139
 140	if (&ret->list == &net->ipv4.mr_tables)
 141		return NULL;
 142	return ret;
 143}
 144
 145static struct mr_table *__ipmr_get_table(struct net *net, u32 id)
 146{
 147	struct mr_table *mrt;
 148
 149	ipmr_for_each_table(mrt, net) {
 150		if (mrt->id == id)
 151			return mrt;
 152	}
 153	return NULL;
 154}
 155
 156static struct mr_table *ipmr_get_table(struct net *net, u32 id)
 157{
 158	struct mr_table *mrt;
 159
 160	rcu_read_lock();
 161	mrt = __ipmr_get_table(net, id);
 162	rcu_read_unlock();
 163	return mrt;
 164}
 165
 166static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4,
 167			   struct mr_table **mrt)
 168{
 169	int err;
 170	struct ipmr_result res;
 171	struct fib_lookup_arg arg = {
 172		.result = &res,
 173		.flags = FIB_LOOKUP_NOREF,
 174	};
 175
 176	/* update flow if oif or iif point to device enslaved to l3mdev */
 177	l3mdev_update_flow(net, flowi4_to_flowi(flp4));
 178
 179	err = fib_rules_lookup(net->ipv4.mr_rules_ops,
 180			       flowi4_to_flowi(flp4), 0, &arg);
 181	if (err < 0)
 182		return err;
 183	*mrt = res.mrt;
 184	return 0;
 185}
 186
 187static int ipmr_rule_action(struct fib_rule *rule, struct flowi *flp,
 188			    int flags, struct fib_lookup_arg *arg)
 189{
 190	struct ipmr_result *res = arg->result;
 191	struct mr_table *mrt;
 192
 193	switch (rule->action) {
 194	case FR_ACT_TO_TBL:
 195		break;
 196	case FR_ACT_UNREACHABLE:
 197		return -ENETUNREACH;
 198	case FR_ACT_PROHIBIT:
 199		return -EACCES;
 200	case FR_ACT_BLACKHOLE:
 201	default:
 202		return -EINVAL;
 203	}
 204
 205	arg->table = fib_rule_get_table(rule, arg);
 206
 207	mrt = __ipmr_get_table(rule->fr_net, arg->table);
 208	if (!mrt)
 209		return -EAGAIN;
 210	res->mrt = mrt;
 211	return 0;
 212}
 213
 214static int ipmr_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
 215{
 216	return 1;
 217}
 218
 
 
 
 
 219static int ipmr_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
 220			       struct fib_rule_hdr *frh, struct nlattr **tb,
 221			       struct netlink_ext_ack *extack)
 222{
 223	return 0;
 224}
 225
 226static int ipmr_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
 227			     struct nlattr **tb)
 228{
 229	return 1;
 230}
 231
 232static int ipmr_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
 233			  struct fib_rule_hdr *frh)
 234{
 235	frh->dst_len = 0;
 236	frh->src_len = 0;
 237	frh->tos     = 0;
 238	return 0;
 239}
 240
 241static const struct fib_rules_ops __net_initconst ipmr_rules_ops_template = {
 242	.family		= RTNL_FAMILY_IPMR,
 243	.rule_size	= sizeof(struct ipmr_rule),
 244	.addr_size	= sizeof(u32),
 245	.action		= ipmr_rule_action,
 246	.match		= ipmr_rule_match,
 247	.configure	= ipmr_rule_configure,
 248	.compare	= ipmr_rule_compare,
 249	.fill		= ipmr_rule_fill,
 250	.nlgroup	= RTNLGRP_IPV4_RULE,
 
 251	.owner		= THIS_MODULE,
 252};
 253
 254static int __net_init ipmr_rules_init(struct net *net)
 255{
 256	struct fib_rules_ops *ops;
 257	struct mr_table *mrt;
 258	int err;
 259
 260	ops = fib_rules_register(&ipmr_rules_ops_template, net);
 261	if (IS_ERR(ops))
 262		return PTR_ERR(ops);
 263
 264	INIT_LIST_HEAD(&net->ipv4.mr_tables);
 265
 266	mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
 267	if (IS_ERR(mrt)) {
 268		err = PTR_ERR(mrt);
 269		goto err1;
 270	}
 271
 272	err = fib_default_rule_add(ops, 0x7fff, RT_TABLE_DEFAULT);
 273	if (err < 0)
 274		goto err2;
 275
 276	net->ipv4.mr_rules_ops = ops;
 277	return 0;
 278
 279err2:
 280	rtnl_lock();
 281	ipmr_free_table(mrt);
 282	rtnl_unlock();
 283err1:
 284	fib_rules_unregister(ops);
 285	return err;
 286}
 287
 288static void __net_exit ipmr_rules_exit(struct net *net)
 289{
 290	struct mr_table *mrt, *next;
 291
 292	ASSERT_RTNL();
 293	list_for_each_entry_safe(mrt, next, &net->ipv4.mr_tables, list) {
 294		list_del(&mrt->list);
 295		ipmr_free_table(mrt);
 296	}
 297	fib_rules_unregister(net->ipv4.mr_rules_ops);
 
 298}
 299
 300static int ipmr_rules_dump(struct net *net, struct notifier_block *nb,
 301			   struct netlink_ext_ack *extack)
 302{
 303	return fib_rules_dump(net, nb, RTNL_FAMILY_IPMR, extack);
 304}
 305
 306static unsigned int ipmr_rules_seq_read(const struct net *net)
 307{
 308	return fib_rules_seq_read(net, RTNL_FAMILY_IPMR);
 309}
 310
 311bool ipmr_rule_default(const struct fib_rule *rule)
 312{
 313	return fib_rule_matchall(rule) && rule->table == RT_TABLE_DEFAULT;
 314}
 315EXPORT_SYMBOL(ipmr_rule_default);
 316#else
 317#define ipmr_for_each_table(mrt, net) \
 318	for (mrt = net->ipv4.mrt; mrt; mrt = NULL)
 319
 320static bool ipmr_can_free_table(struct net *net)
 321{
 322	return !check_net(net);
 323}
 324
 325static struct mr_table *ipmr_mr_table_iter(struct net *net,
 326					   struct mr_table *mrt)
 327{
 328	if (!mrt)
 329		return net->ipv4.mrt;
 330	return NULL;
 331}
 332
 333static struct mr_table *ipmr_get_table(struct net *net, u32 id)
 334{
 335	return net->ipv4.mrt;
 336}
 337
 338#define __ipmr_get_table ipmr_get_table
 339
 340static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4,
 341			   struct mr_table **mrt)
 342{
 343	*mrt = net->ipv4.mrt;
 344	return 0;
 345}
 346
 347static int __net_init ipmr_rules_init(struct net *net)
 348{
 349	struct mr_table *mrt;
 350
 351	mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
 352	if (IS_ERR(mrt))
 353		return PTR_ERR(mrt);
 354	net->ipv4.mrt = mrt;
 355	return 0;
 356}
 357
 358static void __net_exit ipmr_rules_exit(struct net *net)
 359{
 360	ASSERT_RTNL();
 361	ipmr_free_table(net->ipv4.mrt);
 362	net->ipv4.mrt = NULL;
 
 363}
 364
 365static int ipmr_rules_dump(struct net *net, struct notifier_block *nb,
 366			   struct netlink_ext_ack *extack)
 367{
 368	return 0;
 369}
 370
 371static unsigned int ipmr_rules_seq_read(const struct net *net)
 372{
 373	return 0;
 374}
 375
 376bool ipmr_rule_default(const struct fib_rule *rule)
 377{
 378	return true;
 379}
 380EXPORT_SYMBOL(ipmr_rule_default);
 381#endif
 382
 383static inline int ipmr_hash_cmp(struct rhashtable_compare_arg *arg,
 384				const void *ptr)
 385{
 386	const struct mfc_cache_cmp_arg *cmparg = arg->key;
 387	const struct mfc_cache *c = ptr;
 388
 389	return cmparg->mfc_mcastgrp != c->mfc_mcastgrp ||
 390	       cmparg->mfc_origin != c->mfc_origin;
 391}
 392
 393static const struct rhashtable_params ipmr_rht_params = {
 394	.head_offset = offsetof(struct mr_mfc, mnode),
 395	.key_offset = offsetof(struct mfc_cache, cmparg),
 396	.key_len = sizeof(struct mfc_cache_cmp_arg),
 397	.nelem_hint = 3,
 398	.obj_cmpfn = ipmr_hash_cmp,
 399	.automatic_shrinking = true,
 400};
 401
 402static void ipmr_new_table_set(struct mr_table *mrt,
 403			       struct net *net)
 404{
 405#ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
 406	list_add_tail_rcu(&mrt->list, &net->ipv4.mr_tables);
 407#endif
 408}
 409
 410static struct mfc_cache_cmp_arg ipmr_mr_table_ops_cmparg_any = {
 411	.mfc_mcastgrp = htonl(INADDR_ANY),
 412	.mfc_origin = htonl(INADDR_ANY),
 413};
 414
 415static struct mr_table_ops ipmr_mr_table_ops = {
 416	.rht_params = &ipmr_rht_params,
 417	.cmparg_any = &ipmr_mr_table_ops_cmparg_any,
 418};
 419
 420static struct mr_table *ipmr_new_table(struct net *net, u32 id)
 421{
 422	struct mr_table *mrt;
 423
 424	/* "pimreg%u" should not exceed 16 bytes (IFNAMSIZ) */
 425	if (id != RT_TABLE_DEFAULT && id >= 1000000000)
 426		return ERR_PTR(-EINVAL);
 427
 428	mrt = __ipmr_get_table(net, id);
 429	if (mrt)
 430		return mrt;
 431
 432	return mr_table_alloc(net, id, &ipmr_mr_table_ops,
 433			      ipmr_expire_process, ipmr_new_table_set);
 434}
 435
 436static void ipmr_free_table(struct mr_table *mrt)
 437{
 438	struct net *net = read_pnet(&mrt->net);
 439
 440	WARN_ON_ONCE(!ipmr_can_free_table(net));
 441
 442	timer_shutdown_sync(&mrt->ipmr_expire_timer);
 443	mroute_clean_tables(mrt, MRT_FLUSH_VIFS | MRT_FLUSH_VIFS_STATIC |
 444				 MRT_FLUSH_MFC | MRT_FLUSH_MFC_STATIC);
 445	rhltable_destroy(&mrt->mfc_hash);
 446	kfree(mrt);
 447}
 448
 449/* Service routines creating virtual interfaces: DVMRP tunnels and PIMREG */
 450
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 451/* Initialize ipmr pimreg/tunnel in_device */
 452static bool ipmr_init_vif_indev(const struct net_device *dev)
 453{
 454	struct in_device *in_dev;
 455
 456	ASSERT_RTNL();
 457
 458	in_dev = __in_dev_get_rtnl(dev);
 459	if (!in_dev)
 460		return false;
 461	ipv4_devconf_setall(in_dev);
 462	neigh_parms_data_state_setall(in_dev->arp_parms);
 463	IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0;
 464
 465	return true;
 466}
 467
 468static struct net_device *ipmr_new_tunnel(struct net *net, struct vifctl *v)
 469{
 470	struct net_device *tunnel_dev, *new_dev;
 471	struct ip_tunnel_parm_kern p = { };
 472	int err;
 473
 474	tunnel_dev = __dev_get_by_name(net, "tunl0");
 475	if (!tunnel_dev)
 476		goto out;
 477
 478	p.iph.daddr = v->vifc_rmt_addr.s_addr;
 479	p.iph.saddr = v->vifc_lcl_addr.s_addr;
 480	p.iph.version = 4;
 481	p.iph.ihl = 5;
 482	p.iph.protocol = IPPROTO_IPIP;
 483	sprintf(p.name, "dvmrp%d", v->vifc_vifi);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 484
 485	if (!tunnel_dev->netdev_ops->ndo_tunnel_ctl)
 486		goto out;
 487	err = tunnel_dev->netdev_ops->ndo_tunnel_ctl(tunnel_dev, &p,
 488			SIOCADDTUNNEL);
 489	if (err)
 490		goto out;
 491
 492	new_dev = __dev_get_by_name(net, p.name);
 493	if (!new_dev)
 494		goto out;
 495
 496	new_dev->flags |= IFF_MULTICAST;
 497	if (!ipmr_init_vif_indev(new_dev))
 498		goto out_unregister;
 499	if (dev_open(new_dev, NULL))
 500		goto out_unregister;
 501	dev_hold(new_dev);
 502	err = dev_set_allmulti(new_dev, 1);
 503	if (err) {
 504		dev_close(new_dev);
 505		tunnel_dev->netdev_ops->ndo_tunnel_ctl(tunnel_dev, &p,
 506				SIOCDELTUNNEL);
 507		dev_put(new_dev);
 508		new_dev = ERR_PTR(err);
 509	}
 510	return new_dev;
 511
 512out_unregister:
 513	unregister_netdevice(new_dev);
 514out:
 515	return ERR_PTR(-ENOBUFS);
 516}
 517
 518#if defined(CONFIG_IP_PIMSM_V1) || defined(CONFIG_IP_PIMSM_V2)
 519static netdev_tx_t reg_vif_xmit(struct sk_buff *skb, struct net_device *dev)
 520{
 521	struct net *net = dev_net(dev);
 522	struct mr_table *mrt;
 523	struct flowi4 fl4 = {
 524		.flowi4_oif	= dev->ifindex,
 525		.flowi4_iif	= skb->skb_iif ? : LOOPBACK_IFINDEX,
 526		.flowi4_mark	= skb->mark,
 527	};
 528	int err;
 529
 530	err = ipmr_fib_lookup(net, &fl4, &mrt);
 531	if (err < 0) {
 532		kfree_skb(skb);
 533		return err;
 534	}
 535
 536	DEV_STATS_ADD(dev, tx_bytes, skb->len);
 537	DEV_STATS_INC(dev, tx_packets);
 538	rcu_read_lock();
 539
 540	/* Pairs with WRITE_ONCE() in vif_add() and vif_delete() */
 541	ipmr_cache_report(mrt, skb, READ_ONCE(mrt->mroute_reg_vif_num),
 542			  IGMPMSG_WHOLEPKT);
 543
 544	rcu_read_unlock();
 545	kfree_skb(skb);
 546	return NETDEV_TX_OK;
 547}
 548
 549static int reg_vif_get_iflink(const struct net_device *dev)
 550{
 551	return 0;
 552}
 553
 554static const struct net_device_ops reg_vif_netdev_ops = {
 555	.ndo_start_xmit	= reg_vif_xmit,
 556	.ndo_get_iflink = reg_vif_get_iflink,
 557};
 558
 559static void reg_vif_setup(struct net_device *dev)
 560{
 561	dev->type		= ARPHRD_PIMREG;
 562	dev->mtu		= ETH_DATA_LEN - sizeof(struct iphdr) - 8;
 563	dev->flags		= IFF_NOARP;
 564	dev->netdev_ops		= &reg_vif_netdev_ops;
 565	dev->needs_free_netdev	= true;
 566	dev->netns_local	= true;
 567}
 568
 569static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt)
 570{
 571	struct net_device *dev;
 572	char name[IFNAMSIZ];
 573
 574	if (mrt->id == RT_TABLE_DEFAULT)
 575		sprintf(name, "pimreg");
 576	else
 577		sprintf(name, "pimreg%u", mrt->id);
 578
 579	dev = alloc_netdev(0, name, NET_NAME_UNKNOWN, reg_vif_setup);
 580
 581	if (!dev)
 582		return NULL;
 583
 584	dev_net_set(dev, net);
 585
 586	if (register_netdevice(dev)) {
 587		free_netdev(dev);
 588		return NULL;
 589	}
 590
 591	if (!ipmr_init_vif_indev(dev))
 592		goto failure;
 593	if (dev_open(dev, NULL))
 594		goto failure;
 595
 596	dev_hold(dev);
 597
 598	return dev;
 599
 600failure:
 601	unregister_netdevice(dev);
 602	return NULL;
 603}
 604
 605/* called with rcu_read_lock() */
 606static int __pim_rcv(struct mr_table *mrt, struct sk_buff *skb,
 607		     unsigned int pimlen)
 608{
 609	struct net_device *reg_dev = NULL;
 610	struct iphdr *encap;
 611	int vif_num;
 612
 613	encap = (struct iphdr *)(skb_transport_header(skb) + pimlen);
 614	/* Check that:
 615	 * a. packet is really sent to a multicast group
 616	 * b. packet is not a NULL-REGISTER
 617	 * c. packet is not truncated
 618	 */
 619	if (!ipv4_is_multicast(encap->daddr) ||
 620	    encap->tot_len == 0 ||
 621	    ntohs(encap->tot_len) + pimlen > skb->len)
 622		return 1;
 623
 624	/* Pairs with WRITE_ONCE() in vif_add()/vid_delete() */
 625	vif_num = READ_ONCE(mrt->mroute_reg_vif_num);
 626	if (vif_num >= 0)
 627		reg_dev = vif_dev_read(&mrt->vif_table[vif_num]);
 
 628	if (!reg_dev)
 629		return 1;
 630
 631	skb->mac_header = skb->network_header;
 632	skb_pull(skb, (u8 *)encap - skb->data);
 633	skb_reset_network_header(skb);
 634	skb->protocol = htons(ETH_P_IP);
 635	skb->ip_summed = CHECKSUM_NONE;
 636
 637	skb_tunnel_rx(skb, reg_dev, dev_net(reg_dev));
 638
 639	netif_rx(skb);
 640
 641	return NET_RX_SUCCESS;
 642}
 643#else
 644static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt)
 645{
 646	return NULL;
 647}
 648#endif
 649
 650static int call_ipmr_vif_entry_notifiers(struct net *net,
 651					 enum fib_event_type event_type,
 652					 struct vif_device *vif,
 653					 struct net_device *vif_dev,
 654					 vifi_t vif_index, u32 tb_id)
 655{
 656	return mr_call_vif_notifiers(net, RTNL_FAMILY_IPMR, event_type,
 657				     vif, vif_dev, vif_index, tb_id,
 658				     &net->ipv4.ipmr_seq);
 659}
 660
 661static int call_ipmr_mfc_entry_notifiers(struct net *net,
 662					 enum fib_event_type event_type,
 663					 struct mfc_cache *mfc, u32 tb_id)
 664{
 665	return mr_call_mfc_notifiers(net, RTNL_FAMILY_IPMR, event_type,
 666				     &mfc->_c, tb_id, &net->ipv4.ipmr_seq);
 667}
 668
 669/**
 670 *	vif_delete - Delete a VIF entry
 671 *	@mrt: Table to delete from
 672 *	@vifi: VIF identifier to delete
 673 *	@notify: Set to 1, if the caller is a notifier_call
 674 *	@head: if unregistering the VIF, place it on this queue
 675 */
 676static int vif_delete(struct mr_table *mrt, int vifi, int notify,
 677		      struct list_head *head)
 678{
 679	struct net *net = read_pnet(&mrt->net);
 680	struct vif_device *v;
 681	struct net_device *dev;
 682	struct in_device *in_dev;
 683
 684	if (vifi < 0 || vifi >= mrt->maxvif)
 685		return -EADDRNOTAVAIL;
 686
 687	v = &mrt->vif_table[vifi];
 688
 689	dev = rtnl_dereference(v->dev);
 690	if (!dev)
 
 
 
 
 
 
 
 
 691		return -EADDRNOTAVAIL;
 
 
 
 
 692
 693	spin_lock(&mrt_lock);
 694	call_ipmr_vif_entry_notifiers(net, FIB_EVENT_VIF_DEL, v, dev,
 695				      vifi, mrt->id);
 696	RCU_INIT_POINTER(v->dev, NULL);
 697
 698	if (vifi == mrt->mroute_reg_vif_num) {
 699		/* Pairs with READ_ONCE() in ipmr_cache_report() and reg_vif_xmit() */
 700		WRITE_ONCE(mrt->mroute_reg_vif_num, -1);
 701	}
 702	if (vifi + 1 == mrt->maxvif) {
 703		int tmp;
 704
 705		for (tmp = vifi - 1; tmp >= 0; tmp--) {
 706			if (VIF_EXISTS(mrt, tmp))
 707				break;
 708		}
 709		WRITE_ONCE(mrt->maxvif, tmp + 1);
 710	}
 711
 712	spin_unlock(&mrt_lock);
 713
 714	dev_set_allmulti(dev, -1);
 715
 716	in_dev = __in_dev_get_rtnl(dev);
 717	if (in_dev) {
 718		IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)--;
 719		inet_netconf_notify_devconf(dev_net(dev), RTM_NEWNETCONF,
 720					    NETCONFA_MC_FORWARDING,
 721					    dev->ifindex, &in_dev->cnf);
 722		ip_rt_multicast_event(in_dev);
 723	}
 724
 725	if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER) && !notify)
 726		unregister_netdevice_queue(dev, head);
 727
 728	netdev_put(dev, &v->dev_tracker);
 729	return 0;
 730}
 731
 732static void ipmr_cache_free_rcu(struct rcu_head *head)
 733{
 734	struct mr_mfc *c = container_of(head, struct mr_mfc, rcu);
 735
 736	kmem_cache_free(mrt_cachep, (struct mfc_cache *)c);
 737}
 738
 739static void ipmr_cache_free(struct mfc_cache *c)
 740{
 741	call_rcu(&c->_c.rcu, ipmr_cache_free_rcu);
 742}
 743
 744/* Destroy an unresolved cache entry, killing queued skbs
 745 * and reporting error to netlink readers.
 746 */
 747static void ipmr_destroy_unres(struct mr_table *mrt, struct mfc_cache *c)
 748{
 749	struct net *net = read_pnet(&mrt->net);
 750	struct sk_buff *skb;
 751	struct nlmsgerr *e;
 752
 753	atomic_dec(&mrt->cache_resolve_queue_len);
 754
 755	while ((skb = skb_dequeue(&c->_c.mfc_un.unres.unresolved))) {
 756		if (ip_hdr(skb)->version == 0) {
 757			struct nlmsghdr *nlh = skb_pull(skb,
 758							sizeof(struct iphdr));
 759			nlh->nlmsg_type = NLMSG_ERROR;
 760			nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr));
 761			skb_trim(skb, nlh->nlmsg_len);
 762			e = nlmsg_data(nlh);
 763			e->error = -ETIMEDOUT;
 764			memset(&e->msg, 0, sizeof(e->msg));
 765
 766			rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
 767		} else {
 768			kfree_skb(skb);
 769		}
 770	}
 771
 772	ipmr_cache_free(c);
 773}
 774
 775/* Timer process for the unresolved queue. */
 776static void ipmr_expire_process(struct timer_list *t)
 777{
 778	struct mr_table *mrt = from_timer(mrt, t, ipmr_expire_timer);
 779	struct mr_mfc *c, *next;
 780	unsigned long expires;
 781	unsigned long now;
 782
 783	if (!spin_trylock(&mfc_unres_lock)) {
 784		mod_timer(&mrt->ipmr_expire_timer, jiffies+HZ/10);
 785		return;
 786	}
 787
 788	if (list_empty(&mrt->mfc_unres_queue))
 789		goto out;
 790
 791	now = jiffies;
 792	expires = 10*HZ;
 793
 794	list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
 795		if (time_after(c->mfc_un.unres.expires, now)) {
 796			unsigned long interval = c->mfc_un.unres.expires - now;
 797			if (interval < expires)
 798				expires = interval;
 799			continue;
 800		}
 801
 802		list_del(&c->list);
 803		mroute_netlink_event(mrt, (struct mfc_cache *)c, RTM_DELROUTE);
 804		ipmr_destroy_unres(mrt, (struct mfc_cache *)c);
 805	}
 806
 807	if (!list_empty(&mrt->mfc_unres_queue))
 808		mod_timer(&mrt->ipmr_expire_timer, jiffies + expires);
 809
 810out:
 811	spin_unlock(&mfc_unres_lock);
 812}
 813
 814/* Fill oifs list. It is called under locked mrt_lock. */
 815static void ipmr_update_thresholds(struct mr_table *mrt, struct mr_mfc *cache,
 816				   unsigned char *ttls)
 817{
 818	int vifi;
 819
 820	cache->mfc_un.res.minvif = MAXVIFS;
 821	cache->mfc_un.res.maxvif = 0;
 822	memset(cache->mfc_un.res.ttls, 255, MAXVIFS);
 823
 824	for (vifi = 0; vifi < mrt->maxvif; vifi++) {
 825		if (VIF_EXISTS(mrt, vifi) &&
 826		    ttls[vifi] && ttls[vifi] < 255) {
 827			cache->mfc_un.res.ttls[vifi] = ttls[vifi];
 828			if (cache->mfc_un.res.minvif > vifi)
 829				cache->mfc_un.res.minvif = vifi;
 830			if (cache->mfc_un.res.maxvif <= vifi)
 831				cache->mfc_un.res.maxvif = vifi + 1;
 832		}
 833	}
 834	WRITE_ONCE(cache->mfc_un.res.lastuse, jiffies);
 835}
 836
 837static int vif_add(struct net *net, struct mr_table *mrt,
 838		   struct vifctl *vifc, int mrtsock)
 839{
 840	struct netdev_phys_item_id ppid = { };
 841	int vifi = vifc->vifc_vifi;
 842	struct vif_device *v = &mrt->vif_table[vifi];
 843	struct net_device *dev;
 844	struct in_device *in_dev;
 845	int err;
 846
 847	/* Is vif busy ? */
 848	if (VIF_EXISTS(mrt, vifi))
 849		return -EADDRINUSE;
 850
 851	switch (vifc->vifc_flags) {
 852	case VIFF_REGISTER:
 853		if (!ipmr_pimsm_enabled())
 854			return -EINVAL;
 855		/* Special Purpose VIF in PIM
 856		 * All the packets will be sent to the daemon
 857		 */
 858		if (mrt->mroute_reg_vif_num >= 0)
 859			return -EADDRINUSE;
 860		dev = ipmr_reg_vif(net, mrt);
 861		if (!dev)
 862			return -ENOBUFS;
 863		err = dev_set_allmulti(dev, 1);
 864		if (err) {
 865			unregister_netdevice(dev);
 866			dev_put(dev);
 867			return err;
 868		}
 869		break;
 870	case VIFF_TUNNEL:
 871		dev = ipmr_new_tunnel(net, vifc);
 872		if (IS_ERR(dev))
 873			return PTR_ERR(dev);
 
 
 
 
 
 
 874		break;
 875	case VIFF_USE_IFINDEX:
 876	case 0:
 877		if (vifc->vifc_flags == VIFF_USE_IFINDEX) {
 878			dev = dev_get_by_index(net, vifc->vifc_lcl_ifindex);
 879			if (dev && !__in_dev_get_rtnl(dev)) {
 880				dev_put(dev);
 881				return -EADDRNOTAVAIL;
 882			}
 883		} else {
 884			dev = ip_dev_find(net, vifc->vifc_lcl_addr.s_addr);
 885		}
 886		if (!dev)
 887			return -EADDRNOTAVAIL;
 888		err = dev_set_allmulti(dev, 1);
 889		if (err) {
 890			dev_put(dev);
 891			return err;
 892		}
 893		break;
 894	default:
 895		return -EINVAL;
 896	}
 897
 898	in_dev = __in_dev_get_rtnl(dev);
 899	if (!in_dev) {
 900		dev_put(dev);
 901		return -EADDRNOTAVAIL;
 902	}
 903	IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)++;
 904	inet_netconf_notify_devconf(net, RTM_NEWNETCONF, NETCONFA_MC_FORWARDING,
 905				    dev->ifindex, &in_dev->cnf);
 906	ip_rt_multicast_event(in_dev);
 907
 908	/* Fill in the VIF structures */
 909	vif_device_init(v, dev, vifc->vifc_rate_limit,
 910			vifc->vifc_threshold,
 911			vifc->vifc_flags | (!mrtsock ? VIFF_STATIC : 0),
 912			(VIFF_TUNNEL | VIFF_REGISTER));
 913
 914	err = dev_get_port_parent_id(dev, &ppid, true);
 915	if (err == 0) {
 916		memcpy(v->dev_parent_id.id, ppid.id, ppid.id_len);
 917		v->dev_parent_id.id_len = ppid.id_len;
 918	} else {
 919		v->dev_parent_id.id_len = 0;
 920	}
 921
 922	v->local = vifc->vifc_lcl_addr.s_addr;
 923	v->remote = vifc->vifc_rmt_addr.s_addr;
 924
 925	/* And finish update writing critical data */
 926	spin_lock(&mrt_lock);
 927	rcu_assign_pointer(v->dev, dev);
 928	netdev_tracker_alloc(dev, &v->dev_tracker, GFP_ATOMIC);
 929	if (v->flags & VIFF_REGISTER) {
 930		/* Pairs with READ_ONCE() in ipmr_cache_report() and reg_vif_xmit() */
 931		WRITE_ONCE(mrt->mroute_reg_vif_num, vifi);
 932	}
 933	if (vifi+1 > mrt->maxvif)
 934		WRITE_ONCE(mrt->maxvif, vifi + 1);
 935	spin_unlock(&mrt_lock);
 936	call_ipmr_vif_entry_notifiers(net, FIB_EVENT_VIF_ADD, v, dev,
 937				      vifi, mrt->id);
 938	return 0;
 939}
 940
 941/* called with rcu_read_lock() */
 942static struct mfc_cache *ipmr_cache_find(struct mr_table *mrt,
 943					 __be32 origin,
 944					 __be32 mcastgrp)
 945{
 946	struct mfc_cache_cmp_arg arg = {
 947			.mfc_mcastgrp = mcastgrp,
 948			.mfc_origin = origin
 949	};
 950
 951	return mr_mfc_find(mrt, &arg);
 952}
 953
 954/* Look for a (*,G) entry */
 955static struct mfc_cache *ipmr_cache_find_any(struct mr_table *mrt,
 956					     __be32 mcastgrp, int vifi)
 957{
 958	struct mfc_cache_cmp_arg arg = {
 959			.mfc_mcastgrp = mcastgrp,
 960			.mfc_origin = htonl(INADDR_ANY)
 961	};
 962
 963	if (mcastgrp == htonl(INADDR_ANY))
 964		return mr_mfc_find_any_parent(mrt, vifi);
 965	return mr_mfc_find_any(mrt, vifi, &arg);
 966}
 967
 968/* Look for a (S,G,iif) entry if parent != -1 */
 969static struct mfc_cache *ipmr_cache_find_parent(struct mr_table *mrt,
 970						__be32 origin, __be32 mcastgrp,
 971						int parent)
 972{
 973	struct mfc_cache_cmp_arg arg = {
 974			.mfc_mcastgrp = mcastgrp,
 975			.mfc_origin = origin,
 976	};
 977
 978	return mr_mfc_find_parent(mrt, &arg, parent);
 979}
 980
 981/* Allocate a multicast cache entry */
 982static struct mfc_cache *ipmr_cache_alloc(void)
 983{
 984	struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_KERNEL);
 985
 986	if (c) {
 987		c->_c.mfc_un.res.last_assert = jiffies - MFC_ASSERT_THRESH - 1;
 988		c->_c.mfc_un.res.minvif = MAXVIFS;
 989		c->_c.free = ipmr_cache_free_rcu;
 990		refcount_set(&c->_c.mfc_un.res.refcount, 1);
 991	}
 992	return c;
 993}
 994
 995static struct mfc_cache *ipmr_cache_alloc_unres(void)
 996{
 997	struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_ATOMIC);
 998
 999	if (c) {
1000		skb_queue_head_init(&c->_c.mfc_un.unres.unresolved);
1001		c->_c.mfc_un.unres.expires = jiffies + 10 * HZ;
1002	}
1003	return c;
1004}
1005
1006/* A cache entry has gone into a resolved state from queued */
1007static void ipmr_cache_resolve(struct net *net, struct mr_table *mrt,
1008			       struct mfc_cache *uc, struct mfc_cache *c)
1009{
1010	struct sk_buff *skb;
1011	struct nlmsgerr *e;
1012
1013	/* Play the pending entries through our router */
1014	while ((skb = __skb_dequeue(&uc->_c.mfc_un.unres.unresolved))) {
1015		if (ip_hdr(skb)->version == 0) {
1016			struct nlmsghdr *nlh = skb_pull(skb,
1017							sizeof(struct iphdr));
1018
1019			if (mr_fill_mroute(mrt, skb, &c->_c,
1020					   nlmsg_data(nlh)) > 0) {
1021				nlh->nlmsg_len = skb_tail_pointer(skb) -
1022						 (u8 *)nlh;
1023			} else {
1024				nlh->nlmsg_type = NLMSG_ERROR;
1025				nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr));
1026				skb_trim(skb, nlh->nlmsg_len);
1027				e = nlmsg_data(nlh);
1028				e->error = -EMSGSIZE;
1029				memset(&e->msg, 0, sizeof(e->msg));
1030			}
1031
1032			rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
1033		} else {
1034			rcu_read_lock();
1035			ip_mr_forward(net, mrt, skb->dev, skb, c, 0);
1036			rcu_read_unlock();
1037		}
1038	}
1039}
1040
1041/* Bounce a cache query up to mrouted and netlink.
1042 *
1043 * Called under rcu_read_lock().
1044 */
1045static int ipmr_cache_report(const struct mr_table *mrt,
1046			     struct sk_buff *pkt, vifi_t vifi, int assert)
1047{
1048	const int ihl = ip_hdrlen(pkt);
1049	struct sock *mroute_sk;
1050	struct igmphdr *igmp;
1051	struct igmpmsg *msg;
1052	struct sk_buff *skb;
1053	int ret;
1054
1055	mroute_sk = rcu_dereference(mrt->mroute_sk);
1056	if (!mroute_sk)
1057		return -EINVAL;
1058
1059	if (assert == IGMPMSG_WHOLEPKT || assert == IGMPMSG_WRVIFWHOLE)
1060		skb = skb_realloc_headroom(pkt, sizeof(struct iphdr));
1061	else
1062		skb = alloc_skb(128, GFP_ATOMIC);
1063
1064	if (!skb)
1065		return -ENOBUFS;
1066
1067	if (assert == IGMPMSG_WHOLEPKT || assert == IGMPMSG_WRVIFWHOLE) {
1068		/* Ugly, but we have no choice with this interface.
1069		 * Duplicate old header, fix ihl, length etc.
1070		 * And all this only to mangle msg->im_msgtype and
1071		 * to set msg->im_mbz to "mbz" :-)
1072		 */
1073		skb_push(skb, sizeof(struct iphdr));
1074		skb_reset_network_header(skb);
1075		skb_reset_transport_header(skb);
1076		msg = (struct igmpmsg *)skb_network_header(skb);
1077		memcpy(msg, skb_network_header(pkt), sizeof(struct iphdr));
1078		msg->im_msgtype = assert;
1079		msg->im_mbz = 0;
1080		if (assert == IGMPMSG_WRVIFWHOLE) {
1081			msg->im_vif = vifi;
1082			msg->im_vif_hi = vifi >> 8;
1083		} else {
1084			/* Pairs with WRITE_ONCE() in vif_add() and vif_delete() */
1085			int vif_num = READ_ONCE(mrt->mroute_reg_vif_num);
1086
1087			msg->im_vif = vif_num;
1088			msg->im_vif_hi = vif_num >> 8;
1089		}
1090		ip_hdr(skb)->ihl = sizeof(struct iphdr) >> 2;
1091		ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(pkt)->tot_len) +
1092					     sizeof(struct iphdr));
1093	} else {
1094		/* Copy the IP header */
1095		skb_set_network_header(skb, skb->len);
1096		skb_put(skb, ihl);
1097		skb_copy_to_linear_data(skb, pkt->data, ihl);
1098		/* Flag to the kernel this is a route add */
1099		ip_hdr(skb)->protocol = 0;
1100		msg = (struct igmpmsg *)skb_network_header(skb);
1101		msg->im_vif = vifi;
1102		msg->im_vif_hi = vifi >> 8;
1103		ipv4_pktinfo_prepare(mroute_sk, pkt, false);
1104		memcpy(skb->cb, pkt->cb, sizeof(skb->cb));
1105		/* Add our header */
1106		igmp = skb_put(skb, sizeof(struct igmphdr));
1107		igmp->type = assert;
1108		msg->im_msgtype = assert;
1109		igmp->code = 0;
1110		ip_hdr(skb)->tot_len = htons(skb->len);	/* Fix the length */
1111		skb->transport_header = skb->network_header;
1112	}
1113
 
 
 
 
 
 
 
 
1114	igmpmsg_netlink_event(mrt, skb);
1115
1116	/* Deliver to mrouted */
1117	ret = sock_queue_rcv_skb(mroute_sk, skb);
1118
1119	if (ret < 0) {
1120		net_warn_ratelimited("mroute: pending queue full, dropping entries\n");
1121		kfree_skb(skb);
1122	}
1123
1124	return ret;
1125}
1126
1127/* Queue a packet for resolution. It gets locked cache entry! */
1128/* Called under rcu_read_lock() */
1129static int ipmr_cache_unresolved(struct mr_table *mrt, vifi_t vifi,
1130				 struct sk_buff *skb, struct net_device *dev)
1131{
1132	const struct iphdr *iph = ip_hdr(skb);
1133	struct mfc_cache *c;
1134	bool found = false;
1135	int err;
1136
1137	spin_lock_bh(&mfc_unres_lock);
1138	list_for_each_entry(c, &mrt->mfc_unres_queue, _c.list) {
1139		if (c->mfc_mcastgrp == iph->daddr &&
1140		    c->mfc_origin == iph->saddr) {
1141			found = true;
1142			break;
1143		}
1144	}
1145
1146	if (!found) {
1147		/* Create a new entry if allowable */
1148		c = ipmr_cache_alloc_unres();
1149		if (!c) {
1150			spin_unlock_bh(&mfc_unres_lock);
1151
1152			kfree_skb(skb);
1153			return -ENOBUFS;
1154		}
1155
1156		/* Fill in the new cache entry */
1157		c->_c.mfc_parent = -1;
1158		c->mfc_origin	= iph->saddr;
1159		c->mfc_mcastgrp	= iph->daddr;
1160
1161		/* Reflect first query at mrouted. */
1162		err = ipmr_cache_report(mrt, skb, vifi, IGMPMSG_NOCACHE);
1163
1164		if (err < 0) {
1165			/* If the report failed throw the cache entry
1166			   out - Brad Parker
1167			 */
1168			spin_unlock_bh(&mfc_unres_lock);
1169
1170			ipmr_cache_free(c);
1171			kfree_skb(skb);
1172			return err;
1173		}
1174
1175		atomic_inc(&mrt->cache_resolve_queue_len);
1176		list_add(&c->_c.list, &mrt->mfc_unres_queue);
1177		mroute_netlink_event(mrt, c, RTM_NEWROUTE);
1178
1179		if (atomic_read(&mrt->cache_resolve_queue_len) == 1)
1180			mod_timer(&mrt->ipmr_expire_timer,
1181				  c->_c.mfc_un.unres.expires);
1182	}
1183
1184	/* See if we can append the packet */
1185	if (c->_c.mfc_un.unres.unresolved.qlen > 3) {
1186		kfree_skb(skb);
1187		err = -ENOBUFS;
1188	} else {
1189		if (dev) {
1190			skb->dev = dev;
1191			skb->skb_iif = dev->ifindex;
1192		}
1193		skb_queue_tail(&c->_c.mfc_un.unres.unresolved, skb);
1194		err = 0;
1195	}
1196
1197	spin_unlock_bh(&mfc_unres_lock);
1198	return err;
1199}
1200
1201/* MFC cache manipulation by user space mroute daemon */
1202
1203static int ipmr_mfc_delete(struct mr_table *mrt, struct mfcctl *mfc, int parent)
1204{
1205	struct net *net = read_pnet(&mrt->net);
1206	struct mfc_cache *c;
1207
1208	/* The entries are added/deleted only under RTNL */
1209	rcu_read_lock();
1210	c = ipmr_cache_find_parent(mrt, mfc->mfcc_origin.s_addr,
1211				   mfc->mfcc_mcastgrp.s_addr, parent);
1212	rcu_read_unlock();
1213	if (!c)
1214		return -ENOENT;
1215	rhltable_remove(&mrt->mfc_hash, &c->_c.mnode, ipmr_rht_params);
1216	list_del_rcu(&c->_c.list);
1217	call_ipmr_mfc_entry_notifiers(net, FIB_EVENT_ENTRY_DEL, c, mrt->id);
1218	mroute_netlink_event(mrt, c, RTM_DELROUTE);
1219	mr_cache_put(&c->_c);
1220
1221	return 0;
1222}
1223
1224static int ipmr_mfc_add(struct net *net, struct mr_table *mrt,
1225			struct mfcctl *mfc, int mrtsock, int parent)
1226{
1227	struct mfc_cache *uc, *c;
1228	struct mr_mfc *_uc;
1229	bool found;
1230	int ret;
1231
1232	if (mfc->mfcc_parent >= MAXVIFS)
1233		return -ENFILE;
1234
1235	/* The entries are added/deleted only under RTNL */
1236	rcu_read_lock();
1237	c = ipmr_cache_find_parent(mrt, mfc->mfcc_origin.s_addr,
1238				   mfc->mfcc_mcastgrp.s_addr, parent);
1239	rcu_read_unlock();
1240	if (c) {
1241		spin_lock(&mrt_lock);
1242		c->_c.mfc_parent = mfc->mfcc_parent;
1243		ipmr_update_thresholds(mrt, &c->_c, mfc->mfcc_ttls);
1244		if (!mrtsock)
1245			c->_c.mfc_flags |= MFC_STATIC;
1246		spin_unlock(&mrt_lock);
1247		call_ipmr_mfc_entry_notifiers(net, FIB_EVENT_ENTRY_REPLACE, c,
1248					      mrt->id);
1249		mroute_netlink_event(mrt, c, RTM_NEWROUTE);
1250		return 0;
1251	}
1252
1253	if (mfc->mfcc_mcastgrp.s_addr != htonl(INADDR_ANY) &&
1254	    !ipv4_is_multicast(mfc->mfcc_mcastgrp.s_addr))
1255		return -EINVAL;
1256
1257	c = ipmr_cache_alloc();
1258	if (!c)
1259		return -ENOMEM;
1260
1261	c->mfc_origin = mfc->mfcc_origin.s_addr;
1262	c->mfc_mcastgrp = mfc->mfcc_mcastgrp.s_addr;
1263	c->_c.mfc_parent = mfc->mfcc_parent;
1264	ipmr_update_thresholds(mrt, &c->_c, mfc->mfcc_ttls);
1265	if (!mrtsock)
1266		c->_c.mfc_flags |= MFC_STATIC;
1267
1268	ret = rhltable_insert_key(&mrt->mfc_hash, &c->cmparg, &c->_c.mnode,
1269				  ipmr_rht_params);
1270	if (ret) {
1271		pr_err("ipmr: rhtable insert error %d\n", ret);
1272		ipmr_cache_free(c);
1273		return ret;
1274	}
1275	list_add_tail_rcu(&c->_c.list, &mrt->mfc_cache_list);
1276	/* Check to see if we resolved a queued list. If so we
1277	 * need to send on the frames and tidy up.
1278	 */
1279	found = false;
1280	spin_lock_bh(&mfc_unres_lock);
1281	list_for_each_entry(_uc, &mrt->mfc_unres_queue, list) {
1282		uc = (struct mfc_cache *)_uc;
1283		if (uc->mfc_origin == c->mfc_origin &&
1284		    uc->mfc_mcastgrp == c->mfc_mcastgrp) {
1285			list_del(&_uc->list);
1286			atomic_dec(&mrt->cache_resolve_queue_len);
1287			found = true;
1288			break;
1289		}
1290	}
1291	if (list_empty(&mrt->mfc_unres_queue))
1292		del_timer(&mrt->ipmr_expire_timer);
1293	spin_unlock_bh(&mfc_unres_lock);
1294
1295	if (found) {
1296		ipmr_cache_resolve(net, mrt, uc, c);
1297		ipmr_cache_free(uc);
1298	}
1299	call_ipmr_mfc_entry_notifiers(net, FIB_EVENT_ENTRY_ADD, c, mrt->id);
1300	mroute_netlink_event(mrt, c, RTM_NEWROUTE);
1301	return 0;
1302}
1303
1304/* Close the multicast socket, and clear the vif tables etc */
1305static void mroute_clean_tables(struct mr_table *mrt, int flags)
1306{
1307	struct net *net = read_pnet(&mrt->net);
1308	struct mr_mfc *c, *tmp;
1309	struct mfc_cache *cache;
1310	LIST_HEAD(list);
1311	int i;
1312
1313	/* Shut down all active vif entries */
1314	if (flags & (MRT_FLUSH_VIFS | MRT_FLUSH_VIFS_STATIC)) {
1315		for (i = 0; i < mrt->maxvif; i++) {
1316			if (((mrt->vif_table[i].flags & VIFF_STATIC) &&
1317			     !(flags & MRT_FLUSH_VIFS_STATIC)) ||
1318			    (!(mrt->vif_table[i].flags & VIFF_STATIC) && !(flags & MRT_FLUSH_VIFS)))
1319				continue;
1320			vif_delete(mrt, i, 0, &list);
1321		}
1322		unregister_netdevice_many(&list);
1323	}
1324
1325	/* Wipe the cache */
1326	if (flags & (MRT_FLUSH_MFC | MRT_FLUSH_MFC_STATIC)) {
1327		list_for_each_entry_safe(c, tmp, &mrt->mfc_cache_list, list) {
1328			if (((c->mfc_flags & MFC_STATIC) && !(flags & MRT_FLUSH_MFC_STATIC)) ||
1329			    (!(c->mfc_flags & MFC_STATIC) && !(flags & MRT_FLUSH_MFC)))
1330				continue;
1331			rhltable_remove(&mrt->mfc_hash, &c->mnode, ipmr_rht_params);
1332			list_del_rcu(&c->list);
1333			cache = (struct mfc_cache *)c;
1334			call_ipmr_mfc_entry_notifiers(net, FIB_EVENT_ENTRY_DEL, cache,
1335						      mrt->id);
1336			mroute_netlink_event(mrt, cache, RTM_DELROUTE);
1337			mr_cache_put(c);
1338		}
1339	}
1340
1341	if (flags & MRT_FLUSH_MFC) {
1342		if (atomic_read(&mrt->cache_resolve_queue_len) != 0) {
1343			spin_lock_bh(&mfc_unres_lock);
1344			list_for_each_entry_safe(c, tmp, &mrt->mfc_unres_queue, list) {
1345				list_del(&c->list);
1346				cache = (struct mfc_cache *)c;
1347				mroute_netlink_event(mrt, cache, RTM_DELROUTE);
1348				ipmr_destroy_unres(mrt, cache);
1349			}
1350			spin_unlock_bh(&mfc_unres_lock);
1351		}
1352	}
1353}
1354
1355/* called from ip_ra_control(), before an RCU grace period,
1356 * we don't need to call synchronize_rcu() here
1357 */
1358static void mrtsock_destruct(struct sock *sk)
1359{
1360	struct net *net = sock_net(sk);
1361	struct mr_table *mrt;
1362
1363	rtnl_lock();
1364	ipmr_for_each_table(mrt, net) {
1365		if (sk == rtnl_dereference(mrt->mroute_sk)) {
1366			IPV4_DEVCONF_ALL(net, MC_FORWARDING)--;
1367			inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
1368						    NETCONFA_MC_FORWARDING,
1369						    NETCONFA_IFINDEX_ALL,
1370						    net->ipv4.devconf_all);
1371			RCU_INIT_POINTER(mrt->mroute_sk, NULL);
1372			mroute_clean_tables(mrt, MRT_FLUSH_VIFS | MRT_FLUSH_MFC);
1373		}
1374	}
1375	rtnl_unlock();
1376}
1377
1378/* Socket options and virtual interface manipulation. The whole
1379 * virtual interface system is a complete heap, but unfortunately
1380 * that's how BSD mrouted happens to think. Maybe one day with a proper
1381 * MOSPF/PIM router set up we can clean this up.
1382 */
1383
1384int ip_mroute_setsockopt(struct sock *sk, int optname, sockptr_t optval,
1385			 unsigned int optlen)
1386{
1387	struct net *net = sock_net(sk);
1388	int val, ret = 0, parent = 0;
1389	struct mr_table *mrt;
1390	struct vifctl vif;
1391	struct mfcctl mfc;
1392	bool do_wrvifwhole;
1393	u32 uval;
1394
1395	/* There's one exception to the lock - MRT_DONE which needs to unlock */
1396	rtnl_lock();
1397	if (sk->sk_type != SOCK_RAW ||
1398	    inet_sk(sk)->inet_num != IPPROTO_IGMP) {
1399		ret = -EOPNOTSUPP;
1400		goto out_unlock;
1401	}
1402
1403	mrt = __ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1404	if (!mrt) {
1405		ret = -ENOENT;
1406		goto out_unlock;
1407	}
1408	if (optname != MRT_INIT) {
1409		if (sk != rcu_access_pointer(mrt->mroute_sk) &&
1410		    !ns_capable(net->user_ns, CAP_NET_ADMIN)) {
1411			ret = -EACCES;
1412			goto out_unlock;
1413		}
1414	}
1415
1416	switch (optname) {
1417	case MRT_INIT:
1418		if (optlen != sizeof(int)) {
1419			ret = -EINVAL;
1420			break;
1421		}
1422		if (rtnl_dereference(mrt->mroute_sk)) {
1423			ret = -EADDRINUSE;
1424			break;
1425		}
1426
1427		ret = ip_ra_control(sk, 1, mrtsock_destruct);
1428		if (ret == 0) {
1429			rcu_assign_pointer(mrt->mroute_sk, sk);
1430			IPV4_DEVCONF_ALL(net, MC_FORWARDING)++;
1431			inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
1432						    NETCONFA_MC_FORWARDING,
1433						    NETCONFA_IFINDEX_ALL,
1434						    net->ipv4.devconf_all);
1435		}
1436		break;
1437	case MRT_DONE:
1438		if (sk != rcu_access_pointer(mrt->mroute_sk)) {
1439			ret = -EACCES;
1440		} else {
1441			/* We need to unlock here because mrtsock_destruct takes
1442			 * care of rtnl itself and we can't change that due to
1443			 * the IP_ROUTER_ALERT setsockopt which runs without it.
1444			 */
1445			rtnl_unlock();
1446			ret = ip_ra_control(sk, 0, NULL);
1447			goto out;
1448		}
1449		break;
1450	case MRT_ADD_VIF:
1451	case MRT_DEL_VIF:
1452		if (optlen != sizeof(vif)) {
1453			ret = -EINVAL;
1454			break;
1455		}
1456		if (copy_from_sockptr(&vif, optval, sizeof(vif))) {
1457			ret = -EFAULT;
1458			break;
1459		}
1460		if (vif.vifc_vifi >= MAXVIFS) {
1461			ret = -ENFILE;
1462			break;
1463		}
1464		if (optname == MRT_ADD_VIF) {
1465			ret = vif_add(net, mrt, &vif,
1466				      sk == rtnl_dereference(mrt->mroute_sk));
1467		} else {
1468			ret = vif_delete(mrt, vif.vifc_vifi, 0, NULL);
1469		}
1470		break;
1471	/* Manipulate the forwarding caches. These live
1472	 * in a sort of kernel/user symbiosis.
1473	 */
1474	case MRT_ADD_MFC:
1475	case MRT_DEL_MFC:
1476		parent = -1;
1477		fallthrough;
1478	case MRT_ADD_MFC_PROXY:
1479	case MRT_DEL_MFC_PROXY:
1480		if (optlen != sizeof(mfc)) {
1481			ret = -EINVAL;
1482			break;
1483		}
1484		if (copy_from_sockptr(&mfc, optval, sizeof(mfc))) {
1485			ret = -EFAULT;
1486			break;
1487		}
1488		if (parent == 0)
1489			parent = mfc.mfcc_parent;
1490		if (optname == MRT_DEL_MFC || optname == MRT_DEL_MFC_PROXY)
1491			ret = ipmr_mfc_delete(mrt, &mfc, parent);
1492		else
1493			ret = ipmr_mfc_add(net, mrt, &mfc,
1494					   sk == rtnl_dereference(mrt->mroute_sk),
1495					   parent);
1496		break;
1497	case MRT_FLUSH:
1498		if (optlen != sizeof(val)) {
1499			ret = -EINVAL;
1500			break;
1501		}
1502		if (copy_from_sockptr(&val, optval, sizeof(val))) {
1503			ret = -EFAULT;
1504			break;
1505		}
1506		mroute_clean_tables(mrt, val);
1507		break;
1508	/* Control PIM assert. */
1509	case MRT_ASSERT:
1510		if (optlen != sizeof(val)) {
1511			ret = -EINVAL;
1512			break;
1513		}
1514		if (copy_from_sockptr(&val, optval, sizeof(val))) {
1515			ret = -EFAULT;
1516			break;
1517		}
1518		mrt->mroute_do_assert = val;
1519		break;
1520	case MRT_PIM:
1521		if (!ipmr_pimsm_enabled()) {
1522			ret = -ENOPROTOOPT;
1523			break;
1524		}
1525		if (optlen != sizeof(val)) {
1526			ret = -EINVAL;
1527			break;
1528		}
1529		if (copy_from_sockptr(&val, optval, sizeof(val))) {
1530			ret = -EFAULT;
1531			break;
1532		}
1533
1534		do_wrvifwhole = (val == IGMPMSG_WRVIFWHOLE);
1535		val = !!val;
1536		if (val != mrt->mroute_do_pim) {
1537			mrt->mroute_do_pim = val;
1538			mrt->mroute_do_assert = val;
1539			mrt->mroute_do_wrvifwhole = do_wrvifwhole;
1540		}
1541		break;
1542	case MRT_TABLE:
1543		if (!IS_BUILTIN(CONFIG_IP_MROUTE_MULTIPLE_TABLES)) {
1544			ret = -ENOPROTOOPT;
1545			break;
1546		}
1547		if (optlen != sizeof(uval)) {
1548			ret = -EINVAL;
1549			break;
1550		}
1551		if (copy_from_sockptr(&uval, optval, sizeof(uval))) {
1552			ret = -EFAULT;
1553			break;
1554		}
1555
1556		if (sk == rtnl_dereference(mrt->mroute_sk)) {
1557			ret = -EBUSY;
1558		} else {
1559			mrt = ipmr_new_table(net, uval);
1560			if (IS_ERR(mrt))
1561				ret = PTR_ERR(mrt);
1562			else
1563				raw_sk(sk)->ipmr_table = uval;
1564		}
1565		break;
1566	/* Spurious command, or MRT_VERSION which you cannot set. */
1567	default:
1568		ret = -ENOPROTOOPT;
1569	}
1570out_unlock:
1571	rtnl_unlock();
1572out:
1573	return ret;
1574}
1575
1576/* Execute if this ioctl is a special mroute ioctl */
1577int ipmr_sk_ioctl(struct sock *sk, unsigned int cmd, void __user *arg)
1578{
1579	switch (cmd) {
1580	/* These userspace buffers will be consumed by ipmr_ioctl() */
1581	case SIOCGETVIFCNT: {
1582		struct sioc_vif_req buffer;
1583
1584		return sock_ioctl_inout(sk, cmd, arg, &buffer,
1585				      sizeof(buffer));
1586		}
1587	case SIOCGETSGCNT: {
1588		struct sioc_sg_req buffer;
1589
1590		return sock_ioctl_inout(sk, cmd, arg, &buffer,
1591				      sizeof(buffer));
1592		}
1593	}
1594	/* return code > 0 means that the ioctl was not executed */
1595	return 1;
1596}
1597
1598/* Getsock opt support for the multicast routing system. */
1599int ip_mroute_getsockopt(struct sock *sk, int optname, sockptr_t optval,
1600			 sockptr_t optlen)
1601{
1602	int olr;
1603	int val;
1604	struct net *net = sock_net(sk);
1605	struct mr_table *mrt;
1606
1607	if (sk->sk_type != SOCK_RAW ||
1608	    inet_sk(sk)->inet_num != IPPROTO_IGMP)
1609		return -EOPNOTSUPP;
1610
1611	mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1612	if (!mrt)
1613		return -ENOENT;
1614
1615	switch (optname) {
1616	case MRT_VERSION:
1617		val = 0x0305;
1618		break;
1619	case MRT_PIM:
1620		if (!ipmr_pimsm_enabled())
1621			return -ENOPROTOOPT;
1622		val = mrt->mroute_do_pim;
1623		break;
1624	case MRT_ASSERT:
1625		val = mrt->mroute_do_assert;
1626		break;
1627	default:
1628		return -ENOPROTOOPT;
1629	}
1630
1631	if (copy_from_sockptr(&olr, optlen, sizeof(int)))
1632		return -EFAULT;
 
1633	if (olr < 0)
1634		return -EINVAL;
1635
1636	olr = min_t(unsigned int, olr, sizeof(int));
1637
1638	if (copy_to_sockptr(optlen, &olr, sizeof(int)))
1639		return -EFAULT;
1640	if (copy_to_sockptr(optval, &val, olr))
1641		return -EFAULT;
1642	return 0;
1643}
1644
1645/* The IP multicast ioctl support routines. */
1646int ipmr_ioctl(struct sock *sk, int cmd, void *arg)
1647{
 
 
1648	struct vif_device *vif;
1649	struct mfc_cache *c;
1650	struct net *net = sock_net(sk);
1651	struct sioc_vif_req *vr;
1652	struct sioc_sg_req *sr;
1653	struct mr_table *mrt;
1654
1655	mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1656	if (!mrt)
1657		return -ENOENT;
1658
1659	switch (cmd) {
1660	case SIOCGETVIFCNT:
1661		vr = (struct sioc_vif_req *)arg;
1662		if (vr->vifi >= mrt->maxvif)
 
1663			return -EINVAL;
1664		vr->vifi = array_index_nospec(vr->vifi, mrt->maxvif);
1665		rcu_read_lock();
1666		vif = &mrt->vif_table[vr->vifi];
1667		if (VIF_EXISTS(mrt, vr->vifi)) {
1668			vr->icount = READ_ONCE(vif->pkt_in);
1669			vr->ocount = READ_ONCE(vif->pkt_out);
1670			vr->ibytes = READ_ONCE(vif->bytes_in);
1671			vr->obytes = READ_ONCE(vif->bytes_out);
1672			rcu_read_unlock();
1673
 
 
1674			return 0;
1675		}
1676		rcu_read_unlock();
1677		return -EADDRNOTAVAIL;
1678	case SIOCGETSGCNT:
1679		sr = (struct sioc_sg_req *)arg;
 
1680
1681		rcu_read_lock();
1682		c = ipmr_cache_find(mrt, sr->src.s_addr, sr->grp.s_addr);
1683		if (c) {
1684			sr->pktcnt = atomic_long_read(&c->_c.mfc_un.res.pkt);
1685			sr->bytecnt = atomic_long_read(&c->_c.mfc_un.res.bytes);
1686			sr->wrong_if = atomic_long_read(&c->_c.mfc_un.res.wrong_if);
1687			rcu_read_unlock();
 
 
 
1688			return 0;
1689		}
1690		rcu_read_unlock();
1691		return -EADDRNOTAVAIL;
1692	default:
1693		return -ENOIOCTLCMD;
1694	}
1695}
1696
1697#ifdef CONFIG_COMPAT
1698struct compat_sioc_sg_req {
1699	struct in_addr src;
1700	struct in_addr grp;
1701	compat_ulong_t pktcnt;
1702	compat_ulong_t bytecnt;
1703	compat_ulong_t wrong_if;
1704};
1705
1706struct compat_sioc_vif_req {
1707	vifi_t	vifi;		/* Which iface */
1708	compat_ulong_t icount;
1709	compat_ulong_t ocount;
1710	compat_ulong_t ibytes;
1711	compat_ulong_t obytes;
1712};
1713
1714int ipmr_compat_ioctl(struct sock *sk, unsigned int cmd, void __user *arg)
1715{
1716	struct compat_sioc_sg_req sr;
1717	struct compat_sioc_vif_req vr;
1718	struct vif_device *vif;
1719	struct mfc_cache *c;
1720	struct net *net = sock_net(sk);
1721	struct mr_table *mrt;
1722
1723	mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1724	if (!mrt)
1725		return -ENOENT;
1726
1727	switch (cmd) {
1728	case SIOCGETVIFCNT:
1729		if (copy_from_user(&vr, arg, sizeof(vr)))
1730			return -EFAULT;
1731		if (vr.vifi >= mrt->maxvif)
1732			return -EINVAL;
1733		vr.vifi = array_index_nospec(vr.vifi, mrt->maxvif);
1734		rcu_read_lock();
1735		vif = &mrt->vif_table[vr.vifi];
1736		if (VIF_EXISTS(mrt, vr.vifi)) {
1737			vr.icount = READ_ONCE(vif->pkt_in);
1738			vr.ocount = READ_ONCE(vif->pkt_out);
1739			vr.ibytes = READ_ONCE(vif->bytes_in);
1740			vr.obytes = READ_ONCE(vif->bytes_out);
1741			rcu_read_unlock();
1742
1743			if (copy_to_user(arg, &vr, sizeof(vr)))
1744				return -EFAULT;
1745			return 0;
1746		}
1747		rcu_read_unlock();
1748		return -EADDRNOTAVAIL;
1749	case SIOCGETSGCNT:
1750		if (copy_from_user(&sr, arg, sizeof(sr)))
1751			return -EFAULT;
1752
1753		rcu_read_lock();
1754		c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
1755		if (c) {
1756			sr.pktcnt = atomic_long_read(&c->_c.mfc_un.res.pkt);
1757			sr.bytecnt = atomic_long_read(&c->_c.mfc_un.res.bytes);
1758			sr.wrong_if = atomic_long_read(&c->_c.mfc_un.res.wrong_if);
1759			rcu_read_unlock();
1760
1761			if (copy_to_user(arg, &sr, sizeof(sr)))
1762				return -EFAULT;
1763			return 0;
1764		}
1765		rcu_read_unlock();
1766		return -EADDRNOTAVAIL;
1767	default:
1768		return -ENOIOCTLCMD;
1769	}
1770}
1771#endif
1772
1773static int ipmr_device_event(struct notifier_block *this, unsigned long event, void *ptr)
1774{
1775	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1776	struct net *net = dev_net(dev);
1777	struct mr_table *mrt;
1778	struct vif_device *v;
1779	int ct;
1780
1781	if (event != NETDEV_UNREGISTER)
1782		return NOTIFY_DONE;
1783
1784	ipmr_for_each_table(mrt, net) {
1785		v = &mrt->vif_table[0];
1786		for (ct = 0; ct < mrt->maxvif; ct++, v++) {
1787			if (rcu_access_pointer(v->dev) == dev)
1788				vif_delete(mrt, ct, 1, NULL);
1789		}
1790	}
1791	return NOTIFY_DONE;
1792}
1793
1794static struct notifier_block ip_mr_notifier = {
1795	.notifier_call = ipmr_device_event,
1796};
1797
1798/* Encapsulate a packet by attaching a valid IPIP header to it.
1799 * This avoids tunnel drivers and other mess and gives us the speed so
1800 * important for multicast video.
1801 */
1802static void ip_encap(struct net *net, struct sk_buff *skb,
1803		     __be32 saddr, __be32 daddr)
1804{
1805	struct iphdr *iph;
1806	const struct iphdr *old_iph = ip_hdr(skb);
1807
1808	skb_push(skb, sizeof(struct iphdr));
1809	skb->transport_header = skb->network_header;
1810	skb_reset_network_header(skb);
1811	iph = ip_hdr(skb);
1812
1813	iph->version	=	4;
1814	iph->tos	=	old_iph->tos;
1815	iph->ttl	=	old_iph->ttl;
1816	iph->frag_off	=	0;
1817	iph->daddr	=	daddr;
1818	iph->saddr	=	saddr;
1819	iph->protocol	=	IPPROTO_IPIP;
1820	iph->ihl	=	5;
1821	iph->tot_len	=	htons(skb->len);
1822	ip_select_ident(net, skb, NULL);
1823	ip_send_check(iph);
1824
1825	memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1826	nf_reset_ct(skb);
1827}
1828
1829static inline int ipmr_forward_finish(struct net *net, struct sock *sk,
1830				      struct sk_buff *skb)
1831{
1832	struct ip_options *opt = &(IPCB(skb)->opt);
1833
1834	IP_INC_STATS(net, IPSTATS_MIB_OUTFORWDATAGRAMS);
 
1835
1836	if (unlikely(opt->optlen))
1837		ip_forward_options(skb);
1838
1839	return dst_output(net, sk, skb);
1840}
1841
1842#ifdef CONFIG_NET_SWITCHDEV
1843static bool ipmr_forward_offloaded(struct sk_buff *skb, struct mr_table *mrt,
1844				   int in_vifi, int out_vifi)
1845{
1846	struct vif_device *out_vif = &mrt->vif_table[out_vifi];
1847	struct vif_device *in_vif = &mrt->vif_table[in_vifi];
1848
1849	if (!skb->offload_l3_fwd_mark)
1850		return false;
1851	if (!out_vif->dev_parent_id.id_len || !in_vif->dev_parent_id.id_len)
1852		return false;
1853	return netdev_phys_item_id_same(&out_vif->dev_parent_id,
1854					&in_vif->dev_parent_id);
1855}
1856#else
1857static bool ipmr_forward_offloaded(struct sk_buff *skb, struct mr_table *mrt,
1858				   int in_vifi, int out_vifi)
1859{
1860	return false;
1861}
1862#endif
1863
1864/* Processing handlers for ipmr_forward, under rcu_read_lock() */
1865
1866static void ipmr_queue_xmit(struct net *net, struct mr_table *mrt,
1867			    int in_vifi, struct sk_buff *skb, int vifi)
1868{
1869	const struct iphdr *iph = ip_hdr(skb);
1870	struct vif_device *vif = &mrt->vif_table[vifi];
1871	struct net_device *vif_dev;
1872	struct net_device *dev;
1873	struct rtable *rt;
1874	struct flowi4 fl4;
1875	int    encap = 0;
1876
1877	vif_dev = vif_dev_read(vif);
1878	if (!vif_dev)
1879		goto out_free;
1880
1881	if (vif->flags & VIFF_REGISTER) {
1882		WRITE_ONCE(vif->pkt_out, vif->pkt_out + 1);
1883		WRITE_ONCE(vif->bytes_out, vif->bytes_out + skb->len);
1884		DEV_STATS_ADD(vif_dev, tx_bytes, skb->len);
1885		DEV_STATS_INC(vif_dev, tx_packets);
1886		ipmr_cache_report(mrt, skb, vifi, IGMPMSG_WHOLEPKT);
1887		goto out_free;
1888	}
1889
1890	if (ipmr_forward_offloaded(skb, mrt, in_vifi, vifi))
1891		goto out_free;
1892
1893	if (vif->flags & VIFF_TUNNEL) {
1894		rt = ip_route_output_ports(net, &fl4, NULL,
1895					   vif->remote, vif->local,
1896					   0, 0,
1897					   IPPROTO_IPIP,
1898					   iph->tos & INET_DSCP_MASK, vif->link);
1899		if (IS_ERR(rt))
1900			goto out_free;
1901		encap = sizeof(struct iphdr);
1902	} else {
1903		rt = ip_route_output_ports(net, &fl4, NULL, iph->daddr, 0,
1904					   0, 0,
1905					   IPPROTO_IPIP,
1906					   iph->tos & INET_DSCP_MASK, vif->link);
1907		if (IS_ERR(rt))
1908			goto out_free;
1909	}
1910
1911	dev = rt->dst.dev;
1912
1913	if (skb->len+encap > dst_mtu(&rt->dst) && (ntohs(iph->frag_off) & IP_DF)) {
1914		/* Do not fragment multicasts. Alas, IPv4 does not
1915		 * allow to send ICMP, so that packets will disappear
1916		 * to blackhole.
1917		 */
1918		IP_INC_STATS(net, IPSTATS_MIB_FRAGFAILS);
1919		ip_rt_put(rt);
1920		goto out_free;
1921	}
1922
1923	encap += LL_RESERVED_SPACE(dev) + rt->dst.header_len;
1924
1925	if (skb_cow(skb, encap)) {
1926		ip_rt_put(rt);
1927		goto out_free;
1928	}
1929
1930	WRITE_ONCE(vif->pkt_out, vif->pkt_out + 1);
1931	WRITE_ONCE(vif->bytes_out, vif->bytes_out + skb->len);
1932
1933	skb_dst_drop(skb);
1934	skb_dst_set(skb, &rt->dst);
1935	ip_decrease_ttl(ip_hdr(skb));
1936
1937	/* FIXME: forward and output firewalls used to be called here.
1938	 * What do we do with netfilter? -- RR
1939	 */
1940	if (vif->flags & VIFF_TUNNEL) {
1941		ip_encap(net, skb, vif->local, vif->remote);
1942		/* FIXME: extra output firewall step used to be here. --RR */
1943		DEV_STATS_INC(vif_dev, tx_packets);
1944		DEV_STATS_ADD(vif_dev, tx_bytes, skb->len);
1945	}
1946
1947	IPCB(skb)->flags |= IPSKB_FORWARDED;
1948
1949	/* RFC1584 teaches, that DVMRP/PIM router must deliver packets locally
1950	 * not only before forwarding, but after forwarding on all output
1951	 * interfaces. It is clear, if mrouter runs a multicasting
1952	 * program, it should receive packets not depending to what interface
1953	 * program is joined.
1954	 * If we will not make it, the program will have to join on all
1955	 * interfaces. On the other hand, multihoming host (or router, but
1956	 * not mrouter) cannot join to more than one interface - it will
1957	 * result in receiving multiple packets.
1958	 */
1959	NF_HOOK(NFPROTO_IPV4, NF_INET_FORWARD,
1960		net, NULL, skb, skb->dev, dev,
1961		ipmr_forward_finish);
1962	return;
1963
1964out_free:
1965	kfree_skb(skb);
1966}
1967
1968/* Called with mrt_lock or rcu_read_lock() */
1969static int ipmr_find_vif(const struct mr_table *mrt, struct net_device *dev)
1970{
1971	int ct;
1972	/* Pairs with WRITE_ONCE() in vif_delete()/vif_add() */
1973	for (ct = READ_ONCE(mrt->maxvif) - 1; ct >= 0; ct--) {
1974		if (rcu_access_pointer(mrt->vif_table[ct].dev) == dev)
1975			break;
1976	}
1977	return ct;
1978}
1979
1980/* "local" means that we should preserve one skb (for local delivery) */
1981/* Called uner rcu_read_lock() */
1982static void ip_mr_forward(struct net *net, struct mr_table *mrt,
1983			  struct net_device *dev, struct sk_buff *skb,
1984			  struct mfc_cache *c, int local)
1985{
1986	int true_vifi = ipmr_find_vif(mrt, dev);
1987	int psend = -1;
1988	int vif, ct;
1989
1990	vif = c->_c.mfc_parent;
1991	atomic_long_inc(&c->_c.mfc_un.res.pkt);
1992	atomic_long_add(skb->len, &c->_c.mfc_un.res.bytes);
1993	WRITE_ONCE(c->_c.mfc_un.res.lastuse, jiffies);
1994
1995	if (c->mfc_origin == htonl(INADDR_ANY) && true_vifi >= 0) {
1996		struct mfc_cache *cache_proxy;
1997
1998		/* For an (*,G) entry, we only check that the incoming
1999		 * interface is part of the static tree.
2000		 */
2001		cache_proxy = mr_mfc_find_any_parent(mrt, vif);
2002		if (cache_proxy &&
2003		    cache_proxy->_c.mfc_un.res.ttls[true_vifi] < 255)
2004			goto forward;
2005	}
2006
2007	/* Wrong interface: drop packet and (maybe) send PIM assert. */
2008	if (rcu_access_pointer(mrt->vif_table[vif].dev) != dev) {
2009		if (rt_is_output_route(skb_rtable(skb))) {
2010			/* It is our own packet, looped back.
2011			 * Very complicated situation...
2012			 *
2013			 * The best workaround until routing daemons will be
2014			 * fixed is not to redistribute packet, if it was
2015			 * send through wrong interface. It means, that
2016			 * multicast applications WILL NOT work for
2017			 * (S,G), which have default multicast route pointing
2018			 * to wrong oif. In any case, it is not a good
2019			 * idea to use multicasting applications on router.
2020			 */
2021			goto dont_forward;
2022		}
2023
2024		atomic_long_inc(&c->_c.mfc_un.res.wrong_if);
2025
2026		if (true_vifi >= 0 && mrt->mroute_do_assert &&
2027		    /* pimsm uses asserts, when switching from RPT to SPT,
2028		     * so that we cannot check that packet arrived on an oif.
2029		     * It is bad, but otherwise we would need to move pretty
2030		     * large chunk of pimd to kernel. Ough... --ANK
2031		     */
2032		    (mrt->mroute_do_pim ||
2033		     c->_c.mfc_un.res.ttls[true_vifi] < 255) &&
2034		    time_after(jiffies,
2035			       c->_c.mfc_un.res.last_assert +
2036			       MFC_ASSERT_THRESH)) {
2037			c->_c.mfc_un.res.last_assert = jiffies;
2038			ipmr_cache_report(mrt, skb, true_vifi, IGMPMSG_WRONGVIF);
2039			if (mrt->mroute_do_wrvifwhole)
2040				ipmr_cache_report(mrt, skb, true_vifi,
2041						  IGMPMSG_WRVIFWHOLE);
2042		}
2043		goto dont_forward;
2044	}
2045
2046forward:
2047	WRITE_ONCE(mrt->vif_table[vif].pkt_in,
2048		   mrt->vif_table[vif].pkt_in + 1);
2049	WRITE_ONCE(mrt->vif_table[vif].bytes_in,
2050		   mrt->vif_table[vif].bytes_in + skb->len);
2051
2052	/* Forward the frame */
2053	if (c->mfc_origin == htonl(INADDR_ANY) &&
2054	    c->mfc_mcastgrp == htonl(INADDR_ANY)) {
2055		if (true_vifi >= 0 &&
2056		    true_vifi != c->_c.mfc_parent &&
2057		    ip_hdr(skb)->ttl >
2058				c->_c.mfc_un.res.ttls[c->_c.mfc_parent]) {
2059			/* It's an (*,*) entry and the packet is not coming from
2060			 * the upstream: forward the packet to the upstream
2061			 * only.
2062			 */
2063			psend = c->_c.mfc_parent;
2064			goto last_forward;
2065		}
2066		goto dont_forward;
2067	}
2068	for (ct = c->_c.mfc_un.res.maxvif - 1;
2069	     ct >= c->_c.mfc_un.res.minvif; ct--) {
2070		/* For (*,G) entry, don't forward to the incoming interface */
2071		if ((c->mfc_origin != htonl(INADDR_ANY) ||
2072		     ct != true_vifi) &&
2073		    ip_hdr(skb)->ttl > c->_c.mfc_un.res.ttls[ct]) {
2074			if (psend != -1) {
2075				struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
2076
2077				if (skb2)
2078					ipmr_queue_xmit(net, mrt, true_vifi,
2079							skb2, psend);
2080			}
2081			psend = ct;
2082		}
2083	}
2084last_forward:
2085	if (psend != -1) {
2086		if (local) {
2087			struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
2088
2089			if (skb2)
2090				ipmr_queue_xmit(net, mrt, true_vifi, skb2,
2091						psend);
2092		} else {
2093			ipmr_queue_xmit(net, mrt, true_vifi, skb, psend);
2094			return;
2095		}
2096	}
2097
2098dont_forward:
2099	if (!local)
2100		kfree_skb(skb);
2101}
2102
2103static struct mr_table *ipmr_rt_fib_lookup(struct net *net, struct sk_buff *skb)
2104{
2105	struct rtable *rt = skb_rtable(skb);
2106	struct iphdr *iph = ip_hdr(skb);
2107	struct flowi4 fl4 = {
2108		.daddr = iph->daddr,
2109		.saddr = iph->saddr,
2110		.flowi4_tos = inet_dscp_to_dsfield(ip4h_dscp(iph)),
2111		.flowi4_oif = (rt_is_output_route(rt) ?
2112			       skb->dev->ifindex : 0),
2113		.flowi4_iif = (rt_is_output_route(rt) ?
2114			       LOOPBACK_IFINDEX :
2115			       skb->dev->ifindex),
2116		.flowi4_mark = skb->mark,
2117	};
2118	struct mr_table *mrt;
2119	int err;
2120
2121	err = ipmr_fib_lookup(net, &fl4, &mrt);
2122	if (err)
2123		return ERR_PTR(err);
2124	return mrt;
2125}
2126
2127/* Multicast packets for forwarding arrive here
2128 * Called with rcu_read_lock();
2129 */
2130int ip_mr_input(struct sk_buff *skb)
2131{
2132	struct mfc_cache *cache;
2133	struct net *net = dev_net(skb->dev);
2134	int local = skb_rtable(skb)->rt_flags & RTCF_LOCAL;
2135	struct mr_table *mrt;
2136	struct net_device *dev;
2137
2138	/* skb->dev passed in is the loX master dev for vrfs.
2139	 * As there are no vifs associated with loopback devices,
2140	 * get the proper interface that does have a vif associated with it.
2141	 */
2142	dev = skb->dev;
2143	if (netif_is_l3_master(skb->dev)) {
2144		dev = dev_get_by_index_rcu(net, IPCB(skb)->iif);
2145		if (!dev) {
2146			kfree_skb(skb);
2147			return -ENODEV;
2148		}
2149	}
2150
2151	/* Packet is looped back after forward, it should not be
2152	 * forwarded second time, but still can be delivered locally.
2153	 */
2154	if (IPCB(skb)->flags & IPSKB_FORWARDED)
2155		goto dont_forward;
2156
2157	mrt = ipmr_rt_fib_lookup(net, skb);
2158	if (IS_ERR(mrt)) {
2159		kfree_skb(skb);
2160		return PTR_ERR(mrt);
2161	}
2162	if (!local) {
2163		if (IPCB(skb)->opt.router_alert) {
2164			if (ip_call_ra_chain(skb))
2165				return 0;
2166		} else if (ip_hdr(skb)->protocol == IPPROTO_IGMP) {
2167			/* IGMPv1 (and broken IGMPv2 implementations sort of
2168			 * Cisco IOS <= 11.2(8)) do not put router alert
2169			 * option to IGMP packets destined to routable
2170			 * groups. It is very bad, because it means
2171			 * that we can forward NO IGMP messages.
2172			 */
2173			struct sock *mroute_sk;
2174
2175			mroute_sk = rcu_dereference(mrt->mroute_sk);
2176			if (mroute_sk) {
2177				nf_reset_ct(skb);
2178				raw_rcv(mroute_sk, skb);
2179				return 0;
2180			}
2181		}
2182	}
2183
2184	/* already under rcu_read_lock() */
2185	cache = ipmr_cache_find(mrt, ip_hdr(skb)->saddr, ip_hdr(skb)->daddr);
2186	if (!cache) {
2187		int vif = ipmr_find_vif(mrt, dev);
2188
2189		if (vif >= 0)
2190			cache = ipmr_cache_find_any(mrt, ip_hdr(skb)->daddr,
2191						    vif);
2192	}
2193
2194	/* No usable cache entry */
2195	if (!cache) {
2196		int vif;
2197
2198		if (local) {
2199			struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
2200			ip_local_deliver(skb);
2201			if (!skb2)
2202				return -ENOBUFS;
2203			skb = skb2;
2204		}
2205
 
2206		vif = ipmr_find_vif(mrt, dev);
2207		if (vif >= 0)
2208			return ipmr_cache_unresolved(mrt, vif, skb, dev);
 
 
 
 
 
2209		kfree_skb(skb);
2210		return -ENODEV;
2211	}
2212
 
2213	ip_mr_forward(net, mrt, dev, skb, cache, local);
 
2214
2215	if (local)
2216		return ip_local_deliver(skb);
2217
2218	return 0;
2219
2220dont_forward:
2221	if (local)
2222		return ip_local_deliver(skb);
2223	kfree_skb(skb);
2224	return 0;
2225}
2226
2227#ifdef CONFIG_IP_PIMSM_V1
2228/* Handle IGMP messages of PIMv1 */
2229int pim_rcv_v1(struct sk_buff *skb)
2230{
2231	struct igmphdr *pim;
2232	struct net *net = dev_net(skb->dev);
2233	struct mr_table *mrt;
2234
2235	if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
2236		goto drop;
2237
2238	pim = igmp_hdr(skb);
2239
2240	mrt = ipmr_rt_fib_lookup(net, skb);
2241	if (IS_ERR(mrt))
2242		goto drop;
2243	if (!mrt->mroute_do_pim ||
2244	    pim->group != PIM_V1_VERSION || pim->code != PIM_V1_REGISTER)
2245		goto drop;
2246
2247	if (__pim_rcv(mrt, skb, sizeof(*pim))) {
2248drop:
2249		kfree_skb(skb);
2250	}
2251	return 0;
2252}
2253#endif
2254
2255#ifdef CONFIG_IP_PIMSM_V2
2256static int pim_rcv(struct sk_buff *skb)
2257{
2258	struct pimreghdr *pim;
2259	struct net *net = dev_net(skb->dev);
2260	struct mr_table *mrt;
2261
2262	if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
2263		goto drop;
2264
2265	pim = (struct pimreghdr *)skb_transport_header(skb);
2266	if (pim->type != ((PIM_VERSION << 4) | (PIM_TYPE_REGISTER)) ||
2267	    (pim->flags & PIM_NULL_REGISTER) ||
2268	    (ip_compute_csum((void *)pim, sizeof(*pim)) != 0 &&
2269	     csum_fold(skb_checksum(skb, 0, skb->len, 0))))
2270		goto drop;
2271
2272	mrt = ipmr_rt_fib_lookup(net, skb);
2273	if (IS_ERR(mrt))
2274		goto drop;
2275	if (__pim_rcv(mrt, skb, sizeof(*pim))) {
2276drop:
2277		kfree_skb(skb);
2278	}
2279	return 0;
2280}
2281#endif
2282
2283int ipmr_get_route(struct net *net, struct sk_buff *skb,
2284		   __be32 saddr, __be32 daddr,
2285		   struct rtmsg *rtm, u32 portid)
2286{
2287	struct mfc_cache *cache;
2288	struct mr_table *mrt;
2289	int err;
2290
2291	rcu_read_lock();
2292	mrt = __ipmr_get_table(net, RT_TABLE_DEFAULT);
2293	if (!mrt) {
2294		rcu_read_unlock();
2295		return -ENOENT;
2296	}
2297
 
2298	cache = ipmr_cache_find(mrt, saddr, daddr);
2299	if (!cache && skb->dev) {
2300		int vif = ipmr_find_vif(mrt, skb->dev);
2301
2302		if (vif >= 0)
2303			cache = ipmr_cache_find_any(mrt, daddr, vif);
2304	}
2305	if (!cache) {
2306		struct sk_buff *skb2;
2307		struct iphdr *iph;
2308		struct net_device *dev;
2309		int vif = -1;
2310
2311		dev = skb->dev;
 
2312		if (dev)
2313			vif = ipmr_find_vif(mrt, dev);
2314		if (vif < 0) {
 
2315			rcu_read_unlock();
2316			return -ENODEV;
2317		}
2318
2319		skb2 = skb_realloc_headroom(skb, sizeof(struct iphdr));
2320		if (!skb2) {
 
2321			rcu_read_unlock();
2322			return -ENOMEM;
2323		}
2324
2325		NETLINK_CB(skb2).portid = portid;
2326		skb_push(skb2, sizeof(struct iphdr));
2327		skb_reset_network_header(skb2);
2328		iph = ip_hdr(skb2);
2329		iph->ihl = sizeof(struct iphdr) >> 2;
2330		iph->saddr = saddr;
2331		iph->daddr = daddr;
2332		iph->version = 0;
2333		err = ipmr_cache_unresolved(mrt, vif, skb2, dev);
 
2334		rcu_read_unlock();
2335		return err;
2336	}
2337
 
2338	err = mr_fill_mroute(mrt, skb, &cache->_c, rtm);
 
2339	rcu_read_unlock();
2340	return err;
2341}
2342
2343static int ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
2344			    u32 portid, u32 seq, struct mfc_cache *c, int cmd,
2345			    int flags)
2346{
2347	struct nlmsghdr *nlh;
2348	struct rtmsg *rtm;
2349	int err;
2350
2351	nlh = nlmsg_put(skb, portid, seq, cmd, sizeof(*rtm), flags);
2352	if (!nlh)
2353		return -EMSGSIZE;
2354
2355	rtm = nlmsg_data(nlh);
2356	rtm->rtm_family   = RTNL_FAMILY_IPMR;
2357	rtm->rtm_dst_len  = 32;
2358	rtm->rtm_src_len  = 32;
2359	rtm->rtm_tos      = 0;
2360	rtm->rtm_table    = mrt->id;
2361	if (nla_put_u32(skb, RTA_TABLE, mrt->id))
2362		goto nla_put_failure;
2363	rtm->rtm_type     = RTN_MULTICAST;
2364	rtm->rtm_scope    = RT_SCOPE_UNIVERSE;
2365	if (c->_c.mfc_flags & MFC_STATIC)
2366		rtm->rtm_protocol = RTPROT_STATIC;
2367	else
2368		rtm->rtm_protocol = RTPROT_MROUTED;
2369	rtm->rtm_flags    = 0;
2370
2371	if (nla_put_in_addr(skb, RTA_SRC, c->mfc_origin) ||
2372	    nla_put_in_addr(skb, RTA_DST, c->mfc_mcastgrp))
2373		goto nla_put_failure;
2374	err = mr_fill_mroute(mrt, skb, &c->_c, rtm);
2375	/* do not break the dump if cache is unresolved */
2376	if (err < 0 && err != -ENOENT)
2377		goto nla_put_failure;
2378
2379	nlmsg_end(skb, nlh);
2380	return 0;
2381
2382nla_put_failure:
2383	nlmsg_cancel(skb, nlh);
2384	return -EMSGSIZE;
2385}
2386
2387static int _ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
2388			     u32 portid, u32 seq, struct mr_mfc *c, int cmd,
2389			     int flags)
2390{
2391	return ipmr_fill_mroute(mrt, skb, portid, seq, (struct mfc_cache *)c,
2392				cmd, flags);
2393}
2394
2395static size_t mroute_msgsize(bool unresolved, int maxvif)
2396{
2397	size_t len =
2398		NLMSG_ALIGN(sizeof(struct rtmsg))
2399		+ nla_total_size(4)	/* RTA_TABLE */
2400		+ nla_total_size(4)	/* RTA_SRC */
2401		+ nla_total_size(4)	/* RTA_DST */
2402		;
2403
2404	if (!unresolved)
2405		len = len
2406		      + nla_total_size(4)	/* RTA_IIF */
2407		      + nla_total_size(0)	/* RTA_MULTIPATH */
2408		      + maxvif * NLA_ALIGN(sizeof(struct rtnexthop))
2409						/* RTA_MFC_STATS */
2410		      + nla_total_size_64bit(sizeof(struct rta_mfc_stats))
2411		;
2412
2413	return len;
2414}
2415
2416static void mroute_netlink_event(struct mr_table *mrt, struct mfc_cache *mfc,
2417				 int cmd)
2418{
2419	struct net *net = read_pnet(&mrt->net);
2420	struct sk_buff *skb;
2421	int err = -ENOBUFS;
2422
2423	skb = nlmsg_new(mroute_msgsize(mfc->_c.mfc_parent >= MAXVIFS,
2424				       mrt->maxvif),
2425			GFP_ATOMIC);
2426	if (!skb)
2427		goto errout;
2428
2429	err = ipmr_fill_mroute(mrt, skb, 0, 0, mfc, cmd, 0);
2430	if (err < 0)
2431		goto errout;
2432
2433	rtnl_notify(skb, net, 0, RTNLGRP_IPV4_MROUTE, NULL, GFP_ATOMIC);
2434	return;
2435
2436errout:
2437	kfree_skb(skb);
2438	rtnl_set_sk_err(net, RTNLGRP_IPV4_MROUTE, err);
 
2439}
2440
2441static size_t igmpmsg_netlink_msgsize(size_t payloadlen)
2442{
2443	size_t len =
2444		NLMSG_ALIGN(sizeof(struct rtgenmsg))
2445		+ nla_total_size(1)	/* IPMRA_CREPORT_MSGTYPE */
2446		+ nla_total_size(4)	/* IPMRA_CREPORT_VIF_ID */
2447		+ nla_total_size(4)	/* IPMRA_CREPORT_SRC_ADDR */
2448		+ nla_total_size(4)	/* IPMRA_CREPORT_DST_ADDR */
2449		+ nla_total_size(4)	/* IPMRA_CREPORT_TABLE */
2450					/* IPMRA_CREPORT_PKT */
2451		+ nla_total_size(payloadlen)
2452		;
2453
2454	return len;
2455}
2456
2457static void igmpmsg_netlink_event(const struct mr_table *mrt, struct sk_buff *pkt)
2458{
2459	struct net *net = read_pnet(&mrt->net);
2460	struct nlmsghdr *nlh;
2461	struct rtgenmsg *rtgenm;
2462	struct igmpmsg *msg;
2463	struct sk_buff *skb;
2464	struct nlattr *nla;
2465	int payloadlen;
2466
2467	payloadlen = pkt->len - sizeof(struct igmpmsg);
2468	msg = (struct igmpmsg *)skb_network_header(pkt);
2469
2470	skb = nlmsg_new(igmpmsg_netlink_msgsize(payloadlen), GFP_ATOMIC);
2471	if (!skb)
2472		goto errout;
2473
2474	nlh = nlmsg_put(skb, 0, 0, RTM_NEWCACHEREPORT,
2475			sizeof(struct rtgenmsg), 0);
2476	if (!nlh)
2477		goto errout;
2478	rtgenm = nlmsg_data(nlh);
2479	rtgenm->rtgen_family = RTNL_FAMILY_IPMR;
2480	if (nla_put_u8(skb, IPMRA_CREPORT_MSGTYPE, msg->im_msgtype) ||
2481	    nla_put_u32(skb, IPMRA_CREPORT_VIF_ID, msg->im_vif | (msg->im_vif_hi << 8)) ||
2482	    nla_put_in_addr(skb, IPMRA_CREPORT_SRC_ADDR,
2483			    msg->im_src.s_addr) ||
2484	    nla_put_in_addr(skb, IPMRA_CREPORT_DST_ADDR,
2485			    msg->im_dst.s_addr) ||
2486	    nla_put_u32(skb, IPMRA_CREPORT_TABLE, mrt->id))
2487		goto nla_put_failure;
2488
2489	nla = nla_reserve(skb, IPMRA_CREPORT_PKT, payloadlen);
2490	if (!nla || skb_copy_bits(pkt, sizeof(struct igmpmsg),
2491				  nla_data(nla), payloadlen))
2492		goto nla_put_failure;
2493
2494	nlmsg_end(skb, nlh);
2495
2496	rtnl_notify(skb, net, 0, RTNLGRP_IPV4_MROUTE_R, NULL, GFP_ATOMIC);
2497	return;
2498
2499nla_put_failure:
2500	nlmsg_cancel(skb, nlh);
2501errout:
2502	kfree_skb(skb);
2503	rtnl_set_sk_err(net, RTNLGRP_IPV4_MROUTE_R, -ENOBUFS);
2504}
2505
2506static int ipmr_rtm_valid_getroute_req(struct sk_buff *skb,
2507				       const struct nlmsghdr *nlh,
2508				       struct nlattr **tb,
2509				       struct netlink_ext_ack *extack)
2510{
2511	struct rtmsg *rtm;
2512	int i, err;
2513
2514	if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*rtm))) {
2515		NL_SET_ERR_MSG(extack, "ipv4: Invalid header for multicast route get request");
2516		return -EINVAL;
2517	}
2518
2519	if (!netlink_strict_get_check(skb))
2520		return nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX,
2521					      rtm_ipv4_policy, extack);
2522
2523	rtm = nlmsg_data(nlh);
2524	if ((rtm->rtm_src_len && rtm->rtm_src_len != 32) ||
2525	    (rtm->rtm_dst_len && rtm->rtm_dst_len != 32) ||
2526	    rtm->rtm_tos || rtm->rtm_table || rtm->rtm_protocol ||
2527	    rtm->rtm_scope || rtm->rtm_type || rtm->rtm_flags) {
2528		NL_SET_ERR_MSG(extack, "ipv4: Invalid values in header for multicast route get request");
2529		return -EINVAL;
2530	}
2531
2532	err = nlmsg_parse_deprecated_strict(nlh, sizeof(*rtm), tb, RTA_MAX,
2533					    rtm_ipv4_policy, extack);
2534	if (err)
2535		return err;
2536
2537	if ((tb[RTA_SRC] && !rtm->rtm_src_len) ||
2538	    (tb[RTA_DST] && !rtm->rtm_dst_len)) {
2539		NL_SET_ERR_MSG(extack, "ipv4: rtm_src_len and rtm_dst_len must be 32 for IPv4");
2540		return -EINVAL;
2541	}
2542
2543	for (i = 0; i <= RTA_MAX; i++) {
2544		if (!tb[i])
2545			continue;
2546
2547		switch (i) {
2548		case RTA_SRC:
2549		case RTA_DST:
2550		case RTA_TABLE:
2551			break;
2552		default:
2553			NL_SET_ERR_MSG(extack, "ipv4: Unsupported attribute in multicast route get request");
2554			return -EINVAL;
2555		}
2556	}
2557
2558	return 0;
2559}
2560
2561static int ipmr_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh,
2562			     struct netlink_ext_ack *extack)
2563{
2564	struct net *net = sock_net(in_skb->sk);
2565	struct nlattr *tb[RTA_MAX + 1];
2566	struct sk_buff *skb = NULL;
2567	struct mfc_cache *cache;
2568	struct mr_table *mrt;
2569	__be32 src, grp;
2570	u32 tableid;
2571	int err;
2572
2573	err = ipmr_rtm_valid_getroute_req(in_skb, nlh, tb, extack);
2574	if (err < 0)
2575		goto errout;
2576
2577	src = nla_get_in_addr_default(tb[RTA_SRC], 0);
2578	grp = nla_get_in_addr_default(tb[RTA_DST], 0);
2579	tableid = nla_get_u32_default(tb[RTA_TABLE], 0);
2580
2581	mrt = __ipmr_get_table(net, tableid ? tableid : RT_TABLE_DEFAULT);
2582	if (!mrt) {
2583		err = -ENOENT;
2584		goto errout_free;
2585	}
2586
2587	/* entries are added/deleted only under RTNL */
2588	rcu_read_lock();
2589	cache = ipmr_cache_find(mrt, src, grp);
2590	rcu_read_unlock();
2591	if (!cache) {
2592		err = -ENOENT;
2593		goto errout_free;
2594	}
2595
2596	skb = nlmsg_new(mroute_msgsize(false, mrt->maxvif), GFP_KERNEL);
2597	if (!skb) {
2598		err = -ENOBUFS;
2599		goto errout_free;
2600	}
2601
2602	err = ipmr_fill_mroute(mrt, skb, NETLINK_CB(in_skb).portid,
2603			       nlh->nlmsg_seq, cache,
2604			       RTM_NEWROUTE, 0);
2605	if (err < 0)
2606		goto errout_free;
2607
2608	err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
2609
2610errout:
2611	return err;
2612
2613errout_free:
2614	kfree_skb(skb);
2615	goto errout;
2616}
2617
2618static int ipmr_rtm_dumproute(struct sk_buff *skb, struct netlink_callback *cb)
2619{
2620	struct fib_dump_filter filter = {
2621		.rtnl_held = true,
2622	};
2623	int err;
2624
2625	if (cb->strict_check) {
2626		err = ip_valid_fib_dump_req(sock_net(skb->sk), cb->nlh,
2627					    &filter, cb);
2628		if (err < 0)
2629			return err;
2630	}
2631
2632	if (filter.table_id) {
2633		struct mr_table *mrt;
2634
2635		mrt = __ipmr_get_table(sock_net(skb->sk), filter.table_id);
2636		if (!mrt) {
2637			if (rtnl_msg_family(cb->nlh) != RTNL_FAMILY_IPMR)
2638				return skb->len;
2639
2640			NL_SET_ERR_MSG(cb->extack, "ipv4: MR table does not exist");
2641			return -ENOENT;
2642		}
2643		err = mr_table_dump(mrt, skb, cb, _ipmr_fill_mroute,
2644				    &mfc_unres_lock, &filter);
2645		return skb->len ? : err;
2646	}
2647
2648	return mr_rtm_dumproute(skb, cb, ipmr_mr_table_iter,
2649				_ipmr_fill_mroute, &mfc_unres_lock, &filter);
2650}
2651
2652static const struct nla_policy rtm_ipmr_policy[RTA_MAX + 1] = {
2653	[RTA_SRC]	= { .type = NLA_U32 },
2654	[RTA_DST]	= { .type = NLA_U32 },
2655	[RTA_IIF]	= { .type = NLA_U32 },
2656	[RTA_TABLE]	= { .type = NLA_U32 },
2657	[RTA_MULTIPATH]	= { .len = sizeof(struct rtnexthop) },
2658};
2659
2660static bool ipmr_rtm_validate_proto(unsigned char rtm_protocol)
2661{
2662	switch (rtm_protocol) {
2663	case RTPROT_STATIC:
2664	case RTPROT_MROUTED:
2665		return true;
2666	}
2667	return false;
2668}
2669
2670static int ipmr_nla_get_ttls(const struct nlattr *nla, struct mfcctl *mfcc)
2671{
2672	struct rtnexthop *rtnh = nla_data(nla);
2673	int remaining = nla_len(nla), vifi = 0;
2674
2675	while (rtnh_ok(rtnh, remaining)) {
2676		mfcc->mfcc_ttls[vifi] = rtnh->rtnh_hops;
2677		if (++vifi == MAXVIFS)
2678			break;
2679		rtnh = rtnh_next(rtnh, &remaining);
2680	}
2681
2682	return remaining > 0 ? -EINVAL : vifi;
2683}
2684
2685/* returns < 0 on error, 0 for ADD_MFC and 1 for ADD_MFC_PROXY */
2686static int rtm_to_ipmr_mfcc(struct net *net, struct nlmsghdr *nlh,
2687			    struct mfcctl *mfcc, int *mrtsock,
2688			    struct mr_table **mrtret,
2689			    struct netlink_ext_ack *extack)
2690{
2691	struct net_device *dev = NULL;
2692	u32 tblid = RT_TABLE_DEFAULT;
2693	struct mr_table *mrt;
2694	struct nlattr *attr;
2695	struct rtmsg *rtm;
2696	int ret, rem;
2697
2698	ret = nlmsg_validate_deprecated(nlh, sizeof(*rtm), RTA_MAX,
2699					rtm_ipmr_policy, extack);
2700	if (ret < 0)
2701		goto out;
2702	rtm = nlmsg_data(nlh);
2703
2704	ret = -EINVAL;
2705	if (rtm->rtm_family != RTNL_FAMILY_IPMR || rtm->rtm_dst_len != 32 ||
2706	    rtm->rtm_type != RTN_MULTICAST ||
2707	    rtm->rtm_scope != RT_SCOPE_UNIVERSE ||
2708	    !ipmr_rtm_validate_proto(rtm->rtm_protocol))
2709		goto out;
2710
2711	memset(mfcc, 0, sizeof(*mfcc));
2712	mfcc->mfcc_parent = -1;
2713	ret = 0;
2714	nlmsg_for_each_attr(attr, nlh, sizeof(struct rtmsg), rem) {
2715		switch (nla_type(attr)) {
2716		case RTA_SRC:
2717			mfcc->mfcc_origin.s_addr = nla_get_be32(attr);
2718			break;
2719		case RTA_DST:
2720			mfcc->mfcc_mcastgrp.s_addr = nla_get_be32(attr);
2721			break;
2722		case RTA_IIF:
2723			dev = __dev_get_by_index(net, nla_get_u32(attr));
2724			if (!dev) {
2725				ret = -ENODEV;
2726				goto out;
2727			}
2728			break;
2729		case RTA_MULTIPATH:
2730			if (ipmr_nla_get_ttls(attr, mfcc) < 0) {
2731				ret = -EINVAL;
2732				goto out;
2733			}
2734			break;
2735		case RTA_PREFSRC:
2736			ret = 1;
2737			break;
2738		case RTA_TABLE:
2739			tblid = nla_get_u32(attr);
2740			break;
2741		}
2742	}
2743	mrt = __ipmr_get_table(net, tblid);
2744	if (!mrt) {
2745		ret = -ENOENT;
2746		goto out;
2747	}
2748	*mrtret = mrt;
2749	*mrtsock = rtm->rtm_protocol == RTPROT_MROUTED ? 1 : 0;
2750	if (dev)
2751		mfcc->mfcc_parent = ipmr_find_vif(mrt, dev);
2752
2753out:
2754	return ret;
2755}
2756
2757/* takes care of both newroute and delroute */
2758static int ipmr_rtm_route(struct sk_buff *skb, struct nlmsghdr *nlh,
2759			  struct netlink_ext_ack *extack)
2760{
2761	struct net *net = sock_net(skb->sk);
2762	int ret, mrtsock, parent;
2763	struct mr_table *tbl;
2764	struct mfcctl mfcc;
2765
2766	mrtsock = 0;
2767	tbl = NULL;
2768	ret = rtm_to_ipmr_mfcc(net, nlh, &mfcc, &mrtsock, &tbl, extack);
2769	if (ret < 0)
2770		return ret;
2771
2772	parent = ret ? mfcc.mfcc_parent : -1;
2773	if (nlh->nlmsg_type == RTM_NEWROUTE)
2774		return ipmr_mfc_add(net, tbl, &mfcc, mrtsock, parent);
2775	else
2776		return ipmr_mfc_delete(tbl, &mfcc, parent);
2777}
2778
2779static bool ipmr_fill_table(struct mr_table *mrt, struct sk_buff *skb)
2780{
2781	u32 queue_len = atomic_read(&mrt->cache_resolve_queue_len);
2782
2783	if (nla_put_u32(skb, IPMRA_TABLE_ID, mrt->id) ||
2784	    nla_put_u32(skb, IPMRA_TABLE_CACHE_RES_QUEUE_LEN, queue_len) ||
2785	    nla_put_s32(skb, IPMRA_TABLE_MROUTE_REG_VIF_NUM,
2786			mrt->mroute_reg_vif_num) ||
2787	    nla_put_u8(skb, IPMRA_TABLE_MROUTE_DO_ASSERT,
2788		       mrt->mroute_do_assert) ||
2789	    nla_put_u8(skb, IPMRA_TABLE_MROUTE_DO_PIM, mrt->mroute_do_pim) ||
2790	    nla_put_u8(skb, IPMRA_TABLE_MROUTE_DO_WRVIFWHOLE,
2791		       mrt->mroute_do_wrvifwhole))
2792		return false;
2793
2794	return true;
2795}
2796
2797static bool ipmr_fill_vif(struct mr_table *mrt, u32 vifid, struct sk_buff *skb)
2798{
2799	struct net_device *vif_dev;
2800	struct nlattr *vif_nest;
2801	struct vif_device *vif;
2802
2803	vif = &mrt->vif_table[vifid];
2804	vif_dev = rtnl_dereference(vif->dev);
2805	/* if the VIF doesn't exist just continue */
2806	if (!vif_dev)
2807		return true;
2808
 
2809	vif_nest = nla_nest_start_noflag(skb, IPMRA_VIF);
2810	if (!vif_nest)
2811		return false;
2812
2813	if (nla_put_u32(skb, IPMRA_VIFA_IFINDEX, vif_dev->ifindex) ||
2814	    nla_put_u32(skb, IPMRA_VIFA_VIF_ID, vifid) ||
2815	    nla_put_u16(skb, IPMRA_VIFA_FLAGS, vif->flags) ||
2816	    nla_put_u64_64bit(skb, IPMRA_VIFA_BYTES_IN, vif->bytes_in,
2817			      IPMRA_VIFA_PAD) ||
2818	    nla_put_u64_64bit(skb, IPMRA_VIFA_BYTES_OUT, vif->bytes_out,
2819			      IPMRA_VIFA_PAD) ||
2820	    nla_put_u64_64bit(skb, IPMRA_VIFA_PACKETS_IN, vif->pkt_in,
2821			      IPMRA_VIFA_PAD) ||
2822	    nla_put_u64_64bit(skb, IPMRA_VIFA_PACKETS_OUT, vif->pkt_out,
2823			      IPMRA_VIFA_PAD) ||
2824	    nla_put_be32(skb, IPMRA_VIFA_LOCAL_ADDR, vif->local) ||
2825	    nla_put_be32(skb, IPMRA_VIFA_REMOTE_ADDR, vif->remote)) {
2826		nla_nest_cancel(skb, vif_nest);
2827		return false;
2828	}
2829	nla_nest_end(skb, vif_nest);
2830
2831	return true;
2832}
2833
2834static int ipmr_valid_dumplink(const struct nlmsghdr *nlh,
2835			       struct netlink_ext_ack *extack)
2836{
2837	struct ifinfomsg *ifm;
2838
2839	if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
2840		NL_SET_ERR_MSG(extack, "ipv4: Invalid header for ipmr link dump");
2841		return -EINVAL;
2842	}
2843
2844	if (nlmsg_attrlen(nlh, sizeof(*ifm))) {
2845		NL_SET_ERR_MSG(extack, "Invalid data after header in ipmr link dump");
2846		return -EINVAL;
2847	}
2848
2849	ifm = nlmsg_data(nlh);
2850	if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
2851	    ifm->ifi_change || ifm->ifi_index) {
2852		NL_SET_ERR_MSG(extack, "Invalid values in header for ipmr link dump request");
2853		return -EINVAL;
2854	}
2855
2856	return 0;
2857}
2858
2859static int ipmr_rtm_dumplink(struct sk_buff *skb, struct netlink_callback *cb)
2860{
2861	struct net *net = sock_net(skb->sk);
2862	struct nlmsghdr *nlh = NULL;
2863	unsigned int t = 0, s_t;
2864	unsigned int e = 0, s_e;
2865	struct mr_table *mrt;
2866
2867	if (cb->strict_check) {
2868		int err = ipmr_valid_dumplink(cb->nlh, cb->extack);
2869
2870		if (err < 0)
2871			return err;
2872	}
2873
2874	s_t = cb->args[0];
2875	s_e = cb->args[1];
2876
2877	ipmr_for_each_table(mrt, net) {
2878		struct nlattr *vifs, *af;
2879		struct ifinfomsg *hdr;
2880		u32 i;
2881
2882		if (t < s_t)
2883			goto skip_table;
2884		nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid,
2885				cb->nlh->nlmsg_seq, RTM_NEWLINK,
2886				sizeof(*hdr), NLM_F_MULTI);
2887		if (!nlh)
2888			break;
2889
2890		hdr = nlmsg_data(nlh);
2891		memset(hdr, 0, sizeof(*hdr));
2892		hdr->ifi_family = RTNL_FAMILY_IPMR;
2893
2894		af = nla_nest_start_noflag(skb, IFLA_AF_SPEC);
2895		if (!af) {
2896			nlmsg_cancel(skb, nlh);
2897			goto out;
2898		}
2899
2900		if (!ipmr_fill_table(mrt, skb)) {
2901			nlmsg_cancel(skb, nlh);
2902			goto out;
2903		}
2904
2905		vifs = nla_nest_start_noflag(skb, IPMRA_TABLE_VIFS);
2906		if (!vifs) {
2907			nla_nest_end(skb, af);
2908			nlmsg_end(skb, nlh);
2909			goto out;
2910		}
2911		for (i = 0; i < mrt->maxvif; i++) {
2912			if (e < s_e)
2913				goto skip_entry;
2914			if (!ipmr_fill_vif(mrt, i, skb)) {
2915				nla_nest_end(skb, vifs);
2916				nla_nest_end(skb, af);
2917				nlmsg_end(skb, nlh);
2918				goto out;
2919			}
2920skip_entry:
2921			e++;
2922		}
2923		s_e = 0;
2924		e = 0;
2925		nla_nest_end(skb, vifs);
2926		nla_nest_end(skb, af);
2927		nlmsg_end(skb, nlh);
2928skip_table:
2929		t++;
2930	}
2931
2932out:
2933	cb->args[1] = e;
2934	cb->args[0] = t;
2935
2936	return skb->len;
2937}
2938
2939#ifdef CONFIG_PROC_FS
2940/* The /proc interfaces to multicast routing :
2941 * /proc/net/ip_mr_cache & /proc/net/ip_mr_vif
2942 */
2943
2944static void *ipmr_vif_seq_start(struct seq_file *seq, loff_t *pos)
2945	__acquires(RCU)
2946{
2947	struct mr_vif_iter *iter = seq->private;
2948	struct net *net = seq_file_net(seq);
2949	struct mr_table *mrt;
2950
2951	rcu_read_lock();
2952	mrt = __ipmr_get_table(net, RT_TABLE_DEFAULT);
2953	if (!mrt) {
2954		rcu_read_unlock();
2955		return ERR_PTR(-ENOENT);
2956	}
2957
2958	iter->mrt = mrt;
2959
 
2960	return mr_vif_seq_start(seq, pos);
2961}
2962
2963static void ipmr_vif_seq_stop(struct seq_file *seq, void *v)
2964	__releases(RCU)
2965{
2966	rcu_read_unlock();
2967}
2968
2969static int ipmr_vif_seq_show(struct seq_file *seq, void *v)
2970{
2971	struct mr_vif_iter *iter = seq->private;
2972	struct mr_table *mrt = iter->mrt;
2973
2974	if (v == SEQ_START_TOKEN) {
2975		seq_puts(seq,
2976			 "Interface      BytesIn  PktsIn  BytesOut PktsOut Flags Local    Remote\n");
2977	} else {
2978		const struct vif_device *vif = v;
2979		const struct net_device *vif_dev;
2980		const char *name;
2981
2982		vif_dev = vif_dev_read(vif);
2983		name = vif_dev ? vif_dev->name : "none";
2984		seq_printf(seq,
2985			   "%2td %-10s %8ld %7ld  %8ld %7ld %05X %08X %08X\n",
2986			   vif - mrt->vif_table,
2987			   name, vif->bytes_in, vif->pkt_in,
2988			   vif->bytes_out, vif->pkt_out,
2989			   vif->flags, vif->local, vif->remote);
2990	}
2991	return 0;
2992}
2993
2994static const struct seq_operations ipmr_vif_seq_ops = {
2995	.start = ipmr_vif_seq_start,
2996	.next  = mr_vif_seq_next,
2997	.stop  = ipmr_vif_seq_stop,
2998	.show  = ipmr_vif_seq_show,
2999};
3000
3001static void *ipmr_mfc_seq_start(struct seq_file *seq, loff_t *pos)
3002{
3003	struct net *net = seq_file_net(seq);
3004	struct mr_table *mrt;
3005
3006	mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
3007	if (!mrt)
3008		return ERR_PTR(-ENOENT);
3009
3010	return mr_mfc_seq_start(seq, pos, mrt, &mfc_unres_lock);
3011}
3012
3013static int ipmr_mfc_seq_show(struct seq_file *seq, void *v)
3014{
3015	int n;
3016
3017	if (v == SEQ_START_TOKEN) {
3018		seq_puts(seq,
3019		 "Group    Origin   Iif     Pkts    Bytes    Wrong Oifs\n");
3020	} else {
3021		const struct mfc_cache *mfc = v;
3022		const struct mr_mfc_iter *it = seq->private;
3023		const struct mr_table *mrt = it->mrt;
3024
3025		seq_printf(seq, "%08X %08X %-3hd",
3026			   (__force u32) mfc->mfc_mcastgrp,
3027			   (__force u32) mfc->mfc_origin,
3028			   mfc->_c.mfc_parent);
3029
3030		if (it->cache != &mrt->mfc_unres_queue) {
3031			seq_printf(seq, " %8lu %8lu %8lu",
3032				   atomic_long_read(&mfc->_c.mfc_un.res.pkt),
3033				   atomic_long_read(&mfc->_c.mfc_un.res.bytes),
3034				   atomic_long_read(&mfc->_c.mfc_un.res.wrong_if));
3035			for (n = mfc->_c.mfc_un.res.minvif;
3036			     n < mfc->_c.mfc_un.res.maxvif; n++) {
3037				if (VIF_EXISTS(mrt, n) &&
3038				    mfc->_c.mfc_un.res.ttls[n] < 255)
3039					seq_printf(seq,
3040					   " %2d:%-3d",
3041					   n, mfc->_c.mfc_un.res.ttls[n]);
3042			}
3043		} else {
3044			/* unresolved mfc_caches don't contain
3045			 * pkt, bytes and wrong_if values
3046			 */
3047			seq_printf(seq, " %8lu %8lu %8lu", 0ul, 0ul, 0ul);
3048		}
3049		seq_putc(seq, '\n');
3050	}
3051	return 0;
3052}
3053
3054static const struct seq_operations ipmr_mfc_seq_ops = {
3055	.start = ipmr_mfc_seq_start,
3056	.next  = mr_mfc_seq_next,
3057	.stop  = mr_mfc_seq_stop,
3058	.show  = ipmr_mfc_seq_show,
3059};
3060#endif
3061
3062#ifdef CONFIG_IP_PIMSM_V2
3063static const struct net_protocol pim_protocol = {
3064	.handler	=	pim_rcv,
 
3065};
3066#endif
3067
3068static unsigned int ipmr_seq_read(const struct net *net)
3069{
3070	return READ_ONCE(net->ipv4.ipmr_seq) + ipmr_rules_seq_read(net);
 
 
3071}
3072
3073static int ipmr_dump(struct net *net, struct notifier_block *nb,
3074		     struct netlink_ext_ack *extack)
3075{
3076	return mr_dump(net, nb, RTNL_FAMILY_IPMR, ipmr_rules_dump,
3077		       ipmr_mr_table_iter, extack);
3078}
3079
3080static const struct fib_notifier_ops ipmr_notifier_ops_template = {
3081	.family		= RTNL_FAMILY_IPMR,
3082	.fib_seq_read	= ipmr_seq_read,
3083	.fib_dump	= ipmr_dump,
3084	.owner		= THIS_MODULE,
3085};
3086
3087static int __net_init ipmr_notifier_init(struct net *net)
3088{
3089	struct fib_notifier_ops *ops;
3090
3091	net->ipv4.ipmr_seq = 0;
3092
3093	ops = fib_notifier_ops_register(&ipmr_notifier_ops_template, net);
3094	if (IS_ERR(ops))
3095		return PTR_ERR(ops);
3096	net->ipv4.ipmr_notifier_ops = ops;
3097
3098	return 0;
3099}
3100
3101static void __net_exit ipmr_notifier_exit(struct net *net)
3102{
3103	fib_notifier_ops_unregister(net->ipv4.ipmr_notifier_ops);
3104	net->ipv4.ipmr_notifier_ops = NULL;
3105}
3106
3107/* Setup for IP multicast routing */
3108static int __net_init ipmr_net_init(struct net *net)
3109{
3110	int err;
3111
3112	err = ipmr_notifier_init(net);
3113	if (err)
3114		goto ipmr_notifier_fail;
3115
3116	err = ipmr_rules_init(net);
3117	if (err < 0)
3118		goto ipmr_rules_fail;
3119
3120#ifdef CONFIG_PROC_FS
3121	err = -ENOMEM;
3122	if (!proc_create_net("ip_mr_vif", 0, net->proc_net, &ipmr_vif_seq_ops,
3123			sizeof(struct mr_vif_iter)))
3124		goto proc_vif_fail;
3125	if (!proc_create_net("ip_mr_cache", 0, net->proc_net, &ipmr_mfc_seq_ops,
3126			sizeof(struct mr_mfc_iter)))
3127		goto proc_cache_fail;
3128#endif
3129	return 0;
3130
3131#ifdef CONFIG_PROC_FS
3132proc_cache_fail:
3133	remove_proc_entry("ip_mr_vif", net->proc_net);
3134proc_vif_fail:
3135	rtnl_lock();
3136	ipmr_rules_exit(net);
3137	rtnl_unlock();
3138#endif
3139ipmr_rules_fail:
3140	ipmr_notifier_exit(net);
3141ipmr_notifier_fail:
3142	return err;
3143}
3144
3145static void __net_exit ipmr_net_exit(struct net *net)
3146{
3147#ifdef CONFIG_PROC_FS
3148	remove_proc_entry("ip_mr_cache", net->proc_net);
3149	remove_proc_entry("ip_mr_vif", net->proc_net);
3150#endif
3151	ipmr_notifier_exit(net);
3152}
3153
3154static void __net_exit ipmr_net_exit_batch(struct list_head *net_list)
3155{
3156	struct net *net;
3157
3158	rtnl_lock();
3159	list_for_each_entry(net, net_list, exit_list)
3160		ipmr_rules_exit(net);
3161	rtnl_unlock();
3162}
3163
3164static struct pernet_operations ipmr_net_ops = {
3165	.init = ipmr_net_init,
3166	.exit = ipmr_net_exit,
3167	.exit_batch = ipmr_net_exit_batch,
3168};
3169
3170static const struct rtnl_msg_handler ipmr_rtnl_msg_handlers[] __initconst = {
3171	{.protocol = RTNL_FAMILY_IPMR, .msgtype = RTM_GETLINK,
3172	 .dumpit = ipmr_rtm_dumplink},
3173	{.protocol = RTNL_FAMILY_IPMR, .msgtype = RTM_NEWROUTE,
3174	 .doit = ipmr_rtm_route},
3175	{.protocol = RTNL_FAMILY_IPMR, .msgtype = RTM_DELROUTE,
3176	 .doit = ipmr_rtm_route},
3177	{.protocol = RTNL_FAMILY_IPMR, .msgtype = RTM_GETROUTE,
3178	 .doit = ipmr_rtm_getroute, .dumpit = ipmr_rtm_dumproute},
3179};
3180
3181int __init ip_mr_init(void)
3182{
3183	int err;
3184
3185	mrt_cachep = KMEM_CACHE(mfc_cache, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
 
 
 
3186
3187	err = register_pernet_subsys(&ipmr_net_ops);
3188	if (err)
3189		goto reg_pernet_fail;
3190
3191	err = register_netdevice_notifier(&ip_mr_notifier);
3192	if (err)
3193		goto reg_notif_fail;
3194#ifdef CONFIG_IP_PIMSM_V2
3195	if (inet_add_protocol(&pim_protocol, IPPROTO_PIM) < 0) {
3196		pr_err("%s: can't add PIM protocol\n", __func__);
3197		err = -EAGAIN;
3198		goto add_proto_fail;
3199	}
3200#endif
3201	rtnl_register_many(ipmr_rtnl_msg_handlers);
 
 
 
 
 
3202
 
 
3203	return 0;
3204
3205#ifdef CONFIG_IP_PIMSM_V2
3206add_proto_fail:
3207	unregister_netdevice_notifier(&ip_mr_notifier);
3208#endif
3209reg_notif_fail:
3210	unregister_pernet_subsys(&ipmr_net_ops);
3211reg_pernet_fail:
3212	kmem_cache_destroy(mrt_cachep);
3213	return err;
3214}