xref: /linux/net/ipv4/ipmr.c (revision c4854ec8c483fb8deea7400c11cd7fd7c03e306c)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *	IP multicast routing support for mrouted 3.6/3.8
31da177e4SLinus Torvalds  *
4113aa838SAlan Cox  *		(c) 1995 Alan Cox, <alan@lxorguk.ukuu.org.uk>
51da177e4SLinus Torvalds  *	  Linux Consultancy and Custom Driver Development
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  *	This program is free software; you can redistribute it and/or
81da177e4SLinus Torvalds  *	modify it under the terms of the GNU General Public License
91da177e4SLinus Torvalds  *	as published by the Free Software Foundation; either version
101da177e4SLinus Torvalds  *	2 of the License, or (at your option) any later version.
111da177e4SLinus Torvalds  *
121da177e4SLinus Torvalds  *	Fixes:
131da177e4SLinus Torvalds  *	Michael Chastain	:	Incorrect size of copying.
141da177e4SLinus Torvalds  *	Alan Cox		:	Added the cache manager code
151da177e4SLinus Torvalds  *	Alan Cox		:	Fixed the clone/copy bug and device race.
161da177e4SLinus Torvalds  *	Mike McLagan		:	Routing by source
171da177e4SLinus Torvalds  *	Malcolm Beattie		:	Buffer handling fixes.
181da177e4SLinus Torvalds  *	Alexey Kuznetsov	:	Double buffer free and other fixes.
191da177e4SLinus Torvalds  *	SVR Anand		:	Fixed several multicast bugs and problems.
201da177e4SLinus Torvalds  *	Alexey Kuznetsov	:	Status, optimisations and more.
211da177e4SLinus Torvalds  *	Brad Parker		:	Better behaviour on mrouted upcall
221da177e4SLinus Torvalds  *					overflow.
231da177e4SLinus Torvalds  *      Carlos Picoto           :       PIMv1 Support
241da177e4SLinus Torvalds  *	Pavlin Ivanov Radoslavov:	PIMv2 Registers must checksum only PIM header
25f77f13e2SGilles Espinasse  *					Relax this requirement to work with older peers.
261da177e4SLinus Torvalds  *
271da177e4SLinus Torvalds  */
281da177e4SLinus Torvalds 
291da177e4SLinus Torvalds #include <asm/uaccess.h>
301da177e4SLinus Torvalds #include <linux/types.h>
314fc268d2SRandy Dunlap #include <linux/capability.h>
321da177e4SLinus Torvalds #include <linux/errno.h>
331da177e4SLinus Torvalds #include <linux/timer.h>
341da177e4SLinus Torvalds #include <linux/mm.h>
351da177e4SLinus Torvalds #include <linux/kernel.h>
361da177e4SLinus Torvalds #include <linux/fcntl.h>
371da177e4SLinus Torvalds #include <linux/stat.h>
381da177e4SLinus Torvalds #include <linux/socket.h>
391da177e4SLinus Torvalds #include <linux/in.h>
401da177e4SLinus Torvalds #include <linux/inet.h>
411da177e4SLinus Torvalds #include <linux/netdevice.h>
421da177e4SLinus Torvalds #include <linux/inetdevice.h>
431da177e4SLinus Torvalds #include <linux/igmp.h>
441da177e4SLinus Torvalds #include <linux/proc_fs.h>
451da177e4SLinus Torvalds #include <linux/seq_file.h>
461da177e4SLinus Torvalds #include <linux/mroute.h>
471da177e4SLinus Torvalds #include <linux/init.h>
4846f25dffSKris Katterjohn #include <linux/if_ether.h>
495a0e3ad6STejun Heo #include <linux/slab.h>
50457c4cbcSEric W. Biederman #include <net/net_namespace.h>
511da177e4SLinus Torvalds #include <net/ip.h>
521da177e4SLinus Torvalds #include <net/protocol.h>
531da177e4SLinus Torvalds #include <linux/skbuff.h>
5414c85021SArnaldo Carvalho de Melo #include <net/route.h>
551da177e4SLinus Torvalds #include <net/sock.h>
561da177e4SLinus Torvalds #include <net/icmp.h>
571da177e4SLinus Torvalds #include <net/udp.h>
581da177e4SLinus Torvalds #include <net/raw.h>
591da177e4SLinus Torvalds #include <linux/notifier.h>
601da177e4SLinus Torvalds #include <linux/if_arp.h>
611da177e4SLinus Torvalds #include <linux/netfilter_ipv4.h>
62709b46e8SEric W. Biederman #include <linux/compat.h>
63bc3b2d7fSPaul Gortmaker #include <linux/export.h>
64c5441932SPravin B Shelar #include <net/ip_tunnels.h>
651da177e4SLinus Torvalds #include <net/checksum.h>
66dc5fc579SArnaldo Carvalho de Melo #include <net/netlink.h>
67f0ad0860SPatrick McHardy #include <net/fib_rules.h>
68d67b8c61SNicolas Dichtel #include <linux/netconf.h>
691da177e4SLinus Torvalds 
701da177e4SLinus Torvalds #if defined(CONFIG_IP_PIMSM_V1) || defined(CONFIG_IP_PIMSM_V2)
711da177e4SLinus Torvalds #define CONFIG_IP_PIMSM	1
721da177e4SLinus Torvalds #endif
731da177e4SLinus Torvalds 
740c12295aSPatrick McHardy struct mr_table {
75f0ad0860SPatrick McHardy 	struct list_head	list;
768de53dfbSPatrick McHardy #ifdef CONFIG_NET_NS
778de53dfbSPatrick McHardy 	struct net		*net;
788de53dfbSPatrick McHardy #endif
79f0ad0860SPatrick McHardy 	u32			id;
804c968709SEric Dumazet 	struct sock __rcu	*mroute_sk;
810c12295aSPatrick McHardy 	struct timer_list	ipmr_expire_timer;
820c12295aSPatrick McHardy 	struct list_head	mfc_unres_queue;
830c12295aSPatrick McHardy 	struct list_head	mfc_cache_array[MFC_LINES];
840c12295aSPatrick McHardy 	struct vif_device	vif_table[MAXVIFS];
850c12295aSPatrick McHardy 	int			maxvif;
860c12295aSPatrick McHardy 	atomic_t		cache_resolve_queue_len;
8753d6841dSJoe Perches 	bool			mroute_do_assert;
8853d6841dSJoe Perches 	bool			mroute_do_pim;
890c12295aSPatrick McHardy #if defined(CONFIG_IP_PIMSM_V1) || defined(CONFIG_IP_PIMSM_V2)
900c12295aSPatrick McHardy 	int			mroute_reg_vif_num;
910c12295aSPatrick McHardy #endif
920c12295aSPatrick McHardy };
930c12295aSPatrick McHardy 
94f0ad0860SPatrick McHardy struct ipmr_rule {
95f0ad0860SPatrick McHardy 	struct fib_rule		common;
96f0ad0860SPatrick McHardy };
97f0ad0860SPatrick McHardy 
98f0ad0860SPatrick McHardy struct ipmr_result {
99f0ad0860SPatrick McHardy 	struct mr_table		*mrt;
100f0ad0860SPatrick McHardy };
101f0ad0860SPatrick McHardy 
1021da177e4SLinus Torvalds /* Big lock, protecting vif table, mrt cache and mroute socket state.
103a8cb16ddSEric Dumazet  * Note that the changes are semaphored via rtnl_lock.
1041da177e4SLinus Torvalds  */
1051da177e4SLinus Torvalds 
1061da177e4SLinus Torvalds static DEFINE_RWLOCK(mrt_lock);
1071da177e4SLinus Torvalds 
1081da177e4SLinus Torvalds /*
1091da177e4SLinus Torvalds  *	Multicast router control variables
1101da177e4SLinus Torvalds  */
1111da177e4SLinus Torvalds 
1120c12295aSPatrick McHardy #define VIF_EXISTS(_mrt, _idx) ((_mrt)->vif_table[_idx].dev != NULL)
1131da177e4SLinus Torvalds 
1141da177e4SLinus Torvalds /* Special spinlock for queue of unresolved entries */
1151da177e4SLinus Torvalds static DEFINE_SPINLOCK(mfc_unres_lock);
1161da177e4SLinus Torvalds 
1171da177e4SLinus Torvalds /* We return to original Alan's scheme. Hash table of resolved
118a8cb16ddSEric Dumazet  * entries is changed only in process context and protected
119a8cb16ddSEric Dumazet  * with weak lock mrt_lock. Queue of unresolved entries is protected
120a8cb16ddSEric Dumazet  * with strong spinlock mfc_unres_lock.
121a8cb16ddSEric Dumazet  *
122a8cb16ddSEric Dumazet  * In this case data path is free of exclusive locks at all.
1231da177e4SLinus Torvalds  */
1241da177e4SLinus Torvalds 
125e18b890bSChristoph Lameter static struct kmem_cache *mrt_cachep __read_mostly;
1261da177e4SLinus Torvalds 
127f0ad0860SPatrick McHardy static struct mr_table *ipmr_new_table(struct net *net, u32 id);
128acbb219dSFrancesco Ruggeri static void ipmr_free_table(struct mr_table *mrt);
129acbb219dSFrancesco Ruggeri 
130*c4854ec8SRami Rosen static void ip_mr_forward(struct net *net, struct mr_table *mrt,
1310c12295aSPatrick McHardy 			  struct sk_buff *skb, struct mfc_cache *cache,
1320c12295aSPatrick McHardy 			  int local);
1330c12295aSPatrick McHardy static int ipmr_cache_report(struct mr_table *mrt,
1344feb88e5SBenjamin Thery 			     struct sk_buff *pkt, vifi_t vifi, int assert);
135cb6a4e46SPatrick McHardy static int __ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
136d658f8a0SPatrick McHardy 			      struct mfc_cache *c, struct rtmsg *rtm);
1378cd3ac9fSNicolas Dichtel static void mroute_netlink_event(struct mr_table *mrt, struct mfc_cache *mfc,
1388cd3ac9fSNicolas Dichtel 				 int cmd);
139acbb219dSFrancesco Ruggeri static void mroute_clean_tables(struct mr_table *mrt);
140f0ad0860SPatrick McHardy static void ipmr_expire_process(unsigned long arg);
1411da177e4SLinus Torvalds 
142f0ad0860SPatrick McHardy #ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
143f0ad0860SPatrick McHardy #define ipmr_for_each_table(mrt, net) \
144f0ad0860SPatrick McHardy 	list_for_each_entry_rcu(mrt, &net->ipv4.mr_tables, list)
145f0ad0860SPatrick McHardy 
146f0ad0860SPatrick McHardy static struct mr_table *ipmr_get_table(struct net *net, u32 id)
147f0ad0860SPatrick McHardy {
148f0ad0860SPatrick McHardy 	struct mr_table *mrt;
149f0ad0860SPatrick McHardy 
150f0ad0860SPatrick McHardy 	ipmr_for_each_table(mrt, net) {
151f0ad0860SPatrick McHardy 		if (mrt->id == id)
152f0ad0860SPatrick McHardy 			return mrt;
153f0ad0860SPatrick McHardy 	}
154f0ad0860SPatrick McHardy 	return NULL;
155f0ad0860SPatrick McHardy }
156f0ad0860SPatrick McHardy 
157da91981bSDavid S. Miller static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4,
158f0ad0860SPatrick McHardy 			   struct mr_table **mrt)
159f0ad0860SPatrick McHardy {
160f0ad0860SPatrick McHardy 	struct ipmr_result res;
161f0ad0860SPatrick McHardy 	struct fib_lookup_arg arg = { .result = &res, };
162f0ad0860SPatrick McHardy 	int err;
163f0ad0860SPatrick McHardy 
164da91981bSDavid S. Miller 	err = fib_rules_lookup(net->ipv4.mr_rules_ops,
165da91981bSDavid S. Miller 			       flowi4_to_flowi(flp4), 0, &arg);
166f0ad0860SPatrick McHardy 	if (err < 0)
167f0ad0860SPatrick McHardy 		return err;
168f0ad0860SPatrick McHardy 	*mrt = res.mrt;
169f0ad0860SPatrick McHardy 	return 0;
170f0ad0860SPatrick McHardy }
171f0ad0860SPatrick McHardy 
172f0ad0860SPatrick McHardy static int ipmr_rule_action(struct fib_rule *rule, struct flowi *flp,
173f0ad0860SPatrick McHardy 			    int flags, struct fib_lookup_arg *arg)
174f0ad0860SPatrick McHardy {
175f0ad0860SPatrick McHardy 	struct ipmr_result *res = arg->result;
176f0ad0860SPatrick McHardy 	struct mr_table *mrt;
177f0ad0860SPatrick McHardy 
178f0ad0860SPatrick McHardy 	switch (rule->action) {
179f0ad0860SPatrick McHardy 	case FR_ACT_TO_TBL:
180f0ad0860SPatrick McHardy 		break;
181f0ad0860SPatrick McHardy 	case FR_ACT_UNREACHABLE:
182f0ad0860SPatrick McHardy 		return -ENETUNREACH;
183f0ad0860SPatrick McHardy 	case FR_ACT_PROHIBIT:
184f0ad0860SPatrick McHardy 		return -EACCES;
185f0ad0860SPatrick McHardy 	case FR_ACT_BLACKHOLE:
186f0ad0860SPatrick McHardy 	default:
187f0ad0860SPatrick McHardy 		return -EINVAL;
188f0ad0860SPatrick McHardy 	}
189f0ad0860SPatrick McHardy 
190f0ad0860SPatrick McHardy 	mrt = ipmr_get_table(rule->fr_net, rule->table);
191f0ad0860SPatrick McHardy 	if (mrt == NULL)
192f0ad0860SPatrick McHardy 		return -EAGAIN;
193f0ad0860SPatrick McHardy 	res->mrt = mrt;
194f0ad0860SPatrick McHardy 	return 0;
195f0ad0860SPatrick McHardy }
196f0ad0860SPatrick McHardy 
197f0ad0860SPatrick McHardy static int ipmr_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
198f0ad0860SPatrick McHardy {
199f0ad0860SPatrick McHardy 	return 1;
200f0ad0860SPatrick McHardy }
201f0ad0860SPatrick McHardy 
202f0ad0860SPatrick McHardy static const struct nla_policy ipmr_rule_policy[FRA_MAX + 1] = {
203f0ad0860SPatrick McHardy 	FRA_GENERIC_POLICY,
204f0ad0860SPatrick McHardy };
205f0ad0860SPatrick McHardy 
206f0ad0860SPatrick McHardy static int ipmr_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
207f0ad0860SPatrick McHardy 			       struct fib_rule_hdr *frh, struct nlattr **tb)
208f0ad0860SPatrick McHardy {
209f0ad0860SPatrick McHardy 	return 0;
210f0ad0860SPatrick McHardy }
211f0ad0860SPatrick McHardy 
212f0ad0860SPatrick McHardy static int ipmr_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
213f0ad0860SPatrick McHardy 			     struct nlattr **tb)
214f0ad0860SPatrick McHardy {
215f0ad0860SPatrick McHardy 	return 1;
216f0ad0860SPatrick McHardy }
217f0ad0860SPatrick McHardy 
218f0ad0860SPatrick McHardy static int ipmr_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
219f0ad0860SPatrick McHardy 			  struct fib_rule_hdr *frh)
220f0ad0860SPatrick McHardy {
221f0ad0860SPatrick McHardy 	frh->dst_len = 0;
222f0ad0860SPatrick McHardy 	frh->src_len = 0;
223f0ad0860SPatrick McHardy 	frh->tos     = 0;
224f0ad0860SPatrick McHardy 	return 0;
225f0ad0860SPatrick McHardy }
226f0ad0860SPatrick McHardy 
22704a6f82cSAndi Kleen static const struct fib_rules_ops __net_initconst ipmr_rules_ops_template = {
22825239ceeSPatrick McHardy 	.family		= RTNL_FAMILY_IPMR,
229f0ad0860SPatrick McHardy 	.rule_size	= sizeof(struct ipmr_rule),
230f0ad0860SPatrick McHardy 	.addr_size	= sizeof(u32),
231f0ad0860SPatrick McHardy 	.action		= ipmr_rule_action,
232f0ad0860SPatrick McHardy 	.match		= ipmr_rule_match,
233f0ad0860SPatrick McHardy 	.configure	= ipmr_rule_configure,
234f0ad0860SPatrick McHardy 	.compare	= ipmr_rule_compare,
235f0ad0860SPatrick McHardy 	.default_pref	= fib_default_rule_pref,
236f0ad0860SPatrick McHardy 	.fill		= ipmr_rule_fill,
237f0ad0860SPatrick McHardy 	.nlgroup	= RTNLGRP_IPV4_RULE,
238f0ad0860SPatrick McHardy 	.policy		= ipmr_rule_policy,
239f0ad0860SPatrick McHardy 	.owner		= THIS_MODULE,
240f0ad0860SPatrick McHardy };
241f0ad0860SPatrick McHardy 
242f0ad0860SPatrick McHardy static int __net_init ipmr_rules_init(struct net *net)
243f0ad0860SPatrick McHardy {
244f0ad0860SPatrick McHardy 	struct fib_rules_ops *ops;
245f0ad0860SPatrick McHardy 	struct mr_table *mrt;
246f0ad0860SPatrick McHardy 	int err;
247f0ad0860SPatrick McHardy 
248f0ad0860SPatrick McHardy 	ops = fib_rules_register(&ipmr_rules_ops_template, net);
249f0ad0860SPatrick McHardy 	if (IS_ERR(ops))
250f0ad0860SPatrick McHardy 		return PTR_ERR(ops);
251f0ad0860SPatrick McHardy 
252f0ad0860SPatrick McHardy 	INIT_LIST_HEAD(&net->ipv4.mr_tables);
253f0ad0860SPatrick McHardy 
254f0ad0860SPatrick McHardy 	mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
255f0ad0860SPatrick McHardy 	if (mrt == NULL) {
256f0ad0860SPatrick McHardy 		err = -ENOMEM;
257f0ad0860SPatrick McHardy 		goto err1;
258f0ad0860SPatrick McHardy 	}
259f0ad0860SPatrick McHardy 
260f0ad0860SPatrick McHardy 	err = fib_default_rule_add(ops, 0x7fff, RT_TABLE_DEFAULT, 0);
261f0ad0860SPatrick McHardy 	if (err < 0)
262f0ad0860SPatrick McHardy 		goto err2;
263f0ad0860SPatrick McHardy 
264f0ad0860SPatrick McHardy 	net->ipv4.mr_rules_ops = ops;
265f0ad0860SPatrick McHardy 	return 0;
266f0ad0860SPatrick McHardy 
267f0ad0860SPatrick McHardy err2:
268f0ad0860SPatrick McHardy 	kfree(mrt);
269f0ad0860SPatrick McHardy err1:
270f0ad0860SPatrick McHardy 	fib_rules_unregister(ops);
271f0ad0860SPatrick McHardy 	return err;
272f0ad0860SPatrick McHardy }
273f0ad0860SPatrick McHardy 
274f0ad0860SPatrick McHardy static void __net_exit ipmr_rules_exit(struct net *net)
275f0ad0860SPatrick McHardy {
276f0ad0860SPatrick McHardy 	struct mr_table *mrt, *next;
277f0ad0860SPatrick McHardy 
278035320d5SEric Dumazet 	list_for_each_entry_safe(mrt, next, &net->ipv4.mr_tables, list) {
279035320d5SEric Dumazet 		list_del(&mrt->list);
280acbb219dSFrancesco Ruggeri 		ipmr_free_table(mrt);
281035320d5SEric Dumazet 	}
282f0ad0860SPatrick McHardy 	fib_rules_unregister(net->ipv4.mr_rules_ops);
283f0ad0860SPatrick McHardy }
284f0ad0860SPatrick McHardy #else
285f0ad0860SPatrick McHardy #define ipmr_for_each_table(mrt, net) \
286f0ad0860SPatrick McHardy 	for (mrt = net->ipv4.mrt; mrt; mrt = NULL)
287f0ad0860SPatrick McHardy 
288f0ad0860SPatrick McHardy static struct mr_table *ipmr_get_table(struct net *net, u32 id)
289f0ad0860SPatrick McHardy {
290f0ad0860SPatrick McHardy 	return net->ipv4.mrt;
291f0ad0860SPatrick McHardy }
292f0ad0860SPatrick McHardy 
293da91981bSDavid S. Miller static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4,
294f0ad0860SPatrick McHardy 			   struct mr_table **mrt)
295f0ad0860SPatrick McHardy {
296f0ad0860SPatrick McHardy 	*mrt = net->ipv4.mrt;
297f0ad0860SPatrick McHardy 	return 0;
298f0ad0860SPatrick McHardy }
299f0ad0860SPatrick McHardy 
300f0ad0860SPatrick McHardy static int __net_init ipmr_rules_init(struct net *net)
301f0ad0860SPatrick McHardy {
302f0ad0860SPatrick McHardy 	net->ipv4.mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
303f0ad0860SPatrick McHardy 	return net->ipv4.mrt ? 0 : -ENOMEM;
304f0ad0860SPatrick McHardy }
305f0ad0860SPatrick McHardy 
306f0ad0860SPatrick McHardy static void __net_exit ipmr_rules_exit(struct net *net)
307f0ad0860SPatrick McHardy {
308acbb219dSFrancesco Ruggeri 	ipmr_free_table(net->ipv4.mrt);
309f0ad0860SPatrick McHardy }
310f0ad0860SPatrick McHardy #endif
311f0ad0860SPatrick McHardy 
312f0ad0860SPatrick McHardy static struct mr_table *ipmr_new_table(struct net *net, u32 id)
313f0ad0860SPatrick McHardy {
314f0ad0860SPatrick McHardy 	struct mr_table *mrt;
315f0ad0860SPatrick McHardy 	unsigned int i;
316f0ad0860SPatrick McHardy 
317f0ad0860SPatrick McHardy 	mrt = ipmr_get_table(net, id);
318f0ad0860SPatrick McHardy 	if (mrt != NULL)
319f0ad0860SPatrick McHardy 		return mrt;
320f0ad0860SPatrick McHardy 
321f0ad0860SPatrick McHardy 	mrt = kzalloc(sizeof(*mrt), GFP_KERNEL);
322f0ad0860SPatrick McHardy 	if (mrt == NULL)
323f0ad0860SPatrick McHardy 		return NULL;
3248de53dfbSPatrick McHardy 	write_pnet(&mrt->net, net);
325f0ad0860SPatrick McHardy 	mrt->id = id;
326f0ad0860SPatrick McHardy 
327f0ad0860SPatrick McHardy 	/* Forwarding cache */
328f0ad0860SPatrick McHardy 	for (i = 0; i < MFC_LINES; i++)
329f0ad0860SPatrick McHardy 		INIT_LIST_HEAD(&mrt->mfc_cache_array[i]);
330f0ad0860SPatrick McHardy 
331f0ad0860SPatrick McHardy 	INIT_LIST_HEAD(&mrt->mfc_unres_queue);
332f0ad0860SPatrick McHardy 
333f0ad0860SPatrick McHardy 	setup_timer(&mrt->ipmr_expire_timer, ipmr_expire_process,
334f0ad0860SPatrick McHardy 		    (unsigned long)mrt);
335f0ad0860SPatrick McHardy 
336f0ad0860SPatrick McHardy #ifdef CONFIG_IP_PIMSM
337f0ad0860SPatrick McHardy 	mrt->mroute_reg_vif_num = -1;
338f0ad0860SPatrick McHardy #endif
339f0ad0860SPatrick McHardy #ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
340f0ad0860SPatrick McHardy 	list_add_tail_rcu(&mrt->list, &net->ipv4.mr_tables);
341f0ad0860SPatrick McHardy #endif
342f0ad0860SPatrick McHardy 	return mrt;
343f0ad0860SPatrick McHardy }
3441da177e4SLinus Torvalds 
345acbb219dSFrancesco Ruggeri static void ipmr_free_table(struct mr_table *mrt)
346acbb219dSFrancesco Ruggeri {
347acbb219dSFrancesco Ruggeri 	del_timer_sync(&mrt->ipmr_expire_timer);
348acbb219dSFrancesco Ruggeri 	mroute_clean_tables(mrt);
349acbb219dSFrancesco Ruggeri 	kfree(mrt);
350acbb219dSFrancesco Ruggeri }
351acbb219dSFrancesco Ruggeri 
3521da177e4SLinus Torvalds /* Service routines creating virtual interfaces: DVMRP tunnels and PIMREG */
3531da177e4SLinus Torvalds 
354d607032dSWang Chen static void ipmr_del_tunnel(struct net_device *dev, struct vifctl *v)
355d607032dSWang Chen {
3564feb88e5SBenjamin Thery 	struct net *net = dev_net(dev);
3574feb88e5SBenjamin Thery 
358d607032dSWang Chen 	dev_close(dev);
359d607032dSWang Chen 
3604feb88e5SBenjamin Thery 	dev = __dev_get_by_name(net, "tunl0");
361d607032dSWang Chen 	if (dev) {
3625bc3eb7eSStephen Hemminger 		const struct net_device_ops *ops = dev->netdev_ops;
363d607032dSWang Chen 		struct ifreq ifr;
364d607032dSWang Chen 		struct ip_tunnel_parm p;
365d607032dSWang Chen 
366d607032dSWang Chen 		memset(&p, 0, sizeof(p));
367d607032dSWang Chen 		p.iph.daddr = v->vifc_rmt_addr.s_addr;
368d607032dSWang Chen 		p.iph.saddr = v->vifc_lcl_addr.s_addr;
369d607032dSWang Chen 		p.iph.version = 4;
370d607032dSWang Chen 		p.iph.ihl = 5;
371d607032dSWang Chen 		p.iph.protocol = IPPROTO_IPIP;
372d607032dSWang Chen 		sprintf(p.name, "dvmrp%d", v->vifc_vifi);
373d607032dSWang Chen 		ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
374d607032dSWang Chen 
3755bc3eb7eSStephen Hemminger 		if (ops->ndo_do_ioctl) {
3765bc3eb7eSStephen Hemminger 			mm_segment_t oldfs = get_fs();
3775bc3eb7eSStephen Hemminger 
3785bc3eb7eSStephen Hemminger 			set_fs(KERNEL_DS);
3795bc3eb7eSStephen Hemminger 			ops->ndo_do_ioctl(dev, &ifr, SIOCDELTUNNEL);
380d607032dSWang Chen 			set_fs(oldfs);
381d607032dSWang Chen 		}
382d607032dSWang Chen 	}
3835bc3eb7eSStephen Hemminger }
384d607032dSWang Chen 
3851da177e4SLinus Torvalds static
3864feb88e5SBenjamin Thery struct net_device *ipmr_new_tunnel(struct net *net, struct vifctl *v)
3871da177e4SLinus Torvalds {
3881da177e4SLinus Torvalds 	struct net_device  *dev;
3891da177e4SLinus Torvalds 
3904feb88e5SBenjamin Thery 	dev = __dev_get_by_name(net, "tunl0");
3911da177e4SLinus Torvalds 
3921da177e4SLinus Torvalds 	if (dev) {
3935bc3eb7eSStephen Hemminger 		const struct net_device_ops *ops = dev->netdev_ops;
3941da177e4SLinus Torvalds 		int err;
3951da177e4SLinus Torvalds 		struct ifreq ifr;
3961da177e4SLinus Torvalds 		struct ip_tunnel_parm p;
3971da177e4SLinus Torvalds 		struct in_device  *in_dev;
3981da177e4SLinus Torvalds 
3991da177e4SLinus Torvalds 		memset(&p, 0, sizeof(p));
4001da177e4SLinus Torvalds 		p.iph.daddr = v->vifc_rmt_addr.s_addr;
4011da177e4SLinus Torvalds 		p.iph.saddr = v->vifc_lcl_addr.s_addr;
4021da177e4SLinus Torvalds 		p.iph.version = 4;
4031da177e4SLinus Torvalds 		p.iph.ihl = 5;
4041da177e4SLinus Torvalds 		p.iph.protocol = IPPROTO_IPIP;
4051da177e4SLinus Torvalds 		sprintf(p.name, "dvmrp%d", v->vifc_vifi);
406ba93ef74SStephen Hemminger 		ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
4071da177e4SLinus Torvalds 
4085bc3eb7eSStephen Hemminger 		if (ops->ndo_do_ioctl) {
4095bc3eb7eSStephen Hemminger 			mm_segment_t oldfs = get_fs();
4105bc3eb7eSStephen Hemminger 
4115bc3eb7eSStephen Hemminger 			set_fs(KERNEL_DS);
4125bc3eb7eSStephen Hemminger 			err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
4131da177e4SLinus Torvalds 			set_fs(oldfs);
414a8cb16ddSEric Dumazet 		} else {
4155bc3eb7eSStephen Hemminger 			err = -EOPNOTSUPP;
416a8cb16ddSEric Dumazet 		}
4171da177e4SLinus Torvalds 		dev = NULL;
4181da177e4SLinus Torvalds 
4194feb88e5SBenjamin Thery 		if (err == 0 &&
4204feb88e5SBenjamin Thery 		    (dev = __dev_get_by_name(net, p.name)) != NULL) {
4211da177e4SLinus Torvalds 			dev->flags |= IFF_MULTICAST;
4221da177e4SLinus Torvalds 
423e5ed6399SHerbert Xu 			in_dev = __in_dev_get_rtnl(dev);
42471e27da9SHerbert Xu 			if (in_dev == NULL)
4251da177e4SLinus Torvalds 				goto failure;
42671e27da9SHerbert Xu 
42771e27da9SHerbert Xu 			ipv4_devconf_setall(in_dev);
42871e27da9SHerbert Xu 			IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0;
4291da177e4SLinus Torvalds 
4301da177e4SLinus Torvalds 			if (dev_open(dev))
4311da177e4SLinus Torvalds 				goto failure;
4327dc00c82SWang Chen 			dev_hold(dev);
4331da177e4SLinus Torvalds 		}
4341da177e4SLinus Torvalds 	}
4351da177e4SLinus Torvalds 	return dev;
4361da177e4SLinus Torvalds 
4371da177e4SLinus Torvalds failure:
4381da177e4SLinus Torvalds 	/* allow the register to be completed before unregistering. */
4391da177e4SLinus Torvalds 	rtnl_unlock();
4401da177e4SLinus Torvalds 	rtnl_lock();
4411da177e4SLinus Torvalds 
4421da177e4SLinus Torvalds 	unregister_netdevice(dev);
4431da177e4SLinus Torvalds 	return NULL;
4441da177e4SLinus Torvalds }
4451da177e4SLinus Torvalds 
4461da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM
4471da177e4SLinus Torvalds 
4486fef4c0cSStephen Hemminger static netdev_tx_t reg_vif_xmit(struct sk_buff *skb, struct net_device *dev)
4491da177e4SLinus Torvalds {
4504feb88e5SBenjamin Thery 	struct net *net = dev_net(dev);
451f0ad0860SPatrick McHardy 	struct mr_table *mrt;
452da91981bSDavid S. Miller 	struct flowi4 fl4 = {
453da91981bSDavid S. Miller 		.flowi4_oif	= dev->ifindex,
454da91981bSDavid S. Miller 		.flowi4_iif	= skb->skb_iif,
455da91981bSDavid S. Miller 		.flowi4_mark	= skb->mark,
456f0ad0860SPatrick McHardy 	};
457f0ad0860SPatrick McHardy 	int err;
458f0ad0860SPatrick McHardy 
459da91981bSDavid S. Miller 	err = ipmr_fib_lookup(net, &fl4, &mrt);
460e40dbc51SBen Greear 	if (err < 0) {
461e40dbc51SBen Greear 		kfree_skb(skb);
462f0ad0860SPatrick McHardy 		return err;
463e40dbc51SBen Greear 	}
4644feb88e5SBenjamin Thery 
4651da177e4SLinus Torvalds 	read_lock(&mrt_lock);
466cf3677aeSPavel Emelyanov 	dev->stats.tx_bytes += skb->len;
467cf3677aeSPavel Emelyanov 	dev->stats.tx_packets++;
4680c12295aSPatrick McHardy 	ipmr_cache_report(mrt, skb, mrt->mroute_reg_vif_num, IGMPMSG_WHOLEPKT);
4691da177e4SLinus Torvalds 	read_unlock(&mrt_lock);
4701da177e4SLinus Torvalds 	kfree_skb(skb);
4716ed10654SPatrick McHardy 	return NETDEV_TX_OK;
4721da177e4SLinus Torvalds }
4731da177e4SLinus Torvalds 
474007c3838SStephen Hemminger static const struct net_device_ops reg_vif_netdev_ops = {
475007c3838SStephen Hemminger 	.ndo_start_xmit	= reg_vif_xmit,
476007c3838SStephen Hemminger };
477007c3838SStephen Hemminger 
4781da177e4SLinus Torvalds static void reg_vif_setup(struct net_device *dev)
4791da177e4SLinus Torvalds {
4801da177e4SLinus Torvalds 	dev->type		= ARPHRD_PIMREG;
48146f25dffSKris Katterjohn 	dev->mtu		= ETH_DATA_LEN - sizeof(struct iphdr) - 8;
4821da177e4SLinus Torvalds 	dev->flags		= IFF_NOARP;
483007c3838SStephen Hemminger 	dev->netdev_ops		= &reg_vif_netdev_ops,
4841da177e4SLinus Torvalds 	dev->destructor		= free_netdev;
485403dbb97STom Goff 	dev->features		|= NETIF_F_NETNS_LOCAL;
4861da177e4SLinus Torvalds }
4871da177e4SLinus Torvalds 
488f0ad0860SPatrick McHardy static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt)
4891da177e4SLinus Torvalds {
4901da177e4SLinus Torvalds 	struct net_device *dev;
4911da177e4SLinus Torvalds 	struct in_device *in_dev;
492f0ad0860SPatrick McHardy 	char name[IFNAMSIZ];
4931da177e4SLinus Torvalds 
494f0ad0860SPatrick McHardy 	if (mrt->id == RT_TABLE_DEFAULT)
495f0ad0860SPatrick McHardy 		sprintf(name, "pimreg");
496f0ad0860SPatrick McHardy 	else
497f0ad0860SPatrick McHardy 		sprintf(name, "pimreg%u", mrt->id);
498f0ad0860SPatrick McHardy 
499f0ad0860SPatrick McHardy 	dev = alloc_netdev(0, name, reg_vif_setup);
5001da177e4SLinus Torvalds 
5011da177e4SLinus Torvalds 	if (dev == NULL)
5021da177e4SLinus Torvalds 		return NULL;
5031da177e4SLinus Torvalds 
504403dbb97STom Goff 	dev_net_set(dev, net);
505403dbb97STom Goff 
5061da177e4SLinus Torvalds 	if (register_netdevice(dev)) {
5071da177e4SLinus Torvalds 		free_netdev(dev);
5081da177e4SLinus Torvalds 		return NULL;
5091da177e4SLinus Torvalds 	}
5101da177e4SLinus Torvalds 	dev->iflink = 0;
5111da177e4SLinus Torvalds 
51271e27da9SHerbert Xu 	rcu_read_lock();
513a8cb16ddSEric Dumazet 	in_dev = __in_dev_get_rcu(dev);
514a8cb16ddSEric Dumazet 	if (!in_dev) {
51571e27da9SHerbert Xu 		rcu_read_unlock();
5161da177e4SLinus Torvalds 		goto failure;
51771e27da9SHerbert Xu 	}
5181da177e4SLinus Torvalds 
51971e27da9SHerbert Xu 	ipv4_devconf_setall(in_dev);
52071e27da9SHerbert Xu 	IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0;
52171e27da9SHerbert Xu 	rcu_read_unlock();
5221da177e4SLinus Torvalds 
5231da177e4SLinus Torvalds 	if (dev_open(dev))
5241da177e4SLinus Torvalds 		goto failure;
5251da177e4SLinus Torvalds 
5267dc00c82SWang Chen 	dev_hold(dev);
5277dc00c82SWang Chen 
5281da177e4SLinus Torvalds 	return dev;
5291da177e4SLinus Torvalds 
5301da177e4SLinus Torvalds failure:
5311da177e4SLinus Torvalds 	/* allow the register to be completed before unregistering. */
5321da177e4SLinus Torvalds 	rtnl_unlock();
5331da177e4SLinus Torvalds 	rtnl_lock();
5341da177e4SLinus Torvalds 
5351da177e4SLinus Torvalds 	unregister_netdevice(dev);
5361da177e4SLinus Torvalds 	return NULL;
5371da177e4SLinus Torvalds }
5381da177e4SLinus Torvalds #endif
5391da177e4SLinus Torvalds 
5402c53040fSBen Hutchings /**
5412c53040fSBen Hutchings  *	vif_delete - Delete a VIF entry
5427dc00c82SWang Chen  *	@notify: Set to 1, if the caller is a notifier_call
5431da177e4SLinus Torvalds  */
5441da177e4SLinus Torvalds 
5450c12295aSPatrick McHardy static int vif_delete(struct mr_table *mrt, int vifi, int notify,
546d17fa6faSEric Dumazet 		      struct list_head *head)
5471da177e4SLinus Torvalds {
5481da177e4SLinus Torvalds 	struct vif_device *v;
5491da177e4SLinus Torvalds 	struct net_device *dev;
5501da177e4SLinus Torvalds 	struct in_device *in_dev;
5511da177e4SLinus Torvalds 
5520c12295aSPatrick McHardy 	if (vifi < 0 || vifi >= mrt->maxvif)
5531da177e4SLinus Torvalds 		return -EADDRNOTAVAIL;
5541da177e4SLinus Torvalds 
5550c12295aSPatrick McHardy 	v = &mrt->vif_table[vifi];
5561da177e4SLinus Torvalds 
5571da177e4SLinus Torvalds 	write_lock_bh(&mrt_lock);
5581da177e4SLinus Torvalds 	dev = v->dev;
5591da177e4SLinus Torvalds 	v->dev = NULL;
5601da177e4SLinus Torvalds 
5611da177e4SLinus Torvalds 	if (!dev) {
5621da177e4SLinus Torvalds 		write_unlock_bh(&mrt_lock);
5631da177e4SLinus Torvalds 		return -EADDRNOTAVAIL;
5641da177e4SLinus Torvalds 	}
5651da177e4SLinus Torvalds 
5661da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM
5670c12295aSPatrick McHardy 	if (vifi == mrt->mroute_reg_vif_num)
5680c12295aSPatrick McHardy 		mrt->mroute_reg_vif_num = -1;
5691da177e4SLinus Torvalds #endif
5701da177e4SLinus Torvalds 
5710c12295aSPatrick McHardy 	if (vifi + 1 == mrt->maxvif) {
5721da177e4SLinus Torvalds 		int tmp;
573a8cb16ddSEric Dumazet 
5741da177e4SLinus Torvalds 		for (tmp = vifi - 1; tmp >= 0; tmp--) {
5750c12295aSPatrick McHardy 			if (VIF_EXISTS(mrt, tmp))
5761da177e4SLinus Torvalds 				break;
5771da177e4SLinus Torvalds 		}
5780c12295aSPatrick McHardy 		mrt->maxvif = tmp+1;
5791da177e4SLinus Torvalds 	}
5801da177e4SLinus Torvalds 
5811da177e4SLinus Torvalds 	write_unlock_bh(&mrt_lock);
5821da177e4SLinus Torvalds 
5831da177e4SLinus Torvalds 	dev_set_allmulti(dev, -1);
5841da177e4SLinus Torvalds 
585a8cb16ddSEric Dumazet 	in_dev = __in_dev_get_rtnl(dev);
586a8cb16ddSEric Dumazet 	if (in_dev) {
58742f811b8SHerbert Xu 		IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)--;
588d67b8c61SNicolas Dichtel 		inet_netconf_notify_devconf(dev_net(dev),
589d67b8c61SNicolas Dichtel 					    NETCONFA_MC_FORWARDING,
590d67b8c61SNicolas Dichtel 					    dev->ifindex, &in_dev->cnf);
5911da177e4SLinus Torvalds 		ip_rt_multicast_event(in_dev);
5921da177e4SLinus Torvalds 	}
5931da177e4SLinus Torvalds 
5947dc00c82SWang Chen 	if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER) && !notify)
595d17fa6faSEric Dumazet 		unregister_netdevice_queue(dev, head);
5961da177e4SLinus Torvalds 
5971da177e4SLinus Torvalds 	dev_put(dev);
5981da177e4SLinus Torvalds 	return 0;
5991da177e4SLinus Torvalds }
6001da177e4SLinus Torvalds 
601a8c9486bSEric Dumazet static void ipmr_cache_free_rcu(struct rcu_head *head)
602a8c9486bSEric Dumazet {
603a8c9486bSEric Dumazet 	struct mfc_cache *c = container_of(head, struct mfc_cache, rcu);
604a8c9486bSEric Dumazet 
605a8c9486bSEric Dumazet 	kmem_cache_free(mrt_cachep, c);
606a8c9486bSEric Dumazet }
607a8c9486bSEric Dumazet 
6085c0a66f5SBenjamin Thery static inline void ipmr_cache_free(struct mfc_cache *c)
6095c0a66f5SBenjamin Thery {
610a8c9486bSEric Dumazet 	call_rcu(&c->rcu, ipmr_cache_free_rcu);
6115c0a66f5SBenjamin Thery }
6125c0a66f5SBenjamin Thery 
6131da177e4SLinus Torvalds /* Destroy an unresolved cache entry, killing queued skbs
614a8cb16ddSEric Dumazet  * and reporting error to netlink readers.
6151da177e4SLinus Torvalds  */
6161da177e4SLinus Torvalds 
6170c12295aSPatrick McHardy static void ipmr_destroy_unres(struct mr_table *mrt, struct mfc_cache *c)
6181da177e4SLinus Torvalds {
6198de53dfbSPatrick McHardy 	struct net *net = read_pnet(&mrt->net);
6201da177e4SLinus Torvalds 	struct sk_buff *skb;
6219ef1d4c7SPatrick McHardy 	struct nlmsgerr *e;
6221da177e4SLinus Torvalds 
6230c12295aSPatrick McHardy 	atomic_dec(&mrt->cache_resolve_queue_len);
6241da177e4SLinus Torvalds 
6251da177e4SLinus Torvalds 	while ((skb = skb_dequeue(&c->mfc_un.unres.unresolved))) {
626eddc9ec5SArnaldo Carvalho de Melo 		if (ip_hdr(skb)->version == 0) {
6271da177e4SLinus Torvalds 			struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr));
6281da177e4SLinus Torvalds 			nlh->nlmsg_type = NLMSG_ERROR;
629573ce260SHong zhi guo 			nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr));
6301da177e4SLinus Torvalds 			skb_trim(skb, nlh->nlmsg_len);
631573ce260SHong zhi guo 			e = nlmsg_data(nlh);
6329ef1d4c7SPatrick McHardy 			e->error = -ETIMEDOUT;
6339ef1d4c7SPatrick McHardy 			memset(&e->msg, 0, sizeof(e->msg));
6342942e900SThomas Graf 
63515e47304SEric W. Biederman 			rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
636a8cb16ddSEric Dumazet 		} else {
6371da177e4SLinus Torvalds 			kfree_skb(skb);
6381da177e4SLinus Torvalds 		}
639a8cb16ddSEric Dumazet 	}
6401da177e4SLinus Torvalds 
6415c0a66f5SBenjamin Thery 	ipmr_cache_free(c);
6421da177e4SLinus Torvalds }
6431da177e4SLinus Torvalds 
6441da177e4SLinus Torvalds 
645e258beb2SPatrick McHardy /* Timer process for the unresolved queue. */
6461da177e4SLinus Torvalds 
647e258beb2SPatrick McHardy static void ipmr_expire_process(unsigned long arg)
6481da177e4SLinus Torvalds {
6490c12295aSPatrick McHardy 	struct mr_table *mrt = (struct mr_table *)arg;
6501da177e4SLinus Torvalds 	unsigned long now;
6511da177e4SLinus Torvalds 	unsigned long expires;
652862465f2SPatrick McHardy 	struct mfc_cache *c, *next;
6531da177e4SLinus Torvalds 
6541da177e4SLinus Torvalds 	if (!spin_trylock(&mfc_unres_lock)) {
6550c12295aSPatrick McHardy 		mod_timer(&mrt->ipmr_expire_timer, jiffies+HZ/10);
6561da177e4SLinus Torvalds 		return;
6571da177e4SLinus Torvalds 	}
6581da177e4SLinus Torvalds 
6590c12295aSPatrick McHardy 	if (list_empty(&mrt->mfc_unres_queue))
6601da177e4SLinus Torvalds 		goto out;
6611da177e4SLinus Torvalds 
6621da177e4SLinus Torvalds 	now = jiffies;
6631da177e4SLinus Torvalds 	expires = 10*HZ;
6641da177e4SLinus Torvalds 
6650c12295aSPatrick McHardy 	list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
6661da177e4SLinus Torvalds 		if (time_after(c->mfc_un.unres.expires, now)) {
6671da177e4SLinus Torvalds 			unsigned long interval = c->mfc_un.unres.expires - now;
6681da177e4SLinus Torvalds 			if (interval < expires)
6691da177e4SLinus Torvalds 				expires = interval;
6701da177e4SLinus Torvalds 			continue;
6711da177e4SLinus Torvalds 		}
6721da177e4SLinus Torvalds 
673862465f2SPatrick McHardy 		list_del(&c->list);
6748cd3ac9fSNicolas Dichtel 		mroute_netlink_event(mrt, c, RTM_DELROUTE);
6750c12295aSPatrick McHardy 		ipmr_destroy_unres(mrt, c);
6761da177e4SLinus Torvalds 	}
6771da177e4SLinus Torvalds 
6780c12295aSPatrick McHardy 	if (!list_empty(&mrt->mfc_unres_queue))
6790c12295aSPatrick McHardy 		mod_timer(&mrt->ipmr_expire_timer, jiffies + expires);
6801da177e4SLinus Torvalds 
6811da177e4SLinus Torvalds out:
6821da177e4SLinus Torvalds 	spin_unlock(&mfc_unres_lock);
6831da177e4SLinus Torvalds }
6841da177e4SLinus Torvalds 
6851da177e4SLinus Torvalds /* Fill oifs list. It is called under write locked mrt_lock. */
6861da177e4SLinus Torvalds 
6870c12295aSPatrick McHardy static void ipmr_update_thresholds(struct mr_table *mrt, struct mfc_cache *cache,
688d658f8a0SPatrick McHardy 				   unsigned char *ttls)
6891da177e4SLinus Torvalds {
6901da177e4SLinus Torvalds 	int vifi;
6911da177e4SLinus Torvalds 
6921da177e4SLinus Torvalds 	cache->mfc_un.res.minvif = MAXVIFS;
6931da177e4SLinus Torvalds 	cache->mfc_un.res.maxvif = 0;
6941da177e4SLinus Torvalds 	memset(cache->mfc_un.res.ttls, 255, MAXVIFS);
6951da177e4SLinus Torvalds 
6960c12295aSPatrick McHardy 	for (vifi = 0; vifi < mrt->maxvif; vifi++) {
6970c12295aSPatrick McHardy 		if (VIF_EXISTS(mrt, vifi) &&
698cf958ae3SBenjamin Thery 		    ttls[vifi] && ttls[vifi] < 255) {
6991da177e4SLinus Torvalds 			cache->mfc_un.res.ttls[vifi] = ttls[vifi];
7001da177e4SLinus Torvalds 			if (cache->mfc_un.res.minvif > vifi)
7011da177e4SLinus Torvalds 				cache->mfc_un.res.minvif = vifi;
7021da177e4SLinus Torvalds 			if (cache->mfc_un.res.maxvif <= vifi)
7031da177e4SLinus Torvalds 				cache->mfc_un.res.maxvif = vifi + 1;
7041da177e4SLinus Torvalds 		}
7051da177e4SLinus Torvalds 	}
7061da177e4SLinus Torvalds }
7071da177e4SLinus Torvalds 
7080c12295aSPatrick McHardy static int vif_add(struct net *net, struct mr_table *mrt,
7090c12295aSPatrick McHardy 		   struct vifctl *vifc, int mrtsock)
7101da177e4SLinus Torvalds {
7111da177e4SLinus Torvalds 	int vifi = vifc->vifc_vifi;
7120c12295aSPatrick McHardy 	struct vif_device *v = &mrt->vif_table[vifi];
7131da177e4SLinus Torvalds 	struct net_device *dev;
7141da177e4SLinus Torvalds 	struct in_device *in_dev;
715d607032dSWang Chen 	int err;
7161da177e4SLinus Torvalds 
7171da177e4SLinus Torvalds 	/* Is vif busy ? */
7180c12295aSPatrick McHardy 	if (VIF_EXISTS(mrt, vifi))
7191da177e4SLinus Torvalds 		return -EADDRINUSE;
7201da177e4SLinus Torvalds 
7211da177e4SLinus Torvalds 	switch (vifc->vifc_flags) {
7221da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM
7231da177e4SLinus Torvalds 	case VIFF_REGISTER:
7241da177e4SLinus Torvalds 		/*
7251da177e4SLinus Torvalds 		 * Special Purpose VIF in PIM
7261da177e4SLinus Torvalds 		 * All the packets will be sent to the daemon
7271da177e4SLinus Torvalds 		 */
7280c12295aSPatrick McHardy 		if (mrt->mroute_reg_vif_num >= 0)
7291da177e4SLinus Torvalds 			return -EADDRINUSE;
730f0ad0860SPatrick McHardy 		dev = ipmr_reg_vif(net, mrt);
7311da177e4SLinus Torvalds 		if (!dev)
7321da177e4SLinus Torvalds 			return -ENOBUFS;
733d607032dSWang Chen 		err = dev_set_allmulti(dev, 1);
734d607032dSWang Chen 		if (err) {
735d607032dSWang Chen 			unregister_netdevice(dev);
7367dc00c82SWang Chen 			dev_put(dev);
737d607032dSWang Chen 			return err;
738d607032dSWang Chen 		}
7391da177e4SLinus Torvalds 		break;
7401da177e4SLinus Torvalds #endif
7411da177e4SLinus Torvalds 	case VIFF_TUNNEL:
7424feb88e5SBenjamin Thery 		dev = ipmr_new_tunnel(net, vifc);
7431da177e4SLinus Torvalds 		if (!dev)
7441da177e4SLinus Torvalds 			return -ENOBUFS;
745d607032dSWang Chen 		err = dev_set_allmulti(dev, 1);
746d607032dSWang Chen 		if (err) {
747d607032dSWang Chen 			ipmr_del_tunnel(dev, vifc);
7487dc00c82SWang Chen 			dev_put(dev);
749d607032dSWang Chen 			return err;
750d607032dSWang Chen 		}
7511da177e4SLinus Torvalds 		break;
752ee5e81f0SIlia K 
753ee5e81f0SIlia K 	case VIFF_USE_IFINDEX:
7541da177e4SLinus Torvalds 	case 0:
755ee5e81f0SIlia K 		if (vifc->vifc_flags == VIFF_USE_IFINDEX) {
756ee5e81f0SIlia K 			dev = dev_get_by_index(net, vifc->vifc_lcl_ifindex);
75795ae6b22SEric Dumazet 			if (dev && __in_dev_get_rtnl(dev) == NULL) {
758ee5e81f0SIlia K 				dev_put(dev);
759ee5e81f0SIlia K 				return -EADDRNOTAVAIL;
760ee5e81f0SIlia K 			}
761a8cb16ddSEric Dumazet 		} else {
7624feb88e5SBenjamin Thery 			dev = ip_dev_find(net, vifc->vifc_lcl_addr.s_addr);
763a8cb16ddSEric Dumazet 		}
7641da177e4SLinus Torvalds 		if (!dev)
7651da177e4SLinus Torvalds 			return -EADDRNOTAVAIL;
766d607032dSWang Chen 		err = dev_set_allmulti(dev, 1);
7677dc00c82SWang Chen 		if (err) {
7687dc00c82SWang Chen 			dev_put(dev);
769d607032dSWang Chen 			return err;
7707dc00c82SWang Chen 		}
7711da177e4SLinus Torvalds 		break;
7721da177e4SLinus Torvalds 	default:
7731da177e4SLinus Torvalds 		return -EINVAL;
7741da177e4SLinus Torvalds 	}
7751da177e4SLinus Torvalds 
776a8cb16ddSEric Dumazet 	in_dev = __in_dev_get_rtnl(dev);
777a8cb16ddSEric Dumazet 	if (!in_dev) {
778d0490cfdSDan Carpenter 		dev_put(dev);
7791da177e4SLinus Torvalds 		return -EADDRNOTAVAIL;
780d0490cfdSDan Carpenter 	}
78142f811b8SHerbert Xu 	IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)++;
782d67b8c61SNicolas Dichtel 	inet_netconf_notify_devconf(net, NETCONFA_MC_FORWARDING, dev->ifindex,
783d67b8c61SNicolas Dichtel 				    &in_dev->cnf);
7841da177e4SLinus Torvalds 	ip_rt_multicast_event(in_dev);
7851da177e4SLinus Torvalds 
786a8cb16ddSEric Dumazet 	/* Fill in the VIF structures */
787a8cb16ddSEric Dumazet 
7881da177e4SLinus Torvalds 	v->rate_limit = vifc->vifc_rate_limit;
7891da177e4SLinus Torvalds 	v->local = vifc->vifc_lcl_addr.s_addr;
7901da177e4SLinus Torvalds 	v->remote = vifc->vifc_rmt_addr.s_addr;
7911da177e4SLinus Torvalds 	v->flags = vifc->vifc_flags;
7921da177e4SLinus Torvalds 	if (!mrtsock)
7931da177e4SLinus Torvalds 		v->flags |= VIFF_STATIC;
7941da177e4SLinus Torvalds 	v->threshold = vifc->vifc_threshold;
7951da177e4SLinus Torvalds 	v->bytes_in = 0;
7961da177e4SLinus Torvalds 	v->bytes_out = 0;
7971da177e4SLinus Torvalds 	v->pkt_in = 0;
7981da177e4SLinus Torvalds 	v->pkt_out = 0;
7991da177e4SLinus Torvalds 	v->link = dev->ifindex;
8001da177e4SLinus Torvalds 	if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER))
8011da177e4SLinus Torvalds 		v->link = dev->iflink;
8021da177e4SLinus Torvalds 
8031da177e4SLinus Torvalds 	/* And finish update writing critical data */
8041da177e4SLinus Torvalds 	write_lock_bh(&mrt_lock);
8051da177e4SLinus Torvalds 	v->dev = dev;
8061da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM
8071da177e4SLinus Torvalds 	if (v->flags & VIFF_REGISTER)
8080c12295aSPatrick McHardy 		mrt->mroute_reg_vif_num = vifi;
8091da177e4SLinus Torvalds #endif
8100c12295aSPatrick McHardy 	if (vifi+1 > mrt->maxvif)
8110c12295aSPatrick McHardy 		mrt->maxvif = vifi+1;
8121da177e4SLinus Torvalds 	write_unlock_bh(&mrt_lock);
8131da177e4SLinus Torvalds 	return 0;
8141da177e4SLinus Torvalds }
8151da177e4SLinus Torvalds 
816a8c9486bSEric Dumazet /* called with rcu_read_lock() */
8170c12295aSPatrick McHardy static struct mfc_cache *ipmr_cache_find(struct mr_table *mrt,
8184feb88e5SBenjamin Thery 					 __be32 origin,
8194feb88e5SBenjamin Thery 					 __be32 mcastgrp)
8201da177e4SLinus Torvalds {
8211da177e4SLinus Torvalds 	int line = MFC_HASH(mcastgrp, origin);
8221da177e4SLinus Torvalds 	struct mfc_cache *c;
8231da177e4SLinus Torvalds 
824a8c9486bSEric Dumazet 	list_for_each_entry_rcu(c, &mrt->mfc_cache_array[line], list) {
8251da177e4SLinus Torvalds 		if (c->mfc_origin == origin && c->mfc_mcastgrp == mcastgrp)
8261da177e4SLinus Torvalds 			return c;
8271da177e4SLinus Torvalds 	}
828862465f2SPatrick McHardy 	return NULL;
829862465f2SPatrick McHardy }
8301da177e4SLinus Torvalds 
831660b26dcSNicolas Dichtel /* Look for a (*,*,oif) entry */
832660b26dcSNicolas Dichtel static struct mfc_cache *ipmr_cache_find_any_parent(struct mr_table *mrt,
833660b26dcSNicolas Dichtel 						    int vifi)
834660b26dcSNicolas Dichtel {
835360eb5daSNicolas Dichtel 	int line = MFC_HASH(htonl(INADDR_ANY), htonl(INADDR_ANY));
836660b26dcSNicolas Dichtel 	struct mfc_cache *c;
837660b26dcSNicolas Dichtel 
838660b26dcSNicolas Dichtel 	list_for_each_entry_rcu(c, &mrt->mfc_cache_array[line], list)
839360eb5daSNicolas Dichtel 		if (c->mfc_origin == htonl(INADDR_ANY) &&
840360eb5daSNicolas Dichtel 		    c->mfc_mcastgrp == htonl(INADDR_ANY) &&
841660b26dcSNicolas Dichtel 		    c->mfc_un.res.ttls[vifi] < 255)
842660b26dcSNicolas Dichtel 			return c;
843660b26dcSNicolas Dichtel 
844660b26dcSNicolas Dichtel 	return NULL;
845660b26dcSNicolas Dichtel }
846660b26dcSNicolas Dichtel 
847660b26dcSNicolas Dichtel /* Look for a (*,G) entry */
848660b26dcSNicolas Dichtel static struct mfc_cache *ipmr_cache_find_any(struct mr_table *mrt,
849660b26dcSNicolas Dichtel 					     __be32 mcastgrp, int vifi)
850660b26dcSNicolas Dichtel {
851360eb5daSNicolas Dichtel 	int line = MFC_HASH(mcastgrp, htonl(INADDR_ANY));
852660b26dcSNicolas Dichtel 	struct mfc_cache *c, *proxy;
853660b26dcSNicolas Dichtel 
854360eb5daSNicolas Dichtel 	if (mcastgrp == htonl(INADDR_ANY))
855660b26dcSNicolas Dichtel 		goto skip;
856660b26dcSNicolas Dichtel 
857660b26dcSNicolas Dichtel 	list_for_each_entry_rcu(c, &mrt->mfc_cache_array[line], list)
858360eb5daSNicolas Dichtel 		if (c->mfc_origin == htonl(INADDR_ANY) &&
859660b26dcSNicolas Dichtel 		    c->mfc_mcastgrp == mcastgrp) {
860660b26dcSNicolas Dichtel 			if (c->mfc_un.res.ttls[vifi] < 255)
861660b26dcSNicolas Dichtel 				return c;
862660b26dcSNicolas Dichtel 
863660b26dcSNicolas Dichtel 			/* It's ok if the vifi is part of the static tree */
864660b26dcSNicolas Dichtel 			proxy = ipmr_cache_find_any_parent(mrt,
865660b26dcSNicolas Dichtel 							   c->mfc_parent);
866660b26dcSNicolas Dichtel 			if (proxy && proxy->mfc_un.res.ttls[vifi] < 255)
867660b26dcSNicolas Dichtel 				return c;
868660b26dcSNicolas Dichtel 		}
869660b26dcSNicolas Dichtel 
870660b26dcSNicolas Dichtel skip:
871660b26dcSNicolas Dichtel 	return ipmr_cache_find_any_parent(mrt, vifi);
872660b26dcSNicolas Dichtel }
873660b26dcSNicolas Dichtel 
8741da177e4SLinus Torvalds /*
8751da177e4SLinus Torvalds  *	Allocate a multicast cache entry
8761da177e4SLinus Torvalds  */
877d658f8a0SPatrick McHardy static struct mfc_cache *ipmr_cache_alloc(void)
8781da177e4SLinus Torvalds {
879c3762229SRobert P. J. Day 	struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_KERNEL);
880a8c9486bSEric Dumazet 
881a8c9486bSEric Dumazet 	if (c)
8821da177e4SLinus Torvalds 		c->mfc_un.res.minvif = MAXVIFS;
8831da177e4SLinus Torvalds 	return c;
8841da177e4SLinus Torvalds }
8851da177e4SLinus Torvalds 
886d658f8a0SPatrick McHardy static struct mfc_cache *ipmr_cache_alloc_unres(void)
8871da177e4SLinus Torvalds {
888c3762229SRobert P. J. Day 	struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_ATOMIC);
889a8c9486bSEric Dumazet 
890a8c9486bSEric Dumazet 	if (c) {
8911da177e4SLinus Torvalds 		skb_queue_head_init(&c->mfc_un.unres.unresolved);
8921da177e4SLinus Torvalds 		c->mfc_un.unres.expires = jiffies + 10*HZ;
893a8c9486bSEric Dumazet 	}
8941da177e4SLinus Torvalds 	return c;
8951da177e4SLinus Torvalds }
8961da177e4SLinus Torvalds 
8971da177e4SLinus Torvalds /*
8981da177e4SLinus Torvalds  *	A cache entry has gone into a resolved state from queued
8991da177e4SLinus Torvalds  */
9001da177e4SLinus Torvalds 
9010c12295aSPatrick McHardy static void ipmr_cache_resolve(struct net *net, struct mr_table *mrt,
9020c12295aSPatrick McHardy 			       struct mfc_cache *uc, struct mfc_cache *c)
9031da177e4SLinus Torvalds {
9041da177e4SLinus Torvalds 	struct sk_buff *skb;
9059ef1d4c7SPatrick McHardy 	struct nlmsgerr *e;
9061da177e4SLinus Torvalds 
907a8cb16ddSEric Dumazet 	/* Play the pending entries through our router */
9081da177e4SLinus Torvalds 
9091da177e4SLinus Torvalds 	while ((skb = __skb_dequeue(&uc->mfc_un.unres.unresolved))) {
910eddc9ec5SArnaldo Carvalho de Melo 		if (ip_hdr(skb)->version == 0) {
9111da177e4SLinus Torvalds 			struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr));
9121da177e4SLinus Torvalds 
913573ce260SHong zhi guo 			if (__ipmr_fill_mroute(mrt, skb, c, nlmsg_data(nlh)) > 0) {
914a8cb16ddSEric Dumazet 				nlh->nlmsg_len = skb_tail_pointer(skb) -
915a8cb16ddSEric Dumazet 						 (u8 *)nlh;
9161da177e4SLinus Torvalds 			} else {
9171da177e4SLinus Torvalds 				nlh->nlmsg_type = NLMSG_ERROR;
918573ce260SHong zhi guo 				nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr));
9191da177e4SLinus Torvalds 				skb_trim(skb, nlh->nlmsg_len);
920573ce260SHong zhi guo 				e = nlmsg_data(nlh);
9219ef1d4c7SPatrick McHardy 				e->error = -EMSGSIZE;
9229ef1d4c7SPatrick McHardy 				memset(&e->msg, 0, sizeof(e->msg));
9231da177e4SLinus Torvalds 			}
9242942e900SThomas Graf 
92515e47304SEric W. Biederman 			rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
926a8cb16ddSEric Dumazet 		} else {
9270c12295aSPatrick McHardy 			ip_mr_forward(net, mrt, skb, c, 0);
9281da177e4SLinus Torvalds 		}
9291da177e4SLinus Torvalds 	}
930a8cb16ddSEric Dumazet }
9311da177e4SLinus Torvalds 
9321da177e4SLinus Torvalds /*
9331da177e4SLinus Torvalds  *	Bounce a cache query up to mrouted. We could use netlink for this but mrouted
9341da177e4SLinus Torvalds  *	expects the following bizarre scheme.
9351da177e4SLinus Torvalds  *
9361da177e4SLinus Torvalds  *	Called under mrt_lock.
9371da177e4SLinus Torvalds  */
9381da177e4SLinus Torvalds 
9390c12295aSPatrick McHardy static int ipmr_cache_report(struct mr_table *mrt,
9404feb88e5SBenjamin Thery 			     struct sk_buff *pkt, vifi_t vifi, int assert)
9411da177e4SLinus Torvalds {
9421da177e4SLinus Torvalds 	struct sk_buff *skb;
943c9bdd4b5SArnaldo Carvalho de Melo 	const int ihl = ip_hdrlen(pkt);
9441da177e4SLinus Torvalds 	struct igmphdr *igmp;
9451da177e4SLinus Torvalds 	struct igmpmsg *msg;
9464c968709SEric Dumazet 	struct sock *mroute_sk;
9471da177e4SLinus Torvalds 	int ret;
9481da177e4SLinus Torvalds 
9491da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM
9501da177e4SLinus Torvalds 	if (assert == IGMPMSG_WHOLEPKT)
9511da177e4SLinus Torvalds 		skb = skb_realloc_headroom(pkt, sizeof(struct iphdr));
9521da177e4SLinus Torvalds 	else
9531da177e4SLinus Torvalds #endif
9541da177e4SLinus Torvalds 		skb = alloc_skb(128, GFP_ATOMIC);
9551da177e4SLinus Torvalds 
9561da177e4SLinus Torvalds 	if (!skb)
9571da177e4SLinus Torvalds 		return -ENOBUFS;
9581da177e4SLinus Torvalds 
9591da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM
9601da177e4SLinus Torvalds 	if (assert == IGMPMSG_WHOLEPKT) {
9611da177e4SLinus Torvalds 		/* Ugly, but we have no choice with this interface.
962a8cb16ddSEric Dumazet 		 * Duplicate old header, fix ihl, length etc.
963a8cb16ddSEric Dumazet 		 * And all this only to mangle msg->im_msgtype and
964a8cb16ddSEric Dumazet 		 * to set msg->im_mbz to "mbz" :-)
9651da177e4SLinus Torvalds 		 */
966878c8145SArnaldo Carvalho de Melo 		skb_push(skb, sizeof(struct iphdr));
967878c8145SArnaldo Carvalho de Melo 		skb_reset_network_header(skb);
968badff6d0SArnaldo Carvalho de Melo 		skb_reset_transport_header(skb);
9690272ffc4SArnaldo Carvalho de Melo 		msg = (struct igmpmsg *)skb_network_header(skb);
970d56f90a7SArnaldo Carvalho de Melo 		memcpy(msg, skb_network_header(pkt), sizeof(struct iphdr));
9711da177e4SLinus Torvalds 		msg->im_msgtype = IGMPMSG_WHOLEPKT;
9721da177e4SLinus Torvalds 		msg->im_mbz = 0;
9730c12295aSPatrick McHardy 		msg->im_vif = mrt->mroute_reg_vif_num;
974eddc9ec5SArnaldo Carvalho de Melo 		ip_hdr(skb)->ihl = sizeof(struct iphdr) >> 2;
975eddc9ec5SArnaldo Carvalho de Melo 		ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(pkt)->tot_len) +
976eddc9ec5SArnaldo Carvalho de Melo 					     sizeof(struct iphdr));
9771da177e4SLinus Torvalds 	} else
9781da177e4SLinus Torvalds #endif
9791da177e4SLinus Torvalds 	{
9801da177e4SLinus Torvalds 
981a8cb16ddSEric Dumazet 	/* Copy the IP header */
9821da177e4SLinus Torvalds 
98330f3a40fSCong Wang 	skb_set_network_header(skb, skb->len);
984ddc7b8e3SArnaldo Carvalho de Melo 	skb_put(skb, ihl);
98527d7ff46SArnaldo Carvalho de Melo 	skb_copy_to_linear_data(skb, pkt->data, ihl);
986eddc9ec5SArnaldo Carvalho de Melo 	ip_hdr(skb)->protocol = 0;	/* Flag to the kernel this is a route add */
987eddc9ec5SArnaldo Carvalho de Melo 	msg = (struct igmpmsg *)skb_network_header(skb);
9881da177e4SLinus Torvalds 	msg->im_vif = vifi;
989adf30907SEric Dumazet 	skb_dst_set(skb, dst_clone(skb_dst(pkt)));
9901da177e4SLinus Torvalds 
991a8cb16ddSEric Dumazet 	/* Add our header */
9921da177e4SLinus Torvalds 
9931da177e4SLinus Torvalds 	igmp = (struct igmphdr *)skb_put(skb, sizeof(struct igmphdr));
9941da177e4SLinus Torvalds 	igmp->type	=
9951da177e4SLinus Torvalds 	msg->im_msgtype = assert;
9961da177e4SLinus Torvalds 	igmp->code	= 0;
997eddc9ec5SArnaldo Carvalho de Melo 	ip_hdr(skb)->tot_len = htons(skb->len);		/* Fix the length */
998b0e380b1SArnaldo Carvalho de Melo 	skb->transport_header = skb->network_header;
9991da177e4SLinus Torvalds 	}
10001da177e4SLinus Torvalds 
10014c968709SEric Dumazet 	rcu_read_lock();
10024c968709SEric Dumazet 	mroute_sk = rcu_dereference(mrt->mroute_sk);
10034c968709SEric Dumazet 	if (mroute_sk == NULL) {
10044c968709SEric Dumazet 		rcu_read_unlock();
10051da177e4SLinus Torvalds 		kfree_skb(skb);
10061da177e4SLinus Torvalds 		return -EINVAL;
10071da177e4SLinus Torvalds 	}
10081da177e4SLinus Torvalds 
1009a8cb16ddSEric Dumazet 	/* Deliver to mrouted */
1010a8cb16ddSEric Dumazet 
10114c968709SEric Dumazet 	ret = sock_queue_rcv_skb(mroute_sk, skb);
10124c968709SEric Dumazet 	rcu_read_unlock();
101370a269e6SBenjamin Thery 	if (ret < 0) {
1014e87cc472SJoe Perches 		net_warn_ratelimited("mroute: pending queue full, dropping entries\n");
10151da177e4SLinus Torvalds 		kfree_skb(skb);
10161da177e4SLinus Torvalds 	}
10171da177e4SLinus Torvalds 
10181da177e4SLinus Torvalds 	return ret;
10191da177e4SLinus Torvalds }
10201da177e4SLinus Torvalds 
10211da177e4SLinus Torvalds /*
10221da177e4SLinus Torvalds  *	Queue a packet for resolution. It gets locked cache entry!
10231da177e4SLinus Torvalds  */
10241da177e4SLinus Torvalds 
10251da177e4SLinus Torvalds static int
10260c12295aSPatrick McHardy ipmr_cache_unresolved(struct mr_table *mrt, vifi_t vifi, struct sk_buff *skb)
10271da177e4SLinus Torvalds {
1028862465f2SPatrick McHardy 	bool found = false;
10291da177e4SLinus Torvalds 	int err;
10301da177e4SLinus Torvalds 	struct mfc_cache *c;
1031eddc9ec5SArnaldo Carvalho de Melo 	const struct iphdr *iph = ip_hdr(skb);
10321da177e4SLinus Torvalds 
10331da177e4SLinus Torvalds 	spin_lock_bh(&mfc_unres_lock);
10340c12295aSPatrick McHardy 	list_for_each_entry(c, &mrt->mfc_unres_queue, list) {
1035e258beb2SPatrick McHardy 		if (c->mfc_mcastgrp == iph->daddr &&
1036862465f2SPatrick McHardy 		    c->mfc_origin == iph->saddr) {
1037862465f2SPatrick McHardy 			found = true;
10381da177e4SLinus Torvalds 			break;
10391da177e4SLinus Torvalds 		}
1040862465f2SPatrick McHardy 	}
10411da177e4SLinus Torvalds 
1042862465f2SPatrick McHardy 	if (!found) {
1043a8cb16ddSEric Dumazet 		/* Create a new entry if allowable */
10441da177e4SLinus Torvalds 
10450c12295aSPatrick McHardy 		if (atomic_read(&mrt->cache_resolve_queue_len) >= 10 ||
1046d658f8a0SPatrick McHardy 		    (c = ipmr_cache_alloc_unres()) == NULL) {
10471da177e4SLinus Torvalds 			spin_unlock_bh(&mfc_unres_lock);
10481da177e4SLinus Torvalds 
10491da177e4SLinus Torvalds 			kfree_skb(skb);
10501da177e4SLinus Torvalds 			return -ENOBUFS;
10511da177e4SLinus Torvalds 		}
10521da177e4SLinus Torvalds 
1053a8cb16ddSEric Dumazet 		/* Fill in the new cache entry */
1054a8cb16ddSEric Dumazet 
10551da177e4SLinus Torvalds 		c->mfc_parent	= -1;
1056eddc9ec5SArnaldo Carvalho de Melo 		c->mfc_origin	= iph->saddr;
1057eddc9ec5SArnaldo Carvalho de Melo 		c->mfc_mcastgrp	= iph->daddr;
10581da177e4SLinus Torvalds 
1059a8cb16ddSEric Dumazet 		/* Reflect first query at mrouted. */
1060a8cb16ddSEric Dumazet 
10610c12295aSPatrick McHardy 		err = ipmr_cache_report(mrt, skb, vifi, IGMPMSG_NOCACHE);
10624feb88e5SBenjamin Thery 		if (err < 0) {
10631da177e4SLinus Torvalds 			/* If the report failed throw the cache entry
10641da177e4SLinus Torvalds 			   out - Brad Parker
10651da177e4SLinus Torvalds 			 */
10661da177e4SLinus Torvalds 			spin_unlock_bh(&mfc_unres_lock);
10671da177e4SLinus Torvalds 
10685c0a66f5SBenjamin Thery 			ipmr_cache_free(c);
10691da177e4SLinus Torvalds 			kfree_skb(skb);
10701da177e4SLinus Torvalds 			return err;
10711da177e4SLinus Torvalds 		}
10721da177e4SLinus Torvalds 
10730c12295aSPatrick McHardy 		atomic_inc(&mrt->cache_resolve_queue_len);
10740c12295aSPatrick McHardy 		list_add(&c->list, &mrt->mfc_unres_queue);
10758cd3ac9fSNicolas Dichtel 		mroute_netlink_event(mrt, c, RTM_NEWROUTE);
10761da177e4SLinus Torvalds 
1077278554bdSDavid S. Miller 		if (atomic_read(&mrt->cache_resolve_queue_len) == 1)
10780c12295aSPatrick McHardy 			mod_timer(&mrt->ipmr_expire_timer, c->mfc_un.unres.expires);
10791da177e4SLinus Torvalds 	}
10801da177e4SLinus Torvalds 
1081a8cb16ddSEric Dumazet 	/* See if we can append the packet */
1082a8cb16ddSEric Dumazet 
10831da177e4SLinus Torvalds 	if (c->mfc_un.unres.unresolved.qlen > 3) {
10841da177e4SLinus Torvalds 		kfree_skb(skb);
10851da177e4SLinus Torvalds 		err = -ENOBUFS;
10861da177e4SLinus Torvalds 	} else {
10871da177e4SLinus Torvalds 		skb_queue_tail(&c->mfc_un.unres.unresolved, skb);
10881da177e4SLinus Torvalds 		err = 0;
10891da177e4SLinus Torvalds 	}
10901da177e4SLinus Torvalds 
10911da177e4SLinus Torvalds 	spin_unlock_bh(&mfc_unres_lock);
10921da177e4SLinus Torvalds 	return err;
10931da177e4SLinus Torvalds }
10941da177e4SLinus Torvalds 
10951da177e4SLinus Torvalds /*
10961da177e4SLinus Torvalds  *	MFC cache manipulation by user space mroute daemon
10971da177e4SLinus Torvalds  */
10981da177e4SLinus Torvalds 
1099660b26dcSNicolas Dichtel static int ipmr_mfc_delete(struct mr_table *mrt, struct mfcctl *mfc, int parent)
11001da177e4SLinus Torvalds {
11011da177e4SLinus Torvalds 	int line;
1102862465f2SPatrick McHardy 	struct mfc_cache *c, *next;
11031da177e4SLinus Torvalds 
11041da177e4SLinus Torvalds 	line = MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr);
11051da177e4SLinus Torvalds 
11060c12295aSPatrick McHardy 	list_for_each_entry_safe(c, next, &mrt->mfc_cache_array[line], list) {
11071da177e4SLinus Torvalds 		if (c->mfc_origin == mfc->mfcc_origin.s_addr &&
1108660b26dcSNicolas Dichtel 		    c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr &&
1109660b26dcSNicolas Dichtel 		    (parent == -1 || parent == c->mfc_parent)) {
1110a8c9486bSEric Dumazet 			list_del_rcu(&c->list);
11118cd3ac9fSNicolas Dichtel 			mroute_netlink_event(mrt, c, RTM_DELROUTE);
11125c0a66f5SBenjamin Thery 			ipmr_cache_free(c);
11131da177e4SLinus Torvalds 			return 0;
11141da177e4SLinus Torvalds 		}
11151da177e4SLinus Torvalds 	}
11161da177e4SLinus Torvalds 	return -ENOENT;
11171da177e4SLinus Torvalds }
11181da177e4SLinus Torvalds 
11190c12295aSPatrick McHardy static int ipmr_mfc_add(struct net *net, struct mr_table *mrt,
1120660b26dcSNicolas Dichtel 			struct mfcctl *mfc, int mrtsock, int parent)
11211da177e4SLinus Torvalds {
1122862465f2SPatrick McHardy 	bool found = false;
11231da177e4SLinus Torvalds 	int line;
1124862465f2SPatrick McHardy 	struct mfc_cache *uc, *c;
11251da177e4SLinus Torvalds 
1126a50436f2SPatrick McHardy 	if (mfc->mfcc_parent >= MAXVIFS)
1127a50436f2SPatrick McHardy 		return -ENFILE;
1128a50436f2SPatrick McHardy 
11291da177e4SLinus Torvalds 	line = MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr);
11301da177e4SLinus Torvalds 
11310c12295aSPatrick McHardy 	list_for_each_entry(c, &mrt->mfc_cache_array[line], list) {
11321da177e4SLinus Torvalds 		if (c->mfc_origin == mfc->mfcc_origin.s_addr &&
1133660b26dcSNicolas Dichtel 		    c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr &&
1134660b26dcSNicolas Dichtel 		    (parent == -1 || parent == c->mfc_parent)) {
1135862465f2SPatrick McHardy 			found = true;
11361da177e4SLinus Torvalds 			break;
11371da177e4SLinus Torvalds 		}
1138862465f2SPatrick McHardy 	}
11391da177e4SLinus Torvalds 
1140862465f2SPatrick McHardy 	if (found) {
11411da177e4SLinus Torvalds 		write_lock_bh(&mrt_lock);
11421da177e4SLinus Torvalds 		c->mfc_parent = mfc->mfcc_parent;
11430c12295aSPatrick McHardy 		ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls);
11441da177e4SLinus Torvalds 		if (!mrtsock)
11451da177e4SLinus Torvalds 			c->mfc_flags |= MFC_STATIC;
11461da177e4SLinus Torvalds 		write_unlock_bh(&mrt_lock);
11478cd3ac9fSNicolas Dichtel 		mroute_netlink_event(mrt, c, RTM_NEWROUTE);
11481da177e4SLinus Torvalds 		return 0;
11491da177e4SLinus Torvalds 	}
11501da177e4SLinus Torvalds 
1151360eb5daSNicolas Dichtel 	if (mfc->mfcc_mcastgrp.s_addr != htonl(INADDR_ANY) &&
1152660b26dcSNicolas Dichtel 	    !ipv4_is_multicast(mfc->mfcc_mcastgrp.s_addr))
11531da177e4SLinus Torvalds 		return -EINVAL;
11541da177e4SLinus Torvalds 
1155d658f8a0SPatrick McHardy 	c = ipmr_cache_alloc();
11561da177e4SLinus Torvalds 	if (c == NULL)
11571da177e4SLinus Torvalds 		return -ENOMEM;
11581da177e4SLinus Torvalds 
11591da177e4SLinus Torvalds 	c->mfc_origin = mfc->mfcc_origin.s_addr;
11601da177e4SLinus Torvalds 	c->mfc_mcastgrp = mfc->mfcc_mcastgrp.s_addr;
11611da177e4SLinus Torvalds 	c->mfc_parent = mfc->mfcc_parent;
11620c12295aSPatrick McHardy 	ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls);
11631da177e4SLinus Torvalds 	if (!mrtsock)
11641da177e4SLinus Torvalds 		c->mfc_flags |= MFC_STATIC;
11651da177e4SLinus Torvalds 
1166a8c9486bSEric Dumazet 	list_add_rcu(&c->list, &mrt->mfc_cache_array[line]);
11671da177e4SLinus Torvalds 
11681da177e4SLinus Torvalds 	/*
11691da177e4SLinus Torvalds 	 *	Check to see if we resolved a queued list. If so we
11701da177e4SLinus Torvalds 	 *	need to send on the frames and tidy up.
11711da177e4SLinus Torvalds 	 */
1172b0ebb739SPatrick McHardy 	found = false;
11731da177e4SLinus Torvalds 	spin_lock_bh(&mfc_unres_lock);
11740c12295aSPatrick McHardy 	list_for_each_entry(uc, &mrt->mfc_unres_queue, list) {
1175e258beb2SPatrick McHardy 		if (uc->mfc_origin == c->mfc_origin &&
11761da177e4SLinus Torvalds 		    uc->mfc_mcastgrp == c->mfc_mcastgrp) {
1177862465f2SPatrick McHardy 			list_del(&uc->list);
11780c12295aSPatrick McHardy 			atomic_dec(&mrt->cache_resolve_queue_len);
1179b0ebb739SPatrick McHardy 			found = true;
11801da177e4SLinus Torvalds 			break;
11811da177e4SLinus Torvalds 		}
11821da177e4SLinus Torvalds 	}
11830c12295aSPatrick McHardy 	if (list_empty(&mrt->mfc_unres_queue))
11840c12295aSPatrick McHardy 		del_timer(&mrt->ipmr_expire_timer);
11851da177e4SLinus Torvalds 	spin_unlock_bh(&mfc_unres_lock);
11861da177e4SLinus Torvalds 
1187b0ebb739SPatrick McHardy 	if (found) {
11880c12295aSPatrick McHardy 		ipmr_cache_resolve(net, mrt, uc, c);
11895c0a66f5SBenjamin Thery 		ipmr_cache_free(uc);
11901da177e4SLinus Torvalds 	}
11918cd3ac9fSNicolas Dichtel 	mroute_netlink_event(mrt, c, RTM_NEWROUTE);
11921da177e4SLinus Torvalds 	return 0;
11931da177e4SLinus Torvalds }
11941da177e4SLinus Torvalds 
11951da177e4SLinus Torvalds /*
11961da177e4SLinus Torvalds  *	Close the multicast socket, and clear the vif tables etc
11971da177e4SLinus Torvalds  */
11981da177e4SLinus Torvalds 
11990c12295aSPatrick McHardy static void mroute_clean_tables(struct mr_table *mrt)
12001da177e4SLinus Torvalds {
12011da177e4SLinus Torvalds 	int i;
1202d17fa6faSEric Dumazet 	LIST_HEAD(list);
1203862465f2SPatrick McHardy 	struct mfc_cache *c, *next;
12041da177e4SLinus Torvalds 
1205a8cb16ddSEric Dumazet 	/* Shut down all active vif entries */
1206a8cb16ddSEric Dumazet 
12070c12295aSPatrick McHardy 	for (i = 0; i < mrt->maxvif; i++) {
12080c12295aSPatrick McHardy 		if (!(mrt->vif_table[i].flags & VIFF_STATIC))
12090c12295aSPatrick McHardy 			vif_delete(mrt, i, 0, &list);
12101da177e4SLinus Torvalds 	}
1211d17fa6faSEric Dumazet 	unregister_netdevice_many(&list);
12121da177e4SLinus Torvalds 
1213a8cb16ddSEric Dumazet 	/* Wipe the cache */
1214a8cb16ddSEric Dumazet 
12151da177e4SLinus Torvalds 	for (i = 0; i < MFC_LINES; i++) {
12160c12295aSPatrick McHardy 		list_for_each_entry_safe(c, next, &mrt->mfc_cache_array[i], list) {
1217862465f2SPatrick McHardy 			if (c->mfc_flags & MFC_STATIC)
12181da177e4SLinus Torvalds 				continue;
1219a8c9486bSEric Dumazet 			list_del_rcu(&c->list);
12208cd3ac9fSNicolas Dichtel 			mroute_netlink_event(mrt, c, RTM_DELROUTE);
12215c0a66f5SBenjamin Thery 			ipmr_cache_free(c);
12221da177e4SLinus Torvalds 		}
12231da177e4SLinus Torvalds 	}
12241da177e4SLinus Torvalds 
12250c12295aSPatrick McHardy 	if (atomic_read(&mrt->cache_resolve_queue_len) != 0) {
12261da177e4SLinus Torvalds 		spin_lock_bh(&mfc_unres_lock);
12270c12295aSPatrick McHardy 		list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
1228862465f2SPatrick McHardy 			list_del(&c->list);
12298cd3ac9fSNicolas Dichtel 			mroute_netlink_event(mrt, c, RTM_DELROUTE);
12300c12295aSPatrick McHardy 			ipmr_destroy_unres(mrt, c);
12311da177e4SLinus Torvalds 		}
12321da177e4SLinus Torvalds 		spin_unlock_bh(&mfc_unres_lock);
12331da177e4SLinus Torvalds 	}
12341da177e4SLinus Torvalds }
12351da177e4SLinus Torvalds 
12364c968709SEric Dumazet /* called from ip_ra_control(), before an RCU grace period,
12374c968709SEric Dumazet  * we dont need to call synchronize_rcu() here
12384c968709SEric Dumazet  */
12391da177e4SLinus Torvalds static void mrtsock_destruct(struct sock *sk)
12401da177e4SLinus Torvalds {
12414feb88e5SBenjamin Thery 	struct net *net = sock_net(sk);
1242f0ad0860SPatrick McHardy 	struct mr_table *mrt;
12434feb88e5SBenjamin Thery 
12441da177e4SLinus Torvalds 	rtnl_lock();
1245f0ad0860SPatrick McHardy 	ipmr_for_each_table(mrt, net) {
12464c968709SEric Dumazet 		if (sk == rtnl_dereference(mrt->mroute_sk)) {
12474feb88e5SBenjamin Thery 			IPV4_DEVCONF_ALL(net, MC_FORWARDING)--;
1248d67b8c61SNicolas Dichtel 			inet_netconf_notify_devconf(net, NETCONFA_MC_FORWARDING,
1249d67b8c61SNicolas Dichtel 						    NETCONFA_IFINDEX_ALL,
1250d67b8c61SNicolas Dichtel 						    net->ipv4.devconf_all);
1251a9b3cd7fSStephen Hemminger 			RCU_INIT_POINTER(mrt->mroute_sk, NULL);
12520c12295aSPatrick McHardy 			mroute_clean_tables(mrt);
12531da177e4SLinus Torvalds 		}
12541da177e4SLinus Torvalds 	}
12551da177e4SLinus Torvalds 	rtnl_unlock();
12561da177e4SLinus Torvalds }
12571da177e4SLinus Torvalds 
12581da177e4SLinus Torvalds /*
12591da177e4SLinus Torvalds  *	Socket options and virtual interface manipulation. The whole
12601da177e4SLinus Torvalds  *	virtual interface system is a complete heap, but unfortunately
12611da177e4SLinus Torvalds  *	that's how BSD mrouted happens to think. Maybe one day with a proper
12621da177e4SLinus Torvalds  *	MOSPF/PIM router set up we can clean this up.
12631da177e4SLinus Torvalds  */
12641da177e4SLinus Torvalds 
1265b7058842SDavid S. Miller int ip_mroute_setsockopt(struct sock *sk, int optname, char __user *optval, unsigned int optlen)
12661da177e4SLinus Torvalds {
1267660b26dcSNicolas Dichtel 	int ret, parent = 0;
12681da177e4SLinus Torvalds 	struct vifctl vif;
12691da177e4SLinus Torvalds 	struct mfcctl mfc;
12704feb88e5SBenjamin Thery 	struct net *net = sock_net(sk);
1271f0ad0860SPatrick McHardy 	struct mr_table *mrt;
1272f0ad0860SPatrick McHardy 
12735e1859fbSEric Dumazet 	if (sk->sk_type != SOCK_RAW ||
12745e1859fbSEric Dumazet 	    inet_sk(sk)->inet_num != IPPROTO_IGMP)
12755e1859fbSEric Dumazet 		return -EOPNOTSUPP;
12765e1859fbSEric Dumazet 
1277f0ad0860SPatrick McHardy 	mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1278f0ad0860SPatrick McHardy 	if (mrt == NULL)
1279f0ad0860SPatrick McHardy 		return -ENOENT;
12801da177e4SLinus Torvalds 
1281132adf54SStephen Hemminger 	if (optname != MRT_INIT) {
128233d480ceSEric Dumazet 		if (sk != rcu_access_pointer(mrt->mroute_sk) &&
128352e804c6SEric W. Biederman 		    !ns_capable(net->user_ns, CAP_NET_ADMIN))
12841da177e4SLinus Torvalds 			return -EACCES;
12851da177e4SLinus Torvalds 	}
12861da177e4SLinus Torvalds 
1287132adf54SStephen Hemminger 	switch (optname) {
12881da177e4SLinus Torvalds 	case MRT_INIT:
12891da177e4SLinus Torvalds 		if (optlen != sizeof(int))
12905e1859fbSEric Dumazet 			return -EINVAL;
12911da177e4SLinus Torvalds 
12921da177e4SLinus Torvalds 		rtnl_lock();
12934c968709SEric Dumazet 		if (rtnl_dereference(mrt->mroute_sk)) {
12941da177e4SLinus Torvalds 			rtnl_unlock();
12951da177e4SLinus Torvalds 			return -EADDRINUSE;
12961da177e4SLinus Torvalds 		}
12971da177e4SLinus Torvalds 
12981da177e4SLinus Torvalds 		ret = ip_ra_control(sk, 1, mrtsock_destruct);
12991da177e4SLinus Torvalds 		if (ret == 0) {
1300cf778b00SEric Dumazet 			rcu_assign_pointer(mrt->mroute_sk, sk);
13014feb88e5SBenjamin Thery 			IPV4_DEVCONF_ALL(net, MC_FORWARDING)++;
1302d67b8c61SNicolas Dichtel 			inet_netconf_notify_devconf(net, NETCONFA_MC_FORWARDING,
1303d67b8c61SNicolas Dichtel 						    NETCONFA_IFINDEX_ALL,
1304d67b8c61SNicolas Dichtel 						    net->ipv4.devconf_all);
13051da177e4SLinus Torvalds 		}
13061da177e4SLinus Torvalds 		rtnl_unlock();
13071da177e4SLinus Torvalds 		return ret;
13081da177e4SLinus Torvalds 	case MRT_DONE:
130933d480ceSEric Dumazet 		if (sk != rcu_access_pointer(mrt->mroute_sk))
13101da177e4SLinus Torvalds 			return -EACCES;
13111da177e4SLinus Torvalds 		return ip_ra_control(sk, 0, NULL);
13121da177e4SLinus Torvalds 	case MRT_ADD_VIF:
13131da177e4SLinus Torvalds 	case MRT_DEL_VIF:
13141da177e4SLinus Torvalds 		if (optlen != sizeof(vif))
13151da177e4SLinus Torvalds 			return -EINVAL;
13161da177e4SLinus Torvalds 		if (copy_from_user(&vif, optval, sizeof(vif)))
13171da177e4SLinus Torvalds 			return -EFAULT;
13181da177e4SLinus Torvalds 		if (vif.vifc_vifi >= MAXVIFS)
13191da177e4SLinus Torvalds 			return -ENFILE;
13201da177e4SLinus Torvalds 		rtnl_lock();
13211da177e4SLinus Torvalds 		if (optname == MRT_ADD_VIF) {
13224c968709SEric Dumazet 			ret = vif_add(net, mrt, &vif,
13234c968709SEric Dumazet 				      sk == rtnl_dereference(mrt->mroute_sk));
13241da177e4SLinus Torvalds 		} else {
13250c12295aSPatrick McHardy 			ret = vif_delete(mrt, vif.vifc_vifi, 0, NULL);
13261da177e4SLinus Torvalds 		}
13271da177e4SLinus Torvalds 		rtnl_unlock();
13281da177e4SLinus Torvalds 		return ret;
13291da177e4SLinus Torvalds 
13301da177e4SLinus Torvalds 		/*
13311da177e4SLinus Torvalds 		 *	Manipulate the forwarding caches. These live
13321da177e4SLinus Torvalds 		 *	in a sort of kernel/user symbiosis.
13331da177e4SLinus Torvalds 		 */
13341da177e4SLinus Torvalds 	case MRT_ADD_MFC:
13351da177e4SLinus Torvalds 	case MRT_DEL_MFC:
1336660b26dcSNicolas Dichtel 		parent = -1;
1337660b26dcSNicolas Dichtel 	case MRT_ADD_MFC_PROXY:
1338660b26dcSNicolas Dichtel 	case MRT_DEL_MFC_PROXY:
13391da177e4SLinus Torvalds 		if (optlen != sizeof(mfc))
13401da177e4SLinus Torvalds 			return -EINVAL;
13411da177e4SLinus Torvalds 		if (copy_from_user(&mfc, optval, sizeof(mfc)))
13421da177e4SLinus Torvalds 			return -EFAULT;
1343660b26dcSNicolas Dichtel 		if (parent == 0)
1344660b26dcSNicolas Dichtel 			parent = mfc.mfcc_parent;
13451da177e4SLinus Torvalds 		rtnl_lock();
1346660b26dcSNicolas Dichtel 		if (optname == MRT_DEL_MFC || optname == MRT_DEL_MFC_PROXY)
1347660b26dcSNicolas Dichtel 			ret = ipmr_mfc_delete(mrt, &mfc, parent);
13481da177e4SLinus Torvalds 		else
13494c968709SEric Dumazet 			ret = ipmr_mfc_add(net, mrt, &mfc,
1350660b26dcSNicolas Dichtel 					   sk == rtnl_dereference(mrt->mroute_sk),
1351660b26dcSNicolas Dichtel 					   parent);
13521da177e4SLinus Torvalds 		rtnl_unlock();
13531da177e4SLinus Torvalds 		return ret;
13541da177e4SLinus Torvalds 		/*
13551da177e4SLinus Torvalds 		 *	Control PIM assert.
13561da177e4SLinus Torvalds 		 */
13571da177e4SLinus Torvalds 	case MRT_ASSERT:
13581da177e4SLinus Torvalds 	{
13591da177e4SLinus Torvalds 		int v;
13605e1859fbSEric Dumazet 		if (optlen != sizeof(v))
13615e1859fbSEric Dumazet 			return -EINVAL;
13621da177e4SLinus Torvalds 		if (get_user(v, (int __user *)optval))
13631da177e4SLinus Torvalds 			return -EFAULT;
136453d6841dSJoe Perches 		mrt->mroute_do_assert = v;
13651da177e4SLinus Torvalds 		return 0;
13661da177e4SLinus Torvalds 	}
13671da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM
13681da177e4SLinus Torvalds 	case MRT_PIM:
13691da177e4SLinus Torvalds 	{
1370ba93ef74SStephen Hemminger 		int v;
1371ba93ef74SStephen Hemminger 
13725e1859fbSEric Dumazet 		if (optlen != sizeof(v))
13735e1859fbSEric Dumazet 			return -EINVAL;
13741da177e4SLinus Torvalds 		if (get_user(v, (int __user *)optval))
13751da177e4SLinus Torvalds 			return -EFAULT;
13765e1859fbSEric Dumazet 		v = !!v;
1377ba93ef74SStephen Hemminger 
13781da177e4SLinus Torvalds 		rtnl_lock();
13791da177e4SLinus Torvalds 		ret = 0;
13800c12295aSPatrick McHardy 		if (v != mrt->mroute_do_pim) {
13810c12295aSPatrick McHardy 			mrt->mroute_do_pim = v;
13820c12295aSPatrick McHardy 			mrt->mroute_do_assert = v;
13831da177e4SLinus Torvalds 		}
13841da177e4SLinus Torvalds 		rtnl_unlock();
13851da177e4SLinus Torvalds 		return ret;
13861da177e4SLinus Torvalds 	}
13871da177e4SLinus Torvalds #endif
1388f0ad0860SPatrick McHardy #ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
1389f0ad0860SPatrick McHardy 	case MRT_TABLE:
1390f0ad0860SPatrick McHardy 	{
1391f0ad0860SPatrick McHardy 		u32 v;
1392f0ad0860SPatrick McHardy 
1393f0ad0860SPatrick McHardy 		if (optlen != sizeof(u32))
1394f0ad0860SPatrick McHardy 			return -EINVAL;
1395f0ad0860SPatrick McHardy 		if (get_user(v, (u32 __user *)optval))
1396f0ad0860SPatrick McHardy 			return -EFAULT;
1397f0ad0860SPatrick McHardy 
1398b49d3c1eSEric Dumazet 		/* "pimreg%u" should not exceed 16 bytes (IFNAMSIZ) */
1399b49d3c1eSEric Dumazet 		if (v != RT_TABLE_DEFAULT && v >= 1000000000)
1400b49d3c1eSEric Dumazet 			return -EINVAL;
1401b49d3c1eSEric Dumazet 
1402f0ad0860SPatrick McHardy 		rtnl_lock();
1403f0ad0860SPatrick McHardy 		ret = 0;
14044c968709SEric Dumazet 		if (sk == rtnl_dereference(mrt->mroute_sk)) {
14054c968709SEric Dumazet 			ret = -EBUSY;
14064c968709SEric Dumazet 		} else {
1407f0ad0860SPatrick McHardy 			if (!ipmr_new_table(net, v))
1408f0ad0860SPatrick McHardy 				ret = -ENOMEM;
14095e1859fbSEric Dumazet 			else
1410f0ad0860SPatrick McHardy 				raw_sk(sk)->ipmr_table = v;
14114c968709SEric Dumazet 		}
1412f0ad0860SPatrick McHardy 		rtnl_unlock();
1413f0ad0860SPatrick McHardy 		return ret;
1414f0ad0860SPatrick McHardy 	}
1415f0ad0860SPatrick McHardy #endif
14161da177e4SLinus Torvalds 	/*
14171da177e4SLinus Torvalds 	 *	Spurious command, or MRT_VERSION which you cannot
14181da177e4SLinus Torvalds 	 *	set.
14191da177e4SLinus Torvalds 	 */
14201da177e4SLinus Torvalds 	default:
14211da177e4SLinus Torvalds 		return -ENOPROTOOPT;
14221da177e4SLinus Torvalds 	}
14231da177e4SLinus Torvalds }
14241da177e4SLinus Torvalds 
14251da177e4SLinus Torvalds /*
14261da177e4SLinus Torvalds  *	Getsock opt support for the multicast routing system.
14271da177e4SLinus Torvalds  */
14281da177e4SLinus Torvalds 
14291da177e4SLinus Torvalds int ip_mroute_getsockopt(struct sock *sk, int optname, char __user *optval, int __user *optlen)
14301da177e4SLinus Torvalds {
14311da177e4SLinus Torvalds 	int olr;
14321da177e4SLinus Torvalds 	int val;
14334feb88e5SBenjamin Thery 	struct net *net = sock_net(sk);
1434f0ad0860SPatrick McHardy 	struct mr_table *mrt;
1435f0ad0860SPatrick McHardy 
14365e1859fbSEric Dumazet 	if (sk->sk_type != SOCK_RAW ||
14375e1859fbSEric Dumazet 	    inet_sk(sk)->inet_num != IPPROTO_IGMP)
14385e1859fbSEric Dumazet 		return -EOPNOTSUPP;
14395e1859fbSEric Dumazet 
1440f0ad0860SPatrick McHardy 	mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1441f0ad0860SPatrick McHardy 	if (mrt == NULL)
1442f0ad0860SPatrick McHardy 		return -ENOENT;
14431da177e4SLinus Torvalds 
14441da177e4SLinus Torvalds 	if (optname != MRT_VERSION &&
14451da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM
14461da177e4SLinus Torvalds 	   optname != MRT_PIM &&
14471da177e4SLinus Torvalds #endif
14481da177e4SLinus Torvalds 	   optname != MRT_ASSERT)
14491da177e4SLinus Torvalds 		return -ENOPROTOOPT;
14501da177e4SLinus Torvalds 
14511da177e4SLinus Torvalds 	if (get_user(olr, optlen))
14521da177e4SLinus Torvalds 		return -EFAULT;
14531da177e4SLinus Torvalds 
14541da177e4SLinus Torvalds 	olr = min_t(unsigned int, olr, sizeof(int));
14551da177e4SLinus Torvalds 	if (olr < 0)
14561da177e4SLinus Torvalds 		return -EINVAL;
14571da177e4SLinus Torvalds 
14581da177e4SLinus Torvalds 	if (put_user(olr, optlen))
14591da177e4SLinus Torvalds 		return -EFAULT;
14601da177e4SLinus Torvalds 	if (optname == MRT_VERSION)
14611da177e4SLinus Torvalds 		val = 0x0305;
14621da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM
14631da177e4SLinus Torvalds 	else if (optname == MRT_PIM)
14640c12295aSPatrick McHardy 		val = mrt->mroute_do_pim;
14651da177e4SLinus Torvalds #endif
14661da177e4SLinus Torvalds 	else
14670c12295aSPatrick McHardy 		val = mrt->mroute_do_assert;
14681da177e4SLinus Torvalds 	if (copy_to_user(optval, &val, olr))
14691da177e4SLinus Torvalds 		return -EFAULT;
14701da177e4SLinus Torvalds 	return 0;
14711da177e4SLinus Torvalds }
14721da177e4SLinus Torvalds 
14731da177e4SLinus Torvalds /*
14741da177e4SLinus Torvalds  *	The IP multicast ioctl support routines.
14751da177e4SLinus Torvalds  */
14761da177e4SLinus Torvalds 
14771da177e4SLinus Torvalds int ipmr_ioctl(struct sock *sk, int cmd, void __user *arg)
14781da177e4SLinus Torvalds {
14791da177e4SLinus Torvalds 	struct sioc_sg_req sr;
14801da177e4SLinus Torvalds 	struct sioc_vif_req vr;
14811da177e4SLinus Torvalds 	struct vif_device *vif;
14821da177e4SLinus Torvalds 	struct mfc_cache *c;
14834feb88e5SBenjamin Thery 	struct net *net = sock_net(sk);
1484f0ad0860SPatrick McHardy 	struct mr_table *mrt;
1485f0ad0860SPatrick McHardy 
1486f0ad0860SPatrick McHardy 	mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1487f0ad0860SPatrick McHardy 	if (mrt == NULL)
1488f0ad0860SPatrick McHardy 		return -ENOENT;
14891da177e4SLinus Torvalds 
1490132adf54SStephen Hemminger 	switch (cmd) {
14911da177e4SLinus Torvalds 	case SIOCGETVIFCNT:
14921da177e4SLinus Torvalds 		if (copy_from_user(&vr, arg, sizeof(vr)))
14931da177e4SLinus Torvalds 			return -EFAULT;
14940c12295aSPatrick McHardy 		if (vr.vifi >= mrt->maxvif)
14951da177e4SLinus Torvalds 			return -EINVAL;
14961da177e4SLinus Torvalds 		read_lock(&mrt_lock);
14970c12295aSPatrick McHardy 		vif = &mrt->vif_table[vr.vifi];
14980c12295aSPatrick McHardy 		if (VIF_EXISTS(mrt, vr.vifi)) {
14991da177e4SLinus Torvalds 			vr.icount = vif->pkt_in;
15001da177e4SLinus Torvalds 			vr.ocount = vif->pkt_out;
15011da177e4SLinus Torvalds 			vr.ibytes = vif->bytes_in;
15021da177e4SLinus Torvalds 			vr.obytes = vif->bytes_out;
15031da177e4SLinus Torvalds 			read_unlock(&mrt_lock);
15041da177e4SLinus Torvalds 
15051da177e4SLinus Torvalds 			if (copy_to_user(arg, &vr, sizeof(vr)))
15061da177e4SLinus Torvalds 				return -EFAULT;
15071da177e4SLinus Torvalds 			return 0;
15081da177e4SLinus Torvalds 		}
15091da177e4SLinus Torvalds 		read_unlock(&mrt_lock);
15101da177e4SLinus Torvalds 		return -EADDRNOTAVAIL;
15111da177e4SLinus Torvalds 	case SIOCGETSGCNT:
15121da177e4SLinus Torvalds 		if (copy_from_user(&sr, arg, sizeof(sr)))
15131da177e4SLinus Torvalds 			return -EFAULT;
15141da177e4SLinus Torvalds 
1515a8c9486bSEric Dumazet 		rcu_read_lock();
15160c12295aSPatrick McHardy 		c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
15171da177e4SLinus Torvalds 		if (c) {
15181da177e4SLinus Torvalds 			sr.pktcnt = c->mfc_un.res.pkt;
15191da177e4SLinus Torvalds 			sr.bytecnt = c->mfc_un.res.bytes;
15201da177e4SLinus Torvalds 			sr.wrong_if = c->mfc_un.res.wrong_if;
1521a8c9486bSEric Dumazet 			rcu_read_unlock();
15221da177e4SLinus Torvalds 
15231da177e4SLinus Torvalds 			if (copy_to_user(arg, &sr, sizeof(sr)))
15241da177e4SLinus Torvalds 				return -EFAULT;
15251da177e4SLinus Torvalds 			return 0;
15261da177e4SLinus Torvalds 		}
1527a8c9486bSEric Dumazet 		rcu_read_unlock();
15281da177e4SLinus Torvalds 		return -EADDRNOTAVAIL;
15291da177e4SLinus Torvalds 	default:
15301da177e4SLinus Torvalds 		return -ENOIOCTLCMD;
15311da177e4SLinus Torvalds 	}
15321da177e4SLinus Torvalds }
15331da177e4SLinus Torvalds 
1534709b46e8SEric W. Biederman #ifdef CONFIG_COMPAT
1535709b46e8SEric W. Biederman struct compat_sioc_sg_req {
1536709b46e8SEric W. Biederman 	struct in_addr src;
1537709b46e8SEric W. Biederman 	struct in_addr grp;
1538709b46e8SEric W. Biederman 	compat_ulong_t pktcnt;
1539709b46e8SEric W. Biederman 	compat_ulong_t bytecnt;
1540709b46e8SEric W. Biederman 	compat_ulong_t wrong_if;
1541709b46e8SEric W. Biederman };
1542709b46e8SEric W. Biederman 
1543ca6b8bb0SDavid S. Miller struct compat_sioc_vif_req {
1544ca6b8bb0SDavid S. Miller 	vifi_t	vifi;		/* Which iface */
1545ca6b8bb0SDavid S. Miller 	compat_ulong_t icount;
1546ca6b8bb0SDavid S. Miller 	compat_ulong_t ocount;
1547ca6b8bb0SDavid S. Miller 	compat_ulong_t ibytes;
1548ca6b8bb0SDavid S. Miller 	compat_ulong_t obytes;
1549ca6b8bb0SDavid S. Miller };
1550ca6b8bb0SDavid S. Miller 
1551709b46e8SEric W. Biederman int ipmr_compat_ioctl(struct sock *sk, unsigned int cmd, void __user *arg)
1552709b46e8SEric W. Biederman {
15530033d5adSDavid S. Miller 	struct compat_sioc_sg_req sr;
1554ca6b8bb0SDavid S. Miller 	struct compat_sioc_vif_req vr;
1555ca6b8bb0SDavid S. Miller 	struct vif_device *vif;
1556709b46e8SEric W. Biederman 	struct mfc_cache *c;
1557709b46e8SEric W. Biederman 	struct net *net = sock_net(sk);
1558709b46e8SEric W. Biederman 	struct mr_table *mrt;
1559709b46e8SEric W. Biederman 
1560709b46e8SEric W. Biederman 	mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1561709b46e8SEric W. Biederman 	if (mrt == NULL)
1562709b46e8SEric W. Biederman 		return -ENOENT;
1563709b46e8SEric W. Biederman 
1564709b46e8SEric W. Biederman 	switch (cmd) {
1565ca6b8bb0SDavid S. Miller 	case SIOCGETVIFCNT:
1566ca6b8bb0SDavid S. Miller 		if (copy_from_user(&vr, arg, sizeof(vr)))
1567ca6b8bb0SDavid S. Miller 			return -EFAULT;
1568ca6b8bb0SDavid S. Miller 		if (vr.vifi >= mrt->maxvif)
1569ca6b8bb0SDavid S. Miller 			return -EINVAL;
1570ca6b8bb0SDavid S. Miller 		read_lock(&mrt_lock);
1571ca6b8bb0SDavid S. Miller 		vif = &mrt->vif_table[vr.vifi];
1572ca6b8bb0SDavid S. Miller 		if (VIF_EXISTS(mrt, vr.vifi)) {
1573ca6b8bb0SDavid S. Miller 			vr.icount = vif->pkt_in;
1574ca6b8bb0SDavid S. Miller 			vr.ocount = vif->pkt_out;
1575ca6b8bb0SDavid S. Miller 			vr.ibytes = vif->bytes_in;
1576ca6b8bb0SDavid S. Miller 			vr.obytes = vif->bytes_out;
1577ca6b8bb0SDavid S. Miller 			read_unlock(&mrt_lock);
1578ca6b8bb0SDavid S. Miller 
1579ca6b8bb0SDavid S. Miller 			if (copy_to_user(arg, &vr, sizeof(vr)))
1580ca6b8bb0SDavid S. Miller 				return -EFAULT;
1581ca6b8bb0SDavid S. Miller 			return 0;
1582ca6b8bb0SDavid S. Miller 		}
1583ca6b8bb0SDavid S. Miller 		read_unlock(&mrt_lock);
1584ca6b8bb0SDavid S. Miller 		return -EADDRNOTAVAIL;
1585709b46e8SEric W. Biederman 	case SIOCGETSGCNT:
1586709b46e8SEric W. Biederman 		if (copy_from_user(&sr, arg, sizeof(sr)))
1587709b46e8SEric W. Biederman 			return -EFAULT;
1588709b46e8SEric W. Biederman 
1589709b46e8SEric W. Biederman 		rcu_read_lock();
1590709b46e8SEric W. Biederman 		c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
1591709b46e8SEric W. Biederman 		if (c) {
1592709b46e8SEric W. Biederman 			sr.pktcnt = c->mfc_un.res.pkt;
1593709b46e8SEric W. Biederman 			sr.bytecnt = c->mfc_un.res.bytes;
1594709b46e8SEric W. Biederman 			sr.wrong_if = c->mfc_un.res.wrong_if;
1595709b46e8SEric W. Biederman 			rcu_read_unlock();
1596709b46e8SEric W. Biederman 
1597709b46e8SEric W. Biederman 			if (copy_to_user(arg, &sr, sizeof(sr)))
1598709b46e8SEric W. Biederman 				return -EFAULT;
1599709b46e8SEric W. Biederman 			return 0;
1600709b46e8SEric W. Biederman 		}
1601709b46e8SEric W. Biederman 		rcu_read_unlock();
1602709b46e8SEric W. Biederman 		return -EADDRNOTAVAIL;
1603709b46e8SEric W. Biederman 	default:
1604709b46e8SEric W. Biederman 		return -ENOIOCTLCMD;
1605709b46e8SEric W. Biederman 	}
1606709b46e8SEric W. Biederman }
1607709b46e8SEric W. Biederman #endif
1608709b46e8SEric W. Biederman 
16091da177e4SLinus Torvalds 
16101da177e4SLinus Torvalds static int ipmr_device_event(struct notifier_block *this, unsigned long event, void *ptr)
16111da177e4SLinus Torvalds {
1612351638e7SJiri Pirko 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
16134feb88e5SBenjamin Thery 	struct net *net = dev_net(dev);
1614f0ad0860SPatrick McHardy 	struct mr_table *mrt;
16151da177e4SLinus Torvalds 	struct vif_device *v;
16161da177e4SLinus Torvalds 	int ct;
1617e9dc8653SEric W. Biederman 
16181da177e4SLinus Torvalds 	if (event != NETDEV_UNREGISTER)
16191da177e4SLinus Torvalds 		return NOTIFY_DONE;
1620f0ad0860SPatrick McHardy 
1621f0ad0860SPatrick McHardy 	ipmr_for_each_table(mrt, net) {
16220c12295aSPatrick McHardy 		v = &mrt->vif_table[0];
16230c12295aSPatrick McHardy 		for (ct = 0; ct < mrt->maxvif; ct++, v++) {
1624e9dc8653SEric W. Biederman 			if (v->dev == dev)
1625e92036a6SRongQing.Li 				vif_delete(mrt, ct, 1, NULL);
16261da177e4SLinus Torvalds 		}
16271da177e4SLinus Torvalds 	}
16281da177e4SLinus Torvalds 	return NOTIFY_DONE;
16291da177e4SLinus Torvalds }
16301da177e4SLinus Torvalds 
16311da177e4SLinus Torvalds 
16321da177e4SLinus Torvalds static struct notifier_block ip_mr_notifier = {
16331da177e4SLinus Torvalds 	.notifier_call = ipmr_device_event,
16341da177e4SLinus Torvalds };
16351da177e4SLinus Torvalds 
16361da177e4SLinus Torvalds /*
16371da177e4SLinus Torvalds  *	Encapsulate a packet by attaching a valid IPIP header to it.
16381da177e4SLinus Torvalds  *	This avoids tunnel drivers and other mess and gives us the speed so
16391da177e4SLinus Torvalds  *	important for multicast video.
16401da177e4SLinus Torvalds  */
16411da177e4SLinus Torvalds 
1642114c7844SAl Viro static void ip_encap(struct sk_buff *skb, __be32 saddr, __be32 daddr)
16431da177e4SLinus Torvalds {
16448856dfa3SArnaldo Carvalho de Melo 	struct iphdr *iph;
1645b71d1d42SEric Dumazet 	const struct iphdr *old_iph = ip_hdr(skb);
16468856dfa3SArnaldo Carvalho de Melo 
16478856dfa3SArnaldo Carvalho de Melo 	skb_push(skb, sizeof(struct iphdr));
1648b0e380b1SArnaldo Carvalho de Melo 	skb->transport_header = skb->network_header;
16498856dfa3SArnaldo Carvalho de Melo 	skb_reset_network_header(skb);
1650eddc9ec5SArnaldo Carvalho de Melo 	iph = ip_hdr(skb);
16511da177e4SLinus Torvalds 
16521da177e4SLinus Torvalds 	iph->version	=	4;
1653e023dd64SArnaldo Carvalho de Melo 	iph->tos	=	old_iph->tos;
1654e023dd64SArnaldo Carvalho de Melo 	iph->ttl	=	old_iph->ttl;
16551da177e4SLinus Torvalds 	iph->frag_off	=	0;
16561da177e4SLinus Torvalds 	iph->daddr	=	daddr;
16571da177e4SLinus Torvalds 	iph->saddr	=	saddr;
16581da177e4SLinus Torvalds 	iph->protocol	=	IPPROTO_IPIP;
16591da177e4SLinus Torvalds 	iph->ihl	=	5;
16601da177e4SLinus Torvalds 	iph->tot_len	=	htons(skb->len);
1661adf30907SEric Dumazet 	ip_select_ident(iph, skb_dst(skb), NULL);
16621da177e4SLinus Torvalds 	ip_send_check(iph);
16631da177e4SLinus Torvalds 
16641da177e4SLinus Torvalds 	memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
16651da177e4SLinus Torvalds 	nf_reset(skb);
16661da177e4SLinus Torvalds }
16671da177e4SLinus Torvalds 
16681da177e4SLinus Torvalds static inline int ipmr_forward_finish(struct sk_buff *skb)
16691da177e4SLinus Torvalds {
16701da177e4SLinus Torvalds 	struct ip_options *opt = &(IPCB(skb)->opt);
16711da177e4SLinus Torvalds 
1672adf30907SEric Dumazet 	IP_INC_STATS_BH(dev_net(skb_dst(skb)->dev), IPSTATS_MIB_OUTFORWDATAGRAMS);
16732d8dbb04SVincent Bernat 	IP_ADD_STATS_BH(dev_net(skb_dst(skb)->dev), IPSTATS_MIB_OUTOCTETS, skb->len);
16741da177e4SLinus Torvalds 
16751da177e4SLinus Torvalds 	if (unlikely(opt->optlen))
16761da177e4SLinus Torvalds 		ip_forward_options(skb);
16771da177e4SLinus Torvalds 
16781da177e4SLinus Torvalds 	return dst_output(skb);
16791da177e4SLinus Torvalds }
16801da177e4SLinus Torvalds 
16811da177e4SLinus Torvalds /*
16821da177e4SLinus Torvalds  *	Processing handlers for ipmr_forward
16831da177e4SLinus Torvalds  */
16841da177e4SLinus Torvalds 
16850c12295aSPatrick McHardy static void ipmr_queue_xmit(struct net *net, struct mr_table *mrt,
16860c12295aSPatrick McHardy 			    struct sk_buff *skb, struct mfc_cache *c, int vifi)
16871da177e4SLinus Torvalds {
1688eddc9ec5SArnaldo Carvalho de Melo 	const struct iphdr *iph = ip_hdr(skb);
16890c12295aSPatrick McHardy 	struct vif_device *vif = &mrt->vif_table[vifi];
16901da177e4SLinus Torvalds 	struct net_device *dev;
16911da177e4SLinus Torvalds 	struct rtable *rt;
169231e4543dSDavid S. Miller 	struct flowi4 fl4;
16931da177e4SLinus Torvalds 	int    encap = 0;
16941da177e4SLinus Torvalds 
16951da177e4SLinus Torvalds 	if (vif->dev == NULL)
16961da177e4SLinus Torvalds 		goto out_free;
16971da177e4SLinus Torvalds 
16981da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM
16991da177e4SLinus Torvalds 	if (vif->flags & VIFF_REGISTER) {
17001da177e4SLinus Torvalds 		vif->pkt_out++;
17011da177e4SLinus Torvalds 		vif->bytes_out += skb->len;
1702cf3677aeSPavel Emelyanov 		vif->dev->stats.tx_bytes += skb->len;
1703cf3677aeSPavel Emelyanov 		vif->dev->stats.tx_packets++;
17040c12295aSPatrick McHardy 		ipmr_cache_report(mrt, skb, vifi, IGMPMSG_WHOLEPKT);
170569ebbf58SIlpo Järvinen 		goto out_free;
17061da177e4SLinus Torvalds 	}
17071da177e4SLinus Torvalds #endif
17081da177e4SLinus Torvalds 
17091da177e4SLinus Torvalds 	if (vif->flags & VIFF_TUNNEL) {
171031e4543dSDavid S. Miller 		rt = ip_route_output_ports(net, &fl4, NULL,
171178fbfd8aSDavid S. Miller 					   vif->remote, vif->local,
171278fbfd8aSDavid S. Miller 					   0, 0,
171378fbfd8aSDavid S. Miller 					   IPPROTO_IPIP,
171478fbfd8aSDavid S. Miller 					   RT_TOS(iph->tos), vif->link);
1715b23dd4feSDavid S. Miller 		if (IS_ERR(rt))
17161da177e4SLinus Torvalds 			goto out_free;
17171da177e4SLinus Torvalds 		encap = sizeof(struct iphdr);
17181da177e4SLinus Torvalds 	} else {
171931e4543dSDavid S. Miller 		rt = ip_route_output_ports(net, &fl4, NULL, iph->daddr, 0,
172078fbfd8aSDavid S. Miller 					   0, 0,
172178fbfd8aSDavid S. Miller 					   IPPROTO_IPIP,
172278fbfd8aSDavid S. Miller 					   RT_TOS(iph->tos), vif->link);
1723b23dd4feSDavid S. Miller 		if (IS_ERR(rt))
17241da177e4SLinus Torvalds 			goto out_free;
17251da177e4SLinus Torvalds 	}
17261da177e4SLinus Torvalds 
1727d8d1f30bSChangli Gao 	dev = rt->dst.dev;
17281da177e4SLinus Torvalds 
1729d8d1f30bSChangli Gao 	if (skb->len+encap > dst_mtu(&rt->dst) && (ntohs(iph->frag_off) & IP_DF)) {
17301da177e4SLinus Torvalds 		/* Do not fragment multicasts. Alas, IPv4 does not
1731a8cb16ddSEric Dumazet 		 * allow to send ICMP, so that packets will disappear
1732a8cb16ddSEric Dumazet 		 * to blackhole.
17331da177e4SLinus Torvalds 		 */
17341da177e4SLinus Torvalds 
17357c73a6faSPavel Emelyanov 		IP_INC_STATS_BH(dev_net(dev), IPSTATS_MIB_FRAGFAILS);
17361da177e4SLinus Torvalds 		ip_rt_put(rt);
17371da177e4SLinus Torvalds 		goto out_free;
17381da177e4SLinus Torvalds 	}
17391da177e4SLinus Torvalds 
1740d8d1f30bSChangli Gao 	encap += LL_RESERVED_SPACE(dev) + rt->dst.header_len;
17411da177e4SLinus Torvalds 
17421da177e4SLinus Torvalds 	if (skb_cow(skb, encap)) {
17431da177e4SLinus Torvalds 		ip_rt_put(rt);
17441da177e4SLinus Torvalds 		goto out_free;
17451da177e4SLinus Torvalds 	}
17461da177e4SLinus Torvalds 
17471da177e4SLinus Torvalds 	vif->pkt_out++;
17481da177e4SLinus Torvalds 	vif->bytes_out += skb->len;
17491da177e4SLinus Torvalds 
1750adf30907SEric Dumazet 	skb_dst_drop(skb);
1751d8d1f30bSChangli Gao 	skb_dst_set(skb, &rt->dst);
1752eddc9ec5SArnaldo Carvalho de Melo 	ip_decrease_ttl(ip_hdr(skb));
17531da177e4SLinus Torvalds 
17541da177e4SLinus Torvalds 	/* FIXME: forward and output firewalls used to be called here.
1755a8cb16ddSEric Dumazet 	 * What do we do with netfilter? -- RR
1756a8cb16ddSEric Dumazet 	 */
17571da177e4SLinus Torvalds 	if (vif->flags & VIFF_TUNNEL) {
17581da177e4SLinus Torvalds 		ip_encap(skb, vif->local, vif->remote);
17591da177e4SLinus Torvalds 		/* FIXME: extra output firewall step used to be here. --RR */
17602f4c02d4SPavel Emelyanov 		vif->dev->stats.tx_packets++;
17612f4c02d4SPavel Emelyanov 		vif->dev->stats.tx_bytes += skb->len;
17621da177e4SLinus Torvalds 	}
17631da177e4SLinus Torvalds 
17641da177e4SLinus Torvalds 	IPCB(skb)->flags |= IPSKB_FORWARDED;
17651da177e4SLinus Torvalds 
17661da177e4SLinus Torvalds 	/*
17671da177e4SLinus Torvalds 	 * RFC1584 teaches, that DVMRP/PIM router must deliver packets locally
17681da177e4SLinus Torvalds 	 * not only before forwarding, but after forwarding on all output
17691da177e4SLinus Torvalds 	 * interfaces. It is clear, if mrouter runs a multicasting
17701da177e4SLinus Torvalds 	 * program, it should receive packets not depending to what interface
17711da177e4SLinus Torvalds 	 * program is joined.
17721da177e4SLinus Torvalds 	 * If we will not make it, the program will have to join on all
17731da177e4SLinus Torvalds 	 * interfaces. On the other hand, multihoming host (or router, but
17741da177e4SLinus Torvalds 	 * not mrouter) cannot join to more than one interface - it will
17751da177e4SLinus Torvalds 	 * result in receiving multiple packets.
17761da177e4SLinus Torvalds 	 */
17779bbc768aSJan Engelhardt 	NF_HOOK(NFPROTO_IPV4, NF_INET_FORWARD, skb, skb->dev, dev,
17781da177e4SLinus Torvalds 		ipmr_forward_finish);
17791da177e4SLinus Torvalds 	return;
17801da177e4SLinus Torvalds 
17811da177e4SLinus Torvalds out_free:
17821da177e4SLinus Torvalds 	kfree_skb(skb);
17831da177e4SLinus Torvalds }
17841da177e4SLinus Torvalds 
17850c12295aSPatrick McHardy static int ipmr_find_vif(struct mr_table *mrt, struct net_device *dev)
17861da177e4SLinus Torvalds {
17871da177e4SLinus Torvalds 	int ct;
17880c12295aSPatrick McHardy 
17890c12295aSPatrick McHardy 	for (ct = mrt->maxvif-1; ct >= 0; ct--) {
17900c12295aSPatrick McHardy 		if (mrt->vif_table[ct].dev == dev)
17911da177e4SLinus Torvalds 			break;
17921da177e4SLinus Torvalds 	}
17931da177e4SLinus Torvalds 	return ct;
17941da177e4SLinus Torvalds }
17951da177e4SLinus Torvalds 
17961da177e4SLinus Torvalds /* "local" means that we should preserve one skb (for local delivery) */
17971da177e4SLinus Torvalds 
1798*c4854ec8SRami Rosen static void ip_mr_forward(struct net *net, struct mr_table *mrt,
17990c12295aSPatrick McHardy 			  struct sk_buff *skb, struct mfc_cache *cache,
18000c12295aSPatrick McHardy 			  int local)
18011da177e4SLinus Torvalds {
18021da177e4SLinus Torvalds 	int psend = -1;
18031da177e4SLinus Torvalds 	int vif, ct;
1804660b26dcSNicolas Dichtel 	int true_vifi = ipmr_find_vif(mrt, skb->dev);
18051da177e4SLinus Torvalds 
18061da177e4SLinus Torvalds 	vif = cache->mfc_parent;
18071da177e4SLinus Torvalds 	cache->mfc_un.res.pkt++;
18081da177e4SLinus Torvalds 	cache->mfc_un.res.bytes += skb->len;
18091da177e4SLinus Torvalds 
1810360eb5daSNicolas Dichtel 	if (cache->mfc_origin == htonl(INADDR_ANY) && true_vifi >= 0) {
1811660b26dcSNicolas Dichtel 		struct mfc_cache *cache_proxy;
1812660b26dcSNicolas Dichtel 
1813660b26dcSNicolas Dichtel 		/* For an (*,G) entry, we only check that the incomming
1814660b26dcSNicolas Dichtel 		 * interface is part of the static tree.
1815660b26dcSNicolas Dichtel 		 */
1816660b26dcSNicolas Dichtel 		cache_proxy = ipmr_cache_find_any_parent(mrt, vif);
1817660b26dcSNicolas Dichtel 		if (cache_proxy &&
1818660b26dcSNicolas Dichtel 		    cache_proxy->mfc_un.res.ttls[true_vifi] < 255)
1819660b26dcSNicolas Dichtel 			goto forward;
1820660b26dcSNicolas Dichtel 	}
1821660b26dcSNicolas Dichtel 
18221da177e4SLinus Torvalds 	/*
18231da177e4SLinus Torvalds 	 * Wrong interface: drop packet and (maybe) send PIM assert.
18241da177e4SLinus Torvalds 	 */
18250c12295aSPatrick McHardy 	if (mrt->vif_table[vif].dev != skb->dev) {
1826c7537967SDavid S. Miller 		if (rt_is_output_route(skb_rtable(skb))) {
18271da177e4SLinus Torvalds 			/* It is our own packet, looped back.
1828a8cb16ddSEric Dumazet 			 * Very complicated situation...
1829a8cb16ddSEric Dumazet 			 *
1830a8cb16ddSEric Dumazet 			 * The best workaround until routing daemons will be
1831a8cb16ddSEric Dumazet 			 * fixed is not to redistribute packet, if it was
1832a8cb16ddSEric Dumazet 			 * send through wrong interface. It means, that
1833a8cb16ddSEric Dumazet 			 * multicast applications WILL NOT work for
1834a8cb16ddSEric Dumazet 			 * (S,G), which have default multicast route pointing
1835a8cb16ddSEric Dumazet 			 * to wrong oif. In any case, it is not a good
1836a8cb16ddSEric Dumazet 			 * idea to use multicasting applications on router.
18371da177e4SLinus Torvalds 			 */
18381da177e4SLinus Torvalds 			goto dont_forward;
18391da177e4SLinus Torvalds 		}
18401da177e4SLinus Torvalds 
18411da177e4SLinus Torvalds 		cache->mfc_un.res.wrong_if++;
18421da177e4SLinus Torvalds 
18430c12295aSPatrick McHardy 		if (true_vifi >= 0 && mrt->mroute_do_assert &&
18441da177e4SLinus Torvalds 		    /* pimsm uses asserts, when switching from RPT to SPT,
1845a8cb16ddSEric Dumazet 		     * so that we cannot check that packet arrived on an oif.
1846a8cb16ddSEric Dumazet 		     * It is bad, but otherwise we would need to move pretty
1847a8cb16ddSEric Dumazet 		     * large chunk of pimd to kernel. Ough... --ANK
18481da177e4SLinus Torvalds 		     */
18490c12295aSPatrick McHardy 		    (mrt->mroute_do_pim ||
18506f9374a9SBenjamin Thery 		     cache->mfc_un.res.ttls[true_vifi] < 255) &&
18511da177e4SLinus Torvalds 		    time_after(jiffies,
18521da177e4SLinus Torvalds 			       cache->mfc_un.res.last_assert + MFC_ASSERT_THRESH)) {
18531da177e4SLinus Torvalds 			cache->mfc_un.res.last_assert = jiffies;
18540c12295aSPatrick McHardy 			ipmr_cache_report(mrt, skb, true_vifi, IGMPMSG_WRONGVIF);
18551da177e4SLinus Torvalds 		}
18561da177e4SLinus Torvalds 		goto dont_forward;
18571da177e4SLinus Torvalds 	}
18581da177e4SLinus Torvalds 
1859660b26dcSNicolas Dichtel forward:
18600c12295aSPatrick McHardy 	mrt->vif_table[vif].pkt_in++;
18610c12295aSPatrick McHardy 	mrt->vif_table[vif].bytes_in += skb->len;
18621da177e4SLinus Torvalds 
18631da177e4SLinus Torvalds 	/*
18641da177e4SLinus Torvalds 	 *	Forward the frame
18651da177e4SLinus Torvalds 	 */
1866360eb5daSNicolas Dichtel 	if (cache->mfc_origin == htonl(INADDR_ANY) &&
1867360eb5daSNicolas Dichtel 	    cache->mfc_mcastgrp == htonl(INADDR_ANY)) {
1868660b26dcSNicolas Dichtel 		if (true_vifi >= 0 &&
1869660b26dcSNicolas Dichtel 		    true_vifi != cache->mfc_parent &&
1870660b26dcSNicolas Dichtel 		    ip_hdr(skb)->ttl >
1871660b26dcSNicolas Dichtel 				cache->mfc_un.res.ttls[cache->mfc_parent]) {
1872660b26dcSNicolas Dichtel 			/* It's an (*,*) entry and the packet is not coming from
1873660b26dcSNicolas Dichtel 			 * the upstream: forward the packet to the upstream
1874660b26dcSNicolas Dichtel 			 * only.
1875660b26dcSNicolas Dichtel 			 */
1876660b26dcSNicolas Dichtel 			psend = cache->mfc_parent;
1877660b26dcSNicolas Dichtel 			goto last_forward;
1878660b26dcSNicolas Dichtel 		}
1879660b26dcSNicolas Dichtel 		goto dont_forward;
1880660b26dcSNicolas Dichtel 	}
1881a8cb16ddSEric Dumazet 	for (ct = cache->mfc_un.res.maxvif - 1;
1882a8cb16ddSEric Dumazet 	     ct >= cache->mfc_un.res.minvif; ct--) {
1883660b26dcSNicolas Dichtel 		/* For (*,G) entry, don't forward to the incoming interface */
1884360eb5daSNicolas Dichtel 		if ((cache->mfc_origin != htonl(INADDR_ANY) ||
1885360eb5daSNicolas Dichtel 		     ct != true_vifi) &&
1886660b26dcSNicolas Dichtel 		    ip_hdr(skb)->ttl > cache->mfc_un.res.ttls[ct]) {
18871da177e4SLinus Torvalds 			if (psend != -1) {
18881da177e4SLinus Torvalds 				struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
1889a8cb16ddSEric Dumazet 
18901da177e4SLinus Torvalds 				if (skb2)
18910c12295aSPatrick McHardy 					ipmr_queue_xmit(net, mrt, skb2, cache,
18920c12295aSPatrick McHardy 							psend);
18931da177e4SLinus Torvalds 			}
18941da177e4SLinus Torvalds 			psend = ct;
18951da177e4SLinus Torvalds 		}
18961da177e4SLinus Torvalds 	}
1897660b26dcSNicolas Dichtel last_forward:
18981da177e4SLinus Torvalds 	if (psend != -1) {
18991da177e4SLinus Torvalds 		if (local) {
19001da177e4SLinus Torvalds 			struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
1901a8cb16ddSEric Dumazet 
19021da177e4SLinus Torvalds 			if (skb2)
19030c12295aSPatrick McHardy 				ipmr_queue_xmit(net, mrt, skb2, cache, psend);
19041da177e4SLinus Torvalds 		} else {
19050c12295aSPatrick McHardy 			ipmr_queue_xmit(net, mrt, skb, cache, psend);
1906*c4854ec8SRami Rosen 			return;
19071da177e4SLinus Torvalds 		}
19081da177e4SLinus Torvalds 	}
19091da177e4SLinus Torvalds 
19101da177e4SLinus Torvalds dont_forward:
19111da177e4SLinus Torvalds 	if (!local)
19121da177e4SLinus Torvalds 		kfree_skb(skb);
19131da177e4SLinus Torvalds }
19141da177e4SLinus Torvalds 
1915417da66fSDavid S. Miller static struct mr_table *ipmr_rt_fib_lookup(struct net *net, struct sk_buff *skb)
1916ee3f1aafSDavid S. Miller {
1917417da66fSDavid S. Miller 	struct rtable *rt = skb_rtable(skb);
1918417da66fSDavid S. Miller 	struct iphdr *iph = ip_hdr(skb);
1919da91981bSDavid S. Miller 	struct flowi4 fl4 = {
1920417da66fSDavid S. Miller 		.daddr = iph->daddr,
1921417da66fSDavid S. Miller 		.saddr = iph->saddr,
1922b0fe4a31SJulian Anastasov 		.flowi4_tos = RT_TOS(iph->tos),
19234fd551d7SDavid S. Miller 		.flowi4_oif = (rt_is_output_route(rt) ?
19244fd551d7SDavid S. Miller 			       skb->dev->ifindex : 0),
19254fd551d7SDavid S. Miller 		.flowi4_iif = (rt_is_output_route(rt) ?
19261fb9489bSPavel Emelyanov 			       LOOPBACK_IFINDEX :
19274fd551d7SDavid S. Miller 			       skb->dev->ifindex),
1928b4869889SDavid Miller 		.flowi4_mark = skb->mark,
1929ee3f1aafSDavid S. Miller 	};
1930ee3f1aafSDavid S. Miller 	struct mr_table *mrt;
1931ee3f1aafSDavid S. Miller 	int err;
1932ee3f1aafSDavid S. Miller 
1933da91981bSDavid S. Miller 	err = ipmr_fib_lookup(net, &fl4, &mrt);
1934ee3f1aafSDavid S. Miller 	if (err)
1935ee3f1aafSDavid S. Miller 		return ERR_PTR(err);
1936ee3f1aafSDavid S. Miller 	return mrt;
1937ee3f1aafSDavid S. Miller }
19381da177e4SLinus Torvalds 
19391da177e4SLinus Torvalds /*
19401da177e4SLinus Torvalds  *	Multicast packets for forwarding arrive here
19414c968709SEric Dumazet  *	Called with rcu_read_lock();
19421da177e4SLinus Torvalds  */
19431da177e4SLinus Torvalds 
19441da177e4SLinus Torvalds int ip_mr_input(struct sk_buff *skb)
19451da177e4SLinus Torvalds {
19461da177e4SLinus Torvalds 	struct mfc_cache *cache;
19474feb88e5SBenjamin Thery 	struct net *net = dev_net(skb->dev);
1948511c3f92SEric Dumazet 	int local = skb_rtable(skb)->rt_flags & RTCF_LOCAL;
1949f0ad0860SPatrick McHardy 	struct mr_table *mrt;
19501da177e4SLinus Torvalds 
19511da177e4SLinus Torvalds 	/* Packet is looped back after forward, it should not be
1952a8cb16ddSEric Dumazet 	 * forwarded second time, but still can be delivered locally.
19531da177e4SLinus Torvalds 	 */
19541da177e4SLinus Torvalds 	if (IPCB(skb)->flags & IPSKB_FORWARDED)
19551da177e4SLinus Torvalds 		goto dont_forward;
19561da177e4SLinus Torvalds 
1957417da66fSDavid S. Miller 	mrt = ipmr_rt_fib_lookup(net, skb);
1958ee3f1aafSDavid S. Miller 	if (IS_ERR(mrt)) {
1959e40dbc51SBen Greear 		kfree_skb(skb);
1960ee3f1aafSDavid S. Miller 		return PTR_ERR(mrt);
19615e2b61f7SDavid S. Miller 	}
19621da177e4SLinus Torvalds 	if (!local) {
19631da177e4SLinus Torvalds 		if (IPCB(skb)->opt.router_alert) {
19641da177e4SLinus Torvalds 			if (ip_call_ra_chain(skb))
19651da177e4SLinus Torvalds 				return 0;
1966eddc9ec5SArnaldo Carvalho de Melo 		} else if (ip_hdr(skb)->protocol == IPPROTO_IGMP) {
19671da177e4SLinus Torvalds 			/* IGMPv1 (and broken IGMPv2 implementations sort of
19684c968709SEric Dumazet 			 * Cisco IOS <= 11.2(8)) do not put router alert
19694c968709SEric Dumazet 			 * option to IGMP packets destined to routable
19704c968709SEric Dumazet 			 * groups. It is very bad, because it means
19714c968709SEric Dumazet 			 * that we can forward NO IGMP messages.
19721da177e4SLinus Torvalds 			 */
19734c968709SEric Dumazet 			struct sock *mroute_sk;
19744c968709SEric Dumazet 
19754c968709SEric Dumazet 			mroute_sk = rcu_dereference(mrt->mroute_sk);
19764c968709SEric Dumazet 			if (mroute_sk) {
19772715bcf9SPatrick McHardy 				nf_reset(skb);
19784c968709SEric Dumazet 				raw_rcv(mroute_sk, skb);
19791da177e4SLinus Torvalds 				return 0;
19801da177e4SLinus Torvalds 			}
19811da177e4SLinus Torvalds 		    }
19821da177e4SLinus Torvalds 	}
19831da177e4SLinus Torvalds 
1984a8c9486bSEric Dumazet 	/* already under rcu_read_lock() */
19850c12295aSPatrick McHardy 	cache = ipmr_cache_find(mrt, ip_hdr(skb)->saddr, ip_hdr(skb)->daddr);
1986660b26dcSNicolas Dichtel 	if (cache == NULL) {
1987660b26dcSNicolas Dichtel 		int vif = ipmr_find_vif(mrt, skb->dev);
1988660b26dcSNicolas Dichtel 
1989660b26dcSNicolas Dichtel 		if (vif >= 0)
1990660b26dcSNicolas Dichtel 			cache = ipmr_cache_find_any(mrt, ip_hdr(skb)->daddr,
1991660b26dcSNicolas Dichtel 						    vif);
1992660b26dcSNicolas Dichtel 	}
19931da177e4SLinus Torvalds 
19941da177e4SLinus Torvalds 	/*
19951da177e4SLinus Torvalds 	 *	No usable cache entry
19961da177e4SLinus Torvalds 	 */
19971da177e4SLinus Torvalds 	if (cache == NULL) {
19981da177e4SLinus Torvalds 		int vif;
19991da177e4SLinus Torvalds 
20001da177e4SLinus Torvalds 		if (local) {
20011da177e4SLinus Torvalds 			struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
20021da177e4SLinus Torvalds 			ip_local_deliver(skb);
2003a8c9486bSEric Dumazet 			if (skb2 == NULL)
20041da177e4SLinus Torvalds 				return -ENOBUFS;
20051da177e4SLinus Torvalds 			skb = skb2;
20061da177e4SLinus Torvalds 		}
20071da177e4SLinus Torvalds 
2008a8c9486bSEric Dumazet 		read_lock(&mrt_lock);
20090c12295aSPatrick McHardy 		vif = ipmr_find_vif(mrt, skb->dev);
20101da177e4SLinus Torvalds 		if (vif >= 0) {
20110eae88f3SEric Dumazet 			int err2 = ipmr_cache_unresolved(mrt, vif, skb);
20121da177e4SLinus Torvalds 			read_unlock(&mrt_lock);
20131da177e4SLinus Torvalds 
20140eae88f3SEric Dumazet 			return err2;
20151da177e4SLinus Torvalds 		}
20161da177e4SLinus Torvalds 		read_unlock(&mrt_lock);
20171da177e4SLinus Torvalds 		kfree_skb(skb);
20181da177e4SLinus Torvalds 		return -ENODEV;
20191da177e4SLinus Torvalds 	}
20201da177e4SLinus Torvalds 
2021a8c9486bSEric Dumazet 	read_lock(&mrt_lock);
20220c12295aSPatrick McHardy 	ip_mr_forward(net, mrt, skb, cache, local);
20231da177e4SLinus Torvalds 	read_unlock(&mrt_lock);
20241da177e4SLinus Torvalds 
20251da177e4SLinus Torvalds 	if (local)
20261da177e4SLinus Torvalds 		return ip_local_deliver(skb);
20271da177e4SLinus Torvalds 
20281da177e4SLinus Torvalds 	return 0;
20291da177e4SLinus Torvalds 
20301da177e4SLinus Torvalds dont_forward:
20311da177e4SLinus Torvalds 	if (local)
20321da177e4SLinus Torvalds 		return ip_local_deliver(skb);
20331da177e4SLinus Torvalds 	kfree_skb(skb);
20341da177e4SLinus Torvalds 	return 0;
20351da177e4SLinus Torvalds }
20361da177e4SLinus Torvalds 
2037b1879204SIlpo Järvinen #ifdef CONFIG_IP_PIMSM
203855747a0aSEric Dumazet /* called with rcu_read_lock() */
2039f0ad0860SPatrick McHardy static int __pim_rcv(struct mr_table *mrt, struct sk_buff *skb,
2040f0ad0860SPatrick McHardy 		     unsigned int pimlen)
20411da177e4SLinus Torvalds {
20421da177e4SLinus Torvalds 	struct net_device *reg_dev = NULL;
2043b1879204SIlpo Järvinen 	struct iphdr *encap;
20441da177e4SLinus Torvalds 
2045b1879204SIlpo Järvinen 	encap = (struct iphdr *)(skb_transport_header(skb) + pimlen);
20461da177e4SLinus Torvalds 	/*
2047a8cb16ddSEric Dumazet 	 * Check that:
2048a8cb16ddSEric Dumazet 	 * a. packet is really sent to a multicast group
2049a8cb16ddSEric Dumazet 	 * b. packet is not a NULL-REGISTER
2050a8cb16ddSEric Dumazet 	 * c. packet is not truncated
20511da177e4SLinus Torvalds 	 */
2052f97c1e0cSJoe Perches 	if (!ipv4_is_multicast(encap->daddr) ||
20531da177e4SLinus Torvalds 	    encap->tot_len == 0 ||
2054b1879204SIlpo Järvinen 	    ntohs(encap->tot_len) + pimlen > skb->len)
2055b1879204SIlpo Järvinen 		return 1;
20561da177e4SLinus Torvalds 
20571da177e4SLinus Torvalds 	read_lock(&mrt_lock);
20580c12295aSPatrick McHardy 	if (mrt->mroute_reg_vif_num >= 0)
20590c12295aSPatrick McHardy 		reg_dev = mrt->vif_table[mrt->mroute_reg_vif_num].dev;
20601da177e4SLinus Torvalds 	read_unlock(&mrt_lock);
20611da177e4SLinus Torvalds 
20621da177e4SLinus Torvalds 	if (reg_dev == NULL)
2063b1879204SIlpo Järvinen 		return 1;
20641da177e4SLinus Torvalds 
2065b0e380b1SArnaldo Carvalho de Melo 	skb->mac_header = skb->network_header;
20661da177e4SLinus Torvalds 	skb_pull(skb, (u8 *)encap - skb->data);
206731c7711bSArnaldo Carvalho de Melo 	skb_reset_network_header(skb);
20681da177e4SLinus Torvalds 	skb->protocol = htons(ETH_P_IP);
206955747a0aSEric Dumazet 	skb->ip_summed = CHECKSUM_NONE;
20701da177e4SLinus Torvalds 	skb->pkt_type = PACKET_HOST;
2071d19d56ddSEric Dumazet 
2072d19d56ddSEric Dumazet 	skb_tunnel_rx(skb, reg_dev);
2073d19d56ddSEric Dumazet 
20741da177e4SLinus Torvalds 	netif_rx(skb);
2075b1879204SIlpo Järvinen 
207655747a0aSEric Dumazet 	return NET_RX_SUCCESS;
2077b1879204SIlpo Järvinen }
2078b1879204SIlpo Järvinen #endif
2079b1879204SIlpo Järvinen 
2080b1879204SIlpo Järvinen #ifdef CONFIG_IP_PIMSM_V1
2081b1879204SIlpo Järvinen /*
2082b1879204SIlpo Järvinen  * Handle IGMP messages of PIMv1
2083b1879204SIlpo Järvinen  */
2084b1879204SIlpo Järvinen 
2085b1879204SIlpo Järvinen int pim_rcv_v1(struct sk_buff *skb)
2086b1879204SIlpo Järvinen {
2087b1879204SIlpo Järvinen 	struct igmphdr *pim;
20884feb88e5SBenjamin Thery 	struct net *net = dev_net(skb->dev);
2089f0ad0860SPatrick McHardy 	struct mr_table *mrt;
2090b1879204SIlpo Järvinen 
2091b1879204SIlpo Järvinen 	if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
2092b1879204SIlpo Järvinen 		goto drop;
2093b1879204SIlpo Järvinen 
2094b1879204SIlpo Järvinen 	pim = igmp_hdr(skb);
2095b1879204SIlpo Järvinen 
2096417da66fSDavid S. Miller 	mrt = ipmr_rt_fib_lookup(net, skb);
2097ee3f1aafSDavid S. Miller 	if (IS_ERR(mrt))
2098f0ad0860SPatrick McHardy 		goto drop;
20990c12295aSPatrick McHardy 	if (!mrt->mroute_do_pim ||
2100b1879204SIlpo Järvinen 	    pim->group != PIM_V1_VERSION || pim->code != PIM_V1_REGISTER)
2101b1879204SIlpo Järvinen 		goto drop;
2102b1879204SIlpo Järvinen 
2103f0ad0860SPatrick McHardy 	if (__pim_rcv(mrt, skb, sizeof(*pim))) {
21041da177e4SLinus Torvalds drop:
21051da177e4SLinus Torvalds 		kfree_skb(skb);
2106b1879204SIlpo Järvinen 	}
21071da177e4SLinus Torvalds 	return 0;
21081da177e4SLinus Torvalds }
21091da177e4SLinus Torvalds #endif
21101da177e4SLinus Torvalds 
21111da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM_V2
21121da177e4SLinus Torvalds static int pim_rcv(struct sk_buff *skb)
21131da177e4SLinus Torvalds {
21141da177e4SLinus Torvalds 	struct pimreghdr *pim;
2115f0ad0860SPatrick McHardy 	struct net *net = dev_net(skb->dev);
2116f0ad0860SPatrick McHardy 	struct mr_table *mrt;
21171da177e4SLinus Torvalds 
2118b1879204SIlpo Järvinen 	if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
21191da177e4SLinus Torvalds 		goto drop;
21201da177e4SLinus Torvalds 
21219c70220bSArnaldo Carvalho de Melo 	pim = (struct pimreghdr *)skb_transport_header(skb);
21221da177e4SLinus Torvalds 	if (pim->type != ((PIM_VERSION << 4) | (PIM_REGISTER)) ||
21231da177e4SLinus Torvalds 	    (pim->flags & PIM_NULL_REGISTER) ||
21241da177e4SLinus Torvalds 	    (ip_compute_csum((void *)pim, sizeof(*pim)) != 0 &&
2125d3bc23e7SAl Viro 	     csum_fold(skb_checksum(skb, 0, skb->len, 0))))
21261da177e4SLinus Torvalds 		goto drop;
21271da177e4SLinus Torvalds 
2128417da66fSDavid S. Miller 	mrt = ipmr_rt_fib_lookup(net, skb);
2129ee3f1aafSDavid S. Miller 	if (IS_ERR(mrt))
2130f0ad0860SPatrick McHardy 		goto drop;
2131f0ad0860SPatrick McHardy 	if (__pim_rcv(mrt, skb, sizeof(*pim))) {
21321da177e4SLinus Torvalds drop:
21331da177e4SLinus Torvalds 		kfree_skb(skb);
2134b1879204SIlpo Järvinen 	}
21351da177e4SLinus Torvalds 	return 0;
21361da177e4SLinus Torvalds }
21371da177e4SLinus Torvalds #endif
21381da177e4SLinus Torvalds 
2139cb6a4e46SPatrick McHardy static int __ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
2140cb6a4e46SPatrick McHardy 			      struct mfc_cache *c, struct rtmsg *rtm)
21411da177e4SLinus Torvalds {
21421da177e4SLinus Torvalds 	int ct;
21431da177e4SLinus Torvalds 	struct rtnexthop *nhp;
214492a395e5SThomas Graf 	struct nlattr *mp_attr;
2145adfa85e4SNicolas Dichtel 	struct rta_mfc_stats mfcs;
21461da177e4SLinus Torvalds 
21477438189bSNicolas Dichtel 	/* If cache is unresolved, don't try to parse IIF and OIF */
2148ed0f160aSDan Carpenter 	if (c->mfc_parent >= MAXVIFS)
21497438189bSNicolas Dichtel 		return -ENOENT;
21507438189bSNicolas Dichtel 
215192a395e5SThomas Graf 	if (VIF_EXISTS(mrt, c->mfc_parent) &&
215292a395e5SThomas Graf 	    nla_put_u32(skb, RTA_IIF, mrt->vif_table[c->mfc_parent].dev->ifindex) < 0)
215392a395e5SThomas Graf 		return -EMSGSIZE;
21541da177e4SLinus Torvalds 
215592a395e5SThomas Graf 	if (!(mp_attr = nla_nest_start(skb, RTA_MULTIPATH)))
215692a395e5SThomas Graf 		return -EMSGSIZE;
21571da177e4SLinus Torvalds 
21581da177e4SLinus Torvalds 	for (ct = c->mfc_un.res.minvif; ct < c->mfc_un.res.maxvif; ct++) {
21590c12295aSPatrick McHardy 		if (VIF_EXISTS(mrt, ct) && c->mfc_un.res.ttls[ct] < 255) {
216092a395e5SThomas Graf 			if (!(nhp = nla_reserve_nohdr(skb, sizeof(*nhp)))) {
216192a395e5SThomas Graf 				nla_nest_cancel(skb, mp_attr);
216292a395e5SThomas Graf 				return -EMSGSIZE;
216392a395e5SThomas Graf 			}
216492a395e5SThomas Graf 
21651da177e4SLinus Torvalds 			nhp->rtnh_flags = 0;
21661da177e4SLinus Torvalds 			nhp->rtnh_hops = c->mfc_un.res.ttls[ct];
21670c12295aSPatrick McHardy 			nhp->rtnh_ifindex = mrt->vif_table[ct].dev->ifindex;
21681da177e4SLinus Torvalds 			nhp->rtnh_len = sizeof(*nhp);
21691da177e4SLinus Torvalds 		}
21701da177e4SLinus Torvalds 	}
217192a395e5SThomas Graf 
217292a395e5SThomas Graf 	nla_nest_end(skb, mp_attr);
217392a395e5SThomas Graf 
2174adfa85e4SNicolas Dichtel 	mfcs.mfcs_packets = c->mfc_un.res.pkt;
2175adfa85e4SNicolas Dichtel 	mfcs.mfcs_bytes = c->mfc_un.res.bytes;
2176adfa85e4SNicolas Dichtel 	mfcs.mfcs_wrong_if = c->mfc_un.res.wrong_if;
2177adfa85e4SNicolas Dichtel 	if (nla_put(skb, RTA_MFC_STATS, sizeof(mfcs), &mfcs) < 0)
2178adfa85e4SNicolas Dichtel 		return -EMSGSIZE;
2179adfa85e4SNicolas Dichtel 
21801da177e4SLinus Torvalds 	rtm->rtm_type = RTN_MULTICAST;
21811da177e4SLinus Torvalds 	return 1;
21821da177e4SLinus Torvalds }
21831da177e4SLinus Torvalds 
21849a1b9496SDavid S. Miller int ipmr_get_route(struct net *net, struct sk_buff *skb,
21859a1b9496SDavid S. Miller 		   __be32 saddr, __be32 daddr,
21869a1b9496SDavid S. Miller 		   struct rtmsg *rtm, int nowait)
21871da177e4SLinus Torvalds {
21881da177e4SLinus Torvalds 	struct mfc_cache *cache;
21899a1b9496SDavid S. Miller 	struct mr_table *mrt;
21909a1b9496SDavid S. Miller 	int err;
21911da177e4SLinus Torvalds 
2192f0ad0860SPatrick McHardy 	mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
2193f0ad0860SPatrick McHardy 	if (mrt == NULL)
2194f0ad0860SPatrick McHardy 		return -ENOENT;
2195f0ad0860SPatrick McHardy 
2196a8c9486bSEric Dumazet 	rcu_read_lock();
21979a1b9496SDavid S. Miller 	cache = ipmr_cache_find(mrt, saddr, daddr);
2198660b26dcSNicolas Dichtel 	if (cache == NULL && skb->dev) {
2199660b26dcSNicolas Dichtel 		int vif = ipmr_find_vif(mrt, skb->dev);
22001da177e4SLinus Torvalds 
2201660b26dcSNicolas Dichtel 		if (vif >= 0)
2202660b26dcSNicolas Dichtel 			cache = ipmr_cache_find_any(mrt, daddr, vif);
2203660b26dcSNicolas Dichtel 	}
22041da177e4SLinus Torvalds 	if (cache == NULL) {
220572287490SAlexey Kuznetsov 		struct sk_buff *skb2;
2206eddc9ec5SArnaldo Carvalho de Melo 		struct iphdr *iph;
22071da177e4SLinus Torvalds 		struct net_device *dev;
2208a8cb16ddSEric Dumazet 		int vif = -1;
22091da177e4SLinus Torvalds 
22101da177e4SLinus Torvalds 		if (nowait) {
2211a8c9486bSEric Dumazet 			rcu_read_unlock();
22121da177e4SLinus Torvalds 			return -EAGAIN;
22131da177e4SLinus Torvalds 		}
22141da177e4SLinus Torvalds 
22151da177e4SLinus Torvalds 		dev = skb->dev;
2216a8c9486bSEric Dumazet 		read_lock(&mrt_lock);
2217a8cb16ddSEric Dumazet 		if (dev)
2218a8cb16ddSEric Dumazet 			vif = ipmr_find_vif(mrt, dev);
2219a8cb16ddSEric Dumazet 		if (vif < 0) {
22201da177e4SLinus Torvalds 			read_unlock(&mrt_lock);
2221a8c9486bSEric Dumazet 			rcu_read_unlock();
22221da177e4SLinus Torvalds 			return -ENODEV;
22231da177e4SLinus Torvalds 		}
222472287490SAlexey Kuznetsov 		skb2 = skb_clone(skb, GFP_ATOMIC);
222572287490SAlexey Kuznetsov 		if (!skb2) {
222672287490SAlexey Kuznetsov 			read_unlock(&mrt_lock);
2227a8c9486bSEric Dumazet 			rcu_read_unlock();
222872287490SAlexey Kuznetsov 			return -ENOMEM;
222972287490SAlexey Kuznetsov 		}
223072287490SAlexey Kuznetsov 
2231e2d1bca7SArnaldo Carvalho de Melo 		skb_push(skb2, sizeof(struct iphdr));
2232e2d1bca7SArnaldo Carvalho de Melo 		skb_reset_network_header(skb2);
2233eddc9ec5SArnaldo Carvalho de Melo 		iph = ip_hdr(skb2);
2234eddc9ec5SArnaldo Carvalho de Melo 		iph->ihl = sizeof(struct iphdr) >> 2;
22359a1b9496SDavid S. Miller 		iph->saddr = saddr;
22369a1b9496SDavid S. Miller 		iph->daddr = daddr;
2237eddc9ec5SArnaldo Carvalho de Melo 		iph->version = 0;
22380c12295aSPatrick McHardy 		err = ipmr_cache_unresolved(mrt, vif, skb2);
22391da177e4SLinus Torvalds 		read_unlock(&mrt_lock);
2240a8c9486bSEric Dumazet 		rcu_read_unlock();
22411da177e4SLinus Torvalds 		return err;
22421da177e4SLinus Torvalds 	}
22431da177e4SLinus Torvalds 
2244a8c9486bSEric Dumazet 	read_lock(&mrt_lock);
22451da177e4SLinus Torvalds 	if (!nowait && (rtm->rtm_flags & RTM_F_NOTIFY))
22461da177e4SLinus Torvalds 		cache->mfc_flags |= MFC_NOTIFY;
2247cb6a4e46SPatrick McHardy 	err = __ipmr_fill_mroute(mrt, skb, cache, rtm);
22481da177e4SLinus Torvalds 	read_unlock(&mrt_lock);
2249a8c9486bSEric Dumazet 	rcu_read_unlock();
22501da177e4SLinus Torvalds 	return err;
22511da177e4SLinus Torvalds }
22521da177e4SLinus Torvalds 
2253cb6a4e46SPatrick McHardy static int ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
22548cd3ac9fSNicolas Dichtel 			    u32 portid, u32 seq, struct mfc_cache *c, int cmd)
2255cb6a4e46SPatrick McHardy {
2256cb6a4e46SPatrick McHardy 	struct nlmsghdr *nlh;
2257cb6a4e46SPatrick McHardy 	struct rtmsg *rtm;
22581eb99af5SNicolas Dichtel 	int err;
2259cb6a4e46SPatrick McHardy 
22608cd3ac9fSNicolas Dichtel 	nlh = nlmsg_put(skb, portid, seq, cmd, sizeof(*rtm), NLM_F_MULTI);
2261cb6a4e46SPatrick McHardy 	if (nlh == NULL)
2262cb6a4e46SPatrick McHardy 		return -EMSGSIZE;
2263cb6a4e46SPatrick McHardy 
2264cb6a4e46SPatrick McHardy 	rtm = nlmsg_data(nlh);
2265cb6a4e46SPatrick McHardy 	rtm->rtm_family   = RTNL_FAMILY_IPMR;
2266cb6a4e46SPatrick McHardy 	rtm->rtm_dst_len  = 32;
2267cb6a4e46SPatrick McHardy 	rtm->rtm_src_len  = 32;
2268cb6a4e46SPatrick McHardy 	rtm->rtm_tos      = 0;
2269cb6a4e46SPatrick McHardy 	rtm->rtm_table    = mrt->id;
2270f3756b79SDavid S. Miller 	if (nla_put_u32(skb, RTA_TABLE, mrt->id))
2271f3756b79SDavid S. Miller 		goto nla_put_failure;
2272cb6a4e46SPatrick McHardy 	rtm->rtm_type     = RTN_MULTICAST;
2273cb6a4e46SPatrick McHardy 	rtm->rtm_scope    = RT_SCOPE_UNIVERSE;
22749a68ac72SNicolas Dichtel 	if (c->mfc_flags & MFC_STATIC)
22759a68ac72SNicolas Dichtel 		rtm->rtm_protocol = RTPROT_STATIC;
22769a68ac72SNicolas Dichtel 	else
22779a68ac72SNicolas Dichtel 		rtm->rtm_protocol = RTPROT_MROUTED;
2278cb6a4e46SPatrick McHardy 	rtm->rtm_flags    = 0;
2279cb6a4e46SPatrick McHardy 
2280f3756b79SDavid S. Miller 	if (nla_put_be32(skb, RTA_SRC, c->mfc_origin) ||
2281f3756b79SDavid S. Miller 	    nla_put_be32(skb, RTA_DST, c->mfc_mcastgrp))
2282f3756b79SDavid S. Miller 		goto nla_put_failure;
22831eb99af5SNicolas Dichtel 	err = __ipmr_fill_mroute(mrt, skb, c, rtm);
22841eb99af5SNicolas Dichtel 	/* do not break the dump if cache is unresolved */
22851eb99af5SNicolas Dichtel 	if (err < 0 && err != -ENOENT)
2286cb6a4e46SPatrick McHardy 		goto nla_put_failure;
2287cb6a4e46SPatrick McHardy 
2288cb6a4e46SPatrick McHardy 	return nlmsg_end(skb, nlh);
2289cb6a4e46SPatrick McHardy 
2290cb6a4e46SPatrick McHardy nla_put_failure:
2291cb6a4e46SPatrick McHardy 	nlmsg_cancel(skb, nlh);
2292cb6a4e46SPatrick McHardy 	return -EMSGSIZE;
2293cb6a4e46SPatrick McHardy }
2294cb6a4e46SPatrick McHardy 
22958cd3ac9fSNicolas Dichtel static size_t mroute_msgsize(bool unresolved, int maxvif)
22968cd3ac9fSNicolas Dichtel {
22978cd3ac9fSNicolas Dichtel 	size_t len =
22988cd3ac9fSNicolas Dichtel 		NLMSG_ALIGN(sizeof(struct rtmsg))
22998cd3ac9fSNicolas Dichtel 		+ nla_total_size(4)	/* RTA_TABLE */
23008cd3ac9fSNicolas Dichtel 		+ nla_total_size(4)	/* RTA_SRC */
23018cd3ac9fSNicolas Dichtel 		+ nla_total_size(4)	/* RTA_DST */
23028cd3ac9fSNicolas Dichtel 		;
23038cd3ac9fSNicolas Dichtel 
23048cd3ac9fSNicolas Dichtel 	if (!unresolved)
23058cd3ac9fSNicolas Dichtel 		len = len
23068cd3ac9fSNicolas Dichtel 		      + nla_total_size(4)	/* RTA_IIF */
23078cd3ac9fSNicolas Dichtel 		      + nla_total_size(0)	/* RTA_MULTIPATH */
23088cd3ac9fSNicolas Dichtel 		      + maxvif * NLA_ALIGN(sizeof(struct rtnexthop))
23098cd3ac9fSNicolas Dichtel 						/* RTA_MFC_STATS */
23108cd3ac9fSNicolas Dichtel 		      + nla_total_size(sizeof(struct rta_mfc_stats))
23118cd3ac9fSNicolas Dichtel 		;
23128cd3ac9fSNicolas Dichtel 
23138cd3ac9fSNicolas Dichtel 	return len;
23148cd3ac9fSNicolas Dichtel }
23158cd3ac9fSNicolas Dichtel 
23168cd3ac9fSNicolas Dichtel static void mroute_netlink_event(struct mr_table *mrt, struct mfc_cache *mfc,
23178cd3ac9fSNicolas Dichtel 				 int cmd)
23188cd3ac9fSNicolas Dichtel {
23198cd3ac9fSNicolas Dichtel 	struct net *net = read_pnet(&mrt->net);
23208cd3ac9fSNicolas Dichtel 	struct sk_buff *skb;
23218cd3ac9fSNicolas Dichtel 	int err = -ENOBUFS;
23228cd3ac9fSNicolas Dichtel 
23238cd3ac9fSNicolas Dichtel 	skb = nlmsg_new(mroute_msgsize(mfc->mfc_parent >= MAXVIFS, mrt->maxvif),
23248cd3ac9fSNicolas Dichtel 			GFP_ATOMIC);
23258cd3ac9fSNicolas Dichtel 	if (skb == NULL)
23268cd3ac9fSNicolas Dichtel 		goto errout;
23278cd3ac9fSNicolas Dichtel 
23288cd3ac9fSNicolas Dichtel 	err = ipmr_fill_mroute(mrt, skb, 0, 0, mfc, cmd);
23298cd3ac9fSNicolas Dichtel 	if (err < 0)
23308cd3ac9fSNicolas Dichtel 		goto errout;
23318cd3ac9fSNicolas Dichtel 
23328cd3ac9fSNicolas Dichtel 	rtnl_notify(skb, net, 0, RTNLGRP_IPV4_MROUTE, NULL, GFP_ATOMIC);
23338cd3ac9fSNicolas Dichtel 	return;
23348cd3ac9fSNicolas Dichtel 
23358cd3ac9fSNicolas Dichtel errout:
23368cd3ac9fSNicolas Dichtel 	kfree_skb(skb);
23378cd3ac9fSNicolas Dichtel 	if (err < 0)
23388cd3ac9fSNicolas Dichtel 		rtnl_set_sk_err(net, RTNLGRP_IPV4_MROUTE, err);
23398cd3ac9fSNicolas Dichtel }
23408cd3ac9fSNicolas Dichtel 
2341cb6a4e46SPatrick McHardy static int ipmr_rtm_dumproute(struct sk_buff *skb, struct netlink_callback *cb)
2342cb6a4e46SPatrick McHardy {
2343cb6a4e46SPatrick McHardy 	struct net *net = sock_net(skb->sk);
2344cb6a4e46SPatrick McHardy 	struct mr_table *mrt;
2345cb6a4e46SPatrick McHardy 	struct mfc_cache *mfc;
2346cb6a4e46SPatrick McHardy 	unsigned int t = 0, s_t;
2347cb6a4e46SPatrick McHardy 	unsigned int h = 0, s_h;
2348cb6a4e46SPatrick McHardy 	unsigned int e = 0, s_e;
2349cb6a4e46SPatrick McHardy 
2350cb6a4e46SPatrick McHardy 	s_t = cb->args[0];
2351cb6a4e46SPatrick McHardy 	s_h = cb->args[1];
2352cb6a4e46SPatrick McHardy 	s_e = cb->args[2];
2353cb6a4e46SPatrick McHardy 
2354a8c9486bSEric Dumazet 	rcu_read_lock();
2355cb6a4e46SPatrick McHardy 	ipmr_for_each_table(mrt, net) {
2356cb6a4e46SPatrick McHardy 		if (t < s_t)
2357cb6a4e46SPatrick McHardy 			goto next_table;
2358cb6a4e46SPatrick McHardy 		if (t > s_t)
2359cb6a4e46SPatrick McHardy 			s_h = 0;
2360cb6a4e46SPatrick McHardy 		for (h = s_h; h < MFC_LINES; h++) {
2361a8c9486bSEric Dumazet 			list_for_each_entry_rcu(mfc, &mrt->mfc_cache_array[h], list) {
2362cb6a4e46SPatrick McHardy 				if (e < s_e)
2363cb6a4e46SPatrick McHardy 					goto next_entry;
2364cb6a4e46SPatrick McHardy 				if (ipmr_fill_mroute(mrt, skb,
236515e47304SEric W. Biederman 						     NETLINK_CB(cb->skb).portid,
2366cb6a4e46SPatrick McHardy 						     cb->nlh->nlmsg_seq,
23678cd3ac9fSNicolas Dichtel 						     mfc, RTM_NEWROUTE) < 0)
2368cb6a4e46SPatrick McHardy 					goto done;
2369cb6a4e46SPatrick McHardy next_entry:
2370cb6a4e46SPatrick McHardy 				e++;
2371cb6a4e46SPatrick McHardy 			}
2372cb6a4e46SPatrick McHardy 			e = s_e = 0;
2373cb6a4e46SPatrick McHardy 		}
23741eb99af5SNicolas Dichtel 		spin_lock_bh(&mfc_unres_lock);
23751eb99af5SNicolas Dichtel 		list_for_each_entry(mfc, &mrt->mfc_unres_queue, list) {
23761eb99af5SNicolas Dichtel 			if (e < s_e)
23771eb99af5SNicolas Dichtel 				goto next_entry2;
23781eb99af5SNicolas Dichtel 			if (ipmr_fill_mroute(mrt, skb,
23791eb99af5SNicolas Dichtel 					     NETLINK_CB(cb->skb).portid,
23801eb99af5SNicolas Dichtel 					     cb->nlh->nlmsg_seq,
23818cd3ac9fSNicolas Dichtel 					     mfc, RTM_NEWROUTE) < 0) {
23821eb99af5SNicolas Dichtel 				spin_unlock_bh(&mfc_unres_lock);
23831eb99af5SNicolas Dichtel 				goto done;
23841eb99af5SNicolas Dichtel 			}
23851eb99af5SNicolas Dichtel next_entry2:
23861eb99af5SNicolas Dichtel 			e++;
23871eb99af5SNicolas Dichtel 		}
23881eb99af5SNicolas Dichtel 		spin_unlock_bh(&mfc_unres_lock);
23891eb99af5SNicolas Dichtel 		e = s_e = 0;
2390cb6a4e46SPatrick McHardy 		s_h = 0;
2391cb6a4e46SPatrick McHardy next_table:
2392cb6a4e46SPatrick McHardy 		t++;
2393cb6a4e46SPatrick McHardy 	}
2394cb6a4e46SPatrick McHardy done:
2395a8c9486bSEric Dumazet 	rcu_read_unlock();
2396cb6a4e46SPatrick McHardy 
2397cb6a4e46SPatrick McHardy 	cb->args[2] = e;
2398cb6a4e46SPatrick McHardy 	cb->args[1] = h;
2399cb6a4e46SPatrick McHardy 	cb->args[0] = t;
2400cb6a4e46SPatrick McHardy 
2401cb6a4e46SPatrick McHardy 	return skb->len;
2402cb6a4e46SPatrick McHardy }
2403cb6a4e46SPatrick McHardy 
24041da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
24051da177e4SLinus Torvalds /*
2406a8cb16ddSEric Dumazet  *	The /proc interfaces to multicast routing :
2407a8cb16ddSEric Dumazet  *	/proc/net/ip_mr_cache & /proc/net/ip_mr_vif
24081da177e4SLinus Torvalds  */
24091da177e4SLinus Torvalds struct ipmr_vif_iter {
2410f6bb4514SBenjamin Thery 	struct seq_net_private p;
2411f0ad0860SPatrick McHardy 	struct mr_table *mrt;
24121da177e4SLinus Torvalds 	int ct;
24131da177e4SLinus Torvalds };
24141da177e4SLinus Torvalds 
2415f6bb4514SBenjamin Thery static struct vif_device *ipmr_vif_seq_idx(struct net *net,
2416f6bb4514SBenjamin Thery 					   struct ipmr_vif_iter *iter,
24171da177e4SLinus Torvalds 					   loff_t pos)
24181da177e4SLinus Torvalds {
2419f0ad0860SPatrick McHardy 	struct mr_table *mrt = iter->mrt;
24200c12295aSPatrick McHardy 
24210c12295aSPatrick McHardy 	for (iter->ct = 0; iter->ct < mrt->maxvif; ++iter->ct) {
24220c12295aSPatrick McHardy 		if (!VIF_EXISTS(mrt, iter->ct))
24231da177e4SLinus Torvalds 			continue;
24241da177e4SLinus Torvalds 		if (pos-- == 0)
24250c12295aSPatrick McHardy 			return &mrt->vif_table[iter->ct];
24261da177e4SLinus Torvalds 	}
24271da177e4SLinus Torvalds 	return NULL;
24281da177e4SLinus Torvalds }
24291da177e4SLinus Torvalds 
24301da177e4SLinus Torvalds static void *ipmr_vif_seq_start(struct seq_file *seq, loff_t *pos)
2431ba93ef74SStephen Hemminger 	__acquires(mrt_lock)
24321da177e4SLinus Torvalds {
2433f0ad0860SPatrick McHardy 	struct ipmr_vif_iter *iter = seq->private;
2434f6bb4514SBenjamin Thery 	struct net *net = seq_file_net(seq);
2435f0ad0860SPatrick McHardy 	struct mr_table *mrt;
2436f0ad0860SPatrick McHardy 
2437f0ad0860SPatrick McHardy 	mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
2438f0ad0860SPatrick McHardy 	if (mrt == NULL)
2439f0ad0860SPatrick McHardy 		return ERR_PTR(-ENOENT);
2440f0ad0860SPatrick McHardy 
2441f0ad0860SPatrick McHardy 	iter->mrt = mrt;
2442f6bb4514SBenjamin Thery 
24431da177e4SLinus Torvalds 	read_lock(&mrt_lock);
2444f6bb4514SBenjamin Thery 	return *pos ? ipmr_vif_seq_idx(net, seq->private, *pos - 1)
24451da177e4SLinus Torvalds 		: SEQ_START_TOKEN;
24461da177e4SLinus Torvalds }
24471da177e4SLinus Torvalds 
24481da177e4SLinus Torvalds static void *ipmr_vif_seq_next(struct seq_file *seq, void *v, loff_t *pos)
24491da177e4SLinus Torvalds {
24501da177e4SLinus Torvalds 	struct ipmr_vif_iter *iter = seq->private;
2451f6bb4514SBenjamin Thery 	struct net *net = seq_file_net(seq);
2452f0ad0860SPatrick McHardy 	struct mr_table *mrt = iter->mrt;
24531da177e4SLinus Torvalds 
24541da177e4SLinus Torvalds 	++*pos;
24551da177e4SLinus Torvalds 	if (v == SEQ_START_TOKEN)
2456f6bb4514SBenjamin Thery 		return ipmr_vif_seq_idx(net, iter, 0);
24571da177e4SLinus Torvalds 
24580c12295aSPatrick McHardy 	while (++iter->ct < mrt->maxvif) {
24590c12295aSPatrick McHardy 		if (!VIF_EXISTS(mrt, iter->ct))
24601da177e4SLinus Torvalds 			continue;
24610c12295aSPatrick McHardy 		return &mrt->vif_table[iter->ct];
24621da177e4SLinus Torvalds 	}
24631da177e4SLinus Torvalds 	return NULL;
24641da177e4SLinus Torvalds }
24651da177e4SLinus Torvalds 
24661da177e4SLinus Torvalds static void ipmr_vif_seq_stop(struct seq_file *seq, void *v)
2467ba93ef74SStephen Hemminger 	__releases(mrt_lock)
24681da177e4SLinus Torvalds {
24691da177e4SLinus Torvalds 	read_unlock(&mrt_lock);
24701da177e4SLinus Torvalds }
24711da177e4SLinus Torvalds 
24721da177e4SLinus Torvalds static int ipmr_vif_seq_show(struct seq_file *seq, void *v)
24731da177e4SLinus Torvalds {
2474f0ad0860SPatrick McHardy 	struct ipmr_vif_iter *iter = seq->private;
2475f0ad0860SPatrick McHardy 	struct mr_table *mrt = iter->mrt;
2476f6bb4514SBenjamin Thery 
24771da177e4SLinus Torvalds 	if (v == SEQ_START_TOKEN) {
24781da177e4SLinus Torvalds 		seq_puts(seq,
24791da177e4SLinus Torvalds 			 "Interface      BytesIn  PktsIn  BytesOut PktsOut Flags Local    Remote\n");
24801da177e4SLinus Torvalds 	} else {
24811da177e4SLinus Torvalds 		const struct vif_device *vif = v;
24821da177e4SLinus Torvalds 		const char *name =  vif->dev ? vif->dev->name : "none";
24831da177e4SLinus Torvalds 
24841da177e4SLinus Torvalds 		seq_printf(seq,
24851da177e4SLinus Torvalds 			   "%2Zd %-10s %8ld %7ld  %8ld %7ld %05X %08X %08X\n",
24860c12295aSPatrick McHardy 			   vif - mrt->vif_table,
24871da177e4SLinus Torvalds 			   name, vif->bytes_in, vif->pkt_in,
24881da177e4SLinus Torvalds 			   vif->bytes_out, vif->pkt_out,
24891da177e4SLinus Torvalds 			   vif->flags, vif->local, vif->remote);
24901da177e4SLinus Torvalds 	}
24911da177e4SLinus Torvalds 	return 0;
24921da177e4SLinus Torvalds }
24931da177e4SLinus Torvalds 
2494f690808eSStephen Hemminger static const struct seq_operations ipmr_vif_seq_ops = {
24951da177e4SLinus Torvalds 	.start = ipmr_vif_seq_start,
24961da177e4SLinus Torvalds 	.next  = ipmr_vif_seq_next,
24971da177e4SLinus Torvalds 	.stop  = ipmr_vif_seq_stop,
24981da177e4SLinus Torvalds 	.show  = ipmr_vif_seq_show,
24991da177e4SLinus Torvalds };
25001da177e4SLinus Torvalds 
25011da177e4SLinus Torvalds static int ipmr_vif_open(struct inode *inode, struct file *file)
25021da177e4SLinus Torvalds {
2503f6bb4514SBenjamin Thery 	return seq_open_net(inode, file, &ipmr_vif_seq_ops,
2504cf7732e4SPavel Emelyanov 			    sizeof(struct ipmr_vif_iter));
25051da177e4SLinus Torvalds }
25061da177e4SLinus Torvalds 
25079a32144eSArjan van de Ven static const struct file_operations ipmr_vif_fops = {
25081da177e4SLinus Torvalds 	.owner	 = THIS_MODULE,
25091da177e4SLinus Torvalds 	.open    = ipmr_vif_open,
25101da177e4SLinus Torvalds 	.read    = seq_read,
25111da177e4SLinus Torvalds 	.llseek  = seq_lseek,
2512f6bb4514SBenjamin Thery 	.release = seq_release_net,
25131da177e4SLinus Torvalds };
25141da177e4SLinus Torvalds 
25151da177e4SLinus Torvalds struct ipmr_mfc_iter {
2516f6bb4514SBenjamin Thery 	struct seq_net_private p;
2517f0ad0860SPatrick McHardy 	struct mr_table *mrt;
2518862465f2SPatrick McHardy 	struct list_head *cache;
25191da177e4SLinus Torvalds 	int ct;
25201da177e4SLinus Torvalds };
25211da177e4SLinus Torvalds 
25221da177e4SLinus Torvalds 
2523f6bb4514SBenjamin Thery static struct mfc_cache *ipmr_mfc_seq_idx(struct net *net,
2524f6bb4514SBenjamin Thery 					  struct ipmr_mfc_iter *it, loff_t pos)
25251da177e4SLinus Torvalds {
2526f0ad0860SPatrick McHardy 	struct mr_table *mrt = it->mrt;
25271da177e4SLinus Torvalds 	struct mfc_cache *mfc;
25281da177e4SLinus Torvalds 
2529a8c9486bSEric Dumazet 	rcu_read_lock();
2530862465f2SPatrick McHardy 	for (it->ct = 0; it->ct < MFC_LINES; it->ct++) {
25310c12295aSPatrick McHardy 		it->cache = &mrt->mfc_cache_array[it->ct];
2532a8c9486bSEric Dumazet 		list_for_each_entry_rcu(mfc, it->cache, list)
25331da177e4SLinus Torvalds 			if (pos-- == 0)
25341da177e4SLinus Torvalds 				return mfc;
2535862465f2SPatrick McHardy 	}
2536a8c9486bSEric Dumazet 	rcu_read_unlock();
25371da177e4SLinus Torvalds 
25381da177e4SLinus Torvalds 	spin_lock_bh(&mfc_unres_lock);
25390c12295aSPatrick McHardy 	it->cache = &mrt->mfc_unres_queue;
2540862465f2SPatrick McHardy 	list_for_each_entry(mfc, it->cache, list)
2541e258beb2SPatrick McHardy 		if (pos-- == 0)
25421da177e4SLinus Torvalds 			return mfc;
25431da177e4SLinus Torvalds 	spin_unlock_bh(&mfc_unres_lock);
25441da177e4SLinus Torvalds 
25451da177e4SLinus Torvalds 	it->cache = NULL;
25461da177e4SLinus Torvalds 	return NULL;
25471da177e4SLinus Torvalds }
25481da177e4SLinus Torvalds 
25491da177e4SLinus Torvalds 
25501da177e4SLinus Torvalds static void *ipmr_mfc_seq_start(struct seq_file *seq, loff_t *pos)
25511da177e4SLinus Torvalds {
25521da177e4SLinus Torvalds 	struct ipmr_mfc_iter *it = seq->private;
2553f6bb4514SBenjamin Thery 	struct net *net = seq_file_net(seq);
2554f0ad0860SPatrick McHardy 	struct mr_table *mrt;
2555f6bb4514SBenjamin Thery 
2556f0ad0860SPatrick McHardy 	mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
2557f0ad0860SPatrick McHardy 	if (mrt == NULL)
2558f0ad0860SPatrick McHardy 		return ERR_PTR(-ENOENT);
2559f0ad0860SPatrick McHardy 
2560f0ad0860SPatrick McHardy 	it->mrt = mrt;
25611da177e4SLinus Torvalds 	it->cache = NULL;
25621da177e4SLinus Torvalds 	it->ct = 0;
2563f6bb4514SBenjamin Thery 	return *pos ? ipmr_mfc_seq_idx(net, seq->private, *pos - 1)
25641da177e4SLinus Torvalds 		: SEQ_START_TOKEN;
25651da177e4SLinus Torvalds }
25661da177e4SLinus Torvalds 
25671da177e4SLinus Torvalds static void *ipmr_mfc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
25681da177e4SLinus Torvalds {
25691da177e4SLinus Torvalds 	struct mfc_cache *mfc = v;
25701da177e4SLinus Torvalds 	struct ipmr_mfc_iter *it = seq->private;
2571f6bb4514SBenjamin Thery 	struct net *net = seq_file_net(seq);
2572f0ad0860SPatrick McHardy 	struct mr_table *mrt = it->mrt;
25731da177e4SLinus Torvalds 
25741da177e4SLinus Torvalds 	++*pos;
25751da177e4SLinus Torvalds 
25761da177e4SLinus Torvalds 	if (v == SEQ_START_TOKEN)
2577f6bb4514SBenjamin Thery 		return ipmr_mfc_seq_idx(net, seq->private, 0);
25781da177e4SLinus Torvalds 
2579862465f2SPatrick McHardy 	if (mfc->list.next != it->cache)
2580862465f2SPatrick McHardy 		return list_entry(mfc->list.next, struct mfc_cache, list);
25811da177e4SLinus Torvalds 
25820c12295aSPatrick McHardy 	if (it->cache == &mrt->mfc_unres_queue)
25831da177e4SLinus Torvalds 		goto end_of_list;
25841da177e4SLinus Torvalds 
25850c12295aSPatrick McHardy 	BUG_ON(it->cache != &mrt->mfc_cache_array[it->ct]);
25861da177e4SLinus Torvalds 
25871da177e4SLinus Torvalds 	while (++it->ct < MFC_LINES) {
25880c12295aSPatrick McHardy 		it->cache = &mrt->mfc_cache_array[it->ct];
2589862465f2SPatrick McHardy 		if (list_empty(it->cache))
2590862465f2SPatrick McHardy 			continue;
2591862465f2SPatrick McHardy 		return list_first_entry(it->cache, struct mfc_cache, list);
25921da177e4SLinus Torvalds 	}
25931da177e4SLinus Torvalds 
25941da177e4SLinus Torvalds 	/* exhausted cache_array, show unresolved */
2595a8c9486bSEric Dumazet 	rcu_read_unlock();
25960c12295aSPatrick McHardy 	it->cache = &mrt->mfc_unres_queue;
25971da177e4SLinus Torvalds 	it->ct = 0;
25981da177e4SLinus Torvalds 
25991da177e4SLinus Torvalds 	spin_lock_bh(&mfc_unres_lock);
2600862465f2SPatrick McHardy 	if (!list_empty(it->cache))
2601862465f2SPatrick McHardy 		return list_first_entry(it->cache, struct mfc_cache, list);
26021da177e4SLinus Torvalds 
26031da177e4SLinus Torvalds end_of_list:
26041da177e4SLinus Torvalds 	spin_unlock_bh(&mfc_unres_lock);
26051da177e4SLinus Torvalds 	it->cache = NULL;
26061da177e4SLinus Torvalds 
26071da177e4SLinus Torvalds 	return NULL;
26081da177e4SLinus Torvalds }
26091da177e4SLinus Torvalds 
26101da177e4SLinus Torvalds static void ipmr_mfc_seq_stop(struct seq_file *seq, void *v)
26111da177e4SLinus Torvalds {
26121da177e4SLinus Torvalds 	struct ipmr_mfc_iter *it = seq->private;
2613f0ad0860SPatrick McHardy 	struct mr_table *mrt = it->mrt;
26141da177e4SLinus Torvalds 
26150c12295aSPatrick McHardy 	if (it->cache == &mrt->mfc_unres_queue)
26161da177e4SLinus Torvalds 		spin_unlock_bh(&mfc_unres_lock);
26170c12295aSPatrick McHardy 	else if (it->cache == &mrt->mfc_cache_array[it->ct])
2618a8c9486bSEric Dumazet 		rcu_read_unlock();
26191da177e4SLinus Torvalds }
26201da177e4SLinus Torvalds 
26211da177e4SLinus Torvalds static int ipmr_mfc_seq_show(struct seq_file *seq, void *v)
26221da177e4SLinus Torvalds {
26231da177e4SLinus Torvalds 	int n;
26241da177e4SLinus Torvalds 
26251da177e4SLinus Torvalds 	if (v == SEQ_START_TOKEN) {
26261da177e4SLinus Torvalds 		seq_puts(seq,
26271da177e4SLinus Torvalds 		 "Group    Origin   Iif     Pkts    Bytes    Wrong Oifs\n");
26281da177e4SLinus Torvalds 	} else {
26291da177e4SLinus Torvalds 		const struct mfc_cache *mfc = v;
26301da177e4SLinus Torvalds 		const struct ipmr_mfc_iter *it = seq->private;
2631f0ad0860SPatrick McHardy 		const struct mr_table *mrt = it->mrt;
26321da177e4SLinus Torvalds 
26330eae88f3SEric Dumazet 		seq_printf(seq, "%08X %08X %-3hd",
26340eae88f3SEric Dumazet 			   (__force u32) mfc->mfc_mcastgrp,
26350eae88f3SEric Dumazet 			   (__force u32) mfc->mfc_origin,
26361ea472e2SBenjamin Thery 			   mfc->mfc_parent);
26371ea472e2SBenjamin Thery 
26380c12295aSPatrick McHardy 		if (it->cache != &mrt->mfc_unres_queue) {
26391ea472e2SBenjamin Thery 			seq_printf(seq, " %8lu %8lu %8lu",
26401da177e4SLinus Torvalds 				   mfc->mfc_un.res.pkt,
26411da177e4SLinus Torvalds 				   mfc->mfc_un.res.bytes,
26421da177e4SLinus Torvalds 				   mfc->mfc_un.res.wrong_if);
26431da177e4SLinus Torvalds 			for (n = mfc->mfc_un.res.minvif;
26441da177e4SLinus Torvalds 			     n < mfc->mfc_un.res.maxvif; n++) {
26450c12295aSPatrick McHardy 				if (VIF_EXISTS(mrt, n) &&
2646cf958ae3SBenjamin Thery 				    mfc->mfc_un.res.ttls[n] < 255)
26471da177e4SLinus Torvalds 					seq_printf(seq,
26481da177e4SLinus Torvalds 					   " %2d:%-3d",
26491da177e4SLinus Torvalds 					   n, mfc->mfc_un.res.ttls[n]);
26501da177e4SLinus Torvalds 			}
26511ea472e2SBenjamin Thery 		} else {
26521ea472e2SBenjamin Thery 			/* unresolved mfc_caches don't contain
26531ea472e2SBenjamin Thery 			 * pkt, bytes and wrong_if values
26541ea472e2SBenjamin Thery 			 */
26551ea472e2SBenjamin Thery 			seq_printf(seq, " %8lu %8lu %8lu", 0ul, 0ul, 0ul);
26561da177e4SLinus Torvalds 		}
26571da177e4SLinus Torvalds 		seq_putc(seq, '\n');
26581da177e4SLinus Torvalds 	}
26591da177e4SLinus Torvalds 	return 0;
26601da177e4SLinus Torvalds }
26611da177e4SLinus Torvalds 
2662f690808eSStephen Hemminger static const struct seq_operations ipmr_mfc_seq_ops = {
26631da177e4SLinus Torvalds 	.start = ipmr_mfc_seq_start,
26641da177e4SLinus Torvalds 	.next  = ipmr_mfc_seq_next,
26651da177e4SLinus Torvalds 	.stop  = ipmr_mfc_seq_stop,
26661da177e4SLinus Torvalds 	.show  = ipmr_mfc_seq_show,
26671da177e4SLinus Torvalds };
26681da177e4SLinus Torvalds 
26691da177e4SLinus Torvalds static int ipmr_mfc_open(struct inode *inode, struct file *file)
26701da177e4SLinus Torvalds {
2671f6bb4514SBenjamin Thery 	return seq_open_net(inode, file, &ipmr_mfc_seq_ops,
2672cf7732e4SPavel Emelyanov 			    sizeof(struct ipmr_mfc_iter));
26731da177e4SLinus Torvalds }
26741da177e4SLinus Torvalds 
26759a32144eSArjan van de Ven static const struct file_operations ipmr_mfc_fops = {
26761da177e4SLinus Torvalds 	.owner	 = THIS_MODULE,
26771da177e4SLinus Torvalds 	.open    = ipmr_mfc_open,
26781da177e4SLinus Torvalds 	.read    = seq_read,
26791da177e4SLinus Torvalds 	.llseek  = seq_lseek,
2680f6bb4514SBenjamin Thery 	.release = seq_release_net,
26811da177e4SLinus Torvalds };
26821da177e4SLinus Torvalds #endif
26831da177e4SLinus Torvalds 
26841da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM_V2
268532613090SAlexey Dobriyan static const struct net_protocol pim_protocol = {
26861da177e4SLinus Torvalds 	.handler	=	pim_rcv,
2687403dbb97STom Goff 	.netns_ok	=	1,
26881da177e4SLinus Torvalds };
26891da177e4SLinus Torvalds #endif
26901da177e4SLinus Torvalds 
26911da177e4SLinus Torvalds 
26921da177e4SLinus Torvalds /*
26931da177e4SLinus Torvalds  *	Setup for IP multicast routing
26941da177e4SLinus Torvalds  */
2695cf958ae3SBenjamin Thery static int __net_init ipmr_net_init(struct net *net)
2696cf958ae3SBenjamin Thery {
2697f0ad0860SPatrick McHardy 	int err;
2698cf958ae3SBenjamin Thery 
2699f0ad0860SPatrick McHardy 	err = ipmr_rules_init(net);
2700f0ad0860SPatrick McHardy 	if (err < 0)
2701cf958ae3SBenjamin Thery 		goto fail;
2702f6bb4514SBenjamin Thery 
2703f6bb4514SBenjamin Thery #ifdef CONFIG_PROC_FS
2704f6bb4514SBenjamin Thery 	err = -ENOMEM;
2705d4beaa66SGao feng 	if (!proc_create("ip_mr_vif", 0, net->proc_net, &ipmr_vif_fops))
2706f6bb4514SBenjamin Thery 		goto proc_vif_fail;
2707d4beaa66SGao feng 	if (!proc_create("ip_mr_cache", 0, net->proc_net, &ipmr_mfc_fops))
2708f6bb4514SBenjamin Thery 		goto proc_cache_fail;
2709f6bb4514SBenjamin Thery #endif
27102bb8b26cSBenjamin Thery 	return 0;
27112bb8b26cSBenjamin Thery 
2712f6bb4514SBenjamin Thery #ifdef CONFIG_PROC_FS
2713f6bb4514SBenjamin Thery proc_cache_fail:
2714ece31ffdSGao feng 	remove_proc_entry("ip_mr_vif", net->proc_net);
2715f6bb4514SBenjamin Thery proc_vif_fail:
2716f0ad0860SPatrick McHardy 	ipmr_rules_exit(net);
2717f6bb4514SBenjamin Thery #endif
2718cf958ae3SBenjamin Thery fail:
2719cf958ae3SBenjamin Thery 	return err;
2720cf958ae3SBenjamin Thery }
2721cf958ae3SBenjamin Thery 
2722cf958ae3SBenjamin Thery static void __net_exit ipmr_net_exit(struct net *net)
2723cf958ae3SBenjamin Thery {
2724f6bb4514SBenjamin Thery #ifdef CONFIG_PROC_FS
2725ece31ffdSGao feng 	remove_proc_entry("ip_mr_cache", net->proc_net);
2726ece31ffdSGao feng 	remove_proc_entry("ip_mr_vif", net->proc_net);
2727f6bb4514SBenjamin Thery #endif
2728f0ad0860SPatrick McHardy 	ipmr_rules_exit(net);
2729cf958ae3SBenjamin Thery }
2730cf958ae3SBenjamin Thery 
2731cf958ae3SBenjamin Thery static struct pernet_operations ipmr_net_ops = {
2732cf958ae3SBenjamin Thery 	.init = ipmr_net_init,
2733cf958ae3SBenjamin Thery 	.exit = ipmr_net_exit,
2734cf958ae3SBenjamin Thery };
27351da177e4SLinus Torvalds 
273603d2f897SWang Chen int __init ip_mr_init(void)
27371da177e4SLinus Torvalds {
273803d2f897SWang Chen 	int err;
273903d2f897SWang Chen 
27401da177e4SLinus Torvalds 	mrt_cachep = kmem_cache_create("ip_mrt_cache",
27411da177e4SLinus Torvalds 				       sizeof(struct mfc_cache),
2742e5d679f3SAlexey Dobriyan 				       0, SLAB_HWCACHE_ALIGN | SLAB_PANIC,
274320c2df83SPaul Mundt 				       NULL);
274403d2f897SWang Chen 	if (!mrt_cachep)
274503d2f897SWang Chen 		return -ENOMEM;
274603d2f897SWang Chen 
2747cf958ae3SBenjamin Thery 	err = register_pernet_subsys(&ipmr_net_ops);
2748cf958ae3SBenjamin Thery 	if (err)
2749cf958ae3SBenjamin Thery 		goto reg_pernet_fail;
2750cf958ae3SBenjamin Thery 
275103d2f897SWang Chen 	err = register_netdevice_notifier(&ip_mr_notifier);
275203d2f897SWang Chen 	if (err)
275303d2f897SWang Chen 		goto reg_notif_fail;
2754403dbb97STom Goff #ifdef CONFIG_IP_PIMSM_V2
2755403dbb97STom Goff 	if (inet_add_protocol(&pim_protocol, IPPROTO_PIM) < 0) {
2756058bd4d2SJoe Perches 		pr_err("%s: can't add PIM protocol\n", __func__);
2757403dbb97STom Goff 		err = -EAGAIN;
2758403dbb97STom Goff 		goto add_proto_fail;
2759403dbb97STom Goff 	}
2760403dbb97STom Goff #endif
2761c7ac8679SGreg Rose 	rtnl_register(RTNL_FAMILY_IPMR, RTM_GETROUTE,
2762c7ac8679SGreg Rose 		      NULL, ipmr_rtm_dumproute, NULL);
276303d2f897SWang Chen 	return 0;
2764f6bb4514SBenjamin Thery 
2765403dbb97STom Goff #ifdef CONFIG_IP_PIMSM_V2
2766403dbb97STom Goff add_proto_fail:
2767403dbb97STom Goff 	unregister_netdevice_notifier(&ip_mr_notifier);
2768403dbb97STom Goff #endif
2769c3e38896SBenjamin Thery reg_notif_fail:
2770cf958ae3SBenjamin Thery 	unregister_pernet_subsys(&ipmr_net_ops);
2771cf958ae3SBenjamin Thery reg_pernet_fail:
2772c3e38896SBenjamin Thery 	kmem_cache_destroy(mrt_cachep);
277303d2f897SWang Chen 	return err;
27741da177e4SLinus Torvalds }
2775