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/uaccess.h> 301da177e4SLinus Torvalds #include <linux/types.h> 314fc268d2SRandy Dunlap #include <linux/capability.h> 321da177e4SLinus Torvalds #include <linux/errno.h> 331da177e4SLinus Torvalds #include <linux/timer.h> 341da177e4SLinus Torvalds #include <linux/mm.h> 351da177e4SLinus Torvalds #include <linux/kernel.h> 361da177e4SLinus Torvalds #include <linux/fcntl.h> 371da177e4SLinus Torvalds #include <linux/stat.h> 381da177e4SLinus Torvalds #include <linux/socket.h> 391da177e4SLinus Torvalds #include <linux/in.h> 401da177e4SLinus Torvalds #include <linux/inet.h> 411da177e4SLinus Torvalds #include <linux/netdevice.h> 421da177e4SLinus Torvalds #include <linux/inetdevice.h> 431da177e4SLinus Torvalds #include <linux/igmp.h> 441da177e4SLinus Torvalds #include <linux/proc_fs.h> 451da177e4SLinus Torvalds #include <linux/seq_file.h> 461da177e4SLinus Torvalds #include <linux/mroute.h> 471da177e4SLinus Torvalds #include <linux/init.h> 4846f25dffSKris Katterjohn #include <linux/if_ether.h> 495a0e3ad6STejun Heo #include <linux/slab.h> 50457c4cbcSEric W. Biederman #include <net/net_namespace.h> 511da177e4SLinus Torvalds #include <net/ip.h> 521da177e4SLinus Torvalds #include <net/protocol.h> 531da177e4SLinus Torvalds #include <linux/skbuff.h> 5414c85021SArnaldo Carvalho de Melo #include <net/route.h> 551da177e4SLinus Torvalds #include <net/sock.h> 561da177e4SLinus Torvalds #include <net/icmp.h> 571da177e4SLinus Torvalds #include <net/udp.h> 581da177e4SLinus Torvalds #include <net/raw.h> 591da177e4SLinus Torvalds #include <linux/notifier.h> 601da177e4SLinus Torvalds #include <linux/if_arp.h> 611da177e4SLinus Torvalds #include <linux/netfilter_ipv4.h> 62709b46e8SEric W. Biederman #include <linux/compat.h> 63bc3b2d7fSPaul Gortmaker #include <linux/export.h> 64c5441932SPravin B Shelar #include <net/ip_tunnels.h> 651da177e4SLinus Torvalds #include <net/checksum.h> 66dc5fc579SArnaldo Carvalho de Melo #include <net/netlink.h> 67f0ad0860SPatrick McHardy #include <net/fib_rules.h> 68d67b8c61SNicolas Dichtel #include <linux/netconf.h> 691da177e4SLinus Torvalds 701da177e4SLinus Torvalds #if defined(CONFIG_IP_PIMSM_V1) || defined(CONFIG_IP_PIMSM_V2) 711da177e4SLinus Torvalds #define CONFIG_IP_PIMSM 1 721da177e4SLinus Torvalds #endif 731da177e4SLinus Torvalds 740c12295aSPatrick McHardy struct mr_table { 75f0ad0860SPatrick McHardy struct list_head list; 768de53dfbSPatrick McHardy #ifdef CONFIG_NET_NS 778de53dfbSPatrick McHardy struct net *net; 788de53dfbSPatrick McHardy #endif 79f0ad0860SPatrick McHardy u32 id; 804c968709SEric Dumazet struct sock __rcu *mroute_sk; 810c12295aSPatrick McHardy struct timer_list ipmr_expire_timer; 820c12295aSPatrick McHardy struct list_head mfc_unres_queue; 830c12295aSPatrick McHardy struct list_head mfc_cache_array[MFC_LINES]; 840c12295aSPatrick McHardy struct vif_device vif_table[MAXVIFS]; 850c12295aSPatrick McHardy int maxvif; 860c12295aSPatrick McHardy atomic_t cache_resolve_queue_len; 8753d6841dSJoe Perches bool mroute_do_assert; 8853d6841dSJoe Perches bool mroute_do_pim; 890c12295aSPatrick McHardy #if defined(CONFIG_IP_PIMSM_V1) || defined(CONFIG_IP_PIMSM_V2) 900c12295aSPatrick McHardy int mroute_reg_vif_num; 910c12295aSPatrick McHardy #endif 920c12295aSPatrick McHardy }; 930c12295aSPatrick McHardy 94f0ad0860SPatrick McHardy struct ipmr_rule { 95f0ad0860SPatrick McHardy struct fib_rule common; 96f0ad0860SPatrick McHardy }; 97f0ad0860SPatrick McHardy 98f0ad0860SPatrick McHardy struct ipmr_result { 99f0ad0860SPatrick McHardy struct mr_table *mrt; 100f0ad0860SPatrick McHardy }; 101f0ad0860SPatrick McHardy 1021da177e4SLinus Torvalds /* Big lock, protecting vif table, mrt cache and mroute socket state. 103a8cb16ddSEric Dumazet * Note that the changes are semaphored via rtnl_lock. 1041da177e4SLinus Torvalds */ 1051da177e4SLinus Torvalds 1061da177e4SLinus Torvalds static DEFINE_RWLOCK(mrt_lock); 1071da177e4SLinus Torvalds 1081da177e4SLinus Torvalds /* 1091da177e4SLinus Torvalds * Multicast router control variables 1101da177e4SLinus Torvalds */ 1111da177e4SLinus Torvalds 1120c12295aSPatrick McHardy #define VIF_EXISTS(_mrt, _idx) ((_mrt)->vif_table[_idx].dev != NULL) 1131da177e4SLinus Torvalds 1141da177e4SLinus Torvalds /* Special spinlock for queue of unresolved entries */ 1151da177e4SLinus Torvalds static DEFINE_SPINLOCK(mfc_unres_lock); 1161da177e4SLinus Torvalds 1171da177e4SLinus Torvalds /* We return to original Alan's scheme. Hash table of resolved 118a8cb16ddSEric Dumazet * entries is changed only in process context and protected 119a8cb16ddSEric Dumazet * with weak lock mrt_lock. Queue of unresolved entries is protected 120a8cb16ddSEric Dumazet * with strong spinlock mfc_unres_lock. 121a8cb16ddSEric Dumazet * 122a8cb16ddSEric Dumazet * In this case data path is free of exclusive locks at all. 1231da177e4SLinus Torvalds */ 1241da177e4SLinus Torvalds 125e18b890bSChristoph Lameter static struct kmem_cache *mrt_cachep __read_mostly; 1261da177e4SLinus Torvalds 127f0ad0860SPatrick McHardy static struct mr_table *ipmr_new_table(struct net *net, u32 id); 128acbb219dSFrancesco Ruggeri static void ipmr_free_table(struct mr_table *mrt); 129acbb219dSFrancesco Ruggeri 130c4854ec8SRami Rosen static void ip_mr_forward(struct net *net, struct mr_table *mrt, 1310c12295aSPatrick McHardy struct sk_buff *skb, struct mfc_cache *cache, 1320c12295aSPatrick McHardy int local); 1330c12295aSPatrick McHardy static int ipmr_cache_report(struct mr_table *mrt, 1344feb88e5SBenjamin Thery struct sk_buff *pkt, vifi_t vifi, int assert); 135cb6a4e46SPatrick McHardy static int __ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb, 136d658f8a0SPatrick McHardy struct mfc_cache *c, struct rtmsg *rtm); 1378cd3ac9fSNicolas Dichtel static void mroute_netlink_event(struct mr_table *mrt, struct mfc_cache *mfc, 1388cd3ac9fSNicolas Dichtel int cmd); 139acbb219dSFrancesco Ruggeri static void mroute_clean_tables(struct mr_table *mrt); 140f0ad0860SPatrick McHardy static void ipmr_expire_process(unsigned long arg); 1411da177e4SLinus Torvalds 142f0ad0860SPatrick McHardy #ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES 143f0ad0860SPatrick McHardy #define ipmr_for_each_table(mrt, net) \ 144f0ad0860SPatrick McHardy list_for_each_entry_rcu(mrt, &net->ipv4.mr_tables, list) 145f0ad0860SPatrick McHardy 146f0ad0860SPatrick McHardy static struct mr_table *ipmr_get_table(struct net *net, u32 id) 147f0ad0860SPatrick McHardy { 148f0ad0860SPatrick McHardy struct mr_table *mrt; 149f0ad0860SPatrick McHardy 150f0ad0860SPatrick McHardy ipmr_for_each_table(mrt, net) { 151f0ad0860SPatrick McHardy if (mrt->id == id) 152f0ad0860SPatrick McHardy return mrt; 153f0ad0860SPatrick McHardy } 154f0ad0860SPatrick McHardy return NULL; 155f0ad0860SPatrick McHardy } 156f0ad0860SPatrick McHardy 157da91981bSDavid S. Miller static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4, 158f0ad0860SPatrick McHardy struct mr_table **mrt) 159f0ad0860SPatrick McHardy { 160f0ad0860SPatrick McHardy int err; 16195f4a45dSHannes Frederic Sowa struct ipmr_result res; 16295f4a45dSHannes Frederic Sowa struct fib_lookup_arg arg = { 16395f4a45dSHannes Frederic Sowa .result = &res, 16495f4a45dSHannes Frederic Sowa .flags = FIB_LOOKUP_NOREF, 16595f4a45dSHannes Frederic Sowa }; 166f0ad0860SPatrick McHardy 167da91981bSDavid S. Miller err = fib_rules_lookup(net->ipv4.mr_rules_ops, 168da91981bSDavid S. Miller flowi4_to_flowi(flp4), 0, &arg); 169f0ad0860SPatrick McHardy if (err < 0) 170f0ad0860SPatrick McHardy return err; 171f0ad0860SPatrick McHardy *mrt = res.mrt; 172f0ad0860SPatrick McHardy return 0; 173f0ad0860SPatrick McHardy } 174f0ad0860SPatrick McHardy 175f0ad0860SPatrick McHardy static int ipmr_rule_action(struct fib_rule *rule, struct flowi *flp, 176f0ad0860SPatrick McHardy int flags, struct fib_lookup_arg *arg) 177f0ad0860SPatrick McHardy { 178f0ad0860SPatrick McHardy struct ipmr_result *res = arg->result; 179f0ad0860SPatrick McHardy struct mr_table *mrt; 180f0ad0860SPatrick McHardy 181f0ad0860SPatrick McHardy switch (rule->action) { 182f0ad0860SPatrick McHardy case FR_ACT_TO_TBL: 183f0ad0860SPatrick McHardy break; 184f0ad0860SPatrick McHardy case FR_ACT_UNREACHABLE: 185f0ad0860SPatrick McHardy return -ENETUNREACH; 186f0ad0860SPatrick McHardy case FR_ACT_PROHIBIT: 187f0ad0860SPatrick McHardy return -EACCES; 188f0ad0860SPatrick McHardy case FR_ACT_BLACKHOLE: 189f0ad0860SPatrick McHardy default: 190f0ad0860SPatrick McHardy return -EINVAL; 191f0ad0860SPatrick McHardy } 192f0ad0860SPatrick McHardy 193f0ad0860SPatrick McHardy mrt = ipmr_get_table(rule->fr_net, rule->table); 194f0ad0860SPatrick McHardy if (mrt == NULL) 195f0ad0860SPatrick McHardy return -EAGAIN; 196f0ad0860SPatrick McHardy res->mrt = mrt; 197f0ad0860SPatrick McHardy return 0; 198f0ad0860SPatrick McHardy } 199f0ad0860SPatrick McHardy 200f0ad0860SPatrick McHardy static int ipmr_rule_match(struct fib_rule *rule, struct flowi *fl, int flags) 201f0ad0860SPatrick McHardy { 202f0ad0860SPatrick McHardy return 1; 203f0ad0860SPatrick McHardy } 204f0ad0860SPatrick McHardy 205f0ad0860SPatrick McHardy static const struct nla_policy ipmr_rule_policy[FRA_MAX + 1] = { 206f0ad0860SPatrick McHardy FRA_GENERIC_POLICY, 207f0ad0860SPatrick McHardy }; 208f0ad0860SPatrick McHardy 209f0ad0860SPatrick McHardy static int ipmr_rule_configure(struct fib_rule *rule, struct sk_buff *skb, 210f0ad0860SPatrick McHardy struct fib_rule_hdr *frh, struct nlattr **tb) 211f0ad0860SPatrick McHardy { 212f0ad0860SPatrick McHardy return 0; 213f0ad0860SPatrick McHardy } 214f0ad0860SPatrick McHardy 215f0ad0860SPatrick McHardy static int ipmr_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh, 216f0ad0860SPatrick McHardy struct nlattr **tb) 217f0ad0860SPatrick McHardy { 218f0ad0860SPatrick McHardy return 1; 219f0ad0860SPatrick McHardy } 220f0ad0860SPatrick McHardy 221f0ad0860SPatrick McHardy static int ipmr_rule_fill(struct fib_rule *rule, struct sk_buff *skb, 222f0ad0860SPatrick McHardy struct fib_rule_hdr *frh) 223f0ad0860SPatrick McHardy { 224f0ad0860SPatrick McHardy frh->dst_len = 0; 225f0ad0860SPatrick McHardy frh->src_len = 0; 226f0ad0860SPatrick McHardy frh->tos = 0; 227f0ad0860SPatrick McHardy return 0; 228f0ad0860SPatrick McHardy } 229f0ad0860SPatrick McHardy 23004a6f82cSAndi Kleen static const struct fib_rules_ops __net_initconst ipmr_rules_ops_template = { 23125239ceeSPatrick McHardy .family = RTNL_FAMILY_IPMR, 232f0ad0860SPatrick McHardy .rule_size = sizeof(struct ipmr_rule), 233f0ad0860SPatrick McHardy .addr_size = sizeof(u32), 234f0ad0860SPatrick McHardy .action = ipmr_rule_action, 235f0ad0860SPatrick McHardy .match = ipmr_rule_match, 236f0ad0860SPatrick McHardy .configure = ipmr_rule_configure, 237f0ad0860SPatrick McHardy .compare = ipmr_rule_compare, 238f0ad0860SPatrick McHardy .default_pref = fib_default_rule_pref, 239f0ad0860SPatrick McHardy .fill = ipmr_rule_fill, 240f0ad0860SPatrick McHardy .nlgroup = RTNLGRP_IPV4_RULE, 241f0ad0860SPatrick McHardy .policy = ipmr_rule_policy, 242f0ad0860SPatrick McHardy .owner = THIS_MODULE, 243f0ad0860SPatrick McHardy }; 244f0ad0860SPatrick McHardy 245f0ad0860SPatrick McHardy static int __net_init ipmr_rules_init(struct net *net) 246f0ad0860SPatrick McHardy { 247f0ad0860SPatrick McHardy struct fib_rules_ops *ops; 248f0ad0860SPatrick McHardy struct mr_table *mrt; 249f0ad0860SPatrick McHardy int err; 250f0ad0860SPatrick McHardy 251f0ad0860SPatrick McHardy ops = fib_rules_register(&ipmr_rules_ops_template, net); 252f0ad0860SPatrick McHardy if (IS_ERR(ops)) 253f0ad0860SPatrick McHardy return PTR_ERR(ops); 254f0ad0860SPatrick McHardy 255f0ad0860SPatrick McHardy INIT_LIST_HEAD(&net->ipv4.mr_tables); 256f0ad0860SPatrick McHardy 257f0ad0860SPatrick McHardy mrt = ipmr_new_table(net, RT_TABLE_DEFAULT); 258f0ad0860SPatrick McHardy if (mrt == NULL) { 259f0ad0860SPatrick McHardy err = -ENOMEM; 260f0ad0860SPatrick McHardy goto err1; 261f0ad0860SPatrick McHardy } 262f0ad0860SPatrick McHardy 263f0ad0860SPatrick McHardy err = fib_default_rule_add(ops, 0x7fff, RT_TABLE_DEFAULT, 0); 264f0ad0860SPatrick McHardy if (err < 0) 265f0ad0860SPatrick McHardy goto err2; 266f0ad0860SPatrick McHardy 267f0ad0860SPatrick McHardy net->ipv4.mr_rules_ops = ops; 268f0ad0860SPatrick McHardy return 0; 269f0ad0860SPatrick McHardy 270f0ad0860SPatrick McHardy err2: 271f0ad0860SPatrick McHardy kfree(mrt); 272f0ad0860SPatrick McHardy err1: 273f0ad0860SPatrick McHardy fib_rules_unregister(ops); 274f0ad0860SPatrick McHardy return err; 275f0ad0860SPatrick McHardy } 276f0ad0860SPatrick McHardy 277f0ad0860SPatrick McHardy static void __net_exit ipmr_rules_exit(struct net *net) 278f0ad0860SPatrick McHardy { 279f0ad0860SPatrick McHardy struct mr_table *mrt, *next; 280f0ad0860SPatrick McHardy 281035320d5SEric Dumazet list_for_each_entry_safe(mrt, next, &net->ipv4.mr_tables, list) { 282035320d5SEric Dumazet list_del(&mrt->list); 283acbb219dSFrancesco Ruggeri ipmr_free_table(mrt); 284035320d5SEric Dumazet } 285f0ad0860SPatrick McHardy fib_rules_unregister(net->ipv4.mr_rules_ops); 286f0ad0860SPatrick McHardy } 287f0ad0860SPatrick McHardy #else 288f0ad0860SPatrick McHardy #define ipmr_for_each_table(mrt, net) \ 289f0ad0860SPatrick McHardy for (mrt = net->ipv4.mrt; mrt; mrt = NULL) 290f0ad0860SPatrick McHardy 291f0ad0860SPatrick McHardy static struct mr_table *ipmr_get_table(struct net *net, u32 id) 292f0ad0860SPatrick McHardy { 293f0ad0860SPatrick McHardy return net->ipv4.mrt; 294f0ad0860SPatrick McHardy } 295f0ad0860SPatrick McHardy 296da91981bSDavid S. Miller static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4, 297f0ad0860SPatrick McHardy struct mr_table **mrt) 298f0ad0860SPatrick McHardy { 299f0ad0860SPatrick McHardy *mrt = net->ipv4.mrt; 300f0ad0860SPatrick McHardy return 0; 301f0ad0860SPatrick McHardy } 302f0ad0860SPatrick McHardy 303f0ad0860SPatrick McHardy static int __net_init ipmr_rules_init(struct net *net) 304f0ad0860SPatrick McHardy { 305f0ad0860SPatrick McHardy net->ipv4.mrt = ipmr_new_table(net, RT_TABLE_DEFAULT); 306f0ad0860SPatrick McHardy return net->ipv4.mrt ? 0 : -ENOMEM; 307f0ad0860SPatrick McHardy } 308f0ad0860SPatrick McHardy 309f0ad0860SPatrick McHardy static void __net_exit ipmr_rules_exit(struct net *net) 310f0ad0860SPatrick McHardy { 311acbb219dSFrancesco Ruggeri ipmr_free_table(net->ipv4.mrt); 312f0ad0860SPatrick McHardy } 313f0ad0860SPatrick McHardy #endif 314f0ad0860SPatrick McHardy 315f0ad0860SPatrick McHardy static struct mr_table *ipmr_new_table(struct net *net, u32 id) 316f0ad0860SPatrick McHardy { 317f0ad0860SPatrick McHardy struct mr_table *mrt; 318f0ad0860SPatrick McHardy unsigned int i; 319f0ad0860SPatrick McHardy 320f0ad0860SPatrick McHardy mrt = ipmr_get_table(net, id); 321f0ad0860SPatrick McHardy if (mrt != NULL) 322f0ad0860SPatrick McHardy return mrt; 323f0ad0860SPatrick McHardy 324f0ad0860SPatrick McHardy mrt = kzalloc(sizeof(*mrt), GFP_KERNEL); 325f0ad0860SPatrick McHardy if (mrt == NULL) 326f0ad0860SPatrick McHardy return NULL; 3278de53dfbSPatrick McHardy write_pnet(&mrt->net, net); 328f0ad0860SPatrick McHardy mrt->id = id; 329f0ad0860SPatrick McHardy 330f0ad0860SPatrick McHardy /* Forwarding cache */ 331f0ad0860SPatrick McHardy for (i = 0; i < MFC_LINES; i++) 332f0ad0860SPatrick McHardy INIT_LIST_HEAD(&mrt->mfc_cache_array[i]); 333f0ad0860SPatrick McHardy 334f0ad0860SPatrick McHardy INIT_LIST_HEAD(&mrt->mfc_unres_queue); 335f0ad0860SPatrick McHardy 336f0ad0860SPatrick McHardy setup_timer(&mrt->ipmr_expire_timer, ipmr_expire_process, 337f0ad0860SPatrick McHardy (unsigned long)mrt); 338f0ad0860SPatrick McHardy 339f0ad0860SPatrick McHardy #ifdef CONFIG_IP_PIMSM 340f0ad0860SPatrick McHardy mrt->mroute_reg_vif_num = -1; 341f0ad0860SPatrick McHardy #endif 342f0ad0860SPatrick McHardy #ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES 343f0ad0860SPatrick McHardy list_add_tail_rcu(&mrt->list, &net->ipv4.mr_tables); 344f0ad0860SPatrick McHardy #endif 345f0ad0860SPatrick McHardy return mrt; 346f0ad0860SPatrick McHardy } 3471da177e4SLinus Torvalds 348acbb219dSFrancesco Ruggeri static void ipmr_free_table(struct mr_table *mrt) 349acbb219dSFrancesco Ruggeri { 350acbb219dSFrancesco Ruggeri del_timer_sync(&mrt->ipmr_expire_timer); 351acbb219dSFrancesco Ruggeri mroute_clean_tables(mrt); 352acbb219dSFrancesco Ruggeri kfree(mrt); 353acbb219dSFrancesco Ruggeri } 354acbb219dSFrancesco Ruggeri 3551da177e4SLinus Torvalds /* Service routines creating virtual interfaces: DVMRP tunnels and PIMREG */ 3561da177e4SLinus Torvalds 357d607032dSWang Chen static void ipmr_del_tunnel(struct net_device *dev, struct vifctl *v) 358d607032dSWang Chen { 3594feb88e5SBenjamin Thery struct net *net = dev_net(dev); 3604feb88e5SBenjamin Thery 361d607032dSWang Chen dev_close(dev); 362d607032dSWang Chen 3634feb88e5SBenjamin Thery dev = __dev_get_by_name(net, "tunl0"); 364d607032dSWang Chen if (dev) { 3655bc3eb7eSStephen Hemminger const struct net_device_ops *ops = dev->netdev_ops; 366d607032dSWang Chen struct ifreq ifr; 367d607032dSWang Chen struct ip_tunnel_parm p; 368d607032dSWang Chen 369d607032dSWang Chen memset(&p, 0, sizeof(p)); 370d607032dSWang Chen p.iph.daddr = v->vifc_rmt_addr.s_addr; 371d607032dSWang Chen p.iph.saddr = v->vifc_lcl_addr.s_addr; 372d607032dSWang Chen p.iph.version = 4; 373d607032dSWang Chen p.iph.ihl = 5; 374d607032dSWang Chen p.iph.protocol = IPPROTO_IPIP; 375d607032dSWang Chen sprintf(p.name, "dvmrp%d", v->vifc_vifi); 376d607032dSWang Chen ifr.ifr_ifru.ifru_data = (__force void __user *)&p; 377d607032dSWang Chen 3785bc3eb7eSStephen Hemminger if (ops->ndo_do_ioctl) { 3795bc3eb7eSStephen Hemminger mm_segment_t oldfs = get_fs(); 3805bc3eb7eSStephen Hemminger 3815bc3eb7eSStephen Hemminger set_fs(KERNEL_DS); 3825bc3eb7eSStephen Hemminger ops->ndo_do_ioctl(dev, &ifr, SIOCDELTUNNEL); 383d607032dSWang Chen set_fs(oldfs); 384d607032dSWang Chen } 385d607032dSWang Chen } 3865bc3eb7eSStephen Hemminger } 387d607032dSWang Chen 3881da177e4SLinus Torvalds static 3894feb88e5SBenjamin Thery struct net_device *ipmr_new_tunnel(struct net *net, struct vifctl *v) 3901da177e4SLinus Torvalds { 3911da177e4SLinus Torvalds struct net_device *dev; 3921da177e4SLinus Torvalds 3934feb88e5SBenjamin Thery dev = __dev_get_by_name(net, "tunl0"); 3941da177e4SLinus Torvalds 3951da177e4SLinus Torvalds if (dev) { 3965bc3eb7eSStephen Hemminger const struct net_device_ops *ops = dev->netdev_ops; 3971da177e4SLinus Torvalds int err; 3981da177e4SLinus Torvalds struct ifreq ifr; 3991da177e4SLinus Torvalds struct ip_tunnel_parm p; 4001da177e4SLinus Torvalds struct in_device *in_dev; 4011da177e4SLinus Torvalds 4021da177e4SLinus Torvalds memset(&p, 0, sizeof(p)); 4031da177e4SLinus Torvalds p.iph.daddr = v->vifc_rmt_addr.s_addr; 4041da177e4SLinus Torvalds p.iph.saddr = v->vifc_lcl_addr.s_addr; 4051da177e4SLinus Torvalds p.iph.version = 4; 4061da177e4SLinus Torvalds p.iph.ihl = 5; 4071da177e4SLinus Torvalds p.iph.protocol = IPPROTO_IPIP; 4081da177e4SLinus Torvalds sprintf(p.name, "dvmrp%d", v->vifc_vifi); 409ba93ef74SStephen Hemminger ifr.ifr_ifru.ifru_data = (__force void __user *)&p; 4101da177e4SLinus Torvalds 4115bc3eb7eSStephen Hemminger if (ops->ndo_do_ioctl) { 4125bc3eb7eSStephen Hemminger mm_segment_t oldfs = get_fs(); 4135bc3eb7eSStephen Hemminger 4145bc3eb7eSStephen Hemminger set_fs(KERNEL_DS); 4155bc3eb7eSStephen Hemminger err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL); 4161da177e4SLinus Torvalds set_fs(oldfs); 417a8cb16ddSEric Dumazet } else { 4185bc3eb7eSStephen Hemminger err = -EOPNOTSUPP; 419a8cb16ddSEric Dumazet } 4201da177e4SLinus Torvalds dev = NULL; 4211da177e4SLinus Torvalds 4224feb88e5SBenjamin Thery if (err == 0 && 4234feb88e5SBenjamin Thery (dev = __dev_get_by_name(net, p.name)) != NULL) { 4241da177e4SLinus Torvalds dev->flags |= IFF_MULTICAST; 4251da177e4SLinus Torvalds 426e5ed6399SHerbert Xu in_dev = __in_dev_get_rtnl(dev); 42771e27da9SHerbert Xu if (in_dev == NULL) 4281da177e4SLinus Torvalds goto failure; 42971e27da9SHerbert Xu 43071e27da9SHerbert Xu ipv4_devconf_setall(in_dev); 4311d4c8c29SJiri Pirko neigh_parms_data_state_setall(in_dev->arp_parms); 43271e27da9SHerbert Xu IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0; 4331da177e4SLinus Torvalds 4341da177e4SLinus Torvalds if (dev_open(dev)) 4351da177e4SLinus Torvalds goto failure; 4367dc00c82SWang Chen dev_hold(dev); 4371da177e4SLinus Torvalds } 4381da177e4SLinus Torvalds } 4391da177e4SLinus Torvalds return dev; 4401da177e4SLinus Torvalds 4411da177e4SLinus Torvalds failure: 4421da177e4SLinus Torvalds /* allow the register to be completed before unregistering. */ 4431da177e4SLinus Torvalds rtnl_unlock(); 4441da177e4SLinus Torvalds rtnl_lock(); 4451da177e4SLinus Torvalds 4461da177e4SLinus Torvalds unregister_netdevice(dev); 4471da177e4SLinus Torvalds return NULL; 4481da177e4SLinus Torvalds } 4491da177e4SLinus Torvalds 4501da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM 4511da177e4SLinus Torvalds 4526fef4c0cSStephen Hemminger static netdev_tx_t reg_vif_xmit(struct sk_buff *skb, struct net_device *dev) 4531da177e4SLinus Torvalds { 4544feb88e5SBenjamin Thery struct net *net = dev_net(dev); 455f0ad0860SPatrick McHardy struct mr_table *mrt; 456da91981bSDavid S. Miller struct flowi4 fl4 = { 457da91981bSDavid S. Miller .flowi4_oif = dev->ifindex, 458*6a662719SCong Wang .flowi4_iif = skb->skb_iif ? : LOOPBACK_IFINDEX, 459da91981bSDavid S. Miller .flowi4_mark = skb->mark, 460f0ad0860SPatrick McHardy }; 461f0ad0860SPatrick McHardy int err; 462f0ad0860SPatrick McHardy 463da91981bSDavid S. Miller err = ipmr_fib_lookup(net, &fl4, &mrt); 464e40dbc51SBen Greear if (err < 0) { 465e40dbc51SBen Greear kfree_skb(skb); 466f0ad0860SPatrick McHardy return err; 467e40dbc51SBen Greear } 4684feb88e5SBenjamin Thery 4691da177e4SLinus Torvalds read_lock(&mrt_lock); 470cf3677aeSPavel Emelyanov dev->stats.tx_bytes += skb->len; 471cf3677aeSPavel Emelyanov dev->stats.tx_packets++; 4720c12295aSPatrick McHardy ipmr_cache_report(mrt, skb, mrt->mroute_reg_vif_num, IGMPMSG_WHOLEPKT); 4731da177e4SLinus Torvalds read_unlock(&mrt_lock); 4741da177e4SLinus Torvalds kfree_skb(skb); 4756ed10654SPatrick McHardy return NETDEV_TX_OK; 4761da177e4SLinus Torvalds } 4771da177e4SLinus Torvalds 478007c3838SStephen Hemminger static const struct net_device_ops reg_vif_netdev_ops = { 479007c3838SStephen Hemminger .ndo_start_xmit = reg_vif_xmit, 480007c3838SStephen Hemminger }; 481007c3838SStephen Hemminger 4821da177e4SLinus Torvalds static void reg_vif_setup(struct net_device *dev) 4831da177e4SLinus Torvalds { 4841da177e4SLinus Torvalds dev->type = ARPHRD_PIMREG; 48546f25dffSKris Katterjohn dev->mtu = ETH_DATA_LEN - sizeof(struct iphdr) - 8; 4861da177e4SLinus Torvalds dev->flags = IFF_NOARP; 487007c3838SStephen Hemminger dev->netdev_ops = ®_vif_netdev_ops, 4881da177e4SLinus Torvalds dev->destructor = free_netdev; 489403dbb97STom Goff dev->features |= NETIF_F_NETNS_LOCAL; 4901da177e4SLinus Torvalds } 4911da177e4SLinus Torvalds 492f0ad0860SPatrick McHardy static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt) 4931da177e4SLinus Torvalds { 4941da177e4SLinus Torvalds struct net_device *dev; 4951da177e4SLinus Torvalds struct in_device *in_dev; 496f0ad0860SPatrick McHardy char name[IFNAMSIZ]; 4971da177e4SLinus Torvalds 498f0ad0860SPatrick McHardy if (mrt->id == RT_TABLE_DEFAULT) 499f0ad0860SPatrick McHardy sprintf(name, "pimreg"); 500f0ad0860SPatrick McHardy else 501f0ad0860SPatrick McHardy sprintf(name, "pimreg%u", mrt->id); 502f0ad0860SPatrick McHardy 503f0ad0860SPatrick McHardy dev = alloc_netdev(0, name, reg_vif_setup); 5041da177e4SLinus Torvalds 5051da177e4SLinus Torvalds if (dev == NULL) 5061da177e4SLinus Torvalds return NULL; 5071da177e4SLinus Torvalds 508403dbb97STom Goff dev_net_set(dev, net); 509403dbb97STom Goff 5101da177e4SLinus Torvalds if (register_netdevice(dev)) { 5111da177e4SLinus Torvalds free_netdev(dev); 5121da177e4SLinus Torvalds return NULL; 5131da177e4SLinus Torvalds } 5141da177e4SLinus Torvalds dev->iflink = 0; 5151da177e4SLinus Torvalds 51671e27da9SHerbert Xu rcu_read_lock(); 517a8cb16ddSEric Dumazet in_dev = __in_dev_get_rcu(dev); 518a8cb16ddSEric Dumazet if (!in_dev) { 51971e27da9SHerbert Xu rcu_read_unlock(); 5201da177e4SLinus Torvalds goto failure; 52171e27da9SHerbert Xu } 5221da177e4SLinus Torvalds 52371e27da9SHerbert Xu ipv4_devconf_setall(in_dev); 5241d4c8c29SJiri Pirko neigh_parms_data_state_setall(in_dev->arp_parms); 52571e27da9SHerbert Xu IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0; 52671e27da9SHerbert Xu rcu_read_unlock(); 5271da177e4SLinus Torvalds 5281da177e4SLinus Torvalds if (dev_open(dev)) 5291da177e4SLinus Torvalds goto failure; 5301da177e4SLinus Torvalds 5317dc00c82SWang Chen dev_hold(dev); 5327dc00c82SWang Chen 5331da177e4SLinus Torvalds return dev; 5341da177e4SLinus Torvalds 5351da177e4SLinus Torvalds failure: 5361da177e4SLinus Torvalds /* allow the register to be completed before unregistering. */ 5371da177e4SLinus Torvalds rtnl_unlock(); 5381da177e4SLinus Torvalds rtnl_lock(); 5391da177e4SLinus Torvalds 5401da177e4SLinus Torvalds unregister_netdevice(dev); 5411da177e4SLinus Torvalds return NULL; 5421da177e4SLinus Torvalds } 5431da177e4SLinus Torvalds #endif 5441da177e4SLinus Torvalds 5452c53040fSBen Hutchings /** 5462c53040fSBen Hutchings * vif_delete - Delete a VIF entry 5477dc00c82SWang Chen * @notify: Set to 1, if the caller is a notifier_call 5481da177e4SLinus Torvalds */ 5491da177e4SLinus Torvalds 5500c12295aSPatrick McHardy static int vif_delete(struct mr_table *mrt, int vifi, int notify, 551d17fa6faSEric Dumazet struct list_head *head) 5521da177e4SLinus Torvalds { 5531da177e4SLinus Torvalds struct vif_device *v; 5541da177e4SLinus Torvalds struct net_device *dev; 5551da177e4SLinus Torvalds struct in_device *in_dev; 5561da177e4SLinus Torvalds 5570c12295aSPatrick McHardy if (vifi < 0 || vifi >= mrt->maxvif) 5581da177e4SLinus Torvalds return -EADDRNOTAVAIL; 5591da177e4SLinus Torvalds 5600c12295aSPatrick McHardy v = &mrt->vif_table[vifi]; 5611da177e4SLinus Torvalds 5621da177e4SLinus Torvalds write_lock_bh(&mrt_lock); 5631da177e4SLinus Torvalds dev = v->dev; 5641da177e4SLinus Torvalds v->dev = NULL; 5651da177e4SLinus Torvalds 5661da177e4SLinus Torvalds if (!dev) { 5671da177e4SLinus Torvalds write_unlock_bh(&mrt_lock); 5681da177e4SLinus Torvalds return -EADDRNOTAVAIL; 5691da177e4SLinus Torvalds } 5701da177e4SLinus Torvalds 5711da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM 5720c12295aSPatrick McHardy if (vifi == mrt->mroute_reg_vif_num) 5730c12295aSPatrick McHardy mrt->mroute_reg_vif_num = -1; 5741da177e4SLinus Torvalds #endif 5751da177e4SLinus Torvalds 5760c12295aSPatrick McHardy if (vifi + 1 == mrt->maxvif) { 5771da177e4SLinus Torvalds int tmp; 578a8cb16ddSEric Dumazet 5791da177e4SLinus Torvalds for (tmp = vifi - 1; tmp >= 0; tmp--) { 5800c12295aSPatrick McHardy if (VIF_EXISTS(mrt, tmp)) 5811da177e4SLinus Torvalds break; 5821da177e4SLinus Torvalds } 5830c12295aSPatrick McHardy mrt->maxvif = tmp+1; 5841da177e4SLinus Torvalds } 5851da177e4SLinus Torvalds 5861da177e4SLinus Torvalds write_unlock_bh(&mrt_lock); 5871da177e4SLinus Torvalds 5881da177e4SLinus Torvalds dev_set_allmulti(dev, -1); 5891da177e4SLinus Torvalds 590a8cb16ddSEric Dumazet in_dev = __in_dev_get_rtnl(dev); 591a8cb16ddSEric Dumazet if (in_dev) { 59242f811b8SHerbert Xu IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)--; 593d67b8c61SNicolas Dichtel inet_netconf_notify_devconf(dev_net(dev), 594d67b8c61SNicolas Dichtel NETCONFA_MC_FORWARDING, 595d67b8c61SNicolas Dichtel dev->ifindex, &in_dev->cnf); 5961da177e4SLinus Torvalds ip_rt_multicast_event(in_dev); 5971da177e4SLinus Torvalds } 5981da177e4SLinus Torvalds 5997dc00c82SWang Chen if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER) && !notify) 600d17fa6faSEric Dumazet unregister_netdevice_queue(dev, head); 6011da177e4SLinus Torvalds 6021da177e4SLinus Torvalds dev_put(dev); 6031da177e4SLinus Torvalds return 0; 6041da177e4SLinus Torvalds } 6051da177e4SLinus Torvalds 606a8c9486bSEric Dumazet static void ipmr_cache_free_rcu(struct rcu_head *head) 607a8c9486bSEric Dumazet { 608a8c9486bSEric Dumazet struct mfc_cache *c = container_of(head, struct mfc_cache, rcu); 609a8c9486bSEric Dumazet 610a8c9486bSEric Dumazet kmem_cache_free(mrt_cachep, c); 611a8c9486bSEric Dumazet } 612a8c9486bSEric Dumazet 6135c0a66f5SBenjamin Thery static inline void ipmr_cache_free(struct mfc_cache *c) 6145c0a66f5SBenjamin Thery { 615a8c9486bSEric Dumazet call_rcu(&c->rcu, ipmr_cache_free_rcu); 6165c0a66f5SBenjamin Thery } 6175c0a66f5SBenjamin Thery 6181da177e4SLinus Torvalds /* Destroy an unresolved cache entry, killing queued skbs 619a8cb16ddSEric Dumazet * and reporting error to netlink readers. 6201da177e4SLinus Torvalds */ 6211da177e4SLinus Torvalds 6220c12295aSPatrick McHardy static void ipmr_destroy_unres(struct mr_table *mrt, struct mfc_cache *c) 6231da177e4SLinus Torvalds { 6248de53dfbSPatrick McHardy struct net *net = read_pnet(&mrt->net); 6251da177e4SLinus Torvalds struct sk_buff *skb; 6269ef1d4c7SPatrick McHardy struct nlmsgerr *e; 6271da177e4SLinus Torvalds 6280c12295aSPatrick McHardy atomic_dec(&mrt->cache_resolve_queue_len); 6291da177e4SLinus Torvalds 6301da177e4SLinus Torvalds while ((skb = skb_dequeue(&c->mfc_un.unres.unresolved))) { 631eddc9ec5SArnaldo Carvalho de Melo if (ip_hdr(skb)->version == 0) { 6321da177e4SLinus Torvalds struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr)); 6331da177e4SLinus Torvalds nlh->nlmsg_type = NLMSG_ERROR; 634573ce260SHong zhi guo nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr)); 6351da177e4SLinus Torvalds skb_trim(skb, nlh->nlmsg_len); 636573ce260SHong zhi guo e = nlmsg_data(nlh); 6379ef1d4c7SPatrick McHardy e->error = -ETIMEDOUT; 6389ef1d4c7SPatrick McHardy memset(&e->msg, 0, sizeof(e->msg)); 6392942e900SThomas Graf 64015e47304SEric W. Biederman rtnl_unicast(skb, net, NETLINK_CB(skb).portid); 641a8cb16ddSEric Dumazet } else { 6421da177e4SLinus Torvalds kfree_skb(skb); 6431da177e4SLinus Torvalds } 644a8cb16ddSEric Dumazet } 6451da177e4SLinus Torvalds 6465c0a66f5SBenjamin Thery ipmr_cache_free(c); 6471da177e4SLinus Torvalds } 6481da177e4SLinus Torvalds 6491da177e4SLinus Torvalds 650e258beb2SPatrick McHardy /* Timer process for the unresolved queue. */ 6511da177e4SLinus Torvalds 652e258beb2SPatrick McHardy static void ipmr_expire_process(unsigned long arg) 6531da177e4SLinus Torvalds { 6540c12295aSPatrick McHardy struct mr_table *mrt = (struct mr_table *)arg; 6551da177e4SLinus Torvalds unsigned long now; 6561da177e4SLinus Torvalds unsigned long expires; 657862465f2SPatrick McHardy struct mfc_cache *c, *next; 6581da177e4SLinus Torvalds 6591da177e4SLinus Torvalds if (!spin_trylock(&mfc_unres_lock)) { 6600c12295aSPatrick McHardy mod_timer(&mrt->ipmr_expire_timer, jiffies+HZ/10); 6611da177e4SLinus Torvalds return; 6621da177e4SLinus Torvalds } 6631da177e4SLinus Torvalds 6640c12295aSPatrick McHardy if (list_empty(&mrt->mfc_unres_queue)) 6651da177e4SLinus Torvalds goto out; 6661da177e4SLinus Torvalds 6671da177e4SLinus Torvalds now = jiffies; 6681da177e4SLinus Torvalds expires = 10*HZ; 6691da177e4SLinus Torvalds 6700c12295aSPatrick McHardy list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) { 6711da177e4SLinus Torvalds if (time_after(c->mfc_un.unres.expires, now)) { 6721da177e4SLinus Torvalds unsigned long interval = c->mfc_un.unres.expires - now; 6731da177e4SLinus Torvalds if (interval < expires) 6741da177e4SLinus Torvalds expires = interval; 6751da177e4SLinus Torvalds continue; 6761da177e4SLinus Torvalds } 6771da177e4SLinus Torvalds 678862465f2SPatrick McHardy list_del(&c->list); 6798cd3ac9fSNicolas Dichtel mroute_netlink_event(mrt, c, RTM_DELROUTE); 6800c12295aSPatrick McHardy ipmr_destroy_unres(mrt, c); 6811da177e4SLinus Torvalds } 6821da177e4SLinus Torvalds 6830c12295aSPatrick McHardy if (!list_empty(&mrt->mfc_unres_queue)) 6840c12295aSPatrick McHardy mod_timer(&mrt->ipmr_expire_timer, jiffies + expires); 6851da177e4SLinus Torvalds 6861da177e4SLinus Torvalds out: 6871da177e4SLinus Torvalds spin_unlock(&mfc_unres_lock); 6881da177e4SLinus Torvalds } 6891da177e4SLinus Torvalds 6901da177e4SLinus Torvalds /* Fill oifs list. It is called under write locked mrt_lock. */ 6911da177e4SLinus Torvalds 6920c12295aSPatrick McHardy static void ipmr_update_thresholds(struct mr_table *mrt, struct mfc_cache *cache, 693d658f8a0SPatrick McHardy unsigned char *ttls) 6941da177e4SLinus Torvalds { 6951da177e4SLinus Torvalds int vifi; 6961da177e4SLinus Torvalds 6971da177e4SLinus Torvalds cache->mfc_un.res.minvif = MAXVIFS; 6981da177e4SLinus Torvalds cache->mfc_un.res.maxvif = 0; 6991da177e4SLinus Torvalds memset(cache->mfc_un.res.ttls, 255, MAXVIFS); 7001da177e4SLinus Torvalds 7010c12295aSPatrick McHardy for (vifi = 0; vifi < mrt->maxvif; vifi++) { 7020c12295aSPatrick McHardy if (VIF_EXISTS(mrt, vifi) && 703cf958ae3SBenjamin Thery ttls[vifi] && ttls[vifi] < 255) { 7041da177e4SLinus Torvalds cache->mfc_un.res.ttls[vifi] = ttls[vifi]; 7051da177e4SLinus Torvalds if (cache->mfc_un.res.minvif > vifi) 7061da177e4SLinus Torvalds cache->mfc_un.res.minvif = vifi; 7071da177e4SLinus Torvalds if (cache->mfc_un.res.maxvif <= vifi) 7081da177e4SLinus Torvalds cache->mfc_un.res.maxvif = vifi + 1; 7091da177e4SLinus Torvalds } 7101da177e4SLinus Torvalds } 7111da177e4SLinus Torvalds } 7121da177e4SLinus Torvalds 7130c12295aSPatrick McHardy static int vif_add(struct net *net, struct mr_table *mrt, 7140c12295aSPatrick McHardy struct vifctl *vifc, int mrtsock) 7151da177e4SLinus Torvalds { 7161da177e4SLinus Torvalds int vifi = vifc->vifc_vifi; 7170c12295aSPatrick McHardy struct vif_device *v = &mrt->vif_table[vifi]; 7181da177e4SLinus Torvalds struct net_device *dev; 7191da177e4SLinus Torvalds struct in_device *in_dev; 720d607032dSWang Chen int err; 7211da177e4SLinus Torvalds 7221da177e4SLinus Torvalds /* Is vif busy ? */ 7230c12295aSPatrick McHardy if (VIF_EXISTS(mrt, vifi)) 7241da177e4SLinus Torvalds return -EADDRINUSE; 7251da177e4SLinus Torvalds 7261da177e4SLinus Torvalds switch (vifc->vifc_flags) { 7271da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM 7281da177e4SLinus Torvalds case VIFF_REGISTER: 7291da177e4SLinus Torvalds /* 7301da177e4SLinus Torvalds * Special Purpose VIF in PIM 7311da177e4SLinus Torvalds * All the packets will be sent to the daemon 7321da177e4SLinus Torvalds */ 7330c12295aSPatrick McHardy if (mrt->mroute_reg_vif_num >= 0) 7341da177e4SLinus Torvalds return -EADDRINUSE; 735f0ad0860SPatrick McHardy dev = ipmr_reg_vif(net, mrt); 7361da177e4SLinus Torvalds if (!dev) 7371da177e4SLinus Torvalds return -ENOBUFS; 738d607032dSWang Chen err = dev_set_allmulti(dev, 1); 739d607032dSWang Chen if (err) { 740d607032dSWang Chen unregister_netdevice(dev); 7417dc00c82SWang Chen dev_put(dev); 742d607032dSWang Chen return err; 743d607032dSWang Chen } 7441da177e4SLinus Torvalds break; 7451da177e4SLinus Torvalds #endif 7461da177e4SLinus Torvalds case VIFF_TUNNEL: 7474feb88e5SBenjamin Thery dev = ipmr_new_tunnel(net, vifc); 7481da177e4SLinus Torvalds if (!dev) 7491da177e4SLinus Torvalds return -ENOBUFS; 750d607032dSWang Chen err = dev_set_allmulti(dev, 1); 751d607032dSWang Chen if (err) { 752d607032dSWang Chen ipmr_del_tunnel(dev, vifc); 7537dc00c82SWang Chen dev_put(dev); 754d607032dSWang Chen return err; 755d607032dSWang Chen } 7561da177e4SLinus Torvalds break; 757ee5e81f0SIlia K 758ee5e81f0SIlia K case VIFF_USE_IFINDEX: 7591da177e4SLinus Torvalds case 0: 760ee5e81f0SIlia K if (vifc->vifc_flags == VIFF_USE_IFINDEX) { 761ee5e81f0SIlia K dev = dev_get_by_index(net, vifc->vifc_lcl_ifindex); 76295ae6b22SEric Dumazet if (dev && __in_dev_get_rtnl(dev) == NULL) { 763ee5e81f0SIlia K dev_put(dev); 764ee5e81f0SIlia K return -EADDRNOTAVAIL; 765ee5e81f0SIlia K } 766a8cb16ddSEric Dumazet } else { 7674feb88e5SBenjamin Thery dev = ip_dev_find(net, vifc->vifc_lcl_addr.s_addr); 768a8cb16ddSEric Dumazet } 7691da177e4SLinus Torvalds if (!dev) 7701da177e4SLinus Torvalds return -EADDRNOTAVAIL; 771d607032dSWang Chen err = dev_set_allmulti(dev, 1); 7727dc00c82SWang Chen if (err) { 7737dc00c82SWang Chen dev_put(dev); 774d607032dSWang Chen return err; 7757dc00c82SWang Chen } 7761da177e4SLinus Torvalds break; 7771da177e4SLinus Torvalds default: 7781da177e4SLinus Torvalds return -EINVAL; 7791da177e4SLinus Torvalds } 7801da177e4SLinus Torvalds 781a8cb16ddSEric Dumazet in_dev = __in_dev_get_rtnl(dev); 782a8cb16ddSEric Dumazet if (!in_dev) { 783d0490cfdSDan Carpenter dev_put(dev); 7841da177e4SLinus Torvalds return -EADDRNOTAVAIL; 785d0490cfdSDan Carpenter } 78642f811b8SHerbert Xu IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)++; 787d67b8c61SNicolas Dichtel inet_netconf_notify_devconf(net, NETCONFA_MC_FORWARDING, dev->ifindex, 788d67b8c61SNicolas Dichtel &in_dev->cnf); 7891da177e4SLinus Torvalds ip_rt_multicast_event(in_dev); 7901da177e4SLinus Torvalds 791a8cb16ddSEric Dumazet /* Fill in the VIF structures */ 792a8cb16ddSEric Dumazet 7931da177e4SLinus Torvalds v->rate_limit = vifc->vifc_rate_limit; 7941da177e4SLinus Torvalds v->local = vifc->vifc_lcl_addr.s_addr; 7951da177e4SLinus Torvalds v->remote = vifc->vifc_rmt_addr.s_addr; 7961da177e4SLinus Torvalds v->flags = vifc->vifc_flags; 7971da177e4SLinus Torvalds if (!mrtsock) 7981da177e4SLinus Torvalds v->flags |= VIFF_STATIC; 7991da177e4SLinus Torvalds v->threshold = vifc->vifc_threshold; 8001da177e4SLinus Torvalds v->bytes_in = 0; 8011da177e4SLinus Torvalds v->bytes_out = 0; 8021da177e4SLinus Torvalds v->pkt_in = 0; 8031da177e4SLinus Torvalds v->pkt_out = 0; 8041da177e4SLinus Torvalds v->link = dev->ifindex; 8051da177e4SLinus Torvalds if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER)) 8061da177e4SLinus Torvalds v->link = dev->iflink; 8071da177e4SLinus Torvalds 8081da177e4SLinus Torvalds /* And finish update writing critical data */ 8091da177e4SLinus Torvalds write_lock_bh(&mrt_lock); 8101da177e4SLinus Torvalds v->dev = dev; 8111da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM 8121da177e4SLinus Torvalds if (v->flags & VIFF_REGISTER) 8130c12295aSPatrick McHardy mrt->mroute_reg_vif_num = vifi; 8141da177e4SLinus Torvalds #endif 8150c12295aSPatrick McHardy if (vifi+1 > mrt->maxvif) 8160c12295aSPatrick McHardy mrt->maxvif = vifi+1; 8171da177e4SLinus Torvalds write_unlock_bh(&mrt_lock); 8181da177e4SLinus Torvalds return 0; 8191da177e4SLinus Torvalds } 8201da177e4SLinus Torvalds 821a8c9486bSEric Dumazet /* called with rcu_read_lock() */ 8220c12295aSPatrick McHardy static struct mfc_cache *ipmr_cache_find(struct mr_table *mrt, 8234feb88e5SBenjamin Thery __be32 origin, 8244feb88e5SBenjamin Thery __be32 mcastgrp) 8251da177e4SLinus Torvalds { 8261da177e4SLinus Torvalds int line = MFC_HASH(mcastgrp, origin); 8271da177e4SLinus Torvalds struct mfc_cache *c; 8281da177e4SLinus Torvalds 829a8c9486bSEric Dumazet list_for_each_entry_rcu(c, &mrt->mfc_cache_array[line], list) { 8301da177e4SLinus Torvalds if (c->mfc_origin == origin && c->mfc_mcastgrp == mcastgrp) 8311da177e4SLinus Torvalds return c; 8321da177e4SLinus Torvalds } 833862465f2SPatrick McHardy return NULL; 834862465f2SPatrick McHardy } 8351da177e4SLinus Torvalds 836660b26dcSNicolas Dichtel /* Look for a (*,*,oif) entry */ 837660b26dcSNicolas Dichtel static struct mfc_cache *ipmr_cache_find_any_parent(struct mr_table *mrt, 838660b26dcSNicolas Dichtel int vifi) 839660b26dcSNicolas Dichtel { 840360eb5daSNicolas Dichtel int line = MFC_HASH(htonl(INADDR_ANY), htonl(INADDR_ANY)); 841660b26dcSNicolas Dichtel struct mfc_cache *c; 842660b26dcSNicolas Dichtel 843660b26dcSNicolas Dichtel list_for_each_entry_rcu(c, &mrt->mfc_cache_array[line], list) 844360eb5daSNicolas Dichtel if (c->mfc_origin == htonl(INADDR_ANY) && 845360eb5daSNicolas Dichtel c->mfc_mcastgrp == htonl(INADDR_ANY) && 846660b26dcSNicolas Dichtel c->mfc_un.res.ttls[vifi] < 255) 847660b26dcSNicolas Dichtel return c; 848660b26dcSNicolas Dichtel 849660b26dcSNicolas Dichtel return NULL; 850660b26dcSNicolas Dichtel } 851660b26dcSNicolas Dichtel 852660b26dcSNicolas Dichtel /* Look for a (*,G) entry */ 853660b26dcSNicolas Dichtel static struct mfc_cache *ipmr_cache_find_any(struct mr_table *mrt, 854660b26dcSNicolas Dichtel __be32 mcastgrp, int vifi) 855660b26dcSNicolas Dichtel { 856360eb5daSNicolas Dichtel int line = MFC_HASH(mcastgrp, htonl(INADDR_ANY)); 857660b26dcSNicolas Dichtel struct mfc_cache *c, *proxy; 858660b26dcSNicolas Dichtel 859360eb5daSNicolas Dichtel if (mcastgrp == htonl(INADDR_ANY)) 860660b26dcSNicolas Dichtel goto skip; 861660b26dcSNicolas Dichtel 862660b26dcSNicolas Dichtel list_for_each_entry_rcu(c, &mrt->mfc_cache_array[line], list) 863360eb5daSNicolas Dichtel if (c->mfc_origin == htonl(INADDR_ANY) && 864660b26dcSNicolas Dichtel c->mfc_mcastgrp == mcastgrp) { 865660b26dcSNicolas Dichtel if (c->mfc_un.res.ttls[vifi] < 255) 866660b26dcSNicolas Dichtel return c; 867660b26dcSNicolas Dichtel 868660b26dcSNicolas Dichtel /* It's ok if the vifi is part of the static tree */ 869660b26dcSNicolas Dichtel proxy = ipmr_cache_find_any_parent(mrt, 870660b26dcSNicolas Dichtel c->mfc_parent); 871660b26dcSNicolas Dichtel if (proxy && proxy->mfc_un.res.ttls[vifi] < 255) 872660b26dcSNicolas Dichtel return c; 873660b26dcSNicolas Dichtel } 874660b26dcSNicolas Dichtel 875660b26dcSNicolas Dichtel skip: 876660b26dcSNicolas Dichtel return ipmr_cache_find_any_parent(mrt, vifi); 877660b26dcSNicolas Dichtel } 878660b26dcSNicolas Dichtel 8791da177e4SLinus Torvalds /* 8801da177e4SLinus Torvalds * Allocate a multicast cache entry 8811da177e4SLinus Torvalds */ 882d658f8a0SPatrick McHardy static struct mfc_cache *ipmr_cache_alloc(void) 8831da177e4SLinus Torvalds { 884c3762229SRobert P. J. Day struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_KERNEL); 885a8c9486bSEric Dumazet 886a8c9486bSEric Dumazet if (c) 8871da177e4SLinus Torvalds c->mfc_un.res.minvif = MAXVIFS; 8881da177e4SLinus Torvalds return c; 8891da177e4SLinus Torvalds } 8901da177e4SLinus Torvalds 891d658f8a0SPatrick McHardy static struct mfc_cache *ipmr_cache_alloc_unres(void) 8921da177e4SLinus Torvalds { 893c3762229SRobert P. J. Day struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_ATOMIC); 894a8c9486bSEric Dumazet 895a8c9486bSEric Dumazet if (c) { 8961da177e4SLinus Torvalds skb_queue_head_init(&c->mfc_un.unres.unresolved); 8971da177e4SLinus Torvalds c->mfc_un.unres.expires = jiffies + 10*HZ; 898a8c9486bSEric Dumazet } 8991da177e4SLinus Torvalds return c; 9001da177e4SLinus Torvalds } 9011da177e4SLinus Torvalds 9021da177e4SLinus Torvalds /* 9031da177e4SLinus Torvalds * A cache entry has gone into a resolved state from queued 9041da177e4SLinus Torvalds */ 9051da177e4SLinus Torvalds 9060c12295aSPatrick McHardy static void ipmr_cache_resolve(struct net *net, struct mr_table *mrt, 9070c12295aSPatrick McHardy struct mfc_cache *uc, struct mfc_cache *c) 9081da177e4SLinus Torvalds { 9091da177e4SLinus Torvalds struct sk_buff *skb; 9109ef1d4c7SPatrick McHardy struct nlmsgerr *e; 9111da177e4SLinus Torvalds 912a8cb16ddSEric Dumazet /* Play the pending entries through our router */ 9131da177e4SLinus Torvalds 9141da177e4SLinus Torvalds while ((skb = __skb_dequeue(&uc->mfc_un.unres.unresolved))) { 915eddc9ec5SArnaldo Carvalho de Melo if (ip_hdr(skb)->version == 0) { 9161da177e4SLinus Torvalds struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr)); 9171da177e4SLinus Torvalds 918573ce260SHong zhi guo if (__ipmr_fill_mroute(mrt, skb, c, nlmsg_data(nlh)) > 0) { 919a8cb16ddSEric Dumazet nlh->nlmsg_len = skb_tail_pointer(skb) - 920a8cb16ddSEric Dumazet (u8 *)nlh; 9211da177e4SLinus Torvalds } else { 9221da177e4SLinus Torvalds nlh->nlmsg_type = NLMSG_ERROR; 923573ce260SHong zhi guo nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr)); 9241da177e4SLinus Torvalds skb_trim(skb, nlh->nlmsg_len); 925573ce260SHong zhi guo e = nlmsg_data(nlh); 9269ef1d4c7SPatrick McHardy e->error = -EMSGSIZE; 9279ef1d4c7SPatrick McHardy memset(&e->msg, 0, sizeof(e->msg)); 9281da177e4SLinus Torvalds } 9292942e900SThomas Graf 93015e47304SEric W. Biederman rtnl_unicast(skb, net, NETLINK_CB(skb).portid); 931a8cb16ddSEric Dumazet } else { 9320c12295aSPatrick McHardy ip_mr_forward(net, mrt, skb, c, 0); 9331da177e4SLinus Torvalds } 9341da177e4SLinus Torvalds } 935a8cb16ddSEric Dumazet } 9361da177e4SLinus Torvalds 9371da177e4SLinus Torvalds /* 9381da177e4SLinus Torvalds * Bounce a cache query up to mrouted. We could use netlink for this but mrouted 9391da177e4SLinus Torvalds * expects the following bizarre scheme. 9401da177e4SLinus Torvalds * 9411da177e4SLinus Torvalds * Called under mrt_lock. 9421da177e4SLinus Torvalds */ 9431da177e4SLinus Torvalds 9440c12295aSPatrick McHardy static int ipmr_cache_report(struct mr_table *mrt, 9454feb88e5SBenjamin Thery struct sk_buff *pkt, vifi_t vifi, int assert) 9461da177e4SLinus Torvalds { 9471da177e4SLinus Torvalds struct sk_buff *skb; 948c9bdd4b5SArnaldo Carvalho de Melo const int ihl = ip_hdrlen(pkt); 9491da177e4SLinus Torvalds struct igmphdr *igmp; 9501da177e4SLinus Torvalds struct igmpmsg *msg; 9514c968709SEric Dumazet struct sock *mroute_sk; 9521da177e4SLinus Torvalds int ret; 9531da177e4SLinus Torvalds 9541da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM 9551da177e4SLinus Torvalds if (assert == IGMPMSG_WHOLEPKT) 9561da177e4SLinus Torvalds skb = skb_realloc_headroom(pkt, sizeof(struct iphdr)); 9571da177e4SLinus Torvalds else 9581da177e4SLinus Torvalds #endif 9591da177e4SLinus Torvalds skb = alloc_skb(128, GFP_ATOMIC); 9601da177e4SLinus Torvalds 9611da177e4SLinus Torvalds if (!skb) 9621da177e4SLinus Torvalds return -ENOBUFS; 9631da177e4SLinus Torvalds 9641da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM 9651da177e4SLinus Torvalds if (assert == IGMPMSG_WHOLEPKT) { 9661da177e4SLinus Torvalds /* Ugly, but we have no choice with this interface. 967a8cb16ddSEric Dumazet * Duplicate old header, fix ihl, length etc. 968a8cb16ddSEric Dumazet * And all this only to mangle msg->im_msgtype and 969a8cb16ddSEric Dumazet * to set msg->im_mbz to "mbz" :-) 9701da177e4SLinus Torvalds */ 971878c8145SArnaldo Carvalho de Melo skb_push(skb, sizeof(struct iphdr)); 972878c8145SArnaldo Carvalho de Melo skb_reset_network_header(skb); 973badff6d0SArnaldo Carvalho de Melo skb_reset_transport_header(skb); 9740272ffc4SArnaldo Carvalho de Melo msg = (struct igmpmsg *)skb_network_header(skb); 975d56f90a7SArnaldo Carvalho de Melo memcpy(msg, skb_network_header(pkt), sizeof(struct iphdr)); 9761da177e4SLinus Torvalds msg->im_msgtype = IGMPMSG_WHOLEPKT; 9771da177e4SLinus Torvalds msg->im_mbz = 0; 9780c12295aSPatrick McHardy msg->im_vif = mrt->mroute_reg_vif_num; 979eddc9ec5SArnaldo Carvalho de Melo ip_hdr(skb)->ihl = sizeof(struct iphdr) >> 2; 980eddc9ec5SArnaldo Carvalho de Melo ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(pkt)->tot_len) + 981eddc9ec5SArnaldo Carvalho de Melo sizeof(struct iphdr)); 9821da177e4SLinus Torvalds } else 9831da177e4SLinus Torvalds #endif 9841da177e4SLinus Torvalds { 9851da177e4SLinus Torvalds 986a8cb16ddSEric Dumazet /* Copy the IP header */ 9871da177e4SLinus Torvalds 98830f3a40fSCong Wang skb_set_network_header(skb, skb->len); 989ddc7b8e3SArnaldo Carvalho de Melo skb_put(skb, ihl); 99027d7ff46SArnaldo Carvalho de Melo skb_copy_to_linear_data(skb, pkt->data, ihl); 991eddc9ec5SArnaldo Carvalho de Melo ip_hdr(skb)->protocol = 0; /* Flag to the kernel this is a route add */ 992eddc9ec5SArnaldo Carvalho de Melo msg = (struct igmpmsg *)skb_network_header(skb); 9931da177e4SLinus Torvalds msg->im_vif = vifi; 994adf30907SEric Dumazet skb_dst_set(skb, dst_clone(skb_dst(pkt))); 9951da177e4SLinus Torvalds 996a8cb16ddSEric Dumazet /* Add our header */ 9971da177e4SLinus Torvalds 9981da177e4SLinus Torvalds igmp = (struct igmphdr *)skb_put(skb, sizeof(struct igmphdr)); 9991da177e4SLinus Torvalds igmp->type = 10001da177e4SLinus Torvalds msg->im_msgtype = assert; 10011da177e4SLinus Torvalds igmp->code = 0; 1002eddc9ec5SArnaldo Carvalho de Melo ip_hdr(skb)->tot_len = htons(skb->len); /* Fix the length */ 1003b0e380b1SArnaldo Carvalho de Melo skb->transport_header = skb->network_header; 10041da177e4SLinus Torvalds } 10051da177e4SLinus Torvalds 10064c968709SEric Dumazet rcu_read_lock(); 10074c968709SEric Dumazet mroute_sk = rcu_dereference(mrt->mroute_sk); 10084c968709SEric Dumazet if (mroute_sk == NULL) { 10094c968709SEric Dumazet rcu_read_unlock(); 10101da177e4SLinus Torvalds kfree_skb(skb); 10111da177e4SLinus Torvalds return -EINVAL; 10121da177e4SLinus Torvalds } 10131da177e4SLinus Torvalds 1014a8cb16ddSEric Dumazet /* Deliver to mrouted */ 1015a8cb16ddSEric Dumazet 10164c968709SEric Dumazet ret = sock_queue_rcv_skb(mroute_sk, skb); 10174c968709SEric Dumazet rcu_read_unlock(); 101870a269e6SBenjamin Thery if (ret < 0) { 1019e87cc472SJoe Perches net_warn_ratelimited("mroute: pending queue full, dropping entries\n"); 10201da177e4SLinus Torvalds kfree_skb(skb); 10211da177e4SLinus Torvalds } 10221da177e4SLinus Torvalds 10231da177e4SLinus Torvalds return ret; 10241da177e4SLinus Torvalds } 10251da177e4SLinus Torvalds 10261da177e4SLinus Torvalds /* 10271da177e4SLinus Torvalds * Queue a packet for resolution. It gets locked cache entry! 10281da177e4SLinus Torvalds */ 10291da177e4SLinus Torvalds 10301da177e4SLinus Torvalds static int 10310c12295aSPatrick McHardy ipmr_cache_unresolved(struct mr_table *mrt, vifi_t vifi, struct sk_buff *skb) 10321da177e4SLinus Torvalds { 1033862465f2SPatrick McHardy bool found = false; 10341da177e4SLinus Torvalds int err; 10351da177e4SLinus Torvalds struct mfc_cache *c; 1036eddc9ec5SArnaldo Carvalho de Melo const struct iphdr *iph = ip_hdr(skb); 10371da177e4SLinus Torvalds 10381da177e4SLinus Torvalds spin_lock_bh(&mfc_unres_lock); 10390c12295aSPatrick McHardy list_for_each_entry(c, &mrt->mfc_unres_queue, list) { 1040e258beb2SPatrick McHardy if (c->mfc_mcastgrp == iph->daddr && 1041862465f2SPatrick McHardy c->mfc_origin == iph->saddr) { 1042862465f2SPatrick McHardy found = true; 10431da177e4SLinus Torvalds break; 10441da177e4SLinus Torvalds } 1045862465f2SPatrick McHardy } 10461da177e4SLinus Torvalds 1047862465f2SPatrick McHardy if (!found) { 1048a8cb16ddSEric Dumazet /* Create a new entry if allowable */ 10491da177e4SLinus Torvalds 10500c12295aSPatrick McHardy if (atomic_read(&mrt->cache_resolve_queue_len) >= 10 || 1051d658f8a0SPatrick McHardy (c = ipmr_cache_alloc_unres()) == NULL) { 10521da177e4SLinus Torvalds spin_unlock_bh(&mfc_unres_lock); 10531da177e4SLinus Torvalds 10541da177e4SLinus Torvalds kfree_skb(skb); 10551da177e4SLinus Torvalds return -ENOBUFS; 10561da177e4SLinus Torvalds } 10571da177e4SLinus Torvalds 1058a8cb16ddSEric Dumazet /* Fill in the new cache entry */ 1059a8cb16ddSEric Dumazet 10601da177e4SLinus Torvalds c->mfc_parent = -1; 1061eddc9ec5SArnaldo Carvalho de Melo c->mfc_origin = iph->saddr; 1062eddc9ec5SArnaldo Carvalho de Melo c->mfc_mcastgrp = iph->daddr; 10631da177e4SLinus Torvalds 1064a8cb16ddSEric Dumazet /* Reflect first query at mrouted. */ 1065a8cb16ddSEric Dumazet 10660c12295aSPatrick McHardy err = ipmr_cache_report(mrt, skb, vifi, IGMPMSG_NOCACHE); 10674feb88e5SBenjamin Thery if (err < 0) { 10681da177e4SLinus Torvalds /* If the report failed throw the cache entry 10691da177e4SLinus Torvalds out - Brad Parker 10701da177e4SLinus Torvalds */ 10711da177e4SLinus Torvalds spin_unlock_bh(&mfc_unres_lock); 10721da177e4SLinus Torvalds 10735c0a66f5SBenjamin Thery ipmr_cache_free(c); 10741da177e4SLinus Torvalds kfree_skb(skb); 10751da177e4SLinus Torvalds return err; 10761da177e4SLinus Torvalds } 10771da177e4SLinus Torvalds 10780c12295aSPatrick McHardy atomic_inc(&mrt->cache_resolve_queue_len); 10790c12295aSPatrick McHardy list_add(&c->list, &mrt->mfc_unres_queue); 10808cd3ac9fSNicolas Dichtel mroute_netlink_event(mrt, c, RTM_NEWROUTE); 10811da177e4SLinus Torvalds 1082278554bdSDavid S. Miller if (atomic_read(&mrt->cache_resolve_queue_len) == 1) 10830c12295aSPatrick McHardy mod_timer(&mrt->ipmr_expire_timer, c->mfc_un.unres.expires); 10841da177e4SLinus Torvalds } 10851da177e4SLinus Torvalds 1086a8cb16ddSEric Dumazet /* See if we can append the packet */ 1087a8cb16ddSEric Dumazet 10881da177e4SLinus Torvalds if (c->mfc_un.unres.unresolved.qlen > 3) { 10891da177e4SLinus Torvalds kfree_skb(skb); 10901da177e4SLinus Torvalds err = -ENOBUFS; 10911da177e4SLinus Torvalds } else { 10921da177e4SLinus Torvalds skb_queue_tail(&c->mfc_un.unres.unresolved, skb); 10931da177e4SLinus Torvalds err = 0; 10941da177e4SLinus Torvalds } 10951da177e4SLinus Torvalds 10961da177e4SLinus Torvalds spin_unlock_bh(&mfc_unres_lock); 10971da177e4SLinus Torvalds return err; 10981da177e4SLinus Torvalds } 10991da177e4SLinus Torvalds 11001da177e4SLinus Torvalds /* 11011da177e4SLinus Torvalds * MFC cache manipulation by user space mroute daemon 11021da177e4SLinus Torvalds */ 11031da177e4SLinus Torvalds 1104660b26dcSNicolas Dichtel static int ipmr_mfc_delete(struct mr_table *mrt, struct mfcctl *mfc, int parent) 11051da177e4SLinus Torvalds { 11061da177e4SLinus Torvalds int line; 1107862465f2SPatrick McHardy struct mfc_cache *c, *next; 11081da177e4SLinus Torvalds 11091da177e4SLinus Torvalds line = MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr); 11101da177e4SLinus Torvalds 11110c12295aSPatrick McHardy list_for_each_entry_safe(c, next, &mrt->mfc_cache_array[line], list) { 11121da177e4SLinus Torvalds if (c->mfc_origin == mfc->mfcc_origin.s_addr && 1113660b26dcSNicolas Dichtel c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr && 1114660b26dcSNicolas Dichtel (parent == -1 || parent == c->mfc_parent)) { 1115a8c9486bSEric Dumazet list_del_rcu(&c->list); 11168cd3ac9fSNicolas Dichtel mroute_netlink_event(mrt, c, RTM_DELROUTE); 11175c0a66f5SBenjamin Thery ipmr_cache_free(c); 11181da177e4SLinus Torvalds return 0; 11191da177e4SLinus Torvalds } 11201da177e4SLinus Torvalds } 11211da177e4SLinus Torvalds return -ENOENT; 11221da177e4SLinus Torvalds } 11231da177e4SLinus Torvalds 11240c12295aSPatrick McHardy static int ipmr_mfc_add(struct net *net, struct mr_table *mrt, 1125660b26dcSNicolas Dichtel struct mfcctl *mfc, int mrtsock, int parent) 11261da177e4SLinus Torvalds { 1127862465f2SPatrick McHardy bool found = false; 11281da177e4SLinus Torvalds int line; 1129862465f2SPatrick McHardy struct mfc_cache *uc, *c; 11301da177e4SLinus Torvalds 1131a50436f2SPatrick McHardy if (mfc->mfcc_parent >= MAXVIFS) 1132a50436f2SPatrick McHardy return -ENFILE; 1133a50436f2SPatrick McHardy 11341da177e4SLinus Torvalds line = MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr); 11351da177e4SLinus Torvalds 11360c12295aSPatrick McHardy list_for_each_entry(c, &mrt->mfc_cache_array[line], list) { 11371da177e4SLinus Torvalds if (c->mfc_origin == mfc->mfcc_origin.s_addr && 1138660b26dcSNicolas Dichtel c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr && 1139660b26dcSNicolas Dichtel (parent == -1 || parent == c->mfc_parent)) { 1140862465f2SPatrick McHardy found = true; 11411da177e4SLinus Torvalds break; 11421da177e4SLinus Torvalds } 1143862465f2SPatrick McHardy } 11441da177e4SLinus Torvalds 1145862465f2SPatrick McHardy if (found) { 11461da177e4SLinus Torvalds write_lock_bh(&mrt_lock); 11471da177e4SLinus Torvalds c->mfc_parent = mfc->mfcc_parent; 11480c12295aSPatrick McHardy ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls); 11491da177e4SLinus Torvalds if (!mrtsock) 11501da177e4SLinus Torvalds c->mfc_flags |= MFC_STATIC; 11511da177e4SLinus Torvalds write_unlock_bh(&mrt_lock); 11528cd3ac9fSNicolas Dichtel mroute_netlink_event(mrt, c, RTM_NEWROUTE); 11531da177e4SLinus Torvalds return 0; 11541da177e4SLinus Torvalds } 11551da177e4SLinus Torvalds 1156360eb5daSNicolas Dichtel if (mfc->mfcc_mcastgrp.s_addr != htonl(INADDR_ANY) && 1157660b26dcSNicolas Dichtel !ipv4_is_multicast(mfc->mfcc_mcastgrp.s_addr)) 11581da177e4SLinus Torvalds return -EINVAL; 11591da177e4SLinus Torvalds 1160d658f8a0SPatrick McHardy c = ipmr_cache_alloc(); 11611da177e4SLinus Torvalds if (c == NULL) 11621da177e4SLinus Torvalds return -ENOMEM; 11631da177e4SLinus Torvalds 11641da177e4SLinus Torvalds c->mfc_origin = mfc->mfcc_origin.s_addr; 11651da177e4SLinus Torvalds c->mfc_mcastgrp = mfc->mfcc_mcastgrp.s_addr; 11661da177e4SLinus Torvalds c->mfc_parent = mfc->mfcc_parent; 11670c12295aSPatrick McHardy ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls); 11681da177e4SLinus Torvalds if (!mrtsock) 11691da177e4SLinus Torvalds c->mfc_flags |= MFC_STATIC; 11701da177e4SLinus Torvalds 1171a8c9486bSEric Dumazet list_add_rcu(&c->list, &mrt->mfc_cache_array[line]); 11721da177e4SLinus Torvalds 11731da177e4SLinus Torvalds /* 11741da177e4SLinus Torvalds * Check to see if we resolved a queued list. If so we 11751da177e4SLinus Torvalds * need to send on the frames and tidy up. 11761da177e4SLinus Torvalds */ 1177b0ebb739SPatrick McHardy found = false; 11781da177e4SLinus Torvalds spin_lock_bh(&mfc_unres_lock); 11790c12295aSPatrick McHardy list_for_each_entry(uc, &mrt->mfc_unres_queue, list) { 1180e258beb2SPatrick McHardy if (uc->mfc_origin == c->mfc_origin && 11811da177e4SLinus Torvalds uc->mfc_mcastgrp == c->mfc_mcastgrp) { 1182862465f2SPatrick McHardy list_del(&uc->list); 11830c12295aSPatrick McHardy atomic_dec(&mrt->cache_resolve_queue_len); 1184b0ebb739SPatrick McHardy found = true; 11851da177e4SLinus Torvalds break; 11861da177e4SLinus Torvalds } 11871da177e4SLinus Torvalds } 11880c12295aSPatrick McHardy if (list_empty(&mrt->mfc_unres_queue)) 11890c12295aSPatrick McHardy del_timer(&mrt->ipmr_expire_timer); 11901da177e4SLinus Torvalds spin_unlock_bh(&mfc_unres_lock); 11911da177e4SLinus Torvalds 1192b0ebb739SPatrick McHardy if (found) { 11930c12295aSPatrick McHardy ipmr_cache_resolve(net, mrt, uc, c); 11945c0a66f5SBenjamin Thery ipmr_cache_free(uc); 11951da177e4SLinus Torvalds } 11968cd3ac9fSNicolas Dichtel mroute_netlink_event(mrt, c, RTM_NEWROUTE); 11971da177e4SLinus Torvalds return 0; 11981da177e4SLinus Torvalds } 11991da177e4SLinus Torvalds 12001da177e4SLinus Torvalds /* 12011da177e4SLinus Torvalds * Close the multicast socket, and clear the vif tables etc 12021da177e4SLinus Torvalds */ 12031da177e4SLinus Torvalds 12040c12295aSPatrick McHardy static void mroute_clean_tables(struct mr_table *mrt) 12051da177e4SLinus Torvalds { 12061da177e4SLinus Torvalds int i; 1207d17fa6faSEric Dumazet LIST_HEAD(list); 1208862465f2SPatrick McHardy struct mfc_cache *c, *next; 12091da177e4SLinus Torvalds 1210a8cb16ddSEric Dumazet /* Shut down all active vif entries */ 1211a8cb16ddSEric Dumazet 12120c12295aSPatrick McHardy for (i = 0; i < mrt->maxvif; i++) { 12130c12295aSPatrick McHardy if (!(mrt->vif_table[i].flags & VIFF_STATIC)) 12140c12295aSPatrick McHardy vif_delete(mrt, i, 0, &list); 12151da177e4SLinus Torvalds } 1216d17fa6faSEric Dumazet unregister_netdevice_many(&list); 12171da177e4SLinus Torvalds 1218a8cb16ddSEric Dumazet /* Wipe the cache */ 1219a8cb16ddSEric Dumazet 12201da177e4SLinus Torvalds for (i = 0; i < MFC_LINES; i++) { 12210c12295aSPatrick McHardy list_for_each_entry_safe(c, next, &mrt->mfc_cache_array[i], list) { 1222862465f2SPatrick McHardy if (c->mfc_flags & MFC_STATIC) 12231da177e4SLinus Torvalds continue; 1224a8c9486bSEric Dumazet list_del_rcu(&c->list); 12258cd3ac9fSNicolas Dichtel mroute_netlink_event(mrt, c, RTM_DELROUTE); 12265c0a66f5SBenjamin Thery ipmr_cache_free(c); 12271da177e4SLinus Torvalds } 12281da177e4SLinus Torvalds } 12291da177e4SLinus Torvalds 12300c12295aSPatrick McHardy if (atomic_read(&mrt->cache_resolve_queue_len) != 0) { 12311da177e4SLinus Torvalds spin_lock_bh(&mfc_unres_lock); 12320c12295aSPatrick McHardy list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) { 1233862465f2SPatrick McHardy list_del(&c->list); 12348cd3ac9fSNicolas Dichtel mroute_netlink_event(mrt, c, RTM_DELROUTE); 12350c12295aSPatrick McHardy ipmr_destroy_unres(mrt, c); 12361da177e4SLinus Torvalds } 12371da177e4SLinus Torvalds spin_unlock_bh(&mfc_unres_lock); 12381da177e4SLinus Torvalds } 12391da177e4SLinus Torvalds } 12401da177e4SLinus Torvalds 12414c968709SEric Dumazet /* called from ip_ra_control(), before an RCU grace period, 12424c968709SEric Dumazet * we dont need to call synchronize_rcu() here 12434c968709SEric Dumazet */ 12441da177e4SLinus Torvalds static void mrtsock_destruct(struct sock *sk) 12451da177e4SLinus Torvalds { 12464feb88e5SBenjamin Thery struct net *net = sock_net(sk); 1247f0ad0860SPatrick McHardy struct mr_table *mrt; 12484feb88e5SBenjamin Thery 12491da177e4SLinus Torvalds rtnl_lock(); 1250f0ad0860SPatrick McHardy ipmr_for_each_table(mrt, net) { 12514c968709SEric Dumazet if (sk == rtnl_dereference(mrt->mroute_sk)) { 12524feb88e5SBenjamin Thery IPV4_DEVCONF_ALL(net, MC_FORWARDING)--; 1253d67b8c61SNicolas Dichtel inet_netconf_notify_devconf(net, NETCONFA_MC_FORWARDING, 1254d67b8c61SNicolas Dichtel NETCONFA_IFINDEX_ALL, 1255d67b8c61SNicolas Dichtel net->ipv4.devconf_all); 1256a9b3cd7fSStephen Hemminger RCU_INIT_POINTER(mrt->mroute_sk, NULL); 12570c12295aSPatrick McHardy mroute_clean_tables(mrt); 12581da177e4SLinus Torvalds } 12591da177e4SLinus Torvalds } 12601da177e4SLinus Torvalds rtnl_unlock(); 12611da177e4SLinus Torvalds } 12621da177e4SLinus Torvalds 12631da177e4SLinus Torvalds /* 12641da177e4SLinus Torvalds * Socket options and virtual interface manipulation. The whole 12651da177e4SLinus Torvalds * virtual interface system is a complete heap, but unfortunately 12661da177e4SLinus Torvalds * that's how BSD mrouted happens to think. Maybe one day with a proper 12671da177e4SLinus Torvalds * MOSPF/PIM router set up we can clean this up. 12681da177e4SLinus Torvalds */ 12691da177e4SLinus Torvalds 1270b7058842SDavid S. Miller int ip_mroute_setsockopt(struct sock *sk, int optname, char __user *optval, unsigned int optlen) 12711da177e4SLinus Torvalds { 1272660b26dcSNicolas Dichtel int ret, parent = 0; 12731da177e4SLinus Torvalds struct vifctl vif; 12741da177e4SLinus Torvalds struct mfcctl mfc; 12754feb88e5SBenjamin Thery struct net *net = sock_net(sk); 1276f0ad0860SPatrick McHardy struct mr_table *mrt; 1277f0ad0860SPatrick McHardy 12785e1859fbSEric Dumazet if (sk->sk_type != SOCK_RAW || 12795e1859fbSEric Dumazet inet_sk(sk)->inet_num != IPPROTO_IGMP) 12805e1859fbSEric Dumazet return -EOPNOTSUPP; 12815e1859fbSEric Dumazet 1282f0ad0860SPatrick McHardy mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT); 1283f0ad0860SPatrick McHardy if (mrt == NULL) 1284f0ad0860SPatrick McHardy return -ENOENT; 12851da177e4SLinus Torvalds 1286132adf54SStephen Hemminger if (optname != MRT_INIT) { 128733d480ceSEric Dumazet if (sk != rcu_access_pointer(mrt->mroute_sk) && 128852e804c6SEric W. Biederman !ns_capable(net->user_ns, CAP_NET_ADMIN)) 12891da177e4SLinus Torvalds return -EACCES; 12901da177e4SLinus Torvalds } 12911da177e4SLinus Torvalds 1292132adf54SStephen Hemminger switch (optname) { 12931da177e4SLinus Torvalds case MRT_INIT: 12941da177e4SLinus Torvalds if (optlen != sizeof(int)) 12955e1859fbSEric Dumazet return -EINVAL; 12961da177e4SLinus Torvalds 12971da177e4SLinus Torvalds rtnl_lock(); 12984c968709SEric Dumazet if (rtnl_dereference(mrt->mroute_sk)) { 12991da177e4SLinus Torvalds rtnl_unlock(); 13001da177e4SLinus Torvalds return -EADDRINUSE; 13011da177e4SLinus Torvalds } 13021da177e4SLinus Torvalds 13031da177e4SLinus Torvalds ret = ip_ra_control(sk, 1, mrtsock_destruct); 13041da177e4SLinus Torvalds if (ret == 0) { 1305cf778b00SEric Dumazet rcu_assign_pointer(mrt->mroute_sk, sk); 13064feb88e5SBenjamin Thery IPV4_DEVCONF_ALL(net, MC_FORWARDING)++; 1307d67b8c61SNicolas Dichtel inet_netconf_notify_devconf(net, NETCONFA_MC_FORWARDING, 1308d67b8c61SNicolas Dichtel NETCONFA_IFINDEX_ALL, 1309d67b8c61SNicolas Dichtel net->ipv4.devconf_all); 13101da177e4SLinus Torvalds } 13111da177e4SLinus Torvalds rtnl_unlock(); 13121da177e4SLinus Torvalds return ret; 13131da177e4SLinus Torvalds case MRT_DONE: 131433d480ceSEric Dumazet if (sk != rcu_access_pointer(mrt->mroute_sk)) 13151da177e4SLinus Torvalds return -EACCES; 13161da177e4SLinus Torvalds return ip_ra_control(sk, 0, NULL); 13171da177e4SLinus Torvalds case MRT_ADD_VIF: 13181da177e4SLinus Torvalds case MRT_DEL_VIF: 13191da177e4SLinus Torvalds if (optlen != sizeof(vif)) 13201da177e4SLinus Torvalds return -EINVAL; 13211da177e4SLinus Torvalds if (copy_from_user(&vif, optval, sizeof(vif))) 13221da177e4SLinus Torvalds return -EFAULT; 13231da177e4SLinus Torvalds if (vif.vifc_vifi >= MAXVIFS) 13241da177e4SLinus Torvalds return -ENFILE; 13251da177e4SLinus Torvalds rtnl_lock(); 13261da177e4SLinus Torvalds if (optname == MRT_ADD_VIF) { 13274c968709SEric Dumazet ret = vif_add(net, mrt, &vif, 13284c968709SEric Dumazet sk == rtnl_dereference(mrt->mroute_sk)); 13291da177e4SLinus Torvalds } else { 13300c12295aSPatrick McHardy ret = vif_delete(mrt, vif.vifc_vifi, 0, NULL); 13311da177e4SLinus Torvalds } 13321da177e4SLinus Torvalds rtnl_unlock(); 13331da177e4SLinus Torvalds return ret; 13341da177e4SLinus Torvalds 13351da177e4SLinus Torvalds /* 13361da177e4SLinus Torvalds * Manipulate the forwarding caches. These live 13371da177e4SLinus Torvalds * in a sort of kernel/user symbiosis. 13381da177e4SLinus Torvalds */ 13391da177e4SLinus Torvalds case MRT_ADD_MFC: 13401da177e4SLinus Torvalds case MRT_DEL_MFC: 1341660b26dcSNicolas Dichtel parent = -1; 1342660b26dcSNicolas Dichtel case MRT_ADD_MFC_PROXY: 1343660b26dcSNicolas Dichtel case MRT_DEL_MFC_PROXY: 13441da177e4SLinus Torvalds if (optlen != sizeof(mfc)) 13451da177e4SLinus Torvalds return -EINVAL; 13461da177e4SLinus Torvalds if (copy_from_user(&mfc, optval, sizeof(mfc))) 13471da177e4SLinus Torvalds return -EFAULT; 1348660b26dcSNicolas Dichtel if (parent == 0) 1349660b26dcSNicolas Dichtel parent = mfc.mfcc_parent; 13501da177e4SLinus Torvalds rtnl_lock(); 1351660b26dcSNicolas Dichtel if (optname == MRT_DEL_MFC || optname == MRT_DEL_MFC_PROXY) 1352660b26dcSNicolas Dichtel ret = ipmr_mfc_delete(mrt, &mfc, parent); 13531da177e4SLinus Torvalds else 13544c968709SEric Dumazet ret = ipmr_mfc_add(net, mrt, &mfc, 1355660b26dcSNicolas Dichtel sk == rtnl_dereference(mrt->mroute_sk), 1356660b26dcSNicolas Dichtel parent); 13571da177e4SLinus Torvalds rtnl_unlock(); 13581da177e4SLinus Torvalds return ret; 13591da177e4SLinus Torvalds /* 13601da177e4SLinus Torvalds * Control PIM assert. 13611da177e4SLinus Torvalds */ 13621da177e4SLinus Torvalds case MRT_ASSERT: 13631da177e4SLinus Torvalds { 13641da177e4SLinus Torvalds int v; 13655e1859fbSEric Dumazet if (optlen != sizeof(v)) 13665e1859fbSEric Dumazet return -EINVAL; 13671da177e4SLinus Torvalds if (get_user(v, (int __user *)optval)) 13681da177e4SLinus Torvalds return -EFAULT; 136953d6841dSJoe Perches mrt->mroute_do_assert = v; 13701da177e4SLinus Torvalds return 0; 13711da177e4SLinus Torvalds } 13721da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM 13731da177e4SLinus Torvalds case MRT_PIM: 13741da177e4SLinus Torvalds { 1375ba93ef74SStephen Hemminger int v; 1376ba93ef74SStephen Hemminger 13775e1859fbSEric Dumazet if (optlen != sizeof(v)) 13785e1859fbSEric Dumazet return -EINVAL; 13791da177e4SLinus Torvalds if (get_user(v, (int __user *)optval)) 13801da177e4SLinus Torvalds return -EFAULT; 13815e1859fbSEric Dumazet v = !!v; 1382ba93ef74SStephen Hemminger 13831da177e4SLinus Torvalds rtnl_lock(); 13841da177e4SLinus Torvalds ret = 0; 13850c12295aSPatrick McHardy if (v != mrt->mroute_do_pim) { 13860c12295aSPatrick McHardy mrt->mroute_do_pim = v; 13870c12295aSPatrick McHardy mrt->mroute_do_assert = v; 13881da177e4SLinus Torvalds } 13891da177e4SLinus Torvalds rtnl_unlock(); 13901da177e4SLinus Torvalds return ret; 13911da177e4SLinus Torvalds } 13921da177e4SLinus Torvalds #endif 1393f0ad0860SPatrick McHardy #ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES 1394f0ad0860SPatrick McHardy case MRT_TABLE: 1395f0ad0860SPatrick McHardy { 1396f0ad0860SPatrick McHardy u32 v; 1397f0ad0860SPatrick McHardy 1398f0ad0860SPatrick McHardy if (optlen != sizeof(u32)) 1399f0ad0860SPatrick McHardy return -EINVAL; 1400f0ad0860SPatrick McHardy if (get_user(v, (u32 __user *)optval)) 1401f0ad0860SPatrick McHardy return -EFAULT; 1402f0ad0860SPatrick McHardy 1403b49d3c1eSEric Dumazet /* "pimreg%u" should not exceed 16 bytes (IFNAMSIZ) */ 1404b49d3c1eSEric Dumazet if (v != RT_TABLE_DEFAULT && v >= 1000000000) 1405b49d3c1eSEric Dumazet return -EINVAL; 1406b49d3c1eSEric Dumazet 1407f0ad0860SPatrick McHardy rtnl_lock(); 1408f0ad0860SPatrick McHardy ret = 0; 14094c968709SEric Dumazet if (sk == rtnl_dereference(mrt->mroute_sk)) { 14104c968709SEric Dumazet ret = -EBUSY; 14114c968709SEric Dumazet } else { 1412f0ad0860SPatrick McHardy if (!ipmr_new_table(net, v)) 1413f0ad0860SPatrick McHardy ret = -ENOMEM; 14145e1859fbSEric Dumazet else 1415f0ad0860SPatrick McHardy raw_sk(sk)->ipmr_table = v; 14164c968709SEric Dumazet } 1417f0ad0860SPatrick McHardy rtnl_unlock(); 1418f0ad0860SPatrick McHardy return ret; 1419f0ad0860SPatrick McHardy } 1420f0ad0860SPatrick McHardy #endif 14211da177e4SLinus Torvalds /* 14221da177e4SLinus Torvalds * Spurious command, or MRT_VERSION which you cannot 14231da177e4SLinus Torvalds * set. 14241da177e4SLinus Torvalds */ 14251da177e4SLinus Torvalds default: 14261da177e4SLinus Torvalds return -ENOPROTOOPT; 14271da177e4SLinus Torvalds } 14281da177e4SLinus Torvalds } 14291da177e4SLinus Torvalds 14301da177e4SLinus Torvalds /* 14311da177e4SLinus Torvalds * Getsock opt support for the multicast routing system. 14321da177e4SLinus Torvalds */ 14331da177e4SLinus Torvalds 14341da177e4SLinus Torvalds int ip_mroute_getsockopt(struct sock *sk, int optname, char __user *optval, int __user *optlen) 14351da177e4SLinus Torvalds { 14361da177e4SLinus Torvalds int olr; 14371da177e4SLinus Torvalds int val; 14384feb88e5SBenjamin Thery struct net *net = sock_net(sk); 1439f0ad0860SPatrick McHardy struct mr_table *mrt; 1440f0ad0860SPatrick McHardy 14415e1859fbSEric Dumazet if (sk->sk_type != SOCK_RAW || 14425e1859fbSEric Dumazet inet_sk(sk)->inet_num != IPPROTO_IGMP) 14435e1859fbSEric Dumazet return -EOPNOTSUPP; 14445e1859fbSEric Dumazet 1445f0ad0860SPatrick McHardy mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT); 1446f0ad0860SPatrick McHardy if (mrt == NULL) 1447f0ad0860SPatrick McHardy return -ENOENT; 14481da177e4SLinus Torvalds 14491da177e4SLinus Torvalds if (optname != MRT_VERSION && 14501da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM 14511da177e4SLinus Torvalds optname != MRT_PIM && 14521da177e4SLinus Torvalds #endif 14531da177e4SLinus Torvalds optname != MRT_ASSERT) 14541da177e4SLinus Torvalds return -ENOPROTOOPT; 14551da177e4SLinus Torvalds 14561da177e4SLinus Torvalds if (get_user(olr, optlen)) 14571da177e4SLinus Torvalds return -EFAULT; 14581da177e4SLinus Torvalds 14591da177e4SLinus Torvalds olr = min_t(unsigned int, olr, sizeof(int)); 14601da177e4SLinus Torvalds if (olr < 0) 14611da177e4SLinus Torvalds return -EINVAL; 14621da177e4SLinus Torvalds 14631da177e4SLinus Torvalds if (put_user(olr, optlen)) 14641da177e4SLinus Torvalds return -EFAULT; 14651da177e4SLinus Torvalds if (optname == MRT_VERSION) 14661da177e4SLinus Torvalds val = 0x0305; 14671da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM 14681da177e4SLinus Torvalds else if (optname == MRT_PIM) 14690c12295aSPatrick McHardy val = mrt->mroute_do_pim; 14701da177e4SLinus Torvalds #endif 14711da177e4SLinus Torvalds else 14720c12295aSPatrick McHardy val = mrt->mroute_do_assert; 14731da177e4SLinus Torvalds if (copy_to_user(optval, &val, olr)) 14741da177e4SLinus Torvalds return -EFAULT; 14751da177e4SLinus Torvalds return 0; 14761da177e4SLinus Torvalds } 14771da177e4SLinus Torvalds 14781da177e4SLinus Torvalds /* 14791da177e4SLinus Torvalds * The IP multicast ioctl support routines. 14801da177e4SLinus Torvalds */ 14811da177e4SLinus Torvalds 14821da177e4SLinus Torvalds int ipmr_ioctl(struct sock *sk, int cmd, void __user *arg) 14831da177e4SLinus Torvalds { 14841da177e4SLinus Torvalds struct sioc_sg_req sr; 14851da177e4SLinus Torvalds struct sioc_vif_req vr; 14861da177e4SLinus Torvalds struct vif_device *vif; 14871da177e4SLinus Torvalds struct mfc_cache *c; 14884feb88e5SBenjamin Thery struct net *net = sock_net(sk); 1489f0ad0860SPatrick McHardy struct mr_table *mrt; 1490f0ad0860SPatrick McHardy 1491f0ad0860SPatrick McHardy mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT); 1492f0ad0860SPatrick McHardy if (mrt == NULL) 1493f0ad0860SPatrick McHardy return -ENOENT; 14941da177e4SLinus Torvalds 1495132adf54SStephen Hemminger switch (cmd) { 14961da177e4SLinus Torvalds case SIOCGETVIFCNT: 14971da177e4SLinus Torvalds if (copy_from_user(&vr, arg, sizeof(vr))) 14981da177e4SLinus Torvalds return -EFAULT; 14990c12295aSPatrick McHardy if (vr.vifi >= mrt->maxvif) 15001da177e4SLinus Torvalds return -EINVAL; 15011da177e4SLinus Torvalds read_lock(&mrt_lock); 15020c12295aSPatrick McHardy vif = &mrt->vif_table[vr.vifi]; 15030c12295aSPatrick McHardy if (VIF_EXISTS(mrt, vr.vifi)) { 15041da177e4SLinus Torvalds vr.icount = vif->pkt_in; 15051da177e4SLinus Torvalds vr.ocount = vif->pkt_out; 15061da177e4SLinus Torvalds vr.ibytes = vif->bytes_in; 15071da177e4SLinus Torvalds vr.obytes = vif->bytes_out; 15081da177e4SLinus Torvalds read_unlock(&mrt_lock); 15091da177e4SLinus Torvalds 15101da177e4SLinus Torvalds if (copy_to_user(arg, &vr, sizeof(vr))) 15111da177e4SLinus Torvalds return -EFAULT; 15121da177e4SLinus Torvalds return 0; 15131da177e4SLinus Torvalds } 15141da177e4SLinus Torvalds read_unlock(&mrt_lock); 15151da177e4SLinus Torvalds return -EADDRNOTAVAIL; 15161da177e4SLinus Torvalds case SIOCGETSGCNT: 15171da177e4SLinus Torvalds if (copy_from_user(&sr, arg, sizeof(sr))) 15181da177e4SLinus Torvalds return -EFAULT; 15191da177e4SLinus Torvalds 1520a8c9486bSEric Dumazet rcu_read_lock(); 15210c12295aSPatrick McHardy c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr); 15221da177e4SLinus Torvalds if (c) { 15231da177e4SLinus Torvalds sr.pktcnt = c->mfc_un.res.pkt; 15241da177e4SLinus Torvalds sr.bytecnt = c->mfc_un.res.bytes; 15251da177e4SLinus Torvalds sr.wrong_if = c->mfc_un.res.wrong_if; 1526a8c9486bSEric Dumazet rcu_read_unlock(); 15271da177e4SLinus Torvalds 15281da177e4SLinus Torvalds if (copy_to_user(arg, &sr, sizeof(sr))) 15291da177e4SLinus Torvalds return -EFAULT; 15301da177e4SLinus Torvalds return 0; 15311da177e4SLinus Torvalds } 1532a8c9486bSEric Dumazet rcu_read_unlock(); 15331da177e4SLinus Torvalds return -EADDRNOTAVAIL; 15341da177e4SLinus Torvalds default: 15351da177e4SLinus Torvalds return -ENOIOCTLCMD; 15361da177e4SLinus Torvalds } 15371da177e4SLinus Torvalds } 15381da177e4SLinus Torvalds 1539709b46e8SEric W. Biederman #ifdef CONFIG_COMPAT 1540709b46e8SEric W. Biederman struct compat_sioc_sg_req { 1541709b46e8SEric W. Biederman struct in_addr src; 1542709b46e8SEric W. Biederman struct in_addr grp; 1543709b46e8SEric W. Biederman compat_ulong_t pktcnt; 1544709b46e8SEric W. Biederman compat_ulong_t bytecnt; 1545709b46e8SEric W. Biederman compat_ulong_t wrong_if; 1546709b46e8SEric W. Biederman }; 1547709b46e8SEric W. Biederman 1548ca6b8bb0SDavid S. Miller struct compat_sioc_vif_req { 1549ca6b8bb0SDavid S. Miller vifi_t vifi; /* Which iface */ 1550ca6b8bb0SDavid S. Miller compat_ulong_t icount; 1551ca6b8bb0SDavid S. Miller compat_ulong_t ocount; 1552ca6b8bb0SDavid S. Miller compat_ulong_t ibytes; 1553ca6b8bb0SDavid S. Miller compat_ulong_t obytes; 1554ca6b8bb0SDavid S. Miller }; 1555ca6b8bb0SDavid S. Miller 1556709b46e8SEric W. Biederman int ipmr_compat_ioctl(struct sock *sk, unsigned int cmd, void __user *arg) 1557709b46e8SEric W. Biederman { 15580033d5adSDavid S. Miller struct compat_sioc_sg_req sr; 1559ca6b8bb0SDavid S. Miller struct compat_sioc_vif_req vr; 1560ca6b8bb0SDavid S. Miller struct vif_device *vif; 1561709b46e8SEric W. Biederman struct mfc_cache *c; 1562709b46e8SEric W. Biederman struct net *net = sock_net(sk); 1563709b46e8SEric W. Biederman struct mr_table *mrt; 1564709b46e8SEric W. Biederman 1565709b46e8SEric W. Biederman mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT); 1566709b46e8SEric W. Biederman if (mrt == NULL) 1567709b46e8SEric W. Biederman return -ENOENT; 1568709b46e8SEric W. Biederman 1569709b46e8SEric W. Biederman switch (cmd) { 1570ca6b8bb0SDavid S. Miller case SIOCGETVIFCNT: 1571ca6b8bb0SDavid S. Miller if (copy_from_user(&vr, arg, sizeof(vr))) 1572ca6b8bb0SDavid S. Miller return -EFAULT; 1573ca6b8bb0SDavid S. Miller if (vr.vifi >= mrt->maxvif) 1574ca6b8bb0SDavid S. Miller return -EINVAL; 1575ca6b8bb0SDavid S. Miller read_lock(&mrt_lock); 1576ca6b8bb0SDavid S. Miller vif = &mrt->vif_table[vr.vifi]; 1577ca6b8bb0SDavid S. Miller if (VIF_EXISTS(mrt, vr.vifi)) { 1578ca6b8bb0SDavid S. Miller vr.icount = vif->pkt_in; 1579ca6b8bb0SDavid S. Miller vr.ocount = vif->pkt_out; 1580ca6b8bb0SDavid S. Miller vr.ibytes = vif->bytes_in; 1581ca6b8bb0SDavid S. Miller vr.obytes = vif->bytes_out; 1582ca6b8bb0SDavid S. Miller read_unlock(&mrt_lock); 1583ca6b8bb0SDavid S. Miller 1584ca6b8bb0SDavid S. Miller if (copy_to_user(arg, &vr, sizeof(vr))) 1585ca6b8bb0SDavid S. Miller return -EFAULT; 1586ca6b8bb0SDavid S. Miller return 0; 1587ca6b8bb0SDavid S. Miller } 1588ca6b8bb0SDavid S. Miller read_unlock(&mrt_lock); 1589ca6b8bb0SDavid S. Miller return -EADDRNOTAVAIL; 1590709b46e8SEric W. Biederman case SIOCGETSGCNT: 1591709b46e8SEric W. Biederman if (copy_from_user(&sr, arg, sizeof(sr))) 1592709b46e8SEric W. Biederman return -EFAULT; 1593709b46e8SEric W. Biederman 1594709b46e8SEric W. Biederman rcu_read_lock(); 1595709b46e8SEric W. Biederman c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr); 1596709b46e8SEric W. Biederman if (c) { 1597709b46e8SEric W. Biederman sr.pktcnt = c->mfc_un.res.pkt; 1598709b46e8SEric W. Biederman sr.bytecnt = c->mfc_un.res.bytes; 1599709b46e8SEric W. Biederman sr.wrong_if = c->mfc_un.res.wrong_if; 1600709b46e8SEric W. Biederman rcu_read_unlock(); 1601709b46e8SEric W. Biederman 1602709b46e8SEric W. Biederman if (copy_to_user(arg, &sr, sizeof(sr))) 1603709b46e8SEric W. Biederman return -EFAULT; 1604709b46e8SEric W. Biederman return 0; 1605709b46e8SEric W. Biederman } 1606709b46e8SEric W. Biederman rcu_read_unlock(); 1607709b46e8SEric W. Biederman return -EADDRNOTAVAIL; 1608709b46e8SEric W. Biederman default: 1609709b46e8SEric W. Biederman return -ENOIOCTLCMD; 1610709b46e8SEric W. Biederman } 1611709b46e8SEric W. Biederman } 1612709b46e8SEric W. Biederman #endif 1613709b46e8SEric W. Biederman 16141da177e4SLinus Torvalds 16151da177e4SLinus Torvalds static int ipmr_device_event(struct notifier_block *this, unsigned long event, void *ptr) 16161da177e4SLinus Torvalds { 1617351638e7SJiri Pirko struct net_device *dev = netdev_notifier_info_to_dev(ptr); 16184feb88e5SBenjamin Thery struct net *net = dev_net(dev); 1619f0ad0860SPatrick McHardy struct mr_table *mrt; 16201da177e4SLinus Torvalds struct vif_device *v; 16211da177e4SLinus Torvalds int ct; 1622e9dc8653SEric W. Biederman 16231da177e4SLinus Torvalds if (event != NETDEV_UNREGISTER) 16241da177e4SLinus Torvalds return NOTIFY_DONE; 1625f0ad0860SPatrick McHardy 1626f0ad0860SPatrick McHardy ipmr_for_each_table(mrt, net) { 16270c12295aSPatrick McHardy v = &mrt->vif_table[0]; 16280c12295aSPatrick McHardy for (ct = 0; ct < mrt->maxvif; ct++, v++) { 1629e9dc8653SEric W. Biederman if (v->dev == dev) 1630e92036a6SRongQing.Li vif_delete(mrt, ct, 1, NULL); 16311da177e4SLinus Torvalds } 16321da177e4SLinus Torvalds } 16331da177e4SLinus Torvalds return NOTIFY_DONE; 16341da177e4SLinus Torvalds } 16351da177e4SLinus Torvalds 16361da177e4SLinus Torvalds 16371da177e4SLinus Torvalds static struct notifier_block ip_mr_notifier = { 16381da177e4SLinus Torvalds .notifier_call = ipmr_device_event, 16391da177e4SLinus Torvalds }; 16401da177e4SLinus Torvalds 16411da177e4SLinus Torvalds /* 16421da177e4SLinus Torvalds * Encapsulate a packet by attaching a valid IPIP header to it. 16431da177e4SLinus Torvalds * This avoids tunnel drivers and other mess and gives us the speed so 16441da177e4SLinus Torvalds * important for multicast video. 16451da177e4SLinus Torvalds */ 16461da177e4SLinus Torvalds 1647114c7844SAl Viro static void ip_encap(struct sk_buff *skb, __be32 saddr, __be32 daddr) 16481da177e4SLinus Torvalds { 16498856dfa3SArnaldo Carvalho de Melo struct iphdr *iph; 1650b71d1d42SEric Dumazet const struct iphdr *old_iph = ip_hdr(skb); 16518856dfa3SArnaldo Carvalho de Melo 16528856dfa3SArnaldo Carvalho de Melo skb_push(skb, sizeof(struct iphdr)); 1653b0e380b1SArnaldo Carvalho de Melo skb->transport_header = skb->network_header; 16548856dfa3SArnaldo Carvalho de Melo skb_reset_network_header(skb); 1655eddc9ec5SArnaldo Carvalho de Melo iph = ip_hdr(skb); 16561da177e4SLinus Torvalds 16571da177e4SLinus Torvalds iph->version = 4; 1658e023dd64SArnaldo Carvalho de Melo iph->tos = old_iph->tos; 1659e023dd64SArnaldo Carvalho de Melo iph->ttl = old_iph->ttl; 16601da177e4SLinus Torvalds iph->frag_off = 0; 16611da177e4SLinus Torvalds iph->daddr = daddr; 16621da177e4SLinus Torvalds iph->saddr = saddr; 16631da177e4SLinus Torvalds iph->protocol = IPPROTO_IPIP; 16641da177e4SLinus Torvalds iph->ihl = 5; 16651da177e4SLinus Torvalds iph->tot_len = htons(skb->len); 1666703133deSAnsis Atteka ip_select_ident(skb, skb_dst(skb), NULL); 16671da177e4SLinus Torvalds ip_send_check(iph); 16681da177e4SLinus Torvalds 16691da177e4SLinus Torvalds memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt)); 16701da177e4SLinus Torvalds nf_reset(skb); 16711da177e4SLinus Torvalds } 16721da177e4SLinus Torvalds 16731da177e4SLinus Torvalds static inline int ipmr_forward_finish(struct sk_buff *skb) 16741da177e4SLinus Torvalds { 16751da177e4SLinus Torvalds struct ip_options *opt = &(IPCB(skb)->opt); 16761da177e4SLinus Torvalds 1677adf30907SEric Dumazet IP_INC_STATS_BH(dev_net(skb_dst(skb)->dev), IPSTATS_MIB_OUTFORWDATAGRAMS); 16782d8dbb04SVincent Bernat IP_ADD_STATS_BH(dev_net(skb_dst(skb)->dev), IPSTATS_MIB_OUTOCTETS, skb->len); 16791da177e4SLinus Torvalds 16801da177e4SLinus Torvalds if (unlikely(opt->optlen)) 16811da177e4SLinus Torvalds ip_forward_options(skb); 16821da177e4SLinus Torvalds 16831da177e4SLinus Torvalds return dst_output(skb); 16841da177e4SLinus Torvalds } 16851da177e4SLinus Torvalds 16861da177e4SLinus Torvalds /* 16871da177e4SLinus Torvalds * Processing handlers for ipmr_forward 16881da177e4SLinus Torvalds */ 16891da177e4SLinus Torvalds 16900c12295aSPatrick McHardy static void ipmr_queue_xmit(struct net *net, struct mr_table *mrt, 16910c12295aSPatrick McHardy struct sk_buff *skb, struct mfc_cache *c, int vifi) 16921da177e4SLinus Torvalds { 1693eddc9ec5SArnaldo Carvalho de Melo const struct iphdr *iph = ip_hdr(skb); 16940c12295aSPatrick McHardy struct vif_device *vif = &mrt->vif_table[vifi]; 16951da177e4SLinus Torvalds struct net_device *dev; 16961da177e4SLinus Torvalds struct rtable *rt; 169731e4543dSDavid S. Miller struct flowi4 fl4; 16981da177e4SLinus Torvalds int encap = 0; 16991da177e4SLinus Torvalds 17001da177e4SLinus Torvalds if (vif->dev == NULL) 17011da177e4SLinus Torvalds goto out_free; 17021da177e4SLinus Torvalds 17031da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM 17041da177e4SLinus Torvalds if (vif->flags & VIFF_REGISTER) { 17051da177e4SLinus Torvalds vif->pkt_out++; 17061da177e4SLinus Torvalds vif->bytes_out += skb->len; 1707cf3677aeSPavel Emelyanov vif->dev->stats.tx_bytes += skb->len; 1708cf3677aeSPavel Emelyanov vif->dev->stats.tx_packets++; 17090c12295aSPatrick McHardy ipmr_cache_report(mrt, skb, vifi, IGMPMSG_WHOLEPKT); 171069ebbf58SIlpo Järvinen goto out_free; 17111da177e4SLinus Torvalds } 17121da177e4SLinus Torvalds #endif 17131da177e4SLinus Torvalds 17141da177e4SLinus Torvalds if (vif->flags & VIFF_TUNNEL) { 171531e4543dSDavid S. Miller rt = ip_route_output_ports(net, &fl4, NULL, 171678fbfd8aSDavid S. Miller vif->remote, vif->local, 171778fbfd8aSDavid S. Miller 0, 0, 171878fbfd8aSDavid S. Miller IPPROTO_IPIP, 171978fbfd8aSDavid S. Miller RT_TOS(iph->tos), vif->link); 1720b23dd4feSDavid S. Miller if (IS_ERR(rt)) 17211da177e4SLinus Torvalds goto out_free; 17221da177e4SLinus Torvalds encap = sizeof(struct iphdr); 17231da177e4SLinus Torvalds } else { 172431e4543dSDavid S. Miller rt = ip_route_output_ports(net, &fl4, NULL, iph->daddr, 0, 172578fbfd8aSDavid S. Miller 0, 0, 172678fbfd8aSDavid S. Miller IPPROTO_IPIP, 172778fbfd8aSDavid S. Miller RT_TOS(iph->tos), vif->link); 1728b23dd4feSDavid S. Miller if (IS_ERR(rt)) 17291da177e4SLinus Torvalds goto out_free; 17301da177e4SLinus Torvalds } 17311da177e4SLinus Torvalds 1732d8d1f30bSChangli Gao dev = rt->dst.dev; 17331da177e4SLinus Torvalds 1734d8d1f30bSChangli Gao if (skb->len+encap > dst_mtu(&rt->dst) && (ntohs(iph->frag_off) & IP_DF)) { 17351da177e4SLinus Torvalds /* Do not fragment multicasts. Alas, IPv4 does not 1736a8cb16ddSEric Dumazet * allow to send ICMP, so that packets will disappear 1737a8cb16ddSEric Dumazet * to blackhole. 17381da177e4SLinus Torvalds */ 17391da177e4SLinus Torvalds 17407c73a6faSPavel Emelyanov IP_INC_STATS_BH(dev_net(dev), IPSTATS_MIB_FRAGFAILS); 17411da177e4SLinus Torvalds ip_rt_put(rt); 17421da177e4SLinus Torvalds goto out_free; 17431da177e4SLinus Torvalds } 17441da177e4SLinus Torvalds 1745d8d1f30bSChangli Gao encap += LL_RESERVED_SPACE(dev) + rt->dst.header_len; 17461da177e4SLinus Torvalds 17471da177e4SLinus Torvalds if (skb_cow(skb, encap)) { 17481da177e4SLinus Torvalds ip_rt_put(rt); 17491da177e4SLinus Torvalds goto out_free; 17501da177e4SLinus Torvalds } 17511da177e4SLinus Torvalds 17521da177e4SLinus Torvalds vif->pkt_out++; 17531da177e4SLinus Torvalds vif->bytes_out += skb->len; 17541da177e4SLinus Torvalds 1755adf30907SEric Dumazet skb_dst_drop(skb); 1756d8d1f30bSChangli Gao skb_dst_set(skb, &rt->dst); 1757eddc9ec5SArnaldo Carvalho de Melo ip_decrease_ttl(ip_hdr(skb)); 17581da177e4SLinus Torvalds 17591da177e4SLinus Torvalds /* FIXME: forward and output firewalls used to be called here. 1760a8cb16ddSEric Dumazet * What do we do with netfilter? -- RR 1761a8cb16ddSEric Dumazet */ 17621da177e4SLinus Torvalds if (vif->flags & VIFF_TUNNEL) { 17631da177e4SLinus Torvalds ip_encap(skb, vif->local, vif->remote); 17641da177e4SLinus Torvalds /* FIXME: extra output firewall step used to be here. --RR */ 17652f4c02d4SPavel Emelyanov vif->dev->stats.tx_packets++; 17662f4c02d4SPavel Emelyanov vif->dev->stats.tx_bytes += skb->len; 17671da177e4SLinus Torvalds } 17681da177e4SLinus Torvalds 17691da177e4SLinus Torvalds IPCB(skb)->flags |= IPSKB_FORWARDED; 17701da177e4SLinus Torvalds 17711da177e4SLinus Torvalds /* 17721da177e4SLinus Torvalds * RFC1584 teaches, that DVMRP/PIM router must deliver packets locally 17731da177e4SLinus Torvalds * not only before forwarding, but after forwarding on all output 17741da177e4SLinus Torvalds * interfaces. It is clear, if mrouter runs a multicasting 17751da177e4SLinus Torvalds * program, it should receive packets not depending to what interface 17761da177e4SLinus Torvalds * program is joined. 17771da177e4SLinus Torvalds * If we will not make it, the program will have to join on all 17781da177e4SLinus Torvalds * interfaces. On the other hand, multihoming host (or router, but 17791da177e4SLinus Torvalds * not mrouter) cannot join to more than one interface - it will 17801da177e4SLinus Torvalds * result in receiving multiple packets. 17811da177e4SLinus Torvalds */ 17829bbc768aSJan Engelhardt NF_HOOK(NFPROTO_IPV4, NF_INET_FORWARD, skb, skb->dev, dev, 17831da177e4SLinus Torvalds ipmr_forward_finish); 17841da177e4SLinus Torvalds return; 17851da177e4SLinus Torvalds 17861da177e4SLinus Torvalds out_free: 17871da177e4SLinus Torvalds kfree_skb(skb); 17881da177e4SLinus Torvalds } 17891da177e4SLinus Torvalds 17900c12295aSPatrick McHardy static int ipmr_find_vif(struct mr_table *mrt, struct net_device *dev) 17911da177e4SLinus Torvalds { 17921da177e4SLinus Torvalds int ct; 17930c12295aSPatrick McHardy 17940c12295aSPatrick McHardy for (ct = mrt->maxvif-1; ct >= 0; ct--) { 17950c12295aSPatrick McHardy if (mrt->vif_table[ct].dev == dev) 17961da177e4SLinus Torvalds break; 17971da177e4SLinus Torvalds } 17981da177e4SLinus Torvalds return ct; 17991da177e4SLinus Torvalds } 18001da177e4SLinus Torvalds 18011da177e4SLinus Torvalds /* "local" means that we should preserve one skb (for local delivery) */ 18021da177e4SLinus Torvalds 1803c4854ec8SRami Rosen static void ip_mr_forward(struct net *net, struct mr_table *mrt, 18040c12295aSPatrick McHardy struct sk_buff *skb, struct mfc_cache *cache, 18050c12295aSPatrick McHardy int local) 18061da177e4SLinus Torvalds { 18071da177e4SLinus Torvalds int psend = -1; 18081da177e4SLinus Torvalds int vif, ct; 1809660b26dcSNicolas Dichtel int true_vifi = ipmr_find_vif(mrt, skb->dev); 18101da177e4SLinus Torvalds 18111da177e4SLinus Torvalds vif = cache->mfc_parent; 18121da177e4SLinus Torvalds cache->mfc_un.res.pkt++; 18131da177e4SLinus Torvalds cache->mfc_un.res.bytes += skb->len; 18141da177e4SLinus Torvalds 1815360eb5daSNicolas Dichtel if (cache->mfc_origin == htonl(INADDR_ANY) && true_vifi >= 0) { 1816660b26dcSNicolas Dichtel struct mfc_cache *cache_proxy; 1817660b26dcSNicolas Dichtel 1818660b26dcSNicolas Dichtel /* For an (*,G) entry, we only check that the incomming 1819660b26dcSNicolas Dichtel * interface is part of the static tree. 1820660b26dcSNicolas Dichtel */ 1821660b26dcSNicolas Dichtel cache_proxy = ipmr_cache_find_any_parent(mrt, vif); 1822660b26dcSNicolas Dichtel if (cache_proxy && 1823660b26dcSNicolas Dichtel cache_proxy->mfc_un.res.ttls[true_vifi] < 255) 1824660b26dcSNicolas Dichtel goto forward; 1825660b26dcSNicolas Dichtel } 1826660b26dcSNicolas Dichtel 18271da177e4SLinus Torvalds /* 18281da177e4SLinus Torvalds * Wrong interface: drop packet and (maybe) send PIM assert. 18291da177e4SLinus Torvalds */ 18300c12295aSPatrick McHardy if (mrt->vif_table[vif].dev != skb->dev) { 1831c7537967SDavid S. Miller if (rt_is_output_route(skb_rtable(skb))) { 18321da177e4SLinus Torvalds /* It is our own packet, looped back. 1833a8cb16ddSEric Dumazet * Very complicated situation... 1834a8cb16ddSEric Dumazet * 1835a8cb16ddSEric Dumazet * The best workaround until routing daemons will be 1836a8cb16ddSEric Dumazet * fixed is not to redistribute packet, if it was 1837a8cb16ddSEric Dumazet * send through wrong interface. It means, that 1838a8cb16ddSEric Dumazet * multicast applications WILL NOT work for 1839a8cb16ddSEric Dumazet * (S,G), which have default multicast route pointing 1840a8cb16ddSEric Dumazet * to wrong oif. In any case, it is not a good 1841a8cb16ddSEric Dumazet * idea to use multicasting applications on router. 18421da177e4SLinus Torvalds */ 18431da177e4SLinus Torvalds goto dont_forward; 18441da177e4SLinus Torvalds } 18451da177e4SLinus Torvalds 18461da177e4SLinus Torvalds cache->mfc_un.res.wrong_if++; 18471da177e4SLinus Torvalds 18480c12295aSPatrick McHardy if (true_vifi >= 0 && mrt->mroute_do_assert && 18491da177e4SLinus Torvalds /* pimsm uses asserts, when switching from RPT to SPT, 1850a8cb16ddSEric Dumazet * so that we cannot check that packet arrived on an oif. 1851a8cb16ddSEric Dumazet * It is bad, but otherwise we would need to move pretty 1852a8cb16ddSEric Dumazet * large chunk of pimd to kernel. Ough... --ANK 18531da177e4SLinus Torvalds */ 18540c12295aSPatrick McHardy (mrt->mroute_do_pim || 18556f9374a9SBenjamin Thery cache->mfc_un.res.ttls[true_vifi] < 255) && 18561da177e4SLinus Torvalds time_after(jiffies, 18571da177e4SLinus Torvalds cache->mfc_un.res.last_assert + MFC_ASSERT_THRESH)) { 18581da177e4SLinus Torvalds cache->mfc_un.res.last_assert = jiffies; 18590c12295aSPatrick McHardy ipmr_cache_report(mrt, skb, true_vifi, IGMPMSG_WRONGVIF); 18601da177e4SLinus Torvalds } 18611da177e4SLinus Torvalds goto dont_forward; 18621da177e4SLinus Torvalds } 18631da177e4SLinus Torvalds 1864660b26dcSNicolas Dichtel forward: 18650c12295aSPatrick McHardy mrt->vif_table[vif].pkt_in++; 18660c12295aSPatrick McHardy mrt->vif_table[vif].bytes_in += skb->len; 18671da177e4SLinus Torvalds 18681da177e4SLinus Torvalds /* 18691da177e4SLinus Torvalds * Forward the frame 18701da177e4SLinus Torvalds */ 1871360eb5daSNicolas Dichtel if (cache->mfc_origin == htonl(INADDR_ANY) && 1872360eb5daSNicolas Dichtel cache->mfc_mcastgrp == htonl(INADDR_ANY)) { 1873660b26dcSNicolas Dichtel if (true_vifi >= 0 && 1874660b26dcSNicolas Dichtel true_vifi != cache->mfc_parent && 1875660b26dcSNicolas Dichtel ip_hdr(skb)->ttl > 1876660b26dcSNicolas Dichtel cache->mfc_un.res.ttls[cache->mfc_parent]) { 1877660b26dcSNicolas Dichtel /* It's an (*,*) entry and the packet is not coming from 1878660b26dcSNicolas Dichtel * the upstream: forward the packet to the upstream 1879660b26dcSNicolas Dichtel * only. 1880660b26dcSNicolas Dichtel */ 1881660b26dcSNicolas Dichtel psend = cache->mfc_parent; 1882660b26dcSNicolas Dichtel goto last_forward; 1883660b26dcSNicolas Dichtel } 1884660b26dcSNicolas Dichtel goto dont_forward; 1885660b26dcSNicolas Dichtel } 1886a8cb16ddSEric Dumazet for (ct = cache->mfc_un.res.maxvif - 1; 1887a8cb16ddSEric Dumazet ct >= cache->mfc_un.res.minvif; ct--) { 1888660b26dcSNicolas Dichtel /* For (*,G) entry, don't forward to the incoming interface */ 1889360eb5daSNicolas Dichtel if ((cache->mfc_origin != htonl(INADDR_ANY) || 1890360eb5daSNicolas Dichtel ct != true_vifi) && 1891660b26dcSNicolas Dichtel ip_hdr(skb)->ttl > cache->mfc_un.res.ttls[ct]) { 18921da177e4SLinus Torvalds if (psend != -1) { 18931da177e4SLinus Torvalds struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC); 1894a8cb16ddSEric Dumazet 18951da177e4SLinus Torvalds if (skb2) 18960c12295aSPatrick McHardy ipmr_queue_xmit(net, mrt, skb2, cache, 18970c12295aSPatrick McHardy psend); 18981da177e4SLinus Torvalds } 18991da177e4SLinus Torvalds psend = ct; 19001da177e4SLinus Torvalds } 19011da177e4SLinus Torvalds } 1902660b26dcSNicolas Dichtel last_forward: 19031da177e4SLinus Torvalds if (psend != -1) { 19041da177e4SLinus Torvalds if (local) { 19051da177e4SLinus Torvalds struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC); 1906a8cb16ddSEric Dumazet 19071da177e4SLinus Torvalds if (skb2) 19080c12295aSPatrick McHardy ipmr_queue_xmit(net, mrt, skb2, cache, psend); 19091da177e4SLinus Torvalds } else { 19100c12295aSPatrick McHardy ipmr_queue_xmit(net, mrt, skb, cache, psend); 1911c4854ec8SRami Rosen return; 19121da177e4SLinus Torvalds } 19131da177e4SLinus Torvalds } 19141da177e4SLinus Torvalds 19151da177e4SLinus Torvalds dont_forward: 19161da177e4SLinus Torvalds if (!local) 19171da177e4SLinus Torvalds kfree_skb(skb); 19181da177e4SLinus Torvalds } 19191da177e4SLinus Torvalds 1920417da66fSDavid S. Miller static struct mr_table *ipmr_rt_fib_lookup(struct net *net, struct sk_buff *skb) 1921ee3f1aafSDavid S. Miller { 1922417da66fSDavid S. Miller struct rtable *rt = skb_rtable(skb); 1923417da66fSDavid S. Miller struct iphdr *iph = ip_hdr(skb); 1924da91981bSDavid S. Miller struct flowi4 fl4 = { 1925417da66fSDavid S. Miller .daddr = iph->daddr, 1926417da66fSDavid S. Miller .saddr = iph->saddr, 1927b0fe4a31SJulian Anastasov .flowi4_tos = RT_TOS(iph->tos), 19284fd551d7SDavid S. Miller .flowi4_oif = (rt_is_output_route(rt) ? 19294fd551d7SDavid S. Miller skb->dev->ifindex : 0), 19304fd551d7SDavid S. Miller .flowi4_iif = (rt_is_output_route(rt) ? 19311fb9489bSPavel Emelyanov LOOPBACK_IFINDEX : 19324fd551d7SDavid S. Miller skb->dev->ifindex), 1933b4869889SDavid Miller .flowi4_mark = skb->mark, 1934ee3f1aafSDavid S. Miller }; 1935ee3f1aafSDavid S. Miller struct mr_table *mrt; 1936ee3f1aafSDavid S. Miller int err; 1937ee3f1aafSDavid S. Miller 1938da91981bSDavid S. Miller err = ipmr_fib_lookup(net, &fl4, &mrt); 1939ee3f1aafSDavid S. Miller if (err) 1940ee3f1aafSDavid S. Miller return ERR_PTR(err); 1941ee3f1aafSDavid S. Miller return mrt; 1942ee3f1aafSDavid S. Miller } 19431da177e4SLinus Torvalds 19441da177e4SLinus Torvalds /* 19451da177e4SLinus Torvalds * Multicast packets for forwarding arrive here 19464c968709SEric Dumazet * Called with rcu_read_lock(); 19471da177e4SLinus Torvalds */ 19481da177e4SLinus Torvalds 19491da177e4SLinus Torvalds int ip_mr_input(struct sk_buff *skb) 19501da177e4SLinus Torvalds { 19511da177e4SLinus Torvalds struct mfc_cache *cache; 19524feb88e5SBenjamin Thery struct net *net = dev_net(skb->dev); 1953511c3f92SEric Dumazet int local = skb_rtable(skb)->rt_flags & RTCF_LOCAL; 1954f0ad0860SPatrick McHardy struct mr_table *mrt; 19551da177e4SLinus Torvalds 19561da177e4SLinus Torvalds /* Packet is looped back after forward, it should not be 1957a8cb16ddSEric Dumazet * forwarded second time, but still can be delivered locally. 19581da177e4SLinus Torvalds */ 19591da177e4SLinus Torvalds if (IPCB(skb)->flags & IPSKB_FORWARDED) 19601da177e4SLinus Torvalds goto dont_forward; 19611da177e4SLinus Torvalds 1962417da66fSDavid S. Miller mrt = ipmr_rt_fib_lookup(net, skb); 1963ee3f1aafSDavid S. Miller if (IS_ERR(mrt)) { 1964e40dbc51SBen Greear kfree_skb(skb); 1965ee3f1aafSDavid S. Miller return PTR_ERR(mrt); 19665e2b61f7SDavid S. Miller } 19671da177e4SLinus Torvalds if (!local) { 19681da177e4SLinus Torvalds if (IPCB(skb)->opt.router_alert) { 19691da177e4SLinus Torvalds if (ip_call_ra_chain(skb)) 19701da177e4SLinus Torvalds return 0; 1971eddc9ec5SArnaldo Carvalho de Melo } else if (ip_hdr(skb)->protocol == IPPROTO_IGMP) { 19721da177e4SLinus Torvalds /* IGMPv1 (and broken IGMPv2 implementations sort of 19734c968709SEric Dumazet * Cisco IOS <= 11.2(8)) do not put router alert 19744c968709SEric Dumazet * option to IGMP packets destined to routable 19754c968709SEric Dumazet * groups. It is very bad, because it means 19764c968709SEric Dumazet * that we can forward NO IGMP messages. 19771da177e4SLinus Torvalds */ 19784c968709SEric Dumazet struct sock *mroute_sk; 19794c968709SEric Dumazet 19804c968709SEric Dumazet mroute_sk = rcu_dereference(mrt->mroute_sk); 19814c968709SEric Dumazet if (mroute_sk) { 19822715bcf9SPatrick McHardy nf_reset(skb); 19834c968709SEric Dumazet raw_rcv(mroute_sk, skb); 19841da177e4SLinus Torvalds return 0; 19851da177e4SLinus Torvalds } 19861da177e4SLinus Torvalds } 19871da177e4SLinus Torvalds } 19881da177e4SLinus Torvalds 1989a8c9486bSEric Dumazet /* already under rcu_read_lock() */ 19900c12295aSPatrick McHardy cache = ipmr_cache_find(mrt, ip_hdr(skb)->saddr, ip_hdr(skb)->daddr); 1991660b26dcSNicolas Dichtel if (cache == NULL) { 1992660b26dcSNicolas Dichtel int vif = ipmr_find_vif(mrt, skb->dev); 1993660b26dcSNicolas Dichtel 1994660b26dcSNicolas Dichtel if (vif >= 0) 1995660b26dcSNicolas Dichtel cache = ipmr_cache_find_any(mrt, ip_hdr(skb)->daddr, 1996660b26dcSNicolas Dichtel vif); 1997660b26dcSNicolas Dichtel } 19981da177e4SLinus Torvalds 19991da177e4SLinus Torvalds /* 20001da177e4SLinus Torvalds * No usable cache entry 20011da177e4SLinus Torvalds */ 20021da177e4SLinus Torvalds if (cache == NULL) { 20031da177e4SLinus Torvalds int vif; 20041da177e4SLinus Torvalds 20051da177e4SLinus Torvalds if (local) { 20061da177e4SLinus Torvalds struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC); 20071da177e4SLinus Torvalds ip_local_deliver(skb); 2008a8c9486bSEric Dumazet if (skb2 == NULL) 20091da177e4SLinus Torvalds return -ENOBUFS; 20101da177e4SLinus Torvalds skb = skb2; 20111da177e4SLinus Torvalds } 20121da177e4SLinus Torvalds 2013a8c9486bSEric Dumazet read_lock(&mrt_lock); 20140c12295aSPatrick McHardy vif = ipmr_find_vif(mrt, skb->dev); 20151da177e4SLinus Torvalds if (vif >= 0) { 20160eae88f3SEric Dumazet int err2 = ipmr_cache_unresolved(mrt, vif, skb); 20171da177e4SLinus Torvalds read_unlock(&mrt_lock); 20181da177e4SLinus Torvalds 20190eae88f3SEric Dumazet return err2; 20201da177e4SLinus Torvalds } 20211da177e4SLinus Torvalds read_unlock(&mrt_lock); 20221da177e4SLinus Torvalds kfree_skb(skb); 20231da177e4SLinus Torvalds return -ENODEV; 20241da177e4SLinus Torvalds } 20251da177e4SLinus Torvalds 2026a8c9486bSEric Dumazet read_lock(&mrt_lock); 20270c12295aSPatrick McHardy ip_mr_forward(net, mrt, skb, cache, local); 20281da177e4SLinus Torvalds read_unlock(&mrt_lock); 20291da177e4SLinus Torvalds 20301da177e4SLinus Torvalds if (local) 20311da177e4SLinus Torvalds return ip_local_deliver(skb); 20321da177e4SLinus Torvalds 20331da177e4SLinus Torvalds return 0; 20341da177e4SLinus Torvalds 20351da177e4SLinus Torvalds dont_forward: 20361da177e4SLinus Torvalds if (local) 20371da177e4SLinus Torvalds return ip_local_deliver(skb); 20381da177e4SLinus Torvalds kfree_skb(skb); 20391da177e4SLinus Torvalds return 0; 20401da177e4SLinus Torvalds } 20411da177e4SLinus Torvalds 2042b1879204SIlpo Järvinen #ifdef CONFIG_IP_PIMSM 204355747a0aSEric Dumazet /* called with rcu_read_lock() */ 2044f0ad0860SPatrick McHardy static int __pim_rcv(struct mr_table *mrt, struct sk_buff *skb, 2045f0ad0860SPatrick McHardy unsigned int pimlen) 20461da177e4SLinus Torvalds { 20471da177e4SLinus Torvalds struct net_device *reg_dev = NULL; 2048b1879204SIlpo Järvinen struct iphdr *encap; 20491da177e4SLinus Torvalds 2050b1879204SIlpo Järvinen encap = (struct iphdr *)(skb_transport_header(skb) + pimlen); 20511da177e4SLinus Torvalds /* 2052a8cb16ddSEric Dumazet * Check that: 2053a8cb16ddSEric Dumazet * a. packet is really sent to a multicast group 2054a8cb16ddSEric Dumazet * b. packet is not a NULL-REGISTER 2055a8cb16ddSEric Dumazet * c. packet is not truncated 20561da177e4SLinus Torvalds */ 2057f97c1e0cSJoe Perches if (!ipv4_is_multicast(encap->daddr) || 20581da177e4SLinus Torvalds encap->tot_len == 0 || 2059b1879204SIlpo Järvinen ntohs(encap->tot_len) + pimlen > skb->len) 2060b1879204SIlpo Järvinen return 1; 20611da177e4SLinus Torvalds 20621da177e4SLinus Torvalds read_lock(&mrt_lock); 20630c12295aSPatrick McHardy if (mrt->mroute_reg_vif_num >= 0) 20640c12295aSPatrick McHardy reg_dev = mrt->vif_table[mrt->mroute_reg_vif_num].dev; 20651da177e4SLinus Torvalds read_unlock(&mrt_lock); 20661da177e4SLinus Torvalds 20671da177e4SLinus Torvalds if (reg_dev == NULL) 2068b1879204SIlpo Järvinen return 1; 20691da177e4SLinus Torvalds 2070b0e380b1SArnaldo Carvalho de Melo skb->mac_header = skb->network_header; 20711da177e4SLinus Torvalds skb_pull(skb, (u8 *)encap - skb->data); 207231c7711bSArnaldo Carvalho de Melo skb_reset_network_header(skb); 20731da177e4SLinus Torvalds skb->protocol = htons(ETH_P_IP); 207455747a0aSEric Dumazet skb->ip_summed = CHECKSUM_NONE; 2075d19d56ddSEric Dumazet 2076ea23192eSNicolas Dichtel skb_tunnel_rx(skb, reg_dev, dev_net(reg_dev)); 2077d19d56ddSEric Dumazet 20781da177e4SLinus Torvalds netif_rx(skb); 2079b1879204SIlpo Järvinen 208055747a0aSEric Dumazet return NET_RX_SUCCESS; 2081b1879204SIlpo Järvinen } 2082b1879204SIlpo Järvinen #endif 2083b1879204SIlpo Järvinen 2084b1879204SIlpo Järvinen #ifdef CONFIG_IP_PIMSM_V1 2085b1879204SIlpo Järvinen /* 2086b1879204SIlpo Järvinen * Handle IGMP messages of PIMv1 2087b1879204SIlpo Järvinen */ 2088b1879204SIlpo Järvinen 2089b1879204SIlpo Järvinen int pim_rcv_v1(struct sk_buff *skb) 2090b1879204SIlpo Järvinen { 2091b1879204SIlpo Järvinen struct igmphdr *pim; 20924feb88e5SBenjamin Thery struct net *net = dev_net(skb->dev); 2093f0ad0860SPatrick McHardy struct mr_table *mrt; 2094b1879204SIlpo Järvinen 2095b1879204SIlpo Järvinen if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr))) 2096b1879204SIlpo Järvinen goto drop; 2097b1879204SIlpo Järvinen 2098b1879204SIlpo Järvinen pim = igmp_hdr(skb); 2099b1879204SIlpo Järvinen 2100417da66fSDavid S. Miller mrt = ipmr_rt_fib_lookup(net, skb); 2101ee3f1aafSDavid S. Miller if (IS_ERR(mrt)) 2102f0ad0860SPatrick McHardy goto drop; 21030c12295aSPatrick McHardy if (!mrt->mroute_do_pim || 2104b1879204SIlpo Järvinen pim->group != PIM_V1_VERSION || pim->code != PIM_V1_REGISTER) 2105b1879204SIlpo Järvinen goto drop; 2106b1879204SIlpo Järvinen 2107f0ad0860SPatrick McHardy if (__pim_rcv(mrt, skb, sizeof(*pim))) { 21081da177e4SLinus Torvalds drop: 21091da177e4SLinus Torvalds kfree_skb(skb); 2110b1879204SIlpo Järvinen } 21111da177e4SLinus Torvalds return 0; 21121da177e4SLinus Torvalds } 21131da177e4SLinus Torvalds #endif 21141da177e4SLinus Torvalds 21151da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM_V2 21161da177e4SLinus Torvalds static int pim_rcv(struct sk_buff *skb) 21171da177e4SLinus Torvalds { 21181da177e4SLinus Torvalds struct pimreghdr *pim; 2119f0ad0860SPatrick McHardy struct net *net = dev_net(skb->dev); 2120f0ad0860SPatrick McHardy struct mr_table *mrt; 21211da177e4SLinus Torvalds 2122b1879204SIlpo Järvinen if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr))) 21231da177e4SLinus Torvalds goto drop; 21241da177e4SLinus Torvalds 21259c70220bSArnaldo Carvalho de Melo pim = (struct pimreghdr *)skb_transport_header(skb); 21261da177e4SLinus Torvalds if (pim->type != ((PIM_VERSION << 4) | (PIM_REGISTER)) || 21271da177e4SLinus Torvalds (pim->flags & PIM_NULL_REGISTER) || 21281da177e4SLinus Torvalds (ip_compute_csum((void *)pim, sizeof(*pim)) != 0 && 2129d3bc23e7SAl Viro csum_fold(skb_checksum(skb, 0, skb->len, 0)))) 21301da177e4SLinus Torvalds goto drop; 21311da177e4SLinus Torvalds 2132417da66fSDavid S. Miller mrt = ipmr_rt_fib_lookup(net, skb); 2133ee3f1aafSDavid S. Miller if (IS_ERR(mrt)) 2134f0ad0860SPatrick McHardy goto drop; 2135f0ad0860SPatrick McHardy if (__pim_rcv(mrt, skb, sizeof(*pim))) { 21361da177e4SLinus Torvalds drop: 21371da177e4SLinus Torvalds kfree_skb(skb); 2138b1879204SIlpo Järvinen } 21391da177e4SLinus Torvalds return 0; 21401da177e4SLinus Torvalds } 21411da177e4SLinus Torvalds #endif 21421da177e4SLinus Torvalds 2143cb6a4e46SPatrick McHardy static int __ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb, 2144cb6a4e46SPatrick McHardy struct mfc_cache *c, struct rtmsg *rtm) 21451da177e4SLinus Torvalds { 21461da177e4SLinus Torvalds int ct; 21471da177e4SLinus Torvalds struct rtnexthop *nhp; 214892a395e5SThomas Graf struct nlattr *mp_attr; 2149adfa85e4SNicolas Dichtel struct rta_mfc_stats mfcs; 21501da177e4SLinus Torvalds 21517438189bSNicolas Dichtel /* If cache is unresolved, don't try to parse IIF and OIF */ 2152ed0f160aSDan Carpenter if (c->mfc_parent >= MAXVIFS) 21537438189bSNicolas Dichtel return -ENOENT; 21547438189bSNicolas Dichtel 215592a395e5SThomas Graf if (VIF_EXISTS(mrt, c->mfc_parent) && 215692a395e5SThomas Graf nla_put_u32(skb, RTA_IIF, mrt->vif_table[c->mfc_parent].dev->ifindex) < 0) 215792a395e5SThomas Graf return -EMSGSIZE; 21581da177e4SLinus Torvalds 215992a395e5SThomas Graf if (!(mp_attr = nla_nest_start(skb, RTA_MULTIPATH))) 216092a395e5SThomas Graf return -EMSGSIZE; 21611da177e4SLinus Torvalds 21621da177e4SLinus Torvalds for (ct = c->mfc_un.res.minvif; ct < c->mfc_un.res.maxvif; ct++) { 21630c12295aSPatrick McHardy if (VIF_EXISTS(mrt, ct) && c->mfc_un.res.ttls[ct] < 255) { 216492a395e5SThomas Graf if (!(nhp = nla_reserve_nohdr(skb, sizeof(*nhp)))) { 216592a395e5SThomas Graf nla_nest_cancel(skb, mp_attr); 216692a395e5SThomas Graf return -EMSGSIZE; 216792a395e5SThomas Graf } 216892a395e5SThomas Graf 21691da177e4SLinus Torvalds nhp->rtnh_flags = 0; 21701da177e4SLinus Torvalds nhp->rtnh_hops = c->mfc_un.res.ttls[ct]; 21710c12295aSPatrick McHardy nhp->rtnh_ifindex = mrt->vif_table[ct].dev->ifindex; 21721da177e4SLinus Torvalds nhp->rtnh_len = sizeof(*nhp); 21731da177e4SLinus Torvalds } 21741da177e4SLinus Torvalds } 217592a395e5SThomas Graf 217692a395e5SThomas Graf nla_nest_end(skb, mp_attr); 217792a395e5SThomas Graf 2178adfa85e4SNicolas Dichtel mfcs.mfcs_packets = c->mfc_un.res.pkt; 2179adfa85e4SNicolas Dichtel mfcs.mfcs_bytes = c->mfc_un.res.bytes; 2180adfa85e4SNicolas Dichtel mfcs.mfcs_wrong_if = c->mfc_un.res.wrong_if; 2181adfa85e4SNicolas Dichtel if (nla_put(skb, RTA_MFC_STATS, sizeof(mfcs), &mfcs) < 0) 2182adfa85e4SNicolas Dichtel return -EMSGSIZE; 2183adfa85e4SNicolas Dichtel 21841da177e4SLinus Torvalds rtm->rtm_type = RTN_MULTICAST; 21851da177e4SLinus Torvalds return 1; 21861da177e4SLinus Torvalds } 21871da177e4SLinus Torvalds 21889a1b9496SDavid S. Miller int ipmr_get_route(struct net *net, struct sk_buff *skb, 21899a1b9496SDavid S. Miller __be32 saddr, __be32 daddr, 21909a1b9496SDavid S. Miller struct rtmsg *rtm, int nowait) 21911da177e4SLinus Torvalds { 21921da177e4SLinus Torvalds struct mfc_cache *cache; 21939a1b9496SDavid S. Miller struct mr_table *mrt; 21949a1b9496SDavid S. Miller int err; 21951da177e4SLinus Torvalds 2196f0ad0860SPatrick McHardy mrt = ipmr_get_table(net, RT_TABLE_DEFAULT); 2197f0ad0860SPatrick McHardy if (mrt == NULL) 2198f0ad0860SPatrick McHardy return -ENOENT; 2199f0ad0860SPatrick McHardy 2200a8c9486bSEric Dumazet rcu_read_lock(); 22019a1b9496SDavid S. Miller cache = ipmr_cache_find(mrt, saddr, daddr); 2202660b26dcSNicolas Dichtel if (cache == NULL && skb->dev) { 2203660b26dcSNicolas Dichtel int vif = ipmr_find_vif(mrt, skb->dev); 22041da177e4SLinus Torvalds 2205660b26dcSNicolas Dichtel if (vif >= 0) 2206660b26dcSNicolas Dichtel cache = ipmr_cache_find_any(mrt, daddr, vif); 2207660b26dcSNicolas Dichtel } 22081da177e4SLinus Torvalds if (cache == NULL) { 220972287490SAlexey Kuznetsov struct sk_buff *skb2; 2210eddc9ec5SArnaldo Carvalho de Melo struct iphdr *iph; 22111da177e4SLinus Torvalds struct net_device *dev; 2212a8cb16ddSEric Dumazet int vif = -1; 22131da177e4SLinus Torvalds 22141da177e4SLinus Torvalds if (nowait) { 2215a8c9486bSEric Dumazet rcu_read_unlock(); 22161da177e4SLinus Torvalds return -EAGAIN; 22171da177e4SLinus Torvalds } 22181da177e4SLinus Torvalds 22191da177e4SLinus Torvalds dev = skb->dev; 2220a8c9486bSEric Dumazet read_lock(&mrt_lock); 2221a8cb16ddSEric Dumazet if (dev) 2222a8cb16ddSEric Dumazet vif = ipmr_find_vif(mrt, dev); 2223a8cb16ddSEric Dumazet if (vif < 0) { 22241da177e4SLinus Torvalds read_unlock(&mrt_lock); 2225a8c9486bSEric Dumazet rcu_read_unlock(); 22261da177e4SLinus Torvalds return -ENODEV; 22271da177e4SLinus Torvalds } 222872287490SAlexey Kuznetsov skb2 = skb_clone(skb, GFP_ATOMIC); 222972287490SAlexey Kuznetsov if (!skb2) { 223072287490SAlexey Kuznetsov read_unlock(&mrt_lock); 2231a8c9486bSEric Dumazet rcu_read_unlock(); 223272287490SAlexey Kuznetsov return -ENOMEM; 223372287490SAlexey Kuznetsov } 223472287490SAlexey Kuznetsov 2235e2d1bca7SArnaldo Carvalho de Melo skb_push(skb2, sizeof(struct iphdr)); 2236e2d1bca7SArnaldo Carvalho de Melo skb_reset_network_header(skb2); 2237eddc9ec5SArnaldo Carvalho de Melo iph = ip_hdr(skb2); 2238eddc9ec5SArnaldo Carvalho de Melo iph->ihl = sizeof(struct iphdr) >> 2; 22399a1b9496SDavid S. Miller iph->saddr = saddr; 22409a1b9496SDavid S. Miller iph->daddr = daddr; 2241eddc9ec5SArnaldo Carvalho de Melo iph->version = 0; 22420c12295aSPatrick McHardy err = ipmr_cache_unresolved(mrt, vif, skb2); 22431da177e4SLinus Torvalds read_unlock(&mrt_lock); 2244a8c9486bSEric Dumazet rcu_read_unlock(); 22451da177e4SLinus Torvalds return err; 22461da177e4SLinus Torvalds } 22471da177e4SLinus Torvalds 2248a8c9486bSEric Dumazet read_lock(&mrt_lock); 22491da177e4SLinus Torvalds if (!nowait && (rtm->rtm_flags & RTM_F_NOTIFY)) 22501da177e4SLinus Torvalds cache->mfc_flags |= MFC_NOTIFY; 2251cb6a4e46SPatrick McHardy err = __ipmr_fill_mroute(mrt, skb, cache, rtm); 22521da177e4SLinus Torvalds read_unlock(&mrt_lock); 2253a8c9486bSEric Dumazet rcu_read_unlock(); 22541da177e4SLinus Torvalds return err; 22551da177e4SLinus Torvalds } 22561da177e4SLinus Torvalds 2257cb6a4e46SPatrick McHardy static int ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb, 225865886f43SNicolas Dichtel u32 portid, u32 seq, struct mfc_cache *c, int cmd, 225965886f43SNicolas Dichtel int flags) 2260cb6a4e46SPatrick McHardy { 2261cb6a4e46SPatrick McHardy struct nlmsghdr *nlh; 2262cb6a4e46SPatrick McHardy struct rtmsg *rtm; 22631eb99af5SNicolas Dichtel int err; 2264cb6a4e46SPatrick McHardy 226565886f43SNicolas Dichtel nlh = nlmsg_put(skb, portid, seq, cmd, sizeof(*rtm), flags); 2266cb6a4e46SPatrick McHardy if (nlh == NULL) 2267cb6a4e46SPatrick McHardy return -EMSGSIZE; 2268cb6a4e46SPatrick McHardy 2269cb6a4e46SPatrick McHardy rtm = nlmsg_data(nlh); 2270cb6a4e46SPatrick McHardy rtm->rtm_family = RTNL_FAMILY_IPMR; 2271cb6a4e46SPatrick McHardy rtm->rtm_dst_len = 32; 2272cb6a4e46SPatrick McHardy rtm->rtm_src_len = 32; 2273cb6a4e46SPatrick McHardy rtm->rtm_tos = 0; 2274cb6a4e46SPatrick McHardy rtm->rtm_table = mrt->id; 2275f3756b79SDavid S. Miller if (nla_put_u32(skb, RTA_TABLE, mrt->id)) 2276f3756b79SDavid S. Miller goto nla_put_failure; 2277cb6a4e46SPatrick McHardy rtm->rtm_type = RTN_MULTICAST; 2278cb6a4e46SPatrick McHardy rtm->rtm_scope = RT_SCOPE_UNIVERSE; 22799a68ac72SNicolas Dichtel if (c->mfc_flags & MFC_STATIC) 22809a68ac72SNicolas Dichtel rtm->rtm_protocol = RTPROT_STATIC; 22819a68ac72SNicolas Dichtel else 22829a68ac72SNicolas Dichtel rtm->rtm_protocol = RTPROT_MROUTED; 2283cb6a4e46SPatrick McHardy rtm->rtm_flags = 0; 2284cb6a4e46SPatrick McHardy 2285f3756b79SDavid S. Miller if (nla_put_be32(skb, RTA_SRC, c->mfc_origin) || 2286f3756b79SDavid S. Miller nla_put_be32(skb, RTA_DST, c->mfc_mcastgrp)) 2287f3756b79SDavid S. Miller goto nla_put_failure; 22881eb99af5SNicolas Dichtel err = __ipmr_fill_mroute(mrt, skb, c, rtm); 22891eb99af5SNicolas Dichtel /* do not break the dump if cache is unresolved */ 22901eb99af5SNicolas Dichtel if (err < 0 && err != -ENOENT) 2291cb6a4e46SPatrick McHardy goto nla_put_failure; 2292cb6a4e46SPatrick McHardy 2293cb6a4e46SPatrick McHardy return nlmsg_end(skb, nlh); 2294cb6a4e46SPatrick McHardy 2295cb6a4e46SPatrick McHardy nla_put_failure: 2296cb6a4e46SPatrick McHardy nlmsg_cancel(skb, nlh); 2297cb6a4e46SPatrick McHardy return -EMSGSIZE; 2298cb6a4e46SPatrick McHardy } 2299cb6a4e46SPatrick McHardy 23008cd3ac9fSNicolas Dichtel static size_t mroute_msgsize(bool unresolved, int maxvif) 23018cd3ac9fSNicolas Dichtel { 23028cd3ac9fSNicolas Dichtel size_t len = 23038cd3ac9fSNicolas Dichtel NLMSG_ALIGN(sizeof(struct rtmsg)) 23048cd3ac9fSNicolas Dichtel + nla_total_size(4) /* RTA_TABLE */ 23058cd3ac9fSNicolas Dichtel + nla_total_size(4) /* RTA_SRC */ 23068cd3ac9fSNicolas Dichtel + nla_total_size(4) /* RTA_DST */ 23078cd3ac9fSNicolas Dichtel ; 23088cd3ac9fSNicolas Dichtel 23098cd3ac9fSNicolas Dichtel if (!unresolved) 23108cd3ac9fSNicolas Dichtel len = len 23118cd3ac9fSNicolas Dichtel + nla_total_size(4) /* RTA_IIF */ 23128cd3ac9fSNicolas Dichtel + nla_total_size(0) /* RTA_MULTIPATH */ 23138cd3ac9fSNicolas Dichtel + maxvif * NLA_ALIGN(sizeof(struct rtnexthop)) 23148cd3ac9fSNicolas Dichtel /* RTA_MFC_STATS */ 23158cd3ac9fSNicolas Dichtel + nla_total_size(sizeof(struct rta_mfc_stats)) 23168cd3ac9fSNicolas Dichtel ; 23178cd3ac9fSNicolas Dichtel 23188cd3ac9fSNicolas Dichtel return len; 23198cd3ac9fSNicolas Dichtel } 23208cd3ac9fSNicolas Dichtel 23218cd3ac9fSNicolas Dichtel static void mroute_netlink_event(struct mr_table *mrt, struct mfc_cache *mfc, 23228cd3ac9fSNicolas Dichtel int cmd) 23238cd3ac9fSNicolas Dichtel { 23248cd3ac9fSNicolas Dichtel struct net *net = read_pnet(&mrt->net); 23258cd3ac9fSNicolas Dichtel struct sk_buff *skb; 23268cd3ac9fSNicolas Dichtel int err = -ENOBUFS; 23278cd3ac9fSNicolas Dichtel 23288cd3ac9fSNicolas Dichtel skb = nlmsg_new(mroute_msgsize(mfc->mfc_parent >= MAXVIFS, mrt->maxvif), 23298cd3ac9fSNicolas Dichtel GFP_ATOMIC); 23308cd3ac9fSNicolas Dichtel if (skb == NULL) 23318cd3ac9fSNicolas Dichtel goto errout; 23328cd3ac9fSNicolas Dichtel 233365886f43SNicolas Dichtel err = ipmr_fill_mroute(mrt, skb, 0, 0, mfc, cmd, 0); 23348cd3ac9fSNicolas Dichtel if (err < 0) 23358cd3ac9fSNicolas Dichtel goto errout; 23368cd3ac9fSNicolas Dichtel 23378cd3ac9fSNicolas Dichtel rtnl_notify(skb, net, 0, RTNLGRP_IPV4_MROUTE, NULL, GFP_ATOMIC); 23388cd3ac9fSNicolas Dichtel return; 23398cd3ac9fSNicolas Dichtel 23408cd3ac9fSNicolas Dichtel errout: 23418cd3ac9fSNicolas Dichtel kfree_skb(skb); 23428cd3ac9fSNicolas Dichtel if (err < 0) 23438cd3ac9fSNicolas Dichtel rtnl_set_sk_err(net, RTNLGRP_IPV4_MROUTE, err); 23448cd3ac9fSNicolas Dichtel } 23458cd3ac9fSNicolas Dichtel 2346cb6a4e46SPatrick McHardy static int ipmr_rtm_dumproute(struct sk_buff *skb, struct netlink_callback *cb) 2347cb6a4e46SPatrick McHardy { 2348cb6a4e46SPatrick McHardy struct net *net = sock_net(skb->sk); 2349cb6a4e46SPatrick McHardy struct mr_table *mrt; 2350cb6a4e46SPatrick McHardy struct mfc_cache *mfc; 2351cb6a4e46SPatrick McHardy unsigned int t = 0, s_t; 2352cb6a4e46SPatrick McHardy unsigned int h = 0, s_h; 2353cb6a4e46SPatrick McHardy unsigned int e = 0, s_e; 2354cb6a4e46SPatrick McHardy 2355cb6a4e46SPatrick McHardy s_t = cb->args[0]; 2356cb6a4e46SPatrick McHardy s_h = cb->args[1]; 2357cb6a4e46SPatrick McHardy s_e = cb->args[2]; 2358cb6a4e46SPatrick McHardy 2359a8c9486bSEric Dumazet rcu_read_lock(); 2360cb6a4e46SPatrick McHardy ipmr_for_each_table(mrt, net) { 2361cb6a4e46SPatrick McHardy if (t < s_t) 2362cb6a4e46SPatrick McHardy goto next_table; 2363cb6a4e46SPatrick McHardy if (t > s_t) 2364cb6a4e46SPatrick McHardy s_h = 0; 2365cb6a4e46SPatrick McHardy for (h = s_h; h < MFC_LINES; h++) { 2366a8c9486bSEric Dumazet list_for_each_entry_rcu(mfc, &mrt->mfc_cache_array[h], list) { 2367cb6a4e46SPatrick McHardy if (e < s_e) 2368cb6a4e46SPatrick McHardy goto next_entry; 2369cb6a4e46SPatrick McHardy if (ipmr_fill_mroute(mrt, skb, 237015e47304SEric W. Biederman NETLINK_CB(cb->skb).portid, 2371cb6a4e46SPatrick McHardy cb->nlh->nlmsg_seq, 237265886f43SNicolas Dichtel mfc, RTM_NEWROUTE, 237365886f43SNicolas Dichtel NLM_F_MULTI) < 0) 2374cb6a4e46SPatrick McHardy goto done; 2375cb6a4e46SPatrick McHardy next_entry: 2376cb6a4e46SPatrick McHardy e++; 2377cb6a4e46SPatrick McHardy } 2378cb6a4e46SPatrick McHardy e = s_e = 0; 2379cb6a4e46SPatrick McHardy } 23801eb99af5SNicolas Dichtel spin_lock_bh(&mfc_unres_lock); 23811eb99af5SNicolas Dichtel list_for_each_entry(mfc, &mrt->mfc_unres_queue, list) { 23821eb99af5SNicolas Dichtel if (e < s_e) 23831eb99af5SNicolas Dichtel goto next_entry2; 23841eb99af5SNicolas Dichtel if (ipmr_fill_mroute(mrt, skb, 23851eb99af5SNicolas Dichtel NETLINK_CB(cb->skb).portid, 23861eb99af5SNicolas Dichtel cb->nlh->nlmsg_seq, 238765886f43SNicolas Dichtel mfc, RTM_NEWROUTE, 238865886f43SNicolas Dichtel NLM_F_MULTI) < 0) { 23891eb99af5SNicolas Dichtel spin_unlock_bh(&mfc_unres_lock); 23901eb99af5SNicolas Dichtel goto done; 23911eb99af5SNicolas Dichtel } 23921eb99af5SNicolas Dichtel next_entry2: 23931eb99af5SNicolas Dichtel e++; 23941eb99af5SNicolas Dichtel } 23951eb99af5SNicolas Dichtel spin_unlock_bh(&mfc_unres_lock); 23961eb99af5SNicolas Dichtel e = s_e = 0; 2397cb6a4e46SPatrick McHardy s_h = 0; 2398cb6a4e46SPatrick McHardy next_table: 2399cb6a4e46SPatrick McHardy t++; 2400cb6a4e46SPatrick McHardy } 2401cb6a4e46SPatrick McHardy done: 2402a8c9486bSEric Dumazet rcu_read_unlock(); 2403cb6a4e46SPatrick McHardy 2404cb6a4e46SPatrick McHardy cb->args[2] = e; 2405cb6a4e46SPatrick McHardy cb->args[1] = h; 2406cb6a4e46SPatrick McHardy cb->args[0] = t; 2407cb6a4e46SPatrick McHardy 2408cb6a4e46SPatrick McHardy return skb->len; 2409cb6a4e46SPatrick McHardy } 2410cb6a4e46SPatrick McHardy 24111da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 24121da177e4SLinus Torvalds /* 2413a8cb16ddSEric Dumazet * The /proc interfaces to multicast routing : 2414a8cb16ddSEric Dumazet * /proc/net/ip_mr_cache & /proc/net/ip_mr_vif 24151da177e4SLinus Torvalds */ 24161da177e4SLinus Torvalds struct ipmr_vif_iter { 2417f6bb4514SBenjamin Thery struct seq_net_private p; 2418f0ad0860SPatrick McHardy struct mr_table *mrt; 24191da177e4SLinus Torvalds int ct; 24201da177e4SLinus Torvalds }; 24211da177e4SLinus Torvalds 2422f6bb4514SBenjamin Thery static struct vif_device *ipmr_vif_seq_idx(struct net *net, 2423f6bb4514SBenjamin Thery struct ipmr_vif_iter *iter, 24241da177e4SLinus Torvalds loff_t pos) 24251da177e4SLinus Torvalds { 2426f0ad0860SPatrick McHardy struct mr_table *mrt = iter->mrt; 24270c12295aSPatrick McHardy 24280c12295aSPatrick McHardy for (iter->ct = 0; iter->ct < mrt->maxvif; ++iter->ct) { 24290c12295aSPatrick McHardy if (!VIF_EXISTS(mrt, iter->ct)) 24301da177e4SLinus Torvalds continue; 24311da177e4SLinus Torvalds if (pos-- == 0) 24320c12295aSPatrick McHardy return &mrt->vif_table[iter->ct]; 24331da177e4SLinus Torvalds } 24341da177e4SLinus Torvalds return NULL; 24351da177e4SLinus Torvalds } 24361da177e4SLinus Torvalds 24371da177e4SLinus Torvalds static void *ipmr_vif_seq_start(struct seq_file *seq, loff_t *pos) 2438ba93ef74SStephen Hemminger __acquires(mrt_lock) 24391da177e4SLinus Torvalds { 2440f0ad0860SPatrick McHardy struct ipmr_vif_iter *iter = seq->private; 2441f6bb4514SBenjamin Thery struct net *net = seq_file_net(seq); 2442f0ad0860SPatrick McHardy struct mr_table *mrt; 2443f0ad0860SPatrick McHardy 2444f0ad0860SPatrick McHardy mrt = ipmr_get_table(net, RT_TABLE_DEFAULT); 2445f0ad0860SPatrick McHardy if (mrt == NULL) 2446f0ad0860SPatrick McHardy return ERR_PTR(-ENOENT); 2447f0ad0860SPatrick McHardy 2448f0ad0860SPatrick McHardy iter->mrt = mrt; 2449f6bb4514SBenjamin Thery 24501da177e4SLinus Torvalds read_lock(&mrt_lock); 2451f6bb4514SBenjamin Thery return *pos ? ipmr_vif_seq_idx(net, seq->private, *pos - 1) 24521da177e4SLinus Torvalds : SEQ_START_TOKEN; 24531da177e4SLinus Torvalds } 24541da177e4SLinus Torvalds 24551da177e4SLinus Torvalds static void *ipmr_vif_seq_next(struct seq_file *seq, void *v, loff_t *pos) 24561da177e4SLinus Torvalds { 24571da177e4SLinus Torvalds struct ipmr_vif_iter *iter = seq->private; 2458f6bb4514SBenjamin Thery struct net *net = seq_file_net(seq); 2459f0ad0860SPatrick McHardy struct mr_table *mrt = iter->mrt; 24601da177e4SLinus Torvalds 24611da177e4SLinus Torvalds ++*pos; 24621da177e4SLinus Torvalds if (v == SEQ_START_TOKEN) 2463f6bb4514SBenjamin Thery return ipmr_vif_seq_idx(net, iter, 0); 24641da177e4SLinus Torvalds 24650c12295aSPatrick McHardy while (++iter->ct < mrt->maxvif) { 24660c12295aSPatrick McHardy if (!VIF_EXISTS(mrt, iter->ct)) 24671da177e4SLinus Torvalds continue; 24680c12295aSPatrick McHardy return &mrt->vif_table[iter->ct]; 24691da177e4SLinus Torvalds } 24701da177e4SLinus Torvalds return NULL; 24711da177e4SLinus Torvalds } 24721da177e4SLinus Torvalds 24731da177e4SLinus Torvalds static void ipmr_vif_seq_stop(struct seq_file *seq, void *v) 2474ba93ef74SStephen Hemminger __releases(mrt_lock) 24751da177e4SLinus Torvalds { 24761da177e4SLinus Torvalds read_unlock(&mrt_lock); 24771da177e4SLinus Torvalds } 24781da177e4SLinus Torvalds 24791da177e4SLinus Torvalds static int ipmr_vif_seq_show(struct seq_file *seq, void *v) 24801da177e4SLinus Torvalds { 2481f0ad0860SPatrick McHardy struct ipmr_vif_iter *iter = seq->private; 2482f0ad0860SPatrick McHardy struct mr_table *mrt = iter->mrt; 2483f6bb4514SBenjamin Thery 24841da177e4SLinus Torvalds if (v == SEQ_START_TOKEN) { 24851da177e4SLinus Torvalds seq_puts(seq, 24861da177e4SLinus Torvalds "Interface BytesIn PktsIn BytesOut PktsOut Flags Local Remote\n"); 24871da177e4SLinus Torvalds } else { 24881da177e4SLinus Torvalds const struct vif_device *vif = v; 24891da177e4SLinus Torvalds const char *name = vif->dev ? vif->dev->name : "none"; 24901da177e4SLinus Torvalds 24911da177e4SLinus Torvalds seq_printf(seq, 24921da177e4SLinus Torvalds "%2Zd %-10s %8ld %7ld %8ld %7ld %05X %08X %08X\n", 24930c12295aSPatrick McHardy vif - mrt->vif_table, 24941da177e4SLinus Torvalds name, vif->bytes_in, vif->pkt_in, 24951da177e4SLinus Torvalds vif->bytes_out, vif->pkt_out, 24961da177e4SLinus Torvalds vif->flags, vif->local, vif->remote); 24971da177e4SLinus Torvalds } 24981da177e4SLinus Torvalds return 0; 24991da177e4SLinus Torvalds } 25001da177e4SLinus Torvalds 2501f690808eSStephen Hemminger static const struct seq_operations ipmr_vif_seq_ops = { 25021da177e4SLinus Torvalds .start = ipmr_vif_seq_start, 25031da177e4SLinus Torvalds .next = ipmr_vif_seq_next, 25041da177e4SLinus Torvalds .stop = ipmr_vif_seq_stop, 25051da177e4SLinus Torvalds .show = ipmr_vif_seq_show, 25061da177e4SLinus Torvalds }; 25071da177e4SLinus Torvalds 25081da177e4SLinus Torvalds static int ipmr_vif_open(struct inode *inode, struct file *file) 25091da177e4SLinus Torvalds { 2510f6bb4514SBenjamin Thery return seq_open_net(inode, file, &ipmr_vif_seq_ops, 2511cf7732e4SPavel Emelyanov sizeof(struct ipmr_vif_iter)); 25121da177e4SLinus Torvalds } 25131da177e4SLinus Torvalds 25149a32144eSArjan van de Ven static const struct file_operations ipmr_vif_fops = { 25151da177e4SLinus Torvalds .owner = THIS_MODULE, 25161da177e4SLinus Torvalds .open = ipmr_vif_open, 25171da177e4SLinus Torvalds .read = seq_read, 25181da177e4SLinus Torvalds .llseek = seq_lseek, 2519f6bb4514SBenjamin Thery .release = seq_release_net, 25201da177e4SLinus Torvalds }; 25211da177e4SLinus Torvalds 25221da177e4SLinus Torvalds struct ipmr_mfc_iter { 2523f6bb4514SBenjamin Thery struct seq_net_private p; 2524f0ad0860SPatrick McHardy struct mr_table *mrt; 2525862465f2SPatrick McHardy struct list_head *cache; 25261da177e4SLinus Torvalds int ct; 25271da177e4SLinus Torvalds }; 25281da177e4SLinus Torvalds 25291da177e4SLinus Torvalds 2530f6bb4514SBenjamin Thery static struct mfc_cache *ipmr_mfc_seq_idx(struct net *net, 2531f6bb4514SBenjamin Thery struct ipmr_mfc_iter *it, loff_t pos) 25321da177e4SLinus Torvalds { 2533f0ad0860SPatrick McHardy struct mr_table *mrt = it->mrt; 25341da177e4SLinus Torvalds struct mfc_cache *mfc; 25351da177e4SLinus Torvalds 2536a8c9486bSEric Dumazet rcu_read_lock(); 2537862465f2SPatrick McHardy for (it->ct = 0; it->ct < MFC_LINES; it->ct++) { 25380c12295aSPatrick McHardy it->cache = &mrt->mfc_cache_array[it->ct]; 2539a8c9486bSEric Dumazet list_for_each_entry_rcu(mfc, it->cache, list) 25401da177e4SLinus Torvalds if (pos-- == 0) 25411da177e4SLinus Torvalds return mfc; 2542862465f2SPatrick McHardy } 2543a8c9486bSEric Dumazet rcu_read_unlock(); 25441da177e4SLinus Torvalds 25451da177e4SLinus Torvalds spin_lock_bh(&mfc_unres_lock); 25460c12295aSPatrick McHardy it->cache = &mrt->mfc_unres_queue; 2547862465f2SPatrick McHardy list_for_each_entry(mfc, it->cache, list) 2548e258beb2SPatrick McHardy if (pos-- == 0) 25491da177e4SLinus Torvalds return mfc; 25501da177e4SLinus Torvalds spin_unlock_bh(&mfc_unres_lock); 25511da177e4SLinus Torvalds 25521da177e4SLinus Torvalds it->cache = NULL; 25531da177e4SLinus Torvalds return NULL; 25541da177e4SLinus Torvalds } 25551da177e4SLinus Torvalds 25561da177e4SLinus Torvalds 25571da177e4SLinus Torvalds static void *ipmr_mfc_seq_start(struct seq_file *seq, loff_t *pos) 25581da177e4SLinus Torvalds { 25591da177e4SLinus Torvalds struct ipmr_mfc_iter *it = seq->private; 2560f6bb4514SBenjamin Thery struct net *net = seq_file_net(seq); 2561f0ad0860SPatrick McHardy struct mr_table *mrt; 2562f6bb4514SBenjamin Thery 2563f0ad0860SPatrick McHardy mrt = ipmr_get_table(net, RT_TABLE_DEFAULT); 2564f0ad0860SPatrick McHardy if (mrt == NULL) 2565f0ad0860SPatrick McHardy return ERR_PTR(-ENOENT); 2566f0ad0860SPatrick McHardy 2567f0ad0860SPatrick McHardy it->mrt = mrt; 25681da177e4SLinus Torvalds it->cache = NULL; 25691da177e4SLinus Torvalds it->ct = 0; 2570f6bb4514SBenjamin Thery return *pos ? ipmr_mfc_seq_idx(net, seq->private, *pos - 1) 25711da177e4SLinus Torvalds : SEQ_START_TOKEN; 25721da177e4SLinus Torvalds } 25731da177e4SLinus Torvalds 25741da177e4SLinus Torvalds static void *ipmr_mfc_seq_next(struct seq_file *seq, void *v, loff_t *pos) 25751da177e4SLinus Torvalds { 25761da177e4SLinus Torvalds struct mfc_cache *mfc = v; 25771da177e4SLinus Torvalds struct ipmr_mfc_iter *it = seq->private; 2578f6bb4514SBenjamin Thery struct net *net = seq_file_net(seq); 2579f0ad0860SPatrick McHardy struct mr_table *mrt = it->mrt; 25801da177e4SLinus Torvalds 25811da177e4SLinus Torvalds ++*pos; 25821da177e4SLinus Torvalds 25831da177e4SLinus Torvalds if (v == SEQ_START_TOKEN) 2584f6bb4514SBenjamin Thery return ipmr_mfc_seq_idx(net, seq->private, 0); 25851da177e4SLinus Torvalds 2586862465f2SPatrick McHardy if (mfc->list.next != it->cache) 2587862465f2SPatrick McHardy return list_entry(mfc->list.next, struct mfc_cache, list); 25881da177e4SLinus Torvalds 25890c12295aSPatrick McHardy if (it->cache == &mrt->mfc_unres_queue) 25901da177e4SLinus Torvalds goto end_of_list; 25911da177e4SLinus Torvalds 25920c12295aSPatrick McHardy BUG_ON(it->cache != &mrt->mfc_cache_array[it->ct]); 25931da177e4SLinus Torvalds 25941da177e4SLinus Torvalds while (++it->ct < MFC_LINES) { 25950c12295aSPatrick McHardy it->cache = &mrt->mfc_cache_array[it->ct]; 2596862465f2SPatrick McHardy if (list_empty(it->cache)) 2597862465f2SPatrick McHardy continue; 2598862465f2SPatrick McHardy return list_first_entry(it->cache, struct mfc_cache, list); 25991da177e4SLinus Torvalds } 26001da177e4SLinus Torvalds 26011da177e4SLinus Torvalds /* exhausted cache_array, show unresolved */ 2602a8c9486bSEric Dumazet rcu_read_unlock(); 26030c12295aSPatrick McHardy it->cache = &mrt->mfc_unres_queue; 26041da177e4SLinus Torvalds it->ct = 0; 26051da177e4SLinus Torvalds 26061da177e4SLinus Torvalds spin_lock_bh(&mfc_unres_lock); 2607862465f2SPatrick McHardy if (!list_empty(it->cache)) 2608862465f2SPatrick McHardy return list_first_entry(it->cache, struct mfc_cache, list); 26091da177e4SLinus Torvalds 26101da177e4SLinus Torvalds end_of_list: 26111da177e4SLinus Torvalds spin_unlock_bh(&mfc_unres_lock); 26121da177e4SLinus Torvalds it->cache = NULL; 26131da177e4SLinus Torvalds 26141da177e4SLinus Torvalds return NULL; 26151da177e4SLinus Torvalds } 26161da177e4SLinus Torvalds 26171da177e4SLinus Torvalds static void ipmr_mfc_seq_stop(struct seq_file *seq, void *v) 26181da177e4SLinus Torvalds { 26191da177e4SLinus Torvalds struct ipmr_mfc_iter *it = seq->private; 2620f0ad0860SPatrick McHardy struct mr_table *mrt = it->mrt; 26211da177e4SLinus Torvalds 26220c12295aSPatrick McHardy if (it->cache == &mrt->mfc_unres_queue) 26231da177e4SLinus Torvalds spin_unlock_bh(&mfc_unres_lock); 26240c12295aSPatrick McHardy else if (it->cache == &mrt->mfc_cache_array[it->ct]) 2625a8c9486bSEric Dumazet rcu_read_unlock(); 26261da177e4SLinus Torvalds } 26271da177e4SLinus Torvalds 26281da177e4SLinus Torvalds static int ipmr_mfc_seq_show(struct seq_file *seq, void *v) 26291da177e4SLinus Torvalds { 26301da177e4SLinus Torvalds int n; 26311da177e4SLinus Torvalds 26321da177e4SLinus Torvalds if (v == SEQ_START_TOKEN) { 26331da177e4SLinus Torvalds seq_puts(seq, 26341da177e4SLinus Torvalds "Group Origin Iif Pkts Bytes Wrong Oifs\n"); 26351da177e4SLinus Torvalds } else { 26361da177e4SLinus Torvalds const struct mfc_cache *mfc = v; 26371da177e4SLinus Torvalds const struct ipmr_mfc_iter *it = seq->private; 2638f0ad0860SPatrick McHardy const struct mr_table *mrt = it->mrt; 26391da177e4SLinus Torvalds 26400eae88f3SEric Dumazet seq_printf(seq, "%08X %08X %-3hd", 26410eae88f3SEric Dumazet (__force u32) mfc->mfc_mcastgrp, 26420eae88f3SEric Dumazet (__force u32) mfc->mfc_origin, 26431ea472e2SBenjamin Thery mfc->mfc_parent); 26441ea472e2SBenjamin Thery 26450c12295aSPatrick McHardy if (it->cache != &mrt->mfc_unres_queue) { 26461ea472e2SBenjamin Thery seq_printf(seq, " %8lu %8lu %8lu", 26471da177e4SLinus Torvalds mfc->mfc_un.res.pkt, 26481da177e4SLinus Torvalds mfc->mfc_un.res.bytes, 26491da177e4SLinus Torvalds mfc->mfc_un.res.wrong_if); 26501da177e4SLinus Torvalds for (n = mfc->mfc_un.res.minvif; 26511da177e4SLinus Torvalds n < mfc->mfc_un.res.maxvif; n++) { 26520c12295aSPatrick McHardy if (VIF_EXISTS(mrt, n) && 2653cf958ae3SBenjamin Thery mfc->mfc_un.res.ttls[n] < 255) 26541da177e4SLinus Torvalds seq_printf(seq, 26551da177e4SLinus Torvalds " %2d:%-3d", 26561da177e4SLinus Torvalds n, mfc->mfc_un.res.ttls[n]); 26571da177e4SLinus Torvalds } 26581ea472e2SBenjamin Thery } else { 26591ea472e2SBenjamin Thery /* unresolved mfc_caches don't contain 26601ea472e2SBenjamin Thery * pkt, bytes and wrong_if values 26611ea472e2SBenjamin Thery */ 26621ea472e2SBenjamin Thery seq_printf(seq, " %8lu %8lu %8lu", 0ul, 0ul, 0ul); 26631da177e4SLinus Torvalds } 26641da177e4SLinus Torvalds seq_putc(seq, '\n'); 26651da177e4SLinus Torvalds } 26661da177e4SLinus Torvalds return 0; 26671da177e4SLinus Torvalds } 26681da177e4SLinus Torvalds 2669f690808eSStephen Hemminger static const struct seq_operations ipmr_mfc_seq_ops = { 26701da177e4SLinus Torvalds .start = ipmr_mfc_seq_start, 26711da177e4SLinus Torvalds .next = ipmr_mfc_seq_next, 26721da177e4SLinus Torvalds .stop = ipmr_mfc_seq_stop, 26731da177e4SLinus Torvalds .show = ipmr_mfc_seq_show, 26741da177e4SLinus Torvalds }; 26751da177e4SLinus Torvalds 26761da177e4SLinus Torvalds static int ipmr_mfc_open(struct inode *inode, struct file *file) 26771da177e4SLinus Torvalds { 2678f6bb4514SBenjamin Thery return seq_open_net(inode, file, &ipmr_mfc_seq_ops, 2679cf7732e4SPavel Emelyanov sizeof(struct ipmr_mfc_iter)); 26801da177e4SLinus Torvalds } 26811da177e4SLinus Torvalds 26829a32144eSArjan van de Ven static const struct file_operations ipmr_mfc_fops = { 26831da177e4SLinus Torvalds .owner = THIS_MODULE, 26841da177e4SLinus Torvalds .open = ipmr_mfc_open, 26851da177e4SLinus Torvalds .read = seq_read, 26861da177e4SLinus Torvalds .llseek = seq_lseek, 2687f6bb4514SBenjamin Thery .release = seq_release_net, 26881da177e4SLinus Torvalds }; 26891da177e4SLinus Torvalds #endif 26901da177e4SLinus Torvalds 26911da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM_V2 269232613090SAlexey Dobriyan static const struct net_protocol pim_protocol = { 26931da177e4SLinus Torvalds .handler = pim_rcv, 2694403dbb97STom Goff .netns_ok = 1, 26951da177e4SLinus Torvalds }; 26961da177e4SLinus Torvalds #endif 26971da177e4SLinus Torvalds 26981da177e4SLinus Torvalds 26991da177e4SLinus Torvalds /* 27001da177e4SLinus Torvalds * Setup for IP multicast routing 27011da177e4SLinus Torvalds */ 2702cf958ae3SBenjamin Thery static int __net_init ipmr_net_init(struct net *net) 2703cf958ae3SBenjamin Thery { 2704f0ad0860SPatrick McHardy int err; 2705cf958ae3SBenjamin Thery 2706f0ad0860SPatrick McHardy err = ipmr_rules_init(net); 2707f0ad0860SPatrick McHardy if (err < 0) 2708cf958ae3SBenjamin Thery goto fail; 2709f6bb4514SBenjamin Thery 2710f6bb4514SBenjamin Thery #ifdef CONFIG_PROC_FS 2711f6bb4514SBenjamin Thery err = -ENOMEM; 2712d4beaa66SGao feng if (!proc_create("ip_mr_vif", 0, net->proc_net, &ipmr_vif_fops)) 2713f6bb4514SBenjamin Thery goto proc_vif_fail; 2714d4beaa66SGao feng if (!proc_create("ip_mr_cache", 0, net->proc_net, &ipmr_mfc_fops)) 2715f6bb4514SBenjamin Thery goto proc_cache_fail; 2716f6bb4514SBenjamin Thery #endif 27172bb8b26cSBenjamin Thery return 0; 27182bb8b26cSBenjamin Thery 2719f6bb4514SBenjamin Thery #ifdef CONFIG_PROC_FS 2720f6bb4514SBenjamin Thery proc_cache_fail: 2721ece31ffdSGao feng remove_proc_entry("ip_mr_vif", net->proc_net); 2722f6bb4514SBenjamin Thery proc_vif_fail: 2723f0ad0860SPatrick McHardy ipmr_rules_exit(net); 2724f6bb4514SBenjamin Thery #endif 2725cf958ae3SBenjamin Thery fail: 2726cf958ae3SBenjamin Thery return err; 2727cf958ae3SBenjamin Thery } 2728cf958ae3SBenjamin Thery 2729cf958ae3SBenjamin Thery static void __net_exit ipmr_net_exit(struct net *net) 2730cf958ae3SBenjamin Thery { 2731f6bb4514SBenjamin Thery #ifdef CONFIG_PROC_FS 2732ece31ffdSGao feng remove_proc_entry("ip_mr_cache", net->proc_net); 2733ece31ffdSGao feng remove_proc_entry("ip_mr_vif", net->proc_net); 2734f6bb4514SBenjamin Thery #endif 2735f0ad0860SPatrick McHardy ipmr_rules_exit(net); 2736cf958ae3SBenjamin Thery } 2737cf958ae3SBenjamin Thery 2738cf958ae3SBenjamin Thery static struct pernet_operations ipmr_net_ops = { 2739cf958ae3SBenjamin Thery .init = ipmr_net_init, 2740cf958ae3SBenjamin Thery .exit = ipmr_net_exit, 2741cf958ae3SBenjamin Thery }; 27421da177e4SLinus Torvalds 274303d2f897SWang Chen int __init ip_mr_init(void) 27441da177e4SLinus Torvalds { 274503d2f897SWang Chen int err; 274603d2f897SWang Chen 27471da177e4SLinus Torvalds mrt_cachep = kmem_cache_create("ip_mrt_cache", 27481da177e4SLinus Torvalds sizeof(struct mfc_cache), 2749e5d679f3SAlexey Dobriyan 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, 275020c2df83SPaul Mundt NULL); 275103d2f897SWang Chen if (!mrt_cachep) 275203d2f897SWang Chen return -ENOMEM; 275303d2f897SWang Chen 2754cf958ae3SBenjamin Thery err = register_pernet_subsys(&ipmr_net_ops); 2755cf958ae3SBenjamin Thery if (err) 2756cf958ae3SBenjamin Thery goto reg_pernet_fail; 2757cf958ae3SBenjamin Thery 275803d2f897SWang Chen err = register_netdevice_notifier(&ip_mr_notifier); 275903d2f897SWang Chen if (err) 276003d2f897SWang Chen goto reg_notif_fail; 2761403dbb97STom Goff #ifdef CONFIG_IP_PIMSM_V2 2762403dbb97STom Goff if (inet_add_protocol(&pim_protocol, IPPROTO_PIM) < 0) { 2763058bd4d2SJoe Perches pr_err("%s: can't add PIM protocol\n", __func__); 2764403dbb97STom Goff err = -EAGAIN; 2765403dbb97STom Goff goto add_proto_fail; 2766403dbb97STom Goff } 2767403dbb97STom Goff #endif 2768c7ac8679SGreg Rose rtnl_register(RTNL_FAMILY_IPMR, RTM_GETROUTE, 2769c7ac8679SGreg Rose NULL, ipmr_rtm_dumproute, NULL); 277003d2f897SWang Chen return 0; 2771f6bb4514SBenjamin Thery 2772403dbb97STom Goff #ifdef CONFIG_IP_PIMSM_V2 2773403dbb97STom Goff add_proto_fail: 2774403dbb97STom Goff unregister_netdevice_notifier(&ip_mr_notifier); 2775403dbb97STom Goff #endif 2776c3e38896SBenjamin Thery reg_notif_fail: 2777cf958ae3SBenjamin Thery unregister_pernet_subsys(&ipmr_net_ops); 2778cf958ae3SBenjamin Thery reg_pernet_fail: 2779c3e38896SBenjamin Thery kmem_cache_destroy(mrt_cachep); 278003d2f897SWang Chen return err; 27811da177e4SLinus Torvalds } 2782