xref: /linux/net/ipv4/ipmr.c (revision 696e595f707582cd54900d57041721b5223dfdb9)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  *	IP multicast routing support for mrouted 3.6/3.8
41da177e4SLinus Torvalds  *
5113aa838SAlan Cox  *		(c) 1995 Alan Cox, <alan@lxorguk.ukuu.org.uk>
61da177e4SLinus Torvalds  *	  Linux Consultancy and Custom Driver Development
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  *	Fixes:
91da177e4SLinus Torvalds  *	Michael Chastain	:	Incorrect size of copying.
101da177e4SLinus Torvalds  *	Alan Cox		:	Added the cache manager code
111da177e4SLinus Torvalds  *	Alan Cox		:	Fixed the clone/copy bug and device race.
121da177e4SLinus Torvalds  *	Mike McLagan		:	Routing by source
131da177e4SLinus Torvalds  *	Malcolm Beattie		:	Buffer handling fixes.
141da177e4SLinus Torvalds  *	Alexey Kuznetsov	:	Double buffer free and other fixes.
151da177e4SLinus Torvalds  *	SVR Anand		:	Fixed several multicast bugs and problems.
161da177e4SLinus Torvalds  *	Alexey Kuznetsov	:	Status, optimisations and more.
171da177e4SLinus Torvalds  *	Brad Parker		:	Better behaviour on mrouted upcall
181da177e4SLinus Torvalds  *					overflow.
191da177e4SLinus Torvalds  *      Carlos Picoto           :       PIMv1 Support
201da177e4SLinus Torvalds  *	Pavlin Ivanov Radoslavov:	PIMv2 Registers must checksum only PIM header
21f77f13e2SGilles Espinasse  *					Relax this requirement to work with older peers.
221da177e4SLinus Torvalds  */
231da177e4SLinus Torvalds 
247c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
251da177e4SLinus Torvalds #include <linux/types.h>
2608009a76SAlexey Dobriyan #include <linux/cache.h>
274fc268d2SRandy Dunlap #include <linux/capability.h>
281da177e4SLinus Torvalds #include <linux/errno.h>
291da177e4SLinus Torvalds #include <linux/mm.h>
301da177e4SLinus Torvalds #include <linux/kernel.h>
311da177e4SLinus Torvalds #include <linux/fcntl.h>
321da177e4SLinus Torvalds #include <linux/stat.h>
331da177e4SLinus Torvalds #include <linux/socket.h>
341da177e4SLinus Torvalds #include <linux/in.h>
351da177e4SLinus Torvalds #include <linux/inet.h>
361da177e4SLinus Torvalds #include <linux/netdevice.h>
371da177e4SLinus Torvalds #include <linux/inetdevice.h>
381da177e4SLinus Torvalds #include <linux/igmp.h>
391da177e4SLinus Torvalds #include <linux/proc_fs.h>
401da177e4SLinus Torvalds #include <linux/seq_file.h>
411da177e4SLinus Torvalds #include <linux/mroute.h>
421da177e4SLinus Torvalds #include <linux/init.h>
4346f25dffSKris Katterjohn #include <linux/if_ether.h>
445a0e3ad6STejun Heo #include <linux/slab.h>
45457c4cbcSEric W. Biederman #include <net/net_namespace.h>
461da177e4SLinus Torvalds #include <net/ip.h>
471da177e4SLinus Torvalds #include <net/protocol.h>
481da177e4SLinus Torvalds #include <linux/skbuff.h>
4914c85021SArnaldo Carvalho de Melo #include <net/route.h>
501da177e4SLinus Torvalds #include <net/icmp.h>
511da177e4SLinus Torvalds #include <net/udp.h>
521da177e4SLinus Torvalds #include <net/raw.h>
531da177e4SLinus Torvalds #include <linux/notifier.h>
541da177e4SLinus Torvalds #include <linux/if_arp.h>
551da177e4SLinus Torvalds #include <linux/netfilter_ipv4.h>
56709b46e8SEric W. Biederman #include <linux/compat.h>
57bc3b2d7fSPaul Gortmaker #include <linux/export.h>
580eb71a9dSNeilBrown #include <linux/rhashtable.h>
59c5441932SPravin B Shelar #include <net/ip_tunnels.h>
601da177e4SLinus Torvalds #include <net/checksum.h>
61dc5fc579SArnaldo Carvalho de Melo #include <net/netlink.h>
62f0ad0860SPatrick McHardy #include <net/fib_rules.h>
63d67b8c61SNicolas Dichtel #include <linux/netconf.h>
643c618c1dSDavid Ahern #include <net/rtnh.h>
651da177e4SLinus Torvalds 
665648451eSGustavo A. R. Silva #include <linux/nospec.h>
675648451eSGustavo A. R. Silva 
68f0ad0860SPatrick McHardy struct ipmr_rule {
69f0ad0860SPatrick McHardy 	struct fib_rule		common;
70f0ad0860SPatrick McHardy };
71f0ad0860SPatrick McHardy 
72f0ad0860SPatrick McHardy struct ipmr_result {
73f0ad0860SPatrick McHardy 	struct mr_table		*mrt;
74f0ad0860SPatrick McHardy };
75f0ad0860SPatrick McHardy 
761da177e4SLinus Torvalds /* Big lock, protecting vif table, mrt cache and mroute socket state.
77a8cb16ddSEric Dumazet  * Note that the changes are semaphored via rtnl_lock.
781da177e4SLinus Torvalds  */
791da177e4SLinus Torvalds 
801da177e4SLinus Torvalds static DEFINE_RWLOCK(mrt_lock);
811da177e4SLinus Torvalds 
827ef8f65dSNikolay Aleksandrov /* Multicast router control variables */
831da177e4SLinus Torvalds 
841da177e4SLinus Torvalds /* Special spinlock for queue of unresolved entries */
851da177e4SLinus Torvalds static DEFINE_SPINLOCK(mfc_unres_lock);
861da177e4SLinus Torvalds 
871da177e4SLinus Torvalds /* We return to original Alan's scheme. Hash table of resolved
88a8cb16ddSEric Dumazet  * entries is changed only in process context and protected
89a8cb16ddSEric Dumazet  * with weak lock mrt_lock. Queue of unresolved entries is protected
90a8cb16ddSEric Dumazet  * with strong spinlock mfc_unres_lock.
91a8cb16ddSEric Dumazet  *
92a8cb16ddSEric Dumazet  * In this case data path is free of exclusive locks at all.
931da177e4SLinus Torvalds  */
941da177e4SLinus Torvalds 
9508009a76SAlexey Dobriyan static struct kmem_cache *mrt_cachep __ro_after_init;
961da177e4SLinus Torvalds 
97f0ad0860SPatrick McHardy static struct mr_table *ipmr_new_table(struct net *net, u32 id);
98acbb219dSFrancesco Ruggeri static void ipmr_free_table(struct mr_table *mrt);
99acbb219dSFrancesco Ruggeri 
100c4854ec8SRami Rosen static void ip_mr_forward(struct net *net, struct mr_table *mrt,
1014b1f0d33SDonald Sharp 			  struct net_device *dev, struct sk_buff *skb,
1024b1f0d33SDonald Sharp 			  struct mfc_cache *cache, int local);
1030c12295aSPatrick McHardy static int ipmr_cache_report(struct mr_table *mrt,
1044feb88e5SBenjamin Thery 			     struct sk_buff *pkt, vifi_t vifi, int assert);
1058cd3ac9fSNicolas Dichtel static void mroute_netlink_event(struct mr_table *mrt, struct mfc_cache *mfc,
1068cd3ac9fSNicolas Dichtel 				 int cmd);
1075a645dd8SJulien Gomes static void igmpmsg_netlink_event(struct mr_table *mrt, struct sk_buff *pkt);
108ca8d4794SCallum Sinclair static void mroute_clean_tables(struct mr_table *mrt, int flags);
109e99e88a9SKees Cook static void ipmr_expire_process(struct timer_list *t);
1101da177e4SLinus Torvalds 
111f0ad0860SPatrick McHardy #ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
112f0ad0860SPatrick McHardy #define ipmr_for_each_table(mrt, net)					\
113a14fbcd4SAmol Grover 	list_for_each_entry_rcu(mrt, &net->ipv4.mr_tables, list,	\
1147013908cSAmol Grover 				lockdep_rtnl_is_held() ||		\
1157013908cSAmol Grover 				list_empty(&net->ipv4.mr_tables))
116f0ad0860SPatrick McHardy 
1177b0db857SYuval Mintz static struct mr_table *ipmr_mr_table_iter(struct net *net,
1187b0db857SYuval Mintz 					   struct mr_table *mrt)
1197b0db857SYuval Mintz {
1207b0db857SYuval Mintz 	struct mr_table *ret;
1217b0db857SYuval Mintz 
1227b0db857SYuval Mintz 	if (!mrt)
1237b0db857SYuval Mintz 		ret = list_entry_rcu(net->ipv4.mr_tables.next,
1247b0db857SYuval Mintz 				     struct mr_table, list);
1257b0db857SYuval Mintz 	else
1267b0db857SYuval Mintz 		ret = list_entry_rcu(mrt->list.next,
1277b0db857SYuval Mintz 				     struct mr_table, list);
1287b0db857SYuval Mintz 
1297b0db857SYuval Mintz 	if (&ret->list == &net->ipv4.mr_tables)
1307b0db857SYuval Mintz 		return NULL;
1317b0db857SYuval Mintz 	return ret;
1327b0db857SYuval Mintz }
1337b0db857SYuval Mintz 
134f0ad0860SPatrick McHardy static struct mr_table *ipmr_get_table(struct net *net, u32 id)
135f0ad0860SPatrick McHardy {
136f0ad0860SPatrick McHardy 	struct mr_table *mrt;
137f0ad0860SPatrick McHardy 
138f0ad0860SPatrick McHardy 	ipmr_for_each_table(mrt, net) {
139f0ad0860SPatrick McHardy 		if (mrt->id == id)
140f0ad0860SPatrick McHardy 			return mrt;
141f0ad0860SPatrick McHardy 	}
142f0ad0860SPatrick McHardy 	return NULL;
143f0ad0860SPatrick McHardy }
144f0ad0860SPatrick McHardy 
145da91981bSDavid S. Miller static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4,
146f0ad0860SPatrick McHardy 			   struct mr_table **mrt)
147f0ad0860SPatrick McHardy {
148f0ad0860SPatrick McHardy 	int err;
14995f4a45dSHannes Frederic Sowa 	struct ipmr_result res;
15095f4a45dSHannes Frederic Sowa 	struct fib_lookup_arg arg = {
15195f4a45dSHannes Frederic Sowa 		.result = &res,
15295f4a45dSHannes Frederic Sowa 		.flags = FIB_LOOKUP_NOREF,
15395f4a45dSHannes Frederic Sowa 	};
154f0ad0860SPatrick McHardy 
155e58e4159SDavid Ahern 	/* update flow if oif or iif point to device enslaved to l3mdev */
156e58e4159SDavid Ahern 	l3mdev_update_flow(net, flowi4_to_flowi(flp4));
157e58e4159SDavid Ahern 
158da91981bSDavid S. Miller 	err = fib_rules_lookup(net->ipv4.mr_rules_ops,
159da91981bSDavid S. Miller 			       flowi4_to_flowi(flp4), 0, &arg);
160f0ad0860SPatrick McHardy 	if (err < 0)
161f0ad0860SPatrick McHardy 		return err;
162f0ad0860SPatrick McHardy 	*mrt = res.mrt;
163f0ad0860SPatrick McHardy 	return 0;
164f0ad0860SPatrick McHardy }
165f0ad0860SPatrick McHardy 
166f0ad0860SPatrick McHardy static int ipmr_rule_action(struct fib_rule *rule, struct flowi *flp,
167f0ad0860SPatrick McHardy 			    int flags, struct fib_lookup_arg *arg)
168f0ad0860SPatrick McHardy {
169f0ad0860SPatrick McHardy 	struct ipmr_result *res = arg->result;
170f0ad0860SPatrick McHardy 	struct mr_table *mrt;
171f0ad0860SPatrick McHardy 
172f0ad0860SPatrick McHardy 	switch (rule->action) {
173f0ad0860SPatrick McHardy 	case FR_ACT_TO_TBL:
174f0ad0860SPatrick McHardy 		break;
175f0ad0860SPatrick McHardy 	case FR_ACT_UNREACHABLE:
176f0ad0860SPatrick McHardy 		return -ENETUNREACH;
177f0ad0860SPatrick McHardy 	case FR_ACT_PROHIBIT:
178f0ad0860SPatrick McHardy 		return -EACCES;
179f0ad0860SPatrick McHardy 	case FR_ACT_BLACKHOLE:
180f0ad0860SPatrick McHardy 	default:
181f0ad0860SPatrick McHardy 		return -EINVAL;
182f0ad0860SPatrick McHardy 	}
183f0ad0860SPatrick McHardy 
184e58e4159SDavid Ahern 	arg->table = fib_rule_get_table(rule, arg);
185e58e4159SDavid Ahern 
186e58e4159SDavid Ahern 	mrt = ipmr_get_table(rule->fr_net, arg->table);
18751456b29SIan Morris 	if (!mrt)
188f0ad0860SPatrick McHardy 		return -EAGAIN;
189f0ad0860SPatrick McHardy 	res->mrt = mrt;
190f0ad0860SPatrick McHardy 	return 0;
191f0ad0860SPatrick McHardy }
192f0ad0860SPatrick McHardy 
193f0ad0860SPatrick McHardy static int ipmr_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
194f0ad0860SPatrick McHardy {
195f0ad0860SPatrick McHardy 	return 1;
196f0ad0860SPatrick McHardy }
197f0ad0860SPatrick McHardy 
198f0ad0860SPatrick McHardy static int ipmr_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
199b16fb418SRoopa Prabhu 			       struct fib_rule_hdr *frh, struct nlattr **tb,
200b16fb418SRoopa Prabhu 			       struct netlink_ext_ack *extack)
201f0ad0860SPatrick McHardy {
202f0ad0860SPatrick McHardy 	return 0;
203f0ad0860SPatrick McHardy }
204f0ad0860SPatrick McHardy 
205f0ad0860SPatrick McHardy static int ipmr_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
206f0ad0860SPatrick McHardy 			     struct nlattr **tb)
207f0ad0860SPatrick McHardy {
208f0ad0860SPatrick McHardy 	return 1;
209f0ad0860SPatrick McHardy }
210f0ad0860SPatrick McHardy 
211f0ad0860SPatrick McHardy static int ipmr_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
212f0ad0860SPatrick McHardy 			  struct fib_rule_hdr *frh)
213f0ad0860SPatrick McHardy {
214f0ad0860SPatrick McHardy 	frh->dst_len = 0;
215f0ad0860SPatrick McHardy 	frh->src_len = 0;
216f0ad0860SPatrick McHardy 	frh->tos     = 0;
217f0ad0860SPatrick McHardy 	return 0;
218f0ad0860SPatrick McHardy }
219f0ad0860SPatrick McHardy 
22004a6f82cSAndi Kleen static const struct fib_rules_ops __net_initconst ipmr_rules_ops_template = {
22125239ceeSPatrick McHardy 	.family		= RTNL_FAMILY_IPMR,
222f0ad0860SPatrick McHardy 	.rule_size	= sizeof(struct ipmr_rule),
223f0ad0860SPatrick McHardy 	.addr_size	= sizeof(u32),
224f0ad0860SPatrick McHardy 	.action		= ipmr_rule_action,
225f0ad0860SPatrick McHardy 	.match		= ipmr_rule_match,
226f0ad0860SPatrick McHardy 	.configure	= ipmr_rule_configure,
227f0ad0860SPatrick McHardy 	.compare	= ipmr_rule_compare,
228f0ad0860SPatrick McHardy 	.fill		= ipmr_rule_fill,
229f0ad0860SPatrick McHardy 	.nlgroup	= RTNLGRP_IPV4_RULE,
230f0ad0860SPatrick McHardy 	.owner		= THIS_MODULE,
231f0ad0860SPatrick McHardy };
232f0ad0860SPatrick McHardy 
233f0ad0860SPatrick McHardy static int __net_init ipmr_rules_init(struct net *net)
234f0ad0860SPatrick McHardy {
235f0ad0860SPatrick McHardy 	struct fib_rules_ops *ops;
236f0ad0860SPatrick McHardy 	struct mr_table *mrt;
237f0ad0860SPatrick McHardy 	int err;
238f0ad0860SPatrick McHardy 
239f0ad0860SPatrick McHardy 	ops = fib_rules_register(&ipmr_rules_ops_template, net);
240f0ad0860SPatrick McHardy 	if (IS_ERR(ops))
241f0ad0860SPatrick McHardy 		return PTR_ERR(ops);
242f0ad0860SPatrick McHardy 
243f0ad0860SPatrick McHardy 	INIT_LIST_HEAD(&net->ipv4.mr_tables);
244f0ad0860SPatrick McHardy 
245f0ad0860SPatrick McHardy 	mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
2461113ebbcSNikolay Aleksandrov 	if (IS_ERR(mrt)) {
2471113ebbcSNikolay Aleksandrov 		err = PTR_ERR(mrt);
248f0ad0860SPatrick McHardy 		goto err1;
249f0ad0860SPatrick McHardy 	}
250f0ad0860SPatrick McHardy 
251f0ad0860SPatrick McHardy 	err = fib_default_rule_add(ops, 0x7fff, RT_TABLE_DEFAULT, 0);
252f0ad0860SPatrick McHardy 	if (err < 0)
253f0ad0860SPatrick McHardy 		goto err2;
254f0ad0860SPatrick McHardy 
255f0ad0860SPatrick McHardy 	net->ipv4.mr_rules_ops = ops;
256f0ad0860SPatrick McHardy 	return 0;
257f0ad0860SPatrick McHardy 
258f0ad0860SPatrick McHardy err2:
259f243e5a7SWANG Cong 	ipmr_free_table(mrt);
260f0ad0860SPatrick McHardy err1:
261f0ad0860SPatrick McHardy 	fib_rules_unregister(ops);
262f0ad0860SPatrick McHardy 	return err;
263f0ad0860SPatrick McHardy }
264f0ad0860SPatrick McHardy 
265f0ad0860SPatrick McHardy static void __net_exit ipmr_rules_exit(struct net *net)
266f0ad0860SPatrick McHardy {
267f0ad0860SPatrick McHardy 	struct mr_table *mrt, *next;
268f0ad0860SPatrick McHardy 
269*696e595fSEric Dumazet 	ASSERT_RTNL();
270035320d5SEric Dumazet 	list_for_each_entry_safe(mrt, next, &net->ipv4.mr_tables, list) {
271035320d5SEric Dumazet 		list_del(&mrt->list);
272acbb219dSFrancesco Ruggeri 		ipmr_free_table(mrt);
273035320d5SEric Dumazet 	}
274f0ad0860SPatrick McHardy 	fib_rules_unregister(net->ipv4.mr_rules_ops);
275f0ad0860SPatrick McHardy }
2764d65b948SYotam Gigi 
277b7a59557SJiri Pirko static int ipmr_rules_dump(struct net *net, struct notifier_block *nb,
278b7a59557SJiri Pirko 			   struct netlink_ext_ack *extack)
2794d65b948SYotam Gigi {
280b7a59557SJiri Pirko 	return fib_rules_dump(net, nb, RTNL_FAMILY_IPMR, extack);
2814d65b948SYotam Gigi }
2824d65b948SYotam Gigi 
2834d65b948SYotam Gigi static unsigned int ipmr_rules_seq_read(struct net *net)
2844d65b948SYotam Gigi {
2854d65b948SYotam Gigi 	return fib_rules_seq_read(net, RTNL_FAMILY_IPMR);
2864d65b948SYotam Gigi }
287478e4c2fSYotam Gigi 
288478e4c2fSYotam Gigi bool ipmr_rule_default(const struct fib_rule *rule)
289478e4c2fSYotam Gigi {
290478e4c2fSYotam Gigi 	return fib_rule_matchall(rule) && rule->table == RT_TABLE_DEFAULT;
291478e4c2fSYotam Gigi }
292478e4c2fSYotam Gigi EXPORT_SYMBOL(ipmr_rule_default);
293f0ad0860SPatrick McHardy #else
294f0ad0860SPatrick McHardy #define ipmr_for_each_table(mrt, net) \
295f0ad0860SPatrick McHardy 	for (mrt = net->ipv4.mrt; mrt; mrt = NULL)
296f0ad0860SPatrick McHardy 
2977b0db857SYuval Mintz static struct mr_table *ipmr_mr_table_iter(struct net *net,
2987b0db857SYuval Mintz 					   struct mr_table *mrt)
2997b0db857SYuval Mintz {
3007b0db857SYuval Mintz 	if (!mrt)
3017b0db857SYuval Mintz 		return net->ipv4.mrt;
3027b0db857SYuval Mintz 	return NULL;
3037b0db857SYuval Mintz }
3047b0db857SYuval Mintz 
305f0ad0860SPatrick McHardy static struct mr_table *ipmr_get_table(struct net *net, u32 id)
306f0ad0860SPatrick McHardy {
307f0ad0860SPatrick McHardy 	return net->ipv4.mrt;
308f0ad0860SPatrick McHardy }
309f0ad0860SPatrick McHardy 
310da91981bSDavid S. Miller static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4,
311f0ad0860SPatrick McHardy 			   struct mr_table **mrt)
312f0ad0860SPatrick McHardy {
313f0ad0860SPatrick McHardy 	*mrt = net->ipv4.mrt;
314f0ad0860SPatrick McHardy 	return 0;
315f0ad0860SPatrick McHardy }
316f0ad0860SPatrick McHardy 
317f0ad0860SPatrick McHardy static int __net_init ipmr_rules_init(struct net *net)
318f0ad0860SPatrick McHardy {
3191113ebbcSNikolay Aleksandrov 	struct mr_table *mrt;
3201113ebbcSNikolay Aleksandrov 
3211113ebbcSNikolay Aleksandrov 	mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
3221113ebbcSNikolay Aleksandrov 	if (IS_ERR(mrt))
3231113ebbcSNikolay Aleksandrov 		return PTR_ERR(mrt);
3241113ebbcSNikolay Aleksandrov 	net->ipv4.mrt = mrt;
3251113ebbcSNikolay Aleksandrov 	return 0;
326f0ad0860SPatrick McHardy }
327f0ad0860SPatrick McHardy 
328f0ad0860SPatrick McHardy static void __net_exit ipmr_rules_exit(struct net *net)
329f0ad0860SPatrick McHardy {
330*696e595fSEric Dumazet 	ASSERT_RTNL();
331acbb219dSFrancesco Ruggeri 	ipmr_free_table(net->ipv4.mrt);
332ed785309SWANG Cong 	net->ipv4.mrt = NULL;
333f0ad0860SPatrick McHardy }
3344d65b948SYotam Gigi 
335b7a59557SJiri Pirko static int ipmr_rules_dump(struct net *net, struct notifier_block *nb,
336b7a59557SJiri Pirko 			   struct netlink_ext_ack *extack)
3374d65b948SYotam Gigi {
3384d65b948SYotam Gigi 	return 0;
3394d65b948SYotam Gigi }
3404d65b948SYotam Gigi 
3414d65b948SYotam Gigi static unsigned int ipmr_rules_seq_read(struct net *net)
3424d65b948SYotam Gigi {
3434d65b948SYotam Gigi 	return 0;
3444d65b948SYotam Gigi }
345478e4c2fSYotam Gigi 
346478e4c2fSYotam Gigi bool ipmr_rule_default(const struct fib_rule *rule)
347478e4c2fSYotam Gigi {
348478e4c2fSYotam Gigi 	return true;
349478e4c2fSYotam Gigi }
350478e4c2fSYotam Gigi EXPORT_SYMBOL(ipmr_rule_default);
351f0ad0860SPatrick McHardy #endif
352f0ad0860SPatrick McHardy 
3538fb472c0SNikolay Aleksandrov static inline int ipmr_hash_cmp(struct rhashtable_compare_arg *arg,
3548fb472c0SNikolay Aleksandrov 				const void *ptr)
3558fb472c0SNikolay Aleksandrov {
3568fb472c0SNikolay Aleksandrov 	const struct mfc_cache_cmp_arg *cmparg = arg->key;
3578fb472c0SNikolay Aleksandrov 	struct mfc_cache *c = (struct mfc_cache *)ptr;
3588fb472c0SNikolay Aleksandrov 
3598fb472c0SNikolay Aleksandrov 	return cmparg->mfc_mcastgrp != c->mfc_mcastgrp ||
3608fb472c0SNikolay Aleksandrov 	       cmparg->mfc_origin != c->mfc_origin;
3618fb472c0SNikolay Aleksandrov }
3628fb472c0SNikolay Aleksandrov 
3638fb472c0SNikolay Aleksandrov static const struct rhashtable_params ipmr_rht_params = {
364494fff56SYuval Mintz 	.head_offset = offsetof(struct mr_mfc, mnode),
3658fb472c0SNikolay Aleksandrov 	.key_offset = offsetof(struct mfc_cache, cmparg),
3668fb472c0SNikolay Aleksandrov 	.key_len = sizeof(struct mfc_cache_cmp_arg),
3678fb472c0SNikolay Aleksandrov 	.nelem_hint = 3,
3688fb472c0SNikolay Aleksandrov 	.obj_cmpfn = ipmr_hash_cmp,
3698fb472c0SNikolay Aleksandrov 	.automatic_shrinking = true,
3708fb472c0SNikolay Aleksandrov };
3718fb472c0SNikolay Aleksandrov 
3720bbbf0e7SYuval Mintz static void ipmr_new_table_set(struct mr_table *mrt,
3730bbbf0e7SYuval Mintz 			       struct net *net)
3740bbbf0e7SYuval Mintz {
3750bbbf0e7SYuval Mintz #ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
3760bbbf0e7SYuval Mintz 	list_add_tail_rcu(&mrt->list, &net->ipv4.mr_tables);
3770bbbf0e7SYuval Mintz #endif
3780bbbf0e7SYuval Mintz }
3790bbbf0e7SYuval Mintz 
380845c9a7aSYuval Mintz static struct mfc_cache_cmp_arg ipmr_mr_table_ops_cmparg_any = {
381845c9a7aSYuval Mintz 	.mfc_mcastgrp = htonl(INADDR_ANY),
382845c9a7aSYuval Mintz 	.mfc_origin = htonl(INADDR_ANY),
383845c9a7aSYuval Mintz };
384845c9a7aSYuval Mintz 
385845c9a7aSYuval Mintz static struct mr_table_ops ipmr_mr_table_ops = {
386845c9a7aSYuval Mintz 	.rht_params = &ipmr_rht_params,
387845c9a7aSYuval Mintz 	.cmparg_any = &ipmr_mr_table_ops_cmparg_any,
388845c9a7aSYuval Mintz };
389845c9a7aSYuval Mintz 
390f0ad0860SPatrick McHardy static struct mr_table *ipmr_new_table(struct net *net, u32 id)
391f0ad0860SPatrick McHardy {
392f0ad0860SPatrick McHardy 	struct mr_table *mrt;
393f0ad0860SPatrick McHardy 
3941113ebbcSNikolay Aleksandrov 	/* "pimreg%u" should not exceed 16 bytes (IFNAMSIZ) */
3951113ebbcSNikolay Aleksandrov 	if (id != RT_TABLE_DEFAULT && id >= 1000000000)
3961113ebbcSNikolay Aleksandrov 		return ERR_PTR(-EINVAL);
3971113ebbcSNikolay Aleksandrov 
398f0ad0860SPatrick McHardy 	mrt = ipmr_get_table(net, id);
39900db4124SIan Morris 	if (mrt)
400f0ad0860SPatrick McHardy 		return mrt;
401f0ad0860SPatrick McHardy 
402845c9a7aSYuval Mintz 	return mr_table_alloc(net, id, &ipmr_mr_table_ops,
4030bbbf0e7SYuval Mintz 			      ipmr_expire_process, ipmr_new_table_set);
404f0ad0860SPatrick McHardy }
4051da177e4SLinus Torvalds 
406acbb219dSFrancesco Ruggeri static void ipmr_free_table(struct mr_table *mrt)
407acbb219dSFrancesco Ruggeri {
408acbb219dSFrancesco Ruggeri 	del_timer_sync(&mrt->ipmr_expire_timer);
409ca8d4794SCallum Sinclair 	mroute_clean_tables(mrt, MRT_FLUSH_VIFS | MRT_FLUSH_VIFS_STATIC |
410ca8d4794SCallum Sinclair 				 MRT_FLUSH_MFC | MRT_FLUSH_MFC_STATIC);
4118fb472c0SNikolay Aleksandrov 	rhltable_destroy(&mrt->mfc_hash);
412acbb219dSFrancesco Ruggeri 	kfree(mrt);
413acbb219dSFrancesco Ruggeri }
414acbb219dSFrancesco Ruggeri 
4151da177e4SLinus Torvalds /* Service routines creating virtual interfaces: DVMRP tunnels and PIMREG */
4161da177e4SLinus Torvalds 
417a0b47736SNikolay Aleksandrov /* Initialize ipmr pimreg/tunnel in_device */
418a0b47736SNikolay Aleksandrov static bool ipmr_init_vif_indev(const struct net_device *dev)
419a0b47736SNikolay Aleksandrov {
420a0b47736SNikolay Aleksandrov 	struct in_device *in_dev;
421a0b47736SNikolay Aleksandrov 
422a0b47736SNikolay Aleksandrov 	ASSERT_RTNL();
423a0b47736SNikolay Aleksandrov 
424a0b47736SNikolay Aleksandrov 	in_dev = __in_dev_get_rtnl(dev);
425a0b47736SNikolay Aleksandrov 	if (!in_dev)
426a0b47736SNikolay Aleksandrov 		return false;
427a0b47736SNikolay Aleksandrov 	ipv4_devconf_setall(in_dev);
428a0b47736SNikolay Aleksandrov 	neigh_parms_data_state_setall(in_dev->arp_parms);
429a0b47736SNikolay Aleksandrov 	IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0;
430a0b47736SNikolay Aleksandrov 
431a0b47736SNikolay Aleksandrov 	return true;
432a0b47736SNikolay Aleksandrov }
433a0b47736SNikolay Aleksandrov 
4347ef8f65dSNikolay Aleksandrov static struct net_device *ipmr_new_tunnel(struct net *net, struct vifctl *v)
4351da177e4SLinus Torvalds {
436c384b8a7SChristoph Hellwig 	struct net_device *tunnel_dev, *new_dev;
437c384b8a7SChristoph Hellwig 	struct ip_tunnel_parm p = { };
438c384b8a7SChristoph Hellwig 	int err;
4391da177e4SLinus Torvalds 
440c384b8a7SChristoph Hellwig 	tunnel_dev = __dev_get_by_name(net, "tunl0");
441c384b8a7SChristoph Hellwig 	if (!tunnel_dev)
442c384b8a7SChristoph Hellwig 		goto out;
443c384b8a7SChristoph Hellwig 
4441da177e4SLinus Torvalds 	p.iph.daddr = v->vifc_rmt_addr.s_addr;
4451da177e4SLinus Torvalds 	p.iph.saddr = v->vifc_lcl_addr.s_addr;
4461da177e4SLinus Torvalds 	p.iph.version = 4;
4471da177e4SLinus Torvalds 	p.iph.ihl = 5;
4481da177e4SLinus Torvalds 	p.iph.protocol = IPPROTO_IPIP;
4491da177e4SLinus Torvalds 	sprintf(p.name, "dvmrp%d", v->vifc_vifi);
4501da177e4SLinus Torvalds 
451c7e36705SChristoph Hellwig 	if (!tunnel_dev->netdev_ops->ndo_tunnel_ctl)
452c384b8a7SChristoph Hellwig 		goto out;
453c7e36705SChristoph Hellwig 	err = tunnel_dev->netdev_ops->ndo_tunnel_ctl(tunnel_dev, &p,
454c384b8a7SChristoph Hellwig 			SIOCADDTUNNEL);
455c384b8a7SChristoph Hellwig 	if (err)
456c384b8a7SChristoph Hellwig 		goto out;
4571da177e4SLinus Torvalds 
458c384b8a7SChristoph Hellwig 	new_dev = __dev_get_by_name(net, p.name);
459c384b8a7SChristoph Hellwig 	if (!new_dev)
460c384b8a7SChristoph Hellwig 		goto out;
4611da177e4SLinus Torvalds 
462c384b8a7SChristoph Hellwig 	new_dev->flags |= IFF_MULTICAST;
463c384b8a7SChristoph Hellwig 	if (!ipmr_init_vif_indev(new_dev))
464c384b8a7SChristoph Hellwig 		goto out_unregister;
465c384b8a7SChristoph Hellwig 	if (dev_open(new_dev, NULL))
466c384b8a7SChristoph Hellwig 		goto out_unregister;
467c384b8a7SChristoph Hellwig 	dev_hold(new_dev);
468c1fd1182SChristoph Hellwig 	err = dev_set_allmulti(new_dev, 1);
469c1fd1182SChristoph Hellwig 	if (err) {
470c1fd1182SChristoph Hellwig 		dev_close(new_dev);
471c7e36705SChristoph Hellwig 		tunnel_dev->netdev_ops->ndo_tunnel_ctl(tunnel_dev, &p,
472c1fd1182SChristoph Hellwig 				SIOCDELTUNNEL);
473c1fd1182SChristoph Hellwig 		dev_put(new_dev);
474c1fd1182SChristoph Hellwig 		new_dev = ERR_PTR(err);
475c1fd1182SChristoph Hellwig 	}
476c384b8a7SChristoph Hellwig 	return new_dev;
477c384b8a7SChristoph Hellwig 
478c384b8a7SChristoph Hellwig out_unregister:
479c384b8a7SChristoph Hellwig 	unregister_netdevice(new_dev);
480c384b8a7SChristoph Hellwig out:
481c1fd1182SChristoph Hellwig 	return ERR_PTR(-ENOBUFS);
4821da177e4SLinus Torvalds }
4831da177e4SLinus Torvalds 
484c316c629SNikolay Aleksandrov #if defined(CONFIG_IP_PIMSM_V1) || defined(CONFIG_IP_PIMSM_V2)
4856fef4c0cSStephen Hemminger static netdev_tx_t reg_vif_xmit(struct sk_buff *skb, struct net_device *dev)
4861da177e4SLinus Torvalds {
4874feb88e5SBenjamin Thery 	struct net *net = dev_net(dev);
488f0ad0860SPatrick McHardy 	struct mr_table *mrt;
489da91981bSDavid S. Miller 	struct flowi4 fl4 = {
490da91981bSDavid S. Miller 		.flowi4_oif	= dev->ifindex,
4916a662719SCong Wang 		.flowi4_iif	= skb->skb_iif ? : LOOPBACK_IFINDEX,
492da91981bSDavid S. Miller 		.flowi4_mark	= skb->mark,
493f0ad0860SPatrick McHardy 	};
494f0ad0860SPatrick McHardy 	int err;
495f0ad0860SPatrick McHardy 
496da91981bSDavid S. Miller 	err = ipmr_fib_lookup(net, &fl4, &mrt);
497e40dbc51SBen Greear 	if (err < 0) {
498e40dbc51SBen Greear 		kfree_skb(skb);
499f0ad0860SPatrick McHardy 		return err;
500e40dbc51SBen Greear 	}
5014feb88e5SBenjamin Thery 
5021da177e4SLinus Torvalds 	read_lock(&mrt_lock);
503cf3677aeSPavel Emelyanov 	dev->stats.tx_bytes += skb->len;
504cf3677aeSPavel Emelyanov 	dev->stats.tx_packets++;
5050c12295aSPatrick McHardy 	ipmr_cache_report(mrt, skb, mrt->mroute_reg_vif_num, IGMPMSG_WHOLEPKT);
5061da177e4SLinus Torvalds 	read_unlock(&mrt_lock);
5071da177e4SLinus Torvalds 	kfree_skb(skb);
5086ed10654SPatrick McHardy 	return NETDEV_TX_OK;
5091da177e4SLinus Torvalds }
5101da177e4SLinus Torvalds 
511ee9b9596SNicolas Dichtel static int reg_vif_get_iflink(const struct net_device *dev)
512ee9b9596SNicolas Dichtel {
513ee9b9596SNicolas Dichtel 	return 0;
514ee9b9596SNicolas Dichtel }
515ee9b9596SNicolas Dichtel 
516007c3838SStephen Hemminger static const struct net_device_ops reg_vif_netdev_ops = {
517007c3838SStephen Hemminger 	.ndo_start_xmit	= reg_vif_xmit,
518ee9b9596SNicolas Dichtel 	.ndo_get_iflink = reg_vif_get_iflink,
519007c3838SStephen Hemminger };
520007c3838SStephen Hemminger 
5211da177e4SLinus Torvalds static void reg_vif_setup(struct net_device *dev)
5221da177e4SLinus Torvalds {
5231da177e4SLinus Torvalds 	dev->type		= ARPHRD_PIMREG;
52446f25dffSKris Katterjohn 	dev->mtu		= ETH_DATA_LEN - sizeof(struct iphdr) - 8;
5251da177e4SLinus Torvalds 	dev->flags		= IFF_NOARP;
52670cb4a45SHimangi Saraogi 	dev->netdev_ops		= &reg_vif_netdev_ops;
527cf124db5SDavid S. Miller 	dev->needs_free_netdev	= true;
528403dbb97STom Goff 	dev->features		|= NETIF_F_NETNS_LOCAL;
5291da177e4SLinus Torvalds }
5301da177e4SLinus Torvalds 
531f0ad0860SPatrick McHardy static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt)
5321da177e4SLinus Torvalds {
5331da177e4SLinus Torvalds 	struct net_device *dev;
534f0ad0860SPatrick McHardy 	char name[IFNAMSIZ];
5351da177e4SLinus Torvalds 
536f0ad0860SPatrick McHardy 	if (mrt->id == RT_TABLE_DEFAULT)
537f0ad0860SPatrick McHardy 		sprintf(name, "pimreg");
538f0ad0860SPatrick McHardy 	else
539f0ad0860SPatrick McHardy 		sprintf(name, "pimreg%u", mrt->id);
540f0ad0860SPatrick McHardy 
541c835a677STom Gundersen 	dev = alloc_netdev(0, name, NET_NAME_UNKNOWN, reg_vif_setup);
5421da177e4SLinus Torvalds 
54351456b29SIan Morris 	if (!dev)
5441da177e4SLinus Torvalds 		return NULL;
5451da177e4SLinus Torvalds 
546403dbb97STom Goff 	dev_net_set(dev, net);
547403dbb97STom Goff 
5481da177e4SLinus Torvalds 	if (register_netdevice(dev)) {
5491da177e4SLinus Torvalds 		free_netdev(dev);
5501da177e4SLinus Torvalds 		return NULL;
5511da177e4SLinus Torvalds 	}
5521da177e4SLinus Torvalds 
553a0b47736SNikolay Aleksandrov 	if (!ipmr_init_vif_indev(dev))
5541da177e4SLinus Torvalds 		goto failure;
55500f54e68SPetr Machata 	if (dev_open(dev, NULL))
5561da177e4SLinus Torvalds 		goto failure;
5571da177e4SLinus Torvalds 
5587dc00c82SWang Chen 	dev_hold(dev);
5597dc00c82SWang Chen 
5601da177e4SLinus Torvalds 	return dev;
5611da177e4SLinus Torvalds 
5621da177e4SLinus Torvalds failure:
5631da177e4SLinus Torvalds 	unregister_netdevice(dev);
5641da177e4SLinus Torvalds 	return NULL;
5651da177e4SLinus Torvalds }
566c316c629SNikolay Aleksandrov 
567c316c629SNikolay Aleksandrov /* called with rcu_read_lock() */
568c316c629SNikolay Aleksandrov static int __pim_rcv(struct mr_table *mrt, struct sk_buff *skb,
569c316c629SNikolay Aleksandrov 		     unsigned int pimlen)
570c316c629SNikolay Aleksandrov {
571c316c629SNikolay Aleksandrov 	struct net_device *reg_dev = NULL;
572c316c629SNikolay Aleksandrov 	struct iphdr *encap;
573c316c629SNikolay Aleksandrov 
574c316c629SNikolay Aleksandrov 	encap = (struct iphdr *)(skb_transport_header(skb) + pimlen);
5757ef8f65dSNikolay Aleksandrov 	/* Check that:
576c316c629SNikolay Aleksandrov 	 * a. packet is really sent to a multicast group
577c316c629SNikolay Aleksandrov 	 * b. packet is not a NULL-REGISTER
578c316c629SNikolay Aleksandrov 	 * c. packet is not truncated
579c316c629SNikolay Aleksandrov 	 */
580c316c629SNikolay Aleksandrov 	if (!ipv4_is_multicast(encap->daddr) ||
581c316c629SNikolay Aleksandrov 	    encap->tot_len == 0 ||
582c316c629SNikolay Aleksandrov 	    ntohs(encap->tot_len) + pimlen > skb->len)
583c316c629SNikolay Aleksandrov 		return 1;
584c316c629SNikolay Aleksandrov 
585c316c629SNikolay Aleksandrov 	read_lock(&mrt_lock);
586c316c629SNikolay Aleksandrov 	if (mrt->mroute_reg_vif_num >= 0)
587c316c629SNikolay Aleksandrov 		reg_dev = mrt->vif_table[mrt->mroute_reg_vif_num].dev;
588c316c629SNikolay Aleksandrov 	read_unlock(&mrt_lock);
589c316c629SNikolay Aleksandrov 
590c316c629SNikolay Aleksandrov 	if (!reg_dev)
591c316c629SNikolay Aleksandrov 		return 1;
592c316c629SNikolay Aleksandrov 
593c316c629SNikolay Aleksandrov 	skb->mac_header = skb->network_header;
594c316c629SNikolay Aleksandrov 	skb_pull(skb, (u8 *)encap - skb->data);
595c316c629SNikolay Aleksandrov 	skb_reset_network_header(skb);
596c316c629SNikolay Aleksandrov 	skb->protocol = htons(ETH_P_IP);
597c316c629SNikolay Aleksandrov 	skb->ip_summed = CHECKSUM_NONE;
598c316c629SNikolay Aleksandrov 
599c316c629SNikolay Aleksandrov 	skb_tunnel_rx(skb, reg_dev, dev_net(reg_dev));
600c316c629SNikolay Aleksandrov 
601c316c629SNikolay Aleksandrov 	netif_rx(skb);
602c316c629SNikolay Aleksandrov 
603c316c629SNikolay Aleksandrov 	return NET_RX_SUCCESS;
604c316c629SNikolay Aleksandrov }
605c316c629SNikolay Aleksandrov #else
606c316c629SNikolay Aleksandrov static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt)
607c316c629SNikolay Aleksandrov {
608c316c629SNikolay Aleksandrov 	return NULL;
609c316c629SNikolay Aleksandrov }
6101da177e4SLinus Torvalds #endif
6111da177e4SLinus Torvalds 
612b362053aSYotam Gigi static int call_ipmr_vif_entry_notifiers(struct net *net,
613b362053aSYotam Gigi 					 enum fib_event_type event_type,
614b362053aSYotam Gigi 					 struct vif_device *vif,
615b362053aSYotam Gigi 					 vifi_t vif_index, u32 tb_id)
616b362053aSYotam Gigi {
617bc67a0daSYuval Mintz 	return mr_call_vif_notifiers(net, RTNL_FAMILY_IPMR, event_type,
618bc67a0daSYuval Mintz 				     vif, vif_index, tb_id,
619bc67a0daSYuval Mintz 				     &net->ipv4.ipmr_seq);
620b362053aSYotam Gigi }
621b362053aSYotam Gigi 
622b362053aSYotam Gigi static int call_ipmr_mfc_entry_notifiers(struct net *net,
623b362053aSYotam Gigi 					 enum fib_event_type event_type,
624b362053aSYotam Gigi 					 struct mfc_cache *mfc, u32 tb_id)
625b362053aSYotam Gigi {
62654c4cad9SYuval Mintz 	return mr_call_mfc_notifiers(net, RTNL_FAMILY_IPMR, event_type,
62754c4cad9SYuval Mintz 				     &mfc->_c, tb_id, &net->ipv4.ipmr_seq);
628b362053aSYotam Gigi }
629b362053aSYotam Gigi 
6302c53040fSBen Hutchings /**
6312c53040fSBen Hutchings  *	vif_delete - Delete a VIF entry
6323628e3cbSAndrew Lunn  *	@mrt: Table to delete from
6333628e3cbSAndrew Lunn  *	@vifi: VIF identifier to delete
6347dc00c82SWang Chen  *	@notify: Set to 1, if the caller is a notifier_call
6353628e3cbSAndrew Lunn  *	@head: if unregistering the VIF, place it on this queue
6361da177e4SLinus Torvalds  */
6370c12295aSPatrick McHardy static int vif_delete(struct mr_table *mrt, int vifi, int notify,
638d17fa6faSEric Dumazet 		      struct list_head *head)
6391da177e4SLinus Torvalds {
640b362053aSYotam Gigi 	struct net *net = read_pnet(&mrt->net);
6411da177e4SLinus Torvalds 	struct vif_device *v;
6421da177e4SLinus Torvalds 	struct net_device *dev;
6431da177e4SLinus Torvalds 	struct in_device *in_dev;
6441da177e4SLinus Torvalds 
6450c12295aSPatrick McHardy 	if (vifi < 0 || vifi >= mrt->maxvif)
6461da177e4SLinus Torvalds 		return -EADDRNOTAVAIL;
6471da177e4SLinus Torvalds 
6480c12295aSPatrick McHardy 	v = &mrt->vif_table[vifi];
6491da177e4SLinus Torvalds 
650b362053aSYotam Gigi 	if (VIF_EXISTS(mrt, vifi))
651b362053aSYotam Gigi 		call_ipmr_vif_entry_notifiers(net, FIB_EVENT_VIF_DEL, v, vifi,
652b362053aSYotam Gigi 					      mrt->id);
653b362053aSYotam Gigi 
6541da177e4SLinus Torvalds 	write_lock_bh(&mrt_lock);
6551da177e4SLinus Torvalds 	dev = v->dev;
6561da177e4SLinus Torvalds 	v->dev = NULL;
6571da177e4SLinus Torvalds 
6581da177e4SLinus Torvalds 	if (!dev) {
6591da177e4SLinus Torvalds 		write_unlock_bh(&mrt_lock);
6601da177e4SLinus Torvalds 		return -EADDRNOTAVAIL;
6611da177e4SLinus Torvalds 	}
6621da177e4SLinus Torvalds 
6630c12295aSPatrick McHardy 	if (vifi == mrt->mroute_reg_vif_num)
6640c12295aSPatrick McHardy 		mrt->mroute_reg_vif_num = -1;
6651da177e4SLinus Torvalds 
6660c12295aSPatrick McHardy 	if (vifi + 1 == mrt->maxvif) {
6671da177e4SLinus Torvalds 		int tmp;
668a8cb16ddSEric Dumazet 
6691da177e4SLinus Torvalds 		for (tmp = vifi - 1; tmp >= 0; tmp--) {
6700c12295aSPatrick McHardy 			if (VIF_EXISTS(mrt, tmp))
6711da177e4SLinus Torvalds 				break;
6721da177e4SLinus Torvalds 		}
6730c12295aSPatrick McHardy 		mrt->maxvif = tmp+1;
6741da177e4SLinus Torvalds 	}
6751da177e4SLinus Torvalds 
6761da177e4SLinus Torvalds 	write_unlock_bh(&mrt_lock);
6771da177e4SLinus Torvalds 
6781da177e4SLinus Torvalds 	dev_set_allmulti(dev, -1);
6791da177e4SLinus Torvalds 
680a8cb16ddSEric Dumazet 	in_dev = __in_dev_get_rtnl(dev);
681a8cb16ddSEric Dumazet 	if (in_dev) {
68242f811b8SHerbert Xu 		IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)--;
6833b022865SDavid Ahern 		inet_netconf_notify_devconf(dev_net(dev), RTM_NEWNETCONF,
684d67b8c61SNicolas Dichtel 					    NETCONFA_MC_FORWARDING,
685d67b8c61SNicolas Dichtel 					    dev->ifindex, &in_dev->cnf);
6861da177e4SLinus Torvalds 		ip_rt_multicast_event(in_dev);
6871da177e4SLinus Torvalds 	}
6881da177e4SLinus Torvalds 
6897dc00c82SWang Chen 	if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER) && !notify)
690d17fa6faSEric Dumazet 		unregister_netdevice_queue(dev, head);
6911da177e4SLinus Torvalds 
69242120a86SEric Dumazet 	dev_put_track(dev, &v->dev_tracker);
6931da177e4SLinus Torvalds 	return 0;
6941da177e4SLinus Torvalds }
6951da177e4SLinus Torvalds 
696a8c9486bSEric Dumazet static void ipmr_cache_free_rcu(struct rcu_head *head)
697a8c9486bSEric Dumazet {
698494fff56SYuval Mintz 	struct mr_mfc *c = container_of(head, struct mr_mfc, rcu);
699a8c9486bSEric Dumazet 
700494fff56SYuval Mintz 	kmem_cache_free(mrt_cachep, (struct mfc_cache *)c);
701a8c9486bSEric Dumazet }
702a8c9486bSEric Dumazet 
7038c13af2aSYuval Mintz static void ipmr_cache_free(struct mfc_cache *c)
7045c0a66f5SBenjamin Thery {
705494fff56SYuval Mintz 	call_rcu(&c->_c.rcu, ipmr_cache_free_rcu);
7065c0a66f5SBenjamin Thery }
7075c0a66f5SBenjamin Thery 
7081da177e4SLinus Torvalds /* Destroy an unresolved cache entry, killing queued skbs
709a8cb16ddSEric Dumazet  * and reporting error to netlink readers.
7101da177e4SLinus Torvalds  */
7110c12295aSPatrick McHardy static void ipmr_destroy_unres(struct mr_table *mrt, struct mfc_cache *c)
7121da177e4SLinus Torvalds {
7138de53dfbSPatrick McHardy 	struct net *net = read_pnet(&mrt->net);
7141da177e4SLinus Torvalds 	struct sk_buff *skb;
7159ef1d4c7SPatrick McHardy 	struct nlmsgerr *e;
7161da177e4SLinus Torvalds 
7170c12295aSPatrick McHardy 	atomic_dec(&mrt->cache_resolve_queue_len);
7181da177e4SLinus Torvalds 
719494fff56SYuval Mintz 	while ((skb = skb_dequeue(&c->_c.mfc_un.unres.unresolved))) {
720eddc9ec5SArnaldo Carvalho de Melo 		if (ip_hdr(skb)->version == 0) {
721af72868bSJohannes Berg 			struct nlmsghdr *nlh = skb_pull(skb,
722af72868bSJohannes Berg 							sizeof(struct iphdr));
7231da177e4SLinus Torvalds 			nlh->nlmsg_type = NLMSG_ERROR;
724573ce260SHong zhi guo 			nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr));
7251da177e4SLinus Torvalds 			skb_trim(skb, nlh->nlmsg_len);
726573ce260SHong zhi guo 			e = nlmsg_data(nlh);
7279ef1d4c7SPatrick McHardy 			e->error = -ETIMEDOUT;
7289ef1d4c7SPatrick McHardy 			memset(&e->msg, 0, sizeof(e->msg));
7292942e900SThomas Graf 
73015e47304SEric W. Biederman 			rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
731a8cb16ddSEric Dumazet 		} else {
7321da177e4SLinus Torvalds 			kfree_skb(skb);
7331da177e4SLinus Torvalds 		}
734a8cb16ddSEric Dumazet 	}
7351da177e4SLinus Torvalds 
7365c0a66f5SBenjamin Thery 	ipmr_cache_free(c);
7371da177e4SLinus Torvalds }
7381da177e4SLinus Torvalds 
739e258beb2SPatrick McHardy /* Timer process for the unresolved queue. */
740e99e88a9SKees Cook static void ipmr_expire_process(struct timer_list *t)
7411da177e4SLinus Torvalds {
742e99e88a9SKees Cook 	struct mr_table *mrt = from_timer(mrt, t, ipmr_expire_timer);
743494fff56SYuval Mintz 	struct mr_mfc *c, *next;
7441da177e4SLinus Torvalds 	unsigned long expires;
745494fff56SYuval Mintz 	unsigned long now;
7461da177e4SLinus Torvalds 
7471da177e4SLinus Torvalds 	if (!spin_trylock(&mfc_unres_lock)) {
7480c12295aSPatrick McHardy 		mod_timer(&mrt->ipmr_expire_timer, jiffies+HZ/10);
7491da177e4SLinus Torvalds 		return;
7501da177e4SLinus Torvalds 	}
7511da177e4SLinus Torvalds 
7520c12295aSPatrick McHardy 	if (list_empty(&mrt->mfc_unres_queue))
7531da177e4SLinus Torvalds 		goto out;
7541da177e4SLinus Torvalds 
7551da177e4SLinus Torvalds 	now = jiffies;
7561da177e4SLinus Torvalds 	expires = 10*HZ;
7571da177e4SLinus Torvalds 
7580c12295aSPatrick McHardy 	list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
7591da177e4SLinus Torvalds 		if (time_after(c->mfc_un.unres.expires, now)) {
7601da177e4SLinus Torvalds 			unsigned long interval = c->mfc_un.unres.expires - now;
7611da177e4SLinus Torvalds 			if (interval < expires)
7621da177e4SLinus Torvalds 				expires = interval;
7631da177e4SLinus Torvalds 			continue;
7641da177e4SLinus Torvalds 		}
7651da177e4SLinus Torvalds 
766862465f2SPatrick McHardy 		list_del(&c->list);
767494fff56SYuval Mintz 		mroute_netlink_event(mrt, (struct mfc_cache *)c, RTM_DELROUTE);
768494fff56SYuval Mintz 		ipmr_destroy_unres(mrt, (struct mfc_cache *)c);
7691da177e4SLinus Torvalds 	}
7701da177e4SLinus Torvalds 
7710c12295aSPatrick McHardy 	if (!list_empty(&mrt->mfc_unres_queue))
7720c12295aSPatrick McHardy 		mod_timer(&mrt->ipmr_expire_timer, jiffies + expires);
7731da177e4SLinus Torvalds 
7741da177e4SLinus Torvalds out:
7751da177e4SLinus Torvalds 	spin_unlock(&mfc_unres_lock);
7761da177e4SLinus Torvalds }
7771da177e4SLinus Torvalds 
7781da177e4SLinus Torvalds /* Fill oifs list. It is called under write locked mrt_lock. */
779494fff56SYuval Mintz static void ipmr_update_thresholds(struct mr_table *mrt, struct mr_mfc *cache,
780d658f8a0SPatrick McHardy 				   unsigned char *ttls)
7811da177e4SLinus Torvalds {
7821da177e4SLinus Torvalds 	int vifi;
7831da177e4SLinus Torvalds 
7841da177e4SLinus Torvalds 	cache->mfc_un.res.minvif = MAXVIFS;
7851da177e4SLinus Torvalds 	cache->mfc_un.res.maxvif = 0;
7861da177e4SLinus Torvalds 	memset(cache->mfc_un.res.ttls, 255, MAXVIFS);
7871da177e4SLinus Torvalds 
7880c12295aSPatrick McHardy 	for (vifi = 0; vifi < mrt->maxvif; vifi++) {
7890c12295aSPatrick McHardy 		if (VIF_EXISTS(mrt, vifi) &&
790cf958ae3SBenjamin Thery 		    ttls[vifi] && ttls[vifi] < 255) {
7911da177e4SLinus Torvalds 			cache->mfc_un.res.ttls[vifi] = ttls[vifi];
7921da177e4SLinus Torvalds 			if (cache->mfc_un.res.minvif > vifi)
7931da177e4SLinus Torvalds 				cache->mfc_un.res.minvif = vifi;
7941da177e4SLinus Torvalds 			if (cache->mfc_un.res.maxvif <= vifi)
7951da177e4SLinus Torvalds 				cache->mfc_un.res.maxvif = vifi + 1;
7961da177e4SLinus Torvalds 		}
7971da177e4SLinus Torvalds 	}
79890b5ca17SNikolay Aleksandrov 	cache->mfc_un.res.lastuse = jiffies;
7991da177e4SLinus Torvalds }
8001da177e4SLinus Torvalds 
8010c12295aSPatrick McHardy static int vif_add(struct net *net, struct mr_table *mrt,
8020c12295aSPatrick McHardy 		   struct vifctl *vifc, int mrtsock)
8031da177e4SLinus Torvalds {
804bccb3025SFlorian Fainelli 	struct netdev_phys_item_id ppid = { };
8051da177e4SLinus Torvalds 	int vifi = vifc->vifc_vifi;
8060c12295aSPatrick McHardy 	struct vif_device *v = &mrt->vif_table[vifi];
8071da177e4SLinus Torvalds 	struct net_device *dev;
8081da177e4SLinus Torvalds 	struct in_device *in_dev;
809d607032dSWang Chen 	int err;
8101da177e4SLinus Torvalds 
8111da177e4SLinus Torvalds 	/* Is vif busy ? */
8120c12295aSPatrick McHardy 	if (VIF_EXISTS(mrt, vifi))
8131da177e4SLinus Torvalds 		return -EADDRINUSE;
8141da177e4SLinus Torvalds 
8151da177e4SLinus Torvalds 	switch (vifc->vifc_flags) {
8161da177e4SLinus Torvalds 	case VIFF_REGISTER:
8171973a4eaSNikolay Aleksandrov 		if (!ipmr_pimsm_enabled())
818c316c629SNikolay Aleksandrov 			return -EINVAL;
819c316c629SNikolay Aleksandrov 		/* Special Purpose VIF in PIM
8201da177e4SLinus Torvalds 		 * All the packets will be sent to the daemon
8211da177e4SLinus Torvalds 		 */
8220c12295aSPatrick McHardy 		if (mrt->mroute_reg_vif_num >= 0)
8231da177e4SLinus Torvalds 			return -EADDRINUSE;
824f0ad0860SPatrick McHardy 		dev = ipmr_reg_vif(net, mrt);
8251da177e4SLinus Torvalds 		if (!dev)
8261da177e4SLinus Torvalds 			return -ENOBUFS;
827d607032dSWang Chen 		err = dev_set_allmulti(dev, 1);
828d607032dSWang Chen 		if (err) {
829d607032dSWang Chen 			unregister_netdevice(dev);
8307dc00c82SWang Chen 			dev_put(dev);
831d607032dSWang Chen 			return err;
832d607032dSWang Chen 		}
8331da177e4SLinus Torvalds 		break;
8341da177e4SLinus Torvalds 	case VIFF_TUNNEL:
8354feb88e5SBenjamin Thery 		dev = ipmr_new_tunnel(net, vifc);
836c1fd1182SChristoph Hellwig 		if (IS_ERR(dev))
837c1fd1182SChristoph Hellwig 			return PTR_ERR(dev);
8381da177e4SLinus Torvalds 		break;
839ee5e81f0SIlia K 	case VIFF_USE_IFINDEX:
8401da177e4SLinus Torvalds 	case 0:
841ee5e81f0SIlia K 		if (vifc->vifc_flags == VIFF_USE_IFINDEX) {
842ee5e81f0SIlia K 			dev = dev_get_by_index(net, vifc->vifc_lcl_ifindex);
84351456b29SIan Morris 			if (dev && !__in_dev_get_rtnl(dev)) {
844ee5e81f0SIlia K 				dev_put(dev);
845ee5e81f0SIlia K 				return -EADDRNOTAVAIL;
846ee5e81f0SIlia K 			}
847a8cb16ddSEric Dumazet 		} else {
8484feb88e5SBenjamin Thery 			dev = ip_dev_find(net, vifc->vifc_lcl_addr.s_addr);
849a8cb16ddSEric Dumazet 		}
8501da177e4SLinus Torvalds 		if (!dev)
8511da177e4SLinus Torvalds 			return -EADDRNOTAVAIL;
852d607032dSWang Chen 		err = dev_set_allmulti(dev, 1);
8537dc00c82SWang Chen 		if (err) {
8547dc00c82SWang Chen 			dev_put(dev);
855d607032dSWang Chen 			return err;
8567dc00c82SWang Chen 		}
8571da177e4SLinus Torvalds 		break;
8581da177e4SLinus Torvalds 	default:
8591da177e4SLinus Torvalds 		return -EINVAL;
8601da177e4SLinus Torvalds 	}
8611da177e4SLinus Torvalds 
862a8cb16ddSEric Dumazet 	in_dev = __in_dev_get_rtnl(dev);
863a8cb16ddSEric Dumazet 	if (!in_dev) {
864d0490cfdSDan Carpenter 		dev_put(dev);
8651da177e4SLinus Torvalds 		return -EADDRNOTAVAIL;
866d0490cfdSDan Carpenter 	}
86742f811b8SHerbert Xu 	IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)++;
8683b022865SDavid Ahern 	inet_netconf_notify_devconf(net, RTM_NEWNETCONF, NETCONFA_MC_FORWARDING,
8693b022865SDavid Ahern 				    dev->ifindex, &in_dev->cnf);
8701da177e4SLinus Torvalds 	ip_rt_multicast_event(in_dev);
8711da177e4SLinus Torvalds 
872a8cb16ddSEric Dumazet 	/* Fill in the VIF structures */
8736853f21fSYuval Mintz 	vif_device_init(v, dev, vifc->vifc_rate_limit,
8746853f21fSYuval Mintz 			vifc->vifc_threshold,
8756853f21fSYuval Mintz 			vifc->vifc_flags | (!mrtsock ? VIFF_STATIC : 0),
8766853f21fSYuval Mintz 			(VIFF_TUNNEL | VIFF_REGISTER));
877a8cb16ddSEric Dumazet 
878bccb3025SFlorian Fainelli 	err = dev_get_port_parent_id(dev, &ppid, true);
879bccb3025SFlorian Fainelli 	if (err == 0) {
880bccb3025SFlorian Fainelli 		memcpy(v->dev_parent_id.id, ppid.id, ppid.id_len);
881bccb3025SFlorian Fainelli 		v->dev_parent_id.id_len = ppid.id_len;
8825d8b3e69SYotam Gigi 	} else {
8835d8b3e69SYotam Gigi 		v->dev_parent_id.id_len = 0;
8845d8b3e69SYotam Gigi 	}
8856853f21fSYuval Mintz 
8861da177e4SLinus Torvalds 	v->local = vifc->vifc_lcl_addr.s_addr;
8871da177e4SLinus Torvalds 	v->remote = vifc->vifc_rmt_addr.s_addr;
8881da177e4SLinus Torvalds 
8891da177e4SLinus Torvalds 	/* And finish update writing critical data */
8901da177e4SLinus Torvalds 	write_lock_bh(&mrt_lock);
8911da177e4SLinus Torvalds 	v->dev = dev;
89242120a86SEric Dumazet 	netdev_tracker_alloc(dev, &v->dev_tracker, GFP_ATOMIC);
8931da177e4SLinus Torvalds 	if (v->flags & VIFF_REGISTER)
8940c12295aSPatrick McHardy 		mrt->mroute_reg_vif_num = vifi;
8950c12295aSPatrick McHardy 	if (vifi+1 > mrt->maxvif)
8960c12295aSPatrick McHardy 		mrt->maxvif = vifi+1;
8971da177e4SLinus Torvalds 	write_unlock_bh(&mrt_lock);
898b362053aSYotam Gigi 	call_ipmr_vif_entry_notifiers(net, FIB_EVENT_VIF_ADD, v, vifi, mrt->id);
8991da177e4SLinus Torvalds 	return 0;
9001da177e4SLinus Torvalds }
9011da177e4SLinus Torvalds 
902a8c9486bSEric Dumazet /* called with rcu_read_lock() */
9030c12295aSPatrick McHardy static struct mfc_cache *ipmr_cache_find(struct mr_table *mrt,
9044feb88e5SBenjamin Thery 					 __be32 origin,
9054feb88e5SBenjamin Thery 					 __be32 mcastgrp)
9061da177e4SLinus Torvalds {
9078fb472c0SNikolay Aleksandrov 	struct mfc_cache_cmp_arg arg = {
9088fb472c0SNikolay Aleksandrov 			.mfc_mcastgrp = mcastgrp,
9098fb472c0SNikolay Aleksandrov 			.mfc_origin = origin
9108fb472c0SNikolay Aleksandrov 	};
9111da177e4SLinus Torvalds 
912845c9a7aSYuval Mintz 	return mr_mfc_find(mrt, &arg);
913660b26dcSNicolas Dichtel }
914660b26dcSNicolas Dichtel 
915660b26dcSNicolas Dichtel /* Look for a (*,G) entry */
916660b26dcSNicolas Dichtel static struct mfc_cache *ipmr_cache_find_any(struct mr_table *mrt,
917660b26dcSNicolas Dichtel 					     __be32 mcastgrp, int vifi)
918660b26dcSNicolas Dichtel {
9198fb472c0SNikolay Aleksandrov 	struct mfc_cache_cmp_arg arg = {
9208fb472c0SNikolay Aleksandrov 			.mfc_mcastgrp = mcastgrp,
9218fb472c0SNikolay Aleksandrov 			.mfc_origin = htonl(INADDR_ANY)
9228fb472c0SNikolay Aleksandrov 	};
923660b26dcSNicolas Dichtel 
924360eb5daSNicolas Dichtel 	if (mcastgrp == htonl(INADDR_ANY))
925845c9a7aSYuval Mintz 		return mr_mfc_find_any_parent(mrt, vifi);
926845c9a7aSYuval Mintz 	return mr_mfc_find_any(mrt, vifi, &arg);
927660b26dcSNicolas Dichtel }
928660b26dcSNicolas Dichtel 
9298fb472c0SNikolay Aleksandrov /* Look for a (S,G,iif) entry if parent != -1 */
9308fb472c0SNikolay Aleksandrov static struct mfc_cache *ipmr_cache_find_parent(struct mr_table *mrt,
9318fb472c0SNikolay Aleksandrov 						__be32 origin, __be32 mcastgrp,
9328fb472c0SNikolay Aleksandrov 						int parent)
9338fb472c0SNikolay Aleksandrov {
9348fb472c0SNikolay Aleksandrov 	struct mfc_cache_cmp_arg arg = {
9358fb472c0SNikolay Aleksandrov 			.mfc_mcastgrp = mcastgrp,
9368fb472c0SNikolay Aleksandrov 			.mfc_origin = origin,
9378fb472c0SNikolay Aleksandrov 	};
9388fb472c0SNikolay Aleksandrov 
939845c9a7aSYuval Mintz 	return mr_mfc_find_parent(mrt, &arg, parent);
9408fb472c0SNikolay Aleksandrov }
9418fb472c0SNikolay Aleksandrov 
9427ef8f65dSNikolay Aleksandrov /* Allocate a multicast cache entry */
943d658f8a0SPatrick McHardy static struct mfc_cache *ipmr_cache_alloc(void)
9441da177e4SLinus Torvalds {
945c3762229SRobert P. J. Day 	struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_KERNEL);
946a8c9486bSEric Dumazet 
94770a0dec4STom Goff 	if (c) {
948494fff56SYuval Mintz 		c->_c.mfc_un.res.last_assert = jiffies - MFC_ASSERT_THRESH - 1;
949494fff56SYuval Mintz 		c->_c.mfc_un.res.minvif = MAXVIFS;
9508c13af2aSYuval Mintz 		c->_c.free = ipmr_cache_free_rcu;
951494fff56SYuval Mintz 		refcount_set(&c->_c.mfc_un.res.refcount, 1);
95270a0dec4STom Goff 	}
9531da177e4SLinus Torvalds 	return c;
9541da177e4SLinus Torvalds }
9551da177e4SLinus Torvalds 
956d658f8a0SPatrick McHardy static struct mfc_cache *ipmr_cache_alloc_unres(void)
9571da177e4SLinus Torvalds {
958c3762229SRobert P. J. Day 	struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_ATOMIC);
959a8c9486bSEric Dumazet 
960a8c9486bSEric Dumazet 	if (c) {
961494fff56SYuval Mintz 		skb_queue_head_init(&c->_c.mfc_un.unres.unresolved);
962494fff56SYuval Mintz 		c->_c.mfc_un.unres.expires = jiffies + 10 * HZ;
963a8c9486bSEric Dumazet 	}
9641da177e4SLinus Torvalds 	return c;
9651da177e4SLinus Torvalds }
9661da177e4SLinus Torvalds 
9677ef8f65dSNikolay Aleksandrov /* A cache entry has gone into a resolved state from queued */
9680c12295aSPatrick McHardy static void ipmr_cache_resolve(struct net *net, struct mr_table *mrt,
9690c12295aSPatrick McHardy 			       struct mfc_cache *uc, struct mfc_cache *c)
9701da177e4SLinus Torvalds {
9711da177e4SLinus Torvalds 	struct sk_buff *skb;
9729ef1d4c7SPatrick McHardy 	struct nlmsgerr *e;
9731da177e4SLinus Torvalds 
974a8cb16ddSEric Dumazet 	/* Play the pending entries through our router */
975494fff56SYuval Mintz 	while ((skb = __skb_dequeue(&uc->_c.mfc_un.unres.unresolved))) {
976eddc9ec5SArnaldo Carvalho de Melo 		if (ip_hdr(skb)->version == 0) {
977af72868bSJohannes Berg 			struct nlmsghdr *nlh = skb_pull(skb,
978af72868bSJohannes Berg 							sizeof(struct iphdr));
9791da177e4SLinus Torvalds 
9807b0db857SYuval Mintz 			if (mr_fill_mroute(mrt, skb, &c->_c,
981494fff56SYuval Mintz 					   nlmsg_data(nlh)) > 0) {
982a8cb16ddSEric Dumazet 				nlh->nlmsg_len = skb_tail_pointer(skb) -
983a8cb16ddSEric Dumazet 						 (u8 *)nlh;
9841da177e4SLinus Torvalds 			} else {
9851da177e4SLinus Torvalds 				nlh->nlmsg_type = NLMSG_ERROR;
986573ce260SHong zhi guo 				nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr));
9871da177e4SLinus Torvalds 				skb_trim(skb, nlh->nlmsg_len);
988573ce260SHong zhi guo 				e = nlmsg_data(nlh);
9899ef1d4c7SPatrick McHardy 				e->error = -EMSGSIZE;
9909ef1d4c7SPatrick McHardy 				memset(&e->msg, 0, sizeof(e->msg));
9911da177e4SLinus Torvalds 			}
9922942e900SThomas Graf 
99315e47304SEric W. Biederman 			rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
994a8cb16ddSEric Dumazet 		} else {
9954b1f0d33SDonald Sharp 			ip_mr_forward(net, mrt, skb->dev, skb, c, 0);
9961da177e4SLinus Torvalds 		}
9971da177e4SLinus Torvalds 	}
998a8cb16ddSEric Dumazet }
9991da177e4SLinus Torvalds 
10005a645dd8SJulien Gomes /* Bounce a cache query up to mrouted and netlink.
10011da177e4SLinus Torvalds  *
10021da177e4SLinus Torvalds  * Called under mrt_lock.
10031da177e4SLinus Torvalds  */
10040c12295aSPatrick McHardy static int ipmr_cache_report(struct mr_table *mrt,
10054feb88e5SBenjamin Thery 			     struct sk_buff *pkt, vifi_t vifi, int assert)
10061da177e4SLinus Torvalds {
1007c9bdd4b5SArnaldo Carvalho de Melo 	const int ihl = ip_hdrlen(pkt);
1008c316c629SNikolay Aleksandrov 	struct sock *mroute_sk;
10091da177e4SLinus Torvalds 	struct igmphdr *igmp;
10101da177e4SLinus Torvalds 	struct igmpmsg *msg;
1011c316c629SNikolay Aleksandrov 	struct sk_buff *skb;
10121da177e4SLinus Torvalds 	int ret;
10131da177e4SLinus Torvalds 
1014c921c207SNikolay Aleksandrov 	if (assert == IGMPMSG_WHOLEPKT || assert == IGMPMSG_WRVIFWHOLE)
10151da177e4SLinus Torvalds 		skb = skb_realloc_headroom(pkt, sizeof(struct iphdr));
10161da177e4SLinus Torvalds 	else
10171da177e4SLinus Torvalds 		skb = alloc_skb(128, GFP_ATOMIC);
10181da177e4SLinus Torvalds 
10191da177e4SLinus Torvalds 	if (!skb)
10201da177e4SLinus Torvalds 		return -ENOBUFS;
10211da177e4SLinus Torvalds 
1022c921c207SNikolay Aleksandrov 	if (assert == IGMPMSG_WHOLEPKT || assert == IGMPMSG_WRVIFWHOLE) {
10231da177e4SLinus Torvalds 		/* Ugly, but we have no choice with this interface.
1024a8cb16ddSEric Dumazet 		 * Duplicate old header, fix ihl, length etc.
1025a8cb16ddSEric Dumazet 		 * And all this only to mangle msg->im_msgtype and
1026a8cb16ddSEric Dumazet 		 * to set msg->im_mbz to "mbz" :-)
10271da177e4SLinus Torvalds 		 */
1028878c8145SArnaldo Carvalho de Melo 		skb_push(skb, sizeof(struct iphdr));
1029878c8145SArnaldo Carvalho de Melo 		skb_reset_network_header(skb);
1030badff6d0SArnaldo Carvalho de Melo 		skb_reset_transport_header(skb);
10310272ffc4SArnaldo Carvalho de Melo 		msg = (struct igmpmsg *)skb_network_header(skb);
1032d56f90a7SArnaldo Carvalho de Melo 		memcpy(msg, skb_network_header(pkt), sizeof(struct iphdr));
1033c921c207SNikolay Aleksandrov 		msg->im_msgtype = assert;
10341da177e4SLinus Torvalds 		msg->im_mbz = 0;
1035c8715a8eSPaul Davey 		if (assert == IGMPMSG_WRVIFWHOLE) {
1036c921c207SNikolay Aleksandrov 			msg->im_vif = vifi;
1037c8715a8eSPaul Davey 			msg->im_vif_hi = vifi >> 8;
1038c8715a8eSPaul Davey 		} else {
10390c12295aSPatrick McHardy 			msg->im_vif = mrt->mroute_reg_vif_num;
1040c8715a8eSPaul Davey 			msg->im_vif_hi = mrt->mroute_reg_vif_num >> 8;
1041c8715a8eSPaul Davey 		}
1042eddc9ec5SArnaldo Carvalho de Melo 		ip_hdr(skb)->ihl = sizeof(struct iphdr) >> 2;
1043eddc9ec5SArnaldo Carvalho de Melo 		ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(pkt)->tot_len) +
1044eddc9ec5SArnaldo Carvalho de Melo 					     sizeof(struct iphdr));
1045c316c629SNikolay Aleksandrov 	} else {
1046a8cb16ddSEric Dumazet 		/* Copy the IP header */
104730f3a40fSCong Wang 		skb_set_network_header(skb, skb->len);
1048ddc7b8e3SArnaldo Carvalho de Melo 		skb_put(skb, ihl);
104927d7ff46SArnaldo Carvalho de Melo 		skb_copy_to_linear_data(skb, pkt->data, ihl);
1050c316c629SNikolay Aleksandrov 		/* Flag to the kernel this is a route add */
1051c316c629SNikolay Aleksandrov 		ip_hdr(skb)->protocol = 0;
1052eddc9ec5SArnaldo Carvalho de Melo 		msg = (struct igmpmsg *)skb_network_header(skb);
10531da177e4SLinus Torvalds 		msg->im_vif = vifi;
1054c8715a8eSPaul Davey 		msg->im_vif_hi = vifi >> 8;
1055adf30907SEric Dumazet 		skb_dst_set(skb, dst_clone(skb_dst(pkt)));
1056a8cb16ddSEric Dumazet 		/* Add our header */
10574df864c1SJohannes Berg 		igmp = skb_put(skb, sizeof(struct igmphdr));
1058c316c629SNikolay Aleksandrov 		igmp->type = assert;
10591da177e4SLinus Torvalds 		msg->im_msgtype = assert;
10601da177e4SLinus Torvalds 		igmp->code = 0;
1061eddc9ec5SArnaldo Carvalho de Melo 		ip_hdr(skb)->tot_len = htons(skb->len);	/* Fix the length */
1062b0e380b1SArnaldo Carvalho de Melo 		skb->transport_header = skb->network_header;
10631da177e4SLinus Torvalds 	}
10641da177e4SLinus Torvalds 
10654c968709SEric Dumazet 	rcu_read_lock();
10664c968709SEric Dumazet 	mroute_sk = rcu_dereference(mrt->mroute_sk);
106751456b29SIan Morris 	if (!mroute_sk) {
10684c968709SEric Dumazet 		rcu_read_unlock();
10691da177e4SLinus Torvalds 		kfree_skb(skb);
10701da177e4SLinus Torvalds 		return -EINVAL;
10711da177e4SLinus Torvalds 	}
10721da177e4SLinus Torvalds 
10735a645dd8SJulien Gomes 	igmpmsg_netlink_event(mrt, skb);
10745a645dd8SJulien Gomes 
1075a8cb16ddSEric Dumazet 	/* Deliver to mrouted */
10764c968709SEric Dumazet 	ret = sock_queue_rcv_skb(mroute_sk, skb);
10774c968709SEric Dumazet 	rcu_read_unlock();
107870a269e6SBenjamin Thery 	if (ret < 0) {
1079e87cc472SJoe Perches 		net_warn_ratelimited("mroute: pending queue full, dropping entries\n");
10801da177e4SLinus Torvalds 		kfree_skb(skb);
10811da177e4SLinus Torvalds 	}
10821da177e4SLinus Torvalds 
10831da177e4SLinus Torvalds 	return ret;
10841da177e4SLinus Torvalds }
10851da177e4SLinus Torvalds 
10867ef8f65dSNikolay Aleksandrov /* Queue a packet for resolution. It gets locked cache entry! */
10877ef8f65dSNikolay Aleksandrov static int ipmr_cache_unresolved(struct mr_table *mrt, vifi_t vifi,
10884b1f0d33SDonald Sharp 				 struct sk_buff *skb, struct net_device *dev)
10891da177e4SLinus Torvalds {
10908fb472c0SNikolay Aleksandrov 	const struct iphdr *iph = ip_hdr(skb);
10918fb472c0SNikolay Aleksandrov 	struct mfc_cache *c;
1092862465f2SPatrick McHardy 	bool found = false;
10931da177e4SLinus Torvalds 	int err;
10941da177e4SLinus Torvalds 
10951da177e4SLinus Torvalds 	spin_lock_bh(&mfc_unres_lock);
1096494fff56SYuval Mintz 	list_for_each_entry(c, &mrt->mfc_unres_queue, _c.list) {
1097e258beb2SPatrick McHardy 		if (c->mfc_mcastgrp == iph->daddr &&
1098862465f2SPatrick McHardy 		    c->mfc_origin == iph->saddr) {
1099862465f2SPatrick McHardy 			found = true;
11001da177e4SLinus Torvalds 			break;
11011da177e4SLinus Torvalds 		}
1102862465f2SPatrick McHardy 	}
11031da177e4SLinus Torvalds 
1104862465f2SPatrick McHardy 	if (!found) {
1105a8cb16ddSEric Dumazet 		/* Create a new entry if allowable */
11060079ad8eSHangbin Liu 		c = ipmr_cache_alloc_unres();
11070079ad8eSHangbin Liu 		if (!c) {
11081da177e4SLinus Torvalds 			spin_unlock_bh(&mfc_unres_lock);
11091da177e4SLinus Torvalds 
11101da177e4SLinus Torvalds 			kfree_skb(skb);
11111da177e4SLinus Torvalds 			return -ENOBUFS;
11121da177e4SLinus Torvalds 		}
11131da177e4SLinus Torvalds 
1114a8cb16ddSEric Dumazet 		/* Fill in the new cache entry */
1115494fff56SYuval Mintz 		c->_c.mfc_parent = -1;
1116eddc9ec5SArnaldo Carvalho de Melo 		c->mfc_origin	= iph->saddr;
1117eddc9ec5SArnaldo Carvalho de Melo 		c->mfc_mcastgrp	= iph->daddr;
11181da177e4SLinus Torvalds 
1119a8cb16ddSEric Dumazet 		/* Reflect first query at mrouted. */
11200c12295aSPatrick McHardy 		err = ipmr_cache_report(mrt, skb, vifi, IGMPMSG_NOCACHE);
1121494fff56SYuval Mintz 
11224feb88e5SBenjamin Thery 		if (err < 0) {
11231da177e4SLinus Torvalds 			/* If the report failed throw the cache entry
11241da177e4SLinus Torvalds 			   out - Brad Parker
11251da177e4SLinus Torvalds 			 */
11261da177e4SLinus Torvalds 			spin_unlock_bh(&mfc_unres_lock);
11271da177e4SLinus Torvalds 
11285c0a66f5SBenjamin Thery 			ipmr_cache_free(c);
11291da177e4SLinus Torvalds 			kfree_skb(skb);
11301da177e4SLinus Torvalds 			return err;
11311da177e4SLinus Torvalds 		}
11321da177e4SLinus Torvalds 
11330c12295aSPatrick McHardy 		atomic_inc(&mrt->cache_resolve_queue_len);
1134494fff56SYuval Mintz 		list_add(&c->_c.list, &mrt->mfc_unres_queue);
11358cd3ac9fSNicolas Dichtel 		mroute_netlink_event(mrt, c, RTM_NEWROUTE);
11361da177e4SLinus Torvalds 
1137278554bdSDavid S. Miller 		if (atomic_read(&mrt->cache_resolve_queue_len) == 1)
1138494fff56SYuval Mintz 			mod_timer(&mrt->ipmr_expire_timer,
1139494fff56SYuval Mintz 				  c->_c.mfc_un.unres.expires);
11401da177e4SLinus Torvalds 	}
11411da177e4SLinus Torvalds 
1142a8cb16ddSEric Dumazet 	/* See if we can append the packet */
1143494fff56SYuval Mintz 	if (c->_c.mfc_un.unres.unresolved.qlen > 3) {
11441da177e4SLinus Torvalds 		kfree_skb(skb);
11451da177e4SLinus Torvalds 		err = -ENOBUFS;
11461da177e4SLinus Torvalds 	} else {
11474b1f0d33SDonald Sharp 		if (dev) {
11484b1f0d33SDonald Sharp 			skb->dev = dev;
11494b1f0d33SDonald Sharp 			skb->skb_iif = dev->ifindex;
11504b1f0d33SDonald Sharp 		}
1151494fff56SYuval Mintz 		skb_queue_tail(&c->_c.mfc_un.unres.unresolved, skb);
11521da177e4SLinus Torvalds 		err = 0;
11531da177e4SLinus Torvalds 	}
11541da177e4SLinus Torvalds 
11551da177e4SLinus Torvalds 	spin_unlock_bh(&mfc_unres_lock);
11561da177e4SLinus Torvalds 	return err;
11571da177e4SLinus Torvalds }
11581da177e4SLinus Torvalds 
11597ef8f65dSNikolay Aleksandrov /* MFC cache manipulation by user space mroute daemon */
11601da177e4SLinus Torvalds 
1161660b26dcSNicolas Dichtel static int ipmr_mfc_delete(struct mr_table *mrt, struct mfcctl *mfc, int parent)
11621da177e4SLinus Torvalds {
1163b362053aSYotam Gigi 	struct net *net = read_pnet(&mrt->net);
11648fb472c0SNikolay Aleksandrov 	struct mfc_cache *c;
11651da177e4SLinus Torvalds 
11668fb472c0SNikolay Aleksandrov 	/* The entries are added/deleted only under RTNL */
11678fb472c0SNikolay Aleksandrov 	rcu_read_lock();
11688fb472c0SNikolay Aleksandrov 	c = ipmr_cache_find_parent(mrt, mfc->mfcc_origin.s_addr,
11698fb472c0SNikolay Aleksandrov 				   mfc->mfcc_mcastgrp.s_addr, parent);
11708fb472c0SNikolay Aleksandrov 	rcu_read_unlock();
11718fb472c0SNikolay Aleksandrov 	if (!c)
11728fb472c0SNikolay Aleksandrov 		return -ENOENT;
1173494fff56SYuval Mintz 	rhltable_remove(&mrt->mfc_hash, &c->_c.mnode, ipmr_rht_params);
1174494fff56SYuval Mintz 	list_del_rcu(&c->_c.list);
1175b362053aSYotam Gigi 	call_ipmr_mfc_entry_notifiers(net, FIB_EVENT_ENTRY_DEL, c, mrt->id);
11768cd3ac9fSNicolas Dichtel 	mroute_netlink_event(mrt, c, RTM_DELROUTE);
11778c13af2aSYuval Mintz 	mr_cache_put(&c->_c);
11788fb472c0SNikolay Aleksandrov 
11791da177e4SLinus Torvalds 	return 0;
11801da177e4SLinus Torvalds }
11811da177e4SLinus Torvalds 
11820c12295aSPatrick McHardy static int ipmr_mfc_add(struct net *net, struct mr_table *mrt,
1183660b26dcSNicolas Dichtel 			struct mfcctl *mfc, int mrtsock, int parent)
11841da177e4SLinus Torvalds {
1185862465f2SPatrick McHardy 	struct mfc_cache *uc, *c;
1186494fff56SYuval Mintz 	struct mr_mfc *_uc;
11878fb472c0SNikolay Aleksandrov 	bool found;
11888fb472c0SNikolay Aleksandrov 	int ret;
11891da177e4SLinus Torvalds 
1190a50436f2SPatrick McHardy 	if (mfc->mfcc_parent >= MAXVIFS)
1191a50436f2SPatrick McHardy 		return -ENFILE;
1192a50436f2SPatrick McHardy 
11938fb472c0SNikolay Aleksandrov 	/* The entries are added/deleted only under RTNL */
11948fb472c0SNikolay Aleksandrov 	rcu_read_lock();
11958fb472c0SNikolay Aleksandrov 	c = ipmr_cache_find_parent(mrt, mfc->mfcc_origin.s_addr,
11968fb472c0SNikolay Aleksandrov 				   mfc->mfcc_mcastgrp.s_addr, parent);
11978fb472c0SNikolay Aleksandrov 	rcu_read_unlock();
11988fb472c0SNikolay Aleksandrov 	if (c) {
11991da177e4SLinus Torvalds 		write_lock_bh(&mrt_lock);
1200494fff56SYuval Mintz 		c->_c.mfc_parent = mfc->mfcc_parent;
1201494fff56SYuval Mintz 		ipmr_update_thresholds(mrt, &c->_c, mfc->mfcc_ttls);
12021da177e4SLinus Torvalds 		if (!mrtsock)
1203494fff56SYuval Mintz 			c->_c.mfc_flags |= MFC_STATIC;
12041da177e4SLinus Torvalds 		write_unlock_bh(&mrt_lock);
1205b362053aSYotam Gigi 		call_ipmr_mfc_entry_notifiers(net, FIB_EVENT_ENTRY_REPLACE, c,
1206b362053aSYotam Gigi 					      mrt->id);
12078cd3ac9fSNicolas Dichtel 		mroute_netlink_event(mrt, c, RTM_NEWROUTE);
12081da177e4SLinus Torvalds 		return 0;
12091da177e4SLinus Torvalds 	}
12101da177e4SLinus Torvalds 
1211360eb5daSNicolas Dichtel 	if (mfc->mfcc_mcastgrp.s_addr != htonl(INADDR_ANY) &&
1212660b26dcSNicolas Dichtel 	    !ipv4_is_multicast(mfc->mfcc_mcastgrp.s_addr))
12131da177e4SLinus Torvalds 		return -EINVAL;
12141da177e4SLinus Torvalds 
1215d658f8a0SPatrick McHardy 	c = ipmr_cache_alloc();
121651456b29SIan Morris 	if (!c)
12171da177e4SLinus Torvalds 		return -ENOMEM;
12181da177e4SLinus Torvalds 
12191da177e4SLinus Torvalds 	c->mfc_origin = mfc->mfcc_origin.s_addr;
12201da177e4SLinus Torvalds 	c->mfc_mcastgrp = mfc->mfcc_mcastgrp.s_addr;
1221494fff56SYuval Mintz 	c->_c.mfc_parent = mfc->mfcc_parent;
1222494fff56SYuval Mintz 	ipmr_update_thresholds(mrt, &c->_c, mfc->mfcc_ttls);
12231da177e4SLinus Torvalds 	if (!mrtsock)
1224494fff56SYuval Mintz 		c->_c.mfc_flags |= MFC_STATIC;
12251da177e4SLinus Torvalds 
1226494fff56SYuval Mintz 	ret = rhltable_insert_key(&mrt->mfc_hash, &c->cmparg, &c->_c.mnode,
12278fb472c0SNikolay Aleksandrov 				  ipmr_rht_params);
12288fb472c0SNikolay Aleksandrov 	if (ret) {
12298fb472c0SNikolay Aleksandrov 		pr_err("ipmr: rhtable insert error %d\n", ret);
12308fb472c0SNikolay Aleksandrov 		ipmr_cache_free(c);
12318fb472c0SNikolay Aleksandrov 		return ret;
12328fb472c0SNikolay Aleksandrov 	}
1233494fff56SYuval Mintz 	list_add_tail_rcu(&c->_c.list, &mrt->mfc_cache_list);
12347ef8f65dSNikolay Aleksandrov 	/* Check to see if we resolved a queued list. If so we
12351da177e4SLinus Torvalds 	 * need to send on the frames and tidy up.
12361da177e4SLinus Torvalds 	 */
1237b0ebb739SPatrick McHardy 	found = false;
12381da177e4SLinus Torvalds 	spin_lock_bh(&mfc_unres_lock);
1239494fff56SYuval Mintz 	list_for_each_entry(_uc, &mrt->mfc_unres_queue, list) {
1240494fff56SYuval Mintz 		uc = (struct mfc_cache *)_uc;
1241e258beb2SPatrick McHardy 		if (uc->mfc_origin == c->mfc_origin &&
12421da177e4SLinus Torvalds 		    uc->mfc_mcastgrp == c->mfc_mcastgrp) {
1243494fff56SYuval Mintz 			list_del(&_uc->list);
12440c12295aSPatrick McHardy 			atomic_dec(&mrt->cache_resolve_queue_len);
1245b0ebb739SPatrick McHardy 			found = true;
12461da177e4SLinus Torvalds 			break;
12471da177e4SLinus Torvalds 		}
12481da177e4SLinus Torvalds 	}
12490c12295aSPatrick McHardy 	if (list_empty(&mrt->mfc_unres_queue))
12500c12295aSPatrick McHardy 		del_timer(&mrt->ipmr_expire_timer);
12511da177e4SLinus Torvalds 	spin_unlock_bh(&mfc_unres_lock);
12521da177e4SLinus Torvalds 
1253b0ebb739SPatrick McHardy 	if (found) {
12540c12295aSPatrick McHardy 		ipmr_cache_resolve(net, mrt, uc, c);
12555c0a66f5SBenjamin Thery 		ipmr_cache_free(uc);
12561da177e4SLinus Torvalds 	}
1257b362053aSYotam Gigi 	call_ipmr_mfc_entry_notifiers(net, FIB_EVENT_ENTRY_ADD, c, mrt->id);
12588cd3ac9fSNicolas Dichtel 	mroute_netlink_event(mrt, c, RTM_NEWROUTE);
12591da177e4SLinus Torvalds 	return 0;
12601da177e4SLinus Torvalds }
12611da177e4SLinus Torvalds 
12627ef8f65dSNikolay Aleksandrov /* Close the multicast socket, and clear the vif tables etc */
1263ca8d4794SCallum Sinclair static void mroute_clean_tables(struct mr_table *mrt, int flags)
12641da177e4SLinus Torvalds {
1265b362053aSYotam Gigi 	struct net *net = read_pnet(&mrt->net);
1266494fff56SYuval Mintz 	struct mr_mfc *c, *tmp;
1267494fff56SYuval Mintz 	struct mfc_cache *cache;
1268d17fa6faSEric Dumazet 	LIST_HEAD(list);
12698fb472c0SNikolay Aleksandrov 	int i;
12701da177e4SLinus Torvalds 
1271a8cb16ddSEric Dumazet 	/* Shut down all active vif entries */
1272ca8d4794SCallum Sinclair 	if (flags & (MRT_FLUSH_VIFS | MRT_FLUSH_VIFS_STATIC)) {
12730c12295aSPatrick McHardy 		for (i = 0; i < mrt->maxvif; i++) {
1274ca8d4794SCallum Sinclair 			if (((mrt->vif_table[i].flags & VIFF_STATIC) &&
1275ca8d4794SCallum Sinclair 			     !(flags & MRT_FLUSH_VIFS_STATIC)) ||
1276ca8d4794SCallum Sinclair 			    (!(mrt->vif_table[i].flags & VIFF_STATIC) && !(flags & MRT_FLUSH_VIFS)))
12770e615e96SNikolay Aleksandrov 				continue;
12780c12295aSPatrick McHardy 			vif_delete(mrt, i, 0, &list);
12791da177e4SLinus Torvalds 		}
1280d17fa6faSEric Dumazet 		unregister_netdevice_many(&list);
1281ca8d4794SCallum Sinclair 	}
12821da177e4SLinus Torvalds 
1283a8cb16ddSEric Dumazet 	/* Wipe the cache */
1284ca8d4794SCallum Sinclair 	if (flags & (MRT_FLUSH_MFC | MRT_FLUSH_MFC_STATIC)) {
12858fb472c0SNikolay Aleksandrov 		list_for_each_entry_safe(c, tmp, &mrt->mfc_cache_list, list) {
1286ca8d4794SCallum Sinclair 			if (((c->mfc_flags & MFC_STATIC) && !(flags & MRT_FLUSH_MFC_STATIC)) ||
1287ca8d4794SCallum Sinclair 			    (!(c->mfc_flags & MFC_STATIC) && !(flags & MRT_FLUSH_MFC)))
12881da177e4SLinus Torvalds 				continue;
12898fb472c0SNikolay Aleksandrov 			rhltable_remove(&mrt->mfc_hash, &c->mnode, ipmr_rht_params);
1290a8c9486bSEric Dumazet 			list_del_rcu(&c->list);
1291494fff56SYuval Mintz 			cache = (struct mfc_cache *)c;
1292494fff56SYuval Mintz 			call_ipmr_mfc_entry_notifiers(net, FIB_EVENT_ENTRY_DEL, cache,
1293b362053aSYotam Gigi 						      mrt->id);
1294494fff56SYuval Mintz 			mroute_netlink_event(mrt, cache, RTM_DELROUTE);
12958c13af2aSYuval Mintz 			mr_cache_put(c);
12961da177e4SLinus Torvalds 		}
1297ca8d4794SCallum Sinclair 	}
12981da177e4SLinus Torvalds 
1299ca8d4794SCallum Sinclair 	if (flags & MRT_FLUSH_MFC) {
13000c12295aSPatrick McHardy 		if (atomic_read(&mrt->cache_resolve_queue_len) != 0) {
13011da177e4SLinus Torvalds 			spin_lock_bh(&mfc_unres_lock);
13028fb472c0SNikolay Aleksandrov 			list_for_each_entry_safe(c, tmp, &mrt->mfc_unres_queue, list) {
1303862465f2SPatrick McHardy 				list_del(&c->list);
1304494fff56SYuval Mintz 				cache = (struct mfc_cache *)c;
1305494fff56SYuval Mintz 				mroute_netlink_event(mrt, cache, RTM_DELROUTE);
1306494fff56SYuval Mintz 				ipmr_destroy_unres(mrt, cache);
13071da177e4SLinus Torvalds 			}
13081da177e4SLinus Torvalds 			spin_unlock_bh(&mfc_unres_lock);
13091da177e4SLinus Torvalds 		}
13101da177e4SLinus Torvalds 	}
1311ca8d4794SCallum Sinclair }
13121da177e4SLinus Torvalds 
13134c968709SEric Dumazet /* called from ip_ra_control(), before an RCU grace period,
1314974d8f86SZheng Yongjun  * we don't need to call synchronize_rcu() here
13154c968709SEric Dumazet  */
13161da177e4SLinus Torvalds static void mrtsock_destruct(struct sock *sk)
13171da177e4SLinus Torvalds {
13184feb88e5SBenjamin Thery 	struct net *net = sock_net(sk);
1319f0ad0860SPatrick McHardy 	struct mr_table *mrt;
13204feb88e5SBenjamin Thery 
1321128aaa98SKirill Tkhai 	rtnl_lock();
1322f0ad0860SPatrick McHardy 	ipmr_for_each_table(mrt, net) {
13234c968709SEric Dumazet 		if (sk == rtnl_dereference(mrt->mroute_sk)) {
13244feb88e5SBenjamin Thery 			IPV4_DEVCONF_ALL(net, MC_FORWARDING)--;
13253b022865SDavid Ahern 			inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
13263b022865SDavid Ahern 						    NETCONFA_MC_FORWARDING,
1327d67b8c61SNicolas Dichtel 						    NETCONFA_IFINDEX_ALL,
1328d67b8c61SNicolas Dichtel 						    net->ipv4.devconf_all);
1329a9b3cd7fSStephen Hemminger 			RCU_INIT_POINTER(mrt->mroute_sk, NULL);
1330ca8d4794SCallum Sinclair 			mroute_clean_tables(mrt, MRT_FLUSH_VIFS | MRT_FLUSH_MFC);
13311da177e4SLinus Torvalds 		}
13321da177e4SLinus Torvalds 	}
1333128aaa98SKirill Tkhai 	rtnl_unlock();
13341da177e4SLinus Torvalds }
13351da177e4SLinus Torvalds 
13367ef8f65dSNikolay Aleksandrov /* Socket options and virtual interface manipulation. The whole
13371da177e4SLinus Torvalds  * virtual interface system is a complete heap, but unfortunately
13381da177e4SLinus Torvalds  * that's how BSD mrouted happens to think. Maybe one day with a proper
13391da177e4SLinus Torvalds  * MOSPF/PIM router set up we can clean this up.
13401da177e4SLinus Torvalds  */
13411da177e4SLinus Torvalds 
134201ccb5b4SChristoph Hellwig int ip_mroute_setsockopt(struct sock *sk, int optname, sockptr_t optval,
134329e97d21SNikolay Aleksandrov 			 unsigned int optlen)
13441da177e4SLinus Torvalds {
134529e97d21SNikolay Aleksandrov 	struct net *net = sock_net(sk);
134629e97d21SNikolay Aleksandrov 	int val, ret = 0, parent = 0;
134729e97d21SNikolay Aleksandrov 	struct mr_table *mrt;
13481da177e4SLinus Torvalds 	struct vifctl vif;
13491da177e4SLinus Torvalds 	struct mfcctl mfc;
1350c921c207SNikolay Aleksandrov 	bool do_wrvifwhole;
135129e97d21SNikolay Aleksandrov 	u32 uval;
1352f0ad0860SPatrick McHardy 
135329e97d21SNikolay Aleksandrov 	/* There's one exception to the lock - MRT_DONE which needs to unlock */
135429e97d21SNikolay Aleksandrov 	rtnl_lock();
13555e1859fbSEric Dumazet 	if (sk->sk_type != SOCK_RAW ||
135629e97d21SNikolay Aleksandrov 	    inet_sk(sk)->inet_num != IPPROTO_IGMP) {
135729e97d21SNikolay Aleksandrov 		ret = -EOPNOTSUPP;
135829e97d21SNikolay Aleksandrov 		goto out_unlock;
135929e97d21SNikolay Aleksandrov 	}
13605e1859fbSEric Dumazet 
1361f0ad0860SPatrick McHardy 	mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
136229e97d21SNikolay Aleksandrov 	if (!mrt) {
136329e97d21SNikolay Aleksandrov 		ret = -ENOENT;
136429e97d21SNikolay Aleksandrov 		goto out_unlock;
136529e97d21SNikolay Aleksandrov 	}
1366132adf54SStephen Hemminger 	if (optname != MRT_INIT) {
136733d480ceSEric Dumazet 		if (sk != rcu_access_pointer(mrt->mroute_sk) &&
136829e97d21SNikolay Aleksandrov 		    !ns_capable(net->user_ns, CAP_NET_ADMIN)) {
136929e97d21SNikolay Aleksandrov 			ret = -EACCES;
137029e97d21SNikolay Aleksandrov 			goto out_unlock;
137129e97d21SNikolay Aleksandrov 		}
13721da177e4SLinus Torvalds 	}
13731da177e4SLinus Torvalds 
1374132adf54SStephen Hemminger 	switch (optname) {
13751da177e4SLinus Torvalds 	case MRT_INIT:
137642e6b89cSNikolay Aleksandrov 		if (optlen != sizeof(int)) {
137729e97d21SNikolay Aleksandrov 			ret = -EINVAL;
137829e97d21SNikolay Aleksandrov 			break;
137942e6b89cSNikolay Aleksandrov 		}
138042e6b89cSNikolay Aleksandrov 		if (rtnl_dereference(mrt->mroute_sk)) {
138142e6b89cSNikolay Aleksandrov 			ret = -EADDRINUSE;
138242e6b89cSNikolay Aleksandrov 			break;
138342e6b89cSNikolay Aleksandrov 		}
13841da177e4SLinus Torvalds 
13851da177e4SLinus Torvalds 		ret = ip_ra_control(sk, 1, mrtsock_destruct);
13861da177e4SLinus Torvalds 		if (ret == 0) {
1387cf778b00SEric Dumazet 			rcu_assign_pointer(mrt->mroute_sk, sk);
13884feb88e5SBenjamin Thery 			IPV4_DEVCONF_ALL(net, MC_FORWARDING)++;
13893b022865SDavid Ahern 			inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
13903b022865SDavid Ahern 						    NETCONFA_MC_FORWARDING,
1391d67b8c61SNicolas Dichtel 						    NETCONFA_IFINDEX_ALL,
1392d67b8c61SNicolas Dichtel 						    net->ipv4.devconf_all);
13931da177e4SLinus Torvalds 		}
139429e97d21SNikolay Aleksandrov 		break;
13951da177e4SLinus Torvalds 	case MRT_DONE:
139629e97d21SNikolay Aleksandrov 		if (sk != rcu_access_pointer(mrt->mroute_sk)) {
139729e97d21SNikolay Aleksandrov 			ret = -EACCES;
139829e97d21SNikolay Aleksandrov 		} else {
1399128aaa98SKirill Tkhai 			/* We need to unlock here because mrtsock_destruct takes
1400128aaa98SKirill Tkhai 			 * care of rtnl itself and we can't change that due to
1401128aaa98SKirill Tkhai 			 * the IP_ROUTER_ALERT setsockopt which runs without it.
1402128aaa98SKirill Tkhai 			 */
1403128aaa98SKirill Tkhai 			rtnl_unlock();
140429e97d21SNikolay Aleksandrov 			ret = ip_ra_control(sk, 0, NULL);
1405128aaa98SKirill Tkhai 			goto out;
140629e97d21SNikolay Aleksandrov 		}
140729e97d21SNikolay Aleksandrov 		break;
14081da177e4SLinus Torvalds 	case MRT_ADD_VIF:
14091da177e4SLinus Torvalds 	case MRT_DEL_VIF:
141029e97d21SNikolay Aleksandrov 		if (optlen != sizeof(vif)) {
141129e97d21SNikolay Aleksandrov 			ret = -EINVAL;
141229e97d21SNikolay Aleksandrov 			break;
141329e97d21SNikolay Aleksandrov 		}
141401ccb5b4SChristoph Hellwig 		if (copy_from_sockptr(&vif, optval, sizeof(vif))) {
141529e97d21SNikolay Aleksandrov 			ret = -EFAULT;
141629e97d21SNikolay Aleksandrov 			break;
141729e97d21SNikolay Aleksandrov 		}
141829e97d21SNikolay Aleksandrov 		if (vif.vifc_vifi >= MAXVIFS) {
141929e97d21SNikolay Aleksandrov 			ret = -ENFILE;
142029e97d21SNikolay Aleksandrov 			break;
142129e97d21SNikolay Aleksandrov 		}
14221da177e4SLinus Torvalds 		if (optname == MRT_ADD_VIF) {
14234c968709SEric Dumazet 			ret = vif_add(net, mrt, &vif,
14244c968709SEric Dumazet 				      sk == rtnl_dereference(mrt->mroute_sk));
14251da177e4SLinus Torvalds 		} else {
14260c12295aSPatrick McHardy 			ret = vif_delete(mrt, vif.vifc_vifi, 0, NULL);
14271da177e4SLinus Torvalds 		}
142829e97d21SNikolay Aleksandrov 		break;
14297ef8f65dSNikolay Aleksandrov 	/* Manipulate the forwarding caches. These live
14301da177e4SLinus Torvalds 	 * in a sort of kernel/user symbiosis.
14311da177e4SLinus Torvalds 	 */
14321da177e4SLinus Torvalds 	case MRT_ADD_MFC:
14331da177e4SLinus Torvalds 	case MRT_DEL_MFC:
1434660b26dcSNicolas Dichtel 		parent = -1;
1435a8eceea8SJoe Perches 		fallthrough;
1436660b26dcSNicolas Dichtel 	case MRT_ADD_MFC_PROXY:
1437660b26dcSNicolas Dichtel 	case MRT_DEL_MFC_PROXY:
143829e97d21SNikolay Aleksandrov 		if (optlen != sizeof(mfc)) {
143929e97d21SNikolay Aleksandrov 			ret = -EINVAL;
144029e97d21SNikolay Aleksandrov 			break;
144129e97d21SNikolay Aleksandrov 		}
144273cb1193SIdo Schimmel 		if (copy_from_sockptr(&mfc, optval, sizeof(mfc))) {
144329e97d21SNikolay Aleksandrov 			ret = -EFAULT;
144429e97d21SNikolay Aleksandrov 			break;
144529e97d21SNikolay Aleksandrov 		}
1446660b26dcSNicolas Dichtel 		if (parent == 0)
1447660b26dcSNicolas Dichtel 			parent = mfc.mfcc_parent;
1448660b26dcSNicolas Dichtel 		if (optname == MRT_DEL_MFC || optname == MRT_DEL_MFC_PROXY)
1449660b26dcSNicolas Dichtel 			ret = ipmr_mfc_delete(mrt, &mfc, parent);
14501da177e4SLinus Torvalds 		else
14514c968709SEric Dumazet 			ret = ipmr_mfc_add(net, mrt, &mfc,
1452660b26dcSNicolas Dichtel 					   sk == rtnl_dereference(mrt->mroute_sk),
1453660b26dcSNicolas Dichtel 					   parent);
145429e97d21SNikolay Aleksandrov 		break;
1455ca8d4794SCallum Sinclair 	case MRT_FLUSH:
1456ca8d4794SCallum Sinclair 		if (optlen != sizeof(val)) {
1457ca8d4794SCallum Sinclair 			ret = -EINVAL;
1458ca8d4794SCallum Sinclair 			break;
1459ca8d4794SCallum Sinclair 		}
146001ccb5b4SChristoph Hellwig 		if (copy_from_sockptr(&val, optval, sizeof(val))) {
1461ca8d4794SCallum Sinclair 			ret = -EFAULT;
1462ca8d4794SCallum Sinclair 			break;
1463ca8d4794SCallum Sinclair 		}
1464ca8d4794SCallum Sinclair 		mroute_clean_tables(mrt, val);
1465ca8d4794SCallum Sinclair 		break;
14667ef8f65dSNikolay Aleksandrov 	/* Control PIM assert. */
14671da177e4SLinus Torvalds 	case MRT_ASSERT:
146829e97d21SNikolay Aleksandrov 		if (optlen != sizeof(val)) {
146929e97d21SNikolay Aleksandrov 			ret = -EINVAL;
147029e97d21SNikolay Aleksandrov 			break;
14711da177e4SLinus Torvalds 		}
147201ccb5b4SChristoph Hellwig 		if (copy_from_sockptr(&val, optval, sizeof(val))) {
147329e97d21SNikolay Aleksandrov 			ret = -EFAULT;
147429e97d21SNikolay Aleksandrov 			break;
147529e97d21SNikolay Aleksandrov 		}
147629e97d21SNikolay Aleksandrov 		mrt->mroute_do_assert = val;
147729e97d21SNikolay Aleksandrov 		break;
14781da177e4SLinus Torvalds 	case MRT_PIM:
14791973a4eaSNikolay Aleksandrov 		if (!ipmr_pimsm_enabled()) {
148029e97d21SNikolay Aleksandrov 			ret = -ENOPROTOOPT;
148129e97d21SNikolay Aleksandrov 			break;
14821da177e4SLinus Torvalds 		}
148329e97d21SNikolay Aleksandrov 		if (optlen != sizeof(val)) {
148429e97d21SNikolay Aleksandrov 			ret = -EINVAL;
148529e97d21SNikolay Aleksandrov 			break;
14861da177e4SLinus Torvalds 		}
148701ccb5b4SChristoph Hellwig 		if (copy_from_sockptr(&val, optval, sizeof(val))) {
148829e97d21SNikolay Aleksandrov 			ret = -EFAULT;
148929e97d21SNikolay Aleksandrov 			break;
149029e97d21SNikolay Aleksandrov 		}
149129e97d21SNikolay Aleksandrov 
1492c921c207SNikolay Aleksandrov 		do_wrvifwhole = (val == IGMPMSG_WRVIFWHOLE);
149329e97d21SNikolay Aleksandrov 		val = !!val;
149429e97d21SNikolay Aleksandrov 		if (val != mrt->mroute_do_pim) {
149529e97d21SNikolay Aleksandrov 			mrt->mroute_do_pim = val;
149629e97d21SNikolay Aleksandrov 			mrt->mroute_do_assert = val;
1497c921c207SNikolay Aleksandrov 			mrt->mroute_do_wrvifwhole = do_wrvifwhole;
149829e97d21SNikolay Aleksandrov 		}
149929e97d21SNikolay Aleksandrov 		break;
1500f0ad0860SPatrick McHardy 	case MRT_TABLE:
150129e97d21SNikolay Aleksandrov 		if (!IS_BUILTIN(CONFIG_IP_MROUTE_MULTIPLE_TABLES)) {
150229e97d21SNikolay Aleksandrov 			ret = -ENOPROTOOPT;
150329e97d21SNikolay Aleksandrov 			break;
150429e97d21SNikolay Aleksandrov 		}
150529e97d21SNikolay Aleksandrov 		if (optlen != sizeof(uval)) {
150629e97d21SNikolay Aleksandrov 			ret = -EINVAL;
150729e97d21SNikolay Aleksandrov 			break;
150829e97d21SNikolay Aleksandrov 		}
150901ccb5b4SChristoph Hellwig 		if (copy_from_sockptr(&uval, optval, sizeof(uval))) {
151029e97d21SNikolay Aleksandrov 			ret = -EFAULT;
151129e97d21SNikolay Aleksandrov 			break;
151229e97d21SNikolay Aleksandrov 		}
1513f0ad0860SPatrick McHardy 
15144c968709SEric Dumazet 		if (sk == rtnl_dereference(mrt->mroute_sk)) {
15154c968709SEric Dumazet 			ret = -EBUSY;
15164c968709SEric Dumazet 		} else {
151729e97d21SNikolay Aleksandrov 			mrt = ipmr_new_table(net, uval);
15181113ebbcSNikolay Aleksandrov 			if (IS_ERR(mrt))
15191113ebbcSNikolay Aleksandrov 				ret = PTR_ERR(mrt);
15205e1859fbSEric Dumazet 			else
152129e97d21SNikolay Aleksandrov 				raw_sk(sk)->ipmr_table = uval;
15224c968709SEric Dumazet 		}
152329e97d21SNikolay Aleksandrov 		break;
15247ef8f65dSNikolay Aleksandrov 	/* Spurious command, or MRT_VERSION which you cannot set. */
15251da177e4SLinus Torvalds 	default:
152629e97d21SNikolay Aleksandrov 		ret = -ENOPROTOOPT;
15271da177e4SLinus Torvalds 	}
152829e97d21SNikolay Aleksandrov out_unlock:
152929e97d21SNikolay Aleksandrov 	rtnl_unlock();
1530128aaa98SKirill Tkhai out:
153129e97d21SNikolay Aleksandrov 	return ret;
15321da177e4SLinus Torvalds }
15331da177e4SLinus Torvalds 
15347ef8f65dSNikolay Aleksandrov /* Getsock opt support for the multicast routing system. */
15351da177e4SLinus Torvalds int ip_mroute_getsockopt(struct sock *sk, int optname, char __user *optval, int __user *optlen)
15361da177e4SLinus Torvalds {
15371da177e4SLinus Torvalds 	int olr;
15381da177e4SLinus Torvalds 	int val;
15394feb88e5SBenjamin Thery 	struct net *net = sock_net(sk);
1540f0ad0860SPatrick McHardy 	struct mr_table *mrt;
1541f0ad0860SPatrick McHardy 
15425e1859fbSEric Dumazet 	if (sk->sk_type != SOCK_RAW ||
15435e1859fbSEric Dumazet 	    inet_sk(sk)->inet_num != IPPROTO_IGMP)
15445e1859fbSEric Dumazet 		return -EOPNOTSUPP;
15455e1859fbSEric Dumazet 
1546f0ad0860SPatrick McHardy 	mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
154751456b29SIan Morris 	if (!mrt)
1548f0ad0860SPatrick McHardy 		return -ENOENT;
15491da177e4SLinus Torvalds 
1550fe9ef3ceSNikolay Aleksandrov 	switch (optname) {
1551fe9ef3ceSNikolay Aleksandrov 	case MRT_VERSION:
15521da177e4SLinus Torvalds 		val = 0x0305;
1553fe9ef3ceSNikolay Aleksandrov 		break;
1554fe9ef3ceSNikolay Aleksandrov 	case MRT_PIM:
15551973a4eaSNikolay Aleksandrov 		if (!ipmr_pimsm_enabled())
1556c316c629SNikolay Aleksandrov 			return -ENOPROTOOPT;
15570c12295aSPatrick McHardy 		val = mrt->mroute_do_pim;
1558fe9ef3ceSNikolay Aleksandrov 		break;
1559fe9ef3ceSNikolay Aleksandrov 	case MRT_ASSERT:
15600c12295aSPatrick McHardy 		val = mrt->mroute_do_assert;
1561fe9ef3ceSNikolay Aleksandrov 		break;
1562fe9ef3ceSNikolay Aleksandrov 	default:
1563fe9ef3ceSNikolay Aleksandrov 		return -ENOPROTOOPT;
1564c316c629SNikolay Aleksandrov 	}
1565fe9ef3ceSNikolay Aleksandrov 
1566fe9ef3ceSNikolay Aleksandrov 	if (get_user(olr, optlen))
1567fe9ef3ceSNikolay Aleksandrov 		return -EFAULT;
1568fe9ef3ceSNikolay Aleksandrov 	olr = min_t(unsigned int, olr, sizeof(int));
1569fe9ef3ceSNikolay Aleksandrov 	if (olr < 0)
1570fe9ef3ceSNikolay Aleksandrov 		return -EINVAL;
1571fe9ef3ceSNikolay Aleksandrov 	if (put_user(olr, optlen))
1572fe9ef3ceSNikolay Aleksandrov 		return -EFAULT;
15731da177e4SLinus Torvalds 	if (copy_to_user(optval, &val, olr))
15741da177e4SLinus Torvalds 		return -EFAULT;
15751da177e4SLinus Torvalds 	return 0;
15761da177e4SLinus Torvalds }
15771da177e4SLinus Torvalds 
15787ef8f65dSNikolay Aleksandrov /* The IP multicast ioctl support routines. */
15791da177e4SLinus Torvalds int ipmr_ioctl(struct sock *sk, int cmd, void __user *arg)
15801da177e4SLinus Torvalds {
15811da177e4SLinus Torvalds 	struct sioc_sg_req sr;
15821da177e4SLinus Torvalds 	struct sioc_vif_req vr;
15831da177e4SLinus Torvalds 	struct vif_device *vif;
15841da177e4SLinus Torvalds 	struct mfc_cache *c;
15854feb88e5SBenjamin Thery 	struct net *net = sock_net(sk);
1586f0ad0860SPatrick McHardy 	struct mr_table *mrt;
1587f0ad0860SPatrick McHardy 
1588f0ad0860SPatrick McHardy 	mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
158951456b29SIan Morris 	if (!mrt)
1590f0ad0860SPatrick McHardy 		return -ENOENT;
15911da177e4SLinus Torvalds 
1592132adf54SStephen Hemminger 	switch (cmd) {
15931da177e4SLinus Torvalds 	case SIOCGETVIFCNT:
15941da177e4SLinus Torvalds 		if (copy_from_user(&vr, arg, sizeof(vr)))
15951da177e4SLinus Torvalds 			return -EFAULT;
15960c12295aSPatrick McHardy 		if (vr.vifi >= mrt->maxvif)
15971da177e4SLinus Torvalds 			return -EINVAL;
15985648451eSGustavo A. R. Silva 		vr.vifi = array_index_nospec(vr.vifi, mrt->maxvif);
15991da177e4SLinus Torvalds 		read_lock(&mrt_lock);
16000c12295aSPatrick McHardy 		vif = &mrt->vif_table[vr.vifi];
16010c12295aSPatrick McHardy 		if (VIF_EXISTS(mrt, vr.vifi)) {
16021da177e4SLinus Torvalds 			vr.icount = vif->pkt_in;
16031da177e4SLinus Torvalds 			vr.ocount = vif->pkt_out;
16041da177e4SLinus Torvalds 			vr.ibytes = vif->bytes_in;
16051da177e4SLinus Torvalds 			vr.obytes = vif->bytes_out;
16061da177e4SLinus Torvalds 			read_unlock(&mrt_lock);
16071da177e4SLinus Torvalds 
16081da177e4SLinus Torvalds 			if (copy_to_user(arg, &vr, sizeof(vr)))
16091da177e4SLinus Torvalds 				return -EFAULT;
16101da177e4SLinus Torvalds 			return 0;
16111da177e4SLinus Torvalds 		}
16121da177e4SLinus Torvalds 		read_unlock(&mrt_lock);
16131da177e4SLinus Torvalds 		return -EADDRNOTAVAIL;
16141da177e4SLinus Torvalds 	case SIOCGETSGCNT:
16151da177e4SLinus Torvalds 		if (copy_from_user(&sr, arg, sizeof(sr)))
16161da177e4SLinus Torvalds 			return -EFAULT;
16171da177e4SLinus Torvalds 
1618a8c9486bSEric Dumazet 		rcu_read_lock();
16190c12295aSPatrick McHardy 		c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
16201da177e4SLinus Torvalds 		if (c) {
1621494fff56SYuval Mintz 			sr.pktcnt = c->_c.mfc_un.res.pkt;
1622494fff56SYuval Mintz 			sr.bytecnt = c->_c.mfc_un.res.bytes;
1623494fff56SYuval Mintz 			sr.wrong_if = c->_c.mfc_un.res.wrong_if;
1624a8c9486bSEric Dumazet 			rcu_read_unlock();
16251da177e4SLinus Torvalds 
16261da177e4SLinus Torvalds 			if (copy_to_user(arg, &sr, sizeof(sr)))
16271da177e4SLinus Torvalds 				return -EFAULT;
16281da177e4SLinus Torvalds 			return 0;
16291da177e4SLinus Torvalds 		}
1630a8c9486bSEric Dumazet 		rcu_read_unlock();
16311da177e4SLinus Torvalds 		return -EADDRNOTAVAIL;
16321da177e4SLinus Torvalds 	default:
16331da177e4SLinus Torvalds 		return -ENOIOCTLCMD;
16341da177e4SLinus Torvalds 	}
16351da177e4SLinus Torvalds }
16361da177e4SLinus Torvalds 
1637709b46e8SEric W. Biederman #ifdef CONFIG_COMPAT
1638709b46e8SEric W. Biederman struct compat_sioc_sg_req {
1639709b46e8SEric W. Biederman 	struct in_addr src;
1640709b46e8SEric W. Biederman 	struct in_addr grp;
1641709b46e8SEric W. Biederman 	compat_ulong_t pktcnt;
1642709b46e8SEric W. Biederman 	compat_ulong_t bytecnt;
1643709b46e8SEric W. Biederman 	compat_ulong_t wrong_if;
1644709b46e8SEric W. Biederman };
1645709b46e8SEric W. Biederman 
1646ca6b8bb0SDavid S. Miller struct compat_sioc_vif_req {
1647ca6b8bb0SDavid S. Miller 	vifi_t	vifi;		/* Which iface */
1648ca6b8bb0SDavid S. Miller 	compat_ulong_t icount;
1649ca6b8bb0SDavid S. Miller 	compat_ulong_t ocount;
1650ca6b8bb0SDavid S. Miller 	compat_ulong_t ibytes;
1651ca6b8bb0SDavid S. Miller 	compat_ulong_t obytes;
1652ca6b8bb0SDavid S. Miller };
1653ca6b8bb0SDavid S. Miller 
1654709b46e8SEric W. Biederman int ipmr_compat_ioctl(struct sock *sk, unsigned int cmd, void __user *arg)
1655709b46e8SEric W. Biederman {
16560033d5adSDavid S. Miller 	struct compat_sioc_sg_req sr;
1657ca6b8bb0SDavid S. Miller 	struct compat_sioc_vif_req vr;
1658ca6b8bb0SDavid S. Miller 	struct vif_device *vif;
1659709b46e8SEric W. Biederman 	struct mfc_cache *c;
1660709b46e8SEric W. Biederman 	struct net *net = sock_net(sk);
1661709b46e8SEric W. Biederman 	struct mr_table *mrt;
1662709b46e8SEric W. Biederman 
1663709b46e8SEric W. Biederman 	mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
166451456b29SIan Morris 	if (!mrt)
1665709b46e8SEric W. Biederman 		return -ENOENT;
1666709b46e8SEric W. Biederman 
1667709b46e8SEric W. Biederman 	switch (cmd) {
1668ca6b8bb0SDavid S. Miller 	case SIOCGETVIFCNT:
1669ca6b8bb0SDavid S. Miller 		if (copy_from_user(&vr, arg, sizeof(vr)))
1670ca6b8bb0SDavid S. Miller 			return -EFAULT;
1671ca6b8bb0SDavid S. Miller 		if (vr.vifi >= mrt->maxvif)
1672ca6b8bb0SDavid S. Miller 			return -EINVAL;
16735648451eSGustavo A. R. Silva 		vr.vifi = array_index_nospec(vr.vifi, mrt->maxvif);
1674ca6b8bb0SDavid S. Miller 		read_lock(&mrt_lock);
1675ca6b8bb0SDavid S. Miller 		vif = &mrt->vif_table[vr.vifi];
1676ca6b8bb0SDavid S. Miller 		if (VIF_EXISTS(mrt, vr.vifi)) {
1677ca6b8bb0SDavid S. Miller 			vr.icount = vif->pkt_in;
1678ca6b8bb0SDavid S. Miller 			vr.ocount = vif->pkt_out;
1679ca6b8bb0SDavid S. Miller 			vr.ibytes = vif->bytes_in;
1680ca6b8bb0SDavid S. Miller 			vr.obytes = vif->bytes_out;
1681ca6b8bb0SDavid S. Miller 			read_unlock(&mrt_lock);
1682ca6b8bb0SDavid S. Miller 
1683ca6b8bb0SDavid S. Miller 			if (copy_to_user(arg, &vr, sizeof(vr)))
1684ca6b8bb0SDavid S. Miller 				return -EFAULT;
1685ca6b8bb0SDavid S. Miller 			return 0;
1686ca6b8bb0SDavid S. Miller 		}
1687ca6b8bb0SDavid S. Miller 		read_unlock(&mrt_lock);
1688ca6b8bb0SDavid S. Miller 		return -EADDRNOTAVAIL;
1689709b46e8SEric W. Biederman 	case SIOCGETSGCNT:
1690709b46e8SEric W. Biederman 		if (copy_from_user(&sr, arg, sizeof(sr)))
1691709b46e8SEric W. Biederman 			return -EFAULT;
1692709b46e8SEric W. Biederman 
1693709b46e8SEric W. Biederman 		rcu_read_lock();
1694709b46e8SEric W. Biederman 		c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
1695709b46e8SEric W. Biederman 		if (c) {
1696494fff56SYuval Mintz 			sr.pktcnt = c->_c.mfc_un.res.pkt;
1697494fff56SYuval Mintz 			sr.bytecnt = c->_c.mfc_un.res.bytes;
1698494fff56SYuval Mintz 			sr.wrong_if = c->_c.mfc_un.res.wrong_if;
1699709b46e8SEric W. Biederman 			rcu_read_unlock();
1700709b46e8SEric W. Biederman 
1701709b46e8SEric W. Biederman 			if (copy_to_user(arg, &sr, sizeof(sr)))
1702709b46e8SEric W. Biederman 				return -EFAULT;
1703709b46e8SEric W. Biederman 			return 0;
1704709b46e8SEric W. Biederman 		}
1705709b46e8SEric W. Biederman 		rcu_read_unlock();
1706709b46e8SEric W. Biederman 		return -EADDRNOTAVAIL;
1707709b46e8SEric W. Biederman 	default:
1708709b46e8SEric W. Biederman 		return -ENOIOCTLCMD;
1709709b46e8SEric W. Biederman 	}
1710709b46e8SEric W. Biederman }
1711709b46e8SEric W. Biederman #endif
1712709b46e8SEric W. Biederman 
17131da177e4SLinus Torvalds static int ipmr_device_event(struct notifier_block *this, unsigned long event, void *ptr)
17141da177e4SLinus Torvalds {
1715351638e7SJiri Pirko 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
17164feb88e5SBenjamin Thery 	struct net *net = dev_net(dev);
1717f0ad0860SPatrick McHardy 	struct mr_table *mrt;
17181da177e4SLinus Torvalds 	struct vif_device *v;
17191da177e4SLinus Torvalds 	int ct;
1720e9dc8653SEric W. Biederman 
17211da177e4SLinus Torvalds 	if (event != NETDEV_UNREGISTER)
17221da177e4SLinus Torvalds 		return NOTIFY_DONE;
1723f0ad0860SPatrick McHardy 
1724f0ad0860SPatrick McHardy 	ipmr_for_each_table(mrt, net) {
17250c12295aSPatrick McHardy 		v = &mrt->vif_table[0];
17260c12295aSPatrick McHardy 		for (ct = 0; ct < mrt->maxvif; ct++, v++) {
1727e9dc8653SEric W. Biederman 			if (v->dev == dev)
1728e92036a6SRongQing.Li 				vif_delete(mrt, ct, 1, NULL);
17291da177e4SLinus Torvalds 		}
17301da177e4SLinus Torvalds 	}
17311da177e4SLinus Torvalds 	return NOTIFY_DONE;
17321da177e4SLinus Torvalds }
17331da177e4SLinus Torvalds 
17341da177e4SLinus Torvalds static struct notifier_block ip_mr_notifier = {
17351da177e4SLinus Torvalds 	.notifier_call = ipmr_device_event,
17361da177e4SLinus Torvalds };
17371da177e4SLinus Torvalds 
17387ef8f65dSNikolay Aleksandrov /* Encapsulate a packet by attaching a valid IPIP header to it.
17391da177e4SLinus Torvalds  * This avoids tunnel drivers and other mess and gives us the speed so
17401da177e4SLinus Torvalds  * important for multicast video.
17411da177e4SLinus Torvalds  */
1742b6a7719aSHannes Frederic Sowa static void ip_encap(struct net *net, struct sk_buff *skb,
1743b6a7719aSHannes Frederic Sowa 		     __be32 saddr, __be32 daddr)
17441da177e4SLinus Torvalds {
17458856dfa3SArnaldo Carvalho de Melo 	struct iphdr *iph;
1746b71d1d42SEric Dumazet 	const struct iphdr *old_iph = ip_hdr(skb);
17478856dfa3SArnaldo Carvalho de Melo 
17488856dfa3SArnaldo Carvalho de Melo 	skb_push(skb, sizeof(struct iphdr));
1749b0e380b1SArnaldo Carvalho de Melo 	skb->transport_header = skb->network_header;
17508856dfa3SArnaldo Carvalho de Melo 	skb_reset_network_header(skb);
1751eddc9ec5SArnaldo Carvalho de Melo 	iph = ip_hdr(skb);
17521da177e4SLinus Torvalds 
17531da177e4SLinus Torvalds 	iph->version	=	4;
1754e023dd64SArnaldo Carvalho de Melo 	iph->tos	=	old_iph->tos;
1755e023dd64SArnaldo Carvalho de Melo 	iph->ttl	=	old_iph->ttl;
17561da177e4SLinus Torvalds 	iph->frag_off	=	0;
17571da177e4SLinus Torvalds 	iph->daddr	=	daddr;
17581da177e4SLinus Torvalds 	iph->saddr	=	saddr;
17591da177e4SLinus Torvalds 	iph->protocol	=	IPPROTO_IPIP;
17601da177e4SLinus Torvalds 	iph->ihl	=	5;
17611da177e4SLinus Torvalds 	iph->tot_len	=	htons(skb->len);
1762b6a7719aSHannes Frederic Sowa 	ip_select_ident(net, skb, NULL);
17631da177e4SLinus Torvalds 	ip_send_check(iph);
17641da177e4SLinus Torvalds 
17651da177e4SLinus Torvalds 	memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1766895b5c9fSFlorian Westphal 	nf_reset_ct(skb);
17671da177e4SLinus Torvalds }
17681da177e4SLinus Torvalds 
17690c4b51f0SEric W. Biederman static inline int ipmr_forward_finish(struct net *net, struct sock *sk,
17700c4b51f0SEric W. Biederman 				      struct sk_buff *skb)
17711da177e4SLinus Torvalds {
17721da177e4SLinus Torvalds 	struct ip_options *opt = &(IPCB(skb)->opt);
17731da177e4SLinus Torvalds 
177473186df8SDavid S. Miller 	IP_INC_STATS(net, IPSTATS_MIB_OUTFORWDATAGRAMS);
177573186df8SDavid S. Miller 	IP_ADD_STATS(net, IPSTATS_MIB_OUTOCTETS, skb->len);
17761da177e4SLinus Torvalds 
17771da177e4SLinus Torvalds 	if (unlikely(opt->optlen))
17781da177e4SLinus Torvalds 		ip_forward_options(skb);
17791da177e4SLinus Torvalds 
178013206b6bSEric W. Biederman 	return dst_output(net, sk, skb);
17811da177e4SLinus Torvalds }
17821da177e4SLinus Torvalds 
1783a5bc9294SYotam Gigi #ifdef CONFIG_NET_SWITCHDEV
1784a5bc9294SYotam Gigi static bool ipmr_forward_offloaded(struct sk_buff *skb, struct mr_table *mrt,
1785a5bc9294SYotam Gigi 				   int in_vifi, int out_vifi)
1786a5bc9294SYotam Gigi {
1787a5bc9294SYotam Gigi 	struct vif_device *out_vif = &mrt->vif_table[out_vifi];
1788a5bc9294SYotam Gigi 	struct vif_device *in_vif = &mrt->vif_table[in_vifi];
1789a5bc9294SYotam Gigi 
1790875e8939SIdo Schimmel 	if (!skb->offload_l3_fwd_mark)
1791a5bc9294SYotam Gigi 		return false;
1792a5bc9294SYotam Gigi 	if (!out_vif->dev_parent_id.id_len || !in_vif->dev_parent_id.id_len)
1793a5bc9294SYotam Gigi 		return false;
1794a5bc9294SYotam Gigi 	return netdev_phys_item_id_same(&out_vif->dev_parent_id,
1795a5bc9294SYotam Gigi 					&in_vif->dev_parent_id);
1796a5bc9294SYotam Gigi }
1797a5bc9294SYotam Gigi #else
1798a5bc9294SYotam Gigi static bool ipmr_forward_offloaded(struct sk_buff *skb, struct mr_table *mrt,
1799a5bc9294SYotam Gigi 				   int in_vifi, int out_vifi)
1800a5bc9294SYotam Gigi {
1801a5bc9294SYotam Gigi 	return false;
1802a5bc9294SYotam Gigi }
1803a5bc9294SYotam Gigi #endif
1804a5bc9294SYotam Gigi 
18057ef8f65dSNikolay Aleksandrov /* Processing handlers for ipmr_forward */
18061da177e4SLinus Torvalds 
18070c12295aSPatrick McHardy static void ipmr_queue_xmit(struct net *net, struct mr_table *mrt,
18086e0735d1SDavid Ahern 			    int in_vifi, struct sk_buff *skb, int vifi)
18091da177e4SLinus Torvalds {
1810eddc9ec5SArnaldo Carvalho de Melo 	const struct iphdr *iph = ip_hdr(skb);
18110c12295aSPatrick McHardy 	struct vif_device *vif = &mrt->vif_table[vifi];
18121da177e4SLinus Torvalds 	struct net_device *dev;
18131da177e4SLinus Torvalds 	struct rtable *rt;
181431e4543dSDavid S. Miller 	struct flowi4 fl4;
18151da177e4SLinus Torvalds 	int    encap = 0;
18161da177e4SLinus Torvalds 
181751456b29SIan Morris 	if (!vif->dev)
18181da177e4SLinus Torvalds 		goto out_free;
18191da177e4SLinus Torvalds 
18201da177e4SLinus Torvalds 	if (vif->flags & VIFF_REGISTER) {
18211da177e4SLinus Torvalds 		vif->pkt_out++;
18221da177e4SLinus Torvalds 		vif->bytes_out += skb->len;
1823cf3677aeSPavel Emelyanov 		vif->dev->stats.tx_bytes += skb->len;
1824cf3677aeSPavel Emelyanov 		vif->dev->stats.tx_packets++;
18250c12295aSPatrick McHardy 		ipmr_cache_report(mrt, skb, vifi, IGMPMSG_WHOLEPKT);
182669ebbf58SIlpo Järvinen 		goto out_free;
18271da177e4SLinus Torvalds 	}
18281da177e4SLinus Torvalds 
1829a5bc9294SYotam Gigi 	if (ipmr_forward_offloaded(skb, mrt, in_vifi, vifi))
1830a5bc9294SYotam Gigi 		goto out_free;
1831a5bc9294SYotam Gigi 
18321da177e4SLinus Torvalds 	if (vif->flags & VIFF_TUNNEL) {
183331e4543dSDavid S. Miller 		rt = ip_route_output_ports(net, &fl4, NULL,
183478fbfd8aSDavid S. Miller 					   vif->remote, vif->local,
183578fbfd8aSDavid S. Miller 					   0, 0,
183678fbfd8aSDavid S. Miller 					   IPPROTO_IPIP,
183778fbfd8aSDavid S. Miller 					   RT_TOS(iph->tos), vif->link);
1838b23dd4feSDavid S. Miller 		if (IS_ERR(rt))
18391da177e4SLinus Torvalds 			goto out_free;
18401da177e4SLinus Torvalds 		encap = sizeof(struct iphdr);
18411da177e4SLinus Torvalds 	} else {
184231e4543dSDavid S. Miller 		rt = ip_route_output_ports(net, &fl4, NULL, iph->daddr, 0,
184378fbfd8aSDavid S. Miller 					   0, 0,
184478fbfd8aSDavid S. Miller 					   IPPROTO_IPIP,
184578fbfd8aSDavid S. Miller 					   RT_TOS(iph->tos), vif->link);
1846b23dd4feSDavid S. Miller 		if (IS_ERR(rt))
18471da177e4SLinus Torvalds 			goto out_free;
18481da177e4SLinus Torvalds 	}
18491da177e4SLinus Torvalds 
1850d8d1f30bSChangli Gao 	dev = rt->dst.dev;
18511da177e4SLinus Torvalds 
1852d8d1f30bSChangli Gao 	if (skb->len+encap > dst_mtu(&rt->dst) && (ntohs(iph->frag_off) & IP_DF)) {
18531da177e4SLinus Torvalds 		/* Do not fragment multicasts. Alas, IPv4 does not
1854a8cb16ddSEric Dumazet 		 * allow to send ICMP, so that packets will disappear
1855a8cb16ddSEric Dumazet 		 * to blackhole.
18561da177e4SLinus Torvalds 		 */
185773186df8SDavid S. Miller 		IP_INC_STATS(net, IPSTATS_MIB_FRAGFAILS);
18581da177e4SLinus Torvalds 		ip_rt_put(rt);
18591da177e4SLinus Torvalds 		goto out_free;
18601da177e4SLinus Torvalds 	}
18611da177e4SLinus Torvalds 
1862d8d1f30bSChangli Gao 	encap += LL_RESERVED_SPACE(dev) + rt->dst.header_len;
18631da177e4SLinus Torvalds 
18641da177e4SLinus Torvalds 	if (skb_cow(skb, encap)) {
18651da177e4SLinus Torvalds 		ip_rt_put(rt);
18661da177e4SLinus Torvalds 		goto out_free;
18671da177e4SLinus Torvalds 	}
18681da177e4SLinus Torvalds 
18691da177e4SLinus Torvalds 	vif->pkt_out++;
18701da177e4SLinus Torvalds 	vif->bytes_out += skb->len;
18711da177e4SLinus Torvalds 
1872adf30907SEric Dumazet 	skb_dst_drop(skb);
1873d8d1f30bSChangli Gao 	skb_dst_set(skb, &rt->dst);
1874eddc9ec5SArnaldo Carvalho de Melo 	ip_decrease_ttl(ip_hdr(skb));
18751da177e4SLinus Torvalds 
18761da177e4SLinus Torvalds 	/* FIXME: forward and output firewalls used to be called here.
1877a8cb16ddSEric Dumazet 	 * What do we do with netfilter? -- RR
1878a8cb16ddSEric Dumazet 	 */
18791da177e4SLinus Torvalds 	if (vif->flags & VIFF_TUNNEL) {
1880b6a7719aSHannes Frederic Sowa 		ip_encap(net, skb, vif->local, vif->remote);
18811da177e4SLinus Torvalds 		/* FIXME: extra output firewall step used to be here. --RR */
18822f4c02d4SPavel Emelyanov 		vif->dev->stats.tx_packets++;
18832f4c02d4SPavel Emelyanov 		vif->dev->stats.tx_bytes += skb->len;
18841da177e4SLinus Torvalds 	}
18851da177e4SLinus Torvalds 
18869ee6c5dcSLance Richardson 	IPCB(skb)->flags |= IPSKB_FORWARDED;
18871da177e4SLinus Torvalds 
18887ef8f65dSNikolay Aleksandrov 	/* RFC1584 teaches, that DVMRP/PIM router must deliver packets locally
18891da177e4SLinus Torvalds 	 * not only before forwarding, but after forwarding on all output
18901da177e4SLinus Torvalds 	 * interfaces. It is clear, if mrouter runs a multicasting
18911da177e4SLinus Torvalds 	 * program, it should receive packets not depending to what interface
18921da177e4SLinus Torvalds 	 * program is joined.
18931da177e4SLinus Torvalds 	 * If we will not make it, the program will have to join on all
18941da177e4SLinus Torvalds 	 * interfaces. On the other hand, multihoming host (or router, but
18951da177e4SLinus Torvalds 	 * not mrouter) cannot join to more than one interface - it will
18961da177e4SLinus Torvalds 	 * result in receiving multiple packets.
18971da177e4SLinus Torvalds 	 */
189829a26a56SEric W. Biederman 	NF_HOOK(NFPROTO_IPV4, NF_INET_FORWARD,
189929a26a56SEric W. Biederman 		net, NULL, skb, skb->dev, dev,
19001da177e4SLinus Torvalds 		ipmr_forward_finish);
19011da177e4SLinus Torvalds 	return;
19021da177e4SLinus Torvalds 
19031da177e4SLinus Torvalds out_free:
19041da177e4SLinus Torvalds 	kfree_skb(skb);
19051da177e4SLinus Torvalds }
19061da177e4SLinus Torvalds 
19070c12295aSPatrick McHardy static int ipmr_find_vif(struct mr_table *mrt, struct net_device *dev)
19081da177e4SLinus Torvalds {
19091da177e4SLinus Torvalds 	int ct;
19100c12295aSPatrick McHardy 
19110c12295aSPatrick McHardy 	for (ct = mrt->maxvif-1; ct >= 0; ct--) {
19120c12295aSPatrick McHardy 		if (mrt->vif_table[ct].dev == dev)
19131da177e4SLinus Torvalds 			break;
19141da177e4SLinus Torvalds 	}
19151da177e4SLinus Torvalds 	return ct;
19161da177e4SLinus Torvalds }
19171da177e4SLinus Torvalds 
19181da177e4SLinus Torvalds /* "local" means that we should preserve one skb (for local delivery) */
1919c4854ec8SRami Rosen static void ip_mr_forward(struct net *net, struct mr_table *mrt,
19204b1f0d33SDonald Sharp 			  struct net_device *dev, struct sk_buff *skb,
1921494fff56SYuval Mintz 			  struct mfc_cache *c, int local)
19221da177e4SLinus Torvalds {
19234b1f0d33SDonald Sharp 	int true_vifi = ipmr_find_vif(mrt, dev);
19241da177e4SLinus Torvalds 	int psend = -1;
19251da177e4SLinus Torvalds 	int vif, ct;
19261da177e4SLinus Torvalds 
1927494fff56SYuval Mintz 	vif = c->_c.mfc_parent;
1928494fff56SYuval Mintz 	c->_c.mfc_un.res.pkt++;
1929494fff56SYuval Mintz 	c->_c.mfc_un.res.bytes += skb->len;
1930494fff56SYuval Mintz 	c->_c.mfc_un.res.lastuse = jiffies;
19311da177e4SLinus Torvalds 
1932494fff56SYuval Mintz 	if (c->mfc_origin == htonl(INADDR_ANY) && true_vifi >= 0) {
1933660b26dcSNicolas Dichtel 		struct mfc_cache *cache_proxy;
1934660b26dcSNicolas Dichtel 
1935974d8f86SZheng Yongjun 		/* For an (*,G) entry, we only check that the incoming
1936660b26dcSNicolas Dichtel 		 * interface is part of the static tree.
1937660b26dcSNicolas Dichtel 		 */
1938845c9a7aSYuval Mintz 		cache_proxy = mr_mfc_find_any_parent(mrt, vif);
1939660b26dcSNicolas Dichtel 		if (cache_proxy &&
1940494fff56SYuval Mintz 		    cache_proxy->_c.mfc_un.res.ttls[true_vifi] < 255)
1941660b26dcSNicolas Dichtel 			goto forward;
1942660b26dcSNicolas Dichtel 	}
1943660b26dcSNicolas Dichtel 
19447ef8f65dSNikolay Aleksandrov 	/* Wrong interface: drop packet and (maybe) send PIM assert. */
19454b1f0d33SDonald Sharp 	if (mrt->vif_table[vif].dev != dev) {
1946c7537967SDavid S. Miller 		if (rt_is_output_route(skb_rtable(skb))) {
19471da177e4SLinus Torvalds 			/* It is our own packet, looped back.
1948a8cb16ddSEric Dumazet 			 * Very complicated situation...
1949a8cb16ddSEric Dumazet 			 *
1950a8cb16ddSEric Dumazet 			 * The best workaround until routing daemons will be
1951a8cb16ddSEric Dumazet 			 * fixed is not to redistribute packet, if it was
1952a8cb16ddSEric Dumazet 			 * send through wrong interface. It means, that
1953a8cb16ddSEric Dumazet 			 * multicast applications WILL NOT work for
1954a8cb16ddSEric Dumazet 			 * (S,G), which have default multicast route pointing
1955a8cb16ddSEric Dumazet 			 * to wrong oif. In any case, it is not a good
1956a8cb16ddSEric Dumazet 			 * idea to use multicasting applications on router.
19571da177e4SLinus Torvalds 			 */
19581da177e4SLinus Torvalds 			goto dont_forward;
19591da177e4SLinus Torvalds 		}
19601da177e4SLinus Torvalds 
1961494fff56SYuval Mintz 		c->_c.mfc_un.res.wrong_if++;
19621da177e4SLinus Torvalds 
19630c12295aSPatrick McHardy 		if (true_vifi >= 0 && mrt->mroute_do_assert &&
19641da177e4SLinus Torvalds 		    /* pimsm uses asserts, when switching from RPT to SPT,
1965a8cb16ddSEric Dumazet 		     * so that we cannot check that packet arrived on an oif.
1966a8cb16ddSEric Dumazet 		     * It is bad, but otherwise we would need to move pretty
1967a8cb16ddSEric Dumazet 		     * large chunk of pimd to kernel. Ough... --ANK
19681da177e4SLinus Torvalds 		     */
19690c12295aSPatrick McHardy 		    (mrt->mroute_do_pim ||
1970494fff56SYuval Mintz 		     c->_c.mfc_un.res.ttls[true_vifi] < 255) &&
19711da177e4SLinus Torvalds 		    time_after(jiffies,
1972494fff56SYuval Mintz 			       c->_c.mfc_un.res.last_assert +
1973494fff56SYuval Mintz 			       MFC_ASSERT_THRESH)) {
1974494fff56SYuval Mintz 			c->_c.mfc_un.res.last_assert = jiffies;
19750c12295aSPatrick McHardy 			ipmr_cache_report(mrt, skb, true_vifi, IGMPMSG_WRONGVIF);
1976c921c207SNikolay Aleksandrov 			if (mrt->mroute_do_wrvifwhole)
1977c921c207SNikolay Aleksandrov 				ipmr_cache_report(mrt, skb, true_vifi,
1978c921c207SNikolay Aleksandrov 						  IGMPMSG_WRVIFWHOLE);
19791da177e4SLinus Torvalds 		}
19801da177e4SLinus Torvalds 		goto dont_forward;
19811da177e4SLinus Torvalds 	}
19821da177e4SLinus Torvalds 
1983660b26dcSNicolas Dichtel forward:
19840c12295aSPatrick McHardy 	mrt->vif_table[vif].pkt_in++;
19850c12295aSPatrick McHardy 	mrt->vif_table[vif].bytes_in += skb->len;
19861da177e4SLinus Torvalds 
19877ef8f65dSNikolay Aleksandrov 	/* Forward the frame */
1988494fff56SYuval Mintz 	if (c->mfc_origin == htonl(INADDR_ANY) &&
1989494fff56SYuval Mintz 	    c->mfc_mcastgrp == htonl(INADDR_ANY)) {
1990660b26dcSNicolas Dichtel 		if (true_vifi >= 0 &&
1991494fff56SYuval Mintz 		    true_vifi != c->_c.mfc_parent &&
1992660b26dcSNicolas Dichtel 		    ip_hdr(skb)->ttl >
1993494fff56SYuval Mintz 				c->_c.mfc_un.res.ttls[c->_c.mfc_parent]) {
1994660b26dcSNicolas Dichtel 			/* It's an (*,*) entry and the packet is not coming from
1995660b26dcSNicolas Dichtel 			 * the upstream: forward the packet to the upstream
1996660b26dcSNicolas Dichtel 			 * only.
1997660b26dcSNicolas Dichtel 			 */
1998494fff56SYuval Mintz 			psend = c->_c.mfc_parent;
1999660b26dcSNicolas Dichtel 			goto last_forward;
2000660b26dcSNicolas Dichtel 		}
2001660b26dcSNicolas Dichtel 		goto dont_forward;
2002660b26dcSNicolas Dichtel 	}
2003494fff56SYuval Mintz 	for (ct = c->_c.mfc_un.res.maxvif - 1;
2004494fff56SYuval Mintz 	     ct >= c->_c.mfc_un.res.minvif; ct--) {
2005660b26dcSNicolas Dichtel 		/* For (*,G) entry, don't forward to the incoming interface */
2006494fff56SYuval Mintz 		if ((c->mfc_origin != htonl(INADDR_ANY) ||
2007360eb5daSNicolas Dichtel 		     ct != true_vifi) &&
2008494fff56SYuval Mintz 		    ip_hdr(skb)->ttl > c->_c.mfc_un.res.ttls[ct]) {
20091da177e4SLinus Torvalds 			if (psend != -1) {
20101da177e4SLinus Torvalds 				struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
2011a8cb16ddSEric Dumazet 
20121da177e4SLinus Torvalds 				if (skb2)
2013a5bc9294SYotam Gigi 					ipmr_queue_xmit(net, mrt, true_vifi,
20146e0735d1SDavid Ahern 							skb2, psend);
20151da177e4SLinus Torvalds 			}
20161da177e4SLinus Torvalds 			psend = ct;
20171da177e4SLinus Torvalds 		}
20181da177e4SLinus Torvalds 	}
2019660b26dcSNicolas Dichtel last_forward:
20201da177e4SLinus Torvalds 	if (psend != -1) {
20211da177e4SLinus Torvalds 		if (local) {
20221da177e4SLinus Torvalds 			struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
2023a8cb16ddSEric Dumazet 
20241da177e4SLinus Torvalds 			if (skb2)
2025a5bc9294SYotam Gigi 				ipmr_queue_xmit(net, mrt, true_vifi, skb2,
20266e0735d1SDavid Ahern 						psend);
20271da177e4SLinus Torvalds 		} else {
20286e0735d1SDavid Ahern 			ipmr_queue_xmit(net, mrt, true_vifi, skb, psend);
2029c4854ec8SRami Rosen 			return;
20301da177e4SLinus Torvalds 		}
20311da177e4SLinus Torvalds 	}
20321da177e4SLinus Torvalds 
20331da177e4SLinus Torvalds dont_forward:
20341da177e4SLinus Torvalds 	if (!local)
20351da177e4SLinus Torvalds 		kfree_skb(skb);
20361da177e4SLinus Torvalds }
20371da177e4SLinus Torvalds 
2038417da66fSDavid S. Miller static struct mr_table *ipmr_rt_fib_lookup(struct net *net, struct sk_buff *skb)
2039ee3f1aafSDavid S. Miller {
2040417da66fSDavid S. Miller 	struct rtable *rt = skb_rtable(skb);
2041417da66fSDavid S. Miller 	struct iphdr *iph = ip_hdr(skb);
2042da91981bSDavid S. Miller 	struct flowi4 fl4 = {
2043417da66fSDavid S. Miller 		.daddr = iph->daddr,
2044417da66fSDavid S. Miller 		.saddr = iph->saddr,
2045b0fe4a31SJulian Anastasov 		.flowi4_tos = RT_TOS(iph->tos),
20464fd551d7SDavid S. Miller 		.flowi4_oif = (rt_is_output_route(rt) ?
20474fd551d7SDavid S. Miller 			       skb->dev->ifindex : 0),
20484fd551d7SDavid S. Miller 		.flowi4_iif = (rt_is_output_route(rt) ?
20491fb9489bSPavel Emelyanov 			       LOOPBACK_IFINDEX :
20504fd551d7SDavid S. Miller 			       skb->dev->ifindex),
2051b4869889SDavid Miller 		.flowi4_mark = skb->mark,
2052ee3f1aafSDavid S. Miller 	};
2053ee3f1aafSDavid S. Miller 	struct mr_table *mrt;
2054ee3f1aafSDavid S. Miller 	int err;
2055ee3f1aafSDavid S. Miller 
2056da91981bSDavid S. Miller 	err = ipmr_fib_lookup(net, &fl4, &mrt);
2057ee3f1aafSDavid S. Miller 	if (err)
2058ee3f1aafSDavid S. Miller 		return ERR_PTR(err);
2059ee3f1aafSDavid S. Miller 	return mrt;
2060ee3f1aafSDavid S. Miller }
20611da177e4SLinus Torvalds 
20627ef8f65dSNikolay Aleksandrov /* Multicast packets for forwarding arrive here
20634c968709SEric Dumazet  * Called with rcu_read_lock();
20641da177e4SLinus Torvalds  */
20651da177e4SLinus Torvalds int ip_mr_input(struct sk_buff *skb)
20661da177e4SLinus Torvalds {
20671da177e4SLinus Torvalds 	struct mfc_cache *cache;
20684feb88e5SBenjamin Thery 	struct net *net = dev_net(skb->dev);
2069511c3f92SEric Dumazet 	int local = skb_rtable(skb)->rt_flags & RTCF_LOCAL;
2070f0ad0860SPatrick McHardy 	struct mr_table *mrt;
2071bcfc7d33SThomas Winter 	struct net_device *dev;
2072bcfc7d33SThomas Winter 
2073bcfc7d33SThomas Winter 	/* skb->dev passed in is the loX master dev for vrfs.
2074bcfc7d33SThomas Winter 	 * As there are no vifs associated with loopback devices,
2075bcfc7d33SThomas Winter 	 * get the proper interface that does have a vif associated with it.
2076bcfc7d33SThomas Winter 	 */
2077bcfc7d33SThomas Winter 	dev = skb->dev;
2078bcfc7d33SThomas Winter 	if (netif_is_l3_master(skb->dev)) {
2079bcfc7d33SThomas Winter 		dev = dev_get_by_index_rcu(net, IPCB(skb)->iif);
2080bcfc7d33SThomas Winter 		if (!dev) {
2081bcfc7d33SThomas Winter 			kfree_skb(skb);
2082bcfc7d33SThomas Winter 			return -ENODEV;
2083bcfc7d33SThomas Winter 		}
2084bcfc7d33SThomas Winter 	}
20851da177e4SLinus Torvalds 
20861da177e4SLinus Torvalds 	/* Packet is looped back after forward, it should not be
2087a8cb16ddSEric Dumazet 	 * forwarded second time, but still can be delivered locally.
20881da177e4SLinus Torvalds 	 */
20891da177e4SLinus Torvalds 	if (IPCB(skb)->flags & IPSKB_FORWARDED)
20901da177e4SLinus Torvalds 		goto dont_forward;
20911da177e4SLinus Torvalds 
2092417da66fSDavid S. Miller 	mrt = ipmr_rt_fib_lookup(net, skb);
2093ee3f1aafSDavid S. Miller 	if (IS_ERR(mrt)) {
2094e40dbc51SBen Greear 		kfree_skb(skb);
2095ee3f1aafSDavid S. Miller 		return PTR_ERR(mrt);
20965e2b61f7SDavid S. Miller 	}
20971da177e4SLinus Torvalds 	if (!local) {
20981da177e4SLinus Torvalds 		if (IPCB(skb)->opt.router_alert) {
20991da177e4SLinus Torvalds 			if (ip_call_ra_chain(skb))
21001da177e4SLinus Torvalds 				return 0;
2101eddc9ec5SArnaldo Carvalho de Melo 		} else if (ip_hdr(skb)->protocol == IPPROTO_IGMP) {
21021da177e4SLinus Torvalds 			/* IGMPv1 (and broken IGMPv2 implementations sort of
21034c968709SEric Dumazet 			 * Cisco IOS <= 11.2(8)) do not put router alert
21044c968709SEric Dumazet 			 * option to IGMP packets destined to routable
21054c968709SEric Dumazet 			 * groups. It is very bad, because it means
21064c968709SEric Dumazet 			 * that we can forward NO IGMP messages.
21071da177e4SLinus Torvalds 			 */
21084c968709SEric Dumazet 			struct sock *mroute_sk;
21094c968709SEric Dumazet 
21104c968709SEric Dumazet 			mroute_sk = rcu_dereference(mrt->mroute_sk);
21114c968709SEric Dumazet 			if (mroute_sk) {
2112895b5c9fSFlorian Westphal 				nf_reset_ct(skb);
21134c968709SEric Dumazet 				raw_rcv(mroute_sk, skb);
21141da177e4SLinus Torvalds 				return 0;
21151da177e4SLinus Torvalds 			}
21161da177e4SLinus Torvalds 		}
21171da177e4SLinus Torvalds 	}
21181da177e4SLinus Torvalds 
2119a8c9486bSEric Dumazet 	/* already under rcu_read_lock() */
21200c12295aSPatrick McHardy 	cache = ipmr_cache_find(mrt, ip_hdr(skb)->saddr, ip_hdr(skb)->daddr);
212151456b29SIan Morris 	if (!cache) {
2122bcfc7d33SThomas Winter 		int vif = ipmr_find_vif(mrt, dev);
2123660b26dcSNicolas Dichtel 
2124660b26dcSNicolas Dichtel 		if (vif >= 0)
2125660b26dcSNicolas Dichtel 			cache = ipmr_cache_find_any(mrt, ip_hdr(skb)->daddr,
2126660b26dcSNicolas Dichtel 						    vif);
2127660b26dcSNicolas Dichtel 	}
21281da177e4SLinus Torvalds 
21297ef8f65dSNikolay Aleksandrov 	/* No usable cache entry */
213051456b29SIan Morris 	if (!cache) {
21311da177e4SLinus Torvalds 		int vif;
21321da177e4SLinus Torvalds 
21331da177e4SLinus Torvalds 		if (local) {
21341da177e4SLinus Torvalds 			struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
21351da177e4SLinus Torvalds 			ip_local_deliver(skb);
213651456b29SIan Morris 			if (!skb2)
21371da177e4SLinus Torvalds 				return -ENOBUFS;
21381da177e4SLinus Torvalds 			skb = skb2;
21391da177e4SLinus Torvalds 		}
21401da177e4SLinus Torvalds 
2141a8c9486bSEric Dumazet 		read_lock(&mrt_lock);
2142bcfc7d33SThomas Winter 		vif = ipmr_find_vif(mrt, dev);
21431da177e4SLinus Torvalds 		if (vif >= 0) {
21444b1f0d33SDonald Sharp 			int err2 = ipmr_cache_unresolved(mrt, vif, skb, dev);
21451da177e4SLinus Torvalds 			read_unlock(&mrt_lock);
21461da177e4SLinus Torvalds 
21470eae88f3SEric Dumazet 			return err2;
21481da177e4SLinus Torvalds 		}
21491da177e4SLinus Torvalds 		read_unlock(&mrt_lock);
21501da177e4SLinus Torvalds 		kfree_skb(skb);
21511da177e4SLinus Torvalds 		return -ENODEV;
21521da177e4SLinus Torvalds 	}
21531da177e4SLinus Torvalds 
2154a8c9486bSEric Dumazet 	read_lock(&mrt_lock);
21554b1f0d33SDonald Sharp 	ip_mr_forward(net, mrt, dev, skb, cache, local);
21561da177e4SLinus Torvalds 	read_unlock(&mrt_lock);
21571da177e4SLinus Torvalds 
21581da177e4SLinus Torvalds 	if (local)
21591da177e4SLinus Torvalds 		return ip_local_deliver(skb);
21601da177e4SLinus Torvalds 
21611da177e4SLinus Torvalds 	return 0;
21621da177e4SLinus Torvalds 
21631da177e4SLinus Torvalds dont_forward:
21641da177e4SLinus Torvalds 	if (local)
21651da177e4SLinus Torvalds 		return ip_local_deliver(skb);
21661da177e4SLinus Torvalds 	kfree_skb(skb);
21671da177e4SLinus Torvalds 	return 0;
21681da177e4SLinus Torvalds }
21691da177e4SLinus Torvalds 
2170b1879204SIlpo Järvinen #ifdef CONFIG_IP_PIMSM_V1
21717ef8f65dSNikolay Aleksandrov /* Handle IGMP messages of PIMv1 */
2172b1879204SIlpo Järvinen int pim_rcv_v1(struct sk_buff *skb)
2173b1879204SIlpo Järvinen {
2174b1879204SIlpo Järvinen 	struct igmphdr *pim;
21754feb88e5SBenjamin Thery 	struct net *net = dev_net(skb->dev);
2176f0ad0860SPatrick McHardy 	struct mr_table *mrt;
2177b1879204SIlpo Järvinen 
2178b1879204SIlpo Järvinen 	if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
2179b1879204SIlpo Järvinen 		goto drop;
2180b1879204SIlpo Järvinen 
2181b1879204SIlpo Järvinen 	pim = igmp_hdr(skb);
2182b1879204SIlpo Järvinen 
2183417da66fSDavid S. Miller 	mrt = ipmr_rt_fib_lookup(net, skb);
2184ee3f1aafSDavid S. Miller 	if (IS_ERR(mrt))
2185f0ad0860SPatrick McHardy 		goto drop;
21860c12295aSPatrick McHardy 	if (!mrt->mroute_do_pim ||
2187b1879204SIlpo Järvinen 	    pim->group != PIM_V1_VERSION || pim->code != PIM_V1_REGISTER)
2188b1879204SIlpo Järvinen 		goto drop;
2189b1879204SIlpo Järvinen 
2190f0ad0860SPatrick McHardy 	if (__pim_rcv(mrt, skb, sizeof(*pim))) {
21911da177e4SLinus Torvalds drop:
21921da177e4SLinus Torvalds 		kfree_skb(skb);
2193b1879204SIlpo Järvinen 	}
21941da177e4SLinus Torvalds 	return 0;
21951da177e4SLinus Torvalds }
21961da177e4SLinus Torvalds #endif
21971da177e4SLinus Torvalds 
21981da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM_V2
21991da177e4SLinus Torvalds static int pim_rcv(struct sk_buff *skb)
22001da177e4SLinus Torvalds {
22011da177e4SLinus Torvalds 	struct pimreghdr *pim;
2202f0ad0860SPatrick McHardy 	struct net *net = dev_net(skb->dev);
2203f0ad0860SPatrick McHardy 	struct mr_table *mrt;
22041da177e4SLinus Torvalds 
2205b1879204SIlpo Järvinen 	if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
22061da177e4SLinus Torvalds 		goto drop;
22071da177e4SLinus Torvalds 
22089c70220bSArnaldo Carvalho de Melo 	pim = (struct pimreghdr *)skb_transport_header(skb);
220956245caeSNikolay Aleksandrov 	if (pim->type != ((PIM_VERSION << 4) | (PIM_TYPE_REGISTER)) ||
22101da177e4SLinus Torvalds 	    (pim->flags & PIM_NULL_REGISTER) ||
22111da177e4SLinus Torvalds 	    (ip_compute_csum((void *)pim, sizeof(*pim)) != 0 &&
2212d3bc23e7SAl Viro 	     csum_fold(skb_checksum(skb, 0, skb->len, 0))))
22131da177e4SLinus Torvalds 		goto drop;
22141da177e4SLinus Torvalds 
2215417da66fSDavid S. Miller 	mrt = ipmr_rt_fib_lookup(net, skb);
2216ee3f1aafSDavid S. Miller 	if (IS_ERR(mrt))
2217f0ad0860SPatrick McHardy 		goto drop;
2218f0ad0860SPatrick McHardy 	if (__pim_rcv(mrt, skb, sizeof(*pim))) {
22191da177e4SLinus Torvalds drop:
22201da177e4SLinus Torvalds 		kfree_skb(skb);
2221b1879204SIlpo Järvinen 	}
22221da177e4SLinus Torvalds 	return 0;
22231da177e4SLinus Torvalds }
22241da177e4SLinus Torvalds #endif
22251da177e4SLinus Torvalds 
22269a1b9496SDavid S. Miller int ipmr_get_route(struct net *net, struct sk_buff *skb,
22279a1b9496SDavid S. Miller 		   __be32 saddr, __be32 daddr,
22289f09eaeaSDavid Ahern 		   struct rtmsg *rtm, u32 portid)
22291da177e4SLinus Torvalds {
22301da177e4SLinus Torvalds 	struct mfc_cache *cache;
22319a1b9496SDavid S. Miller 	struct mr_table *mrt;
22329a1b9496SDavid S. Miller 	int err;
22331da177e4SLinus Torvalds 
2234f0ad0860SPatrick McHardy 	mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
223551456b29SIan Morris 	if (!mrt)
2236f0ad0860SPatrick McHardy 		return -ENOENT;
2237f0ad0860SPatrick McHardy 
2238a8c9486bSEric Dumazet 	rcu_read_lock();
22399a1b9496SDavid S. Miller 	cache = ipmr_cache_find(mrt, saddr, daddr);
224051456b29SIan Morris 	if (!cache && skb->dev) {
2241660b26dcSNicolas Dichtel 		int vif = ipmr_find_vif(mrt, skb->dev);
22421da177e4SLinus Torvalds 
2243660b26dcSNicolas Dichtel 		if (vif >= 0)
2244660b26dcSNicolas Dichtel 			cache = ipmr_cache_find_any(mrt, daddr, vif);
2245660b26dcSNicolas Dichtel 	}
224651456b29SIan Morris 	if (!cache) {
224772287490SAlexey Kuznetsov 		struct sk_buff *skb2;
2248eddc9ec5SArnaldo Carvalho de Melo 		struct iphdr *iph;
22491da177e4SLinus Torvalds 		struct net_device *dev;
2250a8cb16ddSEric Dumazet 		int vif = -1;
22511da177e4SLinus Torvalds 
22521da177e4SLinus Torvalds 		dev = skb->dev;
2253a8c9486bSEric Dumazet 		read_lock(&mrt_lock);
2254a8cb16ddSEric Dumazet 		if (dev)
2255a8cb16ddSEric Dumazet 			vif = ipmr_find_vif(mrt, dev);
2256a8cb16ddSEric Dumazet 		if (vif < 0) {
22571da177e4SLinus Torvalds 			read_unlock(&mrt_lock);
2258a8c9486bSEric Dumazet 			rcu_read_unlock();
22591da177e4SLinus Torvalds 			return -ENODEV;
22601da177e4SLinus Torvalds 		}
22617901cd97SGuillaume Nault 
22627901cd97SGuillaume Nault 		skb2 = skb_realloc_headroom(skb, sizeof(struct iphdr));
226372287490SAlexey Kuznetsov 		if (!skb2) {
226472287490SAlexey Kuznetsov 			read_unlock(&mrt_lock);
2265a8c9486bSEric Dumazet 			rcu_read_unlock();
226672287490SAlexey Kuznetsov 			return -ENOMEM;
226772287490SAlexey Kuznetsov 		}
226872287490SAlexey Kuznetsov 
22692cf75070SNikolay Aleksandrov 		NETLINK_CB(skb2).portid = portid;
2270e2d1bca7SArnaldo Carvalho de Melo 		skb_push(skb2, sizeof(struct iphdr));
2271e2d1bca7SArnaldo Carvalho de Melo 		skb_reset_network_header(skb2);
2272eddc9ec5SArnaldo Carvalho de Melo 		iph = ip_hdr(skb2);
2273eddc9ec5SArnaldo Carvalho de Melo 		iph->ihl = sizeof(struct iphdr) >> 2;
22749a1b9496SDavid S. Miller 		iph->saddr = saddr;
22759a1b9496SDavid S. Miller 		iph->daddr = daddr;
2276eddc9ec5SArnaldo Carvalho de Melo 		iph->version = 0;
22774b1f0d33SDonald Sharp 		err = ipmr_cache_unresolved(mrt, vif, skb2, dev);
22781da177e4SLinus Torvalds 		read_unlock(&mrt_lock);
2279a8c9486bSEric Dumazet 		rcu_read_unlock();
22801da177e4SLinus Torvalds 		return err;
22811da177e4SLinus Torvalds 	}
22821da177e4SLinus Torvalds 
2283a8c9486bSEric Dumazet 	read_lock(&mrt_lock);
22847b0db857SYuval Mintz 	err = mr_fill_mroute(mrt, skb, &cache->_c, rtm);
22851da177e4SLinus Torvalds 	read_unlock(&mrt_lock);
2286a8c9486bSEric Dumazet 	rcu_read_unlock();
22871da177e4SLinus Torvalds 	return err;
22881da177e4SLinus Torvalds }
22891da177e4SLinus Torvalds 
2290cb6a4e46SPatrick McHardy static int ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
229165886f43SNicolas Dichtel 			    u32 portid, u32 seq, struct mfc_cache *c, int cmd,
229265886f43SNicolas Dichtel 			    int flags)
2293cb6a4e46SPatrick McHardy {
2294cb6a4e46SPatrick McHardy 	struct nlmsghdr *nlh;
2295cb6a4e46SPatrick McHardy 	struct rtmsg *rtm;
22961eb99af5SNicolas Dichtel 	int err;
2297cb6a4e46SPatrick McHardy 
229865886f43SNicolas Dichtel 	nlh = nlmsg_put(skb, portid, seq, cmd, sizeof(*rtm), flags);
229951456b29SIan Morris 	if (!nlh)
2300cb6a4e46SPatrick McHardy 		return -EMSGSIZE;
2301cb6a4e46SPatrick McHardy 
2302cb6a4e46SPatrick McHardy 	rtm = nlmsg_data(nlh);
2303cb6a4e46SPatrick McHardy 	rtm->rtm_family   = RTNL_FAMILY_IPMR;
2304cb6a4e46SPatrick McHardy 	rtm->rtm_dst_len  = 32;
2305cb6a4e46SPatrick McHardy 	rtm->rtm_src_len  = 32;
2306cb6a4e46SPatrick McHardy 	rtm->rtm_tos      = 0;
2307cb6a4e46SPatrick McHardy 	rtm->rtm_table    = mrt->id;
2308f3756b79SDavid S. Miller 	if (nla_put_u32(skb, RTA_TABLE, mrt->id))
2309f3756b79SDavid S. Miller 		goto nla_put_failure;
2310cb6a4e46SPatrick McHardy 	rtm->rtm_type     = RTN_MULTICAST;
2311cb6a4e46SPatrick McHardy 	rtm->rtm_scope    = RT_SCOPE_UNIVERSE;
2312494fff56SYuval Mintz 	if (c->_c.mfc_flags & MFC_STATIC)
23139a68ac72SNicolas Dichtel 		rtm->rtm_protocol = RTPROT_STATIC;
23149a68ac72SNicolas Dichtel 	else
23159a68ac72SNicolas Dichtel 		rtm->rtm_protocol = RTPROT_MROUTED;
2316cb6a4e46SPatrick McHardy 	rtm->rtm_flags    = 0;
2317cb6a4e46SPatrick McHardy 
2318930345eaSJiri Benc 	if (nla_put_in_addr(skb, RTA_SRC, c->mfc_origin) ||
2319930345eaSJiri Benc 	    nla_put_in_addr(skb, RTA_DST, c->mfc_mcastgrp))
2320f3756b79SDavid S. Miller 		goto nla_put_failure;
23217b0db857SYuval Mintz 	err = mr_fill_mroute(mrt, skb, &c->_c, rtm);
23221eb99af5SNicolas Dichtel 	/* do not break the dump if cache is unresolved */
23231eb99af5SNicolas Dichtel 	if (err < 0 && err != -ENOENT)
2324cb6a4e46SPatrick McHardy 		goto nla_put_failure;
2325cb6a4e46SPatrick McHardy 
2326053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
2327053c095aSJohannes Berg 	return 0;
2328cb6a4e46SPatrick McHardy 
2329cb6a4e46SPatrick McHardy nla_put_failure:
2330cb6a4e46SPatrick McHardy 	nlmsg_cancel(skb, nlh);
2331cb6a4e46SPatrick McHardy 	return -EMSGSIZE;
2332cb6a4e46SPatrick McHardy }
2333cb6a4e46SPatrick McHardy 
23347b0db857SYuval Mintz static int _ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
23357b0db857SYuval Mintz 			     u32 portid, u32 seq, struct mr_mfc *c, int cmd,
23367b0db857SYuval Mintz 			     int flags)
23377b0db857SYuval Mintz {
23387b0db857SYuval Mintz 	return ipmr_fill_mroute(mrt, skb, portid, seq, (struct mfc_cache *)c,
23397b0db857SYuval Mintz 				cmd, flags);
23407b0db857SYuval Mintz }
23417b0db857SYuval Mintz 
23428cd3ac9fSNicolas Dichtel static size_t mroute_msgsize(bool unresolved, int maxvif)
23438cd3ac9fSNicolas Dichtel {
23448cd3ac9fSNicolas Dichtel 	size_t len =
23458cd3ac9fSNicolas Dichtel 		NLMSG_ALIGN(sizeof(struct rtmsg))
23468cd3ac9fSNicolas Dichtel 		+ nla_total_size(4)	/* RTA_TABLE */
23478cd3ac9fSNicolas Dichtel 		+ nla_total_size(4)	/* RTA_SRC */
23488cd3ac9fSNicolas Dichtel 		+ nla_total_size(4)	/* RTA_DST */
23498cd3ac9fSNicolas Dichtel 		;
23508cd3ac9fSNicolas Dichtel 
23518cd3ac9fSNicolas Dichtel 	if (!unresolved)
23528cd3ac9fSNicolas Dichtel 		len = len
23538cd3ac9fSNicolas Dichtel 		      + nla_total_size(4)	/* RTA_IIF */
23548cd3ac9fSNicolas Dichtel 		      + nla_total_size(0)	/* RTA_MULTIPATH */
23558cd3ac9fSNicolas Dichtel 		      + maxvif * NLA_ALIGN(sizeof(struct rtnexthop))
23568cd3ac9fSNicolas Dichtel 						/* RTA_MFC_STATS */
2357a9a08042SNicolas Dichtel 		      + nla_total_size_64bit(sizeof(struct rta_mfc_stats))
23588cd3ac9fSNicolas Dichtel 		;
23598cd3ac9fSNicolas Dichtel 
23608cd3ac9fSNicolas Dichtel 	return len;
23618cd3ac9fSNicolas Dichtel }
23628cd3ac9fSNicolas Dichtel 
23638cd3ac9fSNicolas Dichtel static void mroute_netlink_event(struct mr_table *mrt, struct mfc_cache *mfc,
23648cd3ac9fSNicolas Dichtel 				 int cmd)
23658cd3ac9fSNicolas Dichtel {
23668cd3ac9fSNicolas Dichtel 	struct net *net = read_pnet(&mrt->net);
23678cd3ac9fSNicolas Dichtel 	struct sk_buff *skb;
23688cd3ac9fSNicolas Dichtel 	int err = -ENOBUFS;
23698cd3ac9fSNicolas Dichtel 
2370494fff56SYuval Mintz 	skb = nlmsg_new(mroute_msgsize(mfc->_c.mfc_parent >= MAXVIFS,
2371494fff56SYuval Mintz 				       mrt->maxvif),
23728cd3ac9fSNicolas Dichtel 			GFP_ATOMIC);
237351456b29SIan Morris 	if (!skb)
23748cd3ac9fSNicolas Dichtel 		goto errout;
23758cd3ac9fSNicolas Dichtel 
237665886f43SNicolas Dichtel 	err = ipmr_fill_mroute(mrt, skb, 0, 0, mfc, cmd, 0);
23778cd3ac9fSNicolas Dichtel 	if (err < 0)
23788cd3ac9fSNicolas Dichtel 		goto errout;
23798cd3ac9fSNicolas Dichtel 
23808cd3ac9fSNicolas Dichtel 	rtnl_notify(skb, net, 0, RTNLGRP_IPV4_MROUTE, NULL, GFP_ATOMIC);
23818cd3ac9fSNicolas Dichtel 	return;
23828cd3ac9fSNicolas Dichtel 
23838cd3ac9fSNicolas Dichtel errout:
23848cd3ac9fSNicolas Dichtel 	kfree_skb(skb);
23858cd3ac9fSNicolas Dichtel 	if (err < 0)
23868cd3ac9fSNicolas Dichtel 		rtnl_set_sk_err(net, RTNLGRP_IPV4_MROUTE, err);
23878cd3ac9fSNicolas Dichtel }
23888cd3ac9fSNicolas Dichtel 
23895a645dd8SJulien Gomes static size_t igmpmsg_netlink_msgsize(size_t payloadlen)
23905a645dd8SJulien Gomes {
23915a645dd8SJulien Gomes 	size_t len =
23925a645dd8SJulien Gomes 		NLMSG_ALIGN(sizeof(struct rtgenmsg))
23935a645dd8SJulien Gomes 		+ nla_total_size(1)	/* IPMRA_CREPORT_MSGTYPE */
23945a645dd8SJulien Gomes 		+ nla_total_size(4)	/* IPMRA_CREPORT_VIF_ID */
23955a645dd8SJulien Gomes 		+ nla_total_size(4)	/* IPMRA_CREPORT_SRC_ADDR */
23965a645dd8SJulien Gomes 		+ nla_total_size(4)	/* IPMRA_CREPORT_DST_ADDR */
2397501cb008SPaul Davey 		+ nla_total_size(4)	/* IPMRA_CREPORT_TABLE */
23985a645dd8SJulien Gomes 					/* IPMRA_CREPORT_PKT */
23995a645dd8SJulien Gomes 		+ nla_total_size(payloadlen)
24005a645dd8SJulien Gomes 		;
24015a645dd8SJulien Gomes 
24025a645dd8SJulien Gomes 	return len;
24035a645dd8SJulien Gomes }
24045a645dd8SJulien Gomes 
24055a645dd8SJulien Gomes static void igmpmsg_netlink_event(struct mr_table *mrt, struct sk_buff *pkt)
24065a645dd8SJulien Gomes {
24075a645dd8SJulien Gomes 	struct net *net = read_pnet(&mrt->net);
24085a645dd8SJulien Gomes 	struct nlmsghdr *nlh;
24095a645dd8SJulien Gomes 	struct rtgenmsg *rtgenm;
24105a645dd8SJulien Gomes 	struct igmpmsg *msg;
24115a645dd8SJulien Gomes 	struct sk_buff *skb;
24125a645dd8SJulien Gomes 	struct nlattr *nla;
24135a645dd8SJulien Gomes 	int payloadlen;
24145a645dd8SJulien Gomes 
24155a645dd8SJulien Gomes 	payloadlen = pkt->len - sizeof(struct igmpmsg);
24165a645dd8SJulien Gomes 	msg = (struct igmpmsg *)skb_network_header(pkt);
24175a645dd8SJulien Gomes 
24185a645dd8SJulien Gomes 	skb = nlmsg_new(igmpmsg_netlink_msgsize(payloadlen), GFP_ATOMIC);
24195a645dd8SJulien Gomes 	if (!skb)
24205a645dd8SJulien Gomes 		goto errout;
24215a645dd8SJulien Gomes 
24225a645dd8SJulien Gomes 	nlh = nlmsg_put(skb, 0, 0, RTM_NEWCACHEREPORT,
24235a645dd8SJulien Gomes 			sizeof(struct rtgenmsg), 0);
24245a645dd8SJulien Gomes 	if (!nlh)
24255a645dd8SJulien Gomes 		goto errout;
24265a645dd8SJulien Gomes 	rtgenm = nlmsg_data(nlh);
24275a645dd8SJulien Gomes 	rtgenm->rtgen_family = RTNL_FAMILY_IPMR;
24285a645dd8SJulien Gomes 	if (nla_put_u8(skb, IPMRA_CREPORT_MSGTYPE, msg->im_msgtype) ||
2429bb82067cSPaul Davey 	    nla_put_u32(skb, IPMRA_CREPORT_VIF_ID, msg->im_vif | (msg->im_vif_hi << 8)) ||
24305a645dd8SJulien Gomes 	    nla_put_in_addr(skb, IPMRA_CREPORT_SRC_ADDR,
24315a645dd8SJulien Gomes 			    msg->im_src.s_addr) ||
24325a645dd8SJulien Gomes 	    nla_put_in_addr(skb, IPMRA_CREPORT_DST_ADDR,
2433501cb008SPaul Davey 			    msg->im_dst.s_addr) ||
2434501cb008SPaul Davey 	    nla_put_u32(skb, IPMRA_CREPORT_TABLE, mrt->id))
24355a645dd8SJulien Gomes 		goto nla_put_failure;
24365a645dd8SJulien Gomes 
24375a645dd8SJulien Gomes 	nla = nla_reserve(skb, IPMRA_CREPORT_PKT, payloadlen);
24385a645dd8SJulien Gomes 	if (!nla || skb_copy_bits(pkt, sizeof(struct igmpmsg),
24395a645dd8SJulien Gomes 				  nla_data(nla), payloadlen))
24405a645dd8SJulien Gomes 		goto nla_put_failure;
24415a645dd8SJulien Gomes 
24425a645dd8SJulien Gomes 	nlmsg_end(skb, nlh);
24435a645dd8SJulien Gomes 
24445a645dd8SJulien Gomes 	rtnl_notify(skb, net, 0, RTNLGRP_IPV4_MROUTE_R, NULL, GFP_ATOMIC);
24455a645dd8SJulien Gomes 	return;
24465a645dd8SJulien Gomes 
24475a645dd8SJulien Gomes nla_put_failure:
24485a645dd8SJulien Gomes 	nlmsg_cancel(skb, nlh);
24495a645dd8SJulien Gomes errout:
24505a645dd8SJulien Gomes 	kfree_skb(skb);
24515a645dd8SJulien Gomes 	rtnl_set_sk_err(net, RTNLGRP_IPV4_MROUTE_R, -ENOBUFS);
24525a645dd8SJulien Gomes }
24535a645dd8SJulien Gomes 
2454d0440029SJakub Kicinski static int ipmr_rtm_valid_getroute_req(struct sk_buff *skb,
2455d0440029SJakub Kicinski 				       const struct nlmsghdr *nlh,
2456d0440029SJakub Kicinski 				       struct nlattr **tb,
2457d0440029SJakub Kicinski 				       struct netlink_ext_ack *extack)
2458d0440029SJakub Kicinski {
2459d0440029SJakub Kicinski 	struct rtmsg *rtm;
2460d0440029SJakub Kicinski 	int i, err;
2461d0440029SJakub Kicinski 
2462d0440029SJakub Kicinski 	if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*rtm))) {
2463d0440029SJakub Kicinski 		NL_SET_ERR_MSG(extack, "ipv4: Invalid header for multicast route get request");
2464d0440029SJakub Kicinski 		return -EINVAL;
2465d0440029SJakub Kicinski 	}
2466d0440029SJakub Kicinski 
2467d0440029SJakub Kicinski 	if (!netlink_strict_get_check(skb))
24688cb08174SJohannes Berg 		return nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX,
2469d0440029SJakub Kicinski 					      rtm_ipv4_policy, extack);
2470d0440029SJakub Kicinski 
2471d0440029SJakub Kicinski 	rtm = nlmsg_data(nlh);
2472d0440029SJakub Kicinski 	if ((rtm->rtm_src_len && rtm->rtm_src_len != 32) ||
2473d0440029SJakub Kicinski 	    (rtm->rtm_dst_len && rtm->rtm_dst_len != 32) ||
2474d0440029SJakub Kicinski 	    rtm->rtm_tos || rtm->rtm_table || rtm->rtm_protocol ||
2475d0440029SJakub Kicinski 	    rtm->rtm_scope || rtm->rtm_type || rtm->rtm_flags) {
2476d0440029SJakub Kicinski 		NL_SET_ERR_MSG(extack, "ipv4: Invalid values in header for multicast route get request");
2477d0440029SJakub Kicinski 		return -EINVAL;
2478d0440029SJakub Kicinski 	}
2479d0440029SJakub Kicinski 
24808cb08174SJohannes Berg 	err = nlmsg_parse_deprecated_strict(nlh, sizeof(*rtm), tb, RTA_MAX,
2481d0440029SJakub Kicinski 					    rtm_ipv4_policy, extack);
2482d0440029SJakub Kicinski 	if (err)
2483d0440029SJakub Kicinski 		return err;
2484d0440029SJakub Kicinski 
2485d0440029SJakub Kicinski 	if ((tb[RTA_SRC] && !rtm->rtm_src_len) ||
2486d0440029SJakub Kicinski 	    (tb[RTA_DST] && !rtm->rtm_dst_len)) {
2487d0440029SJakub Kicinski 		NL_SET_ERR_MSG(extack, "ipv4: rtm_src_len and rtm_dst_len must be 32 for IPv4");
2488d0440029SJakub Kicinski 		return -EINVAL;
2489d0440029SJakub Kicinski 	}
2490d0440029SJakub Kicinski 
2491d0440029SJakub Kicinski 	for (i = 0; i <= RTA_MAX; i++) {
2492d0440029SJakub Kicinski 		if (!tb[i])
2493d0440029SJakub Kicinski 			continue;
2494d0440029SJakub Kicinski 
2495d0440029SJakub Kicinski 		switch (i) {
2496d0440029SJakub Kicinski 		case RTA_SRC:
2497d0440029SJakub Kicinski 		case RTA_DST:
2498d0440029SJakub Kicinski 		case RTA_TABLE:
2499d0440029SJakub Kicinski 			break;
2500d0440029SJakub Kicinski 		default:
2501d0440029SJakub Kicinski 			NL_SET_ERR_MSG(extack, "ipv4: Unsupported attribute in multicast route get request");
2502d0440029SJakub Kicinski 			return -EINVAL;
2503d0440029SJakub Kicinski 		}
2504d0440029SJakub Kicinski 	}
2505d0440029SJakub Kicinski 
2506d0440029SJakub Kicinski 	return 0;
2507d0440029SJakub Kicinski }
2508d0440029SJakub Kicinski 
25094f75ba69SDonald Sharp static int ipmr_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh,
25104f75ba69SDonald Sharp 			     struct netlink_ext_ack *extack)
25114f75ba69SDonald Sharp {
25124f75ba69SDonald Sharp 	struct net *net = sock_net(in_skb->sk);
25134f75ba69SDonald Sharp 	struct nlattr *tb[RTA_MAX + 1];
25144f75ba69SDonald Sharp 	struct sk_buff *skb = NULL;
25154f75ba69SDonald Sharp 	struct mfc_cache *cache;
25164f75ba69SDonald Sharp 	struct mr_table *mrt;
25174f75ba69SDonald Sharp 	__be32 src, grp;
25184f75ba69SDonald Sharp 	u32 tableid;
25194f75ba69SDonald Sharp 	int err;
25204f75ba69SDonald Sharp 
2521d0440029SJakub Kicinski 	err = ipmr_rtm_valid_getroute_req(in_skb, nlh, tb, extack);
25224f75ba69SDonald Sharp 	if (err < 0)
25234f75ba69SDonald Sharp 		goto errout;
25244f75ba69SDonald Sharp 
25254f75ba69SDonald Sharp 	src = tb[RTA_SRC] ? nla_get_in_addr(tb[RTA_SRC]) : 0;
25264f75ba69SDonald Sharp 	grp = tb[RTA_DST] ? nla_get_in_addr(tb[RTA_DST]) : 0;
25274f75ba69SDonald Sharp 	tableid = tb[RTA_TABLE] ? nla_get_u32(tb[RTA_TABLE]) : 0;
25284f75ba69SDonald Sharp 
25294f75ba69SDonald Sharp 	mrt = ipmr_get_table(net, tableid ? tableid : RT_TABLE_DEFAULT);
25302e3d232eSDan Carpenter 	if (!mrt) {
25312e3d232eSDan Carpenter 		err = -ENOENT;
25324f75ba69SDonald Sharp 		goto errout_free;
25334f75ba69SDonald Sharp 	}
25344f75ba69SDonald Sharp 
25354f75ba69SDonald Sharp 	/* entries are added/deleted only under RTNL */
25364f75ba69SDonald Sharp 	rcu_read_lock();
25374f75ba69SDonald Sharp 	cache = ipmr_cache_find(mrt, src, grp);
25384f75ba69SDonald Sharp 	rcu_read_unlock();
25394f75ba69SDonald Sharp 	if (!cache) {
25404f75ba69SDonald Sharp 		err = -ENOENT;
25414f75ba69SDonald Sharp 		goto errout_free;
25424f75ba69SDonald Sharp 	}
25434f75ba69SDonald Sharp 
25444f75ba69SDonald Sharp 	skb = nlmsg_new(mroute_msgsize(false, mrt->maxvif), GFP_KERNEL);
25454f75ba69SDonald Sharp 	if (!skb) {
25464f75ba69SDonald Sharp 		err = -ENOBUFS;
25474f75ba69SDonald Sharp 		goto errout_free;
25484f75ba69SDonald Sharp 	}
25494f75ba69SDonald Sharp 
25504f75ba69SDonald Sharp 	err = ipmr_fill_mroute(mrt, skb, NETLINK_CB(in_skb).portid,
25514f75ba69SDonald Sharp 			       nlh->nlmsg_seq, cache,
25524f75ba69SDonald Sharp 			       RTM_NEWROUTE, 0);
25534f75ba69SDonald Sharp 	if (err < 0)
25544f75ba69SDonald Sharp 		goto errout_free;
25554f75ba69SDonald Sharp 
25564f75ba69SDonald Sharp 	err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
25574f75ba69SDonald Sharp 
25584f75ba69SDonald Sharp errout:
25594f75ba69SDonald Sharp 	return err;
25604f75ba69SDonald Sharp 
25614f75ba69SDonald Sharp errout_free:
25624f75ba69SDonald Sharp 	kfree_skb(skb);
25634f75ba69SDonald Sharp 	goto errout;
25644f75ba69SDonald Sharp }
25654f75ba69SDonald Sharp 
2566cb6a4e46SPatrick McHardy static int ipmr_rtm_dumproute(struct sk_buff *skb, struct netlink_callback *cb)
2567cb6a4e46SPatrick McHardy {
25684724676dSDavid Ahern 	struct fib_dump_filter filter = {};
25694724676dSDavid Ahern 	int err;
25704724676dSDavid Ahern 
2571cb167893SDavid Ahern 	if (cb->strict_check) {
25724724676dSDavid Ahern 		err = ip_valid_fib_dump_req(sock_net(skb->sk), cb->nlh,
2573effe6792SDavid Ahern 					    &filter, cb);
2574e8ba330aSDavid Ahern 		if (err < 0)
2575e8ba330aSDavid Ahern 			return err;
2576e8ba330aSDavid Ahern 	}
2577e8ba330aSDavid Ahern 
2578cb167893SDavid Ahern 	if (filter.table_id) {
2579cb167893SDavid Ahern 		struct mr_table *mrt;
2580cb167893SDavid Ahern 
2581cb167893SDavid Ahern 		mrt = ipmr_get_table(sock_net(skb->sk), filter.table_id);
2582cb167893SDavid Ahern 		if (!mrt) {
258341b4bd98SSabrina Dubroca 			if (rtnl_msg_family(cb->nlh) != RTNL_FAMILY_IPMR)
2584ae677bbbSDavid Ahern 				return skb->len;
2585ae677bbbSDavid Ahern 
2586cb167893SDavid Ahern 			NL_SET_ERR_MSG(cb->extack, "ipv4: MR table does not exist");
2587cb167893SDavid Ahern 			return -ENOENT;
2588cb167893SDavid Ahern 		}
2589cb167893SDavid Ahern 		err = mr_table_dump(mrt, skb, cb, _ipmr_fill_mroute,
2590cb167893SDavid Ahern 				    &mfc_unres_lock, &filter);
2591cb167893SDavid Ahern 		return skb->len ? : err;
2592cb167893SDavid Ahern 	}
2593cb167893SDavid Ahern 
25947b0db857SYuval Mintz 	return mr_rtm_dumproute(skb, cb, ipmr_mr_table_iter,
2595cb167893SDavid Ahern 				_ipmr_fill_mroute, &mfc_unres_lock, &filter);
2596cb6a4e46SPatrick McHardy }
2597cb6a4e46SPatrick McHardy 
2598ccbb0aa6SNikolay Aleksandrov static const struct nla_policy rtm_ipmr_policy[RTA_MAX + 1] = {
2599ccbb0aa6SNikolay Aleksandrov 	[RTA_SRC]	= { .type = NLA_U32 },
2600ccbb0aa6SNikolay Aleksandrov 	[RTA_DST]	= { .type = NLA_U32 },
2601ccbb0aa6SNikolay Aleksandrov 	[RTA_IIF]	= { .type = NLA_U32 },
2602ccbb0aa6SNikolay Aleksandrov 	[RTA_TABLE]	= { .type = NLA_U32 },
2603ccbb0aa6SNikolay Aleksandrov 	[RTA_MULTIPATH]	= { .len = sizeof(struct rtnexthop) },
2604ccbb0aa6SNikolay Aleksandrov };
2605ccbb0aa6SNikolay Aleksandrov 
2606ccbb0aa6SNikolay Aleksandrov static bool ipmr_rtm_validate_proto(unsigned char rtm_protocol)
2607ccbb0aa6SNikolay Aleksandrov {
2608ccbb0aa6SNikolay Aleksandrov 	switch (rtm_protocol) {
2609ccbb0aa6SNikolay Aleksandrov 	case RTPROT_STATIC:
2610ccbb0aa6SNikolay Aleksandrov 	case RTPROT_MROUTED:
2611ccbb0aa6SNikolay Aleksandrov 		return true;
2612ccbb0aa6SNikolay Aleksandrov 	}
2613ccbb0aa6SNikolay Aleksandrov 	return false;
2614ccbb0aa6SNikolay Aleksandrov }
2615ccbb0aa6SNikolay Aleksandrov 
2616ccbb0aa6SNikolay Aleksandrov static int ipmr_nla_get_ttls(const struct nlattr *nla, struct mfcctl *mfcc)
2617ccbb0aa6SNikolay Aleksandrov {
2618ccbb0aa6SNikolay Aleksandrov 	struct rtnexthop *rtnh = nla_data(nla);
2619ccbb0aa6SNikolay Aleksandrov 	int remaining = nla_len(nla), vifi = 0;
2620ccbb0aa6SNikolay Aleksandrov 
2621ccbb0aa6SNikolay Aleksandrov 	while (rtnh_ok(rtnh, remaining)) {
2622ccbb0aa6SNikolay Aleksandrov 		mfcc->mfcc_ttls[vifi] = rtnh->rtnh_hops;
2623ccbb0aa6SNikolay Aleksandrov 		if (++vifi == MAXVIFS)
2624ccbb0aa6SNikolay Aleksandrov 			break;
2625ccbb0aa6SNikolay Aleksandrov 		rtnh = rtnh_next(rtnh, &remaining);
2626ccbb0aa6SNikolay Aleksandrov 	}
2627ccbb0aa6SNikolay Aleksandrov 
2628ccbb0aa6SNikolay Aleksandrov 	return remaining > 0 ? -EINVAL : vifi;
2629ccbb0aa6SNikolay Aleksandrov }
2630ccbb0aa6SNikolay Aleksandrov 
2631ccbb0aa6SNikolay Aleksandrov /* returns < 0 on error, 0 for ADD_MFC and 1 for ADD_MFC_PROXY */
2632ccbb0aa6SNikolay Aleksandrov static int rtm_to_ipmr_mfcc(struct net *net, struct nlmsghdr *nlh,
2633ccbb0aa6SNikolay Aleksandrov 			    struct mfcctl *mfcc, int *mrtsock,
2634c21ef3e3SDavid Ahern 			    struct mr_table **mrtret,
2635c21ef3e3SDavid Ahern 			    struct netlink_ext_ack *extack)
2636ccbb0aa6SNikolay Aleksandrov {
2637ccbb0aa6SNikolay Aleksandrov 	struct net_device *dev = NULL;
2638ccbb0aa6SNikolay Aleksandrov 	u32 tblid = RT_TABLE_DEFAULT;
2639ccbb0aa6SNikolay Aleksandrov 	struct mr_table *mrt;
2640ccbb0aa6SNikolay Aleksandrov 	struct nlattr *attr;
2641ccbb0aa6SNikolay Aleksandrov 	struct rtmsg *rtm;
2642ccbb0aa6SNikolay Aleksandrov 	int ret, rem;
2643ccbb0aa6SNikolay Aleksandrov 
26448cb08174SJohannes Berg 	ret = nlmsg_validate_deprecated(nlh, sizeof(*rtm), RTA_MAX,
26458cb08174SJohannes Berg 					rtm_ipmr_policy, extack);
2646ccbb0aa6SNikolay Aleksandrov 	if (ret < 0)
2647ccbb0aa6SNikolay Aleksandrov 		goto out;
2648ccbb0aa6SNikolay Aleksandrov 	rtm = nlmsg_data(nlh);
2649ccbb0aa6SNikolay Aleksandrov 
2650ccbb0aa6SNikolay Aleksandrov 	ret = -EINVAL;
2651ccbb0aa6SNikolay Aleksandrov 	if (rtm->rtm_family != RTNL_FAMILY_IPMR || rtm->rtm_dst_len != 32 ||
2652ccbb0aa6SNikolay Aleksandrov 	    rtm->rtm_type != RTN_MULTICAST ||
2653ccbb0aa6SNikolay Aleksandrov 	    rtm->rtm_scope != RT_SCOPE_UNIVERSE ||
2654ccbb0aa6SNikolay Aleksandrov 	    !ipmr_rtm_validate_proto(rtm->rtm_protocol))
2655ccbb0aa6SNikolay Aleksandrov 		goto out;
2656ccbb0aa6SNikolay Aleksandrov 
2657ccbb0aa6SNikolay Aleksandrov 	memset(mfcc, 0, sizeof(*mfcc));
2658ccbb0aa6SNikolay Aleksandrov 	mfcc->mfcc_parent = -1;
2659ccbb0aa6SNikolay Aleksandrov 	ret = 0;
2660ccbb0aa6SNikolay Aleksandrov 	nlmsg_for_each_attr(attr, nlh, sizeof(struct rtmsg), rem) {
2661ccbb0aa6SNikolay Aleksandrov 		switch (nla_type(attr)) {
2662ccbb0aa6SNikolay Aleksandrov 		case RTA_SRC:
2663ccbb0aa6SNikolay Aleksandrov 			mfcc->mfcc_origin.s_addr = nla_get_be32(attr);
2664ccbb0aa6SNikolay Aleksandrov 			break;
2665ccbb0aa6SNikolay Aleksandrov 		case RTA_DST:
2666ccbb0aa6SNikolay Aleksandrov 			mfcc->mfcc_mcastgrp.s_addr = nla_get_be32(attr);
2667ccbb0aa6SNikolay Aleksandrov 			break;
2668ccbb0aa6SNikolay Aleksandrov 		case RTA_IIF:
2669ccbb0aa6SNikolay Aleksandrov 			dev = __dev_get_by_index(net, nla_get_u32(attr));
2670ccbb0aa6SNikolay Aleksandrov 			if (!dev) {
2671ccbb0aa6SNikolay Aleksandrov 				ret = -ENODEV;
2672ccbb0aa6SNikolay Aleksandrov 				goto out;
2673ccbb0aa6SNikolay Aleksandrov 			}
2674ccbb0aa6SNikolay Aleksandrov 			break;
2675ccbb0aa6SNikolay Aleksandrov 		case RTA_MULTIPATH:
2676ccbb0aa6SNikolay Aleksandrov 			if (ipmr_nla_get_ttls(attr, mfcc) < 0) {
2677ccbb0aa6SNikolay Aleksandrov 				ret = -EINVAL;
2678ccbb0aa6SNikolay Aleksandrov 				goto out;
2679ccbb0aa6SNikolay Aleksandrov 			}
2680ccbb0aa6SNikolay Aleksandrov 			break;
2681ccbb0aa6SNikolay Aleksandrov 		case RTA_PREFSRC:
2682ccbb0aa6SNikolay Aleksandrov 			ret = 1;
2683ccbb0aa6SNikolay Aleksandrov 			break;
2684ccbb0aa6SNikolay Aleksandrov 		case RTA_TABLE:
2685ccbb0aa6SNikolay Aleksandrov 			tblid = nla_get_u32(attr);
2686ccbb0aa6SNikolay Aleksandrov 			break;
2687ccbb0aa6SNikolay Aleksandrov 		}
2688ccbb0aa6SNikolay Aleksandrov 	}
2689ccbb0aa6SNikolay Aleksandrov 	mrt = ipmr_get_table(net, tblid);
2690ccbb0aa6SNikolay Aleksandrov 	if (!mrt) {
2691ccbb0aa6SNikolay Aleksandrov 		ret = -ENOENT;
2692ccbb0aa6SNikolay Aleksandrov 		goto out;
2693ccbb0aa6SNikolay Aleksandrov 	}
2694ccbb0aa6SNikolay Aleksandrov 	*mrtret = mrt;
2695ccbb0aa6SNikolay Aleksandrov 	*mrtsock = rtm->rtm_protocol == RTPROT_MROUTED ? 1 : 0;
2696ccbb0aa6SNikolay Aleksandrov 	if (dev)
2697ccbb0aa6SNikolay Aleksandrov 		mfcc->mfcc_parent = ipmr_find_vif(mrt, dev);
2698ccbb0aa6SNikolay Aleksandrov 
2699ccbb0aa6SNikolay Aleksandrov out:
2700ccbb0aa6SNikolay Aleksandrov 	return ret;
2701ccbb0aa6SNikolay Aleksandrov }
2702ccbb0aa6SNikolay Aleksandrov 
2703ccbb0aa6SNikolay Aleksandrov /* takes care of both newroute and delroute */
2704c21ef3e3SDavid Ahern static int ipmr_rtm_route(struct sk_buff *skb, struct nlmsghdr *nlh,
2705c21ef3e3SDavid Ahern 			  struct netlink_ext_ack *extack)
2706ccbb0aa6SNikolay Aleksandrov {
2707ccbb0aa6SNikolay Aleksandrov 	struct net *net = sock_net(skb->sk);
2708ccbb0aa6SNikolay Aleksandrov 	int ret, mrtsock, parent;
2709ccbb0aa6SNikolay Aleksandrov 	struct mr_table *tbl;
2710ccbb0aa6SNikolay Aleksandrov 	struct mfcctl mfcc;
2711ccbb0aa6SNikolay Aleksandrov 
2712ccbb0aa6SNikolay Aleksandrov 	mrtsock = 0;
2713ccbb0aa6SNikolay Aleksandrov 	tbl = NULL;
2714c21ef3e3SDavid Ahern 	ret = rtm_to_ipmr_mfcc(net, nlh, &mfcc, &mrtsock, &tbl, extack);
2715ccbb0aa6SNikolay Aleksandrov 	if (ret < 0)
2716ccbb0aa6SNikolay Aleksandrov 		return ret;
2717ccbb0aa6SNikolay Aleksandrov 
2718ccbb0aa6SNikolay Aleksandrov 	parent = ret ? mfcc.mfcc_parent : -1;
2719ccbb0aa6SNikolay Aleksandrov 	if (nlh->nlmsg_type == RTM_NEWROUTE)
2720ccbb0aa6SNikolay Aleksandrov 		return ipmr_mfc_add(net, tbl, &mfcc, mrtsock, parent);
2721ccbb0aa6SNikolay Aleksandrov 	else
2722ccbb0aa6SNikolay Aleksandrov 		return ipmr_mfc_delete(tbl, &mfcc, parent);
2723ccbb0aa6SNikolay Aleksandrov }
2724ccbb0aa6SNikolay Aleksandrov 
2725772c344dSNikolay Aleksandrov static bool ipmr_fill_table(struct mr_table *mrt, struct sk_buff *skb)
2726772c344dSNikolay Aleksandrov {
2727772c344dSNikolay Aleksandrov 	u32 queue_len = atomic_read(&mrt->cache_resolve_queue_len);
2728772c344dSNikolay Aleksandrov 
2729772c344dSNikolay Aleksandrov 	if (nla_put_u32(skb, IPMRA_TABLE_ID, mrt->id) ||
2730772c344dSNikolay Aleksandrov 	    nla_put_u32(skb, IPMRA_TABLE_CACHE_RES_QUEUE_LEN, queue_len) ||
2731772c344dSNikolay Aleksandrov 	    nla_put_s32(skb, IPMRA_TABLE_MROUTE_REG_VIF_NUM,
2732772c344dSNikolay Aleksandrov 			mrt->mroute_reg_vif_num) ||
2733772c344dSNikolay Aleksandrov 	    nla_put_u8(skb, IPMRA_TABLE_MROUTE_DO_ASSERT,
2734772c344dSNikolay Aleksandrov 		       mrt->mroute_do_assert) ||
2735c921c207SNikolay Aleksandrov 	    nla_put_u8(skb, IPMRA_TABLE_MROUTE_DO_PIM, mrt->mroute_do_pim) ||
2736c921c207SNikolay Aleksandrov 	    nla_put_u8(skb, IPMRA_TABLE_MROUTE_DO_WRVIFWHOLE,
2737c921c207SNikolay Aleksandrov 		       mrt->mroute_do_wrvifwhole))
2738772c344dSNikolay Aleksandrov 		return false;
2739772c344dSNikolay Aleksandrov 
2740772c344dSNikolay Aleksandrov 	return true;
2741772c344dSNikolay Aleksandrov }
2742772c344dSNikolay Aleksandrov 
2743772c344dSNikolay Aleksandrov static bool ipmr_fill_vif(struct mr_table *mrt, u32 vifid, struct sk_buff *skb)
2744772c344dSNikolay Aleksandrov {
2745772c344dSNikolay Aleksandrov 	struct nlattr *vif_nest;
2746772c344dSNikolay Aleksandrov 	struct vif_device *vif;
2747772c344dSNikolay Aleksandrov 
2748772c344dSNikolay Aleksandrov 	/* if the VIF doesn't exist just continue */
2749772c344dSNikolay Aleksandrov 	if (!VIF_EXISTS(mrt, vifid))
2750772c344dSNikolay Aleksandrov 		return true;
2751772c344dSNikolay Aleksandrov 
2752772c344dSNikolay Aleksandrov 	vif = &mrt->vif_table[vifid];
2753ae0be8deSMichal Kubecek 	vif_nest = nla_nest_start_noflag(skb, IPMRA_VIF);
2754772c344dSNikolay Aleksandrov 	if (!vif_nest)
2755772c344dSNikolay Aleksandrov 		return false;
2756772c344dSNikolay Aleksandrov 	if (nla_put_u32(skb, IPMRA_VIFA_IFINDEX, vif->dev->ifindex) ||
2757772c344dSNikolay Aleksandrov 	    nla_put_u32(skb, IPMRA_VIFA_VIF_ID, vifid) ||
2758772c344dSNikolay Aleksandrov 	    nla_put_u16(skb, IPMRA_VIFA_FLAGS, vif->flags) ||
2759772c344dSNikolay Aleksandrov 	    nla_put_u64_64bit(skb, IPMRA_VIFA_BYTES_IN, vif->bytes_in,
2760772c344dSNikolay Aleksandrov 			      IPMRA_VIFA_PAD) ||
2761772c344dSNikolay Aleksandrov 	    nla_put_u64_64bit(skb, IPMRA_VIFA_BYTES_OUT, vif->bytes_out,
2762772c344dSNikolay Aleksandrov 			      IPMRA_VIFA_PAD) ||
2763772c344dSNikolay Aleksandrov 	    nla_put_u64_64bit(skb, IPMRA_VIFA_PACKETS_IN, vif->pkt_in,
2764772c344dSNikolay Aleksandrov 			      IPMRA_VIFA_PAD) ||
2765772c344dSNikolay Aleksandrov 	    nla_put_u64_64bit(skb, IPMRA_VIFA_PACKETS_OUT, vif->pkt_out,
2766772c344dSNikolay Aleksandrov 			      IPMRA_VIFA_PAD) ||
2767772c344dSNikolay Aleksandrov 	    nla_put_be32(skb, IPMRA_VIFA_LOCAL_ADDR, vif->local) ||
2768772c344dSNikolay Aleksandrov 	    nla_put_be32(skb, IPMRA_VIFA_REMOTE_ADDR, vif->remote)) {
2769772c344dSNikolay Aleksandrov 		nla_nest_cancel(skb, vif_nest);
2770772c344dSNikolay Aleksandrov 		return false;
2771772c344dSNikolay Aleksandrov 	}
2772772c344dSNikolay Aleksandrov 	nla_nest_end(skb, vif_nest);
2773772c344dSNikolay Aleksandrov 
2774772c344dSNikolay Aleksandrov 	return true;
2775772c344dSNikolay Aleksandrov }
2776772c344dSNikolay Aleksandrov 
277714fc5bb2SDavid Ahern static int ipmr_valid_dumplink(const struct nlmsghdr *nlh,
277814fc5bb2SDavid Ahern 			       struct netlink_ext_ack *extack)
277914fc5bb2SDavid Ahern {
278014fc5bb2SDavid Ahern 	struct ifinfomsg *ifm;
278114fc5bb2SDavid Ahern 
278214fc5bb2SDavid Ahern 	if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
278314fc5bb2SDavid Ahern 		NL_SET_ERR_MSG(extack, "ipv4: Invalid header for ipmr link dump");
278414fc5bb2SDavid Ahern 		return -EINVAL;
278514fc5bb2SDavid Ahern 	}
278614fc5bb2SDavid Ahern 
278714fc5bb2SDavid Ahern 	if (nlmsg_attrlen(nlh, sizeof(*ifm))) {
278814fc5bb2SDavid Ahern 		NL_SET_ERR_MSG(extack, "Invalid data after header in ipmr link dump");
278914fc5bb2SDavid Ahern 		return -EINVAL;
279014fc5bb2SDavid Ahern 	}
279114fc5bb2SDavid Ahern 
279214fc5bb2SDavid Ahern 	ifm = nlmsg_data(nlh);
279314fc5bb2SDavid Ahern 	if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
279414fc5bb2SDavid Ahern 	    ifm->ifi_change || ifm->ifi_index) {
279514fc5bb2SDavid Ahern 		NL_SET_ERR_MSG(extack, "Invalid values in header for ipmr link dump request");
279614fc5bb2SDavid Ahern 		return -EINVAL;
279714fc5bb2SDavid Ahern 	}
279814fc5bb2SDavid Ahern 
279914fc5bb2SDavid Ahern 	return 0;
280014fc5bb2SDavid Ahern }
280114fc5bb2SDavid Ahern 
2802772c344dSNikolay Aleksandrov static int ipmr_rtm_dumplink(struct sk_buff *skb, struct netlink_callback *cb)
2803772c344dSNikolay Aleksandrov {
2804772c344dSNikolay Aleksandrov 	struct net *net = sock_net(skb->sk);
2805772c344dSNikolay Aleksandrov 	struct nlmsghdr *nlh = NULL;
2806772c344dSNikolay Aleksandrov 	unsigned int t = 0, s_t;
2807772c344dSNikolay Aleksandrov 	unsigned int e = 0, s_e;
2808772c344dSNikolay Aleksandrov 	struct mr_table *mrt;
2809772c344dSNikolay Aleksandrov 
281014fc5bb2SDavid Ahern 	if (cb->strict_check) {
281114fc5bb2SDavid Ahern 		int err = ipmr_valid_dumplink(cb->nlh, cb->extack);
281214fc5bb2SDavid Ahern 
281314fc5bb2SDavid Ahern 		if (err < 0)
281414fc5bb2SDavid Ahern 			return err;
281514fc5bb2SDavid Ahern 	}
281614fc5bb2SDavid Ahern 
2817772c344dSNikolay Aleksandrov 	s_t = cb->args[0];
2818772c344dSNikolay Aleksandrov 	s_e = cb->args[1];
2819772c344dSNikolay Aleksandrov 
2820772c344dSNikolay Aleksandrov 	ipmr_for_each_table(mrt, net) {
2821772c344dSNikolay Aleksandrov 		struct nlattr *vifs, *af;
2822772c344dSNikolay Aleksandrov 		struct ifinfomsg *hdr;
2823772c344dSNikolay Aleksandrov 		u32 i;
2824772c344dSNikolay Aleksandrov 
2825772c344dSNikolay Aleksandrov 		if (t < s_t)
2826772c344dSNikolay Aleksandrov 			goto skip_table;
2827772c344dSNikolay Aleksandrov 		nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid,
2828772c344dSNikolay Aleksandrov 				cb->nlh->nlmsg_seq, RTM_NEWLINK,
2829772c344dSNikolay Aleksandrov 				sizeof(*hdr), NLM_F_MULTI);
2830772c344dSNikolay Aleksandrov 		if (!nlh)
2831772c344dSNikolay Aleksandrov 			break;
2832772c344dSNikolay Aleksandrov 
2833772c344dSNikolay Aleksandrov 		hdr = nlmsg_data(nlh);
2834772c344dSNikolay Aleksandrov 		memset(hdr, 0, sizeof(*hdr));
2835772c344dSNikolay Aleksandrov 		hdr->ifi_family = RTNL_FAMILY_IPMR;
2836772c344dSNikolay Aleksandrov 
2837ae0be8deSMichal Kubecek 		af = nla_nest_start_noflag(skb, IFLA_AF_SPEC);
2838772c344dSNikolay Aleksandrov 		if (!af) {
2839772c344dSNikolay Aleksandrov 			nlmsg_cancel(skb, nlh);
2840772c344dSNikolay Aleksandrov 			goto out;
2841772c344dSNikolay Aleksandrov 		}
2842772c344dSNikolay Aleksandrov 
2843772c344dSNikolay Aleksandrov 		if (!ipmr_fill_table(mrt, skb)) {
2844772c344dSNikolay Aleksandrov 			nlmsg_cancel(skb, nlh);
2845772c344dSNikolay Aleksandrov 			goto out;
2846772c344dSNikolay Aleksandrov 		}
2847772c344dSNikolay Aleksandrov 
2848ae0be8deSMichal Kubecek 		vifs = nla_nest_start_noflag(skb, IPMRA_TABLE_VIFS);
2849772c344dSNikolay Aleksandrov 		if (!vifs) {
2850772c344dSNikolay Aleksandrov 			nla_nest_end(skb, af);
2851772c344dSNikolay Aleksandrov 			nlmsg_end(skb, nlh);
2852772c344dSNikolay Aleksandrov 			goto out;
2853772c344dSNikolay Aleksandrov 		}
2854772c344dSNikolay Aleksandrov 		for (i = 0; i < mrt->maxvif; i++) {
2855772c344dSNikolay Aleksandrov 			if (e < s_e)
2856772c344dSNikolay Aleksandrov 				goto skip_entry;
2857772c344dSNikolay Aleksandrov 			if (!ipmr_fill_vif(mrt, i, skb)) {
2858772c344dSNikolay Aleksandrov 				nla_nest_end(skb, vifs);
2859772c344dSNikolay Aleksandrov 				nla_nest_end(skb, af);
2860772c344dSNikolay Aleksandrov 				nlmsg_end(skb, nlh);
2861772c344dSNikolay Aleksandrov 				goto out;
2862772c344dSNikolay Aleksandrov 			}
2863772c344dSNikolay Aleksandrov skip_entry:
2864772c344dSNikolay Aleksandrov 			e++;
2865772c344dSNikolay Aleksandrov 		}
2866772c344dSNikolay Aleksandrov 		s_e = 0;
2867772c344dSNikolay Aleksandrov 		e = 0;
2868772c344dSNikolay Aleksandrov 		nla_nest_end(skb, vifs);
2869772c344dSNikolay Aleksandrov 		nla_nest_end(skb, af);
2870772c344dSNikolay Aleksandrov 		nlmsg_end(skb, nlh);
2871772c344dSNikolay Aleksandrov skip_table:
2872772c344dSNikolay Aleksandrov 		t++;
2873772c344dSNikolay Aleksandrov 	}
2874772c344dSNikolay Aleksandrov 
2875772c344dSNikolay Aleksandrov out:
2876772c344dSNikolay Aleksandrov 	cb->args[1] = e;
2877772c344dSNikolay Aleksandrov 	cb->args[0] = t;
2878772c344dSNikolay Aleksandrov 
2879772c344dSNikolay Aleksandrov 	return skb->len;
2880772c344dSNikolay Aleksandrov }
2881772c344dSNikolay Aleksandrov 
28821da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
28837ef8f65dSNikolay Aleksandrov /* The /proc interfaces to multicast routing :
2884a8cb16ddSEric Dumazet  * /proc/net/ip_mr_cache & /proc/net/ip_mr_vif
28851da177e4SLinus Torvalds  */
28861da177e4SLinus Torvalds 
28871da177e4SLinus Torvalds static void *ipmr_vif_seq_start(struct seq_file *seq, loff_t *pos)
2888ba93ef74SStephen Hemminger 	__acquires(mrt_lock)
28891da177e4SLinus Torvalds {
28903feda6b4SYuval Mintz 	struct mr_vif_iter *iter = seq->private;
2891f6bb4514SBenjamin Thery 	struct net *net = seq_file_net(seq);
2892f0ad0860SPatrick McHardy 	struct mr_table *mrt;
2893f0ad0860SPatrick McHardy 
2894f0ad0860SPatrick McHardy 	mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
289551456b29SIan Morris 	if (!mrt)
2896f0ad0860SPatrick McHardy 		return ERR_PTR(-ENOENT);
2897f0ad0860SPatrick McHardy 
2898f0ad0860SPatrick McHardy 	iter->mrt = mrt;
2899f6bb4514SBenjamin Thery 
29001da177e4SLinus Torvalds 	read_lock(&mrt_lock);
29013feda6b4SYuval Mintz 	return mr_vif_seq_start(seq, pos);
29021da177e4SLinus Torvalds }
29031da177e4SLinus Torvalds 
29041da177e4SLinus Torvalds static void ipmr_vif_seq_stop(struct seq_file *seq, void *v)
2905ba93ef74SStephen Hemminger 	__releases(mrt_lock)
29061da177e4SLinus Torvalds {
29071da177e4SLinus Torvalds 	read_unlock(&mrt_lock);
29081da177e4SLinus Torvalds }
29091da177e4SLinus Torvalds 
29101da177e4SLinus Torvalds static int ipmr_vif_seq_show(struct seq_file *seq, void *v)
29111da177e4SLinus Torvalds {
29123feda6b4SYuval Mintz 	struct mr_vif_iter *iter = seq->private;
2913f0ad0860SPatrick McHardy 	struct mr_table *mrt = iter->mrt;
2914f6bb4514SBenjamin Thery 
29151da177e4SLinus Torvalds 	if (v == SEQ_START_TOKEN) {
29161da177e4SLinus Torvalds 		seq_puts(seq,
29171da177e4SLinus Torvalds 			 "Interface      BytesIn  PktsIn  BytesOut PktsOut Flags Local    Remote\n");
29181da177e4SLinus Torvalds 	} else {
29191da177e4SLinus Torvalds 		const struct vif_device *vif = v;
29206853f21fSYuval Mintz 		const char *name =  vif->dev ?
29216853f21fSYuval Mintz 				    vif->dev->name : "none";
29221da177e4SLinus Torvalds 
29231da177e4SLinus Torvalds 		seq_printf(seq,
292491e6dd82SJames Hogan 			   "%2td %-10s %8ld %7ld  %8ld %7ld %05X %08X %08X\n",
29250c12295aSPatrick McHardy 			   vif - mrt->vif_table,
29261da177e4SLinus Torvalds 			   name, vif->bytes_in, vif->pkt_in,
29271da177e4SLinus Torvalds 			   vif->bytes_out, vif->pkt_out,
29281da177e4SLinus Torvalds 			   vif->flags, vif->local, vif->remote);
29291da177e4SLinus Torvalds 	}
29301da177e4SLinus Torvalds 	return 0;
29311da177e4SLinus Torvalds }
29321da177e4SLinus Torvalds 
2933f690808eSStephen Hemminger static const struct seq_operations ipmr_vif_seq_ops = {
29341da177e4SLinus Torvalds 	.start = ipmr_vif_seq_start,
29353feda6b4SYuval Mintz 	.next  = mr_vif_seq_next,
29361da177e4SLinus Torvalds 	.stop  = ipmr_vif_seq_stop,
29371da177e4SLinus Torvalds 	.show  = ipmr_vif_seq_show,
29381da177e4SLinus Torvalds };
29391da177e4SLinus Torvalds 
29401da177e4SLinus Torvalds static void *ipmr_mfc_seq_start(struct seq_file *seq, loff_t *pos)
29411da177e4SLinus Torvalds {
2942f6bb4514SBenjamin Thery 	struct net *net = seq_file_net(seq);
2943f0ad0860SPatrick McHardy 	struct mr_table *mrt;
2944f6bb4514SBenjamin Thery 
2945f0ad0860SPatrick McHardy 	mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
294651456b29SIan Morris 	if (!mrt)
2947f0ad0860SPatrick McHardy 		return ERR_PTR(-ENOENT);
2948f0ad0860SPatrick McHardy 
2949c8d61968SYuval Mintz 	return mr_mfc_seq_start(seq, pos, mrt, &mfc_unres_lock);
29501da177e4SLinus Torvalds }
29511da177e4SLinus Torvalds 
29521da177e4SLinus Torvalds static int ipmr_mfc_seq_show(struct seq_file *seq, void *v)
29531da177e4SLinus Torvalds {
29541da177e4SLinus Torvalds 	int n;
29551da177e4SLinus Torvalds 
29561da177e4SLinus Torvalds 	if (v == SEQ_START_TOKEN) {
29571da177e4SLinus Torvalds 		seq_puts(seq,
29581da177e4SLinus Torvalds 		 "Group    Origin   Iif     Pkts    Bytes    Wrong Oifs\n");
29591da177e4SLinus Torvalds 	} else {
29601da177e4SLinus Torvalds 		const struct mfc_cache *mfc = v;
2961c8d61968SYuval Mintz 		const struct mr_mfc_iter *it = seq->private;
2962f0ad0860SPatrick McHardy 		const struct mr_table *mrt = it->mrt;
29631da177e4SLinus Torvalds 
29640eae88f3SEric Dumazet 		seq_printf(seq, "%08X %08X %-3hd",
29650eae88f3SEric Dumazet 			   (__force u32) mfc->mfc_mcastgrp,
29660eae88f3SEric Dumazet 			   (__force u32) mfc->mfc_origin,
2967494fff56SYuval Mintz 			   mfc->_c.mfc_parent);
29681ea472e2SBenjamin Thery 
29690c12295aSPatrick McHardy 		if (it->cache != &mrt->mfc_unres_queue) {
29701ea472e2SBenjamin Thery 			seq_printf(seq, " %8lu %8lu %8lu",
2971494fff56SYuval Mintz 				   mfc->_c.mfc_un.res.pkt,
2972494fff56SYuval Mintz 				   mfc->_c.mfc_un.res.bytes,
2973494fff56SYuval Mintz 				   mfc->_c.mfc_un.res.wrong_if);
2974494fff56SYuval Mintz 			for (n = mfc->_c.mfc_un.res.minvif;
2975494fff56SYuval Mintz 			     n < mfc->_c.mfc_un.res.maxvif; n++) {
29760c12295aSPatrick McHardy 				if (VIF_EXISTS(mrt, n) &&
2977494fff56SYuval Mintz 				    mfc->_c.mfc_un.res.ttls[n] < 255)
29781da177e4SLinus Torvalds 					seq_printf(seq,
29791da177e4SLinus Torvalds 					   " %2d:%-3d",
2980494fff56SYuval Mintz 					   n, mfc->_c.mfc_un.res.ttls[n]);
29811da177e4SLinus Torvalds 			}
29821ea472e2SBenjamin Thery 		} else {
29831ea472e2SBenjamin Thery 			/* unresolved mfc_caches don't contain
29841ea472e2SBenjamin Thery 			 * pkt, bytes and wrong_if values
29851ea472e2SBenjamin Thery 			 */
29861ea472e2SBenjamin Thery 			seq_printf(seq, " %8lu %8lu %8lu", 0ul, 0ul, 0ul);
29871da177e4SLinus Torvalds 		}
29881da177e4SLinus Torvalds 		seq_putc(seq, '\n');
29891da177e4SLinus Torvalds 	}
29901da177e4SLinus Torvalds 	return 0;
29911da177e4SLinus Torvalds }
29921da177e4SLinus Torvalds 
2993f690808eSStephen Hemminger static const struct seq_operations ipmr_mfc_seq_ops = {
29941da177e4SLinus Torvalds 	.start = ipmr_mfc_seq_start,
2995c8d61968SYuval Mintz 	.next  = mr_mfc_seq_next,
2996c8d61968SYuval Mintz 	.stop  = mr_mfc_seq_stop,
29971da177e4SLinus Torvalds 	.show  = ipmr_mfc_seq_show,
29981da177e4SLinus Torvalds };
29991da177e4SLinus Torvalds #endif
30001da177e4SLinus Torvalds 
30011da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM_V2
300232613090SAlexey Dobriyan static const struct net_protocol pim_protocol = {
30031da177e4SLinus Torvalds 	.handler	=	pim_rcv,
30041da177e4SLinus Torvalds };
30051da177e4SLinus Torvalds #endif
30061da177e4SLinus Torvalds 
30074d65b948SYotam Gigi static unsigned int ipmr_seq_read(struct net *net)
30084d65b948SYotam Gigi {
30094d65b948SYotam Gigi 	ASSERT_RTNL();
30104d65b948SYotam Gigi 
30114d65b948SYotam Gigi 	return net->ipv4.ipmr_seq + ipmr_rules_seq_read(net);
30124d65b948SYotam Gigi }
30134d65b948SYotam Gigi 
3014b7a59557SJiri Pirko static int ipmr_dump(struct net *net, struct notifier_block *nb,
3015b7a59557SJiri Pirko 		     struct netlink_ext_ack *extack)
30164d65b948SYotam Gigi {
3017cdc9f944SYuval Mintz 	return mr_dump(net, nb, RTNL_FAMILY_IPMR, ipmr_rules_dump,
3018b7a59557SJiri Pirko 		       ipmr_mr_table_iter, &mrt_lock, extack);
30194d65b948SYotam Gigi }
30204d65b948SYotam Gigi 
30214d65b948SYotam Gigi static const struct fib_notifier_ops ipmr_notifier_ops_template = {
30224d65b948SYotam Gigi 	.family		= RTNL_FAMILY_IPMR,
30234d65b948SYotam Gigi 	.fib_seq_read	= ipmr_seq_read,
30244d65b948SYotam Gigi 	.fib_dump	= ipmr_dump,
30254d65b948SYotam Gigi 	.owner		= THIS_MODULE,
30264d65b948SYotam Gigi };
30274d65b948SYotam Gigi 
3028ef739d8aSColin Ian King static int __net_init ipmr_notifier_init(struct net *net)
30294d65b948SYotam Gigi {
30304d65b948SYotam Gigi 	struct fib_notifier_ops *ops;
30314d65b948SYotam Gigi 
30324d65b948SYotam Gigi 	net->ipv4.ipmr_seq = 0;
30334d65b948SYotam Gigi 
30344d65b948SYotam Gigi 	ops = fib_notifier_ops_register(&ipmr_notifier_ops_template, net);
30354d65b948SYotam Gigi 	if (IS_ERR(ops))
30364d65b948SYotam Gigi 		return PTR_ERR(ops);
30374d65b948SYotam Gigi 	net->ipv4.ipmr_notifier_ops = ops;
30384d65b948SYotam Gigi 
30394d65b948SYotam Gigi 	return 0;
30404d65b948SYotam Gigi }
30414d65b948SYotam Gigi 
30424d65b948SYotam Gigi static void __net_exit ipmr_notifier_exit(struct net *net)
30434d65b948SYotam Gigi {
30444d65b948SYotam Gigi 	fib_notifier_ops_unregister(net->ipv4.ipmr_notifier_ops);
30454d65b948SYotam Gigi 	net->ipv4.ipmr_notifier_ops = NULL;
30464d65b948SYotam Gigi }
30474d65b948SYotam Gigi 
30487ef8f65dSNikolay Aleksandrov /* Setup for IP multicast routing */
3049cf958ae3SBenjamin Thery static int __net_init ipmr_net_init(struct net *net)
3050cf958ae3SBenjamin Thery {
3051f0ad0860SPatrick McHardy 	int err;
3052cf958ae3SBenjamin Thery 
30534d65b948SYotam Gigi 	err = ipmr_notifier_init(net);
30544d65b948SYotam Gigi 	if (err)
30554d65b948SYotam Gigi 		goto ipmr_notifier_fail;
30564d65b948SYotam Gigi 
3057f0ad0860SPatrick McHardy 	err = ipmr_rules_init(net);
3058f0ad0860SPatrick McHardy 	if (err < 0)
30594d65b948SYotam Gigi 		goto ipmr_rules_fail;
3060f6bb4514SBenjamin Thery 
3061f6bb4514SBenjamin Thery #ifdef CONFIG_PROC_FS
3062f6bb4514SBenjamin Thery 	err = -ENOMEM;
3063c3506372SChristoph Hellwig 	if (!proc_create_net("ip_mr_vif", 0, net->proc_net, &ipmr_vif_seq_ops,
3064c3506372SChristoph Hellwig 			sizeof(struct mr_vif_iter)))
3065f6bb4514SBenjamin Thery 		goto proc_vif_fail;
3066c3506372SChristoph Hellwig 	if (!proc_create_net("ip_mr_cache", 0, net->proc_net, &ipmr_mfc_seq_ops,
3067c3506372SChristoph Hellwig 			sizeof(struct mr_mfc_iter)))
3068f6bb4514SBenjamin Thery 		goto proc_cache_fail;
3069f6bb4514SBenjamin Thery #endif
30702bb8b26cSBenjamin Thery 	return 0;
30712bb8b26cSBenjamin Thery 
3072f6bb4514SBenjamin Thery #ifdef CONFIG_PROC_FS
3073f6bb4514SBenjamin Thery proc_cache_fail:
3074ece31ffdSGao feng 	remove_proc_entry("ip_mr_vif", net->proc_net);
3075f6bb4514SBenjamin Thery proc_vif_fail:
3076*696e595fSEric Dumazet 	rtnl_lock();
3077f0ad0860SPatrick McHardy 	ipmr_rules_exit(net);
3078*696e595fSEric Dumazet 	rtnl_unlock();
3079f6bb4514SBenjamin Thery #endif
30804d65b948SYotam Gigi ipmr_rules_fail:
30814d65b948SYotam Gigi 	ipmr_notifier_exit(net);
30824d65b948SYotam Gigi ipmr_notifier_fail:
3083cf958ae3SBenjamin Thery 	return err;
3084cf958ae3SBenjamin Thery }
3085cf958ae3SBenjamin Thery 
3086cf958ae3SBenjamin Thery static void __net_exit ipmr_net_exit(struct net *net)
3087cf958ae3SBenjamin Thery {
3088f6bb4514SBenjamin Thery #ifdef CONFIG_PROC_FS
3089ece31ffdSGao feng 	remove_proc_entry("ip_mr_cache", net->proc_net);
3090ece31ffdSGao feng 	remove_proc_entry("ip_mr_vif", net->proc_net);
3091f6bb4514SBenjamin Thery #endif
30924d65b948SYotam Gigi 	ipmr_notifier_exit(net);
3093*696e595fSEric Dumazet }
3094*696e595fSEric Dumazet 
3095*696e595fSEric Dumazet static void __net_exit ipmr_net_exit_batch(struct list_head *net_list)
3096*696e595fSEric Dumazet {
3097*696e595fSEric Dumazet 	struct net *net;
3098*696e595fSEric Dumazet 
3099*696e595fSEric Dumazet 	rtnl_lock();
3100*696e595fSEric Dumazet 	list_for_each_entry(net, net_list, exit_list)
3101f0ad0860SPatrick McHardy 		ipmr_rules_exit(net);
3102*696e595fSEric Dumazet 	rtnl_unlock();
3103cf958ae3SBenjamin Thery }
3104cf958ae3SBenjamin Thery 
3105cf958ae3SBenjamin Thery static struct pernet_operations ipmr_net_ops = {
3106cf958ae3SBenjamin Thery 	.init = ipmr_net_init,
3107cf958ae3SBenjamin Thery 	.exit = ipmr_net_exit,
3108*696e595fSEric Dumazet 	.exit_batch = ipmr_net_exit_batch,
3109cf958ae3SBenjamin Thery };
31101da177e4SLinus Torvalds 
311103d2f897SWang Chen int __init ip_mr_init(void)
31121da177e4SLinus Torvalds {
311303d2f897SWang Chen 	int err;
311403d2f897SWang Chen 
31151da177e4SLinus Torvalds 	mrt_cachep = kmem_cache_create("ip_mrt_cache",
31161da177e4SLinus Torvalds 				       sizeof(struct mfc_cache),
3117e5d679f3SAlexey Dobriyan 				       0, SLAB_HWCACHE_ALIGN | SLAB_PANIC,
311820c2df83SPaul Mundt 				       NULL);
311903d2f897SWang Chen 
3120cf958ae3SBenjamin Thery 	err = register_pernet_subsys(&ipmr_net_ops);
3121cf958ae3SBenjamin Thery 	if (err)
3122cf958ae3SBenjamin Thery 		goto reg_pernet_fail;
3123cf958ae3SBenjamin Thery 
312403d2f897SWang Chen 	err = register_netdevice_notifier(&ip_mr_notifier);
312503d2f897SWang Chen 	if (err)
312603d2f897SWang Chen 		goto reg_notif_fail;
3127403dbb97STom Goff #ifdef CONFIG_IP_PIMSM_V2
3128403dbb97STom Goff 	if (inet_add_protocol(&pim_protocol, IPPROTO_PIM) < 0) {
3129058bd4d2SJoe Perches 		pr_err("%s: can't add PIM protocol\n", __func__);
3130403dbb97STom Goff 		err = -EAGAIN;
3131403dbb97STom Goff 		goto add_proto_fail;
3132403dbb97STom Goff 	}
3133403dbb97STom Goff #endif
3134c7ac8679SGreg Rose 	rtnl_register(RTNL_FAMILY_IPMR, RTM_GETROUTE,
3135b97bac64SFlorian Westphal 		      ipmr_rtm_getroute, ipmr_rtm_dumproute, 0);
3136ccbb0aa6SNikolay Aleksandrov 	rtnl_register(RTNL_FAMILY_IPMR, RTM_NEWROUTE,
3137b97bac64SFlorian Westphal 		      ipmr_rtm_route, NULL, 0);
3138ccbb0aa6SNikolay Aleksandrov 	rtnl_register(RTNL_FAMILY_IPMR, RTM_DELROUTE,
3139b97bac64SFlorian Westphal 		      ipmr_rtm_route, NULL, 0);
3140772c344dSNikolay Aleksandrov 
3141772c344dSNikolay Aleksandrov 	rtnl_register(RTNL_FAMILY_IPMR, RTM_GETLINK,
3142b97bac64SFlorian Westphal 		      NULL, ipmr_rtm_dumplink, 0);
314303d2f897SWang Chen 	return 0;
3144f6bb4514SBenjamin Thery 
3145403dbb97STom Goff #ifdef CONFIG_IP_PIMSM_V2
3146403dbb97STom Goff add_proto_fail:
3147403dbb97STom Goff 	unregister_netdevice_notifier(&ip_mr_notifier);
3148403dbb97STom Goff #endif
3149c3e38896SBenjamin Thery reg_notif_fail:
3150cf958ae3SBenjamin Thery 	unregister_pernet_subsys(&ipmr_net_ops);
3151cf958ae3SBenjamin Thery reg_pernet_fail:
3152c3e38896SBenjamin Thery 	kmem_cache_destroy(mrt_cachep);
315303d2f897SWang Chen 	return err;
31541da177e4SLinus Torvalds }
3155