xref: /linux/net/ipv4/ipmr.c (revision b0fe4a31849063fcac0bdc93716ca92615e93f57)
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/system.h>
301da177e4SLinus Torvalds #include <asm/uaccess.h>
311da177e4SLinus Torvalds #include <linux/types.h>
324fc268d2SRandy Dunlap #include <linux/capability.h>
331da177e4SLinus Torvalds #include <linux/errno.h>
341da177e4SLinus Torvalds #include <linux/timer.h>
351da177e4SLinus Torvalds #include <linux/mm.h>
361da177e4SLinus Torvalds #include <linux/kernel.h>
371da177e4SLinus Torvalds #include <linux/fcntl.h>
381da177e4SLinus Torvalds #include <linux/stat.h>
391da177e4SLinus Torvalds #include <linux/socket.h>
401da177e4SLinus Torvalds #include <linux/in.h>
411da177e4SLinus Torvalds #include <linux/inet.h>
421da177e4SLinus Torvalds #include <linux/netdevice.h>
431da177e4SLinus Torvalds #include <linux/inetdevice.h>
441da177e4SLinus Torvalds #include <linux/igmp.h>
451da177e4SLinus Torvalds #include <linux/proc_fs.h>
461da177e4SLinus Torvalds #include <linux/seq_file.h>
471da177e4SLinus Torvalds #include <linux/mroute.h>
481da177e4SLinus Torvalds #include <linux/init.h>
4946f25dffSKris Katterjohn #include <linux/if_ether.h>
505a0e3ad6STejun Heo #include <linux/slab.h>
51457c4cbcSEric W. Biederman #include <net/net_namespace.h>
521da177e4SLinus Torvalds #include <net/ip.h>
531da177e4SLinus Torvalds #include <net/protocol.h>
541da177e4SLinus Torvalds #include <linux/skbuff.h>
5514c85021SArnaldo Carvalho de Melo #include <net/route.h>
561da177e4SLinus Torvalds #include <net/sock.h>
571da177e4SLinus Torvalds #include <net/icmp.h>
581da177e4SLinus Torvalds #include <net/udp.h>
591da177e4SLinus Torvalds #include <net/raw.h>
601da177e4SLinus Torvalds #include <linux/notifier.h>
611da177e4SLinus Torvalds #include <linux/if_arp.h>
621da177e4SLinus Torvalds #include <linux/netfilter_ipv4.h>
63709b46e8SEric W. Biederman #include <linux/compat.h>
641da177e4SLinus Torvalds #include <net/ipip.h>
651da177e4SLinus Torvalds #include <net/checksum.h>
66dc5fc579SArnaldo Carvalho de Melo #include <net/netlink.h>
67f0ad0860SPatrick McHardy #include <net/fib_rules.h>
681da177e4SLinus Torvalds 
691da177e4SLinus Torvalds #if defined(CONFIG_IP_PIMSM_V1) || defined(CONFIG_IP_PIMSM_V2)
701da177e4SLinus Torvalds #define CONFIG_IP_PIMSM	1
711da177e4SLinus Torvalds #endif
721da177e4SLinus Torvalds 
730c12295aSPatrick McHardy struct mr_table {
74f0ad0860SPatrick McHardy 	struct list_head	list;
758de53dfbSPatrick McHardy #ifdef CONFIG_NET_NS
768de53dfbSPatrick McHardy 	struct net		*net;
778de53dfbSPatrick McHardy #endif
78f0ad0860SPatrick McHardy 	u32			id;
794c968709SEric Dumazet 	struct sock __rcu	*mroute_sk;
800c12295aSPatrick McHardy 	struct timer_list	ipmr_expire_timer;
810c12295aSPatrick McHardy 	struct list_head	mfc_unres_queue;
820c12295aSPatrick McHardy 	struct list_head	mfc_cache_array[MFC_LINES];
830c12295aSPatrick McHardy 	struct vif_device	vif_table[MAXVIFS];
840c12295aSPatrick McHardy 	int			maxvif;
850c12295aSPatrick McHardy 	atomic_t		cache_resolve_queue_len;
860c12295aSPatrick McHardy 	int			mroute_do_assert;
870c12295aSPatrick McHardy 	int			mroute_do_pim;
880c12295aSPatrick McHardy #if defined(CONFIG_IP_PIMSM_V1) || defined(CONFIG_IP_PIMSM_V2)
890c12295aSPatrick McHardy 	int			mroute_reg_vif_num;
900c12295aSPatrick McHardy #endif
910c12295aSPatrick McHardy };
920c12295aSPatrick McHardy 
93f0ad0860SPatrick McHardy struct ipmr_rule {
94f0ad0860SPatrick McHardy 	struct fib_rule		common;
95f0ad0860SPatrick McHardy };
96f0ad0860SPatrick McHardy 
97f0ad0860SPatrick McHardy struct ipmr_result {
98f0ad0860SPatrick McHardy 	struct mr_table		*mrt;
99f0ad0860SPatrick McHardy };
100f0ad0860SPatrick McHardy 
1011da177e4SLinus Torvalds /* Big lock, protecting vif table, mrt cache and mroute socket state.
102a8cb16ddSEric Dumazet  * Note that the changes are semaphored via rtnl_lock.
1031da177e4SLinus Torvalds  */
1041da177e4SLinus Torvalds 
1051da177e4SLinus Torvalds static DEFINE_RWLOCK(mrt_lock);
1061da177e4SLinus Torvalds 
1071da177e4SLinus Torvalds /*
1081da177e4SLinus Torvalds  *	Multicast router control variables
1091da177e4SLinus Torvalds  */
1101da177e4SLinus Torvalds 
1110c12295aSPatrick McHardy #define VIF_EXISTS(_mrt, _idx) ((_mrt)->vif_table[_idx].dev != NULL)
1121da177e4SLinus Torvalds 
1131da177e4SLinus Torvalds /* Special spinlock for queue of unresolved entries */
1141da177e4SLinus Torvalds static DEFINE_SPINLOCK(mfc_unres_lock);
1151da177e4SLinus Torvalds 
1161da177e4SLinus Torvalds /* We return to original Alan's scheme. Hash table of resolved
117a8cb16ddSEric Dumazet  * entries is changed only in process context and protected
118a8cb16ddSEric Dumazet  * with weak lock mrt_lock. Queue of unresolved entries is protected
119a8cb16ddSEric Dumazet  * with strong spinlock mfc_unres_lock.
120a8cb16ddSEric Dumazet  *
121a8cb16ddSEric Dumazet  * In this case data path is free of exclusive locks at all.
1221da177e4SLinus Torvalds  */
1231da177e4SLinus Torvalds 
124e18b890bSChristoph Lameter static struct kmem_cache *mrt_cachep __read_mostly;
1251da177e4SLinus Torvalds 
126f0ad0860SPatrick McHardy static struct mr_table *ipmr_new_table(struct net *net, u32 id);
1270c12295aSPatrick McHardy static int ip_mr_forward(struct net *net, struct mr_table *mrt,
1280c12295aSPatrick McHardy 			 struct sk_buff *skb, struct mfc_cache *cache,
1290c12295aSPatrick McHardy 			 int local);
1300c12295aSPatrick McHardy static int ipmr_cache_report(struct mr_table *mrt,
1314feb88e5SBenjamin Thery 			     struct sk_buff *pkt, vifi_t vifi, int assert);
132cb6a4e46SPatrick McHardy static int __ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
133d658f8a0SPatrick McHardy 			      struct mfc_cache *c, struct rtmsg *rtm);
134f0ad0860SPatrick McHardy static void ipmr_expire_process(unsigned long arg);
1351da177e4SLinus Torvalds 
136f0ad0860SPatrick McHardy #ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
137f0ad0860SPatrick McHardy #define ipmr_for_each_table(mrt, net) \
138f0ad0860SPatrick McHardy 	list_for_each_entry_rcu(mrt, &net->ipv4.mr_tables, list)
139f0ad0860SPatrick McHardy 
140f0ad0860SPatrick McHardy static struct mr_table *ipmr_get_table(struct net *net, u32 id)
141f0ad0860SPatrick McHardy {
142f0ad0860SPatrick McHardy 	struct mr_table *mrt;
143f0ad0860SPatrick McHardy 
144f0ad0860SPatrick McHardy 	ipmr_for_each_table(mrt, net) {
145f0ad0860SPatrick McHardy 		if (mrt->id == id)
146f0ad0860SPatrick McHardy 			return mrt;
147f0ad0860SPatrick McHardy 	}
148f0ad0860SPatrick McHardy 	return NULL;
149f0ad0860SPatrick McHardy }
150f0ad0860SPatrick McHardy 
151da91981bSDavid S. Miller static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4,
152f0ad0860SPatrick McHardy 			   struct mr_table **mrt)
153f0ad0860SPatrick McHardy {
154f0ad0860SPatrick McHardy 	struct ipmr_result res;
155f0ad0860SPatrick McHardy 	struct fib_lookup_arg arg = { .result = &res, };
156f0ad0860SPatrick McHardy 	int err;
157f0ad0860SPatrick McHardy 
158da91981bSDavid S. Miller 	err = fib_rules_lookup(net->ipv4.mr_rules_ops,
159da91981bSDavid S. Miller 			       flowi4_to_flowi(flp4), 0, &arg);
160f0ad0860SPatrick McHardy 	if (err < 0)
161f0ad0860SPatrick McHardy 		return err;
162f0ad0860SPatrick McHardy 	*mrt = res.mrt;
163f0ad0860SPatrick McHardy 	return 0;
164f0ad0860SPatrick McHardy }
165f0ad0860SPatrick McHardy 
166f0ad0860SPatrick McHardy static int ipmr_rule_action(struct fib_rule *rule, struct flowi *flp,
167f0ad0860SPatrick McHardy 			    int flags, struct fib_lookup_arg *arg)
168f0ad0860SPatrick McHardy {
169f0ad0860SPatrick McHardy 	struct ipmr_result *res = arg->result;
170f0ad0860SPatrick McHardy 	struct mr_table *mrt;
171f0ad0860SPatrick McHardy 
172f0ad0860SPatrick McHardy 	switch (rule->action) {
173f0ad0860SPatrick McHardy 	case FR_ACT_TO_TBL:
174f0ad0860SPatrick McHardy 		break;
175f0ad0860SPatrick McHardy 	case FR_ACT_UNREACHABLE:
176f0ad0860SPatrick McHardy 		return -ENETUNREACH;
177f0ad0860SPatrick McHardy 	case FR_ACT_PROHIBIT:
178f0ad0860SPatrick McHardy 		return -EACCES;
179f0ad0860SPatrick McHardy 	case FR_ACT_BLACKHOLE:
180f0ad0860SPatrick McHardy 	default:
181f0ad0860SPatrick McHardy 		return -EINVAL;
182f0ad0860SPatrick McHardy 	}
183f0ad0860SPatrick McHardy 
184f0ad0860SPatrick McHardy 	mrt = ipmr_get_table(rule->fr_net, rule->table);
185f0ad0860SPatrick McHardy 	if (mrt == NULL)
186f0ad0860SPatrick McHardy 		return -EAGAIN;
187f0ad0860SPatrick McHardy 	res->mrt = mrt;
188f0ad0860SPatrick McHardy 	return 0;
189f0ad0860SPatrick McHardy }
190f0ad0860SPatrick McHardy 
191f0ad0860SPatrick McHardy static int ipmr_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
192f0ad0860SPatrick McHardy {
193f0ad0860SPatrick McHardy 	return 1;
194f0ad0860SPatrick McHardy }
195f0ad0860SPatrick McHardy 
196f0ad0860SPatrick McHardy static const struct nla_policy ipmr_rule_policy[FRA_MAX + 1] = {
197f0ad0860SPatrick McHardy 	FRA_GENERIC_POLICY,
198f0ad0860SPatrick McHardy };
199f0ad0860SPatrick McHardy 
200f0ad0860SPatrick McHardy static int ipmr_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
201f0ad0860SPatrick McHardy 			       struct fib_rule_hdr *frh, struct nlattr **tb)
202f0ad0860SPatrick McHardy {
203f0ad0860SPatrick McHardy 	return 0;
204f0ad0860SPatrick McHardy }
205f0ad0860SPatrick McHardy 
206f0ad0860SPatrick McHardy static int ipmr_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
207f0ad0860SPatrick McHardy 			     struct nlattr **tb)
208f0ad0860SPatrick McHardy {
209f0ad0860SPatrick McHardy 	return 1;
210f0ad0860SPatrick McHardy }
211f0ad0860SPatrick McHardy 
212f0ad0860SPatrick McHardy static int ipmr_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
213f0ad0860SPatrick McHardy 			  struct fib_rule_hdr *frh)
214f0ad0860SPatrick McHardy {
215f0ad0860SPatrick McHardy 	frh->dst_len = 0;
216f0ad0860SPatrick McHardy 	frh->src_len = 0;
217f0ad0860SPatrick McHardy 	frh->tos     = 0;
218f0ad0860SPatrick McHardy 	return 0;
219f0ad0860SPatrick McHardy }
220f0ad0860SPatrick McHardy 
2213d0c9c4eSPatrick McHardy static const struct fib_rules_ops __net_initdata ipmr_rules_ops_template = {
22225239ceeSPatrick McHardy 	.family		= RTNL_FAMILY_IPMR,
223f0ad0860SPatrick McHardy 	.rule_size	= sizeof(struct ipmr_rule),
224f0ad0860SPatrick McHardy 	.addr_size	= sizeof(u32),
225f0ad0860SPatrick McHardy 	.action		= ipmr_rule_action,
226f0ad0860SPatrick McHardy 	.match		= ipmr_rule_match,
227f0ad0860SPatrick McHardy 	.configure	= ipmr_rule_configure,
228f0ad0860SPatrick McHardy 	.compare	= ipmr_rule_compare,
229f0ad0860SPatrick McHardy 	.default_pref	= fib_default_rule_pref,
230f0ad0860SPatrick McHardy 	.fill		= ipmr_rule_fill,
231f0ad0860SPatrick McHardy 	.nlgroup	= RTNLGRP_IPV4_RULE,
232f0ad0860SPatrick McHardy 	.policy		= ipmr_rule_policy,
233f0ad0860SPatrick McHardy 	.owner		= THIS_MODULE,
234f0ad0860SPatrick McHardy };
235f0ad0860SPatrick McHardy 
236f0ad0860SPatrick McHardy static int __net_init ipmr_rules_init(struct net *net)
237f0ad0860SPatrick McHardy {
238f0ad0860SPatrick McHardy 	struct fib_rules_ops *ops;
239f0ad0860SPatrick McHardy 	struct mr_table *mrt;
240f0ad0860SPatrick McHardy 	int err;
241f0ad0860SPatrick McHardy 
242f0ad0860SPatrick McHardy 	ops = fib_rules_register(&ipmr_rules_ops_template, net);
243f0ad0860SPatrick McHardy 	if (IS_ERR(ops))
244f0ad0860SPatrick McHardy 		return PTR_ERR(ops);
245f0ad0860SPatrick McHardy 
246f0ad0860SPatrick McHardy 	INIT_LIST_HEAD(&net->ipv4.mr_tables);
247f0ad0860SPatrick McHardy 
248f0ad0860SPatrick McHardy 	mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
249f0ad0860SPatrick McHardy 	if (mrt == NULL) {
250f0ad0860SPatrick McHardy 		err = -ENOMEM;
251f0ad0860SPatrick McHardy 		goto err1;
252f0ad0860SPatrick McHardy 	}
253f0ad0860SPatrick McHardy 
254f0ad0860SPatrick McHardy 	err = fib_default_rule_add(ops, 0x7fff, RT_TABLE_DEFAULT, 0);
255f0ad0860SPatrick McHardy 	if (err < 0)
256f0ad0860SPatrick McHardy 		goto err2;
257f0ad0860SPatrick McHardy 
258f0ad0860SPatrick McHardy 	net->ipv4.mr_rules_ops = ops;
259f0ad0860SPatrick McHardy 	return 0;
260f0ad0860SPatrick McHardy 
261f0ad0860SPatrick McHardy err2:
262f0ad0860SPatrick McHardy 	kfree(mrt);
263f0ad0860SPatrick McHardy err1:
264f0ad0860SPatrick McHardy 	fib_rules_unregister(ops);
265f0ad0860SPatrick McHardy 	return err;
266f0ad0860SPatrick McHardy }
267f0ad0860SPatrick McHardy 
268f0ad0860SPatrick McHardy static void __net_exit ipmr_rules_exit(struct net *net)
269f0ad0860SPatrick McHardy {
270f0ad0860SPatrick McHardy 	struct mr_table *mrt, *next;
271f0ad0860SPatrick McHardy 
272035320d5SEric Dumazet 	list_for_each_entry_safe(mrt, next, &net->ipv4.mr_tables, list) {
273035320d5SEric Dumazet 		list_del(&mrt->list);
274f0ad0860SPatrick McHardy 		kfree(mrt);
275035320d5SEric Dumazet 	}
276f0ad0860SPatrick McHardy 	fib_rules_unregister(net->ipv4.mr_rules_ops);
277f0ad0860SPatrick McHardy }
278f0ad0860SPatrick McHardy #else
279f0ad0860SPatrick McHardy #define ipmr_for_each_table(mrt, net) \
280f0ad0860SPatrick McHardy 	for (mrt = net->ipv4.mrt; mrt; mrt = NULL)
281f0ad0860SPatrick McHardy 
282f0ad0860SPatrick McHardy static struct mr_table *ipmr_get_table(struct net *net, u32 id)
283f0ad0860SPatrick McHardy {
284f0ad0860SPatrick McHardy 	return net->ipv4.mrt;
285f0ad0860SPatrick McHardy }
286f0ad0860SPatrick McHardy 
287da91981bSDavid S. Miller static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4,
288f0ad0860SPatrick McHardy 			   struct mr_table **mrt)
289f0ad0860SPatrick McHardy {
290f0ad0860SPatrick McHardy 	*mrt = net->ipv4.mrt;
291f0ad0860SPatrick McHardy 	return 0;
292f0ad0860SPatrick McHardy }
293f0ad0860SPatrick McHardy 
294f0ad0860SPatrick McHardy static int __net_init ipmr_rules_init(struct net *net)
295f0ad0860SPatrick McHardy {
296f0ad0860SPatrick McHardy 	net->ipv4.mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
297f0ad0860SPatrick McHardy 	return net->ipv4.mrt ? 0 : -ENOMEM;
298f0ad0860SPatrick McHardy }
299f0ad0860SPatrick McHardy 
300f0ad0860SPatrick McHardy static void __net_exit ipmr_rules_exit(struct net *net)
301f0ad0860SPatrick McHardy {
302f0ad0860SPatrick McHardy 	kfree(net->ipv4.mrt);
303f0ad0860SPatrick McHardy }
304f0ad0860SPatrick McHardy #endif
305f0ad0860SPatrick McHardy 
306f0ad0860SPatrick McHardy static struct mr_table *ipmr_new_table(struct net *net, u32 id)
307f0ad0860SPatrick McHardy {
308f0ad0860SPatrick McHardy 	struct mr_table *mrt;
309f0ad0860SPatrick McHardy 	unsigned int i;
310f0ad0860SPatrick McHardy 
311f0ad0860SPatrick McHardy 	mrt = ipmr_get_table(net, id);
312f0ad0860SPatrick McHardy 	if (mrt != NULL)
313f0ad0860SPatrick McHardy 		return mrt;
314f0ad0860SPatrick McHardy 
315f0ad0860SPatrick McHardy 	mrt = kzalloc(sizeof(*mrt), GFP_KERNEL);
316f0ad0860SPatrick McHardy 	if (mrt == NULL)
317f0ad0860SPatrick McHardy 		return NULL;
3188de53dfbSPatrick McHardy 	write_pnet(&mrt->net, net);
319f0ad0860SPatrick McHardy 	mrt->id = id;
320f0ad0860SPatrick McHardy 
321f0ad0860SPatrick McHardy 	/* Forwarding cache */
322f0ad0860SPatrick McHardy 	for (i = 0; i < MFC_LINES; i++)
323f0ad0860SPatrick McHardy 		INIT_LIST_HEAD(&mrt->mfc_cache_array[i]);
324f0ad0860SPatrick McHardy 
325f0ad0860SPatrick McHardy 	INIT_LIST_HEAD(&mrt->mfc_unres_queue);
326f0ad0860SPatrick McHardy 
327f0ad0860SPatrick McHardy 	setup_timer(&mrt->ipmr_expire_timer, ipmr_expire_process,
328f0ad0860SPatrick McHardy 		    (unsigned long)mrt);
329f0ad0860SPatrick McHardy 
330f0ad0860SPatrick McHardy #ifdef CONFIG_IP_PIMSM
331f0ad0860SPatrick McHardy 	mrt->mroute_reg_vif_num = -1;
332f0ad0860SPatrick McHardy #endif
333f0ad0860SPatrick McHardy #ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
334f0ad0860SPatrick McHardy 	list_add_tail_rcu(&mrt->list, &net->ipv4.mr_tables);
335f0ad0860SPatrick McHardy #endif
336f0ad0860SPatrick McHardy 	return mrt;
337f0ad0860SPatrick McHardy }
3381da177e4SLinus Torvalds 
3391da177e4SLinus Torvalds /* Service routines creating virtual interfaces: DVMRP tunnels and PIMREG */
3401da177e4SLinus Torvalds 
341d607032dSWang Chen static void ipmr_del_tunnel(struct net_device *dev, struct vifctl *v)
342d607032dSWang Chen {
3434feb88e5SBenjamin Thery 	struct net *net = dev_net(dev);
3444feb88e5SBenjamin Thery 
345d607032dSWang Chen 	dev_close(dev);
346d607032dSWang Chen 
3474feb88e5SBenjamin Thery 	dev = __dev_get_by_name(net, "tunl0");
348d607032dSWang Chen 	if (dev) {
3495bc3eb7eSStephen Hemminger 		const struct net_device_ops *ops = dev->netdev_ops;
350d607032dSWang Chen 		struct ifreq ifr;
351d607032dSWang Chen 		struct ip_tunnel_parm p;
352d607032dSWang Chen 
353d607032dSWang Chen 		memset(&p, 0, sizeof(p));
354d607032dSWang Chen 		p.iph.daddr = v->vifc_rmt_addr.s_addr;
355d607032dSWang Chen 		p.iph.saddr = v->vifc_lcl_addr.s_addr;
356d607032dSWang Chen 		p.iph.version = 4;
357d607032dSWang Chen 		p.iph.ihl = 5;
358d607032dSWang Chen 		p.iph.protocol = IPPROTO_IPIP;
359d607032dSWang Chen 		sprintf(p.name, "dvmrp%d", v->vifc_vifi);
360d607032dSWang Chen 		ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
361d607032dSWang Chen 
3625bc3eb7eSStephen Hemminger 		if (ops->ndo_do_ioctl) {
3635bc3eb7eSStephen Hemminger 			mm_segment_t oldfs = get_fs();
3645bc3eb7eSStephen Hemminger 
3655bc3eb7eSStephen Hemminger 			set_fs(KERNEL_DS);
3665bc3eb7eSStephen Hemminger 			ops->ndo_do_ioctl(dev, &ifr, SIOCDELTUNNEL);
367d607032dSWang Chen 			set_fs(oldfs);
368d607032dSWang Chen 		}
369d607032dSWang Chen 	}
3705bc3eb7eSStephen Hemminger }
371d607032dSWang Chen 
3721da177e4SLinus Torvalds static
3734feb88e5SBenjamin Thery struct net_device *ipmr_new_tunnel(struct net *net, struct vifctl *v)
3741da177e4SLinus Torvalds {
3751da177e4SLinus Torvalds 	struct net_device  *dev;
3761da177e4SLinus Torvalds 
3774feb88e5SBenjamin Thery 	dev = __dev_get_by_name(net, "tunl0");
3781da177e4SLinus Torvalds 
3791da177e4SLinus Torvalds 	if (dev) {
3805bc3eb7eSStephen Hemminger 		const struct net_device_ops *ops = dev->netdev_ops;
3811da177e4SLinus Torvalds 		int err;
3821da177e4SLinus Torvalds 		struct ifreq ifr;
3831da177e4SLinus Torvalds 		struct ip_tunnel_parm p;
3841da177e4SLinus Torvalds 		struct in_device  *in_dev;
3851da177e4SLinus Torvalds 
3861da177e4SLinus Torvalds 		memset(&p, 0, sizeof(p));
3871da177e4SLinus Torvalds 		p.iph.daddr = v->vifc_rmt_addr.s_addr;
3881da177e4SLinus Torvalds 		p.iph.saddr = v->vifc_lcl_addr.s_addr;
3891da177e4SLinus Torvalds 		p.iph.version = 4;
3901da177e4SLinus Torvalds 		p.iph.ihl = 5;
3911da177e4SLinus Torvalds 		p.iph.protocol = IPPROTO_IPIP;
3921da177e4SLinus Torvalds 		sprintf(p.name, "dvmrp%d", v->vifc_vifi);
393ba93ef74SStephen Hemminger 		ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
3941da177e4SLinus Torvalds 
3955bc3eb7eSStephen Hemminger 		if (ops->ndo_do_ioctl) {
3965bc3eb7eSStephen Hemminger 			mm_segment_t oldfs = get_fs();
3975bc3eb7eSStephen Hemminger 
3985bc3eb7eSStephen Hemminger 			set_fs(KERNEL_DS);
3995bc3eb7eSStephen Hemminger 			err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
4001da177e4SLinus Torvalds 			set_fs(oldfs);
401a8cb16ddSEric Dumazet 		} else {
4025bc3eb7eSStephen Hemminger 			err = -EOPNOTSUPP;
403a8cb16ddSEric Dumazet 		}
4041da177e4SLinus Torvalds 		dev = NULL;
4051da177e4SLinus Torvalds 
4064feb88e5SBenjamin Thery 		if (err == 0 &&
4074feb88e5SBenjamin Thery 		    (dev = __dev_get_by_name(net, p.name)) != NULL) {
4081da177e4SLinus Torvalds 			dev->flags |= IFF_MULTICAST;
4091da177e4SLinus Torvalds 
410e5ed6399SHerbert Xu 			in_dev = __in_dev_get_rtnl(dev);
41171e27da9SHerbert Xu 			if (in_dev == NULL)
4121da177e4SLinus Torvalds 				goto failure;
41371e27da9SHerbert Xu 
41471e27da9SHerbert Xu 			ipv4_devconf_setall(in_dev);
41571e27da9SHerbert Xu 			IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0;
4161da177e4SLinus Torvalds 
4171da177e4SLinus Torvalds 			if (dev_open(dev))
4181da177e4SLinus Torvalds 				goto failure;
4197dc00c82SWang Chen 			dev_hold(dev);
4201da177e4SLinus Torvalds 		}
4211da177e4SLinus Torvalds 	}
4221da177e4SLinus Torvalds 	return dev;
4231da177e4SLinus Torvalds 
4241da177e4SLinus Torvalds failure:
4251da177e4SLinus Torvalds 	/* allow the register to be completed before unregistering. */
4261da177e4SLinus Torvalds 	rtnl_unlock();
4271da177e4SLinus Torvalds 	rtnl_lock();
4281da177e4SLinus Torvalds 
4291da177e4SLinus Torvalds 	unregister_netdevice(dev);
4301da177e4SLinus Torvalds 	return NULL;
4311da177e4SLinus Torvalds }
4321da177e4SLinus Torvalds 
4331da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM
4341da177e4SLinus Torvalds 
4356fef4c0cSStephen Hemminger static netdev_tx_t reg_vif_xmit(struct sk_buff *skb, struct net_device *dev)
4361da177e4SLinus Torvalds {
4374feb88e5SBenjamin Thery 	struct net *net = dev_net(dev);
438f0ad0860SPatrick McHardy 	struct mr_table *mrt;
439da91981bSDavid S. Miller 	struct flowi4 fl4 = {
440da91981bSDavid S. Miller 		.flowi4_oif	= dev->ifindex,
441da91981bSDavid S. Miller 		.flowi4_iif	= skb->skb_iif,
442da91981bSDavid S. Miller 		.flowi4_mark	= skb->mark,
443f0ad0860SPatrick McHardy 	};
444f0ad0860SPatrick McHardy 	int err;
445f0ad0860SPatrick McHardy 
446da91981bSDavid S. Miller 	err = ipmr_fib_lookup(net, &fl4, &mrt);
447e40dbc51SBen Greear 	if (err < 0) {
448e40dbc51SBen Greear 		kfree_skb(skb);
449f0ad0860SPatrick McHardy 		return err;
450e40dbc51SBen Greear 	}
4514feb88e5SBenjamin Thery 
4521da177e4SLinus Torvalds 	read_lock(&mrt_lock);
453cf3677aeSPavel Emelyanov 	dev->stats.tx_bytes += skb->len;
454cf3677aeSPavel Emelyanov 	dev->stats.tx_packets++;
4550c12295aSPatrick McHardy 	ipmr_cache_report(mrt, skb, mrt->mroute_reg_vif_num, IGMPMSG_WHOLEPKT);
4561da177e4SLinus Torvalds 	read_unlock(&mrt_lock);
4571da177e4SLinus Torvalds 	kfree_skb(skb);
4586ed10654SPatrick McHardy 	return NETDEV_TX_OK;
4591da177e4SLinus Torvalds }
4601da177e4SLinus Torvalds 
461007c3838SStephen Hemminger static const struct net_device_ops reg_vif_netdev_ops = {
462007c3838SStephen Hemminger 	.ndo_start_xmit	= reg_vif_xmit,
463007c3838SStephen Hemminger };
464007c3838SStephen Hemminger 
4651da177e4SLinus Torvalds static void reg_vif_setup(struct net_device *dev)
4661da177e4SLinus Torvalds {
4671da177e4SLinus Torvalds 	dev->type		= ARPHRD_PIMREG;
46846f25dffSKris Katterjohn 	dev->mtu		= ETH_DATA_LEN - sizeof(struct iphdr) - 8;
4691da177e4SLinus Torvalds 	dev->flags		= IFF_NOARP;
470007c3838SStephen Hemminger 	dev->netdev_ops		= &reg_vif_netdev_ops,
4711da177e4SLinus Torvalds 	dev->destructor		= free_netdev;
472403dbb97STom Goff 	dev->features		|= NETIF_F_NETNS_LOCAL;
4731da177e4SLinus Torvalds }
4741da177e4SLinus Torvalds 
475f0ad0860SPatrick McHardy static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt)
4761da177e4SLinus Torvalds {
4771da177e4SLinus Torvalds 	struct net_device *dev;
4781da177e4SLinus Torvalds 	struct in_device *in_dev;
479f0ad0860SPatrick McHardy 	char name[IFNAMSIZ];
4801da177e4SLinus Torvalds 
481f0ad0860SPatrick McHardy 	if (mrt->id == RT_TABLE_DEFAULT)
482f0ad0860SPatrick McHardy 		sprintf(name, "pimreg");
483f0ad0860SPatrick McHardy 	else
484f0ad0860SPatrick McHardy 		sprintf(name, "pimreg%u", mrt->id);
485f0ad0860SPatrick McHardy 
486f0ad0860SPatrick McHardy 	dev = alloc_netdev(0, name, reg_vif_setup);
4871da177e4SLinus Torvalds 
4881da177e4SLinus Torvalds 	if (dev == NULL)
4891da177e4SLinus Torvalds 		return NULL;
4901da177e4SLinus Torvalds 
491403dbb97STom Goff 	dev_net_set(dev, net);
492403dbb97STom Goff 
4931da177e4SLinus Torvalds 	if (register_netdevice(dev)) {
4941da177e4SLinus Torvalds 		free_netdev(dev);
4951da177e4SLinus Torvalds 		return NULL;
4961da177e4SLinus Torvalds 	}
4971da177e4SLinus Torvalds 	dev->iflink = 0;
4981da177e4SLinus Torvalds 
49971e27da9SHerbert Xu 	rcu_read_lock();
500a8cb16ddSEric Dumazet 	in_dev = __in_dev_get_rcu(dev);
501a8cb16ddSEric Dumazet 	if (!in_dev) {
50271e27da9SHerbert Xu 		rcu_read_unlock();
5031da177e4SLinus Torvalds 		goto failure;
50471e27da9SHerbert Xu 	}
5051da177e4SLinus Torvalds 
50671e27da9SHerbert Xu 	ipv4_devconf_setall(in_dev);
50771e27da9SHerbert Xu 	IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0;
50871e27da9SHerbert Xu 	rcu_read_unlock();
5091da177e4SLinus Torvalds 
5101da177e4SLinus Torvalds 	if (dev_open(dev))
5111da177e4SLinus Torvalds 		goto failure;
5121da177e4SLinus Torvalds 
5137dc00c82SWang Chen 	dev_hold(dev);
5147dc00c82SWang Chen 
5151da177e4SLinus Torvalds 	return dev;
5161da177e4SLinus Torvalds 
5171da177e4SLinus Torvalds failure:
5181da177e4SLinus Torvalds 	/* allow the register to be completed before unregistering. */
5191da177e4SLinus Torvalds 	rtnl_unlock();
5201da177e4SLinus Torvalds 	rtnl_lock();
5211da177e4SLinus Torvalds 
5221da177e4SLinus Torvalds 	unregister_netdevice(dev);
5231da177e4SLinus Torvalds 	return NULL;
5241da177e4SLinus Torvalds }
5251da177e4SLinus Torvalds #endif
5261da177e4SLinus Torvalds 
5271da177e4SLinus Torvalds /*
5281da177e4SLinus Torvalds  *	Delete a VIF entry
5297dc00c82SWang Chen  *	@notify: Set to 1, if the caller is a notifier_call
5301da177e4SLinus Torvalds  */
5311da177e4SLinus Torvalds 
5320c12295aSPatrick McHardy static int vif_delete(struct mr_table *mrt, int vifi, int notify,
533d17fa6faSEric Dumazet 		      struct list_head *head)
5341da177e4SLinus Torvalds {
5351da177e4SLinus Torvalds 	struct vif_device *v;
5361da177e4SLinus Torvalds 	struct net_device *dev;
5371da177e4SLinus Torvalds 	struct in_device *in_dev;
5381da177e4SLinus Torvalds 
5390c12295aSPatrick McHardy 	if (vifi < 0 || vifi >= mrt->maxvif)
5401da177e4SLinus Torvalds 		return -EADDRNOTAVAIL;
5411da177e4SLinus Torvalds 
5420c12295aSPatrick McHardy 	v = &mrt->vif_table[vifi];
5431da177e4SLinus Torvalds 
5441da177e4SLinus Torvalds 	write_lock_bh(&mrt_lock);
5451da177e4SLinus Torvalds 	dev = v->dev;
5461da177e4SLinus Torvalds 	v->dev = NULL;
5471da177e4SLinus Torvalds 
5481da177e4SLinus Torvalds 	if (!dev) {
5491da177e4SLinus Torvalds 		write_unlock_bh(&mrt_lock);
5501da177e4SLinus Torvalds 		return -EADDRNOTAVAIL;
5511da177e4SLinus Torvalds 	}
5521da177e4SLinus Torvalds 
5531da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM
5540c12295aSPatrick McHardy 	if (vifi == mrt->mroute_reg_vif_num)
5550c12295aSPatrick McHardy 		mrt->mroute_reg_vif_num = -1;
5561da177e4SLinus Torvalds #endif
5571da177e4SLinus Torvalds 
5580c12295aSPatrick McHardy 	if (vifi + 1 == mrt->maxvif) {
5591da177e4SLinus Torvalds 		int tmp;
560a8cb16ddSEric Dumazet 
5611da177e4SLinus Torvalds 		for (tmp = vifi - 1; tmp >= 0; tmp--) {
5620c12295aSPatrick McHardy 			if (VIF_EXISTS(mrt, tmp))
5631da177e4SLinus Torvalds 				break;
5641da177e4SLinus Torvalds 		}
5650c12295aSPatrick McHardy 		mrt->maxvif = tmp+1;
5661da177e4SLinus Torvalds 	}
5671da177e4SLinus Torvalds 
5681da177e4SLinus Torvalds 	write_unlock_bh(&mrt_lock);
5691da177e4SLinus Torvalds 
5701da177e4SLinus Torvalds 	dev_set_allmulti(dev, -1);
5711da177e4SLinus Torvalds 
572a8cb16ddSEric Dumazet 	in_dev = __in_dev_get_rtnl(dev);
573a8cb16ddSEric Dumazet 	if (in_dev) {
57442f811b8SHerbert Xu 		IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)--;
5751da177e4SLinus Torvalds 		ip_rt_multicast_event(in_dev);
5761da177e4SLinus Torvalds 	}
5771da177e4SLinus Torvalds 
5787dc00c82SWang Chen 	if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER) && !notify)
579d17fa6faSEric Dumazet 		unregister_netdevice_queue(dev, head);
5801da177e4SLinus Torvalds 
5811da177e4SLinus Torvalds 	dev_put(dev);
5821da177e4SLinus Torvalds 	return 0;
5831da177e4SLinus Torvalds }
5841da177e4SLinus Torvalds 
585a8c9486bSEric Dumazet static void ipmr_cache_free_rcu(struct rcu_head *head)
586a8c9486bSEric Dumazet {
587a8c9486bSEric Dumazet 	struct mfc_cache *c = container_of(head, struct mfc_cache, rcu);
588a8c9486bSEric Dumazet 
589a8c9486bSEric Dumazet 	kmem_cache_free(mrt_cachep, c);
590a8c9486bSEric Dumazet }
591a8c9486bSEric Dumazet 
5925c0a66f5SBenjamin Thery static inline void ipmr_cache_free(struct mfc_cache *c)
5935c0a66f5SBenjamin Thery {
594a8c9486bSEric Dumazet 	call_rcu(&c->rcu, ipmr_cache_free_rcu);
5955c0a66f5SBenjamin Thery }
5965c0a66f5SBenjamin Thery 
5971da177e4SLinus Torvalds /* Destroy an unresolved cache entry, killing queued skbs
598a8cb16ddSEric Dumazet  * and reporting error to netlink readers.
5991da177e4SLinus Torvalds  */
6001da177e4SLinus Torvalds 
6010c12295aSPatrick McHardy static void ipmr_destroy_unres(struct mr_table *mrt, struct mfc_cache *c)
6021da177e4SLinus Torvalds {
6038de53dfbSPatrick McHardy 	struct net *net = read_pnet(&mrt->net);
6041da177e4SLinus Torvalds 	struct sk_buff *skb;
6059ef1d4c7SPatrick McHardy 	struct nlmsgerr *e;
6061da177e4SLinus Torvalds 
6070c12295aSPatrick McHardy 	atomic_dec(&mrt->cache_resolve_queue_len);
6081da177e4SLinus Torvalds 
6091da177e4SLinus Torvalds 	while ((skb = skb_dequeue(&c->mfc_un.unres.unresolved))) {
610eddc9ec5SArnaldo Carvalho de Melo 		if (ip_hdr(skb)->version == 0) {
6111da177e4SLinus Torvalds 			struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr));
6121da177e4SLinus Torvalds 			nlh->nlmsg_type = NLMSG_ERROR;
6131da177e4SLinus Torvalds 			nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct nlmsgerr));
6141da177e4SLinus Torvalds 			skb_trim(skb, nlh->nlmsg_len);
6159ef1d4c7SPatrick McHardy 			e = NLMSG_DATA(nlh);
6169ef1d4c7SPatrick McHardy 			e->error = -ETIMEDOUT;
6179ef1d4c7SPatrick McHardy 			memset(&e->msg, 0, sizeof(e->msg));
6182942e900SThomas Graf 
6194feb88e5SBenjamin Thery 			rtnl_unicast(skb, net, NETLINK_CB(skb).pid);
620a8cb16ddSEric Dumazet 		} else {
6211da177e4SLinus Torvalds 			kfree_skb(skb);
6221da177e4SLinus Torvalds 		}
623a8cb16ddSEric Dumazet 	}
6241da177e4SLinus Torvalds 
6255c0a66f5SBenjamin Thery 	ipmr_cache_free(c);
6261da177e4SLinus Torvalds }
6271da177e4SLinus Torvalds 
6281da177e4SLinus Torvalds 
629e258beb2SPatrick McHardy /* Timer process for the unresolved queue. */
6301da177e4SLinus Torvalds 
631e258beb2SPatrick McHardy static void ipmr_expire_process(unsigned long arg)
6321da177e4SLinus Torvalds {
6330c12295aSPatrick McHardy 	struct mr_table *mrt = (struct mr_table *)arg;
6341da177e4SLinus Torvalds 	unsigned long now;
6351da177e4SLinus Torvalds 	unsigned long expires;
636862465f2SPatrick McHardy 	struct mfc_cache *c, *next;
6371da177e4SLinus Torvalds 
6381da177e4SLinus Torvalds 	if (!spin_trylock(&mfc_unres_lock)) {
6390c12295aSPatrick McHardy 		mod_timer(&mrt->ipmr_expire_timer, jiffies+HZ/10);
6401da177e4SLinus Torvalds 		return;
6411da177e4SLinus Torvalds 	}
6421da177e4SLinus Torvalds 
6430c12295aSPatrick McHardy 	if (list_empty(&mrt->mfc_unres_queue))
6441da177e4SLinus Torvalds 		goto out;
6451da177e4SLinus Torvalds 
6461da177e4SLinus Torvalds 	now = jiffies;
6471da177e4SLinus Torvalds 	expires = 10*HZ;
6481da177e4SLinus Torvalds 
6490c12295aSPatrick McHardy 	list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
6501da177e4SLinus Torvalds 		if (time_after(c->mfc_un.unres.expires, now)) {
6511da177e4SLinus Torvalds 			unsigned long interval = c->mfc_un.unres.expires - now;
6521da177e4SLinus Torvalds 			if (interval < expires)
6531da177e4SLinus Torvalds 				expires = interval;
6541da177e4SLinus Torvalds 			continue;
6551da177e4SLinus Torvalds 		}
6561da177e4SLinus Torvalds 
657862465f2SPatrick McHardy 		list_del(&c->list);
6580c12295aSPatrick McHardy 		ipmr_destroy_unres(mrt, c);
6591da177e4SLinus Torvalds 	}
6601da177e4SLinus Torvalds 
6610c12295aSPatrick McHardy 	if (!list_empty(&mrt->mfc_unres_queue))
6620c12295aSPatrick McHardy 		mod_timer(&mrt->ipmr_expire_timer, jiffies + expires);
6631da177e4SLinus Torvalds 
6641da177e4SLinus Torvalds out:
6651da177e4SLinus Torvalds 	spin_unlock(&mfc_unres_lock);
6661da177e4SLinus Torvalds }
6671da177e4SLinus Torvalds 
6681da177e4SLinus Torvalds /* Fill oifs list. It is called under write locked mrt_lock. */
6691da177e4SLinus Torvalds 
6700c12295aSPatrick McHardy static void ipmr_update_thresholds(struct mr_table *mrt, struct mfc_cache *cache,
671d658f8a0SPatrick McHardy 				   unsigned char *ttls)
6721da177e4SLinus Torvalds {
6731da177e4SLinus Torvalds 	int vifi;
6741da177e4SLinus Torvalds 
6751da177e4SLinus Torvalds 	cache->mfc_un.res.minvif = MAXVIFS;
6761da177e4SLinus Torvalds 	cache->mfc_un.res.maxvif = 0;
6771da177e4SLinus Torvalds 	memset(cache->mfc_un.res.ttls, 255, MAXVIFS);
6781da177e4SLinus Torvalds 
6790c12295aSPatrick McHardy 	for (vifi = 0; vifi < mrt->maxvif; vifi++) {
6800c12295aSPatrick McHardy 		if (VIF_EXISTS(mrt, vifi) &&
681cf958ae3SBenjamin Thery 		    ttls[vifi] && ttls[vifi] < 255) {
6821da177e4SLinus Torvalds 			cache->mfc_un.res.ttls[vifi] = ttls[vifi];
6831da177e4SLinus Torvalds 			if (cache->mfc_un.res.minvif > vifi)
6841da177e4SLinus Torvalds 				cache->mfc_un.res.minvif = vifi;
6851da177e4SLinus Torvalds 			if (cache->mfc_un.res.maxvif <= vifi)
6861da177e4SLinus Torvalds 				cache->mfc_un.res.maxvif = vifi + 1;
6871da177e4SLinus Torvalds 		}
6881da177e4SLinus Torvalds 	}
6891da177e4SLinus Torvalds }
6901da177e4SLinus Torvalds 
6910c12295aSPatrick McHardy static int vif_add(struct net *net, struct mr_table *mrt,
6920c12295aSPatrick McHardy 		   struct vifctl *vifc, int mrtsock)
6931da177e4SLinus Torvalds {
6941da177e4SLinus Torvalds 	int vifi = vifc->vifc_vifi;
6950c12295aSPatrick McHardy 	struct vif_device *v = &mrt->vif_table[vifi];
6961da177e4SLinus Torvalds 	struct net_device *dev;
6971da177e4SLinus Torvalds 	struct in_device *in_dev;
698d607032dSWang Chen 	int err;
6991da177e4SLinus Torvalds 
7001da177e4SLinus Torvalds 	/* Is vif busy ? */
7010c12295aSPatrick McHardy 	if (VIF_EXISTS(mrt, vifi))
7021da177e4SLinus Torvalds 		return -EADDRINUSE;
7031da177e4SLinus Torvalds 
7041da177e4SLinus Torvalds 	switch (vifc->vifc_flags) {
7051da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM
7061da177e4SLinus Torvalds 	case VIFF_REGISTER:
7071da177e4SLinus Torvalds 		/*
7081da177e4SLinus Torvalds 		 * Special Purpose VIF in PIM
7091da177e4SLinus Torvalds 		 * All the packets will be sent to the daemon
7101da177e4SLinus Torvalds 		 */
7110c12295aSPatrick McHardy 		if (mrt->mroute_reg_vif_num >= 0)
7121da177e4SLinus Torvalds 			return -EADDRINUSE;
713f0ad0860SPatrick McHardy 		dev = ipmr_reg_vif(net, mrt);
7141da177e4SLinus Torvalds 		if (!dev)
7151da177e4SLinus Torvalds 			return -ENOBUFS;
716d607032dSWang Chen 		err = dev_set_allmulti(dev, 1);
717d607032dSWang Chen 		if (err) {
718d607032dSWang Chen 			unregister_netdevice(dev);
7197dc00c82SWang Chen 			dev_put(dev);
720d607032dSWang Chen 			return err;
721d607032dSWang Chen 		}
7221da177e4SLinus Torvalds 		break;
7231da177e4SLinus Torvalds #endif
7241da177e4SLinus Torvalds 	case VIFF_TUNNEL:
7254feb88e5SBenjamin Thery 		dev = ipmr_new_tunnel(net, vifc);
7261da177e4SLinus Torvalds 		if (!dev)
7271da177e4SLinus Torvalds 			return -ENOBUFS;
728d607032dSWang Chen 		err = dev_set_allmulti(dev, 1);
729d607032dSWang Chen 		if (err) {
730d607032dSWang Chen 			ipmr_del_tunnel(dev, vifc);
7317dc00c82SWang Chen 			dev_put(dev);
732d607032dSWang Chen 			return err;
733d607032dSWang Chen 		}
7341da177e4SLinus Torvalds 		break;
735ee5e81f0SIlia K 
736ee5e81f0SIlia K 	case VIFF_USE_IFINDEX:
7371da177e4SLinus Torvalds 	case 0:
738ee5e81f0SIlia K 		if (vifc->vifc_flags == VIFF_USE_IFINDEX) {
739ee5e81f0SIlia K 			dev = dev_get_by_index(net, vifc->vifc_lcl_ifindex);
74095ae6b22SEric Dumazet 			if (dev && __in_dev_get_rtnl(dev) == NULL) {
741ee5e81f0SIlia K 				dev_put(dev);
742ee5e81f0SIlia K 				return -EADDRNOTAVAIL;
743ee5e81f0SIlia K 			}
744a8cb16ddSEric Dumazet 		} else {
7454feb88e5SBenjamin Thery 			dev = ip_dev_find(net, vifc->vifc_lcl_addr.s_addr);
746a8cb16ddSEric Dumazet 		}
7471da177e4SLinus Torvalds 		if (!dev)
7481da177e4SLinus Torvalds 			return -EADDRNOTAVAIL;
749d607032dSWang Chen 		err = dev_set_allmulti(dev, 1);
7507dc00c82SWang Chen 		if (err) {
7517dc00c82SWang Chen 			dev_put(dev);
752d607032dSWang Chen 			return err;
7537dc00c82SWang Chen 		}
7541da177e4SLinus Torvalds 		break;
7551da177e4SLinus Torvalds 	default:
7561da177e4SLinus Torvalds 		return -EINVAL;
7571da177e4SLinus Torvalds 	}
7581da177e4SLinus Torvalds 
759a8cb16ddSEric Dumazet 	in_dev = __in_dev_get_rtnl(dev);
760a8cb16ddSEric Dumazet 	if (!in_dev) {
761d0490cfdSDan Carpenter 		dev_put(dev);
7621da177e4SLinus Torvalds 		return -EADDRNOTAVAIL;
763d0490cfdSDan Carpenter 	}
76442f811b8SHerbert Xu 	IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)++;
7651da177e4SLinus Torvalds 	ip_rt_multicast_event(in_dev);
7661da177e4SLinus Torvalds 
767a8cb16ddSEric Dumazet 	/* Fill in the VIF structures */
768a8cb16ddSEric Dumazet 
7691da177e4SLinus Torvalds 	v->rate_limit = vifc->vifc_rate_limit;
7701da177e4SLinus Torvalds 	v->local = vifc->vifc_lcl_addr.s_addr;
7711da177e4SLinus Torvalds 	v->remote = vifc->vifc_rmt_addr.s_addr;
7721da177e4SLinus Torvalds 	v->flags = vifc->vifc_flags;
7731da177e4SLinus Torvalds 	if (!mrtsock)
7741da177e4SLinus Torvalds 		v->flags |= VIFF_STATIC;
7751da177e4SLinus Torvalds 	v->threshold = vifc->vifc_threshold;
7761da177e4SLinus Torvalds 	v->bytes_in = 0;
7771da177e4SLinus Torvalds 	v->bytes_out = 0;
7781da177e4SLinus Torvalds 	v->pkt_in = 0;
7791da177e4SLinus Torvalds 	v->pkt_out = 0;
7801da177e4SLinus Torvalds 	v->link = dev->ifindex;
7811da177e4SLinus Torvalds 	if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER))
7821da177e4SLinus Torvalds 		v->link = dev->iflink;
7831da177e4SLinus Torvalds 
7841da177e4SLinus Torvalds 	/* And finish update writing critical data */
7851da177e4SLinus Torvalds 	write_lock_bh(&mrt_lock);
7861da177e4SLinus Torvalds 	v->dev = dev;
7871da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM
7881da177e4SLinus Torvalds 	if (v->flags & VIFF_REGISTER)
7890c12295aSPatrick McHardy 		mrt->mroute_reg_vif_num = vifi;
7901da177e4SLinus Torvalds #endif
7910c12295aSPatrick McHardy 	if (vifi+1 > mrt->maxvif)
7920c12295aSPatrick McHardy 		mrt->maxvif = vifi+1;
7931da177e4SLinus Torvalds 	write_unlock_bh(&mrt_lock);
7941da177e4SLinus Torvalds 	return 0;
7951da177e4SLinus Torvalds }
7961da177e4SLinus Torvalds 
797a8c9486bSEric Dumazet /* called with rcu_read_lock() */
7980c12295aSPatrick McHardy static struct mfc_cache *ipmr_cache_find(struct mr_table *mrt,
7994feb88e5SBenjamin Thery 					 __be32 origin,
8004feb88e5SBenjamin Thery 					 __be32 mcastgrp)
8011da177e4SLinus Torvalds {
8021da177e4SLinus Torvalds 	int line = MFC_HASH(mcastgrp, origin);
8031da177e4SLinus Torvalds 	struct mfc_cache *c;
8041da177e4SLinus Torvalds 
805a8c9486bSEric Dumazet 	list_for_each_entry_rcu(c, &mrt->mfc_cache_array[line], list) {
8061da177e4SLinus Torvalds 		if (c->mfc_origin == origin && c->mfc_mcastgrp == mcastgrp)
8071da177e4SLinus Torvalds 			return c;
8081da177e4SLinus Torvalds 	}
809862465f2SPatrick McHardy 	return NULL;
810862465f2SPatrick McHardy }
8111da177e4SLinus Torvalds 
8121da177e4SLinus Torvalds /*
8131da177e4SLinus Torvalds  *	Allocate a multicast cache entry
8141da177e4SLinus Torvalds  */
815d658f8a0SPatrick McHardy static struct mfc_cache *ipmr_cache_alloc(void)
8161da177e4SLinus Torvalds {
817c3762229SRobert P. J. Day 	struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_KERNEL);
818a8c9486bSEric Dumazet 
819a8c9486bSEric Dumazet 	if (c)
8201da177e4SLinus Torvalds 		c->mfc_un.res.minvif = MAXVIFS;
8211da177e4SLinus Torvalds 	return c;
8221da177e4SLinus Torvalds }
8231da177e4SLinus Torvalds 
824d658f8a0SPatrick McHardy static struct mfc_cache *ipmr_cache_alloc_unres(void)
8251da177e4SLinus Torvalds {
826c3762229SRobert P. J. Day 	struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_ATOMIC);
827a8c9486bSEric Dumazet 
828a8c9486bSEric Dumazet 	if (c) {
8291da177e4SLinus Torvalds 		skb_queue_head_init(&c->mfc_un.unres.unresolved);
8301da177e4SLinus Torvalds 		c->mfc_un.unres.expires = jiffies + 10*HZ;
831a8c9486bSEric Dumazet 	}
8321da177e4SLinus Torvalds 	return c;
8331da177e4SLinus Torvalds }
8341da177e4SLinus Torvalds 
8351da177e4SLinus Torvalds /*
8361da177e4SLinus Torvalds  *	A cache entry has gone into a resolved state from queued
8371da177e4SLinus Torvalds  */
8381da177e4SLinus Torvalds 
8390c12295aSPatrick McHardy static void ipmr_cache_resolve(struct net *net, struct mr_table *mrt,
8400c12295aSPatrick McHardy 			       struct mfc_cache *uc, struct mfc_cache *c)
8411da177e4SLinus Torvalds {
8421da177e4SLinus Torvalds 	struct sk_buff *skb;
8439ef1d4c7SPatrick McHardy 	struct nlmsgerr *e;
8441da177e4SLinus Torvalds 
845a8cb16ddSEric Dumazet 	/* Play the pending entries through our router */
8461da177e4SLinus Torvalds 
8471da177e4SLinus Torvalds 	while ((skb = __skb_dequeue(&uc->mfc_un.unres.unresolved))) {
848eddc9ec5SArnaldo Carvalho de Melo 		if (ip_hdr(skb)->version == 0) {
8491da177e4SLinus Torvalds 			struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr));
8501da177e4SLinus Torvalds 
851cb6a4e46SPatrick McHardy 			if (__ipmr_fill_mroute(mrt, skb, c, NLMSG_DATA(nlh)) > 0) {
852a8cb16ddSEric Dumazet 				nlh->nlmsg_len = skb_tail_pointer(skb) -
853a8cb16ddSEric Dumazet 						 (u8 *)nlh;
8541da177e4SLinus Torvalds 			} else {
8551da177e4SLinus Torvalds 				nlh->nlmsg_type = NLMSG_ERROR;
8561da177e4SLinus Torvalds 				nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct nlmsgerr));
8571da177e4SLinus Torvalds 				skb_trim(skb, nlh->nlmsg_len);
8589ef1d4c7SPatrick McHardy 				e = NLMSG_DATA(nlh);
8599ef1d4c7SPatrick McHardy 				e->error = -EMSGSIZE;
8609ef1d4c7SPatrick McHardy 				memset(&e->msg, 0, sizeof(e->msg));
8611da177e4SLinus Torvalds 			}
8622942e900SThomas Graf 
863d658f8a0SPatrick McHardy 			rtnl_unicast(skb, net, NETLINK_CB(skb).pid);
864a8cb16ddSEric Dumazet 		} else {
8650c12295aSPatrick McHardy 			ip_mr_forward(net, mrt, skb, c, 0);
8661da177e4SLinus Torvalds 		}
8671da177e4SLinus Torvalds 	}
868a8cb16ddSEric Dumazet }
8691da177e4SLinus Torvalds 
8701da177e4SLinus Torvalds /*
8711da177e4SLinus Torvalds  *	Bounce a cache query up to mrouted. We could use netlink for this but mrouted
8721da177e4SLinus Torvalds  *	expects the following bizarre scheme.
8731da177e4SLinus Torvalds  *
8741da177e4SLinus Torvalds  *	Called under mrt_lock.
8751da177e4SLinus Torvalds  */
8761da177e4SLinus Torvalds 
8770c12295aSPatrick McHardy static int ipmr_cache_report(struct mr_table *mrt,
8784feb88e5SBenjamin Thery 			     struct sk_buff *pkt, vifi_t vifi, int assert)
8791da177e4SLinus Torvalds {
8801da177e4SLinus Torvalds 	struct sk_buff *skb;
881c9bdd4b5SArnaldo Carvalho de Melo 	const int ihl = ip_hdrlen(pkt);
8821da177e4SLinus Torvalds 	struct igmphdr *igmp;
8831da177e4SLinus Torvalds 	struct igmpmsg *msg;
8844c968709SEric Dumazet 	struct sock *mroute_sk;
8851da177e4SLinus Torvalds 	int ret;
8861da177e4SLinus Torvalds 
8871da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM
8881da177e4SLinus Torvalds 	if (assert == IGMPMSG_WHOLEPKT)
8891da177e4SLinus Torvalds 		skb = skb_realloc_headroom(pkt, sizeof(struct iphdr));
8901da177e4SLinus Torvalds 	else
8911da177e4SLinus Torvalds #endif
8921da177e4SLinus Torvalds 		skb = alloc_skb(128, GFP_ATOMIC);
8931da177e4SLinus Torvalds 
8941da177e4SLinus Torvalds 	if (!skb)
8951da177e4SLinus Torvalds 		return -ENOBUFS;
8961da177e4SLinus Torvalds 
8971da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM
8981da177e4SLinus Torvalds 	if (assert == IGMPMSG_WHOLEPKT) {
8991da177e4SLinus Torvalds 		/* Ugly, but we have no choice with this interface.
900a8cb16ddSEric Dumazet 		 * Duplicate old header, fix ihl, length etc.
901a8cb16ddSEric Dumazet 		 * And all this only to mangle msg->im_msgtype and
902a8cb16ddSEric Dumazet 		 * to set msg->im_mbz to "mbz" :-)
9031da177e4SLinus Torvalds 		 */
904878c8145SArnaldo Carvalho de Melo 		skb_push(skb, sizeof(struct iphdr));
905878c8145SArnaldo Carvalho de Melo 		skb_reset_network_header(skb);
906badff6d0SArnaldo Carvalho de Melo 		skb_reset_transport_header(skb);
9070272ffc4SArnaldo Carvalho de Melo 		msg = (struct igmpmsg *)skb_network_header(skb);
908d56f90a7SArnaldo Carvalho de Melo 		memcpy(msg, skb_network_header(pkt), sizeof(struct iphdr));
9091da177e4SLinus Torvalds 		msg->im_msgtype = IGMPMSG_WHOLEPKT;
9101da177e4SLinus Torvalds 		msg->im_mbz = 0;
9110c12295aSPatrick McHardy 		msg->im_vif = mrt->mroute_reg_vif_num;
912eddc9ec5SArnaldo Carvalho de Melo 		ip_hdr(skb)->ihl = sizeof(struct iphdr) >> 2;
913eddc9ec5SArnaldo Carvalho de Melo 		ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(pkt)->tot_len) +
914eddc9ec5SArnaldo Carvalho de Melo 					     sizeof(struct iphdr));
9151da177e4SLinus Torvalds 	} else
9161da177e4SLinus Torvalds #endif
9171da177e4SLinus Torvalds 	{
9181da177e4SLinus Torvalds 
919a8cb16ddSEric Dumazet 	/* Copy the IP header */
9201da177e4SLinus Torvalds 
92127a884dcSArnaldo Carvalho de Melo 	skb->network_header = skb->tail;
922ddc7b8e3SArnaldo Carvalho de Melo 	skb_put(skb, ihl);
92327d7ff46SArnaldo Carvalho de Melo 	skb_copy_to_linear_data(skb, pkt->data, ihl);
924eddc9ec5SArnaldo Carvalho de Melo 	ip_hdr(skb)->protocol = 0;	/* Flag to the kernel this is a route add */
925eddc9ec5SArnaldo Carvalho de Melo 	msg = (struct igmpmsg *)skb_network_header(skb);
9261da177e4SLinus Torvalds 	msg->im_vif = vifi;
927adf30907SEric Dumazet 	skb_dst_set(skb, dst_clone(skb_dst(pkt)));
9281da177e4SLinus Torvalds 
929a8cb16ddSEric Dumazet 	/* Add our header */
9301da177e4SLinus Torvalds 
9311da177e4SLinus Torvalds 	igmp = (struct igmphdr *)skb_put(skb, sizeof(struct igmphdr));
9321da177e4SLinus Torvalds 	igmp->type	=
9331da177e4SLinus Torvalds 	msg->im_msgtype = assert;
9341da177e4SLinus Torvalds 	igmp->code	= 0;
935eddc9ec5SArnaldo Carvalho de Melo 	ip_hdr(skb)->tot_len = htons(skb->len);		/* Fix the length */
936b0e380b1SArnaldo Carvalho de Melo 	skb->transport_header = skb->network_header;
9371da177e4SLinus Torvalds 	}
9381da177e4SLinus Torvalds 
9394c968709SEric Dumazet 	rcu_read_lock();
9404c968709SEric Dumazet 	mroute_sk = rcu_dereference(mrt->mroute_sk);
9414c968709SEric Dumazet 	if (mroute_sk == NULL) {
9424c968709SEric Dumazet 		rcu_read_unlock();
9431da177e4SLinus Torvalds 		kfree_skb(skb);
9441da177e4SLinus Torvalds 		return -EINVAL;
9451da177e4SLinus Torvalds 	}
9461da177e4SLinus Torvalds 
947a8cb16ddSEric Dumazet 	/* Deliver to mrouted */
948a8cb16ddSEric Dumazet 
9494c968709SEric Dumazet 	ret = sock_queue_rcv_skb(mroute_sk, skb);
9504c968709SEric Dumazet 	rcu_read_unlock();
95170a269e6SBenjamin Thery 	if (ret < 0) {
9521da177e4SLinus Torvalds 		if (net_ratelimit())
9531da177e4SLinus Torvalds 			printk(KERN_WARNING "mroute: pending queue full, dropping entries.\n");
9541da177e4SLinus Torvalds 		kfree_skb(skb);
9551da177e4SLinus Torvalds 	}
9561da177e4SLinus Torvalds 
9571da177e4SLinus Torvalds 	return ret;
9581da177e4SLinus Torvalds }
9591da177e4SLinus Torvalds 
9601da177e4SLinus Torvalds /*
9611da177e4SLinus Torvalds  *	Queue a packet for resolution. It gets locked cache entry!
9621da177e4SLinus Torvalds  */
9631da177e4SLinus Torvalds 
9641da177e4SLinus Torvalds static int
9650c12295aSPatrick McHardy ipmr_cache_unresolved(struct mr_table *mrt, vifi_t vifi, struct sk_buff *skb)
9661da177e4SLinus Torvalds {
967862465f2SPatrick McHardy 	bool found = false;
9681da177e4SLinus Torvalds 	int err;
9691da177e4SLinus Torvalds 	struct mfc_cache *c;
970eddc9ec5SArnaldo Carvalho de Melo 	const struct iphdr *iph = ip_hdr(skb);
9711da177e4SLinus Torvalds 
9721da177e4SLinus Torvalds 	spin_lock_bh(&mfc_unres_lock);
9730c12295aSPatrick McHardy 	list_for_each_entry(c, &mrt->mfc_unres_queue, list) {
974e258beb2SPatrick McHardy 		if (c->mfc_mcastgrp == iph->daddr &&
975862465f2SPatrick McHardy 		    c->mfc_origin == iph->saddr) {
976862465f2SPatrick McHardy 			found = true;
9771da177e4SLinus Torvalds 			break;
9781da177e4SLinus Torvalds 		}
979862465f2SPatrick McHardy 	}
9801da177e4SLinus Torvalds 
981862465f2SPatrick McHardy 	if (!found) {
982a8cb16ddSEric Dumazet 		/* Create a new entry if allowable */
9831da177e4SLinus Torvalds 
9840c12295aSPatrick McHardy 		if (atomic_read(&mrt->cache_resolve_queue_len) >= 10 ||
985d658f8a0SPatrick McHardy 		    (c = ipmr_cache_alloc_unres()) == NULL) {
9861da177e4SLinus Torvalds 			spin_unlock_bh(&mfc_unres_lock);
9871da177e4SLinus Torvalds 
9881da177e4SLinus Torvalds 			kfree_skb(skb);
9891da177e4SLinus Torvalds 			return -ENOBUFS;
9901da177e4SLinus Torvalds 		}
9911da177e4SLinus Torvalds 
992a8cb16ddSEric Dumazet 		/* Fill in the new cache entry */
993a8cb16ddSEric Dumazet 
9941da177e4SLinus Torvalds 		c->mfc_parent	= -1;
995eddc9ec5SArnaldo Carvalho de Melo 		c->mfc_origin	= iph->saddr;
996eddc9ec5SArnaldo Carvalho de Melo 		c->mfc_mcastgrp	= iph->daddr;
9971da177e4SLinus Torvalds 
998a8cb16ddSEric Dumazet 		/* Reflect first query at mrouted. */
999a8cb16ddSEric Dumazet 
10000c12295aSPatrick McHardy 		err = ipmr_cache_report(mrt, skb, vifi, IGMPMSG_NOCACHE);
10014feb88e5SBenjamin Thery 		if (err < 0) {
10021da177e4SLinus Torvalds 			/* If the report failed throw the cache entry
10031da177e4SLinus Torvalds 			   out - Brad Parker
10041da177e4SLinus Torvalds 			 */
10051da177e4SLinus Torvalds 			spin_unlock_bh(&mfc_unres_lock);
10061da177e4SLinus Torvalds 
10075c0a66f5SBenjamin Thery 			ipmr_cache_free(c);
10081da177e4SLinus Torvalds 			kfree_skb(skb);
10091da177e4SLinus Torvalds 			return err;
10101da177e4SLinus Torvalds 		}
10111da177e4SLinus Torvalds 
10120c12295aSPatrick McHardy 		atomic_inc(&mrt->cache_resolve_queue_len);
10130c12295aSPatrick McHardy 		list_add(&c->list, &mrt->mfc_unres_queue);
10141da177e4SLinus Torvalds 
1015278554bdSDavid S. Miller 		if (atomic_read(&mrt->cache_resolve_queue_len) == 1)
10160c12295aSPatrick McHardy 			mod_timer(&mrt->ipmr_expire_timer, c->mfc_un.unres.expires);
10171da177e4SLinus Torvalds 	}
10181da177e4SLinus Torvalds 
1019a8cb16ddSEric Dumazet 	/* See if we can append the packet */
1020a8cb16ddSEric Dumazet 
10211da177e4SLinus Torvalds 	if (c->mfc_un.unres.unresolved.qlen > 3) {
10221da177e4SLinus Torvalds 		kfree_skb(skb);
10231da177e4SLinus Torvalds 		err = -ENOBUFS;
10241da177e4SLinus Torvalds 	} else {
10251da177e4SLinus Torvalds 		skb_queue_tail(&c->mfc_un.unres.unresolved, skb);
10261da177e4SLinus Torvalds 		err = 0;
10271da177e4SLinus Torvalds 	}
10281da177e4SLinus Torvalds 
10291da177e4SLinus Torvalds 	spin_unlock_bh(&mfc_unres_lock);
10301da177e4SLinus Torvalds 	return err;
10311da177e4SLinus Torvalds }
10321da177e4SLinus Torvalds 
10331da177e4SLinus Torvalds /*
10341da177e4SLinus Torvalds  *	MFC cache manipulation by user space mroute daemon
10351da177e4SLinus Torvalds  */
10361da177e4SLinus Torvalds 
10370c12295aSPatrick McHardy static int ipmr_mfc_delete(struct mr_table *mrt, struct mfcctl *mfc)
10381da177e4SLinus Torvalds {
10391da177e4SLinus Torvalds 	int line;
1040862465f2SPatrick McHardy 	struct mfc_cache *c, *next;
10411da177e4SLinus Torvalds 
10421da177e4SLinus Torvalds 	line = MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr);
10431da177e4SLinus Torvalds 
10440c12295aSPatrick McHardy 	list_for_each_entry_safe(c, next, &mrt->mfc_cache_array[line], list) {
10451da177e4SLinus Torvalds 		if (c->mfc_origin == mfc->mfcc_origin.s_addr &&
10461da177e4SLinus Torvalds 		    c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr) {
1047a8c9486bSEric Dumazet 			list_del_rcu(&c->list);
10481da177e4SLinus Torvalds 
10495c0a66f5SBenjamin Thery 			ipmr_cache_free(c);
10501da177e4SLinus Torvalds 			return 0;
10511da177e4SLinus Torvalds 		}
10521da177e4SLinus Torvalds 	}
10531da177e4SLinus Torvalds 	return -ENOENT;
10541da177e4SLinus Torvalds }
10551da177e4SLinus Torvalds 
10560c12295aSPatrick McHardy static int ipmr_mfc_add(struct net *net, struct mr_table *mrt,
10570c12295aSPatrick McHardy 			struct mfcctl *mfc, int mrtsock)
10581da177e4SLinus Torvalds {
1059862465f2SPatrick McHardy 	bool found = false;
10601da177e4SLinus Torvalds 	int line;
1061862465f2SPatrick McHardy 	struct mfc_cache *uc, *c;
10621da177e4SLinus Torvalds 
1063a50436f2SPatrick McHardy 	if (mfc->mfcc_parent >= MAXVIFS)
1064a50436f2SPatrick McHardy 		return -ENFILE;
1065a50436f2SPatrick McHardy 
10661da177e4SLinus Torvalds 	line = MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr);
10671da177e4SLinus Torvalds 
10680c12295aSPatrick McHardy 	list_for_each_entry(c, &mrt->mfc_cache_array[line], list) {
10691da177e4SLinus Torvalds 		if (c->mfc_origin == mfc->mfcc_origin.s_addr &&
1070862465f2SPatrick McHardy 		    c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr) {
1071862465f2SPatrick McHardy 			found = true;
10721da177e4SLinus Torvalds 			break;
10731da177e4SLinus Torvalds 		}
1074862465f2SPatrick McHardy 	}
10751da177e4SLinus Torvalds 
1076862465f2SPatrick McHardy 	if (found) {
10771da177e4SLinus Torvalds 		write_lock_bh(&mrt_lock);
10781da177e4SLinus Torvalds 		c->mfc_parent = mfc->mfcc_parent;
10790c12295aSPatrick McHardy 		ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls);
10801da177e4SLinus Torvalds 		if (!mrtsock)
10811da177e4SLinus Torvalds 			c->mfc_flags |= MFC_STATIC;
10821da177e4SLinus Torvalds 		write_unlock_bh(&mrt_lock);
10831da177e4SLinus Torvalds 		return 0;
10841da177e4SLinus Torvalds 	}
10851da177e4SLinus Torvalds 
1086f97c1e0cSJoe Perches 	if (!ipv4_is_multicast(mfc->mfcc_mcastgrp.s_addr))
10871da177e4SLinus Torvalds 		return -EINVAL;
10881da177e4SLinus Torvalds 
1089d658f8a0SPatrick McHardy 	c = ipmr_cache_alloc();
10901da177e4SLinus Torvalds 	if (c == NULL)
10911da177e4SLinus Torvalds 		return -ENOMEM;
10921da177e4SLinus Torvalds 
10931da177e4SLinus Torvalds 	c->mfc_origin = mfc->mfcc_origin.s_addr;
10941da177e4SLinus Torvalds 	c->mfc_mcastgrp = mfc->mfcc_mcastgrp.s_addr;
10951da177e4SLinus Torvalds 	c->mfc_parent = mfc->mfcc_parent;
10960c12295aSPatrick McHardy 	ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls);
10971da177e4SLinus Torvalds 	if (!mrtsock)
10981da177e4SLinus Torvalds 		c->mfc_flags |= MFC_STATIC;
10991da177e4SLinus Torvalds 
1100a8c9486bSEric Dumazet 	list_add_rcu(&c->list, &mrt->mfc_cache_array[line]);
11011da177e4SLinus Torvalds 
11021da177e4SLinus Torvalds 	/*
11031da177e4SLinus Torvalds 	 *	Check to see if we resolved a queued list. If so we
11041da177e4SLinus Torvalds 	 *	need to send on the frames and tidy up.
11051da177e4SLinus Torvalds 	 */
1106b0ebb739SPatrick McHardy 	found = false;
11071da177e4SLinus Torvalds 	spin_lock_bh(&mfc_unres_lock);
11080c12295aSPatrick McHardy 	list_for_each_entry(uc, &mrt->mfc_unres_queue, list) {
1109e258beb2SPatrick McHardy 		if (uc->mfc_origin == c->mfc_origin &&
11101da177e4SLinus Torvalds 		    uc->mfc_mcastgrp == c->mfc_mcastgrp) {
1111862465f2SPatrick McHardy 			list_del(&uc->list);
11120c12295aSPatrick McHardy 			atomic_dec(&mrt->cache_resolve_queue_len);
1113b0ebb739SPatrick McHardy 			found = true;
11141da177e4SLinus Torvalds 			break;
11151da177e4SLinus Torvalds 		}
11161da177e4SLinus Torvalds 	}
11170c12295aSPatrick McHardy 	if (list_empty(&mrt->mfc_unres_queue))
11180c12295aSPatrick McHardy 		del_timer(&mrt->ipmr_expire_timer);
11191da177e4SLinus Torvalds 	spin_unlock_bh(&mfc_unres_lock);
11201da177e4SLinus Torvalds 
1121b0ebb739SPatrick McHardy 	if (found) {
11220c12295aSPatrick McHardy 		ipmr_cache_resolve(net, mrt, uc, c);
11235c0a66f5SBenjamin Thery 		ipmr_cache_free(uc);
11241da177e4SLinus Torvalds 	}
11251da177e4SLinus Torvalds 	return 0;
11261da177e4SLinus Torvalds }
11271da177e4SLinus Torvalds 
11281da177e4SLinus Torvalds /*
11291da177e4SLinus Torvalds  *	Close the multicast socket, and clear the vif tables etc
11301da177e4SLinus Torvalds  */
11311da177e4SLinus Torvalds 
11320c12295aSPatrick McHardy static void mroute_clean_tables(struct mr_table *mrt)
11331da177e4SLinus Torvalds {
11341da177e4SLinus Torvalds 	int i;
1135d17fa6faSEric Dumazet 	LIST_HEAD(list);
1136862465f2SPatrick McHardy 	struct mfc_cache *c, *next;
11371da177e4SLinus Torvalds 
1138a8cb16ddSEric Dumazet 	/* Shut down all active vif entries */
1139a8cb16ddSEric Dumazet 
11400c12295aSPatrick McHardy 	for (i = 0; i < mrt->maxvif; i++) {
11410c12295aSPatrick McHardy 		if (!(mrt->vif_table[i].flags & VIFF_STATIC))
11420c12295aSPatrick McHardy 			vif_delete(mrt, i, 0, &list);
11431da177e4SLinus Torvalds 	}
1144d17fa6faSEric Dumazet 	unregister_netdevice_many(&list);
11451da177e4SLinus Torvalds 
1146a8cb16ddSEric Dumazet 	/* Wipe the cache */
1147a8cb16ddSEric Dumazet 
11481da177e4SLinus Torvalds 	for (i = 0; i < MFC_LINES; i++) {
11490c12295aSPatrick McHardy 		list_for_each_entry_safe(c, next, &mrt->mfc_cache_array[i], list) {
1150862465f2SPatrick McHardy 			if (c->mfc_flags & MFC_STATIC)
11511da177e4SLinus Torvalds 				continue;
1152a8c9486bSEric Dumazet 			list_del_rcu(&c->list);
11535c0a66f5SBenjamin Thery 			ipmr_cache_free(c);
11541da177e4SLinus Torvalds 		}
11551da177e4SLinus Torvalds 	}
11561da177e4SLinus Torvalds 
11570c12295aSPatrick McHardy 	if (atomic_read(&mrt->cache_resolve_queue_len) != 0) {
11581da177e4SLinus Torvalds 		spin_lock_bh(&mfc_unres_lock);
11590c12295aSPatrick McHardy 		list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
1160862465f2SPatrick McHardy 			list_del(&c->list);
11610c12295aSPatrick McHardy 			ipmr_destroy_unres(mrt, c);
11621da177e4SLinus Torvalds 		}
11631da177e4SLinus Torvalds 		spin_unlock_bh(&mfc_unres_lock);
11641da177e4SLinus Torvalds 	}
11651da177e4SLinus Torvalds }
11661da177e4SLinus Torvalds 
11674c968709SEric Dumazet /* called from ip_ra_control(), before an RCU grace period,
11684c968709SEric Dumazet  * we dont need to call synchronize_rcu() here
11694c968709SEric Dumazet  */
11701da177e4SLinus Torvalds static void mrtsock_destruct(struct sock *sk)
11711da177e4SLinus Torvalds {
11724feb88e5SBenjamin Thery 	struct net *net = sock_net(sk);
1173f0ad0860SPatrick McHardy 	struct mr_table *mrt;
11744feb88e5SBenjamin Thery 
11751da177e4SLinus Torvalds 	rtnl_lock();
1176f0ad0860SPatrick McHardy 	ipmr_for_each_table(mrt, net) {
11774c968709SEric Dumazet 		if (sk == rtnl_dereference(mrt->mroute_sk)) {
11784feb88e5SBenjamin Thery 			IPV4_DEVCONF_ALL(net, MC_FORWARDING)--;
11794c968709SEric Dumazet 			rcu_assign_pointer(mrt->mroute_sk, NULL);
11800c12295aSPatrick McHardy 			mroute_clean_tables(mrt);
11811da177e4SLinus Torvalds 		}
11821da177e4SLinus Torvalds 	}
11831da177e4SLinus Torvalds 	rtnl_unlock();
11841da177e4SLinus Torvalds }
11851da177e4SLinus Torvalds 
11861da177e4SLinus Torvalds /*
11871da177e4SLinus Torvalds  *	Socket options and virtual interface manipulation. The whole
11881da177e4SLinus Torvalds  *	virtual interface system is a complete heap, but unfortunately
11891da177e4SLinus Torvalds  *	that's how BSD mrouted happens to think. Maybe one day with a proper
11901da177e4SLinus Torvalds  *	MOSPF/PIM router set up we can clean this up.
11911da177e4SLinus Torvalds  */
11921da177e4SLinus Torvalds 
1193b7058842SDavid S. Miller int ip_mroute_setsockopt(struct sock *sk, int optname, char __user *optval, unsigned int optlen)
11941da177e4SLinus Torvalds {
11951da177e4SLinus Torvalds 	int ret;
11961da177e4SLinus Torvalds 	struct vifctl vif;
11971da177e4SLinus Torvalds 	struct mfcctl mfc;
11984feb88e5SBenjamin Thery 	struct net *net = sock_net(sk);
1199f0ad0860SPatrick McHardy 	struct mr_table *mrt;
1200f0ad0860SPatrick McHardy 
1201f0ad0860SPatrick McHardy 	mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1202f0ad0860SPatrick McHardy 	if (mrt == NULL)
1203f0ad0860SPatrick McHardy 		return -ENOENT;
12041da177e4SLinus Torvalds 
1205132adf54SStephen Hemminger 	if (optname != MRT_INIT) {
12064c968709SEric Dumazet 		if (sk != rcu_dereference_raw(mrt->mroute_sk) &&
12074c968709SEric Dumazet 		    !capable(CAP_NET_ADMIN))
12081da177e4SLinus Torvalds 			return -EACCES;
12091da177e4SLinus Torvalds 	}
12101da177e4SLinus Torvalds 
1211132adf54SStephen Hemminger 	switch (optname) {
12121da177e4SLinus Torvalds 	case MRT_INIT:
12131da177e4SLinus Torvalds 		if (sk->sk_type != SOCK_RAW ||
1214c720c7e8SEric Dumazet 		    inet_sk(sk)->inet_num != IPPROTO_IGMP)
12151da177e4SLinus Torvalds 			return -EOPNOTSUPP;
12161da177e4SLinus Torvalds 		if (optlen != sizeof(int))
12171da177e4SLinus Torvalds 			return -ENOPROTOOPT;
12181da177e4SLinus Torvalds 
12191da177e4SLinus Torvalds 		rtnl_lock();
12204c968709SEric Dumazet 		if (rtnl_dereference(mrt->mroute_sk)) {
12211da177e4SLinus Torvalds 			rtnl_unlock();
12221da177e4SLinus Torvalds 			return -EADDRINUSE;
12231da177e4SLinus Torvalds 		}
12241da177e4SLinus Torvalds 
12251da177e4SLinus Torvalds 		ret = ip_ra_control(sk, 1, mrtsock_destruct);
12261da177e4SLinus Torvalds 		if (ret == 0) {
12274c968709SEric Dumazet 			rcu_assign_pointer(mrt->mroute_sk, sk);
12284feb88e5SBenjamin Thery 			IPV4_DEVCONF_ALL(net, MC_FORWARDING)++;
12291da177e4SLinus Torvalds 		}
12301da177e4SLinus Torvalds 		rtnl_unlock();
12311da177e4SLinus Torvalds 		return ret;
12321da177e4SLinus Torvalds 	case MRT_DONE:
12334c968709SEric Dumazet 		if (sk != rcu_dereference_raw(mrt->mroute_sk))
12341da177e4SLinus Torvalds 			return -EACCES;
12351da177e4SLinus Torvalds 		return ip_ra_control(sk, 0, NULL);
12361da177e4SLinus Torvalds 	case MRT_ADD_VIF:
12371da177e4SLinus Torvalds 	case MRT_DEL_VIF:
12381da177e4SLinus Torvalds 		if (optlen != sizeof(vif))
12391da177e4SLinus Torvalds 			return -EINVAL;
12401da177e4SLinus Torvalds 		if (copy_from_user(&vif, optval, sizeof(vif)))
12411da177e4SLinus Torvalds 			return -EFAULT;
12421da177e4SLinus Torvalds 		if (vif.vifc_vifi >= MAXVIFS)
12431da177e4SLinus Torvalds 			return -ENFILE;
12441da177e4SLinus Torvalds 		rtnl_lock();
12451da177e4SLinus Torvalds 		if (optname == MRT_ADD_VIF) {
12464c968709SEric Dumazet 			ret = vif_add(net, mrt, &vif,
12474c968709SEric Dumazet 				      sk == rtnl_dereference(mrt->mroute_sk));
12481da177e4SLinus Torvalds 		} else {
12490c12295aSPatrick McHardy 			ret = vif_delete(mrt, vif.vifc_vifi, 0, NULL);
12501da177e4SLinus Torvalds 		}
12511da177e4SLinus Torvalds 		rtnl_unlock();
12521da177e4SLinus Torvalds 		return ret;
12531da177e4SLinus Torvalds 
12541da177e4SLinus Torvalds 		/*
12551da177e4SLinus Torvalds 		 *	Manipulate the forwarding caches. These live
12561da177e4SLinus Torvalds 		 *	in a sort of kernel/user symbiosis.
12571da177e4SLinus Torvalds 		 */
12581da177e4SLinus Torvalds 	case MRT_ADD_MFC:
12591da177e4SLinus Torvalds 	case MRT_DEL_MFC:
12601da177e4SLinus Torvalds 		if (optlen != sizeof(mfc))
12611da177e4SLinus Torvalds 			return -EINVAL;
12621da177e4SLinus Torvalds 		if (copy_from_user(&mfc, optval, sizeof(mfc)))
12631da177e4SLinus Torvalds 			return -EFAULT;
12641da177e4SLinus Torvalds 		rtnl_lock();
12651da177e4SLinus Torvalds 		if (optname == MRT_DEL_MFC)
12660c12295aSPatrick McHardy 			ret = ipmr_mfc_delete(mrt, &mfc);
12671da177e4SLinus Torvalds 		else
12684c968709SEric Dumazet 			ret = ipmr_mfc_add(net, mrt, &mfc,
12694c968709SEric Dumazet 					   sk == rtnl_dereference(mrt->mroute_sk));
12701da177e4SLinus Torvalds 		rtnl_unlock();
12711da177e4SLinus Torvalds 		return ret;
12721da177e4SLinus Torvalds 		/*
12731da177e4SLinus Torvalds 		 *	Control PIM assert.
12741da177e4SLinus Torvalds 		 */
12751da177e4SLinus Torvalds 	case MRT_ASSERT:
12761da177e4SLinus Torvalds 	{
12771da177e4SLinus Torvalds 		int v;
12781da177e4SLinus Torvalds 		if (get_user(v, (int __user *)optval))
12791da177e4SLinus Torvalds 			return -EFAULT;
12800c12295aSPatrick McHardy 		mrt->mroute_do_assert = (v) ? 1 : 0;
12811da177e4SLinus Torvalds 		return 0;
12821da177e4SLinus Torvalds 	}
12831da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM
12841da177e4SLinus Torvalds 	case MRT_PIM:
12851da177e4SLinus Torvalds 	{
1286ba93ef74SStephen Hemminger 		int v;
1287ba93ef74SStephen Hemminger 
12881da177e4SLinus Torvalds 		if (get_user(v, (int __user *)optval))
12891da177e4SLinus Torvalds 			return -EFAULT;
12901da177e4SLinus Torvalds 		v = (v) ? 1 : 0;
1291ba93ef74SStephen Hemminger 
12921da177e4SLinus Torvalds 		rtnl_lock();
12931da177e4SLinus Torvalds 		ret = 0;
12940c12295aSPatrick McHardy 		if (v != mrt->mroute_do_pim) {
12950c12295aSPatrick McHardy 			mrt->mroute_do_pim = v;
12960c12295aSPatrick McHardy 			mrt->mroute_do_assert = v;
12971da177e4SLinus Torvalds 		}
12981da177e4SLinus Torvalds 		rtnl_unlock();
12991da177e4SLinus Torvalds 		return ret;
13001da177e4SLinus Torvalds 	}
13011da177e4SLinus Torvalds #endif
1302f0ad0860SPatrick McHardy #ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
1303f0ad0860SPatrick McHardy 	case MRT_TABLE:
1304f0ad0860SPatrick McHardy 	{
1305f0ad0860SPatrick McHardy 		u32 v;
1306f0ad0860SPatrick McHardy 
1307f0ad0860SPatrick McHardy 		if (optlen != sizeof(u32))
1308f0ad0860SPatrick McHardy 			return -EINVAL;
1309f0ad0860SPatrick McHardy 		if (get_user(v, (u32 __user *)optval))
1310f0ad0860SPatrick McHardy 			return -EFAULT;
1311f0ad0860SPatrick McHardy 
1312f0ad0860SPatrick McHardy 		rtnl_lock();
1313f0ad0860SPatrick McHardy 		ret = 0;
13144c968709SEric Dumazet 		if (sk == rtnl_dereference(mrt->mroute_sk)) {
13154c968709SEric Dumazet 			ret = -EBUSY;
13164c968709SEric Dumazet 		} else {
1317f0ad0860SPatrick McHardy 			if (!ipmr_new_table(net, v))
1318f0ad0860SPatrick McHardy 				ret = -ENOMEM;
1319f0ad0860SPatrick McHardy 			raw_sk(sk)->ipmr_table = v;
13204c968709SEric Dumazet 		}
1321f0ad0860SPatrick McHardy 		rtnl_unlock();
1322f0ad0860SPatrick McHardy 		return ret;
1323f0ad0860SPatrick McHardy 	}
1324f0ad0860SPatrick McHardy #endif
13251da177e4SLinus Torvalds 	/*
13261da177e4SLinus Torvalds 	 *	Spurious command, or MRT_VERSION which you cannot
13271da177e4SLinus Torvalds 	 *	set.
13281da177e4SLinus Torvalds 	 */
13291da177e4SLinus Torvalds 	default:
13301da177e4SLinus Torvalds 		return -ENOPROTOOPT;
13311da177e4SLinus Torvalds 	}
13321da177e4SLinus Torvalds }
13331da177e4SLinus Torvalds 
13341da177e4SLinus Torvalds /*
13351da177e4SLinus Torvalds  *	Getsock opt support for the multicast routing system.
13361da177e4SLinus Torvalds  */
13371da177e4SLinus Torvalds 
13381da177e4SLinus Torvalds int ip_mroute_getsockopt(struct sock *sk, int optname, char __user *optval, int __user *optlen)
13391da177e4SLinus Torvalds {
13401da177e4SLinus Torvalds 	int olr;
13411da177e4SLinus Torvalds 	int val;
13424feb88e5SBenjamin Thery 	struct net *net = sock_net(sk);
1343f0ad0860SPatrick McHardy 	struct mr_table *mrt;
1344f0ad0860SPatrick McHardy 
1345f0ad0860SPatrick McHardy 	mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1346f0ad0860SPatrick McHardy 	if (mrt == NULL)
1347f0ad0860SPatrick McHardy 		return -ENOENT;
13481da177e4SLinus Torvalds 
13491da177e4SLinus Torvalds 	if (optname != MRT_VERSION &&
13501da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM
13511da177e4SLinus Torvalds 	   optname != MRT_PIM &&
13521da177e4SLinus Torvalds #endif
13531da177e4SLinus Torvalds 	   optname != MRT_ASSERT)
13541da177e4SLinus Torvalds 		return -ENOPROTOOPT;
13551da177e4SLinus Torvalds 
13561da177e4SLinus Torvalds 	if (get_user(olr, optlen))
13571da177e4SLinus Torvalds 		return -EFAULT;
13581da177e4SLinus Torvalds 
13591da177e4SLinus Torvalds 	olr = min_t(unsigned int, olr, sizeof(int));
13601da177e4SLinus Torvalds 	if (olr < 0)
13611da177e4SLinus Torvalds 		return -EINVAL;
13621da177e4SLinus Torvalds 
13631da177e4SLinus Torvalds 	if (put_user(olr, optlen))
13641da177e4SLinus Torvalds 		return -EFAULT;
13651da177e4SLinus Torvalds 	if (optname == MRT_VERSION)
13661da177e4SLinus Torvalds 		val = 0x0305;
13671da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM
13681da177e4SLinus Torvalds 	else if (optname == MRT_PIM)
13690c12295aSPatrick McHardy 		val = mrt->mroute_do_pim;
13701da177e4SLinus Torvalds #endif
13711da177e4SLinus Torvalds 	else
13720c12295aSPatrick McHardy 		val = mrt->mroute_do_assert;
13731da177e4SLinus Torvalds 	if (copy_to_user(optval, &val, olr))
13741da177e4SLinus Torvalds 		return -EFAULT;
13751da177e4SLinus Torvalds 	return 0;
13761da177e4SLinus Torvalds }
13771da177e4SLinus Torvalds 
13781da177e4SLinus Torvalds /*
13791da177e4SLinus Torvalds  *	The IP multicast ioctl support routines.
13801da177e4SLinus Torvalds  */
13811da177e4SLinus Torvalds 
13821da177e4SLinus Torvalds int ipmr_ioctl(struct sock *sk, int cmd, void __user *arg)
13831da177e4SLinus Torvalds {
13841da177e4SLinus Torvalds 	struct sioc_sg_req sr;
13851da177e4SLinus Torvalds 	struct sioc_vif_req vr;
13861da177e4SLinus Torvalds 	struct vif_device *vif;
13871da177e4SLinus Torvalds 	struct mfc_cache *c;
13884feb88e5SBenjamin Thery 	struct net *net = sock_net(sk);
1389f0ad0860SPatrick McHardy 	struct mr_table *mrt;
1390f0ad0860SPatrick McHardy 
1391f0ad0860SPatrick McHardy 	mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1392f0ad0860SPatrick McHardy 	if (mrt == NULL)
1393f0ad0860SPatrick McHardy 		return -ENOENT;
13941da177e4SLinus Torvalds 
1395132adf54SStephen Hemminger 	switch (cmd) {
13961da177e4SLinus Torvalds 	case SIOCGETVIFCNT:
13971da177e4SLinus Torvalds 		if (copy_from_user(&vr, arg, sizeof(vr)))
13981da177e4SLinus Torvalds 			return -EFAULT;
13990c12295aSPatrick McHardy 		if (vr.vifi >= mrt->maxvif)
14001da177e4SLinus Torvalds 			return -EINVAL;
14011da177e4SLinus Torvalds 		read_lock(&mrt_lock);
14020c12295aSPatrick McHardy 		vif = &mrt->vif_table[vr.vifi];
14030c12295aSPatrick McHardy 		if (VIF_EXISTS(mrt, vr.vifi)) {
14041da177e4SLinus Torvalds 			vr.icount = vif->pkt_in;
14051da177e4SLinus Torvalds 			vr.ocount = vif->pkt_out;
14061da177e4SLinus Torvalds 			vr.ibytes = vif->bytes_in;
14071da177e4SLinus Torvalds 			vr.obytes = vif->bytes_out;
14081da177e4SLinus Torvalds 			read_unlock(&mrt_lock);
14091da177e4SLinus Torvalds 
14101da177e4SLinus Torvalds 			if (copy_to_user(arg, &vr, sizeof(vr)))
14111da177e4SLinus Torvalds 				return -EFAULT;
14121da177e4SLinus Torvalds 			return 0;
14131da177e4SLinus Torvalds 		}
14141da177e4SLinus Torvalds 		read_unlock(&mrt_lock);
14151da177e4SLinus Torvalds 		return -EADDRNOTAVAIL;
14161da177e4SLinus Torvalds 	case SIOCGETSGCNT:
14171da177e4SLinus Torvalds 		if (copy_from_user(&sr, arg, sizeof(sr)))
14181da177e4SLinus Torvalds 			return -EFAULT;
14191da177e4SLinus Torvalds 
1420a8c9486bSEric Dumazet 		rcu_read_lock();
14210c12295aSPatrick McHardy 		c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
14221da177e4SLinus Torvalds 		if (c) {
14231da177e4SLinus Torvalds 			sr.pktcnt = c->mfc_un.res.pkt;
14241da177e4SLinus Torvalds 			sr.bytecnt = c->mfc_un.res.bytes;
14251da177e4SLinus Torvalds 			sr.wrong_if = c->mfc_un.res.wrong_if;
1426a8c9486bSEric Dumazet 			rcu_read_unlock();
14271da177e4SLinus Torvalds 
14281da177e4SLinus Torvalds 			if (copy_to_user(arg, &sr, sizeof(sr)))
14291da177e4SLinus Torvalds 				return -EFAULT;
14301da177e4SLinus Torvalds 			return 0;
14311da177e4SLinus Torvalds 		}
1432a8c9486bSEric Dumazet 		rcu_read_unlock();
14331da177e4SLinus Torvalds 		return -EADDRNOTAVAIL;
14341da177e4SLinus Torvalds 	default:
14351da177e4SLinus Torvalds 		return -ENOIOCTLCMD;
14361da177e4SLinus Torvalds 	}
14371da177e4SLinus Torvalds }
14381da177e4SLinus Torvalds 
1439709b46e8SEric W. Biederman #ifdef CONFIG_COMPAT
1440709b46e8SEric W. Biederman struct compat_sioc_sg_req {
1441709b46e8SEric W. Biederman 	struct in_addr src;
1442709b46e8SEric W. Biederman 	struct in_addr grp;
1443709b46e8SEric W. Biederman 	compat_ulong_t pktcnt;
1444709b46e8SEric W. Biederman 	compat_ulong_t bytecnt;
1445709b46e8SEric W. Biederman 	compat_ulong_t wrong_if;
1446709b46e8SEric W. Biederman };
1447709b46e8SEric W. Biederman 
1448ca6b8bb0SDavid S. Miller struct compat_sioc_vif_req {
1449ca6b8bb0SDavid S. Miller 	vifi_t	vifi;		/* Which iface */
1450ca6b8bb0SDavid S. Miller 	compat_ulong_t icount;
1451ca6b8bb0SDavid S. Miller 	compat_ulong_t ocount;
1452ca6b8bb0SDavid S. Miller 	compat_ulong_t ibytes;
1453ca6b8bb0SDavid S. Miller 	compat_ulong_t obytes;
1454ca6b8bb0SDavid S. Miller };
1455ca6b8bb0SDavid S. Miller 
1456709b46e8SEric W. Biederman int ipmr_compat_ioctl(struct sock *sk, unsigned int cmd, void __user *arg)
1457709b46e8SEric W. Biederman {
14580033d5adSDavid S. Miller 	struct compat_sioc_sg_req sr;
1459ca6b8bb0SDavid S. Miller 	struct compat_sioc_vif_req vr;
1460ca6b8bb0SDavid S. Miller 	struct vif_device *vif;
1461709b46e8SEric W. Biederman 	struct mfc_cache *c;
1462709b46e8SEric W. Biederman 	struct net *net = sock_net(sk);
1463709b46e8SEric W. Biederman 	struct mr_table *mrt;
1464709b46e8SEric W. Biederman 
1465709b46e8SEric W. Biederman 	mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1466709b46e8SEric W. Biederman 	if (mrt == NULL)
1467709b46e8SEric W. Biederman 		return -ENOENT;
1468709b46e8SEric W. Biederman 
1469709b46e8SEric W. Biederman 	switch (cmd) {
1470ca6b8bb0SDavid S. Miller 	case SIOCGETVIFCNT:
1471ca6b8bb0SDavid S. Miller 		if (copy_from_user(&vr, arg, sizeof(vr)))
1472ca6b8bb0SDavid S. Miller 			return -EFAULT;
1473ca6b8bb0SDavid S. Miller 		if (vr.vifi >= mrt->maxvif)
1474ca6b8bb0SDavid S. Miller 			return -EINVAL;
1475ca6b8bb0SDavid S. Miller 		read_lock(&mrt_lock);
1476ca6b8bb0SDavid S. Miller 		vif = &mrt->vif_table[vr.vifi];
1477ca6b8bb0SDavid S. Miller 		if (VIF_EXISTS(mrt, vr.vifi)) {
1478ca6b8bb0SDavid S. Miller 			vr.icount = vif->pkt_in;
1479ca6b8bb0SDavid S. Miller 			vr.ocount = vif->pkt_out;
1480ca6b8bb0SDavid S. Miller 			vr.ibytes = vif->bytes_in;
1481ca6b8bb0SDavid S. Miller 			vr.obytes = vif->bytes_out;
1482ca6b8bb0SDavid S. Miller 			read_unlock(&mrt_lock);
1483ca6b8bb0SDavid S. Miller 
1484ca6b8bb0SDavid S. Miller 			if (copy_to_user(arg, &vr, sizeof(vr)))
1485ca6b8bb0SDavid S. Miller 				return -EFAULT;
1486ca6b8bb0SDavid S. Miller 			return 0;
1487ca6b8bb0SDavid S. Miller 		}
1488ca6b8bb0SDavid S. Miller 		read_unlock(&mrt_lock);
1489ca6b8bb0SDavid S. Miller 		return -EADDRNOTAVAIL;
1490709b46e8SEric W. Biederman 	case SIOCGETSGCNT:
1491709b46e8SEric W. Biederman 		if (copy_from_user(&sr, arg, sizeof(sr)))
1492709b46e8SEric W. Biederman 			return -EFAULT;
1493709b46e8SEric W. Biederman 
1494709b46e8SEric W. Biederman 		rcu_read_lock();
1495709b46e8SEric W. Biederman 		c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
1496709b46e8SEric W. Biederman 		if (c) {
1497709b46e8SEric W. Biederman 			sr.pktcnt = c->mfc_un.res.pkt;
1498709b46e8SEric W. Biederman 			sr.bytecnt = c->mfc_un.res.bytes;
1499709b46e8SEric W. Biederman 			sr.wrong_if = c->mfc_un.res.wrong_if;
1500709b46e8SEric W. Biederman 			rcu_read_unlock();
1501709b46e8SEric W. Biederman 
1502709b46e8SEric W. Biederman 			if (copy_to_user(arg, &sr, sizeof(sr)))
1503709b46e8SEric W. Biederman 				return -EFAULT;
1504709b46e8SEric W. Biederman 			return 0;
1505709b46e8SEric W. Biederman 		}
1506709b46e8SEric W. Biederman 		rcu_read_unlock();
1507709b46e8SEric W. Biederman 		return -EADDRNOTAVAIL;
1508709b46e8SEric W. Biederman 	default:
1509709b46e8SEric W. Biederman 		return -ENOIOCTLCMD;
1510709b46e8SEric W. Biederman 	}
1511709b46e8SEric W. Biederman }
1512709b46e8SEric W. Biederman #endif
1513709b46e8SEric W. Biederman 
15141da177e4SLinus Torvalds 
15151da177e4SLinus Torvalds static int ipmr_device_event(struct notifier_block *this, unsigned long event, void *ptr)
15161da177e4SLinus Torvalds {
1517e9dc8653SEric W. Biederman 	struct net_device *dev = ptr;
15184feb88e5SBenjamin Thery 	struct net *net = dev_net(dev);
1519f0ad0860SPatrick McHardy 	struct mr_table *mrt;
15201da177e4SLinus Torvalds 	struct vif_device *v;
15211da177e4SLinus Torvalds 	int ct;
1522d17fa6faSEric Dumazet 	LIST_HEAD(list);
1523e9dc8653SEric W. Biederman 
15241da177e4SLinus Torvalds 	if (event != NETDEV_UNREGISTER)
15251da177e4SLinus Torvalds 		return NOTIFY_DONE;
1526f0ad0860SPatrick McHardy 
1527f0ad0860SPatrick McHardy 	ipmr_for_each_table(mrt, net) {
15280c12295aSPatrick McHardy 		v = &mrt->vif_table[0];
15290c12295aSPatrick McHardy 		for (ct = 0; ct < mrt->maxvif; ct++, v++) {
1530e9dc8653SEric W. Biederman 			if (v->dev == dev)
15310c12295aSPatrick McHardy 				vif_delete(mrt, ct, 1, &list);
15321da177e4SLinus Torvalds 		}
15331da177e4SLinus Torvalds 	}
1534d17fa6faSEric Dumazet 	unregister_netdevice_many(&list);
15351da177e4SLinus Torvalds 	return NOTIFY_DONE;
15361da177e4SLinus Torvalds }
15371da177e4SLinus Torvalds 
15381da177e4SLinus Torvalds 
15391da177e4SLinus Torvalds static struct notifier_block ip_mr_notifier = {
15401da177e4SLinus Torvalds 	.notifier_call = ipmr_device_event,
15411da177e4SLinus Torvalds };
15421da177e4SLinus Torvalds 
15431da177e4SLinus Torvalds /*
15441da177e4SLinus Torvalds  *	Encapsulate a packet by attaching a valid IPIP header to it.
15451da177e4SLinus Torvalds  *	This avoids tunnel drivers and other mess and gives us the speed so
15461da177e4SLinus Torvalds  *	important for multicast video.
15471da177e4SLinus Torvalds  */
15481da177e4SLinus Torvalds 
1549114c7844SAl Viro static void ip_encap(struct sk_buff *skb, __be32 saddr, __be32 daddr)
15501da177e4SLinus Torvalds {
15518856dfa3SArnaldo Carvalho de Melo 	struct iphdr *iph;
1552b71d1d42SEric Dumazet 	const struct iphdr *old_iph = ip_hdr(skb);
15538856dfa3SArnaldo Carvalho de Melo 
15548856dfa3SArnaldo Carvalho de Melo 	skb_push(skb, sizeof(struct iphdr));
1555b0e380b1SArnaldo Carvalho de Melo 	skb->transport_header = skb->network_header;
15568856dfa3SArnaldo Carvalho de Melo 	skb_reset_network_header(skb);
1557eddc9ec5SArnaldo Carvalho de Melo 	iph = ip_hdr(skb);
15581da177e4SLinus Torvalds 
15591da177e4SLinus Torvalds 	iph->version	=	4;
1560e023dd64SArnaldo Carvalho de Melo 	iph->tos	=	old_iph->tos;
1561e023dd64SArnaldo Carvalho de Melo 	iph->ttl	=	old_iph->ttl;
15621da177e4SLinus Torvalds 	iph->frag_off	=	0;
15631da177e4SLinus Torvalds 	iph->daddr	=	daddr;
15641da177e4SLinus Torvalds 	iph->saddr	=	saddr;
15651da177e4SLinus Torvalds 	iph->protocol	=	IPPROTO_IPIP;
15661da177e4SLinus Torvalds 	iph->ihl	=	5;
15671da177e4SLinus Torvalds 	iph->tot_len	=	htons(skb->len);
1568adf30907SEric Dumazet 	ip_select_ident(iph, skb_dst(skb), NULL);
15691da177e4SLinus Torvalds 	ip_send_check(iph);
15701da177e4SLinus Torvalds 
15711da177e4SLinus Torvalds 	memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
15721da177e4SLinus Torvalds 	nf_reset(skb);
15731da177e4SLinus Torvalds }
15741da177e4SLinus Torvalds 
15751da177e4SLinus Torvalds static inline int ipmr_forward_finish(struct sk_buff *skb)
15761da177e4SLinus Torvalds {
15771da177e4SLinus Torvalds 	struct ip_options *opt = &(IPCB(skb)->opt);
15781da177e4SLinus Torvalds 
1579adf30907SEric Dumazet 	IP_INC_STATS_BH(dev_net(skb_dst(skb)->dev), IPSTATS_MIB_OUTFORWDATAGRAMS);
15801da177e4SLinus Torvalds 
15811da177e4SLinus Torvalds 	if (unlikely(opt->optlen))
15821da177e4SLinus Torvalds 		ip_forward_options(skb);
15831da177e4SLinus Torvalds 
15841da177e4SLinus Torvalds 	return dst_output(skb);
15851da177e4SLinus Torvalds }
15861da177e4SLinus Torvalds 
15871da177e4SLinus Torvalds /*
15881da177e4SLinus Torvalds  *	Processing handlers for ipmr_forward
15891da177e4SLinus Torvalds  */
15901da177e4SLinus Torvalds 
15910c12295aSPatrick McHardy static void ipmr_queue_xmit(struct net *net, struct mr_table *mrt,
15920c12295aSPatrick McHardy 			    struct sk_buff *skb, struct mfc_cache *c, int vifi)
15931da177e4SLinus Torvalds {
1594eddc9ec5SArnaldo Carvalho de Melo 	const struct iphdr *iph = ip_hdr(skb);
15950c12295aSPatrick McHardy 	struct vif_device *vif = &mrt->vif_table[vifi];
15961da177e4SLinus Torvalds 	struct net_device *dev;
15971da177e4SLinus Torvalds 	struct rtable *rt;
159831e4543dSDavid S. Miller 	struct flowi4 fl4;
15991da177e4SLinus Torvalds 	int    encap = 0;
16001da177e4SLinus Torvalds 
16011da177e4SLinus Torvalds 	if (vif->dev == NULL)
16021da177e4SLinus Torvalds 		goto out_free;
16031da177e4SLinus Torvalds 
16041da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM
16051da177e4SLinus Torvalds 	if (vif->flags & VIFF_REGISTER) {
16061da177e4SLinus Torvalds 		vif->pkt_out++;
16071da177e4SLinus Torvalds 		vif->bytes_out += skb->len;
1608cf3677aeSPavel Emelyanov 		vif->dev->stats.tx_bytes += skb->len;
1609cf3677aeSPavel Emelyanov 		vif->dev->stats.tx_packets++;
16100c12295aSPatrick McHardy 		ipmr_cache_report(mrt, skb, vifi, IGMPMSG_WHOLEPKT);
161169ebbf58SIlpo Järvinen 		goto out_free;
16121da177e4SLinus Torvalds 	}
16131da177e4SLinus Torvalds #endif
16141da177e4SLinus Torvalds 
16151da177e4SLinus Torvalds 	if (vif->flags & VIFF_TUNNEL) {
161631e4543dSDavid S. Miller 		rt = ip_route_output_ports(net, &fl4, NULL,
161778fbfd8aSDavid S. Miller 					   vif->remote, vif->local,
161878fbfd8aSDavid S. Miller 					   0, 0,
161978fbfd8aSDavid S. Miller 					   IPPROTO_IPIP,
162078fbfd8aSDavid S. Miller 					   RT_TOS(iph->tos), vif->link);
1621b23dd4feSDavid S. Miller 		if (IS_ERR(rt))
16221da177e4SLinus Torvalds 			goto out_free;
16231da177e4SLinus Torvalds 		encap = sizeof(struct iphdr);
16241da177e4SLinus Torvalds 	} else {
162531e4543dSDavid S. Miller 		rt = ip_route_output_ports(net, &fl4, NULL, iph->daddr, 0,
162678fbfd8aSDavid S. Miller 					   0, 0,
162778fbfd8aSDavid S. Miller 					   IPPROTO_IPIP,
162878fbfd8aSDavid S. Miller 					   RT_TOS(iph->tos), vif->link);
1629b23dd4feSDavid S. Miller 		if (IS_ERR(rt))
16301da177e4SLinus Torvalds 			goto out_free;
16311da177e4SLinus Torvalds 	}
16321da177e4SLinus Torvalds 
1633d8d1f30bSChangli Gao 	dev = rt->dst.dev;
16341da177e4SLinus Torvalds 
1635d8d1f30bSChangli Gao 	if (skb->len+encap > dst_mtu(&rt->dst) && (ntohs(iph->frag_off) & IP_DF)) {
16361da177e4SLinus Torvalds 		/* Do not fragment multicasts. Alas, IPv4 does not
1637a8cb16ddSEric Dumazet 		 * allow to send ICMP, so that packets will disappear
1638a8cb16ddSEric Dumazet 		 * to blackhole.
16391da177e4SLinus Torvalds 		 */
16401da177e4SLinus Torvalds 
16417c73a6faSPavel Emelyanov 		IP_INC_STATS_BH(dev_net(dev), IPSTATS_MIB_FRAGFAILS);
16421da177e4SLinus Torvalds 		ip_rt_put(rt);
16431da177e4SLinus Torvalds 		goto out_free;
16441da177e4SLinus Torvalds 	}
16451da177e4SLinus Torvalds 
1646d8d1f30bSChangli Gao 	encap += LL_RESERVED_SPACE(dev) + rt->dst.header_len;
16471da177e4SLinus Torvalds 
16481da177e4SLinus Torvalds 	if (skb_cow(skb, encap)) {
16491da177e4SLinus Torvalds 		ip_rt_put(rt);
16501da177e4SLinus Torvalds 		goto out_free;
16511da177e4SLinus Torvalds 	}
16521da177e4SLinus Torvalds 
16531da177e4SLinus Torvalds 	vif->pkt_out++;
16541da177e4SLinus Torvalds 	vif->bytes_out += skb->len;
16551da177e4SLinus Torvalds 
1656adf30907SEric Dumazet 	skb_dst_drop(skb);
1657d8d1f30bSChangli Gao 	skb_dst_set(skb, &rt->dst);
1658eddc9ec5SArnaldo Carvalho de Melo 	ip_decrease_ttl(ip_hdr(skb));
16591da177e4SLinus Torvalds 
16601da177e4SLinus Torvalds 	/* FIXME: forward and output firewalls used to be called here.
1661a8cb16ddSEric Dumazet 	 * What do we do with netfilter? -- RR
1662a8cb16ddSEric Dumazet 	 */
16631da177e4SLinus Torvalds 	if (vif->flags & VIFF_TUNNEL) {
16641da177e4SLinus Torvalds 		ip_encap(skb, vif->local, vif->remote);
16651da177e4SLinus Torvalds 		/* FIXME: extra output firewall step used to be here. --RR */
16662f4c02d4SPavel Emelyanov 		vif->dev->stats.tx_packets++;
16672f4c02d4SPavel Emelyanov 		vif->dev->stats.tx_bytes += skb->len;
16681da177e4SLinus Torvalds 	}
16691da177e4SLinus Torvalds 
16701da177e4SLinus Torvalds 	IPCB(skb)->flags |= IPSKB_FORWARDED;
16711da177e4SLinus Torvalds 
16721da177e4SLinus Torvalds 	/*
16731da177e4SLinus Torvalds 	 * RFC1584 teaches, that DVMRP/PIM router must deliver packets locally
16741da177e4SLinus Torvalds 	 * not only before forwarding, but after forwarding on all output
16751da177e4SLinus Torvalds 	 * interfaces. It is clear, if mrouter runs a multicasting
16761da177e4SLinus Torvalds 	 * program, it should receive packets not depending to what interface
16771da177e4SLinus Torvalds 	 * program is joined.
16781da177e4SLinus Torvalds 	 * If we will not make it, the program will have to join on all
16791da177e4SLinus Torvalds 	 * interfaces. On the other hand, multihoming host (or router, but
16801da177e4SLinus Torvalds 	 * not mrouter) cannot join to more than one interface - it will
16811da177e4SLinus Torvalds 	 * result in receiving multiple packets.
16821da177e4SLinus Torvalds 	 */
16839bbc768aSJan Engelhardt 	NF_HOOK(NFPROTO_IPV4, NF_INET_FORWARD, skb, skb->dev, dev,
16841da177e4SLinus Torvalds 		ipmr_forward_finish);
16851da177e4SLinus Torvalds 	return;
16861da177e4SLinus Torvalds 
16871da177e4SLinus Torvalds out_free:
16881da177e4SLinus Torvalds 	kfree_skb(skb);
16891da177e4SLinus Torvalds }
16901da177e4SLinus Torvalds 
16910c12295aSPatrick McHardy static int ipmr_find_vif(struct mr_table *mrt, struct net_device *dev)
16921da177e4SLinus Torvalds {
16931da177e4SLinus Torvalds 	int ct;
16940c12295aSPatrick McHardy 
16950c12295aSPatrick McHardy 	for (ct = mrt->maxvif-1; ct >= 0; ct--) {
16960c12295aSPatrick McHardy 		if (mrt->vif_table[ct].dev == dev)
16971da177e4SLinus Torvalds 			break;
16981da177e4SLinus Torvalds 	}
16991da177e4SLinus Torvalds 	return ct;
17001da177e4SLinus Torvalds }
17011da177e4SLinus Torvalds 
17021da177e4SLinus Torvalds /* "local" means that we should preserve one skb (for local delivery) */
17031da177e4SLinus Torvalds 
17040c12295aSPatrick McHardy static int ip_mr_forward(struct net *net, struct mr_table *mrt,
17050c12295aSPatrick McHardy 			 struct sk_buff *skb, struct mfc_cache *cache,
17060c12295aSPatrick McHardy 			 int local)
17071da177e4SLinus Torvalds {
17081da177e4SLinus Torvalds 	int psend = -1;
17091da177e4SLinus Torvalds 	int vif, ct;
17101da177e4SLinus Torvalds 
17111da177e4SLinus Torvalds 	vif = cache->mfc_parent;
17121da177e4SLinus Torvalds 	cache->mfc_un.res.pkt++;
17131da177e4SLinus Torvalds 	cache->mfc_un.res.bytes += skb->len;
17141da177e4SLinus Torvalds 
17151da177e4SLinus Torvalds 	/*
17161da177e4SLinus Torvalds 	 * Wrong interface: drop packet and (maybe) send PIM assert.
17171da177e4SLinus Torvalds 	 */
17180c12295aSPatrick McHardy 	if (mrt->vif_table[vif].dev != skb->dev) {
17191da177e4SLinus Torvalds 		int true_vifi;
17201da177e4SLinus Torvalds 
1721c7537967SDavid S. Miller 		if (rt_is_output_route(skb_rtable(skb))) {
17221da177e4SLinus Torvalds 			/* It is our own packet, looped back.
1723a8cb16ddSEric Dumazet 			 * Very complicated situation...
1724a8cb16ddSEric Dumazet 			 *
1725a8cb16ddSEric Dumazet 			 * The best workaround until routing daemons will be
1726a8cb16ddSEric Dumazet 			 * fixed is not to redistribute packet, if it was
1727a8cb16ddSEric Dumazet 			 * send through wrong interface. It means, that
1728a8cb16ddSEric Dumazet 			 * multicast applications WILL NOT work for
1729a8cb16ddSEric Dumazet 			 * (S,G), which have default multicast route pointing
1730a8cb16ddSEric Dumazet 			 * to wrong oif. In any case, it is not a good
1731a8cb16ddSEric Dumazet 			 * idea to use multicasting applications on router.
17321da177e4SLinus Torvalds 			 */
17331da177e4SLinus Torvalds 			goto dont_forward;
17341da177e4SLinus Torvalds 		}
17351da177e4SLinus Torvalds 
17361da177e4SLinus Torvalds 		cache->mfc_un.res.wrong_if++;
17370c12295aSPatrick McHardy 		true_vifi = ipmr_find_vif(mrt, skb->dev);
17381da177e4SLinus Torvalds 
17390c12295aSPatrick McHardy 		if (true_vifi >= 0 && mrt->mroute_do_assert &&
17401da177e4SLinus Torvalds 		    /* pimsm uses asserts, when switching from RPT to SPT,
1741a8cb16ddSEric Dumazet 		     * so that we cannot check that packet arrived on an oif.
1742a8cb16ddSEric Dumazet 		     * It is bad, but otherwise we would need to move pretty
1743a8cb16ddSEric Dumazet 		     * large chunk of pimd to kernel. Ough... --ANK
17441da177e4SLinus Torvalds 		     */
17450c12295aSPatrick McHardy 		    (mrt->mroute_do_pim ||
17466f9374a9SBenjamin Thery 		     cache->mfc_un.res.ttls[true_vifi] < 255) &&
17471da177e4SLinus Torvalds 		    time_after(jiffies,
17481da177e4SLinus Torvalds 			       cache->mfc_un.res.last_assert + MFC_ASSERT_THRESH)) {
17491da177e4SLinus Torvalds 			cache->mfc_un.res.last_assert = jiffies;
17500c12295aSPatrick McHardy 			ipmr_cache_report(mrt, skb, true_vifi, IGMPMSG_WRONGVIF);
17511da177e4SLinus Torvalds 		}
17521da177e4SLinus Torvalds 		goto dont_forward;
17531da177e4SLinus Torvalds 	}
17541da177e4SLinus Torvalds 
17550c12295aSPatrick McHardy 	mrt->vif_table[vif].pkt_in++;
17560c12295aSPatrick McHardy 	mrt->vif_table[vif].bytes_in += skb->len;
17571da177e4SLinus Torvalds 
17581da177e4SLinus Torvalds 	/*
17591da177e4SLinus Torvalds 	 *	Forward the frame
17601da177e4SLinus Torvalds 	 */
1761a8cb16ddSEric Dumazet 	for (ct = cache->mfc_un.res.maxvif - 1;
1762a8cb16ddSEric Dumazet 	     ct >= cache->mfc_un.res.minvif; ct--) {
1763eddc9ec5SArnaldo Carvalho de Melo 		if (ip_hdr(skb)->ttl > cache->mfc_un.res.ttls[ct]) {
17641da177e4SLinus Torvalds 			if (psend != -1) {
17651da177e4SLinus Torvalds 				struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
1766a8cb16ddSEric Dumazet 
17671da177e4SLinus Torvalds 				if (skb2)
17680c12295aSPatrick McHardy 					ipmr_queue_xmit(net, mrt, skb2, cache,
17690c12295aSPatrick McHardy 							psend);
17701da177e4SLinus Torvalds 			}
17711da177e4SLinus Torvalds 			psend = ct;
17721da177e4SLinus Torvalds 		}
17731da177e4SLinus Torvalds 	}
17741da177e4SLinus Torvalds 	if (psend != -1) {
17751da177e4SLinus Torvalds 		if (local) {
17761da177e4SLinus Torvalds 			struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
1777a8cb16ddSEric Dumazet 
17781da177e4SLinus Torvalds 			if (skb2)
17790c12295aSPatrick McHardy 				ipmr_queue_xmit(net, mrt, skb2, cache, psend);
17801da177e4SLinus Torvalds 		} else {
17810c12295aSPatrick McHardy 			ipmr_queue_xmit(net, mrt, skb, cache, psend);
17821da177e4SLinus Torvalds 			return 0;
17831da177e4SLinus Torvalds 		}
17841da177e4SLinus Torvalds 	}
17851da177e4SLinus Torvalds 
17861da177e4SLinus Torvalds dont_forward:
17871da177e4SLinus Torvalds 	if (!local)
17881da177e4SLinus Torvalds 		kfree_skb(skb);
17891da177e4SLinus Torvalds 	return 0;
17901da177e4SLinus Torvalds }
17911da177e4SLinus Torvalds 
1792417da66fSDavid S. Miller static struct mr_table *ipmr_rt_fib_lookup(struct net *net, struct sk_buff *skb)
1793ee3f1aafSDavid S. Miller {
1794417da66fSDavid S. Miller 	struct rtable *rt = skb_rtable(skb);
1795417da66fSDavid S. Miller 	struct iphdr *iph = ip_hdr(skb);
1796da91981bSDavid S. Miller 	struct flowi4 fl4 = {
1797417da66fSDavid S. Miller 		.daddr = iph->daddr,
1798417da66fSDavid S. Miller 		.saddr = iph->saddr,
1799*b0fe4a31SJulian Anastasov 		.flowi4_tos = RT_TOS(iph->tos),
1800da91981bSDavid S. Miller 		.flowi4_oif = rt->rt_oif,
1801da91981bSDavid S. Miller 		.flowi4_iif = rt->rt_iif,
1802da91981bSDavid S. Miller 		.flowi4_mark = rt->rt_mark,
1803ee3f1aafSDavid S. Miller 	};
1804ee3f1aafSDavid S. Miller 	struct mr_table *mrt;
1805ee3f1aafSDavid S. Miller 	int err;
1806ee3f1aafSDavid S. Miller 
1807da91981bSDavid S. Miller 	err = ipmr_fib_lookup(net, &fl4, &mrt);
1808ee3f1aafSDavid S. Miller 	if (err)
1809ee3f1aafSDavid S. Miller 		return ERR_PTR(err);
1810ee3f1aafSDavid S. Miller 	return mrt;
1811ee3f1aafSDavid S. Miller }
18121da177e4SLinus Torvalds 
18131da177e4SLinus Torvalds /*
18141da177e4SLinus Torvalds  *	Multicast packets for forwarding arrive here
18154c968709SEric Dumazet  *	Called with rcu_read_lock();
18161da177e4SLinus Torvalds  */
18171da177e4SLinus Torvalds 
18181da177e4SLinus Torvalds int ip_mr_input(struct sk_buff *skb)
18191da177e4SLinus Torvalds {
18201da177e4SLinus Torvalds 	struct mfc_cache *cache;
18214feb88e5SBenjamin Thery 	struct net *net = dev_net(skb->dev);
1822511c3f92SEric Dumazet 	int local = skb_rtable(skb)->rt_flags & RTCF_LOCAL;
1823f0ad0860SPatrick McHardy 	struct mr_table *mrt;
18241da177e4SLinus Torvalds 
18251da177e4SLinus Torvalds 	/* Packet is looped back after forward, it should not be
1826a8cb16ddSEric Dumazet 	 * forwarded second time, but still can be delivered locally.
18271da177e4SLinus Torvalds 	 */
18281da177e4SLinus Torvalds 	if (IPCB(skb)->flags & IPSKB_FORWARDED)
18291da177e4SLinus Torvalds 		goto dont_forward;
18301da177e4SLinus Torvalds 
1831417da66fSDavid S. Miller 	mrt = ipmr_rt_fib_lookup(net, skb);
1832ee3f1aafSDavid S. Miller 	if (IS_ERR(mrt)) {
1833e40dbc51SBen Greear 		kfree_skb(skb);
1834ee3f1aafSDavid S. Miller 		return PTR_ERR(mrt);
18355e2b61f7SDavid S. Miller 	}
18361da177e4SLinus Torvalds 	if (!local) {
18371da177e4SLinus Torvalds 		if (IPCB(skb)->opt.router_alert) {
18381da177e4SLinus Torvalds 			if (ip_call_ra_chain(skb))
18391da177e4SLinus Torvalds 				return 0;
1840eddc9ec5SArnaldo Carvalho de Melo 		} else if (ip_hdr(skb)->protocol == IPPROTO_IGMP) {
18411da177e4SLinus Torvalds 			/* IGMPv1 (and broken IGMPv2 implementations sort of
18424c968709SEric Dumazet 			 * Cisco IOS <= 11.2(8)) do not put router alert
18434c968709SEric Dumazet 			 * option to IGMP packets destined to routable
18444c968709SEric Dumazet 			 * groups. It is very bad, because it means
18454c968709SEric Dumazet 			 * that we can forward NO IGMP messages.
18461da177e4SLinus Torvalds 			 */
18474c968709SEric Dumazet 			struct sock *mroute_sk;
18484c968709SEric Dumazet 
18494c968709SEric Dumazet 			mroute_sk = rcu_dereference(mrt->mroute_sk);
18504c968709SEric Dumazet 			if (mroute_sk) {
18512715bcf9SPatrick McHardy 				nf_reset(skb);
18524c968709SEric Dumazet 				raw_rcv(mroute_sk, skb);
18531da177e4SLinus Torvalds 				return 0;
18541da177e4SLinus Torvalds 			}
18551da177e4SLinus Torvalds 		    }
18561da177e4SLinus Torvalds 	}
18571da177e4SLinus Torvalds 
1858a8c9486bSEric Dumazet 	/* already under rcu_read_lock() */
18590c12295aSPatrick McHardy 	cache = ipmr_cache_find(mrt, ip_hdr(skb)->saddr, ip_hdr(skb)->daddr);
18601da177e4SLinus Torvalds 
18611da177e4SLinus Torvalds 	/*
18621da177e4SLinus Torvalds 	 *	No usable cache entry
18631da177e4SLinus Torvalds 	 */
18641da177e4SLinus Torvalds 	if (cache == NULL) {
18651da177e4SLinus Torvalds 		int vif;
18661da177e4SLinus Torvalds 
18671da177e4SLinus Torvalds 		if (local) {
18681da177e4SLinus Torvalds 			struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
18691da177e4SLinus Torvalds 			ip_local_deliver(skb);
1870a8c9486bSEric Dumazet 			if (skb2 == NULL)
18711da177e4SLinus Torvalds 				return -ENOBUFS;
18721da177e4SLinus Torvalds 			skb = skb2;
18731da177e4SLinus Torvalds 		}
18741da177e4SLinus Torvalds 
1875a8c9486bSEric Dumazet 		read_lock(&mrt_lock);
18760c12295aSPatrick McHardy 		vif = ipmr_find_vif(mrt, skb->dev);
18771da177e4SLinus Torvalds 		if (vif >= 0) {
18780eae88f3SEric Dumazet 			int err2 = ipmr_cache_unresolved(mrt, vif, skb);
18791da177e4SLinus Torvalds 			read_unlock(&mrt_lock);
18801da177e4SLinus Torvalds 
18810eae88f3SEric Dumazet 			return err2;
18821da177e4SLinus Torvalds 		}
18831da177e4SLinus Torvalds 		read_unlock(&mrt_lock);
18841da177e4SLinus Torvalds 		kfree_skb(skb);
18851da177e4SLinus Torvalds 		return -ENODEV;
18861da177e4SLinus Torvalds 	}
18871da177e4SLinus Torvalds 
1888a8c9486bSEric Dumazet 	read_lock(&mrt_lock);
18890c12295aSPatrick McHardy 	ip_mr_forward(net, mrt, skb, cache, local);
18901da177e4SLinus Torvalds 	read_unlock(&mrt_lock);
18911da177e4SLinus Torvalds 
18921da177e4SLinus Torvalds 	if (local)
18931da177e4SLinus Torvalds 		return ip_local_deliver(skb);
18941da177e4SLinus Torvalds 
18951da177e4SLinus Torvalds 	return 0;
18961da177e4SLinus Torvalds 
18971da177e4SLinus Torvalds dont_forward:
18981da177e4SLinus Torvalds 	if (local)
18991da177e4SLinus Torvalds 		return ip_local_deliver(skb);
19001da177e4SLinus Torvalds 	kfree_skb(skb);
19011da177e4SLinus Torvalds 	return 0;
19021da177e4SLinus Torvalds }
19031da177e4SLinus Torvalds 
1904b1879204SIlpo Järvinen #ifdef CONFIG_IP_PIMSM
190555747a0aSEric Dumazet /* called with rcu_read_lock() */
1906f0ad0860SPatrick McHardy static int __pim_rcv(struct mr_table *mrt, struct sk_buff *skb,
1907f0ad0860SPatrick McHardy 		     unsigned int pimlen)
19081da177e4SLinus Torvalds {
19091da177e4SLinus Torvalds 	struct net_device *reg_dev = NULL;
1910b1879204SIlpo Järvinen 	struct iphdr *encap;
19111da177e4SLinus Torvalds 
1912b1879204SIlpo Järvinen 	encap = (struct iphdr *)(skb_transport_header(skb) + pimlen);
19131da177e4SLinus Torvalds 	/*
1914a8cb16ddSEric Dumazet 	 * Check that:
1915a8cb16ddSEric Dumazet 	 * a. packet is really sent to a multicast group
1916a8cb16ddSEric Dumazet 	 * b. packet is not a NULL-REGISTER
1917a8cb16ddSEric Dumazet 	 * c. packet is not truncated
19181da177e4SLinus Torvalds 	 */
1919f97c1e0cSJoe Perches 	if (!ipv4_is_multicast(encap->daddr) ||
19201da177e4SLinus Torvalds 	    encap->tot_len == 0 ||
1921b1879204SIlpo Järvinen 	    ntohs(encap->tot_len) + pimlen > skb->len)
1922b1879204SIlpo Järvinen 		return 1;
19231da177e4SLinus Torvalds 
19241da177e4SLinus Torvalds 	read_lock(&mrt_lock);
19250c12295aSPatrick McHardy 	if (mrt->mroute_reg_vif_num >= 0)
19260c12295aSPatrick McHardy 		reg_dev = mrt->vif_table[mrt->mroute_reg_vif_num].dev;
19271da177e4SLinus Torvalds 	read_unlock(&mrt_lock);
19281da177e4SLinus Torvalds 
19291da177e4SLinus Torvalds 	if (reg_dev == NULL)
1930b1879204SIlpo Järvinen 		return 1;
19311da177e4SLinus Torvalds 
1932b0e380b1SArnaldo Carvalho de Melo 	skb->mac_header = skb->network_header;
19331da177e4SLinus Torvalds 	skb_pull(skb, (u8 *)encap - skb->data);
193431c7711bSArnaldo Carvalho de Melo 	skb_reset_network_header(skb);
19351da177e4SLinus Torvalds 	skb->protocol = htons(ETH_P_IP);
193655747a0aSEric Dumazet 	skb->ip_summed = CHECKSUM_NONE;
19371da177e4SLinus Torvalds 	skb->pkt_type = PACKET_HOST;
1938d19d56ddSEric Dumazet 
1939d19d56ddSEric Dumazet 	skb_tunnel_rx(skb, reg_dev);
1940d19d56ddSEric Dumazet 
19411da177e4SLinus Torvalds 	netif_rx(skb);
1942b1879204SIlpo Järvinen 
194355747a0aSEric Dumazet 	return NET_RX_SUCCESS;
1944b1879204SIlpo Järvinen }
1945b1879204SIlpo Järvinen #endif
1946b1879204SIlpo Järvinen 
1947b1879204SIlpo Järvinen #ifdef CONFIG_IP_PIMSM_V1
1948b1879204SIlpo Järvinen /*
1949b1879204SIlpo Järvinen  * Handle IGMP messages of PIMv1
1950b1879204SIlpo Järvinen  */
1951b1879204SIlpo Järvinen 
1952b1879204SIlpo Järvinen int pim_rcv_v1(struct sk_buff *skb)
1953b1879204SIlpo Järvinen {
1954b1879204SIlpo Järvinen 	struct igmphdr *pim;
19554feb88e5SBenjamin Thery 	struct net *net = dev_net(skb->dev);
1956f0ad0860SPatrick McHardy 	struct mr_table *mrt;
1957b1879204SIlpo Järvinen 
1958b1879204SIlpo Järvinen 	if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
1959b1879204SIlpo Järvinen 		goto drop;
1960b1879204SIlpo Järvinen 
1961b1879204SIlpo Järvinen 	pim = igmp_hdr(skb);
1962b1879204SIlpo Järvinen 
1963417da66fSDavid S. Miller 	mrt = ipmr_rt_fib_lookup(net, skb);
1964ee3f1aafSDavid S. Miller 	if (IS_ERR(mrt))
1965f0ad0860SPatrick McHardy 		goto drop;
19660c12295aSPatrick McHardy 	if (!mrt->mroute_do_pim ||
1967b1879204SIlpo Järvinen 	    pim->group != PIM_V1_VERSION || pim->code != PIM_V1_REGISTER)
1968b1879204SIlpo Järvinen 		goto drop;
1969b1879204SIlpo Järvinen 
1970f0ad0860SPatrick McHardy 	if (__pim_rcv(mrt, skb, sizeof(*pim))) {
19711da177e4SLinus Torvalds drop:
19721da177e4SLinus Torvalds 		kfree_skb(skb);
1973b1879204SIlpo Järvinen 	}
19741da177e4SLinus Torvalds 	return 0;
19751da177e4SLinus Torvalds }
19761da177e4SLinus Torvalds #endif
19771da177e4SLinus Torvalds 
19781da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM_V2
19791da177e4SLinus Torvalds static int pim_rcv(struct sk_buff *skb)
19801da177e4SLinus Torvalds {
19811da177e4SLinus Torvalds 	struct pimreghdr *pim;
1982f0ad0860SPatrick McHardy 	struct net *net = dev_net(skb->dev);
1983f0ad0860SPatrick McHardy 	struct mr_table *mrt;
19841da177e4SLinus Torvalds 
1985b1879204SIlpo Järvinen 	if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
19861da177e4SLinus Torvalds 		goto drop;
19871da177e4SLinus Torvalds 
19889c70220bSArnaldo Carvalho de Melo 	pim = (struct pimreghdr *)skb_transport_header(skb);
19891da177e4SLinus Torvalds 	if (pim->type != ((PIM_VERSION << 4) | (PIM_REGISTER)) ||
19901da177e4SLinus Torvalds 	    (pim->flags & PIM_NULL_REGISTER) ||
19911da177e4SLinus Torvalds 	    (ip_compute_csum((void *)pim, sizeof(*pim)) != 0 &&
1992d3bc23e7SAl Viro 	     csum_fold(skb_checksum(skb, 0, skb->len, 0))))
19931da177e4SLinus Torvalds 		goto drop;
19941da177e4SLinus Torvalds 
1995417da66fSDavid S. Miller 	mrt = ipmr_rt_fib_lookup(net, skb);
1996ee3f1aafSDavid S. Miller 	if (IS_ERR(mrt))
1997f0ad0860SPatrick McHardy 		goto drop;
1998f0ad0860SPatrick McHardy 	if (__pim_rcv(mrt, skb, sizeof(*pim))) {
19991da177e4SLinus Torvalds drop:
20001da177e4SLinus Torvalds 		kfree_skb(skb);
2001b1879204SIlpo Järvinen 	}
20021da177e4SLinus Torvalds 	return 0;
20031da177e4SLinus Torvalds }
20041da177e4SLinus Torvalds #endif
20051da177e4SLinus Torvalds 
2006cb6a4e46SPatrick McHardy static int __ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
2007cb6a4e46SPatrick McHardy 			      struct mfc_cache *c, struct rtmsg *rtm)
20081da177e4SLinus Torvalds {
20091da177e4SLinus Torvalds 	int ct;
20101da177e4SLinus Torvalds 	struct rtnexthop *nhp;
201127a884dcSArnaldo Carvalho de Melo 	u8 *b = skb_tail_pointer(skb);
20121da177e4SLinus Torvalds 	struct rtattr *mp_head;
20131da177e4SLinus Torvalds 
20147438189bSNicolas Dichtel 	/* If cache is unresolved, don't try to parse IIF and OIF */
2015ed0f160aSDan Carpenter 	if (c->mfc_parent >= MAXVIFS)
20167438189bSNicolas Dichtel 		return -ENOENT;
20177438189bSNicolas Dichtel 
20180c12295aSPatrick McHardy 	if (VIF_EXISTS(mrt, c->mfc_parent))
20190c12295aSPatrick McHardy 		RTA_PUT(skb, RTA_IIF, 4, &mrt->vif_table[c->mfc_parent].dev->ifindex);
20201da177e4SLinus Torvalds 
20211da177e4SLinus Torvalds 	mp_head = (struct rtattr *)skb_put(skb, RTA_LENGTH(0));
20221da177e4SLinus Torvalds 
20231da177e4SLinus Torvalds 	for (ct = c->mfc_un.res.minvif; ct < c->mfc_un.res.maxvif; ct++) {
20240c12295aSPatrick McHardy 		if (VIF_EXISTS(mrt, ct) && c->mfc_un.res.ttls[ct] < 255) {
20251da177e4SLinus Torvalds 			if (skb_tailroom(skb) < RTA_ALIGN(RTA_ALIGN(sizeof(*nhp)) + 4))
20261da177e4SLinus Torvalds 				goto rtattr_failure;
20271da177e4SLinus Torvalds 			nhp = (struct rtnexthop *)skb_put(skb, RTA_ALIGN(sizeof(*nhp)));
20281da177e4SLinus Torvalds 			nhp->rtnh_flags = 0;
20291da177e4SLinus Torvalds 			nhp->rtnh_hops = c->mfc_un.res.ttls[ct];
20300c12295aSPatrick McHardy 			nhp->rtnh_ifindex = mrt->vif_table[ct].dev->ifindex;
20311da177e4SLinus Torvalds 			nhp->rtnh_len = sizeof(*nhp);
20321da177e4SLinus Torvalds 		}
20331da177e4SLinus Torvalds 	}
20341da177e4SLinus Torvalds 	mp_head->rta_type = RTA_MULTIPATH;
203527a884dcSArnaldo Carvalho de Melo 	mp_head->rta_len = skb_tail_pointer(skb) - (u8 *)mp_head;
20361da177e4SLinus Torvalds 	rtm->rtm_type = RTN_MULTICAST;
20371da177e4SLinus Torvalds 	return 1;
20381da177e4SLinus Torvalds 
20391da177e4SLinus Torvalds rtattr_failure:
2040dc5fc579SArnaldo Carvalho de Melo 	nlmsg_trim(skb, b);
20411da177e4SLinus Torvalds 	return -EMSGSIZE;
20421da177e4SLinus Torvalds }
20431da177e4SLinus Torvalds 
20449a1b9496SDavid S. Miller int ipmr_get_route(struct net *net, struct sk_buff *skb,
20459a1b9496SDavid S. Miller 		   __be32 saddr, __be32 daddr,
20469a1b9496SDavid S. Miller 		   struct rtmsg *rtm, int nowait)
20471da177e4SLinus Torvalds {
20481da177e4SLinus Torvalds 	struct mfc_cache *cache;
20499a1b9496SDavid S. Miller 	struct mr_table *mrt;
20509a1b9496SDavid S. Miller 	int err;
20511da177e4SLinus Torvalds 
2052f0ad0860SPatrick McHardy 	mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
2053f0ad0860SPatrick McHardy 	if (mrt == NULL)
2054f0ad0860SPatrick McHardy 		return -ENOENT;
2055f0ad0860SPatrick McHardy 
2056a8c9486bSEric Dumazet 	rcu_read_lock();
20579a1b9496SDavid S. Miller 	cache = ipmr_cache_find(mrt, saddr, daddr);
20581da177e4SLinus Torvalds 
20591da177e4SLinus Torvalds 	if (cache == NULL) {
206072287490SAlexey Kuznetsov 		struct sk_buff *skb2;
2061eddc9ec5SArnaldo Carvalho de Melo 		struct iphdr *iph;
20621da177e4SLinus Torvalds 		struct net_device *dev;
2063a8cb16ddSEric Dumazet 		int vif = -1;
20641da177e4SLinus Torvalds 
20651da177e4SLinus Torvalds 		if (nowait) {
2066a8c9486bSEric Dumazet 			rcu_read_unlock();
20671da177e4SLinus Torvalds 			return -EAGAIN;
20681da177e4SLinus Torvalds 		}
20691da177e4SLinus Torvalds 
20701da177e4SLinus Torvalds 		dev = skb->dev;
2071a8c9486bSEric Dumazet 		read_lock(&mrt_lock);
2072a8cb16ddSEric Dumazet 		if (dev)
2073a8cb16ddSEric Dumazet 			vif = ipmr_find_vif(mrt, dev);
2074a8cb16ddSEric Dumazet 		if (vif < 0) {
20751da177e4SLinus Torvalds 			read_unlock(&mrt_lock);
2076a8c9486bSEric Dumazet 			rcu_read_unlock();
20771da177e4SLinus Torvalds 			return -ENODEV;
20781da177e4SLinus Torvalds 		}
207972287490SAlexey Kuznetsov 		skb2 = skb_clone(skb, GFP_ATOMIC);
208072287490SAlexey Kuznetsov 		if (!skb2) {
208172287490SAlexey Kuznetsov 			read_unlock(&mrt_lock);
2082a8c9486bSEric Dumazet 			rcu_read_unlock();
208372287490SAlexey Kuznetsov 			return -ENOMEM;
208472287490SAlexey Kuznetsov 		}
208572287490SAlexey Kuznetsov 
2086e2d1bca7SArnaldo Carvalho de Melo 		skb_push(skb2, sizeof(struct iphdr));
2087e2d1bca7SArnaldo Carvalho de Melo 		skb_reset_network_header(skb2);
2088eddc9ec5SArnaldo Carvalho de Melo 		iph = ip_hdr(skb2);
2089eddc9ec5SArnaldo Carvalho de Melo 		iph->ihl = sizeof(struct iphdr) >> 2;
20909a1b9496SDavid S. Miller 		iph->saddr = saddr;
20919a1b9496SDavid S. Miller 		iph->daddr = daddr;
2092eddc9ec5SArnaldo Carvalho de Melo 		iph->version = 0;
20930c12295aSPatrick McHardy 		err = ipmr_cache_unresolved(mrt, vif, skb2);
20941da177e4SLinus Torvalds 		read_unlock(&mrt_lock);
2095a8c9486bSEric Dumazet 		rcu_read_unlock();
20961da177e4SLinus Torvalds 		return err;
20971da177e4SLinus Torvalds 	}
20981da177e4SLinus Torvalds 
2099a8c9486bSEric Dumazet 	read_lock(&mrt_lock);
21001da177e4SLinus Torvalds 	if (!nowait && (rtm->rtm_flags & RTM_F_NOTIFY))
21011da177e4SLinus Torvalds 		cache->mfc_flags |= MFC_NOTIFY;
2102cb6a4e46SPatrick McHardy 	err = __ipmr_fill_mroute(mrt, skb, cache, rtm);
21031da177e4SLinus Torvalds 	read_unlock(&mrt_lock);
2104a8c9486bSEric Dumazet 	rcu_read_unlock();
21051da177e4SLinus Torvalds 	return err;
21061da177e4SLinus Torvalds }
21071da177e4SLinus Torvalds 
2108cb6a4e46SPatrick McHardy static int ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
2109cb6a4e46SPatrick McHardy 			    u32 pid, u32 seq, struct mfc_cache *c)
2110cb6a4e46SPatrick McHardy {
2111cb6a4e46SPatrick McHardy 	struct nlmsghdr *nlh;
2112cb6a4e46SPatrick McHardy 	struct rtmsg *rtm;
2113cb6a4e46SPatrick McHardy 
2114cb6a4e46SPatrick McHardy 	nlh = nlmsg_put(skb, pid, seq, RTM_NEWROUTE, sizeof(*rtm), NLM_F_MULTI);
2115cb6a4e46SPatrick McHardy 	if (nlh == NULL)
2116cb6a4e46SPatrick McHardy 		return -EMSGSIZE;
2117cb6a4e46SPatrick McHardy 
2118cb6a4e46SPatrick McHardy 	rtm = nlmsg_data(nlh);
2119cb6a4e46SPatrick McHardy 	rtm->rtm_family   = RTNL_FAMILY_IPMR;
2120cb6a4e46SPatrick McHardy 	rtm->rtm_dst_len  = 32;
2121cb6a4e46SPatrick McHardy 	rtm->rtm_src_len  = 32;
2122cb6a4e46SPatrick McHardy 	rtm->rtm_tos      = 0;
2123cb6a4e46SPatrick McHardy 	rtm->rtm_table    = mrt->id;
2124cb6a4e46SPatrick McHardy 	NLA_PUT_U32(skb, RTA_TABLE, mrt->id);
2125cb6a4e46SPatrick McHardy 	rtm->rtm_type     = RTN_MULTICAST;
2126cb6a4e46SPatrick McHardy 	rtm->rtm_scope    = RT_SCOPE_UNIVERSE;
2127cb6a4e46SPatrick McHardy 	rtm->rtm_protocol = RTPROT_UNSPEC;
2128cb6a4e46SPatrick McHardy 	rtm->rtm_flags    = 0;
2129cb6a4e46SPatrick McHardy 
2130cb6a4e46SPatrick McHardy 	NLA_PUT_BE32(skb, RTA_SRC, c->mfc_origin);
2131cb6a4e46SPatrick McHardy 	NLA_PUT_BE32(skb, RTA_DST, c->mfc_mcastgrp);
2132cb6a4e46SPatrick McHardy 
2133cb6a4e46SPatrick McHardy 	if (__ipmr_fill_mroute(mrt, skb, c, rtm) < 0)
2134cb6a4e46SPatrick McHardy 		goto nla_put_failure;
2135cb6a4e46SPatrick McHardy 
2136cb6a4e46SPatrick McHardy 	return nlmsg_end(skb, nlh);
2137cb6a4e46SPatrick McHardy 
2138cb6a4e46SPatrick McHardy nla_put_failure:
2139cb6a4e46SPatrick McHardy 	nlmsg_cancel(skb, nlh);
2140cb6a4e46SPatrick McHardy 	return -EMSGSIZE;
2141cb6a4e46SPatrick McHardy }
2142cb6a4e46SPatrick McHardy 
2143cb6a4e46SPatrick McHardy static int ipmr_rtm_dumproute(struct sk_buff *skb, struct netlink_callback *cb)
2144cb6a4e46SPatrick McHardy {
2145cb6a4e46SPatrick McHardy 	struct net *net = sock_net(skb->sk);
2146cb6a4e46SPatrick McHardy 	struct mr_table *mrt;
2147cb6a4e46SPatrick McHardy 	struct mfc_cache *mfc;
2148cb6a4e46SPatrick McHardy 	unsigned int t = 0, s_t;
2149cb6a4e46SPatrick McHardy 	unsigned int h = 0, s_h;
2150cb6a4e46SPatrick McHardy 	unsigned int e = 0, s_e;
2151cb6a4e46SPatrick McHardy 
2152cb6a4e46SPatrick McHardy 	s_t = cb->args[0];
2153cb6a4e46SPatrick McHardy 	s_h = cb->args[1];
2154cb6a4e46SPatrick McHardy 	s_e = cb->args[2];
2155cb6a4e46SPatrick McHardy 
2156a8c9486bSEric Dumazet 	rcu_read_lock();
2157cb6a4e46SPatrick McHardy 	ipmr_for_each_table(mrt, net) {
2158cb6a4e46SPatrick McHardy 		if (t < s_t)
2159cb6a4e46SPatrick McHardy 			goto next_table;
2160cb6a4e46SPatrick McHardy 		if (t > s_t)
2161cb6a4e46SPatrick McHardy 			s_h = 0;
2162cb6a4e46SPatrick McHardy 		for (h = s_h; h < MFC_LINES; h++) {
2163a8c9486bSEric Dumazet 			list_for_each_entry_rcu(mfc, &mrt->mfc_cache_array[h], list) {
2164cb6a4e46SPatrick McHardy 				if (e < s_e)
2165cb6a4e46SPatrick McHardy 					goto next_entry;
2166cb6a4e46SPatrick McHardy 				if (ipmr_fill_mroute(mrt, skb,
2167cb6a4e46SPatrick McHardy 						     NETLINK_CB(cb->skb).pid,
2168cb6a4e46SPatrick McHardy 						     cb->nlh->nlmsg_seq,
2169cb6a4e46SPatrick McHardy 						     mfc) < 0)
2170cb6a4e46SPatrick McHardy 					goto done;
2171cb6a4e46SPatrick McHardy next_entry:
2172cb6a4e46SPatrick McHardy 				e++;
2173cb6a4e46SPatrick McHardy 			}
2174cb6a4e46SPatrick McHardy 			e = s_e = 0;
2175cb6a4e46SPatrick McHardy 		}
2176cb6a4e46SPatrick McHardy 		s_h = 0;
2177cb6a4e46SPatrick McHardy next_table:
2178cb6a4e46SPatrick McHardy 		t++;
2179cb6a4e46SPatrick McHardy 	}
2180cb6a4e46SPatrick McHardy done:
2181a8c9486bSEric Dumazet 	rcu_read_unlock();
2182cb6a4e46SPatrick McHardy 
2183cb6a4e46SPatrick McHardy 	cb->args[2] = e;
2184cb6a4e46SPatrick McHardy 	cb->args[1] = h;
2185cb6a4e46SPatrick McHardy 	cb->args[0] = t;
2186cb6a4e46SPatrick McHardy 
2187cb6a4e46SPatrick McHardy 	return skb->len;
2188cb6a4e46SPatrick McHardy }
2189cb6a4e46SPatrick McHardy 
21901da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
21911da177e4SLinus Torvalds /*
2192a8cb16ddSEric Dumazet  *	The /proc interfaces to multicast routing :
2193a8cb16ddSEric Dumazet  *	/proc/net/ip_mr_cache & /proc/net/ip_mr_vif
21941da177e4SLinus Torvalds  */
21951da177e4SLinus Torvalds struct ipmr_vif_iter {
2196f6bb4514SBenjamin Thery 	struct seq_net_private p;
2197f0ad0860SPatrick McHardy 	struct mr_table *mrt;
21981da177e4SLinus Torvalds 	int ct;
21991da177e4SLinus Torvalds };
22001da177e4SLinus Torvalds 
2201f6bb4514SBenjamin Thery static struct vif_device *ipmr_vif_seq_idx(struct net *net,
2202f6bb4514SBenjamin Thery 					   struct ipmr_vif_iter *iter,
22031da177e4SLinus Torvalds 					   loff_t pos)
22041da177e4SLinus Torvalds {
2205f0ad0860SPatrick McHardy 	struct mr_table *mrt = iter->mrt;
22060c12295aSPatrick McHardy 
22070c12295aSPatrick McHardy 	for (iter->ct = 0; iter->ct < mrt->maxvif; ++iter->ct) {
22080c12295aSPatrick McHardy 		if (!VIF_EXISTS(mrt, iter->ct))
22091da177e4SLinus Torvalds 			continue;
22101da177e4SLinus Torvalds 		if (pos-- == 0)
22110c12295aSPatrick McHardy 			return &mrt->vif_table[iter->ct];
22121da177e4SLinus Torvalds 	}
22131da177e4SLinus Torvalds 	return NULL;
22141da177e4SLinus Torvalds }
22151da177e4SLinus Torvalds 
22161da177e4SLinus Torvalds static void *ipmr_vif_seq_start(struct seq_file *seq, loff_t *pos)
2217ba93ef74SStephen Hemminger 	__acquires(mrt_lock)
22181da177e4SLinus Torvalds {
2219f0ad0860SPatrick McHardy 	struct ipmr_vif_iter *iter = seq->private;
2220f6bb4514SBenjamin Thery 	struct net *net = seq_file_net(seq);
2221f0ad0860SPatrick McHardy 	struct mr_table *mrt;
2222f0ad0860SPatrick McHardy 
2223f0ad0860SPatrick McHardy 	mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
2224f0ad0860SPatrick McHardy 	if (mrt == NULL)
2225f0ad0860SPatrick McHardy 		return ERR_PTR(-ENOENT);
2226f0ad0860SPatrick McHardy 
2227f0ad0860SPatrick McHardy 	iter->mrt = mrt;
2228f6bb4514SBenjamin Thery 
22291da177e4SLinus Torvalds 	read_lock(&mrt_lock);
2230f6bb4514SBenjamin Thery 	return *pos ? ipmr_vif_seq_idx(net, seq->private, *pos - 1)
22311da177e4SLinus Torvalds 		: SEQ_START_TOKEN;
22321da177e4SLinus Torvalds }
22331da177e4SLinus Torvalds 
22341da177e4SLinus Torvalds static void *ipmr_vif_seq_next(struct seq_file *seq, void *v, loff_t *pos)
22351da177e4SLinus Torvalds {
22361da177e4SLinus Torvalds 	struct ipmr_vif_iter *iter = seq->private;
2237f6bb4514SBenjamin Thery 	struct net *net = seq_file_net(seq);
2238f0ad0860SPatrick McHardy 	struct mr_table *mrt = iter->mrt;
22391da177e4SLinus Torvalds 
22401da177e4SLinus Torvalds 	++*pos;
22411da177e4SLinus Torvalds 	if (v == SEQ_START_TOKEN)
2242f6bb4514SBenjamin Thery 		return ipmr_vif_seq_idx(net, iter, 0);
22431da177e4SLinus Torvalds 
22440c12295aSPatrick McHardy 	while (++iter->ct < mrt->maxvif) {
22450c12295aSPatrick McHardy 		if (!VIF_EXISTS(mrt, iter->ct))
22461da177e4SLinus Torvalds 			continue;
22470c12295aSPatrick McHardy 		return &mrt->vif_table[iter->ct];
22481da177e4SLinus Torvalds 	}
22491da177e4SLinus Torvalds 	return NULL;
22501da177e4SLinus Torvalds }
22511da177e4SLinus Torvalds 
22521da177e4SLinus Torvalds static void ipmr_vif_seq_stop(struct seq_file *seq, void *v)
2253ba93ef74SStephen Hemminger 	__releases(mrt_lock)
22541da177e4SLinus Torvalds {
22551da177e4SLinus Torvalds 	read_unlock(&mrt_lock);
22561da177e4SLinus Torvalds }
22571da177e4SLinus Torvalds 
22581da177e4SLinus Torvalds static int ipmr_vif_seq_show(struct seq_file *seq, void *v)
22591da177e4SLinus Torvalds {
2260f0ad0860SPatrick McHardy 	struct ipmr_vif_iter *iter = seq->private;
2261f0ad0860SPatrick McHardy 	struct mr_table *mrt = iter->mrt;
2262f6bb4514SBenjamin Thery 
22631da177e4SLinus Torvalds 	if (v == SEQ_START_TOKEN) {
22641da177e4SLinus Torvalds 		seq_puts(seq,
22651da177e4SLinus Torvalds 			 "Interface      BytesIn  PktsIn  BytesOut PktsOut Flags Local    Remote\n");
22661da177e4SLinus Torvalds 	} else {
22671da177e4SLinus Torvalds 		const struct vif_device *vif = v;
22681da177e4SLinus Torvalds 		const char *name =  vif->dev ? vif->dev->name : "none";
22691da177e4SLinus Torvalds 
22701da177e4SLinus Torvalds 		seq_printf(seq,
22711da177e4SLinus Torvalds 			   "%2Zd %-10s %8ld %7ld  %8ld %7ld %05X %08X %08X\n",
22720c12295aSPatrick McHardy 			   vif - mrt->vif_table,
22731da177e4SLinus Torvalds 			   name, vif->bytes_in, vif->pkt_in,
22741da177e4SLinus Torvalds 			   vif->bytes_out, vif->pkt_out,
22751da177e4SLinus Torvalds 			   vif->flags, vif->local, vif->remote);
22761da177e4SLinus Torvalds 	}
22771da177e4SLinus Torvalds 	return 0;
22781da177e4SLinus Torvalds }
22791da177e4SLinus Torvalds 
2280f690808eSStephen Hemminger static const struct seq_operations ipmr_vif_seq_ops = {
22811da177e4SLinus Torvalds 	.start = ipmr_vif_seq_start,
22821da177e4SLinus Torvalds 	.next  = ipmr_vif_seq_next,
22831da177e4SLinus Torvalds 	.stop  = ipmr_vif_seq_stop,
22841da177e4SLinus Torvalds 	.show  = ipmr_vif_seq_show,
22851da177e4SLinus Torvalds };
22861da177e4SLinus Torvalds 
22871da177e4SLinus Torvalds static int ipmr_vif_open(struct inode *inode, struct file *file)
22881da177e4SLinus Torvalds {
2289f6bb4514SBenjamin Thery 	return seq_open_net(inode, file, &ipmr_vif_seq_ops,
2290cf7732e4SPavel Emelyanov 			    sizeof(struct ipmr_vif_iter));
22911da177e4SLinus Torvalds }
22921da177e4SLinus Torvalds 
22939a32144eSArjan van de Ven static const struct file_operations ipmr_vif_fops = {
22941da177e4SLinus Torvalds 	.owner	 = THIS_MODULE,
22951da177e4SLinus Torvalds 	.open    = ipmr_vif_open,
22961da177e4SLinus Torvalds 	.read    = seq_read,
22971da177e4SLinus Torvalds 	.llseek  = seq_lseek,
2298f6bb4514SBenjamin Thery 	.release = seq_release_net,
22991da177e4SLinus Torvalds };
23001da177e4SLinus Torvalds 
23011da177e4SLinus Torvalds struct ipmr_mfc_iter {
2302f6bb4514SBenjamin Thery 	struct seq_net_private p;
2303f0ad0860SPatrick McHardy 	struct mr_table *mrt;
2304862465f2SPatrick McHardy 	struct list_head *cache;
23051da177e4SLinus Torvalds 	int ct;
23061da177e4SLinus Torvalds };
23071da177e4SLinus Torvalds 
23081da177e4SLinus Torvalds 
2309f6bb4514SBenjamin Thery static struct mfc_cache *ipmr_mfc_seq_idx(struct net *net,
2310f6bb4514SBenjamin Thery 					  struct ipmr_mfc_iter *it, loff_t pos)
23111da177e4SLinus Torvalds {
2312f0ad0860SPatrick McHardy 	struct mr_table *mrt = it->mrt;
23131da177e4SLinus Torvalds 	struct mfc_cache *mfc;
23141da177e4SLinus Torvalds 
2315a8c9486bSEric Dumazet 	rcu_read_lock();
2316862465f2SPatrick McHardy 	for (it->ct = 0; it->ct < MFC_LINES; it->ct++) {
23170c12295aSPatrick McHardy 		it->cache = &mrt->mfc_cache_array[it->ct];
2318a8c9486bSEric Dumazet 		list_for_each_entry_rcu(mfc, it->cache, list)
23191da177e4SLinus Torvalds 			if (pos-- == 0)
23201da177e4SLinus Torvalds 				return mfc;
2321862465f2SPatrick McHardy 	}
2322a8c9486bSEric Dumazet 	rcu_read_unlock();
23231da177e4SLinus Torvalds 
23241da177e4SLinus Torvalds 	spin_lock_bh(&mfc_unres_lock);
23250c12295aSPatrick McHardy 	it->cache = &mrt->mfc_unres_queue;
2326862465f2SPatrick McHardy 	list_for_each_entry(mfc, it->cache, list)
2327e258beb2SPatrick McHardy 		if (pos-- == 0)
23281da177e4SLinus Torvalds 			return mfc;
23291da177e4SLinus Torvalds 	spin_unlock_bh(&mfc_unres_lock);
23301da177e4SLinus Torvalds 
23311da177e4SLinus Torvalds 	it->cache = NULL;
23321da177e4SLinus Torvalds 	return NULL;
23331da177e4SLinus Torvalds }
23341da177e4SLinus Torvalds 
23351da177e4SLinus Torvalds 
23361da177e4SLinus Torvalds static void *ipmr_mfc_seq_start(struct seq_file *seq, loff_t *pos)
23371da177e4SLinus Torvalds {
23381da177e4SLinus Torvalds 	struct ipmr_mfc_iter *it = seq->private;
2339f6bb4514SBenjamin Thery 	struct net *net = seq_file_net(seq);
2340f0ad0860SPatrick McHardy 	struct mr_table *mrt;
2341f6bb4514SBenjamin Thery 
2342f0ad0860SPatrick McHardy 	mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
2343f0ad0860SPatrick McHardy 	if (mrt == NULL)
2344f0ad0860SPatrick McHardy 		return ERR_PTR(-ENOENT);
2345f0ad0860SPatrick McHardy 
2346f0ad0860SPatrick McHardy 	it->mrt = mrt;
23471da177e4SLinus Torvalds 	it->cache = NULL;
23481da177e4SLinus Torvalds 	it->ct = 0;
2349f6bb4514SBenjamin Thery 	return *pos ? ipmr_mfc_seq_idx(net, seq->private, *pos - 1)
23501da177e4SLinus Torvalds 		: SEQ_START_TOKEN;
23511da177e4SLinus Torvalds }
23521da177e4SLinus Torvalds 
23531da177e4SLinus Torvalds static void *ipmr_mfc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
23541da177e4SLinus Torvalds {
23551da177e4SLinus Torvalds 	struct mfc_cache *mfc = v;
23561da177e4SLinus Torvalds 	struct ipmr_mfc_iter *it = seq->private;
2357f6bb4514SBenjamin Thery 	struct net *net = seq_file_net(seq);
2358f0ad0860SPatrick McHardy 	struct mr_table *mrt = it->mrt;
23591da177e4SLinus Torvalds 
23601da177e4SLinus Torvalds 	++*pos;
23611da177e4SLinus Torvalds 
23621da177e4SLinus Torvalds 	if (v == SEQ_START_TOKEN)
2363f6bb4514SBenjamin Thery 		return ipmr_mfc_seq_idx(net, seq->private, 0);
23641da177e4SLinus Torvalds 
2365862465f2SPatrick McHardy 	if (mfc->list.next != it->cache)
2366862465f2SPatrick McHardy 		return list_entry(mfc->list.next, struct mfc_cache, list);
23671da177e4SLinus Torvalds 
23680c12295aSPatrick McHardy 	if (it->cache == &mrt->mfc_unres_queue)
23691da177e4SLinus Torvalds 		goto end_of_list;
23701da177e4SLinus Torvalds 
23710c12295aSPatrick McHardy 	BUG_ON(it->cache != &mrt->mfc_cache_array[it->ct]);
23721da177e4SLinus Torvalds 
23731da177e4SLinus Torvalds 	while (++it->ct < MFC_LINES) {
23740c12295aSPatrick McHardy 		it->cache = &mrt->mfc_cache_array[it->ct];
2375862465f2SPatrick McHardy 		if (list_empty(it->cache))
2376862465f2SPatrick McHardy 			continue;
2377862465f2SPatrick McHardy 		return list_first_entry(it->cache, struct mfc_cache, list);
23781da177e4SLinus Torvalds 	}
23791da177e4SLinus Torvalds 
23801da177e4SLinus Torvalds 	/* exhausted cache_array, show unresolved */
2381a8c9486bSEric Dumazet 	rcu_read_unlock();
23820c12295aSPatrick McHardy 	it->cache = &mrt->mfc_unres_queue;
23831da177e4SLinus Torvalds 	it->ct = 0;
23841da177e4SLinus Torvalds 
23851da177e4SLinus Torvalds 	spin_lock_bh(&mfc_unres_lock);
2386862465f2SPatrick McHardy 	if (!list_empty(it->cache))
2387862465f2SPatrick McHardy 		return list_first_entry(it->cache, struct mfc_cache, list);
23881da177e4SLinus Torvalds 
23891da177e4SLinus Torvalds end_of_list:
23901da177e4SLinus Torvalds 	spin_unlock_bh(&mfc_unres_lock);
23911da177e4SLinus Torvalds 	it->cache = NULL;
23921da177e4SLinus Torvalds 
23931da177e4SLinus Torvalds 	return NULL;
23941da177e4SLinus Torvalds }
23951da177e4SLinus Torvalds 
23961da177e4SLinus Torvalds static void ipmr_mfc_seq_stop(struct seq_file *seq, void *v)
23971da177e4SLinus Torvalds {
23981da177e4SLinus Torvalds 	struct ipmr_mfc_iter *it = seq->private;
2399f0ad0860SPatrick McHardy 	struct mr_table *mrt = it->mrt;
24001da177e4SLinus Torvalds 
24010c12295aSPatrick McHardy 	if (it->cache == &mrt->mfc_unres_queue)
24021da177e4SLinus Torvalds 		spin_unlock_bh(&mfc_unres_lock);
24030c12295aSPatrick McHardy 	else if (it->cache == &mrt->mfc_cache_array[it->ct])
2404a8c9486bSEric Dumazet 		rcu_read_unlock();
24051da177e4SLinus Torvalds }
24061da177e4SLinus Torvalds 
24071da177e4SLinus Torvalds static int ipmr_mfc_seq_show(struct seq_file *seq, void *v)
24081da177e4SLinus Torvalds {
24091da177e4SLinus Torvalds 	int n;
24101da177e4SLinus Torvalds 
24111da177e4SLinus Torvalds 	if (v == SEQ_START_TOKEN) {
24121da177e4SLinus Torvalds 		seq_puts(seq,
24131da177e4SLinus Torvalds 		 "Group    Origin   Iif     Pkts    Bytes    Wrong Oifs\n");
24141da177e4SLinus Torvalds 	} else {
24151da177e4SLinus Torvalds 		const struct mfc_cache *mfc = v;
24161da177e4SLinus Torvalds 		const struct ipmr_mfc_iter *it = seq->private;
2417f0ad0860SPatrick McHardy 		const struct mr_table *mrt = it->mrt;
24181da177e4SLinus Torvalds 
24190eae88f3SEric Dumazet 		seq_printf(seq, "%08X %08X %-3hd",
24200eae88f3SEric Dumazet 			   (__force u32) mfc->mfc_mcastgrp,
24210eae88f3SEric Dumazet 			   (__force u32) mfc->mfc_origin,
24221ea472e2SBenjamin Thery 			   mfc->mfc_parent);
24231ea472e2SBenjamin Thery 
24240c12295aSPatrick McHardy 		if (it->cache != &mrt->mfc_unres_queue) {
24251ea472e2SBenjamin Thery 			seq_printf(seq, " %8lu %8lu %8lu",
24261da177e4SLinus Torvalds 				   mfc->mfc_un.res.pkt,
24271da177e4SLinus Torvalds 				   mfc->mfc_un.res.bytes,
24281da177e4SLinus Torvalds 				   mfc->mfc_un.res.wrong_if);
24291da177e4SLinus Torvalds 			for (n = mfc->mfc_un.res.minvif;
24301da177e4SLinus Torvalds 			     n < mfc->mfc_un.res.maxvif; n++) {
24310c12295aSPatrick McHardy 				if (VIF_EXISTS(mrt, n) &&
2432cf958ae3SBenjamin Thery 				    mfc->mfc_un.res.ttls[n] < 255)
24331da177e4SLinus Torvalds 					seq_printf(seq,
24341da177e4SLinus Torvalds 					   " %2d:%-3d",
24351da177e4SLinus Torvalds 					   n, mfc->mfc_un.res.ttls[n]);
24361da177e4SLinus Torvalds 			}
24371ea472e2SBenjamin Thery 		} else {
24381ea472e2SBenjamin Thery 			/* unresolved mfc_caches don't contain
24391ea472e2SBenjamin Thery 			 * pkt, bytes and wrong_if values
24401ea472e2SBenjamin Thery 			 */
24411ea472e2SBenjamin Thery 			seq_printf(seq, " %8lu %8lu %8lu", 0ul, 0ul, 0ul);
24421da177e4SLinus Torvalds 		}
24431da177e4SLinus Torvalds 		seq_putc(seq, '\n');
24441da177e4SLinus Torvalds 	}
24451da177e4SLinus Torvalds 	return 0;
24461da177e4SLinus Torvalds }
24471da177e4SLinus Torvalds 
2448f690808eSStephen Hemminger static const struct seq_operations ipmr_mfc_seq_ops = {
24491da177e4SLinus Torvalds 	.start = ipmr_mfc_seq_start,
24501da177e4SLinus Torvalds 	.next  = ipmr_mfc_seq_next,
24511da177e4SLinus Torvalds 	.stop  = ipmr_mfc_seq_stop,
24521da177e4SLinus Torvalds 	.show  = ipmr_mfc_seq_show,
24531da177e4SLinus Torvalds };
24541da177e4SLinus Torvalds 
24551da177e4SLinus Torvalds static int ipmr_mfc_open(struct inode *inode, struct file *file)
24561da177e4SLinus Torvalds {
2457f6bb4514SBenjamin Thery 	return seq_open_net(inode, file, &ipmr_mfc_seq_ops,
2458cf7732e4SPavel Emelyanov 			    sizeof(struct ipmr_mfc_iter));
24591da177e4SLinus Torvalds }
24601da177e4SLinus Torvalds 
24619a32144eSArjan van de Ven static const struct file_operations ipmr_mfc_fops = {
24621da177e4SLinus Torvalds 	.owner	 = THIS_MODULE,
24631da177e4SLinus Torvalds 	.open    = ipmr_mfc_open,
24641da177e4SLinus Torvalds 	.read    = seq_read,
24651da177e4SLinus Torvalds 	.llseek  = seq_lseek,
2466f6bb4514SBenjamin Thery 	.release = seq_release_net,
24671da177e4SLinus Torvalds };
24681da177e4SLinus Torvalds #endif
24691da177e4SLinus Torvalds 
24701da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM_V2
247132613090SAlexey Dobriyan static const struct net_protocol pim_protocol = {
24721da177e4SLinus Torvalds 	.handler	=	pim_rcv,
2473403dbb97STom Goff 	.netns_ok	=	1,
24741da177e4SLinus Torvalds };
24751da177e4SLinus Torvalds #endif
24761da177e4SLinus Torvalds 
24771da177e4SLinus Torvalds 
24781da177e4SLinus Torvalds /*
24791da177e4SLinus Torvalds  *	Setup for IP multicast routing
24801da177e4SLinus Torvalds  */
2481cf958ae3SBenjamin Thery static int __net_init ipmr_net_init(struct net *net)
2482cf958ae3SBenjamin Thery {
2483f0ad0860SPatrick McHardy 	int err;
2484cf958ae3SBenjamin Thery 
2485f0ad0860SPatrick McHardy 	err = ipmr_rules_init(net);
2486f0ad0860SPatrick McHardy 	if (err < 0)
2487cf958ae3SBenjamin Thery 		goto fail;
2488f6bb4514SBenjamin Thery 
2489f6bb4514SBenjamin Thery #ifdef CONFIG_PROC_FS
2490f6bb4514SBenjamin Thery 	err = -ENOMEM;
2491f6bb4514SBenjamin Thery 	if (!proc_net_fops_create(net, "ip_mr_vif", 0, &ipmr_vif_fops))
2492f6bb4514SBenjamin Thery 		goto proc_vif_fail;
2493f6bb4514SBenjamin Thery 	if (!proc_net_fops_create(net, "ip_mr_cache", 0, &ipmr_mfc_fops))
2494f6bb4514SBenjamin Thery 		goto proc_cache_fail;
2495f6bb4514SBenjamin Thery #endif
24962bb8b26cSBenjamin Thery 	return 0;
24972bb8b26cSBenjamin Thery 
2498f6bb4514SBenjamin Thery #ifdef CONFIG_PROC_FS
2499f6bb4514SBenjamin Thery proc_cache_fail:
2500f6bb4514SBenjamin Thery 	proc_net_remove(net, "ip_mr_vif");
2501f6bb4514SBenjamin Thery proc_vif_fail:
2502f0ad0860SPatrick McHardy 	ipmr_rules_exit(net);
2503f6bb4514SBenjamin Thery #endif
2504cf958ae3SBenjamin Thery fail:
2505cf958ae3SBenjamin Thery 	return err;
2506cf958ae3SBenjamin Thery }
2507cf958ae3SBenjamin Thery 
2508cf958ae3SBenjamin Thery static void __net_exit ipmr_net_exit(struct net *net)
2509cf958ae3SBenjamin Thery {
2510f6bb4514SBenjamin Thery #ifdef CONFIG_PROC_FS
2511f6bb4514SBenjamin Thery 	proc_net_remove(net, "ip_mr_cache");
2512f6bb4514SBenjamin Thery 	proc_net_remove(net, "ip_mr_vif");
2513f6bb4514SBenjamin Thery #endif
2514f0ad0860SPatrick McHardy 	ipmr_rules_exit(net);
2515cf958ae3SBenjamin Thery }
2516cf958ae3SBenjamin Thery 
2517cf958ae3SBenjamin Thery static struct pernet_operations ipmr_net_ops = {
2518cf958ae3SBenjamin Thery 	.init = ipmr_net_init,
2519cf958ae3SBenjamin Thery 	.exit = ipmr_net_exit,
2520cf958ae3SBenjamin Thery };
25211da177e4SLinus Torvalds 
252203d2f897SWang Chen int __init ip_mr_init(void)
25231da177e4SLinus Torvalds {
252403d2f897SWang Chen 	int err;
252503d2f897SWang Chen 
25261da177e4SLinus Torvalds 	mrt_cachep = kmem_cache_create("ip_mrt_cache",
25271da177e4SLinus Torvalds 				       sizeof(struct mfc_cache),
2528e5d679f3SAlexey Dobriyan 				       0, SLAB_HWCACHE_ALIGN | SLAB_PANIC,
252920c2df83SPaul Mundt 				       NULL);
253003d2f897SWang Chen 	if (!mrt_cachep)
253103d2f897SWang Chen 		return -ENOMEM;
253203d2f897SWang Chen 
2533cf958ae3SBenjamin Thery 	err = register_pernet_subsys(&ipmr_net_ops);
2534cf958ae3SBenjamin Thery 	if (err)
2535cf958ae3SBenjamin Thery 		goto reg_pernet_fail;
2536cf958ae3SBenjamin Thery 
253703d2f897SWang Chen 	err = register_netdevice_notifier(&ip_mr_notifier);
253803d2f897SWang Chen 	if (err)
253903d2f897SWang Chen 		goto reg_notif_fail;
2540403dbb97STom Goff #ifdef CONFIG_IP_PIMSM_V2
2541403dbb97STom Goff 	if (inet_add_protocol(&pim_protocol, IPPROTO_PIM) < 0) {
2542403dbb97STom Goff 		printk(KERN_ERR "ip_mr_init: can't add PIM protocol\n");
2543403dbb97STom Goff 		err = -EAGAIN;
2544403dbb97STom Goff 		goto add_proto_fail;
2545403dbb97STom Goff 	}
2546403dbb97STom Goff #endif
2547c7ac8679SGreg Rose 	rtnl_register(RTNL_FAMILY_IPMR, RTM_GETROUTE,
2548c7ac8679SGreg Rose 		      NULL, ipmr_rtm_dumproute, NULL);
254903d2f897SWang Chen 	return 0;
2550f6bb4514SBenjamin Thery 
2551403dbb97STom Goff #ifdef CONFIG_IP_PIMSM_V2
2552403dbb97STom Goff add_proto_fail:
2553403dbb97STom Goff 	unregister_netdevice_notifier(&ip_mr_notifier);
2554403dbb97STom Goff #endif
2555c3e38896SBenjamin Thery reg_notif_fail:
2556cf958ae3SBenjamin Thery 	unregister_pernet_subsys(&ipmr_net_ops);
2557cf958ae3SBenjamin Thery reg_pernet_fail:
2558c3e38896SBenjamin Thery 	kmem_cache_destroy(mrt_cachep);
255903d2f897SWang Chen 	return err;
25601da177e4SLinus Torvalds }
2561