^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * IP multicast routing support for mrouted 3.6/3.8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * (c) 1995 Alan Cox, <alan@lxorguk.ukuu.org.uk>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Linux Consultancy and Custom Driver Development
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * Fixes:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * Michael Chastain : Incorrect size of copying.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * Alan Cox : Added the cache manager code
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * Alan Cox : Fixed the clone/copy bug and device race.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * Mike McLagan : Routing by source
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * Malcolm Beattie : Buffer handling fixes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * Alexey Kuznetsov : Double buffer free and other fixes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * SVR Anand : Fixed several multicast bugs and problems.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * Alexey Kuznetsov : Status, optimisations and more.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) * Brad Parker : Better behaviour on mrouted upcall
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) * overflow.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) * Carlos Picoto : PIMv1 Support
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * Pavlin Ivanov Radoslavov: PIMv2 Registers must checksum only PIM header
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * Relax this requirement to work with older peers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include <linux/cache.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include <linux/capability.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #include <linux/fcntl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #include <linux/stat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #include <linux/socket.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) #include <linux/in.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) #include <linux/inet.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) #include <linux/netdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) #include <linux/inetdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) #include <linux/igmp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) #include <linux/proc_fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) #include <linux/seq_file.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) #include <linux/mroute.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) #include <linux/if_ether.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) #include <net/net_namespace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) #include <net/ip.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) #include <net/protocol.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) #include <linux/skbuff.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) #include <net/route.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) #include <net/icmp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) #include <net/udp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) #include <net/raw.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) #include <linux/notifier.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) #include <linux/if_arp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) #include <linux/netfilter_ipv4.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) #include <linux/compat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) #include <linux/export.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) #include <linux/rhashtable.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) #include <net/ip_tunnels.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) #include <net/checksum.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) #include <net/netlink.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) #include <net/fib_rules.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) #include <linux/netconf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) #include <net/rtnh.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) #include <linux/nospec.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) struct ipmr_rule {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) struct fib_rule common;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) struct ipmr_result {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) struct mr_table *mrt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) /* Big lock, protecting vif table, mrt cache and mroute socket state.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) * Note that the changes are semaphored via rtnl_lock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) static DEFINE_RWLOCK(mrt_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) /* Multicast router control variables */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) /* Special spinlock for queue of unresolved entries */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) static DEFINE_SPINLOCK(mfc_unres_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) /* We return to original Alan's scheme. Hash table of resolved
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) * entries is changed only in process context and protected
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) * with weak lock mrt_lock. Queue of unresolved entries is protected
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) * with strong spinlock mfc_unres_lock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) * In this case data path is free of exclusive locks at all.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) static struct kmem_cache *mrt_cachep __ro_after_init;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) static struct mr_table *ipmr_new_table(struct net *net, u32 id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) static void ipmr_free_table(struct mr_table *mrt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) static void ip_mr_forward(struct net *net, struct mr_table *mrt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) struct net_device *dev, struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) struct mfc_cache *cache, int local);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) static int ipmr_cache_report(struct mr_table *mrt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) struct sk_buff *pkt, vifi_t vifi, int assert);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) static void mroute_netlink_event(struct mr_table *mrt, struct mfc_cache *mfc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) int cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) static void igmpmsg_netlink_event(struct mr_table *mrt, struct sk_buff *pkt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) static void mroute_clean_tables(struct mr_table *mrt, int flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) static void ipmr_expire_process(struct timer_list *t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) #ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) #define ipmr_for_each_table(mrt, net) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) list_for_each_entry_rcu(mrt, &net->ipv4.mr_tables, list, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) lockdep_rtnl_is_held() || \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) list_empty(&net->ipv4.mr_tables))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) static struct mr_table *ipmr_mr_table_iter(struct net *net,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) struct mr_table *mrt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) struct mr_table *ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) if (!mrt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) ret = list_entry_rcu(net->ipv4.mr_tables.next,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) struct mr_table, list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) ret = list_entry_rcu(mrt->list.next,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) struct mr_table, list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) if (&ret->list == &net->ipv4.mr_tables)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) static struct mr_table *ipmr_get_table(struct net *net, u32 id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) struct mr_table *mrt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) ipmr_for_each_table(mrt, net) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) if (mrt->id == id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) return mrt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) struct mr_table **mrt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) struct ipmr_result res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) struct fib_lookup_arg arg = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) .result = &res,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) .flags = FIB_LOOKUP_NOREF,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) /* update flow if oif or iif point to device enslaved to l3mdev */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) l3mdev_update_flow(net, flowi4_to_flowi(flp4));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) err = fib_rules_lookup(net->ipv4.mr_rules_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) flowi4_to_flowi(flp4), 0, &arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) *mrt = res.mrt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) static int ipmr_rule_action(struct fib_rule *rule, struct flowi *flp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) int flags, struct fib_lookup_arg *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) struct ipmr_result *res = arg->result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) struct mr_table *mrt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) switch (rule->action) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) case FR_ACT_TO_TBL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) case FR_ACT_UNREACHABLE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) return -ENETUNREACH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) case FR_ACT_PROHIBIT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) return -EACCES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) case FR_ACT_BLACKHOLE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) arg->table = fib_rule_get_table(rule, arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) mrt = ipmr_get_table(rule->fr_net, arg->table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) if (!mrt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) return -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) res->mrt = mrt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) static int ipmr_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) static const struct nla_policy ipmr_rule_policy[FRA_MAX + 1] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) FRA_GENERIC_POLICY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) static int ipmr_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) struct fib_rule_hdr *frh, struct nlattr **tb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) static int ipmr_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) struct nlattr **tb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) static int ipmr_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) struct fib_rule_hdr *frh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) frh->dst_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) frh->src_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) frh->tos = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) static const struct fib_rules_ops __net_initconst ipmr_rules_ops_template = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) .family = RTNL_FAMILY_IPMR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) .rule_size = sizeof(struct ipmr_rule),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) .addr_size = sizeof(u32),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) .action = ipmr_rule_action,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) .match = ipmr_rule_match,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) .configure = ipmr_rule_configure,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) .compare = ipmr_rule_compare,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) .fill = ipmr_rule_fill,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) .nlgroup = RTNLGRP_IPV4_RULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) .policy = ipmr_rule_policy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) .owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) static int __net_init ipmr_rules_init(struct net *net)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) struct fib_rules_ops *ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) struct mr_table *mrt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) ops = fib_rules_register(&ipmr_rules_ops_template, net);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) if (IS_ERR(ops))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) return PTR_ERR(ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) INIT_LIST_HEAD(&net->ipv4.mr_tables);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) if (IS_ERR(mrt)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) err = PTR_ERR(mrt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) goto err1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) err = fib_default_rule_add(ops, 0x7fff, RT_TABLE_DEFAULT, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) goto err2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) net->ipv4.mr_rules_ops = ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) err2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) rtnl_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) ipmr_free_table(mrt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) rtnl_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) err1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) fib_rules_unregister(ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) static void __net_exit ipmr_rules_exit(struct net *net)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) struct mr_table *mrt, *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) rtnl_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) list_for_each_entry_safe(mrt, next, &net->ipv4.mr_tables, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) list_del(&mrt->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) ipmr_free_table(mrt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) fib_rules_unregister(net->ipv4.mr_rules_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) rtnl_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) static int ipmr_rules_dump(struct net *net, struct notifier_block *nb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) return fib_rules_dump(net, nb, RTNL_FAMILY_IPMR, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) static unsigned int ipmr_rules_seq_read(struct net *net)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) return fib_rules_seq_read(net, RTNL_FAMILY_IPMR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) bool ipmr_rule_default(const struct fib_rule *rule)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) return fib_rule_matchall(rule) && rule->table == RT_TABLE_DEFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) EXPORT_SYMBOL(ipmr_rule_default);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) #define ipmr_for_each_table(mrt, net) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) for (mrt = net->ipv4.mrt; mrt; mrt = NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) static struct mr_table *ipmr_mr_table_iter(struct net *net,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) struct mr_table *mrt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) if (!mrt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) return net->ipv4.mrt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) static struct mr_table *ipmr_get_table(struct net *net, u32 id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) return net->ipv4.mrt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) struct mr_table **mrt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) *mrt = net->ipv4.mrt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) static int __net_init ipmr_rules_init(struct net *net)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) struct mr_table *mrt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) if (IS_ERR(mrt))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) return PTR_ERR(mrt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) net->ipv4.mrt = mrt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) static void __net_exit ipmr_rules_exit(struct net *net)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) rtnl_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) ipmr_free_table(net->ipv4.mrt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) net->ipv4.mrt = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) rtnl_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) static int ipmr_rules_dump(struct net *net, struct notifier_block *nb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) static unsigned int ipmr_rules_seq_read(struct net *net)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) bool ipmr_rule_default(const struct fib_rule *rule)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) EXPORT_SYMBOL(ipmr_rule_default);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) static inline int ipmr_hash_cmp(struct rhashtable_compare_arg *arg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) const void *ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) const struct mfc_cache_cmp_arg *cmparg = arg->key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) struct mfc_cache *c = (struct mfc_cache *)ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) return cmparg->mfc_mcastgrp != c->mfc_mcastgrp ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) cmparg->mfc_origin != c->mfc_origin;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) static const struct rhashtable_params ipmr_rht_params = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) .head_offset = offsetof(struct mr_mfc, mnode),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) .key_offset = offsetof(struct mfc_cache, cmparg),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) .key_len = sizeof(struct mfc_cache_cmp_arg),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) .nelem_hint = 3,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) .obj_cmpfn = ipmr_hash_cmp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) .automatic_shrinking = true,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) static void ipmr_new_table_set(struct mr_table *mrt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) struct net *net)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) #ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) list_add_tail_rcu(&mrt->list, &net->ipv4.mr_tables);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) static struct mfc_cache_cmp_arg ipmr_mr_table_ops_cmparg_any = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) .mfc_mcastgrp = htonl(INADDR_ANY),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) .mfc_origin = htonl(INADDR_ANY),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) static struct mr_table_ops ipmr_mr_table_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) .rht_params = &ipmr_rht_params,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) .cmparg_any = &ipmr_mr_table_ops_cmparg_any,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) static struct mr_table *ipmr_new_table(struct net *net, u32 id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) struct mr_table *mrt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) /* "pimreg%u" should not exceed 16 bytes (IFNAMSIZ) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) if (id != RT_TABLE_DEFAULT && id >= 1000000000)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) mrt = ipmr_get_table(net, id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) if (mrt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) return mrt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) return mr_table_alloc(net, id, &ipmr_mr_table_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) ipmr_expire_process, ipmr_new_table_set);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) static void ipmr_free_table(struct mr_table *mrt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) del_timer_sync(&mrt->ipmr_expire_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) mroute_clean_tables(mrt, MRT_FLUSH_VIFS | MRT_FLUSH_VIFS_STATIC |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) MRT_FLUSH_MFC | MRT_FLUSH_MFC_STATIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) rhltable_destroy(&mrt->mfc_hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) kfree(mrt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) /* Service routines creating virtual interfaces: DVMRP tunnels and PIMREG */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) /* Initialize ipmr pimreg/tunnel in_device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) static bool ipmr_init_vif_indev(const struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) struct in_device *in_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) ASSERT_RTNL();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) in_dev = __in_dev_get_rtnl(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) if (!in_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) ipv4_devconf_setall(in_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) neigh_parms_data_state_setall(in_dev->arp_parms);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) static struct net_device *ipmr_new_tunnel(struct net *net, struct vifctl *v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) struct net_device *tunnel_dev, *new_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) struct ip_tunnel_parm p = { };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) tunnel_dev = __dev_get_by_name(net, "tunl0");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) if (!tunnel_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) p.iph.daddr = v->vifc_rmt_addr.s_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) p.iph.saddr = v->vifc_lcl_addr.s_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) p.iph.version = 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) p.iph.ihl = 5;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) p.iph.protocol = IPPROTO_IPIP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) sprintf(p.name, "dvmrp%d", v->vifc_vifi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) if (!tunnel_dev->netdev_ops->ndo_tunnel_ctl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) err = tunnel_dev->netdev_ops->ndo_tunnel_ctl(tunnel_dev, &p,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) SIOCADDTUNNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) new_dev = __dev_get_by_name(net, p.name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) if (!new_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) new_dev->flags |= IFF_MULTICAST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) if (!ipmr_init_vif_indev(new_dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) goto out_unregister;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) if (dev_open(new_dev, NULL))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) goto out_unregister;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) dev_hold(new_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) err = dev_set_allmulti(new_dev, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) dev_close(new_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) tunnel_dev->netdev_ops->ndo_tunnel_ctl(tunnel_dev, &p,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) SIOCDELTUNNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) dev_put(new_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) new_dev = ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) return new_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) out_unregister:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) unregister_netdevice(new_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) return ERR_PTR(-ENOBUFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) #if defined(CONFIG_IP_PIMSM_V1) || defined(CONFIG_IP_PIMSM_V2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) static netdev_tx_t reg_vif_xmit(struct sk_buff *skb, struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) struct net *net = dev_net(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) struct mr_table *mrt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) struct flowi4 fl4 = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) .flowi4_oif = dev->ifindex,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) .flowi4_iif = skb->skb_iif ? : LOOPBACK_IFINDEX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) .flowi4_mark = skb->mark,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) err = ipmr_fib_lookup(net, &fl4, &mrt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) read_lock(&mrt_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) dev->stats.tx_bytes += skb->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) dev->stats.tx_packets++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) ipmr_cache_report(mrt, skb, mrt->mroute_reg_vif_num, IGMPMSG_WHOLEPKT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) read_unlock(&mrt_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) return NETDEV_TX_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) static int reg_vif_get_iflink(const struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) static const struct net_device_ops reg_vif_netdev_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) .ndo_start_xmit = reg_vif_xmit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) .ndo_get_iflink = reg_vif_get_iflink,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) static void reg_vif_setup(struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) dev->type = ARPHRD_PIMREG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) dev->mtu = ETH_DATA_LEN - sizeof(struct iphdr) - 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) dev->flags = IFF_NOARP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) dev->netdev_ops = ®_vif_netdev_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) dev->needs_free_netdev = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) dev->features |= NETIF_F_NETNS_LOCAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) struct net_device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) char name[IFNAMSIZ];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) if (mrt->id == RT_TABLE_DEFAULT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) sprintf(name, "pimreg");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) sprintf(name, "pimreg%u", mrt->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) dev = alloc_netdev(0, name, NET_NAME_UNKNOWN, reg_vif_setup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) if (!dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) dev_net_set(dev, net);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) if (register_netdevice(dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) free_netdev(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) if (!ipmr_init_vif_indev(dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) goto failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) if (dev_open(dev, NULL))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) goto failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) dev_hold(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) return dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) failure:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) unregister_netdevice(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) /* called with rcu_read_lock() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) static int __pim_rcv(struct mr_table *mrt, struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) unsigned int pimlen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) struct net_device *reg_dev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) struct iphdr *encap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) encap = (struct iphdr *)(skb_transport_header(skb) + pimlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) /* Check that:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) * a. packet is really sent to a multicast group
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) * b. packet is not a NULL-REGISTER
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) * c. packet is not truncated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) if (!ipv4_is_multicast(encap->daddr) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) encap->tot_len == 0 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) ntohs(encap->tot_len) + pimlen > skb->len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) read_lock(&mrt_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) if (mrt->mroute_reg_vif_num >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) reg_dev = mrt->vif_table[mrt->mroute_reg_vif_num].dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) read_unlock(&mrt_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) if (!reg_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) skb->mac_header = skb->network_header;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) skb_pull(skb, (u8 *)encap - skb->data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) skb_reset_network_header(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) skb->protocol = htons(ETH_P_IP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) skb->ip_summed = CHECKSUM_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) skb_tunnel_rx(skb, reg_dev, dev_net(reg_dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) netif_rx(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) return NET_RX_SUCCESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) static int call_ipmr_vif_entry_notifiers(struct net *net,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) enum fib_event_type event_type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) struct vif_device *vif,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) vifi_t vif_index, u32 tb_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) return mr_call_vif_notifiers(net, RTNL_FAMILY_IPMR, event_type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) vif, vif_index, tb_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) &net->ipv4.ipmr_seq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) static int call_ipmr_mfc_entry_notifiers(struct net *net,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) enum fib_event_type event_type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) struct mfc_cache *mfc, u32 tb_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) return mr_call_mfc_notifiers(net, RTNL_FAMILY_IPMR, event_type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) &mfc->_c, tb_id, &net->ipv4.ipmr_seq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) * vif_delete - Delete a VIF entry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) * @mrt: Table to delete from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) * @vifi: VIF identifier to delete
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) * @notify: Set to 1, if the caller is a notifier_call
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) * @head: if unregistering the VIF, place it on this queue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) static int vif_delete(struct mr_table *mrt, int vifi, int notify,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) struct list_head *head)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) struct net *net = read_pnet(&mrt->net);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) struct vif_device *v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) struct net_device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) struct in_device *in_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) if (vifi < 0 || vifi >= mrt->maxvif)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) return -EADDRNOTAVAIL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) v = &mrt->vif_table[vifi];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) if (VIF_EXISTS(mrt, vifi))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) call_ipmr_vif_entry_notifiers(net, FIB_EVENT_VIF_DEL, v, vifi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) mrt->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) write_lock_bh(&mrt_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) dev = v->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) v->dev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) if (!dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) write_unlock_bh(&mrt_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) return -EADDRNOTAVAIL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) if (vifi == mrt->mroute_reg_vif_num)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) mrt->mroute_reg_vif_num = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) if (vifi + 1 == mrt->maxvif) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) int tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) for (tmp = vifi - 1; tmp >= 0; tmp--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) if (VIF_EXISTS(mrt, tmp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) mrt->maxvif = tmp+1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) write_unlock_bh(&mrt_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) dev_set_allmulti(dev, -1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) in_dev = __in_dev_get_rtnl(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) if (in_dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) inet_netconf_notify_devconf(dev_net(dev), RTM_NEWNETCONF,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) NETCONFA_MC_FORWARDING,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) dev->ifindex, &in_dev->cnf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) ip_rt_multicast_event(in_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER) && !notify)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) unregister_netdevice_queue(dev, head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) dev_put(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) static void ipmr_cache_free_rcu(struct rcu_head *head)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) struct mr_mfc *c = container_of(head, struct mr_mfc, rcu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) kmem_cache_free(mrt_cachep, (struct mfc_cache *)c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) static void ipmr_cache_free(struct mfc_cache *c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) call_rcu(&c->_c.rcu, ipmr_cache_free_rcu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) /* Destroy an unresolved cache entry, killing queued skbs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) * and reporting error to netlink readers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) static void ipmr_destroy_unres(struct mr_table *mrt, struct mfc_cache *c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) struct net *net = read_pnet(&mrt->net);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) struct sk_buff *skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) struct nlmsgerr *e;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) atomic_dec(&mrt->cache_resolve_queue_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) while ((skb = skb_dequeue(&c->_c.mfc_un.unres.unresolved))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) if (ip_hdr(skb)->version == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) struct nlmsghdr *nlh = skb_pull(skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) sizeof(struct iphdr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) nlh->nlmsg_type = NLMSG_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) skb_trim(skb, nlh->nlmsg_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) e = nlmsg_data(nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) e->error = -ETIMEDOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) memset(&e->msg, 0, sizeof(e->msg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) ipmr_cache_free(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) /* Timer process for the unresolved queue. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) static void ipmr_expire_process(struct timer_list *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) struct mr_table *mrt = from_timer(mrt, t, ipmr_expire_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) struct mr_mfc *c, *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) unsigned long expires;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) unsigned long now;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) if (!spin_trylock(&mfc_unres_lock)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) mod_timer(&mrt->ipmr_expire_timer, jiffies+HZ/10);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) if (list_empty(&mrt->mfc_unres_queue))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) now = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) expires = 10*HZ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) if (time_after(c->mfc_un.unres.expires, now)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) unsigned long interval = c->mfc_un.unres.expires - now;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) if (interval < expires)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) expires = interval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) list_del(&c->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) mroute_netlink_event(mrt, (struct mfc_cache *)c, RTM_DELROUTE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) ipmr_destroy_unres(mrt, (struct mfc_cache *)c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) if (!list_empty(&mrt->mfc_unres_queue))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) mod_timer(&mrt->ipmr_expire_timer, jiffies + expires);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) spin_unlock(&mfc_unres_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) /* Fill oifs list. It is called under write locked mrt_lock. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) static void ipmr_update_thresholds(struct mr_table *mrt, struct mr_mfc *cache,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) unsigned char *ttls)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) int vifi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) cache->mfc_un.res.minvif = MAXVIFS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) cache->mfc_un.res.maxvif = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) memset(cache->mfc_un.res.ttls, 255, MAXVIFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) for (vifi = 0; vifi < mrt->maxvif; vifi++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) if (VIF_EXISTS(mrt, vifi) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) ttls[vifi] && ttls[vifi] < 255) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) cache->mfc_un.res.ttls[vifi] = ttls[vifi];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) if (cache->mfc_un.res.minvif > vifi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) cache->mfc_un.res.minvif = vifi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) if (cache->mfc_un.res.maxvif <= vifi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) cache->mfc_un.res.maxvif = vifi + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) cache->mfc_un.res.lastuse = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) static int vif_add(struct net *net, struct mr_table *mrt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) struct vifctl *vifc, int mrtsock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) struct netdev_phys_item_id ppid = { };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) int vifi = vifc->vifc_vifi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) struct vif_device *v = &mrt->vif_table[vifi];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) struct net_device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) struct in_device *in_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) /* Is vif busy ? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) if (VIF_EXISTS(mrt, vifi))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) return -EADDRINUSE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) switch (vifc->vifc_flags) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) case VIFF_REGISTER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) if (!ipmr_pimsm_enabled())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) /* Special Purpose VIF in PIM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) * All the packets will be sent to the daemon
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) if (mrt->mroute_reg_vif_num >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) return -EADDRINUSE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) dev = ipmr_reg_vif(net, mrt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) if (!dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) return -ENOBUFS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) err = dev_set_allmulti(dev, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) unregister_netdevice(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) dev_put(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) case VIFF_TUNNEL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) dev = ipmr_new_tunnel(net, vifc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) if (IS_ERR(dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) return PTR_ERR(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) case VIFF_USE_IFINDEX:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) case 0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) if (vifc->vifc_flags == VIFF_USE_IFINDEX) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) dev = dev_get_by_index(net, vifc->vifc_lcl_ifindex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) if (dev && !__in_dev_get_rtnl(dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) dev_put(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) return -EADDRNOTAVAIL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) dev = ip_dev_find(net, vifc->vifc_lcl_addr.s_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) if (!dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) return -EADDRNOTAVAIL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) err = dev_set_allmulti(dev, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) dev_put(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) in_dev = __in_dev_get_rtnl(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) if (!in_dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873) dev_put(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) return -EADDRNOTAVAIL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) inet_netconf_notify_devconf(net, RTM_NEWNETCONF, NETCONFA_MC_FORWARDING,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) dev->ifindex, &in_dev->cnf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) ip_rt_multicast_event(in_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) /* Fill in the VIF structures */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) vif_device_init(v, dev, vifc->vifc_rate_limit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) vifc->vifc_threshold,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884) vifc->vifc_flags | (!mrtsock ? VIFF_STATIC : 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) (VIFF_TUNNEL | VIFF_REGISTER));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) err = dev_get_port_parent_id(dev, &ppid, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) if (err == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) memcpy(v->dev_parent_id.id, ppid.id, ppid.id_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) v->dev_parent_id.id_len = ppid.id_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) v->dev_parent_id.id_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895) v->local = vifc->vifc_lcl_addr.s_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896) v->remote = vifc->vifc_rmt_addr.s_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898) /* And finish update writing critical data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) write_lock_bh(&mrt_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900) v->dev = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901) if (v->flags & VIFF_REGISTER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902) mrt->mroute_reg_vif_num = vifi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903) if (vifi+1 > mrt->maxvif)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) mrt->maxvif = vifi+1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) write_unlock_bh(&mrt_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) call_ipmr_vif_entry_notifiers(net, FIB_EVENT_VIF_ADD, v, vifi, mrt->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) /* called with rcu_read_lock() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911) static struct mfc_cache *ipmr_cache_find(struct mr_table *mrt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912) __be32 origin,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913) __be32 mcastgrp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915) struct mfc_cache_cmp_arg arg = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916) .mfc_mcastgrp = mcastgrp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) .mfc_origin = origin
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920) return mr_mfc_find(mrt, &arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923) /* Look for a (*,G) entry */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924) static struct mfc_cache *ipmr_cache_find_any(struct mr_table *mrt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925) __be32 mcastgrp, int vifi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927) struct mfc_cache_cmp_arg arg = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928) .mfc_mcastgrp = mcastgrp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929) .mfc_origin = htonl(INADDR_ANY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932) if (mcastgrp == htonl(INADDR_ANY))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933) return mr_mfc_find_any_parent(mrt, vifi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934) return mr_mfc_find_any(mrt, vifi, &arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937) /* Look for a (S,G,iif) entry if parent != -1 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938) static struct mfc_cache *ipmr_cache_find_parent(struct mr_table *mrt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939) __be32 origin, __be32 mcastgrp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940) int parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942) struct mfc_cache_cmp_arg arg = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943) .mfc_mcastgrp = mcastgrp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944) .mfc_origin = origin,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947) return mr_mfc_find_parent(mrt, &arg, parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 949)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 950) /* Allocate a multicast cache entry */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 951) static struct mfc_cache *ipmr_cache_alloc(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 952) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 953) struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 954)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 955) if (c) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 956) c->_c.mfc_un.res.last_assert = jiffies - MFC_ASSERT_THRESH - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 957) c->_c.mfc_un.res.minvif = MAXVIFS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 958) c->_c.free = ipmr_cache_free_rcu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 959) refcount_set(&c->_c.mfc_un.res.refcount, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 960) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 961) return c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 962) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 963)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 964) static struct mfc_cache *ipmr_cache_alloc_unres(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 965) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 966) struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 967)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 968) if (c) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 969) skb_queue_head_init(&c->_c.mfc_un.unres.unresolved);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 970) c->_c.mfc_un.unres.expires = jiffies + 10 * HZ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 971) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 972) return c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 973) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 974)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 975) /* A cache entry has gone into a resolved state from queued */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 976) static void ipmr_cache_resolve(struct net *net, struct mr_table *mrt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 977) struct mfc_cache *uc, struct mfc_cache *c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 978) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 979) struct sk_buff *skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 980) struct nlmsgerr *e;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 981)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 982) /* Play the pending entries through our router */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 983) while ((skb = __skb_dequeue(&uc->_c.mfc_un.unres.unresolved))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 984) if (ip_hdr(skb)->version == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 985) struct nlmsghdr *nlh = skb_pull(skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 986) sizeof(struct iphdr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 987)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 988) if (mr_fill_mroute(mrt, skb, &c->_c,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 989) nlmsg_data(nlh)) > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 990) nlh->nlmsg_len = skb_tail_pointer(skb) -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 991) (u8 *)nlh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 992) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 993) nlh->nlmsg_type = NLMSG_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 994) nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 995) skb_trim(skb, nlh->nlmsg_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 996) e = nlmsg_data(nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 997) e->error = -EMSGSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 998) memset(&e->msg, 0, sizeof(e->msg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 999) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) ip_mr_forward(net, mrt, skb->dev, skb, c, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) /* Bounce a cache query up to mrouted and netlink.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) * Called under mrt_lock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) static int ipmr_cache_report(struct mr_table *mrt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) struct sk_buff *pkt, vifi_t vifi, int assert)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) const int ihl = ip_hdrlen(pkt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) struct sock *mroute_sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) struct igmphdr *igmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) struct igmpmsg *msg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) struct sk_buff *skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) if (assert == IGMPMSG_WHOLEPKT || assert == IGMPMSG_WRVIFWHOLE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) skb = skb_realloc_headroom(pkt, sizeof(struct iphdr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) skb = alloc_skb(128, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) if (!skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) return -ENOBUFS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) if (assert == IGMPMSG_WHOLEPKT || assert == IGMPMSG_WRVIFWHOLE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) /* Ugly, but we have no choice with this interface.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) * Duplicate old header, fix ihl, length etc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) * And all this only to mangle msg->im_msgtype and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) * to set msg->im_mbz to "mbz" :-)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) skb_push(skb, sizeof(struct iphdr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) skb_reset_network_header(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) skb_reset_transport_header(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) msg = (struct igmpmsg *)skb_network_header(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) memcpy(msg, skb_network_header(pkt), sizeof(struct iphdr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) msg->im_msgtype = assert;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) msg->im_mbz = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) if (assert == IGMPMSG_WRVIFWHOLE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) msg->im_vif = vifi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) msg->im_vif_hi = vifi >> 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) msg->im_vif = mrt->mroute_reg_vif_num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) msg->im_vif_hi = mrt->mroute_reg_vif_num >> 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) ip_hdr(skb)->ihl = sizeof(struct iphdr) >> 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(pkt)->tot_len) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) sizeof(struct iphdr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) /* Copy the IP header */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) skb_set_network_header(skb, skb->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) skb_put(skb, ihl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) skb_copy_to_linear_data(skb, pkt->data, ihl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) /* Flag to the kernel this is a route add */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) ip_hdr(skb)->protocol = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) msg = (struct igmpmsg *)skb_network_header(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) msg->im_vif = vifi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) msg->im_vif_hi = vifi >> 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) skb_dst_set(skb, dst_clone(skb_dst(pkt)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) /* Add our header */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) igmp = skb_put(skb, sizeof(struct igmphdr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) igmp->type = assert;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) msg->im_msgtype = assert;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) igmp->code = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) ip_hdr(skb)->tot_len = htons(skb->len); /* Fix the length */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) skb->transport_header = skb->network_header;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) mroute_sk = rcu_dereference(mrt->mroute_sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) if (!mroute_sk) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) igmpmsg_netlink_event(mrt, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) /* Deliver to mrouted */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) ret = sock_queue_rcv_skb(mroute_sk, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) net_warn_ratelimited("mroute: pending queue full, dropping entries\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) /* Queue a packet for resolution. It gets locked cache entry! */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) static int ipmr_cache_unresolved(struct mr_table *mrt, vifi_t vifi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) struct sk_buff *skb, struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) const struct iphdr *iph = ip_hdr(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) struct mfc_cache *c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) bool found = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) spin_lock_bh(&mfc_unres_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) list_for_each_entry(c, &mrt->mfc_unres_queue, _c.list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) if (c->mfc_mcastgrp == iph->daddr &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) c->mfc_origin == iph->saddr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) found = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) if (!found) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) /* Create a new entry if allowable */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) c = ipmr_cache_alloc_unres();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) if (!c) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) spin_unlock_bh(&mfc_unres_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) return -ENOBUFS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) /* Fill in the new cache entry */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) c->_c.mfc_parent = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) c->mfc_origin = iph->saddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) c->mfc_mcastgrp = iph->daddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) /* Reflect first query at mrouted. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) err = ipmr_cache_report(mrt, skb, vifi, IGMPMSG_NOCACHE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) /* If the report failed throw the cache entry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) out - Brad Parker
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) spin_unlock_bh(&mfc_unres_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) ipmr_cache_free(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) atomic_inc(&mrt->cache_resolve_queue_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) list_add(&c->_c.list, &mrt->mfc_unres_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) mroute_netlink_event(mrt, c, RTM_NEWROUTE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) if (atomic_read(&mrt->cache_resolve_queue_len) == 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) mod_timer(&mrt->ipmr_expire_timer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) c->_c.mfc_un.unres.expires);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) /* See if we can append the packet */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) if (c->_c.mfc_un.unres.unresolved.qlen > 3) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) err = -ENOBUFS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) if (dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) skb->dev = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) skb->skb_iif = dev->ifindex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) skb_queue_tail(&c->_c.mfc_un.unres.unresolved, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) spin_unlock_bh(&mfc_unres_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) /* MFC cache manipulation by user space mroute daemon */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) static int ipmr_mfc_delete(struct mr_table *mrt, struct mfcctl *mfc, int parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) struct net *net = read_pnet(&mrt->net);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) struct mfc_cache *c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) /* The entries are added/deleted only under RTNL */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) c = ipmr_cache_find_parent(mrt, mfc->mfcc_origin.s_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) mfc->mfcc_mcastgrp.s_addr, parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) if (!c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) rhltable_remove(&mrt->mfc_hash, &c->_c.mnode, ipmr_rht_params);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) list_del_rcu(&c->_c.list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) call_ipmr_mfc_entry_notifiers(net, FIB_EVENT_ENTRY_DEL, c, mrt->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) mroute_netlink_event(mrt, c, RTM_DELROUTE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) mr_cache_put(&c->_c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) static int ipmr_mfc_add(struct net *net, struct mr_table *mrt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) struct mfcctl *mfc, int mrtsock, int parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) struct mfc_cache *uc, *c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) struct mr_mfc *_uc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) bool found;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) if (mfc->mfcc_parent >= MAXVIFS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) return -ENFILE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) /* The entries are added/deleted only under RTNL */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) c = ipmr_cache_find_parent(mrt, mfc->mfcc_origin.s_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) mfc->mfcc_mcastgrp.s_addr, parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) if (c) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) write_lock_bh(&mrt_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) c->_c.mfc_parent = mfc->mfcc_parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) ipmr_update_thresholds(mrt, &c->_c, mfc->mfcc_ttls);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) if (!mrtsock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) c->_c.mfc_flags |= MFC_STATIC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) write_unlock_bh(&mrt_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) call_ipmr_mfc_entry_notifiers(net, FIB_EVENT_ENTRY_REPLACE, c,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) mrt->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) mroute_netlink_event(mrt, c, RTM_NEWROUTE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) if (mfc->mfcc_mcastgrp.s_addr != htonl(INADDR_ANY) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) !ipv4_is_multicast(mfc->mfcc_mcastgrp.s_addr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) c = ipmr_cache_alloc();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) if (!c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) c->mfc_origin = mfc->mfcc_origin.s_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) c->mfc_mcastgrp = mfc->mfcc_mcastgrp.s_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) c->_c.mfc_parent = mfc->mfcc_parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) ipmr_update_thresholds(mrt, &c->_c, mfc->mfcc_ttls);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) if (!mrtsock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) c->_c.mfc_flags |= MFC_STATIC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) ret = rhltable_insert_key(&mrt->mfc_hash, &c->cmparg, &c->_c.mnode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) ipmr_rht_params);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) pr_err("ipmr: rhtable insert error %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) ipmr_cache_free(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) list_add_tail_rcu(&c->_c.list, &mrt->mfc_cache_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) /* Check to see if we resolved a queued list. If so we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) * need to send on the frames and tidy up.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) found = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) spin_lock_bh(&mfc_unres_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) list_for_each_entry(_uc, &mrt->mfc_unres_queue, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) uc = (struct mfc_cache *)_uc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) if (uc->mfc_origin == c->mfc_origin &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) uc->mfc_mcastgrp == c->mfc_mcastgrp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) list_del(&_uc->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) atomic_dec(&mrt->cache_resolve_queue_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) found = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) if (list_empty(&mrt->mfc_unres_queue))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) del_timer(&mrt->ipmr_expire_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) spin_unlock_bh(&mfc_unres_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) if (found) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) ipmr_cache_resolve(net, mrt, uc, c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) ipmr_cache_free(uc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) call_ipmr_mfc_entry_notifiers(net, FIB_EVENT_ENTRY_ADD, c, mrt->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) mroute_netlink_event(mrt, c, RTM_NEWROUTE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) /* Close the multicast socket, and clear the vif tables etc */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) static void mroute_clean_tables(struct mr_table *mrt, int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) struct net *net = read_pnet(&mrt->net);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) struct mr_mfc *c, *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) struct mfc_cache *cache;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) LIST_HEAD(list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) /* Shut down all active vif entries */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) if (flags & (MRT_FLUSH_VIFS | MRT_FLUSH_VIFS_STATIC)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) for (i = 0; i < mrt->maxvif; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) if (((mrt->vif_table[i].flags & VIFF_STATIC) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) !(flags & MRT_FLUSH_VIFS_STATIC)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) (!(mrt->vif_table[i].flags & VIFF_STATIC) && !(flags & MRT_FLUSH_VIFS)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) vif_delete(mrt, i, 0, &list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) unregister_netdevice_many(&list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) /* Wipe the cache */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) if (flags & (MRT_FLUSH_MFC | MRT_FLUSH_MFC_STATIC)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) list_for_each_entry_safe(c, tmp, &mrt->mfc_cache_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) if (((c->mfc_flags & MFC_STATIC) && !(flags & MRT_FLUSH_MFC_STATIC)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) (!(c->mfc_flags & MFC_STATIC) && !(flags & MRT_FLUSH_MFC)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) rhltable_remove(&mrt->mfc_hash, &c->mnode, ipmr_rht_params);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) list_del_rcu(&c->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) cache = (struct mfc_cache *)c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) call_ipmr_mfc_entry_notifiers(net, FIB_EVENT_ENTRY_DEL, cache,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) mrt->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) mroute_netlink_event(mrt, cache, RTM_DELROUTE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) mr_cache_put(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) if (flags & MRT_FLUSH_MFC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) if (atomic_read(&mrt->cache_resolve_queue_len) != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) spin_lock_bh(&mfc_unres_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) list_for_each_entry_safe(c, tmp, &mrt->mfc_unres_queue, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) list_del(&c->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) cache = (struct mfc_cache *)c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) mroute_netlink_event(mrt, cache, RTM_DELROUTE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) ipmr_destroy_unres(mrt, cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) spin_unlock_bh(&mfc_unres_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) /* called from ip_ra_control(), before an RCU grace period,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) * we dont need to call synchronize_rcu() here
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) static void mrtsock_destruct(struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) struct net *net = sock_net(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) struct mr_table *mrt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) rtnl_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) ipmr_for_each_table(mrt, net) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) if (sk == rtnl_dereference(mrt->mroute_sk)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) IPV4_DEVCONF_ALL(net, MC_FORWARDING)--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) NETCONFA_MC_FORWARDING,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) NETCONFA_IFINDEX_ALL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) net->ipv4.devconf_all);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) RCU_INIT_POINTER(mrt->mroute_sk, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) mroute_clean_tables(mrt, MRT_FLUSH_VIFS | MRT_FLUSH_MFC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) rtnl_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) /* Socket options and virtual interface manipulation. The whole
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) * virtual interface system is a complete heap, but unfortunately
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) * that's how BSD mrouted happens to think. Maybe one day with a proper
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) * MOSPF/PIM router set up we can clean this up.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) int ip_mroute_setsockopt(struct sock *sk, int optname, sockptr_t optval,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) unsigned int optlen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) struct net *net = sock_net(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) int val, ret = 0, parent = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) struct mr_table *mrt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) struct vifctl vif;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) struct mfcctl mfc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) bool do_wrvifwhole;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) u32 uval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) /* There's one exception to the lock - MRT_DONE which needs to unlock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) rtnl_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) if (sk->sk_type != SOCK_RAW ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) inet_sk(sk)->inet_num != IPPROTO_IGMP) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) ret = -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) if (!mrt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) ret = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) if (optname != MRT_INIT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) if (sk != rcu_access_pointer(mrt->mroute_sk) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) !ns_capable(net->user_ns, CAP_NET_ADMIN)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) ret = -EACCES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) switch (optname) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) case MRT_INIT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) if (optlen != sizeof(int)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) if (rtnl_dereference(mrt->mroute_sk)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) ret = -EADDRINUSE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) ret = ip_ra_control(sk, 1, mrtsock_destruct);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) if (ret == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) rcu_assign_pointer(mrt->mroute_sk, sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) IPV4_DEVCONF_ALL(net, MC_FORWARDING)++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) NETCONFA_MC_FORWARDING,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) NETCONFA_IFINDEX_ALL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) net->ipv4.devconf_all);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) case MRT_DONE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) if (sk != rcu_access_pointer(mrt->mroute_sk)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) ret = -EACCES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) /* We need to unlock here because mrtsock_destruct takes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) * care of rtnl itself and we can't change that due to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409) * the IP_ROUTER_ALERT setsockopt which runs without it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) rtnl_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412) ret = ip_ra_control(sk, 0, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) case MRT_ADD_VIF:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) case MRT_DEL_VIF:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) if (optlen != sizeof(vif)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) if (copy_from_sockptr(&vif, optval, sizeof(vif))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) if (vif.vifc_vifi >= MAXVIFS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) ret = -ENFILE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) if (optname == MRT_ADD_VIF) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) ret = vif_add(net, mrt, &vif,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) sk == rtnl_dereference(mrt->mroute_sk));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) ret = vif_delete(mrt, vif.vifc_vifi, 0, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437) /* Manipulate the forwarding caches. These live
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) * in a sort of kernel/user symbiosis.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) case MRT_ADD_MFC:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) case MRT_DEL_MFC:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) parent = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) fallthrough;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) case MRT_ADD_MFC_PROXY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) case MRT_DEL_MFC_PROXY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) if (optlen != sizeof(mfc)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) if (copy_from_sockptr(&mfc, optval, sizeof(mfc))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451) ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454) if (parent == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) parent = mfc.mfcc_parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456) if (optname == MRT_DEL_MFC || optname == MRT_DEL_MFC_PROXY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) ret = ipmr_mfc_delete(mrt, &mfc, parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) ret = ipmr_mfc_add(net, mrt, &mfc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) sk == rtnl_dereference(mrt->mroute_sk),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) case MRT_FLUSH:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) if (optlen != sizeof(val)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) if (copy_from_sockptr(&val, optval, sizeof(val))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469) ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472) mroute_clean_tables(mrt, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474) /* Control PIM assert. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475) case MRT_ASSERT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476) if (optlen != sizeof(val)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480) if (copy_from_sockptr(&val, optval, sizeof(val))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481) ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484) mrt->mroute_do_assert = val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486) case MRT_PIM:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487) if (!ipmr_pimsm_enabled()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) ret = -ENOPROTOOPT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491) if (optlen != sizeof(val)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495) if (copy_from_sockptr(&val, optval, sizeof(val))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496) ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500) do_wrvifwhole = (val == IGMPMSG_WRVIFWHOLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501) val = !!val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502) if (val != mrt->mroute_do_pim) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503) mrt->mroute_do_pim = val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504) mrt->mroute_do_assert = val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505) mrt->mroute_do_wrvifwhole = do_wrvifwhole;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508) case MRT_TABLE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509) if (!IS_BUILTIN(CONFIG_IP_MROUTE_MULTIPLE_TABLES)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510) ret = -ENOPROTOOPT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) if (optlen != sizeof(uval)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517) if (copy_from_sockptr(&uval, optval, sizeof(uval))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518) ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522) if (sk == rtnl_dereference(mrt->mroute_sk)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523) ret = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525) mrt = ipmr_new_table(net, uval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526) if (IS_ERR(mrt))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527) ret = PTR_ERR(mrt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529) raw_sk(sk)->ipmr_table = uval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532) /* Spurious command, or MRT_VERSION which you cannot set. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534) ret = -ENOPROTOOPT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537) rtnl_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542) /* Getsock opt support for the multicast routing system. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543) int ip_mroute_getsockopt(struct sock *sk, int optname, char __user *optval, int __user *optlen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545) int olr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) int val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547) struct net *net = sock_net(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) struct mr_table *mrt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550) if (sk->sk_type != SOCK_RAW ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551) inet_sk(sk)->inet_num != IPPROTO_IGMP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552) return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554) mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555) if (!mrt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556) return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558) switch (optname) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559) case MRT_VERSION:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560) val = 0x0305;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562) case MRT_PIM:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563) if (!ipmr_pimsm_enabled())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564) return -ENOPROTOOPT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565) val = mrt->mroute_do_pim;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567) case MRT_ASSERT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568) val = mrt->mroute_do_assert;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571) return -ENOPROTOOPT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574) if (get_user(olr, optlen))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576) olr = min_t(unsigned int, olr, sizeof(int));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577) if (olr < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579) if (put_user(olr, optlen))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581) if (copy_to_user(optval, &val, olr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586) /* The IP multicast ioctl support routines. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587) int ipmr_ioctl(struct sock *sk, int cmd, void __user *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589) struct sioc_sg_req sr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590) struct sioc_vif_req vr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591) struct vif_device *vif;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592) struct mfc_cache *c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593) struct net *net = sock_net(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594) struct mr_table *mrt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596) mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597) if (!mrt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598) return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1600) switch (cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1601) case SIOCGETVIFCNT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1602) if (copy_from_user(&vr, arg, sizeof(vr)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1603) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1604) if (vr.vifi >= mrt->maxvif)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1605) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1606) vr.vifi = array_index_nospec(vr.vifi, mrt->maxvif);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1607) read_lock(&mrt_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1608) vif = &mrt->vif_table[vr.vifi];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1609) if (VIF_EXISTS(mrt, vr.vifi)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1610) vr.icount = vif->pkt_in;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1611) vr.ocount = vif->pkt_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1612) vr.ibytes = vif->bytes_in;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1613) vr.obytes = vif->bytes_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1614) read_unlock(&mrt_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1615)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1616) if (copy_to_user(arg, &vr, sizeof(vr)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1617) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1618) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1619) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1620) read_unlock(&mrt_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1621) return -EADDRNOTAVAIL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1622) case SIOCGETSGCNT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1623) if (copy_from_user(&sr, arg, sizeof(sr)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1624) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1625)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1626) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1627) c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1628) if (c) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1629) sr.pktcnt = c->_c.mfc_un.res.pkt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1630) sr.bytecnt = c->_c.mfc_un.res.bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1631) sr.wrong_if = c->_c.mfc_un.res.wrong_if;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1632) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1633)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1634) if (copy_to_user(arg, &sr, sizeof(sr)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1635) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1636) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1637) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1638) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1639) return -EADDRNOTAVAIL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1640) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1641) return -ENOIOCTLCMD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1642) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1643) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1644)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1645) #ifdef CONFIG_COMPAT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1646) struct compat_sioc_sg_req {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1647) struct in_addr src;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1648) struct in_addr grp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1649) compat_ulong_t pktcnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1650) compat_ulong_t bytecnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1651) compat_ulong_t wrong_if;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1652) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1653)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1654) struct compat_sioc_vif_req {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1655) vifi_t vifi; /* Which iface */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1656) compat_ulong_t icount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1657) compat_ulong_t ocount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1658) compat_ulong_t ibytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1659) compat_ulong_t obytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1660) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1661)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1662) int ipmr_compat_ioctl(struct sock *sk, unsigned int cmd, void __user *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1663) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1664) struct compat_sioc_sg_req sr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1665) struct compat_sioc_vif_req vr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1666) struct vif_device *vif;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1667) struct mfc_cache *c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1668) struct net *net = sock_net(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1669) struct mr_table *mrt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1670)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1671) mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1672) if (!mrt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1673) return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1674)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1675) switch (cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1676) case SIOCGETVIFCNT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1677) if (copy_from_user(&vr, arg, sizeof(vr)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1678) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1679) if (vr.vifi >= mrt->maxvif)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1680) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1681) vr.vifi = array_index_nospec(vr.vifi, mrt->maxvif);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1682) read_lock(&mrt_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1683) vif = &mrt->vif_table[vr.vifi];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1684) if (VIF_EXISTS(mrt, vr.vifi)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1685) vr.icount = vif->pkt_in;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1686) vr.ocount = vif->pkt_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1687) vr.ibytes = vif->bytes_in;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1688) vr.obytes = vif->bytes_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1689) read_unlock(&mrt_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1690)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1691) if (copy_to_user(arg, &vr, sizeof(vr)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1692) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1693) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1694) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1695) read_unlock(&mrt_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1696) return -EADDRNOTAVAIL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1697) case SIOCGETSGCNT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1698) if (copy_from_user(&sr, arg, sizeof(sr)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1699) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1700)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1701) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1702) c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1703) if (c) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1704) sr.pktcnt = c->_c.mfc_un.res.pkt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1705) sr.bytecnt = c->_c.mfc_un.res.bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1706) sr.wrong_if = c->_c.mfc_un.res.wrong_if;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1707) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1708)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1709) if (copy_to_user(arg, &sr, sizeof(sr)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1710) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1711) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1712) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1713) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1714) return -EADDRNOTAVAIL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1715) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1716) return -ENOIOCTLCMD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1717) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1718) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1719) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1720)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1721) static int ipmr_device_event(struct notifier_block *this, unsigned long event, void *ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1722) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1723) struct net_device *dev = netdev_notifier_info_to_dev(ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1724) struct net *net = dev_net(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1725) struct mr_table *mrt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1726) struct vif_device *v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1727) int ct;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1728)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1729) if (event != NETDEV_UNREGISTER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1730) return NOTIFY_DONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1731)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1732) ipmr_for_each_table(mrt, net) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1733) v = &mrt->vif_table[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1734) for (ct = 0; ct < mrt->maxvif; ct++, v++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1735) if (v->dev == dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1736) vif_delete(mrt, ct, 1, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1737) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1738) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1739) return NOTIFY_DONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1740) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1741)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1742) static struct notifier_block ip_mr_notifier = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1743) .notifier_call = ipmr_device_event,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1744) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1745)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1746) /* Encapsulate a packet by attaching a valid IPIP header to it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1747) * This avoids tunnel drivers and other mess and gives us the speed so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1748) * important for multicast video.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1749) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1750) static void ip_encap(struct net *net, struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1751) __be32 saddr, __be32 daddr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1752) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1753) struct iphdr *iph;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1754) const struct iphdr *old_iph = ip_hdr(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1755)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1756) skb_push(skb, sizeof(struct iphdr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1757) skb->transport_header = skb->network_header;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1758) skb_reset_network_header(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1759) iph = ip_hdr(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1760)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1761) iph->version = 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1762) iph->tos = old_iph->tos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1763) iph->ttl = old_iph->ttl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1764) iph->frag_off = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1765) iph->daddr = daddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1766) iph->saddr = saddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1767) iph->protocol = IPPROTO_IPIP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1768) iph->ihl = 5;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1769) iph->tot_len = htons(skb->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1770) ip_select_ident(net, skb, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1771) ip_send_check(iph);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1772)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1773) memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1774) nf_reset_ct(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1775) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1776)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1777) static inline int ipmr_forward_finish(struct net *net, struct sock *sk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1778) struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1779) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1780) struct ip_options *opt = &(IPCB(skb)->opt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1781)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1782) IP_INC_STATS(net, IPSTATS_MIB_OUTFORWDATAGRAMS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1783) IP_ADD_STATS(net, IPSTATS_MIB_OUTOCTETS, skb->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1784)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1785) if (unlikely(opt->optlen))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1786) ip_forward_options(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1787)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1788) return dst_output(net, sk, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1789) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1790)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1791) #ifdef CONFIG_NET_SWITCHDEV
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1792) static bool ipmr_forward_offloaded(struct sk_buff *skb, struct mr_table *mrt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1793) int in_vifi, int out_vifi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1794) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1795) struct vif_device *out_vif = &mrt->vif_table[out_vifi];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1796) struct vif_device *in_vif = &mrt->vif_table[in_vifi];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1797)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1798) if (!skb->offload_l3_fwd_mark)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1799) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1800) if (!out_vif->dev_parent_id.id_len || !in_vif->dev_parent_id.id_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1801) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1802) return netdev_phys_item_id_same(&out_vif->dev_parent_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1803) &in_vif->dev_parent_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1804) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1805) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1806) static bool ipmr_forward_offloaded(struct sk_buff *skb, struct mr_table *mrt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1807) int in_vifi, int out_vifi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1808) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1809) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1810) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1811) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1812)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1813) /* Processing handlers for ipmr_forward */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1814)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1815) static void ipmr_queue_xmit(struct net *net, struct mr_table *mrt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1816) int in_vifi, struct sk_buff *skb, int vifi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1817) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1818) const struct iphdr *iph = ip_hdr(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1819) struct vif_device *vif = &mrt->vif_table[vifi];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1820) struct net_device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1821) struct rtable *rt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1822) struct flowi4 fl4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1823) int encap = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1824)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1825) if (!vif->dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1826) goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1827)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1828) if (vif->flags & VIFF_REGISTER) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1829) vif->pkt_out++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1830) vif->bytes_out += skb->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1831) vif->dev->stats.tx_bytes += skb->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1832) vif->dev->stats.tx_packets++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1833) ipmr_cache_report(mrt, skb, vifi, IGMPMSG_WHOLEPKT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1834) goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1835) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1836)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1837) if (ipmr_forward_offloaded(skb, mrt, in_vifi, vifi))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1838) goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1839)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1840) if (vif->flags & VIFF_TUNNEL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1841) rt = ip_route_output_ports(net, &fl4, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1842) vif->remote, vif->local,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1843) 0, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1844) IPPROTO_IPIP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1845) RT_TOS(iph->tos), vif->link);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1846) if (IS_ERR(rt))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1847) goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1848) encap = sizeof(struct iphdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1849) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1850) rt = ip_route_output_ports(net, &fl4, NULL, iph->daddr, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1851) 0, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1852) IPPROTO_IPIP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1853) RT_TOS(iph->tos), vif->link);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1854) if (IS_ERR(rt))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1855) goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1856) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1857)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1858) dev = rt->dst.dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1859)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1860) if (skb->len+encap > dst_mtu(&rt->dst) && (ntohs(iph->frag_off) & IP_DF)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1861) /* Do not fragment multicasts. Alas, IPv4 does not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1862) * allow to send ICMP, so that packets will disappear
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1863) * to blackhole.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1864) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1865) IP_INC_STATS(net, IPSTATS_MIB_FRAGFAILS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1866) ip_rt_put(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1867) goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1868) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1869)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1870) encap += LL_RESERVED_SPACE(dev) + rt->dst.header_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1871)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1872) if (skb_cow(skb, encap)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1873) ip_rt_put(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1874) goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1875) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1876)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1877) vif->pkt_out++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1878) vif->bytes_out += skb->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1879)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1880) skb_dst_drop(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1881) skb_dst_set(skb, &rt->dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1882) ip_decrease_ttl(ip_hdr(skb));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1883)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1884) /* FIXME: forward and output firewalls used to be called here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1885) * What do we do with netfilter? -- RR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1886) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1887) if (vif->flags & VIFF_TUNNEL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1888) ip_encap(net, skb, vif->local, vif->remote);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1889) /* FIXME: extra output firewall step used to be here. --RR */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1890) vif->dev->stats.tx_packets++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1891) vif->dev->stats.tx_bytes += skb->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1892) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1893)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1894) IPCB(skb)->flags |= IPSKB_FORWARDED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1895)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1896) /* RFC1584 teaches, that DVMRP/PIM router must deliver packets locally
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1897) * not only before forwarding, but after forwarding on all output
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1898) * interfaces. It is clear, if mrouter runs a multicasting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1899) * program, it should receive packets not depending to what interface
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1900) * program is joined.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1901) * If we will not make it, the program will have to join on all
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1902) * interfaces. On the other hand, multihoming host (or router, but
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1903) * not mrouter) cannot join to more than one interface - it will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1904) * result in receiving multiple packets.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1905) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1906) NF_HOOK(NFPROTO_IPV4, NF_INET_FORWARD,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1907) net, NULL, skb, skb->dev, dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1908) ipmr_forward_finish);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1909) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1910)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1911) out_free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1912) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1913) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1914)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1915) static int ipmr_find_vif(struct mr_table *mrt, struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1916) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1917) int ct;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1918)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1919) for (ct = mrt->maxvif-1; ct >= 0; ct--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1920) if (mrt->vif_table[ct].dev == dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1921) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1922) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1923) return ct;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1924) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1925)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1926) /* "local" means that we should preserve one skb (for local delivery) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1927) static void ip_mr_forward(struct net *net, struct mr_table *mrt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1928) struct net_device *dev, struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1929) struct mfc_cache *c, int local)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1930) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1931) int true_vifi = ipmr_find_vif(mrt, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1932) int psend = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1933) int vif, ct;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1934)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1935) vif = c->_c.mfc_parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1936) c->_c.mfc_un.res.pkt++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1937) c->_c.mfc_un.res.bytes += skb->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1938) c->_c.mfc_un.res.lastuse = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1939)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1940) if (c->mfc_origin == htonl(INADDR_ANY) && true_vifi >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1941) struct mfc_cache *cache_proxy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1942)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1943) /* For an (*,G) entry, we only check that the incomming
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1944) * interface is part of the static tree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1945) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1946) cache_proxy = mr_mfc_find_any_parent(mrt, vif);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1947) if (cache_proxy &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1948) cache_proxy->_c.mfc_un.res.ttls[true_vifi] < 255)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1949) goto forward;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1950) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1951)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1952) /* Wrong interface: drop packet and (maybe) send PIM assert. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1953) if (mrt->vif_table[vif].dev != dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1954) if (rt_is_output_route(skb_rtable(skb))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1955) /* It is our own packet, looped back.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1956) * Very complicated situation...
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1957) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1958) * The best workaround until routing daemons will be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1959) * fixed is not to redistribute packet, if it was
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1960) * send through wrong interface. It means, that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1961) * multicast applications WILL NOT work for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1962) * (S,G), which have default multicast route pointing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1963) * to wrong oif. In any case, it is not a good
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1964) * idea to use multicasting applications on router.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1965) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1966) goto dont_forward;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1967) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1968)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1969) c->_c.mfc_un.res.wrong_if++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1970)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1971) if (true_vifi >= 0 && mrt->mroute_do_assert &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1972) /* pimsm uses asserts, when switching from RPT to SPT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1973) * so that we cannot check that packet arrived on an oif.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1974) * It is bad, but otherwise we would need to move pretty
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1975) * large chunk of pimd to kernel. Ough... --ANK
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1976) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1977) (mrt->mroute_do_pim ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1978) c->_c.mfc_un.res.ttls[true_vifi] < 255) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1979) time_after(jiffies,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1980) c->_c.mfc_un.res.last_assert +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1981) MFC_ASSERT_THRESH)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1982) c->_c.mfc_un.res.last_assert = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1983) ipmr_cache_report(mrt, skb, true_vifi, IGMPMSG_WRONGVIF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1984) if (mrt->mroute_do_wrvifwhole)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1985) ipmr_cache_report(mrt, skb, true_vifi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1986) IGMPMSG_WRVIFWHOLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1987) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1988) goto dont_forward;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1989) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1990)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1991) forward:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1992) mrt->vif_table[vif].pkt_in++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1993) mrt->vif_table[vif].bytes_in += skb->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1994)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1995) /* Forward the frame */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1996) if (c->mfc_origin == htonl(INADDR_ANY) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1997) c->mfc_mcastgrp == htonl(INADDR_ANY)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1998) if (true_vifi >= 0 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1999) true_vifi != c->_c.mfc_parent &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2000) ip_hdr(skb)->ttl >
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2001) c->_c.mfc_un.res.ttls[c->_c.mfc_parent]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2002) /* It's an (*,*) entry and the packet is not coming from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2003) * the upstream: forward the packet to the upstream
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2004) * only.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2005) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2006) psend = c->_c.mfc_parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2007) goto last_forward;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2008) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2009) goto dont_forward;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2010) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2011) for (ct = c->_c.mfc_un.res.maxvif - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2012) ct >= c->_c.mfc_un.res.minvif; ct--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2013) /* For (*,G) entry, don't forward to the incoming interface */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2014) if ((c->mfc_origin != htonl(INADDR_ANY) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2015) ct != true_vifi) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2016) ip_hdr(skb)->ttl > c->_c.mfc_un.res.ttls[ct]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2017) if (psend != -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2018) struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2019)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2020) if (skb2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2021) ipmr_queue_xmit(net, mrt, true_vifi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2022) skb2, psend);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2023) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2024) psend = ct;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2025) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2026) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2027) last_forward:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2028) if (psend != -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2029) if (local) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2030) struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2031)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2032) if (skb2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2033) ipmr_queue_xmit(net, mrt, true_vifi, skb2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2034) psend);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2035) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2036) ipmr_queue_xmit(net, mrt, true_vifi, skb, psend);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2037) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2038) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2039) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2040)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2041) dont_forward:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2042) if (!local)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2043) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2044) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2045)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2046) static struct mr_table *ipmr_rt_fib_lookup(struct net *net, struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2047) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2048) struct rtable *rt = skb_rtable(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2049) struct iphdr *iph = ip_hdr(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2050) struct flowi4 fl4 = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2051) .daddr = iph->daddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2052) .saddr = iph->saddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2053) .flowi4_tos = RT_TOS(iph->tos),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2054) .flowi4_oif = (rt_is_output_route(rt) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2055) skb->dev->ifindex : 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2056) .flowi4_iif = (rt_is_output_route(rt) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2057) LOOPBACK_IFINDEX :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2058) skb->dev->ifindex),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2059) .flowi4_mark = skb->mark,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2060) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2061) struct mr_table *mrt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2062) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2063)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2064) err = ipmr_fib_lookup(net, &fl4, &mrt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2065) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2066) return ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2067) return mrt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2068) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2069)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2070) /* Multicast packets for forwarding arrive here
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2071) * Called with rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2072) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2073) int ip_mr_input(struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2074) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2075) struct mfc_cache *cache;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2076) struct net *net = dev_net(skb->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2077) int local = skb_rtable(skb)->rt_flags & RTCF_LOCAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2078) struct mr_table *mrt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2079) struct net_device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2080)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2081) /* skb->dev passed in is the loX master dev for vrfs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2082) * As there are no vifs associated with loopback devices,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2083) * get the proper interface that does have a vif associated with it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2084) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2085) dev = skb->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2086) if (netif_is_l3_master(skb->dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2087) dev = dev_get_by_index_rcu(net, IPCB(skb)->iif);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2088) if (!dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2089) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2090) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2091) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2092) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2093)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2094) /* Packet is looped back after forward, it should not be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2095) * forwarded second time, but still can be delivered locally.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2096) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2097) if (IPCB(skb)->flags & IPSKB_FORWARDED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2098) goto dont_forward;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2099)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2100) mrt = ipmr_rt_fib_lookup(net, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2101) if (IS_ERR(mrt)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2102) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2103) return PTR_ERR(mrt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2104) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2105) if (!local) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2106) if (IPCB(skb)->opt.router_alert) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2107) if (ip_call_ra_chain(skb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2108) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2109) } else if (ip_hdr(skb)->protocol == IPPROTO_IGMP) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2110) /* IGMPv1 (and broken IGMPv2 implementations sort of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2111) * Cisco IOS <= 11.2(8)) do not put router alert
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2112) * option to IGMP packets destined to routable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2113) * groups. It is very bad, because it means
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2114) * that we can forward NO IGMP messages.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2115) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2116) struct sock *mroute_sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2118) mroute_sk = rcu_dereference(mrt->mroute_sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2119) if (mroute_sk) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2120) nf_reset_ct(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2121) raw_rcv(mroute_sk, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2122) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2123) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2124) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2125) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2127) /* already under rcu_read_lock() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2128) cache = ipmr_cache_find(mrt, ip_hdr(skb)->saddr, ip_hdr(skb)->daddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2129) if (!cache) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2130) int vif = ipmr_find_vif(mrt, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2132) if (vif >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2133) cache = ipmr_cache_find_any(mrt, ip_hdr(skb)->daddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2134) vif);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2135) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2137) /* No usable cache entry */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2138) if (!cache) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2139) int vif;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2141) if (local) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2142) struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2143) ip_local_deliver(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2144) if (!skb2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2145) return -ENOBUFS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2146) skb = skb2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2147) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2149) read_lock(&mrt_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2150) vif = ipmr_find_vif(mrt, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2151) if (vif >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2152) int err2 = ipmr_cache_unresolved(mrt, vif, skb, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2153) read_unlock(&mrt_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2155) return err2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2156) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2157) read_unlock(&mrt_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2158) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2159) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2160) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2162) read_lock(&mrt_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2163) ip_mr_forward(net, mrt, dev, skb, cache, local);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2164) read_unlock(&mrt_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2166) if (local)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2167) return ip_local_deliver(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2168)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2169) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2171) dont_forward:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2172) if (local)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2173) return ip_local_deliver(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2174) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2175) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2176) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2178) #ifdef CONFIG_IP_PIMSM_V1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2179) /* Handle IGMP messages of PIMv1 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2180) int pim_rcv_v1(struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2181) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2182) struct igmphdr *pim;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2183) struct net *net = dev_net(skb->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2184) struct mr_table *mrt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2185)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2186) if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2187) goto drop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2188)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2189) pim = igmp_hdr(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2190)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2191) mrt = ipmr_rt_fib_lookup(net, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2192) if (IS_ERR(mrt))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2193) goto drop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2194) if (!mrt->mroute_do_pim ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2195) pim->group != PIM_V1_VERSION || pim->code != PIM_V1_REGISTER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2196) goto drop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2197)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2198) if (__pim_rcv(mrt, skb, sizeof(*pim))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2199) drop:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2200) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2201) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2202) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2203) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2204) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2205)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2206) #ifdef CONFIG_IP_PIMSM_V2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2207) static int pim_rcv(struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2208) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2209) struct pimreghdr *pim;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2210) struct net *net = dev_net(skb->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2211) struct mr_table *mrt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2212)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2213) if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2214) goto drop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2215)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2216) pim = (struct pimreghdr *)skb_transport_header(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2217) if (pim->type != ((PIM_VERSION << 4) | (PIM_TYPE_REGISTER)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2218) (pim->flags & PIM_NULL_REGISTER) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2219) (ip_compute_csum((void *)pim, sizeof(*pim)) != 0 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2220) csum_fold(skb_checksum(skb, 0, skb->len, 0))))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2221) goto drop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2222)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2223) mrt = ipmr_rt_fib_lookup(net, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2224) if (IS_ERR(mrt))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2225) goto drop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2226) if (__pim_rcv(mrt, skb, sizeof(*pim))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2227) drop:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2228) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2229) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2230) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2231) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2232) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2233)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2234) int ipmr_get_route(struct net *net, struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2235) __be32 saddr, __be32 daddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2236) struct rtmsg *rtm, u32 portid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2237) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2238) struct mfc_cache *cache;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2239) struct mr_table *mrt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2240) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2241)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2242) mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2243) if (!mrt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2244) return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2245)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2246) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2247) cache = ipmr_cache_find(mrt, saddr, daddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2248) if (!cache && skb->dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2249) int vif = ipmr_find_vif(mrt, skb->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2250)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2251) if (vif >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2252) cache = ipmr_cache_find_any(mrt, daddr, vif);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2253) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2254) if (!cache) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2255) struct sk_buff *skb2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2256) struct iphdr *iph;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2257) struct net_device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2258) int vif = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2259)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2260) dev = skb->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2261) read_lock(&mrt_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2262) if (dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2263) vif = ipmr_find_vif(mrt, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2264) if (vif < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2265) read_unlock(&mrt_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2266) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2267) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2268) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2269)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2270) skb2 = skb_realloc_headroom(skb, sizeof(struct iphdr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2271) if (!skb2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2272) read_unlock(&mrt_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2273) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2274) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2275) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2276)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2277) NETLINK_CB(skb2).portid = portid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2278) skb_push(skb2, sizeof(struct iphdr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2279) skb_reset_network_header(skb2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2280) iph = ip_hdr(skb2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2281) iph->ihl = sizeof(struct iphdr) >> 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2282) iph->saddr = saddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2283) iph->daddr = daddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2284) iph->version = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2285) err = ipmr_cache_unresolved(mrt, vif, skb2, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2286) read_unlock(&mrt_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2287) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2288) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2289) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2290)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2291) read_lock(&mrt_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2292) err = mr_fill_mroute(mrt, skb, &cache->_c, rtm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2293) read_unlock(&mrt_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2294) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2295) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2296) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2297)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2298) static int ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2299) u32 portid, u32 seq, struct mfc_cache *c, int cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2300) int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2301) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2302) struct nlmsghdr *nlh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2303) struct rtmsg *rtm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2304) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2305)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2306) nlh = nlmsg_put(skb, portid, seq, cmd, sizeof(*rtm), flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2307) if (!nlh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2308) return -EMSGSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2309)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2310) rtm = nlmsg_data(nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2311) rtm->rtm_family = RTNL_FAMILY_IPMR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2312) rtm->rtm_dst_len = 32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2313) rtm->rtm_src_len = 32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2314) rtm->rtm_tos = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2315) rtm->rtm_table = mrt->id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2316) if (nla_put_u32(skb, RTA_TABLE, mrt->id))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2317) goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2318) rtm->rtm_type = RTN_MULTICAST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2319) rtm->rtm_scope = RT_SCOPE_UNIVERSE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2320) if (c->_c.mfc_flags & MFC_STATIC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2321) rtm->rtm_protocol = RTPROT_STATIC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2322) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2323) rtm->rtm_protocol = RTPROT_MROUTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2324) rtm->rtm_flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2325)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2326) if (nla_put_in_addr(skb, RTA_SRC, c->mfc_origin) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2327) nla_put_in_addr(skb, RTA_DST, c->mfc_mcastgrp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2328) goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2329) err = mr_fill_mroute(mrt, skb, &c->_c, rtm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2330) /* do not break the dump if cache is unresolved */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2331) if (err < 0 && err != -ENOENT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2332) goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2333)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2334) nlmsg_end(skb, nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2335) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2336)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2337) nla_put_failure:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2338) nlmsg_cancel(skb, nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2339) return -EMSGSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2340) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2341)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2342) static int _ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2343) u32 portid, u32 seq, struct mr_mfc *c, int cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2344) int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2345) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2346) return ipmr_fill_mroute(mrt, skb, portid, seq, (struct mfc_cache *)c,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2347) cmd, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2348) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2349)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2350) static size_t mroute_msgsize(bool unresolved, int maxvif)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2351) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2352) size_t len =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2353) NLMSG_ALIGN(sizeof(struct rtmsg))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2354) + nla_total_size(4) /* RTA_TABLE */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2355) + nla_total_size(4) /* RTA_SRC */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2356) + nla_total_size(4) /* RTA_DST */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2357) ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2358)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2359) if (!unresolved)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2360) len = len
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2361) + nla_total_size(4) /* RTA_IIF */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2362) + nla_total_size(0) /* RTA_MULTIPATH */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2363) + maxvif * NLA_ALIGN(sizeof(struct rtnexthop))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2364) /* RTA_MFC_STATS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2365) + nla_total_size_64bit(sizeof(struct rta_mfc_stats))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2366) ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2367)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2368) return len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2369) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2370)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2371) static void mroute_netlink_event(struct mr_table *mrt, struct mfc_cache *mfc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2372) int cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2373) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2374) struct net *net = read_pnet(&mrt->net);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2375) struct sk_buff *skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2376) int err = -ENOBUFS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2377)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2378) skb = nlmsg_new(mroute_msgsize(mfc->_c.mfc_parent >= MAXVIFS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2379) mrt->maxvif),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2380) GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2381) if (!skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2382) goto errout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2383)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2384) err = ipmr_fill_mroute(mrt, skb, 0, 0, mfc, cmd, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2385) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2386) goto errout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2387)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2388) rtnl_notify(skb, net, 0, RTNLGRP_IPV4_MROUTE, NULL, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2389) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2390)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2391) errout:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2392) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2393) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2394) rtnl_set_sk_err(net, RTNLGRP_IPV4_MROUTE, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2395) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2396)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2397) static size_t igmpmsg_netlink_msgsize(size_t payloadlen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2398) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2399) size_t len =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2400) NLMSG_ALIGN(sizeof(struct rtgenmsg))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2401) + nla_total_size(1) /* IPMRA_CREPORT_MSGTYPE */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2402) + nla_total_size(4) /* IPMRA_CREPORT_VIF_ID */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2403) + nla_total_size(4) /* IPMRA_CREPORT_SRC_ADDR */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2404) + nla_total_size(4) /* IPMRA_CREPORT_DST_ADDR */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2405) + nla_total_size(4) /* IPMRA_CREPORT_TABLE */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2406) /* IPMRA_CREPORT_PKT */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2407) + nla_total_size(payloadlen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2408) ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2409)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2410) return len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2411) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2412)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2413) static void igmpmsg_netlink_event(struct mr_table *mrt, struct sk_buff *pkt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2414) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2415) struct net *net = read_pnet(&mrt->net);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2416) struct nlmsghdr *nlh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2417) struct rtgenmsg *rtgenm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2418) struct igmpmsg *msg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2419) struct sk_buff *skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2420) struct nlattr *nla;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2421) int payloadlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2422)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2423) payloadlen = pkt->len - sizeof(struct igmpmsg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2424) msg = (struct igmpmsg *)skb_network_header(pkt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2425)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2426) skb = nlmsg_new(igmpmsg_netlink_msgsize(payloadlen), GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2427) if (!skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2428) goto errout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2429)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2430) nlh = nlmsg_put(skb, 0, 0, RTM_NEWCACHEREPORT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2431) sizeof(struct rtgenmsg), 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2432) if (!nlh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2433) goto errout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2434) rtgenm = nlmsg_data(nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2435) rtgenm->rtgen_family = RTNL_FAMILY_IPMR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2436) if (nla_put_u8(skb, IPMRA_CREPORT_MSGTYPE, msg->im_msgtype) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2437) nla_put_u32(skb, IPMRA_CREPORT_VIF_ID, msg->im_vif | (msg->im_vif_hi << 8)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2438) nla_put_in_addr(skb, IPMRA_CREPORT_SRC_ADDR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2439) msg->im_src.s_addr) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2440) nla_put_in_addr(skb, IPMRA_CREPORT_DST_ADDR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2441) msg->im_dst.s_addr) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2442) nla_put_u32(skb, IPMRA_CREPORT_TABLE, mrt->id))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2443) goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2444)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2445) nla = nla_reserve(skb, IPMRA_CREPORT_PKT, payloadlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2446) if (!nla || skb_copy_bits(pkt, sizeof(struct igmpmsg),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2447) nla_data(nla), payloadlen))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2448) goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2449)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2450) nlmsg_end(skb, nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2451)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2452) rtnl_notify(skb, net, 0, RTNLGRP_IPV4_MROUTE_R, NULL, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2453) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2454)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2455) nla_put_failure:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2456) nlmsg_cancel(skb, nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2457) errout:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2458) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2459) rtnl_set_sk_err(net, RTNLGRP_IPV4_MROUTE_R, -ENOBUFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2460) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2461)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2462) static int ipmr_rtm_valid_getroute_req(struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2463) const struct nlmsghdr *nlh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2464) struct nlattr **tb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2465) struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2466) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2467) struct rtmsg *rtm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2468) int i, err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2469)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2470) if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*rtm))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2471) NL_SET_ERR_MSG(extack, "ipv4: Invalid header for multicast route get request");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2472) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2473) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2474)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2475) if (!netlink_strict_get_check(skb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2476) return nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2477) rtm_ipv4_policy, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2478)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2479) rtm = nlmsg_data(nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2480) if ((rtm->rtm_src_len && rtm->rtm_src_len != 32) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2481) (rtm->rtm_dst_len && rtm->rtm_dst_len != 32) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2482) rtm->rtm_tos || rtm->rtm_table || rtm->rtm_protocol ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2483) rtm->rtm_scope || rtm->rtm_type || rtm->rtm_flags) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2484) NL_SET_ERR_MSG(extack, "ipv4: Invalid values in header for multicast route get request");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2485) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2486) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2487)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2488) err = nlmsg_parse_deprecated_strict(nlh, sizeof(*rtm), tb, RTA_MAX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2489) rtm_ipv4_policy, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2490) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2491) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2492)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2493) if ((tb[RTA_SRC] && !rtm->rtm_src_len) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2494) (tb[RTA_DST] && !rtm->rtm_dst_len)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2495) NL_SET_ERR_MSG(extack, "ipv4: rtm_src_len and rtm_dst_len must be 32 for IPv4");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2496) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2497) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2498)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2499) for (i = 0; i <= RTA_MAX; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2500) if (!tb[i])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2501) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2502)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2503) switch (i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2504) case RTA_SRC:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2505) case RTA_DST:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2506) case RTA_TABLE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2507) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2508) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2509) NL_SET_ERR_MSG(extack, "ipv4: Unsupported attribute in multicast route get request");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2510) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2511) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2512) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2513)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2514) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2515) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2516)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2517) static int ipmr_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2518) struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2519) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2520) struct net *net = sock_net(in_skb->sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2521) struct nlattr *tb[RTA_MAX + 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2522) struct sk_buff *skb = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2523) struct mfc_cache *cache;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2524) struct mr_table *mrt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2525) __be32 src, grp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2526) u32 tableid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2527) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2528)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2529) err = ipmr_rtm_valid_getroute_req(in_skb, nlh, tb, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2530) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2531) goto errout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2532)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2533) src = tb[RTA_SRC] ? nla_get_in_addr(tb[RTA_SRC]) : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2534) grp = tb[RTA_DST] ? nla_get_in_addr(tb[RTA_DST]) : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2535) tableid = tb[RTA_TABLE] ? nla_get_u32(tb[RTA_TABLE]) : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2536)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2537) mrt = ipmr_get_table(net, tableid ? tableid : RT_TABLE_DEFAULT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2538) if (!mrt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2539) err = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2540) goto errout_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2541) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2542)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2543) /* entries are added/deleted only under RTNL */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2544) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2545) cache = ipmr_cache_find(mrt, src, grp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2546) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2547) if (!cache) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2548) err = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2549) goto errout_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2550) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2551)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2552) skb = nlmsg_new(mroute_msgsize(false, mrt->maxvif), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2553) if (!skb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2554) err = -ENOBUFS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2555) goto errout_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2556) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2557)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2558) err = ipmr_fill_mroute(mrt, skb, NETLINK_CB(in_skb).portid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2559) nlh->nlmsg_seq, cache,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2560) RTM_NEWROUTE, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2561) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2562) goto errout_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2563)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2564) err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2565)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2566) errout:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2567) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2568)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2569) errout_free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2570) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2571) goto errout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2572) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2573)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2574) static int ipmr_rtm_dumproute(struct sk_buff *skb, struct netlink_callback *cb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2575) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2576) struct fib_dump_filter filter = {};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2577) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2578)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2579) if (cb->strict_check) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2580) err = ip_valid_fib_dump_req(sock_net(skb->sk), cb->nlh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2581) &filter, cb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2582) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2583) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2584) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2585)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2586) if (filter.table_id) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2587) struct mr_table *mrt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2588)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2589) mrt = ipmr_get_table(sock_net(skb->sk), filter.table_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2590) if (!mrt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2591) if (rtnl_msg_family(cb->nlh) != RTNL_FAMILY_IPMR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2592) return skb->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2593)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2594) NL_SET_ERR_MSG(cb->extack, "ipv4: MR table does not exist");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2595) return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2596) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2597) err = mr_table_dump(mrt, skb, cb, _ipmr_fill_mroute,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2598) &mfc_unres_lock, &filter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2599) return skb->len ? : err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2600) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2601)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2602) return mr_rtm_dumproute(skb, cb, ipmr_mr_table_iter,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2603) _ipmr_fill_mroute, &mfc_unres_lock, &filter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2604) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2605)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2606) static const struct nla_policy rtm_ipmr_policy[RTA_MAX + 1] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2607) [RTA_SRC] = { .type = NLA_U32 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2608) [RTA_DST] = { .type = NLA_U32 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2609) [RTA_IIF] = { .type = NLA_U32 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2610) [RTA_TABLE] = { .type = NLA_U32 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2611) [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2612) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2613)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2614) static bool ipmr_rtm_validate_proto(unsigned char rtm_protocol)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2615) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2616) switch (rtm_protocol) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2617) case RTPROT_STATIC:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2618) case RTPROT_MROUTED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2619) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2620) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2621) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2622) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2623)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2624) static int ipmr_nla_get_ttls(const struct nlattr *nla, struct mfcctl *mfcc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2625) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2626) struct rtnexthop *rtnh = nla_data(nla);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2627) int remaining = nla_len(nla), vifi = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2628)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2629) while (rtnh_ok(rtnh, remaining)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2630) mfcc->mfcc_ttls[vifi] = rtnh->rtnh_hops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2631) if (++vifi == MAXVIFS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2632) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2633) rtnh = rtnh_next(rtnh, &remaining);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2634) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2635)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2636) return remaining > 0 ? -EINVAL : vifi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2637) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2638)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2639) /* returns < 0 on error, 0 for ADD_MFC and 1 for ADD_MFC_PROXY */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2640) static int rtm_to_ipmr_mfcc(struct net *net, struct nlmsghdr *nlh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2641) struct mfcctl *mfcc, int *mrtsock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2642) struct mr_table **mrtret,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2643) struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2644) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2645) struct net_device *dev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2646) u32 tblid = RT_TABLE_DEFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2647) struct mr_table *mrt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2648) struct nlattr *attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2649) struct rtmsg *rtm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2650) int ret, rem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2651)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2652) ret = nlmsg_validate_deprecated(nlh, sizeof(*rtm), RTA_MAX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2653) rtm_ipmr_policy, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2654) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2655) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2656) rtm = nlmsg_data(nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2657)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2658) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2659) if (rtm->rtm_family != RTNL_FAMILY_IPMR || rtm->rtm_dst_len != 32 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2660) rtm->rtm_type != RTN_MULTICAST ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2661) rtm->rtm_scope != RT_SCOPE_UNIVERSE ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2662) !ipmr_rtm_validate_proto(rtm->rtm_protocol))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2663) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2664)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2665) memset(mfcc, 0, sizeof(*mfcc));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2666) mfcc->mfcc_parent = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2667) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2668) nlmsg_for_each_attr(attr, nlh, sizeof(struct rtmsg), rem) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2669) switch (nla_type(attr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2670) case RTA_SRC:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2671) mfcc->mfcc_origin.s_addr = nla_get_be32(attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2672) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2673) case RTA_DST:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2674) mfcc->mfcc_mcastgrp.s_addr = nla_get_be32(attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2675) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2676) case RTA_IIF:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2677) dev = __dev_get_by_index(net, nla_get_u32(attr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2678) if (!dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2679) ret = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2680) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2681) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2682) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2683) case RTA_MULTIPATH:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2684) if (ipmr_nla_get_ttls(attr, mfcc) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2685) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2686) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2687) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2688) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2689) case RTA_PREFSRC:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2690) ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2691) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2692) case RTA_TABLE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2693) tblid = nla_get_u32(attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2694) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2695) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2696) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2697) mrt = ipmr_get_table(net, tblid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2698) if (!mrt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2699) ret = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2700) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2701) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2702) *mrtret = mrt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2703) *mrtsock = rtm->rtm_protocol == RTPROT_MROUTED ? 1 : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2704) if (dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2705) mfcc->mfcc_parent = ipmr_find_vif(mrt, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2706)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2707) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2708) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2709) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2710)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2711) /* takes care of both newroute and delroute */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2712) static int ipmr_rtm_route(struct sk_buff *skb, struct nlmsghdr *nlh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2713) struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2714) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2715) struct net *net = sock_net(skb->sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2716) int ret, mrtsock, parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2717) struct mr_table *tbl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2718) struct mfcctl mfcc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2719)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2720) mrtsock = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2721) tbl = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2722) ret = rtm_to_ipmr_mfcc(net, nlh, &mfcc, &mrtsock, &tbl, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2723) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2724) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2725)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2726) parent = ret ? mfcc.mfcc_parent : -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2727) if (nlh->nlmsg_type == RTM_NEWROUTE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2728) return ipmr_mfc_add(net, tbl, &mfcc, mrtsock, parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2729) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2730) return ipmr_mfc_delete(tbl, &mfcc, parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2731) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2732)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2733) static bool ipmr_fill_table(struct mr_table *mrt, struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2734) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2735) u32 queue_len = atomic_read(&mrt->cache_resolve_queue_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2736)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2737) if (nla_put_u32(skb, IPMRA_TABLE_ID, mrt->id) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2738) nla_put_u32(skb, IPMRA_TABLE_CACHE_RES_QUEUE_LEN, queue_len) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2739) nla_put_s32(skb, IPMRA_TABLE_MROUTE_REG_VIF_NUM,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2740) mrt->mroute_reg_vif_num) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2741) nla_put_u8(skb, IPMRA_TABLE_MROUTE_DO_ASSERT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2742) mrt->mroute_do_assert) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2743) nla_put_u8(skb, IPMRA_TABLE_MROUTE_DO_PIM, mrt->mroute_do_pim) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2744) nla_put_u8(skb, IPMRA_TABLE_MROUTE_DO_WRVIFWHOLE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2745) mrt->mroute_do_wrvifwhole))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2746) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2747)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2748) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2749) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2750)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2751) static bool ipmr_fill_vif(struct mr_table *mrt, u32 vifid, struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2752) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2753) struct nlattr *vif_nest;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2754) struct vif_device *vif;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2755)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2756) /* if the VIF doesn't exist just continue */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2757) if (!VIF_EXISTS(mrt, vifid))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2758) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2759)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2760) vif = &mrt->vif_table[vifid];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2761) vif_nest = nla_nest_start_noflag(skb, IPMRA_VIF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2762) if (!vif_nest)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2763) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2764) if (nla_put_u32(skb, IPMRA_VIFA_IFINDEX, vif->dev->ifindex) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2765) nla_put_u32(skb, IPMRA_VIFA_VIF_ID, vifid) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2766) nla_put_u16(skb, IPMRA_VIFA_FLAGS, vif->flags) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2767) nla_put_u64_64bit(skb, IPMRA_VIFA_BYTES_IN, vif->bytes_in,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2768) IPMRA_VIFA_PAD) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2769) nla_put_u64_64bit(skb, IPMRA_VIFA_BYTES_OUT, vif->bytes_out,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2770) IPMRA_VIFA_PAD) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2771) nla_put_u64_64bit(skb, IPMRA_VIFA_PACKETS_IN, vif->pkt_in,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2772) IPMRA_VIFA_PAD) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2773) nla_put_u64_64bit(skb, IPMRA_VIFA_PACKETS_OUT, vif->pkt_out,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2774) IPMRA_VIFA_PAD) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2775) nla_put_be32(skb, IPMRA_VIFA_LOCAL_ADDR, vif->local) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2776) nla_put_be32(skb, IPMRA_VIFA_REMOTE_ADDR, vif->remote)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2777) nla_nest_cancel(skb, vif_nest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2778) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2779) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2780) nla_nest_end(skb, vif_nest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2781)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2782) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2783) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2784)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2785) static int ipmr_valid_dumplink(const struct nlmsghdr *nlh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2786) struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2787) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2788) struct ifinfomsg *ifm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2789)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2790) if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2791) NL_SET_ERR_MSG(extack, "ipv4: Invalid header for ipmr link dump");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2792) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2793) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2794)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2795) if (nlmsg_attrlen(nlh, sizeof(*ifm))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2796) NL_SET_ERR_MSG(extack, "Invalid data after header in ipmr link dump");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2797) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2798) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2799)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2800) ifm = nlmsg_data(nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2801) if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2802) ifm->ifi_change || ifm->ifi_index) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2803) NL_SET_ERR_MSG(extack, "Invalid values in header for ipmr link dump request");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2804) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2805) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2806)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2807) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2808) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2809)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2810) static int ipmr_rtm_dumplink(struct sk_buff *skb, struct netlink_callback *cb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2811) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2812) struct net *net = sock_net(skb->sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2813) struct nlmsghdr *nlh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2814) unsigned int t = 0, s_t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2815) unsigned int e = 0, s_e;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2816) struct mr_table *mrt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2817)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2818) if (cb->strict_check) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2819) int err = ipmr_valid_dumplink(cb->nlh, cb->extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2820)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2821) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2822) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2823) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2824)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2825) s_t = cb->args[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2826) s_e = cb->args[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2827)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2828) ipmr_for_each_table(mrt, net) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2829) struct nlattr *vifs, *af;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2830) struct ifinfomsg *hdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2831) u32 i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2832)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2833) if (t < s_t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2834) goto skip_table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2835) nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2836) cb->nlh->nlmsg_seq, RTM_NEWLINK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2837) sizeof(*hdr), NLM_F_MULTI);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2838) if (!nlh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2839) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2840)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2841) hdr = nlmsg_data(nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2842) memset(hdr, 0, sizeof(*hdr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2843) hdr->ifi_family = RTNL_FAMILY_IPMR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2844)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2845) af = nla_nest_start_noflag(skb, IFLA_AF_SPEC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2846) if (!af) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2847) nlmsg_cancel(skb, nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2848) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2849) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2850)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2851) if (!ipmr_fill_table(mrt, skb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2852) nlmsg_cancel(skb, nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2853) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2854) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2855)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2856) vifs = nla_nest_start_noflag(skb, IPMRA_TABLE_VIFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2857) if (!vifs) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2858) nla_nest_end(skb, af);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2859) nlmsg_end(skb, nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2860) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2861) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2862) for (i = 0; i < mrt->maxvif; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2863) if (e < s_e)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2864) goto skip_entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2865) if (!ipmr_fill_vif(mrt, i, skb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2866) nla_nest_end(skb, vifs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2867) nla_nest_end(skb, af);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2868) nlmsg_end(skb, nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2869) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2870) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2871) skip_entry:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2872) e++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2873) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2874) s_e = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2875) e = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2876) nla_nest_end(skb, vifs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2877) nla_nest_end(skb, af);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2878) nlmsg_end(skb, nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2879) skip_table:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2880) t++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2881) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2882)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2883) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2884) cb->args[1] = e;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2885) cb->args[0] = t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2886)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2887) return skb->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2888) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2889)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2890) #ifdef CONFIG_PROC_FS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2891) /* The /proc interfaces to multicast routing :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2892) * /proc/net/ip_mr_cache & /proc/net/ip_mr_vif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2893) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2894)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2895) static void *ipmr_vif_seq_start(struct seq_file *seq, loff_t *pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2896) __acquires(mrt_lock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2897) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2898) struct mr_vif_iter *iter = seq->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2899) struct net *net = seq_file_net(seq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2900) struct mr_table *mrt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2901)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2902) mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2903) if (!mrt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2904) return ERR_PTR(-ENOENT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2905)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2906) iter->mrt = mrt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2907)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2908) read_lock(&mrt_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2909) return mr_vif_seq_start(seq, pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2910) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2911)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2912) static void ipmr_vif_seq_stop(struct seq_file *seq, void *v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2913) __releases(mrt_lock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2914) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2915) read_unlock(&mrt_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2916) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2917)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2918) static int ipmr_vif_seq_show(struct seq_file *seq, void *v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2919) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2920) struct mr_vif_iter *iter = seq->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2921) struct mr_table *mrt = iter->mrt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2922)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2923) if (v == SEQ_START_TOKEN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2924) seq_puts(seq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2925) "Interface BytesIn PktsIn BytesOut PktsOut Flags Local Remote\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2926) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2927) const struct vif_device *vif = v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2928) const char *name = vif->dev ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2929) vif->dev->name : "none";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2930)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2931) seq_printf(seq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2932) "%2td %-10s %8ld %7ld %8ld %7ld %05X %08X %08X\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2933) vif - mrt->vif_table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2934) name, vif->bytes_in, vif->pkt_in,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2935) vif->bytes_out, vif->pkt_out,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2936) vif->flags, vif->local, vif->remote);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2937) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2938) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2939) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2940)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2941) static const struct seq_operations ipmr_vif_seq_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2942) .start = ipmr_vif_seq_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2943) .next = mr_vif_seq_next,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2944) .stop = ipmr_vif_seq_stop,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2945) .show = ipmr_vif_seq_show,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2946) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2947)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2948) static void *ipmr_mfc_seq_start(struct seq_file *seq, loff_t *pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2949) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2950) struct net *net = seq_file_net(seq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2951) struct mr_table *mrt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2952)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2953) mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2954) if (!mrt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2955) return ERR_PTR(-ENOENT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2956)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2957) return mr_mfc_seq_start(seq, pos, mrt, &mfc_unres_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2958) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2959)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2960) static int ipmr_mfc_seq_show(struct seq_file *seq, void *v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2961) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2962) int n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2963)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2964) if (v == SEQ_START_TOKEN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2965) seq_puts(seq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2966) "Group Origin Iif Pkts Bytes Wrong Oifs\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2967) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2968) const struct mfc_cache *mfc = v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2969) const struct mr_mfc_iter *it = seq->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2970) const struct mr_table *mrt = it->mrt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2971)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2972) seq_printf(seq, "%08X %08X %-3hd",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2973) (__force u32) mfc->mfc_mcastgrp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2974) (__force u32) mfc->mfc_origin,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2975) mfc->_c.mfc_parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2976)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2977) if (it->cache != &mrt->mfc_unres_queue) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2978) seq_printf(seq, " %8lu %8lu %8lu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2979) mfc->_c.mfc_un.res.pkt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2980) mfc->_c.mfc_un.res.bytes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2981) mfc->_c.mfc_un.res.wrong_if);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2982) for (n = mfc->_c.mfc_un.res.minvif;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2983) n < mfc->_c.mfc_un.res.maxvif; n++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2984) if (VIF_EXISTS(mrt, n) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2985) mfc->_c.mfc_un.res.ttls[n] < 255)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2986) seq_printf(seq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2987) " %2d:%-3d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2988) n, mfc->_c.mfc_un.res.ttls[n]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2989) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2990) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2991) /* unresolved mfc_caches don't contain
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2992) * pkt, bytes and wrong_if values
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2993) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2994) seq_printf(seq, " %8lu %8lu %8lu", 0ul, 0ul, 0ul);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2995) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2996) seq_putc(seq, '\n');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2997) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2998) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2999) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3000)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3001) static const struct seq_operations ipmr_mfc_seq_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3002) .start = ipmr_mfc_seq_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3003) .next = mr_mfc_seq_next,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3004) .stop = mr_mfc_seq_stop,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3005) .show = ipmr_mfc_seq_show,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3006) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3007) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3008)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3009) #ifdef CONFIG_IP_PIMSM_V2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3010) static const struct net_protocol pim_protocol = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3011) .handler = pim_rcv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3012) .netns_ok = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3013) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3014) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3015)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3016) static unsigned int ipmr_seq_read(struct net *net)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3017) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3018) ASSERT_RTNL();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3019)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3020) return net->ipv4.ipmr_seq + ipmr_rules_seq_read(net);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3021) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3022)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3023) static int ipmr_dump(struct net *net, struct notifier_block *nb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3024) struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3025) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3026) return mr_dump(net, nb, RTNL_FAMILY_IPMR, ipmr_rules_dump,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3027) ipmr_mr_table_iter, &mrt_lock, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3028) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3029)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3030) static const struct fib_notifier_ops ipmr_notifier_ops_template = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3031) .family = RTNL_FAMILY_IPMR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3032) .fib_seq_read = ipmr_seq_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3033) .fib_dump = ipmr_dump,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3034) .owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3035) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3036)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3037) static int __net_init ipmr_notifier_init(struct net *net)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3038) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3039) struct fib_notifier_ops *ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3040)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3041) net->ipv4.ipmr_seq = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3042)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3043) ops = fib_notifier_ops_register(&ipmr_notifier_ops_template, net);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3044) if (IS_ERR(ops))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3045) return PTR_ERR(ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3046) net->ipv4.ipmr_notifier_ops = ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3047)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3048) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3049) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3050)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3051) static void __net_exit ipmr_notifier_exit(struct net *net)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3052) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3053) fib_notifier_ops_unregister(net->ipv4.ipmr_notifier_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3054) net->ipv4.ipmr_notifier_ops = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3055) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3056)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3057) /* Setup for IP multicast routing */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3058) static int __net_init ipmr_net_init(struct net *net)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3059) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3060) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3061)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3062) err = ipmr_notifier_init(net);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3063) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3064) goto ipmr_notifier_fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3065)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3066) err = ipmr_rules_init(net);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3067) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3068) goto ipmr_rules_fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3069)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3070) #ifdef CONFIG_PROC_FS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3071) err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3072) if (!proc_create_net("ip_mr_vif", 0, net->proc_net, &ipmr_vif_seq_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3073) sizeof(struct mr_vif_iter)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3074) goto proc_vif_fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3075) if (!proc_create_net("ip_mr_cache", 0, net->proc_net, &ipmr_mfc_seq_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3076) sizeof(struct mr_mfc_iter)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3077) goto proc_cache_fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3078) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3079) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3080)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3081) #ifdef CONFIG_PROC_FS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3082) proc_cache_fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3083) remove_proc_entry("ip_mr_vif", net->proc_net);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3084) proc_vif_fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3085) ipmr_rules_exit(net);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3086) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3087) ipmr_rules_fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3088) ipmr_notifier_exit(net);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3089) ipmr_notifier_fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3090) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3091) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3092)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3093) static void __net_exit ipmr_net_exit(struct net *net)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3094) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3095) #ifdef CONFIG_PROC_FS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3096) remove_proc_entry("ip_mr_cache", net->proc_net);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3097) remove_proc_entry("ip_mr_vif", net->proc_net);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3098) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3099) ipmr_notifier_exit(net);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3100) ipmr_rules_exit(net);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3101) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3103) static struct pernet_operations ipmr_net_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3104) .init = ipmr_net_init,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3105) .exit = ipmr_net_exit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3106) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3108) int __init ip_mr_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3109) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3110) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3112) mrt_cachep = kmem_cache_create("ip_mrt_cache",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3113) sizeof(struct mfc_cache),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3114) 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3115) NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3117) err = register_pernet_subsys(&ipmr_net_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3118) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3119) goto reg_pernet_fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3121) err = register_netdevice_notifier(&ip_mr_notifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3122) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3123) goto reg_notif_fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3124) #ifdef CONFIG_IP_PIMSM_V2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3125) if (inet_add_protocol(&pim_protocol, IPPROTO_PIM) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3126) pr_err("%s: can't add PIM protocol\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3127) err = -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3128) goto add_proto_fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3129) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3130) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3131) rtnl_register(RTNL_FAMILY_IPMR, RTM_GETROUTE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3132) ipmr_rtm_getroute, ipmr_rtm_dumproute, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3133) rtnl_register(RTNL_FAMILY_IPMR, RTM_NEWROUTE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3134) ipmr_rtm_route, NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3135) rtnl_register(RTNL_FAMILY_IPMR, RTM_DELROUTE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3136) ipmr_rtm_route, NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3138) rtnl_register(RTNL_FAMILY_IPMR, RTM_GETLINK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3139) NULL, ipmr_rtm_dumplink, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3140) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3142) #ifdef CONFIG_IP_PIMSM_V2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3143) add_proto_fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3144) unregister_netdevice_notifier(&ip_mr_notifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3145) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3146) reg_notif_fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3147) unregister_pernet_subsys(&ipmr_net_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3148) reg_pernet_fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3149) kmem_cache_destroy(mrt_cachep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3150) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3151) }