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