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 25f77f13e2SGilles Espinasse * Relax this requirement 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; 748de53dfbSPatrick McHardy #ifdef CONFIG_NET_NS 758de53dfbSPatrick McHardy struct net *net; 768de53dfbSPatrick McHardy #endif 77f0ad0860SPatrick McHardy u32 id; 780c12295aSPatrick McHardy struct sock *mroute_sk; 790c12295aSPatrick McHardy struct timer_list ipmr_expire_timer; 800c12295aSPatrick McHardy struct list_head mfc_unres_queue; 810c12295aSPatrick McHardy struct list_head mfc_cache_array[MFC_LINES]; 820c12295aSPatrick McHardy struct vif_device vif_table[MAXVIFS]; 830c12295aSPatrick McHardy int maxvif; 840c12295aSPatrick McHardy atomic_t cache_resolve_queue_len; 850c12295aSPatrick McHardy int mroute_do_assert; 860c12295aSPatrick McHardy int mroute_do_pim; 870c12295aSPatrick McHardy #if defined(CONFIG_IP_PIMSM_V1) || defined(CONFIG_IP_PIMSM_V2) 880c12295aSPatrick McHardy int mroute_reg_vif_num; 890c12295aSPatrick McHardy #endif 900c12295aSPatrick McHardy }; 910c12295aSPatrick McHardy 92f0ad0860SPatrick McHardy struct ipmr_rule { 93f0ad0860SPatrick McHardy struct fib_rule common; 94f0ad0860SPatrick McHardy }; 95f0ad0860SPatrick McHardy 96f0ad0860SPatrick McHardy struct ipmr_result { 97f0ad0860SPatrick McHardy struct mr_table *mrt; 98f0ad0860SPatrick McHardy }; 99f0ad0860SPatrick McHardy 1001da177e4SLinus Torvalds /* Big lock, protecting vif table, mrt cache and mroute socket state. 1011da177e4SLinus Torvalds Note that the changes are semaphored via rtnl_lock. 1021da177e4SLinus Torvalds */ 1031da177e4SLinus Torvalds 1041da177e4SLinus Torvalds static DEFINE_RWLOCK(mrt_lock); 1051da177e4SLinus Torvalds 1061da177e4SLinus Torvalds /* 1071da177e4SLinus Torvalds * Multicast router control variables 1081da177e4SLinus Torvalds */ 1091da177e4SLinus Torvalds 1100c12295aSPatrick McHardy #define VIF_EXISTS(_mrt, _idx) ((_mrt)->vif_table[_idx].dev != NULL) 1111da177e4SLinus Torvalds 1121da177e4SLinus Torvalds /* Special spinlock for queue of unresolved entries */ 1131da177e4SLinus Torvalds static DEFINE_SPINLOCK(mfc_unres_lock); 1141da177e4SLinus Torvalds 1151da177e4SLinus Torvalds /* We return to original Alan's scheme. Hash table of resolved 1161da177e4SLinus Torvalds entries is changed only in process context and protected 1171da177e4SLinus Torvalds with weak lock mrt_lock. Queue of unresolved entries is protected 1181da177e4SLinus Torvalds with strong spinlock mfc_unres_lock. 1191da177e4SLinus Torvalds 1201da177e4SLinus Torvalds In this case data path is free of exclusive locks at all. 1211da177e4SLinus Torvalds */ 1221da177e4SLinus Torvalds 123e18b890bSChristoph Lameter static struct kmem_cache *mrt_cachep __read_mostly; 1241da177e4SLinus Torvalds 125f0ad0860SPatrick McHardy static struct mr_table *ipmr_new_table(struct net *net, u32 id); 1260c12295aSPatrick McHardy static int ip_mr_forward(struct net *net, struct mr_table *mrt, 1270c12295aSPatrick McHardy struct sk_buff *skb, struct mfc_cache *cache, 1280c12295aSPatrick McHardy int local); 1290c12295aSPatrick McHardy static int ipmr_cache_report(struct mr_table *mrt, 1304feb88e5SBenjamin Thery struct sk_buff *pkt, vifi_t vifi, int assert); 131cb6a4e46SPatrick McHardy static int __ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb, 132d658f8a0SPatrick McHardy struct mfc_cache *c, struct rtmsg *rtm); 133f0ad0860SPatrick McHardy static void ipmr_expire_process(unsigned long arg); 1341da177e4SLinus Torvalds 135f0ad0860SPatrick McHardy #ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES 136f0ad0860SPatrick McHardy #define ipmr_for_each_table(mrt, net) \ 137f0ad0860SPatrick McHardy list_for_each_entry_rcu(mrt, &net->ipv4.mr_tables, list) 138f0ad0860SPatrick McHardy 139f0ad0860SPatrick McHardy static struct mr_table *ipmr_get_table(struct net *net, u32 id) 140f0ad0860SPatrick McHardy { 141f0ad0860SPatrick McHardy struct mr_table *mrt; 142f0ad0860SPatrick McHardy 143f0ad0860SPatrick McHardy ipmr_for_each_table(mrt, net) { 144f0ad0860SPatrick McHardy if (mrt->id == id) 145f0ad0860SPatrick McHardy return mrt; 146f0ad0860SPatrick McHardy } 147f0ad0860SPatrick McHardy return NULL; 148f0ad0860SPatrick McHardy } 149f0ad0860SPatrick McHardy 150f0ad0860SPatrick McHardy static int ipmr_fib_lookup(struct net *net, struct flowi *flp, 151f0ad0860SPatrick McHardy struct mr_table **mrt) 152f0ad0860SPatrick McHardy { 153f0ad0860SPatrick McHardy struct ipmr_result res; 154f0ad0860SPatrick McHardy struct fib_lookup_arg arg = { .result = &res, }; 155f0ad0860SPatrick McHardy int err; 156f0ad0860SPatrick McHardy 157f0ad0860SPatrick McHardy err = fib_rules_lookup(net->ipv4.mr_rules_ops, flp, 0, &arg); 158f0ad0860SPatrick McHardy if (err < 0) 159f0ad0860SPatrick McHardy return err; 160f0ad0860SPatrick McHardy *mrt = res.mrt; 161f0ad0860SPatrick McHardy return 0; 162f0ad0860SPatrick McHardy } 163f0ad0860SPatrick McHardy 164f0ad0860SPatrick McHardy static int ipmr_rule_action(struct fib_rule *rule, struct flowi *flp, 165f0ad0860SPatrick McHardy int flags, struct fib_lookup_arg *arg) 166f0ad0860SPatrick McHardy { 167f0ad0860SPatrick McHardy struct ipmr_result *res = arg->result; 168f0ad0860SPatrick McHardy struct mr_table *mrt; 169f0ad0860SPatrick McHardy 170f0ad0860SPatrick McHardy switch (rule->action) { 171f0ad0860SPatrick McHardy case FR_ACT_TO_TBL: 172f0ad0860SPatrick McHardy break; 173f0ad0860SPatrick McHardy case FR_ACT_UNREACHABLE: 174f0ad0860SPatrick McHardy return -ENETUNREACH; 175f0ad0860SPatrick McHardy case FR_ACT_PROHIBIT: 176f0ad0860SPatrick McHardy return -EACCES; 177f0ad0860SPatrick McHardy case FR_ACT_BLACKHOLE: 178f0ad0860SPatrick McHardy default: 179f0ad0860SPatrick McHardy return -EINVAL; 180f0ad0860SPatrick McHardy } 181f0ad0860SPatrick McHardy 182f0ad0860SPatrick McHardy mrt = ipmr_get_table(rule->fr_net, rule->table); 183f0ad0860SPatrick McHardy if (mrt == NULL) 184f0ad0860SPatrick McHardy return -EAGAIN; 185f0ad0860SPatrick McHardy res->mrt = mrt; 186f0ad0860SPatrick McHardy return 0; 187f0ad0860SPatrick McHardy } 188f0ad0860SPatrick McHardy 189f0ad0860SPatrick McHardy static int ipmr_rule_match(struct fib_rule *rule, struct flowi *fl, int flags) 190f0ad0860SPatrick McHardy { 191f0ad0860SPatrick McHardy return 1; 192f0ad0860SPatrick McHardy } 193f0ad0860SPatrick McHardy 194f0ad0860SPatrick McHardy static const struct nla_policy ipmr_rule_policy[FRA_MAX + 1] = { 195f0ad0860SPatrick McHardy FRA_GENERIC_POLICY, 196f0ad0860SPatrick McHardy }; 197f0ad0860SPatrick McHardy 198f0ad0860SPatrick McHardy static int ipmr_rule_configure(struct fib_rule *rule, struct sk_buff *skb, 199f0ad0860SPatrick McHardy struct fib_rule_hdr *frh, struct nlattr **tb) 200f0ad0860SPatrick McHardy { 201f0ad0860SPatrick McHardy return 0; 202f0ad0860SPatrick McHardy } 203f0ad0860SPatrick McHardy 204f0ad0860SPatrick McHardy static int ipmr_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh, 205f0ad0860SPatrick McHardy struct nlattr **tb) 206f0ad0860SPatrick McHardy { 207f0ad0860SPatrick McHardy return 1; 208f0ad0860SPatrick McHardy } 209f0ad0860SPatrick McHardy 210f0ad0860SPatrick McHardy static int ipmr_rule_fill(struct fib_rule *rule, struct sk_buff *skb, 211f0ad0860SPatrick McHardy struct fib_rule_hdr *frh) 212f0ad0860SPatrick McHardy { 213f0ad0860SPatrick McHardy frh->dst_len = 0; 214f0ad0860SPatrick McHardy frh->src_len = 0; 215f0ad0860SPatrick McHardy frh->tos = 0; 216f0ad0860SPatrick McHardy return 0; 217f0ad0860SPatrick McHardy } 218f0ad0860SPatrick McHardy 2193d0c9c4eSPatrick McHardy static const struct fib_rules_ops __net_initdata ipmr_rules_ops_template = { 22025239ceeSPatrick McHardy .family = RTNL_FAMILY_IPMR, 221f0ad0860SPatrick McHardy .rule_size = sizeof(struct ipmr_rule), 222f0ad0860SPatrick McHardy .addr_size = sizeof(u32), 223f0ad0860SPatrick McHardy .action = ipmr_rule_action, 224f0ad0860SPatrick McHardy .match = ipmr_rule_match, 225f0ad0860SPatrick McHardy .configure = ipmr_rule_configure, 226f0ad0860SPatrick McHardy .compare = ipmr_rule_compare, 227f0ad0860SPatrick McHardy .default_pref = fib_default_rule_pref, 228f0ad0860SPatrick McHardy .fill = ipmr_rule_fill, 229f0ad0860SPatrick McHardy .nlgroup = RTNLGRP_IPV4_RULE, 230f0ad0860SPatrick McHardy .policy = ipmr_rule_policy, 231f0ad0860SPatrick McHardy .owner = THIS_MODULE, 232f0ad0860SPatrick McHardy }; 233f0ad0860SPatrick McHardy 234f0ad0860SPatrick McHardy static int __net_init ipmr_rules_init(struct net *net) 235f0ad0860SPatrick McHardy { 236f0ad0860SPatrick McHardy struct fib_rules_ops *ops; 237f0ad0860SPatrick McHardy struct mr_table *mrt; 238f0ad0860SPatrick McHardy int err; 239f0ad0860SPatrick McHardy 240f0ad0860SPatrick McHardy ops = fib_rules_register(&ipmr_rules_ops_template, net); 241f0ad0860SPatrick McHardy if (IS_ERR(ops)) 242f0ad0860SPatrick McHardy return PTR_ERR(ops); 243f0ad0860SPatrick McHardy 244f0ad0860SPatrick McHardy INIT_LIST_HEAD(&net->ipv4.mr_tables); 245f0ad0860SPatrick McHardy 246f0ad0860SPatrick McHardy mrt = ipmr_new_table(net, RT_TABLE_DEFAULT); 247f0ad0860SPatrick McHardy if (mrt == NULL) { 248f0ad0860SPatrick McHardy err = -ENOMEM; 249f0ad0860SPatrick McHardy goto err1; 250f0ad0860SPatrick McHardy } 251f0ad0860SPatrick McHardy 252f0ad0860SPatrick McHardy err = fib_default_rule_add(ops, 0x7fff, RT_TABLE_DEFAULT, 0); 253f0ad0860SPatrick McHardy if (err < 0) 254f0ad0860SPatrick McHardy goto err2; 255f0ad0860SPatrick McHardy 256f0ad0860SPatrick McHardy net->ipv4.mr_rules_ops = ops; 257f0ad0860SPatrick McHardy return 0; 258f0ad0860SPatrick McHardy 259f0ad0860SPatrick McHardy err2: 260f0ad0860SPatrick McHardy kfree(mrt); 261f0ad0860SPatrick McHardy err1: 262f0ad0860SPatrick McHardy fib_rules_unregister(ops); 263f0ad0860SPatrick McHardy return err; 264f0ad0860SPatrick McHardy } 265f0ad0860SPatrick McHardy 266f0ad0860SPatrick McHardy static void __net_exit ipmr_rules_exit(struct net *net) 267f0ad0860SPatrick McHardy { 268f0ad0860SPatrick McHardy struct mr_table *mrt, *next; 269f0ad0860SPatrick McHardy 270f0ad0860SPatrick McHardy list_for_each_entry_safe(mrt, next, &net->ipv4.mr_tables, list) 271f0ad0860SPatrick McHardy kfree(mrt); 272f0ad0860SPatrick McHardy fib_rules_unregister(net->ipv4.mr_rules_ops); 273f0ad0860SPatrick McHardy } 274f0ad0860SPatrick McHardy #else 275f0ad0860SPatrick McHardy #define ipmr_for_each_table(mrt, net) \ 276f0ad0860SPatrick McHardy for (mrt = net->ipv4.mrt; mrt; mrt = NULL) 277f0ad0860SPatrick McHardy 278f0ad0860SPatrick McHardy static struct mr_table *ipmr_get_table(struct net *net, u32 id) 279f0ad0860SPatrick McHardy { 280f0ad0860SPatrick McHardy return net->ipv4.mrt; 281f0ad0860SPatrick McHardy } 282f0ad0860SPatrick McHardy 283f0ad0860SPatrick McHardy static int ipmr_fib_lookup(struct net *net, struct flowi *flp, 284f0ad0860SPatrick McHardy struct mr_table **mrt) 285f0ad0860SPatrick McHardy { 286f0ad0860SPatrick McHardy *mrt = net->ipv4.mrt; 287f0ad0860SPatrick McHardy return 0; 288f0ad0860SPatrick McHardy } 289f0ad0860SPatrick McHardy 290f0ad0860SPatrick McHardy static int __net_init ipmr_rules_init(struct net *net) 291f0ad0860SPatrick McHardy { 292f0ad0860SPatrick McHardy net->ipv4.mrt = ipmr_new_table(net, RT_TABLE_DEFAULT); 293f0ad0860SPatrick McHardy return net->ipv4.mrt ? 0 : -ENOMEM; 294f0ad0860SPatrick McHardy } 295f0ad0860SPatrick McHardy 296f0ad0860SPatrick McHardy static void __net_exit ipmr_rules_exit(struct net *net) 297f0ad0860SPatrick McHardy { 298f0ad0860SPatrick McHardy kfree(net->ipv4.mrt); 299f0ad0860SPatrick McHardy } 300f0ad0860SPatrick McHardy #endif 301f0ad0860SPatrick McHardy 302f0ad0860SPatrick McHardy static struct mr_table *ipmr_new_table(struct net *net, u32 id) 303f0ad0860SPatrick McHardy { 304f0ad0860SPatrick McHardy struct mr_table *mrt; 305f0ad0860SPatrick McHardy unsigned int i; 306f0ad0860SPatrick McHardy 307f0ad0860SPatrick McHardy mrt = ipmr_get_table(net, id); 308f0ad0860SPatrick McHardy if (mrt != NULL) 309f0ad0860SPatrick McHardy return mrt; 310f0ad0860SPatrick McHardy 311f0ad0860SPatrick McHardy mrt = kzalloc(sizeof(*mrt), GFP_KERNEL); 312f0ad0860SPatrick McHardy if (mrt == NULL) 313f0ad0860SPatrick McHardy return NULL; 3148de53dfbSPatrick McHardy write_pnet(&mrt->net, net); 315f0ad0860SPatrick McHardy mrt->id = id; 316f0ad0860SPatrick McHardy 317f0ad0860SPatrick McHardy /* Forwarding cache */ 318f0ad0860SPatrick McHardy for (i = 0; i < MFC_LINES; i++) 319f0ad0860SPatrick McHardy INIT_LIST_HEAD(&mrt->mfc_cache_array[i]); 320f0ad0860SPatrick McHardy 321f0ad0860SPatrick McHardy INIT_LIST_HEAD(&mrt->mfc_unres_queue); 322f0ad0860SPatrick McHardy 323f0ad0860SPatrick McHardy setup_timer(&mrt->ipmr_expire_timer, ipmr_expire_process, 324f0ad0860SPatrick McHardy (unsigned long)mrt); 325f0ad0860SPatrick McHardy 326f0ad0860SPatrick McHardy #ifdef CONFIG_IP_PIMSM 327f0ad0860SPatrick McHardy mrt->mroute_reg_vif_num = -1; 328f0ad0860SPatrick McHardy #endif 329f0ad0860SPatrick McHardy #ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES 330f0ad0860SPatrick McHardy list_add_tail_rcu(&mrt->list, &net->ipv4.mr_tables); 331f0ad0860SPatrick McHardy #endif 332f0ad0860SPatrick McHardy return mrt; 333f0ad0860SPatrick McHardy } 3341da177e4SLinus Torvalds 3351da177e4SLinus Torvalds /* Service routines creating virtual interfaces: DVMRP tunnels and PIMREG */ 3361da177e4SLinus Torvalds 337d607032dSWang Chen static void ipmr_del_tunnel(struct net_device *dev, struct vifctl *v) 338d607032dSWang Chen { 3394feb88e5SBenjamin Thery struct net *net = dev_net(dev); 3404feb88e5SBenjamin Thery 341d607032dSWang Chen dev_close(dev); 342d607032dSWang Chen 3434feb88e5SBenjamin Thery dev = __dev_get_by_name(net, "tunl0"); 344d607032dSWang Chen if (dev) { 3455bc3eb7eSStephen Hemminger const struct net_device_ops *ops = dev->netdev_ops; 346d607032dSWang Chen struct ifreq ifr; 347d607032dSWang Chen struct ip_tunnel_parm p; 348d607032dSWang Chen 349d607032dSWang Chen memset(&p, 0, sizeof(p)); 350d607032dSWang Chen p.iph.daddr = v->vifc_rmt_addr.s_addr; 351d607032dSWang Chen p.iph.saddr = v->vifc_lcl_addr.s_addr; 352d607032dSWang Chen p.iph.version = 4; 353d607032dSWang Chen p.iph.ihl = 5; 354d607032dSWang Chen p.iph.protocol = IPPROTO_IPIP; 355d607032dSWang Chen sprintf(p.name, "dvmrp%d", v->vifc_vifi); 356d607032dSWang Chen ifr.ifr_ifru.ifru_data = (__force void __user *)&p; 357d607032dSWang Chen 3585bc3eb7eSStephen Hemminger if (ops->ndo_do_ioctl) { 3595bc3eb7eSStephen Hemminger mm_segment_t oldfs = get_fs(); 3605bc3eb7eSStephen Hemminger 3615bc3eb7eSStephen Hemminger set_fs(KERNEL_DS); 3625bc3eb7eSStephen Hemminger ops->ndo_do_ioctl(dev, &ifr, SIOCDELTUNNEL); 363d607032dSWang Chen set_fs(oldfs); 364d607032dSWang Chen } 365d607032dSWang Chen } 3665bc3eb7eSStephen Hemminger } 367d607032dSWang Chen 3681da177e4SLinus Torvalds static 3694feb88e5SBenjamin Thery struct net_device *ipmr_new_tunnel(struct net *net, struct vifctl *v) 3701da177e4SLinus Torvalds { 3711da177e4SLinus Torvalds struct net_device *dev; 3721da177e4SLinus Torvalds 3734feb88e5SBenjamin Thery dev = __dev_get_by_name(net, "tunl0"); 3741da177e4SLinus Torvalds 3751da177e4SLinus Torvalds if (dev) { 3765bc3eb7eSStephen Hemminger const struct net_device_ops *ops = dev->netdev_ops; 3771da177e4SLinus Torvalds int err; 3781da177e4SLinus Torvalds struct ifreq ifr; 3791da177e4SLinus Torvalds struct ip_tunnel_parm p; 3801da177e4SLinus Torvalds struct in_device *in_dev; 3811da177e4SLinus Torvalds 3821da177e4SLinus Torvalds memset(&p, 0, sizeof(p)); 3831da177e4SLinus Torvalds p.iph.daddr = v->vifc_rmt_addr.s_addr; 3841da177e4SLinus Torvalds p.iph.saddr = v->vifc_lcl_addr.s_addr; 3851da177e4SLinus Torvalds p.iph.version = 4; 3861da177e4SLinus Torvalds p.iph.ihl = 5; 3871da177e4SLinus Torvalds p.iph.protocol = IPPROTO_IPIP; 3881da177e4SLinus Torvalds sprintf(p.name, "dvmrp%d", v->vifc_vifi); 389ba93ef74SStephen Hemminger ifr.ifr_ifru.ifru_data = (__force void __user *)&p; 3901da177e4SLinus Torvalds 3915bc3eb7eSStephen Hemminger if (ops->ndo_do_ioctl) { 3925bc3eb7eSStephen Hemminger mm_segment_t oldfs = get_fs(); 3935bc3eb7eSStephen Hemminger 3945bc3eb7eSStephen Hemminger set_fs(KERNEL_DS); 3955bc3eb7eSStephen Hemminger err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL); 3961da177e4SLinus Torvalds set_fs(oldfs); 3975bc3eb7eSStephen Hemminger } else 3985bc3eb7eSStephen Hemminger err = -EOPNOTSUPP; 3991da177e4SLinus Torvalds 4001da177e4SLinus Torvalds dev = NULL; 4011da177e4SLinus Torvalds 4024feb88e5SBenjamin Thery if (err == 0 && 4034feb88e5SBenjamin Thery (dev = __dev_get_by_name(net, p.name)) != NULL) { 4041da177e4SLinus Torvalds dev->flags |= IFF_MULTICAST; 4051da177e4SLinus Torvalds 406e5ed6399SHerbert Xu in_dev = __in_dev_get_rtnl(dev); 40771e27da9SHerbert Xu if (in_dev == NULL) 4081da177e4SLinus Torvalds goto failure; 40971e27da9SHerbert Xu 41071e27da9SHerbert Xu ipv4_devconf_setall(in_dev); 41171e27da9SHerbert Xu IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0; 4121da177e4SLinus Torvalds 4131da177e4SLinus Torvalds if (dev_open(dev)) 4141da177e4SLinus Torvalds goto failure; 4157dc00c82SWang Chen dev_hold(dev); 4161da177e4SLinus Torvalds } 4171da177e4SLinus Torvalds } 4181da177e4SLinus Torvalds return dev; 4191da177e4SLinus Torvalds 4201da177e4SLinus Torvalds failure: 4211da177e4SLinus Torvalds /* allow the register to be completed before unregistering. */ 4221da177e4SLinus Torvalds rtnl_unlock(); 4231da177e4SLinus Torvalds rtnl_lock(); 4241da177e4SLinus Torvalds 4251da177e4SLinus Torvalds unregister_netdevice(dev); 4261da177e4SLinus Torvalds return NULL; 4271da177e4SLinus Torvalds } 4281da177e4SLinus Torvalds 4291da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM 4301da177e4SLinus Torvalds 4316fef4c0cSStephen Hemminger static netdev_tx_t reg_vif_xmit(struct sk_buff *skb, struct net_device *dev) 4321da177e4SLinus Torvalds { 4334feb88e5SBenjamin Thery struct net *net = dev_net(dev); 434f0ad0860SPatrick McHardy struct mr_table *mrt; 435f0ad0860SPatrick McHardy struct flowi fl = { 436f0ad0860SPatrick McHardy .oif = dev->ifindex, 437f0ad0860SPatrick McHardy .iif = skb->skb_iif, 438f0ad0860SPatrick McHardy .mark = skb->mark, 439f0ad0860SPatrick McHardy }; 440f0ad0860SPatrick McHardy int err; 441f0ad0860SPatrick McHardy 442f0ad0860SPatrick McHardy err = ipmr_fib_lookup(net, &fl, &mrt); 443f0ad0860SPatrick McHardy if (err < 0) 444f0ad0860SPatrick McHardy return err; 4454feb88e5SBenjamin Thery 4461da177e4SLinus Torvalds read_lock(&mrt_lock); 447cf3677aeSPavel Emelyanov dev->stats.tx_bytes += skb->len; 448cf3677aeSPavel Emelyanov dev->stats.tx_packets++; 4490c12295aSPatrick McHardy ipmr_cache_report(mrt, skb, mrt->mroute_reg_vif_num, IGMPMSG_WHOLEPKT); 4501da177e4SLinus Torvalds read_unlock(&mrt_lock); 4511da177e4SLinus Torvalds kfree_skb(skb); 4526ed10654SPatrick McHardy return NETDEV_TX_OK; 4531da177e4SLinus Torvalds } 4541da177e4SLinus Torvalds 455007c3838SStephen Hemminger static const struct net_device_ops reg_vif_netdev_ops = { 456007c3838SStephen Hemminger .ndo_start_xmit = reg_vif_xmit, 457007c3838SStephen Hemminger }; 458007c3838SStephen Hemminger 4591da177e4SLinus Torvalds static void reg_vif_setup(struct net_device *dev) 4601da177e4SLinus Torvalds { 4611da177e4SLinus Torvalds dev->type = ARPHRD_PIMREG; 46246f25dffSKris Katterjohn dev->mtu = ETH_DATA_LEN - sizeof(struct iphdr) - 8; 4631da177e4SLinus Torvalds dev->flags = IFF_NOARP; 464007c3838SStephen Hemminger dev->netdev_ops = ®_vif_netdev_ops, 4651da177e4SLinus Torvalds dev->destructor = free_netdev; 466403dbb97STom Goff dev->features |= NETIF_F_NETNS_LOCAL; 4671da177e4SLinus Torvalds } 4681da177e4SLinus Torvalds 469f0ad0860SPatrick McHardy static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt) 4701da177e4SLinus Torvalds { 4711da177e4SLinus Torvalds struct net_device *dev; 4721da177e4SLinus Torvalds struct in_device *in_dev; 473f0ad0860SPatrick McHardy char name[IFNAMSIZ]; 4741da177e4SLinus Torvalds 475f0ad0860SPatrick McHardy if (mrt->id == RT_TABLE_DEFAULT) 476f0ad0860SPatrick McHardy sprintf(name, "pimreg"); 477f0ad0860SPatrick McHardy else 478f0ad0860SPatrick McHardy sprintf(name, "pimreg%u", mrt->id); 479f0ad0860SPatrick McHardy 480f0ad0860SPatrick McHardy dev = alloc_netdev(0, name, reg_vif_setup); 4811da177e4SLinus Torvalds 4821da177e4SLinus Torvalds if (dev == NULL) 4831da177e4SLinus Torvalds return NULL; 4841da177e4SLinus Torvalds 485403dbb97STom Goff dev_net_set(dev, net); 486403dbb97STom Goff 4871da177e4SLinus Torvalds if (register_netdevice(dev)) { 4881da177e4SLinus Torvalds free_netdev(dev); 4891da177e4SLinus Torvalds return NULL; 4901da177e4SLinus Torvalds } 4911da177e4SLinus Torvalds dev->iflink = 0; 4921da177e4SLinus Torvalds 49371e27da9SHerbert Xu rcu_read_lock(); 49471e27da9SHerbert Xu if ((in_dev = __in_dev_get_rcu(dev)) == NULL) { 49571e27da9SHerbert Xu rcu_read_unlock(); 4961da177e4SLinus Torvalds goto failure; 49771e27da9SHerbert Xu } 4981da177e4SLinus Torvalds 49971e27da9SHerbert Xu ipv4_devconf_setall(in_dev); 50071e27da9SHerbert Xu IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0; 50171e27da9SHerbert Xu rcu_read_unlock(); 5021da177e4SLinus Torvalds 5031da177e4SLinus Torvalds if (dev_open(dev)) 5041da177e4SLinus Torvalds goto failure; 5051da177e4SLinus Torvalds 5067dc00c82SWang Chen dev_hold(dev); 5077dc00c82SWang Chen 5081da177e4SLinus Torvalds return dev; 5091da177e4SLinus Torvalds 5101da177e4SLinus Torvalds failure: 5111da177e4SLinus Torvalds /* allow the register to be completed before unregistering. */ 5121da177e4SLinus Torvalds rtnl_unlock(); 5131da177e4SLinus Torvalds rtnl_lock(); 5141da177e4SLinus Torvalds 5151da177e4SLinus Torvalds unregister_netdevice(dev); 5161da177e4SLinus Torvalds return NULL; 5171da177e4SLinus Torvalds } 5181da177e4SLinus Torvalds #endif 5191da177e4SLinus Torvalds 5201da177e4SLinus Torvalds /* 5211da177e4SLinus Torvalds * Delete a VIF entry 5227dc00c82SWang Chen * @notify: Set to 1, if the caller is a notifier_call 5231da177e4SLinus Torvalds */ 5241da177e4SLinus Torvalds 5250c12295aSPatrick McHardy static int vif_delete(struct mr_table *mrt, int vifi, int notify, 526d17fa6faSEric Dumazet struct list_head *head) 5271da177e4SLinus Torvalds { 5281da177e4SLinus Torvalds struct vif_device *v; 5291da177e4SLinus Torvalds struct net_device *dev; 5301da177e4SLinus Torvalds struct in_device *in_dev; 5311da177e4SLinus Torvalds 5320c12295aSPatrick McHardy if (vifi < 0 || vifi >= mrt->maxvif) 5331da177e4SLinus Torvalds return -EADDRNOTAVAIL; 5341da177e4SLinus Torvalds 5350c12295aSPatrick McHardy v = &mrt->vif_table[vifi]; 5361da177e4SLinus Torvalds 5371da177e4SLinus Torvalds write_lock_bh(&mrt_lock); 5381da177e4SLinus Torvalds dev = v->dev; 5391da177e4SLinus Torvalds v->dev = NULL; 5401da177e4SLinus Torvalds 5411da177e4SLinus Torvalds if (!dev) { 5421da177e4SLinus Torvalds write_unlock_bh(&mrt_lock); 5431da177e4SLinus Torvalds return -EADDRNOTAVAIL; 5441da177e4SLinus Torvalds } 5451da177e4SLinus Torvalds 5461da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM 5470c12295aSPatrick McHardy if (vifi == mrt->mroute_reg_vif_num) 5480c12295aSPatrick McHardy mrt->mroute_reg_vif_num = -1; 5491da177e4SLinus Torvalds #endif 5501da177e4SLinus Torvalds 5510c12295aSPatrick McHardy if (vifi+1 == mrt->maxvif) { 5521da177e4SLinus Torvalds int tmp; 5531da177e4SLinus Torvalds for (tmp=vifi-1; tmp>=0; tmp--) { 5540c12295aSPatrick McHardy if (VIF_EXISTS(mrt, tmp)) 5551da177e4SLinus Torvalds break; 5561da177e4SLinus Torvalds } 5570c12295aSPatrick McHardy mrt->maxvif = tmp+1; 5581da177e4SLinus Torvalds } 5591da177e4SLinus Torvalds 5601da177e4SLinus Torvalds write_unlock_bh(&mrt_lock); 5611da177e4SLinus Torvalds 5621da177e4SLinus Torvalds dev_set_allmulti(dev, -1); 5631da177e4SLinus Torvalds 564e5ed6399SHerbert Xu if ((in_dev = __in_dev_get_rtnl(dev)) != NULL) { 56542f811b8SHerbert Xu IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)--; 5661da177e4SLinus Torvalds ip_rt_multicast_event(in_dev); 5671da177e4SLinus Torvalds } 5681da177e4SLinus Torvalds 5697dc00c82SWang Chen if (v->flags&(VIFF_TUNNEL|VIFF_REGISTER) && !notify) 570d17fa6faSEric Dumazet unregister_netdevice_queue(dev, head); 5711da177e4SLinus Torvalds 5721da177e4SLinus Torvalds dev_put(dev); 5731da177e4SLinus Torvalds return 0; 5741da177e4SLinus Torvalds } 5751da177e4SLinus Torvalds 5765c0a66f5SBenjamin Thery static inline void ipmr_cache_free(struct mfc_cache *c) 5775c0a66f5SBenjamin Thery { 5785c0a66f5SBenjamin Thery kmem_cache_free(mrt_cachep, c); 5795c0a66f5SBenjamin Thery } 5805c0a66f5SBenjamin Thery 5811da177e4SLinus Torvalds /* Destroy an unresolved cache entry, killing queued skbs 5821da177e4SLinus Torvalds and reporting error to netlink readers. 5831da177e4SLinus Torvalds */ 5841da177e4SLinus Torvalds 5850c12295aSPatrick McHardy static void ipmr_destroy_unres(struct mr_table *mrt, struct mfc_cache *c) 5861da177e4SLinus Torvalds { 5878de53dfbSPatrick McHardy struct net *net = read_pnet(&mrt->net); 5881da177e4SLinus Torvalds struct sk_buff *skb; 5899ef1d4c7SPatrick McHardy struct nlmsgerr *e; 5901da177e4SLinus Torvalds 5910c12295aSPatrick McHardy atomic_dec(&mrt->cache_resolve_queue_len); 5921da177e4SLinus Torvalds 5931da177e4SLinus Torvalds while ((skb = skb_dequeue(&c->mfc_un.unres.unresolved))) { 594eddc9ec5SArnaldo Carvalho de Melo if (ip_hdr(skb)->version == 0) { 5951da177e4SLinus Torvalds struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr)); 5961da177e4SLinus Torvalds nlh->nlmsg_type = NLMSG_ERROR; 5971da177e4SLinus Torvalds nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct nlmsgerr)); 5981da177e4SLinus Torvalds skb_trim(skb, nlh->nlmsg_len); 5999ef1d4c7SPatrick McHardy e = NLMSG_DATA(nlh); 6009ef1d4c7SPatrick McHardy e->error = -ETIMEDOUT; 6019ef1d4c7SPatrick McHardy memset(&e->msg, 0, sizeof(e->msg)); 6022942e900SThomas Graf 6034feb88e5SBenjamin Thery rtnl_unicast(skb, net, NETLINK_CB(skb).pid); 6041da177e4SLinus Torvalds } else 6051da177e4SLinus Torvalds kfree_skb(skb); 6061da177e4SLinus Torvalds } 6071da177e4SLinus Torvalds 6085c0a66f5SBenjamin Thery ipmr_cache_free(c); 6091da177e4SLinus Torvalds } 6101da177e4SLinus Torvalds 6111da177e4SLinus Torvalds 612e258beb2SPatrick McHardy /* Timer process for the unresolved queue. */ 6131da177e4SLinus Torvalds 614e258beb2SPatrick McHardy static void ipmr_expire_process(unsigned long arg) 6151da177e4SLinus Torvalds { 6160c12295aSPatrick McHardy struct mr_table *mrt = (struct mr_table *)arg; 6171da177e4SLinus Torvalds unsigned long now; 6181da177e4SLinus Torvalds unsigned long expires; 619862465f2SPatrick McHardy struct mfc_cache *c, *next; 6201da177e4SLinus Torvalds 6211da177e4SLinus Torvalds if (!spin_trylock(&mfc_unres_lock)) { 6220c12295aSPatrick McHardy mod_timer(&mrt->ipmr_expire_timer, jiffies+HZ/10); 6231da177e4SLinus Torvalds return; 6241da177e4SLinus Torvalds } 6251da177e4SLinus Torvalds 6260c12295aSPatrick McHardy if (list_empty(&mrt->mfc_unres_queue)) 6271da177e4SLinus Torvalds goto out; 6281da177e4SLinus Torvalds 6291da177e4SLinus Torvalds now = jiffies; 6301da177e4SLinus Torvalds expires = 10*HZ; 6311da177e4SLinus Torvalds 6320c12295aSPatrick McHardy list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) { 6331da177e4SLinus Torvalds if (time_after(c->mfc_un.unres.expires, now)) { 6341da177e4SLinus Torvalds unsigned long interval = c->mfc_un.unres.expires - now; 6351da177e4SLinus Torvalds if (interval < expires) 6361da177e4SLinus Torvalds expires = interval; 6371da177e4SLinus Torvalds continue; 6381da177e4SLinus Torvalds } 6391da177e4SLinus Torvalds 640862465f2SPatrick McHardy list_del(&c->list); 6410c12295aSPatrick McHardy ipmr_destroy_unres(mrt, c); 6421da177e4SLinus Torvalds } 6431da177e4SLinus Torvalds 6440c12295aSPatrick McHardy if (!list_empty(&mrt->mfc_unres_queue)) 6450c12295aSPatrick McHardy mod_timer(&mrt->ipmr_expire_timer, jiffies + expires); 6461da177e4SLinus Torvalds 6471da177e4SLinus Torvalds out: 6481da177e4SLinus Torvalds spin_unlock(&mfc_unres_lock); 6491da177e4SLinus Torvalds } 6501da177e4SLinus Torvalds 6511da177e4SLinus Torvalds /* Fill oifs list. It is called under write locked mrt_lock. */ 6521da177e4SLinus Torvalds 6530c12295aSPatrick McHardy static void ipmr_update_thresholds(struct mr_table *mrt, struct mfc_cache *cache, 654d658f8a0SPatrick McHardy unsigned char *ttls) 6551da177e4SLinus Torvalds { 6561da177e4SLinus Torvalds int vifi; 6571da177e4SLinus Torvalds 6581da177e4SLinus Torvalds cache->mfc_un.res.minvif = MAXVIFS; 6591da177e4SLinus Torvalds cache->mfc_un.res.maxvif = 0; 6601da177e4SLinus Torvalds memset(cache->mfc_un.res.ttls, 255, MAXVIFS); 6611da177e4SLinus Torvalds 6620c12295aSPatrick McHardy for (vifi = 0; vifi < mrt->maxvif; vifi++) { 6630c12295aSPatrick McHardy if (VIF_EXISTS(mrt, vifi) && 664cf958ae3SBenjamin Thery ttls[vifi] && ttls[vifi] < 255) { 6651da177e4SLinus Torvalds cache->mfc_un.res.ttls[vifi] = ttls[vifi]; 6661da177e4SLinus Torvalds if (cache->mfc_un.res.minvif > vifi) 6671da177e4SLinus Torvalds cache->mfc_un.res.minvif = vifi; 6681da177e4SLinus Torvalds if (cache->mfc_un.res.maxvif <= vifi) 6691da177e4SLinus Torvalds cache->mfc_un.res.maxvif = vifi + 1; 6701da177e4SLinus Torvalds } 6711da177e4SLinus Torvalds } 6721da177e4SLinus Torvalds } 6731da177e4SLinus Torvalds 6740c12295aSPatrick McHardy static int vif_add(struct net *net, struct mr_table *mrt, 6750c12295aSPatrick McHardy struct vifctl *vifc, int mrtsock) 6761da177e4SLinus Torvalds { 6771da177e4SLinus Torvalds int vifi = vifc->vifc_vifi; 6780c12295aSPatrick McHardy struct vif_device *v = &mrt->vif_table[vifi]; 6791da177e4SLinus Torvalds struct net_device *dev; 6801da177e4SLinus Torvalds struct in_device *in_dev; 681d607032dSWang Chen int err; 6821da177e4SLinus Torvalds 6831da177e4SLinus Torvalds /* Is vif busy ? */ 6840c12295aSPatrick McHardy if (VIF_EXISTS(mrt, vifi)) 6851da177e4SLinus Torvalds return -EADDRINUSE; 6861da177e4SLinus Torvalds 6871da177e4SLinus Torvalds switch (vifc->vifc_flags) { 6881da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM 6891da177e4SLinus Torvalds case VIFF_REGISTER: 6901da177e4SLinus Torvalds /* 6911da177e4SLinus Torvalds * Special Purpose VIF in PIM 6921da177e4SLinus Torvalds * All the packets will be sent to the daemon 6931da177e4SLinus Torvalds */ 6940c12295aSPatrick McHardy if (mrt->mroute_reg_vif_num >= 0) 6951da177e4SLinus Torvalds return -EADDRINUSE; 696f0ad0860SPatrick McHardy dev = ipmr_reg_vif(net, mrt); 6971da177e4SLinus Torvalds if (!dev) 6981da177e4SLinus Torvalds return -ENOBUFS; 699d607032dSWang Chen err = dev_set_allmulti(dev, 1); 700d607032dSWang Chen if (err) { 701d607032dSWang Chen unregister_netdevice(dev); 7027dc00c82SWang Chen dev_put(dev); 703d607032dSWang Chen return err; 704d607032dSWang Chen } 7051da177e4SLinus Torvalds break; 7061da177e4SLinus Torvalds #endif 7071da177e4SLinus Torvalds case VIFF_TUNNEL: 7084feb88e5SBenjamin Thery dev = ipmr_new_tunnel(net, vifc); 7091da177e4SLinus Torvalds if (!dev) 7101da177e4SLinus Torvalds return -ENOBUFS; 711d607032dSWang Chen err = dev_set_allmulti(dev, 1); 712d607032dSWang Chen if (err) { 713d607032dSWang Chen ipmr_del_tunnel(dev, vifc); 7147dc00c82SWang Chen dev_put(dev); 715d607032dSWang Chen return err; 716d607032dSWang Chen } 7171da177e4SLinus Torvalds break; 718ee5e81f0SIlia K 719ee5e81f0SIlia K case VIFF_USE_IFINDEX: 7201da177e4SLinus Torvalds case 0: 721ee5e81f0SIlia K if (vifc->vifc_flags == VIFF_USE_IFINDEX) { 722ee5e81f0SIlia K dev = dev_get_by_index(net, vifc->vifc_lcl_ifindex); 723ee5e81f0SIlia K if (dev && dev->ip_ptr == NULL) { 724ee5e81f0SIlia K dev_put(dev); 725ee5e81f0SIlia K return -EADDRNOTAVAIL; 726ee5e81f0SIlia K } 727ee5e81f0SIlia K } else 7284feb88e5SBenjamin Thery dev = ip_dev_find(net, vifc->vifc_lcl_addr.s_addr); 729ee5e81f0SIlia K 7301da177e4SLinus Torvalds if (!dev) 7311da177e4SLinus Torvalds return -EADDRNOTAVAIL; 732d607032dSWang Chen err = dev_set_allmulti(dev, 1); 7337dc00c82SWang Chen if (err) { 7347dc00c82SWang Chen dev_put(dev); 735d607032dSWang Chen return err; 7367dc00c82SWang Chen } 7371da177e4SLinus Torvalds break; 7381da177e4SLinus Torvalds default: 7391da177e4SLinus Torvalds return -EINVAL; 7401da177e4SLinus Torvalds } 7411da177e4SLinus Torvalds 742d0490cfdSDan Carpenter if ((in_dev = __in_dev_get_rtnl(dev)) == NULL) { 743d0490cfdSDan Carpenter dev_put(dev); 7441da177e4SLinus Torvalds return -EADDRNOTAVAIL; 745d0490cfdSDan Carpenter } 74642f811b8SHerbert Xu IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)++; 7471da177e4SLinus Torvalds ip_rt_multicast_event(in_dev); 7481da177e4SLinus Torvalds 7491da177e4SLinus Torvalds /* 7501da177e4SLinus Torvalds * Fill in the VIF structures 7511da177e4SLinus Torvalds */ 7521da177e4SLinus Torvalds v->rate_limit = vifc->vifc_rate_limit; 7531da177e4SLinus Torvalds v->local = vifc->vifc_lcl_addr.s_addr; 7541da177e4SLinus Torvalds v->remote = vifc->vifc_rmt_addr.s_addr; 7551da177e4SLinus Torvalds v->flags = vifc->vifc_flags; 7561da177e4SLinus Torvalds if (!mrtsock) 7571da177e4SLinus Torvalds v->flags |= VIFF_STATIC; 7581da177e4SLinus Torvalds v->threshold = vifc->vifc_threshold; 7591da177e4SLinus Torvalds v->bytes_in = 0; 7601da177e4SLinus Torvalds v->bytes_out = 0; 7611da177e4SLinus Torvalds v->pkt_in = 0; 7621da177e4SLinus Torvalds v->pkt_out = 0; 7631da177e4SLinus Torvalds v->link = dev->ifindex; 7641da177e4SLinus Torvalds if (v->flags&(VIFF_TUNNEL|VIFF_REGISTER)) 7651da177e4SLinus Torvalds v->link = dev->iflink; 7661da177e4SLinus Torvalds 7671da177e4SLinus Torvalds /* And finish update writing critical data */ 7681da177e4SLinus Torvalds write_lock_bh(&mrt_lock); 7691da177e4SLinus Torvalds v->dev = dev; 7701da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM 7711da177e4SLinus Torvalds if (v->flags&VIFF_REGISTER) 7720c12295aSPatrick McHardy mrt->mroute_reg_vif_num = vifi; 7731da177e4SLinus Torvalds #endif 7740c12295aSPatrick McHardy if (vifi+1 > mrt->maxvif) 7750c12295aSPatrick McHardy mrt->maxvif = vifi+1; 7761da177e4SLinus Torvalds write_unlock_bh(&mrt_lock); 7771da177e4SLinus Torvalds return 0; 7781da177e4SLinus Torvalds } 7791da177e4SLinus Torvalds 7800c12295aSPatrick McHardy static struct mfc_cache *ipmr_cache_find(struct mr_table *mrt, 7814feb88e5SBenjamin Thery __be32 origin, 7824feb88e5SBenjamin Thery __be32 mcastgrp) 7831da177e4SLinus Torvalds { 7841da177e4SLinus Torvalds int line = MFC_HASH(mcastgrp, origin); 7851da177e4SLinus Torvalds struct mfc_cache *c; 7861da177e4SLinus Torvalds 7870c12295aSPatrick McHardy list_for_each_entry(c, &mrt->mfc_cache_array[line], list) { 7881da177e4SLinus Torvalds if (c->mfc_origin == origin && c->mfc_mcastgrp == mcastgrp) 7891da177e4SLinus Torvalds return c; 7901da177e4SLinus Torvalds } 791862465f2SPatrick McHardy return NULL; 792862465f2SPatrick McHardy } 7931da177e4SLinus Torvalds 7941da177e4SLinus Torvalds /* 7951da177e4SLinus Torvalds * Allocate a multicast cache entry 7961da177e4SLinus Torvalds */ 797d658f8a0SPatrick McHardy static struct mfc_cache *ipmr_cache_alloc(void) 7981da177e4SLinus Torvalds { 799c3762229SRobert P. J. Day struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_KERNEL); 8001da177e4SLinus Torvalds if (c == NULL) 8011da177e4SLinus Torvalds return NULL; 8021da177e4SLinus Torvalds c->mfc_un.res.minvif = MAXVIFS; 8031da177e4SLinus Torvalds return c; 8041da177e4SLinus Torvalds } 8051da177e4SLinus Torvalds 806d658f8a0SPatrick McHardy static struct mfc_cache *ipmr_cache_alloc_unres(void) 8071da177e4SLinus Torvalds { 808c3762229SRobert P. J. Day struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_ATOMIC); 8091da177e4SLinus Torvalds if (c == NULL) 8101da177e4SLinus Torvalds return NULL; 8111da177e4SLinus Torvalds skb_queue_head_init(&c->mfc_un.unres.unresolved); 8121da177e4SLinus Torvalds c->mfc_un.unres.expires = jiffies + 10*HZ; 8131da177e4SLinus Torvalds return c; 8141da177e4SLinus Torvalds } 8151da177e4SLinus Torvalds 8161da177e4SLinus Torvalds /* 8171da177e4SLinus Torvalds * A cache entry has gone into a resolved state from queued 8181da177e4SLinus Torvalds */ 8191da177e4SLinus Torvalds 8200c12295aSPatrick McHardy static void ipmr_cache_resolve(struct net *net, struct mr_table *mrt, 8210c12295aSPatrick McHardy struct mfc_cache *uc, struct mfc_cache *c) 8221da177e4SLinus Torvalds { 8231da177e4SLinus Torvalds struct sk_buff *skb; 8249ef1d4c7SPatrick McHardy struct nlmsgerr *e; 8251da177e4SLinus Torvalds 8261da177e4SLinus Torvalds /* 8271da177e4SLinus Torvalds * Play the pending entries through our router 8281da177e4SLinus Torvalds */ 8291da177e4SLinus Torvalds 8301da177e4SLinus Torvalds while ((skb = __skb_dequeue(&uc->mfc_un.unres.unresolved))) { 831eddc9ec5SArnaldo Carvalho de Melo if (ip_hdr(skb)->version == 0) { 8321da177e4SLinus Torvalds struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr)); 8331da177e4SLinus Torvalds 834cb6a4e46SPatrick McHardy if (__ipmr_fill_mroute(mrt, skb, c, NLMSG_DATA(nlh)) > 0) { 83527a884dcSArnaldo Carvalho de Melo nlh->nlmsg_len = (skb_tail_pointer(skb) - 83627a884dcSArnaldo Carvalho de Melo (u8 *)nlh); 8371da177e4SLinus Torvalds } else { 8381da177e4SLinus Torvalds nlh->nlmsg_type = NLMSG_ERROR; 8391da177e4SLinus Torvalds nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct nlmsgerr)); 8401da177e4SLinus Torvalds skb_trim(skb, nlh->nlmsg_len); 8419ef1d4c7SPatrick McHardy e = NLMSG_DATA(nlh); 8429ef1d4c7SPatrick McHardy e->error = -EMSGSIZE; 8439ef1d4c7SPatrick McHardy memset(&e->msg, 0, sizeof(e->msg)); 8441da177e4SLinus Torvalds } 8452942e900SThomas Graf 846d658f8a0SPatrick McHardy rtnl_unicast(skb, net, NETLINK_CB(skb).pid); 8471da177e4SLinus Torvalds } else 8480c12295aSPatrick McHardy ip_mr_forward(net, mrt, skb, c, 0); 8491da177e4SLinus Torvalds } 8501da177e4SLinus Torvalds } 8511da177e4SLinus Torvalds 8521da177e4SLinus Torvalds /* 8531da177e4SLinus Torvalds * Bounce a cache query up to mrouted. We could use netlink for this but mrouted 8541da177e4SLinus Torvalds * expects the following bizarre scheme. 8551da177e4SLinus Torvalds * 8561da177e4SLinus Torvalds * Called under mrt_lock. 8571da177e4SLinus Torvalds */ 8581da177e4SLinus Torvalds 8590c12295aSPatrick McHardy static int ipmr_cache_report(struct mr_table *mrt, 8604feb88e5SBenjamin Thery struct sk_buff *pkt, vifi_t vifi, int assert) 8611da177e4SLinus Torvalds { 8621da177e4SLinus Torvalds struct sk_buff *skb; 863c9bdd4b5SArnaldo Carvalho de Melo const int ihl = ip_hdrlen(pkt); 8641da177e4SLinus Torvalds struct igmphdr *igmp; 8651da177e4SLinus Torvalds struct igmpmsg *msg; 8661da177e4SLinus Torvalds int ret; 8671da177e4SLinus Torvalds 8681da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM 8691da177e4SLinus Torvalds if (assert == IGMPMSG_WHOLEPKT) 8701da177e4SLinus Torvalds skb = skb_realloc_headroom(pkt, sizeof(struct iphdr)); 8711da177e4SLinus Torvalds else 8721da177e4SLinus Torvalds #endif 8731da177e4SLinus Torvalds skb = alloc_skb(128, GFP_ATOMIC); 8741da177e4SLinus Torvalds 8751da177e4SLinus Torvalds if (!skb) 8761da177e4SLinus Torvalds return -ENOBUFS; 8771da177e4SLinus Torvalds 8781da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM 8791da177e4SLinus Torvalds if (assert == IGMPMSG_WHOLEPKT) { 8801da177e4SLinus Torvalds /* Ugly, but we have no choice with this interface. 8811da177e4SLinus Torvalds Duplicate old header, fix ihl, length etc. 8821da177e4SLinus Torvalds And all this only to mangle msg->im_msgtype and 8831da177e4SLinus Torvalds to set msg->im_mbz to "mbz" :-) 8841da177e4SLinus Torvalds */ 885878c8145SArnaldo Carvalho de Melo skb_push(skb, sizeof(struct iphdr)); 886878c8145SArnaldo Carvalho de Melo skb_reset_network_header(skb); 887badff6d0SArnaldo Carvalho de Melo skb_reset_transport_header(skb); 8880272ffc4SArnaldo Carvalho de Melo msg = (struct igmpmsg *)skb_network_header(skb); 889d56f90a7SArnaldo Carvalho de Melo memcpy(msg, skb_network_header(pkt), sizeof(struct iphdr)); 8901da177e4SLinus Torvalds msg->im_msgtype = IGMPMSG_WHOLEPKT; 8911da177e4SLinus Torvalds msg->im_mbz = 0; 8920c12295aSPatrick McHardy msg->im_vif = mrt->mroute_reg_vif_num; 893eddc9ec5SArnaldo Carvalho de Melo ip_hdr(skb)->ihl = sizeof(struct iphdr) >> 2; 894eddc9ec5SArnaldo Carvalho de Melo ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(pkt)->tot_len) + 895eddc9ec5SArnaldo Carvalho de Melo sizeof(struct iphdr)); 8961da177e4SLinus Torvalds } else 8971da177e4SLinus Torvalds #endif 8981da177e4SLinus Torvalds { 8991da177e4SLinus Torvalds 9001da177e4SLinus Torvalds /* 9011da177e4SLinus Torvalds * Copy the IP header 9021da177e4SLinus Torvalds */ 9031da177e4SLinus Torvalds 90427a884dcSArnaldo Carvalho de Melo skb->network_header = skb->tail; 905ddc7b8e3SArnaldo Carvalho de Melo skb_put(skb, ihl); 90627d7ff46SArnaldo Carvalho de Melo skb_copy_to_linear_data(skb, pkt->data, ihl); 907eddc9ec5SArnaldo Carvalho de Melo ip_hdr(skb)->protocol = 0; /* Flag to the kernel this is a route add */ 908eddc9ec5SArnaldo Carvalho de Melo msg = (struct igmpmsg *)skb_network_header(skb); 9091da177e4SLinus Torvalds msg->im_vif = vifi; 910adf30907SEric Dumazet skb_dst_set(skb, dst_clone(skb_dst(pkt))); 9111da177e4SLinus Torvalds 9121da177e4SLinus Torvalds /* 9131da177e4SLinus Torvalds * Add our header 9141da177e4SLinus Torvalds */ 9151da177e4SLinus Torvalds 9161da177e4SLinus Torvalds igmp=(struct igmphdr *)skb_put(skb, sizeof(struct igmphdr)); 9171da177e4SLinus Torvalds igmp->type = 9181da177e4SLinus Torvalds msg->im_msgtype = assert; 9191da177e4SLinus Torvalds igmp->code = 0; 920eddc9ec5SArnaldo Carvalho de Melo ip_hdr(skb)->tot_len = htons(skb->len); /* Fix the length */ 921b0e380b1SArnaldo Carvalho de Melo skb->transport_header = skb->network_header; 9221da177e4SLinus Torvalds } 9231da177e4SLinus Torvalds 9240c12295aSPatrick McHardy if (mrt->mroute_sk == NULL) { 9251da177e4SLinus Torvalds kfree_skb(skb); 9261da177e4SLinus Torvalds return -EINVAL; 9271da177e4SLinus Torvalds } 9281da177e4SLinus Torvalds 9291da177e4SLinus Torvalds /* 9301da177e4SLinus Torvalds * Deliver to mrouted 9311da177e4SLinus Torvalds */ 9320c12295aSPatrick McHardy ret = sock_queue_rcv_skb(mrt->mroute_sk, skb); 93370a269e6SBenjamin Thery if (ret < 0) { 9341da177e4SLinus Torvalds if (net_ratelimit()) 9351da177e4SLinus Torvalds printk(KERN_WARNING "mroute: pending queue full, dropping entries.\n"); 9361da177e4SLinus Torvalds kfree_skb(skb); 9371da177e4SLinus Torvalds } 9381da177e4SLinus Torvalds 9391da177e4SLinus Torvalds return ret; 9401da177e4SLinus Torvalds } 9411da177e4SLinus Torvalds 9421da177e4SLinus Torvalds /* 9431da177e4SLinus Torvalds * Queue a packet for resolution. It gets locked cache entry! 9441da177e4SLinus Torvalds */ 9451da177e4SLinus Torvalds 9461da177e4SLinus Torvalds static int 9470c12295aSPatrick McHardy ipmr_cache_unresolved(struct mr_table *mrt, vifi_t vifi, struct sk_buff *skb) 9481da177e4SLinus Torvalds { 949862465f2SPatrick McHardy bool found = false; 9501da177e4SLinus Torvalds int err; 9511da177e4SLinus Torvalds struct mfc_cache *c; 952eddc9ec5SArnaldo Carvalho de Melo const struct iphdr *iph = ip_hdr(skb); 9531da177e4SLinus Torvalds 9541da177e4SLinus Torvalds spin_lock_bh(&mfc_unres_lock); 9550c12295aSPatrick McHardy list_for_each_entry(c, &mrt->mfc_unres_queue, list) { 956e258beb2SPatrick McHardy if (c->mfc_mcastgrp == iph->daddr && 957862465f2SPatrick McHardy c->mfc_origin == iph->saddr) { 958862465f2SPatrick McHardy found = true; 9591da177e4SLinus Torvalds break; 9601da177e4SLinus Torvalds } 961862465f2SPatrick McHardy } 9621da177e4SLinus Torvalds 963862465f2SPatrick McHardy if (!found) { 9641da177e4SLinus Torvalds /* 9651da177e4SLinus Torvalds * Create a new entry if allowable 9661da177e4SLinus Torvalds */ 9671da177e4SLinus Torvalds 9680c12295aSPatrick McHardy if (atomic_read(&mrt->cache_resolve_queue_len) >= 10 || 969d658f8a0SPatrick McHardy (c = ipmr_cache_alloc_unres()) == NULL) { 9701da177e4SLinus Torvalds spin_unlock_bh(&mfc_unres_lock); 9711da177e4SLinus Torvalds 9721da177e4SLinus Torvalds kfree_skb(skb); 9731da177e4SLinus Torvalds return -ENOBUFS; 9741da177e4SLinus Torvalds } 9751da177e4SLinus Torvalds 9761da177e4SLinus Torvalds /* 9771da177e4SLinus Torvalds * Fill in the new cache entry 9781da177e4SLinus Torvalds */ 9791da177e4SLinus Torvalds c->mfc_parent = -1; 980eddc9ec5SArnaldo Carvalho de Melo c->mfc_origin = iph->saddr; 981eddc9ec5SArnaldo Carvalho de Melo c->mfc_mcastgrp = iph->daddr; 9821da177e4SLinus Torvalds 9831da177e4SLinus Torvalds /* 9841da177e4SLinus Torvalds * Reflect first query at mrouted. 9851da177e4SLinus Torvalds */ 9860c12295aSPatrick McHardy err = ipmr_cache_report(mrt, skb, vifi, IGMPMSG_NOCACHE); 9874feb88e5SBenjamin Thery if (err < 0) { 9881da177e4SLinus Torvalds /* If the report failed throw the cache entry 9891da177e4SLinus Torvalds out - Brad Parker 9901da177e4SLinus Torvalds */ 9911da177e4SLinus Torvalds spin_unlock_bh(&mfc_unres_lock); 9921da177e4SLinus Torvalds 9935c0a66f5SBenjamin Thery ipmr_cache_free(c); 9941da177e4SLinus Torvalds kfree_skb(skb); 9951da177e4SLinus Torvalds return err; 9961da177e4SLinus Torvalds } 9971da177e4SLinus Torvalds 9980c12295aSPatrick McHardy atomic_inc(&mrt->cache_resolve_queue_len); 9990c12295aSPatrick McHardy list_add(&c->list, &mrt->mfc_unres_queue); 10001da177e4SLinus Torvalds 1001278554bdSDavid S. Miller if (atomic_read(&mrt->cache_resolve_queue_len) == 1) 10020c12295aSPatrick McHardy mod_timer(&mrt->ipmr_expire_timer, c->mfc_un.unres.expires); 10031da177e4SLinus Torvalds } 10041da177e4SLinus Torvalds 10051da177e4SLinus Torvalds /* 10061da177e4SLinus Torvalds * See if we can append the packet 10071da177e4SLinus Torvalds */ 10081da177e4SLinus Torvalds if (c->mfc_un.unres.unresolved.qlen>3) { 10091da177e4SLinus Torvalds kfree_skb(skb); 10101da177e4SLinus Torvalds err = -ENOBUFS; 10111da177e4SLinus Torvalds } else { 10121da177e4SLinus Torvalds skb_queue_tail(&c->mfc_un.unres.unresolved, skb); 10131da177e4SLinus Torvalds err = 0; 10141da177e4SLinus Torvalds } 10151da177e4SLinus Torvalds 10161da177e4SLinus Torvalds spin_unlock_bh(&mfc_unres_lock); 10171da177e4SLinus Torvalds return err; 10181da177e4SLinus Torvalds } 10191da177e4SLinus Torvalds 10201da177e4SLinus Torvalds /* 10211da177e4SLinus Torvalds * MFC cache manipulation by user space mroute daemon 10221da177e4SLinus Torvalds */ 10231da177e4SLinus Torvalds 10240c12295aSPatrick McHardy static int ipmr_mfc_delete(struct mr_table *mrt, struct mfcctl *mfc) 10251da177e4SLinus Torvalds { 10261da177e4SLinus Torvalds int line; 1027862465f2SPatrick McHardy struct mfc_cache *c, *next; 10281da177e4SLinus Torvalds 10291da177e4SLinus Torvalds line = MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr); 10301da177e4SLinus Torvalds 10310c12295aSPatrick McHardy list_for_each_entry_safe(c, next, &mrt->mfc_cache_array[line], list) { 10321da177e4SLinus Torvalds if (c->mfc_origin == mfc->mfcc_origin.s_addr && 10331da177e4SLinus Torvalds c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr) { 10341da177e4SLinus Torvalds write_lock_bh(&mrt_lock); 1035862465f2SPatrick McHardy list_del(&c->list); 10361da177e4SLinus Torvalds write_unlock_bh(&mrt_lock); 10371da177e4SLinus Torvalds 10385c0a66f5SBenjamin Thery ipmr_cache_free(c); 10391da177e4SLinus Torvalds return 0; 10401da177e4SLinus Torvalds } 10411da177e4SLinus Torvalds } 10421da177e4SLinus Torvalds return -ENOENT; 10431da177e4SLinus Torvalds } 10441da177e4SLinus Torvalds 10450c12295aSPatrick McHardy static int ipmr_mfc_add(struct net *net, struct mr_table *mrt, 10460c12295aSPatrick McHardy struct mfcctl *mfc, int mrtsock) 10471da177e4SLinus Torvalds { 1048862465f2SPatrick McHardy bool found = false; 10491da177e4SLinus Torvalds int line; 1050862465f2SPatrick McHardy struct mfc_cache *uc, *c; 10511da177e4SLinus Torvalds 1052a50436f2SPatrick McHardy if (mfc->mfcc_parent >= MAXVIFS) 1053a50436f2SPatrick McHardy return -ENFILE; 1054a50436f2SPatrick McHardy 10551da177e4SLinus Torvalds line = MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr); 10561da177e4SLinus Torvalds 10570c12295aSPatrick McHardy list_for_each_entry(c, &mrt->mfc_cache_array[line], list) { 10581da177e4SLinus Torvalds if (c->mfc_origin == mfc->mfcc_origin.s_addr && 1059862465f2SPatrick McHardy c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr) { 1060862465f2SPatrick McHardy found = true; 10611da177e4SLinus Torvalds break; 10621da177e4SLinus Torvalds } 1063862465f2SPatrick McHardy } 10641da177e4SLinus Torvalds 1065862465f2SPatrick McHardy if (found) { 10661da177e4SLinus Torvalds write_lock_bh(&mrt_lock); 10671da177e4SLinus Torvalds c->mfc_parent = mfc->mfcc_parent; 10680c12295aSPatrick McHardy ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls); 10691da177e4SLinus Torvalds if (!mrtsock) 10701da177e4SLinus Torvalds c->mfc_flags |= MFC_STATIC; 10711da177e4SLinus Torvalds write_unlock_bh(&mrt_lock); 10721da177e4SLinus Torvalds return 0; 10731da177e4SLinus Torvalds } 10741da177e4SLinus Torvalds 1075f97c1e0cSJoe Perches if (!ipv4_is_multicast(mfc->mfcc_mcastgrp.s_addr)) 10761da177e4SLinus Torvalds return -EINVAL; 10771da177e4SLinus Torvalds 1078d658f8a0SPatrick McHardy c = ipmr_cache_alloc(); 10791da177e4SLinus Torvalds if (c == NULL) 10801da177e4SLinus Torvalds return -ENOMEM; 10811da177e4SLinus Torvalds 10821da177e4SLinus Torvalds c->mfc_origin = mfc->mfcc_origin.s_addr; 10831da177e4SLinus Torvalds c->mfc_mcastgrp = mfc->mfcc_mcastgrp.s_addr; 10841da177e4SLinus Torvalds c->mfc_parent = mfc->mfcc_parent; 10850c12295aSPatrick McHardy ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls); 10861da177e4SLinus Torvalds if (!mrtsock) 10871da177e4SLinus Torvalds c->mfc_flags |= MFC_STATIC; 10881da177e4SLinus Torvalds 10891da177e4SLinus Torvalds write_lock_bh(&mrt_lock); 10900c12295aSPatrick McHardy list_add(&c->list, &mrt->mfc_cache_array[line]); 10911da177e4SLinus Torvalds write_unlock_bh(&mrt_lock); 10921da177e4SLinus Torvalds 10931da177e4SLinus Torvalds /* 10941da177e4SLinus Torvalds * Check to see if we resolved a queued list. If so we 10951da177e4SLinus Torvalds * need to send on the frames and tidy up. 10961da177e4SLinus Torvalds */ 1097b0ebb739SPatrick McHardy found = false; 10981da177e4SLinus Torvalds spin_lock_bh(&mfc_unres_lock); 10990c12295aSPatrick McHardy list_for_each_entry(uc, &mrt->mfc_unres_queue, list) { 1100e258beb2SPatrick McHardy if (uc->mfc_origin == c->mfc_origin && 11011da177e4SLinus Torvalds uc->mfc_mcastgrp == c->mfc_mcastgrp) { 1102862465f2SPatrick McHardy list_del(&uc->list); 11030c12295aSPatrick McHardy atomic_dec(&mrt->cache_resolve_queue_len); 1104b0ebb739SPatrick McHardy found = true; 11051da177e4SLinus Torvalds break; 11061da177e4SLinus Torvalds } 11071da177e4SLinus Torvalds } 11080c12295aSPatrick McHardy if (list_empty(&mrt->mfc_unres_queue)) 11090c12295aSPatrick McHardy del_timer(&mrt->ipmr_expire_timer); 11101da177e4SLinus Torvalds spin_unlock_bh(&mfc_unres_lock); 11111da177e4SLinus Torvalds 1112b0ebb739SPatrick McHardy if (found) { 11130c12295aSPatrick McHardy ipmr_cache_resolve(net, mrt, uc, c); 11145c0a66f5SBenjamin Thery ipmr_cache_free(uc); 11151da177e4SLinus Torvalds } 11161da177e4SLinus Torvalds return 0; 11171da177e4SLinus Torvalds } 11181da177e4SLinus Torvalds 11191da177e4SLinus Torvalds /* 11201da177e4SLinus Torvalds * Close the multicast socket, and clear the vif tables etc 11211da177e4SLinus Torvalds */ 11221da177e4SLinus Torvalds 11230c12295aSPatrick McHardy static void mroute_clean_tables(struct mr_table *mrt) 11241da177e4SLinus Torvalds { 11251da177e4SLinus Torvalds int i; 1126d17fa6faSEric Dumazet LIST_HEAD(list); 1127862465f2SPatrick McHardy struct mfc_cache *c, *next; 11281da177e4SLinus Torvalds 11291da177e4SLinus Torvalds /* 11301da177e4SLinus Torvalds * Shut down all active vif entries 11311da177e4SLinus Torvalds */ 11320c12295aSPatrick McHardy for (i = 0; i < mrt->maxvif; i++) { 11330c12295aSPatrick McHardy if (!(mrt->vif_table[i].flags&VIFF_STATIC)) 11340c12295aSPatrick McHardy vif_delete(mrt, i, 0, &list); 11351da177e4SLinus Torvalds } 1136d17fa6faSEric Dumazet unregister_netdevice_many(&list); 11371da177e4SLinus Torvalds 11381da177e4SLinus Torvalds /* 11391da177e4SLinus Torvalds * Wipe the cache 11401da177e4SLinus Torvalds */ 11411da177e4SLinus Torvalds for (i = 0; i < MFC_LINES; i++) { 11420c12295aSPatrick McHardy list_for_each_entry_safe(c, next, &mrt->mfc_cache_array[i], list) { 1143862465f2SPatrick McHardy if (c->mfc_flags&MFC_STATIC) 11441da177e4SLinus Torvalds continue; 11451da177e4SLinus Torvalds write_lock_bh(&mrt_lock); 1146862465f2SPatrick McHardy list_del(&c->list); 11471da177e4SLinus Torvalds write_unlock_bh(&mrt_lock); 11481da177e4SLinus Torvalds 11495c0a66f5SBenjamin Thery ipmr_cache_free(c); 11501da177e4SLinus Torvalds } 11511da177e4SLinus Torvalds } 11521da177e4SLinus Torvalds 11530c12295aSPatrick McHardy if (atomic_read(&mrt->cache_resolve_queue_len) != 0) { 11541da177e4SLinus Torvalds spin_lock_bh(&mfc_unres_lock); 11550c12295aSPatrick McHardy list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) { 1156862465f2SPatrick McHardy list_del(&c->list); 11570c12295aSPatrick McHardy ipmr_destroy_unres(mrt, c); 11581da177e4SLinus Torvalds } 11591da177e4SLinus Torvalds spin_unlock_bh(&mfc_unres_lock); 11601da177e4SLinus Torvalds } 11611da177e4SLinus Torvalds } 11621da177e4SLinus Torvalds 11631da177e4SLinus Torvalds static void mrtsock_destruct(struct sock *sk) 11641da177e4SLinus Torvalds { 11654feb88e5SBenjamin Thery struct net *net = sock_net(sk); 1166f0ad0860SPatrick McHardy struct mr_table *mrt; 11674feb88e5SBenjamin Thery 11681da177e4SLinus Torvalds rtnl_lock(); 1169f0ad0860SPatrick McHardy ipmr_for_each_table(mrt, net) { 11700c12295aSPatrick McHardy if (sk == mrt->mroute_sk) { 11714feb88e5SBenjamin Thery IPV4_DEVCONF_ALL(net, MC_FORWARDING)--; 11721da177e4SLinus Torvalds 11731da177e4SLinus Torvalds write_lock_bh(&mrt_lock); 11740c12295aSPatrick McHardy mrt->mroute_sk = NULL; 11751da177e4SLinus Torvalds write_unlock_bh(&mrt_lock); 11761da177e4SLinus Torvalds 11770c12295aSPatrick McHardy mroute_clean_tables(mrt); 11781da177e4SLinus Torvalds } 11791da177e4SLinus Torvalds } 11801da177e4SLinus Torvalds rtnl_unlock(); 11811da177e4SLinus Torvalds } 11821da177e4SLinus Torvalds 11831da177e4SLinus Torvalds /* 11841da177e4SLinus Torvalds * Socket options and virtual interface manipulation. The whole 11851da177e4SLinus Torvalds * virtual interface system is a complete heap, but unfortunately 11861da177e4SLinus Torvalds * that's how BSD mrouted happens to think. Maybe one day with a proper 11871da177e4SLinus Torvalds * MOSPF/PIM router set up we can clean this up. 11881da177e4SLinus Torvalds */ 11891da177e4SLinus Torvalds 1190b7058842SDavid S. Miller int ip_mroute_setsockopt(struct sock *sk, int optname, char __user *optval, unsigned int optlen) 11911da177e4SLinus Torvalds { 11921da177e4SLinus Torvalds int ret; 11931da177e4SLinus Torvalds struct vifctl vif; 11941da177e4SLinus Torvalds struct mfcctl mfc; 11954feb88e5SBenjamin Thery struct net *net = sock_net(sk); 1196f0ad0860SPatrick McHardy struct mr_table *mrt; 1197f0ad0860SPatrick McHardy 1198f0ad0860SPatrick McHardy mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT); 1199f0ad0860SPatrick McHardy if (mrt == NULL) 1200f0ad0860SPatrick McHardy return -ENOENT; 12011da177e4SLinus Torvalds 1202132adf54SStephen Hemminger if (optname != MRT_INIT) { 12030c12295aSPatrick McHardy if (sk != mrt->mroute_sk && !capable(CAP_NET_ADMIN)) 12041da177e4SLinus Torvalds return -EACCES; 12051da177e4SLinus Torvalds } 12061da177e4SLinus Torvalds 1207132adf54SStephen Hemminger switch (optname) { 12081da177e4SLinus Torvalds case MRT_INIT: 12091da177e4SLinus Torvalds if (sk->sk_type != SOCK_RAW || 1210c720c7e8SEric Dumazet inet_sk(sk)->inet_num != IPPROTO_IGMP) 12111da177e4SLinus Torvalds return -EOPNOTSUPP; 12121da177e4SLinus Torvalds if (optlen != sizeof(int)) 12131da177e4SLinus Torvalds return -ENOPROTOOPT; 12141da177e4SLinus Torvalds 12151da177e4SLinus Torvalds rtnl_lock(); 12160c12295aSPatrick McHardy if (mrt->mroute_sk) { 12171da177e4SLinus Torvalds rtnl_unlock(); 12181da177e4SLinus Torvalds return -EADDRINUSE; 12191da177e4SLinus Torvalds } 12201da177e4SLinus Torvalds 12211da177e4SLinus Torvalds ret = ip_ra_control(sk, 1, mrtsock_destruct); 12221da177e4SLinus Torvalds if (ret == 0) { 12231da177e4SLinus Torvalds write_lock_bh(&mrt_lock); 12240c12295aSPatrick McHardy mrt->mroute_sk = sk; 12251da177e4SLinus Torvalds write_unlock_bh(&mrt_lock); 12261da177e4SLinus Torvalds 12274feb88e5SBenjamin Thery IPV4_DEVCONF_ALL(net, MC_FORWARDING)++; 12281da177e4SLinus Torvalds } 12291da177e4SLinus Torvalds rtnl_unlock(); 12301da177e4SLinus Torvalds return ret; 12311da177e4SLinus Torvalds case MRT_DONE: 12320c12295aSPatrick McHardy if (sk != mrt->mroute_sk) 12331da177e4SLinus Torvalds return -EACCES; 12341da177e4SLinus Torvalds return ip_ra_control(sk, 0, NULL); 12351da177e4SLinus Torvalds case MRT_ADD_VIF: 12361da177e4SLinus Torvalds case MRT_DEL_VIF: 12371da177e4SLinus Torvalds if (optlen != sizeof(vif)) 12381da177e4SLinus Torvalds return -EINVAL; 12391da177e4SLinus Torvalds if (copy_from_user(&vif, optval, sizeof(vif))) 12401da177e4SLinus Torvalds return -EFAULT; 12411da177e4SLinus Torvalds if (vif.vifc_vifi >= MAXVIFS) 12421da177e4SLinus Torvalds return -ENFILE; 12431da177e4SLinus Torvalds rtnl_lock(); 12441da177e4SLinus Torvalds if (optname == MRT_ADD_VIF) { 12450c12295aSPatrick McHardy ret = vif_add(net, mrt, &vif, sk == mrt->mroute_sk); 12461da177e4SLinus Torvalds } else { 12470c12295aSPatrick McHardy ret = vif_delete(mrt, vif.vifc_vifi, 0, NULL); 12481da177e4SLinus Torvalds } 12491da177e4SLinus Torvalds rtnl_unlock(); 12501da177e4SLinus Torvalds return ret; 12511da177e4SLinus Torvalds 12521da177e4SLinus Torvalds /* 12531da177e4SLinus Torvalds * Manipulate the forwarding caches. These live 12541da177e4SLinus Torvalds * in a sort of kernel/user symbiosis. 12551da177e4SLinus Torvalds */ 12561da177e4SLinus Torvalds case MRT_ADD_MFC: 12571da177e4SLinus Torvalds case MRT_DEL_MFC: 12581da177e4SLinus Torvalds if (optlen != sizeof(mfc)) 12591da177e4SLinus Torvalds return -EINVAL; 12601da177e4SLinus Torvalds if (copy_from_user(&mfc, optval, sizeof(mfc))) 12611da177e4SLinus Torvalds return -EFAULT; 12621da177e4SLinus Torvalds rtnl_lock(); 12631da177e4SLinus Torvalds if (optname == MRT_DEL_MFC) 12640c12295aSPatrick McHardy ret = ipmr_mfc_delete(mrt, &mfc); 12651da177e4SLinus Torvalds else 12660c12295aSPatrick McHardy ret = ipmr_mfc_add(net, mrt, &mfc, sk == mrt->mroute_sk); 12671da177e4SLinus Torvalds rtnl_unlock(); 12681da177e4SLinus Torvalds return ret; 12691da177e4SLinus Torvalds /* 12701da177e4SLinus Torvalds * Control PIM assert. 12711da177e4SLinus Torvalds */ 12721da177e4SLinus Torvalds case MRT_ASSERT: 12731da177e4SLinus Torvalds { 12741da177e4SLinus Torvalds int v; 12751da177e4SLinus Torvalds if (get_user(v,(int __user *)optval)) 12761da177e4SLinus Torvalds return -EFAULT; 12770c12295aSPatrick McHardy mrt->mroute_do_assert = (v) ? 1 : 0; 12781da177e4SLinus Torvalds return 0; 12791da177e4SLinus Torvalds } 12801da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM 12811da177e4SLinus Torvalds case MRT_PIM: 12821da177e4SLinus Torvalds { 1283ba93ef74SStephen Hemminger int v; 1284ba93ef74SStephen Hemminger 12851da177e4SLinus Torvalds if (get_user(v,(int __user *)optval)) 12861da177e4SLinus Torvalds return -EFAULT; 12871da177e4SLinus Torvalds v = (v) ? 1 : 0; 1288ba93ef74SStephen Hemminger 12891da177e4SLinus Torvalds rtnl_lock(); 12901da177e4SLinus Torvalds ret = 0; 12910c12295aSPatrick McHardy if (v != mrt->mroute_do_pim) { 12920c12295aSPatrick McHardy mrt->mroute_do_pim = v; 12930c12295aSPatrick McHardy mrt->mroute_do_assert = v; 12941da177e4SLinus Torvalds } 12951da177e4SLinus Torvalds rtnl_unlock(); 12961da177e4SLinus Torvalds return ret; 12971da177e4SLinus Torvalds } 12981da177e4SLinus Torvalds #endif 1299f0ad0860SPatrick McHardy #ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES 1300f0ad0860SPatrick McHardy case MRT_TABLE: 1301f0ad0860SPatrick McHardy { 1302f0ad0860SPatrick McHardy u32 v; 1303f0ad0860SPatrick McHardy 1304f0ad0860SPatrick McHardy if (optlen != sizeof(u32)) 1305f0ad0860SPatrick McHardy return -EINVAL; 1306f0ad0860SPatrick McHardy if (get_user(v, (u32 __user *)optval)) 1307f0ad0860SPatrick McHardy return -EFAULT; 1308f0ad0860SPatrick McHardy if (sk == mrt->mroute_sk) 1309f0ad0860SPatrick McHardy return -EBUSY; 1310f0ad0860SPatrick McHardy 1311f0ad0860SPatrick McHardy rtnl_lock(); 1312f0ad0860SPatrick McHardy ret = 0; 1313f0ad0860SPatrick McHardy if (!ipmr_new_table(net, v)) 1314f0ad0860SPatrick McHardy ret = -ENOMEM; 1315f0ad0860SPatrick McHardy raw_sk(sk)->ipmr_table = v; 1316f0ad0860SPatrick McHardy rtnl_unlock(); 1317f0ad0860SPatrick McHardy return ret; 1318f0ad0860SPatrick McHardy } 1319f0ad0860SPatrick McHardy #endif 13201da177e4SLinus Torvalds /* 13211da177e4SLinus Torvalds * Spurious command, or MRT_VERSION which you cannot 13221da177e4SLinus Torvalds * set. 13231da177e4SLinus Torvalds */ 13241da177e4SLinus Torvalds default: 13251da177e4SLinus Torvalds return -ENOPROTOOPT; 13261da177e4SLinus Torvalds } 13271da177e4SLinus Torvalds } 13281da177e4SLinus Torvalds 13291da177e4SLinus Torvalds /* 13301da177e4SLinus Torvalds * Getsock opt support for the multicast routing system. 13311da177e4SLinus Torvalds */ 13321da177e4SLinus Torvalds 13331da177e4SLinus Torvalds int ip_mroute_getsockopt(struct sock *sk, int optname, char __user *optval, int __user *optlen) 13341da177e4SLinus Torvalds { 13351da177e4SLinus Torvalds int olr; 13361da177e4SLinus Torvalds int val; 13374feb88e5SBenjamin Thery struct net *net = sock_net(sk); 1338f0ad0860SPatrick McHardy struct mr_table *mrt; 1339f0ad0860SPatrick McHardy 1340f0ad0860SPatrick McHardy mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT); 1341f0ad0860SPatrick McHardy if (mrt == NULL) 1342f0ad0860SPatrick McHardy return -ENOENT; 13431da177e4SLinus Torvalds 13441da177e4SLinus Torvalds if (optname != MRT_VERSION && 13451da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM 13461da177e4SLinus Torvalds optname!=MRT_PIM && 13471da177e4SLinus Torvalds #endif 13481da177e4SLinus Torvalds optname!=MRT_ASSERT) 13491da177e4SLinus Torvalds return -ENOPROTOOPT; 13501da177e4SLinus Torvalds 13511da177e4SLinus Torvalds if (get_user(olr, optlen)) 13521da177e4SLinus Torvalds return -EFAULT; 13531da177e4SLinus Torvalds 13541da177e4SLinus Torvalds olr = min_t(unsigned int, olr, sizeof(int)); 13551da177e4SLinus Torvalds if (olr < 0) 13561da177e4SLinus Torvalds return -EINVAL; 13571da177e4SLinus Torvalds 13581da177e4SLinus Torvalds if (put_user(olr, optlen)) 13591da177e4SLinus Torvalds return -EFAULT; 13601da177e4SLinus Torvalds if (optname == MRT_VERSION) 13611da177e4SLinus Torvalds val = 0x0305; 13621da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM 13631da177e4SLinus Torvalds else if (optname == MRT_PIM) 13640c12295aSPatrick McHardy val = mrt->mroute_do_pim; 13651da177e4SLinus Torvalds #endif 13661da177e4SLinus Torvalds else 13670c12295aSPatrick McHardy val = mrt->mroute_do_assert; 13681da177e4SLinus Torvalds if (copy_to_user(optval, &val, olr)) 13691da177e4SLinus Torvalds return -EFAULT; 13701da177e4SLinus Torvalds return 0; 13711da177e4SLinus Torvalds } 13721da177e4SLinus Torvalds 13731da177e4SLinus Torvalds /* 13741da177e4SLinus Torvalds * The IP multicast ioctl support routines. 13751da177e4SLinus Torvalds */ 13761da177e4SLinus Torvalds 13771da177e4SLinus Torvalds int ipmr_ioctl(struct sock *sk, int cmd, void __user *arg) 13781da177e4SLinus Torvalds { 13791da177e4SLinus Torvalds struct sioc_sg_req sr; 13801da177e4SLinus Torvalds struct sioc_vif_req vr; 13811da177e4SLinus Torvalds struct vif_device *vif; 13821da177e4SLinus Torvalds struct mfc_cache *c; 13834feb88e5SBenjamin Thery struct net *net = sock_net(sk); 1384f0ad0860SPatrick McHardy struct mr_table *mrt; 1385f0ad0860SPatrick McHardy 1386f0ad0860SPatrick McHardy mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT); 1387f0ad0860SPatrick McHardy if (mrt == NULL) 1388f0ad0860SPatrick McHardy return -ENOENT; 13891da177e4SLinus Torvalds 1390132adf54SStephen Hemminger switch (cmd) { 13911da177e4SLinus Torvalds case SIOCGETVIFCNT: 13921da177e4SLinus Torvalds if (copy_from_user(&vr, arg, sizeof(vr))) 13931da177e4SLinus Torvalds return -EFAULT; 13940c12295aSPatrick McHardy if (vr.vifi >= mrt->maxvif) 13951da177e4SLinus Torvalds return -EINVAL; 13961da177e4SLinus Torvalds read_lock(&mrt_lock); 13970c12295aSPatrick McHardy vif = &mrt->vif_table[vr.vifi]; 13980c12295aSPatrick McHardy if (VIF_EXISTS(mrt, vr.vifi)) { 13991da177e4SLinus Torvalds vr.icount = vif->pkt_in; 14001da177e4SLinus Torvalds vr.ocount = vif->pkt_out; 14011da177e4SLinus Torvalds vr.ibytes = vif->bytes_in; 14021da177e4SLinus Torvalds vr.obytes = vif->bytes_out; 14031da177e4SLinus Torvalds read_unlock(&mrt_lock); 14041da177e4SLinus Torvalds 14051da177e4SLinus Torvalds if (copy_to_user(arg, &vr, sizeof(vr))) 14061da177e4SLinus Torvalds return -EFAULT; 14071da177e4SLinus Torvalds return 0; 14081da177e4SLinus Torvalds } 14091da177e4SLinus Torvalds read_unlock(&mrt_lock); 14101da177e4SLinus Torvalds return -EADDRNOTAVAIL; 14111da177e4SLinus Torvalds case SIOCGETSGCNT: 14121da177e4SLinus Torvalds if (copy_from_user(&sr, arg, sizeof(sr))) 14131da177e4SLinus Torvalds return -EFAULT; 14141da177e4SLinus Torvalds 14151da177e4SLinus Torvalds read_lock(&mrt_lock); 14160c12295aSPatrick McHardy c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr); 14171da177e4SLinus Torvalds if (c) { 14181da177e4SLinus Torvalds sr.pktcnt = c->mfc_un.res.pkt; 14191da177e4SLinus Torvalds sr.bytecnt = c->mfc_un.res.bytes; 14201da177e4SLinus Torvalds sr.wrong_if = c->mfc_un.res.wrong_if; 14211da177e4SLinus Torvalds read_unlock(&mrt_lock); 14221da177e4SLinus Torvalds 14231da177e4SLinus Torvalds if (copy_to_user(arg, &sr, sizeof(sr))) 14241da177e4SLinus Torvalds return -EFAULT; 14251da177e4SLinus Torvalds return 0; 14261da177e4SLinus Torvalds } 14271da177e4SLinus Torvalds read_unlock(&mrt_lock); 14281da177e4SLinus Torvalds return -EADDRNOTAVAIL; 14291da177e4SLinus Torvalds default: 14301da177e4SLinus Torvalds return -ENOIOCTLCMD; 14311da177e4SLinus Torvalds } 14321da177e4SLinus Torvalds } 14331da177e4SLinus Torvalds 14341da177e4SLinus Torvalds 14351da177e4SLinus Torvalds static int ipmr_device_event(struct notifier_block *this, unsigned long event, void *ptr) 14361da177e4SLinus Torvalds { 1437e9dc8653SEric W. Biederman struct net_device *dev = ptr; 14384feb88e5SBenjamin Thery struct net *net = dev_net(dev); 1439f0ad0860SPatrick McHardy struct mr_table *mrt; 14401da177e4SLinus Torvalds struct vif_device *v; 14411da177e4SLinus Torvalds int ct; 1442d17fa6faSEric Dumazet LIST_HEAD(list); 1443e9dc8653SEric W. Biederman 14441da177e4SLinus Torvalds if (event != NETDEV_UNREGISTER) 14451da177e4SLinus Torvalds return NOTIFY_DONE; 1446f0ad0860SPatrick McHardy 1447f0ad0860SPatrick McHardy ipmr_for_each_table(mrt, net) { 14480c12295aSPatrick McHardy v = &mrt->vif_table[0]; 14490c12295aSPatrick McHardy for (ct = 0; ct < mrt->maxvif; ct++, v++) { 1450e9dc8653SEric W. Biederman if (v->dev == dev) 14510c12295aSPatrick McHardy vif_delete(mrt, ct, 1, &list); 14521da177e4SLinus Torvalds } 14531da177e4SLinus Torvalds } 1454d17fa6faSEric Dumazet unregister_netdevice_many(&list); 14551da177e4SLinus Torvalds return NOTIFY_DONE; 14561da177e4SLinus Torvalds } 14571da177e4SLinus Torvalds 14581da177e4SLinus Torvalds 14591da177e4SLinus Torvalds static struct notifier_block ip_mr_notifier = { 14601da177e4SLinus Torvalds .notifier_call = ipmr_device_event, 14611da177e4SLinus Torvalds }; 14621da177e4SLinus Torvalds 14631da177e4SLinus Torvalds /* 14641da177e4SLinus Torvalds * Encapsulate a packet by attaching a valid IPIP header to it. 14651da177e4SLinus Torvalds * This avoids tunnel drivers and other mess and gives us the speed so 14661da177e4SLinus Torvalds * important for multicast video. 14671da177e4SLinus Torvalds */ 14681da177e4SLinus Torvalds 1469114c7844SAl Viro static void ip_encap(struct sk_buff *skb, __be32 saddr, __be32 daddr) 14701da177e4SLinus Torvalds { 14718856dfa3SArnaldo Carvalho de Melo struct iphdr *iph; 1472eddc9ec5SArnaldo Carvalho de Melo struct iphdr *old_iph = ip_hdr(skb); 14738856dfa3SArnaldo Carvalho de Melo 14748856dfa3SArnaldo Carvalho de Melo skb_push(skb, sizeof(struct iphdr)); 1475b0e380b1SArnaldo Carvalho de Melo skb->transport_header = skb->network_header; 14768856dfa3SArnaldo Carvalho de Melo skb_reset_network_header(skb); 1477eddc9ec5SArnaldo Carvalho de Melo iph = ip_hdr(skb); 14781da177e4SLinus Torvalds 14791da177e4SLinus Torvalds iph->version = 4; 1480e023dd64SArnaldo Carvalho de Melo iph->tos = old_iph->tos; 1481e023dd64SArnaldo Carvalho de Melo iph->ttl = old_iph->ttl; 14821da177e4SLinus Torvalds iph->frag_off = 0; 14831da177e4SLinus Torvalds iph->daddr = daddr; 14841da177e4SLinus Torvalds iph->saddr = saddr; 14851da177e4SLinus Torvalds iph->protocol = IPPROTO_IPIP; 14861da177e4SLinus Torvalds iph->ihl = 5; 14871da177e4SLinus Torvalds iph->tot_len = htons(skb->len); 1488adf30907SEric Dumazet ip_select_ident(iph, skb_dst(skb), NULL); 14891da177e4SLinus Torvalds ip_send_check(iph); 14901da177e4SLinus Torvalds 14911da177e4SLinus Torvalds memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt)); 14921da177e4SLinus Torvalds nf_reset(skb); 14931da177e4SLinus Torvalds } 14941da177e4SLinus Torvalds 14951da177e4SLinus Torvalds static inline int ipmr_forward_finish(struct sk_buff *skb) 14961da177e4SLinus Torvalds { 14971da177e4SLinus Torvalds struct ip_options * opt = &(IPCB(skb)->opt); 14981da177e4SLinus Torvalds 1499adf30907SEric Dumazet IP_INC_STATS_BH(dev_net(skb_dst(skb)->dev), IPSTATS_MIB_OUTFORWDATAGRAMS); 15001da177e4SLinus Torvalds 15011da177e4SLinus Torvalds if (unlikely(opt->optlen)) 15021da177e4SLinus Torvalds ip_forward_options(skb); 15031da177e4SLinus Torvalds 15041da177e4SLinus Torvalds return dst_output(skb); 15051da177e4SLinus Torvalds } 15061da177e4SLinus Torvalds 15071da177e4SLinus Torvalds /* 15081da177e4SLinus Torvalds * Processing handlers for ipmr_forward 15091da177e4SLinus Torvalds */ 15101da177e4SLinus Torvalds 15110c12295aSPatrick McHardy static void ipmr_queue_xmit(struct net *net, struct mr_table *mrt, 15120c12295aSPatrick McHardy struct sk_buff *skb, struct mfc_cache *c, int vifi) 15131da177e4SLinus Torvalds { 1514eddc9ec5SArnaldo Carvalho de Melo const struct iphdr *iph = ip_hdr(skb); 15150c12295aSPatrick McHardy struct vif_device *vif = &mrt->vif_table[vifi]; 15161da177e4SLinus Torvalds struct net_device *dev; 15171da177e4SLinus Torvalds struct rtable *rt; 15181da177e4SLinus Torvalds int encap = 0; 15191da177e4SLinus Torvalds 15201da177e4SLinus Torvalds if (vif->dev == NULL) 15211da177e4SLinus Torvalds goto out_free; 15221da177e4SLinus Torvalds 15231da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM 15241da177e4SLinus Torvalds if (vif->flags & VIFF_REGISTER) { 15251da177e4SLinus Torvalds vif->pkt_out++; 15261da177e4SLinus Torvalds vif->bytes_out += skb->len; 1527cf3677aeSPavel Emelyanov vif->dev->stats.tx_bytes += skb->len; 1528cf3677aeSPavel Emelyanov vif->dev->stats.tx_packets++; 15290c12295aSPatrick McHardy ipmr_cache_report(mrt, skb, vifi, IGMPMSG_WHOLEPKT); 153069ebbf58SIlpo Järvinen goto out_free; 15311da177e4SLinus Torvalds } 15321da177e4SLinus Torvalds #endif 15331da177e4SLinus Torvalds 15341da177e4SLinus Torvalds if (vif->flags&VIFF_TUNNEL) { 15351da177e4SLinus Torvalds struct flowi fl = { .oif = vif->link, 15361da177e4SLinus Torvalds .nl_u = { .ip4_u = 15371da177e4SLinus Torvalds { .daddr = vif->remote, 15381da177e4SLinus Torvalds .saddr = vif->local, 15391da177e4SLinus Torvalds .tos = RT_TOS(iph->tos) } }, 15401da177e4SLinus Torvalds .proto = IPPROTO_IPIP }; 15414feb88e5SBenjamin Thery if (ip_route_output_key(net, &rt, &fl)) 15421da177e4SLinus Torvalds goto out_free; 15431da177e4SLinus Torvalds encap = sizeof(struct iphdr); 15441da177e4SLinus Torvalds } else { 15451da177e4SLinus Torvalds struct flowi fl = { .oif = vif->link, 15461da177e4SLinus Torvalds .nl_u = { .ip4_u = 15471da177e4SLinus Torvalds { .daddr = iph->daddr, 15481da177e4SLinus Torvalds .tos = RT_TOS(iph->tos) } }, 15491da177e4SLinus Torvalds .proto = IPPROTO_IPIP }; 15504feb88e5SBenjamin Thery if (ip_route_output_key(net, &rt, &fl)) 15511da177e4SLinus Torvalds goto out_free; 15521da177e4SLinus Torvalds } 15531da177e4SLinus Torvalds 15541da177e4SLinus Torvalds dev = rt->u.dst.dev; 15551da177e4SLinus Torvalds 15561da177e4SLinus Torvalds if (skb->len+encap > dst_mtu(&rt->u.dst) && (ntohs(iph->frag_off) & IP_DF)) { 15571da177e4SLinus Torvalds /* Do not fragment multicasts. Alas, IPv4 does not 15581da177e4SLinus Torvalds allow to send ICMP, so that packets will disappear 15591da177e4SLinus Torvalds to blackhole. 15601da177e4SLinus Torvalds */ 15611da177e4SLinus Torvalds 15627c73a6faSPavel Emelyanov IP_INC_STATS_BH(dev_net(dev), IPSTATS_MIB_FRAGFAILS); 15631da177e4SLinus Torvalds ip_rt_put(rt); 15641da177e4SLinus Torvalds goto out_free; 15651da177e4SLinus Torvalds } 15661da177e4SLinus Torvalds 15671da177e4SLinus Torvalds encap += LL_RESERVED_SPACE(dev) + rt->u.dst.header_len; 15681da177e4SLinus Torvalds 15691da177e4SLinus Torvalds if (skb_cow(skb, encap)) { 15701da177e4SLinus Torvalds ip_rt_put(rt); 15711da177e4SLinus Torvalds goto out_free; 15721da177e4SLinus Torvalds } 15731da177e4SLinus Torvalds 15741da177e4SLinus Torvalds vif->pkt_out++; 15751da177e4SLinus Torvalds vif->bytes_out += skb->len; 15761da177e4SLinus Torvalds 1577adf30907SEric Dumazet skb_dst_drop(skb); 1578adf30907SEric Dumazet skb_dst_set(skb, &rt->u.dst); 1579eddc9ec5SArnaldo Carvalho de Melo ip_decrease_ttl(ip_hdr(skb)); 15801da177e4SLinus Torvalds 15811da177e4SLinus Torvalds /* FIXME: forward and output firewalls used to be called here. 15821da177e4SLinus Torvalds * What do we do with netfilter? -- RR */ 15831da177e4SLinus Torvalds if (vif->flags & VIFF_TUNNEL) { 15841da177e4SLinus Torvalds ip_encap(skb, vif->local, vif->remote); 15851da177e4SLinus Torvalds /* FIXME: extra output firewall step used to be here. --RR */ 15862f4c02d4SPavel Emelyanov vif->dev->stats.tx_packets++; 15872f4c02d4SPavel Emelyanov vif->dev->stats.tx_bytes += skb->len; 15881da177e4SLinus Torvalds } 15891da177e4SLinus Torvalds 15901da177e4SLinus Torvalds IPCB(skb)->flags |= IPSKB_FORWARDED; 15911da177e4SLinus Torvalds 15921da177e4SLinus Torvalds /* 15931da177e4SLinus Torvalds * RFC1584 teaches, that DVMRP/PIM router must deliver packets locally 15941da177e4SLinus Torvalds * not only before forwarding, but after forwarding on all output 15951da177e4SLinus Torvalds * interfaces. It is clear, if mrouter runs a multicasting 15961da177e4SLinus Torvalds * program, it should receive packets not depending to what interface 15971da177e4SLinus Torvalds * program is joined. 15981da177e4SLinus Torvalds * If we will not make it, the program will have to join on all 15991da177e4SLinus Torvalds * interfaces. On the other hand, multihoming host (or router, but 16001da177e4SLinus Torvalds * not mrouter) cannot join to more than one interface - it will 16011da177e4SLinus Torvalds * result in receiving multiple packets. 16021da177e4SLinus Torvalds */ 16039bbc768aSJan Engelhardt NF_HOOK(NFPROTO_IPV4, NF_INET_FORWARD, skb, skb->dev, dev, 16041da177e4SLinus Torvalds ipmr_forward_finish); 16051da177e4SLinus Torvalds return; 16061da177e4SLinus Torvalds 16071da177e4SLinus Torvalds out_free: 16081da177e4SLinus Torvalds kfree_skb(skb); 16091da177e4SLinus Torvalds } 16101da177e4SLinus Torvalds 16110c12295aSPatrick McHardy static int ipmr_find_vif(struct mr_table *mrt, struct net_device *dev) 16121da177e4SLinus Torvalds { 16131da177e4SLinus Torvalds int ct; 16140c12295aSPatrick McHardy 16150c12295aSPatrick McHardy for (ct = mrt->maxvif-1; ct >= 0; ct--) { 16160c12295aSPatrick McHardy if (mrt->vif_table[ct].dev == dev) 16171da177e4SLinus Torvalds break; 16181da177e4SLinus Torvalds } 16191da177e4SLinus Torvalds return ct; 16201da177e4SLinus Torvalds } 16211da177e4SLinus Torvalds 16221da177e4SLinus Torvalds /* "local" means that we should preserve one skb (for local delivery) */ 16231da177e4SLinus Torvalds 16240c12295aSPatrick McHardy static int ip_mr_forward(struct net *net, struct mr_table *mrt, 16250c12295aSPatrick McHardy struct sk_buff *skb, struct mfc_cache *cache, 16260c12295aSPatrick McHardy int local) 16271da177e4SLinus Torvalds { 16281da177e4SLinus Torvalds int psend = -1; 16291da177e4SLinus Torvalds int vif, ct; 16301da177e4SLinus Torvalds 16311da177e4SLinus Torvalds vif = cache->mfc_parent; 16321da177e4SLinus Torvalds cache->mfc_un.res.pkt++; 16331da177e4SLinus Torvalds cache->mfc_un.res.bytes += skb->len; 16341da177e4SLinus Torvalds 16351da177e4SLinus Torvalds /* 16361da177e4SLinus Torvalds * Wrong interface: drop packet and (maybe) send PIM assert. 16371da177e4SLinus Torvalds */ 16380c12295aSPatrick McHardy if (mrt->vif_table[vif].dev != skb->dev) { 16391da177e4SLinus Torvalds int true_vifi; 16401da177e4SLinus Torvalds 1641511c3f92SEric Dumazet if (skb_rtable(skb)->fl.iif == 0) { 16421da177e4SLinus Torvalds /* It is our own packet, looped back. 16431da177e4SLinus Torvalds Very complicated situation... 16441da177e4SLinus Torvalds 16451da177e4SLinus Torvalds The best workaround until routing daemons will be 16461da177e4SLinus Torvalds fixed is not to redistribute packet, if it was 16471da177e4SLinus Torvalds send through wrong interface. It means, that 16481da177e4SLinus Torvalds multicast applications WILL NOT work for 16491da177e4SLinus Torvalds (S,G), which have default multicast route pointing 16501da177e4SLinus Torvalds to wrong oif. In any case, it is not a good 16511da177e4SLinus Torvalds idea to use multicasting applications on router. 16521da177e4SLinus Torvalds */ 16531da177e4SLinus Torvalds goto dont_forward; 16541da177e4SLinus Torvalds } 16551da177e4SLinus Torvalds 16561da177e4SLinus Torvalds cache->mfc_un.res.wrong_if++; 16570c12295aSPatrick McHardy true_vifi = ipmr_find_vif(mrt, skb->dev); 16581da177e4SLinus Torvalds 16590c12295aSPatrick McHardy if (true_vifi >= 0 && mrt->mroute_do_assert && 16601da177e4SLinus Torvalds /* pimsm uses asserts, when switching from RPT to SPT, 16611da177e4SLinus Torvalds so that we cannot check that packet arrived on an oif. 16621da177e4SLinus Torvalds It is bad, but otherwise we would need to move pretty 16631da177e4SLinus Torvalds large chunk of pimd to kernel. Ough... --ANK 16641da177e4SLinus Torvalds */ 16650c12295aSPatrick McHardy (mrt->mroute_do_pim || 16666f9374a9SBenjamin Thery cache->mfc_un.res.ttls[true_vifi] < 255) && 16671da177e4SLinus Torvalds time_after(jiffies, 16681da177e4SLinus Torvalds cache->mfc_un.res.last_assert + MFC_ASSERT_THRESH)) { 16691da177e4SLinus Torvalds cache->mfc_un.res.last_assert = jiffies; 16700c12295aSPatrick McHardy ipmr_cache_report(mrt, skb, true_vifi, IGMPMSG_WRONGVIF); 16711da177e4SLinus Torvalds } 16721da177e4SLinus Torvalds goto dont_forward; 16731da177e4SLinus Torvalds } 16741da177e4SLinus Torvalds 16750c12295aSPatrick McHardy mrt->vif_table[vif].pkt_in++; 16760c12295aSPatrick McHardy mrt->vif_table[vif].bytes_in += skb->len; 16771da177e4SLinus Torvalds 16781da177e4SLinus Torvalds /* 16791da177e4SLinus Torvalds * Forward the frame 16801da177e4SLinus Torvalds */ 16811da177e4SLinus Torvalds for (ct = cache->mfc_un.res.maxvif-1; ct >= cache->mfc_un.res.minvif; ct--) { 1682eddc9ec5SArnaldo Carvalho de Melo if (ip_hdr(skb)->ttl > cache->mfc_un.res.ttls[ct]) { 16831da177e4SLinus Torvalds if (psend != -1) { 16841da177e4SLinus Torvalds struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC); 16851da177e4SLinus Torvalds if (skb2) 16860c12295aSPatrick McHardy ipmr_queue_xmit(net, mrt, skb2, cache, 16870c12295aSPatrick McHardy psend); 16881da177e4SLinus Torvalds } 16891da177e4SLinus Torvalds psend = ct; 16901da177e4SLinus Torvalds } 16911da177e4SLinus Torvalds } 16921da177e4SLinus Torvalds if (psend != -1) { 16931da177e4SLinus Torvalds if (local) { 16941da177e4SLinus Torvalds struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC); 16951da177e4SLinus Torvalds if (skb2) 16960c12295aSPatrick McHardy ipmr_queue_xmit(net, mrt, skb2, cache, psend); 16971da177e4SLinus Torvalds } else { 16980c12295aSPatrick McHardy ipmr_queue_xmit(net, mrt, skb, cache, psend); 16991da177e4SLinus Torvalds return 0; 17001da177e4SLinus Torvalds } 17011da177e4SLinus Torvalds } 17021da177e4SLinus Torvalds 17031da177e4SLinus Torvalds dont_forward: 17041da177e4SLinus Torvalds if (!local) 17051da177e4SLinus Torvalds kfree_skb(skb); 17061da177e4SLinus Torvalds return 0; 17071da177e4SLinus Torvalds } 17081da177e4SLinus Torvalds 17091da177e4SLinus Torvalds 17101da177e4SLinus Torvalds /* 17111da177e4SLinus Torvalds * Multicast packets for forwarding arrive here 17121da177e4SLinus Torvalds */ 17131da177e4SLinus Torvalds 17141da177e4SLinus Torvalds int ip_mr_input(struct sk_buff *skb) 17151da177e4SLinus Torvalds { 17161da177e4SLinus Torvalds struct mfc_cache *cache; 17174feb88e5SBenjamin Thery struct net *net = dev_net(skb->dev); 1718511c3f92SEric Dumazet int local = skb_rtable(skb)->rt_flags & RTCF_LOCAL; 1719f0ad0860SPatrick McHardy struct mr_table *mrt; 1720f0ad0860SPatrick McHardy int err; 17211da177e4SLinus Torvalds 17221da177e4SLinus Torvalds /* Packet is looped back after forward, it should not be 17231da177e4SLinus Torvalds forwarded second time, but still can be delivered locally. 17241da177e4SLinus Torvalds */ 17251da177e4SLinus Torvalds if (IPCB(skb)->flags&IPSKB_FORWARDED) 17261da177e4SLinus Torvalds goto dont_forward; 17271da177e4SLinus Torvalds 1728f0ad0860SPatrick McHardy err = ipmr_fib_lookup(net, &skb_rtable(skb)->fl, &mrt); 1729f0ad0860SPatrick McHardy if (err < 0) 1730f0ad0860SPatrick McHardy return err; 1731f0ad0860SPatrick McHardy 17321da177e4SLinus Torvalds if (!local) { 17331da177e4SLinus Torvalds if (IPCB(skb)->opt.router_alert) { 17341da177e4SLinus Torvalds if (ip_call_ra_chain(skb)) 17351da177e4SLinus Torvalds return 0; 1736eddc9ec5SArnaldo Carvalho de Melo } else if (ip_hdr(skb)->protocol == IPPROTO_IGMP){ 17371da177e4SLinus Torvalds /* IGMPv1 (and broken IGMPv2 implementations sort of 17381da177e4SLinus Torvalds Cisco IOS <= 11.2(8)) do not put router alert 17391da177e4SLinus Torvalds option to IGMP packets destined to routable 17401da177e4SLinus Torvalds groups. It is very bad, because it means 17411da177e4SLinus Torvalds that we can forward NO IGMP messages. 17421da177e4SLinus Torvalds */ 17431da177e4SLinus Torvalds read_lock(&mrt_lock); 17440c12295aSPatrick McHardy if (mrt->mroute_sk) { 17452715bcf9SPatrick McHardy nf_reset(skb); 17460c12295aSPatrick McHardy raw_rcv(mrt->mroute_sk, skb); 17471da177e4SLinus Torvalds read_unlock(&mrt_lock); 17481da177e4SLinus Torvalds return 0; 17491da177e4SLinus Torvalds } 17501da177e4SLinus Torvalds read_unlock(&mrt_lock); 17511da177e4SLinus Torvalds } 17521da177e4SLinus Torvalds } 17531da177e4SLinus Torvalds 17541da177e4SLinus Torvalds read_lock(&mrt_lock); 17550c12295aSPatrick McHardy cache = ipmr_cache_find(mrt, ip_hdr(skb)->saddr, ip_hdr(skb)->daddr); 17561da177e4SLinus Torvalds 17571da177e4SLinus Torvalds /* 17581da177e4SLinus Torvalds * No usable cache entry 17591da177e4SLinus Torvalds */ 17601da177e4SLinus Torvalds if (cache == NULL) { 17611da177e4SLinus Torvalds int vif; 17621da177e4SLinus Torvalds 17631da177e4SLinus Torvalds if (local) { 17641da177e4SLinus Torvalds struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC); 17651da177e4SLinus Torvalds ip_local_deliver(skb); 17661da177e4SLinus Torvalds if (skb2 == NULL) { 17671da177e4SLinus Torvalds read_unlock(&mrt_lock); 17681da177e4SLinus Torvalds return -ENOBUFS; 17691da177e4SLinus Torvalds } 17701da177e4SLinus Torvalds skb = skb2; 17711da177e4SLinus Torvalds } 17721da177e4SLinus Torvalds 17730c12295aSPatrick McHardy vif = ipmr_find_vif(mrt, skb->dev); 17741da177e4SLinus Torvalds if (vif >= 0) { 17750eae88f3SEric Dumazet int err2 = ipmr_cache_unresolved(mrt, vif, skb); 17761da177e4SLinus Torvalds read_unlock(&mrt_lock); 17771da177e4SLinus Torvalds 17780eae88f3SEric Dumazet return err2; 17791da177e4SLinus Torvalds } 17801da177e4SLinus Torvalds read_unlock(&mrt_lock); 17811da177e4SLinus Torvalds kfree_skb(skb); 17821da177e4SLinus Torvalds return -ENODEV; 17831da177e4SLinus Torvalds } 17841da177e4SLinus Torvalds 17850c12295aSPatrick McHardy ip_mr_forward(net, mrt, skb, cache, local); 17861da177e4SLinus Torvalds 17871da177e4SLinus Torvalds read_unlock(&mrt_lock); 17881da177e4SLinus Torvalds 17891da177e4SLinus Torvalds if (local) 17901da177e4SLinus Torvalds return ip_local_deliver(skb); 17911da177e4SLinus Torvalds 17921da177e4SLinus Torvalds return 0; 17931da177e4SLinus Torvalds 17941da177e4SLinus Torvalds dont_forward: 17951da177e4SLinus Torvalds if (local) 17961da177e4SLinus Torvalds return ip_local_deliver(skb); 17971da177e4SLinus Torvalds kfree_skb(skb); 17981da177e4SLinus Torvalds return 0; 17991da177e4SLinus Torvalds } 18001da177e4SLinus Torvalds 1801b1879204SIlpo Järvinen #ifdef CONFIG_IP_PIMSM 1802f0ad0860SPatrick McHardy static int __pim_rcv(struct mr_table *mrt, struct sk_buff *skb, 1803f0ad0860SPatrick McHardy unsigned int pimlen) 18041da177e4SLinus Torvalds { 18051da177e4SLinus Torvalds struct net_device *reg_dev = NULL; 1806b1879204SIlpo Järvinen struct iphdr *encap; 18071da177e4SLinus Torvalds 1808b1879204SIlpo Järvinen encap = (struct iphdr *)(skb_transport_header(skb) + pimlen); 18091da177e4SLinus Torvalds /* 18101da177e4SLinus Torvalds Check that: 18111da177e4SLinus Torvalds a. packet is really destinted to a multicast group 18121da177e4SLinus Torvalds b. packet is not a NULL-REGISTER 18131da177e4SLinus Torvalds c. packet is not truncated 18141da177e4SLinus Torvalds */ 1815f97c1e0cSJoe Perches if (!ipv4_is_multicast(encap->daddr) || 18161da177e4SLinus Torvalds encap->tot_len == 0 || 1817b1879204SIlpo Järvinen ntohs(encap->tot_len) + pimlen > skb->len) 1818b1879204SIlpo Järvinen return 1; 18191da177e4SLinus Torvalds 18201da177e4SLinus Torvalds read_lock(&mrt_lock); 18210c12295aSPatrick McHardy if (mrt->mroute_reg_vif_num >= 0) 18220c12295aSPatrick McHardy reg_dev = mrt->vif_table[mrt->mroute_reg_vif_num].dev; 18231da177e4SLinus Torvalds if (reg_dev) 18241da177e4SLinus Torvalds dev_hold(reg_dev); 18251da177e4SLinus Torvalds read_unlock(&mrt_lock); 18261da177e4SLinus Torvalds 18271da177e4SLinus Torvalds if (reg_dev == NULL) 1828b1879204SIlpo Järvinen return 1; 18291da177e4SLinus Torvalds 1830b0e380b1SArnaldo Carvalho de Melo skb->mac_header = skb->network_header; 18311da177e4SLinus Torvalds skb_pull(skb, (u8*)encap - skb->data); 183231c7711bSArnaldo Carvalho de Melo skb_reset_network_header(skb); 18331da177e4SLinus Torvalds skb->protocol = htons(ETH_P_IP); 18341da177e4SLinus Torvalds skb->ip_summed = 0; 18351da177e4SLinus Torvalds skb->pkt_type = PACKET_HOST; 1836d19d56ddSEric Dumazet 1837d19d56ddSEric Dumazet skb_tunnel_rx(skb, reg_dev); 1838d19d56ddSEric Dumazet 18391da177e4SLinus Torvalds netif_rx(skb); 18401da177e4SLinus Torvalds dev_put(reg_dev); 1841b1879204SIlpo Järvinen 18421da177e4SLinus Torvalds return 0; 1843b1879204SIlpo Järvinen } 1844b1879204SIlpo Järvinen #endif 1845b1879204SIlpo Järvinen 1846b1879204SIlpo Järvinen #ifdef CONFIG_IP_PIMSM_V1 1847b1879204SIlpo Järvinen /* 1848b1879204SIlpo Järvinen * Handle IGMP messages of PIMv1 1849b1879204SIlpo Järvinen */ 1850b1879204SIlpo Järvinen 1851b1879204SIlpo Järvinen int pim_rcv_v1(struct sk_buff * skb) 1852b1879204SIlpo Järvinen { 1853b1879204SIlpo Järvinen struct igmphdr *pim; 18544feb88e5SBenjamin Thery struct net *net = dev_net(skb->dev); 1855f0ad0860SPatrick McHardy struct mr_table *mrt; 1856b1879204SIlpo Järvinen 1857b1879204SIlpo Järvinen if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr))) 1858b1879204SIlpo Järvinen goto drop; 1859b1879204SIlpo Järvinen 1860b1879204SIlpo Järvinen pim = igmp_hdr(skb); 1861b1879204SIlpo Järvinen 1862f0ad0860SPatrick McHardy if (ipmr_fib_lookup(net, &skb_rtable(skb)->fl, &mrt) < 0) 1863f0ad0860SPatrick McHardy goto drop; 1864f0ad0860SPatrick McHardy 18650c12295aSPatrick McHardy if (!mrt->mroute_do_pim || 1866b1879204SIlpo Järvinen pim->group != PIM_V1_VERSION || pim->code != PIM_V1_REGISTER) 1867b1879204SIlpo Järvinen goto drop; 1868b1879204SIlpo Järvinen 1869f0ad0860SPatrick McHardy if (__pim_rcv(mrt, skb, sizeof(*pim))) { 18701da177e4SLinus Torvalds drop: 18711da177e4SLinus Torvalds kfree_skb(skb); 1872b1879204SIlpo Järvinen } 18731da177e4SLinus Torvalds return 0; 18741da177e4SLinus Torvalds } 18751da177e4SLinus Torvalds #endif 18761da177e4SLinus Torvalds 18771da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM_V2 18781da177e4SLinus Torvalds static int pim_rcv(struct sk_buff * skb) 18791da177e4SLinus Torvalds { 18801da177e4SLinus Torvalds struct pimreghdr *pim; 1881f0ad0860SPatrick McHardy struct net *net = dev_net(skb->dev); 1882f0ad0860SPatrick McHardy struct mr_table *mrt; 18831da177e4SLinus Torvalds 1884b1879204SIlpo Järvinen if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr))) 18851da177e4SLinus Torvalds goto drop; 18861da177e4SLinus Torvalds 18879c70220bSArnaldo Carvalho de Melo pim = (struct pimreghdr *)skb_transport_header(skb); 18881da177e4SLinus Torvalds if (pim->type != ((PIM_VERSION<<4)|(PIM_REGISTER)) || 18891da177e4SLinus Torvalds (pim->flags&PIM_NULL_REGISTER) || 18901da177e4SLinus Torvalds (ip_compute_csum((void *)pim, sizeof(*pim)) != 0 && 1891d3bc23e7SAl Viro csum_fold(skb_checksum(skb, 0, skb->len, 0)))) 18921da177e4SLinus Torvalds goto drop; 18931da177e4SLinus Torvalds 1894f0ad0860SPatrick McHardy if (ipmr_fib_lookup(net, &skb_rtable(skb)->fl, &mrt) < 0) 1895f0ad0860SPatrick McHardy goto drop; 1896f0ad0860SPatrick McHardy 1897f0ad0860SPatrick McHardy if (__pim_rcv(mrt, skb, sizeof(*pim))) { 18981da177e4SLinus Torvalds drop: 18991da177e4SLinus Torvalds kfree_skb(skb); 1900b1879204SIlpo Järvinen } 19011da177e4SLinus Torvalds return 0; 19021da177e4SLinus Torvalds } 19031da177e4SLinus Torvalds #endif 19041da177e4SLinus Torvalds 1905cb6a4e46SPatrick McHardy static int __ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb, 1906cb6a4e46SPatrick McHardy struct mfc_cache *c, struct rtmsg *rtm) 19071da177e4SLinus Torvalds { 19081da177e4SLinus Torvalds int ct; 19091da177e4SLinus Torvalds struct rtnexthop *nhp; 191027a884dcSArnaldo Carvalho de Melo u8 *b = skb_tail_pointer(skb); 19111da177e4SLinus Torvalds struct rtattr *mp_head; 19121da177e4SLinus Torvalds 19137438189bSNicolas Dichtel /* If cache is unresolved, don't try to parse IIF and OIF */ 1914*ed0f160aSDan Carpenter if (c->mfc_parent >= MAXVIFS) 19157438189bSNicolas Dichtel return -ENOENT; 19167438189bSNicolas Dichtel 19170c12295aSPatrick McHardy if (VIF_EXISTS(mrt, c->mfc_parent)) 19180c12295aSPatrick McHardy RTA_PUT(skb, RTA_IIF, 4, &mrt->vif_table[c->mfc_parent].dev->ifindex); 19191da177e4SLinus Torvalds 19201da177e4SLinus Torvalds mp_head = (struct rtattr *)skb_put(skb, RTA_LENGTH(0)); 19211da177e4SLinus Torvalds 19221da177e4SLinus Torvalds for (ct = c->mfc_un.res.minvif; ct < c->mfc_un.res.maxvif; ct++) { 19230c12295aSPatrick McHardy if (VIF_EXISTS(mrt, ct) && c->mfc_un.res.ttls[ct] < 255) { 19241da177e4SLinus Torvalds if (skb_tailroom(skb) < RTA_ALIGN(RTA_ALIGN(sizeof(*nhp)) + 4)) 19251da177e4SLinus Torvalds goto rtattr_failure; 19261da177e4SLinus Torvalds nhp = (struct rtnexthop *)skb_put(skb, RTA_ALIGN(sizeof(*nhp))); 19271da177e4SLinus Torvalds nhp->rtnh_flags = 0; 19281da177e4SLinus Torvalds nhp->rtnh_hops = c->mfc_un.res.ttls[ct]; 19290c12295aSPatrick McHardy nhp->rtnh_ifindex = mrt->vif_table[ct].dev->ifindex; 19301da177e4SLinus Torvalds nhp->rtnh_len = sizeof(*nhp); 19311da177e4SLinus Torvalds } 19321da177e4SLinus Torvalds } 19331da177e4SLinus Torvalds mp_head->rta_type = RTA_MULTIPATH; 193427a884dcSArnaldo Carvalho de Melo mp_head->rta_len = skb_tail_pointer(skb) - (u8 *)mp_head; 19351da177e4SLinus Torvalds rtm->rtm_type = RTN_MULTICAST; 19361da177e4SLinus Torvalds return 1; 19371da177e4SLinus Torvalds 19381da177e4SLinus Torvalds rtattr_failure: 1939dc5fc579SArnaldo Carvalho de Melo nlmsg_trim(skb, b); 19401da177e4SLinus Torvalds return -EMSGSIZE; 19411da177e4SLinus Torvalds } 19421da177e4SLinus Torvalds 19434feb88e5SBenjamin Thery int ipmr_get_route(struct net *net, 19444feb88e5SBenjamin Thery struct sk_buff *skb, struct rtmsg *rtm, int nowait) 19451da177e4SLinus Torvalds { 19461da177e4SLinus Torvalds int err; 1947f0ad0860SPatrick McHardy struct mr_table *mrt; 19481da177e4SLinus Torvalds struct mfc_cache *cache; 1949511c3f92SEric Dumazet struct rtable *rt = skb_rtable(skb); 19501da177e4SLinus Torvalds 1951f0ad0860SPatrick McHardy mrt = ipmr_get_table(net, RT_TABLE_DEFAULT); 1952f0ad0860SPatrick McHardy if (mrt == NULL) 1953f0ad0860SPatrick McHardy return -ENOENT; 1954f0ad0860SPatrick McHardy 19551da177e4SLinus Torvalds read_lock(&mrt_lock); 19560c12295aSPatrick McHardy cache = ipmr_cache_find(mrt, rt->rt_src, rt->rt_dst); 19571da177e4SLinus Torvalds 19581da177e4SLinus Torvalds if (cache == NULL) { 195972287490SAlexey Kuznetsov struct sk_buff *skb2; 1960eddc9ec5SArnaldo Carvalho de Melo struct iphdr *iph; 19611da177e4SLinus Torvalds struct net_device *dev; 19621da177e4SLinus Torvalds int vif; 19631da177e4SLinus Torvalds 19641da177e4SLinus Torvalds if (nowait) { 19651da177e4SLinus Torvalds read_unlock(&mrt_lock); 19661da177e4SLinus Torvalds return -EAGAIN; 19671da177e4SLinus Torvalds } 19681da177e4SLinus Torvalds 19691da177e4SLinus Torvalds dev = skb->dev; 19700c12295aSPatrick McHardy if (dev == NULL || (vif = ipmr_find_vif(mrt, dev)) < 0) { 19711da177e4SLinus Torvalds read_unlock(&mrt_lock); 19721da177e4SLinus Torvalds return -ENODEV; 19731da177e4SLinus Torvalds } 197472287490SAlexey Kuznetsov skb2 = skb_clone(skb, GFP_ATOMIC); 197572287490SAlexey Kuznetsov if (!skb2) { 197672287490SAlexey Kuznetsov read_unlock(&mrt_lock); 197772287490SAlexey Kuznetsov return -ENOMEM; 197872287490SAlexey Kuznetsov } 197972287490SAlexey Kuznetsov 1980e2d1bca7SArnaldo Carvalho de Melo skb_push(skb2, sizeof(struct iphdr)); 1981e2d1bca7SArnaldo Carvalho de Melo skb_reset_network_header(skb2); 1982eddc9ec5SArnaldo Carvalho de Melo iph = ip_hdr(skb2); 1983eddc9ec5SArnaldo Carvalho de Melo iph->ihl = sizeof(struct iphdr) >> 2; 1984eddc9ec5SArnaldo Carvalho de Melo iph->saddr = rt->rt_src; 1985eddc9ec5SArnaldo Carvalho de Melo iph->daddr = rt->rt_dst; 1986eddc9ec5SArnaldo Carvalho de Melo iph->version = 0; 19870c12295aSPatrick McHardy err = ipmr_cache_unresolved(mrt, vif, skb2); 19881da177e4SLinus Torvalds read_unlock(&mrt_lock); 19891da177e4SLinus Torvalds return err; 19901da177e4SLinus Torvalds } 19911da177e4SLinus Torvalds 19921da177e4SLinus Torvalds if (!nowait && (rtm->rtm_flags&RTM_F_NOTIFY)) 19931da177e4SLinus Torvalds cache->mfc_flags |= MFC_NOTIFY; 1994cb6a4e46SPatrick McHardy err = __ipmr_fill_mroute(mrt, skb, cache, rtm); 19951da177e4SLinus Torvalds read_unlock(&mrt_lock); 19961da177e4SLinus Torvalds return err; 19971da177e4SLinus Torvalds } 19981da177e4SLinus Torvalds 1999cb6a4e46SPatrick McHardy static int ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb, 2000cb6a4e46SPatrick McHardy u32 pid, u32 seq, struct mfc_cache *c) 2001cb6a4e46SPatrick McHardy { 2002cb6a4e46SPatrick McHardy struct nlmsghdr *nlh; 2003cb6a4e46SPatrick McHardy struct rtmsg *rtm; 2004cb6a4e46SPatrick McHardy 2005cb6a4e46SPatrick McHardy nlh = nlmsg_put(skb, pid, seq, RTM_NEWROUTE, sizeof(*rtm), NLM_F_MULTI); 2006cb6a4e46SPatrick McHardy if (nlh == NULL) 2007cb6a4e46SPatrick McHardy return -EMSGSIZE; 2008cb6a4e46SPatrick McHardy 2009cb6a4e46SPatrick McHardy rtm = nlmsg_data(nlh); 2010cb6a4e46SPatrick McHardy rtm->rtm_family = RTNL_FAMILY_IPMR; 2011cb6a4e46SPatrick McHardy rtm->rtm_dst_len = 32; 2012cb6a4e46SPatrick McHardy rtm->rtm_src_len = 32; 2013cb6a4e46SPatrick McHardy rtm->rtm_tos = 0; 2014cb6a4e46SPatrick McHardy rtm->rtm_table = mrt->id; 2015cb6a4e46SPatrick McHardy NLA_PUT_U32(skb, RTA_TABLE, mrt->id); 2016cb6a4e46SPatrick McHardy rtm->rtm_type = RTN_MULTICAST; 2017cb6a4e46SPatrick McHardy rtm->rtm_scope = RT_SCOPE_UNIVERSE; 2018cb6a4e46SPatrick McHardy rtm->rtm_protocol = RTPROT_UNSPEC; 2019cb6a4e46SPatrick McHardy rtm->rtm_flags = 0; 2020cb6a4e46SPatrick McHardy 2021cb6a4e46SPatrick McHardy NLA_PUT_BE32(skb, RTA_SRC, c->mfc_origin); 2022cb6a4e46SPatrick McHardy NLA_PUT_BE32(skb, RTA_DST, c->mfc_mcastgrp); 2023cb6a4e46SPatrick McHardy 2024cb6a4e46SPatrick McHardy if (__ipmr_fill_mroute(mrt, skb, c, rtm) < 0) 2025cb6a4e46SPatrick McHardy goto nla_put_failure; 2026cb6a4e46SPatrick McHardy 2027cb6a4e46SPatrick McHardy return nlmsg_end(skb, nlh); 2028cb6a4e46SPatrick McHardy 2029cb6a4e46SPatrick McHardy nla_put_failure: 2030cb6a4e46SPatrick McHardy nlmsg_cancel(skb, nlh); 2031cb6a4e46SPatrick McHardy return -EMSGSIZE; 2032cb6a4e46SPatrick McHardy } 2033cb6a4e46SPatrick McHardy 2034cb6a4e46SPatrick McHardy static int ipmr_rtm_dumproute(struct sk_buff *skb, struct netlink_callback *cb) 2035cb6a4e46SPatrick McHardy { 2036cb6a4e46SPatrick McHardy struct net *net = sock_net(skb->sk); 2037cb6a4e46SPatrick McHardy struct mr_table *mrt; 2038cb6a4e46SPatrick McHardy struct mfc_cache *mfc; 2039cb6a4e46SPatrick McHardy unsigned int t = 0, s_t; 2040cb6a4e46SPatrick McHardy unsigned int h = 0, s_h; 2041cb6a4e46SPatrick McHardy unsigned int e = 0, s_e; 2042cb6a4e46SPatrick McHardy 2043cb6a4e46SPatrick McHardy s_t = cb->args[0]; 2044cb6a4e46SPatrick McHardy s_h = cb->args[1]; 2045cb6a4e46SPatrick McHardy s_e = cb->args[2]; 2046cb6a4e46SPatrick McHardy 2047cb6a4e46SPatrick McHardy read_lock(&mrt_lock); 2048cb6a4e46SPatrick McHardy ipmr_for_each_table(mrt, net) { 2049cb6a4e46SPatrick McHardy if (t < s_t) 2050cb6a4e46SPatrick McHardy goto next_table; 2051cb6a4e46SPatrick McHardy if (t > s_t) 2052cb6a4e46SPatrick McHardy s_h = 0; 2053cb6a4e46SPatrick McHardy for (h = s_h; h < MFC_LINES; h++) { 2054cb6a4e46SPatrick McHardy list_for_each_entry(mfc, &mrt->mfc_cache_array[h], list) { 2055cb6a4e46SPatrick McHardy if (e < s_e) 2056cb6a4e46SPatrick McHardy goto next_entry; 2057cb6a4e46SPatrick McHardy if (ipmr_fill_mroute(mrt, skb, 2058cb6a4e46SPatrick McHardy NETLINK_CB(cb->skb).pid, 2059cb6a4e46SPatrick McHardy cb->nlh->nlmsg_seq, 2060cb6a4e46SPatrick McHardy mfc) < 0) 2061cb6a4e46SPatrick McHardy goto done; 2062cb6a4e46SPatrick McHardy next_entry: 2063cb6a4e46SPatrick McHardy e++; 2064cb6a4e46SPatrick McHardy } 2065cb6a4e46SPatrick McHardy e = s_e = 0; 2066cb6a4e46SPatrick McHardy } 2067cb6a4e46SPatrick McHardy s_h = 0; 2068cb6a4e46SPatrick McHardy next_table: 2069cb6a4e46SPatrick McHardy t++; 2070cb6a4e46SPatrick McHardy } 2071cb6a4e46SPatrick McHardy done: 2072cb6a4e46SPatrick McHardy read_unlock(&mrt_lock); 2073cb6a4e46SPatrick McHardy 2074cb6a4e46SPatrick McHardy cb->args[2] = e; 2075cb6a4e46SPatrick McHardy cb->args[1] = h; 2076cb6a4e46SPatrick McHardy cb->args[0] = t; 2077cb6a4e46SPatrick McHardy 2078cb6a4e46SPatrick McHardy return skb->len; 2079cb6a4e46SPatrick McHardy } 2080cb6a4e46SPatrick McHardy 20811da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 20821da177e4SLinus Torvalds /* 20831da177e4SLinus Torvalds * The /proc interfaces to multicast routing /proc/ip_mr_cache /proc/ip_mr_vif 20841da177e4SLinus Torvalds */ 20851da177e4SLinus Torvalds struct ipmr_vif_iter { 2086f6bb4514SBenjamin Thery struct seq_net_private p; 2087f0ad0860SPatrick McHardy struct mr_table *mrt; 20881da177e4SLinus Torvalds int ct; 20891da177e4SLinus Torvalds }; 20901da177e4SLinus Torvalds 2091f6bb4514SBenjamin Thery static struct vif_device *ipmr_vif_seq_idx(struct net *net, 2092f6bb4514SBenjamin Thery struct ipmr_vif_iter *iter, 20931da177e4SLinus Torvalds loff_t pos) 20941da177e4SLinus Torvalds { 2095f0ad0860SPatrick McHardy struct mr_table *mrt = iter->mrt; 20960c12295aSPatrick McHardy 20970c12295aSPatrick McHardy for (iter->ct = 0; iter->ct < mrt->maxvif; ++iter->ct) { 20980c12295aSPatrick McHardy if (!VIF_EXISTS(mrt, iter->ct)) 20991da177e4SLinus Torvalds continue; 21001da177e4SLinus Torvalds if (pos-- == 0) 21010c12295aSPatrick McHardy return &mrt->vif_table[iter->ct]; 21021da177e4SLinus Torvalds } 21031da177e4SLinus Torvalds return NULL; 21041da177e4SLinus Torvalds } 21051da177e4SLinus Torvalds 21061da177e4SLinus Torvalds static void *ipmr_vif_seq_start(struct seq_file *seq, loff_t *pos) 2107ba93ef74SStephen Hemminger __acquires(mrt_lock) 21081da177e4SLinus Torvalds { 2109f0ad0860SPatrick McHardy struct ipmr_vif_iter *iter = seq->private; 2110f6bb4514SBenjamin Thery struct net *net = seq_file_net(seq); 2111f0ad0860SPatrick McHardy struct mr_table *mrt; 2112f0ad0860SPatrick McHardy 2113f0ad0860SPatrick McHardy mrt = ipmr_get_table(net, RT_TABLE_DEFAULT); 2114f0ad0860SPatrick McHardy if (mrt == NULL) 2115f0ad0860SPatrick McHardy return ERR_PTR(-ENOENT); 2116f0ad0860SPatrick McHardy 2117f0ad0860SPatrick McHardy iter->mrt = mrt; 2118f6bb4514SBenjamin Thery 21191da177e4SLinus Torvalds read_lock(&mrt_lock); 2120f6bb4514SBenjamin Thery return *pos ? ipmr_vif_seq_idx(net, seq->private, *pos - 1) 21211da177e4SLinus Torvalds : SEQ_START_TOKEN; 21221da177e4SLinus Torvalds } 21231da177e4SLinus Torvalds 21241da177e4SLinus Torvalds static void *ipmr_vif_seq_next(struct seq_file *seq, void *v, loff_t *pos) 21251da177e4SLinus Torvalds { 21261da177e4SLinus Torvalds struct ipmr_vif_iter *iter = seq->private; 2127f6bb4514SBenjamin Thery struct net *net = seq_file_net(seq); 2128f0ad0860SPatrick McHardy struct mr_table *mrt = iter->mrt; 21291da177e4SLinus Torvalds 21301da177e4SLinus Torvalds ++*pos; 21311da177e4SLinus Torvalds if (v == SEQ_START_TOKEN) 2132f6bb4514SBenjamin Thery return ipmr_vif_seq_idx(net, iter, 0); 21331da177e4SLinus Torvalds 21340c12295aSPatrick McHardy while (++iter->ct < mrt->maxvif) { 21350c12295aSPatrick McHardy if (!VIF_EXISTS(mrt, iter->ct)) 21361da177e4SLinus Torvalds continue; 21370c12295aSPatrick McHardy return &mrt->vif_table[iter->ct]; 21381da177e4SLinus Torvalds } 21391da177e4SLinus Torvalds return NULL; 21401da177e4SLinus Torvalds } 21411da177e4SLinus Torvalds 21421da177e4SLinus Torvalds static void ipmr_vif_seq_stop(struct seq_file *seq, void *v) 2143ba93ef74SStephen Hemminger __releases(mrt_lock) 21441da177e4SLinus Torvalds { 21451da177e4SLinus Torvalds read_unlock(&mrt_lock); 21461da177e4SLinus Torvalds } 21471da177e4SLinus Torvalds 21481da177e4SLinus Torvalds static int ipmr_vif_seq_show(struct seq_file *seq, void *v) 21491da177e4SLinus Torvalds { 2150f0ad0860SPatrick McHardy struct ipmr_vif_iter *iter = seq->private; 2151f0ad0860SPatrick McHardy struct mr_table *mrt = iter->mrt; 2152f6bb4514SBenjamin Thery 21531da177e4SLinus Torvalds if (v == SEQ_START_TOKEN) { 21541da177e4SLinus Torvalds seq_puts(seq, 21551da177e4SLinus Torvalds "Interface BytesIn PktsIn BytesOut PktsOut Flags Local Remote\n"); 21561da177e4SLinus Torvalds } else { 21571da177e4SLinus Torvalds const struct vif_device *vif = v; 21581da177e4SLinus Torvalds const char *name = vif->dev ? vif->dev->name : "none"; 21591da177e4SLinus Torvalds 21601da177e4SLinus Torvalds seq_printf(seq, 21611da177e4SLinus Torvalds "%2Zd %-10s %8ld %7ld %8ld %7ld %05X %08X %08X\n", 21620c12295aSPatrick McHardy vif - mrt->vif_table, 21631da177e4SLinus Torvalds name, vif->bytes_in, vif->pkt_in, 21641da177e4SLinus Torvalds vif->bytes_out, vif->pkt_out, 21651da177e4SLinus Torvalds vif->flags, vif->local, vif->remote); 21661da177e4SLinus Torvalds } 21671da177e4SLinus Torvalds return 0; 21681da177e4SLinus Torvalds } 21691da177e4SLinus Torvalds 2170f690808eSStephen Hemminger static const struct seq_operations ipmr_vif_seq_ops = { 21711da177e4SLinus Torvalds .start = ipmr_vif_seq_start, 21721da177e4SLinus Torvalds .next = ipmr_vif_seq_next, 21731da177e4SLinus Torvalds .stop = ipmr_vif_seq_stop, 21741da177e4SLinus Torvalds .show = ipmr_vif_seq_show, 21751da177e4SLinus Torvalds }; 21761da177e4SLinus Torvalds 21771da177e4SLinus Torvalds static int ipmr_vif_open(struct inode *inode, struct file *file) 21781da177e4SLinus Torvalds { 2179f6bb4514SBenjamin Thery return seq_open_net(inode, file, &ipmr_vif_seq_ops, 2180cf7732e4SPavel Emelyanov sizeof(struct ipmr_vif_iter)); 21811da177e4SLinus Torvalds } 21821da177e4SLinus Torvalds 21839a32144eSArjan van de Ven static const struct file_operations ipmr_vif_fops = { 21841da177e4SLinus Torvalds .owner = THIS_MODULE, 21851da177e4SLinus Torvalds .open = ipmr_vif_open, 21861da177e4SLinus Torvalds .read = seq_read, 21871da177e4SLinus Torvalds .llseek = seq_lseek, 2188f6bb4514SBenjamin Thery .release = seq_release_net, 21891da177e4SLinus Torvalds }; 21901da177e4SLinus Torvalds 21911da177e4SLinus Torvalds struct ipmr_mfc_iter { 2192f6bb4514SBenjamin Thery struct seq_net_private p; 2193f0ad0860SPatrick McHardy struct mr_table *mrt; 2194862465f2SPatrick McHardy struct list_head *cache; 21951da177e4SLinus Torvalds int ct; 21961da177e4SLinus Torvalds }; 21971da177e4SLinus Torvalds 21981da177e4SLinus Torvalds 2199f6bb4514SBenjamin Thery static struct mfc_cache *ipmr_mfc_seq_idx(struct net *net, 2200f6bb4514SBenjamin Thery struct ipmr_mfc_iter *it, loff_t pos) 22011da177e4SLinus Torvalds { 2202f0ad0860SPatrick McHardy struct mr_table *mrt = it->mrt; 22031da177e4SLinus Torvalds struct mfc_cache *mfc; 22041da177e4SLinus Torvalds 22051da177e4SLinus Torvalds read_lock(&mrt_lock); 2206862465f2SPatrick McHardy for (it->ct = 0; it->ct < MFC_LINES; it->ct++) { 22070c12295aSPatrick McHardy it->cache = &mrt->mfc_cache_array[it->ct]; 2208862465f2SPatrick McHardy list_for_each_entry(mfc, it->cache, list) 22091da177e4SLinus Torvalds if (pos-- == 0) 22101da177e4SLinus Torvalds return mfc; 2211862465f2SPatrick McHardy } 22121da177e4SLinus Torvalds read_unlock(&mrt_lock); 22131da177e4SLinus Torvalds 22141da177e4SLinus Torvalds spin_lock_bh(&mfc_unres_lock); 22150c12295aSPatrick McHardy it->cache = &mrt->mfc_unres_queue; 2216862465f2SPatrick McHardy list_for_each_entry(mfc, it->cache, list) 2217e258beb2SPatrick McHardy if (pos-- == 0) 22181da177e4SLinus Torvalds return mfc; 22191da177e4SLinus Torvalds spin_unlock_bh(&mfc_unres_lock); 22201da177e4SLinus Torvalds 22211da177e4SLinus Torvalds it->cache = NULL; 22221da177e4SLinus Torvalds return NULL; 22231da177e4SLinus Torvalds } 22241da177e4SLinus Torvalds 22251da177e4SLinus Torvalds 22261da177e4SLinus Torvalds static void *ipmr_mfc_seq_start(struct seq_file *seq, loff_t *pos) 22271da177e4SLinus Torvalds { 22281da177e4SLinus Torvalds struct ipmr_mfc_iter *it = seq->private; 2229f6bb4514SBenjamin Thery struct net *net = seq_file_net(seq); 2230f0ad0860SPatrick McHardy struct mr_table *mrt; 2231f6bb4514SBenjamin Thery 2232f0ad0860SPatrick McHardy mrt = ipmr_get_table(net, RT_TABLE_DEFAULT); 2233f0ad0860SPatrick McHardy if (mrt == NULL) 2234f0ad0860SPatrick McHardy return ERR_PTR(-ENOENT); 2235f0ad0860SPatrick McHardy 2236f0ad0860SPatrick McHardy it->mrt = mrt; 22371da177e4SLinus Torvalds it->cache = NULL; 22381da177e4SLinus Torvalds it->ct = 0; 2239f6bb4514SBenjamin Thery return *pos ? ipmr_mfc_seq_idx(net, seq->private, *pos - 1) 22401da177e4SLinus Torvalds : SEQ_START_TOKEN; 22411da177e4SLinus Torvalds } 22421da177e4SLinus Torvalds 22431da177e4SLinus Torvalds static void *ipmr_mfc_seq_next(struct seq_file *seq, void *v, loff_t *pos) 22441da177e4SLinus Torvalds { 22451da177e4SLinus Torvalds struct mfc_cache *mfc = v; 22461da177e4SLinus Torvalds struct ipmr_mfc_iter *it = seq->private; 2247f6bb4514SBenjamin Thery struct net *net = seq_file_net(seq); 2248f0ad0860SPatrick McHardy struct mr_table *mrt = it->mrt; 22491da177e4SLinus Torvalds 22501da177e4SLinus Torvalds ++*pos; 22511da177e4SLinus Torvalds 22521da177e4SLinus Torvalds if (v == SEQ_START_TOKEN) 2253f6bb4514SBenjamin Thery return ipmr_mfc_seq_idx(net, seq->private, 0); 22541da177e4SLinus Torvalds 2255862465f2SPatrick McHardy if (mfc->list.next != it->cache) 2256862465f2SPatrick McHardy return list_entry(mfc->list.next, struct mfc_cache, list); 22571da177e4SLinus Torvalds 22580c12295aSPatrick McHardy if (it->cache == &mrt->mfc_unres_queue) 22591da177e4SLinus Torvalds goto end_of_list; 22601da177e4SLinus Torvalds 22610c12295aSPatrick McHardy BUG_ON(it->cache != &mrt->mfc_cache_array[it->ct]); 22621da177e4SLinus Torvalds 22631da177e4SLinus Torvalds while (++it->ct < MFC_LINES) { 22640c12295aSPatrick McHardy it->cache = &mrt->mfc_cache_array[it->ct]; 2265862465f2SPatrick McHardy if (list_empty(it->cache)) 2266862465f2SPatrick McHardy continue; 2267862465f2SPatrick McHardy return list_first_entry(it->cache, struct mfc_cache, list); 22681da177e4SLinus Torvalds } 22691da177e4SLinus Torvalds 22701da177e4SLinus Torvalds /* exhausted cache_array, show unresolved */ 22711da177e4SLinus Torvalds read_unlock(&mrt_lock); 22720c12295aSPatrick McHardy it->cache = &mrt->mfc_unres_queue; 22731da177e4SLinus Torvalds it->ct = 0; 22741da177e4SLinus Torvalds 22751da177e4SLinus Torvalds spin_lock_bh(&mfc_unres_lock); 2276862465f2SPatrick McHardy if (!list_empty(it->cache)) 2277862465f2SPatrick McHardy return list_first_entry(it->cache, struct mfc_cache, list); 22781da177e4SLinus Torvalds 22791da177e4SLinus Torvalds end_of_list: 22801da177e4SLinus Torvalds spin_unlock_bh(&mfc_unres_lock); 22811da177e4SLinus Torvalds it->cache = NULL; 22821da177e4SLinus Torvalds 22831da177e4SLinus Torvalds return NULL; 22841da177e4SLinus Torvalds } 22851da177e4SLinus Torvalds 22861da177e4SLinus Torvalds static void ipmr_mfc_seq_stop(struct seq_file *seq, void *v) 22871da177e4SLinus Torvalds { 22881da177e4SLinus Torvalds struct ipmr_mfc_iter *it = seq->private; 2289f0ad0860SPatrick McHardy struct mr_table *mrt = it->mrt; 22901da177e4SLinus Torvalds 22910c12295aSPatrick McHardy if (it->cache == &mrt->mfc_unres_queue) 22921da177e4SLinus Torvalds spin_unlock_bh(&mfc_unres_lock); 22930c12295aSPatrick McHardy else if (it->cache == &mrt->mfc_cache_array[it->ct]) 22941da177e4SLinus Torvalds read_unlock(&mrt_lock); 22951da177e4SLinus Torvalds } 22961da177e4SLinus Torvalds 22971da177e4SLinus Torvalds static int ipmr_mfc_seq_show(struct seq_file *seq, void *v) 22981da177e4SLinus Torvalds { 22991da177e4SLinus Torvalds int n; 23001da177e4SLinus Torvalds 23011da177e4SLinus Torvalds if (v == SEQ_START_TOKEN) { 23021da177e4SLinus Torvalds seq_puts(seq, 23031da177e4SLinus Torvalds "Group Origin Iif Pkts Bytes Wrong Oifs\n"); 23041da177e4SLinus Torvalds } else { 23051da177e4SLinus Torvalds const struct mfc_cache *mfc = v; 23061da177e4SLinus Torvalds const struct ipmr_mfc_iter *it = seq->private; 2307f0ad0860SPatrick McHardy const struct mr_table *mrt = it->mrt; 23081da177e4SLinus Torvalds 23090eae88f3SEric Dumazet seq_printf(seq, "%08X %08X %-3hd", 23100eae88f3SEric Dumazet (__force u32) mfc->mfc_mcastgrp, 23110eae88f3SEric Dumazet (__force u32) mfc->mfc_origin, 23121ea472e2SBenjamin Thery mfc->mfc_parent); 23131ea472e2SBenjamin Thery 23140c12295aSPatrick McHardy if (it->cache != &mrt->mfc_unres_queue) { 23151ea472e2SBenjamin Thery seq_printf(seq, " %8lu %8lu %8lu", 23161da177e4SLinus Torvalds mfc->mfc_un.res.pkt, 23171da177e4SLinus Torvalds mfc->mfc_un.res.bytes, 23181da177e4SLinus Torvalds mfc->mfc_un.res.wrong_if); 23191da177e4SLinus Torvalds for (n = mfc->mfc_un.res.minvif; 23201da177e4SLinus Torvalds n < mfc->mfc_un.res.maxvif; n++ ) { 23210c12295aSPatrick McHardy if (VIF_EXISTS(mrt, n) && 2322cf958ae3SBenjamin Thery mfc->mfc_un.res.ttls[n] < 255) 23231da177e4SLinus Torvalds seq_printf(seq, 23241da177e4SLinus Torvalds " %2d:%-3d", 23251da177e4SLinus Torvalds n, mfc->mfc_un.res.ttls[n]); 23261da177e4SLinus Torvalds } 23271ea472e2SBenjamin Thery } else { 23281ea472e2SBenjamin Thery /* unresolved mfc_caches don't contain 23291ea472e2SBenjamin Thery * pkt, bytes and wrong_if values 23301ea472e2SBenjamin Thery */ 23311ea472e2SBenjamin Thery seq_printf(seq, " %8lu %8lu %8lu", 0ul, 0ul, 0ul); 23321da177e4SLinus Torvalds } 23331da177e4SLinus Torvalds seq_putc(seq, '\n'); 23341da177e4SLinus Torvalds } 23351da177e4SLinus Torvalds return 0; 23361da177e4SLinus Torvalds } 23371da177e4SLinus Torvalds 2338f690808eSStephen Hemminger static const struct seq_operations ipmr_mfc_seq_ops = { 23391da177e4SLinus Torvalds .start = ipmr_mfc_seq_start, 23401da177e4SLinus Torvalds .next = ipmr_mfc_seq_next, 23411da177e4SLinus Torvalds .stop = ipmr_mfc_seq_stop, 23421da177e4SLinus Torvalds .show = ipmr_mfc_seq_show, 23431da177e4SLinus Torvalds }; 23441da177e4SLinus Torvalds 23451da177e4SLinus Torvalds static int ipmr_mfc_open(struct inode *inode, struct file *file) 23461da177e4SLinus Torvalds { 2347f6bb4514SBenjamin Thery return seq_open_net(inode, file, &ipmr_mfc_seq_ops, 2348cf7732e4SPavel Emelyanov sizeof(struct ipmr_mfc_iter)); 23491da177e4SLinus Torvalds } 23501da177e4SLinus Torvalds 23519a32144eSArjan van de Ven static const struct file_operations ipmr_mfc_fops = { 23521da177e4SLinus Torvalds .owner = THIS_MODULE, 23531da177e4SLinus Torvalds .open = ipmr_mfc_open, 23541da177e4SLinus Torvalds .read = seq_read, 23551da177e4SLinus Torvalds .llseek = seq_lseek, 2356f6bb4514SBenjamin Thery .release = seq_release_net, 23571da177e4SLinus Torvalds }; 23581da177e4SLinus Torvalds #endif 23591da177e4SLinus Torvalds 23601da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM_V2 236132613090SAlexey Dobriyan static const struct net_protocol pim_protocol = { 23621da177e4SLinus Torvalds .handler = pim_rcv, 2363403dbb97STom Goff .netns_ok = 1, 23641da177e4SLinus Torvalds }; 23651da177e4SLinus Torvalds #endif 23661da177e4SLinus Torvalds 23671da177e4SLinus Torvalds 23681da177e4SLinus Torvalds /* 23691da177e4SLinus Torvalds * Setup for IP multicast routing 23701da177e4SLinus Torvalds */ 2371cf958ae3SBenjamin Thery static int __net_init ipmr_net_init(struct net *net) 2372cf958ae3SBenjamin Thery { 2373f0ad0860SPatrick McHardy int err; 2374cf958ae3SBenjamin Thery 2375f0ad0860SPatrick McHardy err = ipmr_rules_init(net); 2376f0ad0860SPatrick McHardy if (err < 0) 2377cf958ae3SBenjamin Thery goto fail; 2378f6bb4514SBenjamin Thery 2379f6bb4514SBenjamin Thery #ifdef CONFIG_PROC_FS 2380f6bb4514SBenjamin Thery err = -ENOMEM; 2381f6bb4514SBenjamin Thery if (!proc_net_fops_create(net, "ip_mr_vif", 0, &ipmr_vif_fops)) 2382f6bb4514SBenjamin Thery goto proc_vif_fail; 2383f6bb4514SBenjamin Thery if (!proc_net_fops_create(net, "ip_mr_cache", 0, &ipmr_mfc_fops)) 2384f6bb4514SBenjamin Thery goto proc_cache_fail; 2385f6bb4514SBenjamin Thery #endif 23862bb8b26cSBenjamin Thery return 0; 23872bb8b26cSBenjamin Thery 2388f6bb4514SBenjamin Thery #ifdef CONFIG_PROC_FS 2389f6bb4514SBenjamin Thery proc_cache_fail: 2390f6bb4514SBenjamin Thery proc_net_remove(net, "ip_mr_vif"); 2391f6bb4514SBenjamin Thery proc_vif_fail: 2392f0ad0860SPatrick McHardy ipmr_rules_exit(net); 2393f6bb4514SBenjamin Thery #endif 2394cf958ae3SBenjamin Thery fail: 2395cf958ae3SBenjamin Thery return err; 2396cf958ae3SBenjamin Thery } 2397cf958ae3SBenjamin Thery 2398cf958ae3SBenjamin Thery static void __net_exit ipmr_net_exit(struct net *net) 2399cf958ae3SBenjamin Thery { 2400f6bb4514SBenjamin Thery #ifdef CONFIG_PROC_FS 2401f6bb4514SBenjamin Thery proc_net_remove(net, "ip_mr_cache"); 2402f6bb4514SBenjamin Thery proc_net_remove(net, "ip_mr_vif"); 2403f6bb4514SBenjamin Thery #endif 2404f0ad0860SPatrick McHardy ipmr_rules_exit(net); 2405cf958ae3SBenjamin Thery } 2406cf958ae3SBenjamin Thery 2407cf958ae3SBenjamin Thery static struct pernet_operations ipmr_net_ops = { 2408cf958ae3SBenjamin Thery .init = ipmr_net_init, 2409cf958ae3SBenjamin Thery .exit = ipmr_net_exit, 2410cf958ae3SBenjamin Thery }; 24111da177e4SLinus Torvalds 241203d2f897SWang Chen int __init ip_mr_init(void) 24131da177e4SLinus Torvalds { 241403d2f897SWang Chen int err; 241503d2f897SWang Chen 24161da177e4SLinus Torvalds mrt_cachep = kmem_cache_create("ip_mrt_cache", 24171da177e4SLinus Torvalds sizeof(struct mfc_cache), 2418e5d679f3SAlexey Dobriyan 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, 241920c2df83SPaul Mundt NULL); 242003d2f897SWang Chen if (!mrt_cachep) 242103d2f897SWang Chen return -ENOMEM; 242203d2f897SWang Chen 2423cf958ae3SBenjamin Thery err = register_pernet_subsys(&ipmr_net_ops); 2424cf958ae3SBenjamin Thery if (err) 2425cf958ae3SBenjamin Thery goto reg_pernet_fail; 2426cf958ae3SBenjamin Thery 242703d2f897SWang Chen err = register_netdevice_notifier(&ip_mr_notifier); 242803d2f897SWang Chen if (err) 242903d2f897SWang Chen goto reg_notif_fail; 2430403dbb97STom Goff #ifdef CONFIG_IP_PIMSM_V2 2431403dbb97STom Goff if (inet_add_protocol(&pim_protocol, IPPROTO_PIM) < 0) { 2432403dbb97STom Goff printk(KERN_ERR "ip_mr_init: can't add PIM protocol\n"); 2433403dbb97STom Goff err = -EAGAIN; 2434403dbb97STom Goff goto add_proto_fail; 2435403dbb97STom Goff } 2436403dbb97STom Goff #endif 2437cb6a4e46SPatrick McHardy rtnl_register(RTNL_FAMILY_IPMR, RTM_GETROUTE, NULL, ipmr_rtm_dumproute); 243803d2f897SWang Chen return 0; 2439f6bb4514SBenjamin Thery 2440403dbb97STom Goff #ifdef CONFIG_IP_PIMSM_V2 2441403dbb97STom Goff add_proto_fail: 2442403dbb97STom Goff unregister_netdevice_notifier(&ip_mr_notifier); 2443403dbb97STom Goff #endif 2444c3e38896SBenjamin Thery reg_notif_fail: 2445cf958ae3SBenjamin Thery unregister_pernet_subsys(&ipmr_net_ops); 2446cf958ae3SBenjamin Thery reg_pernet_fail: 2447c3e38896SBenjamin Thery kmem_cache_destroy(mrt_cachep); 244803d2f897SWang Chen return err; 24491da177e4SLinus Torvalds } 2450