| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | *	IP multicast routing support for mrouted 3.6/3.8 | 
|  | 3 | * | 
| Alan Cox | 113aa83 | 2008-10-13 19:01:08 -0700 | [diff] [blame] | 4 | *		(c) 1995 Alan Cox, <alan@lxorguk.ukuu.org.uk> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | *	  Linux Consultancy and Custom Driver Development | 
|  | 6 | * | 
|  | 7 | *	This program is free software; you can redistribute it and/or | 
|  | 8 | *	modify it under the terms of the GNU General Public License | 
|  | 9 | *	as published by the Free Software Foundation; either version | 
|  | 10 | *	2 of the License, or (at your option) any later version. | 
|  | 11 | * | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | *	Fixes: | 
|  | 13 | *	Michael Chastain	:	Incorrect size of copying. | 
|  | 14 | *	Alan Cox		:	Added the cache manager code | 
|  | 15 | *	Alan Cox		:	Fixed the clone/copy bug and device race. | 
|  | 16 | *	Mike McLagan		:	Routing by source | 
|  | 17 | *	Malcolm Beattie		:	Buffer handling fixes. | 
|  | 18 | *	Alexey Kuznetsov	:	Double buffer free and other fixes. | 
|  | 19 | *	SVR Anand		:	Fixed several multicast bugs and problems. | 
|  | 20 | *	Alexey Kuznetsov	:	Status, optimisations and more. | 
|  | 21 | *	Brad Parker		:	Better behaviour on mrouted upcall | 
|  | 22 | *					overflow. | 
|  | 23 | *      Carlos Picoto           :       PIMv1 Support | 
|  | 24 | *	Pavlin Ivanov Radoslavov:	PIMv2 Registers must checksum only PIM header | 
|  | 25 | *					Relax this requrement to work with older peers. | 
|  | 26 | * | 
|  | 27 | */ | 
|  | 28 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include <asm/system.h> | 
|  | 30 | #include <asm/uaccess.h> | 
|  | 31 | #include <linux/types.h> | 
| Randy Dunlap | 4fc268d | 2006-01-11 12:17:47 -0800 | [diff] [blame] | 32 | #include <linux/capability.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | #include <linux/errno.h> | 
|  | 34 | #include <linux/timer.h> | 
|  | 35 | #include <linux/mm.h> | 
|  | 36 | #include <linux/kernel.h> | 
|  | 37 | #include <linux/fcntl.h> | 
|  | 38 | #include <linux/stat.h> | 
|  | 39 | #include <linux/socket.h> | 
|  | 40 | #include <linux/in.h> | 
|  | 41 | #include <linux/inet.h> | 
|  | 42 | #include <linux/netdevice.h> | 
|  | 43 | #include <linux/inetdevice.h> | 
|  | 44 | #include <linux/igmp.h> | 
|  | 45 | #include <linux/proc_fs.h> | 
|  | 46 | #include <linux/seq_file.h> | 
|  | 47 | #include <linux/mroute.h> | 
|  | 48 | #include <linux/init.h> | 
| Kris Katterjohn | 46f25df | 2006-01-05 16:35:42 -0800 | [diff] [blame] | 49 | #include <linux/if_ether.h> | 
| Eric W. Biederman | 457c4cb | 2007-09-12 12:01:34 +0200 | [diff] [blame] | 50 | #include <net/net_namespace.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | #include <net/ip.h> | 
|  | 52 | #include <net/protocol.h> | 
|  | 53 | #include <linux/skbuff.h> | 
| Arnaldo Carvalho de Melo | 14c8502 | 2005-12-27 02:43:12 -0200 | [diff] [blame] | 54 | #include <net/route.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | #include <net/sock.h> | 
|  | 56 | #include <net/icmp.h> | 
|  | 57 | #include <net/udp.h> | 
|  | 58 | #include <net/raw.h> | 
|  | 59 | #include <linux/notifier.h> | 
|  | 60 | #include <linux/if_arp.h> | 
|  | 61 | #include <linux/netfilter_ipv4.h> | 
|  | 62 | #include <net/ipip.h> | 
|  | 63 | #include <net/checksum.h> | 
| Arnaldo Carvalho de Melo | dc5fc57 | 2007-03-25 23:06:12 -0700 | [diff] [blame] | 64 | #include <net/netlink.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 |  | 
|  | 66 | #if defined(CONFIG_IP_PIMSM_V1) || defined(CONFIG_IP_PIMSM_V2) | 
|  | 67 | #define CONFIG_IP_PIMSM	1 | 
|  | 68 | #endif | 
|  | 69 |  | 
|  | 70 | static struct sock *mroute_socket; | 
|  | 71 |  | 
|  | 72 |  | 
|  | 73 | /* Big lock, protecting vif table, mrt cache and mroute socket state. | 
|  | 74 | Note that the changes are semaphored via rtnl_lock. | 
|  | 75 | */ | 
|  | 76 |  | 
|  | 77 | static DEFINE_RWLOCK(mrt_lock); | 
|  | 78 |  | 
|  | 79 | /* | 
|  | 80 | *	Multicast router control variables | 
|  | 81 | */ | 
|  | 82 |  | 
|  | 83 | static struct vif_device vif_table[MAXVIFS];		/* Devices 		*/ | 
|  | 84 | static int maxvif; | 
|  | 85 |  | 
|  | 86 | #define VIF_EXISTS(idx) (vif_table[idx].dev != NULL) | 
|  | 87 |  | 
|  | 88 | static int mroute_do_assert;				/* Set in PIM assert	*/ | 
|  | 89 | static int mroute_do_pim; | 
|  | 90 |  | 
|  | 91 | static struct mfc_cache *mfc_cache_array[MFC_LINES];	/* Forwarding cache	*/ | 
|  | 92 |  | 
|  | 93 | static struct mfc_cache *mfc_unres_queue;		/* Queue of unresolved entries */ | 
|  | 94 | static atomic_t cache_resolve_queue_len;		/* Size of unresolved	*/ | 
|  | 95 |  | 
|  | 96 | /* Special spinlock for queue of unresolved entries */ | 
|  | 97 | static DEFINE_SPINLOCK(mfc_unres_lock); | 
|  | 98 |  | 
|  | 99 | /* We return to original Alan's scheme. Hash table of resolved | 
|  | 100 | entries is changed only in process context and protected | 
|  | 101 | with weak lock mrt_lock. Queue of unresolved entries is protected | 
|  | 102 | with strong spinlock mfc_unres_lock. | 
|  | 103 |  | 
|  | 104 | In this case data path is free of exclusive locks at all. | 
|  | 105 | */ | 
|  | 106 |  | 
| Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 107 | static struct kmem_cache *mrt_cachep __read_mostly; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 |  | 
|  | 109 | static int ip_mr_forward(struct sk_buff *skb, struct mfc_cache *cache, int local); | 
|  | 110 | static int ipmr_cache_report(struct sk_buff *pkt, vifi_t vifi, int assert); | 
|  | 111 | static int ipmr_fill_mroute(struct sk_buff *skb, struct mfc_cache *c, struct rtmsg *rtm); | 
|  | 112 |  | 
|  | 113 | #ifdef CONFIG_IP_PIMSM_V2 | 
|  | 114 | static struct net_protocol pim_protocol; | 
|  | 115 | #endif | 
|  | 116 |  | 
|  | 117 | static struct timer_list ipmr_expire_timer; | 
|  | 118 |  | 
|  | 119 | /* Service routines creating virtual interfaces: DVMRP tunnels and PIMREG */ | 
|  | 120 |  | 
| Wang Chen | d607032 | 2008-07-14 20:55:26 -0700 | [diff] [blame] | 121 | static void ipmr_del_tunnel(struct net_device *dev, struct vifctl *v) | 
|  | 122 | { | 
|  | 123 | dev_close(dev); | 
|  | 124 |  | 
|  | 125 | dev = __dev_get_by_name(&init_net, "tunl0"); | 
|  | 126 | if (dev) { | 
| Stephen Hemminger | 5bc3eb7 | 2008-11-19 21:52:05 -0800 | [diff] [blame] | 127 | const struct net_device_ops *ops = dev->netdev_ops; | 
| Wang Chen | d607032 | 2008-07-14 20:55:26 -0700 | [diff] [blame] | 128 | struct ifreq ifr; | 
| Wang Chen | d607032 | 2008-07-14 20:55:26 -0700 | [diff] [blame] | 129 | struct ip_tunnel_parm p; | 
|  | 130 |  | 
|  | 131 | memset(&p, 0, sizeof(p)); | 
|  | 132 | p.iph.daddr = v->vifc_rmt_addr.s_addr; | 
|  | 133 | p.iph.saddr = v->vifc_lcl_addr.s_addr; | 
|  | 134 | p.iph.version = 4; | 
|  | 135 | p.iph.ihl = 5; | 
|  | 136 | p.iph.protocol = IPPROTO_IPIP; | 
|  | 137 | sprintf(p.name, "dvmrp%d", v->vifc_vifi); | 
|  | 138 | ifr.ifr_ifru.ifru_data = (__force void __user *)&p; | 
|  | 139 |  | 
| Stephen Hemminger | 5bc3eb7 | 2008-11-19 21:52:05 -0800 | [diff] [blame] | 140 | if (ops->ndo_do_ioctl) { | 
|  | 141 | mm_segment_t oldfs = get_fs(); | 
|  | 142 |  | 
|  | 143 | set_fs(KERNEL_DS); | 
|  | 144 | ops->ndo_do_ioctl(dev, &ifr, SIOCDELTUNNEL); | 
|  | 145 | set_fs(oldfs); | 
|  | 146 | } | 
| Wang Chen | d607032 | 2008-07-14 20:55:26 -0700 | [diff] [blame] | 147 | } | 
|  | 148 | } | 
|  | 149 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | static | 
|  | 151 | struct net_device *ipmr_new_tunnel(struct vifctl *v) | 
|  | 152 | { | 
|  | 153 | struct net_device  *dev; | 
|  | 154 |  | 
| Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 155 | dev = __dev_get_by_name(&init_net, "tunl0"); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 |  | 
|  | 157 | if (dev) { | 
| Stephen Hemminger | 5bc3eb7 | 2008-11-19 21:52:05 -0800 | [diff] [blame] | 158 | const struct net_device_ops *ops = dev->netdev_ops; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | int err; | 
|  | 160 | struct ifreq ifr; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | struct ip_tunnel_parm p; | 
|  | 162 | struct in_device  *in_dev; | 
|  | 163 |  | 
|  | 164 | memset(&p, 0, sizeof(p)); | 
|  | 165 | p.iph.daddr = v->vifc_rmt_addr.s_addr; | 
|  | 166 | p.iph.saddr = v->vifc_lcl_addr.s_addr; | 
|  | 167 | p.iph.version = 4; | 
|  | 168 | p.iph.ihl = 5; | 
|  | 169 | p.iph.protocol = IPPROTO_IPIP; | 
|  | 170 | sprintf(p.name, "dvmrp%d", v->vifc_vifi); | 
| Stephen Hemminger | ba93ef7 | 2008-01-21 17:28:59 -0800 | [diff] [blame] | 171 | ifr.ifr_ifru.ifru_data = (__force void __user *)&p; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 |  | 
| Stephen Hemminger | 5bc3eb7 | 2008-11-19 21:52:05 -0800 | [diff] [blame] | 173 | if (ops->ndo_do_ioctl) { | 
|  | 174 | mm_segment_t oldfs = get_fs(); | 
|  | 175 |  | 
|  | 176 | set_fs(KERNEL_DS); | 
|  | 177 | err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL); | 
|  | 178 | set_fs(oldfs); | 
|  | 179 | } else | 
|  | 180 | err = -EOPNOTSUPP; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 |  | 
|  | 182 | dev = NULL; | 
|  | 183 |  | 
| Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 184 | if (err == 0 && (dev = __dev_get_by_name(&init_net, p.name)) != NULL) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | dev->flags |= IFF_MULTICAST; | 
|  | 186 |  | 
| Herbert Xu | e5ed639 | 2005-10-03 14:35:55 -0700 | [diff] [blame] | 187 | in_dev = __in_dev_get_rtnl(dev); | 
| Herbert Xu | 71e27da | 2007-06-04 23:36:06 -0700 | [diff] [blame] | 188 | if (in_dev == NULL) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | goto failure; | 
| Herbert Xu | 71e27da | 2007-06-04 23:36:06 -0700 | [diff] [blame] | 190 |  | 
|  | 191 | ipv4_devconf_setall(in_dev); | 
|  | 192 | IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 |  | 
|  | 194 | if (dev_open(dev)) | 
|  | 195 | goto failure; | 
| Wang Chen | 7dc00c8 | 2008-07-14 20:56:34 -0700 | [diff] [blame] | 196 | dev_hold(dev); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | } | 
|  | 198 | } | 
|  | 199 | return dev; | 
|  | 200 |  | 
|  | 201 | failure: | 
|  | 202 | /* allow the register to be completed before unregistering. */ | 
|  | 203 | rtnl_unlock(); | 
|  | 204 | rtnl_lock(); | 
|  | 205 |  | 
|  | 206 | unregister_netdevice(dev); | 
|  | 207 | return NULL; | 
|  | 208 | } | 
|  | 209 |  | 
|  | 210 | #ifdef CONFIG_IP_PIMSM | 
|  | 211 |  | 
|  | 212 | static int reg_vif_num = -1; | 
|  | 213 |  | 
|  | 214 | static int reg_vif_xmit(struct sk_buff *skb, struct net_device *dev) | 
|  | 215 | { | 
|  | 216 | read_lock(&mrt_lock); | 
| Pavel Emelyanov | cf3677a | 2008-05-21 14:17:33 -0700 | [diff] [blame] | 217 | dev->stats.tx_bytes += skb->len; | 
|  | 218 | dev->stats.tx_packets++; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | ipmr_cache_report(skb, reg_vif_num, IGMPMSG_WHOLEPKT); | 
|  | 220 | read_unlock(&mrt_lock); | 
|  | 221 | kfree_skb(skb); | 
|  | 222 | return 0; | 
|  | 223 | } | 
|  | 224 |  | 
| Stephen Hemminger | 007c383 | 2008-11-20 20:28:35 -0800 | [diff] [blame] | 225 | static const struct net_device_ops reg_vif_netdev_ops = { | 
|  | 226 | .ndo_start_xmit	= reg_vif_xmit, | 
|  | 227 | }; | 
|  | 228 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | static void reg_vif_setup(struct net_device *dev) | 
|  | 230 | { | 
|  | 231 | dev->type		= ARPHRD_PIMREG; | 
| Kris Katterjohn | 46f25df | 2006-01-05 16:35:42 -0800 | [diff] [blame] | 232 | dev->mtu		= ETH_DATA_LEN - sizeof(struct iphdr) - 8; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | dev->flags		= IFF_NOARP; | 
| Stephen Hemminger | 007c383 | 2008-11-20 20:28:35 -0800 | [diff] [blame] | 234 | dev->netdev_ops		= ®_vif_netdev_ops, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | dev->destructor		= free_netdev; | 
|  | 236 | } | 
|  | 237 |  | 
|  | 238 | static struct net_device *ipmr_reg_vif(void) | 
|  | 239 | { | 
|  | 240 | struct net_device *dev; | 
|  | 241 | struct in_device *in_dev; | 
|  | 242 |  | 
| Pavel Emelyanov | cf3677a | 2008-05-21 14:17:33 -0700 | [diff] [blame] | 243 | dev = alloc_netdev(0, "pimreg", reg_vif_setup); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 |  | 
|  | 245 | if (dev == NULL) | 
|  | 246 | return NULL; | 
|  | 247 |  | 
|  | 248 | if (register_netdevice(dev)) { | 
|  | 249 | free_netdev(dev); | 
|  | 250 | return NULL; | 
|  | 251 | } | 
|  | 252 | dev->iflink = 0; | 
|  | 253 |  | 
| Herbert Xu | 71e27da | 2007-06-04 23:36:06 -0700 | [diff] [blame] | 254 | rcu_read_lock(); | 
|  | 255 | if ((in_dev = __in_dev_get_rcu(dev)) == NULL) { | 
|  | 256 | rcu_read_unlock(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | goto failure; | 
| Herbert Xu | 71e27da | 2007-06-04 23:36:06 -0700 | [diff] [blame] | 258 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 |  | 
| Herbert Xu | 71e27da | 2007-06-04 23:36:06 -0700 | [diff] [blame] | 260 | ipv4_devconf_setall(in_dev); | 
|  | 261 | IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0; | 
|  | 262 | rcu_read_unlock(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 |  | 
|  | 264 | if (dev_open(dev)) | 
|  | 265 | goto failure; | 
|  | 266 |  | 
| Wang Chen | 7dc00c8 | 2008-07-14 20:56:34 -0700 | [diff] [blame] | 267 | dev_hold(dev); | 
|  | 268 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | return dev; | 
|  | 270 |  | 
|  | 271 | failure: | 
|  | 272 | /* allow the register to be completed before unregistering. */ | 
|  | 273 | rtnl_unlock(); | 
|  | 274 | rtnl_lock(); | 
|  | 275 |  | 
|  | 276 | unregister_netdevice(dev); | 
|  | 277 | return NULL; | 
|  | 278 | } | 
|  | 279 | #endif | 
|  | 280 |  | 
|  | 281 | /* | 
|  | 282 | *	Delete a VIF entry | 
| Wang Chen | 7dc00c8 | 2008-07-14 20:56:34 -0700 | [diff] [blame] | 283 | *	@notify: Set to 1, if the caller is a notifier_call | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | */ | 
| YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 285 |  | 
| Wang Chen | 7dc00c8 | 2008-07-14 20:56:34 -0700 | [diff] [blame] | 286 | static int vif_delete(int vifi, int notify) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | { | 
|  | 288 | struct vif_device *v; | 
|  | 289 | struct net_device *dev; | 
|  | 290 | struct in_device *in_dev; | 
|  | 291 |  | 
|  | 292 | if (vifi < 0 || vifi >= maxvif) | 
|  | 293 | return -EADDRNOTAVAIL; | 
|  | 294 |  | 
|  | 295 | v = &vif_table[vifi]; | 
|  | 296 |  | 
|  | 297 | write_lock_bh(&mrt_lock); | 
|  | 298 | dev = v->dev; | 
|  | 299 | v->dev = NULL; | 
|  | 300 |  | 
|  | 301 | if (!dev) { | 
|  | 302 | write_unlock_bh(&mrt_lock); | 
|  | 303 | return -EADDRNOTAVAIL; | 
|  | 304 | } | 
|  | 305 |  | 
|  | 306 | #ifdef CONFIG_IP_PIMSM | 
|  | 307 | if (vifi == reg_vif_num) | 
|  | 308 | reg_vif_num = -1; | 
|  | 309 | #endif | 
|  | 310 |  | 
|  | 311 | if (vifi+1 == maxvif) { | 
|  | 312 | int tmp; | 
|  | 313 | for (tmp=vifi-1; tmp>=0; tmp--) { | 
|  | 314 | if (VIF_EXISTS(tmp)) | 
|  | 315 | break; | 
|  | 316 | } | 
|  | 317 | maxvif = tmp+1; | 
|  | 318 | } | 
|  | 319 |  | 
|  | 320 | write_unlock_bh(&mrt_lock); | 
|  | 321 |  | 
|  | 322 | dev_set_allmulti(dev, -1); | 
|  | 323 |  | 
| Herbert Xu | e5ed639 | 2005-10-03 14:35:55 -0700 | [diff] [blame] | 324 | if ((in_dev = __in_dev_get_rtnl(dev)) != NULL) { | 
| Herbert Xu | 42f811b | 2007-06-04 23:34:44 -0700 | [diff] [blame] | 325 | IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)--; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | ip_rt_multicast_event(in_dev); | 
|  | 327 | } | 
|  | 328 |  | 
| Wang Chen | 7dc00c8 | 2008-07-14 20:56:34 -0700 | [diff] [blame] | 329 | if (v->flags&(VIFF_TUNNEL|VIFF_REGISTER) && !notify) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | unregister_netdevice(dev); | 
|  | 331 |  | 
|  | 332 | dev_put(dev); | 
|  | 333 | return 0; | 
|  | 334 | } | 
|  | 335 |  | 
|  | 336 | /* Destroy an unresolved cache entry, killing queued skbs | 
|  | 337 | and reporting error to netlink readers. | 
|  | 338 | */ | 
|  | 339 |  | 
|  | 340 | static void ipmr_destroy_unres(struct mfc_cache *c) | 
|  | 341 | { | 
|  | 342 | struct sk_buff *skb; | 
| Patrick McHardy | 9ef1d4c | 2005-06-28 12:55:30 -0700 | [diff] [blame] | 343 | struct nlmsgerr *e; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 |  | 
|  | 345 | atomic_dec(&cache_resolve_queue_len); | 
|  | 346 |  | 
| Jianjun Kong | c354e12 | 2008-11-03 00:28:02 -0800 | [diff] [blame] | 347 | while ((skb = skb_dequeue(&c->mfc_un.unres.unresolved))) { | 
| Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 348 | if (ip_hdr(skb)->version == 0) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr)); | 
|  | 350 | nlh->nlmsg_type = NLMSG_ERROR; | 
|  | 351 | nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct nlmsgerr)); | 
|  | 352 | skb_trim(skb, nlh->nlmsg_len); | 
| Patrick McHardy | 9ef1d4c | 2005-06-28 12:55:30 -0700 | [diff] [blame] | 353 | e = NLMSG_DATA(nlh); | 
|  | 354 | e->error = -ETIMEDOUT; | 
|  | 355 | memset(&e->msg, 0, sizeof(e->msg)); | 
| Thomas Graf | 2942e90 | 2006-08-15 00:30:25 -0700 | [diff] [blame] | 356 |  | 
| Denis V. Lunev | 97c53ca | 2007-11-19 22:26:51 -0800 | [diff] [blame] | 357 | rtnl_unicast(skb, &init_net, NETLINK_CB(skb).pid); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | } else | 
|  | 359 | kfree_skb(skb); | 
|  | 360 | } | 
|  | 361 |  | 
|  | 362 | kmem_cache_free(mrt_cachep, c); | 
|  | 363 | } | 
|  | 364 |  | 
|  | 365 |  | 
|  | 366 | /* Single timer process for all the unresolved queue. */ | 
|  | 367 |  | 
|  | 368 | static void ipmr_expire_process(unsigned long dummy) | 
|  | 369 | { | 
|  | 370 | unsigned long now; | 
|  | 371 | unsigned long expires; | 
|  | 372 | struct mfc_cache *c, **cp; | 
|  | 373 |  | 
|  | 374 | if (!spin_trylock(&mfc_unres_lock)) { | 
|  | 375 | mod_timer(&ipmr_expire_timer, jiffies+HZ/10); | 
|  | 376 | return; | 
|  | 377 | } | 
|  | 378 |  | 
|  | 379 | if (atomic_read(&cache_resolve_queue_len) == 0) | 
|  | 380 | goto out; | 
|  | 381 |  | 
|  | 382 | now = jiffies; | 
|  | 383 | expires = 10*HZ; | 
|  | 384 | cp = &mfc_unres_queue; | 
|  | 385 |  | 
|  | 386 | while ((c=*cp) != NULL) { | 
|  | 387 | if (time_after(c->mfc_un.unres.expires, now)) { | 
|  | 388 | unsigned long interval = c->mfc_un.unres.expires - now; | 
|  | 389 | if (interval < expires) | 
|  | 390 | expires = interval; | 
|  | 391 | cp = &c->next; | 
|  | 392 | continue; | 
|  | 393 | } | 
|  | 394 |  | 
|  | 395 | *cp = c->next; | 
|  | 396 |  | 
|  | 397 | ipmr_destroy_unres(c); | 
|  | 398 | } | 
|  | 399 |  | 
|  | 400 | if (atomic_read(&cache_resolve_queue_len)) | 
|  | 401 | mod_timer(&ipmr_expire_timer, jiffies + expires); | 
|  | 402 |  | 
|  | 403 | out: | 
|  | 404 | spin_unlock(&mfc_unres_lock); | 
|  | 405 | } | 
|  | 406 |  | 
|  | 407 | /* Fill oifs list. It is called under write locked mrt_lock. */ | 
|  | 408 |  | 
| Baruch Even | d1b04c0 | 2005-07-30 17:41:59 -0700 | [diff] [blame] | 409 | static void ipmr_update_thresholds(struct mfc_cache *cache, unsigned char *ttls) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | { | 
|  | 411 | int vifi; | 
|  | 412 |  | 
|  | 413 | cache->mfc_un.res.minvif = MAXVIFS; | 
|  | 414 | cache->mfc_un.res.maxvif = 0; | 
|  | 415 | memset(cache->mfc_un.res.ttls, 255, MAXVIFS); | 
|  | 416 |  | 
|  | 417 | for (vifi=0; vifi<maxvif; vifi++) { | 
|  | 418 | if (VIF_EXISTS(vifi) && ttls[vifi] && ttls[vifi] < 255) { | 
|  | 419 | cache->mfc_un.res.ttls[vifi] = ttls[vifi]; | 
|  | 420 | if (cache->mfc_un.res.minvif > vifi) | 
|  | 421 | cache->mfc_un.res.minvif = vifi; | 
|  | 422 | if (cache->mfc_un.res.maxvif <= vifi) | 
|  | 423 | cache->mfc_un.res.maxvif = vifi + 1; | 
|  | 424 | } | 
|  | 425 | } | 
|  | 426 | } | 
|  | 427 |  | 
|  | 428 | static int vif_add(struct vifctl *vifc, int mrtsock) | 
|  | 429 | { | 
|  | 430 | int vifi = vifc->vifc_vifi; | 
|  | 431 | struct vif_device *v = &vif_table[vifi]; | 
|  | 432 | struct net_device *dev; | 
|  | 433 | struct in_device *in_dev; | 
| Wang Chen | d607032 | 2008-07-14 20:55:26 -0700 | [diff] [blame] | 434 | int err; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 |  | 
|  | 436 | /* Is vif busy ? */ | 
|  | 437 | if (VIF_EXISTS(vifi)) | 
|  | 438 | return -EADDRINUSE; | 
|  | 439 |  | 
|  | 440 | switch (vifc->vifc_flags) { | 
|  | 441 | #ifdef CONFIG_IP_PIMSM | 
|  | 442 | case VIFF_REGISTER: | 
|  | 443 | /* | 
|  | 444 | * Special Purpose VIF in PIM | 
|  | 445 | * All the packets will be sent to the daemon | 
|  | 446 | */ | 
|  | 447 | if (reg_vif_num >= 0) | 
|  | 448 | return -EADDRINUSE; | 
|  | 449 | dev = ipmr_reg_vif(); | 
|  | 450 | if (!dev) | 
|  | 451 | return -ENOBUFS; | 
| Wang Chen | d607032 | 2008-07-14 20:55:26 -0700 | [diff] [blame] | 452 | err = dev_set_allmulti(dev, 1); | 
|  | 453 | if (err) { | 
|  | 454 | unregister_netdevice(dev); | 
| Wang Chen | 7dc00c8 | 2008-07-14 20:56:34 -0700 | [diff] [blame] | 455 | dev_put(dev); | 
| Wang Chen | d607032 | 2008-07-14 20:55:26 -0700 | [diff] [blame] | 456 | return err; | 
|  | 457 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | break; | 
|  | 459 | #endif | 
| YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 460 | case VIFF_TUNNEL: | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | dev = ipmr_new_tunnel(vifc); | 
|  | 462 | if (!dev) | 
|  | 463 | return -ENOBUFS; | 
| Wang Chen | d607032 | 2008-07-14 20:55:26 -0700 | [diff] [blame] | 464 | err = dev_set_allmulti(dev, 1); | 
|  | 465 | if (err) { | 
|  | 466 | ipmr_del_tunnel(dev, vifc); | 
| Wang Chen | 7dc00c8 | 2008-07-14 20:56:34 -0700 | [diff] [blame] | 467 | dev_put(dev); | 
| Wang Chen | d607032 | 2008-07-14 20:55:26 -0700 | [diff] [blame] | 468 | return err; | 
|  | 469 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | break; | 
|  | 471 | case 0: | 
| Denis V. Lunev | 1ab3527 | 2008-01-22 22:04:30 -0800 | [diff] [blame] | 472 | dev = ip_dev_find(&init_net, vifc->vifc_lcl_addr.s_addr); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | if (!dev) | 
|  | 474 | return -EADDRNOTAVAIL; | 
| Wang Chen | d607032 | 2008-07-14 20:55:26 -0700 | [diff] [blame] | 475 | err = dev_set_allmulti(dev, 1); | 
| Wang Chen | 7dc00c8 | 2008-07-14 20:56:34 -0700 | [diff] [blame] | 476 | if (err) { | 
|  | 477 | dev_put(dev); | 
| Wang Chen | d607032 | 2008-07-14 20:55:26 -0700 | [diff] [blame] | 478 | return err; | 
| Wang Chen | 7dc00c8 | 2008-07-14 20:56:34 -0700 | [diff] [blame] | 479 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | break; | 
|  | 481 | default: | 
|  | 482 | return -EINVAL; | 
|  | 483 | } | 
|  | 484 |  | 
| Herbert Xu | e5ed639 | 2005-10-03 14:35:55 -0700 | [diff] [blame] | 485 | if ((in_dev = __in_dev_get_rtnl(dev)) == NULL) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 486 | return -EADDRNOTAVAIL; | 
| Herbert Xu | 42f811b | 2007-06-04 23:34:44 -0700 | [diff] [blame] | 487 | IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)++; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | ip_rt_multicast_event(in_dev); | 
|  | 489 |  | 
|  | 490 | /* | 
|  | 491 | *	Fill in the VIF structures | 
|  | 492 | */ | 
| Jianjun Kong | c354e12 | 2008-11-03 00:28:02 -0800 | [diff] [blame] | 493 | v->rate_limit = vifc->vifc_rate_limit; | 
|  | 494 | v->local = vifc->vifc_lcl_addr.s_addr; | 
|  | 495 | v->remote = vifc->vifc_rmt_addr.s_addr; | 
|  | 496 | v->flags = vifc->vifc_flags; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | if (!mrtsock) | 
|  | 498 | v->flags |= VIFF_STATIC; | 
| Jianjun Kong | c354e12 | 2008-11-03 00:28:02 -0800 | [diff] [blame] | 499 | v->threshold = vifc->vifc_threshold; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | v->bytes_in = 0; | 
|  | 501 | v->bytes_out = 0; | 
|  | 502 | v->pkt_in = 0; | 
|  | 503 | v->pkt_out = 0; | 
|  | 504 | v->link = dev->ifindex; | 
|  | 505 | if (v->flags&(VIFF_TUNNEL|VIFF_REGISTER)) | 
|  | 506 | v->link = dev->iflink; | 
|  | 507 |  | 
|  | 508 | /* And finish update writing critical data */ | 
|  | 509 | write_lock_bh(&mrt_lock); | 
| Jianjun Kong | c354e12 | 2008-11-03 00:28:02 -0800 | [diff] [blame] | 510 | v->dev = dev; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | #ifdef CONFIG_IP_PIMSM | 
|  | 512 | if (v->flags&VIFF_REGISTER) | 
|  | 513 | reg_vif_num = vifi; | 
|  | 514 | #endif | 
|  | 515 | if (vifi+1 > maxvif) | 
|  | 516 | maxvif = vifi+1; | 
|  | 517 | write_unlock_bh(&mrt_lock); | 
|  | 518 | return 0; | 
|  | 519 | } | 
|  | 520 |  | 
| Al Viro | 114c784 | 2006-09-27 18:39:29 -0700 | [diff] [blame] | 521 | static struct mfc_cache *ipmr_cache_find(__be32 origin, __be32 mcastgrp) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | { | 
| Jianjun Kong | c354e12 | 2008-11-03 00:28:02 -0800 | [diff] [blame] | 523 | int line = MFC_HASH(mcastgrp, origin); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | struct mfc_cache *c; | 
|  | 525 |  | 
|  | 526 | for (c=mfc_cache_array[line]; c; c = c->next) { | 
|  | 527 | if (c->mfc_origin==origin && c->mfc_mcastgrp==mcastgrp) | 
|  | 528 | break; | 
|  | 529 | } | 
|  | 530 | return c; | 
|  | 531 | } | 
|  | 532 |  | 
|  | 533 | /* | 
|  | 534 | *	Allocate a multicast cache entry | 
|  | 535 | */ | 
|  | 536 | static struct mfc_cache *ipmr_cache_alloc(void) | 
|  | 537 | { | 
| Jianjun Kong | c354e12 | 2008-11-03 00:28:02 -0800 | [diff] [blame] | 538 | struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_KERNEL); | 
|  | 539 | if (c == NULL) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | return NULL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | c->mfc_un.res.minvif = MAXVIFS; | 
|  | 542 | return c; | 
|  | 543 | } | 
|  | 544 |  | 
|  | 545 | static struct mfc_cache *ipmr_cache_alloc_unres(void) | 
|  | 546 | { | 
| Jianjun Kong | c354e12 | 2008-11-03 00:28:02 -0800 | [diff] [blame] | 547 | struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_ATOMIC); | 
|  | 548 | if (c == NULL) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | return NULL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | skb_queue_head_init(&c->mfc_un.unres.unresolved); | 
|  | 551 | c->mfc_un.unres.expires = jiffies + 10*HZ; | 
|  | 552 | return c; | 
|  | 553 | } | 
|  | 554 |  | 
|  | 555 | /* | 
|  | 556 | *	A cache entry has gone into a resolved state from queued | 
|  | 557 | */ | 
| YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 558 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 559 | static void ipmr_cache_resolve(struct mfc_cache *uc, struct mfc_cache *c) | 
|  | 560 | { | 
|  | 561 | struct sk_buff *skb; | 
| Patrick McHardy | 9ef1d4c | 2005-06-28 12:55:30 -0700 | [diff] [blame] | 562 | struct nlmsgerr *e; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 |  | 
|  | 564 | /* | 
|  | 565 | *	Play the pending entries through our router | 
|  | 566 | */ | 
|  | 567 |  | 
| Jianjun Kong | c354e12 | 2008-11-03 00:28:02 -0800 | [diff] [blame] | 568 | while ((skb = __skb_dequeue(&uc->mfc_un.unres.unresolved))) { | 
| Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 569 | if (ip_hdr(skb)->version == 0) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 570 | struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr)); | 
|  | 571 |  | 
|  | 572 | if (ipmr_fill_mroute(skb, c, NLMSG_DATA(nlh)) > 0) { | 
| Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 573 | nlh->nlmsg_len = (skb_tail_pointer(skb) - | 
|  | 574 | (u8 *)nlh); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 575 | } else { | 
|  | 576 | nlh->nlmsg_type = NLMSG_ERROR; | 
|  | 577 | nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct nlmsgerr)); | 
|  | 578 | skb_trim(skb, nlh->nlmsg_len); | 
| Patrick McHardy | 9ef1d4c | 2005-06-28 12:55:30 -0700 | [diff] [blame] | 579 | e = NLMSG_DATA(nlh); | 
|  | 580 | e->error = -EMSGSIZE; | 
|  | 581 | memset(&e->msg, 0, sizeof(e->msg)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 582 | } | 
| Thomas Graf | 2942e90 | 2006-08-15 00:30:25 -0700 | [diff] [blame] | 583 |  | 
| Denis V. Lunev | 97c53ca | 2007-11-19 22:26:51 -0800 | [diff] [blame] | 584 | rtnl_unicast(skb, &init_net, NETLINK_CB(skb).pid); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | } else | 
|  | 586 | ip_mr_forward(skb, c, 0); | 
|  | 587 | } | 
|  | 588 | } | 
|  | 589 |  | 
|  | 590 | /* | 
|  | 591 | *	Bounce a cache query up to mrouted. We could use netlink for this but mrouted | 
|  | 592 | *	expects the following bizarre scheme. | 
|  | 593 | * | 
|  | 594 | *	Called under mrt_lock. | 
|  | 595 | */ | 
| YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 596 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 597 | static int ipmr_cache_report(struct sk_buff *pkt, vifi_t vifi, int assert) | 
|  | 598 | { | 
|  | 599 | struct sk_buff *skb; | 
| Arnaldo Carvalho de Melo | c9bdd4b | 2007-03-12 20:09:15 -0300 | [diff] [blame] | 600 | const int ihl = ip_hdrlen(pkt); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | struct igmphdr *igmp; | 
|  | 602 | struct igmpmsg *msg; | 
|  | 603 | int ret; | 
|  | 604 |  | 
|  | 605 | #ifdef CONFIG_IP_PIMSM | 
|  | 606 | if (assert == IGMPMSG_WHOLEPKT) | 
|  | 607 | skb = skb_realloc_headroom(pkt, sizeof(struct iphdr)); | 
|  | 608 | else | 
|  | 609 | #endif | 
|  | 610 | skb = alloc_skb(128, GFP_ATOMIC); | 
|  | 611 |  | 
| Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 612 | if (!skb) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 613 | return -ENOBUFS; | 
|  | 614 |  | 
|  | 615 | #ifdef CONFIG_IP_PIMSM | 
|  | 616 | if (assert == IGMPMSG_WHOLEPKT) { | 
|  | 617 | /* Ugly, but we have no choice with this interface. | 
|  | 618 | Duplicate old header, fix ihl, length etc. | 
|  | 619 | And all this only to mangle msg->im_msgtype and | 
|  | 620 | to set msg->im_mbz to "mbz" :-) | 
|  | 621 | */ | 
| Arnaldo Carvalho de Melo | 878c814 | 2007-03-11 22:38:29 -0300 | [diff] [blame] | 622 | skb_push(skb, sizeof(struct iphdr)); | 
|  | 623 | skb_reset_network_header(skb); | 
| Arnaldo Carvalho de Melo | badff6d | 2007-03-13 13:06:52 -0300 | [diff] [blame] | 624 | skb_reset_transport_header(skb); | 
| Arnaldo Carvalho de Melo | 0272ffc | 2007-03-12 20:05:39 -0300 | [diff] [blame] | 625 | msg = (struct igmpmsg *)skb_network_header(skb); | 
| Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 626 | memcpy(msg, skb_network_header(pkt), sizeof(struct iphdr)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | msg->im_msgtype = IGMPMSG_WHOLEPKT; | 
|  | 628 | msg->im_mbz = 0; | 
| YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 629 | msg->im_vif = reg_vif_num; | 
| Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 630 | ip_hdr(skb)->ihl = sizeof(struct iphdr) >> 2; | 
|  | 631 | ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(pkt)->tot_len) + | 
|  | 632 | sizeof(struct iphdr)); | 
| YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 633 | } else | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 634 | #endif | 
| YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 635 | { | 
|  | 636 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 637 | /* | 
|  | 638 | *	Copy the IP header | 
|  | 639 | */ | 
|  | 640 |  | 
| Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 641 | skb->network_header = skb->tail; | 
| Arnaldo Carvalho de Melo | ddc7b8e | 2007-03-15 21:42:27 -0300 | [diff] [blame] | 642 | skb_put(skb, ihl); | 
| Arnaldo Carvalho de Melo | 27d7ff4 | 2007-03-31 11:55:19 -0300 | [diff] [blame] | 643 | skb_copy_to_linear_data(skb, pkt->data, ihl); | 
| Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 644 | ip_hdr(skb)->protocol = 0;			/* Flag to the kernel this is a route add */ | 
|  | 645 | msg = (struct igmpmsg *)skb_network_header(skb); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 646 | msg->im_vif = vifi; | 
|  | 647 | skb->dst = dst_clone(pkt->dst); | 
|  | 648 |  | 
|  | 649 | /* | 
|  | 650 | *	Add our header | 
|  | 651 | */ | 
|  | 652 |  | 
| Jianjun Kong | c354e12 | 2008-11-03 00:28:02 -0800 | [diff] [blame] | 653 | igmp=(struct igmphdr *)skb_put(skb, sizeof(struct igmphdr)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 654 | igmp->type	= | 
|  | 655 | msg->im_msgtype = assert; | 
|  | 656 | igmp->code 	=	0; | 
| Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 657 | ip_hdr(skb)->tot_len = htons(skb->len);			/* Fix the length */ | 
| Arnaldo Carvalho de Melo | b0e380b | 2007-04-10 21:21:55 -0700 | [diff] [blame] | 658 | skb->transport_header = skb->network_header; | 
| YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 659 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 660 |  | 
|  | 661 | if (mroute_socket == NULL) { | 
|  | 662 | kfree_skb(skb); | 
|  | 663 | return -EINVAL; | 
|  | 664 | } | 
|  | 665 |  | 
|  | 666 | /* | 
|  | 667 | *	Deliver to mrouted | 
|  | 668 | */ | 
| Jianjun Kong | c354e12 | 2008-11-03 00:28:02 -0800 | [diff] [blame] | 669 | if ((ret = sock_queue_rcv_skb(mroute_socket, skb))<0) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 670 | if (net_ratelimit()) | 
|  | 671 | printk(KERN_WARNING "mroute: pending queue full, dropping entries.\n"); | 
|  | 672 | kfree_skb(skb); | 
|  | 673 | } | 
|  | 674 |  | 
|  | 675 | return ret; | 
|  | 676 | } | 
|  | 677 |  | 
|  | 678 | /* | 
|  | 679 | *	Queue a packet for resolution. It gets locked cache entry! | 
|  | 680 | */ | 
| YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 681 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 682 | static int | 
|  | 683 | ipmr_cache_unresolved(vifi_t vifi, struct sk_buff *skb) | 
|  | 684 | { | 
|  | 685 | int err; | 
|  | 686 | struct mfc_cache *c; | 
| Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 687 | const struct iphdr *iph = ip_hdr(skb); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 688 |  | 
|  | 689 | spin_lock_bh(&mfc_unres_lock); | 
|  | 690 | for (c=mfc_unres_queue; c; c=c->next) { | 
| Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 691 | if (c->mfc_mcastgrp == iph->daddr && | 
|  | 692 | c->mfc_origin == iph->saddr) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | break; | 
|  | 694 | } | 
|  | 695 |  | 
|  | 696 | if (c == NULL) { | 
|  | 697 | /* | 
|  | 698 | *	Create a new entry if allowable | 
|  | 699 | */ | 
|  | 700 |  | 
| Jianjun Kong | c354e12 | 2008-11-03 00:28:02 -0800 | [diff] [blame] | 701 | if (atomic_read(&cache_resolve_queue_len) >= 10 || | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 702 | (c=ipmr_cache_alloc_unres())==NULL) { | 
|  | 703 | spin_unlock_bh(&mfc_unres_lock); | 
|  | 704 |  | 
|  | 705 | kfree_skb(skb); | 
|  | 706 | return -ENOBUFS; | 
|  | 707 | } | 
|  | 708 |  | 
|  | 709 | /* | 
|  | 710 | *	Fill in the new cache entry | 
|  | 711 | */ | 
| Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 712 | c->mfc_parent	= -1; | 
|  | 713 | c->mfc_origin	= iph->saddr; | 
|  | 714 | c->mfc_mcastgrp	= iph->daddr; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 715 |  | 
|  | 716 | /* | 
|  | 717 | *	Reflect first query at mrouted. | 
|  | 718 | */ | 
|  | 719 | if ((err = ipmr_cache_report(skb, vifi, IGMPMSG_NOCACHE))<0) { | 
| YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 720 | /* If the report failed throw the cache entry | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 721 | out - Brad Parker | 
|  | 722 | */ | 
|  | 723 | spin_unlock_bh(&mfc_unres_lock); | 
|  | 724 |  | 
|  | 725 | kmem_cache_free(mrt_cachep, c); | 
|  | 726 | kfree_skb(skb); | 
|  | 727 | return err; | 
|  | 728 | } | 
|  | 729 |  | 
|  | 730 | atomic_inc(&cache_resolve_queue_len); | 
|  | 731 | c->next = mfc_unres_queue; | 
|  | 732 | mfc_unres_queue = c; | 
|  | 733 |  | 
|  | 734 | mod_timer(&ipmr_expire_timer, c->mfc_un.unres.expires); | 
|  | 735 | } | 
|  | 736 |  | 
|  | 737 | /* | 
|  | 738 | *	See if we can append the packet | 
|  | 739 | */ | 
|  | 740 | if (c->mfc_un.unres.unresolved.qlen>3) { | 
|  | 741 | kfree_skb(skb); | 
|  | 742 | err = -ENOBUFS; | 
|  | 743 | } else { | 
| Jianjun Kong | c354e12 | 2008-11-03 00:28:02 -0800 | [diff] [blame] | 744 | skb_queue_tail(&c->mfc_un.unres.unresolved, skb); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 745 | err = 0; | 
|  | 746 | } | 
|  | 747 |  | 
|  | 748 | spin_unlock_bh(&mfc_unres_lock); | 
|  | 749 | return err; | 
|  | 750 | } | 
|  | 751 |  | 
|  | 752 | /* | 
|  | 753 | *	MFC cache manipulation by user space mroute daemon | 
|  | 754 | */ | 
|  | 755 |  | 
|  | 756 | static int ipmr_mfc_delete(struct mfcctl *mfc) | 
|  | 757 | { | 
|  | 758 | int line; | 
|  | 759 | struct mfc_cache *c, **cp; | 
|  | 760 |  | 
| Jianjun Kong | c354e12 | 2008-11-03 00:28:02 -0800 | [diff] [blame] | 761 | line = MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 |  | 
|  | 763 | for (cp=&mfc_cache_array[line]; (c=*cp) != NULL; cp = &c->next) { | 
|  | 764 | if (c->mfc_origin == mfc->mfcc_origin.s_addr && | 
|  | 765 | c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr) { | 
|  | 766 | write_lock_bh(&mrt_lock); | 
|  | 767 | *cp = c->next; | 
|  | 768 | write_unlock_bh(&mrt_lock); | 
|  | 769 |  | 
|  | 770 | kmem_cache_free(mrt_cachep, c); | 
|  | 771 | return 0; | 
|  | 772 | } | 
|  | 773 | } | 
|  | 774 | return -ENOENT; | 
|  | 775 | } | 
|  | 776 |  | 
|  | 777 | static int ipmr_mfc_add(struct mfcctl *mfc, int mrtsock) | 
|  | 778 | { | 
|  | 779 | int line; | 
|  | 780 | struct mfc_cache *uc, *c, **cp; | 
|  | 781 |  | 
| Jianjun Kong | c354e12 | 2008-11-03 00:28:02 -0800 | [diff] [blame] | 782 | line = MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 783 |  | 
|  | 784 | for (cp=&mfc_cache_array[line]; (c=*cp) != NULL; cp = &c->next) { | 
|  | 785 | if (c->mfc_origin == mfc->mfcc_origin.s_addr && | 
|  | 786 | c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr) | 
|  | 787 | break; | 
|  | 788 | } | 
|  | 789 |  | 
|  | 790 | if (c != NULL) { | 
|  | 791 | write_lock_bh(&mrt_lock); | 
|  | 792 | c->mfc_parent = mfc->mfcc_parent; | 
| Baruch Even | d1b04c0 | 2005-07-30 17:41:59 -0700 | [diff] [blame] | 793 | ipmr_update_thresholds(c, mfc->mfcc_ttls); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 794 | if (!mrtsock) | 
|  | 795 | c->mfc_flags |= MFC_STATIC; | 
|  | 796 | write_unlock_bh(&mrt_lock); | 
|  | 797 | return 0; | 
|  | 798 | } | 
|  | 799 |  | 
| Joe Perches | f97c1e0 | 2007-12-16 13:45:43 -0800 | [diff] [blame] | 800 | if (!ipv4_is_multicast(mfc->mfcc_mcastgrp.s_addr)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 801 | return -EINVAL; | 
|  | 802 |  | 
| Jianjun Kong | c354e12 | 2008-11-03 00:28:02 -0800 | [diff] [blame] | 803 | c = ipmr_cache_alloc(); | 
|  | 804 | if (c == NULL) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 805 | return -ENOMEM; | 
|  | 806 |  | 
| Jianjun Kong | c354e12 | 2008-11-03 00:28:02 -0800 | [diff] [blame] | 807 | c->mfc_origin = mfc->mfcc_origin.s_addr; | 
|  | 808 | c->mfc_mcastgrp = mfc->mfcc_mcastgrp.s_addr; | 
|  | 809 | c->mfc_parent = mfc->mfcc_parent; | 
| Baruch Even | d1b04c0 | 2005-07-30 17:41:59 -0700 | [diff] [blame] | 810 | ipmr_update_thresholds(c, mfc->mfcc_ttls); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 811 | if (!mrtsock) | 
|  | 812 | c->mfc_flags |= MFC_STATIC; | 
|  | 813 |  | 
|  | 814 | write_lock_bh(&mrt_lock); | 
|  | 815 | c->next = mfc_cache_array[line]; | 
|  | 816 | mfc_cache_array[line] = c; | 
|  | 817 | write_unlock_bh(&mrt_lock); | 
|  | 818 |  | 
|  | 819 | /* | 
|  | 820 | *	Check to see if we resolved a queued list. If so we | 
|  | 821 | *	need to send on the frames and tidy up. | 
|  | 822 | */ | 
|  | 823 | spin_lock_bh(&mfc_unres_lock); | 
|  | 824 | for (cp = &mfc_unres_queue; (uc=*cp) != NULL; | 
|  | 825 | cp = &uc->next) { | 
|  | 826 | if (uc->mfc_origin == c->mfc_origin && | 
|  | 827 | uc->mfc_mcastgrp == c->mfc_mcastgrp) { | 
|  | 828 | *cp = uc->next; | 
|  | 829 | if (atomic_dec_and_test(&cache_resolve_queue_len)) | 
|  | 830 | del_timer(&ipmr_expire_timer); | 
|  | 831 | break; | 
|  | 832 | } | 
|  | 833 | } | 
|  | 834 | spin_unlock_bh(&mfc_unres_lock); | 
|  | 835 |  | 
|  | 836 | if (uc) { | 
|  | 837 | ipmr_cache_resolve(uc, c); | 
|  | 838 | kmem_cache_free(mrt_cachep, uc); | 
|  | 839 | } | 
|  | 840 | return 0; | 
|  | 841 | } | 
|  | 842 |  | 
|  | 843 | /* | 
|  | 844 | *	Close the multicast socket, and clear the vif tables etc | 
|  | 845 | */ | 
| YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 846 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 847 | static void mroute_clean_tables(struct sock *sk) | 
|  | 848 | { | 
|  | 849 | int i; | 
| YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 850 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 851 | /* | 
|  | 852 | *	Shut down all active vif entries | 
|  | 853 | */ | 
| Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 854 | for (i=0; i<maxvif; i++) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 855 | if (!(vif_table[i].flags&VIFF_STATIC)) | 
| Wang Chen | 7dc00c8 | 2008-07-14 20:56:34 -0700 | [diff] [blame] | 856 | vif_delete(i, 0); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 857 | } | 
|  | 858 |  | 
|  | 859 | /* | 
|  | 860 | *	Wipe the cache | 
|  | 861 | */ | 
| Jianjun Kong | c354e12 | 2008-11-03 00:28:02 -0800 | [diff] [blame] | 862 | for (i=0; i<MFC_LINES; i++) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 863 | struct mfc_cache *c, **cp; | 
|  | 864 |  | 
|  | 865 | cp = &mfc_cache_array[i]; | 
|  | 866 | while ((c = *cp) != NULL) { | 
|  | 867 | if (c->mfc_flags&MFC_STATIC) { | 
|  | 868 | cp = &c->next; | 
|  | 869 | continue; | 
|  | 870 | } | 
|  | 871 | write_lock_bh(&mrt_lock); | 
|  | 872 | *cp = c->next; | 
|  | 873 | write_unlock_bh(&mrt_lock); | 
|  | 874 |  | 
|  | 875 | kmem_cache_free(mrt_cachep, c); | 
|  | 876 | } | 
|  | 877 | } | 
|  | 878 |  | 
|  | 879 | if (atomic_read(&cache_resolve_queue_len) != 0) { | 
|  | 880 | struct mfc_cache *c; | 
|  | 881 |  | 
|  | 882 | spin_lock_bh(&mfc_unres_lock); | 
|  | 883 | while (mfc_unres_queue != NULL) { | 
|  | 884 | c = mfc_unres_queue; | 
|  | 885 | mfc_unres_queue = c->next; | 
|  | 886 | spin_unlock_bh(&mfc_unres_lock); | 
|  | 887 |  | 
|  | 888 | ipmr_destroy_unres(c); | 
|  | 889 |  | 
|  | 890 | spin_lock_bh(&mfc_unres_lock); | 
|  | 891 | } | 
|  | 892 | spin_unlock_bh(&mfc_unres_lock); | 
|  | 893 | } | 
|  | 894 | } | 
|  | 895 |  | 
|  | 896 | static void mrtsock_destruct(struct sock *sk) | 
|  | 897 | { | 
|  | 898 | rtnl_lock(); | 
|  | 899 | if (sk == mroute_socket) { | 
| YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 900 | IPV4_DEVCONF_ALL(sock_net(sk), MC_FORWARDING)--; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 901 |  | 
|  | 902 | write_lock_bh(&mrt_lock); | 
| Jianjun Kong | c354e12 | 2008-11-03 00:28:02 -0800 | [diff] [blame] | 903 | mroute_socket = NULL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 904 | write_unlock_bh(&mrt_lock); | 
|  | 905 |  | 
|  | 906 | mroute_clean_tables(sk); | 
|  | 907 | } | 
|  | 908 | rtnl_unlock(); | 
|  | 909 | } | 
|  | 910 |  | 
|  | 911 | /* | 
|  | 912 | *	Socket options and virtual interface manipulation. The whole | 
|  | 913 | *	virtual interface system is a complete heap, but unfortunately | 
|  | 914 | *	that's how BSD mrouted happens to think. Maybe one day with a proper | 
|  | 915 | *	MOSPF/PIM router set up we can clean this up. | 
|  | 916 | */ | 
| YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 917 |  | 
| Jianjun Kong | c354e12 | 2008-11-03 00:28:02 -0800 | [diff] [blame] | 918 | int ip_mroute_setsockopt(struct sock *sk, int optname, char __user *optval, int optlen) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 919 | { | 
|  | 920 | int ret; | 
|  | 921 | struct vifctl vif; | 
|  | 922 | struct mfcctl mfc; | 
| YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 923 |  | 
| Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 924 | if (optname != MRT_INIT) { | 
|  | 925 | if (sk != mroute_socket && !capable(CAP_NET_ADMIN)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 926 | return -EACCES; | 
|  | 927 | } | 
|  | 928 |  | 
| Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 929 | switch (optname) { | 
|  | 930 | case MRT_INIT: | 
|  | 931 | if (sk->sk_type != SOCK_RAW || | 
|  | 932 | inet_sk(sk)->num != IPPROTO_IGMP) | 
|  | 933 | return -EOPNOTSUPP; | 
| Jianjun Kong | c354e12 | 2008-11-03 00:28:02 -0800 | [diff] [blame] | 934 | if (optlen != sizeof(int)) | 
| Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 935 | return -ENOPROTOOPT; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 936 |  | 
| Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 937 | rtnl_lock(); | 
|  | 938 | if (mroute_socket) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 939 | rtnl_unlock(); | 
| Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 940 | return -EADDRINUSE; | 
|  | 941 | } | 
|  | 942 |  | 
|  | 943 | ret = ip_ra_control(sk, 1, mrtsock_destruct); | 
|  | 944 | if (ret == 0) { | 
|  | 945 | write_lock_bh(&mrt_lock); | 
| Jianjun Kong | c354e12 | 2008-11-03 00:28:02 -0800 | [diff] [blame] | 946 | mroute_socket = sk; | 
| Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 947 | write_unlock_bh(&mrt_lock); | 
|  | 948 |  | 
| YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 949 | IPV4_DEVCONF_ALL(sock_net(sk), MC_FORWARDING)++; | 
| Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 950 | } | 
|  | 951 | rtnl_unlock(); | 
|  | 952 | return ret; | 
|  | 953 | case MRT_DONE: | 
| Jianjun Kong | c354e12 | 2008-11-03 00:28:02 -0800 | [diff] [blame] | 954 | if (sk != mroute_socket) | 
| Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 955 | return -EACCES; | 
|  | 956 | return ip_ra_control(sk, 0, NULL); | 
|  | 957 | case MRT_ADD_VIF: | 
|  | 958 | case MRT_DEL_VIF: | 
| Jianjun Kong | c354e12 | 2008-11-03 00:28:02 -0800 | [diff] [blame] | 959 | if (optlen != sizeof(vif)) | 
| Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 960 | return -EINVAL; | 
| Jianjun Kong | c354e12 | 2008-11-03 00:28:02 -0800 | [diff] [blame] | 961 | if (copy_from_user(&vif, optval, sizeof(vif))) | 
| Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 962 | return -EFAULT; | 
|  | 963 | if (vif.vifc_vifi >= MAXVIFS) | 
|  | 964 | return -ENFILE; | 
|  | 965 | rtnl_lock(); | 
| Jianjun Kong | c354e12 | 2008-11-03 00:28:02 -0800 | [diff] [blame] | 966 | if (optname == MRT_ADD_VIF) { | 
| Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 967 | ret = vif_add(&vif, sk==mroute_socket); | 
|  | 968 | } else { | 
| Wang Chen | 7dc00c8 | 2008-07-14 20:56:34 -0700 | [diff] [blame] | 969 | ret = vif_delete(vif.vifc_vifi, 0); | 
| Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 970 | } | 
|  | 971 | rtnl_unlock(); | 
|  | 972 | return ret; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 973 |  | 
|  | 974 | /* | 
|  | 975 | *	Manipulate the forwarding caches. These live | 
|  | 976 | *	in a sort of kernel/user symbiosis. | 
|  | 977 | */ | 
| Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 978 | case MRT_ADD_MFC: | 
|  | 979 | case MRT_DEL_MFC: | 
| Jianjun Kong | c354e12 | 2008-11-03 00:28:02 -0800 | [diff] [blame] | 980 | if (optlen != sizeof(mfc)) | 
| Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 981 | return -EINVAL; | 
| Jianjun Kong | c354e12 | 2008-11-03 00:28:02 -0800 | [diff] [blame] | 982 | if (copy_from_user(&mfc, optval, sizeof(mfc))) | 
| Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 983 | return -EFAULT; | 
|  | 984 | rtnl_lock(); | 
| Jianjun Kong | c354e12 | 2008-11-03 00:28:02 -0800 | [diff] [blame] | 985 | if (optname == MRT_DEL_MFC) | 
| Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 986 | ret = ipmr_mfc_delete(&mfc); | 
|  | 987 | else | 
|  | 988 | ret = ipmr_mfc_add(&mfc, sk==mroute_socket); | 
|  | 989 | rtnl_unlock(); | 
|  | 990 | return ret; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 991 | /* | 
|  | 992 | *	Control PIM assert. | 
|  | 993 | */ | 
| Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 994 | case MRT_ASSERT: | 
|  | 995 | { | 
|  | 996 | int v; | 
|  | 997 | if (get_user(v,(int __user *)optval)) | 
|  | 998 | return -EFAULT; | 
|  | 999 | mroute_do_assert=(v)?1:0; | 
|  | 1000 | return 0; | 
|  | 1001 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1002 | #ifdef CONFIG_IP_PIMSM | 
| Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1003 | case MRT_PIM: | 
|  | 1004 | { | 
| Stephen Hemminger | ba93ef7 | 2008-01-21 17:28:59 -0800 | [diff] [blame] | 1005 | int v; | 
|  | 1006 |  | 
| Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1007 | if (get_user(v,(int __user *)optval)) | 
|  | 1008 | return -EFAULT; | 
| Stephen Hemminger | ba93ef7 | 2008-01-21 17:28:59 -0800 | [diff] [blame] | 1009 | v = (v) ? 1 : 0; | 
|  | 1010 |  | 
| Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1011 | rtnl_lock(); | 
|  | 1012 | ret = 0; | 
|  | 1013 | if (v != mroute_do_pim) { | 
|  | 1014 | mroute_do_pim = v; | 
|  | 1015 | mroute_do_assert = v; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1016 | #ifdef CONFIG_IP_PIMSM_V2 | 
| Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1017 | if (mroute_do_pim) | 
|  | 1018 | ret = inet_add_protocol(&pim_protocol, | 
|  | 1019 | IPPROTO_PIM); | 
|  | 1020 | else | 
|  | 1021 | ret = inet_del_protocol(&pim_protocol, | 
|  | 1022 | IPPROTO_PIM); | 
|  | 1023 | if (ret < 0) | 
|  | 1024 | ret = -EAGAIN; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1025 | #endif | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1026 | } | 
| Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1027 | rtnl_unlock(); | 
|  | 1028 | return ret; | 
|  | 1029 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1030 | #endif | 
| Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1031 | /* | 
|  | 1032 | *	Spurious command, or MRT_VERSION which you cannot | 
|  | 1033 | *	set. | 
|  | 1034 | */ | 
|  | 1035 | default: | 
|  | 1036 | return -ENOPROTOOPT; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1037 | } | 
|  | 1038 | } | 
|  | 1039 |  | 
|  | 1040 | /* | 
|  | 1041 | *	Getsock opt support for the multicast routing system. | 
|  | 1042 | */ | 
| YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1043 |  | 
| Jianjun Kong | c354e12 | 2008-11-03 00:28:02 -0800 | [diff] [blame] | 1044 | int ip_mroute_getsockopt(struct sock *sk, int optname, char __user *optval, int __user *optlen) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1045 | { | 
|  | 1046 | int olr; | 
|  | 1047 | int val; | 
|  | 1048 |  | 
| Jianjun Kong | c354e12 | 2008-11-03 00:28:02 -0800 | [diff] [blame] | 1049 | if (optname != MRT_VERSION && | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1050 | #ifdef CONFIG_IP_PIMSM | 
|  | 1051 | optname!=MRT_PIM && | 
|  | 1052 | #endif | 
|  | 1053 | optname!=MRT_ASSERT) | 
|  | 1054 | return -ENOPROTOOPT; | 
|  | 1055 |  | 
|  | 1056 | if (get_user(olr, optlen)) | 
|  | 1057 | return -EFAULT; | 
|  | 1058 |  | 
|  | 1059 | olr = min_t(unsigned int, olr, sizeof(int)); | 
|  | 1060 | if (olr < 0) | 
|  | 1061 | return -EINVAL; | 
| YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1062 |  | 
| Jianjun Kong | c354e12 | 2008-11-03 00:28:02 -0800 | [diff] [blame] | 1063 | if (put_user(olr, optlen)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1064 | return -EFAULT; | 
| Jianjun Kong | c354e12 | 2008-11-03 00:28:02 -0800 | [diff] [blame] | 1065 | if (optname == MRT_VERSION) | 
|  | 1066 | val = 0x0305; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1067 | #ifdef CONFIG_IP_PIMSM | 
| Jianjun Kong | c354e12 | 2008-11-03 00:28:02 -0800 | [diff] [blame] | 1068 | else if (optname == MRT_PIM) | 
|  | 1069 | val = mroute_do_pim; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1070 | #endif | 
|  | 1071 | else | 
| Jianjun Kong | c354e12 | 2008-11-03 00:28:02 -0800 | [diff] [blame] | 1072 | val = mroute_do_assert; | 
|  | 1073 | if (copy_to_user(optval, &val, olr)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1074 | return -EFAULT; | 
|  | 1075 | return 0; | 
|  | 1076 | } | 
|  | 1077 |  | 
|  | 1078 | /* | 
|  | 1079 | *	The IP multicast ioctl support routines. | 
|  | 1080 | */ | 
| YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1081 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1082 | int ipmr_ioctl(struct sock *sk, int cmd, void __user *arg) | 
|  | 1083 | { | 
|  | 1084 | struct sioc_sg_req sr; | 
|  | 1085 | struct sioc_vif_req vr; | 
|  | 1086 | struct vif_device *vif; | 
|  | 1087 | struct mfc_cache *c; | 
| YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1088 |  | 
| Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1089 | switch (cmd) { | 
|  | 1090 | case SIOCGETVIFCNT: | 
| Jianjun Kong | c354e12 | 2008-11-03 00:28:02 -0800 | [diff] [blame] | 1091 | if (copy_from_user(&vr, arg, sizeof(vr))) | 
| Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1092 | return -EFAULT; | 
| Jianjun Kong | c354e12 | 2008-11-03 00:28:02 -0800 | [diff] [blame] | 1093 | if (vr.vifi >= maxvif) | 
| Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1094 | return -EINVAL; | 
|  | 1095 | read_lock(&mrt_lock); | 
|  | 1096 | vif=&vif_table[vr.vifi]; | 
|  | 1097 | if (VIF_EXISTS(vr.vifi))	{ | 
| Jianjun Kong | c354e12 | 2008-11-03 00:28:02 -0800 | [diff] [blame] | 1098 | vr.icount = vif->pkt_in; | 
|  | 1099 | vr.ocount = vif->pkt_out; | 
|  | 1100 | vr.ibytes = vif->bytes_in; | 
|  | 1101 | vr.obytes = vif->bytes_out; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1102 | read_unlock(&mrt_lock); | 
| Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1103 |  | 
| Jianjun Kong | c354e12 | 2008-11-03 00:28:02 -0800 | [diff] [blame] | 1104 | if (copy_to_user(arg, &vr, sizeof(vr))) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1105 | return -EFAULT; | 
| Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1106 | return 0; | 
|  | 1107 | } | 
|  | 1108 | read_unlock(&mrt_lock); | 
|  | 1109 | return -EADDRNOTAVAIL; | 
|  | 1110 | case SIOCGETSGCNT: | 
| Jianjun Kong | c354e12 | 2008-11-03 00:28:02 -0800 | [diff] [blame] | 1111 | if (copy_from_user(&sr, arg, sizeof(sr))) | 
| Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1112 | return -EFAULT; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1113 |  | 
| Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1114 | read_lock(&mrt_lock); | 
|  | 1115 | c = ipmr_cache_find(sr.src.s_addr, sr.grp.s_addr); | 
|  | 1116 | if (c) { | 
|  | 1117 | sr.pktcnt = c->mfc_un.res.pkt; | 
|  | 1118 | sr.bytecnt = c->mfc_un.res.bytes; | 
|  | 1119 | sr.wrong_if = c->mfc_un.res.wrong_if; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1120 | read_unlock(&mrt_lock); | 
| Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1121 |  | 
| Jianjun Kong | c354e12 | 2008-11-03 00:28:02 -0800 | [diff] [blame] | 1122 | if (copy_to_user(arg, &sr, sizeof(sr))) | 
| Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1123 | return -EFAULT; | 
|  | 1124 | return 0; | 
|  | 1125 | } | 
|  | 1126 | read_unlock(&mrt_lock); | 
|  | 1127 | return -EADDRNOTAVAIL; | 
|  | 1128 | default: | 
|  | 1129 | return -ENOIOCTLCMD; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1130 | } | 
|  | 1131 | } | 
|  | 1132 |  | 
|  | 1133 |  | 
|  | 1134 | static int ipmr_device_event(struct notifier_block *this, unsigned long event, void *ptr) | 
|  | 1135 | { | 
| Eric W. Biederman | e9dc865 | 2007-09-12 13:02:17 +0200 | [diff] [blame] | 1136 | struct net_device *dev = ptr; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1137 | struct vif_device *v; | 
|  | 1138 | int ct; | 
| Eric W. Biederman | e9dc865 | 2007-09-12 13:02:17 +0200 | [diff] [blame] | 1139 |  | 
| YOSHIFUJI Hideaki | 721499e | 2008-07-19 22:34:43 -0700 | [diff] [blame] | 1140 | if (!net_eq(dev_net(dev), &init_net)) | 
| Eric W. Biederman | e9dc865 | 2007-09-12 13:02:17 +0200 | [diff] [blame] | 1141 | return NOTIFY_DONE; | 
|  | 1142 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1143 | if (event != NETDEV_UNREGISTER) | 
|  | 1144 | return NOTIFY_DONE; | 
|  | 1145 | v=&vif_table[0]; | 
| Jianjun Kong | c354e12 | 2008-11-03 00:28:02 -0800 | [diff] [blame] | 1146 | for (ct=0; ct<maxvif; ct++,v++) { | 
|  | 1147 | if (v->dev == dev) | 
| Wang Chen | 7dc00c8 | 2008-07-14 20:56:34 -0700 | [diff] [blame] | 1148 | vif_delete(ct, 1); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1149 | } | 
|  | 1150 | return NOTIFY_DONE; | 
|  | 1151 | } | 
|  | 1152 |  | 
|  | 1153 |  | 
| Jianjun Kong | c354e12 | 2008-11-03 00:28:02 -0800 | [diff] [blame] | 1154 | static struct notifier_block ip_mr_notifier = { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1155 | .notifier_call = ipmr_device_event, | 
|  | 1156 | }; | 
|  | 1157 |  | 
|  | 1158 | /* | 
|  | 1159 | * 	Encapsulate a packet by attaching a valid IPIP header to it. | 
|  | 1160 | *	This avoids tunnel drivers and other mess and gives us the speed so | 
|  | 1161 | *	important for multicast video. | 
|  | 1162 | */ | 
| YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1163 |  | 
| Al Viro | 114c784 | 2006-09-27 18:39:29 -0700 | [diff] [blame] | 1164 | static void ip_encap(struct sk_buff *skb, __be32 saddr, __be32 daddr) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1165 | { | 
| Arnaldo Carvalho de Melo | 8856dfa | 2007-03-10 19:40:39 -0300 | [diff] [blame] | 1166 | struct iphdr *iph; | 
| Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 1167 | struct iphdr *old_iph = ip_hdr(skb); | 
| Arnaldo Carvalho de Melo | 8856dfa | 2007-03-10 19:40:39 -0300 | [diff] [blame] | 1168 |  | 
|  | 1169 | skb_push(skb, sizeof(struct iphdr)); | 
| Arnaldo Carvalho de Melo | b0e380b | 2007-04-10 21:21:55 -0700 | [diff] [blame] | 1170 | skb->transport_header = skb->network_header; | 
| Arnaldo Carvalho de Melo | 8856dfa | 2007-03-10 19:40:39 -0300 | [diff] [blame] | 1171 | skb_reset_network_header(skb); | 
| Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 1172 | iph = ip_hdr(skb); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1173 |  | 
|  | 1174 | iph->version	= 	4; | 
| Arnaldo Carvalho de Melo | e023dd6 | 2007-03-12 20:09:36 -0300 | [diff] [blame] | 1175 | iph->tos	=	old_iph->tos; | 
|  | 1176 | iph->ttl	=	old_iph->ttl; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1177 | iph->frag_off	=	0; | 
|  | 1178 | iph->daddr	=	daddr; | 
|  | 1179 | iph->saddr	=	saddr; | 
|  | 1180 | iph->protocol	=	IPPROTO_IPIP; | 
|  | 1181 | iph->ihl	=	5; | 
|  | 1182 | iph->tot_len	=	htons(skb->len); | 
|  | 1183 | ip_select_ident(iph, skb->dst, NULL); | 
|  | 1184 | ip_send_check(iph); | 
|  | 1185 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1186 | memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt)); | 
|  | 1187 | nf_reset(skb); | 
|  | 1188 | } | 
|  | 1189 |  | 
|  | 1190 | static inline int ipmr_forward_finish(struct sk_buff *skb) | 
|  | 1191 | { | 
|  | 1192 | struct ip_options * opt	= &(IPCB(skb)->opt); | 
|  | 1193 |  | 
| Pavel Emelyanov | 7c73a6f | 2008-07-16 20:20:11 -0700 | [diff] [blame] | 1194 | IP_INC_STATS_BH(dev_net(skb->dst->dev), IPSTATS_MIB_OUTFORWDATAGRAMS); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1195 |  | 
|  | 1196 | if (unlikely(opt->optlen)) | 
|  | 1197 | ip_forward_options(skb); | 
|  | 1198 |  | 
|  | 1199 | return dst_output(skb); | 
|  | 1200 | } | 
|  | 1201 |  | 
|  | 1202 | /* | 
|  | 1203 | *	Processing handlers for ipmr_forward | 
|  | 1204 | */ | 
|  | 1205 |  | 
|  | 1206 | static void ipmr_queue_xmit(struct sk_buff *skb, struct mfc_cache *c, int vifi) | 
|  | 1207 | { | 
| Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 1208 | const struct iphdr *iph = ip_hdr(skb); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1209 | struct vif_device *vif = &vif_table[vifi]; | 
|  | 1210 | struct net_device *dev; | 
|  | 1211 | struct rtable *rt; | 
|  | 1212 | int    encap = 0; | 
|  | 1213 |  | 
|  | 1214 | if (vif->dev == NULL) | 
|  | 1215 | goto out_free; | 
|  | 1216 |  | 
|  | 1217 | #ifdef CONFIG_IP_PIMSM | 
|  | 1218 | if (vif->flags & VIFF_REGISTER) { | 
|  | 1219 | vif->pkt_out++; | 
| Jianjun Kong | c354e12 | 2008-11-03 00:28:02 -0800 | [diff] [blame] | 1220 | vif->bytes_out += skb->len; | 
| Pavel Emelyanov | cf3677a | 2008-05-21 14:17:33 -0700 | [diff] [blame] | 1221 | vif->dev->stats.tx_bytes += skb->len; | 
|  | 1222 | vif->dev->stats.tx_packets++; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1223 | ipmr_cache_report(skb, vifi, IGMPMSG_WHOLEPKT); | 
|  | 1224 | kfree_skb(skb); | 
|  | 1225 | return; | 
|  | 1226 | } | 
|  | 1227 | #endif | 
|  | 1228 |  | 
|  | 1229 | if (vif->flags&VIFF_TUNNEL) { | 
|  | 1230 | struct flowi fl = { .oif = vif->link, | 
|  | 1231 | .nl_u = { .ip4_u = | 
|  | 1232 | { .daddr = vif->remote, | 
|  | 1233 | .saddr = vif->local, | 
|  | 1234 | .tos = RT_TOS(iph->tos) } }, | 
|  | 1235 | .proto = IPPROTO_IPIP }; | 
| Denis V. Lunev | f206351 | 2008-01-22 22:07:34 -0800 | [diff] [blame] | 1236 | if (ip_route_output_key(&init_net, &rt, &fl)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1237 | goto out_free; | 
|  | 1238 | encap = sizeof(struct iphdr); | 
|  | 1239 | } else { | 
|  | 1240 | struct flowi fl = { .oif = vif->link, | 
|  | 1241 | .nl_u = { .ip4_u = | 
|  | 1242 | { .daddr = iph->daddr, | 
|  | 1243 | .tos = RT_TOS(iph->tos) } }, | 
|  | 1244 | .proto = IPPROTO_IPIP }; | 
| Denis V. Lunev | f206351 | 2008-01-22 22:07:34 -0800 | [diff] [blame] | 1245 | if (ip_route_output_key(&init_net, &rt, &fl)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1246 | goto out_free; | 
|  | 1247 | } | 
|  | 1248 |  | 
|  | 1249 | dev = rt->u.dst.dev; | 
|  | 1250 |  | 
|  | 1251 | if (skb->len+encap > dst_mtu(&rt->u.dst) && (ntohs(iph->frag_off) & IP_DF)) { | 
|  | 1252 | /* Do not fragment multicasts. Alas, IPv4 does not | 
|  | 1253 | allow to send ICMP, so that packets will disappear | 
|  | 1254 | to blackhole. | 
|  | 1255 | */ | 
|  | 1256 |  | 
| Pavel Emelyanov | 7c73a6f | 2008-07-16 20:20:11 -0700 | [diff] [blame] | 1257 | IP_INC_STATS_BH(dev_net(dev), IPSTATS_MIB_FRAGFAILS); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1258 | ip_rt_put(rt); | 
|  | 1259 | goto out_free; | 
|  | 1260 | } | 
|  | 1261 |  | 
|  | 1262 | encap += LL_RESERVED_SPACE(dev) + rt->u.dst.header_len; | 
|  | 1263 |  | 
|  | 1264 | if (skb_cow(skb, encap)) { | 
| YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1265 | ip_rt_put(rt); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1266 | goto out_free; | 
|  | 1267 | } | 
|  | 1268 |  | 
|  | 1269 | vif->pkt_out++; | 
| Jianjun Kong | c354e12 | 2008-11-03 00:28:02 -0800 | [diff] [blame] | 1270 | vif->bytes_out += skb->len; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1271 |  | 
|  | 1272 | dst_release(skb->dst); | 
|  | 1273 | skb->dst = &rt->u.dst; | 
| Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 1274 | ip_decrease_ttl(ip_hdr(skb)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1275 |  | 
|  | 1276 | /* FIXME: forward and output firewalls used to be called here. | 
|  | 1277 | * What do we do with netfilter? -- RR */ | 
|  | 1278 | if (vif->flags & VIFF_TUNNEL) { | 
|  | 1279 | ip_encap(skb, vif->local, vif->remote); | 
|  | 1280 | /* FIXME: extra output firewall step used to be here. --RR */ | 
| Pavel Emelyanov | 2f4c02d | 2008-05-21 14:16:14 -0700 | [diff] [blame] | 1281 | vif->dev->stats.tx_packets++; | 
|  | 1282 | vif->dev->stats.tx_bytes += skb->len; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1283 | } | 
|  | 1284 |  | 
|  | 1285 | IPCB(skb)->flags |= IPSKB_FORWARDED; | 
|  | 1286 |  | 
|  | 1287 | /* | 
|  | 1288 | * RFC1584 teaches, that DVMRP/PIM router must deliver packets locally | 
|  | 1289 | * not only before forwarding, but after forwarding on all output | 
|  | 1290 | * interfaces. It is clear, if mrouter runs a multicasting | 
|  | 1291 | * program, it should receive packets not depending to what interface | 
|  | 1292 | * program is joined. | 
|  | 1293 | * If we will not make it, the program will have to join on all | 
|  | 1294 | * interfaces. On the other hand, multihoming host (or router, but | 
|  | 1295 | * not mrouter) cannot join to more than one interface - it will | 
|  | 1296 | * result in receiving multiple packets. | 
|  | 1297 | */ | 
| Patrick McHardy | 6e23ae2 | 2007-11-19 18:53:30 -0800 | [diff] [blame] | 1298 | NF_HOOK(PF_INET, NF_INET_FORWARD, skb, skb->dev, dev, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1299 | ipmr_forward_finish); | 
|  | 1300 | return; | 
|  | 1301 |  | 
|  | 1302 | out_free: | 
|  | 1303 | kfree_skb(skb); | 
|  | 1304 | return; | 
|  | 1305 | } | 
|  | 1306 |  | 
|  | 1307 | static int ipmr_find_vif(struct net_device *dev) | 
|  | 1308 | { | 
|  | 1309 | int ct; | 
|  | 1310 | for (ct=maxvif-1; ct>=0; ct--) { | 
|  | 1311 | if (vif_table[ct].dev == dev) | 
|  | 1312 | break; | 
|  | 1313 | } | 
|  | 1314 | return ct; | 
|  | 1315 | } | 
|  | 1316 |  | 
|  | 1317 | /* "local" means that we should preserve one skb (for local delivery) */ | 
|  | 1318 |  | 
|  | 1319 | static int ip_mr_forward(struct sk_buff *skb, struct mfc_cache *cache, int local) | 
|  | 1320 | { | 
|  | 1321 | int psend = -1; | 
|  | 1322 | int vif, ct; | 
|  | 1323 |  | 
|  | 1324 | vif = cache->mfc_parent; | 
|  | 1325 | cache->mfc_un.res.pkt++; | 
|  | 1326 | cache->mfc_un.res.bytes += skb->len; | 
|  | 1327 |  | 
|  | 1328 | /* | 
|  | 1329 | * Wrong interface: drop packet and (maybe) send PIM assert. | 
|  | 1330 | */ | 
|  | 1331 | if (vif_table[vif].dev != skb->dev) { | 
|  | 1332 | int true_vifi; | 
|  | 1333 |  | 
| Eric Dumazet | ee6b967 | 2008-03-05 18:30:47 -0800 | [diff] [blame] | 1334 | if (skb->rtable->fl.iif == 0) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1335 | /* It is our own packet, looped back. | 
|  | 1336 | Very complicated situation... | 
|  | 1337 |  | 
|  | 1338 | The best workaround until routing daemons will be | 
|  | 1339 | fixed is not to redistribute packet, if it was | 
|  | 1340 | send through wrong interface. It means, that | 
|  | 1341 | multicast applications WILL NOT work for | 
|  | 1342 | (S,G), which have default multicast route pointing | 
|  | 1343 | to wrong oif. In any case, it is not a good | 
|  | 1344 | idea to use multicasting applications on router. | 
|  | 1345 | */ | 
|  | 1346 | goto dont_forward; | 
|  | 1347 | } | 
|  | 1348 |  | 
|  | 1349 | cache->mfc_un.res.wrong_if++; | 
|  | 1350 | true_vifi = ipmr_find_vif(skb->dev); | 
|  | 1351 |  | 
|  | 1352 | if (true_vifi >= 0 && mroute_do_assert && | 
|  | 1353 | /* pimsm uses asserts, when switching from RPT to SPT, | 
|  | 1354 | so that we cannot check that packet arrived on an oif. | 
|  | 1355 | It is bad, but otherwise we would need to move pretty | 
|  | 1356 | large chunk of pimd to kernel. Ough... --ANK | 
|  | 1357 | */ | 
|  | 1358 | (mroute_do_pim || cache->mfc_un.res.ttls[true_vifi] < 255) && | 
| YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1359 | time_after(jiffies, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1360 | cache->mfc_un.res.last_assert + MFC_ASSERT_THRESH)) { | 
|  | 1361 | cache->mfc_un.res.last_assert = jiffies; | 
|  | 1362 | ipmr_cache_report(skb, true_vifi, IGMPMSG_WRONGVIF); | 
|  | 1363 | } | 
|  | 1364 | goto dont_forward; | 
|  | 1365 | } | 
|  | 1366 |  | 
|  | 1367 | vif_table[vif].pkt_in++; | 
| Jianjun Kong | c354e12 | 2008-11-03 00:28:02 -0800 | [diff] [blame] | 1368 | vif_table[vif].bytes_in += skb->len; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1369 |  | 
|  | 1370 | /* | 
|  | 1371 | *	Forward the frame | 
|  | 1372 | */ | 
|  | 1373 | for (ct = cache->mfc_un.res.maxvif-1; ct >= cache->mfc_un.res.minvif; ct--) { | 
| Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 1374 | if (ip_hdr(skb)->ttl > cache->mfc_un.res.ttls[ct]) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1375 | if (psend != -1) { | 
|  | 1376 | struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC); | 
|  | 1377 | if (skb2) | 
|  | 1378 | ipmr_queue_xmit(skb2, cache, psend); | 
|  | 1379 | } | 
| Jianjun Kong | c354e12 | 2008-11-03 00:28:02 -0800 | [diff] [blame] | 1380 | psend = ct; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1381 | } | 
|  | 1382 | } | 
|  | 1383 | if (psend != -1) { | 
|  | 1384 | if (local) { | 
|  | 1385 | struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC); | 
|  | 1386 | if (skb2) | 
|  | 1387 | ipmr_queue_xmit(skb2, cache, psend); | 
|  | 1388 | } else { | 
|  | 1389 | ipmr_queue_xmit(skb, cache, psend); | 
|  | 1390 | return 0; | 
|  | 1391 | } | 
|  | 1392 | } | 
|  | 1393 |  | 
|  | 1394 | dont_forward: | 
|  | 1395 | if (!local) | 
|  | 1396 | kfree_skb(skb); | 
|  | 1397 | return 0; | 
|  | 1398 | } | 
|  | 1399 |  | 
|  | 1400 |  | 
|  | 1401 | /* | 
|  | 1402 | *	Multicast packets for forwarding arrive here | 
|  | 1403 | */ | 
|  | 1404 |  | 
|  | 1405 | int ip_mr_input(struct sk_buff *skb) | 
|  | 1406 | { | 
|  | 1407 | struct mfc_cache *cache; | 
| Eric Dumazet | ee6b967 | 2008-03-05 18:30:47 -0800 | [diff] [blame] | 1408 | int local = skb->rtable->rt_flags&RTCF_LOCAL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1409 |  | 
|  | 1410 | /* Packet is looped back after forward, it should not be | 
|  | 1411 | forwarded second time, but still can be delivered locally. | 
|  | 1412 | */ | 
|  | 1413 | if (IPCB(skb)->flags&IPSKB_FORWARDED) | 
|  | 1414 | goto dont_forward; | 
|  | 1415 |  | 
|  | 1416 | if (!local) { | 
|  | 1417 | if (IPCB(skb)->opt.router_alert) { | 
|  | 1418 | if (ip_call_ra_chain(skb)) | 
|  | 1419 | return 0; | 
| Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 1420 | } else if (ip_hdr(skb)->protocol == IPPROTO_IGMP){ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1421 | /* IGMPv1 (and broken IGMPv2 implementations sort of | 
|  | 1422 | Cisco IOS <= 11.2(8)) do not put router alert | 
|  | 1423 | option to IGMP packets destined to routable | 
|  | 1424 | groups. It is very bad, because it means | 
|  | 1425 | that we can forward NO IGMP messages. | 
|  | 1426 | */ | 
|  | 1427 | read_lock(&mrt_lock); | 
|  | 1428 | if (mroute_socket) { | 
| Patrick McHardy | 2715bcf | 2005-06-21 14:06:24 -0700 | [diff] [blame] | 1429 | nf_reset(skb); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1430 | raw_rcv(mroute_socket, skb); | 
|  | 1431 | read_unlock(&mrt_lock); | 
|  | 1432 | return 0; | 
|  | 1433 | } | 
|  | 1434 | read_unlock(&mrt_lock); | 
|  | 1435 | } | 
|  | 1436 | } | 
|  | 1437 |  | 
|  | 1438 | read_lock(&mrt_lock); | 
| Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 1439 | cache = ipmr_cache_find(ip_hdr(skb)->saddr, ip_hdr(skb)->daddr); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1440 |  | 
|  | 1441 | /* | 
|  | 1442 | *	No usable cache entry | 
|  | 1443 | */ | 
| Jianjun Kong | c354e12 | 2008-11-03 00:28:02 -0800 | [diff] [blame] | 1444 | if (cache == NULL) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1445 | int vif; | 
|  | 1446 |  | 
|  | 1447 | if (local) { | 
|  | 1448 | struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC); | 
|  | 1449 | ip_local_deliver(skb); | 
|  | 1450 | if (skb2 == NULL) { | 
|  | 1451 | read_unlock(&mrt_lock); | 
|  | 1452 | return -ENOBUFS; | 
|  | 1453 | } | 
|  | 1454 | skb = skb2; | 
|  | 1455 | } | 
|  | 1456 |  | 
|  | 1457 | vif = ipmr_find_vif(skb->dev); | 
|  | 1458 | if (vif >= 0) { | 
|  | 1459 | int err = ipmr_cache_unresolved(vif, skb); | 
|  | 1460 | read_unlock(&mrt_lock); | 
|  | 1461 |  | 
|  | 1462 | return err; | 
|  | 1463 | } | 
|  | 1464 | read_unlock(&mrt_lock); | 
|  | 1465 | kfree_skb(skb); | 
|  | 1466 | return -ENODEV; | 
|  | 1467 | } | 
|  | 1468 |  | 
|  | 1469 | ip_mr_forward(skb, cache, local); | 
|  | 1470 |  | 
|  | 1471 | read_unlock(&mrt_lock); | 
|  | 1472 |  | 
|  | 1473 | if (local) | 
|  | 1474 | return ip_local_deliver(skb); | 
|  | 1475 |  | 
|  | 1476 | return 0; | 
|  | 1477 |  | 
|  | 1478 | dont_forward: | 
|  | 1479 | if (local) | 
|  | 1480 | return ip_local_deliver(skb); | 
|  | 1481 | kfree_skb(skb); | 
|  | 1482 | return 0; | 
|  | 1483 | } | 
|  | 1484 |  | 
| Ilpo Järvinen | b187920 | 2008-12-16 01:15:11 -0800 | [diff] [blame] | 1485 | #ifdef CONFIG_IP_PIMSM | 
|  | 1486 | static int __pim_rcv(struct sk_buff *skb, unsigned int pimlen) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1487 | { | 
| Ilpo Järvinen | b187920 | 2008-12-16 01:15:11 -0800 | [diff] [blame] | 1488 | struct net_device *reg_dev = NULL; | 
|  | 1489 | struct iphdr *encap; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1490 |  | 
| Ilpo Järvinen | b187920 | 2008-12-16 01:15:11 -0800 | [diff] [blame] | 1491 | encap = (struct iphdr *)(skb_transport_header(skb) + pimlen); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1492 | /* | 
|  | 1493 | Check that: | 
|  | 1494 | a. packet is really destinted to a multicast group | 
|  | 1495 | b. packet is not a NULL-REGISTER | 
|  | 1496 | c. packet is not truncated | 
|  | 1497 | */ | 
| Joe Perches | f97c1e0 | 2007-12-16 13:45:43 -0800 | [diff] [blame] | 1498 | if (!ipv4_is_multicast(encap->daddr) || | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1499 | encap->tot_len == 0 || | 
| Ilpo Järvinen | b187920 | 2008-12-16 01:15:11 -0800 | [diff] [blame] | 1500 | ntohs(encap->tot_len) + pimlen > skb->len) | 
|  | 1501 | return 1; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1502 |  | 
|  | 1503 | read_lock(&mrt_lock); | 
|  | 1504 | if (reg_vif_num >= 0) | 
|  | 1505 | reg_dev = vif_table[reg_vif_num].dev; | 
|  | 1506 | if (reg_dev) | 
|  | 1507 | dev_hold(reg_dev); | 
|  | 1508 | read_unlock(&mrt_lock); | 
|  | 1509 |  | 
| YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1510 | if (reg_dev == NULL) | 
| Ilpo Järvinen | b187920 | 2008-12-16 01:15:11 -0800 | [diff] [blame] | 1511 | return 1; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1512 |  | 
| Arnaldo Carvalho de Melo | b0e380b | 2007-04-10 21:21:55 -0700 | [diff] [blame] | 1513 | skb->mac_header = skb->network_header; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1514 | skb_pull(skb, (u8*)encap - skb->data); | 
| Arnaldo Carvalho de Melo | 31c7711 | 2007-03-10 19:04:55 -0300 | [diff] [blame] | 1515 | skb_reset_network_header(skb); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1516 | skb->dev = reg_dev; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1517 | skb->protocol = htons(ETH_P_IP); | 
|  | 1518 | skb->ip_summed = 0; | 
|  | 1519 | skb->pkt_type = PACKET_HOST; | 
|  | 1520 | dst_release(skb->dst); | 
|  | 1521 | skb->dst = NULL; | 
| Pavel Emelyanov | cf3677a | 2008-05-21 14:17:33 -0700 | [diff] [blame] | 1522 | reg_dev->stats.rx_bytes += skb->len; | 
|  | 1523 | reg_dev->stats.rx_packets++; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1524 | nf_reset(skb); | 
|  | 1525 | netif_rx(skb); | 
|  | 1526 | dev_put(reg_dev); | 
| Ilpo Järvinen | b187920 | 2008-12-16 01:15:11 -0800 | [diff] [blame] | 1527 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1528 | return 0; | 
| Ilpo Järvinen | b187920 | 2008-12-16 01:15:11 -0800 | [diff] [blame] | 1529 | } | 
|  | 1530 | #endif | 
|  | 1531 |  | 
|  | 1532 | #ifdef CONFIG_IP_PIMSM_V1 | 
|  | 1533 | /* | 
|  | 1534 | * Handle IGMP messages of PIMv1 | 
|  | 1535 | */ | 
|  | 1536 |  | 
|  | 1537 | int pim_rcv_v1(struct sk_buff * skb) | 
|  | 1538 | { | 
|  | 1539 | struct igmphdr *pim; | 
|  | 1540 |  | 
|  | 1541 | if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr))) | 
|  | 1542 | goto drop; | 
|  | 1543 |  | 
|  | 1544 | pim = igmp_hdr(skb); | 
|  | 1545 |  | 
|  | 1546 | if (!mroute_do_pim || | 
|  | 1547 | pim->group != PIM_V1_VERSION || pim->code != PIM_V1_REGISTER) | 
|  | 1548 | goto drop; | 
|  | 1549 |  | 
|  | 1550 | if (__pim_rcv(skb, sizeof(*pim))) { | 
|  | 1551 | drop: | 
|  | 1552 | kfree_skb(skb); | 
|  | 1553 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1554 | return 0; | 
|  | 1555 | } | 
|  | 1556 | #endif | 
|  | 1557 |  | 
|  | 1558 | #ifdef CONFIG_IP_PIMSM_V2 | 
|  | 1559 | static int pim_rcv(struct sk_buff * skb) | 
|  | 1560 | { | 
|  | 1561 | struct pimreghdr *pim; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1562 |  | 
| Ilpo Järvinen | b187920 | 2008-12-16 01:15:11 -0800 | [diff] [blame] | 1563 | if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr))) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1564 | goto drop; | 
|  | 1565 |  | 
| Arnaldo Carvalho de Melo | 9c70220 | 2007-04-25 18:04:18 -0700 | [diff] [blame] | 1566 | pim = (struct pimreghdr *)skb_transport_header(skb); | 
| YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1567 | if (pim->type != ((PIM_VERSION<<4)|(PIM_REGISTER)) || | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1568 | (pim->flags&PIM_NULL_REGISTER) || | 
| YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1569 | (ip_compute_csum((void *)pim, sizeof(*pim)) != 0 && | 
| Al Viro | d3bc23e | 2006-11-14 21:24:49 -0800 | [diff] [blame] | 1570 | csum_fold(skb_checksum(skb, 0, skb->len, 0)))) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1571 | goto drop; | 
|  | 1572 |  | 
| Ilpo Järvinen | b187920 | 2008-12-16 01:15:11 -0800 | [diff] [blame] | 1573 | if (__pim_rcv(skb, sizeof(*pim))) { | 
|  | 1574 | drop: | 
|  | 1575 | kfree_skb(skb); | 
|  | 1576 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1577 | return 0; | 
|  | 1578 | } | 
|  | 1579 | #endif | 
|  | 1580 |  | 
|  | 1581 | static int | 
|  | 1582 | ipmr_fill_mroute(struct sk_buff *skb, struct mfc_cache *c, struct rtmsg *rtm) | 
|  | 1583 | { | 
|  | 1584 | int ct; | 
|  | 1585 | struct rtnexthop *nhp; | 
|  | 1586 | struct net_device *dev = vif_table[c->mfc_parent].dev; | 
| Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 1587 | u8 *b = skb_tail_pointer(skb); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1588 | struct rtattr *mp_head; | 
|  | 1589 |  | 
|  | 1590 | if (dev) | 
|  | 1591 | RTA_PUT(skb, RTA_IIF, 4, &dev->ifindex); | 
|  | 1592 |  | 
| Jianjun Kong | c354e12 | 2008-11-03 00:28:02 -0800 | [diff] [blame] | 1593 | mp_head = (struct rtattr *)skb_put(skb, RTA_LENGTH(0)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1594 |  | 
|  | 1595 | for (ct = c->mfc_un.res.minvif; ct < c->mfc_un.res.maxvif; ct++) { | 
|  | 1596 | if (c->mfc_un.res.ttls[ct] < 255) { | 
|  | 1597 | if (skb_tailroom(skb) < RTA_ALIGN(RTA_ALIGN(sizeof(*nhp)) + 4)) | 
|  | 1598 | goto rtattr_failure; | 
| Jianjun Kong | c354e12 | 2008-11-03 00:28:02 -0800 | [diff] [blame] | 1599 | nhp = (struct rtnexthop *)skb_put(skb, RTA_ALIGN(sizeof(*nhp))); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1600 | nhp->rtnh_flags = 0; | 
|  | 1601 | nhp->rtnh_hops = c->mfc_un.res.ttls[ct]; | 
|  | 1602 | nhp->rtnh_ifindex = vif_table[ct].dev->ifindex; | 
|  | 1603 | nhp->rtnh_len = sizeof(*nhp); | 
|  | 1604 | } | 
|  | 1605 | } | 
|  | 1606 | mp_head->rta_type = RTA_MULTIPATH; | 
| Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 1607 | mp_head->rta_len = skb_tail_pointer(skb) - (u8 *)mp_head; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1608 | rtm->rtm_type = RTN_MULTICAST; | 
|  | 1609 | return 1; | 
|  | 1610 |  | 
|  | 1611 | rtattr_failure: | 
| Arnaldo Carvalho de Melo | dc5fc57 | 2007-03-25 23:06:12 -0700 | [diff] [blame] | 1612 | nlmsg_trim(skb, b); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1613 | return -EMSGSIZE; | 
|  | 1614 | } | 
|  | 1615 |  | 
|  | 1616 | int ipmr_get_route(struct sk_buff *skb, struct rtmsg *rtm, int nowait) | 
|  | 1617 | { | 
|  | 1618 | int err; | 
|  | 1619 | struct mfc_cache *cache; | 
| Eric Dumazet | ee6b967 | 2008-03-05 18:30:47 -0800 | [diff] [blame] | 1620 | struct rtable *rt = skb->rtable; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1621 |  | 
|  | 1622 | read_lock(&mrt_lock); | 
|  | 1623 | cache = ipmr_cache_find(rt->rt_src, rt->rt_dst); | 
|  | 1624 |  | 
| Jianjun Kong | c354e12 | 2008-11-03 00:28:02 -0800 | [diff] [blame] | 1625 | if (cache == NULL) { | 
| Alexey Kuznetsov | 7228749 | 2006-07-25 16:45:12 -0700 | [diff] [blame] | 1626 | struct sk_buff *skb2; | 
| Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 1627 | struct iphdr *iph; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1628 | struct net_device *dev; | 
|  | 1629 | int vif; | 
|  | 1630 |  | 
|  | 1631 | if (nowait) { | 
|  | 1632 | read_unlock(&mrt_lock); | 
|  | 1633 | return -EAGAIN; | 
|  | 1634 | } | 
|  | 1635 |  | 
|  | 1636 | dev = skb->dev; | 
|  | 1637 | if (dev == NULL || (vif = ipmr_find_vif(dev)) < 0) { | 
|  | 1638 | read_unlock(&mrt_lock); | 
|  | 1639 | return -ENODEV; | 
|  | 1640 | } | 
| Alexey Kuznetsov | 7228749 | 2006-07-25 16:45:12 -0700 | [diff] [blame] | 1641 | skb2 = skb_clone(skb, GFP_ATOMIC); | 
|  | 1642 | if (!skb2) { | 
|  | 1643 | read_unlock(&mrt_lock); | 
|  | 1644 | return -ENOMEM; | 
|  | 1645 | } | 
|  | 1646 |  | 
| Arnaldo Carvalho de Melo | e2d1bca | 2007-04-10 20:46:21 -0700 | [diff] [blame] | 1647 | skb_push(skb2, sizeof(struct iphdr)); | 
|  | 1648 | skb_reset_network_header(skb2); | 
| Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 1649 | iph = ip_hdr(skb2); | 
|  | 1650 | iph->ihl = sizeof(struct iphdr) >> 2; | 
|  | 1651 | iph->saddr = rt->rt_src; | 
|  | 1652 | iph->daddr = rt->rt_dst; | 
|  | 1653 | iph->version = 0; | 
| Alexey Kuznetsov | 7228749 | 2006-07-25 16:45:12 -0700 | [diff] [blame] | 1654 | err = ipmr_cache_unresolved(vif, skb2); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1655 | read_unlock(&mrt_lock); | 
|  | 1656 | return err; | 
|  | 1657 | } | 
|  | 1658 |  | 
|  | 1659 | if (!nowait && (rtm->rtm_flags&RTM_F_NOTIFY)) | 
|  | 1660 | cache->mfc_flags |= MFC_NOTIFY; | 
|  | 1661 | err = ipmr_fill_mroute(skb, cache, rtm); | 
|  | 1662 | read_unlock(&mrt_lock); | 
|  | 1663 | return err; | 
|  | 1664 | } | 
|  | 1665 |  | 
| YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1666 | #ifdef CONFIG_PROC_FS | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1667 | /* | 
|  | 1668 | *	The /proc interfaces to multicast routing /proc/ip_mr_cache /proc/ip_mr_vif | 
|  | 1669 | */ | 
|  | 1670 | struct ipmr_vif_iter { | 
|  | 1671 | int ct; | 
|  | 1672 | }; | 
|  | 1673 |  | 
|  | 1674 | static struct vif_device *ipmr_vif_seq_idx(struct ipmr_vif_iter *iter, | 
|  | 1675 | loff_t pos) | 
|  | 1676 | { | 
|  | 1677 | for (iter->ct = 0; iter->ct < maxvif; ++iter->ct) { | 
| Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1678 | if (!VIF_EXISTS(iter->ct)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1679 | continue; | 
| YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1680 | if (pos-- == 0) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1681 | return &vif_table[iter->ct]; | 
|  | 1682 | } | 
|  | 1683 | return NULL; | 
|  | 1684 | } | 
|  | 1685 |  | 
|  | 1686 | static void *ipmr_vif_seq_start(struct seq_file *seq, loff_t *pos) | 
| Stephen Hemminger | ba93ef7 | 2008-01-21 17:28:59 -0800 | [diff] [blame] | 1687 | __acquires(mrt_lock) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1688 | { | 
|  | 1689 | read_lock(&mrt_lock); | 
| YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1690 | return *pos ? ipmr_vif_seq_idx(seq->private, *pos - 1) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1691 | : SEQ_START_TOKEN; | 
|  | 1692 | } | 
|  | 1693 |  | 
|  | 1694 | static void *ipmr_vif_seq_next(struct seq_file *seq, void *v, loff_t *pos) | 
|  | 1695 | { | 
|  | 1696 | struct ipmr_vif_iter *iter = seq->private; | 
|  | 1697 |  | 
|  | 1698 | ++*pos; | 
|  | 1699 | if (v == SEQ_START_TOKEN) | 
|  | 1700 | return ipmr_vif_seq_idx(iter, 0); | 
| YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1701 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1702 | while (++iter->ct < maxvif) { | 
| Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1703 | if (!VIF_EXISTS(iter->ct)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1704 | continue; | 
|  | 1705 | return &vif_table[iter->ct]; | 
|  | 1706 | } | 
|  | 1707 | return NULL; | 
|  | 1708 | } | 
|  | 1709 |  | 
|  | 1710 | static void ipmr_vif_seq_stop(struct seq_file *seq, void *v) | 
| Stephen Hemminger | ba93ef7 | 2008-01-21 17:28:59 -0800 | [diff] [blame] | 1711 | __releases(mrt_lock) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1712 | { | 
|  | 1713 | read_unlock(&mrt_lock); | 
|  | 1714 | } | 
|  | 1715 |  | 
|  | 1716 | static int ipmr_vif_seq_show(struct seq_file *seq, void *v) | 
|  | 1717 | { | 
|  | 1718 | if (v == SEQ_START_TOKEN) { | 
| YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1719 | seq_puts(seq, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1720 | "Interface      BytesIn  PktsIn  BytesOut PktsOut Flags Local    Remote\n"); | 
|  | 1721 | } else { | 
|  | 1722 | const struct vif_device *vif = v; | 
|  | 1723 | const char *name =  vif->dev ? vif->dev->name : "none"; | 
|  | 1724 |  | 
|  | 1725 | seq_printf(seq, | 
|  | 1726 | "%2Zd %-10s %8ld %7ld  %8ld %7ld %05X %08X %08X\n", | 
|  | 1727 | vif - vif_table, | 
| YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1728 | name, vif->bytes_in, vif->pkt_in, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1729 | vif->bytes_out, vif->pkt_out, | 
|  | 1730 | vif->flags, vif->local, vif->remote); | 
|  | 1731 | } | 
|  | 1732 | return 0; | 
|  | 1733 | } | 
|  | 1734 |  | 
| Stephen Hemminger | f690808 | 2007-03-12 14:34:29 -0700 | [diff] [blame] | 1735 | static const struct seq_operations ipmr_vif_seq_ops = { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1736 | .start = ipmr_vif_seq_start, | 
|  | 1737 | .next  = ipmr_vif_seq_next, | 
|  | 1738 | .stop  = ipmr_vif_seq_stop, | 
|  | 1739 | .show  = ipmr_vif_seq_show, | 
|  | 1740 | }; | 
|  | 1741 |  | 
|  | 1742 | static int ipmr_vif_open(struct inode *inode, struct file *file) | 
|  | 1743 | { | 
| Pavel Emelyanov | cf7732e | 2007-10-10 02:29:29 -0700 | [diff] [blame] | 1744 | return seq_open_private(file, &ipmr_vif_seq_ops, | 
|  | 1745 | sizeof(struct ipmr_vif_iter)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1746 | } | 
|  | 1747 |  | 
| Arjan van de Ven | 9a32144 | 2007-02-12 00:55:35 -0800 | [diff] [blame] | 1748 | static const struct file_operations ipmr_vif_fops = { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1749 | .owner	 = THIS_MODULE, | 
|  | 1750 | .open    = ipmr_vif_open, | 
|  | 1751 | .read    = seq_read, | 
|  | 1752 | .llseek  = seq_lseek, | 
|  | 1753 | .release = seq_release_private, | 
|  | 1754 | }; | 
|  | 1755 |  | 
|  | 1756 | struct ipmr_mfc_iter { | 
|  | 1757 | struct mfc_cache **cache; | 
|  | 1758 | int ct; | 
|  | 1759 | }; | 
|  | 1760 |  | 
|  | 1761 |  | 
|  | 1762 | static struct mfc_cache *ipmr_mfc_seq_idx(struct ipmr_mfc_iter *it, loff_t pos) | 
|  | 1763 | { | 
|  | 1764 | struct mfc_cache *mfc; | 
|  | 1765 |  | 
|  | 1766 | it->cache = mfc_cache_array; | 
|  | 1767 | read_lock(&mrt_lock); | 
| YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1768 | for (it->ct = 0; it->ct < MFC_LINES; it->ct++) | 
| Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1769 | for (mfc = mfc_cache_array[it->ct]; mfc; mfc = mfc->next) | 
| YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1770 | if (pos-- == 0) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1771 | return mfc; | 
|  | 1772 | read_unlock(&mrt_lock); | 
|  | 1773 |  | 
|  | 1774 | it->cache = &mfc_unres_queue; | 
|  | 1775 | spin_lock_bh(&mfc_unres_lock); | 
| Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1776 | for (mfc = mfc_unres_queue; mfc; mfc = mfc->next) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1777 | if (pos-- == 0) | 
|  | 1778 | return mfc; | 
|  | 1779 | spin_unlock_bh(&mfc_unres_lock); | 
|  | 1780 |  | 
|  | 1781 | it->cache = NULL; | 
|  | 1782 | return NULL; | 
|  | 1783 | } | 
|  | 1784 |  | 
|  | 1785 |  | 
|  | 1786 | static void *ipmr_mfc_seq_start(struct seq_file *seq, loff_t *pos) | 
|  | 1787 | { | 
|  | 1788 | struct ipmr_mfc_iter *it = seq->private; | 
|  | 1789 | it->cache = NULL; | 
|  | 1790 | it->ct = 0; | 
| YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1791 | return *pos ? ipmr_mfc_seq_idx(seq->private, *pos - 1) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1792 | : SEQ_START_TOKEN; | 
|  | 1793 | } | 
|  | 1794 |  | 
|  | 1795 | static void *ipmr_mfc_seq_next(struct seq_file *seq, void *v, loff_t *pos) | 
|  | 1796 | { | 
|  | 1797 | struct mfc_cache *mfc = v; | 
|  | 1798 | struct ipmr_mfc_iter *it = seq->private; | 
|  | 1799 |  | 
|  | 1800 | ++*pos; | 
|  | 1801 |  | 
|  | 1802 | if (v == SEQ_START_TOKEN) | 
|  | 1803 | return ipmr_mfc_seq_idx(seq->private, 0); | 
|  | 1804 |  | 
|  | 1805 | if (mfc->next) | 
|  | 1806 | return mfc->next; | 
| YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1807 |  | 
|  | 1808 | if (it->cache == &mfc_unres_queue) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1809 | goto end_of_list; | 
|  | 1810 |  | 
|  | 1811 | BUG_ON(it->cache != mfc_cache_array); | 
|  | 1812 |  | 
|  | 1813 | while (++it->ct < MFC_LINES) { | 
|  | 1814 | mfc = mfc_cache_array[it->ct]; | 
|  | 1815 | if (mfc) | 
|  | 1816 | return mfc; | 
|  | 1817 | } | 
|  | 1818 |  | 
|  | 1819 | /* exhausted cache_array, show unresolved */ | 
|  | 1820 | read_unlock(&mrt_lock); | 
|  | 1821 | it->cache = &mfc_unres_queue; | 
|  | 1822 | it->ct = 0; | 
| YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1823 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1824 | spin_lock_bh(&mfc_unres_lock); | 
|  | 1825 | mfc = mfc_unres_queue; | 
| YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1826 | if (mfc) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1827 | return mfc; | 
|  | 1828 |  | 
|  | 1829 | end_of_list: | 
|  | 1830 | spin_unlock_bh(&mfc_unres_lock); | 
|  | 1831 | it->cache = NULL; | 
|  | 1832 |  | 
|  | 1833 | return NULL; | 
|  | 1834 | } | 
|  | 1835 |  | 
|  | 1836 | static void ipmr_mfc_seq_stop(struct seq_file *seq, void *v) | 
|  | 1837 | { | 
|  | 1838 | struct ipmr_mfc_iter *it = seq->private; | 
|  | 1839 |  | 
|  | 1840 | if (it->cache == &mfc_unres_queue) | 
|  | 1841 | spin_unlock_bh(&mfc_unres_lock); | 
|  | 1842 | else if (it->cache == mfc_cache_array) | 
|  | 1843 | read_unlock(&mrt_lock); | 
|  | 1844 | } | 
|  | 1845 |  | 
|  | 1846 | static int ipmr_mfc_seq_show(struct seq_file *seq, void *v) | 
|  | 1847 | { | 
|  | 1848 | int n; | 
|  | 1849 |  | 
|  | 1850 | if (v == SEQ_START_TOKEN) { | 
| YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1851 | seq_puts(seq, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1852 | "Group    Origin   Iif     Pkts    Bytes    Wrong Oifs\n"); | 
|  | 1853 | } else { | 
|  | 1854 | const struct mfc_cache *mfc = v; | 
|  | 1855 | const struct ipmr_mfc_iter *it = seq->private; | 
| YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1856 |  | 
| Benjamin Thery | 999890b | 2008-12-03 22:22:16 -0800 | [diff] [blame] | 1857 | seq_printf(seq, "%08lX %08lX %-3hd", | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1858 | (unsigned long) mfc->mfc_mcastgrp, | 
|  | 1859 | (unsigned long) mfc->mfc_origin, | 
| Benjamin Thery | 1ea472e | 2008-12-03 22:21:47 -0800 | [diff] [blame] | 1860 | mfc->mfc_parent); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1861 |  | 
|  | 1862 | if (it->cache != &mfc_unres_queue) { | 
| Benjamin Thery | 1ea472e | 2008-12-03 22:21:47 -0800 | [diff] [blame] | 1863 | seq_printf(seq, " %8lu %8lu %8lu", | 
|  | 1864 | mfc->mfc_un.res.pkt, | 
|  | 1865 | mfc->mfc_un.res.bytes, | 
|  | 1866 | mfc->mfc_un.res.wrong_if); | 
| Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1867 | for (n = mfc->mfc_un.res.minvif; | 
|  | 1868 | n < mfc->mfc_un.res.maxvif; n++ ) { | 
|  | 1869 | if (VIF_EXISTS(n) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1870 | && mfc->mfc_un.res.ttls[n] < 255) | 
| YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1871 | seq_printf(seq, | 
|  | 1872 | " %2d:%-3d", | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1873 | n, mfc->mfc_un.res.ttls[n]); | 
|  | 1874 | } | 
| Benjamin Thery | 1ea472e | 2008-12-03 22:21:47 -0800 | [diff] [blame] | 1875 | } else { | 
|  | 1876 | /* unresolved mfc_caches don't contain | 
|  | 1877 | * pkt, bytes and wrong_if values | 
|  | 1878 | */ | 
|  | 1879 | seq_printf(seq, " %8lu %8lu %8lu", 0ul, 0ul, 0ul); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1880 | } | 
|  | 1881 | seq_putc(seq, '\n'); | 
|  | 1882 | } | 
|  | 1883 | return 0; | 
|  | 1884 | } | 
|  | 1885 |  | 
| Stephen Hemminger | f690808 | 2007-03-12 14:34:29 -0700 | [diff] [blame] | 1886 | static const struct seq_operations ipmr_mfc_seq_ops = { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1887 | .start = ipmr_mfc_seq_start, | 
|  | 1888 | .next  = ipmr_mfc_seq_next, | 
|  | 1889 | .stop  = ipmr_mfc_seq_stop, | 
|  | 1890 | .show  = ipmr_mfc_seq_show, | 
|  | 1891 | }; | 
|  | 1892 |  | 
|  | 1893 | static int ipmr_mfc_open(struct inode *inode, struct file *file) | 
|  | 1894 | { | 
| Pavel Emelyanov | cf7732e | 2007-10-10 02:29:29 -0700 | [diff] [blame] | 1895 | return seq_open_private(file, &ipmr_mfc_seq_ops, | 
|  | 1896 | sizeof(struct ipmr_mfc_iter)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1897 | } | 
|  | 1898 |  | 
| Arjan van de Ven | 9a32144 | 2007-02-12 00:55:35 -0800 | [diff] [blame] | 1899 | static const struct file_operations ipmr_mfc_fops = { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1900 | .owner	 = THIS_MODULE, | 
|  | 1901 | .open    = ipmr_mfc_open, | 
|  | 1902 | .read    = seq_read, | 
|  | 1903 | .llseek  = seq_lseek, | 
|  | 1904 | .release = seq_release_private, | 
|  | 1905 | }; | 
| YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1906 | #endif | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1907 |  | 
|  | 1908 | #ifdef CONFIG_IP_PIMSM_V2 | 
|  | 1909 | static struct net_protocol pim_protocol = { | 
|  | 1910 | .handler	=	pim_rcv, | 
|  | 1911 | }; | 
|  | 1912 | #endif | 
|  | 1913 |  | 
|  | 1914 |  | 
|  | 1915 | /* | 
|  | 1916 | *	Setup for IP multicast routing | 
|  | 1917 | */ | 
| YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1918 |  | 
| Wang Chen | 03d2f89 | 2008-07-03 12:13:36 +0800 | [diff] [blame] | 1919 | int __init ip_mr_init(void) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1920 | { | 
| Wang Chen | 03d2f89 | 2008-07-03 12:13:36 +0800 | [diff] [blame] | 1921 | int err; | 
|  | 1922 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1923 | mrt_cachep = kmem_cache_create("ip_mrt_cache", | 
|  | 1924 | sizeof(struct mfc_cache), | 
| Alexey Dobriyan | e5d679f | 2006-08-26 19:25:52 -0700 | [diff] [blame] | 1925 | 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, | 
| Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 1926 | NULL); | 
| Wang Chen | 03d2f89 | 2008-07-03 12:13:36 +0800 | [diff] [blame] | 1927 | if (!mrt_cachep) | 
|  | 1928 | return -ENOMEM; | 
|  | 1929 |  | 
| Pavel Emelyanov | b24b8a2 | 2008-01-23 21:20:07 -0800 | [diff] [blame] | 1930 | setup_timer(&ipmr_expire_timer, ipmr_expire_process, 0); | 
| Wang Chen | 03d2f89 | 2008-07-03 12:13:36 +0800 | [diff] [blame] | 1931 | err = register_netdevice_notifier(&ip_mr_notifier); | 
|  | 1932 | if (err) | 
|  | 1933 | goto reg_notif_fail; | 
| YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1934 | #ifdef CONFIG_PROC_FS | 
| Wang Chen | 03d2f89 | 2008-07-03 12:13:36 +0800 | [diff] [blame] | 1935 | err = -ENOMEM; | 
|  | 1936 | if (!proc_net_fops_create(&init_net, "ip_mr_vif", 0, &ipmr_vif_fops)) | 
|  | 1937 | goto proc_vif_fail; | 
|  | 1938 | if (!proc_net_fops_create(&init_net, "ip_mr_cache", 0, &ipmr_mfc_fops)) | 
|  | 1939 | goto proc_cache_fail; | 
| YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1940 | #endif | 
| Wang Chen | 03d2f89 | 2008-07-03 12:13:36 +0800 | [diff] [blame] | 1941 | return 0; | 
| Wang Chen | 03d2f89 | 2008-07-03 12:13:36 +0800 | [diff] [blame] | 1942 | #ifdef CONFIG_PROC_FS | 
| Wang Chen | 03d2f89 | 2008-07-03 12:13:36 +0800 | [diff] [blame] | 1943 | proc_cache_fail: | 
|  | 1944 | proc_net_remove(&init_net, "ip_mr_vif"); | 
| Benjamin Thery | c3e3889 | 2008-11-19 14:07:41 -0800 | [diff] [blame] | 1945 | proc_vif_fail: | 
|  | 1946 | unregister_netdevice_notifier(&ip_mr_notifier); | 
| Wang Chen | 03d2f89 | 2008-07-03 12:13:36 +0800 | [diff] [blame] | 1947 | #endif | 
| Benjamin Thery | c3e3889 | 2008-11-19 14:07:41 -0800 | [diff] [blame] | 1948 | reg_notif_fail: | 
|  | 1949 | del_timer(&ipmr_expire_timer); | 
|  | 1950 | kmem_cache_destroy(mrt_cachep); | 
| Wang Chen | 03d2f89 | 2008-07-03 12:13:36 +0800 | [diff] [blame] | 1951 | return err; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1952 | } |