xref: /linux/net/ipv4/ipmr.c (revision 5bc3eb7e2f0026f246d939851109df99e8e9f64a)
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
251da177e4SLinus Torvalds  *					Relax this requrement 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>
50457c4cbcSEric W. Biederman #include <net/net_namespace.h>
511da177e4SLinus Torvalds #include <net/ip.h>
521da177e4SLinus Torvalds #include <net/protocol.h>
531da177e4SLinus Torvalds #include <linux/skbuff.h>
5414c85021SArnaldo Carvalho de Melo #include <net/route.h>
551da177e4SLinus Torvalds #include <net/sock.h>
561da177e4SLinus Torvalds #include <net/icmp.h>
571da177e4SLinus Torvalds #include <net/udp.h>
581da177e4SLinus Torvalds #include <net/raw.h>
591da177e4SLinus Torvalds #include <linux/notifier.h>
601da177e4SLinus Torvalds #include <linux/if_arp.h>
611da177e4SLinus Torvalds #include <linux/netfilter_ipv4.h>
621da177e4SLinus Torvalds #include <net/ipip.h>
631da177e4SLinus Torvalds #include <net/checksum.h>
64dc5fc579SArnaldo Carvalho de Melo #include <net/netlink.h>
651da177e4SLinus Torvalds 
661da177e4SLinus Torvalds #if defined(CONFIG_IP_PIMSM_V1) || defined(CONFIG_IP_PIMSM_V2)
671da177e4SLinus Torvalds #define CONFIG_IP_PIMSM	1
681da177e4SLinus Torvalds #endif
691da177e4SLinus Torvalds 
701da177e4SLinus Torvalds static struct sock *mroute_socket;
711da177e4SLinus Torvalds 
721da177e4SLinus Torvalds 
731da177e4SLinus Torvalds /* Big lock, protecting vif table, mrt cache and mroute socket state.
741da177e4SLinus Torvalds    Note that the changes are semaphored via rtnl_lock.
751da177e4SLinus Torvalds  */
761da177e4SLinus Torvalds 
771da177e4SLinus Torvalds static DEFINE_RWLOCK(mrt_lock);
781da177e4SLinus Torvalds 
791da177e4SLinus Torvalds /*
801da177e4SLinus Torvalds  *	Multicast router control variables
811da177e4SLinus Torvalds  */
821da177e4SLinus Torvalds 
831da177e4SLinus Torvalds static struct vif_device vif_table[MAXVIFS];		/* Devices 		*/
841da177e4SLinus Torvalds static int maxvif;
851da177e4SLinus Torvalds 
861da177e4SLinus Torvalds #define VIF_EXISTS(idx) (vif_table[idx].dev != NULL)
871da177e4SLinus Torvalds 
881da177e4SLinus Torvalds static int mroute_do_assert;				/* Set in PIM assert	*/
891da177e4SLinus Torvalds static int mroute_do_pim;
901da177e4SLinus Torvalds 
911da177e4SLinus Torvalds static struct mfc_cache *mfc_cache_array[MFC_LINES];	/* Forwarding cache	*/
921da177e4SLinus Torvalds 
931da177e4SLinus Torvalds static struct mfc_cache *mfc_unres_queue;		/* Queue of unresolved entries */
941da177e4SLinus Torvalds static atomic_t cache_resolve_queue_len;		/* Size of unresolved	*/
951da177e4SLinus Torvalds 
961da177e4SLinus Torvalds /* Special spinlock for queue of unresolved entries */
971da177e4SLinus Torvalds static DEFINE_SPINLOCK(mfc_unres_lock);
981da177e4SLinus Torvalds 
991da177e4SLinus Torvalds /* We return to original Alan's scheme. Hash table of resolved
1001da177e4SLinus Torvalds    entries is changed only in process context and protected
1011da177e4SLinus Torvalds    with weak lock mrt_lock. Queue of unresolved entries is protected
1021da177e4SLinus Torvalds    with strong spinlock mfc_unres_lock.
1031da177e4SLinus Torvalds 
1041da177e4SLinus Torvalds    In this case data path is free of exclusive locks at all.
1051da177e4SLinus Torvalds  */
1061da177e4SLinus Torvalds 
107e18b890bSChristoph Lameter static struct kmem_cache *mrt_cachep __read_mostly;
1081da177e4SLinus Torvalds 
1091da177e4SLinus Torvalds static int ip_mr_forward(struct sk_buff *skb, struct mfc_cache *cache, int local);
1101da177e4SLinus Torvalds static int ipmr_cache_report(struct sk_buff *pkt, vifi_t vifi, int assert);
1111da177e4SLinus Torvalds static int ipmr_fill_mroute(struct sk_buff *skb, struct mfc_cache *c, struct rtmsg *rtm);
1121da177e4SLinus Torvalds 
1131da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM_V2
1141da177e4SLinus Torvalds static struct net_protocol pim_protocol;
1151da177e4SLinus Torvalds #endif
1161da177e4SLinus Torvalds 
1171da177e4SLinus Torvalds static struct timer_list ipmr_expire_timer;
1181da177e4SLinus Torvalds 
1191da177e4SLinus Torvalds /* Service routines creating virtual interfaces: DVMRP tunnels and PIMREG */
1201da177e4SLinus Torvalds 
121d607032dSWang Chen static void ipmr_del_tunnel(struct net_device *dev, struct vifctl *v)
122d607032dSWang Chen {
123d607032dSWang Chen 	dev_close(dev);
124d607032dSWang Chen 
125d607032dSWang Chen 	dev = __dev_get_by_name(&init_net, "tunl0");
126d607032dSWang Chen 	if (dev) {
127*5bc3eb7eSStephen Hemminger 		const struct net_device_ops *ops = dev->netdev_ops;
128d607032dSWang Chen 		struct ifreq ifr;
129d607032dSWang Chen 		struct ip_tunnel_parm p;
130d607032dSWang Chen 
131d607032dSWang Chen 		memset(&p, 0, sizeof(p));
132d607032dSWang Chen 		p.iph.daddr = v->vifc_rmt_addr.s_addr;
133d607032dSWang Chen 		p.iph.saddr = v->vifc_lcl_addr.s_addr;
134d607032dSWang Chen 		p.iph.version = 4;
135d607032dSWang Chen 		p.iph.ihl = 5;
136d607032dSWang Chen 		p.iph.protocol = IPPROTO_IPIP;
137d607032dSWang Chen 		sprintf(p.name, "dvmrp%d", v->vifc_vifi);
138d607032dSWang Chen 		ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
139d607032dSWang Chen 
140*5bc3eb7eSStephen Hemminger 		if (ops->ndo_do_ioctl) {
141*5bc3eb7eSStephen Hemminger 			mm_segment_t oldfs = get_fs();
142*5bc3eb7eSStephen Hemminger 
143*5bc3eb7eSStephen Hemminger 			set_fs(KERNEL_DS);
144*5bc3eb7eSStephen Hemminger 			ops->ndo_do_ioctl(dev, &ifr, SIOCDELTUNNEL);
145d607032dSWang Chen 			set_fs(oldfs);
146d607032dSWang Chen 		}
147d607032dSWang Chen 	}
148*5bc3eb7eSStephen Hemminger }
149d607032dSWang Chen 
1501da177e4SLinus Torvalds static
1511da177e4SLinus Torvalds struct net_device *ipmr_new_tunnel(struct vifctl *v)
1521da177e4SLinus Torvalds {
1531da177e4SLinus Torvalds 	struct net_device  *dev;
1541da177e4SLinus Torvalds 
155881d966bSEric W. Biederman 	dev = __dev_get_by_name(&init_net, "tunl0");
1561da177e4SLinus Torvalds 
1571da177e4SLinus Torvalds 	if (dev) {
158*5bc3eb7eSStephen Hemminger 		const struct net_device_ops *ops = dev->netdev_ops;
1591da177e4SLinus Torvalds 		int err;
1601da177e4SLinus Torvalds 		struct ifreq ifr;
1611da177e4SLinus Torvalds 		struct ip_tunnel_parm p;
1621da177e4SLinus Torvalds 		struct in_device  *in_dev;
1631da177e4SLinus Torvalds 
1641da177e4SLinus Torvalds 		memset(&p, 0, sizeof(p));
1651da177e4SLinus Torvalds 		p.iph.daddr = v->vifc_rmt_addr.s_addr;
1661da177e4SLinus Torvalds 		p.iph.saddr = v->vifc_lcl_addr.s_addr;
1671da177e4SLinus Torvalds 		p.iph.version = 4;
1681da177e4SLinus Torvalds 		p.iph.ihl = 5;
1691da177e4SLinus Torvalds 		p.iph.protocol = IPPROTO_IPIP;
1701da177e4SLinus Torvalds 		sprintf(p.name, "dvmrp%d", v->vifc_vifi);
171ba93ef74SStephen Hemminger 		ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
1721da177e4SLinus Torvalds 
173*5bc3eb7eSStephen Hemminger 		if (ops->ndo_do_ioctl) {
174*5bc3eb7eSStephen Hemminger 			mm_segment_t oldfs = get_fs();
175*5bc3eb7eSStephen Hemminger 
176*5bc3eb7eSStephen Hemminger 			set_fs(KERNEL_DS);
177*5bc3eb7eSStephen Hemminger 			err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
1781da177e4SLinus Torvalds 			set_fs(oldfs);
179*5bc3eb7eSStephen Hemminger 		} else
180*5bc3eb7eSStephen Hemminger 			err = -EOPNOTSUPP;
1811da177e4SLinus Torvalds 
1821da177e4SLinus Torvalds 		dev = NULL;
1831da177e4SLinus Torvalds 
184881d966bSEric W. Biederman 		if (err == 0 && (dev = __dev_get_by_name(&init_net, p.name)) != NULL) {
1851da177e4SLinus Torvalds 			dev->flags |= IFF_MULTICAST;
1861da177e4SLinus Torvalds 
187e5ed6399SHerbert Xu 			in_dev = __in_dev_get_rtnl(dev);
18871e27da9SHerbert Xu 			if (in_dev == NULL)
1891da177e4SLinus Torvalds 				goto failure;
19071e27da9SHerbert Xu 
19171e27da9SHerbert Xu 			ipv4_devconf_setall(in_dev);
19271e27da9SHerbert Xu 			IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0;
1931da177e4SLinus Torvalds 
1941da177e4SLinus Torvalds 			if (dev_open(dev))
1951da177e4SLinus Torvalds 				goto failure;
1967dc00c82SWang Chen 			dev_hold(dev);
1971da177e4SLinus Torvalds 		}
1981da177e4SLinus Torvalds 	}
1991da177e4SLinus Torvalds 	return dev;
2001da177e4SLinus Torvalds 
2011da177e4SLinus Torvalds failure:
2021da177e4SLinus Torvalds 	/* allow the register to be completed before unregistering. */
2031da177e4SLinus Torvalds 	rtnl_unlock();
2041da177e4SLinus Torvalds 	rtnl_lock();
2051da177e4SLinus Torvalds 
2061da177e4SLinus Torvalds 	unregister_netdevice(dev);
2071da177e4SLinus Torvalds 	return NULL;
2081da177e4SLinus Torvalds }
2091da177e4SLinus Torvalds 
2101da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM
2111da177e4SLinus Torvalds 
2121da177e4SLinus Torvalds static int reg_vif_num = -1;
2131da177e4SLinus Torvalds 
2141da177e4SLinus Torvalds static int reg_vif_xmit(struct sk_buff *skb, struct net_device *dev)
2151da177e4SLinus Torvalds {
2161da177e4SLinus Torvalds 	read_lock(&mrt_lock);
217cf3677aeSPavel Emelyanov 	dev->stats.tx_bytes += skb->len;
218cf3677aeSPavel Emelyanov 	dev->stats.tx_packets++;
2191da177e4SLinus Torvalds 	ipmr_cache_report(skb, reg_vif_num, IGMPMSG_WHOLEPKT);
2201da177e4SLinus Torvalds 	read_unlock(&mrt_lock);
2211da177e4SLinus Torvalds 	kfree_skb(skb);
2221da177e4SLinus Torvalds 	return 0;
2231da177e4SLinus Torvalds }
2241da177e4SLinus Torvalds 
2251da177e4SLinus Torvalds static void reg_vif_setup(struct net_device *dev)
2261da177e4SLinus Torvalds {
2271da177e4SLinus Torvalds 	dev->type		= ARPHRD_PIMREG;
22846f25dffSKris Katterjohn 	dev->mtu		= ETH_DATA_LEN - sizeof(struct iphdr) - 8;
2291da177e4SLinus Torvalds 	dev->flags		= IFF_NOARP;
2301da177e4SLinus Torvalds 	dev->hard_start_xmit	= reg_vif_xmit;
2311da177e4SLinus Torvalds 	dev->destructor		= free_netdev;
2321da177e4SLinus Torvalds }
2331da177e4SLinus Torvalds 
2341da177e4SLinus Torvalds static struct net_device *ipmr_reg_vif(void)
2351da177e4SLinus Torvalds {
2361da177e4SLinus Torvalds 	struct net_device *dev;
2371da177e4SLinus Torvalds 	struct in_device *in_dev;
2381da177e4SLinus Torvalds 
239cf3677aeSPavel Emelyanov 	dev = alloc_netdev(0, "pimreg", reg_vif_setup);
2401da177e4SLinus Torvalds 
2411da177e4SLinus Torvalds 	if (dev == NULL)
2421da177e4SLinus Torvalds 		return NULL;
2431da177e4SLinus Torvalds 
2441da177e4SLinus Torvalds 	if (register_netdevice(dev)) {
2451da177e4SLinus Torvalds 		free_netdev(dev);
2461da177e4SLinus Torvalds 		return NULL;
2471da177e4SLinus Torvalds 	}
2481da177e4SLinus Torvalds 	dev->iflink = 0;
2491da177e4SLinus Torvalds 
25071e27da9SHerbert Xu 	rcu_read_lock();
25171e27da9SHerbert Xu 	if ((in_dev = __in_dev_get_rcu(dev)) == NULL) {
25271e27da9SHerbert Xu 		rcu_read_unlock();
2531da177e4SLinus Torvalds 		goto failure;
25471e27da9SHerbert Xu 	}
2551da177e4SLinus Torvalds 
25671e27da9SHerbert Xu 	ipv4_devconf_setall(in_dev);
25771e27da9SHerbert Xu 	IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0;
25871e27da9SHerbert Xu 	rcu_read_unlock();
2591da177e4SLinus Torvalds 
2601da177e4SLinus Torvalds 	if (dev_open(dev))
2611da177e4SLinus Torvalds 		goto failure;
2621da177e4SLinus Torvalds 
2637dc00c82SWang Chen 	dev_hold(dev);
2647dc00c82SWang Chen 
2651da177e4SLinus Torvalds 	return dev;
2661da177e4SLinus Torvalds 
2671da177e4SLinus Torvalds failure:
2681da177e4SLinus Torvalds 	/* allow the register to be completed before unregistering. */
2691da177e4SLinus Torvalds 	rtnl_unlock();
2701da177e4SLinus Torvalds 	rtnl_lock();
2711da177e4SLinus Torvalds 
2721da177e4SLinus Torvalds 	unregister_netdevice(dev);
2731da177e4SLinus Torvalds 	return NULL;
2741da177e4SLinus Torvalds }
2751da177e4SLinus Torvalds #endif
2761da177e4SLinus Torvalds 
2771da177e4SLinus Torvalds /*
2781da177e4SLinus Torvalds  *	Delete a VIF entry
2797dc00c82SWang Chen  *	@notify: Set to 1, if the caller is a notifier_call
2801da177e4SLinus Torvalds  */
2811da177e4SLinus Torvalds 
2827dc00c82SWang Chen static int vif_delete(int vifi, int notify)
2831da177e4SLinus Torvalds {
2841da177e4SLinus Torvalds 	struct vif_device *v;
2851da177e4SLinus Torvalds 	struct net_device *dev;
2861da177e4SLinus Torvalds 	struct in_device *in_dev;
2871da177e4SLinus Torvalds 
2881da177e4SLinus Torvalds 	if (vifi < 0 || vifi >= maxvif)
2891da177e4SLinus Torvalds 		return -EADDRNOTAVAIL;
2901da177e4SLinus Torvalds 
2911da177e4SLinus Torvalds 	v = &vif_table[vifi];
2921da177e4SLinus Torvalds 
2931da177e4SLinus Torvalds 	write_lock_bh(&mrt_lock);
2941da177e4SLinus Torvalds 	dev = v->dev;
2951da177e4SLinus Torvalds 	v->dev = NULL;
2961da177e4SLinus Torvalds 
2971da177e4SLinus Torvalds 	if (!dev) {
2981da177e4SLinus Torvalds 		write_unlock_bh(&mrt_lock);
2991da177e4SLinus Torvalds 		return -EADDRNOTAVAIL;
3001da177e4SLinus Torvalds 	}
3011da177e4SLinus Torvalds 
3021da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM
3031da177e4SLinus Torvalds 	if (vifi == reg_vif_num)
3041da177e4SLinus Torvalds 		reg_vif_num = -1;
3051da177e4SLinus Torvalds #endif
3061da177e4SLinus Torvalds 
3071da177e4SLinus Torvalds 	if (vifi+1 == maxvif) {
3081da177e4SLinus Torvalds 		int tmp;
3091da177e4SLinus Torvalds 		for (tmp=vifi-1; tmp>=0; tmp--) {
3101da177e4SLinus Torvalds 			if (VIF_EXISTS(tmp))
3111da177e4SLinus Torvalds 				break;
3121da177e4SLinus Torvalds 		}
3131da177e4SLinus Torvalds 		maxvif = tmp+1;
3141da177e4SLinus Torvalds 	}
3151da177e4SLinus Torvalds 
3161da177e4SLinus Torvalds 	write_unlock_bh(&mrt_lock);
3171da177e4SLinus Torvalds 
3181da177e4SLinus Torvalds 	dev_set_allmulti(dev, -1);
3191da177e4SLinus Torvalds 
320e5ed6399SHerbert Xu 	if ((in_dev = __in_dev_get_rtnl(dev)) != NULL) {
32142f811b8SHerbert Xu 		IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)--;
3221da177e4SLinus Torvalds 		ip_rt_multicast_event(in_dev);
3231da177e4SLinus Torvalds 	}
3241da177e4SLinus Torvalds 
3257dc00c82SWang Chen 	if (v->flags&(VIFF_TUNNEL|VIFF_REGISTER) && !notify)
3261da177e4SLinus Torvalds 		unregister_netdevice(dev);
3271da177e4SLinus Torvalds 
3281da177e4SLinus Torvalds 	dev_put(dev);
3291da177e4SLinus Torvalds 	return 0;
3301da177e4SLinus Torvalds }
3311da177e4SLinus Torvalds 
3321da177e4SLinus Torvalds /* Destroy an unresolved cache entry, killing queued skbs
3331da177e4SLinus Torvalds    and reporting error to netlink readers.
3341da177e4SLinus Torvalds  */
3351da177e4SLinus Torvalds 
3361da177e4SLinus Torvalds static void ipmr_destroy_unres(struct mfc_cache *c)
3371da177e4SLinus Torvalds {
3381da177e4SLinus Torvalds 	struct sk_buff *skb;
3399ef1d4c7SPatrick McHardy 	struct nlmsgerr *e;
3401da177e4SLinus Torvalds 
3411da177e4SLinus Torvalds 	atomic_dec(&cache_resolve_queue_len);
3421da177e4SLinus Torvalds 
3431da177e4SLinus Torvalds 	while ((skb = skb_dequeue(&c->mfc_un.unres.unresolved))) {
344eddc9ec5SArnaldo Carvalho de Melo 		if (ip_hdr(skb)->version == 0) {
3451da177e4SLinus Torvalds 			struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr));
3461da177e4SLinus Torvalds 			nlh->nlmsg_type = NLMSG_ERROR;
3471da177e4SLinus Torvalds 			nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct nlmsgerr));
3481da177e4SLinus Torvalds 			skb_trim(skb, nlh->nlmsg_len);
3499ef1d4c7SPatrick McHardy 			e = NLMSG_DATA(nlh);
3509ef1d4c7SPatrick McHardy 			e->error = -ETIMEDOUT;
3519ef1d4c7SPatrick McHardy 			memset(&e->msg, 0, sizeof(e->msg));
3522942e900SThomas Graf 
35397c53cacSDenis V. Lunev 			rtnl_unicast(skb, &init_net, NETLINK_CB(skb).pid);
3541da177e4SLinus Torvalds 		} else
3551da177e4SLinus Torvalds 			kfree_skb(skb);
3561da177e4SLinus Torvalds 	}
3571da177e4SLinus Torvalds 
3581da177e4SLinus Torvalds 	kmem_cache_free(mrt_cachep, c);
3591da177e4SLinus Torvalds }
3601da177e4SLinus Torvalds 
3611da177e4SLinus Torvalds 
3621da177e4SLinus Torvalds /* Single timer process for all the unresolved queue. */
3631da177e4SLinus Torvalds 
3641da177e4SLinus Torvalds static void ipmr_expire_process(unsigned long dummy)
3651da177e4SLinus Torvalds {
3661da177e4SLinus Torvalds 	unsigned long now;
3671da177e4SLinus Torvalds 	unsigned long expires;
3681da177e4SLinus Torvalds 	struct mfc_cache *c, **cp;
3691da177e4SLinus Torvalds 
3701da177e4SLinus Torvalds 	if (!spin_trylock(&mfc_unres_lock)) {
3711da177e4SLinus Torvalds 		mod_timer(&ipmr_expire_timer, jiffies+HZ/10);
3721da177e4SLinus Torvalds 		return;
3731da177e4SLinus Torvalds 	}
3741da177e4SLinus Torvalds 
3751da177e4SLinus Torvalds 	if (atomic_read(&cache_resolve_queue_len) == 0)
3761da177e4SLinus Torvalds 		goto out;
3771da177e4SLinus Torvalds 
3781da177e4SLinus Torvalds 	now = jiffies;
3791da177e4SLinus Torvalds 	expires = 10*HZ;
3801da177e4SLinus Torvalds 	cp = &mfc_unres_queue;
3811da177e4SLinus Torvalds 
3821da177e4SLinus Torvalds 	while ((c=*cp) != NULL) {
3831da177e4SLinus Torvalds 		if (time_after(c->mfc_un.unres.expires, now)) {
3841da177e4SLinus Torvalds 			unsigned long interval = c->mfc_un.unres.expires - now;
3851da177e4SLinus Torvalds 			if (interval < expires)
3861da177e4SLinus Torvalds 				expires = interval;
3871da177e4SLinus Torvalds 			cp = &c->next;
3881da177e4SLinus Torvalds 			continue;
3891da177e4SLinus Torvalds 		}
3901da177e4SLinus Torvalds 
3911da177e4SLinus Torvalds 		*cp = c->next;
3921da177e4SLinus Torvalds 
3931da177e4SLinus Torvalds 		ipmr_destroy_unres(c);
3941da177e4SLinus Torvalds 	}
3951da177e4SLinus Torvalds 
3961da177e4SLinus Torvalds 	if (atomic_read(&cache_resolve_queue_len))
3971da177e4SLinus Torvalds 		mod_timer(&ipmr_expire_timer, jiffies + expires);
3981da177e4SLinus Torvalds 
3991da177e4SLinus Torvalds out:
4001da177e4SLinus Torvalds 	spin_unlock(&mfc_unres_lock);
4011da177e4SLinus Torvalds }
4021da177e4SLinus Torvalds 
4031da177e4SLinus Torvalds /* Fill oifs list. It is called under write locked mrt_lock. */
4041da177e4SLinus Torvalds 
405d1b04c08SBaruch Even static void ipmr_update_thresholds(struct mfc_cache *cache, unsigned char *ttls)
4061da177e4SLinus Torvalds {
4071da177e4SLinus Torvalds 	int vifi;
4081da177e4SLinus Torvalds 
4091da177e4SLinus Torvalds 	cache->mfc_un.res.minvif = MAXVIFS;
4101da177e4SLinus Torvalds 	cache->mfc_un.res.maxvif = 0;
4111da177e4SLinus Torvalds 	memset(cache->mfc_un.res.ttls, 255, MAXVIFS);
4121da177e4SLinus Torvalds 
4131da177e4SLinus Torvalds 	for (vifi=0; vifi<maxvif; vifi++) {
4141da177e4SLinus Torvalds 		if (VIF_EXISTS(vifi) && ttls[vifi] && ttls[vifi] < 255) {
4151da177e4SLinus Torvalds 			cache->mfc_un.res.ttls[vifi] = ttls[vifi];
4161da177e4SLinus Torvalds 			if (cache->mfc_un.res.minvif > vifi)
4171da177e4SLinus Torvalds 				cache->mfc_un.res.minvif = vifi;
4181da177e4SLinus Torvalds 			if (cache->mfc_un.res.maxvif <= vifi)
4191da177e4SLinus Torvalds 				cache->mfc_un.res.maxvif = vifi + 1;
4201da177e4SLinus Torvalds 		}
4211da177e4SLinus Torvalds 	}
4221da177e4SLinus Torvalds }
4231da177e4SLinus Torvalds 
4241da177e4SLinus Torvalds static int vif_add(struct vifctl *vifc, int mrtsock)
4251da177e4SLinus Torvalds {
4261da177e4SLinus Torvalds 	int vifi = vifc->vifc_vifi;
4271da177e4SLinus Torvalds 	struct vif_device *v = &vif_table[vifi];
4281da177e4SLinus Torvalds 	struct net_device *dev;
4291da177e4SLinus Torvalds 	struct in_device *in_dev;
430d607032dSWang Chen 	int err;
4311da177e4SLinus Torvalds 
4321da177e4SLinus Torvalds 	/* Is vif busy ? */
4331da177e4SLinus Torvalds 	if (VIF_EXISTS(vifi))
4341da177e4SLinus Torvalds 		return -EADDRINUSE;
4351da177e4SLinus Torvalds 
4361da177e4SLinus Torvalds 	switch (vifc->vifc_flags) {
4371da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM
4381da177e4SLinus Torvalds 	case VIFF_REGISTER:
4391da177e4SLinus Torvalds 		/*
4401da177e4SLinus Torvalds 		 * Special Purpose VIF in PIM
4411da177e4SLinus Torvalds 		 * All the packets will be sent to the daemon
4421da177e4SLinus Torvalds 		 */
4431da177e4SLinus Torvalds 		if (reg_vif_num >= 0)
4441da177e4SLinus Torvalds 			return -EADDRINUSE;
4451da177e4SLinus Torvalds 		dev = ipmr_reg_vif();
4461da177e4SLinus Torvalds 		if (!dev)
4471da177e4SLinus Torvalds 			return -ENOBUFS;
448d607032dSWang Chen 		err = dev_set_allmulti(dev, 1);
449d607032dSWang Chen 		if (err) {
450d607032dSWang Chen 			unregister_netdevice(dev);
4517dc00c82SWang Chen 			dev_put(dev);
452d607032dSWang Chen 			return err;
453d607032dSWang Chen 		}
4541da177e4SLinus Torvalds 		break;
4551da177e4SLinus Torvalds #endif
4561da177e4SLinus Torvalds 	case VIFF_TUNNEL:
4571da177e4SLinus Torvalds 		dev = ipmr_new_tunnel(vifc);
4581da177e4SLinus Torvalds 		if (!dev)
4591da177e4SLinus Torvalds 			return -ENOBUFS;
460d607032dSWang Chen 		err = dev_set_allmulti(dev, 1);
461d607032dSWang Chen 		if (err) {
462d607032dSWang Chen 			ipmr_del_tunnel(dev, vifc);
4637dc00c82SWang Chen 			dev_put(dev);
464d607032dSWang Chen 			return err;
465d607032dSWang Chen 		}
4661da177e4SLinus Torvalds 		break;
4671da177e4SLinus Torvalds 	case 0:
4681ab35276SDenis V. Lunev 		dev = ip_dev_find(&init_net, vifc->vifc_lcl_addr.s_addr);
4691da177e4SLinus Torvalds 		if (!dev)
4701da177e4SLinus Torvalds 			return -EADDRNOTAVAIL;
471d607032dSWang Chen 		err = dev_set_allmulti(dev, 1);
4727dc00c82SWang Chen 		if (err) {
4737dc00c82SWang Chen 			dev_put(dev);
474d607032dSWang Chen 			return err;
4757dc00c82SWang Chen 		}
4761da177e4SLinus Torvalds 		break;
4771da177e4SLinus Torvalds 	default:
4781da177e4SLinus Torvalds 		return -EINVAL;
4791da177e4SLinus Torvalds 	}
4801da177e4SLinus Torvalds 
481e5ed6399SHerbert Xu 	if ((in_dev = __in_dev_get_rtnl(dev)) == NULL)
4821da177e4SLinus Torvalds 		return -EADDRNOTAVAIL;
48342f811b8SHerbert Xu 	IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)++;
4841da177e4SLinus Torvalds 	ip_rt_multicast_event(in_dev);
4851da177e4SLinus Torvalds 
4861da177e4SLinus Torvalds 	/*
4871da177e4SLinus Torvalds 	 *	Fill in the VIF structures
4881da177e4SLinus Torvalds 	 */
4891da177e4SLinus Torvalds 	v->rate_limit = vifc->vifc_rate_limit;
4901da177e4SLinus Torvalds 	v->local = vifc->vifc_lcl_addr.s_addr;
4911da177e4SLinus Torvalds 	v->remote = vifc->vifc_rmt_addr.s_addr;
4921da177e4SLinus Torvalds 	v->flags = vifc->vifc_flags;
4931da177e4SLinus Torvalds 	if (!mrtsock)
4941da177e4SLinus Torvalds 		v->flags |= VIFF_STATIC;
4951da177e4SLinus Torvalds 	v->threshold = vifc->vifc_threshold;
4961da177e4SLinus Torvalds 	v->bytes_in = 0;
4971da177e4SLinus Torvalds 	v->bytes_out = 0;
4981da177e4SLinus Torvalds 	v->pkt_in = 0;
4991da177e4SLinus Torvalds 	v->pkt_out = 0;
5001da177e4SLinus Torvalds 	v->link = dev->ifindex;
5011da177e4SLinus Torvalds 	if (v->flags&(VIFF_TUNNEL|VIFF_REGISTER))
5021da177e4SLinus Torvalds 		v->link = dev->iflink;
5031da177e4SLinus Torvalds 
5041da177e4SLinus Torvalds 	/* And finish update writing critical data */
5051da177e4SLinus Torvalds 	write_lock_bh(&mrt_lock);
5061da177e4SLinus Torvalds 	v->dev = dev;
5071da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM
5081da177e4SLinus Torvalds 	if (v->flags&VIFF_REGISTER)
5091da177e4SLinus Torvalds 		reg_vif_num = vifi;
5101da177e4SLinus Torvalds #endif
5111da177e4SLinus Torvalds 	if (vifi+1 > maxvif)
5121da177e4SLinus Torvalds 		maxvif = vifi+1;
5131da177e4SLinus Torvalds 	write_unlock_bh(&mrt_lock);
5141da177e4SLinus Torvalds 	return 0;
5151da177e4SLinus Torvalds }
5161da177e4SLinus Torvalds 
517114c7844SAl Viro static struct mfc_cache *ipmr_cache_find(__be32 origin, __be32 mcastgrp)
5181da177e4SLinus Torvalds {
5191da177e4SLinus Torvalds 	int line = MFC_HASH(mcastgrp, origin);
5201da177e4SLinus Torvalds 	struct mfc_cache *c;
5211da177e4SLinus Torvalds 
5221da177e4SLinus Torvalds 	for (c=mfc_cache_array[line]; c; c = c->next) {
5231da177e4SLinus Torvalds 		if (c->mfc_origin==origin && c->mfc_mcastgrp==mcastgrp)
5241da177e4SLinus Torvalds 			break;
5251da177e4SLinus Torvalds 	}
5261da177e4SLinus Torvalds 	return c;
5271da177e4SLinus Torvalds }
5281da177e4SLinus Torvalds 
5291da177e4SLinus Torvalds /*
5301da177e4SLinus Torvalds  *	Allocate a multicast cache entry
5311da177e4SLinus Torvalds  */
5321da177e4SLinus Torvalds static struct mfc_cache *ipmr_cache_alloc(void)
5331da177e4SLinus Torvalds {
534c3762229SRobert P. J. Day 	struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_KERNEL);
5351da177e4SLinus Torvalds 	if (c == NULL)
5361da177e4SLinus Torvalds 		return NULL;
5371da177e4SLinus Torvalds 	c->mfc_un.res.minvif = MAXVIFS;
5381da177e4SLinus Torvalds 	return c;
5391da177e4SLinus Torvalds }
5401da177e4SLinus Torvalds 
5411da177e4SLinus Torvalds static struct mfc_cache *ipmr_cache_alloc_unres(void)
5421da177e4SLinus Torvalds {
543c3762229SRobert P. J. Day 	struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_ATOMIC);
5441da177e4SLinus Torvalds 	if (c == NULL)
5451da177e4SLinus Torvalds 		return NULL;
5461da177e4SLinus Torvalds 	skb_queue_head_init(&c->mfc_un.unres.unresolved);
5471da177e4SLinus Torvalds 	c->mfc_un.unres.expires = jiffies + 10*HZ;
5481da177e4SLinus Torvalds 	return c;
5491da177e4SLinus Torvalds }
5501da177e4SLinus Torvalds 
5511da177e4SLinus Torvalds /*
5521da177e4SLinus Torvalds  *	A cache entry has gone into a resolved state from queued
5531da177e4SLinus Torvalds  */
5541da177e4SLinus Torvalds 
5551da177e4SLinus Torvalds static void ipmr_cache_resolve(struct mfc_cache *uc, struct mfc_cache *c)
5561da177e4SLinus Torvalds {
5571da177e4SLinus Torvalds 	struct sk_buff *skb;
5589ef1d4c7SPatrick McHardy 	struct nlmsgerr *e;
5591da177e4SLinus Torvalds 
5601da177e4SLinus Torvalds 	/*
5611da177e4SLinus Torvalds 	 *	Play the pending entries through our router
5621da177e4SLinus Torvalds 	 */
5631da177e4SLinus Torvalds 
5641da177e4SLinus Torvalds 	while ((skb = __skb_dequeue(&uc->mfc_un.unres.unresolved))) {
565eddc9ec5SArnaldo Carvalho de Melo 		if (ip_hdr(skb)->version == 0) {
5661da177e4SLinus Torvalds 			struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr));
5671da177e4SLinus Torvalds 
5681da177e4SLinus Torvalds 			if (ipmr_fill_mroute(skb, c, NLMSG_DATA(nlh)) > 0) {
56927a884dcSArnaldo Carvalho de Melo 				nlh->nlmsg_len = (skb_tail_pointer(skb) -
57027a884dcSArnaldo Carvalho de Melo 						  (u8 *)nlh);
5711da177e4SLinus Torvalds 			} else {
5721da177e4SLinus Torvalds 				nlh->nlmsg_type = NLMSG_ERROR;
5731da177e4SLinus Torvalds 				nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct nlmsgerr));
5741da177e4SLinus Torvalds 				skb_trim(skb, nlh->nlmsg_len);
5759ef1d4c7SPatrick McHardy 				e = NLMSG_DATA(nlh);
5769ef1d4c7SPatrick McHardy 				e->error = -EMSGSIZE;
5779ef1d4c7SPatrick McHardy 				memset(&e->msg, 0, sizeof(e->msg));
5781da177e4SLinus Torvalds 			}
5792942e900SThomas Graf 
58097c53cacSDenis V. Lunev 			rtnl_unicast(skb, &init_net, NETLINK_CB(skb).pid);
5811da177e4SLinus Torvalds 		} else
5821da177e4SLinus Torvalds 			ip_mr_forward(skb, c, 0);
5831da177e4SLinus Torvalds 	}
5841da177e4SLinus Torvalds }
5851da177e4SLinus Torvalds 
5861da177e4SLinus Torvalds /*
5871da177e4SLinus Torvalds  *	Bounce a cache query up to mrouted. We could use netlink for this but mrouted
5881da177e4SLinus Torvalds  *	expects the following bizarre scheme.
5891da177e4SLinus Torvalds  *
5901da177e4SLinus Torvalds  *	Called under mrt_lock.
5911da177e4SLinus Torvalds  */
5921da177e4SLinus Torvalds 
5931da177e4SLinus Torvalds static int ipmr_cache_report(struct sk_buff *pkt, vifi_t vifi, int assert)
5941da177e4SLinus Torvalds {
5951da177e4SLinus Torvalds 	struct sk_buff *skb;
596c9bdd4b5SArnaldo Carvalho de Melo 	const int ihl = ip_hdrlen(pkt);
5971da177e4SLinus Torvalds 	struct igmphdr *igmp;
5981da177e4SLinus Torvalds 	struct igmpmsg *msg;
5991da177e4SLinus Torvalds 	int ret;
6001da177e4SLinus Torvalds 
6011da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM
6021da177e4SLinus Torvalds 	if (assert == IGMPMSG_WHOLEPKT)
6031da177e4SLinus Torvalds 		skb = skb_realloc_headroom(pkt, sizeof(struct iphdr));
6041da177e4SLinus Torvalds 	else
6051da177e4SLinus Torvalds #endif
6061da177e4SLinus Torvalds 		skb = alloc_skb(128, GFP_ATOMIC);
6071da177e4SLinus Torvalds 
6081da177e4SLinus Torvalds 	if (!skb)
6091da177e4SLinus Torvalds 		return -ENOBUFS;
6101da177e4SLinus Torvalds 
6111da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM
6121da177e4SLinus Torvalds 	if (assert == IGMPMSG_WHOLEPKT) {
6131da177e4SLinus Torvalds 		/* Ugly, but we have no choice with this interface.
6141da177e4SLinus Torvalds 		   Duplicate old header, fix ihl, length etc.
6151da177e4SLinus Torvalds 		   And all this only to mangle msg->im_msgtype and
6161da177e4SLinus Torvalds 		   to set msg->im_mbz to "mbz" :-)
6171da177e4SLinus Torvalds 		 */
618878c8145SArnaldo Carvalho de Melo 		skb_push(skb, sizeof(struct iphdr));
619878c8145SArnaldo Carvalho de Melo 		skb_reset_network_header(skb);
620badff6d0SArnaldo Carvalho de Melo 		skb_reset_transport_header(skb);
6210272ffc4SArnaldo Carvalho de Melo 		msg = (struct igmpmsg *)skb_network_header(skb);
622d56f90a7SArnaldo Carvalho de Melo 		memcpy(msg, skb_network_header(pkt), sizeof(struct iphdr));
6231da177e4SLinus Torvalds 		msg->im_msgtype = IGMPMSG_WHOLEPKT;
6241da177e4SLinus Torvalds 		msg->im_mbz = 0;
6251da177e4SLinus Torvalds 		msg->im_vif = reg_vif_num;
626eddc9ec5SArnaldo Carvalho de Melo 		ip_hdr(skb)->ihl = sizeof(struct iphdr) >> 2;
627eddc9ec5SArnaldo Carvalho de Melo 		ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(pkt)->tot_len) +
628eddc9ec5SArnaldo Carvalho de Melo 					     sizeof(struct iphdr));
6291da177e4SLinus Torvalds 	} else
6301da177e4SLinus Torvalds #endif
6311da177e4SLinus Torvalds 	{
6321da177e4SLinus Torvalds 
6331da177e4SLinus Torvalds 	/*
6341da177e4SLinus Torvalds 	 *	Copy the IP header
6351da177e4SLinus Torvalds 	 */
6361da177e4SLinus Torvalds 
63727a884dcSArnaldo Carvalho de Melo 	skb->network_header = skb->tail;
638ddc7b8e3SArnaldo Carvalho de Melo 	skb_put(skb, ihl);
63927d7ff46SArnaldo Carvalho de Melo 	skb_copy_to_linear_data(skb, pkt->data, ihl);
640eddc9ec5SArnaldo Carvalho de Melo 	ip_hdr(skb)->protocol = 0;			/* Flag to the kernel this is a route add */
641eddc9ec5SArnaldo Carvalho de Melo 	msg = (struct igmpmsg *)skb_network_header(skb);
6421da177e4SLinus Torvalds 	msg->im_vif = vifi;
6431da177e4SLinus Torvalds 	skb->dst = dst_clone(pkt->dst);
6441da177e4SLinus Torvalds 
6451da177e4SLinus Torvalds 	/*
6461da177e4SLinus Torvalds 	 *	Add our header
6471da177e4SLinus Torvalds 	 */
6481da177e4SLinus Torvalds 
6491da177e4SLinus Torvalds 	igmp=(struct igmphdr *)skb_put(skb, sizeof(struct igmphdr));
6501da177e4SLinus Torvalds 	igmp->type	=
6511da177e4SLinus Torvalds 	msg->im_msgtype = assert;
6521da177e4SLinus Torvalds 	igmp->code 	=	0;
653eddc9ec5SArnaldo Carvalho de Melo 	ip_hdr(skb)->tot_len = htons(skb->len);			/* Fix the length */
654b0e380b1SArnaldo Carvalho de Melo 	skb->transport_header = skb->network_header;
6551da177e4SLinus Torvalds 	}
6561da177e4SLinus Torvalds 
6571da177e4SLinus Torvalds 	if (mroute_socket == NULL) {
6581da177e4SLinus Torvalds 		kfree_skb(skb);
6591da177e4SLinus Torvalds 		return -EINVAL;
6601da177e4SLinus Torvalds 	}
6611da177e4SLinus Torvalds 
6621da177e4SLinus Torvalds 	/*
6631da177e4SLinus Torvalds 	 *	Deliver to mrouted
6641da177e4SLinus Torvalds 	 */
6651da177e4SLinus Torvalds 	if ((ret = sock_queue_rcv_skb(mroute_socket, skb))<0) {
6661da177e4SLinus Torvalds 		if (net_ratelimit())
6671da177e4SLinus Torvalds 			printk(KERN_WARNING "mroute: pending queue full, dropping entries.\n");
6681da177e4SLinus Torvalds 		kfree_skb(skb);
6691da177e4SLinus Torvalds 	}
6701da177e4SLinus Torvalds 
6711da177e4SLinus Torvalds 	return ret;
6721da177e4SLinus Torvalds }
6731da177e4SLinus Torvalds 
6741da177e4SLinus Torvalds /*
6751da177e4SLinus Torvalds  *	Queue a packet for resolution. It gets locked cache entry!
6761da177e4SLinus Torvalds  */
6771da177e4SLinus Torvalds 
6781da177e4SLinus Torvalds static int
6791da177e4SLinus Torvalds ipmr_cache_unresolved(vifi_t vifi, struct sk_buff *skb)
6801da177e4SLinus Torvalds {
6811da177e4SLinus Torvalds 	int err;
6821da177e4SLinus Torvalds 	struct mfc_cache *c;
683eddc9ec5SArnaldo Carvalho de Melo 	const struct iphdr *iph = ip_hdr(skb);
6841da177e4SLinus Torvalds 
6851da177e4SLinus Torvalds 	spin_lock_bh(&mfc_unres_lock);
6861da177e4SLinus Torvalds 	for (c=mfc_unres_queue; c; c=c->next) {
687eddc9ec5SArnaldo Carvalho de Melo 		if (c->mfc_mcastgrp == iph->daddr &&
688eddc9ec5SArnaldo Carvalho de Melo 		    c->mfc_origin == iph->saddr)
6891da177e4SLinus Torvalds 			break;
6901da177e4SLinus Torvalds 	}
6911da177e4SLinus Torvalds 
6921da177e4SLinus Torvalds 	if (c == NULL) {
6931da177e4SLinus Torvalds 		/*
6941da177e4SLinus Torvalds 		 *	Create a new entry if allowable
6951da177e4SLinus Torvalds 		 */
6961da177e4SLinus Torvalds 
6971da177e4SLinus Torvalds 		if (atomic_read(&cache_resolve_queue_len) >= 10 ||
6981da177e4SLinus Torvalds 		    (c=ipmr_cache_alloc_unres())==NULL) {
6991da177e4SLinus Torvalds 			spin_unlock_bh(&mfc_unres_lock);
7001da177e4SLinus Torvalds 
7011da177e4SLinus Torvalds 			kfree_skb(skb);
7021da177e4SLinus Torvalds 			return -ENOBUFS;
7031da177e4SLinus Torvalds 		}
7041da177e4SLinus Torvalds 
7051da177e4SLinus Torvalds 		/*
7061da177e4SLinus Torvalds 		 *	Fill in the new cache entry
7071da177e4SLinus Torvalds 		 */
7081da177e4SLinus Torvalds 		c->mfc_parent	= -1;
709eddc9ec5SArnaldo Carvalho de Melo 		c->mfc_origin	= iph->saddr;
710eddc9ec5SArnaldo Carvalho de Melo 		c->mfc_mcastgrp	= iph->daddr;
7111da177e4SLinus Torvalds 
7121da177e4SLinus Torvalds 		/*
7131da177e4SLinus Torvalds 		 *	Reflect first query at mrouted.
7141da177e4SLinus Torvalds 		 */
7151da177e4SLinus Torvalds 		if ((err = ipmr_cache_report(skb, vifi, IGMPMSG_NOCACHE))<0) {
7161da177e4SLinus Torvalds 			/* If the report failed throw the cache entry
7171da177e4SLinus Torvalds 			   out - Brad Parker
7181da177e4SLinus Torvalds 			 */
7191da177e4SLinus Torvalds 			spin_unlock_bh(&mfc_unres_lock);
7201da177e4SLinus Torvalds 
7211da177e4SLinus Torvalds 			kmem_cache_free(mrt_cachep, c);
7221da177e4SLinus Torvalds 			kfree_skb(skb);
7231da177e4SLinus Torvalds 			return err;
7241da177e4SLinus Torvalds 		}
7251da177e4SLinus Torvalds 
7261da177e4SLinus Torvalds 		atomic_inc(&cache_resolve_queue_len);
7271da177e4SLinus Torvalds 		c->next = mfc_unres_queue;
7281da177e4SLinus Torvalds 		mfc_unres_queue = c;
7291da177e4SLinus Torvalds 
7301da177e4SLinus Torvalds 		mod_timer(&ipmr_expire_timer, c->mfc_un.unres.expires);
7311da177e4SLinus Torvalds 	}
7321da177e4SLinus Torvalds 
7331da177e4SLinus Torvalds 	/*
7341da177e4SLinus Torvalds 	 *	See if we can append the packet
7351da177e4SLinus Torvalds 	 */
7361da177e4SLinus Torvalds 	if (c->mfc_un.unres.unresolved.qlen>3) {
7371da177e4SLinus Torvalds 		kfree_skb(skb);
7381da177e4SLinus Torvalds 		err = -ENOBUFS;
7391da177e4SLinus Torvalds 	} else {
7401da177e4SLinus Torvalds 		skb_queue_tail(&c->mfc_un.unres.unresolved, skb);
7411da177e4SLinus Torvalds 		err = 0;
7421da177e4SLinus Torvalds 	}
7431da177e4SLinus Torvalds 
7441da177e4SLinus Torvalds 	spin_unlock_bh(&mfc_unres_lock);
7451da177e4SLinus Torvalds 	return err;
7461da177e4SLinus Torvalds }
7471da177e4SLinus Torvalds 
7481da177e4SLinus Torvalds /*
7491da177e4SLinus Torvalds  *	MFC cache manipulation by user space mroute daemon
7501da177e4SLinus Torvalds  */
7511da177e4SLinus Torvalds 
7521da177e4SLinus Torvalds static int ipmr_mfc_delete(struct mfcctl *mfc)
7531da177e4SLinus Torvalds {
7541da177e4SLinus Torvalds 	int line;
7551da177e4SLinus Torvalds 	struct mfc_cache *c, **cp;
7561da177e4SLinus Torvalds 
7571da177e4SLinus Torvalds 	line = MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr);
7581da177e4SLinus Torvalds 
7591da177e4SLinus Torvalds 	for (cp=&mfc_cache_array[line]; (c=*cp) != NULL; cp = &c->next) {
7601da177e4SLinus Torvalds 		if (c->mfc_origin == mfc->mfcc_origin.s_addr &&
7611da177e4SLinus Torvalds 		    c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr) {
7621da177e4SLinus Torvalds 			write_lock_bh(&mrt_lock);
7631da177e4SLinus Torvalds 			*cp = c->next;
7641da177e4SLinus Torvalds 			write_unlock_bh(&mrt_lock);
7651da177e4SLinus Torvalds 
7661da177e4SLinus Torvalds 			kmem_cache_free(mrt_cachep, c);
7671da177e4SLinus Torvalds 			return 0;
7681da177e4SLinus Torvalds 		}
7691da177e4SLinus Torvalds 	}
7701da177e4SLinus Torvalds 	return -ENOENT;
7711da177e4SLinus Torvalds }
7721da177e4SLinus Torvalds 
7731da177e4SLinus Torvalds static int ipmr_mfc_add(struct mfcctl *mfc, int mrtsock)
7741da177e4SLinus Torvalds {
7751da177e4SLinus Torvalds 	int line;
7761da177e4SLinus Torvalds 	struct mfc_cache *uc, *c, **cp;
7771da177e4SLinus Torvalds 
7781da177e4SLinus Torvalds 	line = MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr);
7791da177e4SLinus Torvalds 
7801da177e4SLinus Torvalds 	for (cp=&mfc_cache_array[line]; (c=*cp) != NULL; cp = &c->next) {
7811da177e4SLinus Torvalds 		if (c->mfc_origin == mfc->mfcc_origin.s_addr &&
7821da177e4SLinus Torvalds 		    c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr)
7831da177e4SLinus Torvalds 			break;
7841da177e4SLinus Torvalds 	}
7851da177e4SLinus Torvalds 
7861da177e4SLinus Torvalds 	if (c != NULL) {
7871da177e4SLinus Torvalds 		write_lock_bh(&mrt_lock);
7881da177e4SLinus Torvalds 		c->mfc_parent = mfc->mfcc_parent;
789d1b04c08SBaruch Even 		ipmr_update_thresholds(c, mfc->mfcc_ttls);
7901da177e4SLinus Torvalds 		if (!mrtsock)
7911da177e4SLinus Torvalds 			c->mfc_flags |= MFC_STATIC;
7921da177e4SLinus Torvalds 		write_unlock_bh(&mrt_lock);
7931da177e4SLinus Torvalds 		return 0;
7941da177e4SLinus Torvalds 	}
7951da177e4SLinus Torvalds 
796f97c1e0cSJoe Perches 	if (!ipv4_is_multicast(mfc->mfcc_mcastgrp.s_addr))
7971da177e4SLinus Torvalds 		return -EINVAL;
7981da177e4SLinus Torvalds 
7991da177e4SLinus Torvalds 	c = ipmr_cache_alloc();
8001da177e4SLinus Torvalds 	if (c == NULL)
8011da177e4SLinus Torvalds 		return -ENOMEM;
8021da177e4SLinus Torvalds 
8031da177e4SLinus Torvalds 	c->mfc_origin = mfc->mfcc_origin.s_addr;
8041da177e4SLinus Torvalds 	c->mfc_mcastgrp = mfc->mfcc_mcastgrp.s_addr;
8051da177e4SLinus Torvalds 	c->mfc_parent = mfc->mfcc_parent;
806d1b04c08SBaruch Even 	ipmr_update_thresholds(c, mfc->mfcc_ttls);
8071da177e4SLinus Torvalds 	if (!mrtsock)
8081da177e4SLinus Torvalds 		c->mfc_flags |= MFC_STATIC;
8091da177e4SLinus Torvalds 
8101da177e4SLinus Torvalds 	write_lock_bh(&mrt_lock);
8111da177e4SLinus Torvalds 	c->next = mfc_cache_array[line];
8121da177e4SLinus Torvalds 	mfc_cache_array[line] = c;
8131da177e4SLinus Torvalds 	write_unlock_bh(&mrt_lock);
8141da177e4SLinus Torvalds 
8151da177e4SLinus Torvalds 	/*
8161da177e4SLinus Torvalds 	 *	Check to see if we resolved a queued list. If so we
8171da177e4SLinus Torvalds 	 *	need to send on the frames and tidy up.
8181da177e4SLinus Torvalds 	 */
8191da177e4SLinus Torvalds 	spin_lock_bh(&mfc_unres_lock);
8201da177e4SLinus Torvalds 	for (cp = &mfc_unres_queue; (uc=*cp) != NULL;
8211da177e4SLinus Torvalds 	     cp = &uc->next) {
8221da177e4SLinus Torvalds 		if (uc->mfc_origin == c->mfc_origin &&
8231da177e4SLinus Torvalds 		    uc->mfc_mcastgrp == c->mfc_mcastgrp) {
8241da177e4SLinus Torvalds 			*cp = uc->next;
8251da177e4SLinus Torvalds 			if (atomic_dec_and_test(&cache_resolve_queue_len))
8261da177e4SLinus Torvalds 				del_timer(&ipmr_expire_timer);
8271da177e4SLinus Torvalds 			break;
8281da177e4SLinus Torvalds 		}
8291da177e4SLinus Torvalds 	}
8301da177e4SLinus Torvalds 	spin_unlock_bh(&mfc_unres_lock);
8311da177e4SLinus Torvalds 
8321da177e4SLinus Torvalds 	if (uc) {
8331da177e4SLinus Torvalds 		ipmr_cache_resolve(uc, c);
8341da177e4SLinus Torvalds 		kmem_cache_free(mrt_cachep, uc);
8351da177e4SLinus Torvalds 	}
8361da177e4SLinus Torvalds 	return 0;
8371da177e4SLinus Torvalds }
8381da177e4SLinus Torvalds 
8391da177e4SLinus Torvalds /*
8401da177e4SLinus Torvalds  *	Close the multicast socket, and clear the vif tables etc
8411da177e4SLinus Torvalds  */
8421da177e4SLinus Torvalds 
8431da177e4SLinus Torvalds static void mroute_clean_tables(struct sock *sk)
8441da177e4SLinus Torvalds {
8451da177e4SLinus Torvalds 	int i;
8461da177e4SLinus Torvalds 
8471da177e4SLinus Torvalds 	/*
8481da177e4SLinus Torvalds 	 *	Shut down all active vif entries
8491da177e4SLinus Torvalds 	 */
8501da177e4SLinus Torvalds 	for (i=0; i<maxvif; i++) {
8511da177e4SLinus Torvalds 		if (!(vif_table[i].flags&VIFF_STATIC))
8527dc00c82SWang Chen 			vif_delete(i, 0);
8531da177e4SLinus Torvalds 	}
8541da177e4SLinus Torvalds 
8551da177e4SLinus Torvalds 	/*
8561da177e4SLinus Torvalds 	 *	Wipe the cache
8571da177e4SLinus Torvalds 	 */
8581da177e4SLinus Torvalds 	for (i=0; i<MFC_LINES; i++) {
8591da177e4SLinus Torvalds 		struct mfc_cache *c, **cp;
8601da177e4SLinus Torvalds 
8611da177e4SLinus Torvalds 		cp = &mfc_cache_array[i];
8621da177e4SLinus Torvalds 		while ((c = *cp) != NULL) {
8631da177e4SLinus Torvalds 			if (c->mfc_flags&MFC_STATIC) {
8641da177e4SLinus Torvalds 				cp = &c->next;
8651da177e4SLinus Torvalds 				continue;
8661da177e4SLinus Torvalds 			}
8671da177e4SLinus Torvalds 			write_lock_bh(&mrt_lock);
8681da177e4SLinus Torvalds 			*cp = c->next;
8691da177e4SLinus Torvalds 			write_unlock_bh(&mrt_lock);
8701da177e4SLinus Torvalds 
8711da177e4SLinus Torvalds 			kmem_cache_free(mrt_cachep, c);
8721da177e4SLinus Torvalds 		}
8731da177e4SLinus Torvalds 	}
8741da177e4SLinus Torvalds 
8751da177e4SLinus Torvalds 	if (atomic_read(&cache_resolve_queue_len) != 0) {
8761da177e4SLinus Torvalds 		struct mfc_cache *c;
8771da177e4SLinus Torvalds 
8781da177e4SLinus Torvalds 		spin_lock_bh(&mfc_unres_lock);
8791da177e4SLinus Torvalds 		while (mfc_unres_queue != NULL) {
8801da177e4SLinus Torvalds 			c = mfc_unres_queue;
8811da177e4SLinus Torvalds 			mfc_unres_queue = c->next;
8821da177e4SLinus Torvalds 			spin_unlock_bh(&mfc_unres_lock);
8831da177e4SLinus Torvalds 
8841da177e4SLinus Torvalds 			ipmr_destroy_unres(c);
8851da177e4SLinus Torvalds 
8861da177e4SLinus Torvalds 			spin_lock_bh(&mfc_unres_lock);
8871da177e4SLinus Torvalds 		}
8881da177e4SLinus Torvalds 		spin_unlock_bh(&mfc_unres_lock);
8891da177e4SLinus Torvalds 	}
8901da177e4SLinus Torvalds }
8911da177e4SLinus Torvalds 
8921da177e4SLinus Torvalds static void mrtsock_destruct(struct sock *sk)
8931da177e4SLinus Torvalds {
8941da177e4SLinus Torvalds 	rtnl_lock();
8951da177e4SLinus Torvalds 	if (sk == mroute_socket) {
8963b1e0a65SYOSHIFUJI Hideaki 		IPV4_DEVCONF_ALL(sock_net(sk), MC_FORWARDING)--;
8971da177e4SLinus Torvalds 
8981da177e4SLinus Torvalds 		write_lock_bh(&mrt_lock);
8991da177e4SLinus Torvalds 		mroute_socket = NULL;
9001da177e4SLinus Torvalds 		write_unlock_bh(&mrt_lock);
9011da177e4SLinus Torvalds 
9021da177e4SLinus Torvalds 		mroute_clean_tables(sk);
9031da177e4SLinus Torvalds 	}
9041da177e4SLinus Torvalds 	rtnl_unlock();
9051da177e4SLinus Torvalds }
9061da177e4SLinus Torvalds 
9071da177e4SLinus Torvalds /*
9081da177e4SLinus Torvalds  *	Socket options and virtual interface manipulation. The whole
9091da177e4SLinus Torvalds  *	virtual interface system is a complete heap, but unfortunately
9101da177e4SLinus Torvalds  *	that's how BSD mrouted happens to think. Maybe one day with a proper
9111da177e4SLinus Torvalds  *	MOSPF/PIM router set up we can clean this up.
9121da177e4SLinus Torvalds  */
9131da177e4SLinus Torvalds 
9141da177e4SLinus Torvalds int ip_mroute_setsockopt(struct sock *sk, int optname, char __user *optval, int optlen)
9151da177e4SLinus Torvalds {
9161da177e4SLinus Torvalds 	int ret;
9171da177e4SLinus Torvalds 	struct vifctl vif;
9181da177e4SLinus Torvalds 	struct mfcctl mfc;
9191da177e4SLinus Torvalds 
920132adf54SStephen Hemminger 	if (optname != MRT_INIT) {
9211da177e4SLinus Torvalds 		if (sk != mroute_socket && !capable(CAP_NET_ADMIN))
9221da177e4SLinus Torvalds 			return -EACCES;
9231da177e4SLinus Torvalds 	}
9241da177e4SLinus Torvalds 
925132adf54SStephen Hemminger 	switch (optname) {
9261da177e4SLinus Torvalds 	case MRT_INIT:
9271da177e4SLinus Torvalds 		if (sk->sk_type != SOCK_RAW ||
9281da177e4SLinus Torvalds 		    inet_sk(sk)->num != IPPROTO_IGMP)
9291da177e4SLinus Torvalds 			return -EOPNOTSUPP;
9301da177e4SLinus Torvalds 		if (optlen != sizeof(int))
9311da177e4SLinus Torvalds 			return -ENOPROTOOPT;
9321da177e4SLinus Torvalds 
9331da177e4SLinus Torvalds 		rtnl_lock();
9341da177e4SLinus Torvalds 		if (mroute_socket) {
9351da177e4SLinus Torvalds 			rtnl_unlock();
9361da177e4SLinus Torvalds 			return -EADDRINUSE;
9371da177e4SLinus Torvalds 		}
9381da177e4SLinus Torvalds 
9391da177e4SLinus Torvalds 		ret = ip_ra_control(sk, 1, mrtsock_destruct);
9401da177e4SLinus Torvalds 		if (ret == 0) {
9411da177e4SLinus Torvalds 			write_lock_bh(&mrt_lock);
9421da177e4SLinus Torvalds 			mroute_socket = sk;
9431da177e4SLinus Torvalds 			write_unlock_bh(&mrt_lock);
9441da177e4SLinus Torvalds 
9453b1e0a65SYOSHIFUJI Hideaki 			IPV4_DEVCONF_ALL(sock_net(sk), MC_FORWARDING)++;
9461da177e4SLinus Torvalds 		}
9471da177e4SLinus Torvalds 		rtnl_unlock();
9481da177e4SLinus Torvalds 		return ret;
9491da177e4SLinus Torvalds 	case MRT_DONE:
9501da177e4SLinus Torvalds 		if (sk != mroute_socket)
9511da177e4SLinus Torvalds 			return -EACCES;
9521da177e4SLinus Torvalds 		return ip_ra_control(sk, 0, NULL);
9531da177e4SLinus Torvalds 	case MRT_ADD_VIF:
9541da177e4SLinus Torvalds 	case MRT_DEL_VIF:
9551da177e4SLinus Torvalds 		if (optlen != sizeof(vif))
9561da177e4SLinus Torvalds 			return -EINVAL;
9571da177e4SLinus Torvalds 		if (copy_from_user(&vif, optval, sizeof(vif)))
9581da177e4SLinus Torvalds 			return -EFAULT;
9591da177e4SLinus Torvalds 		if (vif.vifc_vifi >= MAXVIFS)
9601da177e4SLinus Torvalds 			return -ENFILE;
9611da177e4SLinus Torvalds 		rtnl_lock();
9621da177e4SLinus Torvalds 		if (optname == MRT_ADD_VIF) {
9631da177e4SLinus Torvalds 			ret = vif_add(&vif, sk==mroute_socket);
9641da177e4SLinus Torvalds 		} else {
9657dc00c82SWang Chen 			ret = vif_delete(vif.vifc_vifi, 0);
9661da177e4SLinus Torvalds 		}
9671da177e4SLinus Torvalds 		rtnl_unlock();
9681da177e4SLinus Torvalds 		return ret;
9691da177e4SLinus Torvalds 
9701da177e4SLinus Torvalds 		/*
9711da177e4SLinus Torvalds 		 *	Manipulate the forwarding caches. These live
9721da177e4SLinus Torvalds 		 *	in a sort of kernel/user symbiosis.
9731da177e4SLinus Torvalds 		 */
9741da177e4SLinus Torvalds 	case MRT_ADD_MFC:
9751da177e4SLinus Torvalds 	case MRT_DEL_MFC:
9761da177e4SLinus Torvalds 		if (optlen != sizeof(mfc))
9771da177e4SLinus Torvalds 			return -EINVAL;
9781da177e4SLinus Torvalds 		if (copy_from_user(&mfc, optval, sizeof(mfc)))
9791da177e4SLinus Torvalds 			return -EFAULT;
9801da177e4SLinus Torvalds 		rtnl_lock();
9811da177e4SLinus Torvalds 		if (optname == MRT_DEL_MFC)
9821da177e4SLinus Torvalds 			ret = ipmr_mfc_delete(&mfc);
9831da177e4SLinus Torvalds 		else
9841da177e4SLinus Torvalds 			ret = ipmr_mfc_add(&mfc, sk==mroute_socket);
9851da177e4SLinus Torvalds 		rtnl_unlock();
9861da177e4SLinus Torvalds 		return ret;
9871da177e4SLinus Torvalds 		/*
9881da177e4SLinus Torvalds 		 *	Control PIM assert.
9891da177e4SLinus Torvalds 		 */
9901da177e4SLinus Torvalds 	case MRT_ASSERT:
9911da177e4SLinus Torvalds 	{
9921da177e4SLinus Torvalds 		int v;
9931da177e4SLinus Torvalds 		if (get_user(v,(int __user *)optval))
9941da177e4SLinus Torvalds 			return -EFAULT;
9951da177e4SLinus Torvalds 		mroute_do_assert=(v)?1:0;
9961da177e4SLinus Torvalds 		return 0;
9971da177e4SLinus Torvalds 	}
9981da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM
9991da177e4SLinus Torvalds 	case MRT_PIM:
10001da177e4SLinus Torvalds 	{
1001ba93ef74SStephen Hemminger 		int v;
1002ba93ef74SStephen Hemminger 
10031da177e4SLinus Torvalds 		if (get_user(v,(int __user *)optval))
10041da177e4SLinus Torvalds 			return -EFAULT;
10051da177e4SLinus Torvalds 		v = (v) ? 1 : 0;
1006ba93ef74SStephen Hemminger 
10071da177e4SLinus Torvalds 		rtnl_lock();
10081da177e4SLinus Torvalds 		ret = 0;
10091da177e4SLinus Torvalds 		if (v != mroute_do_pim) {
10101da177e4SLinus Torvalds 			mroute_do_pim = v;
10111da177e4SLinus Torvalds 			mroute_do_assert = v;
10121da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM_V2
10131da177e4SLinus Torvalds 			if (mroute_do_pim)
10141da177e4SLinus Torvalds 				ret = inet_add_protocol(&pim_protocol,
10151da177e4SLinus Torvalds 							IPPROTO_PIM);
10161da177e4SLinus Torvalds 			else
10171da177e4SLinus Torvalds 				ret = inet_del_protocol(&pim_protocol,
10181da177e4SLinus Torvalds 							IPPROTO_PIM);
10191da177e4SLinus Torvalds 			if (ret < 0)
10201da177e4SLinus Torvalds 				ret = -EAGAIN;
10211da177e4SLinus Torvalds #endif
10221da177e4SLinus Torvalds 		}
10231da177e4SLinus Torvalds 		rtnl_unlock();
10241da177e4SLinus Torvalds 		return ret;
10251da177e4SLinus Torvalds 	}
10261da177e4SLinus Torvalds #endif
10271da177e4SLinus Torvalds 	/*
10281da177e4SLinus Torvalds 	 *	Spurious command, or MRT_VERSION which you cannot
10291da177e4SLinus Torvalds 	 *	set.
10301da177e4SLinus Torvalds 	 */
10311da177e4SLinus Torvalds 	default:
10321da177e4SLinus Torvalds 		return -ENOPROTOOPT;
10331da177e4SLinus Torvalds 	}
10341da177e4SLinus Torvalds }
10351da177e4SLinus Torvalds 
10361da177e4SLinus Torvalds /*
10371da177e4SLinus Torvalds  *	Getsock opt support for the multicast routing system.
10381da177e4SLinus Torvalds  */
10391da177e4SLinus Torvalds 
10401da177e4SLinus Torvalds int ip_mroute_getsockopt(struct sock *sk, int optname, char __user *optval, int __user *optlen)
10411da177e4SLinus Torvalds {
10421da177e4SLinus Torvalds 	int olr;
10431da177e4SLinus Torvalds 	int val;
10441da177e4SLinus Torvalds 
10451da177e4SLinus Torvalds 	if (optname != MRT_VERSION &&
10461da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM
10471da177e4SLinus Torvalds 	   optname!=MRT_PIM &&
10481da177e4SLinus Torvalds #endif
10491da177e4SLinus Torvalds 	   optname!=MRT_ASSERT)
10501da177e4SLinus Torvalds 		return -ENOPROTOOPT;
10511da177e4SLinus Torvalds 
10521da177e4SLinus Torvalds 	if (get_user(olr, optlen))
10531da177e4SLinus Torvalds 		return -EFAULT;
10541da177e4SLinus Torvalds 
10551da177e4SLinus Torvalds 	olr = min_t(unsigned int, olr, sizeof(int));
10561da177e4SLinus Torvalds 	if (olr < 0)
10571da177e4SLinus Torvalds 		return -EINVAL;
10581da177e4SLinus Torvalds 
10591da177e4SLinus Torvalds 	if (put_user(olr, optlen))
10601da177e4SLinus Torvalds 		return -EFAULT;
10611da177e4SLinus Torvalds 	if (optname == MRT_VERSION)
10621da177e4SLinus Torvalds 		val = 0x0305;
10631da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM
10641da177e4SLinus Torvalds 	else if (optname == MRT_PIM)
10651da177e4SLinus Torvalds 		val = mroute_do_pim;
10661da177e4SLinus Torvalds #endif
10671da177e4SLinus Torvalds 	else
10681da177e4SLinus Torvalds 		val = mroute_do_assert;
10691da177e4SLinus Torvalds 	if (copy_to_user(optval, &val, olr))
10701da177e4SLinus Torvalds 		return -EFAULT;
10711da177e4SLinus Torvalds 	return 0;
10721da177e4SLinus Torvalds }
10731da177e4SLinus Torvalds 
10741da177e4SLinus Torvalds /*
10751da177e4SLinus Torvalds  *	The IP multicast ioctl support routines.
10761da177e4SLinus Torvalds  */
10771da177e4SLinus Torvalds 
10781da177e4SLinus Torvalds int ipmr_ioctl(struct sock *sk, int cmd, void __user *arg)
10791da177e4SLinus Torvalds {
10801da177e4SLinus Torvalds 	struct sioc_sg_req sr;
10811da177e4SLinus Torvalds 	struct sioc_vif_req vr;
10821da177e4SLinus Torvalds 	struct vif_device *vif;
10831da177e4SLinus Torvalds 	struct mfc_cache *c;
10841da177e4SLinus Torvalds 
1085132adf54SStephen Hemminger 	switch (cmd) {
10861da177e4SLinus Torvalds 	case SIOCGETVIFCNT:
10871da177e4SLinus Torvalds 		if (copy_from_user(&vr, arg, sizeof(vr)))
10881da177e4SLinus Torvalds 			return -EFAULT;
10891da177e4SLinus Torvalds 		if (vr.vifi >= maxvif)
10901da177e4SLinus Torvalds 			return -EINVAL;
10911da177e4SLinus Torvalds 		read_lock(&mrt_lock);
10921da177e4SLinus Torvalds 		vif=&vif_table[vr.vifi];
10931da177e4SLinus Torvalds 		if (VIF_EXISTS(vr.vifi))	{
10941da177e4SLinus Torvalds 			vr.icount = vif->pkt_in;
10951da177e4SLinus Torvalds 			vr.ocount = vif->pkt_out;
10961da177e4SLinus Torvalds 			vr.ibytes = vif->bytes_in;
10971da177e4SLinus Torvalds 			vr.obytes = vif->bytes_out;
10981da177e4SLinus Torvalds 			read_unlock(&mrt_lock);
10991da177e4SLinus Torvalds 
11001da177e4SLinus Torvalds 			if (copy_to_user(arg, &vr, sizeof(vr)))
11011da177e4SLinus Torvalds 				return -EFAULT;
11021da177e4SLinus Torvalds 			return 0;
11031da177e4SLinus Torvalds 		}
11041da177e4SLinus Torvalds 		read_unlock(&mrt_lock);
11051da177e4SLinus Torvalds 		return -EADDRNOTAVAIL;
11061da177e4SLinus Torvalds 	case SIOCGETSGCNT:
11071da177e4SLinus Torvalds 		if (copy_from_user(&sr, arg, sizeof(sr)))
11081da177e4SLinus Torvalds 			return -EFAULT;
11091da177e4SLinus Torvalds 
11101da177e4SLinus Torvalds 		read_lock(&mrt_lock);
11111da177e4SLinus Torvalds 		c = ipmr_cache_find(sr.src.s_addr, sr.grp.s_addr);
11121da177e4SLinus Torvalds 		if (c) {
11131da177e4SLinus Torvalds 			sr.pktcnt = c->mfc_un.res.pkt;
11141da177e4SLinus Torvalds 			sr.bytecnt = c->mfc_un.res.bytes;
11151da177e4SLinus Torvalds 			sr.wrong_if = c->mfc_un.res.wrong_if;
11161da177e4SLinus Torvalds 			read_unlock(&mrt_lock);
11171da177e4SLinus Torvalds 
11181da177e4SLinus Torvalds 			if (copy_to_user(arg, &sr, sizeof(sr)))
11191da177e4SLinus Torvalds 				return -EFAULT;
11201da177e4SLinus Torvalds 			return 0;
11211da177e4SLinus Torvalds 		}
11221da177e4SLinus Torvalds 		read_unlock(&mrt_lock);
11231da177e4SLinus Torvalds 		return -EADDRNOTAVAIL;
11241da177e4SLinus Torvalds 	default:
11251da177e4SLinus Torvalds 		return -ENOIOCTLCMD;
11261da177e4SLinus Torvalds 	}
11271da177e4SLinus Torvalds }
11281da177e4SLinus Torvalds 
11291da177e4SLinus Torvalds 
11301da177e4SLinus Torvalds static int ipmr_device_event(struct notifier_block *this, unsigned long event, void *ptr)
11311da177e4SLinus Torvalds {
1132e9dc8653SEric W. Biederman 	struct net_device *dev = ptr;
11331da177e4SLinus Torvalds 	struct vif_device *v;
11341da177e4SLinus Torvalds 	int ct;
1135e9dc8653SEric W. Biederman 
1136721499e8SYOSHIFUJI Hideaki 	if (!net_eq(dev_net(dev), &init_net))
1137e9dc8653SEric W. Biederman 		return NOTIFY_DONE;
1138e9dc8653SEric W. Biederman 
11391da177e4SLinus Torvalds 	if (event != NETDEV_UNREGISTER)
11401da177e4SLinus Torvalds 		return NOTIFY_DONE;
11411da177e4SLinus Torvalds 	v=&vif_table[0];
11421da177e4SLinus Torvalds 	for (ct=0; ct<maxvif; ct++,v++) {
1143e9dc8653SEric W. Biederman 		if (v->dev == dev)
11447dc00c82SWang Chen 			vif_delete(ct, 1);
11451da177e4SLinus Torvalds 	}
11461da177e4SLinus Torvalds 	return NOTIFY_DONE;
11471da177e4SLinus Torvalds }
11481da177e4SLinus Torvalds 
11491da177e4SLinus Torvalds 
11501da177e4SLinus Torvalds static struct notifier_block ip_mr_notifier = {
11511da177e4SLinus Torvalds 	.notifier_call = ipmr_device_event,
11521da177e4SLinus Torvalds };
11531da177e4SLinus Torvalds 
11541da177e4SLinus Torvalds /*
11551da177e4SLinus Torvalds  * 	Encapsulate a packet by attaching a valid IPIP header to it.
11561da177e4SLinus Torvalds  *	This avoids tunnel drivers and other mess and gives us the speed so
11571da177e4SLinus Torvalds  *	important for multicast video.
11581da177e4SLinus Torvalds  */
11591da177e4SLinus Torvalds 
1160114c7844SAl Viro static void ip_encap(struct sk_buff *skb, __be32 saddr, __be32 daddr)
11611da177e4SLinus Torvalds {
11628856dfa3SArnaldo Carvalho de Melo 	struct iphdr *iph;
1163eddc9ec5SArnaldo Carvalho de Melo 	struct iphdr *old_iph = ip_hdr(skb);
11648856dfa3SArnaldo Carvalho de Melo 
11658856dfa3SArnaldo Carvalho de Melo 	skb_push(skb, sizeof(struct iphdr));
1166b0e380b1SArnaldo Carvalho de Melo 	skb->transport_header = skb->network_header;
11678856dfa3SArnaldo Carvalho de Melo 	skb_reset_network_header(skb);
1168eddc9ec5SArnaldo Carvalho de Melo 	iph = ip_hdr(skb);
11691da177e4SLinus Torvalds 
11701da177e4SLinus Torvalds 	iph->version	= 	4;
1171e023dd64SArnaldo Carvalho de Melo 	iph->tos	=	old_iph->tos;
1172e023dd64SArnaldo Carvalho de Melo 	iph->ttl	=	old_iph->ttl;
11731da177e4SLinus Torvalds 	iph->frag_off	=	0;
11741da177e4SLinus Torvalds 	iph->daddr	=	daddr;
11751da177e4SLinus Torvalds 	iph->saddr	=	saddr;
11761da177e4SLinus Torvalds 	iph->protocol	=	IPPROTO_IPIP;
11771da177e4SLinus Torvalds 	iph->ihl	=	5;
11781da177e4SLinus Torvalds 	iph->tot_len	=	htons(skb->len);
11791da177e4SLinus Torvalds 	ip_select_ident(iph, skb->dst, NULL);
11801da177e4SLinus Torvalds 	ip_send_check(iph);
11811da177e4SLinus Torvalds 
11821da177e4SLinus Torvalds 	memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
11831da177e4SLinus Torvalds 	nf_reset(skb);
11841da177e4SLinus Torvalds }
11851da177e4SLinus Torvalds 
11861da177e4SLinus Torvalds static inline int ipmr_forward_finish(struct sk_buff *skb)
11871da177e4SLinus Torvalds {
11881da177e4SLinus Torvalds 	struct ip_options * opt	= &(IPCB(skb)->opt);
11891da177e4SLinus Torvalds 
11907c73a6faSPavel Emelyanov 	IP_INC_STATS_BH(dev_net(skb->dst->dev), IPSTATS_MIB_OUTFORWDATAGRAMS);
11911da177e4SLinus Torvalds 
11921da177e4SLinus Torvalds 	if (unlikely(opt->optlen))
11931da177e4SLinus Torvalds 		ip_forward_options(skb);
11941da177e4SLinus Torvalds 
11951da177e4SLinus Torvalds 	return dst_output(skb);
11961da177e4SLinus Torvalds }
11971da177e4SLinus Torvalds 
11981da177e4SLinus Torvalds /*
11991da177e4SLinus Torvalds  *	Processing handlers for ipmr_forward
12001da177e4SLinus Torvalds  */
12011da177e4SLinus Torvalds 
12021da177e4SLinus Torvalds static void ipmr_queue_xmit(struct sk_buff *skb, struct mfc_cache *c, int vifi)
12031da177e4SLinus Torvalds {
1204eddc9ec5SArnaldo Carvalho de Melo 	const struct iphdr *iph = ip_hdr(skb);
12051da177e4SLinus Torvalds 	struct vif_device *vif = &vif_table[vifi];
12061da177e4SLinus Torvalds 	struct net_device *dev;
12071da177e4SLinus Torvalds 	struct rtable *rt;
12081da177e4SLinus Torvalds 	int    encap = 0;
12091da177e4SLinus Torvalds 
12101da177e4SLinus Torvalds 	if (vif->dev == NULL)
12111da177e4SLinus Torvalds 		goto out_free;
12121da177e4SLinus Torvalds 
12131da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM
12141da177e4SLinus Torvalds 	if (vif->flags & VIFF_REGISTER) {
12151da177e4SLinus Torvalds 		vif->pkt_out++;
12161da177e4SLinus Torvalds 		vif->bytes_out += skb->len;
1217cf3677aeSPavel Emelyanov 		vif->dev->stats.tx_bytes += skb->len;
1218cf3677aeSPavel Emelyanov 		vif->dev->stats.tx_packets++;
12191da177e4SLinus Torvalds 		ipmr_cache_report(skb, vifi, IGMPMSG_WHOLEPKT);
12201da177e4SLinus Torvalds 		kfree_skb(skb);
12211da177e4SLinus Torvalds 		return;
12221da177e4SLinus Torvalds 	}
12231da177e4SLinus Torvalds #endif
12241da177e4SLinus Torvalds 
12251da177e4SLinus Torvalds 	if (vif->flags&VIFF_TUNNEL) {
12261da177e4SLinus Torvalds 		struct flowi fl = { .oif = vif->link,
12271da177e4SLinus Torvalds 				    .nl_u = { .ip4_u =
12281da177e4SLinus Torvalds 					      { .daddr = vif->remote,
12291da177e4SLinus Torvalds 						.saddr = vif->local,
12301da177e4SLinus Torvalds 						.tos = RT_TOS(iph->tos) } },
12311da177e4SLinus Torvalds 				    .proto = IPPROTO_IPIP };
1232f206351aSDenis V. Lunev 		if (ip_route_output_key(&init_net, &rt, &fl))
12331da177e4SLinus Torvalds 			goto out_free;
12341da177e4SLinus Torvalds 		encap = sizeof(struct iphdr);
12351da177e4SLinus Torvalds 	} else {
12361da177e4SLinus Torvalds 		struct flowi fl = { .oif = vif->link,
12371da177e4SLinus Torvalds 				    .nl_u = { .ip4_u =
12381da177e4SLinus Torvalds 					      { .daddr = iph->daddr,
12391da177e4SLinus Torvalds 						.tos = RT_TOS(iph->tos) } },
12401da177e4SLinus Torvalds 				    .proto = IPPROTO_IPIP };
1241f206351aSDenis V. Lunev 		if (ip_route_output_key(&init_net, &rt, &fl))
12421da177e4SLinus Torvalds 			goto out_free;
12431da177e4SLinus Torvalds 	}
12441da177e4SLinus Torvalds 
12451da177e4SLinus Torvalds 	dev = rt->u.dst.dev;
12461da177e4SLinus Torvalds 
12471da177e4SLinus Torvalds 	if (skb->len+encap > dst_mtu(&rt->u.dst) && (ntohs(iph->frag_off) & IP_DF)) {
12481da177e4SLinus Torvalds 		/* Do not fragment multicasts. Alas, IPv4 does not
12491da177e4SLinus Torvalds 		   allow to send ICMP, so that packets will disappear
12501da177e4SLinus Torvalds 		   to blackhole.
12511da177e4SLinus Torvalds 		 */
12521da177e4SLinus Torvalds 
12537c73a6faSPavel Emelyanov 		IP_INC_STATS_BH(dev_net(dev), IPSTATS_MIB_FRAGFAILS);
12541da177e4SLinus Torvalds 		ip_rt_put(rt);
12551da177e4SLinus Torvalds 		goto out_free;
12561da177e4SLinus Torvalds 	}
12571da177e4SLinus Torvalds 
12581da177e4SLinus Torvalds 	encap += LL_RESERVED_SPACE(dev) + rt->u.dst.header_len;
12591da177e4SLinus Torvalds 
12601da177e4SLinus Torvalds 	if (skb_cow(skb, encap)) {
12611da177e4SLinus Torvalds 		ip_rt_put(rt);
12621da177e4SLinus Torvalds 		goto out_free;
12631da177e4SLinus Torvalds 	}
12641da177e4SLinus Torvalds 
12651da177e4SLinus Torvalds 	vif->pkt_out++;
12661da177e4SLinus Torvalds 	vif->bytes_out += skb->len;
12671da177e4SLinus Torvalds 
12681da177e4SLinus Torvalds 	dst_release(skb->dst);
12691da177e4SLinus Torvalds 	skb->dst = &rt->u.dst;
1270eddc9ec5SArnaldo Carvalho de Melo 	ip_decrease_ttl(ip_hdr(skb));
12711da177e4SLinus Torvalds 
12721da177e4SLinus Torvalds 	/* FIXME: forward and output firewalls used to be called here.
12731da177e4SLinus Torvalds 	 * What do we do with netfilter? -- RR */
12741da177e4SLinus Torvalds 	if (vif->flags & VIFF_TUNNEL) {
12751da177e4SLinus Torvalds 		ip_encap(skb, vif->local, vif->remote);
12761da177e4SLinus Torvalds 		/* FIXME: extra output firewall step used to be here. --RR */
12772f4c02d4SPavel Emelyanov 		vif->dev->stats.tx_packets++;
12782f4c02d4SPavel Emelyanov 		vif->dev->stats.tx_bytes += skb->len;
12791da177e4SLinus Torvalds 	}
12801da177e4SLinus Torvalds 
12811da177e4SLinus Torvalds 	IPCB(skb)->flags |= IPSKB_FORWARDED;
12821da177e4SLinus Torvalds 
12831da177e4SLinus Torvalds 	/*
12841da177e4SLinus Torvalds 	 * RFC1584 teaches, that DVMRP/PIM router must deliver packets locally
12851da177e4SLinus Torvalds 	 * not only before forwarding, but after forwarding on all output
12861da177e4SLinus Torvalds 	 * interfaces. It is clear, if mrouter runs a multicasting
12871da177e4SLinus Torvalds 	 * program, it should receive packets not depending to what interface
12881da177e4SLinus Torvalds 	 * program is joined.
12891da177e4SLinus Torvalds 	 * If we will not make it, the program will have to join on all
12901da177e4SLinus Torvalds 	 * interfaces. On the other hand, multihoming host (or router, but
12911da177e4SLinus Torvalds 	 * not mrouter) cannot join to more than one interface - it will
12921da177e4SLinus Torvalds 	 * result in receiving multiple packets.
12931da177e4SLinus Torvalds 	 */
12946e23ae2aSPatrick McHardy 	NF_HOOK(PF_INET, NF_INET_FORWARD, skb, skb->dev, dev,
12951da177e4SLinus Torvalds 		ipmr_forward_finish);
12961da177e4SLinus Torvalds 	return;
12971da177e4SLinus Torvalds 
12981da177e4SLinus Torvalds out_free:
12991da177e4SLinus Torvalds 	kfree_skb(skb);
13001da177e4SLinus Torvalds 	return;
13011da177e4SLinus Torvalds }
13021da177e4SLinus Torvalds 
13031da177e4SLinus Torvalds static int ipmr_find_vif(struct net_device *dev)
13041da177e4SLinus Torvalds {
13051da177e4SLinus Torvalds 	int ct;
13061da177e4SLinus Torvalds 	for (ct=maxvif-1; ct>=0; ct--) {
13071da177e4SLinus Torvalds 		if (vif_table[ct].dev == dev)
13081da177e4SLinus Torvalds 			break;
13091da177e4SLinus Torvalds 	}
13101da177e4SLinus Torvalds 	return ct;
13111da177e4SLinus Torvalds }
13121da177e4SLinus Torvalds 
13131da177e4SLinus Torvalds /* "local" means that we should preserve one skb (for local delivery) */
13141da177e4SLinus Torvalds 
13151da177e4SLinus Torvalds static int ip_mr_forward(struct sk_buff *skb, struct mfc_cache *cache, int local)
13161da177e4SLinus Torvalds {
13171da177e4SLinus Torvalds 	int psend = -1;
13181da177e4SLinus Torvalds 	int vif, ct;
13191da177e4SLinus Torvalds 
13201da177e4SLinus Torvalds 	vif = cache->mfc_parent;
13211da177e4SLinus Torvalds 	cache->mfc_un.res.pkt++;
13221da177e4SLinus Torvalds 	cache->mfc_un.res.bytes += skb->len;
13231da177e4SLinus Torvalds 
13241da177e4SLinus Torvalds 	/*
13251da177e4SLinus Torvalds 	 * Wrong interface: drop packet and (maybe) send PIM assert.
13261da177e4SLinus Torvalds 	 */
13271da177e4SLinus Torvalds 	if (vif_table[vif].dev != skb->dev) {
13281da177e4SLinus Torvalds 		int true_vifi;
13291da177e4SLinus Torvalds 
1330ee6b9673SEric Dumazet 		if (skb->rtable->fl.iif == 0) {
13311da177e4SLinus Torvalds 			/* It is our own packet, looped back.
13321da177e4SLinus Torvalds 			   Very complicated situation...
13331da177e4SLinus Torvalds 
13341da177e4SLinus Torvalds 			   The best workaround until routing daemons will be
13351da177e4SLinus Torvalds 			   fixed is not to redistribute packet, if it was
13361da177e4SLinus Torvalds 			   send through wrong interface. It means, that
13371da177e4SLinus Torvalds 			   multicast applications WILL NOT work for
13381da177e4SLinus Torvalds 			   (S,G), which have default multicast route pointing
13391da177e4SLinus Torvalds 			   to wrong oif. In any case, it is not a good
13401da177e4SLinus Torvalds 			   idea to use multicasting applications on router.
13411da177e4SLinus Torvalds 			 */
13421da177e4SLinus Torvalds 			goto dont_forward;
13431da177e4SLinus Torvalds 		}
13441da177e4SLinus Torvalds 
13451da177e4SLinus Torvalds 		cache->mfc_un.res.wrong_if++;
13461da177e4SLinus Torvalds 		true_vifi = ipmr_find_vif(skb->dev);
13471da177e4SLinus Torvalds 
13481da177e4SLinus Torvalds 		if (true_vifi >= 0 && mroute_do_assert &&
13491da177e4SLinus Torvalds 		    /* pimsm uses asserts, when switching from RPT to SPT,
13501da177e4SLinus Torvalds 		       so that we cannot check that packet arrived on an oif.
13511da177e4SLinus Torvalds 		       It is bad, but otherwise we would need to move pretty
13521da177e4SLinus Torvalds 		       large chunk of pimd to kernel. Ough... --ANK
13531da177e4SLinus Torvalds 		     */
13541da177e4SLinus Torvalds 		    (mroute_do_pim || cache->mfc_un.res.ttls[true_vifi] < 255) &&
13551da177e4SLinus Torvalds 		    time_after(jiffies,
13561da177e4SLinus Torvalds 			       cache->mfc_un.res.last_assert + MFC_ASSERT_THRESH)) {
13571da177e4SLinus Torvalds 			cache->mfc_un.res.last_assert = jiffies;
13581da177e4SLinus Torvalds 			ipmr_cache_report(skb, true_vifi, IGMPMSG_WRONGVIF);
13591da177e4SLinus Torvalds 		}
13601da177e4SLinus Torvalds 		goto dont_forward;
13611da177e4SLinus Torvalds 	}
13621da177e4SLinus Torvalds 
13631da177e4SLinus Torvalds 	vif_table[vif].pkt_in++;
13641da177e4SLinus Torvalds 	vif_table[vif].bytes_in += skb->len;
13651da177e4SLinus Torvalds 
13661da177e4SLinus Torvalds 	/*
13671da177e4SLinus Torvalds 	 *	Forward the frame
13681da177e4SLinus Torvalds 	 */
13691da177e4SLinus Torvalds 	for (ct = cache->mfc_un.res.maxvif-1; ct >= cache->mfc_un.res.minvif; ct--) {
1370eddc9ec5SArnaldo Carvalho de Melo 		if (ip_hdr(skb)->ttl > cache->mfc_un.res.ttls[ct]) {
13711da177e4SLinus Torvalds 			if (psend != -1) {
13721da177e4SLinus Torvalds 				struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
13731da177e4SLinus Torvalds 				if (skb2)
13741da177e4SLinus Torvalds 					ipmr_queue_xmit(skb2, cache, psend);
13751da177e4SLinus Torvalds 			}
13761da177e4SLinus Torvalds 			psend = ct;
13771da177e4SLinus Torvalds 		}
13781da177e4SLinus Torvalds 	}
13791da177e4SLinus Torvalds 	if (psend != -1) {
13801da177e4SLinus Torvalds 		if (local) {
13811da177e4SLinus Torvalds 			struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
13821da177e4SLinus Torvalds 			if (skb2)
13831da177e4SLinus Torvalds 				ipmr_queue_xmit(skb2, cache, psend);
13841da177e4SLinus Torvalds 		} else {
13851da177e4SLinus Torvalds 			ipmr_queue_xmit(skb, cache, psend);
13861da177e4SLinus Torvalds 			return 0;
13871da177e4SLinus Torvalds 		}
13881da177e4SLinus Torvalds 	}
13891da177e4SLinus Torvalds 
13901da177e4SLinus Torvalds dont_forward:
13911da177e4SLinus Torvalds 	if (!local)
13921da177e4SLinus Torvalds 		kfree_skb(skb);
13931da177e4SLinus Torvalds 	return 0;
13941da177e4SLinus Torvalds }
13951da177e4SLinus Torvalds 
13961da177e4SLinus Torvalds 
13971da177e4SLinus Torvalds /*
13981da177e4SLinus Torvalds  *	Multicast packets for forwarding arrive here
13991da177e4SLinus Torvalds  */
14001da177e4SLinus Torvalds 
14011da177e4SLinus Torvalds int ip_mr_input(struct sk_buff *skb)
14021da177e4SLinus Torvalds {
14031da177e4SLinus Torvalds 	struct mfc_cache *cache;
1404ee6b9673SEric Dumazet 	int local = skb->rtable->rt_flags&RTCF_LOCAL;
14051da177e4SLinus Torvalds 
14061da177e4SLinus Torvalds 	/* Packet is looped back after forward, it should not be
14071da177e4SLinus Torvalds 	   forwarded second time, but still can be delivered locally.
14081da177e4SLinus Torvalds 	 */
14091da177e4SLinus Torvalds 	if (IPCB(skb)->flags&IPSKB_FORWARDED)
14101da177e4SLinus Torvalds 		goto dont_forward;
14111da177e4SLinus Torvalds 
14121da177e4SLinus Torvalds 	if (!local) {
14131da177e4SLinus Torvalds 		    if (IPCB(skb)->opt.router_alert) {
14141da177e4SLinus Torvalds 			    if (ip_call_ra_chain(skb))
14151da177e4SLinus Torvalds 				    return 0;
1416eddc9ec5SArnaldo Carvalho de Melo 		    } else if (ip_hdr(skb)->protocol == IPPROTO_IGMP){
14171da177e4SLinus Torvalds 			    /* IGMPv1 (and broken IGMPv2 implementations sort of
14181da177e4SLinus Torvalds 			       Cisco IOS <= 11.2(8)) do not put router alert
14191da177e4SLinus Torvalds 			       option to IGMP packets destined to routable
14201da177e4SLinus Torvalds 			       groups. It is very bad, because it means
14211da177e4SLinus Torvalds 			       that we can forward NO IGMP messages.
14221da177e4SLinus Torvalds 			     */
14231da177e4SLinus Torvalds 			    read_lock(&mrt_lock);
14241da177e4SLinus Torvalds 			    if (mroute_socket) {
14252715bcf9SPatrick McHardy 				    nf_reset(skb);
14261da177e4SLinus Torvalds 				    raw_rcv(mroute_socket, skb);
14271da177e4SLinus Torvalds 				    read_unlock(&mrt_lock);
14281da177e4SLinus Torvalds 				    return 0;
14291da177e4SLinus Torvalds 			    }
14301da177e4SLinus Torvalds 			    read_unlock(&mrt_lock);
14311da177e4SLinus Torvalds 		    }
14321da177e4SLinus Torvalds 	}
14331da177e4SLinus Torvalds 
14341da177e4SLinus Torvalds 	read_lock(&mrt_lock);
1435eddc9ec5SArnaldo Carvalho de Melo 	cache = ipmr_cache_find(ip_hdr(skb)->saddr, ip_hdr(skb)->daddr);
14361da177e4SLinus Torvalds 
14371da177e4SLinus Torvalds 	/*
14381da177e4SLinus Torvalds 	 *	No usable cache entry
14391da177e4SLinus Torvalds 	 */
14401da177e4SLinus Torvalds 	if (cache == NULL) {
14411da177e4SLinus Torvalds 		int vif;
14421da177e4SLinus Torvalds 
14431da177e4SLinus Torvalds 		if (local) {
14441da177e4SLinus Torvalds 			struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
14451da177e4SLinus Torvalds 			ip_local_deliver(skb);
14461da177e4SLinus Torvalds 			if (skb2 == NULL) {
14471da177e4SLinus Torvalds 				read_unlock(&mrt_lock);
14481da177e4SLinus Torvalds 				return -ENOBUFS;
14491da177e4SLinus Torvalds 			}
14501da177e4SLinus Torvalds 			skb = skb2;
14511da177e4SLinus Torvalds 		}
14521da177e4SLinus Torvalds 
14531da177e4SLinus Torvalds 		vif = ipmr_find_vif(skb->dev);
14541da177e4SLinus Torvalds 		if (vif >= 0) {
14551da177e4SLinus Torvalds 			int err = ipmr_cache_unresolved(vif, skb);
14561da177e4SLinus Torvalds 			read_unlock(&mrt_lock);
14571da177e4SLinus Torvalds 
14581da177e4SLinus Torvalds 			return err;
14591da177e4SLinus Torvalds 		}
14601da177e4SLinus Torvalds 		read_unlock(&mrt_lock);
14611da177e4SLinus Torvalds 		kfree_skb(skb);
14621da177e4SLinus Torvalds 		return -ENODEV;
14631da177e4SLinus Torvalds 	}
14641da177e4SLinus Torvalds 
14651da177e4SLinus Torvalds 	ip_mr_forward(skb, cache, local);
14661da177e4SLinus Torvalds 
14671da177e4SLinus Torvalds 	read_unlock(&mrt_lock);
14681da177e4SLinus Torvalds 
14691da177e4SLinus Torvalds 	if (local)
14701da177e4SLinus Torvalds 		return ip_local_deliver(skb);
14711da177e4SLinus Torvalds 
14721da177e4SLinus Torvalds 	return 0;
14731da177e4SLinus Torvalds 
14741da177e4SLinus Torvalds dont_forward:
14751da177e4SLinus Torvalds 	if (local)
14761da177e4SLinus Torvalds 		return ip_local_deliver(skb);
14771da177e4SLinus Torvalds 	kfree_skb(skb);
14781da177e4SLinus Torvalds 	return 0;
14791da177e4SLinus Torvalds }
14801da177e4SLinus Torvalds 
14811da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM_V1
14821da177e4SLinus Torvalds /*
14831da177e4SLinus Torvalds  * Handle IGMP messages of PIMv1
14841da177e4SLinus Torvalds  */
14851da177e4SLinus Torvalds 
14861da177e4SLinus Torvalds int pim_rcv_v1(struct sk_buff * skb)
14871da177e4SLinus Torvalds {
14881da177e4SLinus Torvalds 	struct igmphdr *pim;
14891da177e4SLinus Torvalds 	struct iphdr   *encap;
14901da177e4SLinus Torvalds 	struct net_device  *reg_dev = NULL;
14911da177e4SLinus Torvalds 
14921da177e4SLinus Torvalds 	if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(*encap)))
14931da177e4SLinus Torvalds 		goto drop;
14941da177e4SLinus Torvalds 
1495d9edf9e2SArnaldo Carvalho de Melo 	pim = igmp_hdr(skb);
14961da177e4SLinus Torvalds 
14971da177e4SLinus Torvalds 	if (!mroute_do_pim ||
14981da177e4SLinus Torvalds 	    skb->len < sizeof(*pim) + sizeof(*encap) ||
14991da177e4SLinus Torvalds 	    pim->group != PIM_V1_VERSION || pim->code != PIM_V1_REGISTER)
15001da177e4SLinus Torvalds 		goto drop;
15011da177e4SLinus Torvalds 
15029c70220bSArnaldo Carvalho de Melo 	encap = (struct iphdr *)(skb_transport_header(skb) +
15039c70220bSArnaldo Carvalho de Melo 				 sizeof(struct igmphdr));
15041da177e4SLinus Torvalds 	/*
15051da177e4SLinus Torvalds 	   Check that:
15061da177e4SLinus Torvalds 	   a. packet is really destinted to a multicast group
15071da177e4SLinus Torvalds 	   b. packet is not a NULL-REGISTER
15081da177e4SLinus Torvalds 	   c. packet is not truncated
15091da177e4SLinus Torvalds 	 */
1510f97c1e0cSJoe Perches 	if (!ipv4_is_multicast(encap->daddr) ||
15111da177e4SLinus Torvalds 	    encap->tot_len == 0 ||
15121da177e4SLinus Torvalds 	    ntohs(encap->tot_len) + sizeof(*pim) > skb->len)
15131da177e4SLinus Torvalds 		goto drop;
15141da177e4SLinus Torvalds 
15151da177e4SLinus Torvalds 	read_lock(&mrt_lock);
15161da177e4SLinus Torvalds 	if (reg_vif_num >= 0)
15171da177e4SLinus Torvalds 		reg_dev = vif_table[reg_vif_num].dev;
15181da177e4SLinus Torvalds 	if (reg_dev)
15191da177e4SLinus Torvalds 		dev_hold(reg_dev);
15201da177e4SLinus Torvalds 	read_unlock(&mrt_lock);
15211da177e4SLinus Torvalds 
15221da177e4SLinus Torvalds 	if (reg_dev == NULL)
15231da177e4SLinus Torvalds 		goto drop;
15241da177e4SLinus Torvalds 
1525b0e380b1SArnaldo Carvalho de Melo 	skb->mac_header = skb->network_header;
15261da177e4SLinus Torvalds 	skb_pull(skb, (u8*)encap - skb->data);
152731c7711bSArnaldo Carvalho de Melo 	skb_reset_network_header(skb);
15281da177e4SLinus Torvalds 	skb->dev = reg_dev;
15291da177e4SLinus Torvalds 	skb->protocol = htons(ETH_P_IP);
15301da177e4SLinus Torvalds 	skb->ip_summed = 0;
15311da177e4SLinus Torvalds 	skb->pkt_type = PACKET_HOST;
15321da177e4SLinus Torvalds 	dst_release(skb->dst);
15331da177e4SLinus Torvalds 	skb->dst = NULL;
1534cf3677aeSPavel Emelyanov 	reg_dev->stats.rx_bytes += skb->len;
1535cf3677aeSPavel Emelyanov 	reg_dev->stats.rx_packets++;
15361da177e4SLinus Torvalds 	nf_reset(skb);
15371da177e4SLinus Torvalds 	netif_rx(skb);
15381da177e4SLinus Torvalds 	dev_put(reg_dev);
15391da177e4SLinus Torvalds 	return 0;
15401da177e4SLinus Torvalds  drop:
15411da177e4SLinus Torvalds 	kfree_skb(skb);
15421da177e4SLinus Torvalds 	return 0;
15431da177e4SLinus Torvalds }
15441da177e4SLinus Torvalds #endif
15451da177e4SLinus Torvalds 
15461da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM_V2
15471da177e4SLinus Torvalds static int pim_rcv(struct sk_buff * skb)
15481da177e4SLinus Torvalds {
15491da177e4SLinus Torvalds 	struct pimreghdr *pim;
15501da177e4SLinus Torvalds 	struct iphdr   *encap;
15511da177e4SLinus Torvalds 	struct net_device  *reg_dev = NULL;
15521da177e4SLinus Torvalds 
15531da177e4SLinus Torvalds 	if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(*encap)))
15541da177e4SLinus Torvalds 		goto drop;
15551da177e4SLinus Torvalds 
15569c70220bSArnaldo Carvalho de Melo 	pim = (struct pimreghdr *)skb_transport_header(skb);
15571da177e4SLinus Torvalds 	if (pim->type != ((PIM_VERSION<<4)|(PIM_REGISTER)) ||
15581da177e4SLinus Torvalds 	    (pim->flags&PIM_NULL_REGISTER) ||
15591da177e4SLinus Torvalds 	    (ip_compute_csum((void *)pim, sizeof(*pim)) != 0 &&
1560d3bc23e7SAl Viro 	     csum_fold(skb_checksum(skb, 0, skb->len, 0))))
15611da177e4SLinus Torvalds 		goto drop;
15621da177e4SLinus Torvalds 
15631da177e4SLinus Torvalds 	/* check if the inner packet is destined to mcast group */
15649c70220bSArnaldo Carvalho de Melo 	encap = (struct iphdr *)(skb_transport_header(skb) +
15659c70220bSArnaldo Carvalho de Melo 				 sizeof(struct pimreghdr));
1566f97c1e0cSJoe Perches 	if (!ipv4_is_multicast(encap->daddr) ||
15671da177e4SLinus Torvalds 	    encap->tot_len == 0 ||
15681da177e4SLinus Torvalds 	    ntohs(encap->tot_len) + sizeof(*pim) > skb->len)
15691da177e4SLinus Torvalds 		goto drop;
15701da177e4SLinus Torvalds 
15711da177e4SLinus Torvalds 	read_lock(&mrt_lock);
15721da177e4SLinus Torvalds 	if (reg_vif_num >= 0)
15731da177e4SLinus Torvalds 		reg_dev = vif_table[reg_vif_num].dev;
15741da177e4SLinus Torvalds 	if (reg_dev)
15751da177e4SLinus Torvalds 		dev_hold(reg_dev);
15761da177e4SLinus Torvalds 	read_unlock(&mrt_lock);
15771da177e4SLinus Torvalds 
15781da177e4SLinus Torvalds 	if (reg_dev == NULL)
15791da177e4SLinus Torvalds 		goto drop;
15801da177e4SLinus Torvalds 
1581b0e380b1SArnaldo Carvalho de Melo 	skb->mac_header = skb->network_header;
15821da177e4SLinus Torvalds 	skb_pull(skb, (u8*)encap - skb->data);
158331c7711bSArnaldo Carvalho de Melo 	skb_reset_network_header(skb);
15841da177e4SLinus Torvalds 	skb->dev = reg_dev;
15851da177e4SLinus Torvalds 	skb->protocol = htons(ETH_P_IP);
15861da177e4SLinus Torvalds 	skb->ip_summed = 0;
15871da177e4SLinus Torvalds 	skb->pkt_type = PACKET_HOST;
15881da177e4SLinus Torvalds 	dst_release(skb->dst);
1589cf3677aeSPavel Emelyanov 	reg_dev->stats.rx_bytes += skb->len;
1590cf3677aeSPavel Emelyanov 	reg_dev->stats.rx_packets++;
15911da177e4SLinus Torvalds 	skb->dst = NULL;
15921da177e4SLinus Torvalds 	nf_reset(skb);
15931da177e4SLinus Torvalds 	netif_rx(skb);
15941da177e4SLinus Torvalds 	dev_put(reg_dev);
15951da177e4SLinus Torvalds 	return 0;
15961da177e4SLinus Torvalds  drop:
15971da177e4SLinus Torvalds 	kfree_skb(skb);
15981da177e4SLinus Torvalds 	return 0;
15991da177e4SLinus Torvalds }
16001da177e4SLinus Torvalds #endif
16011da177e4SLinus Torvalds 
16021da177e4SLinus Torvalds static int
16031da177e4SLinus Torvalds ipmr_fill_mroute(struct sk_buff *skb, struct mfc_cache *c, struct rtmsg *rtm)
16041da177e4SLinus Torvalds {
16051da177e4SLinus Torvalds 	int ct;
16061da177e4SLinus Torvalds 	struct rtnexthop *nhp;
16071da177e4SLinus Torvalds 	struct net_device *dev = vif_table[c->mfc_parent].dev;
160827a884dcSArnaldo Carvalho de Melo 	u8 *b = skb_tail_pointer(skb);
16091da177e4SLinus Torvalds 	struct rtattr *mp_head;
16101da177e4SLinus Torvalds 
16111da177e4SLinus Torvalds 	if (dev)
16121da177e4SLinus Torvalds 		RTA_PUT(skb, RTA_IIF, 4, &dev->ifindex);
16131da177e4SLinus Torvalds 
16141da177e4SLinus Torvalds 	mp_head = (struct rtattr *)skb_put(skb, RTA_LENGTH(0));
16151da177e4SLinus Torvalds 
16161da177e4SLinus Torvalds 	for (ct = c->mfc_un.res.minvif; ct < c->mfc_un.res.maxvif; ct++) {
16171da177e4SLinus Torvalds 		if (c->mfc_un.res.ttls[ct] < 255) {
16181da177e4SLinus Torvalds 			if (skb_tailroom(skb) < RTA_ALIGN(RTA_ALIGN(sizeof(*nhp)) + 4))
16191da177e4SLinus Torvalds 				goto rtattr_failure;
16201da177e4SLinus Torvalds 			nhp = (struct rtnexthop *)skb_put(skb, RTA_ALIGN(sizeof(*nhp)));
16211da177e4SLinus Torvalds 			nhp->rtnh_flags = 0;
16221da177e4SLinus Torvalds 			nhp->rtnh_hops = c->mfc_un.res.ttls[ct];
16231da177e4SLinus Torvalds 			nhp->rtnh_ifindex = vif_table[ct].dev->ifindex;
16241da177e4SLinus Torvalds 			nhp->rtnh_len = sizeof(*nhp);
16251da177e4SLinus Torvalds 		}
16261da177e4SLinus Torvalds 	}
16271da177e4SLinus Torvalds 	mp_head->rta_type = RTA_MULTIPATH;
162827a884dcSArnaldo Carvalho de Melo 	mp_head->rta_len = skb_tail_pointer(skb) - (u8 *)mp_head;
16291da177e4SLinus Torvalds 	rtm->rtm_type = RTN_MULTICAST;
16301da177e4SLinus Torvalds 	return 1;
16311da177e4SLinus Torvalds 
16321da177e4SLinus Torvalds rtattr_failure:
1633dc5fc579SArnaldo Carvalho de Melo 	nlmsg_trim(skb, b);
16341da177e4SLinus Torvalds 	return -EMSGSIZE;
16351da177e4SLinus Torvalds }
16361da177e4SLinus Torvalds 
16371da177e4SLinus Torvalds int ipmr_get_route(struct sk_buff *skb, struct rtmsg *rtm, int nowait)
16381da177e4SLinus Torvalds {
16391da177e4SLinus Torvalds 	int err;
16401da177e4SLinus Torvalds 	struct mfc_cache *cache;
1641ee6b9673SEric Dumazet 	struct rtable *rt = skb->rtable;
16421da177e4SLinus Torvalds 
16431da177e4SLinus Torvalds 	read_lock(&mrt_lock);
16441da177e4SLinus Torvalds 	cache = ipmr_cache_find(rt->rt_src, rt->rt_dst);
16451da177e4SLinus Torvalds 
16461da177e4SLinus Torvalds 	if (cache == NULL) {
164772287490SAlexey Kuznetsov 		struct sk_buff *skb2;
1648eddc9ec5SArnaldo Carvalho de Melo 		struct iphdr *iph;
16491da177e4SLinus Torvalds 		struct net_device *dev;
16501da177e4SLinus Torvalds 		int vif;
16511da177e4SLinus Torvalds 
16521da177e4SLinus Torvalds 		if (nowait) {
16531da177e4SLinus Torvalds 			read_unlock(&mrt_lock);
16541da177e4SLinus Torvalds 			return -EAGAIN;
16551da177e4SLinus Torvalds 		}
16561da177e4SLinus Torvalds 
16571da177e4SLinus Torvalds 		dev = skb->dev;
16581da177e4SLinus Torvalds 		if (dev == NULL || (vif = ipmr_find_vif(dev)) < 0) {
16591da177e4SLinus Torvalds 			read_unlock(&mrt_lock);
16601da177e4SLinus Torvalds 			return -ENODEV;
16611da177e4SLinus Torvalds 		}
166272287490SAlexey Kuznetsov 		skb2 = skb_clone(skb, GFP_ATOMIC);
166372287490SAlexey Kuznetsov 		if (!skb2) {
166472287490SAlexey Kuznetsov 			read_unlock(&mrt_lock);
166572287490SAlexey Kuznetsov 			return -ENOMEM;
166672287490SAlexey Kuznetsov 		}
166772287490SAlexey Kuznetsov 
1668e2d1bca7SArnaldo Carvalho de Melo 		skb_push(skb2, sizeof(struct iphdr));
1669e2d1bca7SArnaldo Carvalho de Melo 		skb_reset_network_header(skb2);
1670eddc9ec5SArnaldo Carvalho de Melo 		iph = ip_hdr(skb2);
1671eddc9ec5SArnaldo Carvalho de Melo 		iph->ihl = sizeof(struct iphdr) >> 2;
1672eddc9ec5SArnaldo Carvalho de Melo 		iph->saddr = rt->rt_src;
1673eddc9ec5SArnaldo Carvalho de Melo 		iph->daddr = rt->rt_dst;
1674eddc9ec5SArnaldo Carvalho de Melo 		iph->version = 0;
167572287490SAlexey Kuznetsov 		err = ipmr_cache_unresolved(vif, skb2);
16761da177e4SLinus Torvalds 		read_unlock(&mrt_lock);
16771da177e4SLinus Torvalds 		return err;
16781da177e4SLinus Torvalds 	}
16791da177e4SLinus Torvalds 
16801da177e4SLinus Torvalds 	if (!nowait && (rtm->rtm_flags&RTM_F_NOTIFY))
16811da177e4SLinus Torvalds 		cache->mfc_flags |= MFC_NOTIFY;
16821da177e4SLinus Torvalds 	err = ipmr_fill_mroute(skb, cache, rtm);
16831da177e4SLinus Torvalds 	read_unlock(&mrt_lock);
16841da177e4SLinus Torvalds 	return err;
16851da177e4SLinus Torvalds }
16861da177e4SLinus Torvalds 
16871da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
16881da177e4SLinus Torvalds /*
16891da177e4SLinus Torvalds  *	The /proc interfaces to multicast routing /proc/ip_mr_cache /proc/ip_mr_vif
16901da177e4SLinus Torvalds  */
16911da177e4SLinus Torvalds struct ipmr_vif_iter {
16921da177e4SLinus Torvalds 	int ct;
16931da177e4SLinus Torvalds };
16941da177e4SLinus Torvalds 
16951da177e4SLinus Torvalds static struct vif_device *ipmr_vif_seq_idx(struct ipmr_vif_iter *iter,
16961da177e4SLinus Torvalds 					   loff_t pos)
16971da177e4SLinus Torvalds {
16981da177e4SLinus Torvalds 	for (iter->ct = 0; iter->ct < maxvif; ++iter->ct) {
16991da177e4SLinus Torvalds 		if (!VIF_EXISTS(iter->ct))
17001da177e4SLinus Torvalds 			continue;
17011da177e4SLinus Torvalds 		if (pos-- == 0)
17021da177e4SLinus Torvalds 			return &vif_table[iter->ct];
17031da177e4SLinus Torvalds 	}
17041da177e4SLinus Torvalds 	return NULL;
17051da177e4SLinus Torvalds }
17061da177e4SLinus Torvalds 
17071da177e4SLinus Torvalds static void *ipmr_vif_seq_start(struct seq_file *seq, loff_t *pos)
1708ba93ef74SStephen Hemminger 	__acquires(mrt_lock)
17091da177e4SLinus Torvalds {
17101da177e4SLinus Torvalds 	read_lock(&mrt_lock);
17111da177e4SLinus Torvalds 	return *pos ? ipmr_vif_seq_idx(seq->private, *pos - 1)
17121da177e4SLinus Torvalds 		: SEQ_START_TOKEN;
17131da177e4SLinus Torvalds }
17141da177e4SLinus Torvalds 
17151da177e4SLinus Torvalds static void *ipmr_vif_seq_next(struct seq_file *seq, void *v, loff_t *pos)
17161da177e4SLinus Torvalds {
17171da177e4SLinus Torvalds 	struct ipmr_vif_iter *iter = seq->private;
17181da177e4SLinus Torvalds 
17191da177e4SLinus Torvalds 	++*pos;
17201da177e4SLinus Torvalds 	if (v == SEQ_START_TOKEN)
17211da177e4SLinus Torvalds 		return ipmr_vif_seq_idx(iter, 0);
17221da177e4SLinus Torvalds 
17231da177e4SLinus Torvalds 	while (++iter->ct < maxvif) {
17241da177e4SLinus Torvalds 		if (!VIF_EXISTS(iter->ct))
17251da177e4SLinus Torvalds 			continue;
17261da177e4SLinus Torvalds 		return &vif_table[iter->ct];
17271da177e4SLinus Torvalds 	}
17281da177e4SLinus Torvalds 	return NULL;
17291da177e4SLinus Torvalds }
17301da177e4SLinus Torvalds 
17311da177e4SLinus Torvalds static void ipmr_vif_seq_stop(struct seq_file *seq, void *v)
1732ba93ef74SStephen Hemminger 	__releases(mrt_lock)
17331da177e4SLinus Torvalds {
17341da177e4SLinus Torvalds 	read_unlock(&mrt_lock);
17351da177e4SLinus Torvalds }
17361da177e4SLinus Torvalds 
17371da177e4SLinus Torvalds static int ipmr_vif_seq_show(struct seq_file *seq, void *v)
17381da177e4SLinus Torvalds {
17391da177e4SLinus Torvalds 	if (v == SEQ_START_TOKEN) {
17401da177e4SLinus Torvalds 		seq_puts(seq,
17411da177e4SLinus Torvalds 			 "Interface      BytesIn  PktsIn  BytesOut PktsOut Flags Local    Remote\n");
17421da177e4SLinus Torvalds 	} else {
17431da177e4SLinus Torvalds 		const struct vif_device *vif = v;
17441da177e4SLinus Torvalds 		const char *name =  vif->dev ? vif->dev->name : "none";
17451da177e4SLinus Torvalds 
17461da177e4SLinus Torvalds 		seq_printf(seq,
17471da177e4SLinus Torvalds 			   "%2Zd %-10s %8ld %7ld  %8ld %7ld %05X %08X %08X\n",
17481da177e4SLinus Torvalds 			   vif - vif_table,
17491da177e4SLinus Torvalds 			   name, vif->bytes_in, vif->pkt_in,
17501da177e4SLinus Torvalds 			   vif->bytes_out, vif->pkt_out,
17511da177e4SLinus Torvalds 			   vif->flags, vif->local, vif->remote);
17521da177e4SLinus Torvalds 	}
17531da177e4SLinus Torvalds 	return 0;
17541da177e4SLinus Torvalds }
17551da177e4SLinus Torvalds 
1756f690808eSStephen Hemminger static const struct seq_operations ipmr_vif_seq_ops = {
17571da177e4SLinus Torvalds 	.start = ipmr_vif_seq_start,
17581da177e4SLinus Torvalds 	.next  = ipmr_vif_seq_next,
17591da177e4SLinus Torvalds 	.stop  = ipmr_vif_seq_stop,
17601da177e4SLinus Torvalds 	.show  = ipmr_vif_seq_show,
17611da177e4SLinus Torvalds };
17621da177e4SLinus Torvalds 
17631da177e4SLinus Torvalds static int ipmr_vif_open(struct inode *inode, struct file *file)
17641da177e4SLinus Torvalds {
1765cf7732e4SPavel Emelyanov 	return seq_open_private(file, &ipmr_vif_seq_ops,
1766cf7732e4SPavel Emelyanov 			sizeof(struct ipmr_vif_iter));
17671da177e4SLinus Torvalds }
17681da177e4SLinus Torvalds 
17699a32144eSArjan van de Ven static const struct file_operations ipmr_vif_fops = {
17701da177e4SLinus Torvalds 	.owner	 = THIS_MODULE,
17711da177e4SLinus Torvalds 	.open    = ipmr_vif_open,
17721da177e4SLinus Torvalds 	.read    = seq_read,
17731da177e4SLinus Torvalds 	.llseek  = seq_lseek,
17741da177e4SLinus Torvalds 	.release = seq_release_private,
17751da177e4SLinus Torvalds };
17761da177e4SLinus Torvalds 
17771da177e4SLinus Torvalds struct ipmr_mfc_iter {
17781da177e4SLinus Torvalds 	struct mfc_cache **cache;
17791da177e4SLinus Torvalds 	int ct;
17801da177e4SLinus Torvalds };
17811da177e4SLinus Torvalds 
17821da177e4SLinus Torvalds 
17831da177e4SLinus Torvalds static struct mfc_cache *ipmr_mfc_seq_idx(struct ipmr_mfc_iter *it, loff_t pos)
17841da177e4SLinus Torvalds {
17851da177e4SLinus Torvalds 	struct mfc_cache *mfc;
17861da177e4SLinus Torvalds 
17871da177e4SLinus Torvalds 	it->cache = mfc_cache_array;
17881da177e4SLinus Torvalds 	read_lock(&mrt_lock);
17891da177e4SLinus Torvalds 	for (it->ct = 0; it->ct < MFC_LINES; it->ct++)
17901da177e4SLinus Torvalds 		for (mfc = mfc_cache_array[it->ct]; mfc; mfc = mfc->next)
17911da177e4SLinus Torvalds 			if (pos-- == 0)
17921da177e4SLinus Torvalds 				return mfc;
17931da177e4SLinus Torvalds 	read_unlock(&mrt_lock);
17941da177e4SLinus Torvalds 
17951da177e4SLinus Torvalds 	it->cache = &mfc_unres_queue;
17961da177e4SLinus Torvalds 	spin_lock_bh(&mfc_unres_lock);
17971da177e4SLinus Torvalds 	for (mfc = mfc_unres_queue; mfc; mfc = mfc->next)
17981da177e4SLinus Torvalds 		if (pos-- == 0)
17991da177e4SLinus Torvalds 			return mfc;
18001da177e4SLinus Torvalds 	spin_unlock_bh(&mfc_unres_lock);
18011da177e4SLinus Torvalds 
18021da177e4SLinus Torvalds 	it->cache = NULL;
18031da177e4SLinus Torvalds 	return NULL;
18041da177e4SLinus Torvalds }
18051da177e4SLinus Torvalds 
18061da177e4SLinus Torvalds 
18071da177e4SLinus Torvalds static void *ipmr_mfc_seq_start(struct seq_file *seq, loff_t *pos)
18081da177e4SLinus Torvalds {
18091da177e4SLinus Torvalds 	struct ipmr_mfc_iter *it = seq->private;
18101da177e4SLinus Torvalds 	it->cache = NULL;
18111da177e4SLinus Torvalds 	it->ct = 0;
18121da177e4SLinus Torvalds 	return *pos ? ipmr_mfc_seq_idx(seq->private, *pos - 1)
18131da177e4SLinus Torvalds 		: SEQ_START_TOKEN;
18141da177e4SLinus Torvalds }
18151da177e4SLinus Torvalds 
18161da177e4SLinus Torvalds static void *ipmr_mfc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
18171da177e4SLinus Torvalds {
18181da177e4SLinus Torvalds 	struct mfc_cache *mfc = v;
18191da177e4SLinus Torvalds 	struct ipmr_mfc_iter *it = seq->private;
18201da177e4SLinus Torvalds 
18211da177e4SLinus Torvalds 	++*pos;
18221da177e4SLinus Torvalds 
18231da177e4SLinus Torvalds 	if (v == SEQ_START_TOKEN)
18241da177e4SLinus Torvalds 		return ipmr_mfc_seq_idx(seq->private, 0);
18251da177e4SLinus Torvalds 
18261da177e4SLinus Torvalds 	if (mfc->next)
18271da177e4SLinus Torvalds 		return mfc->next;
18281da177e4SLinus Torvalds 
18291da177e4SLinus Torvalds 	if (it->cache == &mfc_unres_queue)
18301da177e4SLinus Torvalds 		goto end_of_list;
18311da177e4SLinus Torvalds 
18321da177e4SLinus Torvalds 	BUG_ON(it->cache != mfc_cache_array);
18331da177e4SLinus Torvalds 
18341da177e4SLinus Torvalds 	while (++it->ct < MFC_LINES) {
18351da177e4SLinus Torvalds 		mfc = mfc_cache_array[it->ct];
18361da177e4SLinus Torvalds 		if (mfc)
18371da177e4SLinus Torvalds 			return mfc;
18381da177e4SLinus Torvalds 	}
18391da177e4SLinus Torvalds 
18401da177e4SLinus Torvalds 	/* exhausted cache_array, show unresolved */
18411da177e4SLinus Torvalds 	read_unlock(&mrt_lock);
18421da177e4SLinus Torvalds 	it->cache = &mfc_unres_queue;
18431da177e4SLinus Torvalds 	it->ct = 0;
18441da177e4SLinus Torvalds 
18451da177e4SLinus Torvalds 	spin_lock_bh(&mfc_unres_lock);
18461da177e4SLinus Torvalds 	mfc = mfc_unres_queue;
18471da177e4SLinus Torvalds 	if (mfc)
18481da177e4SLinus Torvalds 		return mfc;
18491da177e4SLinus Torvalds 
18501da177e4SLinus Torvalds  end_of_list:
18511da177e4SLinus Torvalds 	spin_unlock_bh(&mfc_unres_lock);
18521da177e4SLinus Torvalds 	it->cache = NULL;
18531da177e4SLinus Torvalds 
18541da177e4SLinus Torvalds 	return NULL;
18551da177e4SLinus Torvalds }
18561da177e4SLinus Torvalds 
18571da177e4SLinus Torvalds static void ipmr_mfc_seq_stop(struct seq_file *seq, void *v)
18581da177e4SLinus Torvalds {
18591da177e4SLinus Torvalds 	struct ipmr_mfc_iter *it = seq->private;
18601da177e4SLinus Torvalds 
18611da177e4SLinus Torvalds 	if (it->cache == &mfc_unres_queue)
18621da177e4SLinus Torvalds 		spin_unlock_bh(&mfc_unres_lock);
18631da177e4SLinus Torvalds 	else if (it->cache == mfc_cache_array)
18641da177e4SLinus Torvalds 		read_unlock(&mrt_lock);
18651da177e4SLinus Torvalds }
18661da177e4SLinus Torvalds 
18671da177e4SLinus Torvalds static int ipmr_mfc_seq_show(struct seq_file *seq, void *v)
18681da177e4SLinus Torvalds {
18691da177e4SLinus Torvalds 	int n;
18701da177e4SLinus Torvalds 
18711da177e4SLinus Torvalds 	if (v == SEQ_START_TOKEN) {
18721da177e4SLinus Torvalds 		seq_puts(seq,
18731da177e4SLinus Torvalds 		 "Group    Origin   Iif     Pkts    Bytes    Wrong Oifs\n");
18741da177e4SLinus Torvalds 	} else {
18751da177e4SLinus Torvalds 		const struct mfc_cache *mfc = v;
18761da177e4SLinus Torvalds 		const struct ipmr_mfc_iter *it = seq->private;
18771da177e4SLinus Torvalds 
18781da177e4SLinus Torvalds 		seq_printf(seq, "%08lX %08lX %-3d %8ld %8ld %8ld",
18791da177e4SLinus Torvalds 			   (unsigned long) mfc->mfc_mcastgrp,
18801da177e4SLinus Torvalds 			   (unsigned long) mfc->mfc_origin,
18811da177e4SLinus Torvalds 			   mfc->mfc_parent,
18821da177e4SLinus Torvalds 			   mfc->mfc_un.res.pkt,
18831da177e4SLinus Torvalds 			   mfc->mfc_un.res.bytes,
18841da177e4SLinus Torvalds 			   mfc->mfc_un.res.wrong_if);
18851da177e4SLinus Torvalds 
18861da177e4SLinus Torvalds 		if (it->cache != &mfc_unres_queue) {
18871da177e4SLinus Torvalds 			for (n = mfc->mfc_un.res.minvif;
18881da177e4SLinus Torvalds 			     n < mfc->mfc_un.res.maxvif; n++ ) {
18891da177e4SLinus Torvalds 				if (VIF_EXISTS(n)
18901da177e4SLinus Torvalds 				   && mfc->mfc_un.res.ttls[n] < 255)
18911da177e4SLinus Torvalds 				seq_printf(seq,
18921da177e4SLinus Torvalds 					   " %2d:%-3d",
18931da177e4SLinus Torvalds 					   n, mfc->mfc_un.res.ttls[n]);
18941da177e4SLinus Torvalds 			}
18951da177e4SLinus Torvalds 		}
18961da177e4SLinus Torvalds 		seq_putc(seq, '\n');
18971da177e4SLinus Torvalds 	}
18981da177e4SLinus Torvalds 	return 0;
18991da177e4SLinus Torvalds }
19001da177e4SLinus Torvalds 
1901f690808eSStephen Hemminger static const struct seq_operations ipmr_mfc_seq_ops = {
19021da177e4SLinus Torvalds 	.start = ipmr_mfc_seq_start,
19031da177e4SLinus Torvalds 	.next  = ipmr_mfc_seq_next,
19041da177e4SLinus Torvalds 	.stop  = ipmr_mfc_seq_stop,
19051da177e4SLinus Torvalds 	.show  = ipmr_mfc_seq_show,
19061da177e4SLinus Torvalds };
19071da177e4SLinus Torvalds 
19081da177e4SLinus Torvalds static int ipmr_mfc_open(struct inode *inode, struct file *file)
19091da177e4SLinus Torvalds {
1910cf7732e4SPavel Emelyanov 	return seq_open_private(file, &ipmr_mfc_seq_ops,
1911cf7732e4SPavel Emelyanov 			sizeof(struct ipmr_mfc_iter));
19121da177e4SLinus Torvalds }
19131da177e4SLinus Torvalds 
19149a32144eSArjan van de Ven static const struct file_operations ipmr_mfc_fops = {
19151da177e4SLinus Torvalds 	.owner	 = THIS_MODULE,
19161da177e4SLinus Torvalds 	.open    = ipmr_mfc_open,
19171da177e4SLinus Torvalds 	.read    = seq_read,
19181da177e4SLinus Torvalds 	.llseek  = seq_lseek,
19191da177e4SLinus Torvalds 	.release = seq_release_private,
19201da177e4SLinus Torvalds };
19211da177e4SLinus Torvalds #endif
19221da177e4SLinus Torvalds 
19231da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM_V2
19241da177e4SLinus Torvalds static struct net_protocol pim_protocol = {
19251da177e4SLinus Torvalds 	.handler	=	pim_rcv,
19261da177e4SLinus Torvalds };
19271da177e4SLinus Torvalds #endif
19281da177e4SLinus Torvalds 
19291da177e4SLinus Torvalds 
19301da177e4SLinus Torvalds /*
19311da177e4SLinus Torvalds  *	Setup for IP multicast routing
19321da177e4SLinus Torvalds  */
19331da177e4SLinus Torvalds 
193403d2f897SWang Chen int __init ip_mr_init(void)
19351da177e4SLinus Torvalds {
193603d2f897SWang Chen 	int err;
193703d2f897SWang Chen 
19381da177e4SLinus Torvalds 	mrt_cachep = kmem_cache_create("ip_mrt_cache",
19391da177e4SLinus Torvalds 				       sizeof(struct mfc_cache),
1940e5d679f3SAlexey Dobriyan 				       0, SLAB_HWCACHE_ALIGN|SLAB_PANIC,
194120c2df83SPaul Mundt 				       NULL);
194203d2f897SWang Chen 	if (!mrt_cachep)
194303d2f897SWang Chen 		return -ENOMEM;
194403d2f897SWang Chen 
1945b24b8a24SPavel Emelyanov 	setup_timer(&ipmr_expire_timer, ipmr_expire_process, 0);
194603d2f897SWang Chen 	err = register_netdevice_notifier(&ip_mr_notifier);
194703d2f897SWang Chen 	if (err)
194803d2f897SWang Chen 		goto reg_notif_fail;
19491da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
195003d2f897SWang Chen 	err = -ENOMEM;
195103d2f897SWang Chen 	if (!proc_net_fops_create(&init_net, "ip_mr_vif", 0, &ipmr_vif_fops))
195203d2f897SWang Chen 		goto proc_vif_fail;
195303d2f897SWang Chen 	if (!proc_net_fops_create(&init_net, "ip_mr_cache", 0, &ipmr_mfc_fops))
195403d2f897SWang Chen 		goto proc_cache_fail;
19551da177e4SLinus Torvalds #endif
195603d2f897SWang Chen 	return 0;
195703d2f897SWang Chen reg_notif_fail:
195803d2f897SWang Chen 	kmem_cache_destroy(mrt_cachep);
195903d2f897SWang Chen #ifdef CONFIG_PROC_FS
196003d2f897SWang Chen proc_vif_fail:
196103d2f897SWang Chen 	unregister_netdevice_notifier(&ip_mr_notifier);
196203d2f897SWang Chen proc_cache_fail:
196303d2f897SWang Chen 	proc_net_remove(&init_net, "ip_mr_vif");
196403d2f897SWang Chen #endif
196503d2f897SWang Chen 	return err;
19661da177e4SLinus Torvalds }
1967