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 | f88d8ea | 2019-06-03 20:19:52 -0700 | [diff] [blame] | 434 | if ((!match->fib6_nsiblings && !match->nh) || 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 | |
Jakub Sitnicki | b673d6c | 2017-08-23 09:58:31 +0200 | [diff] [blame] | 437 | /* We might have already computed the hash for ICMPv6 errors. In such |
| 438 | * case it will always be non-zero. Otherwise now is the time to do it. |
| 439 | */ |
David Ahern | f88d8ea | 2019-06-03 20:19:52 -0700 | [diff] [blame] | 440 | if (!fl6->mp_hash && |
| 441 | (!match->nh || nexthop_is_multipath(match->nh))) |
David Ahern | b4bac17 | 2018-03-02 08:32:18 -0800 | [diff] [blame] | 442 | fl6->mp_hash = rt6_multipath_hash(net, fl6, skb, NULL); |
Jakub Sitnicki | b673d6c | 2017-08-23 09:58:31 +0200 | [diff] [blame] | 443 | |
David Ahern | f88d8ea | 2019-06-03 20:19:52 -0700 | [diff] [blame] | 444 | if (unlikely(match->nh)) { |
| 445 | nexthop_path_fib6_result(res, fl6->mp_hash); |
| 446 | return; |
| 447 | } |
| 448 | |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 449 | 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] | 450 | goto out; |
Ido Schimmel | bbfcd77 | 2017-11-21 09:50:12 +0200 | [diff] [blame] | 451 | |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 452 | list_for_each_entry_safe(sibling, next_sibling, &match->fib6_siblings, |
| 453 | fib6_siblings) { |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 454 | const struct fib6_nh *nh = sibling->fib6_nh; |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 455 | int nh_upper_bound; |
| 456 | |
David Ahern | 702cea5 | 2019-04-09 14:41:13 -0700 | [diff] [blame] | 457 | nh_upper_bound = atomic_read(&nh->fib_nh_upper_bound); |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 458 | if (fl6->mp_hash > nh_upper_bound) |
Ido Schimmel | 3d709f6 | 2018-01-09 16:40:27 +0200 | [diff] [blame] | 459 | continue; |
David Ahern | 702cea5 | 2019-04-09 14:41:13 -0700 | [diff] [blame] | 460 | if (rt6_score_route(nh, sibling->fib6_flags, oif, strict) < 0) |
Ido Schimmel | 3d709f6 | 2018-01-09 16:40:27 +0200 | [diff] [blame] | 461 | break; |
| 462 | match = sibling; |
| 463 | break; |
| 464 | } |
| 465 | |
David Ahern | b1d4099 | 2019-04-16 14:35:59 -0700 | [diff] [blame] | 466 | out: |
| 467 | res->f6i = match; |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 468 | res->nh = match->fib6_nh; |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 469 | } |
| 470 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | /* |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 472 | * Route lookup. rcu_read_lock() should be held. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | */ |
| 474 | |
David Ahern | 0c59d00 | 2019-04-09 14:41:18 -0700 | [diff] [blame] | 475 | static bool __rt6_device_match(struct net *net, const struct fib6_nh *nh, |
| 476 | const struct in6_addr *saddr, int oif, int flags) |
| 477 | { |
| 478 | const struct net_device *dev; |
| 479 | |
| 480 | if (nh->fib_nh_flags & RTNH_F_DEAD) |
| 481 | return false; |
| 482 | |
| 483 | dev = nh->fib_nh_dev; |
| 484 | if (oif) { |
| 485 | if (dev->ifindex == oif) |
| 486 | return true; |
| 487 | } else { |
| 488 | if (ipv6_chk_addr(net, saddr, dev, |
| 489 | flags & RT6_LOOKUP_F_IFACE)) |
| 490 | return true; |
| 491 | } |
| 492 | |
| 493 | return false; |
| 494 | } |
| 495 | |
David Ahern | 962b680 | 2019-06-08 14:53:24 -0700 | [diff] [blame] | 496 | struct fib6_nh_dm_arg { |
| 497 | struct net *net; |
| 498 | const struct in6_addr *saddr; |
| 499 | int oif; |
| 500 | int flags; |
| 501 | struct fib6_nh *nh; |
| 502 | }; |
| 503 | |
| 504 | static int __rt6_nh_dev_match(struct fib6_nh *nh, void *_arg) |
| 505 | { |
| 506 | struct fib6_nh_dm_arg *arg = _arg; |
| 507 | |
| 508 | arg->nh = nh; |
| 509 | return __rt6_device_match(arg->net, nh, arg->saddr, arg->oif, |
| 510 | arg->flags); |
| 511 | } |
| 512 | |
| 513 | /* returns fib6_nh from nexthop or NULL */ |
| 514 | static struct fib6_nh *rt6_nh_dev_match(struct net *net, struct nexthop *nh, |
| 515 | struct fib6_result *res, |
| 516 | const struct in6_addr *saddr, |
| 517 | int oif, int flags) |
| 518 | { |
| 519 | struct fib6_nh_dm_arg arg = { |
| 520 | .net = net, |
| 521 | .saddr = saddr, |
| 522 | .oif = oif, |
| 523 | .flags = flags, |
| 524 | }; |
| 525 | |
| 526 | if (nexthop_is_blackhole(nh)) |
| 527 | return NULL; |
| 528 | |
| 529 | if (nexthop_for_each_fib6_nh(nh, __rt6_nh_dev_match, &arg)) |
| 530 | return arg.nh; |
| 531 | |
| 532 | return NULL; |
| 533 | } |
| 534 | |
David Ahern | 75ef738 | 2019-04-16 14:36:07 -0700 | [diff] [blame] | 535 | static void rt6_device_match(struct net *net, struct fib6_result *res, |
| 536 | const struct in6_addr *saddr, int oif, int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | { |
David Ahern | 75ef738 | 2019-04-16 14:36:07 -0700 | [diff] [blame] | 538 | struct fib6_info *f6i = res->f6i; |
| 539 | struct fib6_info *spf6i; |
| 540 | struct fib6_nh *nh; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | |
David Ahern | 75ef738 | 2019-04-16 14:36:07 -0700 | [diff] [blame] | 542 | if (!oif && ipv6_addr_any(saddr)) { |
David Ahern | f88d8ea | 2019-06-03 20:19:52 -0700 | [diff] [blame] | 543 | if (unlikely(f6i->nh)) { |
| 544 | nh = nexthop_fib6_nh(f6i->nh); |
| 545 | if (nexthop_is_blackhole(f6i->nh)) |
| 546 | goto out_blackhole; |
| 547 | } else { |
| 548 | nh = f6i->fib6_nh; |
| 549 | } |
David Ahern | 7d21fec | 2019-04-16 14:36:11 -0700 | [diff] [blame] | 550 | if (!(nh->fib_nh_flags & RTNH_F_DEAD)) |
| 551 | goto out; |
YOSHIFUJI Hideaki | dd3abc4 | 2008-07-02 18:30:18 +0900 | [diff] [blame] | 552 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | |
David Ahern | 75ef738 | 2019-04-16 14:36:07 -0700 | [diff] [blame] | 554 | for (spf6i = f6i; spf6i; spf6i = rcu_dereference(spf6i->fib6_next)) { |
David Ahern | 962b680 | 2019-06-08 14:53:24 -0700 | [diff] [blame] | 555 | bool matched = false; |
| 556 | |
| 557 | if (unlikely(spf6i->nh)) { |
| 558 | nh = rt6_nh_dev_match(net, spf6i->nh, res, saddr, |
| 559 | oif, flags); |
| 560 | if (nh) |
| 561 | matched = true; |
| 562 | } else { |
| 563 | nh = spf6i->fib6_nh; |
| 564 | if (__rt6_device_match(net, nh, saddr, oif, flags)) |
| 565 | matched = true; |
| 566 | } |
| 567 | if (matched) { |
David Ahern | 75ef738 | 2019-04-16 14:36:07 -0700 | [diff] [blame] | 568 | res->f6i = spf6i; |
David Ahern | 7d21fec | 2019-04-16 14:36:11 -0700 | [diff] [blame] | 569 | goto out; |
David Ahern | 75ef738 | 2019-04-16 14:36:07 -0700 | [diff] [blame] | 570 | } |
| 571 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 | |
David Ahern | 75ef738 | 2019-04-16 14:36:07 -0700 | [diff] [blame] | 573 | if (oif && flags & RT6_LOOKUP_F_IFACE) { |
| 574 | res->f6i = net->ipv6.fib6_null_entry; |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 575 | nh = res->f6i->fib6_nh; |
David Ahern | 7d21fec | 2019-04-16 14:36:11 -0700 | [diff] [blame] | 576 | goto out; |
David Ahern | 75ef738 | 2019-04-16 14:36:07 -0700 | [diff] [blame] | 577 | } |
| 578 | |
David Ahern | f88d8ea | 2019-06-03 20:19:52 -0700 | [diff] [blame] | 579 | if (unlikely(f6i->nh)) { |
| 580 | nh = nexthop_fib6_nh(f6i->nh); |
| 581 | if (nexthop_is_blackhole(f6i->nh)) |
| 582 | goto out_blackhole; |
| 583 | } else { |
| 584 | nh = f6i->fib6_nh; |
| 585 | } |
| 586 | |
David Ahern | 7d21fec | 2019-04-16 14:36:11 -0700 | [diff] [blame] | 587 | if (nh->fib_nh_flags & RTNH_F_DEAD) { |
David Ahern | 75ef738 | 2019-04-16 14:36:07 -0700 | [diff] [blame] | 588 | res->f6i = net->ipv6.fib6_null_entry; |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 589 | nh = res->f6i->fib6_nh; |
David Ahern | 75ef738 | 2019-04-16 14:36:07 -0700 | [diff] [blame] | 590 | } |
David Ahern | 7d21fec | 2019-04-16 14:36:11 -0700 | [diff] [blame] | 591 | out: |
| 592 | res->nh = nh; |
| 593 | res->fib6_type = res->f6i->fib6_type; |
| 594 | res->fib6_flags = res->f6i->fib6_flags; |
David Ahern | f88d8ea | 2019-06-03 20:19:52 -0700 | [diff] [blame] | 595 | return; |
| 596 | |
| 597 | out_blackhole: |
| 598 | res->fib6_flags |= RTF_REJECT; |
| 599 | res->fib6_type = RTN_BLACKHOLE; |
| 600 | res->nh = nh; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | } |
| 602 | |
YOSHIFUJI Hideaki | 2709725 | 2006-03-20 17:05:13 -0800 | [diff] [blame] | 603 | #ifdef CONFIG_IPV6_ROUTER_PREF |
Hannes Frederic Sowa | c2f17e8 | 2013-10-21 06:17:15 +0200 | [diff] [blame] | 604 | struct __rt6_probe_work { |
| 605 | struct work_struct work; |
| 606 | struct in6_addr target; |
| 607 | struct net_device *dev; |
| 608 | }; |
| 609 | |
| 610 | static void rt6_probe_deferred(struct work_struct *w) |
| 611 | { |
| 612 | struct in6_addr mcaddr; |
| 613 | struct __rt6_probe_work *work = |
| 614 | container_of(w, struct __rt6_probe_work, work); |
| 615 | |
| 616 | addrconf_addr_solict_mult(&work->target, &mcaddr); |
Erik Nordmark | adc176c | 2016-12-02 14:00:08 -0800 | [diff] [blame] | 617 | ndisc_send_ns(work->dev, &work->target, &mcaddr, NULL, 0); |
Hannes Frederic Sowa | c2f17e8 | 2013-10-21 06:17:15 +0200 | [diff] [blame] | 618 | dev_put(work->dev); |
Michael Büsch | 662f553 | 2015-02-08 10:14:07 +0100 | [diff] [blame] | 619 | kfree(work); |
Hannes Frederic Sowa | c2f17e8 | 2013-10-21 06:17:15 +0200 | [diff] [blame] | 620 | } |
| 621 | |
David Ahern | cc3a86c | 2019-04-09 14:41:12 -0700 | [diff] [blame] | 622 | static void rt6_probe(struct fib6_nh *fib6_nh) |
YOSHIFUJI Hideaki | 2709725 | 2006-03-20 17:05:13 -0800 | [diff] [blame] | 623 | { |
Sabrina Dubroca | f547fac | 2018-10-12 16:22:47 +0200 | [diff] [blame] | 624 | struct __rt6_probe_work *work = NULL; |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 625 | const struct in6_addr *nh_gw; |
Eric Dumazet | 1bef4c2 | 2019-11-07 09:26:19 -0800 | [diff] [blame] | 626 | unsigned long last_probe; |
Eric Dumazet | f2c31e3 | 2011-07-29 19:00:53 +0000 | [diff] [blame] | 627 | struct neighbour *neigh; |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 628 | struct net_device *dev; |
Sabrina Dubroca | f547fac | 2018-10-12 16:22:47 +0200 | [diff] [blame] | 629 | struct inet6_dev *idev; |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 630 | |
YOSHIFUJI Hideaki | 2709725 | 2006-03-20 17:05:13 -0800 | [diff] [blame] | 631 | /* |
| 632 | * Okay, this does not seem to be appropriate |
| 633 | * for now, however, we need to check if it |
| 634 | * is really so; aka Router Reachability Probing. |
| 635 | * |
| 636 | * Router Reachability Probe MUST be rate-limited |
| 637 | * to no more than one per minute. |
| 638 | */ |
Hangbin Liu | 004b394 | 2019-11-20 15:39:06 +0800 | [diff] [blame] | 639 | if (!fib6_nh->fib_nh_gw_family) |
Amerigo Wang | fdd6681 | 2012-09-10 02:48:44 +0000 | [diff] [blame] | 640 | return; |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 641 | |
David Ahern | cc3a86c | 2019-04-09 14:41:12 -0700 | [diff] [blame] | 642 | nh_gw = &fib6_nh->fib_nh_gw6; |
| 643 | dev = fib6_nh->fib_nh_dev; |
YOSHIFUJI Hideaki / 吉藤英明 | 2152cae | 2013-01-17 12:53:43 +0000 | [diff] [blame] | 644 | rcu_read_lock_bh(); |
Eric Dumazet | 1bef4c2 | 2019-11-07 09:26:19 -0800 | [diff] [blame] | 645 | last_probe = READ_ONCE(fib6_nh->last_probe); |
Sabrina Dubroca | f547fac | 2018-10-12 16:22:47 +0200 | [diff] [blame] | 646 | idev = __in6_dev_get(dev); |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 647 | neigh = __ipv6_neigh_lookup_noref(dev, nh_gw); |
YOSHIFUJI Hideaki / 吉藤英明 | 2152cae | 2013-01-17 12:53:43 +0000 | [diff] [blame] | 648 | if (neigh) { |
Martin KaFai Lau | 8d6c31b | 2015-07-24 09:57:43 -0700 | [diff] [blame] | 649 | if (neigh->nud_state & NUD_VALID) |
| 650 | goto out; |
| 651 | |
YOSHIFUJI Hideaki / 吉藤英明 | 2152cae | 2013-01-17 12:53:43 +0000 | [diff] [blame] | 652 | write_lock(&neigh->lock); |
Martin KaFai Lau | 990edb4 | 2015-07-24 09:57:42 -0700 | [diff] [blame] | 653 | if (!(neigh->nud_state & NUD_VALID) && |
| 654 | time_after(jiffies, |
David Ahern | dcd1f57 | 2018-04-18 15:39:05 -0700 | [diff] [blame] | 655 | neigh->updated + idev->cnf.rtr_probe_interval)) { |
Martin KaFai Lau | 990edb4 | 2015-07-24 09:57:42 -0700 | [diff] [blame] | 656 | work = kmalloc(sizeof(*work), GFP_ATOMIC); |
| 657 | if (work) |
| 658 | __neigh_set_probe_once(neigh); |
Hannes Frederic Sowa | c2f17e8 | 2013-10-21 06:17:15 +0200 | [diff] [blame] | 659 | } |
YOSHIFUJI Hideaki / 吉藤英明 | 2152cae | 2013-01-17 12:53:43 +0000 | [diff] [blame] | 660 | write_unlock(&neigh->lock); |
Eric Dumazet | 1bef4c2 | 2019-11-07 09:26:19 -0800 | [diff] [blame] | 661 | } else if (time_after(jiffies, last_probe + |
Sabrina Dubroca | f547fac | 2018-10-12 16:22:47 +0200 | [diff] [blame] | 662 | idev->cnf.rtr_probe_interval)) { |
Martin KaFai Lau | 990edb4 | 2015-07-24 09:57:42 -0700 | [diff] [blame] | 663 | work = kmalloc(sizeof(*work), GFP_ATOMIC); |
Eric Dumazet | f2c31e3 | 2011-07-29 19:00:53 +0000 | [diff] [blame] | 664 | } |
Martin KaFai Lau | 990edb4 | 2015-07-24 09:57:42 -0700 | [diff] [blame] | 665 | |
Eric Dumazet | 1bef4c2 | 2019-11-07 09:26:19 -0800 | [diff] [blame] | 666 | if (!work || cmpxchg(&fib6_nh->last_probe, |
| 667 | last_probe, jiffies) != last_probe) { |
| 668 | kfree(work); |
| 669 | } else { |
Martin KaFai Lau | 990edb4 | 2015-07-24 09:57:42 -0700 | [diff] [blame] | 670 | INIT_WORK(&work->work, rt6_probe_deferred); |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 671 | work->target = *nh_gw; |
| 672 | dev_hold(dev); |
| 673 | work->dev = dev; |
Martin KaFai Lau | 990edb4 | 2015-07-24 09:57:42 -0700 | [diff] [blame] | 674 | schedule_work(&work->work); |
| 675 | } |
| 676 | |
Martin KaFai Lau | 8d6c31b | 2015-07-24 09:57:43 -0700 | [diff] [blame] | 677 | out: |
YOSHIFUJI Hideaki / 吉藤英明 | 2152cae | 2013-01-17 12:53:43 +0000 | [diff] [blame] | 678 | rcu_read_unlock_bh(); |
YOSHIFUJI Hideaki | 2709725 | 2006-03-20 17:05:13 -0800 | [diff] [blame] | 679 | } |
| 680 | #else |
David Ahern | cc3a86c | 2019-04-09 14:41:12 -0700 | [diff] [blame] | 681 | static inline void rt6_probe(struct fib6_nh *fib6_nh) |
YOSHIFUJI Hideaki | 2709725 | 2006-03-20 17:05:13 -0800 | [diff] [blame] | 682 | { |
YOSHIFUJI Hideaki | 2709725 | 2006-03-20 17:05:13 -0800 | [diff] [blame] | 683 | } |
| 684 | #endif |
| 685 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 686 | /* |
YOSHIFUJI Hideaki | 554cfb7 | 2006-03-20 17:00:26 -0800 | [diff] [blame] | 687 | * Default Router Selection (RFC 2461 6.3.6) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 688 | */ |
David Ahern | 1ba9a89 | 2019-04-09 14:41:10 -0700 | [diff] [blame] | 689 | 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] | 690 | { |
Hannes Frederic Sowa | afc154e | 2013-07-11 12:43:42 +0200 | [diff] [blame] | 691 | enum rt6_nud_state ret = RT6_NUD_FAIL_HARD; |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 692 | struct neighbour *neigh; |
Eric Dumazet | f2c31e3 | 2011-07-29 19:00:53 +0000 | [diff] [blame] | 693 | |
YOSHIFUJI Hideaki / 吉藤英明 | 145a362 | 2013-01-17 12:53:38 +0000 | [diff] [blame] | 694 | rcu_read_lock_bh(); |
David Ahern | 1ba9a89 | 2019-04-09 14:41:10 -0700 | [diff] [blame] | 695 | neigh = __ipv6_neigh_lookup_noref(fib6_nh->fib_nh_dev, |
| 696 | &fib6_nh->fib_nh_gw6); |
YOSHIFUJI Hideaki / 吉藤英明 | 145a362 | 2013-01-17 12:53:38 +0000 | [diff] [blame] | 697 | if (neigh) { |
| 698 | read_lock(&neigh->lock); |
YOSHIFUJI Hideaki | 554cfb7 | 2006-03-20 17:00:26 -0800 | [diff] [blame] | 699 | if (neigh->nud_state & NUD_VALID) |
Hannes Frederic Sowa | afc154e | 2013-07-11 12:43:42 +0200 | [diff] [blame] | 700 | ret = RT6_NUD_SUCCEED; |
YOSHIFUJI Hideaki | 398bcbe | 2008-01-19 00:35:16 -0800 | [diff] [blame] | 701 | #ifdef CONFIG_IPV6_ROUTER_PREF |
Paul Marks | a5a81f0 | 2012-12-03 10:26:54 +0000 | [diff] [blame] | 702 | else if (!(neigh->nud_state & NUD_FAILED)) |
Hannes Frederic Sowa | afc154e | 2013-07-11 12:43:42 +0200 | [diff] [blame] | 703 | ret = RT6_NUD_SUCCEED; |
Jiri Benc | 7e98056 | 2013-12-11 13:48:20 +0100 | [diff] [blame] | 704 | else |
| 705 | ret = RT6_NUD_FAIL_PROBE; |
YOSHIFUJI Hideaki | 398bcbe | 2008-01-19 00:35:16 -0800 | [diff] [blame] | 706 | #endif |
YOSHIFUJI Hideaki / 吉藤英明 | 145a362 | 2013-01-17 12:53:38 +0000 | [diff] [blame] | 707 | read_unlock(&neigh->lock); |
Hannes Frederic Sowa | afc154e | 2013-07-11 12:43:42 +0200 | [diff] [blame] | 708 | } else { |
| 709 | ret = IS_ENABLED(CONFIG_IPV6_ROUTER_PREF) ? |
Jiri Benc | 7e98056 | 2013-12-11 13:48:20 +0100 | [diff] [blame] | 710 | RT6_NUD_SUCCEED : RT6_NUD_FAIL_DO_RR; |
Paul Marks | a5a81f0 | 2012-12-03 10:26:54 +0000 | [diff] [blame] | 711 | } |
YOSHIFUJI Hideaki / 吉藤英明 | 145a362 | 2013-01-17 12:53:38 +0000 | [diff] [blame] | 712 | rcu_read_unlock_bh(); |
| 713 | |
Paul Marks | a5a81f0 | 2012-12-03 10:26:54 +0000 | [diff] [blame] | 714 | return ret; |
YOSHIFUJI Hideaki | 554cfb7 | 2006-03-20 17:00:26 -0800 | [diff] [blame] | 715 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 716 | |
David Ahern | 702cea5 | 2019-04-09 14:41:13 -0700 | [diff] [blame] | 717 | static int rt6_score_route(const struct fib6_nh *nh, u32 fib6_flags, int oif, |
| 718 | int strict) |
YOSHIFUJI Hideaki | 554cfb7 | 2006-03-20 17:00:26 -0800 | [diff] [blame] | 719 | { |
David Ahern | 6e1809a | 2019-04-09 14:41:11 -0700 | [diff] [blame] | 720 | int m = 0; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 721 | |
David Ahern | 6e1809a | 2019-04-09 14:41:11 -0700 | [diff] [blame] | 722 | if (!oif || nh->fib_nh_dev->ifindex == oif) |
| 723 | m = 2; |
| 724 | |
YOSHIFUJI Hideaki | 77d16f4 | 2006-08-23 17:25:05 -0700 | [diff] [blame] | 725 | if (!m && (strict & RT6_LOOKUP_F_IFACE)) |
Hannes Frederic Sowa | afc154e | 2013-07-11 12:43:42 +0200 | [diff] [blame] | 726 | return RT6_NUD_FAIL_HARD; |
YOSHIFUJI Hideaki | ebacaaa | 2006-03-20 17:04:53 -0800 | [diff] [blame] | 727 | #ifdef CONFIG_IPV6_ROUTER_PREF |
David Ahern | 702cea5 | 2019-04-09 14:41:13 -0700 | [diff] [blame] | 728 | m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(fib6_flags)) << 2; |
YOSHIFUJI Hideaki | ebacaaa | 2006-03-20 17:04:53 -0800 | [diff] [blame] | 729 | #endif |
David Ahern | 1ba9a89 | 2019-04-09 14:41:10 -0700 | [diff] [blame] | 730 | if ((strict & RT6_LOOKUP_F_REACHABLE) && |
David Ahern | 702cea5 | 2019-04-09 14:41:13 -0700 | [diff] [blame] | 731 | !(fib6_flags & RTF_NONEXTHOP) && nh->fib_nh_gw_family) { |
David Ahern | 1ba9a89 | 2019-04-09 14:41:10 -0700 | [diff] [blame] | 732 | int n = rt6_check_neigh(nh); |
Hannes Frederic Sowa | afc154e | 2013-07-11 12:43:42 +0200 | [diff] [blame] | 733 | if (n < 0) |
| 734 | return n; |
| 735 | } |
YOSHIFUJI Hideaki | 554cfb7 | 2006-03-20 17:00:26 -0800 | [diff] [blame] | 736 | return m; |
| 737 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 738 | |
David Ahern | 28679ed | 2019-04-09 14:41:14 -0700 | [diff] [blame] | 739 | static bool find_match(struct fib6_nh *nh, u32 fib6_flags, |
| 740 | int oif, int strict, int *mpri, bool *do_rr) |
YOSHIFUJI Hideaki | 554cfb7 | 2006-03-20 17:00:26 -0800 | [diff] [blame] | 741 | { |
Hannes Frederic Sowa | afc154e | 2013-07-11 12:43:42 +0200 | [diff] [blame] | 742 | bool match_do_rr = false; |
David Ahern | 28679ed | 2019-04-09 14:41:14 -0700 | [diff] [blame] | 743 | bool rc = false; |
| 744 | int m; |
Andy Gospodarek | 35103d1 | 2015-08-13 10:39:01 -0400 | [diff] [blame] | 745 | |
David Ahern | 28679ed | 2019-04-09 14:41:14 -0700 | [diff] [blame] | 746 | if (nh->fib_nh_flags & RTNH_F_DEAD) |
Ido Schimmel | 8067bb8 | 2018-01-07 12:45:09 +0200 | [diff] [blame] | 747 | goto out; |
| 748 | |
David Ahern | 28679ed | 2019-04-09 14:41:14 -0700 | [diff] [blame] | 749 | if (ip6_ignore_linkdown(nh->fib_nh_dev) && |
| 750 | nh->fib_nh_flags & RTNH_F_LINKDOWN && |
David Ahern | d5d32e4 | 2016-10-24 12:27:23 -0700 | [diff] [blame] | 751 | !(strict & RT6_LOOKUP_F_IGNORE_LINKSTATE)) |
Andy Gospodarek | 35103d1 | 2015-08-13 10:39:01 -0400 | [diff] [blame] | 752 | goto out; |
David S. Miller | f11e665 | 2007-03-24 20:36:25 -0700 | [diff] [blame] | 753 | |
David Ahern | 28679ed | 2019-04-09 14:41:14 -0700 | [diff] [blame] | 754 | m = rt6_score_route(nh, fib6_flags, oif, strict); |
Jiri Benc | 7e98056 | 2013-12-11 13:48:20 +0100 | [diff] [blame] | 755 | if (m == RT6_NUD_FAIL_DO_RR) { |
Hannes Frederic Sowa | afc154e | 2013-07-11 12:43:42 +0200 | [diff] [blame] | 756 | match_do_rr = true; |
| 757 | m = 0; /* lowest valid score */ |
Jiri Benc | 7e98056 | 2013-12-11 13:48:20 +0100 | [diff] [blame] | 758 | } else if (m == RT6_NUD_FAIL_HARD) { |
David S. Miller | f11e665 | 2007-03-24 20:36:25 -0700 | [diff] [blame] | 759 | goto out; |
David S. Miller | f11e665 | 2007-03-24 20:36:25 -0700 | [diff] [blame] | 760 | } |
| 761 | |
Hannes Frederic Sowa | afc154e | 2013-07-11 12:43:42 +0200 | [diff] [blame] | 762 | if (strict & RT6_LOOKUP_F_REACHABLE) |
David Ahern | 28679ed | 2019-04-09 14:41:14 -0700 | [diff] [blame] | 763 | rt6_probe(nh); |
Hannes Frederic Sowa | afc154e | 2013-07-11 12:43:42 +0200 | [diff] [blame] | 764 | |
Jiri Benc | 7e98056 | 2013-12-11 13:48:20 +0100 | [diff] [blame] | 765 | /* 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] | 766 | if (m > *mpri) { |
| 767 | *do_rr = match_do_rr; |
| 768 | *mpri = m; |
David Ahern | 28679ed | 2019-04-09 14:41:14 -0700 | [diff] [blame] | 769 | rc = true; |
Hannes Frederic Sowa | afc154e | 2013-07-11 12:43:42 +0200 | [diff] [blame] | 770 | } |
David S. Miller | f11e665 | 2007-03-24 20:36:25 -0700 | [diff] [blame] | 771 | out: |
David Ahern | 28679ed | 2019-04-09 14:41:14 -0700 | [diff] [blame] | 772 | return rc; |
David S. Miller | f11e665 | 2007-03-24 20:36:25 -0700 | [diff] [blame] | 773 | } |
| 774 | |
David Ahern | 17a5984 | 2019-06-08 14:53:25 -0700 | [diff] [blame] | 775 | struct fib6_nh_frl_arg { |
| 776 | u32 flags; |
| 777 | int oif; |
| 778 | int strict; |
| 779 | int *mpri; |
| 780 | bool *do_rr; |
| 781 | struct fib6_nh *nh; |
| 782 | }; |
| 783 | |
| 784 | static int rt6_nh_find_match(struct fib6_nh *nh, void *_arg) |
| 785 | { |
| 786 | struct fib6_nh_frl_arg *arg = _arg; |
| 787 | |
| 788 | arg->nh = nh; |
| 789 | return find_match(nh, arg->flags, arg->oif, arg->strict, |
| 790 | arg->mpri, arg->do_rr); |
| 791 | } |
| 792 | |
David Ahern | b7bc4b6 | 2019-04-16 14:36:08 -0700 | [diff] [blame] | 793 | static void __find_rr_leaf(struct fib6_info *f6i_start, |
David Ahern | 30c15f0 | 2019-04-09 14:41:15 -0700 | [diff] [blame] | 794 | struct fib6_info *nomatch, u32 metric, |
David Ahern | b7bc4b6 | 2019-04-16 14:36:08 -0700 | [diff] [blame] | 795 | struct fib6_result *res, struct fib6_info **cont, |
David Ahern | 30c15f0 | 2019-04-09 14:41:15 -0700 | [diff] [blame] | 796 | int oif, int strict, bool *do_rr, int *mpri) |
David S. Miller | f11e665 | 2007-03-24 20:36:25 -0700 | [diff] [blame] | 797 | { |
David Ahern | b7bc4b6 | 2019-04-16 14:36:08 -0700 | [diff] [blame] | 798 | struct fib6_info *f6i; |
David Ahern | 30c15f0 | 2019-04-09 14:41:15 -0700 | [diff] [blame] | 799 | |
David Ahern | b7bc4b6 | 2019-04-16 14:36:08 -0700 | [diff] [blame] | 800 | for (f6i = f6i_start; |
| 801 | f6i && f6i != nomatch; |
| 802 | f6i = rcu_dereference(f6i->fib6_next)) { |
David Ahern | 17a5984 | 2019-06-08 14:53:25 -0700 | [diff] [blame] | 803 | bool matched = false; |
David Ahern | 30c15f0 | 2019-04-09 14:41:15 -0700 | [diff] [blame] | 804 | struct fib6_nh *nh; |
| 805 | |
David Ahern | b7bc4b6 | 2019-04-16 14:36:08 -0700 | [diff] [blame] | 806 | if (cont && f6i->fib6_metric != metric) { |
| 807 | *cont = f6i; |
David Ahern | 30c15f0 | 2019-04-09 14:41:15 -0700 | [diff] [blame] | 808 | return; |
| 809 | } |
| 810 | |
David Ahern | b7bc4b6 | 2019-04-16 14:36:08 -0700 | [diff] [blame] | 811 | if (fib6_check_expired(f6i)) |
David Ahern | 30c15f0 | 2019-04-09 14:41:15 -0700 | [diff] [blame] | 812 | continue; |
| 813 | |
David Ahern | 17a5984 | 2019-06-08 14:53:25 -0700 | [diff] [blame] | 814 | if (unlikely(f6i->nh)) { |
| 815 | struct fib6_nh_frl_arg arg = { |
| 816 | .flags = f6i->fib6_flags, |
| 817 | .oif = oif, |
| 818 | .strict = strict, |
| 819 | .mpri = mpri, |
| 820 | .do_rr = do_rr |
| 821 | }; |
| 822 | |
| 823 | if (nexthop_is_blackhole(f6i->nh)) { |
| 824 | res->fib6_flags = RTF_REJECT; |
| 825 | res->fib6_type = RTN_BLACKHOLE; |
| 826 | res->f6i = f6i; |
| 827 | res->nh = nexthop_fib6_nh(f6i->nh); |
| 828 | return; |
| 829 | } |
| 830 | if (nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_find_match, |
| 831 | &arg)) { |
| 832 | matched = true; |
| 833 | nh = arg.nh; |
| 834 | } |
| 835 | } else { |
| 836 | nh = f6i->fib6_nh; |
| 837 | if (find_match(nh, f6i->fib6_flags, oif, strict, |
| 838 | mpri, do_rr)) |
| 839 | matched = true; |
| 840 | } |
| 841 | if (matched) { |
David Ahern | b7bc4b6 | 2019-04-16 14:36:08 -0700 | [diff] [blame] | 842 | res->f6i = f6i; |
| 843 | res->nh = nh; |
David Ahern | 7d21fec | 2019-04-16 14:36:11 -0700 | [diff] [blame] | 844 | res->fib6_flags = f6i->fib6_flags; |
| 845 | res->fib6_type = f6i->fib6_type; |
David Ahern | b7bc4b6 | 2019-04-16 14:36:08 -0700 | [diff] [blame] | 846 | } |
David Ahern | 30c15f0 | 2019-04-09 14:41:15 -0700 | [diff] [blame] | 847 | } |
| 848 | } |
| 849 | |
David Ahern | b7bc4b6 | 2019-04-16 14:36:08 -0700 | [diff] [blame] | 850 | static void find_rr_leaf(struct fib6_node *fn, struct fib6_info *leaf, |
| 851 | struct fib6_info *rr_head, int oif, int strict, |
| 852 | bool *do_rr, struct fib6_result *res) |
David Ahern | 30c15f0 | 2019-04-09 14:41:15 -0700 | [diff] [blame] | 853 | { |
David Ahern | b7bc4b6 | 2019-04-16 14:36:08 -0700 | [diff] [blame] | 854 | u32 metric = rr_head->fib6_metric; |
| 855 | struct fib6_info *cont = NULL; |
YOSHIFUJI Hideaki | 554cfb7 | 2006-03-20 17:00:26 -0800 | [diff] [blame] | 856 | int mpri = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 857 | |
David Ahern | b7bc4b6 | 2019-04-16 14:36:08 -0700 | [diff] [blame] | 858 | __find_rr_leaf(rr_head, NULL, metric, res, &cont, |
David Ahern | 30c15f0 | 2019-04-09 14:41:15 -0700 | [diff] [blame] | 859 | oif, strict, do_rr, &mpri); |
Steffen Klassert | 9fbdcfa | 2015-04-28 13:03:04 -0700 | [diff] [blame] | 860 | |
David Ahern | b7bc4b6 | 2019-04-16 14:36:08 -0700 | [diff] [blame] | 861 | __find_rr_leaf(leaf, rr_head, 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 | if (res->f6i || !cont) |
| 865 | return; |
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 | __find_rr_leaf(cont, NULL, metric, res, NULL, |
David Ahern | 30c15f0 | 2019-04-09 14:41:15 -0700 | [diff] [blame] | 868 | oif, strict, do_rr, &mpri); |
David S. Miller | f11e665 | 2007-03-24 20:36:25 -0700 | [diff] [blame] | 869 | } |
YOSHIFUJI Hideaki | 554cfb7 | 2006-03-20 17:00:26 -0800 | [diff] [blame] | 870 | |
David Ahern | b7bc4b6 | 2019-04-16 14:36:08 -0700 | [diff] [blame] | 871 | static void rt6_select(struct net *net, struct fib6_node *fn, int oif, |
| 872 | struct fib6_result *res, int strict) |
David S. Miller | f11e665 | 2007-03-24 20:36:25 -0700 | [diff] [blame] | 873 | { |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 874 | struct fib6_info *leaf = rcu_dereference(fn->leaf); |
David Ahern | b7bc4b6 | 2019-04-16 14:36:08 -0700 | [diff] [blame] | 875 | struct fib6_info *rt0; |
Hannes Frederic Sowa | afc154e | 2013-07-11 12:43:42 +0200 | [diff] [blame] | 876 | bool do_rr = false; |
Wei Wang | 17ecf59 | 2017-10-06 12:06:09 -0700 | [diff] [blame] | 877 | int key_plen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 878 | |
David Ahern | b7bc4b6 | 2019-04-16 14:36:08 -0700 | [diff] [blame] | 879 | /* make sure this function or its helpers sets f6i */ |
| 880 | res->f6i = NULL; |
| 881 | |
David Ahern | 421842e | 2018-04-17 17:33:18 -0700 | [diff] [blame] | 882 | if (!leaf || leaf == net->ipv6.fib6_null_entry) |
David Ahern | b7bc4b6 | 2019-04-16 14:36:08 -0700 | [diff] [blame] | 883 | goto out; |
Wei Wang | 8d1040e | 2017-10-06 12:06:08 -0700 | [diff] [blame] | 884 | |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 885 | rt0 = rcu_dereference(fn->rr_ptr); |
David S. Miller | f11e665 | 2007-03-24 20:36:25 -0700 | [diff] [blame] | 886 | if (!rt0) |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 887 | rt0 = leaf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 888 | |
Wei Wang | 17ecf59 | 2017-10-06 12:06:09 -0700 | [diff] [blame] | 889 | /* Double check to make sure fn is not an intermediate node |
| 890 | * and fn->leaf does not points to its child's leaf |
| 891 | * (This might happen if all routes under fn are deleted from |
| 892 | * the tree and fib6_repair_tree() is called on the node.) |
| 893 | */ |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 894 | key_plen = rt0->fib6_dst.plen; |
Wei Wang | 17ecf59 | 2017-10-06 12:06:09 -0700 | [diff] [blame] | 895 | #ifdef CONFIG_IPV6_SUBTREES |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 896 | if (rt0->fib6_src.plen) |
| 897 | key_plen = rt0->fib6_src.plen; |
Wei Wang | 17ecf59 | 2017-10-06 12:06:09 -0700 | [diff] [blame] | 898 | #endif |
| 899 | if (fn->fn_bit != key_plen) |
David Ahern | b7bc4b6 | 2019-04-16 14:36:08 -0700 | [diff] [blame] | 900 | goto out; |
Wei Wang | 17ecf59 | 2017-10-06 12:06:09 -0700 | [diff] [blame] | 901 | |
David Ahern | b7bc4b6 | 2019-04-16 14:36:08 -0700 | [diff] [blame] | 902 | find_rr_leaf(fn, leaf, rt0, oif, strict, &do_rr, res); |
Hannes Frederic Sowa | afc154e | 2013-07-11 12:43:42 +0200 | [diff] [blame] | 903 | if (do_rr) { |
David Ahern | 8fb11a9 | 2018-05-04 13:54:24 -0700 | [diff] [blame] | 904 | struct fib6_info *next = rcu_dereference(rt0->fib6_next); |
David S. Miller | f11e665 | 2007-03-24 20:36:25 -0700 | [diff] [blame] | 905 | |
YOSHIFUJI Hideaki | 554cfb7 | 2006-03-20 17:00:26 -0800 | [diff] [blame] | 906 | /* no entries matched; do round-robin */ |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 907 | if (!next || next->fib6_metric != rt0->fib6_metric) |
Wei Wang | 8d1040e | 2017-10-06 12:06:08 -0700 | [diff] [blame] | 908 | next = leaf; |
David S. Miller | f11e665 | 2007-03-24 20:36:25 -0700 | [diff] [blame] | 909 | |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 910 | if (next != rt0) { |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 911 | spin_lock_bh(&leaf->fib6_table->tb6_lock); |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 912 | /* make sure next is not being deleted from the tree */ |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 913 | if (next->fib6_node) |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 914 | rcu_assign_pointer(fn->rr_ptr, next); |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 915 | spin_unlock_bh(&leaf->fib6_table->tb6_lock); |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 916 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 917 | } |
| 918 | |
David Ahern | b7bc4b6 | 2019-04-16 14:36:08 -0700 | [diff] [blame] | 919 | out: |
| 920 | if (!res->f6i) { |
| 921 | res->f6i = net->ipv6.fib6_null_entry; |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 922 | res->nh = res->f6i->fib6_nh; |
David Ahern | 7d21fec | 2019-04-16 14:36:11 -0700 | [diff] [blame] | 923 | res->fib6_flags = res->f6i->fib6_flags; |
| 924 | res->fib6_type = res->f6i->fib6_type; |
David Ahern | b7bc4b6 | 2019-04-16 14:36:08 -0700 | [diff] [blame] | 925 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 926 | } |
| 927 | |
David Ahern | 85bd05d | 2019-04-16 14:36:01 -0700 | [diff] [blame] | 928 | 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] | 929 | { |
David Ahern | 85bd05d | 2019-04-16 14:36:01 -0700 | [diff] [blame] | 930 | return (res->f6i->fib6_flags & RTF_NONEXTHOP) || |
| 931 | res->nh->fib_nh_gw_family; |
Martin KaFai Lau | 8b9df26 | 2015-05-22 20:55:59 -0700 | [diff] [blame] | 932 | } |
| 933 | |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 934 | #ifdef CONFIG_IPV6_ROUTE_INFO |
| 935 | int rt6_route_rcv(struct net_device *dev, u8 *opt, int len, |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 936 | const struct in6_addr *gwaddr) |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 937 | { |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 938 | struct net *net = dev_net(dev); |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 939 | struct route_info *rinfo = (struct route_info *) opt; |
| 940 | struct in6_addr prefix_buf, *prefix; |
| 941 | unsigned int pref; |
YOSHIFUJI Hideaki | 4bed72e | 2008-05-27 17:37:49 +0900 | [diff] [blame] | 942 | unsigned long lifetime; |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 943 | struct fib6_info *rt; |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 944 | |
| 945 | if (len < sizeof(struct route_info)) { |
| 946 | return -EINVAL; |
| 947 | } |
| 948 | |
| 949 | /* Sanity check for prefix_len and length */ |
| 950 | if (rinfo->length > 3) { |
| 951 | return -EINVAL; |
| 952 | } else if (rinfo->prefix_len > 128) { |
| 953 | return -EINVAL; |
| 954 | } else if (rinfo->prefix_len > 64) { |
| 955 | if (rinfo->length < 2) { |
| 956 | return -EINVAL; |
| 957 | } |
| 958 | } else if (rinfo->prefix_len > 0) { |
| 959 | if (rinfo->length < 1) { |
| 960 | return -EINVAL; |
| 961 | } |
| 962 | } |
| 963 | |
| 964 | pref = rinfo->route_pref; |
| 965 | if (pref == ICMPV6_ROUTER_PREF_INVALID) |
Jens Rosenboom | 3933fc9 | 2009-09-10 06:25:11 +0000 | [diff] [blame] | 966 | return -EINVAL; |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 967 | |
YOSHIFUJI Hideaki | 4bed72e | 2008-05-27 17:37:49 +0900 | [diff] [blame] | 968 | lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ); |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 969 | |
| 970 | if (rinfo->length == 3) |
| 971 | prefix = (struct in6_addr *)rinfo->prefix; |
| 972 | else { |
| 973 | /* this function is safe */ |
| 974 | ipv6_addr_prefix(&prefix_buf, |
| 975 | (struct in6_addr *)rinfo->prefix, |
| 976 | rinfo->prefix_len); |
| 977 | prefix = &prefix_buf; |
| 978 | } |
| 979 | |
Duan Jiong | f104a56 | 2013-11-08 09:56:53 +0800 | [diff] [blame] | 980 | if (rinfo->prefix_len == 0) |
David Ahern | afb1d4b5 | 2018-04-17 17:33:11 -0700 | [diff] [blame] | 981 | rt = rt6_get_dflt_router(net, gwaddr, dev); |
Duan Jiong | f104a56 | 2013-11-08 09:56:53 +0800 | [diff] [blame] | 982 | else |
| 983 | rt = rt6_get_route_info(net, prefix, rinfo->prefix_len, |
David Ahern | 830218c | 2016-10-24 10:52:35 -0700 | [diff] [blame] | 984 | gwaddr, dev); |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 985 | |
| 986 | if (rt && !lifetime) { |
Roopa Prabhu | 11dd74b | 2020-04-27 13:56:45 -0700 | [diff] [blame] | 987 | ip6_del_rt(net, rt, false); |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 988 | rt = NULL; |
| 989 | } |
| 990 | |
| 991 | if (!rt && lifetime) |
David Ahern | 830218c | 2016-10-24 10:52:35 -0700 | [diff] [blame] | 992 | rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr, |
| 993 | dev, pref); |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 994 | else if (rt) |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 995 | rt->fib6_flags = RTF_ROUTEINFO | |
| 996 | (rt->fib6_flags & ~RTF_PREF_MASK) | RTF_PREF(pref); |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 997 | |
| 998 | if (rt) { |
Gao feng | 1716a96 | 2012-04-06 00:13:10 +0000 | [diff] [blame] | 999 | if (!addrconf_finite_timeout(lifetime)) |
David Ahern | 1489568 | 2018-04-17 17:33:17 -0700 | [diff] [blame] | 1000 | fib6_clean_expires(rt); |
Gao feng | 1716a96 | 2012-04-06 00:13:10 +0000 | [diff] [blame] | 1001 | else |
David Ahern | 1489568 | 2018-04-17 17:33:17 -0700 | [diff] [blame] | 1002 | fib6_set_expires(rt, jiffies + HZ * lifetime); |
Gao feng | 1716a96 | 2012-04-06 00:13:10 +0000 | [diff] [blame] | 1003 | |
David Ahern | 93531c6 | 2018-04-17 17:33:25 -0700 | [diff] [blame] | 1004 | fib6_info_release(rt); |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 1005 | } |
| 1006 | return 0; |
| 1007 | } |
| 1008 | #endif |
| 1009 | |
David Ahern | ae90d86 | 2018-04-17 17:33:12 -0700 | [diff] [blame] | 1010 | /* |
| 1011 | * Misc support functions |
| 1012 | */ |
| 1013 | |
| 1014 | /* called with rcu_lock held */ |
David Ahern | 0d16158 | 2019-04-16 14:36:04 -0700 | [diff] [blame] | 1015 | 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] | 1016 | { |
David Ahern | 0d16158 | 2019-04-16 14:36:04 -0700 | [diff] [blame] | 1017 | struct net_device *dev = res->nh->fib_nh_dev; |
David Ahern | ae90d86 | 2018-04-17 17:33:12 -0700 | [diff] [blame] | 1018 | |
David Ahern | 7d21fec | 2019-04-16 14:36:11 -0700 | [diff] [blame] | 1019 | if (res->fib6_flags & (RTF_LOCAL | RTF_ANYCAST)) { |
David Ahern | ae90d86 | 2018-04-17 17:33:12 -0700 | [diff] [blame] | 1020 | /* for copies of local routes, dst->dev needs to be the |
| 1021 | * device if it is a master device, the master device if |
| 1022 | * device is enslaved, and the loopback as the default |
| 1023 | */ |
| 1024 | if (netif_is_l3_slave(dev) && |
David Ahern | 7d21fec | 2019-04-16 14:36:11 -0700 | [diff] [blame] | 1025 | !rt6_need_strict(&res->f6i->fib6_dst.addr)) |
David Ahern | ae90d86 | 2018-04-17 17:33:12 -0700 | [diff] [blame] | 1026 | dev = l3mdev_master_dev_rcu(dev); |
| 1027 | else if (!netif_is_l3_master(dev)) |
| 1028 | dev = dev_net(dev)->loopback_dev; |
| 1029 | /* last case is netif_is_l3_master(dev) is true in which |
| 1030 | * case we want dev returned to be dev |
| 1031 | */ |
| 1032 | } |
| 1033 | |
| 1034 | return dev; |
| 1035 | } |
| 1036 | |
David Ahern | 6edb3c9 | 2018-04-17 17:33:15 -0700 | [diff] [blame] | 1037 | static const int fib6_prop[RTN_MAX + 1] = { |
| 1038 | [RTN_UNSPEC] = 0, |
| 1039 | [RTN_UNICAST] = 0, |
| 1040 | [RTN_LOCAL] = 0, |
| 1041 | [RTN_BROADCAST] = 0, |
| 1042 | [RTN_ANYCAST] = 0, |
| 1043 | [RTN_MULTICAST] = 0, |
| 1044 | [RTN_BLACKHOLE] = -EINVAL, |
| 1045 | [RTN_UNREACHABLE] = -EHOSTUNREACH, |
| 1046 | [RTN_PROHIBIT] = -EACCES, |
| 1047 | [RTN_THROW] = -EAGAIN, |
| 1048 | [RTN_NAT] = -EINVAL, |
| 1049 | [RTN_XRESOLVE] = -EINVAL, |
| 1050 | }; |
| 1051 | |
| 1052 | static int ip6_rt_type_to_error(u8 fib6_type) |
| 1053 | { |
| 1054 | return fib6_prop[fib6_type]; |
| 1055 | } |
| 1056 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 1057 | static unsigned short fib6_info_dst_flags(struct fib6_info *rt) |
David Ahern | 3b6761d | 2018-04-17 17:33:20 -0700 | [diff] [blame] | 1058 | { |
| 1059 | unsigned short flags = 0; |
| 1060 | |
| 1061 | if (rt->dst_nocount) |
| 1062 | flags |= DST_NOCOUNT; |
| 1063 | if (rt->dst_nopolicy) |
| 1064 | flags |= DST_NOPOLICY; |
David Ahern | 3b6761d | 2018-04-17 17:33:20 -0700 | [diff] [blame] | 1065 | |
| 1066 | return flags; |
| 1067 | } |
| 1068 | |
David Ahern | 7d21fec | 2019-04-16 14:36:11 -0700 | [diff] [blame] | 1069 | 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] | 1070 | { |
David Ahern | 7d21fec | 2019-04-16 14:36:11 -0700 | [diff] [blame] | 1071 | rt->dst.error = ip6_rt_type_to_error(fib6_type); |
David Ahern | 6edb3c9 | 2018-04-17 17:33:15 -0700 | [diff] [blame] | 1072 | |
David Ahern | 7d21fec | 2019-04-16 14:36:11 -0700 | [diff] [blame] | 1073 | switch (fib6_type) { |
David Ahern | 6edb3c9 | 2018-04-17 17:33:15 -0700 | [diff] [blame] | 1074 | case RTN_BLACKHOLE: |
| 1075 | rt->dst.output = dst_discard_out; |
| 1076 | rt->dst.input = dst_discard; |
| 1077 | break; |
| 1078 | case RTN_PROHIBIT: |
| 1079 | rt->dst.output = ip6_pkt_prohibit_out; |
| 1080 | rt->dst.input = ip6_pkt_prohibit; |
| 1081 | break; |
| 1082 | case RTN_THROW: |
| 1083 | case RTN_UNREACHABLE: |
| 1084 | default: |
| 1085 | rt->dst.output = ip6_pkt_discard_out; |
| 1086 | rt->dst.input = ip6_pkt_discard; |
| 1087 | break; |
| 1088 | } |
| 1089 | } |
| 1090 | |
David Ahern | 0d16158 | 2019-04-16 14:36:04 -0700 | [diff] [blame] | 1091 | 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] | 1092 | { |
David Ahern | 7d21fec | 2019-04-16 14:36:11 -0700 | [diff] [blame] | 1093 | struct fib6_info *f6i = res->f6i; |
David Ahern | 0d16158 | 2019-04-16 14:36:04 -0700 | [diff] [blame] | 1094 | |
David Ahern | 7d21fec | 2019-04-16 14:36:11 -0700 | [diff] [blame] | 1095 | if (res->fib6_flags & RTF_REJECT) { |
| 1096 | ip6_rt_init_dst_reject(rt, res->fib6_type); |
David Ahern | 6edb3c9 | 2018-04-17 17:33:15 -0700 | [diff] [blame] | 1097 | return; |
| 1098 | } |
| 1099 | |
| 1100 | rt->dst.error = 0; |
| 1101 | rt->dst.output = ip6_output; |
| 1102 | |
David Ahern | 7d21fec | 2019-04-16 14:36:11 -0700 | [diff] [blame] | 1103 | if (res->fib6_type == RTN_LOCAL || res->fib6_type == RTN_ANYCAST) { |
David Ahern | 6edb3c9 | 2018-04-17 17:33:15 -0700 | [diff] [blame] | 1104 | rt->dst.input = ip6_input; |
David Ahern | 7d21fec | 2019-04-16 14:36:11 -0700 | [diff] [blame] | 1105 | } else if (ipv6_addr_type(&f6i->fib6_dst.addr) & IPV6_ADDR_MULTICAST) { |
David Ahern | 6edb3c9 | 2018-04-17 17:33:15 -0700 | [diff] [blame] | 1106 | rt->dst.input = ip6_mc_input; |
| 1107 | } else { |
| 1108 | rt->dst.input = ip6_forward; |
| 1109 | } |
| 1110 | |
David Ahern | 0d16158 | 2019-04-16 14:36:04 -0700 | [diff] [blame] | 1111 | if (res->nh->fib_nh_lws) { |
| 1112 | rt->dst.lwtstate = lwtstate_get(res->nh->fib_nh_lws); |
David Ahern | 6edb3c9 | 2018-04-17 17:33:15 -0700 | [diff] [blame] | 1113 | lwtunnel_set_redirect(&rt->dst); |
| 1114 | } |
| 1115 | |
| 1116 | rt->dst.lastuse = jiffies; |
| 1117 | } |
| 1118 | |
Wei Wang | e873e4b | 2018-07-21 20:56:32 -0700 | [diff] [blame] | 1119 | /* Caller must already hold reference to @from */ |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 1120 | 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] | 1121 | { |
David Ahern | ae90d86 | 2018-04-17 17:33:12 -0700 | [diff] [blame] | 1122 | rt->rt6i_flags &= ~RTF_EXPIRES; |
David Ahern | a68886a | 2018-04-20 15:38:02 -0700 | [diff] [blame] | 1123 | rcu_assign_pointer(rt->from, from); |
David Ahern | e1255ed | 2018-10-04 20:07:53 -0700 | [diff] [blame] | 1124 | ip_dst_init_metrics(&rt->dst, from->fib6_metrics); |
David Ahern | ae90d86 | 2018-04-17 17:33:12 -0700 | [diff] [blame] | 1125 | } |
| 1126 | |
David Ahern | 0d16158 | 2019-04-16 14:36:04 -0700 | [diff] [blame] | 1127 | /* Caller must already hold reference to f6i in result */ |
| 1128 | 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] | 1129 | { |
David Ahern | 0d16158 | 2019-04-16 14:36:04 -0700 | [diff] [blame] | 1130 | const struct fib6_nh *nh = res->nh; |
| 1131 | const struct net_device *dev = nh->fib_nh_dev; |
| 1132 | struct fib6_info *f6i = res->f6i; |
David Ahern | dcd1f57 | 2018-04-18 15:39:05 -0700 | [diff] [blame] | 1133 | |
David Ahern | 0d16158 | 2019-04-16 14:36:04 -0700 | [diff] [blame] | 1134 | ip6_rt_init_dst(rt, res); |
David Ahern | 6edb3c9 | 2018-04-17 17:33:15 -0700 | [diff] [blame] | 1135 | |
David Ahern | 0d16158 | 2019-04-16 14:36:04 -0700 | [diff] [blame] | 1136 | rt->rt6i_dst = f6i->fib6_dst; |
David Ahern | dcd1f57 | 2018-04-18 15:39:05 -0700 | [diff] [blame] | 1137 | rt->rt6i_idev = dev ? in6_dev_get(dev) : NULL; |
David Ahern | 7d21fec | 2019-04-16 14:36:11 -0700 | [diff] [blame] | 1138 | rt->rt6i_flags = res->fib6_flags; |
David Ahern | 0d16158 | 2019-04-16 14:36:04 -0700 | [diff] [blame] | 1139 | if (nh->fib_nh_gw_family) { |
| 1140 | rt->rt6i_gateway = nh->fib_nh_gw6; |
David Ahern | 2b2450c | 2019-03-27 20:53:52 -0700 | [diff] [blame] | 1141 | rt->rt6i_flags |= RTF_GATEWAY; |
| 1142 | } |
David Ahern | 0d16158 | 2019-04-16 14:36:04 -0700 | [diff] [blame] | 1143 | rt6_set_from(rt, f6i); |
David Ahern | ae90d86 | 2018-04-17 17:33:12 -0700 | [diff] [blame] | 1144 | #ifdef CONFIG_IPV6_SUBTREES |
David Ahern | 0d16158 | 2019-04-16 14:36:04 -0700 | [diff] [blame] | 1145 | rt->rt6i_src = f6i->fib6_src; |
David Ahern | ae90d86 | 2018-04-17 17:33:12 -0700 | [diff] [blame] | 1146 | #endif |
David Ahern | ae90d86 | 2018-04-17 17:33:12 -0700 | [diff] [blame] | 1147 | } |
| 1148 | |
Martin KaFai Lau | a3c00e4 | 2014-10-20 13:42:43 -0700 | [diff] [blame] | 1149 | static struct fib6_node* fib6_backtrack(struct fib6_node *fn, |
| 1150 | struct in6_addr *saddr) |
| 1151 | { |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 1152 | struct fib6_node *pn, *sn; |
Martin KaFai Lau | a3c00e4 | 2014-10-20 13:42:43 -0700 | [diff] [blame] | 1153 | while (1) { |
| 1154 | if (fn->fn_flags & RTN_TL_ROOT) |
| 1155 | return NULL; |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 1156 | pn = rcu_dereference(fn->parent); |
| 1157 | sn = FIB6_SUBTREE(pn); |
| 1158 | if (sn && sn != fn) |
David Ahern | 6454743 | 2018-05-09 20:34:19 -0700 | [diff] [blame] | 1159 | fn = fib6_node_lookup(sn, NULL, saddr); |
Martin KaFai Lau | a3c00e4 | 2014-10-20 13:42:43 -0700 | [diff] [blame] | 1160 | else |
| 1161 | fn = pn; |
| 1162 | if (fn->fn_flags & RTN_RTINFO) |
| 1163 | return fn; |
| 1164 | } |
| 1165 | } |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1166 | |
David Ahern | 10585b4 | 2019-03-20 09:24:50 -0700 | [diff] [blame] | 1167 | static bool ip6_hold_safe(struct net *net, struct rt6_info **prt) |
Wei Wang | d3843fe | 2017-10-06 12:06:06 -0700 | [diff] [blame] | 1168 | { |
| 1169 | struct rt6_info *rt = *prt; |
| 1170 | |
| 1171 | if (dst_hold_safe(&rt->dst)) |
| 1172 | return true; |
David Ahern | 10585b4 | 2019-03-20 09:24:50 -0700 | [diff] [blame] | 1173 | if (net) { |
Wei Wang | d3843fe | 2017-10-06 12:06:06 -0700 | [diff] [blame] | 1174 | rt = net->ipv6.ip6_null_entry; |
| 1175 | dst_hold(&rt->dst); |
| 1176 | } else { |
| 1177 | rt = NULL; |
| 1178 | } |
| 1179 | *prt = rt; |
| 1180 | return false; |
| 1181 | } |
| 1182 | |
David Ahern | dec9b0e | 2018-04-17 17:33:19 -0700 | [diff] [blame] | 1183 | /* called with rcu_lock held */ |
David Ahern | 9b6b35a | 2019-04-16 14:36:02 -0700 | [diff] [blame] | 1184 | 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] | 1185 | { |
David Ahern | 9b6b35a | 2019-04-16 14:36:02 -0700 | [diff] [blame] | 1186 | struct net_device *dev = res->nh->fib_nh_dev; |
| 1187 | struct fib6_info *f6i = res->f6i; |
| 1188 | unsigned short flags; |
David Ahern | dec9b0e | 2018-04-17 17:33:19 -0700 | [diff] [blame] | 1189 | struct rt6_info *nrt; |
| 1190 | |
David Ahern | 9b6b35a | 2019-04-16 14:36:02 -0700 | [diff] [blame] | 1191 | if (!fib6_info_hold_safe(f6i)) |
Xin Long | 1c87e79 | 2019-03-20 14:45:48 +0800 | [diff] [blame] | 1192 | goto fallback; |
Wei Wang | e873e4b | 2018-07-21 20:56:32 -0700 | [diff] [blame] | 1193 | |
David Ahern | 9b6b35a | 2019-04-16 14:36:02 -0700 | [diff] [blame] | 1194 | flags = fib6_info_dst_flags(f6i); |
David Ahern | 93531c6 | 2018-04-17 17:33:25 -0700 | [diff] [blame] | 1195 | nrt = ip6_dst_alloc(dev_net(dev), dev, flags); |
Xin Long | 1c87e79 | 2019-03-20 14:45:48 +0800 | [diff] [blame] | 1196 | if (!nrt) { |
David Ahern | 9b6b35a | 2019-04-16 14:36:02 -0700 | [diff] [blame] | 1197 | fib6_info_release(f6i); |
Xin Long | 1c87e79 | 2019-03-20 14:45:48 +0800 | [diff] [blame] | 1198 | goto fallback; |
| 1199 | } |
David Ahern | dec9b0e | 2018-04-17 17:33:19 -0700 | [diff] [blame] | 1200 | |
David Ahern | 0d16158 | 2019-04-16 14:36:04 -0700 | [diff] [blame] | 1201 | ip6_rt_copy_init(nrt, res); |
Xin Long | 1c87e79 | 2019-03-20 14:45:48 +0800 | [diff] [blame] | 1202 | return nrt; |
| 1203 | |
| 1204 | fallback: |
| 1205 | nrt = dev_net(dev)->ipv6.ip6_null_entry; |
| 1206 | dst_hold(&nrt->dst); |
David Ahern | dec9b0e | 2018-04-17 17:33:19 -0700 | [diff] [blame] | 1207 | return nrt; |
| 1208 | } |
| 1209 | |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 1210 | static struct rt6_info *ip6_pol_route_lookup(struct net *net, |
| 1211 | struct fib6_table *table, |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 1212 | struct flowi6 *fl6, |
| 1213 | const struct sk_buff *skb, |
| 1214 | int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1215 | { |
David Ahern | b1d4099 | 2019-04-16 14:35:59 -0700 | [diff] [blame] | 1216 | struct fib6_result res = {}; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1217 | struct fib6_node *fn; |
David Ahern | 23fb93a | 2018-04-17 17:33:23 -0700 | [diff] [blame] | 1218 | struct rt6_info *rt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1219 | |
David Ahern | b6cdbc8 | 2018-03-29 17:44:57 -0700 | [diff] [blame] | 1220 | if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF) |
| 1221 | flags &= ~RT6_LOOKUP_F_IFACE; |
| 1222 | |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 1223 | rcu_read_lock(); |
David Ahern | 6454743 | 2018-05-09 20:34:19 -0700 | [diff] [blame] | 1224 | fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr); |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1225 | restart: |
David Ahern | b1d4099 | 2019-04-16 14:35:59 -0700 | [diff] [blame] | 1226 | res.f6i = rcu_dereference(fn->leaf); |
| 1227 | if (!res.f6i) |
| 1228 | res.f6i = net->ipv6.fib6_null_entry; |
David Ahern | af52a52 | 2019-04-09 14:41:16 -0700 | [diff] [blame] | 1229 | else |
David Ahern | 75ef738 | 2019-04-16 14:36:07 -0700 | [diff] [blame] | 1230 | rt6_device_match(net, &res, &fl6->saddr, fl6->flowi6_oif, |
| 1231 | flags); |
David Ahern | af52a52 | 2019-04-09 14:41:16 -0700 | [diff] [blame] | 1232 | |
David Ahern | b1d4099 | 2019-04-16 14:35:59 -0700 | [diff] [blame] | 1233 | if (res.f6i == net->ipv6.fib6_null_entry) { |
Martin KaFai Lau | a3c00e4 | 2014-10-20 13:42:43 -0700 | [diff] [blame] | 1234 | fn = fib6_backtrack(fn, &fl6->saddr); |
| 1235 | if (fn) |
| 1236 | goto restart; |
David Ahern | af52a52 | 2019-04-09 14:41:16 -0700 | [diff] [blame] | 1237 | |
| 1238 | rt = net->ipv6.ip6_null_entry; |
| 1239 | dst_hold(&rt->dst); |
| 1240 | goto out; |
David Ahern | f88d8ea | 2019-06-03 20:19:52 -0700 | [diff] [blame] | 1241 | } else if (res.fib6_flags & RTF_REJECT) { |
| 1242 | goto do_create; |
Martin KaFai Lau | a3c00e4 | 2014-10-20 13:42:43 -0700 | [diff] [blame] | 1243 | } |
Wei Wang | 2b760fc | 2017-10-06 12:06:03 -0700 | [diff] [blame] | 1244 | |
David Ahern | b1d4099 | 2019-04-16 14:35:59 -0700 | [diff] [blame] | 1245 | fib6_select_path(net, &res, fl6, fl6->flowi6_oif, |
| 1246 | fl6->flowi6_oif != 0, skb, flags); |
| 1247 | |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 1248 | /* Search through exception table */ |
David Ahern | 7e4b512 | 2019-04-16 14:36:00 -0700 | [diff] [blame] | 1249 | rt = rt6_find_cached_rt(&res, &fl6->daddr, &fl6->saddr); |
David Ahern | 23fb93a | 2018-04-17 17:33:23 -0700 | [diff] [blame] | 1250 | if (rt) { |
David Ahern | 10585b4 | 2019-03-20 09:24:50 -0700 | [diff] [blame] | 1251 | if (ip6_hold_safe(net, &rt)) |
David Ahern | dec9b0e | 2018-04-17 17:33:19 -0700 | [diff] [blame] | 1252 | dst_use_noref(&rt->dst, jiffies); |
David Ahern | 23fb93a | 2018-04-17 17:33:23 -0700 | [diff] [blame] | 1253 | } else { |
David Ahern | f88d8ea | 2019-06-03 20:19:52 -0700 | [diff] [blame] | 1254 | do_create: |
David Ahern | 9b6b35a | 2019-04-16 14:36:02 -0700 | [diff] [blame] | 1255 | rt = ip6_create_rt_rcu(&res); |
David Ahern | dec9b0e | 2018-04-17 17:33:19 -0700 | [diff] [blame] | 1256 | } |
Wei Wang | d3843fe | 2017-10-06 12:06:06 -0700 | [diff] [blame] | 1257 | |
David Ahern | af52a52 | 2019-04-09 14:41:16 -0700 | [diff] [blame] | 1258 | out: |
David Ahern | 8ff2e5b | 2019-04-16 14:36:09 -0700 | [diff] [blame] | 1259 | trace_fib6_table_lookup(net, &res, table, fl6); |
David Ahern | af52a52 | 2019-04-09 14:41:16 -0700 | [diff] [blame] | 1260 | |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 1261 | rcu_read_unlock(); |
David Ahern | b811580 | 2015-11-19 12:24:22 -0800 | [diff] [blame] | 1262 | |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1263 | return rt; |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1264 | } |
| 1265 | |
Ian Morris | 67ba415 | 2014-08-24 21:53:10 +0100 | [diff] [blame] | 1266 | struct dst_entry *ip6_route_lookup(struct net *net, struct flowi6 *fl6, |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 1267 | const struct sk_buff *skb, int flags) |
Florian Westphal | ea6e574 | 2011-09-05 16:05:44 +0200 | [diff] [blame] | 1268 | { |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 1269 | return fib6_rule_lookup(net, fl6, skb, flags, ip6_pol_route_lookup); |
Florian Westphal | ea6e574 | 2011-09-05 16:05:44 +0200 | [diff] [blame] | 1270 | } |
| 1271 | EXPORT_SYMBOL_GPL(ip6_route_lookup); |
| 1272 | |
YOSHIFUJI Hideaki | 9acd9f3 | 2008-04-10 15:42:10 +0900 | [diff] [blame] | 1273 | 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] | 1274 | const struct in6_addr *saddr, int oif, |
| 1275 | const struct sk_buff *skb, int strict) |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1276 | { |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 1277 | struct flowi6 fl6 = { |
| 1278 | .flowi6_oif = oif, |
| 1279 | .daddr = *daddr, |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1280 | }; |
| 1281 | struct dst_entry *dst; |
YOSHIFUJI Hideaki | 77d16f4 | 2006-08-23 17:25:05 -0700 | [diff] [blame] | 1282 | int flags = strict ? RT6_LOOKUP_F_IFACE : 0; |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1283 | |
Thomas Graf | adaa70b | 2006-10-13 15:01:03 -0700 | [diff] [blame] | 1284 | if (saddr) { |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 1285 | memcpy(&fl6.saddr, saddr, sizeof(*saddr)); |
Thomas Graf | adaa70b | 2006-10-13 15:01:03 -0700 | [diff] [blame] | 1286 | flags |= RT6_LOOKUP_F_HAS_SADDR; |
| 1287 | } |
| 1288 | |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 1289 | dst = fib6_rule_lookup(net, &fl6, skb, flags, ip6_pol_route_lookup); |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1290 | if (dst->error == 0) |
| 1291 | return (struct rt6_info *) dst; |
| 1292 | |
| 1293 | dst_release(dst); |
| 1294 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1295 | return NULL; |
| 1296 | } |
YOSHIFUJI Hideaki | 7159039 | 2007-02-22 22:05:40 +0900 | [diff] [blame] | 1297 | EXPORT_SYMBOL(rt6_lookup); |
| 1298 | |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1299 | /* ip6_ins_rt is called with FREE table->tb6_lock. |
Wei Wang | 1cfb71e | 2017-06-17 10:42:33 -0700 | [diff] [blame] | 1300 | * It takes new route entry, the addition fails by any reason the |
| 1301 | * route is released. |
| 1302 | * Caller must hold dst before calling it. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1303 | */ |
| 1304 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 1305 | 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] | 1306 | struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1307 | { |
| 1308 | int err; |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1309 | struct fib6_table *table; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1310 | |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 1311 | table = rt->fib6_table; |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 1312 | spin_lock_bh(&table->tb6_lock); |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 1313 | err = fib6_add(&table->tb6_root, rt, info, extack); |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 1314 | spin_unlock_bh(&table->tb6_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1315 | |
| 1316 | return err; |
| 1317 | } |
| 1318 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 1319 | int ip6_ins_rt(struct net *net, struct fib6_info *rt) |
Thomas Graf | 40e22e8 | 2006-08-22 00:00:45 -0700 | [diff] [blame] | 1320 | { |
David Ahern | afb1d4b5 | 2018-04-17 17:33:11 -0700 | [diff] [blame] | 1321 | struct nl_info info = { .nl_net = net, }; |
Florian Westphal | e715b6d | 2015-01-05 23:57:44 +0100 | [diff] [blame] | 1322 | |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 1323 | return __ip6_ins_rt(rt, &info, NULL); |
Thomas Graf | 40e22e8 | 2006-08-22 00:00:45 -0700 | [diff] [blame] | 1324 | } |
| 1325 | |
David Ahern | 85bd05d | 2019-04-16 14:36:01 -0700 | [diff] [blame] | 1326 | 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] | 1327 | const struct in6_addr *daddr, |
| 1328 | const struct in6_addr *saddr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1329 | { |
David Ahern | 85bd05d | 2019-04-16 14:36:01 -0700 | [diff] [blame] | 1330 | struct fib6_info *f6i = res->f6i; |
David Ahern | 4832c30 | 2017-08-17 12:17:20 -0700 | [diff] [blame] | 1331 | struct net_device *dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1332 | struct rt6_info *rt; |
| 1333 | |
| 1334 | /* |
| 1335 | * Clone the route. |
| 1336 | */ |
| 1337 | |
David Ahern | 85bd05d | 2019-04-16 14:36:01 -0700 | [diff] [blame] | 1338 | if (!fib6_info_hold_safe(f6i)) |
Wei Wang | e873e4b | 2018-07-21 20:56:32 -0700 | [diff] [blame] | 1339 | return NULL; |
| 1340 | |
David Ahern | 0d16158 | 2019-04-16 14:36:04 -0700 | [diff] [blame] | 1341 | dev = ip6_rt_get_dev_rcu(res); |
David Ahern | 93531c6 | 2018-04-17 17:33:25 -0700 | [diff] [blame] | 1342 | rt = ip6_dst_alloc(dev_net(dev), dev, 0); |
Wei Wang | e873e4b | 2018-07-21 20:56:32 -0700 | [diff] [blame] | 1343 | if (!rt) { |
David Ahern | 85bd05d | 2019-04-16 14:36:01 -0700 | [diff] [blame] | 1344 | fib6_info_release(f6i); |
Martin KaFai Lau | 83a09ab | 2015-05-22 20:56:05 -0700 | [diff] [blame] | 1345 | return NULL; |
Wei Wang | e873e4b | 2018-07-21 20:56:32 -0700 | [diff] [blame] | 1346 | } |
Martin KaFai Lau | 83a09ab | 2015-05-22 20:56:05 -0700 | [diff] [blame] | 1347 | |
David Ahern | 0d16158 | 2019-04-16 14:36:04 -0700 | [diff] [blame] | 1348 | ip6_rt_copy_init(rt, res); |
Martin KaFai Lau | 83a09ab | 2015-05-22 20:56:05 -0700 | [diff] [blame] | 1349 | rt->rt6i_flags |= RTF_CACHE; |
Martin KaFai Lau | 83a09ab | 2015-05-22 20:56:05 -0700 | [diff] [blame] | 1350 | rt->rt6i_dst.addr = *daddr; |
| 1351 | rt->rt6i_dst.plen = 128; |
| 1352 | |
David Ahern | 85bd05d | 2019-04-16 14:36:01 -0700 | [diff] [blame] | 1353 | if (!rt6_is_gw_or_nonexthop(res)) { |
| 1354 | if (f6i->fib6_dst.plen != 128 && |
| 1355 | ipv6_addr_equal(&f6i->fib6_dst.addr, daddr)) |
Martin KaFai Lau | 83a09ab | 2015-05-22 20:56:05 -0700 | [diff] [blame] | 1356 | rt->rt6i_flags |= RTF_ANYCAST; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1357 | #ifdef CONFIG_IPV6_SUBTREES |
Martin KaFai Lau | 83a09ab | 2015-05-22 20:56:05 -0700 | [diff] [blame] | 1358 | if (rt->rt6i_src.plen && saddr) { |
| 1359 | rt->rt6i_src.addr = *saddr; |
| 1360 | rt->rt6i_src.plen = 128; |
Martin KaFai Lau | 8b9df26 | 2015-05-22 20:55:59 -0700 | [diff] [blame] | 1361 | } |
Martin KaFai Lau | 83a09ab | 2015-05-22 20:56:05 -0700 | [diff] [blame] | 1362 | #endif |
YOSHIFUJI Hideaki | 95a9a5b | 2006-03-20 16:55:51 -0800 | [diff] [blame] | 1363 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1364 | |
YOSHIFUJI Hideaki | 95a9a5b | 2006-03-20 16:55:51 -0800 | [diff] [blame] | 1365 | return rt; |
| 1366 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1367 | |
David Ahern | db3fede | 2019-04-16 14:36:03 -0700 | [diff] [blame] | 1368 | 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] | 1369 | { |
David Ahern | db3fede | 2019-04-16 14:36:03 -0700 | [diff] [blame] | 1370 | struct fib6_info *f6i = res->f6i; |
| 1371 | unsigned short flags = fib6_info_dst_flags(f6i); |
David Ahern | 4832c30 | 2017-08-17 12:17:20 -0700 | [diff] [blame] | 1372 | struct net_device *dev; |
Martin KaFai Lau | d52d399 | 2015-05-22 20:56:06 -0700 | [diff] [blame] | 1373 | struct rt6_info *pcpu_rt; |
| 1374 | |
David Ahern | db3fede | 2019-04-16 14:36:03 -0700 | [diff] [blame] | 1375 | if (!fib6_info_hold_safe(f6i)) |
Wei Wang | e873e4b | 2018-07-21 20:56:32 -0700 | [diff] [blame] | 1376 | return NULL; |
| 1377 | |
David Ahern | 4832c30 | 2017-08-17 12:17:20 -0700 | [diff] [blame] | 1378 | rcu_read_lock(); |
David Ahern | 0d16158 | 2019-04-16 14:36:04 -0700 | [diff] [blame] | 1379 | dev = ip6_rt_get_dev_rcu(res); |
David Ahern | 93531c6 | 2018-04-17 17:33:25 -0700 | [diff] [blame] | 1380 | pcpu_rt = ip6_dst_alloc(dev_net(dev), dev, flags); |
David Ahern | 4832c30 | 2017-08-17 12:17:20 -0700 | [diff] [blame] | 1381 | rcu_read_unlock(); |
Wei Wang | e873e4b | 2018-07-21 20:56:32 -0700 | [diff] [blame] | 1382 | if (!pcpu_rt) { |
David Ahern | db3fede | 2019-04-16 14:36:03 -0700 | [diff] [blame] | 1383 | fib6_info_release(f6i); |
Martin KaFai Lau | d52d399 | 2015-05-22 20:56:06 -0700 | [diff] [blame] | 1384 | return NULL; |
Wei Wang | e873e4b | 2018-07-21 20:56:32 -0700 | [diff] [blame] | 1385 | } |
David Ahern | 0d16158 | 2019-04-16 14:36:04 -0700 | [diff] [blame] | 1386 | ip6_rt_copy_init(pcpu_rt, res); |
Martin KaFai Lau | d52d399 | 2015-05-22 20:56:06 -0700 | [diff] [blame] | 1387 | pcpu_rt->rt6i_flags |= RTF_PCPU; |
| 1388 | return pcpu_rt; |
| 1389 | } |
| 1390 | |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 1391 | /* It should be called with rcu_read_lock() acquired */ |
David Ahern | db3fede | 2019-04-16 14:36:03 -0700 | [diff] [blame] | 1392 | 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] | 1393 | { |
Eric Dumazet | c353071 | 2019-05-31 18:11:25 -0700 | [diff] [blame] | 1394 | struct rt6_info *pcpu_rt; |
Martin KaFai Lau | d52d399 | 2015-05-22 20:56:06 -0700 | [diff] [blame] | 1395 | |
Eric Dumazet | c353071 | 2019-05-31 18:11:25 -0700 | [diff] [blame] | 1396 | pcpu_rt = this_cpu_read(*res->nh->rt6i_pcpu); |
Martin KaFai Lau | d52d399 | 2015-05-22 20:56:06 -0700 | [diff] [blame] | 1397 | |
Martin KaFai Lau | a73e419 | 2015-08-14 11:05:53 -0700 | [diff] [blame] | 1398 | return pcpu_rt; |
| 1399 | } |
| 1400 | |
David Ahern | afb1d4b5 | 2018-04-17 17:33:11 -0700 | [diff] [blame] | 1401 | static struct rt6_info *rt6_make_pcpu_route(struct net *net, |
David Ahern | db3fede | 2019-04-16 14:36:03 -0700 | [diff] [blame] | 1402 | const struct fib6_result *res) |
Martin KaFai Lau | a73e419 | 2015-08-14 11:05:53 -0700 | [diff] [blame] | 1403 | { |
| 1404 | struct rt6_info *pcpu_rt, *prev, **p; |
Martin KaFai Lau | d52d399 | 2015-05-22 20:56:06 -0700 | [diff] [blame] | 1405 | |
David Ahern | db3fede | 2019-04-16 14:36:03 -0700 | [diff] [blame] | 1406 | pcpu_rt = ip6_rt_pcpu_alloc(res); |
Wei Wang | 0e09edc | 2019-06-20 17:36:37 -0700 | [diff] [blame] | 1407 | if (!pcpu_rt) |
| 1408 | return NULL; |
Martin KaFai Lau | d52d399 | 2015-05-22 20:56:06 -0700 | [diff] [blame] | 1409 | |
David Ahern | f40b6ae | 2019-05-22 20:27:55 -0700 | [diff] [blame] | 1410 | p = this_cpu_ptr(res->nh->rt6i_pcpu); |
Wei Wang | a94b936 | 2017-10-06 12:06:04 -0700 | [diff] [blame] | 1411 | prev = cmpxchg(p, NULL, pcpu_rt); |
Eric Dumazet | 951f788 | 2017-10-08 21:07:18 -0700 | [diff] [blame] | 1412 | BUG_ON(prev); |
Wei Wang | a94b936 | 2017-10-06 12:06:04 -0700 | [diff] [blame] | 1413 | |
Eric Dumazet | 61fb0d0 | 2019-05-15 19:39:52 -0700 | [diff] [blame] | 1414 | if (res->f6i->fib6_destroying) { |
| 1415 | struct fib6_info *from; |
| 1416 | |
| 1417 | from = xchg((__force struct fib6_info **)&pcpu_rt->from, NULL); |
| 1418 | fib6_info_release(from); |
| 1419 | } |
| 1420 | |
Martin KaFai Lau | d52d399 | 2015-05-22 20:56:06 -0700 | [diff] [blame] | 1421 | return pcpu_rt; |
| 1422 | } |
| 1423 | |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1424 | /* exception hash table implementation |
| 1425 | */ |
| 1426 | static DEFINE_SPINLOCK(rt6_exception_lock); |
| 1427 | |
| 1428 | /* Remove rt6_ex from hash table and free the memory |
| 1429 | * Caller must hold rt6_exception_lock |
| 1430 | */ |
| 1431 | static void rt6_remove_exception(struct rt6_exception_bucket *bucket, |
| 1432 | struct rt6_exception *rt6_ex) |
| 1433 | { |
Paolo Abeni | f5b51fe | 2019-02-20 18:18:12 +0100 | [diff] [blame] | 1434 | struct fib6_info *from; |
Colin Ian King | b2427e6 | 2017-10-10 18:01:16 +0100 | [diff] [blame] | 1435 | struct net *net; |
Wei Wang | 81eb844 | 2017-10-06 12:06:11 -0700 | [diff] [blame] | 1436 | |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1437 | if (!bucket || !rt6_ex) |
| 1438 | return; |
Colin Ian King | b2427e6 | 2017-10-10 18:01:16 +0100 | [diff] [blame] | 1439 | |
| 1440 | net = dev_net(rt6_ex->rt6i->dst.dev); |
Paolo Abeni | f5b51fe | 2019-02-20 18:18:12 +0100 | [diff] [blame] | 1441 | net->ipv6.rt6_stats->fib_rt_cache--; |
| 1442 | |
| 1443 | /* purge completely the exception to allow releasing the held resources: |
| 1444 | * some [sk] cache may keep the dst around for unlimited time |
| 1445 | */ |
Eric Dumazet | 0e23387 | 2019-04-28 12:22:25 -0700 | [diff] [blame] | 1446 | from = xchg((__force struct fib6_info **)&rt6_ex->rt6i->from, NULL); |
Paolo Abeni | f5b51fe | 2019-02-20 18:18:12 +0100 | [diff] [blame] | 1447 | fib6_info_release(from); |
| 1448 | dst_dev_put(&rt6_ex->rt6i->dst); |
| 1449 | |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1450 | hlist_del_rcu(&rt6_ex->hlist); |
David Ahern | 77634cc | 2018-04-17 17:33:27 -0700 | [diff] [blame] | 1451 | dst_release(&rt6_ex->rt6i->dst); |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1452 | kfree_rcu(rt6_ex, rcu); |
| 1453 | WARN_ON_ONCE(!bucket->depth); |
| 1454 | bucket->depth--; |
| 1455 | } |
| 1456 | |
| 1457 | /* Remove oldest rt6_ex in bucket and free the memory |
| 1458 | * Caller must hold rt6_exception_lock |
| 1459 | */ |
| 1460 | static void rt6_exception_remove_oldest(struct rt6_exception_bucket *bucket) |
| 1461 | { |
| 1462 | struct rt6_exception *rt6_ex, *oldest = NULL; |
| 1463 | |
| 1464 | if (!bucket) |
| 1465 | return; |
| 1466 | |
| 1467 | hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) { |
| 1468 | if (!oldest || time_before(rt6_ex->stamp, oldest->stamp)) |
| 1469 | oldest = rt6_ex; |
| 1470 | } |
| 1471 | rt6_remove_exception(bucket, oldest); |
| 1472 | } |
| 1473 | |
| 1474 | static u32 rt6_exception_hash(const struct in6_addr *dst, |
| 1475 | const struct in6_addr *src) |
| 1476 | { |
| 1477 | static u32 seed __read_mostly; |
| 1478 | u32 val; |
| 1479 | |
| 1480 | net_get_random_once(&seed, sizeof(seed)); |
Eric Dumazet | b6b556a | 2019-11-03 18:24:16 -0800 | [diff] [blame] | 1481 | val = jhash2((const u32 *)dst, sizeof(*dst)/sizeof(u32), seed); |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1482 | |
| 1483 | #ifdef CONFIG_IPV6_SUBTREES |
| 1484 | if (src) |
Eric Dumazet | b6b556a | 2019-11-03 18:24:16 -0800 | [diff] [blame] | 1485 | val = jhash2((const u32 *)src, sizeof(*src)/sizeof(u32), val); |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1486 | #endif |
| 1487 | return hash_32(val, FIB6_EXCEPTION_BUCKET_SIZE_SHIFT); |
| 1488 | } |
| 1489 | |
| 1490 | /* Helper function to find the cached rt in the hash table |
| 1491 | * and update bucket pointer to point to the bucket for this |
| 1492 | * (daddr, saddr) pair |
| 1493 | * Caller must hold rt6_exception_lock |
| 1494 | */ |
| 1495 | static struct rt6_exception * |
| 1496 | __rt6_find_exception_spinlock(struct rt6_exception_bucket **bucket, |
| 1497 | const struct in6_addr *daddr, |
| 1498 | const struct in6_addr *saddr) |
| 1499 | { |
| 1500 | struct rt6_exception *rt6_ex; |
| 1501 | u32 hval; |
| 1502 | |
| 1503 | if (!(*bucket) || !daddr) |
| 1504 | return NULL; |
| 1505 | |
| 1506 | hval = rt6_exception_hash(daddr, saddr); |
| 1507 | *bucket += hval; |
| 1508 | |
| 1509 | hlist_for_each_entry(rt6_ex, &(*bucket)->chain, hlist) { |
| 1510 | struct rt6_info *rt6 = rt6_ex->rt6i; |
| 1511 | bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr); |
| 1512 | |
| 1513 | #ifdef CONFIG_IPV6_SUBTREES |
| 1514 | if (matched && saddr) |
| 1515 | matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr); |
| 1516 | #endif |
| 1517 | if (matched) |
| 1518 | return rt6_ex; |
| 1519 | } |
| 1520 | return NULL; |
| 1521 | } |
| 1522 | |
| 1523 | /* Helper function to find the cached rt in the hash table |
| 1524 | * and update bucket pointer to point to the bucket for this |
| 1525 | * (daddr, saddr) pair |
| 1526 | * Caller must hold rcu_read_lock() |
| 1527 | */ |
| 1528 | static struct rt6_exception * |
| 1529 | __rt6_find_exception_rcu(struct rt6_exception_bucket **bucket, |
| 1530 | const struct in6_addr *daddr, |
| 1531 | const struct in6_addr *saddr) |
| 1532 | { |
| 1533 | struct rt6_exception *rt6_ex; |
| 1534 | u32 hval; |
| 1535 | |
| 1536 | WARN_ON_ONCE(!rcu_read_lock_held()); |
| 1537 | |
| 1538 | if (!(*bucket) || !daddr) |
| 1539 | return NULL; |
| 1540 | |
| 1541 | hval = rt6_exception_hash(daddr, saddr); |
| 1542 | *bucket += hval; |
| 1543 | |
| 1544 | hlist_for_each_entry_rcu(rt6_ex, &(*bucket)->chain, hlist) { |
| 1545 | struct rt6_info *rt6 = rt6_ex->rt6i; |
| 1546 | bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr); |
| 1547 | |
| 1548 | #ifdef CONFIG_IPV6_SUBTREES |
| 1549 | if (matched && saddr) |
| 1550 | matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr); |
| 1551 | #endif |
| 1552 | if (matched) |
| 1553 | return rt6_ex; |
| 1554 | } |
| 1555 | return NULL; |
| 1556 | } |
| 1557 | |
David Ahern | b748f26 | 2019-04-16 14:36:06 -0700 | [diff] [blame] | 1558 | static unsigned int fib6_mtu(const struct fib6_result *res) |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1559 | { |
David Ahern | b748f26 | 2019-04-16 14:36:06 -0700 | [diff] [blame] | 1560 | const struct fib6_nh *nh = res->nh; |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 1561 | unsigned int mtu; |
| 1562 | |
David Ahern | b748f26 | 2019-04-16 14:36:06 -0700 | [diff] [blame] | 1563 | if (res->f6i->fib6_pmtu) { |
| 1564 | mtu = res->f6i->fib6_pmtu; |
David Ahern | dcd1f57 | 2018-04-18 15:39:05 -0700 | [diff] [blame] | 1565 | } else { |
David Ahern | b748f26 | 2019-04-16 14:36:06 -0700 | [diff] [blame] | 1566 | struct net_device *dev = nh->fib_nh_dev; |
David Ahern | dcd1f57 | 2018-04-18 15:39:05 -0700 | [diff] [blame] | 1567 | struct inet6_dev *idev; |
| 1568 | |
| 1569 | rcu_read_lock(); |
| 1570 | idev = __in6_dev_get(dev); |
| 1571 | mtu = idev->cnf.mtu6; |
| 1572 | rcu_read_unlock(); |
| 1573 | } |
| 1574 | |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 1575 | mtu = min_t(unsigned int, mtu, IP6_MAX_MTU); |
| 1576 | |
David Ahern | b748f26 | 2019-04-16 14:36:06 -0700 | [diff] [blame] | 1577 | return mtu - lwtunnel_headroom(nh->fib_nh_lws, mtu); |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 1578 | } |
| 1579 | |
David Ahern | cc5c073 | 2019-05-22 20:27:58 -0700 | [diff] [blame] | 1580 | #define FIB6_EXCEPTION_BUCKET_FLUSHED 0x1UL |
| 1581 | |
| 1582 | /* used when the flushed bit is not relevant, only access to the bucket |
| 1583 | * (ie., all bucket users except rt6_insert_exception); |
| 1584 | * |
| 1585 | * called under rcu lock; sometimes called with rt6_exception_lock held |
| 1586 | */ |
| 1587 | static |
| 1588 | struct rt6_exception_bucket *fib6_nh_get_excptn_bucket(const struct fib6_nh *nh, |
| 1589 | spinlock_t *lock) |
| 1590 | { |
| 1591 | struct rt6_exception_bucket *bucket; |
| 1592 | |
| 1593 | if (lock) |
| 1594 | bucket = rcu_dereference_protected(nh->rt6i_exception_bucket, |
| 1595 | lockdep_is_held(lock)); |
| 1596 | else |
| 1597 | bucket = rcu_dereference(nh->rt6i_exception_bucket); |
| 1598 | |
| 1599 | /* remove bucket flushed bit if set */ |
| 1600 | if (bucket) { |
| 1601 | unsigned long p = (unsigned long)bucket; |
| 1602 | |
| 1603 | p &= ~FIB6_EXCEPTION_BUCKET_FLUSHED; |
| 1604 | bucket = (struct rt6_exception_bucket *)p; |
| 1605 | } |
| 1606 | |
| 1607 | return bucket; |
| 1608 | } |
| 1609 | |
| 1610 | static bool fib6_nh_excptn_bucket_flushed(struct rt6_exception_bucket *bucket) |
| 1611 | { |
| 1612 | unsigned long p = (unsigned long)bucket; |
| 1613 | |
| 1614 | return !!(p & FIB6_EXCEPTION_BUCKET_FLUSHED); |
| 1615 | } |
| 1616 | |
| 1617 | /* called with rt6_exception_lock held */ |
| 1618 | static void fib6_nh_excptn_bucket_set_flushed(struct fib6_nh *nh, |
| 1619 | spinlock_t *lock) |
| 1620 | { |
| 1621 | struct rt6_exception_bucket *bucket; |
| 1622 | unsigned long p; |
| 1623 | |
| 1624 | bucket = rcu_dereference_protected(nh->rt6i_exception_bucket, |
| 1625 | lockdep_is_held(lock)); |
| 1626 | |
| 1627 | p = (unsigned long)bucket; |
| 1628 | p |= FIB6_EXCEPTION_BUCKET_FLUSHED; |
| 1629 | bucket = (struct rt6_exception_bucket *)p; |
| 1630 | rcu_assign_pointer(nh->rt6i_exception_bucket, bucket); |
| 1631 | } |
| 1632 | |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1633 | static int rt6_insert_exception(struct rt6_info *nrt, |
David Ahern | 5012f0a | 2019-04-16 14:36:05 -0700 | [diff] [blame] | 1634 | const struct fib6_result *res) |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1635 | { |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 1636 | struct net *net = dev_net(nrt->dst.dev); |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1637 | struct rt6_exception_bucket *bucket; |
David Ahern | cc5c073 | 2019-05-22 20:27:58 -0700 | [diff] [blame] | 1638 | struct fib6_info *f6i = res->f6i; |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1639 | struct in6_addr *src_key = NULL; |
| 1640 | struct rt6_exception *rt6_ex; |
David Ahern | cc5c073 | 2019-05-22 20:27:58 -0700 | [diff] [blame] | 1641 | struct fib6_nh *nh = res->nh; |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1642 | int err = 0; |
| 1643 | |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1644 | spin_lock_bh(&rt6_exception_lock); |
| 1645 | |
David Ahern | cc5c073 | 2019-05-22 20:27:58 -0700 | [diff] [blame] | 1646 | bucket = rcu_dereference_protected(nh->rt6i_exception_bucket, |
| 1647 | lockdep_is_held(&rt6_exception_lock)); |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1648 | if (!bucket) { |
| 1649 | bucket = kcalloc(FIB6_EXCEPTION_BUCKET_SIZE, sizeof(*bucket), |
| 1650 | GFP_ATOMIC); |
| 1651 | if (!bucket) { |
| 1652 | err = -ENOMEM; |
| 1653 | goto out; |
| 1654 | } |
David Ahern | cc5c073 | 2019-05-22 20:27:58 -0700 | [diff] [blame] | 1655 | rcu_assign_pointer(nh->rt6i_exception_bucket, bucket); |
| 1656 | } else if (fib6_nh_excptn_bucket_flushed(bucket)) { |
| 1657 | err = -EINVAL; |
| 1658 | goto out; |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1659 | } |
| 1660 | |
| 1661 | #ifdef CONFIG_IPV6_SUBTREES |
David Ahern | 5012f0a | 2019-04-16 14:36:05 -0700 | [diff] [blame] | 1662 | /* fib6_src.plen != 0 indicates f6i is in subtree |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1663 | * and exception table is indexed by a hash of |
David Ahern | 5012f0a | 2019-04-16 14:36:05 -0700 | [diff] [blame] | 1664 | * both fib6_dst and fib6_src. |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1665 | * Otherwise, the exception table is indexed by |
David Ahern | 5012f0a | 2019-04-16 14:36:05 -0700 | [diff] [blame] | 1666 | * a hash of only fib6_dst. |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1667 | */ |
David Ahern | 5012f0a | 2019-04-16 14:36:05 -0700 | [diff] [blame] | 1668 | if (f6i->fib6_src.plen) |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1669 | src_key = &nrt->rt6i_src.addr; |
| 1670 | #endif |
David Ahern | 5012f0a | 2019-04-16 14:36:05 -0700 | [diff] [blame] | 1671 | /* rt6_mtu_change() might lower mtu on f6i. |
Wei Wang | f5bbe7e | 2017-10-06 12:05:59 -0700 | [diff] [blame] | 1672 | * Only insert this exception route if its mtu |
David Ahern | 5012f0a | 2019-04-16 14:36:05 -0700 | [diff] [blame] | 1673 | * is less than f6i's mtu value. |
Wei Wang | f5bbe7e | 2017-10-06 12:05:59 -0700 | [diff] [blame] | 1674 | */ |
David Ahern | b748f26 | 2019-04-16 14:36:06 -0700 | [diff] [blame] | 1675 | if (dst_metric_raw(&nrt->dst, RTAX_MTU) >= fib6_mtu(res)) { |
Wei Wang | f5bbe7e | 2017-10-06 12:05:59 -0700 | [diff] [blame] | 1676 | err = -EINVAL; |
| 1677 | goto out; |
| 1678 | } |
Wei Wang | 60006a4 | 2017-10-06 12:05:58 -0700 | [diff] [blame] | 1679 | |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1680 | rt6_ex = __rt6_find_exception_spinlock(&bucket, &nrt->rt6i_dst.addr, |
| 1681 | src_key); |
| 1682 | if (rt6_ex) |
| 1683 | rt6_remove_exception(bucket, rt6_ex); |
| 1684 | |
| 1685 | rt6_ex = kzalloc(sizeof(*rt6_ex), GFP_ATOMIC); |
| 1686 | if (!rt6_ex) { |
| 1687 | err = -ENOMEM; |
| 1688 | goto out; |
| 1689 | } |
| 1690 | rt6_ex->rt6i = nrt; |
| 1691 | rt6_ex->stamp = jiffies; |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1692 | hlist_add_head_rcu(&rt6_ex->hlist, &bucket->chain); |
| 1693 | bucket->depth++; |
Wei Wang | 81eb844 | 2017-10-06 12:06:11 -0700 | [diff] [blame] | 1694 | net->ipv6.rt6_stats->fib_rt_cache++; |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1695 | |
| 1696 | if (bucket->depth > FIB6_MAX_DEPTH) |
| 1697 | rt6_exception_remove_oldest(bucket); |
| 1698 | |
| 1699 | out: |
| 1700 | spin_unlock_bh(&rt6_exception_lock); |
| 1701 | |
| 1702 | /* Update fn->fn_sernum to invalidate all cached dst */ |
Paolo Abeni | b886d5f | 2017-10-19 16:07:10 +0200 | [diff] [blame] | 1703 | if (!err) { |
David Ahern | 5012f0a | 2019-04-16 14:36:05 -0700 | [diff] [blame] | 1704 | spin_lock_bh(&f6i->fib6_table->tb6_lock); |
| 1705 | fib6_update_sernum(net, f6i); |
| 1706 | spin_unlock_bh(&f6i->fib6_table->tb6_lock); |
Paolo Abeni | b886d5f | 2017-10-19 16:07:10 +0200 | [diff] [blame] | 1707 | fib6_force_start_gc(net); |
| 1708 | } |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1709 | |
| 1710 | return err; |
| 1711 | } |
| 1712 | |
David Ahern | c0b220c | 2019-05-22 20:27:57 -0700 | [diff] [blame] | 1713 | 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] | 1714 | { |
| 1715 | struct rt6_exception_bucket *bucket; |
| 1716 | struct rt6_exception *rt6_ex; |
| 1717 | struct hlist_node *tmp; |
| 1718 | int i; |
| 1719 | |
| 1720 | spin_lock_bh(&rt6_exception_lock); |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1721 | |
David Ahern | cc5c073 | 2019-05-22 20:27:58 -0700 | [diff] [blame] | 1722 | bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock); |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1723 | if (!bucket) |
| 1724 | goto out; |
| 1725 | |
David Ahern | cc5c073 | 2019-05-22 20:27:58 -0700 | [diff] [blame] | 1726 | /* Prevent rt6_insert_exception() to recreate the bucket list */ |
| 1727 | if (!from) |
| 1728 | fib6_nh_excptn_bucket_set_flushed(nh, &rt6_exception_lock); |
| 1729 | |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1730 | for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) { |
David Ahern | cc5c073 | 2019-05-22 20:27:58 -0700 | [diff] [blame] | 1731 | hlist_for_each_entry_safe(rt6_ex, tmp, &bucket->chain, hlist) { |
| 1732 | if (!from || |
| 1733 | rcu_access_pointer(rt6_ex->rt6i->from) == from) |
| 1734 | rt6_remove_exception(bucket, rt6_ex); |
| 1735 | } |
| 1736 | WARN_ON_ONCE(!from && bucket->depth); |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1737 | bucket++; |
| 1738 | } |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1739 | out: |
| 1740 | spin_unlock_bh(&rt6_exception_lock); |
| 1741 | } |
| 1742 | |
David Ahern | e659ba3 | 2019-06-08 14:53:28 -0700 | [diff] [blame] | 1743 | static int rt6_nh_flush_exceptions(struct fib6_nh *nh, void *arg) |
| 1744 | { |
| 1745 | struct fib6_info *f6i = arg; |
| 1746 | |
| 1747 | fib6_nh_flush_exceptions(nh, f6i); |
| 1748 | |
| 1749 | return 0; |
| 1750 | } |
| 1751 | |
David Ahern | c0b220c | 2019-05-22 20:27:57 -0700 | [diff] [blame] | 1752 | void rt6_flush_exceptions(struct fib6_info *f6i) |
| 1753 | { |
David Ahern | e659ba3 | 2019-06-08 14:53:28 -0700 | [diff] [blame] | 1754 | if (f6i->nh) |
| 1755 | nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_flush_exceptions, |
| 1756 | f6i); |
| 1757 | else |
| 1758 | fib6_nh_flush_exceptions(f6i->fib6_nh, f6i); |
David Ahern | c0b220c | 2019-05-22 20:27:57 -0700 | [diff] [blame] | 1759 | } |
| 1760 | |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1761 | /* Find cached rt in the hash table inside passed in rt |
| 1762 | * Caller has to hold rcu_read_lock() |
| 1763 | */ |
David Ahern | 7e4b512 | 2019-04-16 14:36:00 -0700 | [diff] [blame] | 1764 | 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] | 1765 | const struct in6_addr *daddr, |
| 1766 | const struct in6_addr *saddr) |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1767 | { |
Wei Wang | 510e2ce | 2019-05-16 13:30:54 -0700 | [diff] [blame] | 1768 | const struct in6_addr *src_key = NULL; |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1769 | struct rt6_exception_bucket *bucket; |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1770 | struct rt6_exception *rt6_ex; |
David Ahern | 7e4b512 | 2019-04-16 14:36:00 -0700 | [diff] [blame] | 1771 | struct rt6_info *ret = NULL; |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1772 | |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1773 | #ifdef CONFIG_IPV6_SUBTREES |
David Ahern | 7e4b512 | 2019-04-16 14:36:00 -0700 | [diff] [blame] | 1774 | /* fib6i_src.plen != 0 indicates f6i is in subtree |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1775 | * and exception table is indexed by a hash of |
David Ahern | 7e4b512 | 2019-04-16 14:36:00 -0700 | [diff] [blame] | 1776 | * both fib6_dst and fib6_src. |
Wei Wang | 510e2ce | 2019-05-16 13:30:54 -0700 | [diff] [blame] | 1777 | * However, the src addr used to create the hash |
| 1778 | * might not be exactly the passed in saddr which |
| 1779 | * is a /128 addr from the flow. |
| 1780 | * So we need to use f6i->fib6_src to redo lookup |
| 1781 | * if the passed in saddr does not find anything. |
| 1782 | * (See the logic in ip6_rt_cache_alloc() on how |
| 1783 | * rt->rt6i_src is updated.) |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1784 | */ |
David Ahern | 7e4b512 | 2019-04-16 14:36:00 -0700 | [diff] [blame] | 1785 | if (res->f6i->fib6_src.plen) |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1786 | src_key = saddr; |
Wei Wang | 510e2ce | 2019-05-16 13:30:54 -0700 | [diff] [blame] | 1787 | find_ex: |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1788 | #endif |
David Ahern | cc5c073 | 2019-05-22 20:27:58 -0700 | [diff] [blame] | 1789 | bucket = fib6_nh_get_excptn_bucket(res->nh, NULL); |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1790 | rt6_ex = __rt6_find_exception_rcu(&bucket, daddr, src_key); |
| 1791 | |
| 1792 | if (rt6_ex && !rt6_check_expired(rt6_ex->rt6i)) |
David Ahern | 7e4b512 | 2019-04-16 14:36:00 -0700 | [diff] [blame] | 1793 | ret = rt6_ex->rt6i; |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1794 | |
Wei Wang | 510e2ce | 2019-05-16 13:30:54 -0700 | [diff] [blame] | 1795 | #ifdef CONFIG_IPV6_SUBTREES |
| 1796 | /* Use fib6_src as src_key and redo lookup */ |
| 1797 | if (!ret && src_key && src_key != &res->f6i->fib6_src.addr) { |
| 1798 | src_key = &res->f6i->fib6_src.addr; |
| 1799 | goto find_ex; |
| 1800 | } |
| 1801 | #endif |
| 1802 | |
David Ahern | 7e4b512 | 2019-04-16 14:36:00 -0700 | [diff] [blame] | 1803 | return ret; |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1804 | } |
| 1805 | |
| 1806 | /* 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] | 1807 | 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] | 1808 | const struct rt6_info *rt) |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1809 | { |
David Ahern | c0b220c | 2019-05-22 20:27:57 -0700 | [diff] [blame] | 1810 | const struct in6_addr *src_key = NULL; |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1811 | struct rt6_exception_bucket *bucket; |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1812 | struct rt6_exception *rt6_ex; |
| 1813 | int err; |
| 1814 | |
David Ahern | cc5c073 | 2019-05-22 20:27:58 -0700 | [diff] [blame] | 1815 | if (!rcu_access_pointer(nh->rt6i_exception_bucket)) |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1816 | return -ENOENT; |
| 1817 | |
| 1818 | spin_lock_bh(&rt6_exception_lock); |
David Ahern | cc5c073 | 2019-05-22 20:27:58 -0700 | [diff] [blame] | 1819 | bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock); |
| 1820 | |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1821 | #ifdef CONFIG_IPV6_SUBTREES |
David Ahern | cc5c073 | 2019-05-22 20:27:58 -0700 | [diff] [blame] | 1822 | /* rt6i_src.plen != 0 indicates 'from' is in subtree |
| 1823 | * and exception table is indexed by a hash of |
| 1824 | * both rt6i_dst and rt6i_src. |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1825 | * Otherwise, the exception table is indexed by |
| 1826 | * a hash of only rt6i_dst. |
| 1827 | */ |
David Ahern | c0b220c | 2019-05-22 20:27:57 -0700 | [diff] [blame] | 1828 | if (plen) |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1829 | src_key = &rt->rt6i_src.addr; |
| 1830 | #endif |
| 1831 | rt6_ex = __rt6_find_exception_spinlock(&bucket, |
| 1832 | &rt->rt6i_dst.addr, |
| 1833 | src_key); |
| 1834 | if (rt6_ex) { |
| 1835 | rt6_remove_exception(bucket, rt6_ex); |
| 1836 | err = 0; |
| 1837 | } else { |
| 1838 | err = -ENOENT; |
| 1839 | } |
| 1840 | |
| 1841 | spin_unlock_bh(&rt6_exception_lock); |
| 1842 | return err; |
| 1843 | } |
| 1844 | |
David Ahern | e659ba3 | 2019-06-08 14:53:28 -0700 | [diff] [blame] | 1845 | struct fib6_nh_excptn_arg { |
| 1846 | struct rt6_info *rt; |
| 1847 | int plen; |
| 1848 | }; |
| 1849 | |
| 1850 | static int rt6_nh_remove_exception_rt(struct fib6_nh *nh, void *_arg) |
| 1851 | { |
| 1852 | struct fib6_nh_excptn_arg *arg = _arg; |
| 1853 | int err; |
| 1854 | |
| 1855 | err = fib6_nh_remove_exception(nh, arg->plen, arg->rt); |
| 1856 | if (err == 0) |
| 1857 | return 1; |
| 1858 | |
| 1859 | return 0; |
| 1860 | } |
| 1861 | |
David Ahern | c0b220c | 2019-05-22 20:27:57 -0700 | [diff] [blame] | 1862 | static int rt6_remove_exception_rt(struct rt6_info *rt) |
| 1863 | { |
| 1864 | struct fib6_info *from; |
| 1865 | |
| 1866 | from = rcu_dereference(rt->from); |
David Ahern | cc5c073 | 2019-05-22 20:27:58 -0700 | [diff] [blame] | 1867 | if (!from || !(rt->rt6i_flags & RTF_CACHE)) |
David Ahern | c0b220c | 2019-05-22 20:27:57 -0700 | [diff] [blame] | 1868 | return -EINVAL; |
| 1869 | |
David Ahern | e659ba3 | 2019-06-08 14:53:28 -0700 | [diff] [blame] | 1870 | if (from->nh) { |
| 1871 | struct fib6_nh_excptn_arg arg = { |
| 1872 | .rt = rt, |
| 1873 | .plen = from->fib6_src.plen |
| 1874 | }; |
| 1875 | int rc; |
| 1876 | |
| 1877 | /* rc = 1 means an entry was found */ |
| 1878 | rc = nexthop_for_each_fib6_nh(from->nh, |
| 1879 | rt6_nh_remove_exception_rt, |
| 1880 | &arg); |
| 1881 | return rc ? 0 : -ENOENT; |
| 1882 | } |
| 1883 | |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 1884 | return fib6_nh_remove_exception(from->fib6_nh, |
David Ahern | cc5c073 | 2019-05-22 20:27:58 -0700 | [diff] [blame] | 1885 | from->fib6_src.plen, rt); |
David Ahern | c0b220c | 2019-05-22 20:27:57 -0700 | [diff] [blame] | 1886 | } |
| 1887 | |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1888 | /* Find rt6_ex which contains the passed in rt cache and |
| 1889 | * refresh its stamp |
| 1890 | */ |
David Ahern | cc5c073 | 2019-05-22 20:27:58 -0700 | [diff] [blame] | 1891 | 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] | 1892 | const struct rt6_info *rt) |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1893 | { |
David Ahern | c0b220c | 2019-05-22 20:27:57 -0700 | [diff] [blame] | 1894 | const struct in6_addr *src_key = NULL; |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1895 | struct rt6_exception_bucket *bucket; |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1896 | struct rt6_exception *rt6_ex; |
Paolo Abeni | 193f368 | 2019-02-21 11:19:41 +0100 | [diff] [blame] | 1897 | |
David Ahern | cc5c073 | 2019-05-22 20:27:58 -0700 | [diff] [blame] | 1898 | bucket = fib6_nh_get_excptn_bucket(nh, NULL); |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1899 | #ifdef CONFIG_IPV6_SUBTREES |
David Ahern | cc5c073 | 2019-05-22 20:27:58 -0700 | [diff] [blame] | 1900 | /* rt6i_src.plen != 0 indicates 'from' is in subtree |
| 1901 | * and exception table is indexed by a hash of |
| 1902 | * both rt6i_dst and rt6i_src. |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1903 | * Otherwise, the exception table is indexed by |
| 1904 | * a hash of only rt6i_dst. |
| 1905 | */ |
David Ahern | c0b220c | 2019-05-22 20:27:57 -0700 | [diff] [blame] | 1906 | if (plen) |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1907 | src_key = &rt->rt6i_src.addr; |
| 1908 | #endif |
David Ahern | cc5c073 | 2019-05-22 20:27:58 -0700 | [diff] [blame] | 1909 | 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] | 1910 | if (rt6_ex) |
| 1911 | rt6_ex->stamp = jiffies; |
David Ahern | c0b220c | 2019-05-22 20:27:57 -0700 | [diff] [blame] | 1912 | } |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1913 | |
David Ahern | e659ba3 | 2019-06-08 14:53:28 -0700 | [diff] [blame] | 1914 | struct fib6_nh_match_arg { |
| 1915 | const struct net_device *dev; |
| 1916 | const struct in6_addr *gw; |
| 1917 | struct fib6_nh *match; |
| 1918 | }; |
| 1919 | |
| 1920 | /* determine if fib6_nh has given device and gateway */ |
| 1921 | static int fib6_nh_find_match(struct fib6_nh *nh, void *_arg) |
| 1922 | { |
| 1923 | struct fib6_nh_match_arg *arg = _arg; |
| 1924 | |
| 1925 | if (arg->dev != nh->fib_nh_dev || |
| 1926 | (arg->gw && !nh->fib_nh_gw_family) || |
| 1927 | (!arg->gw && nh->fib_nh_gw_family) || |
| 1928 | (arg->gw && !ipv6_addr_equal(arg->gw, &nh->fib_nh_gw6))) |
| 1929 | return 0; |
| 1930 | |
| 1931 | arg->match = nh; |
| 1932 | |
| 1933 | /* found a match, break the loop */ |
| 1934 | return 1; |
| 1935 | } |
| 1936 | |
David Ahern | c0b220c | 2019-05-22 20:27:57 -0700 | [diff] [blame] | 1937 | static void rt6_update_exception_stamp_rt(struct rt6_info *rt) |
| 1938 | { |
| 1939 | struct fib6_info *from; |
David Ahern | e659ba3 | 2019-06-08 14:53:28 -0700 | [diff] [blame] | 1940 | struct fib6_nh *fib6_nh; |
David Ahern | c0b220c | 2019-05-22 20:27:57 -0700 | [diff] [blame] | 1941 | |
| 1942 | rcu_read_lock(); |
| 1943 | |
| 1944 | from = rcu_dereference(rt->from); |
| 1945 | if (!from || !(rt->rt6i_flags & RTF_CACHE)) |
| 1946 | goto unlock; |
| 1947 | |
David Ahern | e659ba3 | 2019-06-08 14:53:28 -0700 | [diff] [blame] | 1948 | if (from->nh) { |
| 1949 | struct fib6_nh_match_arg arg = { |
| 1950 | .dev = rt->dst.dev, |
| 1951 | .gw = &rt->rt6i_gateway, |
| 1952 | }; |
| 1953 | |
| 1954 | nexthop_for_each_fib6_nh(from->nh, fib6_nh_find_match, &arg); |
| 1955 | |
| 1956 | if (!arg.match) |
David Ahern | cff6a32 | 2019-08-01 14:36:35 -0700 | [diff] [blame] | 1957 | goto unlock; |
David Ahern | e659ba3 | 2019-06-08 14:53:28 -0700 | [diff] [blame] | 1958 | fib6_nh = arg.match; |
| 1959 | } else { |
| 1960 | fib6_nh = from->fib6_nh; |
| 1961 | } |
| 1962 | fib6_nh_update_exception(fib6_nh, from->fib6_src.plen, rt); |
Paolo Abeni | 193f368 | 2019-02-21 11:19:41 +0100 | [diff] [blame] | 1963 | unlock: |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1964 | rcu_read_unlock(); |
| 1965 | } |
| 1966 | |
Stefano Brivio | e9fa149 | 2018-03-06 11:10:19 +0100 | [diff] [blame] | 1967 | static bool rt6_mtu_change_route_allowed(struct inet6_dev *idev, |
| 1968 | struct rt6_info *rt, int mtu) |
| 1969 | { |
| 1970 | /* If the new MTU is lower than the route PMTU, this new MTU will be the |
| 1971 | * lowest MTU in the path: always allow updating the route PMTU to |
| 1972 | * reflect PMTU decreases. |
| 1973 | * |
| 1974 | * If the new MTU is higher, and the route PMTU is equal to the local |
| 1975 | * MTU, this means the old MTU is the lowest in the path, so allow |
| 1976 | * updating it: if other nodes now have lower MTUs, PMTU discovery will |
| 1977 | * handle this. |
| 1978 | */ |
| 1979 | |
| 1980 | if (dst_mtu(&rt->dst) >= mtu) |
| 1981 | return true; |
| 1982 | |
| 1983 | if (dst_mtu(&rt->dst) == idev->cnf.mtu6) |
| 1984 | return true; |
| 1985 | |
| 1986 | return false; |
| 1987 | } |
| 1988 | |
| 1989 | static void rt6_exceptions_update_pmtu(struct inet6_dev *idev, |
David Ahern | cc5c073 | 2019-05-22 20:27:58 -0700 | [diff] [blame] | 1990 | const struct fib6_nh *nh, int mtu) |
Wei Wang | f5bbe7e | 2017-10-06 12:05:59 -0700 | [diff] [blame] | 1991 | { |
| 1992 | struct rt6_exception_bucket *bucket; |
| 1993 | struct rt6_exception *rt6_ex; |
| 1994 | int i; |
| 1995 | |
David Ahern | cc5c073 | 2019-05-22 20:27:58 -0700 | [diff] [blame] | 1996 | bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock); |
Stefano Brivio | e9fa149 | 2018-03-06 11:10:19 +0100 | [diff] [blame] | 1997 | if (!bucket) |
| 1998 | return; |
| 1999 | |
| 2000 | for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) { |
| 2001 | hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) { |
| 2002 | struct rt6_info *entry = rt6_ex->rt6i; |
| 2003 | |
| 2004 | /* For RTF_CACHE with rt6i_pmtu == 0 (i.e. a redirected |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 2005 | * route), the metrics of its rt->from have already |
Stefano Brivio | e9fa149 | 2018-03-06 11:10:19 +0100 | [diff] [blame] | 2006 | * been updated. |
| 2007 | */ |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 2008 | if (dst_metric_raw(&entry->dst, RTAX_MTU) && |
Stefano Brivio | e9fa149 | 2018-03-06 11:10:19 +0100 | [diff] [blame] | 2009 | rt6_mtu_change_route_allowed(idev, entry, mtu)) |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 2010 | dst_metric_set(&entry->dst, RTAX_MTU, mtu); |
Wei Wang | f5bbe7e | 2017-10-06 12:05:59 -0700 | [diff] [blame] | 2011 | } |
Stefano Brivio | e9fa149 | 2018-03-06 11:10:19 +0100 | [diff] [blame] | 2012 | bucket++; |
Wei Wang | f5bbe7e | 2017-10-06 12:05:59 -0700 | [diff] [blame] | 2013 | } |
| 2014 | } |
| 2015 | |
Wei Wang | b16cb45 | 2017-10-06 12:06:00 -0700 | [diff] [blame] | 2016 | #define RTF_CACHE_GATEWAY (RTF_GATEWAY | RTF_CACHE) |
| 2017 | |
David Ahern | cc5c073 | 2019-05-22 20:27:58 -0700 | [diff] [blame] | 2018 | static void fib6_nh_exceptions_clean_tohost(const struct fib6_nh *nh, |
| 2019 | const struct in6_addr *gateway) |
Wei Wang | b16cb45 | 2017-10-06 12:06:00 -0700 | [diff] [blame] | 2020 | { |
| 2021 | struct rt6_exception_bucket *bucket; |
| 2022 | struct rt6_exception *rt6_ex; |
| 2023 | struct hlist_node *tmp; |
| 2024 | int i; |
| 2025 | |
David Ahern | cc5c073 | 2019-05-22 20:27:58 -0700 | [diff] [blame] | 2026 | if (!rcu_access_pointer(nh->rt6i_exception_bucket)) |
Wei Wang | b16cb45 | 2017-10-06 12:06:00 -0700 | [diff] [blame] | 2027 | return; |
| 2028 | |
| 2029 | spin_lock_bh(&rt6_exception_lock); |
David Ahern | cc5c073 | 2019-05-22 20:27:58 -0700 | [diff] [blame] | 2030 | bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock); |
Wei Wang | b16cb45 | 2017-10-06 12:06:00 -0700 | [diff] [blame] | 2031 | if (bucket) { |
| 2032 | for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) { |
| 2033 | hlist_for_each_entry_safe(rt6_ex, tmp, |
| 2034 | &bucket->chain, hlist) { |
| 2035 | struct rt6_info *entry = rt6_ex->rt6i; |
| 2036 | |
| 2037 | if ((entry->rt6i_flags & RTF_CACHE_GATEWAY) == |
| 2038 | RTF_CACHE_GATEWAY && |
| 2039 | ipv6_addr_equal(gateway, |
| 2040 | &entry->rt6i_gateway)) { |
| 2041 | rt6_remove_exception(bucket, rt6_ex); |
| 2042 | } |
| 2043 | } |
| 2044 | bucket++; |
| 2045 | } |
| 2046 | } |
| 2047 | |
| 2048 | spin_unlock_bh(&rt6_exception_lock); |
| 2049 | } |
| 2050 | |
Wei Wang | c757faa | 2017-10-06 12:06:01 -0700 | [diff] [blame] | 2051 | static void rt6_age_examine_exception(struct rt6_exception_bucket *bucket, |
| 2052 | struct rt6_exception *rt6_ex, |
| 2053 | struct fib6_gc_args *gc_args, |
| 2054 | unsigned long now) |
| 2055 | { |
| 2056 | struct rt6_info *rt = rt6_ex->rt6i; |
| 2057 | |
Paolo Abeni | 1859bac | 2017-10-19 16:07:11 +0200 | [diff] [blame] | 2058 | /* we are pruning and obsoleting aged-out and non gateway exceptions |
| 2059 | * even if others have still references to them, so that on next |
| 2060 | * dst_check() such references can be dropped. |
| 2061 | * EXPIRES exceptions - e.g. pmtu-generated ones are pruned when |
| 2062 | * expired, independently from their aging, as per RFC 8201 section 4 |
| 2063 | */ |
Wei Wang | 31afeb4 | 2018-01-26 11:40:17 -0800 | [diff] [blame] | 2064 | if (!(rt->rt6i_flags & RTF_EXPIRES)) { |
| 2065 | if (time_after_eq(now, rt->dst.lastuse + gc_args->timeout)) { |
| 2066 | RT6_TRACE("aging clone %p\n", rt); |
| 2067 | rt6_remove_exception(bucket, rt6_ex); |
| 2068 | return; |
| 2069 | } |
| 2070 | } else if (time_after(jiffies, rt->dst.expires)) { |
| 2071 | RT6_TRACE("purging expired route %p\n", rt); |
Wei Wang | c757faa | 2017-10-06 12:06:01 -0700 | [diff] [blame] | 2072 | rt6_remove_exception(bucket, rt6_ex); |
| 2073 | return; |
Wei Wang | 31afeb4 | 2018-01-26 11:40:17 -0800 | [diff] [blame] | 2074 | } |
| 2075 | |
| 2076 | if (rt->rt6i_flags & RTF_GATEWAY) { |
Wei Wang | c757faa | 2017-10-06 12:06:01 -0700 | [diff] [blame] | 2077 | struct neighbour *neigh; |
| 2078 | __u8 neigh_flags = 0; |
| 2079 | |
Eric Dumazet | 1bfa26f | 2018-03-23 07:56:58 -0700 | [diff] [blame] | 2080 | neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway); |
| 2081 | if (neigh) |
Wei Wang | c757faa | 2017-10-06 12:06:01 -0700 | [diff] [blame] | 2082 | neigh_flags = neigh->flags; |
Eric Dumazet | 1bfa26f | 2018-03-23 07:56:58 -0700 | [diff] [blame] | 2083 | |
Wei Wang | c757faa | 2017-10-06 12:06:01 -0700 | [diff] [blame] | 2084 | if (!(neigh_flags & NTF_ROUTER)) { |
| 2085 | RT6_TRACE("purging route %p via non-router but gateway\n", |
| 2086 | rt); |
| 2087 | rt6_remove_exception(bucket, rt6_ex); |
| 2088 | return; |
| 2089 | } |
| 2090 | } |
Wei Wang | 31afeb4 | 2018-01-26 11:40:17 -0800 | [diff] [blame] | 2091 | |
Wei Wang | c757faa | 2017-10-06 12:06:01 -0700 | [diff] [blame] | 2092 | gc_args->more++; |
| 2093 | } |
| 2094 | |
David Ahern | cc5c073 | 2019-05-22 20:27:58 -0700 | [diff] [blame] | 2095 | static void fib6_nh_age_exceptions(const struct fib6_nh *nh, |
David Ahern | c0b220c | 2019-05-22 20:27:57 -0700 | [diff] [blame] | 2096 | struct fib6_gc_args *gc_args, |
| 2097 | unsigned long now) |
Wei Wang | c757faa | 2017-10-06 12:06:01 -0700 | [diff] [blame] | 2098 | { |
| 2099 | struct rt6_exception_bucket *bucket; |
| 2100 | struct rt6_exception *rt6_ex; |
| 2101 | struct hlist_node *tmp; |
| 2102 | int i; |
| 2103 | |
David Ahern | cc5c073 | 2019-05-22 20:27:58 -0700 | [diff] [blame] | 2104 | if (!rcu_access_pointer(nh->rt6i_exception_bucket)) |
Wei Wang | c757faa | 2017-10-06 12:06:01 -0700 | [diff] [blame] | 2105 | return; |
| 2106 | |
Eric Dumazet | 1bfa26f | 2018-03-23 07:56:58 -0700 | [diff] [blame] | 2107 | rcu_read_lock_bh(); |
| 2108 | spin_lock(&rt6_exception_lock); |
David Ahern | cc5c073 | 2019-05-22 20:27:58 -0700 | [diff] [blame] | 2109 | bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock); |
Wei Wang | c757faa | 2017-10-06 12:06:01 -0700 | [diff] [blame] | 2110 | if (bucket) { |
| 2111 | for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) { |
| 2112 | hlist_for_each_entry_safe(rt6_ex, tmp, |
| 2113 | &bucket->chain, hlist) { |
| 2114 | rt6_age_examine_exception(bucket, rt6_ex, |
| 2115 | gc_args, now); |
| 2116 | } |
| 2117 | bucket++; |
| 2118 | } |
| 2119 | } |
Eric Dumazet | 1bfa26f | 2018-03-23 07:56:58 -0700 | [diff] [blame] | 2120 | spin_unlock(&rt6_exception_lock); |
| 2121 | rcu_read_unlock_bh(); |
Wei Wang | c757faa | 2017-10-06 12:06:01 -0700 | [diff] [blame] | 2122 | } |
| 2123 | |
David Ahern | e659ba3 | 2019-06-08 14:53:28 -0700 | [diff] [blame] | 2124 | struct fib6_nh_age_excptn_arg { |
| 2125 | struct fib6_gc_args *gc_args; |
| 2126 | unsigned long now; |
| 2127 | }; |
| 2128 | |
| 2129 | static int rt6_nh_age_exceptions(struct fib6_nh *nh, void *_arg) |
| 2130 | { |
| 2131 | struct fib6_nh_age_excptn_arg *arg = _arg; |
| 2132 | |
| 2133 | fib6_nh_age_exceptions(nh, arg->gc_args, arg->now); |
| 2134 | return 0; |
| 2135 | } |
| 2136 | |
David Ahern | cc5c073 | 2019-05-22 20:27:58 -0700 | [diff] [blame] | 2137 | void rt6_age_exceptions(struct fib6_info *f6i, |
David Ahern | c0b220c | 2019-05-22 20:27:57 -0700 | [diff] [blame] | 2138 | struct fib6_gc_args *gc_args, |
| 2139 | unsigned long now) |
| 2140 | { |
David Ahern | e659ba3 | 2019-06-08 14:53:28 -0700 | [diff] [blame] | 2141 | if (f6i->nh) { |
| 2142 | struct fib6_nh_age_excptn_arg arg = { |
| 2143 | .gc_args = gc_args, |
| 2144 | .now = now |
| 2145 | }; |
| 2146 | |
| 2147 | nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_age_exceptions, |
| 2148 | &arg); |
| 2149 | } else { |
| 2150 | fib6_nh_age_exceptions(f6i->fib6_nh, gc_args, now); |
| 2151 | } |
David Ahern | c0b220c | 2019-05-22 20:27:57 -0700 | [diff] [blame] | 2152 | } |
| 2153 | |
David Ahern | 1d053da | 2018-05-09 20:34:21 -0700 | [diff] [blame] | 2154 | /* must be called with rcu lock held */ |
David Ahern | effda4d | 2019-04-16 14:36:10 -0700 | [diff] [blame] | 2155 | int fib6_table_lookup(struct net *net, struct fib6_table *table, int oif, |
| 2156 | struct flowi6 *fl6, struct fib6_result *res, int strict) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2157 | { |
Martin KaFai Lau | 367efcb | 2014-10-20 13:42:45 -0700 | [diff] [blame] | 2158 | struct fib6_node *fn, *saved_fn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2159 | |
David Ahern | 6454743 | 2018-05-09 20:34:19 -0700 | [diff] [blame] | 2160 | fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr); |
Martin KaFai Lau | 367efcb | 2014-10-20 13:42:45 -0700 | [diff] [blame] | 2161 | saved_fn = fn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2162 | |
David Ahern | ca25449 | 2015-10-12 11:47:10 -0700 | [diff] [blame] | 2163 | if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF) |
| 2164 | oif = 0; |
| 2165 | |
Martin KaFai Lau | a3c00e4 | 2014-10-20 13:42:43 -0700 | [diff] [blame] | 2166 | redo_rt6_select: |
David Ahern | effda4d | 2019-04-16 14:36:10 -0700 | [diff] [blame] | 2167 | rt6_select(net, fn, oif, res, strict); |
| 2168 | if (res->f6i == net->ipv6.fib6_null_entry) { |
Martin KaFai Lau | a3c00e4 | 2014-10-20 13:42:43 -0700 | [diff] [blame] | 2169 | fn = fib6_backtrack(fn, &fl6->saddr); |
| 2170 | if (fn) |
| 2171 | goto redo_rt6_select; |
Martin KaFai Lau | 367efcb | 2014-10-20 13:42:45 -0700 | [diff] [blame] | 2172 | else if (strict & RT6_LOOKUP_F_REACHABLE) { |
| 2173 | /* also consider unreachable route */ |
| 2174 | strict &= ~RT6_LOOKUP_F_REACHABLE; |
| 2175 | fn = saved_fn; |
| 2176 | goto redo_rt6_select; |
Martin KaFai Lau | 367efcb | 2014-10-20 13:42:45 -0700 | [diff] [blame] | 2177 | } |
Martin KaFai Lau | a3c00e4 | 2014-10-20 13:42:43 -0700 | [diff] [blame] | 2178 | } |
| 2179 | |
David Ahern | effda4d | 2019-04-16 14:36:10 -0700 | [diff] [blame] | 2180 | trace_fib6_table_lookup(net, res, table, fl6); |
YOSHIFUJI Hideaki | fb9de91 | 2006-03-20 16:59:08 -0800 | [diff] [blame] | 2181 | |
David Ahern | effda4d | 2019-04-16 14:36:10 -0700 | [diff] [blame] | 2182 | return 0; |
David Ahern | 1d053da | 2018-05-09 20:34:21 -0700 | [diff] [blame] | 2183 | } |
| 2184 | |
| 2185 | struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table, |
| 2186 | int oif, struct flowi6 *fl6, |
| 2187 | const struct sk_buff *skb, int flags) |
| 2188 | { |
David Ahern | b1d4099 | 2019-04-16 14:35:59 -0700 | [diff] [blame] | 2189 | struct fib6_result res = {}; |
Wei Wang | 0e09edc | 2019-06-20 17:36:37 -0700 | [diff] [blame] | 2190 | struct rt6_info *rt = NULL; |
David Ahern | 1d053da | 2018-05-09 20:34:21 -0700 | [diff] [blame] | 2191 | int strict = 0; |
| 2192 | |
Wei Wang | 0e09edc | 2019-06-20 17:36:37 -0700 | [diff] [blame] | 2193 | WARN_ON_ONCE((flags & RT6_LOOKUP_F_DST_NOREF) && |
| 2194 | !rcu_read_lock_held()); |
| 2195 | |
David Ahern | 1d053da | 2018-05-09 20:34:21 -0700 | [diff] [blame] | 2196 | strict |= flags & RT6_LOOKUP_F_IFACE; |
| 2197 | strict |= flags & RT6_LOOKUP_F_IGNORE_LINKSTATE; |
| 2198 | if (net->ipv6.devconf_all->forwarding == 0) |
| 2199 | strict |= RT6_LOOKUP_F_REACHABLE; |
| 2200 | |
| 2201 | rcu_read_lock(); |
| 2202 | |
David Ahern | effda4d | 2019-04-16 14:36:10 -0700 | [diff] [blame] | 2203 | fib6_table_lookup(net, table, oif, fl6, &res, strict); |
Wei Wang | 0e09edc | 2019-06-20 17:36:37 -0700 | [diff] [blame] | 2204 | if (res.f6i == net->ipv6.fib6_null_entry) |
| 2205 | goto out; |
David Ahern | 23fb93a | 2018-04-17 17:33:23 -0700 | [diff] [blame] | 2206 | |
David Ahern | b1d4099 | 2019-04-16 14:35:59 -0700 | [diff] [blame] | 2207 | fib6_select_path(net, &res, fl6, oif, false, skb, strict); |
David Ahern | d83009d | 2019-04-09 14:41:17 -0700 | [diff] [blame] | 2208 | |
David Ahern | 23fb93a | 2018-04-17 17:33:23 -0700 | [diff] [blame] | 2209 | /*Search through exception table */ |
David Ahern | 7e4b512 | 2019-04-16 14:36:00 -0700 | [diff] [blame] | 2210 | rt = rt6_find_cached_rt(&res, &fl6->daddr, &fl6->saddr); |
David Ahern | 23fb93a | 2018-04-17 17:33:23 -0700 | [diff] [blame] | 2211 | if (rt) { |
Wei Wang | 0e09edc | 2019-06-20 17:36:37 -0700 | [diff] [blame] | 2212 | goto out; |
Martin KaFai Lau | 3da59bd | 2015-05-22 20:56:03 -0700 | [diff] [blame] | 2213 | } else if (unlikely((fl6->flowi6_flags & FLOWI_FLAG_KNOWN_NH) && |
David Ahern | b1d4099 | 2019-04-16 14:35:59 -0700 | [diff] [blame] | 2214 | !res.nh->fib_nh_gw_family)) { |
Martin KaFai Lau | 3da59bd | 2015-05-22 20:56:03 -0700 | [diff] [blame] | 2215 | /* Create a RTF_CACHE clone which will not be |
| 2216 | * owned by the fib6 tree. It is for the special case where |
| 2217 | * the daddr in the skb during the neighbor look-up is different |
| 2218 | * from the fl6->daddr used to look-up route here. |
| 2219 | */ |
Wei Wang | 0e09edc | 2019-06-20 17:36:37 -0700 | [diff] [blame] | 2220 | rt = ip6_rt_cache_alloc(&res, &fl6->daddr, NULL); |
Martin KaFai Lau | 3da59bd | 2015-05-22 20:56:03 -0700 | [diff] [blame] | 2221 | |
Wei Wang | 0e09edc | 2019-06-20 17:36:37 -0700 | [diff] [blame] | 2222 | if (rt) { |
| 2223 | /* 1 refcnt is taken during ip6_rt_cache_alloc(). |
| 2224 | * As rt6_uncached_list_add() does not consume refcnt, |
| 2225 | * this refcnt is always returned to the caller even |
| 2226 | * if caller sets RT6_LOOKUP_F_DST_NOREF flag. |
Wei Wang | 1cfb71e | 2017-06-17 10:42:33 -0700 | [diff] [blame] | 2227 | */ |
Wei Wang | 0e09edc | 2019-06-20 17:36:37 -0700 | [diff] [blame] | 2228 | rt6_uncached_list_add(rt); |
Wei Wang | 81eb844 | 2017-10-06 12:06:11 -0700 | [diff] [blame] | 2229 | atomic_inc(&net->ipv6.rt6_stats->fib_rt_uncache); |
Wei Wang | 0e09edc | 2019-06-20 17:36:37 -0700 | [diff] [blame] | 2230 | rcu_read_unlock(); |
David Ahern | b811580 | 2015-11-19 12:24:22 -0800 | [diff] [blame] | 2231 | |
Wei Wang | 0e09edc | 2019-06-20 17:36:37 -0700 | [diff] [blame] | 2232 | return rt; |
| 2233 | } |
Martin KaFai Lau | d52d399 | 2015-05-22 20:56:06 -0700 | [diff] [blame] | 2234 | } else { |
| 2235 | /* Get a percpu copy */ |
Eric Dumazet | 951f788 | 2017-10-08 21:07:18 -0700 | [diff] [blame] | 2236 | local_bh_disable(); |
Wei Wang | 0e09edc | 2019-06-20 17:36:37 -0700 | [diff] [blame] | 2237 | rt = rt6_get_pcpu_route(&res); |
Martin KaFai Lau | d52d399 | 2015-05-22 20:56:06 -0700 | [diff] [blame] | 2238 | |
Wei Wang | 0e09edc | 2019-06-20 17:36:37 -0700 | [diff] [blame] | 2239 | if (!rt) |
| 2240 | rt = rt6_make_pcpu_route(net, &res); |
David Ahern | 93531c6 | 2018-04-17 17:33:25 -0700 | [diff] [blame] | 2241 | |
Eric Dumazet | 951f788 | 2017-10-08 21:07:18 -0700 | [diff] [blame] | 2242 | local_bh_enable(); |
Martin KaFai Lau | d52d399 | 2015-05-22 20:56:06 -0700 | [diff] [blame] | 2243 | } |
Wei Wang | 0e09edc | 2019-06-20 17:36:37 -0700 | [diff] [blame] | 2244 | out: |
| 2245 | if (!rt) |
| 2246 | rt = net->ipv6.ip6_null_entry; |
| 2247 | if (!(flags & RT6_LOOKUP_F_DST_NOREF)) |
| 2248 | ip6_hold_safe(net, &rt); |
| 2249 | rcu_read_unlock(); |
| 2250 | |
| 2251 | return rt; |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 2252 | } |
David Ahern | 9ff7438 | 2016-06-13 13:44:19 -0700 | [diff] [blame] | 2253 | EXPORT_SYMBOL_GPL(ip6_pol_route); |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 2254 | |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 2255 | static struct rt6_info *ip6_pol_route_input(struct net *net, |
| 2256 | struct fib6_table *table, |
| 2257 | struct flowi6 *fl6, |
| 2258 | const struct sk_buff *skb, |
| 2259 | int flags) |
Pavel Emelyanov | 4acad72 | 2007-10-15 13:02:51 -0700 | [diff] [blame] | 2260 | { |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 2261 | return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, skb, flags); |
Pavel Emelyanov | 4acad72 | 2007-10-15 13:02:51 -0700 | [diff] [blame] | 2262 | } |
| 2263 | |
Mahesh Bandewar | d409b84 | 2016-09-16 12:59:08 -0700 | [diff] [blame] | 2264 | struct dst_entry *ip6_route_input_lookup(struct net *net, |
| 2265 | struct net_device *dev, |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 2266 | struct flowi6 *fl6, |
| 2267 | const struct sk_buff *skb, |
| 2268 | int flags) |
Shmulik Ladkani | 72331bc | 2012-04-01 04:03:45 +0000 | [diff] [blame] | 2269 | { |
| 2270 | if (rt6_need_strict(&fl6->daddr) && dev->type != ARPHRD_PIMREG) |
| 2271 | flags |= RT6_LOOKUP_F_IFACE; |
| 2272 | |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 2273 | return fib6_rule_lookup(net, fl6, skb, flags, ip6_pol_route_input); |
Shmulik Ladkani | 72331bc | 2012-04-01 04:03:45 +0000 | [diff] [blame] | 2274 | } |
Mahesh Bandewar | d409b84 | 2016-09-16 12:59:08 -0700 | [diff] [blame] | 2275 | EXPORT_SYMBOL_GPL(ip6_route_input_lookup); |
Shmulik Ladkani | 72331bc | 2012-04-01 04:03:45 +0000 | [diff] [blame] | 2276 | |
Jakub Sitnicki | 23aebda | 2017-08-23 09:58:29 +0200 | [diff] [blame] | 2277 | static void ip6_multipath_l3_keys(const struct sk_buff *skb, |
Roopa Prabhu | 5e5d6fe | 2018-02-28 22:43:22 -0500 | [diff] [blame] | 2278 | struct flow_keys *keys, |
| 2279 | struct flow_keys *flkeys) |
Jakub Sitnicki | 23aebda | 2017-08-23 09:58:29 +0200 | [diff] [blame] | 2280 | { |
| 2281 | const struct ipv6hdr *outer_iph = ipv6_hdr(skb); |
| 2282 | const struct ipv6hdr *key_iph = outer_iph; |
Roopa Prabhu | 5e5d6fe | 2018-02-28 22:43:22 -0500 | [diff] [blame] | 2283 | struct flow_keys *_flkeys = flkeys; |
Jakub Sitnicki | 23aebda | 2017-08-23 09:58:29 +0200 | [diff] [blame] | 2284 | const struct ipv6hdr *inner_iph; |
| 2285 | const struct icmp6hdr *icmph; |
| 2286 | struct ipv6hdr _inner_iph; |
Eric Dumazet | cea67a2 | 2018-04-29 09:54:59 -0700 | [diff] [blame] | 2287 | struct icmp6hdr _icmph; |
Jakub Sitnicki | 23aebda | 2017-08-23 09:58:29 +0200 | [diff] [blame] | 2288 | |
| 2289 | if (likely(outer_iph->nexthdr != IPPROTO_ICMPV6)) |
| 2290 | goto out; |
| 2291 | |
Eric Dumazet | cea67a2 | 2018-04-29 09:54:59 -0700 | [diff] [blame] | 2292 | icmph = skb_header_pointer(skb, skb_transport_offset(skb), |
| 2293 | sizeof(_icmph), &_icmph); |
| 2294 | if (!icmph) |
| 2295 | goto out; |
| 2296 | |
Matteo Croce | 54074f1 | 2019-11-02 01:12:04 +0100 | [diff] [blame] | 2297 | if (!icmpv6_is_err(icmph->icmp6_type)) |
Jakub Sitnicki | 23aebda | 2017-08-23 09:58:29 +0200 | [diff] [blame] | 2298 | goto out; |
| 2299 | |
| 2300 | inner_iph = skb_header_pointer(skb, |
| 2301 | skb_transport_offset(skb) + sizeof(*icmph), |
| 2302 | sizeof(_inner_iph), &_inner_iph); |
| 2303 | if (!inner_iph) |
| 2304 | goto out; |
| 2305 | |
| 2306 | key_iph = inner_iph; |
Roopa Prabhu | 5e5d6fe | 2018-02-28 22:43:22 -0500 | [diff] [blame] | 2307 | _flkeys = NULL; |
Jakub Sitnicki | 23aebda | 2017-08-23 09:58:29 +0200 | [diff] [blame] | 2308 | out: |
Roopa Prabhu | 5e5d6fe | 2018-02-28 22:43:22 -0500 | [diff] [blame] | 2309 | if (_flkeys) { |
| 2310 | keys->addrs.v6addrs.src = _flkeys->addrs.v6addrs.src; |
| 2311 | keys->addrs.v6addrs.dst = _flkeys->addrs.v6addrs.dst; |
| 2312 | keys->tags.flow_label = _flkeys->tags.flow_label; |
| 2313 | keys->basic.ip_proto = _flkeys->basic.ip_proto; |
| 2314 | } else { |
| 2315 | keys->addrs.v6addrs.src = key_iph->saddr; |
| 2316 | keys->addrs.v6addrs.dst = key_iph->daddr; |
Michal Kubecek | fa1be7e | 2018-06-04 11:36:05 +0200 | [diff] [blame] | 2317 | keys->tags.flow_label = ip6_flowlabel(key_iph); |
Roopa Prabhu | 5e5d6fe | 2018-02-28 22:43:22 -0500 | [diff] [blame] | 2318 | keys->basic.ip_proto = key_iph->nexthdr; |
| 2319 | } |
Jakub Sitnicki | 23aebda | 2017-08-23 09:58:29 +0200 | [diff] [blame] | 2320 | } |
| 2321 | |
| 2322 | /* 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] | 2323 | u32 rt6_multipath_hash(const struct net *net, const struct flowi6 *fl6, |
| 2324 | const struct sk_buff *skb, struct flow_keys *flkeys) |
Jakub Sitnicki | 23aebda | 2017-08-23 09:58:29 +0200 | [diff] [blame] | 2325 | { |
| 2326 | struct flow_keys hash_keys; |
David Ahern | 9a2a537 | 2018-03-02 08:32:15 -0800 | [diff] [blame] | 2327 | u32 mhash; |
Jakub Sitnicki | 23aebda | 2017-08-23 09:58:29 +0200 | [diff] [blame] | 2328 | |
David S. Miller | bbfa047 | 2018-03-12 11:09:33 -0400 | [diff] [blame] | 2329 | switch (ip6_multipath_hash_policy(net)) { |
David Ahern | b4bac17 | 2018-03-02 08:32:18 -0800 | [diff] [blame] | 2330 | case 0: |
| 2331 | memset(&hash_keys, 0, sizeof(hash_keys)); |
| 2332 | hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; |
| 2333 | if (skb) { |
| 2334 | ip6_multipath_l3_keys(skb, &hash_keys, flkeys); |
| 2335 | } else { |
| 2336 | hash_keys.addrs.v6addrs.src = fl6->saddr; |
| 2337 | hash_keys.addrs.v6addrs.dst = fl6->daddr; |
Michal Kubecek | fa1be7e | 2018-06-04 11:36:05 +0200 | [diff] [blame] | 2338 | hash_keys.tags.flow_label = (__force u32)flowi6_get_flowlabel(fl6); |
David Ahern | b4bac17 | 2018-03-02 08:32:18 -0800 | [diff] [blame] | 2339 | hash_keys.basic.ip_proto = fl6->flowi6_proto; |
| 2340 | } |
| 2341 | break; |
| 2342 | case 1: |
| 2343 | if (skb) { |
| 2344 | unsigned int flag = FLOW_DISSECTOR_F_STOP_AT_ENCAP; |
| 2345 | struct flow_keys keys; |
| 2346 | |
| 2347 | /* short-circuit if we already have L4 hash present */ |
| 2348 | if (skb->l4_hash) |
| 2349 | return skb_get_hash_raw(skb) >> 1; |
| 2350 | |
| 2351 | memset(&hash_keys, 0, sizeof(hash_keys)); |
| 2352 | |
| 2353 | if (!flkeys) { |
| 2354 | skb_flow_dissect_flow_keys(skb, &keys, flag); |
| 2355 | flkeys = &keys; |
| 2356 | } |
| 2357 | hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; |
| 2358 | hash_keys.addrs.v6addrs.src = flkeys->addrs.v6addrs.src; |
| 2359 | hash_keys.addrs.v6addrs.dst = flkeys->addrs.v6addrs.dst; |
| 2360 | hash_keys.ports.src = flkeys->ports.src; |
| 2361 | hash_keys.ports.dst = flkeys->ports.dst; |
| 2362 | hash_keys.basic.ip_proto = flkeys->basic.ip_proto; |
| 2363 | } else { |
| 2364 | memset(&hash_keys, 0, sizeof(hash_keys)); |
| 2365 | hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; |
| 2366 | hash_keys.addrs.v6addrs.src = fl6->saddr; |
| 2367 | hash_keys.addrs.v6addrs.dst = fl6->daddr; |
| 2368 | hash_keys.ports.src = fl6->fl6_sport; |
| 2369 | hash_keys.ports.dst = fl6->fl6_dport; |
| 2370 | hash_keys.basic.ip_proto = fl6->flowi6_proto; |
| 2371 | } |
| 2372 | break; |
Stephen Suryaputra | d8f74f0 | 2019-07-06 10:55:18 -0400 | [diff] [blame] | 2373 | case 2: |
| 2374 | memset(&hash_keys, 0, sizeof(hash_keys)); |
| 2375 | hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; |
| 2376 | if (skb) { |
| 2377 | struct flow_keys keys; |
| 2378 | |
| 2379 | if (!flkeys) { |
| 2380 | skb_flow_dissect_flow_keys(skb, &keys, 0); |
| 2381 | flkeys = &keys; |
| 2382 | } |
| 2383 | |
| 2384 | /* Inner can be v4 or v6 */ |
| 2385 | if (flkeys->control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) { |
| 2386 | hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS; |
| 2387 | hash_keys.addrs.v4addrs.src = flkeys->addrs.v4addrs.src; |
| 2388 | hash_keys.addrs.v4addrs.dst = flkeys->addrs.v4addrs.dst; |
| 2389 | } else if (flkeys->control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) { |
| 2390 | hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; |
| 2391 | hash_keys.addrs.v6addrs.src = flkeys->addrs.v6addrs.src; |
| 2392 | hash_keys.addrs.v6addrs.dst = flkeys->addrs.v6addrs.dst; |
| 2393 | hash_keys.tags.flow_label = flkeys->tags.flow_label; |
| 2394 | hash_keys.basic.ip_proto = flkeys->basic.ip_proto; |
| 2395 | } else { |
| 2396 | /* Same as case 0 */ |
| 2397 | hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; |
| 2398 | ip6_multipath_l3_keys(skb, &hash_keys, flkeys); |
| 2399 | } |
| 2400 | } else { |
| 2401 | /* Same as case 0 */ |
| 2402 | hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; |
| 2403 | hash_keys.addrs.v6addrs.src = fl6->saddr; |
| 2404 | hash_keys.addrs.v6addrs.dst = fl6->daddr; |
| 2405 | hash_keys.tags.flow_label = (__force u32)flowi6_get_flowlabel(fl6); |
| 2406 | hash_keys.basic.ip_proto = fl6->flowi6_proto; |
| 2407 | } |
| 2408 | break; |
Jakub Sitnicki | 23aebda | 2017-08-23 09:58:29 +0200 | [diff] [blame] | 2409 | } |
David Ahern | 9a2a537 | 2018-03-02 08:32:15 -0800 | [diff] [blame] | 2410 | mhash = flow_hash_from_keys(&hash_keys); |
Jakub Sitnicki | 23aebda | 2017-08-23 09:58:29 +0200 | [diff] [blame] | 2411 | |
David Ahern | 9a2a537 | 2018-03-02 08:32:15 -0800 | [diff] [blame] | 2412 | return mhash >> 1; |
Jakub Sitnicki | 23aebda | 2017-08-23 09:58:29 +0200 | [diff] [blame] | 2413 | } |
| 2414 | |
Wei Wang | 67f415d | 2019-06-20 17:36:40 -0700 | [diff] [blame] | 2415 | /* Called with rcu held */ |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 2416 | void ip6_route_input(struct sk_buff *skb) |
| 2417 | { |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 2418 | const struct ipv6hdr *iph = ipv6_hdr(skb); |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 2419 | struct net *net = dev_net(skb->dev); |
Wei Wang | 67f415d | 2019-06-20 17:36:40 -0700 | [diff] [blame] | 2420 | int flags = RT6_LOOKUP_F_HAS_SADDR | RT6_LOOKUP_F_DST_NOREF; |
Jiri Benc | 904af04 | 2015-08-20 13:56:31 +0200 | [diff] [blame] | 2421 | struct ip_tunnel_info *tun_info; |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 2422 | struct flowi6 fl6 = { |
David Ahern | e0d56fd | 2016-09-10 12:09:57 -0700 | [diff] [blame] | 2423 | .flowi6_iif = skb->dev->ifindex, |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 2424 | .daddr = iph->daddr, |
| 2425 | .saddr = iph->saddr, |
YOSHIFUJI Hideaki / 吉藤英明 | 6502ca5 | 2013-01-13 05:01:51 +0000 | [diff] [blame] | 2426 | .flowlabel = ip6_flowinfo(iph), |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 2427 | .flowi6_mark = skb->mark, |
| 2428 | .flowi6_proto = iph->nexthdr, |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 2429 | }; |
Roopa Prabhu | 5e5d6fe | 2018-02-28 22:43:22 -0500 | [diff] [blame] | 2430 | struct flow_keys *flkeys = NULL, _flkeys; |
Thomas Graf | adaa70b | 2006-10-13 15:01:03 -0700 | [diff] [blame] | 2431 | |
Jiri Benc | 904af04 | 2015-08-20 13:56:31 +0200 | [diff] [blame] | 2432 | tun_info = skb_tunnel_info(skb); |
Jiri Benc | 46fa062 | 2015-08-28 20:48:19 +0200 | [diff] [blame] | 2433 | if (tun_info && !(tun_info->mode & IP_TUNNEL_INFO_TX)) |
Jiri Benc | 904af04 | 2015-08-20 13:56:31 +0200 | [diff] [blame] | 2434 | fl6.flowi6_tun_key.tun_id = tun_info->key.tun_id; |
Roopa Prabhu | 5e5d6fe | 2018-02-28 22:43:22 -0500 | [diff] [blame] | 2435 | |
| 2436 | if (fib6_rules_early_flow_dissect(net, skb, &fl6, &_flkeys)) |
| 2437 | flkeys = &_flkeys; |
| 2438 | |
Jakub Sitnicki | 23aebda | 2017-08-23 09:58:29 +0200 | [diff] [blame] | 2439 | if (unlikely(fl6.flowi6_proto == IPPROTO_ICMPV6)) |
David Ahern | b4bac17 | 2018-03-02 08:32:18 -0800 | [diff] [blame] | 2440 | fl6.mp_hash = rt6_multipath_hash(net, &fl6, skb, flkeys); |
Jiri Benc | 06e9d04 | 2015-08-20 13:56:26 +0200 | [diff] [blame] | 2441 | skb_dst_drop(skb); |
Wei Wang | 67f415d | 2019-06-20 17:36:40 -0700 | [diff] [blame] | 2442 | skb_dst_set_noref(skb, ip6_route_input_lookup(net, skb->dev, |
| 2443 | &fl6, skb, flags)); |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 2444 | } |
| 2445 | |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 2446 | static struct rt6_info *ip6_pol_route_output(struct net *net, |
| 2447 | struct fib6_table *table, |
| 2448 | struct flowi6 *fl6, |
| 2449 | const struct sk_buff *skb, |
| 2450 | int flags) |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 2451 | { |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 2452 | return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, skb, flags); |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 2453 | } |
| 2454 | |
Wei Wang | 7d9e5f4 | 2019-06-20 17:36:41 -0700 | [diff] [blame] | 2455 | struct dst_entry *ip6_route_output_flags_noref(struct net *net, |
| 2456 | const struct sock *sk, |
| 2457 | struct flowi6 *fl6, int flags) |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 2458 | { |
David Ahern | d46a9d6 | 2015-10-21 08:42:22 -0700 | [diff] [blame] | 2459 | bool any_src; |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 2460 | |
Robert Shearman | 3ede0bb | 2018-09-19 13:56:53 +0100 | [diff] [blame] | 2461 | if (ipv6_addr_type(&fl6->daddr) & |
| 2462 | (IPV6_ADDR_MULTICAST | IPV6_ADDR_LINKLOCAL)) { |
David Ahern | 4c1feac | 2016-09-10 12:09:56 -0700 | [diff] [blame] | 2463 | struct dst_entry *dst; |
| 2464 | |
Wei Wang | 7d9e5f4 | 2019-06-20 17:36:41 -0700 | [diff] [blame] | 2465 | /* This function does not take refcnt on the dst */ |
David Ahern | 4c1feac | 2016-09-10 12:09:56 -0700 | [diff] [blame] | 2466 | dst = l3mdev_link_scope_lookup(net, fl6); |
| 2467 | if (dst) |
| 2468 | return dst; |
| 2469 | } |
David Ahern | ca25449 | 2015-10-12 11:47:10 -0700 | [diff] [blame] | 2470 | |
Pavel Emelyanov | 1fb9489 | 2012-08-08 21:53:36 +0000 | [diff] [blame] | 2471 | fl6->flowi6_iif = LOOPBACK_IFINDEX; |
David McCullough | 4dc27d1c | 2012-06-25 15:42:26 +0000 | [diff] [blame] | 2472 | |
Wei Wang | 7d9e5f4 | 2019-06-20 17:36:41 -0700 | [diff] [blame] | 2473 | flags |= RT6_LOOKUP_F_DST_NOREF; |
David Ahern | d46a9d6 | 2015-10-21 08:42:22 -0700 | [diff] [blame] | 2474 | any_src = ipv6_addr_any(&fl6->saddr); |
David Ahern | 741a11d | 2015-09-28 10:12:13 -0700 | [diff] [blame] | 2475 | if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr) || |
David Ahern | d46a9d6 | 2015-10-21 08:42:22 -0700 | [diff] [blame] | 2476 | (fl6->flowi6_oif && any_src)) |
YOSHIFUJI Hideaki | 77d16f4 | 2006-08-23 17:25:05 -0700 | [diff] [blame] | 2477 | flags |= RT6_LOOKUP_F_IFACE; |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 2478 | |
David Ahern | d46a9d6 | 2015-10-21 08:42:22 -0700 | [diff] [blame] | 2479 | if (!any_src) |
Thomas Graf | adaa70b | 2006-10-13 15:01:03 -0700 | [diff] [blame] | 2480 | flags |= RT6_LOOKUP_F_HAS_SADDR; |
YOSHIFUJI Hideaki / 吉藤英明 | 0c9a2ac | 2010-03-07 00:14:44 +0000 | [diff] [blame] | 2481 | else if (sk) |
| 2482 | flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs); |
Thomas Graf | adaa70b | 2006-10-13 15:01:03 -0700 | [diff] [blame] | 2483 | |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 2484 | return fib6_rule_lookup(net, fl6, NULL, flags, ip6_pol_route_output); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2485 | } |
Wei Wang | 7d9e5f4 | 2019-06-20 17:36:41 -0700 | [diff] [blame] | 2486 | EXPORT_SYMBOL_GPL(ip6_route_output_flags_noref); |
| 2487 | |
| 2488 | struct dst_entry *ip6_route_output_flags(struct net *net, |
| 2489 | const struct sock *sk, |
| 2490 | struct flowi6 *fl6, |
| 2491 | int flags) |
| 2492 | { |
| 2493 | struct dst_entry *dst; |
| 2494 | struct rt6_info *rt6; |
| 2495 | |
| 2496 | rcu_read_lock(); |
| 2497 | dst = ip6_route_output_flags_noref(net, sk, fl6, flags); |
| 2498 | rt6 = (struct rt6_info *)dst; |
| 2499 | /* For dst cached in uncached_list, refcnt is already taken. */ |
| 2500 | if (list_empty(&rt6->rt6i_uncached) && !dst_hold_safe(dst)) { |
| 2501 | dst = &net->ipv6.ip6_null_entry->dst; |
| 2502 | dst_hold(dst); |
| 2503 | } |
| 2504 | rcu_read_unlock(); |
| 2505 | |
| 2506 | return dst; |
| 2507 | } |
Paolo Abeni | 6f21c96 | 2016-01-29 12:30:19 +0100 | [diff] [blame] | 2508 | EXPORT_SYMBOL_GPL(ip6_route_output_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2509 | |
David S. Miller | 2774c13 | 2011-03-01 14:59:04 -0800 | [diff] [blame] | 2510 | 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] | 2511 | { |
David S. Miller | 5c1e6aa | 2011-04-28 14:13:38 -0700 | [diff] [blame] | 2512 | struct rt6_info *rt, *ort = (struct rt6_info *) dst_orig; |
Wei Wang | 1dbe3252 | 2017-06-17 10:42:26 -0700 | [diff] [blame] | 2513 | struct net_device *loopback_dev = net->loopback_dev; |
David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 2514 | struct dst_entry *new = NULL; |
| 2515 | |
Wei Wang | 1dbe3252 | 2017-06-17 10:42:26 -0700 | [diff] [blame] | 2516 | rt = dst_alloc(&ip6_dst_blackhole_ops, loopback_dev, 1, |
Steffen Klassert | 62cf27e | 2017-10-09 08:39:43 +0200 | [diff] [blame] | 2517 | DST_OBSOLETE_DEAD, 0); |
David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 2518 | if (rt) { |
Martin KaFai Lau | 0a1f596 | 2015-10-15 16:39:58 -0700 | [diff] [blame] | 2519 | rt6_info_init(rt); |
Wei Wang | 81eb844 | 2017-10-06 12:06:11 -0700 | [diff] [blame] | 2520 | atomic_inc(&net->ipv6.rt6_stats->fib_rt_alloc); |
Martin KaFai Lau | 0a1f596 | 2015-10-15 16:39:58 -0700 | [diff] [blame] | 2521 | |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 2522 | new = &rt->dst; |
David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 2523 | new->__use = 1; |
Herbert Xu | 352e512 | 2007-11-13 21:34:06 -0800 | [diff] [blame] | 2524 | new->input = dst_discard; |
Eric W. Biederman | ede2059 | 2015-10-07 16:48:47 -0500 | [diff] [blame] | 2525 | new->output = dst_discard_out; |
David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 2526 | |
Martin KaFai Lau | 0a1f596 | 2015-10-15 16:39:58 -0700 | [diff] [blame] | 2527 | dst_copy_metrics(new, &ort->dst); |
David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 2528 | |
Wei Wang | 1dbe3252 | 2017-06-17 10:42:26 -0700 | [diff] [blame] | 2529 | rt->rt6i_idev = in6_dev_get(loopback_dev); |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 2530 | rt->rt6i_gateway = ort->rt6i_gateway; |
Martin KaFai Lau | 0a1f596 | 2015-10-15 16:39:58 -0700 | [diff] [blame] | 2531 | rt->rt6i_flags = ort->rt6i_flags & ~RTF_PCPU; |
David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 2532 | |
| 2533 | memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key)); |
| 2534 | #ifdef CONFIG_IPV6_SUBTREES |
| 2535 | memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key)); |
| 2536 | #endif |
David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 2537 | } |
| 2538 | |
David S. Miller | 69ead7a | 2011-03-01 14:45:33 -0800 | [diff] [blame] | 2539 | dst_release(dst_orig); |
| 2540 | return new ? new : ERR_PTR(-ENOMEM); |
David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 2541 | } |
David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 2542 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2543 | /* |
| 2544 | * Destination cache support functions |
| 2545 | */ |
| 2546 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 2547 | static bool fib6_check(struct fib6_info *f6i, u32 cookie) |
Martin KaFai Lau | 3da59bd | 2015-05-22 20:56:03 -0700 | [diff] [blame] | 2548 | { |
Steffen Klassert | 3614364 | 2017-08-25 09:05:42 +0200 | [diff] [blame] | 2549 | u32 rt_cookie = 0; |
Wei Wang | c5cff85 | 2017-08-21 09:47:10 -0700 | [diff] [blame] | 2550 | |
David Ahern | 8ae8697 | 2018-04-20 15:38:03 -0700 | [diff] [blame] | 2551 | if (!fib6_get_cookie_safe(f6i, &rt_cookie) || rt_cookie != cookie) |
David Ahern | 93531c6 | 2018-04-17 17:33:25 -0700 | [diff] [blame] | 2552 | return false; |
| 2553 | |
| 2554 | if (fib6_check_expired(f6i)) |
| 2555 | return false; |
| 2556 | |
| 2557 | return true; |
| 2558 | } |
| 2559 | |
David Ahern | a68886a | 2018-04-20 15:38:02 -0700 | [diff] [blame] | 2560 | static struct dst_entry *rt6_check(struct rt6_info *rt, |
| 2561 | struct fib6_info *from, |
| 2562 | u32 cookie) |
Martin KaFai Lau | 3da59bd | 2015-05-22 20:56:03 -0700 | [diff] [blame] | 2563 | { |
Wei Wang | c5cff85 | 2017-08-21 09:47:10 -0700 | [diff] [blame] | 2564 | u32 rt_cookie = 0; |
| 2565 | |
David Ahern | 49d05fe | 2019-07-17 15:08:43 -0700 | [diff] [blame] | 2566 | if (!from || !fib6_get_cookie_safe(from, &rt_cookie) || |
David Ahern | 93531c6 | 2018-04-17 17:33:25 -0700 | [diff] [blame] | 2567 | rt_cookie != cookie) |
Martin KaFai Lau | 3da59bd | 2015-05-22 20:56:03 -0700 | [diff] [blame] | 2568 | return NULL; |
| 2569 | |
| 2570 | if (rt6_check_expired(rt)) |
| 2571 | return NULL; |
| 2572 | |
| 2573 | return &rt->dst; |
| 2574 | } |
| 2575 | |
David Ahern | a68886a | 2018-04-20 15:38:02 -0700 | [diff] [blame] | 2576 | static struct dst_entry *rt6_dst_from_check(struct rt6_info *rt, |
| 2577 | struct fib6_info *from, |
| 2578 | u32 cookie) |
Martin KaFai Lau | 3da59bd | 2015-05-22 20:56:03 -0700 | [diff] [blame] | 2579 | { |
Martin KaFai Lau | 5973fb1 | 2015-11-11 11:51:07 -0800 | [diff] [blame] | 2580 | if (!__rt6_check_expired(rt) && |
| 2581 | rt->dst.obsolete == DST_OBSOLETE_FORCE_CHK && |
David Ahern | a68886a | 2018-04-20 15:38:02 -0700 | [diff] [blame] | 2582 | fib6_check(from, cookie)) |
Martin KaFai Lau | 3da59bd | 2015-05-22 20:56:03 -0700 | [diff] [blame] | 2583 | return &rt->dst; |
| 2584 | else |
| 2585 | return NULL; |
| 2586 | } |
| 2587 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2588 | static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie) |
| 2589 | { |
David Ahern | a87b7dc | 2018-04-20 15:38:00 -0700 | [diff] [blame] | 2590 | struct dst_entry *dst_ret; |
David Ahern | a68886a | 2018-04-20 15:38:02 -0700 | [diff] [blame] | 2591 | struct fib6_info *from; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2592 | struct rt6_info *rt; |
| 2593 | |
David Ahern | a87b7dc | 2018-04-20 15:38:00 -0700 | [diff] [blame] | 2594 | rt = container_of(dst, struct rt6_info, dst); |
| 2595 | |
| 2596 | rcu_read_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2597 | |
Nicolas Dichtel | 6f3118b | 2012-09-10 22:09:46 +0000 | [diff] [blame] | 2598 | /* All IPV6 dsts are created with ->obsolete set to the value |
| 2599 | * DST_OBSOLETE_FORCE_CHK which forces validation calls down |
| 2600 | * into this function always. |
| 2601 | */ |
Hannes Frederic Sowa | e3bc10b | 2013-10-24 07:48:24 +0200 | [diff] [blame] | 2602 | |
David Ahern | a68886a | 2018-04-20 15:38:02 -0700 | [diff] [blame] | 2603 | from = rcu_dereference(rt->from); |
Martin KaFai Lau | 4b32b5a | 2015-04-28 13:03:06 -0700 | [diff] [blame] | 2604 | |
David Ahern | a68886a | 2018-04-20 15:38:02 -0700 | [diff] [blame] | 2605 | if (from && (rt->rt6i_flags & RTF_PCPU || |
| 2606 | unlikely(!list_empty(&rt->rt6i_uncached)))) |
| 2607 | dst_ret = rt6_dst_from_check(rt, from, cookie); |
Martin KaFai Lau | 3da59bd | 2015-05-22 20:56:03 -0700 | [diff] [blame] | 2608 | else |
David Ahern | a68886a | 2018-04-20 15:38:02 -0700 | [diff] [blame] | 2609 | dst_ret = rt6_check(rt, from, cookie); |
David Ahern | a87b7dc | 2018-04-20 15:38:00 -0700 | [diff] [blame] | 2610 | |
| 2611 | rcu_read_unlock(); |
| 2612 | |
| 2613 | return dst_ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2614 | } |
| 2615 | |
| 2616 | static struct dst_entry *ip6_negative_advice(struct dst_entry *dst) |
| 2617 | { |
| 2618 | struct rt6_info *rt = (struct rt6_info *) dst; |
| 2619 | |
| 2620 | if (rt) { |
YOSHIFUJI Hideaki / 吉藤英明 | 54c1a85 | 2010-03-28 07:15:45 +0000 | [diff] [blame] | 2621 | if (rt->rt6i_flags & RTF_CACHE) { |
David Ahern | c3c14da | 2018-04-23 11:32:06 -0700 | [diff] [blame] | 2622 | rcu_read_lock(); |
YOSHIFUJI Hideaki / 吉藤英明 | 54c1a85 | 2010-03-28 07:15:45 +0000 | [diff] [blame] | 2623 | if (rt6_check_expired(rt)) { |
David Ahern | 93531c6 | 2018-04-17 17:33:25 -0700 | [diff] [blame] | 2624 | rt6_remove_exception_rt(rt); |
YOSHIFUJI Hideaki / 吉藤英明 | 54c1a85 | 2010-03-28 07:15:45 +0000 | [diff] [blame] | 2625 | dst = NULL; |
| 2626 | } |
David Ahern | c3c14da | 2018-04-23 11:32:06 -0700 | [diff] [blame] | 2627 | rcu_read_unlock(); |
YOSHIFUJI Hideaki / 吉藤英明 | 54c1a85 | 2010-03-28 07:15:45 +0000 | [diff] [blame] | 2628 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2629 | dst_release(dst); |
YOSHIFUJI Hideaki / 吉藤英明 | 54c1a85 | 2010-03-28 07:15:45 +0000 | [diff] [blame] | 2630 | dst = NULL; |
| 2631 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2632 | } |
YOSHIFUJI Hideaki / 吉藤英明 | 54c1a85 | 2010-03-28 07:15:45 +0000 | [diff] [blame] | 2633 | return dst; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2634 | } |
| 2635 | |
| 2636 | static void ip6_link_failure(struct sk_buff *skb) |
| 2637 | { |
| 2638 | struct rt6_info *rt; |
| 2639 | |
Alexey Dobriyan | 3ffe533 | 2010-02-18 08:25:24 +0000 | [diff] [blame] | 2640 | icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2641 | |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 2642 | rt = (struct rt6_info *) skb_dst(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2643 | if (rt) { |
David Ahern | 8a14e46 | 2018-04-23 11:32:07 -0700 | [diff] [blame] | 2644 | rcu_read_lock(); |
Hannes Frederic Sowa | 1eb4f75 | 2013-07-10 23:00:57 +0200 | [diff] [blame] | 2645 | if (rt->rt6i_flags & RTF_CACHE) { |
Xin Long | 761f602 | 2018-11-14 00:48:28 +0800 | [diff] [blame] | 2646 | rt6_remove_exception_rt(rt); |
Wei Wang | c5cff85 | 2017-08-21 09:47:10 -0700 | [diff] [blame] | 2647 | } else { |
David Ahern | a68886a | 2018-04-20 15:38:02 -0700 | [diff] [blame] | 2648 | struct fib6_info *from; |
Wei Wang | c5cff85 | 2017-08-21 09:47:10 -0700 | [diff] [blame] | 2649 | struct fib6_node *fn; |
| 2650 | |
David Ahern | a68886a | 2018-04-20 15:38:02 -0700 | [diff] [blame] | 2651 | from = rcu_dereference(rt->from); |
| 2652 | if (from) { |
| 2653 | fn = rcu_dereference(from->fib6_node); |
| 2654 | if (fn && (rt->rt6i_flags & RTF_DEFAULT)) |
| 2655 | fn->fn_sernum = -1; |
| 2656 | } |
Hannes Frederic Sowa | 1eb4f75 | 2013-07-10 23:00:57 +0200 | [diff] [blame] | 2657 | } |
David Ahern | 8a14e46 | 2018-04-23 11:32:07 -0700 | [diff] [blame] | 2658 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2659 | } |
| 2660 | } |
| 2661 | |
David Ahern | 6a3e030 | 2018-04-20 15:37:57 -0700 | [diff] [blame] | 2662 | static void rt6_update_expires(struct rt6_info *rt0, int timeout) |
| 2663 | { |
David Ahern | a68886a | 2018-04-20 15:38:02 -0700 | [diff] [blame] | 2664 | if (!(rt0->rt6i_flags & RTF_EXPIRES)) { |
| 2665 | struct fib6_info *from; |
| 2666 | |
| 2667 | rcu_read_lock(); |
| 2668 | from = rcu_dereference(rt0->from); |
| 2669 | if (from) |
| 2670 | rt0->dst.expires = from->expires; |
| 2671 | rcu_read_unlock(); |
| 2672 | } |
David Ahern | 6a3e030 | 2018-04-20 15:37:57 -0700 | [diff] [blame] | 2673 | |
| 2674 | dst_set_expires(&rt0->dst, timeout); |
| 2675 | rt0->rt6i_flags |= RTF_EXPIRES; |
| 2676 | } |
| 2677 | |
Martin KaFai Lau | 45e4fd2 | 2015-05-22 20:56:00 -0700 | [diff] [blame] | 2678 | static void rt6_do_update_pmtu(struct rt6_info *rt, u32 mtu) |
| 2679 | { |
| 2680 | struct net *net = dev_net(rt->dst.dev); |
| 2681 | |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 2682 | dst_metric_set(&rt->dst, RTAX_MTU, mtu); |
Martin KaFai Lau | 45e4fd2 | 2015-05-22 20:56:00 -0700 | [diff] [blame] | 2683 | rt->rt6i_flags |= RTF_MODIFIED; |
Martin KaFai Lau | 45e4fd2 | 2015-05-22 20:56:00 -0700 | [diff] [blame] | 2684 | rt6_update_expires(rt, net->ipv6.sysctl.ip6_rt_mtu_expires); |
| 2685 | } |
| 2686 | |
Martin KaFai Lau | 0d3f6d2 | 2015-11-11 11:51:06 -0800 | [diff] [blame] | 2687 | static bool rt6_cache_allowed_for_pmtu(const struct rt6_info *rt) |
| 2688 | { |
| 2689 | return !(rt->rt6i_flags & RTF_CACHE) && |
Paolo Abeni | 1490ed2 | 2019-02-15 18:15:37 +0100 | [diff] [blame] | 2690 | (rt->rt6i_flags & RTF_PCPU || rcu_access_pointer(rt->from)); |
Martin KaFai Lau | 0d3f6d2 | 2015-11-11 11:51:06 -0800 | [diff] [blame] | 2691 | } |
| 2692 | |
Martin KaFai Lau | 45e4fd2 | 2015-05-22 20:56:00 -0700 | [diff] [blame] | 2693 | 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] | 2694 | const struct ipv6hdr *iph, u32 mtu, |
| 2695 | bool confirm_neigh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2696 | { |
Julian Anastasov | 0dec879 | 2017-02-06 23:14:16 +0200 | [diff] [blame] | 2697 | const struct in6_addr *daddr, *saddr; |
Ian Morris | 67ba415 | 2014-08-24 21:53:10 +0100 | [diff] [blame] | 2698 | struct rt6_info *rt6 = (struct rt6_info *)dst; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2699 | |
Xin Long | 19bda36 | 2016-10-28 18:18:01 +0800 | [diff] [blame] | 2700 | if (dst_metric_locked(dst, RTAX_MTU)) |
| 2701 | return; |
| 2702 | |
Julian Anastasov | 0dec879 | 2017-02-06 23:14:16 +0200 | [diff] [blame] | 2703 | if (iph) { |
| 2704 | daddr = &iph->daddr; |
| 2705 | saddr = &iph->saddr; |
| 2706 | } else if (sk) { |
| 2707 | daddr = &sk->sk_v6_daddr; |
| 2708 | saddr = &inet6_sk(sk)->saddr; |
| 2709 | } else { |
| 2710 | daddr = NULL; |
| 2711 | saddr = NULL; |
| 2712 | } |
Hangbin Liu | bd085ef | 2019-12-22 10:51:09 +0800 | [diff] [blame] | 2713 | |
| 2714 | if (confirm_neigh) |
| 2715 | dst_confirm_neigh(dst, daddr); |
| 2716 | |
Martin KaFai Lau | 45e4fd2 | 2015-05-22 20:56:00 -0700 | [diff] [blame] | 2717 | mtu = max_t(u32, mtu, IPV6_MIN_MTU); |
| 2718 | if (mtu >= dst_mtu(dst)) |
| 2719 | return; |
David S. Miller | 81aded2 | 2012-06-15 14:54:11 -0700 | [diff] [blame] | 2720 | |
Martin KaFai Lau | 0d3f6d2 | 2015-11-11 11:51:06 -0800 | [diff] [blame] | 2721 | if (!rt6_cache_allowed_for_pmtu(rt6)) { |
Martin KaFai Lau | 45e4fd2 | 2015-05-22 20:56:00 -0700 | [diff] [blame] | 2722 | rt6_do_update_pmtu(rt6, mtu); |
Wei Wang | 2b760fc | 2017-10-06 12:06:03 -0700 | [diff] [blame] | 2723 | /* update rt6_ex->stamp for cache */ |
| 2724 | if (rt6->rt6i_flags & RTF_CACHE) |
| 2725 | rt6_update_exception_stamp_rt(rt6); |
Julian Anastasov | 0dec879 | 2017-02-06 23:14:16 +0200 | [diff] [blame] | 2726 | } else if (daddr) { |
David Ahern | 85bd05d | 2019-04-16 14:36:01 -0700 | [diff] [blame] | 2727 | struct fib6_result res = {}; |
Martin KaFai Lau | 45e4fd2 | 2015-05-22 20:56:00 -0700 | [diff] [blame] | 2728 | struct rt6_info *nrt6; |
Hagen Paul Pfeifer | 9d28971 | 2015-01-15 22:34:25 +0100 | [diff] [blame] | 2729 | |
David Ahern | 4d85cd0 | 2018-04-20 15:37:59 -0700 | [diff] [blame] | 2730 | rcu_read_lock(); |
David Ahern | 85bd05d | 2019-04-16 14:36:01 -0700 | [diff] [blame] | 2731 | res.f6i = rcu_dereference(rt6->from); |
David Ahern | 43a4b60 | 2019-08-01 15:18:08 -0700 | [diff] [blame] | 2732 | if (!res.f6i) |
| 2733 | goto out_unlock; |
| 2734 | |
David Ahern | 7d21fec | 2019-04-16 14:36:11 -0700 | [diff] [blame] | 2735 | res.fib6_flags = res.f6i->fib6_flags; |
| 2736 | res.fib6_type = res.f6i->fib6_type; |
| 2737 | |
David Ahern | 2d44234 | 2019-06-08 14:53:31 -0700 | [diff] [blame] | 2738 | if (res.f6i->nh) { |
| 2739 | struct fib6_nh_match_arg arg = { |
| 2740 | .dev = dst->dev, |
| 2741 | .gw = &rt6->rt6i_gateway, |
| 2742 | }; |
| 2743 | |
| 2744 | nexthop_for_each_fib6_nh(res.f6i->nh, |
| 2745 | fib6_nh_find_match, &arg); |
| 2746 | |
| 2747 | /* fib6_info uses a nexthop that does not have fib6_nh |
| 2748 | * using the dst->dev + gw. Should be impossible. |
| 2749 | */ |
David Ahern | 43a4b60 | 2019-08-01 15:18:08 -0700 | [diff] [blame] | 2750 | if (!arg.match) |
| 2751 | goto out_unlock; |
David Ahern | 2d44234 | 2019-06-08 14:53:31 -0700 | [diff] [blame] | 2752 | |
| 2753 | res.nh = arg.match; |
| 2754 | } else { |
| 2755 | res.nh = res.f6i->fib6_nh; |
| 2756 | } |
| 2757 | |
David Ahern | 85bd05d | 2019-04-16 14:36:01 -0700 | [diff] [blame] | 2758 | nrt6 = ip6_rt_cache_alloc(&res, daddr, saddr); |
Martin KaFai Lau | 45e4fd2 | 2015-05-22 20:56:00 -0700 | [diff] [blame] | 2759 | if (nrt6) { |
| 2760 | rt6_do_update_pmtu(nrt6, mtu); |
David Ahern | 5012f0a | 2019-04-16 14:36:05 -0700 | [diff] [blame] | 2761 | if (rt6_insert_exception(nrt6, &res)) |
Wei Wang | 2b760fc | 2017-10-06 12:06:03 -0700 | [diff] [blame] | 2762 | dst_release_immediate(&nrt6->dst); |
Martin KaFai Lau | 45e4fd2 | 2015-05-22 20:56:00 -0700 | [diff] [blame] | 2763 | } |
David Ahern | 43a4b60 | 2019-08-01 15:18:08 -0700 | [diff] [blame] | 2764 | out_unlock: |
David Ahern | a68886a | 2018-04-20 15:38:02 -0700 | [diff] [blame] | 2765 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2766 | } |
| 2767 | } |
| 2768 | |
Martin KaFai Lau | 45e4fd2 | 2015-05-22 20:56:00 -0700 | [diff] [blame] | 2769 | 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] | 2770 | struct sk_buff *skb, u32 mtu, |
| 2771 | bool confirm_neigh) |
Martin KaFai Lau | 45e4fd2 | 2015-05-22 20:56:00 -0700 | [diff] [blame] | 2772 | { |
Hangbin Liu | bd085ef | 2019-12-22 10:51:09 +0800 | [diff] [blame] | 2773 | __ip6_rt_update_pmtu(dst, sk, skb ? ipv6_hdr(skb) : NULL, mtu, |
| 2774 | confirm_neigh); |
Martin KaFai Lau | 45e4fd2 | 2015-05-22 20:56:00 -0700 | [diff] [blame] | 2775 | } |
| 2776 | |
David S. Miller | 42ae66c | 2012-06-15 20:01:57 -0700 | [diff] [blame] | 2777 | 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] | 2778 | int oif, u32 mark, kuid_t uid) |
David S. Miller | 81aded2 | 2012-06-15 14:54:11 -0700 | [diff] [blame] | 2779 | { |
| 2780 | const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data; |
| 2781 | struct dst_entry *dst; |
Maciej Żenczykowski | dc92095 | 2018-09-29 23:44:51 -0700 | [diff] [blame] | 2782 | struct flowi6 fl6 = { |
| 2783 | .flowi6_oif = oif, |
| 2784 | .flowi6_mark = mark ? mark : IP6_REPLY_MARK(net, skb->mark), |
| 2785 | .daddr = iph->daddr, |
| 2786 | .saddr = iph->saddr, |
| 2787 | .flowlabel = ip6_flowinfo(iph), |
| 2788 | .flowi6_uid = uid, |
| 2789 | }; |
David S. Miller | 81aded2 | 2012-06-15 14:54:11 -0700 | [diff] [blame] | 2790 | |
| 2791 | dst = ip6_route_output(net, NULL, &fl6); |
| 2792 | if (!dst->error) |
Hangbin Liu | bd085ef | 2019-12-22 10:51:09 +0800 | [diff] [blame] | 2793 | __ip6_rt_update_pmtu(dst, NULL, iph, ntohl(mtu), true); |
David S. Miller | 81aded2 | 2012-06-15 14:54:11 -0700 | [diff] [blame] | 2794 | dst_release(dst); |
| 2795 | } |
| 2796 | EXPORT_SYMBOL_GPL(ip6_update_pmtu); |
| 2797 | |
| 2798 | void ip6_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, __be32 mtu) |
| 2799 | { |
David Ahern | 7ddacfa | 2018-11-18 10:45:30 -0800 | [diff] [blame] | 2800 | int oif = sk->sk_bound_dev_if; |
Martin KaFai Lau | 33c162a | 2016-04-11 15:29:36 -0700 | [diff] [blame] | 2801 | struct dst_entry *dst; |
| 2802 | |
David Ahern | 7ddacfa | 2018-11-18 10:45:30 -0800 | [diff] [blame] | 2803 | if (!oif && skb->dev) |
| 2804 | oif = l3mdev_master_ifindex(skb->dev); |
| 2805 | |
| 2806 | 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] | 2807 | |
| 2808 | dst = __sk_dst_get(sk); |
| 2809 | if (!dst || !dst->obsolete || |
| 2810 | dst->ops->check(dst, inet6_sk(sk)->dst_cookie)) |
| 2811 | return; |
| 2812 | |
| 2813 | bh_lock_sock(sk); |
| 2814 | if (!sock_owned_by_user(sk) && !ipv6_addr_v4mapped(&sk->sk_v6_daddr)) |
| 2815 | ip6_datagram_dst_update(sk, false); |
| 2816 | bh_unlock_sock(sk); |
David S. Miller | 81aded2 | 2012-06-15 14:54:11 -0700 | [diff] [blame] | 2817 | } |
| 2818 | EXPORT_SYMBOL_GPL(ip6_sk_update_pmtu); |
| 2819 | |
Alexey Kodanev | 7d6850f | 2018-04-03 15:00:07 +0300 | [diff] [blame] | 2820 | void ip6_sk_dst_store_flow(struct sock *sk, struct dst_entry *dst, |
| 2821 | const struct flowi6 *fl6) |
| 2822 | { |
| 2823 | #ifdef CONFIG_IPV6_SUBTREES |
| 2824 | struct ipv6_pinfo *np = inet6_sk(sk); |
| 2825 | #endif |
| 2826 | |
| 2827 | ip6_dst_store(sk, dst, |
| 2828 | ipv6_addr_equal(&fl6->daddr, &sk->sk_v6_daddr) ? |
| 2829 | &sk->sk_v6_daddr : NULL, |
| 2830 | #ifdef CONFIG_IPV6_SUBTREES |
| 2831 | ipv6_addr_equal(&fl6->saddr, &np->saddr) ? |
| 2832 | &np->saddr : |
| 2833 | #endif |
| 2834 | NULL); |
| 2835 | } |
| 2836 | |
David Ahern | 9b6b35a | 2019-04-16 14:36:02 -0700 | [diff] [blame] | 2837 | static bool ip6_redirect_nh_match(const struct fib6_result *res, |
David Ahern | 0b34eb0 | 2019-04-09 14:41:19 -0700 | [diff] [blame] | 2838 | struct flowi6 *fl6, |
| 2839 | const struct in6_addr *gw, |
| 2840 | struct rt6_info **ret) |
| 2841 | { |
David Ahern | 9b6b35a | 2019-04-16 14:36:02 -0700 | [diff] [blame] | 2842 | const struct fib6_nh *nh = res->nh; |
| 2843 | |
David Ahern | 0b34eb0 | 2019-04-09 14:41:19 -0700 | [diff] [blame] | 2844 | if (nh->fib_nh_flags & RTNH_F_DEAD || !nh->fib_nh_gw_family || |
| 2845 | fl6->flowi6_oif != nh->fib_nh_dev->ifindex) |
| 2846 | return false; |
| 2847 | |
| 2848 | /* rt_cache's gateway might be different from its 'parent' |
| 2849 | * in the case of an ip redirect. |
| 2850 | * So we keep searching in the exception table if the gateway |
| 2851 | * is different. |
| 2852 | */ |
| 2853 | if (!ipv6_addr_equal(gw, &nh->fib_nh_gw6)) { |
| 2854 | struct rt6_info *rt_cache; |
| 2855 | |
David Ahern | 9b6b35a | 2019-04-16 14:36:02 -0700 | [diff] [blame] | 2856 | rt_cache = rt6_find_cached_rt(res, &fl6->daddr, &fl6->saddr); |
David Ahern | 0b34eb0 | 2019-04-09 14:41:19 -0700 | [diff] [blame] | 2857 | if (rt_cache && |
| 2858 | ipv6_addr_equal(gw, &rt_cache->rt6i_gateway)) { |
| 2859 | *ret = rt_cache; |
| 2860 | return true; |
| 2861 | } |
| 2862 | return false; |
| 2863 | } |
| 2864 | return true; |
| 2865 | } |
| 2866 | |
David Ahern | c55c898 | 2019-06-08 14:53:29 -0700 | [diff] [blame] | 2867 | struct fib6_nh_rd_arg { |
| 2868 | struct fib6_result *res; |
| 2869 | struct flowi6 *fl6; |
| 2870 | const struct in6_addr *gw; |
| 2871 | struct rt6_info **ret; |
| 2872 | }; |
| 2873 | |
| 2874 | static int fib6_nh_redirect_match(struct fib6_nh *nh, void *_arg) |
| 2875 | { |
| 2876 | struct fib6_nh_rd_arg *arg = _arg; |
| 2877 | |
| 2878 | arg->res->nh = nh; |
| 2879 | return ip6_redirect_nh_match(arg->res, arg->fl6, arg->gw, arg->ret); |
| 2880 | } |
| 2881 | |
Duan Jiong | b55b76b | 2013-09-04 19:44:21 +0800 | [diff] [blame] | 2882 | /* Handle redirects */ |
| 2883 | struct ip6rd_flowi { |
| 2884 | struct flowi6 fl6; |
| 2885 | struct in6_addr gateway; |
| 2886 | }; |
| 2887 | |
| 2888 | static struct rt6_info *__ip6_route_redirect(struct net *net, |
| 2889 | struct fib6_table *table, |
| 2890 | struct flowi6 *fl6, |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 2891 | const struct sk_buff *skb, |
Duan Jiong | b55b76b | 2013-09-04 19:44:21 +0800 | [diff] [blame] | 2892 | int flags) |
| 2893 | { |
| 2894 | struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl6; |
David Ahern | 0b34eb0 | 2019-04-09 14:41:19 -0700 | [diff] [blame] | 2895 | struct rt6_info *ret = NULL; |
David Ahern | 9b6b35a | 2019-04-16 14:36:02 -0700 | [diff] [blame] | 2896 | struct fib6_result res = {}; |
David Ahern | c55c898 | 2019-06-08 14:53:29 -0700 | [diff] [blame] | 2897 | struct fib6_nh_rd_arg arg = { |
| 2898 | .res = &res, |
| 2899 | .fl6 = fl6, |
| 2900 | .gw = &rdfl->gateway, |
| 2901 | .ret = &ret |
| 2902 | }; |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 2903 | struct fib6_info *rt; |
Duan Jiong | b55b76b | 2013-09-04 19:44:21 +0800 | [diff] [blame] | 2904 | struct fib6_node *fn; |
| 2905 | |
David Ahern | 31680ac | 2019-05-22 15:12:18 -0700 | [diff] [blame] | 2906 | /* l3mdev_update_flow overrides oif if the device is enslaved; in |
| 2907 | * this case we must match on the real ingress device, so reset it |
| 2908 | */ |
| 2909 | if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF) |
| 2910 | fl6->flowi6_oif = skb->dev->ifindex; |
| 2911 | |
Duan Jiong | b55b76b | 2013-09-04 19:44:21 +0800 | [diff] [blame] | 2912 | /* Get the "current" route for this destination and |
Alexander Alemayhu | 67c408c | 2017-01-07 23:53:00 +0100 | [diff] [blame] | 2913 | * check if the redirect has come from appropriate router. |
Duan Jiong | b55b76b | 2013-09-04 19:44:21 +0800 | [diff] [blame] | 2914 | * |
| 2915 | * RFC 4861 specifies that redirects should only be |
| 2916 | * accepted if they come from the nexthop to the target. |
| 2917 | * Due to the way the routes are chosen, this notion |
| 2918 | * is a bit fuzzy and one might need to check all possible |
| 2919 | * routes. |
| 2920 | */ |
| 2921 | |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 2922 | rcu_read_lock(); |
David Ahern | 6454743 | 2018-05-09 20:34:19 -0700 | [diff] [blame] | 2923 | fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr); |
Duan Jiong | b55b76b | 2013-09-04 19:44:21 +0800 | [diff] [blame] | 2924 | restart: |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 2925 | for_each_fib6_node_rt_rcu(fn) { |
David Ahern | 9b6b35a | 2019-04-16 14:36:02 -0700 | [diff] [blame] | 2926 | res.f6i = rt; |
David Ahern | 1489568 | 2018-04-17 17:33:17 -0700 | [diff] [blame] | 2927 | if (fib6_check_expired(rt)) |
Duan Jiong | b55b76b | 2013-09-04 19:44:21 +0800 | [diff] [blame] | 2928 | continue; |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 2929 | if (rt->fib6_flags & RTF_REJECT) |
Duan Jiong | b55b76b | 2013-09-04 19:44:21 +0800 | [diff] [blame] | 2930 | break; |
David Ahern | c55c898 | 2019-06-08 14:53:29 -0700 | [diff] [blame] | 2931 | if (unlikely(rt->nh)) { |
| 2932 | if (nexthop_is_blackhole(rt->nh)) |
| 2933 | continue; |
| 2934 | /* on match, res->nh is filled in and potentially ret */ |
| 2935 | if (nexthop_for_each_fib6_nh(rt->nh, |
| 2936 | fib6_nh_redirect_match, |
| 2937 | &arg)) |
| 2938 | goto out; |
| 2939 | } else { |
| 2940 | res.nh = rt->fib6_nh; |
| 2941 | if (ip6_redirect_nh_match(&res, fl6, &rdfl->gateway, |
| 2942 | &ret)) |
| 2943 | goto out; |
| 2944 | } |
Duan Jiong | b55b76b | 2013-09-04 19:44:21 +0800 | [diff] [blame] | 2945 | } |
| 2946 | |
| 2947 | if (!rt) |
David Ahern | 421842e | 2018-04-17 17:33:18 -0700 | [diff] [blame] | 2948 | rt = net->ipv6.fib6_null_entry; |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 2949 | else if (rt->fib6_flags & RTF_REJECT) { |
David Ahern | 23fb93a | 2018-04-17 17:33:23 -0700 | [diff] [blame] | 2950 | ret = net->ipv6.ip6_null_entry; |
Martin KaFai Lau | b0a1ba5 | 2015-01-20 19:16:02 -0800 | [diff] [blame] | 2951 | goto out; |
| 2952 | } |
| 2953 | |
David Ahern | 421842e | 2018-04-17 17:33:18 -0700 | [diff] [blame] | 2954 | if (rt == net->ipv6.fib6_null_entry) { |
Martin KaFai Lau | a3c00e4 | 2014-10-20 13:42:43 -0700 | [diff] [blame] | 2955 | fn = fib6_backtrack(fn, &fl6->saddr); |
| 2956 | if (fn) |
| 2957 | goto restart; |
Duan Jiong | b55b76b | 2013-09-04 19:44:21 +0800 | [diff] [blame] | 2958 | } |
Martin KaFai Lau | a3c00e4 | 2014-10-20 13:42:43 -0700 | [diff] [blame] | 2959 | |
David Ahern | 9b6b35a | 2019-04-16 14:36:02 -0700 | [diff] [blame] | 2960 | res.f6i = rt; |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 2961 | res.nh = rt->fib6_nh; |
Martin KaFai Lau | b0a1ba5 | 2015-01-20 19:16:02 -0800 | [diff] [blame] | 2962 | out: |
David Ahern | 7d21fec | 2019-04-16 14:36:11 -0700 | [diff] [blame] | 2963 | if (ret) { |
David Ahern | 10585b4 | 2019-03-20 09:24:50 -0700 | [diff] [blame] | 2964 | ip6_hold_safe(net, &ret); |
David Ahern | 7d21fec | 2019-04-16 14:36:11 -0700 | [diff] [blame] | 2965 | } else { |
| 2966 | res.fib6_flags = res.f6i->fib6_flags; |
| 2967 | res.fib6_type = res.f6i->fib6_type; |
David Ahern | 9b6b35a | 2019-04-16 14:36:02 -0700 | [diff] [blame] | 2968 | ret = ip6_create_rt_rcu(&res); |
David Ahern | 7d21fec | 2019-04-16 14:36:11 -0700 | [diff] [blame] | 2969 | } |
Duan Jiong | b55b76b | 2013-09-04 19:44:21 +0800 | [diff] [blame] | 2970 | |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 2971 | rcu_read_unlock(); |
Duan Jiong | b55b76b | 2013-09-04 19:44:21 +0800 | [diff] [blame] | 2972 | |
David Ahern | 8ff2e5b | 2019-04-16 14:36:09 -0700 | [diff] [blame] | 2973 | trace_fib6_table_lookup(net, &res, table, fl6); |
David Ahern | 23fb93a | 2018-04-17 17:33:23 -0700 | [diff] [blame] | 2974 | return ret; |
Duan Jiong | b55b76b | 2013-09-04 19:44:21 +0800 | [diff] [blame] | 2975 | }; |
| 2976 | |
| 2977 | static struct dst_entry *ip6_route_redirect(struct net *net, |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 2978 | const struct flowi6 *fl6, |
| 2979 | const struct sk_buff *skb, |
| 2980 | const struct in6_addr *gateway) |
Duan Jiong | b55b76b | 2013-09-04 19:44:21 +0800 | [diff] [blame] | 2981 | { |
| 2982 | int flags = RT6_LOOKUP_F_HAS_SADDR; |
| 2983 | struct ip6rd_flowi rdfl; |
| 2984 | |
| 2985 | rdfl.fl6 = *fl6; |
| 2986 | rdfl.gateway = *gateway; |
| 2987 | |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 2988 | return fib6_rule_lookup(net, &rdfl.fl6, skb, |
Duan Jiong | b55b76b | 2013-09-04 19:44:21 +0800 | [diff] [blame] | 2989 | flags, __ip6_route_redirect); |
| 2990 | } |
| 2991 | |
Lorenzo Colitti | e2d118a | 2016-11-04 02:23:43 +0900 | [diff] [blame] | 2992 | void ip6_redirect(struct sk_buff *skb, struct net *net, int oif, u32 mark, |
| 2993 | kuid_t uid) |
David S. Miller | 3a5ad2e | 2012-07-12 00:08:07 -0700 | [diff] [blame] | 2994 | { |
| 2995 | const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data; |
| 2996 | struct dst_entry *dst; |
Maciej Żenczykowski | 1f7f10a | 2018-09-29 23:44:48 -0700 | [diff] [blame] | 2997 | struct flowi6 fl6 = { |
| 2998 | .flowi6_iif = LOOPBACK_IFINDEX, |
| 2999 | .flowi6_oif = oif, |
| 3000 | .flowi6_mark = mark, |
| 3001 | .daddr = iph->daddr, |
| 3002 | .saddr = iph->saddr, |
| 3003 | .flowlabel = ip6_flowinfo(iph), |
| 3004 | .flowi6_uid = uid, |
| 3005 | }; |
David S. Miller | 3a5ad2e | 2012-07-12 00:08:07 -0700 | [diff] [blame] | 3006 | |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 3007 | dst = ip6_route_redirect(net, &fl6, skb, &ipv6_hdr(skb)->saddr); |
Duan Jiong | b55b76b | 2013-09-04 19:44:21 +0800 | [diff] [blame] | 3008 | rt6_do_redirect(dst, NULL, skb); |
David S. Miller | 3a5ad2e | 2012-07-12 00:08:07 -0700 | [diff] [blame] | 3009 | dst_release(dst); |
| 3010 | } |
| 3011 | EXPORT_SYMBOL_GPL(ip6_redirect); |
| 3012 | |
Maciej Żenczykowski | d456336 | 2018-09-29 23:44:50 -0700 | [diff] [blame] | 3013 | 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] | 3014 | { |
| 3015 | const struct ipv6hdr *iph = ipv6_hdr(skb); |
| 3016 | const struct rd_msg *msg = (struct rd_msg *)icmp6_hdr(skb); |
| 3017 | struct dst_entry *dst; |
Maciej Żenczykowski | 0b26fb1 | 2018-09-29 23:44:49 -0700 | [diff] [blame] | 3018 | struct flowi6 fl6 = { |
| 3019 | .flowi6_iif = LOOPBACK_IFINDEX, |
| 3020 | .flowi6_oif = oif, |
Maciej Żenczykowski | 0b26fb1 | 2018-09-29 23:44:49 -0700 | [diff] [blame] | 3021 | .daddr = msg->dest, |
| 3022 | .saddr = iph->daddr, |
| 3023 | .flowi6_uid = sock_net_uid(net, NULL), |
| 3024 | }; |
Duan Jiong | c92a59e | 2013-08-22 12:07:35 +0800 | [diff] [blame] | 3025 | |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 3026 | dst = ip6_route_redirect(net, &fl6, skb, &iph->saddr); |
Duan Jiong | b55b76b | 2013-09-04 19:44:21 +0800 | [diff] [blame] | 3027 | rt6_do_redirect(dst, NULL, skb); |
Duan Jiong | c92a59e | 2013-08-22 12:07:35 +0800 | [diff] [blame] | 3028 | dst_release(dst); |
| 3029 | } |
| 3030 | |
David S. Miller | 3a5ad2e | 2012-07-12 00:08:07 -0700 | [diff] [blame] | 3031 | void ip6_sk_redirect(struct sk_buff *skb, struct sock *sk) |
| 3032 | { |
Lorenzo Colitti | e2d118a | 2016-11-04 02:23:43 +0900 | [diff] [blame] | 3033 | ip6_redirect(skb, sock_net(sk), sk->sk_bound_dev_if, sk->sk_mark, |
| 3034 | sk->sk_uid); |
David S. Miller | 3a5ad2e | 2012-07-12 00:08:07 -0700 | [diff] [blame] | 3035 | } |
| 3036 | EXPORT_SYMBOL_GPL(ip6_sk_redirect); |
| 3037 | |
David S. Miller | 0dbaee3 | 2010-12-13 12:52:14 -0800 | [diff] [blame] | 3038 | static unsigned int ip6_default_advmss(const struct dst_entry *dst) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3039 | { |
David S. Miller | 0dbaee3 | 2010-12-13 12:52:14 -0800 | [diff] [blame] | 3040 | struct net_device *dev = dst->dev; |
| 3041 | unsigned int mtu = dst_mtu(dst); |
| 3042 | struct net *net = dev_net(dev); |
| 3043 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3044 | mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr); |
| 3045 | |
Daniel Lezcano | 5578689 | 2008-03-04 13:47:47 -0800 | [diff] [blame] | 3046 | if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss) |
| 3047 | mtu = net->ipv6.sysctl.ip6_rt_min_advmss; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3048 | |
| 3049 | /* |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 3050 | * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and |
| 3051 | * corresponding MSS is IPV6_MAXPLEN - tcp_header_size. |
| 3052 | * IPV6_MAXPLEN is also valid and means: "any MSS, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3053 | * rely only on pmtu discovery" |
| 3054 | */ |
| 3055 | if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr)) |
| 3056 | mtu = IPV6_MAXPLEN; |
| 3057 | return mtu; |
| 3058 | } |
| 3059 | |
Steffen Klassert | ebb762f | 2011-11-23 02:12:51 +0000 | [diff] [blame] | 3060 | static unsigned int ip6_mtu(const struct dst_entry *dst) |
David S. Miller | d33e455 | 2010-12-14 13:01:14 -0800 | [diff] [blame] | 3061 | { |
David S. Miller | d33e455 | 2010-12-14 13:01:14 -0800 | [diff] [blame] | 3062 | struct inet6_dev *idev; |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 3063 | unsigned int mtu; |
Steffen Klassert | 618f9bc | 2011-11-23 02:13:31 +0000 | [diff] [blame] | 3064 | |
Martin KaFai Lau | 4b32b5a | 2015-04-28 13:03:06 -0700 | [diff] [blame] | 3065 | mtu = dst_metric_raw(dst, RTAX_MTU); |
| 3066 | if (mtu) |
| 3067 | goto out; |
| 3068 | |
Steffen Klassert | 618f9bc | 2011-11-23 02:13:31 +0000 | [diff] [blame] | 3069 | mtu = IPV6_MIN_MTU; |
David S. Miller | d33e455 | 2010-12-14 13:01:14 -0800 | [diff] [blame] | 3070 | |
| 3071 | rcu_read_lock(); |
| 3072 | idev = __in6_dev_get(dst->dev); |
| 3073 | if (idev) |
| 3074 | mtu = idev->cnf.mtu6; |
| 3075 | rcu_read_unlock(); |
| 3076 | |
Eric Dumazet | 30f78d8 | 2014-04-10 21:23:36 -0700 | [diff] [blame] | 3077 | out: |
Roopa Prabhu | 14972cb | 2016-08-24 20:10:43 -0700 | [diff] [blame] | 3078 | mtu = min_t(unsigned int, mtu, IP6_MAX_MTU); |
| 3079 | |
| 3080 | return mtu - lwtunnel_headroom(dst->lwtstate, mtu); |
David S. Miller | d33e455 | 2010-12-14 13:01:14 -0800 | [diff] [blame] | 3081 | } |
| 3082 | |
David Ahern | 901731b | 2018-05-21 09:08:14 -0700 | [diff] [blame] | 3083 | /* MTU selection: |
| 3084 | * 1. mtu on route is locked - use it |
| 3085 | * 2. mtu from nexthop exception |
| 3086 | * 3. mtu from egress device |
| 3087 | * |
| 3088 | * based on ip6_dst_mtu_forward and exception logic of |
| 3089 | * rt6_find_cached_rt; called with rcu_read_lock |
| 3090 | */ |
David Ahern | b748f26 | 2019-04-16 14:36:06 -0700 | [diff] [blame] | 3091 | u32 ip6_mtu_from_fib6(const struct fib6_result *res, |
| 3092 | const struct in6_addr *daddr, |
| 3093 | const struct in6_addr *saddr) |
David Ahern | 901731b | 2018-05-21 09:08:14 -0700 | [diff] [blame] | 3094 | { |
David Ahern | b748f26 | 2019-04-16 14:36:06 -0700 | [diff] [blame] | 3095 | const struct fib6_nh *nh = res->nh; |
| 3096 | struct fib6_info *f6i = res->f6i; |
David Ahern | 901731b | 2018-05-21 09:08:14 -0700 | [diff] [blame] | 3097 | struct inet6_dev *idev; |
Wei Wang | 510e2ce | 2019-05-16 13:30:54 -0700 | [diff] [blame] | 3098 | struct rt6_info *rt; |
David Ahern | 901731b | 2018-05-21 09:08:14 -0700 | [diff] [blame] | 3099 | u32 mtu = 0; |
| 3100 | |
| 3101 | if (unlikely(fib6_metric_locked(f6i, RTAX_MTU))) { |
| 3102 | mtu = f6i->fib6_pmtu; |
| 3103 | if (mtu) |
| 3104 | goto out; |
| 3105 | } |
| 3106 | |
Wei Wang | 510e2ce | 2019-05-16 13:30:54 -0700 | [diff] [blame] | 3107 | rt = rt6_find_cached_rt(res, daddr, saddr); |
| 3108 | if (unlikely(rt)) { |
| 3109 | mtu = dst_metric_raw(&rt->dst, RTAX_MTU); |
| 3110 | } else { |
David Ahern | b748f26 | 2019-04-16 14:36:06 -0700 | [diff] [blame] | 3111 | struct net_device *dev = nh->fib_nh_dev; |
David Ahern | 901731b | 2018-05-21 09:08:14 -0700 | [diff] [blame] | 3112 | |
| 3113 | mtu = IPV6_MIN_MTU; |
| 3114 | idev = __in6_dev_get(dev); |
| 3115 | if (idev && idev->cnf.mtu6 > mtu) |
| 3116 | mtu = idev->cnf.mtu6; |
| 3117 | } |
| 3118 | |
| 3119 | mtu = min_t(unsigned int, mtu, IP6_MAX_MTU); |
| 3120 | out: |
David Ahern | b748f26 | 2019-04-16 14:36:06 -0700 | [diff] [blame] | 3121 | return mtu - lwtunnel_headroom(nh->fib_nh_lws, mtu); |
David Ahern | 901731b | 2018-05-21 09:08:14 -0700 | [diff] [blame] | 3122 | } |
| 3123 | |
YOSHIFUJI Hideaki | 3b00944 | 2007-12-06 16:11:48 -0800 | [diff] [blame] | 3124 | struct dst_entry *icmp6_dst_alloc(struct net_device *dev, |
David S. Miller | 87a1157 | 2011-12-06 17:04:13 -0500 | [diff] [blame] | 3125 | struct flowi6 *fl6) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3126 | { |
David S. Miller | 87a1157 | 2011-12-06 17:04:13 -0500 | [diff] [blame] | 3127 | struct dst_entry *dst; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3128 | struct rt6_info *rt; |
| 3129 | struct inet6_dev *idev = in6_dev_get(dev); |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 3130 | struct net *net = dev_net(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3131 | |
David S. Miller | 3830847 | 2011-12-03 18:02:47 -0500 | [diff] [blame] | 3132 | if (unlikely(!idev)) |
Eric Dumazet | 122bdf6 | 2012-03-14 21:13:11 +0000 | [diff] [blame] | 3133 | return ERR_PTR(-ENODEV); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3134 | |
Martin KaFai Lau | ad70686 | 2015-08-14 11:05:52 -0700 | [diff] [blame] | 3135 | rt = ip6_dst_alloc(net, dev, 0); |
David S. Miller | 3830847 | 2011-12-03 18:02:47 -0500 | [diff] [blame] | 3136 | if (unlikely(!rt)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3137 | in6_dev_put(idev); |
David S. Miller | 87a1157 | 2011-12-06 17:04:13 -0500 | [diff] [blame] | 3138 | dst = ERR_PTR(-ENOMEM); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3139 | goto out; |
| 3140 | } |
| 3141 | |
Brendan McGrath | 588753f | 2017-12-13 22:14:57 +1100 | [diff] [blame] | 3142 | rt->dst.input = ip6_input; |
Yan, Zheng | 8e2ec63 | 2011-09-05 21:34:30 +0000 | [diff] [blame] | 3143 | rt->dst.output = ip6_output; |
Julian Anastasov | 550bab4 | 2013-10-20 15:43:04 +0300 | [diff] [blame] | 3144 | rt->rt6i_gateway = fl6->daddr; |
David S. Miller | 87a1157 | 2011-12-06 17:04:13 -0500 | [diff] [blame] | 3145 | rt->rt6i_dst.addr = fl6->daddr; |
Yan, Zheng | 8e2ec63 | 2011-09-05 21:34:30 +0000 | [diff] [blame] | 3146 | rt->rt6i_dst.plen = 128; |
| 3147 | rt->rt6i_idev = idev; |
Li RongQing | 14edd87 | 2012-10-24 14:01:18 +0800 | [diff] [blame] | 3148 | dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3149 | |
Ido Schimmel | 4c981e2 | 2018-01-07 12:45:04 +0200 | [diff] [blame] | 3150 | /* Add this dst into uncached_list so that rt6_disable_ip() can |
Wei Wang | 587fea7 | 2017-06-17 10:42:36 -0700 | [diff] [blame] | 3151 | * do proper release of the net_device |
| 3152 | */ |
| 3153 | rt6_uncached_list_add(rt); |
Wei Wang | 81eb844 | 2017-10-06 12:06:11 -0700 | [diff] [blame] | 3154 | atomic_inc(&net->ipv6.rt6_stats->fib_rt_uncache); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3155 | |
David S. Miller | 87a1157 | 2011-12-06 17:04:13 -0500 | [diff] [blame] | 3156 | dst = xfrm_lookup(net, &rt->dst, flowi6_to_flowi(fl6), NULL, 0); |
| 3157 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3158 | out: |
David S. Miller | 87a1157 | 2011-12-06 17:04:13 -0500 | [diff] [blame] | 3159 | return dst; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3160 | } |
| 3161 | |
Daniel Lezcano | 569d364 | 2008-01-18 03:56:57 -0800 | [diff] [blame] | 3162 | static int ip6_dst_gc(struct dst_ops *ops) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3163 | { |
Alexey Dobriyan | 86393e5 | 2009-08-29 01:34:49 +0000 | [diff] [blame] | 3164 | struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops); |
Daniel Lezcano | 7019b78 | 2008-03-04 13:50:14 -0800 | [diff] [blame] | 3165 | int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval; |
| 3166 | int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size; |
| 3167 | int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity; |
| 3168 | int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout; |
| 3169 | unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc; |
Eric Dumazet | fc66f95 | 2010-10-08 06:37:34 +0000 | [diff] [blame] | 3170 | int entries; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3171 | |
Eric Dumazet | fc66f95 | 2010-10-08 06:37:34 +0000 | [diff] [blame] | 3172 | entries = dst_entries_get_fast(ops); |
Michal Kubeček | 49a18d8 | 2013-08-01 10:04:24 +0200 | [diff] [blame] | 3173 | if (time_after(rt_last_gc + rt_min_interval, jiffies) && |
Eric Dumazet | fc66f95 | 2010-10-08 06:37:34 +0000 | [diff] [blame] | 3174 | entries <= rt_max_size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3175 | goto out; |
| 3176 | |
Benjamin Thery | 6891a34 | 2008-03-04 13:49:47 -0800 | [diff] [blame] | 3177 | net->ipv6.ip6_rt_gc_expire++; |
Li RongQing | 1495664 | 2014-05-19 17:30:28 +0800 | [diff] [blame] | 3178 | fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net, true); |
Eric Dumazet | fc66f95 | 2010-10-08 06:37:34 +0000 | [diff] [blame] | 3179 | entries = dst_entries_get_slow(ops); |
| 3180 | if (entries < ops->gc_thresh) |
Daniel Lezcano | 7019b78 | 2008-03-04 13:50:14 -0800 | [diff] [blame] | 3181 | net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3182 | out: |
Daniel Lezcano | 7019b78 | 2008-03-04 13:50:14 -0800 | [diff] [blame] | 3183 | 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] | 3184 | return entries > rt_max_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3185 | } |
| 3186 | |
David Ahern | b2c709c | 2019-06-24 13:44:51 -0700 | [diff] [blame] | 3187 | static int ip6_nh_lookup_table(struct net *net, struct fib6_config *cfg, |
| 3188 | const struct in6_addr *gw_addr, u32 tbid, |
| 3189 | int flags, struct fib6_result *res) |
David Ahern | 8c14586 | 2016-04-24 21:26:04 -0700 | [diff] [blame] | 3190 | { |
| 3191 | struct flowi6 fl6 = { |
| 3192 | .flowi6_oif = cfg->fc_ifindex, |
| 3193 | .daddr = *gw_addr, |
| 3194 | .saddr = cfg->fc_prefsrc, |
| 3195 | }; |
| 3196 | struct fib6_table *table; |
David Ahern | b2c709c | 2019-06-24 13:44:51 -0700 | [diff] [blame] | 3197 | int err; |
David Ahern | 8c14586 | 2016-04-24 21:26:04 -0700 | [diff] [blame] | 3198 | |
David Ahern | f4797b3 | 2018-01-25 16:55:08 -0800 | [diff] [blame] | 3199 | table = fib6_get_table(net, tbid); |
David Ahern | 8c14586 | 2016-04-24 21:26:04 -0700 | [diff] [blame] | 3200 | if (!table) |
David Ahern | b2c709c | 2019-06-24 13:44:51 -0700 | [diff] [blame] | 3201 | return -EINVAL; |
David Ahern | 8c14586 | 2016-04-24 21:26:04 -0700 | [diff] [blame] | 3202 | |
| 3203 | if (!ipv6_addr_any(&cfg->fc_prefsrc)) |
| 3204 | flags |= RT6_LOOKUP_F_HAS_SADDR; |
| 3205 | |
David Ahern | f4797b3 | 2018-01-25 16:55:08 -0800 | [diff] [blame] | 3206 | flags |= RT6_LOOKUP_F_IGNORE_LINKSTATE; |
David Ahern | 8c14586 | 2016-04-24 21:26:04 -0700 | [diff] [blame] | 3207 | |
David Ahern | b2c709c | 2019-06-24 13:44:51 -0700 | [diff] [blame] | 3208 | err = fib6_table_lookup(net, table, cfg->fc_ifindex, &fl6, res, flags); |
| 3209 | if (!err && res->f6i != net->ipv6.fib6_null_entry) |
| 3210 | fib6_select_path(net, res, &fl6, cfg->fc_ifindex, |
| 3211 | cfg->fc_ifindex != 0, NULL, flags); |
David Ahern | 8c14586 | 2016-04-24 21:26:04 -0700 | [diff] [blame] | 3212 | |
David Ahern | b2c709c | 2019-06-24 13:44:51 -0700 | [diff] [blame] | 3213 | return err; |
David Ahern | 8c14586 | 2016-04-24 21:26:04 -0700 | [diff] [blame] | 3214 | } |
| 3215 | |
David Ahern | fc1e64e | 2018-01-25 16:55:09 -0800 | [diff] [blame] | 3216 | static int ip6_route_check_nh_onlink(struct net *net, |
| 3217 | struct fib6_config *cfg, |
David Ahern | 9fbb704 | 2018-03-13 08:29:36 -0700 | [diff] [blame] | 3218 | const struct net_device *dev, |
David Ahern | fc1e64e | 2018-01-25 16:55:09 -0800 | [diff] [blame] | 3219 | struct netlink_ext_ack *extack) |
| 3220 | { |
David Ahern | b2c709c | 2019-06-24 13:44:51 -0700 | [diff] [blame] | 3221 | u32 tbid = l3mdev_fib_table_rcu(dev) ? : RT_TABLE_MAIN; |
David Ahern | fc1e64e | 2018-01-25 16:55:09 -0800 | [diff] [blame] | 3222 | const struct in6_addr *gw_addr = &cfg->fc_gateway; |
David Ahern | b2c709c | 2019-06-24 13:44:51 -0700 | [diff] [blame] | 3223 | struct fib6_result res = {}; |
David Ahern | fc1e64e | 2018-01-25 16:55:09 -0800 | [diff] [blame] | 3224 | int err; |
| 3225 | |
David Ahern | b2c709c | 2019-06-24 13:44:51 -0700 | [diff] [blame] | 3226 | err = ip6_nh_lookup_table(net, cfg, gw_addr, tbid, 0, &res); |
| 3227 | if (!err && !(res.fib6_flags & RTF_REJECT) && |
| 3228 | /* ignore match if it is the default route */ |
| 3229 | !ipv6_addr_any(&res.f6i->fib6_dst.addr) && |
| 3230 | (res.fib6_type != RTN_UNICAST || dev != res.nh->fib_nh_dev)) { |
| 3231 | NL_SET_ERR_MSG(extack, |
| 3232 | "Nexthop has invalid gateway or device mismatch"); |
| 3233 | err = -EINVAL; |
David Ahern | fc1e64e | 2018-01-25 16:55:09 -0800 | [diff] [blame] | 3234 | } |
| 3235 | |
| 3236 | return err; |
| 3237 | } |
| 3238 | |
David Ahern | 1edce99 | 2018-01-25 16:55:07 -0800 | [diff] [blame] | 3239 | static int ip6_route_check_nh(struct net *net, |
| 3240 | struct fib6_config *cfg, |
| 3241 | struct net_device **_dev, |
| 3242 | struct inet6_dev **idev) |
| 3243 | { |
| 3244 | const struct in6_addr *gw_addr = &cfg->fc_gateway; |
| 3245 | struct net_device *dev = _dev ? *_dev : NULL; |
David Ahern | b2c709c | 2019-06-24 13:44:51 -0700 | [diff] [blame] | 3246 | int flags = RT6_LOOKUP_F_IFACE; |
| 3247 | struct fib6_result res = {}; |
David Ahern | 1edce99 | 2018-01-25 16:55:07 -0800 | [diff] [blame] | 3248 | int err = -EHOSTUNREACH; |
| 3249 | |
| 3250 | if (cfg->fc_table) { |
David Ahern | b2c709c | 2019-06-24 13:44:51 -0700 | [diff] [blame] | 3251 | err = ip6_nh_lookup_table(net, cfg, gw_addr, |
| 3252 | cfg->fc_table, flags, &res); |
| 3253 | /* gw_addr can not require a gateway or resolve to a reject |
| 3254 | * route. If a device is given, it must match the result. |
| 3255 | */ |
| 3256 | if (err || res.fib6_flags & RTF_REJECT || |
| 3257 | res.nh->fib_nh_gw_family || |
| 3258 | (dev && dev != res.nh->fib_nh_dev)) |
| 3259 | err = -EHOSTUNREACH; |
David Ahern | 1edce99 | 2018-01-25 16:55:07 -0800 | [diff] [blame] | 3260 | } |
| 3261 | |
David Ahern | b2c709c | 2019-06-24 13:44:51 -0700 | [diff] [blame] | 3262 | if (err < 0) { |
| 3263 | struct flowi6 fl6 = { |
| 3264 | .flowi6_oif = cfg->fc_ifindex, |
| 3265 | .daddr = *gw_addr, |
| 3266 | }; |
David Ahern | 1edce99 | 2018-01-25 16:55:07 -0800 | [diff] [blame] | 3267 | |
David Ahern | b2c709c | 2019-06-24 13:44:51 -0700 | [diff] [blame] | 3268 | err = fib6_lookup(net, cfg->fc_ifindex, &fl6, &res, flags); |
| 3269 | if (err || res.fib6_flags & RTF_REJECT || |
| 3270 | res.nh->fib_nh_gw_family) |
| 3271 | err = -EHOSTUNREACH; |
David Ahern | 1edce99 | 2018-01-25 16:55:07 -0800 | [diff] [blame] | 3272 | |
David Ahern | b2c709c | 2019-06-24 13:44:51 -0700 | [diff] [blame] | 3273 | if (err) |
| 3274 | return err; |
| 3275 | |
| 3276 | fib6_select_path(net, &res, &fl6, cfg->fc_ifindex, |
| 3277 | cfg->fc_ifindex != 0, NULL, flags); |
| 3278 | } |
| 3279 | |
| 3280 | err = 0; |
David Ahern | 1edce99 | 2018-01-25 16:55:07 -0800 | [diff] [blame] | 3281 | if (dev) { |
David Ahern | b2c709c | 2019-06-24 13:44:51 -0700 | [diff] [blame] | 3282 | if (dev != res.nh->fib_nh_dev) |
| 3283 | err = -EHOSTUNREACH; |
David Ahern | 1edce99 | 2018-01-25 16:55:07 -0800 | [diff] [blame] | 3284 | } else { |
David Ahern | b2c709c | 2019-06-24 13:44:51 -0700 | [diff] [blame] | 3285 | *_dev = dev = res.nh->fib_nh_dev; |
David Ahern | 1edce99 | 2018-01-25 16:55:07 -0800 | [diff] [blame] | 3286 | dev_hold(dev); |
David Ahern | b2c709c | 2019-06-24 13:44:51 -0700 | [diff] [blame] | 3287 | *idev = in6_dev_get(dev); |
David Ahern | 1edce99 | 2018-01-25 16:55:07 -0800 | [diff] [blame] | 3288 | } |
| 3289 | |
David Ahern | 1edce99 | 2018-01-25 16:55:07 -0800 | [diff] [blame] | 3290 | return err; |
| 3291 | } |
| 3292 | |
David Ahern | 9fbb704 | 2018-03-13 08:29:36 -0700 | [diff] [blame] | 3293 | static int ip6_validate_gw(struct net *net, struct fib6_config *cfg, |
| 3294 | struct net_device **_dev, struct inet6_dev **idev, |
| 3295 | struct netlink_ext_ack *extack) |
| 3296 | { |
| 3297 | const struct in6_addr *gw_addr = &cfg->fc_gateway; |
| 3298 | int gwa_type = ipv6_addr_type(gw_addr); |
David Ahern | 232378e | 2018-03-13 08:29:37 -0700 | [diff] [blame] | 3299 | bool skip_dev = gwa_type & IPV6_ADDR_LINKLOCAL ? false : true; |
David Ahern | 9fbb704 | 2018-03-13 08:29:36 -0700 | [diff] [blame] | 3300 | const struct net_device *dev = *_dev; |
David Ahern | 232378e | 2018-03-13 08:29:37 -0700 | [diff] [blame] | 3301 | bool need_addr_check = !dev; |
David Ahern | 9fbb704 | 2018-03-13 08:29:36 -0700 | [diff] [blame] | 3302 | int err = -EINVAL; |
| 3303 | |
| 3304 | /* if gw_addr is local we will fail to detect this in case |
| 3305 | * address is still TENTATIVE (DAD in progress). rt6_lookup() |
| 3306 | * will return already-added prefix route via interface that |
| 3307 | * prefix route was assigned to, which might be non-loopback. |
| 3308 | */ |
David Ahern | 232378e | 2018-03-13 08:29:37 -0700 | [diff] [blame] | 3309 | if (dev && |
| 3310 | ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) { |
| 3311 | NL_SET_ERR_MSG(extack, "Gateway can not be a local address"); |
David Ahern | 9fbb704 | 2018-03-13 08:29:36 -0700 | [diff] [blame] | 3312 | goto out; |
| 3313 | } |
| 3314 | |
| 3315 | if (gwa_type != (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_UNICAST)) { |
| 3316 | /* IPv6 strictly inhibits using not link-local |
| 3317 | * addresses as nexthop address. |
| 3318 | * Otherwise, router will not able to send redirects. |
| 3319 | * It is very good, but in some (rare!) circumstances |
| 3320 | * (SIT, PtP, NBMA NOARP links) it is handy to allow |
| 3321 | * some exceptions. --ANK |
| 3322 | * We allow IPv4-mapped nexthops to support RFC4798-type |
| 3323 | * addressing |
| 3324 | */ |
| 3325 | if (!(gwa_type & (IPV6_ADDR_UNICAST | IPV6_ADDR_MAPPED))) { |
| 3326 | NL_SET_ERR_MSG(extack, "Invalid gateway address"); |
| 3327 | goto out; |
| 3328 | } |
| 3329 | |
David Ahern | b2c709c | 2019-06-24 13:44:51 -0700 | [diff] [blame] | 3330 | rcu_read_lock(); |
| 3331 | |
David Ahern | 9fbb704 | 2018-03-13 08:29:36 -0700 | [diff] [blame] | 3332 | if (cfg->fc_flags & RTNH_F_ONLINK) |
| 3333 | err = ip6_route_check_nh_onlink(net, cfg, dev, extack); |
| 3334 | else |
| 3335 | err = ip6_route_check_nh(net, cfg, _dev, idev); |
| 3336 | |
David Ahern | b2c709c | 2019-06-24 13:44:51 -0700 | [diff] [blame] | 3337 | rcu_read_unlock(); |
| 3338 | |
David Ahern | 9fbb704 | 2018-03-13 08:29:36 -0700 | [diff] [blame] | 3339 | if (err) |
| 3340 | goto out; |
| 3341 | } |
| 3342 | |
| 3343 | /* reload in case device was changed */ |
| 3344 | dev = *_dev; |
| 3345 | |
| 3346 | err = -EINVAL; |
| 3347 | if (!dev) { |
| 3348 | NL_SET_ERR_MSG(extack, "Egress device not specified"); |
| 3349 | goto out; |
| 3350 | } else if (dev->flags & IFF_LOOPBACK) { |
| 3351 | NL_SET_ERR_MSG(extack, |
| 3352 | "Egress device can not be loopback device for this route"); |
| 3353 | goto out; |
| 3354 | } |
David Ahern | 232378e | 2018-03-13 08:29:37 -0700 | [diff] [blame] | 3355 | |
| 3356 | /* if we did not check gw_addr above, do so now that the |
| 3357 | * egress device has been resolved. |
| 3358 | */ |
| 3359 | if (need_addr_check && |
| 3360 | ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) { |
| 3361 | NL_SET_ERR_MSG(extack, "Gateway can not be a local address"); |
| 3362 | goto out; |
| 3363 | } |
| 3364 | |
David Ahern | 9fbb704 | 2018-03-13 08:29:36 -0700 | [diff] [blame] | 3365 | err = 0; |
| 3366 | out: |
| 3367 | return err; |
| 3368 | } |
| 3369 | |
David Ahern | 83c44251 | 2019-03-27 20:53:50 -0700 | [diff] [blame] | 3370 | static bool fib6_is_reject(u32 flags, struct net_device *dev, int addr_type) |
| 3371 | { |
| 3372 | if ((flags & RTF_REJECT) || |
| 3373 | (dev && (dev->flags & IFF_LOOPBACK) && |
| 3374 | !(addr_type & IPV6_ADDR_LOOPBACK) && |
| 3375 | !(flags & RTF_LOCAL))) |
| 3376 | return true; |
| 3377 | |
| 3378 | return false; |
| 3379 | } |
| 3380 | |
| 3381 | int fib6_nh_init(struct net *net, struct fib6_nh *fib6_nh, |
| 3382 | struct fib6_config *cfg, gfp_t gfp_flags, |
| 3383 | struct netlink_ext_ack *extack) |
| 3384 | { |
| 3385 | struct net_device *dev = NULL; |
| 3386 | struct inet6_dev *idev = NULL; |
| 3387 | int addr_type; |
| 3388 | int err; |
| 3389 | |
David Ahern | f174173 | 2019-03-27 20:53:57 -0700 | [diff] [blame] | 3390 | fib6_nh->fib_nh_family = AF_INET6; |
Eric Dumazet | 1bef4c2 | 2019-11-07 09:26:19 -0800 | [diff] [blame] | 3391 | #ifdef CONFIG_IPV6_ROUTER_PREF |
| 3392 | fib6_nh->last_probe = jiffies; |
| 3393 | #endif |
David Ahern | f174173 | 2019-03-27 20:53:57 -0700 | [diff] [blame] | 3394 | |
David Ahern | 83c44251 | 2019-03-27 20:53:50 -0700 | [diff] [blame] | 3395 | err = -ENODEV; |
| 3396 | if (cfg->fc_ifindex) { |
| 3397 | dev = dev_get_by_index(net, cfg->fc_ifindex); |
| 3398 | if (!dev) |
| 3399 | goto out; |
| 3400 | idev = in6_dev_get(dev); |
| 3401 | if (!idev) |
| 3402 | goto out; |
| 3403 | } |
| 3404 | |
| 3405 | if (cfg->fc_flags & RTNH_F_ONLINK) { |
| 3406 | if (!dev) { |
| 3407 | NL_SET_ERR_MSG(extack, |
| 3408 | "Nexthop device required for onlink"); |
| 3409 | goto out; |
| 3410 | } |
| 3411 | |
| 3412 | if (!(dev->flags & IFF_UP)) { |
| 3413 | NL_SET_ERR_MSG(extack, "Nexthop device is not up"); |
| 3414 | err = -ENETDOWN; |
| 3415 | goto out; |
| 3416 | } |
| 3417 | |
David Ahern | ad1601a | 2019-03-27 20:53:56 -0700 | [diff] [blame] | 3418 | fib6_nh->fib_nh_flags |= RTNH_F_ONLINK; |
David Ahern | 83c44251 | 2019-03-27 20:53:50 -0700 | [diff] [blame] | 3419 | } |
| 3420 | |
David Ahern | ad1601a | 2019-03-27 20:53:56 -0700 | [diff] [blame] | 3421 | fib6_nh->fib_nh_weight = 1; |
David Ahern | 83c44251 | 2019-03-27 20:53:50 -0700 | [diff] [blame] | 3422 | |
| 3423 | /* We cannot add true routes via loopback here, |
| 3424 | * they would result in kernel looping; promote them to reject routes |
| 3425 | */ |
| 3426 | addr_type = ipv6_addr_type(&cfg->fc_dst); |
| 3427 | if (fib6_is_reject(cfg->fc_flags, dev, addr_type)) { |
| 3428 | /* hold loopback dev/idev if we haven't done so. */ |
| 3429 | if (dev != net->loopback_dev) { |
| 3430 | if (dev) { |
| 3431 | dev_put(dev); |
| 3432 | in6_dev_put(idev); |
| 3433 | } |
| 3434 | dev = net->loopback_dev; |
| 3435 | dev_hold(dev); |
| 3436 | idev = in6_dev_get(dev); |
| 3437 | if (!idev) { |
| 3438 | err = -ENODEV; |
| 3439 | goto out; |
| 3440 | } |
| 3441 | } |
David Ahern | 7dd7316 | 2019-06-03 18:37:03 -0700 | [diff] [blame] | 3442 | goto pcpu_alloc; |
David Ahern | 83c44251 | 2019-03-27 20:53:50 -0700 | [diff] [blame] | 3443 | } |
| 3444 | |
| 3445 | if (cfg->fc_flags & RTF_GATEWAY) { |
| 3446 | err = ip6_validate_gw(net, cfg, &dev, &idev, extack); |
| 3447 | if (err) |
| 3448 | goto out; |
| 3449 | |
David Ahern | ad1601a | 2019-03-27 20:53:56 -0700 | [diff] [blame] | 3450 | fib6_nh->fib_nh_gw6 = cfg->fc_gateway; |
David Ahern | bdf0046 | 2019-04-05 16:30:26 -0700 | [diff] [blame] | 3451 | fib6_nh->fib_nh_gw_family = AF_INET6; |
David Ahern | 83c44251 | 2019-03-27 20:53:50 -0700 | [diff] [blame] | 3452 | } |
| 3453 | |
| 3454 | err = -ENODEV; |
| 3455 | if (!dev) |
| 3456 | goto out; |
| 3457 | |
| 3458 | if (idev->cnf.disable_ipv6) { |
| 3459 | NL_SET_ERR_MSG(extack, "IPv6 is disabled on nexthop device"); |
| 3460 | err = -EACCES; |
| 3461 | goto out; |
| 3462 | } |
| 3463 | |
| 3464 | if (!(dev->flags & IFF_UP) && !cfg->fc_ignore_dev_down) { |
| 3465 | NL_SET_ERR_MSG(extack, "Nexthop device is not up"); |
| 3466 | err = -ENETDOWN; |
| 3467 | goto out; |
| 3468 | } |
| 3469 | |
| 3470 | if (!(cfg->fc_flags & (RTF_LOCAL | RTF_ANYCAST)) && |
| 3471 | !netif_carrier_ok(dev)) |
David Ahern | ad1601a | 2019-03-27 20:53:56 -0700 | [diff] [blame] | 3472 | fib6_nh->fib_nh_flags |= RTNH_F_LINKDOWN; |
David Ahern | 83c44251 | 2019-03-27 20:53:50 -0700 | [diff] [blame] | 3473 | |
Alexander Aring | faee676 | 2020-03-27 18:00:21 -0400 | [diff] [blame] | 3474 | 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] | 3475 | cfg->fc_encap_type, cfg, gfp_flags, extack); |
| 3476 | if (err) |
| 3477 | goto out; |
| 3478 | |
| 3479 | pcpu_alloc: |
David Ahern | f40b6ae | 2019-05-22 20:27:55 -0700 | [diff] [blame] | 3480 | fib6_nh->rt6i_pcpu = alloc_percpu_gfp(struct rt6_info *, gfp_flags); |
| 3481 | if (!fib6_nh->rt6i_pcpu) { |
| 3482 | err = -ENOMEM; |
| 3483 | goto out; |
| 3484 | } |
| 3485 | |
David Ahern | ad1601a | 2019-03-27 20:53:56 -0700 | [diff] [blame] | 3486 | fib6_nh->fib_nh_dev = dev; |
David Ahern | f174173 | 2019-03-27 20:53:57 -0700 | [diff] [blame] | 3487 | fib6_nh->fib_nh_oif = dev->ifindex; |
David Ahern | 83c44251 | 2019-03-27 20:53:50 -0700 | [diff] [blame] | 3488 | err = 0; |
| 3489 | out: |
| 3490 | if (idev) |
| 3491 | in6_dev_put(idev); |
| 3492 | |
| 3493 | if (err) { |
David Ahern | ad1601a | 2019-03-27 20:53:56 -0700 | [diff] [blame] | 3494 | lwtstate_put(fib6_nh->fib_nh_lws); |
| 3495 | fib6_nh->fib_nh_lws = NULL; |
David Ahern | 83c44251 | 2019-03-27 20:53:50 -0700 | [diff] [blame] | 3496 | if (dev) |
| 3497 | dev_put(dev); |
| 3498 | } |
| 3499 | |
| 3500 | return err; |
| 3501 | } |
| 3502 | |
David Ahern | dac7d0f | 2019-03-27 20:53:51 -0700 | [diff] [blame] | 3503 | void fib6_nh_release(struct fib6_nh *fib6_nh) |
| 3504 | { |
David Ahern | cc5c073 | 2019-05-22 20:27:58 -0700 | [diff] [blame] | 3505 | struct rt6_exception_bucket *bucket; |
| 3506 | |
| 3507 | rcu_read_lock(); |
| 3508 | |
| 3509 | fib6_nh_flush_exceptions(fib6_nh, NULL); |
| 3510 | bucket = fib6_nh_get_excptn_bucket(fib6_nh, NULL); |
| 3511 | if (bucket) { |
| 3512 | rcu_assign_pointer(fib6_nh->rt6i_exception_bucket, NULL); |
| 3513 | kfree(bucket); |
| 3514 | } |
| 3515 | |
| 3516 | rcu_read_unlock(); |
| 3517 | |
David Ahern | f40b6ae | 2019-05-22 20:27:55 -0700 | [diff] [blame] | 3518 | if (fib6_nh->rt6i_pcpu) { |
| 3519 | int cpu; |
| 3520 | |
| 3521 | for_each_possible_cpu(cpu) { |
| 3522 | struct rt6_info **ppcpu_rt; |
| 3523 | struct rt6_info *pcpu_rt; |
| 3524 | |
| 3525 | ppcpu_rt = per_cpu_ptr(fib6_nh->rt6i_pcpu, cpu); |
| 3526 | pcpu_rt = *ppcpu_rt; |
| 3527 | if (pcpu_rt) { |
| 3528 | dst_dev_put(&pcpu_rt->dst); |
| 3529 | dst_release(&pcpu_rt->dst); |
| 3530 | *ppcpu_rt = NULL; |
| 3531 | } |
| 3532 | } |
| 3533 | |
| 3534 | free_percpu(fib6_nh->rt6i_pcpu); |
| 3535 | } |
| 3536 | |
David Ahern | 979e276 | 2019-03-27 20:53:58 -0700 | [diff] [blame] | 3537 | fib_nh_common_release(&fib6_nh->nh_common); |
David Ahern | dac7d0f | 2019-03-27 20:53:51 -0700 | [diff] [blame] | 3538 | } |
| 3539 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 3540 | static struct fib6_info *ip6_route_info_create(struct fib6_config *cfg, |
David Ahern | acb54e3 | 2018-04-17 17:33:22 -0700 | [diff] [blame] | 3541 | gfp_t gfp_flags, |
David Ahern | 333c430 | 2017-05-21 10:12:04 -0600 | [diff] [blame] | 3542 | struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3543 | { |
Daniel Lezcano | 5578689 | 2008-03-04 13:47:47 -0800 | [diff] [blame] | 3544 | struct net *net = cfg->fc_nlinfo.nl_net; |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 3545 | struct fib6_info *rt = NULL; |
David Ahern | f88d8ea | 2019-06-03 20:19:52 -0700 | [diff] [blame] | 3546 | struct nexthop *nh = NULL; |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 3547 | struct fib6_table *table; |
David Ahern | f88d8ea | 2019-06-03 20:19:52 -0700 | [diff] [blame] | 3548 | struct fib6_nh *fib6_nh; |
Roopa Prabhu | 8c5b83f | 2015-10-10 08:26:36 -0700 | [diff] [blame] | 3549 | int err = -EINVAL; |
David Ahern | 83c44251 | 2019-03-27 20:53:50 -0700 | [diff] [blame] | 3550 | int addr_type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3551 | |
David Ahern | 557c44b | 2017-04-19 14:19:43 -0700 | [diff] [blame] | 3552 | /* RTF_PCPU is an internal flag; can not be set by userspace */ |
David Ahern | d5d531c | 2017-05-21 10:12:05 -0600 | [diff] [blame] | 3553 | if (cfg->fc_flags & RTF_PCPU) { |
| 3554 | NL_SET_ERR_MSG(extack, "Userspace can not set RTF_PCPU"); |
David Ahern | 557c44b | 2017-04-19 14:19:43 -0700 | [diff] [blame] | 3555 | goto out; |
David Ahern | d5d531c | 2017-05-21 10:12:05 -0600 | [diff] [blame] | 3556 | } |
David Ahern | 557c44b | 2017-04-19 14:19:43 -0700 | [diff] [blame] | 3557 | |
Wei Wang | 2ea2352 | 2017-10-27 17:30:12 -0700 | [diff] [blame] | 3558 | /* RTF_CACHE is an internal flag; can not be set by userspace */ |
| 3559 | if (cfg->fc_flags & RTF_CACHE) { |
| 3560 | NL_SET_ERR_MSG(extack, "Userspace can not set RTF_CACHE"); |
| 3561 | goto out; |
| 3562 | } |
| 3563 | |
David Ahern | e8478e8 | 2018-04-17 17:33:13 -0700 | [diff] [blame] | 3564 | if (cfg->fc_type > RTN_MAX) { |
| 3565 | NL_SET_ERR_MSG(extack, "Invalid route type"); |
| 3566 | goto out; |
| 3567 | } |
| 3568 | |
David Ahern | d5d531c | 2017-05-21 10:12:05 -0600 | [diff] [blame] | 3569 | if (cfg->fc_dst_len > 128) { |
| 3570 | NL_SET_ERR_MSG(extack, "Invalid prefix length"); |
Roopa Prabhu | 8c5b83f | 2015-10-10 08:26:36 -0700 | [diff] [blame] | 3571 | goto out; |
David Ahern | d5d531c | 2017-05-21 10:12:05 -0600 | [diff] [blame] | 3572 | } |
| 3573 | if (cfg->fc_src_len > 128) { |
| 3574 | NL_SET_ERR_MSG(extack, "Invalid source address length"); |
| 3575 | goto out; |
| 3576 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3577 | #ifndef CONFIG_IPV6_SUBTREES |
David Ahern | d5d531c | 2017-05-21 10:12:05 -0600 | [diff] [blame] | 3578 | if (cfg->fc_src_len) { |
| 3579 | NL_SET_ERR_MSG(extack, |
| 3580 | "Specifying source address requires IPV6_SUBTREES to be enabled"); |
Roopa Prabhu | 8c5b83f | 2015-10-10 08:26:36 -0700 | [diff] [blame] | 3581 | goto out; |
David Ahern | d5d531c | 2017-05-21 10:12:05 -0600 | [diff] [blame] | 3582 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3583 | #endif |
David Ahern | 5b98324 | 2019-06-08 14:53:34 -0700 | [diff] [blame] | 3584 | if (cfg->fc_nh_id) { |
| 3585 | nh = nexthop_find_by_id(net, cfg->fc_nh_id); |
| 3586 | if (!nh) { |
| 3587 | NL_SET_ERR_MSG(extack, "Nexthop id does not exist"); |
| 3588 | goto out; |
| 3589 | } |
| 3590 | err = fib6_check_nexthop(nh, cfg, extack); |
| 3591 | if (err) |
| 3592 | goto out; |
| 3593 | } |
David Ahern | fc1e64e | 2018-01-25 16:55:09 -0800 | [diff] [blame] | 3594 | |
Matti Vaittinen | d71314b | 2011-11-14 00:14:49 +0000 | [diff] [blame] | 3595 | err = -ENOBUFS; |
David S. Miller | 3830847 | 2011-12-03 18:02:47 -0500 | [diff] [blame] | 3596 | if (cfg->fc_nlinfo.nlh && |
| 3597 | !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) { |
Matti Vaittinen | d71314b | 2011-11-14 00:14:49 +0000 | [diff] [blame] | 3598 | table = fib6_get_table(net, cfg->fc_table); |
David S. Miller | 3830847 | 2011-12-03 18:02:47 -0500 | [diff] [blame] | 3599 | if (!table) { |
Joe Perches | f321383 | 2012-05-15 14:11:53 +0000 | [diff] [blame] | 3600 | 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] | 3601 | table = fib6_new_table(net, cfg->fc_table); |
| 3602 | } |
| 3603 | } else { |
| 3604 | table = fib6_new_table(net, cfg->fc_table); |
| 3605 | } |
David S. Miller | 3830847 | 2011-12-03 18:02:47 -0500 | [diff] [blame] | 3606 | |
| 3607 | if (!table) |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 3608 | goto out; |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 3609 | |
David Ahern | 93531c6 | 2018-04-17 17:33:25 -0700 | [diff] [blame] | 3610 | err = -ENOMEM; |
David Ahern | f88d8ea | 2019-06-03 20:19:52 -0700 | [diff] [blame] | 3611 | rt = fib6_info_alloc(gfp_flags, !nh); |
David Ahern | 93531c6 | 2018-04-17 17:33:25 -0700 | [diff] [blame] | 3612 | if (!rt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3613 | goto out; |
David Ahern | 93531c6 | 2018-04-17 17:33:25 -0700 | [diff] [blame] | 3614 | |
David Ahern | d7e774f | 2018-11-06 12:51:15 -0800 | [diff] [blame] | 3615 | rt->fib6_metrics = ip_fib_metrics_init(net, cfg->fc_mx, cfg->fc_mx_len, |
| 3616 | extack); |
David Ahern | 767a221 | 2018-10-04 20:07:51 -0700 | [diff] [blame] | 3617 | if (IS_ERR(rt->fib6_metrics)) { |
| 3618 | err = PTR_ERR(rt->fib6_metrics); |
Eric Dumazet | fda21d4 | 2018-10-05 09:17:50 -0700 | [diff] [blame] | 3619 | /* Do not leave garbage there. */ |
| 3620 | rt->fib6_metrics = (struct dst_metrics *)&dst_default_metrics; |
David Ahern | 767a221 | 2018-10-04 20:07:51 -0700 | [diff] [blame] | 3621 | goto out; |
| 3622 | } |
| 3623 | |
David Ahern | 93531c6 | 2018-04-17 17:33:25 -0700 | [diff] [blame] | 3624 | if (cfg->fc_flags & RTF_ADDRCONF) |
| 3625 | rt->dst_nocount = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3626 | |
Gao feng | 1716a96 | 2012-04-06 00:13:10 +0000 | [diff] [blame] | 3627 | if (cfg->fc_flags & RTF_EXPIRES) |
David Ahern | 1489568 | 2018-04-17 17:33:17 -0700 | [diff] [blame] | 3628 | fib6_set_expires(rt, jiffies + |
Gao feng | 1716a96 | 2012-04-06 00:13:10 +0000 | [diff] [blame] | 3629 | clock_t_to_jiffies(cfg->fc_expires)); |
| 3630 | else |
David Ahern | 1489568 | 2018-04-17 17:33:17 -0700 | [diff] [blame] | 3631 | fib6_clean_expires(rt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3632 | |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 3633 | if (cfg->fc_protocol == RTPROT_UNSPEC) |
| 3634 | cfg->fc_protocol = RTPROT_BOOT; |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 3635 | rt->fib6_protocol = cfg->fc_protocol; |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 3636 | |
David Ahern | 83c44251 | 2019-03-27 20:53:50 -0700 | [diff] [blame] | 3637 | rt->fib6_table = table; |
| 3638 | rt->fib6_metric = cfg->fc_metric; |
David Ahern | c7036d9 | 2019-06-19 10:50:24 -0700 | [diff] [blame] | 3639 | rt->fib6_type = cfg->fc_type ? : RTN_UNICAST; |
David Ahern | 2b2450c | 2019-03-27 20:53:52 -0700 | [diff] [blame] | 3640 | rt->fib6_flags = cfg->fc_flags & ~RTF_GATEWAY; |
Roopa Prabhu | 19e42e4 | 2015-07-21 10:43:48 +0200 | [diff] [blame] | 3641 | |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 3642 | ipv6_addr_prefix(&rt->fib6_dst.addr, &cfg->fc_dst, cfg->fc_dst_len); |
| 3643 | rt->fib6_dst.plen = cfg->fc_dst_len; |
Michal Kubeček | e5fd387 | 2014-03-27 13:04:08 +0100 | [diff] [blame] | 3644 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3645 | #ifdef CONFIG_IPV6_SUBTREES |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 3646 | ipv6_addr_prefix(&rt->fib6_src.addr, &cfg->fc_src, cfg->fc_src_len); |
| 3647 | rt->fib6_src.plen = cfg->fc_src_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3648 | #endif |
David Ahern | f88d8ea | 2019-06-03 20:19:52 -0700 | [diff] [blame] | 3649 | if (nh) { |
| 3650 | if (!nexthop_get(nh)) { |
| 3651 | NL_SET_ERR_MSG(extack, "Nexthop has been deleted"); |
| 3652 | goto out; |
| 3653 | } |
| 3654 | if (rt->fib6_src.plen) { |
Colin Ian King | 4daa95a | 2019-06-06 09:40:39 +0100 | [diff] [blame] | 3655 | 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] | 3656 | goto out; |
| 3657 | } |
| 3658 | rt->nh = nh; |
| 3659 | fib6_nh = nexthop_fib6_nh(rt->nh); |
| 3660 | } else { |
| 3661 | err = fib6_nh_init(net, rt->fib6_nh, cfg, gfp_flags, extack); |
| 3662 | if (err) |
| 3663 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3664 | |
David Ahern | f88d8ea | 2019-06-03 20:19:52 -0700 | [diff] [blame] | 3665 | fib6_nh = rt->fib6_nh; |
| 3666 | |
| 3667 | /* We cannot add true routes via loopback here, they would |
| 3668 | * result in kernel looping; promote them to reject routes |
| 3669 | */ |
| 3670 | addr_type = ipv6_addr_type(&cfg->fc_dst); |
| 3671 | if (fib6_is_reject(cfg->fc_flags, rt->fib6_nh->fib_nh_dev, |
| 3672 | addr_type)) |
| 3673 | rt->fib6_flags = RTF_REJECT | RTF_NONEXTHOP; |
| 3674 | } |
David Ahern | 955ec4c | 2018-01-24 19:45:29 -0800 | [diff] [blame] | 3675 | |
Daniel Walter | c3968a8 | 2011-04-13 21:10:57 +0000 | [diff] [blame] | 3676 | if (!ipv6_addr_any(&cfg->fc_prefsrc)) { |
David Ahern | f88d8ea | 2019-06-03 20:19:52 -0700 | [diff] [blame] | 3677 | struct net_device *dev = fib6_nh->fib_nh_dev; |
David Ahern | 83c44251 | 2019-03-27 20:53:50 -0700 | [diff] [blame] | 3678 | |
Daniel Walter | c3968a8 | 2011-04-13 21:10:57 +0000 | [diff] [blame] | 3679 | if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) { |
David Ahern | d5d531c | 2017-05-21 10:12:05 -0600 | [diff] [blame] | 3680 | NL_SET_ERR_MSG(extack, "Invalid source address"); |
Daniel Walter | c3968a8 | 2011-04-13 21:10:57 +0000 | [diff] [blame] | 3681 | err = -EINVAL; |
| 3682 | goto out; |
| 3683 | } |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 3684 | rt->fib6_prefsrc.addr = cfg->fc_prefsrc; |
| 3685 | rt->fib6_prefsrc.plen = 128; |
Daniel Walter | c3968a8 | 2011-04-13 21:10:57 +0000 | [diff] [blame] | 3686 | } else |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 3687 | rt->fib6_prefsrc.plen = 0; |
Daniel Walter | c3968a8 | 2011-04-13 21:10:57 +0000 | [diff] [blame] | 3688 | |
Roopa Prabhu | 8c5b83f | 2015-10-10 08:26:36 -0700 | [diff] [blame] | 3689 | return rt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3690 | out: |
David Ahern | 93531c6 | 2018-04-17 17:33:25 -0700 | [diff] [blame] | 3691 | fib6_info_release(rt); |
Roopa Prabhu | 8c5b83f | 2015-10-10 08:26:36 -0700 | [diff] [blame] | 3692 | return ERR_PTR(err); |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 3693 | } |
| 3694 | |
David Ahern | acb54e3 | 2018-04-17 17:33:22 -0700 | [diff] [blame] | 3695 | int ip6_route_add(struct fib6_config *cfg, gfp_t gfp_flags, |
David Ahern | 333c430 | 2017-05-21 10:12:04 -0600 | [diff] [blame] | 3696 | struct netlink_ext_ack *extack) |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 3697 | { |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 3698 | struct fib6_info *rt; |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 3699 | int err; |
| 3700 | |
David Ahern | acb54e3 | 2018-04-17 17:33:22 -0700 | [diff] [blame] | 3701 | rt = ip6_route_info_create(cfg, gfp_flags, extack); |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 3702 | if (IS_ERR(rt)) |
| 3703 | return PTR_ERR(rt); |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 3704 | |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 3705 | err = __ip6_ins_rt(rt, &cfg->fc_nlinfo, extack); |
David Ahern | 93531c6 | 2018-04-17 17:33:25 -0700 | [diff] [blame] | 3706 | fib6_info_release(rt); |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 3707 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3708 | return err; |
| 3709 | } |
| 3710 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 3711 | 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] | 3712 | { |
David Ahern | afb1d4b5 | 2018-04-17 17:33:11 -0700 | [diff] [blame] | 3713 | struct net *net = info->nl_net; |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 3714 | struct fib6_table *table; |
David Ahern | afb1d4b5 | 2018-04-17 17:33:11 -0700 | [diff] [blame] | 3715 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3716 | |
David Ahern | 421842e | 2018-04-17 17:33:18 -0700 | [diff] [blame] | 3717 | if (rt == net->ipv6.fib6_null_entry) { |
Gao feng | 6825a26 | 2012-09-19 19:25:34 +0000 | [diff] [blame] | 3718 | err = -ENOENT; |
| 3719 | goto out; |
| 3720 | } |
Patrick McHardy | 6c813a7 | 2006-08-06 22:22:47 -0700 | [diff] [blame] | 3721 | |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 3722 | table = rt->fib6_table; |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 3723 | spin_lock_bh(&table->tb6_lock); |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 3724 | err = fib6_del(rt, info); |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 3725 | spin_unlock_bh(&table->tb6_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3726 | |
Gao feng | 6825a26 | 2012-09-19 19:25:34 +0000 | [diff] [blame] | 3727 | out: |
David Ahern | 93531c6 | 2018-04-17 17:33:25 -0700 | [diff] [blame] | 3728 | fib6_info_release(rt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3729 | return err; |
| 3730 | } |
| 3731 | |
Roopa Prabhu | 11dd74b | 2020-04-27 13:56:45 -0700 | [diff] [blame] | 3732 | 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] | 3733 | { |
Roopa Prabhu | 11dd74b | 2020-04-27 13:56:45 -0700 | [diff] [blame] | 3734 | struct nl_info info = { |
| 3735 | .nl_net = net, |
| 3736 | .skip_notify = skip_notify |
| 3737 | }; |
David Ahern | afb1d4b5 | 2018-04-17 17:33:11 -0700 | [diff] [blame] | 3738 | |
Denis V. Lunev | 528c4ce | 2007-12-13 09:45:12 -0800 | [diff] [blame] | 3739 | return __ip6_del_rt(rt, &info); |
Thomas Graf | e0a1ad73 | 2006-08-22 00:00:21 -0700 | [diff] [blame] | 3740 | } |
| 3741 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 3742 | 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] | 3743 | { |
| 3744 | struct nl_info *info = &cfg->fc_nlinfo; |
WANG Cong | e333003 | 2017-02-27 16:07:43 -0800 | [diff] [blame] | 3745 | struct net *net = info->nl_net; |
David Ahern | 16a16cd | 2017-02-02 12:37:11 -0800 | [diff] [blame] | 3746 | struct sk_buff *skb = NULL; |
David Ahern | 0ae8133 | 2017-02-02 12:37:08 -0800 | [diff] [blame] | 3747 | struct fib6_table *table; |
WANG Cong | e333003 | 2017-02-27 16:07:43 -0800 | [diff] [blame] | 3748 | int err = -ENOENT; |
David Ahern | 0ae8133 | 2017-02-02 12:37:08 -0800 | [diff] [blame] | 3749 | |
David Ahern | 421842e | 2018-04-17 17:33:18 -0700 | [diff] [blame] | 3750 | if (rt == net->ipv6.fib6_null_entry) |
WANG Cong | e333003 | 2017-02-27 16:07:43 -0800 | [diff] [blame] | 3751 | goto out_put; |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 3752 | table = rt->fib6_table; |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 3753 | spin_lock_bh(&table->tb6_lock); |
David Ahern | 0ae8133 | 2017-02-02 12:37:08 -0800 | [diff] [blame] | 3754 | |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 3755 | if (rt->fib6_nsiblings && cfg->fc_delete_all_nh) { |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 3756 | struct fib6_info *sibling, *next_sibling; |
Ido Schimmel | 0284696 | 2019-12-23 15:28:18 +0200 | [diff] [blame] | 3757 | struct fib6_node *fn; |
David Ahern | 0ae8133 | 2017-02-02 12:37:08 -0800 | [diff] [blame] | 3758 | |
David Ahern | 16a16cd | 2017-02-02 12:37:11 -0800 | [diff] [blame] | 3759 | /* prefer to send a single notification with all hops */ |
| 3760 | skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any()); |
| 3761 | if (skb) { |
| 3762 | u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0; |
| 3763 | |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 3764 | if (rt6_fill_node(net, skb, rt, NULL, |
David Ahern | 16a16cd | 2017-02-02 12:37:11 -0800 | [diff] [blame] | 3765 | NULL, NULL, 0, RTM_DELROUTE, |
| 3766 | info->portid, seq, 0) < 0) { |
| 3767 | kfree_skb(skb); |
| 3768 | skb = NULL; |
| 3769 | } else |
| 3770 | info->skip_notify = 1; |
| 3771 | } |
| 3772 | |
Ido Schimmel | 0284696 | 2019-12-23 15:28:18 +0200 | [diff] [blame] | 3773 | /* 'rt' points to the first sibling route. If it is not the |
| 3774 | * leaf, then we do not need to send a notification. Otherwise, |
| 3775 | * we need to check if the last sibling has a next route or not |
| 3776 | * and emit a replace or delete notification, respectively. |
| 3777 | */ |
Ido Schimmel | 2881fd6 | 2019-06-18 18:12:49 +0300 | [diff] [blame] | 3778 | info->skip_notify_kernel = 1; |
Ido Schimmel | 0284696 | 2019-12-23 15:28:18 +0200 | [diff] [blame] | 3779 | fn = rcu_dereference_protected(rt->fib6_node, |
| 3780 | lockdep_is_held(&table->tb6_lock)); |
| 3781 | if (rcu_access_pointer(fn->leaf) == rt) { |
| 3782 | struct fib6_info *last_sibling, *replace_rt; |
| 3783 | |
| 3784 | last_sibling = list_last_entry(&rt->fib6_siblings, |
| 3785 | struct fib6_info, |
| 3786 | fib6_siblings); |
| 3787 | replace_rt = rcu_dereference_protected( |
| 3788 | last_sibling->fib6_next, |
| 3789 | lockdep_is_held(&table->tb6_lock)); |
| 3790 | if (replace_rt) |
| 3791 | call_fib6_entry_notifiers_replace(net, |
| 3792 | replace_rt); |
| 3793 | else |
| 3794 | call_fib6_multipath_entry_notifiers(net, |
Ido Schimmel | caafb25 | 2019-12-23 15:28:20 +0200 | [diff] [blame] | 3795 | FIB_EVENT_ENTRY_DEL, |
Ido Schimmel | 0284696 | 2019-12-23 15:28:18 +0200 | [diff] [blame] | 3796 | rt, rt->fib6_nsiblings, |
| 3797 | NULL); |
| 3798 | } |
David Ahern | 0ae8133 | 2017-02-02 12:37:08 -0800 | [diff] [blame] | 3799 | list_for_each_entry_safe(sibling, next_sibling, |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 3800 | &rt->fib6_siblings, |
| 3801 | fib6_siblings) { |
David Ahern | 0ae8133 | 2017-02-02 12:37:08 -0800 | [diff] [blame] | 3802 | err = fib6_del(sibling, info); |
| 3803 | if (err) |
WANG Cong | e333003 | 2017-02-27 16:07:43 -0800 | [diff] [blame] | 3804 | goto out_unlock; |
David Ahern | 0ae8133 | 2017-02-02 12:37:08 -0800 | [diff] [blame] | 3805 | } |
| 3806 | } |
| 3807 | |
| 3808 | err = fib6_del(rt, info); |
WANG Cong | e333003 | 2017-02-27 16:07:43 -0800 | [diff] [blame] | 3809 | out_unlock: |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 3810 | spin_unlock_bh(&table->tb6_lock); |
WANG Cong | e333003 | 2017-02-27 16:07:43 -0800 | [diff] [blame] | 3811 | out_put: |
David Ahern | 93531c6 | 2018-04-17 17:33:25 -0700 | [diff] [blame] | 3812 | fib6_info_release(rt); |
David Ahern | 16a16cd | 2017-02-02 12:37:11 -0800 | [diff] [blame] | 3813 | |
| 3814 | if (skb) { |
WANG Cong | e333003 | 2017-02-27 16:07:43 -0800 | [diff] [blame] | 3815 | rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE, |
David Ahern | 16a16cd | 2017-02-02 12:37:11 -0800 | [diff] [blame] | 3816 | info->nlh, gfp_any()); |
| 3817 | } |
David Ahern | 0ae8133 | 2017-02-02 12:37:08 -0800 | [diff] [blame] | 3818 | return err; |
| 3819 | } |
| 3820 | |
David Ahern | 0fa6efc | 2019-05-22 20:28:00 -0700 | [diff] [blame] | 3821 | 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] | 3822 | { |
| 3823 | int rc = -ESRCH; |
| 3824 | |
| 3825 | if (cfg->fc_ifindex && rt->dst.dev->ifindex != cfg->fc_ifindex) |
| 3826 | goto out; |
| 3827 | |
| 3828 | if (cfg->fc_flags & RTF_GATEWAY && |
| 3829 | !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway)) |
| 3830 | goto out; |
Xin Long | 761f602 | 2018-11-14 00:48:28 +0800 | [diff] [blame] | 3831 | |
| 3832 | rc = rt6_remove_exception_rt(rt); |
David Ahern | 23fb93a | 2018-04-17 17:33:23 -0700 | [diff] [blame] | 3833 | out: |
| 3834 | return rc; |
| 3835 | } |
| 3836 | |
David Ahern | 0fa6efc | 2019-05-22 20:28:00 -0700 | [diff] [blame] | 3837 | static int ip6_del_cached_rt(struct fib6_config *cfg, struct fib6_info *rt, |
| 3838 | struct fib6_nh *nh) |
| 3839 | { |
| 3840 | struct fib6_result res = { |
| 3841 | .f6i = rt, |
| 3842 | .nh = nh, |
| 3843 | }; |
| 3844 | struct rt6_info *rt_cache; |
| 3845 | |
| 3846 | rt_cache = rt6_find_cached_rt(&res, &cfg->fc_dst, &cfg->fc_src); |
| 3847 | if (rt_cache) |
| 3848 | return __ip6_del_cached_rt(rt_cache, cfg); |
| 3849 | |
| 3850 | return 0; |
| 3851 | } |
| 3852 | |
David Ahern | 5b98324 | 2019-06-08 14:53:34 -0700 | [diff] [blame] | 3853 | struct fib6_nh_del_cached_rt_arg { |
| 3854 | struct fib6_config *cfg; |
| 3855 | struct fib6_info *f6i; |
| 3856 | }; |
| 3857 | |
| 3858 | static int fib6_nh_del_cached_rt(struct fib6_nh *nh, void *_arg) |
| 3859 | { |
| 3860 | struct fib6_nh_del_cached_rt_arg *arg = _arg; |
| 3861 | int rc; |
| 3862 | |
| 3863 | rc = ip6_del_cached_rt(arg->cfg, arg->f6i, nh); |
| 3864 | return rc != -ESRCH ? rc : 0; |
| 3865 | } |
| 3866 | |
| 3867 | static int ip6_del_cached_rt_nh(struct fib6_config *cfg, struct fib6_info *f6i) |
| 3868 | { |
| 3869 | struct fib6_nh_del_cached_rt_arg arg = { |
| 3870 | .cfg = cfg, |
| 3871 | .f6i = f6i |
| 3872 | }; |
| 3873 | |
| 3874 | return nexthop_for_each_fib6_nh(f6i->nh, fib6_nh_del_cached_rt, &arg); |
| 3875 | } |
| 3876 | |
David Ahern | 333c430 | 2017-05-21 10:12:04 -0600 | [diff] [blame] | 3877 | static int ip6_route_del(struct fib6_config *cfg, |
| 3878 | struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3879 | { |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 3880 | struct fib6_table *table; |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 3881 | struct fib6_info *rt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3882 | struct fib6_node *fn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3883 | int err = -ESRCH; |
| 3884 | |
Daniel Lezcano | 5578689 | 2008-03-04 13:47:47 -0800 | [diff] [blame] | 3885 | table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table); |
David Ahern | d5d531c | 2017-05-21 10:12:05 -0600 | [diff] [blame] | 3886 | if (!table) { |
| 3887 | NL_SET_ERR_MSG(extack, "FIB table does not exist"); |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 3888 | return err; |
David Ahern | d5d531c | 2017-05-21 10:12:05 -0600 | [diff] [blame] | 3889 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3890 | |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 3891 | rcu_read_lock(); |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 3892 | |
| 3893 | fn = fib6_locate(&table->tb6_root, |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 3894 | &cfg->fc_dst, cfg->fc_dst_len, |
Wei Wang | 38fbeee | 2017-10-06 12:06:02 -0700 | [diff] [blame] | 3895 | &cfg->fc_src, cfg->fc_src_len, |
Wei Wang | 2b760fc | 2017-10-06 12:06:03 -0700 | [diff] [blame] | 3896 | !(cfg->fc_flags & RTF_CACHE)); |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 3897 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3898 | if (fn) { |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 3899 | for_each_fib6_node_rt_rcu(fn) { |
David Ahern | ad1601a | 2019-03-27 20:53:56 -0700 | [diff] [blame] | 3900 | struct fib6_nh *nh; |
| 3901 | |
Stefano Brivio | 3401bfb | 2019-06-21 17:45:25 +0200 | [diff] [blame] | 3902 | if (rt->nh && cfg->fc_nh_id && |
| 3903 | rt->nh->id != cfg->fc_nh_id) |
David Ahern | 5b98324 | 2019-06-08 14:53:34 -0700 | [diff] [blame] | 3904 | continue; |
David Ahern | 23fb93a | 2018-04-17 17:33:23 -0700 | [diff] [blame] | 3905 | |
David Ahern | 5b98324 | 2019-06-08 14:53:34 -0700 | [diff] [blame] | 3906 | if (cfg->fc_flags & RTF_CACHE) { |
| 3907 | int rc = 0; |
| 3908 | |
| 3909 | if (rt->nh) { |
| 3910 | rc = ip6_del_cached_rt_nh(cfg, rt); |
| 3911 | } else if (cfg->fc_nh_id) { |
| 3912 | continue; |
| 3913 | } else { |
| 3914 | nh = rt->fib6_nh; |
| 3915 | rc = ip6_del_cached_rt(cfg, rt, nh); |
| 3916 | } |
David Ahern | 0fa6efc | 2019-05-22 20:28:00 -0700 | [diff] [blame] | 3917 | if (rc != -ESRCH) { |
| 3918 | rcu_read_unlock(); |
| 3919 | return rc; |
David Ahern | 23fb93a | 2018-04-17 17:33:23 -0700 | [diff] [blame] | 3920 | } |
| 3921 | continue; |
Wei Wang | 2b760fc | 2017-10-06 12:06:03 -0700 | [diff] [blame] | 3922 | } |
David Ahern | ad1601a | 2019-03-27 20:53:56 -0700 | [diff] [blame] | 3923 | |
David Ahern | 5b98324 | 2019-06-08 14:53:34 -0700 | [diff] [blame] | 3924 | if (cfg->fc_metric && cfg->fc_metric != rt->fib6_metric) |
| 3925 | continue; |
| 3926 | if (cfg->fc_protocol && |
| 3927 | cfg->fc_protocol != rt->fib6_protocol) |
| 3928 | continue; |
| 3929 | |
| 3930 | if (rt->nh) { |
| 3931 | if (!fib6_info_hold_safe(rt)) |
| 3932 | continue; |
| 3933 | rcu_read_unlock(); |
| 3934 | |
| 3935 | return __ip6_del_rt(rt, &cfg->fc_nlinfo); |
| 3936 | } |
| 3937 | if (cfg->fc_nh_id) |
| 3938 | continue; |
| 3939 | |
| 3940 | nh = rt->fib6_nh; |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 3941 | if (cfg->fc_ifindex && |
David Ahern | ad1601a | 2019-03-27 20:53:56 -0700 | [diff] [blame] | 3942 | (!nh->fib_nh_dev || |
| 3943 | nh->fib_nh_dev->ifindex != cfg->fc_ifindex)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3944 | continue; |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 3945 | if (cfg->fc_flags & RTF_GATEWAY && |
David Ahern | ad1601a | 2019-03-27 20:53:56 -0700 | [diff] [blame] | 3946 | !ipv6_addr_equal(&cfg->fc_gateway, &nh->fib_nh_gw6)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3947 | continue; |
Wei Wang | e873e4b | 2018-07-21 20:56:32 -0700 | [diff] [blame] | 3948 | if (!fib6_info_hold_safe(rt)) |
| 3949 | continue; |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 3950 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3951 | |
David Ahern | 0ae8133 | 2017-02-02 12:37:08 -0800 | [diff] [blame] | 3952 | /* if gateway was specified only delete the one hop */ |
| 3953 | if (cfg->fc_flags & RTF_GATEWAY) |
| 3954 | return __ip6_del_rt(rt, &cfg->fc_nlinfo); |
| 3955 | |
| 3956 | return __ip6_del_rt_siblings(rt, cfg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3957 | } |
| 3958 | } |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 3959 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3960 | |
| 3961 | return err; |
| 3962 | } |
| 3963 | |
David S. Miller | 6700c27 | 2012-07-17 03:29:28 -0700 | [diff] [blame] | 3964 | 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] | 3965 | { |
YOSHIFUJI Hideaki | a627945 | 2006-08-23 17:18:26 -0700 | [diff] [blame] | 3966 | struct netevent_redirect netevent; |
David S. Miller | e8599ff | 2012-07-11 23:43:53 -0700 | [diff] [blame] | 3967 | struct rt6_info *rt, *nrt = NULL; |
David Ahern | 85bd05d | 2019-04-16 14:36:01 -0700 | [diff] [blame] | 3968 | struct fib6_result res = {}; |
David S. Miller | e8599ff | 2012-07-11 23:43:53 -0700 | [diff] [blame] | 3969 | struct ndisc_options ndopts; |
| 3970 | struct inet6_dev *in6_dev; |
| 3971 | struct neighbour *neigh; |
YOSHIFUJI Hideaki / 吉藤英明 | 71bcdba | 2013-01-05 16:34:51 +0000 | [diff] [blame] | 3972 | struct rd_msg *msg; |
David S. Miller | 6e157b6 | 2012-07-12 00:05:02 -0700 | [diff] [blame] | 3973 | int optlen, on_link; |
| 3974 | u8 *lladdr; |
David S. Miller | e8599ff | 2012-07-11 23:43:53 -0700 | [diff] [blame] | 3975 | |
Simon Horman | 29a3cad | 2013-05-28 20:34:26 +0000 | [diff] [blame] | 3976 | optlen = skb_tail_pointer(skb) - skb_transport_header(skb); |
YOSHIFUJI Hideaki / 吉藤英明 | 71bcdba | 2013-01-05 16:34:51 +0000 | [diff] [blame] | 3977 | optlen -= sizeof(*msg); |
David S. Miller | e8599ff | 2012-07-11 23:43:53 -0700 | [diff] [blame] | 3978 | |
| 3979 | if (optlen < 0) { |
David S. Miller | 6e157b6 | 2012-07-12 00:05:02 -0700 | [diff] [blame] | 3980 | net_dbg_ratelimited("rt6_do_redirect: packet too short\n"); |
David S. Miller | e8599ff | 2012-07-11 23:43:53 -0700 | [diff] [blame] | 3981 | return; |
| 3982 | } |
| 3983 | |
YOSHIFUJI Hideaki / 吉藤英明 | 71bcdba | 2013-01-05 16:34:51 +0000 | [diff] [blame] | 3984 | msg = (struct rd_msg *)icmp6_hdr(skb); |
David S. Miller | e8599ff | 2012-07-11 23:43:53 -0700 | [diff] [blame] | 3985 | |
YOSHIFUJI Hideaki / 吉藤英明 | 71bcdba | 2013-01-05 16:34:51 +0000 | [diff] [blame] | 3986 | if (ipv6_addr_is_multicast(&msg->dest)) { |
David S. Miller | 6e157b6 | 2012-07-12 00:05:02 -0700 | [diff] [blame] | 3987 | net_dbg_ratelimited("rt6_do_redirect: destination address is multicast\n"); |
David S. Miller | e8599ff | 2012-07-11 23:43:53 -0700 | [diff] [blame] | 3988 | return; |
| 3989 | } |
| 3990 | |
David S. Miller | 6e157b6 | 2012-07-12 00:05:02 -0700 | [diff] [blame] | 3991 | on_link = 0; |
YOSHIFUJI Hideaki / 吉藤英明 | 71bcdba | 2013-01-05 16:34:51 +0000 | [diff] [blame] | 3992 | if (ipv6_addr_equal(&msg->dest, &msg->target)) { |
David S. Miller | e8599ff | 2012-07-11 23:43:53 -0700 | [diff] [blame] | 3993 | on_link = 1; |
YOSHIFUJI Hideaki / 吉藤英明 | 71bcdba | 2013-01-05 16:34:51 +0000 | [diff] [blame] | 3994 | } else if (ipv6_addr_type(&msg->target) != |
David S. Miller | e8599ff | 2012-07-11 23:43:53 -0700 | [diff] [blame] | 3995 | (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) { |
David S. Miller | 6e157b6 | 2012-07-12 00:05:02 -0700 | [diff] [blame] | 3996 | 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] | 3997 | return; |
| 3998 | } |
| 3999 | |
| 4000 | in6_dev = __in6_dev_get(skb->dev); |
| 4001 | if (!in6_dev) |
| 4002 | return; |
| 4003 | if (in6_dev->cnf.forwarding || !in6_dev->cnf.accept_redirects) |
| 4004 | return; |
| 4005 | |
| 4006 | /* RFC2461 8.1: |
| 4007 | * The IP source address of the Redirect MUST be the same as the current |
| 4008 | * first-hop router for the specified ICMP Destination Address. |
| 4009 | */ |
| 4010 | |
Alexander Aring | f997c55 | 2016-06-15 21:20:23 +0200 | [diff] [blame] | 4011 | if (!ndisc_parse_options(skb->dev, msg->opt, optlen, &ndopts)) { |
David S. Miller | e8599ff | 2012-07-11 23:43:53 -0700 | [diff] [blame] | 4012 | net_dbg_ratelimited("rt6_redirect: invalid ND options\n"); |
| 4013 | return; |
| 4014 | } |
David S. Miller | 6e157b6 | 2012-07-12 00:05:02 -0700 | [diff] [blame] | 4015 | |
| 4016 | lladdr = NULL; |
David S. Miller | e8599ff | 2012-07-11 23:43:53 -0700 | [diff] [blame] | 4017 | if (ndopts.nd_opts_tgt_lladdr) { |
| 4018 | lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr, |
| 4019 | skb->dev); |
| 4020 | if (!lladdr) { |
| 4021 | net_dbg_ratelimited("rt6_redirect: invalid link-layer address length\n"); |
| 4022 | return; |
| 4023 | } |
| 4024 | } |
| 4025 | |
David S. Miller | 6e157b6 | 2012-07-12 00:05:02 -0700 | [diff] [blame] | 4026 | rt = (struct rt6_info *) dst; |
Matthias Schiffer | ec13ad1 | 2015-11-02 01:24:38 +0100 | [diff] [blame] | 4027 | if (rt->rt6i_flags & RTF_REJECT) { |
David S. Miller | 6e157b6 | 2012-07-12 00:05:02 -0700 | [diff] [blame] | 4028 | net_dbg_ratelimited("rt6_redirect: source isn't a valid nexthop for redirect target\n"); |
| 4029 | return; |
| 4030 | } |
| 4031 | |
| 4032 | /* Redirect received -> path was valid. |
| 4033 | * Look, redirects are sent only in response to data packets, |
| 4034 | * so that this nexthop apparently is reachable. --ANK |
| 4035 | */ |
Julian Anastasov | 0dec879 | 2017-02-06 23:14:16 +0200 | [diff] [blame] | 4036 | dst_confirm_neigh(&rt->dst, &ipv6_hdr(skb)->saddr); |
David S. Miller | 6e157b6 | 2012-07-12 00:05:02 -0700 | [diff] [blame] | 4037 | |
YOSHIFUJI Hideaki / 吉藤英明 | 71bcdba | 2013-01-05 16:34:51 +0000 | [diff] [blame] | 4038 | neigh = __neigh_lookup(&nd_tbl, &msg->target, skb->dev, 1); |
David S. Miller | e8599ff | 2012-07-11 23:43:53 -0700 | [diff] [blame] | 4039 | if (!neigh) |
| 4040 | return; |
| 4041 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4042 | /* |
| 4043 | * We have finally decided to accept it. |
| 4044 | */ |
| 4045 | |
Alexander Aring | f997c55 | 2016-06-15 21:20:23 +0200 | [diff] [blame] | 4046 | ndisc_update(skb->dev, neigh, lladdr, NUD_STALE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4047 | NEIGH_UPDATE_F_WEAK_OVERRIDE| |
| 4048 | NEIGH_UPDATE_F_OVERRIDE| |
| 4049 | (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER| |
Alexander Aring | f997c55 | 2016-06-15 21:20:23 +0200 | [diff] [blame] | 4050 | NEIGH_UPDATE_F_ISROUTER)), |
| 4051 | NDISC_REDIRECT, &ndopts); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4052 | |
David Ahern | 4d85cd0 | 2018-04-20 15:37:59 -0700 | [diff] [blame] | 4053 | rcu_read_lock(); |
David Ahern | 85bd05d | 2019-04-16 14:36:01 -0700 | [diff] [blame] | 4054 | res.f6i = rcu_dereference(rt->from); |
David S. Miller | ff24e49 | 2019-05-02 22:14:21 -0400 | [diff] [blame] | 4055 | if (!res.f6i) |
Martin KaFai Lau | 886b7a5 | 2019-04-30 10:45:12 -0700 | [diff] [blame] | 4056 | goto out; |
David Ahern | 8a14e46 | 2018-04-23 11:32:07 -0700 | [diff] [blame] | 4057 | |
David Ahern | 49d5b8e | 2019-06-08 14:53:30 -0700 | [diff] [blame] | 4058 | if (res.f6i->nh) { |
| 4059 | struct fib6_nh_match_arg arg = { |
| 4060 | .dev = dst->dev, |
| 4061 | .gw = &rt->rt6i_gateway, |
| 4062 | }; |
| 4063 | |
| 4064 | nexthop_for_each_fib6_nh(res.f6i->nh, |
| 4065 | fib6_nh_find_match, &arg); |
| 4066 | |
| 4067 | /* fib6_info uses a nexthop that does not have fib6_nh |
| 4068 | * using the dst->dev. Should be impossible |
| 4069 | */ |
| 4070 | if (!arg.match) |
| 4071 | goto out; |
| 4072 | res.nh = arg.match; |
| 4073 | } else { |
| 4074 | res.nh = res.f6i->fib6_nh; |
| 4075 | } |
| 4076 | |
David Ahern | 7d21fec | 2019-04-16 14:36:11 -0700 | [diff] [blame] | 4077 | res.fib6_flags = res.f6i->fib6_flags; |
| 4078 | res.fib6_type = res.f6i->fib6_type; |
David Ahern | 85bd05d | 2019-04-16 14:36:01 -0700 | [diff] [blame] | 4079 | nrt = ip6_rt_cache_alloc(&res, &msg->dest, NULL); |
David S. Miller | 3830847 | 2011-12-03 18:02:47 -0500 | [diff] [blame] | 4080 | if (!nrt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4081 | goto out; |
| 4082 | |
| 4083 | nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE; |
| 4084 | if (on_link) |
| 4085 | nrt->rt6i_flags &= ~RTF_GATEWAY; |
| 4086 | |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 4087 | nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4088 | |
Martin KaFai Lau | 886b7a5 | 2019-04-30 10:45:12 -0700 | [diff] [blame] | 4089 | /* rt6_insert_exception() will take care of duplicated exceptions */ |
David Ahern | 5012f0a | 2019-04-16 14:36:05 -0700 | [diff] [blame] | 4090 | if (rt6_insert_exception(nrt, &res)) { |
Wei Wang | 2b760fc | 2017-10-06 12:06:03 -0700 | [diff] [blame] | 4091 | dst_release_immediate(&nrt->dst); |
| 4092 | goto out; |
| 4093 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4094 | |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 4095 | netevent.old = &rt->dst; |
| 4096 | netevent.new = &nrt->dst; |
YOSHIFUJI Hideaki / 吉藤英明 | 71bcdba | 2013-01-05 16:34:51 +0000 | [diff] [blame] | 4097 | netevent.daddr = &msg->dest; |
YOSHIFUJI Hideaki / 吉藤英明 | 6059283 | 2013-01-14 09:28:27 +0000 | [diff] [blame] | 4098 | netevent.neigh = neigh; |
Tom Tucker | 8d71740 | 2006-07-30 20:43:36 -0700 | [diff] [blame] | 4099 | call_netevent_notifiers(NETEVENT_REDIRECT, &netevent); |
| 4100 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4101 | out: |
Martin KaFai Lau | 886b7a5 | 2019-04-30 10:45:12 -0700 | [diff] [blame] | 4102 | rcu_read_unlock(); |
David S. Miller | e8599ff | 2012-07-11 23:43:53 -0700 | [diff] [blame] | 4103 | neigh_release(neigh); |
David S. Miller | 6e157b6 | 2012-07-12 00:05:02 -0700 | [diff] [blame] | 4104 | } |
| 4105 | |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 4106 | #ifdef CONFIG_IPV6_ROUTE_INFO |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4107 | static struct fib6_info *rt6_get_route_info(struct net *net, |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 4108 | const struct in6_addr *prefix, int prefixlen, |
David Ahern | 830218c | 2016-10-24 10:52:35 -0700 | [diff] [blame] | 4109 | const struct in6_addr *gwaddr, |
| 4110 | struct net_device *dev) |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 4111 | { |
David Ahern | 830218c | 2016-10-24 10:52:35 -0700 | [diff] [blame] | 4112 | u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO; |
| 4113 | int ifindex = dev->ifindex; |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 4114 | struct fib6_node *fn; |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4115 | struct fib6_info *rt = NULL; |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 4116 | struct fib6_table *table; |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 4117 | |
David Ahern | 830218c | 2016-10-24 10:52:35 -0700 | [diff] [blame] | 4118 | table = fib6_get_table(net, tb_id); |
David S. Miller | 3830847 | 2011-12-03 18:02:47 -0500 | [diff] [blame] | 4119 | if (!table) |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 4120 | return NULL; |
| 4121 | |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 4122 | rcu_read_lock(); |
Wei Wang | 38fbeee | 2017-10-06 12:06:02 -0700 | [diff] [blame] | 4123 | fn = fib6_locate(&table->tb6_root, prefix, prefixlen, NULL, 0, true); |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 4124 | if (!fn) |
| 4125 | goto out; |
| 4126 | |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 4127 | for_each_fib6_node_rt_rcu(fn) { |
David Ahern | f88d8ea | 2019-06-03 20:19:52 -0700 | [diff] [blame] | 4128 | /* these routes do not use nexthops */ |
| 4129 | if (rt->nh) |
| 4130 | continue; |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 4131 | if (rt->fib6_nh->fib_nh_dev->ifindex != ifindex) |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 4132 | continue; |
David Ahern | 2b2450c | 2019-03-27 20:53:52 -0700 | [diff] [blame] | 4133 | if (!(rt->fib6_flags & RTF_ROUTEINFO) || |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 4134 | !rt->fib6_nh->fib_nh_gw_family) |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 4135 | continue; |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 4136 | if (!ipv6_addr_equal(&rt->fib6_nh->fib_nh_gw6, gwaddr)) |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 4137 | continue; |
Wei Wang | e873e4b | 2018-07-21 20:56:32 -0700 | [diff] [blame] | 4138 | if (!fib6_info_hold_safe(rt)) |
| 4139 | continue; |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 4140 | break; |
| 4141 | } |
| 4142 | out: |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 4143 | rcu_read_unlock(); |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 4144 | return rt; |
| 4145 | } |
| 4146 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4147 | static struct fib6_info *rt6_add_route_info(struct net *net, |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 4148 | const struct in6_addr *prefix, int prefixlen, |
David Ahern | 830218c | 2016-10-24 10:52:35 -0700 | [diff] [blame] | 4149 | const struct in6_addr *gwaddr, |
| 4150 | struct net_device *dev, |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 4151 | unsigned int pref) |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 4152 | { |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4153 | struct fib6_config cfg = { |
Rami Rosen | 238fc7e | 2008-02-09 23:43:11 -0800 | [diff] [blame] | 4154 | .fc_metric = IP6_RT_PRIO_USER, |
David Ahern | 830218c | 2016-10-24 10:52:35 -0700 | [diff] [blame] | 4155 | .fc_ifindex = dev->ifindex, |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4156 | .fc_dst_len = prefixlen, |
| 4157 | .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO | |
| 4158 | RTF_UP | RTF_PREF(pref), |
Xin Long | b91d532 | 2017-08-03 14:13:46 +0800 | [diff] [blame] | 4159 | .fc_protocol = RTPROT_RA, |
David Ahern | e8478e8 | 2018-04-17 17:33:13 -0700 | [diff] [blame] | 4160 | .fc_type = RTN_UNICAST, |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 4161 | .fc_nlinfo.portid = 0, |
Daniel Lezcano | efa2cea | 2008-03-04 13:46:48 -0800 | [diff] [blame] | 4162 | .fc_nlinfo.nlh = NULL, |
| 4163 | .fc_nlinfo.nl_net = net, |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4164 | }; |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 4165 | |
David Ahern | 830218c | 2016-10-24 10:52:35 -0700 | [diff] [blame] | 4166 | cfg.fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO, |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 4167 | cfg.fc_dst = *prefix; |
| 4168 | cfg.fc_gateway = *gwaddr; |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4169 | |
YOSHIFUJI Hideaki | e317da9 | 2006-03-20 17:06:42 -0800 | [diff] [blame] | 4170 | /* We should treat it as a default route if prefix length is 0. */ |
| 4171 | if (!prefixlen) |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4172 | cfg.fc_flags |= RTF_DEFAULT; |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 4173 | |
David Ahern | acb54e3 | 2018-04-17 17:33:22 -0700 | [diff] [blame] | 4174 | ip6_route_add(&cfg, GFP_ATOMIC, NULL); |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 4175 | |
David Ahern | 830218c | 2016-10-24 10:52:35 -0700 | [diff] [blame] | 4176 | return rt6_get_route_info(net, prefix, prefixlen, gwaddr, dev); |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 4177 | } |
| 4178 | #endif |
| 4179 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4180 | struct fib6_info *rt6_get_dflt_router(struct net *net, |
David Ahern | afb1d4b5 | 2018-04-17 17:33:11 -0700 | [diff] [blame] | 4181 | const struct in6_addr *addr, |
| 4182 | struct net_device *dev) |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 4183 | { |
David Ahern | 830218c | 2016-10-24 10:52:35 -0700 | [diff] [blame] | 4184 | u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT; |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4185 | struct fib6_info *rt; |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 4186 | struct fib6_table *table; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4187 | |
David Ahern | afb1d4b5 | 2018-04-17 17:33:11 -0700 | [diff] [blame] | 4188 | table = fib6_get_table(net, tb_id); |
David S. Miller | 3830847 | 2011-12-03 18:02:47 -0500 | [diff] [blame] | 4189 | if (!table) |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 4190 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4191 | |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 4192 | rcu_read_lock(); |
| 4193 | for_each_fib6_node_rt_rcu(&table->tb6_root) { |
David Ahern | f88d8ea | 2019-06-03 20:19:52 -0700 | [diff] [blame] | 4194 | struct fib6_nh *nh; |
David Ahern | ad1601a | 2019-03-27 20:53:56 -0700 | [diff] [blame] | 4195 | |
David Ahern | f88d8ea | 2019-06-03 20:19:52 -0700 | [diff] [blame] | 4196 | /* RA routes do not use nexthops */ |
| 4197 | if (rt->nh) |
| 4198 | continue; |
| 4199 | |
| 4200 | nh = rt->fib6_nh; |
David Ahern | ad1601a | 2019-03-27 20:53:56 -0700 | [diff] [blame] | 4201 | if (dev == nh->fib_nh_dev && |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 4202 | ((rt->fib6_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) && |
David Ahern | ad1601a | 2019-03-27 20:53:56 -0700 | [diff] [blame] | 4203 | ipv6_addr_equal(&nh->fib_nh_gw6, addr)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4204 | break; |
| 4205 | } |
Wei Wang | e873e4b | 2018-07-21 20:56:32 -0700 | [diff] [blame] | 4206 | if (rt && !fib6_info_hold_safe(rt)) |
| 4207 | rt = NULL; |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 4208 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4209 | return rt; |
| 4210 | } |
| 4211 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4212 | struct fib6_info *rt6_add_dflt_router(struct net *net, |
David Ahern | afb1d4b5 | 2018-04-17 17:33:11 -0700 | [diff] [blame] | 4213 | const struct in6_addr *gwaddr, |
YOSHIFUJI Hideaki | ebacaaa | 2006-03-20 17:04:53 -0800 | [diff] [blame] | 4214 | struct net_device *dev, |
| 4215 | unsigned int pref) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4216 | { |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4217 | struct fib6_config cfg = { |
David Ahern | ca25449 | 2015-10-12 11:47:10 -0700 | [diff] [blame] | 4218 | .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT, |
Rami Rosen | 238fc7e | 2008-02-09 23:43:11 -0800 | [diff] [blame] | 4219 | .fc_metric = IP6_RT_PRIO_USER, |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4220 | .fc_ifindex = dev->ifindex, |
| 4221 | .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT | |
| 4222 | RTF_UP | RTF_EXPIRES | RTF_PREF(pref), |
Xin Long | b91d532 | 2017-08-03 14:13:46 +0800 | [diff] [blame] | 4223 | .fc_protocol = RTPROT_RA, |
David Ahern | e8478e8 | 2018-04-17 17:33:13 -0700 | [diff] [blame] | 4224 | .fc_type = RTN_UNICAST, |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 4225 | .fc_nlinfo.portid = 0, |
Daniel Lezcano | 5578689 | 2008-03-04 13:47:47 -0800 | [diff] [blame] | 4226 | .fc_nlinfo.nlh = NULL, |
David Ahern | afb1d4b5 | 2018-04-17 17:33:11 -0700 | [diff] [blame] | 4227 | .fc_nlinfo.nl_net = net, |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4228 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4229 | |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 4230 | cfg.fc_gateway = *gwaddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4231 | |
David Ahern | acb54e3 | 2018-04-17 17:33:22 -0700 | [diff] [blame] | 4232 | if (!ip6_route_add(&cfg, GFP_ATOMIC, NULL)) { |
David Ahern | 830218c | 2016-10-24 10:52:35 -0700 | [diff] [blame] | 4233 | struct fib6_table *table; |
| 4234 | |
| 4235 | table = fib6_get_table(dev_net(dev), cfg.fc_table); |
| 4236 | if (table) |
| 4237 | table->flags |= RT6_TABLE_HAS_DFLT_ROUTER; |
| 4238 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4239 | |
David Ahern | afb1d4b5 | 2018-04-17 17:33:11 -0700 | [diff] [blame] | 4240 | return rt6_get_dflt_router(net, gwaddr, dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4241 | } |
| 4242 | |
David Ahern | afb1d4b5 | 2018-04-17 17:33:11 -0700 | [diff] [blame] | 4243 | static void __rt6_purge_dflt_routers(struct net *net, |
| 4244 | struct fib6_table *table) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4245 | { |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4246 | struct fib6_info *rt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4247 | |
| 4248 | restart: |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 4249 | rcu_read_lock(); |
| 4250 | for_each_fib6_node_rt_rcu(&table->tb6_root) { |
David Ahern | dcd1f57 | 2018-04-18 15:39:05 -0700 | [diff] [blame] | 4251 | struct net_device *dev = fib6_info_nh_dev(rt); |
| 4252 | struct inet6_dev *idev = dev ? __in6_dev_get(dev) : NULL; |
| 4253 | |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 4254 | if (rt->fib6_flags & (RTF_DEFAULT | RTF_ADDRCONF) && |
Wei Wang | e873e4b | 2018-07-21 20:56:32 -0700 | [diff] [blame] | 4255 | (!idev || idev->cnf.accept_ra != 2) && |
| 4256 | fib6_info_hold_safe(rt)) { |
David Ahern | 93531c6 | 2018-04-17 17:33:25 -0700 | [diff] [blame] | 4257 | rcu_read_unlock(); |
Roopa Prabhu | 11dd74b | 2020-04-27 13:56:45 -0700 | [diff] [blame] | 4258 | ip6_del_rt(net, rt, false); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4259 | goto restart; |
| 4260 | } |
| 4261 | } |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 4262 | rcu_read_unlock(); |
David Ahern | 830218c | 2016-10-24 10:52:35 -0700 | [diff] [blame] | 4263 | |
| 4264 | table->flags &= ~RT6_TABLE_HAS_DFLT_ROUTER; |
| 4265 | } |
| 4266 | |
| 4267 | void rt6_purge_dflt_routers(struct net *net) |
| 4268 | { |
| 4269 | struct fib6_table *table; |
| 4270 | struct hlist_head *head; |
| 4271 | unsigned int h; |
| 4272 | |
| 4273 | rcu_read_lock(); |
| 4274 | |
| 4275 | for (h = 0; h < FIB6_TABLE_HASHSZ; h++) { |
| 4276 | head = &net->ipv6.fib_table_hash[h]; |
| 4277 | hlist_for_each_entry_rcu(table, head, tb6_hlist) { |
| 4278 | if (table->flags & RT6_TABLE_HAS_DFLT_ROUTER) |
David Ahern | afb1d4b5 | 2018-04-17 17:33:11 -0700 | [diff] [blame] | 4279 | __rt6_purge_dflt_routers(net, table); |
David Ahern | 830218c | 2016-10-24 10:52:35 -0700 | [diff] [blame] | 4280 | } |
| 4281 | } |
| 4282 | |
| 4283 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4284 | } |
| 4285 | |
Daniel Lezcano | 5578689 | 2008-03-04 13:47:47 -0800 | [diff] [blame] | 4286 | static void rtmsg_to_fib6_config(struct net *net, |
| 4287 | struct in6_rtmsg *rtmsg, |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4288 | struct fib6_config *cfg) |
| 4289 | { |
Maciej Żenczykowski | 8823a3a | 2018-09-29 23:44:52 -0700 | [diff] [blame] | 4290 | *cfg = (struct fib6_config){ |
| 4291 | .fc_table = l3mdev_fib_table_by_index(net, rtmsg->rtmsg_ifindex) ? |
| 4292 | : RT6_TABLE_MAIN, |
| 4293 | .fc_ifindex = rtmsg->rtmsg_ifindex, |
David Ahern | 67f6951 | 2019-03-21 05:21:34 -0700 | [diff] [blame] | 4294 | .fc_metric = rtmsg->rtmsg_metric ? : IP6_RT_PRIO_USER, |
Maciej Żenczykowski | 8823a3a | 2018-09-29 23:44:52 -0700 | [diff] [blame] | 4295 | .fc_expires = rtmsg->rtmsg_info, |
| 4296 | .fc_dst_len = rtmsg->rtmsg_dst_len, |
| 4297 | .fc_src_len = rtmsg->rtmsg_src_len, |
| 4298 | .fc_flags = rtmsg->rtmsg_flags, |
| 4299 | .fc_type = rtmsg->rtmsg_type, |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4300 | |
Maciej Żenczykowski | 8823a3a | 2018-09-29 23:44:52 -0700 | [diff] [blame] | 4301 | .fc_nlinfo.nl_net = net, |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4302 | |
Maciej Żenczykowski | 8823a3a | 2018-09-29 23:44:52 -0700 | [diff] [blame] | 4303 | .fc_dst = rtmsg->rtmsg_dst, |
| 4304 | .fc_src = rtmsg->rtmsg_src, |
| 4305 | .fc_gateway = rtmsg->rtmsg_gateway, |
| 4306 | }; |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4307 | } |
| 4308 | |
Daniel Lezcano | 5578689 | 2008-03-04 13:47:47 -0800 | [diff] [blame] | 4309 | int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4310 | { |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4311 | struct fib6_config cfg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4312 | struct in6_rtmsg rtmsg; |
| 4313 | int err; |
| 4314 | |
Ian Morris | 67ba415 | 2014-08-24 21:53:10 +0100 | [diff] [blame] | 4315 | switch (cmd) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4316 | case SIOCADDRT: /* Add a route */ |
| 4317 | case SIOCDELRT: /* Delete a route */ |
Eric W. Biederman | af31f41 | 2012-11-16 03:03:06 +0000 | [diff] [blame] | 4318 | if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4319 | return -EPERM; |
| 4320 | err = copy_from_user(&rtmsg, arg, |
| 4321 | sizeof(struct in6_rtmsg)); |
| 4322 | if (err) |
| 4323 | return -EFAULT; |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4324 | |
Daniel Lezcano | 5578689 | 2008-03-04 13:47:47 -0800 | [diff] [blame] | 4325 | rtmsg_to_fib6_config(net, &rtmsg, &cfg); |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4326 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4327 | rtnl_lock(); |
| 4328 | switch (cmd) { |
| 4329 | case SIOCADDRT: |
David Ahern | acb54e3 | 2018-04-17 17:33:22 -0700 | [diff] [blame] | 4330 | err = ip6_route_add(&cfg, GFP_KERNEL, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4331 | break; |
| 4332 | case SIOCDELRT: |
David Ahern | 333c430 | 2017-05-21 10:12:04 -0600 | [diff] [blame] | 4333 | err = ip6_route_del(&cfg, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4334 | break; |
| 4335 | default: |
| 4336 | err = -EINVAL; |
| 4337 | } |
| 4338 | rtnl_unlock(); |
| 4339 | |
| 4340 | return err; |
Stephen Hemminger | 3ff50b7 | 2007-04-20 17:09:22 -0700 | [diff] [blame] | 4341 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4342 | |
| 4343 | return -EINVAL; |
| 4344 | } |
| 4345 | |
| 4346 | /* |
| 4347 | * Drop the packet on the floor |
| 4348 | */ |
| 4349 | |
Brian Haley | d5fdd6b | 2009-06-23 04:31:07 -0700 | [diff] [blame] | 4350 | 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] | 4351 | { |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 4352 | struct dst_entry *dst = skb_dst(skb); |
Stephen Suryaputra | 1d3fd8a | 2019-04-27 09:14:33 -0400 | [diff] [blame] | 4353 | struct net *net = dev_net(dst->dev); |
| 4354 | struct inet6_dev *idev; |
| 4355 | int type; |
| 4356 | |
| 4357 | if (netif_is_l3_master(skb->dev) && |
| 4358 | dst->dev == net->loopback_dev) |
| 4359 | idev = __in6_dev_get_safely(dev_get_by_index_rcu(net, IP6CB(skb)->iif)); |
| 4360 | else |
| 4361 | idev = ip6_dst_idev(dst); |
| 4362 | |
YOSHIFUJI Hideaki | 612f09e | 2007-04-13 16:18:02 -0700 | [diff] [blame] | 4363 | switch (ipstats_mib_noroutes) { |
| 4364 | case IPSTATS_MIB_INNOROUTES: |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 4365 | type = ipv6_addr_type(&ipv6_hdr(skb)->daddr); |
Ulrich Weber | 45bb006 | 2010-02-25 23:28:58 +0000 | [diff] [blame] | 4366 | if (type == IPV6_ADDR_ANY) { |
Stephen Suryaputra | 1d3fd8a | 2019-04-27 09:14:33 -0400 | [diff] [blame] | 4367 | IP6_INC_STATS(net, idev, IPSTATS_MIB_INADDRERRORS); |
YOSHIFUJI Hideaki | 612f09e | 2007-04-13 16:18:02 -0700 | [diff] [blame] | 4368 | break; |
| 4369 | } |
Joe Perches | a8eceea | 2020-03-12 15:50:22 -0700 | [diff] [blame] | 4370 | fallthrough; |
YOSHIFUJI Hideaki | 612f09e | 2007-04-13 16:18:02 -0700 | [diff] [blame] | 4371 | case IPSTATS_MIB_OUTNOROUTES: |
Stephen Suryaputra | 1d3fd8a | 2019-04-27 09:14:33 -0400 | [diff] [blame] | 4372 | IP6_INC_STATS(net, idev, ipstats_mib_noroutes); |
YOSHIFUJI Hideaki | 612f09e | 2007-04-13 16:18:02 -0700 | [diff] [blame] | 4373 | break; |
| 4374 | } |
Stephen Suryaputra | 1d3fd8a | 2019-04-27 09:14:33 -0400 | [diff] [blame] | 4375 | |
| 4376 | /* Start over by dropping the dst for l3mdev case */ |
| 4377 | if (netif_is_l3_master(skb->dev)) |
| 4378 | skb_dst_drop(skb); |
| 4379 | |
Alexey Dobriyan | 3ffe533 | 2010-02-18 08:25:24 +0000 | [diff] [blame] | 4380 | icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4381 | kfree_skb(skb); |
| 4382 | return 0; |
| 4383 | } |
| 4384 | |
Thomas Graf | 9ce8ade | 2006-10-18 20:46:54 -0700 | [diff] [blame] | 4385 | static int ip6_pkt_discard(struct sk_buff *skb) |
| 4386 | { |
YOSHIFUJI Hideaki | 612f09e | 2007-04-13 16:18:02 -0700 | [diff] [blame] | 4387 | return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES); |
Thomas Graf | 9ce8ade | 2006-10-18 20:46:54 -0700 | [diff] [blame] | 4388 | } |
| 4389 | |
Eric W. Biederman | ede2059 | 2015-10-07 16:48:47 -0500 | [diff] [blame] | 4390 | 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] | 4391 | { |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 4392 | skb->dev = skb_dst(skb)->dev; |
YOSHIFUJI Hideaki | 612f09e | 2007-04-13 16:18:02 -0700 | [diff] [blame] | 4393 | return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4394 | } |
| 4395 | |
Thomas Graf | 9ce8ade | 2006-10-18 20:46:54 -0700 | [diff] [blame] | 4396 | static int ip6_pkt_prohibit(struct sk_buff *skb) |
| 4397 | { |
YOSHIFUJI Hideaki | 612f09e | 2007-04-13 16:18:02 -0700 | [diff] [blame] | 4398 | return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES); |
Thomas Graf | 9ce8ade | 2006-10-18 20:46:54 -0700 | [diff] [blame] | 4399 | } |
| 4400 | |
Eric W. Biederman | ede2059 | 2015-10-07 16:48:47 -0500 | [diff] [blame] | 4401 | 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] | 4402 | { |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 4403 | skb->dev = skb_dst(skb)->dev; |
YOSHIFUJI Hideaki | 612f09e | 2007-04-13 16:18:02 -0700 | [diff] [blame] | 4404 | return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES); |
Thomas Graf | 9ce8ade | 2006-10-18 20:46:54 -0700 | [diff] [blame] | 4405 | } |
| 4406 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4407 | /* |
| 4408 | * Allocate a dst for local (unicast / anycast) address. |
| 4409 | */ |
| 4410 | |
David Ahern | 360a988 | 2018-04-18 15:39:00 -0700 | [diff] [blame] | 4411 | struct fib6_info *addrconf_f6i_alloc(struct net *net, |
| 4412 | struct inet6_dev *idev, |
| 4413 | const struct in6_addr *addr, |
| 4414 | bool anycast, gfp_t gfp_flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4415 | { |
David Ahern | c7a1ce3 | 2019-03-21 05:21:35 -0700 | [diff] [blame] | 4416 | struct fib6_config cfg = { |
| 4417 | .fc_table = l3mdev_fib_table(idev->dev) ? : RT6_TABLE_LOCAL, |
| 4418 | .fc_ifindex = idev->dev->ifindex, |
Maciej Żenczykowski | d55a2e3 | 2019-09-02 09:23:36 -0700 | [diff] [blame] | 4419 | .fc_flags = RTF_UP | RTF_NONEXTHOP, |
David Ahern | c7a1ce3 | 2019-03-21 05:21:35 -0700 | [diff] [blame] | 4420 | .fc_dst = *addr, |
| 4421 | .fc_dst_len = 128, |
| 4422 | .fc_protocol = RTPROT_KERNEL, |
| 4423 | .fc_nlinfo.nl_net = net, |
| 4424 | .fc_ignore_dev_down = true, |
| 4425 | }; |
Maciej Żenczykowski | d55a2e3 | 2019-09-02 09:23:36 -0700 | [diff] [blame] | 4426 | struct fib6_info *f6i; |
David Ahern | 5f02ce24 | 2016-09-10 12:09:54 -0700 | [diff] [blame] | 4427 | |
David Ahern | e8478e8 | 2018-04-17 17:33:13 -0700 | [diff] [blame] | 4428 | if (anycast) { |
David Ahern | c7a1ce3 | 2019-03-21 05:21:35 -0700 | [diff] [blame] | 4429 | cfg.fc_type = RTN_ANYCAST; |
| 4430 | cfg.fc_flags |= RTF_ANYCAST; |
David Ahern | e8478e8 | 2018-04-17 17:33:13 -0700 | [diff] [blame] | 4431 | } else { |
David Ahern | c7a1ce3 | 2019-03-21 05:21:35 -0700 | [diff] [blame] | 4432 | cfg.fc_type = RTN_LOCAL; |
| 4433 | cfg.fc_flags |= RTF_LOCAL; |
David Ahern | e8478e8 | 2018-04-17 17:33:13 -0700 | [diff] [blame] | 4434 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4435 | |
Maciej Żenczykowski | d55a2e3 | 2019-09-02 09:23:36 -0700 | [diff] [blame] | 4436 | f6i = ip6_route_info_create(&cfg, gfp_flags, NULL); |
Maciej Żenczykowski | 8652f17 | 2019-09-05 20:56:37 -0700 | [diff] [blame] | 4437 | if (!IS_ERR(f6i)) |
Maciej Żenczykowski | d55a2e3 | 2019-09-02 09:23:36 -0700 | [diff] [blame] | 4438 | f6i->dst_nocount = true; |
| 4439 | return f6i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4440 | } |
| 4441 | |
Daniel Walter | c3968a8 | 2011-04-13 21:10:57 +0000 | [diff] [blame] | 4442 | /* remove deleted ip from prefsrc entries */ |
| 4443 | struct arg_dev_net_ip { |
| 4444 | struct net_device *dev; |
| 4445 | struct net *net; |
| 4446 | struct in6_addr *addr; |
| 4447 | }; |
| 4448 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4449 | static int fib6_remove_prefsrc(struct fib6_info *rt, void *arg) |
Daniel Walter | c3968a8 | 2011-04-13 21:10:57 +0000 | [diff] [blame] | 4450 | { |
| 4451 | struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev; |
| 4452 | struct net *net = ((struct arg_dev_net_ip *)arg)->net; |
| 4453 | struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr; |
| 4454 | |
David Ahern | f88d8ea | 2019-06-03 20:19:52 -0700 | [diff] [blame] | 4455 | if (!rt->nh && |
| 4456 | ((void *)rt->fib6_nh->fib_nh_dev == dev || !dev) && |
David Ahern | 421842e | 2018-04-17 17:33:18 -0700 | [diff] [blame] | 4457 | rt != net->ipv6.fib6_null_entry && |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 4458 | ipv6_addr_equal(addr, &rt->fib6_prefsrc.addr)) { |
Wei Wang | 60006a4 | 2017-10-06 12:05:58 -0700 | [diff] [blame] | 4459 | spin_lock_bh(&rt6_exception_lock); |
Daniel Walter | c3968a8 | 2011-04-13 21:10:57 +0000 | [diff] [blame] | 4460 | /* remove prefsrc entry */ |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 4461 | rt->fib6_prefsrc.plen = 0; |
Wei Wang | 60006a4 | 2017-10-06 12:05:58 -0700 | [diff] [blame] | 4462 | spin_unlock_bh(&rt6_exception_lock); |
Daniel Walter | c3968a8 | 2011-04-13 21:10:57 +0000 | [diff] [blame] | 4463 | } |
| 4464 | return 0; |
| 4465 | } |
| 4466 | |
| 4467 | void rt6_remove_prefsrc(struct inet6_ifaddr *ifp) |
| 4468 | { |
| 4469 | struct net *net = dev_net(ifp->idev->dev); |
| 4470 | struct arg_dev_net_ip adni = { |
| 4471 | .dev = ifp->idev->dev, |
| 4472 | .net = net, |
| 4473 | .addr = &ifp->addr, |
| 4474 | }; |
Li RongQing | 0c3584d | 2013-12-27 16:32:38 +0800 | [diff] [blame] | 4475 | fib6_clean_all(net, fib6_remove_prefsrc, &adni); |
Daniel Walter | c3968a8 | 2011-04-13 21:10:57 +0000 | [diff] [blame] | 4476 | } |
| 4477 | |
David Ahern | 2b2450c | 2019-03-27 20:53:52 -0700 | [diff] [blame] | 4478 | #define RTF_RA_ROUTER (RTF_ADDRCONF | RTF_DEFAULT) |
Duan Jiong | be7a010 | 2014-05-15 15:56:14 +0800 | [diff] [blame] | 4479 | |
| 4480 | /* Remove routers and update dst entries when gateway turn into host. */ |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4481 | static int fib6_clean_tohost(struct fib6_info *rt, void *arg) |
Duan Jiong | be7a010 | 2014-05-15 15:56:14 +0800 | [diff] [blame] | 4482 | { |
| 4483 | struct in6_addr *gateway = (struct in6_addr *)arg; |
David Ahern | f88d8ea | 2019-06-03 20:19:52 -0700 | [diff] [blame] | 4484 | struct fib6_nh *nh; |
Duan Jiong | be7a010 | 2014-05-15 15:56:14 +0800 | [diff] [blame] | 4485 | |
David Ahern | f88d8ea | 2019-06-03 20:19:52 -0700 | [diff] [blame] | 4486 | /* RA routes do not use nexthops */ |
| 4487 | if (rt->nh) |
| 4488 | return 0; |
| 4489 | |
| 4490 | nh = rt->fib6_nh; |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 4491 | if (((rt->fib6_flags & RTF_RA_ROUTER) == RTF_RA_ROUTER) && |
David Ahern | cc5c073 | 2019-05-22 20:27:58 -0700 | [diff] [blame] | 4492 | 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] | 4493 | return -1; |
Wei Wang | b16cb45 | 2017-10-06 12:06:00 -0700 | [diff] [blame] | 4494 | |
| 4495 | /* Further clean up cached routes in exception table. |
| 4496 | * This is needed because cached route may have a different |
| 4497 | * gateway than its 'parent' in the case of an ip redirect. |
| 4498 | */ |
David Ahern | cc5c073 | 2019-05-22 20:27:58 -0700 | [diff] [blame] | 4499 | fib6_nh_exceptions_clean_tohost(nh, gateway); |
Wei Wang | b16cb45 | 2017-10-06 12:06:00 -0700 | [diff] [blame] | 4500 | |
Duan Jiong | be7a010 | 2014-05-15 15:56:14 +0800 | [diff] [blame] | 4501 | return 0; |
| 4502 | } |
| 4503 | |
| 4504 | void rt6_clean_tohost(struct net *net, struct in6_addr *gateway) |
| 4505 | { |
| 4506 | fib6_clean_all(net, fib6_clean_tohost, gateway); |
| 4507 | } |
| 4508 | |
Ido Schimmel | 2127d95 | 2018-01-07 12:45:03 +0200 | [diff] [blame] | 4509 | struct arg_netdev_event { |
| 4510 | const struct net_device *dev; |
Ido Schimmel | 4c981e2 | 2018-01-07 12:45:04 +0200 | [diff] [blame] | 4511 | union { |
David Ahern | ecc5663 | 2019-04-23 08:48:09 -0700 | [diff] [blame] | 4512 | unsigned char nh_flags; |
Ido Schimmel | 4c981e2 | 2018-01-07 12:45:04 +0200 | [diff] [blame] | 4513 | unsigned long event; |
| 4514 | }; |
Ido Schimmel | 2127d95 | 2018-01-07 12:45:03 +0200 | [diff] [blame] | 4515 | }; |
| 4516 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4517 | 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] | 4518 | { |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4519 | struct fib6_info *iter; |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 4520 | struct fib6_node *fn; |
| 4521 | |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 4522 | fn = rcu_dereference_protected(rt->fib6_node, |
| 4523 | lockdep_is_held(&rt->fib6_table->tb6_lock)); |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 4524 | iter = rcu_dereference_protected(fn->leaf, |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 4525 | lockdep_is_held(&rt->fib6_table->tb6_lock)); |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 4526 | while (iter) { |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 4527 | if (iter->fib6_metric == rt->fib6_metric && |
David Ahern | 33bd5ac | 2018-07-03 14:36:21 -0700 | [diff] [blame] | 4528 | rt6_qualify_for_ecmp(iter)) |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 4529 | return iter; |
David Ahern | 8fb11a9 | 2018-05-04 13:54:24 -0700 | [diff] [blame] | 4530 | iter = rcu_dereference_protected(iter->fib6_next, |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 4531 | lockdep_is_held(&rt->fib6_table->tb6_lock)); |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 4532 | } |
| 4533 | |
| 4534 | return NULL; |
| 4535 | } |
| 4536 | |
David Ahern | f88d8ea | 2019-06-03 20:19:52 -0700 | [diff] [blame] | 4537 | /* only called for fib entries with builtin fib6_nh */ |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4538 | static bool rt6_is_dead(const struct fib6_info *rt) |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 4539 | { |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 4540 | if (rt->fib6_nh->fib_nh_flags & RTNH_F_DEAD || |
| 4541 | (rt->fib6_nh->fib_nh_flags & RTNH_F_LINKDOWN && |
| 4542 | ip6_ignore_linkdown(rt->fib6_nh->fib_nh_dev))) |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 4543 | return true; |
| 4544 | |
| 4545 | return false; |
| 4546 | } |
| 4547 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4548 | static int rt6_multipath_total_weight(const struct fib6_info *rt) |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 4549 | { |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4550 | struct fib6_info *iter; |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 4551 | int total = 0; |
| 4552 | |
| 4553 | if (!rt6_is_dead(rt)) |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 4554 | total += rt->fib6_nh->fib_nh_weight; |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 4555 | |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 4556 | list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) { |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 4557 | if (!rt6_is_dead(iter)) |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 4558 | total += iter->fib6_nh->fib_nh_weight; |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 4559 | } |
| 4560 | |
| 4561 | return total; |
| 4562 | } |
| 4563 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4564 | 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] | 4565 | { |
| 4566 | int upper_bound = -1; |
| 4567 | |
| 4568 | if (!rt6_is_dead(rt)) { |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 4569 | *weight += rt->fib6_nh->fib_nh_weight; |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 4570 | upper_bound = DIV_ROUND_CLOSEST_ULL((u64) (*weight) << 31, |
| 4571 | total) - 1; |
| 4572 | } |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 4573 | atomic_set(&rt->fib6_nh->fib_nh_upper_bound, upper_bound); |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 4574 | } |
| 4575 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4576 | 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] | 4577 | { |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4578 | struct fib6_info *iter; |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 4579 | int weight = 0; |
| 4580 | |
| 4581 | rt6_upper_bound_set(rt, &weight, total); |
| 4582 | |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 4583 | list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 4584 | rt6_upper_bound_set(iter, &weight, total); |
| 4585 | } |
| 4586 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4587 | void rt6_multipath_rebalance(struct fib6_info *rt) |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 4588 | { |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4589 | struct fib6_info *first; |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 4590 | int total; |
| 4591 | |
| 4592 | /* In case the entire multipath route was marked for flushing, |
| 4593 | * then there is no need to rebalance upon the removal of every |
| 4594 | * sibling route. |
| 4595 | */ |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 4596 | if (!rt->fib6_nsiblings || rt->should_flush) |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 4597 | return; |
| 4598 | |
| 4599 | /* During lookup routes are evaluated in order, so we need to |
| 4600 | * make sure upper bounds are assigned from the first sibling |
| 4601 | * onwards. |
| 4602 | */ |
| 4603 | first = rt6_multipath_first_sibling(rt); |
| 4604 | if (WARN_ON_ONCE(!first)) |
| 4605 | return; |
| 4606 | |
| 4607 | total = rt6_multipath_total_weight(first); |
| 4608 | rt6_multipath_upper_bound_set(first, total); |
| 4609 | } |
| 4610 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4611 | static int fib6_ifup(struct fib6_info *rt, void *p_arg) |
Ido Schimmel | 2127d95 | 2018-01-07 12:45:03 +0200 | [diff] [blame] | 4612 | { |
| 4613 | const struct arg_netdev_event *arg = p_arg; |
David Ahern | 7aef685 | 2018-04-17 17:33:10 -0700 | [diff] [blame] | 4614 | struct net *net = dev_net(arg->dev); |
Ido Schimmel | 2127d95 | 2018-01-07 12:45:03 +0200 | [diff] [blame] | 4615 | |
David Ahern | f88d8ea | 2019-06-03 20:19:52 -0700 | [diff] [blame] | 4616 | if (rt != net->ipv6.fib6_null_entry && !rt->nh && |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 4617 | rt->fib6_nh->fib_nh_dev == arg->dev) { |
| 4618 | rt->fib6_nh->fib_nh_flags &= ~arg->nh_flags; |
David Ahern | 7aef685 | 2018-04-17 17:33:10 -0700 | [diff] [blame] | 4619 | fib6_update_sernum_upto_root(net, rt); |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 4620 | rt6_multipath_rebalance(rt); |
Ido Schimmel | 1de178e | 2018-01-07 12:45:15 +0200 | [diff] [blame] | 4621 | } |
Ido Schimmel | 2127d95 | 2018-01-07 12:45:03 +0200 | [diff] [blame] | 4622 | |
| 4623 | return 0; |
| 4624 | } |
| 4625 | |
David Ahern | ecc5663 | 2019-04-23 08:48:09 -0700 | [diff] [blame] | 4626 | void rt6_sync_up(struct net_device *dev, unsigned char nh_flags) |
Ido Schimmel | 2127d95 | 2018-01-07 12:45:03 +0200 | [diff] [blame] | 4627 | { |
| 4628 | struct arg_netdev_event arg = { |
| 4629 | .dev = dev, |
Ido Schimmel | 6802f3a | 2018-01-12 22:07:36 +0200 | [diff] [blame] | 4630 | { |
| 4631 | .nh_flags = nh_flags, |
| 4632 | }, |
Ido Schimmel | 2127d95 | 2018-01-07 12:45:03 +0200 | [diff] [blame] | 4633 | }; |
| 4634 | |
| 4635 | if (nh_flags & RTNH_F_DEAD && netif_carrier_ok(dev)) |
| 4636 | arg.nh_flags |= RTNH_F_LINKDOWN; |
| 4637 | |
| 4638 | fib6_clean_all(dev_net(dev), fib6_ifup, &arg); |
| 4639 | } |
| 4640 | |
David Ahern | f88d8ea | 2019-06-03 20:19:52 -0700 | [diff] [blame] | 4641 | /* only called for fib entries with inline fib6_nh */ |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4642 | static bool rt6_multipath_uses_dev(const struct fib6_info *rt, |
Ido Schimmel | 1de178e | 2018-01-07 12:45:15 +0200 | [diff] [blame] | 4643 | const struct net_device *dev) |
| 4644 | { |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4645 | struct fib6_info *iter; |
Ido Schimmel | 1de178e | 2018-01-07 12:45:15 +0200 | [diff] [blame] | 4646 | |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 4647 | if (rt->fib6_nh->fib_nh_dev == dev) |
Ido Schimmel | 1de178e | 2018-01-07 12:45:15 +0200 | [diff] [blame] | 4648 | return true; |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 4649 | list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 4650 | if (iter->fib6_nh->fib_nh_dev == dev) |
Ido Schimmel | 1de178e | 2018-01-07 12:45:15 +0200 | [diff] [blame] | 4651 | return true; |
| 4652 | |
| 4653 | return false; |
| 4654 | } |
| 4655 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4656 | static void rt6_multipath_flush(struct fib6_info *rt) |
Ido Schimmel | 1de178e | 2018-01-07 12:45:15 +0200 | [diff] [blame] | 4657 | { |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4658 | struct fib6_info *iter; |
Ido Schimmel | 1de178e | 2018-01-07 12:45:15 +0200 | [diff] [blame] | 4659 | |
| 4660 | rt->should_flush = 1; |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 4661 | list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) |
Ido Schimmel | 1de178e | 2018-01-07 12:45:15 +0200 | [diff] [blame] | 4662 | iter->should_flush = 1; |
| 4663 | } |
| 4664 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4665 | static unsigned int rt6_multipath_dead_count(const struct fib6_info *rt, |
Ido Schimmel | 1de178e | 2018-01-07 12:45:15 +0200 | [diff] [blame] | 4666 | const struct net_device *down_dev) |
| 4667 | { |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4668 | struct fib6_info *iter; |
Ido Schimmel | 1de178e | 2018-01-07 12:45:15 +0200 | [diff] [blame] | 4669 | unsigned int dead = 0; |
| 4670 | |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 4671 | if (rt->fib6_nh->fib_nh_dev == down_dev || |
| 4672 | rt->fib6_nh->fib_nh_flags & RTNH_F_DEAD) |
Ido Schimmel | 1de178e | 2018-01-07 12:45:15 +0200 | [diff] [blame] | 4673 | dead++; |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 4674 | list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 4675 | if (iter->fib6_nh->fib_nh_dev == down_dev || |
| 4676 | iter->fib6_nh->fib_nh_flags & RTNH_F_DEAD) |
Ido Schimmel | 1de178e | 2018-01-07 12:45:15 +0200 | [diff] [blame] | 4677 | dead++; |
| 4678 | |
| 4679 | return dead; |
| 4680 | } |
| 4681 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4682 | static void rt6_multipath_nh_flags_set(struct fib6_info *rt, |
Ido Schimmel | 1de178e | 2018-01-07 12:45:15 +0200 | [diff] [blame] | 4683 | const struct net_device *dev, |
David Ahern | ecc5663 | 2019-04-23 08:48:09 -0700 | [diff] [blame] | 4684 | unsigned char nh_flags) |
Ido Schimmel | 1de178e | 2018-01-07 12:45:15 +0200 | [diff] [blame] | 4685 | { |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4686 | struct fib6_info *iter; |
Ido Schimmel | 1de178e | 2018-01-07 12:45:15 +0200 | [diff] [blame] | 4687 | |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 4688 | if (rt->fib6_nh->fib_nh_dev == dev) |
| 4689 | rt->fib6_nh->fib_nh_flags |= nh_flags; |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 4690 | list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 4691 | if (iter->fib6_nh->fib_nh_dev == dev) |
| 4692 | iter->fib6_nh->fib_nh_flags |= nh_flags; |
Ido Schimmel | 1de178e | 2018-01-07 12:45:15 +0200 | [diff] [blame] | 4693 | } |
| 4694 | |
David Ahern | a1a22c1 | 2017-01-18 07:40:36 -0800 | [diff] [blame] | 4695 | /* called with write lock held for table with rt */ |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4696 | static int fib6_ifdown(struct fib6_info *rt, void *p_arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4697 | { |
Ido Schimmel | 4c981e2 | 2018-01-07 12:45:04 +0200 | [diff] [blame] | 4698 | const struct arg_netdev_event *arg = p_arg; |
| 4699 | const struct net_device *dev = arg->dev; |
David Ahern | 7aef685 | 2018-04-17 17:33:10 -0700 | [diff] [blame] | 4700 | struct net *net = dev_net(dev); |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 4701 | |
David Ahern | f88d8ea | 2019-06-03 20:19:52 -0700 | [diff] [blame] | 4702 | if (rt == net->ipv6.fib6_null_entry || rt->nh) |
Ido Schimmel | 27c6fa7 | 2018-01-07 12:45:05 +0200 | [diff] [blame] | 4703 | return 0; |
| 4704 | |
| 4705 | switch (arg->event) { |
| 4706 | case NETDEV_UNREGISTER: |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 4707 | return rt->fib6_nh->fib_nh_dev == dev ? -1 : 0; |
Ido Schimmel | 27c6fa7 | 2018-01-07 12:45:05 +0200 | [diff] [blame] | 4708 | case NETDEV_DOWN: |
Ido Schimmel | 1de178e | 2018-01-07 12:45:15 +0200 | [diff] [blame] | 4709 | if (rt->should_flush) |
Ido Schimmel | 27c6fa7 | 2018-01-07 12:45:05 +0200 | [diff] [blame] | 4710 | return -1; |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 4711 | if (!rt->fib6_nsiblings) |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 4712 | return rt->fib6_nh->fib_nh_dev == dev ? -1 : 0; |
Ido Schimmel | 1de178e | 2018-01-07 12:45:15 +0200 | [diff] [blame] | 4713 | if (rt6_multipath_uses_dev(rt, dev)) { |
| 4714 | unsigned int count; |
| 4715 | |
| 4716 | count = rt6_multipath_dead_count(rt, dev); |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 4717 | if (rt->fib6_nsiblings + 1 == count) { |
Ido Schimmel | 1de178e | 2018-01-07 12:45:15 +0200 | [diff] [blame] | 4718 | rt6_multipath_flush(rt); |
| 4719 | return -1; |
| 4720 | } |
| 4721 | rt6_multipath_nh_flags_set(rt, dev, RTNH_F_DEAD | |
| 4722 | RTNH_F_LINKDOWN); |
David Ahern | 7aef685 | 2018-04-17 17:33:10 -0700 | [diff] [blame] | 4723 | fib6_update_sernum(net, rt); |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 4724 | rt6_multipath_rebalance(rt); |
Ido Schimmel | 1de178e | 2018-01-07 12:45:15 +0200 | [diff] [blame] | 4725 | } |
| 4726 | return -2; |
Ido Schimmel | 27c6fa7 | 2018-01-07 12:45:05 +0200 | [diff] [blame] | 4727 | case NETDEV_CHANGE: |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 4728 | if (rt->fib6_nh->fib_nh_dev != dev || |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 4729 | rt->fib6_flags & (RTF_LOCAL | RTF_ANYCAST)) |
Ido Schimmel | 27c6fa7 | 2018-01-07 12:45:05 +0200 | [diff] [blame] | 4730 | break; |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 4731 | rt->fib6_nh->fib_nh_flags |= RTNH_F_LINKDOWN; |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 4732 | rt6_multipath_rebalance(rt); |
Ido Schimmel | 27c6fa7 | 2018-01-07 12:45:05 +0200 | [diff] [blame] | 4733 | break; |
Ido Schimmel | 2b24136 | 2018-01-07 12:45:02 +0200 | [diff] [blame] | 4734 | } |
David S. Miller | c159d30 | 2011-12-26 15:24:36 -0500 | [diff] [blame] | 4735 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4736 | return 0; |
| 4737 | } |
| 4738 | |
Ido Schimmel | 27c6fa7 | 2018-01-07 12:45:05 +0200 | [diff] [blame] | 4739 | void rt6_sync_down_dev(struct net_device *dev, unsigned long event) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4740 | { |
Ido Schimmel | 4c981e2 | 2018-01-07 12:45:04 +0200 | [diff] [blame] | 4741 | struct arg_netdev_event arg = { |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 4742 | .dev = dev, |
Ido Schimmel | 6802f3a | 2018-01-12 22:07:36 +0200 | [diff] [blame] | 4743 | { |
| 4744 | .event = event, |
| 4745 | }, |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 4746 | }; |
David Ahern | 7c6bb7d | 2018-10-11 20:17:21 -0700 | [diff] [blame] | 4747 | struct net *net = dev_net(dev); |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 4748 | |
David Ahern | 7c6bb7d | 2018-10-11 20:17:21 -0700 | [diff] [blame] | 4749 | if (net->ipv6.sysctl.skip_notify_on_dev_down) |
| 4750 | fib6_clean_all_skip_notify(net, fib6_ifdown, &arg); |
| 4751 | else |
| 4752 | fib6_clean_all(net, fib6_ifdown, &arg); |
Ido Schimmel | 4c981e2 | 2018-01-07 12:45:04 +0200 | [diff] [blame] | 4753 | } |
| 4754 | |
| 4755 | void rt6_disable_ip(struct net_device *dev, unsigned long event) |
| 4756 | { |
| 4757 | rt6_sync_down_dev(dev, event); |
| 4758 | rt6_uncached_list_flush_dev(dev_net(dev), dev); |
| 4759 | neigh_ifdown(&nd_tbl, dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4760 | } |
| 4761 | |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 4762 | struct rt6_mtu_change_arg { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4763 | struct net_device *dev; |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 4764 | unsigned int mtu; |
David Ahern | c0b220c | 2019-05-22 20:27:57 -0700 | [diff] [blame] | 4765 | struct fib6_info *f6i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4766 | }; |
| 4767 | |
David Ahern | cc5c073 | 2019-05-22 20:27:58 -0700 | [diff] [blame] | 4768 | static int fib6_nh_mtu_change(struct fib6_nh *nh, void *_arg) |
David Ahern | c0b220c | 2019-05-22 20:27:57 -0700 | [diff] [blame] | 4769 | { |
| 4770 | struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *)_arg; |
David Ahern | cc5c073 | 2019-05-22 20:27:58 -0700 | [diff] [blame] | 4771 | struct fib6_info *f6i = arg->f6i; |
David Ahern | c0b220c | 2019-05-22 20:27:57 -0700 | [diff] [blame] | 4772 | |
| 4773 | /* For administrative MTU increase, there is no way to discover |
| 4774 | * IPv6 PMTU increase, so PMTU increase should be updated here. |
| 4775 | * Since RFC 1981 doesn't include administrative MTU increase |
| 4776 | * update PMTU increase is a MUST. (i.e. jumbo frame) |
| 4777 | */ |
| 4778 | if (nh->fib_nh_dev == arg->dev) { |
| 4779 | struct inet6_dev *idev = __in6_dev_get(arg->dev); |
| 4780 | u32 mtu = f6i->fib6_pmtu; |
| 4781 | |
| 4782 | if (mtu >= arg->mtu || |
| 4783 | (mtu < arg->mtu && mtu == idev->cnf.mtu6)) |
| 4784 | fib6_metric_set(f6i, RTAX_MTU, arg->mtu); |
| 4785 | |
| 4786 | spin_lock_bh(&rt6_exception_lock); |
David Ahern | cc5c073 | 2019-05-22 20:27:58 -0700 | [diff] [blame] | 4787 | rt6_exceptions_update_pmtu(idev, nh, arg->mtu); |
David Ahern | c0b220c | 2019-05-22 20:27:57 -0700 | [diff] [blame] | 4788 | spin_unlock_bh(&rt6_exception_lock); |
| 4789 | } |
| 4790 | |
| 4791 | return 0; |
| 4792 | } |
| 4793 | |
| 4794 | 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] | 4795 | { |
| 4796 | struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg; |
| 4797 | struct inet6_dev *idev; |
| 4798 | |
| 4799 | /* In IPv6 pmtu discovery is not optional, |
| 4800 | so that RTAX_MTU lock cannot disable it. |
| 4801 | We still use this lock to block changes |
| 4802 | caused by addrconf/ndisc. |
| 4803 | */ |
| 4804 | |
| 4805 | idev = __in6_dev_get(arg->dev); |
David S. Miller | 3830847 | 2011-12-03 18:02:47 -0500 | [diff] [blame] | 4806 | if (!idev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4807 | return 0; |
| 4808 | |
David Ahern | c0b220c | 2019-05-22 20:27:57 -0700 | [diff] [blame] | 4809 | if (fib6_metric_locked(f6i, RTAX_MTU)) |
| 4810 | return 0; |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 4811 | |
David Ahern | c0b220c | 2019-05-22 20:27:57 -0700 | [diff] [blame] | 4812 | arg->f6i = f6i; |
David Ahern | 2d44234 | 2019-06-08 14:53:31 -0700 | [diff] [blame] | 4813 | if (f6i->nh) { |
| 4814 | /* fib6_nh_mtu_change only returns 0, so this is safe */ |
| 4815 | return nexthop_for_each_fib6_nh(f6i->nh, fib6_nh_mtu_change, |
| 4816 | arg); |
| 4817 | } |
| 4818 | |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 4819 | return fib6_nh_mtu_change(f6i->fib6_nh, arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4820 | } |
| 4821 | |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 4822 | void rt6_mtu_change(struct net_device *dev, unsigned int mtu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4823 | { |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 4824 | struct rt6_mtu_change_arg arg = { |
| 4825 | .dev = dev, |
| 4826 | .mtu = mtu, |
| 4827 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4828 | |
Li RongQing | 0c3584d | 2013-12-27 16:32:38 +0800 | [diff] [blame] | 4829 | fib6_clean_all(dev_net(dev), rt6_mtu_change_route, &arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4830 | } |
| 4831 | |
Patrick McHardy | ef7c79e | 2007-06-05 12:38:30 -0700 | [diff] [blame] | 4832 | static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = { |
David Ahern | 7542565 | 2019-05-22 12:07:43 -0700 | [diff] [blame] | 4833 | [RTA_UNSPEC] = { .strict_start_type = RTA_DPORT + 1 }, |
Thomas Graf | 5176f91 | 2006-08-26 20:13:18 -0700 | [diff] [blame] | 4834 | [RTA_GATEWAY] = { .len = sizeof(struct in6_addr) }, |
Eric Dumazet | aa8f877 | 2018-04-22 18:29:23 -0700 | [diff] [blame] | 4835 | [RTA_PREFSRC] = { .len = sizeof(struct in6_addr) }, |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4836 | [RTA_OIF] = { .type = NLA_U32 }, |
Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 4837 | [RTA_IIF] = { .type = NLA_U32 }, |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4838 | [RTA_PRIORITY] = { .type = NLA_U32 }, |
| 4839 | [RTA_METRICS] = { .type = NLA_NESTED }, |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 4840 | [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) }, |
Lubomir Rintel | c78ba6d | 2015-03-11 15:39:21 +0100 | [diff] [blame] | 4841 | [RTA_PREF] = { .type = NLA_U8 }, |
Roopa Prabhu | 19e42e4 | 2015-07-21 10:43:48 +0200 | [diff] [blame] | 4842 | [RTA_ENCAP_TYPE] = { .type = NLA_U16 }, |
| 4843 | [RTA_ENCAP] = { .type = NLA_NESTED }, |
Xin Long | 32bc201 | 2015-12-16 17:50:11 +0800 | [diff] [blame] | 4844 | [RTA_EXPIRES] = { .type = NLA_U32 }, |
Lorenzo Colitti | 622ec2c | 2016-11-04 02:23:42 +0900 | [diff] [blame] | 4845 | [RTA_UID] = { .type = NLA_U32 }, |
Liping Zhang | 3b45a41 | 2017-02-27 20:59:39 +0800 | [diff] [blame] | 4846 | [RTA_MARK] = { .type = NLA_U32 }, |
Eric Dumazet | aa8f877 | 2018-04-22 18:29:23 -0700 | [diff] [blame] | 4847 | [RTA_TABLE] = { .type = NLA_U32 }, |
Roopa Prabhu | eacb938 | 2018-05-22 14:03:28 -0700 | [diff] [blame] | 4848 | [RTA_IP_PROTO] = { .type = NLA_U8 }, |
| 4849 | [RTA_SPORT] = { .type = NLA_U16 }, |
| 4850 | [RTA_DPORT] = { .type = NLA_U16 }, |
David Ahern | 5b98324 | 2019-06-08 14:53:34 -0700 | [diff] [blame] | 4851 | [RTA_NH_ID] = { .type = NLA_U32 }, |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4852 | }; |
| 4853 | |
| 4854 | 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] | 4855 | struct fib6_config *cfg, |
| 4856 | struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4857 | { |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4858 | struct rtmsg *rtm; |
| 4859 | struct nlattr *tb[RTA_MAX+1]; |
Lubomir Rintel | c78ba6d | 2015-03-11 15:39:21 +0100 | [diff] [blame] | 4860 | unsigned int pref; |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4861 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4862 | |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 4863 | err = nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX, |
| 4864 | rtm_ipv6_policy, extack); |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4865 | if (err < 0) |
| 4866 | goto errout; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4867 | |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4868 | err = -EINVAL; |
| 4869 | rtm = nlmsg_data(nlh); |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4870 | |
Maciej Żenczykowski | 84db840 | 2018-09-29 23:44:53 -0700 | [diff] [blame] | 4871 | *cfg = (struct fib6_config){ |
| 4872 | .fc_table = rtm->rtm_table, |
| 4873 | .fc_dst_len = rtm->rtm_dst_len, |
| 4874 | .fc_src_len = rtm->rtm_src_len, |
| 4875 | .fc_flags = RTF_UP, |
| 4876 | .fc_protocol = rtm->rtm_protocol, |
| 4877 | .fc_type = rtm->rtm_type, |
| 4878 | |
| 4879 | .fc_nlinfo.portid = NETLINK_CB(skb).portid, |
| 4880 | .fc_nlinfo.nlh = nlh, |
| 4881 | .fc_nlinfo.nl_net = sock_net(skb->sk), |
| 4882 | }; |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4883 | |
Nicolas Dichtel | ef2c7d7 | 2012-09-05 02:12:42 +0000 | [diff] [blame] | 4884 | if (rtm->rtm_type == RTN_UNREACHABLE || |
| 4885 | rtm->rtm_type == RTN_BLACKHOLE || |
Nicolas Dichtel | b4949ab | 2012-09-06 05:53:35 +0000 | [diff] [blame] | 4886 | rtm->rtm_type == RTN_PROHIBIT || |
| 4887 | rtm->rtm_type == RTN_THROW) |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4888 | cfg->fc_flags |= RTF_REJECT; |
| 4889 | |
Maciej Żenczykowski | ab79ad1 | 2010-09-27 00:07:02 +0000 | [diff] [blame] | 4890 | if (rtm->rtm_type == RTN_LOCAL) |
| 4891 | cfg->fc_flags |= RTF_LOCAL; |
| 4892 | |
Martin KaFai Lau | 1f56a01f | 2015-04-28 13:03:03 -0700 | [diff] [blame] | 4893 | if (rtm->rtm_flags & RTM_F_CLONED) |
| 4894 | cfg->fc_flags |= RTF_CACHE; |
| 4895 | |
David Ahern | fc1e64e | 2018-01-25 16:55:09 -0800 | [diff] [blame] | 4896 | cfg->fc_flags |= (rtm->rtm_flags & RTNH_F_ONLINK); |
| 4897 | |
David Ahern | 5b98324 | 2019-06-08 14:53:34 -0700 | [diff] [blame] | 4898 | if (tb[RTA_NH_ID]) { |
| 4899 | if (tb[RTA_GATEWAY] || tb[RTA_OIF] || |
| 4900 | tb[RTA_MULTIPATH] || tb[RTA_ENCAP]) { |
| 4901 | NL_SET_ERR_MSG(extack, |
| 4902 | "Nexthop specification and nexthop id are mutually exclusive"); |
| 4903 | goto errout; |
| 4904 | } |
| 4905 | cfg->fc_nh_id = nla_get_u32(tb[RTA_NH_ID]); |
| 4906 | } |
| 4907 | |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4908 | if (tb[RTA_GATEWAY]) { |
Jiri Benc | 67b61f6 | 2015-03-29 16:59:26 +0200 | [diff] [blame] | 4909 | cfg->fc_gateway = nla_get_in6_addr(tb[RTA_GATEWAY]); |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4910 | cfg->fc_flags |= RTF_GATEWAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4911 | } |
David Ahern | e381854 | 2019-02-26 09:00:03 -0800 | [diff] [blame] | 4912 | if (tb[RTA_VIA]) { |
| 4913 | NL_SET_ERR_MSG(extack, "IPv6 does not support RTA_VIA attribute"); |
| 4914 | goto errout; |
| 4915 | } |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4916 | |
| 4917 | if (tb[RTA_DST]) { |
| 4918 | int plen = (rtm->rtm_dst_len + 7) >> 3; |
| 4919 | |
| 4920 | if (nla_len(tb[RTA_DST]) < plen) |
| 4921 | goto errout; |
| 4922 | |
| 4923 | nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4924 | } |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4925 | |
| 4926 | if (tb[RTA_SRC]) { |
| 4927 | int plen = (rtm->rtm_src_len + 7) >> 3; |
| 4928 | |
| 4929 | if (nla_len(tb[RTA_SRC]) < plen) |
| 4930 | goto errout; |
| 4931 | |
| 4932 | nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4933 | } |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4934 | |
Daniel Walter | c3968a8 | 2011-04-13 21:10:57 +0000 | [diff] [blame] | 4935 | if (tb[RTA_PREFSRC]) |
Jiri Benc | 67b61f6 | 2015-03-29 16:59:26 +0200 | [diff] [blame] | 4936 | cfg->fc_prefsrc = nla_get_in6_addr(tb[RTA_PREFSRC]); |
Daniel Walter | c3968a8 | 2011-04-13 21:10:57 +0000 | [diff] [blame] | 4937 | |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4938 | if (tb[RTA_OIF]) |
| 4939 | cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]); |
| 4940 | |
| 4941 | if (tb[RTA_PRIORITY]) |
| 4942 | cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]); |
| 4943 | |
| 4944 | if (tb[RTA_METRICS]) { |
| 4945 | cfg->fc_mx = nla_data(tb[RTA_METRICS]); |
| 4946 | cfg->fc_mx_len = nla_len(tb[RTA_METRICS]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4947 | } |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4948 | |
| 4949 | if (tb[RTA_TABLE]) |
| 4950 | cfg->fc_table = nla_get_u32(tb[RTA_TABLE]); |
| 4951 | |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 4952 | if (tb[RTA_MULTIPATH]) { |
| 4953 | cfg->fc_mp = nla_data(tb[RTA_MULTIPATH]); |
| 4954 | cfg->fc_mp_len = nla_len(tb[RTA_MULTIPATH]); |
David Ahern | 9ed5959 | 2017-01-17 14:57:36 -0800 | [diff] [blame] | 4955 | |
| 4956 | err = lwtunnel_valid_encap_type_attr(cfg->fc_mp, |
David Ahern | c255bd6 | 2017-05-27 16:19:27 -0600 | [diff] [blame] | 4957 | cfg->fc_mp_len, extack); |
David Ahern | 9ed5959 | 2017-01-17 14:57:36 -0800 | [diff] [blame] | 4958 | if (err < 0) |
| 4959 | goto errout; |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 4960 | } |
| 4961 | |
Lubomir Rintel | c78ba6d | 2015-03-11 15:39:21 +0100 | [diff] [blame] | 4962 | if (tb[RTA_PREF]) { |
| 4963 | pref = nla_get_u8(tb[RTA_PREF]); |
| 4964 | if (pref != ICMPV6_ROUTER_PREF_LOW && |
| 4965 | pref != ICMPV6_ROUTER_PREF_HIGH) |
| 4966 | pref = ICMPV6_ROUTER_PREF_MEDIUM; |
| 4967 | cfg->fc_flags |= RTF_PREF(pref); |
| 4968 | } |
| 4969 | |
Roopa Prabhu | 19e42e4 | 2015-07-21 10:43:48 +0200 | [diff] [blame] | 4970 | if (tb[RTA_ENCAP]) |
| 4971 | cfg->fc_encap = tb[RTA_ENCAP]; |
| 4972 | |
David Ahern | 9ed5959 | 2017-01-17 14:57:36 -0800 | [diff] [blame] | 4973 | if (tb[RTA_ENCAP_TYPE]) { |
Roopa Prabhu | 19e42e4 | 2015-07-21 10:43:48 +0200 | [diff] [blame] | 4974 | cfg->fc_encap_type = nla_get_u16(tb[RTA_ENCAP_TYPE]); |
| 4975 | |
David Ahern | c255bd6 | 2017-05-27 16:19:27 -0600 | [diff] [blame] | 4976 | err = lwtunnel_valid_encap_type(cfg->fc_encap_type, extack); |
David Ahern | 9ed5959 | 2017-01-17 14:57:36 -0800 | [diff] [blame] | 4977 | if (err < 0) |
| 4978 | goto errout; |
| 4979 | } |
| 4980 | |
Xin Long | 32bc201 | 2015-12-16 17:50:11 +0800 | [diff] [blame] | 4981 | if (tb[RTA_EXPIRES]) { |
| 4982 | unsigned long timeout = addrconf_timeout_fixup(nla_get_u32(tb[RTA_EXPIRES]), HZ); |
| 4983 | |
| 4984 | if (addrconf_finite_timeout(timeout)) { |
| 4985 | cfg->fc_expires = jiffies_to_clock_t(timeout * HZ); |
| 4986 | cfg->fc_flags |= RTF_EXPIRES; |
| 4987 | } |
| 4988 | } |
| 4989 | |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4990 | err = 0; |
| 4991 | errout: |
| 4992 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4993 | } |
| 4994 | |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 4995 | struct rt6_nh { |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4996 | struct fib6_info *fib6_info; |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 4997 | struct fib6_config r_cfg; |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 4998 | struct list_head next; |
| 4999 | }; |
| 5000 | |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 5001 | static int ip6_route_info_append(struct net *net, |
| 5002 | struct list_head *rt6_nh_list, |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 5003 | struct fib6_info *rt, |
| 5004 | struct fib6_config *r_cfg) |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 5005 | { |
| 5006 | struct rt6_nh *nh; |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 5007 | int err = -EEXIST; |
| 5008 | |
| 5009 | list_for_each_entry(nh, rt6_nh_list, next) { |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 5010 | /* check if fib6_info already exists */ |
| 5011 | if (rt6_duplicate_nexthop(nh->fib6_info, rt)) |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 5012 | return err; |
| 5013 | } |
| 5014 | |
| 5015 | nh = kzalloc(sizeof(*nh), GFP_KERNEL); |
| 5016 | if (!nh) |
| 5017 | return -ENOMEM; |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 5018 | nh->fib6_info = rt; |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 5019 | memcpy(&nh->r_cfg, r_cfg, sizeof(*r_cfg)); |
| 5020 | list_add_tail(&nh->next, rt6_nh_list); |
| 5021 | |
| 5022 | return 0; |
| 5023 | } |
| 5024 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 5025 | static void ip6_route_mpath_notify(struct fib6_info *rt, |
| 5026 | struct fib6_info *rt_last, |
David Ahern | 3b1137f | 2017-02-02 12:37:10 -0800 | [diff] [blame] | 5027 | struct nl_info *info, |
| 5028 | __u16 nlflags) |
| 5029 | { |
| 5030 | /* if this is an APPEND route, then rt points to the first route |
| 5031 | * inserted and rt_last points to last route inserted. Userspace |
| 5032 | * wants a consistent dump of the route which starts at the first |
| 5033 | * nexthop. Since sibling routes are always added at the end of |
| 5034 | * the list, find the first sibling of the last route appended |
| 5035 | */ |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 5036 | if ((nlflags & NLM_F_APPEND) && rt_last && rt_last->fib6_nsiblings) { |
| 5037 | rt = list_first_entry(&rt_last->fib6_siblings, |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 5038 | struct fib6_info, |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 5039 | fib6_siblings); |
David Ahern | 3b1137f | 2017-02-02 12:37:10 -0800 | [diff] [blame] | 5040 | } |
| 5041 | |
| 5042 | if (rt) |
| 5043 | inet6_rt_notify(RTM_NEWROUTE, rt, info, nlflags); |
| 5044 | } |
| 5045 | |
Ido Schimmel | 0ee0f47 | 2019-12-23 15:28:15 +0200 | [diff] [blame] | 5046 | static bool ip6_route_mpath_should_notify(const struct fib6_info *rt) |
| 5047 | { |
| 5048 | bool rt_can_ecmp = rt6_qualify_for_ecmp(rt); |
| 5049 | bool should_notify = false; |
| 5050 | struct fib6_info *leaf; |
| 5051 | struct fib6_node *fn; |
| 5052 | |
| 5053 | rcu_read_lock(); |
| 5054 | fn = rcu_dereference(rt->fib6_node); |
| 5055 | if (!fn) |
| 5056 | goto out; |
| 5057 | |
| 5058 | leaf = rcu_dereference(fn->leaf); |
| 5059 | if (!leaf) |
| 5060 | goto out; |
| 5061 | |
| 5062 | if (rt == leaf || |
| 5063 | (rt_can_ecmp && rt->fib6_metric == leaf->fib6_metric && |
| 5064 | rt6_qualify_for_ecmp(leaf))) |
| 5065 | should_notify = true; |
| 5066 | out: |
| 5067 | rcu_read_unlock(); |
| 5068 | |
| 5069 | return should_notify; |
| 5070 | } |
| 5071 | |
David Ahern | 333c430 | 2017-05-21 10:12:04 -0600 | [diff] [blame] | 5072 | static int ip6_route_multipath_add(struct fib6_config *cfg, |
| 5073 | struct netlink_ext_ack *extack) |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 5074 | { |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 5075 | struct fib6_info *rt_notif = NULL, *rt_last = NULL; |
David Ahern | 3b1137f | 2017-02-02 12:37:10 -0800 | [diff] [blame] | 5076 | struct nl_info *info = &cfg->fc_nlinfo; |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 5077 | struct fib6_config r_cfg; |
| 5078 | struct rtnexthop *rtnh; |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 5079 | struct fib6_info *rt; |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 5080 | struct rt6_nh *err_nh; |
| 5081 | struct rt6_nh *nh, *nh_safe; |
David Ahern | 3b1137f | 2017-02-02 12:37:10 -0800 | [diff] [blame] | 5082 | __u16 nlflags; |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 5083 | int remaining; |
| 5084 | int attrlen; |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 5085 | int err = 1; |
| 5086 | int nhn = 0; |
| 5087 | int replace = (cfg->fc_nlinfo.nlh && |
| 5088 | (cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_REPLACE)); |
| 5089 | LIST_HEAD(rt6_nh_list); |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 5090 | |
David Ahern | 3b1137f | 2017-02-02 12:37:10 -0800 | [diff] [blame] | 5091 | nlflags = replace ? NLM_F_REPLACE : NLM_F_CREATE; |
| 5092 | if (info->nlh && info->nlh->nlmsg_flags & NLM_F_APPEND) |
| 5093 | nlflags |= NLM_F_APPEND; |
| 5094 | |
Michal Kubeček | 35f1b4e | 2015-05-18 20:53:55 +0200 | [diff] [blame] | 5095 | remaining = cfg->fc_mp_len; |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 5096 | rtnh = (struct rtnexthop *)cfg->fc_mp; |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 5097 | |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 5098 | /* Parse a Multipath Entry and build a list (rt6_nh_list) of |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 5099 | * fib6_info structs per nexthop |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 5100 | */ |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 5101 | while (rtnh_ok(rtnh, remaining)) { |
| 5102 | memcpy(&r_cfg, cfg, sizeof(*cfg)); |
| 5103 | if (rtnh->rtnh_ifindex) |
| 5104 | r_cfg.fc_ifindex = rtnh->rtnh_ifindex; |
| 5105 | |
| 5106 | attrlen = rtnh_attrlen(rtnh); |
| 5107 | if (attrlen > 0) { |
| 5108 | struct nlattr *nla, *attrs = rtnh_attrs(rtnh); |
| 5109 | |
| 5110 | nla = nla_find(attrs, attrlen, RTA_GATEWAY); |
| 5111 | if (nla) { |
Jiri Benc | 67b61f6 | 2015-03-29 16:59:26 +0200 | [diff] [blame] | 5112 | r_cfg.fc_gateway = nla_get_in6_addr(nla); |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 5113 | r_cfg.fc_flags |= RTF_GATEWAY; |
| 5114 | } |
Roopa Prabhu | 19e42e4 | 2015-07-21 10:43:48 +0200 | [diff] [blame] | 5115 | r_cfg.fc_encap = nla_find(attrs, attrlen, RTA_ENCAP); |
| 5116 | nla = nla_find(attrs, attrlen, RTA_ENCAP_TYPE); |
| 5117 | if (nla) |
| 5118 | r_cfg.fc_encap_type = nla_get_u16(nla); |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 5119 | } |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 5120 | |
David Ahern | 68e2ffd | 2018-03-20 10:06:59 -0700 | [diff] [blame] | 5121 | r_cfg.fc_flags |= (rtnh->rtnh_flags & RTNH_F_ONLINK); |
David Ahern | acb54e3 | 2018-04-17 17:33:22 -0700 | [diff] [blame] | 5122 | rt = ip6_route_info_create(&r_cfg, GFP_KERNEL, extack); |
Roopa Prabhu | 8c5b83f | 2015-10-10 08:26:36 -0700 | [diff] [blame] | 5123 | if (IS_ERR(rt)) { |
| 5124 | err = PTR_ERR(rt); |
| 5125 | rt = NULL; |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 5126 | goto cleanup; |
Roopa Prabhu | 8c5b83f | 2015-10-10 08:26:36 -0700 | [diff] [blame] | 5127 | } |
David Ahern | b5d2d75 | 2018-07-15 09:35:19 -0700 | [diff] [blame] | 5128 | if (!rt6_qualify_for_ecmp(rt)) { |
| 5129 | err = -EINVAL; |
| 5130 | NL_SET_ERR_MSG(extack, |
| 5131 | "Device only routes can not be added for IPv6 using the multipath API."); |
| 5132 | fib6_info_release(rt); |
| 5133 | goto cleanup; |
| 5134 | } |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 5135 | |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 5136 | rt->fib6_nh->fib_nh_weight = rtnh->rtnh_hops + 1; |
Ido Schimmel | 398958a | 2018-01-09 16:40:28 +0200 | [diff] [blame] | 5137 | |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 5138 | err = ip6_route_info_append(info->nl_net, &rt6_nh_list, |
| 5139 | rt, &r_cfg); |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 5140 | if (err) { |
David Ahern | 93531c6 | 2018-04-17 17:33:25 -0700 | [diff] [blame] | 5141 | fib6_info_release(rt); |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 5142 | goto cleanup; |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 5143 | } |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 5144 | |
| 5145 | rtnh = rtnh_next(rtnh, &remaining); |
| 5146 | } |
| 5147 | |
Ido Schimmel | 9eee3b4 | 2019-06-20 12:10:21 +0300 | [diff] [blame] | 5148 | if (list_empty(&rt6_nh_list)) { |
| 5149 | NL_SET_ERR_MSG(extack, |
| 5150 | "Invalid nexthop configuration - no valid nexthops"); |
| 5151 | return -EINVAL; |
| 5152 | } |
| 5153 | |
David Ahern | 3b1137f | 2017-02-02 12:37:10 -0800 | [diff] [blame] | 5154 | /* for add and replace send one notification with all nexthops. |
| 5155 | * Skip the notification in fib6_add_rt2node and send one with |
| 5156 | * the full route when done |
| 5157 | */ |
| 5158 | info->skip_notify = 1; |
| 5159 | |
Ido Schimmel | ebee3ca | 2019-06-18 18:12:48 +0300 | [diff] [blame] | 5160 | /* For add and replace, send one notification with all nexthops. For |
| 5161 | * append, send one notification with all appended nexthops. |
| 5162 | */ |
| 5163 | info->skip_notify_kernel = 1; |
| 5164 | |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 5165 | err_nh = NULL; |
| 5166 | list_for_each_entry(nh, &rt6_nh_list, next) { |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 5167 | err = __ip6_ins_rt(nh->fib6_info, info, extack); |
| 5168 | fib6_info_release(nh->fib6_info); |
David Ahern | 3b1137f | 2017-02-02 12:37:10 -0800 | [diff] [blame] | 5169 | |
David Ahern | f722517 | 2018-06-04 13:41:42 -0700 | [diff] [blame] | 5170 | if (!err) { |
| 5171 | /* save reference to last route successfully inserted */ |
| 5172 | rt_last = nh->fib6_info; |
| 5173 | |
| 5174 | /* save reference to first route for notification */ |
| 5175 | if (!rt_notif) |
| 5176 | rt_notif = nh->fib6_info; |
| 5177 | } |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 5178 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 5179 | /* nh->fib6_info is used or freed at this point, reset to NULL*/ |
| 5180 | nh->fib6_info = NULL; |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 5181 | if (err) { |
| 5182 | if (replace && nhn) |
Jakub Kicinski | a5a82d8 | 2019-01-14 10:52:45 -0800 | [diff] [blame] | 5183 | NL_SET_ERR_MSG_MOD(extack, |
| 5184 | "multipath route replace failed (check consistency of installed routes)"); |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 5185 | err_nh = nh; |
| 5186 | goto add_errout; |
| 5187 | } |
| 5188 | |
Nicolas Dichtel | 1a72418 | 2012-11-01 22:58:22 +0000 | [diff] [blame] | 5189 | /* Because each route is added like a single route we remove |
Michal Kubeček | 2759647 | 2015-05-18 20:54:00 +0200 | [diff] [blame] | 5190 | * these flags after the first nexthop: if there is a collision, |
| 5191 | * we have already failed to add the first nexthop: |
| 5192 | * fib6_add_rt2node() has rejected it; when replacing, old |
| 5193 | * nexthops have been replaced by first new, the rest should |
| 5194 | * be added to it. |
Nicolas Dichtel | 1a72418 | 2012-11-01 22:58:22 +0000 | [diff] [blame] | 5195 | */ |
Michal Kubeček | 2759647 | 2015-05-18 20:54:00 +0200 | [diff] [blame] | 5196 | cfg->fc_nlinfo.nlh->nlmsg_flags &= ~(NLM_F_EXCL | |
| 5197 | NLM_F_REPLACE); |
Benjamin Poirier | afecdb3 | 2020-02-12 10:41:07 +0900 | [diff] [blame] | 5198 | cfg->fc_nlinfo.nlh->nlmsg_flags |= NLM_F_CREATE; |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 5199 | nhn++; |
| 5200 | } |
| 5201 | |
Ido Schimmel | 0ee0f47 | 2019-12-23 15:28:15 +0200 | [diff] [blame] | 5202 | /* An in-kernel notification should only be sent in case the new |
| 5203 | * multipath route is added as the first route in the node, or if |
| 5204 | * it was appended to it. We pass 'rt_notif' since it is the first |
| 5205 | * sibling and might allow us to skip some checks in the replace case. |
| 5206 | */ |
| 5207 | if (ip6_route_mpath_should_notify(rt_notif)) { |
| 5208 | enum fib_event_type fib_event; |
| 5209 | |
| 5210 | if (rt_notif->fib6_nsiblings != nhn - 1) |
| 5211 | fib_event = FIB_EVENT_ENTRY_APPEND; |
| 5212 | else |
Ido Schimmel | caafb25 | 2019-12-23 15:28:20 +0200 | [diff] [blame] | 5213 | fib_event = FIB_EVENT_ENTRY_REPLACE; |
Ido Schimmel | 0ee0f47 | 2019-12-23 15:28:15 +0200 | [diff] [blame] | 5214 | |
| 5215 | err = call_fib6_multipath_entry_notifiers(info->nl_net, |
| 5216 | fib_event, rt_notif, |
| 5217 | nhn - 1, extack); |
| 5218 | if (err) { |
| 5219 | /* Delete all the siblings that were just added */ |
| 5220 | err_nh = NULL; |
| 5221 | goto add_errout; |
| 5222 | } |
| 5223 | } |
Ido Schimmel | ebee3ca | 2019-06-18 18:12:48 +0300 | [diff] [blame] | 5224 | |
David Ahern | 3b1137f | 2017-02-02 12:37:10 -0800 | [diff] [blame] | 5225 | /* success ... tell user about new route */ |
| 5226 | ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags); |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 5227 | goto cleanup; |
| 5228 | |
| 5229 | add_errout: |
David Ahern | 3b1137f | 2017-02-02 12:37:10 -0800 | [diff] [blame] | 5230 | /* send notification for routes that were added so that |
| 5231 | * the delete notifications sent by ip6_route_del are |
| 5232 | * coherent |
| 5233 | */ |
| 5234 | if (rt_notif) |
| 5235 | ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags); |
| 5236 | |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 5237 | /* Delete routes that were already added */ |
| 5238 | list_for_each_entry(nh, &rt6_nh_list, next) { |
| 5239 | if (err_nh == nh) |
| 5240 | break; |
David Ahern | 333c430 | 2017-05-21 10:12:04 -0600 | [diff] [blame] | 5241 | ip6_route_del(&nh->r_cfg, extack); |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 5242 | } |
| 5243 | |
| 5244 | cleanup: |
| 5245 | list_for_each_entry_safe(nh, nh_safe, &rt6_nh_list, next) { |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 5246 | if (nh->fib6_info) |
| 5247 | fib6_info_release(nh->fib6_info); |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 5248 | list_del(&nh->next); |
| 5249 | kfree(nh); |
| 5250 | } |
| 5251 | |
| 5252 | return err; |
| 5253 | } |
| 5254 | |
David Ahern | 333c430 | 2017-05-21 10:12:04 -0600 | [diff] [blame] | 5255 | static int ip6_route_multipath_del(struct fib6_config *cfg, |
| 5256 | struct netlink_ext_ack *extack) |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 5257 | { |
| 5258 | struct fib6_config r_cfg; |
| 5259 | struct rtnexthop *rtnh; |
| 5260 | int remaining; |
| 5261 | int attrlen; |
| 5262 | int err = 1, last_err = 0; |
| 5263 | |
| 5264 | remaining = cfg->fc_mp_len; |
| 5265 | rtnh = (struct rtnexthop *)cfg->fc_mp; |
| 5266 | |
| 5267 | /* Parse a Multipath Entry */ |
| 5268 | while (rtnh_ok(rtnh, remaining)) { |
| 5269 | memcpy(&r_cfg, cfg, sizeof(*cfg)); |
| 5270 | if (rtnh->rtnh_ifindex) |
| 5271 | r_cfg.fc_ifindex = rtnh->rtnh_ifindex; |
| 5272 | |
| 5273 | attrlen = rtnh_attrlen(rtnh); |
| 5274 | if (attrlen > 0) { |
| 5275 | struct nlattr *nla, *attrs = rtnh_attrs(rtnh); |
| 5276 | |
| 5277 | nla = nla_find(attrs, attrlen, RTA_GATEWAY); |
| 5278 | if (nla) { |
| 5279 | nla_memcpy(&r_cfg.fc_gateway, nla, 16); |
| 5280 | r_cfg.fc_flags |= RTF_GATEWAY; |
| 5281 | } |
| 5282 | } |
David Ahern | 333c430 | 2017-05-21 10:12:04 -0600 | [diff] [blame] | 5283 | err = ip6_route_del(&r_cfg, extack); |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 5284 | if (err) |
| 5285 | last_err = err; |
| 5286 | |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 5287 | rtnh = rtnh_next(rtnh, &remaining); |
| 5288 | } |
| 5289 | |
| 5290 | return last_err; |
| 5291 | } |
| 5292 | |
David Ahern | c21ef3e | 2017-04-16 09:48:24 -0700 | [diff] [blame] | 5293 | static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh, |
| 5294 | struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5295 | { |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 5296 | struct fib6_config cfg; |
| 5297 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5298 | |
David Ahern | 333c430 | 2017-05-21 10:12:04 -0600 | [diff] [blame] | 5299 | err = rtm_to_fib6_config(skb, nlh, &cfg, extack); |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 5300 | if (err < 0) |
| 5301 | return err; |
| 5302 | |
David Ahern | 5b98324 | 2019-06-08 14:53:34 -0700 | [diff] [blame] | 5303 | if (cfg.fc_nh_id && |
| 5304 | !nexthop_find_by_id(sock_net(skb->sk), cfg.fc_nh_id)) { |
| 5305 | NL_SET_ERR_MSG(extack, "Nexthop id does not exist"); |
| 5306 | return -EINVAL; |
| 5307 | } |
| 5308 | |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 5309 | if (cfg.fc_mp) |
David Ahern | 333c430 | 2017-05-21 10:12:04 -0600 | [diff] [blame] | 5310 | return ip6_route_multipath_del(&cfg, extack); |
David Ahern | 0ae8133 | 2017-02-02 12:37:08 -0800 | [diff] [blame] | 5311 | else { |
| 5312 | cfg.fc_delete_all_nh = 1; |
David Ahern | 333c430 | 2017-05-21 10:12:04 -0600 | [diff] [blame] | 5313 | return ip6_route_del(&cfg, extack); |
David Ahern | 0ae8133 | 2017-02-02 12:37:08 -0800 | [diff] [blame] | 5314 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5315 | } |
| 5316 | |
David Ahern | c21ef3e | 2017-04-16 09:48:24 -0700 | [diff] [blame] | 5317 | static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh, |
| 5318 | struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5319 | { |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 5320 | struct fib6_config cfg; |
| 5321 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5322 | |
David Ahern | 333c430 | 2017-05-21 10:12:04 -0600 | [diff] [blame] | 5323 | err = rtm_to_fib6_config(skb, nlh, &cfg, extack); |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 5324 | if (err < 0) |
| 5325 | return err; |
| 5326 | |
David Ahern | 67f6951 | 2019-03-21 05:21:34 -0700 | [diff] [blame] | 5327 | if (cfg.fc_metric == 0) |
| 5328 | cfg.fc_metric = IP6_RT_PRIO_USER; |
| 5329 | |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 5330 | if (cfg.fc_mp) |
David Ahern | 333c430 | 2017-05-21 10:12:04 -0600 | [diff] [blame] | 5331 | return ip6_route_multipath_add(&cfg, extack); |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 5332 | else |
David Ahern | acb54e3 | 2018-04-17 17:33:22 -0700 | [diff] [blame] | 5333 | return ip6_route_add(&cfg, GFP_KERNEL, extack); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5334 | } |
| 5335 | |
David Ahern | a1b7a1f | 2019-06-08 14:53:26 -0700 | [diff] [blame] | 5336 | /* add the overhead of this fib6_nh to nexthop_len */ |
| 5337 | static int rt6_nh_nlmsg_size(struct fib6_nh *nh, void *arg) |
Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 5338 | { |
David Ahern | a1b7a1f | 2019-06-08 14:53:26 -0700 | [diff] [blame] | 5339 | int *nexthop_len = arg; |
David Ahern | beb1afac5 | 2017-02-02 12:37:09 -0800 | [diff] [blame] | 5340 | |
David Ahern | a1b7a1f | 2019-06-08 14:53:26 -0700 | [diff] [blame] | 5341 | *nexthop_len += nla_total_size(0) /* RTA_MULTIPATH */ |
| 5342 | + NLA_ALIGN(sizeof(struct rtnexthop)) |
| 5343 | + nla_total_size(16); /* RTA_GATEWAY */ |
David Ahern | f88d8ea | 2019-06-03 20:19:52 -0700 | [diff] [blame] | 5344 | |
David Ahern | a1b7a1f | 2019-06-08 14:53:26 -0700 | [diff] [blame] | 5345 | if (nh->fib_nh_lws) { |
| 5346 | /* RTA_ENCAP_TYPE */ |
| 5347 | *nexthop_len += lwtunnel_get_encap_size(nh->fib_nh_lws); |
| 5348 | /* RTA_ENCAP */ |
| 5349 | *nexthop_len += nla_total_size(2); |
| 5350 | } |
David Ahern | beb1afac5 | 2017-02-02 12:37:09 -0800 | [diff] [blame] | 5351 | |
David Ahern | a1b7a1f | 2019-06-08 14:53:26 -0700 | [diff] [blame] | 5352 | return 0; |
| 5353 | } |
| 5354 | |
| 5355 | static size_t rt6_nlmsg_size(struct fib6_info *f6i) |
| 5356 | { |
| 5357 | int nexthop_len; |
| 5358 | |
| 5359 | if (f6i->nh) { |
| 5360 | nexthop_len = nla_total_size(4); /* RTA_NH_ID */ |
| 5361 | nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_nlmsg_size, |
| 5362 | &nexthop_len); |
| 5363 | } else { |
| 5364 | struct fib6_nh *nh = f6i->fib6_nh; |
| 5365 | |
| 5366 | nexthop_len = 0; |
| 5367 | if (f6i->fib6_nsiblings) { |
| 5368 | nexthop_len = nla_total_size(0) /* RTA_MULTIPATH */ |
| 5369 | + NLA_ALIGN(sizeof(struct rtnexthop)) |
| 5370 | + nla_total_size(16) /* RTA_GATEWAY */ |
| 5371 | + lwtunnel_get_encap_size(nh->fib_nh_lws); |
| 5372 | |
| 5373 | nexthop_len *= f6i->fib6_nsiblings; |
| 5374 | } |
| 5375 | nexthop_len += lwtunnel_get_encap_size(nh->fib_nh_lws); |
David Ahern | beb1afac5 | 2017-02-02 12:37:09 -0800 | [diff] [blame] | 5376 | } |
| 5377 | |
Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 5378 | return NLMSG_ALIGN(sizeof(struct rtmsg)) |
| 5379 | + nla_total_size(16) /* RTA_SRC */ |
| 5380 | + nla_total_size(16) /* RTA_DST */ |
| 5381 | + nla_total_size(16) /* RTA_GATEWAY */ |
| 5382 | + nla_total_size(16) /* RTA_PREFSRC */ |
| 5383 | + nla_total_size(4) /* RTA_TABLE */ |
| 5384 | + nla_total_size(4) /* RTA_IIF */ |
| 5385 | + nla_total_size(4) /* RTA_OIF */ |
| 5386 | + nla_total_size(4) /* RTA_PRIORITY */ |
Noriaki TAKAMIYA | 6a2b9ce | 2007-01-23 22:09:41 -0800 | [diff] [blame] | 5387 | + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */ |
Daniel Borkmann | ea69763 | 2015-01-05 23:57:47 +0100 | [diff] [blame] | 5388 | + nla_total_size(sizeof(struct rta_cacheinfo)) |
Lubomir Rintel | c78ba6d | 2015-03-11 15:39:21 +0100 | [diff] [blame] | 5389 | + nla_total_size(TCP_CA_NAME_MAX) /* RTAX_CC_ALGO */ |
Roopa Prabhu | 19e42e4 | 2015-07-21 10:43:48 +0200 | [diff] [blame] | 5390 | + nla_total_size(1) /* RTA_PREF */ |
David Ahern | beb1afac5 | 2017-02-02 12:37:09 -0800 | [diff] [blame] | 5391 | + nexthop_len; |
| 5392 | } |
| 5393 | |
David Ahern | f88d8ea | 2019-06-03 20:19:52 -0700 | [diff] [blame] | 5394 | static int rt6_fill_node_nexthop(struct sk_buff *skb, struct nexthop *nh, |
| 5395 | unsigned char *flags) |
| 5396 | { |
| 5397 | if (nexthop_is_multipath(nh)) { |
| 5398 | struct nlattr *mp; |
| 5399 | |
David Ahern | 4255ff0 | 2019-09-03 15:22:12 -0700 | [diff] [blame] | 5400 | mp = nla_nest_start_noflag(skb, RTA_MULTIPATH); |
David Ahern | f88d8ea | 2019-06-03 20:19:52 -0700 | [diff] [blame] | 5401 | if (!mp) |
| 5402 | goto nla_put_failure; |
| 5403 | |
Donald Sharp | 7bdf4de | 2019-09-04 10:11:58 -0400 | [diff] [blame] | 5404 | if (nexthop_mpath_fill_node(skb, nh, AF_INET6)) |
David Ahern | f88d8ea | 2019-06-03 20:19:52 -0700 | [diff] [blame] | 5405 | goto nla_put_failure; |
| 5406 | |
| 5407 | nla_nest_end(skb, mp); |
| 5408 | } else { |
| 5409 | struct fib6_nh *fib6_nh; |
| 5410 | |
| 5411 | fib6_nh = nexthop_fib6_nh(nh); |
Donald Sharp | 7bdf4de | 2019-09-04 10:11:58 -0400 | [diff] [blame] | 5412 | if (fib_nexthop_info(skb, &fib6_nh->nh_common, AF_INET6, |
David Ahern | f88d8ea | 2019-06-03 20:19:52 -0700 | [diff] [blame] | 5413 | flags, false) < 0) |
| 5414 | goto nla_put_failure; |
| 5415 | } |
| 5416 | |
| 5417 | return 0; |
| 5418 | |
| 5419 | nla_put_failure: |
| 5420 | return -EMSGSIZE; |
| 5421 | } |
| 5422 | |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 5423 | static int rt6_fill_node(struct net *net, struct sk_buff *skb, |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 5424 | struct fib6_info *rt, struct dst_entry *dst, |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 5425 | struct in6_addr *dest, struct in6_addr *src, |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 5426 | int iif, int type, u32 portid, u32 seq, |
David Ahern | f8cfe2c | 2017-01-17 15:51:08 -0800 | [diff] [blame] | 5427 | unsigned int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5428 | { |
Xin Long | 22d0bd8 | 2018-09-11 14:33:58 +0800 | [diff] [blame] | 5429 | struct rt6_info *rt6 = (struct rt6_info *)dst; |
| 5430 | struct rt6key *rt6_dst, *rt6_src; |
| 5431 | u32 *pmetrics, table, rt6_flags; |
David Ahern | f88d8ea | 2019-06-03 20:19:52 -0700 | [diff] [blame] | 5432 | unsigned char nh_flags = 0; |
Thomas Graf | 2d7202b | 2006-08-22 00:01:27 -0700 | [diff] [blame] | 5433 | struct nlmsghdr *nlh; |
Xin Long | 22d0bd8 | 2018-09-11 14:33:58 +0800 | [diff] [blame] | 5434 | struct rtmsg *rtm; |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 5435 | long expires = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5436 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 5437 | nlh = nlmsg_put(skb, portid, seq, type, sizeof(*rtm), flags); |
David S. Miller | 3830847 | 2011-12-03 18:02:47 -0500 | [diff] [blame] | 5438 | if (!nlh) |
Patrick McHardy | 2693256 | 2007-01-31 23:16:40 -0800 | [diff] [blame] | 5439 | return -EMSGSIZE; |
Thomas Graf | 2d7202b | 2006-08-22 00:01:27 -0700 | [diff] [blame] | 5440 | |
Xin Long | 22d0bd8 | 2018-09-11 14:33:58 +0800 | [diff] [blame] | 5441 | if (rt6) { |
| 5442 | rt6_dst = &rt6->rt6i_dst; |
| 5443 | rt6_src = &rt6->rt6i_src; |
| 5444 | rt6_flags = rt6->rt6i_flags; |
| 5445 | } else { |
| 5446 | rt6_dst = &rt->fib6_dst; |
| 5447 | rt6_src = &rt->fib6_src; |
| 5448 | rt6_flags = rt->fib6_flags; |
| 5449 | } |
| 5450 | |
Thomas Graf | 2d7202b | 2006-08-22 00:01:27 -0700 | [diff] [blame] | 5451 | rtm = nlmsg_data(nlh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5452 | rtm->rtm_family = AF_INET6; |
Xin Long | 22d0bd8 | 2018-09-11 14:33:58 +0800 | [diff] [blame] | 5453 | rtm->rtm_dst_len = rt6_dst->plen; |
| 5454 | rtm->rtm_src_len = rt6_src->plen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5455 | rtm->rtm_tos = 0; |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 5456 | if (rt->fib6_table) |
| 5457 | table = rt->fib6_table->tb6_id; |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 5458 | else |
Patrick McHardy | 9e762a4 | 2006-08-10 23:09:48 -0700 | [diff] [blame] | 5459 | table = RT6_TABLE_UNSPEC; |
Kalash Nainwal | 97f0082 | 2019-02-20 16:23:04 -0800 | [diff] [blame] | 5460 | rtm->rtm_table = table < 256 ? table : RT_TABLE_COMPAT; |
David S. Miller | c78679e | 2012-04-01 20:27:33 -0400 | [diff] [blame] | 5461 | if (nla_put_u32(skb, RTA_TABLE, table)) |
| 5462 | goto nla_put_failure; |
David Ahern | e8478e8 | 2018-04-17 17:33:13 -0700 | [diff] [blame] | 5463 | |
| 5464 | rtm->rtm_type = rt->fib6_type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5465 | rtm->rtm_flags = 0; |
| 5466 | rtm->rtm_scope = RT_SCOPE_UNIVERSE; |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 5467 | rtm->rtm_protocol = rt->fib6_protocol; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5468 | |
Xin Long | 22d0bd8 | 2018-09-11 14:33:58 +0800 | [diff] [blame] | 5469 | if (rt6_flags & RTF_CACHE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5470 | rtm->rtm_flags |= RTM_F_CLONED; |
| 5471 | |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 5472 | if (dest) { |
| 5473 | if (nla_put_in6_addr(skb, RTA_DST, dest)) |
David S. Miller | c78679e | 2012-04-01 20:27:33 -0400 | [diff] [blame] | 5474 | goto nla_put_failure; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 5475 | rtm->rtm_dst_len = 128; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5476 | } else if (rtm->rtm_dst_len) |
Xin Long | 22d0bd8 | 2018-09-11 14:33:58 +0800 | [diff] [blame] | 5477 | if (nla_put_in6_addr(skb, RTA_DST, &rt6_dst->addr)) |
David S. Miller | c78679e | 2012-04-01 20:27:33 -0400 | [diff] [blame] | 5478 | goto nla_put_failure; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5479 | #ifdef CONFIG_IPV6_SUBTREES |
| 5480 | if (src) { |
Jiri Benc | 930345e | 2015-03-29 16:59:25 +0200 | [diff] [blame] | 5481 | if (nla_put_in6_addr(skb, RTA_SRC, src)) |
David S. Miller | c78679e | 2012-04-01 20:27:33 -0400 | [diff] [blame] | 5482 | goto nla_put_failure; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 5483 | rtm->rtm_src_len = 128; |
David S. Miller | c78679e | 2012-04-01 20:27:33 -0400 | [diff] [blame] | 5484 | } else if (rtm->rtm_src_len && |
Xin Long | 22d0bd8 | 2018-09-11 14:33:58 +0800 | [diff] [blame] | 5485 | nla_put_in6_addr(skb, RTA_SRC, &rt6_src->addr)) |
David S. Miller | c78679e | 2012-04-01 20:27:33 -0400 | [diff] [blame] | 5486 | goto nla_put_failure; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5487 | #endif |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 5488 | if (iif) { |
| 5489 | #ifdef CONFIG_IPV6_MROUTE |
Xin Long | 22d0bd8 | 2018-09-11 14:33:58 +0800 | [diff] [blame] | 5490 | if (ipv6_addr_is_multicast(&rt6_dst->addr)) { |
David Ahern | fd61c6b | 2017-01-17 15:51:07 -0800 | [diff] [blame] | 5491 | int err = ip6mr_get_route(net, skb, rtm, portid); |
Nikolay Aleksandrov | 2cf7507 | 2016-09-25 23:08:31 +0200 | [diff] [blame] | 5492 | |
David Ahern | fd61c6b | 2017-01-17 15:51:07 -0800 | [diff] [blame] | 5493 | if (err == 0) |
| 5494 | return 0; |
| 5495 | if (err < 0) |
| 5496 | goto nla_put_failure; |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 5497 | } else |
| 5498 | #endif |
David S. Miller | c78679e | 2012-04-01 20:27:33 -0400 | [diff] [blame] | 5499 | if (nla_put_u32(skb, RTA_IIF, iif)) |
| 5500 | goto nla_put_failure; |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 5501 | } else if (dest) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5502 | struct in6_addr saddr_buf; |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 5503 | if (ip6_route_get_saddr(net, rt, dest, 0, &saddr_buf) == 0 && |
Jiri Benc | 930345e | 2015-03-29 16:59:25 +0200 | [diff] [blame] | 5504 | nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf)) |
David S. Miller | c78679e | 2012-04-01 20:27:33 -0400 | [diff] [blame] | 5505 | goto nla_put_failure; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5506 | } |
Thomas Graf | 2d7202b | 2006-08-22 00:01:27 -0700 | [diff] [blame] | 5507 | |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 5508 | if (rt->fib6_prefsrc.plen) { |
Daniel Walter | c3968a8 | 2011-04-13 21:10:57 +0000 | [diff] [blame] | 5509 | struct in6_addr saddr_buf; |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 5510 | saddr_buf = rt->fib6_prefsrc.addr; |
Jiri Benc | 930345e | 2015-03-29 16:59:25 +0200 | [diff] [blame] | 5511 | if (nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf)) |
David S. Miller | c78679e | 2012-04-01 20:27:33 -0400 | [diff] [blame] | 5512 | goto nla_put_failure; |
Daniel Walter | c3968a8 | 2011-04-13 21:10:57 +0000 | [diff] [blame] | 5513 | } |
| 5514 | |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 5515 | pmetrics = dst ? dst_metrics_ptr(dst) : rt->fib6_metrics->metrics; |
| 5516 | if (rtnetlink_put_metrics(skb, pmetrics) < 0) |
Thomas Graf | 2d7202b | 2006-08-22 00:01:27 -0700 | [diff] [blame] | 5517 | goto nla_put_failure; |
| 5518 | |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 5519 | if (nla_put_u32(skb, RTA_PRIORITY, rt->fib6_metric)) |
David S. Miller | c78679e | 2012-04-01 20:27:33 -0400 | [diff] [blame] | 5520 | goto nla_put_failure; |
Li Wei | 8253947 | 2012-07-29 16:01:30 +0000 | [diff] [blame] | 5521 | |
David Ahern | beb1afac5 | 2017-02-02 12:37:09 -0800 | [diff] [blame] | 5522 | /* For multipath routes, walk the siblings list and add |
| 5523 | * each as a nexthop within RTA_MULTIPATH. |
| 5524 | */ |
Xin Long | 22d0bd8 | 2018-09-11 14:33:58 +0800 | [diff] [blame] | 5525 | if (rt6) { |
| 5526 | if (rt6_flags & RTF_GATEWAY && |
| 5527 | nla_put_in6_addr(skb, RTA_GATEWAY, &rt6->rt6i_gateway)) |
| 5528 | goto nla_put_failure; |
| 5529 | |
| 5530 | if (dst->dev && nla_put_u32(skb, RTA_OIF, dst->dev->ifindex)) |
| 5531 | goto nla_put_failure; |
| 5532 | } else if (rt->fib6_nsiblings) { |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 5533 | struct fib6_info *sibling, *next_sibling; |
David Ahern | beb1afac5 | 2017-02-02 12:37:09 -0800 | [diff] [blame] | 5534 | struct nlattr *mp; |
| 5535 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 5536 | mp = nla_nest_start_noflag(skb, RTA_MULTIPATH); |
David Ahern | beb1afac5 | 2017-02-02 12:37:09 -0800 | [diff] [blame] | 5537 | if (!mp) |
| 5538 | goto nla_put_failure; |
| 5539 | |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 5540 | if (fib_add_nexthop(skb, &rt->fib6_nh->nh_common, |
Donald Sharp | 7bdf4de | 2019-09-04 10:11:58 -0400 | [diff] [blame] | 5541 | rt->fib6_nh->fib_nh_weight, AF_INET6) < 0) |
David Ahern | beb1afac5 | 2017-02-02 12:37:09 -0800 | [diff] [blame] | 5542 | goto nla_put_failure; |
| 5543 | |
| 5544 | list_for_each_entry_safe(sibling, next_sibling, |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 5545 | &rt->fib6_siblings, fib6_siblings) { |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 5546 | if (fib_add_nexthop(skb, &sibling->fib6_nh->nh_common, |
Donald Sharp | 7bdf4de | 2019-09-04 10:11:58 -0400 | [diff] [blame] | 5547 | sibling->fib6_nh->fib_nh_weight, |
| 5548 | AF_INET6) < 0) |
David Ahern | beb1afac5 | 2017-02-02 12:37:09 -0800 | [diff] [blame] | 5549 | goto nla_put_failure; |
| 5550 | } |
| 5551 | |
| 5552 | nla_nest_end(skb, mp); |
David Ahern | f88d8ea | 2019-06-03 20:19:52 -0700 | [diff] [blame] | 5553 | } else if (rt->nh) { |
| 5554 | if (nla_put_u32(skb, RTA_NH_ID, rt->nh->id)) |
| 5555 | goto nla_put_failure; |
David Ahern | ecc5663 | 2019-04-23 08:48:09 -0700 | [diff] [blame] | 5556 | |
David Ahern | f88d8ea | 2019-06-03 20:19:52 -0700 | [diff] [blame] | 5557 | if (nexthop_is_blackhole(rt->nh)) |
| 5558 | rtm->rtm_type = RTN_BLACKHOLE; |
| 5559 | |
Roopa Prabhu | 4f80116 | 2020-04-27 13:56:46 -0700 | [diff] [blame] | 5560 | if (net->ipv4.sysctl_nexthop_compat_mode && |
| 5561 | rt6_fill_node_nexthop(skb, rt->nh, &nh_flags) < 0) |
David Ahern | f88d8ea | 2019-06-03 20:19:52 -0700 | [diff] [blame] | 5562 | goto nla_put_failure; |
| 5563 | |
| 5564 | rtm->rtm_flags |= nh_flags; |
| 5565 | } else { |
Donald Sharp | 7bdf4de | 2019-09-04 10:11:58 -0400 | [diff] [blame] | 5566 | if (fib_nexthop_info(skb, &rt->fib6_nh->nh_common, AF_INET6, |
David Ahern | ecc5663 | 2019-04-23 08:48:09 -0700 | [diff] [blame] | 5567 | &nh_flags, false) < 0) |
David Ahern | beb1afac5 | 2017-02-02 12:37:09 -0800 | [diff] [blame] | 5568 | goto nla_put_failure; |
David Ahern | ecc5663 | 2019-04-23 08:48:09 -0700 | [diff] [blame] | 5569 | |
| 5570 | rtm->rtm_flags |= nh_flags; |
David Ahern | beb1afac5 | 2017-02-02 12:37:09 -0800 | [diff] [blame] | 5571 | } |
| 5572 | |
Xin Long | 22d0bd8 | 2018-09-11 14:33:58 +0800 | [diff] [blame] | 5573 | if (rt6_flags & RTF_EXPIRES) { |
David Ahern | 1489568 | 2018-04-17 17:33:17 -0700 | [diff] [blame] | 5574 | expires = dst ? dst->expires : rt->expires; |
| 5575 | expires -= jiffies; |
| 5576 | } |
YOSHIFUJI Hideaki | 69cdf8f | 2008-05-19 16:55:13 -0700 | [diff] [blame] | 5577 | |
Ido Schimmel | bb3c4ab | 2020-01-14 13:23:12 +0200 | [diff] [blame] | 5578 | if (!dst) { |
| 5579 | if (rt->offload) |
| 5580 | rtm->rtm_flags |= RTM_F_OFFLOAD; |
| 5581 | if (rt->trap) |
| 5582 | rtm->rtm_flags |= RTM_F_TRAP; |
| 5583 | } |
| 5584 | |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 5585 | 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] | 5586 | goto nla_put_failure; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5587 | |
Xin Long | 22d0bd8 | 2018-09-11 14:33:58 +0800 | [diff] [blame] | 5588 | if (nla_put_u8(skb, RTA_PREF, IPV6_EXTRACT_PREF(rt6_flags))) |
Lubomir Rintel | c78ba6d | 2015-03-11 15:39:21 +0100 | [diff] [blame] | 5589 | goto nla_put_failure; |
| 5590 | |
Roopa Prabhu | 19e42e4 | 2015-07-21 10:43:48 +0200 | [diff] [blame] | 5591 | |
Johannes Berg | 053c095 | 2015-01-16 22:09:00 +0100 | [diff] [blame] | 5592 | nlmsg_end(skb, nlh); |
| 5593 | return 0; |
Thomas Graf | 2d7202b | 2006-08-22 00:01:27 -0700 | [diff] [blame] | 5594 | |
| 5595 | nla_put_failure: |
Patrick McHardy | 2693256 | 2007-01-31 23:16:40 -0800 | [diff] [blame] | 5596 | nlmsg_cancel(skb, nlh); |
| 5597 | return -EMSGSIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5598 | } |
| 5599 | |
David Ahern | 2c170e0 | 2019-06-08 14:53:27 -0700 | [diff] [blame] | 5600 | static int fib6_info_nh_uses_dev(struct fib6_nh *nh, void *arg) |
| 5601 | { |
| 5602 | const struct net_device *dev = arg; |
| 5603 | |
| 5604 | if (nh->fib_nh_dev == dev) |
| 5605 | return 1; |
| 5606 | |
| 5607 | return 0; |
| 5608 | } |
| 5609 | |
David Ahern | 13e3890 | 2018-10-15 18:56:44 -0700 | [diff] [blame] | 5610 | static bool fib6_info_uses_dev(const struct fib6_info *f6i, |
| 5611 | const struct net_device *dev) |
| 5612 | { |
David Ahern | 2c170e0 | 2019-06-08 14:53:27 -0700 | [diff] [blame] | 5613 | if (f6i->nh) { |
| 5614 | struct net_device *_dev = (struct net_device *)dev; |
| 5615 | |
| 5616 | return !!nexthop_for_each_fib6_nh(f6i->nh, |
| 5617 | fib6_info_nh_uses_dev, |
| 5618 | _dev); |
| 5619 | } |
| 5620 | |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 5621 | if (f6i->fib6_nh->fib_nh_dev == dev) |
David Ahern | 13e3890 | 2018-10-15 18:56:44 -0700 | [diff] [blame] | 5622 | return true; |
| 5623 | |
| 5624 | if (f6i->fib6_nsiblings) { |
| 5625 | struct fib6_info *sibling, *next_sibling; |
| 5626 | |
| 5627 | list_for_each_entry_safe(sibling, next_sibling, |
| 5628 | &f6i->fib6_siblings, fib6_siblings) { |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 5629 | if (sibling->fib6_nh->fib_nh_dev == dev) |
David Ahern | 13e3890 | 2018-10-15 18:56:44 -0700 | [diff] [blame] | 5630 | return true; |
| 5631 | } |
| 5632 | } |
| 5633 | |
| 5634 | return false; |
| 5635 | } |
| 5636 | |
Stefano Brivio | 1e47b48 | 2019-06-21 17:45:27 +0200 | [diff] [blame] | 5637 | struct fib6_nh_exception_dump_walker { |
| 5638 | struct rt6_rtnl_dump_arg *dump; |
| 5639 | struct fib6_info *rt; |
| 5640 | unsigned int flags; |
| 5641 | unsigned int skip; |
| 5642 | unsigned int count; |
| 5643 | }; |
| 5644 | |
| 5645 | static int rt6_nh_dump_exceptions(struct fib6_nh *nh, void *arg) |
| 5646 | { |
| 5647 | struct fib6_nh_exception_dump_walker *w = arg; |
| 5648 | struct rt6_rtnl_dump_arg *dump = w->dump; |
| 5649 | struct rt6_exception_bucket *bucket; |
| 5650 | struct rt6_exception *rt6_ex; |
| 5651 | int i, err; |
| 5652 | |
| 5653 | bucket = fib6_nh_get_excptn_bucket(nh, NULL); |
| 5654 | if (!bucket) |
| 5655 | return 0; |
| 5656 | |
| 5657 | for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) { |
| 5658 | hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) { |
| 5659 | if (w->skip) { |
| 5660 | w->skip--; |
| 5661 | continue; |
| 5662 | } |
| 5663 | |
| 5664 | /* Expiration of entries doesn't bump sernum, insertion |
| 5665 | * does. Removal is triggered by insertion, so we can |
| 5666 | * rely on the fact that if entries change between two |
| 5667 | * partial dumps, this node is scanned again completely, |
| 5668 | * see rt6_insert_exception() and fib6_dump_table(). |
| 5669 | * |
| 5670 | * Count expired entries we go through as handled |
| 5671 | * entries that we'll skip next time, in case of partial |
| 5672 | * node dump. Otherwise, if entries expire meanwhile, |
| 5673 | * we'll skip the wrong amount. |
| 5674 | */ |
| 5675 | if (rt6_check_expired(rt6_ex->rt6i)) { |
| 5676 | w->count++; |
| 5677 | continue; |
| 5678 | } |
| 5679 | |
| 5680 | err = rt6_fill_node(dump->net, dump->skb, w->rt, |
| 5681 | &rt6_ex->rt6i->dst, NULL, NULL, 0, |
| 5682 | RTM_NEWROUTE, |
| 5683 | NETLINK_CB(dump->cb->skb).portid, |
| 5684 | dump->cb->nlh->nlmsg_seq, w->flags); |
| 5685 | if (err) |
| 5686 | return err; |
| 5687 | |
| 5688 | w->count++; |
| 5689 | } |
| 5690 | bucket++; |
| 5691 | } |
| 5692 | |
| 5693 | return 0; |
| 5694 | } |
| 5695 | |
Stefano Brivio | bf9a8a061 | 2019-06-21 17:45:26 +0200 | [diff] [blame] | 5696 | /* 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] | 5697 | 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] | 5698 | { |
| 5699 | 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] | 5700 | struct fib_dump_filter *filter = &arg->filter; |
| 5701 | unsigned int flags = NLM_F_MULTI; |
David Ahern | 1f17e2f | 2017-01-26 13:54:08 -0800 | [diff] [blame] | 5702 | struct net *net = arg->net; |
Stefano Brivio | 1e47b48 | 2019-06-21 17:45:27 +0200 | [diff] [blame] | 5703 | int count = 0; |
David Ahern | 1f17e2f | 2017-01-26 13:54:08 -0800 | [diff] [blame] | 5704 | |
David Ahern | 421842e | 2018-04-17 17:33:18 -0700 | [diff] [blame] | 5705 | if (rt == net->ipv6.fib6_null_entry) |
Stefano Brivio | bf9a8a061 | 2019-06-21 17:45:26 +0200 | [diff] [blame] | 5706 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5707 | |
David Ahern | 13e3890 | 2018-10-15 18:56:44 -0700 | [diff] [blame] | 5708 | if ((filter->flags & RTM_F_PREFIX) && |
| 5709 | !(rt->fib6_flags & RTF_PREFIX_RT)) { |
| 5710 | /* success since this is not a prefix route */ |
Stefano Brivio | bf9a8a061 | 2019-06-21 17:45:26 +0200 | [diff] [blame] | 5711 | return -1; |
David Ahern | 13e3890 | 2018-10-15 18:56:44 -0700 | [diff] [blame] | 5712 | } |
Stefano Brivio | 1e47b48 | 2019-06-21 17:45:27 +0200 | [diff] [blame] | 5713 | if (filter->filter_set && |
| 5714 | ((filter->rt_type && rt->fib6_type != filter->rt_type) || |
| 5715 | (filter->dev && !fib6_info_uses_dev(rt, filter->dev)) || |
| 5716 | (filter->protocol && rt->fib6_protocol != filter->protocol))) { |
| 5717 | return -1; |
| 5718 | } |
| 5719 | |
| 5720 | if (filter->filter_set || |
| 5721 | !filter->dump_routes || !filter->dump_exceptions) { |
David Ahern | 13e3890 | 2018-10-15 18:56:44 -0700 | [diff] [blame] | 5722 | flags |= NLM_F_DUMP_FILTERED; |
David Ahern | f8cfe2c | 2017-01-17 15:51:08 -0800 | [diff] [blame] | 5723 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5724 | |
Stefano Brivio | 1e47b48 | 2019-06-21 17:45:27 +0200 | [diff] [blame] | 5725 | if (filter->dump_routes) { |
| 5726 | if (skip) { |
| 5727 | skip--; |
| 5728 | } else { |
| 5729 | if (rt6_fill_node(net, arg->skb, rt, NULL, NULL, NULL, |
| 5730 | 0, RTM_NEWROUTE, |
| 5731 | NETLINK_CB(arg->cb->skb).portid, |
| 5732 | arg->cb->nlh->nlmsg_seq, flags)) { |
| 5733 | return 0; |
| 5734 | } |
| 5735 | count++; |
| 5736 | } |
| 5737 | } |
| 5738 | |
| 5739 | if (filter->dump_exceptions) { |
| 5740 | struct fib6_nh_exception_dump_walker w = { .dump = arg, |
| 5741 | .rt = rt, |
| 5742 | .flags = flags, |
| 5743 | .skip = skip, |
| 5744 | .count = 0 }; |
| 5745 | int err; |
| 5746 | |
Eric Dumazet | 3b52569 | 2019-06-26 03:05:28 -0700 | [diff] [blame] | 5747 | rcu_read_lock(); |
Stefano Brivio | 1e47b48 | 2019-06-21 17:45:27 +0200 | [diff] [blame] | 5748 | if (rt->nh) { |
| 5749 | err = nexthop_for_each_fib6_nh(rt->nh, |
| 5750 | rt6_nh_dump_exceptions, |
| 5751 | &w); |
| 5752 | } else { |
| 5753 | err = rt6_nh_dump_exceptions(rt->fib6_nh, &w); |
| 5754 | } |
Eric Dumazet | 3b52569 | 2019-06-26 03:05:28 -0700 | [diff] [blame] | 5755 | rcu_read_unlock(); |
Stefano Brivio | 1e47b48 | 2019-06-21 17:45:27 +0200 | [diff] [blame] | 5756 | |
| 5757 | if (err) |
| 5758 | return count += w.count; |
| 5759 | } |
Stefano Brivio | bf9a8a061 | 2019-06-21 17:45:26 +0200 | [diff] [blame] | 5760 | |
| 5761 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5762 | } |
| 5763 | |
Jakub Kicinski | 0eff0a2 | 2019-01-18 10:46:24 -0800 | [diff] [blame] | 5764 | static int inet6_rtm_valid_getroute_req(struct sk_buff *skb, |
| 5765 | const struct nlmsghdr *nlh, |
| 5766 | struct nlattr **tb, |
| 5767 | struct netlink_ext_ack *extack) |
| 5768 | { |
| 5769 | struct rtmsg *rtm; |
| 5770 | int i, err; |
| 5771 | |
| 5772 | if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*rtm))) { |
| 5773 | NL_SET_ERR_MSG_MOD(extack, |
| 5774 | "Invalid header for get route request"); |
| 5775 | return -EINVAL; |
| 5776 | } |
| 5777 | |
| 5778 | if (!netlink_strict_get_check(skb)) |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 5779 | return nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX, |
| 5780 | rtm_ipv6_policy, extack); |
Jakub Kicinski | 0eff0a2 | 2019-01-18 10:46:24 -0800 | [diff] [blame] | 5781 | |
| 5782 | rtm = nlmsg_data(nlh); |
| 5783 | if ((rtm->rtm_src_len && rtm->rtm_src_len != 128) || |
| 5784 | (rtm->rtm_dst_len && rtm->rtm_dst_len != 128) || |
| 5785 | rtm->rtm_table || rtm->rtm_protocol || rtm->rtm_scope || |
| 5786 | rtm->rtm_type) { |
| 5787 | NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for get route request"); |
| 5788 | return -EINVAL; |
| 5789 | } |
| 5790 | if (rtm->rtm_flags & ~RTM_F_FIB_MATCH) { |
| 5791 | NL_SET_ERR_MSG_MOD(extack, |
| 5792 | "Invalid flags for get route request"); |
| 5793 | return -EINVAL; |
| 5794 | } |
| 5795 | |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 5796 | err = nlmsg_parse_deprecated_strict(nlh, sizeof(*rtm), tb, RTA_MAX, |
| 5797 | rtm_ipv6_policy, extack); |
Jakub Kicinski | 0eff0a2 | 2019-01-18 10:46:24 -0800 | [diff] [blame] | 5798 | if (err) |
| 5799 | return err; |
| 5800 | |
| 5801 | if ((tb[RTA_SRC] && !rtm->rtm_src_len) || |
| 5802 | (tb[RTA_DST] && !rtm->rtm_dst_len)) { |
| 5803 | NL_SET_ERR_MSG_MOD(extack, "rtm_src_len and rtm_dst_len must be 128 for IPv6"); |
| 5804 | return -EINVAL; |
| 5805 | } |
| 5806 | |
| 5807 | for (i = 0; i <= RTA_MAX; i++) { |
| 5808 | if (!tb[i]) |
| 5809 | continue; |
| 5810 | |
| 5811 | switch (i) { |
| 5812 | case RTA_SRC: |
| 5813 | case RTA_DST: |
| 5814 | case RTA_IIF: |
| 5815 | case RTA_OIF: |
| 5816 | case RTA_MARK: |
| 5817 | case RTA_UID: |
| 5818 | case RTA_SPORT: |
| 5819 | case RTA_DPORT: |
| 5820 | case RTA_IP_PROTO: |
| 5821 | break; |
| 5822 | default: |
| 5823 | NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in get route request"); |
| 5824 | return -EINVAL; |
| 5825 | } |
| 5826 | } |
| 5827 | |
| 5828 | return 0; |
| 5829 | } |
| 5830 | |
David Ahern | c21ef3e | 2017-04-16 09:48:24 -0700 | [diff] [blame] | 5831 | static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh, |
| 5832 | struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5833 | { |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 5834 | struct net *net = sock_net(in_skb->sk); |
Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 5835 | struct nlattr *tb[RTA_MAX+1]; |
Roopa Prabhu | 18c3a61 | 2017-05-25 10:42:40 -0700 | [diff] [blame] | 5836 | int err, iif = 0, oif = 0; |
David Ahern | a68886a | 2018-04-20 15:38:02 -0700 | [diff] [blame] | 5837 | struct fib6_info *from; |
Roopa Prabhu | 18c3a61 | 2017-05-25 10:42:40 -0700 | [diff] [blame] | 5838 | struct dst_entry *dst; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5839 | struct rt6_info *rt; |
Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 5840 | struct sk_buff *skb; |
| 5841 | struct rtmsg *rtm; |
Maciej Żenczykowski | 744486d | 2018-09-29 23:44:54 -0700 | [diff] [blame] | 5842 | struct flowi6 fl6 = {}; |
Roopa Prabhu | 18c3a61 | 2017-05-25 10:42:40 -0700 | [diff] [blame] | 5843 | bool fibmatch; |
Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 5844 | |
Jakub Kicinski | 0eff0a2 | 2019-01-18 10:46:24 -0800 | [diff] [blame] | 5845 | err = inet6_rtm_valid_getroute_req(in_skb, nlh, tb, extack); |
Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 5846 | if (err < 0) |
| 5847 | goto errout; |
| 5848 | |
| 5849 | err = -EINVAL; |
Hannes Frederic Sowa | 38b7097 | 2016-06-11 20:08:19 +0200 | [diff] [blame] | 5850 | rtm = nlmsg_data(nlh); |
| 5851 | fl6.flowlabel = ip6_make_flowinfo(rtm->rtm_tos, 0); |
Roopa Prabhu | 18c3a61 | 2017-05-25 10:42:40 -0700 | [diff] [blame] | 5852 | fibmatch = !!(rtm->rtm_flags & RTM_F_FIB_MATCH); |
Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 5853 | |
| 5854 | if (tb[RTA_SRC]) { |
| 5855 | if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr)) |
| 5856 | goto errout; |
| 5857 | |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 5858 | fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]); |
Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 5859 | } |
| 5860 | |
| 5861 | if (tb[RTA_DST]) { |
| 5862 | if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr)) |
| 5863 | goto errout; |
| 5864 | |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 5865 | fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]); |
Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 5866 | } |
| 5867 | |
| 5868 | if (tb[RTA_IIF]) |
| 5869 | iif = nla_get_u32(tb[RTA_IIF]); |
| 5870 | |
| 5871 | if (tb[RTA_OIF]) |
Shmulik Ladkani | 72331bc | 2012-04-01 04:03:45 +0000 | [diff] [blame] | 5872 | oif = nla_get_u32(tb[RTA_OIF]); |
Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 5873 | |
Lorenzo Colitti | 2e47b29 | 2014-05-15 16:38:41 -0700 | [diff] [blame] | 5874 | if (tb[RTA_MARK]) |
| 5875 | fl6.flowi6_mark = nla_get_u32(tb[RTA_MARK]); |
| 5876 | |
Lorenzo Colitti | 622ec2c | 2016-11-04 02:23:42 +0900 | [diff] [blame] | 5877 | if (tb[RTA_UID]) |
| 5878 | fl6.flowi6_uid = make_kuid(current_user_ns(), |
| 5879 | nla_get_u32(tb[RTA_UID])); |
| 5880 | else |
| 5881 | fl6.flowi6_uid = iif ? INVALID_UID : current_uid(); |
| 5882 | |
Roopa Prabhu | eacb938 | 2018-05-22 14:03:28 -0700 | [diff] [blame] | 5883 | if (tb[RTA_SPORT]) |
| 5884 | fl6.fl6_sport = nla_get_be16(tb[RTA_SPORT]); |
| 5885 | |
| 5886 | if (tb[RTA_DPORT]) |
| 5887 | fl6.fl6_dport = nla_get_be16(tb[RTA_DPORT]); |
| 5888 | |
| 5889 | if (tb[RTA_IP_PROTO]) { |
| 5890 | err = rtm_getroute_parse_ip_proto(tb[RTA_IP_PROTO], |
Hangbin Liu | 5e1a99e | 2019-02-27 16:15:29 +0800 | [diff] [blame] | 5891 | &fl6.flowi6_proto, AF_INET6, |
| 5892 | extack); |
Roopa Prabhu | eacb938 | 2018-05-22 14:03:28 -0700 | [diff] [blame] | 5893 | if (err) |
| 5894 | goto errout; |
| 5895 | } |
| 5896 | |
Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 5897 | if (iif) { |
| 5898 | struct net_device *dev; |
Shmulik Ladkani | 72331bc | 2012-04-01 04:03:45 +0000 | [diff] [blame] | 5899 | int flags = 0; |
| 5900 | |
Florian Westphal | 121622d | 2017-08-15 16:34:42 +0200 | [diff] [blame] | 5901 | rcu_read_lock(); |
| 5902 | |
| 5903 | dev = dev_get_by_index_rcu(net, iif); |
Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 5904 | if (!dev) { |
Florian Westphal | 121622d | 2017-08-15 16:34:42 +0200 | [diff] [blame] | 5905 | rcu_read_unlock(); |
Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 5906 | err = -ENODEV; |
| 5907 | goto errout; |
| 5908 | } |
Shmulik Ladkani | 72331bc | 2012-04-01 04:03:45 +0000 | [diff] [blame] | 5909 | |
| 5910 | fl6.flowi6_iif = iif; |
| 5911 | |
| 5912 | if (!ipv6_addr_any(&fl6.saddr)) |
| 5913 | flags |= RT6_LOOKUP_F_HAS_SADDR; |
| 5914 | |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 5915 | dst = ip6_route_input_lookup(net, dev, &fl6, NULL, flags); |
Florian Westphal | 121622d | 2017-08-15 16:34:42 +0200 | [diff] [blame] | 5916 | |
| 5917 | rcu_read_unlock(); |
Shmulik Ladkani | 72331bc | 2012-04-01 04:03:45 +0000 | [diff] [blame] | 5918 | } else { |
| 5919 | fl6.flowi6_oif = oif; |
| 5920 | |
Ido Schimmel | 58acfd7 | 2017-12-20 12:28:25 +0200 | [diff] [blame] | 5921 | dst = ip6_route_output(net, NULL, &fl6); |
Roopa Prabhu | 18c3a61 | 2017-05-25 10:42:40 -0700 | [diff] [blame] | 5922 | } |
| 5923 | |
Roopa Prabhu | 18c3a61 | 2017-05-25 10:42:40 -0700 | [diff] [blame] | 5924 | |
| 5925 | rt = container_of(dst, struct rt6_info, dst); |
| 5926 | if (rt->dst.error) { |
| 5927 | err = rt->dst.error; |
| 5928 | ip6_rt_put(rt); |
| 5929 | goto errout; |
Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 5930 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5931 | |
WANG Cong | 9d6acb3 | 2017-03-01 20:48:39 -0800 | [diff] [blame] | 5932 | if (rt == net->ipv6.ip6_null_entry) { |
| 5933 | err = rt->dst.error; |
| 5934 | ip6_rt_put(rt); |
| 5935 | goto errout; |
| 5936 | } |
| 5937 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5938 | skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); |
David S. Miller | 3830847 | 2011-12-03 18:02:47 -0500 | [diff] [blame] | 5939 | if (!skb) { |
Amerigo Wang | 94e187c | 2012-10-29 00:13:19 +0000 | [diff] [blame] | 5940 | ip6_rt_put(rt); |
Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 5941 | err = -ENOBUFS; |
| 5942 | goto errout; |
| 5943 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5944 | |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 5945 | skb_dst_set(skb, &rt->dst); |
David Ahern | a68886a | 2018-04-20 15:38:02 -0700 | [diff] [blame] | 5946 | |
| 5947 | rcu_read_lock(); |
| 5948 | from = rcu_dereference(rt->from); |
Martin KaFai Lau | 886b7a5 | 2019-04-30 10:45:12 -0700 | [diff] [blame] | 5949 | if (from) { |
| 5950 | if (fibmatch) |
| 5951 | err = rt6_fill_node(net, skb, from, NULL, NULL, NULL, |
| 5952 | iif, RTM_NEWROUTE, |
| 5953 | NETLINK_CB(in_skb).portid, |
| 5954 | nlh->nlmsg_seq, 0); |
| 5955 | else |
| 5956 | err = rt6_fill_node(net, skb, from, dst, &fl6.daddr, |
| 5957 | &fl6.saddr, iif, RTM_NEWROUTE, |
| 5958 | NETLINK_CB(in_skb).portid, |
| 5959 | nlh->nlmsg_seq, 0); |
| 5960 | } else { |
| 5961 | err = -ENETUNREACH; |
| 5962 | } |
David Ahern | a68886a | 2018-04-20 15:38:02 -0700 | [diff] [blame] | 5963 | rcu_read_unlock(); |
| 5964 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5965 | if (err < 0) { |
Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 5966 | kfree_skb(skb); |
| 5967 | goto errout; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5968 | } |
| 5969 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 5970 | err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid); |
Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 5971 | errout: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5972 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5973 | } |
| 5974 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 5975 | 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] | 5976 | unsigned int nlm_flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5977 | { |
| 5978 | struct sk_buff *skb; |
Daniel Lezcano | 5578689 | 2008-03-04 13:47:47 -0800 | [diff] [blame] | 5979 | struct net *net = info->nl_net; |
Denis V. Lunev | 528c4ce | 2007-12-13 09:45:12 -0800 | [diff] [blame] | 5980 | u32 seq; |
| 5981 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5982 | |
Denis V. Lunev | 528c4ce | 2007-12-13 09:45:12 -0800 | [diff] [blame] | 5983 | err = -ENOBUFS; |
David S. Miller | 3830847 | 2011-12-03 18:02:47 -0500 | [diff] [blame] | 5984 | seq = info->nlh ? info->nlh->nlmsg_seq : 0; |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 5985 | |
Roopa Prabhu | 19e42e4 | 2015-07-21 10:43:48 +0200 | [diff] [blame] | 5986 | skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any()); |
David S. Miller | 3830847 | 2011-12-03 18:02:47 -0500 | [diff] [blame] | 5987 | if (!skb) |
Thomas Graf | 21713eb | 2006-08-15 00:35:24 -0700 | [diff] [blame] | 5988 | goto errout; |
| 5989 | |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 5990 | err = rt6_fill_node(net, skb, rt, NULL, NULL, NULL, 0, |
| 5991 | event, info->portid, seq, nlm_flags); |
Patrick McHardy | 2693256 | 2007-01-31 23:16:40 -0800 | [diff] [blame] | 5992 | if (err < 0) { |
| 5993 | /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */ |
| 5994 | WARN_ON(err == -EMSGSIZE); |
| 5995 | kfree_skb(skb); |
| 5996 | goto errout; |
| 5997 | } |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 5998 | rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE, |
Pablo Neira Ayuso | 1ce85fe | 2009-02-24 23:18:28 -0800 | [diff] [blame] | 5999 | info->nlh, gfp_any()); |
| 6000 | return; |
Thomas Graf | 21713eb | 2006-08-15 00:35:24 -0700 | [diff] [blame] | 6001 | errout: |
| 6002 | if (err < 0) |
Daniel Lezcano | 5578689 | 2008-03-04 13:47:47 -0800 | [diff] [blame] | 6003 | rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6004 | } |
| 6005 | |
David Ahern | 19a3b7e | 2019-05-22 12:04:41 -0700 | [diff] [blame] | 6006 | void fib6_rt_update(struct net *net, struct fib6_info *rt, |
| 6007 | struct nl_info *info) |
| 6008 | { |
| 6009 | u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0; |
| 6010 | struct sk_buff *skb; |
| 6011 | int err = -ENOBUFS; |
| 6012 | |
| 6013 | /* call_fib6_entry_notifiers will be removed when in-kernel notifier |
| 6014 | * is implemented and supported for nexthop objects |
| 6015 | */ |
| 6016 | call_fib6_entry_notifiers(net, FIB_EVENT_ENTRY_REPLACE, rt, NULL); |
| 6017 | |
| 6018 | skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any()); |
| 6019 | if (!skb) |
| 6020 | goto errout; |
| 6021 | |
| 6022 | err = rt6_fill_node(net, skb, rt, NULL, NULL, NULL, 0, |
| 6023 | RTM_NEWROUTE, info->portid, seq, NLM_F_REPLACE); |
| 6024 | if (err < 0) { |
| 6025 | /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */ |
| 6026 | WARN_ON(err == -EMSGSIZE); |
| 6027 | kfree_skb(skb); |
| 6028 | goto errout; |
| 6029 | } |
| 6030 | rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE, |
| 6031 | info->nlh, gfp_any()); |
| 6032 | return; |
| 6033 | errout: |
| 6034 | if (err < 0) |
| 6035 | rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err); |
| 6036 | } |
| 6037 | |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 6038 | static int ip6_route_dev_notify(struct notifier_block *this, |
Jiri Pirko | 351638e | 2013-05-28 01:30:21 +0000 | [diff] [blame] | 6039 | unsigned long event, void *ptr) |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 6040 | { |
Jiri Pirko | 351638e | 2013-05-28 01:30:21 +0000 | [diff] [blame] | 6041 | struct net_device *dev = netdev_notifier_info_to_dev(ptr); |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 6042 | struct net *net = dev_net(dev); |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 6043 | |
WANG Cong | 242d3a4 | 2017-05-08 10:12:13 -0700 | [diff] [blame] | 6044 | if (!(dev->flags & IFF_LOOPBACK)) |
| 6045 | return NOTIFY_OK; |
| 6046 | |
| 6047 | if (event == NETDEV_REGISTER) { |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 6048 | net->ipv6.fib6_null_entry->fib6_nh->fib_nh_dev = dev; |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 6049 | net->ipv6.ip6_null_entry->dst.dev = dev; |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 6050 | net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev); |
| 6051 | #ifdef CONFIG_IPV6_MULTIPLE_TABLES |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 6052 | net->ipv6.ip6_prohibit_entry->dst.dev = dev; |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 6053 | net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev); |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 6054 | net->ipv6.ip6_blk_hole_entry->dst.dev = dev; |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 6055 | net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev); |
| 6056 | #endif |
WANG Cong | 76da070 | 2017-06-20 11:42:27 -0700 | [diff] [blame] | 6057 | } else if (event == NETDEV_UNREGISTER && |
| 6058 | dev->reg_state != NETREG_UNREGISTERED) { |
| 6059 | /* NETDEV_UNREGISTER could be fired for multiple times by |
| 6060 | * netdev_wait_allrefs(). Make sure we only call this once. |
| 6061 | */ |
Eric Dumazet | 12d94a8 | 2017-08-15 04:09:51 -0700 | [diff] [blame] | 6062 | in6_dev_put_clear(&net->ipv6.ip6_null_entry->rt6i_idev); |
WANG Cong | 242d3a4 | 2017-05-08 10:12:13 -0700 | [diff] [blame] | 6063 | #ifdef CONFIG_IPV6_MULTIPLE_TABLES |
Eric Dumazet | 12d94a8 | 2017-08-15 04:09:51 -0700 | [diff] [blame] | 6064 | in6_dev_put_clear(&net->ipv6.ip6_prohibit_entry->rt6i_idev); |
| 6065 | in6_dev_put_clear(&net->ipv6.ip6_blk_hole_entry->rt6i_idev); |
WANG Cong | 242d3a4 | 2017-05-08 10:12:13 -0700 | [diff] [blame] | 6066 | #endif |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 6067 | } |
| 6068 | |
| 6069 | return NOTIFY_OK; |
| 6070 | } |
| 6071 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6072 | /* |
| 6073 | * /proc |
| 6074 | */ |
| 6075 | |
| 6076 | #ifdef CONFIG_PROC_FS |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6077 | static int rt6_stats_seq_show(struct seq_file *seq, void *v) |
| 6078 | { |
Daniel Lezcano | 69ddb80 | 2008-03-04 13:46:23 -0800 | [diff] [blame] | 6079 | struct net *net = (struct net *)seq->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6080 | seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n", |
Daniel Lezcano | 69ddb80 | 2008-03-04 13:46:23 -0800 | [diff] [blame] | 6081 | net->ipv6.rt6_stats->fib_nodes, |
| 6082 | net->ipv6.rt6_stats->fib_route_nodes, |
Wei Wang | 81eb844 | 2017-10-06 12:06:11 -0700 | [diff] [blame] | 6083 | atomic_read(&net->ipv6.rt6_stats->fib_rt_alloc), |
Daniel Lezcano | 69ddb80 | 2008-03-04 13:46:23 -0800 | [diff] [blame] | 6084 | net->ipv6.rt6_stats->fib_rt_entries, |
| 6085 | net->ipv6.rt6_stats->fib_rt_cache, |
Eric Dumazet | fc66f95 | 2010-10-08 06:37:34 +0000 | [diff] [blame] | 6086 | dst_entries_get_slow(&net->ipv6.ip6_dst_ops), |
Daniel Lezcano | 69ddb80 | 2008-03-04 13:46:23 -0800 | [diff] [blame] | 6087 | net->ipv6.rt6_stats->fib_discarded_routes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6088 | |
| 6089 | return 0; |
| 6090 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6091 | #endif /* CONFIG_PROC_FS */ |
| 6092 | |
| 6093 | #ifdef CONFIG_SYSCTL |
| 6094 | |
Christoph Hellwig | 3292739 | 2020-04-24 08:43:38 +0200 | [diff] [blame] | 6095 | static int ipv6_sysctl_rtcache_flush(struct ctl_table *ctl, int write, |
| 6096 | void *buffer, size_t *lenp, loff_t *ppos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6097 | { |
Lucian Adrian Grijincu | c486da3 | 2011-02-24 19:48:03 +0000 | [diff] [blame] | 6098 | struct net *net; |
| 6099 | int delay; |
Aditya Pakki | f0fb9b2 | 2018-12-24 10:30:17 -0600 | [diff] [blame] | 6100 | int ret; |
Lucian Adrian Grijincu | c486da3 | 2011-02-24 19:48:03 +0000 | [diff] [blame] | 6101 | if (!write) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6102 | return -EINVAL; |
Lucian Adrian Grijincu | c486da3 | 2011-02-24 19:48:03 +0000 | [diff] [blame] | 6103 | |
| 6104 | net = (struct net *)ctl->extra1; |
| 6105 | delay = net->ipv6.sysctl.flush_delay; |
Aditya Pakki | f0fb9b2 | 2018-12-24 10:30:17 -0600 | [diff] [blame] | 6106 | ret = proc_dointvec(ctl, write, buffer, lenp, ppos); |
| 6107 | if (ret) |
| 6108 | return ret; |
| 6109 | |
Michal Kubeček | 2ac3ac8 | 2013-08-01 10:04:14 +0200 | [diff] [blame] | 6110 | 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] | 6111 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6112 | } |
| 6113 | |
David Ahern | ed792e2 | 2018-10-08 14:06:34 -0700 | [diff] [blame] | 6114 | static struct ctl_table ipv6_route_table_template[] = { |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 6115 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6116 | .procname = "flush", |
Daniel Lezcano | 4990509 | 2008-01-10 03:01:01 -0800 | [diff] [blame] | 6117 | .data = &init_net.ipv6.sysctl.flush_delay, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6118 | .maxlen = sizeof(int), |
Dave Jones | 89c8b3a1 | 2005-04-28 12:11:49 -0700 | [diff] [blame] | 6119 | .mode = 0200, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 6120 | .proc_handler = ipv6_sysctl_rtcache_flush |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6121 | }, |
| 6122 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6123 | .procname = "gc_thresh", |
Daniel Lezcano | 9a7ec3a | 2008-03-04 13:48:53 -0800 | [diff] [blame] | 6124 | .data = &ip6_dst_ops_template.gc_thresh, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6125 | .maxlen = sizeof(int), |
| 6126 | .mode = 0644, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 6127 | .proc_handler = proc_dointvec, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6128 | }, |
| 6129 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6130 | .procname = "max_size", |
Daniel Lezcano | 4990509 | 2008-01-10 03:01:01 -0800 | [diff] [blame] | 6131 | .data = &init_net.ipv6.sysctl.ip6_rt_max_size, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6132 | .maxlen = sizeof(int), |
| 6133 | .mode = 0644, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 6134 | .proc_handler = proc_dointvec, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6135 | }, |
| 6136 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6137 | .procname = "gc_min_interval", |
Daniel Lezcano | 4990509 | 2008-01-10 03:01:01 -0800 | [diff] [blame] | 6138 | .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6139 | .maxlen = sizeof(int), |
| 6140 | .mode = 0644, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 6141 | .proc_handler = proc_dointvec_jiffies, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6142 | }, |
| 6143 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6144 | .procname = "gc_timeout", |
Daniel Lezcano | 4990509 | 2008-01-10 03:01:01 -0800 | [diff] [blame] | 6145 | .data = &init_net.ipv6.sysctl.ip6_rt_gc_timeout, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6146 | .maxlen = sizeof(int), |
| 6147 | .mode = 0644, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 6148 | .proc_handler = proc_dointvec_jiffies, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6149 | }, |
| 6150 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6151 | .procname = "gc_interval", |
Daniel Lezcano | 4990509 | 2008-01-10 03:01:01 -0800 | [diff] [blame] | 6152 | .data = &init_net.ipv6.sysctl.ip6_rt_gc_interval, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6153 | .maxlen = sizeof(int), |
| 6154 | .mode = 0644, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 6155 | .proc_handler = proc_dointvec_jiffies, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6156 | }, |
| 6157 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6158 | .procname = "gc_elasticity", |
Daniel Lezcano | 4990509 | 2008-01-10 03:01:01 -0800 | [diff] [blame] | 6159 | .data = &init_net.ipv6.sysctl.ip6_rt_gc_elasticity, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6160 | .maxlen = sizeof(int), |
| 6161 | .mode = 0644, |
Min Zhang | f3d3f61 | 2010-08-14 22:42:51 -0700 | [diff] [blame] | 6162 | .proc_handler = proc_dointvec, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6163 | }, |
| 6164 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6165 | .procname = "mtu_expires", |
Daniel Lezcano | 4990509 | 2008-01-10 03:01:01 -0800 | [diff] [blame] | 6166 | .data = &init_net.ipv6.sysctl.ip6_rt_mtu_expires, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6167 | .maxlen = sizeof(int), |
| 6168 | .mode = 0644, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 6169 | .proc_handler = proc_dointvec_jiffies, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6170 | }, |
| 6171 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6172 | .procname = "min_adv_mss", |
Daniel Lezcano | 4990509 | 2008-01-10 03:01:01 -0800 | [diff] [blame] | 6173 | .data = &init_net.ipv6.sysctl.ip6_rt_min_advmss, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6174 | .maxlen = sizeof(int), |
| 6175 | .mode = 0644, |
Min Zhang | f3d3f61 | 2010-08-14 22:42:51 -0700 | [diff] [blame] | 6176 | .proc_handler = proc_dointvec, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6177 | }, |
| 6178 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6179 | .procname = "gc_min_interval_ms", |
Daniel Lezcano | 4990509 | 2008-01-10 03:01:01 -0800 | [diff] [blame] | 6180 | .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6181 | .maxlen = sizeof(int), |
| 6182 | .mode = 0644, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 6183 | .proc_handler = proc_dointvec_ms_jiffies, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6184 | }, |
David Ahern | 7c6bb7d | 2018-10-11 20:17:21 -0700 | [diff] [blame] | 6185 | { |
| 6186 | .procname = "skip_notify_on_dev_down", |
| 6187 | .data = &init_net.ipv6.sysctl.skip_notify_on_dev_down, |
| 6188 | .maxlen = sizeof(int), |
| 6189 | .mode = 0644, |
Eiichi Tsukata | b8e8a86 | 2019-06-25 12:08:01 +0900 | [diff] [blame] | 6190 | .proc_handler = proc_dointvec_minmax, |
Matteo Croce | eec4844 | 2019-07-18 15:58:50 -0700 | [diff] [blame] | 6191 | .extra1 = SYSCTL_ZERO, |
| 6192 | .extra2 = SYSCTL_ONE, |
David Ahern | 7c6bb7d | 2018-10-11 20:17:21 -0700 | [diff] [blame] | 6193 | }, |
Eric W. Biederman | f8572d8 | 2009-11-05 13:32:03 -0800 | [diff] [blame] | 6194 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6195 | }; |
| 6196 | |
Alexey Dobriyan | 2c8c1e7 | 2010-01-17 03:35:32 +0000 | [diff] [blame] | 6197 | struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net) |
Daniel Lezcano | 760f2d0 | 2008-01-10 02:53:43 -0800 | [diff] [blame] | 6198 | { |
| 6199 | struct ctl_table *table; |
| 6200 | |
| 6201 | table = kmemdup(ipv6_route_table_template, |
| 6202 | sizeof(ipv6_route_table_template), |
| 6203 | GFP_KERNEL); |
YOSHIFUJI Hideaki | 5ee0910 | 2008-02-28 00:24:28 +0900 | [diff] [blame] | 6204 | |
| 6205 | if (table) { |
| 6206 | table[0].data = &net->ipv6.sysctl.flush_delay; |
Lucian Adrian Grijincu | c486da3 | 2011-02-24 19:48:03 +0000 | [diff] [blame] | 6207 | table[0].extra1 = net; |
Alexey Dobriyan | 86393e5 | 2009-08-29 01:34:49 +0000 | [diff] [blame] | 6208 | table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh; |
YOSHIFUJI Hideaki | 5ee0910 | 2008-02-28 00:24:28 +0900 | [diff] [blame] | 6209 | table[2].data = &net->ipv6.sysctl.ip6_rt_max_size; |
| 6210 | table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval; |
| 6211 | table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout; |
| 6212 | table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval; |
| 6213 | table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity; |
| 6214 | table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires; |
| 6215 | table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss; |
Alexey Dobriyan | 9c69fab | 2009-12-18 20:11:03 -0800 | [diff] [blame] | 6216 | table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval; |
David Ahern | 7c6bb7d | 2018-10-11 20:17:21 -0700 | [diff] [blame] | 6217 | table[10].data = &net->ipv6.sysctl.skip_notify_on_dev_down; |
Eric W. Biederman | 464dc80 | 2012-11-16 03:02:59 +0000 | [diff] [blame] | 6218 | |
| 6219 | /* Don't export sysctls to unprivileged users */ |
| 6220 | if (net->user_ns != &init_user_ns) |
| 6221 | table[0].procname = NULL; |
YOSHIFUJI Hideaki | 5ee0910 | 2008-02-28 00:24:28 +0900 | [diff] [blame] | 6222 | } |
| 6223 | |
Daniel Lezcano | 760f2d0 | 2008-01-10 02:53:43 -0800 | [diff] [blame] | 6224 | return table; |
| 6225 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6226 | #endif |
| 6227 | |
Alexey Dobriyan | 2c8c1e7 | 2010-01-17 03:35:32 +0000 | [diff] [blame] | 6228 | static int __net_init ip6_route_net_init(struct net *net) |
Daniel Lezcano | cdb1876 | 2008-03-04 13:45:33 -0800 | [diff] [blame] | 6229 | { |
Pavel Emelyanov | 633d424b | 2008-04-21 14:25:23 -0700 | [diff] [blame] | 6230 | int ret = -ENOMEM; |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 6231 | |
Alexey Dobriyan | 86393e5 | 2009-08-29 01:34:49 +0000 | [diff] [blame] | 6232 | memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template, |
| 6233 | sizeof(net->ipv6.ip6_dst_ops)); |
Benjamin Thery | f2fc6a5 | 2008-03-04 13:49:23 -0800 | [diff] [blame] | 6234 | |
Eric Dumazet | fc66f95 | 2010-10-08 06:37:34 +0000 | [diff] [blame] | 6235 | if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0) |
| 6236 | goto out_ip6_dst_ops; |
| 6237 | |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 6238 | net->ipv6.fib6_null_entry = fib6_info_alloc(GFP_KERNEL, true); |
David Ahern | 421842e | 2018-04-17 17:33:18 -0700 | [diff] [blame] | 6239 | if (!net->ipv6.fib6_null_entry) |
| 6240 | goto out_ip6_dst_entries; |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 6241 | memcpy(net->ipv6.fib6_null_entry, &fib6_null_entry_template, |
| 6242 | sizeof(*net->ipv6.fib6_null_entry)); |
David Ahern | 421842e | 2018-04-17 17:33:18 -0700 | [diff] [blame] | 6243 | |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 6244 | net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template, |
| 6245 | sizeof(*net->ipv6.ip6_null_entry), |
| 6246 | GFP_KERNEL); |
| 6247 | if (!net->ipv6.ip6_null_entry) |
David Ahern | 421842e | 2018-04-17 17:33:18 -0700 | [diff] [blame] | 6248 | goto out_fib6_null_entry; |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 6249 | 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] | 6250 | dst_init_metrics(&net->ipv6.ip6_null_entry->dst, |
| 6251 | ip6_template_metrics, true); |
Wei Wang | 7410921 | 2019-06-20 17:36:38 -0700 | [diff] [blame] | 6252 | INIT_LIST_HEAD(&net->ipv6.ip6_null_entry->rt6i_uncached); |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 6253 | |
| 6254 | #ifdef CONFIG_IPV6_MULTIPLE_TABLES |
Vincent Bernat | feca7d8 | 2017-08-08 20:23:49 +0200 | [diff] [blame] | 6255 | net->ipv6.fib6_has_custom_rules = false; |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 6256 | net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template, |
| 6257 | sizeof(*net->ipv6.ip6_prohibit_entry), |
| 6258 | GFP_KERNEL); |
Peter Zijlstra | 68fffc6 | 2008-10-07 14:12:10 -0700 | [diff] [blame] | 6259 | if (!net->ipv6.ip6_prohibit_entry) |
| 6260 | goto out_ip6_null_entry; |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 6261 | 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] | 6262 | dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst, |
| 6263 | ip6_template_metrics, true); |
Wei Wang | 7410921 | 2019-06-20 17:36:38 -0700 | [diff] [blame] | 6264 | INIT_LIST_HEAD(&net->ipv6.ip6_prohibit_entry->rt6i_uncached); |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 6265 | |
| 6266 | net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template, |
| 6267 | sizeof(*net->ipv6.ip6_blk_hole_entry), |
| 6268 | GFP_KERNEL); |
Peter Zijlstra | 68fffc6 | 2008-10-07 14:12:10 -0700 | [diff] [blame] | 6269 | if (!net->ipv6.ip6_blk_hole_entry) |
| 6270 | goto out_ip6_prohibit_entry; |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 6271 | 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] | 6272 | dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst, |
| 6273 | ip6_template_metrics, true); |
Wei Wang | 7410921 | 2019-06-20 17:36:38 -0700 | [diff] [blame] | 6274 | INIT_LIST_HEAD(&net->ipv6.ip6_blk_hole_entry->rt6i_uncached); |
Paolo Abeni | b9b33e7 | 2019-11-20 13:47:34 +0100 | [diff] [blame] | 6275 | #ifdef CONFIG_IPV6_SUBTREES |
| 6276 | net->ipv6.fib6_routes_require_src = 0; |
| 6277 | #endif |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 6278 | #endif |
| 6279 | |
Peter Zijlstra | b339a47c | 2008-10-07 14:15:00 -0700 | [diff] [blame] | 6280 | net->ipv6.sysctl.flush_delay = 0; |
| 6281 | net->ipv6.sysctl.ip6_rt_max_size = 4096; |
| 6282 | net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2; |
| 6283 | net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ; |
| 6284 | net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ; |
| 6285 | net->ipv6.sysctl.ip6_rt_gc_elasticity = 9; |
| 6286 | net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ; |
| 6287 | net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40; |
David Ahern | 7c6bb7d | 2018-10-11 20:17:21 -0700 | [diff] [blame] | 6288 | net->ipv6.sysctl.skip_notify_on_dev_down = 0; |
Peter Zijlstra | b339a47c | 2008-10-07 14:15:00 -0700 | [diff] [blame] | 6289 | |
Benjamin Thery | 6891a34 | 2008-03-04 13:49:47 -0800 | [diff] [blame] | 6290 | net->ipv6.ip6_rt_gc_expire = 30*HZ; |
| 6291 | |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 6292 | ret = 0; |
| 6293 | out: |
| 6294 | return ret; |
Benjamin Thery | f2fc6a5 | 2008-03-04 13:49:23 -0800 | [diff] [blame] | 6295 | |
Peter Zijlstra | 68fffc6 | 2008-10-07 14:12:10 -0700 | [diff] [blame] | 6296 | #ifdef CONFIG_IPV6_MULTIPLE_TABLES |
| 6297 | out_ip6_prohibit_entry: |
| 6298 | kfree(net->ipv6.ip6_prohibit_entry); |
| 6299 | out_ip6_null_entry: |
| 6300 | kfree(net->ipv6.ip6_null_entry); |
| 6301 | #endif |
David Ahern | 421842e | 2018-04-17 17:33:18 -0700 | [diff] [blame] | 6302 | out_fib6_null_entry: |
| 6303 | kfree(net->ipv6.fib6_null_entry); |
Eric Dumazet | fc66f95 | 2010-10-08 06:37:34 +0000 | [diff] [blame] | 6304 | out_ip6_dst_entries: |
| 6305 | dst_entries_destroy(&net->ipv6.ip6_dst_ops); |
Benjamin Thery | f2fc6a5 | 2008-03-04 13:49:23 -0800 | [diff] [blame] | 6306 | out_ip6_dst_ops: |
Benjamin Thery | f2fc6a5 | 2008-03-04 13:49:23 -0800 | [diff] [blame] | 6307 | goto out; |
Daniel Lezcano | cdb1876 | 2008-03-04 13:45:33 -0800 | [diff] [blame] | 6308 | } |
| 6309 | |
Alexey Dobriyan | 2c8c1e7 | 2010-01-17 03:35:32 +0000 | [diff] [blame] | 6310 | static void __net_exit ip6_route_net_exit(struct net *net) |
Daniel Lezcano | cdb1876 | 2008-03-04 13:45:33 -0800 | [diff] [blame] | 6311 | { |
David Ahern | 421842e | 2018-04-17 17:33:18 -0700 | [diff] [blame] | 6312 | kfree(net->ipv6.fib6_null_entry); |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 6313 | kfree(net->ipv6.ip6_null_entry); |
| 6314 | #ifdef CONFIG_IPV6_MULTIPLE_TABLES |
| 6315 | kfree(net->ipv6.ip6_prohibit_entry); |
| 6316 | kfree(net->ipv6.ip6_blk_hole_entry); |
| 6317 | #endif |
Xiaotian Feng | 41bb78b | 2010-11-02 16:11:05 +0000 | [diff] [blame] | 6318 | dst_entries_destroy(&net->ipv6.ip6_dst_ops); |
Daniel Lezcano | cdb1876 | 2008-03-04 13:45:33 -0800 | [diff] [blame] | 6319 | } |
| 6320 | |
Thomas Graf | d189634 | 2012-06-18 12:08:33 +0000 | [diff] [blame] | 6321 | static int __net_init ip6_route_net_init_late(struct net *net) |
| 6322 | { |
| 6323 | #ifdef CONFIG_PROC_FS |
Christoph Hellwig | c350637 | 2018-04-10 19:42:55 +0200 | [diff] [blame] | 6324 | proc_create_net("ipv6_route", 0, net->proc_net, &ipv6_route_seq_ops, |
| 6325 | sizeof(struct ipv6_route_iter)); |
Christoph Hellwig | 3617d94 | 2018-04-13 20:38:35 +0200 | [diff] [blame] | 6326 | proc_create_net_single("rt6_stats", 0444, net->proc_net, |
| 6327 | rt6_stats_seq_show, NULL); |
Thomas Graf | d189634 | 2012-06-18 12:08:33 +0000 | [diff] [blame] | 6328 | #endif |
| 6329 | return 0; |
| 6330 | } |
| 6331 | |
| 6332 | static void __net_exit ip6_route_net_exit_late(struct net *net) |
| 6333 | { |
| 6334 | #ifdef CONFIG_PROC_FS |
Gao feng | ece31ff | 2013-02-18 01:34:56 +0000 | [diff] [blame] | 6335 | remove_proc_entry("ipv6_route", net->proc_net); |
| 6336 | remove_proc_entry("rt6_stats", net->proc_net); |
Thomas Graf | d189634 | 2012-06-18 12:08:33 +0000 | [diff] [blame] | 6337 | #endif |
| 6338 | } |
| 6339 | |
Daniel Lezcano | cdb1876 | 2008-03-04 13:45:33 -0800 | [diff] [blame] | 6340 | static struct pernet_operations ip6_route_net_ops = { |
| 6341 | .init = ip6_route_net_init, |
| 6342 | .exit = ip6_route_net_exit, |
| 6343 | }; |
| 6344 | |
David S. Miller | c3426b4 | 2012-06-09 16:27:05 -0700 | [diff] [blame] | 6345 | static int __net_init ipv6_inetpeer_init(struct net *net) |
| 6346 | { |
| 6347 | struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL); |
| 6348 | |
| 6349 | if (!bp) |
| 6350 | return -ENOMEM; |
| 6351 | inet_peer_base_init(bp); |
| 6352 | net->ipv6.peers = bp; |
| 6353 | return 0; |
| 6354 | } |
| 6355 | |
| 6356 | static void __net_exit ipv6_inetpeer_exit(struct net *net) |
| 6357 | { |
| 6358 | struct inet_peer_base *bp = net->ipv6.peers; |
| 6359 | |
| 6360 | net->ipv6.peers = NULL; |
David S. Miller | 56a6b24 | 2012-06-09 16:32:41 -0700 | [diff] [blame] | 6361 | inetpeer_invalidate_tree(bp); |
David S. Miller | c3426b4 | 2012-06-09 16:27:05 -0700 | [diff] [blame] | 6362 | kfree(bp); |
| 6363 | } |
| 6364 | |
David S. Miller | 2b823f7 | 2012-06-09 19:00:16 -0700 | [diff] [blame] | 6365 | static struct pernet_operations ipv6_inetpeer_ops = { |
David S. Miller | c3426b4 | 2012-06-09 16:27:05 -0700 | [diff] [blame] | 6366 | .init = ipv6_inetpeer_init, |
| 6367 | .exit = ipv6_inetpeer_exit, |
| 6368 | }; |
| 6369 | |
Thomas Graf | d189634 | 2012-06-18 12:08:33 +0000 | [diff] [blame] | 6370 | static struct pernet_operations ip6_route_net_late_ops = { |
| 6371 | .init = ip6_route_net_init_late, |
| 6372 | .exit = ip6_route_net_exit_late, |
| 6373 | }; |
| 6374 | |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 6375 | static struct notifier_block ip6_route_dev_notifier = { |
| 6376 | .notifier_call = ip6_route_dev_notify, |
WANG Cong | 242d3a4 | 2017-05-08 10:12:13 -0700 | [diff] [blame] | 6377 | .priority = ADDRCONF_NOTIFY_PRIORITY - 10, |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 6378 | }; |
| 6379 | |
WANG Cong | 2f46093 | 2017-05-03 22:07:31 -0700 | [diff] [blame] | 6380 | void __init ip6_route_init_special_entries(void) |
| 6381 | { |
| 6382 | /* Registering of the loopback is done before this portion of code, |
| 6383 | * the loopback reference in rt6_info will not be taken, do it |
| 6384 | * manually for init_net */ |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 6385 | 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] | 6386 | init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev; |
| 6387 | init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); |
| 6388 | #ifdef CONFIG_IPV6_MULTIPLE_TABLES |
| 6389 | init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev; |
| 6390 | init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); |
| 6391 | init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev; |
| 6392 | init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); |
| 6393 | #endif |
| 6394 | } |
| 6395 | |
Yonghong Song | 138d0be | 2020-05-09 10:59:10 -0700 | [diff] [blame] | 6396 | #if IS_BUILTIN(CONFIG_IPV6) |
| 6397 | #if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_PROC_FS) |
| 6398 | DEFINE_BPF_ITER_FUNC(ipv6_route, struct bpf_iter_meta *meta, struct fib6_info *rt) |
| 6399 | |
Yonghong Song | 15172a4 | 2020-05-13 11:02:19 -0700 | [diff] [blame^] | 6400 | static const struct bpf_iter_reg ipv6_route_reg_info = { |
| 6401 | .target = "ipv6_route", |
| 6402 | .seq_ops = &ipv6_route_seq_ops, |
| 6403 | .init_seq_private = bpf_iter_init_seq_net, |
| 6404 | .fini_seq_private = bpf_iter_fini_seq_net, |
| 6405 | .seq_priv_size = sizeof(struct ipv6_route_iter), |
| 6406 | }; |
| 6407 | |
Yonghong Song | 138d0be | 2020-05-09 10:59:10 -0700 | [diff] [blame] | 6408 | static int __init bpf_iter_register(void) |
| 6409 | { |
Yonghong Song | 15172a4 | 2020-05-13 11:02:19 -0700 | [diff] [blame^] | 6410 | return bpf_iter_reg_target(&ipv6_route_reg_info); |
Yonghong Song | 138d0be | 2020-05-09 10:59:10 -0700 | [diff] [blame] | 6411 | } |
| 6412 | |
| 6413 | static void bpf_iter_unregister(void) |
| 6414 | { |
| 6415 | bpf_iter_unreg_target("ipv6_route"); |
| 6416 | } |
| 6417 | #endif |
| 6418 | #endif |
| 6419 | |
Daniel Lezcano | 433d49c | 2007-12-07 00:43:48 -0800 | [diff] [blame] | 6420 | int __init ip6_route_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6421 | { |
Daniel Lezcano | 433d49c | 2007-12-07 00:43:48 -0800 | [diff] [blame] | 6422 | int ret; |
Martin KaFai Lau | 8d0b94a | 2015-05-22 20:56:04 -0700 | [diff] [blame] | 6423 | int cpu; |
Daniel Lezcano | 433d49c | 2007-12-07 00:43:48 -0800 | [diff] [blame] | 6424 | |
Daniel Lezcano | 9a7ec3a | 2008-03-04 13:48:53 -0800 | [diff] [blame] | 6425 | ret = -ENOMEM; |
| 6426 | ip6_dst_ops_template.kmem_cachep = |
| 6427 | kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0, |
| 6428 | SLAB_HWCACHE_ALIGN, NULL); |
| 6429 | if (!ip6_dst_ops_template.kmem_cachep) |
Fernando Carrijo | c19a28e | 2009-01-07 18:09:08 -0800 | [diff] [blame] | 6430 | goto out; |
David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 6431 | |
Eric Dumazet | fc66f95 | 2010-10-08 06:37:34 +0000 | [diff] [blame] | 6432 | ret = dst_entries_init(&ip6_dst_blackhole_ops); |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 6433 | if (ret) |
Daniel Lezcano | bdb3289 | 2008-03-04 13:48:10 -0800 | [diff] [blame] | 6434 | goto out_kmem_cache; |
Daniel Lezcano | bdb3289 | 2008-03-04 13:48:10 -0800 | [diff] [blame] | 6435 | |
David S. Miller | c3426b4 | 2012-06-09 16:27:05 -0700 | [diff] [blame] | 6436 | ret = register_pernet_subsys(&ipv6_inetpeer_ops); |
| 6437 | if (ret) |
David S. Miller | e8803b6 | 2012-06-16 01:12:19 -0700 | [diff] [blame] | 6438 | goto out_dst_entries; |
Thomas Graf | 2a0c451 | 2012-06-14 23:00:17 +0000 | [diff] [blame] | 6439 | |
David S. Miller | 7e52b33 | 2012-06-15 15:51:55 -0700 | [diff] [blame] | 6440 | ret = register_pernet_subsys(&ip6_route_net_ops); |
| 6441 | if (ret) |
| 6442 | goto out_register_inetpeer; |
David S. Miller | c3426b4 | 2012-06-09 16:27:05 -0700 | [diff] [blame] | 6443 | |
Arnaud Ebalard | 5dc121e | 2008-10-01 02:37:56 -0700 | [diff] [blame] | 6444 | ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep; |
| 6445 | |
David S. Miller | e8803b6 | 2012-06-16 01:12:19 -0700 | [diff] [blame] | 6446 | ret = fib6_init(); |
Daniel Lezcano | 433d49c | 2007-12-07 00:43:48 -0800 | [diff] [blame] | 6447 | if (ret) |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 6448 | goto out_register_subsys; |
Daniel Lezcano | 433d49c | 2007-12-07 00:43:48 -0800 | [diff] [blame] | 6449 | |
Daniel Lezcano | 433d49c | 2007-12-07 00:43:48 -0800 | [diff] [blame] | 6450 | ret = xfrm6_init(); |
| 6451 | if (ret) |
David S. Miller | e8803b6 | 2012-06-16 01:12:19 -0700 | [diff] [blame] | 6452 | goto out_fib6_init; |
Daniel Lezcano | c35b7e7 | 2007-12-08 00:14:11 -0800 | [diff] [blame] | 6453 | |
Daniel Lezcano | 433d49c | 2007-12-07 00:43:48 -0800 | [diff] [blame] | 6454 | ret = fib6_rules_init(); |
| 6455 | if (ret) |
| 6456 | goto xfrm6_init; |
Daniel Lezcano | 7e5449c | 2007-12-08 00:14:54 -0800 | [diff] [blame] | 6457 | |
Thomas Graf | d189634 | 2012-06-18 12:08:33 +0000 | [diff] [blame] | 6458 | ret = register_pernet_subsys(&ip6_route_net_late_ops); |
| 6459 | if (ret) |
| 6460 | goto fib6_rules_init; |
| 6461 | |
Florian Westphal | 16feebc | 2017-12-02 21:44:08 +0100 | [diff] [blame] | 6462 | ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_NEWROUTE, |
| 6463 | inet6_rtm_newroute, NULL, 0); |
| 6464 | if (ret < 0) |
| 6465 | goto out_register_late_subsys; |
| 6466 | |
| 6467 | ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_DELROUTE, |
| 6468 | inet6_rtm_delroute, NULL, 0); |
| 6469 | if (ret < 0) |
| 6470 | goto out_register_late_subsys; |
| 6471 | |
| 6472 | ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETROUTE, |
| 6473 | inet6_rtm_getroute, NULL, |
| 6474 | RTNL_FLAG_DOIT_UNLOCKED); |
| 6475 | if (ret < 0) |
Thomas Graf | d189634 | 2012-06-18 12:08:33 +0000 | [diff] [blame] | 6476 | goto out_register_late_subsys; |
Daniel Lezcano | 433d49c | 2007-12-07 00:43:48 -0800 | [diff] [blame] | 6477 | |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 6478 | ret = register_netdevice_notifier(&ip6_route_dev_notifier); |
Daniel Lezcano | cdb1876 | 2008-03-04 13:45:33 -0800 | [diff] [blame] | 6479 | if (ret) |
Thomas Graf | d189634 | 2012-06-18 12:08:33 +0000 | [diff] [blame] | 6480 | goto out_register_late_subsys; |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 6481 | |
Yonghong Song | 138d0be | 2020-05-09 10:59:10 -0700 | [diff] [blame] | 6482 | #if IS_BUILTIN(CONFIG_IPV6) |
| 6483 | #if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_PROC_FS) |
| 6484 | ret = bpf_iter_register(); |
| 6485 | if (ret) |
| 6486 | goto out_register_late_subsys; |
| 6487 | #endif |
| 6488 | #endif |
| 6489 | |
Martin KaFai Lau | 8d0b94a | 2015-05-22 20:56:04 -0700 | [diff] [blame] | 6490 | for_each_possible_cpu(cpu) { |
| 6491 | struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu); |
| 6492 | |
| 6493 | INIT_LIST_HEAD(&ul->head); |
| 6494 | spin_lock_init(&ul->lock); |
| 6495 | } |
| 6496 | |
Daniel Lezcano | 433d49c | 2007-12-07 00:43:48 -0800 | [diff] [blame] | 6497 | out: |
| 6498 | return ret; |
| 6499 | |
Thomas Graf | d189634 | 2012-06-18 12:08:33 +0000 | [diff] [blame] | 6500 | out_register_late_subsys: |
Florian Westphal | 16feebc | 2017-12-02 21:44:08 +0100 | [diff] [blame] | 6501 | rtnl_unregister_all(PF_INET6); |
Thomas Graf | d189634 | 2012-06-18 12:08:33 +0000 | [diff] [blame] | 6502 | unregister_pernet_subsys(&ip6_route_net_late_ops); |
Daniel Lezcano | 433d49c | 2007-12-07 00:43:48 -0800 | [diff] [blame] | 6503 | fib6_rules_init: |
Daniel Lezcano | 433d49c | 2007-12-07 00:43:48 -0800 | [diff] [blame] | 6504 | fib6_rules_cleanup(); |
| 6505 | xfrm6_init: |
Daniel Lezcano | 433d49c | 2007-12-07 00:43:48 -0800 | [diff] [blame] | 6506 | xfrm6_fini(); |
Thomas Graf | 2a0c451 | 2012-06-14 23:00:17 +0000 | [diff] [blame] | 6507 | out_fib6_init: |
| 6508 | fib6_gc_cleanup(); |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 6509 | out_register_subsys: |
| 6510 | unregister_pernet_subsys(&ip6_route_net_ops); |
David S. Miller | 7e52b33 | 2012-06-15 15:51:55 -0700 | [diff] [blame] | 6511 | out_register_inetpeer: |
| 6512 | unregister_pernet_subsys(&ipv6_inetpeer_ops); |
Eric Dumazet | fc66f95 | 2010-10-08 06:37:34 +0000 | [diff] [blame] | 6513 | out_dst_entries: |
| 6514 | dst_entries_destroy(&ip6_dst_blackhole_ops); |
Daniel Lezcano | 433d49c | 2007-12-07 00:43:48 -0800 | [diff] [blame] | 6515 | out_kmem_cache: |
Benjamin Thery | f2fc6a5 | 2008-03-04 13:49:23 -0800 | [diff] [blame] | 6516 | kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep); |
Daniel Lezcano | 433d49c | 2007-12-07 00:43:48 -0800 | [diff] [blame] | 6517 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6518 | } |
| 6519 | |
| 6520 | void ip6_route_cleanup(void) |
| 6521 | { |
Yonghong Song | 138d0be | 2020-05-09 10:59:10 -0700 | [diff] [blame] | 6522 | #if IS_BUILTIN(CONFIG_IPV6) |
| 6523 | #if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_PROC_FS) |
| 6524 | bpf_iter_unregister(); |
| 6525 | #endif |
| 6526 | #endif |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 6527 | unregister_netdevice_notifier(&ip6_route_dev_notifier); |
Thomas Graf | d189634 | 2012-06-18 12:08:33 +0000 | [diff] [blame] | 6528 | unregister_pernet_subsys(&ip6_route_net_late_ops); |
Thomas Graf | 101367c | 2006-08-04 03:39:02 -0700 | [diff] [blame] | 6529 | fib6_rules_cleanup(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6530 | xfrm6_fini(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6531 | fib6_gc_cleanup(); |
David S. Miller | c3426b4 | 2012-06-09 16:27:05 -0700 | [diff] [blame] | 6532 | unregister_pernet_subsys(&ipv6_inetpeer_ops); |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 6533 | unregister_pernet_subsys(&ip6_route_net_ops); |
Xiaotian Feng | 41bb78b | 2010-11-02 16:11:05 +0000 | [diff] [blame] | 6534 | dst_entries_destroy(&ip6_dst_blackhole_ops); |
Benjamin Thery | f2fc6a5 | 2008-03-04 13:49:23 -0800 | [diff] [blame] | 6535 | kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6536 | } |