^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) * Linux INET6 implementation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * FIB front-end.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Authors:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Pedro Roque <roque@di.fc.ul.pt>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) /* Changes:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * YOSHIFUJI Hideaki @USAGI
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * reworked default router selection.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * - respect outgoing interface
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * - select from (probably) reachable routers (i.e.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * routers in REACHABLE, STALE, DELAY or PROBE states).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) * - always select the same router if it is (probably)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) * reachable. otherwise, round-robin the list.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) * Ville Nuorvala
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * Fixed routing subtrees.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #define pr_fmt(fmt) "IPv6: " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <linux/capability.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include <linux/export.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #include <linux/times.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #include <linux/socket.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #include <linux/sockios.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #include <linux/net.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #include <linux/route.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) #include <linux/netdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) #include <linux/in6.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) #include <linux/mroute6.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) #include <linux/if_arp.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/nsproxy.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) #include <linux/jhash.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) #include <linux/siphash.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/snmp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) #include <net/ipv6.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) #include <net/ip6_fib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) #include <net/ip6_route.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) #include <net/ndisc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) #include <net/addrconf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) #include <net/tcp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) #include <linux/rtnetlink.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) #include <net/dst.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) #include <net/dst_metadata.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) #include <net/xfrm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) #include <net/netevent.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) #include <net/netlink.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) #include <net/rtnh.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) #include <net/lwtunnel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) #include <net/ip_tunnels.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) #include <net/l3mdev.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) #include <net/ip.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) #include <linux/btf_ids.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) #ifdef CONFIG_SYSCTL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) #include <linux/sysctl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) static int ip6_rt_type_to_error(u8 fib6_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) #define CREATE_TRACE_POINTS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) #include <trace/events/fib6.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) EXPORT_TRACEPOINT_SYMBOL_GPL(fib6_table_lookup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) #undef CREATE_TRACE_POINTS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) enum rt6_nud_state {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) RT6_NUD_FAIL_HARD = -3,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) RT6_NUD_FAIL_PROBE = -2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) RT6_NUD_FAIL_DO_RR = -1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) RT6_NUD_SUCCEED = 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) static unsigned int ip6_default_advmss(const struct dst_entry *dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) static unsigned int ip6_mtu(const struct dst_entry *dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) static struct dst_entry *ip6_negative_advice(struct dst_entry *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) static void ip6_dst_destroy(struct dst_entry *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) static void ip6_dst_ifdown(struct dst_entry *,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) struct net_device *dev, int how);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) static int ip6_dst_gc(struct dst_ops *ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) static int ip6_pkt_discard(struct sk_buff *skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) static int ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) static int ip6_pkt_prohibit(struct sk_buff *skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) static int ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) static void ip6_link_failure(struct sk_buff *skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) struct sk_buff *skb, u32 mtu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) bool confirm_neigh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) struct sk_buff *skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) static int rt6_score_route(const struct fib6_nh *nh, u32 fib6_flags, int oif,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) int strict);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) static size_t rt6_nlmsg_size(struct fib6_info *f6i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) static int rt6_fill_node(struct net *net, struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) struct fib6_info *rt, struct dst_entry *dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) struct in6_addr *dest, struct in6_addr *src,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) int iif, int type, u32 portid, u32 seq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) unsigned int flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) static struct rt6_info *rt6_find_cached_rt(const struct fib6_result *res,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) const struct in6_addr *daddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) const struct in6_addr *saddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) #ifdef CONFIG_IPV6_ROUTE_INFO
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) static struct fib6_info *rt6_add_route_info(struct net *net,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) const struct in6_addr *prefix, int prefixlen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) const struct in6_addr *gwaddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) struct net_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) unsigned int pref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) static struct fib6_info *rt6_get_route_info(struct net *net,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) const struct in6_addr *prefix, int prefixlen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) const struct in6_addr *gwaddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) struct net_device *dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) struct uncached_list {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) spinlock_t lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) struct list_head head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) static DEFINE_PER_CPU_ALIGNED(struct uncached_list, rt6_uncached_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) void rt6_uncached_list_add(struct rt6_info *rt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) struct uncached_list *ul = raw_cpu_ptr(&rt6_uncached_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) rt->rt6i_uncached_list = ul;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) spin_lock_bh(&ul->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) list_add_tail(&rt->rt6i_uncached, &ul->head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) spin_unlock_bh(&ul->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) void rt6_uncached_list_del(struct rt6_info *rt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) if (!list_empty(&rt->rt6i_uncached)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) struct uncached_list *ul = rt->rt6i_uncached_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) struct net *net = dev_net(rt->dst.dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) spin_lock_bh(&ul->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) list_del(&rt->rt6i_uncached);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) atomic_dec(&net->ipv6.rt6_stats->fib_rt_uncache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) spin_unlock_bh(&ul->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) static void rt6_uncached_list_flush_dev(struct net *net, struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) struct net_device *loopback_dev = net->loopback_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) int cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) if (dev == loopback_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) for_each_possible_cpu(cpu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) struct rt6_info *rt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) spin_lock_bh(&ul->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) list_for_each_entry(rt, &ul->head, rt6i_uncached) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) struct inet6_dev *rt_idev = rt->rt6i_idev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) struct net_device *rt_dev = rt->dst.dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) if (rt_idev->dev == dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) rt->rt6i_idev = in6_dev_get(loopback_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) in6_dev_put(rt_idev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) if (rt_dev == dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) rt->dst.dev = blackhole_netdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) dev_hold(rt->dst.dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) dev_put(rt_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) spin_unlock_bh(&ul->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) static inline const void *choose_neigh_daddr(const struct in6_addr *p,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) const void *daddr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) if (!ipv6_addr_any(p))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) return (const void *) p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) else if (skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) return &ipv6_hdr(skb)->daddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) return daddr;
^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) struct neighbour *ip6_neigh_lookup(const struct in6_addr *gw,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) struct net_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) const void *daddr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) struct neighbour *n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) daddr = choose_neigh_daddr(gw, skb, daddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) n = __ipv6_neigh_lookup(dev, daddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) if (n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) return n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) n = neigh_create(&nd_tbl, daddr, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) return IS_ERR(n) ? NULL : n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) static struct neighbour *ip6_dst_neigh_lookup(const struct dst_entry *dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) const void *daddr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) const struct rt6_info *rt = container_of(dst, struct rt6_info, dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) return ip6_neigh_lookup(rt6_nexthop(rt, &in6addr_any),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) dst->dev, skb, daddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) static void ip6_confirm_neigh(const struct dst_entry *dst, const void *daddr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) struct net_device *dev = dst->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) struct rt6_info *rt = (struct rt6_info *)dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) daddr = choose_neigh_daddr(rt6_nexthop(rt, &in6addr_any), NULL, daddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) if (!daddr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) if (dev->flags & (IFF_NOARP | IFF_LOOPBACK))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) if (ipv6_addr_is_multicast((const struct in6_addr *)daddr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) __ipv6_confirm_neigh(dev, daddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) static struct dst_ops ip6_dst_ops_template = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) .family = AF_INET6,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) .gc = ip6_dst_gc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) .gc_thresh = 1024,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) .check = ip6_dst_check,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) .default_advmss = ip6_default_advmss,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) .mtu = ip6_mtu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) .cow_metrics = dst_cow_metrics_generic,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) .destroy = ip6_dst_destroy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) .ifdown = ip6_dst_ifdown,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) .negative_advice = ip6_negative_advice,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) .link_failure = ip6_link_failure,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) .update_pmtu = ip6_rt_update_pmtu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) .redirect = rt6_do_redirect,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) .local_out = __ip6_local_out,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) .neigh_lookup = ip6_dst_neigh_lookup,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) .confirm_neigh = ip6_confirm_neigh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) static struct dst_ops ip6_dst_blackhole_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) .family = AF_INET6,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) .default_advmss = ip6_default_advmss,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) .neigh_lookup = ip6_dst_neigh_lookup,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) .check = ip6_dst_check,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) .destroy = ip6_dst_destroy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) .cow_metrics = dst_cow_metrics_generic,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) .update_pmtu = dst_blackhole_update_pmtu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) .redirect = dst_blackhole_redirect,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) .mtu = dst_blackhole_mtu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) static const u32 ip6_template_metrics[RTAX_MAX] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) [RTAX_HOPLIMIT - 1] = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) static const struct fib6_info fib6_null_entry_template = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) .fib6_flags = (RTF_REJECT | RTF_NONEXTHOP),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) .fib6_protocol = RTPROT_KERNEL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) .fib6_metric = ~(u32)0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) .fib6_ref = REFCOUNT_INIT(1),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) .fib6_type = RTN_UNREACHABLE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) .fib6_metrics = (struct dst_metrics *)&dst_default_metrics,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) static const struct rt6_info ip6_null_entry_template = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) .dst = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) .__refcnt = ATOMIC_INIT(1),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) .__use = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) .obsolete = DST_OBSOLETE_FORCE_CHK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) .error = -ENETUNREACH,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) .input = ip6_pkt_discard,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) .output = ip6_pkt_discard_out,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) #ifdef CONFIG_IPV6_MULTIPLE_TABLES
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) static const struct rt6_info ip6_prohibit_entry_template = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) .dst = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) .__refcnt = ATOMIC_INIT(1),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) .__use = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) .obsolete = DST_OBSOLETE_FORCE_CHK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) .error = -EACCES,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) .input = ip6_pkt_prohibit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) .output = ip6_pkt_prohibit_out,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
^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 const struct rt6_info ip6_blk_hole_entry_template = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) .dst = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) .__refcnt = ATOMIC_INIT(1),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) .__use = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) .obsolete = DST_OBSOLETE_FORCE_CHK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) .error = -EINVAL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) .input = dst_discard,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) .output = dst_discard_out,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
^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) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) static void rt6_info_init(struct rt6_info *rt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) struct dst_entry *dst = &rt->dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) memset(dst + 1, 0, sizeof(*rt) - sizeof(*dst));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) INIT_LIST_HEAD(&rt->rt6i_uncached);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) /* allocate dst with ip6_dst_ops */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) struct rt6_info *ip6_dst_alloc(struct net *net, struct net_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) struct rt6_info *rt = dst_alloc(&net->ipv6.ip6_dst_ops, dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 1, DST_OBSOLETE_FORCE_CHK, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) if (rt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) rt6_info_init(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) atomic_inc(&net->ipv6.rt6_stats->fib_rt_alloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) return rt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) EXPORT_SYMBOL(ip6_dst_alloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) static void ip6_dst_destroy(struct dst_entry *dst)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) struct rt6_info *rt = (struct rt6_info *)dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) struct fib6_info *from;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) struct inet6_dev *idev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) ip_dst_metrics_put(dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) rt6_uncached_list_del(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) idev = rt->rt6i_idev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) if (idev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) rt->rt6i_idev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) in6_dev_put(idev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) from = xchg((__force struct fib6_info **)&rt->from, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) fib6_info_release(from);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) int how)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) struct rt6_info *rt = (struct rt6_info *)dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) struct inet6_dev *idev = rt->rt6i_idev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) struct net_device *loopback_dev =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) dev_net(dev)->loopback_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) if (idev && idev->dev != loopback_dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) struct inet6_dev *loopback_idev = in6_dev_get(loopback_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) if (loopback_idev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) rt->rt6i_idev = loopback_idev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) in6_dev_put(idev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) static bool __rt6_check_expired(const struct rt6_info *rt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) if (rt->rt6i_flags & RTF_EXPIRES)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) return time_after(jiffies, rt->dst.expires);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) static bool rt6_check_expired(const struct rt6_info *rt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) struct fib6_info *from;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) from = rcu_dereference(rt->from);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) if (rt->rt6i_flags & RTF_EXPIRES) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) if (time_after(jiffies, rt->dst.expires))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) } else if (from) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) return rt->dst.obsolete != DST_OBSOLETE_FORCE_CHK ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) fib6_check_expired(from);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) void fib6_select_path(const struct net *net, struct fib6_result *res,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) struct flowi6 *fl6, int oif, bool have_oif_match,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) const struct sk_buff *skb, int strict)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) struct fib6_info *sibling, *next_sibling;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) struct fib6_info *match = res->f6i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) if (!match->nh && (!match->fib6_nsiblings || have_oif_match))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) if (match->nh && have_oif_match && res->nh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) /* We might have already computed the hash for ICMPv6 errors. In such
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) * case it will always be non-zero. Otherwise now is the time to do it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) if (!fl6->mp_hash &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) (!match->nh || nexthop_is_multipath(match->nh)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) fl6->mp_hash = rt6_multipath_hash(net, fl6, skb, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) if (unlikely(match->nh)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) nexthop_path_fib6_result(res, fl6->mp_hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) if (fl6->mp_hash <= atomic_read(&match->fib6_nh->fib_nh_upper_bound))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) list_for_each_entry_safe(sibling, next_sibling, &match->fib6_siblings,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) fib6_siblings) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) const struct fib6_nh *nh = sibling->fib6_nh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) int nh_upper_bound;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) nh_upper_bound = atomic_read(&nh->fib_nh_upper_bound);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) if (fl6->mp_hash > nh_upper_bound)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) if (rt6_score_route(nh, sibling->fib6_flags, oif, strict) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) match = sibling;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) res->f6i = match;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) res->nh = match->fib6_nh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) * Route lookup. rcu_read_lock() should be held.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) static bool __rt6_device_match(struct net *net, const struct fib6_nh *nh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) const struct in6_addr *saddr, int oif, int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) const struct net_device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) if (nh->fib_nh_flags & RTNH_F_DEAD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) dev = nh->fib_nh_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) if (oif) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) if (dev->ifindex == oif)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) if (ipv6_chk_addr(net, saddr, dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) flags & RT6_LOOKUP_F_IFACE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) struct fib6_nh_dm_arg {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) struct net *net;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) const struct in6_addr *saddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) int oif;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) int flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) struct fib6_nh *nh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) static int __rt6_nh_dev_match(struct fib6_nh *nh, void *_arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) struct fib6_nh_dm_arg *arg = _arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) arg->nh = nh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) return __rt6_device_match(arg->net, nh, arg->saddr, arg->oif,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) arg->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) /* returns fib6_nh from nexthop or NULL */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) static struct fib6_nh *rt6_nh_dev_match(struct net *net, struct nexthop *nh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) struct fib6_result *res,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) const struct in6_addr *saddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) int oif, int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) struct fib6_nh_dm_arg arg = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) .net = net,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) .saddr = saddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) .oif = oif,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) .flags = flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) if (nexthop_is_blackhole(nh))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) if (nexthop_for_each_fib6_nh(nh, __rt6_nh_dev_match, &arg))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) return arg.nh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) static void rt6_device_match(struct net *net, struct fib6_result *res,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) const struct in6_addr *saddr, int oif, int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) struct fib6_info *f6i = res->f6i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) struct fib6_info *spf6i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) struct fib6_nh *nh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) if (!oif && ipv6_addr_any(saddr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) if (unlikely(f6i->nh)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) nh = nexthop_fib6_nh(f6i->nh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) if (nexthop_is_blackhole(f6i->nh))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) goto out_blackhole;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) nh = f6i->fib6_nh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) if (!(nh->fib_nh_flags & RTNH_F_DEAD))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) for (spf6i = f6i; spf6i; spf6i = rcu_dereference(spf6i->fib6_next)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) bool matched = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) if (unlikely(spf6i->nh)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) nh = rt6_nh_dev_match(net, spf6i->nh, res, saddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) oif, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) if (nh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) matched = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) nh = spf6i->fib6_nh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) if (__rt6_device_match(net, nh, saddr, oif, flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) matched = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) if (matched) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) res->f6i = spf6i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) if (oif && flags & RT6_LOOKUP_F_IFACE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) res->f6i = net->ipv6.fib6_null_entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) nh = res->f6i->fib6_nh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) if (unlikely(f6i->nh)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) nh = nexthop_fib6_nh(f6i->nh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) if (nexthop_is_blackhole(f6i->nh))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) goto out_blackhole;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) nh = f6i->fib6_nh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) if (nh->fib_nh_flags & RTNH_F_DEAD) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) res->f6i = net->ipv6.fib6_null_entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) nh = res->f6i->fib6_nh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) res->nh = nh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) res->fib6_type = res->f6i->fib6_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) res->fib6_flags = res->f6i->fib6_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) out_blackhole:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) res->fib6_flags |= RTF_REJECT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) res->fib6_type = RTN_BLACKHOLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) res->nh = nh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) #ifdef CONFIG_IPV6_ROUTER_PREF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) struct __rt6_probe_work {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) struct work_struct work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) struct in6_addr target;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) struct net_device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) static void rt6_probe_deferred(struct work_struct *w)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) struct in6_addr mcaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) struct __rt6_probe_work *work =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) container_of(w, struct __rt6_probe_work, work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) addrconf_addr_solict_mult(&work->target, &mcaddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) ndisc_send_ns(work->dev, &work->target, &mcaddr, NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) dev_put(work->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) kfree(work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) static void rt6_probe(struct fib6_nh *fib6_nh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) struct __rt6_probe_work *work = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) const struct in6_addr *nh_gw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) unsigned long last_probe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) struct neighbour *neigh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) struct net_device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) struct inet6_dev *idev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) * Okay, this does not seem to be appropriate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) * for now, however, we need to check if it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) * is really so; aka Router Reachability Probing.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) * Router Reachability Probe MUST be rate-limited
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) * to no more than one per minute.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) if (!fib6_nh->fib_nh_gw_family)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) nh_gw = &fib6_nh->fib_nh_gw6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) dev = fib6_nh->fib_nh_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) rcu_read_lock_bh();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) last_probe = READ_ONCE(fib6_nh->last_probe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) idev = __in6_dev_get(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) neigh = __ipv6_neigh_lookup_noref(dev, nh_gw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) if (neigh) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) if (neigh->nud_state & NUD_VALID)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) write_lock(&neigh->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) if (!(neigh->nud_state & NUD_VALID) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) time_after(jiffies,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) neigh->updated + idev->cnf.rtr_probe_interval)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) work = kmalloc(sizeof(*work), GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) if (work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) __neigh_set_probe_once(neigh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) write_unlock(&neigh->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) } else if (time_after(jiffies, last_probe +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) idev->cnf.rtr_probe_interval)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) work = kmalloc(sizeof(*work), GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) if (!work || cmpxchg(&fib6_nh->last_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) last_probe, jiffies) != last_probe) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) kfree(work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) INIT_WORK(&work->work, rt6_probe_deferred);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) work->target = *nh_gw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) dev_hold(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) work->dev = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) schedule_work(&work->work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) rcu_read_unlock_bh();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) static inline void rt6_probe(struct fib6_nh *fib6_nh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) * Default Router Selection (RFC 2461 6.3.6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) static enum rt6_nud_state rt6_check_neigh(const struct fib6_nh *fib6_nh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) enum rt6_nud_state ret = RT6_NUD_FAIL_HARD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) struct neighbour *neigh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) rcu_read_lock_bh();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) neigh = __ipv6_neigh_lookup_noref(fib6_nh->fib_nh_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) &fib6_nh->fib_nh_gw6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) if (neigh) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) read_lock(&neigh->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) if (neigh->nud_state & NUD_VALID)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) ret = RT6_NUD_SUCCEED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) #ifdef CONFIG_IPV6_ROUTER_PREF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) else if (!(neigh->nud_state & NUD_FAILED))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) ret = RT6_NUD_SUCCEED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) ret = RT6_NUD_FAIL_PROBE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) read_unlock(&neigh->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) ret = IS_ENABLED(CONFIG_IPV6_ROUTER_PREF) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) RT6_NUD_SUCCEED : RT6_NUD_FAIL_DO_RR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) rcu_read_unlock_bh();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) static int rt6_score_route(const struct fib6_nh *nh, u32 fib6_flags, int oif,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) int strict)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) int m = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) if (!oif || nh->fib_nh_dev->ifindex == oif)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) m = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) if (!m && (strict & RT6_LOOKUP_F_IFACE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) return RT6_NUD_FAIL_HARD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) #ifdef CONFIG_IPV6_ROUTER_PREF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(fib6_flags)) << 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) if ((strict & RT6_LOOKUP_F_REACHABLE) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) !(fib6_flags & RTF_NONEXTHOP) && nh->fib_nh_gw_family) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) int n = rt6_check_neigh(nh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) if (n < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) return n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) return m;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) static bool find_match(struct fib6_nh *nh, u32 fib6_flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) int oif, int strict, int *mpri, bool *do_rr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) bool match_do_rr = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) bool rc = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) int m;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) if (nh->fib_nh_flags & RTNH_F_DEAD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) if (ip6_ignore_linkdown(nh->fib_nh_dev) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) nh->fib_nh_flags & RTNH_F_LINKDOWN &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) !(strict & RT6_LOOKUP_F_IGNORE_LINKSTATE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) m = rt6_score_route(nh, fib6_flags, oif, strict);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) if (m == RT6_NUD_FAIL_DO_RR) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) match_do_rr = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) m = 0; /* lowest valid score */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) } else if (m == RT6_NUD_FAIL_HARD) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) if (strict & RT6_LOOKUP_F_REACHABLE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) rt6_probe(nh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) /* note that m can be RT6_NUD_FAIL_PROBE at this point */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) if (m > *mpri) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) *do_rr = match_do_rr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) *mpri = m;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) rc = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) struct fib6_nh_frl_arg {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) u32 flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) int oif;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) int strict;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) int *mpri;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) bool *do_rr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) struct fib6_nh *nh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) static int rt6_nh_find_match(struct fib6_nh *nh, void *_arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) struct fib6_nh_frl_arg *arg = _arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) arg->nh = nh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) return find_match(nh, arg->flags, arg->oif, arg->strict,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) arg->mpri, arg->do_rr);
^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) static void __find_rr_leaf(struct fib6_info *f6i_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) struct fib6_info *nomatch, u32 metric,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) struct fib6_result *res, struct fib6_info **cont,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) int oif, int strict, bool *do_rr, int *mpri)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) struct fib6_info *f6i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) for (f6i = f6i_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) f6i && f6i != nomatch;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) f6i = rcu_dereference(f6i->fib6_next)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) bool matched = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) struct fib6_nh *nh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) if (cont && f6i->fib6_metric != metric) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) *cont = f6i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) if (fib6_check_expired(f6i))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) if (unlikely(f6i->nh)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) struct fib6_nh_frl_arg arg = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) .flags = f6i->fib6_flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) .oif = oif,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) .strict = strict,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) .mpri = mpri,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) .do_rr = do_rr
^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) if (nexthop_is_blackhole(f6i->nh)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) res->fib6_flags = RTF_REJECT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) res->fib6_type = RTN_BLACKHOLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) res->f6i = f6i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) res->nh = nexthop_fib6_nh(f6i->nh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) if (nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_find_match,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) &arg)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) matched = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) nh = arg.nh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) nh = f6i->fib6_nh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) if (find_match(nh, f6i->fib6_flags, oif, strict,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) mpri, do_rr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) matched = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) if (matched) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) res->f6i = f6i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) res->nh = nh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) res->fib6_flags = f6i->fib6_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) res->fib6_type = f6i->fib6_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) static void find_rr_leaf(struct fib6_node *fn, struct fib6_info *leaf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) struct fib6_info *rr_head, int oif, int strict,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) bool *do_rr, struct fib6_result *res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) u32 metric = rr_head->fib6_metric;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) struct fib6_info *cont = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) int mpri = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) __find_rr_leaf(rr_head, NULL, metric, res, &cont,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) oif, strict, do_rr, &mpri);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) __find_rr_leaf(leaf, rr_head, metric, res, &cont,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) oif, strict, do_rr, &mpri);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) if (res->f6i || !cont)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) __find_rr_leaf(cont, NULL, metric, res, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) oif, strict, do_rr, &mpri);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) static void rt6_select(struct net *net, struct fib6_node *fn, int oif,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) struct fib6_result *res, int strict)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) struct fib6_info *leaf = rcu_dereference(fn->leaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) struct fib6_info *rt0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) bool do_rr = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) int key_plen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) /* make sure this function or its helpers sets f6i */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) res->f6i = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) if (!leaf || leaf == net->ipv6.fib6_null_entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) rt0 = rcu_dereference(fn->rr_ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873) if (!rt0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) rt0 = leaf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) /* Double check to make sure fn is not an intermediate node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) * and fn->leaf does not points to its child's leaf
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) * (This might happen if all routes under fn are deleted from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) * the tree and fib6_repair_tree() is called on the node.)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) key_plen = rt0->fib6_dst.plen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) #ifdef CONFIG_IPV6_SUBTREES
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) if (rt0->fib6_src.plen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884) key_plen = rt0->fib6_src.plen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) if (fn->fn_bit != key_plen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) find_rr_leaf(fn, leaf, rt0, oif, strict, &do_rr, res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) if (do_rr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) struct fib6_info *next = rcu_dereference(rt0->fib6_next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) /* no entries matched; do round-robin */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) if (!next || next->fib6_metric != rt0->fib6_metric)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895) next = leaf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897) if (next != rt0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898) spin_lock_bh(&leaf->fib6_table->tb6_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) /* make sure next is not being deleted from the tree */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900) if (next->fib6_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901) rcu_assign_pointer(fn->rr_ptr, next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902) spin_unlock_bh(&leaf->fib6_table->tb6_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907) if (!res->f6i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908) res->f6i = net->ipv6.fib6_null_entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909) res->nh = res->f6i->fib6_nh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) res->fib6_flags = res->f6i->fib6_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911) res->fib6_type = res->f6i->fib6_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915) static bool rt6_is_gw_or_nonexthop(const struct fib6_result *res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) return (res->f6i->fib6_flags & RTF_NONEXTHOP) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918) res->nh->fib_nh_gw_family;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921) #ifdef CONFIG_IPV6_ROUTE_INFO
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922) int rt6_route_rcv(struct net_device *dev, u8 *opt, int len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923) const struct in6_addr *gwaddr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925) struct net *net = dev_net(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926) struct route_info *rinfo = (struct route_info *) opt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927) struct in6_addr prefix_buf, *prefix;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928) unsigned int pref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929) unsigned long lifetime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930) struct fib6_info *rt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932) if (len < sizeof(struct route_info)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936) /* Sanity check for prefix_len and length */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937) if (rinfo->length > 3) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939) } else if (rinfo->prefix_len > 128) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941) } else if (rinfo->prefix_len > 64) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942) if (rinfo->length < 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945) } else if (rinfo->prefix_len > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946) if (rinfo->length < 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947) return -EINVAL;
^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)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 951) pref = rinfo->route_pref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 952) if (pref == ICMPV6_ROUTER_PREF_INVALID)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 953) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 954)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 955) lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 956)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 957) if (rinfo->length == 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 958) prefix = (struct in6_addr *)rinfo->prefix;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 959) else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 960) /* this function is safe */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 961) ipv6_addr_prefix(&prefix_buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 962) (struct in6_addr *)rinfo->prefix,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 963) rinfo->prefix_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 964) prefix = &prefix_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 965) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 966)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 967) if (rinfo->prefix_len == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 968) rt = rt6_get_dflt_router(net, gwaddr, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 969) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 970) rt = rt6_get_route_info(net, prefix, rinfo->prefix_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 971) gwaddr, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 972)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 973) if (rt && !lifetime) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 974) ip6_del_rt(net, rt, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 975) rt = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 976) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 977)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 978) if (!rt && lifetime)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 979) rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 980) dev, pref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 981) else if (rt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 982) rt->fib6_flags = RTF_ROUTEINFO |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 983) (rt->fib6_flags & ~RTF_PREF_MASK) | RTF_PREF(pref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 984)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 985) if (rt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 986) if (!addrconf_finite_timeout(lifetime))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 987) fib6_clean_expires(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 988) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 989) fib6_set_expires(rt, jiffies + HZ * lifetime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 990)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 991) fib6_info_release(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 992) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 993) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 994) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 995) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 996)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 997) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 998) * Misc support functions
^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) /* called with rcu_lock held */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) static struct net_device *ip6_rt_get_dev_rcu(const struct fib6_result *res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) struct net_device *dev = res->nh->fib_nh_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) if (res->fib6_flags & (RTF_LOCAL | RTF_ANYCAST)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) /* for copies of local routes, dst->dev needs to be the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) * device if it is a master device, the master device if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) * device is enslaved, and the loopback as the default
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) if (netif_is_l3_slave(dev) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) !rt6_need_strict(&res->f6i->fib6_dst.addr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) dev = l3mdev_master_dev_rcu(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) else if (!netif_is_l3_master(dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) dev = dev_net(dev)->loopback_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) /* last case is netif_is_l3_master(dev) is true in which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) * case we want dev returned to be dev
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) return dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) static const int fib6_prop[RTN_MAX + 1] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) [RTN_UNSPEC] = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) [RTN_UNICAST] = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) [RTN_LOCAL] = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) [RTN_BROADCAST] = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) [RTN_ANYCAST] = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) [RTN_MULTICAST] = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) [RTN_BLACKHOLE] = -EINVAL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) [RTN_UNREACHABLE] = -EHOSTUNREACH,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) [RTN_PROHIBIT] = -EACCES,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) [RTN_THROW] = -EAGAIN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) [RTN_NAT] = -EINVAL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) [RTN_XRESOLVE] = -EINVAL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) static int ip6_rt_type_to_error(u8 fib6_type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) return fib6_prop[fib6_type];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) static unsigned short fib6_info_dst_flags(struct fib6_info *rt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) unsigned short flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) if (rt->dst_nocount)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) flags |= DST_NOCOUNT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) if (rt->dst_nopolicy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) flags |= DST_NOPOLICY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) return flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) static void ip6_rt_init_dst_reject(struct rt6_info *rt, u8 fib6_type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) rt->dst.error = ip6_rt_type_to_error(fib6_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) switch (fib6_type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) case RTN_BLACKHOLE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) rt->dst.output = dst_discard_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) rt->dst.input = dst_discard;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) case RTN_PROHIBIT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) rt->dst.output = ip6_pkt_prohibit_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) rt->dst.input = ip6_pkt_prohibit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) case RTN_THROW:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) case RTN_UNREACHABLE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) rt->dst.output = ip6_pkt_discard_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) rt->dst.input = ip6_pkt_discard;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) static void ip6_rt_init_dst(struct rt6_info *rt, const struct fib6_result *res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) struct fib6_info *f6i = res->f6i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) if (res->fib6_flags & RTF_REJECT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) ip6_rt_init_dst_reject(rt, res->fib6_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) rt->dst.error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) rt->dst.output = ip6_output;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) if (res->fib6_type == RTN_LOCAL || res->fib6_type == RTN_ANYCAST) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) rt->dst.input = ip6_input;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) } else if (ipv6_addr_type(&f6i->fib6_dst.addr) & IPV6_ADDR_MULTICAST) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) rt->dst.input = ip6_mc_input;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) rt->dst.input = ip6_forward;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) if (res->nh->fib_nh_lws) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) rt->dst.lwtstate = lwtstate_get(res->nh->fib_nh_lws);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) lwtunnel_set_redirect(&rt->dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) rt->dst.lastuse = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) /* Caller must already hold reference to @from */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) static void rt6_set_from(struct rt6_info *rt, struct fib6_info *from)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) rt->rt6i_flags &= ~RTF_EXPIRES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) rcu_assign_pointer(rt->from, from);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) ip_dst_init_metrics(&rt->dst, from->fib6_metrics);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) /* Caller must already hold reference to f6i in result */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) static void ip6_rt_copy_init(struct rt6_info *rt, const struct fib6_result *res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) const struct fib6_nh *nh = res->nh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) const struct net_device *dev = nh->fib_nh_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) struct fib6_info *f6i = res->f6i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) ip6_rt_init_dst(rt, res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) rt->rt6i_dst = f6i->fib6_dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) rt->rt6i_idev = dev ? in6_dev_get(dev) : NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) rt->rt6i_flags = res->fib6_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) if (nh->fib_nh_gw_family) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) rt->rt6i_gateway = nh->fib_nh_gw6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) rt->rt6i_flags |= RTF_GATEWAY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) rt6_set_from(rt, f6i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) #ifdef CONFIG_IPV6_SUBTREES
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) rt->rt6i_src = f6i->fib6_src;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) static struct fib6_node* fib6_backtrack(struct fib6_node *fn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) struct in6_addr *saddr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) struct fib6_node *pn, *sn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) if (fn->fn_flags & RTN_TL_ROOT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) pn = rcu_dereference(fn->parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) sn = FIB6_SUBTREE(pn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) if (sn && sn != fn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) fn = fib6_node_lookup(sn, NULL, saddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) fn = pn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) if (fn->fn_flags & RTN_RTINFO)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) return fn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) static bool ip6_hold_safe(struct net *net, struct rt6_info **prt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) struct rt6_info *rt = *prt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) if (dst_hold_safe(&rt->dst))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) if (net) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) rt = net->ipv6.ip6_null_entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) dst_hold(&rt->dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) rt = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) *prt = rt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) /* called with rcu_lock held */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) static struct rt6_info *ip6_create_rt_rcu(const struct fib6_result *res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) struct net_device *dev = res->nh->fib_nh_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) struct fib6_info *f6i = res->f6i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) unsigned short flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) struct rt6_info *nrt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) if (!fib6_info_hold_safe(f6i))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) goto fallback;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) flags = fib6_info_dst_flags(f6i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) nrt = ip6_dst_alloc(dev_net(dev), dev, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) if (!nrt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) fib6_info_release(f6i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) goto fallback;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) ip6_rt_copy_init(nrt, res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) return nrt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) fallback:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) nrt = dev_net(dev)->ipv6.ip6_null_entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) dst_hold(&nrt->dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) return nrt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) INDIRECT_CALLABLE_SCOPE struct rt6_info *ip6_pol_route_lookup(struct net *net,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) struct fib6_table *table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) struct flowi6 *fl6,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) const struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) struct fib6_result res = {};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) struct fib6_node *fn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) struct rt6_info *rt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) flags &= ~RT6_LOOKUP_F_IFACE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) restart:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) res.f6i = rcu_dereference(fn->leaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) if (!res.f6i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) res.f6i = net->ipv6.fib6_null_entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) rt6_device_match(net, &res, &fl6->saddr, fl6->flowi6_oif,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) if (res.f6i == net->ipv6.fib6_null_entry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) fn = fib6_backtrack(fn, &fl6->saddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) if (fn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) goto restart;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) rt = net->ipv6.ip6_null_entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) dst_hold(&rt->dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) } else if (res.fib6_flags & RTF_REJECT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) goto do_create;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) fib6_select_path(net, &res, fl6, fl6->flowi6_oif,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) fl6->flowi6_oif != 0, skb, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) /* Search through exception table */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) rt = rt6_find_cached_rt(&res, &fl6->daddr, &fl6->saddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) if (rt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) if (ip6_hold_safe(net, &rt))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) dst_use_noref(&rt->dst, jiffies);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) do_create:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) rt = ip6_create_rt_rcu(&res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) trace_fib6_table_lookup(net, &res, table, fl6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) return rt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) struct dst_entry *ip6_route_lookup(struct net *net, struct flowi6 *fl6,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) const struct sk_buff *skb, int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) return fib6_rule_lookup(net, fl6, skb, flags, ip6_pol_route_lookup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) EXPORT_SYMBOL_GPL(ip6_route_lookup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) const struct in6_addr *saddr, int oif,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) const struct sk_buff *skb, int strict)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) struct flowi6 fl6 = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) .flowi6_oif = oif,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) .daddr = *daddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) struct dst_entry *dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) int flags = strict ? RT6_LOOKUP_F_IFACE : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) if (saddr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) memcpy(&fl6.saddr, saddr, sizeof(*saddr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) flags |= RT6_LOOKUP_F_HAS_SADDR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) dst = fib6_rule_lookup(net, &fl6, skb, flags, ip6_pol_route_lookup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) if (dst->error == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) return (struct rt6_info *) dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) dst_release(dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) EXPORT_SYMBOL(rt6_lookup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) /* ip6_ins_rt is called with FREE table->tb6_lock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) * It takes new route entry, the addition fails by any reason the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) * route is released.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) * Caller must hold dst before calling it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) static int __ip6_ins_rt(struct fib6_info *rt, struct nl_info *info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) struct fib6_table *table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) table = rt->fib6_table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) spin_lock_bh(&table->tb6_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) err = fib6_add(&table->tb6_root, rt, info, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) spin_unlock_bh(&table->tb6_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) return err;
^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) int ip6_ins_rt(struct net *net, struct fib6_info *rt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) struct nl_info info = { .nl_net = net, };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) return __ip6_ins_rt(rt, &info, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) static struct rt6_info *ip6_rt_cache_alloc(const struct fib6_result *res,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) const struct in6_addr *daddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) const struct in6_addr *saddr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) struct fib6_info *f6i = res->f6i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) struct net_device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) struct rt6_info *rt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) * Clone the route.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) if (!fib6_info_hold_safe(f6i))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) dev = ip6_rt_get_dev_rcu(res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) rt = ip6_dst_alloc(dev_net(dev), dev, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) if (!rt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) fib6_info_release(f6i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) ip6_rt_copy_init(rt, res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) rt->rt6i_flags |= RTF_CACHE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) rt->rt6i_dst.addr = *daddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) rt->rt6i_dst.plen = 128;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) if (!rt6_is_gw_or_nonexthop(res)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) if (f6i->fib6_dst.plen != 128 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) ipv6_addr_equal(&f6i->fib6_dst.addr, daddr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) rt->rt6i_flags |= RTF_ANYCAST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) #ifdef CONFIG_IPV6_SUBTREES
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) if (rt->rt6i_src.plen && saddr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) rt->rt6i_src.addr = *saddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) rt->rt6i_src.plen = 128;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) return rt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) static struct rt6_info *ip6_rt_pcpu_alloc(const struct fib6_result *res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) struct fib6_info *f6i = res->f6i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) unsigned short flags = fib6_info_dst_flags(f6i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) struct net_device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) struct rt6_info *pcpu_rt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) if (!fib6_info_hold_safe(f6i))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) dev = ip6_rt_get_dev_rcu(res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) pcpu_rt = ip6_dst_alloc(dev_net(dev), dev, flags | DST_NOCOUNT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) if (!pcpu_rt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) fib6_info_release(f6i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) ip6_rt_copy_init(pcpu_rt, res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) pcpu_rt->rt6i_flags |= RTF_PCPU;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) if (f6i->nh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) pcpu_rt->sernum = rt_genid_ipv6(dev_net(dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) return pcpu_rt;
^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) static bool rt6_is_valid(const struct rt6_info *rt6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) return rt6->sernum == rt_genid_ipv6(dev_net(rt6->dst.dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) /* It should be called with rcu_read_lock() acquired */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) static struct rt6_info *rt6_get_pcpu_route(const struct fib6_result *res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) struct rt6_info *pcpu_rt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) pcpu_rt = this_cpu_read(*res->nh->rt6i_pcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) if (pcpu_rt && pcpu_rt->sernum && !rt6_is_valid(pcpu_rt)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) struct rt6_info *prev, **p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) p = this_cpu_ptr(res->nh->rt6i_pcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) prev = xchg(p, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) if (prev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) dst_dev_put(&prev->dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) dst_release(&prev->dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) pcpu_rt = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) return pcpu_rt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) static struct rt6_info *rt6_make_pcpu_route(struct net *net,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) const struct fib6_result *res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) struct rt6_info *pcpu_rt, *prev, **p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) pcpu_rt = ip6_rt_pcpu_alloc(res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) if (!pcpu_rt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) p = this_cpu_ptr(res->nh->rt6i_pcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) prev = cmpxchg(p, NULL, pcpu_rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) BUG_ON(prev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) if (res->f6i->fib6_destroying) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) struct fib6_info *from;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) from = xchg((__force struct fib6_info **)&pcpu_rt->from, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) fib6_info_release(from);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) return pcpu_rt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) /* exception hash table implementation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) static DEFINE_SPINLOCK(rt6_exception_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437) /* Remove rt6_ex from hash table and free the memory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) * Caller must hold rt6_exception_lock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) static void rt6_remove_exception(struct rt6_exception_bucket *bucket,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) struct rt6_exception *rt6_ex)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) struct fib6_info *from;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) struct net *net;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) if (!bucket || !rt6_ex)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) net = dev_net(rt6_ex->rt6i->dst.dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) net->ipv6.rt6_stats->fib_rt_cache--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452) /* purge completely the exception to allow releasing the held resources:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) * some [sk] cache may keep the dst around for unlimited time
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) from = xchg((__force struct fib6_info **)&rt6_ex->rt6i->from, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456) fib6_info_release(from);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) dst_dev_put(&rt6_ex->rt6i->dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) hlist_del_rcu(&rt6_ex->hlist);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) dst_release(&rt6_ex->rt6i->dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) kfree_rcu(rt6_ex, rcu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462) WARN_ON_ONCE(!bucket->depth);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) bucket->depth--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466) /* Remove oldest rt6_ex in bucket and free the memory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467) * Caller must hold rt6_exception_lock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469) static void rt6_exception_remove_oldest(struct rt6_exception_bucket *bucket)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471) struct rt6_exception *rt6_ex, *oldest = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473) if (!bucket)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476) hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477) if (!oldest || time_before(rt6_ex->stamp, oldest->stamp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478) oldest = rt6_ex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480) rt6_remove_exception(bucket, oldest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483) static u32 rt6_exception_hash(const struct in6_addr *dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484) const struct in6_addr *src)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486) static siphash_key_t rt6_exception_key __read_mostly;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487) struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) struct in6_addr dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489) struct in6_addr src;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) } __aligned(SIPHASH_ALIGNMENT) combined = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491) .dst = *dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493) u64 val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495) net_get_random_once(&rt6_exception_key, sizeof(rt6_exception_key));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497) #ifdef CONFIG_IPV6_SUBTREES
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498) if (src)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499) combined.src = *src;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501) val = siphash(&combined, sizeof(combined), &rt6_exception_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503) return hash_64(val, FIB6_EXCEPTION_BUCKET_SIZE_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506) /* Helper function to find the cached rt in the hash table
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507) * and update bucket pointer to point to the bucket for this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508) * (daddr, saddr) pair
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509) * Caller must hold rt6_exception_lock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511) static struct rt6_exception *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512) __rt6_find_exception_spinlock(struct rt6_exception_bucket **bucket,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) const struct in6_addr *daddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514) const struct in6_addr *saddr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) struct rt6_exception *rt6_ex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517) u32 hval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519) if (!(*bucket) || !daddr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522) hval = rt6_exception_hash(daddr, saddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523) *bucket += hval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525) hlist_for_each_entry(rt6_ex, &(*bucket)->chain, hlist) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526) struct rt6_info *rt6 = rt6_ex->rt6i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527) bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529) #ifdef CONFIG_IPV6_SUBTREES
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) if (matched && saddr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531) matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) if (matched)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534) return rt6_ex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539) /* Helper function to find the cached rt in the hash table
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540) * and update bucket pointer to point to the bucket for this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541) * (daddr, saddr) pair
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542) * Caller must hold rcu_read_lock()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544) static struct rt6_exception *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545) __rt6_find_exception_rcu(struct rt6_exception_bucket **bucket,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) const struct in6_addr *daddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547) const struct in6_addr *saddr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549) struct rt6_exception *rt6_ex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550) u32 hval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552) WARN_ON_ONCE(!rcu_read_lock_held());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554) if (!(*bucket) || !daddr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557) hval = rt6_exception_hash(daddr, saddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558) *bucket += hval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560) hlist_for_each_entry_rcu(rt6_ex, &(*bucket)->chain, hlist) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561) struct rt6_info *rt6 = rt6_ex->rt6i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562) bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564) #ifdef CONFIG_IPV6_SUBTREES
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565) if (matched && saddr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566) matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568) if (matched)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569) return rt6_ex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571) return NULL;
^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) static unsigned int fib6_mtu(const struct fib6_result *res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576) const struct fib6_nh *nh = res->nh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577) unsigned int mtu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579) if (res->f6i->fib6_pmtu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580) mtu = res->f6i->fib6_pmtu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582) struct net_device *dev = nh->fib_nh_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583) struct inet6_dev *idev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586) idev = __in6_dev_get(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587) mtu = idev->cnf.mtu6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591) mtu = min_t(unsigned int, mtu, IP6_MAX_MTU);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593) return mtu - lwtunnel_headroom(nh->fib_nh_lws, mtu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596) #define FIB6_EXCEPTION_BUCKET_FLUSHED 0x1UL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598) /* used when the flushed bit is not relevant, only access to the bucket
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599) * (ie., all bucket users except rt6_insert_exception);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1600) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1601) * called under rcu lock; sometimes called with rt6_exception_lock held
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1602) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1603) static
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1604) struct rt6_exception_bucket *fib6_nh_get_excptn_bucket(const struct fib6_nh *nh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1605) spinlock_t *lock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1606) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1607) struct rt6_exception_bucket *bucket;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1608)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1609) if (lock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1610) bucket = rcu_dereference_protected(nh->rt6i_exception_bucket,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1611) lockdep_is_held(lock));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1612) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1613) bucket = rcu_dereference(nh->rt6i_exception_bucket);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1614)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1615) /* remove bucket flushed bit if set */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1616) if (bucket) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1617) unsigned long p = (unsigned long)bucket;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1618)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1619) p &= ~FIB6_EXCEPTION_BUCKET_FLUSHED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1620) bucket = (struct rt6_exception_bucket *)p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1621) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1622)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1623) return bucket;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1624) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1625)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1626) static bool fib6_nh_excptn_bucket_flushed(struct rt6_exception_bucket *bucket)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1627) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1628) unsigned long p = (unsigned long)bucket;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1629)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1630) return !!(p & FIB6_EXCEPTION_BUCKET_FLUSHED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1631) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1632)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1633) /* called with rt6_exception_lock held */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1634) static void fib6_nh_excptn_bucket_set_flushed(struct fib6_nh *nh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1635) spinlock_t *lock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1636) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1637) struct rt6_exception_bucket *bucket;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1638) unsigned long p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1639)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1640) bucket = rcu_dereference_protected(nh->rt6i_exception_bucket,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1641) lockdep_is_held(lock));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1642)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1643) p = (unsigned long)bucket;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1644) p |= FIB6_EXCEPTION_BUCKET_FLUSHED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1645) bucket = (struct rt6_exception_bucket *)p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1646) rcu_assign_pointer(nh->rt6i_exception_bucket, bucket);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1647) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1648)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1649) static int rt6_insert_exception(struct rt6_info *nrt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1650) const struct fib6_result *res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1651) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1652) struct net *net = dev_net(nrt->dst.dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1653) struct rt6_exception_bucket *bucket;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1654) struct fib6_info *f6i = res->f6i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1655) struct in6_addr *src_key = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1656) struct rt6_exception *rt6_ex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1657) struct fib6_nh *nh = res->nh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1658) int max_depth;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1659) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1660)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1661) spin_lock_bh(&rt6_exception_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1662)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1663) bucket = rcu_dereference_protected(nh->rt6i_exception_bucket,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1664) lockdep_is_held(&rt6_exception_lock));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1665) if (!bucket) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1666) bucket = kcalloc(FIB6_EXCEPTION_BUCKET_SIZE, sizeof(*bucket),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1667) GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1668) if (!bucket) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1669) err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1670) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1671) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1672) rcu_assign_pointer(nh->rt6i_exception_bucket, bucket);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1673) } else if (fib6_nh_excptn_bucket_flushed(bucket)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1674) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1675) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1676) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1677)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1678) #ifdef CONFIG_IPV6_SUBTREES
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1679) /* fib6_src.plen != 0 indicates f6i is in subtree
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1680) * and exception table is indexed by a hash of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1681) * both fib6_dst and fib6_src.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1682) * Otherwise, the exception table is indexed by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1683) * a hash of only fib6_dst.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1684) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1685) if (f6i->fib6_src.plen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1686) src_key = &nrt->rt6i_src.addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1687) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1688) /* rt6_mtu_change() might lower mtu on f6i.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1689) * Only insert this exception route if its mtu
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1690) * is less than f6i's mtu value.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1691) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1692) if (dst_metric_raw(&nrt->dst, RTAX_MTU) >= fib6_mtu(res)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1693) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1694) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1695) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1696)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1697) rt6_ex = __rt6_find_exception_spinlock(&bucket, &nrt->rt6i_dst.addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1698) src_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1699) if (rt6_ex)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1700) rt6_remove_exception(bucket, rt6_ex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1701)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1702) rt6_ex = kzalloc(sizeof(*rt6_ex), GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1703) if (!rt6_ex) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1704) err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1705) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1706) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1707) rt6_ex->rt6i = nrt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1708) rt6_ex->stamp = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1709) hlist_add_head_rcu(&rt6_ex->hlist, &bucket->chain);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1710) bucket->depth++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1711) net->ipv6.rt6_stats->fib_rt_cache++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1712)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1713) /* Randomize max depth to avoid some side channels attacks. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1714) max_depth = FIB6_MAX_DEPTH + prandom_u32_max(FIB6_MAX_DEPTH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1715) while (bucket->depth > max_depth)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1716) rt6_exception_remove_oldest(bucket);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1717)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1718) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1719) spin_unlock_bh(&rt6_exception_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1720)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1721) /* Update fn->fn_sernum to invalidate all cached dst */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1722) if (!err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1723) spin_lock_bh(&f6i->fib6_table->tb6_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1724) fib6_update_sernum(net, f6i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1725) spin_unlock_bh(&f6i->fib6_table->tb6_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1726) fib6_force_start_gc(net);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1727) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1728)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1729) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1730) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1731)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1732) static void fib6_nh_flush_exceptions(struct fib6_nh *nh, struct fib6_info *from)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1733) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1734) struct rt6_exception_bucket *bucket;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1735) struct rt6_exception *rt6_ex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1736) struct hlist_node *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1737) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1738)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1739) spin_lock_bh(&rt6_exception_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1740)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1741) bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1742) if (!bucket)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1743) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1744)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1745) /* Prevent rt6_insert_exception() to recreate the bucket list */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1746) if (!from)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1747) fib6_nh_excptn_bucket_set_flushed(nh, &rt6_exception_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1748)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1749) for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1750) hlist_for_each_entry_safe(rt6_ex, tmp, &bucket->chain, hlist) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1751) if (!from ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1752) rcu_access_pointer(rt6_ex->rt6i->from) == from)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1753) rt6_remove_exception(bucket, rt6_ex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1754) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1755) WARN_ON_ONCE(!from && bucket->depth);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1756) bucket++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1757) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1758) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1759) spin_unlock_bh(&rt6_exception_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1760) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1761)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1762) static int rt6_nh_flush_exceptions(struct fib6_nh *nh, void *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1763) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1764) struct fib6_info *f6i = arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1765)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1766) fib6_nh_flush_exceptions(nh, f6i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1767)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1768) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1769) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1770)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1771) void rt6_flush_exceptions(struct fib6_info *f6i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1772) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1773) if (f6i->nh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1774) nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_flush_exceptions,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1775) f6i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1776) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1777) fib6_nh_flush_exceptions(f6i->fib6_nh, f6i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1778) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1779)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1780) /* Find cached rt in the hash table inside passed in rt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1781) * Caller has to hold rcu_read_lock()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1782) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1783) static struct rt6_info *rt6_find_cached_rt(const struct fib6_result *res,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1784) const struct in6_addr *daddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1785) const struct in6_addr *saddr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1786) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1787) const struct in6_addr *src_key = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1788) struct rt6_exception_bucket *bucket;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1789) struct rt6_exception *rt6_ex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1790) struct rt6_info *ret = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1791)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1792) #ifdef CONFIG_IPV6_SUBTREES
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1793) /* fib6i_src.plen != 0 indicates f6i is in subtree
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1794) * and exception table is indexed by a hash of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1795) * both fib6_dst and fib6_src.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1796) * However, the src addr used to create the hash
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1797) * might not be exactly the passed in saddr which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1798) * is a /128 addr from the flow.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1799) * So we need to use f6i->fib6_src to redo lookup
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1800) * if the passed in saddr does not find anything.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1801) * (See the logic in ip6_rt_cache_alloc() on how
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1802) * rt->rt6i_src is updated.)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1803) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1804) if (res->f6i->fib6_src.plen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1805) src_key = saddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1806) find_ex:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1807) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1808) bucket = fib6_nh_get_excptn_bucket(res->nh, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1809) rt6_ex = __rt6_find_exception_rcu(&bucket, daddr, src_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1810)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1811) if (rt6_ex && !rt6_check_expired(rt6_ex->rt6i))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1812) ret = rt6_ex->rt6i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1813)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1814) #ifdef CONFIG_IPV6_SUBTREES
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1815) /* Use fib6_src as src_key and redo lookup */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1816) if (!ret && src_key && src_key != &res->f6i->fib6_src.addr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1817) src_key = &res->f6i->fib6_src.addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1818) goto find_ex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1819) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1820) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1821)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1822) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1823) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1824)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1825) /* Remove the passed in cached rt from the hash table that contains it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1826) static int fib6_nh_remove_exception(const struct fib6_nh *nh, int plen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1827) const struct rt6_info *rt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1828) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1829) const struct in6_addr *src_key = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1830) struct rt6_exception_bucket *bucket;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1831) struct rt6_exception *rt6_ex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1832) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1833)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1834) if (!rcu_access_pointer(nh->rt6i_exception_bucket))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1835) return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1836)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1837) spin_lock_bh(&rt6_exception_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1838) bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1839)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1840) #ifdef CONFIG_IPV6_SUBTREES
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1841) /* rt6i_src.plen != 0 indicates 'from' is in subtree
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1842) * and exception table is indexed by a hash of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1843) * both rt6i_dst and rt6i_src.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1844) * Otherwise, the exception table is indexed by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1845) * a hash of only rt6i_dst.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1846) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1847) if (plen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1848) src_key = &rt->rt6i_src.addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1849) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1850) rt6_ex = __rt6_find_exception_spinlock(&bucket,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1851) &rt->rt6i_dst.addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1852) src_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1853) if (rt6_ex) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1854) rt6_remove_exception(bucket, rt6_ex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1855) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1856) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1857) err = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1858) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1859)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1860) spin_unlock_bh(&rt6_exception_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1861) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1862) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1863)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1864) struct fib6_nh_excptn_arg {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1865) struct rt6_info *rt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1866) int plen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1867) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1868)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1869) static int rt6_nh_remove_exception_rt(struct fib6_nh *nh, void *_arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1870) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1871) struct fib6_nh_excptn_arg *arg = _arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1872) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1873)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1874) err = fib6_nh_remove_exception(nh, arg->plen, arg->rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1875) if (err == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1876) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1877)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1878) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1879) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1880)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1881) static int rt6_remove_exception_rt(struct rt6_info *rt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1882) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1883) struct fib6_info *from;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1884)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1885) from = rcu_dereference(rt->from);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1886) if (!from || !(rt->rt6i_flags & RTF_CACHE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1887) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1888)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1889) if (from->nh) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1890) struct fib6_nh_excptn_arg arg = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1891) .rt = rt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1892) .plen = from->fib6_src.plen
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1893) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1894) int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1895)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1896) /* rc = 1 means an entry was found */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1897) rc = nexthop_for_each_fib6_nh(from->nh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1898) rt6_nh_remove_exception_rt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1899) &arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1900) return rc ? 0 : -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1901) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1902)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1903) return fib6_nh_remove_exception(from->fib6_nh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1904) from->fib6_src.plen, rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1905) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1906)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1907) /* Find rt6_ex which contains the passed in rt cache and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1908) * refresh its stamp
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1909) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1910) static void fib6_nh_update_exception(const struct fib6_nh *nh, int plen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1911) const struct rt6_info *rt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1912) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1913) const struct in6_addr *src_key = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1914) struct rt6_exception_bucket *bucket;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1915) struct rt6_exception *rt6_ex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1916)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1917) bucket = fib6_nh_get_excptn_bucket(nh, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1918) #ifdef CONFIG_IPV6_SUBTREES
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1919) /* rt6i_src.plen != 0 indicates 'from' is in subtree
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1920) * and exception table is indexed by a hash of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1921) * both rt6i_dst and rt6i_src.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1922) * Otherwise, the exception table is indexed by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1923) * a hash of only rt6i_dst.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1924) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1925) if (plen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1926) src_key = &rt->rt6i_src.addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1927) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1928) rt6_ex = __rt6_find_exception_rcu(&bucket, &rt->rt6i_dst.addr, src_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1929) if (rt6_ex)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1930) rt6_ex->stamp = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1931) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1932)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1933) struct fib6_nh_match_arg {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1934) const struct net_device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1935) const struct in6_addr *gw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1936) struct fib6_nh *match;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1937) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1938)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1939) /* determine if fib6_nh has given device and gateway */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1940) static int fib6_nh_find_match(struct fib6_nh *nh, void *_arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1941) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1942) struct fib6_nh_match_arg *arg = _arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1943)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1944) if (arg->dev != nh->fib_nh_dev ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1945) (arg->gw && !nh->fib_nh_gw_family) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1946) (!arg->gw && nh->fib_nh_gw_family) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1947) (arg->gw && !ipv6_addr_equal(arg->gw, &nh->fib_nh_gw6)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1948) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1949)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1950) arg->match = nh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1951)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1952) /* found a match, break the loop */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1953) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1954) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1955)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1956) static void rt6_update_exception_stamp_rt(struct rt6_info *rt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1957) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1958) struct fib6_info *from;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1959) struct fib6_nh *fib6_nh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1960)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1961) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1962)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1963) from = rcu_dereference(rt->from);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1964) if (!from || !(rt->rt6i_flags & RTF_CACHE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1965) goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1966)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1967) if (from->nh) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1968) struct fib6_nh_match_arg arg = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1969) .dev = rt->dst.dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1970) .gw = &rt->rt6i_gateway,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1971) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1972)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1973) nexthop_for_each_fib6_nh(from->nh, fib6_nh_find_match, &arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1974)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1975) if (!arg.match)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1976) goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1977) fib6_nh = arg.match;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1978) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1979) fib6_nh = from->fib6_nh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1980) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1981) fib6_nh_update_exception(fib6_nh, from->fib6_src.plen, rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1982) unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1983) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1984) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1985)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1986) static bool rt6_mtu_change_route_allowed(struct inet6_dev *idev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1987) struct rt6_info *rt, int mtu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1988) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1989) /* If the new MTU is lower than the route PMTU, this new MTU will be the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1990) * lowest MTU in the path: always allow updating the route PMTU to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1991) * reflect PMTU decreases.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1992) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1993) * If the new MTU is higher, and the route PMTU is equal to the local
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1994) * MTU, this means the old MTU is the lowest in the path, so allow
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1995) * updating it: if other nodes now have lower MTUs, PMTU discovery will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1996) * handle this.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1997) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1998)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1999) if (dst_mtu(&rt->dst) >= mtu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2000) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2001)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2002) if (dst_mtu(&rt->dst) == idev->cnf.mtu6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2003) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2004)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2005) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2006) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2007)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2008) static void rt6_exceptions_update_pmtu(struct inet6_dev *idev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2009) const struct fib6_nh *nh, int mtu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2010) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2011) struct rt6_exception_bucket *bucket;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2012) struct rt6_exception *rt6_ex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2013) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2014)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2015) bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2016) if (!bucket)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2017) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2018)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2019) for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2020) hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2021) struct rt6_info *entry = rt6_ex->rt6i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2022)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2023) /* For RTF_CACHE with rt6i_pmtu == 0 (i.e. a redirected
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2024) * route), the metrics of its rt->from have already
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2025) * been updated.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2026) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2027) if (dst_metric_raw(&entry->dst, RTAX_MTU) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2028) rt6_mtu_change_route_allowed(idev, entry, mtu))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2029) dst_metric_set(&entry->dst, RTAX_MTU, mtu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2030) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2031) bucket++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2032) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2033) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2034)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2035) #define RTF_CACHE_GATEWAY (RTF_GATEWAY | RTF_CACHE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2036)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2037) static void fib6_nh_exceptions_clean_tohost(const struct fib6_nh *nh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2038) const struct in6_addr *gateway)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2039) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2040) struct rt6_exception_bucket *bucket;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2041) struct rt6_exception *rt6_ex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2042) struct hlist_node *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2043) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2044)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2045) if (!rcu_access_pointer(nh->rt6i_exception_bucket))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2046) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2047)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2048) spin_lock_bh(&rt6_exception_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2049) bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2050) if (bucket) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2051) for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2052) hlist_for_each_entry_safe(rt6_ex, tmp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2053) &bucket->chain, hlist) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2054) struct rt6_info *entry = rt6_ex->rt6i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2055)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2056) if ((entry->rt6i_flags & RTF_CACHE_GATEWAY) ==
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2057) RTF_CACHE_GATEWAY &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2058) ipv6_addr_equal(gateway,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2059) &entry->rt6i_gateway)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2060) rt6_remove_exception(bucket, rt6_ex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2061) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2062) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2063) bucket++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2064) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2065) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2066)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2067) spin_unlock_bh(&rt6_exception_lock);
^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) static void rt6_age_examine_exception(struct rt6_exception_bucket *bucket,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2071) struct rt6_exception *rt6_ex,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2072) struct fib6_gc_args *gc_args,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2073) unsigned long now)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2074) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2075) struct rt6_info *rt = rt6_ex->rt6i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2076)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2077) /* we are pruning and obsoleting aged-out and non gateway exceptions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2078) * even if others have still references to them, so that on next
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2079) * dst_check() such references can be dropped.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2080) * EXPIRES exceptions - e.g. pmtu-generated ones are pruned when
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2081) * expired, independently from their aging, as per RFC 8201 section 4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2082) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2083) if (!(rt->rt6i_flags & RTF_EXPIRES)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2084) if (time_after_eq(now, rt->dst.lastuse + gc_args->timeout)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2085) RT6_TRACE("aging clone %p\n", rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2086) rt6_remove_exception(bucket, rt6_ex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2087) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2088) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2089) } else if (time_after(jiffies, rt->dst.expires)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2090) RT6_TRACE("purging expired route %p\n", rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2091) rt6_remove_exception(bucket, rt6_ex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2092) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2093) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2094)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2095) if (rt->rt6i_flags & RTF_GATEWAY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2096) struct neighbour *neigh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2097) __u8 neigh_flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2098)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2099) neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2100) if (neigh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2101) neigh_flags = neigh->flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2103) if (!(neigh_flags & NTF_ROUTER)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2104) RT6_TRACE("purging route %p via non-router but gateway\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2105) rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2106) rt6_remove_exception(bucket, rt6_ex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2107) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2108) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2109) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2111) gc_args->more++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2112) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2114) static void fib6_nh_age_exceptions(const struct fib6_nh *nh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2115) struct fib6_gc_args *gc_args,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2116) unsigned long now)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2117) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2118) struct rt6_exception_bucket *bucket;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2119) struct rt6_exception *rt6_ex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2120) struct hlist_node *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2121) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2123) if (!rcu_access_pointer(nh->rt6i_exception_bucket))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2124) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2126) rcu_read_lock_bh();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2127) spin_lock(&rt6_exception_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2128) bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2129) if (bucket) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2130) for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2131) hlist_for_each_entry_safe(rt6_ex, tmp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2132) &bucket->chain, hlist) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2133) rt6_age_examine_exception(bucket, rt6_ex,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2134) gc_args, now);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2135) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2136) bucket++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2137) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2138) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2139) spin_unlock(&rt6_exception_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2140) rcu_read_unlock_bh();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2141) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2143) struct fib6_nh_age_excptn_arg {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2144) struct fib6_gc_args *gc_args;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2145) unsigned long now;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2146) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2148) static int rt6_nh_age_exceptions(struct fib6_nh *nh, void *_arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2149) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2150) struct fib6_nh_age_excptn_arg *arg = _arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2152) fib6_nh_age_exceptions(nh, arg->gc_args, arg->now);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2153) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2154) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2156) void rt6_age_exceptions(struct fib6_info *f6i,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2157) struct fib6_gc_args *gc_args,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2158) unsigned long now)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2159) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2160) if (f6i->nh) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2161) struct fib6_nh_age_excptn_arg arg = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2162) .gc_args = gc_args,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2163) .now = now
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2164) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2166) nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_age_exceptions,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2167) &arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2168) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2169) fib6_nh_age_exceptions(f6i->fib6_nh, gc_args, now);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2170) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2171) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2172)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2173) /* must be called with rcu lock held */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2174) int fib6_table_lookup(struct net *net, struct fib6_table *table, int oif,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2175) struct flowi6 *fl6, struct fib6_result *res, int strict)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2176) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2177) struct fib6_node *fn, *saved_fn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2178)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2179) fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2180) saved_fn = fn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2181)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2182) if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2183) oif = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2184)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2185) redo_rt6_select:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2186) rt6_select(net, fn, oif, res, strict);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2187) if (res->f6i == net->ipv6.fib6_null_entry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2188) fn = fib6_backtrack(fn, &fl6->saddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2189) if (fn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2190) goto redo_rt6_select;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2191) else if (strict & RT6_LOOKUP_F_REACHABLE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2192) /* also consider unreachable route */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2193) strict &= ~RT6_LOOKUP_F_REACHABLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2194) fn = saved_fn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2195) goto redo_rt6_select;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2196) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2197) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2198)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2199) trace_fib6_table_lookup(net, res, table, fl6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2201) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2202) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2203)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2204) struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2205) int oif, struct flowi6 *fl6,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2206) const struct sk_buff *skb, int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2207) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2208) struct fib6_result res = {};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2209) struct rt6_info *rt = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2210) int strict = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2211)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2212) WARN_ON_ONCE((flags & RT6_LOOKUP_F_DST_NOREF) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2213) !rcu_read_lock_held());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2214)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2215) strict |= flags & RT6_LOOKUP_F_IFACE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2216) strict |= flags & RT6_LOOKUP_F_IGNORE_LINKSTATE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2217) if (net->ipv6.devconf_all->forwarding == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2218) strict |= RT6_LOOKUP_F_REACHABLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2219)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2220) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2221)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2222) fib6_table_lookup(net, table, oif, fl6, &res, strict);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2223) if (res.f6i == net->ipv6.fib6_null_entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2224) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2225)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2226) fib6_select_path(net, &res, fl6, oif, false, skb, strict);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2227)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2228) /*Search through exception table */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2229) rt = rt6_find_cached_rt(&res, &fl6->daddr, &fl6->saddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2230) if (rt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2231) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2232) } else if (unlikely((fl6->flowi6_flags & FLOWI_FLAG_KNOWN_NH) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2233) !res.nh->fib_nh_gw_family)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2234) /* Create a RTF_CACHE clone which will not be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2235) * owned by the fib6 tree. It is for the special case where
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2236) * the daddr in the skb during the neighbor look-up is different
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2237) * from the fl6->daddr used to look-up route here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2238) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2239) rt = ip6_rt_cache_alloc(&res, &fl6->daddr, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2240)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2241) if (rt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2242) /* 1 refcnt is taken during ip6_rt_cache_alloc().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2243) * As rt6_uncached_list_add() does not consume refcnt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2244) * this refcnt is always returned to the caller even
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2245) * if caller sets RT6_LOOKUP_F_DST_NOREF flag.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2246) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2247) rt6_uncached_list_add(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2248) atomic_inc(&net->ipv6.rt6_stats->fib_rt_uncache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2249) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2250)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2251) return rt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2252) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2253) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2254) /* Get a percpu copy */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2255) local_bh_disable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2256) rt = rt6_get_pcpu_route(&res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2257)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2258) if (!rt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2259) rt = rt6_make_pcpu_route(net, &res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2260)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2261) local_bh_enable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2262) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2263) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2264) if (!rt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2265) rt = net->ipv6.ip6_null_entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2266) if (!(flags & RT6_LOOKUP_F_DST_NOREF))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2267) ip6_hold_safe(net, &rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2268) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2269)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2270) return rt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2271) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2272) EXPORT_SYMBOL_GPL(ip6_pol_route);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2273)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2274) INDIRECT_CALLABLE_SCOPE struct rt6_info *ip6_pol_route_input(struct net *net,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2275) struct fib6_table *table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2276) struct flowi6 *fl6,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2277) const struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2278) int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2279) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2280) return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, skb, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2281) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2282)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2283) struct dst_entry *ip6_route_input_lookup(struct net *net,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2284) struct net_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2285) struct flowi6 *fl6,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2286) const struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2287) int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2288) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2289) if (rt6_need_strict(&fl6->daddr) && dev->type != ARPHRD_PIMREG)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2290) flags |= RT6_LOOKUP_F_IFACE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2291)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2292) return fib6_rule_lookup(net, fl6, skb, flags, ip6_pol_route_input);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2293) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2294) EXPORT_SYMBOL_GPL(ip6_route_input_lookup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2295)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2296) static void ip6_multipath_l3_keys(const struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2297) struct flow_keys *keys,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2298) struct flow_keys *flkeys)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2299) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2300) const struct ipv6hdr *outer_iph = ipv6_hdr(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2301) const struct ipv6hdr *key_iph = outer_iph;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2302) struct flow_keys *_flkeys = flkeys;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2303) const struct ipv6hdr *inner_iph;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2304) const struct icmp6hdr *icmph;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2305) struct ipv6hdr _inner_iph;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2306) struct icmp6hdr _icmph;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2307)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2308) if (likely(outer_iph->nexthdr != IPPROTO_ICMPV6))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2309) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2310)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2311) icmph = skb_header_pointer(skb, skb_transport_offset(skb),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2312) sizeof(_icmph), &_icmph);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2313) if (!icmph)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2314) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2315)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2316) if (!icmpv6_is_err(icmph->icmp6_type))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2317) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2318)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2319) inner_iph = skb_header_pointer(skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2320) skb_transport_offset(skb) + sizeof(*icmph),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2321) sizeof(_inner_iph), &_inner_iph);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2322) if (!inner_iph)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2323) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2324)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2325) key_iph = inner_iph;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2326) _flkeys = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2327) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2328) if (_flkeys) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2329) keys->addrs.v6addrs.src = _flkeys->addrs.v6addrs.src;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2330) keys->addrs.v6addrs.dst = _flkeys->addrs.v6addrs.dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2331) keys->tags.flow_label = _flkeys->tags.flow_label;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2332) keys->basic.ip_proto = _flkeys->basic.ip_proto;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2333) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2334) keys->addrs.v6addrs.src = key_iph->saddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2335) keys->addrs.v6addrs.dst = key_iph->daddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2336) keys->tags.flow_label = ip6_flowlabel(key_iph);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2337) keys->basic.ip_proto = key_iph->nexthdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2338) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2339) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2340)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2341) /* if skb is set it will be used and fl6 can be NULL */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2342) u32 rt6_multipath_hash(const struct net *net, const struct flowi6 *fl6,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2343) const struct sk_buff *skb, struct flow_keys *flkeys)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2344) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2345) struct flow_keys hash_keys;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2346) u32 mhash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2347)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2348) switch (ip6_multipath_hash_policy(net)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2349) case 0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2350) memset(&hash_keys, 0, sizeof(hash_keys));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2351) hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2352) if (skb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2353) ip6_multipath_l3_keys(skb, &hash_keys, flkeys);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2354) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2355) hash_keys.addrs.v6addrs.src = fl6->saddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2356) hash_keys.addrs.v6addrs.dst = fl6->daddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2357) hash_keys.tags.flow_label = (__force u32)flowi6_get_flowlabel(fl6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2358) hash_keys.basic.ip_proto = fl6->flowi6_proto;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2359) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2360) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2361) case 1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2362) if (skb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2363) unsigned int flag = FLOW_DISSECTOR_F_STOP_AT_ENCAP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2364) struct flow_keys keys;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2365)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2366) /* short-circuit if we already have L4 hash present */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2367) if (skb->l4_hash)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2368) return skb_get_hash_raw(skb) >> 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2369)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2370) memset(&hash_keys, 0, sizeof(hash_keys));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2371)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2372) if (!flkeys) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2373) skb_flow_dissect_flow_keys(skb, &keys, flag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2374) flkeys = &keys;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2375) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2376) hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2377) hash_keys.addrs.v6addrs.src = flkeys->addrs.v6addrs.src;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2378) hash_keys.addrs.v6addrs.dst = flkeys->addrs.v6addrs.dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2379) hash_keys.ports.src = flkeys->ports.src;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2380) hash_keys.ports.dst = flkeys->ports.dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2381) hash_keys.basic.ip_proto = flkeys->basic.ip_proto;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2382) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2383) memset(&hash_keys, 0, sizeof(hash_keys));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2384) hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2385) hash_keys.addrs.v6addrs.src = fl6->saddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2386) hash_keys.addrs.v6addrs.dst = fl6->daddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2387) hash_keys.ports.src = fl6->fl6_sport;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2388) hash_keys.ports.dst = fl6->fl6_dport;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2389) hash_keys.basic.ip_proto = fl6->flowi6_proto;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2390) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2391) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2392) case 2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2393) memset(&hash_keys, 0, sizeof(hash_keys));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2394) hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2395) if (skb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2396) struct flow_keys keys;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2397)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2398) if (!flkeys) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2399) skb_flow_dissect_flow_keys(skb, &keys, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2400) flkeys = &keys;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2401) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2402)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2403) /* Inner can be v4 or v6 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2404) if (flkeys->control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2405) hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2406) hash_keys.addrs.v4addrs.src = flkeys->addrs.v4addrs.src;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2407) hash_keys.addrs.v4addrs.dst = flkeys->addrs.v4addrs.dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2408) } else if (flkeys->control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2409) hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2410) hash_keys.addrs.v6addrs.src = flkeys->addrs.v6addrs.src;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2411) hash_keys.addrs.v6addrs.dst = flkeys->addrs.v6addrs.dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2412) hash_keys.tags.flow_label = flkeys->tags.flow_label;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2413) hash_keys.basic.ip_proto = flkeys->basic.ip_proto;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2414) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2415) /* Same as case 0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2416) hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2417) ip6_multipath_l3_keys(skb, &hash_keys, flkeys);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2418) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2419) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2420) /* Same as case 0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2421) hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2422) hash_keys.addrs.v6addrs.src = fl6->saddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2423) hash_keys.addrs.v6addrs.dst = fl6->daddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2424) hash_keys.tags.flow_label = (__force u32)flowi6_get_flowlabel(fl6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2425) hash_keys.basic.ip_proto = fl6->flowi6_proto;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2426) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2427) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2428) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2429) mhash = flow_hash_from_keys(&hash_keys);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2430)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2431) return mhash >> 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2432) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2433)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2434) /* Called with rcu held */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2435) void ip6_route_input(struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2436) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2437) const struct ipv6hdr *iph = ipv6_hdr(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2438) struct net *net = dev_net(skb->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2439) int flags = RT6_LOOKUP_F_HAS_SADDR | RT6_LOOKUP_F_DST_NOREF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2440) struct ip_tunnel_info *tun_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2441) struct flowi6 fl6 = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2442) .flowi6_iif = skb->dev->ifindex,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2443) .daddr = iph->daddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2444) .saddr = iph->saddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2445) .flowlabel = ip6_flowinfo(iph),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2446) .flowi6_mark = skb->mark,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2447) .flowi6_proto = iph->nexthdr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2448) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2449) struct flow_keys *flkeys = NULL, _flkeys;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2450)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2451) tun_info = skb_tunnel_info(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2452) if (tun_info && !(tun_info->mode & IP_TUNNEL_INFO_TX))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2453) fl6.flowi6_tun_key.tun_id = tun_info->key.tun_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2454)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2455) if (fib6_rules_early_flow_dissect(net, skb, &fl6, &_flkeys))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2456) flkeys = &_flkeys;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2457)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2458) if (unlikely(fl6.flowi6_proto == IPPROTO_ICMPV6))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2459) fl6.mp_hash = rt6_multipath_hash(net, &fl6, skb, flkeys);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2460) skb_dst_drop(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2461) skb_dst_set_noref(skb, ip6_route_input_lookup(net, skb->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2462) &fl6, skb, flags));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2463) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2464)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2465) INDIRECT_CALLABLE_SCOPE struct rt6_info *ip6_pol_route_output(struct net *net,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2466) struct fib6_table *table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2467) struct flowi6 *fl6,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2468) const struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2469) int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2470) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2471) return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, skb, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2472) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2473)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2474) struct dst_entry *ip6_route_output_flags_noref(struct net *net,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2475) const struct sock *sk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2476) struct flowi6 *fl6, int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2477) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2478) bool any_src;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2479)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2480) if (ipv6_addr_type(&fl6->daddr) &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2481) (IPV6_ADDR_MULTICAST | IPV6_ADDR_LINKLOCAL)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2482) struct dst_entry *dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2483)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2484) /* This function does not take refcnt on the dst */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2485) dst = l3mdev_link_scope_lookup(net, fl6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2486) if (dst)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2487) return dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2488) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2489)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2490) fl6->flowi6_iif = LOOPBACK_IFINDEX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2491)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2492) flags |= RT6_LOOKUP_F_DST_NOREF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2493) any_src = ipv6_addr_any(&fl6->saddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2494) if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2495) (fl6->flowi6_oif && any_src))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2496) flags |= RT6_LOOKUP_F_IFACE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2497)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2498) if (!any_src)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2499) flags |= RT6_LOOKUP_F_HAS_SADDR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2500) else if (sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2501) flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2502)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2503) return fib6_rule_lookup(net, fl6, NULL, flags, ip6_pol_route_output);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2504) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2505) EXPORT_SYMBOL_GPL(ip6_route_output_flags_noref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2506)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2507) struct dst_entry *ip6_route_output_flags(struct net *net,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2508) const struct sock *sk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2509) struct flowi6 *fl6,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2510) int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2511) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2512) struct dst_entry *dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2513) struct rt6_info *rt6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2514)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2515) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2516) dst = ip6_route_output_flags_noref(net, sk, fl6, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2517) rt6 = (struct rt6_info *)dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2518) /* For dst cached in uncached_list, refcnt is already taken. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2519) if (list_empty(&rt6->rt6i_uncached) && !dst_hold_safe(dst)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2520) dst = &net->ipv6.ip6_null_entry->dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2521) dst_hold(dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2522) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2523) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2524)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2525) return dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2526) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2527) EXPORT_SYMBOL_GPL(ip6_route_output_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2528)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2529) struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2530) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2531) struct rt6_info *rt, *ort = (struct rt6_info *) dst_orig;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2532) struct net_device *loopback_dev = net->loopback_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2533) struct dst_entry *new = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2534)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2535) rt = dst_alloc(&ip6_dst_blackhole_ops, loopback_dev, 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2536) DST_OBSOLETE_DEAD, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2537) if (rt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2538) rt6_info_init(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2539) atomic_inc(&net->ipv6.rt6_stats->fib_rt_alloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2540)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2541) new = &rt->dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2542) new->__use = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2543) new->input = dst_discard;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2544) new->output = dst_discard_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2545)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2546) dst_copy_metrics(new, &ort->dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2547)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2548) rt->rt6i_idev = in6_dev_get(loopback_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2549) rt->rt6i_gateway = ort->rt6i_gateway;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2550) rt->rt6i_flags = ort->rt6i_flags & ~RTF_PCPU;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2551)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2552) memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2553) #ifdef CONFIG_IPV6_SUBTREES
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2554) memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2555) #endif
^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) dst_release(dst_orig);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2559) return new ? new : ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2560) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2561)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2562) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2563) * Destination cache support functions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2564) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2565)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2566) static bool fib6_check(struct fib6_info *f6i, u32 cookie)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2567) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2568) u32 rt_cookie = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2569)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2570) if (!fib6_get_cookie_safe(f6i, &rt_cookie) || rt_cookie != cookie)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2571) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2572)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2573) if (fib6_check_expired(f6i))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2574) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2575)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2576) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2577) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2578)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2579) static struct dst_entry *rt6_check(struct rt6_info *rt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2580) struct fib6_info *from,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2581) u32 cookie)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2582) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2583) u32 rt_cookie = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2584)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2585) if (!from || !fib6_get_cookie_safe(from, &rt_cookie) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2586) rt_cookie != cookie)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2587) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2588)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2589) if (rt6_check_expired(rt))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2590) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2591)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2592) return &rt->dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2593) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2594)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2595) static struct dst_entry *rt6_dst_from_check(struct rt6_info *rt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2596) struct fib6_info *from,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2597) u32 cookie)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2598) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2599) if (!__rt6_check_expired(rt) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2600) rt->dst.obsolete == DST_OBSOLETE_FORCE_CHK &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2601) fib6_check(from, cookie))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2602) return &rt->dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2603) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2604) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2605) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2606)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2607) static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2608) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2609) struct dst_entry *dst_ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2610) struct fib6_info *from;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2611) struct rt6_info *rt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2612)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2613) rt = container_of(dst, struct rt6_info, dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2614)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2615) if (rt->sernum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2616) return rt6_is_valid(rt) ? dst : NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2617)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2618) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2619)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2620) /* All IPV6 dsts are created with ->obsolete set to the value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2621) * DST_OBSOLETE_FORCE_CHK which forces validation calls down
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2622) * into this function always.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2623) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2624)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2625) from = rcu_dereference(rt->from);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2626)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2627) if (from && (rt->rt6i_flags & RTF_PCPU ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2628) unlikely(!list_empty(&rt->rt6i_uncached))))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2629) dst_ret = rt6_dst_from_check(rt, from, cookie);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2630) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2631) dst_ret = rt6_check(rt, from, cookie);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2632)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2633) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2634)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2635) return dst_ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2636) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2637)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2638) static struct dst_entry *ip6_negative_advice(struct dst_entry *dst)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2639) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2640) struct rt6_info *rt = (struct rt6_info *) dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2641)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2642) if (rt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2643) if (rt->rt6i_flags & RTF_CACHE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2644) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2645) if (rt6_check_expired(rt)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2646) rt6_remove_exception_rt(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2647) dst = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2648) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2649) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2650) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2651) dst_release(dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2652) dst = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2653) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2654) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2655) return dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2656) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2657)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2658) static void ip6_link_failure(struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2659) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2660) struct rt6_info *rt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2661)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2662) icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2663)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2664) rt = (struct rt6_info *) skb_dst(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2665) if (rt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2666) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2667) if (rt->rt6i_flags & RTF_CACHE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2668) rt6_remove_exception_rt(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2669) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2670) struct fib6_info *from;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2671) struct fib6_node *fn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2672)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2673) from = rcu_dereference(rt->from);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2674) if (from) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2675) fn = rcu_dereference(from->fib6_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2676) if (fn && (rt->rt6i_flags & RTF_DEFAULT))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2677) WRITE_ONCE(fn->fn_sernum, -1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2678) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2679) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2680) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2681) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2682) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2683)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2684) static void rt6_update_expires(struct rt6_info *rt0, int timeout)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2685) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2686) if (!(rt0->rt6i_flags & RTF_EXPIRES)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2687) struct fib6_info *from;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2688)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2689) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2690) from = rcu_dereference(rt0->from);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2691) if (from)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2692) rt0->dst.expires = from->expires;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2693) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2694) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2695)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2696) dst_set_expires(&rt0->dst, timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2697) rt0->rt6i_flags |= RTF_EXPIRES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2698) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2699)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2700) static void rt6_do_update_pmtu(struct rt6_info *rt, u32 mtu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2701) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2702) struct net *net = dev_net(rt->dst.dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2703)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2704) dst_metric_set(&rt->dst, RTAX_MTU, mtu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2705) rt->rt6i_flags |= RTF_MODIFIED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2706) rt6_update_expires(rt, net->ipv6.sysctl.ip6_rt_mtu_expires);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2707) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2708)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2709) static bool rt6_cache_allowed_for_pmtu(const struct rt6_info *rt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2710) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2711) return !(rt->rt6i_flags & RTF_CACHE) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2712) (rt->rt6i_flags & RTF_PCPU || rcu_access_pointer(rt->from));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2713) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2714)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2715) static void __ip6_rt_update_pmtu(struct dst_entry *dst, const struct sock *sk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2716) const struct ipv6hdr *iph, u32 mtu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2717) bool confirm_neigh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2718) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2719) const struct in6_addr *daddr, *saddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2720) struct rt6_info *rt6 = (struct rt6_info *)dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2721)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2722) /* Note: do *NOT* check dst_metric_locked(dst, RTAX_MTU)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2723) * IPv6 pmtu discovery isn't optional, so 'mtu lock' cannot disable it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2724) * [see also comment in rt6_mtu_change_route()]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2725) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2726)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2727) if (iph) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2728) daddr = &iph->daddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2729) saddr = &iph->saddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2730) } else if (sk) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2731) daddr = &sk->sk_v6_daddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2732) saddr = &inet6_sk(sk)->saddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2733) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2734) daddr = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2735) saddr = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2736) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2737)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2738) if (confirm_neigh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2739) dst_confirm_neigh(dst, daddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2740)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2741) if (mtu < IPV6_MIN_MTU)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2742) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2743) if (mtu >= dst_mtu(dst))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2744) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2745)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2746) if (!rt6_cache_allowed_for_pmtu(rt6)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2747) rt6_do_update_pmtu(rt6, mtu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2748) /* update rt6_ex->stamp for cache */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2749) if (rt6->rt6i_flags & RTF_CACHE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2750) rt6_update_exception_stamp_rt(rt6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2751) } else if (daddr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2752) struct fib6_result res = {};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2753) struct rt6_info *nrt6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2754)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2755) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2756) res.f6i = rcu_dereference(rt6->from);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2757) if (!res.f6i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2758) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2759)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2760) res.fib6_flags = res.f6i->fib6_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2761) res.fib6_type = res.f6i->fib6_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2762)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2763) if (res.f6i->nh) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2764) struct fib6_nh_match_arg arg = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2765) .dev = dst->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2766) .gw = &rt6->rt6i_gateway,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2767) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2768)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2769) nexthop_for_each_fib6_nh(res.f6i->nh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2770) fib6_nh_find_match, &arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2771)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2772) /* fib6_info uses a nexthop that does not have fib6_nh
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2773) * using the dst->dev + gw. Should be impossible.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2774) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2775) if (!arg.match)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2776) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2777)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2778) res.nh = arg.match;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2779) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2780) res.nh = res.f6i->fib6_nh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2781) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2782)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2783) nrt6 = ip6_rt_cache_alloc(&res, daddr, saddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2784) if (nrt6) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2785) rt6_do_update_pmtu(nrt6, mtu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2786) if (rt6_insert_exception(nrt6, &res))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2787) dst_release_immediate(&nrt6->dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2788) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2789) out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2790) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2791) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2792) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2793)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2794) static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2795) struct sk_buff *skb, u32 mtu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2796) bool confirm_neigh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2797) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2798) __ip6_rt_update_pmtu(dst, sk, skb ? ipv6_hdr(skb) : NULL, mtu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2799) confirm_neigh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2800) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2801)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2802) void ip6_update_pmtu(struct sk_buff *skb, struct net *net, __be32 mtu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2803) int oif, u32 mark, kuid_t uid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2804) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2805) const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2806) struct dst_entry *dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2807) struct flowi6 fl6 = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2808) .flowi6_oif = oif,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2809) .flowi6_mark = mark ? mark : IP6_REPLY_MARK(net, skb->mark),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2810) .daddr = iph->daddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2811) .saddr = iph->saddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2812) .flowlabel = ip6_flowinfo(iph),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2813) .flowi6_uid = uid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2814) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2815)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2816) dst = ip6_route_output(net, NULL, &fl6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2817) if (!dst->error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2818) __ip6_rt_update_pmtu(dst, NULL, iph, ntohl(mtu), true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2819) dst_release(dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2820) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2821) EXPORT_SYMBOL_GPL(ip6_update_pmtu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2822)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2823) void ip6_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, __be32 mtu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2824) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2825) int oif = sk->sk_bound_dev_if;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2826) struct dst_entry *dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2827)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2828) if (!oif && skb->dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2829) oif = l3mdev_master_ifindex(skb->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2830)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2831) ip6_update_pmtu(skb, sock_net(sk), mtu, oif, sk->sk_mark, sk->sk_uid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2832)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2833) dst = __sk_dst_get(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2834) if (!dst || !dst->obsolete ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2835) dst->ops->check(dst, inet6_sk(sk)->dst_cookie))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2836) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2837)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2838) bh_lock_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2839) if (!sock_owned_by_user(sk) && !ipv6_addr_v4mapped(&sk->sk_v6_daddr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2840) ip6_datagram_dst_update(sk, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2841) bh_unlock_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2842) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2843) EXPORT_SYMBOL_GPL(ip6_sk_update_pmtu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2844)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2845) void ip6_sk_dst_store_flow(struct sock *sk, struct dst_entry *dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2846) const struct flowi6 *fl6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2847) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2848) #ifdef CONFIG_IPV6_SUBTREES
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2849) struct ipv6_pinfo *np = inet6_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2850) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2851)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2852) ip6_dst_store(sk, dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2853) ipv6_addr_equal(&fl6->daddr, &sk->sk_v6_daddr) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2854) &sk->sk_v6_daddr : NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2855) #ifdef CONFIG_IPV6_SUBTREES
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2856) ipv6_addr_equal(&fl6->saddr, &np->saddr) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2857) &np->saddr :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2858) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2859) NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2860) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2861)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2862) static bool ip6_redirect_nh_match(const struct fib6_result *res,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2863) struct flowi6 *fl6,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2864) const struct in6_addr *gw,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2865) struct rt6_info **ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2866) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2867) const struct fib6_nh *nh = res->nh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2868)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2869) if (nh->fib_nh_flags & RTNH_F_DEAD || !nh->fib_nh_gw_family ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2870) fl6->flowi6_oif != nh->fib_nh_dev->ifindex)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2871) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2872)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2873) /* rt_cache's gateway might be different from its 'parent'
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2874) * in the case of an ip redirect.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2875) * So we keep searching in the exception table if the gateway
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2876) * is different.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2877) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2878) if (!ipv6_addr_equal(gw, &nh->fib_nh_gw6)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2879) struct rt6_info *rt_cache;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2880)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2881) rt_cache = rt6_find_cached_rt(res, &fl6->daddr, &fl6->saddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2882) if (rt_cache &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2883) ipv6_addr_equal(gw, &rt_cache->rt6i_gateway)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2884) *ret = rt_cache;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2885) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2886) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2887) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2888) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2889) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2890) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2891)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2892) struct fib6_nh_rd_arg {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2893) struct fib6_result *res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2894) struct flowi6 *fl6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2895) const struct in6_addr *gw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2896) struct rt6_info **ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2897) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2898)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2899) static int fib6_nh_redirect_match(struct fib6_nh *nh, void *_arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2900) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2901) struct fib6_nh_rd_arg *arg = _arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2902)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2903) arg->res->nh = nh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2904) return ip6_redirect_nh_match(arg->res, arg->fl6, arg->gw, arg->ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2905) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2906)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2907) /* Handle redirects */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2908) struct ip6rd_flowi {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2909) struct flowi6 fl6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2910) struct in6_addr gateway;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2911) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2912)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2913) INDIRECT_CALLABLE_SCOPE struct rt6_info *__ip6_route_redirect(struct net *net,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2914) struct fib6_table *table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2915) struct flowi6 *fl6,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2916) const struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2917) int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2918) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2919) struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2920) struct rt6_info *ret = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2921) struct fib6_result res = {};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2922) struct fib6_nh_rd_arg arg = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2923) .res = &res,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2924) .fl6 = fl6,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2925) .gw = &rdfl->gateway,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2926) .ret = &ret
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2927) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2928) struct fib6_info *rt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2929) struct fib6_node *fn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2930)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2931) /* l3mdev_update_flow overrides oif if the device is enslaved; in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2932) * this case we must match on the real ingress device, so reset it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2933) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2934) if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2935) fl6->flowi6_oif = skb->dev->ifindex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2936)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2937) /* Get the "current" route for this destination and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2938) * check if the redirect has come from appropriate router.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2939) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2940) * RFC 4861 specifies that redirects should only be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2941) * accepted if they come from the nexthop to the target.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2942) * Due to the way the routes are chosen, this notion
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2943) * is a bit fuzzy and one might need to check all possible
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2944) * routes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2945) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2946)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2947) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2948) fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2949) restart:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2950) for_each_fib6_node_rt_rcu(fn) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2951) res.f6i = rt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2952) if (fib6_check_expired(rt))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2953) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2954) if (rt->fib6_flags & RTF_REJECT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2955) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2956) if (unlikely(rt->nh)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2957) if (nexthop_is_blackhole(rt->nh))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2958) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2959) /* on match, res->nh is filled in and potentially ret */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2960) if (nexthop_for_each_fib6_nh(rt->nh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2961) fib6_nh_redirect_match,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2962) &arg))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2963) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2964) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2965) res.nh = rt->fib6_nh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2966) if (ip6_redirect_nh_match(&res, fl6, &rdfl->gateway,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2967) &ret))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2968) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2969) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2970) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2971)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2972) if (!rt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2973) rt = net->ipv6.fib6_null_entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2974) else if (rt->fib6_flags & RTF_REJECT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2975) ret = net->ipv6.ip6_null_entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2976) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2977) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2978)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2979) if (rt == net->ipv6.fib6_null_entry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2980) fn = fib6_backtrack(fn, &fl6->saddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2981) if (fn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2982) goto restart;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2983) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2984)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2985) res.f6i = rt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2986) res.nh = rt->fib6_nh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2987) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2988) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2989) ip6_hold_safe(net, &ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2990) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2991) res.fib6_flags = res.f6i->fib6_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2992) res.fib6_type = res.f6i->fib6_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2993) ret = ip6_create_rt_rcu(&res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2994) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2995)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2996) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2997)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2998) trace_fib6_table_lookup(net, &res, table, fl6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2999) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3000) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3001)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3002) static struct dst_entry *ip6_route_redirect(struct net *net,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3003) const struct flowi6 *fl6,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3004) const struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3005) const struct in6_addr *gateway)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3006) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3007) int flags = RT6_LOOKUP_F_HAS_SADDR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3008) struct ip6rd_flowi rdfl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3009)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3010) rdfl.fl6 = *fl6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3011) rdfl.gateway = *gateway;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3012)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3013) return fib6_rule_lookup(net, &rdfl.fl6, skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3014) flags, __ip6_route_redirect);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3015) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3016)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3017) void ip6_redirect(struct sk_buff *skb, struct net *net, int oif, u32 mark,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3018) kuid_t uid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3019) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3020) const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3021) struct dst_entry *dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3022) struct flowi6 fl6 = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3023) .flowi6_iif = LOOPBACK_IFINDEX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3024) .flowi6_oif = oif,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3025) .flowi6_mark = mark,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3026) .daddr = iph->daddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3027) .saddr = iph->saddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3028) .flowlabel = ip6_flowinfo(iph),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3029) .flowi6_uid = uid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3030) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3031)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3032) dst = ip6_route_redirect(net, &fl6, skb, &ipv6_hdr(skb)->saddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3033) rt6_do_redirect(dst, NULL, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3034) dst_release(dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3035) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3036) EXPORT_SYMBOL_GPL(ip6_redirect);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3037)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3038) void ip6_redirect_no_header(struct sk_buff *skb, struct net *net, int oif)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3039) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3040) const struct ipv6hdr *iph = ipv6_hdr(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3041) const struct rd_msg *msg = (struct rd_msg *)icmp6_hdr(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3042) struct dst_entry *dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3043) struct flowi6 fl6 = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3044) .flowi6_iif = LOOPBACK_IFINDEX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3045) .flowi6_oif = oif,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3046) .daddr = msg->dest,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3047) .saddr = iph->daddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3048) .flowi6_uid = sock_net_uid(net, NULL),
^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) dst = ip6_route_redirect(net, &fl6, skb, &iph->saddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3052) rt6_do_redirect(dst, NULL, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3053) dst_release(dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3054) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3055)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3056) void ip6_sk_redirect(struct sk_buff *skb, struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3057) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3058) ip6_redirect(skb, sock_net(sk), sk->sk_bound_dev_if, sk->sk_mark,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3059) sk->sk_uid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3060) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3061) EXPORT_SYMBOL_GPL(ip6_sk_redirect);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3062)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3063) static unsigned int ip6_default_advmss(const struct dst_entry *dst)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3064) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3065) struct net_device *dev = dst->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3066) unsigned int mtu = dst_mtu(dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3067) struct net *net = dev_net(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3068)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3069) mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3070)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3071) if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3072) mtu = net->ipv6.sysctl.ip6_rt_min_advmss;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3073)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3074) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3075) * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3076) * corresponding MSS is IPV6_MAXPLEN - tcp_header_size.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3077) * IPV6_MAXPLEN is also valid and means: "any MSS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3078) * rely only on pmtu discovery"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3079) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3080) if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3081) mtu = IPV6_MAXPLEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3082) return mtu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3083) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3084)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3085) static unsigned int ip6_mtu(const struct dst_entry *dst)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3086) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3087) struct inet6_dev *idev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3088) unsigned int mtu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3089)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3090) mtu = dst_metric_raw(dst, RTAX_MTU);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3091) if (mtu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3092) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3093)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3094) mtu = IPV6_MIN_MTU;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3095)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3096) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3097) idev = __in6_dev_get(dst->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3098) if (idev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3099) mtu = idev->cnf.mtu6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3100) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3102) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3103) mtu = min_t(unsigned int, mtu, IP6_MAX_MTU);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3105) return mtu - lwtunnel_headroom(dst->lwtstate, mtu);
^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) /* MTU selection:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3109) * 1. mtu on route is locked - use it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3110) * 2. mtu from nexthop exception
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3111) * 3. mtu from egress device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3112) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3113) * based on ip6_dst_mtu_forward and exception logic of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3114) * rt6_find_cached_rt; called with rcu_read_lock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3115) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3116) u32 ip6_mtu_from_fib6(const struct fib6_result *res,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3117) const struct in6_addr *daddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3118) const struct in6_addr *saddr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3119) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3120) const struct fib6_nh *nh = res->nh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3121) struct fib6_info *f6i = res->f6i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3122) struct inet6_dev *idev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3123) struct rt6_info *rt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3124) u32 mtu = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3126) if (unlikely(fib6_metric_locked(f6i, RTAX_MTU))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3127) mtu = f6i->fib6_pmtu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3128) if (mtu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3129) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3130) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3132) rt = rt6_find_cached_rt(res, daddr, saddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3133) if (unlikely(rt)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3134) mtu = dst_metric_raw(&rt->dst, RTAX_MTU);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3135) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3136) struct net_device *dev = nh->fib_nh_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3138) mtu = IPV6_MIN_MTU;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3139) idev = __in6_dev_get(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3140) if (idev && idev->cnf.mtu6 > mtu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3141) mtu = idev->cnf.mtu6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3142) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3144) mtu = min_t(unsigned int, mtu, IP6_MAX_MTU);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3145) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3146) return mtu - lwtunnel_headroom(nh->fib_nh_lws, mtu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3147) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3149) struct dst_entry *icmp6_dst_alloc(struct net_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3150) struct flowi6 *fl6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3151) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3152) struct dst_entry *dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3153) struct rt6_info *rt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3154) struct inet6_dev *idev = in6_dev_get(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3155) struct net *net = dev_net(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3157) if (unlikely(!idev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3158) return ERR_PTR(-ENODEV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3160) rt = ip6_dst_alloc(net, dev, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3161) if (unlikely(!rt)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3162) in6_dev_put(idev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3163) dst = ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3164) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3165) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3167) rt->dst.input = ip6_input;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3168) rt->dst.output = ip6_output;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3169) rt->rt6i_gateway = fl6->daddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3170) rt->rt6i_dst.addr = fl6->daddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3171) rt->rt6i_dst.plen = 128;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3172) rt->rt6i_idev = idev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3173) dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3174)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3175) /* Add this dst into uncached_list so that rt6_disable_ip() can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3176) * do proper release of the net_device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3177) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3178) rt6_uncached_list_add(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3179) atomic_inc(&net->ipv6.rt6_stats->fib_rt_uncache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3180)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3181) dst = xfrm_lookup(net, &rt->dst, flowi6_to_flowi(fl6), NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3182)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3183) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3184) return dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3185) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3187) static int ip6_dst_gc(struct dst_ops *ops)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3188) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3189) struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3190) int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3191) int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3192) int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3193) int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3194) unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3195) int entries;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3196)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3197) entries = dst_entries_get_fast(ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3198) if (entries > rt_max_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3199) entries = dst_entries_get_slow(ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3201) if (time_after(rt_last_gc + rt_min_interval, jiffies) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3202) entries <= rt_max_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3203) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3204)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3205) net->ipv6.ip6_rt_gc_expire++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3206) fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3207) entries = dst_entries_get_slow(ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3208) if (entries < ops->gc_thresh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3209) net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3210) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3211) net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3212) return entries > rt_max_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3213) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3214)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3215) static int ip6_nh_lookup_table(struct net *net, struct fib6_config *cfg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3216) const struct in6_addr *gw_addr, u32 tbid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3217) int flags, struct fib6_result *res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3218) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3219) struct flowi6 fl6 = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3220) .flowi6_oif = cfg->fc_ifindex,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3221) .daddr = *gw_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3222) .saddr = cfg->fc_prefsrc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3223) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3224) struct fib6_table *table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3225) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3226)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3227) table = fib6_get_table(net, tbid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3228) if (!table)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3229) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3230)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3231) if (!ipv6_addr_any(&cfg->fc_prefsrc))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3232) flags |= RT6_LOOKUP_F_HAS_SADDR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3233)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3234) flags |= RT6_LOOKUP_F_IGNORE_LINKSTATE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3235)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3236) err = fib6_table_lookup(net, table, cfg->fc_ifindex, &fl6, res, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3237) if (!err && res->f6i != net->ipv6.fib6_null_entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3238) fib6_select_path(net, res, &fl6, cfg->fc_ifindex,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3239) cfg->fc_ifindex != 0, NULL, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3240)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3241) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3242) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3243)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3244) static int ip6_route_check_nh_onlink(struct net *net,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3245) struct fib6_config *cfg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3246) const struct net_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3247) struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3248) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3249) u32 tbid = l3mdev_fib_table_rcu(dev) ? : RT_TABLE_MAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3250) const struct in6_addr *gw_addr = &cfg->fc_gateway;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3251) struct fib6_result res = {};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3252) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3253)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3254) err = ip6_nh_lookup_table(net, cfg, gw_addr, tbid, 0, &res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3255) if (!err && !(res.fib6_flags & RTF_REJECT) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3256) /* ignore match if it is the default route */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3257) !ipv6_addr_any(&res.f6i->fib6_dst.addr) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3258) (res.fib6_type != RTN_UNICAST || dev != res.nh->fib_nh_dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3259) NL_SET_ERR_MSG(extack,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3260) "Nexthop has invalid gateway or device mismatch");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3261) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3262) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3263)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3264) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3265) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3266)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3267) static int ip6_route_check_nh(struct net *net,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3268) struct fib6_config *cfg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3269) struct net_device **_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3270) struct inet6_dev **idev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3271) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3272) const struct in6_addr *gw_addr = &cfg->fc_gateway;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3273) struct net_device *dev = _dev ? *_dev : NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3274) int flags = RT6_LOOKUP_F_IFACE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3275) struct fib6_result res = {};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3276) int err = -EHOSTUNREACH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3277)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3278) if (cfg->fc_table) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3279) err = ip6_nh_lookup_table(net, cfg, gw_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3280) cfg->fc_table, flags, &res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3281) /* gw_addr can not require a gateway or resolve to a reject
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3282) * route. If a device is given, it must match the result.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3283) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3284) if (err || res.fib6_flags & RTF_REJECT ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3285) res.nh->fib_nh_gw_family ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3286) (dev && dev != res.nh->fib_nh_dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3287) err = -EHOSTUNREACH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3288) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3289)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3290) if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3291) struct flowi6 fl6 = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3292) .flowi6_oif = cfg->fc_ifindex,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3293) .daddr = *gw_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3294) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3295)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3296) err = fib6_lookup(net, cfg->fc_ifindex, &fl6, &res, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3297) if (err || res.fib6_flags & RTF_REJECT ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3298) res.nh->fib_nh_gw_family)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3299) err = -EHOSTUNREACH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3300)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3301) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3302) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3303)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3304) fib6_select_path(net, &res, &fl6, cfg->fc_ifindex,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3305) cfg->fc_ifindex != 0, NULL, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3306) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3307)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3308) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3309) if (dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3310) if (dev != res.nh->fib_nh_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3311) err = -EHOSTUNREACH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3312) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3313) *_dev = dev = res.nh->fib_nh_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3314) dev_hold(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3315) *idev = in6_dev_get(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3316) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3317)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3318) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3319) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3320)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3321) static int ip6_validate_gw(struct net *net, struct fib6_config *cfg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3322) struct net_device **_dev, struct inet6_dev **idev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3323) struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3324) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3325) const struct in6_addr *gw_addr = &cfg->fc_gateway;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3326) int gwa_type = ipv6_addr_type(gw_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3327) bool skip_dev = gwa_type & IPV6_ADDR_LINKLOCAL ? false : true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3328) const struct net_device *dev = *_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3329) bool need_addr_check = !dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3330) int err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3331)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3332) /* if gw_addr is local we will fail to detect this in case
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3333) * address is still TENTATIVE (DAD in progress). rt6_lookup()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3334) * will return already-added prefix route via interface that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3335) * prefix route was assigned to, which might be non-loopback.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3336) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3337) if (dev &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3338) ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3339) NL_SET_ERR_MSG(extack, "Gateway can not be a local address");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3340) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3341) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3342)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3343) if (gwa_type != (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_UNICAST)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3344) /* IPv6 strictly inhibits using not link-local
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3345) * addresses as nexthop address.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3346) * Otherwise, router will not able to send redirects.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3347) * It is very good, but in some (rare!) circumstances
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3348) * (SIT, PtP, NBMA NOARP links) it is handy to allow
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3349) * some exceptions. --ANK
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3350) * We allow IPv4-mapped nexthops to support RFC4798-type
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3351) * addressing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3352) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3353) if (!(gwa_type & (IPV6_ADDR_UNICAST | IPV6_ADDR_MAPPED))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3354) NL_SET_ERR_MSG(extack, "Invalid gateway address");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3355) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3356) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3357)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3358) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3359)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3360) if (cfg->fc_flags & RTNH_F_ONLINK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3361) err = ip6_route_check_nh_onlink(net, cfg, dev, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3362) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3363) err = ip6_route_check_nh(net, cfg, _dev, idev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3364)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3365) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3366)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3367) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3368) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3369) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3370)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3371) /* reload in case device was changed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3372) dev = *_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3373)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3374) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3375) if (!dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3376) NL_SET_ERR_MSG(extack, "Egress device not specified");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3377) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3378) } else if (dev->flags & IFF_LOOPBACK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3379) NL_SET_ERR_MSG(extack,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3380) "Egress device can not be loopback device for this route");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3381) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3382) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3383)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3384) /* if we did not check gw_addr above, do so now that the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3385) * egress device has been resolved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3386) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3387) if (need_addr_check &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3388) ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3389) NL_SET_ERR_MSG(extack, "Gateway can not be a local address");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3390) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3391) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3392)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3393) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3394) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3395) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3396) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3397)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3398) static bool fib6_is_reject(u32 flags, struct net_device *dev, int addr_type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3399) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3400) if ((flags & RTF_REJECT) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3401) (dev && (dev->flags & IFF_LOOPBACK) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3402) !(addr_type & IPV6_ADDR_LOOPBACK) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3403) !(flags & (RTF_ANYCAST | RTF_LOCAL))))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3404) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3405)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3406) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3407) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3408)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3409) int fib6_nh_init(struct net *net, struct fib6_nh *fib6_nh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3410) struct fib6_config *cfg, gfp_t gfp_flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3411) struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3412) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3413) struct net_device *dev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3414) struct inet6_dev *idev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3415) int addr_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3416) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3417)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3418) fib6_nh->fib_nh_family = AF_INET6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3419) #ifdef CONFIG_IPV6_ROUTER_PREF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3420) fib6_nh->last_probe = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3421) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3422) if (cfg->fc_is_fdb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3423) fib6_nh->fib_nh_gw6 = cfg->fc_gateway;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3424) fib6_nh->fib_nh_gw_family = AF_INET6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3425) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3426) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3427)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3428) err = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3429) if (cfg->fc_ifindex) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3430) dev = dev_get_by_index(net, cfg->fc_ifindex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3431) if (!dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3432) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3433) idev = in6_dev_get(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3434) if (!idev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3435) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3436) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3437)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3438) if (cfg->fc_flags & RTNH_F_ONLINK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3439) if (!dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3440) NL_SET_ERR_MSG(extack,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3441) "Nexthop device required for onlink");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3442) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3443) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3444)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3445) if (!(dev->flags & IFF_UP)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3446) NL_SET_ERR_MSG(extack, "Nexthop device is not up");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3447) err = -ENETDOWN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3448) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3449) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3450)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3451) fib6_nh->fib_nh_flags |= RTNH_F_ONLINK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3452) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3453)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3454) fib6_nh->fib_nh_weight = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3455)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3456) /* We cannot add true routes via loopback here,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3457) * they would result in kernel looping; promote them to reject routes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3458) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3459) addr_type = ipv6_addr_type(&cfg->fc_dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3460) if (fib6_is_reject(cfg->fc_flags, dev, addr_type)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3461) /* hold loopback dev/idev if we haven't done so. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3462) if (dev != net->loopback_dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3463) if (dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3464) dev_put(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3465) in6_dev_put(idev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3466) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3467) dev = net->loopback_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3468) dev_hold(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3469) idev = in6_dev_get(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3470) if (!idev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3471) err = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3472) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3473) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3474) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3475) goto pcpu_alloc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3476) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3477)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3478) if (cfg->fc_flags & RTF_GATEWAY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3479) err = ip6_validate_gw(net, cfg, &dev, &idev, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3480) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3481) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3482)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3483) fib6_nh->fib_nh_gw6 = cfg->fc_gateway;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3484) fib6_nh->fib_nh_gw_family = AF_INET6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3485) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3486)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3487) err = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3488) if (!dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3489) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3490)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3491) if (idev->cnf.disable_ipv6) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3492) NL_SET_ERR_MSG(extack, "IPv6 is disabled on nexthop device");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3493) err = -EACCES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3494) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3495) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3496)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3497) if (!(dev->flags & IFF_UP) && !cfg->fc_ignore_dev_down) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3498) NL_SET_ERR_MSG(extack, "Nexthop device is not up");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3499) err = -ENETDOWN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3500) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3501) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3502)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3503) if (!(cfg->fc_flags & (RTF_LOCAL | RTF_ANYCAST)) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3504) !netif_carrier_ok(dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3505) fib6_nh->fib_nh_flags |= RTNH_F_LINKDOWN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3506)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3507) err = fib_nh_common_init(net, &fib6_nh->nh_common, cfg->fc_encap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3508) cfg->fc_encap_type, cfg, gfp_flags, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3509) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3510) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3511)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3512) pcpu_alloc:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3513) fib6_nh->rt6i_pcpu = alloc_percpu_gfp(struct rt6_info *, gfp_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3514) if (!fib6_nh->rt6i_pcpu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3515) err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3516) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3517) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3518)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3519) fib6_nh->fib_nh_dev = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3520) fib6_nh->fib_nh_oif = dev->ifindex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3521) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3522) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3523) if (idev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3524) in6_dev_put(idev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3525)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3526) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3527) lwtstate_put(fib6_nh->fib_nh_lws);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3528) fib6_nh->fib_nh_lws = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3529) if (dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3530) dev_put(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3531) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3532)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3533) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3534) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3535)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3536) void fib6_nh_release(struct fib6_nh *fib6_nh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3537) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3538) struct rt6_exception_bucket *bucket;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3539)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3540) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3541)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3542) fib6_nh_flush_exceptions(fib6_nh, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3543) bucket = fib6_nh_get_excptn_bucket(fib6_nh, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3544) if (bucket) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3545) rcu_assign_pointer(fib6_nh->rt6i_exception_bucket, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3546) kfree(bucket);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3547) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3548)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3549) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3550)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3551) if (fib6_nh->rt6i_pcpu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3552) int cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3553)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3554) for_each_possible_cpu(cpu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3555) struct rt6_info **ppcpu_rt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3556) struct rt6_info *pcpu_rt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3557)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3558) ppcpu_rt = per_cpu_ptr(fib6_nh->rt6i_pcpu, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3559) pcpu_rt = *ppcpu_rt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3560) if (pcpu_rt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3561) dst_dev_put(&pcpu_rt->dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3562) dst_release(&pcpu_rt->dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3563) *ppcpu_rt = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3564) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3565) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3566)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3567) free_percpu(fib6_nh->rt6i_pcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3568) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3569)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3570) fib_nh_common_release(&fib6_nh->nh_common);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3571) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3572)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3573) static struct fib6_info *ip6_route_info_create(struct fib6_config *cfg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3574) gfp_t gfp_flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3575) struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3576) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3577) struct net *net = cfg->fc_nlinfo.nl_net;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3578) struct fib6_info *rt = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3579) struct nexthop *nh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3580) struct fib6_table *table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3581) struct fib6_nh *fib6_nh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3582) int err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3583) int addr_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3584)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3585) /* RTF_PCPU is an internal flag; can not be set by userspace */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3586) if (cfg->fc_flags & RTF_PCPU) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3587) NL_SET_ERR_MSG(extack, "Userspace can not set RTF_PCPU");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3588) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3589) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3590)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3591) /* RTF_CACHE is an internal flag; can not be set by userspace */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3592) if (cfg->fc_flags & RTF_CACHE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3593) NL_SET_ERR_MSG(extack, "Userspace can not set RTF_CACHE");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3594) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3595) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3596)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3597) if (cfg->fc_type > RTN_MAX) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3598) NL_SET_ERR_MSG(extack, "Invalid route type");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3599) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3600) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3601)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3602) if (cfg->fc_dst_len > 128) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3603) NL_SET_ERR_MSG(extack, "Invalid prefix length");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3604) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3605) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3606) if (cfg->fc_src_len > 128) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3607) NL_SET_ERR_MSG(extack, "Invalid source address length");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3608) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3609) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3610) #ifndef CONFIG_IPV6_SUBTREES
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3611) if (cfg->fc_src_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3612) NL_SET_ERR_MSG(extack,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3613) "Specifying source address requires IPV6_SUBTREES to be enabled");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3614) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3615) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3616) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3617) if (cfg->fc_nh_id) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3618) nh = nexthop_find_by_id(net, cfg->fc_nh_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3619) if (!nh) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3620) NL_SET_ERR_MSG(extack, "Nexthop id does not exist");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3621) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3622) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3623) err = fib6_check_nexthop(nh, cfg, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3624) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3625) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3626) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3627)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3628) err = -ENOBUFS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3629) if (cfg->fc_nlinfo.nlh &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3630) !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3631) table = fib6_get_table(net, cfg->fc_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3632) if (!table) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3633) pr_warn("NLM_F_CREATE should be specified when creating new route\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3634) table = fib6_new_table(net, cfg->fc_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3635) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3636) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3637) table = fib6_new_table(net, cfg->fc_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3638) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3639)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3640) if (!table)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3641) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3642)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3643) err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3644) rt = fib6_info_alloc(gfp_flags, !nh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3645) if (!rt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3646) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3647)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3648) rt->fib6_metrics = ip_fib_metrics_init(net, cfg->fc_mx, cfg->fc_mx_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3649) extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3650) if (IS_ERR(rt->fib6_metrics)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3651) err = PTR_ERR(rt->fib6_metrics);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3652) /* Do not leave garbage there. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3653) rt->fib6_metrics = (struct dst_metrics *)&dst_default_metrics;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3654) goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3655) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3656)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3657) if (cfg->fc_flags & RTF_ADDRCONF)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3658) rt->dst_nocount = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3659)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3660) if (cfg->fc_flags & RTF_EXPIRES)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3661) fib6_set_expires(rt, jiffies +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3662) clock_t_to_jiffies(cfg->fc_expires));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3663) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3664) fib6_clean_expires(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3665)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3666) if (cfg->fc_protocol == RTPROT_UNSPEC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3667) cfg->fc_protocol = RTPROT_BOOT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3668) rt->fib6_protocol = cfg->fc_protocol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3669)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3670) rt->fib6_table = table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3671) rt->fib6_metric = cfg->fc_metric;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3672) rt->fib6_type = cfg->fc_type ? : RTN_UNICAST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3673) rt->fib6_flags = cfg->fc_flags & ~RTF_GATEWAY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3674)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3675) ipv6_addr_prefix(&rt->fib6_dst.addr, &cfg->fc_dst, cfg->fc_dst_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3676) rt->fib6_dst.plen = cfg->fc_dst_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3677)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3678) #ifdef CONFIG_IPV6_SUBTREES
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3679) ipv6_addr_prefix(&rt->fib6_src.addr, &cfg->fc_src, cfg->fc_src_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3680) rt->fib6_src.plen = cfg->fc_src_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3681) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3682) if (nh) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3683) if (rt->fib6_src.plen) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3684) NL_SET_ERR_MSG(extack, "Nexthops can not be used with source routing");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3685) goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3686) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3687) if (!nexthop_get(nh)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3688) NL_SET_ERR_MSG(extack, "Nexthop has been deleted");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3689) goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3690) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3691) rt->nh = nh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3692) fib6_nh = nexthop_fib6_nh(rt->nh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3693) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3694) err = fib6_nh_init(net, rt->fib6_nh, cfg, gfp_flags, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3695) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3696) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3697)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3698) fib6_nh = rt->fib6_nh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3699)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3700) /* We cannot add true routes via loopback here, they would
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3701) * result in kernel looping; promote them to reject routes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3702) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3703) addr_type = ipv6_addr_type(&cfg->fc_dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3704) if (fib6_is_reject(cfg->fc_flags, rt->fib6_nh->fib_nh_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3705) addr_type))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3706) rt->fib6_flags = RTF_REJECT | RTF_NONEXTHOP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3707) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3708)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3709) if (!ipv6_addr_any(&cfg->fc_prefsrc)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3710) struct net_device *dev = fib6_nh->fib_nh_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3711)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3712) if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3713) NL_SET_ERR_MSG(extack, "Invalid source address");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3714) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3715) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3716) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3717) rt->fib6_prefsrc.addr = cfg->fc_prefsrc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3718) rt->fib6_prefsrc.plen = 128;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3719) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3720) rt->fib6_prefsrc.plen = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3721)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3722) return rt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3723) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3724) fib6_info_release(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3725) return ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3726) out_free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3727) ip_fib_metrics_put(rt->fib6_metrics);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3728) kfree(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3729) return ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3730) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3731)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3732) int ip6_route_add(struct fib6_config *cfg, gfp_t gfp_flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3733) struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3734) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3735) struct fib6_info *rt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3736) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3737)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3738) rt = ip6_route_info_create(cfg, gfp_flags, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3739) if (IS_ERR(rt))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3740) return PTR_ERR(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3741)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3742) err = __ip6_ins_rt(rt, &cfg->fc_nlinfo, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3743) fib6_info_release(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3744)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3745) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3746) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3747)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3748) static int __ip6_del_rt(struct fib6_info *rt, struct nl_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3749) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3750) struct net *net = info->nl_net;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3751) struct fib6_table *table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3752) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3753)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3754) if (rt == net->ipv6.fib6_null_entry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3755) err = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3756) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3757) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3758)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3759) table = rt->fib6_table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3760) spin_lock_bh(&table->tb6_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3761) err = fib6_del(rt, info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3762) spin_unlock_bh(&table->tb6_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3763)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3764) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3765) fib6_info_release(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3766) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3767) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3768)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3769) int ip6_del_rt(struct net *net, struct fib6_info *rt, bool skip_notify)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3770) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3771) struct nl_info info = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3772) .nl_net = net,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3773) .skip_notify = skip_notify
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3774) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3775)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3776) return __ip6_del_rt(rt, &info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3777) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3778)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3779) static int __ip6_del_rt_siblings(struct fib6_info *rt, struct fib6_config *cfg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3780) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3781) struct nl_info *info = &cfg->fc_nlinfo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3782) struct net *net = info->nl_net;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3783) struct sk_buff *skb = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3784) struct fib6_table *table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3785) int err = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3786)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3787) if (rt == net->ipv6.fib6_null_entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3788) goto out_put;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3789) table = rt->fib6_table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3790) spin_lock_bh(&table->tb6_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3791)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3792) if (rt->fib6_nsiblings && cfg->fc_delete_all_nh) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3793) struct fib6_info *sibling, *next_sibling;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3794) struct fib6_node *fn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3795)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3796) /* prefer to send a single notification with all hops */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3797) skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3798) if (skb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3799) u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3800)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3801) if (rt6_fill_node(net, skb, rt, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3802) NULL, NULL, 0, RTM_DELROUTE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3803) info->portid, seq, 0) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3804) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3805) skb = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3806) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3807) info->skip_notify = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3808) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3809)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3810) /* 'rt' points to the first sibling route. If it is not the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3811) * leaf, then we do not need to send a notification. Otherwise,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3812) * we need to check if the last sibling has a next route or not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3813) * and emit a replace or delete notification, respectively.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3814) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3815) info->skip_notify_kernel = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3816) fn = rcu_dereference_protected(rt->fib6_node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3817) lockdep_is_held(&table->tb6_lock));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3818) if (rcu_access_pointer(fn->leaf) == rt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3819) struct fib6_info *last_sibling, *replace_rt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3820)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3821) last_sibling = list_last_entry(&rt->fib6_siblings,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3822) struct fib6_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3823) fib6_siblings);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3824) replace_rt = rcu_dereference_protected(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3825) last_sibling->fib6_next,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3826) lockdep_is_held(&table->tb6_lock));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3827) if (replace_rt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3828) call_fib6_entry_notifiers_replace(net,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3829) replace_rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3830) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3831) call_fib6_multipath_entry_notifiers(net,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3832) FIB_EVENT_ENTRY_DEL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3833) rt, rt->fib6_nsiblings,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3834) NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3835) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3836) list_for_each_entry_safe(sibling, next_sibling,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3837) &rt->fib6_siblings,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3838) fib6_siblings) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3839) err = fib6_del(sibling, info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3840) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3841) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3842) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3843) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3844)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3845) err = fib6_del(rt, info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3846) out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3847) spin_unlock_bh(&table->tb6_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3848) out_put:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3849) fib6_info_release(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3850)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3851) if (skb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3852) rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3853) info->nlh, gfp_any());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3854) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3855) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3856) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3857)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3858) static int __ip6_del_cached_rt(struct rt6_info *rt, struct fib6_config *cfg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3859) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3860) int rc = -ESRCH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3861)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3862) if (cfg->fc_ifindex && rt->dst.dev->ifindex != cfg->fc_ifindex)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3863) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3864)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3865) if (cfg->fc_flags & RTF_GATEWAY &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3866) !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3867) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3868)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3869) rc = rt6_remove_exception_rt(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3870) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3871) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3872) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3873)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3874) static int ip6_del_cached_rt(struct fib6_config *cfg, struct fib6_info *rt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3875) struct fib6_nh *nh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3876) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3877) struct fib6_result res = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3878) .f6i = rt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3879) .nh = nh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3880) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3881) struct rt6_info *rt_cache;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3882)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3883) rt_cache = rt6_find_cached_rt(&res, &cfg->fc_dst, &cfg->fc_src);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3884) if (rt_cache)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3885) return __ip6_del_cached_rt(rt_cache, cfg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3886)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3887) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3888) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3889)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3890) struct fib6_nh_del_cached_rt_arg {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3891) struct fib6_config *cfg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3892) struct fib6_info *f6i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3893) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3894)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3895) static int fib6_nh_del_cached_rt(struct fib6_nh *nh, void *_arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3896) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3897) struct fib6_nh_del_cached_rt_arg *arg = _arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3898) int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3899)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3900) rc = ip6_del_cached_rt(arg->cfg, arg->f6i, nh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3901) return rc != -ESRCH ? rc : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3902) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3903)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3904) static int ip6_del_cached_rt_nh(struct fib6_config *cfg, struct fib6_info *f6i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3905) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3906) struct fib6_nh_del_cached_rt_arg arg = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3907) .cfg = cfg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3908) .f6i = f6i
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3909) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3910)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3911) return nexthop_for_each_fib6_nh(f6i->nh, fib6_nh_del_cached_rt, &arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3912) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3913)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3914) static int ip6_route_del(struct fib6_config *cfg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3915) struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3916) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3917) struct fib6_table *table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3918) struct fib6_info *rt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3919) struct fib6_node *fn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3920) int err = -ESRCH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3921)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3922) table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3923) if (!table) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3924) NL_SET_ERR_MSG(extack, "FIB table does not exist");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3925) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3926) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3927)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3928) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3929)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3930) fn = fib6_locate(&table->tb6_root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3931) &cfg->fc_dst, cfg->fc_dst_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3932) &cfg->fc_src, cfg->fc_src_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3933) !(cfg->fc_flags & RTF_CACHE));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3934)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3935) if (fn) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3936) for_each_fib6_node_rt_rcu(fn) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3937) struct fib6_nh *nh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3938)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3939) if (rt->nh && cfg->fc_nh_id &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3940) rt->nh->id != cfg->fc_nh_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3941) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3942)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3943) if (cfg->fc_flags & RTF_CACHE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3944) int rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3945)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3946) if (rt->nh) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3947) rc = ip6_del_cached_rt_nh(cfg, rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3948) } else if (cfg->fc_nh_id) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3949) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3950) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3951) nh = rt->fib6_nh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3952) rc = ip6_del_cached_rt(cfg, rt, nh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3953) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3954) if (rc != -ESRCH) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3955) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3956) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3957) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3958) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3959) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3960)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3961) if (cfg->fc_metric && cfg->fc_metric != rt->fib6_metric)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3962) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3963) if (cfg->fc_protocol &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3964) cfg->fc_protocol != rt->fib6_protocol)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3965) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3966)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3967) if (rt->nh) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3968) if (!fib6_info_hold_safe(rt))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3969) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3970) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3971)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3972) return __ip6_del_rt(rt, &cfg->fc_nlinfo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3973) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3974) if (cfg->fc_nh_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3975) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3976)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3977) nh = rt->fib6_nh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3978) if (cfg->fc_ifindex &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3979) (!nh->fib_nh_dev ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3980) nh->fib_nh_dev->ifindex != cfg->fc_ifindex))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3981) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3982) if (cfg->fc_flags & RTF_GATEWAY &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3983) !ipv6_addr_equal(&cfg->fc_gateway, &nh->fib_nh_gw6))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3984) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3985) if (!fib6_info_hold_safe(rt))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3986) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3987) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3988)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3989) /* if gateway was specified only delete the one hop */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3990) if (cfg->fc_flags & RTF_GATEWAY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3991) return __ip6_del_rt(rt, &cfg->fc_nlinfo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3992)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3993) return __ip6_del_rt_siblings(rt, cfg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3994) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3995) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3996) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3997)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3998) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3999) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4000)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4001) static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4002) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4003) struct netevent_redirect netevent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4004) struct rt6_info *rt, *nrt = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4005) struct fib6_result res = {};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4006) struct ndisc_options ndopts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4007) struct inet6_dev *in6_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4008) struct neighbour *neigh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4009) struct rd_msg *msg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4010) int optlen, on_link;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4011) u8 *lladdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4012)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4013) optlen = skb_tail_pointer(skb) - skb_transport_header(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4014) optlen -= sizeof(*msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4015)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4016) if (optlen < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4017) net_dbg_ratelimited("rt6_do_redirect: packet too short\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4018) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4019) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4020)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4021) msg = (struct rd_msg *)icmp6_hdr(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4022)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4023) if (ipv6_addr_is_multicast(&msg->dest)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4024) net_dbg_ratelimited("rt6_do_redirect: destination address is multicast\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4025) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4026) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4027)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4028) on_link = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4029) if (ipv6_addr_equal(&msg->dest, &msg->target)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4030) on_link = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4031) } else if (ipv6_addr_type(&msg->target) !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4032) (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4033) net_dbg_ratelimited("rt6_do_redirect: target address is not link-local unicast\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4034) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4035) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4036)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4037) in6_dev = __in6_dev_get(skb->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4038) if (!in6_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4039) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4040) if (in6_dev->cnf.forwarding || !in6_dev->cnf.accept_redirects)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4041) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4042)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4043) /* RFC2461 8.1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4044) * The IP source address of the Redirect MUST be the same as the current
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4045) * first-hop router for the specified ICMP Destination Address.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4046) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4047)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4048) if (!ndisc_parse_options(skb->dev, msg->opt, optlen, &ndopts)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4049) net_dbg_ratelimited("rt6_redirect: invalid ND options\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4050) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4051) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4052)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4053) lladdr = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4054) if (ndopts.nd_opts_tgt_lladdr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4055) lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4056) skb->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4057) if (!lladdr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4058) net_dbg_ratelimited("rt6_redirect: invalid link-layer address length\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4059) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4060) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4061) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4062)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4063) rt = (struct rt6_info *) dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4064) if (rt->rt6i_flags & RTF_REJECT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4065) net_dbg_ratelimited("rt6_redirect: source isn't a valid nexthop for redirect target\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4066) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4067) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4068)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4069) /* Redirect received -> path was valid.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4070) * Look, redirects are sent only in response to data packets,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4071) * so that this nexthop apparently is reachable. --ANK
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4072) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4073) dst_confirm_neigh(&rt->dst, &ipv6_hdr(skb)->saddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4074)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4075) neigh = __neigh_lookup(&nd_tbl, &msg->target, skb->dev, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4076) if (!neigh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4077) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4078)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4079) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4080) * We have finally decided to accept it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4081) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4082)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4083) ndisc_update(skb->dev, neigh, lladdr, NUD_STALE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4084) NEIGH_UPDATE_F_WEAK_OVERRIDE|
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4085) NEIGH_UPDATE_F_OVERRIDE|
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4086) (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4087) NEIGH_UPDATE_F_ISROUTER)),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4088) NDISC_REDIRECT, &ndopts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4089)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4090) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4091) res.f6i = rcu_dereference(rt->from);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4092) if (!res.f6i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4093) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4094)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4095) if (res.f6i->nh) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4096) struct fib6_nh_match_arg arg = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4097) .dev = dst->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4098) .gw = &rt->rt6i_gateway,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4099) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4101) nexthop_for_each_fib6_nh(res.f6i->nh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4102) fib6_nh_find_match, &arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4104) /* fib6_info uses a nexthop that does not have fib6_nh
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4105) * using the dst->dev. Should be impossible
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4106) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4107) if (!arg.match)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4108) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4109) res.nh = arg.match;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4110) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4111) res.nh = res.f6i->fib6_nh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4112) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4114) res.fib6_flags = res.f6i->fib6_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4115) res.fib6_type = res.f6i->fib6_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4116) nrt = ip6_rt_cache_alloc(&res, &msg->dest, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4117) if (!nrt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4118) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4120) nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4121) if (on_link)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4122) nrt->rt6i_flags &= ~RTF_GATEWAY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4124) nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4126) /* rt6_insert_exception() will take care of duplicated exceptions */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4127) if (rt6_insert_exception(nrt, &res)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4128) dst_release_immediate(&nrt->dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4129) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4130) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4132) netevent.old = &rt->dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4133) netevent.new = &nrt->dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4134) netevent.daddr = &msg->dest;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4135) netevent.neigh = neigh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4136) call_netevent_notifiers(NETEVENT_REDIRECT, &netevent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4138) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4139) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4140) neigh_release(neigh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4141) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4143) #ifdef CONFIG_IPV6_ROUTE_INFO
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4144) static struct fib6_info *rt6_get_route_info(struct net *net,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4145) const struct in6_addr *prefix, int prefixlen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4146) const struct in6_addr *gwaddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4147) struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4148) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4149) u32 tb_id = l3mdev_fib_table(dev) ? : addrconf_rt_table(dev, RT6_TABLE_INFO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4150) int ifindex = dev->ifindex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4151) struct fib6_node *fn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4152) struct fib6_info *rt = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4153) struct fib6_table *table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4155) table = fib6_get_table(net, tb_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4156) if (!table)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4157) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4159) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4160) fn = fib6_locate(&table->tb6_root, prefix, prefixlen, NULL, 0, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4161) if (!fn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4162) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4164) for_each_fib6_node_rt_rcu(fn) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4165) /* these routes do not use nexthops */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4166) if (rt->nh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4167) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4168) if (rt->fib6_nh->fib_nh_dev->ifindex != ifindex)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4169) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4170) if (!(rt->fib6_flags & RTF_ROUTEINFO) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4171) !rt->fib6_nh->fib_nh_gw_family)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4172) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4173) if (!ipv6_addr_equal(&rt->fib6_nh->fib_nh_gw6, gwaddr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4174) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4175) if (!fib6_info_hold_safe(rt))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4176) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4177) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4178) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4179) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4180) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4181) return rt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4182) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4183)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4184) static struct fib6_info *rt6_add_route_info(struct net *net,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4185) const struct in6_addr *prefix, int prefixlen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4186) const struct in6_addr *gwaddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4187) struct net_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4188) unsigned int pref)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4189) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4190) struct fib6_config cfg = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4191) .fc_metric = IP6_RT_PRIO_USER,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4192) .fc_ifindex = dev->ifindex,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4193) .fc_dst_len = prefixlen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4194) .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4195) RTF_UP | RTF_PREF(pref),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4196) .fc_protocol = RTPROT_RA,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4197) .fc_type = RTN_UNICAST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4198) .fc_nlinfo.portid = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4199) .fc_nlinfo.nlh = NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4200) .fc_nlinfo.nl_net = net,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4201) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4202)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4203) cfg.fc_table = l3mdev_fib_table(dev) ? : addrconf_rt_table(dev, RT6_TABLE_INFO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4204) cfg.fc_dst = *prefix;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4205) cfg.fc_gateway = *gwaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4206)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4207) /* We should treat it as a default route if prefix length is 0. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4208) if (!prefixlen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4209) cfg.fc_flags |= RTF_DEFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4210)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4211) ip6_route_add(&cfg, GFP_ATOMIC, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4212)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4213) return rt6_get_route_info(net, prefix, prefixlen, gwaddr, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4214) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4215) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4216)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4217) struct fib6_info *rt6_get_dflt_router(struct net *net,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4218) const struct in6_addr *addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4219) struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4220) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4221) u32 tb_id = l3mdev_fib_table(dev) ? : addrconf_rt_table(dev, RT6_TABLE_DFLT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4222) struct fib6_info *rt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4223) struct fib6_table *table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4224)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4225) table = fib6_get_table(net, tb_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4226) if (!table)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4227) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4228)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4229) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4230) for_each_fib6_node_rt_rcu(&table->tb6_root) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4231) struct fib6_nh *nh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4232)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4233) /* RA routes do not use nexthops */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4234) if (rt->nh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4235) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4236)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4237) nh = rt->fib6_nh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4238) if (dev == nh->fib_nh_dev &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4239) ((rt->fib6_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4240) ipv6_addr_equal(&nh->fib_nh_gw6, addr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4241) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4242) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4243) if (rt && !fib6_info_hold_safe(rt))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4244) rt = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4245) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4246) return rt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4247) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4248)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4249) struct fib6_info *rt6_add_dflt_router(struct net *net,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4250) const struct in6_addr *gwaddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4251) struct net_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4252) unsigned int pref)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4253) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4254) struct fib6_config cfg = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4255) .fc_table = l3mdev_fib_table(dev) ? : addrconf_rt_table(dev, RT6_TABLE_DFLT),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4256) .fc_metric = IP6_RT_PRIO_USER,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4257) .fc_ifindex = dev->ifindex,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4258) .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4259) RTF_UP | RTF_EXPIRES | RTF_PREF(pref),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4260) .fc_protocol = RTPROT_RA,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4261) .fc_type = RTN_UNICAST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4262) .fc_nlinfo.portid = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4263) .fc_nlinfo.nlh = NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4264) .fc_nlinfo.nl_net = net,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4265) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4266)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4267) cfg.fc_gateway = *gwaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4268)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4269) if (!ip6_route_add(&cfg, GFP_ATOMIC, NULL)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4270) struct fib6_table *table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4271)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4272) table = fib6_get_table(dev_net(dev), cfg.fc_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4273) if (table)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4274) table->flags |= RT6_TABLE_HAS_DFLT_ROUTER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4275) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4276)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4277) return rt6_get_dflt_router(net, gwaddr, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4278) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4279)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4280) static int rt6_addrconf_purge(struct fib6_info *rt, void *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4281) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4282) struct net_device *dev = fib6_info_nh_dev(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4283) struct inet6_dev *idev = dev ? __in6_dev_get(dev) : NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4284)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4285) if (rt->fib6_flags & (RTF_DEFAULT | RTF_ADDRCONF) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4286) (!idev || idev->cnf.accept_ra != 2)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4287) /* Delete this route. See fib6_clean_tree() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4288) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4289) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4290)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4291) /* Continue walking */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4292) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4293) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4294)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4295) void rt6_purge_dflt_routers(struct net *net)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4296) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4297) fib6_clean_all(net, rt6_addrconf_purge, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4298) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4299)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4300) static void rtmsg_to_fib6_config(struct net *net,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4301) struct in6_rtmsg *rtmsg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4302) struct fib6_config *cfg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4303) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4304) *cfg = (struct fib6_config){
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4305) .fc_table = l3mdev_fib_table_by_index(net, rtmsg->rtmsg_ifindex) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4306) : RT6_TABLE_MAIN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4307) .fc_ifindex = rtmsg->rtmsg_ifindex,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4308) .fc_metric = rtmsg->rtmsg_metric ? : IP6_RT_PRIO_USER,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4309) .fc_expires = rtmsg->rtmsg_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4310) .fc_dst_len = rtmsg->rtmsg_dst_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4311) .fc_src_len = rtmsg->rtmsg_src_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4312) .fc_flags = rtmsg->rtmsg_flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4313) .fc_type = rtmsg->rtmsg_type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4314)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4315) .fc_nlinfo.nl_net = net,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4316)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4317) .fc_dst = rtmsg->rtmsg_dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4318) .fc_src = rtmsg->rtmsg_src,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4319) .fc_gateway = rtmsg->rtmsg_gateway,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4320) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4321) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4322)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4323) int ipv6_route_ioctl(struct net *net, unsigned int cmd, struct in6_rtmsg *rtmsg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4324) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4325) struct fib6_config cfg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4326) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4327)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4328) if (cmd != SIOCADDRT && cmd != SIOCDELRT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4329) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4330) if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4331) return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4332)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4333) rtmsg_to_fib6_config(net, rtmsg, &cfg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4334)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4335) rtnl_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4336) switch (cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4337) case SIOCADDRT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4338) err = ip6_route_add(&cfg, GFP_KERNEL, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4339) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4340) case SIOCDELRT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4341) err = ip6_route_del(&cfg, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4342) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4343) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4344) rtnl_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4345) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4346) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4347)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4348) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4349) * Drop the packet on the floor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4350) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4351)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4352) static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4353) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4354) struct dst_entry *dst = skb_dst(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4355) struct net *net = dev_net(dst->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4356) struct inet6_dev *idev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4357) int type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4358)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4359) if (netif_is_l3_master(skb->dev) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4360) dst->dev == net->loopback_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4361) idev = __in6_dev_get_safely(dev_get_by_index_rcu(net, IP6CB(skb)->iif));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4362) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4363) idev = ip6_dst_idev(dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4364)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4365) switch (ipstats_mib_noroutes) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4366) case IPSTATS_MIB_INNOROUTES:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4367) type = ipv6_addr_type(&ipv6_hdr(skb)->daddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4368) if (type == IPV6_ADDR_ANY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4369) IP6_INC_STATS(net, idev, IPSTATS_MIB_INADDRERRORS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4370) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4371) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4372) fallthrough;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4373) case IPSTATS_MIB_OUTNOROUTES:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4374) IP6_INC_STATS(net, idev, ipstats_mib_noroutes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4375) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4376) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4377)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4378) /* Start over by dropping the dst for l3mdev case */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4379) if (netif_is_l3_master(skb->dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4380) skb_dst_drop(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4381)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4382) icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4383) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4384) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4385) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4386)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4387) static int ip6_pkt_discard(struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4388) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4389) return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4390) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4391)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4392) static int ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4393) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4394) skb->dev = skb_dst(skb)->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4395) return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4396) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4397)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4398) static int ip6_pkt_prohibit(struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4399) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4400) return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4401) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4402)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4403) static int ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4404) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4405) skb->dev = skb_dst(skb)->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4406) return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4407) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4408)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4409) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4410) * Allocate a dst for local (unicast / anycast) address.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4411) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4412)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4413) struct fib6_info *addrconf_f6i_alloc(struct net *net,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4414) struct inet6_dev *idev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4415) const struct in6_addr *addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4416) bool anycast, gfp_t gfp_flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4417) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4418) struct fib6_config cfg = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4419) .fc_table = l3mdev_fib_table(idev->dev) ? : RT6_TABLE_LOCAL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4420) .fc_ifindex = idev->dev->ifindex,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4421) .fc_flags = RTF_UP | RTF_NONEXTHOP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4422) .fc_dst = *addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4423) .fc_dst_len = 128,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4424) .fc_protocol = RTPROT_KERNEL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4425) .fc_nlinfo.nl_net = net,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4426) .fc_ignore_dev_down = true,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4427) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4428) struct fib6_info *f6i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4429)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4430) if (anycast) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4431) cfg.fc_type = RTN_ANYCAST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4432) cfg.fc_flags |= RTF_ANYCAST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4433) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4434) cfg.fc_type = RTN_LOCAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4435) cfg.fc_flags |= RTF_LOCAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4436) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4437)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4438) f6i = ip6_route_info_create(&cfg, gfp_flags, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4439) if (!IS_ERR(f6i))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4440) f6i->dst_nocount = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4441) return f6i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4442) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4443)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4444) /* remove deleted ip from prefsrc entries */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4445) struct arg_dev_net_ip {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4446) struct net_device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4447) struct net *net;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4448) struct in6_addr *addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4449) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4450)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4451) static int fib6_remove_prefsrc(struct fib6_info *rt, void *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4452) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4453) struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4454) struct net *net = ((struct arg_dev_net_ip *)arg)->net;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4455) struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4456)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4457) if (!rt->nh &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4458) ((void *)rt->fib6_nh->fib_nh_dev == dev || !dev) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4459) rt != net->ipv6.fib6_null_entry &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4460) ipv6_addr_equal(addr, &rt->fib6_prefsrc.addr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4461) spin_lock_bh(&rt6_exception_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4462) /* remove prefsrc entry */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4463) rt->fib6_prefsrc.plen = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4464) spin_unlock_bh(&rt6_exception_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4465) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4466) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4467) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4468)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4469) void rt6_remove_prefsrc(struct inet6_ifaddr *ifp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4470) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4471) struct net *net = dev_net(ifp->idev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4472) struct arg_dev_net_ip adni = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4473) .dev = ifp->idev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4474) .net = net,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4475) .addr = &ifp->addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4476) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4477) fib6_clean_all(net, fib6_remove_prefsrc, &adni);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4478) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4479)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4480) #define RTF_RA_ROUTER (RTF_ADDRCONF | RTF_DEFAULT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4481)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4482) /* Remove routers and update dst entries when gateway turn into host. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4483) static int fib6_clean_tohost(struct fib6_info *rt, void *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4484) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4485) struct in6_addr *gateway = (struct in6_addr *)arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4486) struct fib6_nh *nh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4487)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4488) /* RA routes do not use nexthops */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4489) if (rt->nh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4490) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4491)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4492) nh = rt->fib6_nh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4493) if (((rt->fib6_flags & RTF_RA_ROUTER) == RTF_RA_ROUTER) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4494) nh->fib_nh_gw_family && ipv6_addr_equal(gateway, &nh->fib_nh_gw6))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4495) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4496)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4497) /* Further clean up cached routes in exception table.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4498) * This is needed because cached route may have a different
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4499) * gateway than its 'parent' in the case of an ip redirect.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4500) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4501) fib6_nh_exceptions_clean_tohost(nh, gateway);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4502)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4503) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4504) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4505)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4506) void rt6_clean_tohost(struct net *net, struct in6_addr *gateway)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4507) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4508) fib6_clean_all(net, fib6_clean_tohost, gateway);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4509) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4510)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4511) struct arg_netdev_event {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4512) const struct net_device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4513) union {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4514) unsigned char nh_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4515) unsigned long event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4516) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4517) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4518)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4519) static struct fib6_info *rt6_multipath_first_sibling(const struct fib6_info *rt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4520) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4521) struct fib6_info *iter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4522) struct fib6_node *fn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4523)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4524) fn = rcu_dereference_protected(rt->fib6_node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4525) lockdep_is_held(&rt->fib6_table->tb6_lock));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4526) iter = rcu_dereference_protected(fn->leaf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4527) lockdep_is_held(&rt->fib6_table->tb6_lock));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4528) while (iter) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4529) if (iter->fib6_metric == rt->fib6_metric &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4530) rt6_qualify_for_ecmp(iter))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4531) return iter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4532) iter = rcu_dereference_protected(iter->fib6_next,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4533) lockdep_is_held(&rt->fib6_table->tb6_lock));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4534) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4535)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4536) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4537) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4538)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4539) /* only called for fib entries with builtin fib6_nh */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4540) static bool rt6_is_dead(const struct fib6_info *rt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4541) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4542) if (rt->fib6_nh->fib_nh_flags & RTNH_F_DEAD ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4543) (rt->fib6_nh->fib_nh_flags & RTNH_F_LINKDOWN &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4544) ip6_ignore_linkdown(rt->fib6_nh->fib_nh_dev)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4545) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4546)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4547) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4548) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4549)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4550) static int rt6_multipath_total_weight(const struct fib6_info *rt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4551) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4552) struct fib6_info *iter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4553) int total = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4554)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4555) if (!rt6_is_dead(rt))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4556) total += rt->fib6_nh->fib_nh_weight;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4557)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4558) list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4559) if (!rt6_is_dead(iter))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4560) total += iter->fib6_nh->fib_nh_weight;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4561) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4562)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4563) return total;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4564) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4565)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4566) static void rt6_upper_bound_set(struct fib6_info *rt, int *weight, int total)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4567) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4568) int upper_bound = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4569)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4570) if (!rt6_is_dead(rt)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4571) *weight += rt->fib6_nh->fib_nh_weight;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4572) upper_bound = DIV_ROUND_CLOSEST_ULL((u64) (*weight) << 31,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4573) total) - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4574) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4575) atomic_set(&rt->fib6_nh->fib_nh_upper_bound, upper_bound);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4576) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4577)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4578) static void rt6_multipath_upper_bound_set(struct fib6_info *rt, int total)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4579) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4580) struct fib6_info *iter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4581) int weight = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4582)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4583) rt6_upper_bound_set(rt, &weight, total);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4584)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4585) list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4586) rt6_upper_bound_set(iter, &weight, total);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4587) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4588)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4589) void rt6_multipath_rebalance(struct fib6_info *rt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4590) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4591) struct fib6_info *first;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4592) int total;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4593)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4594) /* In case the entire multipath route was marked for flushing,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4595) * then there is no need to rebalance upon the removal of every
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4596) * sibling route.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4597) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4598) if (!rt->fib6_nsiblings || rt->should_flush)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4599) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4600)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4601) /* During lookup routes are evaluated in order, so we need to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4602) * make sure upper bounds are assigned from the first sibling
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4603) * onwards.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4604) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4605) first = rt6_multipath_first_sibling(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4606) if (WARN_ON_ONCE(!first))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4607) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4608)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4609) total = rt6_multipath_total_weight(first);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4610) rt6_multipath_upper_bound_set(first, total);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4611) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4612)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4613) static int fib6_ifup(struct fib6_info *rt, void *p_arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4614) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4615) const struct arg_netdev_event *arg = p_arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4616) struct net *net = dev_net(arg->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4617)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4618) if (rt != net->ipv6.fib6_null_entry && !rt->nh &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4619) rt->fib6_nh->fib_nh_dev == arg->dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4620) rt->fib6_nh->fib_nh_flags &= ~arg->nh_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4621) fib6_update_sernum_upto_root(net, rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4622) rt6_multipath_rebalance(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4623) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4624)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4625) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4626) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4627)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4628) void rt6_sync_up(struct net_device *dev, unsigned char nh_flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4629) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4630) struct arg_netdev_event arg = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4631) .dev = dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4632) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4633) .nh_flags = nh_flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4634) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4635) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4636)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4637) if (nh_flags & RTNH_F_DEAD && netif_carrier_ok(dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4638) arg.nh_flags |= RTNH_F_LINKDOWN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4639)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4640) fib6_clean_all(dev_net(dev), fib6_ifup, &arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4641) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4642)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4643) /* only called for fib entries with inline fib6_nh */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4644) static bool rt6_multipath_uses_dev(const struct fib6_info *rt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4645) const struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4646) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4647) struct fib6_info *iter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4648)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4649) if (rt->fib6_nh->fib_nh_dev == dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4650) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4651) list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4652) if (iter->fib6_nh->fib_nh_dev == dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4653) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4654)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4655) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4656) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4657)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4658) static void rt6_multipath_flush(struct fib6_info *rt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4659) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4660) struct fib6_info *iter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4661)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4662) rt->should_flush = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4663) list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4664) iter->should_flush = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4665) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4666)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4667) static unsigned int rt6_multipath_dead_count(const struct fib6_info *rt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4668) const struct net_device *down_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4669) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4670) struct fib6_info *iter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4671) unsigned int dead = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4672)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4673) if (rt->fib6_nh->fib_nh_dev == down_dev ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4674) rt->fib6_nh->fib_nh_flags & RTNH_F_DEAD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4675) dead++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4676) list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4677) if (iter->fib6_nh->fib_nh_dev == down_dev ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4678) iter->fib6_nh->fib_nh_flags & RTNH_F_DEAD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4679) dead++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4680)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4681) return dead;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4682) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4683)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4684) static void rt6_multipath_nh_flags_set(struct fib6_info *rt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4685) const struct net_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4686) unsigned char nh_flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4687) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4688) struct fib6_info *iter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4689)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4690) if (rt->fib6_nh->fib_nh_dev == dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4691) rt->fib6_nh->fib_nh_flags |= nh_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4692) list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4693) if (iter->fib6_nh->fib_nh_dev == dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4694) iter->fib6_nh->fib_nh_flags |= nh_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4695) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4696)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4697) /* called with write lock held for table with rt */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4698) static int fib6_ifdown(struct fib6_info *rt, void *p_arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4699) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4700) const struct arg_netdev_event *arg = p_arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4701) const struct net_device *dev = arg->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4702) struct net *net = dev_net(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4703)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4704) if (rt == net->ipv6.fib6_null_entry || rt->nh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4705) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4706)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4707) switch (arg->event) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4708) case NETDEV_UNREGISTER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4709) return rt->fib6_nh->fib_nh_dev == dev ? -1 : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4710) case NETDEV_DOWN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4711) if (rt->should_flush)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4712) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4713) if (!rt->fib6_nsiblings)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4714) return rt->fib6_nh->fib_nh_dev == dev ? -1 : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4715) if (rt6_multipath_uses_dev(rt, dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4716) unsigned int count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4717)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4718) count = rt6_multipath_dead_count(rt, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4719) if (rt->fib6_nsiblings + 1 == count) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4720) rt6_multipath_flush(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4721) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4722) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4723) rt6_multipath_nh_flags_set(rt, dev, RTNH_F_DEAD |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4724) RTNH_F_LINKDOWN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4725) fib6_update_sernum(net, rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4726) rt6_multipath_rebalance(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4727) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4728) return -2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4729) case NETDEV_CHANGE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4730) if (rt->fib6_nh->fib_nh_dev != dev ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4731) rt->fib6_flags & (RTF_LOCAL | RTF_ANYCAST))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4732) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4733) rt->fib6_nh->fib_nh_flags |= RTNH_F_LINKDOWN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4734) rt6_multipath_rebalance(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4735) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4736) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4737)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4738) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4739) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4740)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4741) void rt6_sync_down_dev(struct net_device *dev, unsigned long event)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4742) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4743) struct arg_netdev_event arg = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4744) .dev = dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4745) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4746) .event = event,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4747) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4748) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4749) struct net *net = dev_net(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4750)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4751) if (net->ipv6.sysctl.skip_notify_on_dev_down)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4752) fib6_clean_all_skip_notify(net, fib6_ifdown, &arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4753) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4754) fib6_clean_all(net, fib6_ifdown, &arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4755) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4756)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4757) void rt6_disable_ip(struct net_device *dev, unsigned long event)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4758) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4759) rt6_sync_down_dev(dev, event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4760) rt6_uncached_list_flush_dev(dev_net(dev), dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4761) neigh_ifdown(&nd_tbl, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4762) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4763)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4764) struct rt6_mtu_change_arg {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4765) struct net_device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4766) unsigned int mtu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4767) struct fib6_info *f6i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4768) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4769)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4770) static int fib6_nh_mtu_change(struct fib6_nh *nh, void *_arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4771) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4772) struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *)_arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4773) struct fib6_info *f6i = arg->f6i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4774)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4775) /* For administrative MTU increase, there is no way to discover
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4776) * IPv6 PMTU increase, so PMTU increase should be updated here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4777) * Since RFC 1981 doesn't include administrative MTU increase
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4778) * update PMTU increase is a MUST. (i.e. jumbo frame)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4779) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4780) if (nh->fib_nh_dev == arg->dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4781) struct inet6_dev *idev = __in6_dev_get(arg->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4782) u32 mtu = f6i->fib6_pmtu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4783)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4784) if (mtu >= arg->mtu ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4785) (mtu < arg->mtu && mtu == idev->cnf.mtu6))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4786) fib6_metric_set(f6i, RTAX_MTU, arg->mtu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4787)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4788) spin_lock_bh(&rt6_exception_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4789) rt6_exceptions_update_pmtu(idev, nh, arg->mtu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4790) spin_unlock_bh(&rt6_exception_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4791) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4792)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4793) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4794) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4795)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4796) static int rt6_mtu_change_route(struct fib6_info *f6i, void *p_arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4797) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4798) struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4799) struct inet6_dev *idev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4800)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4801) /* In IPv6 pmtu discovery is not optional,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4802) so that RTAX_MTU lock cannot disable it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4803) We still use this lock to block changes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4804) caused by addrconf/ndisc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4805) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4806)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4807) idev = __in6_dev_get(arg->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4808) if (!idev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4809) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4810)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4811) if (fib6_metric_locked(f6i, RTAX_MTU))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4812) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4813)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4814) arg->f6i = f6i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4815) if (f6i->nh) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4816) /* fib6_nh_mtu_change only returns 0, so this is safe */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4817) return nexthop_for_each_fib6_nh(f6i->nh, fib6_nh_mtu_change,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4818) arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4819) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4820)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4821) return fib6_nh_mtu_change(f6i->fib6_nh, arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4822) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4823)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4824) void rt6_mtu_change(struct net_device *dev, unsigned int mtu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4825) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4826) struct rt6_mtu_change_arg arg = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4827) .dev = dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4828) .mtu = mtu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4829) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4830)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4831) fib6_clean_all(dev_net(dev), rt6_mtu_change_route, &arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4832) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4833)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4834) static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4835) [RTA_UNSPEC] = { .strict_start_type = RTA_DPORT + 1 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4836) [RTA_GATEWAY] = { .len = sizeof(struct in6_addr) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4837) [RTA_PREFSRC] = { .len = sizeof(struct in6_addr) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4838) [RTA_OIF] = { .type = NLA_U32 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4839) [RTA_IIF] = { .type = NLA_U32 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4840) [RTA_PRIORITY] = { .type = NLA_U32 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4841) [RTA_METRICS] = { .type = NLA_NESTED },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4842) [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4843) [RTA_PREF] = { .type = NLA_U8 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4844) [RTA_ENCAP_TYPE] = { .type = NLA_U16 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4845) [RTA_ENCAP] = { .type = NLA_NESTED },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4846) [RTA_EXPIRES] = { .type = NLA_U32 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4847) [RTA_UID] = { .type = NLA_U32 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4848) [RTA_MARK] = { .type = NLA_U32 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4849) [RTA_TABLE] = { .type = NLA_U32 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4850) [RTA_IP_PROTO] = { .type = NLA_U8 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4851) [RTA_SPORT] = { .type = NLA_U16 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4852) [RTA_DPORT] = { .type = NLA_U16 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4853) [RTA_NH_ID] = { .type = NLA_U32 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4854) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4855)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4856) static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4857) struct fib6_config *cfg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4858) struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4859) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4860) struct rtmsg *rtm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4861) struct nlattr *tb[RTA_MAX+1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4862) unsigned int pref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4863) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4864)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4865) err = nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4866) rtm_ipv6_policy, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4867) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4868) goto errout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4869)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4870) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4871) rtm = nlmsg_data(nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4872)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4873) *cfg = (struct fib6_config){
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4874) .fc_table = rtm->rtm_table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4875) .fc_dst_len = rtm->rtm_dst_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4876) .fc_src_len = rtm->rtm_src_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4877) .fc_flags = RTF_UP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4878) .fc_protocol = rtm->rtm_protocol,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4879) .fc_type = rtm->rtm_type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4880)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4881) .fc_nlinfo.portid = NETLINK_CB(skb).portid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4882) .fc_nlinfo.nlh = nlh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4883) .fc_nlinfo.nl_net = sock_net(skb->sk),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4884) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4885)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4886) if (rtm->rtm_type == RTN_UNREACHABLE ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4887) rtm->rtm_type == RTN_BLACKHOLE ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4888) rtm->rtm_type == RTN_PROHIBIT ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4889) rtm->rtm_type == RTN_THROW)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4890) cfg->fc_flags |= RTF_REJECT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4891)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4892) if (rtm->rtm_type == RTN_LOCAL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4893) cfg->fc_flags |= RTF_LOCAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4894)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4895) if (rtm->rtm_flags & RTM_F_CLONED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4896) cfg->fc_flags |= RTF_CACHE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4897)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4898) cfg->fc_flags |= (rtm->rtm_flags & RTNH_F_ONLINK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4899)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4900) if (tb[RTA_NH_ID]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4901) if (tb[RTA_GATEWAY] || tb[RTA_OIF] ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4902) tb[RTA_MULTIPATH] || tb[RTA_ENCAP]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4903) NL_SET_ERR_MSG(extack,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4904) "Nexthop specification and nexthop id are mutually exclusive");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4905) goto errout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4906) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4907) cfg->fc_nh_id = nla_get_u32(tb[RTA_NH_ID]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4908) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4909)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4910) if (tb[RTA_GATEWAY]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4911) cfg->fc_gateway = nla_get_in6_addr(tb[RTA_GATEWAY]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4912) cfg->fc_flags |= RTF_GATEWAY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4913) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4914) if (tb[RTA_VIA]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4915) NL_SET_ERR_MSG(extack, "IPv6 does not support RTA_VIA attribute");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4916) goto errout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4917) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4918)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4919) if (tb[RTA_DST]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4920) int plen = (rtm->rtm_dst_len + 7) >> 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4921)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4922) if (nla_len(tb[RTA_DST]) < plen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4923) goto errout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4924)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4925) nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4926) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4927)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4928) if (tb[RTA_SRC]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4929) int plen = (rtm->rtm_src_len + 7) >> 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4930)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4931) if (nla_len(tb[RTA_SRC]) < plen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4932) goto errout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4933)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4934) nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4935) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4936)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4937) if (tb[RTA_PREFSRC])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4938) cfg->fc_prefsrc = nla_get_in6_addr(tb[RTA_PREFSRC]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4939)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4940) if (tb[RTA_OIF])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4941) cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4942)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4943) if (tb[RTA_PRIORITY])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4944) cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4945)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4946) if (tb[RTA_METRICS]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4947) cfg->fc_mx = nla_data(tb[RTA_METRICS]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4948) cfg->fc_mx_len = nla_len(tb[RTA_METRICS]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4949) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4950)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4951) if (tb[RTA_TABLE])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4952) cfg->fc_table = nla_get_u32(tb[RTA_TABLE]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4953)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4954) if (tb[RTA_MULTIPATH]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4955) cfg->fc_mp = nla_data(tb[RTA_MULTIPATH]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4956) cfg->fc_mp_len = nla_len(tb[RTA_MULTIPATH]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4957)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4958) err = lwtunnel_valid_encap_type_attr(cfg->fc_mp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4959) cfg->fc_mp_len, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4960) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4961) goto errout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4962) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4963)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4964) if (tb[RTA_PREF]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4965) pref = nla_get_u8(tb[RTA_PREF]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4966) if (pref != ICMPV6_ROUTER_PREF_LOW &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4967) pref != ICMPV6_ROUTER_PREF_HIGH)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4968) pref = ICMPV6_ROUTER_PREF_MEDIUM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4969) cfg->fc_flags |= RTF_PREF(pref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4970) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4971)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4972) if (tb[RTA_ENCAP])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4973) cfg->fc_encap = tb[RTA_ENCAP];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4974)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4975) if (tb[RTA_ENCAP_TYPE]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4976) cfg->fc_encap_type = nla_get_u16(tb[RTA_ENCAP_TYPE]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4977)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4978) err = lwtunnel_valid_encap_type(cfg->fc_encap_type, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4979) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4980) goto errout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4981) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4982)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4983) if (tb[RTA_EXPIRES]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4984) unsigned long timeout = addrconf_timeout_fixup(nla_get_u32(tb[RTA_EXPIRES]), HZ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4985)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4986) if (addrconf_finite_timeout(timeout)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4987) cfg->fc_expires = jiffies_to_clock_t(timeout * HZ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4988) cfg->fc_flags |= RTF_EXPIRES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4989) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4990) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4991)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4992) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4993) errout:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4994) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4995) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4996)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4997) struct rt6_nh {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4998) struct fib6_info *fib6_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4999) struct fib6_config r_cfg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5000) struct list_head next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5001) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5002)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5003) static int ip6_route_info_append(struct net *net,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5004) struct list_head *rt6_nh_list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5005) struct fib6_info *rt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5006) struct fib6_config *r_cfg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5007) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5008) struct rt6_nh *nh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5009) int err = -EEXIST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5010)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5011) list_for_each_entry(nh, rt6_nh_list, next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5012) /* check if fib6_info already exists */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5013) if (rt6_duplicate_nexthop(nh->fib6_info, rt))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5014) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5015) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5016)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5017) nh = kzalloc(sizeof(*nh), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5018) if (!nh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5019) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5020) nh->fib6_info = rt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5021) memcpy(&nh->r_cfg, r_cfg, sizeof(*r_cfg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5022) list_add_tail(&nh->next, rt6_nh_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5023)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5024) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5025) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5026)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5027) static void ip6_route_mpath_notify(struct fib6_info *rt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5028) struct fib6_info *rt_last,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5029) struct nl_info *info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5030) __u16 nlflags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5031) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5032) /* if this is an APPEND route, then rt points to the first route
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5033) * inserted and rt_last points to last route inserted. Userspace
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5034) * wants a consistent dump of the route which starts at the first
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5035) * nexthop. Since sibling routes are always added at the end of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5036) * the list, find the first sibling of the last route appended
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5037) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5038) if ((nlflags & NLM_F_APPEND) && rt_last && rt_last->fib6_nsiblings) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5039) rt = list_first_entry(&rt_last->fib6_siblings,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5040) struct fib6_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5041) fib6_siblings);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5042) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5043)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5044) if (rt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5045) inet6_rt_notify(RTM_NEWROUTE, rt, info, nlflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5046) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5047)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5048) static bool ip6_route_mpath_should_notify(const struct fib6_info *rt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5049) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5050) bool rt_can_ecmp = rt6_qualify_for_ecmp(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5051) bool should_notify = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5052) struct fib6_info *leaf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5053) struct fib6_node *fn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5054)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5055) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5056) fn = rcu_dereference(rt->fib6_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5057) if (!fn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5058) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5059)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5060) leaf = rcu_dereference(fn->leaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5061) if (!leaf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5062) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5063)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5064) if (rt == leaf ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5065) (rt_can_ecmp && rt->fib6_metric == leaf->fib6_metric &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5066) rt6_qualify_for_ecmp(leaf)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5067) should_notify = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5068) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5069) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5070)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5071) return should_notify;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5072) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5073)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5074) static int fib6_gw_from_attr(struct in6_addr *gw, struct nlattr *nla,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5075) struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5076) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5077) if (nla_len(nla) < sizeof(*gw)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5078) NL_SET_ERR_MSG(extack, "Invalid IPv6 address in RTA_GATEWAY");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5079) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5080) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5081)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5082) *gw = nla_get_in6_addr(nla);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5083)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5084) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5085) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5086)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5087) static int ip6_route_multipath_add(struct fib6_config *cfg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5088) struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5089) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5090) struct fib6_info *rt_notif = NULL, *rt_last = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5091) struct nl_info *info = &cfg->fc_nlinfo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5092) struct fib6_config r_cfg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5093) struct rtnexthop *rtnh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5094) struct fib6_info *rt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5095) struct rt6_nh *err_nh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5096) struct rt6_nh *nh, *nh_safe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5097) __u16 nlflags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5098) int remaining;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5099) int attrlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5100) int err = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5101) int nhn = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5102) int replace = (cfg->fc_nlinfo.nlh &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5103) (cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_REPLACE));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5104) LIST_HEAD(rt6_nh_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5106) nlflags = replace ? NLM_F_REPLACE : NLM_F_CREATE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5107) if (info->nlh && info->nlh->nlmsg_flags & NLM_F_APPEND)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5108) nlflags |= NLM_F_APPEND;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5110) remaining = cfg->fc_mp_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5111) rtnh = (struct rtnexthop *)cfg->fc_mp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5113) /* Parse a Multipath Entry and build a list (rt6_nh_list) of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5114) * fib6_info structs per nexthop
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5115) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5116) while (rtnh_ok(rtnh, remaining)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5117) memcpy(&r_cfg, cfg, sizeof(*cfg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5118) if (rtnh->rtnh_ifindex)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5119) r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5121) attrlen = rtnh_attrlen(rtnh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5122) if (attrlen > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5123) struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5125) nla = nla_find(attrs, attrlen, RTA_GATEWAY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5126) if (nla) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5127) err = fib6_gw_from_attr(&r_cfg.fc_gateway, nla,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5128) extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5129) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5130) goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5132) r_cfg.fc_flags |= RTF_GATEWAY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5133) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5134) r_cfg.fc_encap = nla_find(attrs, attrlen, RTA_ENCAP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5136) /* RTA_ENCAP_TYPE length checked in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5137) * lwtunnel_valid_encap_type_attr
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5138) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5139) nla = nla_find(attrs, attrlen, RTA_ENCAP_TYPE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5140) if (nla)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5141) r_cfg.fc_encap_type = nla_get_u16(nla);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5142) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5144) r_cfg.fc_flags |= (rtnh->rtnh_flags & RTNH_F_ONLINK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5145) rt = ip6_route_info_create(&r_cfg, GFP_KERNEL, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5146) if (IS_ERR(rt)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5147) err = PTR_ERR(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5148) rt = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5149) goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5150) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5151) if (!rt6_qualify_for_ecmp(rt)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5152) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5153) NL_SET_ERR_MSG(extack,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5154) "Device only routes can not be added for IPv6 using the multipath API.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5155) fib6_info_release(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5156) goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5157) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5159) rt->fib6_nh->fib_nh_weight = rtnh->rtnh_hops + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5160)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5161) err = ip6_route_info_append(info->nl_net, &rt6_nh_list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5162) rt, &r_cfg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5163) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5164) fib6_info_release(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5165) goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5166) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5167)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5168) rtnh = rtnh_next(rtnh, &remaining);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5169) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5171) if (list_empty(&rt6_nh_list)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5172) NL_SET_ERR_MSG(extack,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5173) "Invalid nexthop configuration - no valid nexthops");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5174) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5175) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5177) /* for add and replace send one notification with all nexthops.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5178) * Skip the notification in fib6_add_rt2node and send one with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5179) * the full route when done
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5180) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5181) info->skip_notify = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5182)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5183) /* For add and replace, send one notification with all nexthops. For
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5184) * append, send one notification with all appended nexthops.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5185) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5186) info->skip_notify_kernel = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5187)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5188) err_nh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5189) list_for_each_entry(nh, &rt6_nh_list, next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5190) err = __ip6_ins_rt(nh->fib6_info, info, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5191) fib6_info_release(nh->fib6_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5193) if (!err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5194) /* save reference to last route successfully inserted */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5195) rt_last = nh->fib6_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5196)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5197) /* save reference to first route for notification */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5198) if (!rt_notif)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5199) rt_notif = nh->fib6_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5200) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5201)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5202) /* nh->fib6_info is used or freed at this point, reset to NULL*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5203) nh->fib6_info = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5204) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5205) if (replace && nhn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5206) NL_SET_ERR_MSG_MOD(extack,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5207) "multipath route replace failed (check consistency of installed routes)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5208) err_nh = nh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5209) goto add_errout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5210) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5211)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5212) /* Because each route is added like a single route we remove
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5213) * these flags after the first nexthop: if there is a collision,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5214) * we have already failed to add the first nexthop:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5215) * fib6_add_rt2node() has rejected it; when replacing, old
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5216) * nexthops have been replaced by first new, the rest should
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5217) * be added to it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5218) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5219) if (cfg->fc_nlinfo.nlh) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5220) cfg->fc_nlinfo.nlh->nlmsg_flags &= ~(NLM_F_EXCL |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5221) NLM_F_REPLACE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5222) cfg->fc_nlinfo.nlh->nlmsg_flags |= NLM_F_CREATE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5223) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5224) nhn++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5225) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5226)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5227) /* An in-kernel notification should only be sent in case the new
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5228) * multipath route is added as the first route in the node, or if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5229) * it was appended to it. We pass 'rt_notif' since it is the first
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5230) * sibling and might allow us to skip some checks in the replace case.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5231) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5232) if (ip6_route_mpath_should_notify(rt_notif)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5233) enum fib_event_type fib_event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5234)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5235) if (rt_notif->fib6_nsiblings != nhn - 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5236) fib_event = FIB_EVENT_ENTRY_APPEND;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5237) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5238) fib_event = FIB_EVENT_ENTRY_REPLACE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5239)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5240) err = call_fib6_multipath_entry_notifiers(info->nl_net,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5241) fib_event, rt_notif,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5242) nhn - 1, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5243) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5244) /* Delete all the siblings that were just added */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5245) err_nh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5246) goto add_errout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5247) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5248) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5249)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5250) /* success ... tell user about new route */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5251) ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5252) goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5253)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5254) add_errout:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5255) /* send notification for routes that were added so that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5256) * the delete notifications sent by ip6_route_del are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5257) * coherent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5258) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5259) if (rt_notif)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5260) ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5261)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5262) /* Delete routes that were already added */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5263) list_for_each_entry(nh, &rt6_nh_list, next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5264) if (err_nh == nh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5265) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5266) ip6_route_del(&nh->r_cfg, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5267) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5268)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5269) cleanup:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5270) list_for_each_entry_safe(nh, nh_safe, &rt6_nh_list, next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5271) if (nh->fib6_info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5272) fib6_info_release(nh->fib6_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5273) list_del(&nh->next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5274) kfree(nh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5275) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5276)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5277) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5278) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5279)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5280) static int ip6_route_multipath_del(struct fib6_config *cfg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5281) struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5282) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5283) struct fib6_config r_cfg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5284) struct rtnexthop *rtnh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5285) int last_err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5286) int remaining;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5287) int attrlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5288) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5289)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5290) remaining = cfg->fc_mp_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5291) rtnh = (struct rtnexthop *)cfg->fc_mp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5292)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5293) /* Parse a Multipath Entry */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5294) while (rtnh_ok(rtnh, remaining)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5295) memcpy(&r_cfg, cfg, sizeof(*cfg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5296) if (rtnh->rtnh_ifindex)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5297) r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5298)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5299) attrlen = rtnh_attrlen(rtnh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5300) if (attrlen > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5301) struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5302)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5303) nla = nla_find(attrs, attrlen, RTA_GATEWAY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5304) if (nla) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5305) err = fib6_gw_from_attr(&r_cfg.fc_gateway, nla,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5306) extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5307) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5308) last_err = err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5309) goto next_rtnh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5310) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5311)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5312) r_cfg.fc_flags |= RTF_GATEWAY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5313) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5314) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5315) err = ip6_route_del(&r_cfg, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5316) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5317) last_err = err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5318)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5319) next_rtnh:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5320) rtnh = rtnh_next(rtnh, &remaining);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5321) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5322)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5323) return last_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5324) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5325)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5326) static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5327) struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5328) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5329) struct fib6_config cfg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5330) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5331)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5332) err = rtm_to_fib6_config(skb, nlh, &cfg, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5333) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5334) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5335)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5336) if (cfg.fc_nh_id &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5337) !nexthop_find_by_id(sock_net(skb->sk), cfg.fc_nh_id)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5338) NL_SET_ERR_MSG(extack, "Nexthop id does not exist");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5339) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5340) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5341)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5342) if (cfg.fc_mp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5343) return ip6_route_multipath_del(&cfg, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5344) else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5345) cfg.fc_delete_all_nh = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5346) return ip6_route_del(&cfg, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5347) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5348) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5349)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5350) static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5351) struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5352) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5353) struct fib6_config cfg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5354) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5355)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5356) err = rtm_to_fib6_config(skb, nlh, &cfg, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5357) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5358) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5359)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5360) if (cfg.fc_metric == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5361) cfg.fc_metric = IP6_RT_PRIO_USER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5362)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5363) if (cfg.fc_mp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5364) return ip6_route_multipath_add(&cfg, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5365) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5366) return ip6_route_add(&cfg, GFP_KERNEL, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5367) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5368)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5369) /* add the overhead of this fib6_nh to nexthop_len */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5370) static int rt6_nh_nlmsg_size(struct fib6_nh *nh, void *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5371) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5372) int *nexthop_len = arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5373)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5374) *nexthop_len += nla_total_size(0) /* RTA_MULTIPATH */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5375) + NLA_ALIGN(sizeof(struct rtnexthop))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5376) + nla_total_size(16); /* RTA_GATEWAY */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5377)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5378) if (nh->fib_nh_lws) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5379) /* RTA_ENCAP_TYPE */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5380) *nexthop_len += lwtunnel_get_encap_size(nh->fib_nh_lws);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5381) /* RTA_ENCAP */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5382) *nexthop_len += nla_total_size(2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5383) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5384)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5385) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5386) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5387)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5388) static size_t rt6_nlmsg_size(struct fib6_info *f6i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5389) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5390) int nexthop_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5391)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5392) if (f6i->nh) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5393) nexthop_len = nla_total_size(4); /* RTA_NH_ID */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5394) nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_nlmsg_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5395) &nexthop_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5396) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5397) struct fib6_nh *nh = f6i->fib6_nh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5398)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5399) nexthop_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5400) if (f6i->fib6_nsiblings) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5401) nexthop_len = nla_total_size(0) /* RTA_MULTIPATH */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5402) + NLA_ALIGN(sizeof(struct rtnexthop))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5403) + nla_total_size(16) /* RTA_GATEWAY */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5404) + lwtunnel_get_encap_size(nh->fib_nh_lws);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5405)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5406) nexthop_len *= f6i->fib6_nsiblings;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5407) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5408) nexthop_len += lwtunnel_get_encap_size(nh->fib_nh_lws);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5409) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5410)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5411) return NLMSG_ALIGN(sizeof(struct rtmsg))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5412) + nla_total_size(16) /* RTA_SRC */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5413) + nla_total_size(16) /* RTA_DST */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5414) + nla_total_size(16) /* RTA_GATEWAY */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5415) + nla_total_size(16) /* RTA_PREFSRC */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5416) + nla_total_size(4) /* RTA_TABLE */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5417) + nla_total_size(4) /* RTA_IIF */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5418) + nla_total_size(4) /* RTA_OIF */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5419) + nla_total_size(4) /* RTA_PRIORITY */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5420) + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5421) + nla_total_size(sizeof(struct rta_cacheinfo))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5422) + nla_total_size(TCP_CA_NAME_MAX) /* RTAX_CC_ALGO */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5423) + nla_total_size(1) /* RTA_PREF */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5424) + nexthop_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5425) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5426)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5427) static int rt6_fill_node_nexthop(struct sk_buff *skb, struct nexthop *nh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5428) unsigned char *flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5429) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5430) if (nexthop_is_multipath(nh)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5431) struct nlattr *mp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5432)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5433) mp = nla_nest_start_noflag(skb, RTA_MULTIPATH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5434) if (!mp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5435) goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5436)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5437) if (nexthop_mpath_fill_node(skb, nh, AF_INET6))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5438) goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5439)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5440) nla_nest_end(skb, mp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5441) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5442) struct fib6_nh *fib6_nh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5443)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5444) fib6_nh = nexthop_fib6_nh(nh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5445) if (fib_nexthop_info(skb, &fib6_nh->nh_common, AF_INET6,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5446) flags, false) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5447) goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5448) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5449)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5450) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5451)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5452) nla_put_failure:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5453) return -EMSGSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5454) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5455)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5456) static int rt6_fill_node(struct net *net, struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5457) struct fib6_info *rt, struct dst_entry *dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5458) struct in6_addr *dest, struct in6_addr *src,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5459) int iif, int type, u32 portid, u32 seq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5460) unsigned int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5461) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5462) struct rt6_info *rt6 = (struct rt6_info *)dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5463) struct rt6key *rt6_dst, *rt6_src;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5464) u32 *pmetrics, table, rt6_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5465) unsigned char nh_flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5466) struct nlmsghdr *nlh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5467) struct rtmsg *rtm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5468) long expires = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5469)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5470) nlh = nlmsg_put(skb, portid, seq, type, sizeof(*rtm), flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5471) if (!nlh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5472) return -EMSGSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5473)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5474) if (rt6) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5475) rt6_dst = &rt6->rt6i_dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5476) rt6_src = &rt6->rt6i_src;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5477) rt6_flags = rt6->rt6i_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5478) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5479) rt6_dst = &rt->fib6_dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5480) rt6_src = &rt->fib6_src;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5481) rt6_flags = rt->fib6_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5482) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5483)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5484) rtm = nlmsg_data(nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5485) rtm->rtm_family = AF_INET6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5486) rtm->rtm_dst_len = rt6_dst->plen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5487) rtm->rtm_src_len = rt6_src->plen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5488) rtm->rtm_tos = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5489) if (rt->fib6_table)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5490) table = rt->fib6_table->tb6_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5491) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5492) table = RT6_TABLE_UNSPEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5493) rtm->rtm_table = table < 256 ? table : RT_TABLE_COMPAT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5494) if (nla_put_u32(skb, RTA_TABLE, table))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5495) goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5496)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5497) rtm->rtm_type = rt->fib6_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5498) rtm->rtm_flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5499) rtm->rtm_scope = RT_SCOPE_UNIVERSE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5500) rtm->rtm_protocol = rt->fib6_protocol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5501)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5502) if (rt6_flags & RTF_CACHE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5503) rtm->rtm_flags |= RTM_F_CLONED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5504)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5505) if (dest) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5506) if (nla_put_in6_addr(skb, RTA_DST, dest))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5507) goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5508) rtm->rtm_dst_len = 128;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5509) } else if (rtm->rtm_dst_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5510) if (nla_put_in6_addr(skb, RTA_DST, &rt6_dst->addr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5511) goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5512) #ifdef CONFIG_IPV6_SUBTREES
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5513) if (src) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5514) if (nla_put_in6_addr(skb, RTA_SRC, src))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5515) goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5516) rtm->rtm_src_len = 128;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5517) } else if (rtm->rtm_src_len &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5518) nla_put_in6_addr(skb, RTA_SRC, &rt6_src->addr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5519) goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5520) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5521) if (iif) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5522) #ifdef CONFIG_IPV6_MROUTE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5523) if (ipv6_addr_is_multicast(&rt6_dst->addr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5524) int err = ip6mr_get_route(net, skb, rtm, portid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5525)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5526) if (err == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5527) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5528) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5529) goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5530) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5531) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5532) if (nla_put_u32(skb, RTA_IIF, iif))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5533) goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5534) } else if (dest) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5535) struct in6_addr saddr_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5536) if (ip6_route_get_saddr(net, rt, dest, 0, &saddr_buf) == 0 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5537) nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5538) goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5539) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5540)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5541) if (rt->fib6_prefsrc.plen) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5542) struct in6_addr saddr_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5543) saddr_buf = rt->fib6_prefsrc.addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5544) if (nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5545) goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5546) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5547)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5548) pmetrics = dst ? dst_metrics_ptr(dst) : rt->fib6_metrics->metrics;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5549) if (rtnetlink_put_metrics(skb, pmetrics) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5550) goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5551)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5552) if (nla_put_u32(skb, RTA_PRIORITY, rt->fib6_metric))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5553) goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5554)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5555) /* For multipath routes, walk the siblings list and add
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5556) * each as a nexthop within RTA_MULTIPATH.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5557) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5558) if (rt6) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5559) if (rt6_flags & RTF_GATEWAY &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5560) nla_put_in6_addr(skb, RTA_GATEWAY, &rt6->rt6i_gateway))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5561) goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5562)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5563) if (dst->dev && nla_put_u32(skb, RTA_OIF, dst->dev->ifindex))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5564) goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5565) } else if (rt->fib6_nsiblings) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5566) struct fib6_info *sibling, *next_sibling;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5567) struct nlattr *mp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5568)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5569) mp = nla_nest_start_noflag(skb, RTA_MULTIPATH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5570) if (!mp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5571) goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5572)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5573) if (fib_add_nexthop(skb, &rt->fib6_nh->nh_common,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5574) rt->fib6_nh->fib_nh_weight, AF_INET6,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5575) 0) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5576) goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5577)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5578) list_for_each_entry_safe(sibling, next_sibling,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5579) &rt->fib6_siblings, fib6_siblings) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5580) if (fib_add_nexthop(skb, &sibling->fib6_nh->nh_common,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5581) sibling->fib6_nh->fib_nh_weight,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5582) AF_INET6, 0) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5583) goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5584) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5585)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5586) nla_nest_end(skb, mp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5587) } else if (rt->nh) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5588) if (nla_put_u32(skb, RTA_NH_ID, rt->nh->id))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5589) goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5590)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5591) if (nexthop_is_blackhole(rt->nh))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5592) rtm->rtm_type = RTN_BLACKHOLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5593)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5594) if (net->ipv4.sysctl_nexthop_compat_mode &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5595) rt6_fill_node_nexthop(skb, rt->nh, &nh_flags) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5596) goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5597)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5598) rtm->rtm_flags |= nh_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5599) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5600) if (fib_nexthop_info(skb, &rt->fib6_nh->nh_common, AF_INET6,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5601) &nh_flags, false) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5602) goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5603)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5604) rtm->rtm_flags |= nh_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5605) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5606)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5607) if (rt6_flags & RTF_EXPIRES) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5608) expires = dst ? dst->expires : rt->expires;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5609) expires -= jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5610) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5611)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5612) if (!dst) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5613) if (rt->offload)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5614) rtm->rtm_flags |= RTM_F_OFFLOAD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5615) if (rt->trap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5616) rtm->rtm_flags |= RTM_F_TRAP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5617) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5618)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5619) if (rtnl_put_cacheinfo(skb, dst, 0, expires, dst ? dst->error : 0) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5620) goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5621)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5622) if (nla_put_u8(skb, RTA_PREF, IPV6_EXTRACT_PREF(rt6_flags)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5623) goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5624)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5625)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5626) nlmsg_end(skb, nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5627) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5628)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5629) nla_put_failure:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5630) nlmsg_cancel(skb, nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5631) return -EMSGSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5632) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5633)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5634) static int fib6_info_nh_uses_dev(struct fib6_nh *nh, void *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5635) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5636) const struct net_device *dev = arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5637)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5638) if (nh->fib_nh_dev == dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5639) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5640)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5641) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5642) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5643)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5644) static bool fib6_info_uses_dev(const struct fib6_info *f6i,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5645) const struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5646) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5647) if (f6i->nh) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5648) struct net_device *_dev = (struct net_device *)dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5649)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5650) return !!nexthop_for_each_fib6_nh(f6i->nh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5651) fib6_info_nh_uses_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5652) _dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5653) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5654)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5655) if (f6i->fib6_nh->fib_nh_dev == dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5656) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5657)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5658) if (f6i->fib6_nsiblings) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5659) struct fib6_info *sibling, *next_sibling;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5660)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5661) list_for_each_entry_safe(sibling, next_sibling,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5662) &f6i->fib6_siblings, fib6_siblings) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5663) if (sibling->fib6_nh->fib_nh_dev == dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5664) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5665) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5666) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5667)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5668) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5669) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5670)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5671) struct fib6_nh_exception_dump_walker {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5672) struct rt6_rtnl_dump_arg *dump;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5673) struct fib6_info *rt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5674) unsigned int flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5675) unsigned int skip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5676) unsigned int count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5677) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5678)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5679) static int rt6_nh_dump_exceptions(struct fib6_nh *nh, void *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5680) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5681) struct fib6_nh_exception_dump_walker *w = arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5682) struct rt6_rtnl_dump_arg *dump = w->dump;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5683) struct rt6_exception_bucket *bucket;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5684) struct rt6_exception *rt6_ex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5685) int i, err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5686)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5687) bucket = fib6_nh_get_excptn_bucket(nh, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5688) if (!bucket)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5689) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5690)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5691) for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5692) hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5693) if (w->skip) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5694) w->skip--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5695) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5696) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5697)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5698) /* Expiration of entries doesn't bump sernum, insertion
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5699) * does. Removal is triggered by insertion, so we can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5700) * rely on the fact that if entries change between two
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5701) * partial dumps, this node is scanned again completely,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5702) * see rt6_insert_exception() and fib6_dump_table().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5703) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5704) * Count expired entries we go through as handled
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5705) * entries that we'll skip next time, in case of partial
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5706) * node dump. Otherwise, if entries expire meanwhile,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5707) * we'll skip the wrong amount.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5708) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5709) if (rt6_check_expired(rt6_ex->rt6i)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5710) w->count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5711) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5712) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5713)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5714) err = rt6_fill_node(dump->net, dump->skb, w->rt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5715) &rt6_ex->rt6i->dst, NULL, NULL, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5716) RTM_NEWROUTE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5717) NETLINK_CB(dump->cb->skb).portid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5718) dump->cb->nlh->nlmsg_seq, w->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5719) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5720) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5721)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5722) w->count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5723) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5724) bucket++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5725) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5726)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5727) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5728) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5729)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5730) /* Return -1 if done with node, number of handled routes on partial dump */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5731) int rt6_dump_route(struct fib6_info *rt, void *p_arg, unsigned int skip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5732) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5733) struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5734) struct fib_dump_filter *filter = &arg->filter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5735) unsigned int flags = NLM_F_MULTI;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5736) struct net *net = arg->net;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5737) int count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5738)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5739) if (rt == net->ipv6.fib6_null_entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5740) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5741)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5742) if ((filter->flags & RTM_F_PREFIX) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5743) !(rt->fib6_flags & RTF_PREFIX_RT)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5744) /* success since this is not a prefix route */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5745) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5746) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5747) if (filter->filter_set &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5748) ((filter->rt_type && rt->fib6_type != filter->rt_type) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5749) (filter->dev && !fib6_info_uses_dev(rt, filter->dev)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5750) (filter->protocol && rt->fib6_protocol != filter->protocol))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5751) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5752) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5753)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5754) if (filter->filter_set ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5755) !filter->dump_routes || !filter->dump_exceptions) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5756) flags |= NLM_F_DUMP_FILTERED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5757) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5758)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5759) if (filter->dump_routes) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5760) if (skip) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5761) skip--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5762) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5763) if (rt6_fill_node(net, arg->skb, rt, NULL, NULL, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5764) 0, RTM_NEWROUTE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5765) NETLINK_CB(arg->cb->skb).portid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5766) arg->cb->nlh->nlmsg_seq, flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5767) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5768) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5769) count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5770) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5771) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5772)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5773) if (filter->dump_exceptions) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5774) struct fib6_nh_exception_dump_walker w = { .dump = arg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5775) .rt = rt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5776) .flags = flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5777) .skip = skip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5778) .count = 0 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5779) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5780)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5781) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5782) if (rt->nh) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5783) err = nexthop_for_each_fib6_nh(rt->nh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5784) rt6_nh_dump_exceptions,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5785) &w);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5786) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5787) err = rt6_nh_dump_exceptions(rt->fib6_nh, &w);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5788) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5789) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5790)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5791) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5792) return count += w.count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5793) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5794)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5795) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5796) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5797)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5798) static int inet6_rtm_valid_getroute_req(struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5799) const struct nlmsghdr *nlh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5800) struct nlattr **tb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5801) struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5802) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5803) struct rtmsg *rtm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5804) int i, err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5805)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5806) if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*rtm))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5807) NL_SET_ERR_MSG_MOD(extack,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5808) "Invalid header for get route request");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5809) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5810) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5811)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5812) if (!netlink_strict_get_check(skb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5813) return nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5814) rtm_ipv6_policy, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5815)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5816) rtm = nlmsg_data(nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5817) if ((rtm->rtm_src_len && rtm->rtm_src_len != 128) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5818) (rtm->rtm_dst_len && rtm->rtm_dst_len != 128) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5819) rtm->rtm_table || rtm->rtm_protocol || rtm->rtm_scope ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5820) rtm->rtm_type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5821) NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for get route request");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5822) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5823) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5824) if (rtm->rtm_flags & ~RTM_F_FIB_MATCH) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5825) NL_SET_ERR_MSG_MOD(extack,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5826) "Invalid flags for get route request");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5827) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5828) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5829)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5830) err = nlmsg_parse_deprecated_strict(nlh, sizeof(*rtm), tb, RTA_MAX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5831) rtm_ipv6_policy, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5832) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5833) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5834)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5835) if ((tb[RTA_SRC] && !rtm->rtm_src_len) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5836) (tb[RTA_DST] && !rtm->rtm_dst_len)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5837) NL_SET_ERR_MSG_MOD(extack, "rtm_src_len and rtm_dst_len must be 128 for IPv6");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5838) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5839) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5840)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5841) for (i = 0; i <= RTA_MAX; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5842) if (!tb[i])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5843) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5844)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5845) switch (i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5846) case RTA_SRC:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5847) case RTA_DST:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5848) case RTA_IIF:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5849) case RTA_OIF:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5850) case RTA_MARK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5851) case RTA_UID:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5852) case RTA_SPORT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5853) case RTA_DPORT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5854) case RTA_IP_PROTO:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5855) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5856) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5857) NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in get route request");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5858) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5859) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5860) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5861)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5862) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5863) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5864)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5865) static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5866) struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5867) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5868) struct net *net = sock_net(in_skb->sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5869) struct nlattr *tb[RTA_MAX+1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5870) int err, iif = 0, oif = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5871) struct fib6_info *from;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5872) struct dst_entry *dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5873) struct rt6_info *rt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5874) struct sk_buff *skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5875) struct rtmsg *rtm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5876) struct flowi6 fl6 = {};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5877) bool fibmatch;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5878)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5879) err = inet6_rtm_valid_getroute_req(in_skb, nlh, tb, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5880) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5881) goto errout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5882)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5883) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5884) rtm = nlmsg_data(nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5885) fl6.flowlabel = ip6_make_flowinfo(rtm->rtm_tos, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5886) fibmatch = !!(rtm->rtm_flags & RTM_F_FIB_MATCH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5887)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5888) if (tb[RTA_SRC]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5889) if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5890) goto errout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5891)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5892) fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5893) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5894)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5895) if (tb[RTA_DST]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5896) if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5897) goto errout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5898)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5899) fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5900) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5901)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5902) if (tb[RTA_IIF])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5903) iif = nla_get_u32(tb[RTA_IIF]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5904)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5905) if (tb[RTA_OIF])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5906) oif = nla_get_u32(tb[RTA_OIF]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5907)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5908) if (tb[RTA_MARK])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5909) fl6.flowi6_mark = nla_get_u32(tb[RTA_MARK]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5910)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5911) if (tb[RTA_UID])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5912) fl6.flowi6_uid = make_kuid(current_user_ns(),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5913) nla_get_u32(tb[RTA_UID]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5914) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5915) fl6.flowi6_uid = iif ? INVALID_UID : current_uid();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5916)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5917) if (tb[RTA_SPORT])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5918) fl6.fl6_sport = nla_get_be16(tb[RTA_SPORT]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5919)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5920) if (tb[RTA_DPORT])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5921) fl6.fl6_dport = nla_get_be16(tb[RTA_DPORT]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5922)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5923) if (tb[RTA_IP_PROTO]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5924) err = rtm_getroute_parse_ip_proto(tb[RTA_IP_PROTO],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5925) &fl6.flowi6_proto, AF_INET6,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5926) extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5927) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5928) goto errout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5929) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5930)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5931) if (iif) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5932) struct net_device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5933) int flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5934)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5935) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5936)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5937) dev = dev_get_by_index_rcu(net, iif);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5938) if (!dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5939) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5940) err = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5941) goto errout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5942) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5943)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5944) fl6.flowi6_iif = iif;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5945)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5946) if (!ipv6_addr_any(&fl6.saddr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5947) flags |= RT6_LOOKUP_F_HAS_SADDR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5948)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5949) dst = ip6_route_input_lookup(net, dev, &fl6, NULL, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5950)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5951) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5952) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5953) fl6.flowi6_oif = oif;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5954)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5955) dst = ip6_route_output(net, NULL, &fl6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5956) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5957)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5958)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5959) rt = container_of(dst, struct rt6_info, dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5960) if (rt->dst.error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5961) err = rt->dst.error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5962) ip6_rt_put(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5963) goto errout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5964) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5965)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5966) if (rt == net->ipv6.ip6_null_entry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5967) err = rt->dst.error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5968) ip6_rt_put(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5969) goto errout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5970) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5971)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5972) skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5973) if (!skb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5974) ip6_rt_put(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5975) err = -ENOBUFS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5976) goto errout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5977) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5978)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5979) skb_dst_set(skb, &rt->dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5980)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5981) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5982) from = rcu_dereference(rt->from);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5983) if (from) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5984) if (fibmatch)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5985) err = rt6_fill_node(net, skb, from, NULL, NULL, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5986) iif, RTM_NEWROUTE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5987) NETLINK_CB(in_skb).portid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5988) nlh->nlmsg_seq, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5989) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5990) err = rt6_fill_node(net, skb, from, dst, &fl6.daddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5991) &fl6.saddr, iif, RTM_NEWROUTE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5992) NETLINK_CB(in_skb).portid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5993) nlh->nlmsg_seq, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5994) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5995) err = -ENETUNREACH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5996) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5997) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5998)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5999) if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6000) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6001) goto errout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6002) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6003)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6004) err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6005) errout:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6006) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6007) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6008)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6009) void inet6_rt_notify(int event, struct fib6_info *rt, struct nl_info *info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6010) unsigned int nlm_flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6011) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6012) struct sk_buff *skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6013) struct net *net = info->nl_net;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6014) u32 seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6015) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6016)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6017) err = -ENOBUFS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6018) seq = info->nlh ? info->nlh->nlmsg_seq : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6019)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6020) skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6021) if (!skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6022) goto errout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6023)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6024) err = rt6_fill_node(net, skb, rt, NULL, NULL, NULL, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6025) event, info->portid, seq, nlm_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6026) if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6027) /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6028) WARN_ON(err == -EMSGSIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6029) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6030) goto errout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6031) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6032) rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6033) info->nlh, gfp_any());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6034) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6035) errout:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6036) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6037) rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6038) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6039)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6040) void fib6_rt_update(struct net *net, struct fib6_info *rt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6041) struct nl_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6042) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6043) u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6044) struct sk_buff *skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6045) int err = -ENOBUFS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6046)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6047) /* call_fib6_entry_notifiers will be removed when in-kernel notifier
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6048) * is implemented and supported for nexthop objects
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6049) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6050) call_fib6_entry_notifiers(net, FIB_EVENT_ENTRY_REPLACE, rt, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6051)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6052) skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6053) if (!skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6054) goto errout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6055)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6056) err = rt6_fill_node(net, skb, rt, NULL, NULL, NULL, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6057) RTM_NEWROUTE, info->portid, seq, NLM_F_REPLACE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6058) if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6059) /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6060) WARN_ON(err == -EMSGSIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6061) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6062) goto errout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6063) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6064) rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6065) info->nlh, gfp_any());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6066) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6067) errout:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6068) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6069) rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6070) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6071)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6072) static int ip6_route_dev_notify(struct notifier_block *this,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6073) unsigned long event, void *ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6074) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6075) struct net_device *dev = netdev_notifier_info_to_dev(ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6076) struct net *net = dev_net(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6077)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6078) if (!(dev->flags & IFF_LOOPBACK))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6079) return NOTIFY_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6080)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6081) if (event == NETDEV_REGISTER) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6082) net->ipv6.fib6_null_entry->fib6_nh->fib_nh_dev = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6083) net->ipv6.ip6_null_entry->dst.dev = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6084) net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6085) #ifdef CONFIG_IPV6_MULTIPLE_TABLES
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6086) net->ipv6.ip6_prohibit_entry->dst.dev = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6087) net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6088) net->ipv6.ip6_blk_hole_entry->dst.dev = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6089) net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6090) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6091) } else if (event == NETDEV_UNREGISTER &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6092) dev->reg_state != NETREG_UNREGISTERED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6093) /* NETDEV_UNREGISTER could be fired for multiple times by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6094) * netdev_wait_allrefs(). Make sure we only call this once.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6095) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6096) in6_dev_put_clear(&net->ipv6.ip6_null_entry->rt6i_idev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6097) #ifdef CONFIG_IPV6_MULTIPLE_TABLES
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6098) in6_dev_put_clear(&net->ipv6.ip6_prohibit_entry->rt6i_idev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6099) in6_dev_put_clear(&net->ipv6.ip6_blk_hole_entry->rt6i_idev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6100) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6101) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6103) return NOTIFY_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6104) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6106) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6107) * /proc
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6108) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6110) #ifdef CONFIG_PROC_FS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6111) static int rt6_stats_seq_show(struct seq_file *seq, void *v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6112) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6113) struct net *net = (struct net *)seq->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6114) seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6115) net->ipv6.rt6_stats->fib_nodes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6116) net->ipv6.rt6_stats->fib_route_nodes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6117) atomic_read(&net->ipv6.rt6_stats->fib_rt_alloc),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6118) net->ipv6.rt6_stats->fib_rt_entries,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6119) net->ipv6.rt6_stats->fib_rt_cache,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6120) dst_entries_get_slow(&net->ipv6.ip6_dst_ops),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6121) net->ipv6.rt6_stats->fib_discarded_routes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6123) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6124) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6125) #endif /* CONFIG_PROC_FS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6127) #ifdef CONFIG_SYSCTL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6129) static int ipv6_sysctl_rtcache_flush(struct ctl_table *ctl, int write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6130) void *buffer, size_t *lenp, loff_t *ppos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6131) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6132) struct net *net;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6133) int delay;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6134) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6135) if (!write)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6136) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6138) net = (struct net *)ctl->extra1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6139) delay = net->ipv6.sysctl.flush_delay;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6140) ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6141) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6142) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6144) fib6_run_gc(delay <= 0 ? 0 : (unsigned long)delay, net, delay > 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6145) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6146) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6148) static struct ctl_table ipv6_route_table_template[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6149) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6150) .procname = "flush",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6151) .data = &init_net.ipv6.sysctl.flush_delay,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6152) .maxlen = sizeof(int),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6153) .mode = 0200,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6154) .proc_handler = ipv6_sysctl_rtcache_flush
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6155) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6156) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6157) .procname = "gc_thresh",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6158) .data = &ip6_dst_ops_template.gc_thresh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6159) .maxlen = sizeof(int),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6160) .mode = 0644,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6161) .proc_handler = proc_dointvec,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6162) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6163) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6164) .procname = "max_size",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6165) .data = &init_net.ipv6.sysctl.ip6_rt_max_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6166) .maxlen = sizeof(int),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6167) .mode = 0644,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6168) .proc_handler = proc_dointvec,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6169) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6170) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6171) .procname = "gc_min_interval",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6172) .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6173) .maxlen = sizeof(int),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6174) .mode = 0644,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6175) .proc_handler = proc_dointvec_jiffies,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6176) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6177) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6178) .procname = "gc_timeout",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6179) .data = &init_net.ipv6.sysctl.ip6_rt_gc_timeout,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6180) .maxlen = sizeof(int),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6181) .mode = 0644,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6182) .proc_handler = proc_dointvec_jiffies,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6183) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6184) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6185) .procname = "gc_interval",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6186) .data = &init_net.ipv6.sysctl.ip6_rt_gc_interval,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6187) .maxlen = sizeof(int),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6188) .mode = 0644,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6189) .proc_handler = proc_dointvec_jiffies,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6190) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6191) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6192) .procname = "gc_elasticity",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6193) .data = &init_net.ipv6.sysctl.ip6_rt_gc_elasticity,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6194) .maxlen = sizeof(int),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6195) .mode = 0644,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6196) .proc_handler = proc_dointvec,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6197) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6198) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6199) .procname = "mtu_expires",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6200) .data = &init_net.ipv6.sysctl.ip6_rt_mtu_expires,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6201) .maxlen = sizeof(int),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6202) .mode = 0644,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6203) .proc_handler = proc_dointvec_jiffies,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6204) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6205) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6206) .procname = "min_adv_mss",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6207) .data = &init_net.ipv6.sysctl.ip6_rt_min_advmss,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6208) .maxlen = sizeof(int),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6209) .mode = 0644,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6210) .proc_handler = proc_dointvec,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6211) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6212) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6213) .procname = "gc_min_interval_ms",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6214) .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6215) .maxlen = sizeof(int),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6216) .mode = 0644,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6217) .proc_handler = proc_dointvec_ms_jiffies,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6218) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6219) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6220) .procname = "skip_notify_on_dev_down",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6221) .data = &init_net.ipv6.sysctl.skip_notify_on_dev_down,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6222) .maxlen = sizeof(int),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6223) .mode = 0644,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6224) .proc_handler = proc_dointvec_minmax,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6225) .extra1 = SYSCTL_ZERO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6226) .extra2 = SYSCTL_ONE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6227) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6228) { }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6229) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6230)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6231) struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6232) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6233) struct ctl_table *table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6234)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6235) table = kmemdup(ipv6_route_table_template,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6236) sizeof(ipv6_route_table_template),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6237) GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6238)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6239) if (table) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6240) table[0].data = &net->ipv6.sysctl.flush_delay;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6241) table[0].extra1 = net;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6242) table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6243) table[2].data = &net->ipv6.sysctl.ip6_rt_max_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6244) table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6245) table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6246) table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6247) table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6248) table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6249) table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6250) table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6251) table[10].data = &net->ipv6.sysctl.skip_notify_on_dev_down;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6252)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6253) /* Don't export sysctls to unprivileged users */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6254) if (net->user_ns != &init_user_ns)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6255) table[0].procname = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6256) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6257)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6258) return table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6259) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6260) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6261)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6262) static int __net_init ip6_route_net_init(struct net *net)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6263) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6264) int ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6265)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6266) memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6267) sizeof(net->ipv6.ip6_dst_ops));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6268)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6269) if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6270) goto out_ip6_dst_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6271)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6272) net->ipv6.fib6_null_entry = fib6_info_alloc(GFP_KERNEL, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6273) if (!net->ipv6.fib6_null_entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6274) goto out_ip6_dst_entries;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6275) memcpy(net->ipv6.fib6_null_entry, &fib6_null_entry_template,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6276) sizeof(*net->ipv6.fib6_null_entry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6277)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6278) net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6279) sizeof(*net->ipv6.ip6_null_entry),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6280) GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6281) if (!net->ipv6.ip6_null_entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6282) goto out_fib6_null_entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6283) net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6284) dst_init_metrics(&net->ipv6.ip6_null_entry->dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6285) ip6_template_metrics, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6286) INIT_LIST_HEAD(&net->ipv6.ip6_null_entry->rt6i_uncached);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6287)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6288) #ifdef CONFIG_IPV6_MULTIPLE_TABLES
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6289) net->ipv6.fib6_has_custom_rules = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6290) net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6291) sizeof(*net->ipv6.ip6_prohibit_entry),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6292) GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6293) if (!net->ipv6.ip6_prohibit_entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6294) goto out_ip6_null_entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6295) net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6296) dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6297) ip6_template_metrics, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6298) INIT_LIST_HEAD(&net->ipv6.ip6_prohibit_entry->rt6i_uncached);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6299)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6300) net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6301) sizeof(*net->ipv6.ip6_blk_hole_entry),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6302) GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6303) if (!net->ipv6.ip6_blk_hole_entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6304) goto out_ip6_prohibit_entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6305) net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6306) dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6307) ip6_template_metrics, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6308) INIT_LIST_HEAD(&net->ipv6.ip6_blk_hole_entry->rt6i_uncached);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6309) #ifdef CONFIG_IPV6_SUBTREES
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6310) net->ipv6.fib6_routes_require_src = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6311) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6312) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6313)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6314) net->ipv6.sysctl.flush_delay = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6315) net->ipv6.sysctl.ip6_rt_max_size = 4096;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6316) net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6317) net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6318) net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6319) net->ipv6.sysctl.ip6_rt_gc_elasticity = 9;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6320) net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6321) net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6322) net->ipv6.sysctl.skip_notify_on_dev_down = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6323)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6324) net->ipv6.ip6_rt_gc_expire = 30*HZ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6325)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6326) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6327) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6328) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6329)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6330) #ifdef CONFIG_IPV6_MULTIPLE_TABLES
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6331) out_ip6_prohibit_entry:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6332) kfree(net->ipv6.ip6_prohibit_entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6333) out_ip6_null_entry:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6334) kfree(net->ipv6.ip6_null_entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6335) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6336) out_fib6_null_entry:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6337) kfree(net->ipv6.fib6_null_entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6338) out_ip6_dst_entries:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6339) dst_entries_destroy(&net->ipv6.ip6_dst_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6340) out_ip6_dst_ops:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6341) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6342) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6343)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6344) static void __net_exit ip6_route_net_exit(struct net *net)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6345) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6346) kfree(net->ipv6.fib6_null_entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6347) kfree(net->ipv6.ip6_null_entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6348) #ifdef CONFIG_IPV6_MULTIPLE_TABLES
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6349) kfree(net->ipv6.ip6_prohibit_entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6350) kfree(net->ipv6.ip6_blk_hole_entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6351) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6352) dst_entries_destroy(&net->ipv6.ip6_dst_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6353) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6354)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6355) static int __net_init ip6_route_net_init_late(struct net *net)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6356) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6357) #ifdef CONFIG_PROC_FS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6358) proc_create_net("ipv6_route", 0, net->proc_net, &ipv6_route_seq_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6359) sizeof(struct ipv6_route_iter));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6360) proc_create_net_single("rt6_stats", 0444, net->proc_net,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6361) rt6_stats_seq_show, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6362) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6363) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6364) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6365)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6366) static void __net_exit ip6_route_net_exit_late(struct net *net)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6367) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6368) #ifdef CONFIG_PROC_FS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6369) remove_proc_entry("ipv6_route", net->proc_net);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6370) remove_proc_entry("rt6_stats", net->proc_net);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6371) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6372) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6373)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6374) static struct pernet_operations ip6_route_net_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6375) .init = ip6_route_net_init,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6376) .exit = ip6_route_net_exit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6377) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6378)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6379) static int __net_init ipv6_inetpeer_init(struct net *net)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6380) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6381) struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6382)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6383) if (!bp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6384) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6385) inet_peer_base_init(bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6386) net->ipv6.peers = bp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6387) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6388) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6389)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6390) static void __net_exit ipv6_inetpeer_exit(struct net *net)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6391) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6392) struct inet_peer_base *bp = net->ipv6.peers;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6393)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6394) net->ipv6.peers = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6395) inetpeer_invalidate_tree(bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6396) kfree(bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6397) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6398)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6399) static struct pernet_operations ipv6_inetpeer_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6400) .init = ipv6_inetpeer_init,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6401) .exit = ipv6_inetpeer_exit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6402) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6403)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6404) static struct pernet_operations ip6_route_net_late_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6405) .init = ip6_route_net_init_late,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6406) .exit = ip6_route_net_exit_late,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6407) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6408)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6409) static struct notifier_block ip6_route_dev_notifier = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6410) .notifier_call = ip6_route_dev_notify,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6411) .priority = ADDRCONF_NOTIFY_PRIORITY - 10,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6412) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6413)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6414) void __init ip6_route_init_special_entries(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6415) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6416) /* Registering of the loopback is done before this portion of code,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6417) * the loopback reference in rt6_info will not be taken, do it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6418) * manually for init_net */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6419) init_net.ipv6.fib6_null_entry->fib6_nh->fib_nh_dev = init_net.loopback_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6420) init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6421) init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6422) #ifdef CONFIG_IPV6_MULTIPLE_TABLES
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6423) init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6424) init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6425) init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6426) init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6427) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6428) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6429)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6430) #if IS_BUILTIN(CONFIG_IPV6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6431) #if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_PROC_FS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6432) DEFINE_BPF_ITER_FUNC(ipv6_route, struct bpf_iter_meta *meta, struct fib6_info *rt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6433)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6434) BTF_ID_LIST(btf_fib6_info_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6435) BTF_ID(struct, fib6_info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6436)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6437) static const struct bpf_iter_seq_info ipv6_route_seq_info = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6438) .seq_ops = &ipv6_route_seq_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6439) .init_seq_private = bpf_iter_init_seq_net,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6440) .fini_seq_private = bpf_iter_fini_seq_net,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6441) .seq_priv_size = sizeof(struct ipv6_route_iter),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6442) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6443)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6444) static struct bpf_iter_reg ipv6_route_reg_info = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6445) .target = "ipv6_route",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6446) .ctx_arg_info_size = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6447) .ctx_arg_info = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6448) { offsetof(struct bpf_iter__ipv6_route, rt),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6449) PTR_TO_BTF_ID_OR_NULL },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6450) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6451) .seq_info = &ipv6_route_seq_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6452) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6453)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6454) static int __init bpf_iter_register(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6455) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6456) ipv6_route_reg_info.ctx_arg_info[0].btf_id = *btf_fib6_info_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6457) return bpf_iter_reg_target(&ipv6_route_reg_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6458) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6459)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6460) static void bpf_iter_unregister(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6461) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6462) bpf_iter_unreg_target(&ipv6_route_reg_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6463) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6464) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6465) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6466)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6467) int __init ip6_route_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6468) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6469) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6470) int cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6471)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6472) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6473) ip6_dst_ops_template.kmem_cachep =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6474) kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6475) SLAB_HWCACHE_ALIGN, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6476) if (!ip6_dst_ops_template.kmem_cachep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6477) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6478)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6479) ret = dst_entries_init(&ip6_dst_blackhole_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6480) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6481) goto out_kmem_cache;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6482)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6483) ret = register_pernet_subsys(&ipv6_inetpeer_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6484) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6485) goto out_dst_entries;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6486)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6487) ret = register_pernet_subsys(&ip6_route_net_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6488) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6489) goto out_register_inetpeer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6490)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6491) ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6492)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6493) ret = fib6_init();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6494) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6495) goto out_register_subsys;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6496)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6497) ret = xfrm6_init();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6498) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6499) goto out_fib6_init;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6500)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6501) ret = fib6_rules_init();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6502) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6503) goto xfrm6_init;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6504)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6505) ret = register_pernet_subsys(&ip6_route_net_late_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6506) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6507) goto fib6_rules_init;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6508)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6509) ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_NEWROUTE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6510) inet6_rtm_newroute, NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6511) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6512) goto out_register_late_subsys;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6513)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6514) ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_DELROUTE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6515) inet6_rtm_delroute, NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6516) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6517) goto out_register_late_subsys;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6518)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6519) ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETROUTE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6520) inet6_rtm_getroute, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6521) RTNL_FLAG_DOIT_UNLOCKED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6522) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6523) goto out_register_late_subsys;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6524)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6525) ret = register_netdevice_notifier(&ip6_route_dev_notifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6526) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6527) goto out_register_late_subsys;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6528)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6529) #if IS_BUILTIN(CONFIG_IPV6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6530) #if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_PROC_FS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6531) ret = bpf_iter_register();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6532) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6533) goto out_register_late_subsys;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6534) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6535) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6536)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6537) for_each_possible_cpu(cpu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6538) struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6539)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6540) INIT_LIST_HEAD(&ul->head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6541) spin_lock_init(&ul->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6542) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6543)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6544) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6545) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6546)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6547) out_register_late_subsys:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6548) rtnl_unregister_all(PF_INET6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6549) unregister_pernet_subsys(&ip6_route_net_late_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6550) fib6_rules_init:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6551) fib6_rules_cleanup();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6552) xfrm6_init:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6553) xfrm6_fini();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6554) out_fib6_init:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6555) fib6_gc_cleanup();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6556) out_register_subsys:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6557) unregister_pernet_subsys(&ip6_route_net_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6558) out_register_inetpeer:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6559) unregister_pernet_subsys(&ipv6_inetpeer_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6560) out_dst_entries:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6561) dst_entries_destroy(&ip6_dst_blackhole_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6562) out_kmem_cache:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6563) kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6564) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6565) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6566)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6567) void ip6_route_cleanup(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6568) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6569) #if IS_BUILTIN(CONFIG_IPV6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6570) #if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_PROC_FS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6571) bpf_iter_unregister();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6572) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6573) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6574) unregister_netdevice_notifier(&ip6_route_dev_notifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6575) unregister_pernet_subsys(&ip6_route_net_late_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6576) fib6_rules_cleanup();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6577) xfrm6_fini();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6578) fib6_gc_cleanup();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6579) unregister_pernet_subsys(&ipv6_inetpeer_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6580) unregister_pernet_subsys(&ip6_route_net_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6581) dst_entries_destroy(&ip6_dst_blackhole_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6582) kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6583) }