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, |
| 98 | struct sk_buff *skb, u32 mtu); |
| 99 | static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, |
| 100 | struct sk_buff *skb); |
David Ahern | 702cea5 | 2019-04-09 14:41:13 -0700 | [diff] [blame] | 101 | static int rt6_score_route(const struct fib6_nh *nh, u32 fib6_flags, int oif, |
| 102 | int strict); |
David Ahern | a1b7a1f | 2019-06-08 14:53:26 -0700 | [diff] [blame^] | 103 | static size_t rt6_nlmsg_size(struct fib6_info *f6i); |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 104 | static int rt6_fill_node(struct net *net, struct sk_buff *skb, |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 105 | struct fib6_info *rt, struct dst_entry *dst, |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 106 | struct in6_addr *dest, struct in6_addr *src, |
David Ahern | 16a16cd | 2017-02-02 12:37:11 -0800 | [diff] [blame] | 107 | int iif, int type, u32 portid, u32 seq, |
| 108 | unsigned int flags); |
David Ahern | 7e4b512 | 2019-04-16 14:36:00 -0700 | [diff] [blame] | 109 | 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] | 110 | const struct in6_addr *daddr, |
| 111 | const struct in6_addr *saddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 113 | #ifdef CONFIG_IPV6_ROUTE_INFO |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 114 | static struct fib6_info *rt6_add_route_info(struct net *net, |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 115 | const struct in6_addr *prefix, int prefixlen, |
David Ahern | 830218c | 2016-10-24 10:52:35 -0700 | [diff] [blame] | 116 | const struct in6_addr *gwaddr, |
| 117 | struct net_device *dev, |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 118 | unsigned int pref); |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 119 | static struct fib6_info *rt6_get_route_info(struct net *net, |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 120 | const struct in6_addr *prefix, int prefixlen, |
David Ahern | 830218c | 2016-10-24 10:52:35 -0700 | [diff] [blame] | 121 | const struct in6_addr *gwaddr, |
| 122 | struct net_device *dev); |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 123 | #endif |
| 124 | |
Martin KaFai Lau | 8d0b94a | 2015-05-22 20:56:04 -0700 | [diff] [blame] | 125 | struct uncached_list { |
| 126 | spinlock_t lock; |
| 127 | struct list_head head; |
| 128 | }; |
| 129 | |
| 130 | static DEFINE_PER_CPU_ALIGNED(struct uncached_list, rt6_uncached_list); |
| 131 | |
Xin Long | 510c321 | 2018-02-14 19:06:02 +0800 | [diff] [blame] | 132 | void rt6_uncached_list_add(struct rt6_info *rt) |
Martin KaFai Lau | 8d0b94a | 2015-05-22 20:56:04 -0700 | [diff] [blame] | 133 | { |
| 134 | struct uncached_list *ul = raw_cpu_ptr(&rt6_uncached_list); |
| 135 | |
Martin KaFai Lau | 8d0b94a | 2015-05-22 20:56:04 -0700 | [diff] [blame] | 136 | rt->rt6i_uncached_list = ul; |
| 137 | |
| 138 | spin_lock_bh(&ul->lock); |
| 139 | list_add_tail(&rt->rt6i_uncached, &ul->head); |
| 140 | spin_unlock_bh(&ul->lock); |
| 141 | } |
| 142 | |
Xin Long | 510c321 | 2018-02-14 19:06:02 +0800 | [diff] [blame] | 143 | void rt6_uncached_list_del(struct rt6_info *rt) |
Martin KaFai Lau | 8d0b94a | 2015-05-22 20:56:04 -0700 | [diff] [blame] | 144 | { |
| 145 | if (!list_empty(&rt->rt6i_uncached)) { |
| 146 | struct uncached_list *ul = rt->rt6i_uncached_list; |
Wei Wang | 81eb844 | 2017-10-06 12:06:11 -0700 | [diff] [blame] | 147 | struct net *net = dev_net(rt->dst.dev); |
Martin KaFai Lau | 8d0b94a | 2015-05-22 20:56:04 -0700 | [diff] [blame] | 148 | |
| 149 | spin_lock_bh(&ul->lock); |
| 150 | list_del(&rt->rt6i_uncached); |
Wei Wang | 81eb844 | 2017-10-06 12:06:11 -0700 | [diff] [blame] | 151 | atomic_dec(&net->ipv6.rt6_stats->fib_rt_uncache); |
Martin KaFai Lau | 8d0b94a | 2015-05-22 20:56:04 -0700 | [diff] [blame] | 152 | spin_unlock_bh(&ul->lock); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | static void rt6_uncached_list_flush_dev(struct net *net, struct net_device *dev) |
| 157 | { |
| 158 | struct net_device *loopback_dev = net->loopback_dev; |
| 159 | int cpu; |
| 160 | |
Eric W. Biederman | e332bc6 | 2015-10-12 11:02:08 -0500 | [diff] [blame] | 161 | if (dev == loopback_dev) |
| 162 | return; |
| 163 | |
Martin KaFai Lau | 8d0b94a | 2015-05-22 20:56:04 -0700 | [diff] [blame] | 164 | for_each_possible_cpu(cpu) { |
| 165 | struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu); |
| 166 | struct rt6_info *rt; |
| 167 | |
| 168 | spin_lock_bh(&ul->lock); |
| 169 | list_for_each_entry(rt, &ul->head, rt6i_uncached) { |
| 170 | struct inet6_dev *rt_idev = rt->rt6i_idev; |
| 171 | struct net_device *rt_dev = rt->dst.dev; |
| 172 | |
Eric W. Biederman | e332bc6 | 2015-10-12 11:02:08 -0500 | [diff] [blame] | 173 | if (rt_idev->dev == dev) { |
Martin KaFai Lau | 8d0b94a | 2015-05-22 20:56:04 -0700 | [diff] [blame] | 174 | rt->rt6i_idev = in6_dev_get(loopback_dev); |
| 175 | in6_dev_put(rt_idev); |
| 176 | } |
| 177 | |
Eric W. Biederman | e332bc6 | 2015-10-12 11:02:08 -0500 | [diff] [blame] | 178 | if (rt_dev == dev) { |
Martin KaFai Lau | 8d0b94a | 2015-05-22 20:56:04 -0700 | [diff] [blame] | 179 | rt->dst.dev = loopback_dev; |
| 180 | dev_hold(rt->dst.dev); |
| 181 | dev_put(rt_dev); |
| 182 | } |
| 183 | } |
| 184 | spin_unlock_bh(&ul->lock); |
| 185 | } |
| 186 | } |
| 187 | |
David Ahern | f8a1b43 | 2018-04-17 17:33:21 -0700 | [diff] [blame] | 188 | 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] | 189 | struct sk_buff *skb, |
| 190 | const void *daddr) |
David S. Miller | 3923297 | 2012-01-26 15:22:32 -0500 | [diff] [blame] | 191 | { |
David S. Miller | a7563f3 | 2012-01-26 16:29:16 -0500 | [diff] [blame] | 192 | if (!ipv6_addr_any(p)) |
David S. Miller | 3923297 | 2012-01-26 15:22:32 -0500 | [diff] [blame] | 193 | return (const void *) p; |
David S. Miller | f894cbf | 2012-07-02 21:52:24 -0700 | [diff] [blame] | 194 | else if (skb) |
| 195 | return &ipv6_hdr(skb)->daddr; |
David S. Miller | 3923297 | 2012-01-26 15:22:32 -0500 | [diff] [blame] | 196 | return daddr; |
| 197 | } |
| 198 | |
David Ahern | f8a1b43 | 2018-04-17 17:33:21 -0700 | [diff] [blame] | 199 | struct neighbour *ip6_neigh_lookup(const struct in6_addr *gw, |
| 200 | struct net_device *dev, |
| 201 | struct sk_buff *skb, |
| 202 | const void *daddr) |
David S. Miller | d3aaeb3 | 2011-07-18 00:40:17 -0700 | [diff] [blame] | 203 | { |
David S. Miller | 3923297 | 2012-01-26 15:22:32 -0500 | [diff] [blame] | 204 | struct neighbour *n; |
| 205 | |
David Ahern | f8a1b43 | 2018-04-17 17:33:21 -0700 | [diff] [blame] | 206 | daddr = choose_neigh_daddr(gw, skb, daddr); |
| 207 | n = __ipv6_neigh_lookup(dev, daddr); |
David S. Miller | f83c779 | 2011-12-28 15:41:23 -0500 | [diff] [blame] | 208 | if (n) |
| 209 | return n; |
Stefano Brivio | 7adf324 | 2019-01-02 13:29:27 +0100 | [diff] [blame] | 210 | |
| 211 | n = neigh_create(&nd_tbl, daddr, dev); |
| 212 | return IS_ERR(n) ? NULL : n; |
David Ahern | f8a1b43 | 2018-04-17 17:33:21 -0700 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | static struct neighbour *ip6_dst_neigh_lookup(const struct dst_entry *dst, |
| 216 | struct sk_buff *skb, |
| 217 | const void *daddr) |
| 218 | { |
| 219 | const struct rt6_info *rt = container_of(dst, struct rt6_info, dst); |
| 220 | |
| 221 | return ip6_neigh_lookup(&rt->rt6i_gateway, dst->dev, skb, daddr); |
David S. Miller | f83c779 | 2011-12-28 15:41:23 -0500 | [diff] [blame] | 222 | } |
| 223 | |
Julian Anastasov | 63fca65 | 2017-02-06 23:14:15 +0200 | [diff] [blame] | 224 | static void ip6_confirm_neigh(const struct dst_entry *dst, const void *daddr) |
| 225 | { |
| 226 | struct net_device *dev = dst->dev; |
| 227 | struct rt6_info *rt = (struct rt6_info *)dst; |
| 228 | |
David Ahern | f8a1b43 | 2018-04-17 17:33:21 -0700 | [diff] [blame] | 229 | daddr = choose_neigh_daddr(&rt->rt6i_gateway, NULL, daddr); |
Julian Anastasov | 63fca65 | 2017-02-06 23:14:15 +0200 | [diff] [blame] | 230 | if (!daddr) |
| 231 | return; |
| 232 | if (dev->flags & (IFF_NOARP | IFF_LOOPBACK)) |
| 233 | return; |
| 234 | if (ipv6_addr_is_multicast((const struct in6_addr *)daddr)) |
| 235 | return; |
| 236 | __ipv6_confirm_neigh(dev, daddr); |
| 237 | } |
| 238 | |
Daniel Lezcano | 9a7ec3a | 2008-03-04 13:48:53 -0800 | [diff] [blame] | 239 | static struct dst_ops ip6_dst_ops_template = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | .family = AF_INET6, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | .gc = ip6_dst_gc, |
| 242 | .gc_thresh = 1024, |
| 243 | .check = ip6_dst_check, |
David S. Miller | 0dbaee3 | 2010-12-13 12:52:14 -0800 | [diff] [blame] | 244 | .default_advmss = ip6_default_advmss, |
Steffen Klassert | ebb762f | 2011-11-23 02:12:51 +0000 | [diff] [blame] | 245 | .mtu = ip6_mtu, |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 246 | .cow_metrics = dst_cow_metrics_generic, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | .destroy = ip6_dst_destroy, |
| 248 | .ifdown = ip6_dst_ifdown, |
| 249 | .negative_advice = ip6_negative_advice, |
| 250 | .link_failure = ip6_link_failure, |
| 251 | .update_pmtu = ip6_rt_update_pmtu, |
David S. Miller | 6e157b6 | 2012-07-12 00:05:02 -0700 | [diff] [blame] | 252 | .redirect = rt6_do_redirect, |
Eric W. Biederman | 9f8955c | 2015-10-07 16:48:39 -0500 | [diff] [blame] | 253 | .local_out = __ip6_local_out, |
David Ahern | f8a1b43 | 2018-04-17 17:33:21 -0700 | [diff] [blame] | 254 | .neigh_lookup = ip6_dst_neigh_lookup, |
Julian Anastasov | 63fca65 | 2017-02-06 23:14:15 +0200 | [diff] [blame] | 255 | .confirm_neigh = ip6_confirm_neigh, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | }; |
| 257 | |
Steffen Klassert | ebb762f | 2011-11-23 02:12:51 +0000 | [diff] [blame] | 258 | static unsigned int ip6_blackhole_mtu(const struct dst_entry *dst) |
Roland Dreier | ec831ea | 2011-01-31 13:16:00 -0800 | [diff] [blame] | 259 | { |
Steffen Klassert | 618f9bc | 2011-11-23 02:13:31 +0000 | [diff] [blame] | 260 | unsigned int mtu = dst_metric_raw(dst, RTAX_MTU); |
| 261 | |
| 262 | return mtu ? : dst->dev->mtu; |
Roland Dreier | ec831ea | 2011-01-31 13:16:00 -0800 | [diff] [blame] | 263 | } |
| 264 | |
David S. Miller | 6700c27 | 2012-07-17 03:29:28 -0700 | [diff] [blame] | 265 | static void ip6_rt_blackhole_update_pmtu(struct dst_entry *dst, struct sock *sk, |
| 266 | struct sk_buff *skb, u32 mtu) |
David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 267 | { |
| 268 | } |
| 269 | |
David S. Miller | 6700c27 | 2012-07-17 03:29:28 -0700 | [diff] [blame] | 270 | static void ip6_rt_blackhole_redirect(struct dst_entry *dst, struct sock *sk, |
| 271 | struct sk_buff *skb) |
David S. Miller | b587ee3 | 2012-07-12 00:39:24 -0700 | [diff] [blame] | 272 | { |
| 273 | } |
| 274 | |
David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 275 | static struct dst_ops ip6_dst_blackhole_ops = { |
| 276 | .family = AF_INET6, |
David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 277 | .destroy = ip6_dst_destroy, |
| 278 | .check = ip6_dst_check, |
Steffen Klassert | ebb762f | 2011-11-23 02:12:51 +0000 | [diff] [blame] | 279 | .mtu = ip6_blackhole_mtu, |
Eric Dumazet | 214f45c | 2011-02-18 11:39:01 -0800 | [diff] [blame] | 280 | .default_advmss = ip6_default_advmss, |
David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 281 | .update_pmtu = ip6_rt_blackhole_update_pmtu, |
David S. Miller | b587ee3 | 2012-07-12 00:39:24 -0700 | [diff] [blame] | 282 | .redirect = ip6_rt_blackhole_redirect, |
Martin KaFai Lau | 0a1f596 | 2015-10-15 16:39:58 -0700 | [diff] [blame] | 283 | .cow_metrics = dst_cow_metrics_generic, |
David Ahern | f8a1b43 | 2018-04-17 17:33:21 -0700 | [diff] [blame] | 284 | .neigh_lookup = ip6_dst_neigh_lookup, |
David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 285 | }; |
| 286 | |
David S. Miller | 62fa8a8 | 2011-01-26 20:51:05 -0800 | [diff] [blame] | 287 | static const u32 ip6_template_metrics[RTAX_MAX] = { |
Li RongQing | 14edd87 | 2012-10-24 14:01:18 +0800 | [diff] [blame] | 288 | [RTAX_HOPLIMIT - 1] = 0, |
David S. Miller | 62fa8a8 | 2011-01-26 20:51:05 -0800 | [diff] [blame] | 289 | }; |
| 290 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 291 | static const struct fib6_info fib6_null_entry_template = { |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 292 | .fib6_flags = (RTF_REJECT | RTF_NONEXTHOP), |
| 293 | .fib6_protocol = RTPROT_KERNEL, |
| 294 | .fib6_metric = ~(u32)0, |
Eric Dumazet | f05713e | 2019-04-22 18:35:03 -0700 | [diff] [blame] | 295 | .fib6_ref = REFCOUNT_INIT(1), |
David Ahern | 421842e | 2018-04-17 17:33:18 -0700 | [diff] [blame] | 296 | .fib6_type = RTN_UNREACHABLE, |
| 297 | .fib6_metrics = (struct dst_metrics *)&dst_default_metrics, |
| 298 | }; |
| 299 | |
Eric Dumazet | fb0af4c | 2012-09-11 21:47:51 +0000 | [diff] [blame] | 300 | static const struct rt6_info ip6_null_entry_template = { |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 301 | .dst = { |
| 302 | .__refcnt = ATOMIC_INIT(1), |
| 303 | .__use = 1, |
Nicolas Dichtel | 2c20cbd | 2012-09-10 22:09:47 +0000 | [diff] [blame] | 304 | .obsolete = DST_OBSOLETE_FORCE_CHK, |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 305 | .error = -ENETUNREACH, |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 306 | .input = ip6_pkt_discard, |
| 307 | .output = ip6_pkt_discard_out, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | }, |
| 309 | .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | }; |
| 311 | |
Thomas Graf | 101367c | 2006-08-04 03:39:02 -0700 | [diff] [blame] | 312 | #ifdef CONFIG_IPV6_MULTIPLE_TABLES |
| 313 | |
Eric Dumazet | fb0af4c | 2012-09-11 21:47:51 +0000 | [diff] [blame] | 314 | static const struct rt6_info ip6_prohibit_entry_template = { |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 315 | .dst = { |
| 316 | .__refcnt = ATOMIC_INIT(1), |
| 317 | .__use = 1, |
Nicolas Dichtel | 2c20cbd | 2012-09-10 22:09:47 +0000 | [diff] [blame] | 318 | .obsolete = DST_OBSOLETE_FORCE_CHK, |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 319 | .error = -EACCES, |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 320 | .input = ip6_pkt_prohibit, |
| 321 | .output = ip6_pkt_prohibit_out, |
Thomas Graf | 101367c | 2006-08-04 03:39:02 -0700 | [diff] [blame] | 322 | }, |
| 323 | .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP), |
Thomas Graf | 101367c | 2006-08-04 03:39:02 -0700 | [diff] [blame] | 324 | }; |
| 325 | |
Eric Dumazet | fb0af4c | 2012-09-11 21:47:51 +0000 | [diff] [blame] | 326 | static const struct rt6_info ip6_blk_hole_entry_template = { |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 327 | .dst = { |
| 328 | .__refcnt = ATOMIC_INIT(1), |
| 329 | .__use = 1, |
Nicolas Dichtel | 2c20cbd | 2012-09-10 22:09:47 +0000 | [diff] [blame] | 330 | .obsolete = DST_OBSOLETE_FORCE_CHK, |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 331 | .error = -EINVAL, |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 332 | .input = dst_discard, |
Eric W. Biederman | ede2059 | 2015-10-07 16:48:47 -0500 | [diff] [blame] | 333 | .output = dst_discard_out, |
Thomas Graf | 101367c | 2006-08-04 03:39:02 -0700 | [diff] [blame] | 334 | }, |
| 335 | .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP), |
Thomas Graf | 101367c | 2006-08-04 03:39:02 -0700 | [diff] [blame] | 336 | }; |
| 337 | |
| 338 | #endif |
| 339 | |
Martin KaFai Lau | ebfa45f | 2015-10-15 16:39:57 -0700 | [diff] [blame] | 340 | static void rt6_info_init(struct rt6_info *rt) |
| 341 | { |
| 342 | struct dst_entry *dst = &rt->dst; |
| 343 | |
| 344 | memset(dst + 1, 0, sizeof(*rt) - sizeof(*dst)); |
Martin KaFai Lau | ebfa45f | 2015-10-15 16:39:57 -0700 | [diff] [blame] | 345 | INIT_LIST_HEAD(&rt->rt6i_uncached); |
| 346 | } |
| 347 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | /* allocate dst with ip6_dst_ops */ |
David Ahern | 93531c6 | 2018-04-17 17:33:25 -0700 | [diff] [blame] | 349 | struct rt6_info *ip6_dst_alloc(struct net *net, struct net_device *dev, |
| 350 | int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | { |
David S. Miller | 97bab73 | 2012-06-09 22:36:36 -0700 | [diff] [blame] | 352 | struct rt6_info *rt = dst_alloc(&net->ipv6.ip6_dst_ops, dev, |
Wei Wang | b2a9c0e | 2017-06-17 10:42:41 -0700 | [diff] [blame] | 353 | 1, DST_OBSOLETE_FORCE_CHK, flags); |
David S. Miller | cf91166 | 2011-04-28 14:31:47 -0700 | [diff] [blame] | 354 | |
Wei Wang | 81eb844 | 2017-10-06 12:06:11 -0700 | [diff] [blame] | 355 | if (rt) { |
Martin KaFai Lau | ebfa45f | 2015-10-15 16:39:57 -0700 | [diff] [blame] | 356 | rt6_info_init(rt); |
Wei Wang | 81eb844 | 2017-10-06 12:06:11 -0700 | [diff] [blame] | 357 | atomic_inc(&net->ipv6.rt6_stats->fib_rt_alloc); |
| 358 | } |
Steffen Klassert | 8104891 | 2012-07-05 23:37:09 +0000 | [diff] [blame] | 359 | |
David S. Miller | cf91166 | 2011-04-28 14:31:47 -0700 | [diff] [blame] | 360 | return rt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | } |
David Ahern | 9ab179d | 2016-04-07 11:10:06 -0700 | [diff] [blame] | 362 | EXPORT_SYMBOL(ip6_dst_alloc); |
Martin KaFai Lau | d52d399 | 2015-05-22 20:56:06 -0700 | [diff] [blame] | 363 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | static void ip6_dst_destroy(struct dst_entry *dst) |
| 365 | { |
| 366 | struct rt6_info *rt = (struct rt6_info *)dst; |
David Ahern | a68886a | 2018-04-20 15:38:02 -0700 | [diff] [blame] | 367 | struct fib6_info *from; |
Martin KaFai Lau | 8d0b94a | 2015-05-22 20:56:04 -0700 | [diff] [blame] | 368 | struct inet6_dev *idev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | |
David Ahern | 1620a33 | 2018-10-04 20:07:54 -0700 | [diff] [blame] | 370 | ip_dst_metrics_put(dst); |
Martin KaFai Lau | 8d0b94a | 2015-05-22 20:56:04 -0700 | [diff] [blame] | 371 | rt6_uncached_list_del(rt); |
| 372 | |
| 373 | idev = rt->rt6i_idev; |
David S. Miller | 3830847 | 2011-12-03 18:02:47 -0500 | [diff] [blame] | 374 | if (idev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | rt->rt6i_idev = NULL; |
| 376 | in6_dev_put(idev); |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 377 | } |
Gao feng | 1716a96 | 2012-04-06 00:13:10 +0000 | [diff] [blame] | 378 | |
Eric Dumazet | 0e23387 | 2019-04-28 12:22:25 -0700 | [diff] [blame] | 379 | from = xchg((__force struct fib6_info **)&rt->from, NULL); |
David Ahern | 93531c6 | 2018-04-17 17:33:25 -0700 | [diff] [blame] | 380 | fib6_info_release(from); |
David S. Miller | b341936 | 2010-11-30 12:27:11 -0800 | [diff] [blame] | 381 | } |
| 382 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev, |
| 384 | int how) |
| 385 | { |
| 386 | struct rt6_info *rt = (struct rt6_info *)dst; |
| 387 | struct inet6_dev *idev = rt->rt6i_idev; |
Denis V. Lunev | 5a3e55d | 2007-12-07 00:38:10 -0800 | [diff] [blame] | 388 | struct net_device *loopback_dev = |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 389 | dev_net(dev)->loopback_dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | |
Wei Wang | e5645f5 | 2017-08-14 10:44:59 -0700 | [diff] [blame] | 391 | if (idev && idev->dev != loopback_dev) { |
| 392 | struct inet6_dev *loopback_idev = in6_dev_get(loopback_dev); |
| 393 | if (loopback_idev) { |
| 394 | rt->rt6i_idev = loopback_idev; |
| 395 | in6_dev_put(idev); |
David S. Miller | 97cac08 | 2012-07-02 22:43:47 -0700 | [diff] [blame] | 396 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | } |
| 398 | } |
| 399 | |
Martin KaFai Lau | 5973fb1 | 2015-11-11 11:51:07 -0800 | [diff] [blame] | 400 | static bool __rt6_check_expired(const struct rt6_info *rt) |
| 401 | { |
| 402 | if (rt->rt6i_flags & RTF_EXPIRES) |
| 403 | return time_after(jiffies, rt->dst.expires); |
| 404 | else |
| 405 | return false; |
| 406 | } |
| 407 | |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 408 | static bool rt6_check_expired(const struct rt6_info *rt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | { |
David Ahern | a68886a | 2018-04-20 15:38:02 -0700 | [diff] [blame] | 410 | struct fib6_info *from; |
| 411 | |
| 412 | from = rcu_dereference(rt->from); |
| 413 | |
Gao feng | 1716a96 | 2012-04-06 00:13:10 +0000 | [diff] [blame] | 414 | if (rt->rt6i_flags & RTF_EXPIRES) { |
| 415 | if (time_after(jiffies, rt->dst.expires)) |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 416 | return true; |
David Ahern | a68886a | 2018-04-20 15:38:02 -0700 | [diff] [blame] | 417 | } else if (from) { |
Xin Long | 1e2ea8a | 2017-08-26 20:10:10 +0800 | [diff] [blame] | 418 | return rt->dst.obsolete != DST_OBSOLETE_FORCE_CHK || |
David Ahern | a68886a | 2018-04-20 15:38:02 -0700 | [diff] [blame] | 419 | fib6_check_expired(from); |
Gao feng | 1716a96 | 2012-04-06 00:13:10 +0000 | [diff] [blame] | 420 | } |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 421 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | } |
| 423 | |
David Ahern | b1d4099 | 2019-04-16 14:35:59 -0700 | [diff] [blame] | 424 | void fib6_select_path(const struct net *net, struct fib6_result *res, |
| 425 | struct flowi6 *fl6, int oif, bool have_oif_match, |
| 426 | const struct sk_buff *skb, int strict) |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 427 | { |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 428 | struct fib6_info *sibling, *next_sibling; |
David Ahern | b1d4099 | 2019-04-16 14:35:59 -0700 | [diff] [blame] | 429 | struct fib6_info *match = res->f6i; |
| 430 | |
David Ahern | f88d8ea | 2019-06-03 20:19:52 -0700 | [diff] [blame] | 431 | if ((!match->fib6_nsiblings && !match->nh) || have_oif_match) |
David Ahern | b1d4099 | 2019-04-16 14:35:59 -0700 | [diff] [blame] | 432 | goto out; |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 433 | |
Jakub Sitnicki | b673d6c | 2017-08-23 09:58:31 +0200 | [diff] [blame] | 434 | /* We might have already computed the hash for ICMPv6 errors. In such |
| 435 | * case it will always be non-zero. Otherwise now is the time to do it. |
| 436 | */ |
David Ahern | f88d8ea | 2019-06-03 20:19:52 -0700 | [diff] [blame] | 437 | if (!fl6->mp_hash && |
| 438 | (!match->nh || nexthop_is_multipath(match->nh))) |
David Ahern | b4bac17 | 2018-03-02 08:32:18 -0800 | [diff] [blame] | 439 | fl6->mp_hash = rt6_multipath_hash(net, fl6, skb, NULL); |
Jakub Sitnicki | b673d6c | 2017-08-23 09:58:31 +0200 | [diff] [blame] | 440 | |
David Ahern | f88d8ea | 2019-06-03 20:19:52 -0700 | [diff] [blame] | 441 | if (unlikely(match->nh)) { |
| 442 | nexthop_path_fib6_result(res, fl6->mp_hash); |
| 443 | return; |
| 444 | } |
| 445 | |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 446 | 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] | 447 | goto out; |
Ido Schimmel | bbfcd77 | 2017-11-21 09:50:12 +0200 | [diff] [blame] | 448 | |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 449 | list_for_each_entry_safe(sibling, next_sibling, &match->fib6_siblings, |
| 450 | fib6_siblings) { |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 451 | const struct fib6_nh *nh = sibling->fib6_nh; |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 452 | int nh_upper_bound; |
| 453 | |
David Ahern | 702cea5 | 2019-04-09 14:41:13 -0700 | [diff] [blame] | 454 | nh_upper_bound = atomic_read(&nh->fib_nh_upper_bound); |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 455 | if (fl6->mp_hash > nh_upper_bound) |
Ido Schimmel | 3d709f6 | 2018-01-09 16:40:27 +0200 | [diff] [blame] | 456 | continue; |
David Ahern | 702cea5 | 2019-04-09 14:41:13 -0700 | [diff] [blame] | 457 | if (rt6_score_route(nh, sibling->fib6_flags, oif, strict) < 0) |
Ido Schimmel | 3d709f6 | 2018-01-09 16:40:27 +0200 | [diff] [blame] | 458 | break; |
| 459 | match = sibling; |
| 460 | break; |
| 461 | } |
| 462 | |
David Ahern | b1d4099 | 2019-04-16 14:35:59 -0700 | [diff] [blame] | 463 | out: |
| 464 | res->f6i = match; |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 465 | res->nh = match->fib6_nh; |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 466 | } |
| 467 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | /* |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 469 | * Route lookup. rcu_read_lock() should be held. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | */ |
| 471 | |
David Ahern | 0c59d00 | 2019-04-09 14:41:18 -0700 | [diff] [blame] | 472 | static bool __rt6_device_match(struct net *net, const struct fib6_nh *nh, |
| 473 | const struct in6_addr *saddr, int oif, int flags) |
| 474 | { |
| 475 | const struct net_device *dev; |
| 476 | |
| 477 | if (nh->fib_nh_flags & RTNH_F_DEAD) |
| 478 | return false; |
| 479 | |
| 480 | dev = nh->fib_nh_dev; |
| 481 | if (oif) { |
| 482 | if (dev->ifindex == oif) |
| 483 | return true; |
| 484 | } else { |
| 485 | if (ipv6_chk_addr(net, saddr, dev, |
| 486 | flags & RT6_LOOKUP_F_IFACE)) |
| 487 | return true; |
| 488 | } |
| 489 | |
| 490 | return false; |
| 491 | } |
| 492 | |
David Ahern | 962b680 | 2019-06-08 14:53:24 -0700 | [diff] [blame] | 493 | struct fib6_nh_dm_arg { |
| 494 | struct net *net; |
| 495 | const struct in6_addr *saddr; |
| 496 | int oif; |
| 497 | int flags; |
| 498 | struct fib6_nh *nh; |
| 499 | }; |
| 500 | |
| 501 | static int __rt6_nh_dev_match(struct fib6_nh *nh, void *_arg) |
| 502 | { |
| 503 | struct fib6_nh_dm_arg *arg = _arg; |
| 504 | |
| 505 | arg->nh = nh; |
| 506 | return __rt6_device_match(arg->net, nh, arg->saddr, arg->oif, |
| 507 | arg->flags); |
| 508 | } |
| 509 | |
| 510 | /* returns fib6_nh from nexthop or NULL */ |
| 511 | static struct fib6_nh *rt6_nh_dev_match(struct net *net, struct nexthop *nh, |
| 512 | struct fib6_result *res, |
| 513 | const struct in6_addr *saddr, |
| 514 | int oif, int flags) |
| 515 | { |
| 516 | struct fib6_nh_dm_arg arg = { |
| 517 | .net = net, |
| 518 | .saddr = saddr, |
| 519 | .oif = oif, |
| 520 | .flags = flags, |
| 521 | }; |
| 522 | |
| 523 | if (nexthop_is_blackhole(nh)) |
| 524 | return NULL; |
| 525 | |
| 526 | if (nexthop_for_each_fib6_nh(nh, __rt6_nh_dev_match, &arg)) |
| 527 | return arg.nh; |
| 528 | |
| 529 | return NULL; |
| 530 | } |
| 531 | |
David Ahern | 75ef738 | 2019-04-16 14:36:07 -0700 | [diff] [blame] | 532 | static void rt6_device_match(struct net *net, struct fib6_result *res, |
| 533 | const struct in6_addr *saddr, int oif, int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | { |
David Ahern | 75ef738 | 2019-04-16 14:36:07 -0700 | [diff] [blame] | 535 | struct fib6_info *f6i = res->f6i; |
| 536 | struct fib6_info *spf6i; |
| 537 | struct fib6_nh *nh; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 538 | |
David Ahern | 75ef738 | 2019-04-16 14:36:07 -0700 | [diff] [blame] | 539 | if (!oif && ipv6_addr_any(saddr)) { |
David Ahern | f88d8ea | 2019-06-03 20:19:52 -0700 | [diff] [blame] | 540 | if (unlikely(f6i->nh)) { |
| 541 | nh = nexthop_fib6_nh(f6i->nh); |
| 542 | if (nexthop_is_blackhole(f6i->nh)) |
| 543 | goto out_blackhole; |
| 544 | } else { |
| 545 | nh = f6i->fib6_nh; |
| 546 | } |
David Ahern | 7d21fec | 2019-04-16 14:36:11 -0700 | [diff] [blame] | 547 | if (!(nh->fib_nh_flags & RTNH_F_DEAD)) |
| 548 | goto out; |
YOSHIFUJI Hideaki | dd3abc4 | 2008-07-02 18:30:18 +0900 | [diff] [blame] | 549 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | |
David Ahern | 75ef738 | 2019-04-16 14:36:07 -0700 | [diff] [blame] | 551 | for (spf6i = f6i; spf6i; spf6i = rcu_dereference(spf6i->fib6_next)) { |
David Ahern | 962b680 | 2019-06-08 14:53:24 -0700 | [diff] [blame] | 552 | bool matched = false; |
| 553 | |
| 554 | if (unlikely(spf6i->nh)) { |
| 555 | nh = rt6_nh_dev_match(net, spf6i->nh, res, saddr, |
| 556 | oif, flags); |
| 557 | if (nh) |
| 558 | matched = true; |
| 559 | } else { |
| 560 | nh = spf6i->fib6_nh; |
| 561 | if (__rt6_device_match(net, nh, saddr, oif, flags)) |
| 562 | matched = true; |
| 563 | } |
| 564 | if (matched) { |
David Ahern | 75ef738 | 2019-04-16 14:36:07 -0700 | [diff] [blame] | 565 | res->f6i = spf6i; |
David Ahern | 7d21fec | 2019-04-16 14:36:11 -0700 | [diff] [blame] | 566 | goto out; |
David Ahern | 75ef738 | 2019-04-16 14:36:07 -0700 | [diff] [blame] | 567 | } |
| 568 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | |
David Ahern | 75ef738 | 2019-04-16 14:36:07 -0700 | [diff] [blame] | 570 | if (oif && flags & RT6_LOOKUP_F_IFACE) { |
| 571 | res->f6i = net->ipv6.fib6_null_entry; |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 572 | nh = res->f6i->fib6_nh; |
David Ahern | 7d21fec | 2019-04-16 14:36:11 -0700 | [diff] [blame] | 573 | goto out; |
David Ahern | 75ef738 | 2019-04-16 14:36:07 -0700 | [diff] [blame] | 574 | } |
| 575 | |
David Ahern | f88d8ea | 2019-06-03 20:19:52 -0700 | [diff] [blame] | 576 | if (unlikely(f6i->nh)) { |
| 577 | nh = nexthop_fib6_nh(f6i->nh); |
| 578 | if (nexthop_is_blackhole(f6i->nh)) |
| 579 | goto out_blackhole; |
| 580 | } else { |
| 581 | nh = f6i->fib6_nh; |
| 582 | } |
| 583 | |
David Ahern | 7d21fec | 2019-04-16 14:36:11 -0700 | [diff] [blame] | 584 | if (nh->fib_nh_flags & RTNH_F_DEAD) { |
David Ahern | 75ef738 | 2019-04-16 14:36:07 -0700 | [diff] [blame] | 585 | res->f6i = net->ipv6.fib6_null_entry; |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 586 | nh = res->f6i->fib6_nh; |
David Ahern | 75ef738 | 2019-04-16 14:36:07 -0700 | [diff] [blame] | 587 | } |
David Ahern | 7d21fec | 2019-04-16 14:36:11 -0700 | [diff] [blame] | 588 | out: |
| 589 | res->nh = nh; |
| 590 | res->fib6_type = res->f6i->fib6_type; |
| 591 | res->fib6_flags = res->f6i->fib6_flags; |
David Ahern | f88d8ea | 2019-06-03 20:19:52 -0700 | [diff] [blame] | 592 | return; |
| 593 | |
| 594 | out_blackhole: |
| 595 | res->fib6_flags |= RTF_REJECT; |
| 596 | res->fib6_type = RTN_BLACKHOLE; |
| 597 | res->nh = nh; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 598 | } |
| 599 | |
YOSHIFUJI Hideaki | 2709725 | 2006-03-20 17:05:13 -0800 | [diff] [blame] | 600 | #ifdef CONFIG_IPV6_ROUTER_PREF |
Hannes Frederic Sowa | c2f17e8 | 2013-10-21 06:17:15 +0200 | [diff] [blame] | 601 | struct __rt6_probe_work { |
| 602 | struct work_struct work; |
| 603 | struct in6_addr target; |
| 604 | struct net_device *dev; |
| 605 | }; |
| 606 | |
| 607 | static void rt6_probe_deferred(struct work_struct *w) |
| 608 | { |
| 609 | struct in6_addr mcaddr; |
| 610 | struct __rt6_probe_work *work = |
| 611 | container_of(w, struct __rt6_probe_work, work); |
| 612 | |
| 613 | addrconf_addr_solict_mult(&work->target, &mcaddr); |
Erik Nordmark | adc176c | 2016-12-02 14:00:08 -0800 | [diff] [blame] | 614 | ndisc_send_ns(work->dev, &work->target, &mcaddr, NULL, 0); |
Hannes Frederic Sowa | c2f17e8 | 2013-10-21 06:17:15 +0200 | [diff] [blame] | 615 | dev_put(work->dev); |
Michael Büsch | 662f553 | 2015-02-08 10:14:07 +0100 | [diff] [blame] | 616 | kfree(work); |
Hannes Frederic Sowa | c2f17e8 | 2013-10-21 06:17:15 +0200 | [diff] [blame] | 617 | } |
| 618 | |
David Ahern | cc3a86c | 2019-04-09 14:41:12 -0700 | [diff] [blame] | 619 | static void rt6_probe(struct fib6_nh *fib6_nh) |
YOSHIFUJI Hideaki | 2709725 | 2006-03-20 17:05:13 -0800 | [diff] [blame] | 620 | { |
Sabrina Dubroca | f547fac | 2018-10-12 16:22:47 +0200 | [diff] [blame] | 621 | struct __rt6_probe_work *work = NULL; |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 622 | const struct in6_addr *nh_gw; |
Eric Dumazet | f2c31e3 | 2011-07-29 19:00:53 +0000 | [diff] [blame] | 623 | struct neighbour *neigh; |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 624 | struct net_device *dev; |
Sabrina Dubroca | f547fac | 2018-10-12 16:22:47 +0200 | [diff] [blame] | 625 | struct inet6_dev *idev; |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 626 | |
YOSHIFUJI Hideaki | 2709725 | 2006-03-20 17:05:13 -0800 | [diff] [blame] | 627 | /* |
| 628 | * Okay, this does not seem to be appropriate |
| 629 | * for now, however, we need to check if it |
| 630 | * is really so; aka Router Reachability Probing. |
| 631 | * |
| 632 | * Router Reachability Probe MUST be rate-limited |
| 633 | * to no more than one per minute. |
| 634 | */ |
David Ahern | cc3a86c | 2019-04-09 14:41:12 -0700 | [diff] [blame] | 635 | if (fib6_nh->fib_nh_gw_family) |
Amerigo Wang | fdd6681 | 2012-09-10 02:48:44 +0000 | [diff] [blame] | 636 | return; |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 637 | |
David Ahern | cc3a86c | 2019-04-09 14:41:12 -0700 | [diff] [blame] | 638 | nh_gw = &fib6_nh->fib_nh_gw6; |
| 639 | dev = fib6_nh->fib_nh_dev; |
YOSHIFUJI Hideaki / 吉藤英明 | 2152cae | 2013-01-17 12:53:43 +0000 | [diff] [blame] | 640 | rcu_read_lock_bh(); |
Sabrina Dubroca | f547fac | 2018-10-12 16:22:47 +0200 | [diff] [blame] | 641 | idev = __in6_dev_get(dev); |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 642 | neigh = __ipv6_neigh_lookup_noref(dev, nh_gw); |
YOSHIFUJI Hideaki / 吉藤英明 | 2152cae | 2013-01-17 12:53:43 +0000 | [diff] [blame] | 643 | if (neigh) { |
Martin KaFai Lau | 8d6c31b | 2015-07-24 09:57:43 -0700 | [diff] [blame] | 644 | if (neigh->nud_state & NUD_VALID) |
| 645 | goto out; |
| 646 | |
YOSHIFUJI Hideaki / 吉藤英明 | 2152cae | 2013-01-17 12:53:43 +0000 | [diff] [blame] | 647 | write_lock(&neigh->lock); |
Martin KaFai Lau | 990edb4 | 2015-07-24 09:57:42 -0700 | [diff] [blame] | 648 | if (!(neigh->nud_state & NUD_VALID) && |
| 649 | time_after(jiffies, |
David Ahern | dcd1f57 | 2018-04-18 15:39:05 -0700 | [diff] [blame] | 650 | neigh->updated + idev->cnf.rtr_probe_interval)) { |
Martin KaFai Lau | 990edb4 | 2015-07-24 09:57:42 -0700 | [diff] [blame] | 651 | work = kmalloc(sizeof(*work), GFP_ATOMIC); |
| 652 | if (work) |
| 653 | __neigh_set_probe_once(neigh); |
Hannes Frederic Sowa | c2f17e8 | 2013-10-21 06:17:15 +0200 | [diff] [blame] | 654 | } |
YOSHIFUJI Hideaki / 吉藤英明 | 2152cae | 2013-01-17 12:53:43 +0000 | [diff] [blame] | 655 | write_unlock(&neigh->lock); |
David Ahern | cc3a86c | 2019-04-09 14:41:12 -0700 | [diff] [blame] | 656 | } else if (time_after(jiffies, fib6_nh->last_probe + |
Sabrina Dubroca | f547fac | 2018-10-12 16:22:47 +0200 | [diff] [blame] | 657 | idev->cnf.rtr_probe_interval)) { |
Martin KaFai Lau | 990edb4 | 2015-07-24 09:57:42 -0700 | [diff] [blame] | 658 | work = kmalloc(sizeof(*work), GFP_ATOMIC); |
Eric Dumazet | f2c31e3 | 2011-07-29 19:00:53 +0000 | [diff] [blame] | 659 | } |
Martin KaFai Lau | 990edb4 | 2015-07-24 09:57:42 -0700 | [diff] [blame] | 660 | |
| 661 | if (work) { |
David Ahern | cc3a86c | 2019-04-09 14:41:12 -0700 | [diff] [blame] | 662 | fib6_nh->last_probe = jiffies; |
Martin KaFai Lau | 990edb4 | 2015-07-24 09:57:42 -0700 | [diff] [blame] | 663 | INIT_WORK(&work->work, rt6_probe_deferred); |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 664 | work->target = *nh_gw; |
| 665 | dev_hold(dev); |
| 666 | work->dev = dev; |
Martin KaFai Lau | 990edb4 | 2015-07-24 09:57:42 -0700 | [diff] [blame] | 667 | schedule_work(&work->work); |
| 668 | } |
| 669 | |
Martin KaFai Lau | 8d6c31b | 2015-07-24 09:57:43 -0700 | [diff] [blame] | 670 | out: |
YOSHIFUJI Hideaki / 吉藤英明 | 2152cae | 2013-01-17 12:53:43 +0000 | [diff] [blame] | 671 | rcu_read_unlock_bh(); |
YOSHIFUJI Hideaki | 2709725 | 2006-03-20 17:05:13 -0800 | [diff] [blame] | 672 | } |
| 673 | #else |
David Ahern | cc3a86c | 2019-04-09 14:41:12 -0700 | [diff] [blame] | 674 | static inline void rt6_probe(struct fib6_nh *fib6_nh) |
YOSHIFUJI Hideaki | 2709725 | 2006-03-20 17:05:13 -0800 | [diff] [blame] | 675 | { |
YOSHIFUJI Hideaki | 2709725 | 2006-03-20 17:05:13 -0800 | [diff] [blame] | 676 | } |
| 677 | #endif |
| 678 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 679 | /* |
YOSHIFUJI Hideaki | 554cfb7 | 2006-03-20 17:00:26 -0800 | [diff] [blame] | 680 | * Default Router Selection (RFC 2461 6.3.6) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | */ |
David Ahern | 1ba9a89 | 2019-04-09 14:41:10 -0700 | [diff] [blame] | 682 | 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] | 683 | { |
Hannes Frederic Sowa | afc154e | 2013-07-11 12:43:42 +0200 | [diff] [blame] | 684 | enum rt6_nud_state ret = RT6_NUD_FAIL_HARD; |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 685 | struct neighbour *neigh; |
Eric Dumazet | f2c31e3 | 2011-07-29 19:00:53 +0000 | [diff] [blame] | 686 | |
YOSHIFUJI Hideaki / 吉藤英明 | 145a362 | 2013-01-17 12:53:38 +0000 | [diff] [blame] | 687 | rcu_read_lock_bh(); |
David Ahern | 1ba9a89 | 2019-04-09 14:41:10 -0700 | [diff] [blame] | 688 | neigh = __ipv6_neigh_lookup_noref(fib6_nh->fib_nh_dev, |
| 689 | &fib6_nh->fib_nh_gw6); |
YOSHIFUJI Hideaki / 吉藤英明 | 145a362 | 2013-01-17 12:53:38 +0000 | [diff] [blame] | 690 | if (neigh) { |
| 691 | read_lock(&neigh->lock); |
YOSHIFUJI Hideaki | 554cfb7 | 2006-03-20 17:00:26 -0800 | [diff] [blame] | 692 | if (neigh->nud_state & NUD_VALID) |
Hannes Frederic Sowa | afc154e | 2013-07-11 12:43:42 +0200 | [diff] [blame] | 693 | ret = RT6_NUD_SUCCEED; |
YOSHIFUJI Hideaki | 398bcbe | 2008-01-19 00:35:16 -0800 | [diff] [blame] | 694 | #ifdef CONFIG_IPV6_ROUTER_PREF |
Paul Marks | a5a81f0 | 2012-12-03 10:26:54 +0000 | [diff] [blame] | 695 | else if (!(neigh->nud_state & NUD_FAILED)) |
Hannes Frederic Sowa | afc154e | 2013-07-11 12:43:42 +0200 | [diff] [blame] | 696 | ret = RT6_NUD_SUCCEED; |
Jiri Benc | 7e98056 | 2013-12-11 13:48:20 +0100 | [diff] [blame] | 697 | else |
| 698 | ret = RT6_NUD_FAIL_PROBE; |
YOSHIFUJI Hideaki | 398bcbe | 2008-01-19 00:35:16 -0800 | [diff] [blame] | 699 | #endif |
YOSHIFUJI Hideaki / 吉藤英明 | 145a362 | 2013-01-17 12:53:38 +0000 | [diff] [blame] | 700 | read_unlock(&neigh->lock); |
Hannes Frederic Sowa | afc154e | 2013-07-11 12:43:42 +0200 | [diff] [blame] | 701 | } else { |
| 702 | ret = IS_ENABLED(CONFIG_IPV6_ROUTER_PREF) ? |
Jiri Benc | 7e98056 | 2013-12-11 13:48:20 +0100 | [diff] [blame] | 703 | RT6_NUD_SUCCEED : RT6_NUD_FAIL_DO_RR; |
Paul Marks | a5a81f0 | 2012-12-03 10:26:54 +0000 | [diff] [blame] | 704 | } |
YOSHIFUJI Hideaki / 吉藤英明 | 145a362 | 2013-01-17 12:53:38 +0000 | [diff] [blame] | 705 | rcu_read_unlock_bh(); |
| 706 | |
Paul Marks | a5a81f0 | 2012-12-03 10:26:54 +0000 | [diff] [blame] | 707 | return ret; |
YOSHIFUJI Hideaki | 554cfb7 | 2006-03-20 17:00:26 -0800 | [diff] [blame] | 708 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 709 | |
David Ahern | 702cea5 | 2019-04-09 14:41:13 -0700 | [diff] [blame] | 710 | static int rt6_score_route(const struct fib6_nh *nh, u32 fib6_flags, int oif, |
| 711 | int strict) |
YOSHIFUJI Hideaki | 554cfb7 | 2006-03-20 17:00:26 -0800 | [diff] [blame] | 712 | { |
David Ahern | 6e1809a | 2019-04-09 14:41:11 -0700 | [diff] [blame] | 713 | int m = 0; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 714 | |
David Ahern | 6e1809a | 2019-04-09 14:41:11 -0700 | [diff] [blame] | 715 | if (!oif || nh->fib_nh_dev->ifindex == oif) |
| 716 | m = 2; |
| 717 | |
YOSHIFUJI Hideaki | 77d16f4 | 2006-08-23 17:25:05 -0700 | [diff] [blame] | 718 | if (!m && (strict & RT6_LOOKUP_F_IFACE)) |
Hannes Frederic Sowa | afc154e | 2013-07-11 12:43:42 +0200 | [diff] [blame] | 719 | return RT6_NUD_FAIL_HARD; |
YOSHIFUJI Hideaki | ebacaaa | 2006-03-20 17:04:53 -0800 | [diff] [blame] | 720 | #ifdef CONFIG_IPV6_ROUTER_PREF |
David Ahern | 702cea5 | 2019-04-09 14:41:13 -0700 | [diff] [blame] | 721 | m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(fib6_flags)) << 2; |
YOSHIFUJI Hideaki | ebacaaa | 2006-03-20 17:04:53 -0800 | [diff] [blame] | 722 | #endif |
David Ahern | 1ba9a89 | 2019-04-09 14:41:10 -0700 | [diff] [blame] | 723 | if ((strict & RT6_LOOKUP_F_REACHABLE) && |
David Ahern | 702cea5 | 2019-04-09 14:41:13 -0700 | [diff] [blame] | 724 | !(fib6_flags & RTF_NONEXTHOP) && nh->fib_nh_gw_family) { |
David Ahern | 1ba9a89 | 2019-04-09 14:41:10 -0700 | [diff] [blame] | 725 | int n = rt6_check_neigh(nh); |
Hannes Frederic Sowa | afc154e | 2013-07-11 12:43:42 +0200 | [diff] [blame] | 726 | if (n < 0) |
| 727 | return n; |
| 728 | } |
YOSHIFUJI Hideaki | 554cfb7 | 2006-03-20 17:00:26 -0800 | [diff] [blame] | 729 | return m; |
| 730 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 731 | |
David Ahern | 28679ed | 2019-04-09 14:41:14 -0700 | [diff] [blame] | 732 | static bool find_match(struct fib6_nh *nh, u32 fib6_flags, |
| 733 | int oif, int strict, int *mpri, bool *do_rr) |
YOSHIFUJI Hideaki | 554cfb7 | 2006-03-20 17:00:26 -0800 | [diff] [blame] | 734 | { |
Hannes Frederic Sowa | afc154e | 2013-07-11 12:43:42 +0200 | [diff] [blame] | 735 | bool match_do_rr = false; |
David Ahern | 28679ed | 2019-04-09 14:41:14 -0700 | [diff] [blame] | 736 | bool rc = false; |
| 737 | int m; |
Andy Gospodarek | 35103d1 | 2015-08-13 10:39:01 -0400 | [diff] [blame] | 738 | |
David Ahern | 28679ed | 2019-04-09 14:41:14 -0700 | [diff] [blame] | 739 | if (nh->fib_nh_flags & RTNH_F_DEAD) |
Ido Schimmel | 8067bb8 | 2018-01-07 12:45:09 +0200 | [diff] [blame] | 740 | goto out; |
| 741 | |
David Ahern | 28679ed | 2019-04-09 14:41:14 -0700 | [diff] [blame] | 742 | if (ip6_ignore_linkdown(nh->fib_nh_dev) && |
| 743 | nh->fib_nh_flags & RTNH_F_LINKDOWN && |
David Ahern | d5d32e4 | 2016-10-24 12:27:23 -0700 | [diff] [blame] | 744 | !(strict & RT6_LOOKUP_F_IGNORE_LINKSTATE)) |
Andy Gospodarek | 35103d1 | 2015-08-13 10:39:01 -0400 | [diff] [blame] | 745 | goto out; |
David S. Miller | f11e665 | 2007-03-24 20:36:25 -0700 | [diff] [blame] | 746 | |
David Ahern | 28679ed | 2019-04-09 14:41:14 -0700 | [diff] [blame] | 747 | m = rt6_score_route(nh, fib6_flags, oif, strict); |
Jiri Benc | 7e98056 | 2013-12-11 13:48:20 +0100 | [diff] [blame] | 748 | if (m == RT6_NUD_FAIL_DO_RR) { |
Hannes Frederic Sowa | afc154e | 2013-07-11 12:43:42 +0200 | [diff] [blame] | 749 | match_do_rr = true; |
| 750 | m = 0; /* lowest valid score */ |
Jiri Benc | 7e98056 | 2013-12-11 13:48:20 +0100 | [diff] [blame] | 751 | } else if (m == RT6_NUD_FAIL_HARD) { |
David S. Miller | f11e665 | 2007-03-24 20:36:25 -0700 | [diff] [blame] | 752 | goto out; |
David S. Miller | f11e665 | 2007-03-24 20:36:25 -0700 | [diff] [blame] | 753 | } |
| 754 | |
Hannes Frederic Sowa | afc154e | 2013-07-11 12:43:42 +0200 | [diff] [blame] | 755 | if (strict & RT6_LOOKUP_F_REACHABLE) |
David Ahern | 28679ed | 2019-04-09 14:41:14 -0700 | [diff] [blame] | 756 | rt6_probe(nh); |
Hannes Frederic Sowa | afc154e | 2013-07-11 12:43:42 +0200 | [diff] [blame] | 757 | |
Jiri Benc | 7e98056 | 2013-12-11 13:48:20 +0100 | [diff] [blame] | 758 | /* 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] | 759 | if (m > *mpri) { |
| 760 | *do_rr = match_do_rr; |
| 761 | *mpri = m; |
David Ahern | 28679ed | 2019-04-09 14:41:14 -0700 | [diff] [blame] | 762 | rc = true; |
Hannes Frederic Sowa | afc154e | 2013-07-11 12:43:42 +0200 | [diff] [blame] | 763 | } |
David S. Miller | f11e665 | 2007-03-24 20:36:25 -0700 | [diff] [blame] | 764 | out: |
David Ahern | 28679ed | 2019-04-09 14:41:14 -0700 | [diff] [blame] | 765 | return rc; |
David S. Miller | f11e665 | 2007-03-24 20:36:25 -0700 | [diff] [blame] | 766 | } |
| 767 | |
David Ahern | 17a5984 | 2019-06-08 14:53:25 -0700 | [diff] [blame] | 768 | struct fib6_nh_frl_arg { |
| 769 | u32 flags; |
| 770 | int oif; |
| 771 | int strict; |
| 772 | int *mpri; |
| 773 | bool *do_rr; |
| 774 | struct fib6_nh *nh; |
| 775 | }; |
| 776 | |
| 777 | static int rt6_nh_find_match(struct fib6_nh *nh, void *_arg) |
| 778 | { |
| 779 | struct fib6_nh_frl_arg *arg = _arg; |
| 780 | |
| 781 | arg->nh = nh; |
| 782 | return find_match(nh, arg->flags, arg->oif, arg->strict, |
| 783 | arg->mpri, arg->do_rr); |
| 784 | } |
| 785 | |
David Ahern | b7bc4b6 | 2019-04-16 14:36:08 -0700 | [diff] [blame] | 786 | static void __find_rr_leaf(struct fib6_info *f6i_start, |
David Ahern | 30c15f0 | 2019-04-09 14:41:15 -0700 | [diff] [blame] | 787 | struct fib6_info *nomatch, u32 metric, |
David Ahern | b7bc4b6 | 2019-04-16 14:36:08 -0700 | [diff] [blame] | 788 | struct fib6_result *res, struct fib6_info **cont, |
David Ahern | 30c15f0 | 2019-04-09 14:41:15 -0700 | [diff] [blame] | 789 | int oif, int strict, bool *do_rr, int *mpri) |
David S. Miller | f11e665 | 2007-03-24 20:36:25 -0700 | [diff] [blame] | 790 | { |
David Ahern | b7bc4b6 | 2019-04-16 14:36:08 -0700 | [diff] [blame] | 791 | struct fib6_info *f6i; |
David Ahern | 30c15f0 | 2019-04-09 14:41:15 -0700 | [diff] [blame] | 792 | |
David Ahern | b7bc4b6 | 2019-04-16 14:36:08 -0700 | [diff] [blame] | 793 | for (f6i = f6i_start; |
| 794 | f6i && f6i != nomatch; |
| 795 | f6i = rcu_dereference(f6i->fib6_next)) { |
David Ahern | 17a5984 | 2019-06-08 14:53:25 -0700 | [diff] [blame] | 796 | bool matched = false; |
David Ahern | 30c15f0 | 2019-04-09 14:41:15 -0700 | [diff] [blame] | 797 | struct fib6_nh *nh; |
| 798 | |
David Ahern | b7bc4b6 | 2019-04-16 14:36:08 -0700 | [diff] [blame] | 799 | if (cont && f6i->fib6_metric != metric) { |
| 800 | *cont = f6i; |
David Ahern | 30c15f0 | 2019-04-09 14:41:15 -0700 | [diff] [blame] | 801 | return; |
| 802 | } |
| 803 | |
David Ahern | b7bc4b6 | 2019-04-16 14:36:08 -0700 | [diff] [blame] | 804 | if (fib6_check_expired(f6i)) |
David Ahern | 30c15f0 | 2019-04-09 14:41:15 -0700 | [diff] [blame] | 805 | continue; |
| 806 | |
David Ahern | 17a5984 | 2019-06-08 14:53:25 -0700 | [diff] [blame] | 807 | if (unlikely(f6i->nh)) { |
| 808 | struct fib6_nh_frl_arg arg = { |
| 809 | .flags = f6i->fib6_flags, |
| 810 | .oif = oif, |
| 811 | .strict = strict, |
| 812 | .mpri = mpri, |
| 813 | .do_rr = do_rr |
| 814 | }; |
| 815 | |
| 816 | if (nexthop_is_blackhole(f6i->nh)) { |
| 817 | res->fib6_flags = RTF_REJECT; |
| 818 | res->fib6_type = RTN_BLACKHOLE; |
| 819 | res->f6i = f6i; |
| 820 | res->nh = nexthop_fib6_nh(f6i->nh); |
| 821 | return; |
| 822 | } |
| 823 | if (nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_find_match, |
| 824 | &arg)) { |
| 825 | matched = true; |
| 826 | nh = arg.nh; |
| 827 | } |
| 828 | } else { |
| 829 | nh = f6i->fib6_nh; |
| 830 | if (find_match(nh, f6i->fib6_flags, oif, strict, |
| 831 | mpri, do_rr)) |
| 832 | matched = true; |
| 833 | } |
| 834 | if (matched) { |
David Ahern | b7bc4b6 | 2019-04-16 14:36:08 -0700 | [diff] [blame] | 835 | res->f6i = f6i; |
| 836 | res->nh = nh; |
David Ahern | 7d21fec | 2019-04-16 14:36:11 -0700 | [diff] [blame] | 837 | res->fib6_flags = f6i->fib6_flags; |
| 838 | res->fib6_type = f6i->fib6_type; |
David Ahern | b7bc4b6 | 2019-04-16 14:36:08 -0700 | [diff] [blame] | 839 | } |
David Ahern | 30c15f0 | 2019-04-09 14:41:15 -0700 | [diff] [blame] | 840 | } |
| 841 | } |
| 842 | |
David Ahern | b7bc4b6 | 2019-04-16 14:36:08 -0700 | [diff] [blame] | 843 | static void find_rr_leaf(struct fib6_node *fn, struct fib6_info *leaf, |
| 844 | struct fib6_info *rr_head, int oif, int strict, |
| 845 | bool *do_rr, struct fib6_result *res) |
David Ahern | 30c15f0 | 2019-04-09 14:41:15 -0700 | [diff] [blame] | 846 | { |
David Ahern | b7bc4b6 | 2019-04-16 14:36:08 -0700 | [diff] [blame] | 847 | u32 metric = rr_head->fib6_metric; |
| 848 | struct fib6_info *cont = NULL; |
YOSHIFUJI Hideaki | 554cfb7 | 2006-03-20 17:00:26 -0800 | [diff] [blame] | 849 | int mpri = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 850 | |
David Ahern | b7bc4b6 | 2019-04-16 14:36:08 -0700 | [diff] [blame] | 851 | __find_rr_leaf(rr_head, NULL, metric, res, &cont, |
David Ahern | 30c15f0 | 2019-04-09 14:41:15 -0700 | [diff] [blame] | 852 | oif, strict, do_rr, &mpri); |
Steffen Klassert | 9fbdcfa | 2015-04-28 13:03:04 -0700 | [diff] [blame] | 853 | |
David Ahern | b7bc4b6 | 2019-04-16 14:36:08 -0700 | [diff] [blame] | 854 | __find_rr_leaf(leaf, rr_head, metric, res, &cont, |
David Ahern | 30c15f0 | 2019-04-09 14:41:15 -0700 | [diff] [blame] | 855 | oif, strict, do_rr, &mpri); |
Steffen Klassert | 9fbdcfa | 2015-04-28 13:03:04 -0700 | [diff] [blame] | 856 | |
David Ahern | b7bc4b6 | 2019-04-16 14:36:08 -0700 | [diff] [blame] | 857 | if (res->f6i || !cont) |
| 858 | return; |
Steffen Klassert | 9fbdcfa | 2015-04-28 13:03:04 -0700 | [diff] [blame] | 859 | |
David Ahern | b7bc4b6 | 2019-04-16 14:36:08 -0700 | [diff] [blame] | 860 | __find_rr_leaf(cont, NULL, metric, res, NULL, |
David Ahern | 30c15f0 | 2019-04-09 14:41:15 -0700 | [diff] [blame] | 861 | oif, strict, do_rr, &mpri); |
David S. Miller | f11e665 | 2007-03-24 20:36:25 -0700 | [diff] [blame] | 862 | } |
YOSHIFUJI Hideaki | 554cfb7 | 2006-03-20 17:00:26 -0800 | [diff] [blame] | 863 | |
David Ahern | b7bc4b6 | 2019-04-16 14:36:08 -0700 | [diff] [blame] | 864 | static void rt6_select(struct net *net, struct fib6_node *fn, int oif, |
| 865 | struct fib6_result *res, int strict) |
David S. Miller | f11e665 | 2007-03-24 20:36:25 -0700 | [diff] [blame] | 866 | { |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 867 | struct fib6_info *leaf = rcu_dereference(fn->leaf); |
David Ahern | b7bc4b6 | 2019-04-16 14:36:08 -0700 | [diff] [blame] | 868 | struct fib6_info *rt0; |
Hannes Frederic Sowa | afc154e | 2013-07-11 12:43:42 +0200 | [diff] [blame] | 869 | bool do_rr = false; |
Wei Wang | 17ecf59 | 2017-10-06 12:06:09 -0700 | [diff] [blame] | 870 | int key_plen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 871 | |
David Ahern | b7bc4b6 | 2019-04-16 14:36:08 -0700 | [diff] [blame] | 872 | /* make sure this function or its helpers sets f6i */ |
| 873 | res->f6i = NULL; |
| 874 | |
David Ahern | 421842e | 2018-04-17 17:33:18 -0700 | [diff] [blame] | 875 | if (!leaf || leaf == net->ipv6.fib6_null_entry) |
David Ahern | b7bc4b6 | 2019-04-16 14:36:08 -0700 | [diff] [blame] | 876 | goto out; |
Wei Wang | 8d1040e | 2017-10-06 12:06:08 -0700 | [diff] [blame] | 877 | |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 878 | rt0 = rcu_dereference(fn->rr_ptr); |
David S. Miller | f11e665 | 2007-03-24 20:36:25 -0700 | [diff] [blame] | 879 | if (!rt0) |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 880 | rt0 = leaf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 881 | |
Wei Wang | 17ecf59 | 2017-10-06 12:06:09 -0700 | [diff] [blame] | 882 | /* Double check to make sure fn is not an intermediate node |
| 883 | * and fn->leaf does not points to its child's leaf |
| 884 | * (This might happen if all routes under fn are deleted from |
| 885 | * the tree and fib6_repair_tree() is called on the node.) |
| 886 | */ |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 887 | key_plen = rt0->fib6_dst.plen; |
Wei Wang | 17ecf59 | 2017-10-06 12:06:09 -0700 | [diff] [blame] | 888 | #ifdef CONFIG_IPV6_SUBTREES |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 889 | if (rt0->fib6_src.plen) |
| 890 | key_plen = rt0->fib6_src.plen; |
Wei Wang | 17ecf59 | 2017-10-06 12:06:09 -0700 | [diff] [blame] | 891 | #endif |
| 892 | if (fn->fn_bit != key_plen) |
David Ahern | b7bc4b6 | 2019-04-16 14:36:08 -0700 | [diff] [blame] | 893 | goto out; |
Wei Wang | 17ecf59 | 2017-10-06 12:06:09 -0700 | [diff] [blame] | 894 | |
David Ahern | b7bc4b6 | 2019-04-16 14:36:08 -0700 | [diff] [blame] | 895 | find_rr_leaf(fn, leaf, rt0, oif, strict, &do_rr, res); |
Hannes Frederic Sowa | afc154e | 2013-07-11 12:43:42 +0200 | [diff] [blame] | 896 | if (do_rr) { |
David Ahern | 8fb11a9 | 2018-05-04 13:54:24 -0700 | [diff] [blame] | 897 | struct fib6_info *next = rcu_dereference(rt0->fib6_next); |
David S. Miller | f11e665 | 2007-03-24 20:36:25 -0700 | [diff] [blame] | 898 | |
YOSHIFUJI Hideaki | 554cfb7 | 2006-03-20 17:00:26 -0800 | [diff] [blame] | 899 | /* no entries matched; do round-robin */ |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 900 | if (!next || next->fib6_metric != rt0->fib6_metric) |
Wei Wang | 8d1040e | 2017-10-06 12:06:08 -0700 | [diff] [blame] | 901 | next = leaf; |
David S. Miller | f11e665 | 2007-03-24 20:36:25 -0700 | [diff] [blame] | 902 | |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 903 | if (next != rt0) { |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 904 | spin_lock_bh(&leaf->fib6_table->tb6_lock); |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 905 | /* make sure next is not being deleted from the tree */ |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 906 | if (next->fib6_node) |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 907 | rcu_assign_pointer(fn->rr_ptr, next); |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 908 | spin_unlock_bh(&leaf->fib6_table->tb6_lock); |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 909 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 910 | } |
| 911 | |
David Ahern | b7bc4b6 | 2019-04-16 14:36:08 -0700 | [diff] [blame] | 912 | out: |
| 913 | if (!res->f6i) { |
| 914 | res->f6i = net->ipv6.fib6_null_entry; |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 915 | res->nh = res->f6i->fib6_nh; |
David Ahern | 7d21fec | 2019-04-16 14:36:11 -0700 | [diff] [blame] | 916 | res->fib6_flags = res->f6i->fib6_flags; |
| 917 | res->fib6_type = res->f6i->fib6_type; |
David Ahern | b7bc4b6 | 2019-04-16 14:36:08 -0700 | [diff] [blame] | 918 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 919 | } |
| 920 | |
David Ahern | 85bd05d | 2019-04-16 14:36:01 -0700 | [diff] [blame] | 921 | 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] | 922 | { |
David Ahern | 85bd05d | 2019-04-16 14:36:01 -0700 | [diff] [blame] | 923 | return (res->f6i->fib6_flags & RTF_NONEXTHOP) || |
| 924 | res->nh->fib_nh_gw_family; |
Martin KaFai Lau | 8b9df26 | 2015-05-22 20:55:59 -0700 | [diff] [blame] | 925 | } |
| 926 | |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 927 | #ifdef CONFIG_IPV6_ROUTE_INFO |
| 928 | int rt6_route_rcv(struct net_device *dev, u8 *opt, int len, |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 929 | const struct in6_addr *gwaddr) |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 930 | { |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 931 | struct net *net = dev_net(dev); |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 932 | struct route_info *rinfo = (struct route_info *) opt; |
| 933 | struct in6_addr prefix_buf, *prefix; |
| 934 | unsigned int pref; |
YOSHIFUJI Hideaki | 4bed72e | 2008-05-27 17:37:49 +0900 | [diff] [blame] | 935 | unsigned long lifetime; |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 936 | struct fib6_info *rt; |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 937 | |
| 938 | if (len < sizeof(struct route_info)) { |
| 939 | return -EINVAL; |
| 940 | } |
| 941 | |
| 942 | /* Sanity check for prefix_len and length */ |
| 943 | if (rinfo->length > 3) { |
| 944 | return -EINVAL; |
| 945 | } else if (rinfo->prefix_len > 128) { |
| 946 | return -EINVAL; |
| 947 | } else if (rinfo->prefix_len > 64) { |
| 948 | if (rinfo->length < 2) { |
| 949 | return -EINVAL; |
| 950 | } |
| 951 | } else if (rinfo->prefix_len > 0) { |
| 952 | if (rinfo->length < 1) { |
| 953 | return -EINVAL; |
| 954 | } |
| 955 | } |
| 956 | |
| 957 | pref = rinfo->route_pref; |
| 958 | if (pref == ICMPV6_ROUTER_PREF_INVALID) |
Jens Rosenboom | 3933fc9 | 2009-09-10 06:25:11 +0000 | [diff] [blame] | 959 | return -EINVAL; |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 960 | |
YOSHIFUJI Hideaki | 4bed72e | 2008-05-27 17:37:49 +0900 | [diff] [blame] | 961 | lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ); |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 962 | |
| 963 | if (rinfo->length == 3) |
| 964 | prefix = (struct in6_addr *)rinfo->prefix; |
| 965 | else { |
| 966 | /* this function is safe */ |
| 967 | ipv6_addr_prefix(&prefix_buf, |
| 968 | (struct in6_addr *)rinfo->prefix, |
| 969 | rinfo->prefix_len); |
| 970 | prefix = &prefix_buf; |
| 971 | } |
| 972 | |
Duan Jiong | f104a56 | 2013-11-08 09:56:53 +0800 | [diff] [blame] | 973 | if (rinfo->prefix_len == 0) |
David Ahern | afb1d4b5 | 2018-04-17 17:33:11 -0700 | [diff] [blame] | 974 | rt = rt6_get_dflt_router(net, gwaddr, dev); |
Duan Jiong | f104a56 | 2013-11-08 09:56:53 +0800 | [diff] [blame] | 975 | else |
| 976 | rt = rt6_get_route_info(net, prefix, rinfo->prefix_len, |
David Ahern | 830218c | 2016-10-24 10:52:35 -0700 | [diff] [blame] | 977 | gwaddr, dev); |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 978 | |
| 979 | if (rt && !lifetime) { |
David Ahern | afb1d4b5 | 2018-04-17 17:33:11 -0700 | [diff] [blame] | 980 | ip6_del_rt(net, rt); |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 981 | rt = NULL; |
| 982 | } |
| 983 | |
| 984 | if (!rt && lifetime) |
David Ahern | 830218c | 2016-10-24 10:52:35 -0700 | [diff] [blame] | 985 | rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr, |
| 986 | dev, pref); |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 987 | else if (rt) |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 988 | rt->fib6_flags = RTF_ROUTEINFO | |
| 989 | (rt->fib6_flags & ~RTF_PREF_MASK) | RTF_PREF(pref); |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 990 | |
| 991 | if (rt) { |
Gao feng | 1716a96 | 2012-04-06 00:13:10 +0000 | [diff] [blame] | 992 | if (!addrconf_finite_timeout(lifetime)) |
David Ahern | 1489568 | 2018-04-17 17:33:17 -0700 | [diff] [blame] | 993 | fib6_clean_expires(rt); |
Gao feng | 1716a96 | 2012-04-06 00:13:10 +0000 | [diff] [blame] | 994 | else |
David Ahern | 1489568 | 2018-04-17 17:33:17 -0700 | [diff] [blame] | 995 | fib6_set_expires(rt, jiffies + HZ * lifetime); |
Gao feng | 1716a96 | 2012-04-06 00:13:10 +0000 | [diff] [blame] | 996 | |
David Ahern | 93531c6 | 2018-04-17 17:33:25 -0700 | [diff] [blame] | 997 | fib6_info_release(rt); |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 998 | } |
| 999 | return 0; |
| 1000 | } |
| 1001 | #endif |
| 1002 | |
David Ahern | ae90d86 | 2018-04-17 17:33:12 -0700 | [diff] [blame] | 1003 | /* |
| 1004 | * Misc support functions |
| 1005 | */ |
| 1006 | |
| 1007 | /* called with rcu_lock held */ |
David Ahern | 0d16158 | 2019-04-16 14:36:04 -0700 | [diff] [blame] | 1008 | 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] | 1009 | { |
David Ahern | 0d16158 | 2019-04-16 14:36:04 -0700 | [diff] [blame] | 1010 | struct net_device *dev = res->nh->fib_nh_dev; |
David Ahern | ae90d86 | 2018-04-17 17:33:12 -0700 | [diff] [blame] | 1011 | |
David Ahern | 7d21fec | 2019-04-16 14:36:11 -0700 | [diff] [blame] | 1012 | if (res->fib6_flags & (RTF_LOCAL | RTF_ANYCAST)) { |
David Ahern | ae90d86 | 2018-04-17 17:33:12 -0700 | [diff] [blame] | 1013 | /* for copies of local routes, dst->dev needs to be the |
| 1014 | * device if it is a master device, the master device if |
| 1015 | * device is enslaved, and the loopback as the default |
| 1016 | */ |
| 1017 | if (netif_is_l3_slave(dev) && |
David Ahern | 7d21fec | 2019-04-16 14:36:11 -0700 | [diff] [blame] | 1018 | !rt6_need_strict(&res->f6i->fib6_dst.addr)) |
David Ahern | ae90d86 | 2018-04-17 17:33:12 -0700 | [diff] [blame] | 1019 | dev = l3mdev_master_dev_rcu(dev); |
| 1020 | else if (!netif_is_l3_master(dev)) |
| 1021 | dev = dev_net(dev)->loopback_dev; |
| 1022 | /* last case is netif_is_l3_master(dev) is true in which |
| 1023 | * case we want dev returned to be dev |
| 1024 | */ |
| 1025 | } |
| 1026 | |
| 1027 | return dev; |
| 1028 | } |
| 1029 | |
David Ahern | 6edb3c9 | 2018-04-17 17:33:15 -0700 | [diff] [blame] | 1030 | static const int fib6_prop[RTN_MAX + 1] = { |
| 1031 | [RTN_UNSPEC] = 0, |
| 1032 | [RTN_UNICAST] = 0, |
| 1033 | [RTN_LOCAL] = 0, |
| 1034 | [RTN_BROADCAST] = 0, |
| 1035 | [RTN_ANYCAST] = 0, |
| 1036 | [RTN_MULTICAST] = 0, |
| 1037 | [RTN_BLACKHOLE] = -EINVAL, |
| 1038 | [RTN_UNREACHABLE] = -EHOSTUNREACH, |
| 1039 | [RTN_PROHIBIT] = -EACCES, |
| 1040 | [RTN_THROW] = -EAGAIN, |
| 1041 | [RTN_NAT] = -EINVAL, |
| 1042 | [RTN_XRESOLVE] = -EINVAL, |
| 1043 | }; |
| 1044 | |
| 1045 | static int ip6_rt_type_to_error(u8 fib6_type) |
| 1046 | { |
| 1047 | return fib6_prop[fib6_type]; |
| 1048 | } |
| 1049 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 1050 | static unsigned short fib6_info_dst_flags(struct fib6_info *rt) |
David Ahern | 3b6761d | 2018-04-17 17:33:20 -0700 | [diff] [blame] | 1051 | { |
| 1052 | unsigned short flags = 0; |
| 1053 | |
| 1054 | if (rt->dst_nocount) |
| 1055 | flags |= DST_NOCOUNT; |
| 1056 | if (rt->dst_nopolicy) |
| 1057 | flags |= DST_NOPOLICY; |
| 1058 | if (rt->dst_host) |
| 1059 | flags |= DST_HOST; |
| 1060 | |
| 1061 | return flags; |
| 1062 | } |
| 1063 | |
David Ahern | 7d21fec | 2019-04-16 14:36:11 -0700 | [diff] [blame] | 1064 | 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] | 1065 | { |
David Ahern | 7d21fec | 2019-04-16 14:36:11 -0700 | [diff] [blame] | 1066 | rt->dst.error = ip6_rt_type_to_error(fib6_type); |
David Ahern | 6edb3c9 | 2018-04-17 17:33:15 -0700 | [diff] [blame] | 1067 | |
David Ahern | 7d21fec | 2019-04-16 14:36:11 -0700 | [diff] [blame] | 1068 | switch (fib6_type) { |
David Ahern | 6edb3c9 | 2018-04-17 17:33:15 -0700 | [diff] [blame] | 1069 | case RTN_BLACKHOLE: |
| 1070 | rt->dst.output = dst_discard_out; |
| 1071 | rt->dst.input = dst_discard; |
| 1072 | break; |
| 1073 | case RTN_PROHIBIT: |
| 1074 | rt->dst.output = ip6_pkt_prohibit_out; |
| 1075 | rt->dst.input = ip6_pkt_prohibit; |
| 1076 | break; |
| 1077 | case RTN_THROW: |
| 1078 | case RTN_UNREACHABLE: |
| 1079 | default: |
| 1080 | rt->dst.output = ip6_pkt_discard_out; |
| 1081 | rt->dst.input = ip6_pkt_discard; |
| 1082 | break; |
| 1083 | } |
| 1084 | } |
| 1085 | |
David Ahern | 0d16158 | 2019-04-16 14:36:04 -0700 | [diff] [blame] | 1086 | 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] | 1087 | { |
David Ahern | 7d21fec | 2019-04-16 14:36:11 -0700 | [diff] [blame] | 1088 | struct fib6_info *f6i = res->f6i; |
David Ahern | 0d16158 | 2019-04-16 14:36:04 -0700 | [diff] [blame] | 1089 | |
David Ahern | 7d21fec | 2019-04-16 14:36:11 -0700 | [diff] [blame] | 1090 | if (res->fib6_flags & RTF_REJECT) { |
| 1091 | ip6_rt_init_dst_reject(rt, res->fib6_type); |
David Ahern | 6edb3c9 | 2018-04-17 17:33:15 -0700 | [diff] [blame] | 1092 | return; |
| 1093 | } |
| 1094 | |
| 1095 | rt->dst.error = 0; |
| 1096 | rt->dst.output = ip6_output; |
| 1097 | |
David Ahern | 7d21fec | 2019-04-16 14:36:11 -0700 | [diff] [blame] | 1098 | if (res->fib6_type == RTN_LOCAL || res->fib6_type == RTN_ANYCAST) { |
David Ahern | 6edb3c9 | 2018-04-17 17:33:15 -0700 | [diff] [blame] | 1099 | rt->dst.input = ip6_input; |
David Ahern | 7d21fec | 2019-04-16 14:36:11 -0700 | [diff] [blame] | 1100 | } else if (ipv6_addr_type(&f6i->fib6_dst.addr) & IPV6_ADDR_MULTICAST) { |
David Ahern | 6edb3c9 | 2018-04-17 17:33:15 -0700 | [diff] [blame] | 1101 | rt->dst.input = ip6_mc_input; |
| 1102 | } else { |
| 1103 | rt->dst.input = ip6_forward; |
| 1104 | } |
| 1105 | |
David Ahern | 0d16158 | 2019-04-16 14:36:04 -0700 | [diff] [blame] | 1106 | if (res->nh->fib_nh_lws) { |
| 1107 | rt->dst.lwtstate = lwtstate_get(res->nh->fib_nh_lws); |
David Ahern | 6edb3c9 | 2018-04-17 17:33:15 -0700 | [diff] [blame] | 1108 | lwtunnel_set_redirect(&rt->dst); |
| 1109 | } |
| 1110 | |
| 1111 | rt->dst.lastuse = jiffies; |
| 1112 | } |
| 1113 | |
Wei Wang | e873e4b | 2018-07-21 20:56:32 -0700 | [diff] [blame] | 1114 | /* Caller must already hold reference to @from */ |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 1115 | 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] | 1116 | { |
David Ahern | ae90d86 | 2018-04-17 17:33:12 -0700 | [diff] [blame] | 1117 | rt->rt6i_flags &= ~RTF_EXPIRES; |
David Ahern | a68886a | 2018-04-20 15:38:02 -0700 | [diff] [blame] | 1118 | rcu_assign_pointer(rt->from, from); |
David Ahern | e1255ed | 2018-10-04 20:07:53 -0700 | [diff] [blame] | 1119 | ip_dst_init_metrics(&rt->dst, from->fib6_metrics); |
David Ahern | ae90d86 | 2018-04-17 17:33:12 -0700 | [diff] [blame] | 1120 | } |
| 1121 | |
David Ahern | 0d16158 | 2019-04-16 14:36:04 -0700 | [diff] [blame] | 1122 | /* Caller must already hold reference to f6i in result */ |
| 1123 | 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] | 1124 | { |
David Ahern | 0d16158 | 2019-04-16 14:36:04 -0700 | [diff] [blame] | 1125 | const struct fib6_nh *nh = res->nh; |
| 1126 | const struct net_device *dev = nh->fib_nh_dev; |
| 1127 | struct fib6_info *f6i = res->f6i; |
David Ahern | dcd1f57 | 2018-04-18 15:39:05 -0700 | [diff] [blame] | 1128 | |
David Ahern | 0d16158 | 2019-04-16 14:36:04 -0700 | [diff] [blame] | 1129 | ip6_rt_init_dst(rt, res); |
David Ahern | 6edb3c9 | 2018-04-17 17:33:15 -0700 | [diff] [blame] | 1130 | |
David Ahern | 0d16158 | 2019-04-16 14:36:04 -0700 | [diff] [blame] | 1131 | rt->rt6i_dst = f6i->fib6_dst; |
David Ahern | dcd1f57 | 2018-04-18 15:39:05 -0700 | [diff] [blame] | 1132 | rt->rt6i_idev = dev ? in6_dev_get(dev) : NULL; |
David Ahern | 7d21fec | 2019-04-16 14:36:11 -0700 | [diff] [blame] | 1133 | rt->rt6i_flags = res->fib6_flags; |
David Ahern | 0d16158 | 2019-04-16 14:36:04 -0700 | [diff] [blame] | 1134 | if (nh->fib_nh_gw_family) { |
| 1135 | rt->rt6i_gateway = nh->fib_nh_gw6; |
David Ahern | 2b2450c | 2019-03-27 20:53:52 -0700 | [diff] [blame] | 1136 | rt->rt6i_flags |= RTF_GATEWAY; |
| 1137 | } |
David Ahern | 0d16158 | 2019-04-16 14:36:04 -0700 | [diff] [blame] | 1138 | rt6_set_from(rt, f6i); |
David Ahern | ae90d86 | 2018-04-17 17:33:12 -0700 | [diff] [blame] | 1139 | #ifdef CONFIG_IPV6_SUBTREES |
David Ahern | 0d16158 | 2019-04-16 14:36:04 -0700 | [diff] [blame] | 1140 | rt->rt6i_src = f6i->fib6_src; |
David Ahern | ae90d86 | 2018-04-17 17:33:12 -0700 | [diff] [blame] | 1141 | #endif |
David Ahern | ae90d86 | 2018-04-17 17:33:12 -0700 | [diff] [blame] | 1142 | } |
| 1143 | |
Martin KaFai Lau | a3c00e4 | 2014-10-20 13:42:43 -0700 | [diff] [blame] | 1144 | static struct fib6_node* fib6_backtrack(struct fib6_node *fn, |
| 1145 | struct in6_addr *saddr) |
| 1146 | { |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 1147 | struct fib6_node *pn, *sn; |
Martin KaFai Lau | a3c00e4 | 2014-10-20 13:42:43 -0700 | [diff] [blame] | 1148 | while (1) { |
| 1149 | if (fn->fn_flags & RTN_TL_ROOT) |
| 1150 | return NULL; |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 1151 | pn = rcu_dereference(fn->parent); |
| 1152 | sn = FIB6_SUBTREE(pn); |
| 1153 | if (sn && sn != fn) |
David Ahern | 6454743 | 2018-05-09 20:34:19 -0700 | [diff] [blame] | 1154 | fn = fib6_node_lookup(sn, NULL, saddr); |
Martin KaFai Lau | a3c00e4 | 2014-10-20 13:42:43 -0700 | [diff] [blame] | 1155 | else |
| 1156 | fn = pn; |
| 1157 | if (fn->fn_flags & RTN_RTINFO) |
| 1158 | return fn; |
| 1159 | } |
| 1160 | } |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1161 | |
David Ahern | 10585b4 | 2019-03-20 09:24:50 -0700 | [diff] [blame] | 1162 | static bool ip6_hold_safe(struct net *net, struct rt6_info **prt) |
Wei Wang | d3843fe | 2017-10-06 12:06:06 -0700 | [diff] [blame] | 1163 | { |
| 1164 | struct rt6_info *rt = *prt; |
| 1165 | |
| 1166 | if (dst_hold_safe(&rt->dst)) |
| 1167 | return true; |
David Ahern | 10585b4 | 2019-03-20 09:24:50 -0700 | [diff] [blame] | 1168 | if (net) { |
Wei Wang | d3843fe | 2017-10-06 12:06:06 -0700 | [diff] [blame] | 1169 | rt = net->ipv6.ip6_null_entry; |
| 1170 | dst_hold(&rt->dst); |
| 1171 | } else { |
| 1172 | rt = NULL; |
| 1173 | } |
| 1174 | *prt = rt; |
| 1175 | return false; |
| 1176 | } |
| 1177 | |
David Ahern | dec9b0e | 2018-04-17 17:33:19 -0700 | [diff] [blame] | 1178 | /* called with rcu_lock held */ |
David Ahern | 9b6b35a | 2019-04-16 14:36:02 -0700 | [diff] [blame] | 1179 | 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] | 1180 | { |
David Ahern | 9b6b35a | 2019-04-16 14:36:02 -0700 | [diff] [blame] | 1181 | struct net_device *dev = res->nh->fib_nh_dev; |
| 1182 | struct fib6_info *f6i = res->f6i; |
| 1183 | unsigned short flags; |
David Ahern | dec9b0e | 2018-04-17 17:33:19 -0700 | [diff] [blame] | 1184 | struct rt6_info *nrt; |
| 1185 | |
David Ahern | 9b6b35a | 2019-04-16 14:36:02 -0700 | [diff] [blame] | 1186 | if (!fib6_info_hold_safe(f6i)) |
Xin Long | 1c87e79 | 2019-03-20 14:45:48 +0800 | [diff] [blame] | 1187 | goto fallback; |
Wei Wang | e873e4b | 2018-07-21 20:56:32 -0700 | [diff] [blame] | 1188 | |
David Ahern | 9b6b35a | 2019-04-16 14:36:02 -0700 | [diff] [blame] | 1189 | flags = fib6_info_dst_flags(f6i); |
David Ahern | 93531c6 | 2018-04-17 17:33:25 -0700 | [diff] [blame] | 1190 | nrt = ip6_dst_alloc(dev_net(dev), dev, flags); |
Xin Long | 1c87e79 | 2019-03-20 14:45:48 +0800 | [diff] [blame] | 1191 | if (!nrt) { |
David Ahern | 9b6b35a | 2019-04-16 14:36:02 -0700 | [diff] [blame] | 1192 | fib6_info_release(f6i); |
Xin Long | 1c87e79 | 2019-03-20 14:45:48 +0800 | [diff] [blame] | 1193 | goto fallback; |
| 1194 | } |
David Ahern | dec9b0e | 2018-04-17 17:33:19 -0700 | [diff] [blame] | 1195 | |
David Ahern | 0d16158 | 2019-04-16 14:36:04 -0700 | [diff] [blame] | 1196 | ip6_rt_copy_init(nrt, res); |
Xin Long | 1c87e79 | 2019-03-20 14:45:48 +0800 | [diff] [blame] | 1197 | return nrt; |
| 1198 | |
| 1199 | fallback: |
| 1200 | nrt = dev_net(dev)->ipv6.ip6_null_entry; |
| 1201 | dst_hold(&nrt->dst); |
David Ahern | dec9b0e | 2018-04-17 17:33:19 -0700 | [diff] [blame] | 1202 | return nrt; |
| 1203 | } |
| 1204 | |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 1205 | static struct rt6_info *ip6_pol_route_lookup(struct net *net, |
| 1206 | struct fib6_table *table, |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 1207 | struct flowi6 *fl6, |
| 1208 | const struct sk_buff *skb, |
| 1209 | int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1210 | { |
David Ahern | b1d4099 | 2019-04-16 14:35:59 -0700 | [diff] [blame] | 1211 | struct fib6_result res = {}; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1212 | struct fib6_node *fn; |
David Ahern | 23fb93a | 2018-04-17 17:33:23 -0700 | [diff] [blame] | 1213 | struct rt6_info *rt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1214 | |
David Ahern | b6cdbc8 | 2018-03-29 17:44:57 -0700 | [diff] [blame] | 1215 | if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF) |
| 1216 | flags &= ~RT6_LOOKUP_F_IFACE; |
| 1217 | |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 1218 | rcu_read_lock(); |
David Ahern | 6454743 | 2018-05-09 20:34:19 -0700 | [diff] [blame] | 1219 | fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr); |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1220 | restart: |
David Ahern | b1d4099 | 2019-04-16 14:35:59 -0700 | [diff] [blame] | 1221 | res.f6i = rcu_dereference(fn->leaf); |
| 1222 | if (!res.f6i) |
| 1223 | res.f6i = net->ipv6.fib6_null_entry; |
David Ahern | af52a52 | 2019-04-09 14:41:16 -0700 | [diff] [blame] | 1224 | else |
David Ahern | 75ef738 | 2019-04-16 14:36:07 -0700 | [diff] [blame] | 1225 | rt6_device_match(net, &res, &fl6->saddr, fl6->flowi6_oif, |
| 1226 | flags); |
David Ahern | af52a52 | 2019-04-09 14:41:16 -0700 | [diff] [blame] | 1227 | |
David Ahern | b1d4099 | 2019-04-16 14:35:59 -0700 | [diff] [blame] | 1228 | if (res.f6i == net->ipv6.fib6_null_entry) { |
Martin KaFai Lau | a3c00e4 | 2014-10-20 13:42:43 -0700 | [diff] [blame] | 1229 | fn = fib6_backtrack(fn, &fl6->saddr); |
| 1230 | if (fn) |
| 1231 | goto restart; |
David Ahern | af52a52 | 2019-04-09 14:41:16 -0700 | [diff] [blame] | 1232 | |
| 1233 | rt = net->ipv6.ip6_null_entry; |
| 1234 | dst_hold(&rt->dst); |
| 1235 | goto out; |
David Ahern | f88d8ea | 2019-06-03 20:19:52 -0700 | [diff] [blame] | 1236 | } else if (res.fib6_flags & RTF_REJECT) { |
| 1237 | goto do_create; |
Martin KaFai Lau | a3c00e4 | 2014-10-20 13:42:43 -0700 | [diff] [blame] | 1238 | } |
Wei Wang | 2b760fc | 2017-10-06 12:06:03 -0700 | [diff] [blame] | 1239 | |
David Ahern | b1d4099 | 2019-04-16 14:35:59 -0700 | [diff] [blame] | 1240 | fib6_select_path(net, &res, fl6, fl6->flowi6_oif, |
| 1241 | fl6->flowi6_oif != 0, skb, flags); |
| 1242 | |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 1243 | /* Search through exception table */ |
David Ahern | 7e4b512 | 2019-04-16 14:36:00 -0700 | [diff] [blame] | 1244 | rt = rt6_find_cached_rt(&res, &fl6->daddr, &fl6->saddr); |
David Ahern | 23fb93a | 2018-04-17 17:33:23 -0700 | [diff] [blame] | 1245 | if (rt) { |
David Ahern | 10585b4 | 2019-03-20 09:24:50 -0700 | [diff] [blame] | 1246 | if (ip6_hold_safe(net, &rt)) |
David Ahern | dec9b0e | 2018-04-17 17:33:19 -0700 | [diff] [blame] | 1247 | dst_use_noref(&rt->dst, jiffies); |
David Ahern | 23fb93a | 2018-04-17 17:33:23 -0700 | [diff] [blame] | 1248 | } else { |
David Ahern | f88d8ea | 2019-06-03 20:19:52 -0700 | [diff] [blame] | 1249 | do_create: |
David Ahern | 9b6b35a | 2019-04-16 14:36:02 -0700 | [diff] [blame] | 1250 | rt = ip6_create_rt_rcu(&res); |
David Ahern | dec9b0e | 2018-04-17 17:33:19 -0700 | [diff] [blame] | 1251 | } |
Wei Wang | d3843fe | 2017-10-06 12:06:06 -0700 | [diff] [blame] | 1252 | |
David Ahern | af52a52 | 2019-04-09 14:41:16 -0700 | [diff] [blame] | 1253 | out: |
David Ahern | 8ff2e5b | 2019-04-16 14:36:09 -0700 | [diff] [blame] | 1254 | trace_fib6_table_lookup(net, &res, table, fl6); |
David Ahern | af52a52 | 2019-04-09 14:41:16 -0700 | [diff] [blame] | 1255 | |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 1256 | rcu_read_unlock(); |
David Ahern | b811580 | 2015-11-19 12:24:22 -0800 | [diff] [blame] | 1257 | |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1258 | return rt; |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1259 | } |
| 1260 | |
Ian Morris | 67ba415 | 2014-08-24 21:53:10 +0100 | [diff] [blame] | 1261 | struct dst_entry *ip6_route_lookup(struct net *net, struct flowi6 *fl6, |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 1262 | const struct sk_buff *skb, int flags) |
Florian Westphal | ea6e574 | 2011-09-05 16:05:44 +0200 | [diff] [blame] | 1263 | { |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 1264 | return fib6_rule_lookup(net, fl6, skb, flags, ip6_pol_route_lookup); |
Florian Westphal | ea6e574 | 2011-09-05 16:05:44 +0200 | [diff] [blame] | 1265 | } |
| 1266 | EXPORT_SYMBOL_GPL(ip6_route_lookup); |
| 1267 | |
YOSHIFUJI Hideaki | 9acd9f3 | 2008-04-10 15:42:10 +0900 | [diff] [blame] | 1268 | 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] | 1269 | const struct in6_addr *saddr, int oif, |
| 1270 | const struct sk_buff *skb, int strict) |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1271 | { |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 1272 | struct flowi6 fl6 = { |
| 1273 | .flowi6_oif = oif, |
| 1274 | .daddr = *daddr, |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1275 | }; |
| 1276 | struct dst_entry *dst; |
YOSHIFUJI Hideaki | 77d16f4 | 2006-08-23 17:25:05 -0700 | [diff] [blame] | 1277 | int flags = strict ? RT6_LOOKUP_F_IFACE : 0; |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1278 | |
Thomas Graf | adaa70b | 2006-10-13 15:01:03 -0700 | [diff] [blame] | 1279 | if (saddr) { |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 1280 | memcpy(&fl6.saddr, saddr, sizeof(*saddr)); |
Thomas Graf | adaa70b | 2006-10-13 15:01:03 -0700 | [diff] [blame] | 1281 | flags |= RT6_LOOKUP_F_HAS_SADDR; |
| 1282 | } |
| 1283 | |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 1284 | dst = fib6_rule_lookup(net, &fl6, skb, flags, ip6_pol_route_lookup); |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1285 | if (dst->error == 0) |
| 1286 | return (struct rt6_info *) dst; |
| 1287 | |
| 1288 | dst_release(dst); |
| 1289 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1290 | return NULL; |
| 1291 | } |
YOSHIFUJI Hideaki | 7159039 | 2007-02-22 22:05:40 +0900 | [diff] [blame] | 1292 | EXPORT_SYMBOL(rt6_lookup); |
| 1293 | |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1294 | /* ip6_ins_rt is called with FREE table->tb6_lock. |
Wei Wang | 1cfb71e | 2017-06-17 10:42:33 -0700 | [diff] [blame] | 1295 | * It takes new route entry, the addition fails by any reason the |
| 1296 | * route is released. |
| 1297 | * Caller must hold dst before calling it. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1298 | */ |
| 1299 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 1300 | 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] | 1301 | struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1302 | { |
| 1303 | int err; |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1304 | struct fib6_table *table; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1305 | |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 1306 | table = rt->fib6_table; |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 1307 | spin_lock_bh(&table->tb6_lock); |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 1308 | err = fib6_add(&table->tb6_root, rt, info, extack); |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 1309 | spin_unlock_bh(&table->tb6_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1310 | |
| 1311 | return err; |
| 1312 | } |
| 1313 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 1314 | int ip6_ins_rt(struct net *net, struct fib6_info *rt) |
Thomas Graf | 40e22e8 | 2006-08-22 00:00:45 -0700 | [diff] [blame] | 1315 | { |
David Ahern | afb1d4b5 | 2018-04-17 17:33:11 -0700 | [diff] [blame] | 1316 | struct nl_info info = { .nl_net = net, }; |
Florian Westphal | e715b6d | 2015-01-05 23:57:44 +0100 | [diff] [blame] | 1317 | |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 1318 | return __ip6_ins_rt(rt, &info, NULL); |
Thomas Graf | 40e22e8 | 2006-08-22 00:00:45 -0700 | [diff] [blame] | 1319 | } |
| 1320 | |
David Ahern | 85bd05d | 2019-04-16 14:36:01 -0700 | [diff] [blame] | 1321 | 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] | 1322 | const struct in6_addr *daddr, |
| 1323 | const struct in6_addr *saddr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1324 | { |
David Ahern | 85bd05d | 2019-04-16 14:36:01 -0700 | [diff] [blame] | 1325 | struct fib6_info *f6i = res->f6i; |
David Ahern | 4832c30 | 2017-08-17 12:17:20 -0700 | [diff] [blame] | 1326 | struct net_device *dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1327 | struct rt6_info *rt; |
| 1328 | |
| 1329 | /* |
| 1330 | * Clone the route. |
| 1331 | */ |
| 1332 | |
David Ahern | 85bd05d | 2019-04-16 14:36:01 -0700 | [diff] [blame] | 1333 | if (!fib6_info_hold_safe(f6i)) |
Wei Wang | e873e4b | 2018-07-21 20:56:32 -0700 | [diff] [blame] | 1334 | return NULL; |
| 1335 | |
David Ahern | 0d16158 | 2019-04-16 14:36:04 -0700 | [diff] [blame] | 1336 | dev = ip6_rt_get_dev_rcu(res); |
David Ahern | 93531c6 | 2018-04-17 17:33:25 -0700 | [diff] [blame] | 1337 | rt = ip6_dst_alloc(dev_net(dev), dev, 0); |
Wei Wang | e873e4b | 2018-07-21 20:56:32 -0700 | [diff] [blame] | 1338 | if (!rt) { |
David Ahern | 85bd05d | 2019-04-16 14:36:01 -0700 | [diff] [blame] | 1339 | fib6_info_release(f6i); |
Martin KaFai Lau | 83a09ab | 2015-05-22 20:56:05 -0700 | [diff] [blame] | 1340 | return NULL; |
Wei Wang | e873e4b | 2018-07-21 20:56:32 -0700 | [diff] [blame] | 1341 | } |
Martin KaFai Lau | 83a09ab | 2015-05-22 20:56:05 -0700 | [diff] [blame] | 1342 | |
David Ahern | 0d16158 | 2019-04-16 14:36:04 -0700 | [diff] [blame] | 1343 | ip6_rt_copy_init(rt, res); |
Martin KaFai Lau | 83a09ab | 2015-05-22 20:56:05 -0700 | [diff] [blame] | 1344 | rt->rt6i_flags |= RTF_CACHE; |
Martin KaFai Lau | 83a09ab | 2015-05-22 20:56:05 -0700 | [diff] [blame] | 1345 | rt->dst.flags |= DST_HOST; |
| 1346 | rt->rt6i_dst.addr = *daddr; |
| 1347 | rt->rt6i_dst.plen = 128; |
| 1348 | |
David Ahern | 85bd05d | 2019-04-16 14:36:01 -0700 | [diff] [blame] | 1349 | if (!rt6_is_gw_or_nonexthop(res)) { |
| 1350 | if (f6i->fib6_dst.plen != 128 && |
| 1351 | ipv6_addr_equal(&f6i->fib6_dst.addr, daddr)) |
Martin KaFai Lau | 83a09ab | 2015-05-22 20:56:05 -0700 | [diff] [blame] | 1352 | rt->rt6i_flags |= RTF_ANYCAST; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1353 | #ifdef CONFIG_IPV6_SUBTREES |
Martin KaFai Lau | 83a09ab | 2015-05-22 20:56:05 -0700 | [diff] [blame] | 1354 | if (rt->rt6i_src.plen && saddr) { |
| 1355 | rt->rt6i_src.addr = *saddr; |
| 1356 | rt->rt6i_src.plen = 128; |
Martin KaFai Lau | 8b9df26 | 2015-05-22 20:55:59 -0700 | [diff] [blame] | 1357 | } |
Martin KaFai Lau | 83a09ab | 2015-05-22 20:56:05 -0700 | [diff] [blame] | 1358 | #endif |
YOSHIFUJI Hideaki | 95a9a5b | 2006-03-20 16:55:51 -0800 | [diff] [blame] | 1359 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1360 | |
YOSHIFUJI Hideaki | 95a9a5b | 2006-03-20 16:55:51 -0800 | [diff] [blame] | 1361 | return rt; |
| 1362 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1363 | |
David Ahern | db3fede | 2019-04-16 14:36:03 -0700 | [diff] [blame] | 1364 | 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] | 1365 | { |
David Ahern | db3fede | 2019-04-16 14:36:03 -0700 | [diff] [blame] | 1366 | struct fib6_info *f6i = res->f6i; |
| 1367 | unsigned short flags = fib6_info_dst_flags(f6i); |
David Ahern | 4832c30 | 2017-08-17 12:17:20 -0700 | [diff] [blame] | 1368 | struct net_device *dev; |
Martin KaFai Lau | d52d399 | 2015-05-22 20:56:06 -0700 | [diff] [blame] | 1369 | struct rt6_info *pcpu_rt; |
| 1370 | |
David Ahern | db3fede | 2019-04-16 14:36:03 -0700 | [diff] [blame] | 1371 | if (!fib6_info_hold_safe(f6i)) |
Wei Wang | e873e4b | 2018-07-21 20:56:32 -0700 | [diff] [blame] | 1372 | return NULL; |
| 1373 | |
David Ahern | 4832c30 | 2017-08-17 12:17:20 -0700 | [diff] [blame] | 1374 | rcu_read_lock(); |
David Ahern | 0d16158 | 2019-04-16 14:36:04 -0700 | [diff] [blame] | 1375 | dev = ip6_rt_get_dev_rcu(res); |
David Ahern | 93531c6 | 2018-04-17 17:33:25 -0700 | [diff] [blame] | 1376 | pcpu_rt = ip6_dst_alloc(dev_net(dev), dev, flags); |
David Ahern | 4832c30 | 2017-08-17 12:17:20 -0700 | [diff] [blame] | 1377 | rcu_read_unlock(); |
Wei Wang | e873e4b | 2018-07-21 20:56:32 -0700 | [diff] [blame] | 1378 | if (!pcpu_rt) { |
David Ahern | db3fede | 2019-04-16 14:36:03 -0700 | [diff] [blame] | 1379 | fib6_info_release(f6i); |
Martin KaFai Lau | d52d399 | 2015-05-22 20:56:06 -0700 | [diff] [blame] | 1380 | return NULL; |
Wei Wang | e873e4b | 2018-07-21 20:56:32 -0700 | [diff] [blame] | 1381 | } |
David Ahern | 0d16158 | 2019-04-16 14:36:04 -0700 | [diff] [blame] | 1382 | ip6_rt_copy_init(pcpu_rt, res); |
Martin KaFai Lau | d52d399 | 2015-05-22 20:56:06 -0700 | [diff] [blame] | 1383 | pcpu_rt->rt6i_flags |= RTF_PCPU; |
| 1384 | return pcpu_rt; |
| 1385 | } |
| 1386 | |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 1387 | /* It should be called with rcu_read_lock() acquired */ |
David Ahern | db3fede | 2019-04-16 14:36:03 -0700 | [diff] [blame] | 1388 | 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] | 1389 | { |
Eric Dumazet | c353071 | 2019-05-31 18:11:25 -0700 | [diff] [blame] | 1390 | struct rt6_info *pcpu_rt; |
Martin KaFai Lau | d52d399 | 2015-05-22 20:56:06 -0700 | [diff] [blame] | 1391 | |
Eric Dumazet | c353071 | 2019-05-31 18:11:25 -0700 | [diff] [blame] | 1392 | pcpu_rt = this_cpu_read(*res->nh->rt6i_pcpu); |
Martin KaFai Lau | d52d399 | 2015-05-22 20:56:06 -0700 | [diff] [blame] | 1393 | |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 1394 | if (pcpu_rt) |
David Ahern | 10585b4 | 2019-03-20 09:24:50 -0700 | [diff] [blame] | 1395 | ip6_hold_safe(NULL, &pcpu_rt); |
Wei Wang | d3843fe | 2017-10-06 12:06:06 -0700 | [diff] [blame] | 1396 | |
Martin KaFai Lau | a73e419 | 2015-08-14 11:05:53 -0700 | [diff] [blame] | 1397 | return pcpu_rt; |
| 1398 | } |
| 1399 | |
David Ahern | afb1d4b5 | 2018-04-17 17:33:11 -0700 | [diff] [blame] | 1400 | static struct rt6_info *rt6_make_pcpu_route(struct net *net, |
David Ahern | db3fede | 2019-04-16 14:36:03 -0700 | [diff] [blame] | 1401 | const struct fib6_result *res) |
Martin KaFai Lau | a73e419 | 2015-08-14 11:05:53 -0700 | [diff] [blame] | 1402 | { |
| 1403 | struct rt6_info *pcpu_rt, *prev, **p; |
Martin KaFai Lau | d52d399 | 2015-05-22 20:56:06 -0700 | [diff] [blame] | 1404 | |
David Ahern | db3fede | 2019-04-16 14:36:03 -0700 | [diff] [blame] | 1405 | pcpu_rt = ip6_rt_pcpu_alloc(res); |
Martin KaFai Lau | d52d399 | 2015-05-22 20:56:06 -0700 | [diff] [blame] | 1406 | if (!pcpu_rt) { |
Martin KaFai Lau | 9c7370a | 2015-08-14 11:05:54 -0700 | [diff] [blame] | 1407 | dst_hold(&net->ipv6.ip6_null_entry->dst); |
| 1408 | return net->ipv6.ip6_null_entry; |
Martin KaFai Lau | d52d399 | 2015-05-22 20:56:06 -0700 | [diff] [blame] | 1409 | } |
| 1410 | |
Martin KaFai Lau | d52d399 | 2015-05-22 20:56:06 -0700 | [diff] [blame] | 1411 | dst_hold(&pcpu_rt->dst); |
David Ahern | f40b6ae | 2019-05-22 20:27:55 -0700 | [diff] [blame] | 1412 | p = this_cpu_ptr(res->nh->rt6i_pcpu); |
Wei Wang | a94b936 | 2017-10-06 12:06:04 -0700 | [diff] [blame] | 1413 | prev = cmpxchg(p, NULL, pcpu_rt); |
Eric Dumazet | 951f788 | 2017-10-08 21:07:18 -0700 | [diff] [blame] | 1414 | BUG_ON(prev); |
Wei Wang | a94b936 | 2017-10-06 12:06:04 -0700 | [diff] [blame] | 1415 | |
Eric Dumazet | 61fb0d0 | 2019-05-15 19:39:52 -0700 | [diff] [blame] | 1416 | if (res->f6i->fib6_destroying) { |
| 1417 | struct fib6_info *from; |
| 1418 | |
| 1419 | from = xchg((__force struct fib6_info **)&pcpu_rt->from, NULL); |
| 1420 | fib6_info_release(from); |
| 1421 | } |
| 1422 | |
Martin KaFai Lau | d52d399 | 2015-05-22 20:56:06 -0700 | [diff] [blame] | 1423 | return pcpu_rt; |
| 1424 | } |
| 1425 | |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1426 | /* exception hash table implementation |
| 1427 | */ |
| 1428 | static DEFINE_SPINLOCK(rt6_exception_lock); |
| 1429 | |
| 1430 | /* Remove rt6_ex from hash table and free the memory |
| 1431 | * Caller must hold rt6_exception_lock |
| 1432 | */ |
| 1433 | static void rt6_remove_exception(struct rt6_exception_bucket *bucket, |
| 1434 | struct rt6_exception *rt6_ex) |
| 1435 | { |
Paolo Abeni | f5b51fe | 2019-02-20 18:18:12 +0100 | [diff] [blame] | 1436 | struct fib6_info *from; |
Colin Ian King | b2427e6 | 2017-10-10 18:01:16 +0100 | [diff] [blame] | 1437 | struct net *net; |
Wei Wang | 81eb844 | 2017-10-06 12:06:11 -0700 | [diff] [blame] | 1438 | |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1439 | if (!bucket || !rt6_ex) |
| 1440 | return; |
Colin Ian King | b2427e6 | 2017-10-10 18:01:16 +0100 | [diff] [blame] | 1441 | |
| 1442 | net = dev_net(rt6_ex->rt6i->dst.dev); |
Paolo Abeni | f5b51fe | 2019-02-20 18:18:12 +0100 | [diff] [blame] | 1443 | net->ipv6.rt6_stats->fib_rt_cache--; |
| 1444 | |
| 1445 | /* purge completely the exception to allow releasing the held resources: |
| 1446 | * some [sk] cache may keep the dst around for unlimited time |
| 1447 | */ |
Eric Dumazet | 0e23387 | 2019-04-28 12:22:25 -0700 | [diff] [blame] | 1448 | from = xchg((__force struct fib6_info **)&rt6_ex->rt6i->from, NULL); |
Paolo Abeni | f5b51fe | 2019-02-20 18:18:12 +0100 | [diff] [blame] | 1449 | fib6_info_release(from); |
| 1450 | dst_dev_put(&rt6_ex->rt6i->dst); |
| 1451 | |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1452 | hlist_del_rcu(&rt6_ex->hlist); |
David Ahern | 77634cc | 2018-04-17 17:33:27 -0700 | [diff] [blame] | 1453 | dst_release(&rt6_ex->rt6i->dst); |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1454 | kfree_rcu(rt6_ex, rcu); |
| 1455 | WARN_ON_ONCE(!bucket->depth); |
| 1456 | bucket->depth--; |
| 1457 | } |
| 1458 | |
| 1459 | /* Remove oldest rt6_ex in bucket and free the memory |
| 1460 | * Caller must hold rt6_exception_lock |
| 1461 | */ |
| 1462 | static void rt6_exception_remove_oldest(struct rt6_exception_bucket *bucket) |
| 1463 | { |
| 1464 | struct rt6_exception *rt6_ex, *oldest = NULL; |
| 1465 | |
| 1466 | if (!bucket) |
| 1467 | return; |
| 1468 | |
| 1469 | hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) { |
| 1470 | if (!oldest || time_before(rt6_ex->stamp, oldest->stamp)) |
| 1471 | oldest = rt6_ex; |
| 1472 | } |
| 1473 | rt6_remove_exception(bucket, oldest); |
| 1474 | } |
| 1475 | |
| 1476 | static u32 rt6_exception_hash(const struct in6_addr *dst, |
| 1477 | const struct in6_addr *src) |
| 1478 | { |
| 1479 | static u32 seed __read_mostly; |
| 1480 | u32 val; |
| 1481 | |
| 1482 | net_get_random_once(&seed, sizeof(seed)); |
| 1483 | val = jhash(dst, sizeof(*dst), seed); |
| 1484 | |
| 1485 | #ifdef CONFIG_IPV6_SUBTREES |
| 1486 | if (src) |
| 1487 | val = jhash(src, sizeof(*src), val); |
| 1488 | #endif |
| 1489 | return hash_32(val, FIB6_EXCEPTION_BUCKET_SIZE_SHIFT); |
| 1490 | } |
| 1491 | |
| 1492 | /* Helper function to find the cached rt in the hash table |
| 1493 | * and update bucket pointer to point to the bucket for this |
| 1494 | * (daddr, saddr) pair |
| 1495 | * Caller must hold rt6_exception_lock |
| 1496 | */ |
| 1497 | static struct rt6_exception * |
| 1498 | __rt6_find_exception_spinlock(struct rt6_exception_bucket **bucket, |
| 1499 | const struct in6_addr *daddr, |
| 1500 | const struct in6_addr *saddr) |
| 1501 | { |
| 1502 | struct rt6_exception *rt6_ex; |
| 1503 | u32 hval; |
| 1504 | |
| 1505 | if (!(*bucket) || !daddr) |
| 1506 | return NULL; |
| 1507 | |
| 1508 | hval = rt6_exception_hash(daddr, saddr); |
| 1509 | *bucket += hval; |
| 1510 | |
| 1511 | hlist_for_each_entry(rt6_ex, &(*bucket)->chain, hlist) { |
| 1512 | struct rt6_info *rt6 = rt6_ex->rt6i; |
| 1513 | bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr); |
| 1514 | |
| 1515 | #ifdef CONFIG_IPV6_SUBTREES |
| 1516 | if (matched && saddr) |
| 1517 | matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr); |
| 1518 | #endif |
| 1519 | if (matched) |
| 1520 | return rt6_ex; |
| 1521 | } |
| 1522 | return NULL; |
| 1523 | } |
| 1524 | |
| 1525 | /* Helper function to find the cached rt in the hash table |
| 1526 | * and update bucket pointer to point to the bucket for this |
| 1527 | * (daddr, saddr) pair |
| 1528 | * Caller must hold rcu_read_lock() |
| 1529 | */ |
| 1530 | static struct rt6_exception * |
| 1531 | __rt6_find_exception_rcu(struct rt6_exception_bucket **bucket, |
| 1532 | const struct in6_addr *daddr, |
| 1533 | const struct in6_addr *saddr) |
| 1534 | { |
| 1535 | struct rt6_exception *rt6_ex; |
| 1536 | u32 hval; |
| 1537 | |
| 1538 | WARN_ON_ONCE(!rcu_read_lock_held()); |
| 1539 | |
| 1540 | if (!(*bucket) || !daddr) |
| 1541 | return NULL; |
| 1542 | |
| 1543 | hval = rt6_exception_hash(daddr, saddr); |
| 1544 | *bucket += hval; |
| 1545 | |
| 1546 | hlist_for_each_entry_rcu(rt6_ex, &(*bucket)->chain, hlist) { |
| 1547 | struct rt6_info *rt6 = rt6_ex->rt6i; |
| 1548 | bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr); |
| 1549 | |
| 1550 | #ifdef CONFIG_IPV6_SUBTREES |
| 1551 | if (matched && saddr) |
| 1552 | matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr); |
| 1553 | #endif |
| 1554 | if (matched) |
| 1555 | return rt6_ex; |
| 1556 | } |
| 1557 | return NULL; |
| 1558 | } |
| 1559 | |
David Ahern | b748f26 | 2019-04-16 14:36:06 -0700 | [diff] [blame] | 1560 | static unsigned int fib6_mtu(const struct fib6_result *res) |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1561 | { |
David Ahern | b748f26 | 2019-04-16 14:36:06 -0700 | [diff] [blame] | 1562 | const struct fib6_nh *nh = res->nh; |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 1563 | unsigned int mtu; |
| 1564 | |
David Ahern | b748f26 | 2019-04-16 14:36:06 -0700 | [diff] [blame] | 1565 | if (res->f6i->fib6_pmtu) { |
| 1566 | mtu = res->f6i->fib6_pmtu; |
David Ahern | dcd1f57 | 2018-04-18 15:39:05 -0700 | [diff] [blame] | 1567 | } else { |
David Ahern | b748f26 | 2019-04-16 14:36:06 -0700 | [diff] [blame] | 1568 | struct net_device *dev = nh->fib_nh_dev; |
David Ahern | dcd1f57 | 2018-04-18 15:39:05 -0700 | [diff] [blame] | 1569 | struct inet6_dev *idev; |
| 1570 | |
| 1571 | rcu_read_lock(); |
| 1572 | idev = __in6_dev_get(dev); |
| 1573 | mtu = idev->cnf.mtu6; |
| 1574 | rcu_read_unlock(); |
| 1575 | } |
| 1576 | |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 1577 | mtu = min_t(unsigned int, mtu, IP6_MAX_MTU); |
| 1578 | |
David Ahern | b748f26 | 2019-04-16 14:36:06 -0700 | [diff] [blame] | 1579 | return mtu - lwtunnel_headroom(nh->fib_nh_lws, mtu); |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 1580 | } |
| 1581 | |
David Ahern | cc5c073 | 2019-05-22 20:27:58 -0700 | [diff] [blame] | 1582 | #define FIB6_EXCEPTION_BUCKET_FLUSHED 0x1UL |
| 1583 | |
| 1584 | /* used when the flushed bit is not relevant, only access to the bucket |
| 1585 | * (ie., all bucket users except rt6_insert_exception); |
| 1586 | * |
| 1587 | * called under rcu lock; sometimes called with rt6_exception_lock held |
| 1588 | */ |
| 1589 | static |
| 1590 | struct rt6_exception_bucket *fib6_nh_get_excptn_bucket(const struct fib6_nh *nh, |
| 1591 | spinlock_t *lock) |
| 1592 | { |
| 1593 | struct rt6_exception_bucket *bucket; |
| 1594 | |
| 1595 | if (lock) |
| 1596 | bucket = rcu_dereference_protected(nh->rt6i_exception_bucket, |
| 1597 | lockdep_is_held(lock)); |
| 1598 | else |
| 1599 | bucket = rcu_dereference(nh->rt6i_exception_bucket); |
| 1600 | |
| 1601 | /* remove bucket flushed bit if set */ |
| 1602 | if (bucket) { |
| 1603 | unsigned long p = (unsigned long)bucket; |
| 1604 | |
| 1605 | p &= ~FIB6_EXCEPTION_BUCKET_FLUSHED; |
| 1606 | bucket = (struct rt6_exception_bucket *)p; |
| 1607 | } |
| 1608 | |
| 1609 | return bucket; |
| 1610 | } |
| 1611 | |
| 1612 | static bool fib6_nh_excptn_bucket_flushed(struct rt6_exception_bucket *bucket) |
| 1613 | { |
| 1614 | unsigned long p = (unsigned long)bucket; |
| 1615 | |
| 1616 | return !!(p & FIB6_EXCEPTION_BUCKET_FLUSHED); |
| 1617 | } |
| 1618 | |
| 1619 | /* called with rt6_exception_lock held */ |
| 1620 | static void fib6_nh_excptn_bucket_set_flushed(struct fib6_nh *nh, |
| 1621 | spinlock_t *lock) |
| 1622 | { |
| 1623 | struct rt6_exception_bucket *bucket; |
| 1624 | unsigned long p; |
| 1625 | |
| 1626 | bucket = rcu_dereference_protected(nh->rt6i_exception_bucket, |
| 1627 | lockdep_is_held(lock)); |
| 1628 | |
| 1629 | p = (unsigned long)bucket; |
| 1630 | p |= FIB6_EXCEPTION_BUCKET_FLUSHED; |
| 1631 | bucket = (struct rt6_exception_bucket *)p; |
| 1632 | rcu_assign_pointer(nh->rt6i_exception_bucket, bucket); |
| 1633 | } |
| 1634 | |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1635 | static int rt6_insert_exception(struct rt6_info *nrt, |
David Ahern | 5012f0a | 2019-04-16 14:36:05 -0700 | [diff] [blame] | 1636 | const struct fib6_result *res) |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1637 | { |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 1638 | struct net *net = dev_net(nrt->dst.dev); |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1639 | struct rt6_exception_bucket *bucket; |
David Ahern | cc5c073 | 2019-05-22 20:27:58 -0700 | [diff] [blame] | 1640 | struct fib6_info *f6i = res->f6i; |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1641 | struct in6_addr *src_key = NULL; |
| 1642 | struct rt6_exception *rt6_ex; |
David Ahern | cc5c073 | 2019-05-22 20:27:58 -0700 | [diff] [blame] | 1643 | struct fib6_nh *nh = res->nh; |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1644 | int err = 0; |
| 1645 | |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1646 | spin_lock_bh(&rt6_exception_lock); |
| 1647 | |
David Ahern | cc5c073 | 2019-05-22 20:27:58 -0700 | [diff] [blame] | 1648 | bucket = rcu_dereference_protected(nh->rt6i_exception_bucket, |
| 1649 | lockdep_is_held(&rt6_exception_lock)); |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1650 | if (!bucket) { |
| 1651 | bucket = kcalloc(FIB6_EXCEPTION_BUCKET_SIZE, sizeof(*bucket), |
| 1652 | GFP_ATOMIC); |
| 1653 | if (!bucket) { |
| 1654 | err = -ENOMEM; |
| 1655 | goto out; |
| 1656 | } |
David Ahern | cc5c073 | 2019-05-22 20:27:58 -0700 | [diff] [blame] | 1657 | rcu_assign_pointer(nh->rt6i_exception_bucket, bucket); |
| 1658 | } else if (fib6_nh_excptn_bucket_flushed(bucket)) { |
| 1659 | err = -EINVAL; |
| 1660 | goto out; |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1661 | } |
| 1662 | |
| 1663 | #ifdef CONFIG_IPV6_SUBTREES |
David Ahern | 5012f0a | 2019-04-16 14:36:05 -0700 | [diff] [blame] | 1664 | /* fib6_src.plen != 0 indicates f6i is in subtree |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1665 | * and exception table is indexed by a hash of |
David Ahern | 5012f0a | 2019-04-16 14:36:05 -0700 | [diff] [blame] | 1666 | * both fib6_dst and fib6_src. |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1667 | * Otherwise, the exception table is indexed by |
David Ahern | 5012f0a | 2019-04-16 14:36:05 -0700 | [diff] [blame] | 1668 | * a hash of only fib6_dst. |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1669 | */ |
David Ahern | 5012f0a | 2019-04-16 14:36:05 -0700 | [diff] [blame] | 1670 | if (f6i->fib6_src.plen) |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1671 | src_key = &nrt->rt6i_src.addr; |
| 1672 | #endif |
David Ahern | 5012f0a | 2019-04-16 14:36:05 -0700 | [diff] [blame] | 1673 | /* rt6_mtu_change() might lower mtu on f6i. |
Wei Wang | f5bbe7e | 2017-10-06 12:05:59 -0700 | [diff] [blame] | 1674 | * Only insert this exception route if its mtu |
David Ahern | 5012f0a | 2019-04-16 14:36:05 -0700 | [diff] [blame] | 1675 | * is less than f6i's mtu value. |
Wei Wang | f5bbe7e | 2017-10-06 12:05:59 -0700 | [diff] [blame] | 1676 | */ |
David Ahern | b748f26 | 2019-04-16 14:36:06 -0700 | [diff] [blame] | 1677 | if (dst_metric_raw(&nrt->dst, RTAX_MTU) >= fib6_mtu(res)) { |
Wei Wang | f5bbe7e | 2017-10-06 12:05:59 -0700 | [diff] [blame] | 1678 | err = -EINVAL; |
| 1679 | goto out; |
| 1680 | } |
Wei Wang | 60006a4 | 2017-10-06 12:05:58 -0700 | [diff] [blame] | 1681 | |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1682 | rt6_ex = __rt6_find_exception_spinlock(&bucket, &nrt->rt6i_dst.addr, |
| 1683 | src_key); |
| 1684 | if (rt6_ex) |
| 1685 | rt6_remove_exception(bucket, rt6_ex); |
| 1686 | |
| 1687 | rt6_ex = kzalloc(sizeof(*rt6_ex), GFP_ATOMIC); |
| 1688 | if (!rt6_ex) { |
| 1689 | err = -ENOMEM; |
| 1690 | goto out; |
| 1691 | } |
| 1692 | rt6_ex->rt6i = nrt; |
| 1693 | rt6_ex->stamp = jiffies; |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1694 | hlist_add_head_rcu(&rt6_ex->hlist, &bucket->chain); |
| 1695 | bucket->depth++; |
Wei Wang | 81eb844 | 2017-10-06 12:06:11 -0700 | [diff] [blame] | 1696 | net->ipv6.rt6_stats->fib_rt_cache++; |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1697 | |
| 1698 | if (bucket->depth > FIB6_MAX_DEPTH) |
| 1699 | rt6_exception_remove_oldest(bucket); |
| 1700 | |
| 1701 | out: |
| 1702 | spin_unlock_bh(&rt6_exception_lock); |
| 1703 | |
| 1704 | /* Update fn->fn_sernum to invalidate all cached dst */ |
Paolo Abeni | b886d5f | 2017-10-19 16:07:10 +0200 | [diff] [blame] | 1705 | if (!err) { |
David Ahern | 5012f0a | 2019-04-16 14:36:05 -0700 | [diff] [blame] | 1706 | spin_lock_bh(&f6i->fib6_table->tb6_lock); |
| 1707 | fib6_update_sernum(net, f6i); |
| 1708 | spin_unlock_bh(&f6i->fib6_table->tb6_lock); |
Paolo Abeni | b886d5f | 2017-10-19 16:07:10 +0200 | [diff] [blame] | 1709 | fib6_force_start_gc(net); |
| 1710 | } |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1711 | |
| 1712 | return err; |
| 1713 | } |
| 1714 | |
David Ahern | c0b220c | 2019-05-22 20:27:57 -0700 | [diff] [blame] | 1715 | 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] | 1716 | { |
| 1717 | struct rt6_exception_bucket *bucket; |
| 1718 | struct rt6_exception *rt6_ex; |
| 1719 | struct hlist_node *tmp; |
| 1720 | int i; |
| 1721 | |
| 1722 | spin_lock_bh(&rt6_exception_lock); |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1723 | |
David Ahern | cc5c073 | 2019-05-22 20:27:58 -0700 | [diff] [blame] | 1724 | bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock); |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1725 | if (!bucket) |
| 1726 | goto out; |
| 1727 | |
David Ahern | cc5c073 | 2019-05-22 20:27:58 -0700 | [diff] [blame] | 1728 | /* Prevent rt6_insert_exception() to recreate the bucket list */ |
| 1729 | if (!from) |
| 1730 | fib6_nh_excptn_bucket_set_flushed(nh, &rt6_exception_lock); |
| 1731 | |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1732 | for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) { |
David Ahern | cc5c073 | 2019-05-22 20:27:58 -0700 | [diff] [blame] | 1733 | hlist_for_each_entry_safe(rt6_ex, tmp, &bucket->chain, hlist) { |
| 1734 | if (!from || |
| 1735 | rcu_access_pointer(rt6_ex->rt6i->from) == from) |
| 1736 | rt6_remove_exception(bucket, rt6_ex); |
| 1737 | } |
| 1738 | WARN_ON_ONCE(!from && bucket->depth); |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1739 | bucket++; |
| 1740 | } |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1741 | out: |
| 1742 | spin_unlock_bh(&rt6_exception_lock); |
| 1743 | } |
| 1744 | |
David Ahern | c0b220c | 2019-05-22 20:27:57 -0700 | [diff] [blame] | 1745 | void rt6_flush_exceptions(struct fib6_info *f6i) |
| 1746 | { |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 1747 | fib6_nh_flush_exceptions(f6i->fib6_nh, f6i); |
David Ahern | c0b220c | 2019-05-22 20:27:57 -0700 | [diff] [blame] | 1748 | } |
| 1749 | |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1750 | /* Find cached rt in the hash table inside passed in rt |
| 1751 | * Caller has to hold rcu_read_lock() |
| 1752 | */ |
David Ahern | 7e4b512 | 2019-04-16 14:36:00 -0700 | [diff] [blame] | 1753 | 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] | 1754 | const struct in6_addr *daddr, |
| 1755 | const struct in6_addr *saddr) |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1756 | { |
Wei Wang | 510e2ce | 2019-05-16 13:30:54 -0700 | [diff] [blame] | 1757 | const struct in6_addr *src_key = NULL; |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1758 | struct rt6_exception_bucket *bucket; |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1759 | struct rt6_exception *rt6_ex; |
David Ahern | 7e4b512 | 2019-04-16 14:36:00 -0700 | [diff] [blame] | 1760 | struct rt6_info *ret = NULL; |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1761 | |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1762 | #ifdef CONFIG_IPV6_SUBTREES |
David Ahern | 7e4b512 | 2019-04-16 14:36:00 -0700 | [diff] [blame] | 1763 | /* fib6i_src.plen != 0 indicates f6i is in subtree |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1764 | * and exception table is indexed by a hash of |
David Ahern | 7e4b512 | 2019-04-16 14:36:00 -0700 | [diff] [blame] | 1765 | * both fib6_dst and fib6_src. |
Wei Wang | 510e2ce | 2019-05-16 13:30:54 -0700 | [diff] [blame] | 1766 | * However, the src addr used to create the hash |
| 1767 | * might not be exactly the passed in saddr which |
| 1768 | * is a /128 addr from the flow. |
| 1769 | * So we need to use f6i->fib6_src to redo lookup |
| 1770 | * if the passed in saddr does not find anything. |
| 1771 | * (See the logic in ip6_rt_cache_alloc() on how |
| 1772 | * rt->rt6i_src is updated.) |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1773 | */ |
David Ahern | 7e4b512 | 2019-04-16 14:36:00 -0700 | [diff] [blame] | 1774 | if (res->f6i->fib6_src.plen) |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1775 | src_key = saddr; |
Wei Wang | 510e2ce | 2019-05-16 13:30:54 -0700 | [diff] [blame] | 1776 | find_ex: |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1777 | #endif |
David Ahern | cc5c073 | 2019-05-22 20:27:58 -0700 | [diff] [blame] | 1778 | bucket = fib6_nh_get_excptn_bucket(res->nh, NULL); |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1779 | rt6_ex = __rt6_find_exception_rcu(&bucket, daddr, src_key); |
| 1780 | |
| 1781 | if (rt6_ex && !rt6_check_expired(rt6_ex->rt6i)) |
David Ahern | 7e4b512 | 2019-04-16 14:36:00 -0700 | [diff] [blame] | 1782 | ret = rt6_ex->rt6i; |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1783 | |
Wei Wang | 510e2ce | 2019-05-16 13:30:54 -0700 | [diff] [blame] | 1784 | #ifdef CONFIG_IPV6_SUBTREES |
| 1785 | /* Use fib6_src as src_key and redo lookup */ |
| 1786 | if (!ret && src_key && src_key != &res->f6i->fib6_src.addr) { |
| 1787 | src_key = &res->f6i->fib6_src.addr; |
| 1788 | goto find_ex; |
| 1789 | } |
| 1790 | #endif |
| 1791 | |
David Ahern | 7e4b512 | 2019-04-16 14:36:00 -0700 | [diff] [blame] | 1792 | return ret; |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1793 | } |
| 1794 | |
| 1795 | /* 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] | 1796 | 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] | 1797 | const struct rt6_info *rt) |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1798 | { |
David Ahern | c0b220c | 2019-05-22 20:27:57 -0700 | [diff] [blame] | 1799 | const struct in6_addr *src_key = NULL; |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1800 | struct rt6_exception_bucket *bucket; |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1801 | struct rt6_exception *rt6_ex; |
| 1802 | int err; |
| 1803 | |
David Ahern | cc5c073 | 2019-05-22 20:27:58 -0700 | [diff] [blame] | 1804 | if (!rcu_access_pointer(nh->rt6i_exception_bucket)) |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1805 | return -ENOENT; |
| 1806 | |
| 1807 | spin_lock_bh(&rt6_exception_lock); |
David Ahern | cc5c073 | 2019-05-22 20:27:58 -0700 | [diff] [blame] | 1808 | bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock); |
| 1809 | |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1810 | #ifdef CONFIG_IPV6_SUBTREES |
David Ahern | cc5c073 | 2019-05-22 20:27:58 -0700 | [diff] [blame] | 1811 | /* rt6i_src.plen != 0 indicates 'from' is in subtree |
| 1812 | * and exception table is indexed by a hash of |
| 1813 | * both rt6i_dst and rt6i_src. |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1814 | * Otherwise, the exception table is indexed by |
| 1815 | * a hash of only rt6i_dst. |
| 1816 | */ |
David Ahern | c0b220c | 2019-05-22 20:27:57 -0700 | [diff] [blame] | 1817 | if (plen) |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1818 | src_key = &rt->rt6i_src.addr; |
| 1819 | #endif |
| 1820 | rt6_ex = __rt6_find_exception_spinlock(&bucket, |
| 1821 | &rt->rt6i_dst.addr, |
| 1822 | src_key); |
| 1823 | if (rt6_ex) { |
| 1824 | rt6_remove_exception(bucket, rt6_ex); |
| 1825 | err = 0; |
| 1826 | } else { |
| 1827 | err = -ENOENT; |
| 1828 | } |
| 1829 | |
| 1830 | spin_unlock_bh(&rt6_exception_lock); |
| 1831 | return err; |
| 1832 | } |
| 1833 | |
David Ahern | c0b220c | 2019-05-22 20:27:57 -0700 | [diff] [blame] | 1834 | static int rt6_remove_exception_rt(struct rt6_info *rt) |
| 1835 | { |
| 1836 | struct fib6_info *from; |
| 1837 | |
| 1838 | from = rcu_dereference(rt->from); |
David Ahern | cc5c073 | 2019-05-22 20:27:58 -0700 | [diff] [blame] | 1839 | if (!from || !(rt->rt6i_flags & RTF_CACHE)) |
David Ahern | c0b220c | 2019-05-22 20:27:57 -0700 | [diff] [blame] | 1840 | return -EINVAL; |
| 1841 | |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 1842 | return fib6_nh_remove_exception(from->fib6_nh, |
David Ahern | cc5c073 | 2019-05-22 20:27:58 -0700 | [diff] [blame] | 1843 | from->fib6_src.plen, rt); |
David Ahern | c0b220c | 2019-05-22 20:27:57 -0700 | [diff] [blame] | 1844 | } |
| 1845 | |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1846 | /* Find rt6_ex which contains the passed in rt cache and |
| 1847 | * refresh its stamp |
| 1848 | */ |
David Ahern | cc5c073 | 2019-05-22 20:27:58 -0700 | [diff] [blame] | 1849 | 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] | 1850 | const struct rt6_info *rt) |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1851 | { |
David Ahern | c0b220c | 2019-05-22 20:27:57 -0700 | [diff] [blame] | 1852 | const struct in6_addr *src_key = NULL; |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1853 | struct rt6_exception_bucket *bucket; |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1854 | struct rt6_exception *rt6_ex; |
Paolo Abeni | 193f368 | 2019-02-21 11:19:41 +0100 | [diff] [blame] | 1855 | |
David Ahern | cc5c073 | 2019-05-22 20:27:58 -0700 | [diff] [blame] | 1856 | bucket = fib6_nh_get_excptn_bucket(nh, NULL); |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1857 | #ifdef CONFIG_IPV6_SUBTREES |
David Ahern | cc5c073 | 2019-05-22 20:27:58 -0700 | [diff] [blame] | 1858 | /* rt6i_src.plen != 0 indicates 'from' is in subtree |
| 1859 | * and exception table is indexed by a hash of |
| 1860 | * both rt6i_dst and rt6i_src. |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1861 | * Otherwise, the exception table is indexed by |
| 1862 | * a hash of only rt6i_dst. |
| 1863 | */ |
David Ahern | c0b220c | 2019-05-22 20:27:57 -0700 | [diff] [blame] | 1864 | if (plen) |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1865 | src_key = &rt->rt6i_src.addr; |
| 1866 | #endif |
David Ahern | cc5c073 | 2019-05-22 20:27:58 -0700 | [diff] [blame] | 1867 | 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] | 1868 | if (rt6_ex) |
| 1869 | rt6_ex->stamp = jiffies; |
David Ahern | c0b220c | 2019-05-22 20:27:57 -0700 | [diff] [blame] | 1870 | } |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1871 | |
David Ahern | c0b220c | 2019-05-22 20:27:57 -0700 | [diff] [blame] | 1872 | static void rt6_update_exception_stamp_rt(struct rt6_info *rt) |
| 1873 | { |
| 1874 | struct fib6_info *from; |
| 1875 | |
| 1876 | rcu_read_lock(); |
| 1877 | |
| 1878 | from = rcu_dereference(rt->from); |
| 1879 | if (!from || !(rt->rt6i_flags & RTF_CACHE)) |
| 1880 | goto unlock; |
| 1881 | |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 1882 | fib6_nh_update_exception(from->fib6_nh, from->fib6_src.plen, rt); |
Paolo Abeni | 193f368 | 2019-02-21 11:19:41 +0100 | [diff] [blame] | 1883 | unlock: |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1884 | rcu_read_unlock(); |
| 1885 | } |
| 1886 | |
Stefano Brivio | e9fa149 | 2018-03-06 11:10:19 +0100 | [diff] [blame] | 1887 | static bool rt6_mtu_change_route_allowed(struct inet6_dev *idev, |
| 1888 | struct rt6_info *rt, int mtu) |
| 1889 | { |
| 1890 | /* If the new MTU is lower than the route PMTU, this new MTU will be the |
| 1891 | * lowest MTU in the path: always allow updating the route PMTU to |
| 1892 | * reflect PMTU decreases. |
| 1893 | * |
| 1894 | * If the new MTU is higher, and the route PMTU is equal to the local |
| 1895 | * MTU, this means the old MTU is the lowest in the path, so allow |
| 1896 | * updating it: if other nodes now have lower MTUs, PMTU discovery will |
| 1897 | * handle this. |
| 1898 | */ |
| 1899 | |
| 1900 | if (dst_mtu(&rt->dst) >= mtu) |
| 1901 | return true; |
| 1902 | |
| 1903 | if (dst_mtu(&rt->dst) == idev->cnf.mtu6) |
| 1904 | return true; |
| 1905 | |
| 1906 | return false; |
| 1907 | } |
| 1908 | |
| 1909 | static void rt6_exceptions_update_pmtu(struct inet6_dev *idev, |
David Ahern | cc5c073 | 2019-05-22 20:27:58 -0700 | [diff] [blame] | 1910 | const struct fib6_nh *nh, int mtu) |
Wei Wang | f5bbe7e | 2017-10-06 12:05:59 -0700 | [diff] [blame] | 1911 | { |
| 1912 | struct rt6_exception_bucket *bucket; |
| 1913 | struct rt6_exception *rt6_ex; |
| 1914 | int i; |
| 1915 | |
David Ahern | cc5c073 | 2019-05-22 20:27:58 -0700 | [diff] [blame] | 1916 | bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock); |
Stefano Brivio | e9fa149 | 2018-03-06 11:10:19 +0100 | [diff] [blame] | 1917 | if (!bucket) |
| 1918 | return; |
| 1919 | |
| 1920 | for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) { |
| 1921 | hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) { |
| 1922 | struct rt6_info *entry = rt6_ex->rt6i; |
| 1923 | |
| 1924 | /* For RTF_CACHE with rt6i_pmtu == 0 (i.e. a redirected |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 1925 | * route), the metrics of its rt->from have already |
Stefano Brivio | e9fa149 | 2018-03-06 11:10:19 +0100 | [diff] [blame] | 1926 | * been updated. |
| 1927 | */ |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 1928 | if (dst_metric_raw(&entry->dst, RTAX_MTU) && |
Stefano Brivio | e9fa149 | 2018-03-06 11:10:19 +0100 | [diff] [blame] | 1929 | rt6_mtu_change_route_allowed(idev, entry, mtu)) |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 1930 | dst_metric_set(&entry->dst, RTAX_MTU, mtu); |
Wei Wang | f5bbe7e | 2017-10-06 12:05:59 -0700 | [diff] [blame] | 1931 | } |
Stefano Brivio | e9fa149 | 2018-03-06 11:10:19 +0100 | [diff] [blame] | 1932 | bucket++; |
Wei Wang | f5bbe7e | 2017-10-06 12:05:59 -0700 | [diff] [blame] | 1933 | } |
| 1934 | } |
| 1935 | |
Wei Wang | b16cb45 | 2017-10-06 12:06:00 -0700 | [diff] [blame] | 1936 | #define RTF_CACHE_GATEWAY (RTF_GATEWAY | RTF_CACHE) |
| 1937 | |
David Ahern | cc5c073 | 2019-05-22 20:27:58 -0700 | [diff] [blame] | 1938 | static void fib6_nh_exceptions_clean_tohost(const struct fib6_nh *nh, |
| 1939 | const struct in6_addr *gateway) |
Wei Wang | b16cb45 | 2017-10-06 12:06:00 -0700 | [diff] [blame] | 1940 | { |
| 1941 | struct rt6_exception_bucket *bucket; |
| 1942 | struct rt6_exception *rt6_ex; |
| 1943 | struct hlist_node *tmp; |
| 1944 | int i; |
| 1945 | |
David Ahern | cc5c073 | 2019-05-22 20:27:58 -0700 | [diff] [blame] | 1946 | if (!rcu_access_pointer(nh->rt6i_exception_bucket)) |
Wei Wang | b16cb45 | 2017-10-06 12:06:00 -0700 | [diff] [blame] | 1947 | return; |
| 1948 | |
| 1949 | spin_lock_bh(&rt6_exception_lock); |
David Ahern | cc5c073 | 2019-05-22 20:27:58 -0700 | [diff] [blame] | 1950 | bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock); |
Wei Wang | b16cb45 | 2017-10-06 12:06:00 -0700 | [diff] [blame] | 1951 | if (bucket) { |
| 1952 | for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) { |
| 1953 | hlist_for_each_entry_safe(rt6_ex, tmp, |
| 1954 | &bucket->chain, hlist) { |
| 1955 | struct rt6_info *entry = rt6_ex->rt6i; |
| 1956 | |
| 1957 | if ((entry->rt6i_flags & RTF_CACHE_GATEWAY) == |
| 1958 | RTF_CACHE_GATEWAY && |
| 1959 | ipv6_addr_equal(gateway, |
| 1960 | &entry->rt6i_gateway)) { |
| 1961 | rt6_remove_exception(bucket, rt6_ex); |
| 1962 | } |
| 1963 | } |
| 1964 | bucket++; |
| 1965 | } |
| 1966 | } |
| 1967 | |
| 1968 | spin_unlock_bh(&rt6_exception_lock); |
| 1969 | } |
| 1970 | |
Wei Wang | c757faa | 2017-10-06 12:06:01 -0700 | [diff] [blame] | 1971 | static void rt6_age_examine_exception(struct rt6_exception_bucket *bucket, |
| 1972 | struct rt6_exception *rt6_ex, |
| 1973 | struct fib6_gc_args *gc_args, |
| 1974 | unsigned long now) |
| 1975 | { |
| 1976 | struct rt6_info *rt = rt6_ex->rt6i; |
| 1977 | |
Paolo Abeni | 1859bac | 2017-10-19 16:07:11 +0200 | [diff] [blame] | 1978 | /* we are pruning and obsoleting aged-out and non gateway exceptions |
| 1979 | * even if others have still references to them, so that on next |
| 1980 | * dst_check() such references can be dropped. |
| 1981 | * EXPIRES exceptions - e.g. pmtu-generated ones are pruned when |
| 1982 | * expired, independently from their aging, as per RFC 8201 section 4 |
| 1983 | */ |
Wei Wang | 31afeb4 | 2018-01-26 11:40:17 -0800 | [diff] [blame] | 1984 | if (!(rt->rt6i_flags & RTF_EXPIRES)) { |
| 1985 | if (time_after_eq(now, rt->dst.lastuse + gc_args->timeout)) { |
| 1986 | RT6_TRACE("aging clone %p\n", rt); |
| 1987 | rt6_remove_exception(bucket, rt6_ex); |
| 1988 | return; |
| 1989 | } |
| 1990 | } else if (time_after(jiffies, rt->dst.expires)) { |
| 1991 | RT6_TRACE("purging expired route %p\n", rt); |
Wei Wang | c757faa | 2017-10-06 12:06:01 -0700 | [diff] [blame] | 1992 | rt6_remove_exception(bucket, rt6_ex); |
| 1993 | return; |
Wei Wang | 31afeb4 | 2018-01-26 11:40:17 -0800 | [diff] [blame] | 1994 | } |
| 1995 | |
| 1996 | if (rt->rt6i_flags & RTF_GATEWAY) { |
Wei Wang | c757faa | 2017-10-06 12:06:01 -0700 | [diff] [blame] | 1997 | struct neighbour *neigh; |
| 1998 | __u8 neigh_flags = 0; |
| 1999 | |
Eric Dumazet | 1bfa26f | 2018-03-23 07:56:58 -0700 | [diff] [blame] | 2000 | neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway); |
| 2001 | if (neigh) |
Wei Wang | c757faa | 2017-10-06 12:06:01 -0700 | [diff] [blame] | 2002 | neigh_flags = neigh->flags; |
Eric Dumazet | 1bfa26f | 2018-03-23 07:56:58 -0700 | [diff] [blame] | 2003 | |
Wei Wang | c757faa | 2017-10-06 12:06:01 -0700 | [diff] [blame] | 2004 | if (!(neigh_flags & NTF_ROUTER)) { |
| 2005 | RT6_TRACE("purging route %p via non-router but gateway\n", |
| 2006 | rt); |
| 2007 | rt6_remove_exception(bucket, rt6_ex); |
| 2008 | return; |
| 2009 | } |
| 2010 | } |
Wei Wang | 31afeb4 | 2018-01-26 11:40:17 -0800 | [diff] [blame] | 2011 | |
Wei Wang | c757faa | 2017-10-06 12:06:01 -0700 | [diff] [blame] | 2012 | gc_args->more++; |
| 2013 | } |
| 2014 | |
David Ahern | cc5c073 | 2019-05-22 20:27:58 -0700 | [diff] [blame] | 2015 | static void fib6_nh_age_exceptions(const struct fib6_nh *nh, |
David Ahern | c0b220c | 2019-05-22 20:27:57 -0700 | [diff] [blame] | 2016 | struct fib6_gc_args *gc_args, |
| 2017 | unsigned long now) |
Wei Wang | c757faa | 2017-10-06 12:06:01 -0700 | [diff] [blame] | 2018 | { |
| 2019 | struct rt6_exception_bucket *bucket; |
| 2020 | struct rt6_exception *rt6_ex; |
| 2021 | struct hlist_node *tmp; |
| 2022 | int i; |
| 2023 | |
David Ahern | cc5c073 | 2019-05-22 20:27:58 -0700 | [diff] [blame] | 2024 | if (!rcu_access_pointer(nh->rt6i_exception_bucket)) |
Wei Wang | c757faa | 2017-10-06 12:06:01 -0700 | [diff] [blame] | 2025 | return; |
| 2026 | |
Eric Dumazet | 1bfa26f | 2018-03-23 07:56:58 -0700 | [diff] [blame] | 2027 | rcu_read_lock_bh(); |
| 2028 | spin_lock(&rt6_exception_lock); |
David Ahern | cc5c073 | 2019-05-22 20:27:58 -0700 | [diff] [blame] | 2029 | bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock); |
Wei Wang | c757faa | 2017-10-06 12:06:01 -0700 | [diff] [blame] | 2030 | if (bucket) { |
| 2031 | for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) { |
| 2032 | hlist_for_each_entry_safe(rt6_ex, tmp, |
| 2033 | &bucket->chain, hlist) { |
| 2034 | rt6_age_examine_exception(bucket, rt6_ex, |
| 2035 | gc_args, now); |
| 2036 | } |
| 2037 | bucket++; |
| 2038 | } |
| 2039 | } |
Eric Dumazet | 1bfa26f | 2018-03-23 07:56:58 -0700 | [diff] [blame] | 2040 | spin_unlock(&rt6_exception_lock); |
| 2041 | rcu_read_unlock_bh(); |
Wei Wang | c757faa | 2017-10-06 12:06:01 -0700 | [diff] [blame] | 2042 | } |
| 2043 | |
David Ahern | cc5c073 | 2019-05-22 20:27:58 -0700 | [diff] [blame] | 2044 | void rt6_age_exceptions(struct fib6_info *f6i, |
David Ahern | c0b220c | 2019-05-22 20:27:57 -0700 | [diff] [blame] | 2045 | struct fib6_gc_args *gc_args, |
| 2046 | unsigned long now) |
| 2047 | { |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 2048 | fib6_nh_age_exceptions(f6i->fib6_nh, gc_args, now); |
David Ahern | c0b220c | 2019-05-22 20:27:57 -0700 | [diff] [blame] | 2049 | } |
| 2050 | |
David Ahern | 1d053da | 2018-05-09 20:34:21 -0700 | [diff] [blame] | 2051 | /* must be called with rcu lock held */ |
David Ahern | effda4d | 2019-04-16 14:36:10 -0700 | [diff] [blame] | 2052 | int fib6_table_lookup(struct net *net, struct fib6_table *table, int oif, |
| 2053 | struct flowi6 *fl6, struct fib6_result *res, int strict) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2054 | { |
Martin KaFai Lau | 367efcb | 2014-10-20 13:42:45 -0700 | [diff] [blame] | 2055 | struct fib6_node *fn, *saved_fn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2056 | |
David Ahern | 6454743 | 2018-05-09 20:34:19 -0700 | [diff] [blame] | 2057 | fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr); |
Martin KaFai Lau | 367efcb | 2014-10-20 13:42:45 -0700 | [diff] [blame] | 2058 | saved_fn = fn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2059 | |
David Ahern | ca25449 | 2015-10-12 11:47:10 -0700 | [diff] [blame] | 2060 | if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF) |
| 2061 | oif = 0; |
| 2062 | |
Martin KaFai Lau | a3c00e4 | 2014-10-20 13:42:43 -0700 | [diff] [blame] | 2063 | redo_rt6_select: |
David Ahern | effda4d | 2019-04-16 14:36:10 -0700 | [diff] [blame] | 2064 | rt6_select(net, fn, oif, res, strict); |
| 2065 | if (res->f6i == net->ipv6.fib6_null_entry) { |
Martin KaFai Lau | a3c00e4 | 2014-10-20 13:42:43 -0700 | [diff] [blame] | 2066 | fn = fib6_backtrack(fn, &fl6->saddr); |
| 2067 | if (fn) |
| 2068 | goto redo_rt6_select; |
Martin KaFai Lau | 367efcb | 2014-10-20 13:42:45 -0700 | [diff] [blame] | 2069 | else if (strict & RT6_LOOKUP_F_REACHABLE) { |
| 2070 | /* also consider unreachable route */ |
| 2071 | strict &= ~RT6_LOOKUP_F_REACHABLE; |
| 2072 | fn = saved_fn; |
| 2073 | goto redo_rt6_select; |
Martin KaFai Lau | 367efcb | 2014-10-20 13:42:45 -0700 | [diff] [blame] | 2074 | } |
Martin KaFai Lau | a3c00e4 | 2014-10-20 13:42:43 -0700 | [diff] [blame] | 2075 | } |
| 2076 | |
David Ahern | effda4d | 2019-04-16 14:36:10 -0700 | [diff] [blame] | 2077 | trace_fib6_table_lookup(net, res, table, fl6); |
YOSHIFUJI Hideaki | fb9de91 | 2006-03-20 16:59:08 -0800 | [diff] [blame] | 2078 | |
David Ahern | effda4d | 2019-04-16 14:36:10 -0700 | [diff] [blame] | 2079 | return 0; |
David Ahern | 1d053da | 2018-05-09 20:34:21 -0700 | [diff] [blame] | 2080 | } |
| 2081 | |
| 2082 | struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table, |
| 2083 | int oif, struct flowi6 *fl6, |
| 2084 | const struct sk_buff *skb, int flags) |
| 2085 | { |
David Ahern | b1d4099 | 2019-04-16 14:35:59 -0700 | [diff] [blame] | 2086 | struct fib6_result res = {}; |
David Ahern | 1d053da | 2018-05-09 20:34:21 -0700 | [diff] [blame] | 2087 | struct rt6_info *rt; |
| 2088 | int strict = 0; |
| 2089 | |
| 2090 | strict |= flags & RT6_LOOKUP_F_IFACE; |
| 2091 | strict |= flags & RT6_LOOKUP_F_IGNORE_LINKSTATE; |
| 2092 | if (net->ipv6.devconf_all->forwarding == 0) |
| 2093 | strict |= RT6_LOOKUP_F_REACHABLE; |
| 2094 | |
| 2095 | rcu_read_lock(); |
| 2096 | |
David Ahern | effda4d | 2019-04-16 14:36:10 -0700 | [diff] [blame] | 2097 | fib6_table_lookup(net, table, oif, fl6, &res, strict); |
David Ahern | b1d4099 | 2019-04-16 14:35:59 -0700 | [diff] [blame] | 2098 | if (res.f6i == net->ipv6.fib6_null_entry) { |
David Ahern | 421842e | 2018-04-17 17:33:18 -0700 | [diff] [blame] | 2099 | rt = net->ipv6.ip6_null_entry; |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 2100 | rcu_read_unlock(); |
Wei Wang | d3843fe | 2017-10-06 12:06:06 -0700 | [diff] [blame] | 2101 | dst_hold(&rt->dst); |
Wei Wang | d3843fe | 2017-10-06 12:06:06 -0700 | [diff] [blame] | 2102 | return rt; |
David Ahern | 23fb93a | 2018-04-17 17:33:23 -0700 | [diff] [blame] | 2103 | } |
| 2104 | |
David Ahern | b1d4099 | 2019-04-16 14:35:59 -0700 | [diff] [blame] | 2105 | fib6_select_path(net, &res, fl6, oif, false, skb, strict); |
David Ahern | d83009d | 2019-04-09 14:41:17 -0700 | [diff] [blame] | 2106 | |
David Ahern | 23fb93a | 2018-04-17 17:33:23 -0700 | [diff] [blame] | 2107 | /*Search through exception table */ |
David Ahern | 7e4b512 | 2019-04-16 14:36:00 -0700 | [diff] [blame] | 2108 | rt = rt6_find_cached_rt(&res, &fl6->daddr, &fl6->saddr); |
David Ahern | 23fb93a | 2018-04-17 17:33:23 -0700 | [diff] [blame] | 2109 | if (rt) { |
David Ahern | 10585b4 | 2019-03-20 09:24:50 -0700 | [diff] [blame] | 2110 | if (ip6_hold_safe(net, &rt)) |
Wei Wang | d3843fe | 2017-10-06 12:06:06 -0700 | [diff] [blame] | 2111 | dst_use_noref(&rt->dst, jiffies); |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 2112 | |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 2113 | rcu_read_unlock(); |
Martin KaFai Lau | d52d399 | 2015-05-22 20:56:06 -0700 | [diff] [blame] | 2114 | return rt; |
Martin KaFai Lau | 3da59bd | 2015-05-22 20:56:03 -0700 | [diff] [blame] | 2115 | } else if (unlikely((fl6->flowi6_flags & FLOWI_FLAG_KNOWN_NH) && |
David Ahern | b1d4099 | 2019-04-16 14:35:59 -0700 | [diff] [blame] | 2116 | !res.nh->fib_nh_gw_family)) { |
Martin KaFai Lau | 3da59bd | 2015-05-22 20:56:03 -0700 | [diff] [blame] | 2117 | /* Create a RTF_CACHE clone which will not be |
| 2118 | * owned by the fib6 tree. It is for the special case where |
| 2119 | * the daddr in the skb during the neighbor look-up is different |
| 2120 | * from the fl6->daddr used to look-up route here. |
| 2121 | */ |
Martin KaFai Lau | 3da59bd | 2015-05-22 20:56:03 -0700 | [diff] [blame] | 2122 | struct rt6_info *uncached_rt; |
| 2123 | |
David Ahern | 85bd05d | 2019-04-16 14:36:01 -0700 | [diff] [blame] | 2124 | uncached_rt = ip6_rt_cache_alloc(&res, &fl6->daddr, NULL); |
Martin KaFai Lau | d52d399 | 2015-05-22 20:56:06 -0700 | [diff] [blame] | 2125 | |
David Ahern | 4d85cd0 | 2018-04-20 15:37:59 -0700 | [diff] [blame] | 2126 | rcu_read_unlock(); |
Martin KaFai Lau | 3da59bd | 2015-05-22 20:56:03 -0700 | [diff] [blame] | 2127 | |
Wei Wang | 1cfb71e | 2017-06-17 10:42:33 -0700 | [diff] [blame] | 2128 | if (uncached_rt) { |
| 2129 | /* Uncached_rt's refcnt is taken during ip6_rt_cache_alloc() |
| 2130 | * No need for another dst_hold() |
| 2131 | */ |
Martin KaFai Lau | 8d0b94a | 2015-05-22 20:56:04 -0700 | [diff] [blame] | 2132 | rt6_uncached_list_add(uncached_rt); |
Wei Wang | 81eb844 | 2017-10-06 12:06:11 -0700 | [diff] [blame] | 2133 | atomic_inc(&net->ipv6.rt6_stats->fib_rt_uncache); |
Wei Wang | 1cfb71e | 2017-06-17 10:42:33 -0700 | [diff] [blame] | 2134 | } else { |
Martin KaFai Lau | 3da59bd | 2015-05-22 20:56:03 -0700 | [diff] [blame] | 2135 | uncached_rt = net->ipv6.ip6_null_entry; |
Wei Wang | 1cfb71e | 2017-06-17 10:42:33 -0700 | [diff] [blame] | 2136 | dst_hold(&uncached_rt->dst); |
| 2137 | } |
David Ahern | b811580 | 2015-11-19 12:24:22 -0800 | [diff] [blame] | 2138 | |
Martin KaFai Lau | 3da59bd | 2015-05-22 20:56:03 -0700 | [diff] [blame] | 2139 | return uncached_rt; |
Martin KaFai Lau | d52d399 | 2015-05-22 20:56:06 -0700 | [diff] [blame] | 2140 | } else { |
| 2141 | /* Get a percpu copy */ |
| 2142 | |
| 2143 | struct rt6_info *pcpu_rt; |
| 2144 | |
Eric Dumazet | 951f788 | 2017-10-08 21:07:18 -0700 | [diff] [blame] | 2145 | local_bh_disable(); |
David Ahern | db3fede | 2019-04-16 14:36:03 -0700 | [diff] [blame] | 2146 | pcpu_rt = rt6_get_pcpu_route(&res); |
Martin KaFai Lau | d52d399 | 2015-05-22 20:56:06 -0700 | [diff] [blame] | 2147 | |
David Ahern | 93531c6 | 2018-04-17 17:33:25 -0700 | [diff] [blame] | 2148 | if (!pcpu_rt) |
David Ahern | db3fede | 2019-04-16 14:36:03 -0700 | [diff] [blame] | 2149 | pcpu_rt = rt6_make_pcpu_route(net, &res); |
David Ahern | 93531c6 | 2018-04-17 17:33:25 -0700 | [diff] [blame] | 2150 | |
Eric Dumazet | 951f788 | 2017-10-08 21:07:18 -0700 | [diff] [blame] | 2151 | local_bh_enable(); |
| 2152 | rcu_read_unlock(); |
David Ahern | d4bea42 | 2018-05-09 20:34:24 -0700 | [diff] [blame] | 2153 | |
Martin KaFai Lau | d52d399 | 2015-05-22 20:56:06 -0700 | [diff] [blame] | 2154 | return pcpu_rt; |
| 2155 | } |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 2156 | } |
David Ahern | 9ff7438 | 2016-06-13 13:44:19 -0700 | [diff] [blame] | 2157 | EXPORT_SYMBOL_GPL(ip6_pol_route); |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 2158 | |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 2159 | static struct rt6_info *ip6_pol_route_input(struct net *net, |
| 2160 | struct fib6_table *table, |
| 2161 | struct flowi6 *fl6, |
| 2162 | const struct sk_buff *skb, |
| 2163 | int flags) |
Pavel Emelyanov | 4acad72 | 2007-10-15 13:02:51 -0700 | [diff] [blame] | 2164 | { |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 2165 | return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, skb, flags); |
Pavel Emelyanov | 4acad72 | 2007-10-15 13:02:51 -0700 | [diff] [blame] | 2166 | } |
| 2167 | |
Mahesh Bandewar | d409b84 | 2016-09-16 12:59:08 -0700 | [diff] [blame] | 2168 | struct dst_entry *ip6_route_input_lookup(struct net *net, |
| 2169 | struct net_device *dev, |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 2170 | struct flowi6 *fl6, |
| 2171 | const struct sk_buff *skb, |
| 2172 | int flags) |
Shmulik Ladkani | 72331bc | 2012-04-01 04:03:45 +0000 | [diff] [blame] | 2173 | { |
| 2174 | if (rt6_need_strict(&fl6->daddr) && dev->type != ARPHRD_PIMREG) |
| 2175 | flags |= RT6_LOOKUP_F_IFACE; |
| 2176 | |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 2177 | return fib6_rule_lookup(net, fl6, skb, flags, ip6_pol_route_input); |
Shmulik Ladkani | 72331bc | 2012-04-01 04:03:45 +0000 | [diff] [blame] | 2178 | } |
Mahesh Bandewar | d409b84 | 2016-09-16 12:59:08 -0700 | [diff] [blame] | 2179 | EXPORT_SYMBOL_GPL(ip6_route_input_lookup); |
Shmulik Ladkani | 72331bc | 2012-04-01 04:03:45 +0000 | [diff] [blame] | 2180 | |
Jakub Sitnicki | 23aebda | 2017-08-23 09:58:29 +0200 | [diff] [blame] | 2181 | static void ip6_multipath_l3_keys(const struct sk_buff *skb, |
Roopa Prabhu | 5e5d6fe | 2018-02-28 22:43:22 -0500 | [diff] [blame] | 2182 | struct flow_keys *keys, |
| 2183 | struct flow_keys *flkeys) |
Jakub Sitnicki | 23aebda | 2017-08-23 09:58:29 +0200 | [diff] [blame] | 2184 | { |
| 2185 | const struct ipv6hdr *outer_iph = ipv6_hdr(skb); |
| 2186 | const struct ipv6hdr *key_iph = outer_iph; |
Roopa Prabhu | 5e5d6fe | 2018-02-28 22:43:22 -0500 | [diff] [blame] | 2187 | struct flow_keys *_flkeys = flkeys; |
Jakub Sitnicki | 23aebda | 2017-08-23 09:58:29 +0200 | [diff] [blame] | 2188 | const struct ipv6hdr *inner_iph; |
| 2189 | const struct icmp6hdr *icmph; |
| 2190 | struct ipv6hdr _inner_iph; |
Eric Dumazet | cea67a2 | 2018-04-29 09:54:59 -0700 | [diff] [blame] | 2191 | struct icmp6hdr _icmph; |
Jakub Sitnicki | 23aebda | 2017-08-23 09:58:29 +0200 | [diff] [blame] | 2192 | |
| 2193 | if (likely(outer_iph->nexthdr != IPPROTO_ICMPV6)) |
| 2194 | goto out; |
| 2195 | |
Eric Dumazet | cea67a2 | 2018-04-29 09:54:59 -0700 | [diff] [blame] | 2196 | icmph = skb_header_pointer(skb, skb_transport_offset(skb), |
| 2197 | sizeof(_icmph), &_icmph); |
| 2198 | if (!icmph) |
| 2199 | goto out; |
| 2200 | |
Jakub Sitnicki | 23aebda | 2017-08-23 09:58:29 +0200 | [diff] [blame] | 2201 | if (icmph->icmp6_type != ICMPV6_DEST_UNREACH && |
| 2202 | icmph->icmp6_type != ICMPV6_PKT_TOOBIG && |
| 2203 | icmph->icmp6_type != ICMPV6_TIME_EXCEED && |
| 2204 | icmph->icmp6_type != ICMPV6_PARAMPROB) |
| 2205 | goto out; |
| 2206 | |
| 2207 | inner_iph = skb_header_pointer(skb, |
| 2208 | skb_transport_offset(skb) + sizeof(*icmph), |
| 2209 | sizeof(_inner_iph), &_inner_iph); |
| 2210 | if (!inner_iph) |
| 2211 | goto out; |
| 2212 | |
| 2213 | key_iph = inner_iph; |
Roopa Prabhu | 5e5d6fe | 2018-02-28 22:43:22 -0500 | [diff] [blame] | 2214 | _flkeys = NULL; |
Jakub Sitnicki | 23aebda | 2017-08-23 09:58:29 +0200 | [diff] [blame] | 2215 | out: |
Roopa Prabhu | 5e5d6fe | 2018-02-28 22:43:22 -0500 | [diff] [blame] | 2216 | if (_flkeys) { |
| 2217 | keys->addrs.v6addrs.src = _flkeys->addrs.v6addrs.src; |
| 2218 | keys->addrs.v6addrs.dst = _flkeys->addrs.v6addrs.dst; |
| 2219 | keys->tags.flow_label = _flkeys->tags.flow_label; |
| 2220 | keys->basic.ip_proto = _flkeys->basic.ip_proto; |
| 2221 | } else { |
| 2222 | keys->addrs.v6addrs.src = key_iph->saddr; |
| 2223 | keys->addrs.v6addrs.dst = key_iph->daddr; |
Michal Kubecek | fa1be7e | 2018-06-04 11:36:05 +0200 | [diff] [blame] | 2224 | keys->tags.flow_label = ip6_flowlabel(key_iph); |
Roopa Prabhu | 5e5d6fe | 2018-02-28 22:43:22 -0500 | [diff] [blame] | 2225 | keys->basic.ip_proto = key_iph->nexthdr; |
| 2226 | } |
Jakub Sitnicki | 23aebda | 2017-08-23 09:58:29 +0200 | [diff] [blame] | 2227 | } |
| 2228 | |
| 2229 | /* 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] | 2230 | u32 rt6_multipath_hash(const struct net *net, const struct flowi6 *fl6, |
| 2231 | const struct sk_buff *skb, struct flow_keys *flkeys) |
Jakub Sitnicki | 23aebda | 2017-08-23 09:58:29 +0200 | [diff] [blame] | 2232 | { |
| 2233 | struct flow_keys hash_keys; |
David Ahern | 9a2a537 | 2018-03-02 08:32:15 -0800 | [diff] [blame] | 2234 | u32 mhash; |
Jakub Sitnicki | 23aebda | 2017-08-23 09:58:29 +0200 | [diff] [blame] | 2235 | |
David S. Miller | bbfa047 | 2018-03-12 11:09:33 -0400 | [diff] [blame] | 2236 | switch (ip6_multipath_hash_policy(net)) { |
David Ahern | b4bac17 | 2018-03-02 08:32:18 -0800 | [diff] [blame] | 2237 | case 0: |
| 2238 | memset(&hash_keys, 0, sizeof(hash_keys)); |
| 2239 | hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; |
| 2240 | if (skb) { |
| 2241 | ip6_multipath_l3_keys(skb, &hash_keys, flkeys); |
| 2242 | } else { |
| 2243 | hash_keys.addrs.v6addrs.src = fl6->saddr; |
| 2244 | hash_keys.addrs.v6addrs.dst = fl6->daddr; |
Michal Kubecek | fa1be7e | 2018-06-04 11:36:05 +0200 | [diff] [blame] | 2245 | hash_keys.tags.flow_label = (__force u32)flowi6_get_flowlabel(fl6); |
David Ahern | b4bac17 | 2018-03-02 08:32:18 -0800 | [diff] [blame] | 2246 | hash_keys.basic.ip_proto = fl6->flowi6_proto; |
| 2247 | } |
| 2248 | break; |
| 2249 | case 1: |
| 2250 | if (skb) { |
| 2251 | unsigned int flag = FLOW_DISSECTOR_F_STOP_AT_ENCAP; |
| 2252 | struct flow_keys keys; |
| 2253 | |
| 2254 | /* short-circuit if we already have L4 hash present */ |
| 2255 | if (skb->l4_hash) |
| 2256 | return skb_get_hash_raw(skb) >> 1; |
| 2257 | |
| 2258 | memset(&hash_keys, 0, sizeof(hash_keys)); |
| 2259 | |
| 2260 | if (!flkeys) { |
| 2261 | skb_flow_dissect_flow_keys(skb, &keys, flag); |
| 2262 | flkeys = &keys; |
| 2263 | } |
| 2264 | hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; |
| 2265 | hash_keys.addrs.v6addrs.src = flkeys->addrs.v6addrs.src; |
| 2266 | hash_keys.addrs.v6addrs.dst = flkeys->addrs.v6addrs.dst; |
| 2267 | hash_keys.ports.src = flkeys->ports.src; |
| 2268 | hash_keys.ports.dst = flkeys->ports.dst; |
| 2269 | hash_keys.basic.ip_proto = flkeys->basic.ip_proto; |
| 2270 | } else { |
| 2271 | memset(&hash_keys, 0, sizeof(hash_keys)); |
| 2272 | hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; |
| 2273 | hash_keys.addrs.v6addrs.src = fl6->saddr; |
| 2274 | hash_keys.addrs.v6addrs.dst = fl6->daddr; |
| 2275 | hash_keys.ports.src = fl6->fl6_sport; |
| 2276 | hash_keys.ports.dst = fl6->fl6_dport; |
| 2277 | hash_keys.basic.ip_proto = fl6->flowi6_proto; |
| 2278 | } |
| 2279 | break; |
Jakub Sitnicki | 23aebda | 2017-08-23 09:58:29 +0200 | [diff] [blame] | 2280 | } |
David Ahern | 9a2a537 | 2018-03-02 08:32:15 -0800 | [diff] [blame] | 2281 | mhash = flow_hash_from_keys(&hash_keys); |
Jakub Sitnicki | 23aebda | 2017-08-23 09:58:29 +0200 | [diff] [blame] | 2282 | |
David Ahern | 9a2a537 | 2018-03-02 08:32:15 -0800 | [diff] [blame] | 2283 | return mhash >> 1; |
Jakub Sitnicki | 23aebda | 2017-08-23 09:58:29 +0200 | [diff] [blame] | 2284 | } |
| 2285 | |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 2286 | void ip6_route_input(struct sk_buff *skb) |
| 2287 | { |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 2288 | const struct ipv6hdr *iph = ipv6_hdr(skb); |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 2289 | struct net *net = dev_net(skb->dev); |
Thomas Graf | adaa70b | 2006-10-13 15:01:03 -0700 | [diff] [blame] | 2290 | int flags = RT6_LOOKUP_F_HAS_SADDR; |
Jiri Benc | 904af04 | 2015-08-20 13:56:31 +0200 | [diff] [blame] | 2291 | struct ip_tunnel_info *tun_info; |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 2292 | struct flowi6 fl6 = { |
David Ahern | e0d56fd | 2016-09-10 12:09:57 -0700 | [diff] [blame] | 2293 | .flowi6_iif = skb->dev->ifindex, |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 2294 | .daddr = iph->daddr, |
| 2295 | .saddr = iph->saddr, |
YOSHIFUJI Hideaki / 吉藤英明 | 6502ca5 | 2013-01-13 05:01:51 +0000 | [diff] [blame] | 2296 | .flowlabel = ip6_flowinfo(iph), |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 2297 | .flowi6_mark = skb->mark, |
| 2298 | .flowi6_proto = iph->nexthdr, |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 2299 | }; |
Roopa Prabhu | 5e5d6fe | 2018-02-28 22:43:22 -0500 | [diff] [blame] | 2300 | struct flow_keys *flkeys = NULL, _flkeys; |
Thomas Graf | adaa70b | 2006-10-13 15:01:03 -0700 | [diff] [blame] | 2301 | |
Jiri Benc | 904af04 | 2015-08-20 13:56:31 +0200 | [diff] [blame] | 2302 | tun_info = skb_tunnel_info(skb); |
Jiri Benc | 46fa062 | 2015-08-28 20:48:19 +0200 | [diff] [blame] | 2303 | if (tun_info && !(tun_info->mode & IP_TUNNEL_INFO_TX)) |
Jiri Benc | 904af04 | 2015-08-20 13:56:31 +0200 | [diff] [blame] | 2304 | fl6.flowi6_tun_key.tun_id = tun_info->key.tun_id; |
Roopa Prabhu | 5e5d6fe | 2018-02-28 22:43:22 -0500 | [diff] [blame] | 2305 | |
| 2306 | if (fib6_rules_early_flow_dissect(net, skb, &fl6, &_flkeys)) |
| 2307 | flkeys = &_flkeys; |
| 2308 | |
Jakub Sitnicki | 23aebda | 2017-08-23 09:58:29 +0200 | [diff] [blame] | 2309 | if (unlikely(fl6.flowi6_proto == IPPROTO_ICMPV6)) |
David Ahern | b4bac17 | 2018-03-02 08:32:18 -0800 | [diff] [blame] | 2310 | fl6.mp_hash = rt6_multipath_hash(net, &fl6, skb, flkeys); |
Jiri Benc | 06e9d04 | 2015-08-20 13:56:26 +0200 | [diff] [blame] | 2311 | skb_dst_drop(skb); |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 2312 | skb_dst_set(skb, |
| 2313 | ip6_route_input_lookup(net, skb->dev, &fl6, skb, flags)); |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 2314 | } |
| 2315 | |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 2316 | static struct rt6_info *ip6_pol_route_output(struct net *net, |
| 2317 | struct fib6_table *table, |
| 2318 | struct flowi6 *fl6, |
| 2319 | const struct sk_buff *skb, |
| 2320 | int flags) |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 2321 | { |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 2322 | return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, skb, flags); |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 2323 | } |
| 2324 | |
Paolo Abeni | 6f21c96 | 2016-01-29 12:30:19 +0100 | [diff] [blame] | 2325 | struct dst_entry *ip6_route_output_flags(struct net *net, const struct sock *sk, |
| 2326 | struct flowi6 *fl6, int flags) |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 2327 | { |
David Ahern | d46a9d6 | 2015-10-21 08:42:22 -0700 | [diff] [blame] | 2328 | bool any_src; |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 2329 | |
Robert Shearman | 3ede0bb | 2018-09-19 13:56:53 +0100 | [diff] [blame] | 2330 | if (ipv6_addr_type(&fl6->daddr) & |
| 2331 | (IPV6_ADDR_MULTICAST | IPV6_ADDR_LINKLOCAL)) { |
David Ahern | 4c1feac | 2016-09-10 12:09:56 -0700 | [diff] [blame] | 2332 | struct dst_entry *dst; |
| 2333 | |
| 2334 | dst = l3mdev_link_scope_lookup(net, fl6); |
| 2335 | if (dst) |
| 2336 | return dst; |
| 2337 | } |
David Ahern | ca25449 | 2015-10-12 11:47:10 -0700 | [diff] [blame] | 2338 | |
Pavel Emelyanov | 1fb9489 | 2012-08-08 21:53:36 +0000 | [diff] [blame] | 2339 | fl6->flowi6_iif = LOOPBACK_IFINDEX; |
David McCullough | 4dc27d1c | 2012-06-25 15:42:26 +0000 | [diff] [blame] | 2340 | |
David Ahern | d46a9d6 | 2015-10-21 08:42:22 -0700 | [diff] [blame] | 2341 | any_src = ipv6_addr_any(&fl6->saddr); |
David Ahern | 741a11d | 2015-09-28 10:12:13 -0700 | [diff] [blame] | 2342 | if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr) || |
David Ahern | d46a9d6 | 2015-10-21 08:42:22 -0700 | [diff] [blame] | 2343 | (fl6->flowi6_oif && any_src)) |
YOSHIFUJI Hideaki | 77d16f4 | 2006-08-23 17:25:05 -0700 | [diff] [blame] | 2344 | flags |= RT6_LOOKUP_F_IFACE; |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 2345 | |
David Ahern | d46a9d6 | 2015-10-21 08:42:22 -0700 | [diff] [blame] | 2346 | if (!any_src) |
Thomas Graf | adaa70b | 2006-10-13 15:01:03 -0700 | [diff] [blame] | 2347 | flags |= RT6_LOOKUP_F_HAS_SADDR; |
YOSHIFUJI Hideaki / 吉藤英明 | 0c9a2ac | 2010-03-07 00:14:44 +0000 | [diff] [blame] | 2348 | else if (sk) |
| 2349 | flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs); |
Thomas Graf | adaa70b | 2006-10-13 15:01:03 -0700 | [diff] [blame] | 2350 | |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 2351 | return fib6_rule_lookup(net, fl6, NULL, flags, ip6_pol_route_output); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2352 | } |
Paolo Abeni | 6f21c96 | 2016-01-29 12:30:19 +0100 | [diff] [blame] | 2353 | EXPORT_SYMBOL_GPL(ip6_route_output_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2354 | |
David S. Miller | 2774c13 | 2011-03-01 14:59:04 -0800 | [diff] [blame] | 2355 | 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] | 2356 | { |
David S. Miller | 5c1e6aa | 2011-04-28 14:13:38 -0700 | [diff] [blame] | 2357 | struct rt6_info *rt, *ort = (struct rt6_info *) dst_orig; |
Wei Wang | 1dbe3252 | 2017-06-17 10:42:26 -0700 | [diff] [blame] | 2358 | struct net_device *loopback_dev = net->loopback_dev; |
David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 2359 | struct dst_entry *new = NULL; |
| 2360 | |
Wei Wang | 1dbe3252 | 2017-06-17 10:42:26 -0700 | [diff] [blame] | 2361 | rt = dst_alloc(&ip6_dst_blackhole_ops, loopback_dev, 1, |
Steffen Klassert | 62cf27e | 2017-10-09 08:39:43 +0200 | [diff] [blame] | 2362 | DST_OBSOLETE_DEAD, 0); |
David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 2363 | if (rt) { |
Martin KaFai Lau | 0a1f596 | 2015-10-15 16:39:58 -0700 | [diff] [blame] | 2364 | rt6_info_init(rt); |
Wei Wang | 81eb844 | 2017-10-06 12:06:11 -0700 | [diff] [blame] | 2365 | atomic_inc(&net->ipv6.rt6_stats->fib_rt_alloc); |
Martin KaFai Lau | 0a1f596 | 2015-10-15 16:39:58 -0700 | [diff] [blame] | 2366 | |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 2367 | new = &rt->dst; |
David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 2368 | new->__use = 1; |
Herbert Xu | 352e512 | 2007-11-13 21:34:06 -0800 | [diff] [blame] | 2369 | new->input = dst_discard; |
Eric W. Biederman | ede2059 | 2015-10-07 16:48:47 -0500 | [diff] [blame] | 2370 | new->output = dst_discard_out; |
David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 2371 | |
Martin KaFai Lau | 0a1f596 | 2015-10-15 16:39:58 -0700 | [diff] [blame] | 2372 | dst_copy_metrics(new, &ort->dst); |
David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 2373 | |
Wei Wang | 1dbe3252 | 2017-06-17 10:42:26 -0700 | [diff] [blame] | 2374 | rt->rt6i_idev = in6_dev_get(loopback_dev); |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 2375 | rt->rt6i_gateway = ort->rt6i_gateway; |
Martin KaFai Lau | 0a1f596 | 2015-10-15 16:39:58 -0700 | [diff] [blame] | 2376 | rt->rt6i_flags = ort->rt6i_flags & ~RTF_PCPU; |
David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 2377 | |
| 2378 | memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key)); |
| 2379 | #ifdef CONFIG_IPV6_SUBTREES |
| 2380 | memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key)); |
| 2381 | #endif |
David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 2382 | } |
| 2383 | |
David S. Miller | 69ead7a | 2011-03-01 14:45:33 -0800 | [diff] [blame] | 2384 | dst_release(dst_orig); |
| 2385 | return new ? new : ERR_PTR(-ENOMEM); |
David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 2386 | } |
David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 2387 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2388 | /* |
| 2389 | * Destination cache support functions |
| 2390 | */ |
| 2391 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 2392 | static bool fib6_check(struct fib6_info *f6i, u32 cookie) |
Martin KaFai Lau | 3da59bd | 2015-05-22 20:56:03 -0700 | [diff] [blame] | 2393 | { |
Steffen Klassert | 3614364 | 2017-08-25 09:05:42 +0200 | [diff] [blame] | 2394 | u32 rt_cookie = 0; |
Wei Wang | c5cff85 | 2017-08-21 09:47:10 -0700 | [diff] [blame] | 2395 | |
David Ahern | 8ae8697 | 2018-04-20 15:38:03 -0700 | [diff] [blame] | 2396 | if (!fib6_get_cookie_safe(f6i, &rt_cookie) || rt_cookie != cookie) |
David Ahern | 93531c6 | 2018-04-17 17:33:25 -0700 | [diff] [blame] | 2397 | return false; |
| 2398 | |
| 2399 | if (fib6_check_expired(f6i)) |
| 2400 | return false; |
| 2401 | |
| 2402 | return true; |
| 2403 | } |
| 2404 | |
David Ahern | a68886a | 2018-04-20 15:38:02 -0700 | [diff] [blame] | 2405 | static struct dst_entry *rt6_check(struct rt6_info *rt, |
| 2406 | struct fib6_info *from, |
| 2407 | u32 cookie) |
Martin KaFai Lau | 3da59bd | 2015-05-22 20:56:03 -0700 | [diff] [blame] | 2408 | { |
Wei Wang | c5cff85 | 2017-08-21 09:47:10 -0700 | [diff] [blame] | 2409 | u32 rt_cookie = 0; |
| 2410 | |
David Ahern | a68886a | 2018-04-20 15:38:02 -0700 | [diff] [blame] | 2411 | if ((from && !fib6_get_cookie_safe(from, &rt_cookie)) || |
David Ahern | 93531c6 | 2018-04-17 17:33:25 -0700 | [diff] [blame] | 2412 | rt_cookie != cookie) |
Martin KaFai Lau | 3da59bd | 2015-05-22 20:56:03 -0700 | [diff] [blame] | 2413 | return NULL; |
| 2414 | |
| 2415 | if (rt6_check_expired(rt)) |
| 2416 | return NULL; |
| 2417 | |
| 2418 | return &rt->dst; |
| 2419 | } |
| 2420 | |
David Ahern | a68886a | 2018-04-20 15:38:02 -0700 | [diff] [blame] | 2421 | static struct dst_entry *rt6_dst_from_check(struct rt6_info *rt, |
| 2422 | struct fib6_info *from, |
| 2423 | u32 cookie) |
Martin KaFai Lau | 3da59bd | 2015-05-22 20:56:03 -0700 | [diff] [blame] | 2424 | { |
Martin KaFai Lau | 5973fb1 | 2015-11-11 11:51:07 -0800 | [diff] [blame] | 2425 | if (!__rt6_check_expired(rt) && |
| 2426 | rt->dst.obsolete == DST_OBSOLETE_FORCE_CHK && |
David Ahern | a68886a | 2018-04-20 15:38:02 -0700 | [diff] [blame] | 2427 | fib6_check(from, cookie)) |
Martin KaFai Lau | 3da59bd | 2015-05-22 20:56:03 -0700 | [diff] [blame] | 2428 | return &rt->dst; |
| 2429 | else |
| 2430 | return NULL; |
| 2431 | } |
| 2432 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2433 | static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie) |
| 2434 | { |
David Ahern | a87b7dc | 2018-04-20 15:38:00 -0700 | [diff] [blame] | 2435 | struct dst_entry *dst_ret; |
David Ahern | a68886a | 2018-04-20 15:38:02 -0700 | [diff] [blame] | 2436 | struct fib6_info *from; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2437 | struct rt6_info *rt; |
| 2438 | |
David Ahern | a87b7dc | 2018-04-20 15:38:00 -0700 | [diff] [blame] | 2439 | rt = container_of(dst, struct rt6_info, dst); |
| 2440 | |
| 2441 | rcu_read_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2442 | |
Nicolas Dichtel | 6f3118b | 2012-09-10 22:09:46 +0000 | [diff] [blame] | 2443 | /* All IPV6 dsts are created with ->obsolete set to the value |
| 2444 | * DST_OBSOLETE_FORCE_CHK which forces validation calls down |
| 2445 | * into this function always. |
| 2446 | */ |
Hannes Frederic Sowa | e3bc10b | 2013-10-24 07:48:24 +0200 | [diff] [blame] | 2447 | |
David Ahern | a68886a | 2018-04-20 15:38:02 -0700 | [diff] [blame] | 2448 | from = rcu_dereference(rt->from); |
Martin KaFai Lau | 4b32b5a | 2015-04-28 13:03:06 -0700 | [diff] [blame] | 2449 | |
David Ahern | a68886a | 2018-04-20 15:38:02 -0700 | [diff] [blame] | 2450 | if (from && (rt->rt6i_flags & RTF_PCPU || |
| 2451 | unlikely(!list_empty(&rt->rt6i_uncached)))) |
| 2452 | dst_ret = rt6_dst_from_check(rt, from, cookie); |
Martin KaFai Lau | 3da59bd | 2015-05-22 20:56:03 -0700 | [diff] [blame] | 2453 | else |
David Ahern | a68886a | 2018-04-20 15:38:02 -0700 | [diff] [blame] | 2454 | dst_ret = rt6_check(rt, from, cookie); |
David Ahern | a87b7dc | 2018-04-20 15:38:00 -0700 | [diff] [blame] | 2455 | |
| 2456 | rcu_read_unlock(); |
| 2457 | |
| 2458 | return dst_ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2459 | } |
| 2460 | |
| 2461 | static struct dst_entry *ip6_negative_advice(struct dst_entry *dst) |
| 2462 | { |
| 2463 | struct rt6_info *rt = (struct rt6_info *) dst; |
| 2464 | |
| 2465 | if (rt) { |
YOSHIFUJI Hideaki / 吉藤英明 | 54c1a85 | 2010-03-28 07:15:45 +0000 | [diff] [blame] | 2466 | if (rt->rt6i_flags & RTF_CACHE) { |
David Ahern | c3c14da | 2018-04-23 11:32:06 -0700 | [diff] [blame] | 2467 | rcu_read_lock(); |
YOSHIFUJI Hideaki / 吉藤英明 | 54c1a85 | 2010-03-28 07:15:45 +0000 | [diff] [blame] | 2468 | if (rt6_check_expired(rt)) { |
David Ahern | 93531c6 | 2018-04-17 17:33:25 -0700 | [diff] [blame] | 2469 | rt6_remove_exception_rt(rt); |
YOSHIFUJI Hideaki / 吉藤英明 | 54c1a85 | 2010-03-28 07:15:45 +0000 | [diff] [blame] | 2470 | dst = NULL; |
| 2471 | } |
David Ahern | c3c14da | 2018-04-23 11:32:06 -0700 | [diff] [blame] | 2472 | rcu_read_unlock(); |
YOSHIFUJI Hideaki / 吉藤英明 | 54c1a85 | 2010-03-28 07:15:45 +0000 | [diff] [blame] | 2473 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2474 | dst_release(dst); |
YOSHIFUJI Hideaki / 吉藤英明 | 54c1a85 | 2010-03-28 07:15:45 +0000 | [diff] [blame] | 2475 | dst = NULL; |
| 2476 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2477 | } |
YOSHIFUJI Hideaki / 吉藤英明 | 54c1a85 | 2010-03-28 07:15:45 +0000 | [diff] [blame] | 2478 | return dst; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2479 | } |
| 2480 | |
| 2481 | static void ip6_link_failure(struct sk_buff *skb) |
| 2482 | { |
| 2483 | struct rt6_info *rt; |
| 2484 | |
Alexey Dobriyan | 3ffe533 | 2010-02-18 08:25:24 +0000 | [diff] [blame] | 2485 | icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2486 | |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 2487 | rt = (struct rt6_info *) skb_dst(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2488 | if (rt) { |
David Ahern | 8a14e46 | 2018-04-23 11:32:07 -0700 | [diff] [blame] | 2489 | rcu_read_lock(); |
Hannes Frederic Sowa | 1eb4f75 | 2013-07-10 23:00:57 +0200 | [diff] [blame] | 2490 | if (rt->rt6i_flags & RTF_CACHE) { |
Xin Long | 761f602 | 2018-11-14 00:48:28 +0800 | [diff] [blame] | 2491 | rt6_remove_exception_rt(rt); |
Wei Wang | c5cff85 | 2017-08-21 09:47:10 -0700 | [diff] [blame] | 2492 | } else { |
David Ahern | a68886a | 2018-04-20 15:38:02 -0700 | [diff] [blame] | 2493 | struct fib6_info *from; |
Wei Wang | c5cff85 | 2017-08-21 09:47:10 -0700 | [diff] [blame] | 2494 | struct fib6_node *fn; |
| 2495 | |
David Ahern | a68886a | 2018-04-20 15:38:02 -0700 | [diff] [blame] | 2496 | from = rcu_dereference(rt->from); |
| 2497 | if (from) { |
| 2498 | fn = rcu_dereference(from->fib6_node); |
| 2499 | if (fn && (rt->rt6i_flags & RTF_DEFAULT)) |
| 2500 | fn->fn_sernum = -1; |
| 2501 | } |
Hannes Frederic Sowa | 1eb4f75 | 2013-07-10 23:00:57 +0200 | [diff] [blame] | 2502 | } |
David Ahern | 8a14e46 | 2018-04-23 11:32:07 -0700 | [diff] [blame] | 2503 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2504 | } |
| 2505 | } |
| 2506 | |
David Ahern | 6a3e030 | 2018-04-20 15:37:57 -0700 | [diff] [blame] | 2507 | static void rt6_update_expires(struct rt6_info *rt0, int timeout) |
| 2508 | { |
David Ahern | a68886a | 2018-04-20 15:38:02 -0700 | [diff] [blame] | 2509 | if (!(rt0->rt6i_flags & RTF_EXPIRES)) { |
| 2510 | struct fib6_info *from; |
| 2511 | |
| 2512 | rcu_read_lock(); |
| 2513 | from = rcu_dereference(rt0->from); |
| 2514 | if (from) |
| 2515 | rt0->dst.expires = from->expires; |
| 2516 | rcu_read_unlock(); |
| 2517 | } |
David Ahern | 6a3e030 | 2018-04-20 15:37:57 -0700 | [diff] [blame] | 2518 | |
| 2519 | dst_set_expires(&rt0->dst, timeout); |
| 2520 | rt0->rt6i_flags |= RTF_EXPIRES; |
| 2521 | } |
| 2522 | |
Martin KaFai Lau | 45e4fd2 | 2015-05-22 20:56:00 -0700 | [diff] [blame] | 2523 | static void rt6_do_update_pmtu(struct rt6_info *rt, u32 mtu) |
| 2524 | { |
| 2525 | struct net *net = dev_net(rt->dst.dev); |
| 2526 | |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 2527 | dst_metric_set(&rt->dst, RTAX_MTU, mtu); |
Martin KaFai Lau | 45e4fd2 | 2015-05-22 20:56:00 -0700 | [diff] [blame] | 2528 | rt->rt6i_flags |= RTF_MODIFIED; |
Martin KaFai Lau | 45e4fd2 | 2015-05-22 20:56:00 -0700 | [diff] [blame] | 2529 | rt6_update_expires(rt, net->ipv6.sysctl.ip6_rt_mtu_expires); |
| 2530 | } |
| 2531 | |
Martin KaFai Lau | 0d3f6d2 | 2015-11-11 11:51:06 -0800 | [diff] [blame] | 2532 | static bool rt6_cache_allowed_for_pmtu(const struct rt6_info *rt) |
| 2533 | { |
| 2534 | return !(rt->rt6i_flags & RTF_CACHE) && |
Paolo Abeni | 1490ed2 | 2019-02-15 18:15:37 +0100 | [diff] [blame] | 2535 | (rt->rt6i_flags & RTF_PCPU || rcu_access_pointer(rt->from)); |
Martin KaFai Lau | 0d3f6d2 | 2015-11-11 11:51:06 -0800 | [diff] [blame] | 2536 | } |
| 2537 | |
Martin KaFai Lau | 45e4fd2 | 2015-05-22 20:56:00 -0700 | [diff] [blame] | 2538 | static void __ip6_rt_update_pmtu(struct dst_entry *dst, const struct sock *sk, |
| 2539 | const struct ipv6hdr *iph, u32 mtu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2540 | { |
Julian Anastasov | 0dec879 | 2017-02-06 23:14:16 +0200 | [diff] [blame] | 2541 | const struct in6_addr *daddr, *saddr; |
Ian Morris | 67ba415 | 2014-08-24 21:53:10 +0100 | [diff] [blame] | 2542 | struct rt6_info *rt6 = (struct rt6_info *)dst; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2543 | |
Xin Long | 19bda36 | 2016-10-28 18:18:01 +0800 | [diff] [blame] | 2544 | if (dst_metric_locked(dst, RTAX_MTU)) |
| 2545 | return; |
| 2546 | |
Julian Anastasov | 0dec879 | 2017-02-06 23:14:16 +0200 | [diff] [blame] | 2547 | if (iph) { |
| 2548 | daddr = &iph->daddr; |
| 2549 | saddr = &iph->saddr; |
| 2550 | } else if (sk) { |
| 2551 | daddr = &sk->sk_v6_daddr; |
| 2552 | saddr = &inet6_sk(sk)->saddr; |
| 2553 | } else { |
| 2554 | daddr = NULL; |
| 2555 | saddr = NULL; |
| 2556 | } |
| 2557 | dst_confirm_neigh(dst, daddr); |
Martin KaFai Lau | 45e4fd2 | 2015-05-22 20:56:00 -0700 | [diff] [blame] | 2558 | mtu = max_t(u32, mtu, IPV6_MIN_MTU); |
| 2559 | if (mtu >= dst_mtu(dst)) |
| 2560 | return; |
David S. Miller | 81aded2 | 2012-06-15 14:54:11 -0700 | [diff] [blame] | 2561 | |
Martin KaFai Lau | 0d3f6d2 | 2015-11-11 11:51:06 -0800 | [diff] [blame] | 2562 | if (!rt6_cache_allowed_for_pmtu(rt6)) { |
Martin KaFai Lau | 45e4fd2 | 2015-05-22 20:56:00 -0700 | [diff] [blame] | 2563 | rt6_do_update_pmtu(rt6, mtu); |
Wei Wang | 2b760fc | 2017-10-06 12:06:03 -0700 | [diff] [blame] | 2564 | /* update rt6_ex->stamp for cache */ |
| 2565 | if (rt6->rt6i_flags & RTF_CACHE) |
| 2566 | rt6_update_exception_stamp_rt(rt6); |
Julian Anastasov | 0dec879 | 2017-02-06 23:14:16 +0200 | [diff] [blame] | 2567 | } else if (daddr) { |
David Ahern | 85bd05d | 2019-04-16 14:36:01 -0700 | [diff] [blame] | 2568 | struct fib6_result res = {}; |
Martin KaFai Lau | 45e4fd2 | 2015-05-22 20:56:00 -0700 | [diff] [blame] | 2569 | struct rt6_info *nrt6; |
Hagen Paul Pfeifer | 9d28971 | 2015-01-15 22:34:25 +0100 | [diff] [blame] | 2570 | |
David Ahern | 4d85cd0 | 2018-04-20 15:37:59 -0700 | [diff] [blame] | 2571 | rcu_read_lock(); |
David Ahern | 85bd05d | 2019-04-16 14:36:01 -0700 | [diff] [blame] | 2572 | res.f6i = rcu_dereference(rt6->from); |
| 2573 | if (!res.f6i) { |
Jonathan Lemon | 9c69a13 | 2019-04-14 14:21:29 -0700 | [diff] [blame] | 2574 | rcu_read_unlock(); |
| 2575 | return; |
| 2576 | } |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 2577 | res.nh = res.f6i->fib6_nh; |
David Ahern | 7d21fec | 2019-04-16 14:36:11 -0700 | [diff] [blame] | 2578 | res.fib6_flags = res.f6i->fib6_flags; |
| 2579 | res.fib6_type = res.f6i->fib6_type; |
| 2580 | |
David Ahern | 85bd05d | 2019-04-16 14:36:01 -0700 | [diff] [blame] | 2581 | nrt6 = ip6_rt_cache_alloc(&res, daddr, saddr); |
Martin KaFai Lau | 45e4fd2 | 2015-05-22 20:56:00 -0700 | [diff] [blame] | 2582 | if (nrt6) { |
| 2583 | rt6_do_update_pmtu(nrt6, mtu); |
David Ahern | 5012f0a | 2019-04-16 14:36:05 -0700 | [diff] [blame] | 2584 | if (rt6_insert_exception(nrt6, &res)) |
Wei Wang | 2b760fc | 2017-10-06 12:06:03 -0700 | [diff] [blame] | 2585 | dst_release_immediate(&nrt6->dst); |
Martin KaFai Lau | 45e4fd2 | 2015-05-22 20:56:00 -0700 | [diff] [blame] | 2586 | } |
David Ahern | a68886a | 2018-04-20 15:38:02 -0700 | [diff] [blame] | 2587 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2588 | } |
| 2589 | } |
| 2590 | |
Martin KaFai Lau | 45e4fd2 | 2015-05-22 20:56:00 -0700 | [diff] [blame] | 2591 | static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk, |
| 2592 | struct sk_buff *skb, u32 mtu) |
| 2593 | { |
| 2594 | __ip6_rt_update_pmtu(dst, sk, skb ? ipv6_hdr(skb) : NULL, mtu); |
| 2595 | } |
| 2596 | |
David S. Miller | 42ae66c | 2012-06-15 20:01:57 -0700 | [diff] [blame] | 2597 | 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] | 2598 | int oif, u32 mark, kuid_t uid) |
David S. Miller | 81aded2 | 2012-06-15 14:54:11 -0700 | [diff] [blame] | 2599 | { |
| 2600 | const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data; |
| 2601 | struct dst_entry *dst; |
Maciej Żenczykowski | dc92095 | 2018-09-29 23:44:51 -0700 | [diff] [blame] | 2602 | struct flowi6 fl6 = { |
| 2603 | .flowi6_oif = oif, |
| 2604 | .flowi6_mark = mark ? mark : IP6_REPLY_MARK(net, skb->mark), |
| 2605 | .daddr = iph->daddr, |
| 2606 | .saddr = iph->saddr, |
| 2607 | .flowlabel = ip6_flowinfo(iph), |
| 2608 | .flowi6_uid = uid, |
| 2609 | }; |
David S. Miller | 81aded2 | 2012-06-15 14:54:11 -0700 | [diff] [blame] | 2610 | |
| 2611 | dst = ip6_route_output(net, NULL, &fl6); |
| 2612 | if (!dst->error) |
Martin KaFai Lau | 45e4fd2 | 2015-05-22 20:56:00 -0700 | [diff] [blame] | 2613 | __ip6_rt_update_pmtu(dst, NULL, iph, ntohl(mtu)); |
David S. Miller | 81aded2 | 2012-06-15 14:54:11 -0700 | [diff] [blame] | 2614 | dst_release(dst); |
| 2615 | } |
| 2616 | EXPORT_SYMBOL_GPL(ip6_update_pmtu); |
| 2617 | |
| 2618 | void ip6_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, __be32 mtu) |
| 2619 | { |
David Ahern | 7ddacfa | 2018-11-18 10:45:30 -0800 | [diff] [blame] | 2620 | int oif = sk->sk_bound_dev_if; |
Martin KaFai Lau | 33c162a | 2016-04-11 15:29:36 -0700 | [diff] [blame] | 2621 | struct dst_entry *dst; |
| 2622 | |
David Ahern | 7ddacfa | 2018-11-18 10:45:30 -0800 | [diff] [blame] | 2623 | if (!oif && skb->dev) |
| 2624 | oif = l3mdev_master_ifindex(skb->dev); |
| 2625 | |
| 2626 | 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] | 2627 | |
| 2628 | dst = __sk_dst_get(sk); |
| 2629 | if (!dst || !dst->obsolete || |
| 2630 | dst->ops->check(dst, inet6_sk(sk)->dst_cookie)) |
| 2631 | return; |
| 2632 | |
| 2633 | bh_lock_sock(sk); |
| 2634 | if (!sock_owned_by_user(sk) && !ipv6_addr_v4mapped(&sk->sk_v6_daddr)) |
| 2635 | ip6_datagram_dst_update(sk, false); |
| 2636 | bh_unlock_sock(sk); |
David S. Miller | 81aded2 | 2012-06-15 14:54:11 -0700 | [diff] [blame] | 2637 | } |
| 2638 | EXPORT_SYMBOL_GPL(ip6_sk_update_pmtu); |
| 2639 | |
Alexey Kodanev | 7d6850f | 2018-04-03 15:00:07 +0300 | [diff] [blame] | 2640 | void ip6_sk_dst_store_flow(struct sock *sk, struct dst_entry *dst, |
| 2641 | const struct flowi6 *fl6) |
| 2642 | { |
| 2643 | #ifdef CONFIG_IPV6_SUBTREES |
| 2644 | struct ipv6_pinfo *np = inet6_sk(sk); |
| 2645 | #endif |
| 2646 | |
| 2647 | ip6_dst_store(sk, dst, |
| 2648 | ipv6_addr_equal(&fl6->daddr, &sk->sk_v6_daddr) ? |
| 2649 | &sk->sk_v6_daddr : NULL, |
| 2650 | #ifdef CONFIG_IPV6_SUBTREES |
| 2651 | ipv6_addr_equal(&fl6->saddr, &np->saddr) ? |
| 2652 | &np->saddr : |
| 2653 | #endif |
| 2654 | NULL); |
| 2655 | } |
| 2656 | |
David Ahern | 9b6b35a | 2019-04-16 14:36:02 -0700 | [diff] [blame] | 2657 | static bool ip6_redirect_nh_match(const struct fib6_result *res, |
David Ahern | 0b34eb0 | 2019-04-09 14:41:19 -0700 | [diff] [blame] | 2658 | struct flowi6 *fl6, |
| 2659 | const struct in6_addr *gw, |
| 2660 | struct rt6_info **ret) |
| 2661 | { |
David Ahern | 9b6b35a | 2019-04-16 14:36:02 -0700 | [diff] [blame] | 2662 | const struct fib6_nh *nh = res->nh; |
| 2663 | |
David Ahern | 0b34eb0 | 2019-04-09 14:41:19 -0700 | [diff] [blame] | 2664 | if (nh->fib_nh_flags & RTNH_F_DEAD || !nh->fib_nh_gw_family || |
| 2665 | fl6->flowi6_oif != nh->fib_nh_dev->ifindex) |
| 2666 | return false; |
| 2667 | |
| 2668 | /* rt_cache's gateway might be different from its 'parent' |
| 2669 | * in the case of an ip redirect. |
| 2670 | * So we keep searching in the exception table if the gateway |
| 2671 | * is different. |
| 2672 | */ |
| 2673 | if (!ipv6_addr_equal(gw, &nh->fib_nh_gw6)) { |
| 2674 | struct rt6_info *rt_cache; |
| 2675 | |
David Ahern | 9b6b35a | 2019-04-16 14:36:02 -0700 | [diff] [blame] | 2676 | rt_cache = rt6_find_cached_rt(res, &fl6->daddr, &fl6->saddr); |
David Ahern | 0b34eb0 | 2019-04-09 14:41:19 -0700 | [diff] [blame] | 2677 | if (rt_cache && |
| 2678 | ipv6_addr_equal(gw, &rt_cache->rt6i_gateway)) { |
| 2679 | *ret = rt_cache; |
| 2680 | return true; |
| 2681 | } |
| 2682 | return false; |
| 2683 | } |
| 2684 | return true; |
| 2685 | } |
| 2686 | |
Duan Jiong | b55b76b | 2013-09-04 19:44:21 +0800 | [diff] [blame] | 2687 | /* Handle redirects */ |
| 2688 | struct ip6rd_flowi { |
| 2689 | struct flowi6 fl6; |
| 2690 | struct in6_addr gateway; |
| 2691 | }; |
| 2692 | |
| 2693 | static struct rt6_info *__ip6_route_redirect(struct net *net, |
| 2694 | struct fib6_table *table, |
| 2695 | struct flowi6 *fl6, |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 2696 | const struct sk_buff *skb, |
Duan Jiong | b55b76b | 2013-09-04 19:44:21 +0800 | [diff] [blame] | 2697 | int flags) |
| 2698 | { |
| 2699 | struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl6; |
David Ahern | 0b34eb0 | 2019-04-09 14:41:19 -0700 | [diff] [blame] | 2700 | struct rt6_info *ret = NULL; |
David Ahern | 9b6b35a | 2019-04-16 14:36:02 -0700 | [diff] [blame] | 2701 | struct fib6_result res = {}; |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 2702 | struct fib6_info *rt; |
Duan Jiong | b55b76b | 2013-09-04 19:44:21 +0800 | [diff] [blame] | 2703 | struct fib6_node *fn; |
| 2704 | |
David Ahern | 31680ac | 2019-05-22 15:12:18 -0700 | [diff] [blame] | 2705 | /* l3mdev_update_flow overrides oif if the device is enslaved; in |
| 2706 | * this case we must match on the real ingress device, so reset it |
| 2707 | */ |
| 2708 | if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF) |
| 2709 | fl6->flowi6_oif = skb->dev->ifindex; |
| 2710 | |
Duan Jiong | b55b76b | 2013-09-04 19:44:21 +0800 | [diff] [blame] | 2711 | /* Get the "current" route for this destination and |
Alexander Alemayhu | 67c408c | 2017-01-07 23:53:00 +0100 | [diff] [blame] | 2712 | * check if the redirect has come from appropriate router. |
Duan Jiong | b55b76b | 2013-09-04 19:44:21 +0800 | [diff] [blame] | 2713 | * |
| 2714 | * RFC 4861 specifies that redirects should only be |
| 2715 | * accepted if they come from the nexthop to the target. |
| 2716 | * Due to the way the routes are chosen, this notion |
| 2717 | * is a bit fuzzy and one might need to check all possible |
| 2718 | * routes. |
| 2719 | */ |
| 2720 | |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 2721 | rcu_read_lock(); |
David Ahern | 6454743 | 2018-05-09 20:34:19 -0700 | [diff] [blame] | 2722 | fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr); |
Duan Jiong | b55b76b | 2013-09-04 19:44:21 +0800 | [diff] [blame] | 2723 | restart: |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 2724 | for_each_fib6_node_rt_rcu(fn) { |
David Ahern | 9b6b35a | 2019-04-16 14:36:02 -0700 | [diff] [blame] | 2725 | res.f6i = rt; |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 2726 | res.nh = rt->fib6_nh; |
David Ahern | 9b6b35a | 2019-04-16 14:36:02 -0700 | [diff] [blame] | 2727 | |
David Ahern | 1489568 | 2018-04-17 17:33:17 -0700 | [diff] [blame] | 2728 | if (fib6_check_expired(rt)) |
Duan Jiong | b55b76b | 2013-09-04 19:44:21 +0800 | [diff] [blame] | 2729 | continue; |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 2730 | if (rt->fib6_flags & RTF_REJECT) |
Duan Jiong | b55b76b | 2013-09-04 19:44:21 +0800 | [diff] [blame] | 2731 | break; |
David Ahern | 9b6b35a | 2019-04-16 14:36:02 -0700 | [diff] [blame] | 2732 | if (ip6_redirect_nh_match(&res, fl6, &rdfl->gateway, &ret)) |
David Ahern | 0b34eb0 | 2019-04-09 14:41:19 -0700 | [diff] [blame] | 2733 | goto out; |
Duan Jiong | b55b76b | 2013-09-04 19:44:21 +0800 | [diff] [blame] | 2734 | } |
| 2735 | |
| 2736 | if (!rt) |
David Ahern | 421842e | 2018-04-17 17:33:18 -0700 | [diff] [blame] | 2737 | rt = net->ipv6.fib6_null_entry; |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 2738 | else if (rt->fib6_flags & RTF_REJECT) { |
David Ahern | 23fb93a | 2018-04-17 17:33:23 -0700 | [diff] [blame] | 2739 | ret = net->ipv6.ip6_null_entry; |
Martin KaFai Lau | b0a1ba5 | 2015-01-20 19:16:02 -0800 | [diff] [blame] | 2740 | goto out; |
| 2741 | } |
| 2742 | |
David Ahern | 421842e | 2018-04-17 17:33:18 -0700 | [diff] [blame] | 2743 | if (rt == net->ipv6.fib6_null_entry) { |
Martin KaFai Lau | a3c00e4 | 2014-10-20 13:42:43 -0700 | [diff] [blame] | 2744 | fn = fib6_backtrack(fn, &fl6->saddr); |
| 2745 | if (fn) |
| 2746 | goto restart; |
Duan Jiong | b55b76b | 2013-09-04 19:44:21 +0800 | [diff] [blame] | 2747 | } |
Martin KaFai Lau | a3c00e4 | 2014-10-20 13:42:43 -0700 | [diff] [blame] | 2748 | |
David Ahern | 9b6b35a | 2019-04-16 14:36:02 -0700 | [diff] [blame] | 2749 | res.f6i = rt; |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 2750 | res.nh = rt->fib6_nh; |
Martin KaFai Lau | b0a1ba5 | 2015-01-20 19:16:02 -0800 | [diff] [blame] | 2751 | out: |
David Ahern | 7d21fec | 2019-04-16 14:36:11 -0700 | [diff] [blame] | 2752 | if (ret) { |
David Ahern | 10585b4 | 2019-03-20 09:24:50 -0700 | [diff] [blame] | 2753 | ip6_hold_safe(net, &ret); |
David Ahern | 7d21fec | 2019-04-16 14:36:11 -0700 | [diff] [blame] | 2754 | } else { |
| 2755 | res.fib6_flags = res.f6i->fib6_flags; |
| 2756 | res.fib6_type = res.f6i->fib6_type; |
David Ahern | 9b6b35a | 2019-04-16 14:36:02 -0700 | [diff] [blame] | 2757 | ret = ip6_create_rt_rcu(&res); |
David Ahern | 7d21fec | 2019-04-16 14:36:11 -0700 | [diff] [blame] | 2758 | } |
Duan Jiong | b55b76b | 2013-09-04 19:44:21 +0800 | [diff] [blame] | 2759 | |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 2760 | rcu_read_unlock(); |
Duan Jiong | b55b76b | 2013-09-04 19:44:21 +0800 | [diff] [blame] | 2761 | |
David Ahern | 8ff2e5b | 2019-04-16 14:36:09 -0700 | [diff] [blame] | 2762 | trace_fib6_table_lookup(net, &res, table, fl6); |
David Ahern | 23fb93a | 2018-04-17 17:33:23 -0700 | [diff] [blame] | 2763 | return ret; |
Duan Jiong | b55b76b | 2013-09-04 19:44:21 +0800 | [diff] [blame] | 2764 | }; |
| 2765 | |
| 2766 | static struct dst_entry *ip6_route_redirect(struct net *net, |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 2767 | const struct flowi6 *fl6, |
| 2768 | const struct sk_buff *skb, |
| 2769 | const struct in6_addr *gateway) |
Duan Jiong | b55b76b | 2013-09-04 19:44:21 +0800 | [diff] [blame] | 2770 | { |
| 2771 | int flags = RT6_LOOKUP_F_HAS_SADDR; |
| 2772 | struct ip6rd_flowi rdfl; |
| 2773 | |
| 2774 | rdfl.fl6 = *fl6; |
| 2775 | rdfl.gateway = *gateway; |
| 2776 | |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 2777 | return fib6_rule_lookup(net, &rdfl.fl6, skb, |
Duan Jiong | b55b76b | 2013-09-04 19:44:21 +0800 | [diff] [blame] | 2778 | flags, __ip6_route_redirect); |
| 2779 | } |
| 2780 | |
Lorenzo Colitti | e2d118a | 2016-11-04 02:23:43 +0900 | [diff] [blame] | 2781 | void ip6_redirect(struct sk_buff *skb, struct net *net, int oif, u32 mark, |
| 2782 | kuid_t uid) |
David S. Miller | 3a5ad2e | 2012-07-12 00:08:07 -0700 | [diff] [blame] | 2783 | { |
| 2784 | const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data; |
| 2785 | struct dst_entry *dst; |
Maciej Żenczykowski | 1f7f10a | 2018-09-29 23:44:48 -0700 | [diff] [blame] | 2786 | struct flowi6 fl6 = { |
| 2787 | .flowi6_iif = LOOPBACK_IFINDEX, |
| 2788 | .flowi6_oif = oif, |
| 2789 | .flowi6_mark = mark, |
| 2790 | .daddr = iph->daddr, |
| 2791 | .saddr = iph->saddr, |
| 2792 | .flowlabel = ip6_flowinfo(iph), |
| 2793 | .flowi6_uid = uid, |
| 2794 | }; |
David S. Miller | 3a5ad2e | 2012-07-12 00:08:07 -0700 | [diff] [blame] | 2795 | |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 2796 | dst = ip6_route_redirect(net, &fl6, skb, &ipv6_hdr(skb)->saddr); |
Duan Jiong | b55b76b | 2013-09-04 19:44:21 +0800 | [diff] [blame] | 2797 | rt6_do_redirect(dst, NULL, skb); |
David S. Miller | 3a5ad2e | 2012-07-12 00:08:07 -0700 | [diff] [blame] | 2798 | dst_release(dst); |
| 2799 | } |
| 2800 | EXPORT_SYMBOL_GPL(ip6_redirect); |
| 2801 | |
Maciej Żenczykowski | d456336 | 2018-09-29 23:44:50 -0700 | [diff] [blame] | 2802 | 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] | 2803 | { |
| 2804 | const struct ipv6hdr *iph = ipv6_hdr(skb); |
| 2805 | const struct rd_msg *msg = (struct rd_msg *)icmp6_hdr(skb); |
| 2806 | struct dst_entry *dst; |
Maciej Żenczykowski | 0b26fb1 | 2018-09-29 23:44:49 -0700 | [diff] [blame] | 2807 | struct flowi6 fl6 = { |
| 2808 | .flowi6_iif = LOOPBACK_IFINDEX, |
| 2809 | .flowi6_oif = oif, |
Maciej Żenczykowski | 0b26fb1 | 2018-09-29 23:44:49 -0700 | [diff] [blame] | 2810 | .daddr = msg->dest, |
| 2811 | .saddr = iph->daddr, |
| 2812 | .flowi6_uid = sock_net_uid(net, NULL), |
| 2813 | }; |
Duan Jiong | c92a59e | 2013-08-22 12:07:35 +0800 | [diff] [blame] | 2814 | |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 2815 | dst = ip6_route_redirect(net, &fl6, skb, &iph->saddr); |
Duan Jiong | b55b76b | 2013-09-04 19:44:21 +0800 | [diff] [blame] | 2816 | rt6_do_redirect(dst, NULL, skb); |
Duan Jiong | c92a59e | 2013-08-22 12:07:35 +0800 | [diff] [blame] | 2817 | dst_release(dst); |
| 2818 | } |
| 2819 | |
David S. Miller | 3a5ad2e | 2012-07-12 00:08:07 -0700 | [diff] [blame] | 2820 | void ip6_sk_redirect(struct sk_buff *skb, struct sock *sk) |
| 2821 | { |
Lorenzo Colitti | e2d118a | 2016-11-04 02:23:43 +0900 | [diff] [blame] | 2822 | ip6_redirect(skb, sock_net(sk), sk->sk_bound_dev_if, sk->sk_mark, |
| 2823 | sk->sk_uid); |
David S. Miller | 3a5ad2e | 2012-07-12 00:08:07 -0700 | [diff] [blame] | 2824 | } |
| 2825 | EXPORT_SYMBOL_GPL(ip6_sk_redirect); |
| 2826 | |
David S. Miller | 0dbaee3 | 2010-12-13 12:52:14 -0800 | [diff] [blame] | 2827 | static unsigned int ip6_default_advmss(const struct dst_entry *dst) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2828 | { |
David S. Miller | 0dbaee3 | 2010-12-13 12:52:14 -0800 | [diff] [blame] | 2829 | struct net_device *dev = dst->dev; |
| 2830 | unsigned int mtu = dst_mtu(dst); |
| 2831 | struct net *net = dev_net(dev); |
| 2832 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2833 | mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr); |
| 2834 | |
Daniel Lezcano | 5578689 | 2008-03-04 13:47:47 -0800 | [diff] [blame] | 2835 | if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss) |
| 2836 | mtu = net->ipv6.sysctl.ip6_rt_min_advmss; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2837 | |
| 2838 | /* |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 2839 | * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and |
| 2840 | * corresponding MSS is IPV6_MAXPLEN - tcp_header_size. |
| 2841 | * IPV6_MAXPLEN is also valid and means: "any MSS, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2842 | * rely only on pmtu discovery" |
| 2843 | */ |
| 2844 | if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr)) |
| 2845 | mtu = IPV6_MAXPLEN; |
| 2846 | return mtu; |
| 2847 | } |
| 2848 | |
Steffen Klassert | ebb762f | 2011-11-23 02:12:51 +0000 | [diff] [blame] | 2849 | static unsigned int ip6_mtu(const struct dst_entry *dst) |
David S. Miller | d33e455 | 2010-12-14 13:01:14 -0800 | [diff] [blame] | 2850 | { |
David S. Miller | d33e455 | 2010-12-14 13:01:14 -0800 | [diff] [blame] | 2851 | struct inet6_dev *idev; |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 2852 | unsigned int mtu; |
Steffen Klassert | 618f9bc | 2011-11-23 02:13:31 +0000 | [diff] [blame] | 2853 | |
Martin KaFai Lau | 4b32b5a | 2015-04-28 13:03:06 -0700 | [diff] [blame] | 2854 | mtu = dst_metric_raw(dst, RTAX_MTU); |
| 2855 | if (mtu) |
| 2856 | goto out; |
| 2857 | |
Steffen Klassert | 618f9bc | 2011-11-23 02:13:31 +0000 | [diff] [blame] | 2858 | mtu = IPV6_MIN_MTU; |
David S. Miller | d33e455 | 2010-12-14 13:01:14 -0800 | [diff] [blame] | 2859 | |
| 2860 | rcu_read_lock(); |
| 2861 | idev = __in6_dev_get(dst->dev); |
| 2862 | if (idev) |
| 2863 | mtu = idev->cnf.mtu6; |
| 2864 | rcu_read_unlock(); |
| 2865 | |
Eric Dumazet | 30f78d8 | 2014-04-10 21:23:36 -0700 | [diff] [blame] | 2866 | out: |
Roopa Prabhu | 14972cb | 2016-08-24 20:10:43 -0700 | [diff] [blame] | 2867 | mtu = min_t(unsigned int, mtu, IP6_MAX_MTU); |
| 2868 | |
| 2869 | return mtu - lwtunnel_headroom(dst->lwtstate, mtu); |
David S. Miller | d33e455 | 2010-12-14 13:01:14 -0800 | [diff] [blame] | 2870 | } |
| 2871 | |
David Ahern | 901731b | 2018-05-21 09:08:14 -0700 | [diff] [blame] | 2872 | /* MTU selection: |
| 2873 | * 1. mtu on route is locked - use it |
| 2874 | * 2. mtu from nexthop exception |
| 2875 | * 3. mtu from egress device |
| 2876 | * |
| 2877 | * based on ip6_dst_mtu_forward and exception logic of |
| 2878 | * rt6_find_cached_rt; called with rcu_read_lock |
| 2879 | */ |
David Ahern | b748f26 | 2019-04-16 14:36:06 -0700 | [diff] [blame] | 2880 | u32 ip6_mtu_from_fib6(const struct fib6_result *res, |
| 2881 | const struct in6_addr *daddr, |
| 2882 | const struct in6_addr *saddr) |
David Ahern | 901731b | 2018-05-21 09:08:14 -0700 | [diff] [blame] | 2883 | { |
David Ahern | b748f26 | 2019-04-16 14:36:06 -0700 | [diff] [blame] | 2884 | const struct fib6_nh *nh = res->nh; |
| 2885 | struct fib6_info *f6i = res->f6i; |
David Ahern | 901731b | 2018-05-21 09:08:14 -0700 | [diff] [blame] | 2886 | struct inet6_dev *idev; |
Wei Wang | 510e2ce | 2019-05-16 13:30:54 -0700 | [diff] [blame] | 2887 | struct rt6_info *rt; |
David Ahern | 901731b | 2018-05-21 09:08:14 -0700 | [diff] [blame] | 2888 | u32 mtu = 0; |
| 2889 | |
| 2890 | if (unlikely(fib6_metric_locked(f6i, RTAX_MTU))) { |
| 2891 | mtu = f6i->fib6_pmtu; |
| 2892 | if (mtu) |
| 2893 | goto out; |
| 2894 | } |
| 2895 | |
Wei Wang | 510e2ce | 2019-05-16 13:30:54 -0700 | [diff] [blame] | 2896 | rt = rt6_find_cached_rt(res, daddr, saddr); |
| 2897 | if (unlikely(rt)) { |
| 2898 | mtu = dst_metric_raw(&rt->dst, RTAX_MTU); |
| 2899 | } else { |
David Ahern | b748f26 | 2019-04-16 14:36:06 -0700 | [diff] [blame] | 2900 | struct net_device *dev = nh->fib_nh_dev; |
David Ahern | 901731b | 2018-05-21 09:08:14 -0700 | [diff] [blame] | 2901 | |
| 2902 | mtu = IPV6_MIN_MTU; |
| 2903 | idev = __in6_dev_get(dev); |
| 2904 | if (idev && idev->cnf.mtu6 > mtu) |
| 2905 | mtu = idev->cnf.mtu6; |
| 2906 | } |
| 2907 | |
| 2908 | mtu = min_t(unsigned int, mtu, IP6_MAX_MTU); |
| 2909 | out: |
David Ahern | b748f26 | 2019-04-16 14:36:06 -0700 | [diff] [blame] | 2910 | return mtu - lwtunnel_headroom(nh->fib_nh_lws, mtu); |
David Ahern | 901731b | 2018-05-21 09:08:14 -0700 | [diff] [blame] | 2911 | } |
| 2912 | |
YOSHIFUJI Hideaki | 3b00944 | 2007-12-06 16:11:48 -0800 | [diff] [blame] | 2913 | struct dst_entry *icmp6_dst_alloc(struct net_device *dev, |
David S. Miller | 87a1157 | 2011-12-06 17:04:13 -0500 | [diff] [blame] | 2914 | struct flowi6 *fl6) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2915 | { |
David S. Miller | 87a1157 | 2011-12-06 17:04:13 -0500 | [diff] [blame] | 2916 | struct dst_entry *dst; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2917 | struct rt6_info *rt; |
| 2918 | struct inet6_dev *idev = in6_dev_get(dev); |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 2919 | struct net *net = dev_net(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2920 | |
David S. Miller | 3830847 | 2011-12-03 18:02:47 -0500 | [diff] [blame] | 2921 | if (unlikely(!idev)) |
Eric Dumazet | 122bdf6 | 2012-03-14 21:13:11 +0000 | [diff] [blame] | 2922 | return ERR_PTR(-ENODEV); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2923 | |
Martin KaFai Lau | ad70686 | 2015-08-14 11:05:52 -0700 | [diff] [blame] | 2924 | rt = ip6_dst_alloc(net, dev, 0); |
David S. Miller | 3830847 | 2011-12-03 18:02:47 -0500 | [diff] [blame] | 2925 | if (unlikely(!rt)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2926 | in6_dev_put(idev); |
David S. Miller | 87a1157 | 2011-12-06 17:04:13 -0500 | [diff] [blame] | 2927 | dst = ERR_PTR(-ENOMEM); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2928 | goto out; |
| 2929 | } |
| 2930 | |
Yan, Zheng | 8e2ec63 | 2011-09-05 21:34:30 +0000 | [diff] [blame] | 2931 | rt->dst.flags |= DST_HOST; |
Brendan McGrath | 588753f | 2017-12-13 22:14:57 +1100 | [diff] [blame] | 2932 | rt->dst.input = ip6_input; |
Yan, Zheng | 8e2ec63 | 2011-09-05 21:34:30 +0000 | [diff] [blame] | 2933 | rt->dst.output = ip6_output; |
Julian Anastasov | 550bab4 | 2013-10-20 15:43:04 +0300 | [diff] [blame] | 2934 | rt->rt6i_gateway = fl6->daddr; |
David S. Miller | 87a1157 | 2011-12-06 17:04:13 -0500 | [diff] [blame] | 2935 | rt->rt6i_dst.addr = fl6->daddr; |
Yan, Zheng | 8e2ec63 | 2011-09-05 21:34:30 +0000 | [diff] [blame] | 2936 | rt->rt6i_dst.plen = 128; |
| 2937 | rt->rt6i_idev = idev; |
Li RongQing | 14edd87 | 2012-10-24 14:01:18 +0800 | [diff] [blame] | 2938 | dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2939 | |
Ido Schimmel | 4c981e2 | 2018-01-07 12:45:04 +0200 | [diff] [blame] | 2940 | /* Add this dst into uncached_list so that rt6_disable_ip() can |
Wei Wang | 587fea7 | 2017-06-17 10:42:36 -0700 | [diff] [blame] | 2941 | * do proper release of the net_device |
| 2942 | */ |
| 2943 | rt6_uncached_list_add(rt); |
Wei Wang | 81eb844 | 2017-10-06 12:06:11 -0700 | [diff] [blame] | 2944 | atomic_inc(&net->ipv6.rt6_stats->fib_rt_uncache); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2945 | |
David S. Miller | 87a1157 | 2011-12-06 17:04:13 -0500 | [diff] [blame] | 2946 | dst = xfrm_lookup(net, &rt->dst, flowi6_to_flowi(fl6), NULL, 0); |
| 2947 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2948 | out: |
David S. Miller | 87a1157 | 2011-12-06 17:04:13 -0500 | [diff] [blame] | 2949 | return dst; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2950 | } |
| 2951 | |
Daniel Lezcano | 569d364 | 2008-01-18 03:56:57 -0800 | [diff] [blame] | 2952 | static int ip6_dst_gc(struct dst_ops *ops) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2953 | { |
Alexey Dobriyan | 86393e5 | 2009-08-29 01:34:49 +0000 | [diff] [blame] | 2954 | struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops); |
Daniel Lezcano | 7019b78 | 2008-03-04 13:50:14 -0800 | [diff] [blame] | 2955 | int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval; |
| 2956 | int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size; |
| 2957 | int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity; |
| 2958 | int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout; |
| 2959 | unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc; |
Eric Dumazet | fc66f95 | 2010-10-08 06:37:34 +0000 | [diff] [blame] | 2960 | int entries; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2961 | |
Eric Dumazet | fc66f95 | 2010-10-08 06:37:34 +0000 | [diff] [blame] | 2962 | entries = dst_entries_get_fast(ops); |
Michal Kubeček | 49a18d8 | 2013-08-01 10:04:24 +0200 | [diff] [blame] | 2963 | if (time_after(rt_last_gc + rt_min_interval, jiffies) && |
Eric Dumazet | fc66f95 | 2010-10-08 06:37:34 +0000 | [diff] [blame] | 2964 | entries <= rt_max_size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2965 | goto out; |
| 2966 | |
Benjamin Thery | 6891a34 | 2008-03-04 13:49:47 -0800 | [diff] [blame] | 2967 | net->ipv6.ip6_rt_gc_expire++; |
Li RongQing | 1495664 | 2014-05-19 17:30:28 +0800 | [diff] [blame] | 2968 | fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net, true); |
Eric Dumazet | fc66f95 | 2010-10-08 06:37:34 +0000 | [diff] [blame] | 2969 | entries = dst_entries_get_slow(ops); |
| 2970 | if (entries < ops->gc_thresh) |
Daniel Lezcano | 7019b78 | 2008-03-04 13:50:14 -0800 | [diff] [blame] | 2971 | net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2972 | out: |
Daniel Lezcano | 7019b78 | 2008-03-04 13:50:14 -0800 | [diff] [blame] | 2973 | 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] | 2974 | return entries > rt_max_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2975 | } |
| 2976 | |
David Ahern | 8c14586 | 2016-04-24 21:26:04 -0700 | [diff] [blame] | 2977 | static struct rt6_info *ip6_nh_lookup_table(struct net *net, |
| 2978 | struct fib6_config *cfg, |
David Ahern | f4797b3 | 2018-01-25 16:55:08 -0800 | [diff] [blame] | 2979 | const struct in6_addr *gw_addr, |
| 2980 | u32 tbid, int flags) |
David Ahern | 8c14586 | 2016-04-24 21:26:04 -0700 | [diff] [blame] | 2981 | { |
| 2982 | struct flowi6 fl6 = { |
| 2983 | .flowi6_oif = cfg->fc_ifindex, |
| 2984 | .daddr = *gw_addr, |
| 2985 | .saddr = cfg->fc_prefsrc, |
| 2986 | }; |
| 2987 | struct fib6_table *table; |
| 2988 | struct rt6_info *rt; |
David Ahern | 8c14586 | 2016-04-24 21:26:04 -0700 | [diff] [blame] | 2989 | |
David Ahern | f4797b3 | 2018-01-25 16:55:08 -0800 | [diff] [blame] | 2990 | table = fib6_get_table(net, tbid); |
David Ahern | 8c14586 | 2016-04-24 21:26:04 -0700 | [diff] [blame] | 2991 | if (!table) |
| 2992 | return NULL; |
| 2993 | |
| 2994 | if (!ipv6_addr_any(&cfg->fc_prefsrc)) |
| 2995 | flags |= RT6_LOOKUP_F_HAS_SADDR; |
| 2996 | |
David Ahern | f4797b3 | 2018-01-25 16:55:08 -0800 | [diff] [blame] | 2997 | flags |= RT6_LOOKUP_F_IGNORE_LINKSTATE; |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 2998 | rt = ip6_pol_route(net, table, cfg->fc_ifindex, &fl6, NULL, flags); |
David Ahern | 8c14586 | 2016-04-24 21:26:04 -0700 | [diff] [blame] | 2999 | |
| 3000 | /* if table lookup failed, fall back to full lookup */ |
| 3001 | if (rt == net->ipv6.ip6_null_entry) { |
| 3002 | ip6_rt_put(rt); |
| 3003 | rt = NULL; |
| 3004 | } |
| 3005 | |
| 3006 | return rt; |
| 3007 | } |
| 3008 | |
David Ahern | fc1e64e | 2018-01-25 16:55:09 -0800 | [diff] [blame] | 3009 | static int ip6_route_check_nh_onlink(struct net *net, |
| 3010 | struct fib6_config *cfg, |
David Ahern | 9fbb704 | 2018-03-13 08:29:36 -0700 | [diff] [blame] | 3011 | const struct net_device *dev, |
David Ahern | fc1e64e | 2018-01-25 16:55:09 -0800 | [diff] [blame] | 3012 | struct netlink_ext_ack *extack) |
| 3013 | { |
David Ahern | 44750f8 | 2018-02-06 13:17:06 -0800 | [diff] [blame] | 3014 | u32 tbid = l3mdev_fib_table(dev) ? : RT_TABLE_MAIN; |
David Ahern | fc1e64e | 2018-01-25 16:55:09 -0800 | [diff] [blame] | 3015 | const struct in6_addr *gw_addr = &cfg->fc_gateway; |
| 3016 | u32 flags = RTF_LOCAL | RTF_ANYCAST | RTF_REJECT; |
Paolo Abeni | bf1dc8b | 2019-02-21 11:19:42 +0100 | [diff] [blame] | 3017 | struct fib6_info *from; |
David Ahern | fc1e64e | 2018-01-25 16:55:09 -0800 | [diff] [blame] | 3018 | struct rt6_info *grt; |
| 3019 | int err; |
| 3020 | |
| 3021 | err = 0; |
| 3022 | grt = ip6_nh_lookup_table(net, cfg, gw_addr, tbid, 0); |
| 3023 | if (grt) { |
Paolo Abeni | bf1dc8b | 2019-02-21 11:19:42 +0100 | [diff] [blame] | 3024 | rcu_read_lock(); |
| 3025 | from = rcu_dereference(grt->from); |
David Ahern | 58e354c | 2018-02-06 12:14:12 -0800 | [diff] [blame] | 3026 | if (!grt->dst.error && |
David Ahern | 4ed591c | 2018-10-24 13:58:39 -0700 | [diff] [blame] | 3027 | /* ignore match if it is the default route */ |
Paolo Abeni | bf1dc8b | 2019-02-21 11:19:42 +0100 | [diff] [blame] | 3028 | from && !ipv6_addr_any(&from->fib6_dst.addr) && |
David Ahern | 58e354c | 2018-02-06 12:14:12 -0800 | [diff] [blame] | 3029 | (grt->rt6i_flags & flags || dev != grt->dst.dev)) { |
David Ahern | 44750f8 | 2018-02-06 13:17:06 -0800 | [diff] [blame] | 3030 | NL_SET_ERR_MSG(extack, |
| 3031 | "Nexthop has invalid gateway or device mismatch"); |
David Ahern | fc1e64e | 2018-01-25 16:55:09 -0800 | [diff] [blame] | 3032 | err = -EINVAL; |
| 3033 | } |
Paolo Abeni | bf1dc8b | 2019-02-21 11:19:42 +0100 | [diff] [blame] | 3034 | rcu_read_unlock(); |
David Ahern | fc1e64e | 2018-01-25 16:55:09 -0800 | [diff] [blame] | 3035 | |
| 3036 | ip6_rt_put(grt); |
| 3037 | } |
| 3038 | |
| 3039 | return err; |
| 3040 | } |
| 3041 | |
David Ahern | 1edce99 | 2018-01-25 16:55:07 -0800 | [diff] [blame] | 3042 | static int ip6_route_check_nh(struct net *net, |
| 3043 | struct fib6_config *cfg, |
| 3044 | struct net_device **_dev, |
| 3045 | struct inet6_dev **idev) |
| 3046 | { |
| 3047 | const struct in6_addr *gw_addr = &cfg->fc_gateway; |
| 3048 | struct net_device *dev = _dev ? *_dev : NULL; |
| 3049 | struct rt6_info *grt = NULL; |
| 3050 | int err = -EHOSTUNREACH; |
| 3051 | |
| 3052 | if (cfg->fc_table) { |
David Ahern | f4797b3 | 2018-01-25 16:55:08 -0800 | [diff] [blame] | 3053 | int flags = RT6_LOOKUP_F_IFACE; |
| 3054 | |
| 3055 | grt = ip6_nh_lookup_table(net, cfg, gw_addr, |
| 3056 | cfg->fc_table, flags); |
David Ahern | 1edce99 | 2018-01-25 16:55:07 -0800 | [diff] [blame] | 3057 | if (grt) { |
| 3058 | if (grt->rt6i_flags & RTF_GATEWAY || |
| 3059 | (dev && dev != grt->dst.dev)) { |
| 3060 | ip6_rt_put(grt); |
| 3061 | grt = NULL; |
| 3062 | } |
| 3063 | } |
| 3064 | } |
| 3065 | |
| 3066 | if (!grt) |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 3067 | grt = rt6_lookup(net, gw_addr, NULL, cfg->fc_ifindex, NULL, 1); |
David Ahern | 1edce99 | 2018-01-25 16:55:07 -0800 | [diff] [blame] | 3068 | |
| 3069 | if (!grt) |
| 3070 | goto out; |
| 3071 | |
| 3072 | if (dev) { |
| 3073 | if (dev != grt->dst.dev) { |
| 3074 | ip6_rt_put(grt); |
| 3075 | goto out; |
| 3076 | } |
| 3077 | } else { |
| 3078 | *_dev = dev = grt->dst.dev; |
| 3079 | *idev = grt->rt6i_idev; |
| 3080 | dev_hold(dev); |
| 3081 | in6_dev_hold(grt->rt6i_idev); |
| 3082 | } |
| 3083 | |
| 3084 | if (!(grt->rt6i_flags & RTF_GATEWAY)) |
| 3085 | err = 0; |
| 3086 | |
| 3087 | ip6_rt_put(grt); |
| 3088 | |
| 3089 | out: |
| 3090 | return err; |
| 3091 | } |
| 3092 | |
David Ahern | 9fbb704 | 2018-03-13 08:29:36 -0700 | [diff] [blame] | 3093 | static int ip6_validate_gw(struct net *net, struct fib6_config *cfg, |
| 3094 | struct net_device **_dev, struct inet6_dev **idev, |
| 3095 | struct netlink_ext_ack *extack) |
| 3096 | { |
| 3097 | const struct in6_addr *gw_addr = &cfg->fc_gateway; |
| 3098 | int gwa_type = ipv6_addr_type(gw_addr); |
David Ahern | 232378e | 2018-03-13 08:29:37 -0700 | [diff] [blame] | 3099 | bool skip_dev = gwa_type & IPV6_ADDR_LINKLOCAL ? false : true; |
David Ahern | 9fbb704 | 2018-03-13 08:29:36 -0700 | [diff] [blame] | 3100 | const struct net_device *dev = *_dev; |
David Ahern | 232378e | 2018-03-13 08:29:37 -0700 | [diff] [blame] | 3101 | bool need_addr_check = !dev; |
David Ahern | 9fbb704 | 2018-03-13 08:29:36 -0700 | [diff] [blame] | 3102 | int err = -EINVAL; |
| 3103 | |
| 3104 | /* if gw_addr is local we will fail to detect this in case |
| 3105 | * address is still TENTATIVE (DAD in progress). rt6_lookup() |
| 3106 | * will return already-added prefix route via interface that |
| 3107 | * prefix route was assigned to, which might be non-loopback. |
| 3108 | */ |
David Ahern | 232378e | 2018-03-13 08:29:37 -0700 | [diff] [blame] | 3109 | if (dev && |
| 3110 | ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) { |
| 3111 | NL_SET_ERR_MSG(extack, "Gateway can not be a local address"); |
David Ahern | 9fbb704 | 2018-03-13 08:29:36 -0700 | [diff] [blame] | 3112 | goto out; |
| 3113 | } |
| 3114 | |
| 3115 | if (gwa_type != (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_UNICAST)) { |
| 3116 | /* IPv6 strictly inhibits using not link-local |
| 3117 | * addresses as nexthop address. |
| 3118 | * Otherwise, router will not able to send redirects. |
| 3119 | * It is very good, but in some (rare!) circumstances |
| 3120 | * (SIT, PtP, NBMA NOARP links) it is handy to allow |
| 3121 | * some exceptions. --ANK |
| 3122 | * We allow IPv4-mapped nexthops to support RFC4798-type |
| 3123 | * addressing |
| 3124 | */ |
| 3125 | if (!(gwa_type & (IPV6_ADDR_UNICAST | IPV6_ADDR_MAPPED))) { |
| 3126 | NL_SET_ERR_MSG(extack, "Invalid gateway address"); |
| 3127 | goto out; |
| 3128 | } |
| 3129 | |
| 3130 | if (cfg->fc_flags & RTNH_F_ONLINK) |
| 3131 | err = ip6_route_check_nh_onlink(net, cfg, dev, extack); |
| 3132 | else |
| 3133 | err = ip6_route_check_nh(net, cfg, _dev, idev); |
| 3134 | |
| 3135 | if (err) |
| 3136 | goto out; |
| 3137 | } |
| 3138 | |
| 3139 | /* reload in case device was changed */ |
| 3140 | dev = *_dev; |
| 3141 | |
| 3142 | err = -EINVAL; |
| 3143 | if (!dev) { |
| 3144 | NL_SET_ERR_MSG(extack, "Egress device not specified"); |
| 3145 | goto out; |
| 3146 | } else if (dev->flags & IFF_LOOPBACK) { |
| 3147 | NL_SET_ERR_MSG(extack, |
| 3148 | "Egress device can not be loopback device for this route"); |
| 3149 | goto out; |
| 3150 | } |
David Ahern | 232378e | 2018-03-13 08:29:37 -0700 | [diff] [blame] | 3151 | |
| 3152 | /* if we did not check gw_addr above, do so now that the |
| 3153 | * egress device has been resolved. |
| 3154 | */ |
| 3155 | if (need_addr_check && |
| 3156 | ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) { |
| 3157 | NL_SET_ERR_MSG(extack, "Gateway can not be a local address"); |
| 3158 | goto out; |
| 3159 | } |
| 3160 | |
David Ahern | 9fbb704 | 2018-03-13 08:29:36 -0700 | [diff] [blame] | 3161 | err = 0; |
| 3162 | out: |
| 3163 | return err; |
| 3164 | } |
| 3165 | |
David Ahern | 83c44251 | 2019-03-27 20:53:50 -0700 | [diff] [blame] | 3166 | static bool fib6_is_reject(u32 flags, struct net_device *dev, int addr_type) |
| 3167 | { |
| 3168 | if ((flags & RTF_REJECT) || |
| 3169 | (dev && (dev->flags & IFF_LOOPBACK) && |
| 3170 | !(addr_type & IPV6_ADDR_LOOPBACK) && |
| 3171 | !(flags & RTF_LOCAL))) |
| 3172 | return true; |
| 3173 | |
| 3174 | return false; |
| 3175 | } |
| 3176 | |
| 3177 | int fib6_nh_init(struct net *net, struct fib6_nh *fib6_nh, |
| 3178 | struct fib6_config *cfg, gfp_t gfp_flags, |
| 3179 | struct netlink_ext_ack *extack) |
| 3180 | { |
| 3181 | struct net_device *dev = NULL; |
| 3182 | struct inet6_dev *idev = NULL; |
| 3183 | int addr_type; |
| 3184 | int err; |
| 3185 | |
David Ahern | f174173 | 2019-03-27 20:53:57 -0700 | [diff] [blame] | 3186 | fib6_nh->fib_nh_family = AF_INET6; |
| 3187 | |
David Ahern | 83c44251 | 2019-03-27 20:53:50 -0700 | [diff] [blame] | 3188 | err = -ENODEV; |
| 3189 | if (cfg->fc_ifindex) { |
| 3190 | dev = dev_get_by_index(net, cfg->fc_ifindex); |
| 3191 | if (!dev) |
| 3192 | goto out; |
| 3193 | idev = in6_dev_get(dev); |
| 3194 | if (!idev) |
| 3195 | goto out; |
| 3196 | } |
| 3197 | |
| 3198 | if (cfg->fc_flags & RTNH_F_ONLINK) { |
| 3199 | if (!dev) { |
| 3200 | NL_SET_ERR_MSG(extack, |
| 3201 | "Nexthop device required for onlink"); |
| 3202 | goto out; |
| 3203 | } |
| 3204 | |
| 3205 | if (!(dev->flags & IFF_UP)) { |
| 3206 | NL_SET_ERR_MSG(extack, "Nexthop device is not up"); |
| 3207 | err = -ENETDOWN; |
| 3208 | goto out; |
| 3209 | } |
| 3210 | |
David Ahern | ad1601a | 2019-03-27 20:53:56 -0700 | [diff] [blame] | 3211 | fib6_nh->fib_nh_flags |= RTNH_F_ONLINK; |
David Ahern | 83c44251 | 2019-03-27 20:53:50 -0700 | [diff] [blame] | 3212 | } |
| 3213 | |
David Ahern | ad1601a | 2019-03-27 20:53:56 -0700 | [diff] [blame] | 3214 | fib6_nh->fib_nh_weight = 1; |
David Ahern | 83c44251 | 2019-03-27 20:53:50 -0700 | [diff] [blame] | 3215 | |
| 3216 | /* We cannot add true routes via loopback here, |
| 3217 | * they would result in kernel looping; promote them to reject routes |
| 3218 | */ |
| 3219 | addr_type = ipv6_addr_type(&cfg->fc_dst); |
| 3220 | if (fib6_is_reject(cfg->fc_flags, dev, addr_type)) { |
| 3221 | /* hold loopback dev/idev if we haven't done so. */ |
| 3222 | if (dev != net->loopback_dev) { |
| 3223 | if (dev) { |
| 3224 | dev_put(dev); |
| 3225 | in6_dev_put(idev); |
| 3226 | } |
| 3227 | dev = net->loopback_dev; |
| 3228 | dev_hold(dev); |
| 3229 | idev = in6_dev_get(dev); |
| 3230 | if (!idev) { |
| 3231 | err = -ENODEV; |
| 3232 | goto out; |
| 3233 | } |
| 3234 | } |
David Ahern | 7dd7316 | 2019-06-03 18:37:03 -0700 | [diff] [blame] | 3235 | goto pcpu_alloc; |
David Ahern | 83c44251 | 2019-03-27 20:53:50 -0700 | [diff] [blame] | 3236 | } |
| 3237 | |
| 3238 | if (cfg->fc_flags & RTF_GATEWAY) { |
| 3239 | err = ip6_validate_gw(net, cfg, &dev, &idev, extack); |
| 3240 | if (err) |
| 3241 | goto out; |
| 3242 | |
David Ahern | ad1601a | 2019-03-27 20:53:56 -0700 | [diff] [blame] | 3243 | fib6_nh->fib_nh_gw6 = cfg->fc_gateway; |
David Ahern | bdf0046 | 2019-04-05 16:30:26 -0700 | [diff] [blame] | 3244 | fib6_nh->fib_nh_gw_family = AF_INET6; |
David Ahern | 83c44251 | 2019-03-27 20:53:50 -0700 | [diff] [blame] | 3245 | } |
| 3246 | |
| 3247 | err = -ENODEV; |
| 3248 | if (!dev) |
| 3249 | goto out; |
| 3250 | |
| 3251 | if (idev->cnf.disable_ipv6) { |
| 3252 | NL_SET_ERR_MSG(extack, "IPv6 is disabled on nexthop device"); |
| 3253 | err = -EACCES; |
| 3254 | goto out; |
| 3255 | } |
| 3256 | |
| 3257 | if (!(dev->flags & IFF_UP) && !cfg->fc_ignore_dev_down) { |
| 3258 | NL_SET_ERR_MSG(extack, "Nexthop device is not up"); |
| 3259 | err = -ENETDOWN; |
| 3260 | goto out; |
| 3261 | } |
| 3262 | |
| 3263 | if (!(cfg->fc_flags & (RTF_LOCAL | RTF_ANYCAST)) && |
| 3264 | !netif_carrier_ok(dev)) |
David Ahern | ad1601a | 2019-03-27 20:53:56 -0700 | [diff] [blame] | 3265 | fib6_nh->fib_nh_flags |= RTNH_F_LINKDOWN; |
David Ahern | 83c44251 | 2019-03-27 20:53:50 -0700 | [diff] [blame] | 3266 | |
David Ahern | 7dd7316 | 2019-06-03 18:37:03 -0700 | [diff] [blame] | 3267 | err = fib_nh_common_init(&fib6_nh->nh_common, cfg->fc_encap, |
| 3268 | cfg->fc_encap_type, cfg, gfp_flags, extack); |
| 3269 | if (err) |
| 3270 | goto out; |
| 3271 | |
| 3272 | pcpu_alloc: |
David Ahern | f40b6ae | 2019-05-22 20:27:55 -0700 | [diff] [blame] | 3273 | fib6_nh->rt6i_pcpu = alloc_percpu_gfp(struct rt6_info *, gfp_flags); |
| 3274 | if (!fib6_nh->rt6i_pcpu) { |
| 3275 | err = -ENOMEM; |
| 3276 | goto out; |
| 3277 | } |
| 3278 | |
David Ahern | ad1601a | 2019-03-27 20:53:56 -0700 | [diff] [blame] | 3279 | fib6_nh->fib_nh_dev = dev; |
David Ahern | f174173 | 2019-03-27 20:53:57 -0700 | [diff] [blame] | 3280 | fib6_nh->fib_nh_oif = dev->ifindex; |
David Ahern | 83c44251 | 2019-03-27 20:53:50 -0700 | [diff] [blame] | 3281 | err = 0; |
| 3282 | out: |
| 3283 | if (idev) |
| 3284 | in6_dev_put(idev); |
| 3285 | |
| 3286 | if (err) { |
David Ahern | ad1601a | 2019-03-27 20:53:56 -0700 | [diff] [blame] | 3287 | lwtstate_put(fib6_nh->fib_nh_lws); |
| 3288 | fib6_nh->fib_nh_lws = NULL; |
David Ahern | 83c44251 | 2019-03-27 20:53:50 -0700 | [diff] [blame] | 3289 | if (dev) |
| 3290 | dev_put(dev); |
| 3291 | } |
| 3292 | |
| 3293 | return err; |
| 3294 | } |
| 3295 | |
David Ahern | dac7d0f | 2019-03-27 20:53:51 -0700 | [diff] [blame] | 3296 | void fib6_nh_release(struct fib6_nh *fib6_nh) |
| 3297 | { |
David Ahern | cc5c073 | 2019-05-22 20:27:58 -0700 | [diff] [blame] | 3298 | struct rt6_exception_bucket *bucket; |
| 3299 | |
| 3300 | rcu_read_lock(); |
| 3301 | |
| 3302 | fib6_nh_flush_exceptions(fib6_nh, NULL); |
| 3303 | bucket = fib6_nh_get_excptn_bucket(fib6_nh, NULL); |
| 3304 | if (bucket) { |
| 3305 | rcu_assign_pointer(fib6_nh->rt6i_exception_bucket, NULL); |
| 3306 | kfree(bucket); |
| 3307 | } |
| 3308 | |
| 3309 | rcu_read_unlock(); |
| 3310 | |
David Ahern | f40b6ae | 2019-05-22 20:27:55 -0700 | [diff] [blame] | 3311 | if (fib6_nh->rt6i_pcpu) { |
| 3312 | int cpu; |
| 3313 | |
| 3314 | for_each_possible_cpu(cpu) { |
| 3315 | struct rt6_info **ppcpu_rt; |
| 3316 | struct rt6_info *pcpu_rt; |
| 3317 | |
| 3318 | ppcpu_rt = per_cpu_ptr(fib6_nh->rt6i_pcpu, cpu); |
| 3319 | pcpu_rt = *ppcpu_rt; |
| 3320 | if (pcpu_rt) { |
| 3321 | dst_dev_put(&pcpu_rt->dst); |
| 3322 | dst_release(&pcpu_rt->dst); |
| 3323 | *ppcpu_rt = NULL; |
| 3324 | } |
| 3325 | } |
| 3326 | |
| 3327 | free_percpu(fib6_nh->rt6i_pcpu); |
| 3328 | } |
| 3329 | |
David Ahern | 979e276 | 2019-03-27 20:53:58 -0700 | [diff] [blame] | 3330 | fib_nh_common_release(&fib6_nh->nh_common); |
David Ahern | dac7d0f | 2019-03-27 20:53:51 -0700 | [diff] [blame] | 3331 | } |
| 3332 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 3333 | static struct fib6_info *ip6_route_info_create(struct fib6_config *cfg, |
David Ahern | acb54e3 | 2018-04-17 17:33:22 -0700 | [diff] [blame] | 3334 | gfp_t gfp_flags, |
David Ahern | 333c430 | 2017-05-21 10:12:04 -0600 | [diff] [blame] | 3335 | struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3336 | { |
Daniel Lezcano | 5578689 | 2008-03-04 13:47:47 -0800 | [diff] [blame] | 3337 | struct net *net = cfg->fc_nlinfo.nl_net; |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 3338 | struct fib6_info *rt = NULL; |
David Ahern | f88d8ea | 2019-06-03 20:19:52 -0700 | [diff] [blame] | 3339 | struct nexthop *nh = NULL; |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 3340 | struct fib6_table *table; |
David Ahern | f88d8ea | 2019-06-03 20:19:52 -0700 | [diff] [blame] | 3341 | struct fib6_nh *fib6_nh; |
Roopa Prabhu | 8c5b83f | 2015-10-10 08:26:36 -0700 | [diff] [blame] | 3342 | int err = -EINVAL; |
David Ahern | 83c44251 | 2019-03-27 20:53:50 -0700 | [diff] [blame] | 3343 | int addr_type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3344 | |
David Ahern | 557c44b | 2017-04-19 14:19:43 -0700 | [diff] [blame] | 3345 | /* RTF_PCPU is an internal flag; can not be set by userspace */ |
David Ahern | d5d531c | 2017-05-21 10:12:05 -0600 | [diff] [blame] | 3346 | if (cfg->fc_flags & RTF_PCPU) { |
| 3347 | NL_SET_ERR_MSG(extack, "Userspace can not set RTF_PCPU"); |
David Ahern | 557c44b | 2017-04-19 14:19:43 -0700 | [diff] [blame] | 3348 | goto out; |
David Ahern | d5d531c | 2017-05-21 10:12:05 -0600 | [diff] [blame] | 3349 | } |
David Ahern | 557c44b | 2017-04-19 14:19:43 -0700 | [diff] [blame] | 3350 | |
Wei Wang | 2ea2352 | 2017-10-27 17:30:12 -0700 | [diff] [blame] | 3351 | /* RTF_CACHE is an internal flag; can not be set by userspace */ |
| 3352 | if (cfg->fc_flags & RTF_CACHE) { |
| 3353 | NL_SET_ERR_MSG(extack, "Userspace can not set RTF_CACHE"); |
| 3354 | goto out; |
| 3355 | } |
| 3356 | |
David Ahern | e8478e8 | 2018-04-17 17:33:13 -0700 | [diff] [blame] | 3357 | if (cfg->fc_type > RTN_MAX) { |
| 3358 | NL_SET_ERR_MSG(extack, "Invalid route type"); |
| 3359 | goto out; |
| 3360 | } |
| 3361 | |
David Ahern | d5d531c | 2017-05-21 10:12:05 -0600 | [diff] [blame] | 3362 | if (cfg->fc_dst_len > 128) { |
| 3363 | NL_SET_ERR_MSG(extack, "Invalid prefix length"); |
Roopa Prabhu | 8c5b83f | 2015-10-10 08:26:36 -0700 | [diff] [blame] | 3364 | goto out; |
David Ahern | d5d531c | 2017-05-21 10:12:05 -0600 | [diff] [blame] | 3365 | } |
| 3366 | if (cfg->fc_src_len > 128) { |
| 3367 | NL_SET_ERR_MSG(extack, "Invalid source address length"); |
| 3368 | goto out; |
| 3369 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3370 | #ifndef CONFIG_IPV6_SUBTREES |
David Ahern | d5d531c | 2017-05-21 10:12:05 -0600 | [diff] [blame] | 3371 | if (cfg->fc_src_len) { |
| 3372 | NL_SET_ERR_MSG(extack, |
| 3373 | "Specifying source address requires IPV6_SUBTREES to be enabled"); |
Roopa Prabhu | 8c5b83f | 2015-10-10 08:26:36 -0700 | [diff] [blame] | 3374 | goto out; |
David Ahern | d5d531c | 2017-05-21 10:12:05 -0600 | [diff] [blame] | 3375 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3376 | #endif |
David Ahern | fc1e64e | 2018-01-25 16:55:09 -0800 | [diff] [blame] | 3377 | |
Matti Vaittinen | d71314b | 2011-11-14 00:14:49 +0000 | [diff] [blame] | 3378 | err = -ENOBUFS; |
David S. Miller | 3830847 | 2011-12-03 18:02:47 -0500 | [diff] [blame] | 3379 | if (cfg->fc_nlinfo.nlh && |
| 3380 | !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) { |
Matti Vaittinen | d71314b | 2011-11-14 00:14:49 +0000 | [diff] [blame] | 3381 | table = fib6_get_table(net, cfg->fc_table); |
David S. Miller | 3830847 | 2011-12-03 18:02:47 -0500 | [diff] [blame] | 3382 | if (!table) { |
Joe Perches | f321383 | 2012-05-15 14:11:53 +0000 | [diff] [blame] | 3383 | 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] | 3384 | table = fib6_new_table(net, cfg->fc_table); |
| 3385 | } |
| 3386 | } else { |
| 3387 | table = fib6_new_table(net, cfg->fc_table); |
| 3388 | } |
David S. Miller | 3830847 | 2011-12-03 18:02:47 -0500 | [diff] [blame] | 3389 | |
| 3390 | if (!table) |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 3391 | goto out; |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 3392 | |
David Ahern | 93531c6 | 2018-04-17 17:33:25 -0700 | [diff] [blame] | 3393 | err = -ENOMEM; |
David Ahern | f88d8ea | 2019-06-03 20:19:52 -0700 | [diff] [blame] | 3394 | rt = fib6_info_alloc(gfp_flags, !nh); |
David Ahern | 93531c6 | 2018-04-17 17:33:25 -0700 | [diff] [blame] | 3395 | if (!rt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3396 | goto out; |
David Ahern | 93531c6 | 2018-04-17 17:33:25 -0700 | [diff] [blame] | 3397 | |
David Ahern | d7e774f | 2018-11-06 12:51:15 -0800 | [diff] [blame] | 3398 | rt->fib6_metrics = ip_fib_metrics_init(net, cfg->fc_mx, cfg->fc_mx_len, |
| 3399 | extack); |
David Ahern | 767a221 | 2018-10-04 20:07:51 -0700 | [diff] [blame] | 3400 | if (IS_ERR(rt->fib6_metrics)) { |
| 3401 | err = PTR_ERR(rt->fib6_metrics); |
Eric Dumazet | fda21d4 | 2018-10-05 09:17:50 -0700 | [diff] [blame] | 3402 | /* Do not leave garbage there. */ |
| 3403 | rt->fib6_metrics = (struct dst_metrics *)&dst_default_metrics; |
David Ahern | 767a221 | 2018-10-04 20:07:51 -0700 | [diff] [blame] | 3404 | goto out; |
| 3405 | } |
| 3406 | |
David Ahern | 93531c6 | 2018-04-17 17:33:25 -0700 | [diff] [blame] | 3407 | if (cfg->fc_flags & RTF_ADDRCONF) |
| 3408 | rt->dst_nocount = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3409 | |
Gao feng | 1716a96 | 2012-04-06 00:13:10 +0000 | [diff] [blame] | 3410 | if (cfg->fc_flags & RTF_EXPIRES) |
David Ahern | 1489568 | 2018-04-17 17:33:17 -0700 | [diff] [blame] | 3411 | fib6_set_expires(rt, jiffies + |
Gao feng | 1716a96 | 2012-04-06 00:13:10 +0000 | [diff] [blame] | 3412 | clock_t_to_jiffies(cfg->fc_expires)); |
| 3413 | else |
David Ahern | 1489568 | 2018-04-17 17:33:17 -0700 | [diff] [blame] | 3414 | fib6_clean_expires(rt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3415 | |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 3416 | if (cfg->fc_protocol == RTPROT_UNSPEC) |
| 3417 | cfg->fc_protocol = RTPROT_BOOT; |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 3418 | rt->fib6_protocol = cfg->fc_protocol; |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 3419 | |
David Ahern | 83c44251 | 2019-03-27 20:53:50 -0700 | [diff] [blame] | 3420 | rt->fib6_table = table; |
| 3421 | rt->fib6_metric = cfg->fc_metric; |
| 3422 | rt->fib6_type = cfg->fc_type; |
David Ahern | 2b2450c | 2019-03-27 20:53:52 -0700 | [diff] [blame] | 3423 | rt->fib6_flags = cfg->fc_flags & ~RTF_GATEWAY; |
Roopa Prabhu | 19e42e4 | 2015-07-21 10:43:48 +0200 | [diff] [blame] | 3424 | |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 3425 | ipv6_addr_prefix(&rt->fib6_dst.addr, &cfg->fc_dst, cfg->fc_dst_len); |
| 3426 | rt->fib6_dst.plen = cfg->fc_dst_len; |
| 3427 | if (rt->fib6_dst.plen == 128) |
David Ahern | 3b6761d | 2018-04-17 17:33:20 -0700 | [diff] [blame] | 3428 | rt->dst_host = true; |
Michal Kubeček | e5fd387 | 2014-03-27 13:04:08 +0100 | [diff] [blame] | 3429 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3430 | #ifdef CONFIG_IPV6_SUBTREES |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 3431 | ipv6_addr_prefix(&rt->fib6_src.addr, &cfg->fc_src, cfg->fc_src_len); |
| 3432 | rt->fib6_src.plen = cfg->fc_src_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3433 | #endif |
David Ahern | f88d8ea | 2019-06-03 20:19:52 -0700 | [diff] [blame] | 3434 | if (nh) { |
| 3435 | if (!nexthop_get(nh)) { |
| 3436 | NL_SET_ERR_MSG(extack, "Nexthop has been deleted"); |
| 3437 | goto out; |
| 3438 | } |
| 3439 | if (rt->fib6_src.plen) { |
Colin Ian King | 4daa95a | 2019-06-06 09:40:39 +0100 | [diff] [blame] | 3440 | 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] | 3441 | goto out; |
| 3442 | } |
| 3443 | rt->nh = nh; |
| 3444 | fib6_nh = nexthop_fib6_nh(rt->nh); |
| 3445 | } else { |
| 3446 | err = fib6_nh_init(net, rt->fib6_nh, cfg, gfp_flags, extack); |
| 3447 | if (err) |
| 3448 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3449 | |
David Ahern | f88d8ea | 2019-06-03 20:19:52 -0700 | [diff] [blame] | 3450 | fib6_nh = rt->fib6_nh; |
| 3451 | |
| 3452 | /* We cannot add true routes via loopback here, they would |
| 3453 | * result in kernel looping; promote them to reject routes |
| 3454 | */ |
| 3455 | addr_type = ipv6_addr_type(&cfg->fc_dst); |
| 3456 | if (fib6_is_reject(cfg->fc_flags, rt->fib6_nh->fib_nh_dev, |
| 3457 | addr_type)) |
| 3458 | rt->fib6_flags = RTF_REJECT | RTF_NONEXTHOP; |
| 3459 | } |
David Ahern | 955ec4c | 2018-01-24 19:45:29 -0800 | [diff] [blame] | 3460 | |
Daniel Walter | c3968a8 | 2011-04-13 21:10:57 +0000 | [diff] [blame] | 3461 | if (!ipv6_addr_any(&cfg->fc_prefsrc)) { |
David Ahern | f88d8ea | 2019-06-03 20:19:52 -0700 | [diff] [blame] | 3462 | struct net_device *dev = fib6_nh->fib_nh_dev; |
David Ahern | 83c44251 | 2019-03-27 20:53:50 -0700 | [diff] [blame] | 3463 | |
Daniel Walter | c3968a8 | 2011-04-13 21:10:57 +0000 | [diff] [blame] | 3464 | if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) { |
David Ahern | d5d531c | 2017-05-21 10:12:05 -0600 | [diff] [blame] | 3465 | NL_SET_ERR_MSG(extack, "Invalid source address"); |
Daniel Walter | c3968a8 | 2011-04-13 21:10:57 +0000 | [diff] [blame] | 3466 | err = -EINVAL; |
| 3467 | goto out; |
| 3468 | } |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 3469 | rt->fib6_prefsrc.addr = cfg->fc_prefsrc; |
| 3470 | rt->fib6_prefsrc.plen = 128; |
Daniel Walter | c3968a8 | 2011-04-13 21:10:57 +0000 | [diff] [blame] | 3471 | } else |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 3472 | rt->fib6_prefsrc.plen = 0; |
Daniel Walter | c3968a8 | 2011-04-13 21:10:57 +0000 | [diff] [blame] | 3473 | |
Roopa Prabhu | 8c5b83f | 2015-10-10 08:26:36 -0700 | [diff] [blame] | 3474 | return rt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3475 | out: |
David Ahern | 93531c6 | 2018-04-17 17:33:25 -0700 | [diff] [blame] | 3476 | fib6_info_release(rt); |
Roopa Prabhu | 8c5b83f | 2015-10-10 08:26:36 -0700 | [diff] [blame] | 3477 | return ERR_PTR(err); |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 3478 | } |
| 3479 | |
David Ahern | acb54e3 | 2018-04-17 17:33:22 -0700 | [diff] [blame] | 3480 | int ip6_route_add(struct fib6_config *cfg, gfp_t gfp_flags, |
David Ahern | 333c430 | 2017-05-21 10:12:04 -0600 | [diff] [blame] | 3481 | struct netlink_ext_ack *extack) |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 3482 | { |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 3483 | struct fib6_info *rt; |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 3484 | int err; |
| 3485 | |
David Ahern | acb54e3 | 2018-04-17 17:33:22 -0700 | [diff] [blame] | 3486 | rt = ip6_route_info_create(cfg, gfp_flags, extack); |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 3487 | if (IS_ERR(rt)) |
| 3488 | return PTR_ERR(rt); |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 3489 | |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 3490 | err = __ip6_ins_rt(rt, &cfg->fc_nlinfo, extack); |
David Ahern | 93531c6 | 2018-04-17 17:33:25 -0700 | [diff] [blame] | 3491 | fib6_info_release(rt); |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 3492 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3493 | return err; |
| 3494 | } |
| 3495 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 3496 | 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] | 3497 | { |
David Ahern | afb1d4b5 | 2018-04-17 17:33:11 -0700 | [diff] [blame] | 3498 | struct net *net = info->nl_net; |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 3499 | struct fib6_table *table; |
David Ahern | afb1d4b5 | 2018-04-17 17:33:11 -0700 | [diff] [blame] | 3500 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3501 | |
David Ahern | 421842e | 2018-04-17 17:33:18 -0700 | [diff] [blame] | 3502 | if (rt == net->ipv6.fib6_null_entry) { |
Gao feng | 6825a26 | 2012-09-19 19:25:34 +0000 | [diff] [blame] | 3503 | err = -ENOENT; |
| 3504 | goto out; |
| 3505 | } |
Patrick McHardy | 6c813a7 | 2006-08-06 22:22:47 -0700 | [diff] [blame] | 3506 | |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 3507 | table = rt->fib6_table; |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 3508 | spin_lock_bh(&table->tb6_lock); |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 3509 | err = fib6_del(rt, info); |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 3510 | spin_unlock_bh(&table->tb6_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3511 | |
Gao feng | 6825a26 | 2012-09-19 19:25:34 +0000 | [diff] [blame] | 3512 | out: |
David Ahern | 93531c6 | 2018-04-17 17:33:25 -0700 | [diff] [blame] | 3513 | fib6_info_release(rt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3514 | return err; |
| 3515 | } |
| 3516 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 3517 | int ip6_del_rt(struct net *net, struct fib6_info *rt) |
Thomas Graf | e0a1ad73 | 2006-08-22 00:00:21 -0700 | [diff] [blame] | 3518 | { |
David Ahern | afb1d4b5 | 2018-04-17 17:33:11 -0700 | [diff] [blame] | 3519 | struct nl_info info = { .nl_net = net }; |
| 3520 | |
Denis V. Lunev | 528c4ce | 2007-12-13 09:45:12 -0800 | [diff] [blame] | 3521 | return __ip6_del_rt(rt, &info); |
Thomas Graf | e0a1ad73 | 2006-08-22 00:00:21 -0700 | [diff] [blame] | 3522 | } |
| 3523 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 3524 | 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] | 3525 | { |
| 3526 | struct nl_info *info = &cfg->fc_nlinfo; |
WANG Cong | e333003 | 2017-02-27 16:07:43 -0800 | [diff] [blame] | 3527 | struct net *net = info->nl_net; |
David Ahern | 16a16cd | 2017-02-02 12:37:11 -0800 | [diff] [blame] | 3528 | struct sk_buff *skb = NULL; |
David Ahern | 0ae8133 | 2017-02-02 12:37:08 -0800 | [diff] [blame] | 3529 | struct fib6_table *table; |
WANG Cong | e333003 | 2017-02-27 16:07:43 -0800 | [diff] [blame] | 3530 | int err = -ENOENT; |
David Ahern | 0ae8133 | 2017-02-02 12:37:08 -0800 | [diff] [blame] | 3531 | |
David Ahern | 421842e | 2018-04-17 17:33:18 -0700 | [diff] [blame] | 3532 | if (rt == net->ipv6.fib6_null_entry) |
WANG Cong | e333003 | 2017-02-27 16:07:43 -0800 | [diff] [blame] | 3533 | goto out_put; |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 3534 | table = rt->fib6_table; |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 3535 | spin_lock_bh(&table->tb6_lock); |
David Ahern | 0ae8133 | 2017-02-02 12:37:08 -0800 | [diff] [blame] | 3536 | |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 3537 | if (rt->fib6_nsiblings && cfg->fc_delete_all_nh) { |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 3538 | struct fib6_info *sibling, *next_sibling; |
David Ahern | 0ae8133 | 2017-02-02 12:37:08 -0800 | [diff] [blame] | 3539 | |
David Ahern | 16a16cd | 2017-02-02 12:37:11 -0800 | [diff] [blame] | 3540 | /* prefer to send a single notification with all hops */ |
| 3541 | skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any()); |
| 3542 | if (skb) { |
| 3543 | u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0; |
| 3544 | |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 3545 | if (rt6_fill_node(net, skb, rt, NULL, |
David Ahern | 16a16cd | 2017-02-02 12:37:11 -0800 | [diff] [blame] | 3546 | NULL, NULL, 0, RTM_DELROUTE, |
| 3547 | info->portid, seq, 0) < 0) { |
| 3548 | kfree_skb(skb); |
| 3549 | skb = NULL; |
| 3550 | } else |
| 3551 | info->skip_notify = 1; |
| 3552 | } |
| 3553 | |
David Ahern | 0ae8133 | 2017-02-02 12:37:08 -0800 | [diff] [blame] | 3554 | list_for_each_entry_safe(sibling, next_sibling, |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 3555 | &rt->fib6_siblings, |
| 3556 | fib6_siblings) { |
David Ahern | 0ae8133 | 2017-02-02 12:37:08 -0800 | [diff] [blame] | 3557 | err = fib6_del(sibling, info); |
| 3558 | if (err) |
WANG Cong | e333003 | 2017-02-27 16:07:43 -0800 | [diff] [blame] | 3559 | goto out_unlock; |
David Ahern | 0ae8133 | 2017-02-02 12:37:08 -0800 | [diff] [blame] | 3560 | } |
| 3561 | } |
| 3562 | |
| 3563 | err = fib6_del(rt, info); |
WANG Cong | e333003 | 2017-02-27 16:07:43 -0800 | [diff] [blame] | 3564 | out_unlock: |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 3565 | spin_unlock_bh(&table->tb6_lock); |
WANG Cong | e333003 | 2017-02-27 16:07:43 -0800 | [diff] [blame] | 3566 | out_put: |
David Ahern | 93531c6 | 2018-04-17 17:33:25 -0700 | [diff] [blame] | 3567 | fib6_info_release(rt); |
David Ahern | 16a16cd | 2017-02-02 12:37:11 -0800 | [diff] [blame] | 3568 | |
| 3569 | if (skb) { |
WANG Cong | e333003 | 2017-02-27 16:07:43 -0800 | [diff] [blame] | 3570 | rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE, |
David Ahern | 16a16cd | 2017-02-02 12:37:11 -0800 | [diff] [blame] | 3571 | info->nlh, gfp_any()); |
| 3572 | } |
David Ahern | 0ae8133 | 2017-02-02 12:37:08 -0800 | [diff] [blame] | 3573 | return err; |
| 3574 | } |
| 3575 | |
David Ahern | 0fa6efc | 2019-05-22 20:28:00 -0700 | [diff] [blame] | 3576 | 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] | 3577 | { |
| 3578 | int rc = -ESRCH; |
| 3579 | |
| 3580 | if (cfg->fc_ifindex && rt->dst.dev->ifindex != cfg->fc_ifindex) |
| 3581 | goto out; |
| 3582 | |
| 3583 | if (cfg->fc_flags & RTF_GATEWAY && |
| 3584 | !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway)) |
| 3585 | goto out; |
Xin Long | 761f602 | 2018-11-14 00:48:28 +0800 | [diff] [blame] | 3586 | |
| 3587 | rc = rt6_remove_exception_rt(rt); |
David Ahern | 23fb93a | 2018-04-17 17:33:23 -0700 | [diff] [blame] | 3588 | out: |
| 3589 | return rc; |
| 3590 | } |
| 3591 | |
David Ahern | 0fa6efc | 2019-05-22 20:28:00 -0700 | [diff] [blame] | 3592 | static int ip6_del_cached_rt(struct fib6_config *cfg, struct fib6_info *rt, |
| 3593 | struct fib6_nh *nh) |
| 3594 | { |
| 3595 | struct fib6_result res = { |
| 3596 | .f6i = rt, |
| 3597 | .nh = nh, |
| 3598 | }; |
| 3599 | struct rt6_info *rt_cache; |
| 3600 | |
| 3601 | rt_cache = rt6_find_cached_rt(&res, &cfg->fc_dst, &cfg->fc_src); |
| 3602 | if (rt_cache) |
| 3603 | return __ip6_del_cached_rt(rt_cache, cfg); |
| 3604 | |
| 3605 | return 0; |
| 3606 | } |
| 3607 | |
David Ahern | 333c430 | 2017-05-21 10:12:04 -0600 | [diff] [blame] | 3608 | static int ip6_route_del(struct fib6_config *cfg, |
| 3609 | struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3610 | { |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 3611 | struct fib6_table *table; |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 3612 | struct fib6_info *rt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3613 | struct fib6_node *fn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3614 | int err = -ESRCH; |
| 3615 | |
Daniel Lezcano | 5578689 | 2008-03-04 13:47:47 -0800 | [diff] [blame] | 3616 | table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table); |
David Ahern | d5d531c | 2017-05-21 10:12:05 -0600 | [diff] [blame] | 3617 | if (!table) { |
| 3618 | NL_SET_ERR_MSG(extack, "FIB table does not exist"); |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 3619 | return err; |
David Ahern | d5d531c | 2017-05-21 10:12:05 -0600 | [diff] [blame] | 3620 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3621 | |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 3622 | rcu_read_lock(); |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 3623 | |
| 3624 | fn = fib6_locate(&table->tb6_root, |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 3625 | &cfg->fc_dst, cfg->fc_dst_len, |
Wei Wang | 38fbeee | 2017-10-06 12:06:02 -0700 | [diff] [blame] | 3626 | &cfg->fc_src, cfg->fc_src_len, |
Wei Wang | 2b760fc | 2017-10-06 12:06:03 -0700 | [diff] [blame] | 3627 | !(cfg->fc_flags & RTF_CACHE)); |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 3628 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3629 | if (fn) { |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 3630 | for_each_fib6_node_rt_rcu(fn) { |
David Ahern | ad1601a | 2019-03-27 20:53:56 -0700 | [diff] [blame] | 3631 | struct fib6_nh *nh; |
| 3632 | |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 3633 | nh = rt->fib6_nh; |
Wei Wang | 2b760fc | 2017-10-06 12:06:03 -0700 | [diff] [blame] | 3634 | if (cfg->fc_flags & RTF_CACHE) { |
David Ahern | 23fb93a | 2018-04-17 17:33:23 -0700 | [diff] [blame] | 3635 | int rc; |
| 3636 | |
David Ahern | 0fa6efc | 2019-05-22 20:28:00 -0700 | [diff] [blame] | 3637 | rc = ip6_del_cached_rt(cfg, rt, nh); |
| 3638 | if (rc != -ESRCH) { |
| 3639 | rcu_read_unlock(); |
| 3640 | return rc; |
David Ahern | 23fb93a | 2018-04-17 17:33:23 -0700 | [diff] [blame] | 3641 | } |
| 3642 | continue; |
Wei Wang | 2b760fc | 2017-10-06 12:06:03 -0700 | [diff] [blame] | 3643 | } |
David Ahern | ad1601a | 2019-03-27 20:53:56 -0700 | [diff] [blame] | 3644 | |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 3645 | if (cfg->fc_ifindex && |
David Ahern | ad1601a | 2019-03-27 20:53:56 -0700 | [diff] [blame] | 3646 | (!nh->fib_nh_dev || |
| 3647 | nh->fib_nh_dev->ifindex != cfg->fc_ifindex)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3648 | continue; |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 3649 | if (cfg->fc_flags & RTF_GATEWAY && |
David Ahern | ad1601a | 2019-03-27 20:53:56 -0700 | [diff] [blame] | 3650 | !ipv6_addr_equal(&cfg->fc_gateway, &nh->fib_nh_gw6)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3651 | continue; |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 3652 | if (cfg->fc_metric && cfg->fc_metric != rt->fib6_metric) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3653 | continue; |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 3654 | if (cfg->fc_protocol && cfg->fc_protocol != rt->fib6_protocol) |
Mantas M | c2ed188 | 2016-12-16 10:30:59 +0200 | [diff] [blame] | 3655 | continue; |
Wei Wang | e873e4b | 2018-07-21 20:56:32 -0700 | [diff] [blame] | 3656 | if (!fib6_info_hold_safe(rt)) |
| 3657 | continue; |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 3658 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3659 | |
David Ahern | 0ae8133 | 2017-02-02 12:37:08 -0800 | [diff] [blame] | 3660 | /* if gateway was specified only delete the one hop */ |
| 3661 | if (cfg->fc_flags & RTF_GATEWAY) |
| 3662 | return __ip6_del_rt(rt, &cfg->fc_nlinfo); |
| 3663 | |
| 3664 | return __ip6_del_rt_siblings(rt, cfg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3665 | } |
| 3666 | } |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 3667 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3668 | |
| 3669 | return err; |
| 3670 | } |
| 3671 | |
David S. Miller | 6700c27 | 2012-07-17 03:29:28 -0700 | [diff] [blame] | 3672 | 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] | 3673 | { |
YOSHIFUJI Hideaki | a627945 | 2006-08-23 17:18:26 -0700 | [diff] [blame] | 3674 | struct netevent_redirect netevent; |
David S. Miller | e8599ff | 2012-07-11 23:43:53 -0700 | [diff] [blame] | 3675 | struct rt6_info *rt, *nrt = NULL; |
David Ahern | 85bd05d | 2019-04-16 14:36:01 -0700 | [diff] [blame] | 3676 | struct fib6_result res = {}; |
David S. Miller | e8599ff | 2012-07-11 23:43:53 -0700 | [diff] [blame] | 3677 | struct ndisc_options ndopts; |
| 3678 | struct inet6_dev *in6_dev; |
| 3679 | struct neighbour *neigh; |
YOSHIFUJI Hideaki / 吉藤英明 | 71bcdba | 2013-01-05 16:34:51 +0000 | [diff] [blame] | 3680 | struct rd_msg *msg; |
David S. Miller | 6e157b6 | 2012-07-12 00:05:02 -0700 | [diff] [blame] | 3681 | int optlen, on_link; |
| 3682 | u8 *lladdr; |
David S. Miller | e8599ff | 2012-07-11 23:43:53 -0700 | [diff] [blame] | 3683 | |
Simon Horman | 29a3cad | 2013-05-28 20:34:26 +0000 | [diff] [blame] | 3684 | optlen = skb_tail_pointer(skb) - skb_transport_header(skb); |
YOSHIFUJI Hideaki / 吉藤英明 | 71bcdba | 2013-01-05 16:34:51 +0000 | [diff] [blame] | 3685 | optlen -= sizeof(*msg); |
David S. Miller | e8599ff | 2012-07-11 23:43:53 -0700 | [diff] [blame] | 3686 | |
| 3687 | if (optlen < 0) { |
David S. Miller | 6e157b6 | 2012-07-12 00:05:02 -0700 | [diff] [blame] | 3688 | net_dbg_ratelimited("rt6_do_redirect: packet too short\n"); |
David S. Miller | e8599ff | 2012-07-11 23:43:53 -0700 | [diff] [blame] | 3689 | return; |
| 3690 | } |
| 3691 | |
YOSHIFUJI Hideaki / 吉藤英明 | 71bcdba | 2013-01-05 16:34:51 +0000 | [diff] [blame] | 3692 | msg = (struct rd_msg *)icmp6_hdr(skb); |
David S. Miller | e8599ff | 2012-07-11 23:43:53 -0700 | [diff] [blame] | 3693 | |
YOSHIFUJI Hideaki / 吉藤英明 | 71bcdba | 2013-01-05 16:34:51 +0000 | [diff] [blame] | 3694 | if (ipv6_addr_is_multicast(&msg->dest)) { |
David S. Miller | 6e157b6 | 2012-07-12 00:05:02 -0700 | [diff] [blame] | 3695 | net_dbg_ratelimited("rt6_do_redirect: destination address is multicast\n"); |
David S. Miller | e8599ff | 2012-07-11 23:43:53 -0700 | [diff] [blame] | 3696 | return; |
| 3697 | } |
| 3698 | |
David S. Miller | 6e157b6 | 2012-07-12 00:05:02 -0700 | [diff] [blame] | 3699 | on_link = 0; |
YOSHIFUJI Hideaki / 吉藤英明 | 71bcdba | 2013-01-05 16:34:51 +0000 | [diff] [blame] | 3700 | if (ipv6_addr_equal(&msg->dest, &msg->target)) { |
David S. Miller | e8599ff | 2012-07-11 23:43:53 -0700 | [diff] [blame] | 3701 | on_link = 1; |
YOSHIFUJI Hideaki / 吉藤英明 | 71bcdba | 2013-01-05 16:34:51 +0000 | [diff] [blame] | 3702 | } else if (ipv6_addr_type(&msg->target) != |
David S. Miller | e8599ff | 2012-07-11 23:43:53 -0700 | [diff] [blame] | 3703 | (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) { |
David S. Miller | 6e157b6 | 2012-07-12 00:05:02 -0700 | [diff] [blame] | 3704 | 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] | 3705 | return; |
| 3706 | } |
| 3707 | |
| 3708 | in6_dev = __in6_dev_get(skb->dev); |
| 3709 | if (!in6_dev) |
| 3710 | return; |
| 3711 | if (in6_dev->cnf.forwarding || !in6_dev->cnf.accept_redirects) |
| 3712 | return; |
| 3713 | |
| 3714 | /* RFC2461 8.1: |
| 3715 | * The IP source address of the Redirect MUST be the same as the current |
| 3716 | * first-hop router for the specified ICMP Destination Address. |
| 3717 | */ |
| 3718 | |
Alexander Aring | f997c55 | 2016-06-15 21:20:23 +0200 | [diff] [blame] | 3719 | if (!ndisc_parse_options(skb->dev, msg->opt, optlen, &ndopts)) { |
David S. Miller | e8599ff | 2012-07-11 23:43:53 -0700 | [diff] [blame] | 3720 | net_dbg_ratelimited("rt6_redirect: invalid ND options\n"); |
| 3721 | return; |
| 3722 | } |
David S. Miller | 6e157b6 | 2012-07-12 00:05:02 -0700 | [diff] [blame] | 3723 | |
| 3724 | lladdr = NULL; |
David S. Miller | e8599ff | 2012-07-11 23:43:53 -0700 | [diff] [blame] | 3725 | if (ndopts.nd_opts_tgt_lladdr) { |
| 3726 | lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr, |
| 3727 | skb->dev); |
| 3728 | if (!lladdr) { |
| 3729 | net_dbg_ratelimited("rt6_redirect: invalid link-layer address length\n"); |
| 3730 | return; |
| 3731 | } |
| 3732 | } |
| 3733 | |
David S. Miller | 6e157b6 | 2012-07-12 00:05:02 -0700 | [diff] [blame] | 3734 | rt = (struct rt6_info *) dst; |
Matthias Schiffer | ec13ad1 | 2015-11-02 01:24:38 +0100 | [diff] [blame] | 3735 | if (rt->rt6i_flags & RTF_REJECT) { |
David S. Miller | 6e157b6 | 2012-07-12 00:05:02 -0700 | [diff] [blame] | 3736 | net_dbg_ratelimited("rt6_redirect: source isn't a valid nexthop for redirect target\n"); |
| 3737 | return; |
| 3738 | } |
| 3739 | |
| 3740 | /* Redirect received -> path was valid. |
| 3741 | * Look, redirects are sent only in response to data packets, |
| 3742 | * so that this nexthop apparently is reachable. --ANK |
| 3743 | */ |
Julian Anastasov | 0dec879 | 2017-02-06 23:14:16 +0200 | [diff] [blame] | 3744 | dst_confirm_neigh(&rt->dst, &ipv6_hdr(skb)->saddr); |
David S. Miller | 6e157b6 | 2012-07-12 00:05:02 -0700 | [diff] [blame] | 3745 | |
YOSHIFUJI Hideaki / 吉藤英明 | 71bcdba | 2013-01-05 16:34:51 +0000 | [diff] [blame] | 3746 | neigh = __neigh_lookup(&nd_tbl, &msg->target, skb->dev, 1); |
David S. Miller | e8599ff | 2012-07-11 23:43:53 -0700 | [diff] [blame] | 3747 | if (!neigh) |
| 3748 | return; |
| 3749 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3750 | /* |
| 3751 | * We have finally decided to accept it. |
| 3752 | */ |
| 3753 | |
Alexander Aring | f997c55 | 2016-06-15 21:20:23 +0200 | [diff] [blame] | 3754 | ndisc_update(skb->dev, neigh, lladdr, NUD_STALE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3755 | NEIGH_UPDATE_F_WEAK_OVERRIDE| |
| 3756 | NEIGH_UPDATE_F_OVERRIDE| |
| 3757 | (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER| |
Alexander Aring | f997c55 | 2016-06-15 21:20:23 +0200 | [diff] [blame] | 3758 | NEIGH_UPDATE_F_ISROUTER)), |
| 3759 | NDISC_REDIRECT, &ndopts); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3760 | |
David Ahern | 4d85cd0 | 2018-04-20 15:37:59 -0700 | [diff] [blame] | 3761 | rcu_read_lock(); |
David Ahern | 85bd05d | 2019-04-16 14:36:01 -0700 | [diff] [blame] | 3762 | res.f6i = rcu_dereference(rt->from); |
David S. Miller | ff24e49 | 2019-05-02 22:14:21 -0400 | [diff] [blame] | 3763 | if (!res.f6i) |
Martin KaFai Lau | 886b7a5 | 2019-04-30 10:45:12 -0700 | [diff] [blame] | 3764 | goto out; |
David Ahern | 8a14e46 | 2018-04-23 11:32:07 -0700 | [diff] [blame] | 3765 | |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 3766 | res.nh = res.f6i->fib6_nh; |
David Ahern | 7d21fec | 2019-04-16 14:36:11 -0700 | [diff] [blame] | 3767 | res.fib6_flags = res.f6i->fib6_flags; |
| 3768 | res.fib6_type = res.f6i->fib6_type; |
David Ahern | 85bd05d | 2019-04-16 14:36:01 -0700 | [diff] [blame] | 3769 | nrt = ip6_rt_cache_alloc(&res, &msg->dest, NULL); |
David S. Miller | 3830847 | 2011-12-03 18:02:47 -0500 | [diff] [blame] | 3770 | if (!nrt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3771 | goto out; |
| 3772 | |
| 3773 | nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE; |
| 3774 | if (on_link) |
| 3775 | nrt->rt6i_flags &= ~RTF_GATEWAY; |
| 3776 | |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 3777 | nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3778 | |
Martin KaFai Lau | 886b7a5 | 2019-04-30 10:45:12 -0700 | [diff] [blame] | 3779 | /* rt6_insert_exception() will take care of duplicated exceptions */ |
David Ahern | 5012f0a | 2019-04-16 14:36:05 -0700 | [diff] [blame] | 3780 | if (rt6_insert_exception(nrt, &res)) { |
Wei Wang | 2b760fc | 2017-10-06 12:06:03 -0700 | [diff] [blame] | 3781 | dst_release_immediate(&nrt->dst); |
| 3782 | goto out; |
| 3783 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3784 | |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 3785 | netevent.old = &rt->dst; |
| 3786 | netevent.new = &nrt->dst; |
YOSHIFUJI Hideaki / 吉藤英明 | 71bcdba | 2013-01-05 16:34:51 +0000 | [diff] [blame] | 3787 | netevent.daddr = &msg->dest; |
YOSHIFUJI Hideaki / 吉藤英明 | 6059283 | 2013-01-14 09:28:27 +0000 | [diff] [blame] | 3788 | netevent.neigh = neigh; |
Tom Tucker | 8d71740 | 2006-07-30 20:43:36 -0700 | [diff] [blame] | 3789 | call_netevent_notifiers(NETEVENT_REDIRECT, &netevent); |
| 3790 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3791 | out: |
Martin KaFai Lau | 886b7a5 | 2019-04-30 10:45:12 -0700 | [diff] [blame] | 3792 | rcu_read_unlock(); |
David S. Miller | e8599ff | 2012-07-11 23:43:53 -0700 | [diff] [blame] | 3793 | neigh_release(neigh); |
David S. Miller | 6e157b6 | 2012-07-12 00:05:02 -0700 | [diff] [blame] | 3794 | } |
| 3795 | |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 3796 | #ifdef CONFIG_IPV6_ROUTE_INFO |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 3797 | static struct fib6_info *rt6_get_route_info(struct net *net, |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 3798 | const struct in6_addr *prefix, int prefixlen, |
David Ahern | 830218c | 2016-10-24 10:52:35 -0700 | [diff] [blame] | 3799 | const struct in6_addr *gwaddr, |
| 3800 | struct net_device *dev) |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 3801 | { |
David Ahern | 830218c | 2016-10-24 10:52:35 -0700 | [diff] [blame] | 3802 | u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO; |
| 3803 | int ifindex = dev->ifindex; |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 3804 | struct fib6_node *fn; |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 3805 | struct fib6_info *rt = NULL; |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 3806 | struct fib6_table *table; |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 3807 | |
David Ahern | 830218c | 2016-10-24 10:52:35 -0700 | [diff] [blame] | 3808 | table = fib6_get_table(net, tb_id); |
David S. Miller | 3830847 | 2011-12-03 18:02:47 -0500 | [diff] [blame] | 3809 | if (!table) |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 3810 | return NULL; |
| 3811 | |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 3812 | rcu_read_lock(); |
Wei Wang | 38fbeee | 2017-10-06 12:06:02 -0700 | [diff] [blame] | 3813 | fn = fib6_locate(&table->tb6_root, prefix, prefixlen, NULL, 0, true); |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 3814 | if (!fn) |
| 3815 | goto out; |
| 3816 | |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 3817 | for_each_fib6_node_rt_rcu(fn) { |
David Ahern | f88d8ea | 2019-06-03 20:19:52 -0700 | [diff] [blame] | 3818 | /* these routes do not use nexthops */ |
| 3819 | if (rt->nh) |
| 3820 | continue; |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 3821 | if (rt->fib6_nh->fib_nh_dev->ifindex != ifindex) |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 3822 | continue; |
David Ahern | 2b2450c | 2019-03-27 20:53:52 -0700 | [diff] [blame] | 3823 | if (!(rt->fib6_flags & RTF_ROUTEINFO) || |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 3824 | !rt->fib6_nh->fib_nh_gw_family) |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 3825 | continue; |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 3826 | if (!ipv6_addr_equal(&rt->fib6_nh->fib_nh_gw6, gwaddr)) |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 3827 | continue; |
Wei Wang | e873e4b | 2018-07-21 20:56:32 -0700 | [diff] [blame] | 3828 | if (!fib6_info_hold_safe(rt)) |
| 3829 | continue; |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 3830 | break; |
| 3831 | } |
| 3832 | out: |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 3833 | rcu_read_unlock(); |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 3834 | return rt; |
| 3835 | } |
| 3836 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 3837 | static struct fib6_info *rt6_add_route_info(struct net *net, |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 3838 | const struct in6_addr *prefix, int prefixlen, |
David Ahern | 830218c | 2016-10-24 10:52:35 -0700 | [diff] [blame] | 3839 | const struct in6_addr *gwaddr, |
| 3840 | struct net_device *dev, |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 3841 | unsigned int pref) |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 3842 | { |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 3843 | struct fib6_config cfg = { |
Rami Rosen | 238fc7e | 2008-02-09 23:43:11 -0800 | [diff] [blame] | 3844 | .fc_metric = IP6_RT_PRIO_USER, |
David Ahern | 830218c | 2016-10-24 10:52:35 -0700 | [diff] [blame] | 3845 | .fc_ifindex = dev->ifindex, |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 3846 | .fc_dst_len = prefixlen, |
| 3847 | .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO | |
| 3848 | RTF_UP | RTF_PREF(pref), |
Xin Long | b91d532 | 2017-08-03 14:13:46 +0800 | [diff] [blame] | 3849 | .fc_protocol = RTPROT_RA, |
David Ahern | e8478e8 | 2018-04-17 17:33:13 -0700 | [diff] [blame] | 3850 | .fc_type = RTN_UNICAST, |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 3851 | .fc_nlinfo.portid = 0, |
Daniel Lezcano | efa2cea | 2008-03-04 13:46:48 -0800 | [diff] [blame] | 3852 | .fc_nlinfo.nlh = NULL, |
| 3853 | .fc_nlinfo.nl_net = net, |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 3854 | }; |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 3855 | |
David Ahern | 830218c | 2016-10-24 10:52:35 -0700 | [diff] [blame] | 3856 | cfg.fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO, |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 3857 | cfg.fc_dst = *prefix; |
| 3858 | cfg.fc_gateway = *gwaddr; |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 3859 | |
YOSHIFUJI Hideaki | e317da9 | 2006-03-20 17:06:42 -0800 | [diff] [blame] | 3860 | /* We should treat it as a default route if prefix length is 0. */ |
| 3861 | if (!prefixlen) |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 3862 | cfg.fc_flags |= RTF_DEFAULT; |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 3863 | |
David Ahern | acb54e3 | 2018-04-17 17:33:22 -0700 | [diff] [blame] | 3864 | ip6_route_add(&cfg, GFP_ATOMIC, NULL); |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 3865 | |
David Ahern | 830218c | 2016-10-24 10:52:35 -0700 | [diff] [blame] | 3866 | return rt6_get_route_info(net, prefix, prefixlen, gwaddr, dev); |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 3867 | } |
| 3868 | #endif |
| 3869 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 3870 | struct fib6_info *rt6_get_dflt_router(struct net *net, |
David Ahern | afb1d4b5 | 2018-04-17 17:33:11 -0700 | [diff] [blame] | 3871 | const struct in6_addr *addr, |
| 3872 | struct net_device *dev) |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 3873 | { |
David Ahern | 830218c | 2016-10-24 10:52:35 -0700 | [diff] [blame] | 3874 | u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT; |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 3875 | struct fib6_info *rt; |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 3876 | struct fib6_table *table; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3877 | |
David Ahern | afb1d4b5 | 2018-04-17 17:33:11 -0700 | [diff] [blame] | 3878 | table = fib6_get_table(net, tb_id); |
David S. Miller | 3830847 | 2011-12-03 18:02:47 -0500 | [diff] [blame] | 3879 | if (!table) |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 3880 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3881 | |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 3882 | rcu_read_lock(); |
| 3883 | for_each_fib6_node_rt_rcu(&table->tb6_root) { |
David Ahern | f88d8ea | 2019-06-03 20:19:52 -0700 | [diff] [blame] | 3884 | struct fib6_nh *nh; |
David Ahern | ad1601a | 2019-03-27 20:53:56 -0700 | [diff] [blame] | 3885 | |
David Ahern | f88d8ea | 2019-06-03 20:19:52 -0700 | [diff] [blame] | 3886 | /* RA routes do not use nexthops */ |
| 3887 | if (rt->nh) |
| 3888 | continue; |
| 3889 | |
| 3890 | nh = rt->fib6_nh; |
David Ahern | ad1601a | 2019-03-27 20:53:56 -0700 | [diff] [blame] | 3891 | if (dev == nh->fib_nh_dev && |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 3892 | ((rt->fib6_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) && |
David Ahern | ad1601a | 2019-03-27 20:53:56 -0700 | [diff] [blame] | 3893 | ipv6_addr_equal(&nh->fib_nh_gw6, addr)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3894 | break; |
| 3895 | } |
Wei Wang | e873e4b | 2018-07-21 20:56:32 -0700 | [diff] [blame] | 3896 | if (rt && !fib6_info_hold_safe(rt)) |
| 3897 | rt = NULL; |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 3898 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3899 | return rt; |
| 3900 | } |
| 3901 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 3902 | struct fib6_info *rt6_add_dflt_router(struct net *net, |
David Ahern | afb1d4b5 | 2018-04-17 17:33:11 -0700 | [diff] [blame] | 3903 | const struct in6_addr *gwaddr, |
YOSHIFUJI Hideaki | ebacaaa | 2006-03-20 17:04:53 -0800 | [diff] [blame] | 3904 | struct net_device *dev, |
| 3905 | unsigned int pref) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3906 | { |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 3907 | struct fib6_config cfg = { |
David Ahern | ca25449 | 2015-10-12 11:47:10 -0700 | [diff] [blame] | 3908 | .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT, |
Rami Rosen | 238fc7e | 2008-02-09 23:43:11 -0800 | [diff] [blame] | 3909 | .fc_metric = IP6_RT_PRIO_USER, |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 3910 | .fc_ifindex = dev->ifindex, |
| 3911 | .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT | |
| 3912 | RTF_UP | RTF_EXPIRES | RTF_PREF(pref), |
Xin Long | b91d532 | 2017-08-03 14:13:46 +0800 | [diff] [blame] | 3913 | .fc_protocol = RTPROT_RA, |
David Ahern | e8478e8 | 2018-04-17 17:33:13 -0700 | [diff] [blame] | 3914 | .fc_type = RTN_UNICAST, |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 3915 | .fc_nlinfo.portid = 0, |
Daniel Lezcano | 5578689 | 2008-03-04 13:47:47 -0800 | [diff] [blame] | 3916 | .fc_nlinfo.nlh = NULL, |
David Ahern | afb1d4b5 | 2018-04-17 17:33:11 -0700 | [diff] [blame] | 3917 | .fc_nlinfo.nl_net = net, |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 3918 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3919 | |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 3920 | cfg.fc_gateway = *gwaddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3921 | |
David Ahern | acb54e3 | 2018-04-17 17:33:22 -0700 | [diff] [blame] | 3922 | if (!ip6_route_add(&cfg, GFP_ATOMIC, NULL)) { |
David Ahern | 830218c | 2016-10-24 10:52:35 -0700 | [diff] [blame] | 3923 | struct fib6_table *table; |
| 3924 | |
| 3925 | table = fib6_get_table(dev_net(dev), cfg.fc_table); |
| 3926 | if (table) |
| 3927 | table->flags |= RT6_TABLE_HAS_DFLT_ROUTER; |
| 3928 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3929 | |
David Ahern | afb1d4b5 | 2018-04-17 17:33:11 -0700 | [diff] [blame] | 3930 | return rt6_get_dflt_router(net, gwaddr, dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3931 | } |
| 3932 | |
David Ahern | afb1d4b5 | 2018-04-17 17:33:11 -0700 | [diff] [blame] | 3933 | static void __rt6_purge_dflt_routers(struct net *net, |
| 3934 | struct fib6_table *table) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3935 | { |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 3936 | struct fib6_info *rt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3937 | |
| 3938 | restart: |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 3939 | rcu_read_lock(); |
| 3940 | for_each_fib6_node_rt_rcu(&table->tb6_root) { |
David Ahern | dcd1f57 | 2018-04-18 15:39:05 -0700 | [diff] [blame] | 3941 | struct net_device *dev = fib6_info_nh_dev(rt); |
| 3942 | struct inet6_dev *idev = dev ? __in6_dev_get(dev) : NULL; |
| 3943 | |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 3944 | if (rt->fib6_flags & (RTF_DEFAULT | RTF_ADDRCONF) && |
Wei Wang | e873e4b | 2018-07-21 20:56:32 -0700 | [diff] [blame] | 3945 | (!idev || idev->cnf.accept_ra != 2) && |
| 3946 | fib6_info_hold_safe(rt)) { |
David Ahern | 93531c6 | 2018-04-17 17:33:25 -0700 | [diff] [blame] | 3947 | rcu_read_unlock(); |
| 3948 | ip6_del_rt(net, rt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3949 | goto restart; |
| 3950 | } |
| 3951 | } |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 3952 | rcu_read_unlock(); |
David Ahern | 830218c | 2016-10-24 10:52:35 -0700 | [diff] [blame] | 3953 | |
| 3954 | table->flags &= ~RT6_TABLE_HAS_DFLT_ROUTER; |
| 3955 | } |
| 3956 | |
| 3957 | void rt6_purge_dflt_routers(struct net *net) |
| 3958 | { |
| 3959 | struct fib6_table *table; |
| 3960 | struct hlist_head *head; |
| 3961 | unsigned int h; |
| 3962 | |
| 3963 | rcu_read_lock(); |
| 3964 | |
| 3965 | for (h = 0; h < FIB6_TABLE_HASHSZ; h++) { |
| 3966 | head = &net->ipv6.fib_table_hash[h]; |
| 3967 | hlist_for_each_entry_rcu(table, head, tb6_hlist) { |
| 3968 | if (table->flags & RT6_TABLE_HAS_DFLT_ROUTER) |
David Ahern | afb1d4b5 | 2018-04-17 17:33:11 -0700 | [diff] [blame] | 3969 | __rt6_purge_dflt_routers(net, table); |
David Ahern | 830218c | 2016-10-24 10:52:35 -0700 | [diff] [blame] | 3970 | } |
| 3971 | } |
| 3972 | |
| 3973 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3974 | } |
| 3975 | |
Daniel Lezcano | 5578689 | 2008-03-04 13:47:47 -0800 | [diff] [blame] | 3976 | static void rtmsg_to_fib6_config(struct net *net, |
| 3977 | struct in6_rtmsg *rtmsg, |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 3978 | struct fib6_config *cfg) |
| 3979 | { |
Maciej Żenczykowski | 8823a3a | 2018-09-29 23:44:52 -0700 | [diff] [blame] | 3980 | *cfg = (struct fib6_config){ |
| 3981 | .fc_table = l3mdev_fib_table_by_index(net, rtmsg->rtmsg_ifindex) ? |
| 3982 | : RT6_TABLE_MAIN, |
| 3983 | .fc_ifindex = rtmsg->rtmsg_ifindex, |
David Ahern | 67f6951 | 2019-03-21 05:21:34 -0700 | [diff] [blame] | 3984 | .fc_metric = rtmsg->rtmsg_metric ? : IP6_RT_PRIO_USER, |
Maciej Żenczykowski | 8823a3a | 2018-09-29 23:44:52 -0700 | [diff] [blame] | 3985 | .fc_expires = rtmsg->rtmsg_info, |
| 3986 | .fc_dst_len = rtmsg->rtmsg_dst_len, |
| 3987 | .fc_src_len = rtmsg->rtmsg_src_len, |
| 3988 | .fc_flags = rtmsg->rtmsg_flags, |
| 3989 | .fc_type = rtmsg->rtmsg_type, |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 3990 | |
Maciej Żenczykowski | 8823a3a | 2018-09-29 23:44:52 -0700 | [diff] [blame] | 3991 | .fc_nlinfo.nl_net = net, |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 3992 | |
Maciej Żenczykowski | 8823a3a | 2018-09-29 23:44:52 -0700 | [diff] [blame] | 3993 | .fc_dst = rtmsg->rtmsg_dst, |
| 3994 | .fc_src = rtmsg->rtmsg_src, |
| 3995 | .fc_gateway = rtmsg->rtmsg_gateway, |
| 3996 | }; |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 3997 | } |
| 3998 | |
Daniel Lezcano | 5578689 | 2008-03-04 13:47:47 -0800 | [diff] [blame] | 3999 | 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] | 4000 | { |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4001 | struct fib6_config cfg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4002 | struct in6_rtmsg rtmsg; |
| 4003 | int err; |
| 4004 | |
Ian Morris | 67ba415 | 2014-08-24 21:53:10 +0100 | [diff] [blame] | 4005 | switch (cmd) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4006 | case SIOCADDRT: /* Add a route */ |
| 4007 | case SIOCDELRT: /* Delete a route */ |
Eric W. Biederman | af31f41 | 2012-11-16 03:03:06 +0000 | [diff] [blame] | 4008 | if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4009 | return -EPERM; |
| 4010 | err = copy_from_user(&rtmsg, arg, |
| 4011 | sizeof(struct in6_rtmsg)); |
| 4012 | if (err) |
| 4013 | return -EFAULT; |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4014 | |
Daniel Lezcano | 5578689 | 2008-03-04 13:47:47 -0800 | [diff] [blame] | 4015 | rtmsg_to_fib6_config(net, &rtmsg, &cfg); |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4016 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4017 | rtnl_lock(); |
| 4018 | switch (cmd) { |
| 4019 | case SIOCADDRT: |
David Ahern | acb54e3 | 2018-04-17 17:33:22 -0700 | [diff] [blame] | 4020 | err = ip6_route_add(&cfg, GFP_KERNEL, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4021 | break; |
| 4022 | case SIOCDELRT: |
David Ahern | 333c430 | 2017-05-21 10:12:04 -0600 | [diff] [blame] | 4023 | err = ip6_route_del(&cfg, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4024 | break; |
| 4025 | default: |
| 4026 | err = -EINVAL; |
| 4027 | } |
| 4028 | rtnl_unlock(); |
| 4029 | |
| 4030 | return err; |
Stephen Hemminger | 3ff50b7 | 2007-04-20 17:09:22 -0700 | [diff] [blame] | 4031 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4032 | |
| 4033 | return -EINVAL; |
| 4034 | } |
| 4035 | |
| 4036 | /* |
| 4037 | * Drop the packet on the floor |
| 4038 | */ |
| 4039 | |
Brian Haley | d5fdd6b | 2009-06-23 04:31:07 -0700 | [diff] [blame] | 4040 | 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] | 4041 | { |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 4042 | struct dst_entry *dst = skb_dst(skb); |
Stephen Suryaputra | 1d3fd8a | 2019-04-27 09:14:33 -0400 | [diff] [blame] | 4043 | struct net *net = dev_net(dst->dev); |
| 4044 | struct inet6_dev *idev; |
| 4045 | int type; |
| 4046 | |
| 4047 | if (netif_is_l3_master(skb->dev) && |
| 4048 | dst->dev == net->loopback_dev) |
| 4049 | idev = __in6_dev_get_safely(dev_get_by_index_rcu(net, IP6CB(skb)->iif)); |
| 4050 | else |
| 4051 | idev = ip6_dst_idev(dst); |
| 4052 | |
YOSHIFUJI Hideaki | 612f09e | 2007-04-13 16:18:02 -0700 | [diff] [blame] | 4053 | switch (ipstats_mib_noroutes) { |
| 4054 | case IPSTATS_MIB_INNOROUTES: |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 4055 | type = ipv6_addr_type(&ipv6_hdr(skb)->daddr); |
Ulrich Weber | 45bb006 | 2010-02-25 23:28:58 +0000 | [diff] [blame] | 4056 | if (type == IPV6_ADDR_ANY) { |
Stephen Suryaputra | 1d3fd8a | 2019-04-27 09:14:33 -0400 | [diff] [blame] | 4057 | IP6_INC_STATS(net, idev, IPSTATS_MIB_INADDRERRORS); |
YOSHIFUJI Hideaki | 612f09e | 2007-04-13 16:18:02 -0700 | [diff] [blame] | 4058 | break; |
| 4059 | } |
| 4060 | /* FALLTHROUGH */ |
| 4061 | case IPSTATS_MIB_OUTNOROUTES: |
Stephen Suryaputra | 1d3fd8a | 2019-04-27 09:14:33 -0400 | [diff] [blame] | 4062 | IP6_INC_STATS(net, idev, ipstats_mib_noroutes); |
YOSHIFUJI Hideaki | 612f09e | 2007-04-13 16:18:02 -0700 | [diff] [blame] | 4063 | break; |
| 4064 | } |
Stephen Suryaputra | 1d3fd8a | 2019-04-27 09:14:33 -0400 | [diff] [blame] | 4065 | |
| 4066 | /* Start over by dropping the dst for l3mdev case */ |
| 4067 | if (netif_is_l3_master(skb->dev)) |
| 4068 | skb_dst_drop(skb); |
| 4069 | |
Alexey Dobriyan | 3ffe533 | 2010-02-18 08:25:24 +0000 | [diff] [blame] | 4070 | icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4071 | kfree_skb(skb); |
| 4072 | return 0; |
| 4073 | } |
| 4074 | |
Thomas Graf | 9ce8ade | 2006-10-18 20:46:54 -0700 | [diff] [blame] | 4075 | static int ip6_pkt_discard(struct sk_buff *skb) |
| 4076 | { |
YOSHIFUJI Hideaki | 612f09e | 2007-04-13 16:18:02 -0700 | [diff] [blame] | 4077 | return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES); |
Thomas Graf | 9ce8ade | 2006-10-18 20:46:54 -0700 | [diff] [blame] | 4078 | } |
| 4079 | |
Eric W. Biederman | ede2059 | 2015-10-07 16:48:47 -0500 | [diff] [blame] | 4080 | 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] | 4081 | { |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 4082 | skb->dev = skb_dst(skb)->dev; |
YOSHIFUJI Hideaki | 612f09e | 2007-04-13 16:18:02 -0700 | [diff] [blame] | 4083 | return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4084 | } |
| 4085 | |
Thomas Graf | 9ce8ade | 2006-10-18 20:46:54 -0700 | [diff] [blame] | 4086 | static int ip6_pkt_prohibit(struct sk_buff *skb) |
| 4087 | { |
YOSHIFUJI Hideaki | 612f09e | 2007-04-13 16:18:02 -0700 | [diff] [blame] | 4088 | return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES); |
Thomas Graf | 9ce8ade | 2006-10-18 20:46:54 -0700 | [diff] [blame] | 4089 | } |
| 4090 | |
Eric W. Biederman | ede2059 | 2015-10-07 16:48:47 -0500 | [diff] [blame] | 4091 | 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] | 4092 | { |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 4093 | skb->dev = skb_dst(skb)->dev; |
YOSHIFUJI Hideaki | 612f09e | 2007-04-13 16:18:02 -0700 | [diff] [blame] | 4094 | return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES); |
Thomas Graf | 9ce8ade | 2006-10-18 20:46:54 -0700 | [diff] [blame] | 4095 | } |
| 4096 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4097 | /* |
| 4098 | * Allocate a dst for local (unicast / anycast) address. |
| 4099 | */ |
| 4100 | |
David Ahern | 360a988 | 2018-04-18 15:39:00 -0700 | [diff] [blame] | 4101 | struct fib6_info *addrconf_f6i_alloc(struct net *net, |
| 4102 | struct inet6_dev *idev, |
| 4103 | const struct in6_addr *addr, |
| 4104 | bool anycast, gfp_t gfp_flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4105 | { |
David Ahern | c7a1ce3 | 2019-03-21 05:21:35 -0700 | [diff] [blame] | 4106 | struct fib6_config cfg = { |
| 4107 | .fc_table = l3mdev_fib_table(idev->dev) ? : RT6_TABLE_LOCAL, |
| 4108 | .fc_ifindex = idev->dev->ifindex, |
| 4109 | .fc_flags = RTF_UP | RTF_ADDRCONF | RTF_NONEXTHOP, |
| 4110 | .fc_dst = *addr, |
| 4111 | .fc_dst_len = 128, |
| 4112 | .fc_protocol = RTPROT_KERNEL, |
| 4113 | .fc_nlinfo.nl_net = net, |
| 4114 | .fc_ignore_dev_down = true, |
| 4115 | }; |
David Ahern | 5f02ce24 | 2016-09-10 12:09:54 -0700 | [diff] [blame] | 4116 | |
David Ahern | e8478e8 | 2018-04-17 17:33:13 -0700 | [diff] [blame] | 4117 | if (anycast) { |
David Ahern | c7a1ce3 | 2019-03-21 05:21:35 -0700 | [diff] [blame] | 4118 | cfg.fc_type = RTN_ANYCAST; |
| 4119 | cfg.fc_flags |= RTF_ANYCAST; |
David Ahern | e8478e8 | 2018-04-17 17:33:13 -0700 | [diff] [blame] | 4120 | } else { |
David Ahern | c7a1ce3 | 2019-03-21 05:21:35 -0700 | [diff] [blame] | 4121 | cfg.fc_type = RTN_LOCAL; |
| 4122 | cfg.fc_flags |= RTF_LOCAL; |
David Ahern | e8478e8 | 2018-04-17 17:33:13 -0700 | [diff] [blame] | 4123 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4124 | |
David Ahern | c7a1ce3 | 2019-03-21 05:21:35 -0700 | [diff] [blame] | 4125 | return ip6_route_info_create(&cfg, gfp_flags, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4126 | } |
| 4127 | |
Daniel Walter | c3968a8 | 2011-04-13 21:10:57 +0000 | [diff] [blame] | 4128 | /* remove deleted ip from prefsrc entries */ |
| 4129 | struct arg_dev_net_ip { |
| 4130 | struct net_device *dev; |
| 4131 | struct net *net; |
| 4132 | struct in6_addr *addr; |
| 4133 | }; |
| 4134 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4135 | static int fib6_remove_prefsrc(struct fib6_info *rt, void *arg) |
Daniel Walter | c3968a8 | 2011-04-13 21:10:57 +0000 | [diff] [blame] | 4136 | { |
| 4137 | struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev; |
| 4138 | struct net *net = ((struct arg_dev_net_ip *)arg)->net; |
| 4139 | struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr; |
| 4140 | |
David Ahern | f88d8ea | 2019-06-03 20:19:52 -0700 | [diff] [blame] | 4141 | if (!rt->nh && |
| 4142 | ((void *)rt->fib6_nh->fib_nh_dev == dev || !dev) && |
David Ahern | 421842e | 2018-04-17 17:33:18 -0700 | [diff] [blame] | 4143 | rt != net->ipv6.fib6_null_entry && |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 4144 | ipv6_addr_equal(addr, &rt->fib6_prefsrc.addr)) { |
Wei Wang | 60006a4 | 2017-10-06 12:05:58 -0700 | [diff] [blame] | 4145 | spin_lock_bh(&rt6_exception_lock); |
Daniel Walter | c3968a8 | 2011-04-13 21:10:57 +0000 | [diff] [blame] | 4146 | /* remove prefsrc entry */ |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 4147 | rt->fib6_prefsrc.plen = 0; |
Wei Wang | 60006a4 | 2017-10-06 12:05:58 -0700 | [diff] [blame] | 4148 | spin_unlock_bh(&rt6_exception_lock); |
Daniel Walter | c3968a8 | 2011-04-13 21:10:57 +0000 | [diff] [blame] | 4149 | } |
| 4150 | return 0; |
| 4151 | } |
| 4152 | |
| 4153 | void rt6_remove_prefsrc(struct inet6_ifaddr *ifp) |
| 4154 | { |
| 4155 | struct net *net = dev_net(ifp->idev->dev); |
| 4156 | struct arg_dev_net_ip adni = { |
| 4157 | .dev = ifp->idev->dev, |
| 4158 | .net = net, |
| 4159 | .addr = &ifp->addr, |
| 4160 | }; |
Li RongQing | 0c3584d | 2013-12-27 16:32:38 +0800 | [diff] [blame] | 4161 | fib6_clean_all(net, fib6_remove_prefsrc, &adni); |
Daniel Walter | c3968a8 | 2011-04-13 21:10:57 +0000 | [diff] [blame] | 4162 | } |
| 4163 | |
David Ahern | 2b2450c | 2019-03-27 20:53:52 -0700 | [diff] [blame] | 4164 | #define RTF_RA_ROUTER (RTF_ADDRCONF | RTF_DEFAULT) |
Duan Jiong | be7a010 | 2014-05-15 15:56:14 +0800 | [diff] [blame] | 4165 | |
| 4166 | /* Remove routers and update dst entries when gateway turn into host. */ |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4167 | static int fib6_clean_tohost(struct fib6_info *rt, void *arg) |
Duan Jiong | be7a010 | 2014-05-15 15:56:14 +0800 | [diff] [blame] | 4168 | { |
| 4169 | struct in6_addr *gateway = (struct in6_addr *)arg; |
David Ahern | f88d8ea | 2019-06-03 20:19:52 -0700 | [diff] [blame] | 4170 | struct fib6_nh *nh; |
Duan Jiong | be7a010 | 2014-05-15 15:56:14 +0800 | [diff] [blame] | 4171 | |
David Ahern | f88d8ea | 2019-06-03 20:19:52 -0700 | [diff] [blame] | 4172 | /* RA routes do not use nexthops */ |
| 4173 | if (rt->nh) |
| 4174 | return 0; |
| 4175 | |
| 4176 | nh = rt->fib6_nh; |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 4177 | if (((rt->fib6_flags & RTF_RA_ROUTER) == RTF_RA_ROUTER) && |
David Ahern | cc5c073 | 2019-05-22 20:27:58 -0700 | [diff] [blame] | 4178 | 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] | 4179 | return -1; |
Wei Wang | b16cb45 | 2017-10-06 12:06:00 -0700 | [diff] [blame] | 4180 | |
| 4181 | /* Further clean up cached routes in exception table. |
| 4182 | * This is needed because cached route may have a different |
| 4183 | * gateway than its 'parent' in the case of an ip redirect. |
| 4184 | */ |
David Ahern | cc5c073 | 2019-05-22 20:27:58 -0700 | [diff] [blame] | 4185 | fib6_nh_exceptions_clean_tohost(nh, gateway); |
Wei Wang | b16cb45 | 2017-10-06 12:06:00 -0700 | [diff] [blame] | 4186 | |
Duan Jiong | be7a010 | 2014-05-15 15:56:14 +0800 | [diff] [blame] | 4187 | return 0; |
| 4188 | } |
| 4189 | |
| 4190 | void rt6_clean_tohost(struct net *net, struct in6_addr *gateway) |
| 4191 | { |
| 4192 | fib6_clean_all(net, fib6_clean_tohost, gateway); |
| 4193 | } |
| 4194 | |
Ido Schimmel | 2127d95 | 2018-01-07 12:45:03 +0200 | [diff] [blame] | 4195 | struct arg_netdev_event { |
| 4196 | const struct net_device *dev; |
Ido Schimmel | 4c981e2 | 2018-01-07 12:45:04 +0200 | [diff] [blame] | 4197 | union { |
David Ahern | ecc5663 | 2019-04-23 08:48:09 -0700 | [diff] [blame] | 4198 | unsigned char nh_flags; |
Ido Schimmel | 4c981e2 | 2018-01-07 12:45:04 +0200 | [diff] [blame] | 4199 | unsigned long event; |
| 4200 | }; |
Ido Schimmel | 2127d95 | 2018-01-07 12:45:03 +0200 | [diff] [blame] | 4201 | }; |
| 4202 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4203 | 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] | 4204 | { |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4205 | struct fib6_info *iter; |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 4206 | struct fib6_node *fn; |
| 4207 | |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 4208 | fn = rcu_dereference_protected(rt->fib6_node, |
| 4209 | lockdep_is_held(&rt->fib6_table->tb6_lock)); |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 4210 | iter = rcu_dereference_protected(fn->leaf, |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 4211 | lockdep_is_held(&rt->fib6_table->tb6_lock)); |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 4212 | while (iter) { |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 4213 | if (iter->fib6_metric == rt->fib6_metric && |
David Ahern | 33bd5ac | 2018-07-03 14:36:21 -0700 | [diff] [blame] | 4214 | rt6_qualify_for_ecmp(iter)) |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 4215 | return iter; |
David Ahern | 8fb11a9 | 2018-05-04 13:54:24 -0700 | [diff] [blame] | 4216 | iter = rcu_dereference_protected(iter->fib6_next, |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 4217 | lockdep_is_held(&rt->fib6_table->tb6_lock)); |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 4218 | } |
| 4219 | |
| 4220 | return NULL; |
| 4221 | } |
| 4222 | |
David Ahern | f88d8ea | 2019-06-03 20:19:52 -0700 | [diff] [blame] | 4223 | /* only called for fib entries with builtin fib6_nh */ |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4224 | static bool rt6_is_dead(const struct fib6_info *rt) |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 4225 | { |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 4226 | if (rt->fib6_nh->fib_nh_flags & RTNH_F_DEAD || |
| 4227 | (rt->fib6_nh->fib_nh_flags & RTNH_F_LINKDOWN && |
| 4228 | ip6_ignore_linkdown(rt->fib6_nh->fib_nh_dev))) |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 4229 | return true; |
| 4230 | |
| 4231 | return false; |
| 4232 | } |
| 4233 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4234 | static int rt6_multipath_total_weight(const struct fib6_info *rt) |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 4235 | { |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4236 | struct fib6_info *iter; |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 4237 | int total = 0; |
| 4238 | |
| 4239 | if (!rt6_is_dead(rt)) |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 4240 | total += rt->fib6_nh->fib_nh_weight; |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 4241 | |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 4242 | list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) { |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 4243 | if (!rt6_is_dead(iter)) |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 4244 | total += iter->fib6_nh->fib_nh_weight; |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 4245 | } |
| 4246 | |
| 4247 | return total; |
| 4248 | } |
| 4249 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4250 | 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] | 4251 | { |
| 4252 | int upper_bound = -1; |
| 4253 | |
| 4254 | if (!rt6_is_dead(rt)) { |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 4255 | *weight += rt->fib6_nh->fib_nh_weight; |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 4256 | upper_bound = DIV_ROUND_CLOSEST_ULL((u64) (*weight) << 31, |
| 4257 | total) - 1; |
| 4258 | } |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 4259 | atomic_set(&rt->fib6_nh->fib_nh_upper_bound, upper_bound); |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 4260 | } |
| 4261 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4262 | 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] | 4263 | { |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4264 | struct fib6_info *iter; |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 4265 | int weight = 0; |
| 4266 | |
| 4267 | rt6_upper_bound_set(rt, &weight, total); |
| 4268 | |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 4269 | list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 4270 | rt6_upper_bound_set(iter, &weight, total); |
| 4271 | } |
| 4272 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4273 | void rt6_multipath_rebalance(struct fib6_info *rt) |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 4274 | { |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4275 | struct fib6_info *first; |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 4276 | int total; |
| 4277 | |
| 4278 | /* In case the entire multipath route was marked for flushing, |
| 4279 | * then there is no need to rebalance upon the removal of every |
| 4280 | * sibling route. |
| 4281 | */ |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 4282 | if (!rt->fib6_nsiblings || rt->should_flush) |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 4283 | return; |
| 4284 | |
| 4285 | /* During lookup routes are evaluated in order, so we need to |
| 4286 | * make sure upper bounds are assigned from the first sibling |
| 4287 | * onwards. |
| 4288 | */ |
| 4289 | first = rt6_multipath_first_sibling(rt); |
| 4290 | if (WARN_ON_ONCE(!first)) |
| 4291 | return; |
| 4292 | |
| 4293 | total = rt6_multipath_total_weight(first); |
| 4294 | rt6_multipath_upper_bound_set(first, total); |
| 4295 | } |
| 4296 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4297 | static int fib6_ifup(struct fib6_info *rt, void *p_arg) |
Ido Schimmel | 2127d95 | 2018-01-07 12:45:03 +0200 | [diff] [blame] | 4298 | { |
| 4299 | const struct arg_netdev_event *arg = p_arg; |
David Ahern | 7aef685 | 2018-04-17 17:33:10 -0700 | [diff] [blame] | 4300 | struct net *net = dev_net(arg->dev); |
Ido Schimmel | 2127d95 | 2018-01-07 12:45:03 +0200 | [diff] [blame] | 4301 | |
David Ahern | f88d8ea | 2019-06-03 20:19:52 -0700 | [diff] [blame] | 4302 | if (rt != net->ipv6.fib6_null_entry && !rt->nh && |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 4303 | rt->fib6_nh->fib_nh_dev == arg->dev) { |
| 4304 | rt->fib6_nh->fib_nh_flags &= ~arg->nh_flags; |
David Ahern | 7aef685 | 2018-04-17 17:33:10 -0700 | [diff] [blame] | 4305 | fib6_update_sernum_upto_root(net, rt); |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 4306 | rt6_multipath_rebalance(rt); |
Ido Schimmel | 1de178e | 2018-01-07 12:45:15 +0200 | [diff] [blame] | 4307 | } |
Ido Schimmel | 2127d95 | 2018-01-07 12:45:03 +0200 | [diff] [blame] | 4308 | |
| 4309 | return 0; |
| 4310 | } |
| 4311 | |
David Ahern | ecc5663 | 2019-04-23 08:48:09 -0700 | [diff] [blame] | 4312 | void rt6_sync_up(struct net_device *dev, unsigned char nh_flags) |
Ido Schimmel | 2127d95 | 2018-01-07 12:45:03 +0200 | [diff] [blame] | 4313 | { |
| 4314 | struct arg_netdev_event arg = { |
| 4315 | .dev = dev, |
Ido Schimmel | 6802f3a | 2018-01-12 22:07:36 +0200 | [diff] [blame] | 4316 | { |
| 4317 | .nh_flags = nh_flags, |
| 4318 | }, |
Ido Schimmel | 2127d95 | 2018-01-07 12:45:03 +0200 | [diff] [blame] | 4319 | }; |
| 4320 | |
| 4321 | if (nh_flags & RTNH_F_DEAD && netif_carrier_ok(dev)) |
| 4322 | arg.nh_flags |= RTNH_F_LINKDOWN; |
| 4323 | |
| 4324 | fib6_clean_all(dev_net(dev), fib6_ifup, &arg); |
| 4325 | } |
| 4326 | |
David Ahern | f88d8ea | 2019-06-03 20:19:52 -0700 | [diff] [blame] | 4327 | /* only called for fib entries with inline fib6_nh */ |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4328 | static bool rt6_multipath_uses_dev(const struct fib6_info *rt, |
Ido Schimmel | 1de178e | 2018-01-07 12:45:15 +0200 | [diff] [blame] | 4329 | const struct net_device *dev) |
| 4330 | { |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4331 | struct fib6_info *iter; |
Ido Schimmel | 1de178e | 2018-01-07 12:45:15 +0200 | [diff] [blame] | 4332 | |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 4333 | if (rt->fib6_nh->fib_nh_dev == dev) |
Ido Schimmel | 1de178e | 2018-01-07 12:45:15 +0200 | [diff] [blame] | 4334 | return true; |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 4335 | list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 4336 | if (iter->fib6_nh->fib_nh_dev == dev) |
Ido Schimmel | 1de178e | 2018-01-07 12:45:15 +0200 | [diff] [blame] | 4337 | return true; |
| 4338 | |
| 4339 | return false; |
| 4340 | } |
| 4341 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4342 | static void rt6_multipath_flush(struct fib6_info *rt) |
Ido Schimmel | 1de178e | 2018-01-07 12:45:15 +0200 | [diff] [blame] | 4343 | { |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4344 | struct fib6_info *iter; |
Ido Schimmel | 1de178e | 2018-01-07 12:45:15 +0200 | [diff] [blame] | 4345 | |
| 4346 | rt->should_flush = 1; |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 4347 | list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) |
Ido Schimmel | 1de178e | 2018-01-07 12:45:15 +0200 | [diff] [blame] | 4348 | iter->should_flush = 1; |
| 4349 | } |
| 4350 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4351 | static unsigned int rt6_multipath_dead_count(const struct fib6_info *rt, |
Ido Schimmel | 1de178e | 2018-01-07 12:45:15 +0200 | [diff] [blame] | 4352 | const struct net_device *down_dev) |
| 4353 | { |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4354 | struct fib6_info *iter; |
Ido Schimmel | 1de178e | 2018-01-07 12:45:15 +0200 | [diff] [blame] | 4355 | unsigned int dead = 0; |
| 4356 | |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 4357 | if (rt->fib6_nh->fib_nh_dev == down_dev || |
| 4358 | rt->fib6_nh->fib_nh_flags & RTNH_F_DEAD) |
Ido Schimmel | 1de178e | 2018-01-07 12:45:15 +0200 | [diff] [blame] | 4359 | dead++; |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 4360 | list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 4361 | if (iter->fib6_nh->fib_nh_dev == down_dev || |
| 4362 | iter->fib6_nh->fib_nh_flags & RTNH_F_DEAD) |
Ido Schimmel | 1de178e | 2018-01-07 12:45:15 +0200 | [diff] [blame] | 4363 | dead++; |
| 4364 | |
| 4365 | return dead; |
| 4366 | } |
| 4367 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4368 | static void rt6_multipath_nh_flags_set(struct fib6_info *rt, |
Ido Schimmel | 1de178e | 2018-01-07 12:45:15 +0200 | [diff] [blame] | 4369 | const struct net_device *dev, |
David Ahern | ecc5663 | 2019-04-23 08:48:09 -0700 | [diff] [blame] | 4370 | unsigned char nh_flags) |
Ido Schimmel | 1de178e | 2018-01-07 12:45:15 +0200 | [diff] [blame] | 4371 | { |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4372 | struct fib6_info *iter; |
Ido Schimmel | 1de178e | 2018-01-07 12:45:15 +0200 | [diff] [blame] | 4373 | |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 4374 | if (rt->fib6_nh->fib_nh_dev == dev) |
| 4375 | rt->fib6_nh->fib_nh_flags |= nh_flags; |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 4376 | list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 4377 | if (iter->fib6_nh->fib_nh_dev == dev) |
| 4378 | iter->fib6_nh->fib_nh_flags |= nh_flags; |
Ido Schimmel | 1de178e | 2018-01-07 12:45:15 +0200 | [diff] [blame] | 4379 | } |
| 4380 | |
David Ahern | a1a22c1 | 2017-01-18 07:40:36 -0800 | [diff] [blame] | 4381 | /* called with write lock held for table with rt */ |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4382 | static int fib6_ifdown(struct fib6_info *rt, void *p_arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4383 | { |
Ido Schimmel | 4c981e2 | 2018-01-07 12:45:04 +0200 | [diff] [blame] | 4384 | const struct arg_netdev_event *arg = p_arg; |
| 4385 | const struct net_device *dev = arg->dev; |
David Ahern | 7aef685 | 2018-04-17 17:33:10 -0700 | [diff] [blame] | 4386 | struct net *net = dev_net(dev); |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 4387 | |
David Ahern | f88d8ea | 2019-06-03 20:19:52 -0700 | [diff] [blame] | 4388 | if (rt == net->ipv6.fib6_null_entry || rt->nh) |
Ido Schimmel | 27c6fa7 | 2018-01-07 12:45:05 +0200 | [diff] [blame] | 4389 | return 0; |
| 4390 | |
| 4391 | switch (arg->event) { |
| 4392 | case NETDEV_UNREGISTER: |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 4393 | return rt->fib6_nh->fib_nh_dev == dev ? -1 : 0; |
Ido Schimmel | 27c6fa7 | 2018-01-07 12:45:05 +0200 | [diff] [blame] | 4394 | case NETDEV_DOWN: |
Ido Schimmel | 1de178e | 2018-01-07 12:45:15 +0200 | [diff] [blame] | 4395 | if (rt->should_flush) |
Ido Schimmel | 27c6fa7 | 2018-01-07 12:45:05 +0200 | [diff] [blame] | 4396 | return -1; |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 4397 | if (!rt->fib6_nsiblings) |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 4398 | return rt->fib6_nh->fib_nh_dev == dev ? -1 : 0; |
Ido Schimmel | 1de178e | 2018-01-07 12:45:15 +0200 | [diff] [blame] | 4399 | if (rt6_multipath_uses_dev(rt, dev)) { |
| 4400 | unsigned int count; |
| 4401 | |
| 4402 | count = rt6_multipath_dead_count(rt, dev); |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 4403 | if (rt->fib6_nsiblings + 1 == count) { |
Ido Schimmel | 1de178e | 2018-01-07 12:45:15 +0200 | [diff] [blame] | 4404 | rt6_multipath_flush(rt); |
| 4405 | return -1; |
| 4406 | } |
| 4407 | rt6_multipath_nh_flags_set(rt, dev, RTNH_F_DEAD | |
| 4408 | RTNH_F_LINKDOWN); |
David Ahern | 7aef685 | 2018-04-17 17:33:10 -0700 | [diff] [blame] | 4409 | fib6_update_sernum(net, rt); |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 4410 | rt6_multipath_rebalance(rt); |
Ido Schimmel | 1de178e | 2018-01-07 12:45:15 +0200 | [diff] [blame] | 4411 | } |
| 4412 | return -2; |
Ido Schimmel | 27c6fa7 | 2018-01-07 12:45:05 +0200 | [diff] [blame] | 4413 | case NETDEV_CHANGE: |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 4414 | if (rt->fib6_nh->fib_nh_dev != dev || |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 4415 | rt->fib6_flags & (RTF_LOCAL | RTF_ANYCAST)) |
Ido Schimmel | 27c6fa7 | 2018-01-07 12:45:05 +0200 | [diff] [blame] | 4416 | break; |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 4417 | rt->fib6_nh->fib_nh_flags |= RTNH_F_LINKDOWN; |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 4418 | rt6_multipath_rebalance(rt); |
Ido Schimmel | 27c6fa7 | 2018-01-07 12:45:05 +0200 | [diff] [blame] | 4419 | break; |
Ido Schimmel | 2b24136 | 2018-01-07 12:45:02 +0200 | [diff] [blame] | 4420 | } |
David S. Miller | c159d30 | 2011-12-26 15:24:36 -0500 | [diff] [blame] | 4421 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4422 | return 0; |
| 4423 | } |
| 4424 | |
Ido Schimmel | 27c6fa7 | 2018-01-07 12:45:05 +0200 | [diff] [blame] | 4425 | void rt6_sync_down_dev(struct net_device *dev, unsigned long event) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4426 | { |
Ido Schimmel | 4c981e2 | 2018-01-07 12:45:04 +0200 | [diff] [blame] | 4427 | struct arg_netdev_event arg = { |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 4428 | .dev = dev, |
Ido Schimmel | 6802f3a | 2018-01-12 22:07:36 +0200 | [diff] [blame] | 4429 | { |
| 4430 | .event = event, |
| 4431 | }, |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 4432 | }; |
David Ahern | 7c6bb7d | 2018-10-11 20:17:21 -0700 | [diff] [blame] | 4433 | struct net *net = dev_net(dev); |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 4434 | |
David Ahern | 7c6bb7d | 2018-10-11 20:17:21 -0700 | [diff] [blame] | 4435 | if (net->ipv6.sysctl.skip_notify_on_dev_down) |
| 4436 | fib6_clean_all_skip_notify(net, fib6_ifdown, &arg); |
| 4437 | else |
| 4438 | fib6_clean_all(net, fib6_ifdown, &arg); |
Ido Schimmel | 4c981e2 | 2018-01-07 12:45:04 +0200 | [diff] [blame] | 4439 | } |
| 4440 | |
| 4441 | void rt6_disable_ip(struct net_device *dev, unsigned long event) |
| 4442 | { |
| 4443 | rt6_sync_down_dev(dev, event); |
| 4444 | rt6_uncached_list_flush_dev(dev_net(dev), dev); |
| 4445 | neigh_ifdown(&nd_tbl, dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4446 | } |
| 4447 | |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 4448 | struct rt6_mtu_change_arg { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4449 | struct net_device *dev; |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 4450 | unsigned int mtu; |
David Ahern | c0b220c | 2019-05-22 20:27:57 -0700 | [diff] [blame] | 4451 | struct fib6_info *f6i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4452 | }; |
| 4453 | |
David Ahern | cc5c073 | 2019-05-22 20:27:58 -0700 | [diff] [blame] | 4454 | static int fib6_nh_mtu_change(struct fib6_nh *nh, void *_arg) |
David Ahern | c0b220c | 2019-05-22 20:27:57 -0700 | [diff] [blame] | 4455 | { |
| 4456 | struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *)_arg; |
David Ahern | cc5c073 | 2019-05-22 20:27:58 -0700 | [diff] [blame] | 4457 | struct fib6_info *f6i = arg->f6i; |
David Ahern | c0b220c | 2019-05-22 20:27:57 -0700 | [diff] [blame] | 4458 | |
| 4459 | /* For administrative MTU increase, there is no way to discover |
| 4460 | * IPv6 PMTU increase, so PMTU increase should be updated here. |
| 4461 | * Since RFC 1981 doesn't include administrative MTU increase |
| 4462 | * update PMTU increase is a MUST. (i.e. jumbo frame) |
| 4463 | */ |
| 4464 | if (nh->fib_nh_dev == arg->dev) { |
| 4465 | struct inet6_dev *idev = __in6_dev_get(arg->dev); |
| 4466 | u32 mtu = f6i->fib6_pmtu; |
| 4467 | |
| 4468 | if (mtu >= arg->mtu || |
| 4469 | (mtu < arg->mtu && mtu == idev->cnf.mtu6)) |
| 4470 | fib6_metric_set(f6i, RTAX_MTU, arg->mtu); |
| 4471 | |
| 4472 | spin_lock_bh(&rt6_exception_lock); |
David Ahern | cc5c073 | 2019-05-22 20:27:58 -0700 | [diff] [blame] | 4473 | rt6_exceptions_update_pmtu(idev, nh, arg->mtu); |
David Ahern | c0b220c | 2019-05-22 20:27:57 -0700 | [diff] [blame] | 4474 | spin_unlock_bh(&rt6_exception_lock); |
| 4475 | } |
| 4476 | |
| 4477 | return 0; |
| 4478 | } |
| 4479 | |
| 4480 | 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] | 4481 | { |
| 4482 | struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg; |
| 4483 | struct inet6_dev *idev; |
| 4484 | |
| 4485 | /* In IPv6 pmtu discovery is not optional, |
| 4486 | so that RTAX_MTU lock cannot disable it. |
| 4487 | We still use this lock to block changes |
| 4488 | caused by addrconf/ndisc. |
| 4489 | */ |
| 4490 | |
| 4491 | idev = __in6_dev_get(arg->dev); |
David S. Miller | 3830847 | 2011-12-03 18:02:47 -0500 | [diff] [blame] | 4492 | if (!idev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4493 | return 0; |
| 4494 | |
David Ahern | c0b220c | 2019-05-22 20:27:57 -0700 | [diff] [blame] | 4495 | if (fib6_metric_locked(f6i, RTAX_MTU)) |
| 4496 | return 0; |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 4497 | |
David Ahern | c0b220c | 2019-05-22 20:27:57 -0700 | [diff] [blame] | 4498 | arg->f6i = f6i; |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 4499 | return fib6_nh_mtu_change(f6i->fib6_nh, arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4500 | } |
| 4501 | |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 4502 | void rt6_mtu_change(struct net_device *dev, unsigned int mtu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4503 | { |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 4504 | struct rt6_mtu_change_arg arg = { |
| 4505 | .dev = dev, |
| 4506 | .mtu = mtu, |
| 4507 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4508 | |
Li RongQing | 0c3584d | 2013-12-27 16:32:38 +0800 | [diff] [blame] | 4509 | fib6_clean_all(dev_net(dev), rt6_mtu_change_route, &arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4510 | } |
| 4511 | |
Patrick McHardy | ef7c79e | 2007-06-05 12:38:30 -0700 | [diff] [blame] | 4512 | static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = { |
David Ahern | 7542565 | 2019-05-22 12:07:43 -0700 | [diff] [blame] | 4513 | [RTA_UNSPEC] = { .strict_start_type = RTA_DPORT + 1 }, |
Thomas Graf | 5176f91 | 2006-08-26 20:13:18 -0700 | [diff] [blame] | 4514 | [RTA_GATEWAY] = { .len = sizeof(struct in6_addr) }, |
Eric Dumazet | aa8f877 | 2018-04-22 18:29:23 -0700 | [diff] [blame] | 4515 | [RTA_PREFSRC] = { .len = sizeof(struct in6_addr) }, |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4516 | [RTA_OIF] = { .type = NLA_U32 }, |
Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 4517 | [RTA_IIF] = { .type = NLA_U32 }, |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4518 | [RTA_PRIORITY] = { .type = NLA_U32 }, |
| 4519 | [RTA_METRICS] = { .type = NLA_NESTED }, |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 4520 | [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) }, |
Lubomir Rintel | c78ba6d | 2015-03-11 15:39:21 +0100 | [diff] [blame] | 4521 | [RTA_PREF] = { .type = NLA_U8 }, |
Roopa Prabhu | 19e42e4 | 2015-07-21 10:43:48 +0200 | [diff] [blame] | 4522 | [RTA_ENCAP_TYPE] = { .type = NLA_U16 }, |
| 4523 | [RTA_ENCAP] = { .type = NLA_NESTED }, |
Xin Long | 32bc201 | 2015-12-16 17:50:11 +0800 | [diff] [blame] | 4524 | [RTA_EXPIRES] = { .type = NLA_U32 }, |
Lorenzo Colitti | 622ec2c | 2016-11-04 02:23:42 +0900 | [diff] [blame] | 4525 | [RTA_UID] = { .type = NLA_U32 }, |
Liping Zhang | 3b45a41 | 2017-02-27 20:59:39 +0800 | [diff] [blame] | 4526 | [RTA_MARK] = { .type = NLA_U32 }, |
Eric Dumazet | aa8f877 | 2018-04-22 18:29:23 -0700 | [diff] [blame] | 4527 | [RTA_TABLE] = { .type = NLA_U32 }, |
Roopa Prabhu | eacb938 | 2018-05-22 14:03:28 -0700 | [diff] [blame] | 4528 | [RTA_IP_PROTO] = { .type = NLA_U8 }, |
| 4529 | [RTA_SPORT] = { .type = NLA_U16 }, |
| 4530 | [RTA_DPORT] = { .type = NLA_U16 }, |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4531 | }; |
| 4532 | |
| 4533 | 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] | 4534 | struct fib6_config *cfg, |
| 4535 | struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4536 | { |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4537 | struct rtmsg *rtm; |
| 4538 | struct nlattr *tb[RTA_MAX+1]; |
Lubomir Rintel | c78ba6d | 2015-03-11 15:39:21 +0100 | [diff] [blame] | 4539 | unsigned int pref; |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4540 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4541 | |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 4542 | err = nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX, |
| 4543 | rtm_ipv6_policy, extack); |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4544 | if (err < 0) |
| 4545 | goto errout; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4546 | |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4547 | err = -EINVAL; |
| 4548 | rtm = nlmsg_data(nlh); |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4549 | |
Maciej Żenczykowski | 84db840 | 2018-09-29 23:44:53 -0700 | [diff] [blame] | 4550 | *cfg = (struct fib6_config){ |
| 4551 | .fc_table = rtm->rtm_table, |
| 4552 | .fc_dst_len = rtm->rtm_dst_len, |
| 4553 | .fc_src_len = rtm->rtm_src_len, |
| 4554 | .fc_flags = RTF_UP, |
| 4555 | .fc_protocol = rtm->rtm_protocol, |
| 4556 | .fc_type = rtm->rtm_type, |
| 4557 | |
| 4558 | .fc_nlinfo.portid = NETLINK_CB(skb).portid, |
| 4559 | .fc_nlinfo.nlh = nlh, |
| 4560 | .fc_nlinfo.nl_net = sock_net(skb->sk), |
| 4561 | }; |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4562 | |
Nicolas Dichtel | ef2c7d7 | 2012-09-05 02:12:42 +0000 | [diff] [blame] | 4563 | if (rtm->rtm_type == RTN_UNREACHABLE || |
| 4564 | rtm->rtm_type == RTN_BLACKHOLE || |
Nicolas Dichtel | b4949ab | 2012-09-06 05:53:35 +0000 | [diff] [blame] | 4565 | rtm->rtm_type == RTN_PROHIBIT || |
| 4566 | rtm->rtm_type == RTN_THROW) |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4567 | cfg->fc_flags |= RTF_REJECT; |
| 4568 | |
Maciej Żenczykowski | ab79ad1 | 2010-09-27 00:07:02 +0000 | [diff] [blame] | 4569 | if (rtm->rtm_type == RTN_LOCAL) |
| 4570 | cfg->fc_flags |= RTF_LOCAL; |
| 4571 | |
Martin KaFai Lau | 1f56a01f | 2015-04-28 13:03:03 -0700 | [diff] [blame] | 4572 | if (rtm->rtm_flags & RTM_F_CLONED) |
| 4573 | cfg->fc_flags |= RTF_CACHE; |
| 4574 | |
David Ahern | fc1e64e | 2018-01-25 16:55:09 -0800 | [diff] [blame] | 4575 | cfg->fc_flags |= (rtm->rtm_flags & RTNH_F_ONLINK); |
| 4576 | |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4577 | if (tb[RTA_GATEWAY]) { |
Jiri Benc | 67b61f6 | 2015-03-29 16:59:26 +0200 | [diff] [blame] | 4578 | cfg->fc_gateway = nla_get_in6_addr(tb[RTA_GATEWAY]); |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4579 | cfg->fc_flags |= RTF_GATEWAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4580 | } |
David Ahern | e381854 | 2019-02-26 09:00:03 -0800 | [diff] [blame] | 4581 | if (tb[RTA_VIA]) { |
| 4582 | NL_SET_ERR_MSG(extack, "IPv6 does not support RTA_VIA attribute"); |
| 4583 | goto errout; |
| 4584 | } |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4585 | |
| 4586 | if (tb[RTA_DST]) { |
| 4587 | int plen = (rtm->rtm_dst_len + 7) >> 3; |
| 4588 | |
| 4589 | if (nla_len(tb[RTA_DST]) < plen) |
| 4590 | goto errout; |
| 4591 | |
| 4592 | nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4593 | } |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4594 | |
| 4595 | if (tb[RTA_SRC]) { |
| 4596 | int plen = (rtm->rtm_src_len + 7) >> 3; |
| 4597 | |
| 4598 | if (nla_len(tb[RTA_SRC]) < plen) |
| 4599 | goto errout; |
| 4600 | |
| 4601 | nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4602 | } |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4603 | |
Daniel Walter | c3968a8 | 2011-04-13 21:10:57 +0000 | [diff] [blame] | 4604 | if (tb[RTA_PREFSRC]) |
Jiri Benc | 67b61f6 | 2015-03-29 16:59:26 +0200 | [diff] [blame] | 4605 | cfg->fc_prefsrc = nla_get_in6_addr(tb[RTA_PREFSRC]); |
Daniel Walter | c3968a8 | 2011-04-13 21:10:57 +0000 | [diff] [blame] | 4606 | |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4607 | if (tb[RTA_OIF]) |
| 4608 | cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]); |
| 4609 | |
| 4610 | if (tb[RTA_PRIORITY]) |
| 4611 | cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]); |
| 4612 | |
| 4613 | if (tb[RTA_METRICS]) { |
| 4614 | cfg->fc_mx = nla_data(tb[RTA_METRICS]); |
| 4615 | cfg->fc_mx_len = nla_len(tb[RTA_METRICS]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4616 | } |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4617 | |
| 4618 | if (tb[RTA_TABLE]) |
| 4619 | cfg->fc_table = nla_get_u32(tb[RTA_TABLE]); |
| 4620 | |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 4621 | if (tb[RTA_MULTIPATH]) { |
| 4622 | cfg->fc_mp = nla_data(tb[RTA_MULTIPATH]); |
| 4623 | cfg->fc_mp_len = nla_len(tb[RTA_MULTIPATH]); |
David Ahern | 9ed5959 | 2017-01-17 14:57:36 -0800 | [diff] [blame] | 4624 | |
| 4625 | err = lwtunnel_valid_encap_type_attr(cfg->fc_mp, |
David Ahern | c255bd6 | 2017-05-27 16:19:27 -0600 | [diff] [blame] | 4626 | cfg->fc_mp_len, extack); |
David Ahern | 9ed5959 | 2017-01-17 14:57:36 -0800 | [diff] [blame] | 4627 | if (err < 0) |
| 4628 | goto errout; |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 4629 | } |
| 4630 | |
Lubomir Rintel | c78ba6d | 2015-03-11 15:39:21 +0100 | [diff] [blame] | 4631 | if (tb[RTA_PREF]) { |
| 4632 | pref = nla_get_u8(tb[RTA_PREF]); |
| 4633 | if (pref != ICMPV6_ROUTER_PREF_LOW && |
| 4634 | pref != ICMPV6_ROUTER_PREF_HIGH) |
| 4635 | pref = ICMPV6_ROUTER_PREF_MEDIUM; |
| 4636 | cfg->fc_flags |= RTF_PREF(pref); |
| 4637 | } |
| 4638 | |
Roopa Prabhu | 19e42e4 | 2015-07-21 10:43:48 +0200 | [diff] [blame] | 4639 | if (tb[RTA_ENCAP]) |
| 4640 | cfg->fc_encap = tb[RTA_ENCAP]; |
| 4641 | |
David Ahern | 9ed5959 | 2017-01-17 14:57:36 -0800 | [diff] [blame] | 4642 | if (tb[RTA_ENCAP_TYPE]) { |
Roopa Prabhu | 19e42e4 | 2015-07-21 10:43:48 +0200 | [diff] [blame] | 4643 | cfg->fc_encap_type = nla_get_u16(tb[RTA_ENCAP_TYPE]); |
| 4644 | |
David Ahern | c255bd6 | 2017-05-27 16:19:27 -0600 | [diff] [blame] | 4645 | err = lwtunnel_valid_encap_type(cfg->fc_encap_type, extack); |
David Ahern | 9ed5959 | 2017-01-17 14:57:36 -0800 | [diff] [blame] | 4646 | if (err < 0) |
| 4647 | goto errout; |
| 4648 | } |
| 4649 | |
Xin Long | 32bc201 | 2015-12-16 17:50:11 +0800 | [diff] [blame] | 4650 | if (tb[RTA_EXPIRES]) { |
| 4651 | unsigned long timeout = addrconf_timeout_fixup(nla_get_u32(tb[RTA_EXPIRES]), HZ); |
| 4652 | |
| 4653 | if (addrconf_finite_timeout(timeout)) { |
| 4654 | cfg->fc_expires = jiffies_to_clock_t(timeout * HZ); |
| 4655 | cfg->fc_flags |= RTF_EXPIRES; |
| 4656 | } |
| 4657 | } |
| 4658 | |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4659 | err = 0; |
| 4660 | errout: |
| 4661 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4662 | } |
| 4663 | |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 4664 | struct rt6_nh { |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4665 | struct fib6_info *fib6_info; |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 4666 | struct fib6_config r_cfg; |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 4667 | struct list_head next; |
| 4668 | }; |
| 4669 | |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 4670 | static int ip6_route_info_append(struct net *net, |
| 4671 | struct list_head *rt6_nh_list, |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4672 | struct fib6_info *rt, |
| 4673 | struct fib6_config *r_cfg) |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 4674 | { |
| 4675 | struct rt6_nh *nh; |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 4676 | int err = -EEXIST; |
| 4677 | |
| 4678 | list_for_each_entry(nh, rt6_nh_list, next) { |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4679 | /* check if fib6_info already exists */ |
| 4680 | if (rt6_duplicate_nexthop(nh->fib6_info, rt)) |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 4681 | return err; |
| 4682 | } |
| 4683 | |
| 4684 | nh = kzalloc(sizeof(*nh), GFP_KERNEL); |
| 4685 | if (!nh) |
| 4686 | return -ENOMEM; |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4687 | nh->fib6_info = rt; |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 4688 | memcpy(&nh->r_cfg, r_cfg, sizeof(*r_cfg)); |
| 4689 | list_add_tail(&nh->next, rt6_nh_list); |
| 4690 | |
| 4691 | return 0; |
| 4692 | } |
| 4693 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4694 | static void ip6_route_mpath_notify(struct fib6_info *rt, |
| 4695 | struct fib6_info *rt_last, |
David Ahern | 3b1137f | 2017-02-02 12:37:10 -0800 | [diff] [blame] | 4696 | struct nl_info *info, |
| 4697 | __u16 nlflags) |
| 4698 | { |
| 4699 | /* if this is an APPEND route, then rt points to the first route |
| 4700 | * inserted and rt_last points to last route inserted. Userspace |
| 4701 | * wants a consistent dump of the route which starts at the first |
| 4702 | * nexthop. Since sibling routes are always added at the end of |
| 4703 | * the list, find the first sibling of the last route appended |
| 4704 | */ |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 4705 | if ((nlflags & NLM_F_APPEND) && rt_last && rt_last->fib6_nsiblings) { |
| 4706 | rt = list_first_entry(&rt_last->fib6_siblings, |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4707 | struct fib6_info, |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 4708 | fib6_siblings); |
David Ahern | 3b1137f | 2017-02-02 12:37:10 -0800 | [diff] [blame] | 4709 | } |
| 4710 | |
| 4711 | if (rt) |
| 4712 | inet6_rt_notify(RTM_NEWROUTE, rt, info, nlflags); |
| 4713 | } |
| 4714 | |
David Ahern | 333c430 | 2017-05-21 10:12:04 -0600 | [diff] [blame] | 4715 | static int ip6_route_multipath_add(struct fib6_config *cfg, |
| 4716 | struct netlink_ext_ack *extack) |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 4717 | { |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4718 | struct fib6_info *rt_notif = NULL, *rt_last = NULL; |
David Ahern | 3b1137f | 2017-02-02 12:37:10 -0800 | [diff] [blame] | 4719 | struct nl_info *info = &cfg->fc_nlinfo; |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 4720 | struct fib6_config r_cfg; |
| 4721 | struct rtnexthop *rtnh; |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4722 | struct fib6_info *rt; |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 4723 | struct rt6_nh *err_nh; |
| 4724 | struct rt6_nh *nh, *nh_safe; |
David Ahern | 3b1137f | 2017-02-02 12:37:10 -0800 | [diff] [blame] | 4725 | __u16 nlflags; |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 4726 | int remaining; |
| 4727 | int attrlen; |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 4728 | int err = 1; |
| 4729 | int nhn = 0; |
| 4730 | int replace = (cfg->fc_nlinfo.nlh && |
| 4731 | (cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_REPLACE)); |
| 4732 | LIST_HEAD(rt6_nh_list); |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 4733 | |
David Ahern | 3b1137f | 2017-02-02 12:37:10 -0800 | [diff] [blame] | 4734 | nlflags = replace ? NLM_F_REPLACE : NLM_F_CREATE; |
| 4735 | if (info->nlh && info->nlh->nlmsg_flags & NLM_F_APPEND) |
| 4736 | nlflags |= NLM_F_APPEND; |
| 4737 | |
Michal Kubeček | 35f1b4e | 2015-05-18 20:53:55 +0200 | [diff] [blame] | 4738 | remaining = cfg->fc_mp_len; |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 4739 | rtnh = (struct rtnexthop *)cfg->fc_mp; |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 4740 | |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 4741 | /* Parse a Multipath Entry and build a list (rt6_nh_list) of |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4742 | * fib6_info structs per nexthop |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 4743 | */ |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 4744 | while (rtnh_ok(rtnh, remaining)) { |
| 4745 | memcpy(&r_cfg, cfg, sizeof(*cfg)); |
| 4746 | if (rtnh->rtnh_ifindex) |
| 4747 | r_cfg.fc_ifindex = rtnh->rtnh_ifindex; |
| 4748 | |
| 4749 | attrlen = rtnh_attrlen(rtnh); |
| 4750 | if (attrlen > 0) { |
| 4751 | struct nlattr *nla, *attrs = rtnh_attrs(rtnh); |
| 4752 | |
| 4753 | nla = nla_find(attrs, attrlen, RTA_GATEWAY); |
| 4754 | if (nla) { |
Jiri Benc | 67b61f6 | 2015-03-29 16:59:26 +0200 | [diff] [blame] | 4755 | r_cfg.fc_gateway = nla_get_in6_addr(nla); |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 4756 | r_cfg.fc_flags |= RTF_GATEWAY; |
| 4757 | } |
Roopa Prabhu | 19e42e4 | 2015-07-21 10:43:48 +0200 | [diff] [blame] | 4758 | r_cfg.fc_encap = nla_find(attrs, attrlen, RTA_ENCAP); |
| 4759 | nla = nla_find(attrs, attrlen, RTA_ENCAP_TYPE); |
| 4760 | if (nla) |
| 4761 | r_cfg.fc_encap_type = nla_get_u16(nla); |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 4762 | } |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 4763 | |
David Ahern | 68e2ffd | 2018-03-20 10:06:59 -0700 | [diff] [blame] | 4764 | r_cfg.fc_flags |= (rtnh->rtnh_flags & RTNH_F_ONLINK); |
David Ahern | acb54e3 | 2018-04-17 17:33:22 -0700 | [diff] [blame] | 4765 | rt = ip6_route_info_create(&r_cfg, GFP_KERNEL, extack); |
Roopa Prabhu | 8c5b83f | 2015-10-10 08:26:36 -0700 | [diff] [blame] | 4766 | if (IS_ERR(rt)) { |
| 4767 | err = PTR_ERR(rt); |
| 4768 | rt = NULL; |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 4769 | goto cleanup; |
Roopa Prabhu | 8c5b83f | 2015-10-10 08:26:36 -0700 | [diff] [blame] | 4770 | } |
David Ahern | b5d2d75 | 2018-07-15 09:35:19 -0700 | [diff] [blame] | 4771 | if (!rt6_qualify_for_ecmp(rt)) { |
| 4772 | err = -EINVAL; |
| 4773 | NL_SET_ERR_MSG(extack, |
| 4774 | "Device only routes can not be added for IPv6 using the multipath API."); |
| 4775 | fib6_info_release(rt); |
| 4776 | goto cleanup; |
| 4777 | } |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 4778 | |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 4779 | rt->fib6_nh->fib_nh_weight = rtnh->rtnh_hops + 1; |
Ido Schimmel | 398958a | 2018-01-09 16:40:28 +0200 | [diff] [blame] | 4780 | |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 4781 | err = ip6_route_info_append(info->nl_net, &rt6_nh_list, |
| 4782 | rt, &r_cfg); |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 4783 | if (err) { |
David Ahern | 93531c6 | 2018-04-17 17:33:25 -0700 | [diff] [blame] | 4784 | fib6_info_release(rt); |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 4785 | goto cleanup; |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 4786 | } |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 4787 | |
| 4788 | rtnh = rtnh_next(rtnh, &remaining); |
| 4789 | } |
| 4790 | |
David Ahern | 3b1137f | 2017-02-02 12:37:10 -0800 | [diff] [blame] | 4791 | /* for add and replace send one notification with all nexthops. |
| 4792 | * Skip the notification in fib6_add_rt2node and send one with |
| 4793 | * the full route when done |
| 4794 | */ |
| 4795 | info->skip_notify = 1; |
| 4796 | |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 4797 | err_nh = NULL; |
| 4798 | list_for_each_entry(nh, &rt6_nh_list, next) { |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4799 | err = __ip6_ins_rt(nh->fib6_info, info, extack); |
| 4800 | fib6_info_release(nh->fib6_info); |
David Ahern | 3b1137f | 2017-02-02 12:37:10 -0800 | [diff] [blame] | 4801 | |
David Ahern | f722517 | 2018-06-04 13:41:42 -0700 | [diff] [blame] | 4802 | if (!err) { |
| 4803 | /* save reference to last route successfully inserted */ |
| 4804 | rt_last = nh->fib6_info; |
| 4805 | |
| 4806 | /* save reference to first route for notification */ |
| 4807 | if (!rt_notif) |
| 4808 | rt_notif = nh->fib6_info; |
| 4809 | } |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 4810 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4811 | /* nh->fib6_info is used or freed at this point, reset to NULL*/ |
| 4812 | nh->fib6_info = NULL; |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 4813 | if (err) { |
| 4814 | if (replace && nhn) |
Jakub Kicinski | a5a82d8 | 2019-01-14 10:52:45 -0800 | [diff] [blame] | 4815 | NL_SET_ERR_MSG_MOD(extack, |
| 4816 | "multipath route replace failed (check consistency of installed routes)"); |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 4817 | err_nh = nh; |
| 4818 | goto add_errout; |
| 4819 | } |
| 4820 | |
Nicolas Dichtel | 1a72418 | 2012-11-01 22:58:22 +0000 | [diff] [blame] | 4821 | /* Because each route is added like a single route we remove |
Michal Kubeček | 2759647 | 2015-05-18 20:54:00 +0200 | [diff] [blame] | 4822 | * these flags after the first nexthop: if there is a collision, |
| 4823 | * we have already failed to add the first nexthop: |
| 4824 | * fib6_add_rt2node() has rejected it; when replacing, old |
| 4825 | * nexthops have been replaced by first new, the rest should |
| 4826 | * be added to it. |
Nicolas Dichtel | 1a72418 | 2012-11-01 22:58:22 +0000 | [diff] [blame] | 4827 | */ |
Michal Kubeček | 2759647 | 2015-05-18 20:54:00 +0200 | [diff] [blame] | 4828 | cfg->fc_nlinfo.nlh->nlmsg_flags &= ~(NLM_F_EXCL | |
| 4829 | NLM_F_REPLACE); |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 4830 | nhn++; |
| 4831 | } |
| 4832 | |
David Ahern | 3b1137f | 2017-02-02 12:37:10 -0800 | [diff] [blame] | 4833 | /* success ... tell user about new route */ |
| 4834 | ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags); |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 4835 | goto cleanup; |
| 4836 | |
| 4837 | add_errout: |
David Ahern | 3b1137f | 2017-02-02 12:37:10 -0800 | [diff] [blame] | 4838 | /* send notification for routes that were added so that |
| 4839 | * the delete notifications sent by ip6_route_del are |
| 4840 | * coherent |
| 4841 | */ |
| 4842 | if (rt_notif) |
| 4843 | ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags); |
| 4844 | |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 4845 | /* Delete routes that were already added */ |
| 4846 | list_for_each_entry(nh, &rt6_nh_list, next) { |
| 4847 | if (err_nh == nh) |
| 4848 | break; |
David Ahern | 333c430 | 2017-05-21 10:12:04 -0600 | [diff] [blame] | 4849 | ip6_route_del(&nh->r_cfg, extack); |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 4850 | } |
| 4851 | |
| 4852 | cleanup: |
| 4853 | list_for_each_entry_safe(nh, nh_safe, &rt6_nh_list, next) { |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4854 | if (nh->fib6_info) |
| 4855 | fib6_info_release(nh->fib6_info); |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 4856 | list_del(&nh->next); |
| 4857 | kfree(nh); |
| 4858 | } |
| 4859 | |
| 4860 | return err; |
| 4861 | } |
| 4862 | |
David Ahern | 333c430 | 2017-05-21 10:12:04 -0600 | [diff] [blame] | 4863 | static int ip6_route_multipath_del(struct fib6_config *cfg, |
| 4864 | struct netlink_ext_ack *extack) |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 4865 | { |
| 4866 | struct fib6_config r_cfg; |
| 4867 | struct rtnexthop *rtnh; |
| 4868 | int remaining; |
| 4869 | int attrlen; |
| 4870 | int err = 1, last_err = 0; |
| 4871 | |
| 4872 | remaining = cfg->fc_mp_len; |
| 4873 | rtnh = (struct rtnexthop *)cfg->fc_mp; |
| 4874 | |
| 4875 | /* Parse a Multipath Entry */ |
| 4876 | while (rtnh_ok(rtnh, remaining)) { |
| 4877 | memcpy(&r_cfg, cfg, sizeof(*cfg)); |
| 4878 | if (rtnh->rtnh_ifindex) |
| 4879 | r_cfg.fc_ifindex = rtnh->rtnh_ifindex; |
| 4880 | |
| 4881 | attrlen = rtnh_attrlen(rtnh); |
| 4882 | if (attrlen > 0) { |
| 4883 | struct nlattr *nla, *attrs = rtnh_attrs(rtnh); |
| 4884 | |
| 4885 | nla = nla_find(attrs, attrlen, RTA_GATEWAY); |
| 4886 | if (nla) { |
| 4887 | nla_memcpy(&r_cfg.fc_gateway, nla, 16); |
| 4888 | r_cfg.fc_flags |= RTF_GATEWAY; |
| 4889 | } |
| 4890 | } |
David Ahern | 333c430 | 2017-05-21 10:12:04 -0600 | [diff] [blame] | 4891 | err = ip6_route_del(&r_cfg, extack); |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 4892 | if (err) |
| 4893 | last_err = err; |
| 4894 | |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 4895 | rtnh = rtnh_next(rtnh, &remaining); |
| 4896 | } |
| 4897 | |
| 4898 | return last_err; |
| 4899 | } |
| 4900 | |
David Ahern | c21ef3e | 2017-04-16 09:48:24 -0700 | [diff] [blame] | 4901 | static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh, |
| 4902 | struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4903 | { |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4904 | struct fib6_config cfg; |
| 4905 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4906 | |
David Ahern | 333c430 | 2017-05-21 10:12:04 -0600 | [diff] [blame] | 4907 | err = rtm_to_fib6_config(skb, nlh, &cfg, extack); |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4908 | if (err < 0) |
| 4909 | return err; |
| 4910 | |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 4911 | if (cfg.fc_mp) |
David Ahern | 333c430 | 2017-05-21 10:12:04 -0600 | [diff] [blame] | 4912 | return ip6_route_multipath_del(&cfg, extack); |
David Ahern | 0ae8133 | 2017-02-02 12:37:08 -0800 | [diff] [blame] | 4913 | else { |
| 4914 | cfg.fc_delete_all_nh = 1; |
David Ahern | 333c430 | 2017-05-21 10:12:04 -0600 | [diff] [blame] | 4915 | return ip6_route_del(&cfg, extack); |
David Ahern | 0ae8133 | 2017-02-02 12:37:08 -0800 | [diff] [blame] | 4916 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4917 | } |
| 4918 | |
David Ahern | c21ef3e | 2017-04-16 09:48:24 -0700 | [diff] [blame] | 4919 | static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh, |
| 4920 | struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4921 | { |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4922 | struct fib6_config cfg; |
| 4923 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4924 | |
David Ahern | 333c430 | 2017-05-21 10:12:04 -0600 | [diff] [blame] | 4925 | err = rtm_to_fib6_config(skb, nlh, &cfg, extack); |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4926 | if (err < 0) |
| 4927 | return err; |
| 4928 | |
David Ahern | 67f6951 | 2019-03-21 05:21:34 -0700 | [diff] [blame] | 4929 | if (cfg.fc_metric == 0) |
| 4930 | cfg.fc_metric = IP6_RT_PRIO_USER; |
| 4931 | |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 4932 | if (cfg.fc_mp) |
David Ahern | 333c430 | 2017-05-21 10:12:04 -0600 | [diff] [blame] | 4933 | return ip6_route_multipath_add(&cfg, extack); |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 4934 | else |
David Ahern | acb54e3 | 2018-04-17 17:33:22 -0700 | [diff] [blame] | 4935 | return ip6_route_add(&cfg, GFP_KERNEL, extack); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4936 | } |
| 4937 | |
David Ahern | a1b7a1f | 2019-06-08 14:53:26 -0700 | [diff] [blame^] | 4938 | /* add the overhead of this fib6_nh to nexthop_len */ |
| 4939 | static int rt6_nh_nlmsg_size(struct fib6_nh *nh, void *arg) |
Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 4940 | { |
David Ahern | a1b7a1f | 2019-06-08 14:53:26 -0700 | [diff] [blame^] | 4941 | int *nexthop_len = arg; |
David Ahern | beb1afac5 | 2017-02-02 12:37:09 -0800 | [diff] [blame] | 4942 | |
David Ahern | a1b7a1f | 2019-06-08 14:53:26 -0700 | [diff] [blame^] | 4943 | *nexthop_len += nla_total_size(0) /* RTA_MULTIPATH */ |
| 4944 | + NLA_ALIGN(sizeof(struct rtnexthop)) |
| 4945 | + nla_total_size(16); /* RTA_GATEWAY */ |
David Ahern | f88d8ea | 2019-06-03 20:19:52 -0700 | [diff] [blame] | 4946 | |
David Ahern | a1b7a1f | 2019-06-08 14:53:26 -0700 | [diff] [blame^] | 4947 | if (nh->fib_nh_lws) { |
| 4948 | /* RTA_ENCAP_TYPE */ |
| 4949 | *nexthop_len += lwtunnel_get_encap_size(nh->fib_nh_lws); |
| 4950 | /* RTA_ENCAP */ |
| 4951 | *nexthop_len += nla_total_size(2); |
| 4952 | } |
David Ahern | beb1afac5 | 2017-02-02 12:37:09 -0800 | [diff] [blame] | 4953 | |
David Ahern | a1b7a1f | 2019-06-08 14:53:26 -0700 | [diff] [blame^] | 4954 | return 0; |
| 4955 | } |
| 4956 | |
| 4957 | static size_t rt6_nlmsg_size(struct fib6_info *f6i) |
| 4958 | { |
| 4959 | int nexthop_len; |
| 4960 | |
| 4961 | if (f6i->nh) { |
| 4962 | nexthop_len = nla_total_size(4); /* RTA_NH_ID */ |
| 4963 | nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_nlmsg_size, |
| 4964 | &nexthop_len); |
| 4965 | } else { |
| 4966 | struct fib6_nh *nh = f6i->fib6_nh; |
| 4967 | |
| 4968 | nexthop_len = 0; |
| 4969 | if (f6i->fib6_nsiblings) { |
| 4970 | nexthop_len = nla_total_size(0) /* RTA_MULTIPATH */ |
| 4971 | + NLA_ALIGN(sizeof(struct rtnexthop)) |
| 4972 | + nla_total_size(16) /* RTA_GATEWAY */ |
| 4973 | + lwtunnel_get_encap_size(nh->fib_nh_lws); |
| 4974 | |
| 4975 | nexthop_len *= f6i->fib6_nsiblings; |
| 4976 | } |
| 4977 | nexthop_len += lwtunnel_get_encap_size(nh->fib_nh_lws); |
David Ahern | beb1afac5 | 2017-02-02 12:37:09 -0800 | [diff] [blame] | 4978 | } |
| 4979 | |
Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 4980 | return NLMSG_ALIGN(sizeof(struct rtmsg)) |
| 4981 | + nla_total_size(16) /* RTA_SRC */ |
| 4982 | + nla_total_size(16) /* RTA_DST */ |
| 4983 | + nla_total_size(16) /* RTA_GATEWAY */ |
| 4984 | + nla_total_size(16) /* RTA_PREFSRC */ |
| 4985 | + nla_total_size(4) /* RTA_TABLE */ |
| 4986 | + nla_total_size(4) /* RTA_IIF */ |
| 4987 | + nla_total_size(4) /* RTA_OIF */ |
| 4988 | + nla_total_size(4) /* RTA_PRIORITY */ |
Noriaki TAKAMIYA | 6a2b9ce | 2007-01-23 22:09:41 -0800 | [diff] [blame] | 4989 | + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */ |
Daniel Borkmann | ea69763 | 2015-01-05 23:57:47 +0100 | [diff] [blame] | 4990 | + nla_total_size(sizeof(struct rta_cacheinfo)) |
Lubomir Rintel | c78ba6d | 2015-03-11 15:39:21 +0100 | [diff] [blame] | 4991 | + nla_total_size(TCP_CA_NAME_MAX) /* RTAX_CC_ALGO */ |
Roopa Prabhu | 19e42e4 | 2015-07-21 10:43:48 +0200 | [diff] [blame] | 4992 | + nla_total_size(1) /* RTA_PREF */ |
David Ahern | beb1afac5 | 2017-02-02 12:37:09 -0800 | [diff] [blame] | 4993 | + nexthop_len; |
| 4994 | } |
| 4995 | |
David Ahern | f88d8ea | 2019-06-03 20:19:52 -0700 | [diff] [blame] | 4996 | static int rt6_fill_node_nexthop(struct sk_buff *skb, struct nexthop *nh, |
| 4997 | unsigned char *flags) |
| 4998 | { |
| 4999 | if (nexthop_is_multipath(nh)) { |
| 5000 | struct nlattr *mp; |
| 5001 | |
| 5002 | mp = nla_nest_start(skb, RTA_MULTIPATH); |
| 5003 | if (!mp) |
| 5004 | goto nla_put_failure; |
| 5005 | |
| 5006 | if (nexthop_mpath_fill_node(skb, nh)) |
| 5007 | goto nla_put_failure; |
| 5008 | |
| 5009 | nla_nest_end(skb, mp); |
| 5010 | } else { |
| 5011 | struct fib6_nh *fib6_nh; |
| 5012 | |
| 5013 | fib6_nh = nexthop_fib6_nh(nh); |
| 5014 | if (fib_nexthop_info(skb, &fib6_nh->nh_common, |
| 5015 | flags, false) < 0) |
| 5016 | goto nla_put_failure; |
| 5017 | } |
| 5018 | |
| 5019 | return 0; |
| 5020 | |
| 5021 | nla_put_failure: |
| 5022 | return -EMSGSIZE; |
| 5023 | } |
| 5024 | |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 5025 | static int rt6_fill_node(struct net *net, struct sk_buff *skb, |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 5026 | struct fib6_info *rt, struct dst_entry *dst, |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 5027 | struct in6_addr *dest, struct in6_addr *src, |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 5028 | int iif, int type, u32 portid, u32 seq, |
David Ahern | f8cfe2c | 2017-01-17 15:51:08 -0800 | [diff] [blame] | 5029 | unsigned int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5030 | { |
Xin Long | 22d0bd8 | 2018-09-11 14:33:58 +0800 | [diff] [blame] | 5031 | struct rt6_info *rt6 = (struct rt6_info *)dst; |
| 5032 | struct rt6key *rt6_dst, *rt6_src; |
| 5033 | u32 *pmetrics, table, rt6_flags; |
David Ahern | f88d8ea | 2019-06-03 20:19:52 -0700 | [diff] [blame] | 5034 | unsigned char nh_flags = 0; |
Thomas Graf | 2d7202b | 2006-08-22 00:01:27 -0700 | [diff] [blame] | 5035 | struct nlmsghdr *nlh; |
Xin Long | 22d0bd8 | 2018-09-11 14:33:58 +0800 | [diff] [blame] | 5036 | struct rtmsg *rtm; |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 5037 | long expires = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5038 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 5039 | nlh = nlmsg_put(skb, portid, seq, type, sizeof(*rtm), flags); |
David S. Miller | 3830847 | 2011-12-03 18:02:47 -0500 | [diff] [blame] | 5040 | if (!nlh) |
Patrick McHardy | 2693256 | 2007-01-31 23:16:40 -0800 | [diff] [blame] | 5041 | return -EMSGSIZE; |
Thomas Graf | 2d7202b | 2006-08-22 00:01:27 -0700 | [diff] [blame] | 5042 | |
Xin Long | 22d0bd8 | 2018-09-11 14:33:58 +0800 | [diff] [blame] | 5043 | if (rt6) { |
| 5044 | rt6_dst = &rt6->rt6i_dst; |
| 5045 | rt6_src = &rt6->rt6i_src; |
| 5046 | rt6_flags = rt6->rt6i_flags; |
| 5047 | } else { |
| 5048 | rt6_dst = &rt->fib6_dst; |
| 5049 | rt6_src = &rt->fib6_src; |
| 5050 | rt6_flags = rt->fib6_flags; |
| 5051 | } |
| 5052 | |
Thomas Graf | 2d7202b | 2006-08-22 00:01:27 -0700 | [diff] [blame] | 5053 | rtm = nlmsg_data(nlh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5054 | rtm->rtm_family = AF_INET6; |
Xin Long | 22d0bd8 | 2018-09-11 14:33:58 +0800 | [diff] [blame] | 5055 | rtm->rtm_dst_len = rt6_dst->plen; |
| 5056 | rtm->rtm_src_len = rt6_src->plen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5057 | rtm->rtm_tos = 0; |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 5058 | if (rt->fib6_table) |
| 5059 | table = rt->fib6_table->tb6_id; |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 5060 | else |
Patrick McHardy | 9e762a4 | 2006-08-10 23:09:48 -0700 | [diff] [blame] | 5061 | table = RT6_TABLE_UNSPEC; |
Kalash Nainwal | 97f0082 | 2019-02-20 16:23:04 -0800 | [diff] [blame] | 5062 | rtm->rtm_table = table < 256 ? table : RT_TABLE_COMPAT; |
David S. Miller | c78679e | 2012-04-01 20:27:33 -0400 | [diff] [blame] | 5063 | if (nla_put_u32(skb, RTA_TABLE, table)) |
| 5064 | goto nla_put_failure; |
David Ahern | e8478e8 | 2018-04-17 17:33:13 -0700 | [diff] [blame] | 5065 | |
| 5066 | rtm->rtm_type = rt->fib6_type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5067 | rtm->rtm_flags = 0; |
| 5068 | rtm->rtm_scope = RT_SCOPE_UNIVERSE; |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 5069 | rtm->rtm_protocol = rt->fib6_protocol; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5070 | |
Xin Long | 22d0bd8 | 2018-09-11 14:33:58 +0800 | [diff] [blame] | 5071 | if (rt6_flags & RTF_CACHE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5072 | rtm->rtm_flags |= RTM_F_CLONED; |
| 5073 | |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 5074 | if (dest) { |
| 5075 | if (nla_put_in6_addr(skb, RTA_DST, dest)) |
David S. Miller | c78679e | 2012-04-01 20:27:33 -0400 | [diff] [blame] | 5076 | goto nla_put_failure; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 5077 | rtm->rtm_dst_len = 128; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5078 | } else if (rtm->rtm_dst_len) |
Xin Long | 22d0bd8 | 2018-09-11 14:33:58 +0800 | [diff] [blame] | 5079 | if (nla_put_in6_addr(skb, RTA_DST, &rt6_dst->addr)) |
David S. Miller | c78679e | 2012-04-01 20:27:33 -0400 | [diff] [blame] | 5080 | goto nla_put_failure; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5081 | #ifdef CONFIG_IPV6_SUBTREES |
| 5082 | if (src) { |
Jiri Benc | 930345e | 2015-03-29 16:59:25 +0200 | [diff] [blame] | 5083 | if (nla_put_in6_addr(skb, RTA_SRC, src)) |
David S. Miller | c78679e | 2012-04-01 20:27:33 -0400 | [diff] [blame] | 5084 | goto nla_put_failure; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 5085 | rtm->rtm_src_len = 128; |
David S. Miller | c78679e | 2012-04-01 20:27:33 -0400 | [diff] [blame] | 5086 | } else if (rtm->rtm_src_len && |
Xin Long | 22d0bd8 | 2018-09-11 14:33:58 +0800 | [diff] [blame] | 5087 | nla_put_in6_addr(skb, RTA_SRC, &rt6_src->addr)) |
David S. Miller | c78679e | 2012-04-01 20:27:33 -0400 | [diff] [blame] | 5088 | goto nla_put_failure; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5089 | #endif |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 5090 | if (iif) { |
| 5091 | #ifdef CONFIG_IPV6_MROUTE |
Xin Long | 22d0bd8 | 2018-09-11 14:33:58 +0800 | [diff] [blame] | 5092 | if (ipv6_addr_is_multicast(&rt6_dst->addr)) { |
David Ahern | fd61c6b | 2017-01-17 15:51:07 -0800 | [diff] [blame] | 5093 | int err = ip6mr_get_route(net, skb, rtm, portid); |
Nikolay Aleksandrov | 2cf7507 | 2016-09-25 23:08:31 +0200 | [diff] [blame] | 5094 | |
David Ahern | fd61c6b | 2017-01-17 15:51:07 -0800 | [diff] [blame] | 5095 | if (err == 0) |
| 5096 | return 0; |
| 5097 | if (err < 0) |
| 5098 | goto nla_put_failure; |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 5099 | } else |
| 5100 | #endif |
David S. Miller | c78679e | 2012-04-01 20:27:33 -0400 | [diff] [blame] | 5101 | if (nla_put_u32(skb, RTA_IIF, iif)) |
| 5102 | goto nla_put_failure; |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 5103 | } else if (dest) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5104 | struct in6_addr saddr_buf; |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 5105 | if (ip6_route_get_saddr(net, rt, dest, 0, &saddr_buf) == 0 && |
Jiri Benc | 930345e | 2015-03-29 16:59:25 +0200 | [diff] [blame] | 5106 | nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf)) |
David S. Miller | c78679e | 2012-04-01 20:27:33 -0400 | [diff] [blame] | 5107 | goto nla_put_failure; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5108 | } |
Thomas Graf | 2d7202b | 2006-08-22 00:01:27 -0700 | [diff] [blame] | 5109 | |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 5110 | if (rt->fib6_prefsrc.plen) { |
Daniel Walter | c3968a8 | 2011-04-13 21:10:57 +0000 | [diff] [blame] | 5111 | struct in6_addr saddr_buf; |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 5112 | saddr_buf = rt->fib6_prefsrc.addr; |
Jiri Benc | 930345e | 2015-03-29 16:59:25 +0200 | [diff] [blame] | 5113 | if (nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf)) |
David S. Miller | c78679e | 2012-04-01 20:27:33 -0400 | [diff] [blame] | 5114 | goto nla_put_failure; |
Daniel Walter | c3968a8 | 2011-04-13 21:10:57 +0000 | [diff] [blame] | 5115 | } |
| 5116 | |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 5117 | pmetrics = dst ? dst_metrics_ptr(dst) : rt->fib6_metrics->metrics; |
| 5118 | if (rtnetlink_put_metrics(skb, pmetrics) < 0) |
Thomas Graf | 2d7202b | 2006-08-22 00:01:27 -0700 | [diff] [blame] | 5119 | goto nla_put_failure; |
| 5120 | |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 5121 | if (nla_put_u32(skb, RTA_PRIORITY, rt->fib6_metric)) |
David S. Miller | c78679e | 2012-04-01 20:27:33 -0400 | [diff] [blame] | 5122 | goto nla_put_failure; |
Li Wei | 8253947 | 2012-07-29 16:01:30 +0000 | [diff] [blame] | 5123 | |
David Ahern | beb1afac5 | 2017-02-02 12:37:09 -0800 | [diff] [blame] | 5124 | /* For multipath routes, walk the siblings list and add |
| 5125 | * each as a nexthop within RTA_MULTIPATH. |
| 5126 | */ |
Xin Long | 22d0bd8 | 2018-09-11 14:33:58 +0800 | [diff] [blame] | 5127 | if (rt6) { |
| 5128 | if (rt6_flags & RTF_GATEWAY && |
| 5129 | nla_put_in6_addr(skb, RTA_GATEWAY, &rt6->rt6i_gateway)) |
| 5130 | goto nla_put_failure; |
| 5131 | |
| 5132 | if (dst->dev && nla_put_u32(skb, RTA_OIF, dst->dev->ifindex)) |
| 5133 | goto nla_put_failure; |
| 5134 | } else if (rt->fib6_nsiblings) { |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 5135 | struct fib6_info *sibling, *next_sibling; |
David Ahern | beb1afac5 | 2017-02-02 12:37:09 -0800 | [diff] [blame] | 5136 | struct nlattr *mp; |
| 5137 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 5138 | mp = nla_nest_start_noflag(skb, RTA_MULTIPATH); |
David Ahern | beb1afac5 | 2017-02-02 12:37:09 -0800 | [diff] [blame] | 5139 | if (!mp) |
| 5140 | goto nla_put_failure; |
| 5141 | |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 5142 | if (fib_add_nexthop(skb, &rt->fib6_nh->nh_common, |
| 5143 | rt->fib6_nh->fib_nh_weight) < 0) |
David Ahern | beb1afac5 | 2017-02-02 12:37:09 -0800 | [diff] [blame] | 5144 | goto nla_put_failure; |
| 5145 | |
| 5146 | list_for_each_entry_safe(sibling, next_sibling, |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 5147 | &rt->fib6_siblings, fib6_siblings) { |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 5148 | if (fib_add_nexthop(skb, &sibling->fib6_nh->nh_common, |
| 5149 | sibling->fib6_nh->fib_nh_weight) < 0) |
David Ahern | beb1afac5 | 2017-02-02 12:37:09 -0800 | [diff] [blame] | 5150 | goto nla_put_failure; |
| 5151 | } |
| 5152 | |
| 5153 | nla_nest_end(skb, mp); |
David Ahern | f88d8ea | 2019-06-03 20:19:52 -0700 | [diff] [blame] | 5154 | } else if (rt->nh) { |
| 5155 | if (nla_put_u32(skb, RTA_NH_ID, rt->nh->id)) |
| 5156 | goto nla_put_failure; |
David Ahern | ecc5663 | 2019-04-23 08:48:09 -0700 | [diff] [blame] | 5157 | |
David Ahern | f88d8ea | 2019-06-03 20:19:52 -0700 | [diff] [blame] | 5158 | if (nexthop_is_blackhole(rt->nh)) |
| 5159 | rtm->rtm_type = RTN_BLACKHOLE; |
| 5160 | |
| 5161 | if (rt6_fill_node_nexthop(skb, rt->nh, &nh_flags) < 0) |
| 5162 | goto nla_put_failure; |
| 5163 | |
| 5164 | rtm->rtm_flags |= nh_flags; |
| 5165 | } else { |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 5166 | if (fib_nexthop_info(skb, &rt->fib6_nh->nh_common, |
David Ahern | ecc5663 | 2019-04-23 08:48:09 -0700 | [diff] [blame] | 5167 | &nh_flags, false) < 0) |
David Ahern | beb1afac5 | 2017-02-02 12:37:09 -0800 | [diff] [blame] | 5168 | goto nla_put_failure; |
David Ahern | ecc5663 | 2019-04-23 08:48:09 -0700 | [diff] [blame] | 5169 | |
| 5170 | rtm->rtm_flags |= nh_flags; |
David Ahern | beb1afac5 | 2017-02-02 12:37:09 -0800 | [diff] [blame] | 5171 | } |
| 5172 | |
Xin Long | 22d0bd8 | 2018-09-11 14:33:58 +0800 | [diff] [blame] | 5173 | if (rt6_flags & RTF_EXPIRES) { |
David Ahern | 1489568 | 2018-04-17 17:33:17 -0700 | [diff] [blame] | 5174 | expires = dst ? dst->expires : rt->expires; |
| 5175 | expires -= jiffies; |
| 5176 | } |
YOSHIFUJI Hideaki | 69cdf8f | 2008-05-19 16:55:13 -0700 | [diff] [blame] | 5177 | |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 5178 | 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] | 5179 | goto nla_put_failure; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5180 | |
Xin Long | 22d0bd8 | 2018-09-11 14:33:58 +0800 | [diff] [blame] | 5181 | if (nla_put_u8(skb, RTA_PREF, IPV6_EXTRACT_PREF(rt6_flags))) |
Lubomir Rintel | c78ba6d | 2015-03-11 15:39:21 +0100 | [diff] [blame] | 5182 | goto nla_put_failure; |
| 5183 | |
Roopa Prabhu | 19e42e4 | 2015-07-21 10:43:48 +0200 | [diff] [blame] | 5184 | |
Johannes Berg | 053c095 | 2015-01-16 22:09:00 +0100 | [diff] [blame] | 5185 | nlmsg_end(skb, nlh); |
| 5186 | return 0; |
Thomas Graf | 2d7202b | 2006-08-22 00:01:27 -0700 | [diff] [blame] | 5187 | |
| 5188 | nla_put_failure: |
Patrick McHardy | 2693256 | 2007-01-31 23:16:40 -0800 | [diff] [blame] | 5189 | nlmsg_cancel(skb, nlh); |
| 5190 | return -EMSGSIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5191 | } |
| 5192 | |
David Ahern | 13e3890 | 2018-10-15 18:56:44 -0700 | [diff] [blame] | 5193 | static bool fib6_info_uses_dev(const struct fib6_info *f6i, |
| 5194 | const struct net_device *dev) |
| 5195 | { |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 5196 | if (f6i->fib6_nh->fib_nh_dev == dev) |
David Ahern | 13e3890 | 2018-10-15 18:56:44 -0700 | [diff] [blame] | 5197 | return true; |
| 5198 | |
| 5199 | if (f6i->fib6_nsiblings) { |
| 5200 | struct fib6_info *sibling, *next_sibling; |
| 5201 | |
| 5202 | list_for_each_entry_safe(sibling, next_sibling, |
| 5203 | &f6i->fib6_siblings, fib6_siblings) { |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 5204 | if (sibling->fib6_nh->fib_nh_dev == dev) |
David Ahern | 13e3890 | 2018-10-15 18:56:44 -0700 | [diff] [blame] | 5205 | return true; |
| 5206 | } |
| 5207 | } |
| 5208 | |
| 5209 | return false; |
| 5210 | } |
| 5211 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 5212 | int rt6_dump_route(struct fib6_info *rt, void *p_arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5213 | { |
| 5214 | 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] | 5215 | struct fib_dump_filter *filter = &arg->filter; |
| 5216 | unsigned int flags = NLM_F_MULTI; |
David Ahern | 1f17e2f | 2017-01-26 13:54:08 -0800 | [diff] [blame] | 5217 | struct net *net = arg->net; |
| 5218 | |
David Ahern | 421842e | 2018-04-17 17:33:18 -0700 | [diff] [blame] | 5219 | if (rt == net->ipv6.fib6_null_entry) |
David Ahern | 1f17e2f | 2017-01-26 13:54:08 -0800 | [diff] [blame] | 5220 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5221 | |
David Ahern | 13e3890 | 2018-10-15 18:56:44 -0700 | [diff] [blame] | 5222 | if ((filter->flags & RTM_F_PREFIX) && |
| 5223 | !(rt->fib6_flags & RTF_PREFIX_RT)) { |
| 5224 | /* success since this is not a prefix route */ |
| 5225 | return 1; |
| 5226 | } |
| 5227 | if (filter->filter_set) { |
| 5228 | if ((filter->rt_type && rt->fib6_type != filter->rt_type) || |
| 5229 | (filter->dev && !fib6_info_uses_dev(rt, filter->dev)) || |
| 5230 | (filter->protocol && rt->fib6_protocol != filter->protocol)) { |
David Ahern | f8cfe2c | 2017-01-17 15:51:08 -0800 | [diff] [blame] | 5231 | return 1; |
| 5232 | } |
David Ahern | 13e3890 | 2018-10-15 18:56:44 -0700 | [diff] [blame] | 5233 | flags |= NLM_F_DUMP_FILTERED; |
David Ahern | f8cfe2c | 2017-01-17 15:51:08 -0800 | [diff] [blame] | 5234 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5235 | |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 5236 | return rt6_fill_node(net, arg->skb, rt, NULL, NULL, NULL, 0, |
| 5237 | RTM_NEWROUTE, NETLINK_CB(arg->cb->skb).portid, |
David Ahern | 13e3890 | 2018-10-15 18:56:44 -0700 | [diff] [blame] | 5238 | arg->cb->nlh->nlmsg_seq, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5239 | } |
| 5240 | |
Jakub Kicinski | 0eff0a2 | 2019-01-18 10:46:24 -0800 | [diff] [blame] | 5241 | static int inet6_rtm_valid_getroute_req(struct sk_buff *skb, |
| 5242 | const struct nlmsghdr *nlh, |
| 5243 | struct nlattr **tb, |
| 5244 | struct netlink_ext_ack *extack) |
| 5245 | { |
| 5246 | struct rtmsg *rtm; |
| 5247 | int i, err; |
| 5248 | |
| 5249 | if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*rtm))) { |
| 5250 | NL_SET_ERR_MSG_MOD(extack, |
| 5251 | "Invalid header for get route request"); |
| 5252 | return -EINVAL; |
| 5253 | } |
| 5254 | |
| 5255 | if (!netlink_strict_get_check(skb)) |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 5256 | return nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX, |
| 5257 | rtm_ipv6_policy, extack); |
Jakub Kicinski | 0eff0a2 | 2019-01-18 10:46:24 -0800 | [diff] [blame] | 5258 | |
| 5259 | rtm = nlmsg_data(nlh); |
| 5260 | if ((rtm->rtm_src_len && rtm->rtm_src_len != 128) || |
| 5261 | (rtm->rtm_dst_len && rtm->rtm_dst_len != 128) || |
| 5262 | rtm->rtm_table || rtm->rtm_protocol || rtm->rtm_scope || |
| 5263 | rtm->rtm_type) { |
| 5264 | NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for get route request"); |
| 5265 | return -EINVAL; |
| 5266 | } |
| 5267 | if (rtm->rtm_flags & ~RTM_F_FIB_MATCH) { |
| 5268 | NL_SET_ERR_MSG_MOD(extack, |
| 5269 | "Invalid flags for get route request"); |
| 5270 | return -EINVAL; |
| 5271 | } |
| 5272 | |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 5273 | err = nlmsg_parse_deprecated_strict(nlh, sizeof(*rtm), tb, RTA_MAX, |
| 5274 | rtm_ipv6_policy, extack); |
Jakub Kicinski | 0eff0a2 | 2019-01-18 10:46:24 -0800 | [diff] [blame] | 5275 | if (err) |
| 5276 | return err; |
| 5277 | |
| 5278 | if ((tb[RTA_SRC] && !rtm->rtm_src_len) || |
| 5279 | (tb[RTA_DST] && !rtm->rtm_dst_len)) { |
| 5280 | NL_SET_ERR_MSG_MOD(extack, "rtm_src_len and rtm_dst_len must be 128 for IPv6"); |
| 5281 | return -EINVAL; |
| 5282 | } |
| 5283 | |
| 5284 | for (i = 0; i <= RTA_MAX; i++) { |
| 5285 | if (!tb[i]) |
| 5286 | continue; |
| 5287 | |
| 5288 | switch (i) { |
| 5289 | case RTA_SRC: |
| 5290 | case RTA_DST: |
| 5291 | case RTA_IIF: |
| 5292 | case RTA_OIF: |
| 5293 | case RTA_MARK: |
| 5294 | case RTA_UID: |
| 5295 | case RTA_SPORT: |
| 5296 | case RTA_DPORT: |
| 5297 | case RTA_IP_PROTO: |
| 5298 | break; |
| 5299 | default: |
| 5300 | NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in get route request"); |
| 5301 | return -EINVAL; |
| 5302 | } |
| 5303 | } |
| 5304 | |
| 5305 | return 0; |
| 5306 | } |
| 5307 | |
David Ahern | c21ef3e | 2017-04-16 09:48:24 -0700 | [diff] [blame] | 5308 | static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh, |
| 5309 | struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5310 | { |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 5311 | struct net *net = sock_net(in_skb->sk); |
Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 5312 | struct nlattr *tb[RTA_MAX+1]; |
Roopa Prabhu | 18c3a61 | 2017-05-25 10:42:40 -0700 | [diff] [blame] | 5313 | int err, iif = 0, oif = 0; |
David Ahern | a68886a | 2018-04-20 15:38:02 -0700 | [diff] [blame] | 5314 | struct fib6_info *from; |
Roopa Prabhu | 18c3a61 | 2017-05-25 10:42:40 -0700 | [diff] [blame] | 5315 | struct dst_entry *dst; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5316 | struct rt6_info *rt; |
Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 5317 | struct sk_buff *skb; |
| 5318 | struct rtmsg *rtm; |
Maciej Żenczykowski | 744486d | 2018-09-29 23:44:54 -0700 | [diff] [blame] | 5319 | struct flowi6 fl6 = {}; |
Roopa Prabhu | 18c3a61 | 2017-05-25 10:42:40 -0700 | [diff] [blame] | 5320 | bool fibmatch; |
Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 5321 | |
Jakub Kicinski | 0eff0a2 | 2019-01-18 10:46:24 -0800 | [diff] [blame] | 5322 | err = inet6_rtm_valid_getroute_req(in_skb, nlh, tb, extack); |
Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 5323 | if (err < 0) |
| 5324 | goto errout; |
| 5325 | |
| 5326 | err = -EINVAL; |
Hannes Frederic Sowa | 38b7097 | 2016-06-11 20:08:19 +0200 | [diff] [blame] | 5327 | rtm = nlmsg_data(nlh); |
| 5328 | fl6.flowlabel = ip6_make_flowinfo(rtm->rtm_tos, 0); |
Roopa Prabhu | 18c3a61 | 2017-05-25 10:42:40 -0700 | [diff] [blame] | 5329 | fibmatch = !!(rtm->rtm_flags & RTM_F_FIB_MATCH); |
Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 5330 | |
| 5331 | if (tb[RTA_SRC]) { |
| 5332 | if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr)) |
| 5333 | goto errout; |
| 5334 | |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 5335 | fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]); |
Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 5336 | } |
| 5337 | |
| 5338 | if (tb[RTA_DST]) { |
| 5339 | if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr)) |
| 5340 | goto errout; |
| 5341 | |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 5342 | fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]); |
Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 5343 | } |
| 5344 | |
| 5345 | if (tb[RTA_IIF]) |
| 5346 | iif = nla_get_u32(tb[RTA_IIF]); |
| 5347 | |
| 5348 | if (tb[RTA_OIF]) |
Shmulik Ladkani | 72331bc | 2012-04-01 04:03:45 +0000 | [diff] [blame] | 5349 | oif = nla_get_u32(tb[RTA_OIF]); |
Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 5350 | |
Lorenzo Colitti | 2e47b29 | 2014-05-15 16:38:41 -0700 | [diff] [blame] | 5351 | if (tb[RTA_MARK]) |
| 5352 | fl6.flowi6_mark = nla_get_u32(tb[RTA_MARK]); |
| 5353 | |
Lorenzo Colitti | 622ec2c | 2016-11-04 02:23:42 +0900 | [diff] [blame] | 5354 | if (tb[RTA_UID]) |
| 5355 | fl6.flowi6_uid = make_kuid(current_user_ns(), |
| 5356 | nla_get_u32(tb[RTA_UID])); |
| 5357 | else |
| 5358 | fl6.flowi6_uid = iif ? INVALID_UID : current_uid(); |
| 5359 | |
Roopa Prabhu | eacb938 | 2018-05-22 14:03:28 -0700 | [diff] [blame] | 5360 | if (tb[RTA_SPORT]) |
| 5361 | fl6.fl6_sport = nla_get_be16(tb[RTA_SPORT]); |
| 5362 | |
| 5363 | if (tb[RTA_DPORT]) |
| 5364 | fl6.fl6_dport = nla_get_be16(tb[RTA_DPORT]); |
| 5365 | |
| 5366 | if (tb[RTA_IP_PROTO]) { |
| 5367 | err = rtm_getroute_parse_ip_proto(tb[RTA_IP_PROTO], |
Hangbin Liu | 5e1a99e | 2019-02-27 16:15:29 +0800 | [diff] [blame] | 5368 | &fl6.flowi6_proto, AF_INET6, |
| 5369 | extack); |
Roopa Prabhu | eacb938 | 2018-05-22 14:03:28 -0700 | [diff] [blame] | 5370 | if (err) |
| 5371 | goto errout; |
| 5372 | } |
| 5373 | |
Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 5374 | if (iif) { |
| 5375 | struct net_device *dev; |
Shmulik Ladkani | 72331bc | 2012-04-01 04:03:45 +0000 | [diff] [blame] | 5376 | int flags = 0; |
| 5377 | |
Florian Westphal | 121622d | 2017-08-15 16:34:42 +0200 | [diff] [blame] | 5378 | rcu_read_lock(); |
| 5379 | |
| 5380 | dev = dev_get_by_index_rcu(net, iif); |
Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 5381 | if (!dev) { |
Florian Westphal | 121622d | 2017-08-15 16:34:42 +0200 | [diff] [blame] | 5382 | rcu_read_unlock(); |
Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 5383 | err = -ENODEV; |
| 5384 | goto errout; |
| 5385 | } |
Shmulik Ladkani | 72331bc | 2012-04-01 04:03:45 +0000 | [diff] [blame] | 5386 | |
| 5387 | fl6.flowi6_iif = iif; |
| 5388 | |
| 5389 | if (!ipv6_addr_any(&fl6.saddr)) |
| 5390 | flags |= RT6_LOOKUP_F_HAS_SADDR; |
| 5391 | |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 5392 | dst = ip6_route_input_lookup(net, dev, &fl6, NULL, flags); |
Florian Westphal | 121622d | 2017-08-15 16:34:42 +0200 | [diff] [blame] | 5393 | |
| 5394 | rcu_read_unlock(); |
Shmulik Ladkani | 72331bc | 2012-04-01 04:03:45 +0000 | [diff] [blame] | 5395 | } else { |
| 5396 | fl6.flowi6_oif = oif; |
| 5397 | |
Ido Schimmel | 58acfd7 | 2017-12-20 12:28:25 +0200 | [diff] [blame] | 5398 | dst = ip6_route_output(net, NULL, &fl6); |
Roopa Prabhu | 18c3a61 | 2017-05-25 10:42:40 -0700 | [diff] [blame] | 5399 | } |
| 5400 | |
Roopa Prabhu | 18c3a61 | 2017-05-25 10:42:40 -0700 | [diff] [blame] | 5401 | |
| 5402 | rt = container_of(dst, struct rt6_info, dst); |
| 5403 | if (rt->dst.error) { |
| 5404 | err = rt->dst.error; |
| 5405 | ip6_rt_put(rt); |
| 5406 | goto errout; |
Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 5407 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5408 | |
WANG Cong | 9d6acb3 | 2017-03-01 20:48:39 -0800 | [diff] [blame] | 5409 | if (rt == net->ipv6.ip6_null_entry) { |
| 5410 | err = rt->dst.error; |
| 5411 | ip6_rt_put(rt); |
| 5412 | goto errout; |
| 5413 | } |
| 5414 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5415 | skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); |
David S. Miller | 3830847 | 2011-12-03 18:02:47 -0500 | [diff] [blame] | 5416 | if (!skb) { |
Amerigo Wang | 94e187c | 2012-10-29 00:13:19 +0000 | [diff] [blame] | 5417 | ip6_rt_put(rt); |
Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 5418 | err = -ENOBUFS; |
| 5419 | goto errout; |
| 5420 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5421 | |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 5422 | skb_dst_set(skb, &rt->dst); |
David Ahern | a68886a | 2018-04-20 15:38:02 -0700 | [diff] [blame] | 5423 | |
| 5424 | rcu_read_lock(); |
| 5425 | from = rcu_dereference(rt->from); |
Martin KaFai Lau | 886b7a5 | 2019-04-30 10:45:12 -0700 | [diff] [blame] | 5426 | if (from) { |
| 5427 | if (fibmatch) |
| 5428 | err = rt6_fill_node(net, skb, from, NULL, NULL, NULL, |
| 5429 | iif, RTM_NEWROUTE, |
| 5430 | NETLINK_CB(in_skb).portid, |
| 5431 | nlh->nlmsg_seq, 0); |
| 5432 | else |
| 5433 | err = rt6_fill_node(net, skb, from, dst, &fl6.daddr, |
| 5434 | &fl6.saddr, iif, RTM_NEWROUTE, |
| 5435 | NETLINK_CB(in_skb).portid, |
| 5436 | nlh->nlmsg_seq, 0); |
| 5437 | } else { |
| 5438 | err = -ENETUNREACH; |
| 5439 | } |
David Ahern | a68886a | 2018-04-20 15:38:02 -0700 | [diff] [blame] | 5440 | rcu_read_unlock(); |
| 5441 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5442 | if (err < 0) { |
Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 5443 | kfree_skb(skb); |
| 5444 | goto errout; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5445 | } |
| 5446 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 5447 | err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid); |
Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 5448 | errout: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5449 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5450 | } |
| 5451 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 5452 | 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] | 5453 | unsigned int nlm_flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5454 | { |
| 5455 | struct sk_buff *skb; |
Daniel Lezcano | 5578689 | 2008-03-04 13:47:47 -0800 | [diff] [blame] | 5456 | struct net *net = info->nl_net; |
Denis V. Lunev | 528c4ce | 2007-12-13 09:45:12 -0800 | [diff] [blame] | 5457 | u32 seq; |
| 5458 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5459 | |
Denis V. Lunev | 528c4ce | 2007-12-13 09:45:12 -0800 | [diff] [blame] | 5460 | err = -ENOBUFS; |
David S. Miller | 3830847 | 2011-12-03 18:02:47 -0500 | [diff] [blame] | 5461 | seq = info->nlh ? info->nlh->nlmsg_seq : 0; |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 5462 | |
Roopa Prabhu | 19e42e4 | 2015-07-21 10:43:48 +0200 | [diff] [blame] | 5463 | skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any()); |
David S. Miller | 3830847 | 2011-12-03 18:02:47 -0500 | [diff] [blame] | 5464 | if (!skb) |
Thomas Graf | 21713eb | 2006-08-15 00:35:24 -0700 | [diff] [blame] | 5465 | goto errout; |
| 5466 | |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 5467 | err = rt6_fill_node(net, skb, rt, NULL, NULL, NULL, 0, |
| 5468 | event, info->portid, seq, nlm_flags); |
Patrick McHardy | 2693256 | 2007-01-31 23:16:40 -0800 | [diff] [blame] | 5469 | if (err < 0) { |
| 5470 | /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */ |
| 5471 | WARN_ON(err == -EMSGSIZE); |
| 5472 | kfree_skb(skb); |
| 5473 | goto errout; |
| 5474 | } |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 5475 | rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE, |
Pablo Neira Ayuso | 1ce85fe | 2009-02-24 23:18:28 -0800 | [diff] [blame] | 5476 | info->nlh, gfp_any()); |
| 5477 | return; |
Thomas Graf | 21713eb | 2006-08-15 00:35:24 -0700 | [diff] [blame] | 5478 | errout: |
| 5479 | if (err < 0) |
Daniel Lezcano | 5578689 | 2008-03-04 13:47:47 -0800 | [diff] [blame] | 5480 | rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5481 | } |
| 5482 | |
David Ahern | 19a3b7e | 2019-05-22 12:04:41 -0700 | [diff] [blame] | 5483 | void fib6_rt_update(struct net *net, struct fib6_info *rt, |
| 5484 | struct nl_info *info) |
| 5485 | { |
| 5486 | u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0; |
| 5487 | struct sk_buff *skb; |
| 5488 | int err = -ENOBUFS; |
| 5489 | |
| 5490 | /* call_fib6_entry_notifiers will be removed when in-kernel notifier |
| 5491 | * is implemented and supported for nexthop objects |
| 5492 | */ |
| 5493 | call_fib6_entry_notifiers(net, FIB_EVENT_ENTRY_REPLACE, rt, NULL); |
| 5494 | |
| 5495 | skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any()); |
| 5496 | if (!skb) |
| 5497 | goto errout; |
| 5498 | |
| 5499 | err = rt6_fill_node(net, skb, rt, NULL, NULL, NULL, 0, |
| 5500 | RTM_NEWROUTE, info->portid, seq, NLM_F_REPLACE); |
| 5501 | if (err < 0) { |
| 5502 | /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */ |
| 5503 | WARN_ON(err == -EMSGSIZE); |
| 5504 | kfree_skb(skb); |
| 5505 | goto errout; |
| 5506 | } |
| 5507 | rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE, |
| 5508 | info->nlh, gfp_any()); |
| 5509 | return; |
| 5510 | errout: |
| 5511 | if (err < 0) |
| 5512 | rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err); |
| 5513 | } |
| 5514 | |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 5515 | static int ip6_route_dev_notify(struct notifier_block *this, |
Jiri Pirko | 351638e | 2013-05-28 01:30:21 +0000 | [diff] [blame] | 5516 | unsigned long event, void *ptr) |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 5517 | { |
Jiri Pirko | 351638e | 2013-05-28 01:30:21 +0000 | [diff] [blame] | 5518 | struct net_device *dev = netdev_notifier_info_to_dev(ptr); |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 5519 | struct net *net = dev_net(dev); |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 5520 | |
WANG Cong | 242d3a4 | 2017-05-08 10:12:13 -0700 | [diff] [blame] | 5521 | if (!(dev->flags & IFF_LOOPBACK)) |
| 5522 | return NOTIFY_OK; |
| 5523 | |
| 5524 | if (event == NETDEV_REGISTER) { |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 5525 | net->ipv6.fib6_null_entry->fib6_nh->fib_nh_dev = dev; |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 5526 | net->ipv6.ip6_null_entry->dst.dev = dev; |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 5527 | net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev); |
| 5528 | #ifdef CONFIG_IPV6_MULTIPLE_TABLES |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 5529 | net->ipv6.ip6_prohibit_entry->dst.dev = dev; |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 5530 | net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev); |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 5531 | net->ipv6.ip6_blk_hole_entry->dst.dev = dev; |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 5532 | net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev); |
| 5533 | #endif |
WANG Cong | 76da070 | 2017-06-20 11:42:27 -0700 | [diff] [blame] | 5534 | } else if (event == NETDEV_UNREGISTER && |
| 5535 | dev->reg_state != NETREG_UNREGISTERED) { |
| 5536 | /* NETDEV_UNREGISTER could be fired for multiple times by |
| 5537 | * netdev_wait_allrefs(). Make sure we only call this once. |
| 5538 | */ |
Eric Dumazet | 12d94a8 | 2017-08-15 04:09:51 -0700 | [diff] [blame] | 5539 | in6_dev_put_clear(&net->ipv6.ip6_null_entry->rt6i_idev); |
WANG Cong | 242d3a4 | 2017-05-08 10:12:13 -0700 | [diff] [blame] | 5540 | #ifdef CONFIG_IPV6_MULTIPLE_TABLES |
Eric Dumazet | 12d94a8 | 2017-08-15 04:09:51 -0700 | [diff] [blame] | 5541 | in6_dev_put_clear(&net->ipv6.ip6_prohibit_entry->rt6i_idev); |
| 5542 | in6_dev_put_clear(&net->ipv6.ip6_blk_hole_entry->rt6i_idev); |
WANG Cong | 242d3a4 | 2017-05-08 10:12:13 -0700 | [diff] [blame] | 5543 | #endif |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 5544 | } |
| 5545 | |
| 5546 | return NOTIFY_OK; |
| 5547 | } |
| 5548 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5549 | /* |
| 5550 | * /proc |
| 5551 | */ |
| 5552 | |
| 5553 | #ifdef CONFIG_PROC_FS |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5554 | static int rt6_stats_seq_show(struct seq_file *seq, void *v) |
| 5555 | { |
Daniel Lezcano | 69ddb80 | 2008-03-04 13:46:23 -0800 | [diff] [blame] | 5556 | struct net *net = (struct net *)seq->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5557 | seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n", |
Daniel Lezcano | 69ddb80 | 2008-03-04 13:46:23 -0800 | [diff] [blame] | 5558 | net->ipv6.rt6_stats->fib_nodes, |
| 5559 | net->ipv6.rt6_stats->fib_route_nodes, |
Wei Wang | 81eb844 | 2017-10-06 12:06:11 -0700 | [diff] [blame] | 5560 | atomic_read(&net->ipv6.rt6_stats->fib_rt_alloc), |
Daniel Lezcano | 69ddb80 | 2008-03-04 13:46:23 -0800 | [diff] [blame] | 5561 | net->ipv6.rt6_stats->fib_rt_entries, |
| 5562 | net->ipv6.rt6_stats->fib_rt_cache, |
Eric Dumazet | fc66f95 | 2010-10-08 06:37:34 +0000 | [diff] [blame] | 5563 | dst_entries_get_slow(&net->ipv6.ip6_dst_ops), |
Daniel Lezcano | 69ddb80 | 2008-03-04 13:46:23 -0800 | [diff] [blame] | 5564 | net->ipv6.rt6_stats->fib_discarded_routes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5565 | |
| 5566 | return 0; |
| 5567 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5568 | #endif /* CONFIG_PROC_FS */ |
| 5569 | |
| 5570 | #ifdef CONFIG_SYSCTL |
| 5571 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5572 | static |
Joe Perches | fe2c633 | 2013-06-11 23:04:25 -0700 | [diff] [blame] | 5573 | int ipv6_sysctl_rtcache_flush(struct ctl_table *ctl, int write, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5574 | void __user *buffer, size_t *lenp, loff_t *ppos) |
| 5575 | { |
Lucian Adrian Grijincu | c486da3 | 2011-02-24 19:48:03 +0000 | [diff] [blame] | 5576 | struct net *net; |
| 5577 | int delay; |
Aditya Pakki | f0fb9b2 | 2018-12-24 10:30:17 -0600 | [diff] [blame] | 5578 | int ret; |
Lucian Adrian Grijincu | c486da3 | 2011-02-24 19:48:03 +0000 | [diff] [blame] | 5579 | if (!write) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5580 | return -EINVAL; |
Lucian Adrian Grijincu | c486da3 | 2011-02-24 19:48:03 +0000 | [diff] [blame] | 5581 | |
| 5582 | net = (struct net *)ctl->extra1; |
| 5583 | delay = net->ipv6.sysctl.flush_delay; |
Aditya Pakki | f0fb9b2 | 2018-12-24 10:30:17 -0600 | [diff] [blame] | 5584 | ret = proc_dointvec(ctl, write, buffer, lenp, ppos); |
| 5585 | if (ret) |
| 5586 | return ret; |
| 5587 | |
Michal Kubeček | 2ac3ac8 | 2013-08-01 10:04:14 +0200 | [diff] [blame] | 5588 | 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] | 5589 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5590 | } |
| 5591 | |
David Ahern | 7c6bb7d | 2018-10-11 20:17:21 -0700 | [diff] [blame] | 5592 | static int zero; |
| 5593 | static int one = 1; |
| 5594 | |
David Ahern | ed792e2 | 2018-10-08 14:06:34 -0700 | [diff] [blame] | 5595 | static struct ctl_table ipv6_route_table_template[] = { |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 5596 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5597 | .procname = "flush", |
Daniel Lezcano | 4990509 | 2008-01-10 03:01:01 -0800 | [diff] [blame] | 5598 | .data = &init_net.ipv6.sysctl.flush_delay, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5599 | .maxlen = sizeof(int), |
Dave Jones | 89c8b3a1 | 2005-04-28 12:11:49 -0700 | [diff] [blame] | 5600 | .mode = 0200, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 5601 | .proc_handler = ipv6_sysctl_rtcache_flush |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5602 | }, |
| 5603 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5604 | .procname = "gc_thresh", |
Daniel Lezcano | 9a7ec3a | 2008-03-04 13:48:53 -0800 | [diff] [blame] | 5605 | .data = &ip6_dst_ops_template.gc_thresh, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5606 | .maxlen = sizeof(int), |
| 5607 | .mode = 0644, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 5608 | .proc_handler = proc_dointvec, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5609 | }, |
| 5610 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5611 | .procname = "max_size", |
Daniel Lezcano | 4990509 | 2008-01-10 03:01:01 -0800 | [diff] [blame] | 5612 | .data = &init_net.ipv6.sysctl.ip6_rt_max_size, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5613 | .maxlen = sizeof(int), |
| 5614 | .mode = 0644, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 5615 | .proc_handler = proc_dointvec, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5616 | }, |
| 5617 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5618 | .procname = "gc_min_interval", |
Daniel Lezcano | 4990509 | 2008-01-10 03:01:01 -0800 | [diff] [blame] | 5619 | .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5620 | .maxlen = sizeof(int), |
| 5621 | .mode = 0644, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 5622 | .proc_handler = proc_dointvec_jiffies, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5623 | }, |
| 5624 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5625 | .procname = "gc_timeout", |
Daniel Lezcano | 4990509 | 2008-01-10 03:01:01 -0800 | [diff] [blame] | 5626 | .data = &init_net.ipv6.sysctl.ip6_rt_gc_timeout, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5627 | .maxlen = sizeof(int), |
| 5628 | .mode = 0644, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 5629 | .proc_handler = proc_dointvec_jiffies, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5630 | }, |
| 5631 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5632 | .procname = "gc_interval", |
Daniel Lezcano | 4990509 | 2008-01-10 03:01:01 -0800 | [diff] [blame] | 5633 | .data = &init_net.ipv6.sysctl.ip6_rt_gc_interval, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5634 | .maxlen = sizeof(int), |
| 5635 | .mode = 0644, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 5636 | .proc_handler = proc_dointvec_jiffies, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5637 | }, |
| 5638 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5639 | .procname = "gc_elasticity", |
Daniel Lezcano | 4990509 | 2008-01-10 03:01:01 -0800 | [diff] [blame] | 5640 | .data = &init_net.ipv6.sysctl.ip6_rt_gc_elasticity, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5641 | .maxlen = sizeof(int), |
| 5642 | .mode = 0644, |
Min Zhang | f3d3f61 | 2010-08-14 22:42:51 -0700 | [diff] [blame] | 5643 | .proc_handler = proc_dointvec, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5644 | }, |
| 5645 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5646 | .procname = "mtu_expires", |
Daniel Lezcano | 4990509 | 2008-01-10 03:01:01 -0800 | [diff] [blame] | 5647 | .data = &init_net.ipv6.sysctl.ip6_rt_mtu_expires, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5648 | .maxlen = sizeof(int), |
| 5649 | .mode = 0644, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 5650 | .proc_handler = proc_dointvec_jiffies, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5651 | }, |
| 5652 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5653 | .procname = "min_adv_mss", |
Daniel Lezcano | 4990509 | 2008-01-10 03:01:01 -0800 | [diff] [blame] | 5654 | .data = &init_net.ipv6.sysctl.ip6_rt_min_advmss, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5655 | .maxlen = sizeof(int), |
| 5656 | .mode = 0644, |
Min Zhang | f3d3f61 | 2010-08-14 22:42:51 -0700 | [diff] [blame] | 5657 | .proc_handler = proc_dointvec, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5658 | }, |
| 5659 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5660 | .procname = "gc_min_interval_ms", |
Daniel Lezcano | 4990509 | 2008-01-10 03:01:01 -0800 | [diff] [blame] | 5661 | .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5662 | .maxlen = sizeof(int), |
| 5663 | .mode = 0644, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 5664 | .proc_handler = proc_dointvec_ms_jiffies, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5665 | }, |
David Ahern | 7c6bb7d | 2018-10-11 20:17:21 -0700 | [diff] [blame] | 5666 | { |
| 5667 | .procname = "skip_notify_on_dev_down", |
| 5668 | .data = &init_net.ipv6.sysctl.skip_notify_on_dev_down, |
| 5669 | .maxlen = sizeof(int), |
| 5670 | .mode = 0644, |
| 5671 | .proc_handler = proc_dointvec, |
| 5672 | .extra1 = &zero, |
| 5673 | .extra2 = &one, |
| 5674 | }, |
Eric W. Biederman | f8572d8 | 2009-11-05 13:32:03 -0800 | [diff] [blame] | 5675 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5676 | }; |
| 5677 | |
Alexey Dobriyan | 2c8c1e7 | 2010-01-17 03:35:32 +0000 | [diff] [blame] | 5678 | struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net) |
Daniel Lezcano | 760f2d0 | 2008-01-10 02:53:43 -0800 | [diff] [blame] | 5679 | { |
| 5680 | struct ctl_table *table; |
| 5681 | |
| 5682 | table = kmemdup(ipv6_route_table_template, |
| 5683 | sizeof(ipv6_route_table_template), |
| 5684 | GFP_KERNEL); |
YOSHIFUJI Hideaki | 5ee0910 | 2008-02-28 00:24:28 +0900 | [diff] [blame] | 5685 | |
| 5686 | if (table) { |
| 5687 | table[0].data = &net->ipv6.sysctl.flush_delay; |
Lucian Adrian Grijincu | c486da3 | 2011-02-24 19:48:03 +0000 | [diff] [blame] | 5688 | table[0].extra1 = net; |
Alexey Dobriyan | 86393e5 | 2009-08-29 01:34:49 +0000 | [diff] [blame] | 5689 | table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh; |
YOSHIFUJI Hideaki | 5ee0910 | 2008-02-28 00:24:28 +0900 | [diff] [blame] | 5690 | table[2].data = &net->ipv6.sysctl.ip6_rt_max_size; |
| 5691 | table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval; |
| 5692 | table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout; |
| 5693 | table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval; |
| 5694 | table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity; |
| 5695 | table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires; |
| 5696 | table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss; |
Alexey Dobriyan | 9c69fab | 2009-12-18 20:11:03 -0800 | [diff] [blame] | 5697 | table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval; |
David Ahern | 7c6bb7d | 2018-10-11 20:17:21 -0700 | [diff] [blame] | 5698 | table[10].data = &net->ipv6.sysctl.skip_notify_on_dev_down; |
Eric W. Biederman | 464dc80 | 2012-11-16 03:02:59 +0000 | [diff] [blame] | 5699 | |
| 5700 | /* Don't export sysctls to unprivileged users */ |
| 5701 | if (net->user_ns != &init_user_ns) |
| 5702 | table[0].procname = NULL; |
YOSHIFUJI Hideaki | 5ee0910 | 2008-02-28 00:24:28 +0900 | [diff] [blame] | 5703 | } |
| 5704 | |
Daniel Lezcano | 760f2d0 | 2008-01-10 02:53:43 -0800 | [diff] [blame] | 5705 | return table; |
| 5706 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5707 | #endif |
| 5708 | |
Alexey Dobriyan | 2c8c1e7 | 2010-01-17 03:35:32 +0000 | [diff] [blame] | 5709 | static int __net_init ip6_route_net_init(struct net *net) |
Daniel Lezcano | cdb1876 | 2008-03-04 13:45:33 -0800 | [diff] [blame] | 5710 | { |
Pavel Emelyanov | 633d424b | 2008-04-21 14:25:23 -0700 | [diff] [blame] | 5711 | int ret = -ENOMEM; |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 5712 | |
Alexey Dobriyan | 86393e5 | 2009-08-29 01:34:49 +0000 | [diff] [blame] | 5713 | memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template, |
| 5714 | sizeof(net->ipv6.ip6_dst_ops)); |
Benjamin Thery | f2fc6a5 | 2008-03-04 13:49:23 -0800 | [diff] [blame] | 5715 | |
Eric Dumazet | fc66f95 | 2010-10-08 06:37:34 +0000 | [diff] [blame] | 5716 | if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0) |
| 5717 | goto out_ip6_dst_ops; |
| 5718 | |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 5719 | net->ipv6.fib6_null_entry = fib6_info_alloc(GFP_KERNEL, true); |
David Ahern | 421842e | 2018-04-17 17:33:18 -0700 | [diff] [blame] | 5720 | if (!net->ipv6.fib6_null_entry) |
| 5721 | goto out_ip6_dst_entries; |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 5722 | memcpy(net->ipv6.fib6_null_entry, &fib6_null_entry_template, |
| 5723 | sizeof(*net->ipv6.fib6_null_entry)); |
David Ahern | 421842e | 2018-04-17 17:33:18 -0700 | [diff] [blame] | 5724 | |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 5725 | net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template, |
| 5726 | sizeof(*net->ipv6.ip6_null_entry), |
| 5727 | GFP_KERNEL); |
| 5728 | if (!net->ipv6.ip6_null_entry) |
David Ahern | 421842e | 2018-04-17 17:33:18 -0700 | [diff] [blame] | 5729 | goto out_fib6_null_entry; |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 5730 | 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] | 5731 | dst_init_metrics(&net->ipv6.ip6_null_entry->dst, |
| 5732 | ip6_template_metrics, true); |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 5733 | |
| 5734 | #ifdef CONFIG_IPV6_MULTIPLE_TABLES |
Vincent Bernat | feca7d8 | 2017-08-08 20:23:49 +0200 | [diff] [blame] | 5735 | net->ipv6.fib6_has_custom_rules = false; |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 5736 | net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template, |
| 5737 | sizeof(*net->ipv6.ip6_prohibit_entry), |
| 5738 | GFP_KERNEL); |
Peter Zijlstra | 68fffc6 | 2008-10-07 14:12:10 -0700 | [diff] [blame] | 5739 | if (!net->ipv6.ip6_prohibit_entry) |
| 5740 | goto out_ip6_null_entry; |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 5741 | 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] | 5742 | dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst, |
| 5743 | ip6_template_metrics, true); |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 5744 | |
| 5745 | net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template, |
| 5746 | sizeof(*net->ipv6.ip6_blk_hole_entry), |
| 5747 | GFP_KERNEL); |
Peter Zijlstra | 68fffc6 | 2008-10-07 14:12:10 -0700 | [diff] [blame] | 5748 | if (!net->ipv6.ip6_blk_hole_entry) |
| 5749 | goto out_ip6_prohibit_entry; |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 5750 | 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] | 5751 | dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst, |
| 5752 | ip6_template_metrics, true); |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 5753 | #endif |
| 5754 | |
Peter Zijlstra | b339a47c | 2008-10-07 14:15:00 -0700 | [diff] [blame] | 5755 | net->ipv6.sysctl.flush_delay = 0; |
| 5756 | net->ipv6.sysctl.ip6_rt_max_size = 4096; |
| 5757 | net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2; |
| 5758 | net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ; |
| 5759 | net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ; |
| 5760 | net->ipv6.sysctl.ip6_rt_gc_elasticity = 9; |
| 5761 | net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ; |
| 5762 | net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40; |
David Ahern | 7c6bb7d | 2018-10-11 20:17:21 -0700 | [diff] [blame] | 5763 | net->ipv6.sysctl.skip_notify_on_dev_down = 0; |
Peter Zijlstra | b339a47c | 2008-10-07 14:15:00 -0700 | [diff] [blame] | 5764 | |
Benjamin Thery | 6891a34 | 2008-03-04 13:49:47 -0800 | [diff] [blame] | 5765 | net->ipv6.ip6_rt_gc_expire = 30*HZ; |
| 5766 | |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 5767 | ret = 0; |
| 5768 | out: |
| 5769 | return ret; |
Benjamin Thery | f2fc6a5 | 2008-03-04 13:49:23 -0800 | [diff] [blame] | 5770 | |
Peter Zijlstra | 68fffc6 | 2008-10-07 14:12:10 -0700 | [diff] [blame] | 5771 | #ifdef CONFIG_IPV6_MULTIPLE_TABLES |
| 5772 | out_ip6_prohibit_entry: |
| 5773 | kfree(net->ipv6.ip6_prohibit_entry); |
| 5774 | out_ip6_null_entry: |
| 5775 | kfree(net->ipv6.ip6_null_entry); |
| 5776 | #endif |
David Ahern | 421842e | 2018-04-17 17:33:18 -0700 | [diff] [blame] | 5777 | out_fib6_null_entry: |
| 5778 | kfree(net->ipv6.fib6_null_entry); |
Eric Dumazet | fc66f95 | 2010-10-08 06:37:34 +0000 | [diff] [blame] | 5779 | out_ip6_dst_entries: |
| 5780 | dst_entries_destroy(&net->ipv6.ip6_dst_ops); |
Benjamin Thery | f2fc6a5 | 2008-03-04 13:49:23 -0800 | [diff] [blame] | 5781 | out_ip6_dst_ops: |
Benjamin Thery | f2fc6a5 | 2008-03-04 13:49:23 -0800 | [diff] [blame] | 5782 | goto out; |
Daniel Lezcano | cdb1876 | 2008-03-04 13:45:33 -0800 | [diff] [blame] | 5783 | } |
| 5784 | |
Alexey Dobriyan | 2c8c1e7 | 2010-01-17 03:35:32 +0000 | [diff] [blame] | 5785 | static void __net_exit ip6_route_net_exit(struct net *net) |
Daniel Lezcano | cdb1876 | 2008-03-04 13:45:33 -0800 | [diff] [blame] | 5786 | { |
David Ahern | 421842e | 2018-04-17 17:33:18 -0700 | [diff] [blame] | 5787 | kfree(net->ipv6.fib6_null_entry); |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 5788 | kfree(net->ipv6.ip6_null_entry); |
| 5789 | #ifdef CONFIG_IPV6_MULTIPLE_TABLES |
| 5790 | kfree(net->ipv6.ip6_prohibit_entry); |
| 5791 | kfree(net->ipv6.ip6_blk_hole_entry); |
| 5792 | #endif |
Xiaotian Feng | 41bb78b | 2010-11-02 16:11:05 +0000 | [diff] [blame] | 5793 | dst_entries_destroy(&net->ipv6.ip6_dst_ops); |
Daniel Lezcano | cdb1876 | 2008-03-04 13:45:33 -0800 | [diff] [blame] | 5794 | } |
| 5795 | |
Thomas Graf | d189634 | 2012-06-18 12:08:33 +0000 | [diff] [blame] | 5796 | static int __net_init ip6_route_net_init_late(struct net *net) |
| 5797 | { |
| 5798 | #ifdef CONFIG_PROC_FS |
Christoph Hellwig | c350637 | 2018-04-10 19:42:55 +0200 | [diff] [blame] | 5799 | proc_create_net("ipv6_route", 0, net->proc_net, &ipv6_route_seq_ops, |
| 5800 | sizeof(struct ipv6_route_iter)); |
Christoph Hellwig | 3617d94 | 2018-04-13 20:38:35 +0200 | [diff] [blame] | 5801 | proc_create_net_single("rt6_stats", 0444, net->proc_net, |
| 5802 | rt6_stats_seq_show, NULL); |
Thomas Graf | d189634 | 2012-06-18 12:08:33 +0000 | [diff] [blame] | 5803 | #endif |
| 5804 | return 0; |
| 5805 | } |
| 5806 | |
| 5807 | static void __net_exit ip6_route_net_exit_late(struct net *net) |
| 5808 | { |
| 5809 | #ifdef CONFIG_PROC_FS |
Gao feng | ece31ff | 2013-02-18 01:34:56 +0000 | [diff] [blame] | 5810 | remove_proc_entry("ipv6_route", net->proc_net); |
| 5811 | remove_proc_entry("rt6_stats", net->proc_net); |
Thomas Graf | d189634 | 2012-06-18 12:08:33 +0000 | [diff] [blame] | 5812 | #endif |
| 5813 | } |
| 5814 | |
Daniel Lezcano | cdb1876 | 2008-03-04 13:45:33 -0800 | [diff] [blame] | 5815 | static struct pernet_operations ip6_route_net_ops = { |
| 5816 | .init = ip6_route_net_init, |
| 5817 | .exit = ip6_route_net_exit, |
| 5818 | }; |
| 5819 | |
David S. Miller | c3426b4 | 2012-06-09 16:27:05 -0700 | [diff] [blame] | 5820 | static int __net_init ipv6_inetpeer_init(struct net *net) |
| 5821 | { |
| 5822 | struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL); |
| 5823 | |
| 5824 | if (!bp) |
| 5825 | return -ENOMEM; |
| 5826 | inet_peer_base_init(bp); |
| 5827 | net->ipv6.peers = bp; |
| 5828 | return 0; |
| 5829 | } |
| 5830 | |
| 5831 | static void __net_exit ipv6_inetpeer_exit(struct net *net) |
| 5832 | { |
| 5833 | struct inet_peer_base *bp = net->ipv6.peers; |
| 5834 | |
| 5835 | net->ipv6.peers = NULL; |
David S. Miller | 56a6b24 | 2012-06-09 16:32:41 -0700 | [diff] [blame] | 5836 | inetpeer_invalidate_tree(bp); |
David S. Miller | c3426b4 | 2012-06-09 16:27:05 -0700 | [diff] [blame] | 5837 | kfree(bp); |
| 5838 | } |
| 5839 | |
David S. Miller | 2b823f7 | 2012-06-09 19:00:16 -0700 | [diff] [blame] | 5840 | static struct pernet_operations ipv6_inetpeer_ops = { |
David S. Miller | c3426b4 | 2012-06-09 16:27:05 -0700 | [diff] [blame] | 5841 | .init = ipv6_inetpeer_init, |
| 5842 | .exit = ipv6_inetpeer_exit, |
| 5843 | }; |
| 5844 | |
Thomas Graf | d189634 | 2012-06-18 12:08:33 +0000 | [diff] [blame] | 5845 | static struct pernet_operations ip6_route_net_late_ops = { |
| 5846 | .init = ip6_route_net_init_late, |
| 5847 | .exit = ip6_route_net_exit_late, |
| 5848 | }; |
| 5849 | |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 5850 | static struct notifier_block ip6_route_dev_notifier = { |
| 5851 | .notifier_call = ip6_route_dev_notify, |
WANG Cong | 242d3a4 | 2017-05-08 10:12:13 -0700 | [diff] [blame] | 5852 | .priority = ADDRCONF_NOTIFY_PRIORITY - 10, |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 5853 | }; |
| 5854 | |
WANG Cong | 2f46093 | 2017-05-03 22:07:31 -0700 | [diff] [blame] | 5855 | void __init ip6_route_init_special_entries(void) |
| 5856 | { |
| 5857 | /* Registering of the loopback is done before this portion of code, |
| 5858 | * the loopback reference in rt6_info will not be taken, do it |
| 5859 | * manually for init_net */ |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 5860 | 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] | 5861 | init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev; |
| 5862 | init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); |
| 5863 | #ifdef CONFIG_IPV6_MULTIPLE_TABLES |
| 5864 | init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev; |
| 5865 | init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); |
| 5866 | init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev; |
| 5867 | init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); |
| 5868 | #endif |
| 5869 | } |
| 5870 | |
Daniel Lezcano | 433d49c | 2007-12-07 00:43:48 -0800 | [diff] [blame] | 5871 | int __init ip6_route_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5872 | { |
Daniel Lezcano | 433d49c | 2007-12-07 00:43:48 -0800 | [diff] [blame] | 5873 | int ret; |
Martin KaFai Lau | 8d0b94a | 2015-05-22 20:56:04 -0700 | [diff] [blame] | 5874 | int cpu; |
Daniel Lezcano | 433d49c | 2007-12-07 00:43:48 -0800 | [diff] [blame] | 5875 | |
Daniel Lezcano | 9a7ec3a | 2008-03-04 13:48:53 -0800 | [diff] [blame] | 5876 | ret = -ENOMEM; |
| 5877 | ip6_dst_ops_template.kmem_cachep = |
| 5878 | kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0, |
| 5879 | SLAB_HWCACHE_ALIGN, NULL); |
| 5880 | if (!ip6_dst_ops_template.kmem_cachep) |
Fernando Carrijo | c19a28e | 2009-01-07 18:09:08 -0800 | [diff] [blame] | 5881 | goto out; |
David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 5882 | |
Eric Dumazet | fc66f95 | 2010-10-08 06:37:34 +0000 | [diff] [blame] | 5883 | ret = dst_entries_init(&ip6_dst_blackhole_ops); |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 5884 | if (ret) |
Daniel Lezcano | bdb3289 | 2008-03-04 13:48:10 -0800 | [diff] [blame] | 5885 | goto out_kmem_cache; |
Daniel Lezcano | bdb3289 | 2008-03-04 13:48:10 -0800 | [diff] [blame] | 5886 | |
David S. Miller | c3426b4 | 2012-06-09 16:27:05 -0700 | [diff] [blame] | 5887 | ret = register_pernet_subsys(&ipv6_inetpeer_ops); |
| 5888 | if (ret) |
David S. Miller | e8803b6 | 2012-06-16 01:12:19 -0700 | [diff] [blame] | 5889 | goto out_dst_entries; |
Thomas Graf | 2a0c451 | 2012-06-14 23:00:17 +0000 | [diff] [blame] | 5890 | |
David S. Miller | 7e52b33 | 2012-06-15 15:51:55 -0700 | [diff] [blame] | 5891 | ret = register_pernet_subsys(&ip6_route_net_ops); |
| 5892 | if (ret) |
| 5893 | goto out_register_inetpeer; |
David S. Miller | c3426b4 | 2012-06-09 16:27:05 -0700 | [diff] [blame] | 5894 | |
Arnaud Ebalard | 5dc121e | 2008-10-01 02:37:56 -0700 | [diff] [blame] | 5895 | ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep; |
| 5896 | |
David S. Miller | e8803b6 | 2012-06-16 01:12:19 -0700 | [diff] [blame] | 5897 | ret = fib6_init(); |
Daniel Lezcano | 433d49c | 2007-12-07 00:43:48 -0800 | [diff] [blame] | 5898 | if (ret) |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 5899 | goto out_register_subsys; |
Daniel Lezcano | 433d49c | 2007-12-07 00:43:48 -0800 | [diff] [blame] | 5900 | |
Daniel Lezcano | 433d49c | 2007-12-07 00:43:48 -0800 | [diff] [blame] | 5901 | ret = xfrm6_init(); |
| 5902 | if (ret) |
David S. Miller | e8803b6 | 2012-06-16 01:12:19 -0700 | [diff] [blame] | 5903 | goto out_fib6_init; |
Daniel Lezcano | c35b7e7 | 2007-12-08 00:14:11 -0800 | [diff] [blame] | 5904 | |
Daniel Lezcano | 433d49c | 2007-12-07 00:43:48 -0800 | [diff] [blame] | 5905 | ret = fib6_rules_init(); |
| 5906 | if (ret) |
| 5907 | goto xfrm6_init; |
Daniel Lezcano | 7e5449c | 2007-12-08 00:14:54 -0800 | [diff] [blame] | 5908 | |
Thomas Graf | d189634 | 2012-06-18 12:08:33 +0000 | [diff] [blame] | 5909 | ret = register_pernet_subsys(&ip6_route_net_late_ops); |
| 5910 | if (ret) |
| 5911 | goto fib6_rules_init; |
| 5912 | |
Florian Westphal | 16feebc | 2017-12-02 21:44:08 +0100 | [diff] [blame] | 5913 | ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_NEWROUTE, |
| 5914 | inet6_rtm_newroute, NULL, 0); |
| 5915 | if (ret < 0) |
| 5916 | goto out_register_late_subsys; |
| 5917 | |
| 5918 | ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_DELROUTE, |
| 5919 | inet6_rtm_delroute, NULL, 0); |
| 5920 | if (ret < 0) |
| 5921 | goto out_register_late_subsys; |
| 5922 | |
| 5923 | ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETROUTE, |
| 5924 | inet6_rtm_getroute, NULL, |
| 5925 | RTNL_FLAG_DOIT_UNLOCKED); |
| 5926 | if (ret < 0) |
Thomas Graf | d189634 | 2012-06-18 12:08:33 +0000 | [diff] [blame] | 5927 | goto out_register_late_subsys; |
Daniel Lezcano | 433d49c | 2007-12-07 00:43:48 -0800 | [diff] [blame] | 5928 | |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 5929 | ret = register_netdevice_notifier(&ip6_route_dev_notifier); |
Daniel Lezcano | cdb1876 | 2008-03-04 13:45:33 -0800 | [diff] [blame] | 5930 | if (ret) |
Thomas Graf | d189634 | 2012-06-18 12:08:33 +0000 | [diff] [blame] | 5931 | goto out_register_late_subsys; |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 5932 | |
Martin KaFai Lau | 8d0b94a | 2015-05-22 20:56:04 -0700 | [diff] [blame] | 5933 | for_each_possible_cpu(cpu) { |
| 5934 | struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu); |
| 5935 | |
| 5936 | INIT_LIST_HEAD(&ul->head); |
| 5937 | spin_lock_init(&ul->lock); |
| 5938 | } |
| 5939 | |
Daniel Lezcano | 433d49c | 2007-12-07 00:43:48 -0800 | [diff] [blame] | 5940 | out: |
| 5941 | return ret; |
| 5942 | |
Thomas Graf | d189634 | 2012-06-18 12:08:33 +0000 | [diff] [blame] | 5943 | out_register_late_subsys: |
Florian Westphal | 16feebc | 2017-12-02 21:44:08 +0100 | [diff] [blame] | 5944 | rtnl_unregister_all(PF_INET6); |
Thomas Graf | d189634 | 2012-06-18 12:08:33 +0000 | [diff] [blame] | 5945 | unregister_pernet_subsys(&ip6_route_net_late_ops); |
Daniel Lezcano | 433d49c | 2007-12-07 00:43:48 -0800 | [diff] [blame] | 5946 | fib6_rules_init: |
Daniel Lezcano | 433d49c | 2007-12-07 00:43:48 -0800 | [diff] [blame] | 5947 | fib6_rules_cleanup(); |
| 5948 | xfrm6_init: |
Daniel Lezcano | 433d49c | 2007-12-07 00:43:48 -0800 | [diff] [blame] | 5949 | xfrm6_fini(); |
Thomas Graf | 2a0c451 | 2012-06-14 23:00:17 +0000 | [diff] [blame] | 5950 | out_fib6_init: |
| 5951 | fib6_gc_cleanup(); |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 5952 | out_register_subsys: |
| 5953 | unregister_pernet_subsys(&ip6_route_net_ops); |
David S. Miller | 7e52b33 | 2012-06-15 15:51:55 -0700 | [diff] [blame] | 5954 | out_register_inetpeer: |
| 5955 | unregister_pernet_subsys(&ipv6_inetpeer_ops); |
Eric Dumazet | fc66f95 | 2010-10-08 06:37:34 +0000 | [diff] [blame] | 5956 | out_dst_entries: |
| 5957 | dst_entries_destroy(&ip6_dst_blackhole_ops); |
Daniel Lezcano | 433d49c | 2007-12-07 00:43:48 -0800 | [diff] [blame] | 5958 | out_kmem_cache: |
Benjamin Thery | f2fc6a5 | 2008-03-04 13:49:23 -0800 | [diff] [blame] | 5959 | kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep); |
Daniel Lezcano | 433d49c | 2007-12-07 00:43:48 -0800 | [diff] [blame] | 5960 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5961 | } |
| 5962 | |
| 5963 | void ip6_route_cleanup(void) |
| 5964 | { |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 5965 | unregister_netdevice_notifier(&ip6_route_dev_notifier); |
Thomas Graf | d189634 | 2012-06-18 12:08:33 +0000 | [diff] [blame] | 5966 | unregister_pernet_subsys(&ip6_route_net_late_ops); |
Thomas Graf | 101367c | 2006-08-04 03:39:02 -0700 | [diff] [blame] | 5967 | fib6_rules_cleanup(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5968 | xfrm6_fini(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5969 | fib6_gc_cleanup(); |
David S. Miller | c3426b4 | 2012-06-09 16:27:05 -0700 | [diff] [blame] | 5970 | unregister_pernet_subsys(&ipv6_inetpeer_ops); |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 5971 | unregister_pernet_subsys(&ip6_route_net_ops); |
Xiaotian Feng | 41bb78b | 2010-11-02 16:11:05 +0000 | [diff] [blame] | 5972 | dst_entries_destroy(&ip6_dst_blackhole_ops); |
Benjamin Thery | f2fc6a5 | 2008-03-04 13:49:23 -0800 | [diff] [blame] | 5973 | kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5974 | } |