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