12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 21da177e4SLinus Torvalds /* 31da177e4SLinus Torvalds * IP multicast routing support for mrouted 3.6/3.8 41da177e4SLinus Torvalds * 5113aa838SAlan Cox * (c) 1995 Alan Cox, <alan@lxorguk.ukuu.org.uk> 61da177e4SLinus Torvalds * Linux Consultancy and Custom Driver Development 71da177e4SLinus Torvalds * 81da177e4SLinus Torvalds * Fixes: 91da177e4SLinus Torvalds * Michael Chastain : Incorrect size of copying. 101da177e4SLinus Torvalds * Alan Cox : Added the cache manager code 111da177e4SLinus Torvalds * Alan Cox : Fixed the clone/copy bug and device race. 121da177e4SLinus Torvalds * Mike McLagan : Routing by source 131da177e4SLinus Torvalds * Malcolm Beattie : Buffer handling fixes. 141da177e4SLinus Torvalds * Alexey Kuznetsov : Double buffer free and other fixes. 151da177e4SLinus Torvalds * SVR Anand : Fixed several multicast bugs and problems. 161da177e4SLinus Torvalds * Alexey Kuznetsov : Status, optimisations and more. 171da177e4SLinus Torvalds * Brad Parker : Better behaviour on mrouted upcall 181da177e4SLinus Torvalds * overflow. 191da177e4SLinus Torvalds * Carlos Picoto : PIMv1 Support 201da177e4SLinus Torvalds * Pavlin Ivanov Radoslavov: PIMv2 Registers must checksum only PIM header 21f77f13e2SGilles Espinasse * Relax this requirement to work with older peers. 221da177e4SLinus Torvalds */ 231da177e4SLinus Torvalds 247c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 251da177e4SLinus Torvalds #include <linux/types.h> 2608009a76SAlexey Dobriyan #include <linux/cache.h> 274fc268d2SRandy Dunlap #include <linux/capability.h> 281da177e4SLinus Torvalds #include <linux/errno.h> 291da177e4SLinus Torvalds #include <linux/mm.h> 301da177e4SLinus Torvalds #include <linux/kernel.h> 311da177e4SLinus Torvalds #include <linux/fcntl.h> 321da177e4SLinus Torvalds #include <linux/stat.h> 331da177e4SLinus Torvalds #include <linux/socket.h> 341da177e4SLinus Torvalds #include <linux/in.h> 351da177e4SLinus Torvalds #include <linux/inet.h> 361da177e4SLinus Torvalds #include <linux/netdevice.h> 371da177e4SLinus Torvalds #include <linux/inetdevice.h> 381da177e4SLinus Torvalds #include <linux/igmp.h> 391da177e4SLinus Torvalds #include <linux/proc_fs.h> 401da177e4SLinus Torvalds #include <linux/seq_file.h> 411da177e4SLinus Torvalds #include <linux/mroute.h> 421da177e4SLinus Torvalds #include <linux/init.h> 4346f25dffSKris Katterjohn #include <linux/if_ether.h> 445a0e3ad6STejun Heo #include <linux/slab.h> 45457c4cbcSEric W. Biederman #include <net/net_namespace.h> 461da177e4SLinus Torvalds #include <net/ip.h> 471da177e4SLinus Torvalds #include <net/protocol.h> 481da177e4SLinus Torvalds #include <linux/skbuff.h> 4914c85021SArnaldo Carvalho de Melo #include <net/route.h> 501da177e4SLinus Torvalds #include <net/icmp.h> 511da177e4SLinus Torvalds #include <net/udp.h> 521da177e4SLinus Torvalds #include <net/raw.h> 531da177e4SLinus Torvalds #include <linux/notifier.h> 541da177e4SLinus Torvalds #include <linux/if_arp.h> 551da177e4SLinus Torvalds #include <linux/netfilter_ipv4.h> 56709b46e8SEric W. Biederman #include <linux/compat.h> 57bc3b2d7fSPaul Gortmaker #include <linux/export.h> 580eb71a9dSNeilBrown #include <linux/rhashtable.h> 59c5441932SPravin B Shelar #include <net/ip_tunnels.h> 601da177e4SLinus Torvalds #include <net/checksum.h> 61dc5fc579SArnaldo Carvalho de Melo #include <net/netlink.h> 62f0ad0860SPatrick McHardy #include <net/fib_rules.h> 63d67b8c61SNicolas Dichtel #include <linux/netconf.h> 643c618c1dSDavid Ahern #include <net/rtnh.h> 651da177e4SLinus Torvalds 665648451eSGustavo A. R. Silva #include <linux/nospec.h> 675648451eSGustavo A. R. Silva 68f0ad0860SPatrick McHardy struct ipmr_rule { 69f0ad0860SPatrick McHardy struct fib_rule common; 70f0ad0860SPatrick McHardy }; 71f0ad0860SPatrick McHardy 72f0ad0860SPatrick McHardy struct ipmr_result { 73f0ad0860SPatrick McHardy struct mr_table *mrt; 74f0ad0860SPatrick McHardy }; 75f0ad0860SPatrick McHardy 761da177e4SLinus Torvalds /* Big lock, protecting vif table, mrt cache and mroute socket state. 77a8cb16ddSEric Dumazet * Note that the changes are semaphored via rtnl_lock. 781da177e4SLinus Torvalds */ 791da177e4SLinus Torvalds 801da177e4SLinus Torvalds static DEFINE_RWLOCK(mrt_lock); 811da177e4SLinus Torvalds 827ef8f65dSNikolay Aleksandrov /* Multicast router control variables */ 831da177e4SLinus Torvalds 841da177e4SLinus Torvalds /* Special spinlock for queue of unresolved entries */ 851da177e4SLinus Torvalds static DEFINE_SPINLOCK(mfc_unres_lock); 861da177e4SLinus Torvalds 871da177e4SLinus Torvalds /* We return to original Alan's scheme. Hash table of resolved 88a8cb16ddSEric Dumazet * entries is changed only in process context and protected 89a8cb16ddSEric Dumazet * with weak lock mrt_lock. Queue of unresolved entries is protected 90a8cb16ddSEric Dumazet * with strong spinlock mfc_unres_lock. 91a8cb16ddSEric Dumazet * 92a8cb16ddSEric Dumazet * In this case data path is free of exclusive locks at all. 931da177e4SLinus Torvalds */ 941da177e4SLinus Torvalds 9508009a76SAlexey Dobriyan static struct kmem_cache *mrt_cachep __ro_after_init; 961da177e4SLinus Torvalds 97f0ad0860SPatrick McHardy static struct mr_table *ipmr_new_table(struct net *net, u32 id); 98acbb219dSFrancesco Ruggeri static void ipmr_free_table(struct mr_table *mrt); 99acbb219dSFrancesco Ruggeri 100c4854ec8SRami Rosen static void ip_mr_forward(struct net *net, struct mr_table *mrt, 1014b1f0d33SDonald Sharp struct net_device *dev, struct sk_buff *skb, 1024b1f0d33SDonald Sharp struct mfc_cache *cache, int local); 1030c12295aSPatrick McHardy static int ipmr_cache_report(struct mr_table *mrt, 1044feb88e5SBenjamin Thery struct sk_buff *pkt, vifi_t vifi, int assert); 1058cd3ac9fSNicolas Dichtel static void mroute_netlink_event(struct mr_table *mrt, struct mfc_cache *mfc, 1068cd3ac9fSNicolas Dichtel int cmd); 1075a645dd8SJulien Gomes static void igmpmsg_netlink_event(struct mr_table *mrt, struct sk_buff *pkt); 108ca8d4794SCallum Sinclair static void mroute_clean_tables(struct mr_table *mrt, int flags); 109e99e88a9SKees Cook static void ipmr_expire_process(struct timer_list *t); 1101da177e4SLinus Torvalds 111f0ad0860SPatrick McHardy #ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES 112f0ad0860SPatrick McHardy #define ipmr_for_each_table(mrt, net) \ 113f0ad0860SPatrick McHardy list_for_each_entry_rcu(mrt, &net->ipv4.mr_tables, list) 114f0ad0860SPatrick McHardy 1157b0db857SYuval Mintz static struct mr_table *ipmr_mr_table_iter(struct net *net, 1167b0db857SYuval Mintz struct mr_table *mrt) 1177b0db857SYuval Mintz { 1187b0db857SYuval Mintz struct mr_table *ret; 1197b0db857SYuval Mintz 1207b0db857SYuval Mintz if (!mrt) 1217b0db857SYuval Mintz ret = list_entry_rcu(net->ipv4.mr_tables.next, 1227b0db857SYuval Mintz struct mr_table, list); 1237b0db857SYuval Mintz else 1247b0db857SYuval Mintz ret = list_entry_rcu(mrt->list.next, 1257b0db857SYuval Mintz struct mr_table, list); 1267b0db857SYuval Mintz 1277b0db857SYuval Mintz if (&ret->list == &net->ipv4.mr_tables) 1287b0db857SYuval Mintz return NULL; 1297b0db857SYuval Mintz return ret; 1307b0db857SYuval Mintz } 1317b0db857SYuval Mintz 132f0ad0860SPatrick McHardy static struct mr_table *ipmr_get_table(struct net *net, u32 id) 133f0ad0860SPatrick McHardy { 134f0ad0860SPatrick McHardy struct mr_table *mrt; 135f0ad0860SPatrick McHardy 136f0ad0860SPatrick McHardy ipmr_for_each_table(mrt, net) { 137f0ad0860SPatrick McHardy if (mrt->id == id) 138f0ad0860SPatrick McHardy return mrt; 139f0ad0860SPatrick McHardy } 140f0ad0860SPatrick McHardy return NULL; 141f0ad0860SPatrick McHardy } 142f0ad0860SPatrick McHardy 143da91981bSDavid S. Miller static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4, 144f0ad0860SPatrick McHardy struct mr_table **mrt) 145f0ad0860SPatrick McHardy { 146f0ad0860SPatrick McHardy int err; 14795f4a45dSHannes Frederic Sowa struct ipmr_result res; 14895f4a45dSHannes Frederic Sowa struct fib_lookup_arg arg = { 14995f4a45dSHannes Frederic Sowa .result = &res, 15095f4a45dSHannes Frederic Sowa .flags = FIB_LOOKUP_NOREF, 15195f4a45dSHannes Frederic Sowa }; 152f0ad0860SPatrick McHardy 153e58e4159SDavid Ahern /* update flow if oif or iif point to device enslaved to l3mdev */ 154e58e4159SDavid Ahern l3mdev_update_flow(net, flowi4_to_flowi(flp4)); 155e58e4159SDavid Ahern 156da91981bSDavid S. Miller err = fib_rules_lookup(net->ipv4.mr_rules_ops, 157da91981bSDavid S. Miller flowi4_to_flowi(flp4), 0, &arg); 158f0ad0860SPatrick McHardy if (err < 0) 159f0ad0860SPatrick McHardy return err; 160f0ad0860SPatrick McHardy *mrt = res.mrt; 161f0ad0860SPatrick McHardy return 0; 162f0ad0860SPatrick McHardy } 163f0ad0860SPatrick McHardy 164f0ad0860SPatrick McHardy static int ipmr_rule_action(struct fib_rule *rule, struct flowi *flp, 165f0ad0860SPatrick McHardy int flags, struct fib_lookup_arg *arg) 166f0ad0860SPatrick McHardy { 167f0ad0860SPatrick McHardy struct ipmr_result *res = arg->result; 168f0ad0860SPatrick McHardy struct mr_table *mrt; 169f0ad0860SPatrick McHardy 170f0ad0860SPatrick McHardy switch (rule->action) { 171f0ad0860SPatrick McHardy case FR_ACT_TO_TBL: 172f0ad0860SPatrick McHardy break; 173f0ad0860SPatrick McHardy case FR_ACT_UNREACHABLE: 174f0ad0860SPatrick McHardy return -ENETUNREACH; 175f0ad0860SPatrick McHardy case FR_ACT_PROHIBIT: 176f0ad0860SPatrick McHardy return -EACCES; 177f0ad0860SPatrick McHardy case FR_ACT_BLACKHOLE: 178f0ad0860SPatrick McHardy default: 179f0ad0860SPatrick McHardy return -EINVAL; 180f0ad0860SPatrick McHardy } 181f0ad0860SPatrick McHardy 182e58e4159SDavid Ahern arg->table = fib_rule_get_table(rule, arg); 183e58e4159SDavid Ahern 184e58e4159SDavid Ahern mrt = ipmr_get_table(rule->fr_net, arg->table); 18551456b29SIan Morris if (!mrt) 186f0ad0860SPatrick McHardy return -EAGAIN; 187f0ad0860SPatrick McHardy res->mrt = mrt; 188f0ad0860SPatrick McHardy return 0; 189f0ad0860SPatrick McHardy } 190f0ad0860SPatrick McHardy 191f0ad0860SPatrick McHardy static int ipmr_rule_match(struct fib_rule *rule, struct flowi *fl, int flags) 192f0ad0860SPatrick McHardy { 193f0ad0860SPatrick McHardy return 1; 194f0ad0860SPatrick McHardy } 195f0ad0860SPatrick McHardy 196f0ad0860SPatrick McHardy static const struct nla_policy ipmr_rule_policy[FRA_MAX + 1] = { 197f0ad0860SPatrick McHardy FRA_GENERIC_POLICY, 198f0ad0860SPatrick McHardy }; 199f0ad0860SPatrick McHardy 200f0ad0860SPatrick McHardy static int ipmr_rule_configure(struct fib_rule *rule, struct sk_buff *skb, 201b16fb418SRoopa Prabhu struct fib_rule_hdr *frh, struct nlattr **tb, 202b16fb418SRoopa Prabhu struct netlink_ext_ack *extack) 203f0ad0860SPatrick McHardy { 204f0ad0860SPatrick McHardy return 0; 205f0ad0860SPatrick McHardy } 206f0ad0860SPatrick McHardy 207f0ad0860SPatrick McHardy static int ipmr_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh, 208f0ad0860SPatrick McHardy struct nlattr **tb) 209f0ad0860SPatrick McHardy { 210f0ad0860SPatrick McHardy return 1; 211f0ad0860SPatrick McHardy } 212f0ad0860SPatrick McHardy 213f0ad0860SPatrick McHardy static int ipmr_rule_fill(struct fib_rule *rule, struct sk_buff *skb, 214f0ad0860SPatrick McHardy struct fib_rule_hdr *frh) 215f0ad0860SPatrick McHardy { 216f0ad0860SPatrick McHardy frh->dst_len = 0; 217f0ad0860SPatrick McHardy frh->src_len = 0; 218f0ad0860SPatrick McHardy frh->tos = 0; 219f0ad0860SPatrick McHardy return 0; 220f0ad0860SPatrick McHardy } 221f0ad0860SPatrick McHardy 22204a6f82cSAndi Kleen static const struct fib_rules_ops __net_initconst ipmr_rules_ops_template = { 22325239ceeSPatrick McHardy .family = RTNL_FAMILY_IPMR, 224f0ad0860SPatrick McHardy .rule_size = sizeof(struct ipmr_rule), 225f0ad0860SPatrick McHardy .addr_size = sizeof(u32), 226f0ad0860SPatrick McHardy .action = ipmr_rule_action, 227f0ad0860SPatrick McHardy .match = ipmr_rule_match, 228f0ad0860SPatrick McHardy .configure = ipmr_rule_configure, 229f0ad0860SPatrick McHardy .compare = ipmr_rule_compare, 230f0ad0860SPatrick McHardy .fill = ipmr_rule_fill, 231f0ad0860SPatrick McHardy .nlgroup = RTNLGRP_IPV4_RULE, 232f0ad0860SPatrick McHardy .policy = ipmr_rule_policy, 233f0ad0860SPatrick McHardy .owner = THIS_MODULE, 234f0ad0860SPatrick McHardy }; 235f0ad0860SPatrick McHardy 236f0ad0860SPatrick McHardy static int __net_init ipmr_rules_init(struct net *net) 237f0ad0860SPatrick McHardy { 238f0ad0860SPatrick McHardy struct fib_rules_ops *ops; 239f0ad0860SPatrick McHardy struct mr_table *mrt; 240f0ad0860SPatrick McHardy int err; 241f0ad0860SPatrick McHardy 242f0ad0860SPatrick McHardy ops = fib_rules_register(&ipmr_rules_ops_template, net); 243f0ad0860SPatrick McHardy if (IS_ERR(ops)) 244f0ad0860SPatrick McHardy return PTR_ERR(ops); 245f0ad0860SPatrick McHardy 246f0ad0860SPatrick McHardy INIT_LIST_HEAD(&net->ipv4.mr_tables); 247f0ad0860SPatrick McHardy 248f0ad0860SPatrick McHardy mrt = ipmr_new_table(net, RT_TABLE_DEFAULT); 2491113ebbcSNikolay Aleksandrov if (IS_ERR(mrt)) { 2501113ebbcSNikolay Aleksandrov err = PTR_ERR(mrt); 251f0ad0860SPatrick McHardy goto err1; 252f0ad0860SPatrick McHardy } 253f0ad0860SPatrick McHardy 254f0ad0860SPatrick McHardy err = fib_default_rule_add(ops, 0x7fff, RT_TABLE_DEFAULT, 0); 255f0ad0860SPatrick McHardy if (err < 0) 256f0ad0860SPatrick McHardy goto err2; 257f0ad0860SPatrick McHardy 258f0ad0860SPatrick McHardy net->ipv4.mr_rules_ops = ops; 259f0ad0860SPatrick McHardy return 0; 260f0ad0860SPatrick McHardy 261f0ad0860SPatrick McHardy err2: 262f243e5a7SWANG Cong ipmr_free_table(mrt); 263f0ad0860SPatrick McHardy err1: 264f0ad0860SPatrick McHardy fib_rules_unregister(ops); 265f0ad0860SPatrick McHardy return err; 266f0ad0860SPatrick McHardy } 267f0ad0860SPatrick McHardy 268f0ad0860SPatrick McHardy static void __net_exit ipmr_rules_exit(struct net *net) 269f0ad0860SPatrick McHardy { 270f0ad0860SPatrick McHardy struct mr_table *mrt, *next; 271f0ad0860SPatrick McHardy 272ed785309SWANG Cong rtnl_lock(); 273035320d5SEric Dumazet list_for_each_entry_safe(mrt, next, &net->ipv4.mr_tables, list) { 274035320d5SEric Dumazet list_del(&mrt->list); 275acbb219dSFrancesco Ruggeri ipmr_free_table(mrt); 276035320d5SEric Dumazet } 277f0ad0860SPatrick McHardy fib_rules_unregister(net->ipv4.mr_rules_ops); 278419df12fSWANG Cong rtnl_unlock(); 279f0ad0860SPatrick McHardy } 2804d65b948SYotam Gigi 281b7a59557SJiri Pirko static int ipmr_rules_dump(struct net *net, struct notifier_block *nb, 282b7a59557SJiri Pirko struct netlink_ext_ack *extack) 2834d65b948SYotam Gigi { 284b7a59557SJiri Pirko return fib_rules_dump(net, nb, RTNL_FAMILY_IPMR, extack); 2854d65b948SYotam Gigi } 2864d65b948SYotam Gigi 2874d65b948SYotam Gigi static unsigned int ipmr_rules_seq_read(struct net *net) 2884d65b948SYotam Gigi { 2894d65b948SYotam Gigi return fib_rules_seq_read(net, RTNL_FAMILY_IPMR); 2904d65b948SYotam Gigi } 291478e4c2fSYotam Gigi 292478e4c2fSYotam Gigi bool ipmr_rule_default(const struct fib_rule *rule) 293478e4c2fSYotam Gigi { 294478e4c2fSYotam Gigi return fib_rule_matchall(rule) && rule->table == RT_TABLE_DEFAULT; 295478e4c2fSYotam Gigi } 296478e4c2fSYotam Gigi EXPORT_SYMBOL(ipmr_rule_default); 297f0ad0860SPatrick McHardy #else 298f0ad0860SPatrick McHardy #define ipmr_for_each_table(mrt, net) \ 299f0ad0860SPatrick McHardy for (mrt = net->ipv4.mrt; mrt; mrt = NULL) 300f0ad0860SPatrick McHardy 3017b0db857SYuval Mintz static struct mr_table *ipmr_mr_table_iter(struct net *net, 3027b0db857SYuval Mintz struct mr_table *mrt) 3037b0db857SYuval Mintz { 3047b0db857SYuval Mintz if (!mrt) 3057b0db857SYuval Mintz return net->ipv4.mrt; 3067b0db857SYuval Mintz return NULL; 3077b0db857SYuval Mintz } 3087b0db857SYuval Mintz 309f0ad0860SPatrick McHardy static struct mr_table *ipmr_get_table(struct net *net, u32 id) 310f0ad0860SPatrick McHardy { 311f0ad0860SPatrick McHardy return net->ipv4.mrt; 312f0ad0860SPatrick McHardy } 313f0ad0860SPatrick McHardy 314da91981bSDavid S. Miller static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4, 315f0ad0860SPatrick McHardy struct mr_table **mrt) 316f0ad0860SPatrick McHardy { 317f0ad0860SPatrick McHardy *mrt = net->ipv4.mrt; 318f0ad0860SPatrick McHardy return 0; 319f0ad0860SPatrick McHardy } 320f0ad0860SPatrick McHardy 321f0ad0860SPatrick McHardy static int __net_init ipmr_rules_init(struct net *net) 322f0ad0860SPatrick McHardy { 3231113ebbcSNikolay Aleksandrov struct mr_table *mrt; 3241113ebbcSNikolay Aleksandrov 3251113ebbcSNikolay Aleksandrov mrt = ipmr_new_table(net, RT_TABLE_DEFAULT); 3261113ebbcSNikolay Aleksandrov if (IS_ERR(mrt)) 3271113ebbcSNikolay Aleksandrov return PTR_ERR(mrt); 3281113ebbcSNikolay Aleksandrov net->ipv4.mrt = mrt; 3291113ebbcSNikolay Aleksandrov return 0; 330f0ad0860SPatrick McHardy } 331f0ad0860SPatrick McHardy 332f0ad0860SPatrick McHardy static void __net_exit ipmr_rules_exit(struct net *net) 333f0ad0860SPatrick McHardy { 334ed785309SWANG Cong rtnl_lock(); 335acbb219dSFrancesco Ruggeri ipmr_free_table(net->ipv4.mrt); 336ed785309SWANG Cong net->ipv4.mrt = NULL; 337ed785309SWANG Cong rtnl_unlock(); 338f0ad0860SPatrick McHardy } 3394d65b948SYotam Gigi 340b7a59557SJiri Pirko static int ipmr_rules_dump(struct net *net, struct notifier_block *nb, 341b7a59557SJiri Pirko struct netlink_ext_ack *extack) 3424d65b948SYotam Gigi { 3434d65b948SYotam Gigi return 0; 3444d65b948SYotam Gigi } 3454d65b948SYotam Gigi 3464d65b948SYotam Gigi static unsigned int ipmr_rules_seq_read(struct net *net) 3474d65b948SYotam Gigi { 3484d65b948SYotam Gigi return 0; 3494d65b948SYotam Gigi } 350478e4c2fSYotam Gigi 351478e4c2fSYotam Gigi bool ipmr_rule_default(const struct fib_rule *rule) 352478e4c2fSYotam Gigi { 353478e4c2fSYotam Gigi return true; 354478e4c2fSYotam Gigi } 355478e4c2fSYotam Gigi EXPORT_SYMBOL(ipmr_rule_default); 356f0ad0860SPatrick McHardy #endif 357f0ad0860SPatrick McHardy 3588fb472c0SNikolay Aleksandrov static inline int ipmr_hash_cmp(struct rhashtable_compare_arg *arg, 3598fb472c0SNikolay Aleksandrov const void *ptr) 3608fb472c0SNikolay Aleksandrov { 3618fb472c0SNikolay Aleksandrov const struct mfc_cache_cmp_arg *cmparg = arg->key; 3628fb472c0SNikolay Aleksandrov struct mfc_cache *c = (struct mfc_cache *)ptr; 3638fb472c0SNikolay Aleksandrov 3648fb472c0SNikolay Aleksandrov return cmparg->mfc_mcastgrp != c->mfc_mcastgrp || 3658fb472c0SNikolay Aleksandrov cmparg->mfc_origin != c->mfc_origin; 3668fb472c0SNikolay Aleksandrov } 3678fb472c0SNikolay Aleksandrov 3688fb472c0SNikolay Aleksandrov static const struct rhashtable_params ipmr_rht_params = { 369494fff56SYuval Mintz .head_offset = offsetof(struct mr_mfc, mnode), 3708fb472c0SNikolay Aleksandrov .key_offset = offsetof(struct mfc_cache, cmparg), 3718fb472c0SNikolay Aleksandrov .key_len = sizeof(struct mfc_cache_cmp_arg), 3728fb472c0SNikolay Aleksandrov .nelem_hint = 3, 3738fb472c0SNikolay Aleksandrov .obj_cmpfn = ipmr_hash_cmp, 3748fb472c0SNikolay Aleksandrov .automatic_shrinking = true, 3758fb472c0SNikolay Aleksandrov }; 3768fb472c0SNikolay Aleksandrov 3770bbbf0e7SYuval Mintz static void ipmr_new_table_set(struct mr_table *mrt, 3780bbbf0e7SYuval Mintz struct net *net) 3790bbbf0e7SYuval Mintz { 3800bbbf0e7SYuval Mintz #ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES 3810bbbf0e7SYuval Mintz list_add_tail_rcu(&mrt->list, &net->ipv4.mr_tables); 3820bbbf0e7SYuval Mintz #endif 3830bbbf0e7SYuval Mintz } 3840bbbf0e7SYuval Mintz 385845c9a7aSYuval Mintz static struct mfc_cache_cmp_arg ipmr_mr_table_ops_cmparg_any = { 386845c9a7aSYuval Mintz .mfc_mcastgrp = htonl(INADDR_ANY), 387845c9a7aSYuval Mintz .mfc_origin = htonl(INADDR_ANY), 388845c9a7aSYuval Mintz }; 389845c9a7aSYuval Mintz 390845c9a7aSYuval Mintz static struct mr_table_ops ipmr_mr_table_ops = { 391845c9a7aSYuval Mintz .rht_params = &ipmr_rht_params, 392845c9a7aSYuval Mintz .cmparg_any = &ipmr_mr_table_ops_cmparg_any, 393845c9a7aSYuval Mintz }; 394845c9a7aSYuval Mintz 395f0ad0860SPatrick McHardy static struct mr_table *ipmr_new_table(struct net *net, u32 id) 396f0ad0860SPatrick McHardy { 397f0ad0860SPatrick McHardy struct mr_table *mrt; 398f0ad0860SPatrick McHardy 3991113ebbcSNikolay Aleksandrov /* "pimreg%u" should not exceed 16 bytes (IFNAMSIZ) */ 4001113ebbcSNikolay Aleksandrov if (id != RT_TABLE_DEFAULT && id >= 1000000000) 4011113ebbcSNikolay Aleksandrov return ERR_PTR(-EINVAL); 4021113ebbcSNikolay Aleksandrov 403f0ad0860SPatrick McHardy mrt = ipmr_get_table(net, id); 40400db4124SIan Morris if (mrt) 405f0ad0860SPatrick McHardy return mrt; 406f0ad0860SPatrick McHardy 407845c9a7aSYuval Mintz return mr_table_alloc(net, id, &ipmr_mr_table_ops, 4080bbbf0e7SYuval Mintz ipmr_expire_process, ipmr_new_table_set); 409f0ad0860SPatrick McHardy } 4101da177e4SLinus Torvalds 411acbb219dSFrancesco Ruggeri static void ipmr_free_table(struct mr_table *mrt) 412acbb219dSFrancesco Ruggeri { 413acbb219dSFrancesco Ruggeri del_timer_sync(&mrt->ipmr_expire_timer); 414ca8d4794SCallum Sinclair mroute_clean_tables(mrt, MRT_FLUSH_VIFS | MRT_FLUSH_VIFS_STATIC | 415ca8d4794SCallum Sinclair MRT_FLUSH_MFC | MRT_FLUSH_MFC_STATIC); 4168fb472c0SNikolay Aleksandrov rhltable_destroy(&mrt->mfc_hash); 417acbb219dSFrancesco Ruggeri kfree(mrt); 418acbb219dSFrancesco Ruggeri } 419acbb219dSFrancesco Ruggeri 4201da177e4SLinus Torvalds /* Service routines creating virtual interfaces: DVMRP tunnels and PIMREG */ 4211da177e4SLinus Torvalds 422d607032dSWang Chen static void ipmr_del_tunnel(struct net_device *dev, struct vifctl *v) 423d607032dSWang Chen { 4244feb88e5SBenjamin Thery struct net *net = dev_net(dev); 4254feb88e5SBenjamin Thery 426d607032dSWang Chen dev_close(dev); 427d607032dSWang Chen 4284feb88e5SBenjamin Thery dev = __dev_get_by_name(net, "tunl0"); 429d607032dSWang Chen if (dev) { 4305bc3eb7eSStephen Hemminger const struct net_device_ops *ops = dev->netdev_ops; 431d607032dSWang Chen struct ifreq ifr; 432d607032dSWang Chen struct ip_tunnel_parm p; 433d607032dSWang Chen 434d607032dSWang Chen memset(&p, 0, sizeof(p)); 435d607032dSWang Chen p.iph.daddr = v->vifc_rmt_addr.s_addr; 436d607032dSWang Chen p.iph.saddr = v->vifc_lcl_addr.s_addr; 437d607032dSWang Chen p.iph.version = 4; 438d607032dSWang Chen p.iph.ihl = 5; 439d607032dSWang Chen p.iph.protocol = IPPROTO_IPIP; 440d607032dSWang Chen sprintf(p.name, "dvmrp%d", v->vifc_vifi); 441d607032dSWang Chen ifr.ifr_ifru.ifru_data = (__force void __user *)&p; 442d607032dSWang Chen 4435bc3eb7eSStephen Hemminger if (ops->ndo_do_ioctl) { 4445bc3eb7eSStephen Hemminger mm_segment_t oldfs = get_fs(); 4455bc3eb7eSStephen Hemminger 4465bc3eb7eSStephen Hemminger set_fs(KERNEL_DS); 4475bc3eb7eSStephen Hemminger ops->ndo_do_ioctl(dev, &ifr, SIOCDELTUNNEL); 448d607032dSWang Chen set_fs(oldfs); 449d607032dSWang Chen } 450d607032dSWang Chen } 4515bc3eb7eSStephen Hemminger } 452d607032dSWang Chen 453a0b47736SNikolay Aleksandrov /* Initialize ipmr pimreg/tunnel in_device */ 454a0b47736SNikolay Aleksandrov static bool ipmr_init_vif_indev(const struct net_device *dev) 455a0b47736SNikolay Aleksandrov { 456a0b47736SNikolay Aleksandrov struct in_device *in_dev; 457a0b47736SNikolay Aleksandrov 458a0b47736SNikolay Aleksandrov ASSERT_RTNL(); 459a0b47736SNikolay Aleksandrov 460a0b47736SNikolay Aleksandrov in_dev = __in_dev_get_rtnl(dev); 461a0b47736SNikolay Aleksandrov if (!in_dev) 462a0b47736SNikolay Aleksandrov return false; 463a0b47736SNikolay Aleksandrov ipv4_devconf_setall(in_dev); 464a0b47736SNikolay Aleksandrov neigh_parms_data_state_setall(in_dev->arp_parms); 465a0b47736SNikolay Aleksandrov IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0; 466a0b47736SNikolay Aleksandrov 467a0b47736SNikolay Aleksandrov return true; 468a0b47736SNikolay Aleksandrov } 469a0b47736SNikolay Aleksandrov 4707ef8f65dSNikolay Aleksandrov static struct net_device *ipmr_new_tunnel(struct net *net, struct vifctl *v) 4711da177e4SLinus Torvalds { 4721da177e4SLinus Torvalds struct net_device *dev; 4731da177e4SLinus Torvalds 4744feb88e5SBenjamin Thery dev = __dev_get_by_name(net, "tunl0"); 4751da177e4SLinus Torvalds 4761da177e4SLinus Torvalds if (dev) { 4775bc3eb7eSStephen Hemminger const struct net_device_ops *ops = dev->netdev_ops; 4781da177e4SLinus Torvalds int err; 4791da177e4SLinus Torvalds struct ifreq ifr; 4801da177e4SLinus Torvalds struct ip_tunnel_parm p; 4811da177e4SLinus Torvalds 4821da177e4SLinus Torvalds memset(&p, 0, sizeof(p)); 4831da177e4SLinus Torvalds p.iph.daddr = v->vifc_rmt_addr.s_addr; 4841da177e4SLinus Torvalds p.iph.saddr = v->vifc_lcl_addr.s_addr; 4851da177e4SLinus Torvalds p.iph.version = 4; 4861da177e4SLinus Torvalds p.iph.ihl = 5; 4871da177e4SLinus Torvalds p.iph.protocol = IPPROTO_IPIP; 4881da177e4SLinus Torvalds sprintf(p.name, "dvmrp%d", v->vifc_vifi); 489ba93ef74SStephen Hemminger ifr.ifr_ifru.ifru_data = (__force void __user *)&p; 4901da177e4SLinus Torvalds 4915bc3eb7eSStephen Hemminger if (ops->ndo_do_ioctl) { 4925bc3eb7eSStephen Hemminger mm_segment_t oldfs = get_fs(); 4935bc3eb7eSStephen Hemminger 4945bc3eb7eSStephen Hemminger set_fs(KERNEL_DS); 4955bc3eb7eSStephen Hemminger err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL); 4961da177e4SLinus Torvalds set_fs(oldfs); 497a8cb16ddSEric Dumazet } else { 4985bc3eb7eSStephen Hemminger err = -EOPNOTSUPP; 499a8cb16ddSEric Dumazet } 5001da177e4SLinus Torvalds dev = NULL; 5011da177e4SLinus Torvalds 5024feb88e5SBenjamin Thery if (err == 0 && 5034feb88e5SBenjamin Thery (dev = __dev_get_by_name(net, p.name)) != NULL) { 5041da177e4SLinus Torvalds dev->flags |= IFF_MULTICAST; 505a0b47736SNikolay Aleksandrov if (!ipmr_init_vif_indev(dev)) 5061da177e4SLinus Torvalds goto failure; 50700f54e68SPetr Machata if (dev_open(dev, NULL)) 5081da177e4SLinus Torvalds goto failure; 5097dc00c82SWang Chen dev_hold(dev); 5101da177e4SLinus Torvalds } 5111da177e4SLinus Torvalds } 5121da177e4SLinus Torvalds return dev; 5131da177e4SLinus Torvalds 5141da177e4SLinus Torvalds failure: 5151da177e4SLinus Torvalds unregister_netdevice(dev); 5161da177e4SLinus Torvalds return NULL; 5171da177e4SLinus Torvalds } 5181da177e4SLinus Torvalds 519c316c629SNikolay Aleksandrov #if defined(CONFIG_IP_PIMSM_V1) || defined(CONFIG_IP_PIMSM_V2) 5206fef4c0cSStephen Hemminger static netdev_tx_t reg_vif_xmit(struct sk_buff *skb, struct net_device *dev) 5211da177e4SLinus Torvalds { 5224feb88e5SBenjamin Thery struct net *net = dev_net(dev); 523f0ad0860SPatrick McHardy struct mr_table *mrt; 524da91981bSDavid S. Miller struct flowi4 fl4 = { 525da91981bSDavid S. Miller .flowi4_oif = dev->ifindex, 5266a662719SCong Wang .flowi4_iif = skb->skb_iif ? : LOOPBACK_IFINDEX, 527da91981bSDavid S. Miller .flowi4_mark = skb->mark, 528f0ad0860SPatrick McHardy }; 529f0ad0860SPatrick McHardy int err; 530f0ad0860SPatrick McHardy 531da91981bSDavid S. Miller err = ipmr_fib_lookup(net, &fl4, &mrt); 532e40dbc51SBen Greear if (err < 0) { 533e40dbc51SBen Greear kfree_skb(skb); 534f0ad0860SPatrick McHardy return err; 535e40dbc51SBen Greear } 5364feb88e5SBenjamin Thery 5371da177e4SLinus Torvalds read_lock(&mrt_lock); 538cf3677aeSPavel Emelyanov dev->stats.tx_bytes += skb->len; 539cf3677aeSPavel Emelyanov dev->stats.tx_packets++; 5400c12295aSPatrick McHardy ipmr_cache_report(mrt, skb, mrt->mroute_reg_vif_num, IGMPMSG_WHOLEPKT); 5411da177e4SLinus Torvalds read_unlock(&mrt_lock); 5421da177e4SLinus Torvalds kfree_skb(skb); 5436ed10654SPatrick McHardy return NETDEV_TX_OK; 5441da177e4SLinus Torvalds } 5451da177e4SLinus Torvalds 546ee9b9596SNicolas Dichtel static int reg_vif_get_iflink(const struct net_device *dev) 547ee9b9596SNicolas Dichtel { 548ee9b9596SNicolas Dichtel return 0; 549ee9b9596SNicolas Dichtel } 550ee9b9596SNicolas Dichtel 551007c3838SStephen Hemminger static const struct net_device_ops reg_vif_netdev_ops = { 552007c3838SStephen Hemminger .ndo_start_xmit = reg_vif_xmit, 553ee9b9596SNicolas Dichtel .ndo_get_iflink = reg_vif_get_iflink, 554007c3838SStephen Hemminger }; 555007c3838SStephen Hemminger 5561da177e4SLinus Torvalds static void reg_vif_setup(struct net_device *dev) 5571da177e4SLinus Torvalds { 5581da177e4SLinus Torvalds dev->type = ARPHRD_PIMREG; 55946f25dffSKris Katterjohn dev->mtu = ETH_DATA_LEN - sizeof(struct iphdr) - 8; 5601da177e4SLinus Torvalds dev->flags = IFF_NOARP; 56170cb4a45SHimangi Saraogi dev->netdev_ops = ®_vif_netdev_ops; 562cf124db5SDavid S. Miller dev->needs_free_netdev = true; 563403dbb97STom Goff dev->features |= NETIF_F_NETNS_LOCAL; 5641da177e4SLinus Torvalds } 5651da177e4SLinus Torvalds 566f0ad0860SPatrick McHardy static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt) 5671da177e4SLinus Torvalds { 5681da177e4SLinus Torvalds struct net_device *dev; 569f0ad0860SPatrick McHardy char name[IFNAMSIZ]; 5701da177e4SLinus Torvalds 571f0ad0860SPatrick McHardy if (mrt->id == RT_TABLE_DEFAULT) 572f0ad0860SPatrick McHardy sprintf(name, "pimreg"); 573f0ad0860SPatrick McHardy else 574f0ad0860SPatrick McHardy sprintf(name, "pimreg%u", mrt->id); 575f0ad0860SPatrick McHardy 576c835a677STom Gundersen dev = alloc_netdev(0, name, NET_NAME_UNKNOWN, reg_vif_setup); 5771da177e4SLinus Torvalds 57851456b29SIan Morris if (!dev) 5791da177e4SLinus Torvalds return NULL; 5801da177e4SLinus Torvalds 581403dbb97STom Goff dev_net_set(dev, net); 582403dbb97STom Goff 5831da177e4SLinus Torvalds if (register_netdevice(dev)) { 5841da177e4SLinus Torvalds free_netdev(dev); 5851da177e4SLinus Torvalds return NULL; 5861da177e4SLinus Torvalds } 5871da177e4SLinus Torvalds 588a0b47736SNikolay Aleksandrov if (!ipmr_init_vif_indev(dev)) 5891da177e4SLinus Torvalds goto failure; 59000f54e68SPetr Machata if (dev_open(dev, NULL)) 5911da177e4SLinus Torvalds goto failure; 5921da177e4SLinus Torvalds 5937dc00c82SWang Chen dev_hold(dev); 5947dc00c82SWang Chen 5951da177e4SLinus Torvalds return dev; 5961da177e4SLinus Torvalds 5971da177e4SLinus Torvalds failure: 5981da177e4SLinus Torvalds unregister_netdevice(dev); 5991da177e4SLinus Torvalds return NULL; 6001da177e4SLinus Torvalds } 601c316c629SNikolay Aleksandrov 602c316c629SNikolay Aleksandrov /* called with rcu_read_lock() */ 603c316c629SNikolay Aleksandrov static int __pim_rcv(struct mr_table *mrt, struct sk_buff *skb, 604c316c629SNikolay Aleksandrov unsigned int pimlen) 605c316c629SNikolay Aleksandrov { 606c316c629SNikolay Aleksandrov struct net_device *reg_dev = NULL; 607c316c629SNikolay Aleksandrov struct iphdr *encap; 608c316c629SNikolay Aleksandrov 609c316c629SNikolay Aleksandrov encap = (struct iphdr *)(skb_transport_header(skb) + pimlen); 6107ef8f65dSNikolay Aleksandrov /* Check that: 611c316c629SNikolay Aleksandrov * a. packet is really sent to a multicast group 612c316c629SNikolay Aleksandrov * b. packet is not a NULL-REGISTER 613c316c629SNikolay Aleksandrov * c. packet is not truncated 614c316c629SNikolay Aleksandrov */ 615c316c629SNikolay Aleksandrov if (!ipv4_is_multicast(encap->daddr) || 616c316c629SNikolay Aleksandrov encap->tot_len == 0 || 617c316c629SNikolay Aleksandrov ntohs(encap->tot_len) + pimlen > skb->len) 618c316c629SNikolay Aleksandrov return 1; 619c316c629SNikolay Aleksandrov 620c316c629SNikolay Aleksandrov read_lock(&mrt_lock); 621c316c629SNikolay Aleksandrov if (mrt->mroute_reg_vif_num >= 0) 622c316c629SNikolay Aleksandrov reg_dev = mrt->vif_table[mrt->mroute_reg_vif_num].dev; 623c316c629SNikolay Aleksandrov read_unlock(&mrt_lock); 624c316c629SNikolay Aleksandrov 625c316c629SNikolay Aleksandrov if (!reg_dev) 626c316c629SNikolay Aleksandrov return 1; 627c316c629SNikolay Aleksandrov 628c316c629SNikolay Aleksandrov skb->mac_header = skb->network_header; 629c316c629SNikolay Aleksandrov skb_pull(skb, (u8 *)encap - skb->data); 630c316c629SNikolay Aleksandrov skb_reset_network_header(skb); 631c316c629SNikolay Aleksandrov skb->protocol = htons(ETH_P_IP); 632c316c629SNikolay Aleksandrov skb->ip_summed = CHECKSUM_NONE; 633c316c629SNikolay Aleksandrov 634c316c629SNikolay Aleksandrov skb_tunnel_rx(skb, reg_dev, dev_net(reg_dev)); 635c316c629SNikolay Aleksandrov 636c316c629SNikolay Aleksandrov netif_rx(skb); 637c316c629SNikolay Aleksandrov 638c316c629SNikolay Aleksandrov return NET_RX_SUCCESS; 639c316c629SNikolay Aleksandrov } 640c316c629SNikolay Aleksandrov #else 641c316c629SNikolay Aleksandrov static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt) 642c316c629SNikolay Aleksandrov { 643c316c629SNikolay Aleksandrov return NULL; 644c316c629SNikolay Aleksandrov } 6451da177e4SLinus Torvalds #endif 6461da177e4SLinus Torvalds 647b362053aSYotam Gigi static int call_ipmr_vif_entry_notifiers(struct net *net, 648b362053aSYotam Gigi enum fib_event_type event_type, 649b362053aSYotam Gigi struct vif_device *vif, 650b362053aSYotam Gigi vifi_t vif_index, u32 tb_id) 651b362053aSYotam Gigi { 652bc67a0daSYuval Mintz return mr_call_vif_notifiers(net, RTNL_FAMILY_IPMR, event_type, 653bc67a0daSYuval Mintz vif, vif_index, tb_id, 654bc67a0daSYuval Mintz &net->ipv4.ipmr_seq); 655b362053aSYotam Gigi } 656b362053aSYotam Gigi 657b362053aSYotam Gigi static int call_ipmr_mfc_entry_notifiers(struct net *net, 658b362053aSYotam Gigi enum fib_event_type event_type, 659b362053aSYotam Gigi struct mfc_cache *mfc, u32 tb_id) 660b362053aSYotam Gigi { 66154c4cad9SYuval Mintz return mr_call_mfc_notifiers(net, RTNL_FAMILY_IPMR, event_type, 66254c4cad9SYuval Mintz &mfc->_c, tb_id, &net->ipv4.ipmr_seq); 663b362053aSYotam Gigi } 664b362053aSYotam Gigi 6652c53040fSBen Hutchings /** 6662c53040fSBen Hutchings * vif_delete - Delete a VIF entry 6677dc00c82SWang Chen * @notify: Set to 1, if the caller is a notifier_call 6681da177e4SLinus Torvalds */ 6690c12295aSPatrick McHardy static int vif_delete(struct mr_table *mrt, int vifi, int notify, 670d17fa6faSEric Dumazet struct list_head *head) 6711da177e4SLinus Torvalds { 672b362053aSYotam Gigi struct net *net = read_pnet(&mrt->net); 6731da177e4SLinus Torvalds struct vif_device *v; 6741da177e4SLinus Torvalds struct net_device *dev; 6751da177e4SLinus Torvalds struct in_device *in_dev; 6761da177e4SLinus Torvalds 6770c12295aSPatrick McHardy if (vifi < 0 || vifi >= mrt->maxvif) 6781da177e4SLinus Torvalds return -EADDRNOTAVAIL; 6791da177e4SLinus Torvalds 6800c12295aSPatrick McHardy v = &mrt->vif_table[vifi]; 6811da177e4SLinus Torvalds 682b362053aSYotam Gigi if (VIF_EXISTS(mrt, vifi)) 683b362053aSYotam Gigi call_ipmr_vif_entry_notifiers(net, FIB_EVENT_VIF_DEL, v, vifi, 684b362053aSYotam Gigi mrt->id); 685b362053aSYotam Gigi 6861da177e4SLinus Torvalds write_lock_bh(&mrt_lock); 6871da177e4SLinus Torvalds dev = v->dev; 6881da177e4SLinus Torvalds v->dev = NULL; 6891da177e4SLinus Torvalds 6901da177e4SLinus Torvalds if (!dev) { 6911da177e4SLinus Torvalds write_unlock_bh(&mrt_lock); 6921da177e4SLinus Torvalds return -EADDRNOTAVAIL; 6931da177e4SLinus Torvalds } 6941da177e4SLinus Torvalds 6950c12295aSPatrick McHardy if (vifi == mrt->mroute_reg_vif_num) 6960c12295aSPatrick McHardy mrt->mroute_reg_vif_num = -1; 6971da177e4SLinus Torvalds 6980c12295aSPatrick McHardy if (vifi + 1 == mrt->maxvif) { 6991da177e4SLinus Torvalds int tmp; 700a8cb16ddSEric Dumazet 7011da177e4SLinus Torvalds for (tmp = vifi - 1; tmp >= 0; tmp--) { 7020c12295aSPatrick McHardy if (VIF_EXISTS(mrt, tmp)) 7031da177e4SLinus Torvalds break; 7041da177e4SLinus Torvalds } 7050c12295aSPatrick McHardy mrt->maxvif = tmp+1; 7061da177e4SLinus Torvalds } 7071da177e4SLinus Torvalds 7081da177e4SLinus Torvalds write_unlock_bh(&mrt_lock); 7091da177e4SLinus Torvalds 7101da177e4SLinus Torvalds dev_set_allmulti(dev, -1); 7111da177e4SLinus Torvalds 712a8cb16ddSEric Dumazet in_dev = __in_dev_get_rtnl(dev); 713a8cb16ddSEric Dumazet if (in_dev) { 71442f811b8SHerbert Xu IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)--; 7153b022865SDavid Ahern inet_netconf_notify_devconf(dev_net(dev), RTM_NEWNETCONF, 716d67b8c61SNicolas Dichtel NETCONFA_MC_FORWARDING, 717d67b8c61SNicolas Dichtel dev->ifindex, &in_dev->cnf); 7181da177e4SLinus Torvalds ip_rt_multicast_event(in_dev); 7191da177e4SLinus Torvalds } 7201da177e4SLinus Torvalds 7217dc00c82SWang Chen if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER) && !notify) 722d17fa6faSEric Dumazet unregister_netdevice_queue(dev, head); 7231da177e4SLinus Torvalds 7241da177e4SLinus Torvalds dev_put(dev); 7251da177e4SLinus Torvalds return 0; 7261da177e4SLinus Torvalds } 7271da177e4SLinus Torvalds 728a8c9486bSEric Dumazet static void ipmr_cache_free_rcu(struct rcu_head *head) 729a8c9486bSEric Dumazet { 730494fff56SYuval Mintz struct mr_mfc *c = container_of(head, struct mr_mfc, rcu); 731a8c9486bSEric Dumazet 732494fff56SYuval Mintz kmem_cache_free(mrt_cachep, (struct mfc_cache *)c); 733a8c9486bSEric Dumazet } 734a8c9486bSEric Dumazet 7358c13af2aSYuval Mintz static void ipmr_cache_free(struct mfc_cache *c) 7365c0a66f5SBenjamin Thery { 737494fff56SYuval Mintz call_rcu(&c->_c.rcu, ipmr_cache_free_rcu); 7385c0a66f5SBenjamin Thery } 7395c0a66f5SBenjamin Thery 7401da177e4SLinus Torvalds /* Destroy an unresolved cache entry, killing queued skbs 741a8cb16ddSEric Dumazet * and reporting error to netlink readers. 7421da177e4SLinus Torvalds */ 7430c12295aSPatrick McHardy static void ipmr_destroy_unres(struct mr_table *mrt, struct mfc_cache *c) 7441da177e4SLinus Torvalds { 7458de53dfbSPatrick McHardy struct net *net = read_pnet(&mrt->net); 7461da177e4SLinus Torvalds struct sk_buff *skb; 7479ef1d4c7SPatrick McHardy struct nlmsgerr *e; 7481da177e4SLinus Torvalds 7490c12295aSPatrick McHardy atomic_dec(&mrt->cache_resolve_queue_len); 7501da177e4SLinus Torvalds 751494fff56SYuval Mintz while ((skb = skb_dequeue(&c->_c.mfc_un.unres.unresolved))) { 752eddc9ec5SArnaldo Carvalho de Melo if (ip_hdr(skb)->version == 0) { 753af72868bSJohannes Berg struct nlmsghdr *nlh = skb_pull(skb, 754af72868bSJohannes Berg sizeof(struct iphdr)); 7551da177e4SLinus Torvalds nlh->nlmsg_type = NLMSG_ERROR; 756573ce260SHong zhi guo nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr)); 7571da177e4SLinus Torvalds skb_trim(skb, nlh->nlmsg_len); 758573ce260SHong zhi guo e = nlmsg_data(nlh); 7599ef1d4c7SPatrick McHardy e->error = -ETIMEDOUT; 7609ef1d4c7SPatrick McHardy memset(&e->msg, 0, sizeof(e->msg)); 7612942e900SThomas Graf 76215e47304SEric W. Biederman rtnl_unicast(skb, net, NETLINK_CB(skb).portid); 763a8cb16ddSEric Dumazet } else { 7641da177e4SLinus Torvalds kfree_skb(skb); 7651da177e4SLinus Torvalds } 766a8cb16ddSEric Dumazet } 7671da177e4SLinus Torvalds 7685c0a66f5SBenjamin Thery ipmr_cache_free(c); 7691da177e4SLinus Torvalds } 7701da177e4SLinus Torvalds 771e258beb2SPatrick McHardy /* Timer process for the unresolved queue. */ 772e99e88a9SKees Cook static void ipmr_expire_process(struct timer_list *t) 7731da177e4SLinus Torvalds { 774e99e88a9SKees Cook struct mr_table *mrt = from_timer(mrt, t, ipmr_expire_timer); 775494fff56SYuval Mintz struct mr_mfc *c, *next; 7761da177e4SLinus Torvalds unsigned long expires; 777494fff56SYuval Mintz unsigned long now; 7781da177e4SLinus Torvalds 7791da177e4SLinus Torvalds if (!spin_trylock(&mfc_unres_lock)) { 7800c12295aSPatrick McHardy mod_timer(&mrt->ipmr_expire_timer, jiffies+HZ/10); 7811da177e4SLinus Torvalds return; 7821da177e4SLinus Torvalds } 7831da177e4SLinus Torvalds 7840c12295aSPatrick McHardy if (list_empty(&mrt->mfc_unres_queue)) 7851da177e4SLinus Torvalds goto out; 7861da177e4SLinus Torvalds 7871da177e4SLinus Torvalds now = jiffies; 7881da177e4SLinus Torvalds expires = 10*HZ; 7891da177e4SLinus Torvalds 7900c12295aSPatrick McHardy list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) { 7911da177e4SLinus Torvalds if (time_after(c->mfc_un.unres.expires, now)) { 7921da177e4SLinus Torvalds unsigned long interval = c->mfc_un.unres.expires - now; 7931da177e4SLinus Torvalds if (interval < expires) 7941da177e4SLinus Torvalds expires = interval; 7951da177e4SLinus Torvalds continue; 7961da177e4SLinus Torvalds } 7971da177e4SLinus Torvalds 798862465f2SPatrick McHardy list_del(&c->list); 799494fff56SYuval Mintz mroute_netlink_event(mrt, (struct mfc_cache *)c, RTM_DELROUTE); 800494fff56SYuval Mintz ipmr_destroy_unres(mrt, (struct mfc_cache *)c); 8011da177e4SLinus Torvalds } 8021da177e4SLinus Torvalds 8030c12295aSPatrick McHardy if (!list_empty(&mrt->mfc_unres_queue)) 8040c12295aSPatrick McHardy mod_timer(&mrt->ipmr_expire_timer, jiffies + expires); 8051da177e4SLinus Torvalds 8061da177e4SLinus Torvalds out: 8071da177e4SLinus Torvalds spin_unlock(&mfc_unres_lock); 8081da177e4SLinus Torvalds } 8091da177e4SLinus Torvalds 8101da177e4SLinus Torvalds /* Fill oifs list. It is called under write locked mrt_lock. */ 811494fff56SYuval Mintz static void ipmr_update_thresholds(struct mr_table *mrt, struct mr_mfc *cache, 812d658f8a0SPatrick McHardy unsigned char *ttls) 8131da177e4SLinus Torvalds { 8141da177e4SLinus Torvalds int vifi; 8151da177e4SLinus Torvalds 8161da177e4SLinus Torvalds cache->mfc_un.res.minvif = MAXVIFS; 8171da177e4SLinus Torvalds cache->mfc_un.res.maxvif = 0; 8181da177e4SLinus Torvalds memset(cache->mfc_un.res.ttls, 255, MAXVIFS); 8191da177e4SLinus Torvalds 8200c12295aSPatrick McHardy for (vifi = 0; vifi < mrt->maxvif; vifi++) { 8210c12295aSPatrick McHardy if (VIF_EXISTS(mrt, vifi) && 822cf958ae3SBenjamin Thery ttls[vifi] && ttls[vifi] < 255) { 8231da177e4SLinus Torvalds cache->mfc_un.res.ttls[vifi] = ttls[vifi]; 8241da177e4SLinus Torvalds if (cache->mfc_un.res.minvif > vifi) 8251da177e4SLinus Torvalds cache->mfc_un.res.minvif = vifi; 8261da177e4SLinus Torvalds if (cache->mfc_un.res.maxvif <= vifi) 8271da177e4SLinus Torvalds cache->mfc_un.res.maxvif = vifi + 1; 8281da177e4SLinus Torvalds } 8291da177e4SLinus Torvalds } 83090b5ca17SNikolay Aleksandrov cache->mfc_un.res.lastuse = jiffies; 8311da177e4SLinus Torvalds } 8321da177e4SLinus Torvalds 8330c12295aSPatrick McHardy static int vif_add(struct net *net, struct mr_table *mrt, 8340c12295aSPatrick McHardy struct vifctl *vifc, int mrtsock) 8351da177e4SLinus Torvalds { 836bccb3025SFlorian Fainelli struct netdev_phys_item_id ppid = { }; 8371da177e4SLinus Torvalds int vifi = vifc->vifc_vifi; 8380c12295aSPatrick McHardy struct vif_device *v = &mrt->vif_table[vifi]; 8391da177e4SLinus Torvalds struct net_device *dev; 8401da177e4SLinus Torvalds struct in_device *in_dev; 841d607032dSWang Chen int err; 8421da177e4SLinus Torvalds 8431da177e4SLinus Torvalds /* Is vif busy ? */ 8440c12295aSPatrick McHardy if (VIF_EXISTS(mrt, vifi)) 8451da177e4SLinus Torvalds return -EADDRINUSE; 8461da177e4SLinus Torvalds 8471da177e4SLinus Torvalds switch (vifc->vifc_flags) { 8481da177e4SLinus Torvalds case VIFF_REGISTER: 8491973a4eaSNikolay Aleksandrov if (!ipmr_pimsm_enabled()) 850c316c629SNikolay Aleksandrov return -EINVAL; 851c316c629SNikolay Aleksandrov /* Special Purpose VIF in PIM 8521da177e4SLinus Torvalds * All the packets will be sent to the daemon 8531da177e4SLinus Torvalds */ 8540c12295aSPatrick McHardy if (mrt->mroute_reg_vif_num >= 0) 8551da177e4SLinus Torvalds return -EADDRINUSE; 856f0ad0860SPatrick McHardy dev = ipmr_reg_vif(net, mrt); 8571da177e4SLinus Torvalds if (!dev) 8581da177e4SLinus Torvalds return -ENOBUFS; 859d607032dSWang Chen err = dev_set_allmulti(dev, 1); 860d607032dSWang Chen if (err) { 861d607032dSWang Chen unregister_netdevice(dev); 8627dc00c82SWang Chen dev_put(dev); 863d607032dSWang Chen return err; 864d607032dSWang Chen } 8651da177e4SLinus Torvalds break; 8661da177e4SLinus Torvalds case VIFF_TUNNEL: 8674feb88e5SBenjamin Thery dev = ipmr_new_tunnel(net, vifc); 8681da177e4SLinus Torvalds if (!dev) 8691da177e4SLinus Torvalds return -ENOBUFS; 870d607032dSWang Chen err = dev_set_allmulti(dev, 1); 871d607032dSWang Chen if (err) { 872d607032dSWang Chen ipmr_del_tunnel(dev, vifc); 8737dc00c82SWang Chen dev_put(dev); 874d607032dSWang Chen return err; 875d607032dSWang Chen } 8761da177e4SLinus Torvalds break; 877ee5e81f0SIlia K case VIFF_USE_IFINDEX: 8781da177e4SLinus Torvalds case 0: 879ee5e81f0SIlia K if (vifc->vifc_flags == VIFF_USE_IFINDEX) { 880ee5e81f0SIlia K dev = dev_get_by_index(net, vifc->vifc_lcl_ifindex); 88151456b29SIan Morris if (dev && !__in_dev_get_rtnl(dev)) { 882ee5e81f0SIlia K dev_put(dev); 883ee5e81f0SIlia K return -EADDRNOTAVAIL; 884ee5e81f0SIlia K } 885a8cb16ddSEric Dumazet } else { 8864feb88e5SBenjamin Thery dev = ip_dev_find(net, vifc->vifc_lcl_addr.s_addr); 887a8cb16ddSEric Dumazet } 8881da177e4SLinus Torvalds if (!dev) 8891da177e4SLinus Torvalds return -EADDRNOTAVAIL; 890d607032dSWang Chen err = dev_set_allmulti(dev, 1); 8917dc00c82SWang Chen if (err) { 8927dc00c82SWang Chen dev_put(dev); 893d607032dSWang Chen return err; 8947dc00c82SWang Chen } 8951da177e4SLinus Torvalds break; 8961da177e4SLinus Torvalds default: 8971da177e4SLinus Torvalds return -EINVAL; 8981da177e4SLinus Torvalds } 8991da177e4SLinus Torvalds 900a8cb16ddSEric Dumazet in_dev = __in_dev_get_rtnl(dev); 901a8cb16ddSEric Dumazet if (!in_dev) { 902d0490cfdSDan Carpenter dev_put(dev); 9031da177e4SLinus Torvalds return -EADDRNOTAVAIL; 904d0490cfdSDan Carpenter } 90542f811b8SHerbert Xu IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)++; 9063b022865SDavid Ahern inet_netconf_notify_devconf(net, RTM_NEWNETCONF, NETCONFA_MC_FORWARDING, 9073b022865SDavid Ahern dev->ifindex, &in_dev->cnf); 9081da177e4SLinus Torvalds ip_rt_multicast_event(in_dev); 9091da177e4SLinus Torvalds 910a8cb16ddSEric Dumazet /* Fill in the VIF structures */ 9116853f21fSYuval Mintz vif_device_init(v, dev, vifc->vifc_rate_limit, 9126853f21fSYuval Mintz vifc->vifc_threshold, 9136853f21fSYuval Mintz vifc->vifc_flags | (!mrtsock ? VIFF_STATIC : 0), 9146853f21fSYuval Mintz (VIFF_TUNNEL | VIFF_REGISTER)); 915a8cb16ddSEric Dumazet 916bccb3025SFlorian Fainelli err = dev_get_port_parent_id(dev, &ppid, true); 917bccb3025SFlorian Fainelli if (err == 0) { 918bccb3025SFlorian Fainelli memcpy(v->dev_parent_id.id, ppid.id, ppid.id_len); 919bccb3025SFlorian Fainelli v->dev_parent_id.id_len = ppid.id_len; 9205d8b3e69SYotam Gigi } else { 9215d8b3e69SYotam Gigi v->dev_parent_id.id_len = 0; 9225d8b3e69SYotam Gigi } 9236853f21fSYuval Mintz 9241da177e4SLinus Torvalds v->local = vifc->vifc_lcl_addr.s_addr; 9251da177e4SLinus Torvalds v->remote = vifc->vifc_rmt_addr.s_addr; 9261da177e4SLinus Torvalds 9271da177e4SLinus Torvalds /* And finish update writing critical data */ 9281da177e4SLinus Torvalds write_lock_bh(&mrt_lock); 9291da177e4SLinus Torvalds v->dev = dev; 9301da177e4SLinus Torvalds if (v->flags & VIFF_REGISTER) 9310c12295aSPatrick McHardy mrt->mroute_reg_vif_num = vifi; 9320c12295aSPatrick McHardy if (vifi+1 > mrt->maxvif) 9330c12295aSPatrick McHardy mrt->maxvif = vifi+1; 9341da177e4SLinus Torvalds write_unlock_bh(&mrt_lock); 935b362053aSYotam Gigi call_ipmr_vif_entry_notifiers(net, FIB_EVENT_VIF_ADD, v, vifi, mrt->id); 9361da177e4SLinus Torvalds return 0; 9371da177e4SLinus Torvalds } 9381da177e4SLinus Torvalds 939a8c9486bSEric Dumazet /* called with rcu_read_lock() */ 9400c12295aSPatrick McHardy static struct mfc_cache *ipmr_cache_find(struct mr_table *mrt, 9414feb88e5SBenjamin Thery __be32 origin, 9424feb88e5SBenjamin Thery __be32 mcastgrp) 9431da177e4SLinus Torvalds { 9448fb472c0SNikolay Aleksandrov struct mfc_cache_cmp_arg arg = { 9458fb472c0SNikolay Aleksandrov .mfc_mcastgrp = mcastgrp, 9468fb472c0SNikolay Aleksandrov .mfc_origin = origin 9478fb472c0SNikolay Aleksandrov }; 9481da177e4SLinus Torvalds 949845c9a7aSYuval Mintz return mr_mfc_find(mrt, &arg); 950660b26dcSNicolas Dichtel } 951660b26dcSNicolas Dichtel 952660b26dcSNicolas Dichtel /* Look for a (*,G) entry */ 953660b26dcSNicolas Dichtel static struct mfc_cache *ipmr_cache_find_any(struct mr_table *mrt, 954660b26dcSNicolas Dichtel __be32 mcastgrp, int vifi) 955660b26dcSNicolas Dichtel { 9568fb472c0SNikolay Aleksandrov struct mfc_cache_cmp_arg arg = { 9578fb472c0SNikolay Aleksandrov .mfc_mcastgrp = mcastgrp, 9588fb472c0SNikolay Aleksandrov .mfc_origin = htonl(INADDR_ANY) 9598fb472c0SNikolay Aleksandrov }; 960660b26dcSNicolas Dichtel 961360eb5daSNicolas Dichtel if (mcastgrp == htonl(INADDR_ANY)) 962845c9a7aSYuval Mintz return mr_mfc_find_any_parent(mrt, vifi); 963845c9a7aSYuval Mintz return mr_mfc_find_any(mrt, vifi, &arg); 964660b26dcSNicolas Dichtel } 965660b26dcSNicolas Dichtel 9668fb472c0SNikolay Aleksandrov /* Look for a (S,G,iif) entry if parent != -1 */ 9678fb472c0SNikolay Aleksandrov static struct mfc_cache *ipmr_cache_find_parent(struct mr_table *mrt, 9688fb472c0SNikolay Aleksandrov __be32 origin, __be32 mcastgrp, 9698fb472c0SNikolay Aleksandrov int parent) 9708fb472c0SNikolay Aleksandrov { 9718fb472c0SNikolay Aleksandrov struct mfc_cache_cmp_arg arg = { 9728fb472c0SNikolay Aleksandrov .mfc_mcastgrp = mcastgrp, 9738fb472c0SNikolay Aleksandrov .mfc_origin = origin, 9748fb472c0SNikolay Aleksandrov }; 9758fb472c0SNikolay Aleksandrov 976845c9a7aSYuval Mintz return mr_mfc_find_parent(mrt, &arg, parent); 9778fb472c0SNikolay Aleksandrov } 9788fb472c0SNikolay Aleksandrov 9797ef8f65dSNikolay Aleksandrov /* Allocate a multicast cache entry */ 980d658f8a0SPatrick McHardy static struct mfc_cache *ipmr_cache_alloc(void) 9811da177e4SLinus Torvalds { 982c3762229SRobert P. J. Day struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_KERNEL); 983a8c9486bSEric Dumazet 98470a0dec4STom Goff if (c) { 985494fff56SYuval Mintz c->_c.mfc_un.res.last_assert = jiffies - MFC_ASSERT_THRESH - 1; 986494fff56SYuval Mintz c->_c.mfc_un.res.minvif = MAXVIFS; 9878c13af2aSYuval Mintz c->_c.free = ipmr_cache_free_rcu; 988494fff56SYuval Mintz refcount_set(&c->_c.mfc_un.res.refcount, 1); 98970a0dec4STom Goff } 9901da177e4SLinus Torvalds return c; 9911da177e4SLinus Torvalds } 9921da177e4SLinus Torvalds 993d658f8a0SPatrick McHardy static struct mfc_cache *ipmr_cache_alloc_unres(void) 9941da177e4SLinus Torvalds { 995c3762229SRobert P. J. Day struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_ATOMIC); 996a8c9486bSEric Dumazet 997a8c9486bSEric Dumazet if (c) { 998494fff56SYuval Mintz skb_queue_head_init(&c->_c.mfc_un.unres.unresolved); 999494fff56SYuval Mintz c->_c.mfc_un.unres.expires = jiffies + 10 * HZ; 1000a8c9486bSEric Dumazet } 10011da177e4SLinus Torvalds return c; 10021da177e4SLinus Torvalds } 10031da177e4SLinus Torvalds 10047ef8f65dSNikolay Aleksandrov /* A cache entry has gone into a resolved state from queued */ 10050c12295aSPatrick McHardy static void ipmr_cache_resolve(struct net *net, struct mr_table *mrt, 10060c12295aSPatrick McHardy struct mfc_cache *uc, struct mfc_cache *c) 10071da177e4SLinus Torvalds { 10081da177e4SLinus Torvalds struct sk_buff *skb; 10099ef1d4c7SPatrick McHardy struct nlmsgerr *e; 10101da177e4SLinus Torvalds 1011a8cb16ddSEric Dumazet /* Play the pending entries through our router */ 1012494fff56SYuval Mintz while ((skb = __skb_dequeue(&uc->_c.mfc_un.unres.unresolved))) { 1013eddc9ec5SArnaldo Carvalho de Melo if (ip_hdr(skb)->version == 0) { 1014af72868bSJohannes Berg struct nlmsghdr *nlh = skb_pull(skb, 1015af72868bSJohannes Berg sizeof(struct iphdr)); 10161da177e4SLinus Torvalds 10177b0db857SYuval Mintz if (mr_fill_mroute(mrt, skb, &c->_c, 1018494fff56SYuval Mintz nlmsg_data(nlh)) > 0) { 1019a8cb16ddSEric Dumazet nlh->nlmsg_len = skb_tail_pointer(skb) - 1020a8cb16ddSEric Dumazet (u8 *)nlh; 10211da177e4SLinus Torvalds } else { 10221da177e4SLinus Torvalds nlh->nlmsg_type = NLMSG_ERROR; 1023573ce260SHong zhi guo nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr)); 10241da177e4SLinus Torvalds skb_trim(skb, nlh->nlmsg_len); 1025573ce260SHong zhi guo e = nlmsg_data(nlh); 10269ef1d4c7SPatrick McHardy e->error = -EMSGSIZE; 10279ef1d4c7SPatrick McHardy memset(&e->msg, 0, sizeof(e->msg)); 10281da177e4SLinus Torvalds } 10292942e900SThomas Graf 103015e47304SEric W. Biederman rtnl_unicast(skb, net, NETLINK_CB(skb).portid); 1031a8cb16ddSEric Dumazet } else { 10324b1f0d33SDonald Sharp ip_mr_forward(net, mrt, skb->dev, skb, c, 0); 10331da177e4SLinus Torvalds } 10341da177e4SLinus Torvalds } 1035a8cb16ddSEric Dumazet } 10361da177e4SLinus Torvalds 10375a645dd8SJulien Gomes /* Bounce a cache query up to mrouted and netlink. 10381da177e4SLinus Torvalds * 10391da177e4SLinus Torvalds * Called under mrt_lock. 10401da177e4SLinus Torvalds */ 10410c12295aSPatrick McHardy static int ipmr_cache_report(struct mr_table *mrt, 10424feb88e5SBenjamin Thery struct sk_buff *pkt, vifi_t vifi, int assert) 10431da177e4SLinus Torvalds { 1044c9bdd4b5SArnaldo Carvalho de Melo const int ihl = ip_hdrlen(pkt); 1045c316c629SNikolay Aleksandrov struct sock *mroute_sk; 10461da177e4SLinus Torvalds struct igmphdr *igmp; 10471da177e4SLinus Torvalds struct igmpmsg *msg; 1048c316c629SNikolay Aleksandrov struct sk_buff *skb; 10491da177e4SLinus Torvalds int ret; 10501da177e4SLinus Torvalds 1051c921c207SNikolay Aleksandrov if (assert == IGMPMSG_WHOLEPKT || assert == IGMPMSG_WRVIFWHOLE) 10521da177e4SLinus Torvalds skb = skb_realloc_headroom(pkt, sizeof(struct iphdr)); 10531da177e4SLinus Torvalds else 10541da177e4SLinus Torvalds skb = alloc_skb(128, GFP_ATOMIC); 10551da177e4SLinus Torvalds 10561da177e4SLinus Torvalds if (!skb) 10571da177e4SLinus Torvalds return -ENOBUFS; 10581da177e4SLinus Torvalds 1059c921c207SNikolay Aleksandrov if (assert == IGMPMSG_WHOLEPKT || assert == IGMPMSG_WRVIFWHOLE) { 10601da177e4SLinus Torvalds /* Ugly, but we have no choice with this interface. 1061a8cb16ddSEric Dumazet * Duplicate old header, fix ihl, length etc. 1062a8cb16ddSEric Dumazet * And all this only to mangle msg->im_msgtype and 1063a8cb16ddSEric Dumazet * to set msg->im_mbz to "mbz" :-) 10641da177e4SLinus Torvalds */ 1065878c8145SArnaldo Carvalho de Melo skb_push(skb, sizeof(struct iphdr)); 1066878c8145SArnaldo Carvalho de Melo skb_reset_network_header(skb); 1067badff6d0SArnaldo Carvalho de Melo skb_reset_transport_header(skb); 10680272ffc4SArnaldo Carvalho de Melo msg = (struct igmpmsg *)skb_network_header(skb); 1069d56f90a7SArnaldo Carvalho de Melo memcpy(msg, skb_network_header(pkt), sizeof(struct iphdr)); 1070c921c207SNikolay Aleksandrov msg->im_msgtype = assert; 10711da177e4SLinus Torvalds msg->im_mbz = 0; 1072c921c207SNikolay Aleksandrov if (assert == IGMPMSG_WRVIFWHOLE) 1073c921c207SNikolay Aleksandrov msg->im_vif = vifi; 1074c921c207SNikolay Aleksandrov else 10750c12295aSPatrick McHardy msg->im_vif = mrt->mroute_reg_vif_num; 1076eddc9ec5SArnaldo Carvalho de Melo ip_hdr(skb)->ihl = sizeof(struct iphdr) >> 2; 1077eddc9ec5SArnaldo Carvalho de Melo ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(pkt)->tot_len) + 1078eddc9ec5SArnaldo Carvalho de Melo sizeof(struct iphdr)); 1079c316c629SNikolay Aleksandrov } else { 1080a8cb16ddSEric Dumazet /* Copy the IP header */ 108130f3a40fSCong Wang skb_set_network_header(skb, skb->len); 1082ddc7b8e3SArnaldo Carvalho de Melo skb_put(skb, ihl); 108327d7ff46SArnaldo Carvalho de Melo skb_copy_to_linear_data(skb, pkt->data, ihl); 1084c316c629SNikolay Aleksandrov /* Flag to the kernel this is a route add */ 1085c316c629SNikolay Aleksandrov ip_hdr(skb)->protocol = 0; 1086eddc9ec5SArnaldo Carvalho de Melo msg = (struct igmpmsg *)skb_network_header(skb); 10871da177e4SLinus Torvalds msg->im_vif = vifi; 1088adf30907SEric Dumazet skb_dst_set(skb, dst_clone(skb_dst(pkt))); 1089a8cb16ddSEric Dumazet /* Add our header */ 10904df864c1SJohannes Berg igmp = skb_put(skb, sizeof(struct igmphdr)); 1091c316c629SNikolay Aleksandrov igmp->type = assert; 10921da177e4SLinus Torvalds msg->im_msgtype = assert; 10931da177e4SLinus Torvalds igmp->code = 0; 1094eddc9ec5SArnaldo Carvalho de Melo ip_hdr(skb)->tot_len = htons(skb->len); /* Fix the length */ 1095b0e380b1SArnaldo Carvalho de Melo skb->transport_header = skb->network_header; 10961da177e4SLinus Torvalds } 10971da177e4SLinus Torvalds 10984c968709SEric Dumazet rcu_read_lock(); 10994c968709SEric Dumazet mroute_sk = rcu_dereference(mrt->mroute_sk); 110051456b29SIan Morris if (!mroute_sk) { 11014c968709SEric Dumazet rcu_read_unlock(); 11021da177e4SLinus Torvalds kfree_skb(skb); 11031da177e4SLinus Torvalds return -EINVAL; 11041da177e4SLinus Torvalds } 11051da177e4SLinus Torvalds 11065a645dd8SJulien Gomes igmpmsg_netlink_event(mrt, skb); 11075a645dd8SJulien Gomes 1108a8cb16ddSEric Dumazet /* Deliver to mrouted */ 11094c968709SEric Dumazet ret = sock_queue_rcv_skb(mroute_sk, skb); 11104c968709SEric Dumazet rcu_read_unlock(); 111170a269e6SBenjamin Thery if (ret < 0) { 1112e87cc472SJoe Perches net_warn_ratelimited("mroute: pending queue full, dropping entries\n"); 11131da177e4SLinus Torvalds kfree_skb(skb); 11141da177e4SLinus Torvalds } 11151da177e4SLinus Torvalds 11161da177e4SLinus Torvalds return ret; 11171da177e4SLinus Torvalds } 11181da177e4SLinus Torvalds 11197ef8f65dSNikolay Aleksandrov /* Queue a packet for resolution. It gets locked cache entry! */ 11207ef8f65dSNikolay Aleksandrov static int ipmr_cache_unresolved(struct mr_table *mrt, vifi_t vifi, 11214b1f0d33SDonald Sharp struct sk_buff *skb, struct net_device *dev) 11221da177e4SLinus Torvalds { 11238fb472c0SNikolay Aleksandrov const struct iphdr *iph = ip_hdr(skb); 11248fb472c0SNikolay Aleksandrov struct mfc_cache *c; 1125862465f2SPatrick McHardy bool found = false; 11261da177e4SLinus Torvalds int err; 11271da177e4SLinus Torvalds 11281da177e4SLinus Torvalds spin_lock_bh(&mfc_unres_lock); 1129494fff56SYuval Mintz list_for_each_entry(c, &mrt->mfc_unres_queue, _c.list) { 1130e258beb2SPatrick McHardy if (c->mfc_mcastgrp == iph->daddr && 1131862465f2SPatrick McHardy c->mfc_origin == iph->saddr) { 1132862465f2SPatrick McHardy found = true; 11331da177e4SLinus Torvalds break; 11341da177e4SLinus Torvalds } 1135862465f2SPatrick McHardy } 11361da177e4SLinus Torvalds 1137862465f2SPatrick McHardy if (!found) { 1138a8cb16ddSEric Dumazet /* Create a new entry if allowable */ 11390079ad8eSHangbin Liu c = ipmr_cache_alloc_unres(); 11400079ad8eSHangbin Liu if (!c) { 11411da177e4SLinus Torvalds spin_unlock_bh(&mfc_unres_lock); 11421da177e4SLinus Torvalds 11431da177e4SLinus Torvalds kfree_skb(skb); 11441da177e4SLinus Torvalds return -ENOBUFS; 11451da177e4SLinus Torvalds } 11461da177e4SLinus Torvalds 1147a8cb16ddSEric Dumazet /* Fill in the new cache entry */ 1148494fff56SYuval Mintz c->_c.mfc_parent = -1; 1149eddc9ec5SArnaldo Carvalho de Melo c->mfc_origin = iph->saddr; 1150eddc9ec5SArnaldo Carvalho de Melo c->mfc_mcastgrp = iph->daddr; 11511da177e4SLinus Torvalds 1152a8cb16ddSEric Dumazet /* Reflect first query at mrouted. */ 11530c12295aSPatrick McHardy err = ipmr_cache_report(mrt, skb, vifi, IGMPMSG_NOCACHE); 1154494fff56SYuval Mintz 11554feb88e5SBenjamin Thery if (err < 0) { 11561da177e4SLinus Torvalds /* If the report failed throw the cache entry 11571da177e4SLinus Torvalds out - Brad Parker 11581da177e4SLinus Torvalds */ 11591da177e4SLinus Torvalds spin_unlock_bh(&mfc_unres_lock); 11601da177e4SLinus Torvalds 11615c0a66f5SBenjamin Thery ipmr_cache_free(c); 11621da177e4SLinus Torvalds kfree_skb(skb); 11631da177e4SLinus Torvalds return err; 11641da177e4SLinus Torvalds } 11651da177e4SLinus Torvalds 11660c12295aSPatrick McHardy atomic_inc(&mrt->cache_resolve_queue_len); 1167494fff56SYuval Mintz list_add(&c->_c.list, &mrt->mfc_unres_queue); 11688cd3ac9fSNicolas Dichtel mroute_netlink_event(mrt, c, RTM_NEWROUTE); 11691da177e4SLinus Torvalds 1170278554bdSDavid S. Miller if (atomic_read(&mrt->cache_resolve_queue_len) == 1) 1171494fff56SYuval Mintz mod_timer(&mrt->ipmr_expire_timer, 1172494fff56SYuval Mintz c->_c.mfc_un.unres.expires); 11731da177e4SLinus Torvalds } 11741da177e4SLinus Torvalds 1175a8cb16ddSEric Dumazet /* See if we can append the packet */ 1176494fff56SYuval Mintz if (c->_c.mfc_un.unres.unresolved.qlen > 3) { 11771da177e4SLinus Torvalds kfree_skb(skb); 11781da177e4SLinus Torvalds err = -ENOBUFS; 11791da177e4SLinus Torvalds } else { 11804b1f0d33SDonald Sharp if (dev) { 11814b1f0d33SDonald Sharp skb->dev = dev; 11824b1f0d33SDonald Sharp skb->skb_iif = dev->ifindex; 11834b1f0d33SDonald Sharp } 1184494fff56SYuval Mintz skb_queue_tail(&c->_c.mfc_un.unres.unresolved, skb); 11851da177e4SLinus Torvalds err = 0; 11861da177e4SLinus Torvalds } 11871da177e4SLinus Torvalds 11881da177e4SLinus Torvalds spin_unlock_bh(&mfc_unres_lock); 11891da177e4SLinus Torvalds return err; 11901da177e4SLinus Torvalds } 11911da177e4SLinus Torvalds 11927ef8f65dSNikolay Aleksandrov /* MFC cache manipulation by user space mroute daemon */ 11931da177e4SLinus Torvalds 1194660b26dcSNicolas Dichtel static int ipmr_mfc_delete(struct mr_table *mrt, struct mfcctl *mfc, int parent) 11951da177e4SLinus Torvalds { 1196b362053aSYotam Gigi struct net *net = read_pnet(&mrt->net); 11978fb472c0SNikolay Aleksandrov struct mfc_cache *c; 11981da177e4SLinus Torvalds 11998fb472c0SNikolay Aleksandrov /* The entries are added/deleted only under RTNL */ 12008fb472c0SNikolay Aleksandrov rcu_read_lock(); 12018fb472c0SNikolay Aleksandrov c = ipmr_cache_find_parent(mrt, mfc->mfcc_origin.s_addr, 12028fb472c0SNikolay Aleksandrov mfc->mfcc_mcastgrp.s_addr, parent); 12038fb472c0SNikolay Aleksandrov rcu_read_unlock(); 12048fb472c0SNikolay Aleksandrov if (!c) 12058fb472c0SNikolay Aleksandrov return -ENOENT; 1206494fff56SYuval Mintz rhltable_remove(&mrt->mfc_hash, &c->_c.mnode, ipmr_rht_params); 1207494fff56SYuval Mintz list_del_rcu(&c->_c.list); 1208b362053aSYotam Gigi call_ipmr_mfc_entry_notifiers(net, FIB_EVENT_ENTRY_DEL, c, mrt->id); 12098cd3ac9fSNicolas Dichtel mroute_netlink_event(mrt, c, RTM_DELROUTE); 12108c13af2aSYuval Mintz mr_cache_put(&c->_c); 12118fb472c0SNikolay Aleksandrov 12121da177e4SLinus Torvalds return 0; 12131da177e4SLinus Torvalds } 12141da177e4SLinus Torvalds 12150c12295aSPatrick McHardy static int ipmr_mfc_add(struct net *net, struct mr_table *mrt, 1216660b26dcSNicolas Dichtel struct mfcctl *mfc, int mrtsock, int parent) 12171da177e4SLinus Torvalds { 1218862465f2SPatrick McHardy struct mfc_cache *uc, *c; 1219494fff56SYuval Mintz struct mr_mfc *_uc; 12208fb472c0SNikolay Aleksandrov bool found; 12218fb472c0SNikolay Aleksandrov int ret; 12221da177e4SLinus Torvalds 1223a50436f2SPatrick McHardy if (mfc->mfcc_parent >= MAXVIFS) 1224a50436f2SPatrick McHardy return -ENFILE; 1225a50436f2SPatrick McHardy 12268fb472c0SNikolay Aleksandrov /* The entries are added/deleted only under RTNL */ 12278fb472c0SNikolay Aleksandrov rcu_read_lock(); 12288fb472c0SNikolay Aleksandrov c = ipmr_cache_find_parent(mrt, mfc->mfcc_origin.s_addr, 12298fb472c0SNikolay Aleksandrov mfc->mfcc_mcastgrp.s_addr, parent); 12308fb472c0SNikolay Aleksandrov rcu_read_unlock(); 12318fb472c0SNikolay Aleksandrov if (c) { 12321da177e4SLinus Torvalds write_lock_bh(&mrt_lock); 1233494fff56SYuval Mintz c->_c.mfc_parent = mfc->mfcc_parent; 1234494fff56SYuval Mintz ipmr_update_thresholds(mrt, &c->_c, mfc->mfcc_ttls); 12351da177e4SLinus Torvalds if (!mrtsock) 1236494fff56SYuval Mintz c->_c.mfc_flags |= MFC_STATIC; 12371da177e4SLinus Torvalds write_unlock_bh(&mrt_lock); 1238b362053aSYotam Gigi call_ipmr_mfc_entry_notifiers(net, FIB_EVENT_ENTRY_REPLACE, c, 1239b362053aSYotam Gigi mrt->id); 12408cd3ac9fSNicolas Dichtel mroute_netlink_event(mrt, c, RTM_NEWROUTE); 12411da177e4SLinus Torvalds return 0; 12421da177e4SLinus Torvalds } 12431da177e4SLinus Torvalds 1244360eb5daSNicolas Dichtel if (mfc->mfcc_mcastgrp.s_addr != htonl(INADDR_ANY) && 1245660b26dcSNicolas Dichtel !ipv4_is_multicast(mfc->mfcc_mcastgrp.s_addr)) 12461da177e4SLinus Torvalds return -EINVAL; 12471da177e4SLinus Torvalds 1248d658f8a0SPatrick McHardy c = ipmr_cache_alloc(); 124951456b29SIan Morris if (!c) 12501da177e4SLinus Torvalds return -ENOMEM; 12511da177e4SLinus Torvalds 12521da177e4SLinus Torvalds c->mfc_origin = mfc->mfcc_origin.s_addr; 12531da177e4SLinus Torvalds c->mfc_mcastgrp = mfc->mfcc_mcastgrp.s_addr; 1254494fff56SYuval Mintz c->_c.mfc_parent = mfc->mfcc_parent; 1255494fff56SYuval Mintz ipmr_update_thresholds(mrt, &c->_c, mfc->mfcc_ttls); 12561da177e4SLinus Torvalds if (!mrtsock) 1257494fff56SYuval Mintz c->_c.mfc_flags |= MFC_STATIC; 12581da177e4SLinus Torvalds 1259494fff56SYuval Mintz ret = rhltable_insert_key(&mrt->mfc_hash, &c->cmparg, &c->_c.mnode, 12608fb472c0SNikolay Aleksandrov ipmr_rht_params); 12618fb472c0SNikolay Aleksandrov if (ret) { 12628fb472c0SNikolay Aleksandrov pr_err("ipmr: rhtable insert error %d\n", ret); 12638fb472c0SNikolay Aleksandrov ipmr_cache_free(c); 12648fb472c0SNikolay Aleksandrov return ret; 12658fb472c0SNikolay Aleksandrov } 1266494fff56SYuval Mintz list_add_tail_rcu(&c->_c.list, &mrt->mfc_cache_list); 12677ef8f65dSNikolay Aleksandrov /* Check to see if we resolved a queued list. If so we 12681da177e4SLinus Torvalds * need to send on the frames and tidy up. 12691da177e4SLinus Torvalds */ 1270b0ebb739SPatrick McHardy found = false; 12711da177e4SLinus Torvalds spin_lock_bh(&mfc_unres_lock); 1272494fff56SYuval Mintz list_for_each_entry(_uc, &mrt->mfc_unres_queue, list) { 1273494fff56SYuval Mintz uc = (struct mfc_cache *)_uc; 1274e258beb2SPatrick McHardy if (uc->mfc_origin == c->mfc_origin && 12751da177e4SLinus Torvalds uc->mfc_mcastgrp == c->mfc_mcastgrp) { 1276494fff56SYuval Mintz list_del(&_uc->list); 12770c12295aSPatrick McHardy atomic_dec(&mrt->cache_resolve_queue_len); 1278b0ebb739SPatrick McHardy found = true; 12791da177e4SLinus Torvalds break; 12801da177e4SLinus Torvalds } 12811da177e4SLinus Torvalds } 12820c12295aSPatrick McHardy if (list_empty(&mrt->mfc_unres_queue)) 12830c12295aSPatrick McHardy del_timer(&mrt->ipmr_expire_timer); 12841da177e4SLinus Torvalds spin_unlock_bh(&mfc_unres_lock); 12851da177e4SLinus Torvalds 1286b0ebb739SPatrick McHardy if (found) { 12870c12295aSPatrick McHardy ipmr_cache_resolve(net, mrt, uc, c); 12885c0a66f5SBenjamin Thery ipmr_cache_free(uc); 12891da177e4SLinus Torvalds } 1290b362053aSYotam Gigi call_ipmr_mfc_entry_notifiers(net, FIB_EVENT_ENTRY_ADD, c, mrt->id); 12918cd3ac9fSNicolas Dichtel mroute_netlink_event(mrt, c, RTM_NEWROUTE); 12921da177e4SLinus Torvalds return 0; 12931da177e4SLinus Torvalds } 12941da177e4SLinus Torvalds 12957ef8f65dSNikolay Aleksandrov /* Close the multicast socket, and clear the vif tables etc */ 1296ca8d4794SCallum Sinclair static void mroute_clean_tables(struct mr_table *mrt, int flags) 12971da177e4SLinus Torvalds { 1298b362053aSYotam Gigi struct net *net = read_pnet(&mrt->net); 1299494fff56SYuval Mintz struct mr_mfc *c, *tmp; 1300494fff56SYuval Mintz struct mfc_cache *cache; 1301d17fa6faSEric Dumazet LIST_HEAD(list); 13028fb472c0SNikolay Aleksandrov int i; 13031da177e4SLinus Torvalds 1304a8cb16ddSEric Dumazet /* Shut down all active vif entries */ 1305ca8d4794SCallum Sinclair if (flags & (MRT_FLUSH_VIFS | MRT_FLUSH_VIFS_STATIC)) { 13060c12295aSPatrick McHardy for (i = 0; i < mrt->maxvif; i++) { 1307ca8d4794SCallum Sinclair if (((mrt->vif_table[i].flags & VIFF_STATIC) && 1308ca8d4794SCallum Sinclair !(flags & MRT_FLUSH_VIFS_STATIC)) || 1309ca8d4794SCallum Sinclair (!(mrt->vif_table[i].flags & VIFF_STATIC) && !(flags & MRT_FLUSH_VIFS))) 13100e615e96SNikolay Aleksandrov continue; 13110c12295aSPatrick McHardy vif_delete(mrt, i, 0, &list); 13121da177e4SLinus Torvalds } 1313d17fa6faSEric Dumazet unregister_netdevice_many(&list); 1314ca8d4794SCallum Sinclair } 13151da177e4SLinus Torvalds 1316a8cb16ddSEric Dumazet /* Wipe the cache */ 1317ca8d4794SCallum Sinclair if (flags & (MRT_FLUSH_MFC | MRT_FLUSH_MFC_STATIC)) { 13188fb472c0SNikolay Aleksandrov list_for_each_entry_safe(c, tmp, &mrt->mfc_cache_list, list) { 1319ca8d4794SCallum Sinclair if (((c->mfc_flags & MFC_STATIC) && !(flags & MRT_FLUSH_MFC_STATIC)) || 1320ca8d4794SCallum Sinclair (!(c->mfc_flags & MFC_STATIC) && !(flags & MRT_FLUSH_MFC))) 13211da177e4SLinus Torvalds continue; 13228fb472c0SNikolay Aleksandrov rhltable_remove(&mrt->mfc_hash, &c->mnode, ipmr_rht_params); 1323a8c9486bSEric Dumazet list_del_rcu(&c->list); 1324494fff56SYuval Mintz cache = (struct mfc_cache *)c; 1325494fff56SYuval Mintz call_ipmr_mfc_entry_notifiers(net, FIB_EVENT_ENTRY_DEL, cache, 1326b362053aSYotam Gigi mrt->id); 1327494fff56SYuval Mintz mroute_netlink_event(mrt, cache, RTM_DELROUTE); 13288c13af2aSYuval Mintz mr_cache_put(c); 13291da177e4SLinus Torvalds } 1330ca8d4794SCallum Sinclair } 13311da177e4SLinus Torvalds 1332ca8d4794SCallum Sinclair if (flags & MRT_FLUSH_MFC) { 13330c12295aSPatrick McHardy if (atomic_read(&mrt->cache_resolve_queue_len) != 0) { 13341da177e4SLinus Torvalds spin_lock_bh(&mfc_unres_lock); 13358fb472c0SNikolay Aleksandrov list_for_each_entry_safe(c, tmp, &mrt->mfc_unres_queue, list) { 1336862465f2SPatrick McHardy list_del(&c->list); 1337494fff56SYuval Mintz cache = (struct mfc_cache *)c; 1338494fff56SYuval Mintz mroute_netlink_event(mrt, cache, RTM_DELROUTE); 1339494fff56SYuval Mintz ipmr_destroy_unres(mrt, cache); 13401da177e4SLinus Torvalds } 13411da177e4SLinus Torvalds spin_unlock_bh(&mfc_unres_lock); 13421da177e4SLinus Torvalds } 13431da177e4SLinus Torvalds } 1344ca8d4794SCallum Sinclair } 13451da177e4SLinus Torvalds 13464c968709SEric Dumazet /* called from ip_ra_control(), before an RCU grace period, 13474c968709SEric Dumazet * we dont need to call synchronize_rcu() here 13484c968709SEric Dumazet */ 13491da177e4SLinus Torvalds static void mrtsock_destruct(struct sock *sk) 13501da177e4SLinus Torvalds { 13514feb88e5SBenjamin Thery struct net *net = sock_net(sk); 1352f0ad0860SPatrick McHardy struct mr_table *mrt; 13534feb88e5SBenjamin Thery 1354128aaa98SKirill Tkhai rtnl_lock(); 1355f0ad0860SPatrick McHardy ipmr_for_each_table(mrt, net) { 13564c968709SEric Dumazet if (sk == rtnl_dereference(mrt->mroute_sk)) { 13574feb88e5SBenjamin Thery IPV4_DEVCONF_ALL(net, MC_FORWARDING)--; 13583b022865SDavid Ahern inet_netconf_notify_devconf(net, RTM_NEWNETCONF, 13593b022865SDavid Ahern NETCONFA_MC_FORWARDING, 1360d67b8c61SNicolas Dichtel NETCONFA_IFINDEX_ALL, 1361d67b8c61SNicolas Dichtel net->ipv4.devconf_all); 1362a9b3cd7fSStephen Hemminger RCU_INIT_POINTER(mrt->mroute_sk, NULL); 1363ca8d4794SCallum Sinclair mroute_clean_tables(mrt, MRT_FLUSH_VIFS | MRT_FLUSH_MFC); 13641da177e4SLinus Torvalds } 13651da177e4SLinus Torvalds } 1366128aaa98SKirill Tkhai rtnl_unlock(); 13671da177e4SLinus Torvalds } 13681da177e4SLinus Torvalds 13697ef8f65dSNikolay Aleksandrov /* Socket options and virtual interface manipulation. The whole 13701da177e4SLinus Torvalds * virtual interface system is a complete heap, but unfortunately 13711da177e4SLinus Torvalds * that's how BSD mrouted happens to think. Maybe one day with a proper 13721da177e4SLinus Torvalds * MOSPF/PIM router set up we can clean this up. 13731da177e4SLinus Torvalds */ 13741da177e4SLinus Torvalds 137529e97d21SNikolay Aleksandrov int ip_mroute_setsockopt(struct sock *sk, int optname, char __user *optval, 137629e97d21SNikolay Aleksandrov unsigned int optlen) 13771da177e4SLinus Torvalds { 137829e97d21SNikolay Aleksandrov struct net *net = sock_net(sk); 137929e97d21SNikolay Aleksandrov int val, ret = 0, parent = 0; 138029e97d21SNikolay Aleksandrov struct mr_table *mrt; 13811da177e4SLinus Torvalds struct vifctl vif; 13821da177e4SLinus Torvalds struct mfcctl mfc; 1383c921c207SNikolay Aleksandrov bool do_wrvifwhole; 138429e97d21SNikolay Aleksandrov u32 uval; 1385f0ad0860SPatrick McHardy 138629e97d21SNikolay Aleksandrov /* There's one exception to the lock - MRT_DONE which needs to unlock */ 138729e97d21SNikolay Aleksandrov rtnl_lock(); 13885e1859fbSEric Dumazet if (sk->sk_type != SOCK_RAW || 138929e97d21SNikolay Aleksandrov inet_sk(sk)->inet_num != IPPROTO_IGMP) { 139029e97d21SNikolay Aleksandrov ret = -EOPNOTSUPP; 139129e97d21SNikolay Aleksandrov goto out_unlock; 139229e97d21SNikolay Aleksandrov } 13935e1859fbSEric Dumazet 1394f0ad0860SPatrick McHardy mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT); 139529e97d21SNikolay Aleksandrov if (!mrt) { 139629e97d21SNikolay Aleksandrov ret = -ENOENT; 139729e97d21SNikolay Aleksandrov goto out_unlock; 139829e97d21SNikolay Aleksandrov } 1399132adf54SStephen Hemminger if (optname != MRT_INIT) { 140033d480ceSEric Dumazet if (sk != rcu_access_pointer(mrt->mroute_sk) && 140129e97d21SNikolay Aleksandrov !ns_capable(net->user_ns, CAP_NET_ADMIN)) { 140229e97d21SNikolay Aleksandrov ret = -EACCES; 140329e97d21SNikolay Aleksandrov goto out_unlock; 140429e97d21SNikolay Aleksandrov } 14051da177e4SLinus Torvalds } 14061da177e4SLinus Torvalds 1407132adf54SStephen Hemminger switch (optname) { 14081da177e4SLinus Torvalds case MRT_INIT: 140942e6b89cSNikolay Aleksandrov if (optlen != sizeof(int)) { 141029e97d21SNikolay Aleksandrov ret = -EINVAL; 141129e97d21SNikolay Aleksandrov break; 141242e6b89cSNikolay Aleksandrov } 141342e6b89cSNikolay Aleksandrov if (rtnl_dereference(mrt->mroute_sk)) { 141442e6b89cSNikolay Aleksandrov ret = -EADDRINUSE; 141542e6b89cSNikolay Aleksandrov break; 141642e6b89cSNikolay Aleksandrov } 14171da177e4SLinus Torvalds 14181da177e4SLinus Torvalds ret = ip_ra_control(sk, 1, mrtsock_destruct); 14191da177e4SLinus Torvalds if (ret == 0) { 1420cf778b00SEric Dumazet rcu_assign_pointer(mrt->mroute_sk, sk); 14214feb88e5SBenjamin Thery IPV4_DEVCONF_ALL(net, MC_FORWARDING)++; 14223b022865SDavid Ahern inet_netconf_notify_devconf(net, RTM_NEWNETCONF, 14233b022865SDavid Ahern NETCONFA_MC_FORWARDING, 1424d67b8c61SNicolas Dichtel NETCONFA_IFINDEX_ALL, 1425d67b8c61SNicolas Dichtel net->ipv4.devconf_all); 14261da177e4SLinus Torvalds } 142729e97d21SNikolay Aleksandrov break; 14281da177e4SLinus Torvalds case MRT_DONE: 142929e97d21SNikolay Aleksandrov if (sk != rcu_access_pointer(mrt->mroute_sk)) { 143029e97d21SNikolay Aleksandrov ret = -EACCES; 143129e97d21SNikolay Aleksandrov } else { 1432128aaa98SKirill Tkhai /* We need to unlock here because mrtsock_destruct takes 1433128aaa98SKirill Tkhai * care of rtnl itself and we can't change that due to 1434128aaa98SKirill Tkhai * the IP_ROUTER_ALERT setsockopt which runs without it. 1435128aaa98SKirill Tkhai */ 1436128aaa98SKirill Tkhai rtnl_unlock(); 143729e97d21SNikolay Aleksandrov ret = ip_ra_control(sk, 0, NULL); 1438128aaa98SKirill Tkhai goto out; 143929e97d21SNikolay Aleksandrov } 144029e97d21SNikolay Aleksandrov break; 14411da177e4SLinus Torvalds case MRT_ADD_VIF: 14421da177e4SLinus Torvalds case MRT_DEL_VIF: 144329e97d21SNikolay Aleksandrov if (optlen != sizeof(vif)) { 144429e97d21SNikolay Aleksandrov ret = -EINVAL; 144529e97d21SNikolay Aleksandrov break; 144629e97d21SNikolay Aleksandrov } 144729e97d21SNikolay Aleksandrov if (copy_from_user(&vif, optval, sizeof(vif))) { 144829e97d21SNikolay Aleksandrov ret = -EFAULT; 144929e97d21SNikolay Aleksandrov break; 145029e97d21SNikolay Aleksandrov } 145129e97d21SNikolay Aleksandrov if (vif.vifc_vifi >= MAXVIFS) { 145229e97d21SNikolay Aleksandrov ret = -ENFILE; 145329e97d21SNikolay Aleksandrov break; 145429e97d21SNikolay Aleksandrov } 14551da177e4SLinus Torvalds if (optname == MRT_ADD_VIF) { 14564c968709SEric Dumazet ret = vif_add(net, mrt, &vif, 14574c968709SEric Dumazet sk == rtnl_dereference(mrt->mroute_sk)); 14581da177e4SLinus Torvalds } else { 14590c12295aSPatrick McHardy ret = vif_delete(mrt, vif.vifc_vifi, 0, NULL); 14601da177e4SLinus Torvalds } 146129e97d21SNikolay Aleksandrov break; 14627ef8f65dSNikolay Aleksandrov /* Manipulate the forwarding caches. These live 14631da177e4SLinus Torvalds * in a sort of kernel/user symbiosis. 14641da177e4SLinus Torvalds */ 14651da177e4SLinus Torvalds case MRT_ADD_MFC: 14661da177e4SLinus Torvalds case MRT_DEL_MFC: 1467660b26dcSNicolas Dichtel parent = -1; 1468*a8eceea8SJoe Perches fallthrough; 1469660b26dcSNicolas Dichtel case MRT_ADD_MFC_PROXY: 1470660b26dcSNicolas Dichtel case MRT_DEL_MFC_PROXY: 147129e97d21SNikolay Aleksandrov if (optlen != sizeof(mfc)) { 147229e97d21SNikolay Aleksandrov ret = -EINVAL; 147329e97d21SNikolay Aleksandrov break; 147429e97d21SNikolay Aleksandrov } 147529e97d21SNikolay Aleksandrov if (copy_from_user(&mfc, optval, sizeof(mfc))) { 147629e97d21SNikolay Aleksandrov ret = -EFAULT; 147729e97d21SNikolay Aleksandrov break; 147829e97d21SNikolay Aleksandrov } 1479660b26dcSNicolas Dichtel if (parent == 0) 1480660b26dcSNicolas Dichtel parent = mfc.mfcc_parent; 1481660b26dcSNicolas Dichtel if (optname == MRT_DEL_MFC || optname == MRT_DEL_MFC_PROXY) 1482660b26dcSNicolas Dichtel ret = ipmr_mfc_delete(mrt, &mfc, parent); 14831da177e4SLinus Torvalds else 14844c968709SEric Dumazet ret = ipmr_mfc_add(net, mrt, &mfc, 1485660b26dcSNicolas Dichtel sk == rtnl_dereference(mrt->mroute_sk), 1486660b26dcSNicolas Dichtel parent); 148729e97d21SNikolay Aleksandrov break; 1488ca8d4794SCallum Sinclair case MRT_FLUSH: 1489ca8d4794SCallum Sinclair if (optlen != sizeof(val)) { 1490ca8d4794SCallum Sinclair ret = -EINVAL; 1491ca8d4794SCallum Sinclair break; 1492ca8d4794SCallum Sinclair } 1493ca8d4794SCallum Sinclair if (get_user(val, (int __user *)optval)) { 1494ca8d4794SCallum Sinclair ret = -EFAULT; 1495ca8d4794SCallum Sinclair break; 1496ca8d4794SCallum Sinclair } 1497ca8d4794SCallum Sinclair mroute_clean_tables(mrt, val); 1498ca8d4794SCallum Sinclair break; 14997ef8f65dSNikolay Aleksandrov /* Control PIM assert. */ 15001da177e4SLinus Torvalds case MRT_ASSERT: 150129e97d21SNikolay Aleksandrov if (optlen != sizeof(val)) { 150229e97d21SNikolay Aleksandrov ret = -EINVAL; 150329e97d21SNikolay Aleksandrov break; 15041da177e4SLinus Torvalds } 150529e97d21SNikolay Aleksandrov if (get_user(val, (int __user *)optval)) { 150629e97d21SNikolay Aleksandrov ret = -EFAULT; 150729e97d21SNikolay Aleksandrov break; 150829e97d21SNikolay Aleksandrov } 150929e97d21SNikolay Aleksandrov mrt->mroute_do_assert = val; 151029e97d21SNikolay Aleksandrov break; 15111da177e4SLinus Torvalds case MRT_PIM: 15121973a4eaSNikolay Aleksandrov if (!ipmr_pimsm_enabled()) { 151329e97d21SNikolay Aleksandrov ret = -ENOPROTOOPT; 151429e97d21SNikolay Aleksandrov break; 15151da177e4SLinus Torvalds } 151629e97d21SNikolay Aleksandrov if (optlen != sizeof(val)) { 151729e97d21SNikolay Aleksandrov ret = -EINVAL; 151829e97d21SNikolay Aleksandrov break; 15191da177e4SLinus Torvalds } 152029e97d21SNikolay Aleksandrov if (get_user(val, (int __user *)optval)) { 152129e97d21SNikolay Aleksandrov ret = -EFAULT; 152229e97d21SNikolay Aleksandrov break; 152329e97d21SNikolay Aleksandrov } 152429e97d21SNikolay Aleksandrov 1525c921c207SNikolay Aleksandrov do_wrvifwhole = (val == IGMPMSG_WRVIFWHOLE); 152629e97d21SNikolay Aleksandrov val = !!val; 152729e97d21SNikolay Aleksandrov if (val != mrt->mroute_do_pim) { 152829e97d21SNikolay Aleksandrov mrt->mroute_do_pim = val; 152929e97d21SNikolay Aleksandrov mrt->mroute_do_assert = val; 1530c921c207SNikolay Aleksandrov mrt->mroute_do_wrvifwhole = do_wrvifwhole; 153129e97d21SNikolay Aleksandrov } 153229e97d21SNikolay Aleksandrov break; 1533f0ad0860SPatrick McHardy case MRT_TABLE: 153429e97d21SNikolay Aleksandrov if (!IS_BUILTIN(CONFIG_IP_MROUTE_MULTIPLE_TABLES)) { 153529e97d21SNikolay Aleksandrov ret = -ENOPROTOOPT; 153629e97d21SNikolay Aleksandrov break; 153729e97d21SNikolay Aleksandrov } 153829e97d21SNikolay Aleksandrov if (optlen != sizeof(uval)) { 153929e97d21SNikolay Aleksandrov ret = -EINVAL; 154029e97d21SNikolay Aleksandrov break; 154129e97d21SNikolay Aleksandrov } 154229e97d21SNikolay Aleksandrov if (get_user(uval, (u32 __user *)optval)) { 154329e97d21SNikolay Aleksandrov ret = -EFAULT; 154429e97d21SNikolay Aleksandrov break; 154529e97d21SNikolay Aleksandrov } 1546f0ad0860SPatrick McHardy 15474c968709SEric Dumazet if (sk == rtnl_dereference(mrt->mroute_sk)) { 15484c968709SEric Dumazet ret = -EBUSY; 15494c968709SEric Dumazet } else { 155029e97d21SNikolay Aleksandrov mrt = ipmr_new_table(net, uval); 15511113ebbcSNikolay Aleksandrov if (IS_ERR(mrt)) 15521113ebbcSNikolay Aleksandrov ret = PTR_ERR(mrt); 15535e1859fbSEric Dumazet else 155429e97d21SNikolay Aleksandrov raw_sk(sk)->ipmr_table = uval; 15554c968709SEric Dumazet } 155629e97d21SNikolay Aleksandrov break; 15577ef8f65dSNikolay Aleksandrov /* Spurious command, or MRT_VERSION which you cannot set. */ 15581da177e4SLinus Torvalds default: 155929e97d21SNikolay Aleksandrov ret = -ENOPROTOOPT; 15601da177e4SLinus Torvalds } 156129e97d21SNikolay Aleksandrov out_unlock: 156229e97d21SNikolay Aleksandrov rtnl_unlock(); 1563128aaa98SKirill Tkhai out: 156429e97d21SNikolay Aleksandrov return ret; 15651da177e4SLinus Torvalds } 15661da177e4SLinus Torvalds 15677ef8f65dSNikolay Aleksandrov /* Getsock opt support for the multicast routing system. */ 15681da177e4SLinus Torvalds int ip_mroute_getsockopt(struct sock *sk, int optname, char __user *optval, int __user *optlen) 15691da177e4SLinus Torvalds { 15701da177e4SLinus Torvalds int olr; 15711da177e4SLinus Torvalds int val; 15724feb88e5SBenjamin Thery struct net *net = sock_net(sk); 1573f0ad0860SPatrick McHardy struct mr_table *mrt; 1574f0ad0860SPatrick McHardy 15755e1859fbSEric Dumazet if (sk->sk_type != SOCK_RAW || 15765e1859fbSEric Dumazet inet_sk(sk)->inet_num != IPPROTO_IGMP) 15775e1859fbSEric Dumazet return -EOPNOTSUPP; 15785e1859fbSEric Dumazet 1579f0ad0860SPatrick McHardy mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT); 158051456b29SIan Morris if (!mrt) 1581f0ad0860SPatrick McHardy return -ENOENT; 15821da177e4SLinus Torvalds 1583fe9ef3ceSNikolay Aleksandrov switch (optname) { 1584fe9ef3ceSNikolay Aleksandrov case MRT_VERSION: 15851da177e4SLinus Torvalds val = 0x0305; 1586fe9ef3ceSNikolay Aleksandrov break; 1587fe9ef3ceSNikolay Aleksandrov case MRT_PIM: 15881973a4eaSNikolay Aleksandrov if (!ipmr_pimsm_enabled()) 1589c316c629SNikolay Aleksandrov return -ENOPROTOOPT; 15900c12295aSPatrick McHardy val = mrt->mroute_do_pim; 1591fe9ef3ceSNikolay Aleksandrov break; 1592fe9ef3ceSNikolay Aleksandrov case MRT_ASSERT: 15930c12295aSPatrick McHardy val = mrt->mroute_do_assert; 1594fe9ef3ceSNikolay Aleksandrov break; 1595fe9ef3ceSNikolay Aleksandrov default: 1596fe9ef3ceSNikolay Aleksandrov return -ENOPROTOOPT; 1597c316c629SNikolay Aleksandrov } 1598fe9ef3ceSNikolay Aleksandrov 1599fe9ef3ceSNikolay Aleksandrov if (get_user(olr, optlen)) 1600fe9ef3ceSNikolay Aleksandrov return -EFAULT; 1601fe9ef3ceSNikolay Aleksandrov olr = min_t(unsigned int, olr, sizeof(int)); 1602fe9ef3ceSNikolay Aleksandrov if (olr < 0) 1603fe9ef3ceSNikolay Aleksandrov return -EINVAL; 1604fe9ef3ceSNikolay Aleksandrov if (put_user(olr, optlen)) 1605fe9ef3ceSNikolay Aleksandrov return -EFAULT; 16061da177e4SLinus Torvalds if (copy_to_user(optval, &val, olr)) 16071da177e4SLinus Torvalds return -EFAULT; 16081da177e4SLinus Torvalds return 0; 16091da177e4SLinus Torvalds } 16101da177e4SLinus Torvalds 16117ef8f65dSNikolay Aleksandrov /* The IP multicast ioctl support routines. */ 16121da177e4SLinus Torvalds int ipmr_ioctl(struct sock *sk, int cmd, void __user *arg) 16131da177e4SLinus Torvalds { 16141da177e4SLinus Torvalds struct sioc_sg_req sr; 16151da177e4SLinus Torvalds struct sioc_vif_req vr; 16161da177e4SLinus Torvalds struct vif_device *vif; 16171da177e4SLinus Torvalds struct mfc_cache *c; 16184feb88e5SBenjamin Thery struct net *net = sock_net(sk); 1619f0ad0860SPatrick McHardy struct mr_table *mrt; 1620f0ad0860SPatrick McHardy 1621f0ad0860SPatrick McHardy mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT); 162251456b29SIan Morris if (!mrt) 1623f0ad0860SPatrick McHardy return -ENOENT; 16241da177e4SLinus Torvalds 1625132adf54SStephen Hemminger switch (cmd) { 16261da177e4SLinus Torvalds case SIOCGETVIFCNT: 16271da177e4SLinus Torvalds if (copy_from_user(&vr, arg, sizeof(vr))) 16281da177e4SLinus Torvalds return -EFAULT; 16290c12295aSPatrick McHardy if (vr.vifi >= mrt->maxvif) 16301da177e4SLinus Torvalds return -EINVAL; 16315648451eSGustavo A. R. Silva vr.vifi = array_index_nospec(vr.vifi, mrt->maxvif); 16321da177e4SLinus Torvalds read_lock(&mrt_lock); 16330c12295aSPatrick McHardy vif = &mrt->vif_table[vr.vifi]; 16340c12295aSPatrick McHardy if (VIF_EXISTS(mrt, vr.vifi)) { 16351da177e4SLinus Torvalds vr.icount = vif->pkt_in; 16361da177e4SLinus Torvalds vr.ocount = vif->pkt_out; 16371da177e4SLinus Torvalds vr.ibytes = vif->bytes_in; 16381da177e4SLinus Torvalds vr.obytes = vif->bytes_out; 16391da177e4SLinus Torvalds read_unlock(&mrt_lock); 16401da177e4SLinus Torvalds 16411da177e4SLinus Torvalds if (copy_to_user(arg, &vr, sizeof(vr))) 16421da177e4SLinus Torvalds return -EFAULT; 16431da177e4SLinus Torvalds return 0; 16441da177e4SLinus Torvalds } 16451da177e4SLinus Torvalds read_unlock(&mrt_lock); 16461da177e4SLinus Torvalds return -EADDRNOTAVAIL; 16471da177e4SLinus Torvalds case SIOCGETSGCNT: 16481da177e4SLinus Torvalds if (copy_from_user(&sr, arg, sizeof(sr))) 16491da177e4SLinus Torvalds return -EFAULT; 16501da177e4SLinus Torvalds 1651a8c9486bSEric Dumazet rcu_read_lock(); 16520c12295aSPatrick McHardy c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr); 16531da177e4SLinus Torvalds if (c) { 1654494fff56SYuval Mintz sr.pktcnt = c->_c.mfc_un.res.pkt; 1655494fff56SYuval Mintz sr.bytecnt = c->_c.mfc_un.res.bytes; 1656494fff56SYuval Mintz sr.wrong_if = c->_c.mfc_un.res.wrong_if; 1657a8c9486bSEric Dumazet rcu_read_unlock(); 16581da177e4SLinus Torvalds 16591da177e4SLinus Torvalds if (copy_to_user(arg, &sr, sizeof(sr))) 16601da177e4SLinus Torvalds return -EFAULT; 16611da177e4SLinus Torvalds return 0; 16621da177e4SLinus Torvalds } 1663a8c9486bSEric Dumazet rcu_read_unlock(); 16641da177e4SLinus Torvalds return -EADDRNOTAVAIL; 16651da177e4SLinus Torvalds default: 16661da177e4SLinus Torvalds return -ENOIOCTLCMD; 16671da177e4SLinus Torvalds } 16681da177e4SLinus Torvalds } 16691da177e4SLinus Torvalds 1670709b46e8SEric W. Biederman #ifdef CONFIG_COMPAT 1671709b46e8SEric W. Biederman struct compat_sioc_sg_req { 1672709b46e8SEric W. Biederman struct in_addr src; 1673709b46e8SEric W. Biederman struct in_addr grp; 1674709b46e8SEric W. Biederman compat_ulong_t pktcnt; 1675709b46e8SEric W. Biederman compat_ulong_t bytecnt; 1676709b46e8SEric W. Biederman compat_ulong_t wrong_if; 1677709b46e8SEric W. Biederman }; 1678709b46e8SEric W. Biederman 1679ca6b8bb0SDavid S. Miller struct compat_sioc_vif_req { 1680ca6b8bb0SDavid S. Miller vifi_t vifi; /* Which iface */ 1681ca6b8bb0SDavid S. Miller compat_ulong_t icount; 1682ca6b8bb0SDavid S. Miller compat_ulong_t ocount; 1683ca6b8bb0SDavid S. Miller compat_ulong_t ibytes; 1684ca6b8bb0SDavid S. Miller compat_ulong_t obytes; 1685ca6b8bb0SDavid S. Miller }; 1686ca6b8bb0SDavid S. Miller 1687709b46e8SEric W. Biederman int ipmr_compat_ioctl(struct sock *sk, unsigned int cmd, void __user *arg) 1688709b46e8SEric W. Biederman { 16890033d5adSDavid S. Miller struct compat_sioc_sg_req sr; 1690ca6b8bb0SDavid S. Miller struct compat_sioc_vif_req vr; 1691ca6b8bb0SDavid S. Miller struct vif_device *vif; 1692709b46e8SEric W. Biederman struct mfc_cache *c; 1693709b46e8SEric W. Biederman struct net *net = sock_net(sk); 1694709b46e8SEric W. Biederman struct mr_table *mrt; 1695709b46e8SEric W. Biederman 1696709b46e8SEric W. Biederman mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT); 169751456b29SIan Morris if (!mrt) 1698709b46e8SEric W. Biederman return -ENOENT; 1699709b46e8SEric W. Biederman 1700709b46e8SEric W. Biederman switch (cmd) { 1701ca6b8bb0SDavid S. Miller case SIOCGETVIFCNT: 1702ca6b8bb0SDavid S. Miller if (copy_from_user(&vr, arg, sizeof(vr))) 1703ca6b8bb0SDavid S. Miller return -EFAULT; 1704ca6b8bb0SDavid S. Miller if (vr.vifi >= mrt->maxvif) 1705ca6b8bb0SDavid S. Miller return -EINVAL; 17065648451eSGustavo A. R. Silva vr.vifi = array_index_nospec(vr.vifi, mrt->maxvif); 1707ca6b8bb0SDavid S. Miller read_lock(&mrt_lock); 1708ca6b8bb0SDavid S. Miller vif = &mrt->vif_table[vr.vifi]; 1709ca6b8bb0SDavid S. Miller if (VIF_EXISTS(mrt, vr.vifi)) { 1710ca6b8bb0SDavid S. Miller vr.icount = vif->pkt_in; 1711ca6b8bb0SDavid S. Miller vr.ocount = vif->pkt_out; 1712ca6b8bb0SDavid S. Miller vr.ibytes = vif->bytes_in; 1713ca6b8bb0SDavid S. Miller vr.obytes = vif->bytes_out; 1714ca6b8bb0SDavid S. Miller read_unlock(&mrt_lock); 1715ca6b8bb0SDavid S. Miller 1716ca6b8bb0SDavid S. Miller if (copy_to_user(arg, &vr, sizeof(vr))) 1717ca6b8bb0SDavid S. Miller return -EFAULT; 1718ca6b8bb0SDavid S. Miller return 0; 1719ca6b8bb0SDavid S. Miller } 1720ca6b8bb0SDavid S. Miller read_unlock(&mrt_lock); 1721ca6b8bb0SDavid S. Miller return -EADDRNOTAVAIL; 1722709b46e8SEric W. Biederman case SIOCGETSGCNT: 1723709b46e8SEric W. Biederman if (copy_from_user(&sr, arg, sizeof(sr))) 1724709b46e8SEric W. Biederman return -EFAULT; 1725709b46e8SEric W. Biederman 1726709b46e8SEric W. Biederman rcu_read_lock(); 1727709b46e8SEric W. Biederman c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr); 1728709b46e8SEric W. Biederman if (c) { 1729494fff56SYuval Mintz sr.pktcnt = c->_c.mfc_un.res.pkt; 1730494fff56SYuval Mintz sr.bytecnt = c->_c.mfc_un.res.bytes; 1731494fff56SYuval Mintz sr.wrong_if = c->_c.mfc_un.res.wrong_if; 1732709b46e8SEric W. Biederman rcu_read_unlock(); 1733709b46e8SEric W. Biederman 1734709b46e8SEric W. Biederman if (copy_to_user(arg, &sr, sizeof(sr))) 1735709b46e8SEric W. Biederman return -EFAULT; 1736709b46e8SEric W. Biederman return 0; 1737709b46e8SEric W. Biederman } 1738709b46e8SEric W. Biederman rcu_read_unlock(); 1739709b46e8SEric W. Biederman return -EADDRNOTAVAIL; 1740709b46e8SEric W. Biederman default: 1741709b46e8SEric W. Biederman return -ENOIOCTLCMD; 1742709b46e8SEric W. Biederman } 1743709b46e8SEric W. Biederman } 1744709b46e8SEric W. Biederman #endif 1745709b46e8SEric W. Biederman 17461da177e4SLinus Torvalds static int ipmr_device_event(struct notifier_block *this, unsigned long event, void *ptr) 17471da177e4SLinus Torvalds { 1748351638e7SJiri Pirko struct net_device *dev = netdev_notifier_info_to_dev(ptr); 17494feb88e5SBenjamin Thery struct net *net = dev_net(dev); 1750f0ad0860SPatrick McHardy struct mr_table *mrt; 17511da177e4SLinus Torvalds struct vif_device *v; 17521da177e4SLinus Torvalds int ct; 1753e9dc8653SEric W. Biederman 17541da177e4SLinus Torvalds if (event != NETDEV_UNREGISTER) 17551da177e4SLinus Torvalds return NOTIFY_DONE; 1756f0ad0860SPatrick McHardy 1757f0ad0860SPatrick McHardy ipmr_for_each_table(mrt, net) { 17580c12295aSPatrick McHardy v = &mrt->vif_table[0]; 17590c12295aSPatrick McHardy for (ct = 0; ct < mrt->maxvif; ct++, v++) { 1760e9dc8653SEric W. Biederman if (v->dev == dev) 1761e92036a6SRongQing.Li vif_delete(mrt, ct, 1, NULL); 17621da177e4SLinus Torvalds } 17631da177e4SLinus Torvalds } 17641da177e4SLinus Torvalds return NOTIFY_DONE; 17651da177e4SLinus Torvalds } 17661da177e4SLinus Torvalds 17671da177e4SLinus Torvalds static struct notifier_block ip_mr_notifier = { 17681da177e4SLinus Torvalds .notifier_call = ipmr_device_event, 17691da177e4SLinus Torvalds }; 17701da177e4SLinus Torvalds 17717ef8f65dSNikolay Aleksandrov /* Encapsulate a packet by attaching a valid IPIP header to it. 17721da177e4SLinus Torvalds * This avoids tunnel drivers and other mess and gives us the speed so 17731da177e4SLinus Torvalds * important for multicast video. 17741da177e4SLinus Torvalds */ 1775b6a7719aSHannes Frederic Sowa static void ip_encap(struct net *net, struct sk_buff *skb, 1776b6a7719aSHannes Frederic Sowa __be32 saddr, __be32 daddr) 17771da177e4SLinus Torvalds { 17788856dfa3SArnaldo Carvalho de Melo struct iphdr *iph; 1779b71d1d42SEric Dumazet const struct iphdr *old_iph = ip_hdr(skb); 17808856dfa3SArnaldo Carvalho de Melo 17818856dfa3SArnaldo Carvalho de Melo skb_push(skb, sizeof(struct iphdr)); 1782b0e380b1SArnaldo Carvalho de Melo skb->transport_header = skb->network_header; 17838856dfa3SArnaldo Carvalho de Melo skb_reset_network_header(skb); 1784eddc9ec5SArnaldo Carvalho de Melo iph = ip_hdr(skb); 17851da177e4SLinus Torvalds 17861da177e4SLinus Torvalds iph->version = 4; 1787e023dd64SArnaldo Carvalho de Melo iph->tos = old_iph->tos; 1788e023dd64SArnaldo Carvalho de Melo iph->ttl = old_iph->ttl; 17891da177e4SLinus Torvalds iph->frag_off = 0; 17901da177e4SLinus Torvalds iph->daddr = daddr; 17911da177e4SLinus Torvalds iph->saddr = saddr; 17921da177e4SLinus Torvalds iph->protocol = IPPROTO_IPIP; 17931da177e4SLinus Torvalds iph->ihl = 5; 17941da177e4SLinus Torvalds iph->tot_len = htons(skb->len); 1795b6a7719aSHannes Frederic Sowa ip_select_ident(net, skb, NULL); 17961da177e4SLinus Torvalds ip_send_check(iph); 17971da177e4SLinus Torvalds 17981da177e4SLinus Torvalds memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt)); 1799895b5c9fSFlorian Westphal nf_reset_ct(skb); 18001da177e4SLinus Torvalds } 18011da177e4SLinus Torvalds 18020c4b51f0SEric W. Biederman static inline int ipmr_forward_finish(struct net *net, struct sock *sk, 18030c4b51f0SEric W. Biederman struct sk_buff *skb) 18041da177e4SLinus Torvalds { 18051da177e4SLinus Torvalds struct ip_options *opt = &(IPCB(skb)->opt); 18061da177e4SLinus Torvalds 180773186df8SDavid S. Miller IP_INC_STATS(net, IPSTATS_MIB_OUTFORWDATAGRAMS); 180873186df8SDavid S. Miller IP_ADD_STATS(net, IPSTATS_MIB_OUTOCTETS, skb->len); 18091da177e4SLinus Torvalds 18101da177e4SLinus Torvalds if (unlikely(opt->optlen)) 18111da177e4SLinus Torvalds ip_forward_options(skb); 18121da177e4SLinus Torvalds 181313206b6bSEric W. Biederman return dst_output(net, sk, skb); 18141da177e4SLinus Torvalds } 18151da177e4SLinus Torvalds 1816a5bc9294SYotam Gigi #ifdef CONFIG_NET_SWITCHDEV 1817a5bc9294SYotam Gigi static bool ipmr_forward_offloaded(struct sk_buff *skb, struct mr_table *mrt, 1818a5bc9294SYotam Gigi int in_vifi, int out_vifi) 1819a5bc9294SYotam Gigi { 1820a5bc9294SYotam Gigi struct vif_device *out_vif = &mrt->vif_table[out_vifi]; 1821a5bc9294SYotam Gigi struct vif_device *in_vif = &mrt->vif_table[in_vifi]; 1822a5bc9294SYotam Gigi 1823875e8939SIdo Schimmel if (!skb->offload_l3_fwd_mark) 1824a5bc9294SYotam Gigi return false; 1825a5bc9294SYotam Gigi if (!out_vif->dev_parent_id.id_len || !in_vif->dev_parent_id.id_len) 1826a5bc9294SYotam Gigi return false; 1827a5bc9294SYotam Gigi return netdev_phys_item_id_same(&out_vif->dev_parent_id, 1828a5bc9294SYotam Gigi &in_vif->dev_parent_id); 1829a5bc9294SYotam Gigi } 1830a5bc9294SYotam Gigi #else 1831a5bc9294SYotam Gigi static bool ipmr_forward_offloaded(struct sk_buff *skb, struct mr_table *mrt, 1832a5bc9294SYotam Gigi int in_vifi, int out_vifi) 1833a5bc9294SYotam Gigi { 1834a5bc9294SYotam Gigi return false; 1835a5bc9294SYotam Gigi } 1836a5bc9294SYotam Gigi #endif 1837a5bc9294SYotam Gigi 18387ef8f65dSNikolay Aleksandrov /* Processing handlers for ipmr_forward */ 18391da177e4SLinus Torvalds 18400c12295aSPatrick McHardy static void ipmr_queue_xmit(struct net *net, struct mr_table *mrt, 18416e0735d1SDavid Ahern int in_vifi, struct sk_buff *skb, int vifi) 18421da177e4SLinus Torvalds { 1843eddc9ec5SArnaldo Carvalho de Melo const struct iphdr *iph = ip_hdr(skb); 18440c12295aSPatrick McHardy struct vif_device *vif = &mrt->vif_table[vifi]; 18451da177e4SLinus Torvalds struct net_device *dev; 18461da177e4SLinus Torvalds struct rtable *rt; 184731e4543dSDavid S. Miller struct flowi4 fl4; 18481da177e4SLinus Torvalds int encap = 0; 18491da177e4SLinus Torvalds 185051456b29SIan Morris if (!vif->dev) 18511da177e4SLinus Torvalds goto out_free; 18521da177e4SLinus Torvalds 18531da177e4SLinus Torvalds if (vif->flags & VIFF_REGISTER) { 18541da177e4SLinus Torvalds vif->pkt_out++; 18551da177e4SLinus Torvalds vif->bytes_out += skb->len; 1856cf3677aeSPavel Emelyanov vif->dev->stats.tx_bytes += skb->len; 1857cf3677aeSPavel Emelyanov vif->dev->stats.tx_packets++; 18580c12295aSPatrick McHardy ipmr_cache_report(mrt, skb, vifi, IGMPMSG_WHOLEPKT); 185969ebbf58SIlpo Järvinen goto out_free; 18601da177e4SLinus Torvalds } 18611da177e4SLinus Torvalds 1862a5bc9294SYotam Gigi if (ipmr_forward_offloaded(skb, mrt, in_vifi, vifi)) 1863a5bc9294SYotam Gigi goto out_free; 1864a5bc9294SYotam Gigi 18651da177e4SLinus Torvalds if (vif->flags & VIFF_TUNNEL) { 186631e4543dSDavid S. Miller rt = ip_route_output_ports(net, &fl4, NULL, 186778fbfd8aSDavid S. Miller vif->remote, vif->local, 186878fbfd8aSDavid S. Miller 0, 0, 186978fbfd8aSDavid S. Miller IPPROTO_IPIP, 187078fbfd8aSDavid S. Miller RT_TOS(iph->tos), vif->link); 1871b23dd4feSDavid S. Miller if (IS_ERR(rt)) 18721da177e4SLinus Torvalds goto out_free; 18731da177e4SLinus Torvalds encap = sizeof(struct iphdr); 18741da177e4SLinus Torvalds } else { 187531e4543dSDavid S. Miller rt = ip_route_output_ports(net, &fl4, NULL, iph->daddr, 0, 187678fbfd8aSDavid S. Miller 0, 0, 187778fbfd8aSDavid S. Miller IPPROTO_IPIP, 187878fbfd8aSDavid S. Miller RT_TOS(iph->tos), vif->link); 1879b23dd4feSDavid S. Miller if (IS_ERR(rt)) 18801da177e4SLinus Torvalds goto out_free; 18811da177e4SLinus Torvalds } 18821da177e4SLinus Torvalds 1883d8d1f30bSChangli Gao dev = rt->dst.dev; 18841da177e4SLinus Torvalds 1885d8d1f30bSChangli Gao if (skb->len+encap > dst_mtu(&rt->dst) && (ntohs(iph->frag_off) & IP_DF)) { 18861da177e4SLinus Torvalds /* Do not fragment multicasts. Alas, IPv4 does not 1887a8cb16ddSEric Dumazet * allow to send ICMP, so that packets will disappear 1888a8cb16ddSEric Dumazet * to blackhole. 18891da177e4SLinus Torvalds */ 189073186df8SDavid S. Miller IP_INC_STATS(net, IPSTATS_MIB_FRAGFAILS); 18911da177e4SLinus Torvalds ip_rt_put(rt); 18921da177e4SLinus Torvalds goto out_free; 18931da177e4SLinus Torvalds } 18941da177e4SLinus Torvalds 1895d8d1f30bSChangli Gao encap += LL_RESERVED_SPACE(dev) + rt->dst.header_len; 18961da177e4SLinus Torvalds 18971da177e4SLinus Torvalds if (skb_cow(skb, encap)) { 18981da177e4SLinus Torvalds ip_rt_put(rt); 18991da177e4SLinus Torvalds goto out_free; 19001da177e4SLinus Torvalds } 19011da177e4SLinus Torvalds 19021da177e4SLinus Torvalds vif->pkt_out++; 19031da177e4SLinus Torvalds vif->bytes_out += skb->len; 19041da177e4SLinus Torvalds 1905adf30907SEric Dumazet skb_dst_drop(skb); 1906d8d1f30bSChangli Gao skb_dst_set(skb, &rt->dst); 1907eddc9ec5SArnaldo Carvalho de Melo ip_decrease_ttl(ip_hdr(skb)); 19081da177e4SLinus Torvalds 19091da177e4SLinus Torvalds /* FIXME: forward and output firewalls used to be called here. 1910a8cb16ddSEric Dumazet * What do we do with netfilter? -- RR 1911a8cb16ddSEric Dumazet */ 19121da177e4SLinus Torvalds if (vif->flags & VIFF_TUNNEL) { 1913b6a7719aSHannes Frederic Sowa ip_encap(net, skb, vif->local, vif->remote); 19141da177e4SLinus Torvalds /* FIXME: extra output firewall step used to be here. --RR */ 19152f4c02d4SPavel Emelyanov vif->dev->stats.tx_packets++; 19162f4c02d4SPavel Emelyanov vif->dev->stats.tx_bytes += skb->len; 19171da177e4SLinus Torvalds } 19181da177e4SLinus Torvalds 19199ee6c5dcSLance Richardson IPCB(skb)->flags |= IPSKB_FORWARDED; 19201da177e4SLinus Torvalds 19217ef8f65dSNikolay Aleksandrov /* RFC1584 teaches, that DVMRP/PIM router must deliver packets locally 19221da177e4SLinus Torvalds * not only before forwarding, but after forwarding on all output 19231da177e4SLinus Torvalds * interfaces. It is clear, if mrouter runs a multicasting 19241da177e4SLinus Torvalds * program, it should receive packets not depending to what interface 19251da177e4SLinus Torvalds * program is joined. 19261da177e4SLinus Torvalds * If we will not make it, the program will have to join on all 19271da177e4SLinus Torvalds * interfaces. On the other hand, multihoming host (or router, but 19281da177e4SLinus Torvalds * not mrouter) cannot join to more than one interface - it will 19291da177e4SLinus Torvalds * result in receiving multiple packets. 19301da177e4SLinus Torvalds */ 193129a26a56SEric W. Biederman NF_HOOK(NFPROTO_IPV4, NF_INET_FORWARD, 193229a26a56SEric W. Biederman net, NULL, skb, skb->dev, dev, 19331da177e4SLinus Torvalds ipmr_forward_finish); 19341da177e4SLinus Torvalds return; 19351da177e4SLinus Torvalds 19361da177e4SLinus Torvalds out_free: 19371da177e4SLinus Torvalds kfree_skb(skb); 19381da177e4SLinus Torvalds } 19391da177e4SLinus Torvalds 19400c12295aSPatrick McHardy static int ipmr_find_vif(struct mr_table *mrt, struct net_device *dev) 19411da177e4SLinus Torvalds { 19421da177e4SLinus Torvalds int ct; 19430c12295aSPatrick McHardy 19440c12295aSPatrick McHardy for (ct = mrt->maxvif-1; ct >= 0; ct--) { 19450c12295aSPatrick McHardy if (mrt->vif_table[ct].dev == dev) 19461da177e4SLinus Torvalds break; 19471da177e4SLinus Torvalds } 19481da177e4SLinus Torvalds return ct; 19491da177e4SLinus Torvalds } 19501da177e4SLinus Torvalds 19511da177e4SLinus Torvalds /* "local" means that we should preserve one skb (for local delivery) */ 1952c4854ec8SRami Rosen static void ip_mr_forward(struct net *net, struct mr_table *mrt, 19534b1f0d33SDonald Sharp struct net_device *dev, struct sk_buff *skb, 1954494fff56SYuval Mintz struct mfc_cache *c, int local) 19551da177e4SLinus Torvalds { 19564b1f0d33SDonald Sharp int true_vifi = ipmr_find_vif(mrt, dev); 19571da177e4SLinus Torvalds int psend = -1; 19581da177e4SLinus Torvalds int vif, ct; 19591da177e4SLinus Torvalds 1960494fff56SYuval Mintz vif = c->_c.mfc_parent; 1961494fff56SYuval Mintz c->_c.mfc_un.res.pkt++; 1962494fff56SYuval Mintz c->_c.mfc_un.res.bytes += skb->len; 1963494fff56SYuval Mintz c->_c.mfc_un.res.lastuse = jiffies; 19641da177e4SLinus Torvalds 1965494fff56SYuval Mintz if (c->mfc_origin == htonl(INADDR_ANY) && true_vifi >= 0) { 1966660b26dcSNicolas Dichtel struct mfc_cache *cache_proxy; 1967660b26dcSNicolas Dichtel 1968660b26dcSNicolas Dichtel /* For an (*,G) entry, we only check that the incomming 1969660b26dcSNicolas Dichtel * interface is part of the static tree. 1970660b26dcSNicolas Dichtel */ 1971845c9a7aSYuval Mintz cache_proxy = mr_mfc_find_any_parent(mrt, vif); 1972660b26dcSNicolas Dichtel if (cache_proxy && 1973494fff56SYuval Mintz cache_proxy->_c.mfc_un.res.ttls[true_vifi] < 255) 1974660b26dcSNicolas Dichtel goto forward; 1975660b26dcSNicolas Dichtel } 1976660b26dcSNicolas Dichtel 19777ef8f65dSNikolay Aleksandrov /* Wrong interface: drop packet and (maybe) send PIM assert. */ 19784b1f0d33SDonald Sharp if (mrt->vif_table[vif].dev != dev) { 1979c7537967SDavid S. Miller if (rt_is_output_route(skb_rtable(skb))) { 19801da177e4SLinus Torvalds /* It is our own packet, looped back. 1981a8cb16ddSEric Dumazet * Very complicated situation... 1982a8cb16ddSEric Dumazet * 1983a8cb16ddSEric Dumazet * The best workaround until routing daemons will be 1984a8cb16ddSEric Dumazet * fixed is not to redistribute packet, if it was 1985a8cb16ddSEric Dumazet * send through wrong interface. It means, that 1986a8cb16ddSEric Dumazet * multicast applications WILL NOT work for 1987a8cb16ddSEric Dumazet * (S,G), which have default multicast route pointing 1988a8cb16ddSEric Dumazet * to wrong oif. In any case, it is not a good 1989a8cb16ddSEric Dumazet * idea to use multicasting applications on router. 19901da177e4SLinus Torvalds */ 19911da177e4SLinus Torvalds goto dont_forward; 19921da177e4SLinus Torvalds } 19931da177e4SLinus Torvalds 1994494fff56SYuval Mintz c->_c.mfc_un.res.wrong_if++; 19951da177e4SLinus Torvalds 19960c12295aSPatrick McHardy if (true_vifi >= 0 && mrt->mroute_do_assert && 19971da177e4SLinus Torvalds /* pimsm uses asserts, when switching from RPT to SPT, 1998a8cb16ddSEric Dumazet * so that we cannot check that packet arrived on an oif. 1999a8cb16ddSEric Dumazet * It is bad, but otherwise we would need to move pretty 2000a8cb16ddSEric Dumazet * large chunk of pimd to kernel. Ough... --ANK 20011da177e4SLinus Torvalds */ 20020c12295aSPatrick McHardy (mrt->mroute_do_pim || 2003494fff56SYuval Mintz c->_c.mfc_un.res.ttls[true_vifi] < 255) && 20041da177e4SLinus Torvalds time_after(jiffies, 2005494fff56SYuval Mintz c->_c.mfc_un.res.last_assert + 2006494fff56SYuval Mintz MFC_ASSERT_THRESH)) { 2007494fff56SYuval Mintz c->_c.mfc_un.res.last_assert = jiffies; 20080c12295aSPatrick McHardy ipmr_cache_report(mrt, skb, true_vifi, IGMPMSG_WRONGVIF); 2009c921c207SNikolay Aleksandrov if (mrt->mroute_do_wrvifwhole) 2010c921c207SNikolay Aleksandrov ipmr_cache_report(mrt, skb, true_vifi, 2011c921c207SNikolay Aleksandrov IGMPMSG_WRVIFWHOLE); 20121da177e4SLinus Torvalds } 20131da177e4SLinus Torvalds goto dont_forward; 20141da177e4SLinus Torvalds } 20151da177e4SLinus Torvalds 2016660b26dcSNicolas Dichtel forward: 20170c12295aSPatrick McHardy mrt->vif_table[vif].pkt_in++; 20180c12295aSPatrick McHardy mrt->vif_table[vif].bytes_in += skb->len; 20191da177e4SLinus Torvalds 20207ef8f65dSNikolay Aleksandrov /* Forward the frame */ 2021494fff56SYuval Mintz if (c->mfc_origin == htonl(INADDR_ANY) && 2022494fff56SYuval Mintz c->mfc_mcastgrp == htonl(INADDR_ANY)) { 2023660b26dcSNicolas Dichtel if (true_vifi >= 0 && 2024494fff56SYuval Mintz true_vifi != c->_c.mfc_parent && 2025660b26dcSNicolas Dichtel ip_hdr(skb)->ttl > 2026494fff56SYuval Mintz c->_c.mfc_un.res.ttls[c->_c.mfc_parent]) { 2027660b26dcSNicolas Dichtel /* It's an (*,*) entry and the packet is not coming from 2028660b26dcSNicolas Dichtel * the upstream: forward the packet to the upstream 2029660b26dcSNicolas Dichtel * only. 2030660b26dcSNicolas Dichtel */ 2031494fff56SYuval Mintz psend = c->_c.mfc_parent; 2032660b26dcSNicolas Dichtel goto last_forward; 2033660b26dcSNicolas Dichtel } 2034660b26dcSNicolas Dichtel goto dont_forward; 2035660b26dcSNicolas Dichtel } 2036494fff56SYuval Mintz for (ct = c->_c.mfc_un.res.maxvif - 1; 2037494fff56SYuval Mintz ct >= c->_c.mfc_un.res.minvif; ct--) { 2038660b26dcSNicolas Dichtel /* For (*,G) entry, don't forward to the incoming interface */ 2039494fff56SYuval Mintz if ((c->mfc_origin != htonl(INADDR_ANY) || 2040360eb5daSNicolas Dichtel ct != true_vifi) && 2041494fff56SYuval Mintz ip_hdr(skb)->ttl > c->_c.mfc_un.res.ttls[ct]) { 20421da177e4SLinus Torvalds if (psend != -1) { 20431da177e4SLinus Torvalds struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC); 2044a8cb16ddSEric Dumazet 20451da177e4SLinus Torvalds if (skb2) 2046a5bc9294SYotam Gigi ipmr_queue_xmit(net, mrt, true_vifi, 20476e0735d1SDavid Ahern skb2, psend); 20481da177e4SLinus Torvalds } 20491da177e4SLinus Torvalds psend = ct; 20501da177e4SLinus Torvalds } 20511da177e4SLinus Torvalds } 2052660b26dcSNicolas Dichtel last_forward: 20531da177e4SLinus Torvalds if (psend != -1) { 20541da177e4SLinus Torvalds if (local) { 20551da177e4SLinus Torvalds struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC); 2056a8cb16ddSEric Dumazet 20571da177e4SLinus Torvalds if (skb2) 2058a5bc9294SYotam Gigi ipmr_queue_xmit(net, mrt, true_vifi, skb2, 20596e0735d1SDavid Ahern psend); 20601da177e4SLinus Torvalds } else { 20616e0735d1SDavid Ahern ipmr_queue_xmit(net, mrt, true_vifi, skb, psend); 2062c4854ec8SRami Rosen return; 20631da177e4SLinus Torvalds } 20641da177e4SLinus Torvalds } 20651da177e4SLinus Torvalds 20661da177e4SLinus Torvalds dont_forward: 20671da177e4SLinus Torvalds if (!local) 20681da177e4SLinus Torvalds kfree_skb(skb); 20691da177e4SLinus Torvalds } 20701da177e4SLinus Torvalds 2071417da66fSDavid S. Miller static struct mr_table *ipmr_rt_fib_lookup(struct net *net, struct sk_buff *skb) 2072ee3f1aafSDavid S. Miller { 2073417da66fSDavid S. Miller struct rtable *rt = skb_rtable(skb); 2074417da66fSDavid S. Miller struct iphdr *iph = ip_hdr(skb); 2075da91981bSDavid S. Miller struct flowi4 fl4 = { 2076417da66fSDavid S. Miller .daddr = iph->daddr, 2077417da66fSDavid S. Miller .saddr = iph->saddr, 2078b0fe4a31SJulian Anastasov .flowi4_tos = RT_TOS(iph->tos), 20794fd551d7SDavid S. Miller .flowi4_oif = (rt_is_output_route(rt) ? 20804fd551d7SDavid S. Miller skb->dev->ifindex : 0), 20814fd551d7SDavid S. Miller .flowi4_iif = (rt_is_output_route(rt) ? 20821fb9489bSPavel Emelyanov LOOPBACK_IFINDEX : 20834fd551d7SDavid S. Miller skb->dev->ifindex), 2084b4869889SDavid Miller .flowi4_mark = skb->mark, 2085ee3f1aafSDavid S. Miller }; 2086ee3f1aafSDavid S. Miller struct mr_table *mrt; 2087ee3f1aafSDavid S. Miller int err; 2088ee3f1aafSDavid S. Miller 2089da91981bSDavid S. Miller err = ipmr_fib_lookup(net, &fl4, &mrt); 2090ee3f1aafSDavid S. Miller if (err) 2091ee3f1aafSDavid S. Miller return ERR_PTR(err); 2092ee3f1aafSDavid S. Miller return mrt; 2093ee3f1aafSDavid S. Miller } 20941da177e4SLinus Torvalds 20957ef8f65dSNikolay Aleksandrov /* Multicast packets for forwarding arrive here 20964c968709SEric Dumazet * Called with rcu_read_lock(); 20971da177e4SLinus Torvalds */ 20981da177e4SLinus Torvalds int ip_mr_input(struct sk_buff *skb) 20991da177e4SLinus Torvalds { 21001da177e4SLinus Torvalds struct mfc_cache *cache; 21014feb88e5SBenjamin Thery struct net *net = dev_net(skb->dev); 2102511c3f92SEric Dumazet int local = skb_rtable(skb)->rt_flags & RTCF_LOCAL; 2103f0ad0860SPatrick McHardy struct mr_table *mrt; 2104bcfc7d33SThomas Winter struct net_device *dev; 2105bcfc7d33SThomas Winter 2106bcfc7d33SThomas Winter /* skb->dev passed in is the loX master dev for vrfs. 2107bcfc7d33SThomas Winter * As there are no vifs associated with loopback devices, 2108bcfc7d33SThomas Winter * get the proper interface that does have a vif associated with it. 2109bcfc7d33SThomas Winter */ 2110bcfc7d33SThomas Winter dev = skb->dev; 2111bcfc7d33SThomas Winter if (netif_is_l3_master(skb->dev)) { 2112bcfc7d33SThomas Winter dev = dev_get_by_index_rcu(net, IPCB(skb)->iif); 2113bcfc7d33SThomas Winter if (!dev) { 2114bcfc7d33SThomas Winter kfree_skb(skb); 2115bcfc7d33SThomas Winter return -ENODEV; 2116bcfc7d33SThomas Winter } 2117bcfc7d33SThomas Winter } 21181da177e4SLinus Torvalds 21191da177e4SLinus Torvalds /* Packet is looped back after forward, it should not be 2120a8cb16ddSEric Dumazet * forwarded second time, but still can be delivered locally. 21211da177e4SLinus Torvalds */ 21221da177e4SLinus Torvalds if (IPCB(skb)->flags & IPSKB_FORWARDED) 21231da177e4SLinus Torvalds goto dont_forward; 21241da177e4SLinus Torvalds 2125417da66fSDavid S. Miller mrt = ipmr_rt_fib_lookup(net, skb); 2126ee3f1aafSDavid S. Miller if (IS_ERR(mrt)) { 2127e40dbc51SBen Greear kfree_skb(skb); 2128ee3f1aafSDavid S. Miller return PTR_ERR(mrt); 21295e2b61f7SDavid S. Miller } 21301da177e4SLinus Torvalds if (!local) { 21311da177e4SLinus Torvalds if (IPCB(skb)->opt.router_alert) { 21321da177e4SLinus Torvalds if (ip_call_ra_chain(skb)) 21331da177e4SLinus Torvalds return 0; 2134eddc9ec5SArnaldo Carvalho de Melo } else if (ip_hdr(skb)->protocol == IPPROTO_IGMP) { 21351da177e4SLinus Torvalds /* IGMPv1 (and broken IGMPv2 implementations sort of 21364c968709SEric Dumazet * Cisco IOS <= 11.2(8)) do not put router alert 21374c968709SEric Dumazet * option to IGMP packets destined to routable 21384c968709SEric Dumazet * groups. It is very bad, because it means 21394c968709SEric Dumazet * that we can forward NO IGMP messages. 21401da177e4SLinus Torvalds */ 21414c968709SEric Dumazet struct sock *mroute_sk; 21424c968709SEric Dumazet 21434c968709SEric Dumazet mroute_sk = rcu_dereference(mrt->mroute_sk); 21444c968709SEric Dumazet if (mroute_sk) { 2145895b5c9fSFlorian Westphal nf_reset_ct(skb); 21464c968709SEric Dumazet raw_rcv(mroute_sk, skb); 21471da177e4SLinus Torvalds return 0; 21481da177e4SLinus Torvalds } 21491da177e4SLinus Torvalds } 21501da177e4SLinus Torvalds } 21511da177e4SLinus Torvalds 2152a8c9486bSEric Dumazet /* already under rcu_read_lock() */ 21530c12295aSPatrick McHardy cache = ipmr_cache_find(mrt, ip_hdr(skb)->saddr, ip_hdr(skb)->daddr); 215451456b29SIan Morris if (!cache) { 2155bcfc7d33SThomas Winter int vif = ipmr_find_vif(mrt, dev); 2156660b26dcSNicolas Dichtel 2157660b26dcSNicolas Dichtel if (vif >= 0) 2158660b26dcSNicolas Dichtel cache = ipmr_cache_find_any(mrt, ip_hdr(skb)->daddr, 2159660b26dcSNicolas Dichtel vif); 2160660b26dcSNicolas Dichtel } 21611da177e4SLinus Torvalds 21627ef8f65dSNikolay Aleksandrov /* No usable cache entry */ 216351456b29SIan Morris if (!cache) { 21641da177e4SLinus Torvalds int vif; 21651da177e4SLinus Torvalds 21661da177e4SLinus Torvalds if (local) { 21671da177e4SLinus Torvalds struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC); 21681da177e4SLinus Torvalds ip_local_deliver(skb); 216951456b29SIan Morris if (!skb2) 21701da177e4SLinus Torvalds return -ENOBUFS; 21711da177e4SLinus Torvalds skb = skb2; 21721da177e4SLinus Torvalds } 21731da177e4SLinus Torvalds 2174a8c9486bSEric Dumazet read_lock(&mrt_lock); 2175bcfc7d33SThomas Winter vif = ipmr_find_vif(mrt, dev); 21761da177e4SLinus Torvalds if (vif >= 0) { 21774b1f0d33SDonald Sharp int err2 = ipmr_cache_unresolved(mrt, vif, skb, dev); 21781da177e4SLinus Torvalds read_unlock(&mrt_lock); 21791da177e4SLinus Torvalds 21800eae88f3SEric Dumazet return err2; 21811da177e4SLinus Torvalds } 21821da177e4SLinus Torvalds read_unlock(&mrt_lock); 21831da177e4SLinus Torvalds kfree_skb(skb); 21841da177e4SLinus Torvalds return -ENODEV; 21851da177e4SLinus Torvalds } 21861da177e4SLinus Torvalds 2187a8c9486bSEric Dumazet read_lock(&mrt_lock); 21884b1f0d33SDonald Sharp ip_mr_forward(net, mrt, dev, skb, cache, local); 21891da177e4SLinus Torvalds read_unlock(&mrt_lock); 21901da177e4SLinus Torvalds 21911da177e4SLinus Torvalds if (local) 21921da177e4SLinus Torvalds return ip_local_deliver(skb); 21931da177e4SLinus Torvalds 21941da177e4SLinus Torvalds return 0; 21951da177e4SLinus Torvalds 21961da177e4SLinus Torvalds dont_forward: 21971da177e4SLinus Torvalds if (local) 21981da177e4SLinus Torvalds return ip_local_deliver(skb); 21991da177e4SLinus Torvalds kfree_skb(skb); 22001da177e4SLinus Torvalds return 0; 22011da177e4SLinus Torvalds } 22021da177e4SLinus Torvalds 2203b1879204SIlpo Järvinen #ifdef CONFIG_IP_PIMSM_V1 22047ef8f65dSNikolay Aleksandrov /* Handle IGMP messages of PIMv1 */ 2205b1879204SIlpo Järvinen int pim_rcv_v1(struct sk_buff *skb) 2206b1879204SIlpo Järvinen { 2207b1879204SIlpo Järvinen struct igmphdr *pim; 22084feb88e5SBenjamin Thery struct net *net = dev_net(skb->dev); 2209f0ad0860SPatrick McHardy struct mr_table *mrt; 2210b1879204SIlpo Järvinen 2211b1879204SIlpo Järvinen if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr))) 2212b1879204SIlpo Järvinen goto drop; 2213b1879204SIlpo Järvinen 2214b1879204SIlpo Järvinen pim = igmp_hdr(skb); 2215b1879204SIlpo Järvinen 2216417da66fSDavid S. Miller mrt = ipmr_rt_fib_lookup(net, skb); 2217ee3f1aafSDavid S. Miller if (IS_ERR(mrt)) 2218f0ad0860SPatrick McHardy goto drop; 22190c12295aSPatrick McHardy if (!mrt->mroute_do_pim || 2220b1879204SIlpo Järvinen pim->group != PIM_V1_VERSION || pim->code != PIM_V1_REGISTER) 2221b1879204SIlpo Järvinen goto drop; 2222b1879204SIlpo Järvinen 2223f0ad0860SPatrick McHardy if (__pim_rcv(mrt, skb, sizeof(*pim))) { 22241da177e4SLinus Torvalds drop: 22251da177e4SLinus Torvalds kfree_skb(skb); 2226b1879204SIlpo Järvinen } 22271da177e4SLinus Torvalds return 0; 22281da177e4SLinus Torvalds } 22291da177e4SLinus Torvalds #endif 22301da177e4SLinus Torvalds 22311da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM_V2 22321da177e4SLinus Torvalds static int pim_rcv(struct sk_buff *skb) 22331da177e4SLinus Torvalds { 22341da177e4SLinus Torvalds struct pimreghdr *pim; 2235f0ad0860SPatrick McHardy struct net *net = dev_net(skb->dev); 2236f0ad0860SPatrick McHardy struct mr_table *mrt; 22371da177e4SLinus Torvalds 2238b1879204SIlpo Järvinen if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr))) 22391da177e4SLinus Torvalds goto drop; 22401da177e4SLinus Torvalds 22419c70220bSArnaldo Carvalho de Melo pim = (struct pimreghdr *)skb_transport_header(skb); 224256245caeSNikolay Aleksandrov if (pim->type != ((PIM_VERSION << 4) | (PIM_TYPE_REGISTER)) || 22431da177e4SLinus Torvalds (pim->flags & PIM_NULL_REGISTER) || 22441da177e4SLinus Torvalds (ip_compute_csum((void *)pim, sizeof(*pim)) != 0 && 2245d3bc23e7SAl Viro csum_fold(skb_checksum(skb, 0, skb->len, 0)))) 22461da177e4SLinus Torvalds goto drop; 22471da177e4SLinus Torvalds 2248417da66fSDavid S. Miller mrt = ipmr_rt_fib_lookup(net, skb); 2249ee3f1aafSDavid S. Miller if (IS_ERR(mrt)) 2250f0ad0860SPatrick McHardy goto drop; 2251f0ad0860SPatrick McHardy if (__pim_rcv(mrt, skb, sizeof(*pim))) { 22521da177e4SLinus Torvalds drop: 22531da177e4SLinus Torvalds kfree_skb(skb); 2254b1879204SIlpo Järvinen } 22551da177e4SLinus Torvalds return 0; 22561da177e4SLinus Torvalds } 22571da177e4SLinus Torvalds #endif 22581da177e4SLinus Torvalds 22599a1b9496SDavid S. Miller int ipmr_get_route(struct net *net, struct sk_buff *skb, 22609a1b9496SDavid S. Miller __be32 saddr, __be32 daddr, 22619f09eaeaSDavid Ahern struct rtmsg *rtm, u32 portid) 22621da177e4SLinus Torvalds { 22631da177e4SLinus Torvalds struct mfc_cache *cache; 22649a1b9496SDavid S. Miller struct mr_table *mrt; 22659a1b9496SDavid S. Miller int err; 22661da177e4SLinus Torvalds 2267f0ad0860SPatrick McHardy mrt = ipmr_get_table(net, RT_TABLE_DEFAULT); 226851456b29SIan Morris if (!mrt) 2269f0ad0860SPatrick McHardy return -ENOENT; 2270f0ad0860SPatrick McHardy 2271a8c9486bSEric Dumazet rcu_read_lock(); 22729a1b9496SDavid S. Miller cache = ipmr_cache_find(mrt, saddr, daddr); 227351456b29SIan Morris if (!cache && skb->dev) { 2274660b26dcSNicolas Dichtel int vif = ipmr_find_vif(mrt, skb->dev); 22751da177e4SLinus Torvalds 2276660b26dcSNicolas Dichtel if (vif >= 0) 2277660b26dcSNicolas Dichtel cache = ipmr_cache_find_any(mrt, daddr, vif); 2278660b26dcSNicolas Dichtel } 227951456b29SIan Morris if (!cache) { 228072287490SAlexey Kuznetsov struct sk_buff *skb2; 2281eddc9ec5SArnaldo Carvalho de Melo struct iphdr *iph; 22821da177e4SLinus Torvalds struct net_device *dev; 2283a8cb16ddSEric Dumazet int vif = -1; 22841da177e4SLinus Torvalds 22851da177e4SLinus Torvalds dev = skb->dev; 2286a8c9486bSEric Dumazet read_lock(&mrt_lock); 2287a8cb16ddSEric Dumazet if (dev) 2288a8cb16ddSEric Dumazet vif = ipmr_find_vif(mrt, dev); 2289a8cb16ddSEric Dumazet if (vif < 0) { 22901da177e4SLinus Torvalds read_unlock(&mrt_lock); 2291a8c9486bSEric Dumazet rcu_read_unlock(); 22921da177e4SLinus Torvalds return -ENODEV; 22931da177e4SLinus Torvalds } 22947901cd97SGuillaume Nault 22957901cd97SGuillaume Nault skb2 = skb_realloc_headroom(skb, sizeof(struct iphdr)); 229672287490SAlexey Kuznetsov if (!skb2) { 229772287490SAlexey Kuznetsov read_unlock(&mrt_lock); 2298a8c9486bSEric Dumazet rcu_read_unlock(); 229972287490SAlexey Kuznetsov return -ENOMEM; 230072287490SAlexey Kuznetsov } 230172287490SAlexey Kuznetsov 23022cf75070SNikolay Aleksandrov NETLINK_CB(skb2).portid = portid; 2303e2d1bca7SArnaldo Carvalho de Melo skb_push(skb2, sizeof(struct iphdr)); 2304e2d1bca7SArnaldo Carvalho de Melo skb_reset_network_header(skb2); 2305eddc9ec5SArnaldo Carvalho de Melo iph = ip_hdr(skb2); 2306eddc9ec5SArnaldo Carvalho de Melo iph->ihl = sizeof(struct iphdr) >> 2; 23079a1b9496SDavid S. Miller iph->saddr = saddr; 23089a1b9496SDavid S. Miller iph->daddr = daddr; 2309eddc9ec5SArnaldo Carvalho de Melo iph->version = 0; 23104b1f0d33SDonald Sharp err = ipmr_cache_unresolved(mrt, vif, skb2, dev); 23111da177e4SLinus Torvalds read_unlock(&mrt_lock); 2312a8c9486bSEric Dumazet rcu_read_unlock(); 23131da177e4SLinus Torvalds return err; 23141da177e4SLinus Torvalds } 23151da177e4SLinus Torvalds 2316a8c9486bSEric Dumazet read_lock(&mrt_lock); 23177b0db857SYuval Mintz err = mr_fill_mroute(mrt, skb, &cache->_c, rtm); 23181da177e4SLinus Torvalds read_unlock(&mrt_lock); 2319a8c9486bSEric Dumazet rcu_read_unlock(); 23201da177e4SLinus Torvalds return err; 23211da177e4SLinus Torvalds } 23221da177e4SLinus Torvalds 2323cb6a4e46SPatrick McHardy static int ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb, 232465886f43SNicolas Dichtel u32 portid, u32 seq, struct mfc_cache *c, int cmd, 232565886f43SNicolas Dichtel int flags) 2326cb6a4e46SPatrick McHardy { 2327cb6a4e46SPatrick McHardy struct nlmsghdr *nlh; 2328cb6a4e46SPatrick McHardy struct rtmsg *rtm; 23291eb99af5SNicolas Dichtel int err; 2330cb6a4e46SPatrick McHardy 233165886f43SNicolas Dichtel nlh = nlmsg_put(skb, portid, seq, cmd, sizeof(*rtm), flags); 233251456b29SIan Morris if (!nlh) 2333cb6a4e46SPatrick McHardy return -EMSGSIZE; 2334cb6a4e46SPatrick McHardy 2335cb6a4e46SPatrick McHardy rtm = nlmsg_data(nlh); 2336cb6a4e46SPatrick McHardy rtm->rtm_family = RTNL_FAMILY_IPMR; 2337cb6a4e46SPatrick McHardy rtm->rtm_dst_len = 32; 2338cb6a4e46SPatrick McHardy rtm->rtm_src_len = 32; 2339cb6a4e46SPatrick McHardy rtm->rtm_tos = 0; 2340cb6a4e46SPatrick McHardy rtm->rtm_table = mrt->id; 2341f3756b79SDavid S. Miller if (nla_put_u32(skb, RTA_TABLE, mrt->id)) 2342f3756b79SDavid S. Miller goto nla_put_failure; 2343cb6a4e46SPatrick McHardy rtm->rtm_type = RTN_MULTICAST; 2344cb6a4e46SPatrick McHardy rtm->rtm_scope = RT_SCOPE_UNIVERSE; 2345494fff56SYuval Mintz if (c->_c.mfc_flags & MFC_STATIC) 23469a68ac72SNicolas Dichtel rtm->rtm_protocol = RTPROT_STATIC; 23479a68ac72SNicolas Dichtel else 23489a68ac72SNicolas Dichtel rtm->rtm_protocol = RTPROT_MROUTED; 2349cb6a4e46SPatrick McHardy rtm->rtm_flags = 0; 2350cb6a4e46SPatrick McHardy 2351930345eaSJiri Benc if (nla_put_in_addr(skb, RTA_SRC, c->mfc_origin) || 2352930345eaSJiri Benc nla_put_in_addr(skb, RTA_DST, c->mfc_mcastgrp)) 2353f3756b79SDavid S. Miller goto nla_put_failure; 23547b0db857SYuval Mintz err = mr_fill_mroute(mrt, skb, &c->_c, rtm); 23551eb99af5SNicolas Dichtel /* do not break the dump if cache is unresolved */ 23561eb99af5SNicolas Dichtel if (err < 0 && err != -ENOENT) 2357cb6a4e46SPatrick McHardy goto nla_put_failure; 2358cb6a4e46SPatrick McHardy 2359053c095aSJohannes Berg nlmsg_end(skb, nlh); 2360053c095aSJohannes Berg return 0; 2361cb6a4e46SPatrick McHardy 2362cb6a4e46SPatrick McHardy nla_put_failure: 2363cb6a4e46SPatrick McHardy nlmsg_cancel(skb, nlh); 2364cb6a4e46SPatrick McHardy return -EMSGSIZE; 2365cb6a4e46SPatrick McHardy } 2366cb6a4e46SPatrick McHardy 23677b0db857SYuval Mintz static int _ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb, 23687b0db857SYuval Mintz u32 portid, u32 seq, struct mr_mfc *c, int cmd, 23697b0db857SYuval Mintz int flags) 23707b0db857SYuval Mintz { 23717b0db857SYuval Mintz return ipmr_fill_mroute(mrt, skb, portid, seq, (struct mfc_cache *)c, 23727b0db857SYuval Mintz cmd, flags); 23737b0db857SYuval Mintz } 23747b0db857SYuval Mintz 23758cd3ac9fSNicolas Dichtel static size_t mroute_msgsize(bool unresolved, int maxvif) 23768cd3ac9fSNicolas Dichtel { 23778cd3ac9fSNicolas Dichtel size_t len = 23788cd3ac9fSNicolas Dichtel NLMSG_ALIGN(sizeof(struct rtmsg)) 23798cd3ac9fSNicolas Dichtel + nla_total_size(4) /* RTA_TABLE */ 23808cd3ac9fSNicolas Dichtel + nla_total_size(4) /* RTA_SRC */ 23818cd3ac9fSNicolas Dichtel + nla_total_size(4) /* RTA_DST */ 23828cd3ac9fSNicolas Dichtel ; 23838cd3ac9fSNicolas Dichtel 23848cd3ac9fSNicolas Dichtel if (!unresolved) 23858cd3ac9fSNicolas Dichtel len = len 23868cd3ac9fSNicolas Dichtel + nla_total_size(4) /* RTA_IIF */ 23878cd3ac9fSNicolas Dichtel + nla_total_size(0) /* RTA_MULTIPATH */ 23888cd3ac9fSNicolas Dichtel + maxvif * NLA_ALIGN(sizeof(struct rtnexthop)) 23898cd3ac9fSNicolas Dichtel /* RTA_MFC_STATS */ 2390a9a08042SNicolas Dichtel + nla_total_size_64bit(sizeof(struct rta_mfc_stats)) 23918cd3ac9fSNicolas Dichtel ; 23928cd3ac9fSNicolas Dichtel 23938cd3ac9fSNicolas Dichtel return len; 23948cd3ac9fSNicolas Dichtel } 23958cd3ac9fSNicolas Dichtel 23968cd3ac9fSNicolas Dichtel static void mroute_netlink_event(struct mr_table *mrt, struct mfc_cache *mfc, 23978cd3ac9fSNicolas Dichtel int cmd) 23988cd3ac9fSNicolas Dichtel { 23998cd3ac9fSNicolas Dichtel struct net *net = read_pnet(&mrt->net); 24008cd3ac9fSNicolas Dichtel struct sk_buff *skb; 24018cd3ac9fSNicolas Dichtel int err = -ENOBUFS; 24028cd3ac9fSNicolas Dichtel 2403494fff56SYuval Mintz skb = nlmsg_new(mroute_msgsize(mfc->_c.mfc_parent >= MAXVIFS, 2404494fff56SYuval Mintz mrt->maxvif), 24058cd3ac9fSNicolas Dichtel GFP_ATOMIC); 240651456b29SIan Morris if (!skb) 24078cd3ac9fSNicolas Dichtel goto errout; 24088cd3ac9fSNicolas Dichtel 240965886f43SNicolas Dichtel err = ipmr_fill_mroute(mrt, skb, 0, 0, mfc, cmd, 0); 24108cd3ac9fSNicolas Dichtel if (err < 0) 24118cd3ac9fSNicolas Dichtel goto errout; 24128cd3ac9fSNicolas Dichtel 24138cd3ac9fSNicolas Dichtel rtnl_notify(skb, net, 0, RTNLGRP_IPV4_MROUTE, NULL, GFP_ATOMIC); 24148cd3ac9fSNicolas Dichtel return; 24158cd3ac9fSNicolas Dichtel 24168cd3ac9fSNicolas Dichtel errout: 24178cd3ac9fSNicolas Dichtel kfree_skb(skb); 24188cd3ac9fSNicolas Dichtel if (err < 0) 24198cd3ac9fSNicolas Dichtel rtnl_set_sk_err(net, RTNLGRP_IPV4_MROUTE, err); 24208cd3ac9fSNicolas Dichtel } 24218cd3ac9fSNicolas Dichtel 24225a645dd8SJulien Gomes static size_t igmpmsg_netlink_msgsize(size_t payloadlen) 24235a645dd8SJulien Gomes { 24245a645dd8SJulien Gomes size_t len = 24255a645dd8SJulien Gomes NLMSG_ALIGN(sizeof(struct rtgenmsg)) 24265a645dd8SJulien Gomes + nla_total_size(1) /* IPMRA_CREPORT_MSGTYPE */ 24275a645dd8SJulien Gomes + nla_total_size(4) /* IPMRA_CREPORT_VIF_ID */ 24285a645dd8SJulien Gomes + nla_total_size(4) /* IPMRA_CREPORT_SRC_ADDR */ 24295a645dd8SJulien Gomes + nla_total_size(4) /* IPMRA_CREPORT_DST_ADDR */ 24305a645dd8SJulien Gomes /* IPMRA_CREPORT_PKT */ 24315a645dd8SJulien Gomes + nla_total_size(payloadlen) 24325a645dd8SJulien Gomes ; 24335a645dd8SJulien Gomes 24345a645dd8SJulien Gomes return len; 24355a645dd8SJulien Gomes } 24365a645dd8SJulien Gomes 24375a645dd8SJulien Gomes static void igmpmsg_netlink_event(struct mr_table *mrt, struct sk_buff *pkt) 24385a645dd8SJulien Gomes { 24395a645dd8SJulien Gomes struct net *net = read_pnet(&mrt->net); 24405a645dd8SJulien Gomes struct nlmsghdr *nlh; 24415a645dd8SJulien Gomes struct rtgenmsg *rtgenm; 24425a645dd8SJulien Gomes struct igmpmsg *msg; 24435a645dd8SJulien Gomes struct sk_buff *skb; 24445a645dd8SJulien Gomes struct nlattr *nla; 24455a645dd8SJulien Gomes int payloadlen; 24465a645dd8SJulien Gomes 24475a645dd8SJulien Gomes payloadlen = pkt->len - sizeof(struct igmpmsg); 24485a645dd8SJulien Gomes msg = (struct igmpmsg *)skb_network_header(pkt); 24495a645dd8SJulien Gomes 24505a645dd8SJulien Gomes skb = nlmsg_new(igmpmsg_netlink_msgsize(payloadlen), GFP_ATOMIC); 24515a645dd8SJulien Gomes if (!skb) 24525a645dd8SJulien Gomes goto errout; 24535a645dd8SJulien Gomes 24545a645dd8SJulien Gomes nlh = nlmsg_put(skb, 0, 0, RTM_NEWCACHEREPORT, 24555a645dd8SJulien Gomes sizeof(struct rtgenmsg), 0); 24565a645dd8SJulien Gomes if (!nlh) 24575a645dd8SJulien Gomes goto errout; 24585a645dd8SJulien Gomes rtgenm = nlmsg_data(nlh); 24595a645dd8SJulien Gomes rtgenm->rtgen_family = RTNL_FAMILY_IPMR; 24605a645dd8SJulien Gomes if (nla_put_u8(skb, IPMRA_CREPORT_MSGTYPE, msg->im_msgtype) || 24615a645dd8SJulien Gomes nla_put_u32(skb, IPMRA_CREPORT_VIF_ID, msg->im_vif) || 24625a645dd8SJulien Gomes nla_put_in_addr(skb, IPMRA_CREPORT_SRC_ADDR, 24635a645dd8SJulien Gomes msg->im_src.s_addr) || 24645a645dd8SJulien Gomes nla_put_in_addr(skb, IPMRA_CREPORT_DST_ADDR, 24655a645dd8SJulien Gomes msg->im_dst.s_addr)) 24665a645dd8SJulien Gomes goto nla_put_failure; 24675a645dd8SJulien Gomes 24685a645dd8SJulien Gomes nla = nla_reserve(skb, IPMRA_CREPORT_PKT, payloadlen); 24695a645dd8SJulien Gomes if (!nla || skb_copy_bits(pkt, sizeof(struct igmpmsg), 24705a645dd8SJulien Gomes nla_data(nla), payloadlen)) 24715a645dd8SJulien Gomes goto nla_put_failure; 24725a645dd8SJulien Gomes 24735a645dd8SJulien Gomes nlmsg_end(skb, nlh); 24745a645dd8SJulien Gomes 24755a645dd8SJulien Gomes rtnl_notify(skb, net, 0, RTNLGRP_IPV4_MROUTE_R, NULL, GFP_ATOMIC); 24765a645dd8SJulien Gomes return; 24775a645dd8SJulien Gomes 24785a645dd8SJulien Gomes nla_put_failure: 24795a645dd8SJulien Gomes nlmsg_cancel(skb, nlh); 24805a645dd8SJulien Gomes errout: 24815a645dd8SJulien Gomes kfree_skb(skb); 24825a645dd8SJulien Gomes rtnl_set_sk_err(net, RTNLGRP_IPV4_MROUTE_R, -ENOBUFS); 24835a645dd8SJulien Gomes } 24845a645dd8SJulien Gomes 2485d0440029SJakub Kicinski static int ipmr_rtm_valid_getroute_req(struct sk_buff *skb, 2486d0440029SJakub Kicinski const struct nlmsghdr *nlh, 2487d0440029SJakub Kicinski struct nlattr **tb, 2488d0440029SJakub Kicinski struct netlink_ext_ack *extack) 2489d0440029SJakub Kicinski { 2490d0440029SJakub Kicinski struct rtmsg *rtm; 2491d0440029SJakub Kicinski int i, err; 2492d0440029SJakub Kicinski 2493d0440029SJakub Kicinski if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*rtm))) { 2494d0440029SJakub Kicinski NL_SET_ERR_MSG(extack, "ipv4: Invalid header for multicast route get request"); 2495d0440029SJakub Kicinski return -EINVAL; 2496d0440029SJakub Kicinski } 2497d0440029SJakub Kicinski 2498d0440029SJakub Kicinski if (!netlink_strict_get_check(skb)) 24998cb08174SJohannes Berg return nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX, 2500d0440029SJakub Kicinski rtm_ipv4_policy, extack); 2501d0440029SJakub Kicinski 2502d0440029SJakub Kicinski rtm = nlmsg_data(nlh); 2503d0440029SJakub Kicinski if ((rtm->rtm_src_len && rtm->rtm_src_len != 32) || 2504d0440029SJakub Kicinski (rtm->rtm_dst_len && rtm->rtm_dst_len != 32) || 2505d0440029SJakub Kicinski rtm->rtm_tos || rtm->rtm_table || rtm->rtm_protocol || 2506d0440029SJakub Kicinski rtm->rtm_scope || rtm->rtm_type || rtm->rtm_flags) { 2507d0440029SJakub Kicinski NL_SET_ERR_MSG(extack, "ipv4: Invalid values in header for multicast route get request"); 2508d0440029SJakub Kicinski return -EINVAL; 2509d0440029SJakub Kicinski } 2510d0440029SJakub Kicinski 25118cb08174SJohannes Berg err = nlmsg_parse_deprecated_strict(nlh, sizeof(*rtm), tb, RTA_MAX, 2512d0440029SJakub Kicinski rtm_ipv4_policy, extack); 2513d0440029SJakub Kicinski if (err) 2514d0440029SJakub Kicinski return err; 2515d0440029SJakub Kicinski 2516d0440029SJakub Kicinski if ((tb[RTA_SRC] && !rtm->rtm_src_len) || 2517d0440029SJakub Kicinski (tb[RTA_DST] && !rtm->rtm_dst_len)) { 2518d0440029SJakub Kicinski NL_SET_ERR_MSG(extack, "ipv4: rtm_src_len and rtm_dst_len must be 32 for IPv4"); 2519d0440029SJakub Kicinski return -EINVAL; 2520d0440029SJakub Kicinski } 2521d0440029SJakub Kicinski 2522d0440029SJakub Kicinski for (i = 0; i <= RTA_MAX; i++) { 2523d0440029SJakub Kicinski if (!tb[i]) 2524d0440029SJakub Kicinski continue; 2525d0440029SJakub Kicinski 2526d0440029SJakub Kicinski switch (i) { 2527d0440029SJakub Kicinski case RTA_SRC: 2528d0440029SJakub Kicinski case RTA_DST: 2529d0440029SJakub Kicinski case RTA_TABLE: 2530d0440029SJakub Kicinski break; 2531d0440029SJakub Kicinski default: 2532d0440029SJakub Kicinski NL_SET_ERR_MSG(extack, "ipv4: Unsupported attribute in multicast route get request"); 2533d0440029SJakub Kicinski return -EINVAL; 2534d0440029SJakub Kicinski } 2535d0440029SJakub Kicinski } 2536d0440029SJakub Kicinski 2537d0440029SJakub Kicinski return 0; 2538d0440029SJakub Kicinski } 2539d0440029SJakub Kicinski 25404f75ba69SDonald Sharp static int ipmr_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh, 25414f75ba69SDonald Sharp struct netlink_ext_ack *extack) 25424f75ba69SDonald Sharp { 25434f75ba69SDonald Sharp struct net *net = sock_net(in_skb->sk); 25444f75ba69SDonald Sharp struct nlattr *tb[RTA_MAX + 1]; 25454f75ba69SDonald Sharp struct sk_buff *skb = NULL; 25464f75ba69SDonald Sharp struct mfc_cache *cache; 25474f75ba69SDonald Sharp struct mr_table *mrt; 25484f75ba69SDonald Sharp __be32 src, grp; 25494f75ba69SDonald Sharp u32 tableid; 25504f75ba69SDonald Sharp int err; 25514f75ba69SDonald Sharp 2552d0440029SJakub Kicinski err = ipmr_rtm_valid_getroute_req(in_skb, nlh, tb, extack); 25534f75ba69SDonald Sharp if (err < 0) 25544f75ba69SDonald Sharp goto errout; 25554f75ba69SDonald Sharp 25564f75ba69SDonald Sharp src = tb[RTA_SRC] ? nla_get_in_addr(tb[RTA_SRC]) : 0; 25574f75ba69SDonald Sharp grp = tb[RTA_DST] ? nla_get_in_addr(tb[RTA_DST]) : 0; 25584f75ba69SDonald Sharp tableid = tb[RTA_TABLE] ? nla_get_u32(tb[RTA_TABLE]) : 0; 25594f75ba69SDonald Sharp 25604f75ba69SDonald Sharp mrt = ipmr_get_table(net, tableid ? tableid : RT_TABLE_DEFAULT); 25612e3d232eSDan Carpenter if (!mrt) { 25622e3d232eSDan Carpenter err = -ENOENT; 25634f75ba69SDonald Sharp goto errout_free; 25644f75ba69SDonald Sharp } 25654f75ba69SDonald Sharp 25664f75ba69SDonald Sharp /* entries are added/deleted only under RTNL */ 25674f75ba69SDonald Sharp rcu_read_lock(); 25684f75ba69SDonald Sharp cache = ipmr_cache_find(mrt, src, grp); 25694f75ba69SDonald Sharp rcu_read_unlock(); 25704f75ba69SDonald Sharp if (!cache) { 25714f75ba69SDonald Sharp err = -ENOENT; 25724f75ba69SDonald Sharp goto errout_free; 25734f75ba69SDonald Sharp } 25744f75ba69SDonald Sharp 25754f75ba69SDonald Sharp skb = nlmsg_new(mroute_msgsize(false, mrt->maxvif), GFP_KERNEL); 25764f75ba69SDonald Sharp if (!skb) { 25774f75ba69SDonald Sharp err = -ENOBUFS; 25784f75ba69SDonald Sharp goto errout_free; 25794f75ba69SDonald Sharp } 25804f75ba69SDonald Sharp 25814f75ba69SDonald Sharp err = ipmr_fill_mroute(mrt, skb, NETLINK_CB(in_skb).portid, 25824f75ba69SDonald Sharp nlh->nlmsg_seq, cache, 25834f75ba69SDonald Sharp RTM_NEWROUTE, 0); 25844f75ba69SDonald Sharp if (err < 0) 25854f75ba69SDonald Sharp goto errout_free; 25864f75ba69SDonald Sharp 25874f75ba69SDonald Sharp err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid); 25884f75ba69SDonald Sharp 25894f75ba69SDonald Sharp errout: 25904f75ba69SDonald Sharp return err; 25914f75ba69SDonald Sharp 25924f75ba69SDonald Sharp errout_free: 25934f75ba69SDonald Sharp kfree_skb(skb); 25944f75ba69SDonald Sharp goto errout; 25954f75ba69SDonald Sharp } 25964f75ba69SDonald Sharp 2597cb6a4e46SPatrick McHardy static int ipmr_rtm_dumproute(struct sk_buff *skb, struct netlink_callback *cb) 2598cb6a4e46SPatrick McHardy { 25994724676dSDavid Ahern struct fib_dump_filter filter = {}; 26004724676dSDavid Ahern int err; 26014724676dSDavid Ahern 2602cb167893SDavid Ahern if (cb->strict_check) { 26034724676dSDavid Ahern err = ip_valid_fib_dump_req(sock_net(skb->sk), cb->nlh, 2604effe6792SDavid Ahern &filter, cb); 2605e8ba330aSDavid Ahern if (err < 0) 2606e8ba330aSDavid Ahern return err; 2607e8ba330aSDavid Ahern } 2608e8ba330aSDavid Ahern 2609cb167893SDavid Ahern if (filter.table_id) { 2610cb167893SDavid Ahern struct mr_table *mrt; 2611cb167893SDavid Ahern 2612cb167893SDavid Ahern mrt = ipmr_get_table(sock_net(skb->sk), filter.table_id); 2613cb167893SDavid Ahern if (!mrt) { 2614ae677bbbSDavid Ahern if (filter.dump_all_families) 2615ae677bbbSDavid Ahern return skb->len; 2616ae677bbbSDavid Ahern 2617cb167893SDavid Ahern NL_SET_ERR_MSG(cb->extack, "ipv4: MR table does not exist"); 2618cb167893SDavid Ahern return -ENOENT; 2619cb167893SDavid Ahern } 2620cb167893SDavid Ahern err = mr_table_dump(mrt, skb, cb, _ipmr_fill_mroute, 2621cb167893SDavid Ahern &mfc_unres_lock, &filter); 2622cb167893SDavid Ahern return skb->len ? : err; 2623cb167893SDavid Ahern } 2624cb167893SDavid Ahern 26257b0db857SYuval Mintz return mr_rtm_dumproute(skb, cb, ipmr_mr_table_iter, 2626cb167893SDavid Ahern _ipmr_fill_mroute, &mfc_unres_lock, &filter); 2627cb6a4e46SPatrick McHardy } 2628cb6a4e46SPatrick McHardy 2629ccbb0aa6SNikolay Aleksandrov static const struct nla_policy rtm_ipmr_policy[RTA_MAX + 1] = { 2630ccbb0aa6SNikolay Aleksandrov [RTA_SRC] = { .type = NLA_U32 }, 2631ccbb0aa6SNikolay Aleksandrov [RTA_DST] = { .type = NLA_U32 }, 2632ccbb0aa6SNikolay Aleksandrov [RTA_IIF] = { .type = NLA_U32 }, 2633ccbb0aa6SNikolay Aleksandrov [RTA_TABLE] = { .type = NLA_U32 }, 2634ccbb0aa6SNikolay Aleksandrov [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) }, 2635ccbb0aa6SNikolay Aleksandrov }; 2636ccbb0aa6SNikolay Aleksandrov 2637ccbb0aa6SNikolay Aleksandrov static bool ipmr_rtm_validate_proto(unsigned char rtm_protocol) 2638ccbb0aa6SNikolay Aleksandrov { 2639ccbb0aa6SNikolay Aleksandrov switch (rtm_protocol) { 2640ccbb0aa6SNikolay Aleksandrov case RTPROT_STATIC: 2641ccbb0aa6SNikolay Aleksandrov case RTPROT_MROUTED: 2642ccbb0aa6SNikolay Aleksandrov return true; 2643ccbb0aa6SNikolay Aleksandrov } 2644ccbb0aa6SNikolay Aleksandrov return false; 2645ccbb0aa6SNikolay Aleksandrov } 2646ccbb0aa6SNikolay Aleksandrov 2647ccbb0aa6SNikolay Aleksandrov static int ipmr_nla_get_ttls(const struct nlattr *nla, struct mfcctl *mfcc) 2648ccbb0aa6SNikolay Aleksandrov { 2649ccbb0aa6SNikolay Aleksandrov struct rtnexthop *rtnh = nla_data(nla); 2650ccbb0aa6SNikolay Aleksandrov int remaining = nla_len(nla), vifi = 0; 2651ccbb0aa6SNikolay Aleksandrov 2652ccbb0aa6SNikolay Aleksandrov while (rtnh_ok(rtnh, remaining)) { 2653ccbb0aa6SNikolay Aleksandrov mfcc->mfcc_ttls[vifi] = rtnh->rtnh_hops; 2654ccbb0aa6SNikolay Aleksandrov if (++vifi == MAXVIFS) 2655ccbb0aa6SNikolay Aleksandrov break; 2656ccbb0aa6SNikolay Aleksandrov rtnh = rtnh_next(rtnh, &remaining); 2657ccbb0aa6SNikolay Aleksandrov } 2658ccbb0aa6SNikolay Aleksandrov 2659ccbb0aa6SNikolay Aleksandrov return remaining > 0 ? -EINVAL : vifi; 2660ccbb0aa6SNikolay Aleksandrov } 2661ccbb0aa6SNikolay Aleksandrov 2662ccbb0aa6SNikolay Aleksandrov /* returns < 0 on error, 0 for ADD_MFC and 1 for ADD_MFC_PROXY */ 2663ccbb0aa6SNikolay Aleksandrov static int rtm_to_ipmr_mfcc(struct net *net, struct nlmsghdr *nlh, 2664ccbb0aa6SNikolay Aleksandrov struct mfcctl *mfcc, int *mrtsock, 2665c21ef3e3SDavid Ahern struct mr_table **mrtret, 2666c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 2667ccbb0aa6SNikolay Aleksandrov { 2668ccbb0aa6SNikolay Aleksandrov struct net_device *dev = NULL; 2669ccbb0aa6SNikolay Aleksandrov u32 tblid = RT_TABLE_DEFAULT; 2670ccbb0aa6SNikolay Aleksandrov struct mr_table *mrt; 2671ccbb0aa6SNikolay Aleksandrov struct nlattr *attr; 2672ccbb0aa6SNikolay Aleksandrov struct rtmsg *rtm; 2673ccbb0aa6SNikolay Aleksandrov int ret, rem; 2674ccbb0aa6SNikolay Aleksandrov 26758cb08174SJohannes Berg ret = nlmsg_validate_deprecated(nlh, sizeof(*rtm), RTA_MAX, 26768cb08174SJohannes Berg rtm_ipmr_policy, extack); 2677ccbb0aa6SNikolay Aleksandrov if (ret < 0) 2678ccbb0aa6SNikolay Aleksandrov goto out; 2679ccbb0aa6SNikolay Aleksandrov rtm = nlmsg_data(nlh); 2680ccbb0aa6SNikolay Aleksandrov 2681ccbb0aa6SNikolay Aleksandrov ret = -EINVAL; 2682ccbb0aa6SNikolay Aleksandrov if (rtm->rtm_family != RTNL_FAMILY_IPMR || rtm->rtm_dst_len != 32 || 2683ccbb0aa6SNikolay Aleksandrov rtm->rtm_type != RTN_MULTICAST || 2684ccbb0aa6SNikolay Aleksandrov rtm->rtm_scope != RT_SCOPE_UNIVERSE || 2685ccbb0aa6SNikolay Aleksandrov !ipmr_rtm_validate_proto(rtm->rtm_protocol)) 2686ccbb0aa6SNikolay Aleksandrov goto out; 2687ccbb0aa6SNikolay Aleksandrov 2688ccbb0aa6SNikolay Aleksandrov memset(mfcc, 0, sizeof(*mfcc)); 2689ccbb0aa6SNikolay Aleksandrov mfcc->mfcc_parent = -1; 2690ccbb0aa6SNikolay Aleksandrov ret = 0; 2691ccbb0aa6SNikolay Aleksandrov nlmsg_for_each_attr(attr, nlh, sizeof(struct rtmsg), rem) { 2692ccbb0aa6SNikolay Aleksandrov switch (nla_type(attr)) { 2693ccbb0aa6SNikolay Aleksandrov case RTA_SRC: 2694ccbb0aa6SNikolay Aleksandrov mfcc->mfcc_origin.s_addr = nla_get_be32(attr); 2695ccbb0aa6SNikolay Aleksandrov break; 2696ccbb0aa6SNikolay Aleksandrov case RTA_DST: 2697ccbb0aa6SNikolay Aleksandrov mfcc->mfcc_mcastgrp.s_addr = nla_get_be32(attr); 2698ccbb0aa6SNikolay Aleksandrov break; 2699ccbb0aa6SNikolay Aleksandrov case RTA_IIF: 2700ccbb0aa6SNikolay Aleksandrov dev = __dev_get_by_index(net, nla_get_u32(attr)); 2701ccbb0aa6SNikolay Aleksandrov if (!dev) { 2702ccbb0aa6SNikolay Aleksandrov ret = -ENODEV; 2703ccbb0aa6SNikolay Aleksandrov goto out; 2704ccbb0aa6SNikolay Aleksandrov } 2705ccbb0aa6SNikolay Aleksandrov break; 2706ccbb0aa6SNikolay Aleksandrov case RTA_MULTIPATH: 2707ccbb0aa6SNikolay Aleksandrov if (ipmr_nla_get_ttls(attr, mfcc) < 0) { 2708ccbb0aa6SNikolay Aleksandrov ret = -EINVAL; 2709ccbb0aa6SNikolay Aleksandrov goto out; 2710ccbb0aa6SNikolay Aleksandrov } 2711ccbb0aa6SNikolay Aleksandrov break; 2712ccbb0aa6SNikolay Aleksandrov case RTA_PREFSRC: 2713ccbb0aa6SNikolay Aleksandrov ret = 1; 2714ccbb0aa6SNikolay Aleksandrov break; 2715ccbb0aa6SNikolay Aleksandrov case RTA_TABLE: 2716ccbb0aa6SNikolay Aleksandrov tblid = nla_get_u32(attr); 2717ccbb0aa6SNikolay Aleksandrov break; 2718ccbb0aa6SNikolay Aleksandrov } 2719ccbb0aa6SNikolay Aleksandrov } 2720ccbb0aa6SNikolay Aleksandrov mrt = ipmr_get_table(net, tblid); 2721ccbb0aa6SNikolay Aleksandrov if (!mrt) { 2722ccbb0aa6SNikolay Aleksandrov ret = -ENOENT; 2723ccbb0aa6SNikolay Aleksandrov goto out; 2724ccbb0aa6SNikolay Aleksandrov } 2725ccbb0aa6SNikolay Aleksandrov *mrtret = mrt; 2726ccbb0aa6SNikolay Aleksandrov *mrtsock = rtm->rtm_protocol == RTPROT_MROUTED ? 1 : 0; 2727ccbb0aa6SNikolay Aleksandrov if (dev) 2728ccbb0aa6SNikolay Aleksandrov mfcc->mfcc_parent = ipmr_find_vif(mrt, dev); 2729ccbb0aa6SNikolay Aleksandrov 2730ccbb0aa6SNikolay Aleksandrov out: 2731ccbb0aa6SNikolay Aleksandrov return ret; 2732ccbb0aa6SNikolay Aleksandrov } 2733ccbb0aa6SNikolay Aleksandrov 2734ccbb0aa6SNikolay Aleksandrov /* takes care of both newroute and delroute */ 2735c21ef3e3SDavid Ahern static int ipmr_rtm_route(struct sk_buff *skb, struct nlmsghdr *nlh, 2736c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 2737ccbb0aa6SNikolay Aleksandrov { 2738ccbb0aa6SNikolay Aleksandrov struct net *net = sock_net(skb->sk); 2739ccbb0aa6SNikolay Aleksandrov int ret, mrtsock, parent; 2740ccbb0aa6SNikolay Aleksandrov struct mr_table *tbl; 2741ccbb0aa6SNikolay Aleksandrov struct mfcctl mfcc; 2742ccbb0aa6SNikolay Aleksandrov 2743ccbb0aa6SNikolay Aleksandrov mrtsock = 0; 2744ccbb0aa6SNikolay Aleksandrov tbl = NULL; 2745c21ef3e3SDavid Ahern ret = rtm_to_ipmr_mfcc(net, nlh, &mfcc, &mrtsock, &tbl, extack); 2746ccbb0aa6SNikolay Aleksandrov if (ret < 0) 2747ccbb0aa6SNikolay Aleksandrov return ret; 2748ccbb0aa6SNikolay Aleksandrov 2749ccbb0aa6SNikolay Aleksandrov parent = ret ? mfcc.mfcc_parent : -1; 2750ccbb0aa6SNikolay Aleksandrov if (nlh->nlmsg_type == RTM_NEWROUTE) 2751ccbb0aa6SNikolay Aleksandrov return ipmr_mfc_add(net, tbl, &mfcc, mrtsock, parent); 2752ccbb0aa6SNikolay Aleksandrov else 2753ccbb0aa6SNikolay Aleksandrov return ipmr_mfc_delete(tbl, &mfcc, parent); 2754ccbb0aa6SNikolay Aleksandrov } 2755ccbb0aa6SNikolay Aleksandrov 2756772c344dSNikolay Aleksandrov static bool ipmr_fill_table(struct mr_table *mrt, struct sk_buff *skb) 2757772c344dSNikolay Aleksandrov { 2758772c344dSNikolay Aleksandrov u32 queue_len = atomic_read(&mrt->cache_resolve_queue_len); 2759772c344dSNikolay Aleksandrov 2760772c344dSNikolay Aleksandrov if (nla_put_u32(skb, IPMRA_TABLE_ID, mrt->id) || 2761772c344dSNikolay Aleksandrov nla_put_u32(skb, IPMRA_TABLE_CACHE_RES_QUEUE_LEN, queue_len) || 2762772c344dSNikolay Aleksandrov nla_put_s32(skb, IPMRA_TABLE_MROUTE_REG_VIF_NUM, 2763772c344dSNikolay Aleksandrov mrt->mroute_reg_vif_num) || 2764772c344dSNikolay Aleksandrov nla_put_u8(skb, IPMRA_TABLE_MROUTE_DO_ASSERT, 2765772c344dSNikolay Aleksandrov mrt->mroute_do_assert) || 2766c921c207SNikolay Aleksandrov nla_put_u8(skb, IPMRA_TABLE_MROUTE_DO_PIM, mrt->mroute_do_pim) || 2767c921c207SNikolay Aleksandrov nla_put_u8(skb, IPMRA_TABLE_MROUTE_DO_WRVIFWHOLE, 2768c921c207SNikolay Aleksandrov mrt->mroute_do_wrvifwhole)) 2769772c344dSNikolay Aleksandrov return false; 2770772c344dSNikolay Aleksandrov 2771772c344dSNikolay Aleksandrov return true; 2772772c344dSNikolay Aleksandrov } 2773772c344dSNikolay Aleksandrov 2774772c344dSNikolay Aleksandrov static bool ipmr_fill_vif(struct mr_table *mrt, u32 vifid, struct sk_buff *skb) 2775772c344dSNikolay Aleksandrov { 2776772c344dSNikolay Aleksandrov struct nlattr *vif_nest; 2777772c344dSNikolay Aleksandrov struct vif_device *vif; 2778772c344dSNikolay Aleksandrov 2779772c344dSNikolay Aleksandrov /* if the VIF doesn't exist just continue */ 2780772c344dSNikolay Aleksandrov if (!VIF_EXISTS(mrt, vifid)) 2781772c344dSNikolay Aleksandrov return true; 2782772c344dSNikolay Aleksandrov 2783772c344dSNikolay Aleksandrov vif = &mrt->vif_table[vifid]; 2784ae0be8deSMichal Kubecek vif_nest = nla_nest_start_noflag(skb, IPMRA_VIF); 2785772c344dSNikolay Aleksandrov if (!vif_nest) 2786772c344dSNikolay Aleksandrov return false; 2787772c344dSNikolay Aleksandrov if (nla_put_u32(skb, IPMRA_VIFA_IFINDEX, vif->dev->ifindex) || 2788772c344dSNikolay Aleksandrov nla_put_u32(skb, IPMRA_VIFA_VIF_ID, vifid) || 2789772c344dSNikolay Aleksandrov nla_put_u16(skb, IPMRA_VIFA_FLAGS, vif->flags) || 2790772c344dSNikolay Aleksandrov nla_put_u64_64bit(skb, IPMRA_VIFA_BYTES_IN, vif->bytes_in, 2791772c344dSNikolay Aleksandrov IPMRA_VIFA_PAD) || 2792772c344dSNikolay Aleksandrov nla_put_u64_64bit(skb, IPMRA_VIFA_BYTES_OUT, vif->bytes_out, 2793772c344dSNikolay Aleksandrov IPMRA_VIFA_PAD) || 2794772c344dSNikolay Aleksandrov nla_put_u64_64bit(skb, IPMRA_VIFA_PACKETS_IN, vif->pkt_in, 2795772c344dSNikolay Aleksandrov IPMRA_VIFA_PAD) || 2796772c344dSNikolay Aleksandrov nla_put_u64_64bit(skb, IPMRA_VIFA_PACKETS_OUT, vif->pkt_out, 2797772c344dSNikolay Aleksandrov IPMRA_VIFA_PAD) || 2798772c344dSNikolay Aleksandrov nla_put_be32(skb, IPMRA_VIFA_LOCAL_ADDR, vif->local) || 2799772c344dSNikolay Aleksandrov nla_put_be32(skb, IPMRA_VIFA_REMOTE_ADDR, vif->remote)) { 2800772c344dSNikolay Aleksandrov nla_nest_cancel(skb, vif_nest); 2801772c344dSNikolay Aleksandrov return false; 2802772c344dSNikolay Aleksandrov } 2803772c344dSNikolay Aleksandrov nla_nest_end(skb, vif_nest); 2804772c344dSNikolay Aleksandrov 2805772c344dSNikolay Aleksandrov return true; 2806772c344dSNikolay Aleksandrov } 2807772c344dSNikolay Aleksandrov 280814fc5bb2SDavid Ahern static int ipmr_valid_dumplink(const struct nlmsghdr *nlh, 280914fc5bb2SDavid Ahern struct netlink_ext_ack *extack) 281014fc5bb2SDavid Ahern { 281114fc5bb2SDavid Ahern struct ifinfomsg *ifm; 281214fc5bb2SDavid Ahern 281314fc5bb2SDavid Ahern if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) { 281414fc5bb2SDavid Ahern NL_SET_ERR_MSG(extack, "ipv4: Invalid header for ipmr link dump"); 281514fc5bb2SDavid Ahern return -EINVAL; 281614fc5bb2SDavid Ahern } 281714fc5bb2SDavid Ahern 281814fc5bb2SDavid Ahern if (nlmsg_attrlen(nlh, sizeof(*ifm))) { 281914fc5bb2SDavid Ahern NL_SET_ERR_MSG(extack, "Invalid data after header in ipmr link dump"); 282014fc5bb2SDavid Ahern return -EINVAL; 282114fc5bb2SDavid Ahern } 282214fc5bb2SDavid Ahern 282314fc5bb2SDavid Ahern ifm = nlmsg_data(nlh); 282414fc5bb2SDavid Ahern if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags || 282514fc5bb2SDavid Ahern ifm->ifi_change || ifm->ifi_index) { 282614fc5bb2SDavid Ahern NL_SET_ERR_MSG(extack, "Invalid values in header for ipmr link dump request"); 282714fc5bb2SDavid Ahern return -EINVAL; 282814fc5bb2SDavid Ahern } 282914fc5bb2SDavid Ahern 283014fc5bb2SDavid Ahern return 0; 283114fc5bb2SDavid Ahern } 283214fc5bb2SDavid Ahern 2833772c344dSNikolay Aleksandrov static int ipmr_rtm_dumplink(struct sk_buff *skb, struct netlink_callback *cb) 2834772c344dSNikolay Aleksandrov { 2835772c344dSNikolay Aleksandrov struct net *net = sock_net(skb->sk); 2836772c344dSNikolay Aleksandrov struct nlmsghdr *nlh = NULL; 2837772c344dSNikolay Aleksandrov unsigned int t = 0, s_t; 2838772c344dSNikolay Aleksandrov unsigned int e = 0, s_e; 2839772c344dSNikolay Aleksandrov struct mr_table *mrt; 2840772c344dSNikolay Aleksandrov 284114fc5bb2SDavid Ahern if (cb->strict_check) { 284214fc5bb2SDavid Ahern int err = ipmr_valid_dumplink(cb->nlh, cb->extack); 284314fc5bb2SDavid Ahern 284414fc5bb2SDavid Ahern if (err < 0) 284514fc5bb2SDavid Ahern return err; 284614fc5bb2SDavid Ahern } 284714fc5bb2SDavid Ahern 2848772c344dSNikolay Aleksandrov s_t = cb->args[0]; 2849772c344dSNikolay Aleksandrov s_e = cb->args[1]; 2850772c344dSNikolay Aleksandrov 2851772c344dSNikolay Aleksandrov ipmr_for_each_table(mrt, net) { 2852772c344dSNikolay Aleksandrov struct nlattr *vifs, *af; 2853772c344dSNikolay Aleksandrov struct ifinfomsg *hdr; 2854772c344dSNikolay Aleksandrov u32 i; 2855772c344dSNikolay Aleksandrov 2856772c344dSNikolay Aleksandrov if (t < s_t) 2857772c344dSNikolay Aleksandrov goto skip_table; 2858772c344dSNikolay Aleksandrov nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, 2859772c344dSNikolay Aleksandrov cb->nlh->nlmsg_seq, RTM_NEWLINK, 2860772c344dSNikolay Aleksandrov sizeof(*hdr), NLM_F_MULTI); 2861772c344dSNikolay Aleksandrov if (!nlh) 2862772c344dSNikolay Aleksandrov break; 2863772c344dSNikolay Aleksandrov 2864772c344dSNikolay Aleksandrov hdr = nlmsg_data(nlh); 2865772c344dSNikolay Aleksandrov memset(hdr, 0, sizeof(*hdr)); 2866772c344dSNikolay Aleksandrov hdr->ifi_family = RTNL_FAMILY_IPMR; 2867772c344dSNikolay Aleksandrov 2868ae0be8deSMichal Kubecek af = nla_nest_start_noflag(skb, IFLA_AF_SPEC); 2869772c344dSNikolay Aleksandrov if (!af) { 2870772c344dSNikolay Aleksandrov nlmsg_cancel(skb, nlh); 2871772c344dSNikolay Aleksandrov goto out; 2872772c344dSNikolay Aleksandrov } 2873772c344dSNikolay Aleksandrov 2874772c344dSNikolay Aleksandrov if (!ipmr_fill_table(mrt, skb)) { 2875772c344dSNikolay Aleksandrov nlmsg_cancel(skb, nlh); 2876772c344dSNikolay Aleksandrov goto out; 2877772c344dSNikolay Aleksandrov } 2878772c344dSNikolay Aleksandrov 2879ae0be8deSMichal Kubecek vifs = nla_nest_start_noflag(skb, IPMRA_TABLE_VIFS); 2880772c344dSNikolay Aleksandrov if (!vifs) { 2881772c344dSNikolay Aleksandrov nla_nest_end(skb, af); 2882772c344dSNikolay Aleksandrov nlmsg_end(skb, nlh); 2883772c344dSNikolay Aleksandrov goto out; 2884772c344dSNikolay Aleksandrov } 2885772c344dSNikolay Aleksandrov for (i = 0; i < mrt->maxvif; i++) { 2886772c344dSNikolay Aleksandrov if (e < s_e) 2887772c344dSNikolay Aleksandrov goto skip_entry; 2888772c344dSNikolay Aleksandrov if (!ipmr_fill_vif(mrt, i, skb)) { 2889772c344dSNikolay Aleksandrov nla_nest_end(skb, vifs); 2890772c344dSNikolay Aleksandrov nla_nest_end(skb, af); 2891772c344dSNikolay Aleksandrov nlmsg_end(skb, nlh); 2892772c344dSNikolay Aleksandrov goto out; 2893772c344dSNikolay Aleksandrov } 2894772c344dSNikolay Aleksandrov skip_entry: 2895772c344dSNikolay Aleksandrov e++; 2896772c344dSNikolay Aleksandrov } 2897772c344dSNikolay Aleksandrov s_e = 0; 2898772c344dSNikolay Aleksandrov e = 0; 2899772c344dSNikolay Aleksandrov nla_nest_end(skb, vifs); 2900772c344dSNikolay Aleksandrov nla_nest_end(skb, af); 2901772c344dSNikolay Aleksandrov nlmsg_end(skb, nlh); 2902772c344dSNikolay Aleksandrov skip_table: 2903772c344dSNikolay Aleksandrov t++; 2904772c344dSNikolay Aleksandrov } 2905772c344dSNikolay Aleksandrov 2906772c344dSNikolay Aleksandrov out: 2907772c344dSNikolay Aleksandrov cb->args[1] = e; 2908772c344dSNikolay Aleksandrov cb->args[0] = t; 2909772c344dSNikolay Aleksandrov 2910772c344dSNikolay Aleksandrov return skb->len; 2911772c344dSNikolay Aleksandrov } 2912772c344dSNikolay Aleksandrov 29131da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 29147ef8f65dSNikolay Aleksandrov /* The /proc interfaces to multicast routing : 2915a8cb16ddSEric Dumazet * /proc/net/ip_mr_cache & /proc/net/ip_mr_vif 29161da177e4SLinus Torvalds */ 29171da177e4SLinus Torvalds 29181da177e4SLinus Torvalds static void *ipmr_vif_seq_start(struct seq_file *seq, loff_t *pos) 2919ba93ef74SStephen Hemminger __acquires(mrt_lock) 29201da177e4SLinus Torvalds { 29213feda6b4SYuval Mintz struct mr_vif_iter *iter = seq->private; 2922f6bb4514SBenjamin Thery struct net *net = seq_file_net(seq); 2923f0ad0860SPatrick McHardy struct mr_table *mrt; 2924f0ad0860SPatrick McHardy 2925f0ad0860SPatrick McHardy mrt = ipmr_get_table(net, RT_TABLE_DEFAULT); 292651456b29SIan Morris if (!mrt) 2927f0ad0860SPatrick McHardy return ERR_PTR(-ENOENT); 2928f0ad0860SPatrick McHardy 2929f0ad0860SPatrick McHardy iter->mrt = mrt; 2930f6bb4514SBenjamin Thery 29311da177e4SLinus Torvalds read_lock(&mrt_lock); 29323feda6b4SYuval Mintz return mr_vif_seq_start(seq, pos); 29331da177e4SLinus Torvalds } 29341da177e4SLinus Torvalds 29351da177e4SLinus Torvalds static void ipmr_vif_seq_stop(struct seq_file *seq, void *v) 2936ba93ef74SStephen Hemminger __releases(mrt_lock) 29371da177e4SLinus Torvalds { 29381da177e4SLinus Torvalds read_unlock(&mrt_lock); 29391da177e4SLinus Torvalds } 29401da177e4SLinus Torvalds 29411da177e4SLinus Torvalds static int ipmr_vif_seq_show(struct seq_file *seq, void *v) 29421da177e4SLinus Torvalds { 29433feda6b4SYuval Mintz struct mr_vif_iter *iter = seq->private; 2944f0ad0860SPatrick McHardy struct mr_table *mrt = iter->mrt; 2945f6bb4514SBenjamin Thery 29461da177e4SLinus Torvalds if (v == SEQ_START_TOKEN) { 29471da177e4SLinus Torvalds seq_puts(seq, 29481da177e4SLinus Torvalds "Interface BytesIn PktsIn BytesOut PktsOut Flags Local Remote\n"); 29491da177e4SLinus Torvalds } else { 29501da177e4SLinus Torvalds const struct vif_device *vif = v; 29516853f21fSYuval Mintz const char *name = vif->dev ? 29526853f21fSYuval Mintz vif->dev->name : "none"; 29531da177e4SLinus Torvalds 29541da177e4SLinus Torvalds seq_printf(seq, 295591e6dd82SJames Hogan "%2td %-10s %8ld %7ld %8ld %7ld %05X %08X %08X\n", 29560c12295aSPatrick McHardy vif - mrt->vif_table, 29571da177e4SLinus Torvalds name, vif->bytes_in, vif->pkt_in, 29581da177e4SLinus Torvalds vif->bytes_out, vif->pkt_out, 29591da177e4SLinus Torvalds vif->flags, vif->local, vif->remote); 29601da177e4SLinus Torvalds } 29611da177e4SLinus Torvalds return 0; 29621da177e4SLinus Torvalds } 29631da177e4SLinus Torvalds 2964f690808eSStephen Hemminger static const struct seq_operations ipmr_vif_seq_ops = { 29651da177e4SLinus Torvalds .start = ipmr_vif_seq_start, 29663feda6b4SYuval Mintz .next = mr_vif_seq_next, 29671da177e4SLinus Torvalds .stop = ipmr_vif_seq_stop, 29681da177e4SLinus Torvalds .show = ipmr_vif_seq_show, 29691da177e4SLinus Torvalds }; 29701da177e4SLinus Torvalds 29711da177e4SLinus Torvalds static void *ipmr_mfc_seq_start(struct seq_file *seq, loff_t *pos) 29721da177e4SLinus Torvalds { 2973f6bb4514SBenjamin Thery struct net *net = seq_file_net(seq); 2974f0ad0860SPatrick McHardy struct mr_table *mrt; 2975f6bb4514SBenjamin Thery 2976f0ad0860SPatrick McHardy mrt = ipmr_get_table(net, RT_TABLE_DEFAULT); 297751456b29SIan Morris if (!mrt) 2978f0ad0860SPatrick McHardy return ERR_PTR(-ENOENT); 2979f0ad0860SPatrick McHardy 2980c8d61968SYuval Mintz return mr_mfc_seq_start(seq, pos, mrt, &mfc_unres_lock); 29811da177e4SLinus Torvalds } 29821da177e4SLinus Torvalds 29831da177e4SLinus Torvalds static int ipmr_mfc_seq_show(struct seq_file *seq, void *v) 29841da177e4SLinus Torvalds { 29851da177e4SLinus Torvalds int n; 29861da177e4SLinus Torvalds 29871da177e4SLinus Torvalds if (v == SEQ_START_TOKEN) { 29881da177e4SLinus Torvalds seq_puts(seq, 29891da177e4SLinus Torvalds "Group Origin Iif Pkts Bytes Wrong Oifs\n"); 29901da177e4SLinus Torvalds } else { 29911da177e4SLinus Torvalds const struct mfc_cache *mfc = v; 2992c8d61968SYuval Mintz const struct mr_mfc_iter *it = seq->private; 2993f0ad0860SPatrick McHardy const struct mr_table *mrt = it->mrt; 29941da177e4SLinus Torvalds 29950eae88f3SEric Dumazet seq_printf(seq, "%08X %08X %-3hd", 29960eae88f3SEric Dumazet (__force u32) mfc->mfc_mcastgrp, 29970eae88f3SEric Dumazet (__force u32) mfc->mfc_origin, 2998494fff56SYuval Mintz mfc->_c.mfc_parent); 29991ea472e2SBenjamin Thery 30000c12295aSPatrick McHardy if (it->cache != &mrt->mfc_unres_queue) { 30011ea472e2SBenjamin Thery seq_printf(seq, " %8lu %8lu %8lu", 3002494fff56SYuval Mintz mfc->_c.mfc_un.res.pkt, 3003494fff56SYuval Mintz mfc->_c.mfc_un.res.bytes, 3004494fff56SYuval Mintz mfc->_c.mfc_un.res.wrong_if); 3005494fff56SYuval Mintz for (n = mfc->_c.mfc_un.res.minvif; 3006494fff56SYuval Mintz n < mfc->_c.mfc_un.res.maxvif; n++) { 30070c12295aSPatrick McHardy if (VIF_EXISTS(mrt, n) && 3008494fff56SYuval Mintz mfc->_c.mfc_un.res.ttls[n] < 255) 30091da177e4SLinus Torvalds seq_printf(seq, 30101da177e4SLinus Torvalds " %2d:%-3d", 3011494fff56SYuval Mintz n, mfc->_c.mfc_un.res.ttls[n]); 30121da177e4SLinus Torvalds } 30131ea472e2SBenjamin Thery } else { 30141ea472e2SBenjamin Thery /* unresolved mfc_caches don't contain 30151ea472e2SBenjamin Thery * pkt, bytes and wrong_if values 30161ea472e2SBenjamin Thery */ 30171ea472e2SBenjamin Thery seq_printf(seq, " %8lu %8lu %8lu", 0ul, 0ul, 0ul); 30181da177e4SLinus Torvalds } 30191da177e4SLinus Torvalds seq_putc(seq, '\n'); 30201da177e4SLinus Torvalds } 30211da177e4SLinus Torvalds return 0; 30221da177e4SLinus Torvalds } 30231da177e4SLinus Torvalds 3024f690808eSStephen Hemminger static const struct seq_operations ipmr_mfc_seq_ops = { 30251da177e4SLinus Torvalds .start = ipmr_mfc_seq_start, 3026c8d61968SYuval Mintz .next = mr_mfc_seq_next, 3027c8d61968SYuval Mintz .stop = mr_mfc_seq_stop, 30281da177e4SLinus Torvalds .show = ipmr_mfc_seq_show, 30291da177e4SLinus Torvalds }; 30301da177e4SLinus Torvalds #endif 30311da177e4SLinus Torvalds 30321da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM_V2 303332613090SAlexey Dobriyan static const struct net_protocol pim_protocol = { 30341da177e4SLinus Torvalds .handler = pim_rcv, 3035403dbb97STom Goff .netns_ok = 1, 30361da177e4SLinus Torvalds }; 30371da177e4SLinus Torvalds #endif 30381da177e4SLinus Torvalds 30394d65b948SYotam Gigi static unsigned int ipmr_seq_read(struct net *net) 30404d65b948SYotam Gigi { 30414d65b948SYotam Gigi ASSERT_RTNL(); 30424d65b948SYotam Gigi 30434d65b948SYotam Gigi return net->ipv4.ipmr_seq + ipmr_rules_seq_read(net); 30444d65b948SYotam Gigi } 30454d65b948SYotam Gigi 3046b7a59557SJiri Pirko static int ipmr_dump(struct net *net, struct notifier_block *nb, 3047b7a59557SJiri Pirko struct netlink_ext_ack *extack) 30484d65b948SYotam Gigi { 3049cdc9f944SYuval Mintz return mr_dump(net, nb, RTNL_FAMILY_IPMR, ipmr_rules_dump, 3050b7a59557SJiri Pirko ipmr_mr_table_iter, &mrt_lock, extack); 30514d65b948SYotam Gigi } 30524d65b948SYotam Gigi 30534d65b948SYotam Gigi static const struct fib_notifier_ops ipmr_notifier_ops_template = { 30544d65b948SYotam Gigi .family = RTNL_FAMILY_IPMR, 30554d65b948SYotam Gigi .fib_seq_read = ipmr_seq_read, 30564d65b948SYotam Gigi .fib_dump = ipmr_dump, 30574d65b948SYotam Gigi .owner = THIS_MODULE, 30584d65b948SYotam Gigi }; 30594d65b948SYotam Gigi 3060ef739d8aSColin Ian King static int __net_init ipmr_notifier_init(struct net *net) 30614d65b948SYotam Gigi { 30624d65b948SYotam Gigi struct fib_notifier_ops *ops; 30634d65b948SYotam Gigi 30644d65b948SYotam Gigi net->ipv4.ipmr_seq = 0; 30654d65b948SYotam Gigi 30664d65b948SYotam Gigi ops = fib_notifier_ops_register(&ipmr_notifier_ops_template, net); 30674d65b948SYotam Gigi if (IS_ERR(ops)) 30684d65b948SYotam Gigi return PTR_ERR(ops); 30694d65b948SYotam Gigi net->ipv4.ipmr_notifier_ops = ops; 30704d65b948SYotam Gigi 30714d65b948SYotam Gigi return 0; 30724d65b948SYotam Gigi } 30734d65b948SYotam Gigi 30744d65b948SYotam Gigi static void __net_exit ipmr_notifier_exit(struct net *net) 30754d65b948SYotam Gigi { 30764d65b948SYotam Gigi fib_notifier_ops_unregister(net->ipv4.ipmr_notifier_ops); 30774d65b948SYotam Gigi net->ipv4.ipmr_notifier_ops = NULL; 30784d65b948SYotam Gigi } 30794d65b948SYotam Gigi 30807ef8f65dSNikolay Aleksandrov /* Setup for IP multicast routing */ 3081cf958ae3SBenjamin Thery static int __net_init ipmr_net_init(struct net *net) 3082cf958ae3SBenjamin Thery { 3083f0ad0860SPatrick McHardy int err; 3084cf958ae3SBenjamin Thery 30854d65b948SYotam Gigi err = ipmr_notifier_init(net); 30864d65b948SYotam Gigi if (err) 30874d65b948SYotam Gigi goto ipmr_notifier_fail; 30884d65b948SYotam Gigi 3089f0ad0860SPatrick McHardy err = ipmr_rules_init(net); 3090f0ad0860SPatrick McHardy if (err < 0) 30914d65b948SYotam Gigi goto ipmr_rules_fail; 3092f6bb4514SBenjamin Thery 3093f6bb4514SBenjamin Thery #ifdef CONFIG_PROC_FS 3094f6bb4514SBenjamin Thery err = -ENOMEM; 3095c3506372SChristoph Hellwig if (!proc_create_net("ip_mr_vif", 0, net->proc_net, &ipmr_vif_seq_ops, 3096c3506372SChristoph Hellwig sizeof(struct mr_vif_iter))) 3097f6bb4514SBenjamin Thery goto proc_vif_fail; 3098c3506372SChristoph Hellwig if (!proc_create_net("ip_mr_cache", 0, net->proc_net, &ipmr_mfc_seq_ops, 3099c3506372SChristoph Hellwig sizeof(struct mr_mfc_iter))) 3100f6bb4514SBenjamin Thery goto proc_cache_fail; 3101f6bb4514SBenjamin Thery #endif 31022bb8b26cSBenjamin Thery return 0; 31032bb8b26cSBenjamin Thery 3104f6bb4514SBenjamin Thery #ifdef CONFIG_PROC_FS 3105f6bb4514SBenjamin Thery proc_cache_fail: 3106ece31ffdSGao feng remove_proc_entry("ip_mr_vif", net->proc_net); 3107f6bb4514SBenjamin Thery proc_vif_fail: 3108f0ad0860SPatrick McHardy ipmr_rules_exit(net); 3109f6bb4514SBenjamin Thery #endif 31104d65b948SYotam Gigi ipmr_rules_fail: 31114d65b948SYotam Gigi ipmr_notifier_exit(net); 31124d65b948SYotam Gigi ipmr_notifier_fail: 3113cf958ae3SBenjamin Thery return err; 3114cf958ae3SBenjamin Thery } 3115cf958ae3SBenjamin Thery 3116cf958ae3SBenjamin Thery static void __net_exit ipmr_net_exit(struct net *net) 3117cf958ae3SBenjamin Thery { 3118f6bb4514SBenjamin Thery #ifdef CONFIG_PROC_FS 3119ece31ffdSGao feng remove_proc_entry("ip_mr_cache", net->proc_net); 3120ece31ffdSGao feng remove_proc_entry("ip_mr_vif", net->proc_net); 3121f6bb4514SBenjamin Thery #endif 31224d65b948SYotam Gigi ipmr_notifier_exit(net); 3123f0ad0860SPatrick McHardy ipmr_rules_exit(net); 3124cf958ae3SBenjamin Thery } 3125cf958ae3SBenjamin Thery 3126cf958ae3SBenjamin Thery static struct pernet_operations ipmr_net_ops = { 3127cf958ae3SBenjamin Thery .init = ipmr_net_init, 3128cf958ae3SBenjamin Thery .exit = ipmr_net_exit, 3129cf958ae3SBenjamin Thery }; 31301da177e4SLinus Torvalds 313103d2f897SWang Chen int __init ip_mr_init(void) 31321da177e4SLinus Torvalds { 313303d2f897SWang Chen int err; 313403d2f897SWang Chen 31351da177e4SLinus Torvalds mrt_cachep = kmem_cache_create("ip_mrt_cache", 31361da177e4SLinus Torvalds sizeof(struct mfc_cache), 3137e5d679f3SAlexey Dobriyan 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, 313820c2df83SPaul Mundt NULL); 313903d2f897SWang Chen 3140cf958ae3SBenjamin Thery err = register_pernet_subsys(&ipmr_net_ops); 3141cf958ae3SBenjamin Thery if (err) 3142cf958ae3SBenjamin Thery goto reg_pernet_fail; 3143cf958ae3SBenjamin Thery 314403d2f897SWang Chen err = register_netdevice_notifier(&ip_mr_notifier); 314503d2f897SWang Chen if (err) 314603d2f897SWang Chen goto reg_notif_fail; 3147403dbb97STom Goff #ifdef CONFIG_IP_PIMSM_V2 3148403dbb97STom Goff if (inet_add_protocol(&pim_protocol, IPPROTO_PIM) < 0) { 3149058bd4d2SJoe Perches pr_err("%s: can't add PIM protocol\n", __func__); 3150403dbb97STom Goff err = -EAGAIN; 3151403dbb97STom Goff goto add_proto_fail; 3152403dbb97STom Goff } 3153403dbb97STom Goff #endif 3154c7ac8679SGreg Rose rtnl_register(RTNL_FAMILY_IPMR, RTM_GETROUTE, 3155b97bac64SFlorian Westphal ipmr_rtm_getroute, ipmr_rtm_dumproute, 0); 3156ccbb0aa6SNikolay Aleksandrov rtnl_register(RTNL_FAMILY_IPMR, RTM_NEWROUTE, 3157b97bac64SFlorian Westphal ipmr_rtm_route, NULL, 0); 3158ccbb0aa6SNikolay Aleksandrov rtnl_register(RTNL_FAMILY_IPMR, RTM_DELROUTE, 3159b97bac64SFlorian Westphal ipmr_rtm_route, NULL, 0); 3160772c344dSNikolay Aleksandrov 3161772c344dSNikolay Aleksandrov rtnl_register(RTNL_FAMILY_IPMR, RTM_GETLINK, 3162b97bac64SFlorian Westphal NULL, ipmr_rtm_dumplink, 0); 316303d2f897SWang Chen return 0; 3164f6bb4514SBenjamin Thery 3165403dbb97STom Goff #ifdef CONFIG_IP_PIMSM_V2 3166403dbb97STom Goff add_proto_fail: 3167403dbb97STom Goff unregister_netdevice_notifier(&ip_mr_notifier); 3168403dbb97STom Goff #endif 3169c3e38896SBenjamin Thery reg_notif_fail: 3170cf958ae3SBenjamin Thery unregister_pernet_subsys(&ipmr_net_ops); 3171cf958ae3SBenjamin Thery reg_pernet_fail: 3172c3e38896SBenjamin Thery kmem_cache_destroy(mrt_cachep); 317303d2f897SWang Chen return err; 31741da177e4SLinus Torvalds } 3175