11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * IP multicast routing support for mrouted 3.6/3.8 31da177e4SLinus Torvalds * 4113aa838SAlan Cox * (c) 1995 Alan Cox, <alan@lxorguk.ukuu.org.uk> 51da177e4SLinus Torvalds * Linux Consultancy and Custom Driver Development 61da177e4SLinus Torvalds * 71da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or 81da177e4SLinus Torvalds * modify it under the terms of the GNU General Public License 91da177e4SLinus Torvalds * as published by the Free Software Foundation; either version 101da177e4SLinus Torvalds * 2 of the License, or (at your option) any later version. 111da177e4SLinus Torvalds * 121da177e4SLinus Torvalds * Fixes: 131da177e4SLinus Torvalds * Michael Chastain : Incorrect size of copying. 141da177e4SLinus Torvalds * Alan Cox : Added the cache manager code 151da177e4SLinus Torvalds * Alan Cox : Fixed the clone/copy bug and device race. 161da177e4SLinus Torvalds * Mike McLagan : Routing by source 171da177e4SLinus Torvalds * Malcolm Beattie : Buffer handling fixes. 181da177e4SLinus Torvalds * Alexey Kuznetsov : Double buffer free and other fixes. 191da177e4SLinus Torvalds * SVR Anand : Fixed several multicast bugs and problems. 201da177e4SLinus Torvalds * Alexey Kuznetsov : Status, optimisations and more. 211da177e4SLinus Torvalds * Brad Parker : Better behaviour on mrouted upcall 221da177e4SLinus Torvalds * overflow. 231da177e4SLinus Torvalds * Carlos Picoto : PIMv1 Support 241da177e4SLinus Torvalds * Pavlin Ivanov Radoslavov: PIMv2 Registers must checksum only PIM header 251da177e4SLinus Torvalds * Relax this requrement to work with older peers. 261da177e4SLinus Torvalds * 271da177e4SLinus Torvalds */ 281da177e4SLinus Torvalds 291da177e4SLinus Torvalds #include <asm/system.h> 301da177e4SLinus Torvalds #include <asm/uaccess.h> 311da177e4SLinus Torvalds #include <linux/types.h> 324fc268d2SRandy Dunlap #include <linux/capability.h> 331da177e4SLinus Torvalds #include <linux/errno.h> 341da177e4SLinus Torvalds #include <linux/timer.h> 351da177e4SLinus Torvalds #include <linux/mm.h> 361da177e4SLinus Torvalds #include <linux/kernel.h> 371da177e4SLinus Torvalds #include <linux/fcntl.h> 381da177e4SLinus Torvalds #include <linux/stat.h> 391da177e4SLinus Torvalds #include <linux/socket.h> 401da177e4SLinus Torvalds #include <linux/in.h> 411da177e4SLinus Torvalds #include <linux/inet.h> 421da177e4SLinus Torvalds #include <linux/netdevice.h> 431da177e4SLinus Torvalds #include <linux/inetdevice.h> 441da177e4SLinus Torvalds #include <linux/igmp.h> 451da177e4SLinus Torvalds #include <linux/proc_fs.h> 461da177e4SLinus Torvalds #include <linux/seq_file.h> 471da177e4SLinus Torvalds #include <linux/mroute.h> 481da177e4SLinus Torvalds #include <linux/init.h> 4946f25dffSKris Katterjohn #include <linux/if_ether.h> 50457c4cbcSEric W. Biederman #include <net/net_namespace.h> 511da177e4SLinus Torvalds #include <net/ip.h> 521da177e4SLinus Torvalds #include <net/protocol.h> 531da177e4SLinus Torvalds #include <linux/skbuff.h> 5414c85021SArnaldo Carvalho de Melo #include <net/route.h> 551da177e4SLinus Torvalds #include <net/sock.h> 561da177e4SLinus Torvalds #include <net/icmp.h> 571da177e4SLinus Torvalds #include <net/udp.h> 581da177e4SLinus Torvalds #include <net/raw.h> 591da177e4SLinus Torvalds #include <linux/notifier.h> 601da177e4SLinus Torvalds #include <linux/if_arp.h> 611da177e4SLinus Torvalds #include <linux/netfilter_ipv4.h> 621da177e4SLinus Torvalds #include <net/ipip.h> 631da177e4SLinus Torvalds #include <net/checksum.h> 64dc5fc579SArnaldo Carvalho de Melo #include <net/netlink.h> 651da177e4SLinus Torvalds 661da177e4SLinus Torvalds #if defined(CONFIG_IP_PIMSM_V1) || defined(CONFIG_IP_PIMSM_V2) 671da177e4SLinus Torvalds #define CONFIG_IP_PIMSM 1 681da177e4SLinus Torvalds #endif 691da177e4SLinus Torvalds 701da177e4SLinus Torvalds /* Big lock, protecting vif table, mrt cache and mroute socket state. 711da177e4SLinus Torvalds Note that the changes are semaphored via rtnl_lock. 721da177e4SLinus Torvalds */ 731da177e4SLinus Torvalds 741da177e4SLinus Torvalds static DEFINE_RWLOCK(mrt_lock); 751da177e4SLinus Torvalds 761da177e4SLinus Torvalds /* 771da177e4SLinus Torvalds * Multicast router control variables 781da177e4SLinus Torvalds */ 791da177e4SLinus Torvalds 80cf958ae3SBenjamin Thery #define VIF_EXISTS(_net, _idx) ((_net)->ipv4.vif_table[_idx].dev != NULL) 811da177e4SLinus Torvalds 821da177e4SLinus Torvalds static struct mfc_cache *mfc_unres_queue; /* Queue of unresolved entries */ 831da177e4SLinus Torvalds 841da177e4SLinus Torvalds /* Special spinlock for queue of unresolved entries */ 851da177e4SLinus Torvalds static DEFINE_SPINLOCK(mfc_unres_lock); 861da177e4SLinus Torvalds 871da177e4SLinus Torvalds /* We return to original Alan's scheme. Hash table of resolved 881da177e4SLinus Torvalds entries is changed only in process context and protected 891da177e4SLinus Torvalds with weak lock mrt_lock. Queue of unresolved entries is protected 901da177e4SLinus Torvalds with strong spinlock mfc_unres_lock. 911da177e4SLinus Torvalds 921da177e4SLinus Torvalds In this case data path is free of exclusive locks at all. 931da177e4SLinus Torvalds */ 941da177e4SLinus Torvalds 95e18b890bSChristoph Lameter static struct kmem_cache *mrt_cachep __read_mostly; 961da177e4SLinus Torvalds 971da177e4SLinus Torvalds static int ip_mr_forward(struct sk_buff *skb, struct mfc_cache *cache, int local); 984feb88e5SBenjamin Thery static int ipmr_cache_report(struct net *net, 994feb88e5SBenjamin Thery struct sk_buff *pkt, vifi_t vifi, int assert); 1001da177e4SLinus Torvalds static int ipmr_fill_mroute(struct sk_buff *skb, struct mfc_cache *c, struct rtmsg *rtm); 1011da177e4SLinus Torvalds 1021da177e4SLinus Torvalds static struct timer_list ipmr_expire_timer; 1031da177e4SLinus Torvalds 1041da177e4SLinus Torvalds /* Service routines creating virtual interfaces: DVMRP tunnels and PIMREG */ 1051da177e4SLinus Torvalds 106d607032dSWang Chen static void ipmr_del_tunnel(struct net_device *dev, struct vifctl *v) 107d607032dSWang Chen { 1084feb88e5SBenjamin Thery struct net *net = dev_net(dev); 1094feb88e5SBenjamin Thery 110d607032dSWang Chen dev_close(dev); 111d607032dSWang Chen 1124feb88e5SBenjamin Thery dev = __dev_get_by_name(net, "tunl0"); 113d607032dSWang Chen if (dev) { 1145bc3eb7eSStephen Hemminger const struct net_device_ops *ops = dev->netdev_ops; 115d607032dSWang Chen struct ifreq ifr; 116d607032dSWang Chen struct ip_tunnel_parm p; 117d607032dSWang Chen 118d607032dSWang Chen memset(&p, 0, sizeof(p)); 119d607032dSWang Chen p.iph.daddr = v->vifc_rmt_addr.s_addr; 120d607032dSWang Chen p.iph.saddr = v->vifc_lcl_addr.s_addr; 121d607032dSWang Chen p.iph.version = 4; 122d607032dSWang Chen p.iph.ihl = 5; 123d607032dSWang Chen p.iph.protocol = IPPROTO_IPIP; 124d607032dSWang Chen sprintf(p.name, "dvmrp%d", v->vifc_vifi); 125d607032dSWang Chen ifr.ifr_ifru.ifru_data = (__force void __user *)&p; 126d607032dSWang Chen 1275bc3eb7eSStephen Hemminger if (ops->ndo_do_ioctl) { 1285bc3eb7eSStephen Hemminger mm_segment_t oldfs = get_fs(); 1295bc3eb7eSStephen Hemminger 1305bc3eb7eSStephen Hemminger set_fs(KERNEL_DS); 1315bc3eb7eSStephen Hemminger ops->ndo_do_ioctl(dev, &ifr, SIOCDELTUNNEL); 132d607032dSWang Chen set_fs(oldfs); 133d607032dSWang Chen } 134d607032dSWang Chen } 1355bc3eb7eSStephen Hemminger } 136d607032dSWang Chen 1371da177e4SLinus Torvalds static 1384feb88e5SBenjamin Thery struct net_device *ipmr_new_tunnel(struct net *net, struct vifctl *v) 1391da177e4SLinus Torvalds { 1401da177e4SLinus Torvalds struct net_device *dev; 1411da177e4SLinus Torvalds 1424feb88e5SBenjamin Thery dev = __dev_get_by_name(net, "tunl0"); 1431da177e4SLinus Torvalds 1441da177e4SLinus Torvalds if (dev) { 1455bc3eb7eSStephen Hemminger const struct net_device_ops *ops = dev->netdev_ops; 1461da177e4SLinus Torvalds int err; 1471da177e4SLinus Torvalds struct ifreq ifr; 1481da177e4SLinus Torvalds struct ip_tunnel_parm p; 1491da177e4SLinus Torvalds struct in_device *in_dev; 1501da177e4SLinus Torvalds 1511da177e4SLinus Torvalds memset(&p, 0, sizeof(p)); 1521da177e4SLinus Torvalds p.iph.daddr = v->vifc_rmt_addr.s_addr; 1531da177e4SLinus Torvalds p.iph.saddr = v->vifc_lcl_addr.s_addr; 1541da177e4SLinus Torvalds p.iph.version = 4; 1551da177e4SLinus Torvalds p.iph.ihl = 5; 1561da177e4SLinus Torvalds p.iph.protocol = IPPROTO_IPIP; 1571da177e4SLinus Torvalds sprintf(p.name, "dvmrp%d", v->vifc_vifi); 158ba93ef74SStephen Hemminger ifr.ifr_ifru.ifru_data = (__force void __user *)&p; 1591da177e4SLinus Torvalds 1605bc3eb7eSStephen Hemminger if (ops->ndo_do_ioctl) { 1615bc3eb7eSStephen Hemminger mm_segment_t oldfs = get_fs(); 1625bc3eb7eSStephen Hemminger 1635bc3eb7eSStephen Hemminger set_fs(KERNEL_DS); 1645bc3eb7eSStephen Hemminger err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL); 1651da177e4SLinus Torvalds set_fs(oldfs); 1665bc3eb7eSStephen Hemminger } else 1675bc3eb7eSStephen Hemminger err = -EOPNOTSUPP; 1681da177e4SLinus Torvalds 1691da177e4SLinus Torvalds dev = NULL; 1701da177e4SLinus Torvalds 1714feb88e5SBenjamin Thery if (err == 0 && 1724feb88e5SBenjamin Thery (dev = __dev_get_by_name(net, p.name)) != NULL) { 1731da177e4SLinus Torvalds dev->flags |= IFF_MULTICAST; 1741da177e4SLinus Torvalds 175e5ed6399SHerbert Xu in_dev = __in_dev_get_rtnl(dev); 17671e27da9SHerbert Xu if (in_dev == NULL) 1771da177e4SLinus Torvalds goto failure; 17871e27da9SHerbert Xu 17971e27da9SHerbert Xu ipv4_devconf_setall(in_dev); 18071e27da9SHerbert Xu IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0; 1811da177e4SLinus Torvalds 1821da177e4SLinus Torvalds if (dev_open(dev)) 1831da177e4SLinus Torvalds goto failure; 1847dc00c82SWang Chen dev_hold(dev); 1851da177e4SLinus Torvalds } 1861da177e4SLinus Torvalds } 1871da177e4SLinus Torvalds return dev; 1881da177e4SLinus Torvalds 1891da177e4SLinus Torvalds failure: 1901da177e4SLinus Torvalds /* allow the register to be completed before unregistering. */ 1911da177e4SLinus Torvalds rtnl_unlock(); 1921da177e4SLinus Torvalds rtnl_lock(); 1931da177e4SLinus Torvalds 1941da177e4SLinus Torvalds unregister_netdevice(dev); 1951da177e4SLinus Torvalds return NULL; 1961da177e4SLinus Torvalds } 1971da177e4SLinus Torvalds 1981da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM 1991da177e4SLinus Torvalds 2006fef4c0cSStephen Hemminger static netdev_tx_t reg_vif_xmit(struct sk_buff *skb, struct net_device *dev) 2011da177e4SLinus Torvalds { 2024feb88e5SBenjamin Thery struct net *net = dev_net(dev); 2034feb88e5SBenjamin Thery 2041da177e4SLinus Torvalds read_lock(&mrt_lock); 205cf3677aeSPavel Emelyanov dev->stats.tx_bytes += skb->len; 206cf3677aeSPavel Emelyanov dev->stats.tx_packets++; 2074feb88e5SBenjamin Thery ipmr_cache_report(net, skb, net->ipv4.mroute_reg_vif_num, 2084feb88e5SBenjamin Thery IGMPMSG_WHOLEPKT); 2091da177e4SLinus Torvalds read_unlock(&mrt_lock); 2101da177e4SLinus Torvalds kfree_skb(skb); 2116ed10654SPatrick McHardy return NETDEV_TX_OK; 2121da177e4SLinus Torvalds } 2131da177e4SLinus Torvalds 214007c3838SStephen Hemminger static const struct net_device_ops reg_vif_netdev_ops = { 215007c3838SStephen Hemminger .ndo_start_xmit = reg_vif_xmit, 216007c3838SStephen Hemminger }; 217007c3838SStephen Hemminger 2181da177e4SLinus Torvalds static void reg_vif_setup(struct net_device *dev) 2191da177e4SLinus Torvalds { 2201da177e4SLinus Torvalds dev->type = ARPHRD_PIMREG; 22146f25dffSKris Katterjohn dev->mtu = ETH_DATA_LEN - sizeof(struct iphdr) - 8; 2221da177e4SLinus Torvalds dev->flags = IFF_NOARP; 223007c3838SStephen Hemminger dev->netdev_ops = ®_vif_netdev_ops, 2241da177e4SLinus Torvalds dev->destructor = free_netdev; 225403dbb97STom Goff dev->features |= NETIF_F_NETNS_LOCAL; 2261da177e4SLinus Torvalds } 2271da177e4SLinus Torvalds 228403dbb97STom Goff static struct net_device *ipmr_reg_vif(struct net *net) 2291da177e4SLinus Torvalds { 2301da177e4SLinus Torvalds struct net_device *dev; 2311da177e4SLinus Torvalds struct in_device *in_dev; 2321da177e4SLinus Torvalds 233cf3677aeSPavel Emelyanov dev = alloc_netdev(0, "pimreg", reg_vif_setup); 2341da177e4SLinus Torvalds 2351da177e4SLinus Torvalds if (dev == NULL) 2361da177e4SLinus Torvalds return NULL; 2371da177e4SLinus Torvalds 238403dbb97STom Goff dev_net_set(dev, net); 239403dbb97STom Goff 2401da177e4SLinus Torvalds if (register_netdevice(dev)) { 2411da177e4SLinus Torvalds free_netdev(dev); 2421da177e4SLinus Torvalds return NULL; 2431da177e4SLinus Torvalds } 2441da177e4SLinus Torvalds dev->iflink = 0; 2451da177e4SLinus Torvalds 24671e27da9SHerbert Xu rcu_read_lock(); 24771e27da9SHerbert Xu if ((in_dev = __in_dev_get_rcu(dev)) == NULL) { 24871e27da9SHerbert Xu rcu_read_unlock(); 2491da177e4SLinus Torvalds goto failure; 25071e27da9SHerbert Xu } 2511da177e4SLinus Torvalds 25271e27da9SHerbert Xu ipv4_devconf_setall(in_dev); 25371e27da9SHerbert Xu IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0; 25471e27da9SHerbert Xu rcu_read_unlock(); 2551da177e4SLinus Torvalds 2561da177e4SLinus Torvalds if (dev_open(dev)) 2571da177e4SLinus Torvalds goto failure; 2581da177e4SLinus Torvalds 2597dc00c82SWang Chen dev_hold(dev); 2607dc00c82SWang Chen 2611da177e4SLinus Torvalds return dev; 2621da177e4SLinus Torvalds 2631da177e4SLinus Torvalds failure: 2641da177e4SLinus Torvalds /* allow the register to be completed before unregistering. */ 2651da177e4SLinus Torvalds rtnl_unlock(); 2661da177e4SLinus Torvalds rtnl_lock(); 2671da177e4SLinus Torvalds 2681da177e4SLinus Torvalds unregister_netdevice(dev); 2691da177e4SLinus Torvalds return NULL; 2701da177e4SLinus Torvalds } 2711da177e4SLinus Torvalds #endif 2721da177e4SLinus Torvalds 2731da177e4SLinus Torvalds /* 2741da177e4SLinus Torvalds * Delete a VIF entry 2757dc00c82SWang Chen * @notify: Set to 1, if the caller is a notifier_call 2761da177e4SLinus Torvalds */ 2771da177e4SLinus Torvalds 278d17fa6faSEric Dumazet static int vif_delete(struct net *net, int vifi, int notify, 279d17fa6faSEric Dumazet struct list_head *head) 2801da177e4SLinus Torvalds { 2811da177e4SLinus Torvalds struct vif_device *v; 2821da177e4SLinus Torvalds struct net_device *dev; 2831da177e4SLinus Torvalds struct in_device *in_dev; 2841da177e4SLinus Torvalds 2854feb88e5SBenjamin Thery if (vifi < 0 || vifi >= net->ipv4.maxvif) 2861da177e4SLinus Torvalds return -EADDRNOTAVAIL; 2871da177e4SLinus Torvalds 2884feb88e5SBenjamin Thery v = &net->ipv4.vif_table[vifi]; 2891da177e4SLinus Torvalds 2901da177e4SLinus Torvalds write_lock_bh(&mrt_lock); 2911da177e4SLinus Torvalds dev = v->dev; 2921da177e4SLinus Torvalds v->dev = NULL; 2931da177e4SLinus Torvalds 2941da177e4SLinus Torvalds if (!dev) { 2951da177e4SLinus Torvalds write_unlock_bh(&mrt_lock); 2961da177e4SLinus Torvalds return -EADDRNOTAVAIL; 2971da177e4SLinus Torvalds } 2981da177e4SLinus Torvalds 2991da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM 3004feb88e5SBenjamin Thery if (vifi == net->ipv4.mroute_reg_vif_num) 3014feb88e5SBenjamin Thery net->ipv4.mroute_reg_vif_num = -1; 3021da177e4SLinus Torvalds #endif 3031da177e4SLinus Torvalds 3044feb88e5SBenjamin Thery if (vifi+1 == net->ipv4.maxvif) { 3051da177e4SLinus Torvalds int tmp; 3061da177e4SLinus Torvalds for (tmp=vifi-1; tmp>=0; tmp--) { 3074feb88e5SBenjamin Thery if (VIF_EXISTS(net, tmp)) 3081da177e4SLinus Torvalds break; 3091da177e4SLinus Torvalds } 3104feb88e5SBenjamin Thery net->ipv4.maxvif = tmp+1; 3111da177e4SLinus Torvalds } 3121da177e4SLinus Torvalds 3131da177e4SLinus Torvalds write_unlock_bh(&mrt_lock); 3141da177e4SLinus Torvalds 3151da177e4SLinus Torvalds dev_set_allmulti(dev, -1); 3161da177e4SLinus Torvalds 317e5ed6399SHerbert Xu if ((in_dev = __in_dev_get_rtnl(dev)) != NULL) { 31842f811b8SHerbert Xu IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)--; 3191da177e4SLinus Torvalds ip_rt_multicast_event(in_dev); 3201da177e4SLinus Torvalds } 3211da177e4SLinus Torvalds 3227dc00c82SWang Chen if (v->flags&(VIFF_TUNNEL|VIFF_REGISTER) && !notify) 323d17fa6faSEric Dumazet unregister_netdevice_queue(dev, head); 3241da177e4SLinus Torvalds 3251da177e4SLinus Torvalds dev_put(dev); 3261da177e4SLinus Torvalds return 0; 3271da177e4SLinus Torvalds } 3281da177e4SLinus Torvalds 3295c0a66f5SBenjamin Thery static inline void ipmr_cache_free(struct mfc_cache *c) 3305c0a66f5SBenjamin Thery { 3315c0a66f5SBenjamin Thery release_net(mfc_net(c)); 3325c0a66f5SBenjamin Thery kmem_cache_free(mrt_cachep, c); 3335c0a66f5SBenjamin Thery } 3345c0a66f5SBenjamin Thery 3351da177e4SLinus Torvalds /* Destroy an unresolved cache entry, killing queued skbs 3361da177e4SLinus Torvalds and reporting error to netlink readers. 3371da177e4SLinus Torvalds */ 3381da177e4SLinus Torvalds 3391da177e4SLinus Torvalds static void ipmr_destroy_unres(struct mfc_cache *c) 3401da177e4SLinus Torvalds { 3411da177e4SLinus Torvalds struct sk_buff *skb; 3429ef1d4c7SPatrick McHardy struct nlmsgerr *e; 3434feb88e5SBenjamin Thery struct net *net = mfc_net(c); 3441da177e4SLinus Torvalds 3454feb88e5SBenjamin Thery atomic_dec(&net->ipv4.cache_resolve_queue_len); 3461da177e4SLinus Torvalds 3471da177e4SLinus Torvalds while ((skb = skb_dequeue(&c->mfc_un.unres.unresolved))) { 348eddc9ec5SArnaldo Carvalho de Melo if (ip_hdr(skb)->version == 0) { 3491da177e4SLinus Torvalds struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr)); 3501da177e4SLinus Torvalds nlh->nlmsg_type = NLMSG_ERROR; 3511da177e4SLinus Torvalds nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct nlmsgerr)); 3521da177e4SLinus Torvalds skb_trim(skb, nlh->nlmsg_len); 3539ef1d4c7SPatrick McHardy e = NLMSG_DATA(nlh); 3549ef1d4c7SPatrick McHardy e->error = -ETIMEDOUT; 3559ef1d4c7SPatrick McHardy memset(&e->msg, 0, sizeof(e->msg)); 3562942e900SThomas Graf 3574feb88e5SBenjamin Thery rtnl_unicast(skb, net, NETLINK_CB(skb).pid); 3581da177e4SLinus Torvalds } else 3591da177e4SLinus Torvalds kfree_skb(skb); 3601da177e4SLinus Torvalds } 3611da177e4SLinus Torvalds 3625c0a66f5SBenjamin Thery ipmr_cache_free(c); 3631da177e4SLinus Torvalds } 3641da177e4SLinus Torvalds 3651da177e4SLinus Torvalds 3661da177e4SLinus Torvalds /* Single timer process for all the unresolved queue. */ 3671da177e4SLinus Torvalds 3681da177e4SLinus Torvalds static void ipmr_expire_process(unsigned long dummy) 3691da177e4SLinus Torvalds { 3701da177e4SLinus Torvalds unsigned long now; 3711da177e4SLinus Torvalds unsigned long expires; 3721da177e4SLinus Torvalds struct mfc_cache *c, **cp; 3731da177e4SLinus Torvalds 3741da177e4SLinus Torvalds if (!spin_trylock(&mfc_unres_lock)) { 3751da177e4SLinus Torvalds mod_timer(&ipmr_expire_timer, jiffies+HZ/10); 3761da177e4SLinus Torvalds return; 3771da177e4SLinus Torvalds } 3781da177e4SLinus Torvalds 3791e8fb3b6SBenjamin Thery if (mfc_unres_queue == NULL) 3801da177e4SLinus Torvalds goto out; 3811da177e4SLinus Torvalds 3821da177e4SLinus Torvalds now = jiffies; 3831da177e4SLinus Torvalds expires = 10*HZ; 3841da177e4SLinus Torvalds cp = &mfc_unres_queue; 3851da177e4SLinus Torvalds 3861da177e4SLinus Torvalds while ((c=*cp) != NULL) { 3871da177e4SLinus Torvalds if (time_after(c->mfc_un.unres.expires, now)) { 3881da177e4SLinus Torvalds unsigned long interval = c->mfc_un.unres.expires - now; 3891da177e4SLinus Torvalds if (interval < expires) 3901da177e4SLinus Torvalds expires = interval; 3911da177e4SLinus Torvalds cp = &c->next; 3921da177e4SLinus Torvalds continue; 3931da177e4SLinus Torvalds } 3941da177e4SLinus Torvalds 3951da177e4SLinus Torvalds *cp = c->next; 3961da177e4SLinus Torvalds 3971da177e4SLinus Torvalds ipmr_destroy_unres(c); 3981da177e4SLinus Torvalds } 3991da177e4SLinus Torvalds 4001e8fb3b6SBenjamin Thery if (mfc_unres_queue != NULL) 4011da177e4SLinus Torvalds mod_timer(&ipmr_expire_timer, jiffies + expires); 4021da177e4SLinus Torvalds 4031da177e4SLinus Torvalds out: 4041da177e4SLinus Torvalds spin_unlock(&mfc_unres_lock); 4051da177e4SLinus Torvalds } 4061da177e4SLinus Torvalds 4071da177e4SLinus Torvalds /* Fill oifs list. It is called under write locked mrt_lock. */ 4081da177e4SLinus Torvalds 409d1b04c08SBaruch Even static void ipmr_update_thresholds(struct mfc_cache *cache, unsigned char *ttls) 4101da177e4SLinus Torvalds { 4111da177e4SLinus Torvalds int vifi; 4124feb88e5SBenjamin Thery struct net *net = mfc_net(cache); 4131da177e4SLinus Torvalds 4141da177e4SLinus Torvalds cache->mfc_un.res.minvif = MAXVIFS; 4151da177e4SLinus Torvalds cache->mfc_un.res.maxvif = 0; 4161da177e4SLinus Torvalds memset(cache->mfc_un.res.ttls, 255, MAXVIFS); 4171da177e4SLinus Torvalds 4184feb88e5SBenjamin Thery for (vifi = 0; vifi < net->ipv4.maxvif; vifi++) { 4194feb88e5SBenjamin Thery if (VIF_EXISTS(net, vifi) && 420cf958ae3SBenjamin Thery ttls[vifi] && ttls[vifi] < 255) { 4211da177e4SLinus Torvalds cache->mfc_un.res.ttls[vifi] = ttls[vifi]; 4221da177e4SLinus Torvalds if (cache->mfc_un.res.minvif > vifi) 4231da177e4SLinus Torvalds cache->mfc_un.res.minvif = vifi; 4241da177e4SLinus Torvalds if (cache->mfc_un.res.maxvif <= vifi) 4251da177e4SLinus Torvalds cache->mfc_un.res.maxvif = vifi + 1; 4261da177e4SLinus Torvalds } 4271da177e4SLinus Torvalds } 4281da177e4SLinus Torvalds } 4291da177e4SLinus Torvalds 4304feb88e5SBenjamin Thery static int vif_add(struct net *net, struct vifctl *vifc, int mrtsock) 4311da177e4SLinus Torvalds { 4321da177e4SLinus Torvalds int vifi = vifc->vifc_vifi; 4334feb88e5SBenjamin Thery struct vif_device *v = &net->ipv4.vif_table[vifi]; 4341da177e4SLinus Torvalds struct net_device *dev; 4351da177e4SLinus Torvalds struct in_device *in_dev; 436d607032dSWang Chen int err; 4371da177e4SLinus Torvalds 4381da177e4SLinus Torvalds /* Is vif busy ? */ 4394feb88e5SBenjamin Thery if (VIF_EXISTS(net, vifi)) 4401da177e4SLinus Torvalds return -EADDRINUSE; 4411da177e4SLinus Torvalds 4421da177e4SLinus Torvalds switch (vifc->vifc_flags) { 4431da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM 4441da177e4SLinus Torvalds case VIFF_REGISTER: 4451da177e4SLinus Torvalds /* 4461da177e4SLinus Torvalds * Special Purpose VIF in PIM 4471da177e4SLinus Torvalds * All the packets will be sent to the daemon 4481da177e4SLinus Torvalds */ 4494feb88e5SBenjamin Thery if (net->ipv4.mroute_reg_vif_num >= 0) 4501da177e4SLinus Torvalds return -EADDRINUSE; 451403dbb97STom Goff dev = ipmr_reg_vif(net); 4521da177e4SLinus Torvalds if (!dev) 4531da177e4SLinus Torvalds return -ENOBUFS; 454d607032dSWang Chen err = dev_set_allmulti(dev, 1); 455d607032dSWang Chen if (err) { 456d607032dSWang Chen unregister_netdevice(dev); 4577dc00c82SWang Chen dev_put(dev); 458d607032dSWang Chen return err; 459d607032dSWang Chen } 4601da177e4SLinus Torvalds break; 4611da177e4SLinus Torvalds #endif 4621da177e4SLinus Torvalds case VIFF_TUNNEL: 4634feb88e5SBenjamin Thery dev = ipmr_new_tunnel(net, vifc); 4641da177e4SLinus Torvalds if (!dev) 4651da177e4SLinus Torvalds return -ENOBUFS; 466d607032dSWang Chen err = dev_set_allmulti(dev, 1); 467d607032dSWang Chen if (err) { 468d607032dSWang Chen ipmr_del_tunnel(dev, vifc); 4697dc00c82SWang Chen dev_put(dev); 470d607032dSWang Chen return err; 471d607032dSWang Chen } 4721da177e4SLinus Torvalds break; 473ee5e81f0SIlia K 474ee5e81f0SIlia K case VIFF_USE_IFINDEX: 4751da177e4SLinus Torvalds case 0: 476ee5e81f0SIlia K if (vifc->vifc_flags == VIFF_USE_IFINDEX) { 477ee5e81f0SIlia K dev = dev_get_by_index(net, vifc->vifc_lcl_ifindex); 478ee5e81f0SIlia K if (dev && dev->ip_ptr == NULL) { 479ee5e81f0SIlia K dev_put(dev); 480ee5e81f0SIlia K return -EADDRNOTAVAIL; 481ee5e81f0SIlia K } 482ee5e81f0SIlia K } else 4834feb88e5SBenjamin Thery dev = ip_dev_find(net, vifc->vifc_lcl_addr.s_addr); 484ee5e81f0SIlia K 4851da177e4SLinus Torvalds if (!dev) 4861da177e4SLinus Torvalds return -EADDRNOTAVAIL; 487d607032dSWang Chen err = dev_set_allmulti(dev, 1); 4887dc00c82SWang Chen if (err) { 4897dc00c82SWang Chen dev_put(dev); 490d607032dSWang Chen return err; 4917dc00c82SWang Chen } 4921da177e4SLinus Torvalds break; 4931da177e4SLinus Torvalds default: 4941da177e4SLinus Torvalds return -EINVAL; 4951da177e4SLinus Torvalds } 4961da177e4SLinus Torvalds 497d0490cfdSDan Carpenter if ((in_dev = __in_dev_get_rtnl(dev)) == NULL) { 498d0490cfdSDan Carpenter dev_put(dev); 4991da177e4SLinus Torvalds return -EADDRNOTAVAIL; 500d0490cfdSDan Carpenter } 50142f811b8SHerbert Xu IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)++; 5021da177e4SLinus Torvalds ip_rt_multicast_event(in_dev); 5031da177e4SLinus Torvalds 5041da177e4SLinus Torvalds /* 5051da177e4SLinus Torvalds * Fill in the VIF structures 5061da177e4SLinus Torvalds */ 5071da177e4SLinus Torvalds v->rate_limit = vifc->vifc_rate_limit; 5081da177e4SLinus Torvalds v->local = vifc->vifc_lcl_addr.s_addr; 5091da177e4SLinus Torvalds v->remote = vifc->vifc_rmt_addr.s_addr; 5101da177e4SLinus Torvalds v->flags = vifc->vifc_flags; 5111da177e4SLinus Torvalds if (!mrtsock) 5121da177e4SLinus Torvalds v->flags |= VIFF_STATIC; 5131da177e4SLinus Torvalds v->threshold = vifc->vifc_threshold; 5141da177e4SLinus Torvalds v->bytes_in = 0; 5151da177e4SLinus Torvalds v->bytes_out = 0; 5161da177e4SLinus Torvalds v->pkt_in = 0; 5171da177e4SLinus Torvalds v->pkt_out = 0; 5181da177e4SLinus Torvalds v->link = dev->ifindex; 5191da177e4SLinus Torvalds if (v->flags&(VIFF_TUNNEL|VIFF_REGISTER)) 5201da177e4SLinus Torvalds v->link = dev->iflink; 5211da177e4SLinus Torvalds 5221da177e4SLinus Torvalds /* And finish update writing critical data */ 5231da177e4SLinus Torvalds write_lock_bh(&mrt_lock); 5241da177e4SLinus Torvalds v->dev = dev; 5251da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM 5261da177e4SLinus Torvalds if (v->flags&VIFF_REGISTER) 5274feb88e5SBenjamin Thery net->ipv4.mroute_reg_vif_num = vifi; 5281da177e4SLinus Torvalds #endif 5294feb88e5SBenjamin Thery if (vifi+1 > net->ipv4.maxvif) 5304feb88e5SBenjamin Thery net->ipv4.maxvif = vifi+1; 5311da177e4SLinus Torvalds write_unlock_bh(&mrt_lock); 5321da177e4SLinus Torvalds return 0; 5331da177e4SLinus Torvalds } 5341da177e4SLinus Torvalds 5354feb88e5SBenjamin Thery static struct mfc_cache *ipmr_cache_find(struct net *net, 5364feb88e5SBenjamin Thery __be32 origin, 5374feb88e5SBenjamin Thery __be32 mcastgrp) 5381da177e4SLinus Torvalds { 5391da177e4SLinus Torvalds int line = MFC_HASH(mcastgrp, origin); 5401da177e4SLinus Torvalds struct mfc_cache *c; 5411da177e4SLinus Torvalds 5424feb88e5SBenjamin Thery for (c = net->ipv4.mfc_cache_array[line]; c; c = c->next) { 5431da177e4SLinus Torvalds if (c->mfc_origin==origin && c->mfc_mcastgrp==mcastgrp) 5441da177e4SLinus Torvalds break; 5451da177e4SLinus Torvalds } 5461da177e4SLinus Torvalds return c; 5471da177e4SLinus Torvalds } 5481da177e4SLinus Torvalds 5491da177e4SLinus Torvalds /* 5501da177e4SLinus Torvalds * Allocate a multicast cache entry 5511da177e4SLinus Torvalds */ 5525c0a66f5SBenjamin Thery static struct mfc_cache *ipmr_cache_alloc(struct net *net) 5531da177e4SLinus Torvalds { 554c3762229SRobert P. J. Day struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_KERNEL); 5551da177e4SLinus Torvalds if (c == NULL) 5561da177e4SLinus Torvalds return NULL; 5571da177e4SLinus Torvalds c->mfc_un.res.minvif = MAXVIFS; 5585c0a66f5SBenjamin Thery mfc_net_set(c, net); 5591da177e4SLinus Torvalds return c; 5601da177e4SLinus Torvalds } 5611da177e4SLinus Torvalds 5625c0a66f5SBenjamin Thery static struct mfc_cache *ipmr_cache_alloc_unres(struct net *net) 5631da177e4SLinus Torvalds { 564c3762229SRobert P. J. Day struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_ATOMIC); 5651da177e4SLinus Torvalds if (c == NULL) 5661da177e4SLinus Torvalds return NULL; 5671da177e4SLinus Torvalds skb_queue_head_init(&c->mfc_un.unres.unresolved); 5681da177e4SLinus Torvalds c->mfc_un.unres.expires = jiffies + 10*HZ; 5695c0a66f5SBenjamin Thery mfc_net_set(c, net); 5701da177e4SLinus Torvalds return c; 5711da177e4SLinus Torvalds } 5721da177e4SLinus Torvalds 5731da177e4SLinus Torvalds /* 5741da177e4SLinus Torvalds * A cache entry has gone into a resolved state from queued 5751da177e4SLinus Torvalds */ 5761da177e4SLinus Torvalds 5771da177e4SLinus Torvalds static void ipmr_cache_resolve(struct mfc_cache *uc, struct mfc_cache *c) 5781da177e4SLinus Torvalds { 5791da177e4SLinus Torvalds struct sk_buff *skb; 5809ef1d4c7SPatrick McHardy struct nlmsgerr *e; 5811da177e4SLinus Torvalds 5821da177e4SLinus Torvalds /* 5831da177e4SLinus Torvalds * Play the pending entries through our router 5841da177e4SLinus Torvalds */ 5851da177e4SLinus Torvalds 5861da177e4SLinus Torvalds while ((skb = __skb_dequeue(&uc->mfc_un.unres.unresolved))) { 587eddc9ec5SArnaldo Carvalho de Melo if (ip_hdr(skb)->version == 0) { 5881da177e4SLinus Torvalds struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr)); 5891da177e4SLinus Torvalds 5901da177e4SLinus Torvalds if (ipmr_fill_mroute(skb, c, NLMSG_DATA(nlh)) > 0) { 59127a884dcSArnaldo Carvalho de Melo nlh->nlmsg_len = (skb_tail_pointer(skb) - 59227a884dcSArnaldo Carvalho de Melo (u8 *)nlh); 5931da177e4SLinus Torvalds } else { 5941da177e4SLinus Torvalds nlh->nlmsg_type = NLMSG_ERROR; 5951da177e4SLinus Torvalds nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct nlmsgerr)); 5961da177e4SLinus Torvalds skb_trim(skb, nlh->nlmsg_len); 5979ef1d4c7SPatrick McHardy e = NLMSG_DATA(nlh); 5989ef1d4c7SPatrick McHardy e->error = -EMSGSIZE; 5999ef1d4c7SPatrick McHardy memset(&e->msg, 0, sizeof(e->msg)); 6001da177e4SLinus Torvalds } 6012942e900SThomas Graf 6024feb88e5SBenjamin Thery rtnl_unicast(skb, mfc_net(c), NETLINK_CB(skb).pid); 6031da177e4SLinus Torvalds } else 6041da177e4SLinus Torvalds ip_mr_forward(skb, c, 0); 6051da177e4SLinus Torvalds } 6061da177e4SLinus Torvalds } 6071da177e4SLinus Torvalds 6081da177e4SLinus Torvalds /* 6091da177e4SLinus Torvalds * Bounce a cache query up to mrouted. We could use netlink for this but mrouted 6101da177e4SLinus Torvalds * expects the following bizarre scheme. 6111da177e4SLinus Torvalds * 6121da177e4SLinus Torvalds * Called under mrt_lock. 6131da177e4SLinus Torvalds */ 6141da177e4SLinus Torvalds 6154feb88e5SBenjamin Thery static int ipmr_cache_report(struct net *net, 6164feb88e5SBenjamin Thery struct sk_buff *pkt, vifi_t vifi, int assert) 6171da177e4SLinus Torvalds { 6181da177e4SLinus Torvalds struct sk_buff *skb; 619c9bdd4b5SArnaldo Carvalho de Melo const int ihl = ip_hdrlen(pkt); 6201da177e4SLinus Torvalds struct igmphdr *igmp; 6211da177e4SLinus Torvalds struct igmpmsg *msg; 6221da177e4SLinus Torvalds int ret; 6231da177e4SLinus Torvalds 6241da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM 6251da177e4SLinus Torvalds if (assert == IGMPMSG_WHOLEPKT) 6261da177e4SLinus Torvalds skb = skb_realloc_headroom(pkt, sizeof(struct iphdr)); 6271da177e4SLinus Torvalds else 6281da177e4SLinus Torvalds #endif 6291da177e4SLinus Torvalds skb = alloc_skb(128, GFP_ATOMIC); 6301da177e4SLinus Torvalds 6311da177e4SLinus Torvalds if (!skb) 6321da177e4SLinus Torvalds return -ENOBUFS; 6331da177e4SLinus Torvalds 6341da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM 6351da177e4SLinus Torvalds if (assert == IGMPMSG_WHOLEPKT) { 6361da177e4SLinus Torvalds /* Ugly, but we have no choice with this interface. 6371da177e4SLinus Torvalds Duplicate old header, fix ihl, length etc. 6381da177e4SLinus Torvalds And all this only to mangle msg->im_msgtype and 6391da177e4SLinus Torvalds to set msg->im_mbz to "mbz" :-) 6401da177e4SLinus Torvalds */ 641878c8145SArnaldo Carvalho de Melo skb_push(skb, sizeof(struct iphdr)); 642878c8145SArnaldo Carvalho de Melo skb_reset_network_header(skb); 643badff6d0SArnaldo Carvalho de Melo skb_reset_transport_header(skb); 6440272ffc4SArnaldo Carvalho de Melo msg = (struct igmpmsg *)skb_network_header(skb); 645d56f90a7SArnaldo Carvalho de Melo memcpy(msg, skb_network_header(pkt), sizeof(struct iphdr)); 6461da177e4SLinus Torvalds msg->im_msgtype = IGMPMSG_WHOLEPKT; 6471da177e4SLinus Torvalds msg->im_mbz = 0; 6484feb88e5SBenjamin Thery msg->im_vif = net->ipv4.mroute_reg_vif_num; 649eddc9ec5SArnaldo Carvalho de Melo ip_hdr(skb)->ihl = sizeof(struct iphdr) >> 2; 650eddc9ec5SArnaldo Carvalho de Melo ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(pkt)->tot_len) + 651eddc9ec5SArnaldo Carvalho de Melo sizeof(struct iphdr)); 6521da177e4SLinus Torvalds } else 6531da177e4SLinus Torvalds #endif 6541da177e4SLinus Torvalds { 6551da177e4SLinus Torvalds 6561da177e4SLinus Torvalds /* 6571da177e4SLinus Torvalds * Copy the IP header 6581da177e4SLinus Torvalds */ 6591da177e4SLinus Torvalds 66027a884dcSArnaldo Carvalho de Melo skb->network_header = skb->tail; 661ddc7b8e3SArnaldo Carvalho de Melo skb_put(skb, ihl); 66227d7ff46SArnaldo Carvalho de Melo skb_copy_to_linear_data(skb, pkt->data, ihl); 663eddc9ec5SArnaldo Carvalho de Melo ip_hdr(skb)->protocol = 0; /* Flag to the kernel this is a route add */ 664eddc9ec5SArnaldo Carvalho de Melo msg = (struct igmpmsg *)skb_network_header(skb); 6651da177e4SLinus Torvalds msg->im_vif = vifi; 666adf30907SEric Dumazet skb_dst_set(skb, dst_clone(skb_dst(pkt))); 6671da177e4SLinus Torvalds 6681da177e4SLinus Torvalds /* 6691da177e4SLinus Torvalds * Add our header 6701da177e4SLinus Torvalds */ 6711da177e4SLinus Torvalds 6721da177e4SLinus Torvalds igmp=(struct igmphdr *)skb_put(skb, sizeof(struct igmphdr)); 6731da177e4SLinus Torvalds igmp->type = 6741da177e4SLinus Torvalds msg->im_msgtype = assert; 6751da177e4SLinus Torvalds igmp->code = 0; 676eddc9ec5SArnaldo Carvalho de Melo ip_hdr(skb)->tot_len = htons(skb->len); /* Fix the length */ 677b0e380b1SArnaldo Carvalho de Melo skb->transport_header = skb->network_header; 6781da177e4SLinus Torvalds } 6791da177e4SLinus Torvalds 6804feb88e5SBenjamin Thery if (net->ipv4.mroute_sk == NULL) { 6811da177e4SLinus Torvalds kfree_skb(skb); 6821da177e4SLinus Torvalds return -EINVAL; 6831da177e4SLinus Torvalds } 6841da177e4SLinus Torvalds 6851da177e4SLinus Torvalds /* 6861da177e4SLinus Torvalds * Deliver to mrouted 6871da177e4SLinus Torvalds */ 6884feb88e5SBenjamin Thery ret = sock_queue_rcv_skb(net->ipv4.mroute_sk, skb); 68970a269e6SBenjamin Thery if (ret < 0) { 6901da177e4SLinus Torvalds if (net_ratelimit()) 6911da177e4SLinus Torvalds printk(KERN_WARNING "mroute: pending queue full, dropping entries.\n"); 6921da177e4SLinus Torvalds kfree_skb(skb); 6931da177e4SLinus Torvalds } 6941da177e4SLinus Torvalds 6951da177e4SLinus Torvalds return ret; 6961da177e4SLinus Torvalds } 6971da177e4SLinus Torvalds 6981da177e4SLinus Torvalds /* 6991da177e4SLinus Torvalds * Queue a packet for resolution. It gets locked cache entry! 7001da177e4SLinus Torvalds */ 7011da177e4SLinus Torvalds 7021da177e4SLinus Torvalds static int 7034feb88e5SBenjamin Thery ipmr_cache_unresolved(struct net *net, vifi_t vifi, struct sk_buff *skb) 7041da177e4SLinus Torvalds { 7051da177e4SLinus Torvalds int err; 7061da177e4SLinus Torvalds struct mfc_cache *c; 707eddc9ec5SArnaldo Carvalho de Melo const struct iphdr *iph = ip_hdr(skb); 7081da177e4SLinus Torvalds 7091da177e4SLinus Torvalds spin_lock_bh(&mfc_unres_lock); 7101da177e4SLinus Torvalds for (c=mfc_unres_queue; c; c=c->next) { 7114feb88e5SBenjamin Thery if (net_eq(mfc_net(c), net) && 7121e8fb3b6SBenjamin Thery c->mfc_mcastgrp == iph->daddr && 713eddc9ec5SArnaldo Carvalho de Melo c->mfc_origin == iph->saddr) 7141da177e4SLinus Torvalds break; 7151da177e4SLinus Torvalds } 7161da177e4SLinus Torvalds 7171da177e4SLinus Torvalds if (c == NULL) { 7181da177e4SLinus Torvalds /* 7191da177e4SLinus Torvalds * Create a new entry if allowable 7201da177e4SLinus Torvalds */ 7211da177e4SLinus Torvalds 7224feb88e5SBenjamin Thery if (atomic_read(&net->ipv4.cache_resolve_queue_len) >= 10 || 7234feb88e5SBenjamin Thery (c = ipmr_cache_alloc_unres(net)) == NULL) { 7241da177e4SLinus Torvalds spin_unlock_bh(&mfc_unres_lock); 7251da177e4SLinus Torvalds 7261da177e4SLinus Torvalds kfree_skb(skb); 7271da177e4SLinus Torvalds return -ENOBUFS; 7281da177e4SLinus Torvalds } 7291da177e4SLinus Torvalds 7301da177e4SLinus Torvalds /* 7311da177e4SLinus Torvalds * Fill in the new cache entry 7321da177e4SLinus Torvalds */ 7331da177e4SLinus Torvalds c->mfc_parent = -1; 734eddc9ec5SArnaldo Carvalho de Melo c->mfc_origin = iph->saddr; 735eddc9ec5SArnaldo Carvalho de Melo c->mfc_mcastgrp = iph->daddr; 7361da177e4SLinus Torvalds 7371da177e4SLinus Torvalds /* 7381da177e4SLinus Torvalds * Reflect first query at mrouted. 7391da177e4SLinus Torvalds */ 7404feb88e5SBenjamin Thery err = ipmr_cache_report(net, skb, vifi, IGMPMSG_NOCACHE); 7414feb88e5SBenjamin Thery if (err < 0) { 7421da177e4SLinus Torvalds /* If the report failed throw the cache entry 7431da177e4SLinus Torvalds out - Brad Parker 7441da177e4SLinus Torvalds */ 7451da177e4SLinus Torvalds spin_unlock_bh(&mfc_unres_lock); 7461da177e4SLinus Torvalds 7475c0a66f5SBenjamin Thery ipmr_cache_free(c); 7481da177e4SLinus Torvalds kfree_skb(skb); 7491da177e4SLinus Torvalds return err; 7501da177e4SLinus Torvalds } 7511da177e4SLinus Torvalds 7524feb88e5SBenjamin Thery atomic_inc(&net->ipv4.cache_resolve_queue_len); 7531da177e4SLinus Torvalds c->next = mfc_unres_queue; 7541da177e4SLinus Torvalds mfc_unres_queue = c; 7551da177e4SLinus Torvalds 7561da177e4SLinus Torvalds mod_timer(&ipmr_expire_timer, c->mfc_un.unres.expires); 7571da177e4SLinus Torvalds } 7581da177e4SLinus Torvalds 7591da177e4SLinus Torvalds /* 7601da177e4SLinus Torvalds * See if we can append the packet 7611da177e4SLinus Torvalds */ 7621da177e4SLinus Torvalds if (c->mfc_un.unres.unresolved.qlen>3) { 7631da177e4SLinus Torvalds kfree_skb(skb); 7641da177e4SLinus Torvalds err = -ENOBUFS; 7651da177e4SLinus Torvalds } else { 7661da177e4SLinus Torvalds skb_queue_tail(&c->mfc_un.unres.unresolved, skb); 7671da177e4SLinus Torvalds err = 0; 7681da177e4SLinus Torvalds } 7691da177e4SLinus Torvalds 7701da177e4SLinus Torvalds spin_unlock_bh(&mfc_unres_lock); 7711da177e4SLinus Torvalds return err; 7721da177e4SLinus Torvalds } 7731da177e4SLinus Torvalds 7741da177e4SLinus Torvalds /* 7751da177e4SLinus Torvalds * MFC cache manipulation by user space mroute daemon 7761da177e4SLinus Torvalds */ 7771da177e4SLinus Torvalds 7784feb88e5SBenjamin Thery static int ipmr_mfc_delete(struct net *net, struct mfcctl *mfc) 7791da177e4SLinus Torvalds { 7801da177e4SLinus Torvalds int line; 7811da177e4SLinus Torvalds struct mfc_cache *c, **cp; 7821da177e4SLinus Torvalds 7831da177e4SLinus Torvalds line = MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr); 7841da177e4SLinus Torvalds 7854feb88e5SBenjamin Thery for (cp = &net->ipv4.mfc_cache_array[line]; 7862bb8b26cSBenjamin Thery (c = *cp) != NULL; cp = &c->next) { 7871da177e4SLinus Torvalds if (c->mfc_origin == mfc->mfcc_origin.s_addr && 7881da177e4SLinus Torvalds c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr) { 7891da177e4SLinus Torvalds write_lock_bh(&mrt_lock); 7901da177e4SLinus Torvalds *cp = c->next; 7911da177e4SLinus Torvalds write_unlock_bh(&mrt_lock); 7921da177e4SLinus Torvalds 7935c0a66f5SBenjamin Thery ipmr_cache_free(c); 7941da177e4SLinus Torvalds return 0; 7951da177e4SLinus Torvalds } 7961da177e4SLinus Torvalds } 7971da177e4SLinus Torvalds return -ENOENT; 7981da177e4SLinus Torvalds } 7991da177e4SLinus Torvalds 8004feb88e5SBenjamin Thery static int ipmr_mfc_add(struct net *net, struct mfcctl *mfc, int mrtsock) 8011da177e4SLinus Torvalds { 8021da177e4SLinus Torvalds int line; 8031da177e4SLinus Torvalds struct mfc_cache *uc, *c, **cp; 8041da177e4SLinus Torvalds 805*a50436f2SPatrick McHardy if (mfc->mfcc_parent >= MAXVIFS) 806*a50436f2SPatrick McHardy return -ENFILE; 807*a50436f2SPatrick McHardy 8081da177e4SLinus Torvalds line = MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr); 8091da177e4SLinus Torvalds 8104feb88e5SBenjamin Thery for (cp = &net->ipv4.mfc_cache_array[line]; 8112bb8b26cSBenjamin Thery (c = *cp) != NULL; cp = &c->next) { 8121da177e4SLinus Torvalds if (c->mfc_origin == mfc->mfcc_origin.s_addr && 8131da177e4SLinus Torvalds c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr) 8141da177e4SLinus Torvalds break; 8151da177e4SLinus Torvalds } 8161da177e4SLinus Torvalds 8171da177e4SLinus Torvalds if (c != NULL) { 8181da177e4SLinus Torvalds write_lock_bh(&mrt_lock); 8191da177e4SLinus Torvalds c->mfc_parent = mfc->mfcc_parent; 820d1b04c08SBaruch Even ipmr_update_thresholds(c, mfc->mfcc_ttls); 8211da177e4SLinus Torvalds if (!mrtsock) 8221da177e4SLinus Torvalds c->mfc_flags |= MFC_STATIC; 8231da177e4SLinus Torvalds write_unlock_bh(&mrt_lock); 8241da177e4SLinus Torvalds return 0; 8251da177e4SLinus Torvalds } 8261da177e4SLinus Torvalds 827f97c1e0cSJoe Perches if (!ipv4_is_multicast(mfc->mfcc_mcastgrp.s_addr)) 8281da177e4SLinus Torvalds return -EINVAL; 8291da177e4SLinus Torvalds 8304feb88e5SBenjamin Thery c = ipmr_cache_alloc(net); 8311da177e4SLinus Torvalds if (c == NULL) 8321da177e4SLinus Torvalds return -ENOMEM; 8331da177e4SLinus Torvalds 8341da177e4SLinus Torvalds c->mfc_origin = mfc->mfcc_origin.s_addr; 8351da177e4SLinus Torvalds c->mfc_mcastgrp = mfc->mfcc_mcastgrp.s_addr; 8361da177e4SLinus Torvalds c->mfc_parent = mfc->mfcc_parent; 837d1b04c08SBaruch Even ipmr_update_thresholds(c, mfc->mfcc_ttls); 8381da177e4SLinus Torvalds if (!mrtsock) 8391da177e4SLinus Torvalds c->mfc_flags |= MFC_STATIC; 8401da177e4SLinus Torvalds 8411da177e4SLinus Torvalds write_lock_bh(&mrt_lock); 8424feb88e5SBenjamin Thery c->next = net->ipv4.mfc_cache_array[line]; 8434feb88e5SBenjamin Thery net->ipv4.mfc_cache_array[line] = c; 8441da177e4SLinus Torvalds write_unlock_bh(&mrt_lock); 8451da177e4SLinus Torvalds 8461da177e4SLinus Torvalds /* 8471da177e4SLinus Torvalds * Check to see if we resolved a queued list. If so we 8481da177e4SLinus Torvalds * need to send on the frames and tidy up. 8491da177e4SLinus Torvalds */ 8501da177e4SLinus Torvalds spin_lock_bh(&mfc_unres_lock); 8511da177e4SLinus Torvalds for (cp = &mfc_unres_queue; (uc=*cp) != NULL; 8521da177e4SLinus Torvalds cp = &uc->next) { 8534feb88e5SBenjamin Thery if (net_eq(mfc_net(uc), net) && 8541e8fb3b6SBenjamin Thery uc->mfc_origin == c->mfc_origin && 8551da177e4SLinus Torvalds uc->mfc_mcastgrp == c->mfc_mcastgrp) { 8561da177e4SLinus Torvalds *cp = uc->next; 8574feb88e5SBenjamin Thery atomic_dec(&net->ipv4.cache_resolve_queue_len); 8581da177e4SLinus Torvalds break; 8591da177e4SLinus Torvalds } 8601da177e4SLinus Torvalds } 8611e8fb3b6SBenjamin Thery if (mfc_unres_queue == NULL) 8621e8fb3b6SBenjamin Thery del_timer(&ipmr_expire_timer); 8631da177e4SLinus Torvalds spin_unlock_bh(&mfc_unres_lock); 8641da177e4SLinus Torvalds 8651da177e4SLinus Torvalds if (uc) { 8661da177e4SLinus Torvalds ipmr_cache_resolve(uc, c); 8675c0a66f5SBenjamin Thery ipmr_cache_free(uc); 8681da177e4SLinus Torvalds } 8691da177e4SLinus Torvalds return 0; 8701da177e4SLinus Torvalds } 8711da177e4SLinus Torvalds 8721da177e4SLinus Torvalds /* 8731da177e4SLinus Torvalds * Close the multicast socket, and clear the vif tables etc 8741da177e4SLinus Torvalds */ 8751da177e4SLinus Torvalds 8764feb88e5SBenjamin Thery static void mroute_clean_tables(struct net *net) 8771da177e4SLinus Torvalds { 8781da177e4SLinus Torvalds int i; 879d17fa6faSEric Dumazet LIST_HEAD(list); 8801da177e4SLinus Torvalds 8811da177e4SLinus Torvalds /* 8821da177e4SLinus Torvalds * Shut down all active vif entries 8831da177e4SLinus Torvalds */ 8844feb88e5SBenjamin Thery for (i = 0; i < net->ipv4.maxvif; i++) { 8854feb88e5SBenjamin Thery if (!(net->ipv4.vif_table[i].flags&VIFF_STATIC)) 886d17fa6faSEric Dumazet vif_delete(net, i, 0, &list); 8871da177e4SLinus Torvalds } 888d17fa6faSEric Dumazet unregister_netdevice_many(&list); 8891da177e4SLinus Torvalds 8901da177e4SLinus Torvalds /* 8911da177e4SLinus Torvalds * Wipe the cache 8921da177e4SLinus Torvalds */ 8931da177e4SLinus Torvalds for (i=0; i<MFC_LINES; i++) { 8941da177e4SLinus Torvalds struct mfc_cache *c, **cp; 8951da177e4SLinus Torvalds 8964feb88e5SBenjamin Thery cp = &net->ipv4.mfc_cache_array[i]; 8971da177e4SLinus Torvalds while ((c = *cp) != NULL) { 8981da177e4SLinus Torvalds if (c->mfc_flags&MFC_STATIC) { 8991da177e4SLinus Torvalds cp = &c->next; 9001da177e4SLinus Torvalds continue; 9011da177e4SLinus Torvalds } 9021da177e4SLinus Torvalds write_lock_bh(&mrt_lock); 9031da177e4SLinus Torvalds *cp = c->next; 9041da177e4SLinus Torvalds write_unlock_bh(&mrt_lock); 9051da177e4SLinus Torvalds 9065c0a66f5SBenjamin Thery ipmr_cache_free(c); 9071da177e4SLinus Torvalds } 9081da177e4SLinus Torvalds } 9091da177e4SLinus Torvalds 9104feb88e5SBenjamin Thery if (atomic_read(&net->ipv4.cache_resolve_queue_len) != 0) { 9111e8fb3b6SBenjamin Thery struct mfc_cache *c, **cp; 9121da177e4SLinus Torvalds 9131da177e4SLinus Torvalds spin_lock_bh(&mfc_unres_lock); 9141e8fb3b6SBenjamin Thery cp = &mfc_unres_queue; 9151e8fb3b6SBenjamin Thery while ((c = *cp) != NULL) { 9164feb88e5SBenjamin Thery if (!net_eq(mfc_net(c), net)) { 9171e8fb3b6SBenjamin Thery cp = &c->next; 9181e8fb3b6SBenjamin Thery continue; 9191e8fb3b6SBenjamin Thery } 9201e8fb3b6SBenjamin Thery *cp = c->next; 9211da177e4SLinus Torvalds 9221da177e4SLinus Torvalds ipmr_destroy_unres(c); 9231da177e4SLinus Torvalds } 9241da177e4SLinus Torvalds spin_unlock_bh(&mfc_unres_lock); 9251da177e4SLinus Torvalds } 9261da177e4SLinus Torvalds } 9271da177e4SLinus Torvalds 9281da177e4SLinus Torvalds static void mrtsock_destruct(struct sock *sk) 9291da177e4SLinus Torvalds { 9304feb88e5SBenjamin Thery struct net *net = sock_net(sk); 9314feb88e5SBenjamin Thery 9321da177e4SLinus Torvalds rtnl_lock(); 9334feb88e5SBenjamin Thery if (sk == net->ipv4.mroute_sk) { 9344feb88e5SBenjamin Thery IPV4_DEVCONF_ALL(net, MC_FORWARDING)--; 9351da177e4SLinus Torvalds 9361da177e4SLinus Torvalds write_lock_bh(&mrt_lock); 9374feb88e5SBenjamin Thery net->ipv4.mroute_sk = NULL; 9381da177e4SLinus Torvalds write_unlock_bh(&mrt_lock); 9391da177e4SLinus Torvalds 9404feb88e5SBenjamin Thery mroute_clean_tables(net); 9411da177e4SLinus Torvalds } 9421da177e4SLinus Torvalds rtnl_unlock(); 9431da177e4SLinus Torvalds } 9441da177e4SLinus Torvalds 9451da177e4SLinus Torvalds /* 9461da177e4SLinus Torvalds * Socket options and virtual interface manipulation. The whole 9471da177e4SLinus Torvalds * virtual interface system is a complete heap, but unfortunately 9481da177e4SLinus Torvalds * that's how BSD mrouted happens to think. Maybe one day with a proper 9491da177e4SLinus Torvalds * MOSPF/PIM router set up we can clean this up. 9501da177e4SLinus Torvalds */ 9511da177e4SLinus Torvalds 952b7058842SDavid S. Miller int ip_mroute_setsockopt(struct sock *sk, int optname, char __user *optval, unsigned int optlen) 9531da177e4SLinus Torvalds { 9541da177e4SLinus Torvalds int ret; 9551da177e4SLinus Torvalds struct vifctl vif; 9561da177e4SLinus Torvalds struct mfcctl mfc; 9574feb88e5SBenjamin Thery struct net *net = sock_net(sk); 9581da177e4SLinus Torvalds 959132adf54SStephen Hemminger if (optname != MRT_INIT) { 9604feb88e5SBenjamin Thery if (sk != net->ipv4.mroute_sk && !capable(CAP_NET_ADMIN)) 9611da177e4SLinus Torvalds return -EACCES; 9621da177e4SLinus Torvalds } 9631da177e4SLinus Torvalds 964132adf54SStephen Hemminger switch (optname) { 9651da177e4SLinus Torvalds case MRT_INIT: 9661da177e4SLinus Torvalds if (sk->sk_type != SOCK_RAW || 967c720c7e8SEric Dumazet inet_sk(sk)->inet_num != IPPROTO_IGMP) 9681da177e4SLinus Torvalds return -EOPNOTSUPP; 9691da177e4SLinus Torvalds if (optlen != sizeof(int)) 9701da177e4SLinus Torvalds return -ENOPROTOOPT; 9711da177e4SLinus Torvalds 9721da177e4SLinus Torvalds rtnl_lock(); 9734feb88e5SBenjamin Thery if (net->ipv4.mroute_sk) { 9741da177e4SLinus Torvalds rtnl_unlock(); 9751da177e4SLinus Torvalds return -EADDRINUSE; 9761da177e4SLinus Torvalds } 9771da177e4SLinus Torvalds 9781da177e4SLinus Torvalds ret = ip_ra_control(sk, 1, mrtsock_destruct); 9791da177e4SLinus Torvalds if (ret == 0) { 9801da177e4SLinus Torvalds write_lock_bh(&mrt_lock); 9814feb88e5SBenjamin Thery net->ipv4.mroute_sk = sk; 9821da177e4SLinus Torvalds write_unlock_bh(&mrt_lock); 9831da177e4SLinus Torvalds 9844feb88e5SBenjamin Thery IPV4_DEVCONF_ALL(net, MC_FORWARDING)++; 9851da177e4SLinus Torvalds } 9861da177e4SLinus Torvalds rtnl_unlock(); 9871da177e4SLinus Torvalds return ret; 9881da177e4SLinus Torvalds case MRT_DONE: 9894feb88e5SBenjamin Thery if (sk != net->ipv4.mroute_sk) 9901da177e4SLinus Torvalds return -EACCES; 9911da177e4SLinus Torvalds return ip_ra_control(sk, 0, NULL); 9921da177e4SLinus Torvalds case MRT_ADD_VIF: 9931da177e4SLinus Torvalds case MRT_DEL_VIF: 9941da177e4SLinus Torvalds if (optlen != sizeof(vif)) 9951da177e4SLinus Torvalds return -EINVAL; 9961da177e4SLinus Torvalds if (copy_from_user(&vif, optval, sizeof(vif))) 9971da177e4SLinus Torvalds return -EFAULT; 9981da177e4SLinus Torvalds if (vif.vifc_vifi >= MAXVIFS) 9991da177e4SLinus Torvalds return -ENFILE; 10001da177e4SLinus Torvalds rtnl_lock(); 10011da177e4SLinus Torvalds if (optname == MRT_ADD_VIF) { 10024feb88e5SBenjamin Thery ret = vif_add(net, &vif, sk == net->ipv4.mroute_sk); 10031da177e4SLinus Torvalds } else { 1004d17fa6faSEric Dumazet ret = vif_delete(net, vif.vifc_vifi, 0, NULL); 10051da177e4SLinus Torvalds } 10061da177e4SLinus Torvalds rtnl_unlock(); 10071da177e4SLinus Torvalds return ret; 10081da177e4SLinus Torvalds 10091da177e4SLinus Torvalds /* 10101da177e4SLinus Torvalds * Manipulate the forwarding caches. These live 10111da177e4SLinus Torvalds * in a sort of kernel/user symbiosis. 10121da177e4SLinus Torvalds */ 10131da177e4SLinus Torvalds case MRT_ADD_MFC: 10141da177e4SLinus Torvalds case MRT_DEL_MFC: 10151da177e4SLinus Torvalds if (optlen != sizeof(mfc)) 10161da177e4SLinus Torvalds return -EINVAL; 10171da177e4SLinus Torvalds if (copy_from_user(&mfc, optval, sizeof(mfc))) 10181da177e4SLinus Torvalds return -EFAULT; 10191da177e4SLinus Torvalds rtnl_lock(); 10201da177e4SLinus Torvalds if (optname == MRT_DEL_MFC) 10214feb88e5SBenjamin Thery ret = ipmr_mfc_delete(net, &mfc); 10221da177e4SLinus Torvalds else 10234feb88e5SBenjamin Thery ret = ipmr_mfc_add(net, &mfc, sk == net->ipv4.mroute_sk); 10241da177e4SLinus Torvalds rtnl_unlock(); 10251da177e4SLinus Torvalds return ret; 10261da177e4SLinus Torvalds /* 10271da177e4SLinus Torvalds * Control PIM assert. 10281da177e4SLinus Torvalds */ 10291da177e4SLinus Torvalds case MRT_ASSERT: 10301da177e4SLinus Torvalds { 10311da177e4SLinus Torvalds int v; 10321da177e4SLinus Torvalds if (get_user(v,(int __user *)optval)) 10331da177e4SLinus Torvalds return -EFAULT; 10344feb88e5SBenjamin Thery net->ipv4.mroute_do_assert = (v) ? 1 : 0; 10351da177e4SLinus Torvalds return 0; 10361da177e4SLinus Torvalds } 10371da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM 10381da177e4SLinus Torvalds case MRT_PIM: 10391da177e4SLinus Torvalds { 1040ba93ef74SStephen Hemminger int v; 1041ba93ef74SStephen Hemminger 10421da177e4SLinus Torvalds if (get_user(v,(int __user *)optval)) 10431da177e4SLinus Torvalds return -EFAULT; 10441da177e4SLinus Torvalds v = (v) ? 1 : 0; 1045ba93ef74SStephen Hemminger 10461da177e4SLinus Torvalds rtnl_lock(); 10471da177e4SLinus Torvalds ret = 0; 10484feb88e5SBenjamin Thery if (v != net->ipv4.mroute_do_pim) { 10494feb88e5SBenjamin Thery net->ipv4.mroute_do_pim = v; 10504feb88e5SBenjamin Thery net->ipv4.mroute_do_assert = v; 10511da177e4SLinus Torvalds } 10521da177e4SLinus Torvalds rtnl_unlock(); 10531da177e4SLinus Torvalds return ret; 10541da177e4SLinus Torvalds } 10551da177e4SLinus Torvalds #endif 10561da177e4SLinus Torvalds /* 10571da177e4SLinus Torvalds * Spurious command, or MRT_VERSION which you cannot 10581da177e4SLinus Torvalds * set. 10591da177e4SLinus Torvalds */ 10601da177e4SLinus Torvalds default: 10611da177e4SLinus Torvalds return -ENOPROTOOPT; 10621da177e4SLinus Torvalds } 10631da177e4SLinus Torvalds } 10641da177e4SLinus Torvalds 10651da177e4SLinus Torvalds /* 10661da177e4SLinus Torvalds * Getsock opt support for the multicast routing system. 10671da177e4SLinus Torvalds */ 10681da177e4SLinus Torvalds 10691da177e4SLinus Torvalds int ip_mroute_getsockopt(struct sock *sk, int optname, char __user *optval, int __user *optlen) 10701da177e4SLinus Torvalds { 10711da177e4SLinus Torvalds int olr; 10721da177e4SLinus Torvalds int val; 10734feb88e5SBenjamin Thery struct net *net = sock_net(sk); 10741da177e4SLinus Torvalds 10751da177e4SLinus Torvalds if (optname != MRT_VERSION && 10761da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM 10771da177e4SLinus Torvalds optname!=MRT_PIM && 10781da177e4SLinus Torvalds #endif 10791da177e4SLinus Torvalds optname!=MRT_ASSERT) 10801da177e4SLinus Torvalds return -ENOPROTOOPT; 10811da177e4SLinus Torvalds 10821da177e4SLinus Torvalds if (get_user(olr, optlen)) 10831da177e4SLinus Torvalds return -EFAULT; 10841da177e4SLinus Torvalds 10851da177e4SLinus Torvalds olr = min_t(unsigned int, olr, sizeof(int)); 10861da177e4SLinus Torvalds if (olr < 0) 10871da177e4SLinus Torvalds return -EINVAL; 10881da177e4SLinus Torvalds 10891da177e4SLinus Torvalds if (put_user(olr, optlen)) 10901da177e4SLinus Torvalds return -EFAULT; 10911da177e4SLinus Torvalds if (optname == MRT_VERSION) 10921da177e4SLinus Torvalds val = 0x0305; 10931da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM 10941da177e4SLinus Torvalds else if (optname == MRT_PIM) 10954feb88e5SBenjamin Thery val = net->ipv4.mroute_do_pim; 10961da177e4SLinus Torvalds #endif 10971da177e4SLinus Torvalds else 10984feb88e5SBenjamin Thery val = net->ipv4.mroute_do_assert; 10991da177e4SLinus Torvalds if (copy_to_user(optval, &val, olr)) 11001da177e4SLinus Torvalds return -EFAULT; 11011da177e4SLinus Torvalds return 0; 11021da177e4SLinus Torvalds } 11031da177e4SLinus Torvalds 11041da177e4SLinus Torvalds /* 11051da177e4SLinus Torvalds * The IP multicast ioctl support routines. 11061da177e4SLinus Torvalds */ 11071da177e4SLinus Torvalds 11081da177e4SLinus Torvalds int ipmr_ioctl(struct sock *sk, int cmd, void __user *arg) 11091da177e4SLinus Torvalds { 11101da177e4SLinus Torvalds struct sioc_sg_req sr; 11111da177e4SLinus Torvalds struct sioc_vif_req vr; 11121da177e4SLinus Torvalds struct vif_device *vif; 11131da177e4SLinus Torvalds struct mfc_cache *c; 11144feb88e5SBenjamin Thery struct net *net = sock_net(sk); 11151da177e4SLinus Torvalds 1116132adf54SStephen Hemminger switch (cmd) { 11171da177e4SLinus Torvalds case SIOCGETVIFCNT: 11181da177e4SLinus Torvalds if (copy_from_user(&vr, arg, sizeof(vr))) 11191da177e4SLinus Torvalds return -EFAULT; 11204feb88e5SBenjamin Thery if (vr.vifi >= net->ipv4.maxvif) 11211da177e4SLinus Torvalds return -EINVAL; 11221da177e4SLinus Torvalds read_lock(&mrt_lock); 11234feb88e5SBenjamin Thery vif = &net->ipv4.vif_table[vr.vifi]; 11244feb88e5SBenjamin Thery if (VIF_EXISTS(net, vr.vifi)) { 11251da177e4SLinus Torvalds vr.icount = vif->pkt_in; 11261da177e4SLinus Torvalds vr.ocount = vif->pkt_out; 11271da177e4SLinus Torvalds vr.ibytes = vif->bytes_in; 11281da177e4SLinus Torvalds vr.obytes = vif->bytes_out; 11291da177e4SLinus Torvalds read_unlock(&mrt_lock); 11301da177e4SLinus Torvalds 11311da177e4SLinus Torvalds if (copy_to_user(arg, &vr, sizeof(vr))) 11321da177e4SLinus Torvalds return -EFAULT; 11331da177e4SLinus Torvalds return 0; 11341da177e4SLinus Torvalds } 11351da177e4SLinus Torvalds read_unlock(&mrt_lock); 11361da177e4SLinus Torvalds return -EADDRNOTAVAIL; 11371da177e4SLinus Torvalds case SIOCGETSGCNT: 11381da177e4SLinus Torvalds if (copy_from_user(&sr, arg, sizeof(sr))) 11391da177e4SLinus Torvalds return -EFAULT; 11401da177e4SLinus Torvalds 11411da177e4SLinus Torvalds read_lock(&mrt_lock); 11424feb88e5SBenjamin Thery c = ipmr_cache_find(net, sr.src.s_addr, sr.grp.s_addr); 11431da177e4SLinus Torvalds if (c) { 11441da177e4SLinus Torvalds sr.pktcnt = c->mfc_un.res.pkt; 11451da177e4SLinus Torvalds sr.bytecnt = c->mfc_un.res.bytes; 11461da177e4SLinus Torvalds sr.wrong_if = c->mfc_un.res.wrong_if; 11471da177e4SLinus Torvalds read_unlock(&mrt_lock); 11481da177e4SLinus Torvalds 11491da177e4SLinus Torvalds if (copy_to_user(arg, &sr, sizeof(sr))) 11501da177e4SLinus Torvalds return -EFAULT; 11511da177e4SLinus Torvalds return 0; 11521da177e4SLinus Torvalds } 11531da177e4SLinus Torvalds read_unlock(&mrt_lock); 11541da177e4SLinus Torvalds return -EADDRNOTAVAIL; 11551da177e4SLinus Torvalds default: 11561da177e4SLinus Torvalds return -ENOIOCTLCMD; 11571da177e4SLinus Torvalds } 11581da177e4SLinus Torvalds } 11591da177e4SLinus Torvalds 11601da177e4SLinus Torvalds 11611da177e4SLinus Torvalds static int ipmr_device_event(struct notifier_block *this, unsigned long event, void *ptr) 11621da177e4SLinus Torvalds { 1163e9dc8653SEric W. Biederman struct net_device *dev = ptr; 11644feb88e5SBenjamin Thery struct net *net = dev_net(dev); 11651da177e4SLinus Torvalds struct vif_device *v; 11661da177e4SLinus Torvalds int ct; 1167d17fa6faSEric Dumazet LIST_HEAD(list); 1168e9dc8653SEric W. Biederman 11691da177e4SLinus Torvalds if (event != NETDEV_UNREGISTER) 11701da177e4SLinus Torvalds return NOTIFY_DONE; 11714feb88e5SBenjamin Thery v = &net->ipv4.vif_table[0]; 11724feb88e5SBenjamin Thery for (ct = 0; ct < net->ipv4.maxvif; ct++, v++) { 1173e9dc8653SEric W. Biederman if (v->dev == dev) 1174d17fa6faSEric Dumazet vif_delete(net, ct, 1, &list); 11751da177e4SLinus Torvalds } 1176d17fa6faSEric Dumazet unregister_netdevice_many(&list); 11771da177e4SLinus Torvalds return NOTIFY_DONE; 11781da177e4SLinus Torvalds } 11791da177e4SLinus Torvalds 11801da177e4SLinus Torvalds 11811da177e4SLinus Torvalds static struct notifier_block ip_mr_notifier = { 11821da177e4SLinus Torvalds .notifier_call = ipmr_device_event, 11831da177e4SLinus Torvalds }; 11841da177e4SLinus Torvalds 11851da177e4SLinus Torvalds /* 11861da177e4SLinus Torvalds * Encapsulate a packet by attaching a valid IPIP header to it. 11871da177e4SLinus Torvalds * This avoids tunnel drivers and other mess and gives us the speed so 11881da177e4SLinus Torvalds * important for multicast video. 11891da177e4SLinus Torvalds */ 11901da177e4SLinus Torvalds 1191114c7844SAl Viro static void ip_encap(struct sk_buff *skb, __be32 saddr, __be32 daddr) 11921da177e4SLinus Torvalds { 11938856dfa3SArnaldo Carvalho de Melo struct iphdr *iph; 1194eddc9ec5SArnaldo Carvalho de Melo struct iphdr *old_iph = ip_hdr(skb); 11958856dfa3SArnaldo Carvalho de Melo 11968856dfa3SArnaldo Carvalho de Melo skb_push(skb, sizeof(struct iphdr)); 1197b0e380b1SArnaldo Carvalho de Melo skb->transport_header = skb->network_header; 11988856dfa3SArnaldo Carvalho de Melo skb_reset_network_header(skb); 1199eddc9ec5SArnaldo Carvalho de Melo iph = ip_hdr(skb); 12001da177e4SLinus Torvalds 12011da177e4SLinus Torvalds iph->version = 4; 1202e023dd64SArnaldo Carvalho de Melo iph->tos = old_iph->tos; 1203e023dd64SArnaldo Carvalho de Melo iph->ttl = old_iph->ttl; 12041da177e4SLinus Torvalds iph->frag_off = 0; 12051da177e4SLinus Torvalds iph->daddr = daddr; 12061da177e4SLinus Torvalds iph->saddr = saddr; 12071da177e4SLinus Torvalds iph->protocol = IPPROTO_IPIP; 12081da177e4SLinus Torvalds iph->ihl = 5; 12091da177e4SLinus Torvalds iph->tot_len = htons(skb->len); 1210adf30907SEric Dumazet ip_select_ident(iph, skb_dst(skb), NULL); 12111da177e4SLinus Torvalds ip_send_check(iph); 12121da177e4SLinus Torvalds 12131da177e4SLinus Torvalds memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt)); 12141da177e4SLinus Torvalds nf_reset(skb); 12151da177e4SLinus Torvalds } 12161da177e4SLinus Torvalds 12171da177e4SLinus Torvalds static inline int ipmr_forward_finish(struct sk_buff *skb) 12181da177e4SLinus Torvalds { 12191da177e4SLinus Torvalds struct ip_options * opt = &(IPCB(skb)->opt); 12201da177e4SLinus Torvalds 1221adf30907SEric Dumazet IP_INC_STATS_BH(dev_net(skb_dst(skb)->dev), IPSTATS_MIB_OUTFORWDATAGRAMS); 12221da177e4SLinus Torvalds 12231da177e4SLinus Torvalds if (unlikely(opt->optlen)) 12241da177e4SLinus Torvalds ip_forward_options(skb); 12251da177e4SLinus Torvalds 12261da177e4SLinus Torvalds return dst_output(skb); 12271da177e4SLinus Torvalds } 12281da177e4SLinus Torvalds 12291da177e4SLinus Torvalds /* 12301da177e4SLinus Torvalds * Processing handlers for ipmr_forward 12311da177e4SLinus Torvalds */ 12321da177e4SLinus Torvalds 12331da177e4SLinus Torvalds static void ipmr_queue_xmit(struct sk_buff *skb, struct mfc_cache *c, int vifi) 12341da177e4SLinus Torvalds { 12354feb88e5SBenjamin Thery struct net *net = mfc_net(c); 1236eddc9ec5SArnaldo Carvalho de Melo const struct iphdr *iph = ip_hdr(skb); 12374feb88e5SBenjamin Thery struct vif_device *vif = &net->ipv4.vif_table[vifi]; 12381da177e4SLinus Torvalds struct net_device *dev; 12391da177e4SLinus Torvalds struct rtable *rt; 12401da177e4SLinus Torvalds int encap = 0; 12411da177e4SLinus Torvalds 12421da177e4SLinus Torvalds if (vif->dev == NULL) 12431da177e4SLinus Torvalds goto out_free; 12441da177e4SLinus Torvalds 12451da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM 12461da177e4SLinus Torvalds if (vif->flags & VIFF_REGISTER) { 12471da177e4SLinus Torvalds vif->pkt_out++; 12481da177e4SLinus Torvalds vif->bytes_out += skb->len; 1249cf3677aeSPavel Emelyanov vif->dev->stats.tx_bytes += skb->len; 1250cf3677aeSPavel Emelyanov vif->dev->stats.tx_packets++; 12514feb88e5SBenjamin Thery ipmr_cache_report(net, skb, vifi, IGMPMSG_WHOLEPKT); 125269ebbf58SIlpo Järvinen goto out_free; 12531da177e4SLinus Torvalds } 12541da177e4SLinus Torvalds #endif 12551da177e4SLinus Torvalds 12561da177e4SLinus Torvalds if (vif->flags&VIFF_TUNNEL) { 12571da177e4SLinus Torvalds struct flowi fl = { .oif = vif->link, 12581da177e4SLinus Torvalds .nl_u = { .ip4_u = 12591da177e4SLinus Torvalds { .daddr = vif->remote, 12601da177e4SLinus Torvalds .saddr = vif->local, 12611da177e4SLinus Torvalds .tos = RT_TOS(iph->tos) } }, 12621da177e4SLinus Torvalds .proto = IPPROTO_IPIP }; 12634feb88e5SBenjamin Thery if (ip_route_output_key(net, &rt, &fl)) 12641da177e4SLinus Torvalds goto out_free; 12651da177e4SLinus Torvalds encap = sizeof(struct iphdr); 12661da177e4SLinus Torvalds } else { 12671da177e4SLinus Torvalds struct flowi fl = { .oif = vif->link, 12681da177e4SLinus Torvalds .nl_u = { .ip4_u = 12691da177e4SLinus Torvalds { .daddr = iph->daddr, 12701da177e4SLinus Torvalds .tos = RT_TOS(iph->tos) } }, 12711da177e4SLinus Torvalds .proto = IPPROTO_IPIP }; 12724feb88e5SBenjamin Thery if (ip_route_output_key(net, &rt, &fl)) 12731da177e4SLinus Torvalds goto out_free; 12741da177e4SLinus Torvalds } 12751da177e4SLinus Torvalds 12761da177e4SLinus Torvalds dev = rt->u.dst.dev; 12771da177e4SLinus Torvalds 12781da177e4SLinus Torvalds if (skb->len+encap > dst_mtu(&rt->u.dst) && (ntohs(iph->frag_off) & IP_DF)) { 12791da177e4SLinus Torvalds /* Do not fragment multicasts. Alas, IPv4 does not 12801da177e4SLinus Torvalds allow to send ICMP, so that packets will disappear 12811da177e4SLinus Torvalds to blackhole. 12821da177e4SLinus Torvalds */ 12831da177e4SLinus Torvalds 12847c73a6faSPavel Emelyanov IP_INC_STATS_BH(dev_net(dev), IPSTATS_MIB_FRAGFAILS); 12851da177e4SLinus Torvalds ip_rt_put(rt); 12861da177e4SLinus Torvalds goto out_free; 12871da177e4SLinus Torvalds } 12881da177e4SLinus Torvalds 12891da177e4SLinus Torvalds encap += LL_RESERVED_SPACE(dev) + rt->u.dst.header_len; 12901da177e4SLinus Torvalds 12911da177e4SLinus Torvalds if (skb_cow(skb, encap)) { 12921da177e4SLinus Torvalds ip_rt_put(rt); 12931da177e4SLinus Torvalds goto out_free; 12941da177e4SLinus Torvalds } 12951da177e4SLinus Torvalds 12961da177e4SLinus Torvalds vif->pkt_out++; 12971da177e4SLinus Torvalds vif->bytes_out += skb->len; 12981da177e4SLinus Torvalds 1299adf30907SEric Dumazet skb_dst_drop(skb); 1300adf30907SEric Dumazet skb_dst_set(skb, &rt->u.dst); 1301eddc9ec5SArnaldo Carvalho de Melo ip_decrease_ttl(ip_hdr(skb)); 13021da177e4SLinus Torvalds 13031da177e4SLinus Torvalds /* FIXME: forward and output firewalls used to be called here. 13041da177e4SLinus Torvalds * What do we do with netfilter? -- RR */ 13051da177e4SLinus Torvalds if (vif->flags & VIFF_TUNNEL) { 13061da177e4SLinus Torvalds ip_encap(skb, vif->local, vif->remote); 13071da177e4SLinus Torvalds /* FIXME: extra output firewall step used to be here. --RR */ 13082f4c02d4SPavel Emelyanov vif->dev->stats.tx_packets++; 13092f4c02d4SPavel Emelyanov vif->dev->stats.tx_bytes += skb->len; 13101da177e4SLinus Torvalds } 13111da177e4SLinus Torvalds 13121da177e4SLinus Torvalds IPCB(skb)->flags |= IPSKB_FORWARDED; 13131da177e4SLinus Torvalds 13141da177e4SLinus Torvalds /* 13151da177e4SLinus Torvalds * RFC1584 teaches, that DVMRP/PIM router must deliver packets locally 13161da177e4SLinus Torvalds * not only before forwarding, but after forwarding on all output 13171da177e4SLinus Torvalds * interfaces. It is clear, if mrouter runs a multicasting 13181da177e4SLinus Torvalds * program, it should receive packets not depending to what interface 13191da177e4SLinus Torvalds * program is joined. 13201da177e4SLinus Torvalds * If we will not make it, the program will have to join on all 13211da177e4SLinus Torvalds * interfaces. On the other hand, multihoming host (or router, but 13221da177e4SLinus Torvalds * not mrouter) cannot join to more than one interface - it will 13231da177e4SLinus Torvalds * result in receiving multiple packets. 13241da177e4SLinus Torvalds */ 13256e23ae2aSPatrick McHardy NF_HOOK(PF_INET, NF_INET_FORWARD, skb, skb->dev, dev, 13261da177e4SLinus Torvalds ipmr_forward_finish); 13271da177e4SLinus Torvalds return; 13281da177e4SLinus Torvalds 13291da177e4SLinus Torvalds out_free: 13301da177e4SLinus Torvalds kfree_skb(skb); 13311da177e4SLinus Torvalds return; 13321da177e4SLinus Torvalds } 13331da177e4SLinus Torvalds 13341da177e4SLinus Torvalds static int ipmr_find_vif(struct net_device *dev) 13351da177e4SLinus Torvalds { 13364feb88e5SBenjamin Thery struct net *net = dev_net(dev); 13371da177e4SLinus Torvalds int ct; 13384feb88e5SBenjamin Thery for (ct = net->ipv4.maxvif-1; ct >= 0; ct--) { 13394feb88e5SBenjamin Thery if (net->ipv4.vif_table[ct].dev == dev) 13401da177e4SLinus Torvalds break; 13411da177e4SLinus Torvalds } 13421da177e4SLinus Torvalds return ct; 13431da177e4SLinus Torvalds } 13441da177e4SLinus Torvalds 13451da177e4SLinus Torvalds /* "local" means that we should preserve one skb (for local delivery) */ 13461da177e4SLinus Torvalds 13471da177e4SLinus Torvalds static int ip_mr_forward(struct sk_buff *skb, struct mfc_cache *cache, int local) 13481da177e4SLinus Torvalds { 13491da177e4SLinus Torvalds int psend = -1; 13501da177e4SLinus Torvalds int vif, ct; 13514feb88e5SBenjamin Thery struct net *net = mfc_net(cache); 13521da177e4SLinus Torvalds 13531da177e4SLinus Torvalds vif = cache->mfc_parent; 13541da177e4SLinus Torvalds cache->mfc_un.res.pkt++; 13551da177e4SLinus Torvalds cache->mfc_un.res.bytes += skb->len; 13561da177e4SLinus Torvalds 13571da177e4SLinus Torvalds /* 13581da177e4SLinus Torvalds * Wrong interface: drop packet and (maybe) send PIM assert. 13591da177e4SLinus Torvalds */ 13604feb88e5SBenjamin Thery if (net->ipv4.vif_table[vif].dev != skb->dev) { 13611da177e4SLinus Torvalds int true_vifi; 13621da177e4SLinus Torvalds 1363511c3f92SEric Dumazet if (skb_rtable(skb)->fl.iif == 0) { 13641da177e4SLinus Torvalds /* It is our own packet, looped back. 13651da177e4SLinus Torvalds Very complicated situation... 13661da177e4SLinus Torvalds 13671da177e4SLinus Torvalds The best workaround until routing daemons will be 13681da177e4SLinus Torvalds fixed is not to redistribute packet, if it was 13691da177e4SLinus Torvalds send through wrong interface. It means, that 13701da177e4SLinus Torvalds multicast applications WILL NOT work for 13711da177e4SLinus Torvalds (S,G), which have default multicast route pointing 13721da177e4SLinus Torvalds to wrong oif. In any case, it is not a good 13731da177e4SLinus Torvalds idea to use multicasting applications on router. 13741da177e4SLinus Torvalds */ 13751da177e4SLinus Torvalds goto dont_forward; 13761da177e4SLinus Torvalds } 13771da177e4SLinus Torvalds 13781da177e4SLinus Torvalds cache->mfc_un.res.wrong_if++; 13791da177e4SLinus Torvalds true_vifi = ipmr_find_vif(skb->dev); 13801da177e4SLinus Torvalds 13814feb88e5SBenjamin Thery if (true_vifi >= 0 && net->ipv4.mroute_do_assert && 13821da177e4SLinus Torvalds /* pimsm uses asserts, when switching from RPT to SPT, 13831da177e4SLinus Torvalds so that we cannot check that packet arrived on an oif. 13841da177e4SLinus Torvalds It is bad, but otherwise we would need to move pretty 13851da177e4SLinus Torvalds large chunk of pimd to kernel. Ough... --ANK 13861da177e4SLinus Torvalds */ 13874feb88e5SBenjamin Thery (net->ipv4.mroute_do_pim || 13886f9374a9SBenjamin Thery cache->mfc_un.res.ttls[true_vifi] < 255) && 13891da177e4SLinus Torvalds time_after(jiffies, 13901da177e4SLinus Torvalds cache->mfc_un.res.last_assert + MFC_ASSERT_THRESH)) { 13911da177e4SLinus Torvalds cache->mfc_un.res.last_assert = jiffies; 13924feb88e5SBenjamin Thery ipmr_cache_report(net, skb, true_vifi, IGMPMSG_WRONGVIF); 13931da177e4SLinus Torvalds } 13941da177e4SLinus Torvalds goto dont_forward; 13951da177e4SLinus Torvalds } 13961da177e4SLinus Torvalds 13974feb88e5SBenjamin Thery net->ipv4.vif_table[vif].pkt_in++; 13984feb88e5SBenjamin Thery net->ipv4.vif_table[vif].bytes_in += skb->len; 13991da177e4SLinus Torvalds 14001da177e4SLinus Torvalds /* 14011da177e4SLinus Torvalds * Forward the frame 14021da177e4SLinus Torvalds */ 14031da177e4SLinus Torvalds for (ct = cache->mfc_un.res.maxvif-1; ct >= cache->mfc_un.res.minvif; ct--) { 1404eddc9ec5SArnaldo Carvalho de Melo if (ip_hdr(skb)->ttl > cache->mfc_un.res.ttls[ct]) { 14051da177e4SLinus Torvalds if (psend != -1) { 14061da177e4SLinus Torvalds struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC); 14071da177e4SLinus Torvalds if (skb2) 14081da177e4SLinus Torvalds ipmr_queue_xmit(skb2, cache, psend); 14091da177e4SLinus Torvalds } 14101da177e4SLinus Torvalds psend = ct; 14111da177e4SLinus Torvalds } 14121da177e4SLinus Torvalds } 14131da177e4SLinus Torvalds if (psend != -1) { 14141da177e4SLinus Torvalds if (local) { 14151da177e4SLinus Torvalds struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC); 14161da177e4SLinus Torvalds if (skb2) 14171da177e4SLinus Torvalds ipmr_queue_xmit(skb2, cache, psend); 14181da177e4SLinus Torvalds } else { 14191da177e4SLinus Torvalds ipmr_queue_xmit(skb, cache, psend); 14201da177e4SLinus Torvalds return 0; 14211da177e4SLinus Torvalds } 14221da177e4SLinus Torvalds } 14231da177e4SLinus Torvalds 14241da177e4SLinus Torvalds dont_forward: 14251da177e4SLinus Torvalds if (!local) 14261da177e4SLinus Torvalds kfree_skb(skb); 14271da177e4SLinus Torvalds return 0; 14281da177e4SLinus Torvalds } 14291da177e4SLinus Torvalds 14301da177e4SLinus Torvalds 14311da177e4SLinus Torvalds /* 14321da177e4SLinus Torvalds * Multicast packets for forwarding arrive here 14331da177e4SLinus Torvalds */ 14341da177e4SLinus Torvalds 14351da177e4SLinus Torvalds int ip_mr_input(struct sk_buff *skb) 14361da177e4SLinus Torvalds { 14371da177e4SLinus Torvalds struct mfc_cache *cache; 14384feb88e5SBenjamin Thery struct net *net = dev_net(skb->dev); 1439511c3f92SEric Dumazet int local = skb_rtable(skb)->rt_flags & RTCF_LOCAL; 14401da177e4SLinus Torvalds 14411da177e4SLinus Torvalds /* Packet is looped back after forward, it should not be 14421da177e4SLinus Torvalds forwarded second time, but still can be delivered locally. 14431da177e4SLinus Torvalds */ 14441da177e4SLinus Torvalds if (IPCB(skb)->flags&IPSKB_FORWARDED) 14451da177e4SLinus Torvalds goto dont_forward; 14461da177e4SLinus Torvalds 14471da177e4SLinus Torvalds if (!local) { 14481da177e4SLinus Torvalds if (IPCB(skb)->opt.router_alert) { 14491da177e4SLinus Torvalds if (ip_call_ra_chain(skb)) 14501da177e4SLinus Torvalds return 0; 1451eddc9ec5SArnaldo Carvalho de Melo } else if (ip_hdr(skb)->protocol == IPPROTO_IGMP){ 14521da177e4SLinus Torvalds /* IGMPv1 (and broken IGMPv2 implementations sort of 14531da177e4SLinus Torvalds Cisco IOS <= 11.2(8)) do not put router alert 14541da177e4SLinus Torvalds option to IGMP packets destined to routable 14551da177e4SLinus Torvalds groups. It is very bad, because it means 14561da177e4SLinus Torvalds that we can forward NO IGMP messages. 14571da177e4SLinus Torvalds */ 14581da177e4SLinus Torvalds read_lock(&mrt_lock); 14594feb88e5SBenjamin Thery if (net->ipv4.mroute_sk) { 14602715bcf9SPatrick McHardy nf_reset(skb); 14614feb88e5SBenjamin Thery raw_rcv(net->ipv4.mroute_sk, skb); 14621da177e4SLinus Torvalds read_unlock(&mrt_lock); 14631da177e4SLinus Torvalds return 0; 14641da177e4SLinus Torvalds } 14651da177e4SLinus Torvalds read_unlock(&mrt_lock); 14661da177e4SLinus Torvalds } 14671da177e4SLinus Torvalds } 14681da177e4SLinus Torvalds 14691da177e4SLinus Torvalds read_lock(&mrt_lock); 14704feb88e5SBenjamin Thery cache = ipmr_cache_find(net, ip_hdr(skb)->saddr, ip_hdr(skb)->daddr); 14711da177e4SLinus Torvalds 14721da177e4SLinus Torvalds /* 14731da177e4SLinus Torvalds * No usable cache entry 14741da177e4SLinus Torvalds */ 14751da177e4SLinus Torvalds if (cache == NULL) { 14761da177e4SLinus Torvalds int vif; 14771da177e4SLinus Torvalds 14781da177e4SLinus Torvalds if (local) { 14791da177e4SLinus Torvalds struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC); 14801da177e4SLinus Torvalds ip_local_deliver(skb); 14811da177e4SLinus Torvalds if (skb2 == NULL) { 14821da177e4SLinus Torvalds read_unlock(&mrt_lock); 14831da177e4SLinus Torvalds return -ENOBUFS; 14841da177e4SLinus Torvalds } 14851da177e4SLinus Torvalds skb = skb2; 14861da177e4SLinus Torvalds } 14871da177e4SLinus Torvalds 14881da177e4SLinus Torvalds vif = ipmr_find_vif(skb->dev); 14891da177e4SLinus Torvalds if (vif >= 0) { 14904feb88e5SBenjamin Thery int err = ipmr_cache_unresolved(net, vif, skb); 14911da177e4SLinus Torvalds read_unlock(&mrt_lock); 14921da177e4SLinus Torvalds 14931da177e4SLinus Torvalds return err; 14941da177e4SLinus Torvalds } 14951da177e4SLinus Torvalds read_unlock(&mrt_lock); 14961da177e4SLinus Torvalds kfree_skb(skb); 14971da177e4SLinus Torvalds return -ENODEV; 14981da177e4SLinus Torvalds } 14991da177e4SLinus Torvalds 15001da177e4SLinus Torvalds ip_mr_forward(skb, cache, local); 15011da177e4SLinus Torvalds 15021da177e4SLinus Torvalds read_unlock(&mrt_lock); 15031da177e4SLinus Torvalds 15041da177e4SLinus Torvalds if (local) 15051da177e4SLinus Torvalds return ip_local_deliver(skb); 15061da177e4SLinus Torvalds 15071da177e4SLinus Torvalds return 0; 15081da177e4SLinus Torvalds 15091da177e4SLinus Torvalds dont_forward: 15101da177e4SLinus Torvalds if (local) 15111da177e4SLinus Torvalds return ip_local_deliver(skb); 15121da177e4SLinus Torvalds kfree_skb(skb); 15131da177e4SLinus Torvalds return 0; 15141da177e4SLinus Torvalds } 15151da177e4SLinus Torvalds 1516b1879204SIlpo Järvinen #ifdef CONFIG_IP_PIMSM 1517b1879204SIlpo Järvinen static int __pim_rcv(struct sk_buff *skb, unsigned int pimlen) 15181da177e4SLinus Torvalds { 15191da177e4SLinus Torvalds struct net_device *reg_dev = NULL; 1520b1879204SIlpo Järvinen struct iphdr *encap; 15214feb88e5SBenjamin Thery struct net *net = dev_net(skb->dev); 15221da177e4SLinus Torvalds 1523b1879204SIlpo Järvinen encap = (struct iphdr *)(skb_transport_header(skb) + pimlen); 15241da177e4SLinus Torvalds /* 15251da177e4SLinus Torvalds Check that: 15261da177e4SLinus Torvalds a. packet is really destinted to a multicast group 15271da177e4SLinus Torvalds b. packet is not a NULL-REGISTER 15281da177e4SLinus Torvalds c. packet is not truncated 15291da177e4SLinus Torvalds */ 1530f97c1e0cSJoe Perches if (!ipv4_is_multicast(encap->daddr) || 15311da177e4SLinus Torvalds encap->tot_len == 0 || 1532b1879204SIlpo Järvinen ntohs(encap->tot_len) + pimlen > skb->len) 1533b1879204SIlpo Järvinen return 1; 15341da177e4SLinus Torvalds 15351da177e4SLinus Torvalds read_lock(&mrt_lock); 15364feb88e5SBenjamin Thery if (net->ipv4.mroute_reg_vif_num >= 0) 15374feb88e5SBenjamin Thery reg_dev = net->ipv4.vif_table[net->ipv4.mroute_reg_vif_num].dev; 15381da177e4SLinus Torvalds if (reg_dev) 15391da177e4SLinus Torvalds dev_hold(reg_dev); 15401da177e4SLinus Torvalds read_unlock(&mrt_lock); 15411da177e4SLinus Torvalds 15421da177e4SLinus Torvalds if (reg_dev == NULL) 1543b1879204SIlpo Järvinen return 1; 15441da177e4SLinus Torvalds 1545b0e380b1SArnaldo Carvalho de Melo skb->mac_header = skb->network_header; 15461da177e4SLinus Torvalds skb_pull(skb, (u8*)encap - skb->data); 154731c7711bSArnaldo Carvalho de Melo skb_reset_network_header(skb); 15481da177e4SLinus Torvalds skb->dev = reg_dev; 15491da177e4SLinus Torvalds skb->protocol = htons(ETH_P_IP); 15501da177e4SLinus Torvalds skb->ip_summed = 0; 15511da177e4SLinus Torvalds skb->pkt_type = PACKET_HOST; 1552adf30907SEric Dumazet skb_dst_drop(skb); 1553cf3677aeSPavel Emelyanov reg_dev->stats.rx_bytes += skb->len; 1554cf3677aeSPavel Emelyanov reg_dev->stats.rx_packets++; 15551da177e4SLinus Torvalds nf_reset(skb); 15561da177e4SLinus Torvalds netif_rx(skb); 15571da177e4SLinus Torvalds dev_put(reg_dev); 1558b1879204SIlpo Järvinen 15591da177e4SLinus Torvalds return 0; 1560b1879204SIlpo Järvinen } 1561b1879204SIlpo Järvinen #endif 1562b1879204SIlpo Järvinen 1563b1879204SIlpo Järvinen #ifdef CONFIG_IP_PIMSM_V1 1564b1879204SIlpo Järvinen /* 1565b1879204SIlpo Järvinen * Handle IGMP messages of PIMv1 1566b1879204SIlpo Järvinen */ 1567b1879204SIlpo Järvinen 1568b1879204SIlpo Järvinen int pim_rcv_v1(struct sk_buff * skb) 1569b1879204SIlpo Järvinen { 1570b1879204SIlpo Järvinen struct igmphdr *pim; 15714feb88e5SBenjamin Thery struct net *net = dev_net(skb->dev); 1572b1879204SIlpo Järvinen 1573b1879204SIlpo Järvinen if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr))) 1574b1879204SIlpo Järvinen goto drop; 1575b1879204SIlpo Järvinen 1576b1879204SIlpo Järvinen pim = igmp_hdr(skb); 1577b1879204SIlpo Järvinen 15784feb88e5SBenjamin Thery if (!net->ipv4.mroute_do_pim || 1579b1879204SIlpo Järvinen pim->group != PIM_V1_VERSION || pim->code != PIM_V1_REGISTER) 1580b1879204SIlpo Järvinen goto drop; 1581b1879204SIlpo Järvinen 1582b1879204SIlpo Järvinen if (__pim_rcv(skb, sizeof(*pim))) { 15831da177e4SLinus Torvalds drop: 15841da177e4SLinus Torvalds kfree_skb(skb); 1585b1879204SIlpo Järvinen } 15861da177e4SLinus Torvalds return 0; 15871da177e4SLinus Torvalds } 15881da177e4SLinus Torvalds #endif 15891da177e4SLinus Torvalds 15901da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM_V2 15911da177e4SLinus Torvalds static int pim_rcv(struct sk_buff * skb) 15921da177e4SLinus Torvalds { 15931da177e4SLinus Torvalds struct pimreghdr *pim; 15941da177e4SLinus Torvalds 1595b1879204SIlpo Järvinen if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr))) 15961da177e4SLinus Torvalds goto drop; 15971da177e4SLinus Torvalds 15989c70220bSArnaldo Carvalho de Melo pim = (struct pimreghdr *)skb_transport_header(skb); 15991da177e4SLinus Torvalds if (pim->type != ((PIM_VERSION<<4)|(PIM_REGISTER)) || 16001da177e4SLinus Torvalds (pim->flags&PIM_NULL_REGISTER) || 16011da177e4SLinus Torvalds (ip_compute_csum((void *)pim, sizeof(*pim)) != 0 && 1602d3bc23e7SAl Viro csum_fold(skb_checksum(skb, 0, skb->len, 0)))) 16031da177e4SLinus Torvalds goto drop; 16041da177e4SLinus Torvalds 1605b1879204SIlpo Järvinen if (__pim_rcv(skb, sizeof(*pim))) { 16061da177e4SLinus Torvalds drop: 16071da177e4SLinus Torvalds kfree_skb(skb); 1608b1879204SIlpo Järvinen } 16091da177e4SLinus Torvalds return 0; 16101da177e4SLinus Torvalds } 16111da177e4SLinus Torvalds #endif 16121da177e4SLinus Torvalds 16131da177e4SLinus Torvalds static int 16141da177e4SLinus Torvalds ipmr_fill_mroute(struct sk_buff *skb, struct mfc_cache *c, struct rtmsg *rtm) 16151da177e4SLinus Torvalds { 16161da177e4SLinus Torvalds int ct; 16171da177e4SLinus Torvalds struct rtnexthop *nhp; 16184feb88e5SBenjamin Thery struct net *net = mfc_net(c); 16194feb88e5SBenjamin Thery struct net_device *dev = net->ipv4.vif_table[c->mfc_parent].dev; 162027a884dcSArnaldo Carvalho de Melo u8 *b = skb_tail_pointer(skb); 16211da177e4SLinus Torvalds struct rtattr *mp_head; 16221da177e4SLinus Torvalds 16231da177e4SLinus Torvalds if (dev) 16241da177e4SLinus Torvalds RTA_PUT(skb, RTA_IIF, 4, &dev->ifindex); 16251da177e4SLinus Torvalds 16261da177e4SLinus Torvalds mp_head = (struct rtattr *)skb_put(skb, RTA_LENGTH(0)); 16271da177e4SLinus Torvalds 16281da177e4SLinus Torvalds for (ct = c->mfc_un.res.minvif; ct < c->mfc_un.res.maxvif; ct++) { 16291da177e4SLinus Torvalds if (c->mfc_un.res.ttls[ct] < 255) { 16301da177e4SLinus Torvalds if (skb_tailroom(skb) < RTA_ALIGN(RTA_ALIGN(sizeof(*nhp)) + 4)) 16311da177e4SLinus Torvalds goto rtattr_failure; 16321da177e4SLinus Torvalds nhp = (struct rtnexthop *)skb_put(skb, RTA_ALIGN(sizeof(*nhp))); 16331da177e4SLinus Torvalds nhp->rtnh_flags = 0; 16341da177e4SLinus Torvalds nhp->rtnh_hops = c->mfc_un.res.ttls[ct]; 16354feb88e5SBenjamin Thery nhp->rtnh_ifindex = net->ipv4.vif_table[ct].dev->ifindex; 16361da177e4SLinus Torvalds nhp->rtnh_len = sizeof(*nhp); 16371da177e4SLinus Torvalds } 16381da177e4SLinus Torvalds } 16391da177e4SLinus Torvalds mp_head->rta_type = RTA_MULTIPATH; 164027a884dcSArnaldo Carvalho de Melo mp_head->rta_len = skb_tail_pointer(skb) - (u8 *)mp_head; 16411da177e4SLinus Torvalds rtm->rtm_type = RTN_MULTICAST; 16421da177e4SLinus Torvalds return 1; 16431da177e4SLinus Torvalds 16441da177e4SLinus Torvalds rtattr_failure: 1645dc5fc579SArnaldo Carvalho de Melo nlmsg_trim(skb, b); 16461da177e4SLinus Torvalds return -EMSGSIZE; 16471da177e4SLinus Torvalds } 16481da177e4SLinus Torvalds 16494feb88e5SBenjamin Thery int ipmr_get_route(struct net *net, 16504feb88e5SBenjamin Thery struct sk_buff *skb, struct rtmsg *rtm, int nowait) 16511da177e4SLinus Torvalds { 16521da177e4SLinus Torvalds int err; 16531da177e4SLinus Torvalds struct mfc_cache *cache; 1654511c3f92SEric Dumazet struct rtable *rt = skb_rtable(skb); 16551da177e4SLinus Torvalds 16561da177e4SLinus Torvalds read_lock(&mrt_lock); 16574feb88e5SBenjamin Thery cache = ipmr_cache_find(net, rt->rt_src, rt->rt_dst); 16581da177e4SLinus Torvalds 16591da177e4SLinus Torvalds if (cache == NULL) { 166072287490SAlexey Kuznetsov struct sk_buff *skb2; 1661eddc9ec5SArnaldo Carvalho de Melo struct iphdr *iph; 16621da177e4SLinus Torvalds struct net_device *dev; 16631da177e4SLinus Torvalds int vif; 16641da177e4SLinus Torvalds 16651da177e4SLinus Torvalds if (nowait) { 16661da177e4SLinus Torvalds read_unlock(&mrt_lock); 16671da177e4SLinus Torvalds return -EAGAIN; 16681da177e4SLinus Torvalds } 16691da177e4SLinus Torvalds 16701da177e4SLinus Torvalds dev = skb->dev; 16711da177e4SLinus Torvalds if (dev == NULL || (vif = ipmr_find_vif(dev)) < 0) { 16721da177e4SLinus Torvalds read_unlock(&mrt_lock); 16731da177e4SLinus Torvalds return -ENODEV; 16741da177e4SLinus Torvalds } 167572287490SAlexey Kuznetsov skb2 = skb_clone(skb, GFP_ATOMIC); 167672287490SAlexey Kuznetsov if (!skb2) { 167772287490SAlexey Kuznetsov read_unlock(&mrt_lock); 167872287490SAlexey Kuznetsov return -ENOMEM; 167972287490SAlexey Kuznetsov } 168072287490SAlexey Kuznetsov 1681e2d1bca7SArnaldo Carvalho de Melo skb_push(skb2, sizeof(struct iphdr)); 1682e2d1bca7SArnaldo Carvalho de Melo skb_reset_network_header(skb2); 1683eddc9ec5SArnaldo Carvalho de Melo iph = ip_hdr(skb2); 1684eddc9ec5SArnaldo Carvalho de Melo iph->ihl = sizeof(struct iphdr) >> 2; 1685eddc9ec5SArnaldo Carvalho de Melo iph->saddr = rt->rt_src; 1686eddc9ec5SArnaldo Carvalho de Melo iph->daddr = rt->rt_dst; 1687eddc9ec5SArnaldo Carvalho de Melo iph->version = 0; 16884feb88e5SBenjamin Thery err = ipmr_cache_unresolved(net, vif, skb2); 16891da177e4SLinus Torvalds read_unlock(&mrt_lock); 16901da177e4SLinus Torvalds return err; 16911da177e4SLinus Torvalds } 16921da177e4SLinus Torvalds 16931da177e4SLinus Torvalds if (!nowait && (rtm->rtm_flags&RTM_F_NOTIFY)) 16941da177e4SLinus Torvalds cache->mfc_flags |= MFC_NOTIFY; 16951da177e4SLinus Torvalds err = ipmr_fill_mroute(skb, cache, rtm); 16961da177e4SLinus Torvalds read_unlock(&mrt_lock); 16971da177e4SLinus Torvalds return err; 16981da177e4SLinus Torvalds } 16991da177e4SLinus Torvalds 17001da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 17011da177e4SLinus Torvalds /* 17021da177e4SLinus Torvalds * The /proc interfaces to multicast routing /proc/ip_mr_cache /proc/ip_mr_vif 17031da177e4SLinus Torvalds */ 17041da177e4SLinus Torvalds struct ipmr_vif_iter { 1705f6bb4514SBenjamin Thery struct seq_net_private p; 17061da177e4SLinus Torvalds int ct; 17071da177e4SLinus Torvalds }; 17081da177e4SLinus Torvalds 1709f6bb4514SBenjamin Thery static struct vif_device *ipmr_vif_seq_idx(struct net *net, 1710f6bb4514SBenjamin Thery struct ipmr_vif_iter *iter, 17111da177e4SLinus Torvalds loff_t pos) 17121da177e4SLinus Torvalds { 1713f6bb4514SBenjamin Thery for (iter->ct = 0; iter->ct < net->ipv4.maxvif; ++iter->ct) { 1714f6bb4514SBenjamin Thery if (!VIF_EXISTS(net, iter->ct)) 17151da177e4SLinus Torvalds continue; 17161da177e4SLinus Torvalds if (pos-- == 0) 1717f6bb4514SBenjamin Thery return &net->ipv4.vif_table[iter->ct]; 17181da177e4SLinus Torvalds } 17191da177e4SLinus Torvalds return NULL; 17201da177e4SLinus Torvalds } 17211da177e4SLinus Torvalds 17221da177e4SLinus Torvalds static void *ipmr_vif_seq_start(struct seq_file *seq, loff_t *pos) 1723ba93ef74SStephen Hemminger __acquires(mrt_lock) 17241da177e4SLinus Torvalds { 1725f6bb4514SBenjamin Thery struct net *net = seq_file_net(seq); 1726f6bb4514SBenjamin Thery 17271da177e4SLinus Torvalds read_lock(&mrt_lock); 1728f6bb4514SBenjamin Thery return *pos ? ipmr_vif_seq_idx(net, seq->private, *pos - 1) 17291da177e4SLinus Torvalds : SEQ_START_TOKEN; 17301da177e4SLinus Torvalds } 17311da177e4SLinus Torvalds 17321da177e4SLinus Torvalds static void *ipmr_vif_seq_next(struct seq_file *seq, void *v, loff_t *pos) 17331da177e4SLinus Torvalds { 17341da177e4SLinus Torvalds struct ipmr_vif_iter *iter = seq->private; 1735f6bb4514SBenjamin Thery struct net *net = seq_file_net(seq); 17361da177e4SLinus Torvalds 17371da177e4SLinus Torvalds ++*pos; 17381da177e4SLinus Torvalds if (v == SEQ_START_TOKEN) 1739f6bb4514SBenjamin Thery return ipmr_vif_seq_idx(net, iter, 0); 17401da177e4SLinus Torvalds 1741f6bb4514SBenjamin Thery while (++iter->ct < net->ipv4.maxvif) { 1742f6bb4514SBenjamin Thery if (!VIF_EXISTS(net, iter->ct)) 17431da177e4SLinus Torvalds continue; 1744f6bb4514SBenjamin Thery return &net->ipv4.vif_table[iter->ct]; 17451da177e4SLinus Torvalds } 17461da177e4SLinus Torvalds return NULL; 17471da177e4SLinus Torvalds } 17481da177e4SLinus Torvalds 17491da177e4SLinus Torvalds static void ipmr_vif_seq_stop(struct seq_file *seq, void *v) 1750ba93ef74SStephen Hemminger __releases(mrt_lock) 17511da177e4SLinus Torvalds { 17521da177e4SLinus Torvalds read_unlock(&mrt_lock); 17531da177e4SLinus Torvalds } 17541da177e4SLinus Torvalds 17551da177e4SLinus Torvalds static int ipmr_vif_seq_show(struct seq_file *seq, void *v) 17561da177e4SLinus Torvalds { 1757f6bb4514SBenjamin Thery struct net *net = seq_file_net(seq); 1758f6bb4514SBenjamin Thery 17591da177e4SLinus Torvalds if (v == SEQ_START_TOKEN) { 17601da177e4SLinus Torvalds seq_puts(seq, 17611da177e4SLinus Torvalds "Interface BytesIn PktsIn BytesOut PktsOut Flags Local Remote\n"); 17621da177e4SLinus Torvalds } else { 17631da177e4SLinus Torvalds const struct vif_device *vif = v; 17641da177e4SLinus Torvalds const char *name = vif->dev ? vif->dev->name : "none"; 17651da177e4SLinus Torvalds 17661da177e4SLinus Torvalds seq_printf(seq, 17671da177e4SLinus Torvalds "%2Zd %-10s %8ld %7ld %8ld %7ld %05X %08X %08X\n", 1768f6bb4514SBenjamin Thery vif - net->ipv4.vif_table, 17691da177e4SLinus Torvalds name, vif->bytes_in, vif->pkt_in, 17701da177e4SLinus Torvalds vif->bytes_out, vif->pkt_out, 17711da177e4SLinus Torvalds vif->flags, vif->local, vif->remote); 17721da177e4SLinus Torvalds } 17731da177e4SLinus Torvalds return 0; 17741da177e4SLinus Torvalds } 17751da177e4SLinus Torvalds 1776f690808eSStephen Hemminger static const struct seq_operations ipmr_vif_seq_ops = { 17771da177e4SLinus Torvalds .start = ipmr_vif_seq_start, 17781da177e4SLinus Torvalds .next = ipmr_vif_seq_next, 17791da177e4SLinus Torvalds .stop = ipmr_vif_seq_stop, 17801da177e4SLinus Torvalds .show = ipmr_vif_seq_show, 17811da177e4SLinus Torvalds }; 17821da177e4SLinus Torvalds 17831da177e4SLinus Torvalds static int ipmr_vif_open(struct inode *inode, struct file *file) 17841da177e4SLinus Torvalds { 1785f6bb4514SBenjamin Thery return seq_open_net(inode, file, &ipmr_vif_seq_ops, 1786cf7732e4SPavel Emelyanov sizeof(struct ipmr_vif_iter)); 17871da177e4SLinus Torvalds } 17881da177e4SLinus Torvalds 17899a32144eSArjan van de Ven static const struct file_operations ipmr_vif_fops = { 17901da177e4SLinus Torvalds .owner = THIS_MODULE, 17911da177e4SLinus Torvalds .open = ipmr_vif_open, 17921da177e4SLinus Torvalds .read = seq_read, 17931da177e4SLinus Torvalds .llseek = seq_lseek, 1794f6bb4514SBenjamin Thery .release = seq_release_net, 17951da177e4SLinus Torvalds }; 17961da177e4SLinus Torvalds 17971da177e4SLinus Torvalds struct ipmr_mfc_iter { 1798f6bb4514SBenjamin Thery struct seq_net_private p; 17991da177e4SLinus Torvalds struct mfc_cache **cache; 18001da177e4SLinus Torvalds int ct; 18011da177e4SLinus Torvalds }; 18021da177e4SLinus Torvalds 18031da177e4SLinus Torvalds 1804f6bb4514SBenjamin Thery static struct mfc_cache *ipmr_mfc_seq_idx(struct net *net, 1805f6bb4514SBenjamin Thery struct ipmr_mfc_iter *it, loff_t pos) 18061da177e4SLinus Torvalds { 18071da177e4SLinus Torvalds struct mfc_cache *mfc; 18081da177e4SLinus Torvalds 1809f6bb4514SBenjamin Thery it->cache = net->ipv4.mfc_cache_array; 18101da177e4SLinus Torvalds read_lock(&mrt_lock); 18111da177e4SLinus Torvalds for (it->ct = 0; it->ct < MFC_LINES; it->ct++) 1812f6bb4514SBenjamin Thery for (mfc = net->ipv4.mfc_cache_array[it->ct]; 18132bb8b26cSBenjamin Thery mfc; mfc = mfc->next) 18141da177e4SLinus Torvalds if (pos-- == 0) 18151da177e4SLinus Torvalds return mfc; 18161da177e4SLinus Torvalds read_unlock(&mrt_lock); 18171da177e4SLinus Torvalds 18181da177e4SLinus Torvalds it->cache = &mfc_unres_queue; 18191da177e4SLinus Torvalds spin_lock_bh(&mfc_unres_lock); 18201da177e4SLinus Torvalds for (mfc = mfc_unres_queue; mfc; mfc = mfc->next) 1821f6bb4514SBenjamin Thery if (net_eq(mfc_net(mfc), net) && 1822f6bb4514SBenjamin Thery pos-- == 0) 18231da177e4SLinus Torvalds return mfc; 18241da177e4SLinus Torvalds spin_unlock_bh(&mfc_unres_lock); 18251da177e4SLinus Torvalds 18261da177e4SLinus Torvalds it->cache = NULL; 18271da177e4SLinus Torvalds return NULL; 18281da177e4SLinus Torvalds } 18291da177e4SLinus Torvalds 18301da177e4SLinus Torvalds 18311da177e4SLinus Torvalds static void *ipmr_mfc_seq_start(struct seq_file *seq, loff_t *pos) 18321da177e4SLinus Torvalds { 18331da177e4SLinus Torvalds struct ipmr_mfc_iter *it = seq->private; 1834f6bb4514SBenjamin Thery struct net *net = seq_file_net(seq); 1835f6bb4514SBenjamin Thery 18361da177e4SLinus Torvalds it->cache = NULL; 18371da177e4SLinus Torvalds it->ct = 0; 1838f6bb4514SBenjamin Thery return *pos ? ipmr_mfc_seq_idx(net, seq->private, *pos - 1) 18391da177e4SLinus Torvalds : SEQ_START_TOKEN; 18401da177e4SLinus Torvalds } 18411da177e4SLinus Torvalds 18421da177e4SLinus Torvalds static void *ipmr_mfc_seq_next(struct seq_file *seq, void *v, loff_t *pos) 18431da177e4SLinus Torvalds { 18441da177e4SLinus Torvalds struct mfc_cache *mfc = v; 18451da177e4SLinus Torvalds struct ipmr_mfc_iter *it = seq->private; 1846f6bb4514SBenjamin Thery struct net *net = seq_file_net(seq); 18471da177e4SLinus Torvalds 18481da177e4SLinus Torvalds ++*pos; 18491da177e4SLinus Torvalds 18501da177e4SLinus Torvalds if (v == SEQ_START_TOKEN) 1851f6bb4514SBenjamin Thery return ipmr_mfc_seq_idx(net, seq->private, 0); 18521da177e4SLinus Torvalds 18531da177e4SLinus Torvalds if (mfc->next) 18541da177e4SLinus Torvalds return mfc->next; 18551da177e4SLinus Torvalds 18561da177e4SLinus Torvalds if (it->cache == &mfc_unres_queue) 18571da177e4SLinus Torvalds goto end_of_list; 18581da177e4SLinus Torvalds 1859f6bb4514SBenjamin Thery BUG_ON(it->cache != net->ipv4.mfc_cache_array); 18601da177e4SLinus Torvalds 18611da177e4SLinus Torvalds while (++it->ct < MFC_LINES) { 1862f6bb4514SBenjamin Thery mfc = net->ipv4.mfc_cache_array[it->ct]; 18631da177e4SLinus Torvalds if (mfc) 18641da177e4SLinus Torvalds return mfc; 18651da177e4SLinus Torvalds } 18661da177e4SLinus Torvalds 18671da177e4SLinus Torvalds /* exhausted cache_array, show unresolved */ 18681da177e4SLinus Torvalds read_unlock(&mrt_lock); 18691da177e4SLinus Torvalds it->cache = &mfc_unres_queue; 18701da177e4SLinus Torvalds it->ct = 0; 18711da177e4SLinus Torvalds 18721da177e4SLinus Torvalds spin_lock_bh(&mfc_unres_lock); 18731da177e4SLinus Torvalds mfc = mfc_unres_queue; 1874f6bb4514SBenjamin Thery while (mfc && !net_eq(mfc_net(mfc), net)) 1875f6bb4514SBenjamin Thery mfc = mfc->next; 18761da177e4SLinus Torvalds if (mfc) 18771da177e4SLinus Torvalds return mfc; 18781da177e4SLinus Torvalds 18791da177e4SLinus Torvalds end_of_list: 18801da177e4SLinus Torvalds spin_unlock_bh(&mfc_unres_lock); 18811da177e4SLinus Torvalds it->cache = NULL; 18821da177e4SLinus Torvalds 18831da177e4SLinus Torvalds return NULL; 18841da177e4SLinus Torvalds } 18851da177e4SLinus Torvalds 18861da177e4SLinus Torvalds static void ipmr_mfc_seq_stop(struct seq_file *seq, void *v) 18871da177e4SLinus Torvalds { 18881da177e4SLinus Torvalds struct ipmr_mfc_iter *it = seq->private; 1889f6bb4514SBenjamin Thery struct net *net = seq_file_net(seq); 18901da177e4SLinus Torvalds 18911da177e4SLinus Torvalds if (it->cache == &mfc_unres_queue) 18921da177e4SLinus Torvalds spin_unlock_bh(&mfc_unres_lock); 1893f6bb4514SBenjamin Thery else if (it->cache == net->ipv4.mfc_cache_array) 18941da177e4SLinus Torvalds read_unlock(&mrt_lock); 18951da177e4SLinus Torvalds } 18961da177e4SLinus Torvalds 18971da177e4SLinus Torvalds static int ipmr_mfc_seq_show(struct seq_file *seq, void *v) 18981da177e4SLinus Torvalds { 18991da177e4SLinus Torvalds int n; 1900f6bb4514SBenjamin Thery struct net *net = seq_file_net(seq); 19011da177e4SLinus Torvalds 19021da177e4SLinus Torvalds if (v == SEQ_START_TOKEN) { 19031da177e4SLinus Torvalds seq_puts(seq, 19041da177e4SLinus Torvalds "Group Origin Iif Pkts Bytes Wrong Oifs\n"); 19051da177e4SLinus Torvalds } else { 19061da177e4SLinus Torvalds const struct mfc_cache *mfc = v; 19071da177e4SLinus Torvalds const struct ipmr_mfc_iter *it = seq->private; 19081da177e4SLinus Torvalds 1909999890b2SBenjamin Thery seq_printf(seq, "%08lX %08lX %-3hd", 19101da177e4SLinus Torvalds (unsigned long) mfc->mfc_mcastgrp, 19111da177e4SLinus Torvalds (unsigned long) mfc->mfc_origin, 19121ea472e2SBenjamin Thery mfc->mfc_parent); 19131ea472e2SBenjamin Thery 19141ea472e2SBenjamin Thery if (it->cache != &mfc_unres_queue) { 19151ea472e2SBenjamin Thery seq_printf(seq, " %8lu %8lu %8lu", 19161da177e4SLinus Torvalds mfc->mfc_un.res.pkt, 19171da177e4SLinus Torvalds mfc->mfc_un.res.bytes, 19181da177e4SLinus Torvalds mfc->mfc_un.res.wrong_if); 19191da177e4SLinus Torvalds for (n = mfc->mfc_un.res.minvif; 19201da177e4SLinus Torvalds n < mfc->mfc_un.res.maxvif; n++ ) { 1921f6bb4514SBenjamin Thery if (VIF_EXISTS(net, n) && 1922cf958ae3SBenjamin Thery mfc->mfc_un.res.ttls[n] < 255) 19231da177e4SLinus Torvalds seq_printf(seq, 19241da177e4SLinus Torvalds " %2d:%-3d", 19251da177e4SLinus Torvalds n, mfc->mfc_un.res.ttls[n]); 19261da177e4SLinus Torvalds } 19271ea472e2SBenjamin Thery } else { 19281ea472e2SBenjamin Thery /* unresolved mfc_caches don't contain 19291ea472e2SBenjamin Thery * pkt, bytes and wrong_if values 19301ea472e2SBenjamin Thery */ 19311ea472e2SBenjamin Thery seq_printf(seq, " %8lu %8lu %8lu", 0ul, 0ul, 0ul); 19321da177e4SLinus Torvalds } 19331da177e4SLinus Torvalds seq_putc(seq, '\n'); 19341da177e4SLinus Torvalds } 19351da177e4SLinus Torvalds return 0; 19361da177e4SLinus Torvalds } 19371da177e4SLinus Torvalds 1938f690808eSStephen Hemminger static const struct seq_operations ipmr_mfc_seq_ops = { 19391da177e4SLinus Torvalds .start = ipmr_mfc_seq_start, 19401da177e4SLinus Torvalds .next = ipmr_mfc_seq_next, 19411da177e4SLinus Torvalds .stop = ipmr_mfc_seq_stop, 19421da177e4SLinus Torvalds .show = ipmr_mfc_seq_show, 19431da177e4SLinus Torvalds }; 19441da177e4SLinus Torvalds 19451da177e4SLinus Torvalds static int ipmr_mfc_open(struct inode *inode, struct file *file) 19461da177e4SLinus Torvalds { 1947f6bb4514SBenjamin Thery return seq_open_net(inode, file, &ipmr_mfc_seq_ops, 1948cf7732e4SPavel Emelyanov sizeof(struct ipmr_mfc_iter)); 19491da177e4SLinus Torvalds } 19501da177e4SLinus Torvalds 19519a32144eSArjan van de Ven static const struct file_operations ipmr_mfc_fops = { 19521da177e4SLinus Torvalds .owner = THIS_MODULE, 19531da177e4SLinus Torvalds .open = ipmr_mfc_open, 19541da177e4SLinus Torvalds .read = seq_read, 19551da177e4SLinus Torvalds .llseek = seq_lseek, 1956f6bb4514SBenjamin Thery .release = seq_release_net, 19571da177e4SLinus Torvalds }; 19581da177e4SLinus Torvalds #endif 19591da177e4SLinus Torvalds 19601da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM_V2 196132613090SAlexey Dobriyan static const struct net_protocol pim_protocol = { 19621da177e4SLinus Torvalds .handler = pim_rcv, 1963403dbb97STom Goff .netns_ok = 1, 19641da177e4SLinus Torvalds }; 19651da177e4SLinus Torvalds #endif 19661da177e4SLinus Torvalds 19671da177e4SLinus Torvalds 19681da177e4SLinus Torvalds /* 19691da177e4SLinus Torvalds * Setup for IP multicast routing 19701da177e4SLinus Torvalds */ 1971cf958ae3SBenjamin Thery static int __net_init ipmr_net_init(struct net *net) 1972cf958ae3SBenjamin Thery { 1973cf958ae3SBenjamin Thery int err = 0; 1974cf958ae3SBenjamin Thery 1975cf958ae3SBenjamin Thery net->ipv4.vif_table = kcalloc(MAXVIFS, sizeof(struct vif_device), 1976cf958ae3SBenjamin Thery GFP_KERNEL); 1977cf958ae3SBenjamin Thery if (!net->ipv4.vif_table) { 1978cf958ae3SBenjamin Thery err = -ENOMEM; 1979cf958ae3SBenjamin Thery goto fail; 1980cf958ae3SBenjamin Thery } 19812bb8b26cSBenjamin Thery 19822bb8b26cSBenjamin Thery /* Forwarding cache */ 19832bb8b26cSBenjamin Thery net->ipv4.mfc_cache_array = kcalloc(MFC_LINES, 19842bb8b26cSBenjamin Thery sizeof(struct mfc_cache *), 19852bb8b26cSBenjamin Thery GFP_KERNEL); 19862bb8b26cSBenjamin Thery if (!net->ipv4.mfc_cache_array) { 19872bb8b26cSBenjamin Thery err = -ENOMEM; 19882bb8b26cSBenjamin Thery goto fail_mfc_cache; 19892bb8b26cSBenjamin Thery } 19906c5143dbSBenjamin Thery 19916c5143dbSBenjamin Thery #ifdef CONFIG_IP_PIMSM 19926c5143dbSBenjamin Thery net->ipv4.mroute_reg_vif_num = -1; 19936c5143dbSBenjamin Thery #endif 1994f6bb4514SBenjamin Thery 1995f6bb4514SBenjamin Thery #ifdef CONFIG_PROC_FS 1996f6bb4514SBenjamin Thery err = -ENOMEM; 1997f6bb4514SBenjamin Thery if (!proc_net_fops_create(net, "ip_mr_vif", 0, &ipmr_vif_fops)) 1998f6bb4514SBenjamin Thery goto proc_vif_fail; 1999f6bb4514SBenjamin Thery if (!proc_net_fops_create(net, "ip_mr_cache", 0, &ipmr_mfc_fops)) 2000f6bb4514SBenjamin Thery goto proc_cache_fail; 2001f6bb4514SBenjamin Thery #endif 20022bb8b26cSBenjamin Thery return 0; 20032bb8b26cSBenjamin Thery 2004f6bb4514SBenjamin Thery #ifdef CONFIG_PROC_FS 2005f6bb4514SBenjamin Thery proc_cache_fail: 2006f6bb4514SBenjamin Thery proc_net_remove(net, "ip_mr_vif"); 2007f6bb4514SBenjamin Thery proc_vif_fail: 2008f6bb4514SBenjamin Thery kfree(net->ipv4.mfc_cache_array); 2009f6bb4514SBenjamin Thery #endif 20102bb8b26cSBenjamin Thery fail_mfc_cache: 20112bb8b26cSBenjamin Thery kfree(net->ipv4.vif_table); 2012cf958ae3SBenjamin Thery fail: 2013cf958ae3SBenjamin Thery return err; 2014cf958ae3SBenjamin Thery } 2015cf958ae3SBenjamin Thery 2016cf958ae3SBenjamin Thery static void __net_exit ipmr_net_exit(struct net *net) 2017cf958ae3SBenjamin Thery { 2018f6bb4514SBenjamin Thery #ifdef CONFIG_PROC_FS 2019f6bb4514SBenjamin Thery proc_net_remove(net, "ip_mr_cache"); 2020f6bb4514SBenjamin Thery proc_net_remove(net, "ip_mr_vif"); 2021f6bb4514SBenjamin Thery #endif 20222bb8b26cSBenjamin Thery kfree(net->ipv4.mfc_cache_array); 2023cf958ae3SBenjamin Thery kfree(net->ipv4.vif_table); 2024cf958ae3SBenjamin Thery } 2025cf958ae3SBenjamin Thery 2026cf958ae3SBenjamin Thery static struct pernet_operations ipmr_net_ops = { 2027cf958ae3SBenjamin Thery .init = ipmr_net_init, 2028cf958ae3SBenjamin Thery .exit = ipmr_net_exit, 2029cf958ae3SBenjamin Thery }; 20301da177e4SLinus Torvalds 203103d2f897SWang Chen int __init ip_mr_init(void) 20321da177e4SLinus Torvalds { 203303d2f897SWang Chen int err; 203403d2f897SWang Chen 20351da177e4SLinus Torvalds mrt_cachep = kmem_cache_create("ip_mrt_cache", 20361da177e4SLinus Torvalds sizeof(struct mfc_cache), 2037e5d679f3SAlexey Dobriyan 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, 203820c2df83SPaul Mundt NULL); 203903d2f897SWang Chen if (!mrt_cachep) 204003d2f897SWang Chen return -ENOMEM; 204103d2f897SWang Chen 2042cf958ae3SBenjamin Thery err = register_pernet_subsys(&ipmr_net_ops); 2043cf958ae3SBenjamin Thery if (err) 2044cf958ae3SBenjamin Thery goto reg_pernet_fail; 2045cf958ae3SBenjamin Thery 2046b24b8a24SPavel Emelyanov setup_timer(&ipmr_expire_timer, ipmr_expire_process, 0); 204703d2f897SWang Chen err = register_netdevice_notifier(&ip_mr_notifier); 204803d2f897SWang Chen if (err) 204903d2f897SWang Chen goto reg_notif_fail; 2050403dbb97STom Goff #ifdef CONFIG_IP_PIMSM_V2 2051403dbb97STom Goff if (inet_add_protocol(&pim_protocol, IPPROTO_PIM) < 0) { 2052403dbb97STom Goff printk(KERN_ERR "ip_mr_init: can't add PIM protocol\n"); 2053403dbb97STom Goff err = -EAGAIN; 2054403dbb97STom Goff goto add_proto_fail; 2055403dbb97STom Goff } 2056403dbb97STom Goff #endif 205703d2f897SWang Chen return 0; 2058f6bb4514SBenjamin Thery 2059403dbb97STom Goff #ifdef CONFIG_IP_PIMSM_V2 2060403dbb97STom Goff add_proto_fail: 2061403dbb97STom Goff unregister_netdevice_notifier(&ip_mr_notifier); 2062403dbb97STom Goff #endif 2063c3e38896SBenjamin Thery reg_notif_fail: 2064c3e38896SBenjamin Thery del_timer(&ipmr_expire_timer); 2065cf958ae3SBenjamin Thery unregister_pernet_subsys(&ipmr_net_ops); 2066cf958ae3SBenjamin Thery reg_pernet_fail: 2067c3e38896SBenjamin Thery kmem_cache_destroy(mrt_cachep); 206803d2f897SWang Chen return err; 20691da177e4SLinus Torvalds } 2070