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> 505a0e3ad6STejun Heo #include <linux/slab.h> 51457c4cbcSEric W. Biederman #include <net/net_namespace.h> 521da177e4SLinus Torvalds #include <net/ip.h> 531da177e4SLinus Torvalds #include <net/protocol.h> 541da177e4SLinus Torvalds #include <linux/skbuff.h> 5514c85021SArnaldo Carvalho de Melo #include <net/route.h> 561da177e4SLinus Torvalds #include <net/sock.h> 571da177e4SLinus Torvalds #include <net/icmp.h> 581da177e4SLinus Torvalds #include <net/udp.h> 591da177e4SLinus Torvalds #include <net/raw.h> 601da177e4SLinus Torvalds #include <linux/notifier.h> 611da177e4SLinus Torvalds #include <linux/if_arp.h> 621da177e4SLinus Torvalds #include <linux/netfilter_ipv4.h> 631da177e4SLinus Torvalds #include <net/ipip.h> 641da177e4SLinus Torvalds #include <net/checksum.h> 65dc5fc579SArnaldo Carvalho de Melo #include <net/netlink.h> 66f0ad0860SPatrick McHardy #include <net/fib_rules.h> 671da177e4SLinus Torvalds 681da177e4SLinus Torvalds #if defined(CONFIG_IP_PIMSM_V1) || defined(CONFIG_IP_PIMSM_V2) 691da177e4SLinus Torvalds #define CONFIG_IP_PIMSM 1 701da177e4SLinus Torvalds #endif 711da177e4SLinus Torvalds 720c12295aSPatrick McHardy struct mr_table { 73f0ad0860SPatrick McHardy struct list_head list; 74f0ad0860SPatrick McHardy u32 id; 750c12295aSPatrick McHardy struct sock *mroute_sk; 760c12295aSPatrick McHardy struct timer_list ipmr_expire_timer; 770c12295aSPatrick McHardy struct list_head mfc_unres_queue; 780c12295aSPatrick McHardy struct list_head mfc_cache_array[MFC_LINES]; 790c12295aSPatrick McHardy struct vif_device vif_table[MAXVIFS]; 800c12295aSPatrick McHardy int maxvif; 810c12295aSPatrick McHardy atomic_t cache_resolve_queue_len; 820c12295aSPatrick McHardy int mroute_do_assert; 830c12295aSPatrick McHardy int mroute_do_pim; 840c12295aSPatrick McHardy #if defined(CONFIG_IP_PIMSM_V1) || defined(CONFIG_IP_PIMSM_V2) 850c12295aSPatrick McHardy int mroute_reg_vif_num; 860c12295aSPatrick McHardy #endif 870c12295aSPatrick McHardy }; 880c12295aSPatrick McHardy 89f0ad0860SPatrick McHardy struct ipmr_rule { 90f0ad0860SPatrick McHardy struct fib_rule common; 91f0ad0860SPatrick McHardy }; 92f0ad0860SPatrick McHardy 93f0ad0860SPatrick McHardy struct ipmr_result { 94f0ad0860SPatrick McHardy struct mr_table *mrt; 95f0ad0860SPatrick McHardy }; 96f0ad0860SPatrick McHardy 971da177e4SLinus Torvalds /* Big lock, protecting vif table, mrt cache and mroute socket state. 981da177e4SLinus Torvalds Note that the changes are semaphored via rtnl_lock. 991da177e4SLinus Torvalds */ 1001da177e4SLinus Torvalds 1011da177e4SLinus Torvalds static DEFINE_RWLOCK(mrt_lock); 1021da177e4SLinus Torvalds 1031da177e4SLinus Torvalds /* 1041da177e4SLinus Torvalds * Multicast router control variables 1051da177e4SLinus Torvalds */ 1061da177e4SLinus Torvalds 1070c12295aSPatrick McHardy #define VIF_EXISTS(_mrt, _idx) ((_mrt)->vif_table[_idx].dev != NULL) 1081da177e4SLinus Torvalds 1091da177e4SLinus Torvalds /* Special spinlock for queue of unresolved entries */ 1101da177e4SLinus Torvalds static DEFINE_SPINLOCK(mfc_unres_lock); 1111da177e4SLinus Torvalds 1121da177e4SLinus Torvalds /* We return to original Alan's scheme. Hash table of resolved 1131da177e4SLinus Torvalds entries is changed only in process context and protected 1141da177e4SLinus Torvalds with weak lock mrt_lock. Queue of unresolved entries is protected 1151da177e4SLinus Torvalds with strong spinlock mfc_unres_lock. 1161da177e4SLinus Torvalds 1171da177e4SLinus Torvalds In this case data path is free of exclusive locks at all. 1181da177e4SLinus Torvalds */ 1191da177e4SLinus Torvalds 120e18b890bSChristoph Lameter static struct kmem_cache *mrt_cachep __read_mostly; 1211da177e4SLinus Torvalds 122f0ad0860SPatrick McHardy static struct mr_table *ipmr_new_table(struct net *net, u32 id); 1230c12295aSPatrick McHardy static int ip_mr_forward(struct net *net, struct mr_table *mrt, 1240c12295aSPatrick McHardy struct sk_buff *skb, struct mfc_cache *cache, 1250c12295aSPatrick McHardy int local); 1260c12295aSPatrick McHardy static int ipmr_cache_report(struct mr_table *mrt, 1274feb88e5SBenjamin Thery struct sk_buff *pkt, vifi_t vifi, int assert); 1280c12295aSPatrick McHardy static int ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb, 129d658f8a0SPatrick McHardy struct mfc_cache *c, struct rtmsg *rtm); 130f0ad0860SPatrick McHardy static void ipmr_expire_process(unsigned long arg); 131f0ad0860SPatrick McHardy 132f0ad0860SPatrick McHardy #ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES 133f0ad0860SPatrick McHardy #define ipmr_for_each_table(mrt, net) \ 134f0ad0860SPatrick McHardy list_for_each_entry_rcu(mrt, &net->ipv4.mr_tables, list) 135f0ad0860SPatrick McHardy 136f0ad0860SPatrick McHardy static struct mr_table *ipmr_get_table(struct net *net, u32 id) 137f0ad0860SPatrick McHardy { 138f0ad0860SPatrick McHardy struct mr_table *mrt; 139f0ad0860SPatrick McHardy 140f0ad0860SPatrick McHardy ipmr_for_each_table(mrt, net) { 141f0ad0860SPatrick McHardy if (mrt->id == id) 142f0ad0860SPatrick McHardy return mrt; 143f0ad0860SPatrick McHardy } 144f0ad0860SPatrick McHardy return NULL; 145f0ad0860SPatrick McHardy } 146f0ad0860SPatrick McHardy 147f0ad0860SPatrick McHardy static int ipmr_fib_lookup(struct net *net, struct flowi *flp, 148f0ad0860SPatrick McHardy struct mr_table **mrt) 149f0ad0860SPatrick McHardy { 150f0ad0860SPatrick McHardy struct ipmr_result res; 151f0ad0860SPatrick McHardy struct fib_lookup_arg arg = { .result = &res, }; 152f0ad0860SPatrick McHardy int err; 153f0ad0860SPatrick McHardy 154f0ad0860SPatrick McHardy err = fib_rules_lookup(net->ipv4.mr_rules_ops, flp, 0, &arg); 155f0ad0860SPatrick McHardy if (err < 0) 156f0ad0860SPatrick McHardy return err; 157f0ad0860SPatrick McHardy *mrt = res.mrt; 158f0ad0860SPatrick McHardy return 0; 159f0ad0860SPatrick McHardy } 160f0ad0860SPatrick McHardy 161f0ad0860SPatrick McHardy static int ipmr_rule_action(struct fib_rule *rule, struct flowi *flp, 162f0ad0860SPatrick McHardy int flags, struct fib_lookup_arg *arg) 163f0ad0860SPatrick McHardy { 164f0ad0860SPatrick McHardy struct ipmr_result *res = arg->result; 165f0ad0860SPatrick McHardy struct mr_table *mrt; 166f0ad0860SPatrick McHardy 167f0ad0860SPatrick McHardy switch (rule->action) { 168f0ad0860SPatrick McHardy case FR_ACT_TO_TBL: 169f0ad0860SPatrick McHardy break; 170f0ad0860SPatrick McHardy case FR_ACT_UNREACHABLE: 171f0ad0860SPatrick McHardy return -ENETUNREACH; 172f0ad0860SPatrick McHardy case FR_ACT_PROHIBIT: 173f0ad0860SPatrick McHardy return -EACCES; 174f0ad0860SPatrick McHardy case FR_ACT_BLACKHOLE: 175f0ad0860SPatrick McHardy default: 176f0ad0860SPatrick McHardy return -EINVAL; 177f0ad0860SPatrick McHardy } 178f0ad0860SPatrick McHardy 179f0ad0860SPatrick McHardy mrt = ipmr_get_table(rule->fr_net, rule->table); 180f0ad0860SPatrick McHardy if (mrt == NULL) 181f0ad0860SPatrick McHardy return -EAGAIN; 182f0ad0860SPatrick McHardy res->mrt = mrt; 183f0ad0860SPatrick McHardy return 0; 184f0ad0860SPatrick McHardy } 185f0ad0860SPatrick McHardy 186f0ad0860SPatrick McHardy static int ipmr_rule_match(struct fib_rule *rule, struct flowi *fl, int flags) 187f0ad0860SPatrick McHardy { 188f0ad0860SPatrick McHardy return 1; 189f0ad0860SPatrick McHardy } 190f0ad0860SPatrick McHardy 191f0ad0860SPatrick McHardy static const struct nla_policy ipmr_rule_policy[FRA_MAX + 1] = { 192f0ad0860SPatrick McHardy FRA_GENERIC_POLICY, 193f0ad0860SPatrick McHardy }; 194f0ad0860SPatrick McHardy 195f0ad0860SPatrick McHardy static int ipmr_rule_configure(struct fib_rule *rule, struct sk_buff *skb, 196f0ad0860SPatrick McHardy struct fib_rule_hdr *frh, struct nlattr **tb) 197f0ad0860SPatrick McHardy { 198f0ad0860SPatrick McHardy return 0; 199f0ad0860SPatrick McHardy } 200f0ad0860SPatrick McHardy 201f0ad0860SPatrick McHardy static int ipmr_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh, 202f0ad0860SPatrick McHardy struct nlattr **tb) 203f0ad0860SPatrick McHardy { 204f0ad0860SPatrick McHardy return 1; 205f0ad0860SPatrick McHardy } 206f0ad0860SPatrick McHardy 207f0ad0860SPatrick McHardy static int ipmr_rule_fill(struct fib_rule *rule, struct sk_buff *skb, 208f0ad0860SPatrick McHardy struct fib_rule_hdr *frh) 209f0ad0860SPatrick McHardy { 210f0ad0860SPatrick McHardy frh->dst_len = 0; 211f0ad0860SPatrick McHardy frh->src_len = 0; 212f0ad0860SPatrick McHardy frh->tos = 0; 213f0ad0860SPatrick McHardy return 0; 214f0ad0860SPatrick McHardy } 215f0ad0860SPatrick McHardy 216f0ad0860SPatrick McHardy static struct fib_rules_ops ipmr_rules_ops_template = { 217f0ad0860SPatrick McHardy .family = FIB_RULES_IPMR, 218f0ad0860SPatrick McHardy .rule_size = sizeof(struct ipmr_rule), 219f0ad0860SPatrick McHardy .addr_size = sizeof(u32), 220f0ad0860SPatrick McHardy .action = ipmr_rule_action, 221f0ad0860SPatrick McHardy .match = ipmr_rule_match, 222f0ad0860SPatrick McHardy .configure = ipmr_rule_configure, 223f0ad0860SPatrick McHardy .compare = ipmr_rule_compare, 224f0ad0860SPatrick McHardy .default_pref = fib_default_rule_pref, 225f0ad0860SPatrick McHardy .fill = ipmr_rule_fill, 226f0ad0860SPatrick McHardy .nlgroup = RTNLGRP_IPV4_RULE, 227f0ad0860SPatrick McHardy .policy = ipmr_rule_policy, 228f0ad0860SPatrick McHardy .owner = THIS_MODULE, 229f0ad0860SPatrick McHardy }; 230f0ad0860SPatrick McHardy 231f0ad0860SPatrick McHardy static int __net_init ipmr_rules_init(struct net *net) 232f0ad0860SPatrick McHardy { 233f0ad0860SPatrick McHardy struct fib_rules_ops *ops; 234f0ad0860SPatrick McHardy struct mr_table *mrt; 235f0ad0860SPatrick McHardy int err; 236f0ad0860SPatrick McHardy 237f0ad0860SPatrick McHardy ops = fib_rules_register(&ipmr_rules_ops_template, net); 238f0ad0860SPatrick McHardy if (IS_ERR(ops)) 239f0ad0860SPatrick McHardy return PTR_ERR(ops); 240f0ad0860SPatrick McHardy 241f0ad0860SPatrick McHardy INIT_LIST_HEAD(&net->ipv4.mr_tables); 242f0ad0860SPatrick McHardy 243f0ad0860SPatrick McHardy mrt = ipmr_new_table(net, RT_TABLE_DEFAULT); 244f0ad0860SPatrick McHardy if (mrt == NULL) { 245f0ad0860SPatrick McHardy err = -ENOMEM; 246f0ad0860SPatrick McHardy goto err1; 247f0ad0860SPatrick McHardy } 248f0ad0860SPatrick McHardy 249f0ad0860SPatrick McHardy err = fib_default_rule_add(ops, 0x7fff, RT_TABLE_DEFAULT, 0); 250f0ad0860SPatrick McHardy if (err < 0) 251f0ad0860SPatrick McHardy goto err2; 252f0ad0860SPatrick McHardy 253f0ad0860SPatrick McHardy net->ipv4.mr_rules_ops = ops; 254f0ad0860SPatrick McHardy return 0; 255f0ad0860SPatrick McHardy 256f0ad0860SPatrick McHardy err2: 257f0ad0860SPatrick McHardy kfree(mrt); 258f0ad0860SPatrick McHardy err1: 259f0ad0860SPatrick McHardy fib_rules_unregister(ops); 260f0ad0860SPatrick McHardy return err; 261f0ad0860SPatrick McHardy } 262f0ad0860SPatrick McHardy 263f0ad0860SPatrick McHardy static void __net_exit ipmr_rules_exit(struct net *net) 264f0ad0860SPatrick McHardy { 265f0ad0860SPatrick McHardy struct mr_table *mrt, *next; 266f0ad0860SPatrick McHardy 267f0ad0860SPatrick McHardy list_for_each_entry_safe(mrt, next, &net->ipv4.mr_tables, list) 268f0ad0860SPatrick McHardy kfree(mrt); 269f0ad0860SPatrick McHardy fib_rules_unregister(net->ipv4.mr_rules_ops); 270f0ad0860SPatrick McHardy } 271f0ad0860SPatrick McHardy #else 272f0ad0860SPatrick McHardy #define ipmr_for_each_table(mrt, net) \ 273f0ad0860SPatrick McHardy for (mrt = net->ipv4.mrt; mrt; mrt = NULL) 274f0ad0860SPatrick McHardy 275f0ad0860SPatrick McHardy static struct mr_table *ipmr_get_table(struct net *net, u32 id) 276f0ad0860SPatrick McHardy { 277f0ad0860SPatrick McHardy return net->ipv4.mrt; 278f0ad0860SPatrick McHardy } 279f0ad0860SPatrick McHardy 280f0ad0860SPatrick McHardy static int ipmr_fib_lookup(struct net *net, struct flowi *flp, 281f0ad0860SPatrick McHardy struct mr_table **mrt) 282f0ad0860SPatrick McHardy { 283f0ad0860SPatrick McHardy *mrt = net->ipv4.mrt; 284f0ad0860SPatrick McHardy return 0; 285f0ad0860SPatrick McHardy } 286f0ad0860SPatrick McHardy 287f0ad0860SPatrick McHardy static int __net_init ipmr_rules_init(struct net *net) 288f0ad0860SPatrick McHardy { 289f0ad0860SPatrick McHardy net->ipv4.mrt = ipmr_new_table(net, RT_TABLE_DEFAULT); 290f0ad0860SPatrick McHardy return net->ipv4.mrt ? 0 : -ENOMEM; 291f0ad0860SPatrick McHardy } 292f0ad0860SPatrick McHardy 293f0ad0860SPatrick McHardy static void __net_exit ipmr_rules_exit(struct net *net) 294f0ad0860SPatrick McHardy { 295f0ad0860SPatrick McHardy kfree(net->ipv4.mrt); 296f0ad0860SPatrick McHardy } 297f0ad0860SPatrick McHardy #endif 298f0ad0860SPatrick McHardy 299f0ad0860SPatrick McHardy static struct mr_table *ipmr_new_table(struct net *net, u32 id) 300f0ad0860SPatrick McHardy { 301f0ad0860SPatrick McHardy struct mr_table *mrt; 302f0ad0860SPatrick McHardy unsigned int i; 303f0ad0860SPatrick McHardy 304f0ad0860SPatrick McHardy mrt = ipmr_get_table(net, id); 305f0ad0860SPatrick McHardy if (mrt != NULL) 306f0ad0860SPatrick McHardy return mrt; 307f0ad0860SPatrick McHardy 308f0ad0860SPatrick McHardy mrt = kzalloc(sizeof(*mrt), GFP_KERNEL); 309f0ad0860SPatrick McHardy if (mrt == NULL) 310f0ad0860SPatrick McHardy return NULL; 311f0ad0860SPatrick McHardy mrt->id = id; 312f0ad0860SPatrick McHardy 313f0ad0860SPatrick McHardy /* Forwarding cache */ 314f0ad0860SPatrick McHardy for (i = 0; i < MFC_LINES; i++) 315f0ad0860SPatrick McHardy INIT_LIST_HEAD(&mrt->mfc_cache_array[i]); 316f0ad0860SPatrick McHardy 317f0ad0860SPatrick McHardy INIT_LIST_HEAD(&mrt->mfc_unres_queue); 318f0ad0860SPatrick McHardy 319f0ad0860SPatrick McHardy setup_timer(&mrt->ipmr_expire_timer, ipmr_expire_process, 320f0ad0860SPatrick McHardy (unsigned long)mrt); 321f0ad0860SPatrick McHardy 322f0ad0860SPatrick McHardy #ifdef CONFIG_IP_PIMSM 323f0ad0860SPatrick McHardy mrt->mroute_reg_vif_num = -1; 324f0ad0860SPatrick McHardy #endif 325f0ad0860SPatrick McHardy #ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES 326f0ad0860SPatrick McHardy list_add_tail_rcu(&mrt->list, &net->ipv4.mr_tables); 327f0ad0860SPatrick McHardy #endif 328f0ad0860SPatrick McHardy return mrt; 329f0ad0860SPatrick McHardy } 3301da177e4SLinus Torvalds 3311da177e4SLinus Torvalds /* Service routines creating virtual interfaces: DVMRP tunnels and PIMREG */ 3321da177e4SLinus Torvalds 333d607032dSWang Chen static void ipmr_del_tunnel(struct net_device *dev, struct vifctl *v) 334d607032dSWang Chen { 3354feb88e5SBenjamin Thery struct net *net = dev_net(dev); 3364feb88e5SBenjamin Thery 337d607032dSWang Chen dev_close(dev); 338d607032dSWang Chen 3394feb88e5SBenjamin Thery dev = __dev_get_by_name(net, "tunl0"); 340d607032dSWang Chen if (dev) { 3415bc3eb7eSStephen Hemminger const struct net_device_ops *ops = dev->netdev_ops; 342d607032dSWang Chen struct ifreq ifr; 343d607032dSWang Chen struct ip_tunnel_parm p; 344d607032dSWang Chen 345d607032dSWang Chen memset(&p, 0, sizeof(p)); 346d607032dSWang Chen p.iph.daddr = v->vifc_rmt_addr.s_addr; 347d607032dSWang Chen p.iph.saddr = v->vifc_lcl_addr.s_addr; 348d607032dSWang Chen p.iph.version = 4; 349d607032dSWang Chen p.iph.ihl = 5; 350d607032dSWang Chen p.iph.protocol = IPPROTO_IPIP; 351d607032dSWang Chen sprintf(p.name, "dvmrp%d", v->vifc_vifi); 352d607032dSWang Chen ifr.ifr_ifru.ifru_data = (__force void __user *)&p; 353d607032dSWang Chen 3545bc3eb7eSStephen Hemminger if (ops->ndo_do_ioctl) { 3555bc3eb7eSStephen Hemminger mm_segment_t oldfs = get_fs(); 3565bc3eb7eSStephen Hemminger 3575bc3eb7eSStephen Hemminger set_fs(KERNEL_DS); 3585bc3eb7eSStephen Hemminger ops->ndo_do_ioctl(dev, &ifr, SIOCDELTUNNEL); 359d607032dSWang Chen set_fs(oldfs); 360d607032dSWang Chen } 361d607032dSWang Chen } 3625bc3eb7eSStephen Hemminger } 363d607032dSWang Chen 3641da177e4SLinus Torvalds static 3654feb88e5SBenjamin Thery struct net_device *ipmr_new_tunnel(struct net *net, struct vifctl *v) 3661da177e4SLinus Torvalds { 3671da177e4SLinus Torvalds struct net_device *dev; 3681da177e4SLinus Torvalds 3694feb88e5SBenjamin Thery dev = __dev_get_by_name(net, "tunl0"); 3701da177e4SLinus Torvalds 3711da177e4SLinus Torvalds if (dev) { 3725bc3eb7eSStephen Hemminger const struct net_device_ops *ops = dev->netdev_ops; 3731da177e4SLinus Torvalds int err; 3741da177e4SLinus Torvalds struct ifreq ifr; 3751da177e4SLinus Torvalds struct ip_tunnel_parm p; 3761da177e4SLinus Torvalds struct in_device *in_dev; 3771da177e4SLinus Torvalds 3781da177e4SLinus Torvalds memset(&p, 0, sizeof(p)); 3791da177e4SLinus Torvalds p.iph.daddr = v->vifc_rmt_addr.s_addr; 3801da177e4SLinus Torvalds p.iph.saddr = v->vifc_lcl_addr.s_addr; 3811da177e4SLinus Torvalds p.iph.version = 4; 3821da177e4SLinus Torvalds p.iph.ihl = 5; 3831da177e4SLinus Torvalds p.iph.protocol = IPPROTO_IPIP; 3841da177e4SLinus Torvalds sprintf(p.name, "dvmrp%d", v->vifc_vifi); 385ba93ef74SStephen Hemminger ifr.ifr_ifru.ifru_data = (__force void __user *)&p; 3861da177e4SLinus Torvalds 3875bc3eb7eSStephen Hemminger if (ops->ndo_do_ioctl) { 3885bc3eb7eSStephen Hemminger mm_segment_t oldfs = get_fs(); 3895bc3eb7eSStephen Hemminger 3905bc3eb7eSStephen Hemminger set_fs(KERNEL_DS); 3915bc3eb7eSStephen Hemminger err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL); 3921da177e4SLinus Torvalds set_fs(oldfs); 3935bc3eb7eSStephen Hemminger } else 3945bc3eb7eSStephen Hemminger err = -EOPNOTSUPP; 3951da177e4SLinus Torvalds 3961da177e4SLinus Torvalds dev = NULL; 3971da177e4SLinus Torvalds 3984feb88e5SBenjamin Thery if (err == 0 && 3994feb88e5SBenjamin Thery (dev = __dev_get_by_name(net, p.name)) != NULL) { 4001da177e4SLinus Torvalds dev->flags |= IFF_MULTICAST; 4011da177e4SLinus Torvalds 402e5ed6399SHerbert Xu in_dev = __in_dev_get_rtnl(dev); 40371e27da9SHerbert Xu if (in_dev == NULL) 4041da177e4SLinus Torvalds goto failure; 40571e27da9SHerbert Xu 40671e27da9SHerbert Xu ipv4_devconf_setall(in_dev); 40771e27da9SHerbert Xu IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0; 4081da177e4SLinus Torvalds 4091da177e4SLinus Torvalds if (dev_open(dev)) 4101da177e4SLinus Torvalds goto failure; 4117dc00c82SWang Chen dev_hold(dev); 4121da177e4SLinus Torvalds } 4131da177e4SLinus Torvalds } 4141da177e4SLinus Torvalds return dev; 4151da177e4SLinus Torvalds 4161da177e4SLinus Torvalds failure: 4171da177e4SLinus Torvalds /* allow the register to be completed before unregistering. */ 4181da177e4SLinus Torvalds rtnl_unlock(); 4191da177e4SLinus Torvalds rtnl_lock(); 4201da177e4SLinus Torvalds 4211da177e4SLinus Torvalds unregister_netdevice(dev); 4221da177e4SLinus Torvalds return NULL; 4231da177e4SLinus Torvalds } 4241da177e4SLinus Torvalds 4251da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM 4261da177e4SLinus Torvalds 4276fef4c0cSStephen Hemminger static netdev_tx_t reg_vif_xmit(struct sk_buff *skb, struct net_device *dev) 4281da177e4SLinus Torvalds { 4294feb88e5SBenjamin Thery struct net *net = dev_net(dev); 430f0ad0860SPatrick McHardy struct mr_table *mrt; 431f0ad0860SPatrick McHardy struct flowi fl = { 432f0ad0860SPatrick McHardy .oif = dev->ifindex, 433f0ad0860SPatrick McHardy .iif = skb->skb_iif, 434f0ad0860SPatrick McHardy .mark = skb->mark, 435f0ad0860SPatrick McHardy }; 436f0ad0860SPatrick McHardy int err; 437f0ad0860SPatrick McHardy 438f0ad0860SPatrick McHardy err = ipmr_fib_lookup(net, &fl, &mrt); 439f0ad0860SPatrick McHardy if (err < 0) 440f0ad0860SPatrick McHardy return err; 4414feb88e5SBenjamin Thery 4421da177e4SLinus Torvalds read_lock(&mrt_lock); 443cf3677aeSPavel Emelyanov dev->stats.tx_bytes += skb->len; 444cf3677aeSPavel Emelyanov dev->stats.tx_packets++; 4450c12295aSPatrick McHardy ipmr_cache_report(mrt, skb, mrt->mroute_reg_vif_num, IGMPMSG_WHOLEPKT); 4461da177e4SLinus Torvalds read_unlock(&mrt_lock); 4471da177e4SLinus Torvalds kfree_skb(skb); 4486ed10654SPatrick McHardy return NETDEV_TX_OK; 4491da177e4SLinus Torvalds } 4501da177e4SLinus Torvalds 451007c3838SStephen Hemminger static const struct net_device_ops reg_vif_netdev_ops = { 452007c3838SStephen Hemminger .ndo_start_xmit = reg_vif_xmit, 453007c3838SStephen Hemminger }; 454007c3838SStephen Hemminger 4551da177e4SLinus Torvalds static void reg_vif_setup(struct net_device *dev) 4561da177e4SLinus Torvalds { 4571da177e4SLinus Torvalds dev->type = ARPHRD_PIMREG; 45846f25dffSKris Katterjohn dev->mtu = ETH_DATA_LEN - sizeof(struct iphdr) - 8; 4591da177e4SLinus Torvalds dev->flags = IFF_NOARP; 460007c3838SStephen Hemminger dev->netdev_ops = ®_vif_netdev_ops, 4611da177e4SLinus Torvalds dev->destructor = free_netdev; 462403dbb97STom Goff dev->features |= NETIF_F_NETNS_LOCAL; 4631da177e4SLinus Torvalds } 4641da177e4SLinus Torvalds 465f0ad0860SPatrick McHardy static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt) 4661da177e4SLinus Torvalds { 4671da177e4SLinus Torvalds struct net_device *dev; 4681da177e4SLinus Torvalds struct in_device *in_dev; 469f0ad0860SPatrick McHardy char name[IFNAMSIZ]; 4701da177e4SLinus Torvalds 471f0ad0860SPatrick McHardy if (mrt->id == RT_TABLE_DEFAULT) 472f0ad0860SPatrick McHardy sprintf(name, "pimreg"); 473f0ad0860SPatrick McHardy else 474f0ad0860SPatrick McHardy sprintf(name, "pimreg%u", mrt->id); 475f0ad0860SPatrick McHardy 476f0ad0860SPatrick McHardy dev = alloc_netdev(0, name, reg_vif_setup); 4771da177e4SLinus Torvalds 4781da177e4SLinus Torvalds if (dev == NULL) 4791da177e4SLinus Torvalds return NULL; 4801da177e4SLinus Torvalds 481403dbb97STom Goff dev_net_set(dev, net); 482403dbb97STom Goff 4831da177e4SLinus Torvalds if (register_netdevice(dev)) { 4841da177e4SLinus Torvalds free_netdev(dev); 4851da177e4SLinus Torvalds return NULL; 4861da177e4SLinus Torvalds } 4871da177e4SLinus Torvalds dev->iflink = 0; 4881da177e4SLinus Torvalds 48971e27da9SHerbert Xu rcu_read_lock(); 49071e27da9SHerbert Xu if ((in_dev = __in_dev_get_rcu(dev)) == NULL) { 49171e27da9SHerbert Xu rcu_read_unlock(); 4921da177e4SLinus Torvalds goto failure; 49371e27da9SHerbert Xu } 4941da177e4SLinus Torvalds 49571e27da9SHerbert Xu ipv4_devconf_setall(in_dev); 49671e27da9SHerbert Xu IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0; 49771e27da9SHerbert Xu rcu_read_unlock(); 4981da177e4SLinus Torvalds 4991da177e4SLinus Torvalds if (dev_open(dev)) 5001da177e4SLinus Torvalds goto failure; 5011da177e4SLinus Torvalds 5027dc00c82SWang Chen dev_hold(dev); 5037dc00c82SWang Chen 5041da177e4SLinus Torvalds return dev; 5051da177e4SLinus Torvalds 5061da177e4SLinus Torvalds failure: 5071da177e4SLinus Torvalds /* allow the register to be completed before unregistering. */ 5081da177e4SLinus Torvalds rtnl_unlock(); 5091da177e4SLinus Torvalds rtnl_lock(); 5101da177e4SLinus Torvalds 5111da177e4SLinus Torvalds unregister_netdevice(dev); 5121da177e4SLinus Torvalds return NULL; 5131da177e4SLinus Torvalds } 5141da177e4SLinus Torvalds #endif 5151da177e4SLinus Torvalds 5161da177e4SLinus Torvalds /* 5171da177e4SLinus Torvalds * Delete a VIF entry 5187dc00c82SWang Chen * @notify: Set to 1, if the caller is a notifier_call 5191da177e4SLinus Torvalds */ 5201da177e4SLinus Torvalds 5210c12295aSPatrick McHardy static int vif_delete(struct mr_table *mrt, int vifi, int notify, 522d17fa6faSEric Dumazet struct list_head *head) 5231da177e4SLinus Torvalds { 5241da177e4SLinus Torvalds struct vif_device *v; 5251da177e4SLinus Torvalds struct net_device *dev; 5261da177e4SLinus Torvalds struct in_device *in_dev; 5271da177e4SLinus Torvalds 5280c12295aSPatrick McHardy if (vifi < 0 || vifi >= mrt->maxvif) 5291da177e4SLinus Torvalds return -EADDRNOTAVAIL; 5301da177e4SLinus Torvalds 5310c12295aSPatrick McHardy v = &mrt->vif_table[vifi]; 5321da177e4SLinus Torvalds 5331da177e4SLinus Torvalds write_lock_bh(&mrt_lock); 5341da177e4SLinus Torvalds dev = v->dev; 5351da177e4SLinus Torvalds v->dev = NULL; 5361da177e4SLinus Torvalds 5371da177e4SLinus Torvalds if (!dev) { 5381da177e4SLinus Torvalds write_unlock_bh(&mrt_lock); 5391da177e4SLinus Torvalds return -EADDRNOTAVAIL; 5401da177e4SLinus Torvalds } 5411da177e4SLinus Torvalds 5421da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM 5430c12295aSPatrick McHardy if (vifi == mrt->mroute_reg_vif_num) 5440c12295aSPatrick McHardy mrt->mroute_reg_vif_num = -1; 5451da177e4SLinus Torvalds #endif 5461da177e4SLinus Torvalds 5470c12295aSPatrick McHardy if (vifi+1 == mrt->maxvif) { 5481da177e4SLinus Torvalds int tmp; 5491da177e4SLinus Torvalds for (tmp=vifi-1; tmp>=0; tmp--) { 5500c12295aSPatrick McHardy if (VIF_EXISTS(mrt, tmp)) 5511da177e4SLinus Torvalds break; 5521da177e4SLinus Torvalds } 5530c12295aSPatrick McHardy mrt->maxvif = tmp+1; 5541da177e4SLinus Torvalds } 5551da177e4SLinus Torvalds 5561da177e4SLinus Torvalds write_unlock_bh(&mrt_lock); 5571da177e4SLinus Torvalds 5581da177e4SLinus Torvalds dev_set_allmulti(dev, -1); 5591da177e4SLinus Torvalds 560e5ed6399SHerbert Xu if ((in_dev = __in_dev_get_rtnl(dev)) != NULL) { 56142f811b8SHerbert Xu IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)--; 5621da177e4SLinus Torvalds ip_rt_multicast_event(in_dev); 5631da177e4SLinus Torvalds } 5641da177e4SLinus Torvalds 5657dc00c82SWang Chen if (v->flags&(VIFF_TUNNEL|VIFF_REGISTER) && !notify) 566d17fa6faSEric Dumazet unregister_netdevice_queue(dev, head); 5671da177e4SLinus Torvalds 5681da177e4SLinus Torvalds dev_put(dev); 5691da177e4SLinus Torvalds return 0; 5701da177e4SLinus Torvalds } 5711da177e4SLinus Torvalds 5725c0a66f5SBenjamin Thery static inline void ipmr_cache_free(struct mfc_cache *c) 5735c0a66f5SBenjamin Thery { 5745c0a66f5SBenjamin Thery kmem_cache_free(mrt_cachep, c); 5755c0a66f5SBenjamin Thery } 5765c0a66f5SBenjamin Thery 5771da177e4SLinus Torvalds /* Destroy an unresolved cache entry, killing queued skbs 5781da177e4SLinus Torvalds and reporting error to netlink readers. 5791da177e4SLinus Torvalds */ 5801da177e4SLinus Torvalds 5810c12295aSPatrick McHardy static void ipmr_destroy_unres(struct mr_table *mrt, struct mfc_cache *c) 5821da177e4SLinus Torvalds { 5830c12295aSPatrick McHardy struct net *net = NULL; //mrt->net; 5841da177e4SLinus Torvalds struct sk_buff *skb; 5859ef1d4c7SPatrick McHardy struct nlmsgerr *e; 5861da177e4SLinus Torvalds 5870c12295aSPatrick McHardy atomic_dec(&mrt->cache_resolve_queue_len); 5881da177e4SLinus Torvalds 5891da177e4SLinus Torvalds while ((skb = skb_dequeue(&c->mfc_un.unres.unresolved))) { 590eddc9ec5SArnaldo Carvalho de Melo if (ip_hdr(skb)->version == 0) { 5911da177e4SLinus Torvalds struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr)); 5921da177e4SLinus Torvalds nlh->nlmsg_type = NLMSG_ERROR; 5931da177e4SLinus Torvalds nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct nlmsgerr)); 5941da177e4SLinus Torvalds skb_trim(skb, nlh->nlmsg_len); 5959ef1d4c7SPatrick McHardy e = NLMSG_DATA(nlh); 5969ef1d4c7SPatrick McHardy e->error = -ETIMEDOUT; 5979ef1d4c7SPatrick McHardy memset(&e->msg, 0, sizeof(e->msg)); 5982942e900SThomas Graf 5994feb88e5SBenjamin Thery rtnl_unicast(skb, net, NETLINK_CB(skb).pid); 6001da177e4SLinus Torvalds } else 6011da177e4SLinus Torvalds kfree_skb(skb); 6021da177e4SLinus Torvalds } 6031da177e4SLinus Torvalds 6045c0a66f5SBenjamin Thery ipmr_cache_free(c); 6051da177e4SLinus Torvalds } 6061da177e4SLinus Torvalds 6071da177e4SLinus Torvalds 608e258beb2SPatrick McHardy /* Timer process for the unresolved queue. */ 6091da177e4SLinus Torvalds 610e258beb2SPatrick McHardy static void ipmr_expire_process(unsigned long arg) 6111da177e4SLinus Torvalds { 6120c12295aSPatrick McHardy struct mr_table *mrt = (struct mr_table *)arg; 6131da177e4SLinus Torvalds unsigned long now; 6141da177e4SLinus Torvalds unsigned long expires; 615862465f2SPatrick McHardy struct mfc_cache *c, *next; 6161da177e4SLinus Torvalds 6171da177e4SLinus Torvalds if (!spin_trylock(&mfc_unres_lock)) { 6180c12295aSPatrick McHardy mod_timer(&mrt->ipmr_expire_timer, jiffies+HZ/10); 6191da177e4SLinus Torvalds return; 6201da177e4SLinus Torvalds } 6211da177e4SLinus Torvalds 6220c12295aSPatrick McHardy if (list_empty(&mrt->mfc_unres_queue)) 6231da177e4SLinus Torvalds goto out; 6241da177e4SLinus Torvalds 6251da177e4SLinus Torvalds now = jiffies; 6261da177e4SLinus Torvalds expires = 10*HZ; 6271da177e4SLinus Torvalds 6280c12295aSPatrick McHardy list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) { 6291da177e4SLinus Torvalds if (time_after(c->mfc_un.unres.expires, now)) { 6301da177e4SLinus Torvalds unsigned long interval = c->mfc_un.unres.expires - now; 6311da177e4SLinus Torvalds if (interval < expires) 6321da177e4SLinus Torvalds expires = interval; 6331da177e4SLinus Torvalds continue; 6341da177e4SLinus Torvalds } 6351da177e4SLinus Torvalds 636862465f2SPatrick McHardy list_del(&c->list); 6370c12295aSPatrick McHardy ipmr_destroy_unres(mrt, c); 6381da177e4SLinus Torvalds } 6391da177e4SLinus Torvalds 6400c12295aSPatrick McHardy if (!list_empty(&mrt->mfc_unres_queue)) 6410c12295aSPatrick McHardy mod_timer(&mrt->ipmr_expire_timer, jiffies + expires); 6421da177e4SLinus Torvalds 6431da177e4SLinus Torvalds out: 6441da177e4SLinus Torvalds spin_unlock(&mfc_unres_lock); 6451da177e4SLinus Torvalds } 6461da177e4SLinus Torvalds 6471da177e4SLinus Torvalds /* Fill oifs list. It is called under write locked mrt_lock. */ 6481da177e4SLinus Torvalds 6490c12295aSPatrick McHardy static void ipmr_update_thresholds(struct mr_table *mrt, struct mfc_cache *cache, 650d658f8a0SPatrick McHardy unsigned char *ttls) 6511da177e4SLinus Torvalds { 6521da177e4SLinus Torvalds int vifi; 6531da177e4SLinus Torvalds 6541da177e4SLinus Torvalds cache->mfc_un.res.minvif = MAXVIFS; 6551da177e4SLinus Torvalds cache->mfc_un.res.maxvif = 0; 6561da177e4SLinus Torvalds memset(cache->mfc_un.res.ttls, 255, MAXVIFS); 6571da177e4SLinus Torvalds 6580c12295aSPatrick McHardy for (vifi = 0; vifi < mrt->maxvif; vifi++) { 6590c12295aSPatrick McHardy if (VIF_EXISTS(mrt, vifi) && 660cf958ae3SBenjamin Thery ttls[vifi] && ttls[vifi] < 255) { 6611da177e4SLinus Torvalds cache->mfc_un.res.ttls[vifi] = ttls[vifi]; 6621da177e4SLinus Torvalds if (cache->mfc_un.res.minvif > vifi) 6631da177e4SLinus Torvalds cache->mfc_un.res.minvif = vifi; 6641da177e4SLinus Torvalds if (cache->mfc_un.res.maxvif <= vifi) 6651da177e4SLinus Torvalds cache->mfc_un.res.maxvif = vifi + 1; 6661da177e4SLinus Torvalds } 6671da177e4SLinus Torvalds } 6681da177e4SLinus Torvalds } 6691da177e4SLinus Torvalds 6700c12295aSPatrick McHardy static int vif_add(struct net *net, struct mr_table *mrt, 6710c12295aSPatrick McHardy struct vifctl *vifc, int mrtsock) 6721da177e4SLinus Torvalds { 6731da177e4SLinus Torvalds int vifi = vifc->vifc_vifi; 6740c12295aSPatrick McHardy struct vif_device *v = &mrt->vif_table[vifi]; 6751da177e4SLinus Torvalds struct net_device *dev; 6761da177e4SLinus Torvalds struct in_device *in_dev; 677d607032dSWang Chen int err; 6781da177e4SLinus Torvalds 6791da177e4SLinus Torvalds /* Is vif busy ? */ 6800c12295aSPatrick McHardy if (VIF_EXISTS(mrt, vifi)) 6811da177e4SLinus Torvalds return -EADDRINUSE; 6821da177e4SLinus Torvalds 6831da177e4SLinus Torvalds switch (vifc->vifc_flags) { 6841da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM 6851da177e4SLinus Torvalds case VIFF_REGISTER: 6861da177e4SLinus Torvalds /* 6871da177e4SLinus Torvalds * Special Purpose VIF in PIM 6881da177e4SLinus Torvalds * All the packets will be sent to the daemon 6891da177e4SLinus Torvalds */ 6900c12295aSPatrick McHardy if (mrt->mroute_reg_vif_num >= 0) 6911da177e4SLinus Torvalds return -EADDRINUSE; 692f0ad0860SPatrick McHardy dev = ipmr_reg_vif(net, mrt); 6931da177e4SLinus Torvalds if (!dev) 6941da177e4SLinus Torvalds return -ENOBUFS; 695d607032dSWang Chen err = dev_set_allmulti(dev, 1); 696d607032dSWang Chen if (err) { 697d607032dSWang Chen unregister_netdevice(dev); 6987dc00c82SWang Chen dev_put(dev); 699d607032dSWang Chen return err; 700d607032dSWang Chen } 7011da177e4SLinus Torvalds break; 7021da177e4SLinus Torvalds #endif 7031da177e4SLinus Torvalds case VIFF_TUNNEL: 7044feb88e5SBenjamin Thery dev = ipmr_new_tunnel(net, vifc); 7051da177e4SLinus Torvalds if (!dev) 7061da177e4SLinus Torvalds return -ENOBUFS; 707d607032dSWang Chen err = dev_set_allmulti(dev, 1); 708d607032dSWang Chen if (err) { 709d607032dSWang Chen ipmr_del_tunnel(dev, vifc); 7107dc00c82SWang Chen dev_put(dev); 711d607032dSWang Chen return err; 712d607032dSWang Chen } 7131da177e4SLinus Torvalds break; 714ee5e81f0SIlia K 715ee5e81f0SIlia K case VIFF_USE_IFINDEX: 7161da177e4SLinus Torvalds case 0: 717ee5e81f0SIlia K if (vifc->vifc_flags == VIFF_USE_IFINDEX) { 718ee5e81f0SIlia K dev = dev_get_by_index(net, vifc->vifc_lcl_ifindex); 719ee5e81f0SIlia K if (dev && dev->ip_ptr == NULL) { 720ee5e81f0SIlia K dev_put(dev); 721ee5e81f0SIlia K return -EADDRNOTAVAIL; 722ee5e81f0SIlia K } 723ee5e81f0SIlia K } else 7244feb88e5SBenjamin Thery dev = ip_dev_find(net, vifc->vifc_lcl_addr.s_addr); 725ee5e81f0SIlia K 7261da177e4SLinus Torvalds if (!dev) 7271da177e4SLinus Torvalds return -EADDRNOTAVAIL; 728d607032dSWang Chen err = dev_set_allmulti(dev, 1); 7297dc00c82SWang Chen if (err) { 7307dc00c82SWang Chen dev_put(dev); 731d607032dSWang Chen return err; 7327dc00c82SWang Chen } 7331da177e4SLinus Torvalds break; 7341da177e4SLinus Torvalds default: 7351da177e4SLinus Torvalds return -EINVAL; 7361da177e4SLinus Torvalds } 7371da177e4SLinus Torvalds 738d0490cfdSDan Carpenter if ((in_dev = __in_dev_get_rtnl(dev)) == NULL) { 739d0490cfdSDan Carpenter dev_put(dev); 7401da177e4SLinus Torvalds return -EADDRNOTAVAIL; 741d0490cfdSDan Carpenter } 74242f811b8SHerbert Xu IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)++; 7431da177e4SLinus Torvalds ip_rt_multicast_event(in_dev); 7441da177e4SLinus Torvalds 7451da177e4SLinus Torvalds /* 7461da177e4SLinus Torvalds * Fill in the VIF structures 7471da177e4SLinus Torvalds */ 7481da177e4SLinus Torvalds v->rate_limit = vifc->vifc_rate_limit; 7491da177e4SLinus Torvalds v->local = vifc->vifc_lcl_addr.s_addr; 7501da177e4SLinus Torvalds v->remote = vifc->vifc_rmt_addr.s_addr; 7511da177e4SLinus Torvalds v->flags = vifc->vifc_flags; 7521da177e4SLinus Torvalds if (!mrtsock) 7531da177e4SLinus Torvalds v->flags |= VIFF_STATIC; 7541da177e4SLinus Torvalds v->threshold = vifc->vifc_threshold; 7551da177e4SLinus Torvalds v->bytes_in = 0; 7561da177e4SLinus Torvalds v->bytes_out = 0; 7571da177e4SLinus Torvalds v->pkt_in = 0; 7581da177e4SLinus Torvalds v->pkt_out = 0; 7591da177e4SLinus Torvalds v->link = dev->ifindex; 7601da177e4SLinus Torvalds if (v->flags&(VIFF_TUNNEL|VIFF_REGISTER)) 7611da177e4SLinus Torvalds v->link = dev->iflink; 7621da177e4SLinus Torvalds 7631da177e4SLinus Torvalds /* And finish update writing critical data */ 7641da177e4SLinus Torvalds write_lock_bh(&mrt_lock); 7651da177e4SLinus Torvalds v->dev = dev; 7661da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM 7671da177e4SLinus Torvalds if (v->flags&VIFF_REGISTER) 7680c12295aSPatrick McHardy mrt->mroute_reg_vif_num = vifi; 7691da177e4SLinus Torvalds #endif 7700c12295aSPatrick McHardy if (vifi+1 > mrt->maxvif) 7710c12295aSPatrick McHardy mrt->maxvif = vifi+1; 7721da177e4SLinus Torvalds write_unlock_bh(&mrt_lock); 7731da177e4SLinus Torvalds return 0; 7741da177e4SLinus Torvalds } 7751da177e4SLinus Torvalds 7760c12295aSPatrick McHardy static struct mfc_cache *ipmr_cache_find(struct mr_table *mrt, 7774feb88e5SBenjamin Thery __be32 origin, 7784feb88e5SBenjamin Thery __be32 mcastgrp) 7791da177e4SLinus Torvalds { 7801da177e4SLinus Torvalds int line = MFC_HASH(mcastgrp, origin); 7811da177e4SLinus Torvalds struct mfc_cache *c; 7821da177e4SLinus Torvalds 7830c12295aSPatrick McHardy list_for_each_entry(c, &mrt->mfc_cache_array[line], list) { 7841da177e4SLinus Torvalds if (c->mfc_origin == origin && c->mfc_mcastgrp == mcastgrp) 7851da177e4SLinus Torvalds return c; 7861da177e4SLinus Torvalds } 787862465f2SPatrick McHardy return NULL; 788862465f2SPatrick McHardy } 7891da177e4SLinus Torvalds 7901da177e4SLinus Torvalds /* 7911da177e4SLinus Torvalds * Allocate a multicast cache entry 7921da177e4SLinus Torvalds */ 793d658f8a0SPatrick McHardy static struct mfc_cache *ipmr_cache_alloc(void) 7941da177e4SLinus Torvalds { 795c3762229SRobert P. J. Day struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_KERNEL); 7961da177e4SLinus Torvalds if (c == NULL) 7971da177e4SLinus Torvalds return NULL; 7981da177e4SLinus Torvalds c->mfc_un.res.minvif = MAXVIFS; 7991da177e4SLinus Torvalds return c; 8001da177e4SLinus Torvalds } 8011da177e4SLinus Torvalds 802d658f8a0SPatrick McHardy static struct mfc_cache *ipmr_cache_alloc_unres(void) 8031da177e4SLinus Torvalds { 804c3762229SRobert P. J. Day struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_ATOMIC); 8051da177e4SLinus Torvalds if (c == NULL) 8061da177e4SLinus Torvalds return NULL; 8071da177e4SLinus Torvalds skb_queue_head_init(&c->mfc_un.unres.unresolved); 8081da177e4SLinus Torvalds c->mfc_un.unres.expires = jiffies + 10*HZ; 8091da177e4SLinus Torvalds return c; 8101da177e4SLinus Torvalds } 8111da177e4SLinus Torvalds 8121da177e4SLinus Torvalds /* 8131da177e4SLinus Torvalds * A cache entry has gone into a resolved state from queued 8141da177e4SLinus Torvalds */ 8151da177e4SLinus Torvalds 8160c12295aSPatrick McHardy static void ipmr_cache_resolve(struct net *net, struct mr_table *mrt, 8170c12295aSPatrick McHardy struct mfc_cache *uc, struct mfc_cache *c) 8181da177e4SLinus Torvalds { 8191da177e4SLinus Torvalds struct sk_buff *skb; 8209ef1d4c7SPatrick McHardy struct nlmsgerr *e; 8211da177e4SLinus Torvalds 8221da177e4SLinus Torvalds /* 8231da177e4SLinus Torvalds * Play the pending entries through our router 8241da177e4SLinus Torvalds */ 8251da177e4SLinus Torvalds 8261da177e4SLinus Torvalds while ((skb = __skb_dequeue(&uc->mfc_un.unres.unresolved))) { 827eddc9ec5SArnaldo Carvalho de Melo if (ip_hdr(skb)->version == 0) { 8281da177e4SLinus Torvalds struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr)); 8291da177e4SLinus Torvalds 8300c12295aSPatrick McHardy if (ipmr_fill_mroute(mrt, skb, c, NLMSG_DATA(nlh)) > 0) { 83127a884dcSArnaldo Carvalho de Melo nlh->nlmsg_len = (skb_tail_pointer(skb) - 83227a884dcSArnaldo Carvalho de Melo (u8 *)nlh); 8331da177e4SLinus Torvalds } else { 8341da177e4SLinus Torvalds nlh->nlmsg_type = NLMSG_ERROR; 8351da177e4SLinus Torvalds nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct nlmsgerr)); 8361da177e4SLinus Torvalds skb_trim(skb, nlh->nlmsg_len); 8379ef1d4c7SPatrick McHardy e = NLMSG_DATA(nlh); 8389ef1d4c7SPatrick McHardy e->error = -EMSGSIZE; 8399ef1d4c7SPatrick McHardy memset(&e->msg, 0, sizeof(e->msg)); 8401da177e4SLinus Torvalds } 8412942e900SThomas Graf 842d658f8a0SPatrick McHardy rtnl_unicast(skb, net, NETLINK_CB(skb).pid); 8431da177e4SLinus Torvalds } else 8440c12295aSPatrick McHardy ip_mr_forward(net, mrt, skb, c, 0); 8451da177e4SLinus Torvalds } 8461da177e4SLinus Torvalds } 8471da177e4SLinus Torvalds 8481da177e4SLinus Torvalds /* 8491da177e4SLinus Torvalds * Bounce a cache query up to mrouted. We could use netlink for this but mrouted 8501da177e4SLinus Torvalds * expects the following bizarre scheme. 8511da177e4SLinus Torvalds * 8521da177e4SLinus Torvalds * Called under mrt_lock. 8531da177e4SLinus Torvalds */ 8541da177e4SLinus Torvalds 8550c12295aSPatrick McHardy static int ipmr_cache_report(struct mr_table *mrt, 8564feb88e5SBenjamin Thery struct sk_buff *pkt, vifi_t vifi, int assert) 8571da177e4SLinus Torvalds { 8581da177e4SLinus Torvalds struct sk_buff *skb; 859c9bdd4b5SArnaldo Carvalho de Melo const int ihl = ip_hdrlen(pkt); 8601da177e4SLinus Torvalds struct igmphdr *igmp; 8611da177e4SLinus Torvalds struct igmpmsg *msg; 8621da177e4SLinus Torvalds int ret; 8631da177e4SLinus Torvalds 8641da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM 8651da177e4SLinus Torvalds if (assert == IGMPMSG_WHOLEPKT) 8661da177e4SLinus Torvalds skb = skb_realloc_headroom(pkt, sizeof(struct iphdr)); 8671da177e4SLinus Torvalds else 8681da177e4SLinus Torvalds #endif 8691da177e4SLinus Torvalds skb = alloc_skb(128, GFP_ATOMIC); 8701da177e4SLinus Torvalds 8711da177e4SLinus Torvalds if (!skb) 8721da177e4SLinus Torvalds return -ENOBUFS; 8731da177e4SLinus Torvalds 8741da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM 8751da177e4SLinus Torvalds if (assert == IGMPMSG_WHOLEPKT) { 8761da177e4SLinus Torvalds /* Ugly, but we have no choice with this interface. 8771da177e4SLinus Torvalds Duplicate old header, fix ihl, length etc. 8781da177e4SLinus Torvalds And all this only to mangle msg->im_msgtype and 8791da177e4SLinus Torvalds to set msg->im_mbz to "mbz" :-) 8801da177e4SLinus Torvalds */ 881878c8145SArnaldo Carvalho de Melo skb_push(skb, sizeof(struct iphdr)); 882878c8145SArnaldo Carvalho de Melo skb_reset_network_header(skb); 883badff6d0SArnaldo Carvalho de Melo skb_reset_transport_header(skb); 8840272ffc4SArnaldo Carvalho de Melo msg = (struct igmpmsg *)skb_network_header(skb); 885d56f90a7SArnaldo Carvalho de Melo memcpy(msg, skb_network_header(pkt), sizeof(struct iphdr)); 8861da177e4SLinus Torvalds msg->im_msgtype = IGMPMSG_WHOLEPKT; 8871da177e4SLinus Torvalds msg->im_mbz = 0; 8880c12295aSPatrick McHardy msg->im_vif = mrt->mroute_reg_vif_num; 889eddc9ec5SArnaldo Carvalho de Melo ip_hdr(skb)->ihl = sizeof(struct iphdr) >> 2; 890eddc9ec5SArnaldo Carvalho de Melo ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(pkt)->tot_len) + 891eddc9ec5SArnaldo Carvalho de Melo sizeof(struct iphdr)); 8921da177e4SLinus Torvalds } else 8931da177e4SLinus Torvalds #endif 8941da177e4SLinus Torvalds { 8951da177e4SLinus Torvalds 8961da177e4SLinus Torvalds /* 8971da177e4SLinus Torvalds * Copy the IP header 8981da177e4SLinus Torvalds */ 8991da177e4SLinus Torvalds 90027a884dcSArnaldo Carvalho de Melo skb->network_header = skb->tail; 901ddc7b8e3SArnaldo Carvalho de Melo skb_put(skb, ihl); 90227d7ff46SArnaldo Carvalho de Melo skb_copy_to_linear_data(skb, pkt->data, ihl); 903eddc9ec5SArnaldo Carvalho de Melo ip_hdr(skb)->protocol = 0; /* Flag to the kernel this is a route add */ 904eddc9ec5SArnaldo Carvalho de Melo msg = (struct igmpmsg *)skb_network_header(skb); 9051da177e4SLinus Torvalds msg->im_vif = vifi; 906adf30907SEric Dumazet skb_dst_set(skb, dst_clone(skb_dst(pkt))); 9071da177e4SLinus Torvalds 9081da177e4SLinus Torvalds /* 9091da177e4SLinus Torvalds * Add our header 9101da177e4SLinus Torvalds */ 9111da177e4SLinus Torvalds 9121da177e4SLinus Torvalds igmp=(struct igmphdr *)skb_put(skb, sizeof(struct igmphdr)); 9131da177e4SLinus Torvalds igmp->type = 9141da177e4SLinus Torvalds msg->im_msgtype = assert; 9151da177e4SLinus Torvalds igmp->code = 0; 916eddc9ec5SArnaldo Carvalho de Melo ip_hdr(skb)->tot_len = htons(skb->len); /* Fix the length */ 917b0e380b1SArnaldo Carvalho de Melo skb->transport_header = skb->network_header; 9181da177e4SLinus Torvalds } 9191da177e4SLinus Torvalds 9200c12295aSPatrick McHardy if (mrt->mroute_sk == NULL) { 9211da177e4SLinus Torvalds kfree_skb(skb); 9221da177e4SLinus Torvalds return -EINVAL; 9231da177e4SLinus Torvalds } 9241da177e4SLinus Torvalds 9251da177e4SLinus Torvalds /* 9261da177e4SLinus Torvalds * Deliver to mrouted 9271da177e4SLinus Torvalds */ 9280c12295aSPatrick McHardy ret = sock_queue_rcv_skb(mrt->mroute_sk, skb); 92970a269e6SBenjamin Thery if (ret < 0) { 9301da177e4SLinus Torvalds if (net_ratelimit()) 9311da177e4SLinus Torvalds printk(KERN_WARNING "mroute: pending queue full, dropping entries.\n"); 9321da177e4SLinus Torvalds kfree_skb(skb); 9331da177e4SLinus Torvalds } 9341da177e4SLinus Torvalds 9351da177e4SLinus Torvalds return ret; 9361da177e4SLinus Torvalds } 9371da177e4SLinus Torvalds 9381da177e4SLinus Torvalds /* 9391da177e4SLinus Torvalds * Queue a packet for resolution. It gets locked cache entry! 9401da177e4SLinus Torvalds */ 9411da177e4SLinus Torvalds 9421da177e4SLinus Torvalds static int 9430c12295aSPatrick McHardy ipmr_cache_unresolved(struct mr_table *mrt, vifi_t vifi, struct sk_buff *skb) 9441da177e4SLinus Torvalds { 945862465f2SPatrick McHardy bool found = false; 9461da177e4SLinus Torvalds int err; 9471da177e4SLinus Torvalds struct mfc_cache *c; 948eddc9ec5SArnaldo Carvalho de Melo const struct iphdr *iph = ip_hdr(skb); 9491da177e4SLinus Torvalds 9501da177e4SLinus Torvalds spin_lock_bh(&mfc_unres_lock); 9510c12295aSPatrick McHardy list_for_each_entry(c, &mrt->mfc_unres_queue, list) { 952e258beb2SPatrick McHardy if (c->mfc_mcastgrp == iph->daddr && 953862465f2SPatrick McHardy c->mfc_origin == iph->saddr) { 954862465f2SPatrick McHardy found = true; 9551da177e4SLinus Torvalds break; 9561da177e4SLinus Torvalds } 957862465f2SPatrick McHardy } 9581da177e4SLinus Torvalds 959862465f2SPatrick McHardy if (!found) { 9601da177e4SLinus Torvalds /* 9611da177e4SLinus Torvalds * Create a new entry if allowable 9621da177e4SLinus Torvalds */ 9631da177e4SLinus Torvalds 9640c12295aSPatrick McHardy if (atomic_read(&mrt->cache_resolve_queue_len) >= 10 || 965d658f8a0SPatrick McHardy (c = ipmr_cache_alloc_unres()) == NULL) { 9661da177e4SLinus Torvalds spin_unlock_bh(&mfc_unres_lock); 9671da177e4SLinus Torvalds 9681da177e4SLinus Torvalds kfree_skb(skb); 9691da177e4SLinus Torvalds return -ENOBUFS; 9701da177e4SLinus Torvalds } 9711da177e4SLinus Torvalds 9721da177e4SLinus Torvalds /* 9731da177e4SLinus Torvalds * Fill in the new cache entry 9741da177e4SLinus Torvalds */ 9751da177e4SLinus Torvalds c->mfc_parent = -1; 976eddc9ec5SArnaldo Carvalho de Melo c->mfc_origin = iph->saddr; 977eddc9ec5SArnaldo Carvalho de Melo c->mfc_mcastgrp = iph->daddr; 9781da177e4SLinus Torvalds 9791da177e4SLinus Torvalds /* 9801da177e4SLinus Torvalds * Reflect first query at mrouted. 9811da177e4SLinus Torvalds */ 9820c12295aSPatrick McHardy err = ipmr_cache_report(mrt, skb, vifi, IGMPMSG_NOCACHE); 9834feb88e5SBenjamin Thery if (err < 0) { 9841da177e4SLinus Torvalds /* If the report failed throw the cache entry 9851da177e4SLinus Torvalds out - Brad Parker 9861da177e4SLinus Torvalds */ 9871da177e4SLinus Torvalds spin_unlock_bh(&mfc_unres_lock); 9881da177e4SLinus Torvalds 9895c0a66f5SBenjamin Thery ipmr_cache_free(c); 9901da177e4SLinus Torvalds kfree_skb(skb); 9911da177e4SLinus Torvalds return err; 9921da177e4SLinus Torvalds } 9931da177e4SLinus Torvalds 9940c12295aSPatrick McHardy atomic_inc(&mrt->cache_resolve_queue_len); 9950c12295aSPatrick McHardy list_add(&c->list, &mrt->mfc_unres_queue); 9961da177e4SLinus Torvalds 9970c12295aSPatrick McHardy mod_timer(&mrt->ipmr_expire_timer, c->mfc_un.unres.expires); 9981da177e4SLinus Torvalds } 9991da177e4SLinus Torvalds 10001da177e4SLinus Torvalds /* 10011da177e4SLinus Torvalds * See if we can append the packet 10021da177e4SLinus Torvalds */ 10031da177e4SLinus Torvalds if (c->mfc_un.unres.unresolved.qlen>3) { 10041da177e4SLinus Torvalds kfree_skb(skb); 10051da177e4SLinus Torvalds err = -ENOBUFS; 10061da177e4SLinus Torvalds } else { 10071da177e4SLinus Torvalds skb_queue_tail(&c->mfc_un.unres.unresolved, skb); 10081da177e4SLinus Torvalds err = 0; 10091da177e4SLinus Torvalds } 10101da177e4SLinus Torvalds 10111da177e4SLinus Torvalds spin_unlock_bh(&mfc_unres_lock); 10121da177e4SLinus Torvalds return err; 10131da177e4SLinus Torvalds } 10141da177e4SLinus Torvalds 10151da177e4SLinus Torvalds /* 10161da177e4SLinus Torvalds * MFC cache manipulation by user space mroute daemon 10171da177e4SLinus Torvalds */ 10181da177e4SLinus Torvalds 10190c12295aSPatrick McHardy static int ipmr_mfc_delete(struct mr_table *mrt, struct mfcctl *mfc) 10201da177e4SLinus Torvalds { 10211da177e4SLinus Torvalds int line; 1022862465f2SPatrick McHardy struct mfc_cache *c, *next; 10231da177e4SLinus Torvalds 10241da177e4SLinus Torvalds line = MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr); 10251da177e4SLinus Torvalds 10260c12295aSPatrick McHardy list_for_each_entry_safe(c, next, &mrt->mfc_cache_array[line], list) { 10271da177e4SLinus Torvalds if (c->mfc_origin == mfc->mfcc_origin.s_addr && 10281da177e4SLinus Torvalds c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr) { 10291da177e4SLinus Torvalds write_lock_bh(&mrt_lock); 1030862465f2SPatrick McHardy list_del(&c->list); 10311da177e4SLinus Torvalds write_unlock_bh(&mrt_lock); 10321da177e4SLinus Torvalds 10335c0a66f5SBenjamin Thery ipmr_cache_free(c); 10341da177e4SLinus Torvalds return 0; 10351da177e4SLinus Torvalds } 10361da177e4SLinus Torvalds } 10371da177e4SLinus Torvalds return -ENOENT; 10381da177e4SLinus Torvalds } 10391da177e4SLinus Torvalds 10400c12295aSPatrick McHardy static int ipmr_mfc_add(struct net *net, struct mr_table *mrt, 10410c12295aSPatrick McHardy struct mfcctl *mfc, int mrtsock) 10421da177e4SLinus Torvalds { 1043862465f2SPatrick McHardy bool found = false; 10441da177e4SLinus Torvalds int line; 1045862465f2SPatrick McHardy struct mfc_cache *uc, *c; 10461da177e4SLinus Torvalds 1047a50436f2SPatrick McHardy if (mfc->mfcc_parent >= MAXVIFS) 1048a50436f2SPatrick McHardy return -ENFILE; 1049a50436f2SPatrick McHardy 10501da177e4SLinus Torvalds line = MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr); 10511da177e4SLinus Torvalds 10520c12295aSPatrick McHardy list_for_each_entry(c, &mrt->mfc_cache_array[line], list) { 10531da177e4SLinus Torvalds if (c->mfc_origin == mfc->mfcc_origin.s_addr && 1054862465f2SPatrick McHardy c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr) { 1055862465f2SPatrick McHardy found = true; 10561da177e4SLinus Torvalds break; 10571da177e4SLinus Torvalds } 1058862465f2SPatrick McHardy } 10591da177e4SLinus Torvalds 1060862465f2SPatrick McHardy if (found) { 10611da177e4SLinus Torvalds write_lock_bh(&mrt_lock); 10621da177e4SLinus Torvalds c->mfc_parent = mfc->mfcc_parent; 10630c12295aSPatrick McHardy ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls); 10641da177e4SLinus Torvalds if (!mrtsock) 10651da177e4SLinus Torvalds c->mfc_flags |= MFC_STATIC; 10661da177e4SLinus Torvalds write_unlock_bh(&mrt_lock); 10671da177e4SLinus Torvalds return 0; 10681da177e4SLinus Torvalds } 10691da177e4SLinus Torvalds 1070f97c1e0cSJoe Perches if (!ipv4_is_multicast(mfc->mfcc_mcastgrp.s_addr)) 10711da177e4SLinus Torvalds return -EINVAL; 10721da177e4SLinus Torvalds 1073d658f8a0SPatrick McHardy c = ipmr_cache_alloc(); 10741da177e4SLinus Torvalds if (c == NULL) 10751da177e4SLinus Torvalds return -ENOMEM; 10761da177e4SLinus Torvalds 10771da177e4SLinus Torvalds c->mfc_origin = mfc->mfcc_origin.s_addr; 10781da177e4SLinus Torvalds c->mfc_mcastgrp = mfc->mfcc_mcastgrp.s_addr; 10791da177e4SLinus Torvalds c->mfc_parent = mfc->mfcc_parent; 10800c12295aSPatrick McHardy ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls); 10811da177e4SLinus Torvalds if (!mrtsock) 10821da177e4SLinus Torvalds c->mfc_flags |= MFC_STATIC; 10831da177e4SLinus Torvalds 10841da177e4SLinus Torvalds write_lock_bh(&mrt_lock); 10850c12295aSPatrick McHardy list_add(&c->list, &mrt->mfc_cache_array[line]); 10861da177e4SLinus Torvalds write_unlock_bh(&mrt_lock); 10871da177e4SLinus Torvalds 10881da177e4SLinus Torvalds /* 10891da177e4SLinus Torvalds * Check to see if we resolved a queued list. If so we 10901da177e4SLinus Torvalds * need to send on the frames and tidy up. 10911da177e4SLinus Torvalds */ 1092*b0ebb739SPatrick McHardy found = false; 10931da177e4SLinus Torvalds spin_lock_bh(&mfc_unres_lock); 10940c12295aSPatrick McHardy list_for_each_entry(uc, &mrt->mfc_unres_queue, list) { 1095e258beb2SPatrick McHardy if (uc->mfc_origin == c->mfc_origin && 10961da177e4SLinus Torvalds uc->mfc_mcastgrp == c->mfc_mcastgrp) { 1097862465f2SPatrick McHardy list_del(&uc->list); 10980c12295aSPatrick McHardy atomic_dec(&mrt->cache_resolve_queue_len); 1099*b0ebb739SPatrick McHardy found = true; 11001da177e4SLinus Torvalds break; 11011da177e4SLinus Torvalds } 11021da177e4SLinus Torvalds } 11030c12295aSPatrick McHardy if (list_empty(&mrt->mfc_unres_queue)) 11040c12295aSPatrick McHardy del_timer(&mrt->ipmr_expire_timer); 11051da177e4SLinus Torvalds spin_unlock_bh(&mfc_unres_lock); 11061da177e4SLinus Torvalds 1107*b0ebb739SPatrick McHardy if (found) { 11080c12295aSPatrick McHardy ipmr_cache_resolve(net, mrt, uc, c); 11095c0a66f5SBenjamin Thery ipmr_cache_free(uc); 11101da177e4SLinus Torvalds } 11111da177e4SLinus Torvalds return 0; 11121da177e4SLinus Torvalds } 11131da177e4SLinus Torvalds 11141da177e4SLinus Torvalds /* 11151da177e4SLinus Torvalds * Close the multicast socket, and clear the vif tables etc 11161da177e4SLinus Torvalds */ 11171da177e4SLinus Torvalds 11180c12295aSPatrick McHardy static void mroute_clean_tables(struct mr_table *mrt) 11191da177e4SLinus Torvalds { 11201da177e4SLinus Torvalds int i; 1121d17fa6faSEric Dumazet LIST_HEAD(list); 1122862465f2SPatrick McHardy struct mfc_cache *c, *next; 11231da177e4SLinus Torvalds 11241da177e4SLinus Torvalds /* 11251da177e4SLinus Torvalds * Shut down all active vif entries 11261da177e4SLinus Torvalds */ 11270c12295aSPatrick McHardy for (i = 0; i < mrt->maxvif; i++) { 11280c12295aSPatrick McHardy if (!(mrt->vif_table[i].flags&VIFF_STATIC)) 11290c12295aSPatrick McHardy vif_delete(mrt, i, 0, &list); 11301da177e4SLinus Torvalds } 1131d17fa6faSEric Dumazet unregister_netdevice_many(&list); 11321da177e4SLinus Torvalds 11331da177e4SLinus Torvalds /* 11341da177e4SLinus Torvalds * Wipe the cache 11351da177e4SLinus Torvalds */ 11361da177e4SLinus Torvalds for (i = 0; i < MFC_LINES; i++) { 11370c12295aSPatrick McHardy list_for_each_entry_safe(c, next, &mrt->mfc_cache_array[i], list) { 1138862465f2SPatrick McHardy if (c->mfc_flags&MFC_STATIC) 11391da177e4SLinus Torvalds continue; 11401da177e4SLinus Torvalds write_lock_bh(&mrt_lock); 1141862465f2SPatrick McHardy list_del(&c->list); 11421da177e4SLinus Torvalds write_unlock_bh(&mrt_lock); 11431da177e4SLinus Torvalds 11445c0a66f5SBenjamin Thery ipmr_cache_free(c); 11451da177e4SLinus Torvalds } 11461da177e4SLinus Torvalds } 11471da177e4SLinus Torvalds 11480c12295aSPatrick McHardy if (atomic_read(&mrt->cache_resolve_queue_len) != 0) { 11491da177e4SLinus Torvalds spin_lock_bh(&mfc_unres_lock); 11500c12295aSPatrick McHardy list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) { 1151862465f2SPatrick McHardy list_del(&c->list); 11520c12295aSPatrick McHardy ipmr_destroy_unres(mrt, c); 11531da177e4SLinus Torvalds } 11541da177e4SLinus Torvalds spin_unlock_bh(&mfc_unres_lock); 11551da177e4SLinus Torvalds } 11561da177e4SLinus Torvalds } 11571da177e4SLinus Torvalds 11581da177e4SLinus Torvalds static void mrtsock_destruct(struct sock *sk) 11591da177e4SLinus Torvalds { 11604feb88e5SBenjamin Thery struct net *net = sock_net(sk); 1161f0ad0860SPatrick McHardy struct mr_table *mrt; 11624feb88e5SBenjamin Thery 11631da177e4SLinus Torvalds rtnl_lock(); 1164f0ad0860SPatrick McHardy ipmr_for_each_table(mrt, net) { 11650c12295aSPatrick McHardy if (sk == mrt->mroute_sk) { 11664feb88e5SBenjamin Thery IPV4_DEVCONF_ALL(net, MC_FORWARDING)--; 11671da177e4SLinus Torvalds 11681da177e4SLinus Torvalds write_lock_bh(&mrt_lock); 11690c12295aSPatrick McHardy mrt->mroute_sk = NULL; 11701da177e4SLinus Torvalds write_unlock_bh(&mrt_lock); 11711da177e4SLinus Torvalds 11720c12295aSPatrick McHardy mroute_clean_tables(mrt); 11731da177e4SLinus Torvalds } 1174f0ad0860SPatrick McHardy } 11751da177e4SLinus Torvalds rtnl_unlock(); 11761da177e4SLinus Torvalds } 11771da177e4SLinus Torvalds 11781da177e4SLinus Torvalds /* 11791da177e4SLinus Torvalds * Socket options and virtual interface manipulation. The whole 11801da177e4SLinus Torvalds * virtual interface system is a complete heap, but unfortunately 11811da177e4SLinus Torvalds * that's how BSD mrouted happens to think. Maybe one day with a proper 11821da177e4SLinus Torvalds * MOSPF/PIM router set up we can clean this up. 11831da177e4SLinus Torvalds */ 11841da177e4SLinus Torvalds 1185b7058842SDavid S. Miller int ip_mroute_setsockopt(struct sock *sk, int optname, char __user *optval, unsigned int optlen) 11861da177e4SLinus Torvalds { 11871da177e4SLinus Torvalds int ret; 11881da177e4SLinus Torvalds struct vifctl vif; 11891da177e4SLinus Torvalds struct mfcctl mfc; 11904feb88e5SBenjamin Thery struct net *net = sock_net(sk); 1191f0ad0860SPatrick McHardy struct mr_table *mrt; 1192f0ad0860SPatrick McHardy 1193f0ad0860SPatrick McHardy mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT); 1194f0ad0860SPatrick McHardy if (mrt == NULL) 1195f0ad0860SPatrick McHardy return -ENOENT; 11961da177e4SLinus Torvalds 1197132adf54SStephen Hemminger if (optname != MRT_INIT) { 11980c12295aSPatrick McHardy if (sk != mrt->mroute_sk && !capable(CAP_NET_ADMIN)) 11991da177e4SLinus Torvalds return -EACCES; 12001da177e4SLinus Torvalds } 12011da177e4SLinus Torvalds 1202132adf54SStephen Hemminger switch (optname) { 12031da177e4SLinus Torvalds case MRT_INIT: 12041da177e4SLinus Torvalds if (sk->sk_type != SOCK_RAW || 1205c720c7e8SEric Dumazet inet_sk(sk)->inet_num != IPPROTO_IGMP) 12061da177e4SLinus Torvalds return -EOPNOTSUPP; 12071da177e4SLinus Torvalds if (optlen != sizeof(int)) 12081da177e4SLinus Torvalds return -ENOPROTOOPT; 12091da177e4SLinus Torvalds 12101da177e4SLinus Torvalds rtnl_lock(); 12110c12295aSPatrick McHardy if (mrt->mroute_sk) { 12121da177e4SLinus Torvalds rtnl_unlock(); 12131da177e4SLinus Torvalds return -EADDRINUSE; 12141da177e4SLinus Torvalds } 12151da177e4SLinus Torvalds 12161da177e4SLinus Torvalds ret = ip_ra_control(sk, 1, mrtsock_destruct); 12171da177e4SLinus Torvalds if (ret == 0) { 12181da177e4SLinus Torvalds write_lock_bh(&mrt_lock); 12190c12295aSPatrick McHardy mrt->mroute_sk = sk; 12201da177e4SLinus Torvalds write_unlock_bh(&mrt_lock); 12211da177e4SLinus Torvalds 12224feb88e5SBenjamin Thery IPV4_DEVCONF_ALL(net, MC_FORWARDING)++; 12231da177e4SLinus Torvalds } 12241da177e4SLinus Torvalds rtnl_unlock(); 12251da177e4SLinus Torvalds return ret; 12261da177e4SLinus Torvalds case MRT_DONE: 12270c12295aSPatrick McHardy if (sk != mrt->mroute_sk) 12281da177e4SLinus Torvalds return -EACCES; 12291da177e4SLinus Torvalds return ip_ra_control(sk, 0, NULL); 12301da177e4SLinus Torvalds case MRT_ADD_VIF: 12311da177e4SLinus Torvalds case MRT_DEL_VIF: 12321da177e4SLinus Torvalds if (optlen != sizeof(vif)) 12331da177e4SLinus Torvalds return -EINVAL; 12341da177e4SLinus Torvalds if (copy_from_user(&vif, optval, sizeof(vif))) 12351da177e4SLinus Torvalds return -EFAULT; 12361da177e4SLinus Torvalds if (vif.vifc_vifi >= MAXVIFS) 12371da177e4SLinus Torvalds return -ENFILE; 12381da177e4SLinus Torvalds rtnl_lock(); 12391da177e4SLinus Torvalds if (optname == MRT_ADD_VIF) { 12400c12295aSPatrick McHardy ret = vif_add(net, mrt, &vif, sk == mrt->mroute_sk); 12411da177e4SLinus Torvalds } else { 12420c12295aSPatrick McHardy ret = vif_delete(mrt, vif.vifc_vifi, 0, NULL); 12431da177e4SLinus Torvalds } 12441da177e4SLinus Torvalds rtnl_unlock(); 12451da177e4SLinus Torvalds return ret; 12461da177e4SLinus Torvalds 12471da177e4SLinus Torvalds /* 12481da177e4SLinus Torvalds * Manipulate the forwarding caches. These live 12491da177e4SLinus Torvalds * in a sort of kernel/user symbiosis. 12501da177e4SLinus Torvalds */ 12511da177e4SLinus Torvalds case MRT_ADD_MFC: 12521da177e4SLinus Torvalds case MRT_DEL_MFC: 12531da177e4SLinus Torvalds if (optlen != sizeof(mfc)) 12541da177e4SLinus Torvalds return -EINVAL; 12551da177e4SLinus Torvalds if (copy_from_user(&mfc, optval, sizeof(mfc))) 12561da177e4SLinus Torvalds return -EFAULT; 12571da177e4SLinus Torvalds rtnl_lock(); 12581da177e4SLinus Torvalds if (optname == MRT_DEL_MFC) 12590c12295aSPatrick McHardy ret = ipmr_mfc_delete(mrt, &mfc); 12601da177e4SLinus Torvalds else 12610c12295aSPatrick McHardy ret = ipmr_mfc_add(net, mrt, &mfc, sk == mrt->mroute_sk); 12621da177e4SLinus Torvalds rtnl_unlock(); 12631da177e4SLinus Torvalds return ret; 12641da177e4SLinus Torvalds /* 12651da177e4SLinus Torvalds * Control PIM assert. 12661da177e4SLinus Torvalds */ 12671da177e4SLinus Torvalds case MRT_ASSERT: 12681da177e4SLinus Torvalds { 12691da177e4SLinus Torvalds int v; 12701da177e4SLinus Torvalds if (get_user(v,(int __user *)optval)) 12711da177e4SLinus Torvalds return -EFAULT; 12720c12295aSPatrick McHardy mrt->mroute_do_assert = (v) ? 1 : 0; 12731da177e4SLinus Torvalds return 0; 12741da177e4SLinus Torvalds } 12751da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM 12761da177e4SLinus Torvalds case MRT_PIM: 12771da177e4SLinus Torvalds { 1278ba93ef74SStephen Hemminger int v; 1279ba93ef74SStephen Hemminger 12801da177e4SLinus Torvalds if (get_user(v,(int __user *)optval)) 12811da177e4SLinus Torvalds return -EFAULT; 12821da177e4SLinus Torvalds v = (v) ? 1 : 0; 1283ba93ef74SStephen Hemminger 12841da177e4SLinus Torvalds rtnl_lock(); 12851da177e4SLinus Torvalds ret = 0; 12860c12295aSPatrick McHardy if (v != mrt->mroute_do_pim) { 12870c12295aSPatrick McHardy mrt->mroute_do_pim = v; 12880c12295aSPatrick McHardy mrt->mroute_do_assert = v; 12891da177e4SLinus Torvalds } 12901da177e4SLinus Torvalds rtnl_unlock(); 12911da177e4SLinus Torvalds return ret; 12921da177e4SLinus Torvalds } 12931da177e4SLinus Torvalds #endif 1294f0ad0860SPatrick McHardy #ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES 1295f0ad0860SPatrick McHardy case MRT_TABLE: 1296f0ad0860SPatrick McHardy { 1297f0ad0860SPatrick McHardy u32 v; 1298f0ad0860SPatrick McHardy 1299f0ad0860SPatrick McHardy if (optlen != sizeof(u32)) 1300f0ad0860SPatrick McHardy return -EINVAL; 1301f0ad0860SPatrick McHardy if (get_user(v, (u32 __user *)optval)) 1302f0ad0860SPatrick McHardy return -EFAULT; 1303f0ad0860SPatrick McHardy if (sk == mrt->mroute_sk) 1304f0ad0860SPatrick McHardy return -EBUSY; 1305f0ad0860SPatrick McHardy 1306f0ad0860SPatrick McHardy rtnl_lock(); 1307f0ad0860SPatrick McHardy ret = 0; 1308f0ad0860SPatrick McHardy if (!ipmr_new_table(net, v)) 1309f0ad0860SPatrick McHardy ret = -ENOMEM; 1310f0ad0860SPatrick McHardy raw_sk(sk)->ipmr_table = v; 1311f0ad0860SPatrick McHardy rtnl_unlock(); 1312f0ad0860SPatrick McHardy return ret; 1313f0ad0860SPatrick McHardy } 1314f0ad0860SPatrick McHardy #endif 13151da177e4SLinus Torvalds /* 13161da177e4SLinus Torvalds * Spurious command, or MRT_VERSION which you cannot 13171da177e4SLinus Torvalds * set. 13181da177e4SLinus Torvalds */ 13191da177e4SLinus Torvalds default: 13201da177e4SLinus Torvalds return -ENOPROTOOPT; 13211da177e4SLinus Torvalds } 13221da177e4SLinus Torvalds } 13231da177e4SLinus Torvalds 13241da177e4SLinus Torvalds /* 13251da177e4SLinus Torvalds * Getsock opt support for the multicast routing system. 13261da177e4SLinus Torvalds */ 13271da177e4SLinus Torvalds 13281da177e4SLinus Torvalds int ip_mroute_getsockopt(struct sock *sk, int optname, char __user *optval, int __user *optlen) 13291da177e4SLinus Torvalds { 13301da177e4SLinus Torvalds int olr; 13311da177e4SLinus Torvalds int val; 13324feb88e5SBenjamin Thery struct net *net = sock_net(sk); 1333f0ad0860SPatrick McHardy struct mr_table *mrt; 1334f0ad0860SPatrick McHardy 1335f0ad0860SPatrick McHardy mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT); 1336f0ad0860SPatrick McHardy if (mrt == NULL) 1337f0ad0860SPatrick McHardy return -ENOENT; 13381da177e4SLinus Torvalds 13391da177e4SLinus Torvalds if (optname != MRT_VERSION && 13401da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM 13411da177e4SLinus Torvalds optname!=MRT_PIM && 13421da177e4SLinus Torvalds #endif 13431da177e4SLinus Torvalds optname!=MRT_ASSERT) 13441da177e4SLinus Torvalds return -ENOPROTOOPT; 13451da177e4SLinus Torvalds 13461da177e4SLinus Torvalds if (get_user(olr, optlen)) 13471da177e4SLinus Torvalds return -EFAULT; 13481da177e4SLinus Torvalds 13491da177e4SLinus Torvalds olr = min_t(unsigned int, olr, sizeof(int)); 13501da177e4SLinus Torvalds if (olr < 0) 13511da177e4SLinus Torvalds return -EINVAL; 13521da177e4SLinus Torvalds 13531da177e4SLinus Torvalds if (put_user(olr, optlen)) 13541da177e4SLinus Torvalds return -EFAULT; 13551da177e4SLinus Torvalds if (optname == MRT_VERSION) 13561da177e4SLinus Torvalds val = 0x0305; 13571da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM 13581da177e4SLinus Torvalds else if (optname == MRT_PIM) 13590c12295aSPatrick McHardy val = mrt->mroute_do_pim; 13601da177e4SLinus Torvalds #endif 13611da177e4SLinus Torvalds else 13620c12295aSPatrick McHardy val = mrt->mroute_do_assert; 13631da177e4SLinus Torvalds if (copy_to_user(optval, &val, olr)) 13641da177e4SLinus Torvalds return -EFAULT; 13651da177e4SLinus Torvalds return 0; 13661da177e4SLinus Torvalds } 13671da177e4SLinus Torvalds 13681da177e4SLinus Torvalds /* 13691da177e4SLinus Torvalds * The IP multicast ioctl support routines. 13701da177e4SLinus Torvalds */ 13711da177e4SLinus Torvalds 13721da177e4SLinus Torvalds int ipmr_ioctl(struct sock *sk, int cmd, void __user *arg) 13731da177e4SLinus Torvalds { 13741da177e4SLinus Torvalds struct sioc_sg_req sr; 13751da177e4SLinus Torvalds struct sioc_vif_req vr; 13761da177e4SLinus Torvalds struct vif_device *vif; 13771da177e4SLinus Torvalds struct mfc_cache *c; 13784feb88e5SBenjamin Thery struct net *net = sock_net(sk); 1379f0ad0860SPatrick McHardy struct mr_table *mrt; 1380f0ad0860SPatrick McHardy 1381f0ad0860SPatrick McHardy mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT); 1382f0ad0860SPatrick McHardy if (mrt == NULL) 1383f0ad0860SPatrick McHardy return -ENOENT; 13841da177e4SLinus Torvalds 1385132adf54SStephen Hemminger switch (cmd) { 13861da177e4SLinus Torvalds case SIOCGETVIFCNT: 13871da177e4SLinus Torvalds if (copy_from_user(&vr, arg, sizeof(vr))) 13881da177e4SLinus Torvalds return -EFAULT; 13890c12295aSPatrick McHardy if (vr.vifi >= mrt->maxvif) 13901da177e4SLinus Torvalds return -EINVAL; 13911da177e4SLinus Torvalds read_lock(&mrt_lock); 13920c12295aSPatrick McHardy vif = &mrt->vif_table[vr.vifi]; 13930c12295aSPatrick McHardy if (VIF_EXISTS(mrt, vr.vifi)) { 13941da177e4SLinus Torvalds vr.icount = vif->pkt_in; 13951da177e4SLinus Torvalds vr.ocount = vif->pkt_out; 13961da177e4SLinus Torvalds vr.ibytes = vif->bytes_in; 13971da177e4SLinus Torvalds vr.obytes = vif->bytes_out; 13981da177e4SLinus Torvalds read_unlock(&mrt_lock); 13991da177e4SLinus Torvalds 14001da177e4SLinus Torvalds if (copy_to_user(arg, &vr, sizeof(vr))) 14011da177e4SLinus Torvalds return -EFAULT; 14021da177e4SLinus Torvalds return 0; 14031da177e4SLinus Torvalds } 14041da177e4SLinus Torvalds read_unlock(&mrt_lock); 14051da177e4SLinus Torvalds return -EADDRNOTAVAIL; 14061da177e4SLinus Torvalds case SIOCGETSGCNT: 14071da177e4SLinus Torvalds if (copy_from_user(&sr, arg, sizeof(sr))) 14081da177e4SLinus Torvalds return -EFAULT; 14091da177e4SLinus Torvalds 14101da177e4SLinus Torvalds read_lock(&mrt_lock); 14110c12295aSPatrick McHardy c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr); 14121da177e4SLinus Torvalds if (c) { 14131da177e4SLinus Torvalds sr.pktcnt = c->mfc_un.res.pkt; 14141da177e4SLinus Torvalds sr.bytecnt = c->mfc_un.res.bytes; 14151da177e4SLinus Torvalds sr.wrong_if = c->mfc_un.res.wrong_if; 14161da177e4SLinus Torvalds read_unlock(&mrt_lock); 14171da177e4SLinus Torvalds 14181da177e4SLinus Torvalds if (copy_to_user(arg, &sr, sizeof(sr))) 14191da177e4SLinus Torvalds return -EFAULT; 14201da177e4SLinus Torvalds return 0; 14211da177e4SLinus Torvalds } 14221da177e4SLinus Torvalds read_unlock(&mrt_lock); 14231da177e4SLinus Torvalds return -EADDRNOTAVAIL; 14241da177e4SLinus Torvalds default: 14251da177e4SLinus Torvalds return -ENOIOCTLCMD; 14261da177e4SLinus Torvalds } 14271da177e4SLinus Torvalds } 14281da177e4SLinus Torvalds 14291da177e4SLinus Torvalds 14301da177e4SLinus Torvalds static int ipmr_device_event(struct notifier_block *this, unsigned long event, void *ptr) 14311da177e4SLinus Torvalds { 1432e9dc8653SEric W. Biederman struct net_device *dev = ptr; 14334feb88e5SBenjamin Thery struct net *net = dev_net(dev); 1434f0ad0860SPatrick McHardy struct mr_table *mrt; 14351da177e4SLinus Torvalds struct vif_device *v; 14361da177e4SLinus Torvalds int ct; 1437d17fa6faSEric Dumazet LIST_HEAD(list); 1438e9dc8653SEric W. Biederman 14391da177e4SLinus Torvalds if (event != NETDEV_UNREGISTER) 14401da177e4SLinus Torvalds return NOTIFY_DONE; 1441f0ad0860SPatrick McHardy 1442f0ad0860SPatrick McHardy ipmr_for_each_table(mrt, net) { 14430c12295aSPatrick McHardy v = &mrt->vif_table[0]; 14440c12295aSPatrick McHardy for (ct = 0; ct < mrt->maxvif; ct++, v++) { 1445e9dc8653SEric W. Biederman if (v->dev == dev) 14460c12295aSPatrick McHardy vif_delete(mrt, ct, 1, &list); 14471da177e4SLinus Torvalds } 1448f0ad0860SPatrick McHardy } 1449d17fa6faSEric Dumazet unregister_netdevice_many(&list); 14501da177e4SLinus Torvalds return NOTIFY_DONE; 14511da177e4SLinus Torvalds } 14521da177e4SLinus Torvalds 14531da177e4SLinus Torvalds 14541da177e4SLinus Torvalds static struct notifier_block ip_mr_notifier = { 14551da177e4SLinus Torvalds .notifier_call = ipmr_device_event, 14561da177e4SLinus Torvalds }; 14571da177e4SLinus Torvalds 14581da177e4SLinus Torvalds /* 14591da177e4SLinus Torvalds * Encapsulate a packet by attaching a valid IPIP header to it. 14601da177e4SLinus Torvalds * This avoids tunnel drivers and other mess and gives us the speed so 14611da177e4SLinus Torvalds * important for multicast video. 14621da177e4SLinus Torvalds */ 14631da177e4SLinus Torvalds 1464114c7844SAl Viro static void ip_encap(struct sk_buff *skb, __be32 saddr, __be32 daddr) 14651da177e4SLinus Torvalds { 14668856dfa3SArnaldo Carvalho de Melo struct iphdr *iph; 1467eddc9ec5SArnaldo Carvalho de Melo struct iphdr *old_iph = ip_hdr(skb); 14688856dfa3SArnaldo Carvalho de Melo 14698856dfa3SArnaldo Carvalho de Melo skb_push(skb, sizeof(struct iphdr)); 1470b0e380b1SArnaldo Carvalho de Melo skb->transport_header = skb->network_header; 14718856dfa3SArnaldo Carvalho de Melo skb_reset_network_header(skb); 1472eddc9ec5SArnaldo Carvalho de Melo iph = ip_hdr(skb); 14731da177e4SLinus Torvalds 14741da177e4SLinus Torvalds iph->version = 4; 1475e023dd64SArnaldo Carvalho de Melo iph->tos = old_iph->tos; 1476e023dd64SArnaldo Carvalho de Melo iph->ttl = old_iph->ttl; 14771da177e4SLinus Torvalds iph->frag_off = 0; 14781da177e4SLinus Torvalds iph->daddr = daddr; 14791da177e4SLinus Torvalds iph->saddr = saddr; 14801da177e4SLinus Torvalds iph->protocol = IPPROTO_IPIP; 14811da177e4SLinus Torvalds iph->ihl = 5; 14821da177e4SLinus Torvalds iph->tot_len = htons(skb->len); 1483adf30907SEric Dumazet ip_select_ident(iph, skb_dst(skb), NULL); 14841da177e4SLinus Torvalds ip_send_check(iph); 14851da177e4SLinus Torvalds 14861da177e4SLinus Torvalds memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt)); 14871da177e4SLinus Torvalds nf_reset(skb); 14881da177e4SLinus Torvalds } 14891da177e4SLinus Torvalds 14901da177e4SLinus Torvalds static inline int ipmr_forward_finish(struct sk_buff *skb) 14911da177e4SLinus Torvalds { 14921da177e4SLinus Torvalds struct ip_options * opt = &(IPCB(skb)->opt); 14931da177e4SLinus Torvalds 1494adf30907SEric Dumazet IP_INC_STATS_BH(dev_net(skb_dst(skb)->dev), IPSTATS_MIB_OUTFORWDATAGRAMS); 14951da177e4SLinus Torvalds 14961da177e4SLinus Torvalds if (unlikely(opt->optlen)) 14971da177e4SLinus Torvalds ip_forward_options(skb); 14981da177e4SLinus Torvalds 14991da177e4SLinus Torvalds return dst_output(skb); 15001da177e4SLinus Torvalds } 15011da177e4SLinus Torvalds 15021da177e4SLinus Torvalds /* 15031da177e4SLinus Torvalds * Processing handlers for ipmr_forward 15041da177e4SLinus Torvalds */ 15051da177e4SLinus Torvalds 15060c12295aSPatrick McHardy static void ipmr_queue_xmit(struct net *net, struct mr_table *mrt, 15070c12295aSPatrick McHardy struct sk_buff *skb, struct mfc_cache *c, int vifi) 15081da177e4SLinus Torvalds { 1509eddc9ec5SArnaldo Carvalho de Melo const struct iphdr *iph = ip_hdr(skb); 15100c12295aSPatrick McHardy struct vif_device *vif = &mrt->vif_table[vifi]; 15111da177e4SLinus Torvalds struct net_device *dev; 15121da177e4SLinus Torvalds struct rtable *rt; 15131da177e4SLinus Torvalds int encap = 0; 15141da177e4SLinus Torvalds 15151da177e4SLinus Torvalds if (vif->dev == NULL) 15161da177e4SLinus Torvalds goto out_free; 15171da177e4SLinus Torvalds 15181da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM 15191da177e4SLinus Torvalds if (vif->flags & VIFF_REGISTER) { 15201da177e4SLinus Torvalds vif->pkt_out++; 15211da177e4SLinus Torvalds vif->bytes_out += skb->len; 1522cf3677aeSPavel Emelyanov vif->dev->stats.tx_bytes += skb->len; 1523cf3677aeSPavel Emelyanov vif->dev->stats.tx_packets++; 15240c12295aSPatrick McHardy ipmr_cache_report(mrt, skb, vifi, IGMPMSG_WHOLEPKT); 152569ebbf58SIlpo Järvinen goto out_free; 15261da177e4SLinus Torvalds } 15271da177e4SLinus Torvalds #endif 15281da177e4SLinus Torvalds 15291da177e4SLinus Torvalds if (vif->flags&VIFF_TUNNEL) { 15301da177e4SLinus Torvalds struct flowi fl = { .oif = vif->link, 15311da177e4SLinus Torvalds .nl_u = { .ip4_u = 15321da177e4SLinus Torvalds { .daddr = vif->remote, 15331da177e4SLinus Torvalds .saddr = vif->local, 15341da177e4SLinus Torvalds .tos = RT_TOS(iph->tos) } }, 15351da177e4SLinus Torvalds .proto = IPPROTO_IPIP }; 15364feb88e5SBenjamin Thery if (ip_route_output_key(net, &rt, &fl)) 15371da177e4SLinus Torvalds goto out_free; 15381da177e4SLinus Torvalds encap = sizeof(struct iphdr); 15391da177e4SLinus Torvalds } else { 15401da177e4SLinus Torvalds struct flowi fl = { .oif = vif->link, 15411da177e4SLinus Torvalds .nl_u = { .ip4_u = 15421da177e4SLinus Torvalds { .daddr = iph->daddr, 15431da177e4SLinus Torvalds .tos = RT_TOS(iph->tos) } }, 15441da177e4SLinus Torvalds .proto = IPPROTO_IPIP }; 15454feb88e5SBenjamin Thery if (ip_route_output_key(net, &rt, &fl)) 15461da177e4SLinus Torvalds goto out_free; 15471da177e4SLinus Torvalds } 15481da177e4SLinus Torvalds 15491da177e4SLinus Torvalds dev = rt->u.dst.dev; 15501da177e4SLinus Torvalds 15511da177e4SLinus Torvalds if (skb->len+encap > dst_mtu(&rt->u.dst) && (ntohs(iph->frag_off) & IP_DF)) { 15521da177e4SLinus Torvalds /* Do not fragment multicasts. Alas, IPv4 does not 15531da177e4SLinus Torvalds allow to send ICMP, so that packets will disappear 15541da177e4SLinus Torvalds to blackhole. 15551da177e4SLinus Torvalds */ 15561da177e4SLinus Torvalds 15577c73a6faSPavel Emelyanov IP_INC_STATS_BH(dev_net(dev), IPSTATS_MIB_FRAGFAILS); 15581da177e4SLinus Torvalds ip_rt_put(rt); 15591da177e4SLinus Torvalds goto out_free; 15601da177e4SLinus Torvalds } 15611da177e4SLinus Torvalds 15621da177e4SLinus Torvalds encap += LL_RESERVED_SPACE(dev) + rt->u.dst.header_len; 15631da177e4SLinus Torvalds 15641da177e4SLinus Torvalds if (skb_cow(skb, encap)) { 15651da177e4SLinus Torvalds ip_rt_put(rt); 15661da177e4SLinus Torvalds goto out_free; 15671da177e4SLinus Torvalds } 15681da177e4SLinus Torvalds 15691da177e4SLinus Torvalds vif->pkt_out++; 15701da177e4SLinus Torvalds vif->bytes_out += skb->len; 15711da177e4SLinus Torvalds 1572adf30907SEric Dumazet skb_dst_drop(skb); 1573adf30907SEric Dumazet skb_dst_set(skb, &rt->u.dst); 1574eddc9ec5SArnaldo Carvalho de Melo ip_decrease_ttl(ip_hdr(skb)); 15751da177e4SLinus Torvalds 15761da177e4SLinus Torvalds /* FIXME: forward and output firewalls used to be called here. 15771da177e4SLinus Torvalds * What do we do with netfilter? -- RR */ 15781da177e4SLinus Torvalds if (vif->flags & VIFF_TUNNEL) { 15791da177e4SLinus Torvalds ip_encap(skb, vif->local, vif->remote); 15801da177e4SLinus Torvalds /* FIXME: extra output firewall step used to be here. --RR */ 15812f4c02d4SPavel Emelyanov vif->dev->stats.tx_packets++; 15822f4c02d4SPavel Emelyanov vif->dev->stats.tx_bytes += skb->len; 15831da177e4SLinus Torvalds } 15841da177e4SLinus Torvalds 15851da177e4SLinus Torvalds IPCB(skb)->flags |= IPSKB_FORWARDED; 15861da177e4SLinus Torvalds 15871da177e4SLinus Torvalds /* 15881da177e4SLinus Torvalds * RFC1584 teaches, that DVMRP/PIM router must deliver packets locally 15891da177e4SLinus Torvalds * not only before forwarding, but after forwarding on all output 15901da177e4SLinus Torvalds * interfaces. It is clear, if mrouter runs a multicasting 15911da177e4SLinus Torvalds * program, it should receive packets not depending to what interface 15921da177e4SLinus Torvalds * program is joined. 15931da177e4SLinus Torvalds * If we will not make it, the program will have to join on all 15941da177e4SLinus Torvalds * interfaces. On the other hand, multihoming host (or router, but 15951da177e4SLinus Torvalds * not mrouter) cannot join to more than one interface - it will 15961da177e4SLinus Torvalds * result in receiving multiple packets. 15971da177e4SLinus Torvalds */ 15986e23ae2aSPatrick McHardy NF_HOOK(PF_INET, NF_INET_FORWARD, skb, skb->dev, dev, 15991da177e4SLinus Torvalds ipmr_forward_finish); 16001da177e4SLinus Torvalds return; 16011da177e4SLinus Torvalds 16021da177e4SLinus Torvalds out_free: 16031da177e4SLinus Torvalds kfree_skb(skb); 16041da177e4SLinus Torvalds return; 16051da177e4SLinus Torvalds } 16061da177e4SLinus Torvalds 16070c12295aSPatrick McHardy static int ipmr_find_vif(struct mr_table *mrt, struct net_device *dev) 16081da177e4SLinus Torvalds { 16091da177e4SLinus Torvalds int ct; 16100c12295aSPatrick McHardy 16110c12295aSPatrick McHardy for (ct = mrt->maxvif-1; ct >= 0; ct--) { 16120c12295aSPatrick McHardy if (mrt->vif_table[ct].dev == dev) 16131da177e4SLinus Torvalds break; 16141da177e4SLinus Torvalds } 16151da177e4SLinus Torvalds return ct; 16161da177e4SLinus Torvalds } 16171da177e4SLinus Torvalds 16181da177e4SLinus Torvalds /* "local" means that we should preserve one skb (for local delivery) */ 16191da177e4SLinus Torvalds 16200c12295aSPatrick McHardy static int ip_mr_forward(struct net *net, struct mr_table *mrt, 16210c12295aSPatrick McHardy struct sk_buff *skb, struct mfc_cache *cache, 16220c12295aSPatrick McHardy int local) 16231da177e4SLinus Torvalds { 16241da177e4SLinus Torvalds int psend = -1; 16251da177e4SLinus Torvalds int vif, ct; 16261da177e4SLinus Torvalds 16271da177e4SLinus Torvalds vif = cache->mfc_parent; 16281da177e4SLinus Torvalds cache->mfc_un.res.pkt++; 16291da177e4SLinus Torvalds cache->mfc_un.res.bytes += skb->len; 16301da177e4SLinus Torvalds 16311da177e4SLinus Torvalds /* 16321da177e4SLinus Torvalds * Wrong interface: drop packet and (maybe) send PIM assert. 16331da177e4SLinus Torvalds */ 16340c12295aSPatrick McHardy if (mrt->vif_table[vif].dev != skb->dev) { 16351da177e4SLinus Torvalds int true_vifi; 16361da177e4SLinus Torvalds 1637511c3f92SEric Dumazet if (skb_rtable(skb)->fl.iif == 0) { 16381da177e4SLinus Torvalds /* It is our own packet, looped back. 16391da177e4SLinus Torvalds Very complicated situation... 16401da177e4SLinus Torvalds 16411da177e4SLinus Torvalds The best workaround until routing daemons will be 16421da177e4SLinus Torvalds fixed is not to redistribute packet, if it was 16431da177e4SLinus Torvalds send through wrong interface. It means, that 16441da177e4SLinus Torvalds multicast applications WILL NOT work for 16451da177e4SLinus Torvalds (S,G), which have default multicast route pointing 16461da177e4SLinus Torvalds to wrong oif. In any case, it is not a good 16471da177e4SLinus Torvalds idea to use multicasting applications on router. 16481da177e4SLinus Torvalds */ 16491da177e4SLinus Torvalds goto dont_forward; 16501da177e4SLinus Torvalds } 16511da177e4SLinus Torvalds 16521da177e4SLinus Torvalds cache->mfc_un.res.wrong_if++; 16530c12295aSPatrick McHardy true_vifi = ipmr_find_vif(mrt, skb->dev); 16541da177e4SLinus Torvalds 16550c12295aSPatrick McHardy if (true_vifi >= 0 && mrt->mroute_do_assert && 16561da177e4SLinus Torvalds /* pimsm uses asserts, when switching from RPT to SPT, 16571da177e4SLinus Torvalds so that we cannot check that packet arrived on an oif. 16581da177e4SLinus Torvalds It is bad, but otherwise we would need to move pretty 16591da177e4SLinus Torvalds large chunk of pimd to kernel. Ough... --ANK 16601da177e4SLinus Torvalds */ 16610c12295aSPatrick McHardy (mrt->mroute_do_pim || 16626f9374a9SBenjamin Thery cache->mfc_un.res.ttls[true_vifi] < 255) && 16631da177e4SLinus Torvalds time_after(jiffies, 16641da177e4SLinus Torvalds cache->mfc_un.res.last_assert + MFC_ASSERT_THRESH)) { 16651da177e4SLinus Torvalds cache->mfc_un.res.last_assert = jiffies; 16660c12295aSPatrick McHardy ipmr_cache_report(mrt, skb, true_vifi, IGMPMSG_WRONGVIF); 16671da177e4SLinus Torvalds } 16681da177e4SLinus Torvalds goto dont_forward; 16691da177e4SLinus Torvalds } 16701da177e4SLinus Torvalds 16710c12295aSPatrick McHardy mrt->vif_table[vif].pkt_in++; 16720c12295aSPatrick McHardy mrt->vif_table[vif].bytes_in += skb->len; 16731da177e4SLinus Torvalds 16741da177e4SLinus Torvalds /* 16751da177e4SLinus Torvalds * Forward the frame 16761da177e4SLinus Torvalds */ 16771da177e4SLinus Torvalds for (ct = cache->mfc_un.res.maxvif-1; ct >= cache->mfc_un.res.minvif; ct--) { 1678eddc9ec5SArnaldo Carvalho de Melo if (ip_hdr(skb)->ttl > cache->mfc_un.res.ttls[ct]) { 16791da177e4SLinus Torvalds if (psend != -1) { 16801da177e4SLinus Torvalds struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC); 16811da177e4SLinus Torvalds if (skb2) 16820c12295aSPatrick McHardy ipmr_queue_xmit(net, mrt, skb2, cache, 16830c12295aSPatrick McHardy psend); 16841da177e4SLinus Torvalds } 16851da177e4SLinus Torvalds psend = ct; 16861da177e4SLinus Torvalds } 16871da177e4SLinus Torvalds } 16881da177e4SLinus Torvalds if (psend != -1) { 16891da177e4SLinus Torvalds if (local) { 16901da177e4SLinus Torvalds struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC); 16911da177e4SLinus Torvalds if (skb2) 16920c12295aSPatrick McHardy ipmr_queue_xmit(net, mrt, skb2, cache, psend); 16931da177e4SLinus Torvalds } else { 16940c12295aSPatrick McHardy ipmr_queue_xmit(net, mrt, skb, cache, psend); 16951da177e4SLinus Torvalds return 0; 16961da177e4SLinus Torvalds } 16971da177e4SLinus Torvalds } 16981da177e4SLinus Torvalds 16991da177e4SLinus Torvalds dont_forward: 17001da177e4SLinus Torvalds if (!local) 17011da177e4SLinus Torvalds kfree_skb(skb); 17021da177e4SLinus Torvalds return 0; 17031da177e4SLinus Torvalds } 17041da177e4SLinus Torvalds 17051da177e4SLinus Torvalds 17061da177e4SLinus Torvalds /* 17071da177e4SLinus Torvalds * Multicast packets for forwarding arrive here 17081da177e4SLinus Torvalds */ 17091da177e4SLinus Torvalds 17101da177e4SLinus Torvalds int ip_mr_input(struct sk_buff *skb) 17111da177e4SLinus Torvalds { 17121da177e4SLinus Torvalds struct mfc_cache *cache; 17134feb88e5SBenjamin Thery struct net *net = dev_net(skb->dev); 1714511c3f92SEric Dumazet int local = skb_rtable(skb)->rt_flags & RTCF_LOCAL; 1715f0ad0860SPatrick McHardy struct mr_table *mrt; 1716f0ad0860SPatrick McHardy int err; 17171da177e4SLinus Torvalds 17181da177e4SLinus Torvalds /* Packet is looped back after forward, it should not be 17191da177e4SLinus Torvalds forwarded second time, but still can be delivered locally. 17201da177e4SLinus Torvalds */ 17211da177e4SLinus Torvalds if (IPCB(skb)->flags&IPSKB_FORWARDED) 17221da177e4SLinus Torvalds goto dont_forward; 17231da177e4SLinus Torvalds 1724f0ad0860SPatrick McHardy err = ipmr_fib_lookup(net, &skb_rtable(skb)->fl, &mrt); 1725f0ad0860SPatrick McHardy if (err < 0) 1726f0ad0860SPatrick McHardy return err; 1727f0ad0860SPatrick McHardy 17281da177e4SLinus Torvalds if (!local) { 17291da177e4SLinus Torvalds if (IPCB(skb)->opt.router_alert) { 17301da177e4SLinus Torvalds if (ip_call_ra_chain(skb)) 17311da177e4SLinus Torvalds return 0; 1732eddc9ec5SArnaldo Carvalho de Melo } else if (ip_hdr(skb)->protocol == IPPROTO_IGMP){ 17331da177e4SLinus Torvalds /* IGMPv1 (and broken IGMPv2 implementations sort of 17341da177e4SLinus Torvalds Cisco IOS <= 11.2(8)) do not put router alert 17351da177e4SLinus Torvalds option to IGMP packets destined to routable 17361da177e4SLinus Torvalds groups. It is very bad, because it means 17371da177e4SLinus Torvalds that we can forward NO IGMP messages. 17381da177e4SLinus Torvalds */ 17391da177e4SLinus Torvalds read_lock(&mrt_lock); 17400c12295aSPatrick McHardy if (mrt->mroute_sk) { 17412715bcf9SPatrick McHardy nf_reset(skb); 17420c12295aSPatrick McHardy raw_rcv(mrt->mroute_sk, skb); 17431da177e4SLinus Torvalds read_unlock(&mrt_lock); 17441da177e4SLinus Torvalds return 0; 17451da177e4SLinus Torvalds } 17461da177e4SLinus Torvalds read_unlock(&mrt_lock); 17471da177e4SLinus Torvalds } 17481da177e4SLinus Torvalds } 17491da177e4SLinus Torvalds 17501da177e4SLinus Torvalds read_lock(&mrt_lock); 17510c12295aSPatrick McHardy cache = ipmr_cache_find(mrt, ip_hdr(skb)->saddr, ip_hdr(skb)->daddr); 17521da177e4SLinus Torvalds 17531da177e4SLinus Torvalds /* 17541da177e4SLinus Torvalds * No usable cache entry 17551da177e4SLinus Torvalds */ 17561da177e4SLinus Torvalds if (cache == NULL) { 17571da177e4SLinus Torvalds int vif; 17581da177e4SLinus Torvalds 17591da177e4SLinus Torvalds if (local) { 17601da177e4SLinus Torvalds struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC); 17611da177e4SLinus Torvalds ip_local_deliver(skb); 17621da177e4SLinus Torvalds if (skb2 == NULL) { 17631da177e4SLinus Torvalds read_unlock(&mrt_lock); 17641da177e4SLinus Torvalds return -ENOBUFS; 17651da177e4SLinus Torvalds } 17661da177e4SLinus Torvalds skb = skb2; 17671da177e4SLinus Torvalds } 17681da177e4SLinus Torvalds 17690c12295aSPatrick McHardy vif = ipmr_find_vif(mrt, skb->dev); 17701da177e4SLinus Torvalds if (vif >= 0) { 17710c12295aSPatrick McHardy int err = ipmr_cache_unresolved(mrt, vif, skb); 17721da177e4SLinus Torvalds read_unlock(&mrt_lock); 17731da177e4SLinus Torvalds 17741da177e4SLinus Torvalds return err; 17751da177e4SLinus Torvalds } 17761da177e4SLinus Torvalds read_unlock(&mrt_lock); 17771da177e4SLinus Torvalds kfree_skb(skb); 17781da177e4SLinus Torvalds return -ENODEV; 17791da177e4SLinus Torvalds } 17801da177e4SLinus Torvalds 17810c12295aSPatrick McHardy ip_mr_forward(net, mrt, skb, cache, local); 17821da177e4SLinus Torvalds 17831da177e4SLinus Torvalds read_unlock(&mrt_lock); 17841da177e4SLinus Torvalds 17851da177e4SLinus Torvalds if (local) 17861da177e4SLinus Torvalds return ip_local_deliver(skb); 17871da177e4SLinus Torvalds 17881da177e4SLinus Torvalds return 0; 17891da177e4SLinus Torvalds 17901da177e4SLinus Torvalds dont_forward: 17911da177e4SLinus Torvalds if (local) 17921da177e4SLinus Torvalds return ip_local_deliver(skb); 17931da177e4SLinus Torvalds kfree_skb(skb); 17941da177e4SLinus Torvalds return 0; 17951da177e4SLinus Torvalds } 17961da177e4SLinus Torvalds 1797b1879204SIlpo Järvinen #ifdef CONFIG_IP_PIMSM 1798f0ad0860SPatrick McHardy static int __pim_rcv(struct mr_table *mrt, struct sk_buff *skb, 1799f0ad0860SPatrick McHardy unsigned int pimlen) 18001da177e4SLinus Torvalds { 18011da177e4SLinus Torvalds struct net_device *reg_dev = NULL; 1802b1879204SIlpo Järvinen struct iphdr *encap; 18031da177e4SLinus Torvalds 1804b1879204SIlpo Järvinen encap = (struct iphdr *)(skb_transport_header(skb) + pimlen); 18051da177e4SLinus Torvalds /* 18061da177e4SLinus Torvalds Check that: 18071da177e4SLinus Torvalds a. packet is really destinted to a multicast group 18081da177e4SLinus Torvalds b. packet is not a NULL-REGISTER 18091da177e4SLinus Torvalds c. packet is not truncated 18101da177e4SLinus Torvalds */ 1811f97c1e0cSJoe Perches if (!ipv4_is_multicast(encap->daddr) || 18121da177e4SLinus Torvalds encap->tot_len == 0 || 1813b1879204SIlpo Järvinen ntohs(encap->tot_len) + pimlen > skb->len) 1814b1879204SIlpo Järvinen return 1; 18151da177e4SLinus Torvalds 18161da177e4SLinus Torvalds read_lock(&mrt_lock); 18170c12295aSPatrick McHardy if (mrt->mroute_reg_vif_num >= 0) 18180c12295aSPatrick McHardy reg_dev = mrt->vif_table[mrt->mroute_reg_vif_num].dev; 18191da177e4SLinus Torvalds if (reg_dev) 18201da177e4SLinus Torvalds dev_hold(reg_dev); 18211da177e4SLinus Torvalds read_unlock(&mrt_lock); 18221da177e4SLinus Torvalds 18231da177e4SLinus Torvalds if (reg_dev == NULL) 1824b1879204SIlpo Järvinen return 1; 18251da177e4SLinus Torvalds 1826b0e380b1SArnaldo Carvalho de Melo skb->mac_header = skb->network_header; 18271da177e4SLinus Torvalds skb_pull(skb, (u8*)encap - skb->data); 182831c7711bSArnaldo Carvalho de Melo skb_reset_network_header(skb); 18291da177e4SLinus Torvalds skb->dev = reg_dev; 18301da177e4SLinus Torvalds skb->protocol = htons(ETH_P_IP); 18311da177e4SLinus Torvalds skb->ip_summed = 0; 18321da177e4SLinus Torvalds skb->pkt_type = PACKET_HOST; 1833adf30907SEric Dumazet skb_dst_drop(skb); 1834cf3677aeSPavel Emelyanov reg_dev->stats.rx_bytes += skb->len; 1835cf3677aeSPavel Emelyanov reg_dev->stats.rx_packets++; 18361da177e4SLinus Torvalds nf_reset(skb); 18371da177e4SLinus Torvalds netif_rx(skb); 18381da177e4SLinus Torvalds dev_put(reg_dev); 1839b1879204SIlpo Järvinen 18401da177e4SLinus Torvalds return 0; 1841b1879204SIlpo Järvinen } 1842b1879204SIlpo Järvinen #endif 1843b1879204SIlpo Järvinen 1844b1879204SIlpo Järvinen #ifdef CONFIG_IP_PIMSM_V1 1845b1879204SIlpo Järvinen /* 1846b1879204SIlpo Järvinen * Handle IGMP messages of PIMv1 1847b1879204SIlpo Järvinen */ 1848b1879204SIlpo Järvinen 1849b1879204SIlpo Järvinen int pim_rcv_v1(struct sk_buff * skb) 1850b1879204SIlpo Järvinen { 1851b1879204SIlpo Järvinen struct igmphdr *pim; 18524feb88e5SBenjamin Thery struct net *net = dev_net(skb->dev); 1853f0ad0860SPatrick McHardy struct mr_table *mrt; 1854b1879204SIlpo Järvinen 1855b1879204SIlpo Järvinen if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr))) 1856b1879204SIlpo Järvinen goto drop; 1857b1879204SIlpo Järvinen 1858b1879204SIlpo Järvinen pim = igmp_hdr(skb); 1859b1879204SIlpo Järvinen 1860f0ad0860SPatrick McHardy if (ipmr_fib_lookup(net, &skb_rtable(skb)->fl, &mrt) < 0) 1861f0ad0860SPatrick McHardy goto drop; 1862f0ad0860SPatrick McHardy 18630c12295aSPatrick McHardy if (!mrt->mroute_do_pim || 1864b1879204SIlpo Järvinen pim->group != PIM_V1_VERSION || pim->code != PIM_V1_REGISTER) 1865b1879204SIlpo Järvinen goto drop; 1866b1879204SIlpo Järvinen 1867f0ad0860SPatrick McHardy if (__pim_rcv(mrt, skb, sizeof(*pim))) { 18681da177e4SLinus Torvalds drop: 18691da177e4SLinus Torvalds kfree_skb(skb); 1870b1879204SIlpo Järvinen } 18711da177e4SLinus Torvalds return 0; 18721da177e4SLinus Torvalds } 18731da177e4SLinus Torvalds #endif 18741da177e4SLinus Torvalds 18751da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM_V2 18761da177e4SLinus Torvalds static int pim_rcv(struct sk_buff * skb) 18771da177e4SLinus Torvalds { 18781da177e4SLinus Torvalds struct pimreghdr *pim; 1879f0ad0860SPatrick McHardy struct net *net = dev_net(skb->dev); 1880f0ad0860SPatrick McHardy struct mr_table *mrt; 18811da177e4SLinus Torvalds 1882b1879204SIlpo Järvinen if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr))) 18831da177e4SLinus Torvalds goto drop; 18841da177e4SLinus Torvalds 18859c70220bSArnaldo Carvalho de Melo pim = (struct pimreghdr *)skb_transport_header(skb); 18861da177e4SLinus Torvalds if (pim->type != ((PIM_VERSION<<4)|(PIM_REGISTER)) || 18871da177e4SLinus Torvalds (pim->flags&PIM_NULL_REGISTER) || 18881da177e4SLinus Torvalds (ip_compute_csum((void *)pim, sizeof(*pim)) != 0 && 1889d3bc23e7SAl Viro csum_fold(skb_checksum(skb, 0, skb->len, 0)))) 18901da177e4SLinus Torvalds goto drop; 18911da177e4SLinus Torvalds 1892f0ad0860SPatrick McHardy if (ipmr_fib_lookup(net, &skb_rtable(skb)->fl, &mrt) < 0) 1893f0ad0860SPatrick McHardy goto drop; 1894f0ad0860SPatrick McHardy 1895f0ad0860SPatrick McHardy if (__pim_rcv(mrt, skb, sizeof(*pim))) { 18961da177e4SLinus Torvalds drop: 18971da177e4SLinus Torvalds kfree_skb(skb); 1898b1879204SIlpo Järvinen } 18991da177e4SLinus Torvalds return 0; 19001da177e4SLinus Torvalds } 19011da177e4SLinus Torvalds #endif 19021da177e4SLinus Torvalds 19031da177e4SLinus Torvalds static int 19040c12295aSPatrick McHardy ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb, struct mfc_cache *c, 1905d658f8a0SPatrick McHardy struct rtmsg *rtm) 19061da177e4SLinus Torvalds { 19071da177e4SLinus Torvalds int ct; 19081da177e4SLinus Torvalds struct rtnexthop *nhp; 190927a884dcSArnaldo Carvalho de Melo u8 *b = skb_tail_pointer(skb); 19101da177e4SLinus Torvalds struct rtattr *mp_head; 19111da177e4SLinus Torvalds 19127438189bSNicolas Dichtel /* If cache is unresolved, don't try to parse IIF and OIF */ 19137438189bSNicolas Dichtel if (c->mfc_parent > MAXVIFS) 19147438189bSNicolas Dichtel return -ENOENT; 19157438189bSNicolas Dichtel 19160c12295aSPatrick McHardy if (VIF_EXISTS(mrt, c->mfc_parent)) 19170c12295aSPatrick McHardy RTA_PUT(skb, RTA_IIF, 4, &mrt->vif_table[c->mfc_parent].dev->ifindex); 19181da177e4SLinus Torvalds 19191da177e4SLinus Torvalds mp_head = (struct rtattr *)skb_put(skb, RTA_LENGTH(0)); 19201da177e4SLinus Torvalds 19211da177e4SLinus Torvalds for (ct = c->mfc_un.res.minvif; ct < c->mfc_un.res.maxvif; ct++) { 19220c12295aSPatrick McHardy if (VIF_EXISTS(mrt, ct) && c->mfc_un.res.ttls[ct] < 255) { 19231da177e4SLinus Torvalds if (skb_tailroom(skb) < RTA_ALIGN(RTA_ALIGN(sizeof(*nhp)) + 4)) 19241da177e4SLinus Torvalds goto rtattr_failure; 19251da177e4SLinus Torvalds nhp = (struct rtnexthop *)skb_put(skb, RTA_ALIGN(sizeof(*nhp))); 19261da177e4SLinus Torvalds nhp->rtnh_flags = 0; 19271da177e4SLinus Torvalds nhp->rtnh_hops = c->mfc_un.res.ttls[ct]; 19280c12295aSPatrick McHardy nhp->rtnh_ifindex = mrt->vif_table[ct].dev->ifindex; 19291da177e4SLinus Torvalds nhp->rtnh_len = sizeof(*nhp); 19301da177e4SLinus Torvalds } 19311da177e4SLinus Torvalds } 19321da177e4SLinus Torvalds mp_head->rta_type = RTA_MULTIPATH; 193327a884dcSArnaldo Carvalho de Melo mp_head->rta_len = skb_tail_pointer(skb) - (u8 *)mp_head; 19341da177e4SLinus Torvalds rtm->rtm_type = RTN_MULTICAST; 19351da177e4SLinus Torvalds return 1; 19361da177e4SLinus Torvalds 19371da177e4SLinus Torvalds rtattr_failure: 1938dc5fc579SArnaldo Carvalho de Melo nlmsg_trim(skb, b); 19391da177e4SLinus Torvalds return -EMSGSIZE; 19401da177e4SLinus Torvalds } 19411da177e4SLinus Torvalds 19424feb88e5SBenjamin Thery int ipmr_get_route(struct net *net, 19434feb88e5SBenjamin Thery struct sk_buff *skb, struct rtmsg *rtm, int nowait) 19441da177e4SLinus Torvalds { 19451da177e4SLinus Torvalds int err; 1946f0ad0860SPatrick McHardy struct mr_table *mrt; 19471da177e4SLinus Torvalds struct mfc_cache *cache; 1948511c3f92SEric Dumazet struct rtable *rt = skb_rtable(skb); 19491da177e4SLinus Torvalds 1950f0ad0860SPatrick McHardy mrt = ipmr_get_table(net, RT_TABLE_DEFAULT); 1951f0ad0860SPatrick McHardy if (mrt == NULL) 1952f0ad0860SPatrick McHardy return -ENOENT; 1953f0ad0860SPatrick McHardy 19541da177e4SLinus Torvalds read_lock(&mrt_lock); 19550c12295aSPatrick McHardy cache = ipmr_cache_find(mrt, rt->rt_src, rt->rt_dst); 19561da177e4SLinus Torvalds 19571da177e4SLinus Torvalds if (cache == NULL) { 195872287490SAlexey Kuznetsov struct sk_buff *skb2; 1959eddc9ec5SArnaldo Carvalho de Melo struct iphdr *iph; 19601da177e4SLinus Torvalds struct net_device *dev; 19611da177e4SLinus Torvalds int vif; 19621da177e4SLinus Torvalds 19631da177e4SLinus Torvalds if (nowait) { 19641da177e4SLinus Torvalds read_unlock(&mrt_lock); 19651da177e4SLinus Torvalds return -EAGAIN; 19661da177e4SLinus Torvalds } 19671da177e4SLinus Torvalds 19681da177e4SLinus Torvalds dev = skb->dev; 19690c12295aSPatrick McHardy if (dev == NULL || (vif = ipmr_find_vif(mrt, dev)) < 0) { 19701da177e4SLinus Torvalds read_unlock(&mrt_lock); 19711da177e4SLinus Torvalds return -ENODEV; 19721da177e4SLinus Torvalds } 197372287490SAlexey Kuznetsov skb2 = skb_clone(skb, GFP_ATOMIC); 197472287490SAlexey Kuznetsov if (!skb2) { 197572287490SAlexey Kuznetsov read_unlock(&mrt_lock); 197672287490SAlexey Kuznetsov return -ENOMEM; 197772287490SAlexey Kuznetsov } 197872287490SAlexey Kuznetsov 1979e2d1bca7SArnaldo Carvalho de Melo skb_push(skb2, sizeof(struct iphdr)); 1980e2d1bca7SArnaldo Carvalho de Melo skb_reset_network_header(skb2); 1981eddc9ec5SArnaldo Carvalho de Melo iph = ip_hdr(skb2); 1982eddc9ec5SArnaldo Carvalho de Melo iph->ihl = sizeof(struct iphdr) >> 2; 1983eddc9ec5SArnaldo Carvalho de Melo iph->saddr = rt->rt_src; 1984eddc9ec5SArnaldo Carvalho de Melo iph->daddr = rt->rt_dst; 1985eddc9ec5SArnaldo Carvalho de Melo iph->version = 0; 19860c12295aSPatrick McHardy err = ipmr_cache_unresolved(mrt, vif, skb2); 19871da177e4SLinus Torvalds read_unlock(&mrt_lock); 19881da177e4SLinus Torvalds return err; 19891da177e4SLinus Torvalds } 19901da177e4SLinus Torvalds 19911da177e4SLinus Torvalds if (!nowait && (rtm->rtm_flags&RTM_F_NOTIFY)) 19921da177e4SLinus Torvalds cache->mfc_flags |= MFC_NOTIFY; 19930c12295aSPatrick McHardy err = ipmr_fill_mroute(mrt, skb, cache, rtm); 19941da177e4SLinus Torvalds read_unlock(&mrt_lock); 19951da177e4SLinus Torvalds return err; 19961da177e4SLinus Torvalds } 19971da177e4SLinus Torvalds 19981da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 19991da177e4SLinus Torvalds /* 20001da177e4SLinus Torvalds * The /proc interfaces to multicast routing /proc/ip_mr_cache /proc/ip_mr_vif 20011da177e4SLinus Torvalds */ 20021da177e4SLinus Torvalds struct ipmr_vif_iter { 2003f6bb4514SBenjamin Thery struct seq_net_private p; 2004f0ad0860SPatrick McHardy struct mr_table *mrt; 20051da177e4SLinus Torvalds int ct; 20061da177e4SLinus Torvalds }; 20071da177e4SLinus Torvalds 2008f6bb4514SBenjamin Thery static struct vif_device *ipmr_vif_seq_idx(struct net *net, 2009f6bb4514SBenjamin Thery struct ipmr_vif_iter *iter, 20101da177e4SLinus Torvalds loff_t pos) 20111da177e4SLinus Torvalds { 2012f0ad0860SPatrick McHardy struct mr_table *mrt = iter->mrt; 20130c12295aSPatrick McHardy 20140c12295aSPatrick McHardy for (iter->ct = 0; iter->ct < mrt->maxvif; ++iter->ct) { 20150c12295aSPatrick McHardy if (!VIF_EXISTS(mrt, iter->ct)) 20161da177e4SLinus Torvalds continue; 20171da177e4SLinus Torvalds if (pos-- == 0) 20180c12295aSPatrick McHardy return &mrt->vif_table[iter->ct]; 20191da177e4SLinus Torvalds } 20201da177e4SLinus Torvalds return NULL; 20211da177e4SLinus Torvalds } 20221da177e4SLinus Torvalds 20231da177e4SLinus Torvalds static void *ipmr_vif_seq_start(struct seq_file *seq, loff_t *pos) 2024ba93ef74SStephen Hemminger __acquires(mrt_lock) 20251da177e4SLinus Torvalds { 2026f0ad0860SPatrick McHardy struct ipmr_vif_iter *iter = seq->private; 2027f6bb4514SBenjamin Thery struct net *net = seq_file_net(seq); 2028f0ad0860SPatrick McHardy struct mr_table *mrt; 2029f0ad0860SPatrick McHardy 2030f0ad0860SPatrick McHardy mrt = ipmr_get_table(net, RT_TABLE_DEFAULT); 2031f0ad0860SPatrick McHardy if (mrt == NULL) 2032f0ad0860SPatrick McHardy return ERR_PTR(-ENOENT); 2033f0ad0860SPatrick McHardy 2034f0ad0860SPatrick McHardy iter->mrt = mrt; 2035f6bb4514SBenjamin Thery 20361da177e4SLinus Torvalds read_lock(&mrt_lock); 2037f6bb4514SBenjamin Thery return *pos ? ipmr_vif_seq_idx(net, seq->private, *pos - 1) 20381da177e4SLinus Torvalds : SEQ_START_TOKEN; 20391da177e4SLinus Torvalds } 20401da177e4SLinus Torvalds 20411da177e4SLinus Torvalds static void *ipmr_vif_seq_next(struct seq_file *seq, void *v, loff_t *pos) 20421da177e4SLinus Torvalds { 20431da177e4SLinus Torvalds struct ipmr_vif_iter *iter = seq->private; 2044f6bb4514SBenjamin Thery struct net *net = seq_file_net(seq); 2045f0ad0860SPatrick McHardy struct mr_table *mrt = iter->mrt; 20461da177e4SLinus Torvalds 20471da177e4SLinus Torvalds ++*pos; 20481da177e4SLinus Torvalds if (v == SEQ_START_TOKEN) 2049f6bb4514SBenjamin Thery return ipmr_vif_seq_idx(net, iter, 0); 20501da177e4SLinus Torvalds 20510c12295aSPatrick McHardy while (++iter->ct < mrt->maxvif) { 20520c12295aSPatrick McHardy if (!VIF_EXISTS(mrt, iter->ct)) 20531da177e4SLinus Torvalds continue; 20540c12295aSPatrick McHardy return &mrt->vif_table[iter->ct]; 20551da177e4SLinus Torvalds } 20561da177e4SLinus Torvalds return NULL; 20571da177e4SLinus Torvalds } 20581da177e4SLinus Torvalds 20591da177e4SLinus Torvalds static void ipmr_vif_seq_stop(struct seq_file *seq, void *v) 2060ba93ef74SStephen Hemminger __releases(mrt_lock) 20611da177e4SLinus Torvalds { 20621da177e4SLinus Torvalds read_unlock(&mrt_lock); 20631da177e4SLinus Torvalds } 20641da177e4SLinus Torvalds 20651da177e4SLinus Torvalds static int ipmr_vif_seq_show(struct seq_file *seq, void *v) 20661da177e4SLinus Torvalds { 2067f0ad0860SPatrick McHardy struct ipmr_vif_iter *iter = seq->private; 2068f0ad0860SPatrick McHardy struct mr_table *mrt = iter->mrt; 2069f6bb4514SBenjamin Thery 20701da177e4SLinus Torvalds if (v == SEQ_START_TOKEN) { 20711da177e4SLinus Torvalds seq_puts(seq, 20721da177e4SLinus Torvalds "Interface BytesIn PktsIn BytesOut PktsOut Flags Local Remote\n"); 20731da177e4SLinus Torvalds } else { 20741da177e4SLinus Torvalds const struct vif_device *vif = v; 20751da177e4SLinus Torvalds const char *name = vif->dev ? vif->dev->name : "none"; 20761da177e4SLinus Torvalds 20771da177e4SLinus Torvalds seq_printf(seq, 20781da177e4SLinus Torvalds "%2Zd %-10s %8ld %7ld %8ld %7ld %05X %08X %08X\n", 20790c12295aSPatrick McHardy vif - mrt->vif_table, 20801da177e4SLinus Torvalds name, vif->bytes_in, vif->pkt_in, 20811da177e4SLinus Torvalds vif->bytes_out, vif->pkt_out, 20821da177e4SLinus Torvalds vif->flags, vif->local, vif->remote); 20831da177e4SLinus Torvalds } 20841da177e4SLinus Torvalds return 0; 20851da177e4SLinus Torvalds } 20861da177e4SLinus Torvalds 2087f690808eSStephen Hemminger static const struct seq_operations ipmr_vif_seq_ops = { 20881da177e4SLinus Torvalds .start = ipmr_vif_seq_start, 20891da177e4SLinus Torvalds .next = ipmr_vif_seq_next, 20901da177e4SLinus Torvalds .stop = ipmr_vif_seq_stop, 20911da177e4SLinus Torvalds .show = ipmr_vif_seq_show, 20921da177e4SLinus Torvalds }; 20931da177e4SLinus Torvalds 20941da177e4SLinus Torvalds static int ipmr_vif_open(struct inode *inode, struct file *file) 20951da177e4SLinus Torvalds { 2096f6bb4514SBenjamin Thery return seq_open_net(inode, file, &ipmr_vif_seq_ops, 2097cf7732e4SPavel Emelyanov sizeof(struct ipmr_vif_iter)); 20981da177e4SLinus Torvalds } 20991da177e4SLinus Torvalds 21009a32144eSArjan van de Ven static const struct file_operations ipmr_vif_fops = { 21011da177e4SLinus Torvalds .owner = THIS_MODULE, 21021da177e4SLinus Torvalds .open = ipmr_vif_open, 21031da177e4SLinus Torvalds .read = seq_read, 21041da177e4SLinus Torvalds .llseek = seq_lseek, 2105f6bb4514SBenjamin Thery .release = seq_release_net, 21061da177e4SLinus Torvalds }; 21071da177e4SLinus Torvalds 21081da177e4SLinus Torvalds struct ipmr_mfc_iter { 2109f6bb4514SBenjamin Thery struct seq_net_private p; 2110f0ad0860SPatrick McHardy struct mr_table *mrt; 2111862465f2SPatrick McHardy struct list_head *cache; 21121da177e4SLinus Torvalds int ct; 21131da177e4SLinus Torvalds }; 21141da177e4SLinus Torvalds 21151da177e4SLinus Torvalds 2116f6bb4514SBenjamin Thery static struct mfc_cache *ipmr_mfc_seq_idx(struct net *net, 2117f6bb4514SBenjamin Thery struct ipmr_mfc_iter *it, loff_t pos) 21181da177e4SLinus Torvalds { 2119f0ad0860SPatrick McHardy struct mr_table *mrt = it->mrt; 21201da177e4SLinus Torvalds struct mfc_cache *mfc; 21211da177e4SLinus Torvalds 21221da177e4SLinus Torvalds read_lock(&mrt_lock); 2123862465f2SPatrick McHardy for (it->ct = 0; it->ct < MFC_LINES; it->ct++) { 21240c12295aSPatrick McHardy it->cache = &mrt->mfc_cache_array[it->ct]; 2125862465f2SPatrick McHardy list_for_each_entry(mfc, it->cache, list) 21261da177e4SLinus Torvalds if (pos-- == 0) 21271da177e4SLinus Torvalds return mfc; 2128862465f2SPatrick McHardy } 21291da177e4SLinus Torvalds read_unlock(&mrt_lock); 21301da177e4SLinus Torvalds 21311da177e4SLinus Torvalds spin_lock_bh(&mfc_unres_lock); 21320c12295aSPatrick McHardy it->cache = &mrt->mfc_unres_queue; 2133862465f2SPatrick McHardy list_for_each_entry(mfc, it->cache, list) 2134e258beb2SPatrick McHardy if (pos-- == 0) 21351da177e4SLinus Torvalds return mfc; 21361da177e4SLinus Torvalds spin_unlock_bh(&mfc_unres_lock); 21371da177e4SLinus Torvalds 21381da177e4SLinus Torvalds it->cache = NULL; 21391da177e4SLinus Torvalds return NULL; 21401da177e4SLinus Torvalds } 21411da177e4SLinus Torvalds 21421da177e4SLinus Torvalds 21431da177e4SLinus Torvalds static void *ipmr_mfc_seq_start(struct seq_file *seq, loff_t *pos) 21441da177e4SLinus Torvalds { 21451da177e4SLinus Torvalds struct ipmr_mfc_iter *it = seq->private; 2146f6bb4514SBenjamin Thery struct net *net = seq_file_net(seq); 2147f0ad0860SPatrick McHardy struct mr_table *mrt; 2148f6bb4514SBenjamin Thery 2149f0ad0860SPatrick McHardy mrt = ipmr_get_table(net, RT_TABLE_DEFAULT); 2150f0ad0860SPatrick McHardy if (mrt == NULL) 2151f0ad0860SPatrick McHardy return ERR_PTR(-ENOENT); 2152f0ad0860SPatrick McHardy 2153f0ad0860SPatrick McHardy it->mrt = mrt; 21541da177e4SLinus Torvalds it->cache = NULL; 21551da177e4SLinus Torvalds it->ct = 0; 2156f6bb4514SBenjamin Thery return *pos ? ipmr_mfc_seq_idx(net, seq->private, *pos - 1) 21571da177e4SLinus Torvalds : SEQ_START_TOKEN; 21581da177e4SLinus Torvalds } 21591da177e4SLinus Torvalds 21601da177e4SLinus Torvalds static void *ipmr_mfc_seq_next(struct seq_file *seq, void *v, loff_t *pos) 21611da177e4SLinus Torvalds { 21621da177e4SLinus Torvalds struct mfc_cache *mfc = v; 21631da177e4SLinus Torvalds struct ipmr_mfc_iter *it = seq->private; 2164f6bb4514SBenjamin Thery struct net *net = seq_file_net(seq); 2165f0ad0860SPatrick McHardy struct mr_table *mrt = it->mrt; 21661da177e4SLinus Torvalds 21671da177e4SLinus Torvalds ++*pos; 21681da177e4SLinus Torvalds 21691da177e4SLinus Torvalds if (v == SEQ_START_TOKEN) 2170f6bb4514SBenjamin Thery return ipmr_mfc_seq_idx(net, seq->private, 0); 21711da177e4SLinus Torvalds 2172862465f2SPatrick McHardy if (mfc->list.next != it->cache) 2173862465f2SPatrick McHardy return list_entry(mfc->list.next, struct mfc_cache, list); 21741da177e4SLinus Torvalds 21750c12295aSPatrick McHardy if (it->cache == &mrt->mfc_unres_queue) 21761da177e4SLinus Torvalds goto end_of_list; 21771da177e4SLinus Torvalds 21780c12295aSPatrick McHardy BUG_ON(it->cache != &mrt->mfc_cache_array[it->ct]); 21791da177e4SLinus Torvalds 21801da177e4SLinus Torvalds while (++it->ct < MFC_LINES) { 21810c12295aSPatrick McHardy it->cache = &mrt->mfc_cache_array[it->ct]; 2182862465f2SPatrick McHardy if (list_empty(it->cache)) 2183862465f2SPatrick McHardy continue; 2184862465f2SPatrick McHardy return list_first_entry(it->cache, struct mfc_cache, list); 21851da177e4SLinus Torvalds } 21861da177e4SLinus Torvalds 21871da177e4SLinus Torvalds /* exhausted cache_array, show unresolved */ 21881da177e4SLinus Torvalds read_unlock(&mrt_lock); 21890c12295aSPatrick McHardy it->cache = &mrt->mfc_unres_queue; 21901da177e4SLinus Torvalds it->ct = 0; 21911da177e4SLinus Torvalds 21921da177e4SLinus Torvalds spin_lock_bh(&mfc_unres_lock); 2193862465f2SPatrick McHardy if (!list_empty(it->cache)) 2194862465f2SPatrick McHardy return list_first_entry(it->cache, struct mfc_cache, list); 21951da177e4SLinus Torvalds 21961da177e4SLinus Torvalds end_of_list: 21971da177e4SLinus Torvalds spin_unlock_bh(&mfc_unres_lock); 21981da177e4SLinus Torvalds it->cache = NULL; 21991da177e4SLinus Torvalds 22001da177e4SLinus Torvalds return NULL; 22011da177e4SLinus Torvalds } 22021da177e4SLinus Torvalds 22031da177e4SLinus Torvalds static void ipmr_mfc_seq_stop(struct seq_file *seq, void *v) 22041da177e4SLinus Torvalds { 22051da177e4SLinus Torvalds struct ipmr_mfc_iter *it = seq->private; 2206f0ad0860SPatrick McHardy struct mr_table *mrt = it->mrt; 22071da177e4SLinus Torvalds 22080c12295aSPatrick McHardy if (it->cache == &mrt->mfc_unres_queue) 22091da177e4SLinus Torvalds spin_unlock_bh(&mfc_unres_lock); 22100c12295aSPatrick McHardy else if (it->cache == &mrt->mfc_cache_array[it->ct]) 22111da177e4SLinus Torvalds read_unlock(&mrt_lock); 22121da177e4SLinus Torvalds } 22131da177e4SLinus Torvalds 22141da177e4SLinus Torvalds static int ipmr_mfc_seq_show(struct seq_file *seq, void *v) 22151da177e4SLinus Torvalds { 22161da177e4SLinus Torvalds int n; 22171da177e4SLinus Torvalds 22181da177e4SLinus Torvalds if (v == SEQ_START_TOKEN) { 22191da177e4SLinus Torvalds seq_puts(seq, 22201da177e4SLinus Torvalds "Group Origin Iif Pkts Bytes Wrong Oifs\n"); 22211da177e4SLinus Torvalds } else { 22221da177e4SLinus Torvalds const struct mfc_cache *mfc = v; 22231da177e4SLinus Torvalds const struct ipmr_mfc_iter *it = seq->private; 2224f0ad0860SPatrick McHardy const struct mr_table *mrt = it->mrt; 22251da177e4SLinus Torvalds 2226999890b2SBenjamin Thery seq_printf(seq, "%08lX %08lX %-3hd", 22271da177e4SLinus Torvalds (unsigned long) mfc->mfc_mcastgrp, 22281da177e4SLinus Torvalds (unsigned long) mfc->mfc_origin, 22291ea472e2SBenjamin Thery mfc->mfc_parent); 22301ea472e2SBenjamin Thery 22310c12295aSPatrick McHardy if (it->cache != &mrt->mfc_unres_queue) { 22321ea472e2SBenjamin Thery seq_printf(seq, " %8lu %8lu %8lu", 22331da177e4SLinus Torvalds mfc->mfc_un.res.pkt, 22341da177e4SLinus Torvalds mfc->mfc_un.res.bytes, 22351da177e4SLinus Torvalds mfc->mfc_un.res.wrong_if); 22361da177e4SLinus Torvalds for (n = mfc->mfc_un.res.minvif; 22371da177e4SLinus Torvalds n < mfc->mfc_un.res.maxvif; n++ ) { 22380c12295aSPatrick McHardy if (VIF_EXISTS(mrt, n) && 2239cf958ae3SBenjamin Thery mfc->mfc_un.res.ttls[n] < 255) 22401da177e4SLinus Torvalds seq_printf(seq, 22411da177e4SLinus Torvalds " %2d:%-3d", 22421da177e4SLinus Torvalds n, mfc->mfc_un.res.ttls[n]); 22431da177e4SLinus Torvalds } 22441ea472e2SBenjamin Thery } else { 22451ea472e2SBenjamin Thery /* unresolved mfc_caches don't contain 22461ea472e2SBenjamin Thery * pkt, bytes and wrong_if values 22471ea472e2SBenjamin Thery */ 22481ea472e2SBenjamin Thery seq_printf(seq, " %8lu %8lu %8lu", 0ul, 0ul, 0ul); 22491da177e4SLinus Torvalds } 22501da177e4SLinus Torvalds seq_putc(seq, '\n'); 22511da177e4SLinus Torvalds } 22521da177e4SLinus Torvalds return 0; 22531da177e4SLinus Torvalds } 22541da177e4SLinus Torvalds 2255f690808eSStephen Hemminger static const struct seq_operations ipmr_mfc_seq_ops = { 22561da177e4SLinus Torvalds .start = ipmr_mfc_seq_start, 22571da177e4SLinus Torvalds .next = ipmr_mfc_seq_next, 22581da177e4SLinus Torvalds .stop = ipmr_mfc_seq_stop, 22591da177e4SLinus Torvalds .show = ipmr_mfc_seq_show, 22601da177e4SLinus Torvalds }; 22611da177e4SLinus Torvalds 22621da177e4SLinus Torvalds static int ipmr_mfc_open(struct inode *inode, struct file *file) 22631da177e4SLinus Torvalds { 2264f6bb4514SBenjamin Thery return seq_open_net(inode, file, &ipmr_mfc_seq_ops, 2265cf7732e4SPavel Emelyanov sizeof(struct ipmr_mfc_iter)); 22661da177e4SLinus Torvalds } 22671da177e4SLinus Torvalds 22689a32144eSArjan van de Ven static const struct file_operations ipmr_mfc_fops = { 22691da177e4SLinus Torvalds .owner = THIS_MODULE, 22701da177e4SLinus Torvalds .open = ipmr_mfc_open, 22711da177e4SLinus Torvalds .read = seq_read, 22721da177e4SLinus Torvalds .llseek = seq_lseek, 2273f6bb4514SBenjamin Thery .release = seq_release_net, 22741da177e4SLinus Torvalds }; 22751da177e4SLinus Torvalds #endif 22761da177e4SLinus Torvalds 22771da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM_V2 227832613090SAlexey Dobriyan static const struct net_protocol pim_protocol = { 22791da177e4SLinus Torvalds .handler = pim_rcv, 2280403dbb97STom Goff .netns_ok = 1, 22811da177e4SLinus Torvalds }; 22821da177e4SLinus Torvalds #endif 22831da177e4SLinus Torvalds 22841da177e4SLinus Torvalds 22851da177e4SLinus Torvalds /* 22861da177e4SLinus Torvalds * Setup for IP multicast routing 22871da177e4SLinus Torvalds */ 2288cf958ae3SBenjamin Thery static int __net_init ipmr_net_init(struct net *net) 2289cf958ae3SBenjamin Thery { 2290f0ad0860SPatrick McHardy int err; 2291cf958ae3SBenjamin Thery 2292f0ad0860SPatrick McHardy err = ipmr_rules_init(net); 2293f0ad0860SPatrick McHardy if (err < 0) 2294cf958ae3SBenjamin Thery goto fail; 2295f6bb4514SBenjamin Thery 2296f6bb4514SBenjamin Thery #ifdef CONFIG_PROC_FS 2297f6bb4514SBenjamin Thery err = -ENOMEM; 2298f6bb4514SBenjamin Thery if (!proc_net_fops_create(net, "ip_mr_vif", 0, &ipmr_vif_fops)) 2299f6bb4514SBenjamin Thery goto proc_vif_fail; 2300f6bb4514SBenjamin Thery if (!proc_net_fops_create(net, "ip_mr_cache", 0, &ipmr_mfc_fops)) 2301f6bb4514SBenjamin Thery goto proc_cache_fail; 2302f6bb4514SBenjamin Thery #endif 23032bb8b26cSBenjamin Thery return 0; 23042bb8b26cSBenjamin Thery 2305f6bb4514SBenjamin Thery #ifdef CONFIG_PROC_FS 2306f6bb4514SBenjamin Thery proc_cache_fail: 2307f6bb4514SBenjamin Thery proc_net_remove(net, "ip_mr_vif"); 2308f6bb4514SBenjamin Thery proc_vif_fail: 2309f0ad0860SPatrick McHardy ipmr_rules_exit(net); 2310f6bb4514SBenjamin Thery #endif 2311cf958ae3SBenjamin Thery fail: 2312cf958ae3SBenjamin Thery return err; 2313cf958ae3SBenjamin Thery } 2314cf958ae3SBenjamin Thery 2315cf958ae3SBenjamin Thery static void __net_exit ipmr_net_exit(struct net *net) 2316cf958ae3SBenjamin Thery { 2317f6bb4514SBenjamin Thery #ifdef CONFIG_PROC_FS 2318f6bb4514SBenjamin Thery proc_net_remove(net, "ip_mr_cache"); 2319f6bb4514SBenjamin Thery proc_net_remove(net, "ip_mr_vif"); 2320f6bb4514SBenjamin Thery #endif 2321f0ad0860SPatrick McHardy ipmr_rules_exit(net); 2322cf958ae3SBenjamin Thery } 2323cf958ae3SBenjamin Thery 2324cf958ae3SBenjamin Thery static struct pernet_operations ipmr_net_ops = { 2325cf958ae3SBenjamin Thery .init = ipmr_net_init, 2326cf958ae3SBenjamin Thery .exit = ipmr_net_exit, 2327cf958ae3SBenjamin Thery }; 23281da177e4SLinus Torvalds 232903d2f897SWang Chen int __init ip_mr_init(void) 23301da177e4SLinus Torvalds { 233103d2f897SWang Chen int err; 233203d2f897SWang Chen 23331da177e4SLinus Torvalds mrt_cachep = kmem_cache_create("ip_mrt_cache", 23341da177e4SLinus Torvalds sizeof(struct mfc_cache), 2335e5d679f3SAlexey Dobriyan 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, 233620c2df83SPaul Mundt NULL); 233703d2f897SWang Chen if (!mrt_cachep) 233803d2f897SWang Chen return -ENOMEM; 233903d2f897SWang Chen 2340cf958ae3SBenjamin Thery err = register_pernet_subsys(&ipmr_net_ops); 2341cf958ae3SBenjamin Thery if (err) 2342cf958ae3SBenjamin Thery goto reg_pernet_fail; 2343cf958ae3SBenjamin Thery 234403d2f897SWang Chen err = register_netdevice_notifier(&ip_mr_notifier); 234503d2f897SWang Chen if (err) 234603d2f897SWang Chen goto reg_notif_fail; 2347403dbb97STom Goff #ifdef CONFIG_IP_PIMSM_V2 2348403dbb97STom Goff if (inet_add_protocol(&pim_protocol, IPPROTO_PIM) < 0) { 2349403dbb97STom Goff printk(KERN_ERR "ip_mr_init: can't add PIM protocol\n"); 2350403dbb97STom Goff err = -EAGAIN; 2351403dbb97STom Goff goto add_proto_fail; 2352403dbb97STom Goff } 2353403dbb97STom Goff #endif 235403d2f897SWang Chen return 0; 2355f6bb4514SBenjamin Thery 2356403dbb97STom Goff #ifdef CONFIG_IP_PIMSM_V2 2357403dbb97STom Goff add_proto_fail: 2358403dbb97STom Goff unregister_netdevice_notifier(&ip_mr_notifier); 2359403dbb97STom Goff #endif 2360c3e38896SBenjamin Thery reg_notif_fail: 2361cf958ae3SBenjamin Thery unregister_pernet_subsys(&ipmr_net_ops); 2362cf958ae3SBenjamin Thery reg_pernet_fail: 2363c3e38896SBenjamin Thery kmem_cache_destroy(mrt_cachep); 236403d2f897SWang Chen return err; 23651da177e4SLinus Torvalds } 2366