11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * IP multicast routing support for mrouted 3.6/3.8 31da177e4SLinus Torvalds * 4113aa838SAlan Cox * (c) 1995 Alan Cox, <alan@lxorguk.ukuu.org.uk> 51da177e4SLinus Torvalds * Linux Consultancy and Custom Driver Development 61da177e4SLinus Torvalds * 71da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or 81da177e4SLinus Torvalds * modify it under the terms of the GNU General Public License 91da177e4SLinus Torvalds * as published by the Free Software Foundation; either version 101da177e4SLinus Torvalds * 2 of the License, or (at your option) any later version. 111da177e4SLinus Torvalds * 121da177e4SLinus Torvalds * Fixes: 131da177e4SLinus Torvalds * Michael Chastain : Incorrect size of copying. 141da177e4SLinus Torvalds * Alan Cox : Added the cache manager code 151da177e4SLinus Torvalds * Alan Cox : Fixed the clone/copy bug and device race. 161da177e4SLinus Torvalds * Mike McLagan : Routing by source 171da177e4SLinus Torvalds * Malcolm Beattie : Buffer handling fixes. 181da177e4SLinus Torvalds * Alexey Kuznetsov : Double buffer free and other fixes. 191da177e4SLinus Torvalds * SVR Anand : Fixed several multicast bugs and problems. 201da177e4SLinus Torvalds * Alexey Kuznetsov : Status, optimisations and more. 211da177e4SLinus Torvalds * Brad Parker : Better behaviour on mrouted upcall 221da177e4SLinus Torvalds * overflow. 231da177e4SLinus Torvalds * Carlos Picoto : PIMv1 Support 241da177e4SLinus Torvalds * Pavlin Ivanov Radoslavov: PIMv2 Registers must checksum only PIM header 25f77f13e2SGilles Espinasse * Relax this requirement to work with older peers. 261da177e4SLinus Torvalds * 271da177e4SLinus Torvalds */ 281da177e4SLinus Torvalds 297c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 301da177e4SLinus Torvalds #include <linux/types.h> 314fc268d2SRandy Dunlap #include <linux/capability.h> 321da177e4SLinus Torvalds #include <linux/errno.h> 331da177e4SLinus Torvalds #include <linux/timer.h> 341da177e4SLinus Torvalds #include <linux/mm.h> 351da177e4SLinus Torvalds #include <linux/kernel.h> 361da177e4SLinus Torvalds #include <linux/fcntl.h> 371da177e4SLinus Torvalds #include <linux/stat.h> 381da177e4SLinus Torvalds #include <linux/socket.h> 391da177e4SLinus Torvalds #include <linux/in.h> 401da177e4SLinus Torvalds #include <linux/inet.h> 411da177e4SLinus Torvalds #include <linux/netdevice.h> 421da177e4SLinus Torvalds #include <linux/inetdevice.h> 431da177e4SLinus Torvalds #include <linux/igmp.h> 441da177e4SLinus Torvalds #include <linux/proc_fs.h> 451da177e4SLinus Torvalds #include <linux/seq_file.h> 461da177e4SLinus Torvalds #include <linux/mroute.h> 471da177e4SLinus Torvalds #include <linux/init.h> 4846f25dffSKris Katterjohn #include <linux/if_ether.h> 495a0e3ad6STejun Heo #include <linux/slab.h> 50457c4cbcSEric W. Biederman #include <net/net_namespace.h> 511da177e4SLinus Torvalds #include <net/ip.h> 521da177e4SLinus Torvalds #include <net/protocol.h> 531da177e4SLinus Torvalds #include <linux/skbuff.h> 5414c85021SArnaldo Carvalho de Melo #include <net/route.h> 551da177e4SLinus Torvalds #include <net/sock.h> 561da177e4SLinus Torvalds #include <net/icmp.h> 571da177e4SLinus Torvalds #include <net/udp.h> 581da177e4SLinus Torvalds #include <net/raw.h> 591da177e4SLinus Torvalds #include <linux/notifier.h> 601da177e4SLinus Torvalds #include <linux/if_arp.h> 611da177e4SLinus Torvalds #include <linux/netfilter_ipv4.h> 62709b46e8SEric W. Biederman #include <linux/compat.h> 63bc3b2d7fSPaul Gortmaker #include <linux/export.h> 64c5441932SPravin B Shelar #include <net/ip_tunnels.h> 651da177e4SLinus Torvalds #include <net/checksum.h> 66dc5fc579SArnaldo Carvalho de Melo #include <net/netlink.h> 67f0ad0860SPatrick McHardy #include <net/fib_rules.h> 68d67b8c61SNicolas Dichtel #include <linux/netconf.h> 69ccbb0aa6SNikolay Aleksandrov #include <net/nexthop.h> 701da177e4SLinus Torvalds 71f0ad0860SPatrick McHardy struct ipmr_rule { 72f0ad0860SPatrick McHardy struct fib_rule common; 73f0ad0860SPatrick McHardy }; 74f0ad0860SPatrick McHardy 75f0ad0860SPatrick McHardy struct ipmr_result { 76f0ad0860SPatrick McHardy struct mr_table *mrt; 77f0ad0860SPatrick McHardy }; 78f0ad0860SPatrick McHardy 791da177e4SLinus Torvalds /* Big lock, protecting vif table, mrt cache and mroute socket state. 80a8cb16ddSEric Dumazet * Note that the changes are semaphored via rtnl_lock. 811da177e4SLinus Torvalds */ 821da177e4SLinus Torvalds 831da177e4SLinus Torvalds static DEFINE_RWLOCK(mrt_lock); 841da177e4SLinus Torvalds 857ef8f65dSNikolay Aleksandrov /* Multicast router control variables */ 861da177e4SLinus Torvalds 871da177e4SLinus Torvalds /* Special spinlock for queue of unresolved entries */ 881da177e4SLinus Torvalds static DEFINE_SPINLOCK(mfc_unres_lock); 891da177e4SLinus Torvalds 901da177e4SLinus Torvalds /* We return to original Alan's scheme. Hash table of resolved 91a8cb16ddSEric Dumazet * entries is changed only in process context and protected 92a8cb16ddSEric Dumazet * with weak lock mrt_lock. Queue of unresolved entries is protected 93a8cb16ddSEric Dumazet * with strong spinlock mfc_unres_lock. 94a8cb16ddSEric Dumazet * 95a8cb16ddSEric Dumazet * In this case data path is free of exclusive locks at all. 961da177e4SLinus Torvalds */ 971da177e4SLinus Torvalds 98e18b890bSChristoph Lameter static struct kmem_cache *mrt_cachep __read_mostly; 991da177e4SLinus Torvalds 100f0ad0860SPatrick McHardy static struct mr_table *ipmr_new_table(struct net *net, u32 id); 101acbb219dSFrancesco Ruggeri static void ipmr_free_table(struct mr_table *mrt); 102acbb219dSFrancesco Ruggeri 103c4854ec8SRami Rosen static void ip_mr_forward(struct net *net, struct mr_table *mrt, 104*4b1f0d33SDonald Sharp struct net_device *dev, struct sk_buff *skb, 105*4b1f0d33SDonald Sharp struct mfc_cache *cache, int local); 1060c12295aSPatrick McHardy static int ipmr_cache_report(struct mr_table *mrt, 1074feb88e5SBenjamin Thery struct sk_buff *pkt, vifi_t vifi, int assert); 108cb6a4e46SPatrick McHardy static int __ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb, 109d658f8a0SPatrick McHardy struct mfc_cache *c, struct rtmsg *rtm); 1108cd3ac9fSNicolas Dichtel static void mroute_netlink_event(struct mr_table *mrt, struct mfc_cache *mfc, 1118cd3ac9fSNicolas Dichtel int cmd); 1120e615e96SNikolay Aleksandrov static void mroute_clean_tables(struct mr_table *mrt, bool all); 113f0ad0860SPatrick McHardy static void ipmr_expire_process(unsigned long arg); 1141da177e4SLinus Torvalds 115f0ad0860SPatrick McHardy #ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES 116f0ad0860SPatrick McHardy #define ipmr_for_each_table(mrt, net) \ 117f0ad0860SPatrick McHardy list_for_each_entry_rcu(mrt, &net->ipv4.mr_tables, list) 118f0ad0860SPatrick McHardy 119f0ad0860SPatrick McHardy static struct mr_table *ipmr_get_table(struct net *net, u32 id) 120f0ad0860SPatrick McHardy { 121f0ad0860SPatrick McHardy struct mr_table *mrt; 122f0ad0860SPatrick McHardy 123f0ad0860SPatrick McHardy ipmr_for_each_table(mrt, net) { 124f0ad0860SPatrick McHardy if (mrt->id == id) 125f0ad0860SPatrick McHardy return mrt; 126f0ad0860SPatrick McHardy } 127f0ad0860SPatrick McHardy return NULL; 128f0ad0860SPatrick McHardy } 129f0ad0860SPatrick McHardy 130da91981bSDavid S. Miller static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4, 131f0ad0860SPatrick McHardy struct mr_table **mrt) 132f0ad0860SPatrick McHardy { 133f0ad0860SPatrick McHardy int err; 13495f4a45dSHannes Frederic Sowa struct ipmr_result res; 13595f4a45dSHannes Frederic Sowa struct fib_lookup_arg arg = { 13695f4a45dSHannes Frederic Sowa .result = &res, 13795f4a45dSHannes Frederic Sowa .flags = FIB_LOOKUP_NOREF, 13895f4a45dSHannes Frederic Sowa }; 139f0ad0860SPatrick McHardy 140e58e4159SDavid Ahern /* update flow if oif or iif point to device enslaved to l3mdev */ 141e58e4159SDavid Ahern l3mdev_update_flow(net, flowi4_to_flowi(flp4)); 142e58e4159SDavid Ahern 143da91981bSDavid S. Miller err = fib_rules_lookup(net->ipv4.mr_rules_ops, 144da91981bSDavid S. Miller flowi4_to_flowi(flp4), 0, &arg); 145f0ad0860SPatrick McHardy if (err < 0) 146f0ad0860SPatrick McHardy return err; 147f0ad0860SPatrick McHardy *mrt = res.mrt; 148f0ad0860SPatrick McHardy return 0; 149f0ad0860SPatrick McHardy } 150f0ad0860SPatrick McHardy 151f0ad0860SPatrick McHardy static int ipmr_rule_action(struct fib_rule *rule, struct flowi *flp, 152f0ad0860SPatrick McHardy int flags, struct fib_lookup_arg *arg) 153f0ad0860SPatrick McHardy { 154f0ad0860SPatrick McHardy struct ipmr_result *res = arg->result; 155f0ad0860SPatrick McHardy struct mr_table *mrt; 156f0ad0860SPatrick McHardy 157f0ad0860SPatrick McHardy switch (rule->action) { 158f0ad0860SPatrick McHardy case FR_ACT_TO_TBL: 159f0ad0860SPatrick McHardy break; 160f0ad0860SPatrick McHardy case FR_ACT_UNREACHABLE: 161f0ad0860SPatrick McHardy return -ENETUNREACH; 162f0ad0860SPatrick McHardy case FR_ACT_PROHIBIT: 163f0ad0860SPatrick McHardy return -EACCES; 164f0ad0860SPatrick McHardy case FR_ACT_BLACKHOLE: 165f0ad0860SPatrick McHardy default: 166f0ad0860SPatrick McHardy return -EINVAL; 167f0ad0860SPatrick McHardy } 168f0ad0860SPatrick McHardy 169e58e4159SDavid Ahern arg->table = fib_rule_get_table(rule, arg); 170e58e4159SDavid Ahern 171e58e4159SDavid Ahern mrt = ipmr_get_table(rule->fr_net, arg->table); 17251456b29SIan Morris if (!mrt) 173f0ad0860SPatrick McHardy return -EAGAIN; 174f0ad0860SPatrick McHardy res->mrt = mrt; 175f0ad0860SPatrick McHardy return 0; 176f0ad0860SPatrick McHardy } 177f0ad0860SPatrick McHardy 178f0ad0860SPatrick McHardy static int ipmr_rule_match(struct fib_rule *rule, struct flowi *fl, int flags) 179f0ad0860SPatrick McHardy { 180f0ad0860SPatrick McHardy return 1; 181f0ad0860SPatrick McHardy } 182f0ad0860SPatrick McHardy 183f0ad0860SPatrick McHardy static const struct nla_policy ipmr_rule_policy[FRA_MAX + 1] = { 184f0ad0860SPatrick McHardy FRA_GENERIC_POLICY, 185f0ad0860SPatrick McHardy }; 186f0ad0860SPatrick McHardy 187f0ad0860SPatrick McHardy static int ipmr_rule_configure(struct fib_rule *rule, struct sk_buff *skb, 188f0ad0860SPatrick McHardy struct fib_rule_hdr *frh, struct nlattr **tb) 189f0ad0860SPatrick McHardy { 190f0ad0860SPatrick McHardy return 0; 191f0ad0860SPatrick McHardy } 192f0ad0860SPatrick McHardy 193f0ad0860SPatrick McHardy static int ipmr_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh, 194f0ad0860SPatrick McHardy struct nlattr **tb) 195f0ad0860SPatrick McHardy { 196f0ad0860SPatrick McHardy return 1; 197f0ad0860SPatrick McHardy } 198f0ad0860SPatrick McHardy 199f0ad0860SPatrick McHardy static int ipmr_rule_fill(struct fib_rule *rule, struct sk_buff *skb, 200f0ad0860SPatrick McHardy struct fib_rule_hdr *frh) 201f0ad0860SPatrick McHardy { 202f0ad0860SPatrick McHardy frh->dst_len = 0; 203f0ad0860SPatrick McHardy frh->src_len = 0; 204f0ad0860SPatrick McHardy frh->tos = 0; 205f0ad0860SPatrick McHardy return 0; 206f0ad0860SPatrick McHardy } 207f0ad0860SPatrick McHardy 20804a6f82cSAndi Kleen static const struct fib_rules_ops __net_initconst ipmr_rules_ops_template = { 20925239ceeSPatrick McHardy .family = RTNL_FAMILY_IPMR, 210f0ad0860SPatrick McHardy .rule_size = sizeof(struct ipmr_rule), 211f0ad0860SPatrick McHardy .addr_size = sizeof(u32), 212f0ad0860SPatrick McHardy .action = ipmr_rule_action, 213f0ad0860SPatrick McHardy .match = ipmr_rule_match, 214f0ad0860SPatrick McHardy .configure = ipmr_rule_configure, 215f0ad0860SPatrick McHardy .compare = ipmr_rule_compare, 216f0ad0860SPatrick McHardy .fill = ipmr_rule_fill, 217f0ad0860SPatrick McHardy .nlgroup = RTNLGRP_IPV4_RULE, 218f0ad0860SPatrick McHardy .policy = ipmr_rule_policy, 219f0ad0860SPatrick McHardy .owner = THIS_MODULE, 220f0ad0860SPatrick McHardy }; 221f0ad0860SPatrick McHardy 222f0ad0860SPatrick McHardy static int __net_init ipmr_rules_init(struct net *net) 223f0ad0860SPatrick McHardy { 224f0ad0860SPatrick McHardy struct fib_rules_ops *ops; 225f0ad0860SPatrick McHardy struct mr_table *mrt; 226f0ad0860SPatrick McHardy int err; 227f0ad0860SPatrick McHardy 228f0ad0860SPatrick McHardy ops = fib_rules_register(&ipmr_rules_ops_template, net); 229f0ad0860SPatrick McHardy if (IS_ERR(ops)) 230f0ad0860SPatrick McHardy return PTR_ERR(ops); 231f0ad0860SPatrick McHardy 232f0ad0860SPatrick McHardy INIT_LIST_HEAD(&net->ipv4.mr_tables); 233f0ad0860SPatrick McHardy 234f0ad0860SPatrick McHardy mrt = ipmr_new_table(net, RT_TABLE_DEFAULT); 2351113ebbcSNikolay Aleksandrov if (IS_ERR(mrt)) { 2361113ebbcSNikolay Aleksandrov err = PTR_ERR(mrt); 237f0ad0860SPatrick McHardy goto err1; 238f0ad0860SPatrick McHardy } 239f0ad0860SPatrick McHardy 240f0ad0860SPatrick McHardy err = fib_default_rule_add(ops, 0x7fff, RT_TABLE_DEFAULT, 0); 241f0ad0860SPatrick McHardy if (err < 0) 242f0ad0860SPatrick McHardy goto err2; 243f0ad0860SPatrick McHardy 244f0ad0860SPatrick McHardy net->ipv4.mr_rules_ops = ops; 245f0ad0860SPatrick McHardy return 0; 246f0ad0860SPatrick McHardy 247f0ad0860SPatrick McHardy err2: 248f243e5a7SWANG Cong ipmr_free_table(mrt); 249f0ad0860SPatrick McHardy err1: 250f0ad0860SPatrick McHardy fib_rules_unregister(ops); 251f0ad0860SPatrick McHardy return err; 252f0ad0860SPatrick McHardy } 253f0ad0860SPatrick McHardy 254f0ad0860SPatrick McHardy static void __net_exit ipmr_rules_exit(struct net *net) 255f0ad0860SPatrick McHardy { 256f0ad0860SPatrick McHardy struct mr_table *mrt, *next; 257f0ad0860SPatrick McHardy 258ed785309SWANG Cong rtnl_lock(); 259035320d5SEric Dumazet list_for_each_entry_safe(mrt, next, &net->ipv4.mr_tables, list) { 260035320d5SEric Dumazet list_del(&mrt->list); 261acbb219dSFrancesco Ruggeri ipmr_free_table(mrt); 262035320d5SEric Dumazet } 263f0ad0860SPatrick McHardy fib_rules_unregister(net->ipv4.mr_rules_ops); 264419df12fSWANG Cong rtnl_unlock(); 265f0ad0860SPatrick McHardy } 266f0ad0860SPatrick McHardy #else 267f0ad0860SPatrick McHardy #define ipmr_for_each_table(mrt, net) \ 268f0ad0860SPatrick McHardy for (mrt = net->ipv4.mrt; mrt; mrt = NULL) 269f0ad0860SPatrick McHardy 270f0ad0860SPatrick McHardy static struct mr_table *ipmr_get_table(struct net *net, u32 id) 271f0ad0860SPatrick McHardy { 272f0ad0860SPatrick McHardy return net->ipv4.mrt; 273f0ad0860SPatrick McHardy } 274f0ad0860SPatrick McHardy 275da91981bSDavid S. Miller static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4, 276f0ad0860SPatrick McHardy struct mr_table **mrt) 277f0ad0860SPatrick McHardy { 278f0ad0860SPatrick McHardy *mrt = net->ipv4.mrt; 279f0ad0860SPatrick McHardy return 0; 280f0ad0860SPatrick McHardy } 281f0ad0860SPatrick McHardy 282f0ad0860SPatrick McHardy static int __net_init ipmr_rules_init(struct net *net) 283f0ad0860SPatrick McHardy { 2841113ebbcSNikolay Aleksandrov struct mr_table *mrt; 2851113ebbcSNikolay Aleksandrov 2861113ebbcSNikolay Aleksandrov mrt = ipmr_new_table(net, RT_TABLE_DEFAULT); 2871113ebbcSNikolay Aleksandrov if (IS_ERR(mrt)) 2881113ebbcSNikolay Aleksandrov return PTR_ERR(mrt); 2891113ebbcSNikolay Aleksandrov net->ipv4.mrt = mrt; 2901113ebbcSNikolay Aleksandrov return 0; 291f0ad0860SPatrick McHardy } 292f0ad0860SPatrick McHardy 293f0ad0860SPatrick McHardy static void __net_exit ipmr_rules_exit(struct net *net) 294f0ad0860SPatrick McHardy { 295ed785309SWANG Cong rtnl_lock(); 296acbb219dSFrancesco Ruggeri ipmr_free_table(net->ipv4.mrt); 297ed785309SWANG Cong net->ipv4.mrt = NULL; 298ed785309SWANG Cong rtnl_unlock(); 299f0ad0860SPatrick McHardy } 300f0ad0860SPatrick McHardy #endif 301f0ad0860SPatrick McHardy 3028fb472c0SNikolay Aleksandrov static inline int ipmr_hash_cmp(struct rhashtable_compare_arg *arg, 3038fb472c0SNikolay Aleksandrov const void *ptr) 3048fb472c0SNikolay Aleksandrov { 3058fb472c0SNikolay Aleksandrov const struct mfc_cache_cmp_arg *cmparg = arg->key; 3068fb472c0SNikolay Aleksandrov struct mfc_cache *c = (struct mfc_cache *)ptr; 3078fb472c0SNikolay Aleksandrov 3088fb472c0SNikolay Aleksandrov return cmparg->mfc_mcastgrp != c->mfc_mcastgrp || 3098fb472c0SNikolay Aleksandrov cmparg->mfc_origin != c->mfc_origin; 3108fb472c0SNikolay Aleksandrov } 3118fb472c0SNikolay Aleksandrov 3128fb472c0SNikolay Aleksandrov static const struct rhashtable_params ipmr_rht_params = { 3138fb472c0SNikolay Aleksandrov .head_offset = offsetof(struct mfc_cache, mnode), 3148fb472c0SNikolay Aleksandrov .key_offset = offsetof(struct mfc_cache, cmparg), 3158fb472c0SNikolay Aleksandrov .key_len = sizeof(struct mfc_cache_cmp_arg), 3168fb472c0SNikolay Aleksandrov .nelem_hint = 3, 3178fb472c0SNikolay Aleksandrov .locks_mul = 1, 3188fb472c0SNikolay Aleksandrov .obj_cmpfn = ipmr_hash_cmp, 3198fb472c0SNikolay Aleksandrov .automatic_shrinking = true, 3208fb472c0SNikolay Aleksandrov }; 3218fb472c0SNikolay Aleksandrov 322f0ad0860SPatrick McHardy static struct mr_table *ipmr_new_table(struct net *net, u32 id) 323f0ad0860SPatrick McHardy { 324f0ad0860SPatrick McHardy struct mr_table *mrt; 325f0ad0860SPatrick McHardy 3261113ebbcSNikolay Aleksandrov /* "pimreg%u" should not exceed 16 bytes (IFNAMSIZ) */ 3271113ebbcSNikolay Aleksandrov if (id != RT_TABLE_DEFAULT && id >= 1000000000) 3281113ebbcSNikolay Aleksandrov return ERR_PTR(-EINVAL); 3291113ebbcSNikolay Aleksandrov 330f0ad0860SPatrick McHardy mrt = ipmr_get_table(net, id); 33100db4124SIan Morris if (mrt) 332f0ad0860SPatrick McHardy return mrt; 333f0ad0860SPatrick McHardy 334f0ad0860SPatrick McHardy mrt = kzalloc(sizeof(*mrt), GFP_KERNEL); 33551456b29SIan Morris if (!mrt) 3361113ebbcSNikolay Aleksandrov return ERR_PTR(-ENOMEM); 3378de53dfbSPatrick McHardy write_pnet(&mrt->net, net); 338f0ad0860SPatrick McHardy mrt->id = id; 339f0ad0860SPatrick McHardy 3408fb472c0SNikolay Aleksandrov rhltable_init(&mrt->mfc_hash, &ipmr_rht_params); 3418fb472c0SNikolay Aleksandrov INIT_LIST_HEAD(&mrt->mfc_cache_list); 342f0ad0860SPatrick McHardy INIT_LIST_HEAD(&mrt->mfc_unres_queue); 343f0ad0860SPatrick McHardy 344f0ad0860SPatrick McHardy setup_timer(&mrt->ipmr_expire_timer, ipmr_expire_process, 345f0ad0860SPatrick McHardy (unsigned long)mrt); 346f0ad0860SPatrick McHardy 347f0ad0860SPatrick McHardy mrt->mroute_reg_vif_num = -1; 348f0ad0860SPatrick McHardy #ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES 349f0ad0860SPatrick McHardy list_add_tail_rcu(&mrt->list, &net->ipv4.mr_tables); 350f0ad0860SPatrick McHardy #endif 351f0ad0860SPatrick McHardy return mrt; 352f0ad0860SPatrick McHardy } 3531da177e4SLinus Torvalds 354acbb219dSFrancesco Ruggeri static void ipmr_free_table(struct mr_table *mrt) 355acbb219dSFrancesco Ruggeri { 356acbb219dSFrancesco Ruggeri del_timer_sync(&mrt->ipmr_expire_timer); 3570e615e96SNikolay Aleksandrov mroute_clean_tables(mrt, true); 3588fb472c0SNikolay Aleksandrov rhltable_destroy(&mrt->mfc_hash); 359acbb219dSFrancesco Ruggeri kfree(mrt); 360acbb219dSFrancesco Ruggeri } 361acbb219dSFrancesco Ruggeri 3621da177e4SLinus Torvalds /* Service routines creating virtual interfaces: DVMRP tunnels and PIMREG */ 3631da177e4SLinus Torvalds 364d607032dSWang Chen static void ipmr_del_tunnel(struct net_device *dev, struct vifctl *v) 365d607032dSWang Chen { 3664feb88e5SBenjamin Thery struct net *net = dev_net(dev); 3674feb88e5SBenjamin Thery 368d607032dSWang Chen dev_close(dev); 369d607032dSWang Chen 3704feb88e5SBenjamin Thery dev = __dev_get_by_name(net, "tunl0"); 371d607032dSWang Chen if (dev) { 3725bc3eb7eSStephen Hemminger const struct net_device_ops *ops = dev->netdev_ops; 373d607032dSWang Chen struct ifreq ifr; 374d607032dSWang Chen struct ip_tunnel_parm p; 375d607032dSWang Chen 376d607032dSWang Chen memset(&p, 0, sizeof(p)); 377d607032dSWang Chen p.iph.daddr = v->vifc_rmt_addr.s_addr; 378d607032dSWang Chen p.iph.saddr = v->vifc_lcl_addr.s_addr; 379d607032dSWang Chen p.iph.version = 4; 380d607032dSWang Chen p.iph.ihl = 5; 381d607032dSWang Chen p.iph.protocol = IPPROTO_IPIP; 382d607032dSWang Chen sprintf(p.name, "dvmrp%d", v->vifc_vifi); 383d607032dSWang Chen ifr.ifr_ifru.ifru_data = (__force void __user *)&p; 384d607032dSWang Chen 3855bc3eb7eSStephen Hemminger if (ops->ndo_do_ioctl) { 3865bc3eb7eSStephen Hemminger mm_segment_t oldfs = get_fs(); 3875bc3eb7eSStephen Hemminger 3885bc3eb7eSStephen Hemminger set_fs(KERNEL_DS); 3895bc3eb7eSStephen Hemminger ops->ndo_do_ioctl(dev, &ifr, SIOCDELTUNNEL); 390d607032dSWang Chen set_fs(oldfs); 391d607032dSWang Chen } 392d607032dSWang Chen } 3935bc3eb7eSStephen Hemminger } 394d607032dSWang Chen 395a0b47736SNikolay Aleksandrov /* Initialize ipmr pimreg/tunnel in_device */ 396a0b47736SNikolay Aleksandrov static bool ipmr_init_vif_indev(const struct net_device *dev) 397a0b47736SNikolay Aleksandrov { 398a0b47736SNikolay Aleksandrov struct in_device *in_dev; 399a0b47736SNikolay Aleksandrov 400a0b47736SNikolay Aleksandrov ASSERT_RTNL(); 401a0b47736SNikolay Aleksandrov 402a0b47736SNikolay Aleksandrov in_dev = __in_dev_get_rtnl(dev); 403a0b47736SNikolay Aleksandrov if (!in_dev) 404a0b47736SNikolay Aleksandrov return false; 405a0b47736SNikolay Aleksandrov ipv4_devconf_setall(in_dev); 406a0b47736SNikolay Aleksandrov neigh_parms_data_state_setall(in_dev->arp_parms); 407a0b47736SNikolay Aleksandrov IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0; 408a0b47736SNikolay Aleksandrov 409a0b47736SNikolay Aleksandrov return true; 410a0b47736SNikolay Aleksandrov } 411a0b47736SNikolay Aleksandrov 4127ef8f65dSNikolay Aleksandrov static struct net_device *ipmr_new_tunnel(struct net *net, struct vifctl *v) 4131da177e4SLinus Torvalds { 4141da177e4SLinus Torvalds struct net_device *dev; 4151da177e4SLinus Torvalds 4164feb88e5SBenjamin Thery dev = __dev_get_by_name(net, "tunl0"); 4171da177e4SLinus Torvalds 4181da177e4SLinus Torvalds if (dev) { 4195bc3eb7eSStephen Hemminger const struct net_device_ops *ops = dev->netdev_ops; 4201da177e4SLinus Torvalds int err; 4211da177e4SLinus Torvalds struct ifreq ifr; 4221da177e4SLinus Torvalds struct ip_tunnel_parm p; 4231da177e4SLinus Torvalds 4241da177e4SLinus Torvalds memset(&p, 0, sizeof(p)); 4251da177e4SLinus Torvalds p.iph.daddr = v->vifc_rmt_addr.s_addr; 4261da177e4SLinus Torvalds p.iph.saddr = v->vifc_lcl_addr.s_addr; 4271da177e4SLinus Torvalds p.iph.version = 4; 4281da177e4SLinus Torvalds p.iph.ihl = 5; 4291da177e4SLinus Torvalds p.iph.protocol = IPPROTO_IPIP; 4301da177e4SLinus Torvalds sprintf(p.name, "dvmrp%d", v->vifc_vifi); 431ba93ef74SStephen Hemminger ifr.ifr_ifru.ifru_data = (__force void __user *)&p; 4321da177e4SLinus Torvalds 4335bc3eb7eSStephen Hemminger if (ops->ndo_do_ioctl) { 4345bc3eb7eSStephen Hemminger mm_segment_t oldfs = get_fs(); 4355bc3eb7eSStephen Hemminger 4365bc3eb7eSStephen Hemminger set_fs(KERNEL_DS); 4375bc3eb7eSStephen Hemminger err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL); 4381da177e4SLinus Torvalds set_fs(oldfs); 439a8cb16ddSEric Dumazet } else { 4405bc3eb7eSStephen Hemminger err = -EOPNOTSUPP; 441a8cb16ddSEric Dumazet } 4421da177e4SLinus Torvalds dev = NULL; 4431da177e4SLinus Torvalds 4444feb88e5SBenjamin Thery if (err == 0 && 4454feb88e5SBenjamin Thery (dev = __dev_get_by_name(net, p.name)) != NULL) { 4461da177e4SLinus Torvalds dev->flags |= IFF_MULTICAST; 447a0b47736SNikolay Aleksandrov if (!ipmr_init_vif_indev(dev)) 4481da177e4SLinus Torvalds goto failure; 4491da177e4SLinus Torvalds if (dev_open(dev)) 4501da177e4SLinus Torvalds goto failure; 4517dc00c82SWang Chen dev_hold(dev); 4521da177e4SLinus Torvalds } 4531da177e4SLinus Torvalds } 4541da177e4SLinus Torvalds return dev; 4551da177e4SLinus Torvalds 4561da177e4SLinus Torvalds failure: 4571da177e4SLinus Torvalds unregister_netdevice(dev); 4581da177e4SLinus Torvalds return NULL; 4591da177e4SLinus Torvalds } 4601da177e4SLinus Torvalds 461c316c629SNikolay Aleksandrov #if defined(CONFIG_IP_PIMSM_V1) || defined(CONFIG_IP_PIMSM_V2) 4626fef4c0cSStephen Hemminger static netdev_tx_t reg_vif_xmit(struct sk_buff *skb, struct net_device *dev) 4631da177e4SLinus Torvalds { 4644feb88e5SBenjamin Thery struct net *net = dev_net(dev); 465f0ad0860SPatrick McHardy struct mr_table *mrt; 466da91981bSDavid S. Miller struct flowi4 fl4 = { 467da91981bSDavid S. Miller .flowi4_oif = dev->ifindex, 4686a662719SCong Wang .flowi4_iif = skb->skb_iif ? : LOOPBACK_IFINDEX, 469da91981bSDavid S. Miller .flowi4_mark = skb->mark, 470f0ad0860SPatrick McHardy }; 471f0ad0860SPatrick McHardy int err; 472f0ad0860SPatrick McHardy 473da91981bSDavid S. Miller err = ipmr_fib_lookup(net, &fl4, &mrt); 474e40dbc51SBen Greear if (err < 0) { 475e40dbc51SBen Greear kfree_skb(skb); 476f0ad0860SPatrick McHardy return err; 477e40dbc51SBen Greear } 4784feb88e5SBenjamin Thery 4791da177e4SLinus Torvalds read_lock(&mrt_lock); 480cf3677aeSPavel Emelyanov dev->stats.tx_bytes += skb->len; 481cf3677aeSPavel Emelyanov dev->stats.tx_packets++; 4820c12295aSPatrick McHardy ipmr_cache_report(mrt, skb, mrt->mroute_reg_vif_num, IGMPMSG_WHOLEPKT); 4831da177e4SLinus Torvalds read_unlock(&mrt_lock); 4841da177e4SLinus Torvalds kfree_skb(skb); 4856ed10654SPatrick McHardy return NETDEV_TX_OK; 4861da177e4SLinus Torvalds } 4871da177e4SLinus Torvalds 488ee9b9596SNicolas Dichtel static int reg_vif_get_iflink(const struct net_device *dev) 489ee9b9596SNicolas Dichtel { 490ee9b9596SNicolas Dichtel return 0; 491ee9b9596SNicolas Dichtel } 492ee9b9596SNicolas Dichtel 493007c3838SStephen Hemminger static const struct net_device_ops reg_vif_netdev_ops = { 494007c3838SStephen Hemminger .ndo_start_xmit = reg_vif_xmit, 495ee9b9596SNicolas Dichtel .ndo_get_iflink = reg_vif_get_iflink, 496007c3838SStephen Hemminger }; 497007c3838SStephen Hemminger 4981da177e4SLinus Torvalds static void reg_vif_setup(struct net_device *dev) 4991da177e4SLinus Torvalds { 5001da177e4SLinus Torvalds dev->type = ARPHRD_PIMREG; 50146f25dffSKris Katterjohn dev->mtu = ETH_DATA_LEN - sizeof(struct iphdr) - 8; 5021da177e4SLinus Torvalds dev->flags = IFF_NOARP; 50370cb4a45SHimangi Saraogi dev->netdev_ops = ®_vif_netdev_ops; 504cf124db5SDavid S. Miller dev->needs_free_netdev = true; 505403dbb97STom Goff dev->features |= NETIF_F_NETNS_LOCAL; 5061da177e4SLinus Torvalds } 5071da177e4SLinus Torvalds 508f0ad0860SPatrick McHardy static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt) 5091da177e4SLinus Torvalds { 5101da177e4SLinus Torvalds struct net_device *dev; 511f0ad0860SPatrick McHardy char name[IFNAMSIZ]; 5121da177e4SLinus Torvalds 513f0ad0860SPatrick McHardy if (mrt->id == RT_TABLE_DEFAULT) 514f0ad0860SPatrick McHardy sprintf(name, "pimreg"); 515f0ad0860SPatrick McHardy else 516f0ad0860SPatrick McHardy sprintf(name, "pimreg%u", mrt->id); 517f0ad0860SPatrick McHardy 518c835a677STom Gundersen dev = alloc_netdev(0, name, NET_NAME_UNKNOWN, reg_vif_setup); 5191da177e4SLinus Torvalds 52051456b29SIan Morris if (!dev) 5211da177e4SLinus Torvalds return NULL; 5221da177e4SLinus Torvalds 523403dbb97STom Goff dev_net_set(dev, net); 524403dbb97STom Goff 5251da177e4SLinus Torvalds if (register_netdevice(dev)) { 5261da177e4SLinus Torvalds free_netdev(dev); 5271da177e4SLinus Torvalds return NULL; 5281da177e4SLinus Torvalds } 5291da177e4SLinus Torvalds 530a0b47736SNikolay Aleksandrov if (!ipmr_init_vif_indev(dev)) 5311da177e4SLinus Torvalds goto failure; 5321da177e4SLinus Torvalds if (dev_open(dev)) 5331da177e4SLinus Torvalds goto failure; 5341da177e4SLinus Torvalds 5357dc00c82SWang Chen dev_hold(dev); 5367dc00c82SWang Chen 5371da177e4SLinus Torvalds return dev; 5381da177e4SLinus Torvalds 5391da177e4SLinus Torvalds failure: 5401da177e4SLinus Torvalds unregister_netdevice(dev); 5411da177e4SLinus Torvalds return NULL; 5421da177e4SLinus Torvalds } 543c316c629SNikolay Aleksandrov 544c316c629SNikolay Aleksandrov /* called with rcu_read_lock() */ 545c316c629SNikolay Aleksandrov static int __pim_rcv(struct mr_table *mrt, struct sk_buff *skb, 546c316c629SNikolay Aleksandrov unsigned int pimlen) 547c316c629SNikolay Aleksandrov { 548c316c629SNikolay Aleksandrov struct net_device *reg_dev = NULL; 549c316c629SNikolay Aleksandrov struct iphdr *encap; 550c316c629SNikolay Aleksandrov 551c316c629SNikolay Aleksandrov encap = (struct iphdr *)(skb_transport_header(skb) + pimlen); 5527ef8f65dSNikolay Aleksandrov /* Check that: 553c316c629SNikolay Aleksandrov * a. packet is really sent to a multicast group 554c316c629SNikolay Aleksandrov * b. packet is not a NULL-REGISTER 555c316c629SNikolay Aleksandrov * c. packet is not truncated 556c316c629SNikolay Aleksandrov */ 557c316c629SNikolay Aleksandrov if (!ipv4_is_multicast(encap->daddr) || 558c316c629SNikolay Aleksandrov encap->tot_len == 0 || 559c316c629SNikolay Aleksandrov ntohs(encap->tot_len) + pimlen > skb->len) 560c316c629SNikolay Aleksandrov return 1; 561c316c629SNikolay Aleksandrov 562c316c629SNikolay Aleksandrov read_lock(&mrt_lock); 563c316c629SNikolay Aleksandrov if (mrt->mroute_reg_vif_num >= 0) 564c316c629SNikolay Aleksandrov reg_dev = mrt->vif_table[mrt->mroute_reg_vif_num].dev; 565c316c629SNikolay Aleksandrov read_unlock(&mrt_lock); 566c316c629SNikolay Aleksandrov 567c316c629SNikolay Aleksandrov if (!reg_dev) 568c316c629SNikolay Aleksandrov return 1; 569c316c629SNikolay Aleksandrov 570c316c629SNikolay Aleksandrov skb->mac_header = skb->network_header; 571c316c629SNikolay Aleksandrov skb_pull(skb, (u8 *)encap - skb->data); 572c316c629SNikolay Aleksandrov skb_reset_network_header(skb); 573c316c629SNikolay Aleksandrov skb->protocol = htons(ETH_P_IP); 574c316c629SNikolay Aleksandrov skb->ip_summed = CHECKSUM_NONE; 575c316c629SNikolay Aleksandrov 576c316c629SNikolay Aleksandrov skb_tunnel_rx(skb, reg_dev, dev_net(reg_dev)); 577c316c629SNikolay Aleksandrov 578c316c629SNikolay Aleksandrov netif_rx(skb); 579c316c629SNikolay Aleksandrov 580c316c629SNikolay Aleksandrov return NET_RX_SUCCESS; 581c316c629SNikolay Aleksandrov } 582c316c629SNikolay Aleksandrov #else 583c316c629SNikolay Aleksandrov static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt) 584c316c629SNikolay Aleksandrov { 585c316c629SNikolay Aleksandrov return NULL; 586c316c629SNikolay Aleksandrov } 5871da177e4SLinus Torvalds #endif 5881da177e4SLinus Torvalds 5892c53040fSBen Hutchings /** 5902c53040fSBen Hutchings * vif_delete - Delete a VIF entry 5917dc00c82SWang Chen * @notify: Set to 1, if the caller is a notifier_call 5921da177e4SLinus Torvalds */ 5930c12295aSPatrick McHardy static int vif_delete(struct mr_table *mrt, int vifi, int notify, 594d17fa6faSEric Dumazet struct list_head *head) 5951da177e4SLinus Torvalds { 5961da177e4SLinus Torvalds struct vif_device *v; 5971da177e4SLinus Torvalds struct net_device *dev; 5981da177e4SLinus Torvalds struct in_device *in_dev; 5991da177e4SLinus Torvalds 6000c12295aSPatrick McHardy if (vifi < 0 || vifi >= mrt->maxvif) 6011da177e4SLinus Torvalds return -EADDRNOTAVAIL; 6021da177e4SLinus Torvalds 6030c12295aSPatrick McHardy v = &mrt->vif_table[vifi]; 6041da177e4SLinus Torvalds 6051da177e4SLinus Torvalds write_lock_bh(&mrt_lock); 6061da177e4SLinus Torvalds dev = v->dev; 6071da177e4SLinus Torvalds v->dev = NULL; 6081da177e4SLinus Torvalds 6091da177e4SLinus Torvalds if (!dev) { 6101da177e4SLinus Torvalds write_unlock_bh(&mrt_lock); 6111da177e4SLinus Torvalds return -EADDRNOTAVAIL; 6121da177e4SLinus Torvalds } 6131da177e4SLinus Torvalds 6140c12295aSPatrick McHardy if (vifi == mrt->mroute_reg_vif_num) 6150c12295aSPatrick McHardy mrt->mroute_reg_vif_num = -1; 6161da177e4SLinus Torvalds 6170c12295aSPatrick McHardy if (vifi + 1 == mrt->maxvif) { 6181da177e4SLinus Torvalds int tmp; 619a8cb16ddSEric Dumazet 6201da177e4SLinus Torvalds for (tmp = vifi - 1; tmp >= 0; tmp--) { 6210c12295aSPatrick McHardy if (VIF_EXISTS(mrt, tmp)) 6221da177e4SLinus Torvalds break; 6231da177e4SLinus Torvalds } 6240c12295aSPatrick McHardy mrt->maxvif = tmp+1; 6251da177e4SLinus Torvalds } 6261da177e4SLinus Torvalds 6271da177e4SLinus Torvalds write_unlock_bh(&mrt_lock); 6281da177e4SLinus Torvalds 6291da177e4SLinus Torvalds dev_set_allmulti(dev, -1); 6301da177e4SLinus Torvalds 631a8cb16ddSEric Dumazet in_dev = __in_dev_get_rtnl(dev); 632a8cb16ddSEric Dumazet if (in_dev) { 63342f811b8SHerbert Xu IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)--; 6343b022865SDavid Ahern inet_netconf_notify_devconf(dev_net(dev), RTM_NEWNETCONF, 635d67b8c61SNicolas Dichtel NETCONFA_MC_FORWARDING, 636d67b8c61SNicolas Dichtel dev->ifindex, &in_dev->cnf); 6371da177e4SLinus Torvalds ip_rt_multicast_event(in_dev); 6381da177e4SLinus Torvalds } 6391da177e4SLinus Torvalds 6407dc00c82SWang Chen if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER) && !notify) 641d17fa6faSEric Dumazet unregister_netdevice_queue(dev, head); 6421da177e4SLinus Torvalds 6431da177e4SLinus Torvalds dev_put(dev); 6441da177e4SLinus Torvalds return 0; 6451da177e4SLinus Torvalds } 6461da177e4SLinus Torvalds 647a8c9486bSEric Dumazet static void ipmr_cache_free_rcu(struct rcu_head *head) 648a8c9486bSEric Dumazet { 649a8c9486bSEric Dumazet struct mfc_cache *c = container_of(head, struct mfc_cache, rcu); 650a8c9486bSEric Dumazet 651a8c9486bSEric Dumazet kmem_cache_free(mrt_cachep, c); 652a8c9486bSEric Dumazet } 653a8c9486bSEric Dumazet 6545c0a66f5SBenjamin Thery static inline void ipmr_cache_free(struct mfc_cache *c) 6555c0a66f5SBenjamin Thery { 656a8c9486bSEric Dumazet call_rcu(&c->rcu, ipmr_cache_free_rcu); 6575c0a66f5SBenjamin Thery } 6585c0a66f5SBenjamin Thery 6591da177e4SLinus Torvalds /* Destroy an unresolved cache entry, killing queued skbs 660a8cb16ddSEric Dumazet * and reporting error to netlink readers. 6611da177e4SLinus Torvalds */ 6620c12295aSPatrick McHardy static void ipmr_destroy_unres(struct mr_table *mrt, struct mfc_cache *c) 6631da177e4SLinus Torvalds { 6648de53dfbSPatrick McHardy struct net *net = read_pnet(&mrt->net); 6651da177e4SLinus Torvalds struct sk_buff *skb; 6669ef1d4c7SPatrick McHardy struct nlmsgerr *e; 6671da177e4SLinus Torvalds 6680c12295aSPatrick McHardy atomic_dec(&mrt->cache_resolve_queue_len); 6691da177e4SLinus Torvalds 6701da177e4SLinus Torvalds while ((skb = skb_dequeue(&c->mfc_un.unres.unresolved))) { 671eddc9ec5SArnaldo Carvalho de Melo if (ip_hdr(skb)->version == 0) { 6721da177e4SLinus Torvalds struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr)); 6731da177e4SLinus Torvalds nlh->nlmsg_type = NLMSG_ERROR; 674573ce260SHong zhi guo nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr)); 6751da177e4SLinus Torvalds skb_trim(skb, nlh->nlmsg_len); 676573ce260SHong zhi guo e = nlmsg_data(nlh); 6779ef1d4c7SPatrick McHardy e->error = -ETIMEDOUT; 6789ef1d4c7SPatrick McHardy memset(&e->msg, 0, sizeof(e->msg)); 6792942e900SThomas Graf 68015e47304SEric W. Biederman rtnl_unicast(skb, net, NETLINK_CB(skb).portid); 681a8cb16ddSEric Dumazet } else { 6821da177e4SLinus Torvalds kfree_skb(skb); 6831da177e4SLinus Torvalds } 684a8cb16ddSEric Dumazet } 6851da177e4SLinus Torvalds 6865c0a66f5SBenjamin Thery ipmr_cache_free(c); 6871da177e4SLinus Torvalds } 6881da177e4SLinus Torvalds 689e258beb2SPatrick McHardy /* Timer process for the unresolved queue. */ 690e258beb2SPatrick McHardy static void ipmr_expire_process(unsigned long arg) 6911da177e4SLinus Torvalds { 6920c12295aSPatrick McHardy struct mr_table *mrt = (struct mr_table *)arg; 6931da177e4SLinus Torvalds unsigned long now; 6941da177e4SLinus Torvalds unsigned long expires; 695862465f2SPatrick McHardy struct mfc_cache *c, *next; 6961da177e4SLinus Torvalds 6971da177e4SLinus Torvalds if (!spin_trylock(&mfc_unres_lock)) { 6980c12295aSPatrick McHardy mod_timer(&mrt->ipmr_expire_timer, jiffies+HZ/10); 6991da177e4SLinus Torvalds return; 7001da177e4SLinus Torvalds } 7011da177e4SLinus Torvalds 7020c12295aSPatrick McHardy if (list_empty(&mrt->mfc_unres_queue)) 7031da177e4SLinus Torvalds goto out; 7041da177e4SLinus Torvalds 7051da177e4SLinus Torvalds now = jiffies; 7061da177e4SLinus Torvalds expires = 10*HZ; 7071da177e4SLinus Torvalds 7080c12295aSPatrick McHardy list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) { 7091da177e4SLinus Torvalds if (time_after(c->mfc_un.unres.expires, now)) { 7101da177e4SLinus Torvalds unsigned long interval = c->mfc_un.unres.expires - now; 7111da177e4SLinus Torvalds if (interval < expires) 7121da177e4SLinus Torvalds expires = interval; 7131da177e4SLinus Torvalds continue; 7141da177e4SLinus Torvalds } 7151da177e4SLinus Torvalds 716862465f2SPatrick McHardy list_del(&c->list); 7178cd3ac9fSNicolas Dichtel mroute_netlink_event(mrt, c, RTM_DELROUTE); 7180c12295aSPatrick McHardy ipmr_destroy_unres(mrt, c); 7191da177e4SLinus Torvalds } 7201da177e4SLinus Torvalds 7210c12295aSPatrick McHardy if (!list_empty(&mrt->mfc_unres_queue)) 7220c12295aSPatrick McHardy mod_timer(&mrt->ipmr_expire_timer, jiffies + expires); 7231da177e4SLinus Torvalds 7241da177e4SLinus Torvalds out: 7251da177e4SLinus Torvalds spin_unlock(&mfc_unres_lock); 7261da177e4SLinus Torvalds } 7271da177e4SLinus Torvalds 7281da177e4SLinus Torvalds /* Fill oifs list. It is called under write locked mrt_lock. */ 7290c12295aSPatrick McHardy static void ipmr_update_thresholds(struct mr_table *mrt, struct mfc_cache *cache, 730d658f8a0SPatrick McHardy unsigned char *ttls) 7311da177e4SLinus Torvalds { 7321da177e4SLinus Torvalds int vifi; 7331da177e4SLinus Torvalds 7341da177e4SLinus Torvalds cache->mfc_un.res.minvif = MAXVIFS; 7351da177e4SLinus Torvalds cache->mfc_un.res.maxvif = 0; 7361da177e4SLinus Torvalds memset(cache->mfc_un.res.ttls, 255, MAXVIFS); 7371da177e4SLinus Torvalds 7380c12295aSPatrick McHardy for (vifi = 0; vifi < mrt->maxvif; vifi++) { 7390c12295aSPatrick McHardy if (VIF_EXISTS(mrt, vifi) && 740cf958ae3SBenjamin Thery ttls[vifi] && ttls[vifi] < 255) { 7411da177e4SLinus Torvalds cache->mfc_un.res.ttls[vifi] = ttls[vifi]; 7421da177e4SLinus Torvalds if (cache->mfc_un.res.minvif > vifi) 7431da177e4SLinus Torvalds cache->mfc_un.res.minvif = vifi; 7441da177e4SLinus Torvalds if (cache->mfc_un.res.maxvif <= vifi) 7451da177e4SLinus Torvalds cache->mfc_un.res.maxvif = vifi + 1; 7461da177e4SLinus Torvalds } 7471da177e4SLinus Torvalds } 74890b5ca17SNikolay Aleksandrov cache->mfc_un.res.lastuse = jiffies; 7491da177e4SLinus Torvalds } 7501da177e4SLinus Torvalds 7510c12295aSPatrick McHardy static int vif_add(struct net *net, struct mr_table *mrt, 7520c12295aSPatrick McHardy struct vifctl *vifc, int mrtsock) 7531da177e4SLinus Torvalds { 7541da177e4SLinus Torvalds int vifi = vifc->vifc_vifi; 7550c12295aSPatrick McHardy struct vif_device *v = &mrt->vif_table[vifi]; 7561da177e4SLinus Torvalds struct net_device *dev; 7571da177e4SLinus Torvalds struct in_device *in_dev; 758d607032dSWang Chen int err; 7591da177e4SLinus Torvalds 7601da177e4SLinus Torvalds /* Is vif busy ? */ 7610c12295aSPatrick McHardy if (VIF_EXISTS(mrt, vifi)) 7621da177e4SLinus Torvalds return -EADDRINUSE; 7631da177e4SLinus Torvalds 7641da177e4SLinus Torvalds switch (vifc->vifc_flags) { 7651da177e4SLinus Torvalds case VIFF_REGISTER: 7661973a4eaSNikolay Aleksandrov if (!ipmr_pimsm_enabled()) 767c316c629SNikolay Aleksandrov return -EINVAL; 768c316c629SNikolay Aleksandrov /* Special Purpose VIF in PIM 7691da177e4SLinus Torvalds * All the packets will be sent to the daemon 7701da177e4SLinus Torvalds */ 7710c12295aSPatrick McHardy if (mrt->mroute_reg_vif_num >= 0) 7721da177e4SLinus Torvalds return -EADDRINUSE; 773f0ad0860SPatrick McHardy dev = ipmr_reg_vif(net, mrt); 7741da177e4SLinus Torvalds if (!dev) 7751da177e4SLinus Torvalds return -ENOBUFS; 776d607032dSWang Chen err = dev_set_allmulti(dev, 1); 777d607032dSWang Chen if (err) { 778d607032dSWang Chen unregister_netdevice(dev); 7797dc00c82SWang Chen dev_put(dev); 780d607032dSWang Chen return err; 781d607032dSWang Chen } 7821da177e4SLinus Torvalds break; 7831da177e4SLinus Torvalds case VIFF_TUNNEL: 7844feb88e5SBenjamin Thery dev = ipmr_new_tunnel(net, vifc); 7851da177e4SLinus Torvalds if (!dev) 7861da177e4SLinus Torvalds return -ENOBUFS; 787d607032dSWang Chen err = dev_set_allmulti(dev, 1); 788d607032dSWang Chen if (err) { 789d607032dSWang Chen ipmr_del_tunnel(dev, vifc); 7907dc00c82SWang Chen dev_put(dev); 791d607032dSWang Chen return err; 792d607032dSWang Chen } 7931da177e4SLinus Torvalds break; 794ee5e81f0SIlia K case VIFF_USE_IFINDEX: 7951da177e4SLinus Torvalds case 0: 796ee5e81f0SIlia K if (vifc->vifc_flags == VIFF_USE_IFINDEX) { 797ee5e81f0SIlia K dev = dev_get_by_index(net, vifc->vifc_lcl_ifindex); 79851456b29SIan Morris if (dev && !__in_dev_get_rtnl(dev)) { 799ee5e81f0SIlia K dev_put(dev); 800ee5e81f0SIlia K return -EADDRNOTAVAIL; 801ee5e81f0SIlia K } 802a8cb16ddSEric Dumazet } else { 8034feb88e5SBenjamin Thery dev = ip_dev_find(net, vifc->vifc_lcl_addr.s_addr); 804a8cb16ddSEric Dumazet } 8051da177e4SLinus Torvalds if (!dev) 8061da177e4SLinus Torvalds return -EADDRNOTAVAIL; 807d607032dSWang Chen err = dev_set_allmulti(dev, 1); 8087dc00c82SWang Chen if (err) { 8097dc00c82SWang Chen dev_put(dev); 810d607032dSWang Chen return err; 8117dc00c82SWang Chen } 8121da177e4SLinus Torvalds break; 8131da177e4SLinus Torvalds default: 8141da177e4SLinus Torvalds return -EINVAL; 8151da177e4SLinus Torvalds } 8161da177e4SLinus Torvalds 817a8cb16ddSEric Dumazet in_dev = __in_dev_get_rtnl(dev); 818a8cb16ddSEric Dumazet if (!in_dev) { 819d0490cfdSDan Carpenter dev_put(dev); 8201da177e4SLinus Torvalds return -EADDRNOTAVAIL; 821d0490cfdSDan Carpenter } 82242f811b8SHerbert Xu IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)++; 8233b022865SDavid Ahern inet_netconf_notify_devconf(net, RTM_NEWNETCONF, NETCONFA_MC_FORWARDING, 8243b022865SDavid Ahern dev->ifindex, &in_dev->cnf); 8251da177e4SLinus Torvalds ip_rt_multicast_event(in_dev); 8261da177e4SLinus Torvalds 827a8cb16ddSEric Dumazet /* Fill in the VIF structures */ 828a8cb16ddSEric Dumazet 8291da177e4SLinus Torvalds v->rate_limit = vifc->vifc_rate_limit; 8301da177e4SLinus Torvalds v->local = vifc->vifc_lcl_addr.s_addr; 8311da177e4SLinus Torvalds v->remote = vifc->vifc_rmt_addr.s_addr; 8321da177e4SLinus Torvalds v->flags = vifc->vifc_flags; 8331da177e4SLinus Torvalds if (!mrtsock) 8341da177e4SLinus Torvalds v->flags |= VIFF_STATIC; 8351da177e4SLinus Torvalds v->threshold = vifc->vifc_threshold; 8361da177e4SLinus Torvalds v->bytes_in = 0; 8371da177e4SLinus Torvalds v->bytes_out = 0; 8381da177e4SLinus Torvalds v->pkt_in = 0; 8391da177e4SLinus Torvalds v->pkt_out = 0; 8401da177e4SLinus Torvalds v->link = dev->ifindex; 8411da177e4SLinus Torvalds if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER)) 842a54acb3aSNicolas Dichtel v->link = dev_get_iflink(dev); 8431da177e4SLinus Torvalds 8441da177e4SLinus Torvalds /* And finish update writing critical data */ 8451da177e4SLinus Torvalds write_lock_bh(&mrt_lock); 8461da177e4SLinus Torvalds v->dev = dev; 8471da177e4SLinus Torvalds if (v->flags & VIFF_REGISTER) 8480c12295aSPatrick McHardy mrt->mroute_reg_vif_num = vifi; 8490c12295aSPatrick McHardy if (vifi+1 > mrt->maxvif) 8500c12295aSPatrick McHardy mrt->maxvif = vifi+1; 8511da177e4SLinus Torvalds write_unlock_bh(&mrt_lock); 8521da177e4SLinus Torvalds return 0; 8531da177e4SLinus Torvalds } 8541da177e4SLinus Torvalds 855a8c9486bSEric Dumazet /* called with rcu_read_lock() */ 8560c12295aSPatrick McHardy static struct mfc_cache *ipmr_cache_find(struct mr_table *mrt, 8574feb88e5SBenjamin Thery __be32 origin, 8584feb88e5SBenjamin Thery __be32 mcastgrp) 8591da177e4SLinus Torvalds { 8608fb472c0SNikolay Aleksandrov struct mfc_cache_cmp_arg arg = { 8618fb472c0SNikolay Aleksandrov .mfc_mcastgrp = mcastgrp, 8628fb472c0SNikolay Aleksandrov .mfc_origin = origin 8638fb472c0SNikolay Aleksandrov }; 8648fb472c0SNikolay Aleksandrov struct rhlist_head *tmp, *list; 8651da177e4SLinus Torvalds struct mfc_cache *c; 8661da177e4SLinus Torvalds 8678fb472c0SNikolay Aleksandrov list = rhltable_lookup(&mrt->mfc_hash, &arg, ipmr_rht_params); 8688fb472c0SNikolay Aleksandrov rhl_for_each_entry_rcu(c, tmp, list, mnode) 8691da177e4SLinus Torvalds return c; 8708fb472c0SNikolay Aleksandrov 871862465f2SPatrick McHardy return NULL; 872862465f2SPatrick McHardy } 8731da177e4SLinus Torvalds 874660b26dcSNicolas Dichtel /* Look for a (*,*,oif) entry */ 875660b26dcSNicolas Dichtel static struct mfc_cache *ipmr_cache_find_any_parent(struct mr_table *mrt, 876660b26dcSNicolas Dichtel int vifi) 877660b26dcSNicolas Dichtel { 8788fb472c0SNikolay Aleksandrov struct mfc_cache_cmp_arg arg = { 8798fb472c0SNikolay Aleksandrov .mfc_mcastgrp = htonl(INADDR_ANY), 8808fb472c0SNikolay Aleksandrov .mfc_origin = htonl(INADDR_ANY) 8818fb472c0SNikolay Aleksandrov }; 8828fb472c0SNikolay Aleksandrov struct rhlist_head *tmp, *list; 883660b26dcSNicolas Dichtel struct mfc_cache *c; 884660b26dcSNicolas Dichtel 8858fb472c0SNikolay Aleksandrov list = rhltable_lookup(&mrt->mfc_hash, &arg, ipmr_rht_params); 8868fb472c0SNikolay Aleksandrov rhl_for_each_entry_rcu(c, tmp, list, mnode) 8878fb472c0SNikolay Aleksandrov if (c->mfc_un.res.ttls[vifi] < 255) 888660b26dcSNicolas Dichtel return c; 889660b26dcSNicolas Dichtel 890660b26dcSNicolas Dichtel return NULL; 891660b26dcSNicolas Dichtel } 892660b26dcSNicolas Dichtel 893660b26dcSNicolas Dichtel /* Look for a (*,G) entry */ 894660b26dcSNicolas Dichtel static struct mfc_cache *ipmr_cache_find_any(struct mr_table *mrt, 895660b26dcSNicolas Dichtel __be32 mcastgrp, int vifi) 896660b26dcSNicolas Dichtel { 8978fb472c0SNikolay Aleksandrov struct mfc_cache_cmp_arg arg = { 8988fb472c0SNikolay Aleksandrov .mfc_mcastgrp = mcastgrp, 8998fb472c0SNikolay Aleksandrov .mfc_origin = htonl(INADDR_ANY) 9008fb472c0SNikolay Aleksandrov }; 9018fb472c0SNikolay Aleksandrov struct rhlist_head *tmp, *list; 902660b26dcSNicolas Dichtel struct mfc_cache *c, *proxy; 903660b26dcSNicolas Dichtel 904360eb5daSNicolas Dichtel if (mcastgrp == htonl(INADDR_ANY)) 905660b26dcSNicolas Dichtel goto skip; 906660b26dcSNicolas Dichtel 9078fb472c0SNikolay Aleksandrov list = rhltable_lookup(&mrt->mfc_hash, &arg, ipmr_rht_params); 9088fb472c0SNikolay Aleksandrov rhl_for_each_entry_rcu(c, tmp, list, mnode) { 909660b26dcSNicolas Dichtel if (c->mfc_un.res.ttls[vifi] < 255) 910660b26dcSNicolas Dichtel return c; 911660b26dcSNicolas Dichtel 912660b26dcSNicolas Dichtel /* It's ok if the vifi is part of the static tree */ 9138fb472c0SNikolay Aleksandrov proxy = ipmr_cache_find_any_parent(mrt, c->mfc_parent); 914660b26dcSNicolas Dichtel if (proxy && proxy->mfc_un.res.ttls[vifi] < 255) 915660b26dcSNicolas Dichtel return c; 916660b26dcSNicolas Dichtel } 917660b26dcSNicolas Dichtel 918660b26dcSNicolas Dichtel skip: 919660b26dcSNicolas Dichtel return ipmr_cache_find_any_parent(mrt, vifi); 920660b26dcSNicolas Dichtel } 921660b26dcSNicolas Dichtel 9228fb472c0SNikolay Aleksandrov /* Look for a (S,G,iif) entry if parent != -1 */ 9238fb472c0SNikolay Aleksandrov static struct mfc_cache *ipmr_cache_find_parent(struct mr_table *mrt, 9248fb472c0SNikolay Aleksandrov __be32 origin, __be32 mcastgrp, 9258fb472c0SNikolay Aleksandrov int parent) 9268fb472c0SNikolay Aleksandrov { 9278fb472c0SNikolay Aleksandrov struct mfc_cache_cmp_arg arg = { 9288fb472c0SNikolay Aleksandrov .mfc_mcastgrp = mcastgrp, 9298fb472c0SNikolay Aleksandrov .mfc_origin = origin, 9308fb472c0SNikolay Aleksandrov }; 9318fb472c0SNikolay Aleksandrov struct rhlist_head *tmp, *list; 9328fb472c0SNikolay Aleksandrov struct mfc_cache *c; 9338fb472c0SNikolay Aleksandrov 9348fb472c0SNikolay Aleksandrov list = rhltable_lookup(&mrt->mfc_hash, &arg, ipmr_rht_params); 9358fb472c0SNikolay Aleksandrov rhl_for_each_entry_rcu(c, tmp, list, mnode) 9368fb472c0SNikolay Aleksandrov if (parent == -1 || parent == c->mfc_parent) 9378fb472c0SNikolay Aleksandrov return c; 9388fb472c0SNikolay Aleksandrov 9398fb472c0SNikolay Aleksandrov return NULL; 9408fb472c0SNikolay Aleksandrov } 9418fb472c0SNikolay Aleksandrov 9427ef8f65dSNikolay Aleksandrov /* Allocate a multicast cache entry */ 943d658f8a0SPatrick McHardy static struct mfc_cache *ipmr_cache_alloc(void) 9441da177e4SLinus Torvalds { 945c3762229SRobert P. J. Day struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_KERNEL); 946a8c9486bSEric Dumazet 94770a0dec4STom Goff if (c) { 94870a0dec4STom Goff c->mfc_un.res.last_assert = jiffies - MFC_ASSERT_THRESH - 1; 9491da177e4SLinus Torvalds c->mfc_un.res.minvif = MAXVIFS; 95070a0dec4STom Goff } 9511da177e4SLinus Torvalds return c; 9521da177e4SLinus Torvalds } 9531da177e4SLinus Torvalds 954d658f8a0SPatrick McHardy static struct mfc_cache *ipmr_cache_alloc_unres(void) 9551da177e4SLinus Torvalds { 956c3762229SRobert P. J. Day struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_ATOMIC); 957a8c9486bSEric Dumazet 958a8c9486bSEric Dumazet if (c) { 9591da177e4SLinus Torvalds skb_queue_head_init(&c->mfc_un.unres.unresolved); 9601da177e4SLinus Torvalds c->mfc_un.unres.expires = jiffies + 10*HZ; 961a8c9486bSEric Dumazet } 9621da177e4SLinus Torvalds return c; 9631da177e4SLinus Torvalds } 9641da177e4SLinus Torvalds 9657ef8f65dSNikolay Aleksandrov /* A cache entry has gone into a resolved state from queued */ 9660c12295aSPatrick McHardy static void ipmr_cache_resolve(struct net *net, struct mr_table *mrt, 9670c12295aSPatrick McHardy struct mfc_cache *uc, struct mfc_cache *c) 9681da177e4SLinus Torvalds { 9691da177e4SLinus Torvalds struct sk_buff *skb; 9709ef1d4c7SPatrick McHardy struct nlmsgerr *e; 9711da177e4SLinus Torvalds 972a8cb16ddSEric Dumazet /* Play the pending entries through our router */ 9731da177e4SLinus Torvalds while ((skb = __skb_dequeue(&uc->mfc_un.unres.unresolved))) { 974eddc9ec5SArnaldo Carvalho de Melo if (ip_hdr(skb)->version == 0) { 9751da177e4SLinus Torvalds struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr)); 9761da177e4SLinus Torvalds 977573ce260SHong zhi guo if (__ipmr_fill_mroute(mrt, skb, c, nlmsg_data(nlh)) > 0) { 978a8cb16ddSEric Dumazet nlh->nlmsg_len = skb_tail_pointer(skb) - 979a8cb16ddSEric Dumazet (u8 *)nlh; 9801da177e4SLinus Torvalds } else { 9811da177e4SLinus Torvalds nlh->nlmsg_type = NLMSG_ERROR; 982573ce260SHong zhi guo nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr)); 9831da177e4SLinus Torvalds skb_trim(skb, nlh->nlmsg_len); 984573ce260SHong zhi guo e = nlmsg_data(nlh); 9859ef1d4c7SPatrick McHardy e->error = -EMSGSIZE; 9869ef1d4c7SPatrick McHardy memset(&e->msg, 0, sizeof(e->msg)); 9871da177e4SLinus Torvalds } 9882942e900SThomas Graf 98915e47304SEric W. Biederman rtnl_unicast(skb, net, NETLINK_CB(skb).portid); 990a8cb16ddSEric Dumazet } else { 991*4b1f0d33SDonald Sharp ip_mr_forward(net, mrt, skb->dev, skb, c, 0); 9921da177e4SLinus Torvalds } 9931da177e4SLinus Torvalds } 994a8cb16ddSEric Dumazet } 9951da177e4SLinus Torvalds 996c316c629SNikolay Aleksandrov /* Bounce a cache query up to mrouted. We could use netlink for this but mrouted 9971da177e4SLinus Torvalds * expects the following bizarre scheme. 9981da177e4SLinus Torvalds * 9991da177e4SLinus Torvalds * Called under mrt_lock. 10001da177e4SLinus Torvalds */ 10010c12295aSPatrick McHardy static int ipmr_cache_report(struct mr_table *mrt, 10024feb88e5SBenjamin Thery struct sk_buff *pkt, vifi_t vifi, int assert) 10031da177e4SLinus Torvalds { 1004c9bdd4b5SArnaldo Carvalho de Melo const int ihl = ip_hdrlen(pkt); 1005c316c629SNikolay Aleksandrov struct sock *mroute_sk; 10061da177e4SLinus Torvalds struct igmphdr *igmp; 10071da177e4SLinus Torvalds struct igmpmsg *msg; 1008c316c629SNikolay Aleksandrov struct sk_buff *skb; 10091da177e4SLinus Torvalds int ret; 10101da177e4SLinus Torvalds 10111da177e4SLinus Torvalds if (assert == IGMPMSG_WHOLEPKT) 10121da177e4SLinus Torvalds skb = skb_realloc_headroom(pkt, sizeof(struct iphdr)); 10131da177e4SLinus Torvalds else 10141da177e4SLinus Torvalds skb = alloc_skb(128, GFP_ATOMIC); 10151da177e4SLinus Torvalds 10161da177e4SLinus Torvalds if (!skb) 10171da177e4SLinus Torvalds return -ENOBUFS; 10181da177e4SLinus Torvalds 10191da177e4SLinus Torvalds if (assert == IGMPMSG_WHOLEPKT) { 10201da177e4SLinus Torvalds /* Ugly, but we have no choice with this interface. 1021a8cb16ddSEric Dumazet * Duplicate old header, fix ihl, length etc. 1022a8cb16ddSEric Dumazet * And all this only to mangle msg->im_msgtype and 1023a8cb16ddSEric Dumazet * to set msg->im_mbz to "mbz" :-) 10241da177e4SLinus Torvalds */ 1025878c8145SArnaldo Carvalho de Melo skb_push(skb, sizeof(struct iphdr)); 1026878c8145SArnaldo Carvalho de Melo skb_reset_network_header(skb); 1027badff6d0SArnaldo Carvalho de Melo skb_reset_transport_header(skb); 10280272ffc4SArnaldo Carvalho de Melo msg = (struct igmpmsg *)skb_network_header(skb); 1029d56f90a7SArnaldo Carvalho de Melo memcpy(msg, skb_network_header(pkt), sizeof(struct iphdr)); 10301da177e4SLinus Torvalds msg->im_msgtype = IGMPMSG_WHOLEPKT; 10311da177e4SLinus Torvalds msg->im_mbz = 0; 10320c12295aSPatrick McHardy msg->im_vif = mrt->mroute_reg_vif_num; 1033eddc9ec5SArnaldo Carvalho de Melo ip_hdr(skb)->ihl = sizeof(struct iphdr) >> 2; 1034eddc9ec5SArnaldo Carvalho de Melo ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(pkt)->tot_len) + 1035eddc9ec5SArnaldo Carvalho de Melo sizeof(struct iphdr)); 1036c316c629SNikolay Aleksandrov } else { 1037a8cb16ddSEric Dumazet /* Copy the IP header */ 103830f3a40fSCong Wang skb_set_network_header(skb, skb->len); 1039ddc7b8e3SArnaldo Carvalho de Melo skb_put(skb, ihl); 104027d7ff46SArnaldo Carvalho de Melo skb_copy_to_linear_data(skb, pkt->data, ihl); 1041c316c629SNikolay Aleksandrov /* Flag to the kernel this is a route add */ 1042c316c629SNikolay Aleksandrov ip_hdr(skb)->protocol = 0; 1043eddc9ec5SArnaldo Carvalho de Melo msg = (struct igmpmsg *)skb_network_header(skb); 10441da177e4SLinus Torvalds msg->im_vif = vifi; 1045adf30907SEric Dumazet skb_dst_set(skb, dst_clone(skb_dst(pkt))); 1046a8cb16ddSEric Dumazet /* Add our header */ 10471da177e4SLinus Torvalds igmp = (struct igmphdr *)skb_put(skb, sizeof(struct igmphdr)); 1048c316c629SNikolay Aleksandrov igmp->type = assert; 10491da177e4SLinus Torvalds msg->im_msgtype = assert; 10501da177e4SLinus Torvalds igmp->code = 0; 1051eddc9ec5SArnaldo Carvalho de Melo ip_hdr(skb)->tot_len = htons(skb->len); /* Fix the length */ 1052b0e380b1SArnaldo Carvalho de Melo skb->transport_header = skb->network_header; 10531da177e4SLinus Torvalds } 10541da177e4SLinus Torvalds 10554c968709SEric Dumazet rcu_read_lock(); 10564c968709SEric Dumazet mroute_sk = rcu_dereference(mrt->mroute_sk); 105751456b29SIan Morris if (!mroute_sk) { 10584c968709SEric Dumazet rcu_read_unlock(); 10591da177e4SLinus Torvalds kfree_skb(skb); 10601da177e4SLinus Torvalds return -EINVAL; 10611da177e4SLinus Torvalds } 10621da177e4SLinus Torvalds 1063a8cb16ddSEric Dumazet /* Deliver to mrouted */ 10644c968709SEric Dumazet ret = sock_queue_rcv_skb(mroute_sk, skb); 10654c968709SEric Dumazet rcu_read_unlock(); 106670a269e6SBenjamin Thery if (ret < 0) { 1067e87cc472SJoe Perches net_warn_ratelimited("mroute: pending queue full, dropping entries\n"); 10681da177e4SLinus Torvalds kfree_skb(skb); 10691da177e4SLinus Torvalds } 10701da177e4SLinus Torvalds 10711da177e4SLinus Torvalds return ret; 10721da177e4SLinus Torvalds } 10731da177e4SLinus Torvalds 10747ef8f65dSNikolay Aleksandrov /* Queue a packet for resolution. It gets locked cache entry! */ 10757ef8f65dSNikolay Aleksandrov static int ipmr_cache_unresolved(struct mr_table *mrt, vifi_t vifi, 1076*4b1f0d33SDonald Sharp struct sk_buff *skb, struct net_device *dev) 10771da177e4SLinus Torvalds { 10788fb472c0SNikolay Aleksandrov const struct iphdr *iph = ip_hdr(skb); 10798fb472c0SNikolay Aleksandrov struct mfc_cache *c; 1080862465f2SPatrick McHardy bool found = false; 10811da177e4SLinus Torvalds int err; 10821da177e4SLinus Torvalds 10831da177e4SLinus Torvalds spin_lock_bh(&mfc_unres_lock); 10840c12295aSPatrick McHardy list_for_each_entry(c, &mrt->mfc_unres_queue, list) { 1085e258beb2SPatrick McHardy if (c->mfc_mcastgrp == iph->daddr && 1086862465f2SPatrick McHardy c->mfc_origin == iph->saddr) { 1087862465f2SPatrick McHardy found = true; 10881da177e4SLinus Torvalds break; 10891da177e4SLinus Torvalds } 1090862465f2SPatrick McHardy } 10911da177e4SLinus Torvalds 1092862465f2SPatrick McHardy if (!found) { 1093a8cb16ddSEric Dumazet /* Create a new entry if allowable */ 10940c12295aSPatrick McHardy if (atomic_read(&mrt->cache_resolve_queue_len) >= 10 || 1095d658f8a0SPatrick McHardy (c = ipmr_cache_alloc_unres()) == NULL) { 10961da177e4SLinus Torvalds spin_unlock_bh(&mfc_unres_lock); 10971da177e4SLinus Torvalds 10981da177e4SLinus Torvalds kfree_skb(skb); 10991da177e4SLinus Torvalds return -ENOBUFS; 11001da177e4SLinus Torvalds } 11011da177e4SLinus Torvalds 1102a8cb16ddSEric Dumazet /* Fill in the new cache entry */ 11031da177e4SLinus Torvalds c->mfc_parent = -1; 1104eddc9ec5SArnaldo Carvalho de Melo c->mfc_origin = iph->saddr; 1105eddc9ec5SArnaldo Carvalho de Melo c->mfc_mcastgrp = iph->daddr; 11061da177e4SLinus Torvalds 1107a8cb16ddSEric Dumazet /* Reflect first query at mrouted. */ 11080c12295aSPatrick McHardy err = ipmr_cache_report(mrt, skb, vifi, IGMPMSG_NOCACHE); 11094feb88e5SBenjamin Thery if (err < 0) { 11101da177e4SLinus Torvalds /* If the report failed throw the cache entry 11111da177e4SLinus Torvalds out - Brad Parker 11121da177e4SLinus Torvalds */ 11131da177e4SLinus Torvalds spin_unlock_bh(&mfc_unres_lock); 11141da177e4SLinus Torvalds 11155c0a66f5SBenjamin Thery ipmr_cache_free(c); 11161da177e4SLinus Torvalds kfree_skb(skb); 11171da177e4SLinus Torvalds return err; 11181da177e4SLinus Torvalds } 11191da177e4SLinus Torvalds 11200c12295aSPatrick McHardy atomic_inc(&mrt->cache_resolve_queue_len); 11210c12295aSPatrick McHardy list_add(&c->list, &mrt->mfc_unres_queue); 11228cd3ac9fSNicolas Dichtel mroute_netlink_event(mrt, c, RTM_NEWROUTE); 11231da177e4SLinus Torvalds 1124278554bdSDavid S. Miller if (atomic_read(&mrt->cache_resolve_queue_len) == 1) 11250c12295aSPatrick McHardy mod_timer(&mrt->ipmr_expire_timer, c->mfc_un.unres.expires); 11261da177e4SLinus Torvalds } 11271da177e4SLinus Torvalds 1128a8cb16ddSEric Dumazet /* See if we can append the packet */ 11291da177e4SLinus Torvalds if (c->mfc_un.unres.unresolved.qlen > 3) { 11301da177e4SLinus Torvalds kfree_skb(skb); 11311da177e4SLinus Torvalds err = -ENOBUFS; 11321da177e4SLinus Torvalds } else { 1133*4b1f0d33SDonald Sharp if (dev) { 1134*4b1f0d33SDonald Sharp skb->dev = dev; 1135*4b1f0d33SDonald Sharp skb->skb_iif = dev->ifindex; 1136*4b1f0d33SDonald Sharp } 11371da177e4SLinus Torvalds skb_queue_tail(&c->mfc_un.unres.unresolved, skb); 11381da177e4SLinus Torvalds err = 0; 11391da177e4SLinus Torvalds } 11401da177e4SLinus Torvalds 11411da177e4SLinus Torvalds spin_unlock_bh(&mfc_unres_lock); 11421da177e4SLinus Torvalds return err; 11431da177e4SLinus Torvalds } 11441da177e4SLinus Torvalds 11457ef8f65dSNikolay Aleksandrov /* MFC cache manipulation by user space mroute daemon */ 11461da177e4SLinus Torvalds 1147660b26dcSNicolas Dichtel static int ipmr_mfc_delete(struct mr_table *mrt, struct mfcctl *mfc, int parent) 11481da177e4SLinus Torvalds { 11498fb472c0SNikolay Aleksandrov struct mfc_cache *c; 11501da177e4SLinus Torvalds 11518fb472c0SNikolay Aleksandrov /* The entries are added/deleted only under RTNL */ 11528fb472c0SNikolay Aleksandrov rcu_read_lock(); 11538fb472c0SNikolay Aleksandrov c = ipmr_cache_find_parent(mrt, mfc->mfcc_origin.s_addr, 11548fb472c0SNikolay Aleksandrov mfc->mfcc_mcastgrp.s_addr, parent); 11558fb472c0SNikolay Aleksandrov rcu_read_unlock(); 11568fb472c0SNikolay Aleksandrov if (!c) 11578fb472c0SNikolay Aleksandrov return -ENOENT; 11588fb472c0SNikolay Aleksandrov rhltable_remove(&mrt->mfc_hash, &c->mnode, ipmr_rht_params); 1159a8c9486bSEric Dumazet list_del_rcu(&c->list); 11608cd3ac9fSNicolas Dichtel mroute_netlink_event(mrt, c, RTM_DELROUTE); 11615c0a66f5SBenjamin Thery ipmr_cache_free(c); 11628fb472c0SNikolay Aleksandrov 11631da177e4SLinus Torvalds return 0; 11641da177e4SLinus Torvalds } 11651da177e4SLinus Torvalds 11660c12295aSPatrick McHardy static int ipmr_mfc_add(struct net *net, struct mr_table *mrt, 1167660b26dcSNicolas Dichtel struct mfcctl *mfc, int mrtsock, int parent) 11681da177e4SLinus Torvalds { 1169862465f2SPatrick McHardy struct mfc_cache *uc, *c; 11708fb472c0SNikolay Aleksandrov bool found; 11718fb472c0SNikolay Aleksandrov int ret; 11721da177e4SLinus Torvalds 1173a50436f2SPatrick McHardy if (mfc->mfcc_parent >= MAXVIFS) 1174a50436f2SPatrick McHardy return -ENFILE; 1175a50436f2SPatrick McHardy 11768fb472c0SNikolay Aleksandrov /* The entries are added/deleted only under RTNL */ 11778fb472c0SNikolay Aleksandrov rcu_read_lock(); 11788fb472c0SNikolay Aleksandrov c = ipmr_cache_find_parent(mrt, mfc->mfcc_origin.s_addr, 11798fb472c0SNikolay Aleksandrov mfc->mfcc_mcastgrp.s_addr, parent); 11808fb472c0SNikolay Aleksandrov rcu_read_unlock(); 11818fb472c0SNikolay Aleksandrov if (c) { 11821da177e4SLinus Torvalds write_lock_bh(&mrt_lock); 11831da177e4SLinus Torvalds c->mfc_parent = mfc->mfcc_parent; 11840c12295aSPatrick McHardy ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls); 11851da177e4SLinus Torvalds if (!mrtsock) 11861da177e4SLinus Torvalds c->mfc_flags |= MFC_STATIC; 11871da177e4SLinus Torvalds write_unlock_bh(&mrt_lock); 11888cd3ac9fSNicolas Dichtel mroute_netlink_event(mrt, c, RTM_NEWROUTE); 11891da177e4SLinus Torvalds return 0; 11901da177e4SLinus Torvalds } 11911da177e4SLinus Torvalds 1192360eb5daSNicolas Dichtel if (mfc->mfcc_mcastgrp.s_addr != htonl(INADDR_ANY) && 1193660b26dcSNicolas Dichtel !ipv4_is_multicast(mfc->mfcc_mcastgrp.s_addr)) 11941da177e4SLinus Torvalds return -EINVAL; 11951da177e4SLinus Torvalds 1196d658f8a0SPatrick McHardy c = ipmr_cache_alloc(); 119751456b29SIan Morris if (!c) 11981da177e4SLinus Torvalds return -ENOMEM; 11991da177e4SLinus Torvalds 12001da177e4SLinus Torvalds c->mfc_origin = mfc->mfcc_origin.s_addr; 12011da177e4SLinus Torvalds c->mfc_mcastgrp = mfc->mfcc_mcastgrp.s_addr; 12021da177e4SLinus Torvalds c->mfc_parent = mfc->mfcc_parent; 12030c12295aSPatrick McHardy ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls); 12041da177e4SLinus Torvalds if (!mrtsock) 12051da177e4SLinus Torvalds c->mfc_flags |= MFC_STATIC; 12061da177e4SLinus Torvalds 12078fb472c0SNikolay Aleksandrov ret = rhltable_insert_key(&mrt->mfc_hash, &c->cmparg, &c->mnode, 12088fb472c0SNikolay Aleksandrov ipmr_rht_params); 12098fb472c0SNikolay Aleksandrov if (ret) { 12108fb472c0SNikolay Aleksandrov pr_err("ipmr: rhtable insert error %d\n", ret); 12118fb472c0SNikolay Aleksandrov ipmr_cache_free(c); 12128fb472c0SNikolay Aleksandrov return ret; 12138fb472c0SNikolay Aleksandrov } 12148fb472c0SNikolay Aleksandrov list_add_tail_rcu(&c->list, &mrt->mfc_cache_list); 12157ef8f65dSNikolay Aleksandrov /* Check to see if we resolved a queued list. If so we 12161da177e4SLinus Torvalds * need to send on the frames and tidy up. 12171da177e4SLinus Torvalds */ 1218b0ebb739SPatrick McHardy found = false; 12191da177e4SLinus Torvalds spin_lock_bh(&mfc_unres_lock); 12200c12295aSPatrick McHardy list_for_each_entry(uc, &mrt->mfc_unres_queue, list) { 1221e258beb2SPatrick McHardy if (uc->mfc_origin == c->mfc_origin && 12221da177e4SLinus Torvalds uc->mfc_mcastgrp == c->mfc_mcastgrp) { 1223862465f2SPatrick McHardy list_del(&uc->list); 12240c12295aSPatrick McHardy atomic_dec(&mrt->cache_resolve_queue_len); 1225b0ebb739SPatrick McHardy found = true; 12261da177e4SLinus Torvalds break; 12271da177e4SLinus Torvalds } 12281da177e4SLinus Torvalds } 12290c12295aSPatrick McHardy if (list_empty(&mrt->mfc_unres_queue)) 12300c12295aSPatrick McHardy del_timer(&mrt->ipmr_expire_timer); 12311da177e4SLinus Torvalds spin_unlock_bh(&mfc_unres_lock); 12321da177e4SLinus Torvalds 1233b0ebb739SPatrick McHardy if (found) { 12340c12295aSPatrick McHardy ipmr_cache_resolve(net, mrt, uc, c); 12355c0a66f5SBenjamin Thery ipmr_cache_free(uc); 12361da177e4SLinus Torvalds } 12378cd3ac9fSNicolas Dichtel mroute_netlink_event(mrt, c, RTM_NEWROUTE); 12381da177e4SLinus Torvalds return 0; 12391da177e4SLinus Torvalds } 12401da177e4SLinus Torvalds 12417ef8f65dSNikolay Aleksandrov /* Close the multicast socket, and clear the vif tables etc */ 12420e615e96SNikolay Aleksandrov static void mroute_clean_tables(struct mr_table *mrt, bool all) 12431da177e4SLinus Torvalds { 12448fb472c0SNikolay Aleksandrov struct mfc_cache *c, *tmp; 1245d17fa6faSEric Dumazet LIST_HEAD(list); 12468fb472c0SNikolay Aleksandrov int i; 12471da177e4SLinus Torvalds 1248a8cb16ddSEric Dumazet /* Shut down all active vif entries */ 12490c12295aSPatrick McHardy for (i = 0; i < mrt->maxvif; i++) { 12500e615e96SNikolay Aleksandrov if (!all && (mrt->vif_table[i].flags & VIFF_STATIC)) 12510e615e96SNikolay Aleksandrov continue; 12520c12295aSPatrick McHardy vif_delete(mrt, i, 0, &list); 12531da177e4SLinus Torvalds } 1254d17fa6faSEric Dumazet unregister_netdevice_many(&list); 12551da177e4SLinus Torvalds 1256a8cb16ddSEric Dumazet /* Wipe the cache */ 12578fb472c0SNikolay Aleksandrov list_for_each_entry_safe(c, tmp, &mrt->mfc_cache_list, list) { 12580e615e96SNikolay Aleksandrov if (!all && (c->mfc_flags & MFC_STATIC)) 12591da177e4SLinus Torvalds continue; 12608fb472c0SNikolay Aleksandrov rhltable_remove(&mrt->mfc_hash, &c->mnode, ipmr_rht_params); 1261a8c9486bSEric Dumazet list_del_rcu(&c->list); 12628cd3ac9fSNicolas Dichtel mroute_netlink_event(mrt, c, RTM_DELROUTE); 12635c0a66f5SBenjamin Thery ipmr_cache_free(c); 12641da177e4SLinus Torvalds } 12651da177e4SLinus Torvalds 12660c12295aSPatrick McHardy if (atomic_read(&mrt->cache_resolve_queue_len) != 0) { 12671da177e4SLinus Torvalds spin_lock_bh(&mfc_unres_lock); 12688fb472c0SNikolay Aleksandrov list_for_each_entry_safe(c, tmp, &mrt->mfc_unres_queue, list) { 1269862465f2SPatrick McHardy list_del(&c->list); 12708cd3ac9fSNicolas Dichtel mroute_netlink_event(mrt, c, RTM_DELROUTE); 12710c12295aSPatrick McHardy ipmr_destroy_unres(mrt, c); 12721da177e4SLinus Torvalds } 12731da177e4SLinus Torvalds spin_unlock_bh(&mfc_unres_lock); 12741da177e4SLinus Torvalds } 12751da177e4SLinus Torvalds } 12761da177e4SLinus Torvalds 12774c968709SEric Dumazet /* called from ip_ra_control(), before an RCU grace period, 12784c968709SEric Dumazet * we dont need to call synchronize_rcu() here 12794c968709SEric Dumazet */ 12801da177e4SLinus Torvalds static void mrtsock_destruct(struct sock *sk) 12811da177e4SLinus Torvalds { 12824feb88e5SBenjamin Thery struct net *net = sock_net(sk); 1283f0ad0860SPatrick McHardy struct mr_table *mrt; 12844feb88e5SBenjamin Thery 12851215e51eSWANG Cong ASSERT_RTNL(); 1286f0ad0860SPatrick McHardy ipmr_for_each_table(mrt, net) { 12874c968709SEric Dumazet if (sk == rtnl_dereference(mrt->mroute_sk)) { 12884feb88e5SBenjamin Thery IPV4_DEVCONF_ALL(net, MC_FORWARDING)--; 12893b022865SDavid Ahern inet_netconf_notify_devconf(net, RTM_NEWNETCONF, 12903b022865SDavid Ahern NETCONFA_MC_FORWARDING, 1291d67b8c61SNicolas Dichtel NETCONFA_IFINDEX_ALL, 1292d67b8c61SNicolas Dichtel net->ipv4.devconf_all); 1293a9b3cd7fSStephen Hemminger RCU_INIT_POINTER(mrt->mroute_sk, NULL); 12940e615e96SNikolay Aleksandrov mroute_clean_tables(mrt, false); 12951da177e4SLinus Torvalds } 12961da177e4SLinus Torvalds } 12971da177e4SLinus Torvalds } 12981da177e4SLinus Torvalds 12997ef8f65dSNikolay Aleksandrov /* Socket options and virtual interface manipulation. The whole 13001da177e4SLinus Torvalds * virtual interface system is a complete heap, but unfortunately 13011da177e4SLinus Torvalds * that's how BSD mrouted happens to think. Maybe one day with a proper 13021da177e4SLinus Torvalds * MOSPF/PIM router set up we can clean this up. 13031da177e4SLinus Torvalds */ 13041da177e4SLinus Torvalds 130529e97d21SNikolay Aleksandrov int ip_mroute_setsockopt(struct sock *sk, int optname, char __user *optval, 130629e97d21SNikolay Aleksandrov unsigned int optlen) 13071da177e4SLinus Torvalds { 130829e97d21SNikolay Aleksandrov struct net *net = sock_net(sk); 130929e97d21SNikolay Aleksandrov int val, ret = 0, parent = 0; 131029e97d21SNikolay Aleksandrov struct mr_table *mrt; 13111da177e4SLinus Torvalds struct vifctl vif; 13121da177e4SLinus Torvalds struct mfcctl mfc; 131329e97d21SNikolay Aleksandrov u32 uval; 1314f0ad0860SPatrick McHardy 131529e97d21SNikolay Aleksandrov /* There's one exception to the lock - MRT_DONE which needs to unlock */ 131629e97d21SNikolay Aleksandrov rtnl_lock(); 13175e1859fbSEric Dumazet if (sk->sk_type != SOCK_RAW || 131829e97d21SNikolay Aleksandrov inet_sk(sk)->inet_num != IPPROTO_IGMP) { 131929e97d21SNikolay Aleksandrov ret = -EOPNOTSUPP; 132029e97d21SNikolay Aleksandrov goto out_unlock; 132129e97d21SNikolay Aleksandrov } 13225e1859fbSEric Dumazet 1323f0ad0860SPatrick McHardy mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT); 132429e97d21SNikolay Aleksandrov if (!mrt) { 132529e97d21SNikolay Aleksandrov ret = -ENOENT; 132629e97d21SNikolay Aleksandrov goto out_unlock; 132729e97d21SNikolay Aleksandrov } 1328132adf54SStephen Hemminger if (optname != MRT_INIT) { 132933d480ceSEric Dumazet if (sk != rcu_access_pointer(mrt->mroute_sk) && 133029e97d21SNikolay Aleksandrov !ns_capable(net->user_ns, CAP_NET_ADMIN)) { 133129e97d21SNikolay Aleksandrov ret = -EACCES; 133229e97d21SNikolay Aleksandrov goto out_unlock; 133329e97d21SNikolay Aleksandrov } 13341da177e4SLinus Torvalds } 13351da177e4SLinus Torvalds 1336132adf54SStephen Hemminger switch (optname) { 13371da177e4SLinus Torvalds case MRT_INIT: 133842e6b89cSNikolay Aleksandrov if (optlen != sizeof(int)) { 133929e97d21SNikolay Aleksandrov ret = -EINVAL; 134029e97d21SNikolay Aleksandrov break; 134142e6b89cSNikolay Aleksandrov } 134242e6b89cSNikolay Aleksandrov if (rtnl_dereference(mrt->mroute_sk)) { 134342e6b89cSNikolay Aleksandrov ret = -EADDRINUSE; 134442e6b89cSNikolay Aleksandrov break; 134542e6b89cSNikolay Aleksandrov } 13461da177e4SLinus Torvalds 13471da177e4SLinus Torvalds ret = ip_ra_control(sk, 1, mrtsock_destruct); 13481da177e4SLinus Torvalds if (ret == 0) { 1349cf778b00SEric Dumazet rcu_assign_pointer(mrt->mroute_sk, sk); 13504feb88e5SBenjamin Thery IPV4_DEVCONF_ALL(net, MC_FORWARDING)++; 13513b022865SDavid Ahern inet_netconf_notify_devconf(net, RTM_NEWNETCONF, 13523b022865SDavid Ahern NETCONFA_MC_FORWARDING, 1353d67b8c61SNicolas Dichtel NETCONFA_IFINDEX_ALL, 1354d67b8c61SNicolas Dichtel net->ipv4.devconf_all); 13551da177e4SLinus Torvalds } 135629e97d21SNikolay Aleksandrov break; 13571da177e4SLinus Torvalds case MRT_DONE: 135829e97d21SNikolay Aleksandrov if (sk != rcu_access_pointer(mrt->mroute_sk)) { 135929e97d21SNikolay Aleksandrov ret = -EACCES; 136029e97d21SNikolay Aleksandrov } else { 136129e97d21SNikolay Aleksandrov ret = ip_ra_control(sk, 0, NULL); 13621215e51eSWANG Cong goto out_unlock; 136329e97d21SNikolay Aleksandrov } 136429e97d21SNikolay Aleksandrov break; 13651da177e4SLinus Torvalds case MRT_ADD_VIF: 13661da177e4SLinus Torvalds case MRT_DEL_VIF: 136729e97d21SNikolay Aleksandrov if (optlen != sizeof(vif)) { 136829e97d21SNikolay Aleksandrov ret = -EINVAL; 136929e97d21SNikolay Aleksandrov break; 137029e97d21SNikolay Aleksandrov } 137129e97d21SNikolay Aleksandrov if (copy_from_user(&vif, optval, sizeof(vif))) { 137229e97d21SNikolay Aleksandrov ret = -EFAULT; 137329e97d21SNikolay Aleksandrov break; 137429e97d21SNikolay Aleksandrov } 137529e97d21SNikolay Aleksandrov if (vif.vifc_vifi >= MAXVIFS) { 137629e97d21SNikolay Aleksandrov ret = -ENFILE; 137729e97d21SNikolay Aleksandrov break; 137829e97d21SNikolay Aleksandrov } 13791da177e4SLinus Torvalds if (optname == MRT_ADD_VIF) { 13804c968709SEric Dumazet ret = vif_add(net, mrt, &vif, 13814c968709SEric Dumazet sk == rtnl_dereference(mrt->mroute_sk)); 13821da177e4SLinus Torvalds } else { 13830c12295aSPatrick McHardy ret = vif_delete(mrt, vif.vifc_vifi, 0, NULL); 13841da177e4SLinus Torvalds } 138529e97d21SNikolay Aleksandrov break; 13867ef8f65dSNikolay Aleksandrov /* Manipulate the forwarding caches. These live 13871da177e4SLinus Torvalds * in a sort of kernel/user symbiosis. 13881da177e4SLinus Torvalds */ 13891da177e4SLinus Torvalds case MRT_ADD_MFC: 13901da177e4SLinus Torvalds case MRT_DEL_MFC: 1391660b26dcSNicolas Dichtel parent = -1; 1392660b26dcSNicolas Dichtel case MRT_ADD_MFC_PROXY: 1393660b26dcSNicolas Dichtel case MRT_DEL_MFC_PROXY: 139429e97d21SNikolay Aleksandrov if (optlen != sizeof(mfc)) { 139529e97d21SNikolay Aleksandrov ret = -EINVAL; 139629e97d21SNikolay Aleksandrov break; 139729e97d21SNikolay Aleksandrov } 139829e97d21SNikolay Aleksandrov if (copy_from_user(&mfc, optval, sizeof(mfc))) { 139929e97d21SNikolay Aleksandrov ret = -EFAULT; 140029e97d21SNikolay Aleksandrov break; 140129e97d21SNikolay Aleksandrov } 1402660b26dcSNicolas Dichtel if (parent == 0) 1403660b26dcSNicolas Dichtel parent = mfc.mfcc_parent; 1404660b26dcSNicolas Dichtel if (optname == MRT_DEL_MFC || optname == MRT_DEL_MFC_PROXY) 1405660b26dcSNicolas Dichtel ret = ipmr_mfc_delete(mrt, &mfc, parent); 14061da177e4SLinus Torvalds else 14074c968709SEric Dumazet ret = ipmr_mfc_add(net, mrt, &mfc, 1408660b26dcSNicolas Dichtel sk == rtnl_dereference(mrt->mroute_sk), 1409660b26dcSNicolas Dichtel parent); 141029e97d21SNikolay Aleksandrov break; 14117ef8f65dSNikolay Aleksandrov /* Control PIM assert. */ 14121da177e4SLinus Torvalds case MRT_ASSERT: 141329e97d21SNikolay Aleksandrov if (optlen != sizeof(val)) { 141429e97d21SNikolay Aleksandrov ret = -EINVAL; 141529e97d21SNikolay Aleksandrov break; 14161da177e4SLinus Torvalds } 141729e97d21SNikolay Aleksandrov if (get_user(val, (int __user *)optval)) { 141829e97d21SNikolay Aleksandrov ret = -EFAULT; 141929e97d21SNikolay Aleksandrov break; 142029e97d21SNikolay Aleksandrov } 142129e97d21SNikolay Aleksandrov mrt->mroute_do_assert = val; 142229e97d21SNikolay Aleksandrov break; 14231da177e4SLinus Torvalds case MRT_PIM: 14241973a4eaSNikolay Aleksandrov if (!ipmr_pimsm_enabled()) { 142529e97d21SNikolay Aleksandrov ret = -ENOPROTOOPT; 142629e97d21SNikolay Aleksandrov break; 14271da177e4SLinus Torvalds } 142829e97d21SNikolay Aleksandrov if (optlen != sizeof(val)) { 142929e97d21SNikolay Aleksandrov ret = -EINVAL; 143029e97d21SNikolay Aleksandrov break; 14311da177e4SLinus Torvalds } 143229e97d21SNikolay Aleksandrov if (get_user(val, (int __user *)optval)) { 143329e97d21SNikolay Aleksandrov ret = -EFAULT; 143429e97d21SNikolay Aleksandrov break; 143529e97d21SNikolay Aleksandrov } 143629e97d21SNikolay Aleksandrov 143729e97d21SNikolay Aleksandrov val = !!val; 143829e97d21SNikolay Aleksandrov if (val != mrt->mroute_do_pim) { 143929e97d21SNikolay Aleksandrov mrt->mroute_do_pim = val; 144029e97d21SNikolay Aleksandrov mrt->mroute_do_assert = val; 144129e97d21SNikolay Aleksandrov } 144229e97d21SNikolay Aleksandrov break; 1443f0ad0860SPatrick McHardy case MRT_TABLE: 144429e97d21SNikolay Aleksandrov if (!IS_BUILTIN(CONFIG_IP_MROUTE_MULTIPLE_TABLES)) { 144529e97d21SNikolay Aleksandrov ret = -ENOPROTOOPT; 144629e97d21SNikolay Aleksandrov break; 144729e97d21SNikolay Aleksandrov } 144829e97d21SNikolay Aleksandrov if (optlen != sizeof(uval)) { 144929e97d21SNikolay Aleksandrov ret = -EINVAL; 145029e97d21SNikolay Aleksandrov break; 145129e97d21SNikolay Aleksandrov } 145229e97d21SNikolay Aleksandrov if (get_user(uval, (u32 __user *)optval)) { 145329e97d21SNikolay Aleksandrov ret = -EFAULT; 145429e97d21SNikolay Aleksandrov break; 145529e97d21SNikolay Aleksandrov } 1456f0ad0860SPatrick McHardy 14574c968709SEric Dumazet if (sk == rtnl_dereference(mrt->mroute_sk)) { 14584c968709SEric Dumazet ret = -EBUSY; 14594c968709SEric Dumazet } else { 146029e97d21SNikolay Aleksandrov mrt = ipmr_new_table(net, uval); 14611113ebbcSNikolay Aleksandrov if (IS_ERR(mrt)) 14621113ebbcSNikolay Aleksandrov ret = PTR_ERR(mrt); 14635e1859fbSEric Dumazet else 146429e97d21SNikolay Aleksandrov raw_sk(sk)->ipmr_table = uval; 14654c968709SEric Dumazet } 146629e97d21SNikolay Aleksandrov break; 14677ef8f65dSNikolay Aleksandrov /* Spurious command, or MRT_VERSION which you cannot set. */ 14681da177e4SLinus Torvalds default: 146929e97d21SNikolay Aleksandrov ret = -ENOPROTOOPT; 14701da177e4SLinus Torvalds } 147129e97d21SNikolay Aleksandrov out_unlock: 147229e97d21SNikolay Aleksandrov rtnl_unlock(); 147329e97d21SNikolay Aleksandrov return ret; 14741da177e4SLinus Torvalds } 14751da177e4SLinus Torvalds 14767ef8f65dSNikolay Aleksandrov /* Getsock opt support for the multicast routing system. */ 14771da177e4SLinus Torvalds int ip_mroute_getsockopt(struct sock *sk, int optname, char __user *optval, int __user *optlen) 14781da177e4SLinus Torvalds { 14791da177e4SLinus Torvalds int olr; 14801da177e4SLinus Torvalds int val; 14814feb88e5SBenjamin Thery struct net *net = sock_net(sk); 1482f0ad0860SPatrick McHardy struct mr_table *mrt; 1483f0ad0860SPatrick McHardy 14845e1859fbSEric Dumazet if (sk->sk_type != SOCK_RAW || 14855e1859fbSEric Dumazet inet_sk(sk)->inet_num != IPPROTO_IGMP) 14865e1859fbSEric Dumazet return -EOPNOTSUPP; 14875e1859fbSEric Dumazet 1488f0ad0860SPatrick McHardy mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT); 148951456b29SIan Morris if (!mrt) 1490f0ad0860SPatrick McHardy return -ENOENT; 14911da177e4SLinus Torvalds 1492fe9ef3ceSNikolay Aleksandrov switch (optname) { 1493fe9ef3ceSNikolay Aleksandrov case MRT_VERSION: 14941da177e4SLinus Torvalds val = 0x0305; 1495fe9ef3ceSNikolay Aleksandrov break; 1496fe9ef3ceSNikolay Aleksandrov case MRT_PIM: 14971973a4eaSNikolay Aleksandrov if (!ipmr_pimsm_enabled()) 1498c316c629SNikolay Aleksandrov return -ENOPROTOOPT; 14990c12295aSPatrick McHardy val = mrt->mroute_do_pim; 1500fe9ef3ceSNikolay Aleksandrov break; 1501fe9ef3ceSNikolay Aleksandrov case MRT_ASSERT: 15020c12295aSPatrick McHardy val = mrt->mroute_do_assert; 1503fe9ef3ceSNikolay Aleksandrov break; 1504fe9ef3ceSNikolay Aleksandrov default: 1505fe9ef3ceSNikolay Aleksandrov return -ENOPROTOOPT; 1506c316c629SNikolay Aleksandrov } 1507fe9ef3ceSNikolay Aleksandrov 1508fe9ef3ceSNikolay Aleksandrov if (get_user(olr, optlen)) 1509fe9ef3ceSNikolay Aleksandrov return -EFAULT; 1510fe9ef3ceSNikolay Aleksandrov olr = min_t(unsigned int, olr, sizeof(int)); 1511fe9ef3ceSNikolay Aleksandrov if (olr < 0) 1512fe9ef3ceSNikolay Aleksandrov return -EINVAL; 1513fe9ef3ceSNikolay Aleksandrov if (put_user(olr, optlen)) 1514fe9ef3ceSNikolay Aleksandrov return -EFAULT; 15151da177e4SLinus Torvalds if (copy_to_user(optval, &val, olr)) 15161da177e4SLinus Torvalds return -EFAULT; 15171da177e4SLinus Torvalds return 0; 15181da177e4SLinus Torvalds } 15191da177e4SLinus Torvalds 15207ef8f65dSNikolay Aleksandrov /* The IP multicast ioctl support routines. */ 15211da177e4SLinus Torvalds int ipmr_ioctl(struct sock *sk, int cmd, void __user *arg) 15221da177e4SLinus Torvalds { 15231da177e4SLinus Torvalds struct sioc_sg_req sr; 15241da177e4SLinus Torvalds struct sioc_vif_req vr; 15251da177e4SLinus Torvalds struct vif_device *vif; 15261da177e4SLinus Torvalds struct mfc_cache *c; 15274feb88e5SBenjamin Thery struct net *net = sock_net(sk); 1528f0ad0860SPatrick McHardy struct mr_table *mrt; 1529f0ad0860SPatrick McHardy 1530f0ad0860SPatrick McHardy mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT); 153151456b29SIan Morris if (!mrt) 1532f0ad0860SPatrick McHardy return -ENOENT; 15331da177e4SLinus Torvalds 1534132adf54SStephen Hemminger switch (cmd) { 15351da177e4SLinus Torvalds case SIOCGETVIFCNT: 15361da177e4SLinus Torvalds if (copy_from_user(&vr, arg, sizeof(vr))) 15371da177e4SLinus Torvalds return -EFAULT; 15380c12295aSPatrick McHardy if (vr.vifi >= mrt->maxvif) 15391da177e4SLinus Torvalds return -EINVAL; 15401da177e4SLinus Torvalds read_lock(&mrt_lock); 15410c12295aSPatrick McHardy vif = &mrt->vif_table[vr.vifi]; 15420c12295aSPatrick McHardy if (VIF_EXISTS(mrt, vr.vifi)) { 15431da177e4SLinus Torvalds vr.icount = vif->pkt_in; 15441da177e4SLinus Torvalds vr.ocount = vif->pkt_out; 15451da177e4SLinus Torvalds vr.ibytes = vif->bytes_in; 15461da177e4SLinus Torvalds vr.obytes = vif->bytes_out; 15471da177e4SLinus Torvalds read_unlock(&mrt_lock); 15481da177e4SLinus Torvalds 15491da177e4SLinus Torvalds if (copy_to_user(arg, &vr, sizeof(vr))) 15501da177e4SLinus Torvalds return -EFAULT; 15511da177e4SLinus Torvalds return 0; 15521da177e4SLinus Torvalds } 15531da177e4SLinus Torvalds read_unlock(&mrt_lock); 15541da177e4SLinus Torvalds return -EADDRNOTAVAIL; 15551da177e4SLinus Torvalds case SIOCGETSGCNT: 15561da177e4SLinus Torvalds if (copy_from_user(&sr, arg, sizeof(sr))) 15571da177e4SLinus Torvalds return -EFAULT; 15581da177e4SLinus Torvalds 1559a8c9486bSEric Dumazet rcu_read_lock(); 15600c12295aSPatrick McHardy c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr); 15611da177e4SLinus Torvalds if (c) { 15621da177e4SLinus Torvalds sr.pktcnt = c->mfc_un.res.pkt; 15631da177e4SLinus Torvalds sr.bytecnt = c->mfc_un.res.bytes; 15641da177e4SLinus Torvalds sr.wrong_if = c->mfc_un.res.wrong_if; 1565a8c9486bSEric Dumazet rcu_read_unlock(); 15661da177e4SLinus Torvalds 15671da177e4SLinus Torvalds if (copy_to_user(arg, &sr, sizeof(sr))) 15681da177e4SLinus Torvalds return -EFAULT; 15691da177e4SLinus Torvalds return 0; 15701da177e4SLinus Torvalds } 1571a8c9486bSEric Dumazet rcu_read_unlock(); 15721da177e4SLinus Torvalds return -EADDRNOTAVAIL; 15731da177e4SLinus Torvalds default: 15741da177e4SLinus Torvalds return -ENOIOCTLCMD; 15751da177e4SLinus Torvalds } 15761da177e4SLinus Torvalds } 15771da177e4SLinus Torvalds 1578709b46e8SEric W. Biederman #ifdef CONFIG_COMPAT 1579709b46e8SEric W. Biederman struct compat_sioc_sg_req { 1580709b46e8SEric W. Biederman struct in_addr src; 1581709b46e8SEric W. Biederman struct in_addr grp; 1582709b46e8SEric W. Biederman compat_ulong_t pktcnt; 1583709b46e8SEric W. Biederman compat_ulong_t bytecnt; 1584709b46e8SEric W. Biederman compat_ulong_t wrong_if; 1585709b46e8SEric W. Biederman }; 1586709b46e8SEric W. Biederman 1587ca6b8bb0SDavid S. Miller struct compat_sioc_vif_req { 1588ca6b8bb0SDavid S. Miller vifi_t vifi; /* Which iface */ 1589ca6b8bb0SDavid S. Miller compat_ulong_t icount; 1590ca6b8bb0SDavid S. Miller compat_ulong_t ocount; 1591ca6b8bb0SDavid S. Miller compat_ulong_t ibytes; 1592ca6b8bb0SDavid S. Miller compat_ulong_t obytes; 1593ca6b8bb0SDavid S. Miller }; 1594ca6b8bb0SDavid S. Miller 1595709b46e8SEric W. Biederman int ipmr_compat_ioctl(struct sock *sk, unsigned int cmd, void __user *arg) 1596709b46e8SEric W. Biederman { 15970033d5adSDavid S. Miller struct compat_sioc_sg_req sr; 1598ca6b8bb0SDavid S. Miller struct compat_sioc_vif_req vr; 1599ca6b8bb0SDavid S. Miller struct vif_device *vif; 1600709b46e8SEric W. Biederman struct mfc_cache *c; 1601709b46e8SEric W. Biederman struct net *net = sock_net(sk); 1602709b46e8SEric W. Biederman struct mr_table *mrt; 1603709b46e8SEric W. Biederman 1604709b46e8SEric W. Biederman mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT); 160551456b29SIan Morris if (!mrt) 1606709b46e8SEric W. Biederman return -ENOENT; 1607709b46e8SEric W. Biederman 1608709b46e8SEric W. Biederman switch (cmd) { 1609ca6b8bb0SDavid S. Miller case SIOCGETVIFCNT: 1610ca6b8bb0SDavid S. Miller if (copy_from_user(&vr, arg, sizeof(vr))) 1611ca6b8bb0SDavid S. Miller return -EFAULT; 1612ca6b8bb0SDavid S. Miller if (vr.vifi >= mrt->maxvif) 1613ca6b8bb0SDavid S. Miller return -EINVAL; 1614ca6b8bb0SDavid S. Miller read_lock(&mrt_lock); 1615ca6b8bb0SDavid S. Miller vif = &mrt->vif_table[vr.vifi]; 1616ca6b8bb0SDavid S. Miller if (VIF_EXISTS(mrt, vr.vifi)) { 1617ca6b8bb0SDavid S. Miller vr.icount = vif->pkt_in; 1618ca6b8bb0SDavid S. Miller vr.ocount = vif->pkt_out; 1619ca6b8bb0SDavid S. Miller vr.ibytes = vif->bytes_in; 1620ca6b8bb0SDavid S. Miller vr.obytes = vif->bytes_out; 1621ca6b8bb0SDavid S. Miller read_unlock(&mrt_lock); 1622ca6b8bb0SDavid S. Miller 1623ca6b8bb0SDavid S. Miller if (copy_to_user(arg, &vr, sizeof(vr))) 1624ca6b8bb0SDavid S. Miller return -EFAULT; 1625ca6b8bb0SDavid S. Miller return 0; 1626ca6b8bb0SDavid S. Miller } 1627ca6b8bb0SDavid S. Miller read_unlock(&mrt_lock); 1628ca6b8bb0SDavid S. Miller return -EADDRNOTAVAIL; 1629709b46e8SEric W. Biederman case SIOCGETSGCNT: 1630709b46e8SEric W. Biederman if (copy_from_user(&sr, arg, sizeof(sr))) 1631709b46e8SEric W. Biederman return -EFAULT; 1632709b46e8SEric W. Biederman 1633709b46e8SEric W. Biederman rcu_read_lock(); 1634709b46e8SEric W. Biederman c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr); 1635709b46e8SEric W. Biederman if (c) { 1636709b46e8SEric W. Biederman sr.pktcnt = c->mfc_un.res.pkt; 1637709b46e8SEric W. Biederman sr.bytecnt = c->mfc_un.res.bytes; 1638709b46e8SEric W. Biederman sr.wrong_if = c->mfc_un.res.wrong_if; 1639709b46e8SEric W. Biederman rcu_read_unlock(); 1640709b46e8SEric W. Biederman 1641709b46e8SEric W. Biederman if (copy_to_user(arg, &sr, sizeof(sr))) 1642709b46e8SEric W. Biederman return -EFAULT; 1643709b46e8SEric W. Biederman return 0; 1644709b46e8SEric W. Biederman } 1645709b46e8SEric W. Biederman rcu_read_unlock(); 1646709b46e8SEric W. Biederman return -EADDRNOTAVAIL; 1647709b46e8SEric W. Biederman default: 1648709b46e8SEric W. Biederman return -ENOIOCTLCMD; 1649709b46e8SEric W. Biederman } 1650709b46e8SEric W. Biederman } 1651709b46e8SEric W. Biederman #endif 1652709b46e8SEric W. Biederman 16531da177e4SLinus Torvalds static int ipmr_device_event(struct notifier_block *this, unsigned long event, void *ptr) 16541da177e4SLinus Torvalds { 1655351638e7SJiri Pirko struct net_device *dev = netdev_notifier_info_to_dev(ptr); 16564feb88e5SBenjamin Thery struct net *net = dev_net(dev); 1657f0ad0860SPatrick McHardy struct mr_table *mrt; 16581da177e4SLinus Torvalds struct vif_device *v; 16591da177e4SLinus Torvalds int ct; 1660e9dc8653SEric W. Biederman 16611da177e4SLinus Torvalds if (event != NETDEV_UNREGISTER) 16621da177e4SLinus Torvalds return NOTIFY_DONE; 1663f0ad0860SPatrick McHardy 1664f0ad0860SPatrick McHardy ipmr_for_each_table(mrt, net) { 16650c12295aSPatrick McHardy v = &mrt->vif_table[0]; 16660c12295aSPatrick McHardy for (ct = 0; ct < mrt->maxvif; ct++, v++) { 1667e9dc8653SEric W. Biederman if (v->dev == dev) 1668e92036a6SRongQing.Li vif_delete(mrt, ct, 1, NULL); 16691da177e4SLinus Torvalds } 16701da177e4SLinus Torvalds } 16711da177e4SLinus Torvalds return NOTIFY_DONE; 16721da177e4SLinus Torvalds } 16731da177e4SLinus Torvalds 16741da177e4SLinus Torvalds static struct notifier_block ip_mr_notifier = { 16751da177e4SLinus Torvalds .notifier_call = ipmr_device_event, 16761da177e4SLinus Torvalds }; 16771da177e4SLinus Torvalds 16787ef8f65dSNikolay Aleksandrov /* Encapsulate a packet by attaching a valid IPIP header to it. 16791da177e4SLinus Torvalds * This avoids tunnel drivers and other mess and gives us the speed so 16801da177e4SLinus Torvalds * important for multicast video. 16811da177e4SLinus Torvalds */ 1682b6a7719aSHannes Frederic Sowa static void ip_encap(struct net *net, struct sk_buff *skb, 1683b6a7719aSHannes Frederic Sowa __be32 saddr, __be32 daddr) 16841da177e4SLinus Torvalds { 16858856dfa3SArnaldo Carvalho de Melo struct iphdr *iph; 1686b71d1d42SEric Dumazet const struct iphdr *old_iph = ip_hdr(skb); 16878856dfa3SArnaldo Carvalho de Melo 16888856dfa3SArnaldo Carvalho de Melo skb_push(skb, sizeof(struct iphdr)); 1689b0e380b1SArnaldo Carvalho de Melo skb->transport_header = skb->network_header; 16908856dfa3SArnaldo Carvalho de Melo skb_reset_network_header(skb); 1691eddc9ec5SArnaldo Carvalho de Melo iph = ip_hdr(skb); 16921da177e4SLinus Torvalds 16931da177e4SLinus Torvalds iph->version = 4; 1694e023dd64SArnaldo Carvalho de Melo iph->tos = old_iph->tos; 1695e023dd64SArnaldo Carvalho de Melo iph->ttl = old_iph->ttl; 16961da177e4SLinus Torvalds iph->frag_off = 0; 16971da177e4SLinus Torvalds iph->daddr = daddr; 16981da177e4SLinus Torvalds iph->saddr = saddr; 16991da177e4SLinus Torvalds iph->protocol = IPPROTO_IPIP; 17001da177e4SLinus Torvalds iph->ihl = 5; 17011da177e4SLinus Torvalds iph->tot_len = htons(skb->len); 1702b6a7719aSHannes Frederic Sowa ip_select_ident(net, skb, NULL); 17031da177e4SLinus Torvalds ip_send_check(iph); 17041da177e4SLinus Torvalds 17051da177e4SLinus Torvalds memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt)); 17061da177e4SLinus Torvalds nf_reset(skb); 17071da177e4SLinus Torvalds } 17081da177e4SLinus Torvalds 17090c4b51f0SEric W. Biederman static inline int ipmr_forward_finish(struct net *net, struct sock *sk, 17100c4b51f0SEric W. Biederman struct sk_buff *skb) 17111da177e4SLinus Torvalds { 17121da177e4SLinus Torvalds struct ip_options *opt = &(IPCB(skb)->opt); 17131da177e4SLinus Torvalds 171473186df8SDavid S. Miller IP_INC_STATS(net, IPSTATS_MIB_OUTFORWDATAGRAMS); 171573186df8SDavid S. Miller IP_ADD_STATS(net, IPSTATS_MIB_OUTOCTETS, skb->len); 17161da177e4SLinus Torvalds 17171da177e4SLinus Torvalds if (unlikely(opt->optlen)) 17181da177e4SLinus Torvalds ip_forward_options(skb); 17191da177e4SLinus Torvalds 172013206b6bSEric W. Biederman return dst_output(net, sk, skb); 17211da177e4SLinus Torvalds } 17221da177e4SLinus Torvalds 17237ef8f65dSNikolay Aleksandrov /* Processing handlers for ipmr_forward */ 17241da177e4SLinus Torvalds 17250c12295aSPatrick McHardy static void ipmr_queue_xmit(struct net *net, struct mr_table *mrt, 17260c12295aSPatrick McHardy struct sk_buff *skb, struct mfc_cache *c, int vifi) 17271da177e4SLinus Torvalds { 1728eddc9ec5SArnaldo Carvalho de Melo const struct iphdr *iph = ip_hdr(skb); 17290c12295aSPatrick McHardy struct vif_device *vif = &mrt->vif_table[vifi]; 17301da177e4SLinus Torvalds struct net_device *dev; 17311da177e4SLinus Torvalds struct rtable *rt; 173231e4543dSDavid S. Miller struct flowi4 fl4; 17331da177e4SLinus Torvalds int encap = 0; 17341da177e4SLinus Torvalds 173551456b29SIan Morris if (!vif->dev) 17361da177e4SLinus Torvalds goto out_free; 17371da177e4SLinus Torvalds 17381da177e4SLinus Torvalds if (vif->flags & VIFF_REGISTER) { 17391da177e4SLinus Torvalds vif->pkt_out++; 17401da177e4SLinus Torvalds vif->bytes_out += skb->len; 1741cf3677aeSPavel Emelyanov vif->dev->stats.tx_bytes += skb->len; 1742cf3677aeSPavel Emelyanov vif->dev->stats.tx_packets++; 17430c12295aSPatrick McHardy ipmr_cache_report(mrt, skb, vifi, IGMPMSG_WHOLEPKT); 174469ebbf58SIlpo Järvinen goto out_free; 17451da177e4SLinus Torvalds } 17461da177e4SLinus Torvalds 17471da177e4SLinus Torvalds if (vif->flags & VIFF_TUNNEL) { 174831e4543dSDavid S. Miller rt = ip_route_output_ports(net, &fl4, NULL, 174978fbfd8aSDavid S. Miller vif->remote, vif->local, 175078fbfd8aSDavid S. Miller 0, 0, 175178fbfd8aSDavid S. Miller IPPROTO_IPIP, 175278fbfd8aSDavid S. Miller RT_TOS(iph->tos), vif->link); 1753b23dd4feSDavid S. Miller if (IS_ERR(rt)) 17541da177e4SLinus Torvalds goto out_free; 17551da177e4SLinus Torvalds encap = sizeof(struct iphdr); 17561da177e4SLinus Torvalds } else { 175731e4543dSDavid S. Miller rt = ip_route_output_ports(net, &fl4, NULL, iph->daddr, 0, 175878fbfd8aSDavid S. Miller 0, 0, 175978fbfd8aSDavid S. Miller IPPROTO_IPIP, 176078fbfd8aSDavid S. Miller RT_TOS(iph->tos), vif->link); 1761b23dd4feSDavid S. Miller if (IS_ERR(rt)) 17621da177e4SLinus Torvalds goto out_free; 17631da177e4SLinus Torvalds } 17641da177e4SLinus Torvalds 1765d8d1f30bSChangli Gao dev = rt->dst.dev; 17661da177e4SLinus Torvalds 1767d8d1f30bSChangli Gao if (skb->len+encap > dst_mtu(&rt->dst) && (ntohs(iph->frag_off) & IP_DF)) { 17681da177e4SLinus Torvalds /* Do not fragment multicasts. Alas, IPv4 does not 1769a8cb16ddSEric Dumazet * allow to send ICMP, so that packets will disappear 1770a8cb16ddSEric Dumazet * to blackhole. 17711da177e4SLinus Torvalds */ 177273186df8SDavid S. Miller IP_INC_STATS(net, IPSTATS_MIB_FRAGFAILS); 17731da177e4SLinus Torvalds ip_rt_put(rt); 17741da177e4SLinus Torvalds goto out_free; 17751da177e4SLinus Torvalds } 17761da177e4SLinus Torvalds 1777d8d1f30bSChangli Gao encap += LL_RESERVED_SPACE(dev) + rt->dst.header_len; 17781da177e4SLinus Torvalds 17791da177e4SLinus Torvalds if (skb_cow(skb, encap)) { 17801da177e4SLinus Torvalds ip_rt_put(rt); 17811da177e4SLinus Torvalds goto out_free; 17821da177e4SLinus Torvalds } 17831da177e4SLinus Torvalds 17841da177e4SLinus Torvalds vif->pkt_out++; 17851da177e4SLinus Torvalds vif->bytes_out += skb->len; 17861da177e4SLinus Torvalds 1787adf30907SEric Dumazet skb_dst_drop(skb); 1788d8d1f30bSChangli Gao skb_dst_set(skb, &rt->dst); 1789eddc9ec5SArnaldo Carvalho de Melo ip_decrease_ttl(ip_hdr(skb)); 17901da177e4SLinus Torvalds 17911da177e4SLinus Torvalds /* FIXME: forward and output firewalls used to be called here. 1792a8cb16ddSEric Dumazet * What do we do with netfilter? -- RR 1793a8cb16ddSEric Dumazet */ 17941da177e4SLinus Torvalds if (vif->flags & VIFF_TUNNEL) { 1795b6a7719aSHannes Frederic Sowa ip_encap(net, skb, vif->local, vif->remote); 17961da177e4SLinus Torvalds /* FIXME: extra output firewall step used to be here. --RR */ 17972f4c02d4SPavel Emelyanov vif->dev->stats.tx_packets++; 17982f4c02d4SPavel Emelyanov vif->dev->stats.tx_bytes += skb->len; 17991da177e4SLinus Torvalds } 18001da177e4SLinus Torvalds 18019ee6c5dcSLance Richardson IPCB(skb)->flags |= IPSKB_FORWARDED; 18021da177e4SLinus Torvalds 18037ef8f65dSNikolay Aleksandrov /* RFC1584 teaches, that DVMRP/PIM router must deliver packets locally 18041da177e4SLinus Torvalds * not only before forwarding, but after forwarding on all output 18051da177e4SLinus Torvalds * interfaces. It is clear, if mrouter runs a multicasting 18061da177e4SLinus Torvalds * program, it should receive packets not depending to what interface 18071da177e4SLinus Torvalds * program is joined. 18081da177e4SLinus Torvalds * If we will not make it, the program will have to join on all 18091da177e4SLinus Torvalds * interfaces. On the other hand, multihoming host (or router, but 18101da177e4SLinus Torvalds * not mrouter) cannot join to more than one interface - it will 18111da177e4SLinus Torvalds * result in receiving multiple packets. 18121da177e4SLinus Torvalds */ 181329a26a56SEric W. Biederman NF_HOOK(NFPROTO_IPV4, NF_INET_FORWARD, 181429a26a56SEric W. Biederman net, NULL, skb, skb->dev, dev, 18151da177e4SLinus Torvalds ipmr_forward_finish); 18161da177e4SLinus Torvalds return; 18171da177e4SLinus Torvalds 18181da177e4SLinus Torvalds out_free: 18191da177e4SLinus Torvalds kfree_skb(skb); 18201da177e4SLinus Torvalds } 18211da177e4SLinus Torvalds 18220c12295aSPatrick McHardy static int ipmr_find_vif(struct mr_table *mrt, struct net_device *dev) 18231da177e4SLinus Torvalds { 18241da177e4SLinus Torvalds int ct; 18250c12295aSPatrick McHardy 18260c12295aSPatrick McHardy for (ct = mrt->maxvif-1; ct >= 0; ct--) { 18270c12295aSPatrick McHardy if (mrt->vif_table[ct].dev == dev) 18281da177e4SLinus Torvalds break; 18291da177e4SLinus Torvalds } 18301da177e4SLinus Torvalds return ct; 18311da177e4SLinus Torvalds } 18321da177e4SLinus Torvalds 18331da177e4SLinus Torvalds /* "local" means that we should preserve one skb (for local delivery) */ 1834c4854ec8SRami Rosen static void ip_mr_forward(struct net *net, struct mr_table *mrt, 1835*4b1f0d33SDonald Sharp struct net_device *dev, struct sk_buff *skb, 1836*4b1f0d33SDonald Sharp struct mfc_cache *cache, int local) 18371da177e4SLinus Torvalds { 1838*4b1f0d33SDonald Sharp int true_vifi = ipmr_find_vif(mrt, dev); 18391da177e4SLinus Torvalds int psend = -1; 18401da177e4SLinus Torvalds int vif, ct; 18411da177e4SLinus Torvalds 18421da177e4SLinus Torvalds vif = cache->mfc_parent; 18431da177e4SLinus Torvalds cache->mfc_un.res.pkt++; 18441da177e4SLinus Torvalds cache->mfc_un.res.bytes += skb->len; 184543b9e127SNikolay Aleksandrov cache->mfc_un.res.lastuse = jiffies; 18461da177e4SLinus Torvalds 1847360eb5daSNicolas Dichtel if (cache->mfc_origin == htonl(INADDR_ANY) && true_vifi >= 0) { 1848660b26dcSNicolas Dichtel struct mfc_cache *cache_proxy; 1849660b26dcSNicolas Dichtel 1850660b26dcSNicolas Dichtel /* For an (*,G) entry, we only check that the incomming 1851660b26dcSNicolas Dichtel * interface is part of the static tree. 1852660b26dcSNicolas Dichtel */ 1853660b26dcSNicolas Dichtel cache_proxy = ipmr_cache_find_any_parent(mrt, vif); 1854660b26dcSNicolas Dichtel if (cache_proxy && 1855660b26dcSNicolas Dichtel cache_proxy->mfc_un.res.ttls[true_vifi] < 255) 1856660b26dcSNicolas Dichtel goto forward; 1857660b26dcSNicolas Dichtel } 1858660b26dcSNicolas Dichtel 18597ef8f65dSNikolay Aleksandrov /* Wrong interface: drop packet and (maybe) send PIM assert. */ 1860*4b1f0d33SDonald Sharp if (mrt->vif_table[vif].dev != dev) { 1861c7537967SDavid S. Miller if (rt_is_output_route(skb_rtable(skb))) { 18621da177e4SLinus Torvalds /* It is our own packet, looped back. 1863a8cb16ddSEric Dumazet * Very complicated situation... 1864a8cb16ddSEric Dumazet * 1865a8cb16ddSEric Dumazet * The best workaround until routing daemons will be 1866a8cb16ddSEric Dumazet * fixed is not to redistribute packet, if it was 1867a8cb16ddSEric Dumazet * send through wrong interface. It means, that 1868a8cb16ddSEric Dumazet * multicast applications WILL NOT work for 1869a8cb16ddSEric Dumazet * (S,G), which have default multicast route pointing 1870a8cb16ddSEric Dumazet * to wrong oif. In any case, it is not a good 1871a8cb16ddSEric Dumazet * idea to use multicasting applications on router. 18721da177e4SLinus Torvalds */ 18731da177e4SLinus Torvalds goto dont_forward; 18741da177e4SLinus Torvalds } 18751da177e4SLinus Torvalds 18761da177e4SLinus Torvalds cache->mfc_un.res.wrong_if++; 18771da177e4SLinus Torvalds 18780c12295aSPatrick McHardy if (true_vifi >= 0 && mrt->mroute_do_assert && 18791da177e4SLinus Torvalds /* pimsm uses asserts, when switching from RPT to SPT, 1880a8cb16ddSEric Dumazet * so that we cannot check that packet arrived on an oif. 1881a8cb16ddSEric Dumazet * It is bad, but otherwise we would need to move pretty 1882a8cb16ddSEric Dumazet * large chunk of pimd to kernel. Ough... --ANK 18831da177e4SLinus Torvalds */ 18840c12295aSPatrick McHardy (mrt->mroute_do_pim || 18856f9374a9SBenjamin Thery cache->mfc_un.res.ttls[true_vifi] < 255) && 18861da177e4SLinus Torvalds time_after(jiffies, 18871da177e4SLinus Torvalds cache->mfc_un.res.last_assert + MFC_ASSERT_THRESH)) { 18881da177e4SLinus Torvalds cache->mfc_un.res.last_assert = jiffies; 18890c12295aSPatrick McHardy ipmr_cache_report(mrt, skb, true_vifi, IGMPMSG_WRONGVIF); 18901da177e4SLinus Torvalds } 18911da177e4SLinus Torvalds goto dont_forward; 18921da177e4SLinus Torvalds } 18931da177e4SLinus Torvalds 1894660b26dcSNicolas Dichtel forward: 18950c12295aSPatrick McHardy mrt->vif_table[vif].pkt_in++; 18960c12295aSPatrick McHardy mrt->vif_table[vif].bytes_in += skb->len; 18971da177e4SLinus Torvalds 18987ef8f65dSNikolay Aleksandrov /* Forward the frame */ 1899360eb5daSNicolas Dichtel if (cache->mfc_origin == htonl(INADDR_ANY) && 1900360eb5daSNicolas Dichtel cache->mfc_mcastgrp == htonl(INADDR_ANY)) { 1901660b26dcSNicolas Dichtel if (true_vifi >= 0 && 1902660b26dcSNicolas Dichtel true_vifi != cache->mfc_parent && 1903660b26dcSNicolas Dichtel ip_hdr(skb)->ttl > 1904660b26dcSNicolas Dichtel cache->mfc_un.res.ttls[cache->mfc_parent]) { 1905660b26dcSNicolas Dichtel /* It's an (*,*) entry and the packet is not coming from 1906660b26dcSNicolas Dichtel * the upstream: forward the packet to the upstream 1907660b26dcSNicolas Dichtel * only. 1908660b26dcSNicolas Dichtel */ 1909660b26dcSNicolas Dichtel psend = cache->mfc_parent; 1910660b26dcSNicolas Dichtel goto last_forward; 1911660b26dcSNicolas Dichtel } 1912660b26dcSNicolas Dichtel goto dont_forward; 1913660b26dcSNicolas Dichtel } 1914a8cb16ddSEric Dumazet for (ct = cache->mfc_un.res.maxvif - 1; 1915a8cb16ddSEric Dumazet ct >= cache->mfc_un.res.minvif; ct--) { 1916660b26dcSNicolas Dichtel /* For (*,G) entry, don't forward to the incoming interface */ 1917360eb5daSNicolas Dichtel if ((cache->mfc_origin != htonl(INADDR_ANY) || 1918360eb5daSNicolas Dichtel ct != true_vifi) && 1919660b26dcSNicolas Dichtel ip_hdr(skb)->ttl > cache->mfc_un.res.ttls[ct]) { 19201da177e4SLinus Torvalds if (psend != -1) { 19211da177e4SLinus Torvalds struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC); 1922a8cb16ddSEric Dumazet 19231da177e4SLinus Torvalds if (skb2) 19240c12295aSPatrick McHardy ipmr_queue_xmit(net, mrt, skb2, cache, 19250c12295aSPatrick McHardy psend); 19261da177e4SLinus Torvalds } 19271da177e4SLinus Torvalds psend = ct; 19281da177e4SLinus Torvalds } 19291da177e4SLinus Torvalds } 1930660b26dcSNicolas Dichtel last_forward: 19311da177e4SLinus Torvalds if (psend != -1) { 19321da177e4SLinus Torvalds if (local) { 19331da177e4SLinus Torvalds struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC); 1934a8cb16ddSEric Dumazet 19351da177e4SLinus Torvalds if (skb2) 19360c12295aSPatrick McHardy ipmr_queue_xmit(net, mrt, skb2, cache, psend); 19371da177e4SLinus Torvalds } else { 19380c12295aSPatrick McHardy ipmr_queue_xmit(net, mrt, skb, cache, psend); 1939c4854ec8SRami Rosen return; 19401da177e4SLinus Torvalds } 19411da177e4SLinus Torvalds } 19421da177e4SLinus Torvalds 19431da177e4SLinus Torvalds dont_forward: 19441da177e4SLinus Torvalds if (!local) 19451da177e4SLinus Torvalds kfree_skb(skb); 19461da177e4SLinus Torvalds } 19471da177e4SLinus Torvalds 1948417da66fSDavid S. Miller static struct mr_table *ipmr_rt_fib_lookup(struct net *net, struct sk_buff *skb) 1949ee3f1aafSDavid S. Miller { 1950417da66fSDavid S. Miller struct rtable *rt = skb_rtable(skb); 1951417da66fSDavid S. Miller struct iphdr *iph = ip_hdr(skb); 1952da91981bSDavid S. Miller struct flowi4 fl4 = { 1953417da66fSDavid S. Miller .daddr = iph->daddr, 1954417da66fSDavid S. Miller .saddr = iph->saddr, 1955b0fe4a31SJulian Anastasov .flowi4_tos = RT_TOS(iph->tos), 19564fd551d7SDavid S. Miller .flowi4_oif = (rt_is_output_route(rt) ? 19574fd551d7SDavid S. Miller skb->dev->ifindex : 0), 19584fd551d7SDavid S. Miller .flowi4_iif = (rt_is_output_route(rt) ? 19591fb9489bSPavel Emelyanov LOOPBACK_IFINDEX : 19604fd551d7SDavid S. Miller skb->dev->ifindex), 1961b4869889SDavid Miller .flowi4_mark = skb->mark, 1962ee3f1aafSDavid S. Miller }; 1963ee3f1aafSDavid S. Miller struct mr_table *mrt; 1964ee3f1aafSDavid S. Miller int err; 1965ee3f1aafSDavid S. Miller 1966da91981bSDavid S. Miller err = ipmr_fib_lookup(net, &fl4, &mrt); 1967ee3f1aafSDavid S. Miller if (err) 1968ee3f1aafSDavid S. Miller return ERR_PTR(err); 1969ee3f1aafSDavid S. Miller return mrt; 1970ee3f1aafSDavid S. Miller } 19711da177e4SLinus Torvalds 19727ef8f65dSNikolay Aleksandrov /* Multicast packets for forwarding arrive here 19734c968709SEric Dumazet * Called with rcu_read_lock(); 19741da177e4SLinus Torvalds */ 19751da177e4SLinus Torvalds int ip_mr_input(struct sk_buff *skb) 19761da177e4SLinus Torvalds { 19771da177e4SLinus Torvalds struct mfc_cache *cache; 19784feb88e5SBenjamin Thery struct net *net = dev_net(skb->dev); 1979511c3f92SEric Dumazet int local = skb_rtable(skb)->rt_flags & RTCF_LOCAL; 1980f0ad0860SPatrick McHardy struct mr_table *mrt; 1981bcfc7d33SThomas Winter struct net_device *dev; 1982bcfc7d33SThomas Winter 1983bcfc7d33SThomas Winter /* skb->dev passed in is the loX master dev for vrfs. 1984bcfc7d33SThomas Winter * As there are no vifs associated with loopback devices, 1985bcfc7d33SThomas Winter * get the proper interface that does have a vif associated with it. 1986bcfc7d33SThomas Winter */ 1987bcfc7d33SThomas Winter dev = skb->dev; 1988bcfc7d33SThomas Winter if (netif_is_l3_master(skb->dev)) { 1989bcfc7d33SThomas Winter dev = dev_get_by_index_rcu(net, IPCB(skb)->iif); 1990bcfc7d33SThomas Winter if (!dev) { 1991bcfc7d33SThomas Winter kfree_skb(skb); 1992bcfc7d33SThomas Winter return -ENODEV; 1993bcfc7d33SThomas Winter } 1994bcfc7d33SThomas Winter } 19951da177e4SLinus Torvalds 19961da177e4SLinus Torvalds /* Packet is looped back after forward, it should not be 1997a8cb16ddSEric Dumazet * forwarded second time, but still can be delivered locally. 19981da177e4SLinus Torvalds */ 19991da177e4SLinus Torvalds if (IPCB(skb)->flags & IPSKB_FORWARDED) 20001da177e4SLinus Torvalds goto dont_forward; 20011da177e4SLinus Torvalds 2002417da66fSDavid S. Miller mrt = ipmr_rt_fib_lookup(net, skb); 2003ee3f1aafSDavid S. Miller if (IS_ERR(mrt)) { 2004e40dbc51SBen Greear kfree_skb(skb); 2005ee3f1aafSDavid S. Miller return PTR_ERR(mrt); 20065e2b61f7SDavid S. Miller } 20071da177e4SLinus Torvalds if (!local) { 20081da177e4SLinus Torvalds if (IPCB(skb)->opt.router_alert) { 20091da177e4SLinus Torvalds if (ip_call_ra_chain(skb)) 20101da177e4SLinus Torvalds return 0; 2011eddc9ec5SArnaldo Carvalho de Melo } else if (ip_hdr(skb)->protocol == IPPROTO_IGMP) { 20121da177e4SLinus Torvalds /* IGMPv1 (and broken IGMPv2 implementations sort of 20134c968709SEric Dumazet * Cisco IOS <= 11.2(8)) do not put router alert 20144c968709SEric Dumazet * option to IGMP packets destined to routable 20154c968709SEric Dumazet * groups. It is very bad, because it means 20164c968709SEric Dumazet * that we can forward NO IGMP messages. 20171da177e4SLinus Torvalds */ 20184c968709SEric Dumazet struct sock *mroute_sk; 20194c968709SEric Dumazet 20204c968709SEric Dumazet mroute_sk = rcu_dereference(mrt->mroute_sk); 20214c968709SEric Dumazet if (mroute_sk) { 20222715bcf9SPatrick McHardy nf_reset(skb); 20234c968709SEric Dumazet raw_rcv(mroute_sk, skb); 20241da177e4SLinus Torvalds return 0; 20251da177e4SLinus Torvalds } 20261da177e4SLinus Torvalds } 20271da177e4SLinus Torvalds } 20281da177e4SLinus Torvalds 2029a8c9486bSEric Dumazet /* already under rcu_read_lock() */ 20300c12295aSPatrick McHardy cache = ipmr_cache_find(mrt, ip_hdr(skb)->saddr, ip_hdr(skb)->daddr); 203151456b29SIan Morris if (!cache) { 2032bcfc7d33SThomas Winter int vif = ipmr_find_vif(mrt, dev); 2033660b26dcSNicolas Dichtel 2034660b26dcSNicolas Dichtel if (vif >= 0) 2035660b26dcSNicolas Dichtel cache = ipmr_cache_find_any(mrt, ip_hdr(skb)->daddr, 2036660b26dcSNicolas Dichtel vif); 2037660b26dcSNicolas Dichtel } 20381da177e4SLinus Torvalds 20397ef8f65dSNikolay Aleksandrov /* No usable cache entry */ 204051456b29SIan Morris if (!cache) { 20411da177e4SLinus Torvalds int vif; 20421da177e4SLinus Torvalds 20431da177e4SLinus Torvalds if (local) { 20441da177e4SLinus Torvalds struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC); 20451da177e4SLinus Torvalds ip_local_deliver(skb); 204651456b29SIan Morris if (!skb2) 20471da177e4SLinus Torvalds return -ENOBUFS; 20481da177e4SLinus Torvalds skb = skb2; 20491da177e4SLinus Torvalds } 20501da177e4SLinus Torvalds 2051a8c9486bSEric Dumazet read_lock(&mrt_lock); 2052bcfc7d33SThomas Winter vif = ipmr_find_vif(mrt, dev); 20531da177e4SLinus Torvalds if (vif >= 0) { 2054*4b1f0d33SDonald Sharp int err2 = ipmr_cache_unresolved(mrt, vif, skb, dev); 20551da177e4SLinus Torvalds read_unlock(&mrt_lock); 20561da177e4SLinus Torvalds 20570eae88f3SEric Dumazet return err2; 20581da177e4SLinus Torvalds } 20591da177e4SLinus Torvalds read_unlock(&mrt_lock); 20601da177e4SLinus Torvalds kfree_skb(skb); 20611da177e4SLinus Torvalds return -ENODEV; 20621da177e4SLinus Torvalds } 20631da177e4SLinus Torvalds 2064a8c9486bSEric Dumazet read_lock(&mrt_lock); 2065*4b1f0d33SDonald Sharp ip_mr_forward(net, mrt, dev, skb, cache, local); 20661da177e4SLinus Torvalds read_unlock(&mrt_lock); 20671da177e4SLinus Torvalds 20681da177e4SLinus Torvalds if (local) 20691da177e4SLinus Torvalds return ip_local_deliver(skb); 20701da177e4SLinus Torvalds 20711da177e4SLinus Torvalds return 0; 20721da177e4SLinus Torvalds 20731da177e4SLinus Torvalds dont_forward: 20741da177e4SLinus Torvalds if (local) 20751da177e4SLinus Torvalds return ip_local_deliver(skb); 20761da177e4SLinus Torvalds kfree_skb(skb); 20771da177e4SLinus Torvalds return 0; 20781da177e4SLinus Torvalds } 20791da177e4SLinus Torvalds 2080b1879204SIlpo Järvinen #ifdef CONFIG_IP_PIMSM_V1 20817ef8f65dSNikolay Aleksandrov /* Handle IGMP messages of PIMv1 */ 2082b1879204SIlpo Järvinen int pim_rcv_v1(struct sk_buff *skb) 2083b1879204SIlpo Järvinen { 2084b1879204SIlpo Järvinen struct igmphdr *pim; 20854feb88e5SBenjamin Thery struct net *net = dev_net(skb->dev); 2086f0ad0860SPatrick McHardy struct mr_table *mrt; 2087b1879204SIlpo Järvinen 2088b1879204SIlpo Järvinen if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr))) 2089b1879204SIlpo Järvinen goto drop; 2090b1879204SIlpo Järvinen 2091b1879204SIlpo Järvinen pim = igmp_hdr(skb); 2092b1879204SIlpo Järvinen 2093417da66fSDavid S. Miller mrt = ipmr_rt_fib_lookup(net, skb); 2094ee3f1aafSDavid S. Miller if (IS_ERR(mrt)) 2095f0ad0860SPatrick McHardy goto drop; 20960c12295aSPatrick McHardy if (!mrt->mroute_do_pim || 2097b1879204SIlpo Järvinen pim->group != PIM_V1_VERSION || pim->code != PIM_V1_REGISTER) 2098b1879204SIlpo Järvinen goto drop; 2099b1879204SIlpo Järvinen 2100f0ad0860SPatrick McHardy if (__pim_rcv(mrt, skb, sizeof(*pim))) { 21011da177e4SLinus Torvalds drop: 21021da177e4SLinus Torvalds kfree_skb(skb); 2103b1879204SIlpo Järvinen } 21041da177e4SLinus Torvalds return 0; 21051da177e4SLinus Torvalds } 21061da177e4SLinus Torvalds #endif 21071da177e4SLinus Torvalds 21081da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM_V2 21091da177e4SLinus Torvalds static int pim_rcv(struct sk_buff *skb) 21101da177e4SLinus Torvalds { 21111da177e4SLinus Torvalds struct pimreghdr *pim; 2112f0ad0860SPatrick McHardy struct net *net = dev_net(skb->dev); 2113f0ad0860SPatrick McHardy struct mr_table *mrt; 21141da177e4SLinus Torvalds 2115b1879204SIlpo Järvinen if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr))) 21161da177e4SLinus Torvalds goto drop; 21171da177e4SLinus Torvalds 21189c70220bSArnaldo Carvalho de Melo pim = (struct pimreghdr *)skb_transport_header(skb); 211956245caeSNikolay Aleksandrov if (pim->type != ((PIM_VERSION << 4) | (PIM_TYPE_REGISTER)) || 21201da177e4SLinus Torvalds (pim->flags & PIM_NULL_REGISTER) || 21211da177e4SLinus Torvalds (ip_compute_csum((void *)pim, sizeof(*pim)) != 0 && 2122d3bc23e7SAl Viro csum_fold(skb_checksum(skb, 0, skb->len, 0)))) 21231da177e4SLinus Torvalds goto drop; 21241da177e4SLinus Torvalds 2125417da66fSDavid S. Miller mrt = ipmr_rt_fib_lookup(net, skb); 2126ee3f1aafSDavid S. Miller if (IS_ERR(mrt)) 2127f0ad0860SPatrick McHardy goto drop; 2128f0ad0860SPatrick McHardy if (__pim_rcv(mrt, skb, sizeof(*pim))) { 21291da177e4SLinus Torvalds drop: 21301da177e4SLinus Torvalds kfree_skb(skb); 2131b1879204SIlpo Järvinen } 21321da177e4SLinus Torvalds return 0; 21331da177e4SLinus Torvalds } 21341da177e4SLinus Torvalds #endif 21351da177e4SLinus Torvalds 2136cb6a4e46SPatrick McHardy static int __ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb, 2137cb6a4e46SPatrick McHardy struct mfc_cache *c, struct rtmsg *rtm) 21381da177e4SLinus Torvalds { 2139adfa85e4SNicolas Dichtel struct rta_mfc_stats mfcs; 214043b9e127SNikolay Aleksandrov struct nlattr *mp_attr; 214143b9e127SNikolay Aleksandrov struct rtnexthop *nhp; 2142b5036cd4SNikolay Aleksandrov unsigned long lastuse; 214343b9e127SNikolay Aleksandrov int ct; 21441da177e4SLinus Torvalds 21457438189bSNicolas Dichtel /* If cache is unresolved, don't try to parse IIF and OIF */ 21461708ebc9SNikolay Aleksandrov if (c->mfc_parent >= MAXVIFS) { 21471708ebc9SNikolay Aleksandrov rtm->rtm_flags |= RTNH_F_UNRESOLVED; 21487438189bSNicolas Dichtel return -ENOENT; 21491708ebc9SNikolay Aleksandrov } 21507438189bSNicolas Dichtel 215192a395e5SThomas Graf if (VIF_EXISTS(mrt, c->mfc_parent) && 215292a395e5SThomas Graf nla_put_u32(skb, RTA_IIF, mrt->vif_table[c->mfc_parent].dev->ifindex) < 0) 215392a395e5SThomas Graf return -EMSGSIZE; 21541da177e4SLinus Torvalds 215592a395e5SThomas Graf if (!(mp_attr = nla_nest_start(skb, RTA_MULTIPATH))) 215692a395e5SThomas Graf return -EMSGSIZE; 21571da177e4SLinus Torvalds 21581da177e4SLinus Torvalds for (ct = c->mfc_un.res.minvif; ct < c->mfc_un.res.maxvif; ct++) { 21590c12295aSPatrick McHardy if (VIF_EXISTS(mrt, ct) && c->mfc_un.res.ttls[ct] < 255) { 216092a395e5SThomas Graf if (!(nhp = nla_reserve_nohdr(skb, sizeof(*nhp)))) { 216192a395e5SThomas Graf nla_nest_cancel(skb, mp_attr); 216292a395e5SThomas Graf return -EMSGSIZE; 216392a395e5SThomas Graf } 216492a395e5SThomas Graf 21651da177e4SLinus Torvalds nhp->rtnh_flags = 0; 21661da177e4SLinus Torvalds nhp->rtnh_hops = c->mfc_un.res.ttls[ct]; 21670c12295aSPatrick McHardy nhp->rtnh_ifindex = mrt->vif_table[ct].dev->ifindex; 21681da177e4SLinus Torvalds nhp->rtnh_len = sizeof(*nhp); 21691da177e4SLinus Torvalds } 21701da177e4SLinus Torvalds } 217192a395e5SThomas Graf 217292a395e5SThomas Graf nla_nest_end(skb, mp_attr); 217392a395e5SThomas Graf 2174b5036cd4SNikolay Aleksandrov lastuse = READ_ONCE(c->mfc_un.res.lastuse); 2175b5036cd4SNikolay Aleksandrov lastuse = time_after_eq(jiffies, lastuse) ? jiffies - lastuse : 0; 2176b5036cd4SNikolay Aleksandrov 2177adfa85e4SNicolas Dichtel mfcs.mfcs_packets = c->mfc_un.res.pkt; 2178adfa85e4SNicolas Dichtel mfcs.mfcs_bytes = c->mfc_un.res.bytes; 2179adfa85e4SNicolas Dichtel mfcs.mfcs_wrong_if = c->mfc_un.res.wrong_if; 218043b9e127SNikolay Aleksandrov if (nla_put_64bit(skb, RTA_MFC_STATS, sizeof(mfcs), &mfcs, RTA_PAD) || 2181b5036cd4SNikolay Aleksandrov nla_put_u64_64bit(skb, RTA_EXPIRES, jiffies_to_clock_t(lastuse), 218243b9e127SNikolay Aleksandrov RTA_PAD)) 2183adfa85e4SNicolas Dichtel return -EMSGSIZE; 2184adfa85e4SNicolas Dichtel 21851da177e4SLinus Torvalds rtm->rtm_type = RTN_MULTICAST; 21861da177e4SLinus Torvalds return 1; 21871da177e4SLinus Torvalds } 21881da177e4SLinus Torvalds 21899a1b9496SDavid S. Miller int ipmr_get_route(struct net *net, struct sk_buff *skb, 21909a1b9496SDavid S. Miller __be32 saddr, __be32 daddr, 21919f09eaeaSDavid Ahern struct rtmsg *rtm, u32 portid) 21921da177e4SLinus Torvalds { 21931da177e4SLinus Torvalds struct mfc_cache *cache; 21949a1b9496SDavid S. Miller struct mr_table *mrt; 21959a1b9496SDavid S. Miller int err; 21961da177e4SLinus Torvalds 2197f0ad0860SPatrick McHardy mrt = ipmr_get_table(net, RT_TABLE_DEFAULT); 219851456b29SIan Morris if (!mrt) 2199f0ad0860SPatrick McHardy return -ENOENT; 2200f0ad0860SPatrick McHardy 2201a8c9486bSEric Dumazet rcu_read_lock(); 22029a1b9496SDavid S. Miller cache = ipmr_cache_find(mrt, saddr, daddr); 220351456b29SIan Morris if (!cache && skb->dev) { 2204660b26dcSNicolas Dichtel int vif = ipmr_find_vif(mrt, skb->dev); 22051da177e4SLinus Torvalds 2206660b26dcSNicolas Dichtel if (vif >= 0) 2207660b26dcSNicolas Dichtel cache = ipmr_cache_find_any(mrt, daddr, vif); 2208660b26dcSNicolas Dichtel } 220951456b29SIan Morris if (!cache) { 221072287490SAlexey Kuznetsov struct sk_buff *skb2; 2211eddc9ec5SArnaldo Carvalho de Melo struct iphdr *iph; 22121da177e4SLinus Torvalds struct net_device *dev; 2213a8cb16ddSEric Dumazet int vif = -1; 22141da177e4SLinus Torvalds 22151da177e4SLinus Torvalds dev = skb->dev; 2216a8c9486bSEric Dumazet read_lock(&mrt_lock); 2217a8cb16ddSEric Dumazet if (dev) 2218a8cb16ddSEric Dumazet vif = ipmr_find_vif(mrt, dev); 2219a8cb16ddSEric Dumazet if (vif < 0) { 22201da177e4SLinus Torvalds read_unlock(&mrt_lock); 2221a8c9486bSEric Dumazet rcu_read_unlock(); 22221da177e4SLinus Torvalds return -ENODEV; 22231da177e4SLinus Torvalds } 222472287490SAlexey Kuznetsov skb2 = skb_clone(skb, GFP_ATOMIC); 222572287490SAlexey Kuznetsov if (!skb2) { 222672287490SAlexey Kuznetsov read_unlock(&mrt_lock); 2227a8c9486bSEric Dumazet rcu_read_unlock(); 222872287490SAlexey Kuznetsov return -ENOMEM; 222972287490SAlexey Kuznetsov } 223072287490SAlexey Kuznetsov 22312cf75070SNikolay Aleksandrov NETLINK_CB(skb2).portid = portid; 2232e2d1bca7SArnaldo Carvalho de Melo skb_push(skb2, sizeof(struct iphdr)); 2233e2d1bca7SArnaldo Carvalho de Melo skb_reset_network_header(skb2); 2234eddc9ec5SArnaldo Carvalho de Melo iph = ip_hdr(skb2); 2235eddc9ec5SArnaldo Carvalho de Melo iph->ihl = sizeof(struct iphdr) >> 2; 22369a1b9496SDavid S. Miller iph->saddr = saddr; 22379a1b9496SDavid S. Miller iph->daddr = daddr; 2238eddc9ec5SArnaldo Carvalho de Melo iph->version = 0; 2239*4b1f0d33SDonald Sharp err = ipmr_cache_unresolved(mrt, vif, skb2, dev); 22401da177e4SLinus Torvalds read_unlock(&mrt_lock); 2241a8c9486bSEric Dumazet rcu_read_unlock(); 22421da177e4SLinus Torvalds return err; 22431da177e4SLinus Torvalds } 22441da177e4SLinus Torvalds 2245a8c9486bSEric Dumazet read_lock(&mrt_lock); 2246cb6a4e46SPatrick McHardy err = __ipmr_fill_mroute(mrt, skb, cache, rtm); 22471da177e4SLinus Torvalds read_unlock(&mrt_lock); 2248a8c9486bSEric Dumazet rcu_read_unlock(); 22491da177e4SLinus Torvalds return err; 22501da177e4SLinus Torvalds } 22511da177e4SLinus Torvalds 2252cb6a4e46SPatrick McHardy static int ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb, 225365886f43SNicolas Dichtel u32 portid, u32 seq, struct mfc_cache *c, int cmd, 225465886f43SNicolas Dichtel int flags) 2255cb6a4e46SPatrick McHardy { 2256cb6a4e46SPatrick McHardy struct nlmsghdr *nlh; 2257cb6a4e46SPatrick McHardy struct rtmsg *rtm; 22581eb99af5SNicolas Dichtel int err; 2259cb6a4e46SPatrick McHardy 226065886f43SNicolas Dichtel nlh = nlmsg_put(skb, portid, seq, cmd, sizeof(*rtm), flags); 226151456b29SIan Morris if (!nlh) 2262cb6a4e46SPatrick McHardy return -EMSGSIZE; 2263cb6a4e46SPatrick McHardy 2264cb6a4e46SPatrick McHardy rtm = nlmsg_data(nlh); 2265cb6a4e46SPatrick McHardy rtm->rtm_family = RTNL_FAMILY_IPMR; 2266cb6a4e46SPatrick McHardy rtm->rtm_dst_len = 32; 2267cb6a4e46SPatrick McHardy rtm->rtm_src_len = 32; 2268cb6a4e46SPatrick McHardy rtm->rtm_tos = 0; 2269cb6a4e46SPatrick McHardy rtm->rtm_table = mrt->id; 2270f3756b79SDavid S. Miller if (nla_put_u32(skb, RTA_TABLE, mrt->id)) 2271f3756b79SDavid S. Miller goto nla_put_failure; 2272cb6a4e46SPatrick McHardy rtm->rtm_type = RTN_MULTICAST; 2273cb6a4e46SPatrick McHardy rtm->rtm_scope = RT_SCOPE_UNIVERSE; 22749a68ac72SNicolas Dichtel if (c->mfc_flags & MFC_STATIC) 22759a68ac72SNicolas Dichtel rtm->rtm_protocol = RTPROT_STATIC; 22769a68ac72SNicolas Dichtel else 22779a68ac72SNicolas Dichtel rtm->rtm_protocol = RTPROT_MROUTED; 2278cb6a4e46SPatrick McHardy rtm->rtm_flags = 0; 2279cb6a4e46SPatrick McHardy 2280930345eaSJiri Benc if (nla_put_in_addr(skb, RTA_SRC, c->mfc_origin) || 2281930345eaSJiri Benc nla_put_in_addr(skb, RTA_DST, c->mfc_mcastgrp)) 2282f3756b79SDavid S. Miller goto nla_put_failure; 22831eb99af5SNicolas Dichtel err = __ipmr_fill_mroute(mrt, skb, c, rtm); 22841eb99af5SNicolas Dichtel /* do not break the dump if cache is unresolved */ 22851eb99af5SNicolas Dichtel if (err < 0 && err != -ENOENT) 2286cb6a4e46SPatrick McHardy goto nla_put_failure; 2287cb6a4e46SPatrick McHardy 2288053c095aSJohannes Berg nlmsg_end(skb, nlh); 2289053c095aSJohannes Berg return 0; 2290cb6a4e46SPatrick McHardy 2291cb6a4e46SPatrick McHardy nla_put_failure: 2292cb6a4e46SPatrick McHardy nlmsg_cancel(skb, nlh); 2293cb6a4e46SPatrick McHardy return -EMSGSIZE; 2294cb6a4e46SPatrick McHardy } 2295cb6a4e46SPatrick McHardy 22968cd3ac9fSNicolas Dichtel static size_t mroute_msgsize(bool unresolved, int maxvif) 22978cd3ac9fSNicolas Dichtel { 22988cd3ac9fSNicolas Dichtel size_t len = 22998cd3ac9fSNicolas Dichtel NLMSG_ALIGN(sizeof(struct rtmsg)) 23008cd3ac9fSNicolas Dichtel + nla_total_size(4) /* RTA_TABLE */ 23018cd3ac9fSNicolas Dichtel + nla_total_size(4) /* RTA_SRC */ 23028cd3ac9fSNicolas Dichtel + nla_total_size(4) /* RTA_DST */ 23038cd3ac9fSNicolas Dichtel ; 23048cd3ac9fSNicolas Dichtel 23058cd3ac9fSNicolas Dichtel if (!unresolved) 23068cd3ac9fSNicolas Dichtel len = len 23078cd3ac9fSNicolas Dichtel + nla_total_size(4) /* RTA_IIF */ 23088cd3ac9fSNicolas Dichtel + nla_total_size(0) /* RTA_MULTIPATH */ 23098cd3ac9fSNicolas Dichtel + maxvif * NLA_ALIGN(sizeof(struct rtnexthop)) 23108cd3ac9fSNicolas Dichtel /* RTA_MFC_STATS */ 2311a9a08042SNicolas Dichtel + nla_total_size_64bit(sizeof(struct rta_mfc_stats)) 23128cd3ac9fSNicolas Dichtel ; 23138cd3ac9fSNicolas Dichtel 23148cd3ac9fSNicolas Dichtel return len; 23158cd3ac9fSNicolas Dichtel } 23168cd3ac9fSNicolas Dichtel 23178cd3ac9fSNicolas Dichtel static void mroute_netlink_event(struct mr_table *mrt, struct mfc_cache *mfc, 23188cd3ac9fSNicolas Dichtel int cmd) 23198cd3ac9fSNicolas Dichtel { 23208cd3ac9fSNicolas Dichtel struct net *net = read_pnet(&mrt->net); 23218cd3ac9fSNicolas Dichtel struct sk_buff *skb; 23228cd3ac9fSNicolas Dichtel int err = -ENOBUFS; 23238cd3ac9fSNicolas Dichtel 23248cd3ac9fSNicolas Dichtel skb = nlmsg_new(mroute_msgsize(mfc->mfc_parent >= MAXVIFS, mrt->maxvif), 23258cd3ac9fSNicolas Dichtel GFP_ATOMIC); 232651456b29SIan Morris if (!skb) 23278cd3ac9fSNicolas Dichtel goto errout; 23288cd3ac9fSNicolas Dichtel 232965886f43SNicolas Dichtel err = ipmr_fill_mroute(mrt, skb, 0, 0, mfc, cmd, 0); 23308cd3ac9fSNicolas Dichtel if (err < 0) 23318cd3ac9fSNicolas Dichtel goto errout; 23328cd3ac9fSNicolas Dichtel 23338cd3ac9fSNicolas Dichtel rtnl_notify(skb, net, 0, RTNLGRP_IPV4_MROUTE, NULL, GFP_ATOMIC); 23348cd3ac9fSNicolas Dichtel return; 23358cd3ac9fSNicolas Dichtel 23368cd3ac9fSNicolas Dichtel errout: 23378cd3ac9fSNicolas Dichtel kfree_skb(skb); 23388cd3ac9fSNicolas Dichtel if (err < 0) 23398cd3ac9fSNicolas Dichtel rtnl_set_sk_err(net, RTNLGRP_IPV4_MROUTE, err); 23408cd3ac9fSNicolas Dichtel } 23418cd3ac9fSNicolas Dichtel 2342cb6a4e46SPatrick McHardy static int ipmr_rtm_dumproute(struct sk_buff *skb, struct netlink_callback *cb) 2343cb6a4e46SPatrick McHardy { 2344cb6a4e46SPatrick McHardy struct net *net = sock_net(skb->sk); 2345cb6a4e46SPatrick McHardy struct mr_table *mrt; 2346cb6a4e46SPatrick McHardy struct mfc_cache *mfc; 2347cb6a4e46SPatrick McHardy unsigned int t = 0, s_t; 2348cb6a4e46SPatrick McHardy unsigned int e = 0, s_e; 2349cb6a4e46SPatrick McHardy 2350cb6a4e46SPatrick McHardy s_t = cb->args[0]; 23518fb472c0SNikolay Aleksandrov s_e = cb->args[1]; 2352cb6a4e46SPatrick McHardy 2353a8c9486bSEric Dumazet rcu_read_lock(); 2354cb6a4e46SPatrick McHardy ipmr_for_each_table(mrt, net) { 2355cb6a4e46SPatrick McHardy if (t < s_t) 2356cb6a4e46SPatrick McHardy goto next_table; 23578fb472c0SNikolay Aleksandrov list_for_each_entry_rcu(mfc, &mrt->mfc_cache_list, list) { 2358cb6a4e46SPatrick McHardy if (e < s_e) 2359cb6a4e46SPatrick McHardy goto next_entry; 2360cb6a4e46SPatrick McHardy if (ipmr_fill_mroute(mrt, skb, 236115e47304SEric W. Biederman NETLINK_CB(cb->skb).portid, 2362cb6a4e46SPatrick McHardy cb->nlh->nlmsg_seq, 236365886f43SNicolas Dichtel mfc, RTM_NEWROUTE, 236465886f43SNicolas Dichtel NLM_F_MULTI) < 0) 2365cb6a4e46SPatrick McHardy goto done; 2366cb6a4e46SPatrick McHardy next_entry: 2367cb6a4e46SPatrick McHardy e++; 2368cb6a4e46SPatrick McHardy } 23698fb472c0SNikolay Aleksandrov e = 0; 23708fb472c0SNikolay Aleksandrov s_e = 0; 23718fb472c0SNikolay Aleksandrov 23721eb99af5SNicolas Dichtel spin_lock_bh(&mfc_unres_lock); 23731eb99af5SNicolas Dichtel list_for_each_entry(mfc, &mrt->mfc_unres_queue, list) { 23741eb99af5SNicolas Dichtel if (e < s_e) 23751eb99af5SNicolas Dichtel goto next_entry2; 23761eb99af5SNicolas Dichtel if (ipmr_fill_mroute(mrt, skb, 23771eb99af5SNicolas Dichtel NETLINK_CB(cb->skb).portid, 23781eb99af5SNicolas Dichtel cb->nlh->nlmsg_seq, 237965886f43SNicolas Dichtel mfc, RTM_NEWROUTE, 238065886f43SNicolas Dichtel NLM_F_MULTI) < 0) { 23811eb99af5SNicolas Dichtel spin_unlock_bh(&mfc_unres_lock); 23821eb99af5SNicolas Dichtel goto done; 23831eb99af5SNicolas Dichtel } 23841eb99af5SNicolas Dichtel next_entry2: 23851eb99af5SNicolas Dichtel e++; 23861eb99af5SNicolas Dichtel } 23871eb99af5SNicolas Dichtel spin_unlock_bh(&mfc_unres_lock); 23888fb472c0SNikolay Aleksandrov e = 0; 23898fb472c0SNikolay Aleksandrov s_e = 0; 2390cb6a4e46SPatrick McHardy next_table: 2391cb6a4e46SPatrick McHardy t++; 2392cb6a4e46SPatrick McHardy } 2393cb6a4e46SPatrick McHardy done: 2394a8c9486bSEric Dumazet rcu_read_unlock(); 2395cb6a4e46SPatrick McHardy 23968fb472c0SNikolay Aleksandrov cb->args[1] = e; 2397cb6a4e46SPatrick McHardy cb->args[0] = t; 2398cb6a4e46SPatrick McHardy 2399cb6a4e46SPatrick McHardy return skb->len; 2400cb6a4e46SPatrick McHardy } 2401cb6a4e46SPatrick McHardy 2402ccbb0aa6SNikolay Aleksandrov static const struct nla_policy rtm_ipmr_policy[RTA_MAX + 1] = { 2403ccbb0aa6SNikolay Aleksandrov [RTA_SRC] = { .type = NLA_U32 }, 2404ccbb0aa6SNikolay Aleksandrov [RTA_DST] = { .type = NLA_U32 }, 2405ccbb0aa6SNikolay Aleksandrov [RTA_IIF] = { .type = NLA_U32 }, 2406ccbb0aa6SNikolay Aleksandrov [RTA_TABLE] = { .type = NLA_U32 }, 2407ccbb0aa6SNikolay Aleksandrov [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) }, 2408ccbb0aa6SNikolay Aleksandrov }; 2409ccbb0aa6SNikolay Aleksandrov 2410ccbb0aa6SNikolay Aleksandrov static bool ipmr_rtm_validate_proto(unsigned char rtm_protocol) 2411ccbb0aa6SNikolay Aleksandrov { 2412ccbb0aa6SNikolay Aleksandrov switch (rtm_protocol) { 2413ccbb0aa6SNikolay Aleksandrov case RTPROT_STATIC: 2414ccbb0aa6SNikolay Aleksandrov case RTPROT_MROUTED: 2415ccbb0aa6SNikolay Aleksandrov return true; 2416ccbb0aa6SNikolay Aleksandrov } 2417ccbb0aa6SNikolay Aleksandrov return false; 2418ccbb0aa6SNikolay Aleksandrov } 2419ccbb0aa6SNikolay Aleksandrov 2420ccbb0aa6SNikolay Aleksandrov static int ipmr_nla_get_ttls(const struct nlattr *nla, struct mfcctl *mfcc) 2421ccbb0aa6SNikolay Aleksandrov { 2422ccbb0aa6SNikolay Aleksandrov struct rtnexthop *rtnh = nla_data(nla); 2423ccbb0aa6SNikolay Aleksandrov int remaining = nla_len(nla), vifi = 0; 2424ccbb0aa6SNikolay Aleksandrov 2425ccbb0aa6SNikolay Aleksandrov while (rtnh_ok(rtnh, remaining)) { 2426ccbb0aa6SNikolay Aleksandrov mfcc->mfcc_ttls[vifi] = rtnh->rtnh_hops; 2427ccbb0aa6SNikolay Aleksandrov if (++vifi == MAXVIFS) 2428ccbb0aa6SNikolay Aleksandrov break; 2429ccbb0aa6SNikolay Aleksandrov rtnh = rtnh_next(rtnh, &remaining); 2430ccbb0aa6SNikolay Aleksandrov } 2431ccbb0aa6SNikolay Aleksandrov 2432ccbb0aa6SNikolay Aleksandrov return remaining > 0 ? -EINVAL : vifi; 2433ccbb0aa6SNikolay Aleksandrov } 2434ccbb0aa6SNikolay Aleksandrov 2435ccbb0aa6SNikolay Aleksandrov /* returns < 0 on error, 0 for ADD_MFC and 1 for ADD_MFC_PROXY */ 2436ccbb0aa6SNikolay Aleksandrov static int rtm_to_ipmr_mfcc(struct net *net, struct nlmsghdr *nlh, 2437ccbb0aa6SNikolay Aleksandrov struct mfcctl *mfcc, int *mrtsock, 2438c21ef3e3SDavid Ahern struct mr_table **mrtret, 2439c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 2440ccbb0aa6SNikolay Aleksandrov { 2441ccbb0aa6SNikolay Aleksandrov struct net_device *dev = NULL; 2442ccbb0aa6SNikolay Aleksandrov u32 tblid = RT_TABLE_DEFAULT; 2443ccbb0aa6SNikolay Aleksandrov struct mr_table *mrt; 2444ccbb0aa6SNikolay Aleksandrov struct nlattr *attr; 2445ccbb0aa6SNikolay Aleksandrov struct rtmsg *rtm; 2446ccbb0aa6SNikolay Aleksandrov int ret, rem; 2447ccbb0aa6SNikolay Aleksandrov 2448fceb6435SJohannes Berg ret = nlmsg_validate(nlh, sizeof(*rtm), RTA_MAX, rtm_ipmr_policy, 2449c21ef3e3SDavid Ahern extack); 2450ccbb0aa6SNikolay Aleksandrov if (ret < 0) 2451ccbb0aa6SNikolay Aleksandrov goto out; 2452ccbb0aa6SNikolay Aleksandrov rtm = nlmsg_data(nlh); 2453ccbb0aa6SNikolay Aleksandrov 2454ccbb0aa6SNikolay Aleksandrov ret = -EINVAL; 2455ccbb0aa6SNikolay Aleksandrov if (rtm->rtm_family != RTNL_FAMILY_IPMR || rtm->rtm_dst_len != 32 || 2456ccbb0aa6SNikolay Aleksandrov rtm->rtm_type != RTN_MULTICAST || 2457ccbb0aa6SNikolay Aleksandrov rtm->rtm_scope != RT_SCOPE_UNIVERSE || 2458ccbb0aa6SNikolay Aleksandrov !ipmr_rtm_validate_proto(rtm->rtm_protocol)) 2459ccbb0aa6SNikolay Aleksandrov goto out; 2460ccbb0aa6SNikolay Aleksandrov 2461ccbb0aa6SNikolay Aleksandrov memset(mfcc, 0, sizeof(*mfcc)); 2462ccbb0aa6SNikolay Aleksandrov mfcc->mfcc_parent = -1; 2463ccbb0aa6SNikolay Aleksandrov ret = 0; 2464ccbb0aa6SNikolay Aleksandrov nlmsg_for_each_attr(attr, nlh, sizeof(struct rtmsg), rem) { 2465ccbb0aa6SNikolay Aleksandrov switch (nla_type(attr)) { 2466ccbb0aa6SNikolay Aleksandrov case RTA_SRC: 2467ccbb0aa6SNikolay Aleksandrov mfcc->mfcc_origin.s_addr = nla_get_be32(attr); 2468ccbb0aa6SNikolay Aleksandrov break; 2469ccbb0aa6SNikolay Aleksandrov case RTA_DST: 2470ccbb0aa6SNikolay Aleksandrov mfcc->mfcc_mcastgrp.s_addr = nla_get_be32(attr); 2471ccbb0aa6SNikolay Aleksandrov break; 2472ccbb0aa6SNikolay Aleksandrov case RTA_IIF: 2473ccbb0aa6SNikolay Aleksandrov dev = __dev_get_by_index(net, nla_get_u32(attr)); 2474ccbb0aa6SNikolay Aleksandrov if (!dev) { 2475ccbb0aa6SNikolay Aleksandrov ret = -ENODEV; 2476ccbb0aa6SNikolay Aleksandrov goto out; 2477ccbb0aa6SNikolay Aleksandrov } 2478ccbb0aa6SNikolay Aleksandrov break; 2479ccbb0aa6SNikolay Aleksandrov case RTA_MULTIPATH: 2480ccbb0aa6SNikolay Aleksandrov if (ipmr_nla_get_ttls(attr, mfcc) < 0) { 2481ccbb0aa6SNikolay Aleksandrov ret = -EINVAL; 2482ccbb0aa6SNikolay Aleksandrov goto out; 2483ccbb0aa6SNikolay Aleksandrov } 2484ccbb0aa6SNikolay Aleksandrov break; 2485ccbb0aa6SNikolay Aleksandrov case RTA_PREFSRC: 2486ccbb0aa6SNikolay Aleksandrov ret = 1; 2487ccbb0aa6SNikolay Aleksandrov break; 2488ccbb0aa6SNikolay Aleksandrov case RTA_TABLE: 2489ccbb0aa6SNikolay Aleksandrov tblid = nla_get_u32(attr); 2490ccbb0aa6SNikolay Aleksandrov break; 2491ccbb0aa6SNikolay Aleksandrov } 2492ccbb0aa6SNikolay Aleksandrov } 2493ccbb0aa6SNikolay Aleksandrov mrt = ipmr_get_table(net, tblid); 2494ccbb0aa6SNikolay Aleksandrov if (!mrt) { 2495ccbb0aa6SNikolay Aleksandrov ret = -ENOENT; 2496ccbb0aa6SNikolay Aleksandrov goto out; 2497ccbb0aa6SNikolay Aleksandrov } 2498ccbb0aa6SNikolay Aleksandrov *mrtret = mrt; 2499ccbb0aa6SNikolay Aleksandrov *mrtsock = rtm->rtm_protocol == RTPROT_MROUTED ? 1 : 0; 2500ccbb0aa6SNikolay Aleksandrov if (dev) 2501ccbb0aa6SNikolay Aleksandrov mfcc->mfcc_parent = ipmr_find_vif(mrt, dev); 2502ccbb0aa6SNikolay Aleksandrov 2503ccbb0aa6SNikolay Aleksandrov out: 2504ccbb0aa6SNikolay Aleksandrov return ret; 2505ccbb0aa6SNikolay Aleksandrov } 2506ccbb0aa6SNikolay Aleksandrov 2507ccbb0aa6SNikolay Aleksandrov /* takes care of both newroute and delroute */ 2508c21ef3e3SDavid Ahern static int ipmr_rtm_route(struct sk_buff *skb, struct nlmsghdr *nlh, 2509c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 2510ccbb0aa6SNikolay Aleksandrov { 2511ccbb0aa6SNikolay Aleksandrov struct net *net = sock_net(skb->sk); 2512ccbb0aa6SNikolay Aleksandrov int ret, mrtsock, parent; 2513ccbb0aa6SNikolay Aleksandrov struct mr_table *tbl; 2514ccbb0aa6SNikolay Aleksandrov struct mfcctl mfcc; 2515ccbb0aa6SNikolay Aleksandrov 2516ccbb0aa6SNikolay Aleksandrov mrtsock = 0; 2517ccbb0aa6SNikolay Aleksandrov tbl = NULL; 2518c21ef3e3SDavid Ahern ret = rtm_to_ipmr_mfcc(net, nlh, &mfcc, &mrtsock, &tbl, extack); 2519ccbb0aa6SNikolay Aleksandrov if (ret < 0) 2520ccbb0aa6SNikolay Aleksandrov return ret; 2521ccbb0aa6SNikolay Aleksandrov 2522ccbb0aa6SNikolay Aleksandrov parent = ret ? mfcc.mfcc_parent : -1; 2523ccbb0aa6SNikolay Aleksandrov if (nlh->nlmsg_type == RTM_NEWROUTE) 2524ccbb0aa6SNikolay Aleksandrov return ipmr_mfc_add(net, tbl, &mfcc, mrtsock, parent); 2525ccbb0aa6SNikolay Aleksandrov else 2526ccbb0aa6SNikolay Aleksandrov return ipmr_mfc_delete(tbl, &mfcc, parent); 2527ccbb0aa6SNikolay Aleksandrov } 2528ccbb0aa6SNikolay Aleksandrov 25291da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 25307ef8f65dSNikolay Aleksandrov /* The /proc interfaces to multicast routing : 2531a8cb16ddSEric Dumazet * /proc/net/ip_mr_cache & /proc/net/ip_mr_vif 25321da177e4SLinus Torvalds */ 25331da177e4SLinus Torvalds struct ipmr_vif_iter { 2534f6bb4514SBenjamin Thery struct seq_net_private p; 2535f0ad0860SPatrick McHardy struct mr_table *mrt; 25361da177e4SLinus Torvalds int ct; 25371da177e4SLinus Torvalds }; 25381da177e4SLinus Torvalds 2539f6bb4514SBenjamin Thery static struct vif_device *ipmr_vif_seq_idx(struct net *net, 2540f6bb4514SBenjamin Thery struct ipmr_vif_iter *iter, 25411da177e4SLinus Torvalds loff_t pos) 25421da177e4SLinus Torvalds { 2543f0ad0860SPatrick McHardy struct mr_table *mrt = iter->mrt; 25440c12295aSPatrick McHardy 25450c12295aSPatrick McHardy for (iter->ct = 0; iter->ct < mrt->maxvif; ++iter->ct) { 25460c12295aSPatrick McHardy if (!VIF_EXISTS(mrt, iter->ct)) 25471da177e4SLinus Torvalds continue; 25481da177e4SLinus Torvalds if (pos-- == 0) 25490c12295aSPatrick McHardy return &mrt->vif_table[iter->ct]; 25501da177e4SLinus Torvalds } 25511da177e4SLinus Torvalds return NULL; 25521da177e4SLinus Torvalds } 25531da177e4SLinus Torvalds 25541da177e4SLinus Torvalds static void *ipmr_vif_seq_start(struct seq_file *seq, loff_t *pos) 2555ba93ef74SStephen Hemminger __acquires(mrt_lock) 25561da177e4SLinus Torvalds { 2557f0ad0860SPatrick McHardy struct ipmr_vif_iter *iter = seq->private; 2558f6bb4514SBenjamin Thery struct net *net = seq_file_net(seq); 2559f0ad0860SPatrick McHardy struct mr_table *mrt; 2560f0ad0860SPatrick McHardy 2561f0ad0860SPatrick McHardy mrt = ipmr_get_table(net, RT_TABLE_DEFAULT); 256251456b29SIan Morris if (!mrt) 2563f0ad0860SPatrick McHardy return ERR_PTR(-ENOENT); 2564f0ad0860SPatrick McHardy 2565f0ad0860SPatrick McHardy iter->mrt = mrt; 2566f6bb4514SBenjamin Thery 25671da177e4SLinus Torvalds read_lock(&mrt_lock); 2568f6bb4514SBenjamin Thery return *pos ? ipmr_vif_seq_idx(net, seq->private, *pos - 1) 25691da177e4SLinus Torvalds : SEQ_START_TOKEN; 25701da177e4SLinus Torvalds } 25711da177e4SLinus Torvalds 25721da177e4SLinus Torvalds static void *ipmr_vif_seq_next(struct seq_file *seq, void *v, loff_t *pos) 25731da177e4SLinus Torvalds { 25741da177e4SLinus Torvalds struct ipmr_vif_iter *iter = seq->private; 2575f6bb4514SBenjamin Thery struct net *net = seq_file_net(seq); 2576f0ad0860SPatrick McHardy struct mr_table *mrt = iter->mrt; 25771da177e4SLinus Torvalds 25781da177e4SLinus Torvalds ++*pos; 25791da177e4SLinus Torvalds if (v == SEQ_START_TOKEN) 2580f6bb4514SBenjamin Thery return ipmr_vif_seq_idx(net, iter, 0); 25811da177e4SLinus Torvalds 25820c12295aSPatrick McHardy while (++iter->ct < mrt->maxvif) { 25830c12295aSPatrick McHardy if (!VIF_EXISTS(mrt, iter->ct)) 25841da177e4SLinus Torvalds continue; 25850c12295aSPatrick McHardy return &mrt->vif_table[iter->ct]; 25861da177e4SLinus Torvalds } 25871da177e4SLinus Torvalds return NULL; 25881da177e4SLinus Torvalds } 25891da177e4SLinus Torvalds 25901da177e4SLinus Torvalds static void ipmr_vif_seq_stop(struct seq_file *seq, void *v) 2591ba93ef74SStephen Hemminger __releases(mrt_lock) 25921da177e4SLinus Torvalds { 25931da177e4SLinus Torvalds read_unlock(&mrt_lock); 25941da177e4SLinus Torvalds } 25951da177e4SLinus Torvalds 25961da177e4SLinus Torvalds static int ipmr_vif_seq_show(struct seq_file *seq, void *v) 25971da177e4SLinus Torvalds { 2598f0ad0860SPatrick McHardy struct ipmr_vif_iter *iter = seq->private; 2599f0ad0860SPatrick McHardy struct mr_table *mrt = iter->mrt; 2600f6bb4514SBenjamin Thery 26011da177e4SLinus Torvalds if (v == SEQ_START_TOKEN) { 26021da177e4SLinus Torvalds seq_puts(seq, 26031da177e4SLinus Torvalds "Interface BytesIn PktsIn BytesOut PktsOut Flags Local Remote\n"); 26041da177e4SLinus Torvalds } else { 26051da177e4SLinus Torvalds const struct vif_device *vif = v; 26061da177e4SLinus Torvalds const char *name = vif->dev ? vif->dev->name : "none"; 26071da177e4SLinus Torvalds 26081da177e4SLinus Torvalds seq_printf(seq, 26095b5e0928SAlexey Dobriyan "%2zd %-10s %8ld %7ld %8ld %7ld %05X %08X %08X\n", 26100c12295aSPatrick McHardy vif - mrt->vif_table, 26111da177e4SLinus Torvalds name, vif->bytes_in, vif->pkt_in, 26121da177e4SLinus Torvalds vif->bytes_out, vif->pkt_out, 26131da177e4SLinus Torvalds vif->flags, vif->local, vif->remote); 26141da177e4SLinus Torvalds } 26151da177e4SLinus Torvalds return 0; 26161da177e4SLinus Torvalds } 26171da177e4SLinus Torvalds 2618f690808eSStephen Hemminger static const struct seq_operations ipmr_vif_seq_ops = { 26191da177e4SLinus Torvalds .start = ipmr_vif_seq_start, 26201da177e4SLinus Torvalds .next = ipmr_vif_seq_next, 26211da177e4SLinus Torvalds .stop = ipmr_vif_seq_stop, 26221da177e4SLinus Torvalds .show = ipmr_vif_seq_show, 26231da177e4SLinus Torvalds }; 26241da177e4SLinus Torvalds 26251da177e4SLinus Torvalds static int ipmr_vif_open(struct inode *inode, struct file *file) 26261da177e4SLinus Torvalds { 2627f6bb4514SBenjamin Thery return seq_open_net(inode, file, &ipmr_vif_seq_ops, 2628cf7732e4SPavel Emelyanov sizeof(struct ipmr_vif_iter)); 26291da177e4SLinus Torvalds } 26301da177e4SLinus Torvalds 26319a32144eSArjan van de Ven static const struct file_operations ipmr_vif_fops = { 26321da177e4SLinus Torvalds .owner = THIS_MODULE, 26331da177e4SLinus Torvalds .open = ipmr_vif_open, 26341da177e4SLinus Torvalds .read = seq_read, 26351da177e4SLinus Torvalds .llseek = seq_lseek, 2636f6bb4514SBenjamin Thery .release = seq_release_net, 26371da177e4SLinus Torvalds }; 26381da177e4SLinus Torvalds 26391da177e4SLinus Torvalds struct ipmr_mfc_iter { 2640f6bb4514SBenjamin Thery struct seq_net_private p; 2641f0ad0860SPatrick McHardy struct mr_table *mrt; 2642862465f2SPatrick McHardy struct list_head *cache; 26431da177e4SLinus Torvalds }; 26441da177e4SLinus Torvalds 2645f6bb4514SBenjamin Thery static struct mfc_cache *ipmr_mfc_seq_idx(struct net *net, 2646f6bb4514SBenjamin Thery struct ipmr_mfc_iter *it, loff_t pos) 26471da177e4SLinus Torvalds { 2648f0ad0860SPatrick McHardy struct mr_table *mrt = it->mrt; 26491da177e4SLinus Torvalds struct mfc_cache *mfc; 26501da177e4SLinus Torvalds 2651a8c9486bSEric Dumazet rcu_read_lock(); 26528fb472c0SNikolay Aleksandrov it->cache = &mrt->mfc_cache_list; 26538fb472c0SNikolay Aleksandrov list_for_each_entry_rcu(mfc, &mrt->mfc_cache_list, list) 26541da177e4SLinus Torvalds if (pos-- == 0) 26551da177e4SLinus Torvalds return mfc; 2656a8c9486bSEric Dumazet rcu_read_unlock(); 26571da177e4SLinus Torvalds 26581da177e4SLinus Torvalds spin_lock_bh(&mfc_unres_lock); 26590c12295aSPatrick McHardy it->cache = &mrt->mfc_unres_queue; 2660862465f2SPatrick McHardy list_for_each_entry(mfc, it->cache, list) 2661e258beb2SPatrick McHardy if (pos-- == 0) 26621da177e4SLinus Torvalds return mfc; 26631da177e4SLinus Torvalds spin_unlock_bh(&mfc_unres_lock); 26641da177e4SLinus Torvalds 26651da177e4SLinus Torvalds it->cache = NULL; 26661da177e4SLinus Torvalds return NULL; 26671da177e4SLinus Torvalds } 26681da177e4SLinus Torvalds 26691da177e4SLinus Torvalds 26701da177e4SLinus Torvalds static void *ipmr_mfc_seq_start(struct seq_file *seq, loff_t *pos) 26711da177e4SLinus Torvalds { 26721da177e4SLinus Torvalds struct ipmr_mfc_iter *it = seq->private; 2673f6bb4514SBenjamin Thery struct net *net = seq_file_net(seq); 2674f0ad0860SPatrick McHardy struct mr_table *mrt; 2675f6bb4514SBenjamin Thery 2676f0ad0860SPatrick McHardy mrt = ipmr_get_table(net, RT_TABLE_DEFAULT); 267751456b29SIan Morris if (!mrt) 2678f0ad0860SPatrick McHardy return ERR_PTR(-ENOENT); 2679f0ad0860SPatrick McHardy 2680f0ad0860SPatrick McHardy it->mrt = mrt; 26811da177e4SLinus Torvalds it->cache = NULL; 2682f6bb4514SBenjamin Thery return *pos ? ipmr_mfc_seq_idx(net, seq->private, *pos - 1) 26831da177e4SLinus Torvalds : SEQ_START_TOKEN; 26841da177e4SLinus Torvalds } 26851da177e4SLinus Torvalds 26861da177e4SLinus Torvalds static void *ipmr_mfc_seq_next(struct seq_file *seq, void *v, loff_t *pos) 26871da177e4SLinus Torvalds { 26881da177e4SLinus Torvalds struct ipmr_mfc_iter *it = seq->private; 2689f6bb4514SBenjamin Thery struct net *net = seq_file_net(seq); 2690f0ad0860SPatrick McHardy struct mr_table *mrt = it->mrt; 26918fb472c0SNikolay Aleksandrov struct mfc_cache *mfc = v; 26921da177e4SLinus Torvalds 26931da177e4SLinus Torvalds ++*pos; 26941da177e4SLinus Torvalds 26951da177e4SLinus Torvalds if (v == SEQ_START_TOKEN) 2696f6bb4514SBenjamin Thery return ipmr_mfc_seq_idx(net, seq->private, 0); 26971da177e4SLinus Torvalds 2698862465f2SPatrick McHardy if (mfc->list.next != it->cache) 2699862465f2SPatrick McHardy return list_entry(mfc->list.next, struct mfc_cache, list); 27001da177e4SLinus Torvalds 27010c12295aSPatrick McHardy if (it->cache == &mrt->mfc_unres_queue) 27021da177e4SLinus Torvalds goto end_of_list; 27031da177e4SLinus Torvalds 27041da177e4SLinus Torvalds /* exhausted cache_array, show unresolved */ 2705a8c9486bSEric Dumazet rcu_read_unlock(); 27060c12295aSPatrick McHardy it->cache = &mrt->mfc_unres_queue; 27071da177e4SLinus Torvalds 27081da177e4SLinus Torvalds spin_lock_bh(&mfc_unres_lock); 2709862465f2SPatrick McHardy if (!list_empty(it->cache)) 2710862465f2SPatrick McHardy return list_first_entry(it->cache, struct mfc_cache, list); 27111da177e4SLinus Torvalds 27121da177e4SLinus Torvalds end_of_list: 27131da177e4SLinus Torvalds spin_unlock_bh(&mfc_unres_lock); 27141da177e4SLinus Torvalds it->cache = NULL; 27151da177e4SLinus Torvalds 27161da177e4SLinus Torvalds return NULL; 27171da177e4SLinus Torvalds } 27181da177e4SLinus Torvalds 27191da177e4SLinus Torvalds static void ipmr_mfc_seq_stop(struct seq_file *seq, void *v) 27201da177e4SLinus Torvalds { 27211da177e4SLinus Torvalds struct ipmr_mfc_iter *it = seq->private; 2722f0ad0860SPatrick McHardy struct mr_table *mrt = it->mrt; 27231da177e4SLinus Torvalds 27240c12295aSPatrick McHardy if (it->cache == &mrt->mfc_unres_queue) 27251da177e4SLinus Torvalds spin_unlock_bh(&mfc_unres_lock); 27268fb472c0SNikolay Aleksandrov else if (it->cache == &mrt->mfc_cache_list) 2727a8c9486bSEric Dumazet rcu_read_unlock(); 27281da177e4SLinus Torvalds } 27291da177e4SLinus Torvalds 27301da177e4SLinus Torvalds static int ipmr_mfc_seq_show(struct seq_file *seq, void *v) 27311da177e4SLinus Torvalds { 27321da177e4SLinus Torvalds int n; 27331da177e4SLinus Torvalds 27341da177e4SLinus Torvalds if (v == SEQ_START_TOKEN) { 27351da177e4SLinus Torvalds seq_puts(seq, 27361da177e4SLinus Torvalds "Group Origin Iif Pkts Bytes Wrong Oifs\n"); 27371da177e4SLinus Torvalds } else { 27381da177e4SLinus Torvalds const struct mfc_cache *mfc = v; 27391da177e4SLinus Torvalds const struct ipmr_mfc_iter *it = seq->private; 2740f0ad0860SPatrick McHardy const struct mr_table *mrt = it->mrt; 27411da177e4SLinus Torvalds 27420eae88f3SEric Dumazet seq_printf(seq, "%08X %08X %-3hd", 27430eae88f3SEric Dumazet (__force u32) mfc->mfc_mcastgrp, 27440eae88f3SEric Dumazet (__force u32) mfc->mfc_origin, 27451ea472e2SBenjamin Thery mfc->mfc_parent); 27461ea472e2SBenjamin Thery 27470c12295aSPatrick McHardy if (it->cache != &mrt->mfc_unres_queue) { 27481ea472e2SBenjamin Thery seq_printf(seq, " %8lu %8lu %8lu", 27491da177e4SLinus Torvalds mfc->mfc_un.res.pkt, 27501da177e4SLinus Torvalds mfc->mfc_un.res.bytes, 27511da177e4SLinus Torvalds mfc->mfc_un.res.wrong_if); 27521da177e4SLinus Torvalds for (n = mfc->mfc_un.res.minvif; 27531da177e4SLinus Torvalds n < mfc->mfc_un.res.maxvif; n++) { 27540c12295aSPatrick McHardy if (VIF_EXISTS(mrt, n) && 2755cf958ae3SBenjamin Thery mfc->mfc_un.res.ttls[n] < 255) 27561da177e4SLinus Torvalds seq_printf(seq, 27571da177e4SLinus Torvalds " %2d:%-3d", 27581da177e4SLinus Torvalds n, mfc->mfc_un.res.ttls[n]); 27591da177e4SLinus Torvalds } 27601ea472e2SBenjamin Thery } else { 27611ea472e2SBenjamin Thery /* unresolved mfc_caches don't contain 27621ea472e2SBenjamin Thery * pkt, bytes and wrong_if values 27631ea472e2SBenjamin Thery */ 27641ea472e2SBenjamin Thery seq_printf(seq, " %8lu %8lu %8lu", 0ul, 0ul, 0ul); 27651da177e4SLinus Torvalds } 27661da177e4SLinus Torvalds seq_putc(seq, '\n'); 27671da177e4SLinus Torvalds } 27681da177e4SLinus Torvalds return 0; 27691da177e4SLinus Torvalds } 27701da177e4SLinus Torvalds 2771f690808eSStephen Hemminger static const struct seq_operations ipmr_mfc_seq_ops = { 27721da177e4SLinus Torvalds .start = ipmr_mfc_seq_start, 27731da177e4SLinus Torvalds .next = ipmr_mfc_seq_next, 27741da177e4SLinus Torvalds .stop = ipmr_mfc_seq_stop, 27751da177e4SLinus Torvalds .show = ipmr_mfc_seq_show, 27761da177e4SLinus Torvalds }; 27771da177e4SLinus Torvalds 27781da177e4SLinus Torvalds static int ipmr_mfc_open(struct inode *inode, struct file *file) 27791da177e4SLinus Torvalds { 2780f6bb4514SBenjamin Thery return seq_open_net(inode, file, &ipmr_mfc_seq_ops, 2781cf7732e4SPavel Emelyanov sizeof(struct ipmr_mfc_iter)); 27821da177e4SLinus Torvalds } 27831da177e4SLinus Torvalds 27849a32144eSArjan van de Ven static const struct file_operations ipmr_mfc_fops = { 27851da177e4SLinus Torvalds .owner = THIS_MODULE, 27861da177e4SLinus Torvalds .open = ipmr_mfc_open, 27871da177e4SLinus Torvalds .read = seq_read, 27881da177e4SLinus Torvalds .llseek = seq_lseek, 2789f6bb4514SBenjamin Thery .release = seq_release_net, 27901da177e4SLinus Torvalds }; 27911da177e4SLinus Torvalds #endif 27921da177e4SLinus Torvalds 27931da177e4SLinus Torvalds #ifdef CONFIG_IP_PIMSM_V2 279432613090SAlexey Dobriyan static const struct net_protocol pim_protocol = { 27951da177e4SLinus Torvalds .handler = pim_rcv, 2796403dbb97STom Goff .netns_ok = 1, 27971da177e4SLinus Torvalds }; 27981da177e4SLinus Torvalds #endif 27991da177e4SLinus Torvalds 28007ef8f65dSNikolay Aleksandrov /* Setup for IP multicast routing */ 2801cf958ae3SBenjamin Thery static int __net_init ipmr_net_init(struct net *net) 2802cf958ae3SBenjamin Thery { 2803f0ad0860SPatrick McHardy int err; 2804cf958ae3SBenjamin Thery 2805f0ad0860SPatrick McHardy err = ipmr_rules_init(net); 2806f0ad0860SPatrick McHardy if (err < 0) 2807cf958ae3SBenjamin Thery goto fail; 2808f6bb4514SBenjamin Thery 2809f6bb4514SBenjamin Thery #ifdef CONFIG_PROC_FS 2810f6bb4514SBenjamin Thery err = -ENOMEM; 2811d4beaa66SGao feng if (!proc_create("ip_mr_vif", 0, net->proc_net, &ipmr_vif_fops)) 2812f6bb4514SBenjamin Thery goto proc_vif_fail; 2813d4beaa66SGao feng if (!proc_create("ip_mr_cache", 0, net->proc_net, &ipmr_mfc_fops)) 2814f6bb4514SBenjamin Thery goto proc_cache_fail; 2815f6bb4514SBenjamin Thery #endif 28162bb8b26cSBenjamin Thery return 0; 28172bb8b26cSBenjamin Thery 2818f6bb4514SBenjamin Thery #ifdef CONFIG_PROC_FS 2819f6bb4514SBenjamin Thery proc_cache_fail: 2820ece31ffdSGao feng remove_proc_entry("ip_mr_vif", net->proc_net); 2821f6bb4514SBenjamin Thery proc_vif_fail: 2822f0ad0860SPatrick McHardy ipmr_rules_exit(net); 2823f6bb4514SBenjamin Thery #endif 2824cf958ae3SBenjamin Thery fail: 2825cf958ae3SBenjamin Thery return err; 2826cf958ae3SBenjamin Thery } 2827cf958ae3SBenjamin Thery 2828cf958ae3SBenjamin Thery static void __net_exit ipmr_net_exit(struct net *net) 2829cf958ae3SBenjamin Thery { 2830f6bb4514SBenjamin Thery #ifdef CONFIG_PROC_FS 2831ece31ffdSGao feng remove_proc_entry("ip_mr_cache", net->proc_net); 2832ece31ffdSGao feng remove_proc_entry("ip_mr_vif", net->proc_net); 2833f6bb4514SBenjamin Thery #endif 2834f0ad0860SPatrick McHardy ipmr_rules_exit(net); 2835cf958ae3SBenjamin Thery } 2836cf958ae3SBenjamin Thery 2837cf958ae3SBenjamin Thery static struct pernet_operations ipmr_net_ops = { 2838cf958ae3SBenjamin Thery .init = ipmr_net_init, 2839cf958ae3SBenjamin Thery .exit = ipmr_net_exit, 2840cf958ae3SBenjamin Thery }; 28411da177e4SLinus Torvalds 284203d2f897SWang Chen int __init ip_mr_init(void) 28431da177e4SLinus Torvalds { 284403d2f897SWang Chen int err; 284503d2f897SWang Chen 28461da177e4SLinus Torvalds mrt_cachep = kmem_cache_create("ip_mrt_cache", 28471da177e4SLinus Torvalds sizeof(struct mfc_cache), 2848e5d679f3SAlexey Dobriyan 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, 284920c2df83SPaul Mundt NULL); 285003d2f897SWang Chen 2851cf958ae3SBenjamin Thery err = register_pernet_subsys(&ipmr_net_ops); 2852cf958ae3SBenjamin Thery if (err) 2853cf958ae3SBenjamin Thery goto reg_pernet_fail; 2854cf958ae3SBenjamin Thery 285503d2f897SWang Chen err = register_netdevice_notifier(&ip_mr_notifier); 285603d2f897SWang Chen if (err) 285703d2f897SWang Chen goto reg_notif_fail; 2858403dbb97STom Goff #ifdef CONFIG_IP_PIMSM_V2 2859403dbb97STom Goff if (inet_add_protocol(&pim_protocol, IPPROTO_PIM) < 0) { 2860058bd4d2SJoe Perches pr_err("%s: can't add PIM protocol\n", __func__); 2861403dbb97STom Goff err = -EAGAIN; 2862403dbb97STom Goff goto add_proto_fail; 2863403dbb97STom Goff } 2864403dbb97STom Goff #endif 2865c7ac8679SGreg Rose rtnl_register(RTNL_FAMILY_IPMR, RTM_GETROUTE, 2866c7ac8679SGreg Rose NULL, ipmr_rtm_dumproute, NULL); 2867ccbb0aa6SNikolay Aleksandrov rtnl_register(RTNL_FAMILY_IPMR, RTM_NEWROUTE, 2868ccbb0aa6SNikolay Aleksandrov ipmr_rtm_route, NULL, NULL); 2869ccbb0aa6SNikolay Aleksandrov rtnl_register(RTNL_FAMILY_IPMR, RTM_DELROUTE, 2870ccbb0aa6SNikolay Aleksandrov ipmr_rtm_route, NULL, NULL); 287103d2f897SWang Chen return 0; 2872f6bb4514SBenjamin Thery 2873403dbb97STom Goff #ifdef CONFIG_IP_PIMSM_V2 2874403dbb97STom Goff add_proto_fail: 2875403dbb97STom Goff unregister_netdevice_notifier(&ip_mr_notifier); 2876403dbb97STom Goff #endif 2877c3e38896SBenjamin Thery reg_notif_fail: 2878cf958ae3SBenjamin Thery unregister_pernet_subsys(&ipmr_net_ops); 2879cf958ae3SBenjamin Thery reg_pernet_fail: 2880c3e38896SBenjamin Thery kmem_cache_destroy(mrt_cachep); 288103d2f897SWang Chen return err; 28821da177e4SLinus Torvalds } 2883