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