xref: /linux/net/ipv4/ipmr.c (revision d3bc23e7ee9db8023dff5a86bb3b0069ed018789)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *	IP multicast routing support for mrouted 3.6/3.8
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *		(c) 1995 Alan Cox, <alan@redhat.com>
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  *	Version: $Id: ipmr.c,v 1.65 2001/10/31 21:55:54 davem Exp $
131da177e4SLinus Torvalds  *
141da177e4SLinus Torvalds  *	Fixes:
151da177e4SLinus Torvalds  *	Michael Chastain	:	Incorrect size of copying.
161da177e4SLinus Torvalds  *	Alan Cox		:	Added the cache manager code
171da177e4SLinus Torvalds  *	Alan Cox		:	Fixed the clone/copy bug and device race.
181da177e4SLinus Torvalds  *	Mike McLagan		:	Routing by source
191da177e4SLinus Torvalds  *	Malcolm Beattie		:	Buffer handling fixes.
201da177e4SLinus Torvalds  *	Alexey Kuznetsov	:	Double buffer free and other fixes.
211da177e4SLinus Torvalds  *	SVR Anand		:	Fixed several multicast bugs and problems.
221da177e4SLinus Torvalds  *	Alexey Kuznetsov	:	Status, optimisations and more.
231da177e4SLinus Torvalds  *	Brad Parker		:	Better behaviour on mrouted upcall
241da177e4SLinus Torvalds  *					overflow.
251da177e4SLinus Torvalds  *      Carlos Picoto           :       PIMv1 Support
261da177e4SLinus Torvalds  *	Pavlin Ivanov Radoslavov:	PIMv2 Registers must checksum only PIM header
271da177e4SLinus Torvalds  *					Relax this requrement to work with older peers.
281da177e4SLinus Torvalds  *
291da177e4SLinus Torvalds  */
301da177e4SLinus Torvalds 
311da177e4SLinus Torvalds #include <asm/system.h>
321da177e4SLinus Torvalds #include <asm/uaccess.h>
331da177e4SLinus Torvalds #include <linux/types.h>
341da177e4SLinus Torvalds #include <linux/sched.h>
354fc268d2SRandy Dunlap #include <linux/capability.h>
361da177e4SLinus Torvalds #include <linux/errno.h>
371da177e4SLinus Torvalds #include <linux/timer.h>
381da177e4SLinus Torvalds #include <linux/mm.h>
391da177e4SLinus Torvalds #include <linux/kernel.h>
401da177e4SLinus Torvalds #include <linux/fcntl.h>
411da177e4SLinus Torvalds #include <linux/stat.h>
421da177e4SLinus Torvalds #include <linux/socket.h>
431da177e4SLinus Torvalds #include <linux/in.h>
441da177e4SLinus Torvalds #include <linux/inet.h>
451da177e4SLinus Torvalds #include <linux/netdevice.h>
461da177e4SLinus Torvalds #include <linux/inetdevice.h>
471da177e4SLinus Torvalds #include <linux/igmp.h>
481da177e4SLinus Torvalds #include <linux/proc_fs.h>
491da177e4SLinus Torvalds #include <linux/seq_file.h>
501da177e4SLinus Torvalds #include <linux/mroute.h>
511da177e4SLinus Torvalds #include <linux/init.h>
5246f25dffSKris Katterjohn #include <linux/if_ether.h>
531da177e4SLinus Torvalds #include <net/ip.h>
541da177e4SLinus Torvalds #include <net/protocol.h>
551da177e4SLinus Torvalds #include <linux/skbuff.h>
5614c85021SArnaldo Carvalho de Melo #include <net/route.h>
571da177e4SLinus Torvalds #include <net/sock.h>
581da177e4SLinus Torvalds #include <net/icmp.h>
591da177e4SLinus Torvalds #include <net/udp.h>
601da177e4SLinus Torvalds #include <net/raw.h>
611da177e4SLinus Torvalds #include <linux/notifier.h>
621da177e4SLinus Torvalds #include <linux/if_arp.h>
631da177e4SLinus Torvalds #include <linux/netfilter_ipv4.h>
641da177e4SLinus Torvalds #include <net/ipip.h>
651da177e4SLinus Torvalds #include <net/checksum.h>
661da177e4SLinus Torvalds 
671da177e4SLinus Torvalds #if defined(CONFIG_IP_PIMSM_V1) || defined(CONFIG_IP_PIMSM_V2)
681da177e4SLinus Torvalds #define CONFIG_IP_PIMSM	1
691da177e4SLinus Torvalds #endif
701da177e4SLinus Torvalds 
711da177e4SLinus Torvalds static struct sock *mroute_socket;
721da177e4SLinus Torvalds 
731da177e4SLinus Torvalds 
741da177e4SLinus Torvalds /* Big lock, protecting vif table, mrt cache and mroute socket state.
751da177e4SLinus Torvalds    Note that the changes are semaphored via rtnl_lock.
761da177e4SLinus Torvalds  */
771da177e4SLinus Torvalds 
781da177e4SLinus Torvalds static DEFINE_RWLOCK(mrt_lock);
791da177e4SLinus Torvalds 
801da177e4SLinus Torvalds /*
811da177e4SLinus Torvalds  *	Multicast router control variables
821da177e4SLinus Torvalds  */
831da177e4SLinus Torvalds 
841da177e4SLinus Torvalds static struct vif_device vif_table[MAXVIFS];		/* Devices 		*/
851da177e4SLinus Torvalds static int maxvif;
861da177e4SLinus Torvalds 
871da177e4SLinus Torvalds #define VIF_EXISTS(idx) (vif_table[idx].dev != NULL)
881da177e4SLinus Torvalds 
891da177e4SLinus Torvalds static int mroute_do_assert;				/* Set in PIM assert	*/
901da177e4SLinus Torvalds static int mroute_do_pim;
911da177e4SLinus Torvalds 
921da177e4SLinus Torvalds static struct mfc_cache *mfc_cache_array[MFC_LINES];	/* Forwarding cache	*/
931da177e4SLinus Torvalds 
941da177e4SLinus Torvalds static struct mfc_cache *mfc_unres_queue;		/* Queue of unresolved entries */
951da177e4SLinus Torvalds static atomic_t cache_resolve_queue_len;		/* Size of unresolved	*/
961da177e4SLinus Torvalds 
971da177e4SLinus Torvalds /* Special spinlock for queue of unresolved entries */
981da177e4SLinus Torvalds static DEFINE_SPINLOCK(mfc_unres_lock);
991da177e4SLinus Torvalds 
1001da177e4SLinus Torvalds /* We return to original Alan's scheme. Hash table of resolved
1011da177e4SLinus Torvalds    entries is changed only in process context and protected
1021da177e4SLinus Torvalds    with weak lock mrt_lock. Queue of unresolved entries is protected
1031da177e4SLinus Torvalds    with strong spinlock mfc_unres_lock.
1041da177e4SLinus Torvalds 
1051da177e4SLinus Torvalds    In this case data path is free of exclusive locks at all.
1061da177e4SLinus Torvalds  */
1071da177e4SLinus Torvalds 
108ba89966cSEric Dumazet static kmem_cache_t *mrt_cachep __read_mostly;
1091da177e4SLinus Torvalds 
1101da177e4SLinus Torvalds static int ip_mr_forward(struct sk_buff *skb, struct mfc_cache *cache, int local);
1111da177e4SLinus Torvalds static int ipmr_cache_report(struct sk_buff *pkt, vifi_t vifi, int assert);
1121da177e4SLinus Torvalds static int ipmr_fill_mroute(struct sk_buff *skb, struct mfc_cache *c, struct rtmsg *rtm);
1131da177e4SLinus Torvalds 
1141da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM_V2
1151da177e4SLinus Torvalds static struct net_protocol pim_protocol;
1161da177e4SLinus Torvalds #endif
1171da177e4SLinus Torvalds 
1181da177e4SLinus Torvalds static struct timer_list ipmr_expire_timer;
1191da177e4SLinus Torvalds 
1201da177e4SLinus Torvalds /* Service routines creating virtual interfaces: DVMRP tunnels and PIMREG */
1211da177e4SLinus Torvalds 
1221da177e4SLinus Torvalds static
1231da177e4SLinus Torvalds struct net_device *ipmr_new_tunnel(struct vifctl *v)
1241da177e4SLinus Torvalds {
1251da177e4SLinus Torvalds 	struct net_device  *dev;
1261da177e4SLinus Torvalds 
1271da177e4SLinus Torvalds 	dev = __dev_get_by_name("tunl0");
1281da177e4SLinus Torvalds 
1291da177e4SLinus Torvalds 	if (dev) {
1301da177e4SLinus Torvalds 		int err;
1311da177e4SLinus Torvalds 		struct ifreq ifr;
1321da177e4SLinus Torvalds 		mm_segment_t	oldfs;
1331da177e4SLinus Torvalds 		struct ip_tunnel_parm p;
1341da177e4SLinus Torvalds 		struct in_device  *in_dev;
1351da177e4SLinus Torvalds 
1361da177e4SLinus Torvalds 		memset(&p, 0, sizeof(p));
1371da177e4SLinus Torvalds 		p.iph.daddr = v->vifc_rmt_addr.s_addr;
1381da177e4SLinus Torvalds 		p.iph.saddr = v->vifc_lcl_addr.s_addr;
1391da177e4SLinus Torvalds 		p.iph.version = 4;
1401da177e4SLinus Torvalds 		p.iph.ihl = 5;
1411da177e4SLinus Torvalds 		p.iph.protocol = IPPROTO_IPIP;
1421da177e4SLinus Torvalds 		sprintf(p.name, "dvmrp%d", v->vifc_vifi);
1431da177e4SLinus Torvalds 		ifr.ifr_ifru.ifru_data = (void*)&p;
1441da177e4SLinus Torvalds 
1451da177e4SLinus Torvalds 		oldfs = get_fs(); set_fs(KERNEL_DS);
1461da177e4SLinus Torvalds 		err = dev->do_ioctl(dev, &ifr, SIOCADDTUNNEL);
1471da177e4SLinus Torvalds 		set_fs(oldfs);
1481da177e4SLinus Torvalds 
1491da177e4SLinus Torvalds 		dev = NULL;
1501da177e4SLinus Torvalds 
1511da177e4SLinus Torvalds 		if (err == 0 && (dev = __dev_get_by_name(p.name)) != NULL) {
1521da177e4SLinus Torvalds 			dev->flags |= IFF_MULTICAST;
1531da177e4SLinus Torvalds 
154e5ed6399SHerbert Xu 			in_dev = __in_dev_get_rtnl(dev);
1551da177e4SLinus Torvalds 			if (in_dev == NULL && (in_dev = inetdev_init(dev)) == NULL)
1561da177e4SLinus Torvalds 				goto failure;
1571da177e4SLinus Torvalds 			in_dev->cnf.rp_filter = 0;
1581da177e4SLinus Torvalds 
1591da177e4SLinus Torvalds 			if (dev_open(dev))
1601da177e4SLinus Torvalds 				goto failure;
1611da177e4SLinus Torvalds 		}
1621da177e4SLinus Torvalds 	}
1631da177e4SLinus Torvalds 	return dev;
1641da177e4SLinus Torvalds 
1651da177e4SLinus Torvalds failure:
1661da177e4SLinus Torvalds 	/* allow the register to be completed before unregistering. */
1671da177e4SLinus Torvalds 	rtnl_unlock();
1681da177e4SLinus Torvalds 	rtnl_lock();
1691da177e4SLinus Torvalds 
1701da177e4SLinus Torvalds 	unregister_netdevice(dev);
1711da177e4SLinus Torvalds 	return NULL;
1721da177e4SLinus Torvalds }
1731da177e4SLinus Torvalds 
1741da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM
1751da177e4SLinus Torvalds 
1761da177e4SLinus Torvalds static int reg_vif_num = -1;
1771da177e4SLinus Torvalds 
1781da177e4SLinus Torvalds static int reg_vif_xmit(struct sk_buff *skb, struct net_device *dev)
1791da177e4SLinus Torvalds {
1801da177e4SLinus Torvalds 	read_lock(&mrt_lock);
1812941a486SPatrick McHardy 	((struct net_device_stats*)netdev_priv(dev))->tx_bytes += skb->len;
1822941a486SPatrick McHardy 	((struct net_device_stats*)netdev_priv(dev))->tx_packets++;
1831da177e4SLinus Torvalds 	ipmr_cache_report(skb, reg_vif_num, IGMPMSG_WHOLEPKT);
1841da177e4SLinus Torvalds 	read_unlock(&mrt_lock);
1851da177e4SLinus Torvalds 	kfree_skb(skb);
1861da177e4SLinus Torvalds 	return 0;
1871da177e4SLinus Torvalds }
1881da177e4SLinus Torvalds 
1891da177e4SLinus Torvalds static struct net_device_stats *reg_vif_get_stats(struct net_device *dev)
1901da177e4SLinus Torvalds {
1912941a486SPatrick McHardy 	return (struct net_device_stats*)netdev_priv(dev);
1921da177e4SLinus Torvalds }
1931da177e4SLinus Torvalds 
1941da177e4SLinus Torvalds static void reg_vif_setup(struct net_device *dev)
1951da177e4SLinus Torvalds {
1961da177e4SLinus Torvalds 	dev->type		= ARPHRD_PIMREG;
19746f25dffSKris Katterjohn 	dev->mtu		= ETH_DATA_LEN - sizeof(struct iphdr) - 8;
1981da177e4SLinus Torvalds 	dev->flags		= IFF_NOARP;
1991da177e4SLinus Torvalds 	dev->hard_start_xmit	= reg_vif_xmit;
2001da177e4SLinus Torvalds 	dev->get_stats		= reg_vif_get_stats;
2011da177e4SLinus Torvalds 	dev->destructor		= free_netdev;
2021da177e4SLinus Torvalds }
2031da177e4SLinus Torvalds 
2041da177e4SLinus Torvalds static struct net_device *ipmr_reg_vif(void)
2051da177e4SLinus Torvalds {
2061da177e4SLinus Torvalds 	struct net_device *dev;
2071da177e4SLinus Torvalds 	struct in_device *in_dev;
2081da177e4SLinus Torvalds 
2091da177e4SLinus Torvalds 	dev = alloc_netdev(sizeof(struct net_device_stats), "pimreg",
2101da177e4SLinus Torvalds 			   reg_vif_setup);
2111da177e4SLinus Torvalds 
2121da177e4SLinus Torvalds 	if (dev == NULL)
2131da177e4SLinus Torvalds 		return NULL;
2141da177e4SLinus Torvalds 
2151da177e4SLinus Torvalds 	if (register_netdevice(dev)) {
2161da177e4SLinus Torvalds 		free_netdev(dev);
2171da177e4SLinus Torvalds 		return NULL;
2181da177e4SLinus Torvalds 	}
2191da177e4SLinus Torvalds 	dev->iflink = 0;
2201da177e4SLinus Torvalds 
2211da177e4SLinus Torvalds 	if ((in_dev = inetdev_init(dev)) == NULL)
2221da177e4SLinus Torvalds 		goto failure;
2231da177e4SLinus Torvalds 
2241da177e4SLinus Torvalds 	in_dev->cnf.rp_filter = 0;
2251da177e4SLinus Torvalds 
2261da177e4SLinus Torvalds 	if (dev_open(dev))
2271da177e4SLinus Torvalds 		goto failure;
2281da177e4SLinus Torvalds 
2291da177e4SLinus Torvalds 	return dev;
2301da177e4SLinus Torvalds 
2311da177e4SLinus Torvalds failure:
2321da177e4SLinus Torvalds 	/* allow the register to be completed before unregistering. */
2331da177e4SLinus Torvalds 	rtnl_unlock();
2341da177e4SLinus Torvalds 	rtnl_lock();
2351da177e4SLinus Torvalds 
2361da177e4SLinus Torvalds 	unregister_netdevice(dev);
2371da177e4SLinus Torvalds 	return NULL;
2381da177e4SLinus Torvalds }
2391da177e4SLinus Torvalds #endif
2401da177e4SLinus Torvalds 
2411da177e4SLinus Torvalds /*
2421da177e4SLinus Torvalds  *	Delete a VIF entry
2431da177e4SLinus Torvalds  */
2441da177e4SLinus Torvalds 
2451da177e4SLinus Torvalds static int vif_delete(int vifi)
2461da177e4SLinus Torvalds {
2471da177e4SLinus Torvalds 	struct vif_device *v;
2481da177e4SLinus Torvalds 	struct net_device *dev;
2491da177e4SLinus Torvalds 	struct in_device *in_dev;
2501da177e4SLinus Torvalds 
2511da177e4SLinus Torvalds 	if (vifi < 0 || vifi >= maxvif)
2521da177e4SLinus Torvalds 		return -EADDRNOTAVAIL;
2531da177e4SLinus Torvalds 
2541da177e4SLinus Torvalds 	v = &vif_table[vifi];
2551da177e4SLinus Torvalds 
2561da177e4SLinus Torvalds 	write_lock_bh(&mrt_lock);
2571da177e4SLinus Torvalds 	dev = v->dev;
2581da177e4SLinus Torvalds 	v->dev = NULL;
2591da177e4SLinus Torvalds 
2601da177e4SLinus Torvalds 	if (!dev) {
2611da177e4SLinus Torvalds 		write_unlock_bh(&mrt_lock);
2621da177e4SLinus Torvalds 		return -EADDRNOTAVAIL;
2631da177e4SLinus Torvalds 	}
2641da177e4SLinus Torvalds 
2651da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM
2661da177e4SLinus Torvalds 	if (vifi == reg_vif_num)
2671da177e4SLinus Torvalds 		reg_vif_num = -1;
2681da177e4SLinus Torvalds #endif
2691da177e4SLinus Torvalds 
2701da177e4SLinus Torvalds 	if (vifi+1 == maxvif) {
2711da177e4SLinus Torvalds 		int tmp;
2721da177e4SLinus Torvalds 		for (tmp=vifi-1; tmp>=0; tmp--) {
2731da177e4SLinus Torvalds 			if (VIF_EXISTS(tmp))
2741da177e4SLinus Torvalds 				break;
2751da177e4SLinus Torvalds 		}
2761da177e4SLinus Torvalds 		maxvif = tmp+1;
2771da177e4SLinus Torvalds 	}
2781da177e4SLinus Torvalds 
2791da177e4SLinus Torvalds 	write_unlock_bh(&mrt_lock);
2801da177e4SLinus Torvalds 
2811da177e4SLinus Torvalds 	dev_set_allmulti(dev, -1);
2821da177e4SLinus Torvalds 
283e5ed6399SHerbert Xu 	if ((in_dev = __in_dev_get_rtnl(dev)) != NULL) {
2841da177e4SLinus Torvalds 		in_dev->cnf.mc_forwarding--;
2851da177e4SLinus Torvalds 		ip_rt_multicast_event(in_dev);
2861da177e4SLinus Torvalds 	}
2871da177e4SLinus Torvalds 
2881da177e4SLinus Torvalds 	if (v->flags&(VIFF_TUNNEL|VIFF_REGISTER))
2891da177e4SLinus Torvalds 		unregister_netdevice(dev);
2901da177e4SLinus Torvalds 
2911da177e4SLinus Torvalds 	dev_put(dev);
2921da177e4SLinus Torvalds 	return 0;
2931da177e4SLinus Torvalds }
2941da177e4SLinus Torvalds 
2951da177e4SLinus Torvalds /* Destroy an unresolved cache entry, killing queued skbs
2961da177e4SLinus Torvalds    and reporting error to netlink readers.
2971da177e4SLinus Torvalds  */
2981da177e4SLinus Torvalds 
2991da177e4SLinus Torvalds static void ipmr_destroy_unres(struct mfc_cache *c)
3001da177e4SLinus Torvalds {
3011da177e4SLinus Torvalds 	struct sk_buff *skb;
3029ef1d4c7SPatrick McHardy 	struct nlmsgerr *e;
3031da177e4SLinus Torvalds 
3041da177e4SLinus Torvalds 	atomic_dec(&cache_resolve_queue_len);
3051da177e4SLinus Torvalds 
3061da177e4SLinus Torvalds 	while((skb=skb_dequeue(&c->mfc_un.unres.unresolved))) {
3071da177e4SLinus Torvalds 		if (skb->nh.iph->version == 0) {
3081da177e4SLinus Torvalds 			struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr));
3091da177e4SLinus Torvalds 			nlh->nlmsg_type = NLMSG_ERROR;
3101da177e4SLinus Torvalds 			nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct nlmsgerr));
3111da177e4SLinus Torvalds 			skb_trim(skb, nlh->nlmsg_len);
3129ef1d4c7SPatrick McHardy 			e = NLMSG_DATA(nlh);
3139ef1d4c7SPatrick McHardy 			e->error = -ETIMEDOUT;
3149ef1d4c7SPatrick McHardy 			memset(&e->msg, 0, sizeof(e->msg));
3152942e900SThomas Graf 
3162942e900SThomas Graf 			rtnl_unicast(skb, NETLINK_CB(skb).pid);
3171da177e4SLinus Torvalds 		} else
3181da177e4SLinus Torvalds 			kfree_skb(skb);
3191da177e4SLinus Torvalds 	}
3201da177e4SLinus Torvalds 
3211da177e4SLinus Torvalds 	kmem_cache_free(mrt_cachep, c);
3221da177e4SLinus Torvalds }
3231da177e4SLinus Torvalds 
3241da177e4SLinus Torvalds 
3251da177e4SLinus Torvalds /* Single timer process for all the unresolved queue. */
3261da177e4SLinus Torvalds 
3271da177e4SLinus Torvalds static void ipmr_expire_process(unsigned long dummy)
3281da177e4SLinus Torvalds {
3291da177e4SLinus Torvalds 	unsigned long now;
3301da177e4SLinus Torvalds 	unsigned long expires;
3311da177e4SLinus Torvalds 	struct mfc_cache *c, **cp;
3321da177e4SLinus Torvalds 
3331da177e4SLinus Torvalds 	if (!spin_trylock(&mfc_unres_lock)) {
3341da177e4SLinus Torvalds 		mod_timer(&ipmr_expire_timer, jiffies+HZ/10);
3351da177e4SLinus Torvalds 		return;
3361da177e4SLinus Torvalds 	}
3371da177e4SLinus Torvalds 
3381da177e4SLinus Torvalds 	if (atomic_read(&cache_resolve_queue_len) == 0)
3391da177e4SLinus Torvalds 		goto out;
3401da177e4SLinus Torvalds 
3411da177e4SLinus Torvalds 	now = jiffies;
3421da177e4SLinus Torvalds 	expires = 10*HZ;
3431da177e4SLinus Torvalds 	cp = &mfc_unres_queue;
3441da177e4SLinus Torvalds 
3451da177e4SLinus Torvalds 	while ((c=*cp) != NULL) {
3461da177e4SLinus Torvalds 		if (time_after(c->mfc_un.unres.expires, now)) {
3471da177e4SLinus Torvalds 			unsigned long interval = c->mfc_un.unres.expires - now;
3481da177e4SLinus Torvalds 			if (interval < expires)
3491da177e4SLinus Torvalds 				expires = interval;
3501da177e4SLinus Torvalds 			cp = &c->next;
3511da177e4SLinus Torvalds 			continue;
3521da177e4SLinus Torvalds 		}
3531da177e4SLinus Torvalds 
3541da177e4SLinus Torvalds 		*cp = c->next;
3551da177e4SLinus Torvalds 
3561da177e4SLinus Torvalds 		ipmr_destroy_unres(c);
3571da177e4SLinus Torvalds 	}
3581da177e4SLinus Torvalds 
3591da177e4SLinus Torvalds 	if (atomic_read(&cache_resolve_queue_len))
3601da177e4SLinus Torvalds 		mod_timer(&ipmr_expire_timer, jiffies + expires);
3611da177e4SLinus Torvalds 
3621da177e4SLinus Torvalds out:
3631da177e4SLinus Torvalds 	spin_unlock(&mfc_unres_lock);
3641da177e4SLinus Torvalds }
3651da177e4SLinus Torvalds 
3661da177e4SLinus Torvalds /* Fill oifs list. It is called under write locked mrt_lock. */
3671da177e4SLinus Torvalds 
368d1b04c08SBaruch Even static void ipmr_update_thresholds(struct mfc_cache *cache, unsigned char *ttls)
3691da177e4SLinus Torvalds {
3701da177e4SLinus Torvalds 	int vifi;
3711da177e4SLinus Torvalds 
3721da177e4SLinus Torvalds 	cache->mfc_un.res.minvif = MAXVIFS;
3731da177e4SLinus Torvalds 	cache->mfc_un.res.maxvif = 0;
3741da177e4SLinus Torvalds 	memset(cache->mfc_un.res.ttls, 255, MAXVIFS);
3751da177e4SLinus Torvalds 
3761da177e4SLinus Torvalds 	for (vifi=0; vifi<maxvif; vifi++) {
3771da177e4SLinus Torvalds 		if (VIF_EXISTS(vifi) && ttls[vifi] && ttls[vifi] < 255) {
3781da177e4SLinus Torvalds 			cache->mfc_un.res.ttls[vifi] = ttls[vifi];
3791da177e4SLinus Torvalds 			if (cache->mfc_un.res.minvif > vifi)
3801da177e4SLinus Torvalds 				cache->mfc_un.res.minvif = vifi;
3811da177e4SLinus Torvalds 			if (cache->mfc_un.res.maxvif <= vifi)
3821da177e4SLinus Torvalds 				cache->mfc_un.res.maxvif = vifi + 1;
3831da177e4SLinus Torvalds 		}
3841da177e4SLinus Torvalds 	}
3851da177e4SLinus Torvalds }
3861da177e4SLinus Torvalds 
3871da177e4SLinus Torvalds static int vif_add(struct vifctl *vifc, int mrtsock)
3881da177e4SLinus Torvalds {
3891da177e4SLinus Torvalds 	int vifi = vifc->vifc_vifi;
3901da177e4SLinus Torvalds 	struct vif_device *v = &vif_table[vifi];
3911da177e4SLinus Torvalds 	struct net_device *dev;
3921da177e4SLinus Torvalds 	struct in_device *in_dev;
3931da177e4SLinus Torvalds 
3941da177e4SLinus Torvalds 	/* Is vif busy ? */
3951da177e4SLinus Torvalds 	if (VIF_EXISTS(vifi))
3961da177e4SLinus Torvalds 		return -EADDRINUSE;
3971da177e4SLinus Torvalds 
3981da177e4SLinus Torvalds 	switch (vifc->vifc_flags) {
3991da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM
4001da177e4SLinus Torvalds 	case VIFF_REGISTER:
4011da177e4SLinus Torvalds 		/*
4021da177e4SLinus Torvalds 		 * Special Purpose VIF in PIM
4031da177e4SLinus Torvalds 		 * All the packets will be sent to the daemon
4041da177e4SLinus Torvalds 		 */
4051da177e4SLinus Torvalds 		if (reg_vif_num >= 0)
4061da177e4SLinus Torvalds 			return -EADDRINUSE;
4071da177e4SLinus Torvalds 		dev = ipmr_reg_vif();
4081da177e4SLinus Torvalds 		if (!dev)
4091da177e4SLinus Torvalds 			return -ENOBUFS;
4101da177e4SLinus Torvalds 		break;
4111da177e4SLinus Torvalds #endif
4121da177e4SLinus Torvalds 	case VIFF_TUNNEL:
4131da177e4SLinus Torvalds 		dev = ipmr_new_tunnel(vifc);
4141da177e4SLinus Torvalds 		if (!dev)
4151da177e4SLinus Torvalds 			return -ENOBUFS;
4161da177e4SLinus Torvalds 		break;
4171da177e4SLinus Torvalds 	case 0:
4181da177e4SLinus Torvalds 		dev = ip_dev_find(vifc->vifc_lcl_addr.s_addr);
4191da177e4SLinus Torvalds 		if (!dev)
4201da177e4SLinus Torvalds 			return -EADDRNOTAVAIL;
42115333061SStephen Hemminger 		dev_put(dev);
4221da177e4SLinus Torvalds 		break;
4231da177e4SLinus Torvalds 	default:
4241da177e4SLinus Torvalds 		return -EINVAL;
4251da177e4SLinus Torvalds 	}
4261da177e4SLinus Torvalds 
427e5ed6399SHerbert Xu 	if ((in_dev = __in_dev_get_rtnl(dev)) == NULL)
4281da177e4SLinus Torvalds 		return -EADDRNOTAVAIL;
4291da177e4SLinus Torvalds 	in_dev->cnf.mc_forwarding++;
4301da177e4SLinus Torvalds 	dev_set_allmulti(dev, +1);
4311da177e4SLinus Torvalds 	ip_rt_multicast_event(in_dev);
4321da177e4SLinus Torvalds 
4331da177e4SLinus Torvalds 	/*
4341da177e4SLinus Torvalds 	 *	Fill in the VIF structures
4351da177e4SLinus Torvalds 	 */
4361da177e4SLinus Torvalds 	v->rate_limit=vifc->vifc_rate_limit;
4371da177e4SLinus Torvalds 	v->local=vifc->vifc_lcl_addr.s_addr;
4381da177e4SLinus Torvalds 	v->remote=vifc->vifc_rmt_addr.s_addr;
4391da177e4SLinus Torvalds 	v->flags=vifc->vifc_flags;
4401da177e4SLinus Torvalds 	if (!mrtsock)
4411da177e4SLinus Torvalds 		v->flags |= VIFF_STATIC;
4421da177e4SLinus Torvalds 	v->threshold=vifc->vifc_threshold;
4431da177e4SLinus Torvalds 	v->bytes_in = 0;
4441da177e4SLinus Torvalds 	v->bytes_out = 0;
4451da177e4SLinus Torvalds 	v->pkt_in = 0;
4461da177e4SLinus Torvalds 	v->pkt_out = 0;
4471da177e4SLinus Torvalds 	v->link = dev->ifindex;
4481da177e4SLinus Torvalds 	if (v->flags&(VIFF_TUNNEL|VIFF_REGISTER))
4491da177e4SLinus Torvalds 		v->link = dev->iflink;
4501da177e4SLinus Torvalds 
4511da177e4SLinus Torvalds 	/* And finish update writing critical data */
4521da177e4SLinus Torvalds 	write_lock_bh(&mrt_lock);
4531da177e4SLinus Torvalds 	dev_hold(dev);
4541da177e4SLinus Torvalds 	v->dev=dev;
4551da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM
4561da177e4SLinus Torvalds 	if (v->flags&VIFF_REGISTER)
4571da177e4SLinus Torvalds 		reg_vif_num = vifi;
4581da177e4SLinus Torvalds #endif
4591da177e4SLinus Torvalds 	if (vifi+1 > maxvif)
4601da177e4SLinus Torvalds 		maxvif = vifi+1;
4611da177e4SLinus Torvalds 	write_unlock_bh(&mrt_lock);
4621da177e4SLinus Torvalds 	return 0;
4631da177e4SLinus Torvalds }
4641da177e4SLinus Torvalds 
465114c7844SAl Viro static struct mfc_cache *ipmr_cache_find(__be32 origin, __be32 mcastgrp)
4661da177e4SLinus Torvalds {
4671da177e4SLinus Torvalds 	int line=MFC_HASH(mcastgrp,origin);
4681da177e4SLinus Torvalds 	struct mfc_cache *c;
4691da177e4SLinus Torvalds 
4701da177e4SLinus Torvalds 	for (c=mfc_cache_array[line]; c; c = c->next) {
4711da177e4SLinus Torvalds 		if (c->mfc_origin==origin && c->mfc_mcastgrp==mcastgrp)
4721da177e4SLinus Torvalds 			break;
4731da177e4SLinus Torvalds 	}
4741da177e4SLinus Torvalds 	return c;
4751da177e4SLinus Torvalds }
4761da177e4SLinus Torvalds 
4771da177e4SLinus Torvalds /*
4781da177e4SLinus Torvalds  *	Allocate a multicast cache entry
4791da177e4SLinus Torvalds  */
4801da177e4SLinus Torvalds static struct mfc_cache *ipmr_cache_alloc(void)
4811da177e4SLinus Torvalds {
4821da177e4SLinus Torvalds 	struct mfc_cache *c=kmem_cache_alloc(mrt_cachep, GFP_KERNEL);
4831da177e4SLinus Torvalds 	if(c==NULL)
4841da177e4SLinus Torvalds 		return NULL;
4851da177e4SLinus Torvalds 	memset(c, 0, sizeof(*c));
4861da177e4SLinus Torvalds 	c->mfc_un.res.minvif = MAXVIFS;
4871da177e4SLinus Torvalds 	return c;
4881da177e4SLinus Torvalds }
4891da177e4SLinus Torvalds 
4901da177e4SLinus Torvalds static struct mfc_cache *ipmr_cache_alloc_unres(void)
4911da177e4SLinus Torvalds {
4921da177e4SLinus Torvalds 	struct mfc_cache *c=kmem_cache_alloc(mrt_cachep, GFP_ATOMIC);
4931da177e4SLinus Torvalds 	if(c==NULL)
4941da177e4SLinus Torvalds 		return NULL;
4951da177e4SLinus Torvalds 	memset(c, 0, sizeof(*c));
4961da177e4SLinus Torvalds 	skb_queue_head_init(&c->mfc_un.unres.unresolved);
4971da177e4SLinus Torvalds 	c->mfc_un.unres.expires = jiffies + 10*HZ;
4981da177e4SLinus Torvalds 	return c;
4991da177e4SLinus Torvalds }
5001da177e4SLinus Torvalds 
5011da177e4SLinus Torvalds /*
5021da177e4SLinus Torvalds  *	A cache entry has gone into a resolved state from queued
5031da177e4SLinus Torvalds  */
5041da177e4SLinus Torvalds 
5051da177e4SLinus Torvalds static void ipmr_cache_resolve(struct mfc_cache *uc, struct mfc_cache *c)
5061da177e4SLinus Torvalds {
5071da177e4SLinus Torvalds 	struct sk_buff *skb;
5089ef1d4c7SPatrick McHardy 	struct nlmsgerr *e;
5091da177e4SLinus Torvalds 
5101da177e4SLinus Torvalds 	/*
5111da177e4SLinus Torvalds 	 *	Play the pending entries through our router
5121da177e4SLinus Torvalds 	 */
5131da177e4SLinus Torvalds 
5141da177e4SLinus Torvalds 	while((skb=__skb_dequeue(&uc->mfc_un.unres.unresolved))) {
5151da177e4SLinus Torvalds 		if (skb->nh.iph->version == 0) {
5161da177e4SLinus Torvalds 			struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr));
5171da177e4SLinus Torvalds 
5181da177e4SLinus Torvalds 			if (ipmr_fill_mroute(skb, c, NLMSG_DATA(nlh)) > 0) {
5191da177e4SLinus Torvalds 				nlh->nlmsg_len = skb->tail - (u8*)nlh;
5201da177e4SLinus Torvalds 			} else {
5211da177e4SLinus Torvalds 				nlh->nlmsg_type = NLMSG_ERROR;
5221da177e4SLinus Torvalds 				nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct nlmsgerr));
5231da177e4SLinus Torvalds 				skb_trim(skb, nlh->nlmsg_len);
5249ef1d4c7SPatrick McHardy 				e = NLMSG_DATA(nlh);
5259ef1d4c7SPatrick McHardy 				e->error = -EMSGSIZE;
5269ef1d4c7SPatrick McHardy 				memset(&e->msg, 0, sizeof(e->msg));
5271da177e4SLinus Torvalds 			}
5282942e900SThomas Graf 
5292942e900SThomas Graf 			rtnl_unicast(skb, NETLINK_CB(skb).pid);
5301da177e4SLinus Torvalds 		} else
5311da177e4SLinus Torvalds 			ip_mr_forward(skb, c, 0);
5321da177e4SLinus Torvalds 	}
5331da177e4SLinus Torvalds }
5341da177e4SLinus Torvalds 
5351da177e4SLinus Torvalds /*
5361da177e4SLinus Torvalds  *	Bounce a cache query up to mrouted. We could use netlink for this but mrouted
5371da177e4SLinus Torvalds  *	expects the following bizarre scheme.
5381da177e4SLinus Torvalds  *
5391da177e4SLinus Torvalds  *	Called under mrt_lock.
5401da177e4SLinus Torvalds  */
5411da177e4SLinus Torvalds 
5421da177e4SLinus Torvalds static int ipmr_cache_report(struct sk_buff *pkt, vifi_t vifi, int assert)
5431da177e4SLinus Torvalds {
5441da177e4SLinus Torvalds 	struct sk_buff *skb;
5451da177e4SLinus Torvalds 	int ihl = pkt->nh.iph->ihl<<2;
5461da177e4SLinus Torvalds 	struct igmphdr *igmp;
5471da177e4SLinus Torvalds 	struct igmpmsg *msg;
5481da177e4SLinus Torvalds 	int ret;
5491da177e4SLinus Torvalds 
5501da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM
5511da177e4SLinus Torvalds 	if (assert == IGMPMSG_WHOLEPKT)
5521da177e4SLinus Torvalds 		skb = skb_realloc_headroom(pkt, sizeof(struct iphdr));
5531da177e4SLinus Torvalds 	else
5541da177e4SLinus Torvalds #endif
5551da177e4SLinus Torvalds 		skb = alloc_skb(128, GFP_ATOMIC);
5561da177e4SLinus Torvalds 
5571da177e4SLinus Torvalds 	if(!skb)
5581da177e4SLinus Torvalds 		return -ENOBUFS;
5591da177e4SLinus Torvalds 
5601da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM
5611da177e4SLinus Torvalds 	if (assert == IGMPMSG_WHOLEPKT) {
5621da177e4SLinus Torvalds 		/* Ugly, but we have no choice with this interface.
5631da177e4SLinus Torvalds 		   Duplicate old header, fix ihl, length etc.
5641da177e4SLinus Torvalds 		   And all this only to mangle msg->im_msgtype and
5651da177e4SLinus Torvalds 		   to set msg->im_mbz to "mbz" :-)
5661da177e4SLinus Torvalds 		 */
5671da177e4SLinus Torvalds 		msg = (struct igmpmsg*)skb_push(skb, sizeof(struct iphdr));
5681da177e4SLinus Torvalds 		skb->nh.raw = skb->h.raw = (u8*)msg;
5691da177e4SLinus Torvalds 		memcpy(msg, pkt->nh.raw, sizeof(struct iphdr));
5701da177e4SLinus Torvalds 		msg->im_msgtype = IGMPMSG_WHOLEPKT;
5711da177e4SLinus Torvalds 		msg->im_mbz = 0;
5721da177e4SLinus Torvalds  		msg->im_vif = reg_vif_num;
5731da177e4SLinus Torvalds 		skb->nh.iph->ihl = sizeof(struct iphdr) >> 2;
5741da177e4SLinus Torvalds 		skb->nh.iph->tot_len = htons(ntohs(pkt->nh.iph->tot_len) + sizeof(struct iphdr));
5751da177e4SLinus Torvalds 	} else
5761da177e4SLinus Torvalds #endif
5771da177e4SLinus Torvalds 	{
5781da177e4SLinus Torvalds 
5791da177e4SLinus Torvalds 	/*
5801da177e4SLinus Torvalds 	 *	Copy the IP header
5811da177e4SLinus Torvalds 	 */
5821da177e4SLinus Torvalds 
5831da177e4SLinus Torvalds 	skb->nh.iph = (struct iphdr *)skb_put(skb, ihl);
5841da177e4SLinus Torvalds 	memcpy(skb->data,pkt->data,ihl);
5851da177e4SLinus Torvalds 	skb->nh.iph->protocol = 0;			/* Flag to the kernel this is a route add */
5861da177e4SLinus Torvalds 	msg = (struct igmpmsg*)skb->nh.iph;
5871da177e4SLinus Torvalds 	msg->im_vif = vifi;
5881da177e4SLinus Torvalds 	skb->dst = dst_clone(pkt->dst);
5891da177e4SLinus Torvalds 
5901da177e4SLinus Torvalds 	/*
5911da177e4SLinus Torvalds 	 *	Add our header
5921da177e4SLinus Torvalds 	 */
5931da177e4SLinus Torvalds 
5941da177e4SLinus Torvalds 	igmp=(struct igmphdr *)skb_put(skb,sizeof(struct igmphdr));
5951da177e4SLinus Torvalds 	igmp->type	=
5961da177e4SLinus Torvalds 	msg->im_msgtype = assert;
5971da177e4SLinus Torvalds 	igmp->code 	=	0;
5981da177e4SLinus Torvalds 	skb->nh.iph->tot_len=htons(skb->len);			/* Fix the length */
5991da177e4SLinus Torvalds 	skb->h.raw = skb->nh.raw;
6001da177e4SLinus Torvalds         }
6011da177e4SLinus Torvalds 
6021da177e4SLinus Torvalds 	if (mroute_socket == NULL) {
6031da177e4SLinus Torvalds 		kfree_skb(skb);
6041da177e4SLinus Torvalds 		return -EINVAL;
6051da177e4SLinus Torvalds 	}
6061da177e4SLinus Torvalds 
6071da177e4SLinus Torvalds 	/*
6081da177e4SLinus Torvalds 	 *	Deliver to mrouted
6091da177e4SLinus Torvalds 	 */
6101da177e4SLinus Torvalds 	if ((ret=sock_queue_rcv_skb(mroute_socket,skb))<0) {
6111da177e4SLinus Torvalds 		if (net_ratelimit())
6121da177e4SLinus Torvalds 			printk(KERN_WARNING "mroute: pending queue full, dropping entries.\n");
6131da177e4SLinus Torvalds 		kfree_skb(skb);
6141da177e4SLinus Torvalds 	}
6151da177e4SLinus Torvalds 
6161da177e4SLinus Torvalds 	return ret;
6171da177e4SLinus Torvalds }
6181da177e4SLinus Torvalds 
6191da177e4SLinus Torvalds /*
6201da177e4SLinus Torvalds  *	Queue a packet for resolution. It gets locked cache entry!
6211da177e4SLinus Torvalds  */
6221da177e4SLinus Torvalds 
6231da177e4SLinus Torvalds static int
6241da177e4SLinus Torvalds ipmr_cache_unresolved(vifi_t vifi, struct sk_buff *skb)
6251da177e4SLinus Torvalds {
6261da177e4SLinus Torvalds 	int err;
6271da177e4SLinus Torvalds 	struct mfc_cache *c;
6281da177e4SLinus Torvalds 
6291da177e4SLinus Torvalds 	spin_lock_bh(&mfc_unres_lock);
6301da177e4SLinus Torvalds 	for (c=mfc_unres_queue; c; c=c->next) {
6311da177e4SLinus Torvalds 		if (c->mfc_mcastgrp == skb->nh.iph->daddr &&
6321da177e4SLinus Torvalds 		    c->mfc_origin == skb->nh.iph->saddr)
6331da177e4SLinus Torvalds 			break;
6341da177e4SLinus Torvalds 	}
6351da177e4SLinus Torvalds 
6361da177e4SLinus Torvalds 	if (c == NULL) {
6371da177e4SLinus Torvalds 		/*
6381da177e4SLinus Torvalds 		 *	Create a new entry if allowable
6391da177e4SLinus Torvalds 		 */
6401da177e4SLinus Torvalds 
6411da177e4SLinus Torvalds 		if (atomic_read(&cache_resolve_queue_len)>=10 ||
6421da177e4SLinus Torvalds 		    (c=ipmr_cache_alloc_unres())==NULL) {
6431da177e4SLinus Torvalds 			spin_unlock_bh(&mfc_unres_lock);
6441da177e4SLinus Torvalds 
6451da177e4SLinus Torvalds 			kfree_skb(skb);
6461da177e4SLinus Torvalds 			return -ENOBUFS;
6471da177e4SLinus Torvalds 		}
6481da177e4SLinus Torvalds 
6491da177e4SLinus Torvalds 		/*
6501da177e4SLinus Torvalds 		 *	Fill in the new cache entry
6511da177e4SLinus Torvalds 		 */
6521da177e4SLinus Torvalds 		c->mfc_parent=-1;
6531da177e4SLinus Torvalds 		c->mfc_origin=skb->nh.iph->saddr;
6541da177e4SLinus Torvalds 		c->mfc_mcastgrp=skb->nh.iph->daddr;
6551da177e4SLinus Torvalds 
6561da177e4SLinus Torvalds 		/*
6571da177e4SLinus Torvalds 		 *	Reflect first query at mrouted.
6581da177e4SLinus Torvalds 		 */
6591da177e4SLinus Torvalds 		if ((err = ipmr_cache_report(skb, vifi, IGMPMSG_NOCACHE))<0) {
6601da177e4SLinus Torvalds 			/* If the report failed throw the cache entry
6611da177e4SLinus Torvalds 			   out - Brad Parker
6621da177e4SLinus Torvalds 			 */
6631da177e4SLinus Torvalds 			spin_unlock_bh(&mfc_unres_lock);
6641da177e4SLinus Torvalds 
6651da177e4SLinus Torvalds 			kmem_cache_free(mrt_cachep, c);
6661da177e4SLinus Torvalds 			kfree_skb(skb);
6671da177e4SLinus Torvalds 			return err;
6681da177e4SLinus Torvalds 		}
6691da177e4SLinus Torvalds 
6701da177e4SLinus Torvalds 		atomic_inc(&cache_resolve_queue_len);
6711da177e4SLinus Torvalds 		c->next = mfc_unres_queue;
6721da177e4SLinus Torvalds 		mfc_unres_queue = c;
6731da177e4SLinus Torvalds 
6741da177e4SLinus Torvalds 		mod_timer(&ipmr_expire_timer, c->mfc_un.unres.expires);
6751da177e4SLinus Torvalds 	}
6761da177e4SLinus Torvalds 
6771da177e4SLinus Torvalds 	/*
6781da177e4SLinus Torvalds 	 *	See if we can append the packet
6791da177e4SLinus Torvalds 	 */
6801da177e4SLinus Torvalds 	if (c->mfc_un.unres.unresolved.qlen>3) {
6811da177e4SLinus Torvalds 		kfree_skb(skb);
6821da177e4SLinus Torvalds 		err = -ENOBUFS;
6831da177e4SLinus Torvalds 	} else {
6841da177e4SLinus Torvalds 		skb_queue_tail(&c->mfc_un.unres.unresolved,skb);
6851da177e4SLinus Torvalds 		err = 0;
6861da177e4SLinus Torvalds 	}
6871da177e4SLinus Torvalds 
6881da177e4SLinus Torvalds 	spin_unlock_bh(&mfc_unres_lock);
6891da177e4SLinus Torvalds 	return err;
6901da177e4SLinus Torvalds }
6911da177e4SLinus Torvalds 
6921da177e4SLinus Torvalds /*
6931da177e4SLinus Torvalds  *	MFC cache manipulation by user space mroute daemon
6941da177e4SLinus Torvalds  */
6951da177e4SLinus Torvalds 
6961da177e4SLinus Torvalds static int ipmr_mfc_delete(struct mfcctl *mfc)
6971da177e4SLinus Torvalds {
6981da177e4SLinus Torvalds 	int line;
6991da177e4SLinus Torvalds 	struct mfc_cache *c, **cp;
7001da177e4SLinus Torvalds 
7011da177e4SLinus Torvalds 	line=MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr);
7021da177e4SLinus Torvalds 
7031da177e4SLinus Torvalds 	for (cp=&mfc_cache_array[line]; (c=*cp) != NULL; cp = &c->next) {
7041da177e4SLinus Torvalds 		if (c->mfc_origin == mfc->mfcc_origin.s_addr &&
7051da177e4SLinus Torvalds 		    c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr) {
7061da177e4SLinus Torvalds 			write_lock_bh(&mrt_lock);
7071da177e4SLinus Torvalds 			*cp = c->next;
7081da177e4SLinus Torvalds 			write_unlock_bh(&mrt_lock);
7091da177e4SLinus Torvalds 
7101da177e4SLinus Torvalds 			kmem_cache_free(mrt_cachep, c);
7111da177e4SLinus Torvalds 			return 0;
7121da177e4SLinus Torvalds 		}
7131da177e4SLinus Torvalds 	}
7141da177e4SLinus Torvalds 	return -ENOENT;
7151da177e4SLinus Torvalds }
7161da177e4SLinus Torvalds 
7171da177e4SLinus Torvalds static int ipmr_mfc_add(struct mfcctl *mfc, int mrtsock)
7181da177e4SLinus Torvalds {
7191da177e4SLinus Torvalds 	int line;
7201da177e4SLinus Torvalds 	struct mfc_cache *uc, *c, **cp;
7211da177e4SLinus Torvalds 
7221da177e4SLinus Torvalds 	line=MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr);
7231da177e4SLinus Torvalds 
7241da177e4SLinus Torvalds 	for (cp=&mfc_cache_array[line]; (c=*cp) != NULL; cp = &c->next) {
7251da177e4SLinus Torvalds 		if (c->mfc_origin == mfc->mfcc_origin.s_addr &&
7261da177e4SLinus Torvalds 		    c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr)
7271da177e4SLinus Torvalds 			break;
7281da177e4SLinus Torvalds 	}
7291da177e4SLinus Torvalds 
7301da177e4SLinus Torvalds 	if (c != NULL) {
7311da177e4SLinus Torvalds 		write_lock_bh(&mrt_lock);
7321da177e4SLinus Torvalds 		c->mfc_parent = mfc->mfcc_parent;
733d1b04c08SBaruch Even 		ipmr_update_thresholds(c, mfc->mfcc_ttls);
7341da177e4SLinus Torvalds 		if (!mrtsock)
7351da177e4SLinus Torvalds 			c->mfc_flags |= MFC_STATIC;
7361da177e4SLinus Torvalds 		write_unlock_bh(&mrt_lock);
7371da177e4SLinus Torvalds 		return 0;
7381da177e4SLinus Torvalds 	}
7391da177e4SLinus Torvalds 
7401da177e4SLinus Torvalds 	if(!MULTICAST(mfc->mfcc_mcastgrp.s_addr))
7411da177e4SLinus Torvalds 		return -EINVAL;
7421da177e4SLinus Torvalds 
7431da177e4SLinus Torvalds 	c=ipmr_cache_alloc();
7441da177e4SLinus Torvalds 	if (c==NULL)
7451da177e4SLinus Torvalds 		return -ENOMEM;
7461da177e4SLinus Torvalds 
7471da177e4SLinus Torvalds 	c->mfc_origin=mfc->mfcc_origin.s_addr;
7481da177e4SLinus Torvalds 	c->mfc_mcastgrp=mfc->mfcc_mcastgrp.s_addr;
7491da177e4SLinus Torvalds 	c->mfc_parent=mfc->mfcc_parent;
750d1b04c08SBaruch Even 	ipmr_update_thresholds(c, mfc->mfcc_ttls);
7511da177e4SLinus Torvalds 	if (!mrtsock)
7521da177e4SLinus Torvalds 		c->mfc_flags |= MFC_STATIC;
7531da177e4SLinus Torvalds 
7541da177e4SLinus Torvalds 	write_lock_bh(&mrt_lock);
7551da177e4SLinus Torvalds 	c->next = mfc_cache_array[line];
7561da177e4SLinus Torvalds 	mfc_cache_array[line] = c;
7571da177e4SLinus Torvalds 	write_unlock_bh(&mrt_lock);
7581da177e4SLinus Torvalds 
7591da177e4SLinus Torvalds 	/*
7601da177e4SLinus Torvalds 	 *	Check to see if we resolved a queued list. If so we
7611da177e4SLinus Torvalds 	 *	need to send on the frames and tidy up.
7621da177e4SLinus Torvalds 	 */
7631da177e4SLinus Torvalds 	spin_lock_bh(&mfc_unres_lock);
7641da177e4SLinus Torvalds 	for (cp = &mfc_unres_queue; (uc=*cp) != NULL;
7651da177e4SLinus Torvalds 	     cp = &uc->next) {
7661da177e4SLinus Torvalds 		if (uc->mfc_origin == c->mfc_origin &&
7671da177e4SLinus Torvalds 		    uc->mfc_mcastgrp == c->mfc_mcastgrp) {
7681da177e4SLinus Torvalds 			*cp = uc->next;
7691da177e4SLinus Torvalds 			if (atomic_dec_and_test(&cache_resolve_queue_len))
7701da177e4SLinus Torvalds 				del_timer(&ipmr_expire_timer);
7711da177e4SLinus Torvalds 			break;
7721da177e4SLinus Torvalds 		}
7731da177e4SLinus Torvalds 	}
7741da177e4SLinus Torvalds 	spin_unlock_bh(&mfc_unres_lock);
7751da177e4SLinus Torvalds 
7761da177e4SLinus Torvalds 	if (uc) {
7771da177e4SLinus Torvalds 		ipmr_cache_resolve(uc, c);
7781da177e4SLinus Torvalds 		kmem_cache_free(mrt_cachep, uc);
7791da177e4SLinus Torvalds 	}
7801da177e4SLinus Torvalds 	return 0;
7811da177e4SLinus Torvalds }
7821da177e4SLinus Torvalds 
7831da177e4SLinus Torvalds /*
7841da177e4SLinus Torvalds  *	Close the multicast socket, and clear the vif tables etc
7851da177e4SLinus Torvalds  */
7861da177e4SLinus Torvalds 
7871da177e4SLinus Torvalds static void mroute_clean_tables(struct sock *sk)
7881da177e4SLinus Torvalds {
7891da177e4SLinus Torvalds 	int i;
7901da177e4SLinus Torvalds 
7911da177e4SLinus Torvalds 	/*
7921da177e4SLinus Torvalds 	 *	Shut down all active vif entries
7931da177e4SLinus Torvalds 	 */
7941da177e4SLinus Torvalds 	for(i=0; i<maxvif; i++) {
7951da177e4SLinus Torvalds 		if (!(vif_table[i].flags&VIFF_STATIC))
7961da177e4SLinus Torvalds 			vif_delete(i);
7971da177e4SLinus Torvalds 	}
7981da177e4SLinus Torvalds 
7991da177e4SLinus Torvalds 	/*
8001da177e4SLinus Torvalds 	 *	Wipe the cache
8011da177e4SLinus Torvalds 	 */
8021da177e4SLinus Torvalds 	for (i=0;i<MFC_LINES;i++) {
8031da177e4SLinus Torvalds 		struct mfc_cache *c, **cp;
8041da177e4SLinus Torvalds 
8051da177e4SLinus Torvalds 		cp = &mfc_cache_array[i];
8061da177e4SLinus Torvalds 		while ((c = *cp) != NULL) {
8071da177e4SLinus Torvalds 			if (c->mfc_flags&MFC_STATIC) {
8081da177e4SLinus Torvalds 				cp = &c->next;
8091da177e4SLinus Torvalds 				continue;
8101da177e4SLinus Torvalds 			}
8111da177e4SLinus Torvalds 			write_lock_bh(&mrt_lock);
8121da177e4SLinus Torvalds 			*cp = c->next;
8131da177e4SLinus Torvalds 			write_unlock_bh(&mrt_lock);
8141da177e4SLinus Torvalds 
8151da177e4SLinus Torvalds 			kmem_cache_free(mrt_cachep, c);
8161da177e4SLinus Torvalds 		}
8171da177e4SLinus Torvalds 	}
8181da177e4SLinus Torvalds 
8191da177e4SLinus Torvalds 	if (atomic_read(&cache_resolve_queue_len) != 0) {
8201da177e4SLinus Torvalds 		struct mfc_cache *c;
8211da177e4SLinus Torvalds 
8221da177e4SLinus Torvalds 		spin_lock_bh(&mfc_unres_lock);
8231da177e4SLinus Torvalds 		while (mfc_unres_queue != NULL) {
8241da177e4SLinus Torvalds 			c = mfc_unres_queue;
8251da177e4SLinus Torvalds 			mfc_unres_queue = c->next;
8261da177e4SLinus Torvalds 			spin_unlock_bh(&mfc_unres_lock);
8271da177e4SLinus Torvalds 
8281da177e4SLinus Torvalds 			ipmr_destroy_unres(c);
8291da177e4SLinus Torvalds 
8301da177e4SLinus Torvalds 			spin_lock_bh(&mfc_unres_lock);
8311da177e4SLinus Torvalds 		}
8321da177e4SLinus Torvalds 		spin_unlock_bh(&mfc_unres_lock);
8331da177e4SLinus Torvalds 	}
8341da177e4SLinus Torvalds }
8351da177e4SLinus Torvalds 
8361da177e4SLinus Torvalds static void mrtsock_destruct(struct sock *sk)
8371da177e4SLinus Torvalds {
8381da177e4SLinus Torvalds 	rtnl_lock();
8391da177e4SLinus Torvalds 	if (sk == mroute_socket) {
8401da177e4SLinus Torvalds 		ipv4_devconf.mc_forwarding--;
8411da177e4SLinus Torvalds 
8421da177e4SLinus Torvalds 		write_lock_bh(&mrt_lock);
8431da177e4SLinus Torvalds 		mroute_socket=NULL;
8441da177e4SLinus Torvalds 		write_unlock_bh(&mrt_lock);
8451da177e4SLinus Torvalds 
8461da177e4SLinus Torvalds 		mroute_clean_tables(sk);
8471da177e4SLinus Torvalds 	}
8481da177e4SLinus Torvalds 	rtnl_unlock();
8491da177e4SLinus Torvalds }
8501da177e4SLinus Torvalds 
8511da177e4SLinus Torvalds /*
8521da177e4SLinus Torvalds  *	Socket options and virtual interface manipulation. The whole
8531da177e4SLinus Torvalds  *	virtual interface system is a complete heap, but unfortunately
8541da177e4SLinus Torvalds  *	that's how BSD mrouted happens to think. Maybe one day with a proper
8551da177e4SLinus Torvalds  *	MOSPF/PIM router set up we can clean this up.
8561da177e4SLinus Torvalds  */
8571da177e4SLinus Torvalds 
8581da177e4SLinus Torvalds int ip_mroute_setsockopt(struct sock *sk,int optname,char __user *optval,int optlen)
8591da177e4SLinus Torvalds {
8601da177e4SLinus Torvalds 	int ret;
8611da177e4SLinus Torvalds 	struct vifctl vif;
8621da177e4SLinus Torvalds 	struct mfcctl mfc;
8631da177e4SLinus Torvalds 
8641da177e4SLinus Torvalds 	if(optname!=MRT_INIT)
8651da177e4SLinus Torvalds 	{
8661da177e4SLinus Torvalds 		if(sk!=mroute_socket && !capable(CAP_NET_ADMIN))
8671da177e4SLinus Torvalds 			return -EACCES;
8681da177e4SLinus Torvalds 	}
8691da177e4SLinus Torvalds 
8701da177e4SLinus Torvalds 	switch(optname)
8711da177e4SLinus Torvalds 	{
8721da177e4SLinus Torvalds 		case MRT_INIT:
8731da177e4SLinus Torvalds 			if (sk->sk_type != SOCK_RAW ||
8741da177e4SLinus Torvalds 			    inet_sk(sk)->num != IPPROTO_IGMP)
8751da177e4SLinus Torvalds 				return -EOPNOTSUPP;
8761da177e4SLinus Torvalds 			if(optlen!=sizeof(int))
8771da177e4SLinus Torvalds 				return -ENOPROTOOPT;
8781da177e4SLinus Torvalds 
8791da177e4SLinus Torvalds 			rtnl_lock();
8801da177e4SLinus Torvalds 			if (mroute_socket) {
8811da177e4SLinus Torvalds 				rtnl_unlock();
8821da177e4SLinus Torvalds 				return -EADDRINUSE;
8831da177e4SLinus Torvalds 			}
8841da177e4SLinus Torvalds 
8851da177e4SLinus Torvalds 			ret = ip_ra_control(sk, 1, mrtsock_destruct);
8861da177e4SLinus Torvalds 			if (ret == 0) {
8871da177e4SLinus Torvalds 				write_lock_bh(&mrt_lock);
8881da177e4SLinus Torvalds 				mroute_socket=sk;
8891da177e4SLinus Torvalds 				write_unlock_bh(&mrt_lock);
8901da177e4SLinus Torvalds 
8911da177e4SLinus Torvalds 				ipv4_devconf.mc_forwarding++;
8921da177e4SLinus Torvalds 			}
8931da177e4SLinus Torvalds 			rtnl_unlock();
8941da177e4SLinus Torvalds 			return ret;
8951da177e4SLinus Torvalds 		case MRT_DONE:
8961da177e4SLinus Torvalds 			if (sk!=mroute_socket)
8971da177e4SLinus Torvalds 				return -EACCES;
8981da177e4SLinus Torvalds 			return ip_ra_control(sk, 0, NULL);
8991da177e4SLinus Torvalds 		case MRT_ADD_VIF:
9001da177e4SLinus Torvalds 		case MRT_DEL_VIF:
9011da177e4SLinus Torvalds 			if(optlen!=sizeof(vif))
9021da177e4SLinus Torvalds 				return -EINVAL;
9031da177e4SLinus Torvalds 			if (copy_from_user(&vif,optval,sizeof(vif)))
9041da177e4SLinus Torvalds 				return -EFAULT;
9051da177e4SLinus Torvalds 			if(vif.vifc_vifi >= MAXVIFS)
9061da177e4SLinus Torvalds 				return -ENFILE;
9071da177e4SLinus Torvalds 			rtnl_lock();
9081da177e4SLinus Torvalds 			if (optname==MRT_ADD_VIF) {
9091da177e4SLinus Torvalds 				ret = vif_add(&vif, sk==mroute_socket);
9101da177e4SLinus Torvalds 			} else {
9111da177e4SLinus Torvalds 				ret = vif_delete(vif.vifc_vifi);
9121da177e4SLinus Torvalds 			}
9131da177e4SLinus Torvalds 			rtnl_unlock();
9141da177e4SLinus Torvalds 			return ret;
9151da177e4SLinus Torvalds 
9161da177e4SLinus Torvalds 		/*
9171da177e4SLinus Torvalds 		 *	Manipulate the forwarding caches. These live
9181da177e4SLinus Torvalds 		 *	in a sort of kernel/user symbiosis.
9191da177e4SLinus Torvalds 		 */
9201da177e4SLinus Torvalds 		case MRT_ADD_MFC:
9211da177e4SLinus Torvalds 		case MRT_DEL_MFC:
9221da177e4SLinus Torvalds 			if(optlen!=sizeof(mfc))
9231da177e4SLinus Torvalds 				return -EINVAL;
9241da177e4SLinus Torvalds 			if (copy_from_user(&mfc,optval, sizeof(mfc)))
9251da177e4SLinus Torvalds 				return -EFAULT;
9261da177e4SLinus Torvalds 			rtnl_lock();
9271da177e4SLinus Torvalds 			if (optname==MRT_DEL_MFC)
9281da177e4SLinus Torvalds 				ret = ipmr_mfc_delete(&mfc);
9291da177e4SLinus Torvalds 			else
9301da177e4SLinus Torvalds 				ret = ipmr_mfc_add(&mfc, sk==mroute_socket);
9311da177e4SLinus Torvalds 			rtnl_unlock();
9321da177e4SLinus Torvalds 			return ret;
9331da177e4SLinus Torvalds 		/*
9341da177e4SLinus Torvalds 		 *	Control PIM assert.
9351da177e4SLinus Torvalds 		 */
9361da177e4SLinus Torvalds 		case MRT_ASSERT:
9371da177e4SLinus Torvalds 		{
9381da177e4SLinus Torvalds 			int v;
9391da177e4SLinus Torvalds 			if(get_user(v,(int __user *)optval))
9401da177e4SLinus Torvalds 				return -EFAULT;
9411da177e4SLinus Torvalds 			mroute_do_assert=(v)?1:0;
9421da177e4SLinus Torvalds 			return 0;
9431da177e4SLinus Torvalds 		}
9441da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM
9451da177e4SLinus Torvalds 		case MRT_PIM:
9461da177e4SLinus Torvalds 		{
9471da177e4SLinus Torvalds 			int v, ret;
9481da177e4SLinus Torvalds 			if(get_user(v,(int __user *)optval))
9491da177e4SLinus Torvalds 				return -EFAULT;
9501da177e4SLinus Torvalds 			v = (v)?1:0;
9511da177e4SLinus Torvalds 			rtnl_lock();
9521da177e4SLinus Torvalds 			ret = 0;
9531da177e4SLinus Torvalds 			if (v != mroute_do_pim) {
9541da177e4SLinus Torvalds 				mroute_do_pim = v;
9551da177e4SLinus Torvalds 				mroute_do_assert = v;
9561da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM_V2
9571da177e4SLinus Torvalds 				if (mroute_do_pim)
9581da177e4SLinus Torvalds 					ret = inet_add_protocol(&pim_protocol,
9591da177e4SLinus Torvalds 								IPPROTO_PIM);
9601da177e4SLinus Torvalds 				else
9611da177e4SLinus Torvalds 					ret = inet_del_protocol(&pim_protocol,
9621da177e4SLinus Torvalds 								IPPROTO_PIM);
9631da177e4SLinus Torvalds 				if (ret < 0)
9641da177e4SLinus Torvalds 					ret = -EAGAIN;
9651da177e4SLinus Torvalds #endif
9661da177e4SLinus Torvalds 			}
9671da177e4SLinus Torvalds 			rtnl_unlock();
9681da177e4SLinus Torvalds 			return ret;
9691da177e4SLinus Torvalds 		}
9701da177e4SLinus Torvalds #endif
9711da177e4SLinus Torvalds 		/*
9721da177e4SLinus Torvalds 		 *	Spurious command, or MRT_VERSION which you cannot
9731da177e4SLinus Torvalds 		 *	set.
9741da177e4SLinus Torvalds 		 */
9751da177e4SLinus Torvalds 		default:
9761da177e4SLinus Torvalds 			return -ENOPROTOOPT;
9771da177e4SLinus Torvalds 	}
9781da177e4SLinus Torvalds }
9791da177e4SLinus Torvalds 
9801da177e4SLinus Torvalds /*
9811da177e4SLinus Torvalds  *	Getsock opt support for the multicast routing system.
9821da177e4SLinus Torvalds  */
9831da177e4SLinus Torvalds 
9841da177e4SLinus Torvalds int ip_mroute_getsockopt(struct sock *sk,int optname,char __user *optval,int __user *optlen)
9851da177e4SLinus Torvalds {
9861da177e4SLinus Torvalds 	int olr;
9871da177e4SLinus Torvalds 	int val;
9881da177e4SLinus Torvalds 
9891da177e4SLinus Torvalds 	if(optname!=MRT_VERSION &&
9901da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM
9911da177e4SLinus Torvalds 	   optname!=MRT_PIM &&
9921da177e4SLinus Torvalds #endif
9931da177e4SLinus Torvalds 	   optname!=MRT_ASSERT)
9941da177e4SLinus Torvalds 		return -ENOPROTOOPT;
9951da177e4SLinus Torvalds 
9961da177e4SLinus Torvalds 	if (get_user(olr, optlen))
9971da177e4SLinus Torvalds 		return -EFAULT;
9981da177e4SLinus Torvalds 
9991da177e4SLinus Torvalds 	olr = min_t(unsigned int, olr, sizeof(int));
10001da177e4SLinus Torvalds 	if (olr < 0)
10011da177e4SLinus Torvalds 		return -EINVAL;
10021da177e4SLinus Torvalds 
10031da177e4SLinus Torvalds 	if(put_user(olr,optlen))
10041da177e4SLinus Torvalds 		return -EFAULT;
10051da177e4SLinus Torvalds 	if(optname==MRT_VERSION)
10061da177e4SLinus Torvalds 		val=0x0305;
10071da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM
10081da177e4SLinus Torvalds 	else if(optname==MRT_PIM)
10091da177e4SLinus Torvalds 		val=mroute_do_pim;
10101da177e4SLinus Torvalds #endif
10111da177e4SLinus Torvalds 	else
10121da177e4SLinus Torvalds 		val=mroute_do_assert;
10131da177e4SLinus Torvalds 	if(copy_to_user(optval,&val,olr))
10141da177e4SLinus Torvalds 		return -EFAULT;
10151da177e4SLinus Torvalds 	return 0;
10161da177e4SLinus Torvalds }
10171da177e4SLinus Torvalds 
10181da177e4SLinus Torvalds /*
10191da177e4SLinus Torvalds  *	The IP multicast ioctl support routines.
10201da177e4SLinus Torvalds  */
10211da177e4SLinus Torvalds 
10221da177e4SLinus Torvalds int ipmr_ioctl(struct sock *sk, int cmd, void __user *arg)
10231da177e4SLinus Torvalds {
10241da177e4SLinus Torvalds 	struct sioc_sg_req sr;
10251da177e4SLinus Torvalds 	struct sioc_vif_req vr;
10261da177e4SLinus Torvalds 	struct vif_device *vif;
10271da177e4SLinus Torvalds 	struct mfc_cache *c;
10281da177e4SLinus Torvalds 
10291da177e4SLinus Torvalds 	switch(cmd)
10301da177e4SLinus Torvalds 	{
10311da177e4SLinus Torvalds 		case SIOCGETVIFCNT:
10321da177e4SLinus Torvalds 			if (copy_from_user(&vr,arg,sizeof(vr)))
10331da177e4SLinus Torvalds 				return -EFAULT;
10341da177e4SLinus Torvalds 			if(vr.vifi>=maxvif)
10351da177e4SLinus Torvalds 				return -EINVAL;
10361da177e4SLinus Torvalds 			read_lock(&mrt_lock);
10371da177e4SLinus Torvalds 			vif=&vif_table[vr.vifi];
10381da177e4SLinus Torvalds 			if(VIF_EXISTS(vr.vifi))	{
10391da177e4SLinus Torvalds 				vr.icount=vif->pkt_in;
10401da177e4SLinus Torvalds 				vr.ocount=vif->pkt_out;
10411da177e4SLinus Torvalds 				vr.ibytes=vif->bytes_in;
10421da177e4SLinus Torvalds 				vr.obytes=vif->bytes_out;
10431da177e4SLinus Torvalds 				read_unlock(&mrt_lock);
10441da177e4SLinus Torvalds 
10451da177e4SLinus Torvalds 				if (copy_to_user(arg,&vr,sizeof(vr)))
10461da177e4SLinus Torvalds 					return -EFAULT;
10471da177e4SLinus Torvalds 				return 0;
10481da177e4SLinus Torvalds 			}
10491da177e4SLinus Torvalds 			read_unlock(&mrt_lock);
10501da177e4SLinus Torvalds 			return -EADDRNOTAVAIL;
10511da177e4SLinus Torvalds 		case SIOCGETSGCNT:
10521da177e4SLinus Torvalds 			if (copy_from_user(&sr,arg,sizeof(sr)))
10531da177e4SLinus Torvalds 				return -EFAULT;
10541da177e4SLinus Torvalds 
10551da177e4SLinus Torvalds 			read_lock(&mrt_lock);
10561da177e4SLinus Torvalds 			c = ipmr_cache_find(sr.src.s_addr, sr.grp.s_addr);
10571da177e4SLinus Torvalds 			if (c) {
10581da177e4SLinus Torvalds 				sr.pktcnt = c->mfc_un.res.pkt;
10591da177e4SLinus Torvalds 				sr.bytecnt = c->mfc_un.res.bytes;
10601da177e4SLinus Torvalds 				sr.wrong_if = c->mfc_un.res.wrong_if;
10611da177e4SLinus Torvalds 				read_unlock(&mrt_lock);
10621da177e4SLinus Torvalds 
10631da177e4SLinus Torvalds 				if (copy_to_user(arg,&sr,sizeof(sr)))
10641da177e4SLinus Torvalds 					return -EFAULT;
10651da177e4SLinus Torvalds 				return 0;
10661da177e4SLinus Torvalds 			}
10671da177e4SLinus Torvalds 			read_unlock(&mrt_lock);
10681da177e4SLinus Torvalds 			return -EADDRNOTAVAIL;
10691da177e4SLinus Torvalds 		default:
10701da177e4SLinus Torvalds 			return -ENOIOCTLCMD;
10711da177e4SLinus Torvalds 	}
10721da177e4SLinus Torvalds }
10731da177e4SLinus Torvalds 
10741da177e4SLinus Torvalds 
10751da177e4SLinus Torvalds static int ipmr_device_event(struct notifier_block *this, unsigned long event, void *ptr)
10761da177e4SLinus Torvalds {
10771da177e4SLinus Torvalds 	struct vif_device *v;
10781da177e4SLinus Torvalds 	int ct;
10791da177e4SLinus Torvalds 	if (event != NETDEV_UNREGISTER)
10801da177e4SLinus Torvalds 		return NOTIFY_DONE;
10811da177e4SLinus Torvalds 	v=&vif_table[0];
10821da177e4SLinus Torvalds 	for(ct=0;ct<maxvif;ct++,v++) {
10831da177e4SLinus Torvalds 		if (v->dev==ptr)
10841da177e4SLinus Torvalds 			vif_delete(ct);
10851da177e4SLinus Torvalds 	}
10861da177e4SLinus Torvalds 	return NOTIFY_DONE;
10871da177e4SLinus Torvalds }
10881da177e4SLinus Torvalds 
10891da177e4SLinus Torvalds 
10901da177e4SLinus Torvalds static struct notifier_block ip_mr_notifier={
10911da177e4SLinus Torvalds 	.notifier_call = ipmr_device_event,
10921da177e4SLinus Torvalds };
10931da177e4SLinus Torvalds 
10941da177e4SLinus Torvalds /*
10951da177e4SLinus Torvalds  * 	Encapsulate a packet by attaching a valid IPIP header to it.
10961da177e4SLinus Torvalds  *	This avoids tunnel drivers and other mess and gives us the speed so
10971da177e4SLinus Torvalds  *	important for multicast video.
10981da177e4SLinus Torvalds  */
10991da177e4SLinus Torvalds 
1100114c7844SAl Viro static void ip_encap(struct sk_buff *skb, __be32 saddr, __be32 daddr)
11011da177e4SLinus Torvalds {
11021da177e4SLinus Torvalds 	struct iphdr *iph = (struct iphdr *)skb_push(skb,sizeof(struct iphdr));
11031da177e4SLinus Torvalds 
11041da177e4SLinus Torvalds 	iph->version	= 	4;
11051da177e4SLinus Torvalds 	iph->tos	=	skb->nh.iph->tos;
11061da177e4SLinus Torvalds 	iph->ttl	=	skb->nh.iph->ttl;
11071da177e4SLinus Torvalds 	iph->frag_off	=	0;
11081da177e4SLinus Torvalds 	iph->daddr	=	daddr;
11091da177e4SLinus Torvalds 	iph->saddr	=	saddr;
11101da177e4SLinus Torvalds 	iph->protocol	=	IPPROTO_IPIP;
11111da177e4SLinus Torvalds 	iph->ihl	=	5;
11121da177e4SLinus Torvalds 	iph->tot_len	=	htons(skb->len);
11131da177e4SLinus Torvalds 	ip_select_ident(iph, skb->dst, NULL);
11141da177e4SLinus Torvalds 	ip_send_check(iph);
11151da177e4SLinus Torvalds 
11161da177e4SLinus Torvalds 	skb->h.ipiph = skb->nh.iph;
11171da177e4SLinus Torvalds 	skb->nh.iph = iph;
11181da177e4SLinus Torvalds 	memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
11191da177e4SLinus Torvalds 	nf_reset(skb);
11201da177e4SLinus Torvalds }
11211da177e4SLinus Torvalds 
11221da177e4SLinus Torvalds static inline int ipmr_forward_finish(struct sk_buff *skb)
11231da177e4SLinus Torvalds {
11241da177e4SLinus Torvalds 	struct ip_options * opt	= &(IPCB(skb)->opt);
11251da177e4SLinus Torvalds 
11261da177e4SLinus Torvalds 	IP_INC_STATS_BH(IPSTATS_MIB_OUTFORWDATAGRAMS);
11271da177e4SLinus Torvalds 
11281da177e4SLinus Torvalds 	if (unlikely(opt->optlen))
11291da177e4SLinus Torvalds 		ip_forward_options(skb);
11301da177e4SLinus Torvalds 
11311da177e4SLinus Torvalds 	return dst_output(skb);
11321da177e4SLinus Torvalds }
11331da177e4SLinus Torvalds 
11341da177e4SLinus Torvalds /*
11351da177e4SLinus Torvalds  *	Processing handlers for ipmr_forward
11361da177e4SLinus Torvalds  */
11371da177e4SLinus Torvalds 
11381da177e4SLinus Torvalds static void ipmr_queue_xmit(struct sk_buff *skb, struct mfc_cache *c, int vifi)
11391da177e4SLinus Torvalds {
11401da177e4SLinus Torvalds 	struct iphdr *iph = skb->nh.iph;
11411da177e4SLinus Torvalds 	struct vif_device *vif = &vif_table[vifi];
11421da177e4SLinus Torvalds 	struct net_device *dev;
11431da177e4SLinus Torvalds 	struct rtable *rt;
11441da177e4SLinus Torvalds 	int    encap = 0;
11451da177e4SLinus Torvalds 
11461da177e4SLinus Torvalds 	if (vif->dev == NULL)
11471da177e4SLinus Torvalds 		goto out_free;
11481da177e4SLinus Torvalds 
11491da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM
11501da177e4SLinus Torvalds 	if (vif->flags & VIFF_REGISTER) {
11511da177e4SLinus Torvalds 		vif->pkt_out++;
11521da177e4SLinus Torvalds 		vif->bytes_out+=skb->len;
11532941a486SPatrick McHardy 		((struct net_device_stats*)netdev_priv(vif->dev))->tx_bytes += skb->len;
11542941a486SPatrick McHardy 		((struct net_device_stats*)netdev_priv(vif->dev))->tx_packets++;
11551da177e4SLinus Torvalds 		ipmr_cache_report(skb, vifi, IGMPMSG_WHOLEPKT);
11561da177e4SLinus Torvalds 		kfree_skb(skb);
11571da177e4SLinus Torvalds 		return;
11581da177e4SLinus Torvalds 	}
11591da177e4SLinus Torvalds #endif
11601da177e4SLinus Torvalds 
11611da177e4SLinus Torvalds 	if (vif->flags&VIFF_TUNNEL) {
11621da177e4SLinus Torvalds 		struct flowi fl = { .oif = vif->link,
11631da177e4SLinus Torvalds 				    .nl_u = { .ip4_u =
11641da177e4SLinus Torvalds 					      { .daddr = vif->remote,
11651da177e4SLinus Torvalds 						.saddr = vif->local,
11661da177e4SLinus Torvalds 						.tos = RT_TOS(iph->tos) } },
11671da177e4SLinus Torvalds 				    .proto = IPPROTO_IPIP };
11681da177e4SLinus Torvalds 		if (ip_route_output_key(&rt, &fl))
11691da177e4SLinus Torvalds 			goto out_free;
11701da177e4SLinus Torvalds 		encap = sizeof(struct iphdr);
11711da177e4SLinus Torvalds 	} else {
11721da177e4SLinus Torvalds 		struct flowi fl = { .oif = vif->link,
11731da177e4SLinus Torvalds 				    .nl_u = { .ip4_u =
11741da177e4SLinus Torvalds 					      { .daddr = iph->daddr,
11751da177e4SLinus Torvalds 						.tos = RT_TOS(iph->tos) } },
11761da177e4SLinus Torvalds 				    .proto = IPPROTO_IPIP };
11771da177e4SLinus Torvalds 		if (ip_route_output_key(&rt, &fl))
11781da177e4SLinus Torvalds 			goto out_free;
11791da177e4SLinus Torvalds 	}
11801da177e4SLinus Torvalds 
11811da177e4SLinus Torvalds 	dev = rt->u.dst.dev;
11821da177e4SLinus Torvalds 
11831da177e4SLinus Torvalds 	if (skb->len+encap > dst_mtu(&rt->u.dst) && (ntohs(iph->frag_off) & IP_DF)) {
11841da177e4SLinus Torvalds 		/* Do not fragment multicasts. Alas, IPv4 does not
11851da177e4SLinus Torvalds 		   allow to send ICMP, so that packets will disappear
11861da177e4SLinus Torvalds 		   to blackhole.
11871da177e4SLinus Torvalds 		 */
11881da177e4SLinus Torvalds 
11891da177e4SLinus Torvalds 		IP_INC_STATS_BH(IPSTATS_MIB_FRAGFAILS);
11901da177e4SLinus Torvalds 		ip_rt_put(rt);
11911da177e4SLinus Torvalds 		goto out_free;
11921da177e4SLinus Torvalds 	}
11931da177e4SLinus Torvalds 
11941da177e4SLinus Torvalds 	encap += LL_RESERVED_SPACE(dev) + rt->u.dst.header_len;
11951da177e4SLinus Torvalds 
11961da177e4SLinus Torvalds 	if (skb_cow(skb, encap)) {
11971da177e4SLinus Torvalds  		ip_rt_put(rt);
11981da177e4SLinus Torvalds 		goto out_free;
11991da177e4SLinus Torvalds 	}
12001da177e4SLinus Torvalds 
12011da177e4SLinus Torvalds 	vif->pkt_out++;
12021da177e4SLinus Torvalds 	vif->bytes_out+=skb->len;
12031da177e4SLinus Torvalds 
12041da177e4SLinus Torvalds 	dst_release(skb->dst);
12051da177e4SLinus Torvalds 	skb->dst = &rt->u.dst;
12061da177e4SLinus Torvalds 	iph = skb->nh.iph;
12071da177e4SLinus Torvalds 	ip_decrease_ttl(iph);
12081da177e4SLinus Torvalds 
12091da177e4SLinus Torvalds 	/* FIXME: forward and output firewalls used to be called here.
12101da177e4SLinus Torvalds 	 * What do we do with netfilter? -- RR */
12111da177e4SLinus Torvalds 	if (vif->flags & VIFF_TUNNEL) {
12121da177e4SLinus Torvalds 		ip_encap(skb, vif->local, vif->remote);
12131da177e4SLinus Torvalds 		/* FIXME: extra output firewall step used to be here. --RR */
12142941a486SPatrick McHardy 		((struct ip_tunnel *)netdev_priv(vif->dev))->stat.tx_packets++;
12152941a486SPatrick McHardy 		((struct ip_tunnel *)netdev_priv(vif->dev))->stat.tx_bytes+=skb->len;
12161da177e4SLinus Torvalds 	}
12171da177e4SLinus Torvalds 
12181da177e4SLinus Torvalds 	IPCB(skb)->flags |= IPSKB_FORWARDED;
12191da177e4SLinus Torvalds 
12201da177e4SLinus Torvalds 	/*
12211da177e4SLinus Torvalds 	 * RFC1584 teaches, that DVMRP/PIM router must deliver packets locally
12221da177e4SLinus Torvalds 	 * not only before forwarding, but after forwarding on all output
12231da177e4SLinus Torvalds 	 * interfaces. It is clear, if mrouter runs a multicasting
12241da177e4SLinus Torvalds 	 * program, it should receive packets not depending to what interface
12251da177e4SLinus Torvalds 	 * program is joined.
12261da177e4SLinus Torvalds 	 * If we will not make it, the program will have to join on all
12271da177e4SLinus Torvalds 	 * interfaces. On the other hand, multihoming host (or router, but
12281da177e4SLinus Torvalds 	 * not mrouter) cannot join to more than one interface - it will
12291da177e4SLinus Torvalds 	 * result in receiving multiple packets.
12301da177e4SLinus Torvalds 	 */
12311da177e4SLinus Torvalds 	NF_HOOK(PF_INET, NF_IP_FORWARD, skb, skb->dev, dev,
12321da177e4SLinus Torvalds 		ipmr_forward_finish);
12331da177e4SLinus Torvalds 	return;
12341da177e4SLinus Torvalds 
12351da177e4SLinus Torvalds out_free:
12361da177e4SLinus Torvalds 	kfree_skb(skb);
12371da177e4SLinus Torvalds 	return;
12381da177e4SLinus Torvalds }
12391da177e4SLinus Torvalds 
12401da177e4SLinus Torvalds static int ipmr_find_vif(struct net_device *dev)
12411da177e4SLinus Torvalds {
12421da177e4SLinus Torvalds 	int ct;
12431da177e4SLinus Torvalds 	for (ct=maxvif-1; ct>=0; ct--) {
12441da177e4SLinus Torvalds 		if (vif_table[ct].dev == dev)
12451da177e4SLinus Torvalds 			break;
12461da177e4SLinus Torvalds 	}
12471da177e4SLinus Torvalds 	return ct;
12481da177e4SLinus Torvalds }
12491da177e4SLinus Torvalds 
12501da177e4SLinus Torvalds /* "local" means that we should preserve one skb (for local delivery) */
12511da177e4SLinus Torvalds 
12521da177e4SLinus Torvalds static int ip_mr_forward(struct sk_buff *skb, struct mfc_cache *cache, int local)
12531da177e4SLinus Torvalds {
12541da177e4SLinus Torvalds 	int psend = -1;
12551da177e4SLinus Torvalds 	int vif, ct;
12561da177e4SLinus Torvalds 
12571da177e4SLinus Torvalds 	vif = cache->mfc_parent;
12581da177e4SLinus Torvalds 	cache->mfc_un.res.pkt++;
12591da177e4SLinus Torvalds 	cache->mfc_un.res.bytes += skb->len;
12601da177e4SLinus Torvalds 
12611da177e4SLinus Torvalds 	/*
12621da177e4SLinus Torvalds 	 * Wrong interface: drop packet and (maybe) send PIM assert.
12631da177e4SLinus Torvalds 	 */
12641da177e4SLinus Torvalds 	if (vif_table[vif].dev != skb->dev) {
12651da177e4SLinus Torvalds 		int true_vifi;
12661da177e4SLinus Torvalds 
12671da177e4SLinus Torvalds 		if (((struct rtable*)skb->dst)->fl.iif == 0) {
12681da177e4SLinus Torvalds 			/* It is our own packet, looped back.
12691da177e4SLinus Torvalds 			   Very complicated situation...
12701da177e4SLinus Torvalds 
12711da177e4SLinus Torvalds 			   The best workaround until routing daemons will be
12721da177e4SLinus Torvalds 			   fixed is not to redistribute packet, if it was
12731da177e4SLinus Torvalds 			   send through wrong interface. It means, that
12741da177e4SLinus Torvalds 			   multicast applications WILL NOT work for
12751da177e4SLinus Torvalds 			   (S,G), which have default multicast route pointing
12761da177e4SLinus Torvalds 			   to wrong oif. In any case, it is not a good
12771da177e4SLinus Torvalds 			   idea to use multicasting applications on router.
12781da177e4SLinus Torvalds 			 */
12791da177e4SLinus Torvalds 			goto dont_forward;
12801da177e4SLinus Torvalds 		}
12811da177e4SLinus Torvalds 
12821da177e4SLinus Torvalds 		cache->mfc_un.res.wrong_if++;
12831da177e4SLinus Torvalds 		true_vifi = ipmr_find_vif(skb->dev);
12841da177e4SLinus Torvalds 
12851da177e4SLinus Torvalds 		if (true_vifi >= 0 && mroute_do_assert &&
12861da177e4SLinus Torvalds 		    /* pimsm uses asserts, when switching from RPT to SPT,
12871da177e4SLinus Torvalds 		       so that we cannot check that packet arrived on an oif.
12881da177e4SLinus Torvalds 		       It is bad, but otherwise we would need to move pretty
12891da177e4SLinus Torvalds 		       large chunk of pimd to kernel. Ough... --ANK
12901da177e4SLinus Torvalds 		     */
12911da177e4SLinus Torvalds 		    (mroute_do_pim || cache->mfc_un.res.ttls[true_vifi] < 255) &&
12921da177e4SLinus Torvalds 		    time_after(jiffies,
12931da177e4SLinus Torvalds 			       cache->mfc_un.res.last_assert + MFC_ASSERT_THRESH)) {
12941da177e4SLinus Torvalds 			cache->mfc_un.res.last_assert = jiffies;
12951da177e4SLinus Torvalds 			ipmr_cache_report(skb, true_vifi, IGMPMSG_WRONGVIF);
12961da177e4SLinus Torvalds 		}
12971da177e4SLinus Torvalds 		goto dont_forward;
12981da177e4SLinus Torvalds 	}
12991da177e4SLinus Torvalds 
13001da177e4SLinus Torvalds 	vif_table[vif].pkt_in++;
13011da177e4SLinus Torvalds 	vif_table[vif].bytes_in+=skb->len;
13021da177e4SLinus Torvalds 
13031da177e4SLinus Torvalds 	/*
13041da177e4SLinus Torvalds 	 *	Forward the frame
13051da177e4SLinus Torvalds 	 */
13061da177e4SLinus Torvalds 	for (ct = cache->mfc_un.res.maxvif-1; ct >= cache->mfc_un.res.minvif; ct--) {
13071da177e4SLinus Torvalds 		if (skb->nh.iph->ttl > cache->mfc_un.res.ttls[ct]) {
13081da177e4SLinus Torvalds 			if (psend != -1) {
13091da177e4SLinus Torvalds 				struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
13101da177e4SLinus Torvalds 				if (skb2)
13111da177e4SLinus Torvalds 					ipmr_queue_xmit(skb2, cache, psend);
13121da177e4SLinus Torvalds 			}
13131da177e4SLinus Torvalds 			psend=ct;
13141da177e4SLinus Torvalds 		}
13151da177e4SLinus Torvalds 	}
13161da177e4SLinus Torvalds 	if (psend != -1) {
13171da177e4SLinus Torvalds 		if (local) {
13181da177e4SLinus Torvalds 			struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
13191da177e4SLinus Torvalds 			if (skb2)
13201da177e4SLinus Torvalds 				ipmr_queue_xmit(skb2, cache, psend);
13211da177e4SLinus Torvalds 		} else {
13221da177e4SLinus Torvalds 			ipmr_queue_xmit(skb, cache, psend);
13231da177e4SLinus Torvalds 			return 0;
13241da177e4SLinus Torvalds 		}
13251da177e4SLinus Torvalds 	}
13261da177e4SLinus Torvalds 
13271da177e4SLinus Torvalds dont_forward:
13281da177e4SLinus Torvalds 	if (!local)
13291da177e4SLinus Torvalds 		kfree_skb(skb);
13301da177e4SLinus Torvalds 	return 0;
13311da177e4SLinus Torvalds }
13321da177e4SLinus Torvalds 
13331da177e4SLinus Torvalds 
13341da177e4SLinus Torvalds /*
13351da177e4SLinus Torvalds  *	Multicast packets for forwarding arrive here
13361da177e4SLinus Torvalds  */
13371da177e4SLinus Torvalds 
13381da177e4SLinus Torvalds int ip_mr_input(struct sk_buff *skb)
13391da177e4SLinus Torvalds {
13401da177e4SLinus Torvalds 	struct mfc_cache *cache;
13411da177e4SLinus Torvalds 	int local = ((struct rtable*)skb->dst)->rt_flags&RTCF_LOCAL;
13421da177e4SLinus Torvalds 
13431da177e4SLinus Torvalds 	/* Packet is looped back after forward, it should not be
13441da177e4SLinus Torvalds 	   forwarded second time, but still can be delivered locally.
13451da177e4SLinus Torvalds 	 */
13461da177e4SLinus Torvalds 	if (IPCB(skb)->flags&IPSKB_FORWARDED)
13471da177e4SLinus Torvalds 		goto dont_forward;
13481da177e4SLinus Torvalds 
13491da177e4SLinus Torvalds 	if (!local) {
13501da177e4SLinus Torvalds 		    if (IPCB(skb)->opt.router_alert) {
13511da177e4SLinus Torvalds 			    if (ip_call_ra_chain(skb))
13521da177e4SLinus Torvalds 				    return 0;
13531da177e4SLinus Torvalds 		    } else if (skb->nh.iph->protocol == IPPROTO_IGMP){
13541da177e4SLinus Torvalds 			    /* IGMPv1 (and broken IGMPv2 implementations sort of
13551da177e4SLinus Torvalds 			       Cisco IOS <= 11.2(8)) do not put router alert
13561da177e4SLinus Torvalds 			       option to IGMP packets destined to routable
13571da177e4SLinus Torvalds 			       groups. It is very bad, because it means
13581da177e4SLinus Torvalds 			       that we can forward NO IGMP messages.
13591da177e4SLinus Torvalds 			     */
13601da177e4SLinus Torvalds 			    read_lock(&mrt_lock);
13611da177e4SLinus Torvalds 			    if (mroute_socket) {
13622715bcf9SPatrick McHardy 				    nf_reset(skb);
13631da177e4SLinus Torvalds 				    raw_rcv(mroute_socket, skb);
13641da177e4SLinus Torvalds 				    read_unlock(&mrt_lock);
13651da177e4SLinus Torvalds 				    return 0;
13661da177e4SLinus Torvalds 			    }
13671da177e4SLinus Torvalds 			    read_unlock(&mrt_lock);
13681da177e4SLinus Torvalds 		    }
13691da177e4SLinus Torvalds 	}
13701da177e4SLinus Torvalds 
13711da177e4SLinus Torvalds 	read_lock(&mrt_lock);
13721da177e4SLinus Torvalds 	cache = ipmr_cache_find(skb->nh.iph->saddr, skb->nh.iph->daddr);
13731da177e4SLinus Torvalds 
13741da177e4SLinus Torvalds 	/*
13751da177e4SLinus Torvalds 	 *	No usable cache entry
13761da177e4SLinus Torvalds 	 */
13771da177e4SLinus Torvalds 	if (cache==NULL) {
13781da177e4SLinus Torvalds 		int vif;
13791da177e4SLinus Torvalds 
13801da177e4SLinus Torvalds 		if (local) {
13811da177e4SLinus Torvalds 			struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
13821da177e4SLinus Torvalds 			ip_local_deliver(skb);
13831da177e4SLinus Torvalds 			if (skb2 == NULL) {
13841da177e4SLinus Torvalds 				read_unlock(&mrt_lock);
13851da177e4SLinus Torvalds 				return -ENOBUFS;
13861da177e4SLinus Torvalds 			}
13871da177e4SLinus Torvalds 			skb = skb2;
13881da177e4SLinus Torvalds 		}
13891da177e4SLinus Torvalds 
13901da177e4SLinus Torvalds 		vif = ipmr_find_vif(skb->dev);
13911da177e4SLinus Torvalds 		if (vif >= 0) {
13921da177e4SLinus Torvalds 			int err = ipmr_cache_unresolved(vif, skb);
13931da177e4SLinus Torvalds 			read_unlock(&mrt_lock);
13941da177e4SLinus Torvalds 
13951da177e4SLinus Torvalds 			return err;
13961da177e4SLinus Torvalds 		}
13971da177e4SLinus Torvalds 		read_unlock(&mrt_lock);
13981da177e4SLinus Torvalds 		kfree_skb(skb);
13991da177e4SLinus Torvalds 		return -ENODEV;
14001da177e4SLinus Torvalds 	}
14011da177e4SLinus Torvalds 
14021da177e4SLinus Torvalds 	ip_mr_forward(skb, cache, local);
14031da177e4SLinus Torvalds 
14041da177e4SLinus Torvalds 	read_unlock(&mrt_lock);
14051da177e4SLinus Torvalds 
14061da177e4SLinus Torvalds 	if (local)
14071da177e4SLinus Torvalds 		return ip_local_deliver(skb);
14081da177e4SLinus Torvalds 
14091da177e4SLinus Torvalds 	return 0;
14101da177e4SLinus Torvalds 
14111da177e4SLinus Torvalds dont_forward:
14121da177e4SLinus Torvalds 	if (local)
14131da177e4SLinus Torvalds 		return ip_local_deliver(skb);
14141da177e4SLinus Torvalds 	kfree_skb(skb);
14151da177e4SLinus Torvalds 	return 0;
14161da177e4SLinus Torvalds }
14171da177e4SLinus Torvalds 
14181da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM_V1
14191da177e4SLinus Torvalds /*
14201da177e4SLinus Torvalds  * Handle IGMP messages of PIMv1
14211da177e4SLinus Torvalds  */
14221da177e4SLinus Torvalds 
14231da177e4SLinus Torvalds int pim_rcv_v1(struct sk_buff * skb)
14241da177e4SLinus Torvalds {
14251da177e4SLinus Torvalds 	struct igmphdr *pim;
14261da177e4SLinus Torvalds 	struct iphdr   *encap;
14271da177e4SLinus Torvalds 	struct net_device  *reg_dev = NULL;
14281da177e4SLinus Torvalds 
14291da177e4SLinus Torvalds 	if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(*encap)))
14301da177e4SLinus Torvalds 		goto drop;
14311da177e4SLinus Torvalds 
14321da177e4SLinus Torvalds 	pim = (struct igmphdr*)skb->h.raw;
14331da177e4SLinus Torvalds 
14341da177e4SLinus Torvalds         if (!mroute_do_pim ||
14351da177e4SLinus Torvalds 	    skb->len < sizeof(*pim) + sizeof(*encap) ||
14361da177e4SLinus Torvalds 	    pim->group != PIM_V1_VERSION || pim->code != PIM_V1_REGISTER)
14371da177e4SLinus Torvalds 		goto drop;
14381da177e4SLinus Torvalds 
14391da177e4SLinus Torvalds 	encap = (struct iphdr*)(skb->h.raw + sizeof(struct igmphdr));
14401da177e4SLinus Torvalds 	/*
14411da177e4SLinus Torvalds 	   Check that:
14421da177e4SLinus Torvalds 	   a. packet is really destinted to a multicast group
14431da177e4SLinus Torvalds 	   b. packet is not a NULL-REGISTER
14441da177e4SLinus Torvalds 	   c. packet is not truncated
14451da177e4SLinus Torvalds 	 */
14461da177e4SLinus Torvalds 	if (!MULTICAST(encap->daddr) ||
14471da177e4SLinus Torvalds 	    encap->tot_len == 0 ||
14481da177e4SLinus Torvalds 	    ntohs(encap->tot_len) + sizeof(*pim) > skb->len)
14491da177e4SLinus Torvalds 		goto drop;
14501da177e4SLinus Torvalds 
14511da177e4SLinus Torvalds 	read_lock(&mrt_lock);
14521da177e4SLinus Torvalds 	if (reg_vif_num >= 0)
14531da177e4SLinus Torvalds 		reg_dev = vif_table[reg_vif_num].dev;
14541da177e4SLinus Torvalds 	if (reg_dev)
14551da177e4SLinus Torvalds 		dev_hold(reg_dev);
14561da177e4SLinus Torvalds 	read_unlock(&mrt_lock);
14571da177e4SLinus Torvalds 
14581da177e4SLinus Torvalds 	if (reg_dev == NULL)
14591da177e4SLinus Torvalds 		goto drop;
14601da177e4SLinus Torvalds 
14611da177e4SLinus Torvalds 	skb->mac.raw = skb->nh.raw;
14621da177e4SLinus Torvalds 	skb_pull(skb, (u8*)encap - skb->data);
14631da177e4SLinus Torvalds 	skb->nh.iph = (struct iphdr *)skb->data;
14641da177e4SLinus Torvalds 	skb->dev = reg_dev;
14651da177e4SLinus Torvalds 	skb->protocol = htons(ETH_P_IP);
14661da177e4SLinus Torvalds 	skb->ip_summed = 0;
14671da177e4SLinus Torvalds 	skb->pkt_type = PACKET_HOST;
14681da177e4SLinus Torvalds 	dst_release(skb->dst);
14691da177e4SLinus Torvalds 	skb->dst = NULL;
14702941a486SPatrick McHardy 	((struct net_device_stats*)netdev_priv(reg_dev))->rx_bytes += skb->len;
14712941a486SPatrick McHardy 	((struct net_device_stats*)netdev_priv(reg_dev))->rx_packets++;
14721da177e4SLinus Torvalds 	nf_reset(skb);
14731da177e4SLinus Torvalds 	netif_rx(skb);
14741da177e4SLinus Torvalds 	dev_put(reg_dev);
14751da177e4SLinus Torvalds 	return 0;
14761da177e4SLinus Torvalds  drop:
14771da177e4SLinus Torvalds 	kfree_skb(skb);
14781da177e4SLinus Torvalds 	return 0;
14791da177e4SLinus Torvalds }
14801da177e4SLinus Torvalds #endif
14811da177e4SLinus Torvalds 
14821da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM_V2
14831da177e4SLinus Torvalds static int pim_rcv(struct sk_buff * skb)
14841da177e4SLinus Torvalds {
14851da177e4SLinus Torvalds 	struct pimreghdr *pim;
14861da177e4SLinus Torvalds 	struct iphdr   *encap;
14871da177e4SLinus Torvalds 	struct net_device  *reg_dev = NULL;
14881da177e4SLinus Torvalds 
14891da177e4SLinus Torvalds 	if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(*encap)))
14901da177e4SLinus Torvalds 		goto drop;
14911da177e4SLinus Torvalds 
14921da177e4SLinus Torvalds 	pim = (struct pimreghdr*)skb->h.raw;
14931da177e4SLinus Torvalds         if (pim->type != ((PIM_VERSION<<4)|(PIM_REGISTER)) ||
14941da177e4SLinus Torvalds 	    (pim->flags&PIM_NULL_REGISTER) ||
14951da177e4SLinus Torvalds 	    (ip_compute_csum((void *)pim, sizeof(*pim)) != 0 &&
1496*d3bc23e7SAl Viro 	     csum_fold(skb_checksum(skb, 0, skb->len, 0))))
14971da177e4SLinus Torvalds 		goto drop;
14981da177e4SLinus Torvalds 
14991da177e4SLinus Torvalds 	/* check if the inner packet is destined to mcast group */
15001da177e4SLinus Torvalds 	encap = (struct iphdr*)(skb->h.raw + sizeof(struct pimreghdr));
15011da177e4SLinus Torvalds 	if (!MULTICAST(encap->daddr) ||
15021da177e4SLinus Torvalds 	    encap->tot_len == 0 ||
15031da177e4SLinus Torvalds 	    ntohs(encap->tot_len) + sizeof(*pim) > skb->len)
15041da177e4SLinus Torvalds 		goto drop;
15051da177e4SLinus Torvalds 
15061da177e4SLinus Torvalds 	read_lock(&mrt_lock);
15071da177e4SLinus Torvalds 	if (reg_vif_num >= 0)
15081da177e4SLinus Torvalds 		reg_dev = vif_table[reg_vif_num].dev;
15091da177e4SLinus Torvalds 	if (reg_dev)
15101da177e4SLinus Torvalds 		dev_hold(reg_dev);
15111da177e4SLinus Torvalds 	read_unlock(&mrt_lock);
15121da177e4SLinus Torvalds 
15131da177e4SLinus Torvalds 	if (reg_dev == NULL)
15141da177e4SLinus Torvalds 		goto drop;
15151da177e4SLinus Torvalds 
15161da177e4SLinus Torvalds 	skb->mac.raw = skb->nh.raw;
15171da177e4SLinus Torvalds 	skb_pull(skb, (u8*)encap - skb->data);
15181da177e4SLinus Torvalds 	skb->nh.iph = (struct iphdr *)skb->data;
15191da177e4SLinus Torvalds 	skb->dev = reg_dev;
15201da177e4SLinus Torvalds 	skb->protocol = htons(ETH_P_IP);
15211da177e4SLinus Torvalds 	skb->ip_summed = 0;
15221da177e4SLinus Torvalds 	skb->pkt_type = PACKET_HOST;
15231da177e4SLinus Torvalds 	dst_release(skb->dst);
15242941a486SPatrick McHardy 	((struct net_device_stats*)netdev_priv(reg_dev))->rx_bytes += skb->len;
15252941a486SPatrick McHardy 	((struct net_device_stats*)netdev_priv(reg_dev))->rx_packets++;
15261da177e4SLinus Torvalds 	skb->dst = NULL;
15271da177e4SLinus Torvalds 	nf_reset(skb);
15281da177e4SLinus Torvalds 	netif_rx(skb);
15291da177e4SLinus Torvalds 	dev_put(reg_dev);
15301da177e4SLinus Torvalds 	return 0;
15311da177e4SLinus Torvalds  drop:
15321da177e4SLinus Torvalds 	kfree_skb(skb);
15331da177e4SLinus Torvalds 	return 0;
15341da177e4SLinus Torvalds }
15351da177e4SLinus Torvalds #endif
15361da177e4SLinus Torvalds 
15371da177e4SLinus Torvalds static int
15381da177e4SLinus Torvalds ipmr_fill_mroute(struct sk_buff *skb, struct mfc_cache *c, struct rtmsg *rtm)
15391da177e4SLinus Torvalds {
15401da177e4SLinus Torvalds 	int ct;
15411da177e4SLinus Torvalds 	struct rtnexthop *nhp;
15421da177e4SLinus Torvalds 	struct net_device *dev = vif_table[c->mfc_parent].dev;
15431da177e4SLinus Torvalds 	u8 *b = skb->tail;
15441da177e4SLinus Torvalds 	struct rtattr *mp_head;
15451da177e4SLinus Torvalds 
15461da177e4SLinus Torvalds 	if (dev)
15471da177e4SLinus Torvalds 		RTA_PUT(skb, RTA_IIF, 4, &dev->ifindex);
15481da177e4SLinus Torvalds 
15491da177e4SLinus Torvalds 	mp_head = (struct rtattr*)skb_put(skb, RTA_LENGTH(0));
15501da177e4SLinus Torvalds 
15511da177e4SLinus Torvalds 	for (ct = c->mfc_un.res.minvif; ct < c->mfc_un.res.maxvif; ct++) {
15521da177e4SLinus Torvalds 		if (c->mfc_un.res.ttls[ct] < 255) {
15531da177e4SLinus Torvalds 			if (skb_tailroom(skb) < RTA_ALIGN(RTA_ALIGN(sizeof(*nhp)) + 4))
15541da177e4SLinus Torvalds 				goto rtattr_failure;
15551da177e4SLinus Torvalds 			nhp = (struct rtnexthop*)skb_put(skb, RTA_ALIGN(sizeof(*nhp)));
15561da177e4SLinus Torvalds 			nhp->rtnh_flags = 0;
15571da177e4SLinus Torvalds 			nhp->rtnh_hops = c->mfc_un.res.ttls[ct];
15581da177e4SLinus Torvalds 			nhp->rtnh_ifindex = vif_table[ct].dev->ifindex;
15591da177e4SLinus Torvalds 			nhp->rtnh_len = sizeof(*nhp);
15601da177e4SLinus Torvalds 		}
15611da177e4SLinus Torvalds 	}
15621da177e4SLinus Torvalds 	mp_head->rta_type = RTA_MULTIPATH;
15631da177e4SLinus Torvalds 	mp_head->rta_len = skb->tail - (u8*)mp_head;
15641da177e4SLinus Torvalds 	rtm->rtm_type = RTN_MULTICAST;
15651da177e4SLinus Torvalds 	return 1;
15661da177e4SLinus Torvalds 
15671da177e4SLinus Torvalds rtattr_failure:
15681da177e4SLinus Torvalds 	skb_trim(skb, b - skb->data);
15691da177e4SLinus Torvalds 	return -EMSGSIZE;
15701da177e4SLinus Torvalds }
15711da177e4SLinus Torvalds 
15721da177e4SLinus Torvalds int ipmr_get_route(struct sk_buff *skb, struct rtmsg *rtm, int nowait)
15731da177e4SLinus Torvalds {
15741da177e4SLinus Torvalds 	int err;
15751da177e4SLinus Torvalds 	struct mfc_cache *cache;
15761da177e4SLinus Torvalds 	struct rtable *rt = (struct rtable*)skb->dst;
15771da177e4SLinus Torvalds 
15781da177e4SLinus Torvalds 	read_lock(&mrt_lock);
15791da177e4SLinus Torvalds 	cache = ipmr_cache_find(rt->rt_src, rt->rt_dst);
15801da177e4SLinus Torvalds 
15811da177e4SLinus Torvalds 	if (cache==NULL) {
158272287490SAlexey Kuznetsov 		struct sk_buff *skb2;
15831da177e4SLinus Torvalds 		struct net_device *dev;
15841da177e4SLinus Torvalds 		int vif;
15851da177e4SLinus Torvalds 
15861da177e4SLinus Torvalds 		if (nowait) {
15871da177e4SLinus Torvalds 			read_unlock(&mrt_lock);
15881da177e4SLinus Torvalds 			return -EAGAIN;
15891da177e4SLinus Torvalds 		}
15901da177e4SLinus Torvalds 
15911da177e4SLinus Torvalds 		dev = skb->dev;
15921da177e4SLinus Torvalds 		if (dev == NULL || (vif = ipmr_find_vif(dev)) < 0) {
15931da177e4SLinus Torvalds 			read_unlock(&mrt_lock);
15941da177e4SLinus Torvalds 			return -ENODEV;
15951da177e4SLinus Torvalds 		}
159672287490SAlexey Kuznetsov 		skb2 = skb_clone(skb, GFP_ATOMIC);
159772287490SAlexey Kuznetsov 		if (!skb2) {
159872287490SAlexey Kuznetsov 			read_unlock(&mrt_lock);
159972287490SAlexey Kuznetsov 			return -ENOMEM;
160072287490SAlexey Kuznetsov 		}
160172287490SAlexey Kuznetsov 
160272287490SAlexey Kuznetsov 		skb2->nh.raw = skb_push(skb2, sizeof(struct iphdr));
160372287490SAlexey Kuznetsov 		skb2->nh.iph->ihl = sizeof(struct iphdr)>>2;
160472287490SAlexey Kuznetsov 		skb2->nh.iph->saddr = rt->rt_src;
160572287490SAlexey Kuznetsov 		skb2->nh.iph->daddr = rt->rt_dst;
160672287490SAlexey Kuznetsov 		skb2->nh.iph->version = 0;
160772287490SAlexey Kuznetsov 		err = ipmr_cache_unresolved(vif, skb2);
16081da177e4SLinus Torvalds 		read_unlock(&mrt_lock);
16091da177e4SLinus Torvalds 		return err;
16101da177e4SLinus Torvalds 	}
16111da177e4SLinus Torvalds 
16121da177e4SLinus Torvalds 	if (!nowait && (rtm->rtm_flags&RTM_F_NOTIFY))
16131da177e4SLinus Torvalds 		cache->mfc_flags |= MFC_NOTIFY;
16141da177e4SLinus Torvalds 	err = ipmr_fill_mroute(skb, cache, rtm);
16151da177e4SLinus Torvalds 	read_unlock(&mrt_lock);
16161da177e4SLinus Torvalds 	return err;
16171da177e4SLinus Torvalds }
16181da177e4SLinus Torvalds 
16191da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
16201da177e4SLinus Torvalds /*
16211da177e4SLinus Torvalds  *	The /proc interfaces to multicast routing /proc/ip_mr_cache /proc/ip_mr_vif
16221da177e4SLinus Torvalds  */
16231da177e4SLinus Torvalds struct ipmr_vif_iter {
16241da177e4SLinus Torvalds 	int ct;
16251da177e4SLinus Torvalds };
16261da177e4SLinus Torvalds 
16271da177e4SLinus Torvalds static struct vif_device *ipmr_vif_seq_idx(struct ipmr_vif_iter *iter,
16281da177e4SLinus Torvalds 					   loff_t pos)
16291da177e4SLinus Torvalds {
16301da177e4SLinus Torvalds 	for (iter->ct = 0; iter->ct < maxvif; ++iter->ct) {
16311da177e4SLinus Torvalds 		if(!VIF_EXISTS(iter->ct))
16321da177e4SLinus Torvalds 			continue;
16331da177e4SLinus Torvalds 		if (pos-- == 0)
16341da177e4SLinus Torvalds 			return &vif_table[iter->ct];
16351da177e4SLinus Torvalds 	}
16361da177e4SLinus Torvalds 	return NULL;
16371da177e4SLinus Torvalds }
16381da177e4SLinus Torvalds 
16391da177e4SLinus Torvalds static void *ipmr_vif_seq_start(struct seq_file *seq, loff_t *pos)
16401da177e4SLinus Torvalds {
16411da177e4SLinus Torvalds 	read_lock(&mrt_lock);
16421da177e4SLinus Torvalds 	return *pos ? ipmr_vif_seq_idx(seq->private, *pos - 1)
16431da177e4SLinus Torvalds 		: SEQ_START_TOKEN;
16441da177e4SLinus Torvalds }
16451da177e4SLinus Torvalds 
16461da177e4SLinus Torvalds static void *ipmr_vif_seq_next(struct seq_file *seq, void *v, loff_t *pos)
16471da177e4SLinus Torvalds {
16481da177e4SLinus Torvalds 	struct ipmr_vif_iter *iter = seq->private;
16491da177e4SLinus Torvalds 
16501da177e4SLinus Torvalds 	++*pos;
16511da177e4SLinus Torvalds 	if (v == SEQ_START_TOKEN)
16521da177e4SLinus Torvalds 		return ipmr_vif_seq_idx(iter, 0);
16531da177e4SLinus Torvalds 
16541da177e4SLinus Torvalds 	while (++iter->ct < maxvif) {
16551da177e4SLinus Torvalds 		if(!VIF_EXISTS(iter->ct))
16561da177e4SLinus Torvalds 			continue;
16571da177e4SLinus Torvalds 		return &vif_table[iter->ct];
16581da177e4SLinus Torvalds 	}
16591da177e4SLinus Torvalds 	return NULL;
16601da177e4SLinus Torvalds }
16611da177e4SLinus Torvalds 
16621da177e4SLinus Torvalds static void ipmr_vif_seq_stop(struct seq_file *seq, void *v)
16631da177e4SLinus Torvalds {
16641da177e4SLinus Torvalds 	read_unlock(&mrt_lock);
16651da177e4SLinus Torvalds }
16661da177e4SLinus Torvalds 
16671da177e4SLinus Torvalds static int ipmr_vif_seq_show(struct seq_file *seq, void *v)
16681da177e4SLinus Torvalds {
16691da177e4SLinus Torvalds 	if (v == SEQ_START_TOKEN) {
16701da177e4SLinus Torvalds 		seq_puts(seq,
16711da177e4SLinus Torvalds 			 "Interface      BytesIn  PktsIn  BytesOut PktsOut Flags Local    Remote\n");
16721da177e4SLinus Torvalds 	} else {
16731da177e4SLinus Torvalds 		const struct vif_device *vif = v;
16741da177e4SLinus Torvalds 		const char *name =  vif->dev ? vif->dev->name : "none";
16751da177e4SLinus Torvalds 
16761da177e4SLinus Torvalds 		seq_printf(seq,
16771da177e4SLinus Torvalds 			   "%2Zd %-10s %8ld %7ld  %8ld %7ld %05X %08X %08X\n",
16781da177e4SLinus Torvalds 			   vif - vif_table,
16791da177e4SLinus Torvalds 			   name, vif->bytes_in, vif->pkt_in,
16801da177e4SLinus Torvalds 			   vif->bytes_out, vif->pkt_out,
16811da177e4SLinus Torvalds 			   vif->flags, vif->local, vif->remote);
16821da177e4SLinus Torvalds 	}
16831da177e4SLinus Torvalds 	return 0;
16841da177e4SLinus Torvalds }
16851da177e4SLinus Torvalds 
16861da177e4SLinus Torvalds static struct seq_operations ipmr_vif_seq_ops = {
16871da177e4SLinus Torvalds 	.start = ipmr_vif_seq_start,
16881da177e4SLinus Torvalds 	.next  = ipmr_vif_seq_next,
16891da177e4SLinus Torvalds 	.stop  = ipmr_vif_seq_stop,
16901da177e4SLinus Torvalds 	.show  = ipmr_vif_seq_show,
16911da177e4SLinus Torvalds };
16921da177e4SLinus Torvalds 
16931da177e4SLinus Torvalds static int ipmr_vif_open(struct inode *inode, struct file *file)
16941da177e4SLinus Torvalds {
16951da177e4SLinus Torvalds 	struct seq_file *seq;
16961da177e4SLinus Torvalds 	int rc = -ENOMEM;
16971da177e4SLinus Torvalds 	struct ipmr_vif_iter *s = kmalloc(sizeof(*s), GFP_KERNEL);
16981da177e4SLinus Torvalds 
16991da177e4SLinus Torvalds 	if (!s)
17001da177e4SLinus Torvalds 		goto out;
17011da177e4SLinus Torvalds 
17021da177e4SLinus Torvalds 	rc = seq_open(file, &ipmr_vif_seq_ops);
17031da177e4SLinus Torvalds 	if (rc)
17041da177e4SLinus Torvalds 		goto out_kfree;
17051da177e4SLinus Torvalds 
17061da177e4SLinus Torvalds 	s->ct = 0;
17071da177e4SLinus Torvalds 	seq = file->private_data;
17081da177e4SLinus Torvalds 	seq->private = s;
17091da177e4SLinus Torvalds out:
17101da177e4SLinus Torvalds 	return rc;
17111da177e4SLinus Torvalds out_kfree:
17121da177e4SLinus Torvalds 	kfree(s);
17131da177e4SLinus Torvalds 	goto out;
17141da177e4SLinus Torvalds 
17151da177e4SLinus Torvalds }
17161da177e4SLinus Torvalds 
17171da177e4SLinus Torvalds static struct file_operations ipmr_vif_fops = {
17181da177e4SLinus Torvalds 	.owner	 = THIS_MODULE,
17191da177e4SLinus Torvalds 	.open    = ipmr_vif_open,
17201da177e4SLinus Torvalds 	.read    = seq_read,
17211da177e4SLinus Torvalds 	.llseek  = seq_lseek,
17221da177e4SLinus Torvalds 	.release = seq_release_private,
17231da177e4SLinus Torvalds };
17241da177e4SLinus Torvalds 
17251da177e4SLinus Torvalds struct ipmr_mfc_iter {
17261da177e4SLinus Torvalds 	struct mfc_cache **cache;
17271da177e4SLinus Torvalds 	int ct;
17281da177e4SLinus Torvalds };
17291da177e4SLinus Torvalds 
17301da177e4SLinus Torvalds 
17311da177e4SLinus Torvalds static struct mfc_cache *ipmr_mfc_seq_idx(struct ipmr_mfc_iter *it, loff_t pos)
17321da177e4SLinus Torvalds {
17331da177e4SLinus Torvalds 	struct mfc_cache *mfc;
17341da177e4SLinus Torvalds 
17351da177e4SLinus Torvalds 	it->cache = mfc_cache_array;
17361da177e4SLinus Torvalds 	read_lock(&mrt_lock);
17371da177e4SLinus Torvalds 	for (it->ct = 0; it->ct < MFC_LINES; it->ct++)
17381da177e4SLinus Torvalds 		for(mfc = mfc_cache_array[it->ct]; mfc; mfc = mfc->next)
17391da177e4SLinus Torvalds 			if (pos-- == 0)
17401da177e4SLinus Torvalds 				return mfc;
17411da177e4SLinus Torvalds 	read_unlock(&mrt_lock);
17421da177e4SLinus Torvalds 
17431da177e4SLinus Torvalds 	it->cache = &mfc_unres_queue;
17441da177e4SLinus Torvalds 	spin_lock_bh(&mfc_unres_lock);
17451da177e4SLinus Torvalds 	for(mfc = mfc_unres_queue; mfc; mfc = mfc->next)
17461da177e4SLinus Torvalds 		if (pos-- == 0)
17471da177e4SLinus Torvalds 			return mfc;
17481da177e4SLinus Torvalds 	spin_unlock_bh(&mfc_unres_lock);
17491da177e4SLinus Torvalds 
17501da177e4SLinus Torvalds 	it->cache = NULL;
17511da177e4SLinus Torvalds 	return NULL;
17521da177e4SLinus Torvalds }
17531da177e4SLinus Torvalds 
17541da177e4SLinus Torvalds 
17551da177e4SLinus Torvalds static void *ipmr_mfc_seq_start(struct seq_file *seq, loff_t *pos)
17561da177e4SLinus Torvalds {
17571da177e4SLinus Torvalds 	struct ipmr_mfc_iter *it = seq->private;
17581da177e4SLinus Torvalds 	it->cache = NULL;
17591da177e4SLinus Torvalds 	it->ct = 0;
17601da177e4SLinus Torvalds 	return *pos ? ipmr_mfc_seq_idx(seq->private, *pos - 1)
17611da177e4SLinus Torvalds 		: SEQ_START_TOKEN;
17621da177e4SLinus Torvalds }
17631da177e4SLinus Torvalds 
17641da177e4SLinus Torvalds static void *ipmr_mfc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
17651da177e4SLinus Torvalds {
17661da177e4SLinus Torvalds 	struct mfc_cache *mfc = v;
17671da177e4SLinus Torvalds 	struct ipmr_mfc_iter *it = seq->private;
17681da177e4SLinus Torvalds 
17691da177e4SLinus Torvalds 	++*pos;
17701da177e4SLinus Torvalds 
17711da177e4SLinus Torvalds 	if (v == SEQ_START_TOKEN)
17721da177e4SLinus Torvalds 		return ipmr_mfc_seq_idx(seq->private, 0);
17731da177e4SLinus Torvalds 
17741da177e4SLinus Torvalds 	if (mfc->next)
17751da177e4SLinus Torvalds 		return mfc->next;
17761da177e4SLinus Torvalds 
17771da177e4SLinus Torvalds 	if (it->cache == &mfc_unres_queue)
17781da177e4SLinus Torvalds 		goto end_of_list;
17791da177e4SLinus Torvalds 
17801da177e4SLinus Torvalds 	BUG_ON(it->cache != mfc_cache_array);
17811da177e4SLinus Torvalds 
17821da177e4SLinus Torvalds 	while (++it->ct < MFC_LINES) {
17831da177e4SLinus Torvalds 		mfc = mfc_cache_array[it->ct];
17841da177e4SLinus Torvalds 		if (mfc)
17851da177e4SLinus Torvalds 			return mfc;
17861da177e4SLinus Torvalds 	}
17871da177e4SLinus Torvalds 
17881da177e4SLinus Torvalds 	/* exhausted cache_array, show unresolved */
17891da177e4SLinus Torvalds 	read_unlock(&mrt_lock);
17901da177e4SLinus Torvalds 	it->cache = &mfc_unres_queue;
17911da177e4SLinus Torvalds 	it->ct = 0;
17921da177e4SLinus Torvalds 
17931da177e4SLinus Torvalds 	spin_lock_bh(&mfc_unres_lock);
17941da177e4SLinus Torvalds 	mfc = mfc_unres_queue;
17951da177e4SLinus Torvalds 	if (mfc)
17961da177e4SLinus Torvalds 		return mfc;
17971da177e4SLinus Torvalds 
17981da177e4SLinus Torvalds  end_of_list:
17991da177e4SLinus Torvalds 	spin_unlock_bh(&mfc_unres_lock);
18001da177e4SLinus Torvalds 	it->cache = NULL;
18011da177e4SLinus Torvalds 
18021da177e4SLinus Torvalds 	return NULL;
18031da177e4SLinus Torvalds }
18041da177e4SLinus Torvalds 
18051da177e4SLinus Torvalds static void ipmr_mfc_seq_stop(struct seq_file *seq, void *v)
18061da177e4SLinus Torvalds {
18071da177e4SLinus Torvalds 	struct ipmr_mfc_iter *it = seq->private;
18081da177e4SLinus Torvalds 
18091da177e4SLinus Torvalds 	if (it->cache == &mfc_unres_queue)
18101da177e4SLinus Torvalds 		spin_unlock_bh(&mfc_unres_lock);
18111da177e4SLinus Torvalds 	else if (it->cache == mfc_cache_array)
18121da177e4SLinus Torvalds 		read_unlock(&mrt_lock);
18131da177e4SLinus Torvalds }
18141da177e4SLinus Torvalds 
18151da177e4SLinus Torvalds static int ipmr_mfc_seq_show(struct seq_file *seq, void *v)
18161da177e4SLinus Torvalds {
18171da177e4SLinus Torvalds 	int n;
18181da177e4SLinus Torvalds 
18191da177e4SLinus Torvalds 	if (v == SEQ_START_TOKEN) {
18201da177e4SLinus Torvalds 		seq_puts(seq,
18211da177e4SLinus Torvalds 		 "Group    Origin   Iif     Pkts    Bytes    Wrong Oifs\n");
18221da177e4SLinus Torvalds 	} else {
18231da177e4SLinus Torvalds 		const struct mfc_cache *mfc = v;
18241da177e4SLinus Torvalds 		const struct ipmr_mfc_iter *it = seq->private;
18251da177e4SLinus Torvalds 
18261da177e4SLinus Torvalds 		seq_printf(seq, "%08lX %08lX %-3d %8ld %8ld %8ld",
18271da177e4SLinus Torvalds 			   (unsigned long) mfc->mfc_mcastgrp,
18281da177e4SLinus Torvalds 			   (unsigned long) mfc->mfc_origin,
18291da177e4SLinus Torvalds 			   mfc->mfc_parent,
18301da177e4SLinus Torvalds 			   mfc->mfc_un.res.pkt,
18311da177e4SLinus Torvalds 			   mfc->mfc_un.res.bytes,
18321da177e4SLinus Torvalds 			   mfc->mfc_un.res.wrong_if);
18331da177e4SLinus Torvalds 
18341da177e4SLinus Torvalds 		if (it->cache != &mfc_unres_queue) {
18351da177e4SLinus Torvalds 			for(n = mfc->mfc_un.res.minvif;
18361da177e4SLinus Torvalds 			    n < mfc->mfc_un.res.maxvif; n++ ) {
18371da177e4SLinus Torvalds 				if(VIF_EXISTS(n)
18381da177e4SLinus Torvalds 				   && mfc->mfc_un.res.ttls[n] < 255)
18391da177e4SLinus Torvalds 				seq_printf(seq,
18401da177e4SLinus Torvalds 					   " %2d:%-3d",
18411da177e4SLinus Torvalds 					   n, mfc->mfc_un.res.ttls[n]);
18421da177e4SLinus Torvalds 			}
18431da177e4SLinus Torvalds 		}
18441da177e4SLinus Torvalds 		seq_putc(seq, '\n');
18451da177e4SLinus Torvalds 	}
18461da177e4SLinus Torvalds 	return 0;
18471da177e4SLinus Torvalds }
18481da177e4SLinus Torvalds 
18491da177e4SLinus Torvalds static struct seq_operations ipmr_mfc_seq_ops = {
18501da177e4SLinus Torvalds 	.start = ipmr_mfc_seq_start,
18511da177e4SLinus Torvalds 	.next  = ipmr_mfc_seq_next,
18521da177e4SLinus Torvalds 	.stop  = ipmr_mfc_seq_stop,
18531da177e4SLinus Torvalds 	.show  = ipmr_mfc_seq_show,
18541da177e4SLinus Torvalds };
18551da177e4SLinus Torvalds 
18561da177e4SLinus Torvalds static int ipmr_mfc_open(struct inode *inode, struct file *file)
18571da177e4SLinus Torvalds {
18581da177e4SLinus Torvalds 	struct seq_file *seq;
18591da177e4SLinus Torvalds 	int rc = -ENOMEM;
18601da177e4SLinus Torvalds 	struct ipmr_mfc_iter *s = kmalloc(sizeof(*s), GFP_KERNEL);
18611da177e4SLinus Torvalds 
18621da177e4SLinus Torvalds 	if (!s)
18631da177e4SLinus Torvalds 		goto out;
18641da177e4SLinus Torvalds 
18651da177e4SLinus Torvalds 	rc = seq_open(file, &ipmr_mfc_seq_ops);
18661da177e4SLinus Torvalds 	if (rc)
18671da177e4SLinus Torvalds 		goto out_kfree;
18681da177e4SLinus Torvalds 
18691da177e4SLinus Torvalds 	seq = file->private_data;
18701da177e4SLinus Torvalds 	seq->private = s;
18711da177e4SLinus Torvalds out:
18721da177e4SLinus Torvalds 	return rc;
18731da177e4SLinus Torvalds out_kfree:
18741da177e4SLinus Torvalds 	kfree(s);
18751da177e4SLinus Torvalds 	goto out;
18761da177e4SLinus Torvalds 
18771da177e4SLinus Torvalds }
18781da177e4SLinus Torvalds 
18791da177e4SLinus Torvalds static struct file_operations ipmr_mfc_fops = {
18801da177e4SLinus Torvalds 	.owner	 = THIS_MODULE,
18811da177e4SLinus Torvalds 	.open    = ipmr_mfc_open,
18821da177e4SLinus Torvalds 	.read    = seq_read,
18831da177e4SLinus Torvalds 	.llseek  = seq_lseek,
18841da177e4SLinus Torvalds 	.release = seq_release_private,
18851da177e4SLinus Torvalds };
18861da177e4SLinus Torvalds #endif
18871da177e4SLinus Torvalds 
18881da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM_V2
18891da177e4SLinus Torvalds static struct net_protocol pim_protocol = {
18901da177e4SLinus Torvalds 	.handler	=	pim_rcv,
18911da177e4SLinus Torvalds };
18921da177e4SLinus Torvalds #endif
18931da177e4SLinus Torvalds 
18941da177e4SLinus Torvalds 
18951da177e4SLinus Torvalds /*
18961da177e4SLinus Torvalds  *	Setup for IP multicast routing
18971da177e4SLinus Torvalds  */
18981da177e4SLinus Torvalds 
18991da177e4SLinus Torvalds void __init ip_mr_init(void)
19001da177e4SLinus Torvalds {
19011da177e4SLinus Torvalds 	mrt_cachep = kmem_cache_create("ip_mrt_cache",
19021da177e4SLinus Torvalds 				       sizeof(struct mfc_cache),
1903e5d679f3SAlexey Dobriyan 				       0, SLAB_HWCACHE_ALIGN|SLAB_PANIC,
19041da177e4SLinus Torvalds 				       NULL, NULL);
19051da177e4SLinus Torvalds 	init_timer(&ipmr_expire_timer);
19061da177e4SLinus Torvalds 	ipmr_expire_timer.function=ipmr_expire_process;
19071da177e4SLinus Torvalds 	register_netdevice_notifier(&ip_mr_notifier);
19081da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
19091da177e4SLinus Torvalds 	proc_net_fops_create("ip_mr_vif", 0, &ipmr_vif_fops);
19101da177e4SLinus Torvalds 	proc_net_fops_create("ip_mr_cache", 0, &ipmr_mfc_fops);
19111da177e4SLinus Torvalds #endif
19121da177e4SLinus Torvalds }
1913