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