Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Linux INET6 implementation |
| 4 | * FIB front-end. |
| 5 | * |
| 6 | * Authors: |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 7 | * Pedro Roque <roque@di.fc.ul.pt> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | /* Changes: |
| 11 | * |
| 12 | * YOSHIFUJI Hideaki @USAGI |
| 13 | * reworked default router selection. |
| 14 | * - respect outgoing interface |
| 15 | * - select from (probably) reachable routers (i.e. |
| 16 | * routers in REACHABLE, STALE, DELAY or PROBE states). |
| 17 | * - always select the same router if it is (probably) |
| 18 | * reachable. otherwise, round-robin the list. |
YOSHIFUJI Hideaki | c0bece9 | 2006-08-23 17:23:25 -0700 | [diff] [blame] | 19 | * Ville Nuorvala |
| 20 | * Fixed routing subtrees. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | */ |
| 22 | |
Joe Perches | f321383 | 2012-05-15 14:11:53 +0000 | [diff] [blame] | 23 | #define pr_fmt(fmt) "IPv6: " fmt |
| 24 | |
Randy Dunlap | 4fc268d | 2006-01-11 12:17:47 -0800 | [diff] [blame] | 25 | #include <linux/capability.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include <linux/errno.h> |
Paul Gortmaker | bc3b2d7 | 2011-07-15 11:47:34 -0400 | [diff] [blame] | 27 | #include <linux/export.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include <linux/types.h> |
| 29 | #include <linux/times.h> |
| 30 | #include <linux/socket.h> |
| 31 | #include <linux/sockios.h> |
| 32 | #include <linux/net.h> |
| 33 | #include <linux/route.h> |
| 34 | #include <linux/netdevice.h> |
| 35 | #include <linux/in6.h> |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 36 | #include <linux/mroute6.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | #include <linux/init.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | #include <linux/if_arp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | #include <linux/proc_fs.h> |
| 40 | #include <linux/seq_file.h> |
Daniel Lezcano | 5b7c931 | 2008-03-03 23:28:58 -0800 | [diff] [blame] | 41 | #include <linux/nsproxy.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 42 | #include <linux/slab.h> |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 43 | #include <linux/jhash.h> |
Eric W. Biederman | 457c4cb | 2007-09-12 12:01:34 +0200 | [diff] [blame] | 44 | #include <net/net_namespace.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | #include <net/snmp.h> |
| 46 | #include <net/ipv6.h> |
| 47 | #include <net/ip6_fib.h> |
| 48 | #include <net/ip6_route.h> |
| 49 | #include <net/ndisc.h> |
| 50 | #include <net/addrconf.h> |
| 51 | #include <net/tcp.h> |
| 52 | #include <linux/rtnetlink.h> |
| 53 | #include <net/dst.h> |
Jiri Benc | 904af04 | 2015-08-20 13:56:31 +0200 | [diff] [blame] | 54 | #include <net/dst_metadata.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | #include <net/xfrm.h> |
Tom Tucker | 8d71740 | 2006-07-30 20:43:36 -0700 | [diff] [blame] | 56 | #include <net/netevent.h> |
Thomas Graf | 21713eb | 2006-08-15 00:35:24 -0700 | [diff] [blame] | 57 | #include <net/netlink.h> |
David Ahern | 3c618c1 | 2019-04-20 09:28:20 -0700 | [diff] [blame] | 58 | #include <net/rtnh.h> |
Roopa Prabhu | 19e42e4 | 2015-07-21 10:43:48 +0200 | [diff] [blame] | 59 | #include <net/lwtunnel.h> |
Jiri Benc | 904af04 | 2015-08-20 13:56:31 +0200 | [diff] [blame] | 60 | #include <net/ip_tunnels.h> |
David Ahern | ca25449 | 2015-10-12 11:47:10 -0700 | [diff] [blame] | 61 | #include <net/l3mdev.h> |
Roopa Prabhu | eacb938 | 2018-05-22 14:03:28 -0700 | [diff] [blame] | 62 | #include <net/ip.h> |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 63 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | |
| 65 | #ifdef CONFIG_SYSCTL |
| 66 | #include <linux/sysctl.h> |
| 67 | #endif |
| 68 | |
David Ahern | 30d444d | 2018-05-23 17:08:48 -0700 | [diff] [blame] | 69 | static int ip6_rt_type_to_error(u8 fib6_type); |
| 70 | |
| 71 | #define CREATE_TRACE_POINTS |
| 72 | #include <trace/events/fib6.h> |
| 73 | EXPORT_TRACEPOINT_SYMBOL_GPL(fib6_table_lookup); |
| 74 | #undef CREATE_TRACE_POINTS |
| 75 | |
Hannes Frederic Sowa | afc154e | 2013-07-11 12:43:42 +0200 | [diff] [blame] | 76 | enum rt6_nud_state { |
Jiri Benc | 7e98056 | 2013-12-11 13:48:20 +0100 | [diff] [blame] | 77 | RT6_NUD_FAIL_HARD = -3, |
| 78 | RT6_NUD_FAIL_PROBE = -2, |
| 79 | RT6_NUD_FAIL_DO_RR = -1, |
Hannes Frederic Sowa | afc154e | 2013-07-11 12:43:42 +0200 | [diff] [blame] | 80 | RT6_NUD_SUCCEED = 1 |
| 81 | }; |
| 82 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie); |
David S. Miller | 0dbaee3 | 2010-12-13 12:52:14 -0800 | [diff] [blame] | 84 | static unsigned int ip6_default_advmss(const struct dst_entry *dst); |
Steffen Klassert | ebb762f | 2011-11-23 02:12:51 +0000 | [diff] [blame] | 85 | static unsigned int ip6_mtu(const struct dst_entry *dst); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | static struct dst_entry *ip6_negative_advice(struct dst_entry *); |
| 87 | static void ip6_dst_destroy(struct dst_entry *); |
| 88 | static void ip6_dst_ifdown(struct dst_entry *, |
| 89 | struct net_device *dev, int how); |
Daniel Lezcano | 569d364 | 2008-01-18 03:56:57 -0800 | [diff] [blame] | 90 | static int ip6_dst_gc(struct dst_ops *ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | |
| 92 | static int ip6_pkt_discard(struct sk_buff *skb); |
Eric W. Biederman | ede2059 | 2015-10-07 16:48:47 -0500 | [diff] [blame] | 93 | static int ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb); |
Kamala R | 7150aed | 2013-12-02 19:55:21 +0530 | [diff] [blame] | 94 | static int ip6_pkt_prohibit(struct sk_buff *skb); |
Eric W. Biederman | ede2059 | 2015-10-07 16:48:47 -0500 | [diff] [blame] | 95 | static int ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | static void ip6_link_failure(struct sk_buff *skb); |
David S. Miller | 6700c27 | 2012-07-17 03:29:28 -0700 | [diff] [blame] | 97 | static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk, |
Hangbin Liu | bd085ef | 2019-12-22 10:51:09 +0800 | [diff] [blame] | 98 | struct sk_buff *skb, u32 mtu, |
| 99 | bool confirm_neigh); |
David S. Miller | 6700c27 | 2012-07-17 03:29:28 -0700 | [diff] [blame] | 100 | static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, |
| 101 | struct sk_buff *skb); |
David Ahern | 702cea5 | 2019-04-09 14:41:13 -0700 | [diff] [blame] | 102 | static int rt6_score_route(const struct fib6_nh *nh, u32 fib6_flags, int oif, |
| 103 | int strict); |
David Ahern | a1b7a1f | 2019-06-08 14:53:26 -0700 | [diff] [blame] | 104 | static size_t rt6_nlmsg_size(struct fib6_info *f6i); |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 105 | static int rt6_fill_node(struct net *net, struct sk_buff *skb, |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 106 | struct fib6_info *rt, struct dst_entry *dst, |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 107 | struct in6_addr *dest, struct in6_addr *src, |
David Ahern | 16a16cd | 2017-02-02 12:37:11 -0800 | [diff] [blame] | 108 | int iif, int type, u32 portid, u32 seq, |
| 109 | unsigned int flags); |
David Ahern | 7e4b512 | 2019-04-16 14:36:00 -0700 | [diff] [blame] | 110 | static struct rt6_info *rt6_find_cached_rt(const struct fib6_result *res, |
Wei Wang | 510e2ce | 2019-05-16 13:30:54 -0700 | [diff] [blame] | 111 | const struct in6_addr *daddr, |
| 112 | const struct in6_addr *saddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 114 | #ifdef CONFIG_IPV6_ROUTE_INFO |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 115 | static struct fib6_info *rt6_add_route_info(struct net *net, |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 116 | const struct in6_addr *prefix, int prefixlen, |
David Ahern | 830218c | 2016-10-24 10:52:35 -0700 | [diff] [blame] | 117 | const struct in6_addr *gwaddr, |
| 118 | struct net_device *dev, |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 119 | unsigned int pref); |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 120 | static struct fib6_info *rt6_get_route_info(struct net *net, |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 121 | const struct in6_addr *prefix, int prefixlen, |
David Ahern | 830218c | 2016-10-24 10:52:35 -0700 | [diff] [blame] | 122 | const struct in6_addr *gwaddr, |
| 123 | struct net_device *dev); |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 124 | #endif |
| 125 | |
Martin KaFai Lau | 8d0b94a | 2015-05-22 20:56:04 -0700 | [diff] [blame] | 126 | struct uncached_list { |
| 127 | spinlock_t lock; |
| 128 | struct list_head head; |
| 129 | }; |
| 130 | |
| 131 | static DEFINE_PER_CPU_ALIGNED(struct uncached_list, rt6_uncached_list); |
| 132 | |
Xin Long | 510c321 | 2018-02-14 19:06:02 +0800 | [diff] [blame] | 133 | void rt6_uncached_list_add(struct rt6_info *rt) |
Martin KaFai Lau | 8d0b94a | 2015-05-22 20:56:04 -0700 | [diff] [blame] | 134 | { |
| 135 | struct uncached_list *ul = raw_cpu_ptr(&rt6_uncached_list); |
| 136 | |
Martin KaFai Lau | 8d0b94a | 2015-05-22 20:56:04 -0700 | [diff] [blame] | 137 | rt->rt6i_uncached_list = ul; |
| 138 | |
| 139 | spin_lock_bh(&ul->lock); |
| 140 | list_add_tail(&rt->rt6i_uncached, &ul->head); |
| 141 | spin_unlock_bh(&ul->lock); |
| 142 | } |
| 143 | |
Xin Long | 510c321 | 2018-02-14 19:06:02 +0800 | [diff] [blame] | 144 | void rt6_uncached_list_del(struct rt6_info *rt) |
Martin KaFai Lau | 8d0b94a | 2015-05-22 20:56:04 -0700 | [diff] [blame] | 145 | { |
| 146 | if (!list_empty(&rt->rt6i_uncached)) { |
| 147 | struct uncached_list *ul = rt->rt6i_uncached_list; |
Wei Wang | 81eb844 | 2017-10-06 12:06:11 -0700 | [diff] [blame] | 148 | struct net *net = dev_net(rt->dst.dev); |
Martin KaFai Lau | 8d0b94a | 2015-05-22 20:56:04 -0700 | [diff] [blame] | 149 | |
| 150 | spin_lock_bh(&ul->lock); |
| 151 | list_del(&rt->rt6i_uncached); |
Wei Wang | 81eb844 | 2017-10-06 12:06:11 -0700 | [diff] [blame] | 152 | atomic_dec(&net->ipv6.rt6_stats->fib_rt_uncache); |
Martin KaFai Lau | 8d0b94a | 2015-05-22 20:56:04 -0700 | [diff] [blame] | 153 | spin_unlock_bh(&ul->lock); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | static void rt6_uncached_list_flush_dev(struct net *net, struct net_device *dev) |
| 158 | { |
| 159 | struct net_device *loopback_dev = net->loopback_dev; |
| 160 | int cpu; |
| 161 | |
Eric W. Biederman | e332bc6 | 2015-10-12 11:02:08 -0500 | [diff] [blame] | 162 | if (dev == loopback_dev) |
| 163 | return; |
| 164 | |
Martin KaFai Lau | 8d0b94a | 2015-05-22 20:56:04 -0700 | [diff] [blame] | 165 | for_each_possible_cpu(cpu) { |
| 166 | struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu); |
| 167 | struct rt6_info *rt; |
| 168 | |
| 169 | spin_lock_bh(&ul->lock); |
| 170 | list_for_each_entry(rt, &ul->head, rt6i_uncached) { |
| 171 | struct inet6_dev *rt_idev = rt->rt6i_idev; |
| 172 | struct net_device *rt_dev = rt->dst.dev; |
| 173 | |
Eric W. Biederman | e332bc6 | 2015-10-12 11:02:08 -0500 | [diff] [blame] | 174 | if (rt_idev->dev == dev) { |
Martin KaFai Lau | 8d0b94a | 2015-05-22 20:56:04 -0700 | [diff] [blame] | 175 | rt->rt6i_idev = in6_dev_get(loopback_dev); |
| 176 | in6_dev_put(rt_idev); |
| 177 | } |
| 178 | |
Eric W. Biederman | e332bc6 | 2015-10-12 11:02:08 -0500 | [diff] [blame] | 179 | if (rt_dev == dev) { |
Mahesh Bandewar | 8d7017f | 2019-07-01 14:38:57 -0700 | [diff] [blame] | 180 | rt->dst.dev = blackhole_netdev; |
Martin KaFai Lau | 8d0b94a | 2015-05-22 20:56:04 -0700 | [diff] [blame] | 181 | dev_hold(rt->dst.dev); |
| 182 | dev_put(rt_dev); |
| 183 | } |
| 184 | } |
| 185 | spin_unlock_bh(&ul->lock); |
| 186 | } |
| 187 | } |
| 188 | |
David Ahern | f8a1b43 | 2018-04-17 17:33:21 -0700 | [diff] [blame] | 189 | static inline const void *choose_neigh_daddr(const struct in6_addr *p, |
David S. Miller | f894cbf | 2012-07-02 21:52:24 -0700 | [diff] [blame] | 190 | struct sk_buff *skb, |
| 191 | const void *daddr) |
David S. Miller | 3923297 | 2012-01-26 15:22:32 -0500 | [diff] [blame] | 192 | { |
David S. Miller | a7563f3 | 2012-01-26 16:29:16 -0500 | [diff] [blame] | 193 | if (!ipv6_addr_any(p)) |
David S. Miller | 3923297 | 2012-01-26 15:22:32 -0500 | [diff] [blame] | 194 | return (const void *) p; |
David S. Miller | f894cbf | 2012-07-02 21:52:24 -0700 | [diff] [blame] | 195 | else if (skb) |
| 196 | return &ipv6_hdr(skb)->daddr; |
David S. Miller | 3923297 | 2012-01-26 15:22:32 -0500 | [diff] [blame] | 197 | return daddr; |
| 198 | } |
| 199 | |
David Ahern | f8a1b43 | 2018-04-17 17:33:21 -0700 | [diff] [blame] | 200 | struct neighbour *ip6_neigh_lookup(const struct in6_addr *gw, |
| 201 | struct net_device *dev, |
| 202 | struct sk_buff *skb, |
| 203 | const void *daddr) |
David S. Miller | d3aaeb3 | 2011-07-18 00:40:17 -0700 | [diff] [blame] | 204 | { |
David S. Miller | 3923297 | 2012-01-26 15:22:32 -0500 | [diff] [blame] | 205 | struct neighbour *n; |
| 206 | |
David Ahern | f8a1b43 | 2018-04-17 17:33:21 -0700 | [diff] [blame] | 207 | daddr = choose_neigh_daddr(gw, skb, daddr); |
| 208 | n = __ipv6_neigh_lookup(dev, daddr); |
David S. Miller | f83c779 | 2011-12-28 15:41:23 -0500 | [diff] [blame] | 209 | if (n) |
| 210 | return n; |
Stefano Brivio | 7adf324 | 2019-01-02 13:29:27 +0100 | [diff] [blame] | 211 | |
| 212 | n = neigh_create(&nd_tbl, daddr, dev); |
| 213 | return IS_ERR(n) ? NULL : n; |
David Ahern | f8a1b43 | 2018-04-17 17:33:21 -0700 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | static struct neighbour *ip6_dst_neigh_lookup(const struct dst_entry *dst, |
| 217 | struct sk_buff *skb, |
| 218 | const void *daddr) |
| 219 | { |
| 220 | const struct rt6_info *rt = container_of(dst, struct rt6_info, dst); |
| 221 | |
Nicolas Dichtel | 2c6b55f | 2019-06-24 16:01:09 +0200 | [diff] [blame] | 222 | return ip6_neigh_lookup(rt6_nexthop(rt, &in6addr_any), |
| 223 | dst->dev, skb, daddr); |
David S. Miller | f83c779 | 2011-12-28 15:41:23 -0500 | [diff] [blame] | 224 | } |
| 225 | |
Julian Anastasov | 63fca65 | 2017-02-06 23:14:15 +0200 | [diff] [blame] | 226 | static void ip6_confirm_neigh(const struct dst_entry *dst, const void *daddr) |
| 227 | { |
| 228 | struct net_device *dev = dst->dev; |
| 229 | struct rt6_info *rt = (struct rt6_info *)dst; |
| 230 | |
Stefano Brivio | cbfd689 | 2019-09-09 22:44:06 +0200 | [diff] [blame] | 231 | daddr = choose_neigh_daddr(rt6_nexthop(rt, &in6addr_any), NULL, daddr); |
Julian Anastasov | 63fca65 | 2017-02-06 23:14:15 +0200 | [diff] [blame] | 232 | if (!daddr) |
| 233 | return; |
| 234 | if (dev->flags & (IFF_NOARP | IFF_LOOPBACK)) |
| 235 | return; |
| 236 | if (ipv6_addr_is_multicast((const struct in6_addr *)daddr)) |
| 237 | return; |
| 238 | __ipv6_confirm_neigh(dev, daddr); |
| 239 | } |
| 240 | |
Daniel Lezcano | 9a7ec3a | 2008-03-04 13:48:53 -0800 | [diff] [blame] | 241 | static struct dst_ops ip6_dst_ops_template = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | .family = AF_INET6, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | .gc = ip6_dst_gc, |
| 244 | .gc_thresh = 1024, |
| 245 | .check = ip6_dst_check, |
David S. Miller | 0dbaee3 | 2010-12-13 12:52:14 -0800 | [diff] [blame] | 246 | .default_advmss = ip6_default_advmss, |
Steffen Klassert | ebb762f | 2011-11-23 02:12:51 +0000 | [diff] [blame] | 247 | .mtu = ip6_mtu, |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 248 | .cow_metrics = dst_cow_metrics_generic, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | .destroy = ip6_dst_destroy, |
| 250 | .ifdown = ip6_dst_ifdown, |
| 251 | .negative_advice = ip6_negative_advice, |
| 252 | .link_failure = ip6_link_failure, |
| 253 | .update_pmtu = ip6_rt_update_pmtu, |
David S. Miller | 6e157b6 | 2012-07-12 00:05:02 -0700 | [diff] [blame] | 254 | .redirect = rt6_do_redirect, |
Eric W. Biederman | 9f8955c | 2015-10-07 16:48:39 -0500 | [diff] [blame] | 255 | .local_out = __ip6_local_out, |
David Ahern | f8a1b43 | 2018-04-17 17:33:21 -0700 | [diff] [blame] | 256 | .neigh_lookup = ip6_dst_neigh_lookup, |
Julian Anastasov | 63fca65 | 2017-02-06 23:14:15 +0200 | [diff] [blame] | 257 | .confirm_neigh = ip6_confirm_neigh, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | }; |
| 259 | |
Steffen Klassert | ebb762f | 2011-11-23 02:12:51 +0000 | [diff] [blame] | 260 | static unsigned int ip6_blackhole_mtu(const struct dst_entry *dst) |
Roland Dreier | ec831ea | 2011-01-31 13:16:00 -0800 | [diff] [blame] | 261 | { |
Steffen Klassert | 618f9bc | 2011-11-23 02:13:31 +0000 | [diff] [blame] | 262 | unsigned int mtu = dst_metric_raw(dst, RTAX_MTU); |
| 263 | |
| 264 | return mtu ? : dst->dev->mtu; |
Roland Dreier | ec831ea | 2011-01-31 13:16:00 -0800 | [diff] [blame] | 265 | } |
| 266 | |
David S. Miller | 6700c27 | 2012-07-17 03:29:28 -0700 | [diff] [blame] | 267 | static void ip6_rt_blackhole_update_pmtu(struct dst_entry *dst, struct sock *sk, |
Hangbin Liu | bd085ef | 2019-12-22 10:51:09 +0800 | [diff] [blame] | 268 | struct sk_buff *skb, u32 mtu, |
| 269 | bool confirm_neigh) |
David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 270 | { |
| 271 | } |
| 272 | |
David S. Miller | 6700c27 | 2012-07-17 03:29:28 -0700 | [diff] [blame] | 273 | static void ip6_rt_blackhole_redirect(struct dst_entry *dst, struct sock *sk, |
| 274 | struct sk_buff *skb) |
David S. Miller | b587ee3 | 2012-07-12 00:39:24 -0700 | [diff] [blame] | 275 | { |
| 276 | } |
| 277 | |
David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 278 | static struct dst_ops ip6_dst_blackhole_ops = { |
| 279 | .family = AF_INET6, |
David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 280 | .destroy = ip6_dst_destroy, |
| 281 | .check = ip6_dst_check, |
Steffen Klassert | ebb762f | 2011-11-23 02:12:51 +0000 | [diff] [blame] | 282 | .mtu = ip6_blackhole_mtu, |
Eric Dumazet | 214f45c | 2011-02-18 11:39:01 -0800 | [diff] [blame] | 283 | .default_advmss = ip6_default_advmss, |
David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 284 | .update_pmtu = ip6_rt_blackhole_update_pmtu, |
David S. Miller | b587ee3 | 2012-07-12 00:39:24 -0700 | [diff] [blame] | 285 | .redirect = ip6_rt_blackhole_redirect, |
Martin KaFai Lau | 0a1f596 | 2015-10-15 16:39:58 -0700 | [diff] [blame] | 286 | .cow_metrics = dst_cow_metrics_generic, |
David Ahern | f8a1b43 | 2018-04-17 17:33:21 -0700 | [diff] [blame] | 287 | .neigh_lookup = ip6_dst_neigh_lookup, |
David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 288 | }; |
| 289 | |
David S. Miller | 62fa8a8 | 2011-01-26 20:51:05 -0800 | [diff] [blame] | 290 | static const u32 ip6_template_metrics[RTAX_MAX] = { |
Li RongQing | 14edd87 | 2012-10-24 14:01:18 +0800 | [diff] [blame] | 291 | [RTAX_HOPLIMIT - 1] = 0, |
David S. Miller | 62fa8a8 | 2011-01-26 20:51:05 -0800 | [diff] [blame] | 292 | }; |
| 293 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 294 | static const struct fib6_info fib6_null_entry_template = { |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 295 | .fib6_flags = (RTF_REJECT | RTF_NONEXTHOP), |
| 296 | .fib6_protocol = RTPROT_KERNEL, |
| 297 | .fib6_metric = ~(u32)0, |
Eric Dumazet | f05713e | 2019-04-22 18:35:03 -0700 | [diff] [blame] | 298 | .fib6_ref = REFCOUNT_INIT(1), |
David Ahern | 421842e | 2018-04-17 17:33:18 -0700 | [diff] [blame] | 299 | .fib6_type = RTN_UNREACHABLE, |
| 300 | .fib6_metrics = (struct dst_metrics *)&dst_default_metrics, |
| 301 | }; |
| 302 | |
Eric Dumazet | fb0af4c | 2012-09-11 21:47:51 +0000 | [diff] [blame] | 303 | static const struct rt6_info ip6_null_entry_template = { |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 304 | .dst = { |
| 305 | .__refcnt = ATOMIC_INIT(1), |
| 306 | .__use = 1, |
Nicolas Dichtel | 2c20cbd | 2012-09-10 22:09:47 +0000 | [diff] [blame] | 307 | .obsolete = DST_OBSOLETE_FORCE_CHK, |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 308 | .error = -ENETUNREACH, |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 309 | .input = ip6_pkt_discard, |
| 310 | .output = ip6_pkt_discard_out, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | }, |
| 312 | .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | }; |
| 314 | |
Thomas Graf | 101367c | 2006-08-04 03:39:02 -0700 | [diff] [blame] | 315 | #ifdef CONFIG_IPV6_MULTIPLE_TABLES |
| 316 | |
Eric Dumazet | fb0af4c | 2012-09-11 21:47:51 +0000 | [diff] [blame] | 317 | static const struct rt6_info ip6_prohibit_entry_template = { |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 318 | .dst = { |
| 319 | .__refcnt = ATOMIC_INIT(1), |
| 320 | .__use = 1, |
Nicolas Dichtel | 2c20cbd | 2012-09-10 22:09:47 +0000 | [diff] [blame] | 321 | .obsolete = DST_OBSOLETE_FORCE_CHK, |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 322 | .error = -EACCES, |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 323 | .input = ip6_pkt_prohibit, |
| 324 | .output = ip6_pkt_prohibit_out, |
Thomas Graf | 101367c | 2006-08-04 03:39:02 -0700 | [diff] [blame] | 325 | }, |
| 326 | .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP), |
Thomas Graf | 101367c | 2006-08-04 03:39:02 -0700 | [diff] [blame] | 327 | }; |
| 328 | |
Eric Dumazet | fb0af4c | 2012-09-11 21:47:51 +0000 | [diff] [blame] | 329 | static const struct rt6_info ip6_blk_hole_entry_template = { |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 330 | .dst = { |
| 331 | .__refcnt = ATOMIC_INIT(1), |
| 332 | .__use = 1, |
Nicolas Dichtel | 2c20cbd | 2012-09-10 22:09:47 +0000 | [diff] [blame] | 333 | .obsolete = DST_OBSOLETE_FORCE_CHK, |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 334 | .error = -EINVAL, |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 335 | .input = dst_discard, |
Eric W. Biederman | ede2059 | 2015-10-07 16:48:47 -0500 | [diff] [blame] | 336 | .output = dst_discard_out, |
Thomas Graf | 101367c | 2006-08-04 03:39:02 -0700 | [diff] [blame] | 337 | }, |
| 338 | .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP), |
Thomas Graf | 101367c | 2006-08-04 03:39:02 -0700 | [diff] [blame] | 339 | }; |
| 340 | |
| 341 | #endif |
| 342 | |
Martin KaFai Lau | ebfa45f | 2015-10-15 16:39:57 -0700 | [diff] [blame] | 343 | static void rt6_info_init(struct rt6_info *rt) |
| 344 | { |
| 345 | struct dst_entry *dst = &rt->dst; |
| 346 | |
| 347 | memset(dst + 1, 0, sizeof(*rt) - sizeof(*dst)); |
Martin KaFai Lau | ebfa45f | 2015-10-15 16:39:57 -0700 | [diff] [blame] | 348 | INIT_LIST_HEAD(&rt->rt6i_uncached); |
| 349 | } |
| 350 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | /* allocate dst with ip6_dst_ops */ |
David Ahern | 93531c6 | 2018-04-17 17:33:25 -0700 | [diff] [blame] | 352 | struct rt6_info *ip6_dst_alloc(struct net *net, struct net_device *dev, |
| 353 | int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | { |
David S. Miller | 97bab73 | 2012-06-09 22:36:36 -0700 | [diff] [blame] | 355 | struct rt6_info *rt = dst_alloc(&net->ipv6.ip6_dst_ops, dev, |
Wei Wang | b2a9c0e | 2017-06-17 10:42:41 -0700 | [diff] [blame] | 356 | 1, DST_OBSOLETE_FORCE_CHK, flags); |
David S. Miller | cf91166 | 2011-04-28 14:31:47 -0700 | [diff] [blame] | 357 | |
Wei Wang | 81eb844 | 2017-10-06 12:06:11 -0700 | [diff] [blame] | 358 | if (rt) { |
Martin KaFai Lau | ebfa45f | 2015-10-15 16:39:57 -0700 | [diff] [blame] | 359 | rt6_info_init(rt); |
Wei Wang | 81eb844 | 2017-10-06 12:06:11 -0700 | [diff] [blame] | 360 | atomic_inc(&net->ipv6.rt6_stats->fib_rt_alloc); |
| 361 | } |
Steffen Klassert | 8104891 | 2012-07-05 23:37:09 +0000 | [diff] [blame] | 362 | |
David S. Miller | cf91166 | 2011-04-28 14:31:47 -0700 | [diff] [blame] | 363 | return rt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | } |
David Ahern | 9ab179d | 2016-04-07 11:10:06 -0700 | [diff] [blame] | 365 | EXPORT_SYMBOL(ip6_dst_alloc); |
Martin KaFai Lau | d52d399 | 2015-05-22 20:56:06 -0700 | [diff] [blame] | 366 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | static void ip6_dst_destroy(struct dst_entry *dst) |
| 368 | { |
| 369 | struct rt6_info *rt = (struct rt6_info *)dst; |
David Ahern | a68886a | 2018-04-20 15:38:02 -0700 | [diff] [blame] | 370 | struct fib6_info *from; |
Martin KaFai Lau | 8d0b94a | 2015-05-22 20:56:04 -0700 | [diff] [blame] | 371 | struct inet6_dev *idev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | |
David Ahern | 1620a33 | 2018-10-04 20:07:54 -0700 | [diff] [blame] | 373 | ip_dst_metrics_put(dst); |
Martin KaFai Lau | 8d0b94a | 2015-05-22 20:56:04 -0700 | [diff] [blame] | 374 | rt6_uncached_list_del(rt); |
| 375 | |
| 376 | idev = rt->rt6i_idev; |
David S. Miller | 3830847 | 2011-12-03 18:02:47 -0500 | [diff] [blame] | 377 | if (idev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | rt->rt6i_idev = NULL; |
| 379 | in6_dev_put(idev); |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 380 | } |
Gao feng | 1716a96 | 2012-04-06 00:13:10 +0000 | [diff] [blame] | 381 | |
Eric Dumazet | 0e23387 | 2019-04-28 12:22:25 -0700 | [diff] [blame] | 382 | from = xchg((__force struct fib6_info **)&rt->from, NULL); |
David Ahern | 93531c6 | 2018-04-17 17:33:25 -0700 | [diff] [blame] | 383 | fib6_info_release(from); |
David S. Miller | b341936 | 2010-11-30 12:27:11 -0800 | [diff] [blame] | 384 | } |
| 385 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev, |
| 387 | int how) |
| 388 | { |
| 389 | struct rt6_info *rt = (struct rt6_info *)dst; |
| 390 | struct inet6_dev *idev = rt->rt6i_idev; |
Denis V. Lunev | 5a3e55d | 2007-12-07 00:38:10 -0800 | [diff] [blame] | 391 | struct net_device *loopback_dev = |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 392 | dev_net(dev)->loopback_dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | |
Wei Wang | e5645f5 | 2017-08-14 10:44:59 -0700 | [diff] [blame] | 394 | if (idev && idev->dev != loopback_dev) { |
| 395 | struct inet6_dev *loopback_idev = in6_dev_get(loopback_dev); |
| 396 | if (loopback_idev) { |
| 397 | rt->rt6i_idev = loopback_idev; |
| 398 | in6_dev_put(idev); |
David S. Miller | 97cac08 | 2012-07-02 22:43:47 -0700 | [diff] [blame] | 399 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | } |
| 401 | } |
| 402 | |
Martin KaFai Lau | 5973fb1 | 2015-11-11 11:51:07 -0800 | [diff] [blame] | 403 | static bool __rt6_check_expired(const struct rt6_info *rt) |
| 404 | { |
| 405 | if (rt->rt6i_flags & RTF_EXPIRES) |
| 406 | return time_after(jiffies, rt->dst.expires); |
| 407 | else |
| 408 | return false; |
| 409 | } |
| 410 | |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 411 | static bool rt6_check_expired(const struct rt6_info *rt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | { |
David Ahern | a68886a | 2018-04-20 15:38:02 -0700 | [diff] [blame] | 413 | struct fib6_info *from; |
| 414 | |
| 415 | from = rcu_dereference(rt->from); |
| 416 | |
Gao feng | 1716a96 | 2012-04-06 00:13:10 +0000 | [diff] [blame] | 417 | if (rt->rt6i_flags & RTF_EXPIRES) { |
| 418 | if (time_after(jiffies, rt->dst.expires)) |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 419 | return true; |
David Ahern | a68886a | 2018-04-20 15:38:02 -0700 | [diff] [blame] | 420 | } else if (from) { |
Xin Long | 1e2ea8a | 2017-08-26 20:10:10 +0800 | [diff] [blame] | 421 | return rt->dst.obsolete != DST_OBSOLETE_FORCE_CHK || |
David Ahern | a68886a | 2018-04-20 15:38:02 -0700 | [diff] [blame] | 422 | fib6_check_expired(from); |
Gao feng | 1716a96 | 2012-04-06 00:13:10 +0000 | [diff] [blame] | 423 | } |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 424 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | } |
| 426 | |
David Ahern | b1d4099 | 2019-04-16 14:35:59 -0700 | [diff] [blame] | 427 | void fib6_select_path(const struct net *net, struct fib6_result *res, |
| 428 | struct flowi6 *fl6, int oif, bool have_oif_match, |
| 429 | const struct sk_buff *skb, int strict) |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 430 | { |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 431 | struct fib6_info *sibling, *next_sibling; |
David Ahern | b1d4099 | 2019-04-16 14:35:59 -0700 | [diff] [blame] | 432 | struct fib6_info *match = res->f6i; |
| 433 | |
David Ahern | 34fe5a1 | 2020-07-06 11:45:07 -0600 | [diff] [blame] | 434 | if (!match->nh && (!match->fib6_nsiblings || have_oif_match)) |
David Ahern | b1d4099 | 2019-04-16 14:35:59 -0700 | [diff] [blame] | 435 | goto out; |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 436 | |
David Ahern | 34fe5a1 | 2020-07-06 11:45:07 -0600 | [diff] [blame] | 437 | if (match->nh && have_oif_match && res->nh) |
| 438 | return; |
| 439 | |
Jakub Sitnicki | b673d6c | 2017-08-23 09:58:31 +0200 | [diff] [blame] | 440 | /* We might have already computed the hash for ICMPv6 errors. In such |
| 441 | * case it will always be non-zero. Otherwise now is the time to do it. |
| 442 | */ |
David Ahern | f88d8ea | 2019-06-03 20:19:52 -0700 | [diff] [blame] | 443 | if (!fl6->mp_hash && |
| 444 | (!match->nh || nexthop_is_multipath(match->nh))) |
David Ahern | b4bac17 | 2018-03-02 08:32:18 -0800 | [diff] [blame] | 445 | fl6->mp_hash = rt6_multipath_hash(net, fl6, skb, NULL); |
Jakub Sitnicki | b673d6c | 2017-08-23 09:58:31 +0200 | [diff] [blame] | 446 | |
David Ahern | f88d8ea | 2019-06-03 20:19:52 -0700 | [diff] [blame] | 447 | if (unlikely(match->nh)) { |
| 448 | nexthop_path_fib6_result(res, fl6->mp_hash); |
| 449 | return; |
| 450 | } |
| 451 | |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 452 | if (fl6->mp_hash <= atomic_read(&match->fib6_nh->fib_nh_upper_bound)) |
David Ahern | b1d4099 | 2019-04-16 14:35:59 -0700 | [diff] [blame] | 453 | goto out; |
Ido Schimmel | bbfcd77 | 2017-11-21 09:50:12 +0200 | [diff] [blame] | 454 | |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 455 | list_for_each_entry_safe(sibling, next_sibling, &match->fib6_siblings, |
| 456 | fib6_siblings) { |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 457 | const struct fib6_nh *nh = sibling->fib6_nh; |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 458 | int nh_upper_bound; |
| 459 | |
David Ahern | 702cea5 | 2019-04-09 14:41:13 -0700 | [diff] [blame] | 460 | nh_upper_bound = atomic_read(&nh->fib_nh_upper_bound); |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 461 | if (fl6->mp_hash > nh_upper_bound) |
Ido Schimmel | 3d709f6 | 2018-01-09 16:40:27 +0200 | [diff] [blame] | 462 | continue; |
David Ahern | 702cea5 | 2019-04-09 14:41:13 -0700 | [diff] [blame] | 463 | if (rt6_score_route(nh, sibling->fib6_flags, oif, strict) < 0) |
Ido Schimmel | 3d709f6 | 2018-01-09 16:40:27 +0200 | [diff] [blame] | 464 | break; |
| 465 | match = sibling; |
| 466 | break; |
| 467 | } |
| 468 | |
David Ahern | b1d4099 | 2019-04-16 14:35:59 -0700 | [diff] [blame] | 469 | out: |
| 470 | res->f6i = match; |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 471 | res->nh = match->fib6_nh; |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 472 | } |
| 473 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | /* |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 475 | * Route lookup. rcu_read_lock() should be held. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | */ |
| 477 | |
David Ahern | 0c59d00 | 2019-04-09 14:41:18 -0700 | [diff] [blame] | 478 | static bool __rt6_device_match(struct net *net, const struct fib6_nh *nh, |
| 479 | const struct in6_addr *saddr, int oif, int flags) |
| 480 | { |
| 481 | const struct net_device *dev; |
| 482 | |
| 483 | if (nh->fib_nh_flags & RTNH_F_DEAD) |
| 484 | return false; |
| 485 | |
| 486 | dev = nh->fib_nh_dev; |
| 487 | if (oif) { |
| 488 | if (dev->ifindex == oif) |
| 489 | return true; |
| 490 | } else { |
| 491 | if (ipv6_chk_addr(net, saddr, dev, |
| 492 | flags & RT6_LOOKUP_F_IFACE)) |
| 493 | return true; |
| 494 | } |
| 495 | |
| 496 | return false; |
| 497 | } |
| 498 | |
David Ahern | 962b680 | 2019-06-08 14:53:24 -0700 | [diff] [blame] | 499 | struct fib6_nh_dm_arg { |
| 500 | struct net *net; |
| 501 | const struct in6_addr *saddr; |
| 502 | int oif; |
| 503 | int flags; |
| 504 | struct fib6_nh *nh; |
| 505 | }; |
| 506 | |
| 507 | static int __rt6_nh_dev_match(struct fib6_nh *nh, void *_arg) |
| 508 | { |
| 509 | struct fib6_nh_dm_arg *arg = _arg; |
| 510 | |
| 511 | arg->nh = nh; |
| 512 | return __rt6_device_match(arg->net, nh, arg->saddr, arg->oif, |
| 513 | arg->flags); |
| 514 | } |
| 515 | |
| 516 | /* returns fib6_nh from nexthop or NULL */ |
| 517 | static struct fib6_nh *rt6_nh_dev_match(struct net *net, struct nexthop *nh, |
| 518 | struct fib6_result *res, |
| 519 | const struct in6_addr *saddr, |
| 520 | int oif, int flags) |
| 521 | { |
| 522 | struct fib6_nh_dm_arg arg = { |
| 523 | .net = net, |
| 524 | .saddr = saddr, |
| 525 | .oif = oif, |
| 526 | .flags = flags, |
| 527 | }; |
| 528 | |
| 529 | if (nexthop_is_blackhole(nh)) |
| 530 | return NULL; |
| 531 | |
| 532 | if (nexthop_for_each_fib6_nh(nh, __rt6_nh_dev_match, &arg)) |
| 533 | return arg.nh; |
| 534 | |
| 535 | return NULL; |
| 536 | } |
| 537 | |
David Ahern | 75ef738 | 2019-04-16 14:36:07 -0700 | [diff] [blame] | 538 | static void rt6_device_match(struct net *net, struct fib6_result *res, |
| 539 | const struct in6_addr *saddr, int oif, int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | { |
David Ahern | 75ef738 | 2019-04-16 14:36:07 -0700 | [diff] [blame] | 541 | struct fib6_info *f6i = res->f6i; |
| 542 | struct fib6_info *spf6i; |
| 543 | struct fib6_nh *nh; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | |
David Ahern | 75ef738 | 2019-04-16 14:36:07 -0700 | [diff] [blame] | 545 | if (!oif && ipv6_addr_any(saddr)) { |
David Ahern | f88d8ea | 2019-06-03 20:19:52 -0700 | [diff] [blame] | 546 | if (unlikely(f6i->nh)) { |
| 547 | nh = nexthop_fib6_nh(f6i->nh); |
| 548 | if (nexthop_is_blackhole(f6i->nh)) |
| 549 | goto out_blackhole; |
| 550 | } else { |
| 551 | nh = f6i->fib6_nh; |
| 552 | } |
David Ahern | 7d21fec | 2019-04-16 14:36:11 -0700 | [diff] [blame] | 553 | if (!(nh->fib_nh_flags & RTNH_F_DEAD)) |
| 554 | goto out; |
YOSHIFUJI Hideaki | dd3abc4 | 2008-07-02 18:30:18 +0900 | [diff] [blame] | 555 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | |
David Ahern | 75ef738 | 2019-04-16 14:36:07 -0700 | [diff] [blame] | 557 | for (spf6i = f6i; spf6i; spf6i = rcu_dereference(spf6i->fib6_next)) { |
David Ahern | 962b680 | 2019-06-08 14:53:24 -0700 | [diff] [blame] | 558 | bool matched = false; |
| 559 | |
| 560 | if (unlikely(spf6i->nh)) { |
| 561 | nh = rt6_nh_dev_match(net, spf6i->nh, res, saddr, |
| 562 | oif, flags); |
| 563 | if (nh) |
| 564 | matched = true; |
| 565 | } else { |
| 566 | nh = spf6i->fib6_nh; |
| 567 | if (__rt6_device_match(net, nh, saddr, oif, flags)) |
| 568 | matched = true; |
| 569 | } |
| 570 | if (matched) { |
David Ahern | 75ef738 | 2019-04-16 14:36:07 -0700 | [diff] [blame] | 571 | res->f6i = spf6i; |
David Ahern | 7d21fec | 2019-04-16 14:36:11 -0700 | [diff] [blame] | 572 | goto out; |
David Ahern | 75ef738 | 2019-04-16 14:36:07 -0700 | [diff] [blame] | 573 | } |
| 574 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 575 | |
David Ahern | 75ef738 | 2019-04-16 14:36:07 -0700 | [diff] [blame] | 576 | if (oif && flags & RT6_LOOKUP_F_IFACE) { |
| 577 | res->f6i = net->ipv6.fib6_null_entry; |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 578 | nh = res->f6i->fib6_nh; |
David Ahern | 7d21fec | 2019-04-16 14:36:11 -0700 | [diff] [blame] | 579 | goto out; |
David Ahern | 75ef738 | 2019-04-16 14:36:07 -0700 | [diff] [blame] | 580 | } |
| 581 | |
David Ahern | f88d8ea | 2019-06-03 20:19:52 -0700 | [diff] [blame] | 582 | if (unlikely(f6i->nh)) { |
| 583 | nh = nexthop_fib6_nh(f6i->nh); |
| 584 | if (nexthop_is_blackhole(f6i->nh)) |
| 585 | goto out_blackhole; |
| 586 | } else { |
| 587 | nh = f6i->fib6_nh; |
| 588 | } |
| 589 | |
David Ahern | 7d21fec | 2019-04-16 14:36:11 -0700 | [diff] [blame] | 590 | if (nh->fib_nh_flags & RTNH_F_DEAD) { |
David Ahern | 75ef738 | 2019-04-16 14:36:07 -0700 | [diff] [blame] | 591 | res->f6i = net->ipv6.fib6_null_entry; |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 592 | nh = res->f6i->fib6_nh; |
David Ahern | 75ef738 | 2019-04-16 14:36:07 -0700 | [diff] [blame] | 593 | } |
David Ahern | 7d21fec | 2019-04-16 14:36:11 -0700 | [diff] [blame] | 594 | out: |
| 595 | res->nh = nh; |
| 596 | res->fib6_type = res->f6i->fib6_type; |
| 597 | res->fib6_flags = res->f6i->fib6_flags; |
David Ahern | f88d8ea | 2019-06-03 20:19:52 -0700 | [diff] [blame] | 598 | return; |
| 599 | |
| 600 | out_blackhole: |
| 601 | res->fib6_flags |= RTF_REJECT; |
| 602 | res->fib6_type = RTN_BLACKHOLE; |
| 603 | res->nh = nh; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 604 | } |
| 605 | |
YOSHIFUJI Hideaki | 2709725 | 2006-03-20 17:05:13 -0800 | [diff] [blame] | 606 | #ifdef CONFIG_IPV6_ROUTER_PREF |
Hannes Frederic Sowa | c2f17e8 | 2013-10-21 06:17:15 +0200 | [diff] [blame] | 607 | struct __rt6_probe_work { |
| 608 | struct work_struct work; |
| 609 | struct in6_addr target; |
| 610 | struct net_device *dev; |
| 611 | }; |
| 612 | |
| 613 | static void rt6_probe_deferred(struct work_struct *w) |
| 614 | { |
| 615 | struct in6_addr mcaddr; |
| 616 | struct __rt6_probe_work *work = |
| 617 | container_of(w, struct __rt6_probe_work, work); |
| 618 | |
| 619 | addrconf_addr_solict_mult(&work->target, &mcaddr); |
Erik Nordmark | adc176c | 2016-12-02 14:00:08 -0800 | [diff] [blame] | 620 | ndisc_send_ns(work->dev, &work->target, &mcaddr, NULL, 0); |
Hannes Frederic Sowa | c2f17e8 | 2013-10-21 06:17:15 +0200 | [diff] [blame] | 621 | dev_put(work->dev); |
Michael Büsch | 662f553 | 2015-02-08 10:14:07 +0100 | [diff] [blame] | 622 | kfree(work); |
Hannes Frederic Sowa | c2f17e8 | 2013-10-21 06:17:15 +0200 | [diff] [blame] | 623 | } |
| 624 | |
David Ahern | cc3a86c | 2019-04-09 14:41:12 -0700 | [diff] [blame] | 625 | static void rt6_probe(struct fib6_nh *fib6_nh) |
YOSHIFUJI Hideaki | 2709725 | 2006-03-20 17:05:13 -0800 | [diff] [blame] | 626 | { |
Sabrina Dubroca | f547fac | 2018-10-12 16:22:47 +0200 | [diff] [blame] | 627 | struct __rt6_probe_work *work = NULL; |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 628 | const struct in6_addr *nh_gw; |
Eric Dumazet | 1bef4c2 | 2019-11-07 09:26:19 -0800 | [diff] [blame] | 629 | unsigned long last_probe; |
Eric Dumazet | f2c31e3 | 2011-07-29 19:00:53 +0000 | [diff] [blame] | 630 | struct neighbour *neigh; |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 631 | struct net_device *dev; |
Sabrina Dubroca | f547fac | 2018-10-12 16:22:47 +0200 | [diff] [blame] | 632 | struct inet6_dev *idev; |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 633 | |
YOSHIFUJI Hideaki | 2709725 | 2006-03-20 17:05:13 -0800 | [diff] [blame] | 634 | /* |
| 635 | * Okay, this does not seem to be appropriate |
| 636 | * for now, however, we need to check if it |
| 637 | * is really so; aka Router Reachability Probing. |
| 638 | * |
| 639 | * Router Reachability Probe MUST be rate-limited |
| 640 | * to no more than one per minute. |
| 641 | */ |
Hangbin Liu | 004b394 | 2019-11-20 15:39:06 +0800 | [diff] [blame] | 642 | if (!fib6_nh->fib_nh_gw_family) |
Amerigo Wang | fdd6681 | 2012-09-10 02:48:44 +0000 | [diff] [blame] | 643 | return; |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 644 | |
David Ahern | cc3a86c | 2019-04-09 14:41:12 -0700 | [diff] [blame] | 645 | nh_gw = &fib6_nh->fib_nh_gw6; |
| 646 | dev = fib6_nh->fib_nh_dev; |
YOSHIFUJI Hideaki / 吉藤英明 | 2152cae | 2013-01-17 12:53:43 +0000 | [diff] [blame] | 647 | rcu_read_lock_bh(); |
Eric Dumazet | 1bef4c2 | 2019-11-07 09:26:19 -0800 | [diff] [blame] | 648 | last_probe = READ_ONCE(fib6_nh->last_probe); |
Sabrina Dubroca | f547fac | 2018-10-12 16:22:47 +0200 | [diff] [blame] | 649 | idev = __in6_dev_get(dev); |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 650 | neigh = __ipv6_neigh_lookup_noref(dev, nh_gw); |
YOSHIFUJI Hideaki / 吉藤英明 | 2152cae | 2013-01-17 12:53:43 +0000 | [diff] [blame] | 651 | if (neigh) { |
Martin KaFai Lau | 8d6c31b | 2015-07-24 09:57:43 -0700 | [diff] [blame] | 652 | if (neigh->nud_state & NUD_VALID) |
| 653 | goto out; |
| 654 | |
YOSHIFUJI Hideaki / 吉藤英明 | 2152cae | 2013-01-17 12:53:43 +0000 | [diff] [blame] | 655 | write_lock(&neigh->lock); |
Martin KaFai Lau | 990edb4 | 2015-07-24 09:57:42 -0700 | [diff] [blame] | 656 | if (!(neigh->nud_state & NUD_VALID) && |
| 657 | time_after(jiffies, |
David Ahern | dcd1f57 | 2018-04-18 15:39:05 -0700 | [diff] [blame] | 658 | neigh->updated + idev->cnf.rtr_probe_interval)) { |
Martin KaFai Lau | 990edb4 | 2015-07-24 09:57:42 -0700 | [diff] [blame] | 659 | work = kmalloc(sizeof(*work), GFP_ATOMIC); |
| 660 | if (work) |
| 661 | __neigh_set_probe_once(neigh); |
Hannes Frederic Sowa | c2f17e8 | 2013-10-21 06:17:15 +0200 | [diff] [blame] | 662 | } |
YOSHIFUJI Hideaki / 吉藤英明 | 2152cae | 2013-01-17 12:53:43 +0000 | [diff] [blame] | 663 | write_unlock(&neigh->lock); |
Eric Dumazet | 1bef4c2 | 2019-11-07 09:26:19 -0800 | [diff] [blame] | 664 | } else if (time_after(jiffies, last_probe + |
Sabrina Dubroca | f547fac | 2018-10-12 16:22:47 +0200 | [diff] [blame] | 665 | idev->cnf.rtr_probe_interval)) { |
Martin KaFai Lau | 990edb4 | 2015-07-24 09:57:42 -0700 | [diff] [blame] | 666 | work = kmalloc(sizeof(*work), GFP_ATOMIC); |
Eric Dumazet | f2c31e3 | 2011-07-29 19:00:53 +0000 | [diff] [blame] | 667 | } |
Martin KaFai Lau | 990edb4 | 2015-07-24 09:57:42 -0700 | [diff] [blame] | 668 | |
Eric Dumazet | 1bef4c2 | 2019-11-07 09:26:19 -0800 | [diff] [blame] | 669 | if (!work || cmpxchg(&fib6_nh->last_probe, |
| 670 | last_probe, jiffies) != last_probe) { |
| 671 | kfree(work); |
| 672 | } else { |
Martin KaFai Lau | 990edb4 | 2015-07-24 09:57:42 -0700 | [diff] [blame] | 673 | INIT_WORK(&work->work, rt6_probe_deferred); |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 674 | work->target = *nh_gw; |
| 675 | dev_hold(dev); |
| 676 | work->dev = dev; |
Martin KaFai Lau | 990edb4 | 2015-07-24 09:57:42 -0700 | [diff] [blame] | 677 | schedule_work(&work->work); |
| 678 | } |
| 679 | |
Martin KaFai Lau | 8d6c31b | 2015-07-24 09:57:43 -0700 | [diff] [blame] | 680 | out: |
YOSHIFUJI Hideaki / 吉藤英明 | 2152cae | 2013-01-17 12:53:43 +0000 | [diff] [blame] | 681 | rcu_read_unlock_bh(); |
YOSHIFUJI Hideaki | 2709725 | 2006-03-20 17:05:13 -0800 | [diff] [blame] | 682 | } |
| 683 | #else |
David Ahern | cc3a86c | 2019-04-09 14:41:12 -0700 | [diff] [blame] | 684 | static inline void rt6_probe(struct fib6_nh *fib6_nh) |
YOSHIFUJI Hideaki | 2709725 | 2006-03-20 17:05:13 -0800 | [diff] [blame] | 685 | { |
YOSHIFUJI Hideaki | 2709725 | 2006-03-20 17:05:13 -0800 | [diff] [blame] | 686 | } |
| 687 | #endif |
| 688 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | /* |
YOSHIFUJI Hideaki | 554cfb7 | 2006-03-20 17:00:26 -0800 | [diff] [blame] | 690 | * Default Router Selection (RFC 2461 6.3.6) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 | */ |
David Ahern | 1ba9a89 | 2019-04-09 14:41:10 -0700 | [diff] [blame] | 692 | static enum rt6_nud_state rt6_check_neigh(const struct fib6_nh *fib6_nh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | { |
Hannes Frederic Sowa | afc154e | 2013-07-11 12:43:42 +0200 | [diff] [blame] | 694 | enum rt6_nud_state ret = RT6_NUD_FAIL_HARD; |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 695 | struct neighbour *neigh; |
Eric Dumazet | f2c31e3 | 2011-07-29 19:00:53 +0000 | [diff] [blame] | 696 | |
YOSHIFUJI Hideaki / 吉藤英明 | 145a362 | 2013-01-17 12:53:38 +0000 | [diff] [blame] | 697 | rcu_read_lock_bh(); |
David Ahern | 1ba9a89 | 2019-04-09 14:41:10 -0700 | [diff] [blame] | 698 | neigh = __ipv6_neigh_lookup_noref(fib6_nh->fib_nh_dev, |
| 699 | &fib6_nh->fib_nh_gw6); |
YOSHIFUJI Hideaki / 吉藤英明 | 145a362 | 2013-01-17 12:53:38 +0000 | [diff] [blame] | 700 | if (neigh) { |
| 701 | read_lock(&neigh->lock); |
YOSHIFUJI Hideaki | 554cfb7 | 2006-03-20 17:00:26 -0800 | [diff] [blame] | 702 | if (neigh->nud_state & NUD_VALID) |
Hannes Frederic Sowa | afc154e | 2013-07-11 12:43:42 +0200 | [diff] [blame] | 703 | ret = RT6_NUD_SUCCEED; |
YOSHIFUJI Hideaki | 398bcbe | 2008-01-19 00:35:16 -0800 | [diff] [blame] | 704 | #ifdef CONFIG_IPV6_ROUTER_PREF |
Paul Marks | a5a81f0 | 2012-12-03 10:26:54 +0000 | [diff] [blame] | 705 | else if (!(neigh->nud_state & NUD_FAILED)) |
Hannes Frederic Sowa | afc154e | 2013-07-11 12:43:42 +0200 | [diff] [blame] | 706 | ret = RT6_NUD_SUCCEED; |
Jiri Benc | 7e98056 | 2013-12-11 13:48:20 +0100 | [diff] [blame] | 707 | else |
| 708 | ret = RT6_NUD_FAIL_PROBE; |
YOSHIFUJI Hideaki | 398bcbe | 2008-01-19 00:35:16 -0800 | [diff] [blame] | 709 | #endif |
YOSHIFUJI Hideaki / 吉藤英明 | 145a362 | 2013-01-17 12:53:38 +0000 | [diff] [blame] | 710 | read_unlock(&neigh->lock); |
Hannes Frederic Sowa | afc154e | 2013-07-11 12:43:42 +0200 | [diff] [blame] | 711 | } else { |
| 712 | ret = IS_ENABLED(CONFIG_IPV6_ROUTER_PREF) ? |
Jiri Benc | 7e98056 | 2013-12-11 13:48:20 +0100 | [diff] [blame] | 713 | RT6_NUD_SUCCEED : RT6_NUD_FAIL_DO_RR; |
Paul Marks | a5a81f0 | 2012-12-03 10:26:54 +0000 | [diff] [blame] | 714 | } |
YOSHIFUJI Hideaki / 吉藤英明 | 145a362 | 2013-01-17 12:53:38 +0000 | [diff] [blame] | 715 | rcu_read_unlock_bh(); |
| 716 | |
Paul Marks | a5a81f0 | 2012-12-03 10:26:54 +0000 | [diff] [blame] | 717 | return ret; |
YOSHIFUJI Hideaki | 554cfb7 | 2006-03-20 17:00:26 -0800 | [diff] [blame] | 718 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 719 | |
David Ahern | 702cea5 | 2019-04-09 14:41:13 -0700 | [diff] [blame] | 720 | static int rt6_score_route(const struct fib6_nh *nh, u32 fib6_flags, int oif, |
| 721 | int strict) |
YOSHIFUJI Hideaki | 554cfb7 | 2006-03-20 17:00:26 -0800 | [diff] [blame] | 722 | { |
David Ahern | 6e1809a | 2019-04-09 14:41:11 -0700 | [diff] [blame] | 723 | int m = 0; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 724 | |
David Ahern | 6e1809a | 2019-04-09 14:41:11 -0700 | [diff] [blame] | 725 | if (!oif || nh->fib_nh_dev->ifindex == oif) |
| 726 | m = 2; |
| 727 | |
YOSHIFUJI Hideaki | 77d16f4 | 2006-08-23 17:25:05 -0700 | [diff] [blame] | 728 | if (!m && (strict & RT6_LOOKUP_F_IFACE)) |
Hannes Frederic Sowa | afc154e | 2013-07-11 12:43:42 +0200 | [diff] [blame] | 729 | return RT6_NUD_FAIL_HARD; |
YOSHIFUJI Hideaki | ebacaaa | 2006-03-20 17:04:53 -0800 | [diff] [blame] | 730 | #ifdef CONFIG_IPV6_ROUTER_PREF |
David Ahern | 702cea5 | 2019-04-09 14:41:13 -0700 | [diff] [blame] | 731 | m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(fib6_flags)) << 2; |
YOSHIFUJI Hideaki | ebacaaa | 2006-03-20 17:04:53 -0800 | [diff] [blame] | 732 | #endif |
David Ahern | 1ba9a89 | 2019-04-09 14:41:10 -0700 | [diff] [blame] | 733 | if ((strict & RT6_LOOKUP_F_REACHABLE) && |
David Ahern | 702cea5 | 2019-04-09 14:41:13 -0700 | [diff] [blame] | 734 | !(fib6_flags & RTF_NONEXTHOP) && nh->fib_nh_gw_family) { |
David Ahern | 1ba9a89 | 2019-04-09 14:41:10 -0700 | [diff] [blame] | 735 | int n = rt6_check_neigh(nh); |
Hannes Frederic Sowa | afc154e | 2013-07-11 12:43:42 +0200 | [diff] [blame] | 736 | if (n < 0) |
| 737 | return n; |
| 738 | } |
YOSHIFUJI Hideaki | 554cfb7 | 2006-03-20 17:00:26 -0800 | [diff] [blame] | 739 | return m; |
| 740 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 741 | |
David Ahern | 28679ed | 2019-04-09 14:41:14 -0700 | [diff] [blame] | 742 | static bool find_match(struct fib6_nh *nh, u32 fib6_flags, |
| 743 | int oif, int strict, int *mpri, bool *do_rr) |
YOSHIFUJI Hideaki | 554cfb7 | 2006-03-20 17:00:26 -0800 | [diff] [blame] | 744 | { |
Hannes Frederic Sowa | afc154e | 2013-07-11 12:43:42 +0200 | [diff] [blame] | 745 | bool match_do_rr = false; |
David Ahern | 28679ed | 2019-04-09 14:41:14 -0700 | [diff] [blame] | 746 | bool rc = false; |
| 747 | int m; |
Andy Gospodarek | 35103d1 | 2015-08-13 10:39:01 -0400 | [diff] [blame] | 748 | |
David Ahern | 28679ed | 2019-04-09 14:41:14 -0700 | [diff] [blame] | 749 | if (nh->fib_nh_flags & RTNH_F_DEAD) |
Ido Schimmel | 8067bb8 | 2018-01-07 12:45:09 +0200 | [diff] [blame] | 750 | goto out; |
| 751 | |
David Ahern | 28679ed | 2019-04-09 14:41:14 -0700 | [diff] [blame] | 752 | if (ip6_ignore_linkdown(nh->fib_nh_dev) && |
| 753 | nh->fib_nh_flags & RTNH_F_LINKDOWN && |
David Ahern | d5d32e4 | 2016-10-24 12:27:23 -0700 | [diff] [blame] | 754 | !(strict & RT6_LOOKUP_F_IGNORE_LINKSTATE)) |
Andy Gospodarek | 35103d1 | 2015-08-13 10:39:01 -0400 | [diff] [blame] | 755 | goto out; |
David S. Miller | f11e665 | 2007-03-24 20:36:25 -0700 | [diff] [blame] | 756 | |
David Ahern | 28679ed | 2019-04-09 14:41:14 -0700 | [diff] [blame] | 757 | m = rt6_score_route(nh, fib6_flags, oif, strict); |
Jiri Benc | 7e98056 | 2013-12-11 13:48:20 +0100 | [diff] [blame] | 758 | if (m == RT6_NUD_FAIL_DO_RR) { |
Hannes Frederic Sowa | afc154e | 2013-07-11 12:43:42 +0200 | [diff] [blame] | 759 | match_do_rr = true; |
| 760 | m = 0; /* lowest valid score */ |
Jiri Benc | 7e98056 | 2013-12-11 13:48:20 +0100 | [diff] [blame] | 761 | } else if (m == RT6_NUD_FAIL_HARD) { |
David S. Miller | f11e665 | 2007-03-24 20:36:25 -0700 | [diff] [blame] | 762 | goto out; |
David S. Miller | f11e665 | 2007-03-24 20:36:25 -0700 | [diff] [blame] | 763 | } |
| 764 | |
Hannes Frederic Sowa | afc154e | 2013-07-11 12:43:42 +0200 | [diff] [blame] | 765 | if (strict & RT6_LOOKUP_F_REACHABLE) |
David Ahern | 28679ed | 2019-04-09 14:41:14 -0700 | [diff] [blame] | 766 | rt6_probe(nh); |
Hannes Frederic Sowa | afc154e | 2013-07-11 12:43:42 +0200 | [diff] [blame] | 767 | |
Jiri Benc | 7e98056 | 2013-12-11 13:48:20 +0100 | [diff] [blame] | 768 | /* note that m can be RT6_NUD_FAIL_PROBE at this point */ |
Hannes Frederic Sowa | afc154e | 2013-07-11 12:43:42 +0200 | [diff] [blame] | 769 | if (m > *mpri) { |
| 770 | *do_rr = match_do_rr; |
| 771 | *mpri = m; |
David Ahern | 28679ed | 2019-04-09 14:41:14 -0700 | [diff] [blame] | 772 | rc = true; |
Hannes Frederic Sowa | afc154e | 2013-07-11 12:43:42 +0200 | [diff] [blame] | 773 | } |
David S. Miller | f11e665 | 2007-03-24 20:36:25 -0700 | [diff] [blame] | 774 | out: |
David Ahern | 28679ed | 2019-04-09 14:41:14 -0700 | [diff] [blame] | 775 | return rc; |
David S. Miller | f11e665 | 2007-03-24 20:36:25 -0700 | [diff] [blame] | 776 | } |
| 777 | |
David Ahern | 17a5984 | 2019-06-08 14:53:25 -0700 | [diff] [blame] | 778 | struct fib6_nh_frl_arg { |
| 779 | u32 flags; |
| 780 | int oif; |
| 781 | int strict; |
| 782 | int *mpri; |
| 783 | bool *do_rr; |
| 784 | struct fib6_nh *nh; |
| 785 | }; |
| 786 | |
| 787 | static int rt6_nh_find_match(struct fib6_nh *nh, void *_arg) |
| 788 | { |
| 789 | struct fib6_nh_frl_arg *arg = _arg; |
| 790 | |
| 791 | arg->nh = nh; |
| 792 | return find_match(nh, arg->flags, arg->oif, arg->strict, |
| 793 | arg->mpri, arg->do_rr); |
| 794 | } |
| 795 | |
David Ahern | b7bc4b6 | 2019-04-16 14:36:08 -0700 | [diff] [blame] | 796 | static void __find_rr_leaf(struct fib6_info *f6i_start, |
David Ahern | 30c15f0 | 2019-04-09 14:41:15 -0700 | [diff] [blame] | 797 | struct fib6_info *nomatch, u32 metric, |
David Ahern | b7bc4b6 | 2019-04-16 14:36:08 -0700 | [diff] [blame] | 798 | struct fib6_result *res, struct fib6_info **cont, |
David Ahern | 30c15f0 | 2019-04-09 14:41:15 -0700 | [diff] [blame] | 799 | int oif, int strict, bool *do_rr, int *mpri) |
David S. Miller | f11e665 | 2007-03-24 20:36:25 -0700 | [diff] [blame] | 800 | { |
David Ahern | b7bc4b6 | 2019-04-16 14:36:08 -0700 | [diff] [blame] | 801 | struct fib6_info *f6i; |
David Ahern | 30c15f0 | 2019-04-09 14:41:15 -0700 | [diff] [blame] | 802 | |
David Ahern | b7bc4b6 | 2019-04-16 14:36:08 -0700 | [diff] [blame] | 803 | for (f6i = f6i_start; |
| 804 | f6i && f6i != nomatch; |
| 805 | f6i = rcu_dereference(f6i->fib6_next)) { |
David Ahern | 17a5984 | 2019-06-08 14:53:25 -0700 | [diff] [blame] | 806 | bool matched = false; |
David Ahern | 30c15f0 | 2019-04-09 14:41:15 -0700 | [diff] [blame] | 807 | struct fib6_nh *nh; |
| 808 | |
David Ahern | b7bc4b6 | 2019-04-16 14:36:08 -0700 | [diff] [blame] | 809 | if (cont && f6i->fib6_metric != metric) { |
| 810 | *cont = f6i; |
David Ahern | 30c15f0 | 2019-04-09 14:41:15 -0700 | [diff] [blame] | 811 | return; |
| 812 | } |
| 813 | |
David Ahern | b7bc4b6 | 2019-04-16 14:36:08 -0700 | [diff] [blame] | 814 | if (fib6_check_expired(f6i)) |
David Ahern | 30c15f0 | 2019-04-09 14:41:15 -0700 | [diff] [blame] | 815 | continue; |
| 816 | |
David Ahern | 17a5984 | 2019-06-08 14:53:25 -0700 | [diff] [blame] | 817 | if (unlikely(f6i->nh)) { |
| 818 | struct fib6_nh_frl_arg arg = { |
| 819 | .flags = f6i->fib6_flags, |
| 820 | .oif = oif, |
| 821 | .strict = strict, |
| 822 | .mpri = mpri, |
| 823 | .do_rr = do_rr |
| 824 | }; |
| 825 | |
| 826 | if (nexthop_is_blackhole(f6i->nh)) { |
| 827 | res->fib6_flags = RTF_REJECT; |
| 828 | res->fib6_type = RTN_BLACKHOLE; |
| 829 | res->f6i = f6i; |
| 830 | res->nh = nexthop_fib6_nh(f6i->nh); |
| 831 | return; |
| 832 | } |
| 833 | if (nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_find_match, |
| 834 | &arg)) { |
| 835 | matched = true; |
| 836 | nh = arg.nh; |
| 837 | } |
| 838 | } else { |
| 839 | nh = f6i->fib6_nh; |
| 840 | if (find_match(nh, f6i->fib6_flags, oif, strict, |
| 841 | mpri, do_rr)) |
| 842 | matched = true; |
| 843 | } |
| 844 | if (matched) { |
David Ahern | b7bc4b6 | 2019-04-16 14:36:08 -0700 | [diff] [blame] | 845 | res->f6i = f6i; |
| 846 | res->nh = nh; |
David Ahern | 7d21fec | 2019-04-16 14:36:11 -0700 | [diff] [blame] | 847 | res->fib6_flags = f6i->fib6_flags; |
| 848 | res->fib6_type = f6i->fib6_type; |
David Ahern | b7bc4b6 | 2019-04-16 14:36:08 -0700 | [diff] [blame] | 849 | } |
David Ahern | 30c15f0 | 2019-04-09 14:41:15 -0700 | [diff] [blame] | 850 | } |
| 851 | } |
| 852 | |
David Ahern | b7bc4b6 | 2019-04-16 14:36:08 -0700 | [diff] [blame] | 853 | static void find_rr_leaf(struct fib6_node *fn, struct fib6_info *leaf, |
| 854 | struct fib6_info *rr_head, int oif, int strict, |
| 855 | bool *do_rr, struct fib6_result *res) |
David Ahern | 30c15f0 | 2019-04-09 14:41:15 -0700 | [diff] [blame] | 856 | { |
David Ahern | b7bc4b6 | 2019-04-16 14:36:08 -0700 | [diff] [blame] | 857 | u32 metric = rr_head->fib6_metric; |
| 858 | struct fib6_info *cont = NULL; |
YOSHIFUJI Hideaki | 554cfb7 | 2006-03-20 17:00:26 -0800 | [diff] [blame] | 859 | int mpri = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 860 | |
David Ahern | b7bc4b6 | 2019-04-16 14:36:08 -0700 | [diff] [blame] | 861 | __find_rr_leaf(rr_head, NULL, metric, res, &cont, |
David Ahern | 30c15f0 | 2019-04-09 14:41:15 -0700 | [diff] [blame] | 862 | oif, strict, do_rr, &mpri); |
Steffen Klassert | 9fbdcfa | 2015-04-28 13:03:04 -0700 | [diff] [blame] | 863 | |
David Ahern | b7bc4b6 | 2019-04-16 14:36:08 -0700 | [diff] [blame] | 864 | __find_rr_leaf(leaf, rr_head, metric, res, &cont, |
David Ahern | 30c15f0 | 2019-04-09 14:41:15 -0700 | [diff] [blame] | 865 | oif, strict, do_rr, &mpri); |
Steffen Klassert | 9fbdcfa | 2015-04-28 13:03:04 -0700 | [diff] [blame] | 866 | |
David Ahern | b7bc4b6 | 2019-04-16 14:36:08 -0700 | [diff] [blame] | 867 | if (res->f6i || !cont) |
| 868 | return; |
Steffen Klassert | 9fbdcfa | 2015-04-28 13:03:04 -0700 | [diff] [blame] | 869 | |
David Ahern | b7bc4b6 | 2019-04-16 14:36:08 -0700 | [diff] [blame] | 870 | __find_rr_leaf(cont, NULL, metric, res, NULL, |
David Ahern | 30c15f0 | 2019-04-09 14:41:15 -0700 | [diff] [blame] | 871 | oif, strict, do_rr, &mpri); |
David S. Miller | f11e665 | 2007-03-24 20:36:25 -0700 | [diff] [blame] | 872 | } |
YOSHIFUJI Hideaki | 554cfb7 | 2006-03-20 17:00:26 -0800 | [diff] [blame] | 873 | |
David Ahern | b7bc4b6 | 2019-04-16 14:36:08 -0700 | [diff] [blame] | 874 | static void rt6_select(struct net *net, struct fib6_node *fn, int oif, |
| 875 | struct fib6_result *res, int strict) |
David S. Miller | f11e665 | 2007-03-24 20:36:25 -0700 | [diff] [blame] | 876 | { |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 877 | struct fib6_info *leaf = rcu_dereference(fn->leaf); |
David Ahern | b7bc4b6 | 2019-04-16 14:36:08 -0700 | [diff] [blame] | 878 | struct fib6_info *rt0; |
Hannes Frederic Sowa | afc154e | 2013-07-11 12:43:42 +0200 | [diff] [blame] | 879 | bool do_rr = false; |
Wei Wang | 17ecf59 | 2017-10-06 12:06:09 -0700 | [diff] [blame] | 880 | int key_plen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 881 | |
David Ahern | b7bc4b6 | 2019-04-16 14:36:08 -0700 | [diff] [blame] | 882 | /* make sure this function or its helpers sets f6i */ |
| 883 | res->f6i = NULL; |
| 884 | |
David Ahern | 421842e | 2018-04-17 17:33:18 -0700 | [diff] [blame] | 885 | if (!leaf || leaf == net->ipv6.fib6_null_entry) |
David Ahern | b7bc4b6 | 2019-04-16 14:36:08 -0700 | [diff] [blame] | 886 | goto out; |
Wei Wang | 8d1040e | 2017-10-06 12:06:08 -0700 | [diff] [blame] | 887 | |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 888 | rt0 = rcu_dereference(fn->rr_ptr); |
David S. Miller | f11e665 | 2007-03-24 20:36:25 -0700 | [diff] [blame] | 889 | if (!rt0) |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 890 | rt0 = leaf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 891 | |
Wei Wang | 17ecf59 | 2017-10-06 12:06:09 -0700 | [diff] [blame] | 892 | /* Double check to make sure fn is not an intermediate node |
| 893 | * and fn->leaf does not points to its child's leaf |
| 894 | * (This might happen if all routes under fn are deleted from |
| 895 | * the tree and fib6_repair_tree() is called on the node.) |
| 896 | */ |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 897 | key_plen = rt0->fib6_dst.plen; |
Wei Wang | 17ecf59 | 2017-10-06 12:06:09 -0700 | [diff] [blame] | 898 | #ifdef CONFIG_IPV6_SUBTREES |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 899 | if (rt0->fib6_src.plen) |
| 900 | key_plen = rt0->fib6_src.plen; |
Wei Wang | 17ecf59 | 2017-10-06 12:06:09 -0700 | [diff] [blame] | 901 | #endif |
| 902 | if (fn->fn_bit != key_plen) |
David Ahern | b7bc4b6 | 2019-04-16 14:36:08 -0700 | [diff] [blame] | 903 | goto out; |
Wei Wang | 17ecf59 | 2017-10-06 12:06:09 -0700 | [diff] [blame] | 904 | |
David Ahern | b7bc4b6 | 2019-04-16 14:36:08 -0700 | [diff] [blame] | 905 | find_rr_leaf(fn, leaf, rt0, oif, strict, &do_rr, res); |
Hannes Frederic Sowa | afc154e | 2013-07-11 12:43:42 +0200 | [diff] [blame] | 906 | if (do_rr) { |
David Ahern | 8fb11a9 | 2018-05-04 13:54:24 -0700 | [diff] [blame] | 907 | struct fib6_info *next = rcu_dereference(rt0->fib6_next); |
David S. Miller | f11e665 | 2007-03-24 20:36:25 -0700 | [diff] [blame] | 908 | |
YOSHIFUJI Hideaki | 554cfb7 | 2006-03-20 17:00:26 -0800 | [diff] [blame] | 909 | /* no entries matched; do round-robin */ |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 910 | if (!next || next->fib6_metric != rt0->fib6_metric) |
Wei Wang | 8d1040e | 2017-10-06 12:06:08 -0700 | [diff] [blame] | 911 | next = leaf; |
David S. Miller | f11e665 | 2007-03-24 20:36:25 -0700 | [diff] [blame] | 912 | |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 913 | if (next != rt0) { |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 914 | spin_lock_bh(&leaf->fib6_table->tb6_lock); |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 915 | /* make sure next is not being deleted from the tree */ |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 916 | if (next->fib6_node) |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 917 | rcu_assign_pointer(fn->rr_ptr, next); |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 918 | spin_unlock_bh(&leaf->fib6_table->tb6_lock); |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 919 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 920 | } |
| 921 | |
David Ahern | b7bc4b6 | 2019-04-16 14:36:08 -0700 | [diff] [blame] | 922 | out: |
| 923 | if (!res->f6i) { |
| 924 | res->f6i = net->ipv6.fib6_null_entry; |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 925 | res->nh = res->f6i->fib6_nh; |
David Ahern | 7d21fec | 2019-04-16 14:36:11 -0700 | [diff] [blame] | 926 | res->fib6_flags = res->f6i->fib6_flags; |
| 927 | res->fib6_type = res->f6i->fib6_type; |
David Ahern | b7bc4b6 | 2019-04-16 14:36:08 -0700 | [diff] [blame] | 928 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 929 | } |
| 930 | |
David Ahern | 85bd05d | 2019-04-16 14:36:01 -0700 | [diff] [blame] | 931 | static bool rt6_is_gw_or_nonexthop(const struct fib6_result *res) |
Martin KaFai Lau | 8b9df26 | 2015-05-22 20:55:59 -0700 | [diff] [blame] | 932 | { |
David Ahern | 85bd05d | 2019-04-16 14:36:01 -0700 | [diff] [blame] | 933 | return (res->f6i->fib6_flags & RTF_NONEXTHOP) || |
| 934 | res->nh->fib_nh_gw_family; |
Martin KaFai Lau | 8b9df26 | 2015-05-22 20:55:59 -0700 | [diff] [blame] | 935 | } |
| 936 | |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 937 | #ifdef CONFIG_IPV6_ROUTE_INFO |
| 938 | int rt6_route_rcv(struct net_device *dev, u8 *opt, int len, |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 939 | const struct in6_addr *gwaddr) |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 940 | { |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 941 | struct net *net = dev_net(dev); |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 942 | struct route_info *rinfo = (struct route_info *) opt; |
| 943 | struct in6_addr prefix_buf, *prefix; |
| 944 | unsigned int pref; |
YOSHIFUJI Hideaki | 4bed72e | 2008-05-27 17:37:49 +0900 | [diff] [blame] | 945 | unsigned long lifetime; |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 946 | struct fib6_info *rt; |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 947 | |
| 948 | if (len < sizeof(struct route_info)) { |
| 949 | return -EINVAL; |
| 950 | } |
| 951 | |
| 952 | /* Sanity check for prefix_len and length */ |
| 953 | if (rinfo->length > 3) { |
| 954 | return -EINVAL; |
| 955 | } else if (rinfo->prefix_len > 128) { |
| 956 | return -EINVAL; |
| 957 | } else if (rinfo->prefix_len > 64) { |
| 958 | if (rinfo->length < 2) { |
| 959 | return -EINVAL; |
| 960 | } |
| 961 | } else if (rinfo->prefix_len > 0) { |
| 962 | if (rinfo->length < 1) { |
| 963 | return -EINVAL; |
| 964 | } |
| 965 | } |
| 966 | |
| 967 | pref = rinfo->route_pref; |
| 968 | if (pref == ICMPV6_ROUTER_PREF_INVALID) |
Jens Rosenboom | 3933fc9 | 2009-09-10 06:25:11 +0000 | [diff] [blame] | 969 | return -EINVAL; |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 970 | |
YOSHIFUJI Hideaki | 4bed72e | 2008-05-27 17:37:49 +0900 | [diff] [blame] | 971 | lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ); |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 972 | |
| 973 | if (rinfo->length == 3) |
| 974 | prefix = (struct in6_addr *)rinfo->prefix; |
| 975 | else { |
| 976 | /* this function is safe */ |
| 977 | ipv6_addr_prefix(&prefix_buf, |
| 978 | (struct in6_addr *)rinfo->prefix, |
| 979 | rinfo->prefix_len); |
| 980 | prefix = &prefix_buf; |
| 981 | } |
| 982 | |
Duan Jiong | f104a56 | 2013-11-08 09:56:53 +0800 | [diff] [blame] | 983 | if (rinfo->prefix_len == 0) |
David Ahern | afb1d4b5 | 2018-04-17 17:33:11 -0700 | [diff] [blame] | 984 | rt = rt6_get_dflt_router(net, gwaddr, dev); |
Duan Jiong | f104a56 | 2013-11-08 09:56:53 +0800 | [diff] [blame] | 985 | else |
| 986 | rt = rt6_get_route_info(net, prefix, rinfo->prefix_len, |
David Ahern | 830218c | 2016-10-24 10:52:35 -0700 | [diff] [blame] | 987 | gwaddr, dev); |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 988 | |
| 989 | if (rt && !lifetime) { |
Roopa Prabhu | 11dd74b | 2020-04-27 13:56:45 -0700 | [diff] [blame] | 990 | ip6_del_rt(net, rt, false); |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 991 | rt = NULL; |
| 992 | } |
| 993 | |
| 994 | if (!rt && lifetime) |
David Ahern | 830218c | 2016-10-24 10:52:35 -0700 | [diff] [blame] | 995 | rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr, |
| 996 | dev, pref); |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 997 | else if (rt) |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 998 | rt->fib6_flags = RTF_ROUTEINFO | |
| 999 | (rt->fib6_flags & ~RTF_PREF_MASK) | RTF_PREF(pref); |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 1000 | |
| 1001 | if (rt) { |
Gao feng | 1716a96 | 2012-04-06 00:13:10 +0000 | [diff] [blame] | 1002 | if (!addrconf_finite_timeout(lifetime)) |
David Ahern | 1489568 | 2018-04-17 17:33:17 -0700 | [diff] [blame] | 1003 | fib6_clean_expires(rt); |
Gao feng | 1716a96 | 2012-04-06 00:13:10 +0000 | [diff] [blame] | 1004 | else |
David Ahern | 1489568 | 2018-04-17 17:33:17 -0700 | [diff] [blame] | 1005 | fib6_set_expires(rt, jiffies + HZ * lifetime); |
Gao feng | 1716a96 | 2012-04-06 00:13:10 +0000 | [diff] [blame] | 1006 | |
David Ahern | 93531c6 | 2018-04-17 17:33:25 -0700 | [diff] [blame] | 1007 | fib6_info_release(rt); |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 1008 | } |
| 1009 | return 0; |
| 1010 | } |
| 1011 | #endif |
| 1012 | |
David Ahern | ae90d86 | 2018-04-17 17:33:12 -0700 | [diff] [blame] | 1013 | /* |
| 1014 | * Misc support functions |
| 1015 | */ |
| 1016 | |
| 1017 | /* called with rcu_lock held */ |
David Ahern | 0d16158 | 2019-04-16 14:36:04 -0700 | [diff] [blame] | 1018 | static struct net_device *ip6_rt_get_dev_rcu(const struct fib6_result *res) |
David Ahern | ae90d86 | 2018-04-17 17:33:12 -0700 | [diff] [blame] | 1019 | { |
David Ahern | 0d16158 | 2019-04-16 14:36:04 -0700 | [diff] [blame] | 1020 | struct net_device *dev = res->nh->fib_nh_dev; |
David Ahern | ae90d86 | 2018-04-17 17:33:12 -0700 | [diff] [blame] | 1021 | |
David Ahern | 7d21fec | 2019-04-16 14:36:11 -0700 | [diff] [blame] | 1022 | if (res->fib6_flags & (RTF_LOCAL | RTF_ANYCAST)) { |
David Ahern | ae90d86 | 2018-04-17 17:33:12 -0700 | [diff] [blame] | 1023 | /* for copies of local routes, dst->dev needs to be the |
| 1024 | * device if it is a master device, the master device if |
| 1025 | * device is enslaved, and the loopback as the default |
| 1026 | */ |
| 1027 | if (netif_is_l3_slave(dev) && |
David Ahern | 7d21fec | 2019-04-16 14:36:11 -0700 | [diff] [blame] | 1028 | !rt6_need_strict(&res->f6i->fib6_dst.addr)) |
David Ahern | ae90d86 | 2018-04-17 17:33:12 -0700 | [diff] [blame] | 1029 | dev = l3mdev_master_dev_rcu(dev); |
| 1030 | else if (!netif_is_l3_master(dev)) |
| 1031 | dev = dev_net(dev)->loopback_dev; |
| 1032 | /* last case is netif_is_l3_master(dev) is true in which |
| 1033 | * case we want dev returned to be dev |
| 1034 | */ |
| 1035 | } |
| 1036 | |
| 1037 | return dev; |
| 1038 | } |
| 1039 | |
David Ahern | 6edb3c9 | 2018-04-17 17:33:15 -0700 | [diff] [blame] | 1040 | static const int fib6_prop[RTN_MAX + 1] = { |
| 1041 | [RTN_UNSPEC] = 0, |
| 1042 | [RTN_UNICAST] = 0, |
| 1043 | [RTN_LOCAL] = 0, |
| 1044 | [RTN_BROADCAST] = 0, |
| 1045 | [RTN_ANYCAST] = 0, |
| 1046 | [RTN_MULTICAST] = 0, |
| 1047 | [RTN_BLACKHOLE] = -EINVAL, |
| 1048 | [RTN_UNREACHABLE] = -EHOSTUNREACH, |
| 1049 | [RTN_PROHIBIT] = -EACCES, |
| 1050 | [RTN_THROW] = -EAGAIN, |
| 1051 | [RTN_NAT] = -EINVAL, |
| 1052 | [RTN_XRESOLVE] = -EINVAL, |
| 1053 | }; |
| 1054 | |
| 1055 | static int ip6_rt_type_to_error(u8 fib6_type) |
| 1056 | { |
| 1057 | return fib6_prop[fib6_type]; |
| 1058 | } |
| 1059 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 1060 | static unsigned short fib6_info_dst_flags(struct fib6_info *rt) |
David Ahern | 3b6761d | 2018-04-17 17:33:20 -0700 | [diff] [blame] | 1061 | { |
| 1062 | unsigned short flags = 0; |
| 1063 | |
| 1064 | if (rt->dst_nocount) |
| 1065 | flags |= DST_NOCOUNT; |
| 1066 | if (rt->dst_nopolicy) |
| 1067 | flags |= DST_NOPOLICY; |
David Ahern | 3b6761d | 2018-04-17 17:33:20 -0700 | [diff] [blame] | 1068 | |
| 1069 | return flags; |
| 1070 | } |
| 1071 | |
David Ahern | 7d21fec | 2019-04-16 14:36:11 -0700 | [diff] [blame] | 1072 | static void ip6_rt_init_dst_reject(struct rt6_info *rt, u8 fib6_type) |
David Ahern | 6edb3c9 | 2018-04-17 17:33:15 -0700 | [diff] [blame] | 1073 | { |
David Ahern | 7d21fec | 2019-04-16 14:36:11 -0700 | [diff] [blame] | 1074 | rt->dst.error = ip6_rt_type_to_error(fib6_type); |
David Ahern | 6edb3c9 | 2018-04-17 17:33:15 -0700 | [diff] [blame] | 1075 | |
David Ahern | 7d21fec | 2019-04-16 14:36:11 -0700 | [diff] [blame] | 1076 | switch (fib6_type) { |
David Ahern | 6edb3c9 | 2018-04-17 17:33:15 -0700 | [diff] [blame] | 1077 | case RTN_BLACKHOLE: |
| 1078 | rt->dst.output = dst_discard_out; |
| 1079 | rt->dst.input = dst_discard; |
| 1080 | break; |
| 1081 | case RTN_PROHIBIT: |
| 1082 | rt->dst.output = ip6_pkt_prohibit_out; |
| 1083 | rt->dst.input = ip6_pkt_prohibit; |
| 1084 | break; |
| 1085 | case RTN_THROW: |
| 1086 | case RTN_UNREACHABLE: |
| 1087 | default: |
| 1088 | rt->dst.output = ip6_pkt_discard_out; |
| 1089 | rt->dst.input = ip6_pkt_discard; |
| 1090 | break; |
| 1091 | } |
| 1092 | } |
| 1093 | |
David Ahern | 0d16158 | 2019-04-16 14:36:04 -0700 | [diff] [blame] | 1094 | static void ip6_rt_init_dst(struct rt6_info *rt, const struct fib6_result *res) |
David Ahern | 6edb3c9 | 2018-04-17 17:33:15 -0700 | [diff] [blame] | 1095 | { |
David Ahern | 7d21fec | 2019-04-16 14:36:11 -0700 | [diff] [blame] | 1096 | struct fib6_info *f6i = res->f6i; |
David Ahern | 0d16158 | 2019-04-16 14:36:04 -0700 | [diff] [blame] | 1097 | |
David Ahern | 7d21fec | 2019-04-16 14:36:11 -0700 | [diff] [blame] | 1098 | if (res->fib6_flags & RTF_REJECT) { |
| 1099 | ip6_rt_init_dst_reject(rt, res->fib6_type); |
David Ahern | 6edb3c9 | 2018-04-17 17:33:15 -0700 | [diff] [blame] | 1100 | return; |
| 1101 | } |
| 1102 | |
| 1103 | rt->dst.error = 0; |
| 1104 | rt->dst.output = ip6_output; |
| 1105 | |
David Ahern | 7d21fec | 2019-04-16 14:36:11 -0700 | [diff] [blame] | 1106 | if (res->fib6_type == RTN_LOCAL || res->fib6_type == RTN_ANYCAST) { |
David Ahern | 6edb3c9 | 2018-04-17 17:33:15 -0700 | [diff] [blame] | 1107 | rt->dst.input = ip6_input; |
David Ahern | 7d21fec | 2019-04-16 14:36:11 -0700 | [diff] [blame] | 1108 | } else if (ipv6_addr_type(&f6i->fib6_dst.addr) & IPV6_ADDR_MULTICAST) { |
David Ahern | 6edb3c9 | 2018-04-17 17:33:15 -0700 | [diff] [blame] | 1109 | rt->dst.input = ip6_mc_input; |
| 1110 | } else { |
| 1111 | rt->dst.input = ip6_forward; |
| 1112 | } |
| 1113 | |
David Ahern | 0d16158 | 2019-04-16 14:36:04 -0700 | [diff] [blame] | 1114 | if (res->nh->fib_nh_lws) { |
| 1115 | rt->dst.lwtstate = lwtstate_get(res->nh->fib_nh_lws); |
David Ahern | 6edb3c9 | 2018-04-17 17:33:15 -0700 | [diff] [blame] | 1116 | lwtunnel_set_redirect(&rt->dst); |
| 1117 | } |
| 1118 | |
| 1119 | rt->dst.lastuse = jiffies; |
| 1120 | } |
| 1121 | |
Wei Wang | e873e4b | 2018-07-21 20:56:32 -0700 | [diff] [blame] | 1122 | /* Caller must already hold reference to @from */ |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 1123 | static void rt6_set_from(struct rt6_info *rt, struct fib6_info *from) |
David Ahern | ae90d86 | 2018-04-17 17:33:12 -0700 | [diff] [blame] | 1124 | { |
David Ahern | ae90d86 | 2018-04-17 17:33:12 -0700 | [diff] [blame] | 1125 | rt->rt6i_flags &= ~RTF_EXPIRES; |
David Ahern | a68886a | 2018-04-20 15:38:02 -0700 | [diff] [blame] | 1126 | rcu_assign_pointer(rt->from, from); |
David Ahern | e1255ed | 2018-10-04 20:07:53 -0700 | [diff] [blame] | 1127 | ip_dst_init_metrics(&rt->dst, from->fib6_metrics); |
David Ahern | ae90d86 | 2018-04-17 17:33:12 -0700 | [diff] [blame] | 1128 | } |
| 1129 | |
David Ahern | 0d16158 | 2019-04-16 14:36:04 -0700 | [diff] [blame] | 1130 | /* Caller must already hold reference to f6i in result */ |
| 1131 | static void ip6_rt_copy_init(struct rt6_info *rt, const struct fib6_result *res) |
David Ahern | ae90d86 | 2018-04-17 17:33:12 -0700 | [diff] [blame] | 1132 | { |
David Ahern | 0d16158 | 2019-04-16 14:36:04 -0700 | [diff] [blame] | 1133 | const struct fib6_nh *nh = res->nh; |
| 1134 | const struct net_device *dev = nh->fib_nh_dev; |
| 1135 | struct fib6_info *f6i = res->f6i; |
David Ahern | dcd1f57 | 2018-04-18 15:39:05 -0700 | [diff] [blame] | 1136 | |
David Ahern | 0d16158 | 2019-04-16 14:36:04 -0700 | [diff] [blame] | 1137 | ip6_rt_init_dst(rt, res); |
David Ahern | 6edb3c9 | 2018-04-17 17:33:15 -0700 | [diff] [blame] | 1138 | |
David Ahern | 0d16158 | 2019-04-16 14:36:04 -0700 | [diff] [blame] | 1139 | rt->rt6i_dst = f6i->fib6_dst; |
David Ahern | dcd1f57 | 2018-04-18 15:39:05 -0700 | [diff] [blame] | 1140 | rt->rt6i_idev = dev ? in6_dev_get(dev) : NULL; |
David Ahern | 7d21fec | 2019-04-16 14:36:11 -0700 | [diff] [blame] | 1141 | rt->rt6i_flags = res->fib6_flags; |
David Ahern | 0d16158 | 2019-04-16 14:36:04 -0700 | [diff] [blame] | 1142 | if (nh->fib_nh_gw_family) { |
| 1143 | rt->rt6i_gateway = nh->fib_nh_gw6; |
David Ahern | 2b2450c | 2019-03-27 20:53:52 -0700 | [diff] [blame] | 1144 | rt->rt6i_flags |= RTF_GATEWAY; |
| 1145 | } |
David Ahern | 0d16158 | 2019-04-16 14:36:04 -0700 | [diff] [blame] | 1146 | rt6_set_from(rt, f6i); |
David Ahern | ae90d86 | 2018-04-17 17:33:12 -0700 | [diff] [blame] | 1147 | #ifdef CONFIG_IPV6_SUBTREES |
David Ahern | 0d16158 | 2019-04-16 14:36:04 -0700 | [diff] [blame] | 1148 | rt->rt6i_src = f6i->fib6_src; |
David Ahern | ae90d86 | 2018-04-17 17:33:12 -0700 | [diff] [blame] | 1149 | #endif |
David Ahern | ae90d86 | 2018-04-17 17:33:12 -0700 | [diff] [blame] | 1150 | } |
| 1151 | |
Martin KaFai Lau | a3c00e4 | 2014-10-20 13:42:43 -0700 | [diff] [blame] | 1152 | static struct fib6_node* fib6_backtrack(struct fib6_node *fn, |
| 1153 | struct in6_addr *saddr) |
| 1154 | { |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 1155 | struct fib6_node *pn, *sn; |
Martin KaFai Lau | a3c00e4 | 2014-10-20 13:42:43 -0700 | [diff] [blame] | 1156 | while (1) { |
| 1157 | if (fn->fn_flags & RTN_TL_ROOT) |
| 1158 | return NULL; |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 1159 | pn = rcu_dereference(fn->parent); |
| 1160 | sn = FIB6_SUBTREE(pn); |
| 1161 | if (sn && sn != fn) |
David Ahern | 6454743 | 2018-05-09 20:34:19 -0700 | [diff] [blame] | 1162 | fn = fib6_node_lookup(sn, NULL, saddr); |
Martin KaFai Lau | a3c00e4 | 2014-10-20 13:42:43 -0700 | [diff] [blame] | 1163 | else |
| 1164 | fn = pn; |
| 1165 | if (fn->fn_flags & RTN_RTINFO) |
| 1166 | return fn; |
| 1167 | } |
| 1168 | } |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1169 | |
David Ahern | 10585b4 | 2019-03-20 09:24:50 -0700 | [diff] [blame] | 1170 | static bool ip6_hold_safe(struct net *net, struct rt6_info **prt) |
Wei Wang | d3843fe | 2017-10-06 12:06:06 -0700 | [diff] [blame] | 1171 | { |
| 1172 | struct rt6_info *rt = *prt; |
| 1173 | |
| 1174 | if (dst_hold_safe(&rt->dst)) |
| 1175 | return true; |
David Ahern | 10585b4 | 2019-03-20 09:24:50 -0700 | [diff] [blame] | 1176 | if (net) { |
Wei Wang | d3843fe | 2017-10-06 12:06:06 -0700 | [diff] [blame] | 1177 | rt = net->ipv6.ip6_null_entry; |
| 1178 | dst_hold(&rt->dst); |
| 1179 | } else { |
| 1180 | rt = NULL; |
| 1181 | } |
| 1182 | *prt = rt; |
| 1183 | return false; |
| 1184 | } |
| 1185 | |
David Ahern | dec9b0e | 2018-04-17 17:33:19 -0700 | [diff] [blame] | 1186 | /* called with rcu_lock held */ |
David Ahern | 9b6b35a | 2019-04-16 14:36:02 -0700 | [diff] [blame] | 1187 | static struct rt6_info *ip6_create_rt_rcu(const struct fib6_result *res) |
David Ahern | dec9b0e | 2018-04-17 17:33:19 -0700 | [diff] [blame] | 1188 | { |
David Ahern | 9b6b35a | 2019-04-16 14:36:02 -0700 | [diff] [blame] | 1189 | struct net_device *dev = res->nh->fib_nh_dev; |
| 1190 | struct fib6_info *f6i = res->f6i; |
| 1191 | unsigned short flags; |
David Ahern | dec9b0e | 2018-04-17 17:33:19 -0700 | [diff] [blame] | 1192 | struct rt6_info *nrt; |
| 1193 | |
David Ahern | 9b6b35a | 2019-04-16 14:36:02 -0700 | [diff] [blame] | 1194 | if (!fib6_info_hold_safe(f6i)) |
Xin Long | 1c87e79 | 2019-03-20 14:45:48 +0800 | [diff] [blame] | 1195 | goto fallback; |
Wei Wang | e873e4b | 2018-07-21 20:56:32 -0700 | [diff] [blame] | 1196 | |
David Ahern | 9b6b35a | 2019-04-16 14:36:02 -0700 | [diff] [blame] | 1197 | flags = fib6_info_dst_flags(f6i); |
David Ahern | 93531c6 | 2018-04-17 17:33:25 -0700 | [diff] [blame] | 1198 | nrt = ip6_dst_alloc(dev_net(dev), dev, flags); |
Xin Long | 1c87e79 | 2019-03-20 14:45:48 +0800 | [diff] [blame] | 1199 | if (!nrt) { |
David Ahern | 9b6b35a | 2019-04-16 14:36:02 -0700 | [diff] [blame] | 1200 | fib6_info_release(f6i); |
Xin Long | 1c87e79 | 2019-03-20 14:45:48 +0800 | [diff] [blame] | 1201 | goto fallback; |
| 1202 | } |
David Ahern | dec9b0e | 2018-04-17 17:33:19 -0700 | [diff] [blame] | 1203 | |
David Ahern | 0d16158 | 2019-04-16 14:36:04 -0700 | [diff] [blame] | 1204 | ip6_rt_copy_init(nrt, res); |
Xin Long | 1c87e79 | 2019-03-20 14:45:48 +0800 | [diff] [blame] | 1205 | return nrt; |
| 1206 | |
| 1207 | fallback: |
| 1208 | nrt = dev_net(dev)->ipv6.ip6_null_entry; |
| 1209 | dst_hold(&nrt->dst); |
David Ahern | dec9b0e | 2018-04-17 17:33:19 -0700 | [diff] [blame] | 1210 | return nrt; |
| 1211 | } |
| 1212 | |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 1213 | static struct rt6_info *ip6_pol_route_lookup(struct net *net, |
| 1214 | struct fib6_table *table, |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 1215 | struct flowi6 *fl6, |
| 1216 | const struct sk_buff *skb, |
| 1217 | int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1218 | { |
David Ahern | b1d4099 | 2019-04-16 14:35:59 -0700 | [diff] [blame] | 1219 | struct fib6_result res = {}; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1220 | struct fib6_node *fn; |
David Ahern | 23fb93a | 2018-04-17 17:33:23 -0700 | [diff] [blame] | 1221 | struct rt6_info *rt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1222 | |
David Ahern | b6cdbc8 | 2018-03-29 17:44:57 -0700 | [diff] [blame] | 1223 | if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF) |
| 1224 | flags &= ~RT6_LOOKUP_F_IFACE; |
| 1225 | |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 1226 | rcu_read_lock(); |
David Ahern | 6454743 | 2018-05-09 20:34:19 -0700 | [diff] [blame] | 1227 | fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr); |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1228 | restart: |
David Ahern | b1d4099 | 2019-04-16 14:35:59 -0700 | [diff] [blame] | 1229 | res.f6i = rcu_dereference(fn->leaf); |
| 1230 | if (!res.f6i) |
| 1231 | res.f6i = net->ipv6.fib6_null_entry; |
David Ahern | af52a52 | 2019-04-09 14:41:16 -0700 | [diff] [blame] | 1232 | else |
David Ahern | 75ef738 | 2019-04-16 14:36:07 -0700 | [diff] [blame] | 1233 | rt6_device_match(net, &res, &fl6->saddr, fl6->flowi6_oif, |
| 1234 | flags); |
David Ahern | af52a52 | 2019-04-09 14:41:16 -0700 | [diff] [blame] | 1235 | |
David Ahern | b1d4099 | 2019-04-16 14:35:59 -0700 | [diff] [blame] | 1236 | if (res.f6i == net->ipv6.fib6_null_entry) { |
Martin KaFai Lau | a3c00e4 | 2014-10-20 13:42:43 -0700 | [diff] [blame] | 1237 | fn = fib6_backtrack(fn, &fl6->saddr); |
| 1238 | if (fn) |
| 1239 | goto restart; |
David Ahern | af52a52 | 2019-04-09 14:41:16 -0700 | [diff] [blame] | 1240 | |
| 1241 | rt = net->ipv6.ip6_null_entry; |
| 1242 | dst_hold(&rt->dst); |
| 1243 | goto out; |
David Ahern | f88d8ea | 2019-06-03 20:19:52 -0700 | [diff] [blame] | 1244 | } else if (res.fib6_flags & RTF_REJECT) { |
| 1245 | goto do_create; |
Martin KaFai Lau | a3c00e4 | 2014-10-20 13:42:43 -0700 | [diff] [blame] | 1246 | } |
Wei Wang | 2b760fc | 2017-10-06 12:06:03 -0700 | [diff] [blame] | 1247 | |
David Ahern | b1d4099 | 2019-04-16 14:35:59 -0700 | [diff] [blame] | 1248 | fib6_select_path(net, &res, fl6, fl6->flowi6_oif, |
| 1249 | fl6->flowi6_oif != 0, skb, flags); |
| 1250 | |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 1251 | /* Search through exception table */ |
David Ahern | 7e4b512 | 2019-04-16 14:36:00 -0700 | [diff] [blame] | 1252 | rt = rt6_find_cached_rt(&res, &fl6->daddr, &fl6->saddr); |
David Ahern | 23fb93a | 2018-04-17 17:33:23 -0700 | [diff] [blame] | 1253 | if (rt) { |
David Ahern | 10585b4 | 2019-03-20 09:24:50 -0700 | [diff] [blame] | 1254 | if (ip6_hold_safe(net, &rt)) |
David Ahern | dec9b0e | 2018-04-17 17:33:19 -0700 | [diff] [blame] | 1255 | dst_use_noref(&rt->dst, jiffies); |
David Ahern | 23fb93a | 2018-04-17 17:33:23 -0700 | [diff] [blame] | 1256 | } else { |
David Ahern | f88d8ea | 2019-06-03 20:19:52 -0700 | [diff] [blame] | 1257 | do_create: |
David Ahern | 9b6b35a | 2019-04-16 14:36:02 -0700 | [diff] [blame] | 1258 | rt = ip6_create_rt_rcu(&res); |
David Ahern | dec9b0e | 2018-04-17 17:33:19 -0700 | [diff] [blame] | 1259 | } |
Wei Wang | d3843fe | 2017-10-06 12:06:06 -0700 | [diff] [blame] | 1260 | |
David Ahern | af52a52 | 2019-04-09 14:41:16 -0700 | [diff] [blame] | 1261 | out: |
David Ahern | 8ff2e5b | 2019-04-16 14:36:09 -0700 | [diff] [blame] | 1262 | trace_fib6_table_lookup(net, &res, table, fl6); |
David Ahern | af52a52 | 2019-04-09 14:41:16 -0700 | [diff] [blame] | 1263 | |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 1264 | rcu_read_unlock(); |
David Ahern | b811580 | 2015-11-19 12:24:22 -0800 | [diff] [blame] | 1265 | |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1266 | return rt; |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1267 | } |
| 1268 | |
Ian Morris | 67ba415 | 2014-08-24 21:53:10 +0100 | [diff] [blame] | 1269 | struct dst_entry *ip6_route_lookup(struct net *net, struct flowi6 *fl6, |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 1270 | const struct sk_buff *skb, int flags) |
Florian Westphal | ea6e574 | 2011-09-05 16:05:44 +0200 | [diff] [blame] | 1271 | { |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 1272 | return fib6_rule_lookup(net, fl6, skb, flags, ip6_pol_route_lookup); |
Florian Westphal | ea6e574 | 2011-09-05 16:05:44 +0200 | [diff] [blame] | 1273 | } |
| 1274 | EXPORT_SYMBOL_GPL(ip6_route_lookup); |
| 1275 | |
YOSHIFUJI Hideaki | 9acd9f3 | 2008-04-10 15:42:10 +0900 | [diff] [blame] | 1276 | struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr, |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 1277 | const struct in6_addr *saddr, int oif, |
| 1278 | const struct sk_buff *skb, int strict) |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1279 | { |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 1280 | struct flowi6 fl6 = { |
| 1281 | .flowi6_oif = oif, |
| 1282 | .daddr = *daddr, |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1283 | }; |
| 1284 | struct dst_entry *dst; |
YOSHIFUJI Hideaki | 77d16f4 | 2006-08-23 17:25:05 -0700 | [diff] [blame] | 1285 | int flags = strict ? RT6_LOOKUP_F_IFACE : 0; |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1286 | |
Thomas Graf | adaa70b | 2006-10-13 15:01:03 -0700 | [diff] [blame] | 1287 | if (saddr) { |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 1288 | memcpy(&fl6.saddr, saddr, sizeof(*saddr)); |
Thomas Graf | adaa70b | 2006-10-13 15:01:03 -0700 | [diff] [blame] | 1289 | flags |= RT6_LOOKUP_F_HAS_SADDR; |
| 1290 | } |
| 1291 | |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 1292 | dst = fib6_rule_lookup(net, &fl6, skb, flags, ip6_pol_route_lookup); |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1293 | if (dst->error == 0) |
| 1294 | return (struct rt6_info *) dst; |
| 1295 | |
| 1296 | dst_release(dst); |
| 1297 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1298 | return NULL; |
| 1299 | } |
YOSHIFUJI Hideaki | 7159039 | 2007-02-22 22:05:40 +0900 | [diff] [blame] | 1300 | EXPORT_SYMBOL(rt6_lookup); |
| 1301 | |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1302 | /* ip6_ins_rt is called with FREE table->tb6_lock. |
Wei Wang | 1cfb71e | 2017-06-17 10:42:33 -0700 | [diff] [blame] | 1303 | * It takes new route entry, the addition fails by any reason the |
| 1304 | * route is released. |
| 1305 | * Caller must hold dst before calling it. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1306 | */ |
| 1307 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 1308 | static int __ip6_ins_rt(struct fib6_info *rt, struct nl_info *info, |
David Ahern | 333c430 | 2017-05-21 10:12:04 -0600 | [diff] [blame] | 1309 | struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1310 | { |
| 1311 | int err; |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1312 | struct fib6_table *table; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1313 | |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 1314 | table = rt->fib6_table; |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 1315 | spin_lock_bh(&table->tb6_lock); |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 1316 | err = fib6_add(&table->tb6_root, rt, info, extack); |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 1317 | spin_unlock_bh(&table->tb6_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1318 | |
| 1319 | return err; |
| 1320 | } |
| 1321 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 1322 | int ip6_ins_rt(struct net *net, struct fib6_info *rt) |
Thomas Graf | 40e22e8 | 2006-08-22 00:00:45 -0700 | [diff] [blame] | 1323 | { |
David Ahern | afb1d4b5 | 2018-04-17 17:33:11 -0700 | [diff] [blame] | 1324 | struct nl_info info = { .nl_net = net, }; |
Florian Westphal | e715b6d | 2015-01-05 23:57:44 +0100 | [diff] [blame] | 1325 | |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 1326 | return __ip6_ins_rt(rt, &info, NULL); |
Thomas Graf | 40e22e8 | 2006-08-22 00:00:45 -0700 | [diff] [blame] | 1327 | } |
| 1328 | |
David Ahern | 85bd05d | 2019-04-16 14:36:01 -0700 | [diff] [blame] | 1329 | static struct rt6_info *ip6_rt_cache_alloc(const struct fib6_result *res, |
Martin KaFai Lau | 8b9df26 | 2015-05-22 20:55:59 -0700 | [diff] [blame] | 1330 | const struct in6_addr *daddr, |
| 1331 | const struct in6_addr *saddr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1332 | { |
David Ahern | 85bd05d | 2019-04-16 14:36:01 -0700 | [diff] [blame] | 1333 | struct fib6_info *f6i = res->f6i; |
David Ahern | 4832c30 | 2017-08-17 12:17:20 -0700 | [diff] [blame] | 1334 | struct net_device *dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1335 | struct rt6_info *rt; |
| 1336 | |
| 1337 | /* |
| 1338 | * Clone the route. |
| 1339 | */ |
| 1340 | |
David Ahern | 85bd05d | 2019-04-16 14:36:01 -0700 | [diff] [blame] | 1341 | if (!fib6_info_hold_safe(f6i)) |
Wei Wang | e873e4b | 2018-07-21 20:56:32 -0700 | [diff] [blame] | 1342 | return NULL; |
| 1343 | |
David Ahern | 0d16158 | 2019-04-16 14:36:04 -0700 | [diff] [blame] | 1344 | dev = ip6_rt_get_dev_rcu(res); |
David Ahern | 93531c6 | 2018-04-17 17:33:25 -0700 | [diff] [blame] | 1345 | rt = ip6_dst_alloc(dev_net(dev), dev, 0); |
Wei Wang | e873e4b | 2018-07-21 20:56:32 -0700 | [diff] [blame] | 1346 | if (!rt) { |
David Ahern | 85bd05d | 2019-04-16 14:36:01 -0700 | [diff] [blame] | 1347 | fib6_info_release(f6i); |
Martin KaFai Lau | 83a09ab | 2015-05-22 20:56:05 -0700 | [diff] [blame] | 1348 | return NULL; |
Wei Wang | e873e4b | 2018-07-21 20:56:32 -0700 | [diff] [blame] | 1349 | } |
Martin KaFai Lau | 83a09ab | 2015-05-22 20:56:05 -0700 | [diff] [blame] | 1350 | |
David Ahern | 0d16158 | 2019-04-16 14:36:04 -0700 | [diff] [blame] | 1351 | ip6_rt_copy_init(rt, res); |
Martin KaFai Lau | 83a09ab | 2015-05-22 20:56:05 -0700 | [diff] [blame] | 1352 | rt->rt6i_flags |= RTF_CACHE; |
Martin KaFai Lau | 83a09ab | 2015-05-22 20:56:05 -0700 | [diff] [blame] | 1353 | rt->rt6i_dst.addr = *daddr; |
| 1354 | rt->rt6i_dst.plen = 128; |
| 1355 | |
David Ahern | 85bd05d | 2019-04-16 14:36:01 -0700 | [diff] [blame] | 1356 | if (!rt6_is_gw_or_nonexthop(res)) { |
| 1357 | if (f6i->fib6_dst.plen != 128 && |
| 1358 | ipv6_addr_equal(&f6i->fib6_dst.addr, daddr)) |
Martin KaFai Lau | 83a09ab | 2015-05-22 20:56:05 -0700 | [diff] [blame] | 1359 | rt->rt6i_flags |= RTF_ANYCAST; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1360 | #ifdef CONFIG_IPV6_SUBTREES |
Martin KaFai Lau | 83a09ab | 2015-05-22 20:56:05 -0700 | [diff] [blame] | 1361 | if (rt->rt6i_src.plen && saddr) { |
| 1362 | rt->rt6i_src.addr = *saddr; |
| 1363 | rt->rt6i_src.plen = 128; |
Martin KaFai Lau | 8b9df26 | 2015-05-22 20:55:59 -0700 | [diff] [blame] | 1364 | } |
Martin KaFai Lau | 83a09ab | 2015-05-22 20:56:05 -0700 | [diff] [blame] | 1365 | #endif |
YOSHIFUJI Hideaki | 95a9a5b | 2006-03-20 16:55:51 -0800 | [diff] [blame] | 1366 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1367 | |
YOSHIFUJI Hideaki | 95a9a5b | 2006-03-20 16:55:51 -0800 | [diff] [blame] | 1368 | return rt; |
| 1369 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1370 | |
David Ahern | db3fede | 2019-04-16 14:36:03 -0700 | [diff] [blame] | 1371 | static struct rt6_info *ip6_rt_pcpu_alloc(const struct fib6_result *res) |
Martin KaFai Lau | d52d399 | 2015-05-22 20:56:06 -0700 | [diff] [blame] | 1372 | { |
David Ahern | db3fede | 2019-04-16 14:36:03 -0700 | [diff] [blame] | 1373 | struct fib6_info *f6i = res->f6i; |
| 1374 | unsigned short flags = fib6_info_dst_flags(f6i); |
David Ahern | 4832c30 | 2017-08-17 12:17:20 -0700 | [diff] [blame] | 1375 | struct net_device *dev; |
Martin KaFai Lau | d52d399 | 2015-05-22 20:56:06 -0700 | [diff] [blame] | 1376 | struct rt6_info *pcpu_rt; |
| 1377 | |
David Ahern | db3fede | 2019-04-16 14:36:03 -0700 | [diff] [blame] | 1378 | if (!fib6_info_hold_safe(f6i)) |
Wei Wang | e873e4b | 2018-07-21 20:56:32 -0700 | [diff] [blame] | 1379 | return NULL; |
| 1380 | |
David Ahern | 4832c30 | 2017-08-17 12:17:20 -0700 | [diff] [blame] | 1381 | rcu_read_lock(); |
David Ahern | 0d16158 | 2019-04-16 14:36:04 -0700 | [diff] [blame] | 1382 | dev = ip6_rt_get_dev_rcu(res); |
Eric Dumazet | d8882935 | 2020-05-08 07:34:14 -0700 | [diff] [blame] | 1383 | pcpu_rt = ip6_dst_alloc(dev_net(dev), dev, flags | DST_NOCOUNT); |
David Ahern | 4832c30 | 2017-08-17 12:17:20 -0700 | [diff] [blame] | 1384 | rcu_read_unlock(); |
Wei Wang | e873e4b | 2018-07-21 20:56:32 -0700 | [diff] [blame] | 1385 | if (!pcpu_rt) { |
David Ahern | db3fede | 2019-04-16 14:36:03 -0700 | [diff] [blame] | 1386 | fib6_info_release(f6i); |
Martin KaFai Lau | d52d399 | 2015-05-22 20:56:06 -0700 | [diff] [blame] | 1387 | return NULL; |
Wei Wang | e873e4b | 2018-07-21 20:56:32 -0700 | [diff] [blame] | 1388 | } |
David Ahern | 0d16158 | 2019-04-16 14:36:04 -0700 | [diff] [blame] | 1389 | ip6_rt_copy_init(pcpu_rt, res); |
Martin KaFai Lau | d52d399 | 2015-05-22 20:56:06 -0700 | [diff] [blame] | 1390 | pcpu_rt->rt6i_flags |= RTF_PCPU; |
David Ahern | 8f34e53 | 2020-05-01 08:53:08 -0600 | [diff] [blame] | 1391 | |
| 1392 | if (f6i->nh) |
| 1393 | pcpu_rt->sernum = rt_genid_ipv6(dev_net(dev)); |
| 1394 | |
Martin KaFai Lau | d52d399 | 2015-05-22 20:56:06 -0700 | [diff] [blame] | 1395 | return pcpu_rt; |
| 1396 | } |
| 1397 | |
David Ahern | 8f34e53 | 2020-05-01 08:53:08 -0600 | [diff] [blame] | 1398 | static bool rt6_is_valid(const struct rt6_info *rt6) |
| 1399 | { |
| 1400 | return rt6->sernum == rt_genid_ipv6(dev_net(rt6->dst.dev)); |
| 1401 | } |
| 1402 | |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 1403 | /* It should be called with rcu_read_lock() acquired */ |
David Ahern | db3fede | 2019-04-16 14:36:03 -0700 | [diff] [blame] | 1404 | static struct rt6_info *rt6_get_pcpu_route(const struct fib6_result *res) |
Martin KaFai Lau | d52d399 | 2015-05-22 20:56:06 -0700 | [diff] [blame] | 1405 | { |
Eric Dumazet | c353071 | 2019-05-31 18:11:25 -0700 | [diff] [blame] | 1406 | struct rt6_info *pcpu_rt; |
Martin KaFai Lau | d52d399 | 2015-05-22 20:56:06 -0700 | [diff] [blame] | 1407 | |
Eric Dumazet | c353071 | 2019-05-31 18:11:25 -0700 | [diff] [blame] | 1408 | pcpu_rt = this_cpu_read(*res->nh->rt6i_pcpu); |
Martin KaFai Lau | d52d399 | 2015-05-22 20:56:06 -0700 | [diff] [blame] | 1409 | |
David Ahern | 8f34e53 | 2020-05-01 08:53:08 -0600 | [diff] [blame] | 1410 | if (pcpu_rt && pcpu_rt->sernum && !rt6_is_valid(pcpu_rt)) { |
| 1411 | struct rt6_info *prev, **p; |
| 1412 | |
| 1413 | p = this_cpu_ptr(res->nh->rt6i_pcpu); |
| 1414 | prev = xchg(p, NULL); |
| 1415 | if (prev) { |
| 1416 | dst_dev_put(&prev->dst); |
| 1417 | dst_release(&prev->dst); |
| 1418 | } |
| 1419 | |
| 1420 | pcpu_rt = NULL; |
| 1421 | } |
| 1422 | |
Martin KaFai Lau | a73e419 | 2015-08-14 11:05:53 -0700 | [diff] [blame] | 1423 | return pcpu_rt; |
| 1424 | } |
| 1425 | |
David Ahern | afb1d4b5 | 2018-04-17 17:33:11 -0700 | [diff] [blame] | 1426 | static struct rt6_info *rt6_make_pcpu_route(struct net *net, |
David Ahern | db3fede | 2019-04-16 14:36:03 -0700 | [diff] [blame] | 1427 | const struct fib6_result *res) |
Martin KaFai Lau | a73e419 | 2015-08-14 11:05:53 -0700 | [diff] [blame] | 1428 | { |
| 1429 | struct rt6_info *pcpu_rt, *prev, **p; |
Martin KaFai Lau | d52d399 | 2015-05-22 20:56:06 -0700 | [diff] [blame] | 1430 | |
David Ahern | db3fede | 2019-04-16 14:36:03 -0700 | [diff] [blame] | 1431 | pcpu_rt = ip6_rt_pcpu_alloc(res); |
Wei Wang | 0e09edc | 2019-06-20 17:36:37 -0700 | [diff] [blame] | 1432 | if (!pcpu_rt) |
| 1433 | return NULL; |
Martin KaFai Lau | d52d399 | 2015-05-22 20:56:06 -0700 | [diff] [blame] | 1434 | |
David Ahern | f40b6ae | 2019-05-22 20:27:55 -0700 | [diff] [blame] | 1435 | p = this_cpu_ptr(res->nh->rt6i_pcpu); |
Wei Wang | a94b936 | 2017-10-06 12:06:04 -0700 | [diff] [blame] | 1436 | prev = cmpxchg(p, NULL, pcpu_rt); |
Eric Dumazet | 951f788 | 2017-10-08 21:07:18 -0700 | [diff] [blame] | 1437 | BUG_ON(prev); |
Wei Wang | a94b936 | 2017-10-06 12:06:04 -0700 | [diff] [blame] | 1438 | |
Eric Dumazet | 61fb0d0 | 2019-05-15 19:39:52 -0700 | [diff] [blame] | 1439 | if (res->f6i->fib6_destroying) { |
| 1440 | struct fib6_info *from; |
| 1441 | |
| 1442 | from = xchg((__force struct fib6_info **)&pcpu_rt->from, NULL); |
| 1443 | fib6_info_release(from); |
| 1444 | } |
| 1445 | |
Martin KaFai Lau | d52d399 | 2015-05-22 20:56:06 -0700 | [diff] [blame] | 1446 | return pcpu_rt; |
| 1447 | } |
| 1448 | |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1449 | /* exception hash table implementation |
| 1450 | */ |
| 1451 | static DEFINE_SPINLOCK(rt6_exception_lock); |
| 1452 | |
| 1453 | /* Remove rt6_ex from hash table and free the memory |
| 1454 | * Caller must hold rt6_exception_lock |
| 1455 | */ |
| 1456 | static void rt6_remove_exception(struct rt6_exception_bucket *bucket, |
| 1457 | struct rt6_exception *rt6_ex) |
| 1458 | { |
Paolo Abeni | f5b51fe | 2019-02-20 18:18:12 +0100 | [diff] [blame] | 1459 | struct fib6_info *from; |
Colin Ian King | b2427e6 | 2017-10-10 18:01:16 +0100 | [diff] [blame] | 1460 | struct net *net; |
Wei Wang | 81eb844 | 2017-10-06 12:06:11 -0700 | [diff] [blame] | 1461 | |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1462 | if (!bucket || !rt6_ex) |
| 1463 | return; |
Colin Ian King | b2427e6 | 2017-10-10 18:01:16 +0100 | [diff] [blame] | 1464 | |
| 1465 | net = dev_net(rt6_ex->rt6i->dst.dev); |
Paolo Abeni | f5b51fe | 2019-02-20 18:18:12 +0100 | [diff] [blame] | 1466 | net->ipv6.rt6_stats->fib_rt_cache--; |
| 1467 | |
| 1468 | /* purge completely the exception to allow releasing the held resources: |
| 1469 | * some [sk] cache may keep the dst around for unlimited time |
| 1470 | */ |
Eric Dumazet | 0e23387 | 2019-04-28 12:22:25 -0700 | [diff] [blame] | 1471 | from = xchg((__force struct fib6_info **)&rt6_ex->rt6i->from, NULL); |
Paolo Abeni | f5b51fe | 2019-02-20 18:18:12 +0100 | [diff] [blame] | 1472 | fib6_info_release(from); |
| 1473 | dst_dev_put(&rt6_ex->rt6i->dst); |
| 1474 | |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1475 | hlist_del_rcu(&rt6_ex->hlist); |
David Ahern | 77634cc | 2018-04-17 17:33:27 -0700 | [diff] [blame] | 1476 | dst_release(&rt6_ex->rt6i->dst); |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1477 | kfree_rcu(rt6_ex, rcu); |
| 1478 | WARN_ON_ONCE(!bucket->depth); |
| 1479 | bucket->depth--; |
| 1480 | } |
| 1481 | |
| 1482 | /* Remove oldest rt6_ex in bucket and free the memory |
| 1483 | * Caller must hold rt6_exception_lock |
| 1484 | */ |
| 1485 | static void rt6_exception_remove_oldest(struct rt6_exception_bucket *bucket) |
| 1486 | { |
| 1487 | struct rt6_exception *rt6_ex, *oldest = NULL; |
| 1488 | |
| 1489 | if (!bucket) |
| 1490 | return; |
| 1491 | |
| 1492 | hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) { |
| 1493 | if (!oldest || time_before(rt6_ex->stamp, oldest->stamp)) |
| 1494 | oldest = rt6_ex; |
| 1495 | } |
| 1496 | rt6_remove_exception(bucket, oldest); |
| 1497 | } |
| 1498 | |
| 1499 | static u32 rt6_exception_hash(const struct in6_addr *dst, |
| 1500 | const struct in6_addr *src) |
| 1501 | { |
| 1502 | static u32 seed __read_mostly; |
| 1503 | u32 val; |
| 1504 | |
| 1505 | net_get_random_once(&seed, sizeof(seed)); |
Eric Dumazet | b6b556a | 2019-11-03 18:24:16 -0800 | [diff] [blame] | 1506 | val = jhash2((const u32 *)dst, sizeof(*dst)/sizeof(u32), seed); |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1507 | |
| 1508 | #ifdef CONFIG_IPV6_SUBTREES |
| 1509 | if (src) |
Eric Dumazet | b6b556a | 2019-11-03 18:24:16 -0800 | [diff] [blame] | 1510 | val = jhash2((const u32 *)src, sizeof(*src)/sizeof(u32), val); |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1511 | #endif |
| 1512 | return hash_32(val, FIB6_EXCEPTION_BUCKET_SIZE_SHIFT); |
| 1513 | } |
| 1514 | |
| 1515 | /* Helper function to find the cached rt in the hash table |
| 1516 | * and update bucket pointer to point to the bucket for this |
| 1517 | * (daddr, saddr) pair |
| 1518 | * Caller must hold rt6_exception_lock |
| 1519 | */ |
| 1520 | static struct rt6_exception * |
| 1521 | __rt6_find_exception_spinlock(struct rt6_exception_bucket **bucket, |
| 1522 | const struct in6_addr *daddr, |
| 1523 | const struct in6_addr *saddr) |
| 1524 | { |
| 1525 | struct rt6_exception *rt6_ex; |
| 1526 | u32 hval; |
| 1527 | |
| 1528 | if (!(*bucket) || !daddr) |
| 1529 | return NULL; |
| 1530 | |
| 1531 | hval = rt6_exception_hash(daddr, saddr); |
| 1532 | *bucket += hval; |
| 1533 | |
| 1534 | hlist_for_each_entry(rt6_ex, &(*bucket)->chain, hlist) { |
| 1535 | struct rt6_info *rt6 = rt6_ex->rt6i; |
| 1536 | bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr); |
| 1537 | |
| 1538 | #ifdef CONFIG_IPV6_SUBTREES |
| 1539 | if (matched && saddr) |
| 1540 | matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr); |
| 1541 | #endif |
| 1542 | if (matched) |
| 1543 | return rt6_ex; |
| 1544 | } |
| 1545 | return NULL; |
| 1546 | } |
| 1547 | |
| 1548 | /* Helper function to find the cached rt in the hash table |
| 1549 | * and update bucket pointer to point to the bucket for this |
| 1550 | * (daddr, saddr) pair |
| 1551 | * Caller must hold rcu_read_lock() |
| 1552 | */ |
| 1553 | static struct rt6_exception * |
| 1554 | __rt6_find_exception_rcu(struct rt6_exception_bucket **bucket, |
| 1555 | const struct in6_addr *daddr, |
| 1556 | const struct in6_addr *saddr) |
| 1557 | { |
| 1558 | struct rt6_exception *rt6_ex; |
| 1559 | u32 hval; |
| 1560 | |
| 1561 | WARN_ON_ONCE(!rcu_read_lock_held()); |
| 1562 | |
| 1563 | if (!(*bucket) || !daddr) |
| 1564 | return NULL; |
| 1565 | |
| 1566 | hval = rt6_exception_hash(daddr, saddr); |
| 1567 | *bucket += hval; |
| 1568 | |
| 1569 | hlist_for_each_entry_rcu(rt6_ex, &(*bucket)->chain, hlist) { |
| 1570 | struct rt6_info *rt6 = rt6_ex->rt6i; |
| 1571 | bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr); |
| 1572 | |
| 1573 | #ifdef CONFIG_IPV6_SUBTREES |
| 1574 | if (matched && saddr) |
| 1575 | matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr); |
| 1576 | #endif |
| 1577 | if (matched) |
| 1578 | return rt6_ex; |
| 1579 | } |
| 1580 | return NULL; |
| 1581 | } |
| 1582 | |
David Ahern | b748f26 | 2019-04-16 14:36:06 -0700 | [diff] [blame] | 1583 | static unsigned int fib6_mtu(const struct fib6_result *res) |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1584 | { |
David Ahern | b748f26 | 2019-04-16 14:36:06 -0700 | [diff] [blame] | 1585 | const struct fib6_nh *nh = res->nh; |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 1586 | unsigned int mtu; |
| 1587 | |
David Ahern | b748f26 | 2019-04-16 14:36:06 -0700 | [diff] [blame] | 1588 | if (res->f6i->fib6_pmtu) { |
| 1589 | mtu = res->f6i->fib6_pmtu; |
David Ahern | dcd1f57 | 2018-04-18 15:39:05 -0700 | [diff] [blame] | 1590 | } else { |
David Ahern | b748f26 | 2019-04-16 14:36:06 -0700 | [diff] [blame] | 1591 | struct net_device *dev = nh->fib_nh_dev; |
David Ahern | dcd1f57 | 2018-04-18 15:39:05 -0700 | [diff] [blame] | 1592 | struct inet6_dev *idev; |
| 1593 | |
| 1594 | rcu_read_lock(); |
| 1595 | idev = __in6_dev_get(dev); |
| 1596 | mtu = idev->cnf.mtu6; |
| 1597 | rcu_read_unlock(); |
| 1598 | } |
| 1599 | |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 1600 | mtu = min_t(unsigned int, mtu, IP6_MAX_MTU); |
| 1601 | |
David Ahern | b748f26 | 2019-04-16 14:36:06 -0700 | [diff] [blame] | 1602 | return mtu - lwtunnel_headroom(nh->fib_nh_lws, mtu); |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 1603 | } |
| 1604 | |
David Ahern | cc5c073 | 2019-05-22 20:27:58 -0700 | [diff] [blame] | 1605 | #define FIB6_EXCEPTION_BUCKET_FLUSHED 0x1UL |
| 1606 | |
| 1607 | /* used when the flushed bit is not relevant, only access to the bucket |
| 1608 | * (ie., all bucket users except rt6_insert_exception); |
| 1609 | * |
| 1610 | * called under rcu lock; sometimes called with rt6_exception_lock held |
| 1611 | */ |
| 1612 | static |
| 1613 | struct rt6_exception_bucket *fib6_nh_get_excptn_bucket(const struct fib6_nh *nh, |
| 1614 | spinlock_t *lock) |
| 1615 | { |
| 1616 | struct rt6_exception_bucket *bucket; |
| 1617 | |
| 1618 | if (lock) |
| 1619 | bucket = rcu_dereference_protected(nh->rt6i_exception_bucket, |
| 1620 | lockdep_is_held(lock)); |
| 1621 | else |
| 1622 | bucket = rcu_dereference(nh->rt6i_exception_bucket); |
| 1623 | |
| 1624 | /* remove bucket flushed bit if set */ |
| 1625 | if (bucket) { |
| 1626 | unsigned long p = (unsigned long)bucket; |
| 1627 | |
| 1628 | p &= ~FIB6_EXCEPTION_BUCKET_FLUSHED; |
| 1629 | bucket = (struct rt6_exception_bucket *)p; |
| 1630 | } |
| 1631 | |
| 1632 | return bucket; |
| 1633 | } |
| 1634 | |
| 1635 | static bool fib6_nh_excptn_bucket_flushed(struct rt6_exception_bucket *bucket) |
| 1636 | { |
| 1637 | unsigned long p = (unsigned long)bucket; |
| 1638 | |
| 1639 | return !!(p & FIB6_EXCEPTION_BUCKET_FLUSHED); |
| 1640 | } |
| 1641 | |
| 1642 | /* called with rt6_exception_lock held */ |
| 1643 | static void fib6_nh_excptn_bucket_set_flushed(struct fib6_nh *nh, |
| 1644 | spinlock_t *lock) |
| 1645 | { |
| 1646 | struct rt6_exception_bucket *bucket; |
| 1647 | unsigned long p; |
| 1648 | |
| 1649 | bucket = rcu_dereference_protected(nh->rt6i_exception_bucket, |
| 1650 | lockdep_is_held(lock)); |
| 1651 | |
| 1652 | p = (unsigned long)bucket; |
| 1653 | p |= FIB6_EXCEPTION_BUCKET_FLUSHED; |
| 1654 | bucket = (struct rt6_exception_bucket *)p; |
| 1655 | rcu_assign_pointer(nh->rt6i_exception_bucket, bucket); |
| 1656 | } |
| 1657 | |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1658 | static int rt6_insert_exception(struct rt6_info *nrt, |
David Ahern | 5012f0a | 2019-04-16 14:36:05 -0700 | [diff] [blame] | 1659 | const struct fib6_result *res) |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1660 | { |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 1661 | struct net *net = dev_net(nrt->dst.dev); |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1662 | struct rt6_exception_bucket *bucket; |
David Ahern | cc5c073 | 2019-05-22 20:27:58 -0700 | [diff] [blame] | 1663 | struct fib6_info *f6i = res->f6i; |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1664 | struct in6_addr *src_key = NULL; |
| 1665 | struct rt6_exception *rt6_ex; |
David Ahern | cc5c073 | 2019-05-22 20:27:58 -0700 | [diff] [blame] | 1666 | struct fib6_nh *nh = res->nh; |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1667 | int err = 0; |
| 1668 | |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1669 | spin_lock_bh(&rt6_exception_lock); |
| 1670 | |
David Ahern | cc5c073 | 2019-05-22 20:27:58 -0700 | [diff] [blame] | 1671 | bucket = rcu_dereference_protected(nh->rt6i_exception_bucket, |
| 1672 | lockdep_is_held(&rt6_exception_lock)); |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1673 | if (!bucket) { |
| 1674 | bucket = kcalloc(FIB6_EXCEPTION_BUCKET_SIZE, sizeof(*bucket), |
| 1675 | GFP_ATOMIC); |
| 1676 | if (!bucket) { |
| 1677 | err = -ENOMEM; |
| 1678 | goto out; |
| 1679 | } |
David Ahern | cc5c073 | 2019-05-22 20:27:58 -0700 | [diff] [blame] | 1680 | rcu_assign_pointer(nh->rt6i_exception_bucket, bucket); |
| 1681 | } else if (fib6_nh_excptn_bucket_flushed(bucket)) { |
| 1682 | err = -EINVAL; |
| 1683 | goto out; |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1684 | } |
| 1685 | |
| 1686 | #ifdef CONFIG_IPV6_SUBTREES |
David Ahern | 5012f0a | 2019-04-16 14:36:05 -0700 | [diff] [blame] | 1687 | /* fib6_src.plen != 0 indicates f6i is in subtree |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1688 | * and exception table is indexed by a hash of |
David Ahern | 5012f0a | 2019-04-16 14:36:05 -0700 | [diff] [blame] | 1689 | * both fib6_dst and fib6_src. |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1690 | * Otherwise, the exception table is indexed by |
David Ahern | 5012f0a | 2019-04-16 14:36:05 -0700 | [diff] [blame] | 1691 | * a hash of only fib6_dst. |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1692 | */ |
David Ahern | 5012f0a | 2019-04-16 14:36:05 -0700 | [diff] [blame] | 1693 | if (f6i->fib6_src.plen) |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1694 | src_key = &nrt->rt6i_src.addr; |
| 1695 | #endif |
David Ahern | 5012f0a | 2019-04-16 14:36:05 -0700 | [diff] [blame] | 1696 | /* rt6_mtu_change() might lower mtu on f6i. |
Wei Wang | f5bbe7e | 2017-10-06 12:05:59 -0700 | [diff] [blame] | 1697 | * Only insert this exception route if its mtu |
David Ahern | 5012f0a | 2019-04-16 14:36:05 -0700 | [diff] [blame] | 1698 | * is less than f6i's mtu value. |
Wei Wang | f5bbe7e | 2017-10-06 12:05:59 -0700 | [diff] [blame] | 1699 | */ |
David Ahern | b748f26 | 2019-04-16 14:36:06 -0700 | [diff] [blame] | 1700 | if (dst_metric_raw(&nrt->dst, RTAX_MTU) >= fib6_mtu(res)) { |
Wei Wang | f5bbe7e | 2017-10-06 12:05:59 -0700 | [diff] [blame] | 1701 | err = -EINVAL; |
| 1702 | goto out; |
| 1703 | } |
Wei Wang | 60006a4 | 2017-10-06 12:05:58 -0700 | [diff] [blame] | 1704 | |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1705 | rt6_ex = __rt6_find_exception_spinlock(&bucket, &nrt->rt6i_dst.addr, |
| 1706 | src_key); |
| 1707 | if (rt6_ex) |
| 1708 | rt6_remove_exception(bucket, rt6_ex); |
| 1709 | |
| 1710 | rt6_ex = kzalloc(sizeof(*rt6_ex), GFP_ATOMIC); |
| 1711 | if (!rt6_ex) { |
| 1712 | err = -ENOMEM; |
| 1713 | goto out; |
| 1714 | } |
| 1715 | rt6_ex->rt6i = nrt; |
| 1716 | rt6_ex->stamp = jiffies; |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1717 | hlist_add_head_rcu(&rt6_ex->hlist, &bucket->chain); |
| 1718 | bucket->depth++; |
Wei Wang | 81eb844 | 2017-10-06 12:06:11 -0700 | [diff] [blame] | 1719 | net->ipv6.rt6_stats->fib_rt_cache++; |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1720 | |
| 1721 | if (bucket->depth > FIB6_MAX_DEPTH) |
| 1722 | rt6_exception_remove_oldest(bucket); |
| 1723 | |
| 1724 | out: |
| 1725 | spin_unlock_bh(&rt6_exception_lock); |
| 1726 | |
| 1727 | /* Update fn->fn_sernum to invalidate all cached dst */ |
Paolo Abeni | b886d5f | 2017-10-19 16:07:10 +0200 | [diff] [blame] | 1728 | if (!err) { |
David Ahern | 5012f0a | 2019-04-16 14:36:05 -0700 | [diff] [blame] | 1729 | spin_lock_bh(&f6i->fib6_table->tb6_lock); |
| 1730 | fib6_update_sernum(net, f6i); |
| 1731 | spin_unlock_bh(&f6i->fib6_table->tb6_lock); |
Paolo Abeni | b886d5f | 2017-10-19 16:07:10 +0200 | [diff] [blame] | 1732 | fib6_force_start_gc(net); |
| 1733 | } |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1734 | |
| 1735 | return err; |
| 1736 | } |
| 1737 | |
David Ahern | c0b220c | 2019-05-22 20:27:57 -0700 | [diff] [blame] | 1738 | static void fib6_nh_flush_exceptions(struct fib6_nh *nh, struct fib6_info *from) |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1739 | { |
| 1740 | struct rt6_exception_bucket *bucket; |
| 1741 | struct rt6_exception *rt6_ex; |
| 1742 | struct hlist_node *tmp; |
| 1743 | int i; |
| 1744 | |
| 1745 | spin_lock_bh(&rt6_exception_lock); |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1746 | |
David Ahern | cc5c073 | 2019-05-22 20:27:58 -0700 | [diff] [blame] | 1747 | bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock); |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1748 | if (!bucket) |
| 1749 | goto out; |
| 1750 | |
David Ahern | cc5c073 | 2019-05-22 20:27:58 -0700 | [diff] [blame] | 1751 | /* Prevent rt6_insert_exception() to recreate the bucket list */ |
| 1752 | if (!from) |
| 1753 | fib6_nh_excptn_bucket_set_flushed(nh, &rt6_exception_lock); |
| 1754 | |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1755 | for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) { |
David Ahern | cc5c073 | 2019-05-22 20:27:58 -0700 | [diff] [blame] | 1756 | hlist_for_each_entry_safe(rt6_ex, tmp, &bucket->chain, hlist) { |
| 1757 | if (!from || |
| 1758 | rcu_access_pointer(rt6_ex->rt6i->from) == from) |
| 1759 | rt6_remove_exception(bucket, rt6_ex); |
| 1760 | } |
| 1761 | WARN_ON_ONCE(!from && bucket->depth); |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1762 | bucket++; |
| 1763 | } |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1764 | out: |
| 1765 | spin_unlock_bh(&rt6_exception_lock); |
| 1766 | } |
| 1767 | |
David Ahern | e659ba3 | 2019-06-08 14:53:28 -0700 | [diff] [blame] | 1768 | static int rt6_nh_flush_exceptions(struct fib6_nh *nh, void *arg) |
| 1769 | { |
| 1770 | struct fib6_info *f6i = arg; |
| 1771 | |
| 1772 | fib6_nh_flush_exceptions(nh, f6i); |
| 1773 | |
| 1774 | return 0; |
| 1775 | } |
| 1776 | |
David Ahern | c0b220c | 2019-05-22 20:27:57 -0700 | [diff] [blame] | 1777 | void rt6_flush_exceptions(struct fib6_info *f6i) |
| 1778 | { |
David Ahern | e659ba3 | 2019-06-08 14:53:28 -0700 | [diff] [blame] | 1779 | if (f6i->nh) |
| 1780 | nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_flush_exceptions, |
| 1781 | f6i); |
| 1782 | else |
| 1783 | fib6_nh_flush_exceptions(f6i->fib6_nh, f6i); |
David Ahern | c0b220c | 2019-05-22 20:27:57 -0700 | [diff] [blame] | 1784 | } |
| 1785 | |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1786 | /* Find cached rt in the hash table inside passed in rt |
| 1787 | * Caller has to hold rcu_read_lock() |
| 1788 | */ |
David Ahern | 7e4b512 | 2019-04-16 14:36:00 -0700 | [diff] [blame] | 1789 | static struct rt6_info *rt6_find_cached_rt(const struct fib6_result *res, |
Wei Wang | 510e2ce | 2019-05-16 13:30:54 -0700 | [diff] [blame] | 1790 | const struct in6_addr *daddr, |
| 1791 | const struct in6_addr *saddr) |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1792 | { |
Wei Wang | 510e2ce | 2019-05-16 13:30:54 -0700 | [diff] [blame] | 1793 | const struct in6_addr *src_key = NULL; |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1794 | struct rt6_exception_bucket *bucket; |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1795 | struct rt6_exception *rt6_ex; |
David Ahern | 7e4b512 | 2019-04-16 14:36:00 -0700 | [diff] [blame] | 1796 | struct rt6_info *ret = NULL; |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1797 | |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1798 | #ifdef CONFIG_IPV6_SUBTREES |
David Ahern | 7e4b512 | 2019-04-16 14:36:00 -0700 | [diff] [blame] | 1799 | /* fib6i_src.plen != 0 indicates f6i is in subtree |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1800 | * and exception table is indexed by a hash of |
David Ahern | 7e4b512 | 2019-04-16 14:36:00 -0700 | [diff] [blame] | 1801 | * both fib6_dst and fib6_src. |
Wei Wang | 510e2ce | 2019-05-16 13:30:54 -0700 | [diff] [blame] | 1802 | * However, the src addr used to create the hash |
| 1803 | * might not be exactly the passed in saddr which |
| 1804 | * is a /128 addr from the flow. |
| 1805 | * So we need to use f6i->fib6_src to redo lookup |
| 1806 | * if the passed in saddr does not find anything. |
| 1807 | * (See the logic in ip6_rt_cache_alloc() on how |
| 1808 | * rt->rt6i_src is updated.) |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1809 | */ |
David Ahern | 7e4b512 | 2019-04-16 14:36:00 -0700 | [diff] [blame] | 1810 | if (res->f6i->fib6_src.plen) |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1811 | src_key = saddr; |
Wei Wang | 510e2ce | 2019-05-16 13:30:54 -0700 | [diff] [blame] | 1812 | find_ex: |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1813 | #endif |
David Ahern | cc5c073 | 2019-05-22 20:27:58 -0700 | [diff] [blame] | 1814 | bucket = fib6_nh_get_excptn_bucket(res->nh, NULL); |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1815 | rt6_ex = __rt6_find_exception_rcu(&bucket, daddr, src_key); |
| 1816 | |
| 1817 | if (rt6_ex && !rt6_check_expired(rt6_ex->rt6i)) |
David Ahern | 7e4b512 | 2019-04-16 14:36:00 -0700 | [diff] [blame] | 1818 | ret = rt6_ex->rt6i; |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1819 | |
Wei Wang | 510e2ce | 2019-05-16 13:30:54 -0700 | [diff] [blame] | 1820 | #ifdef CONFIG_IPV6_SUBTREES |
| 1821 | /* Use fib6_src as src_key and redo lookup */ |
| 1822 | if (!ret && src_key && src_key != &res->f6i->fib6_src.addr) { |
| 1823 | src_key = &res->f6i->fib6_src.addr; |
| 1824 | goto find_ex; |
| 1825 | } |
| 1826 | #endif |
| 1827 | |
David Ahern | 7e4b512 | 2019-04-16 14:36:00 -0700 | [diff] [blame] | 1828 | return ret; |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1829 | } |
| 1830 | |
| 1831 | /* Remove the passed in cached rt from the hash table that contains it */ |
David Ahern | cc5c073 | 2019-05-22 20:27:58 -0700 | [diff] [blame] | 1832 | static int fib6_nh_remove_exception(const struct fib6_nh *nh, int plen, |
David Ahern | c0b220c | 2019-05-22 20:27:57 -0700 | [diff] [blame] | 1833 | const struct rt6_info *rt) |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1834 | { |
David Ahern | c0b220c | 2019-05-22 20:27:57 -0700 | [diff] [blame] | 1835 | const struct in6_addr *src_key = NULL; |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1836 | struct rt6_exception_bucket *bucket; |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1837 | struct rt6_exception *rt6_ex; |
| 1838 | int err; |
| 1839 | |
David Ahern | cc5c073 | 2019-05-22 20:27:58 -0700 | [diff] [blame] | 1840 | if (!rcu_access_pointer(nh->rt6i_exception_bucket)) |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1841 | return -ENOENT; |
| 1842 | |
| 1843 | spin_lock_bh(&rt6_exception_lock); |
David Ahern | cc5c073 | 2019-05-22 20:27:58 -0700 | [diff] [blame] | 1844 | bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock); |
| 1845 | |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1846 | #ifdef CONFIG_IPV6_SUBTREES |
David Ahern | cc5c073 | 2019-05-22 20:27:58 -0700 | [diff] [blame] | 1847 | /* rt6i_src.plen != 0 indicates 'from' is in subtree |
| 1848 | * and exception table is indexed by a hash of |
| 1849 | * both rt6i_dst and rt6i_src. |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1850 | * Otherwise, the exception table is indexed by |
| 1851 | * a hash of only rt6i_dst. |
| 1852 | */ |
David Ahern | c0b220c | 2019-05-22 20:27:57 -0700 | [diff] [blame] | 1853 | if (plen) |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1854 | src_key = &rt->rt6i_src.addr; |
| 1855 | #endif |
| 1856 | rt6_ex = __rt6_find_exception_spinlock(&bucket, |
| 1857 | &rt->rt6i_dst.addr, |
| 1858 | src_key); |
| 1859 | if (rt6_ex) { |
| 1860 | rt6_remove_exception(bucket, rt6_ex); |
| 1861 | err = 0; |
| 1862 | } else { |
| 1863 | err = -ENOENT; |
| 1864 | } |
| 1865 | |
| 1866 | spin_unlock_bh(&rt6_exception_lock); |
| 1867 | return err; |
| 1868 | } |
| 1869 | |
David Ahern | e659ba3 | 2019-06-08 14:53:28 -0700 | [diff] [blame] | 1870 | struct fib6_nh_excptn_arg { |
| 1871 | struct rt6_info *rt; |
| 1872 | int plen; |
| 1873 | }; |
| 1874 | |
| 1875 | static int rt6_nh_remove_exception_rt(struct fib6_nh *nh, void *_arg) |
| 1876 | { |
| 1877 | struct fib6_nh_excptn_arg *arg = _arg; |
| 1878 | int err; |
| 1879 | |
| 1880 | err = fib6_nh_remove_exception(nh, arg->plen, arg->rt); |
| 1881 | if (err == 0) |
| 1882 | return 1; |
| 1883 | |
| 1884 | return 0; |
| 1885 | } |
| 1886 | |
David Ahern | c0b220c | 2019-05-22 20:27:57 -0700 | [diff] [blame] | 1887 | static int rt6_remove_exception_rt(struct rt6_info *rt) |
| 1888 | { |
| 1889 | struct fib6_info *from; |
| 1890 | |
| 1891 | from = rcu_dereference(rt->from); |
David Ahern | cc5c073 | 2019-05-22 20:27:58 -0700 | [diff] [blame] | 1892 | if (!from || !(rt->rt6i_flags & RTF_CACHE)) |
David Ahern | c0b220c | 2019-05-22 20:27:57 -0700 | [diff] [blame] | 1893 | return -EINVAL; |
| 1894 | |
David Ahern | e659ba3 | 2019-06-08 14:53:28 -0700 | [diff] [blame] | 1895 | if (from->nh) { |
| 1896 | struct fib6_nh_excptn_arg arg = { |
| 1897 | .rt = rt, |
| 1898 | .plen = from->fib6_src.plen |
| 1899 | }; |
| 1900 | int rc; |
| 1901 | |
| 1902 | /* rc = 1 means an entry was found */ |
| 1903 | rc = nexthop_for_each_fib6_nh(from->nh, |
| 1904 | rt6_nh_remove_exception_rt, |
| 1905 | &arg); |
| 1906 | return rc ? 0 : -ENOENT; |
| 1907 | } |
| 1908 | |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 1909 | return fib6_nh_remove_exception(from->fib6_nh, |
David Ahern | cc5c073 | 2019-05-22 20:27:58 -0700 | [diff] [blame] | 1910 | from->fib6_src.plen, rt); |
David Ahern | c0b220c | 2019-05-22 20:27:57 -0700 | [diff] [blame] | 1911 | } |
| 1912 | |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1913 | /* Find rt6_ex which contains the passed in rt cache and |
| 1914 | * refresh its stamp |
| 1915 | */ |
David Ahern | cc5c073 | 2019-05-22 20:27:58 -0700 | [diff] [blame] | 1916 | static void fib6_nh_update_exception(const struct fib6_nh *nh, int plen, |
David Ahern | c0b220c | 2019-05-22 20:27:57 -0700 | [diff] [blame] | 1917 | const struct rt6_info *rt) |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1918 | { |
David Ahern | c0b220c | 2019-05-22 20:27:57 -0700 | [diff] [blame] | 1919 | const struct in6_addr *src_key = NULL; |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1920 | struct rt6_exception_bucket *bucket; |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1921 | struct rt6_exception *rt6_ex; |
Paolo Abeni | 193f368 | 2019-02-21 11:19:41 +0100 | [diff] [blame] | 1922 | |
David Ahern | cc5c073 | 2019-05-22 20:27:58 -0700 | [diff] [blame] | 1923 | bucket = fib6_nh_get_excptn_bucket(nh, NULL); |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1924 | #ifdef CONFIG_IPV6_SUBTREES |
David Ahern | cc5c073 | 2019-05-22 20:27:58 -0700 | [diff] [blame] | 1925 | /* rt6i_src.plen != 0 indicates 'from' is in subtree |
| 1926 | * and exception table is indexed by a hash of |
| 1927 | * both rt6i_dst and rt6i_src. |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1928 | * Otherwise, the exception table is indexed by |
| 1929 | * a hash of only rt6i_dst. |
| 1930 | */ |
David Ahern | c0b220c | 2019-05-22 20:27:57 -0700 | [diff] [blame] | 1931 | if (plen) |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1932 | src_key = &rt->rt6i_src.addr; |
| 1933 | #endif |
David Ahern | cc5c073 | 2019-05-22 20:27:58 -0700 | [diff] [blame] | 1934 | rt6_ex = __rt6_find_exception_rcu(&bucket, &rt->rt6i_dst.addr, src_key); |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1935 | if (rt6_ex) |
| 1936 | rt6_ex->stamp = jiffies; |
David Ahern | c0b220c | 2019-05-22 20:27:57 -0700 | [diff] [blame] | 1937 | } |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1938 | |
David Ahern | e659ba3 | 2019-06-08 14:53:28 -0700 | [diff] [blame] | 1939 | struct fib6_nh_match_arg { |
| 1940 | const struct net_device *dev; |
| 1941 | const struct in6_addr *gw; |
| 1942 | struct fib6_nh *match; |
| 1943 | }; |
| 1944 | |
| 1945 | /* determine if fib6_nh has given device and gateway */ |
| 1946 | static int fib6_nh_find_match(struct fib6_nh *nh, void *_arg) |
| 1947 | { |
| 1948 | struct fib6_nh_match_arg *arg = _arg; |
| 1949 | |
| 1950 | if (arg->dev != nh->fib_nh_dev || |
| 1951 | (arg->gw && !nh->fib_nh_gw_family) || |
| 1952 | (!arg->gw && nh->fib_nh_gw_family) || |
| 1953 | (arg->gw && !ipv6_addr_equal(arg->gw, &nh->fib_nh_gw6))) |
| 1954 | return 0; |
| 1955 | |
| 1956 | arg->match = nh; |
| 1957 | |
| 1958 | /* found a match, break the loop */ |
| 1959 | return 1; |
| 1960 | } |
| 1961 | |
David Ahern | c0b220c | 2019-05-22 20:27:57 -0700 | [diff] [blame] | 1962 | static void rt6_update_exception_stamp_rt(struct rt6_info *rt) |
| 1963 | { |
| 1964 | struct fib6_info *from; |
David Ahern | e659ba3 | 2019-06-08 14:53:28 -0700 | [diff] [blame] | 1965 | struct fib6_nh *fib6_nh; |
David Ahern | c0b220c | 2019-05-22 20:27:57 -0700 | [diff] [blame] | 1966 | |
| 1967 | rcu_read_lock(); |
| 1968 | |
| 1969 | from = rcu_dereference(rt->from); |
| 1970 | if (!from || !(rt->rt6i_flags & RTF_CACHE)) |
| 1971 | goto unlock; |
| 1972 | |
David Ahern | e659ba3 | 2019-06-08 14:53:28 -0700 | [diff] [blame] | 1973 | if (from->nh) { |
| 1974 | struct fib6_nh_match_arg arg = { |
| 1975 | .dev = rt->dst.dev, |
| 1976 | .gw = &rt->rt6i_gateway, |
| 1977 | }; |
| 1978 | |
| 1979 | nexthop_for_each_fib6_nh(from->nh, fib6_nh_find_match, &arg); |
| 1980 | |
| 1981 | if (!arg.match) |
David Ahern | cff6a32 | 2019-08-01 14:36:35 -0700 | [diff] [blame] | 1982 | goto unlock; |
David Ahern | e659ba3 | 2019-06-08 14:53:28 -0700 | [diff] [blame] | 1983 | fib6_nh = arg.match; |
| 1984 | } else { |
| 1985 | fib6_nh = from->fib6_nh; |
| 1986 | } |
| 1987 | fib6_nh_update_exception(fib6_nh, from->fib6_src.plen, rt); |
Paolo Abeni | 193f368 | 2019-02-21 11:19:41 +0100 | [diff] [blame] | 1988 | unlock: |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1989 | rcu_read_unlock(); |
| 1990 | } |
| 1991 | |
Stefano Brivio | e9fa149 | 2018-03-06 11:10:19 +0100 | [diff] [blame] | 1992 | static bool rt6_mtu_change_route_allowed(struct inet6_dev *idev, |
| 1993 | struct rt6_info *rt, int mtu) |
| 1994 | { |
| 1995 | /* If the new MTU is lower than the route PMTU, this new MTU will be the |
| 1996 | * lowest MTU in the path: always allow updating the route PMTU to |
| 1997 | * reflect PMTU decreases. |
| 1998 | * |
| 1999 | * If the new MTU is higher, and the route PMTU is equal to the local |
| 2000 | * MTU, this means the old MTU is the lowest in the path, so allow |
| 2001 | * updating it: if other nodes now have lower MTUs, PMTU discovery will |
| 2002 | * handle this. |
| 2003 | */ |
| 2004 | |
| 2005 | if (dst_mtu(&rt->dst) >= mtu) |
| 2006 | return true; |
| 2007 | |
| 2008 | if (dst_mtu(&rt->dst) == idev->cnf.mtu6) |
| 2009 | return true; |
| 2010 | |
| 2011 | return false; |
| 2012 | } |
| 2013 | |
| 2014 | static void rt6_exceptions_update_pmtu(struct inet6_dev *idev, |
David Ahern | cc5c073 | 2019-05-22 20:27:58 -0700 | [diff] [blame] | 2015 | const struct fib6_nh *nh, int mtu) |
Wei Wang | f5bbe7e | 2017-10-06 12:05:59 -0700 | [diff] [blame] | 2016 | { |
| 2017 | struct rt6_exception_bucket *bucket; |
| 2018 | struct rt6_exception *rt6_ex; |
| 2019 | int i; |
| 2020 | |
David Ahern | cc5c073 | 2019-05-22 20:27:58 -0700 | [diff] [blame] | 2021 | bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock); |
Stefano Brivio | e9fa149 | 2018-03-06 11:10:19 +0100 | [diff] [blame] | 2022 | if (!bucket) |
| 2023 | return; |
| 2024 | |
| 2025 | for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) { |
| 2026 | hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) { |
| 2027 | struct rt6_info *entry = rt6_ex->rt6i; |
| 2028 | |
| 2029 | /* For RTF_CACHE with rt6i_pmtu == 0 (i.e. a redirected |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 2030 | * route), the metrics of its rt->from have already |
Stefano Brivio | e9fa149 | 2018-03-06 11:10:19 +0100 | [diff] [blame] | 2031 | * been updated. |
| 2032 | */ |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 2033 | if (dst_metric_raw(&entry->dst, RTAX_MTU) && |
Stefano Brivio | e9fa149 | 2018-03-06 11:10:19 +0100 | [diff] [blame] | 2034 | rt6_mtu_change_route_allowed(idev, entry, mtu)) |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 2035 | dst_metric_set(&entry->dst, RTAX_MTU, mtu); |
Wei Wang | f5bbe7e | 2017-10-06 12:05:59 -0700 | [diff] [blame] | 2036 | } |
Stefano Brivio | e9fa149 | 2018-03-06 11:10:19 +0100 | [diff] [blame] | 2037 | bucket++; |
Wei Wang | f5bbe7e | 2017-10-06 12:05:59 -0700 | [diff] [blame] | 2038 | } |
| 2039 | } |
| 2040 | |
Wei Wang | b16cb45 | 2017-10-06 12:06:00 -0700 | [diff] [blame] | 2041 | #define RTF_CACHE_GATEWAY (RTF_GATEWAY | RTF_CACHE) |
| 2042 | |
David Ahern | cc5c073 | 2019-05-22 20:27:58 -0700 | [diff] [blame] | 2043 | static void fib6_nh_exceptions_clean_tohost(const struct fib6_nh *nh, |
| 2044 | const struct in6_addr *gateway) |
Wei Wang | b16cb45 | 2017-10-06 12:06:00 -0700 | [diff] [blame] | 2045 | { |
| 2046 | struct rt6_exception_bucket *bucket; |
| 2047 | struct rt6_exception *rt6_ex; |
| 2048 | struct hlist_node *tmp; |
| 2049 | int i; |
| 2050 | |
David Ahern | cc5c073 | 2019-05-22 20:27:58 -0700 | [diff] [blame] | 2051 | if (!rcu_access_pointer(nh->rt6i_exception_bucket)) |
Wei Wang | b16cb45 | 2017-10-06 12:06:00 -0700 | [diff] [blame] | 2052 | return; |
| 2053 | |
| 2054 | spin_lock_bh(&rt6_exception_lock); |
David Ahern | cc5c073 | 2019-05-22 20:27:58 -0700 | [diff] [blame] | 2055 | bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock); |
Wei Wang | b16cb45 | 2017-10-06 12:06:00 -0700 | [diff] [blame] | 2056 | if (bucket) { |
| 2057 | for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) { |
| 2058 | hlist_for_each_entry_safe(rt6_ex, tmp, |
| 2059 | &bucket->chain, hlist) { |
| 2060 | struct rt6_info *entry = rt6_ex->rt6i; |
| 2061 | |
| 2062 | if ((entry->rt6i_flags & RTF_CACHE_GATEWAY) == |
| 2063 | RTF_CACHE_GATEWAY && |
| 2064 | ipv6_addr_equal(gateway, |
| 2065 | &entry->rt6i_gateway)) { |
| 2066 | rt6_remove_exception(bucket, rt6_ex); |
| 2067 | } |
| 2068 | } |
| 2069 | bucket++; |
| 2070 | } |
| 2071 | } |
| 2072 | |
| 2073 | spin_unlock_bh(&rt6_exception_lock); |
| 2074 | } |
| 2075 | |
Wei Wang | c757faa | 2017-10-06 12:06:01 -0700 | [diff] [blame] | 2076 | static void rt6_age_examine_exception(struct rt6_exception_bucket *bucket, |
| 2077 | struct rt6_exception *rt6_ex, |
| 2078 | struct fib6_gc_args *gc_args, |
| 2079 | unsigned long now) |
| 2080 | { |
| 2081 | struct rt6_info *rt = rt6_ex->rt6i; |
| 2082 | |
Paolo Abeni | 1859bac | 2017-10-19 16:07:11 +0200 | [diff] [blame] | 2083 | /* we are pruning and obsoleting aged-out and non gateway exceptions |
| 2084 | * even if others have still references to them, so that on next |
| 2085 | * dst_check() such references can be dropped. |
| 2086 | * EXPIRES exceptions - e.g. pmtu-generated ones are pruned when |
| 2087 | * expired, independently from their aging, as per RFC 8201 section 4 |
| 2088 | */ |
Wei Wang | 31afeb4 | 2018-01-26 11:40:17 -0800 | [diff] [blame] | 2089 | if (!(rt->rt6i_flags & RTF_EXPIRES)) { |
| 2090 | if (time_after_eq(now, rt->dst.lastuse + gc_args->timeout)) { |
| 2091 | RT6_TRACE("aging clone %p\n", rt); |
| 2092 | rt6_remove_exception(bucket, rt6_ex); |
| 2093 | return; |
| 2094 | } |
| 2095 | } else if (time_after(jiffies, rt->dst.expires)) { |
| 2096 | RT6_TRACE("purging expired route %p\n", rt); |
Wei Wang | c757faa | 2017-10-06 12:06:01 -0700 | [diff] [blame] | 2097 | rt6_remove_exception(bucket, rt6_ex); |
| 2098 | return; |
Wei Wang | 31afeb4 | 2018-01-26 11:40:17 -0800 | [diff] [blame] | 2099 | } |
| 2100 | |
| 2101 | if (rt->rt6i_flags & RTF_GATEWAY) { |
Wei Wang | c757faa | 2017-10-06 12:06:01 -0700 | [diff] [blame] | 2102 | struct neighbour *neigh; |
| 2103 | __u8 neigh_flags = 0; |
| 2104 | |
Eric Dumazet | 1bfa26f | 2018-03-23 07:56:58 -0700 | [diff] [blame] | 2105 | neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway); |
| 2106 | if (neigh) |
Wei Wang | c757faa | 2017-10-06 12:06:01 -0700 | [diff] [blame] | 2107 | neigh_flags = neigh->flags; |
Eric Dumazet | 1bfa26f | 2018-03-23 07:56:58 -0700 | [diff] [blame] | 2108 | |
Wei Wang | c757faa | 2017-10-06 12:06:01 -0700 | [diff] [blame] | 2109 | if (!(neigh_flags & NTF_ROUTER)) { |
| 2110 | RT6_TRACE("purging route %p via non-router but gateway\n", |
| 2111 | rt); |
| 2112 | rt6_remove_exception(bucket, rt6_ex); |
| 2113 | return; |
| 2114 | } |
| 2115 | } |
Wei Wang | 31afeb4 | 2018-01-26 11:40:17 -0800 | [diff] [blame] | 2116 | |
Wei Wang | c757faa | 2017-10-06 12:06:01 -0700 | [diff] [blame] | 2117 | gc_args->more++; |
| 2118 | } |
| 2119 | |
David Ahern | cc5c073 | 2019-05-22 20:27:58 -0700 | [diff] [blame] | 2120 | static void fib6_nh_age_exceptions(const struct fib6_nh *nh, |
David Ahern | c0b220c | 2019-05-22 20:27:57 -0700 | [diff] [blame] | 2121 | struct fib6_gc_args *gc_args, |
| 2122 | unsigned long now) |
Wei Wang | c757faa | 2017-10-06 12:06:01 -0700 | [diff] [blame] | 2123 | { |
| 2124 | struct rt6_exception_bucket *bucket; |
| 2125 | struct rt6_exception *rt6_ex; |
| 2126 | struct hlist_node *tmp; |
| 2127 | int i; |
| 2128 | |
David Ahern | cc5c073 | 2019-05-22 20:27:58 -0700 | [diff] [blame] | 2129 | if (!rcu_access_pointer(nh->rt6i_exception_bucket)) |
Wei Wang | c757faa | 2017-10-06 12:06:01 -0700 | [diff] [blame] | 2130 | return; |
| 2131 | |
Eric Dumazet | 1bfa26f | 2018-03-23 07:56:58 -0700 | [diff] [blame] | 2132 | rcu_read_lock_bh(); |
| 2133 | spin_lock(&rt6_exception_lock); |
David Ahern | cc5c073 | 2019-05-22 20:27:58 -0700 | [diff] [blame] | 2134 | bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock); |
Wei Wang | c757faa | 2017-10-06 12:06:01 -0700 | [diff] [blame] | 2135 | if (bucket) { |
| 2136 | for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) { |
| 2137 | hlist_for_each_entry_safe(rt6_ex, tmp, |
| 2138 | &bucket->chain, hlist) { |
| 2139 | rt6_age_examine_exception(bucket, rt6_ex, |
| 2140 | gc_args, now); |
| 2141 | } |
| 2142 | bucket++; |
| 2143 | } |
| 2144 | } |
Eric Dumazet | 1bfa26f | 2018-03-23 07:56:58 -0700 | [diff] [blame] | 2145 | spin_unlock(&rt6_exception_lock); |
| 2146 | rcu_read_unlock_bh(); |
Wei Wang | c757faa | 2017-10-06 12:06:01 -0700 | [diff] [blame] | 2147 | } |
| 2148 | |
David Ahern | e659ba3 | 2019-06-08 14:53:28 -0700 | [diff] [blame] | 2149 | struct fib6_nh_age_excptn_arg { |
| 2150 | struct fib6_gc_args *gc_args; |
| 2151 | unsigned long now; |
| 2152 | }; |
| 2153 | |
| 2154 | static int rt6_nh_age_exceptions(struct fib6_nh *nh, void *_arg) |
| 2155 | { |
| 2156 | struct fib6_nh_age_excptn_arg *arg = _arg; |
| 2157 | |
| 2158 | fib6_nh_age_exceptions(nh, arg->gc_args, arg->now); |
| 2159 | return 0; |
| 2160 | } |
| 2161 | |
David Ahern | cc5c073 | 2019-05-22 20:27:58 -0700 | [diff] [blame] | 2162 | void rt6_age_exceptions(struct fib6_info *f6i, |
David Ahern | c0b220c | 2019-05-22 20:27:57 -0700 | [diff] [blame] | 2163 | struct fib6_gc_args *gc_args, |
| 2164 | unsigned long now) |
| 2165 | { |
David Ahern | e659ba3 | 2019-06-08 14:53:28 -0700 | [diff] [blame] | 2166 | if (f6i->nh) { |
| 2167 | struct fib6_nh_age_excptn_arg arg = { |
| 2168 | .gc_args = gc_args, |
| 2169 | .now = now |
| 2170 | }; |
| 2171 | |
| 2172 | nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_age_exceptions, |
| 2173 | &arg); |
| 2174 | } else { |
| 2175 | fib6_nh_age_exceptions(f6i->fib6_nh, gc_args, now); |
| 2176 | } |
David Ahern | c0b220c | 2019-05-22 20:27:57 -0700 | [diff] [blame] | 2177 | } |
| 2178 | |
David Ahern | 1d053da | 2018-05-09 20:34:21 -0700 | [diff] [blame] | 2179 | /* must be called with rcu lock held */ |
David Ahern | effda4d | 2019-04-16 14:36:10 -0700 | [diff] [blame] | 2180 | int fib6_table_lookup(struct net *net, struct fib6_table *table, int oif, |
| 2181 | struct flowi6 *fl6, struct fib6_result *res, int strict) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2182 | { |
Martin KaFai Lau | 367efcb | 2014-10-20 13:42:45 -0700 | [diff] [blame] | 2183 | struct fib6_node *fn, *saved_fn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2184 | |
David Ahern | 6454743 | 2018-05-09 20:34:19 -0700 | [diff] [blame] | 2185 | fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr); |
Martin KaFai Lau | 367efcb | 2014-10-20 13:42:45 -0700 | [diff] [blame] | 2186 | saved_fn = fn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2187 | |
David Ahern | ca25449 | 2015-10-12 11:47:10 -0700 | [diff] [blame] | 2188 | if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF) |
| 2189 | oif = 0; |
| 2190 | |
Martin KaFai Lau | a3c00e4 | 2014-10-20 13:42:43 -0700 | [diff] [blame] | 2191 | redo_rt6_select: |
David Ahern | effda4d | 2019-04-16 14:36:10 -0700 | [diff] [blame] | 2192 | rt6_select(net, fn, oif, res, strict); |
| 2193 | if (res->f6i == net->ipv6.fib6_null_entry) { |
Martin KaFai Lau | a3c00e4 | 2014-10-20 13:42:43 -0700 | [diff] [blame] | 2194 | fn = fib6_backtrack(fn, &fl6->saddr); |
| 2195 | if (fn) |
| 2196 | goto redo_rt6_select; |
Martin KaFai Lau | 367efcb | 2014-10-20 13:42:45 -0700 | [diff] [blame] | 2197 | else if (strict & RT6_LOOKUP_F_REACHABLE) { |
| 2198 | /* also consider unreachable route */ |
| 2199 | strict &= ~RT6_LOOKUP_F_REACHABLE; |
| 2200 | fn = saved_fn; |
| 2201 | goto redo_rt6_select; |
Martin KaFai Lau | 367efcb | 2014-10-20 13:42:45 -0700 | [diff] [blame] | 2202 | } |
Martin KaFai Lau | a3c00e4 | 2014-10-20 13:42:43 -0700 | [diff] [blame] | 2203 | } |
| 2204 | |
David Ahern | effda4d | 2019-04-16 14:36:10 -0700 | [diff] [blame] | 2205 | trace_fib6_table_lookup(net, res, table, fl6); |
YOSHIFUJI Hideaki | fb9de91 | 2006-03-20 16:59:08 -0800 | [diff] [blame] | 2206 | |
David Ahern | effda4d | 2019-04-16 14:36:10 -0700 | [diff] [blame] | 2207 | return 0; |
David Ahern | 1d053da | 2018-05-09 20:34:21 -0700 | [diff] [blame] | 2208 | } |
| 2209 | |
| 2210 | struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table, |
| 2211 | int oif, struct flowi6 *fl6, |
| 2212 | const struct sk_buff *skb, int flags) |
| 2213 | { |
David Ahern | b1d4099 | 2019-04-16 14:35:59 -0700 | [diff] [blame] | 2214 | struct fib6_result res = {}; |
Wei Wang | 0e09edc | 2019-06-20 17:36:37 -0700 | [diff] [blame] | 2215 | struct rt6_info *rt = NULL; |
David Ahern | 1d053da | 2018-05-09 20:34:21 -0700 | [diff] [blame] | 2216 | int strict = 0; |
| 2217 | |
Wei Wang | 0e09edc | 2019-06-20 17:36:37 -0700 | [diff] [blame] | 2218 | WARN_ON_ONCE((flags & RT6_LOOKUP_F_DST_NOREF) && |
| 2219 | !rcu_read_lock_held()); |
| 2220 | |
David Ahern | 1d053da | 2018-05-09 20:34:21 -0700 | [diff] [blame] | 2221 | strict |= flags & RT6_LOOKUP_F_IFACE; |
| 2222 | strict |= flags & RT6_LOOKUP_F_IGNORE_LINKSTATE; |
| 2223 | if (net->ipv6.devconf_all->forwarding == 0) |
| 2224 | strict |= RT6_LOOKUP_F_REACHABLE; |
| 2225 | |
| 2226 | rcu_read_lock(); |
| 2227 | |
David Ahern | effda4d | 2019-04-16 14:36:10 -0700 | [diff] [blame] | 2228 | fib6_table_lookup(net, table, oif, fl6, &res, strict); |
Wei Wang | 0e09edc | 2019-06-20 17:36:37 -0700 | [diff] [blame] | 2229 | if (res.f6i == net->ipv6.fib6_null_entry) |
| 2230 | goto out; |
David Ahern | 23fb93a | 2018-04-17 17:33:23 -0700 | [diff] [blame] | 2231 | |
David Ahern | b1d4099 | 2019-04-16 14:35:59 -0700 | [diff] [blame] | 2232 | fib6_select_path(net, &res, fl6, oif, false, skb, strict); |
David Ahern | d83009d | 2019-04-09 14:41:17 -0700 | [diff] [blame] | 2233 | |
David Ahern | 23fb93a | 2018-04-17 17:33:23 -0700 | [diff] [blame] | 2234 | /*Search through exception table */ |
David Ahern | 7e4b512 | 2019-04-16 14:36:00 -0700 | [diff] [blame] | 2235 | rt = rt6_find_cached_rt(&res, &fl6->daddr, &fl6->saddr); |
David Ahern | 23fb93a | 2018-04-17 17:33:23 -0700 | [diff] [blame] | 2236 | if (rt) { |
Wei Wang | 0e09edc | 2019-06-20 17:36:37 -0700 | [diff] [blame] | 2237 | goto out; |
Martin KaFai Lau | 3da59bd | 2015-05-22 20:56:03 -0700 | [diff] [blame] | 2238 | } else if (unlikely((fl6->flowi6_flags & FLOWI_FLAG_KNOWN_NH) && |
David Ahern | b1d4099 | 2019-04-16 14:35:59 -0700 | [diff] [blame] | 2239 | !res.nh->fib_nh_gw_family)) { |
Martin KaFai Lau | 3da59bd | 2015-05-22 20:56:03 -0700 | [diff] [blame] | 2240 | /* Create a RTF_CACHE clone which will not be |
| 2241 | * owned by the fib6 tree. It is for the special case where |
| 2242 | * the daddr in the skb during the neighbor look-up is different |
| 2243 | * from the fl6->daddr used to look-up route here. |
| 2244 | */ |
Wei Wang | 0e09edc | 2019-06-20 17:36:37 -0700 | [diff] [blame] | 2245 | rt = ip6_rt_cache_alloc(&res, &fl6->daddr, NULL); |
Martin KaFai Lau | 3da59bd | 2015-05-22 20:56:03 -0700 | [diff] [blame] | 2246 | |
Wei Wang | 0e09edc | 2019-06-20 17:36:37 -0700 | [diff] [blame] | 2247 | if (rt) { |
| 2248 | /* 1 refcnt is taken during ip6_rt_cache_alloc(). |
| 2249 | * As rt6_uncached_list_add() does not consume refcnt, |
| 2250 | * this refcnt is always returned to the caller even |
| 2251 | * if caller sets RT6_LOOKUP_F_DST_NOREF flag. |
Wei Wang | 1cfb71e | 2017-06-17 10:42:33 -0700 | [diff] [blame] | 2252 | */ |
Wei Wang | 0e09edc | 2019-06-20 17:36:37 -0700 | [diff] [blame] | 2253 | rt6_uncached_list_add(rt); |
Wei Wang | 81eb844 | 2017-10-06 12:06:11 -0700 | [diff] [blame] | 2254 | atomic_inc(&net->ipv6.rt6_stats->fib_rt_uncache); |
Wei Wang | 0e09edc | 2019-06-20 17:36:37 -0700 | [diff] [blame] | 2255 | rcu_read_unlock(); |
David Ahern | b811580 | 2015-11-19 12:24:22 -0800 | [diff] [blame] | 2256 | |
Wei Wang | 0e09edc | 2019-06-20 17:36:37 -0700 | [diff] [blame] | 2257 | return rt; |
| 2258 | } |
Martin KaFai Lau | d52d399 | 2015-05-22 20:56:06 -0700 | [diff] [blame] | 2259 | } else { |
| 2260 | /* Get a percpu copy */ |
Eric Dumazet | 951f788 | 2017-10-08 21:07:18 -0700 | [diff] [blame] | 2261 | local_bh_disable(); |
Wei Wang | 0e09edc | 2019-06-20 17:36:37 -0700 | [diff] [blame] | 2262 | rt = rt6_get_pcpu_route(&res); |
Martin KaFai Lau | d52d399 | 2015-05-22 20:56:06 -0700 | [diff] [blame] | 2263 | |
Wei Wang | 0e09edc | 2019-06-20 17:36:37 -0700 | [diff] [blame] | 2264 | if (!rt) |
| 2265 | rt = rt6_make_pcpu_route(net, &res); |
David Ahern | 93531c6 | 2018-04-17 17:33:25 -0700 | [diff] [blame] | 2266 | |
Eric Dumazet | 951f788 | 2017-10-08 21:07:18 -0700 | [diff] [blame] | 2267 | local_bh_enable(); |
Martin KaFai Lau | d52d399 | 2015-05-22 20:56:06 -0700 | [diff] [blame] | 2268 | } |
Wei Wang | 0e09edc | 2019-06-20 17:36:37 -0700 | [diff] [blame] | 2269 | out: |
| 2270 | if (!rt) |
| 2271 | rt = net->ipv6.ip6_null_entry; |
| 2272 | if (!(flags & RT6_LOOKUP_F_DST_NOREF)) |
| 2273 | ip6_hold_safe(net, &rt); |
| 2274 | rcu_read_unlock(); |
| 2275 | |
| 2276 | return rt; |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 2277 | } |
David Ahern | 9ff7438 | 2016-06-13 13:44:19 -0700 | [diff] [blame] | 2278 | EXPORT_SYMBOL_GPL(ip6_pol_route); |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 2279 | |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 2280 | static struct rt6_info *ip6_pol_route_input(struct net *net, |
| 2281 | struct fib6_table *table, |
| 2282 | struct flowi6 *fl6, |
| 2283 | const struct sk_buff *skb, |
| 2284 | int flags) |
Pavel Emelyanov | 4acad72 | 2007-10-15 13:02:51 -0700 | [diff] [blame] | 2285 | { |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 2286 | return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, skb, flags); |
Pavel Emelyanov | 4acad72 | 2007-10-15 13:02:51 -0700 | [diff] [blame] | 2287 | } |
| 2288 | |
Mahesh Bandewar | d409b84 | 2016-09-16 12:59:08 -0700 | [diff] [blame] | 2289 | struct dst_entry *ip6_route_input_lookup(struct net *net, |
| 2290 | struct net_device *dev, |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 2291 | struct flowi6 *fl6, |
| 2292 | const struct sk_buff *skb, |
| 2293 | int flags) |
Shmulik Ladkani | 72331bc | 2012-04-01 04:03:45 +0000 | [diff] [blame] | 2294 | { |
| 2295 | if (rt6_need_strict(&fl6->daddr) && dev->type != ARPHRD_PIMREG) |
| 2296 | flags |= RT6_LOOKUP_F_IFACE; |
| 2297 | |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 2298 | return fib6_rule_lookup(net, fl6, skb, flags, ip6_pol_route_input); |
Shmulik Ladkani | 72331bc | 2012-04-01 04:03:45 +0000 | [diff] [blame] | 2299 | } |
Mahesh Bandewar | d409b84 | 2016-09-16 12:59:08 -0700 | [diff] [blame] | 2300 | EXPORT_SYMBOL_GPL(ip6_route_input_lookup); |
Shmulik Ladkani | 72331bc | 2012-04-01 04:03:45 +0000 | [diff] [blame] | 2301 | |
Jakub Sitnicki | 23aebda | 2017-08-23 09:58:29 +0200 | [diff] [blame] | 2302 | static void ip6_multipath_l3_keys(const struct sk_buff *skb, |
Roopa Prabhu | 5e5d6fe | 2018-02-28 22:43:22 -0500 | [diff] [blame] | 2303 | struct flow_keys *keys, |
| 2304 | struct flow_keys *flkeys) |
Jakub Sitnicki | 23aebda | 2017-08-23 09:58:29 +0200 | [diff] [blame] | 2305 | { |
| 2306 | const struct ipv6hdr *outer_iph = ipv6_hdr(skb); |
| 2307 | const struct ipv6hdr *key_iph = outer_iph; |
Roopa Prabhu | 5e5d6fe | 2018-02-28 22:43:22 -0500 | [diff] [blame] | 2308 | struct flow_keys *_flkeys = flkeys; |
Jakub Sitnicki | 23aebda | 2017-08-23 09:58:29 +0200 | [diff] [blame] | 2309 | const struct ipv6hdr *inner_iph; |
| 2310 | const struct icmp6hdr *icmph; |
| 2311 | struct ipv6hdr _inner_iph; |
Eric Dumazet | cea67a2 | 2018-04-29 09:54:59 -0700 | [diff] [blame] | 2312 | struct icmp6hdr _icmph; |
Jakub Sitnicki | 23aebda | 2017-08-23 09:58:29 +0200 | [diff] [blame] | 2313 | |
| 2314 | if (likely(outer_iph->nexthdr != IPPROTO_ICMPV6)) |
| 2315 | goto out; |
| 2316 | |
Eric Dumazet | cea67a2 | 2018-04-29 09:54:59 -0700 | [diff] [blame] | 2317 | icmph = skb_header_pointer(skb, skb_transport_offset(skb), |
| 2318 | sizeof(_icmph), &_icmph); |
| 2319 | if (!icmph) |
| 2320 | goto out; |
| 2321 | |
Matteo Croce | 54074f1 | 2019-11-02 01:12:04 +0100 | [diff] [blame] | 2322 | if (!icmpv6_is_err(icmph->icmp6_type)) |
Jakub Sitnicki | 23aebda | 2017-08-23 09:58:29 +0200 | [diff] [blame] | 2323 | goto out; |
| 2324 | |
| 2325 | inner_iph = skb_header_pointer(skb, |
| 2326 | skb_transport_offset(skb) + sizeof(*icmph), |
| 2327 | sizeof(_inner_iph), &_inner_iph); |
| 2328 | if (!inner_iph) |
| 2329 | goto out; |
| 2330 | |
| 2331 | key_iph = inner_iph; |
Roopa Prabhu | 5e5d6fe | 2018-02-28 22:43:22 -0500 | [diff] [blame] | 2332 | _flkeys = NULL; |
Jakub Sitnicki | 23aebda | 2017-08-23 09:58:29 +0200 | [diff] [blame] | 2333 | out: |
Roopa Prabhu | 5e5d6fe | 2018-02-28 22:43:22 -0500 | [diff] [blame] | 2334 | if (_flkeys) { |
| 2335 | keys->addrs.v6addrs.src = _flkeys->addrs.v6addrs.src; |
| 2336 | keys->addrs.v6addrs.dst = _flkeys->addrs.v6addrs.dst; |
| 2337 | keys->tags.flow_label = _flkeys->tags.flow_label; |
| 2338 | keys->basic.ip_proto = _flkeys->basic.ip_proto; |
| 2339 | } else { |
| 2340 | keys->addrs.v6addrs.src = key_iph->saddr; |
| 2341 | keys->addrs.v6addrs.dst = key_iph->daddr; |
Michal Kubecek | fa1be7e | 2018-06-04 11:36:05 +0200 | [diff] [blame] | 2342 | keys->tags.flow_label = ip6_flowlabel(key_iph); |
Roopa Prabhu | 5e5d6fe | 2018-02-28 22:43:22 -0500 | [diff] [blame] | 2343 | keys->basic.ip_proto = key_iph->nexthdr; |
| 2344 | } |
Jakub Sitnicki | 23aebda | 2017-08-23 09:58:29 +0200 | [diff] [blame] | 2345 | } |
| 2346 | |
| 2347 | /* if skb is set it will be used and fl6 can be NULL */ |
David Ahern | b4bac17 | 2018-03-02 08:32:18 -0800 | [diff] [blame] | 2348 | u32 rt6_multipath_hash(const struct net *net, const struct flowi6 *fl6, |
| 2349 | const struct sk_buff *skb, struct flow_keys *flkeys) |
Jakub Sitnicki | 23aebda | 2017-08-23 09:58:29 +0200 | [diff] [blame] | 2350 | { |
| 2351 | struct flow_keys hash_keys; |
David Ahern | 9a2a537 | 2018-03-02 08:32:15 -0800 | [diff] [blame] | 2352 | u32 mhash; |
Jakub Sitnicki | 23aebda | 2017-08-23 09:58:29 +0200 | [diff] [blame] | 2353 | |
David S. Miller | bbfa047 | 2018-03-12 11:09:33 -0400 | [diff] [blame] | 2354 | switch (ip6_multipath_hash_policy(net)) { |
David Ahern | b4bac17 | 2018-03-02 08:32:18 -0800 | [diff] [blame] | 2355 | case 0: |
| 2356 | memset(&hash_keys, 0, sizeof(hash_keys)); |
| 2357 | hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; |
| 2358 | if (skb) { |
| 2359 | ip6_multipath_l3_keys(skb, &hash_keys, flkeys); |
| 2360 | } else { |
| 2361 | hash_keys.addrs.v6addrs.src = fl6->saddr; |
| 2362 | hash_keys.addrs.v6addrs.dst = fl6->daddr; |
Michal Kubecek | fa1be7e | 2018-06-04 11:36:05 +0200 | [diff] [blame] | 2363 | hash_keys.tags.flow_label = (__force u32)flowi6_get_flowlabel(fl6); |
David Ahern | b4bac17 | 2018-03-02 08:32:18 -0800 | [diff] [blame] | 2364 | hash_keys.basic.ip_proto = fl6->flowi6_proto; |
| 2365 | } |
| 2366 | break; |
| 2367 | case 1: |
| 2368 | if (skb) { |
| 2369 | unsigned int flag = FLOW_DISSECTOR_F_STOP_AT_ENCAP; |
| 2370 | struct flow_keys keys; |
| 2371 | |
| 2372 | /* short-circuit if we already have L4 hash present */ |
| 2373 | if (skb->l4_hash) |
| 2374 | return skb_get_hash_raw(skb) >> 1; |
| 2375 | |
| 2376 | memset(&hash_keys, 0, sizeof(hash_keys)); |
| 2377 | |
| 2378 | if (!flkeys) { |
| 2379 | skb_flow_dissect_flow_keys(skb, &keys, flag); |
| 2380 | flkeys = &keys; |
| 2381 | } |
| 2382 | hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; |
| 2383 | hash_keys.addrs.v6addrs.src = flkeys->addrs.v6addrs.src; |
| 2384 | hash_keys.addrs.v6addrs.dst = flkeys->addrs.v6addrs.dst; |
| 2385 | hash_keys.ports.src = flkeys->ports.src; |
| 2386 | hash_keys.ports.dst = flkeys->ports.dst; |
| 2387 | hash_keys.basic.ip_proto = flkeys->basic.ip_proto; |
| 2388 | } else { |
| 2389 | memset(&hash_keys, 0, sizeof(hash_keys)); |
| 2390 | hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; |
| 2391 | hash_keys.addrs.v6addrs.src = fl6->saddr; |
| 2392 | hash_keys.addrs.v6addrs.dst = fl6->daddr; |
| 2393 | hash_keys.ports.src = fl6->fl6_sport; |
| 2394 | hash_keys.ports.dst = fl6->fl6_dport; |
| 2395 | hash_keys.basic.ip_proto = fl6->flowi6_proto; |
| 2396 | } |
| 2397 | break; |
Stephen Suryaputra | d8f74f0 | 2019-07-06 10:55:18 -0400 | [diff] [blame] | 2398 | case 2: |
| 2399 | memset(&hash_keys, 0, sizeof(hash_keys)); |
| 2400 | hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; |
| 2401 | if (skb) { |
| 2402 | struct flow_keys keys; |
| 2403 | |
| 2404 | if (!flkeys) { |
| 2405 | skb_flow_dissect_flow_keys(skb, &keys, 0); |
| 2406 | flkeys = &keys; |
| 2407 | } |
| 2408 | |
| 2409 | /* Inner can be v4 or v6 */ |
| 2410 | if (flkeys->control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) { |
| 2411 | hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS; |
| 2412 | hash_keys.addrs.v4addrs.src = flkeys->addrs.v4addrs.src; |
| 2413 | hash_keys.addrs.v4addrs.dst = flkeys->addrs.v4addrs.dst; |
| 2414 | } else if (flkeys->control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) { |
| 2415 | hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; |
| 2416 | hash_keys.addrs.v6addrs.src = flkeys->addrs.v6addrs.src; |
| 2417 | hash_keys.addrs.v6addrs.dst = flkeys->addrs.v6addrs.dst; |
| 2418 | hash_keys.tags.flow_label = flkeys->tags.flow_label; |
| 2419 | hash_keys.basic.ip_proto = flkeys->basic.ip_proto; |
| 2420 | } else { |
| 2421 | /* Same as case 0 */ |
| 2422 | hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; |
| 2423 | ip6_multipath_l3_keys(skb, &hash_keys, flkeys); |
| 2424 | } |
| 2425 | } else { |
| 2426 | /* Same as case 0 */ |
| 2427 | hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; |
| 2428 | hash_keys.addrs.v6addrs.src = fl6->saddr; |
| 2429 | hash_keys.addrs.v6addrs.dst = fl6->daddr; |
| 2430 | hash_keys.tags.flow_label = (__force u32)flowi6_get_flowlabel(fl6); |
| 2431 | hash_keys.basic.ip_proto = fl6->flowi6_proto; |
| 2432 | } |
| 2433 | break; |
Jakub Sitnicki | 23aebda | 2017-08-23 09:58:29 +0200 | [diff] [blame] | 2434 | } |
David Ahern | 9a2a537 | 2018-03-02 08:32:15 -0800 | [diff] [blame] | 2435 | mhash = flow_hash_from_keys(&hash_keys); |
Jakub Sitnicki | 23aebda | 2017-08-23 09:58:29 +0200 | [diff] [blame] | 2436 | |
David Ahern | 9a2a537 | 2018-03-02 08:32:15 -0800 | [diff] [blame] | 2437 | return mhash >> 1; |
Jakub Sitnicki | 23aebda | 2017-08-23 09:58:29 +0200 | [diff] [blame] | 2438 | } |
| 2439 | |
Wei Wang | 67f415d | 2019-06-20 17:36:40 -0700 | [diff] [blame] | 2440 | /* Called with rcu held */ |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 2441 | void ip6_route_input(struct sk_buff *skb) |
| 2442 | { |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 2443 | const struct ipv6hdr *iph = ipv6_hdr(skb); |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 2444 | struct net *net = dev_net(skb->dev); |
Wei Wang | 67f415d | 2019-06-20 17:36:40 -0700 | [diff] [blame] | 2445 | int flags = RT6_LOOKUP_F_HAS_SADDR | RT6_LOOKUP_F_DST_NOREF; |
Jiri Benc | 904af04 | 2015-08-20 13:56:31 +0200 | [diff] [blame] | 2446 | struct ip_tunnel_info *tun_info; |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 2447 | struct flowi6 fl6 = { |
David Ahern | e0d56fd | 2016-09-10 12:09:57 -0700 | [diff] [blame] | 2448 | .flowi6_iif = skb->dev->ifindex, |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 2449 | .daddr = iph->daddr, |
| 2450 | .saddr = iph->saddr, |
YOSHIFUJI Hideaki / 吉藤英明 | 6502ca5 | 2013-01-13 05:01:51 +0000 | [diff] [blame] | 2451 | .flowlabel = ip6_flowinfo(iph), |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 2452 | .flowi6_mark = skb->mark, |
| 2453 | .flowi6_proto = iph->nexthdr, |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 2454 | }; |
Roopa Prabhu | 5e5d6fe | 2018-02-28 22:43:22 -0500 | [diff] [blame] | 2455 | struct flow_keys *flkeys = NULL, _flkeys; |
Thomas Graf | adaa70b | 2006-10-13 15:01:03 -0700 | [diff] [blame] | 2456 | |
Jiri Benc | 904af04 | 2015-08-20 13:56:31 +0200 | [diff] [blame] | 2457 | tun_info = skb_tunnel_info(skb); |
Jiri Benc | 46fa062 | 2015-08-28 20:48:19 +0200 | [diff] [blame] | 2458 | if (tun_info && !(tun_info->mode & IP_TUNNEL_INFO_TX)) |
Jiri Benc | 904af04 | 2015-08-20 13:56:31 +0200 | [diff] [blame] | 2459 | fl6.flowi6_tun_key.tun_id = tun_info->key.tun_id; |
Roopa Prabhu | 5e5d6fe | 2018-02-28 22:43:22 -0500 | [diff] [blame] | 2460 | |
| 2461 | if (fib6_rules_early_flow_dissect(net, skb, &fl6, &_flkeys)) |
| 2462 | flkeys = &_flkeys; |
| 2463 | |
Jakub Sitnicki | 23aebda | 2017-08-23 09:58:29 +0200 | [diff] [blame] | 2464 | if (unlikely(fl6.flowi6_proto == IPPROTO_ICMPV6)) |
David Ahern | b4bac17 | 2018-03-02 08:32:18 -0800 | [diff] [blame] | 2465 | fl6.mp_hash = rt6_multipath_hash(net, &fl6, skb, flkeys); |
Jiri Benc | 06e9d04 | 2015-08-20 13:56:26 +0200 | [diff] [blame] | 2466 | skb_dst_drop(skb); |
Wei Wang | 67f415d | 2019-06-20 17:36:40 -0700 | [diff] [blame] | 2467 | skb_dst_set_noref(skb, ip6_route_input_lookup(net, skb->dev, |
| 2468 | &fl6, skb, flags)); |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 2469 | } |
| 2470 | |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 2471 | static struct rt6_info *ip6_pol_route_output(struct net *net, |
| 2472 | struct fib6_table *table, |
| 2473 | struct flowi6 *fl6, |
| 2474 | const struct sk_buff *skb, |
| 2475 | int flags) |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 2476 | { |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 2477 | return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, skb, flags); |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 2478 | } |
| 2479 | |
Wei Wang | 7d9e5f4 | 2019-06-20 17:36:41 -0700 | [diff] [blame] | 2480 | struct dst_entry *ip6_route_output_flags_noref(struct net *net, |
| 2481 | const struct sock *sk, |
| 2482 | struct flowi6 *fl6, int flags) |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 2483 | { |
David Ahern | d46a9d6 | 2015-10-21 08:42:22 -0700 | [diff] [blame] | 2484 | bool any_src; |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 2485 | |
Robert Shearman | 3ede0bb | 2018-09-19 13:56:53 +0100 | [diff] [blame] | 2486 | if (ipv6_addr_type(&fl6->daddr) & |
| 2487 | (IPV6_ADDR_MULTICAST | IPV6_ADDR_LINKLOCAL)) { |
David Ahern | 4c1feac | 2016-09-10 12:09:56 -0700 | [diff] [blame] | 2488 | struct dst_entry *dst; |
| 2489 | |
Wei Wang | 7d9e5f4 | 2019-06-20 17:36:41 -0700 | [diff] [blame] | 2490 | /* This function does not take refcnt on the dst */ |
David Ahern | 4c1feac | 2016-09-10 12:09:56 -0700 | [diff] [blame] | 2491 | dst = l3mdev_link_scope_lookup(net, fl6); |
| 2492 | if (dst) |
| 2493 | return dst; |
| 2494 | } |
David Ahern | ca25449 | 2015-10-12 11:47:10 -0700 | [diff] [blame] | 2495 | |
Pavel Emelyanov | 1fb9489 | 2012-08-08 21:53:36 +0000 | [diff] [blame] | 2496 | fl6->flowi6_iif = LOOPBACK_IFINDEX; |
David McCullough | 4dc27d1c | 2012-06-25 15:42:26 +0000 | [diff] [blame] | 2497 | |
Wei Wang | 7d9e5f4 | 2019-06-20 17:36:41 -0700 | [diff] [blame] | 2498 | flags |= RT6_LOOKUP_F_DST_NOREF; |
David Ahern | d46a9d6 | 2015-10-21 08:42:22 -0700 | [diff] [blame] | 2499 | any_src = ipv6_addr_any(&fl6->saddr); |
David Ahern | 741a11d | 2015-09-28 10:12:13 -0700 | [diff] [blame] | 2500 | if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr) || |
David Ahern | d46a9d6 | 2015-10-21 08:42:22 -0700 | [diff] [blame] | 2501 | (fl6->flowi6_oif && any_src)) |
YOSHIFUJI Hideaki | 77d16f4 | 2006-08-23 17:25:05 -0700 | [diff] [blame] | 2502 | flags |= RT6_LOOKUP_F_IFACE; |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 2503 | |
David Ahern | d46a9d6 | 2015-10-21 08:42:22 -0700 | [diff] [blame] | 2504 | if (!any_src) |
Thomas Graf | adaa70b | 2006-10-13 15:01:03 -0700 | [diff] [blame] | 2505 | flags |= RT6_LOOKUP_F_HAS_SADDR; |
YOSHIFUJI Hideaki / 吉藤英明 | 0c9a2ac | 2010-03-07 00:14:44 +0000 | [diff] [blame] | 2506 | else if (sk) |
| 2507 | flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs); |
Thomas Graf | adaa70b | 2006-10-13 15:01:03 -0700 | [diff] [blame] | 2508 | |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 2509 | return fib6_rule_lookup(net, fl6, NULL, flags, ip6_pol_route_output); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2510 | } |
Wei Wang | 7d9e5f4 | 2019-06-20 17:36:41 -0700 | [diff] [blame] | 2511 | EXPORT_SYMBOL_GPL(ip6_route_output_flags_noref); |
| 2512 | |
| 2513 | struct dst_entry *ip6_route_output_flags(struct net *net, |
| 2514 | const struct sock *sk, |
| 2515 | struct flowi6 *fl6, |
| 2516 | int flags) |
| 2517 | { |
| 2518 | struct dst_entry *dst; |
| 2519 | struct rt6_info *rt6; |
| 2520 | |
| 2521 | rcu_read_lock(); |
| 2522 | dst = ip6_route_output_flags_noref(net, sk, fl6, flags); |
| 2523 | rt6 = (struct rt6_info *)dst; |
| 2524 | /* For dst cached in uncached_list, refcnt is already taken. */ |
| 2525 | if (list_empty(&rt6->rt6i_uncached) && !dst_hold_safe(dst)) { |
| 2526 | dst = &net->ipv6.ip6_null_entry->dst; |
| 2527 | dst_hold(dst); |
| 2528 | } |
| 2529 | rcu_read_unlock(); |
| 2530 | |
| 2531 | return dst; |
| 2532 | } |
Paolo Abeni | 6f21c96 | 2016-01-29 12:30:19 +0100 | [diff] [blame] | 2533 | EXPORT_SYMBOL_GPL(ip6_route_output_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2534 | |
David S. Miller | 2774c13 | 2011-03-01 14:59:04 -0800 | [diff] [blame] | 2535 | struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig) |
David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 2536 | { |
David S. Miller | 5c1e6aa | 2011-04-28 14:13:38 -0700 | [diff] [blame] | 2537 | struct rt6_info *rt, *ort = (struct rt6_info *) dst_orig; |
Wei Wang | 1dbe3252 | 2017-06-17 10:42:26 -0700 | [diff] [blame] | 2538 | struct net_device *loopback_dev = net->loopback_dev; |
David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 2539 | struct dst_entry *new = NULL; |
| 2540 | |
Wei Wang | 1dbe3252 | 2017-06-17 10:42:26 -0700 | [diff] [blame] | 2541 | rt = dst_alloc(&ip6_dst_blackhole_ops, loopback_dev, 1, |
Steffen Klassert | 62cf27e | 2017-10-09 08:39:43 +0200 | [diff] [blame] | 2542 | DST_OBSOLETE_DEAD, 0); |
David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 2543 | if (rt) { |
Martin KaFai Lau | 0a1f596 | 2015-10-15 16:39:58 -0700 | [diff] [blame] | 2544 | rt6_info_init(rt); |
Wei Wang | 81eb844 | 2017-10-06 12:06:11 -0700 | [diff] [blame] | 2545 | atomic_inc(&net->ipv6.rt6_stats->fib_rt_alloc); |
Martin KaFai Lau | 0a1f596 | 2015-10-15 16:39:58 -0700 | [diff] [blame] | 2546 | |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 2547 | new = &rt->dst; |
David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 2548 | new->__use = 1; |
Herbert Xu | 352e512 | 2007-11-13 21:34:06 -0800 | [diff] [blame] | 2549 | new->input = dst_discard; |
Eric W. Biederman | ede2059 | 2015-10-07 16:48:47 -0500 | [diff] [blame] | 2550 | new->output = dst_discard_out; |
David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 2551 | |
Martin KaFai Lau | 0a1f596 | 2015-10-15 16:39:58 -0700 | [diff] [blame] | 2552 | dst_copy_metrics(new, &ort->dst); |
David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 2553 | |
Wei Wang | 1dbe3252 | 2017-06-17 10:42:26 -0700 | [diff] [blame] | 2554 | rt->rt6i_idev = in6_dev_get(loopback_dev); |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 2555 | rt->rt6i_gateway = ort->rt6i_gateway; |
Martin KaFai Lau | 0a1f596 | 2015-10-15 16:39:58 -0700 | [diff] [blame] | 2556 | rt->rt6i_flags = ort->rt6i_flags & ~RTF_PCPU; |
David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 2557 | |
| 2558 | memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key)); |
| 2559 | #ifdef CONFIG_IPV6_SUBTREES |
| 2560 | memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key)); |
| 2561 | #endif |
David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 2562 | } |
| 2563 | |
David S. Miller | 69ead7a | 2011-03-01 14:45:33 -0800 | [diff] [blame] | 2564 | dst_release(dst_orig); |
| 2565 | return new ? new : ERR_PTR(-ENOMEM); |
David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 2566 | } |
David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 2567 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2568 | /* |
| 2569 | * Destination cache support functions |
| 2570 | */ |
| 2571 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 2572 | static bool fib6_check(struct fib6_info *f6i, u32 cookie) |
Martin KaFai Lau | 3da59bd | 2015-05-22 20:56:03 -0700 | [diff] [blame] | 2573 | { |
Steffen Klassert | 3614364 | 2017-08-25 09:05:42 +0200 | [diff] [blame] | 2574 | u32 rt_cookie = 0; |
Wei Wang | c5cff85 | 2017-08-21 09:47:10 -0700 | [diff] [blame] | 2575 | |
David Ahern | 8ae8697 | 2018-04-20 15:38:03 -0700 | [diff] [blame] | 2576 | if (!fib6_get_cookie_safe(f6i, &rt_cookie) || rt_cookie != cookie) |
David Ahern | 93531c6 | 2018-04-17 17:33:25 -0700 | [diff] [blame] | 2577 | return false; |
| 2578 | |
| 2579 | if (fib6_check_expired(f6i)) |
| 2580 | return false; |
| 2581 | |
| 2582 | return true; |
| 2583 | } |
| 2584 | |
David Ahern | a68886a | 2018-04-20 15:38:02 -0700 | [diff] [blame] | 2585 | static struct dst_entry *rt6_check(struct rt6_info *rt, |
| 2586 | struct fib6_info *from, |
| 2587 | u32 cookie) |
Martin KaFai Lau | 3da59bd | 2015-05-22 20:56:03 -0700 | [diff] [blame] | 2588 | { |
Wei Wang | c5cff85 | 2017-08-21 09:47:10 -0700 | [diff] [blame] | 2589 | u32 rt_cookie = 0; |
| 2590 | |
David Ahern | 49d05fe | 2019-07-17 15:08:43 -0700 | [diff] [blame] | 2591 | if (!from || !fib6_get_cookie_safe(from, &rt_cookie) || |
David Ahern | 93531c6 | 2018-04-17 17:33:25 -0700 | [diff] [blame] | 2592 | rt_cookie != cookie) |
Martin KaFai Lau | 3da59bd | 2015-05-22 20:56:03 -0700 | [diff] [blame] | 2593 | return NULL; |
| 2594 | |
| 2595 | if (rt6_check_expired(rt)) |
| 2596 | return NULL; |
| 2597 | |
| 2598 | return &rt->dst; |
| 2599 | } |
| 2600 | |
David Ahern | a68886a | 2018-04-20 15:38:02 -0700 | [diff] [blame] | 2601 | static struct dst_entry *rt6_dst_from_check(struct rt6_info *rt, |
| 2602 | struct fib6_info *from, |
| 2603 | u32 cookie) |
Martin KaFai Lau | 3da59bd | 2015-05-22 20:56:03 -0700 | [diff] [blame] | 2604 | { |
Martin KaFai Lau | 5973fb1 | 2015-11-11 11:51:07 -0800 | [diff] [blame] | 2605 | if (!__rt6_check_expired(rt) && |
| 2606 | rt->dst.obsolete == DST_OBSOLETE_FORCE_CHK && |
David Ahern | a68886a | 2018-04-20 15:38:02 -0700 | [diff] [blame] | 2607 | fib6_check(from, cookie)) |
Martin KaFai Lau | 3da59bd | 2015-05-22 20:56:03 -0700 | [diff] [blame] | 2608 | return &rt->dst; |
| 2609 | else |
| 2610 | return NULL; |
| 2611 | } |
| 2612 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2613 | static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie) |
| 2614 | { |
David Ahern | a87b7dc | 2018-04-20 15:38:00 -0700 | [diff] [blame] | 2615 | struct dst_entry *dst_ret; |
David Ahern | a68886a | 2018-04-20 15:38:02 -0700 | [diff] [blame] | 2616 | struct fib6_info *from; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2617 | struct rt6_info *rt; |
| 2618 | |
David Ahern | a87b7dc | 2018-04-20 15:38:00 -0700 | [diff] [blame] | 2619 | rt = container_of(dst, struct rt6_info, dst); |
| 2620 | |
David Ahern | 8f34e53 | 2020-05-01 08:53:08 -0600 | [diff] [blame] | 2621 | if (rt->sernum) |
| 2622 | return rt6_is_valid(rt) ? dst : NULL; |
| 2623 | |
David Ahern | a87b7dc | 2018-04-20 15:38:00 -0700 | [diff] [blame] | 2624 | rcu_read_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2625 | |
Nicolas Dichtel | 6f3118b | 2012-09-10 22:09:46 +0000 | [diff] [blame] | 2626 | /* All IPV6 dsts are created with ->obsolete set to the value |
| 2627 | * DST_OBSOLETE_FORCE_CHK which forces validation calls down |
| 2628 | * into this function always. |
| 2629 | */ |
Hannes Frederic Sowa | e3bc10b | 2013-10-24 07:48:24 +0200 | [diff] [blame] | 2630 | |
David Ahern | a68886a | 2018-04-20 15:38:02 -0700 | [diff] [blame] | 2631 | from = rcu_dereference(rt->from); |
Martin KaFai Lau | 4b32b5a | 2015-04-28 13:03:06 -0700 | [diff] [blame] | 2632 | |
David Ahern | a68886a | 2018-04-20 15:38:02 -0700 | [diff] [blame] | 2633 | if (from && (rt->rt6i_flags & RTF_PCPU || |
| 2634 | unlikely(!list_empty(&rt->rt6i_uncached)))) |
| 2635 | dst_ret = rt6_dst_from_check(rt, from, cookie); |
Martin KaFai Lau | 3da59bd | 2015-05-22 20:56:03 -0700 | [diff] [blame] | 2636 | else |
David Ahern | a68886a | 2018-04-20 15:38:02 -0700 | [diff] [blame] | 2637 | dst_ret = rt6_check(rt, from, cookie); |
David Ahern | a87b7dc | 2018-04-20 15:38:00 -0700 | [diff] [blame] | 2638 | |
| 2639 | rcu_read_unlock(); |
| 2640 | |
| 2641 | return dst_ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2642 | } |
| 2643 | |
| 2644 | static struct dst_entry *ip6_negative_advice(struct dst_entry *dst) |
| 2645 | { |
| 2646 | struct rt6_info *rt = (struct rt6_info *) dst; |
| 2647 | |
| 2648 | if (rt) { |
YOSHIFUJI Hideaki / 吉藤英明 | 54c1a85 | 2010-03-28 07:15:45 +0000 | [diff] [blame] | 2649 | if (rt->rt6i_flags & RTF_CACHE) { |
David Ahern | c3c14da | 2018-04-23 11:32:06 -0700 | [diff] [blame] | 2650 | rcu_read_lock(); |
YOSHIFUJI Hideaki / 吉藤英明 | 54c1a85 | 2010-03-28 07:15:45 +0000 | [diff] [blame] | 2651 | if (rt6_check_expired(rt)) { |
David Ahern | 93531c6 | 2018-04-17 17:33:25 -0700 | [diff] [blame] | 2652 | rt6_remove_exception_rt(rt); |
YOSHIFUJI Hideaki / 吉藤英明 | 54c1a85 | 2010-03-28 07:15:45 +0000 | [diff] [blame] | 2653 | dst = NULL; |
| 2654 | } |
David Ahern | c3c14da | 2018-04-23 11:32:06 -0700 | [diff] [blame] | 2655 | rcu_read_unlock(); |
YOSHIFUJI Hideaki / 吉藤英明 | 54c1a85 | 2010-03-28 07:15:45 +0000 | [diff] [blame] | 2656 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2657 | dst_release(dst); |
YOSHIFUJI Hideaki / 吉藤英明 | 54c1a85 | 2010-03-28 07:15:45 +0000 | [diff] [blame] | 2658 | dst = NULL; |
| 2659 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2660 | } |
YOSHIFUJI Hideaki / 吉藤英明 | 54c1a85 | 2010-03-28 07:15:45 +0000 | [diff] [blame] | 2661 | return dst; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2662 | } |
| 2663 | |
| 2664 | static void ip6_link_failure(struct sk_buff *skb) |
| 2665 | { |
| 2666 | struct rt6_info *rt; |
| 2667 | |
Alexey Dobriyan | 3ffe533 | 2010-02-18 08:25:24 +0000 | [diff] [blame] | 2668 | icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2669 | |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 2670 | rt = (struct rt6_info *) skb_dst(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2671 | if (rt) { |
David Ahern | 8a14e46 | 2018-04-23 11:32:07 -0700 | [diff] [blame] | 2672 | rcu_read_lock(); |
Hannes Frederic Sowa | 1eb4f75 | 2013-07-10 23:00:57 +0200 | [diff] [blame] | 2673 | if (rt->rt6i_flags & RTF_CACHE) { |
Xin Long | 761f602 | 2018-11-14 00:48:28 +0800 | [diff] [blame] | 2674 | rt6_remove_exception_rt(rt); |
Wei Wang | c5cff85 | 2017-08-21 09:47:10 -0700 | [diff] [blame] | 2675 | } else { |
David Ahern | a68886a | 2018-04-20 15:38:02 -0700 | [diff] [blame] | 2676 | struct fib6_info *from; |
Wei Wang | c5cff85 | 2017-08-21 09:47:10 -0700 | [diff] [blame] | 2677 | struct fib6_node *fn; |
| 2678 | |
David Ahern | a68886a | 2018-04-20 15:38:02 -0700 | [diff] [blame] | 2679 | from = rcu_dereference(rt->from); |
| 2680 | if (from) { |
| 2681 | fn = rcu_dereference(from->fib6_node); |
| 2682 | if (fn && (rt->rt6i_flags & RTF_DEFAULT)) |
| 2683 | fn->fn_sernum = -1; |
| 2684 | } |
Hannes Frederic Sowa | 1eb4f75 | 2013-07-10 23:00:57 +0200 | [diff] [blame] | 2685 | } |
David Ahern | 8a14e46 | 2018-04-23 11:32:07 -0700 | [diff] [blame] | 2686 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2687 | } |
| 2688 | } |
| 2689 | |
David Ahern | 6a3e030 | 2018-04-20 15:37:57 -0700 | [diff] [blame] | 2690 | static void rt6_update_expires(struct rt6_info *rt0, int timeout) |
| 2691 | { |
David Ahern | a68886a | 2018-04-20 15:38:02 -0700 | [diff] [blame] | 2692 | if (!(rt0->rt6i_flags & RTF_EXPIRES)) { |
| 2693 | struct fib6_info *from; |
| 2694 | |
| 2695 | rcu_read_lock(); |
| 2696 | from = rcu_dereference(rt0->from); |
| 2697 | if (from) |
| 2698 | rt0->dst.expires = from->expires; |
| 2699 | rcu_read_unlock(); |
| 2700 | } |
David Ahern | 6a3e030 | 2018-04-20 15:37:57 -0700 | [diff] [blame] | 2701 | |
| 2702 | dst_set_expires(&rt0->dst, timeout); |
| 2703 | rt0->rt6i_flags |= RTF_EXPIRES; |
| 2704 | } |
| 2705 | |
Martin KaFai Lau | 45e4fd2 | 2015-05-22 20:56:00 -0700 | [diff] [blame] | 2706 | static void rt6_do_update_pmtu(struct rt6_info *rt, u32 mtu) |
| 2707 | { |
| 2708 | struct net *net = dev_net(rt->dst.dev); |
| 2709 | |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 2710 | dst_metric_set(&rt->dst, RTAX_MTU, mtu); |
Martin KaFai Lau | 45e4fd2 | 2015-05-22 20:56:00 -0700 | [diff] [blame] | 2711 | rt->rt6i_flags |= RTF_MODIFIED; |
Martin KaFai Lau | 45e4fd2 | 2015-05-22 20:56:00 -0700 | [diff] [blame] | 2712 | rt6_update_expires(rt, net->ipv6.sysctl.ip6_rt_mtu_expires); |
| 2713 | } |
| 2714 | |
Martin KaFai Lau | 0d3f6d2 | 2015-11-11 11:51:06 -0800 | [diff] [blame] | 2715 | static bool rt6_cache_allowed_for_pmtu(const struct rt6_info *rt) |
| 2716 | { |
| 2717 | return !(rt->rt6i_flags & RTF_CACHE) && |
Paolo Abeni | 1490ed2 | 2019-02-15 18:15:37 +0100 | [diff] [blame] | 2718 | (rt->rt6i_flags & RTF_PCPU || rcu_access_pointer(rt->from)); |
Martin KaFai Lau | 0d3f6d2 | 2015-11-11 11:51:06 -0800 | [diff] [blame] | 2719 | } |
| 2720 | |
Martin KaFai Lau | 45e4fd2 | 2015-05-22 20:56:00 -0700 | [diff] [blame] | 2721 | static void __ip6_rt_update_pmtu(struct dst_entry *dst, const struct sock *sk, |
Hangbin Liu | bd085ef | 2019-12-22 10:51:09 +0800 | [diff] [blame] | 2722 | const struct ipv6hdr *iph, u32 mtu, |
| 2723 | bool confirm_neigh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2724 | { |
Julian Anastasov | 0dec879 | 2017-02-06 23:14:16 +0200 | [diff] [blame] | 2725 | const struct in6_addr *daddr, *saddr; |
Ian Morris | 67ba415 | 2014-08-24 21:53:10 +0100 | [diff] [blame] | 2726 | struct rt6_info *rt6 = (struct rt6_info *)dst; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2727 | |
Maciej Żenczykowski | 09454fd | 2020-05-05 11:57:23 -0700 | [diff] [blame] | 2728 | /* Note: do *NOT* check dst_metric_locked(dst, RTAX_MTU) |
| 2729 | * IPv6 pmtu discovery isn't optional, so 'mtu lock' cannot disable it. |
| 2730 | * [see also comment in rt6_mtu_change_route()] |
| 2731 | */ |
Xin Long | 19bda36 | 2016-10-28 18:18:01 +0800 | [diff] [blame] | 2732 | |
Julian Anastasov | 0dec879 | 2017-02-06 23:14:16 +0200 | [diff] [blame] | 2733 | if (iph) { |
| 2734 | daddr = &iph->daddr; |
| 2735 | saddr = &iph->saddr; |
| 2736 | } else if (sk) { |
| 2737 | daddr = &sk->sk_v6_daddr; |
| 2738 | saddr = &inet6_sk(sk)->saddr; |
| 2739 | } else { |
| 2740 | daddr = NULL; |
| 2741 | saddr = NULL; |
| 2742 | } |
Hangbin Liu | bd085ef | 2019-12-22 10:51:09 +0800 | [diff] [blame] | 2743 | |
| 2744 | if (confirm_neigh) |
| 2745 | dst_confirm_neigh(dst, daddr); |
| 2746 | |
Martin KaFai Lau | 45e4fd2 | 2015-05-22 20:56:00 -0700 | [diff] [blame] | 2747 | mtu = max_t(u32, mtu, IPV6_MIN_MTU); |
| 2748 | if (mtu >= dst_mtu(dst)) |
| 2749 | return; |
David S. Miller | 81aded2 | 2012-06-15 14:54:11 -0700 | [diff] [blame] | 2750 | |
Martin KaFai Lau | 0d3f6d2 | 2015-11-11 11:51:06 -0800 | [diff] [blame] | 2751 | if (!rt6_cache_allowed_for_pmtu(rt6)) { |
Martin KaFai Lau | 45e4fd2 | 2015-05-22 20:56:00 -0700 | [diff] [blame] | 2752 | rt6_do_update_pmtu(rt6, mtu); |
Wei Wang | 2b760fc | 2017-10-06 12:06:03 -0700 | [diff] [blame] | 2753 | /* update rt6_ex->stamp for cache */ |
| 2754 | if (rt6->rt6i_flags & RTF_CACHE) |
| 2755 | rt6_update_exception_stamp_rt(rt6); |
Julian Anastasov | 0dec879 | 2017-02-06 23:14:16 +0200 | [diff] [blame] | 2756 | } else if (daddr) { |
David Ahern | 85bd05d | 2019-04-16 14:36:01 -0700 | [diff] [blame] | 2757 | struct fib6_result res = {}; |
Martin KaFai Lau | 45e4fd2 | 2015-05-22 20:56:00 -0700 | [diff] [blame] | 2758 | struct rt6_info *nrt6; |
Hagen Paul Pfeifer | 9d28971 | 2015-01-15 22:34:25 +0100 | [diff] [blame] | 2759 | |
David Ahern | 4d85cd0 | 2018-04-20 15:37:59 -0700 | [diff] [blame] | 2760 | rcu_read_lock(); |
David Ahern | 85bd05d | 2019-04-16 14:36:01 -0700 | [diff] [blame] | 2761 | res.f6i = rcu_dereference(rt6->from); |
David Ahern | 43a4b60 | 2019-08-01 15:18:08 -0700 | [diff] [blame] | 2762 | if (!res.f6i) |
| 2763 | goto out_unlock; |
| 2764 | |
David Ahern | 7d21fec | 2019-04-16 14:36:11 -0700 | [diff] [blame] | 2765 | res.fib6_flags = res.f6i->fib6_flags; |
| 2766 | res.fib6_type = res.f6i->fib6_type; |
| 2767 | |
David Ahern | 2d44234 | 2019-06-08 14:53:31 -0700 | [diff] [blame] | 2768 | if (res.f6i->nh) { |
| 2769 | struct fib6_nh_match_arg arg = { |
| 2770 | .dev = dst->dev, |
| 2771 | .gw = &rt6->rt6i_gateway, |
| 2772 | }; |
| 2773 | |
| 2774 | nexthop_for_each_fib6_nh(res.f6i->nh, |
| 2775 | fib6_nh_find_match, &arg); |
| 2776 | |
| 2777 | /* fib6_info uses a nexthop that does not have fib6_nh |
| 2778 | * using the dst->dev + gw. Should be impossible. |
| 2779 | */ |
David Ahern | 43a4b60 | 2019-08-01 15:18:08 -0700 | [diff] [blame] | 2780 | if (!arg.match) |
| 2781 | goto out_unlock; |
David Ahern | 2d44234 | 2019-06-08 14:53:31 -0700 | [diff] [blame] | 2782 | |
| 2783 | res.nh = arg.match; |
| 2784 | } else { |
| 2785 | res.nh = res.f6i->fib6_nh; |
| 2786 | } |
| 2787 | |
David Ahern | 85bd05d | 2019-04-16 14:36:01 -0700 | [diff] [blame] | 2788 | nrt6 = ip6_rt_cache_alloc(&res, daddr, saddr); |
Martin KaFai Lau | 45e4fd2 | 2015-05-22 20:56:00 -0700 | [diff] [blame] | 2789 | if (nrt6) { |
| 2790 | rt6_do_update_pmtu(nrt6, mtu); |
David Ahern | 5012f0a | 2019-04-16 14:36:05 -0700 | [diff] [blame] | 2791 | if (rt6_insert_exception(nrt6, &res)) |
Wei Wang | 2b760fc | 2017-10-06 12:06:03 -0700 | [diff] [blame] | 2792 | dst_release_immediate(&nrt6->dst); |
Martin KaFai Lau | 45e4fd2 | 2015-05-22 20:56:00 -0700 | [diff] [blame] | 2793 | } |
David Ahern | 43a4b60 | 2019-08-01 15:18:08 -0700 | [diff] [blame] | 2794 | out_unlock: |
David Ahern | a68886a | 2018-04-20 15:38:02 -0700 | [diff] [blame] | 2795 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2796 | } |
| 2797 | } |
| 2798 | |
Martin KaFai Lau | 45e4fd2 | 2015-05-22 20:56:00 -0700 | [diff] [blame] | 2799 | static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk, |
Hangbin Liu | bd085ef | 2019-12-22 10:51:09 +0800 | [diff] [blame] | 2800 | struct sk_buff *skb, u32 mtu, |
| 2801 | bool confirm_neigh) |
Martin KaFai Lau | 45e4fd2 | 2015-05-22 20:56:00 -0700 | [diff] [blame] | 2802 | { |
Hangbin Liu | bd085ef | 2019-12-22 10:51:09 +0800 | [diff] [blame] | 2803 | __ip6_rt_update_pmtu(dst, sk, skb ? ipv6_hdr(skb) : NULL, mtu, |
| 2804 | confirm_neigh); |
Martin KaFai Lau | 45e4fd2 | 2015-05-22 20:56:00 -0700 | [diff] [blame] | 2805 | } |
| 2806 | |
David S. Miller | 42ae66c | 2012-06-15 20:01:57 -0700 | [diff] [blame] | 2807 | void ip6_update_pmtu(struct sk_buff *skb, struct net *net, __be32 mtu, |
Lorenzo Colitti | e2d118a | 2016-11-04 02:23:43 +0900 | [diff] [blame] | 2808 | int oif, u32 mark, kuid_t uid) |
David S. Miller | 81aded2 | 2012-06-15 14:54:11 -0700 | [diff] [blame] | 2809 | { |
| 2810 | const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data; |
| 2811 | struct dst_entry *dst; |
Maciej Żenczykowski | dc92095 | 2018-09-29 23:44:51 -0700 | [diff] [blame] | 2812 | struct flowi6 fl6 = { |
| 2813 | .flowi6_oif = oif, |
| 2814 | .flowi6_mark = mark ? mark : IP6_REPLY_MARK(net, skb->mark), |
| 2815 | .daddr = iph->daddr, |
| 2816 | .saddr = iph->saddr, |
| 2817 | .flowlabel = ip6_flowinfo(iph), |
| 2818 | .flowi6_uid = uid, |
| 2819 | }; |
David S. Miller | 81aded2 | 2012-06-15 14:54:11 -0700 | [diff] [blame] | 2820 | |
| 2821 | dst = ip6_route_output(net, NULL, &fl6); |
| 2822 | if (!dst->error) |
Hangbin Liu | bd085ef | 2019-12-22 10:51:09 +0800 | [diff] [blame] | 2823 | __ip6_rt_update_pmtu(dst, NULL, iph, ntohl(mtu), true); |
David S. Miller | 81aded2 | 2012-06-15 14:54:11 -0700 | [diff] [blame] | 2824 | dst_release(dst); |
| 2825 | } |
| 2826 | EXPORT_SYMBOL_GPL(ip6_update_pmtu); |
| 2827 | |
| 2828 | void ip6_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, __be32 mtu) |
| 2829 | { |
David Ahern | 7ddacfa | 2018-11-18 10:45:30 -0800 | [diff] [blame] | 2830 | int oif = sk->sk_bound_dev_if; |
Martin KaFai Lau | 33c162a | 2016-04-11 15:29:36 -0700 | [diff] [blame] | 2831 | struct dst_entry *dst; |
| 2832 | |
David Ahern | 7ddacfa | 2018-11-18 10:45:30 -0800 | [diff] [blame] | 2833 | if (!oif && skb->dev) |
| 2834 | oif = l3mdev_master_ifindex(skb->dev); |
| 2835 | |
| 2836 | ip6_update_pmtu(skb, sock_net(sk), mtu, oif, sk->sk_mark, sk->sk_uid); |
Martin KaFai Lau | 33c162a | 2016-04-11 15:29:36 -0700 | [diff] [blame] | 2837 | |
| 2838 | dst = __sk_dst_get(sk); |
| 2839 | if (!dst || !dst->obsolete || |
| 2840 | dst->ops->check(dst, inet6_sk(sk)->dst_cookie)) |
| 2841 | return; |
| 2842 | |
| 2843 | bh_lock_sock(sk); |
| 2844 | if (!sock_owned_by_user(sk) && !ipv6_addr_v4mapped(&sk->sk_v6_daddr)) |
| 2845 | ip6_datagram_dst_update(sk, false); |
| 2846 | bh_unlock_sock(sk); |
David S. Miller | 81aded2 | 2012-06-15 14:54:11 -0700 | [diff] [blame] | 2847 | } |
| 2848 | EXPORT_SYMBOL_GPL(ip6_sk_update_pmtu); |
| 2849 | |
Alexey Kodanev | 7d6850f | 2018-04-03 15:00:07 +0300 | [diff] [blame] | 2850 | void ip6_sk_dst_store_flow(struct sock *sk, struct dst_entry *dst, |
| 2851 | const struct flowi6 *fl6) |
| 2852 | { |
| 2853 | #ifdef CONFIG_IPV6_SUBTREES |
| 2854 | struct ipv6_pinfo *np = inet6_sk(sk); |
| 2855 | #endif |
| 2856 | |
| 2857 | ip6_dst_store(sk, dst, |
| 2858 | ipv6_addr_equal(&fl6->daddr, &sk->sk_v6_daddr) ? |
| 2859 | &sk->sk_v6_daddr : NULL, |
| 2860 | #ifdef CONFIG_IPV6_SUBTREES |
| 2861 | ipv6_addr_equal(&fl6->saddr, &np->saddr) ? |
| 2862 | &np->saddr : |
| 2863 | #endif |
| 2864 | NULL); |
| 2865 | } |
| 2866 | |
David Ahern | 9b6b35a | 2019-04-16 14:36:02 -0700 | [diff] [blame] | 2867 | static bool ip6_redirect_nh_match(const struct fib6_result *res, |
David Ahern | 0b34eb0 | 2019-04-09 14:41:19 -0700 | [diff] [blame] | 2868 | struct flowi6 *fl6, |
| 2869 | const struct in6_addr *gw, |
| 2870 | struct rt6_info **ret) |
| 2871 | { |
David Ahern | 9b6b35a | 2019-04-16 14:36:02 -0700 | [diff] [blame] | 2872 | const struct fib6_nh *nh = res->nh; |
| 2873 | |
David Ahern | 0b34eb0 | 2019-04-09 14:41:19 -0700 | [diff] [blame] | 2874 | if (nh->fib_nh_flags & RTNH_F_DEAD || !nh->fib_nh_gw_family || |
| 2875 | fl6->flowi6_oif != nh->fib_nh_dev->ifindex) |
| 2876 | return false; |
| 2877 | |
| 2878 | /* rt_cache's gateway might be different from its 'parent' |
| 2879 | * in the case of an ip redirect. |
| 2880 | * So we keep searching in the exception table if the gateway |
| 2881 | * is different. |
| 2882 | */ |
| 2883 | if (!ipv6_addr_equal(gw, &nh->fib_nh_gw6)) { |
| 2884 | struct rt6_info *rt_cache; |
| 2885 | |
David Ahern | 9b6b35a | 2019-04-16 14:36:02 -0700 | [diff] [blame] | 2886 | rt_cache = rt6_find_cached_rt(res, &fl6->daddr, &fl6->saddr); |
David Ahern | 0b34eb0 | 2019-04-09 14:41:19 -0700 | [diff] [blame] | 2887 | if (rt_cache && |
| 2888 | ipv6_addr_equal(gw, &rt_cache->rt6i_gateway)) { |
| 2889 | *ret = rt_cache; |
| 2890 | return true; |
| 2891 | } |
| 2892 | return false; |
| 2893 | } |
| 2894 | return true; |
| 2895 | } |
| 2896 | |
David Ahern | c55c898 | 2019-06-08 14:53:29 -0700 | [diff] [blame] | 2897 | struct fib6_nh_rd_arg { |
| 2898 | struct fib6_result *res; |
| 2899 | struct flowi6 *fl6; |
| 2900 | const struct in6_addr *gw; |
| 2901 | struct rt6_info **ret; |
| 2902 | }; |
| 2903 | |
| 2904 | static int fib6_nh_redirect_match(struct fib6_nh *nh, void *_arg) |
| 2905 | { |
| 2906 | struct fib6_nh_rd_arg *arg = _arg; |
| 2907 | |
| 2908 | arg->res->nh = nh; |
| 2909 | return ip6_redirect_nh_match(arg->res, arg->fl6, arg->gw, arg->ret); |
| 2910 | } |
| 2911 | |
Duan Jiong | b55b76b | 2013-09-04 19:44:21 +0800 | [diff] [blame] | 2912 | /* Handle redirects */ |
| 2913 | struct ip6rd_flowi { |
| 2914 | struct flowi6 fl6; |
| 2915 | struct in6_addr gateway; |
| 2916 | }; |
| 2917 | |
| 2918 | static struct rt6_info *__ip6_route_redirect(struct net *net, |
| 2919 | struct fib6_table *table, |
| 2920 | struct flowi6 *fl6, |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 2921 | const struct sk_buff *skb, |
Duan Jiong | b55b76b | 2013-09-04 19:44:21 +0800 | [diff] [blame] | 2922 | int flags) |
| 2923 | { |
| 2924 | struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl6; |
David Ahern | 0b34eb0 | 2019-04-09 14:41:19 -0700 | [diff] [blame] | 2925 | struct rt6_info *ret = NULL; |
David Ahern | 9b6b35a | 2019-04-16 14:36:02 -0700 | [diff] [blame] | 2926 | struct fib6_result res = {}; |
David Ahern | c55c898 | 2019-06-08 14:53:29 -0700 | [diff] [blame] | 2927 | struct fib6_nh_rd_arg arg = { |
| 2928 | .res = &res, |
| 2929 | .fl6 = fl6, |
| 2930 | .gw = &rdfl->gateway, |
| 2931 | .ret = &ret |
| 2932 | }; |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 2933 | struct fib6_info *rt; |
Duan Jiong | b55b76b | 2013-09-04 19:44:21 +0800 | [diff] [blame] | 2934 | struct fib6_node *fn; |
| 2935 | |
David Ahern | 31680ac | 2019-05-22 15:12:18 -0700 | [diff] [blame] | 2936 | /* l3mdev_update_flow overrides oif if the device is enslaved; in |
| 2937 | * this case we must match on the real ingress device, so reset it |
| 2938 | */ |
| 2939 | if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF) |
| 2940 | fl6->flowi6_oif = skb->dev->ifindex; |
| 2941 | |
Duan Jiong | b55b76b | 2013-09-04 19:44:21 +0800 | [diff] [blame] | 2942 | /* Get the "current" route for this destination and |
Alexander Alemayhu | 67c408c | 2017-01-07 23:53:00 +0100 | [diff] [blame] | 2943 | * check if the redirect has come from appropriate router. |
Duan Jiong | b55b76b | 2013-09-04 19:44:21 +0800 | [diff] [blame] | 2944 | * |
| 2945 | * RFC 4861 specifies that redirects should only be |
| 2946 | * accepted if they come from the nexthop to the target. |
| 2947 | * Due to the way the routes are chosen, this notion |
| 2948 | * is a bit fuzzy and one might need to check all possible |
| 2949 | * routes. |
| 2950 | */ |
| 2951 | |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 2952 | rcu_read_lock(); |
David Ahern | 6454743 | 2018-05-09 20:34:19 -0700 | [diff] [blame] | 2953 | fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr); |
Duan Jiong | b55b76b | 2013-09-04 19:44:21 +0800 | [diff] [blame] | 2954 | restart: |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 2955 | for_each_fib6_node_rt_rcu(fn) { |
David Ahern | 9b6b35a | 2019-04-16 14:36:02 -0700 | [diff] [blame] | 2956 | res.f6i = rt; |
David Ahern | 1489568 | 2018-04-17 17:33:17 -0700 | [diff] [blame] | 2957 | if (fib6_check_expired(rt)) |
Duan Jiong | b55b76b | 2013-09-04 19:44:21 +0800 | [diff] [blame] | 2958 | continue; |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 2959 | if (rt->fib6_flags & RTF_REJECT) |
Duan Jiong | b55b76b | 2013-09-04 19:44:21 +0800 | [diff] [blame] | 2960 | break; |
David Ahern | c55c898 | 2019-06-08 14:53:29 -0700 | [diff] [blame] | 2961 | if (unlikely(rt->nh)) { |
| 2962 | if (nexthop_is_blackhole(rt->nh)) |
| 2963 | continue; |
| 2964 | /* on match, res->nh is filled in and potentially ret */ |
| 2965 | if (nexthop_for_each_fib6_nh(rt->nh, |
| 2966 | fib6_nh_redirect_match, |
| 2967 | &arg)) |
| 2968 | goto out; |
| 2969 | } else { |
| 2970 | res.nh = rt->fib6_nh; |
| 2971 | if (ip6_redirect_nh_match(&res, fl6, &rdfl->gateway, |
| 2972 | &ret)) |
| 2973 | goto out; |
| 2974 | } |
Duan Jiong | b55b76b | 2013-09-04 19:44:21 +0800 | [diff] [blame] | 2975 | } |
| 2976 | |
| 2977 | if (!rt) |
David Ahern | 421842e | 2018-04-17 17:33:18 -0700 | [diff] [blame] | 2978 | rt = net->ipv6.fib6_null_entry; |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 2979 | else if (rt->fib6_flags & RTF_REJECT) { |
David Ahern | 23fb93a | 2018-04-17 17:33:23 -0700 | [diff] [blame] | 2980 | ret = net->ipv6.ip6_null_entry; |
Martin KaFai Lau | b0a1ba5 | 2015-01-20 19:16:02 -0800 | [diff] [blame] | 2981 | goto out; |
| 2982 | } |
| 2983 | |
David Ahern | 421842e | 2018-04-17 17:33:18 -0700 | [diff] [blame] | 2984 | if (rt == net->ipv6.fib6_null_entry) { |
Martin KaFai Lau | a3c00e4 | 2014-10-20 13:42:43 -0700 | [diff] [blame] | 2985 | fn = fib6_backtrack(fn, &fl6->saddr); |
| 2986 | if (fn) |
| 2987 | goto restart; |
Duan Jiong | b55b76b | 2013-09-04 19:44:21 +0800 | [diff] [blame] | 2988 | } |
Martin KaFai Lau | a3c00e4 | 2014-10-20 13:42:43 -0700 | [diff] [blame] | 2989 | |
David Ahern | 9b6b35a | 2019-04-16 14:36:02 -0700 | [diff] [blame] | 2990 | res.f6i = rt; |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 2991 | res.nh = rt->fib6_nh; |
Martin KaFai Lau | b0a1ba5 | 2015-01-20 19:16:02 -0800 | [diff] [blame] | 2992 | out: |
David Ahern | 7d21fec | 2019-04-16 14:36:11 -0700 | [diff] [blame] | 2993 | if (ret) { |
David Ahern | 10585b4 | 2019-03-20 09:24:50 -0700 | [diff] [blame] | 2994 | ip6_hold_safe(net, &ret); |
David Ahern | 7d21fec | 2019-04-16 14:36:11 -0700 | [diff] [blame] | 2995 | } else { |
| 2996 | res.fib6_flags = res.f6i->fib6_flags; |
| 2997 | res.fib6_type = res.f6i->fib6_type; |
David Ahern | 9b6b35a | 2019-04-16 14:36:02 -0700 | [diff] [blame] | 2998 | ret = ip6_create_rt_rcu(&res); |
David Ahern | 7d21fec | 2019-04-16 14:36:11 -0700 | [diff] [blame] | 2999 | } |
Duan Jiong | b55b76b | 2013-09-04 19:44:21 +0800 | [diff] [blame] | 3000 | |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 3001 | rcu_read_unlock(); |
Duan Jiong | b55b76b | 2013-09-04 19:44:21 +0800 | [diff] [blame] | 3002 | |
David Ahern | 8ff2e5b | 2019-04-16 14:36:09 -0700 | [diff] [blame] | 3003 | trace_fib6_table_lookup(net, &res, table, fl6); |
David Ahern | 23fb93a | 2018-04-17 17:33:23 -0700 | [diff] [blame] | 3004 | return ret; |
Duan Jiong | b55b76b | 2013-09-04 19:44:21 +0800 | [diff] [blame] | 3005 | }; |
| 3006 | |
| 3007 | static struct dst_entry *ip6_route_redirect(struct net *net, |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 3008 | const struct flowi6 *fl6, |
| 3009 | const struct sk_buff *skb, |
| 3010 | const struct in6_addr *gateway) |
Duan Jiong | b55b76b | 2013-09-04 19:44:21 +0800 | [diff] [blame] | 3011 | { |
| 3012 | int flags = RT6_LOOKUP_F_HAS_SADDR; |
| 3013 | struct ip6rd_flowi rdfl; |
| 3014 | |
| 3015 | rdfl.fl6 = *fl6; |
| 3016 | rdfl.gateway = *gateway; |
| 3017 | |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 3018 | return fib6_rule_lookup(net, &rdfl.fl6, skb, |
Duan Jiong | b55b76b | 2013-09-04 19:44:21 +0800 | [diff] [blame] | 3019 | flags, __ip6_route_redirect); |
| 3020 | } |
| 3021 | |
Lorenzo Colitti | e2d118a | 2016-11-04 02:23:43 +0900 | [diff] [blame] | 3022 | void ip6_redirect(struct sk_buff *skb, struct net *net, int oif, u32 mark, |
| 3023 | kuid_t uid) |
David S. Miller | 3a5ad2e | 2012-07-12 00:08:07 -0700 | [diff] [blame] | 3024 | { |
| 3025 | const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data; |
| 3026 | struct dst_entry *dst; |
Maciej Żenczykowski | 1f7f10a | 2018-09-29 23:44:48 -0700 | [diff] [blame] | 3027 | struct flowi6 fl6 = { |
| 3028 | .flowi6_iif = LOOPBACK_IFINDEX, |
| 3029 | .flowi6_oif = oif, |
| 3030 | .flowi6_mark = mark, |
| 3031 | .daddr = iph->daddr, |
| 3032 | .saddr = iph->saddr, |
| 3033 | .flowlabel = ip6_flowinfo(iph), |
| 3034 | .flowi6_uid = uid, |
| 3035 | }; |
David S. Miller | 3a5ad2e | 2012-07-12 00:08:07 -0700 | [diff] [blame] | 3036 | |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 3037 | dst = ip6_route_redirect(net, &fl6, skb, &ipv6_hdr(skb)->saddr); |
Duan Jiong | b55b76b | 2013-09-04 19:44:21 +0800 | [diff] [blame] | 3038 | rt6_do_redirect(dst, NULL, skb); |
David S. Miller | 3a5ad2e | 2012-07-12 00:08:07 -0700 | [diff] [blame] | 3039 | dst_release(dst); |
| 3040 | } |
| 3041 | EXPORT_SYMBOL_GPL(ip6_redirect); |
| 3042 | |
Maciej Żenczykowski | d456336 | 2018-09-29 23:44:50 -0700 | [diff] [blame] | 3043 | void ip6_redirect_no_header(struct sk_buff *skb, struct net *net, int oif) |
Duan Jiong | c92a59e | 2013-08-22 12:07:35 +0800 | [diff] [blame] | 3044 | { |
| 3045 | const struct ipv6hdr *iph = ipv6_hdr(skb); |
| 3046 | const struct rd_msg *msg = (struct rd_msg *)icmp6_hdr(skb); |
| 3047 | struct dst_entry *dst; |
Maciej Żenczykowski | 0b26fb1 | 2018-09-29 23:44:49 -0700 | [diff] [blame] | 3048 | struct flowi6 fl6 = { |
| 3049 | .flowi6_iif = LOOPBACK_IFINDEX, |
| 3050 | .flowi6_oif = oif, |
Maciej Żenczykowski | 0b26fb1 | 2018-09-29 23:44:49 -0700 | [diff] [blame] | 3051 | .daddr = msg->dest, |
| 3052 | .saddr = iph->daddr, |
| 3053 | .flowi6_uid = sock_net_uid(net, NULL), |
| 3054 | }; |
Duan Jiong | c92a59e | 2013-08-22 12:07:35 +0800 | [diff] [blame] | 3055 | |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 3056 | dst = ip6_route_redirect(net, &fl6, skb, &iph->saddr); |
Duan Jiong | b55b76b | 2013-09-04 19:44:21 +0800 | [diff] [blame] | 3057 | rt6_do_redirect(dst, NULL, skb); |
Duan Jiong | c92a59e | 2013-08-22 12:07:35 +0800 | [diff] [blame] | 3058 | dst_release(dst); |
| 3059 | } |
| 3060 | |
David S. Miller | 3a5ad2e | 2012-07-12 00:08:07 -0700 | [diff] [blame] | 3061 | void ip6_sk_redirect(struct sk_buff *skb, struct sock *sk) |
| 3062 | { |
Lorenzo Colitti | e2d118a | 2016-11-04 02:23:43 +0900 | [diff] [blame] | 3063 | ip6_redirect(skb, sock_net(sk), sk->sk_bound_dev_if, sk->sk_mark, |
| 3064 | sk->sk_uid); |
David S. Miller | 3a5ad2e | 2012-07-12 00:08:07 -0700 | [diff] [blame] | 3065 | } |
| 3066 | EXPORT_SYMBOL_GPL(ip6_sk_redirect); |
| 3067 | |
David S. Miller | 0dbaee3 | 2010-12-13 12:52:14 -0800 | [diff] [blame] | 3068 | static unsigned int ip6_default_advmss(const struct dst_entry *dst) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3069 | { |
David S. Miller | 0dbaee3 | 2010-12-13 12:52:14 -0800 | [diff] [blame] | 3070 | struct net_device *dev = dst->dev; |
| 3071 | unsigned int mtu = dst_mtu(dst); |
| 3072 | struct net *net = dev_net(dev); |
| 3073 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3074 | mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr); |
| 3075 | |
Daniel Lezcano | 5578689 | 2008-03-04 13:47:47 -0800 | [diff] [blame] | 3076 | if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss) |
| 3077 | mtu = net->ipv6.sysctl.ip6_rt_min_advmss; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3078 | |
| 3079 | /* |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 3080 | * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and |
| 3081 | * corresponding MSS is IPV6_MAXPLEN - tcp_header_size. |
| 3082 | * IPV6_MAXPLEN is also valid and means: "any MSS, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3083 | * rely only on pmtu discovery" |
| 3084 | */ |
| 3085 | if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr)) |
| 3086 | mtu = IPV6_MAXPLEN; |
| 3087 | return mtu; |
| 3088 | } |
| 3089 | |
Steffen Klassert | ebb762f | 2011-11-23 02:12:51 +0000 | [diff] [blame] | 3090 | static unsigned int ip6_mtu(const struct dst_entry *dst) |
David S. Miller | d33e455 | 2010-12-14 13:01:14 -0800 | [diff] [blame] | 3091 | { |
David S. Miller | d33e455 | 2010-12-14 13:01:14 -0800 | [diff] [blame] | 3092 | struct inet6_dev *idev; |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 3093 | unsigned int mtu; |
Steffen Klassert | 618f9bc | 2011-11-23 02:13:31 +0000 | [diff] [blame] | 3094 | |
Martin KaFai Lau | 4b32b5a | 2015-04-28 13:03:06 -0700 | [diff] [blame] | 3095 | mtu = dst_metric_raw(dst, RTAX_MTU); |
| 3096 | if (mtu) |
| 3097 | goto out; |
| 3098 | |
Steffen Klassert | 618f9bc | 2011-11-23 02:13:31 +0000 | [diff] [blame] | 3099 | mtu = IPV6_MIN_MTU; |
David S. Miller | d33e455 | 2010-12-14 13:01:14 -0800 | [diff] [blame] | 3100 | |
| 3101 | rcu_read_lock(); |
| 3102 | idev = __in6_dev_get(dst->dev); |
| 3103 | if (idev) |
| 3104 | mtu = idev->cnf.mtu6; |
| 3105 | rcu_read_unlock(); |
| 3106 | |
Eric Dumazet | 30f78d8 | 2014-04-10 21:23:36 -0700 | [diff] [blame] | 3107 | out: |
Roopa Prabhu | 14972cb | 2016-08-24 20:10:43 -0700 | [diff] [blame] | 3108 | mtu = min_t(unsigned int, mtu, IP6_MAX_MTU); |
| 3109 | |
| 3110 | return mtu - lwtunnel_headroom(dst->lwtstate, mtu); |
David S. Miller | d33e455 | 2010-12-14 13:01:14 -0800 | [diff] [blame] | 3111 | } |
| 3112 | |
David Ahern | 901731b | 2018-05-21 09:08:14 -0700 | [diff] [blame] | 3113 | /* MTU selection: |
| 3114 | * 1. mtu on route is locked - use it |
| 3115 | * 2. mtu from nexthop exception |
| 3116 | * 3. mtu from egress device |
| 3117 | * |
| 3118 | * based on ip6_dst_mtu_forward and exception logic of |
| 3119 | * rt6_find_cached_rt; called with rcu_read_lock |
| 3120 | */ |
David Ahern | b748f26 | 2019-04-16 14:36:06 -0700 | [diff] [blame] | 3121 | u32 ip6_mtu_from_fib6(const struct fib6_result *res, |
| 3122 | const struct in6_addr *daddr, |
| 3123 | const struct in6_addr *saddr) |
David Ahern | 901731b | 2018-05-21 09:08:14 -0700 | [diff] [blame] | 3124 | { |
David Ahern | b748f26 | 2019-04-16 14:36:06 -0700 | [diff] [blame] | 3125 | const struct fib6_nh *nh = res->nh; |
| 3126 | struct fib6_info *f6i = res->f6i; |
David Ahern | 901731b | 2018-05-21 09:08:14 -0700 | [diff] [blame] | 3127 | struct inet6_dev *idev; |
Wei Wang | 510e2ce | 2019-05-16 13:30:54 -0700 | [diff] [blame] | 3128 | struct rt6_info *rt; |
David Ahern | 901731b | 2018-05-21 09:08:14 -0700 | [diff] [blame] | 3129 | u32 mtu = 0; |
| 3130 | |
| 3131 | if (unlikely(fib6_metric_locked(f6i, RTAX_MTU))) { |
| 3132 | mtu = f6i->fib6_pmtu; |
| 3133 | if (mtu) |
| 3134 | goto out; |
| 3135 | } |
| 3136 | |
Wei Wang | 510e2ce | 2019-05-16 13:30:54 -0700 | [diff] [blame] | 3137 | rt = rt6_find_cached_rt(res, daddr, saddr); |
| 3138 | if (unlikely(rt)) { |
| 3139 | mtu = dst_metric_raw(&rt->dst, RTAX_MTU); |
| 3140 | } else { |
David Ahern | b748f26 | 2019-04-16 14:36:06 -0700 | [diff] [blame] | 3141 | struct net_device *dev = nh->fib_nh_dev; |
David Ahern | 901731b | 2018-05-21 09:08:14 -0700 | [diff] [blame] | 3142 | |
| 3143 | mtu = IPV6_MIN_MTU; |
| 3144 | idev = __in6_dev_get(dev); |
| 3145 | if (idev && idev->cnf.mtu6 > mtu) |
| 3146 | mtu = idev->cnf.mtu6; |
| 3147 | } |
| 3148 | |
| 3149 | mtu = min_t(unsigned int, mtu, IP6_MAX_MTU); |
| 3150 | out: |
David Ahern | b748f26 | 2019-04-16 14:36:06 -0700 | [diff] [blame] | 3151 | return mtu - lwtunnel_headroom(nh->fib_nh_lws, mtu); |
David Ahern | 901731b | 2018-05-21 09:08:14 -0700 | [diff] [blame] | 3152 | } |
| 3153 | |
YOSHIFUJI Hideaki | 3b00944 | 2007-12-06 16:11:48 -0800 | [diff] [blame] | 3154 | struct dst_entry *icmp6_dst_alloc(struct net_device *dev, |
David S. Miller | 87a1157 | 2011-12-06 17:04:13 -0500 | [diff] [blame] | 3155 | struct flowi6 *fl6) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3156 | { |
David S. Miller | 87a1157 | 2011-12-06 17:04:13 -0500 | [diff] [blame] | 3157 | struct dst_entry *dst; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3158 | struct rt6_info *rt; |
| 3159 | struct inet6_dev *idev = in6_dev_get(dev); |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 3160 | struct net *net = dev_net(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3161 | |
David S. Miller | 3830847 | 2011-12-03 18:02:47 -0500 | [diff] [blame] | 3162 | if (unlikely(!idev)) |
Eric Dumazet | 122bdf6 | 2012-03-14 21:13:11 +0000 | [diff] [blame] | 3163 | return ERR_PTR(-ENODEV); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3164 | |
Martin KaFai Lau | ad70686 | 2015-08-14 11:05:52 -0700 | [diff] [blame] | 3165 | rt = ip6_dst_alloc(net, dev, 0); |
David S. Miller | 3830847 | 2011-12-03 18:02:47 -0500 | [diff] [blame] | 3166 | if (unlikely(!rt)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3167 | in6_dev_put(idev); |
David S. Miller | 87a1157 | 2011-12-06 17:04:13 -0500 | [diff] [blame] | 3168 | dst = ERR_PTR(-ENOMEM); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3169 | goto out; |
| 3170 | } |
| 3171 | |
Brendan McGrath | 588753f | 2017-12-13 22:14:57 +1100 | [diff] [blame] | 3172 | rt->dst.input = ip6_input; |
Yan, Zheng | 8e2ec63 | 2011-09-05 21:34:30 +0000 | [diff] [blame] | 3173 | rt->dst.output = ip6_output; |
Julian Anastasov | 550bab4 | 2013-10-20 15:43:04 +0300 | [diff] [blame] | 3174 | rt->rt6i_gateway = fl6->daddr; |
David S. Miller | 87a1157 | 2011-12-06 17:04:13 -0500 | [diff] [blame] | 3175 | rt->rt6i_dst.addr = fl6->daddr; |
Yan, Zheng | 8e2ec63 | 2011-09-05 21:34:30 +0000 | [diff] [blame] | 3176 | rt->rt6i_dst.plen = 128; |
| 3177 | rt->rt6i_idev = idev; |
Li RongQing | 14edd87 | 2012-10-24 14:01:18 +0800 | [diff] [blame] | 3178 | dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3179 | |
Ido Schimmel | 4c981e2 | 2018-01-07 12:45:04 +0200 | [diff] [blame] | 3180 | /* Add this dst into uncached_list so that rt6_disable_ip() can |
Wei Wang | 587fea7 | 2017-06-17 10:42:36 -0700 | [diff] [blame] | 3181 | * do proper release of the net_device |
| 3182 | */ |
| 3183 | rt6_uncached_list_add(rt); |
Wei Wang | 81eb844 | 2017-10-06 12:06:11 -0700 | [diff] [blame] | 3184 | atomic_inc(&net->ipv6.rt6_stats->fib_rt_uncache); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3185 | |
David S. Miller | 87a1157 | 2011-12-06 17:04:13 -0500 | [diff] [blame] | 3186 | dst = xfrm_lookup(net, &rt->dst, flowi6_to_flowi(fl6), NULL, 0); |
| 3187 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3188 | out: |
David S. Miller | 87a1157 | 2011-12-06 17:04:13 -0500 | [diff] [blame] | 3189 | return dst; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3190 | } |
| 3191 | |
Daniel Lezcano | 569d364 | 2008-01-18 03:56:57 -0800 | [diff] [blame] | 3192 | static int ip6_dst_gc(struct dst_ops *ops) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3193 | { |
Alexey Dobriyan | 86393e5 | 2009-08-29 01:34:49 +0000 | [diff] [blame] | 3194 | struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops); |
Daniel Lezcano | 7019b78 | 2008-03-04 13:50:14 -0800 | [diff] [blame] | 3195 | int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval; |
| 3196 | int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size; |
| 3197 | int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity; |
| 3198 | int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout; |
| 3199 | unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc; |
Eric Dumazet | fc66f95 | 2010-10-08 06:37:34 +0000 | [diff] [blame] | 3200 | int entries; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3201 | |
Eric Dumazet | fc66f95 | 2010-10-08 06:37:34 +0000 | [diff] [blame] | 3202 | entries = dst_entries_get_fast(ops); |
Eric Dumazet | cf86a08 | 2020-05-07 18:58:10 -0700 | [diff] [blame] | 3203 | if (entries > rt_max_size) |
| 3204 | entries = dst_entries_get_slow(ops); |
| 3205 | |
Michal Kubeček | 49a18d8 | 2013-08-01 10:04:24 +0200 | [diff] [blame] | 3206 | if (time_after(rt_last_gc + rt_min_interval, jiffies) && |
Eric Dumazet | fc66f95 | 2010-10-08 06:37:34 +0000 | [diff] [blame] | 3207 | entries <= rt_max_size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3208 | goto out; |
| 3209 | |
Benjamin Thery | 6891a34 | 2008-03-04 13:49:47 -0800 | [diff] [blame] | 3210 | net->ipv6.ip6_rt_gc_expire++; |
Li RongQing | 1495664 | 2014-05-19 17:30:28 +0800 | [diff] [blame] | 3211 | fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net, true); |
Eric Dumazet | fc66f95 | 2010-10-08 06:37:34 +0000 | [diff] [blame] | 3212 | entries = dst_entries_get_slow(ops); |
| 3213 | if (entries < ops->gc_thresh) |
Daniel Lezcano | 7019b78 | 2008-03-04 13:50:14 -0800 | [diff] [blame] | 3214 | net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3215 | out: |
Daniel Lezcano | 7019b78 | 2008-03-04 13:50:14 -0800 | [diff] [blame] | 3216 | net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity; |
Eric Dumazet | fc66f95 | 2010-10-08 06:37:34 +0000 | [diff] [blame] | 3217 | return entries > rt_max_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3218 | } |
| 3219 | |
David Ahern | b2c709c | 2019-06-24 13:44:51 -0700 | [diff] [blame] | 3220 | static int ip6_nh_lookup_table(struct net *net, struct fib6_config *cfg, |
| 3221 | const struct in6_addr *gw_addr, u32 tbid, |
| 3222 | int flags, struct fib6_result *res) |
David Ahern | 8c14586 | 2016-04-24 21:26:04 -0700 | [diff] [blame] | 3223 | { |
| 3224 | struct flowi6 fl6 = { |
| 3225 | .flowi6_oif = cfg->fc_ifindex, |
| 3226 | .daddr = *gw_addr, |
| 3227 | .saddr = cfg->fc_prefsrc, |
| 3228 | }; |
| 3229 | struct fib6_table *table; |
David Ahern | b2c709c | 2019-06-24 13:44:51 -0700 | [diff] [blame] | 3230 | int err; |
David Ahern | 8c14586 | 2016-04-24 21:26:04 -0700 | [diff] [blame] | 3231 | |
David Ahern | f4797b3 | 2018-01-25 16:55:08 -0800 | [diff] [blame] | 3232 | table = fib6_get_table(net, tbid); |
David Ahern | 8c14586 | 2016-04-24 21:26:04 -0700 | [diff] [blame] | 3233 | if (!table) |
David Ahern | b2c709c | 2019-06-24 13:44:51 -0700 | [diff] [blame] | 3234 | return -EINVAL; |
David Ahern | 8c14586 | 2016-04-24 21:26:04 -0700 | [diff] [blame] | 3235 | |
| 3236 | if (!ipv6_addr_any(&cfg->fc_prefsrc)) |
| 3237 | flags |= RT6_LOOKUP_F_HAS_SADDR; |
| 3238 | |
David Ahern | f4797b3 | 2018-01-25 16:55:08 -0800 | [diff] [blame] | 3239 | flags |= RT6_LOOKUP_F_IGNORE_LINKSTATE; |
David Ahern | 8c14586 | 2016-04-24 21:26:04 -0700 | [diff] [blame] | 3240 | |
David Ahern | b2c709c | 2019-06-24 13:44:51 -0700 | [diff] [blame] | 3241 | err = fib6_table_lookup(net, table, cfg->fc_ifindex, &fl6, res, flags); |
| 3242 | if (!err && res->f6i != net->ipv6.fib6_null_entry) |
| 3243 | fib6_select_path(net, res, &fl6, cfg->fc_ifindex, |
| 3244 | cfg->fc_ifindex != 0, NULL, flags); |
David Ahern | 8c14586 | 2016-04-24 21:26:04 -0700 | [diff] [blame] | 3245 | |
David Ahern | b2c709c | 2019-06-24 13:44:51 -0700 | [diff] [blame] | 3246 | return err; |
David Ahern | 8c14586 | 2016-04-24 21:26:04 -0700 | [diff] [blame] | 3247 | } |
| 3248 | |
David Ahern | fc1e64e | 2018-01-25 16:55:09 -0800 | [diff] [blame] | 3249 | static int ip6_route_check_nh_onlink(struct net *net, |
| 3250 | struct fib6_config *cfg, |
David Ahern | 9fbb704 | 2018-03-13 08:29:36 -0700 | [diff] [blame] | 3251 | const struct net_device *dev, |
David Ahern | fc1e64e | 2018-01-25 16:55:09 -0800 | [diff] [blame] | 3252 | struct netlink_ext_ack *extack) |
| 3253 | { |
David Ahern | b2c709c | 2019-06-24 13:44:51 -0700 | [diff] [blame] | 3254 | u32 tbid = l3mdev_fib_table_rcu(dev) ? : RT_TABLE_MAIN; |
David Ahern | fc1e64e | 2018-01-25 16:55:09 -0800 | [diff] [blame] | 3255 | const struct in6_addr *gw_addr = &cfg->fc_gateway; |
David Ahern | b2c709c | 2019-06-24 13:44:51 -0700 | [diff] [blame] | 3256 | struct fib6_result res = {}; |
David Ahern | fc1e64e | 2018-01-25 16:55:09 -0800 | [diff] [blame] | 3257 | int err; |
| 3258 | |
David Ahern | b2c709c | 2019-06-24 13:44:51 -0700 | [diff] [blame] | 3259 | err = ip6_nh_lookup_table(net, cfg, gw_addr, tbid, 0, &res); |
| 3260 | if (!err && !(res.fib6_flags & RTF_REJECT) && |
| 3261 | /* ignore match if it is the default route */ |
| 3262 | !ipv6_addr_any(&res.f6i->fib6_dst.addr) && |
| 3263 | (res.fib6_type != RTN_UNICAST || dev != res.nh->fib_nh_dev)) { |
| 3264 | NL_SET_ERR_MSG(extack, |
| 3265 | "Nexthop has invalid gateway or device mismatch"); |
| 3266 | err = -EINVAL; |
David Ahern | fc1e64e | 2018-01-25 16:55:09 -0800 | [diff] [blame] | 3267 | } |
| 3268 | |
| 3269 | return err; |
| 3270 | } |
| 3271 | |
David Ahern | 1edce99 | 2018-01-25 16:55:07 -0800 | [diff] [blame] | 3272 | static int ip6_route_check_nh(struct net *net, |
| 3273 | struct fib6_config *cfg, |
| 3274 | struct net_device **_dev, |
| 3275 | struct inet6_dev **idev) |
| 3276 | { |
| 3277 | const struct in6_addr *gw_addr = &cfg->fc_gateway; |
| 3278 | struct net_device *dev = _dev ? *_dev : NULL; |
David Ahern | b2c709c | 2019-06-24 13:44:51 -0700 | [diff] [blame] | 3279 | int flags = RT6_LOOKUP_F_IFACE; |
| 3280 | struct fib6_result res = {}; |
David Ahern | 1edce99 | 2018-01-25 16:55:07 -0800 | [diff] [blame] | 3281 | int err = -EHOSTUNREACH; |
| 3282 | |
| 3283 | if (cfg->fc_table) { |
David Ahern | b2c709c | 2019-06-24 13:44:51 -0700 | [diff] [blame] | 3284 | err = ip6_nh_lookup_table(net, cfg, gw_addr, |
| 3285 | cfg->fc_table, flags, &res); |
| 3286 | /* gw_addr can not require a gateway or resolve to a reject |
| 3287 | * route. If a device is given, it must match the result. |
| 3288 | */ |
| 3289 | if (err || res.fib6_flags & RTF_REJECT || |
| 3290 | res.nh->fib_nh_gw_family || |
| 3291 | (dev && dev != res.nh->fib_nh_dev)) |
| 3292 | err = -EHOSTUNREACH; |
David Ahern | 1edce99 | 2018-01-25 16:55:07 -0800 | [diff] [blame] | 3293 | } |
| 3294 | |
David Ahern | b2c709c | 2019-06-24 13:44:51 -0700 | [diff] [blame] | 3295 | if (err < 0) { |
| 3296 | struct flowi6 fl6 = { |
| 3297 | .flowi6_oif = cfg->fc_ifindex, |
| 3298 | .daddr = *gw_addr, |
| 3299 | }; |
David Ahern | 1edce99 | 2018-01-25 16:55:07 -0800 | [diff] [blame] | 3300 | |
David Ahern | b2c709c | 2019-06-24 13:44:51 -0700 | [diff] [blame] | 3301 | err = fib6_lookup(net, cfg->fc_ifindex, &fl6, &res, flags); |
| 3302 | if (err || res.fib6_flags & RTF_REJECT || |
| 3303 | res.nh->fib_nh_gw_family) |
| 3304 | err = -EHOSTUNREACH; |
David Ahern | 1edce99 | 2018-01-25 16:55:07 -0800 | [diff] [blame] | 3305 | |
David Ahern | b2c709c | 2019-06-24 13:44:51 -0700 | [diff] [blame] | 3306 | if (err) |
| 3307 | return err; |
| 3308 | |
| 3309 | fib6_select_path(net, &res, &fl6, cfg->fc_ifindex, |
| 3310 | cfg->fc_ifindex != 0, NULL, flags); |
| 3311 | } |
| 3312 | |
| 3313 | err = 0; |
David Ahern | 1edce99 | 2018-01-25 16:55:07 -0800 | [diff] [blame] | 3314 | if (dev) { |
David Ahern | b2c709c | 2019-06-24 13:44:51 -0700 | [diff] [blame] | 3315 | if (dev != res.nh->fib_nh_dev) |
| 3316 | err = -EHOSTUNREACH; |
David Ahern | 1edce99 | 2018-01-25 16:55:07 -0800 | [diff] [blame] | 3317 | } else { |
David Ahern | b2c709c | 2019-06-24 13:44:51 -0700 | [diff] [blame] | 3318 | *_dev = dev = res.nh->fib_nh_dev; |
David Ahern | 1edce99 | 2018-01-25 16:55:07 -0800 | [diff] [blame] | 3319 | dev_hold(dev); |
David Ahern | b2c709c | 2019-06-24 13:44:51 -0700 | [diff] [blame] | 3320 | *idev = in6_dev_get(dev); |
David Ahern | 1edce99 | 2018-01-25 16:55:07 -0800 | [diff] [blame] | 3321 | } |
| 3322 | |
David Ahern | 1edce99 | 2018-01-25 16:55:07 -0800 | [diff] [blame] | 3323 | return err; |
| 3324 | } |
| 3325 | |
David Ahern | 9fbb704 | 2018-03-13 08:29:36 -0700 | [diff] [blame] | 3326 | static int ip6_validate_gw(struct net *net, struct fib6_config *cfg, |
| 3327 | struct net_device **_dev, struct inet6_dev **idev, |
| 3328 | struct netlink_ext_ack *extack) |
| 3329 | { |
| 3330 | const struct in6_addr *gw_addr = &cfg->fc_gateway; |
| 3331 | int gwa_type = ipv6_addr_type(gw_addr); |
David Ahern | 232378e | 2018-03-13 08:29:37 -0700 | [diff] [blame] | 3332 | bool skip_dev = gwa_type & IPV6_ADDR_LINKLOCAL ? false : true; |
David Ahern | 9fbb704 | 2018-03-13 08:29:36 -0700 | [diff] [blame] | 3333 | const struct net_device *dev = *_dev; |
David Ahern | 232378e | 2018-03-13 08:29:37 -0700 | [diff] [blame] | 3334 | bool need_addr_check = !dev; |
David Ahern | 9fbb704 | 2018-03-13 08:29:36 -0700 | [diff] [blame] | 3335 | int err = -EINVAL; |
| 3336 | |
| 3337 | /* if gw_addr is local we will fail to detect this in case |
| 3338 | * address is still TENTATIVE (DAD in progress). rt6_lookup() |
| 3339 | * will return already-added prefix route via interface that |
| 3340 | * prefix route was assigned to, which might be non-loopback. |
| 3341 | */ |
David Ahern | 232378e | 2018-03-13 08:29:37 -0700 | [diff] [blame] | 3342 | if (dev && |
| 3343 | ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) { |
| 3344 | NL_SET_ERR_MSG(extack, "Gateway can not be a local address"); |
David Ahern | 9fbb704 | 2018-03-13 08:29:36 -0700 | [diff] [blame] | 3345 | goto out; |
| 3346 | } |
| 3347 | |
| 3348 | if (gwa_type != (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_UNICAST)) { |
| 3349 | /* IPv6 strictly inhibits using not link-local |
| 3350 | * addresses as nexthop address. |
| 3351 | * Otherwise, router will not able to send redirects. |
| 3352 | * It is very good, but in some (rare!) circumstances |
| 3353 | * (SIT, PtP, NBMA NOARP links) it is handy to allow |
| 3354 | * some exceptions. --ANK |
| 3355 | * We allow IPv4-mapped nexthops to support RFC4798-type |
| 3356 | * addressing |
| 3357 | */ |
| 3358 | if (!(gwa_type & (IPV6_ADDR_UNICAST | IPV6_ADDR_MAPPED))) { |
| 3359 | NL_SET_ERR_MSG(extack, "Invalid gateway address"); |
| 3360 | goto out; |
| 3361 | } |
| 3362 | |
David Ahern | b2c709c | 2019-06-24 13:44:51 -0700 | [diff] [blame] | 3363 | rcu_read_lock(); |
| 3364 | |
David Ahern | 9fbb704 | 2018-03-13 08:29:36 -0700 | [diff] [blame] | 3365 | if (cfg->fc_flags & RTNH_F_ONLINK) |
| 3366 | err = ip6_route_check_nh_onlink(net, cfg, dev, extack); |
| 3367 | else |
| 3368 | err = ip6_route_check_nh(net, cfg, _dev, idev); |
| 3369 | |
David Ahern | b2c709c | 2019-06-24 13:44:51 -0700 | [diff] [blame] | 3370 | rcu_read_unlock(); |
| 3371 | |
David Ahern | 9fbb704 | 2018-03-13 08:29:36 -0700 | [diff] [blame] | 3372 | if (err) |
| 3373 | goto out; |
| 3374 | } |
| 3375 | |
| 3376 | /* reload in case device was changed */ |
| 3377 | dev = *_dev; |
| 3378 | |
| 3379 | err = -EINVAL; |
| 3380 | if (!dev) { |
| 3381 | NL_SET_ERR_MSG(extack, "Egress device not specified"); |
| 3382 | goto out; |
| 3383 | } else if (dev->flags & IFF_LOOPBACK) { |
| 3384 | NL_SET_ERR_MSG(extack, |
| 3385 | "Egress device can not be loopback device for this route"); |
| 3386 | goto out; |
| 3387 | } |
David Ahern | 232378e | 2018-03-13 08:29:37 -0700 | [diff] [blame] | 3388 | |
| 3389 | /* if we did not check gw_addr above, do so now that the |
| 3390 | * egress device has been resolved. |
| 3391 | */ |
| 3392 | if (need_addr_check && |
| 3393 | ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) { |
| 3394 | NL_SET_ERR_MSG(extack, "Gateway can not be a local address"); |
| 3395 | goto out; |
| 3396 | } |
| 3397 | |
David Ahern | 9fbb704 | 2018-03-13 08:29:36 -0700 | [diff] [blame] | 3398 | err = 0; |
| 3399 | out: |
| 3400 | return err; |
| 3401 | } |
| 3402 | |
David Ahern | 83c44251 | 2019-03-27 20:53:50 -0700 | [diff] [blame] | 3403 | static bool fib6_is_reject(u32 flags, struct net_device *dev, int addr_type) |
| 3404 | { |
| 3405 | if ((flags & RTF_REJECT) || |
| 3406 | (dev && (dev->flags & IFF_LOOPBACK) && |
| 3407 | !(addr_type & IPV6_ADDR_LOOPBACK) && |
David Ahern | aea23c32 | 2020-07-07 07:39:24 -0600 | [diff] [blame] | 3408 | !(flags & (RTF_ANYCAST | RTF_LOCAL)))) |
David Ahern | 83c44251 | 2019-03-27 20:53:50 -0700 | [diff] [blame] | 3409 | return true; |
| 3410 | |
| 3411 | return false; |
| 3412 | } |
| 3413 | |
| 3414 | int fib6_nh_init(struct net *net, struct fib6_nh *fib6_nh, |
| 3415 | struct fib6_config *cfg, gfp_t gfp_flags, |
| 3416 | struct netlink_ext_ack *extack) |
| 3417 | { |
| 3418 | struct net_device *dev = NULL; |
| 3419 | struct inet6_dev *idev = NULL; |
| 3420 | int addr_type; |
| 3421 | int err; |
| 3422 | |
David Ahern | f174173 | 2019-03-27 20:53:57 -0700 | [diff] [blame] | 3423 | fib6_nh->fib_nh_family = AF_INET6; |
Eric Dumazet | 1bef4c2 | 2019-11-07 09:26:19 -0800 | [diff] [blame] | 3424 | #ifdef CONFIG_IPV6_ROUTER_PREF |
| 3425 | fib6_nh->last_probe = jiffies; |
| 3426 | #endif |
Roopa Prabhu | 38428d6 | 2020-05-21 22:26:13 -0700 | [diff] [blame] | 3427 | if (cfg->fc_is_fdb) { |
| 3428 | fib6_nh->fib_nh_gw6 = cfg->fc_gateway; |
| 3429 | fib6_nh->fib_nh_gw_family = AF_INET6; |
| 3430 | return 0; |
| 3431 | } |
David Ahern | f174173 | 2019-03-27 20:53:57 -0700 | [diff] [blame] | 3432 | |
David Ahern | 83c44251 | 2019-03-27 20:53:50 -0700 | [diff] [blame] | 3433 | err = -ENODEV; |
| 3434 | if (cfg->fc_ifindex) { |
| 3435 | dev = dev_get_by_index(net, cfg->fc_ifindex); |
| 3436 | if (!dev) |
| 3437 | goto out; |
| 3438 | idev = in6_dev_get(dev); |
| 3439 | if (!idev) |
| 3440 | goto out; |
| 3441 | } |
| 3442 | |
| 3443 | if (cfg->fc_flags & RTNH_F_ONLINK) { |
| 3444 | if (!dev) { |
| 3445 | NL_SET_ERR_MSG(extack, |
| 3446 | "Nexthop device required for onlink"); |
| 3447 | goto out; |
| 3448 | } |
| 3449 | |
| 3450 | if (!(dev->flags & IFF_UP)) { |
| 3451 | NL_SET_ERR_MSG(extack, "Nexthop device is not up"); |
| 3452 | err = -ENETDOWN; |
| 3453 | goto out; |
| 3454 | } |
| 3455 | |
David Ahern | ad1601a | 2019-03-27 20:53:56 -0700 | [diff] [blame] | 3456 | fib6_nh->fib_nh_flags |= RTNH_F_ONLINK; |
David Ahern | 83c44251 | 2019-03-27 20:53:50 -0700 | [diff] [blame] | 3457 | } |
| 3458 | |
David Ahern | ad1601a | 2019-03-27 20:53:56 -0700 | [diff] [blame] | 3459 | fib6_nh->fib_nh_weight = 1; |
David Ahern | 83c44251 | 2019-03-27 20:53:50 -0700 | [diff] [blame] | 3460 | |
| 3461 | /* We cannot add true routes via loopback here, |
| 3462 | * they would result in kernel looping; promote them to reject routes |
| 3463 | */ |
| 3464 | addr_type = ipv6_addr_type(&cfg->fc_dst); |
| 3465 | if (fib6_is_reject(cfg->fc_flags, dev, addr_type)) { |
| 3466 | /* hold loopback dev/idev if we haven't done so. */ |
| 3467 | if (dev != net->loopback_dev) { |
| 3468 | if (dev) { |
| 3469 | dev_put(dev); |
| 3470 | in6_dev_put(idev); |
| 3471 | } |
| 3472 | dev = net->loopback_dev; |
| 3473 | dev_hold(dev); |
| 3474 | idev = in6_dev_get(dev); |
| 3475 | if (!idev) { |
| 3476 | err = -ENODEV; |
| 3477 | goto out; |
| 3478 | } |
| 3479 | } |
David Ahern | 7dd7316 | 2019-06-03 18:37:03 -0700 | [diff] [blame] | 3480 | goto pcpu_alloc; |
David Ahern | 83c44251 | 2019-03-27 20:53:50 -0700 | [diff] [blame] | 3481 | } |
| 3482 | |
| 3483 | if (cfg->fc_flags & RTF_GATEWAY) { |
| 3484 | err = ip6_validate_gw(net, cfg, &dev, &idev, extack); |
| 3485 | if (err) |
| 3486 | goto out; |
| 3487 | |
David Ahern | ad1601a | 2019-03-27 20:53:56 -0700 | [diff] [blame] | 3488 | fib6_nh->fib_nh_gw6 = cfg->fc_gateway; |
David Ahern | bdf0046 | 2019-04-05 16:30:26 -0700 | [diff] [blame] | 3489 | fib6_nh->fib_nh_gw_family = AF_INET6; |
David Ahern | 83c44251 | 2019-03-27 20:53:50 -0700 | [diff] [blame] | 3490 | } |
| 3491 | |
| 3492 | err = -ENODEV; |
| 3493 | if (!dev) |
| 3494 | goto out; |
| 3495 | |
| 3496 | if (idev->cnf.disable_ipv6) { |
| 3497 | NL_SET_ERR_MSG(extack, "IPv6 is disabled on nexthop device"); |
| 3498 | err = -EACCES; |
| 3499 | goto out; |
| 3500 | } |
| 3501 | |
| 3502 | if (!(dev->flags & IFF_UP) && !cfg->fc_ignore_dev_down) { |
| 3503 | NL_SET_ERR_MSG(extack, "Nexthop device is not up"); |
| 3504 | err = -ENETDOWN; |
| 3505 | goto out; |
| 3506 | } |
| 3507 | |
| 3508 | if (!(cfg->fc_flags & (RTF_LOCAL | RTF_ANYCAST)) && |
| 3509 | !netif_carrier_ok(dev)) |
David Ahern | ad1601a | 2019-03-27 20:53:56 -0700 | [diff] [blame] | 3510 | fib6_nh->fib_nh_flags |= RTNH_F_LINKDOWN; |
David Ahern | 83c44251 | 2019-03-27 20:53:50 -0700 | [diff] [blame] | 3511 | |
Alexander Aring | faee676 | 2020-03-27 18:00:21 -0400 | [diff] [blame] | 3512 | err = fib_nh_common_init(net, &fib6_nh->nh_common, cfg->fc_encap, |
David Ahern | 7dd7316 | 2019-06-03 18:37:03 -0700 | [diff] [blame] | 3513 | cfg->fc_encap_type, cfg, gfp_flags, extack); |
| 3514 | if (err) |
| 3515 | goto out; |
| 3516 | |
| 3517 | pcpu_alloc: |
David Ahern | f40b6ae | 2019-05-22 20:27:55 -0700 | [diff] [blame] | 3518 | fib6_nh->rt6i_pcpu = alloc_percpu_gfp(struct rt6_info *, gfp_flags); |
| 3519 | if (!fib6_nh->rt6i_pcpu) { |
| 3520 | err = -ENOMEM; |
| 3521 | goto out; |
| 3522 | } |
| 3523 | |
David Ahern | ad1601a | 2019-03-27 20:53:56 -0700 | [diff] [blame] | 3524 | fib6_nh->fib_nh_dev = dev; |
David Ahern | f174173 | 2019-03-27 20:53:57 -0700 | [diff] [blame] | 3525 | fib6_nh->fib_nh_oif = dev->ifindex; |
David Ahern | 83c44251 | 2019-03-27 20:53:50 -0700 | [diff] [blame] | 3526 | err = 0; |
| 3527 | out: |
| 3528 | if (idev) |
| 3529 | in6_dev_put(idev); |
| 3530 | |
| 3531 | if (err) { |
David Ahern | ad1601a | 2019-03-27 20:53:56 -0700 | [diff] [blame] | 3532 | lwtstate_put(fib6_nh->fib_nh_lws); |
| 3533 | fib6_nh->fib_nh_lws = NULL; |
David Ahern | 83c44251 | 2019-03-27 20:53:50 -0700 | [diff] [blame] | 3534 | if (dev) |
| 3535 | dev_put(dev); |
| 3536 | } |
| 3537 | |
| 3538 | return err; |
| 3539 | } |
| 3540 | |
David Ahern | dac7d0f | 2019-03-27 20:53:51 -0700 | [diff] [blame] | 3541 | void fib6_nh_release(struct fib6_nh *fib6_nh) |
| 3542 | { |
David Ahern | cc5c073 | 2019-05-22 20:27:58 -0700 | [diff] [blame] | 3543 | struct rt6_exception_bucket *bucket; |
| 3544 | |
| 3545 | rcu_read_lock(); |
| 3546 | |
| 3547 | fib6_nh_flush_exceptions(fib6_nh, NULL); |
| 3548 | bucket = fib6_nh_get_excptn_bucket(fib6_nh, NULL); |
| 3549 | if (bucket) { |
| 3550 | rcu_assign_pointer(fib6_nh->rt6i_exception_bucket, NULL); |
| 3551 | kfree(bucket); |
| 3552 | } |
| 3553 | |
| 3554 | rcu_read_unlock(); |
| 3555 | |
David Ahern | f40b6ae | 2019-05-22 20:27:55 -0700 | [diff] [blame] | 3556 | if (fib6_nh->rt6i_pcpu) { |
| 3557 | int cpu; |
| 3558 | |
| 3559 | for_each_possible_cpu(cpu) { |
| 3560 | struct rt6_info **ppcpu_rt; |
| 3561 | struct rt6_info *pcpu_rt; |
| 3562 | |
| 3563 | ppcpu_rt = per_cpu_ptr(fib6_nh->rt6i_pcpu, cpu); |
| 3564 | pcpu_rt = *ppcpu_rt; |
| 3565 | if (pcpu_rt) { |
| 3566 | dst_dev_put(&pcpu_rt->dst); |
| 3567 | dst_release(&pcpu_rt->dst); |
| 3568 | *ppcpu_rt = NULL; |
| 3569 | } |
| 3570 | } |
| 3571 | |
| 3572 | free_percpu(fib6_nh->rt6i_pcpu); |
| 3573 | } |
| 3574 | |
David Ahern | 979e276 | 2019-03-27 20:53:58 -0700 | [diff] [blame] | 3575 | fib_nh_common_release(&fib6_nh->nh_common); |
David Ahern | dac7d0f | 2019-03-27 20:53:51 -0700 | [diff] [blame] | 3576 | } |
| 3577 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 3578 | static struct fib6_info *ip6_route_info_create(struct fib6_config *cfg, |
David Ahern | acb54e3 | 2018-04-17 17:33:22 -0700 | [diff] [blame] | 3579 | gfp_t gfp_flags, |
David Ahern | 333c430 | 2017-05-21 10:12:04 -0600 | [diff] [blame] | 3580 | struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3581 | { |
Daniel Lezcano | 5578689 | 2008-03-04 13:47:47 -0800 | [diff] [blame] | 3582 | struct net *net = cfg->fc_nlinfo.nl_net; |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 3583 | struct fib6_info *rt = NULL; |
David Ahern | f88d8ea | 2019-06-03 20:19:52 -0700 | [diff] [blame] | 3584 | struct nexthop *nh = NULL; |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 3585 | struct fib6_table *table; |
David Ahern | f88d8ea | 2019-06-03 20:19:52 -0700 | [diff] [blame] | 3586 | struct fib6_nh *fib6_nh; |
Roopa Prabhu | 8c5b83f | 2015-10-10 08:26:36 -0700 | [diff] [blame] | 3587 | int err = -EINVAL; |
David Ahern | 83c44251 | 2019-03-27 20:53:50 -0700 | [diff] [blame] | 3588 | int addr_type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3589 | |
David Ahern | 557c44b | 2017-04-19 14:19:43 -0700 | [diff] [blame] | 3590 | /* RTF_PCPU is an internal flag; can not be set by userspace */ |
David Ahern | d5d531c | 2017-05-21 10:12:05 -0600 | [diff] [blame] | 3591 | if (cfg->fc_flags & RTF_PCPU) { |
| 3592 | NL_SET_ERR_MSG(extack, "Userspace can not set RTF_PCPU"); |
David Ahern | 557c44b | 2017-04-19 14:19:43 -0700 | [diff] [blame] | 3593 | goto out; |
David Ahern | d5d531c | 2017-05-21 10:12:05 -0600 | [diff] [blame] | 3594 | } |
David Ahern | 557c44b | 2017-04-19 14:19:43 -0700 | [diff] [blame] | 3595 | |
Wei Wang | 2ea2352 | 2017-10-27 17:30:12 -0700 | [diff] [blame] | 3596 | /* RTF_CACHE is an internal flag; can not be set by userspace */ |
| 3597 | if (cfg->fc_flags & RTF_CACHE) { |
| 3598 | NL_SET_ERR_MSG(extack, "Userspace can not set RTF_CACHE"); |
| 3599 | goto out; |
| 3600 | } |
| 3601 | |
David Ahern | e8478e8 | 2018-04-17 17:33:13 -0700 | [diff] [blame] | 3602 | if (cfg->fc_type > RTN_MAX) { |
| 3603 | NL_SET_ERR_MSG(extack, "Invalid route type"); |
| 3604 | goto out; |
| 3605 | } |
| 3606 | |
David Ahern | d5d531c | 2017-05-21 10:12:05 -0600 | [diff] [blame] | 3607 | if (cfg->fc_dst_len > 128) { |
| 3608 | NL_SET_ERR_MSG(extack, "Invalid prefix length"); |
Roopa Prabhu | 8c5b83f | 2015-10-10 08:26:36 -0700 | [diff] [blame] | 3609 | goto out; |
David Ahern | d5d531c | 2017-05-21 10:12:05 -0600 | [diff] [blame] | 3610 | } |
| 3611 | if (cfg->fc_src_len > 128) { |
| 3612 | NL_SET_ERR_MSG(extack, "Invalid source address length"); |
| 3613 | goto out; |
| 3614 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3615 | #ifndef CONFIG_IPV6_SUBTREES |
David Ahern | d5d531c | 2017-05-21 10:12:05 -0600 | [diff] [blame] | 3616 | if (cfg->fc_src_len) { |
| 3617 | NL_SET_ERR_MSG(extack, |
| 3618 | "Specifying source address requires IPV6_SUBTREES to be enabled"); |
Roopa Prabhu | 8c5b83f | 2015-10-10 08:26:36 -0700 | [diff] [blame] | 3619 | goto out; |
David Ahern | d5d531c | 2017-05-21 10:12:05 -0600 | [diff] [blame] | 3620 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3621 | #endif |
David Ahern | 5b98324 | 2019-06-08 14:53:34 -0700 | [diff] [blame] | 3622 | if (cfg->fc_nh_id) { |
| 3623 | nh = nexthop_find_by_id(net, cfg->fc_nh_id); |
| 3624 | if (!nh) { |
| 3625 | NL_SET_ERR_MSG(extack, "Nexthop id does not exist"); |
| 3626 | goto out; |
| 3627 | } |
| 3628 | err = fib6_check_nexthop(nh, cfg, extack); |
| 3629 | if (err) |
| 3630 | goto out; |
| 3631 | } |
David Ahern | fc1e64e | 2018-01-25 16:55:09 -0800 | [diff] [blame] | 3632 | |
Matti Vaittinen | d71314b | 2011-11-14 00:14:49 +0000 | [diff] [blame] | 3633 | err = -ENOBUFS; |
David S. Miller | 3830847 | 2011-12-03 18:02:47 -0500 | [diff] [blame] | 3634 | if (cfg->fc_nlinfo.nlh && |
| 3635 | !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) { |
Matti Vaittinen | d71314b | 2011-11-14 00:14:49 +0000 | [diff] [blame] | 3636 | table = fib6_get_table(net, cfg->fc_table); |
David S. Miller | 3830847 | 2011-12-03 18:02:47 -0500 | [diff] [blame] | 3637 | if (!table) { |
Joe Perches | f321383 | 2012-05-15 14:11:53 +0000 | [diff] [blame] | 3638 | pr_warn("NLM_F_CREATE should be specified when creating new route\n"); |
Matti Vaittinen | d71314b | 2011-11-14 00:14:49 +0000 | [diff] [blame] | 3639 | table = fib6_new_table(net, cfg->fc_table); |
| 3640 | } |
| 3641 | } else { |
| 3642 | table = fib6_new_table(net, cfg->fc_table); |
| 3643 | } |
David S. Miller | 3830847 | 2011-12-03 18:02:47 -0500 | [diff] [blame] | 3644 | |
| 3645 | if (!table) |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 3646 | goto out; |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 3647 | |
David Ahern | 93531c6 | 2018-04-17 17:33:25 -0700 | [diff] [blame] | 3648 | err = -ENOMEM; |
David Ahern | f88d8ea | 2019-06-03 20:19:52 -0700 | [diff] [blame] | 3649 | rt = fib6_info_alloc(gfp_flags, !nh); |
David Ahern | 93531c6 | 2018-04-17 17:33:25 -0700 | [diff] [blame] | 3650 | if (!rt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3651 | goto out; |
David Ahern | 93531c6 | 2018-04-17 17:33:25 -0700 | [diff] [blame] | 3652 | |
David Ahern | d7e774f | 2018-11-06 12:51:15 -0800 | [diff] [blame] | 3653 | rt->fib6_metrics = ip_fib_metrics_init(net, cfg->fc_mx, cfg->fc_mx_len, |
| 3654 | extack); |
David Ahern | 767a221 | 2018-10-04 20:07:51 -0700 | [diff] [blame] | 3655 | if (IS_ERR(rt->fib6_metrics)) { |
| 3656 | err = PTR_ERR(rt->fib6_metrics); |
Eric Dumazet | fda21d4 | 2018-10-05 09:17:50 -0700 | [diff] [blame] | 3657 | /* Do not leave garbage there. */ |
| 3658 | rt->fib6_metrics = (struct dst_metrics *)&dst_default_metrics; |
David Ahern | 767a221 | 2018-10-04 20:07:51 -0700 | [diff] [blame] | 3659 | goto out; |
| 3660 | } |
| 3661 | |
David Ahern | 93531c6 | 2018-04-17 17:33:25 -0700 | [diff] [blame] | 3662 | if (cfg->fc_flags & RTF_ADDRCONF) |
| 3663 | rt->dst_nocount = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3664 | |
Gao feng | 1716a96 | 2012-04-06 00:13:10 +0000 | [diff] [blame] | 3665 | if (cfg->fc_flags & RTF_EXPIRES) |
David Ahern | 1489568 | 2018-04-17 17:33:17 -0700 | [diff] [blame] | 3666 | fib6_set_expires(rt, jiffies + |
Gao feng | 1716a96 | 2012-04-06 00:13:10 +0000 | [diff] [blame] | 3667 | clock_t_to_jiffies(cfg->fc_expires)); |
| 3668 | else |
David Ahern | 1489568 | 2018-04-17 17:33:17 -0700 | [diff] [blame] | 3669 | fib6_clean_expires(rt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3670 | |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 3671 | if (cfg->fc_protocol == RTPROT_UNSPEC) |
| 3672 | cfg->fc_protocol = RTPROT_BOOT; |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 3673 | rt->fib6_protocol = cfg->fc_protocol; |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 3674 | |
David Ahern | 83c44251 | 2019-03-27 20:53:50 -0700 | [diff] [blame] | 3675 | rt->fib6_table = table; |
| 3676 | rt->fib6_metric = cfg->fc_metric; |
David Ahern | c7036d9 | 2019-06-19 10:50:24 -0700 | [diff] [blame] | 3677 | rt->fib6_type = cfg->fc_type ? : RTN_UNICAST; |
David Ahern | 2b2450c | 2019-03-27 20:53:52 -0700 | [diff] [blame] | 3678 | rt->fib6_flags = cfg->fc_flags & ~RTF_GATEWAY; |
Roopa Prabhu | 19e42e4 | 2015-07-21 10:43:48 +0200 | [diff] [blame] | 3679 | |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 3680 | ipv6_addr_prefix(&rt->fib6_dst.addr, &cfg->fc_dst, cfg->fc_dst_len); |
| 3681 | rt->fib6_dst.plen = cfg->fc_dst_len; |
Michal Kubeček | e5fd387 | 2014-03-27 13:04:08 +0100 | [diff] [blame] | 3682 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3683 | #ifdef CONFIG_IPV6_SUBTREES |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 3684 | ipv6_addr_prefix(&rt->fib6_src.addr, &cfg->fc_src, cfg->fc_src_len); |
| 3685 | rt->fib6_src.plen = cfg->fc_src_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3686 | #endif |
David Ahern | f88d8ea | 2019-06-03 20:19:52 -0700 | [diff] [blame] | 3687 | if (nh) { |
| 3688 | if (!nexthop_get(nh)) { |
| 3689 | NL_SET_ERR_MSG(extack, "Nexthop has been deleted"); |
| 3690 | goto out; |
| 3691 | } |
| 3692 | if (rt->fib6_src.plen) { |
Colin Ian King | 4daa95a | 2019-06-06 09:40:39 +0100 | [diff] [blame] | 3693 | NL_SET_ERR_MSG(extack, "Nexthops can not be used with source routing"); |
David Ahern | f88d8ea | 2019-06-03 20:19:52 -0700 | [diff] [blame] | 3694 | goto out; |
| 3695 | } |
| 3696 | rt->nh = nh; |
| 3697 | fib6_nh = nexthop_fib6_nh(rt->nh); |
| 3698 | } else { |
| 3699 | err = fib6_nh_init(net, rt->fib6_nh, cfg, gfp_flags, extack); |
| 3700 | if (err) |
| 3701 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3702 | |
David Ahern | f88d8ea | 2019-06-03 20:19:52 -0700 | [diff] [blame] | 3703 | fib6_nh = rt->fib6_nh; |
| 3704 | |
| 3705 | /* We cannot add true routes via loopback here, they would |
| 3706 | * result in kernel looping; promote them to reject routes |
| 3707 | */ |
| 3708 | addr_type = ipv6_addr_type(&cfg->fc_dst); |
| 3709 | if (fib6_is_reject(cfg->fc_flags, rt->fib6_nh->fib_nh_dev, |
| 3710 | addr_type)) |
| 3711 | rt->fib6_flags = RTF_REJECT | RTF_NONEXTHOP; |
| 3712 | } |
David Ahern | 955ec4c | 2018-01-24 19:45:29 -0800 | [diff] [blame] | 3713 | |
Daniel Walter | c3968a8 | 2011-04-13 21:10:57 +0000 | [diff] [blame] | 3714 | if (!ipv6_addr_any(&cfg->fc_prefsrc)) { |
David Ahern | f88d8ea | 2019-06-03 20:19:52 -0700 | [diff] [blame] | 3715 | struct net_device *dev = fib6_nh->fib_nh_dev; |
David Ahern | 83c44251 | 2019-03-27 20:53:50 -0700 | [diff] [blame] | 3716 | |
Daniel Walter | c3968a8 | 2011-04-13 21:10:57 +0000 | [diff] [blame] | 3717 | if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) { |
David Ahern | d5d531c | 2017-05-21 10:12:05 -0600 | [diff] [blame] | 3718 | NL_SET_ERR_MSG(extack, "Invalid source address"); |
Daniel Walter | c3968a8 | 2011-04-13 21:10:57 +0000 | [diff] [blame] | 3719 | err = -EINVAL; |
| 3720 | goto out; |
| 3721 | } |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 3722 | rt->fib6_prefsrc.addr = cfg->fc_prefsrc; |
| 3723 | rt->fib6_prefsrc.plen = 128; |
Daniel Walter | c3968a8 | 2011-04-13 21:10:57 +0000 | [diff] [blame] | 3724 | } else |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 3725 | rt->fib6_prefsrc.plen = 0; |
Daniel Walter | c3968a8 | 2011-04-13 21:10:57 +0000 | [diff] [blame] | 3726 | |
Roopa Prabhu | 8c5b83f | 2015-10-10 08:26:36 -0700 | [diff] [blame] | 3727 | return rt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3728 | out: |
David Ahern | 93531c6 | 2018-04-17 17:33:25 -0700 | [diff] [blame] | 3729 | fib6_info_release(rt); |
Roopa Prabhu | 8c5b83f | 2015-10-10 08:26:36 -0700 | [diff] [blame] | 3730 | return ERR_PTR(err); |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 3731 | } |
| 3732 | |
David Ahern | acb54e3 | 2018-04-17 17:33:22 -0700 | [diff] [blame] | 3733 | int ip6_route_add(struct fib6_config *cfg, gfp_t gfp_flags, |
David Ahern | 333c430 | 2017-05-21 10:12:04 -0600 | [diff] [blame] | 3734 | struct netlink_ext_ack *extack) |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 3735 | { |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 3736 | struct fib6_info *rt; |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 3737 | int err; |
| 3738 | |
David Ahern | acb54e3 | 2018-04-17 17:33:22 -0700 | [diff] [blame] | 3739 | rt = ip6_route_info_create(cfg, gfp_flags, extack); |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 3740 | if (IS_ERR(rt)) |
| 3741 | return PTR_ERR(rt); |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 3742 | |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 3743 | err = __ip6_ins_rt(rt, &cfg->fc_nlinfo, extack); |
David Ahern | 93531c6 | 2018-04-17 17:33:25 -0700 | [diff] [blame] | 3744 | fib6_info_release(rt); |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 3745 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3746 | return err; |
| 3747 | } |
| 3748 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 3749 | static int __ip6_del_rt(struct fib6_info *rt, struct nl_info *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3750 | { |
David Ahern | afb1d4b5 | 2018-04-17 17:33:11 -0700 | [diff] [blame] | 3751 | struct net *net = info->nl_net; |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 3752 | struct fib6_table *table; |
David Ahern | afb1d4b5 | 2018-04-17 17:33:11 -0700 | [diff] [blame] | 3753 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3754 | |
David Ahern | 421842e | 2018-04-17 17:33:18 -0700 | [diff] [blame] | 3755 | if (rt == net->ipv6.fib6_null_entry) { |
Gao feng | 6825a26 | 2012-09-19 19:25:34 +0000 | [diff] [blame] | 3756 | err = -ENOENT; |
| 3757 | goto out; |
| 3758 | } |
Patrick McHardy | 6c813a7 | 2006-08-06 22:22:47 -0700 | [diff] [blame] | 3759 | |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 3760 | table = rt->fib6_table; |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 3761 | spin_lock_bh(&table->tb6_lock); |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 3762 | err = fib6_del(rt, info); |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 3763 | spin_unlock_bh(&table->tb6_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3764 | |
Gao feng | 6825a26 | 2012-09-19 19:25:34 +0000 | [diff] [blame] | 3765 | out: |
David Ahern | 93531c6 | 2018-04-17 17:33:25 -0700 | [diff] [blame] | 3766 | fib6_info_release(rt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3767 | return err; |
| 3768 | } |
| 3769 | |
Roopa Prabhu | 11dd74b | 2020-04-27 13:56:45 -0700 | [diff] [blame] | 3770 | int ip6_del_rt(struct net *net, struct fib6_info *rt, bool skip_notify) |
Thomas Graf | e0a1ad73 | 2006-08-22 00:00:21 -0700 | [diff] [blame] | 3771 | { |
Roopa Prabhu | 11dd74b | 2020-04-27 13:56:45 -0700 | [diff] [blame] | 3772 | struct nl_info info = { |
| 3773 | .nl_net = net, |
| 3774 | .skip_notify = skip_notify |
| 3775 | }; |
David Ahern | afb1d4b5 | 2018-04-17 17:33:11 -0700 | [diff] [blame] | 3776 | |
Denis V. Lunev | 528c4ce | 2007-12-13 09:45:12 -0800 | [diff] [blame] | 3777 | return __ip6_del_rt(rt, &info); |
Thomas Graf | e0a1ad73 | 2006-08-22 00:00:21 -0700 | [diff] [blame] | 3778 | } |
| 3779 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 3780 | static int __ip6_del_rt_siblings(struct fib6_info *rt, struct fib6_config *cfg) |
David Ahern | 0ae8133 | 2017-02-02 12:37:08 -0800 | [diff] [blame] | 3781 | { |
| 3782 | struct nl_info *info = &cfg->fc_nlinfo; |
WANG Cong | e333003 | 2017-02-27 16:07:43 -0800 | [diff] [blame] | 3783 | struct net *net = info->nl_net; |
David Ahern | 16a16cd | 2017-02-02 12:37:11 -0800 | [diff] [blame] | 3784 | struct sk_buff *skb = NULL; |
David Ahern | 0ae8133 | 2017-02-02 12:37:08 -0800 | [diff] [blame] | 3785 | struct fib6_table *table; |
WANG Cong | e333003 | 2017-02-27 16:07:43 -0800 | [diff] [blame] | 3786 | int err = -ENOENT; |
David Ahern | 0ae8133 | 2017-02-02 12:37:08 -0800 | [diff] [blame] | 3787 | |
David Ahern | 421842e | 2018-04-17 17:33:18 -0700 | [diff] [blame] | 3788 | if (rt == net->ipv6.fib6_null_entry) |
WANG Cong | e333003 | 2017-02-27 16:07:43 -0800 | [diff] [blame] | 3789 | goto out_put; |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 3790 | table = rt->fib6_table; |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 3791 | spin_lock_bh(&table->tb6_lock); |
David Ahern | 0ae8133 | 2017-02-02 12:37:08 -0800 | [diff] [blame] | 3792 | |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 3793 | if (rt->fib6_nsiblings && cfg->fc_delete_all_nh) { |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 3794 | struct fib6_info *sibling, *next_sibling; |
Ido Schimmel | 0284696 | 2019-12-23 15:28:18 +0200 | [diff] [blame] | 3795 | struct fib6_node *fn; |
David Ahern | 0ae8133 | 2017-02-02 12:37:08 -0800 | [diff] [blame] | 3796 | |
David Ahern | 16a16cd | 2017-02-02 12:37:11 -0800 | [diff] [blame] | 3797 | /* prefer to send a single notification with all hops */ |
| 3798 | skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any()); |
| 3799 | if (skb) { |
| 3800 | u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0; |
| 3801 | |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 3802 | if (rt6_fill_node(net, skb, rt, NULL, |
David Ahern | 16a16cd | 2017-02-02 12:37:11 -0800 | [diff] [blame] | 3803 | NULL, NULL, 0, RTM_DELROUTE, |
| 3804 | info->portid, seq, 0) < 0) { |
| 3805 | kfree_skb(skb); |
| 3806 | skb = NULL; |
| 3807 | } else |
| 3808 | info->skip_notify = 1; |
| 3809 | } |
| 3810 | |
Ido Schimmel | 0284696 | 2019-12-23 15:28:18 +0200 | [diff] [blame] | 3811 | /* 'rt' points to the first sibling route. If it is not the |
| 3812 | * leaf, then we do not need to send a notification. Otherwise, |
| 3813 | * we need to check if the last sibling has a next route or not |
| 3814 | * and emit a replace or delete notification, respectively. |
| 3815 | */ |
Ido Schimmel | 2881fd6 | 2019-06-18 18:12:49 +0300 | [diff] [blame] | 3816 | info->skip_notify_kernel = 1; |
Ido Schimmel | 0284696 | 2019-12-23 15:28:18 +0200 | [diff] [blame] | 3817 | fn = rcu_dereference_protected(rt->fib6_node, |
| 3818 | lockdep_is_held(&table->tb6_lock)); |
| 3819 | if (rcu_access_pointer(fn->leaf) == rt) { |
| 3820 | struct fib6_info *last_sibling, *replace_rt; |
| 3821 | |
| 3822 | last_sibling = list_last_entry(&rt->fib6_siblings, |
| 3823 | struct fib6_info, |
| 3824 | fib6_siblings); |
| 3825 | replace_rt = rcu_dereference_protected( |
| 3826 | last_sibling->fib6_next, |
| 3827 | lockdep_is_held(&table->tb6_lock)); |
| 3828 | if (replace_rt) |
| 3829 | call_fib6_entry_notifiers_replace(net, |
| 3830 | replace_rt); |
| 3831 | else |
| 3832 | call_fib6_multipath_entry_notifiers(net, |
Ido Schimmel | caafb25 | 2019-12-23 15:28:20 +0200 | [diff] [blame] | 3833 | FIB_EVENT_ENTRY_DEL, |
Ido Schimmel | 0284696 | 2019-12-23 15:28:18 +0200 | [diff] [blame] | 3834 | rt, rt->fib6_nsiblings, |
| 3835 | NULL); |
| 3836 | } |
David Ahern | 0ae8133 | 2017-02-02 12:37:08 -0800 | [diff] [blame] | 3837 | list_for_each_entry_safe(sibling, next_sibling, |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 3838 | &rt->fib6_siblings, |
| 3839 | fib6_siblings) { |
David Ahern | 0ae8133 | 2017-02-02 12:37:08 -0800 | [diff] [blame] | 3840 | err = fib6_del(sibling, info); |
| 3841 | if (err) |
WANG Cong | e333003 | 2017-02-27 16:07:43 -0800 | [diff] [blame] | 3842 | goto out_unlock; |
David Ahern | 0ae8133 | 2017-02-02 12:37:08 -0800 | [diff] [blame] | 3843 | } |
| 3844 | } |
| 3845 | |
| 3846 | err = fib6_del(rt, info); |
WANG Cong | e333003 | 2017-02-27 16:07:43 -0800 | [diff] [blame] | 3847 | out_unlock: |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 3848 | spin_unlock_bh(&table->tb6_lock); |
WANG Cong | e333003 | 2017-02-27 16:07:43 -0800 | [diff] [blame] | 3849 | out_put: |
David Ahern | 93531c6 | 2018-04-17 17:33:25 -0700 | [diff] [blame] | 3850 | fib6_info_release(rt); |
David Ahern | 16a16cd | 2017-02-02 12:37:11 -0800 | [diff] [blame] | 3851 | |
| 3852 | if (skb) { |
WANG Cong | e333003 | 2017-02-27 16:07:43 -0800 | [diff] [blame] | 3853 | rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE, |
David Ahern | 16a16cd | 2017-02-02 12:37:11 -0800 | [diff] [blame] | 3854 | info->nlh, gfp_any()); |
| 3855 | } |
David Ahern | 0ae8133 | 2017-02-02 12:37:08 -0800 | [diff] [blame] | 3856 | return err; |
| 3857 | } |
| 3858 | |
David Ahern | 0fa6efc | 2019-05-22 20:28:00 -0700 | [diff] [blame] | 3859 | static int __ip6_del_cached_rt(struct rt6_info *rt, struct fib6_config *cfg) |
David Ahern | 23fb93a | 2018-04-17 17:33:23 -0700 | [diff] [blame] | 3860 | { |
| 3861 | int rc = -ESRCH; |
| 3862 | |
| 3863 | if (cfg->fc_ifindex && rt->dst.dev->ifindex != cfg->fc_ifindex) |
| 3864 | goto out; |
| 3865 | |
| 3866 | if (cfg->fc_flags & RTF_GATEWAY && |
| 3867 | !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway)) |
| 3868 | goto out; |
Xin Long | 761f602 | 2018-11-14 00:48:28 +0800 | [diff] [blame] | 3869 | |
| 3870 | rc = rt6_remove_exception_rt(rt); |
David Ahern | 23fb93a | 2018-04-17 17:33:23 -0700 | [diff] [blame] | 3871 | out: |
| 3872 | return rc; |
| 3873 | } |
| 3874 | |
David Ahern | 0fa6efc | 2019-05-22 20:28:00 -0700 | [diff] [blame] | 3875 | static int ip6_del_cached_rt(struct fib6_config *cfg, struct fib6_info *rt, |
| 3876 | struct fib6_nh *nh) |
| 3877 | { |
| 3878 | struct fib6_result res = { |
| 3879 | .f6i = rt, |
| 3880 | .nh = nh, |
| 3881 | }; |
| 3882 | struct rt6_info *rt_cache; |
| 3883 | |
| 3884 | rt_cache = rt6_find_cached_rt(&res, &cfg->fc_dst, &cfg->fc_src); |
| 3885 | if (rt_cache) |
| 3886 | return __ip6_del_cached_rt(rt_cache, cfg); |
| 3887 | |
| 3888 | return 0; |
| 3889 | } |
| 3890 | |
David Ahern | 5b98324 | 2019-06-08 14:53:34 -0700 | [diff] [blame] | 3891 | struct fib6_nh_del_cached_rt_arg { |
| 3892 | struct fib6_config *cfg; |
| 3893 | struct fib6_info *f6i; |
| 3894 | }; |
| 3895 | |
| 3896 | static int fib6_nh_del_cached_rt(struct fib6_nh *nh, void *_arg) |
| 3897 | { |
| 3898 | struct fib6_nh_del_cached_rt_arg *arg = _arg; |
| 3899 | int rc; |
| 3900 | |
| 3901 | rc = ip6_del_cached_rt(arg->cfg, arg->f6i, nh); |
| 3902 | return rc != -ESRCH ? rc : 0; |
| 3903 | } |
| 3904 | |
| 3905 | static int ip6_del_cached_rt_nh(struct fib6_config *cfg, struct fib6_info *f6i) |
| 3906 | { |
| 3907 | struct fib6_nh_del_cached_rt_arg arg = { |
| 3908 | .cfg = cfg, |
| 3909 | .f6i = f6i |
| 3910 | }; |
| 3911 | |
| 3912 | return nexthop_for_each_fib6_nh(f6i->nh, fib6_nh_del_cached_rt, &arg); |
| 3913 | } |
| 3914 | |
David Ahern | 333c430 | 2017-05-21 10:12:04 -0600 | [diff] [blame] | 3915 | static int ip6_route_del(struct fib6_config *cfg, |
| 3916 | struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3917 | { |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 3918 | struct fib6_table *table; |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 3919 | struct fib6_info *rt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3920 | struct fib6_node *fn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3921 | int err = -ESRCH; |
| 3922 | |
Daniel Lezcano | 5578689 | 2008-03-04 13:47:47 -0800 | [diff] [blame] | 3923 | table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table); |
David Ahern | d5d531c | 2017-05-21 10:12:05 -0600 | [diff] [blame] | 3924 | if (!table) { |
| 3925 | NL_SET_ERR_MSG(extack, "FIB table does not exist"); |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 3926 | return err; |
David Ahern | d5d531c | 2017-05-21 10:12:05 -0600 | [diff] [blame] | 3927 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3928 | |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 3929 | rcu_read_lock(); |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 3930 | |
| 3931 | fn = fib6_locate(&table->tb6_root, |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 3932 | &cfg->fc_dst, cfg->fc_dst_len, |
Wei Wang | 38fbeee | 2017-10-06 12:06:02 -0700 | [diff] [blame] | 3933 | &cfg->fc_src, cfg->fc_src_len, |
Wei Wang | 2b760fc | 2017-10-06 12:06:03 -0700 | [diff] [blame] | 3934 | !(cfg->fc_flags & RTF_CACHE)); |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 3935 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3936 | if (fn) { |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 3937 | for_each_fib6_node_rt_rcu(fn) { |
David Ahern | ad1601a | 2019-03-27 20:53:56 -0700 | [diff] [blame] | 3938 | struct fib6_nh *nh; |
| 3939 | |
Stefano Brivio | 3401bfb | 2019-06-21 17:45:25 +0200 | [diff] [blame] | 3940 | if (rt->nh && cfg->fc_nh_id && |
| 3941 | rt->nh->id != cfg->fc_nh_id) |
David Ahern | 5b98324 | 2019-06-08 14:53:34 -0700 | [diff] [blame] | 3942 | continue; |
David Ahern | 23fb93a | 2018-04-17 17:33:23 -0700 | [diff] [blame] | 3943 | |
David Ahern | 5b98324 | 2019-06-08 14:53:34 -0700 | [diff] [blame] | 3944 | if (cfg->fc_flags & RTF_CACHE) { |
| 3945 | int rc = 0; |
| 3946 | |
| 3947 | if (rt->nh) { |
| 3948 | rc = ip6_del_cached_rt_nh(cfg, rt); |
| 3949 | } else if (cfg->fc_nh_id) { |
| 3950 | continue; |
| 3951 | } else { |
| 3952 | nh = rt->fib6_nh; |
| 3953 | rc = ip6_del_cached_rt(cfg, rt, nh); |
| 3954 | } |
David Ahern | 0fa6efc | 2019-05-22 20:28:00 -0700 | [diff] [blame] | 3955 | if (rc != -ESRCH) { |
| 3956 | rcu_read_unlock(); |
| 3957 | return rc; |
David Ahern | 23fb93a | 2018-04-17 17:33:23 -0700 | [diff] [blame] | 3958 | } |
| 3959 | continue; |
Wei Wang | 2b760fc | 2017-10-06 12:06:03 -0700 | [diff] [blame] | 3960 | } |
David Ahern | ad1601a | 2019-03-27 20:53:56 -0700 | [diff] [blame] | 3961 | |
David Ahern | 5b98324 | 2019-06-08 14:53:34 -0700 | [diff] [blame] | 3962 | if (cfg->fc_metric && cfg->fc_metric != rt->fib6_metric) |
| 3963 | continue; |
| 3964 | if (cfg->fc_protocol && |
| 3965 | cfg->fc_protocol != rt->fib6_protocol) |
| 3966 | continue; |
| 3967 | |
| 3968 | if (rt->nh) { |
| 3969 | if (!fib6_info_hold_safe(rt)) |
| 3970 | continue; |
| 3971 | rcu_read_unlock(); |
| 3972 | |
| 3973 | return __ip6_del_rt(rt, &cfg->fc_nlinfo); |
| 3974 | } |
| 3975 | if (cfg->fc_nh_id) |
| 3976 | continue; |
| 3977 | |
| 3978 | nh = rt->fib6_nh; |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 3979 | if (cfg->fc_ifindex && |
David Ahern | ad1601a | 2019-03-27 20:53:56 -0700 | [diff] [blame] | 3980 | (!nh->fib_nh_dev || |
| 3981 | nh->fib_nh_dev->ifindex != cfg->fc_ifindex)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3982 | continue; |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 3983 | if (cfg->fc_flags & RTF_GATEWAY && |
David Ahern | ad1601a | 2019-03-27 20:53:56 -0700 | [diff] [blame] | 3984 | !ipv6_addr_equal(&cfg->fc_gateway, &nh->fib_nh_gw6)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3985 | continue; |
Wei Wang | e873e4b | 2018-07-21 20:56:32 -0700 | [diff] [blame] | 3986 | if (!fib6_info_hold_safe(rt)) |
| 3987 | continue; |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 3988 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3989 | |
David Ahern | 0ae8133 | 2017-02-02 12:37:08 -0800 | [diff] [blame] | 3990 | /* if gateway was specified only delete the one hop */ |
| 3991 | if (cfg->fc_flags & RTF_GATEWAY) |
| 3992 | return __ip6_del_rt(rt, &cfg->fc_nlinfo); |
| 3993 | |
| 3994 | return __ip6_del_rt_siblings(rt, cfg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3995 | } |
| 3996 | } |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 3997 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3998 | |
| 3999 | return err; |
| 4000 | } |
| 4001 | |
David S. Miller | 6700c27 | 2012-07-17 03:29:28 -0700 | [diff] [blame] | 4002 | static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_buff *skb) |
YOSHIFUJI Hideaki | a627945 | 2006-08-23 17:18:26 -0700 | [diff] [blame] | 4003 | { |
YOSHIFUJI Hideaki | a627945 | 2006-08-23 17:18:26 -0700 | [diff] [blame] | 4004 | struct netevent_redirect netevent; |
David S. Miller | e8599ff | 2012-07-11 23:43:53 -0700 | [diff] [blame] | 4005 | struct rt6_info *rt, *nrt = NULL; |
David Ahern | 85bd05d | 2019-04-16 14:36:01 -0700 | [diff] [blame] | 4006 | struct fib6_result res = {}; |
David S. Miller | e8599ff | 2012-07-11 23:43:53 -0700 | [diff] [blame] | 4007 | struct ndisc_options ndopts; |
| 4008 | struct inet6_dev *in6_dev; |
| 4009 | struct neighbour *neigh; |
YOSHIFUJI Hideaki / 吉藤英明 | 71bcdba | 2013-01-05 16:34:51 +0000 | [diff] [blame] | 4010 | struct rd_msg *msg; |
David S. Miller | 6e157b6 | 2012-07-12 00:05:02 -0700 | [diff] [blame] | 4011 | int optlen, on_link; |
| 4012 | u8 *lladdr; |
David S. Miller | e8599ff | 2012-07-11 23:43:53 -0700 | [diff] [blame] | 4013 | |
Simon Horman | 29a3cad | 2013-05-28 20:34:26 +0000 | [diff] [blame] | 4014 | optlen = skb_tail_pointer(skb) - skb_transport_header(skb); |
YOSHIFUJI Hideaki / 吉藤英明 | 71bcdba | 2013-01-05 16:34:51 +0000 | [diff] [blame] | 4015 | optlen -= sizeof(*msg); |
David S. Miller | e8599ff | 2012-07-11 23:43:53 -0700 | [diff] [blame] | 4016 | |
| 4017 | if (optlen < 0) { |
David S. Miller | 6e157b6 | 2012-07-12 00:05:02 -0700 | [diff] [blame] | 4018 | net_dbg_ratelimited("rt6_do_redirect: packet too short\n"); |
David S. Miller | e8599ff | 2012-07-11 23:43:53 -0700 | [diff] [blame] | 4019 | return; |
| 4020 | } |
| 4021 | |
YOSHIFUJI Hideaki / 吉藤英明 | 71bcdba | 2013-01-05 16:34:51 +0000 | [diff] [blame] | 4022 | msg = (struct rd_msg *)icmp6_hdr(skb); |
David S. Miller | e8599ff | 2012-07-11 23:43:53 -0700 | [diff] [blame] | 4023 | |
YOSHIFUJI Hideaki / 吉藤英明 | 71bcdba | 2013-01-05 16:34:51 +0000 | [diff] [blame] | 4024 | if (ipv6_addr_is_multicast(&msg->dest)) { |
David S. Miller | 6e157b6 | 2012-07-12 00:05:02 -0700 | [diff] [blame] | 4025 | net_dbg_ratelimited("rt6_do_redirect: destination address is multicast\n"); |
David S. Miller | e8599ff | 2012-07-11 23:43:53 -0700 | [diff] [blame] | 4026 | return; |
| 4027 | } |
| 4028 | |
David S. Miller | 6e157b6 | 2012-07-12 00:05:02 -0700 | [diff] [blame] | 4029 | on_link = 0; |
YOSHIFUJI Hideaki / 吉藤英明 | 71bcdba | 2013-01-05 16:34:51 +0000 | [diff] [blame] | 4030 | if (ipv6_addr_equal(&msg->dest, &msg->target)) { |
David S. Miller | e8599ff | 2012-07-11 23:43:53 -0700 | [diff] [blame] | 4031 | on_link = 1; |
YOSHIFUJI Hideaki / 吉藤英明 | 71bcdba | 2013-01-05 16:34:51 +0000 | [diff] [blame] | 4032 | } else if (ipv6_addr_type(&msg->target) != |
David S. Miller | e8599ff | 2012-07-11 23:43:53 -0700 | [diff] [blame] | 4033 | (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) { |
David S. Miller | 6e157b6 | 2012-07-12 00:05:02 -0700 | [diff] [blame] | 4034 | net_dbg_ratelimited("rt6_do_redirect: target address is not link-local unicast\n"); |
David S. Miller | e8599ff | 2012-07-11 23:43:53 -0700 | [diff] [blame] | 4035 | return; |
| 4036 | } |
| 4037 | |
| 4038 | in6_dev = __in6_dev_get(skb->dev); |
| 4039 | if (!in6_dev) |
| 4040 | return; |
| 4041 | if (in6_dev->cnf.forwarding || !in6_dev->cnf.accept_redirects) |
| 4042 | return; |
| 4043 | |
| 4044 | /* RFC2461 8.1: |
| 4045 | * The IP source address of the Redirect MUST be the same as the current |
| 4046 | * first-hop router for the specified ICMP Destination Address. |
| 4047 | */ |
| 4048 | |
Alexander Aring | f997c55 | 2016-06-15 21:20:23 +0200 | [diff] [blame] | 4049 | if (!ndisc_parse_options(skb->dev, msg->opt, optlen, &ndopts)) { |
David S. Miller | e8599ff | 2012-07-11 23:43:53 -0700 | [diff] [blame] | 4050 | net_dbg_ratelimited("rt6_redirect: invalid ND options\n"); |
| 4051 | return; |
| 4052 | } |
David S. Miller | 6e157b6 | 2012-07-12 00:05:02 -0700 | [diff] [blame] | 4053 | |
| 4054 | lladdr = NULL; |
David S. Miller | e8599ff | 2012-07-11 23:43:53 -0700 | [diff] [blame] | 4055 | if (ndopts.nd_opts_tgt_lladdr) { |
| 4056 | lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr, |
| 4057 | skb->dev); |
| 4058 | if (!lladdr) { |
| 4059 | net_dbg_ratelimited("rt6_redirect: invalid link-layer address length\n"); |
| 4060 | return; |
| 4061 | } |
| 4062 | } |
| 4063 | |
David S. Miller | 6e157b6 | 2012-07-12 00:05:02 -0700 | [diff] [blame] | 4064 | rt = (struct rt6_info *) dst; |
Matthias Schiffer | ec13ad1 | 2015-11-02 01:24:38 +0100 | [diff] [blame] | 4065 | if (rt->rt6i_flags & RTF_REJECT) { |
David S. Miller | 6e157b6 | 2012-07-12 00:05:02 -0700 | [diff] [blame] | 4066 | net_dbg_ratelimited("rt6_redirect: source isn't a valid nexthop for redirect target\n"); |
| 4067 | return; |
| 4068 | } |
| 4069 | |
| 4070 | /* Redirect received -> path was valid. |
| 4071 | * Look, redirects are sent only in response to data packets, |
| 4072 | * so that this nexthop apparently is reachable. --ANK |
| 4073 | */ |
Julian Anastasov | 0dec879 | 2017-02-06 23:14:16 +0200 | [diff] [blame] | 4074 | dst_confirm_neigh(&rt->dst, &ipv6_hdr(skb)->saddr); |
David S. Miller | 6e157b6 | 2012-07-12 00:05:02 -0700 | [diff] [blame] | 4075 | |
YOSHIFUJI Hideaki / 吉藤英明 | 71bcdba | 2013-01-05 16:34:51 +0000 | [diff] [blame] | 4076 | neigh = __neigh_lookup(&nd_tbl, &msg->target, skb->dev, 1); |
David S. Miller | e8599ff | 2012-07-11 23:43:53 -0700 | [diff] [blame] | 4077 | if (!neigh) |
| 4078 | return; |
| 4079 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4080 | /* |
| 4081 | * We have finally decided to accept it. |
| 4082 | */ |
| 4083 | |
Alexander Aring | f997c55 | 2016-06-15 21:20:23 +0200 | [diff] [blame] | 4084 | ndisc_update(skb->dev, neigh, lladdr, NUD_STALE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4085 | NEIGH_UPDATE_F_WEAK_OVERRIDE| |
| 4086 | NEIGH_UPDATE_F_OVERRIDE| |
| 4087 | (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER| |
Alexander Aring | f997c55 | 2016-06-15 21:20:23 +0200 | [diff] [blame] | 4088 | NEIGH_UPDATE_F_ISROUTER)), |
| 4089 | NDISC_REDIRECT, &ndopts); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4090 | |
David Ahern | 4d85cd0 | 2018-04-20 15:37:59 -0700 | [diff] [blame] | 4091 | rcu_read_lock(); |
David Ahern | 85bd05d | 2019-04-16 14:36:01 -0700 | [diff] [blame] | 4092 | res.f6i = rcu_dereference(rt->from); |
David S. Miller | ff24e49 | 2019-05-02 22:14:21 -0400 | [diff] [blame] | 4093 | if (!res.f6i) |
Martin KaFai Lau | 886b7a5 | 2019-04-30 10:45:12 -0700 | [diff] [blame] | 4094 | goto out; |
David Ahern | 8a14e46 | 2018-04-23 11:32:07 -0700 | [diff] [blame] | 4095 | |
David Ahern | 49d5b8e | 2019-06-08 14:53:30 -0700 | [diff] [blame] | 4096 | if (res.f6i->nh) { |
| 4097 | struct fib6_nh_match_arg arg = { |
| 4098 | .dev = dst->dev, |
| 4099 | .gw = &rt->rt6i_gateway, |
| 4100 | }; |
| 4101 | |
| 4102 | nexthop_for_each_fib6_nh(res.f6i->nh, |
| 4103 | fib6_nh_find_match, &arg); |
| 4104 | |
| 4105 | /* fib6_info uses a nexthop that does not have fib6_nh |
| 4106 | * using the dst->dev. Should be impossible |
| 4107 | */ |
| 4108 | if (!arg.match) |
| 4109 | goto out; |
| 4110 | res.nh = arg.match; |
| 4111 | } else { |
| 4112 | res.nh = res.f6i->fib6_nh; |
| 4113 | } |
| 4114 | |
David Ahern | 7d21fec | 2019-04-16 14:36:11 -0700 | [diff] [blame] | 4115 | res.fib6_flags = res.f6i->fib6_flags; |
| 4116 | res.fib6_type = res.f6i->fib6_type; |
David Ahern | 85bd05d | 2019-04-16 14:36:01 -0700 | [diff] [blame] | 4117 | nrt = ip6_rt_cache_alloc(&res, &msg->dest, NULL); |
David S. Miller | 3830847 | 2011-12-03 18:02:47 -0500 | [diff] [blame] | 4118 | if (!nrt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4119 | goto out; |
| 4120 | |
| 4121 | nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE; |
| 4122 | if (on_link) |
| 4123 | nrt->rt6i_flags &= ~RTF_GATEWAY; |
| 4124 | |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 4125 | nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4126 | |
Martin KaFai Lau | 886b7a5 | 2019-04-30 10:45:12 -0700 | [diff] [blame] | 4127 | /* rt6_insert_exception() will take care of duplicated exceptions */ |
David Ahern | 5012f0a | 2019-04-16 14:36:05 -0700 | [diff] [blame] | 4128 | if (rt6_insert_exception(nrt, &res)) { |
Wei Wang | 2b760fc | 2017-10-06 12:06:03 -0700 | [diff] [blame] | 4129 | dst_release_immediate(&nrt->dst); |
| 4130 | goto out; |
| 4131 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4132 | |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 4133 | netevent.old = &rt->dst; |
| 4134 | netevent.new = &nrt->dst; |
YOSHIFUJI Hideaki / 吉藤英明 | 71bcdba | 2013-01-05 16:34:51 +0000 | [diff] [blame] | 4135 | netevent.daddr = &msg->dest; |
YOSHIFUJI Hideaki / 吉藤英明 | 6059283 | 2013-01-14 09:28:27 +0000 | [diff] [blame] | 4136 | netevent.neigh = neigh; |
Tom Tucker | 8d71740 | 2006-07-30 20:43:36 -0700 | [diff] [blame] | 4137 | call_netevent_notifiers(NETEVENT_REDIRECT, &netevent); |
| 4138 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4139 | out: |
Martin KaFai Lau | 886b7a5 | 2019-04-30 10:45:12 -0700 | [diff] [blame] | 4140 | rcu_read_unlock(); |
David S. Miller | e8599ff | 2012-07-11 23:43:53 -0700 | [diff] [blame] | 4141 | neigh_release(neigh); |
David S. Miller | 6e157b6 | 2012-07-12 00:05:02 -0700 | [diff] [blame] | 4142 | } |
| 4143 | |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 4144 | #ifdef CONFIG_IPV6_ROUTE_INFO |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4145 | static struct fib6_info *rt6_get_route_info(struct net *net, |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 4146 | const struct in6_addr *prefix, int prefixlen, |
David Ahern | 830218c | 2016-10-24 10:52:35 -0700 | [diff] [blame] | 4147 | const struct in6_addr *gwaddr, |
| 4148 | struct net_device *dev) |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 4149 | { |
David Ahern | 830218c | 2016-10-24 10:52:35 -0700 | [diff] [blame] | 4150 | u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO; |
| 4151 | int ifindex = dev->ifindex; |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 4152 | struct fib6_node *fn; |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4153 | struct fib6_info *rt = NULL; |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 4154 | struct fib6_table *table; |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 4155 | |
David Ahern | 830218c | 2016-10-24 10:52:35 -0700 | [diff] [blame] | 4156 | table = fib6_get_table(net, tb_id); |
David S. Miller | 3830847 | 2011-12-03 18:02:47 -0500 | [diff] [blame] | 4157 | if (!table) |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 4158 | return NULL; |
| 4159 | |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 4160 | rcu_read_lock(); |
Wei Wang | 38fbeee | 2017-10-06 12:06:02 -0700 | [diff] [blame] | 4161 | fn = fib6_locate(&table->tb6_root, prefix, prefixlen, NULL, 0, true); |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 4162 | if (!fn) |
| 4163 | goto out; |
| 4164 | |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 4165 | for_each_fib6_node_rt_rcu(fn) { |
David Ahern | f88d8ea | 2019-06-03 20:19:52 -0700 | [diff] [blame] | 4166 | /* these routes do not use nexthops */ |
| 4167 | if (rt->nh) |
| 4168 | continue; |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 4169 | if (rt->fib6_nh->fib_nh_dev->ifindex != ifindex) |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 4170 | continue; |
David Ahern | 2b2450c | 2019-03-27 20:53:52 -0700 | [diff] [blame] | 4171 | if (!(rt->fib6_flags & RTF_ROUTEINFO) || |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 4172 | !rt->fib6_nh->fib_nh_gw_family) |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 4173 | continue; |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 4174 | if (!ipv6_addr_equal(&rt->fib6_nh->fib_nh_gw6, gwaddr)) |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 4175 | continue; |
Wei Wang | e873e4b | 2018-07-21 20:56:32 -0700 | [diff] [blame] | 4176 | if (!fib6_info_hold_safe(rt)) |
| 4177 | continue; |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 4178 | break; |
| 4179 | } |
| 4180 | out: |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 4181 | rcu_read_unlock(); |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 4182 | return rt; |
| 4183 | } |
| 4184 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4185 | static struct fib6_info *rt6_add_route_info(struct net *net, |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 4186 | const struct in6_addr *prefix, int prefixlen, |
David Ahern | 830218c | 2016-10-24 10:52:35 -0700 | [diff] [blame] | 4187 | const struct in6_addr *gwaddr, |
| 4188 | struct net_device *dev, |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 4189 | unsigned int pref) |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 4190 | { |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4191 | struct fib6_config cfg = { |
Rami Rosen | 238fc7e | 2008-02-09 23:43:11 -0800 | [diff] [blame] | 4192 | .fc_metric = IP6_RT_PRIO_USER, |
David Ahern | 830218c | 2016-10-24 10:52:35 -0700 | [diff] [blame] | 4193 | .fc_ifindex = dev->ifindex, |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4194 | .fc_dst_len = prefixlen, |
| 4195 | .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO | |
| 4196 | RTF_UP | RTF_PREF(pref), |
Xin Long | b91d532 | 2017-08-03 14:13:46 +0800 | [diff] [blame] | 4197 | .fc_protocol = RTPROT_RA, |
David Ahern | e8478e8 | 2018-04-17 17:33:13 -0700 | [diff] [blame] | 4198 | .fc_type = RTN_UNICAST, |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 4199 | .fc_nlinfo.portid = 0, |
Daniel Lezcano | efa2cea | 2008-03-04 13:46:48 -0800 | [diff] [blame] | 4200 | .fc_nlinfo.nlh = NULL, |
| 4201 | .fc_nlinfo.nl_net = net, |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4202 | }; |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 4203 | |
David Ahern | 830218c | 2016-10-24 10:52:35 -0700 | [diff] [blame] | 4204 | cfg.fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO, |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 4205 | cfg.fc_dst = *prefix; |
| 4206 | cfg.fc_gateway = *gwaddr; |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4207 | |
YOSHIFUJI Hideaki | e317da9 | 2006-03-20 17:06:42 -0800 | [diff] [blame] | 4208 | /* We should treat it as a default route if prefix length is 0. */ |
| 4209 | if (!prefixlen) |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4210 | cfg.fc_flags |= RTF_DEFAULT; |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 4211 | |
David Ahern | acb54e3 | 2018-04-17 17:33:22 -0700 | [diff] [blame] | 4212 | ip6_route_add(&cfg, GFP_ATOMIC, NULL); |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 4213 | |
David Ahern | 830218c | 2016-10-24 10:52:35 -0700 | [diff] [blame] | 4214 | return rt6_get_route_info(net, prefix, prefixlen, gwaddr, dev); |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 4215 | } |
| 4216 | #endif |
| 4217 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4218 | struct fib6_info *rt6_get_dflt_router(struct net *net, |
David Ahern | afb1d4b5 | 2018-04-17 17:33:11 -0700 | [diff] [blame] | 4219 | const struct in6_addr *addr, |
| 4220 | struct net_device *dev) |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 4221 | { |
David Ahern | 830218c | 2016-10-24 10:52:35 -0700 | [diff] [blame] | 4222 | u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT; |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4223 | struct fib6_info *rt; |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 4224 | struct fib6_table *table; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4225 | |
David Ahern | afb1d4b5 | 2018-04-17 17:33:11 -0700 | [diff] [blame] | 4226 | table = fib6_get_table(net, tb_id); |
David S. Miller | 3830847 | 2011-12-03 18:02:47 -0500 | [diff] [blame] | 4227 | if (!table) |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 4228 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4229 | |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 4230 | rcu_read_lock(); |
| 4231 | for_each_fib6_node_rt_rcu(&table->tb6_root) { |
David Ahern | f88d8ea | 2019-06-03 20:19:52 -0700 | [diff] [blame] | 4232 | struct fib6_nh *nh; |
David Ahern | ad1601a | 2019-03-27 20:53:56 -0700 | [diff] [blame] | 4233 | |
David Ahern | f88d8ea | 2019-06-03 20:19:52 -0700 | [diff] [blame] | 4234 | /* RA routes do not use nexthops */ |
| 4235 | if (rt->nh) |
| 4236 | continue; |
| 4237 | |
| 4238 | nh = rt->fib6_nh; |
David Ahern | ad1601a | 2019-03-27 20:53:56 -0700 | [diff] [blame] | 4239 | if (dev == nh->fib_nh_dev && |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 4240 | ((rt->fib6_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) && |
David Ahern | ad1601a | 2019-03-27 20:53:56 -0700 | [diff] [blame] | 4241 | ipv6_addr_equal(&nh->fib_nh_gw6, addr)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4242 | break; |
| 4243 | } |
Wei Wang | e873e4b | 2018-07-21 20:56:32 -0700 | [diff] [blame] | 4244 | if (rt && !fib6_info_hold_safe(rt)) |
| 4245 | rt = NULL; |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 4246 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4247 | return rt; |
| 4248 | } |
| 4249 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4250 | struct fib6_info *rt6_add_dflt_router(struct net *net, |
David Ahern | afb1d4b5 | 2018-04-17 17:33:11 -0700 | [diff] [blame] | 4251 | const struct in6_addr *gwaddr, |
YOSHIFUJI Hideaki | ebacaaa | 2006-03-20 17:04:53 -0800 | [diff] [blame] | 4252 | struct net_device *dev, |
| 4253 | unsigned int pref) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4254 | { |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4255 | struct fib6_config cfg = { |
David Ahern | ca25449 | 2015-10-12 11:47:10 -0700 | [diff] [blame] | 4256 | .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT, |
Rami Rosen | 238fc7e | 2008-02-09 23:43:11 -0800 | [diff] [blame] | 4257 | .fc_metric = IP6_RT_PRIO_USER, |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4258 | .fc_ifindex = dev->ifindex, |
| 4259 | .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT | |
| 4260 | RTF_UP | RTF_EXPIRES | RTF_PREF(pref), |
Xin Long | b91d532 | 2017-08-03 14:13:46 +0800 | [diff] [blame] | 4261 | .fc_protocol = RTPROT_RA, |
David Ahern | e8478e8 | 2018-04-17 17:33:13 -0700 | [diff] [blame] | 4262 | .fc_type = RTN_UNICAST, |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 4263 | .fc_nlinfo.portid = 0, |
Daniel Lezcano | 5578689 | 2008-03-04 13:47:47 -0800 | [diff] [blame] | 4264 | .fc_nlinfo.nlh = NULL, |
David Ahern | afb1d4b5 | 2018-04-17 17:33:11 -0700 | [diff] [blame] | 4265 | .fc_nlinfo.nl_net = net, |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4266 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4267 | |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 4268 | cfg.fc_gateway = *gwaddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4269 | |
David Ahern | acb54e3 | 2018-04-17 17:33:22 -0700 | [diff] [blame] | 4270 | if (!ip6_route_add(&cfg, GFP_ATOMIC, NULL)) { |
David Ahern | 830218c | 2016-10-24 10:52:35 -0700 | [diff] [blame] | 4271 | struct fib6_table *table; |
| 4272 | |
| 4273 | table = fib6_get_table(dev_net(dev), cfg.fc_table); |
| 4274 | if (table) |
| 4275 | table->flags |= RT6_TABLE_HAS_DFLT_ROUTER; |
| 4276 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4277 | |
David Ahern | afb1d4b5 | 2018-04-17 17:33:11 -0700 | [diff] [blame] | 4278 | return rt6_get_dflt_router(net, gwaddr, dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4279 | } |
| 4280 | |
David Ahern | afb1d4b5 | 2018-04-17 17:33:11 -0700 | [diff] [blame] | 4281 | static void __rt6_purge_dflt_routers(struct net *net, |
| 4282 | struct fib6_table *table) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4283 | { |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4284 | struct fib6_info *rt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4285 | |
| 4286 | restart: |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 4287 | rcu_read_lock(); |
| 4288 | for_each_fib6_node_rt_rcu(&table->tb6_root) { |
David Ahern | dcd1f57 | 2018-04-18 15:39:05 -0700 | [diff] [blame] | 4289 | struct net_device *dev = fib6_info_nh_dev(rt); |
| 4290 | struct inet6_dev *idev = dev ? __in6_dev_get(dev) : NULL; |
| 4291 | |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 4292 | if (rt->fib6_flags & (RTF_DEFAULT | RTF_ADDRCONF) && |
Wei Wang | e873e4b | 2018-07-21 20:56:32 -0700 | [diff] [blame] | 4293 | (!idev || idev->cnf.accept_ra != 2) && |
| 4294 | fib6_info_hold_safe(rt)) { |
David Ahern | 93531c6 | 2018-04-17 17:33:25 -0700 | [diff] [blame] | 4295 | rcu_read_unlock(); |
Roopa Prabhu | 11dd74b | 2020-04-27 13:56:45 -0700 | [diff] [blame] | 4296 | ip6_del_rt(net, rt, false); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4297 | goto restart; |
| 4298 | } |
| 4299 | } |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 4300 | rcu_read_unlock(); |
David Ahern | 830218c | 2016-10-24 10:52:35 -0700 | [diff] [blame] | 4301 | |
| 4302 | table->flags &= ~RT6_TABLE_HAS_DFLT_ROUTER; |
| 4303 | } |
| 4304 | |
| 4305 | void rt6_purge_dflt_routers(struct net *net) |
| 4306 | { |
| 4307 | struct fib6_table *table; |
| 4308 | struct hlist_head *head; |
| 4309 | unsigned int h; |
| 4310 | |
| 4311 | rcu_read_lock(); |
| 4312 | |
| 4313 | for (h = 0; h < FIB6_TABLE_HASHSZ; h++) { |
| 4314 | head = &net->ipv6.fib_table_hash[h]; |
| 4315 | hlist_for_each_entry_rcu(table, head, tb6_hlist) { |
| 4316 | if (table->flags & RT6_TABLE_HAS_DFLT_ROUTER) |
David Ahern | afb1d4b5 | 2018-04-17 17:33:11 -0700 | [diff] [blame] | 4317 | __rt6_purge_dflt_routers(net, table); |
David Ahern | 830218c | 2016-10-24 10:52:35 -0700 | [diff] [blame] | 4318 | } |
| 4319 | } |
| 4320 | |
| 4321 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4322 | } |
| 4323 | |
Daniel Lezcano | 5578689 | 2008-03-04 13:47:47 -0800 | [diff] [blame] | 4324 | static void rtmsg_to_fib6_config(struct net *net, |
| 4325 | struct in6_rtmsg *rtmsg, |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4326 | struct fib6_config *cfg) |
| 4327 | { |
Maciej Żenczykowski | 8823a3a | 2018-09-29 23:44:52 -0700 | [diff] [blame] | 4328 | *cfg = (struct fib6_config){ |
| 4329 | .fc_table = l3mdev_fib_table_by_index(net, rtmsg->rtmsg_ifindex) ? |
| 4330 | : RT6_TABLE_MAIN, |
| 4331 | .fc_ifindex = rtmsg->rtmsg_ifindex, |
David Ahern | 67f6951 | 2019-03-21 05:21:34 -0700 | [diff] [blame] | 4332 | .fc_metric = rtmsg->rtmsg_metric ? : IP6_RT_PRIO_USER, |
Maciej Żenczykowski | 8823a3a | 2018-09-29 23:44:52 -0700 | [diff] [blame] | 4333 | .fc_expires = rtmsg->rtmsg_info, |
| 4334 | .fc_dst_len = rtmsg->rtmsg_dst_len, |
| 4335 | .fc_src_len = rtmsg->rtmsg_src_len, |
| 4336 | .fc_flags = rtmsg->rtmsg_flags, |
| 4337 | .fc_type = rtmsg->rtmsg_type, |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4338 | |
Maciej Żenczykowski | 8823a3a | 2018-09-29 23:44:52 -0700 | [diff] [blame] | 4339 | .fc_nlinfo.nl_net = net, |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4340 | |
Maciej Żenczykowski | 8823a3a | 2018-09-29 23:44:52 -0700 | [diff] [blame] | 4341 | .fc_dst = rtmsg->rtmsg_dst, |
| 4342 | .fc_src = rtmsg->rtmsg_src, |
| 4343 | .fc_gateway = rtmsg->rtmsg_gateway, |
| 4344 | }; |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4345 | } |
| 4346 | |
Christoph Hellwig | 7c1552da | 2020-05-18 08:28:05 +0200 | [diff] [blame] | 4347 | int ipv6_route_ioctl(struct net *net, unsigned int cmd, struct in6_rtmsg *rtmsg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4348 | { |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4349 | struct fib6_config cfg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4350 | int err; |
| 4351 | |
Christoph Hellwig | 7c1552da | 2020-05-18 08:28:05 +0200 | [diff] [blame] | 4352 | if (cmd != SIOCADDRT && cmd != SIOCDELRT) |
| 4353 | return -EINVAL; |
| 4354 | if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) |
| 4355 | return -EPERM; |
| 4356 | |
| 4357 | rtmsg_to_fib6_config(net, rtmsg, &cfg); |
| 4358 | |
| 4359 | rtnl_lock(); |
Ian Morris | 67ba415 | 2014-08-24 21:53:10 +0100 | [diff] [blame] | 4360 | switch (cmd) { |
Christoph Hellwig | 7c1552da | 2020-05-18 08:28:05 +0200 | [diff] [blame] | 4361 | case SIOCADDRT: |
| 4362 | err = ip6_route_add(&cfg, GFP_KERNEL, NULL); |
| 4363 | break; |
| 4364 | case SIOCDELRT: |
| 4365 | err = ip6_route_del(&cfg, NULL); |
| 4366 | break; |
Stephen Hemminger | 3ff50b7 | 2007-04-20 17:09:22 -0700 | [diff] [blame] | 4367 | } |
Christoph Hellwig | 7c1552da | 2020-05-18 08:28:05 +0200 | [diff] [blame] | 4368 | rtnl_unlock(); |
| 4369 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4370 | } |
| 4371 | |
| 4372 | /* |
| 4373 | * Drop the packet on the floor |
| 4374 | */ |
| 4375 | |
Brian Haley | d5fdd6b | 2009-06-23 04:31:07 -0700 | [diff] [blame] | 4376 | static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4377 | { |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 4378 | struct dst_entry *dst = skb_dst(skb); |
Stephen Suryaputra | 1d3fd8a | 2019-04-27 09:14:33 -0400 | [diff] [blame] | 4379 | struct net *net = dev_net(dst->dev); |
| 4380 | struct inet6_dev *idev; |
| 4381 | int type; |
| 4382 | |
| 4383 | if (netif_is_l3_master(skb->dev) && |
| 4384 | dst->dev == net->loopback_dev) |
| 4385 | idev = __in6_dev_get_safely(dev_get_by_index_rcu(net, IP6CB(skb)->iif)); |
| 4386 | else |
| 4387 | idev = ip6_dst_idev(dst); |
| 4388 | |
YOSHIFUJI Hideaki | 612f09e | 2007-04-13 16:18:02 -0700 | [diff] [blame] | 4389 | switch (ipstats_mib_noroutes) { |
| 4390 | case IPSTATS_MIB_INNOROUTES: |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 4391 | type = ipv6_addr_type(&ipv6_hdr(skb)->daddr); |
Ulrich Weber | 45bb006 | 2010-02-25 23:28:58 +0000 | [diff] [blame] | 4392 | if (type == IPV6_ADDR_ANY) { |
Stephen Suryaputra | 1d3fd8a | 2019-04-27 09:14:33 -0400 | [diff] [blame] | 4393 | IP6_INC_STATS(net, idev, IPSTATS_MIB_INADDRERRORS); |
YOSHIFUJI Hideaki | 612f09e | 2007-04-13 16:18:02 -0700 | [diff] [blame] | 4394 | break; |
| 4395 | } |
Joe Perches | a8eceea | 2020-03-12 15:50:22 -0700 | [diff] [blame] | 4396 | fallthrough; |
YOSHIFUJI Hideaki | 612f09e | 2007-04-13 16:18:02 -0700 | [diff] [blame] | 4397 | case IPSTATS_MIB_OUTNOROUTES: |
Stephen Suryaputra | 1d3fd8a | 2019-04-27 09:14:33 -0400 | [diff] [blame] | 4398 | IP6_INC_STATS(net, idev, ipstats_mib_noroutes); |
YOSHIFUJI Hideaki | 612f09e | 2007-04-13 16:18:02 -0700 | [diff] [blame] | 4399 | break; |
| 4400 | } |
Stephen Suryaputra | 1d3fd8a | 2019-04-27 09:14:33 -0400 | [diff] [blame] | 4401 | |
| 4402 | /* Start over by dropping the dst for l3mdev case */ |
| 4403 | if (netif_is_l3_master(skb->dev)) |
| 4404 | skb_dst_drop(skb); |
| 4405 | |
Alexey Dobriyan | 3ffe533 | 2010-02-18 08:25:24 +0000 | [diff] [blame] | 4406 | icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4407 | kfree_skb(skb); |
| 4408 | return 0; |
| 4409 | } |
| 4410 | |
Thomas Graf | 9ce8ade | 2006-10-18 20:46:54 -0700 | [diff] [blame] | 4411 | static int ip6_pkt_discard(struct sk_buff *skb) |
| 4412 | { |
YOSHIFUJI Hideaki | 612f09e | 2007-04-13 16:18:02 -0700 | [diff] [blame] | 4413 | return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES); |
Thomas Graf | 9ce8ade | 2006-10-18 20:46:54 -0700 | [diff] [blame] | 4414 | } |
| 4415 | |
Eric W. Biederman | ede2059 | 2015-10-07 16:48:47 -0500 | [diff] [blame] | 4416 | static int ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4417 | { |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 4418 | skb->dev = skb_dst(skb)->dev; |
YOSHIFUJI Hideaki | 612f09e | 2007-04-13 16:18:02 -0700 | [diff] [blame] | 4419 | return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4420 | } |
| 4421 | |
Thomas Graf | 9ce8ade | 2006-10-18 20:46:54 -0700 | [diff] [blame] | 4422 | static int ip6_pkt_prohibit(struct sk_buff *skb) |
| 4423 | { |
YOSHIFUJI Hideaki | 612f09e | 2007-04-13 16:18:02 -0700 | [diff] [blame] | 4424 | return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES); |
Thomas Graf | 9ce8ade | 2006-10-18 20:46:54 -0700 | [diff] [blame] | 4425 | } |
| 4426 | |
Eric W. Biederman | ede2059 | 2015-10-07 16:48:47 -0500 | [diff] [blame] | 4427 | static int ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb) |
Thomas Graf | 9ce8ade | 2006-10-18 20:46:54 -0700 | [diff] [blame] | 4428 | { |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 4429 | skb->dev = skb_dst(skb)->dev; |
YOSHIFUJI Hideaki | 612f09e | 2007-04-13 16:18:02 -0700 | [diff] [blame] | 4430 | return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES); |
Thomas Graf | 9ce8ade | 2006-10-18 20:46:54 -0700 | [diff] [blame] | 4431 | } |
| 4432 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4433 | /* |
| 4434 | * Allocate a dst for local (unicast / anycast) address. |
| 4435 | */ |
| 4436 | |
David Ahern | 360a988 | 2018-04-18 15:39:00 -0700 | [diff] [blame] | 4437 | struct fib6_info *addrconf_f6i_alloc(struct net *net, |
| 4438 | struct inet6_dev *idev, |
| 4439 | const struct in6_addr *addr, |
| 4440 | bool anycast, gfp_t gfp_flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4441 | { |
David Ahern | c7a1ce3 | 2019-03-21 05:21:35 -0700 | [diff] [blame] | 4442 | struct fib6_config cfg = { |
| 4443 | .fc_table = l3mdev_fib_table(idev->dev) ? : RT6_TABLE_LOCAL, |
| 4444 | .fc_ifindex = idev->dev->ifindex, |
Maciej Żenczykowski | d55a2e3 | 2019-09-02 09:23:36 -0700 | [diff] [blame] | 4445 | .fc_flags = RTF_UP | RTF_NONEXTHOP, |
David Ahern | c7a1ce3 | 2019-03-21 05:21:35 -0700 | [diff] [blame] | 4446 | .fc_dst = *addr, |
| 4447 | .fc_dst_len = 128, |
| 4448 | .fc_protocol = RTPROT_KERNEL, |
| 4449 | .fc_nlinfo.nl_net = net, |
| 4450 | .fc_ignore_dev_down = true, |
| 4451 | }; |
Maciej Żenczykowski | d55a2e3 | 2019-09-02 09:23:36 -0700 | [diff] [blame] | 4452 | struct fib6_info *f6i; |
David Ahern | 5f02ce24 | 2016-09-10 12:09:54 -0700 | [diff] [blame] | 4453 | |
David Ahern | e8478e8 | 2018-04-17 17:33:13 -0700 | [diff] [blame] | 4454 | if (anycast) { |
David Ahern | c7a1ce3 | 2019-03-21 05:21:35 -0700 | [diff] [blame] | 4455 | cfg.fc_type = RTN_ANYCAST; |
| 4456 | cfg.fc_flags |= RTF_ANYCAST; |
David Ahern | e8478e8 | 2018-04-17 17:33:13 -0700 | [diff] [blame] | 4457 | } else { |
David Ahern | c7a1ce3 | 2019-03-21 05:21:35 -0700 | [diff] [blame] | 4458 | cfg.fc_type = RTN_LOCAL; |
| 4459 | cfg.fc_flags |= RTF_LOCAL; |
David Ahern | e8478e8 | 2018-04-17 17:33:13 -0700 | [diff] [blame] | 4460 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4461 | |
Maciej Żenczykowski | d55a2e3 | 2019-09-02 09:23:36 -0700 | [diff] [blame] | 4462 | f6i = ip6_route_info_create(&cfg, gfp_flags, NULL); |
Maciej Żenczykowski | 8652f17 | 2019-09-05 20:56:37 -0700 | [diff] [blame] | 4463 | if (!IS_ERR(f6i)) |
Maciej Żenczykowski | d55a2e3 | 2019-09-02 09:23:36 -0700 | [diff] [blame] | 4464 | f6i->dst_nocount = true; |
| 4465 | return f6i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4466 | } |
| 4467 | |
Daniel Walter | c3968a8 | 2011-04-13 21:10:57 +0000 | [diff] [blame] | 4468 | /* remove deleted ip from prefsrc entries */ |
| 4469 | struct arg_dev_net_ip { |
| 4470 | struct net_device *dev; |
| 4471 | struct net *net; |
| 4472 | struct in6_addr *addr; |
| 4473 | }; |
| 4474 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4475 | static int fib6_remove_prefsrc(struct fib6_info *rt, void *arg) |
Daniel Walter | c3968a8 | 2011-04-13 21:10:57 +0000 | [diff] [blame] | 4476 | { |
| 4477 | struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev; |
| 4478 | struct net *net = ((struct arg_dev_net_ip *)arg)->net; |
| 4479 | struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr; |
| 4480 | |
David Ahern | f88d8ea | 2019-06-03 20:19:52 -0700 | [diff] [blame] | 4481 | if (!rt->nh && |
| 4482 | ((void *)rt->fib6_nh->fib_nh_dev == dev || !dev) && |
David Ahern | 421842e | 2018-04-17 17:33:18 -0700 | [diff] [blame] | 4483 | rt != net->ipv6.fib6_null_entry && |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 4484 | ipv6_addr_equal(addr, &rt->fib6_prefsrc.addr)) { |
Wei Wang | 60006a4 | 2017-10-06 12:05:58 -0700 | [diff] [blame] | 4485 | spin_lock_bh(&rt6_exception_lock); |
Daniel Walter | c3968a8 | 2011-04-13 21:10:57 +0000 | [diff] [blame] | 4486 | /* remove prefsrc entry */ |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 4487 | rt->fib6_prefsrc.plen = 0; |
Wei Wang | 60006a4 | 2017-10-06 12:05:58 -0700 | [diff] [blame] | 4488 | spin_unlock_bh(&rt6_exception_lock); |
Daniel Walter | c3968a8 | 2011-04-13 21:10:57 +0000 | [diff] [blame] | 4489 | } |
| 4490 | return 0; |
| 4491 | } |
| 4492 | |
| 4493 | void rt6_remove_prefsrc(struct inet6_ifaddr *ifp) |
| 4494 | { |
| 4495 | struct net *net = dev_net(ifp->idev->dev); |
| 4496 | struct arg_dev_net_ip adni = { |
| 4497 | .dev = ifp->idev->dev, |
| 4498 | .net = net, |
| 4499 | .addr = &ifp->addr, |
| 4500 | }; |
Li RongQing | 0c3584d | 2013-12-27 16:32:38 +0800 | [diff] [blame] | 4501 | fib6_clean_all(net, fib6_remove_prefsrc, &adni); |
Daniel Walter | c3968a8 | 2011-04-13 21:10:57 +0000 | [diff] [blame] | 4502 | } |
| 4503 | |
David Ahern | 2b2450c | 2019-03-27 20:53:52 -0700 | [diff] [blame] | 4504 | #define RTF_RA_ROUTER (RTF_ADDRCONF | RTF_DEFAULT) |
Duan Jiong | be7a010 | 2014-05-15 15:56:14 +0800 | [diff] [blame] | 4505 | |
| 4506 | /* Remove routers and update dst entries when gateway turn into host. */ |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4507 | static int fib6_clean_tohost(struct fib6_info *rt, void *arg) |
Duan Jiong | be7a010 | 2014-05-15 15:56:14 +0800 | [diff] [blame] | 4508 | { |
| 4509 | struct in6_addr *gateway = (struct in6_addr *)arg; |
David Ahern | f88d8ea | 2019-06-03 20:19:52 -0700 | [diff] [blame] | 4510 | struct fib6_nh *nh; |
Duan Jiong | be7a010 | 2014-05-15 15:56:14 +0800 | [diff] [blame] | 4511 | |
David Ahern | f88d8ea | 2019-06-03 20:19:52 -0700 | [diff] [blame] | 4512 | /* RA routes do not use nexthops */ |
| 4513 | if (rt->nh) |
| 4514 | return 0; |
| 4515 | |
| 4516 | nh = rt->fib6_nh; |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 4517 | if (((rt->fib6_flags & RTF_RA_ROUTER) == RTF_RA_ROUTER) && |
David Ahern | cc5c073 | 2019-05-22 20:27:58 -0700 | [diff] [blame] | 4518 | nh->fib_nh_gw_family && ipv6_addr_equal(gateway, &nh->fib_nh_gw6)) |
Duan Jiong | be7a010 | 2014-05-15 15:56:14 +0800 | [diff] [blame] | 4519 | return -1; |
Wei Wang | b16cb45 | 2017-10-06 12:06:00 -0700 | [diff] [blame] | 4520 | |
| 4521 | /* Further clean up cached routes in exception table. |
| 4522 | * This is needed because cached route may have a different |
| 4523 | * gateway than its 'parent' in the case of an ip redirect. |
| 4524 | */ |
David Ahern | cc5c073 | 2019-05-22 20:27:58 -0700 | [diff] [blame] | 4525 | fib6_nh_exceptions_clean_tohost(nh, gateway); |
Wei Wang | b16cb45 | 2017-10-06 12:06:00 -0700 | [diff] [blame] | 4526 | |
Duan Jiong | be7a010 | 2014-05-15 15:56:14 +0800 | [diff] [blame] | 4527 | return 0; |
| 4528 | } |
| 4529 | |
| 4530 | void rt6_clean_tohost(struct net *net, struct in6_addr *gateway) |
| 4531 | { |
| 4532 | fib6_clean_all(net, fib6_clean_tohost, gateway); |
| 4533 | } |
| 4534 | |
Ido Schimmel | 2127d95 | 2018-01-07 12:45:03 +0200 | [diff] [blame] | 4535 | struct arg_netdev_event { |
| 4536 | const struct net_device *dev; |
Ido Schimmel | 4c981e2 | 2018-01-07 12:45:04 +0200 | [diff] [blame] | 4537 | union { |
David Ahern | ecc5663 | 2019-04-23 08:48:09 -0700 | [diff] [blame] | 4538 | unsigned char nh_flags; |
Ido Schimmel | 4c981e2 | 2018-01-07 12:45:04 +0200 | [diff] [blame] | 4539 | unsigned long event; |
| 4540 | }; |
Ido Schimmel | 2127d95 | 2018-01-07 12:45:03 +0200 | [diff] [blame] | 4541 | }; |
| 4542 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4543 | static struct fib6_info *rt6_multipath_first_sibling(const struct fib6_info *rt) |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 4544 | { |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4545 | struct fib6_info *iter; |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 4546 | struct fib6_node *fn; |
| 4547 | |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 4548 | fn = rcu_dereference_protected(rt->fib6_node, |
| 4549 | lockdep_is_held(&rt->fib6_table->tb6_lock)); |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 4550 | iter = rcu_dereference_protected(fn->leaf, |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 4551 | lockdep_is_held(&rt->fib6_table->tb6_lock)); |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 4552 | while (iter) { |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 4553 | if (iter->fib6_metric == rt->fib6_metric && |
David Ahern | 33bd5ac | 2018-07-03 14:36:21 -0700 | [diff] [blame] | 4554 | rt6_qualify_for_ecmp(iter)) |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 4555 | return iter; |
David Ahern | 8fb11a9 | 2018-05-04 13:54:24 -0700 | [diff] [blame] | 4556 | iter = rcu_dereference_protected(iter->fib6_next, |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 4557 | lockdep_is_held(&rt->fib6_table->tb6_lock)); |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 4558 | } |
| 4559 | |
| 4560 | return NULL; |
| 4561 | } |
| 4562 | |
David Ahern | f88d8ea | 2019-06-03 20:19:52 -0700 | [diff] [blame] | 4563 | /* only called for fib entries with builtin fib6_nh */ |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4564 | static bool rt6_is_dead(const struct fib6_info *rt) |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 4565 | { |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 4566 | if (rt->fib6_nh->fib_nh_flags & RTNH_F_DEAD || |
| 4567 | (rt->fib6_nh->fib_nh_flags & RTNH_F_LINKDOWN && |
| 4568 | ip6_ignore_linkdown(rt->fib6_nh->fib_nh_dev))) |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 4569 | return true; |
| 4570 | |
| 4571 | return false; |
| 4572 | } |
| 4573 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4574 | static int rt6_multipath_total_weight(const struct fib6_info *rt) |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 4575 | { |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4576 | struct fib6_info *iter; |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 4577 | int total = 0; |
| 4578 | |
| 4579 | if (!rt6_is_dead(rt)) |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 4580 | total += rt->fib6_nh->fib_nh_weight; |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 4581 | |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 4582 | list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) { |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 4583 | if (!rt6_is_dead(iter)) |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 4584 | total += iter->fib6_nh->fib_nh_weight; |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 4585 | } |
| 4586 | |
| 4587 | return total; |
| 4588 | } |
| 4589 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4590 | static void rt6_upper_bound_set(struct fib6_info *rt, int *weight, int total) |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 4591 | { |
| 4592 | int upper_bound = -1; |
| 4593 | |
| 4594 | if (!rt6_is_dead(rt)) { |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 4595 | *weight += rt->fib6_nh->fib_nh_weight; |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 4596 | upper_bound = DIV_ROUND_CLOSEST_ULL((u64) (*weight) << 31, |
| 4597 | total) - 1; |
| 4598 | } |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 4599 | atomic_set(&rt->fib6_nh->fib_nh_upper_bound, upper_bound); |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 4600 | } |
| 4601 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4602 | static void rt6_multipath_upper_bound_set(struct fib6_info *rt, int total) |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 4603 | { |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4604 | struct fib6_info *iter; |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 4605 | int weight = 0; |
| 4606 | |
| 4607 | rt6_upper_bound_set(rt, &weight, total); |
| 4608 | |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 4609 | list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 4610 | rt6_upper_bound_set(iter, &weight, total); |
| 4611 | } |
| 4612 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4613 | void rt6_multipath_rebalance(struct fib6_info *rt) |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 4614 | { |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4615 | struct fib6_info *first; |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 4616 | int total; |
| 4617 | |
| 4618 | /* In case the entire multipath route was marked for flushing, |
| 4619 | * then there is no need to rebalance upon the removal of every |
| 4620 | * sibling route. |
| 4621 | */ |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 4622 | if (!rt->fib6_nsiblings || rt->should_flush) |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 4623 | return; |
| 4624 | |
| 4625 | /* During lookup routes are evaluated in order, so we need to |
| 4626 | * make sure upper bounds are assigned from the first sibling |
| 4627 | * onwards. |
| 4628 | */ |
| 4629 | first = rt6_multipath_first_sibling(rt); |
| 4630 | if (WARN_ON_ONCE(!first)) |
| 4631 | return; |
| 4632 | |
| 4633 | total = rt6_multipath_total_weight(first); |
| 4634 | rt6_multipath_upper_bound_set(first, total); |
| 4635 | } |
| 4636 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4637 | static int fib6_ifup(struct fib6_info *rt, void *p_arg) |
Ido Schimmel | 2127d95 | 2018-01-07 12:45:03 +0200 | [diff] [blame] | 4638 | { |
| 4639 | const struct arg_netdev_event *arg = p_arg; |
David Ahern | 7aef685 | 2018-04-17 17:33:10 -0700 | [diff] [blame] | 4640 | struct net *net = dev_net(arg->dev); |
Ido Schimmel | 2127d95 | 2018-01-07 12:45:03 +0200 | [diff] [blame] | 4641 | |
David Ahern | f88d8ea | 2019-06-03 20:19:52 -0700 | [diff] [blame] | 4642 | if (rt != net->ipv6.fib6_null_entry && !rt->nh && |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 4643 | rt->fib6_nh->fib_nh_dev == arg->dev) { |
| 4644 | rt->fib6_nh->fib_nh_flags &= ~arg->nh_flags; |
David Ahern | 7aef685 | 2018-04-17 17:33:10 -0700 | [diff] [blame] | 4645 | fib6_update_sernum_upto_root(net, rt); |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 4646 | rt6_multipath_rebalance(rt); |
Ido Schimmel | 1de178e | 2018-01-07 12:45:15 +0200 | [diff] [blame] | 4647 | } |
Ido Schimmel | 2127d95 | 2018-01-07 12:45:03 +0200 | [diff] [blame] | 4648 | |
| 4649 | return 0; |
| 4650 | } |
| 4651 | |
David Ahern | ecc5663 | 2019-04-23 08:48:09 -0700 | [diff] [blame] | 4652 | void rt6_sync_up(struct net_device *dev, unsigned char nh_flags) |
Ido Schimmel | 2127d95 | 2018-01-07 12:45:03 +0200 | [diff] [blame] | 4653 | { |
| 4654 | struct arg_netdev_event arg = { |
| 4655 | .dev = dev, |
Ido Schimmel | 6802f3a | 2018-01-12 22:07:36 +0200 | [diff] [blame] | 4656 | { |
| 4657 | .nh_flags = nh_flags, |
| 4658 | }, |
Ido Schimmel | 2127d95 | 2018-01-07 12:45:03 +0200 | [diff] [blame] | 4659 | }; |
| 4660 | |
| 4661 | if (nh_flags & RTNH_F_DEAD && netif_carrier_ok(dev)) |
| 4662 | arg.nh_flags |= RTNH_F_LINKDOWN; |
| 4663 | |
| 4664 | fib6_clean_all(dev_net(dev), fib6_ifup, &arg); |
| 4665 | } |
| 4666 | |
David Ahern | f88d8ea | 2019-06-03 20:19:52 -0700 | [diff] [blame] | 4667 | /* only called for fib entries with inline fib6_nh */ |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4668 | static bool rt6_multipath_uses_dev(const struct fib6_info *rt, |
Ido Schimmel | 1de178e | 2018-01-07 12:45:15 +0200 | [diff] [blame] | 4669 | const struct net_device *dev) |
| 4670 | { |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4671 | struct fib6_info *iter; |
Ido Schimmel | 1de178e | 2018-01-07 12:45:15 +0200 | [diff] [blame] | 4672 | |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 4673 | if (rt->fib6_nh->fib_nh_dev == dev) |
Ido Schimmel | 1de178e | 2018-01-07 12:45:15 +0200 | [diff] [blame] | 4674 | return true; |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 4675 | list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 4676 | if (iter->fib6_nh->fib_nh_dev == dev) |
Ido Schimmel | 1de178e | 2018-01-07 12:45:15 +0200 | [diff] [blame] | 4677 | return true; |
| 4678 | |
| 4679 | return false; |
| 4680 | } |
| 4681 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4682 | static void rt6_multipath_flush(struct fib6_info *rt) |
Ido Schimmel | 1de178e | 2018-01-07 12:45:15 +0200 | [diff] [blame] | 4683 | { |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4684 | struct fib6_info *iter; |
Ido Schimmel | 1de178e | 2018-01-07 12:45:15 +0200 | [diff] [blame] | 4685 | |
| 4686 | rt->should_flush = 1; |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 4687 | list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) |
Ido Schimmel | 1de178e | 2018-01-07 12:45:15 +0200 | [diff] [blame] | 4688 | iter->should_flush = 1; |
| 4689 | } |
| 4690 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4691 | static unsigned int rt6_multipath_dead_count(const struct fib6_info *rt, |
Ido Schimmel | 1de178e | 2018-01-07 12:45:15 +0200 | [diff] [blame] | 4692 | const struct net_device *down_dev) |
| 4693 | { |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4694 | struct fib6_info *iter; |
Ido Schimmel | 1de178e | 2018-01-07 12:45:15 +0200 | [diff] [blame] | 4695 | unsigned int dead = 0; |
| 4696 | |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 4697 | if (rt->fib6_nh->fib_nh_dev == down_dev || |
| 4698 | rt->fib6_nh->fib_nh_flags & RTNH_F_DEAD) |
Ido Schimmel | 1de178e | 2018-01-07 12:45:15 +0200 | [diff] [blame] | 4699 | dead++; |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 4700 | list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 4701 | if (iter->fib6_nh->fib_nh_dev == down_dev || |
| 4702 | iter->fib6_nh->fib_nh_flags & RTNH_F_DEAD) |
Ido Schimmel | 1de178e | 2018-01-07 12:45:15 +0200 | [diff] [blame] | 4703 | dead++; |
| 4704 | |
| 4705 | return dead; |
| 4706 | } |
| 4707 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4708 | static void rt6_multipath_nh_flags_set(struct fib6_info *rt, |
Ido Schimmel | 1de178e | 2018-01-07 12:45:15 +0200 | [diff] [blame] | 4709 | const struct net_device *dev, |
David Ahern | ecc5663 | 2019-04-23 08:48:09 -0700 | [diff] [blame] | 4710 | unsigned char nh_flags) |
Ido Schimmel | 1de178e | 2018-01-07 12:45:15 +0200 | [diff] [blame] | 4711 | { |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4712 | struct fib6_info *iter; |
Ido Schimmel | 1de178e | 2018-01-07 12:45:15 +0200 | [diff] [blame] | 4713 | |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 4714 | if (rt->fib6_nh->fib_nh_dev == dev) |
| 4715 | rt->fib6_nh->fib_nh_flags |= nh_flags; |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 4716 | list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 4717 | if (iter->fib6_nh->fib_nh_dev == dev) |
| 4718 | iter->fib6_nh->fib_nh_flags |= nh_flags; |
Ido Schimmel | 1de178e | 2018-01-07 12:45:15 +0200 | [diff] [blame] | 4719 | } |
| 4720 | |
David Ahern | a1a22c1 | 2017-01-18 07:40:36 -0800 | [diff] [blame] | 4721 | /* called with write lock held for table with rt */ |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4722 | static int fib6_ifdown(struct fib6_info *rt, void *p_arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4723 | { |
Ido Schimmel | 4c981e2 | 2018-01-07 12:45:04 +0200 | [diff] [blame] | 4724 | const struct arg_netdev_event *arg = p_arg; |
| 4725 | const struct net_device *dev = arg->dev; |
David Ahern | 7aef685 | 2018-04-17 17:33:10 -0700 | [diff] [blame] | 4726 | struct net *net = dev_net(dev); |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 4727 | |
David Ahern | f88d8ea | 2019-06-03 20:19:52 -0700 | [diff] [blame] | 4728 | if (rt == net->ipv6.fib6_null_entry || rt->nh) |
Ido Schimmel | 27c6fa7 | 2018-01-07 12:45:05 +0200 | [diff] [blame] | 4729 | return 0; |
| 4730 | |
| 4731 | switch (arg->event) { |
| 4732 | case NETDEV_UNREGISTER: |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 4733 | return rt->fib6_nh->fib_nh_dev == dev ? -1 : 0; |
Ido Schimmel | 27c6fa7 | 2018-01-07 12:45:05 +0200 | [diff] [blame] | 4734 | case NETDEV_DOWN: |
Ido Schimmel | 1de178e | 2018-01-07 12:45:15 +0200 | [diff] [blame] | 4735 | if (rt->should_flush) |
Ido Schimmel | 27c6fa7 | 2018-01-07 12:45:05 +0200 | [diff] [blame] | 4736 | return -1; |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 4737 | if (!rt->fib6_nsiblings) |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 4738 | return rt->fib6_nh->fib_nh_dev == dev ? -1 : 0; |
Ido Schimmel | 1de178e | 2018-01-07 12:45:15 +0200 | [diff] [blame] | 4739 | if (rt6_multipath_uses_dev(rt, dev)) { |
| 4740 | unsigned int count; |
| 4741 | |
| 4742 | count = rt6_multipath_dead_count(rt, dev); |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 4743 | if (rt->fib6_nsiblings + 1 == count) { |
Ido Schimmel | 1de178e | 2018-01-07 12:45:15 +0200 | [diff] [blame] | 4744 | rt6_multipath_flush(rt); |
| 4745 | return -1; |
| 4746 | } |
| 4747 | rt6_multipath_nh_flags_set(rt, dev, RTNH_F_DEAD | |
| 4748 | RTNH_F_LINKDOWN); |
David Ahern | 7aef685 | 2018-04-17 17:33:10 -0700 | [diff] [blame] | 4749 | fib6_update_sernum(net, rt); |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 4750 | rt6_multipath_rebalance(rt); |
Ido Schimmel | 1de178e | 2018-01-07 12:45:15 +0200 | [diff] [blame] | 4751 | } |
| 4752 | return -2; |
Ido Schimmel | 27c6fa7 | 2018-01-07 12:45:05 +0200 | [diff] [blame] | 4753 | case NETDEV_CHANGE: |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 4754 | if (rt->fib6_nh->fib_nh_dev != dev || |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 4755 | rt->fib6_flags & (RTF_LOCAL | RTF_ANYCAST)) |
Ido Schimmel | 27c6fa7 | 2018-01-07 12:45:05 +0200 | [diff] [blame] | 4756 | break; |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 4757 | rt->fib6_nh->fib_nh_flags |= RTNH_F_LINKDOWN; |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 4758 | rt6_multipath_rebalance(rt); |
Ido Schimmel | 27c6fa7 | 2018-01-07 12:45:05 +0200 | [diff] [blame] | 4759 | break; |
Ido Schimmel | 2b24136 | 2018-01-07 12:45:02 +0200 | [diff] [blame] | 4760 | } |
David S. Miller | c159d30 | 2011-12-26 15:24:36 -0500 | [diff] [blame] | 4761 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4762 | return 0; |
| 4763 | } |
| 4764 | |
Ido Schimmel | 27c6fa7 | 2018-01-07 12:45:05 +0200 | [diff] [blame] | 4765 | void rt6_sync_down_dev(struct net_device *dev, unsigned long event) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4766 | { |
Ido Schimmel | 4c981e2 | 2018-01-07 12:45:04 +0200 | [diff] [blame] | 4767 | struct arg_netdev_event arg = { |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 4768 | .dev = dev, |
Ido Schimmel | 6802f3a | 2018-01-12 22:07:36 +0200 | [diff] [blame] | 4769 | { |
| 4770 | .event = event, |
| 4771 | }, |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 4772 | }; |
David Ahern | 7c6bb7d | 2018-10-11 20:17:21 -0700 | [diff] [blame] | 4773 | struct net *net = dev_net(dev); |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 4774 | |
David Ahern | 7c6bb7d | 2018-10-11 20:17:21 -0700 | [diff] [blame] | 4775 | if (net->ipv6.sysctl.skip_notify_on_dev_down) |
| 4776 | fib6_clean_all_skip_notify(net, fib6_ifdown, &arg); |
| 4777 | else |
| 4778 | fib6_clean_all(net, fib6_ifdown, &arg); |
Ido Schimmel | 4c981e2 | 2018-01-07 12:45:04 +0200 | [diff] [blame] | 4779 | } |
| 4780 | |
| 4781 | void rt6_disable_ip(struct net_device *dev, unsigned long event) |
| 4782 | { |
| 4783 | rt6_sync_down_dev(dev, event); |
| 4784 | rt6_uncached_list_flush_dev(dev_net(dev), dev); |
| 4785 | neigh_ifdown(&nd_tbl, dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4786 | } |
| 4787 | |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 4788 | struct rt6_mtu_change_arg { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4789 | struct net_device *dev; |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 4790 | unsigned int mtu; |
David Ahern | c0b220c | 2019-05-22 20:27:57 -0700 | [diff] [blame] | 4791 | struct fib6_info *f6i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4792 | }; |
| 4793 | |
David Ahern | cc5c073 | 2019-05-22 20:27:58 -0700 | [diff] [blame] | 4794 | static int fib6_nh_mtu_change(struct fib6_nh *nh, void *_arg) |
David Ahern | c0b220c | 2019-05-22 20:27:57 -0700 | [diff] [blame] | 4795 | { |
| 4796 | struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *)_arg; |
David Ahern | cc5c073 | 2019-05-22 20:27:58 -0700 | [diff] [blame] | 4797 | struct fib6_info *f6i = arg->f6i; |
David Ahern | c0b220c | 2019-05-22 20:27:57 -0700 | [diff] [blame] | 4798 | |
| 4799 | /* For administrative MTU increase, there is no way to discover |
| 4800 | * IPv6 PMTU increase, so PMTU increase should be updated here. |
| 4801 | * Since RFC 1981 doesn't include administrative MTU increase |
| 4802 | * update PMTU increase is a MUST. (i.e. jumbo frame) |
| 4803 | */ |
| 4804 | if (nh->fib_nh_dev == arg->dev) { |
| 4805 | struct inet6_dev *idev = __in6_dev_get(arg->dev); |
| 4806 | u32 mtu = f6i->fib6_pmtu; |
| 4807 | |
| 4808 | if (mtu >= arg->mtu || |
| 4809 | (mtu < arg->mtu && mtu == idev->cnf.mtu6)) |
| 4810 | fib6_metric_set(f6i, RTAX_MTU, arg->mtu); |
| 4811 | |
| 4812 | spin_lock_bh(&rt6_exception_lock); |
David Ahern | cc5c073 | 2019-05-22 20:27:58 -0700 | [diff] [blame] | 4813 | rt6_exceptions_update_pmtu(idev, nh, arg->mtu); |
David Ahern | c0b220c | 2019-05-22 20:27:57 -0700 | [diff] [blame] | 4814 | spin_unlock_bh(&rt6_exception_lock); |
| 4815 | } |
| 4816 | |
| 4817 | return 0; |
| 4818 | } |
| 4819 | |
| 4820 | static int rt6_mtu_change_route(struct fib6_info *f6i, void *p_arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4821 | { |
| 4822 | struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg; |
| 4823 | struct inet6_dev *idev; |
| 4824 | |
| 4825 | /* In IPv6 pmtu discovery is not optional, |
| 4826 | so that RTAX_MTU lock cannot disable it. |
| 4827 | We still use this lock to block changes |
| 4828 | caused by addrconf/ndisc. |
| 4829 | */ |
| 4830 | |
| 4831 | idev = __in6_dev_get(arg->dev); |
David S. Miller | 3830847 | 2011-12-03 18:02:47 -0500 | [diff] [blame] | 4832 | if (!idev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4833 | return 0; |
| 4834 | |
David Ahern | c0b220c | 2019-05-22 20:27:57 -0700 | [diff] [blame] | 4835 | if (fib6_metric_locked(f6i, RTAX_MTU)) |
| 4836 | return 0; |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 4837 | |
David Ahern | c0b220c | 2019-05-22 20:27:57 -0700 | [diff] [blame] | 4838 | arg->f6i = f6i; |
David Ahern | 2d44234 | 2019-06-08 14:53:31 -0700 | [diff] [blame] | 4839 | if (f6i->nh) { |
| 4840 | /* fib6_nh_mtu_change only returns 0, so this is safe */ |
| 4841 | return nexthop_for_each_fib6_nh(f6i->nh, fib6_nh_mtu_change, |
| 4842 | arg); |
| 4843 | } |
| 4844 | |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 4845 | return fib6_nh_mtu_change(f6i->fib6_nh, arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4846 | } |
| 4847 | |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 4848 | void rt6_mtu_change(struct net_device *dev, unsigned int mtu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4849 | { |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 4850 | struct rt6_mtu_change_arg arg = { |
| 4851 | .dev = dev, |
| 4852 | .mtu = mtu, |
| 4853 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4854 | |
Li RongQing | 0c3584d | 2013-12-27 16:32:38 +0800 | [diff] [blame] | 4855 | fib6_clean_all(dev_net(dev), rt6_mtu_change_route, &arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4856 | } |
| 4857 | |
Patrick McHardy | ef7c79e | 2007-06-05 12:38:30 -0700 | [diff] [blame] | 4858 | static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = { |
David Ahern | 7542565 | 2019-05-22 12:07:43 -0700 | [diff] [blame] | 4859 | [RTA_UNSPEC] = { .strict_start_type = RTA_DPORT + 1 }, |
Thomas Graf | 5176f91 | 2006-08-26 20:13:18 -0700 | [diff] [blame] | 4860 | [RTA_GATEWAY] = { .len = sizeof(struct in6_addr) }, |
Eric Dumazet | aa8f877 | 2018-04-22 18:29:23 -0700 | [diff] [blame] | 4861 | [RTA_PREFSRC] = { .len = sizeof(struct in6_addr) }, |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4862 | [RTA_OIF] = { .type = NLA_U32 }, |
Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 4863 | [RTA_IIF] = { .type = NLA_U32 }, |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4864 | [RTA_PRIORITY] = { .type = NLA_U32 }, |
| 4865 | [RTA_METRICS] = { .type = NLA_NESTED }, |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 4866 | [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) }, |
Lubomir Rintel | c78ba6d | 2015-03-11 15:39:21 +0100 | [diff] [blame] | 4867 | [RTA_PREF] = { .type = NLA_U8 }, |
Roopa Prabhu | 19e42e4 | 2015-07-21 10:43:48 +0200 | [diff] [blame] | 4868 | [RTA_ENCAP_TYPE] = { .type = NLA_U16 }, |
| 4869 | [RTA_ENCAP] = { .type = NLA_NESTED }, |
Xin Long | 32bc201 | 2015-12-16 17:50:11 +0800 | [diff] [blame] | 4870 | [RTA_EXPIRES] = { .type = NLA_U32 }, |
Lorenzo Colitti | 622ec2c | 2016-11-04 02:23:42 +0900 | [diff] [blame] | 4871 | [RTA_UID] = { .type = NLA_U32 }, |
Liping Zhang | 3b45a41 | 2017-02-27 20:59:39 +0800 | [diff] [blame] | 4872 | [RTA_MARK] = { .type = NLA_U32 }, |
Eric Dumazet | aa8f877 | 2018-04-22 18:29:23 -0700 | [diff] [blame] | 4873 | [RTA_TABLE] = { .type = NLA_U32 }, |
Roopa Prabhu | eacb938 | 2018-05-22 14:03:28 -0700 | [diff] [blame] | 4874 | [RTA_IP_PROTO] = { .type = NLA_U8 }, |
| 4875 | [RTA_SPORT] = { .type = NLA_U16 }, |
| 4876 | [RTA_DPORT] = { .type = NLA_U16 }, |
David Ahern | 5b98324 | 2019-06-08 14:53:34 -0700 | [diff] [blame] | 4877 | [RTA_NH_ID] = { .type = NLA_U32 }, |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4878 | }; |
| 4879 | |
| 4880 | static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh, |
David Ahern | 333c430 | 2017-05-21 10:12:04 -0600 | [diff] [blame] | 4881 | struct fib6_config *cfg, |
| 4882 | struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4883 | { |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4884 | struct rtmsg *rtm; |
| 4885 | struct nlattr *tb[RTA_MAX+1]; |
Lubomir Rintel | c78ba6d | 2015-03-11 15:39:21 +0100 | [diff] [blame] | 4886 | unsigned int pref; |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4887 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4888 | |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 4889 | err = nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX, |
| 4890 | rtm_ipv6_policy, extack); |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4891 | if (err < 0) |
| 4892 | goto errout; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4893 | |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4894 | err = -EINVAL; |
| 4895 | rtm = nlmsg_data(nlh); |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4896 | |
Maciej Żenczykowski | 84db840 | 2018-09-29 23:44:53 -0700 | [diff] [blame] | 4897 | *cfg = (struct fib6_config){ |
| 4898 | .fc_table = rtm->rtm_table, |
| 4899 | .fc_dst_len = rtm->rtm_dst_len, |
| 4900 | .fc_src_len = rtm->rtm_src_len, |
| 4901 | .fc_flags = RTF_UP, |
| 4902 | .fc_protocol = rtm->rtm_protocol, |
| 4903 | .fc_type = rtm->rtm_type, |
| 4904 | |
| 4905 | .fc_nlinfo.portid = NETLINK_CB(skb).portid, |
| 4906 | .fc_nlinfo.nlh = nlh, |
| 4907 | .fc_nlinfo.nl_net = sock_net(skb->sk), |
| 4908 | }; |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4909 | |
Nicolas Dichtel | ef2c7d7 | 2012-09-05 02:12:42 +0000 | [diff] [blame] | 4910 | if (rtm->rtm_type == RTN_UNREACHABLE || |
| 4911 | rtm->rtm_type == RTN_BLACKHOLE || |
Nicolas Dichtel | b4949ab | 2012-09-06 05:53:35 +0000 | [diff] [blame] | 4912 | rtm->rtm_type == RTN_PROHIBIT || |
| 4913 | rtm->rtm_type == RTN_THROW) |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4914 | cfg->fc_flags |= RTF_REJECT; |
| 4915 | |
Maciej Żenczykowski | ab79ad1 | 2010-09-27 00:07:02 +0000 | [diff] [blame] | 4916 | if (rtm->rtm_type == RTN_LOCAL) |
| 4917 | cfg->fc_flags |= RTF_LOCAL; |
| 4918 | |
Martin KaFai Lau | 1f56a01f | 2015-04-28 13:03:03 -0700 | [diff] [blame] | 4919 | if (rtm->rtm_flags & RTM_F_CLONED) |
| 4920 | cfg->fc_flags |= RTF_CACHE; |
| 4921 | |
David Ahern | fc1e64e | 2018-01-25 16:55:09 -0800 | [diff] [blame] | 4922 | cfg->fc_flags |= (rtm->rtm_flags & RTNH_F_ONLINK); |
| 4923 | |
David Ahern | 5b98324 | 2019-06-08 14:53:34 -0700 | [diff] [blame] | 4924 | if (tb[RTA_NH_ID]) { |
| 4925 | if (tb[RTA_GATEWAY] || tb[RTA_OIF] || |
| 4926 | tb[RTA_MULTIPATH] || tb[RTA_ENCAP]) { |
| 4927 | NL_SET_ERR_MSG(extack, |
| 4928 | "Nexthop specification and nexthop id are mutually exclusive"); |
| 4929 | goto errout; |
| 4930 | } |
| 4931 | cfg->fc_nh_id = nla_get_u32(tb[RTA_NH_ID]); |
| 4932 | } |
| 4933 | |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4934 | if (tb[RTA_GATEWAY]) { |
Jiri Benc | 67b61f6 | 2015-03-29 16:59:26 +0200 | [diff] [blame] | 4935 | cfg->fc_gateway = nla_get_in6_addr(tb[RTA_GATEWAY]); |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4936 | cfg->fc_flags |= RTF_GATEWAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4937 | } |
David Ahern | e381854 | 2019-02-26 09:00:03 -0800 | [diff] [blame] | 4938 | if (tb[RTA_VIA]) { |
| 4939 | NL_SET_ERR_MSG(extack, "IPv6 does not support RTA_VIA attribute"); |
| 4940 | goto errout; |
| 4941 | } |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4942 | |
| 4943 | if (tb[RTA_DST]) { |
| 4944 | int plen = (rtm->rtm_dst_len + 7) >> 3; |
| 4945 | |
| 4946 | if (nla_len(tb[RTA_DST]) < plen) |
| 4947 | goto errout; |
| 4948 | |
| 4949 | nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4950 | } |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4951 | |
| 4952 | if (tb[RTA_SRC]) { |
| 4953 | int plen = (rtm->rtm_src_len + 7) >> 3; |
| 4954 | |
| 4955 | if (nla_len(tb[RTA_SRC]) < plen) |
| 4956 | goto errout; |
| 4957 | |
| 4958 | nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4959 | } |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4960 | |
Daniel Walter | c3968a8 | 2011-04-13 21:10:57 +0000 | [diff] [blame] | 4961 | if (tb[RTA_PREFSRC]) |
Jiri Benc | 67b61f6 | 2015-03-29 16:59:26 +0200 | [diff] [blame] | 4962 | cfg->fc_prefsrc = nla_get_in6_addr(tb[RTA_PREFSRC]); |
Daniel Walter | c3968a8 | 2011-04-13 21:10:57 +0000 | [diff] [blame] | 4963 | |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4964 | if (tb[RTA_OIF]) |
| 4965 | cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]); |
| 4966 | |
| 4967 | if (tb[RTA_PRIORITY]) |
| 4968 | cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]); |
| 4969 | |
| 4970 | if (tb[RTA_METRICS]) { |
| 4971 | cfg->fc_mx = nla_data(tb[RTA_METRICS]); |
| 4972 | cfg->fc_mx_len = nla_len(tb[RTA_METRICS]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4973 | } |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4974 | |
| 4975 | if (tb[RTA_TABLE]) |
| 4976 | cfg->fc_table = nla_get_u32(tb[RTA_TABLE]); |
| 4977 | |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 4978 | if (tb[RTA_MULTIPATH]) { |
| 4979 | cfg->fc_mp = nla_data(tb[RTA_MULTIPATH]); |
| 4980 | cfg->fc_mp_len = nla_len(tb[RTA_MULTIPATH]); |
David Ahern | 9ed5959 | 2017-01-17 14:57:36 -0800 | [diff] [blame] | 4981 | |
| 4982 | err = lwtunnel_valid_encap_type_attr(cfg->fc_mp, |
David Ahern | c255bd6 | 2017-05-27 16:19:27 -0600 | [diff] [blame] | 4983 | cfg->fc_mp_len, extack); |
David Ahern | 9ed5959 | 2017-01-17 14:57:36 -0800 | [diff] [blame] | 4984 | if (err < 0) |
| 4985 | goto errout; |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 4986 | } |
| 4987 | |
Lubomir Rintel | c78ba6d | 2015-03-11 15:39:21 +0100 | [diff] [blame] | 4988 | if (tb[RTA_PREF]) { |
| 4989 | pref = nla_get_u8(tb[RTA_PREF]); |
| 4990 | if (pref != ICMPV6_ROUTER_PREF_LOW && |
| 4991 | pref != ICMPV6_ROUTER_PREF_HIGH) |
| 4992 | pref = ICMPV6_ROUTER_PREF_MEDIUM; |
| 4993 | cfg->fc_flags |= RTF_PREF(pref); |
| 4994 | } |
| 4995 | |
Roopa Prabhu | 19e42e4 | 2015-07-21 10:43:48 +0200 | [diff] [blame] | 4996 | if (tb[RTA_ENCAP]) |
| 4997 | cfg->fc_encap = tb[RTA_ENCAP]; |
| 4998 | |
David Ahern | 9ed5959 | 2017-01-17 14:57:36 -0800 | [diff] [blame] | 4999 | if (tb[RTA_ENCAP_TYPE]) { |
Roopa Prabhu | 19e42e4 | 2015-07-21 10:43:48 +0200 | [diff] [blame] | 5000 | cfg->fc_encap_type = nla_get_u16(tb[RTA_ENCAP_TYPE]); |
| 5001 | |
David Ahern | c255bd6 | 2017-05-27 16:19:27 -0600 | [diff] [blame] | 5002 | err = lwtunnel_valid_encap_type(cfg->fc_encap_type, extack); |
David Ahern | 9ed5959 | 2017-01-17 14:57:36 -0800 | [diff] [blame] | 5003 | if (err < 0) |
| 5004 | goto errout; |
| 5005 | } |
| 5006 | |
Xin Long | 32bc201 | 2015-12-16 17:50:11 +0800 | [diff] [blame] | 5007 | if (tb[RTA_EXPIRES]) { |
| 5008 | unsigned long timeout = addrconf_timeout_fixup(nla_get_u32(tb[RTA_EXPIRES]), HZ); |
| 5009 | |
| 5010 | if (addrconf_finite_timeout(timeout)) { |
| 5011 | cfg->fc_expires = jiffies_to_clock_t(timeout * HZ); |
| 5012 | cfg->fc_flags |= RTF_EXPIRES; |
| 5013 | } |
| 5014 | } |
| 5015 | |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 5016 | err = 0; |
| 5017 | errout: |
| 5018 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5019 | } |
| 5020 | |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 5021 | struct rt6_nh { |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 5022 | struct fib6_info *fib6_info; |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 5023 | struct fib6_config r_cfg; |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 5024 | struct list_head next; |
| 5025 | }; |
| 5026 | |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 5027 | static int ip6_route_info_append(struct net *net, |
| 5028 | struct list_head *rt6_nh_list, |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 5029 | struct fib6_info *rt, |
| 5030 | struct fib6_config *r_cfg) |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 5031 | { |
| 5032 | struct rt6_nh *nh; |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 5033 | int err = -EEXIST; |
| 5034 | |
| 5035 | list_for_each_entry(nh, rt6_nh_list, next) { |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 5036 | /* check if fib6_info already exists */ |
| 5037 | if (rt6_duplicate_nexthop(nh->fib6_info, rt)) |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 5038 | return err; |
| 5039 | } |
| 5040 | |
| 5041 | nh = kzalloc(sizeof(*nh), GFP_KERNEL); |
| 5042 | if (!nh) |
| 5043 | return -ENOMEM; |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 5044 | nh->fib6_info = rt; |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 5045 | memcpy(&nh->r_cfg, r_cfg, sizeof(*r_cfg)); |
| 5046 | list_add_tail(&nh->next, rt6_nh_list); |
| 5047 | |
| 5048 | return 0; |
| 5049 | } |
| 5050 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 5051 | static void ip6_route_mpath_notify(struct fib6_info *rt, |
| 5052 | struct fib6_info *rt_last, |
David Ahern | 3b1137f | 2017-02-02 12:37:10 -0800 | [diff] [blame] | 5053 | struct nl_info *info, |
| 5054 | __u16 nlflags) |
| 5055 | { |
| 5056 | /* if this is an APPEND route, then rt points to the first route |
| 5057 | * inserted and rt_last points to last route inserted. Userspace |
| 5058 | * wants a consistent dump of the route which starts at the first |
| 5059 | * nexthop. Since sibling routes are always added at the end of |
| 5060 | * the list, find the first sibling of the last route appended |
| 5061 | */ |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 5062 | if ((nlflags & NLM_F_APPEND) && rt_last && rt_last->fib6_nsiblings) { |
| 5063 | rt = list_first_entry(&rt_last->fib6_siblings, |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 5064 | struct fib6_info, |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 5065 | fib6_siblings); |
David Ahern | 3b1137f | 2017-02-02 12:37:10 -0800 | [diff] [blame] | 5066 | } |
| 5067 | |
| 5068 | if (rt) |
| 5069 | inet6_rt_notify(RTM_NEWROUTE, rt, info, nlflags); |
| 5070 | } |
| 5071 | |
Ido Schimmel | 0ee0f47 | 2019-12-23 15:28:15 +0200 | [diff] [blame] | 5072 | static bool ip6_route_mpath_should_notify(const struct fib6_info *rt) |
| 5073 | { |
| 5074 | bool rt_can_ecmp = rt6_qualify_for_ecmp(rt); |
| 5075 | bool should_notify = false; |
| 5076 | struct fib6_info *leaf; |
| 5077 | struct fib6_node *fn; |
| 5078 | |
| 5079 | rcu_read_lock(); |
| 5080 | fn = rcu_dereference(rt->fib6_node); |
| 5081 | if (!fn) |
| 5082 | goto out; |
| 5083 | |
| 5084 | leaf = rcu_dereference(fn->leaf); |
| 5085 | if (!leaf) |
| 5086 | goto out; |
| 5087 | |
| 5088 | if (rt == leaf || |
| 5089 | (rt_can_ecmp && rt->fib6_metric == leaf->fib6_metric && |
| 5090 | rt6_qualify_for_ecmp(leaf))) |
| 5091 | should_notify = true; |
| 5092 | out: |
| 5093 | rcu_read_unlock(); |
| 5094 | |
| 5095 | return should_notify; |
| 5096 | } |
| 5097 | |
David Ahern | 333c430 | 2017-05-21 10:12:04 -0600 | [diff] [blame] | 5098 | static int ip6_route_multipath_add(struct fib6_config *cfg, |
| 5099 | struct netlink_ext_ack *extack) |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 5100 | { |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 5101 | struct fib6_info *rt_notif = NULL, *rt_last = NULL; |
David Ahern | 3b1137f | 2017-02-02 12:37:10 -0800 | [diff] [blame] | 5102 | struct nl_info *info = &cfg->fc_nlinfo; |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 5103 | struct fib6_config r_cfg; |
| 5104 | struct rtnexthop *rtnh; |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 5105 | struct fib6_info *rt; |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 5106 | struct rt6_nh *err_nh; |
| 5107 | struct rt6_nh *nh, *nh_safe; |
David Ahern | 3b1137f | 2017-02-02 12:37:10 -0800 | [diff] [blame] | 5108 | __u16 nlflags; |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 5109 | int remaining; |
| 5110 | int attrlen; |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 5111 | int err = 1; |
| 5112 | int nhn = 0; |
| 5113 | int replace = (cfg->fc_nlinfo.nlh && |
| 5114 | (cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_REPLACE)); |
| 5115 | LIST_HEAD(rt6_nh_list); |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 5116 | |
David Ahern | 3b1137f | 2017-02-02 12:37:10 -0800 | [diff] [blame] | 5117 | nlflags = replace ? NLM_F_REPLACE : NLM_F_CREATE; |
| 5118 | if (info->nlh && info->nlh->nlmsg_flags & NLM_F_APPEND) |
| 5119 | nlflags |= NLM_F_APPEND; |
| 5120 | |
Michal Kubeček | 35f1b4e | 2015-05-18 20:53:55 +0200 | [diff] [blame] | 5121 | remaining = cfg->fc_mp_len; |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 5122 | rtnh = (struct rtnexthop *)cfg->fc_mp; |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 5123 | |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 5124 | /* Parse a Multipath Entry and build a list (rt6_nh_list) of |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 5125 | * fib6_info structs per nexthop |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 5126 | */ |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 5127 | while (rtnh_ok(rtnh, remaining)) { |
| 5128 | memcpy(&r_cfg, cfg, sizeof(*cfg)); |
| 5129 | if (rtnh->rtnh_ifindex) |
| 5130 | r_cfg.fc_ifindex = rtnh->rtnh_ifindex; |
| 5131 | |
| 5132 | attrlen = rtnh_attrlen(rtnh); |
| 5133 | if (attrlen > 0) { |
| 5134 | struct nlattr *nla, *attrs = rtnh_attrs(rtnh); |
| 5135 | |
| 5136 | nla = nla_find(attrs, attrlen, RTA_GATEWAY); |
| 5137 | if (nla) { |
Jiri Benc | 67b61f6 | 2015-03-29 16:59:26 +0200 | [diff] [blame] | 5138 | r_cfg.fc_gateway = nla_get_in6_addr(nla); |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 5139 | r_cfg.fc_flags |= RTF_GATEWAY; |
| 5140 | } |
Roopa Prabhu | 19e42e4 | 2015-07-21 10:43:48 +0200 | [diff] [blame] | 5141 | r_cfg.fc_encap = nla_find(attrs, attrlen, RTA_ENCAP); |
| 5142 | nla = nla_find(attrs, attrlen, RTA_ENCAP_TYPE); |
| 5143 | if (nla) |
| 5144 | r_cfg.fc_encap_type = nla_get_u16(nla); |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 5145 | } |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 5146 | |
David Ahern | 68e2ffd | 2018-03-20 10:06:59 -0700 | [diff] [blame] | 5147 | r_cfg.fc_flags |= (rtnh->rtnh_flags & RTNH_F_ONLINK); |
David Ahern | acb54e3 | 2018-04-17 17:33:22 -0700 | [diff] [blame] | 5148 | rt = ip6_route_info_create(&r_cfg, GFP_KERNEL, extack); |
Roopa Prabhu | 8c5b83f | 2015-10-10 08:26:36 -0700 | [diff] [blame] | 5149 | if (IS_ERR(rt)) { |
| 5150 | err = PTR_ERR(rt); |
| 5151 | rt = NULL; |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 5152 | goto cleanup; |
Roopa Prabhu | 8c5b83f | 2015-10-10 08:26:36 -0700 | [diff] [blame] | 5153 | } |
David Ahern | b5d2d75 | 2018-07-15 09:35:19 -0700 | [diff] [blame] | 5154 | if (!rt6_qualify_for_ecmp(rt)) { |
| 5155 | err = -EINVAL; |
| 5156 | NL_SET_ERR_MSG(extack, |
| 5157 | "Device only routes can not be added for IPv6 using the multipath API."); |
| 5158 | fib6_info_release(rt); |
| 5159 | goto cleanup; |
| 5160 | } |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 5161 | |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 5162 | rt->fib6_nh->fib_nh_weight = rtnh->rtnh_hops + 1; |
Ido Schimmel | 398958a | 2018-01-09 16:40:28 +0200 | [diff] [blame] | 5163 | |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 5164 | err = ip6_route_info_append(info->nl_net, &rt6_nh_list, |
| 5165 | rt, &r_cfg); |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 5166 | if (err) { |
David Ahern | 93531c6 | 2018-04-17 17:33:25 -0700 | [diff] [blame] | 5167 | fib6_info_release(rt); |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 5168 | goto cleanup; |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 5169 | } |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 5170 | |
| 5171 | rtnh = rtnh_next(rtnh, &remaining); |
| 5172 | } |
| 5173 | |
Ido Schimmel | 9eee3b4 | 2019-06-20 12:10:21 +0300 | [diff] [blame] | 5174 | if (list_empty(&rt6_nh_list)) { |
| 5175 | NL_SET_ERR_MSG(extack, |
| 5176 | "Invalid nexthop configuration - no valid nexthops"); |
| 5177 | return -EINVAL; |
| 5178 | } |
| 5179 | |
David Ahern | 3b1137f | 2017-02-02 12:37:10 -0800 | [diff] [blame] | 5180 | /* for add and replace send one notification with all nexthops. |
| 5181 | * Skip the notification in fib6_add_rt2node and send one with |
| 5182 | * the full route when done |
| 5183 | */ |
| 5184 | info->skip_notify = 1; |
| 5185 | |
Ido Schimmel | ebee3ca | 2019-06-18 18:12:48 +0300 | [diff] [blame] | 5186 | /* For add and replace, send one notification with all nexthops. For |
| 5187 | * append, send one notification with all appended nexthops. |
| 5188 | */ |
| 5189 | info->skip_notify_kernel = 1; |
| 5190 | |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 5191 | err_nh = NULL; |
| 5192 | list_for_each_entry(nh, &rt6_nh_list, next) { |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 5193 | err = __ip6_ins_rt(nh->fib6_info, info, extack); |
| 5194 | fib6_info_release(nh->fib6_info); |
David Ahern | 3b1137f | 2017-02-02 12:37:10 -0800 | [diff] [blame] | 5195 | |
David Ahern | f722517 | 2018-06-04 13:41:42 -0700 | [diff] [blame] | 5196 | if (!err) { |
| 5197 | /* save reference to last route successfully inserted */ |
| 5198 | rt_last = nh->fib6_info; |
| 5199 | |
| 5200 | /* save reference to first route for notification */ |
| 5201 | if (!rt_notif) |
| 5202 | rt_notif = nh->fib6_info; |
| 5203 | } |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 5204 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 5205 | /* nh->fib6_info is used or freed at this point, reset to NULL*/ |
| 5206 | nh->fib6_info = NULL; |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 5207 | if (err) { |
| 5208 | if (replace && nhn) |
Jakub Kicinski | a5a82d8 | 2019-01-14 10:52:45 -0800 | [diff] [blame] | 5209 | NL_SET_ERR_MSG_MOD(extack, |
| 5210 | "multipath route replace failed (check consistency of installed routes)"); |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 5211 | err_nh = nh; |
| 5212 | goto add_errout; |
| 5213 | } |
| 5214 | |
Nicolas Dichtel | 1a72418 | 2012-11-01 22:58:22 +0000 | [diff] [blame] | 5215 | /* Because each route is added like a single route we remove |
Michal Kubeček | 2759647 | 2015-05-18 20:54:00 +0200 | [diff] [blame] | 5216 | * these flags after the first nexthop: if there is a collision, |
| 5217 | * we have already failed to add the first nexthop: |
| 5218 | * fib6_add_rt2node() has rejected it; when replacing, old |
| 5219 | * nexthops have been replaced by first new, the rest should |
| 5220 | * be added to it. |
Nicolas Dichtel | 1a72418 | 2012-11-01 22:58:22 +0000 | [diff] [blame] | 5221 | */ |
Michal Kubeček | 2759647 | 2015-05-18 20:54:00 +0200 | [diff] [blame] | 5222 | cfg->fc_nlinfo.nlh->nlmsg_flags &= ~(NLM_F_EXCL | |
| 5223 | NLM_F_REPLACE); |
Benjamin Poirier | afecdb3 | 2020-02-12 10:41:07 +0900 | [diff] [blame] | 5224 | cfg->fc_nlinfo.nlh->nlmsg_flags |= NLM_F_CREATE; |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 5225 | nhn++; |
| 5226 | } |
| 5227 | |
Ido Schimmel | 0ee0f47 | 2019-12-23 15:28:15 +0200 | [diff] [blame] | 5228 | /* An in-kernel notification should only be sent in case the new |
| 5229 | * multipath route is added as the first route in the node, or if |
| 5230 | * it was appended to it. We pass 'rt_notif' since it is the first |
| 5231 | * sibling and might allow us to skip some checks in the replace case. |
| 5232 | */ |
| 5233 | if (ip6_route_mpath_should_notify(rt_notif)) { |
| 5234 | enum fib_event_type fib_event; |
| 5235 | |
| 5236 | if (rt_notif->fib6_nsiblings != nhn - 1) |
| 5237 | fib_event = FIB_EVENT_ENTRY_APPEND; |
| 5238 | else |
Ido Schimmel | caafb25 | 2019-12-23 15:28:20 +0200 | [diff] [blame] | 5239 | fib_event = FIB_EVENT_ENTRY_REPLACE; |
Ido Schimmel | 0ee0f47 | 2019-12-23 15:28:15 +0200 | [diff] [blame] | 5240 | |
| 5241 | err = call_fib6_multipath_entry_notifiers(info->nl_net, |
| 5242 | fib_event, rt_notif, |
| 5243 | nhn - 1, extack); |
| 5244 | if (err) { |
| 5245 | /* Delete all the siblings that were just added */ |
| 5246 | err_nh = NULL; |
| 5247 | goto add_errout; |
| 5248 | } |
| 5249 | } |
Ido Schimmel | ebee3ca | 2019-06-18 18:12:48 +0300 | [diff] [blame] | 5250 | |
David Ahern | 3b1137f | 2017-02-02 12:37:10 -0800 | [diff] [blame] | 5251 | /* success ... tell user about new route */ |
| 5252 | ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags); |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 5253 | goto cleanup; |
| 5254 | |
| 5255 | add_errout: |
David Ahern | 3b1137f | 2017-02-02 12:37:10 -0800 | [diff] [blame] | 5256 | /* send notification for routes that were added so that |
| 5257 | * the delete notifications sent by ip6_route_del are |
| 5258 | * coherent |
| 5259 | */ |
| 5260 | if (rt_notif) |
| 5261 | ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags); |
| 5262 | |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 5263 | /* Delete routes that were already added */ |
| 5264 | list_for_each_entry(nh, &rt6_nh_list, next) { |
| 5265 | if (err_nh == nh) |
| 5266 | break; |
David Ahern | 333c430 | 2017-05-21 10:12:04 -0600 | [diff] [blame] | 5267 | ip6_route_del(&nh->r_cfg, extack); |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 5268 | } |
| 5269 | |
| 5270 | cleanup: |
| 5271 | list_for_each_entry_safe(nh, nh_safe, &rt6_nh_list, next) { |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 5272 | if (nh->fib6_info) |
| 5273 | fib6_info_release(nh->fib6_info); |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 5274 | list_del(&nh->next); |
| 5275 | kfree(nh); |
| 5276 | } |
| 5277 | |
| 5278 | return err; |
| 5279 | } |
| 5280 | |
David Ahern | 333c430 | 2017-05-21 10:12:04 -0600 | [diff] [blame] | 5281 | static int ip6_route_multipath_del(struct fib6_config *cfg, |
| 5282 | struct netlink_ext_ack *extack) |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 5283 | { |
| 5284 | struct fib6_config r_cfg; |
| 5285 | struct rtnexthop *rtnh; |
| 5286 | int remaining; |
| 5287 | int attrlen; |
| 5288 | int err = 1, last_err = 0; |
| 5289 | |
| 5290 | remaining = cfg->fc_mp_len; |
| 5291 | rtnh = (struct rtnexthop *)cfg->fc_mp; |
| 5292 | |
| 5293 | /* Parse a Multipath Entry */ |
| 5294 | while (rtnh_ok(rtnh, remaining)) { |
| 5295 | memcpy(&r_cfg, cfg, sizeof(*cfg)); |
| 5296 | if (rtnh->rtnh_ifindex) |
| 5297 | r_cfg.fc_ifindex = rtnh->rtnh_ifindex; |
| 5298 | |
| 5299 | attrlen = rtnh_attrlen(rtnh); |
| 5300 | if (attrlen > 0) { |
| 5301 | struct nlattr *nla, *attrs = rtnh_attrs(rtnh); |
| 5302 | |
| 5303 | nla = nla_find(attrs, attrlen, RTA_GATEWAY); |
| 5304 | if (nla) { |
| 5305 | nla_memcpy(&r_cfg.fc_gateway, nla, 16); |
| 5306 | r_cfg.fc_flags |= RTF_GATEWAY; |
| 5307 | } |
| 5308 | } |
David Ahern | 333c430 | 2017-05-21 10:12:04 -0600 | [diff] [blame] | 5309 | err = ip6_route_del(&r_cfg, extack); |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 5310 | if (err) |
| 5311 | last_err = err; |
| 5312 | |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 5313 | rtnh = rtnh_next(rtnh, &remaining); |
| 5314 | } |
| 5315 | |
| 5316 | return last_err; |
| 5317 | } |
| 5318 | |
David Ahern | c21ef3e | 2017-04-16 09:48:24 -0700 | [diff] [blame] | 5319 | static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh, |
| 5320 | struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5321 | { |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 5322 | struct fib6_config cfg; |
| 5323 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5324 | |
David Ahern | 333c430 | 2017-05-21 10:12:04 -0600 | [diff] [blame] | 5325 | err = rtm_to_fib6_config(skb, nlh, &cfg, extack); |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 5326 | if (err < 0) |
| 5327 | return err; |
| 5328 | |
David Ahern | 5b98324 | 2019-06-08 14:53:34 -0700 | [diff] [blame] | 5329 | if (cfg.fc_nh_id && |
| 5330 | !nexthop_find_by_id(sock_net(skb->sk), cfg.fc_nh_id)) { |
| 5331 | NL_SET_ERR_MSG(extack, "Nexthop id does not exist"); |
| 5332 | return -EINVAL; |
| 5333 | } |
| 5334 | |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 5335 | if (cfg.fc_mp) |
David Ahern | 333c430 | 2017-05-21 10:12:04 -0600 | [diff] [blame] | 5336 | return ip6_route_multipath_del(&cfg, extack); |
David Ahern | 0ae8133 | 2017-02-02 12:37:08 -0800 | [diff] [blame] | 5337 | else { |
| 5338 | cfg.fc_delete_all_nh = 1; |
David Ahern | 333c430 | 2017-05-21 10:12:04 -0600 | [diff] [blame] | 5339 | return ip6_route_del(&cfg, extack); |
David Ahern | 0ae8133 | 2017-02-02 12:37:08 -0800 | [diff] [blame] | 5340 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5341 | } |
| 5342 | |
David Ahern | c21ef3e | 2017-04-16 09:48:24 -0700 | [diff] [blame] | 5343 | static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh, |
| 5344 | struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5345 | { |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 5346 | struct fib6_config cfg; |
| 5347 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5348 | |
David Ahern | 333c430 | 2017-05-21 10:12:04 -0600 | [diff] [blame] | 5349 | err = rtm_to_fib6_config(skb, nlh, &cfg, extack); |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 5350 | if (err < 0) |
| 5351 | return err; |
| 5352 | |
David Ahern | 67f6951 | 2019-03-21 05:21:34 -0700 | [diff] [blame] | 5353 | if (cfg.fc_metric == 0) |
| 5354 | cfg.fc_metric = IP6_RT_PRIO_USER; |
| 5355 | |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 5356 | if (cfg.fc_mp) |
David Ahern | 333c430 | 2017-05-21 10:12:04 -0600 | [diff] [blame] | 5357 | return ip6_route_multipath_add(&cfg, extack); |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 5358 | else |
David Ahern | acb54e3 | 2018-04-17 17:33:22 -0700 | [diff] [blame] | 5359 | return ip6_route_add(&cfg, GFP_KERNEL, extack); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5360 | } |
| 5361 | |
David Ahern | a1b7a1f | 2019-06-08 14:53:26 -0700 | [diff] [blame] | 5362 | /* add the overhead of this fib6_nh to nexthop_len */ |
| 5363 | static int rt6_nh_nlmsg_size(struct fib6_nh *nh, void *arg) |
Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 5364 | { |
David Ahern | a1b7a1f | 2019-06-08 14:53:26 -0700 | [diff] [blame] | 5365 | int *nexthop_len = arg; |
David Ahern | beb1afac5 | 2017-02-02 12:37:09 -0800 | [diff] [blame] | 5366 | |
David Ahern | a1b7a1f | 2019-06-08 14:53:26 -0700 | [diff] [blame] | 5367 | *nexthop_len += nla_total_size(0) /* RTA_MULTIPATH */ |
| 5368 | + NLA_ALIGN(sizeof(struct rtnexthop)) |
| 5369 | + nla_total_size(16); /* RTA_GATEWAY */ |
David Ahern | f88d8ea | 2019-06-03 20:19:52 -0700 | [diff] [blame] | 5370 | |
David Ahern | a1b7a1f | 2019-06-08 14:53:26 -0700 | [diff] [blame] | 5371 | if (nh->fib_nh_lws) { |
| 5372 | /* RTA_ENCAP_TYPE */ |
| 5373 | *nexthop_len += lwtunnel_get_encap_size(nh->fib_nh_lws); |
| 5374 | /* RTA_ENCAP */ |
| 5375 | *nexthop_len += nla_total_size(2); |
| 5376 | } |
David Ahern | beb1afac5 | 2017-02-02 12:37:09 -0800 | [diff] [blame] | 5377 | |
David Ahern | a1b7a1f | 2019-06-08 14:53:26 -0700 | [diff] [blame] | 5378 | return 0; |
| 5379 | } |
| 5380 | |
| 5381 | static size_t rt6_nlmsg_size(struct fib6_info *f6i) |
| 5382 | { |
| 5383 | int nexthop_len; |
| 5384 | |
| 5385 | if (f6i->nh) { |
| 5386 | nexthop_len = nla_total_size(4); /* RTA_NH_ID */ |
| 5387 | nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_nlmsg_size, |
| 5388 | &nexthop_len); |
| 5389 | } else { |
| 5390 | struct fib6_nh *nh = f6i->fib6_nh; |
| 5391 | |
| 5392 | nexthop_len = 0; |
| 5393 | if (f6i->fib6_nsiblings) { |
| 5394 | nexthop_len = nla_total_size(0) /* RTA_MULTIPATH */ |
| 5395 | + NLA_ALIGN(sizeof(struct rtnexthop)) |
| 5396 | + nla_total_size(16) /* RTA_GATEWAY */ |
| 5397 | + lwtunnel_get_encap_size(nh->fib_nh_lws); |
| 5398 | |
| 5399 | nexthop_len *= f6i->fib6_nsiblings; |
| 5400 | } |
| 5401 | nexthop_len += lwtunnel_get_encap_size(nh->fib_nh_lws); |
David Ahern | beb1afac5 | 2017-02-02 12:37:09 -0800 | [diff] [blame] | 5402 | } |
| 5403 | |
Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 5404 | return NLMSG_ALIGN(sizeof(struct rtmsg)) |
| 5405 | + nla_total_size(16) /* RTA_SRC */ |
| 5406 | + nla_total_size(16) /* RTA_DST */ |
| 5407 | + nla_total_size(16) /* RTA_GATEWAY */ |
| 5408 | + nla_total_size(16) /* RTA_PREFSRC */ |
| 5409 | + nla_total_size(4) /* RTA_TABLE */ |
| 5410 | + nla_total_size(4) /* RTA_IIF */ |
| 5411 | + nla_total_size(4) /* RTA_OIF */ |
| 5412 | + nla_total_size(4) /* RTA_PRIORITY */ |
Noriaki TAKAMIYA | 6a2b9ce | 2007-01-23 22:09:41 -0800 | [diff] [blame] | 5413 | + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */ |
Daniel Borkmann | ea69763 | 2015-01-05 23:57:47 +0100 | [diff] [blame] | 5414 | + nla_total_size(sizeof(struct rta_cacheinfo)) |
Lubomir Rintel | c78ba6d | 2015-03-11 15:39:21 +0100 | [diff] [blame] | 5415 | + nla_total_size(TCP_CA_NAME_MAX) /* RTAX_CC_ALGO */ |
Roopa Prabhu | 19e42e4 | 2015-07-21 10:43:48 +0200 | [diff] [blame] | 5416 | + nla_total_size(1) /* RTA_PREF */ |
David Ahern | beb1afac5 | 2017-02-02 12:37:09 -0800 | [diff] [blame] | 5417 | + nexthop_len; |
| 5418 | } |
| 5419 | |
David Ahern | f88d8ea | 2019-06-03 20:19:52 -0700 | [diff] [blame] | 5420 | static int rt6_fill_node_nexthop(struct sk_buff *skb, struct nexthop *nh, |
| 5421 | unsigned char *flags) |
| 5422 | { |
| 5423 | if (nexthop_is_multipath(nh)) { |
| 5424 | struct nlattr *mp; |
| 5425 | |
David Ahern | 4255ff0 | 2019-09-03 15:22:12 -0700 | [diff] [blame] | 5426 | mp = nla_nest_start_noflag(skb, RTA_MULTIPATH); |
David Ahern | f88d8ea | 2019-06-03 20:19:52 -0700 | [diff] [blame] | 5427 | if (!mp) |
| 5428 | goto nla_put_failure; |
| 5429 | |
Donald Sharp | 7bdf4de | 2019-09-04 10:11:58 -0400 | [diff] [blame] | 5430 | if (nexthop_mpath_fill_node(skb, nh, AF_INET6)) |
David Ahern | f88d8ea | 2019-06-03 20:19:52 -0700 | [diff] [blame] | 5431 | goto nla_put_failure; |
| 5432 | |
| 5433 | nla_nest_end(skb, mp); |
| 5434 | } else { |
| 5435 | struct fib6_nh *fib6_nh; |
| 5436 | |
| 5437 | fib6_nh = nexthop_fib6_nh(nh); |
Donald Sharp | 7bdf4de | 2019-09-04 10:11:58 -0400 | [diff] [blame] | 5438 | if (fib_nexthop_info(skb, &fib6_nh->nh_common, AF_INET6, |
David Ahern | f88d8ea | 2019-06-03 20:19:52 -0700 | [diff] [blame] | 5439 | flags, false) < 0) |
| 5440 | goto nla_put_failure; |
| 5441 | } |
| 5442 | |
| 5443 | return 0; |
| 5444 | |
| 5445 | nla_put_failure: |
| 5446 | return -EMSGSIZE; |
| 5447 | } |
| 5448 | |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 5449 | static int rt6_fill_node(struct net *net, struct sk_buff *skb, |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 5450 | struct fib6_info *rt, struct dst_entry *dst, |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 5451 | struct in6_addr *dest, struct in6_addr *src, |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 5452 | int iif, int type, u32 portid, u32 seq, |
David Ahern | f8cfe2c | 2017-01-17 15:51:08 -0800 | [diff] [blame] | 5453 | unsigned int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5454 | { |
Xin Long | 22d0bd8 | 2018-09-11 14:33:58 +0800 | [diff] [blame] | 5455 | struct rt6_info *rt6 = (struct rt6_info *)dst; |
| 5456 | struct rt6key *rt6_dst, *rt6_src; |
| 5457 | u32 *pmetrics, table, rt6_flags; |
David Ahern | f88d8ea | 2019-06-03 20:19:52 -0700 | [diff] [blame] | 5458 | unsigned char nh_flags = 0; |
Thomas Graf | 2d7202b | 2006-08-22 00:01:27 -0700 | [diff] [blame] | 5459 | struct nlmsghdr *nlh; |
Xin Long | 22d0bd8 | 2018-09-11 14:33:58 +0800 | [diff] [blame] | 5460 | struct rtmsg *rtm; |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 5461 | long expires = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5462 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 5463 | nlh = nlmsg_put(skb, portid, seq, type, sizeof(*rtm), flags); |
David S. Miller | 3830847 | 2011-12-03 18:02:47 -0500 | [diff] [blame] | 5464 | if (!nlh) |
Patrick McHardy | 2693256 | 2007-01-31 23:16:40 -0800 | [diff] [blame] | 5465 | return -EMSGSIZE; |
Thomas Graf | 2d7202b | 2006-08-22 00:01:27 -0700 | [diff] [blame] | 5466 | |
Xin Long | 22d0bd8 | 2018-09-11 14:33:58 +0800 | [diff] [blame] | 5467 | if (rt6) { |
| 5468 | rt6_dst = &rt6->rt6i_dst; |
| 5469 | rt6_src = &rt6->rt6i_src; |
| 5470 | rt6_flags = rt6->rt6i_flags; |
| 5471 | } else { |
| 5472 | rt6_dst = &rt->fib6_dst; |
| 5473 | rt6_src = &rt->fib6_src; |
| 5474 | rt6_flags = rt->fib6_flags; |
| 5475 | } |
| 5476 | |
Thomas Graf | 2d7202b | 2006-08-22 00:01:27 -0700 | [diff] [blame] | 5477 | rtm = nlmsg_data(nlh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5478 | rtm->rtm_family = AF_INET6; |
Xin Long | 22d0bd8 | 2018-09-11 14:33:58 +0800 | [diff] [blame] | 5479 | rtm->rtm_dst_len = rt6_dst->plen; |
| 5480 | rtm->rtm_src_len = rt6_src->plen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5481 | rtm->rtm_tos = 0; |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 5482 | if (rt->fib6_table) |
| 5483 | table = rt->fib6_table->tb6_id; |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 5484 | else |
Patrick McHardy | 9e762a4 | 2006-08-10 23:09:48 -0700 | [diff] [blame] | 5485 | table = RT6_TABLE_UNSPEC; |
Kalash Nainwal | 97f0082 | 2019-02-20 16:23:04 -0800 | [diff] [blame] | 5486 | rtm->rtm_table = table < 256 ? table : RT_TABLE_COMPAT; |
David S. Miller | c78679e | 2012-04-01 20:27:33 -0400 | [diff] [blame] | 5487 | if (nla_put_u32(skb, RTA_TABLE, table)) |
| 5488 | goto nla_put_failure; |
David Ahern | e8478e8 | 2018-04-17 17:33:13 -0700 | [diff] [blame] | 5489 | |
| 5490 | rtm->rtm_type = rt->fib6_type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5491 | rtm->rtm_flags = 0; |
| 5492 | rtm->rtm_scope = RT_SCOPE_UNIVERSE; |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 5493 | rtm->rtm_protocol = rt->fib6_protocol; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5494 | |
Xin Long | 22d0bd8 | 2018-09-11 14:33:58 +0800 | [diff] [blame] | 5495 | if (rt6_flags & RTF_CACHE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5496 | rtm->rtm_flags |= RTM_F_CLONED; |
| 5497 | |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 5498 | if (dest) { |
| 5499 | if (nla_put_in6_addr(skb, RTA_DST, dest)) |
David S. Miller | c78679e | 2012-04-01 20:27:33 -0400 | [diff] [blame] | 5500 | goto nla_put_failure; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 5501 | rtm->rtm_dst_len = 128; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5502 | } else if (rtm->rtm_dst_len) |
Xin Long | 22d0bd8 | 2018-09-11 14:33:58 +0800 | [diff] [blame] | 5503 | if (nla_put_in6_addr(skb, RTA_DST, &rt6_dst->addr)) |
David S. Miller | c78679e | 2012-04-01 20:27:33 -0400 | [diff] [blame] | 5504 | goto nla_put_failure; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5505 | #ifdef CONFIG_IPV6_SUBTREES |
| 5506 | if (src) { |
Jiri Benc | 930345e | 2015-03-29 16:59:25 +0200 | [diff] [blame] | 5507 | if (nla_put_in6_addr(skb, RTA_SRC, src)) |
David S. Miller | c78679e | 2012-04-01 20:27:33 -0400 | [diff] [blame] | 5508 | goto nla_put_failure; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 5509 | rtm->rtm_src_len = 128; |
David S. Miller | c78679e | 2012-04-01 20:27:33 -0400 | [diff] [blame] | 5510 | } else if (rtm->rtm_src_len && |
Xin Long | 22d0bd8 | 2018-09-11 14:33:58 +0800 | [diff] [blame] | 5511 | nla_put_in6_addr(skb, RTA_SRC, &rt6_src->addr)) |
David S. Miller | c78679e | 2012-04-01 20:27:33 -0400 | [diff] [blame] | 5512 | goto nla_put_failure; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5513 | #endif |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 5514 | if (iif) { |
| 5515 | #ifdef CONFIG_IPV6_MROUTE |
Xin Long | 22d0bd8 | 2018-09-11 14:33:58 +0800 | [diff] [blame] | 5516 | if (ipv6_addr_is_multicast(&rt6_dst->addr)) { |
David Ahern | fd61c6b | 2017-01-17 15:51:07 -0800 | [diff] [blame] | 5517 | int err = ip6mr_get_route(net, skb, rtm, portid); |
Nikolay Aleksandrov | 2cf7507 | 2016-09-25 23:08:31 +0200 | [diff] [blame] | 5518 | |
David Ahern | fd61c6b | 2017-01-17 15:51:07 -0800 | [diff] [blame] | 5519 | if (err == 0) |
| 5520 | return 0; |
| 5521 | if (err < 0) |
| 5522 | goto nla_put_failure; |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 5523 | } else |
| 5524 | #endif |
David S. Miller | c78679e | 2012-04-01 20:27:33 -0400 | [diff] [blame] | 5525 | if (nla_put_u32(skb, RTA_IIF, iif)) |
| 5526 | goto nla_put_failure; |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 5527 | } else if (dest) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5528 | struct in6_addr saddr_buf; |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 5529 | if (ip6_route_get_saddr(net, rt, dest, 0, &saddr_buf) == 0 && |
Jiri Benc | 930345e | 2015-03-29 16:59:25 +0200 | [diff] [blame] | 5530 | nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf)) |
David S. Miller | c78679e | 2012-04-01 20:27:33 -0400 | [diff] [blame] | 5531 | goto nla_put_failure; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5532 | } |
Thomas Graf | 2d7202b | 2006-08-22 00:01:27 -0700 | [diff] [blame] | 5533 | |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 5534 | if (rt->fib6_prefsrc.plen) { |
Daniel Walter | c3968a8 | 2011-04-13 21:10:57 +0000 | [diff] [blame] | 5535 | struct in6_addr saddr_buf; |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 5536 | saddr_buf = rt->fib6_prefsrc.addr; |
Jiri Benc | 930345e | 2015-03-29 16:59:25 +0200 | [diff] [blame] | 5537 | if (nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf)) |
David S. Miller | c78679e | 2012-04-01 20:27:33 -0400 | [diff] [blame] | 5538 | goto nla_put_failure; |
Daniel Walter | c3968a8 | 2011-04-13 21:10:57 +0000 | [diff] [blame] | 5539 | } |
| 5540 | |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 5541 | pmetrics = dst ? dst_metrics_ptr(dst) : rt->fib6_metrics->metrics; |
| 5542 | if (rtnetlink_put_metrics(skb, pmetrics) < 0) |
Thomas Graf | 2d7202b | 2006-08-22 00:01:27 -0700 | [diff] [blame] | 5543 | goto nla_put_failure; |
| 5544 | |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 5545 | if (nla_put_u32(skb, RTA_PRIORITY, rt->fib6_metric)) |
David S. Miller | c78679e | 2012-04-01 20:27:33 -0400 | [diff] [blame] | 5546 | goto nla_put_failure; |
Li Wei | 8253947 | 2012-07-29 16:01:30 +0000 | [diff] [blame] | 5547 | |
David Ahern | beb1afac5 | 2017-02-02 12:37:09 -0800 | [diff] [blame] | 5548 | /* For multipath routes, walk the siblings list and add |
| 5549 | * each as a nexthop within RTA_MULTIPATH. |
| 5550 | */ |
Xin Long | 22d0bd8 | 2018-09-11 14:33:58 +0800 | [diff] [blame] | 5551 | if (rt6) { |
| 5552 | if (rt6_flags & RTF_GATEWAY && |
| 5553 | nla_put_in6_addr(skb, RTA_GATEWAY, &rt6->rt6i_gateway)) |
| 5554 | goto nla_put_failure; |
| 5555 | |
| 5556 | if (dst->dev && nla_put_u32(skb, RTA_OIF, dst->dev->ifindex)) |
| 5557 | goto nla_put_failure; |
| 5558 | } else if (rt->fib6_nsiblings) { |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 5559 | struct fib6_info *sibling, *next_sibling; |
David Ahern | beb1afac5 | 2017-02-02 12:37:09 -0800 | [diff] [blame] | 5560 | struct nlattr *mp; |
| 5561 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 5562 | mp = nla_nest_start_noflag(skb, RTA_MULTIPATH); |
David Ahern | beb1afac5 | 2017-02-02 12:37:09 -0800 | [diff] [blame] | 5563 | if (!mp) |
| 5564 | goto nla_put_failure; |
| 5565 | |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 5566 | if (fib_add_nexthop(skb, &rt->fib6_nh->nh_common, |
Donald Sharp | 7bdf4de | 2019-09-04 10:11:58 -0400 | [diff] [blame] | 5567 | rt->fib6_nh->fib_nh_weight, AF_INET6) < 0) |
David Ahern | beb1afac5 | 2017-02-02 12:37:09 -0800 | [diff] [blame] | 5568 | goto nla_put_failure; |
| 5569 | |
| 5570 | list_for_each_entry_safe(sibling, next_sibling, |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 5571 | &rt->fib6_siblings, fib6_siblings) { |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 5572 | if (fib_add_nexthop(skb, &sibling->fib6_nh->nh_common, |
Donald Sharp | 7bdf4de | 2019-09-04 10:11:58 -0400 | [diff] [blame] | 5573 | sibling->fib6_nh->fib_nh_weight, |
| 5574 | AF_INET6) < 0) |
David Ahern | beb1afac5 | 2017-02-02 12:37:09 -0800 | [diff] [blame] | 5575 | goto nla_put_failure; |
| 5576 | } |
| 5577 | |
| 5578 | nla_nest_end(skb, mp); |
David Ahern | f88d8ea | 2019-06-03 20:19:52 -0700 | [diff] [blame] | 5579 | } else if (rt->nh) { |
| 5580 | if (nla_put_u32(skb, RTA_NH_ID, rt->nh->id)) |
| 5581 | goto nla_put_failure; |
David Ahern | ecc5663 | 2019-04-23 08:48:09 -0700 | [diff] [blame] | 5582 | |
David Ahern | f88d8ea | 2019-06-03 20:19:52 -0700 | [diff] [blame] | 5583 | if (nexthop_is_blackhole(rt->nh)) |
| 5584 | rtm->rtm_type = RTN_BLACKHOLE; |
| 5585 | |
Roopa Prabhu | 4f80116 | 2020-04-27 13:56:46 -0700 | [diff] [blame] | 5586 | if (net->ipv4.sysctl_nexthop_compat_mode && |
| 5587 | rt6_fill_node_nexthop(skb, rt->nh, &nh_flags) < 0) |
David Ahern | f88d8ea | 2019-06-03 20:19:52 -0700 | [diff] [blame] | 5588 | goto nla_put_failure; |
| 5589 | |
| 5590 | rtm->rtm_flags |= nh_flags; |
| 5591 | } else { |
Donald Sharp | 7bdf4de | 2019-09-04 10:11:58 -0400 | [diff] [blame] | 5592 | if (fib_nexthop_info(skb, &rt->fib6_nh->nh_common, AF_INET6, |
David Ahern | ecc5663 | 2019-04-23 08:48:09 -0700 | [diff] [blame] | 5593 | &nh_flags, false) < 0) |
David Ahern | beb1afac5 | 2017-02-02 12:37:09 -0800 | [diff] [blame] | 5594 | goto nla_put_failure; |
David Ahern | ecc5663 | 2019-04-23 08:48:09 -0700 | [diff] [blame] | 5595 | |
| 5596 | rtm->rtm_flags |= nh_flags; |
David Ahern | beb1afac5 | 2017-02-02 12:37:09 -0800 | [diff] [blame] | 5597 | } |
| 5598 | |
Xin Long | 22d0bd8 | 2018-09-11 14:33:58 +0800 | [diff] [blame] | 5599 | if (rt6_flags & RTF_EXPIRES) { |
David Ahern | 1489568 | 2018-04-17 17:33:17 -0700 | [diff] [blame] | 5600 | expires = dst ? dst->expires : rt->expires; |
| 5601 | expires -= jiffies; |
| 5602 | } |
YOSHIFUJI Hideaki | 69cdf8f | 2008-05-19 16:55:13 -0700 | [diff] [blame] | 5603 | |
Ido Schimmel | bb3c4ab | 2020-01-14 13:23:12 +0200 | [diff] [blame] | 5604 | if (!dst) { |
| 5605 | if (rt->offload) |
| 5606 | rtm->rtm_flags |= RTM_F_OFFLOAD; |
| 5607 | if (rt->trap) |
| 5608 | rtm->rtm_flags |= RTM_F_TRAP; |
| 5609 | } |
| 5610 | |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 5611 | if (rtnl_put_cacheinfo(skb, dst, 0, expires, dst ? dst->error : 0) < 0) |
Thomas Graf | e3703b3 | 2006-11-27 09:27:07 -0800 | [diff] [blame] | 5612 | goto nla_put_failure; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5613 | |
Xin Long | 22d0bd8 | 2018-09-11 14:33:58 +0800 | [diff] [blame] | 5614 | if (nla_put_u8(skb, RTA_PREF, IPV6_EXTRACT_PREF(rt6_flags))) |
Lubomir Rintel | c78ba6d | 2015-03-11 15:39:21 +0100 | [diff] [blame] | 5615 | goto nla_put_failure; |
| 5616 | |
Roopa Prabhu | 19e42e4 | 2015-07-21 10:43:48 +0200 | [diff] [blame] | 5617 | |
Johannes Berg | 053c095 | 2015-01-16 22:09:00 +0100 | [diff] [blame] | 5618 | nlmsg_end(skb, nlh); |
| 5619 | return 0; |
Thomas Graf | 2d7202b | 2006-08-22 00:01:27 -0700 | [diff] [blame] | 5620 | |
| 5621 | nla_put_failure: |
Patrick McHardy | 2693256 | 2007-01-31 23:16:40 -0800 | [diff] [blame] | 5622 | nlmsg_cancel(skb, nlh); |
| 5623 | return -EMSGSIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5624 | } |
| 5625 | |
David Ahern | 2c170e0 | 2019-06-08 14:53:27 -0700 | [diff] [blame] | 5626 | static int fib6_info_nh_uses_dev(struct fib6_nh *nh, void *arg) |
| 5627 | { |
| 5628 | const struct net_device *dev = arg; |
| 5629 | |
| 5630 | if (nh->fib_nh_dev == dev) |
| 5631 | return 1; |
| 5632 | |
| 5633 | return 0; |
| 5634 | } |
| 5635 | |
David Ahern | 13e3890 | 2018-10-15 18:56:44 -0700 | [diff] [blame] | 5636 | static bool fib6_info_uses_dev(const struct fib6_info *f6i, |
| 5637 | const struct net_device *dev) |
| 5638 | { |
David Ahern | 2c170e0 | 2019-06-08 14:53:27 -0700 | [diff] [blame] | 5639 | if (f6i->nh) { |
| 5640 | struct net_device *_dev = (struct net_device *)dev; |
| 5641 | |
| 5642 | return !!nexthop_for_each_fib6_nh(f6i->nh, |
| 5643 | fib6_info_nh_uses_dev, |
| 5644 | _dev); |
| 5645 | } |
| 5646 | |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 5647 | if (f6i->fib6_nh->fib_nh_dev == dev) |
David Ahern | 13e3890 | 2018-10-15 18:56:44 -0700 | [diff] [blame] | 5648 | return true; |
| 5649 | |
| 5650 | if (f6i->fib6_nsiblings) { |
| 5651 | struct fib6_info *sibling, *next_sibling; |
| 5652 | |
| 5653 | list_for_each_entry_safe(sibling, next_sibling, |
| 5654 | &f6i->fib6_siblings, fib6_siblings) { |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 5655 | if (sibling->fib6_nh->fib_nh_dev == dev) |
David Ahern | 13e3890 | 2018-10-15 18:56:44 -0700 | [diff] [blame] | 5656 | return true; |
| 5657 | } |
| 5658 | } |
| 5659 | |
| 5660 | return false; |
| 5661 | } |
| 5662 | |
Stefano Brivio | 1e47b48 | 2019-06-21 17:45:27 +0200 | [diff] [blame] | 5663 | struct fib6_nh_exception_dump_walker { |
| 5664 | struct rt6_rtnl_dump_arg *dump; |
| 5665 | struct fib6_info *rt; |
| 5666 | unsigned int flags; |
| 5667 | unsigned int skip; |
| 5668 | unsigned int count; |
| 5669 | }; |
| 5670 | |
| 5671 | static int rt6_nh_dump_exceptions(struct fib6_nh *nh, void *arg) |
| 5672 | { |
| 5673 | struct fib6_nh_exception_dump_walker *w = arg; |
| 5674 | struct rt6_rtnl_dump_arg *dump = w->dump; |
| 5675 | struct rt6_exception_bucket *bucket; |
| 5676 | struct rt6_exception *rt6_ex; |
| 5677 | int i, err; |
| 5678 | |
| 5679 | bucket = fib6_nh_get_excptn_bucket(nh, NULL); |
| 5680 | if (!bucket) |
| 5681 | return 0; |
| 5682 | |
| 5683 | for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) { |
| 5684 | hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) { |
| 5685 | if (w->skip) { |
| 5686 | w->skip--; |
| 5687 | continue; |
| 5688 | } |
| 5689 | |
| 5690 | /* Expiration of entries doesn't bump sernum, insertion |
| 5691 | * does. Removal is triggered by insertion, so we can |
| 5692 | * rely on the fact that if entries change between two |
| 5693 | * partial dumps, this node is scanned again completely, |
| 5694 | * see rt6_insert_exception() and fib6_dump_table(). |
| 5695 | * |
| 5696 | * Count expired entries we go through as handled |
| 5697 | * entries that we'll skip next time, in case of partial |
| 5698 | * node dump. Otherwise, if entries expire meanwhile, |
| 5699 | * we'll skip the wrong amount. |
| 5700 | */ |
| 5701 | if (rt6_check_expired(rt6_ex->rt6i)) { |
| 5702 | w->count++; |
| 5703 | continue; |
| 5704 | } |
| 5705 | |
| 5706 | err = rt6_fill_node(dump->net, dump->skb, w->rt, |
| 5707 | &rt6_ex->rt6i->dst, NULL, NULL, 0, |
| 5708 | RTM_NEWROUTE, |
| 5709 | NETLINK_CB(dump->cb->skb).portid, |
| 5710 | dump->cb->nlh->nlmsg_seq, w->flags); |
| 5711 | if (err) |
| 5712 | return err; |
| 5713 | |
| 5714 | w->count++; |
| 5715 | } |
| 5716 | bucket++; |
| 5717 | } |
| 5718 | |
| 5719 | return 0; |
| 5720 | } |
| 5721 | |
Stefano Brivio | bf9a8a061 | 2019-06-21 17:45:26 +0200 | [diff] [blame] | 5722 | /* Return -1 if done with node, number of handled routes on partial dump */ |
Stefano Brivio | 1e47b48 | 2019-06-21 17:45:27 +0200 | [diff] [blame] | 5723 | int rt6_dump_route(struct fib6_info *rt, void *p_arg, unsigned int skip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5724 | { |
| 5725 | struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg; |
David Ahern | 13e3890 | 2018-10-15 18:56:44 -0700 | [diff] [blame] | 5726 | struct fib_dump_filter *filter = &arg->filter; |
| 5727 | unsigned int flags = NLM_F_MULTI; |
David Ahern | 1f17e2f | 2017-01-26 13:54:08 -0800 | [diff] [blame] | 5728 | struct net *net = arg->net; |
Stefano Brivio | 1e47b48 | 2019-06-21 17:45:27 +0200 | [diff] [blame] | 5729 | int count = 0; |
David Ahern | 1f17e2f | 2017-01-26 13:54:08 -0800 | [diff] [blame] | 5730 | |
David Ahern | 421842e | 2018-04-17 17:33:18 -0700 | [diff] [blame] | 5731 | if (rt == net->ipv6.fib6_null_entry) |
Stefano Brivio | bf9a8a061 | 2019-06-21 17:45:26 +0200 | [diff] [blame] | 5732 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5733 | |
David Ahern | 13e3890 | 2018-10-15 18:56:44 -0700 | [diff] [blame] | 5734 | if ((filter->flags & RTM_F_PREFIX) && |
| 5735 | !(rt->fib6_flags & RTF_PREFIX_RT)) { |
| 5736 | /* success since this is not a prefix route */ |
Stefano Brivio | bf9a8a061 | 2019-06-21 17:45:26 +0200 | [diff] [blame] | 5737 | return -1; |
David Ahern | 13e3890 | 2018-10-15 18:56:44 -0700 | [diff] [blame] | 5738 | } |
Stefano Brivio | 1e47b48 | 2019-06-21 17:45:27 +0200 | [diff] [blame] | 5739 | if (filter->filter_set && |
| 5740 | ((filter->rt_type && rt->fib6_type != filter->rt_type) || |
| 5741 | (filter->dev && !fib6_info_uses_dev(rt, filter->dev)) || |
| 5742 | (filter->protocol && rt->fib6_protocol != filter->protocol))) { |
| 5743 | return -1; |
| 5744 | } |
| 5745 | |
| 5746 | if (filter->filter_set || |
| 5747 | !filter->dump_routes || !filter->dump_exceptions) { |
David Ahern | 13e3890 | 2018-10-15 18:56:44 -0700 | [diff] [blame] | 5748 | flags |= NLM_F_DUMP_FILTERED; |
David Ahern | f8cfe2c | 2017-01-17 15:51:08 -0800 | [diff] [blame] | 5749 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5750 | |
Stefano Brivio | 1e47b48 | 2019-06-21 17:45:27 +0200 | [diff] [blame] | 5751 | if (filter->dump_routes) { |
| 5752 | if (skip) { |
| 5753 | skip--; |
| 5754 | } else { |
| 5755 | if (rt6_fill_node(net, arg->skb, rt, NULL, NULL, NULL, |
| 5756 | 0, RTM_NEWROUTE, |
| 5757 | NETLINK_CB(arg->cb->skb).portid, |
| 5758 | arg->cb->nlh->nlmsg_seq, flags)) { |
| 5759 | return 0; |
| 5760 | } |
| 5761 | count++; |
| 5762 | } |
| 5763 | } |
| 5764 | |
| 5765 | if (filter->dump_exceptions) { |
| 5766 | struct fib6_nh_exception_dump_walker w = { .dump = arg, |
| 5767 | .rt = rt, |
| 5768 | .flags = flags, |
| 5769 | .skip = skip, |
| 5770 | .count = 0 }; |
| 5771 | int err; |
| 5772 | |
Eric Dumazet | 3b52569 | 2019-06-26 03:05:28 -0700 | [diff] [blame] | 5773 | rcu_read_lock(); |
Stefano Brivio | 1e47b48 | 2019-06-21 17:45:27 +0200 | [diff] [blame] | 5774 | if (rt->nh) { |
| 5775 | err = nexthop_for_each_fib6_nh(rt->nh, |
| 5776 | rt6_nh_dump_exceptions, |
| 5777 | &w); |
| 5778 | } else { |
| 5779 | err = rt6_nh_dump_exceptions(rt->fib6_nh, &w); |
| 5780 | } |
Eric Dumazet | 3b52569 | 2019-06-26 03:05:28 -0700 | [diff] [blame] | 5781 | rcu_read_unlock(); |
Stefano Brivio | 1e47b48 | 2019-06-21 17:45:27 +0200 | [diff] [blame] | 5782 | |
| 5783 | if (err) |
| 5784 | return count += w.count; |
| 5785 | } |
Stefano Brivio | bf9a8a061 | 2019-06-21 17:45:26 +0200 | [diff] [blame] | 5786 | |
| 5787 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5788 | } |
| 5789 | |
Jakub Kicinski | 0eff0a2 | 2019-01-18 10:46:24 -0800 | [diff] [blame] | 5790 | static int inet6_rtm_valid_getroute_req(struct sk_buff *skb, |
| 5791 | const struct nlmsghdr *nlh, |
| 5792 | struct nlattr **tb, |
| 5793 | struct netlink_ext_ack *extack) |
| 5794 | { |
| 5795 | struct rtmsg *rtm; |
| 5796 | int i, err; |
| 5797 | |
| 5798 | if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*rtm))) { |
| 5799 | NL_SET_ERR_MSG_MOD(extack, |
| 5800 | "Invalid header for get route request"); |
| 5801 | return -EINVAL; |
| 5802 | } |
| 5803 | |
| 5804 | if (!netlink_strict_get_check(skb)) |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 5805 | return nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX, |
| 5806 | rtm_ipv6_policy, extack); |
Jakub Kicinski | 0eff0a2 | 2019-01-18 10:46:24 -0800 | [diff] [blame] | 5807 | |
| 5808 | rtm = nlmsg_data(nlh); |
| 5809 | if ((rtm->rtm_src_len && rtm->rtm_src_len != 128) || |
| 5810 | (rtm->rtm_dst_len && rtm->rtm_dst_len != 128) || |
| 5811 | rtm->rtm_table || rtm->rtm_protocol || rtm->rtm_scope || |
| 5812 | rtm->rtm_type) { |
| 5813 | NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for get route request"); |
| 5814 | return -EINVAL; |
| 5815 | } |
| 5816 | if (rtm->rtm_flags & ~RTM_F_FIB_MATCH) { |
| 5817 | NL_SET_ERR_MSG_MOD(extack, |
| 5818 | "Invalid flags for get route request"); |
| 5819 | return -EINVAL; |
| 5820 | } |
| 5821 | |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 5822 | err = nlmsg_parse_deprecated_strict(nlh, sizeof(*rtm), tb, RTA_MAX, |
| 5823 | rtm_ipv6_policy, extack); |
Jakub Kicinski | 0eff0a2 | 2019-01-18 10:46:24 -0800 | [diff] [blame] | 5824 | if (err) |
| 5825 | return err; |
| 5826 | |
| 5827 | if ((tb[RTA_SRC] && !rtm->rtm_src_len) || |
| 5828 | (tb[RTA_DST] && !rtm->rtm_dst_len)) { |
| 5829 | NL_SET_ERR_MSG_MOD(extack, "rtm_src_len and rtm_dst_len must be 128 for IPv6"); |
| 5830 | return -EINVAL; |
| 5831 | } |
| 5832 | |
| 5833 | for (i = 0; i <= RTA_MAX; i++) { |
| 5834 | if (!tb[i]) |
| 5835 | continue; |
| 5836 | |
| 5837 | switch (i) { |
| 5838 | case RTA_SRC: |
| 5839 | case RTA_DST: |
| 5840 | case RTA_IIF: |
| 5841 | case RTA_OIF: |
| 5842 | case RTA_MARK: |
| 5843 | case RTA_UID: |
| 5844 | case RTA_SPORT: |
| 5845 | case RTA_DPORT: |
| 5846 | case RTA_IP_PROTO: |
| 5847 | break; |
| 5848 | default: |
| 5849 | NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in get route request"); |
| 5850 | return -EINVAL; |
| 5851 | } |
| 5852 | } |
| 5853 | |
| 5854 | return 0; |
| 5855 | } |
| 5856 | |
David Ahern | c21ef3e | 2017-04-16 09:48:24 -0700 | [diff] [blame] | 5857 | static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh, |
| 5858 | struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5859 | { |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 5860 | struct net *net = sock_net(in_skb->sk); |
Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 5861 | struct nlattr *tb[RTA_MAX+1]; |
Roopa Prabhu | 18c3a61 | 2017-05-25 10:42:40 -0700 | [diff] [blame] | 5862 | int err, iif = 0, oif = 0; |
David Ahern | a68886a | 2018-04-20 15:38:02 -0700 | [diff] [blame] | 5863 | struct fib6_info *from; |
Roopa Prabhu | 18c3a61 | 2017-05-25 10:42:40 -0700 | [diff] [blame] | 5864 | struct dst_entry *dst; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5865 | struct rt6_info *rt; |
Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 5866 | struct sk_buff *skb; |
| 5867 | struct rtmsg *rtm; |
Maciej Żenczykowski | 744486d | 2018-09-29 23:44:54 -0700 | [diff] [blame] | 5868 | struct flowi6 fl6 = {}; |
Roopa Prabhu | 18c3a61 | 2017-05-25 10:42:40 -0700 | [diff] [blame] | 5869 | bool fibmatch; |
Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 5870 | |
Jakub Kicinski | 0eff0a2 | 2019-01-18 10:46:24 -0800 | [diff] [blame] | 5871 | err = inet6_rtm_valid_getroute_req(in_skb, nlh, tb, extack); |
Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 5872 | if (err < 0) |
| 5873 | goto errout; |
| 5874 | |
| 5875 | err = -EINVAL; |
Hannes Frederic Sowa | 38b7097 | 2016-06-11 20:08:19 +0200 | [diff] [blame] | 5876 | rtm = nlmsg_data(nlh); |
| 5877 | fl6.flowlabel = ip6_make_flowinfo(rtm->rtm_tos, 0); |
Roopa Prabhu | 18c3a61 | 2017-05-25 10:42:40 -0700 | [diff] [blame] | 5878 | fibmatch = !!(rtm->rtm_flags & RTM_F_FIB_MATCH); |
Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 5879 | |
| 5880 | if (tb[RTA_SRC]) { |
| 5881 | if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr)) |
| 5882 | goto errout; |
| 5883 | |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 5884 | fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]); |
Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 5885 | } |
| 5886 | |
| 5887 | if (tb[RTA_DST]) { |
| 5888 | if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr)) |
| 5889 | goto errout; |
| 5890 | |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 5891 | fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]); |
Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 5892 | } |
| 5893 | |
| 5894 | if (tb[RTA_IIF]) |
| 5895 | iif = nla_get_u32(tb[RTA_IIF]); |
| 5896 | |
| 5897 | if (tb[RTA_OIF]) |
Shmulik Ladkani | 72331bc | 2012-04-01 04:03:45 +0000 | [diff] [blame] | 5898 | oif = nla_get_u32(tb[RTA_OIF]); |
Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 5899 | |
Lorenzo Colitti | 2e47b29 | 2014-05-15 16:38:41 -0700 | [diff] [blame] | 5900 | if (tb[RTA_MARK]) |
| 5901 | fl6.flowi6_mark = nla_get_u32(tb[RTA_MARK]); |
| 5902 | |
Lorenzo Colitti | 622ec2c | 2016-11-04 02:23:42 +0900 | [diff] [blame] | 5903 | if (tb[RTA_UID]) |
| 5904 | fl6.flowi6_uid = make_kuid(current_user_ns(), |
| 5905 | nla_get_u32(tb[RTA_UID])); |
| 5906 | else |
| 5907 | fl6.flowi6_uid = iif ? INVALID_UID : current_uid(); |
| 5908 | |
Roopa Prabhu | eacb938 | 2018-05-22 14:03:28 -0700 | [diff] [blame] | 5909 | if (tb[RTA_SPORT]) |
| 5910 | fl6.fl6_sport = nla_get_be16(tb[RTA_SPORT]); |
| 5911 | |
| 5912 | if (tb[RTA_DPORT]) |
| 5913 | fl6.fl6_dport = nla_get_be16(tb[RTA_DPORT]); |
| 5914 | |
| 5915 | if (tb[RTA_IP_PROTO]) { |
| 5916 | err = rtm_getroute_parse_ip_proto(tb[RTA_IP_PROTO], |
Hangbin Liu | 5e1a99e | 2019-02-27 16:15:29 +0800 | [diff] [blame] | 5917 | &fl6.flowi6_proto, AF_INET6, |
| 5918 | extack); |
Roopa Prabhu | eacb938 | 2018-05-22 14:03:28 -0700 | [diff] [blame] | 5919 | if (err) |
| 5920 | goto errout; |
| 5921 | } |
| 5922 | |
Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 5923 | if (iif) { |
| 5924 | struct net_device *dev; |
Shmulik Ladkani | 72331bc | 2012-04-01 04:03:45 +0000 | [diff] [blame] | 5925 | int flags = 0; |
| 5926 | |
Florian Westphal | 121622d | 2017-08-15 16:34:42 +0200 | [diff] [blame] | 5927 | rcu_read_lock(); |
| 5928 | |
| 5929 | dev = dev_get_by_index_rcu(net, iif); |
Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 5930 | if (!dev) { |
Florian Westphal | 121622d | 2017-08-15 16:34:42 +0200 | [diff] [blame] | 5931 | rcu_read_unlock(); |
Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 5932 | err = -ENODEV; |
| 5933 | goto errout; |
| 5934 | } |
Shmulik Ladkani | 72331bc | 2012-04-01 04:03:45 +0000 | [diff] [blame] | 5935 | |
| 5936 | fl6.flowi6_iif = iif; |
| 5937 | |
| 5938 | if (!ipv6_addr_any(&fl6.saddr)) |
| 5939 | flags |= RT6_LOOKUP_F_HAS_SADDR; |
| 5940 | |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 5941 | dst = ip6_route_input_lookup(net, dev, &fl6, NULL, flags); |
Florian Westphal | 121622d | 2017-08-15 16:34:42 +0200 | [diff] [blame] | 5942 | |
| 5943 | rcu_read_unlock(); |
Shmulik Ladkani | 72331bc | 2012-04-01 04:03:45 +0000 | [diff] [blame] | 5944 | } else { |
| 5945 | fl6.flowi6_oif = oif; |
| 5946 | |
Ido Schimmel | 58acfd7 | 2017-12-20 12:28:25 +0200 | [diff] [blame] | 5947 | dst = ip6_route_output(net, NULL, &fl6); |
Roopa Prabhu | 18c3a61 | 2017-05-25 10:42:40 -0700 | [diff] [blame] | 5948 | } |
| 5949 | |
Roopa Prabhu | 18c3a61 | 2017-05-25 10:42:40 -0700 | [diff] [blame] | 5950 | |
| 5951 | rt = container_of(dst, struct rt6_info, dst); |
| 5952 | if (rt->dst.error) { |
| 5953 | err = rt->dst.error; |
| 5954 | ip6_rt_put(rt); |
| 5955 | goto errout; |
Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 5956 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5957 | |
WANG Cong | 9d6acb3 | 2017-03-01 20:48:39 -0800 | [diff] [blame] | 5958 | if (rt == net->ipv6.ip6_null_entry) { |
| 5959 | err = rt->dst.error; |
| 5960 | ip6_rt_put(rt); |
| 5961 | goto errout; |
| 5962 | } |
| 5963 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5964 | skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); |
David S. Miller | 3830847 | 2011-12-03 18:02:47 -0500 | [diff] [blame] | 5965 | if (!skb) { |
Amerigo Wang | 94e187c | 2012-10-29 00:13:19 +0000 | [diff] [blame] | 5966 | ip6_rt_put(rt); |
Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 5967 | err = -ENOBUFS; |
| 5968 | goto errout; |
| 5969 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5970 | |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 5971 | skb_dst_set(skb, &rt->dst); |
David Ahern | a68886a | 2018-04-20 15:38:02 -0700 | [diff] [blame] | 5972 | |
| 5973 | rcu_read_lock(); |
| 5974 | from = rcu_dereference(rt->from); |
Martin KaFai Lau | 886b7a5 | 2019-04-30 10:45:12 -0700 | [diff] [blame] | 5975 | if (from) { |
| 5976 | if (fibmatch) |
| 5977 | err = rt6_fill_node(net, skb, from, NULL, NULL, NULL, |
| 5978 | iif, RTM_NEWROUTE, |
| 5979 | NETLINK_CB(in_skb).portid, |
| 5980 | nlh->nlmsg_seq, 0); |
| 5981 | else |
| 5982 | err = rt6_fill_node(net, skb, from, dst, &fl6.daddr, |
| 5983 | &fl6.saddr, iif, RTM_NEWROUTE, |
| 5984 | NETLINK_CB(in_skb).portid, |
| 5985 | nlh->nlmsg_seq, 0); |
| 5986 | } else { |
| 5987 | err = -ENETUNREACH; |
| 5988 | } |
David Ahern | a68886a | 2018-04-20 15:38:02 -0700 | [diff] [blame] | 5989 | rcu_read_unlock(); |
| 5990 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5991 | if (err < 0) { |
Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 5992 | kfree_skb(skb); |
| 5993 | goto errout; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5994 | } |
| 5995 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 5996 | err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid); |
Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 5997 | errout: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5998 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5999 | } |
| 6000 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 6001 | void inet6_rt_notify(int event, struct fib6_info *rt, struct nl_info *info, |
Roopa Prabhu | 37a1d36 | 2015-09-13 10:18:33 -0700 | [diff] [blame] | 6002 | unsigned int nlm_flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6003 | { |
| 6004 | struct sk_buff *skb; |
Daniel Lezcano | 5578689 | 2008-03-04 13:47:47 -0800 | [diff] [blame] | 6005 | struct net *net = info->nl_net; |
Denis V. Lunev | 528c4ce | 2007-12-13 09:45:12 -0800 | [diff] [blame] | 6006 | u32 seq; |
| 6007 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6008 | |
Denis V. Lunev | 528c4ce | 2007-12-13 09:45:12 -0800 | [diff] [blame] | 6009 | err = -ENOBUFS; |
David S. Miller | 3830847 | 2011-12-03 18:02:47 -0500 | [diff] [blame] | 6010 | seq = info->nlh ? info->nlh->nlmsg_seq : 0; |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 6011 | |
Roopa Prabhu | 19e42e4 | 2015-07-21 10:43:48 +0200 | [diff] [blame] | 6012 | skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any()); |
David S. Miller | 3830847 | 2011-12-03 18:02:47 -0500 | [diff] [blame] | 6013 | if (!skb) |
Thomas Graf | 21713eb | 2006-08-15 00:35:24 -0700 | [diff] [blame] | 6014 | goto errout; |
| 6015 | |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 6016 | err = rt6_fill_node(net, skb, rt, NULL, NULL, NULL, 0, |
| 6017 | event, info->portid, seq, nlm_flags); |
Patrick McHardy | 2693256 | 2007-01-31 23:16:40 -0800 | [diff] [blame] | 6018 | if (err < 0) { |
| 6019 | /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */ |
| 6020 | WARN_ON(err == -EMSGSIZE); |
| 6021 | kfree_skb(skb); |
| 6022 | goto errout; |
| 6023 | } |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 6024 | rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE, |
Pablo Neira Ayuso | 1ce85fe | 2009-02-24 23:18:28 -0800 | [diff] [blame] | 6025 | info->nlh, gfp_any()); |
| 6026 | return; |
Thomas Graf | 21713eb | 2006-08-15 00:35:24 -0700 | [diff] [blame] | 6027 | errout: |
| 6028 | if (err < 0) |
Daniel Lezcano | 5578689 | 2008-03-04 13:47:47 -0800 | [diff] [blame] | 6029 | rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6030 | } |
| 6031 | |
David Ahern | 19a3b7e | 2019-05-22 12:04:41 -0700 | [diff] [blame] | 6032 | void fib6_rt_update(struct net *net, struct fib6_info *rt, |
| 6033 | struct nl_info *info) |
| 6034 | { |
| 6035 | u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0; |
| 6036 | struct sk_buff *skb; |
| 6037 | int err = -ENOBUFS; |
| 6038 | |
| 6039 | /* call_fib6_entry_notifiers will be removed when in-kernel notifier |
| 6040 | * is implemented and supported for nexthop objects |
| 6041 | */ |
| 6042 | call_fib6_entry_notifiers(net, FIB_EVENT_ENTRY_REPLACE, rt, NULL); |
| 6043 | |
| 6044 | skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any()); |
| 6045 | if (!skb) |
| 6046 | goto errout; |
| 6047 | |
| 6048 | err = rt6_fill_node(net, skb, rt, NULL, NULL, NULL, 0, |
| 6049 | RTM_NEWROUTE, info->portid, seq, NLM_F_REPLACE); |
| 6050 | if (err < 0) { |
| 6051 | /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */ |
| 6052 | WARN_ON(err == -EMSGSIZE); |
| 6053 | kfree_skb(skb); |
| 6054 | goto errout; |
| 6055 | } |
| 6056 | rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE, |
| 6057 | info->nlh, gfp_any()); |
| 6058 | return; |
| 6059 | errout: |
| 6060 | if (err < 0) |
| 6061 | rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err); |
| 6062 | } |
| 6063 | |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 6064 | static int ip6_route_dev_notify(struct notifier_block *this, |
Jiri Pirko | 351638e | 2013-05-28 01:30:21 +0000 | [diff] [blame] | 6065 | unsigned long event, void *ptr) |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 6066 | { |
Jiri Pirko | 351638e | 2013-05-28 01:30:21 +0000 | [diff] [blame] | 6067 | struct net_device *dev = netdev_notifier_info_to_dev(ptr); |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 6068 | struct net *net = dev_net(dev); |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 6069 | |
WANG Cong | 242d3a4 | 2017-05-08 10:12:13 -0700 | [diff] [blame] | 6070 | if (!(dev->flags & IFF_LOOPBACK)) |
| 6071 | return NOTIFY_OK; |
| 6072 | |
| 6073 | if (event == NETDEV_REGISTER) { |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 6074 | net->ipv6.fib6_null_entry->fib6_nh->fib_nh_dev = dev; |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 6075 | net->ipv6.ip6_null_entry->dst.dev = dev; |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 6076 | net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev); |
| 6077 | #ifdef CONFIG_IPV6_MULTIPLE_TABLES |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 6078 | net->ipv6.ip6_prohibit_entry->dst.dev = dev; |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 6079 | net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev); |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 6080 | net->ipv6.ip6_blk_hole_entry->dst.dev = dev; |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 6081 | net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev); |
| 6082 | #endif |
WANG Cong | 76da070 | 2017-06-20 11:42:27 -0700 | [diff] [blame] | 6083 | } else if (event == NETDEV_UNREGISTER && |
| 6084 | dev->reg_state != NETREG_UNREGISTERED) { |
| 6085 | /* NETDEV_UNREGISTER could be fired for multiple times by |
| 6086 | * netdev_wait_allrefs(). Make sure we only call this once. |
| 6087 | */ |
Eric Dumazet | 12d94a8 | 2017-08-15 04:09:51 -0700 | [diff] [blame] | 6088 | in6_dev_put_clear(&net->ipv6.ip6_null_entry->rt6i_idev); |
WANG Cong | 242d3a4 | 2017-05-08 10:12:13 -0700 | [diff] [blame] | 6089 | #ifdef CONFIG_IPV6_MULTIPLE_TABLES |
Eric Dumazet | 12d94a8 | 2017-08-15 04:09:51 -0700 | [diff] [blame] | 6090 | in6_dev_put_clear(&net->ipv6.ip6_prohibit_entry->rt6i_idev); |
| 6091 | in6_dev_put_clear(&net->ipv6.ip6_blk_hole_entry->rt6i_idev); |
WANG Cong | 242d3a4 | 2017-05-08 10:12:13 -0700 | [diff] [blame] | 6092 | #endif |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 6093 | } |
| 6094 | |
| 6095 | return NOTIFY_OK; |
| 6096 | } |
| 6097 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6098 | /* |
| 6099 | * /proc |
| 6100 | */ |
| 6101 | |
| 6102 | #ifdef CONFIG_PROC_FS |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6103 | static int rt6_stats_seq_show(struct seq_file *seq, void *v) |
| 6104 | { |
Daniel Lezcano | 69ddb80 | 2008-03-04 13:46:23 -0800 | [diff] [blame] | 6105 | struct net *net = (struct net *)seq->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6106 | seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n", |
Daniel Lezcano | 69ddb80 | 2008-03-04 13:46:23 -0800 | [diff] [blame] | 6107 | net->ipv6.rt6_stats->fib_nodes, |
| 6108 | net->ipv6.rt6_stats->fib_route_nodes, |
Wei Wang | 81eb844 | 2017-10-06 12:06:11 -0700 | [diff] [blame] | 6109 | atomic_read(&net->ipv6.rt6_stats->fib_rt_alloc), |
Daniel Lezcano | 69ddb80 | 2008-03-04 13:46:23 -0800 | [diff] [blame] | 6110 | net->ipv6.rt6_stats->fib_rt_entries, |
| 6111 | net->ipv6.rt6_stats->fib_rt_cache, |
Eric Dumazet | fc66f95 | 2010-10-08 06:37:34 +0000 | [diff] [blame] | 6112 | dst_entries_get_slow(&net->ipv6.ip6_dst_ops), |
Daniel Lezcano | 69ddb80 | 2008-03-04 13:46:23 -0800 | [diff] [blame] | 6113 | net->ipv6.rt6_stats->fib_discarded_routes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6114 | |
| 6115 | return 0; |
| 6116 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6117 | #endif /* CONFIG_PROC_FS */ |
| 6118 | |
| 6119 | #ifdef CONFIG_SYSCTL |
| 6120 | |
Christoph Hellwig | 3292739 | 2020-04-24 08:43:38 +0200 | [diff] [blame] | 6121 | static int ipv6_sysctl_rtcache_flush(struct ctl_table *ctl, int write, |
| 6122 | void *buffer, size_t *lenp, loff_t *ppos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6123 | { |
Lucian Adrian Grijincu | c486da3 | 2011-02-24 19:48:03 +0000 | [diff] [blame] | 6124 | struct net *net; |
| 6125 | int delay; |
Aditya Pakki | f0fb9b2 | 2018-12-24 10:30:17 -0600 | [diff] [blame] | 6126 | int ret; |
Lucian Adrian Grijincu | c486da3 | 2011-02-24 19:48:03 +0000 | [diff] [blame] | 6127 | if (!write) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6128 | return -EINVAL; |
Lucian Adrian Grijincu | c486da3 | 2011-02-24 19:48:03 +0000 | [diff] [blame] | 6129 | |
| 6130 | net = (struct net *)ctl->extra1; |
| 6131 | delay = net->ipv6.sysctl.flush_delay; |
Aditya Pakki | f0fb9b2 | 2018-12-24 10:30:17 -0600 | [diff] [blame] | 6132 | ret = proc_dointvec(ctl, write, buffer, lenp, ppos); |
| 6133 | if (ret) |
| 6134 | return ret; |
| 6135 | |
Michal Kubeček | 2ac3ac8 | 2013-08-01 10:04:14 +0200 | [diff] [blame] | 6136 | fib6_run_gc(delay <= 0 ? 0 : (unsigned long)delay, net, delay > 0); |
Lucian Adrian Grijincu | c486da3 | 2011-02-24 19:48:03 +0000 | [diff] [blame] | 6137 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6138 | } |
| 6139 | |
David Ahern | ed792e2 | 2018-10-08 14:06:34 -0700 | [diff] [blame] | 6140 | static struct ctl_table ipv6_route_table_template[] = { |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 6141 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6142 | .procname = "flush", |
Daniel Lezcano | 4990509 | 2008-01-10 03:01:01 -0800 | [diff] [blame] | 6143 | .data = &init_net.ipv6.sysctl.flush_delay, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6144 | .maxlen = sizeof(int), |
Dave Jones | 89c8b3a1 | 2005-04-28 12:11:49 -0700 | [diff] [blame] | 6145 | .mode = 0200, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 6146 | .proc_handler = ipv6_sysctl_rtcache_flush |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6147 | }, |
| 6148 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6149 | .procname = "gc_thresh", |
Daniel Lezcano | 9a7ec3a | 2008-03-04 13:48:53 -0800 | [diff] [blame] | 6150 | .data = &ip6_dst_ops_template.gc_thresh, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6151 | .maxlen = sizeof(int), |
| 6152 | .mode = 0644, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 6153 | .proc_handler = proc_dointvec, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6154 | }, |
| 6155 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6156 | .procname = "max_size", |
Daniel Lezcano | 4990509 | 2008-01-10 03:01:01 -0800 | [diff] [blame] | 6157 | .data = &init_net.ipv6.sysctl.ip6_rt_max_size, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6158 | .maxlen = sizeof(int), |
| 6159 | .mode = 0644, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 6160 | .proc_handler = proc_dointvec, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6161 | }, |
| 6162 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6163 | .procname = "gc_min_interval", |
Daniel Lezcano | 4990509 | 2008-01-10 03:01:01 -0800 | [diff] [blame] | 6164 | .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6165 | .maxlen = sizeof(int), |
| 6166 | .mode = 0644, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 6167 | .proc_handler = proc_dointvec_jiffies, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6168 | }, |
| 6169 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6170 | .procname = "gc_timeout", |
Daniel Lezcano | 4990509 | 2008-01-10 03:01:01 -0800 | [diff] [blame] | 6171 | .data = &init_net.ipv6.sysctl.ip6_rt_gc_timeout, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6172 | .maxlen = sizeof(int), |
| 6173 | .mode = 0644, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 6174 | .proc_handler = proc_dointvec_jiffies, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6175 | }, |
| 6176 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6177 | .procname = "gc_interval", |
Daniel Lezcano | 4990509 | 2008-01-10 03:01:01 -0800 | [diff] [blame] | 6178 | .data = &init_net.ipv6.sysctl.ip6_rt_gc_interval, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6179 | .maxlen = sizeof(int), |
| 6180 | .mode = 0644, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 6181 | .proc_handler = proc_dointvec_jiffies, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6182 | }, |
| 6183 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6184 | .procname = "gc_elasticity", |
Daniel Lezcano | 4990509 | 2008-01-10 03:01:01 -0800 | [diff] [blame] | 6185 | .data = &init_net.ipv6.sysctl.ip6_rt_gc_elasticity, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6186 | .maxlen = sizeof(int), |
| 6187 | .mode = 0644, |
Min Zhang | f3d3f61 | 2010-08-14 22:42:51 -0700 | [diff] [blame] | 6188 | .proc_handler = proc_dointvec, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6189 | }, |
| 6190 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6191 | .procname = "mtu_expires", |
Daniel Lezcano | 4990509 | 2008-01-10 03:01:01 -0800 | [diff] [blame] | 6192 | .data = &init_net.ipv6.sysctl.ip6_rt_mtu_expires, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6193 | .maxlen = sizeof(int), |
| 6194 | .mode = 0644, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 6195 | .proc_handler = proc_dointvec_jiffies, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6196 | }, |
| 6197 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6198 | .procname = "min_adv_mss", |
Daniel Lezcano | 4990509 | 2008-01-10 03:01:01 -0800 | [diff] [blame] | 6199 | .data = &init_net.ipv6.sysctl.ip6_rt_min_advmss, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6200 | .maxlen = sizeof(int), |
| 6201 | .mode = 0644, |
Min Zhang | f3d3f61 | 2010-08-14 22:42:51 -0700 | [diff] [blame] | 6202 | .proc_handler = proc_dointvec, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6203 | }, |
| 6204 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6205 | .procname = "gc_min_interval_ms", |
Daniel Lezcano | 4990509 | 2008-01-10 03:01:01 -0800 | [diff] [blame] | 6206 | .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6207 | .maxlen = sizeof(int), |
| 6208 | .mode = 0644, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 6209 | .proc_handler = proc_dointvec_ms_jiffies, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6210 | }, |
David Ahern | 7c6bb7d | 2018-10-11 20:17:21 -0700 | [diff] [blame] | 6211 | { |
| 6212 | .procname = "skip_notify_on_dev_down", |
| 6213 | .data = &init_net.ipv6.sysctl.skip_notify_on_dev_down, |
| 6214 | .maxlen = sizeof(int), |
| 6215 | .mode = 0644, |
Eiichi Tsukata | b8e8a86 | 2019-06-25 12:08:01 +0900 | [diff] [blame] | 6216 | .proc_handler = proc_dointvec_minmax, |
Matteo Croce | eec4844 | 2019-07-18 15:58:50 -0700 | [diff] [blame] | 6217 | .extra1 = SYSCTL_ZERO, |
| 6218 | .extra2 = SYSCTL_ONE, |
David Ahern | 7c6bb7d | 2018-10-11 20:17:21 -0700 | [diff] [blame] | 6219 | }, |
Eric W. Biederman | f8572d8 | 2009-11-05 13:32:03 -0800 | [diff] [blame] | 6220 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6221 | }; |
| 6222 | |
Alexey Dobriyan | 2c8c1e7 | 2010-01-17 03:35:32 +0000 | [diff] [blame] | 6223 | struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net) |
Daniel Lezcano | 760f2d0 | 2008-01-10 02:53:43 -0800 | [diff] [blame] | 6224 | { |
| 6225 | struct ctl_table *table; |
| 6226 | |
| 6227 | table = kmemdup(ipv6_route_table_template, |
| 6228 | sizeof(ipv6_route_table_template), |
| 6229 | GFP_KERNEL); |
YOSHIFUJI Hideaki | 5ee0910 | 2008-02-28 00:24:28 +0900 | [diff] [blame] | 6230 | |
| 6231 | if (table) { |
| 6232 | table[0].data = &net->ipv6.sysctl.flush_delay; |
Lucian Adrian Grijincu | c486da3 | 2011-02-24 19:48:03 +0000 | [diff] [blame] | 6233 | table[0].extra1 = net; |
Alexey Dobriyan | 86393e5 | 2009-08-29 01:34:49 +0000 | [diff] [blame] | 6234 | table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh; |
YOSHIFUJI Hideaki | 5ee0910 | 2008-02-28 00:24:28 +0900 | [diff] [blame] | 6235 | table[2].data = &net->ipv6.sysctl.ip6_rt_max_size; |
| 6236 | table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval; |
| 6237 | table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout; |
| 6238 | table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval; |
| 6239 | table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity; |
| 6240 | table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires; |
| 6241 | table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss; |
Alexey Dobriyan | 9c69fab | 2009-12-18 20:11:03 -0800 | [diff] [blame] | 6242 | table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval; |
David Ahern | 7c6bb7d | 2018-10-11 20:17:21 -0700 | [diff] [blame] | 6243 | table[10].data = &net->ipv6.sysctl.skip_notify_on_dev_down; |
Eric W. Biederman | 464dc80 | 2012-11-16 03:02:59 +0000 | [diff] [blame] | 6244 | |
| 6245 | /* Don't export sysctls to unprivileged users */ |
| 6246 | if (net->user_ns != &init_user_ns) |
| 6247 | table[0].procname = NULL; |
YOSHIFUJI Hideaki | 5ee0910 | 2008-02-28 00:24:28 +0900 | [diff] [blame] | 6248 | } |
| 6249 | |
Daniel Lezcano | 760f2d0 | 2008-01-10 02:53:43 -0800 | [diff] [blame] | 6250 | return table; |
| 6251 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6252 | #endif |
| 6253 | |
Alexey Dobriyan | 2c8c1e7 | 2010-01-17 03:35:32 +0000 | [diff] [blame] | 6254 | static int __net_init ip6_route_net_init(struct net *net) |
Daniel Lezcano | cdb1876 | 2008-03-04 13:45:33 -0800 | [diff] [blame] | 6255 | { |
Pavel Emelyanov | 633d424b | 2008-04-21 14:25:23 -0700 | [diff] [blame] | 6256 | int ret = -ENOMEM; |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 6257 | |
Alexey Dobriyan | 86393e5 | 2009-08-29 01:34:49 +0000 | [diff] [blame] | 6258 | memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template, |
| 6259 | sizeof(net->ipv6.ip6_dst_ops)); |
Benjamin Thery | f2fc6a5 | 2008-03-04 13:49:23 -0800 | [diff] [blame] | 6260 | |
Eric Dumazet | fc66f95 | 2010-10-08 06:37:34 +0000 | [diff] [blame] | 6261 | if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0) |
| 6262 | goto out_ip6_dst_ops; |
| 6263 | |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 6264 | net->ipv6.fib6_null_entry = fib6_info_alloc(GFP_KERNEL, true); |
David Ahern | 421842e | 2018-04-17 17:33:18 -0700 | [diff] [blame] | 6265 | if (!net->ipv6.fib6_null_entry) |
| 6266 | goto out_ip6_dst_entries; |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 6267 | memcpy(net->ipv6.fib6_null_entry, &fib6_null_entry_template, |
| 6268 | sizeof(*net->ipv6.fib6_null_entry)); |
David Ahern | 421842e | 2018-04-17 17:33:18 -0700 | [diff] [blame] | 6269 | |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 6270 | net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template, |
| 6271 | sizeof(*net->ipv6.ip6_null_entry), |
| 6272 | GFP_KERNEL); |
| 6273 | if (!net->ipv6.ip6_null_entry) |
David Ahern | 421842e | 2018-04-17 17:33:18 -0700 | [diff] [blame] | 6274 | goto out_fib6_null_entry; |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 6275 | net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops; |
David S. Miller | 62fa8a8 | 2011-01-26 20:51:05 -0800 | [diff] [blame] | 6276 | dst_init_metrics(&net->ipv6.ip6_null_entry->dst, |
| 6277 | ip6_template_metrics, true); |
Wei Wang | 7410921 | 2019-06-20 17:36:38 -0700 | [diff] [blame] | 6278 | INIT_LIST_HEAD(&net->ipv6.ip6_null_entry->rt6i_uncached); |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 6279 | |
| 6280 | #ifdef CONFIG_IPV6_MULTIPLE_TABLES |
Vincent Bernat | feca7d8 | 2017-08-08 20:23:49 +0200 | [diff] [blame] | 6281 | net->ipv6.fib6_has_custom_rules = false; |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 6282 | net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template, |
| 6283 | sizeof(*net->ipv6.ip6_prohibit_entry), |
| 6284 | GFP_KERNEL); |
Peter Zijlstra | 68fffc6 | 2008-10-07 14:12:10 -0700 | [diff] [blame] | 6285 | if (!net->ipv6.ip6_prohibit_entry) |
| 6286 | goto out_ip6_null_entry; |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 6287 | net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops; |
David S. Miller | 62fa8a8 | 2011-01-26 20:51:05 -0800 | [diff] [blame] | 6288 | dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst, |
| 6289 | ip6_template_metrics, true); |
Wei Wang | 7410921 | 2019-06-20 17:36:38 -0700 | [diff] [blame] | 6290 | INIT_LIST_HEAD(&net->ipv6.ip6_prohibit_entry->rt6i_uncached); |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 6291 | |
| 6292 | net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template, |
| 6293 | sizeof(*net->ipv6.ip6_blk_hole_entry), |
| 6294 | GFP_KERNEL); |
Peter Zijlstra | 68fffc6 | 2008-10-07 14:12:10 -0700 | [diff] [blame] | 6295 | if (!net->ipv6.ip6_blk_hole_entry) |
| 6296 | goto out_ip6_prohibit_entry; |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 6297 | net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops; |
David S. Miller | 62fa8a8 | 2011-01-26 20:51:05 -0800 | [diff] [blame] | 6298 | dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst, |
| 6299 | ip6_template_metrics, true); |
Wei Wang | 7410921 | 2019-06-20 17:36:38 -0700 | [diff] [blame] | 6300 | INIT_LIST_HEAD(&net->ipv6.ip6_blk_hole_entry->rt6i_uncached); |
Paolo Abeni | b9b33e7 | 2019-11-20 13:47:34 +0100 | [diff] [blame] | 6301 | #ifdef CONFIG_IPV6_SUBTREES |
| 6302 | net->ipv6.fib6_routes_require_src = 0; |
| 6303 | #endif |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 6304 | #endif |
| 6305 | |
Peter Zijlstra | b339a47c | 2008-10-07 14:15:00 -0700 | [diff] [blame] | 6306 | net->ipv6.sysctl.flush_delay = 0; |
| 6307 | net->ipv6.sysctl.ip6_rt_max_size = 4096; |
| 6308 | net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2; |
| 6309 | net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ; |
| 6310 | net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ; |
| 6311 | net->ipv6.sysctl.ip6_rt_gc_elasticity = 9; |
| 6312 | net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ; |
| 6313 | net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40; |
David Ahern | 7c6bb7d | 2018-10-11 20:17:21 -0700 | [diff] [blame] | 6314 | net->ipv6.sysctl.skip_notify_on_dev_down = 0; |
Peter Zijlstra | b339a47c | 2008-10-07 14:15:00 -0700 | [diff] [blame] | 6315 | |
Benjamin Thery | 6891a34 | 2008-03-04 13:49:47 -0800 | [diff] [blame] | 6316 | net->ipv6.ip6_rt_gc_expire = 30*HZ; |
| 6317 | |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 6318 | ret = 0; |
| 6319 | out: |
| 6320 | return ret; |
Benjamin Thery | f2fc6a5 | 2008-03-04 13:49:23 -0800 | [diff] [blame] | 6321 | |
Peter Zijlstra | 68fffc6 | 2008-10-07 14:12:10 -0700 | [diff] [blame] | 6322 | #ifdef CONFIG_IPV6_MULTIPLE_TABLES |
| 6323 | out_ip6_prohibit_entry: |
| 6324 | kfree(net->ipv6.ip6_prohibit_entry); |
| 6325 | out_ip6_null_entry: |
| 6326 | kfree(net->ipv6.ip6_null_entry); |
| 6327 | #endif |
David Ahern | 421842e | 2018-04-17 17:33:18 -0700 | [diff] [blame] | 6328 | out_fib6_null_entry: |
| 6329 | kfree(net->ipv6.fib6_null_entry); |
Eric Dumazet | fc66f95 | 2010-10-08 06:37:34 +0000 | [diff] [blame] | 6330 | out_ip6_dst_entries: |
| 6331 | dst_entries_destroy(&net->ipv6.ip6_dst_ops); |
Benjamin Thery | f2fc6a5 | 2008-03-04 13:49:23 -0800 | [diff] [blame] | 6332 | out_ip6_dst_ops: |
Benjamin Thery | f2fc6a5 | 2008-03-04 13:49:23 -0800 | [diff] [blame] | 6333 | goto out; |
Daniel Lezcano | cdb1876 | 2008-03-04 13:45:33 -0800 | [diff] [blame] | 6334 | } |
| 6335 | |
Alexey Dobriyan | 2c8c1e7 | 2010-01-17 03:35:32 +0000 | [diff] [blame] | 6336 | static void __net_exit ip6_route_net_exit(struct net *net) |
Daniel Lezcano | cdb1876 | 2008-03-04 13:45:33 -0800 | [diff] [blame] | 6337 | { |
David Ahern | 421842e | 2018-04-17 17:33:18 -0700 | [diff] [blame] | 6338 | kfree(net->ipv6.fib6_null_entry); |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 6339 | kfree(net->ipv6.ip6_null_entry); |
| 6340 | #ifdef CONFIG_IPV6_MULTIPLE_TABLES |
| 6341 | kfree(net->ipv6.ip6_prohibit_entry); |
| 6342 | kfree(net->ipv6.ip6_blk_hole_entry); |
| 6343 | #endif |
Xiaotian Feng | 41bb78b | 2010-11-02 16:11:05 +0000 | [diff] [blame] | 6344 | dst_entries_destroy(&net->ipv6.ip6_dst_ops); |
Daniel Lezcano | cdb1876 | 2008-03-04 13:45:33 -0800 | [diff] [blame] | 6345 | } |
| 6346 | |
Thomas Graf | d189634 | 2012-06-18 12:08:33 +0000 | [diff] [blame] | 6347 | static int __net_init ip6_route_net_init_late(struct net *net) |
| 6348 | { |
| 6349 | #ifdef CONFIG_PROC_FS |
Christoph Hellwig | c350637 | 2018-04-10 19:42:55 +0200 | [diff] [blame] | 6350 | proc_create_net("ipv6_route", 0, net->proc_net, &ipv6_route_seq_ops, |
| 6351 | sizeof(struct ipv6_route_iter)); |
Christoph Hellwig | 3617d94 | 2018-04-13 20:38:35 +0200 | [diff] [blame] | 6352 | proc_create_net_single("rt6_stats", 0444, net->proc_net, |
| 6353 | rt6_stats_seq_show, NULL); |
Thomas Graf | d189634 | 2012-06-18 12:08:33 +0000 | [diff] [blame] | 6354 | #endif |
| 6355 | return 0; |
| 6356 | } |
| 6357 | |
| 6358 | static void __net_exit ip6_route_net_exit_late(struct net *net) |
| 6359 | { |
| 6360 | #ifdef CONFIG_PROC_FS |
Gao feng | ece31ff | 2013-02-18 01:34:56 +0000 | [diff] [blame] | 6361 | remove_proc_entry("ipv6_route", net->proc_net); |
| 6362 | remove_proc_entry("rt6_stats", net->proc_net); |
Thomas Graf | d189634 | 2012-06-18 12:08:33 +0000 | [diff] [blame] | 6363 | #endif |
| 6364 | } |
| 6365 | |
Daniel Lezcano | cdb1876 | 2008-03-04 13:45:33 -0800 | [diff] [blame] | 6366 | static struct pernet_operations ip6_route_net_ops = { |
| 6367 | .init = ip6_route_net_init, |
| 6368 | .exit = ip6_route_net_exit, |
| 6369 | }; |
| 6370 | |
David S. Miller | c3426b4 | 2012-06-09 16:27:05 -0700 | [diff] [blame] | 6371 | static int __net_init ipv6_inetpeer_init(struct net *net) |
| 6372 | { |
| 6373 | struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL); |
| 6374 | |
| 6375 | if (!bp) |
| 6376 | return -ENOMEM; |
| 6377 | inet_peer_base_init(bp); |
| 6378 | net->ipv6.peers = bp; |
| 6379 | return 0; |
| 6380 | } |
| 6381 | |
| 6382 | static void __net_exit ipv6_inetpeer_exit(struct net *net) |
| 6383 | { |
| 6384 | struct inet_peer_base *bp = net->ipv6.peers; |
| 6385 | |
| 6386 | net->ipv6.peers = NULL; |
David S. Miller | 56a6b24 | 2012-06-09 16:32:41 -0700 | [diff] [blame] | 6387 | inetpeer_invalidate_tree(bp); |
David S. Miller | c3426b4 | 2012-06-09 16:27:05 -0700 | [diff] [blame] | 6388 | kfree(bp); |
| 6389 | } |
| 6390 | |
David S. Miller | 2b823f7 | 2012-06-09 19:00:16 -0700 | [diff] [blame] | 6391 | static struct pernet_operations ipv6_inetpeer_ops = { |
David S. Miller | c3426b4 | 2012-06-09 16:27:05 -0700 | [diff] [blame] | 6392 | .init = ipv6_inetpeer_init, |
| 6393 | .exit = ipv6_inetpeer_exit, |
| 6394 | }; |
| 6395 | |
Thomas Graf | d189634 | 2012-06-18 12:08:33 +0000 | [diff] [blame] | 6396 | static struct pernet_operations ip6_route_net_late_ops = { |
| 6397 | .init = ip6_route_net_init_late, |
| 6398 | .exit = ip6_route_net_exit_late, |
| 6399 | }; |
| 6400 | |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 6401 | static struct notifier_block ip6_route_dev_notifier = { |
| 6402 | .notifier_call = ip6_route_dev_notify, |
WANG Cong | 242d3a4 | 2017-05-08 10:12:13 -0700 | [diff] [blame] | 6403 | .priority = ADDRCONF_NOTIFY_PRIORITY - 10, |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 6404 | }; |
| 6405 | |
WANG Cong | 2f46093 | 2017-05-03 22:07:31 -0700 | [diff] [blame] | 6406 | void __init ip6_route_init_special_entries(void) |
| 6407 | { |
| 6408 | /* Registering of the loopback is done before this portion of code, |
| 6409 | * the loopback reference in rt6_info will not be taken, do it |
| 6410 | * manually for init_net */ |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 6411 | init_net.ipv6.fib6_null_entry->fib6_nh->fib_nh_dev = init_net.loopback_dev; |
WANG Cong | 2f46093 | 2017-05-03 22:07:31 -0700 | [diff] [blame] | 6412 | init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev; |
| 6413 | init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); |
| 6414 | #ifdef CONFIG_IPV6_MULTIPLE_TABLES |
| 6415 | init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev; |
| 6416 | init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); |
| 6417 | init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev; |
| 6418 | init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); |
| 6419 | #endif |
| 6420 | } |
| 6421 | |
Yonghong Song | 138d0be | 2020-05-09 10:59:10 -0700 | [diff] [blame] | 6422 | #if IS_BUILTIN(CONFIG_IPV6) |
| 6423 | #if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_PROC_FS) |
| 6424 | DEFINE_BPF_ITER_FUNC(ipv6_route, struct bpf_iter_meta *meta, struct fib6_info *rt) |
| 6425 | |
Yonghong Song | 15172a4 | 2020-05-13 11:02:19 -0700 | [diff] [blame] | 6426 | static const struct bpf_iter_reg ipv6_route_reg_info = { |
| 6427 | .target = "ipv6_route", |
| 6428 | .seq_ops = &ipv6_route_seq_ops, |
| 6429 | .init_seq_private = bpf_iter_init_seq_net, |
| 6430 | .fini_seq_private = bpf_iter_fini_seq_net, |
| 6431 | .seq_priv_size = sizeof(struct ipv6_route_iter), |
Yonghong Song | 3c32cc1 | 2020-05-13 11:02:21 -0700 | [diff] [blame] | 6432 | .ctx_arg_info_size = 1, |
| 6433 | .ctx_arg_info = { |
| 6434 | { offsetof(struct bpf_iter__ipv6_route, rt), |
| 6435 | PTR_TO_BTF_ID_OR_NULL }, |
| 6436 | }, |
Yonghong Song | 15172a4 | 2020-05-13 11:02:19 -0700 | [diff] [blame] | 6437 | }; |
| 6438 | |
Yonghong Song | 138d0be | 2020-05-09 10:59:10 -0700 | [diff] [blame] | 6439 | static int __init bpf_iter_register(void) |
| 6440 | { |
Yonghong Song | 15172a4 | 2020-05-13 11:02:19 -0700 | [diff] [blame] | 6441 | return bpf_iter_reg_target(&ipv6_route_reg_info); |
Yonghong Song | 138d0be | 2020-05-09 10:59:10 -0700 | [diff] [blame] | 6442 | } |
| 6443 | |
| 6444 | static void bpf_iter_unregister(void) |
| 6445 | { |
Yonghong Song | ab2ee4f | 2020-05-13 11:02:20 -0700 | [diff] [blame] | 6446 | bpf_iter_unreg_target(&ipv6_route_reg_info); |
Yonghong Song | 138d0be | 2020-05-09 10:59:10 -0700 | [diff] [blame] | 6447 | } |
| 6448 | #endif |
| 6449 | #endif |
| 6450 | |
Daniel Lezcano | 433d49c | 2007-12-07 00:43:48 -0800 | [diff] [blame] | 6451 | int __init ip6_route_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6452 | { |
Daniel Lezcano | 433d49c | 2007-12-07 00:43:48 -0800 | [diff] [blame] | 6453 | int ret; |
Martin KaFai Lau | 8d0b94a | 2015-05-22 20:56:04 -0700 | [diff] [blame] | 6454 | int cpu; |
Daniel Lezcano | 433d49c | 2007-12-07 00:43:48 -0800 | [diff] [blame] | 6455 | |
Daniel Lezcano | 9a7ec3a | 2008-03-04 13:48:53 -0800 | [diff] [blame] | 6456 | ret = -ENOMEM; |
| 6457 | ip6_dst_ops_template.kmem_cachep = |
| 6458 | kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0, |
| 6459 | SLAB_HWCACHE_ALIGN, NULL); |
| 6460 | if (!ip6_dst_ops_template.kmem_cachep) |
Fernando Carrijo | c19a28e | 2009-01-07 18:09:08 -0800 | [diff] [blame] | 6461 | goto out; |
David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 6462 | |
Eric Dumazet | fc66f95 | 2010-10-08 06:37:34 +0000 | [diff] [blame] | 6463 | ret = dst_entries_init(&ip6_dst_blackhole_ops); |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 6464 | if (ret) |
Daniel Lezcano | bdb3289 | 2008-03-04 13:48:10 -0800 | [diff] [blame] | 6465 | goto out_kmem_cache; |
Daniel Lezcano | bdb3289 | 2008-03-04 13:48:10 -0800 | [diff] [blame] | 6466 | |
David S. Miller | c3426b4 | 2012-06-09 16:27:05 -0700 | [diff] [blame] | 6467 | ret = register_pernet_subsys(&ipv6_inetpeer_ops); |
| 6468 | if (ret) |
David S. Miller | e8803b6 | 2012-06-16 01:12:19 -0700 | [diff] [blame] | 6469 | goto out_dst_entries; |
Thomas Graf | 2a0c451 | 2012-06-14 23:00:17 +0000 | [diff] [blame] | 6470 | |
David S. Miller | 7e52b33 | 2012-06-15 15:51:55 -0700 | [diff] [blame] | 6471 | ret = register_pernet_subsys(&ip6_route_net_ops); |
| 6472 | if (ret) |
| 6473 | goto out_register_inetpeer; |
David S. Miller | c3426b4 | 2012-06-09 16:27:05 -0700 | [diff] [blame] | 6474 | |
Arnaud Ebalard | 5dc121e | 2008-10-01 02:37:56 -0700 | [diff] [blame] | 6475 | ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep; |
| 6476 | |
David S. Miller | e8803b6 | 2012-06-16 01:12:19 -0700 | [diff] [blame] | 6477 | ret = fib6_init(); |
Daniel Lezcano | 433d49c | 2007-12-07 00:43:48 -0800 | [diff] [blame] | 6478 | if (ret) |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 6479 | goto out_register_subsys; |
Daniel Lezcano | 433d49c | 2007-12-07 00:43:48 -0800 | [diff] [blame] | 6480 | |
Daniel Lezcano | 433d49c | 2007-12-07 00:43:48 -0800 | [diff] [blame] | 6481 | ret = xfrm6_init(); |
| 6482 | if (ret) |
David S. Miller | e8803b6 | 2012-06-16 01:12:19 -0700 | [diff] [blame] | 6483 | goto out_fib6_init; |
Daniel Lezcano | c35b7e7 | 2007-12-08 00:14:11 -0800 | [diff] [blame] | 6484 | |
Daniel Lezcano | 433d49c | 2007-12-07 00:43:48 -0800 | [diff] [blame] | 6485 | ret = fib6_rules_init(); |
| 6486 | if (ret) |
| 6487 | goto xfrm6_init; |
Daniel Lezcano | 7e5449c | 2007-12-08 00:14:54 -0800 | [diff] [blame] | 6488 | |
Thomas Graf | d189634 | 2012-06-18 12:08:33 +0000 | [diff] [blame] | 6489 | ret = register_pernet_subsys(&ip6_route_net_late_ops); |
| 6490 | if (ret) |
| 6491 | goto fib6_rules_init; |
| 6492 | |
Florian Westphal | 16feebc | 2017-12-02 21:44:08 +0100 | [diff] [blame] | 6493 | ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_NEWROUTE, |
| 6494 | inet6_rtm_newroute, NULL, 0); |
| 6495 | if (ret < 0) |
| 6496 | goto out_register_late_subsys; |
| 6497 | |
| 6498 | ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_DELROUTE, |
| 6499 | inet6_rtm_delroute, NULL, 0); |
| 6500 | if (ret < 0) |
| 6501 | goto out_register_late_subsys; |
| 6502 | |
| 6503 | ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETROUTE, |
| 6504 | inet6_rtm_getroute, NULL, |
| 6505 | RTNL_FLAG_DOIT_UNLOCKED); |
| 6506 | if (ret < 0) |
Thomas Graf | d189634 | 2012-06-18 12:08:33 +0000 | [diff] [blame] | 6507 | goto out_register_late_subsys; |
Daniel Lezcano | 433d49c | 2007-12-07 00:43:48 -0800 | [diff] [blame] | 6508 | |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 6509 | ret = register_netdevice_notifier(&ip6_route_dev_notifier); |
Daniel Lezcano | cdb1876 | 2008-03-04 13:45:33 -0800 | [diff] [blame] | 6510 | if (ret) |
Thomas Graf | d189634 | 2012-06-18 12:08:33 +0000 | [diff] [blame] | 6511 | goto out_register_late_subsys; |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 6512 | |
Yonghong Song | 138d0be | 2020-05-09 10:59:10 -0700 | [diff] [blame] | 6513 | #if IS_BUILTIN(CONFIG_IPV6) |
| 6514 | #if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_PROC_FS) |
| 6515 | ret = bpf_iter_register(); |
| 6516 | if (ret) |
| 6517 | goto out_register_late_subsys; |
| 6518 | #endif |
| 6519 | #endif |
| 6520 | |
Martin KaFai Lau | 8d0b94a | 2015-05-22 20:56:04 -0700 | [diff] [blame] | 6521 | for_each_possible_cpu(cpu) { |
| 6522 | struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu); |
| 6523 | |
| 6524 | INIT_LIST_HEAD(&ul->head); |
| 6525 | spin_lock_init(&ul->lock); |
| 6526 | } |
| 6527 | |
Daniel Lezcano | 433d49c | 2007-12-07 00:43:48 -0800 | [diff] [blame] | 6528 | out: |
| 6529 | return ret; |
| 6530 | |
Thomas Graf | d189634 | 2012-06-18 12:08:33 +0000 | [diff] [blame] | 6531 | out_register_late_subsys: |
Florian Westphal | 16feebc | 2017-12-02 21:44:08 +0100 | [diff] [blame] | 6532 | rtnl_unregister_all(PF_INET6); |
Thomas Graf | d189634 | 2012-06-18 12:08:33 +0000 | [diff] [blame] | 6533 | unregister_pernet_subsys(&ip6_route_net_late_ops); |
Daniel Lezcano | 433d49c | 2007-12-07 00:43:48 -0800 | [diff] [blame] | 6534 | fib6_rules_init: |
Daniel Lezcano | 433d49c | 2007-12-07 00:43:48 -0800 | [diff] [blame] | 6535 | fib6_rules_cleanup(); |
| 6536 | xfrm6_init: |
Daniel Lezcano | 433d49c | 2007-12-07 00:43:48 -0800 | [diff] [blame] | 6537 | xfrm6_fini(); |
Thomas Graf | 2a0c451 | 2012-06-14 23:00:17 +0000 | [diff] [blame] | 6538 | out_fib6_init: |
| 6539 | fib6_gc_cleanup(); |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 6540 | out_register_subsys: |
| 6541 | unregister_pernet_subsys(&ip6_route_net_ops); |
David S. Miller | 7e52b33 | 2012-06-15 15:51:55 -0700 | [diff] [blame] | 6542 | out_register_inetpeer: |
| 6543 | unregister_pernet_subsys(&ipv6_inetpeer_ops); |
Eric Dumazet | fc66f95 | 2010-10-08 06:37:34 +0000 | [diff] [blame] | 6544 | out_dst_entries: |
| 6545 | dst_entries_destroy(&ip6_dst_blackhole_ops); |
Daniel Lezcano | 433d49c | 2007-12-07 00:43:48 -0800 | [diff] [blame] | 6546 | out_kmem_cache: |
Benjamin Thery | f2fc6a5 | 2008-03-04 13:49:23 -0800 | [diff] [blame] | 6547 | kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep); |
Daniel Lezcano | 433d49c | 2007-12-07 00:43:48 -0800 | [diff] [blame] | 6548 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6549 | } |
| 6550 | |
| 6551 | void ip6_route_cleanup(void) |
| 6552 | { |
Yonghong Song | 138d0be | 2020-05-09 10:59:10 -0700 | [diff] [blame] | 6553 | #if IS_BUILTIN(CONFIG_IPV6) |
| 6554 | #if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_PROC_FS) |
| 6555 | bpf_iter_unregister(); |
| 6556 | #endif |
| 6557 | #endif |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 6558 | unregister_netdevice_notifier(&ip6_route_dev_notifier); |
Thomas Graf | d189634 | 2012-06-18 12:08:33 +0000 | [diff] [blame] | 6559 | unregister_pernet_subsys(&ip6_route_net_late_ops); |
Thomas Graf | 101367c | 2006-08-04 03:39:02 -0700 | [diff] [blame] | 6560 | fib6_rules_cleanup(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6561 | xfrm6_fini(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6562 | fib6_gc_cleanup(); |
David S. Miller | c3426b4 | 2012-06-09 16:27:05 -0700 | [diff] [blame] | 6563 | unregister_pernet_subsys(&ipv6_inetpeer_ops); |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 6564 | unregister_pernet_subsys(&ip6_route_net_ops); |
Xiaotian Feng | 41bb78b | 2010-11-02 16:11:05 +0000 | [diff] [blame] | 6565 | dst_entries_destroy(&ip6_dst_blackhole_ops); |
Benjamin Thery | f2fc6a5 | 2008-03-04 13:49:23 -0800 | [diff] [blame] | 6566 | kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6567 | } |