Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Linux INET6 implementation |
| 3 | * FIB front-end. |
| 4 | * |
| 5 | * Authors: |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 6 | * Pedro Roque <roque@di.fc.ul.pt> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License |
| 10 | * as published by the Free Software Foundation; either version |
| 11 | * 2 of the License, or (at your option) any later version. |
| 12 | */ |
| 13 | |
| 14 | /* Changes: |
| 15 | * |
| 16 | * YOSHIFUJI Hideaki @USAGI |
| 17 | * reworked default router selection. |
| 18 | * - respect outgoing interface |
| 19 | * - select from (probably) reachable routers (i.e. |
| 20 | * routers in REACHABLE, STALE, DELAY or PROBE states). |
| 21 | * - always select the same router if it is (probably) |
| 22 | * reachable. otherwise, round-robin the list. |
YOSHIFUJI Hideaki | c0bece9 | 2006-08-23 17:23:25 -0700 | [diff] [blame] | 23 | * Ville Nuorvala |
| 24 | * Fixed routing subtrees. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | */ |
| 26 | |
Joe Perches | f321383 | 2012-05-15 14:11:53 +0000 | [diff] [blame] | 27 | #define pr_fmt(fmt) "IPv6: " fmt |
| 28 | |
Randy Dunlap | 4fc268d | 2006-01-11 12:17:47 -0800 | [diff] [blame] | 29 | #include <linux/capability.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | #include <linux/errno.h> |
Paul Gortmaker | bc3b2d7 | 2011-07-15 11:47:34 -0400 | [diff] [blame] | 31 | #include <linux/export.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #include <linux/types.h> |
| 33 | #include <linux/times.h> |
| 34 | #include <linux/socket.h> |
| 35 | #include <linux/sockios.h> |
| 36 | #include <linux/net.h> |
| 37 | #include <linux/route.h> |
| 38 | #include <linux/netdevice.h> |
| 39 | #include <linux/in6.h> |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 40 | #include <linux/mroute6.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | #include <linux/init.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | #include <linux/if_arp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | #include <linux/proc_fs.h> |
| 44 | #include <linux/seq_file.h> |
Daniel Lezcano | 5b7c931 | 2008-03-03 23:28:58 -0800 | [diff] [blame] | 45 | #include <linux/nsproxy.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 46 | #include <linux/slab.h> |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 47 | #include <linux/jhash.h> |
Eric W. Biederman | 457c4cb | 2007-09-12 12:01:34 +0200 | [diff] [blame] | 48 | #include <net/net_namespace.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | #include <net/snmp.h> |
| 50 | #include <net/ipv6.h> |
| 51 | #include <net/ip6_fib.h> |
| 52 | #include <net/ip6_route.h> |
| 53 | #include <net/ndisc.h> |
| 54 | #include <net/addrconf.h> |
| 55 | #include <net/tcp.h> |
| 56 | #include <linux/rtnetlink.h> |
| 57 | #include <net/dst.h> |
Jiri Benc | 904af04 | 2015-08-20 13:56:31 +0200 | [diff] [blame] | 58 | #include <net/dst_metadata.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | #include <net/xfrm.h> |
Tom Tucker | 8d71740 | 2006-07-30 20:43:36 -0700 | [diff] [blame] | 60 | #include <net/netevent.h> |
Thomas Graf | 21713eb | 2006-08-15 00:35:24 -0700 | [diff] [blame] | 61 | #include <net/netlink.h> |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 62 | #include <net/nexthop.h> |
Roopa Prabhu | 19e42e4 | 2015-07-21 10:43:48 +0200 | [diff] [blame] | 63 | #include <net/lwtunnel.h> |
Jiri Benc | 904af04 | 2015-08-20 13:56:31 +0200 | [diff] [blame] | 64 | #include <net/ip_tunnels.h> |
David Ahern | ca25449 | 2015-10-12 11:47:10 -0700 | [diff] [blame] | 65 | #include <net/l3mdev.h> |
Roopa Prabhu | eacb938 | 2018-05-22 14:03:28 -0700 | [diff] [blame] | 66 | #include <net/ip.h> |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 67 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | |
| 69 | #ifdef CONFIG_SYSCTL |
| 70 | #include <linux/sysctl.h> |
| 71 | #endif |
| 72 | |
David Ahern | 30d444d | 2018-05-23 17:08:48 -0700 | [diff] [blame] | 73 | static int ip6_rt_type_to_error(u8 fib6_type); |
| 74 | |
| 75 | #define CREATE_TRACE_POINTS |
| 76 | #include <trace/events/fib6.h> |
| 77 | EXPORT_TRACEPOINT_SYMBOL_GPL(fib6_table_lookup); |
| 78 | #undef CREATE_TRACE_POINTS |
| 79 | |
Hannes Frederic Sowa | afc154e | 2013-07-11 12:43:42 +0200 | [diff] [blame] | 80 | enum rt6_nud_state { |
Jiri Benc | 7e98056 | 2013-12-11 13:48:20 +0100 | [diff] [blame] | 81 | RT6_NUD_FAIL_HARD = -3, |
| 82 | RT6_NUD_FAIL_PROBE = -2, |
| 83 | RT6_NUD_FAIL_DO_RR = -1, |
Hannes Frederic Sowa | afc154e | 2013-07-11 12:43:42 +0200 | [diff] [blame] | 84 | RT6_NUD_SUCCEED = 1 |
| 85 | }; |
| 86 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | 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] | 88 | static unsigned int ip6_default_advmss(const struct dst_entry *dst); |
Steffen Klassert | ebb762f | 2011-11-23 02:12:51 +0000 | [diff] [blame] | 89 | static unsigned int ip6_mtu(const struct dst_entry *dst); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | static struct dst_entry *ip6_negative_advice(struct dst_entry *); |
| 91 | static void ip6_dst_destroy(struct dst_entry *); |
| 92 | static void ip6_dst_ifdown(struct dst_entry *, |
| 93 | struct net_device *dev, int how); |
Daniel Lezcano | 569d364 | 2008-01-18 03:56:57 -0800 | [diff] [blame] | 94 | static int ip6_dst_gc(struct dst_ops *ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | |
| 96 | static int ip6_pkt_discard(struct sk_buff *skb); |
Eric W. Biederman | ede2059 | 2015-10-07 16:48:47 -0500 | [diff] [blame] | 97 | 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] | 98 | static int ip6_pkt_prohibit(struct sk_buff *skb); |
Eric W. Biederman | ede2059 | 2015-10-07 16:48:47 -0500 | [diff] [blame] | 99 | 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] | 100 | static void ip6_link_failure(struct sk_buff *skb); |
David S. Miller | 6700c27 | 2012-07-17 03:29:28 -0700 | [diff] [blame] | 101 | static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk, |
| 102 | struct sk_buff *skb, u32 mtu); |
| 103 | static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, |
| 104 | struct sk_buff *skb); |
David Ahern | 702cea5 | 2019-04-09 14:41:13 -0700 | [diff] [blame] | 105 | static int rt6_score_route(const struct fib6_nh *nh, u32 fib6_flags, int oif, |
| 106 | int strict); |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 107 | static size_t rt6_nlmsg_size(struct fib6_info *rt); |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 108 | static int rt6_fill_node(struct net *net, struct sk_buff *skb, |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 109 | struct fib6_info *rt, struct dst_entry *dst, |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 110 | struct in6_addr *dest, struct in6_addr *src, |
David Ahern | 16a16cd | 2017-02-02 12:37:11 -0800 | [diff] [blame] | 111 | int iif, int type, u32 portid, u32 seq, |
| 112 | unsigned int flags); |
David Ahern | 7e4b512 | 2019-04-16 14:36:00 -0700 | [diff] [blame] | 113 | static struct rt6_info *rt6_find_cached_rt(const struct fib6_result *res, |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 114 | struct in6_addr *daddr, |
| 115 | struct in6_addr *saddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 117 | #ifdef CONFIG_IPV6_ROUTE_INFO |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 118 | static struct fib6_info *rt6_add_route_info(struct net *net, |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 119 | const struct in6_addr *prefix, int prefixlen, |
David Ahern | 830218c | 2016-10-24 10:52:35 -0700 | [diff] [blame] | 120 | const struct in6_addr *gwaddr, |
| 121 | struct net_device *dev, |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 122 | unsigned int pref); |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 123 | static struct fib6_info *rt6_get_route_info(struct net *net, |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 124 | const struct in6_addr *prefix, int prefixlen, |
David Ahern | 830218c | 2016-10-24 10:52:35 -0700 | [diff] [blame] | 125 | const struct in6_addr *gwaddr, |
| 126 | struct net_device *dev); |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 127 | #endif |
| 128 | |
Martin KaFai Lau | 8d0b94a | 2015-05-22 20:56:04 -0700 | [diff] [blame] | 129 | struct uncached_list { |
| 130 | spinlock_t lock; |
| 131 | struct list_head head; |
| 132 | }; |
| 133 | |
| 134 | static DEFINE_PER_CPU_ALIGNED(struct uncached_list, rt6_uncached_list); |
| 135 | |
Xin Long | 510c321 | 2018-02-14 19:06:02 +0800 | [diff] [blame] | 136 | void rt6_uncached_list_add(struct rt6_info *rt) |
Martin KaFai Lau | 8d0b94a | 2015-05-22 20:56:04 -0700 | [diff] [blame] | 137 | { |
| 138 | struct uncached_list *ul = raw_cpu_ptr(&rt6_uncached_list); |
| 139 | |
Martin KaFai Lau | 8d0b94a | 2015-05-22 20:56:04 -0700 | [diff] [blame] | 140 | rt->rt6i_uncached_list = ul; |
| 141 | |
| 142 | spin_lock_bh(&ul->lock); |
| 143 | list_add_tail(&rt->rt6i_uncached, &ul->head); |
| 144 | spin_unlock_bh(&ul->lock); |
| 145 | } |
| 146 | |
Xin Long | 510c321 | 2018-02-14 19:06:02 +0800 | [diff] [blame] | 147 | void rt6_uncached_list_del(struct rt6_info *rt) |
Martin KaFai Lau | 8d0b94a | 2015-05-22 20:56:04 -0700 | [diff] [blame] | 148 | { |
| 149 | if (!list_empty(&rt->rt6i_uncached)) { |
| 150 | struct uncached_list *ul = rt->rt6i_uncached_list; |
Wei Wang | 81eb844 | 2017-10-06 12:06:11 -0700 | [diff] [blame] | 151 | struct net *net = dev_net(rt->dst.dev); |
Martin KaFai Lau | 8d0b94a | 2015-05-22 20:56:04 -0700 | [diff] [blame] | 152 | |
| 153 | spin_lock_bh(&ul->lock); |
| 154 | list_del(&rt->rt6i_uncached); |
Wei Wang | 81eb844 | 2017-10-06 12:06:11 -0700 | [diff] [blame] | 155 | atomic_dec(&net->ipv6.rt6_stats->fib_rt_uncache); |
Martin KaFai Lau | 8d0b94a | 2015-05-22 20:56:04 -0700 | [diff] [blame] | 156 | spin_unlock_bh(&ul->lock); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | static void rt6_uncached_list_flush_dev(struct net *net, struct net_device *dev) |
| 161 | { |
| 162 | struct net_device *loopback_dev = net->loopback_dev; |
| 163 | int cpu; |
| 164 | |
Eric W. Biederman | e332bc6 | 2015-10-12 11:02:08 -0500 | [diff] [blame] | 165 | if (dev == loopback_dev) |
| 166 | return; |
| 167 | |
Martin KaFai Lau | 8d0b94a | 2015-05-22 20:56:04 -0700 | [diff] [blame] | 168 | for_each_possible_cpu(cpu) { |
| 169 | struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu); |
| 170 | struct rt6_info *rt; |
| 171 | |
| 172 | spin_lock_bh(&ul->lock); |
| 173 | list_for_each_entry(rt, &ul->head, rt6i_uncached) { |
| 174 | struct inet6_dev *rt_idev = rt->rt6i_idev; |
| 175 | struct net_device *rt_dev = rt->dst.dev; |
| 176 | |
Eric W. Biederman | e332bc6 | 2015-10-12 11:02:08 -0500 | [diff] [blame] | 177 | if (rt_idev->dev == dev) { |
Martin KaFai Lau | 8d0b94a | 2015-05-22 20:56:04 -0700 | [diff] [blame] | 178 | rt->rt6i_idev = in6_dev_get(loopback_dev); |
| 179 | in6_dev_put(rt_idev); |
| 180 | } |
| 181 | |
Eric W. Biederman | e332bc6 | 2015-10-12 11:02:08 -0500 | [diff] [blame] | 182 | if (rt_dev == dev) { |
Martin KaFai Lau | 8d0b94a | 2015-05-22 20:56:04 -0700 | [diff] [blame] | 183 | rt->dst.dev = loopback_dev; |
| 184 | dev_hold(rt->dst.dev); |
| 185 | dev_put(rt_dev); |
| 186 | } |
| 187 | } |
| 188 | spin_unlock_bh(&ul->lock); |
| 189 | } |
| 190 | } |
| 191 | |
David Ahern | f8a1b43 | 2018-04-17 17:33:21 -0700 | [diff] [blame] | 192 | 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] | 193 | struct sk_buff *skb, |
| 194 | const void *daddr) |
David S. Miller | 3923297 | 2012-01-26 15:22:32 -0500 | [diff] [blame] | 195 | { |
David S. Miller | a7563f3 | 2012-01-26 16:29:16 -0500 | [diff] [blame] | 196 | if (!ipv6_addr_any(p)) |
David S. Miller | 3923297 | 2012-01-26 15:22:32 -0500 | [diff] [blame] | 197 | return (const void *) p; |
David S. Miller | f894cbf | 2012-07-02 21:52:24 -0700 | [diff] [blame] | 198 | else if (skb) |
| 199 | return &ipv6_hdr(skb)->daddr; |
David S. Miller | 3923297 | 2012-01-26 15:22:32 -0500 | [diff] [blame] | 200 | return daddr; |
| 201 | } |
| 202 | |
David Ahern | f8a1b43 | 2018-04-17 17:33:21 -0700 | [diff] [blame] | 203 | struct neighbour *ip6_neigh_lookup(const struct in6_addr *gw, |
| 204 | struct net_device *dev, |
| 205 | struct sk_buff *skb, |
| 206 | const void *daddr) |
David S. Miller | d3aaeb3 | 2011-07-18 00:40:17 -0700 | [diff] [blame] | 207 | { |
David S. Miller | 3923297 | 2012-01-26 15:22:32 -0500 | [diff] [blame] | 208 | struct neighbour *n; |
| 209 | |
David Ahern | f8a1b43 | 2018-04-17 17:33:21 -0700 | [diff] [blame] | 210 | daddr = choose_neigh_daddr(gw, skb, daddr); |
| 211 | n = __ipv6_neigh_lookup(dev, daddr); |
David S. Miller | f83c779 | 2011-12-28 15:41:23 -0500 | [diff] [blame] | 212 | if (n) |
| 213 | return n; |
Stefano Brivio | 7adf324 | 2019-01-02 13:29:27 +0100 | [diff] [blame] | 214 | |
| 215 | n = neigh_create(&nd_tbl, daddr, dev); |
| 216 | return IS_ERR(n) ? NULL : n; |
David Ahern | f8a1b43 | 2018-04-17 17:33:21 -0700 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | static struct neighbour *ip6_dst_neigh_lookup(const struct dst_entry *dst, |
| 220 | struct sk_buff *skb, |
| 221 | const void *daddr) |
| 222 | { |
| 223 | const struct rt6_info *rt = container_of(dst, struct rt6_info, dst); |
| 224 | |
| 225 | return ip6_neigh_lookup(&rt->rt6i_gateway, dst->dev, skb, daddr); |
David S. Miller | f83c779 | 2011-12-28 15:41:23 -0500 | [diff] [blame] | 226 | } |
| 227 | |
Julian Anastasov | 63fca65 | 2017-02-06 23:14:15 +0200 | [diff] [blame] | 228 | static void ip6_confirm_neigh(const struct dst_entry *dst, const void *daddr) |
| 229 | { |
| 230 | struct net_device *dev = dst->dev; |
| 231 | struct rt6_info *rt = (struct rt6_info *)dst; |
| 232 | |
David Ahern | f8a1b43 | 2018-04-17 17:33:21 -0700 | [diff] [blame] | 233 | daddr = choose_neigh_daddr(&rt->rt6i_gateway, NULL, daddr); |
Julian Anastasov | 63fca65 | 2017-02-06 23:14:15 +0200 | [diff] [blame] | 234 | if (!daddr) |
| 235 | return; |
| 236 | if (dev->flags & (IFF_NOARP | IFF_LOOPBACK)) |
| 237 | return; |
| 238 | if (ipv6_addr_is_multicast((const struct in6_addr *)daddr)) |
| 239 | return; |
| 240 | __ipv6_confirm_neigh(dev, daddr); |
| 241 | } |
| 242 | |
Daniel Lezcano | 9a7ec3a | 2008-03-04 13:48:53 -0800 | [diff] [blame] | 243 | static struct dst_ops ip6_dst_ops_template = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | .family = AF_INET6, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | .gc = ip6_dst_gc, |
| 246 | .gc_thresh = 1024, |
| 247 | .check = ip6_dst_check, |
David S. Miller | 0dbaee3 | 2010-12-13 12:52:14 -0800 | [diff] [blame] | 248 | .default_advmss = ip6_default_advmss, |
Steffen Klassert | ebb762f | 2011-11-23 02:12:51 +0000 | [diff] [blame] | 249 | .mtu = ip6_mtu, |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 250 | .cow_metrics = dst_cow_metrics_generic, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | .destroy = ip6_dst_destroy, |
| 252 | .ifdown = ip6_dst_ifdown, |
| 253 | .negative_advice = ip6_negative_advice, |
| 254 | .link_failure = ip6_link_failure, |
| 255 | .update_pmtu = ip6_rt_update_pmtu, |
David S. Miller | 6e157b6 | 2012-07-12 00:05:02 -0700 | [diff] [blame] | 256 | .redirect = rt6_do_redirect, |
Eric W. Biederman | 9f8955c | 2015-10-07 16:48:39 -0500 | [diff] [blame] | 257 | .local_out = __ip6_local_out, |
David Ahern | f8a1b43 | 2018-04-17 17:33:21 -0700 | [diff] [blame] | 258 | .neigh_lookup = ip6_dst_neigh_lookup, |
Julian Anastasov | 63fca65 | 2017-02-06 23:14:15 +0200 | [diff] [blame] | 259 | .confirm_neigh = ip6_confirm_neigh, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | }; |
| 261 | |
Steffen Klassert | ebb762f | 2011-11-23 02:12:51 +0000 | [diff] [blame] | 262 | static unsigned int ip6_blackhole_mtu(const struct dst_entry *dst) |
Roland Dreier | ec831ea | 2011-01-31 13:16:00 -0800 | [diff] [blame] | 263 | { |
Steffen Klassert | 618f9bc | 2011-11-23 02:13:31 +0000 | [diff] [blame] | 264 | unsigned int mtu = dst_metric_raw(dst, RTAX_MTU); |
| 265 | |
| 266 | return mtu ? : dst->dev->mtu; |
Roland Dreier | ec831ea | 2011-01-31 13:16:00 -0800 | [diff] [blame] | 267 | } |
| 268 | |
David S. Miller | 6700c27 | 2012-07-17 03:29:28 -0700 | [diff] [blame] | 269 | static void ip6_rt_blackhole_update_pmtu(struct dst_entry *dst, struct sock *sk, |
| 270 | struct sk_buff *skb, u32 mtu) |
David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 271 | { |
| 272 | } |
| 273 | |
David S. Miller | 6700c27 | 2012-07-17 03:29:28 -0700 | [diff] [blame] | 274 | static void ip6_rt_blackhole_redirect(struct dst_entry *dst, struct sock *sk, |
| 275 | struct sk_buff *skb) |
David S. Miller | b587ee3 | 2012-07-12 00:39:24 -0700 | [diff] [blame] | 276 | { |
| 277 | } |
| 278 | |
David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 279 | static struct dst_ops ip6_dst_blackhole_ops = { |
| 280 | .family = AF_INET6, |
David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 281 | .destroy = ip6_dst_destroy, |
| 282 | .check = ip6_dst_check, |
Steffen Klassert | ebb762f | 2011-11-23 02:12:51 +0000 | [diff] [blame] | 283 | .mtu = ip6_blackhole_mtu, |
Eric Dumazet | 214f45c | 2011-02-18 11:39:01 -0800 | [diff] [blame] | 284 | .default_advmss = ip6_default_advmss, |
David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 285 | .update_pmtu = ip6_rt_blackhole_update_pmtu, |
David S. Miller | b587ee3 | 2012-07-12 00:39:24 -0700 | [diff] [blame] | 286 | .redirect = ip6_rt_blackhole_redirect, |
Martin KaFai Lau | 0a1f596 | 2015-10-15 16:39:58 -0700 | [diff] [blame] | 287 | .cow_metrics = dst_cow_metrics_generic, |
David Ahern | f8a1b43 | 2018-04-17 17:33:21 -0700 | [diff] [blame] | 288 | .neigh_lookup = ip6_dst_neigh_lookup, |
David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 289 | }; |
| 290 | |
David S. Miller | 62fa8a8 | 2011-01-26 20:51:05 -0800 | [diff] [blame] | 291 | static const u32 ip6_template_metrics[RTAX_MAX] = { |
Li RongQing | 14edd87 | 2012-10-24 14:01:18 +0800 | [diff] [blame] | 292 | [RTAX_HOPLIMIT - 1] = 0, |
David S. Miller | 62fa8a8 | 2011-01-26 20:51:05 -0800 | [diff] [blame] | 293 | }; |
| 294 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 295 | static const struct fib6_info fib6_null_entry_template = { |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 296 | .fib6_flags = (RTF_REJECT | RTF_NONEXTHOP), |
| 297 | .fib6_protocol = RTPROT_KERNEL, |
| 298 | .fib6_metric = ~(u32)0, |
| 299 | .fib6_ref = ATOMIC_INIT(1), |
David Ahern | 421842e | 2018-04-17 17:33:18 -0700 | [diff] [blame] | 300 | .fib6_type = RTN_UNREACHABLE, |
| 301 | .fib6_metrics = (struct dst_metrics *)&dst_default_metrics, |
| 302 | }; |
| 303 | |
Eric Dumazet | fb0af4c | 2012-09-11 21:47:51 +0000 | [diff] [blame] | 304 | static const struct rt6_info ip6_null_entry_template = { |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 305 | .dst = { |
| 306 | .__refcnt = ATOMIC_INIT(1), |
| 307 | .__use = 1, |
Nicolas Dichtel | 2c20cbd | 2012-09-10 22:09:47 +0000 | [diff] [blame] | 308 | .obsolete = DST_OBSOLETE_FORCE_CHK, |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 309 | .error = -ENETUNREACH, |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 310 | .input = ip6_pkt_discard, |
| 311 | .output = ip6_pkt_discard_out, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | }, |
| 313 | .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | }; |
| 315 | |
Thomas Graf | 101367c | 2006-08-04 03:39:02 -0700 | [diff] [blame] | 316 | #ifdef CONFIG_IPV6_MULTIPLE_TABLES |
| 317 | |
Eric Dumazet | fb0af4c | 2012-09-11 21:47:51 +0000 | [diff] [blame] | 318 | static const struct rt6_info ip6_prohibit_entry_template = { |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 319 | .dst = { |
| 320 | .__refcnt = ATOMIC_INIT(1), |
| 321 | .__use = 1, |
Nicolas Dichtel | 2c20cbd | 2012-09-10 22:09:47 +0000 | [diff] [blame] | 322 | .obsolete = DST_OBSOLETE_FORCE_CHK, |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 323 | .error = -EACCES, |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 324 | .input = ip6_pkt_prohibit, |
| 325 | .output = ip6_pkt_prohibit_out, |
Thomas Graf | 101367c | 2006-08-04 03:39:02 -0700 | [diff] [blame] | 326 | }, |
| 327 | .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP), |
Thomas Graf | 101367c | 2006-08-04 03:39:02 -0700 | [diff] [blame] | 328 | }; |
| 329 | |
Eric Dumazet | fb0af4c | 2012-09-11 21:47:51 +0000 | [diff] [blame] | 330 | static const struct rt6_info ip6_blk_hole_entry_template = { |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 331 | .dst = { |
| 332 | .__refcnt = ATOMIC_INIT(1), |
| 333 | .__use = 1, |
Nicolas Dichtel | 2c20cbd | 2012-09-10 22:09:47 +0000 | [diff] [blame] | 334 | .obsolete = DST_OBSOLETE_FORCE_CHK, |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 335 | .error = -EINVAL, |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 336 | .input = dst_discard, |
Eric W. Biederman | ede2059 | 2015-10-07 16:48:47 -0500 | [diff] [blame] | 337 | .output = dst_discard_out, |
Thomas Graf | 101367c | 2006-08-04 03:39:02 -0700 | [diff] [blame] | 338 | }, |
| 339 | .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP), |
Thomas Graf | 101367c | 2006-08-04 03:39:02 -0700 | [diff] [blame] | 340 | }; |
| 341 | |
| 342 | #endif |
| 343 | |
Martin KaFai Lau | ebfa45f | 2015-10-15 16:39:57 -0700 | [diff] [blame] | 344 | static void rt6_info_init(struct rt6_info *rt) |
| 345 | { |
| 346 | struct dst_entry *dst = &rt->dst; |
| 347 | |
| 348 | memset(dst + 1, 0, sizeof(*rt) - sizeof(*dst)); |
Martin KaFai Lau | ebfa45f | 2015-10-15 16:39:57 -0700 | [diff] [blame] | 349 | INIT_LIST_HEAD(&rt->rt6i_uncached); |
| 350 | } |
| 351 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | /* allocate dst with ip6_dst_ops */ |
David Ahern | 93531c6 | 2018-04-17 17:33:25 -0700 | [diff] [blame] | 353 | struct rt6_info *ip6_dst_alloc(struct net *net, struct net_device *dev, |
| 354 | int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | { |
David S. Miller | 97bab73 | 2012-06-09 22:36:36 -0700 | [diff] [blame] | 356 | struct rt6_info *rt = dst_alloc(&net->ipv6.ip6_dst_ops, dev, |
Wei Wang | b2a9c0e | 2017-06-17 10:42:41 -0700 | [diff] [blame] | 357 | 1, DST_OBSOLETE_FORCE_CHK, flags); |
David S. Miller | cf91166 | 2011-04-28 14:31:47 -0700 | [diff] [blame] | 358 | |
Wei Wang | 81eb844 | 2017-10-06 12:06:11 -0700 | [diff] [blame] | 359 | if (rt) { |
Martin KaFai Lau | ebfa45f | 2015-10-15 16:39:57 -0700 | [diff] [blame] | 360 | rt6_info_init(rt); |
Wei Wang | 81eb844 | 2017-10-06 12:06:11 -0700 | [diff] [blame] | 361 | atomic_inc(&net->ipv6.rt6_stats->fib_rt_alloc); |
| 362 | } |
Steffen Klassert | 8104891 | 2012-07-05 23:37:09 +0000 | [diff] [blame] | 363 | |
David S. Miller | cf91166 | 2011-04-28 14:31:47 -0700 | [diff] [blame] | 364 | return rt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | } |
David Ahern | 9ab179d | 2016-04-07 11:10:06 -0700 | [diff] [blame] | 366 | EXPORT_SYMBOL(ip6_dst_alloc); |
Martin KaFai Lau | d52d399 | 2015-05-22 20:56:06 -0700 | [diff] [blame] | 367 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | static void ip6_dst_destroy(struct dst_entry *dst) |
| 369 | { |
| 370 | struct rt6_info *rt = (struct rt6_info *)dst; |
David Ahern | a68886a | 2018-04-20 15:38:02 -0700 | [diff] [blame] | 371 | struct fib6_info *from; |
Martin KaFai Lau | 8d0b94a | 2015-05-22 20:56:04 -0700 | [diff] [blame] | 372 | struct inet6_dev *idev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | |
David Ahern | 1620a33 | 2018-10-04 20:07:54 -0700 | [diff] [blame] | 374 | ip_dst_metrics_put(dst); |
Martin KaFai Lau | 8d0b94a | 2015-05-22 20:56:04 -0700 | [diff] [blame] | 375 | rt6_uncached_list_del(rt); |
| 376 | |
| 377 | idev = rt->rt6i_idev; |
David S. Miller | 3830847 | 2011-12-03 18:02:47 -0500 | [diff] [blame] | 378 | if (idev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | rt->rt6i_idev = NULL; |
| 380 | in6_dev_put(idev); |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 381 | } |
Gao feng | 1716a96 | 2012-04-06 00:13:10 +0000 | [diff] [blame] | 382 | |
David Ahern | a68886a | 2018-04-20 15:38:02 -0700 | [diff] [blame] | 383 | rcu_read_lock(); |
| 384 | from = rcu_dereference(rt->from); |
| 385 | rcu_assign_pointer(rt->from, NULL); |
David Ahern | 93531c6 | 2018-04-17 17:33:25 -0700 | [diff] [blame] | 386 | fib6_info_release(from); |
David Ahern | a68886a | 2018-04-20 15:38:02 -0700 | [diff] [blame] | 387 | rcu_read_unlock(); |
David S. Miller | b341936 | 2010-11-30 12:27:11 -0800 | [diff] [blame] | 388 | } |
| 389 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev, |
| 391 | int how) |
| 392 | { |
| 393 | struct rt6_info *rt = (struct rt6_info *)dst; |
| 394 | struct inet6_dev *idev = rt->rt6i_idev; |
Denis V. Lunev | 5a3e55d | 2007-12-07 00:38:10 -0800 | [diff] [blame] | 395 | struct net_device *loopback_dev = |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 396 | dev_net(dev)->loopback_dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | |
Wei Wang | e5645f5 | 2017-08-14 10:44:59 -0700 | [diff] [blame] | 398 | if (idev && idev->dev != loopback_dev) { |
| 399 | struct inet6_dev *loopback_idev = in6_dev_get(loopback_dev); |
| 400 | if (loopback_idev) { |
| 401 | rt->rt6i_idev = loopback_idev; |
| 402 | in6_dev_put(idev); |
David S. Miller | 97cac08 | 2012-07-02 22:43:47 -0700 | [diff] [blame] | 403 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | } |
| 405 | } |
| 406 | |
Martin KaFai Lau | 5973fb1 | 2015-11-11 11:51:07 -0800 | [diff] [blame] | 407 | static bool __rt6_check_expired(const struct rt6_info *rt) |
| 408 | { |
| 409 | if (rt->rt6i_flags & RTF_EXPIRES) |
| 410 | return time_after(jiffies, rt->dst.expires); |
| 411 | else |
| 412 | return false; |
| 413 | } |
| 414 | |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 415 | static bool rt6_check_expired(const struct rt6_info *rt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | { |
David Ahern | a68886a | 2018-04-20 15:38:02 -0700 | [diff] [blame] | 417 | struct fib6_info *from; |
| 418 | |
| 419 | from = rcu_dereference(rt->from); |
| 420 | |
Gao feng | 1716a96 | 2012-04-06 00:13:10 +0000 | [diff] [blame] | 421 | if (rt->rt6i_flags & RTF_EXPIRES) { |
| 422 | if (time_after(jiffies, rt->dst.expires)) |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 423 | return true; |
David Ahern | a68886a | 2018-04-20 15:38:02 -0700 | [diff] [blame] | 424 | } else if (from) { |
Xin Long | 1e2ea8a | 2017-08-26 20:10:10 +0800 | [diff] [blame] | 425 | return rt->dst.obsolete != DST_OBSOLETE_FORCE_CHK || |
David Ahern | a68886a | 2018-04-20 15:38:02 -0700 | [diff] [blame] | 426 | fib6_check_expired(from); |
Gao feng | 1716a96 | 2012-04-06 00:13:10 +0000 | [diff] [blame] | 427 | } |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 428 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | } |
| 430 | |
David Ahern | b1d4099 | 2019-04-16 14:35:59 -0700 | [diff] [blame] | 431 | void fib6_select_path(const struct net *net, struct fib6_result *res, |
| 432 | struct flowi6 *fl6, int oif, bool have_oif_match, |
| 433 | const struct sk_buff *skb, int strict) |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 434 | { |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 435 | struct fib6_info *sibling, *next_sibling; |
David Ahern | b1d4099 | 2019-04-16 14:35:59 -0700 | [diff] [blame] | 436 | struct fib6_info *match = res->f6i; |
| 437 | |
| 438 | if (!match->fib6_nsiblings || have_oif_match) |
| 439 | goto out; |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 440 | |
Jakub Sitnicki | b673d6c | 2017-08-23 09:58:31 +0200 | [diff] [blame] | 441 | /* We might have already computed the hash for ICMPv6 errors. In such |
| 442 | * case it will always be non-zero. Otherwise now is the time to do it. |
| 443 | */ |
| 444 | if (!fl6->mp_hash) |
David Ahern | b4bac17 | 2018-03-02 08:32:18 -0800 | [diff] [blame] | 445 | fl6->mp_hash = rt6_multipath_hash(net, fl6, skb, NULL); |
Jakub Sitnicki | b673d6c | 2017-08-23 09:58:31 +0200 | [diff] [blame] | 446 | |
David Ahern | ad1601a | 2019-03-27 20:53:56 -0700 | [diff] [blame] | 447 | 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] | 448 | goto out; |
Ido Schimmel | bbfcd77 | 2017-11-21 09:50:12 +0200 | [diff] [blame] | 449 | |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 450 | list_for_each_entry_safe(sibling, next_sibling, &match->fib6_siblings, |
| 451 | fib6_siblings) { |
David Ahern | 702cea5 | 2019-04-09 14:41:13 -0700 | [diff] [blame] | 452 | const struct fib6_nh *nh = &sibling->fib6_nh; |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 453 | int nh_upper_bound; |
| 454 | |
David Ahern | 702cea5 | 2019-04-09 14:41:13 -0700 | [diff] [blame] | 455 | nh_upper_bound = atomic_read(&nh->fib_nh_upper_bound); |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 456 | if (fl6->mp_hash > nh_upper_bound) |
Ido Schimmel | 3d709f6 | 2018-01-09 16:40:27 +0200 | [diff] [blame] | 457 | continue; |
David Ahern | 702cea5 | 2019-04-09 14:41:13 -0700 | [diff] [blame] | 458 | if (rt6_score_route(nh, sibling->fib6_flags, oif, strict) < 0) |
Ido Schimmel | 3d709f6 | 2018-01-09 16:40:27 +0200 | [diff] [blame] | 459 | break; |
| 460 | match = sibling; |
| 461 | break; |
| 462 | } |
| 463 | |
David Ahern | b1d4099 | 2019-04-16 14:35:59 -0700 | [diff] [blame] | 464 | out: |
| 465 | res->f6i = match; |
| 466 | res->nh = &match->fib6_nh; |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 467 | } |
| 468 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | /* |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 470 | * Route lookup. rcu_read_lock() should be held. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | */ |
| 472 | |
David Ahern | 0c59d00 | 2019-04-09 14:41:18 -0700 | [diff] [blame] | 473 | static bool __rt6_device_match(struct net *net, const struct fib6_nh *nh, |
| 474 | const struct in6_addr *saddr, int oif, int flags) |
| 475 | { |
| 476 | const struct net_device *dev; |
| 477 | |
| 478 | if (nh->fib_nh_flags & RTNH_F_DEAD) |
| 479 | return false; |
| 480 | |
| 481 | dev = nh->fib_nh_dev; |
| 482 | if (oif) { |
| 483 | if (dev->ifindex == oif) |
| 484 | return true; |
| 485 | } else { |
| 486 | if (ipv6_chk_addr(net, saddr, dev, |
| 487 | flags & RT6_LOOKUP_F_IFACE)) |
| 488 | return true; |
| 489 | } |
| 490 | |
| 491 | return false; |
| 492 | } |
| 493 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 494 | static inline struct fib6_info *rt6_device_match(struct net *net, |
| 495 | struct fib6_info *rt, |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 496 | const struct in6_addr *saddr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | int oif, |
YOSHIFUJI Hideaki | d420895 | 2008-06-27 20:14:54 -0700 | [diff] [blame] | 498 | int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | { |
David Ahern | 0c59d00 | 2019-04-09 14:41:18 -0700 | [diff] [blame] | 500 | const struct fib6_nh *nh; |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 501 | struct fib6_info *sprt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 503 | if (!oif && ipv6_addr_any(saddr) && |
David Ahern | ad1601a | 2019-03-27 20:53:56 -0700 | [diff] [blame] | 504 | !(rt->fib6_nh.fib_nh_flags & RTNH_F_DEAD)) |
Ido Schimmel | 8067bb8 | 2018-01-07 12:45:09 +0200 | [diff] [blame] | 505 | return rt; |
YOSHIFUJI Hideaki | dd3abc4 | 2008-07-02 18:30:18 +0900 | [diff] [blame] | 506 | |
David Ahern | 8fb11a9 | 2018-05-04 13:54:24 -0700 | [diff] [blame] | 507 | for (sprt = rt; sprt; sprt = rcu_dereference(sprt->fib6_next)) { |
David Ahern | 0c59d00 | 2019-04-09 14:41:18 -0700 | [diff] [blame] | 508 | nh = &sprt->fib6_nh; |
| 509 | if (__rt6_device_match(net, nh, saddr, oif, flags)) |
| 510 | return sprt; |
YOSHIFUJI Hideaki | dd3abc4 | 2008-07-02 18:30:18 +0900 | [diff] [blame] | 511 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | |
David Ahern | eea68cd | 2018-04-18 15:39:02 -0700 | [diff] [blame] | 513 | if (oif && flags & RT6_LOOKUP_F_IFACE) |
| 514 | return net->ipv6.fib6_null_entry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 515 | |
David Ahern | ad1601a | 2019-03-27 20:53:56 -0700 | [diff] [blame] | 516 | return rt->fib6_nh.fib_nh_flags & RTNH_F_DEAD ? net->ipv6.fib6_null_entry : rt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | } |
| 518 | |
YOSHIFUJI Hideaki | 2709725 | 2006-03-20 17:05:13 -0800 | [diff] [blame] | 519 | #ifdef CONFIG_IPV6_ROUTER_PREF |
Hannes Frederic Sowa | c2f17e8 | 2013-10-21 06:17:15 +0200 | [diff] [blame] | 520 | struct __rt6_probe_work { |
| 521 | struct work_struct work; |
| 522 | struct in6_addr target; |
| 523 | struct net_device *dev; |
| 524 | }; |
| 525 | |
| 526 | static void rt6_probe_deferred(struct work_struct *w) |
| 527 | { |
| 528 | struct in6_addr mcaddr; |
| 529 | struct __rt6_probe_work *work = |
| 530 | container_of(w, struct __rt6_probe_work, work); |
| 531 | |
| 532 | addrconf_addr_solict_mult(&work->target, &mcaddr); |
Erik Nordmark | adc176c | 2016-12-02 14:00:08 -0800 | [diff] [blame] | 533 | ndisc_send_ns(work->dev, &work->target, &mcaddr, NULL, 0); |
Hannes Frederic Sowa | c2f17e8 | 2013-10-21 06:17:15 +0200 | [diff] [blame] | 534 | dev_put(work->dev); |
Michael Büsch | 662f553 | 2015-02-08 10:14:07 +0100 | [diff] [blame] | 535 | kfree(work); |
Hannes Frederic Sowa | c2f17e8 | 2013-10-21 06:17:15 +0200 | [diff] [blame] | 536 | } |
| 537 | |
David Ahern | cc3a86c | 2019-04-09 14:41:12 -0700 | [diff] [blame] | 538 | static void rt6_probe(struct fib6_nh *fib6_nh) |
YOSHIFUJI Hideaki | 2709725 | 2006-03-20 17:05:13 -0800 | [diff] [blame] | 539 | { |
Sabrina Dubroca | f547fac | 2018-10-12 16:22:47 +0200 | [diff] [blame] | 540 | struct __rt6_probe_work *work = NULL; |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 541 | const struct in6_addr *nh_gw; |
Eric Dumazet | f2c31e3 | 2011-07-29 19:00:53 +0000 | [diff] [blame] | 542 | struct neighbour *neigh; |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 543 | struct net_device *dev; |
Sabrina Dubroca | f547fac | 2018-10-12 16:22:47 +0200 | [diff] [blame] | 544 | struct inet6_dev *idev; |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 545 | |
YOSHIFUJI Hideaki | 2709725 | 2006-03-20 17:05:13 -0800 | [diff] [blame] | 546 | /* |
| 547 | * Okay, this does not seem to be appropriate |
| 548 | * for now, however, we need to check if it |
| 549 | * is really so; aka Router Reachability Probing. |
| 550 | * |
| 551 | * Router Reachability Probe MUST be rate-limited |
| 552 | * to no more than one per minute. |
| 553 | */ |
David Ahern | cc3a86c | 2019-04-09 14:41:12 -0700 | [diff] [blame] | 554 | if (fib6_nh->fib_nh_gw_family) |
Amerigo Wang | fdd6681 | 2012-09-10 02:48:44 +0000 | [diff] [blame] | 555 | return; |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 556 | |
David Ahern | cc3a86c | 2019-04-09 14:41:12 -0700 | [diff] [blame] | 557 | nh_gw = &fib6_nh->fib_nh_gw6; |
| 558 | dev = fib6_nh->fib_nh_dev; |
YOSHIFUJI Hideaki / 吉藤英明 | 2152cae | 2013-01-17 12:53:43 +0000 | [diff] [blame] | 559 | rcu_read_lock_bh(); |
Sabrina Dubroca | f547fac | 2018-10-12 16:22:47 +0200 | [diff] [blame] | 560 | idev = __in6_dev_get(dev); |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 561 | neigh = __ipv6_neigh_lookup_noref(dev, nh_gw); |
YOSHIFUJI Hideaki / 吉藤英明 | 2152cae | 2013-01-17 12:53:43 +0000 | [diff] [blame] | 562 | if (neigh) { |
Martin KaFai Lau | 8d6c31b | 2015-07-24 09:57:43 -0700 | [diff] [blame] | 563 | if (neigh->nud_state & NUD_VALID) |
| 564 | goto out; |
| 565 | |
YOSHIFUJI Hideaki / 吉藤英明 | 2152cae | 2013-01-17 12:53:43 +0000 | [diff] [blame] | 566 | write_lock(&neigh->lock); |
Martin KaFai Lau | 990edb4 | 2015-07-24 09:57:42 -0700 | [diff] [blame] | 567 | if (!(neigh->nud_state & NUD_VALID) && |
| 568 | time_after(jiffies, |
David Ahern | dcd1f57 | 2018-04-18 15:39:05 -0700 | [diff] [blame] | 569 | neigh->updated + idev->cnf.rtr_probe_interval)) { |
Martin KaFai Lau | 990edb4 | 2015-07-24 09:57:42 -0700 | [diff] [blame] | 570 | work = kmalloc(sizeof(*work), GFP_ATOMIC); |
| 571 | if (work) |
| 572 | __neigh_set_probe_once(neigh); |
Hannes Frederic Sowa | c2f17e8 | 2013-10-21 06:17:15 +0200 | [diff] [blame] | 573 | } |
YOSHIFUJI Hideaki / 吉藤英明 | 2152cae | 2013-01-17 12:53:43 +0000 | [diff] [blame] | 574 | write_unlock(&neigh->lock); |
David Ahern | cc3a86c | 2019-04-09 14:41:12 -0700 | [diff] [blame] | 575 | } else if (time_after(jiffies, fib6_nh->last_probe + |
Sabrina Dubroca | f547fac | 2018-10-12 16:22:47 +0200 | [diff] [blame] | 576 | idev->cnf.rtr_probe_interval)) { |
Martin KaFai Lau | 990edb4 | 2015-07-24 09:57:42 -0700 | [diff] [blame] | 577 | work = kmalloc(sizeof(*work), GFP_ATOMIC); |
Eric Dumazet | f2c31e3 | 2011-07-29 19:00:53 +0000 | [diff] [blame] | 578 | } |
Martin KaFai Lau | 990edb4 | 2015-07-24 09:57:42 -0700 | [diff] [blame] | 579 | |
| 580 | if (work) { |
David Ahern | cc3a86c | 2019-04-09 14:41:12 -0700 | [diff] [blame] | 581 | fib6_nh->last_probe = jiffies; |
Martin KaFai Lau | 990edb4 | 2015-07-24 09:57:42 -0700 | [diff] [blame] | 582 | INIT_WORK(&work->work, rt6_probe_deferred); |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 583 | work->target = *nh_gw; |
| 584 | dev_hold(dev); |
| 585 | work->dev = dev; |
Martin KaFai Lau | 990edb4 | 2015-07-24 09:57:42 -0700 | [diff] [blame] | 586 | schedule_work(&work->work); |
| 587 | } |
| 588 | |
Martin KaFai Lau | 8d6c31b | 2015-07-24 09:57:43 -0700 | [diff] [blame] | 589 | out: |
YOSHIFUJI Hideaki / 吉藤英明 | 2152cae | 2013-01-17 12:53:43 +0000 | [diff] [blame] | 590 | rcu_read_unlock_bh(); |
YOSHIFUJI Hideaki | 2709725 | 2006-03-20 17:05:13 -0800 | [diff] [blame] | 591 | } |
| 592 | #else |
David Ahern | cc3a86c | 2019-04-09 14:41:12 -0700 | [diff] [blame] | 593 | static inline void rt6_probe(struct fib6_nh *fib6_nh) |
YOSHIFUJI Hideaki | 2709725 | 2006-03-20 17:05:13 -0800 | [diff] [blame] | 594 | { |
YOSHIFUJI Hideaki | 2709725 | 2006-03-20 17:05:13 -0800 | [diff] [blame] | 595 | } |
| 596 | #endif |
| 597 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 598 | /* |
YOSHIFUJI Hideaki | 554cfb7 | 2006-03-20 17:00:26 -0800 | [diff] [blame] | 599 | * Default Router Selection (RFC 2461 6.3.6) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 600 | */ |
David Ahern | 1ba9a89 | 2019-04-09 14:41:10 -0700 | [diff] [blame] | 601 | 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] | 602 | { |
Hannes Frederic Sowa | afc154e | 2013-07-11 12:43:42 +0200 | [diff] [blame] | 603 | enum rt6_nud_state ret = RT6_NUD_FAIL_HARD; |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 604 | struct neighbour *neigh; |
Eric Dumazet | f2c31e3 | 2011-07-29 19:00:53 +0000 | [diff] [blame] | 605 | |
YOSHIFUJI Hideaki / 吉藤英明 | 145a362 | 2013-01-17 12:53:38 +0000 | [diff] [blame] | 606 | rcu_read_lock_bh(); |
David Ahern | 1ba9a89 | 2019-04-09 14:41:10 -0700 | [diff] [blame] | 607 | neigh = __ipv6_neigh_lookup_noref(fib6_nh->fib_nh_dev, |
| 608 | &fib6_nh->fib_nh_gw6); |
YOSHIFUJI Hideaki / 吉藤英明 | 145a362 | 2013-01-17 12:53:38 +0000 | [diff] [blame] | 609 | if (neigh) { |
| 610 | read_lock(&neigh->lock); |
YOSHIFUJI Hideaki | 554cfb7 | 2006-03-20 17:00:26 -0800 | [diff] [blame] | 611 | if (neigh->nud_state & NUD_VALID) |
Hannes Frederic Sowa | afc154e | 2013-07-11 12:43:42 +0200 | [diff] [blame] | 612 | ret = RT6_NUD_SUCCEED; |
YOSHIFUJI Hideaki | 398bcbe | 2008-01-19 00:35:16 -0800 | [diff] [blame] | 613 | #ifdef CONFIG_IPV6_ROUTER_PREF |
Paul Marks | a5a81f0 | 2012-12-03 10:26:54 +0000 | [diff] [blame] | 614 | else if (!(neigh->nud_state & NUD_FAILED)) |
Hannes Frederic Sowa | afc154e | 2013-07-11 12:43:42 +0200 | [diff] [blame] | 615 | ret = RT6_NUD_SUCCEED; |
Jiri Benc | 7e98056 | 2013-12-11 13:48:20 +0100 | [diff] [blame] | 616 | else |
| 617 | ret = RT6_NUD_FAIL_PROBE; |
YOSHIFUJI Hideaki | 398bcbe | 2008-01-19 00:35:16 -0800 | [diff] [blame] | 618 | #endif |
YOSHIFUJI Hideaki / 吉藤英明 | 145a362 | 2013-01-17 12:53:38 +0000 | [diff] [blame] | 619 | read_unlock(&neigh->lock); |
Hannes Frederic Sowa | afc154e | 2013-07-11 12:43:42 +0200 | [diff] [blame] | 620 | } else { |
| 621 | ret = IS_ENABLED(CONFIG_IPV6_ROUTER_PREF) ? |
Jiri Benc | 7e98056 | 2013-12-11 13:48:20 +0100 | [diff] [blame] | 622 | RT6_NUD_SUCCEED : RT6_NUD_FAIL_DO_RR; |
Paul Marks | a5a81f0 | 2012-12-03 10:26:54 +0000 | [diff] [blame] | 623 | } |
YOSHIFUJI Hideaki / 吉藤英明 | 145a362 | 2013-01-17 12:53:38 +0000 | [diff] [blame] | 624 | rcu_read_unlock_bh(); |
| 625 | |
Paul Marks | a5a81f0 | 2012-12-03 10:26:54 +0000 | [diff] [blame] | 626 | return ret; |
YOSHIFUJI Hideaki | 554cfb7 | 2006-03-20 17:00:26 -0800 | [diff] [blame] | 627 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | |
David Ahern | 702cea5 | 2019-04-09 14:41:13 -0700 | [diff] [blame] | 629 | static int rt6_score_route(const struct fib6_nh *nh, u32 fib6_flags, int oif, |
| 630 | int strict) |
YOSHIFUJI Hideaki | 554cfb7 | 2006-03-20 17:00:26 -0800 | [diff] [blame] | 631 | { |
David Ahern | 6e1809a | 2019-04-09 14:41:11 -0700 | [diff] [blame] | 632 | int m = 0; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 633 | |
David Ahern | 6e1809a | 2019-04-09 14:41:11 -0700 | [diff] [blame] | 634 | if (!oif || nh->fib_nh_dev->ifindex == oif) |
| 635 | m = 2; |
| 636 | |
YOSHIFUJI Hideaki | 77d16f4 | 2006-08-23 17:25:05 -0700 | [diff] [blame] | 637 | if (!m && (strict & RT6_LOOKUP_F_IFACE)) |
Hannes Frederic Sowa | afc154e | 2013-07-11 12:43:42 +0200 | [diff] [blame] | 638 | return RT6_NUD_FAIL_HARD; |
YOSHIFUJI Hideaki | ebacaaa | 2006-03-20 17:04:53 -0800 | [diff] [blame] | 639 | #ifdef CONFIG_IPV6_ROUTER_PREF |
David Ahern | 702cea5 | 2019-04-09 14:41:13 -0700 | [diff] [blame] | 640 | m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(fib6_flags)) << 2; |
YOSHIFUJI Hideaki | ebacaaa | 2006-03-20 17:04:53 -0800 | [diff] [blame] | 641 | #endif |
David Ahern | 1ba9a89 | 2019-04-09 14:41:10 -0700 | [diff] [blame] | 642 | if ((strict & RT6_LOOKUP_F_REACHABLE) && |
David Ahern | 702cea5 | 2019-04-09 14:41:13 -0700 | [diff] [blame] | 643 | !(fib6_flags & RTF_NONEXTHOP) && nh->fib_nh_gw_family) { |
David Ahern | 1ba9a89 | 2019-04-09 14:41:10 -0700 | [diff] [blame] | 644 | int n = rt6_check_neigh(nh); |
Hannes Frederic Sowa | afc154e | 2013-07-11 12:43:42 +0200 | [diff] [blame] | 645 | if (n < 0) |
| 646 | return n; |
| 647 | } |
YOSHIFUJI Hideaki | 554cfb7 | 2006-03-20 17:00:26 -0800 | [diff] [blame] | 648 | return m; |
| 649 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 650 | |
David Ahern | 28679ed | 2019-04-09 14:41:14 -0700 | [diff] [blame] | 651 | static bool find_match(struct fib6_nh *nh, u32 fib6_flags, |
| 652 | int oif, int strict, int *mpri, bool *do_rr) |
YOSHIFUJI Hideaki | 554cfb7 | 2006-03-20 17:00:26 -0800 | [diff] [blame] | 653 | { |
Hannes Frederic Sowa | afc154e | 2013-07-11 12:43:42 +0200 | [diff] [blame] | 654 | bool match_do_rr = false; |
David Ahern | 28679ed | 2019-04-09 14:41:14 -0700 | [diff] [blame] | 655 | bool rc = false; |
| 656 | int m; |
Andy Gospodarek | 35103d1 | 2015-08-13 10:39:01 -0400 | [diff] [blame] | 657 | |
David Ahern | 28679ed | 2019-04-09 14:41:14 -0700 | [diff] [blame] | 658 | if (nh->fib_nh_flags & RTNH_F_DEAD) |
Ido Schimmel | 8067bb8 | 2018-01-07 12:45:09 +0200 | [diff] [blame] | 659 | goto out; |
| 660 | |
David Ahern | 28679ed | 2019-04-09 14:41:14 -0700 | [diff] [blame] | 661 | if (ip6_ignore_linkdown(nh->fib_nh_dev) && |
| 662 | nh->fib_nh_flags & RTNH_F_LINKDOWN && |
David Ahern | d5d32e4 | 2016-10-24 12:27:23 -0700 | [diff] [blame] | 663 | !(strict & RT6_LOOKUP_F_IGNORE_LINKSTATE)) |
Andy Gospodarek | 35103d1 | 2015-08-13 10:39:01 -0400 | [diff] [blame] | 664 | goto out; |
David S. Miller | f11e665 | 2007-03-24 20:36:25 -0700 | [diff] [blame] | 665 | |
David Ahern | 28679ed | 2019-04-09 14:41:14 -0700 | [diff] [blame] | 666 | m = rt6_score_route(nh, fib6_flags, oif, strict); |
Jiri Benc | 7e98056 | 2013-12-11 13:48:20 +0100 | [diff] [blame] | 667 | if (m == RT6_NUD_FAIL_DO_RR) { |
Hannes Frederic Sowa | afc154e | 2013-07-11 12:43:42 +0200 | [diff] [blame] | 668 | match_do_rr = true; |
| 669 | m = 0; /* lowest valid score */ |
Jiri Benc | 7e98056 | 2013-12-11 13:48:20 +0100 | [diff] [blame] | 670 | } else if (m == RT6_NUD_FAIL_HARD) { |
David S. Miller | f11e665 | 2007-03-24 20:36:25 -0700 | [diff] [blame] | 671 | goto out; |
David S. Miller | f11e665 | 2007-03-24 20:36:25 -0700 | [diff] [blame] | 672 | } |
| 673 | |
Hannes Frederic Sowa | afc154e | 2013-07-11 12:43:42 +0200 | [diff] [blame] | 674 | if (strict & RT6_LOOKUP_F_REACHABLE) |
David Ahern | 28679ed | 2019-04-09 14:41:14 -0700 | [diff] [blame] | 675 | rt6_probe(nh); |
Hannes Frederic Sowa | afc154e | 2013-07-11 12:43:42 +0200 | [diff] [blame] | 676 | |
Jiri Benc | 7e98056 | 2013-12-11 13:48:20 +0100 | [diff] [blame] | 677 | /* 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] | 678 | if (m > *mpri) { |
| 679 | *do_rr = match_do_rr; |
| 680 | *mpri = m; |
David Ahern | 28679ed | 2019-04-09 14:41:14 -0700 | [diff] [blame] | 681 | rc = true; |
Hannes Frederic Sowa | afc154e | 2013-07-11 12:43:42 +0200 | [diff] [blame] | 682 | } |
David S. Miller | f11e665 | 2007-03-24 20:36:25 -0700 | [diff] [blame] | 683 | out: |
David Ahern | 28679ed | 2019-04-09 14:41:14 -0700 | [diff] [blame] | 684 | return rc; |
David S. Miller | f11e665 | 2007-03-24 20:36:25 -0700 | [diff] [blame] | 685 | } |
| 686 | |
David Ahern | 30c15f0 | 2019-04-09 14:41:15 -0700 | [diff] [blame] | 687 | static void __find_rr_leaf(struct fib6_info *rt_start, |
| 688 | struct fib6_info *nomatch, u32 metric, |
| 689 | struct fib6_info **match, struct fib6_info **cont, |
| 690 | int oif, int strict, bool *do_rr, int *mpri) |
David S. Miller | f11e665 | 2007-03-24 20:36:25 -0700 | [diff] [blame] | 691 | { |
David Ahern | 30c15f0 | 2019-04-09 14:41:15 -0700 | [diff] [blame] | 692 | struct fib6_info *rt; |
| 693 | |
| 694 | for (rt = rt_start; |
| 695 | rt && rt != nomatch; |
| 696 | rt = rcu_dereference(rt->fib6_next)) { |
| 697 | struct fib6_nh *nh; |
| 698 | |
| 699 | if (cont && rt->fib6_metric != metric) { |
| 700 | *cont = rt; |
| 701 | return; |
| 702 | } |
| 703 | |
| 704 | if (fib6_check_expired(rt)) |
| 705 | continue; |
| 706 | |
| 707 | nh = &rt->fib6_nh; |
| 708 | if (find_match(nh, rt->fib6_flags, oif, strict, mpri, do_rr)) |
| 709 | *match = rt; |
| 710 | } |
| 711 | } |
| 712 | |
| 713 | static struct fib6_info *find_rr_leaf(struct fib6_node *fn, |
| 714 | struct fib6_info *leaf, |
| 715 | struct fib6_info *rr_head, |
| 716 | u32 metric, int oif, int strict, |
| 717 | bool *do_rr) |
| 718 | { |
| 719 | struct fib6_info *match = NULL, *cont = NULL; |
YOSHIFUJI Hideaki | 554cfb7 | 2006-03-20 17:00:26 -0800 | [diff] [blame] | 720 | int mpri = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 721 | |
David Ahern | 30c15f0 | 2019-04-09 14:41:15 -0700 | [diff] [blame] | 722 | __find_rr_leaf(rr_head, NULL, metric, &match, &cont, |
| 723 | oif, strict, do_rr, &mpri); |
Steffen Klassert | 9fbdcfa | 2015-04-28 13:03:04 -0700 | [diff] [blame] | 724 | |
David Ahern | 30c15f0 | 2019-04-09 14:41:15 -0700 | [diff] [blame] | 725 | __find_rr_leaf(leaf, rr_head, metric, &match, &cont, |
| 726 | oif, strict, do_rr, &mpri); |
Steffen Klassert | 9fbdcfa | 2015-04-28 13:03:04 -0700 | [diff] [blame] | 727 | |
| 728 | if (match || !cont) |
| 729 | return match; |
| 730 | |
David Ahern | 30c15f0 | 2019-04-09 14:41:15 -0700 | [diff] [blame] | 731 | __find_rr_leaf(cont, NULL, metric, &match, NULL, |
| 732 | oif, strict, do_rr, &mpri); |
YOSHIFUJI Hideaki | 554cfb7 | 2006-03-20 17:00:26 -0800 | [diff] [blame] | 733 | |
David S. Miller | f11e665 | 2007-03-24 20:36:25 -0700 | [diff] [blame] | 734 | return match; |
| 735 | } |
YOSHIFUJI Hideaki | 554cfb7 | 2006-03-20 17:00:26 -0800 | [diff] [blame] | 736 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 737 | static struct fib6_info *rt6_select(struct net *net, struct fib6_node *fn, |
Wei Wang | 8d1040e | 2017-10-06 12:06:08 -0700 | [diff] [blame] | 738 | int oif, int strict) |
David S. Miller | f11e665 | 2007-03-24 20:36:25 -0700 | [diff] [blame] | 739 | { |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 740 | struct fib6_info *leaf = rcu_dereference(fn->leaf); |
| 741 | struct fib6_info *match, *rt0; |
Hannes Frederic Sowa | afc154e | 2013-07-11 12:43:42 +0200 | [diff] [blame] | 742 | bool do_rr = false; |
Wei Wang | 17ecf59 | 2017-10-06 12:06:09 -0700 | [diff] [blame] | 743 | int key_plen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 744 | |
David Ahern | 421842e | 2018-04-17 17:33:18 -0700 | [diff] [blame] | 745 | if (!leaf || leaf == net->ipv6.fib6_null_entry) |
| 746 | return net->ipv6.fib6_null_entry; |
Wei Wang | 8d1040e | 2017-10-06 12:06:08 -0700 | [diff] [blame] | 747 | |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 748 | rt0 = rcu_dereference(fn->rr_ptr); |
David S. Miller | f11e665 | 2007-03-24 20:36:25 -0700 | [diff] [blame] | 749 | if (!rt0) |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 750 | rt0 = leaf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 751 | |
Wei Wang | 17ecf59 | 2017-10-06 12:06:09 -0700 | [diff] [blame] | 752 | /* Double check to make sure fn is not an intermediate node |
| 753 | * and fn->leaf does not points to its child's leaf |
| 754 | * (This might happen if all routes under fn are deleted from |
| 755 | * the tree and fib6_repair_tree() is called on the node.) |
| 756 | */ |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 757 | key_plen = rt0->fib6_dst.plen; |
Wei Wang | 17ecf59 | 2017-10-06 12:06:09 -0700 | [diff] [blame] | 758 | #ifdef CONFIG_IPV6_SUBTREES |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 759 | if (rt0->fib6_src.plen) |
| 760 | key_plen = rt0->fib6_src.plen; |
Wei Wang | 17ecf59 | 2017-10-06 12:06:09 -0700 | [diff] [blame] | 761 | #endif |
| 762 | if (fn->fn_bit != key_plen) |
David Ahern | 421842e | 2018-04-17 17:33:18 -0700 | [diff] [blame] | 763 | return net->ipv6.fib6_null_entry; |
Wei Wang | 17ecf59 | 2017-10-06 12:06:09 -0700 | [diff] [blame] | 764 | |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 765 | match = find_rr_leaf(fn, leaf, rt0, rt0->fib6_metric, oif, strict, |
Hannes Frederic Sowa | afc154e | 2013-07-11 12:43:42 +0200 | [diff] [blame] | 766 | &do_rr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 767 | |
Hannes Frederic Sowa | afc154e | 2013-07-11 12:43:42 +0200 | [diff] [blame] | 768 | if (do_rr) { |
David Ahern | 8fb11a9 | 2018-05-04 13:54:24 -0700 | [diff] [blame] | 769 | struct fib6_info *next = rcu_dereference(rt0->fib6_next); |
David S. Miller | f11e665 | 2007-03-24 20:36:25 -0700 | [diff] [blame] | 770 | |
YOSHIFUJI Hideaki | 554cfb7 | 2006-03-20 17:00:26 -0800 | [diff] [blame] | 771 | /* no entries matched; do round-robin */ |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 772 | if (!next || next->fib6_metric != rt0->fib6_metric) |
Wei Wang | 8d1040e | 2017-10-06 12:06:08 -0700 | [diff] [blame] | 773 | next = leaf; |
David S. Miller | f11e665 | 2007-03-24 20:36:25 -0700 | [diff] [blame] | 774 | |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 775 | if (next != rt0) { |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 776 | spin_lock_bh(&leaf->fib6_table->tb6_lock); |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 777 | /* make sure next is not being deleted from the tree */ |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 778 | if (next->fib6_node) |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 779 | rcu_assign_pointer(fn->rr_ptr, next); |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 780 | spin_unlock_bh(&leaf->fib6_table->tb6_lock); |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 781 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 782 | } |
| 783 | |
David Ahern | 421842e | 2018-04-17 17:33:18 -0700 | [diff] [blame] | 784 | return match ? match : net->ipv6.fib6_null_entry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 785 | } |
| 786 | |
David Ahern | 85bd05d | 2019-04-16 14:36:01 -0700 | [diff] [blame^] | 787 | 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] | 788 | { |
David Ahern | 85bd05d | 2019-04-16 14:36:01 -0700 | [diff] [blame^] | 789 | return (res->f6i->fib6_flags & RTF_NONEXTHOP) || |
| 790 | res->nh->fib_nh_gw_family; |
Martin KaFai Lau | 8b9df26 | 2015-05-22 20:55:59 -0700 | [diff] [blame] | 791 | } |
| 792 | |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 793 | #ifdef CONFIG_IPV6_ROUTE_INFO |
| 794 | int rt6_route_rcv(struct net_device *dev, u8 *opt, int len, |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 795 | const struct in6_addr *gwaddr) |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 796 | { |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 797 | struct net *net = dev_net(dev); |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 798 | struct route_info *rinfo = (struct route_info *) opt; |
| 799 | struct in6_addr prefix_buf, *prefix; |
| 800 | unsigned int pref; |
YOSHIFUJI Hideaki | 4bed72e | 2008-05-27 17:37:49 +0900 | [diff] [blame] | 801 | unsigned long lifetime; |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 802 | struct fib6_info *rt; |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 803 | |
| 804 | if (len < sizeof(struct route_info)) { |
| 805 | return -EINVAL; |
| 806 | } |
| 807 | |
| 808 | /* Sanity check for prefix_len and length */ |
| 809 | if (rinfo->length > 3) { |
| 810 | return -EINVAL; |
| 811 | } else if (rinfo->prefix_len > 128) { |
| 812 | return -EINVAL; |
| 813 | } else if (rinfo->prefix_len > 64) { |
| 814 | if (rinfo->length < 2) { |
| 815 | return -EINVAL; |
| 816 | } |
| 817 | } else if (rinfo->prefix_len > 0) { |
| 818 | if (rinfo->length < 1) { |
| 819 | return -EINVAL; |
| 820 | } |
| 821 | } |
| 822 | |
| 823 | pref = rinfo->route_pref; |
| 824 | if (pref == ICMPV6_ROUTER_PREF_INVALID) |
Jens Rosenboom | 3933fc9 | 2009-09-10 06:25:11 +0000 | [diff] [blame] | 825 | return -EINVAL; |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 826 | |
YOSHIFUJI Hideaki | 4bed72e | 2008-05-27 17:37:49 +0900 | [diff] [blame] | 827 | lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ); |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 828 | |
| 829 | if (rinfo->length == 3) |
| 830 | prefix = (struct in6_addr *)rinfo->prefix; |
| 831 | else { |
| 832 | /* this function is safe */ |
| 833 | ipv6_addr_prefix(&prefix_buf, |
| 834 | (struct in6_addr *)rinfo->prefix, |
| 835 | rinfo->prefix_len); |
| 836 | prefix = &prefix_buf; |
| 837 | } |
| 838 | |
Duan Jiong | f104a56 | 2013-11-08 09:56:53 +0800 | [diff] [blame] | 839 | if (rinfo->prefix_len == 0) |
David Ahern | afb1d4b5 | 2018-04-17 17:33:11 -0700 | [diff] [blame] | 840 | rt = rt6_get_dflt_router(net, gwaddr, dev); |
Duan Jiong | f104a56 | 2013-11-08 09:56:53 +0800 | [diff] [blame] | 841 | else |
| 842 | rt = rt6_get_route_info(net, prefix, rinfo->prefix_len, |
David Ahern | 830218c | 2016-10-24 10:52:35 -0700 | [diff] [blame] | 843 | gwaddr, dev); |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 844 | |
| 845 | if (rt && !lifetime) { |
David Ahern | afb1d4b5 | 2018-04-17 17:33:11 -0700 | [diff] [blame] | 846 | ip6_del_rt(net, rt); |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 847 | rt = NULL; |
| 848 | } |
| 849 | |
| 850 | if (!rt && lifetime) |
David Ahern | 830218c | 2016-10-24 10:52:35 -0700 | [diff] [blame] | 851 | rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr, |
| 852 | dev, pref); |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 853 | else if (rt) |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 854 | rt->fib6_flags = RTF_ROUTEINFO | |
| 855 | (rt->fib6_flags & ~RTF_PREF_MASK) | RTF_PREF(pref); |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 856 | |
| 857 | if (rt) { |
Gao feng | 1716a96 | 2012-04-06 00:13:10 +0000 | [diff] [blame] | 858 | if (!addrconf_finite_timeout(lifetime)) |
David Ahern | 1489568 | 2018-04-17 17:33:17 -0700 | [diff] [blame] | 859 | fib6_clean_expires(rt); |
Gao feng | 1716a96 | 2012-04-06 00:13:10 +0000 | [diff] [blame] | 860 | else |
David Ahern | 1489568 | 2018-04-17 17:33:17 -0700 | [diff] [blame] | 861 | fib6_set_expires(rt, jiffies + HZ * lifetime); |
Gao feng | 1716a96 | 2012-04-06 00:13:10 +0000 | [diff] [blame] | 862 | |
David Ahern | 93531c6 | 2018-04-17 17:33:25 -0700 | [diff] [blame] | 863 | fib6_info_release(rt); |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 864 | } |
| 865 | return 0; |
| 866 | } |
| 867 | #endif |
| 868 | |
David Ahern | ae90d86 | 2018-04-17 17:33:12 -0700 | [diff] [blame] | 869 | /* |
| 870 | * Misc support functions |
| 871 | */ |
| 872 | |
| 873 | /* called with rcu_lock held */ |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 874 | static struct net_device *ip6_rt_get_dev_rcu(struct fib6_info *rt) |
David Ahern | ae90d86 | 2018-04-17 17:33:12 -0700 | [diff] [blame] | 875 | { |
David Ahern | ad1601a | 2019-03-27 20:53:56 -0700 | [diff] [blame] | 876 | struct net_device *dev = rt->fib6_nh.fib_nh_dev; |
David Ahern | ae90d86 | 2018-04-17 17:33:12 -0700 | [diff] [blame] | 877 | |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 878 | if (rt->fib6_flags & (RTF_LOCAL | RTF_ANYCAST)) { |
David Ahern | ae90d86 | 2018-04-17 17:33:12 -0700 | [diff] [blame] | 879 | /* for copies of local routes, dst->dev needs to be the |
| 880 | * device if it is a master device, the master device if |
| 881 | * device is enslaved, and the loopback as the default |
| 882 | */ |
| 883 | if (netif_is_l3_slave(dev) && |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 884 | !rt6_need_strict(&rt->fib6_dst.addr)) |
David Ahern | ae90d86 | 2018-04-17 17:33:12 -0700 | [diff] [blame] | 885 | dev = l3mdev_master_dev_rcu(dev); |
| 886 | else if (!netif_is_l3_master(dev)) |
| 887 | dev = dev_net(dev)->loopback_dev; |
| 888 | /* last case is netif_is_l3_master(dev) is true in which |
| 889 | * case we want dev returned to be dev |
| 890 | */ |
| 891 | } |
| 892 | |
| 893 | return dev; |
| 894 | } |
| 895 | |
David Ahern | 6edb3c9 | 2018-04-17 17:33:15 -0700 | [diff] [blame] | 896 | static const int fib6_prop[RTN_MAX + 1] = { |
| 897 | [RTN_UNSPEC] = 0, |
| 898 | [RTN_UNICAST] = 0, |
| 899 | [RTN_LOCAL] = 0, |
| 900 | [RTN_BROADCAST] = 0, |
| 901 | [RTN_ANYCAST] = 0, |
| 902 | [RTN_MULTICAST] = 0, |
| 903 | [RTN_BLACKHOLE] = -EINVAL, |
| 904 | [RTN_UNREACHABLE] = -EHOSTUNREACH, |
| 905 | [RTN_PROHIBIT] = -EACCES, |
| 906 | [RTN_THROW] = -EAGAIN, |
| 907 | [RTN_NAT] = -EINVAL, |
| 908 | [RTN_XRESOLVE] = -EINVAL, |
| 909 | }; |
| 910 | |
| 911 | static int ip6_rt_type_to_error(u8 fib6_type) |
| 912 | { |
| 913 | return fib6_prop[fib6_type]; |
| 914 | } |
| 915 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 916 | static unsigned short fib6_info_dst_flags(struct fib6_info *rt) |
David Ahern | 3b6761d | 2018-04-17 17:33:20 -0700 | [diff] [blame] | 917 | { |
| 918 | unsigned short flags = 0; |
| 919 | |
| 920 | if (rt->dst_nocount) |
| 921 | flags |= DST_NOCOUNT; |
| 922 | if (rt->dst_nopolicy) |
| 923 | flags |= DST_NOPOLICY; |
| 924 | if (rt->dst_host) |
| 925 | flags |= DST_HOST; |
| 926 | |
| 927 | return flags; |
| 928 | } |
| 929 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 930 | static void ip6_rt_init_dst_reject(struct rt6_info *rt, struct fib6_info *ort) |
David Ahern | 6edb3c9 | 2018-04-17 17:33:15 -0700 | [diff] [blame] | 931 | { |
| 932 | rt->dst.error = ip6_rt_type_to_error(ort->fib6_type); |
| 933 | |
| 934 | switch (ort->fib6_type) { |
| 935 | case RTN_BLACKHOLE: |
| 936 | rt->dst.output = dst_discard_out; |
| 937 | rt->dst.input = dst_discard; |
| 938 | break; |
| 939 | case RTN_PROHIBIT: |
| 940 | rt->dst.output = ip6_pkt_prohibit_out; |
| 941 | rt->dst.input = ip6_pkt_prohibit; |
| 942 | break; |
| 943 | case RTN_THROW: |
| 944 | case RTN_UNREACHABLE: |
| 945 | default: |
| 946 | rt->dst.output = ip6_pkt_discard_out; |
| 947 | rt->dst.input = ip6_pkt_discard; |
| 948 | break; |
| 949 | } |
| 950 | } |
| 951 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 952 | static void ip6_rt_init_dst(struct rt6_info *rt, struct fib6_info *ort) |
David Ahern | 6edb3c9 | 2018-04-17 17:33:15 -0700 | [diff] [blame] | 953 | { |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 954 | if (ort->fib6_flags & RTF_REJECT) { |
David Ahern | 6edb3c9 | 2018-04-17 17:33:15 -0700 | [diff] [blame] | 955 | ip6_rt_init_dst_reject(rt, ort); |
| 956 | return; |
| 957 | } |
| 958 | |
| 959 | rt->dst.error = 0; |
| 960 | rt->dst.output = ip6_output; |
| 961 | |
Hangbin Liu | d23c4b6 | 2018-08-23 11:31:37 +0800 | [diff] [blame] | 962 | if (ort->fib6_type == RTN_LOCAL || ort->fib6_type == RTN_ANYCAST) { |
David Ahern | 6edb3c9 | 2018-04-17 17:33:15 -0700 | [diff] [blame] | 963 | rt->dst.input = ip6_input; |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 964 | } else if (ipv6_addr_type(&ort->fib6_dst.addr) & IPV6_ADDR_MULTICAST) { |
David Ahern | 6edb3c9 | 2018-04-17 17:33:15 -0700 | [diff] [blame] | 965 | rt->dst.input = ip6_mc_input; |
| 966 | } else { |
| 967 | rt->dst.input = ip6_forward; |
| 968 | } |
| 969 | |
David Ahern | ad1601a | 2019-03-27 20:53:56 -0700 | [diff] [blame] | 970 | if (ort->fib6_nh.fib_nh_lws) { |
| 971 | rt->dst.lwtstate = lwtstate_get(ort->fib6_nh.fib_nh_lws); |
David Ahern | 6edb3c9 | 2018-04-17 17:33:15 -0700 | [diff] [blame] | 972 | lwtunnel_set_redirect(&rt->dst); |
| 973 | } |
| 974 | |
| 975 | rt->dst.lastuse = jiffies; |
| 976 | } |
| 977 | |
Wei Wang | e873e4b | 2018-07-21 20:56:32 -0700 | [diff] [blame] | 978 | /* Caller must already hold reference to @from */ |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 979 | 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] | 980 | { |
David Ahern | ae90d86 | 2018-04-17 17:33:12 -0700 | [diff] [blame] | 981 | rt->rt6i_flags &= ~RTF_EXPIRES; |
David Ahern | a68886a | 2018-04-20 15:38:02 -0700 | [diff] [blame] | 982 | rcu_assign_pointer(rt->from, from); |
David Ahern | e1255ed | 2018-10-04 20:07:53 -0700 | [diff] [blame] | 983 | ip_dst_init_metrics(&rt->dst, from->fib6_metrics); |
David Ahern | ae90d86 | 2018-04-17 17:33:12 -0700 | [diff] [blame] | 984 | } |
| 985 | |
Wei Wang | e873e4b | 2018-07-21 20:56:32 -0700 | [diff] [blame] | 986 | /* Caller must already hold reference to @ort */ |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 987 | static void ip6_rt_copy_init(struct rt6_info *rt, struct fib6_info *ort) |
David Ahern | ae90d86 | 2018-04-17 17:33:12 -0700 | [diff] [blame] | 988 | { |
David Ahern | dcd1f57 | 2018-04-18 15:39:05 -0700 | [diff] [blame] | 989 | struct net_device *dev = fib6_info_nh_dev(ort); |
| 990 | |
David Ahern | 6edb3c9 | 2018-04-17 17:33:15 -0700 | [diff] [blame] | 991 | ip6_rt_init_dst(rt, ort); |
| 992 | |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 993 | rt->rt6i_dst = ort->fib6_dst; |
David Ahern | dcd1f57 | 2018-04-18 15:39:05 -0700 | [diff] [blame] | 994 | rt->rt6i_idev = dev ? in6_dev_get(dev) : NULL; |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 995 | rt->rt6i_flags = ort->fib6_flags; |
David Ahern | bdf0046 | 2019-04-05 16:30:26 -0700 | [diff] [blame] | 996 | if (ort->fib6_nh.fib_nh_gw_family) { |
David Ahern | ad1601a | 2019-03-27 20:53:56 -0700 | [diff] [blame] | 997 | rt->rt6i_gateway = ort->fib6_nh.fib_nh_gw6; |
David Ahern | 2b2450c | 2019-03-27 20:53:52 -0700 | [diff] [blame] | 998 | rt->rt6i_flags |= RTF_GATEWAY; |
| 999 | } |
David Ahern | ae90d86 | 2018-04-17 17:33:12 -0700 | [diff] [blame] | 1000 | rt6_set_from(rt, ort); |
David Ahern | ae90d86 | 2018-04-17 17:33:12 -0700 | [diff] [blame] | 1001 | #ifdef CONFIG_IPV6_SUBTREES |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 1002 | rt->rt6i_src = ort->fib6_src; |
David Ahern | ae90d86 | 2018-04-17 17:33:12 -0700 | [diff] [blame] | 1003 | #endif |
David Ahern | ae90d86 | 2018-04-17 17:33:12 -0700 | [diff] [blame] | 1004 | } |
| 1005 | |
Martin KaFai Lau | a3c00e4 | 2014-10-20 13:42:43 -0700 | [diff] [blame] | 1006 | static struct fib6_node* fib6_backtrack(struct fib6_node *fn, |
| 1007 | struct in6_addr *saddr) |
| 1008 | { |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 1009 | struct fib6_node *pn, *sn; |
Martin KaFai Lau | a3c00e4 | 2014-10-20 13:42:43 -0700 | [diff] [blame] | 1010 | while (1) { |
| 1011 | if (fn->fn_flags & RTN_TL_ROOT) |
| 1012 | return NULL; |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 1013 | pn = rcu_dereference(fn->parent); |
| 1014 | sn = FIB6_SUBTREE(pn); |
| 1015 | if (sn && sn != fn) |
David Ahern | 6454743 | 2018-05-09 20:34:19 -0700 | [diff] [blame] | 1016 | fn = fib6_node_lookup(sn, NULL, saddr); |
Martin KaFai Lau | a3c00e4 | 2014-10-20 13:42:43 -0700 | [diff] [blame] | 1017 | else |
| 1018 | fn = pn; |
| 1019 | if (fn->fn_flags & RTN_RTINFO) |
| 1020 | return fn; |
| 1021 | } |
| 1022 | } |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1023 | |
David Ahern | 10585b4 | 2019-03-20 09:24:50 -0700 | [diff] [blame] | 1024 | static bool ip6_hold_safe(struct net *net, struct rt6_info **prt) |
Wei Wang | d3843fe | 2017-10-06 12:06:06 -0700 | [diff] [blame] | 1025 | { |
| 1026 | struct rt6_info *rt = *prt; |
| 1027 | |
| 1028 | if (dst_hold_safe(&rt->dst)) |
| 1029 | return true; |
David Ahern | 10585b4 | 2019-03-20 09:24:50 -0700 | [diff] [blame] | 1030 | if (net) { |
Wei Wang | d3843fe | 2017-10-06 12:06:06 -0700 | [diff] [blame] | 1031 | rt = net->ipv6.ip6_null_entry; |
| 1032 | dst_hold(&rt->dst); |
| 1033 | } else { |
| 1034 | rt = NULL; |
| 1035 | } |
| 1036 | *prt = rt; |
| 1037 | return false; |
| 1038 | } |
| 1039 | |
David Ahern | dec9b0e | 2018-04-17 17:33:19 -0700 | [diff] [blame] | 1040 | /* called with rcu_lock held */ |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 1041 | static struct rt6_info *ip6_create_rt_rcu(struct fib6_info *rt) |
David Ahern | dec9b0e | 2018-04-17 17:33:19 -0700 | [diff] [blame] | 1042 | { |
David Ahern | 3b6761d | 2018-04-17 17:33:20 -0700 | [diff] [blame] | 1043 | unsigned short flags = fib6_info_dst_flags(rt); |
David Ahern | ad1601a | 2019-03-27 20:53:56 -0700 | [diff] [blame] | 1044 | struct net_device *dev = rt->fib6_nh.fib_nh_dev; |
David Ahern | dec9b0e | 2018-04-17 17:33:19 -0700 | [diff] [blame] | 1045 | struct rt6_info *nrt; |
| 1046 | |
Wei Wang | e873e4b | 2018-07-21 20:56:32 -0700 | [diff] [blame] | 1047 | if (!fib6_info_hold_safe(rt)) |
Xin Long | 1c87e79 | 2019-03-20 14:45:48 +0800 | [diff] [blame] | 1048 | goto fallback; |
Wei Wang | e873e4b | 2018-07-21 20:56:32 -0700 | [diff] [blame] | 1049 | |
David Ahern | 93531c6 | 2018-04-17 17:33:25 -0700 | [diff] [blame] | 1050 | nrt = ip6_dst_alloc(dev_net(dev), dev, flags); |
Xin Long | 1c87e79 | 2019-03-20 14:45:48 +0800 | [diff] [blame] | 1051 | if (!nrt) { |
Wei Wang | e873e4b | 2018-07-21 20:56:32 -0700 | [diff] [blame] | 1052 | fib6_info_release(rt); |
Xin Long | 1c87e79 | 2019-03-20 14:45:48 +0800 | [diff] [blame] | 1053 | goto fallback; |
| 1054 | } |
David Ahern | dec9b0e | 2018-04-17 17:33:19 -0700 | [diff] [blame] | 1055 | |
Xin Long | 1c87e79 | 2019-03-20 14:45:48 +0800 | [diff] [blame] | 1056 | ip6_rt_copy_init(nrt, rt); |
| 1057 | return nrt; |
| 1058 | |
| 1059 | fallback: |
| 1060 | nrt = dev_net(dev)->ipv6.ip6_null_entry; |
| 1061 | dst_hold(&nrt->dst); |
David Ahern | dec9b0e | 2018-04-17 17:33:19 -0700 | [diff] [blame] | 1062 | return nrt; |
| 1063 | } |
| 1064 | |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 1065 | static struct rt6_info *ip6_pol_route_lookup(struct net *net, |
| 1066 | struct fib6_table *table, |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 1067 | struct flowi6 *fl6, |
| 1068 | const struct sk_buff *skb, |
| 1069 | int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1070 | { |
David Ahern | b1d4099 | 2019-04-16 14:35:59 -0700 | [diff] [blame] | 1071 | struct fib6_result res = {}; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1072 | struct fib6_node *fn; |
David Ahern | 23fb93a | 2018-04-17 17:33:23 -0700 | [diff] [blame] | 1073 | struct rt6_info *rt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1074 | |
David Ahern | b6cdbc8 | 2018-03-29 17:44:57 -0700 | [diff] [blame] | 1075 | if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF) |
| 1076 | flags &= ~RT6_LOOKUP_F_IFACE; |
| 1077 | |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 1078 | rcu_read_lock(); |
David Ahern | 6454743 | 2018-05-09 20:34:19 -0700 | [diff] [blame] | 1079 | fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr); |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1080 | restart: |
David Ahern | b1d4099 | 2019-04-16 14:35:59 -0700 | [diff] [blame] | 1081 | res.f6i = rcu_dereference(fn->leaf); |
| 1082 | if (!res.f6i) |
| 1083 | res.f6i = net->ipv6.fib6_null_entry; |
David Ahern | af52a52 | 2019-04-09 14:41:16 -0700 | [diff] [blame] | 1084 | else |
David Ahern | b1d4099 | 2019-04-16 14:35:59 -0700 | [diff] [blame] | 1085 | res.f6i = rt6_device_match(net, res.f6i, &fl6->saddr, |
| 1086 | fl6->flowi6_oif, flags); |
David Ahern | af52a52 | 2019-04-09 14:41:16 -0700 | [diff] [blame] | 1087 | |
David Ahern | b1d4099 | 2019-04-16 14:35:59 -0700 | [diff] [blame] | 1088 | if (res.f6i == net->ipv6.fib6_null_entry) { |
Martin KaFai Lau | a3c00e4 | 2014-10-20 13:42:43 -0700 | [diff] [blame] | 1089 | fn = fib6_backtrack(fn, &fl6->saddr); |
| 1090 | if (fn) |
| 1091 | goto restart; |
David Ahern | af52a52 | 2019-04-09 14:41:16 -0700 | [diff] [blame] | 1092 | |
| 1093 | rt = net->ipv6.ip6_null_entry; |
| 1094 | dst_hold(&rt->dst); |
| 1095 | goto out; |
Martin KaFai Lau | a3c00e4 | 2014-10-20 13:42:43 -0700 | [diff] [blame] | 1096 | } |
Wei Wang | 2b760fc | 2017-10-06 12:06:03 -0700 | [diff] [blame] | 1097 | |
David Ahern | b1d4099 | 2019-04-16 14:35:59 -0700 | [diff] [blame] | 1098 | fib6_select_path(net, &res, fl6, fl6->flowi6_oif, |
| 1099 | fl6->flowi6_oif != 0, skb, flags); |
| 1100 | |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 1101 | /* Search through exception table */ |
David Ahern | 7e4b512 | 2019-04-16 14:36:00 -0700 | [diff] [blame] | 1102 | rt = rt6_find_cached_rt(&res, &fl6->daddr, &fl6->saddr); |
David Ahern | 23fb93a | 2018-04-17 17:33:23 -0700 | [diff] [blame] | 1103 | if (rt) { |
David Ahern | 10585b4 | 2019-03-20 09:24:50 -0700 | [diff] [blame] | 1104 | if (ip6_hold_safe(net, &rt)) |
David Ahern | dec9b0e | 2018-04-17 17:33:19 -0700 | [diff] [blame] | 1105 | dst_use_noref(&rt->dst, jiffies); |
David Ahern | 23fb93a | 2018-04-17 17:33:23 -0700 | [diff] [blame] | 1106 | } else { |
David Ahern | b1d4099 | 2019-04-16 14:35:59 -0700 | [diff] [blame] | 1107 | rt = ip6_create_rt_rcu(res.f6i); |
David Ahern | dec9b0e | 2018-04-17 17:33:19 -0700 | [diff] [blame] | 1108 | } |
Wei Wang | d3843fe | 2017-10-06 12:06:06 -0700 | [diff] [blame] | 1109 | |
David Ahern | af52a52 | 2019-04-09 14:41:16 -0700 | [diff] [blame] | 1110 | out: |
David Ahern | b1d4099 | 2019-04-16 14:35:59 -0700 | [diff] [blame] | 1111 | trace_fib6_table_lookup(net, res.f6i, table, fl6); |
David Ahern | af52a52 | 2019-04-09 14:41:16 -0700 | [diff] [blame] | 1112 | |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 1113 | rcu_read_unlock(); |
David Ahern | b811580 | 2015-11-19 12:24:22 -0800 | [diff] [blame] | 1114 | |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1115 | return rt; |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1116 | } |
| 1117 | |
Ian Morris | 67ba415 | 2014-08-24 21:53:10 +0100 | [diff] [blame] | 1118 | struct dst_entry *ip6_route_lookup(struct net *net, struct flowi6 *fl6, |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 1119 | const struct sk_buff *skb, int flags) |
Florian Westphal | ea6e574 | 2011-09-05 16:05:44 +0200 | [diff] [blame] | 1120 | { |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 1121 | return fib6_rule_lookup(net, fl6, skb, flags, ip6_pol_route_lookup); |
Florian Westphal | ea6e574 | 2011-09-05 16:05:44 +0200 | [diff] [blame] | 1122 | } |
| 1123 | EXPORT_SYMBOL_GPL(ip6_route_lookup); |
| 1124 | |
YOSHIFUJI Hideaki | 9acd9f3 | 2008-04-10 15:42:10 +0900 | [diff] [blame] | 1125 | 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] | 1126 | const struct in6_addr *saddr, int oif, |
| 1127 | const struct sk_buff *skb, int strict) |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1128 | { |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 1129 | struct flowi6 fl6 = { |
| 1130 | .flowi6_oif = oif, |
| 1131 | .daddr = *daddr, |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1132 | }; |
| 1133 | struct dst_entry *dst; |
YOSHIFUJI Hideaki | 77d16f4 | 2006-08-23 17:25:05 -0700 | [diff] [blame] | 1134 | int flags = strict ? RT6_LOOKUP_F_IFACE : 0; |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1135 | |
Thomas Graf | adaa70b | 2006-10-13 15:01:03 -0700 | [diff] [blame] | 1136 | if (saddr) { |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 1137 | memcpy(&fl6.saddr, saddr, sizeof(*saddr)); |
Thomas Graf | adaa70b | 2006-10-13 15:01:03 -0700 | [diff] [blame] | 1138 | flags |= RT6_LOOKUP_F_HAS_SADDR; |
| 1139 | } |
| 1140 | |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 1141 | dst = fib6_rule_lookup(net, &fl6, skb, flags, ip6_pol_route_lookup); |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1142 | if (dst->error == 0) |
| 1143 | return (struct rt6_info *) dst; |
| 1144 | |
| 1145 | dst_release(dst); |
| 1146 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1147 | return NULL; |
| 1148 | } |
YOSHIFUJI Hideaki | 7159039 | 2007-02-22 22:05:40 +0900 | [diff] [blame] | 1149 | EXPORT_SYMBOL(rt6_lookup); |
| 1150 | |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1151 | /* ip6_ins_rt is called with FREE table->tb6_lock. |
Wei Wang | 1cfb71e | 2017-06-17 10:42:33 -0700 | [diff] [blame] | 1152 | * It takes new route entry, the addition fails by any reason the |
| 1153 | * route is released. |
| 1154 | * Caller must hold dst before calling it. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1155 | */ |
| 1156 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 1157 | 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] | 1158 | struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1159 | { |
| 1160 | int err; |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1161 | struct fib6_table *table; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1162 | |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 1163 | table = rt->fib6_table; |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 1164 | spin_lock_bh(&table->tb6_lock); |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 1165 | err = fib6_add(&table->tb6_root, rt, info, extack); |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 1166 | spin_unlock_bh(&table->tb6_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1167 | |
| 1168 | return err; |
| 1169 | } |
| 1170 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 1171 | int ip6_ins_rt(struct net *net, struct fib6_info *rt) |
Thomas Graf | 40e22e8 | 2006-08-22 00:00:45 -0700 | [diff] [blame] | 1172 | { |
David Ahern | afb1d4b5 | 2018-04-17 17:33:11 -0700 | [diff] [blame] | 1173 | struct nl_info info = { .nl_net = net, }; |
Florian Westphal | e715b6d | 2015-01-05 23:57:44 +0100 | [diff] [blame] | 1174 | |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 1175 | return __ip6_ins_rt(rt, &info, NULL); |
Thomas Graf | 40e22e8 | 2006-08-22 00:00:45 -0700 | [diff] [blame] | 1176 | } |
| 1177 | |
David Ahern | 85bd05d | 2019-04-16 14:36:01 -0700 | [diff] [blame^] | 1178 | 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] | 1179 | const struct in6_addr *daddr, |
| 1180 | const struct in6_addr *saddr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1181 | { |
David Ahern | 85bd05d | 2019-04-16 14:36:01 -0700 | [diff] [blame^] | 1182 | struct fib6_info *f6i = res->f6i; |
David Ahern | 4832c30 | 2017-08-17 12:17:20 -0700 | [diff] [blame] | 1183 | struct net_device *dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1184 | struct rt6_info *rt; |
| 1185 | |
| 1186 | /* |
| 1187 | * Clone the route. |
| 1188 | */ |
| 1189 | |
David Ahern | 85bd05d | 2019-04-16 14:36:01 -0700 | [diff] [blame^] | 1190 | if (!fib6_info_hold_safe(f6i)) |
Wei Wang | e873e4b | 2018-07-21 20:56:32 -0700 | [diff] [blame] | 1191 | return NULL; |
| 1192 | |
David Ahern | 85bd05d | 2019-04-16 14:36:01 -0700 | [diff] [blame^] | 1193 | dev = ip6_rt_get_dev_rcu(f6i); |
David Ahern | 93531c6 | 2018-04-17 17:33:25 -0700 | [diff] [blame] | 1194 | rt = ip6_dst_alloc(dev_net(dev), dev, 0); |
Wei Wang | e873e4b | 2018-07-21 20:56:32 -0700 | [diff] [blame] | 1195 | if (!rt) { |
David Ahern | 85bd05d | 2019-04-16 14:36:01 -0700 | [diff] [blame^] | 1196 | fib6_info_release(f6i); |
Martin KaFai Lau | 83a09ab | 2015-05-22 20:56:05 -0700 | [diff] [blame] | 1197 | return NULL; |
Wei Wang | e873e4b | 2018-07-21 20:56:32 -0700 | [diff] [blame] | 1198 | } |
Martin KaFai Lau | 83a09ab | 2015-05-22 20:56:05 -0700 | [diff] [blame] | 1199 | |
David Ahern | 85bd05d | 2019-04-16 14:36:01 -0700 | [diff] [blame^] | 1200 | ip6_rt_copy_init(rt, res->f6i); |
Martin KaFai Lau | 83a09ab | 2015-05-22 20:56:05 -0700 | [diff] [blame] | 1201 | rt->rt6i_flags |= RTF_CACHE; |
Martin KaFai Lau | 83a09ab | 2015-05-22 20:56:05 -0700 | [diff] [blame] | 1202 | rt->dst.flags |= DST_HOST; |
| 1203 | rt->rt6i_dst.addr = *daddr; |
| 1204 | rt->rt6i_dst.plen = 128; |
| 1205 | |
David Ahern | 85bd05d | 2019-04-16 14:36:01 -0700 | [diff] [blame^] | 1206 | if (!rt6_is_gw_or_nonexthop(res)) { |
| 1207 | if (f6i->fib6_dst.plen != 128 && |
| 1208 | ipv6_addr_equal(&f6i->fib6_dst.addr, daddr)) |
Martin KaFai Lau | 83a09ab | 2015-05-22 20:56:05 -0700 | [diff] [blame] | 1209 | rt->rt6i_flags |= RTF_ANYCAST; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1210 | #ifdef CONFIG_IPV6_SUBTREES |
Martin KaFai Lau | 83a09ab | 2015-05-22 20:56:05 -0700 | [diff] [blame] | 1211 | if (rt->rt6i_src.plen && saddr) { |
| 1212 | rt->rt6i_src.addr = *saddr; |
| 1213 | rt->rt6i_src.plen = 128; |
Martin KaFai Lau | 8b9df26 | 2015-05-22 20:55:59 -0700 | [diff] [blame] | 1214 | } |
Martin KaFai Lau | 83a09ab | 2015-05-22 20:56:05 -0700 | [diff] [blame] | 1215 | #endif |
YOSHIFUJI Hideaki | 95a9a5b | 2006-03-20 16:55:51 -0800 | [diff] [blame] | 1216 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1217 | |
YOSHIFUJI Hideaki | 95a9a5b | 2006-03-20 16:55:51 -0800 | [diff] [blame] | 1218 | return rt; |
| 1219 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1220 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 1221 | static struct rt6_info *ip6_rt_pcpu_alloc(struct fib6_info *rt) |
Martin KaFai Lau | d52d399 | 2015-05-22 20:56:06 -0700 | [diff] [blame] | 1222 | { |
David Ahern | 3b6761d | 2018-04-17 17:33:20 -0700 | [diff] [blame] | 1223 | unsigned short flags = fib6_info_dst_flags(rt); |
David Ahern | 4832c30 | 2017-08-17 12:17:20 -0700 | [diff] [blame] | 1224 | struct net_device *dev; |
Martin KaFai Lau | d52d399 | 2015-05-22 20:56:06 -0700 | [diff] [blame] | 1225 | struct rt6_info *pcpu_rt; |
| 1226 | |
Wei Wang | e873e4b | 2018-07-21 20:56:32 -0700 | [diff] [blame] | 1227 | if (!fib6_info_hold_safe(rt)) |
| 1228 | return NULL; |
| 1229 | |
David Ahern | 4832c30 | 2017-08-17 12:17:20 -0700 | [diff] [blame] | 1230 | rcu_read_lock(); |
| 1231 | dev = ip6_rt_get_dev_rcu(rt); |
David Ahern | 93531c6 | 2018-04-17 17:33:25 -0700 | [diff] [blame] | 1232 | pcpu_rt = ip6_dst_alloc(dev_net(dev), dev, flags); |
David Ahern | 4832c30 | 2017-08-17 12:17:20 -0700 | [diff] [blame] | 1233 | rcu_read_unlock(); |
Wei Wang | e873e4b | 2018-07-21 20:56:32 -0700 | [diff] [blame] | 1234 | if (!pcpu_rt) { |
| 1235 | fib6_info_release(rt); |
Martin KaFai Lau | d52d399 | 2015-05-22 20:56:06 -0700 | [diff] [blame] | 1236 | return NULL; |
Wei Wang | e873e4b | 2018-07-21 20:56:32 -0700 | [diff] [blame] | 1237 | } |
Martin KaFai Lau | d52d399 | 2015-05-22 20:56:06 -0700 | [diff] [blame] | 1238 | ip6_rt_copy_init(pcpu_rt, rt); |
Martin KaFai Lau | d52d399 | 2015-05-22 20:56:06 -0700 | [diff] [blame] | 1239 | pcpu_rt->rt6i_flags |= RTF_PCPU; |
| 1240 | return pcpu_rt; |
| 1241 | } |
| 1242 | |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 1243 | /* It should be called with rcu_read_lock() acquired */ |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 1244 | static struct rt6_info *rt6_get_pcpu_route(struct fib6_info *rt) |
Martin KaFai Lau | d52d399 | 2015-05-22 20:56:06 -0700 | [diff] [blame] | 1245 | { |
Martin KaFai Lau | a73e419 | 2015-08-14 11:05:53 -0700 | [diff] [blame] | 1246 | struct rt6_info *pcpu_rt, **p; |
Martin KaFai Lau | d52d399 | 2015-05-22 20:56:06 -0700 | [diff] [blame] | 1247 | |
| 1248 | p = this_cpu_ptr(rt->rt6i_pcpu); |
| 1249 | pcpu_rt = *p; |
| 1250 | |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 1251 | if (pcpu_rt) |
David Ahern | 10585b4 | 2019-03-20 09:24:50 -0700 | [diff] [blame] | 1252 | ip6_hold_safe(NULL, &pcpu_rt); |
Wei Wang | d3843fe | 2017-10-06 12:06:06 -0700 | [diff] [blame] | 1253 | |
Martin KaFai Lau | a73e419 | 2015-08-14 11:05:53 -0700 | [diff] [blame] | 1254 | return pcpu_rt; |
| 1255 | } |
| 1256 | |
David Ahern | afb1d4b5 | 2018-04-17 17:33:11 -0700 | [diff] [blame] | 1257 | static struct rt6_info *rt6_make_pcpu_route(struct net *net, |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 1258 | struct fib6_info *rt) |
Martin KaFai Lau | a73e419 | 2015-08-14 11:05:53 -0700 | [diff] [blame] | 1259 | { |
| 1260 | struct rt6_info *pcpu_rt, *prev, **p; |
Martin KaFai Lau | d52d399 | 2015-05-22 20:56:06 -0700 | [diff] [blame] | 1261 | |
| 1262 | pcpu_rt = ip6_rt_pcpu_alloc(rt); |
| 1263 | if (!pcpu_rt) { |
Martin KaFai Lau | 9c7370a | 2015-08-14 11:05:54 -0700 | [diff] [blame] | 1264 | dst_hold(&net->ipv6.ip6_null_entry->dst); |
| 1265 | return net->ipv6.ip6_null_entry; |
Martin KaFai Lau | d52d399 | 2015-05-22 20:56:06 -0700 | [diff] [blame] | 1266 | } |
| 1267 | |
Martin KaFai Lau | d52d399 | 2015-05-22 20:56:06 -0700 | [diff] [blame] | 1268 | dst_hold(&pcpu_rt->dst); |
Wei Wang | a94b936 | 2017-10-06 12:06:04 -0700 | [diff] [blame] | 1269 | p = this_cpu_ptr(rt->rt6i_pcpu); |
| 1270 | prev = cmpxchg(p, NULL, pcpu_rt); |
Eric Dumazet | 951f788 | 2017-10-08 21:07:18 -0700 | [diff] [blame] | 1271 | BUG_ON(prev); |
Wei Wang | a94b936 | 2017-10-06 12:06:04 -0700 | [diff] [blame] | 1272 | |
Martin KaFai Lau | d52d399 | 2015-05-22 20:56:06 -0700 | [diff] [blame] | 1273 | return pcpu_rt; |
| 1274 | } |
| 1275 | |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1276 | /* exception hash table implementation |
| 1277 | */ |
| 1278 | static DEFINE_SPINLOCK(rt6_exception_lock); |
| 1279 | |
| 1280 | /* Remove rt6_ex from hash table and free the memory |
| 1281 | * Caller must hold rt6_exception_lock |
| 1282 | */ |
| 1283 | static void rt6_remove_exception(struct rt6_exception_bucket *bucket, |
| 1284 | struct rt6_exception *rt6_ex) |
| 1285 | { |
Paolo Abeni | f5b51fe | 2019-02-20 18:18:12 +0100 | [diff] [blame] | 1286 | struct fib6_info *from; |
Colin Ian King | b2427e6 | 2017-10-10 18:01:16 +0100 | [diff] [blame] | 1287 | struct net *net; |
Wei Wang | 81eb844 | 2017-10-06 12:06:11 -0700 | [diff] [blame] | 1288 | |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1289 | if (!bucket || !rt6_ex) |
| 1290 | return; |
Colin Ian King | b2427e6 | 2017-10-10 18:01:16 +0100 | [diff] [blame] | 1291 | |
| 1292 | net = dev_net(rt6_ex->rt6i->dst.dev); |
Paolo Abeni | f5b51fe | 2019-02-20 18:18:12 +0100 | [diff] [blame] | 1293 | net->ipv6.rt6_stats->fib_rt_cache--; |
| 1294 | |
| 1295 | /* purge completely the exception to allow releasing the held resources: |
| 1296 | * some [sk] cache may keep the dst around for unlimited time |
| 1297 | */ |
| 1298 | from = rcu_dereference_protected(rt6_ex->rt6i->from, |
| 1299 | lockdep_is_held(&rt6_exception_lock)); |
| 1300 | rcu_assign_pointer(rt6_ex->rt6i->from, NULL); |
| 1301 | fib6_info_release(from); |
| 1302 | dst_dev_put(&rt6_ex->rt6i->dst); |
| 1303 | |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1304 | hlist_del_rcu(&rt6_ex->hlist); |
David Ahern | 77634cc | 2018-04-17 17:33:27 -0700 | [diff] [blame] | 1305 | dst_release(&rt6_ex->rt6i->dst); |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1306 | kfree_rcu(rt6_ex, rcu); |
| 1307 | WARN_ON_ONCE(!bucket->depth); |
| 1308 | bucket->depth--; |
| 1309 | } |
| 1310 | |
| 1311 | /* Remove oldest rt6_ex in bucket and free the memory |
| 1312 | * Caller must hold rt6_exception_lock |
| 1313 | */ |
| 1314 | static void rt6_exception_remove_oldest(struct rt6_exception_bucket *bucket) |
| 1315 | { |
| 1316 | struct rt6_exception *rt6_ex, *oldest = NULL; |
| 1317 | |
| 1318 | if (!bucket) |
| 1319 | return; |
| 1320 | |
| 1321 | hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) { |
| 1322 | if (!oldest || time_before(rt6_ex->stamp, oldest->stamp)) |
| 1323 | oldest = rt6_ex; |
| 1324 | } |
| 1325 | rt6_remove_exception(bucket, oldest); |
| 1326 | } |
| 1327 | |
| 1328 | static u32 rt6_exception_hash(const struct in6_addr *dst, |
| 1329 | const struct in6_addr *src) |
| 1330 | { |
| 1331 | static u32 seed __read_mostly; |
| 1332 | u32 val; |
| 1333 | |
| 1334 | net_get_random_once(&seed, sizeof(seed)); |
| 1335 | val = jhash(dst, sizeof(*dst), seed); |
| 1336 | |
| 1337 | #ifdef CONFIG_IPV6_SUBTREES |
| 1338 | if (src) |
| 1339 | val = jhash(src, sizeof(*src), val); |
| 1340 | #endif |
| 1341 | return hash_32(val, FIB6_EXCEPTION_BUCKET_SIZE_SHIFT); |
| 1342 | } |
| 1343 | |
| 1344 | /* Helper function to find the cached rt in the hash table |
| 1345 | * and update bucket pointer to point to the bucket for this |
| 1346 | * (daddr, saddr) pair |
| 1347 | * Caller must hold rt6_exception_lock |
| 1348 | */ |
| 1349 | static struct rt6_exception * |
| 1350 | __rt6_find_exception_spinlock(struct rt6_exception_bucket **bucket, |
| 1351 | const struct in6_addr *daddr, |
| 1352 | const struct in6_addr *saddr) |
| 1353 | { |
| 1354 | struct rt6_exception *rt6_ex; |
| 1355 | u32 hval; |
| 1356 | |
| 1357 | if (!(*bucket) || !daddr) |
| 1358 | return NULL; |
| 1359 | |
| 1360 | hval = rt6_exception_hash(daddr, saddr); |
| 1361 | *bucket += hval; |
| 1362 | |
| 1363 | hlist_for_each_entry(rt6_ex, &(*bucket)->chain, hlist) { |
| 1364 | struct rt6_info *rt6 = rt6_ex->rt6i; |
| 1365 | bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr); |
| 1366 | |
| 1367 | #ifdef CONFIG_IPV6_SUBTREES |
| 1368 | if (matched && saddr) |
| 1369 | matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr); |
| 1370 | #endif |
| 1371 | if (matched) |
| 1372 | return rt6_ex; |
| 1373 | } |
| 1374 | return NULL; |
| 1375 | } |
| 1376 | |
| 1377 | /* Helper function to find the cached rt in the hash table |
| 1378 | * and update bucket pointer to point to the bucket for this |
| 1379 | * (daddr, saddr) pair |
| 1380 | * Caller must hold rcu_read_lock() |
| 1381 | */ |
| 1382 | static struct rt6_exception * |
| 1383 | __rt6_find_exception_rcu(struct rt6_exception_bucket **bucket, |
| 1384 | const struct in6_addr *daddr, |
| 1385 | const struct in6_addr *saddr) |
| 1386 | { |
| 1387 | struct rt6_exception *rt6_ex; |
| 1388 | u32 hval; |
| 1389 | |
| 1390 | WARN_ON_ONCE(!rcu_read_lock_held()); |
| 1391 | |
| 1392 | if (!(*bucket) || !daddr) |
| 1393 | return NULL; |
| 1394 | |
| 1395 | hval = rt6_exception_hash(daddr, saddr); |
| 1396 | *bucket += hval; |
| 1397 | |
| 1398 | hlist_for_each_entry_rcu(rt6_ex, &(*bucket)->chain, hlist) { |
| 1399 | struct rt6_info *rt6 = rt6_ex->rt6i; |
| 1400 | bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr); |
| 1401 | |
| 1402 | #ifdef CONFIG_IPV6_SUBTREES |
| 1403 | if (matched && saddr) |
| 1404 | matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr); |
| 1405 | #endif |
| 1406 | if (matched) |
| 1407 | return rt6_ex; |
| 1408 | } |
| 1409 | return NULL; |
| 1410 | } |
| 1411 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 1412 | static unsigned int fib6_mtu(const struct fib6_info *rt) |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1413 | { |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 1414 | unsigned int mtu; |
| 1415 | |
David Ahern | dcd1f57 | 2018-04-18 15:39:05 -0700 | [diff] [blame] | 1416 | if (rt->fib6_pmtu) { |
| 1417 | mtu = rt->fib6_pmtu; |
| 1418 | } else { |
| 1419 | struct net_device *dev = fib6_info_nh_dev(rt); |
| 1420 | struct inet6_dev *idev; |
| 1421 | |
| 1422 | rcu_read_lock(); |
| 1423 | idev = __in6_dev_get(dev); |
| 1424 | mtu = idev->cnf.mtu6; |
| 1425 | rcu_read_unlock(); |
| 1426 | } |
| 1427 | |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 1428 | mtu = min_t(unsigned int, mtu, IP6_MAX_MTU); |
| 1429 | |
David Ahern | ad1601a | 2019-03-27 20:53:56 -0700 | [diff] [blame] | 1430 | return mtu - lwtunnel_headroom(rt->fib6_nh.fib_nh_lws, mtu); |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 1431 | } |
| 1432 | |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1433 | static int rt6_insert_exception(struct rt6_info *nrt, |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 1434 | struct fib6_info *ort) |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1435 | { |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 1436 | struct net *net = dev_net(nrt->dst.dev); |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1437 | struct rt6_exception_bucket *bucket; |
| 1438 | struct in6_addr *src_key = NULL; |
| 1439 | struct rt6_exception *rt6_ex; |
| 1440 | int err = 0; |
| 1441 | |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1442 | spin_lock_bh(&rt6_exception_lock); |
| 1443 | |
| 1444 | if (ort->exception_bucket_flushed) { |
| 1445 | err = -EINVAL; |
| 1446 | goto out; |
| 1447 | } |
| 1448 | |
| 1449 | bucket = rcu_dereference_protected(ort->rt6i_exception_bucket, |
| 1450 | lockdep_is_held(&rt6_exception_lock)); |
| 1451 | if (!bucket) { |
| 1452 | bucket = kcalloc(FIB6_EXCEPTION_BUCKET_SIZE, sizeof(*bucket), |
| 1453 | GFP_ATOMIC); |
| 1454 | if (!bucket) { |
| 1455 | err = -ENOMEM; |
| 1456 | goto out; |
| 1457 | } |
| 1458 | rcu_assign_pointer(ort->rt6i_exception_bucket, bucket); |
| 1459 | } |
| 1460 | |
| 1461 | #ifdef CONFIG_IPV6_SUBTREES |
| 1462 | /* rt6i_src.plen != 0 indicates ort is in subtree |
| 1463 | * and exception table is indexed by a hash of |
| 1464 | * both rt6i_dst and rt6i_src. |
| 1465 | * Otherwise, the exception table is indexed by |
| 1466 | * a hash of only rt6i_dst. |
| 1467 | */ |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 1468 | if (ort->fib6_src.plen) |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1469 | src_key = &nrt->rt6i_src.addr; |
| 1470 | #endif |
Wei Wang | f5bbe7e | 2017-10-06 12:05:59 -0700 | [diff] [blame] | 1471 | /* rt6_mtu_change() might lower mtu on ort. |
| 1472 | * Only insert this exception route if its mtu |
| 1473 | * is less than ort's mtu value. |
| 1474 | */ |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 1475 | if (dst_metric_raw(&nrt->dst, RTAX_MTU) >= fib6_mtu(ort)) { |
Wei Wang | f5bbe7e | 2017-10-06 12:05:59 -0700 | [diff] [blame] | 1476 | err = -EINVAL; |
| 1477 | goto out; |
| 1478 | } |
Wei Wang | 60006a4 | 2017-10-06 12:05:58 -0700 | [diff] [blame] | 1479 | |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1480 | rt6_ex = __rt6_find_exception_spinlock(&bucket, &nrt->rt6i_dst.addr, |
| 1481 | src_key); |
| 1482 | if (rt6_ex) |
| 1483 | rt6_remove_exception(bucket, rt6_ex); |
| 1484 | |
| 1485 | rt6_ex = kzalloc(sizeof(*rt6_ex), GFP_ATOMIC); |
| 1486 | if (!rt6_ex) { |
| 1487 | err = -ENOMEM; |
| 1488 | goto out; |
| 1489 | } |
| 1490 | rt6_ex->rt6i = nrt; |
| 1491 | rt6_ex->stamp = jiffies; |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1492 | hlist_add_head_rcu(&rt6_ex->hlist, &bucket->chain); |
| 1493 | bucket->depth++; |
Wei Wang | 81eb844 | 2017-10-06 12:06:11 -0700 | [diff] [blame] | 1494 | net->ipv6.rt6_stats->fib_rt_cache++; |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1495 | |
| 1496 | if (bucket->depth > FIB6_MAX_DEPTH) |
| 1497 | rt6_exception_remove_oldest(bucket); |
| 1498 | |
| 1499 | out: |
| 1500 | spin_unlock_bh(&rt6_exception_lock); |
| 1501 | |
| 1502 | /* Update fn->fn_sernum to invalidate all cached dst */ |
Paolo Abeni | b886d5f | 2017-10-19 16:07:10 +0200 | [diff] [blame] | 1503 | if (!err) { |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 1504 | spin_lock_bh(&ort->fib6_table->tb6_lock); |
David Ahern | 7aef685 | 2018-04-17 17:33:10 -0700 | [diff] [blame] | 1505 | fib6_update_sernum(net, ort); |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 1506 | spin_unlock_bh(&ort->fib6_table->tb6_lock); |
Paolo Abeni | b886d5f | 2017-10-19 16:07:10 +0200 | [diff] [blame] | 1507 | fib6_force_start_gc(net); |
| 1508 | } |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1509 | |
| 1510 | return err; |
| 1511 | } |
| 1512 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 1513 | void rt6_flush_exceptions(struct fib6_info *rt) |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1514 | { |
| 1515 | struct rt6_exception_bucket *bucket; |
| 1516 | struct rt6_exception *rt6_ex; |
| 1517 | struct hlist_node *tmp; |
| 1518 | int i; |
| 1519 | |
| 1520 | spin_lock_bh(&rt6_exception_lock); |
| 1521 | /* Prevent rt6_insert_exception() to recreate the bucket list */ |
| 1522 | rt->exception_bucket_flushed = 1; |
| 1523 | |
| 1524 | bucket = rcu_dereference_protected(rt->rt6i_exception_bucket, |
| 1525 | lockdep_is_held(&rt6_exception_lock)); |
| 1526 | if (!bucket) |
| 1527 | goto out; |
| 1528 | |
| 1529 | for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) { |
| 1530 | hlist_for_each_entry_safe(rt6_ex, tmp, &bucket->chain, hlist) |
| 1531 | rt6_remove_exception(bucket, rt6_ex); |
| 1532 | WARN_ON_ONCE(bucket->depth); |
| 1533 | bucket++; |
| 1534 | } |
| 1535 | |
| 1536 | out: |
| 1537 | spin_unlock_bh(&rt6_exception_lock); |
| 1538 | } |
| 1539 | |
| 1540 | /* Find cached rt in the hash table inside passed in rt |
| 1541 | * Caller has to hold rcu_read_lock() |
| 1542 | */ |
David Ahern | 7e4b512 | 2019-04-16 14:36:00 -0700 | [diff] [blame] | 1543 | static struct rt6_info *rt6_find_cached_rt(const struct fib6_result *res, |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1544 | struct in6_addr *daddr, |
| 1545 | struct in6_addr *saddr) |
| 1546 | { |
| 1547 | struct rt6_exception_bucket *bucket; |
| 1548 | struct in6_addr *src_key = NULL; |
| 1549 | struct rt6_exception *rt6_ex; |
David Ahern | 7e4b512 | 2019-04-16 14:36:00 -0700 | [diff] [blame] | 1550 | struct rt6_info *ret = NULL; |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1551 | |
David Ahern | 7e4b512 | 2019-04-16 14:36:00 -0700 | [diff] [blame] | 1552 | bucket = rcu_dereference(res->f6i->rt6i_exception_bucket); |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1553 | |
| 1554 | #ifdef CONFIG_IPV6_SUBTREES |
David Ahern | 7e4b512 | 2019-04-16 14:36:00 -0700 | [diff] [blame] | 1555 | /* fib6i_src.plen != 0 indicates f6i is in subtree |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1556 | * and exception table is indexed by a hash of |
David Ahern | 7e4b512 | 2019-04-16 14:36:00 -0700 | [diff] [blame] | 1557 | * both fib6_dst and fib6_src. |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1558 | * Otherwise, the exception table is indexed by |
David Ahern | 7e4b512 | 2019-04-16 14:36:00 -0700 | [diff] [blame] | 1559 | * a hash of only fib6_dst. |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1560 | */ |
David Ahern | 7e4b512 | 2019-04-16 14:36:00 -0700 | [diff] [blame] | 1561 | if (res->f6i->fib6_src.plen) |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1562 | src_key = saddr; |
| 1563 | #endif |
| 1564 | rt6_ex = __rt6_find_exception_rcu(&bucket, daddr, src_key); |
| 1565 | |
| 1566 | if (rt6_ex && !rt6_check_expired(rt6_ex->rt6i)) |
David Ahern | 7e4b512 | 2019-04-16 14:36:00 -0700 | [diff] [blame] | 1567 | ret = rt6_ex->rt6i; |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1568 | |
David Ahern | 7e4b512 | 2019-04-16 14:36:00 -0700 | [diff] [blame] | 1569 | return ret; |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1570 | } |
| 1571 | |
| 1572 | /* Remove the passed in cached rt from the hash table that contains it */ |
David Ahern | 23fb93a | 2018-04-17 17:33:23 -0700 | [diff] [blame] | 1573 | static int rt6_remove_exception_rt(struct rt6_info *rt) |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1574 | { |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1575 | struct rt6_exception_bucket *bucket; |
| 1576 | struct in6_addr *src_key = NULL; |
| 1577 | struct rt6_exception *rt6_ex; |
David Ahern | 8a14e46 | 2018-04-23 11:32:07 -0700 | [diff] [blame] | 1578 | struct fib6_info *from; |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1579 | int err; |
| 1580 | |
Eric Dumazet | 091311d | 2018-04-24 09:22:49 -0700 | [diff] [blame] | 1581 | from = rcu_dereference(rt->from); |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1582 | if (!from || |
Colin Ian King | 442d713 | 2017-10-10 19:10:30 +0100 | [diff] [blame] | 1583 | !(rt->rt6i_flags & RTF_CACHE)) |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1584 | return -EINVAL; |
| 1585 | |
| 1586 | if (!rcu_access_pointer(from->rt6i_exception_bucket)) |
| 1587 | return -ENOENT; |
| 1588 | |
| 1589 | spin_lock_bh(&rt6_exception_lock); |
| 1590 | bucket = rcu_dereference_protected(from->rt6i_exception_bucket, |
| 1591 | lockdep_is_held(&rt6_exception_lock)); |
| 1592 | #ifdef CONFIG_IPV6_SUBTREES |
| 1593 | /* rt6i_src.plen != 0 indicates 'from' is in subtree |
| 1594 | * and exception table is indexed by a hash of |
| 1595 | * both rt6i_dst and rt6i_src. |
| 1596 | * Otherwise, the exception table is indexed by |
| 1597 | * a hash of only rt6i_dst. |
| 1598 | */ |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 1599 | if (from->fib6_src.plen) |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1600 | src_key = &rt->rt6i_src.addr; |
| 1601 | #endif |
| 1602 | rt6_ex = __rt6_find_exception_spinlock(&bucket, |
| 1603 | &rt->rt6i_dst.addr, |
| 1604 | src_key); |
| 1605 | if (rt6_ex) { |
| 1606 | rt6_remove_exception(bucket, rt6_ex); |
| 1607 | err = 0; |
| 1608 | } else { |
| 1609 | err = -ENOENT; |
| 1610 | } |
| 1611 | |
| 1612 | spin_unlock_bh(&rt6_exception_lock); |
| 1613 | return err; |
| 1614 | } |
| 1615 | |
| 1616 | /* Find rt6_ex which contains the passed in rt cache and |
| 1617 | * refresh its stamp |
| 1618 | */ |
| 1619 | static void rt6_update_exception_stamp_rt(struct rt6_info *rt) |
| 1620 | { |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1621 | struct rt6_exception_bucket *bucket; |
| 1622 | struct in6_addr *src_key = NULL; |
| 1623 | struct rt6_exception *rt6_ex; |
Paolo Abeni | 193f368 | 2019-02-21 11:19:41 +0100 | [diff] [blame] | 1624 | struct fib6_info *from; |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1625 | |
| 1626 | rcu_read_lock(); |
Paolo Abeni | 193f368 | 2019-02-21 11:19:41 +0100 | [diff] [blame] | 1627 | from = rcu_dereference(rt->from); |
| 1628 | if (!from || !(rt->rt6i_flags & RTF_CACHE)) |
| 1629 | goto unlock; |
| 1630 | |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1631 | bucket = rcu_dereference(from->rt6i_exception_bucket); |
| 1632 | |
| 1633 | #ifdef CONFIG_IPV6_SUBTREES |
| 1634 | /* rt6i_src.plen != 0 indicates 'from' is in subtree |
| 1635 | * and exception table is indexed by a hash of |
| 1636 | * both rt6i_dst and rt6i_src. |
| 1637 | * Otherwise, the exception table is indexed by |
| 1638 | * a hash of only rt6i_dst. |
| 1639 | */ |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 1640 | if (from->fib6_src.plen) |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1641 | src_key = &rt->rt6i_src.addr; |
| 1642 | #endif |
| 1643 | rt6_ex = __rt6_find_exception_rcu(&bucket, |
| 1644 | &rt->rt6i_dst.addr, |
| 1645 | src_key); |
| 1646 | if (rt6_ex) |
| 1647 | rt6_ex->stamp = jiffies; |
| 1648 | |
Paolo Abeni | 193f368 | 2019-02-21 11:19:41 +0100 | [diff] [blame] | 1649 | unlock: |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1650 | rcu_read_unlock(); |
| 1651 | } |
| 1652 | |
Stefano Brivio | e9fa149 | 2018-03-06 11:10:19 +0100 | [diff] [blame] | 1653 | static bool rt6_mtu_change_route_allowed(struct inet6_dev *idev, |
| 1654 | struct rt6_info *rt, int mtu) |
| 1655 | { |
| 1656 | /* If the new MTU is lower than the route PMTU, this new MTU will be the |
| 1657 | * lowest MTU in the path: always allow updating the route PMTU to |
| 1658 | * reflect PMTU decreases. |
| 1659 | * |
| 1660 | * If the new MTU is higher, and the route PMTU is equal to the local |
| 1661 | * MTU, this means the old MTU is the lowest in the path, so allow |
| 1662 | * updating it: if other nodes now have lower MTUs, PMTU discovery will |
| 1663 | * handle this. |
| 1664 | */ |
| 1665 | |
| 1666 | if (dst_mtu(&rt->dst) >= mtu) |
| 1667 | return true; |
| 1668 | |
| 1669 | if (dst_mtu(&rt->dst) == idev->cnf.mtu6) |
| 1670 | return true; |
| 1671 | |
| 1672 | return false; |
| 1673 | } |
| 1674 | |
| 1675 | static void rt6_exceptions_update_pmtu(struct inet6_dev *idev, |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 1676 | struct fib6_info *rt, int mtu) |
Wei Wang | f5bbe7e | 2017-10-06 12:05:59 -0700 | [diff] [blame] | 1677 | { |
| 1678 | struct rt6_exception_bucket *bucket; |
| 1679 | struct rt6_exception *rt6_ex; |
| 1680 | int i; |
| 1681 | |
| 1682 | bucket = rcu_dereference_protected(rt->rt6i_exception_bucket, |
| 1683 | lockdep_is_held(&rt6_exception_lock)); |
| 1684 | |
Stefano Brivio | e9fa149 | 2018-03-06 11:10:19 +0100 | [diff] [blame] | 1685 | if (!bucket) |
| 1686 | return; |
| 1687 | |
| 1688 | for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) { |
| 1689 | hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) { |
| 1690 | struct rt6_info *entry = rt6_ex->rt6i; |
| 1691 | |
| 1692 | /* For RTF_CACHE with rt6i_pmtu == 0 (i.e. a redirected |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 1693 | * route), the metrics of its rt->from have already |
Stefano Brivio | e9fa149 | 2018-03-06 11:10:19 +0100 | [diff] [blame] | 1694 | * been updated. |
| 1695 | */ |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 1696 | if (dst_metric_raw(&entry->dst, RTAX_MTU) && |
Stefano Brivio | e9fa149 | 2018-03-06 11:10:19 +0100 | [diff] [blame] | 1697 | rt6_mtu_change_route_allowed(idev, entry, mtu)) |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 1698 | dst_metric_set(&entry->dst, RTAX_MTU, mtu); |
Wei Wang | f5bbe7e | 2017-10-06 12:05:59 -0700 | [diff] [blame] | 1699 | } |
Stefano Brivio | e9fa149 | 2018-03-06 11:10:19 +0100 | [diff] [blame] | 1700 | bucket++; |
Wei Wang | f5bbe7e | 2017-10-06 12:05:59 -0700 | [diff] [blame] | 1701 | } |
| 1702 | } |
| 1703 | |
Wei Wang | b16cb45 | 2017-10-06 12:06:00 -0700 | [diff] [blame] | 1704 | #define RTF_CACHE_GATEWAY (RTF_GATEWAY | RTF_CACHE) |
| 1705 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 1706 | static void rt6_exceptions_clean_tohost(struct fib6_info *rt, |
Wei Wang | b16cb45 | 2017-10-06 12:06:00 -0700 | [diff] [blame] | 1707 | struct in6_addr *gateway) |
| 1708 | { |
| 1709 | struct rt6_exception_bucket *bucket; |
| 1710 | struct rt6_exception *rt6_ex; |
| 1711 | struct hlist_node *tmp; |
| 1712 | int i; |
| 1713 | |
| 1714 | if (!rcu_access_pointer(rt->rt6i_exception_bucket)) |
| 1715 | return; |
| 1716 | |
| 1717 | spin_lock_bh(&rt6_exception_lock); |
| 1718 | bucket = rcu_dereference_protected(rt->rt6i_exception_bucket, |
| 1719 | lockdep_is_held(&rt6_exception_lock)); |
| 1720 | |
| 1721 | if (bucket) { |
| 1722 | for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) { |
| 1723 | hlist_for_each_entry_safe(rt6_ex, tmp, |
| 1724 | &bucket->chain, hlist) { |
| 1725 | struct rt6_info *entry = rt6_ex->rt6i; |
| 1726 | |
| 1727 | if ((entry->rt6i_flags & RTF_CACHE_GATEWAY) == |
| 1728 | RTF_CACHE_GATEWAY && |
| 1729 | ipv6_addr_equal(gateway, |
| 1730 | &entry->rt6i_gateway)) { |
| 1731 | rt6_remove_exception(bucket, rt6_ex); |
| 1732 | } |
| 1733 | } |
| 1734 | bucket++; |
| 1735 | } |
| 1736 | } |
| 1737 | |
| 1738 | spin_unlock_bh(&rt6_exception_lock); |
| 1739 | } |
| 1740 | |
Wei Wang | c757faa | 2017-10-06 12:06:01 -0700 | [diff] [blame] | 1741 | static void rt6_age_examine_exception(struct rt6_exception_bucket *bucket, |
| 1742 | struct rt6_exception *rt6_ex, |
| 1743 | struct fib6_gc_args *gc_args, |
| 1744 | unsigned long now) |
| 1745 | { |
| 1746 | struct rt6_info *rt = rt6_ex->rt6i; |
| 1747 | |
Paolo Abeni | 1859bac | 2017-10-19 16:07:11 +0200 | [diff] [blame] | 1748 | /* we are pruning and obsoleting aged-out and non gateway exceptions |
| 1749 | * even if others have still references to them, so that on next |
| 1750 | * dst_check() such references can be dropped. |
| 1751 | * EXPIRES exceptions - e.g. pmtu-generated ones are pruned when |
| 1752 | * expired, independently from their aging, as per RFC 8201 section 4 |
| 1753 | */ |
Wei Wang | 31afeb4 | 2018-01-26 11:40:17 -0800 | [diff] [blame] | 1754 | if (!(rt->rt6i_flags & RTF_EXPIRES)) { |
| 1755 | if (time_after_eq(now, rt->dst.lastuse + gc_args->timeout)) { |
| 1756 | RT6_TRACE("aging clone %p\n", rt); |
| 1757 | rt6_remove_exception(bucket, rt6_ex); |
| 1758 | return; |
| 1759 | } |
| 1760 | } else if (time_after(jiffies, rt->dst.expires)) { |
| 1761 | RT6_TRACE("purging expired route %p\n", rt); |
Wei Wang | c757faa | 2017-10-06 12:06:01 -0700 | [diff] [blame] | 1762 | rt6_remove_exception(bucket, rt6_ex); |
| 1763 | return; |
Wei Wang | 31afeb4 | 2018-01-26 11:40:17 -0800 | [diff] [blame] | 1764 | } |
| 1765 | |
| 1766 | if (rt->rt6i_flags & RTF_GATEWAY) { |
Wei Wang | c757faa | 2017-10-06 12:06:01 -0700 | [diff] [blame] | 1767 | struct neighbour *neigh; |
| 1768 | __u8 neigh_flags = 0; |
| 1769 | |
Eric Dumazet | 1bfa26f | 2018-03-23 07:56:58 -0700 | [diff] [blame] | 1770 | neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway); |
| 1771 | if (neigh) |
Wei Wang | c757faa | 2017-10-06 12:06:01 -0700 | [diff] [blame] | 1772 | neigh_flags = neigh->flags; |
Eric Dumazet | 1bfa26f | 2018-03-23 07:56:58 -0700 | [diff] [blame] | 1773 | |
Wei Wang | c757faa | 2017-10-06 12:06:01 -0700 | [diff] [blame] | 1774 | if (!(neigh_flags & NTF_ROUTER)) { |
| 1775 | RT6_TRACE("purging route %p via non-router but gateway\n", |
| 1776 | rt); |
| 1777 | rt6_remove_exception(bucket, rt6_ex); |
| 1778 | return; |
| 1779 | } |
| 1780 | } |
Wei Wang | 31afeb4 | 2018-01-26 11:40:17 -0800 | [diff] [blame] | 1781 | |
Wei Wang | c757faa | 2017-10-06 12:06:01 -0700 | [diff] [blame] | 1782 | gc_args->more++; |
| 1783 | } |
| 1784 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 1785 | void rt6_age_exceptions(struct fib6_info *rt, |
Wei Wang | c757faa | 2017-10-06 12:06:01 -0700 | [diff] [blame] | 1786 | struct fib6_gc_args *gc_args, |
| 1787 | unsigned long now) |
| 1788 | { |
| 1789 | struct rt6_exception_bucket *bucket; |
| 1790 | struct rt6_exception *rt6_ex; |
| 1791 | struct hlist_node *tmp; |
| 1792 | int i; |
| 1793 | |
| 1794 | if (!rcu_access_pointer(rt->rt6i_exception_bucket)) |
| 1795 | return; |
| 1796 | |
Eric Dumazet | 1bfa26f | 2018-03-23 07:56:58 -0700 | [diff] [blame] | 1797 | rcu_read_lock_bh(); |
| 1798 | spin_lock(&rt6_exception_lock); |
Wei Wang | c757faa | 2017-10-06 12:06:01 -0700 | [diff] [blame] | 1799 | bucket = rcu_dereference_protected(rt->rt6i_exception_bucket, |
| 1800 | lockdep_is_held(&rt6_exception_lock)); |
| 1801 | |
| 1802 | if (bucket) { |
| 1803 | for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) { |
| 1804 | hlist_for_each_entry_safe(rt6_ex, tmp, |
| 1805 | &bucket->chain, hlist) { |
| 1806 | rt6_age_examine_exception(bucket, rt6_ex, |
| 1807 | gc_args, now); |
| 1808 | } |
| 1809 | bucket++; |
| 1810 | } |
| 1811 | } |
Eric Dumazet | 1bfa26f | 2018-03-23 07:56:58 -0700 | [diff] [blame] | 1812 | spin_unlock(&rt6_exception_lock); |
| 1813 | rcu_read_unlock_bh(); |
Wei Wang | c757faa | 2017-10-06 12:06:01 -0700 | [diff] [blame] | 1814 | } |
| 1815 | |
David Ahern | 1d053da | 2018-05-09 20:34:21 -0700 | [diff] [blame] | 1816 | /* must be called with rcu lock held */ |
| 1817 | struct fib6_info *fib6_table_lookup(struct net *net, struct fib6_table *table, |
| 1818 | int oif, struct flowi6 *fl6, int strict) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1819 | { |
Martin KaFai Lau | 367efcb | 2014-10-20 13:42:45 -0700 | [diff] [blame] | 1820 | struct fib6_node *fn, *saved_fn; |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 1821 | struct fib6_info *f6i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1822 | |
David Ahern | 6454743 | 2018-05-09 20:34:19 -0700 | [diff] [blame] | 1823 | fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr); |
Martin KaFai Lau | 367efcb | 2014-10-20 13:42:45 -0700 | [diff] [blame] | 1824 | saved_fn = fn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1825 | |
David Ahern | ca25449 | 2015-10-12 11:47:10 -0700 | [diff] [blame] | 1826 | if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF) |
| 1827 | oif = 0; |
| 1828 | |
Martin KaFai Lau | a3c00e4 | 2014-10-20 13:42:43 -0700 | [diff] [blame] | 1829 | redo_rt6_select: |
David Ahern | 23fb93a | 2018-04-17 17:33:23 -0700 | [diff] [blame] | 1830 | f6i = rt6_select(net, fn, oif, strict); |
David Ahern | 23fb93a | 2018-04-17 17:33:23 -0700 | [diff] [blame] | 1831 | if (f6i == net->ipv6.fib6_null_entry) { |
Martin KaFai Lau | a3c00e4 | 2014-10-20 13:42:43 -0700 | [diff] [blame] | 1832 | fn = fib6_backtrack(fn, &fl6->saddr); |
| 1833 | if (fn) |
| 1834 | goto redo_rt6_select; |
Martin KaFai Lau | 367efcb | 2014-10-20 13:42:45 -0700 | [diff] [blame] | 1835 | else if (strict & RT6_LOOKUP_F_REACHABLE) { |
| 1836 | /* also consider unreachable route */ |
| 1837 | strict &= ~RT6_LOOKUP_F_REACHABLE; |
| 1838 | fn = saved_fn; |
| 1839 | goto redo_rt6_select; |
Martin KaFai Lau | 367efcb | 2014-10-20 13:42:45 -0700 | [diff] [blame] | 1840 | } |
Martin KaFai Lau | a3c00e4 | 2014-10-20 13:42:43 -0700 | [diff] [blame] | 1841 | } |
| 1842 | |
David Ahern | d4bea42 | 2018-05-09 20:34:24 -0700 | [diff] [blame] | 1843 | trace_fib6_table_lookup(net, f6i, table, fl6); |
YOSHIFUJI Hideaki | fb9de91 | 2006-03-20 16:59:08 -0800 | [diff] [blame] | 1844 | |
David Ahern | 1d053da | 2018-05-09 20:34:21 -0700 | [diff] [blame] | 1845 | return f6i; |
| 1846 | } |
| 1847 | |
| 1848 | struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table, |
| 1849 | int oif, struct flowi6 *fl6, |
| 1850 | const struct sk_buff *skb, int flags) |
| 1851 | { |
David Ahern | b1d4099 | 2019-04-16 14:35:59 -0700 | [diff] [blame] | 1852 | struct fib6_result res = {}; |
David Ahern | 1d053da | 2018-05-09 20:34:21 -0700 | [diff] [blame] | 1853 | struct rt6_info *rt; |
| 1854 | int strict = 0; |
| 1855 | |
| 1856 | strict |= flags & RT6_LOOKUP_F_IFACE; |
| 1857 | strict |= flags & RT6_LOOKUP_F_IGNORE_LINKSTATE; |
| 1858 | if (net->ipv6.devconf_all->forwarding == 0) |
| 1859 | strict |= RT6_LOOKUP_F_REACHABLE; |
| 1860 | |
| 1861 | rcu_read_lock(); |
| 1862 | |
David Ahern | b1d4099 | 2019-04-16 14:35:59 -0700 | [diff] [blame] | 1863 | res.f6i = fib6_table_lookup(net, table, oif, fl6, strict); |
| 1864 | if (res.f6i == net->ipv6.fib6_null_entry) { |
David Ahern | 421842e | 2018-04-17 17:33:18 -0700 | [diff] [blame] | 1865 | rt = net->ipv6.ip6_null_entry; |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 1866 | rcu_read_unlock(); |
Wei Wang | d3843fe | 2017-10-06 12:06:06 -0700 | [diff] [blame] | 1867 | dst_hold(&rt->dst); |
Wei Wang | d3843fe | 2017-10-06 12:06:06 -0700 | [diff] [blame] | 1868 | return rt; |
David Ahern | 23fb93a | 2018-04-17 17:33:23 -0700 | [diff] [blame] | 1869 | } |
| 1870 | |
David Ahern | b1d4099 | 2019-04-16 14:35:59 -0700 | [diff] [blame] | 1871 | fib6_select_path(net, &res, fl6, oif, false, skb, strict); |
David Ahern | d83009d | 2019-04-09 14:41:17 -0700 | [diff] [blame] | 1872 | |
David Ahern | 23fb93a | 2018-04-17 17:33:23 -0700 | [diff] [blame] | 1873 | /*Search through exception table */ |
David Ahern | 7e4b512 | 2019-04-16 14:36:00 -0700 | [diff] [blame] | 1874 | rt = rt6_find_cached_rt(&res, &fl6->daddr, &fl6->saddr); |
David Ahern | 23fb93a | 2018-04-17 17:33:23 -0700 | [diff] [blame] | 1875 | if (rt) { |
David Ahern | 10585b4 | 2019-03-20 09:24:50 -0700 | [diff] [blame] | 1876 | if (ip6_hold_safe(net, &rt)) |
Wei Wang | d3843fe | 2017-10-06 12:06:06 -0700 | [diff] [blame] | 1877 | dst_use_noref(&rt->dst, jiffies); |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 1878 | |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 1879 | rcu_read_unlock(); |
Martin KaFai Lau | d52d399 | 2015-05-22 20:56:06 -0700 | [diff] [blame] | 1880 | return rt; |
Martin KaFai Lau | 3da59bd | 2015-05-22 20:56:03 -0700 | [diff] [blame] | 1881 | } else if (unlikely((fl6->flowi6_flags & FLOWI_FLAG_KNOWN_NH) && |
David Ahern | b1d4099 | 2019-04-16 14:35:59 -0700 | [diff] [blame] | 1882 | !res.nh->fib_nh_gw_family)) { |
Martin KaFai Lau | 3da59bd | 2015-05-22 20:56:03 -0700 | [diff] [blame] | 1883 | /* Create a RTF_CACHE clone which will not be |
| 1884 | * owned by the fib6 tree. It is for the special case where |
| 1885 | * the daddr in the skb during the neighbor look-up is different |
| 1886 | * from the fl6->daddr used to look-up route here. |
| 1887 | */ |
Martin KaFai Lau | 3da59bd | 2015-05-22 20:56:03 -0700 | [diff] [blame] | 1888 | struct rt6_info *uncached_rt; |
| 1889 | |
David Ahern | 85bd05d | 2019-04-16 14:36:01 -0700 | [diff] [blame^] | 1890 | uncached_rt = ip6_rt_cache_alloc(&res, &fl6->daddr, NULL); |
Martin KaFai Lau | d52d399 | 2015-05-22 20:56:06 -0700 | [diff] [blame] | 1891 | |
David Ahern | 4d85cd0 | 2018-04-20 15:37:59 -0700 | [diff] [blame] | 1892 | rcu_read_unlock(); |
Martin KaFai Lau | 3da59bd | 2015-05-22 20:56:03 -0700 | [diff] [blame] | 1893 | |
Wei Wang | 1cfb71e | 2017-06-17 10:42:33 -0700 | [diff] [blame] | 1894 | if (uncached_rt) { |
| 1895 | /* Uncached_rt's refcnt is taken during ip6_rt_cache_alloc() |
| 1896 | * No need for another dst_hold() |
| 1897 | */ |
Martin KaFai Lau | 8d0b94a | 2015-05-22 20:56:04 -0700 | [diff] [blame] | 1898 | rt6_uncached_list_add(uncached_rt); |
Wei Wang | 81eb844 | 2017-10-06 12:06:11 -0700 | [diff] [blame] | 1899 | atomic_inc(&net->ipv6.rt6_stats->fib_rt_uncache); |
Wei Wang | 1cfb71e | 2017-06-17 10:42:33 -0700 | [diff] [blame] | 1900 | } else { |
Martin KaFai Lau | 3da59bd | 2015-05-22 20:56:03 -0700 | [diff] [blame] | 1901 | uncached_rt = net->ipv6.ip6_null_entry; |
Wei Wang | 1cfb71e | 2017-06-17 10:42:33 -0700 | [diff] [blame] | 1902 | dst_hold(&uncached_rt->dst); |
| 1903 | } |
David Ahern | b811580 | 2015-11-19 12:24:22 -0800 | [diff] [blame] | 1904 | |
Martin KaFai Lau | 3da59bd | 2015-05-22 20:56:03 -0700 | [diff] [blame] | 1905 | return uncached_rt; |
Martin KaFai Lau | d52d399 | 2015-05-22 20:56:06 -0700 | [diff] [blame] | 1906 | } else { |
| 1907 | /* Get a percpu copy */ |
| 1908 | |
| 1909 | struct rt6_info *pcpu_rt; |
| 1910 | |
Eric Dumazet | 951f788 | 2017-10-08 21:07:18 -0700 | [diff] [blame] | 1911 | local_bh_disable(); |
David Ahern | b1d4099 | 2019-04-16 14:35:59 -0700 | [diff] [blame] | 1912 | pcpu_rt = rt6_get_pcpu_route(res.f6i); |
Martin KaFai Lau | d52d399 | 2015-05-22 20:56:06 -0700 | [diff] [blame] | 1913 | |
David Ahern | 93531c6 | 2018-04-17 17:33:25 -0700 | [diff] [blame] | 1914 | if (!pcpu_rt) |
David Ahern | b1d4099 | 2019-04-16 14:35:59 -0700 | [diff] [blame] | 1915 | pcpu_rt = rt6_make_pcpu_route(net, res.f6i); |
David Ahern | 93531c6 | 2018-04-17 17:33:25 -0700 | [diff] [blame] | 1916 | |
Eric Dumazet | 951f788 | 2017-10-08 21:07:18 -0700 | [diff] [blame] | 1917 | local_bh_enable(); |
| 1918 | rcu_read_unlock(); |
David Ahern | d4bea42 | 2018-05-09 20:34:24 -0700 | [diff] [blame] | 1919 | |
Martin KaFai Lau | d52d399 | 2015-05-22 20:56:06 -0700 | [diff] [blame] | 1920 | return pcpu_rt; |
| 1921 | } |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1922 | } |
David Ahern | 9ff7438 | 2016-06-13 13:44:19 -0700 | [diff] [blame] | 1923 | EXPORT_SYMBOL_GPL(ip6_pol_route); |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1924 | |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 1925 | static struct rt6_info *ip6_pol_route_input(struct net *net, |
| 1926 | struct fib6_table *table, |
| 1927 | struct flowi6 *fl6, |
| 1928 | const struct sk_buff *skb, |
| 1929 | int flags) |
Pavel Emelyanov | 4acad72 | 2007-10-15 13:02:51 -0700 | [diff] [blame] | 1930 | { |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 1931 | return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, skb, flags); |
Pavel Emelyanov | 4acad72 | 2007-10-15 13:02:51 -0700 | [diff] [blame] | 1932 | } |
| 1933 | |
Mahesh Bandewar | d409b84 | 2016-09-16 12:59:08 -0700 | [diff] [blame] | 1934 | struct dst_entry *ip6_route_input_lookup(struct net *net, |
| 1935 | struct net_device *dev, |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 1936 | struct flowi6 *fl6, |
| 1937 | const struct sk_buff *skb, |
| 1938 | int flags) |
Shmulik Ladkani | 72331bc | 2012-04-01 04:03:45 +0000 | [diff] [blame] | 1939 | { |
| 1940 | if (rt6_need_strict(&fl6->daddr) && dev->type != ARPHRD_PIMREG) |
| 1941 | flags |= RT6_LOOKUP_F_IFACE; |
| 1942 | |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 1943 | return fib6_rule_lookup(net, fl6, skb, flags, ip6_pol_route_input); |
Shmulik Ladkani | 72331bc | 2012-04-01 04:03:45 +0000 | [diff] [blame] | 1944 | } |
Mahesh Bandewar | d409b84 | 2016-09-16 12:59:08 -0700 | [diff] [blame] | 1945 | EXPORT_SYMBOL_GPL(ip6_route_input_lookup); |
Shmulik Ladkani | 72331bc | 2012-04-01 04:03:45 +0000 | [diff] [blame] | 1946 | |
Jakub Sitnicki | 23aebda | 2017-08-23 09:58:29 +0200 | [diff] [blame] | 1947 | static void ip6_multipath_l3_keys(const struct sk_buff *skb, |
Roopa Prabhu | 5e5d6fe | 2018-02-28 22:43:22 -0500 | [diff] [blame] | 1948 | struct flow_keys *keys, |
| 1949 | struct flow_keys *flkeys) |
Jakub Sitnicki | 23aebda | 2017-08-23 09:58:29 +0200 | [diff] [blame] | 1950 | { |
| 1951 | const struct ipv6hdr *outer_iph = ipv6_hdr(skb); |
| 1952 | const struct ipv6hdr *key_iph = outer_iph; |
Roopa Prabhu | 5e5d6fe | 2018-02-28 22:43:22 -0500 | [diff] [blame] | 1953 | struct flow_keys *_flkeys = flkeys; |
Jakub Sitnicki | 23aebda | 2017-08-23 09:58:29 +0200 | [diff] [blame] | 1954 | const struct ipv6hdr *inner_iph; |
| 1955 | const struct icmp6hdr *icmph; |
| 1956 | struct ipv6hdr _inner_iph; |
Eric Dumazet | cea67a2 | 2018-04-29 09:54:59 -0700 | [diff] [blame] | 1957 | struct icmp6hdr _icmph; |
Jakub Sitnicki | 23aebda | 2017-08-23 09:58:29 +0200 | [diff] [blame] | 1958 | |
| 1959 | if (likely(outer_iph->nexthdr != IPPROTO_ICMPV6)) |
| 1960 | goto out; |
| 1961 | |
Eric Dumazet | cea67a2 | 2018-04-29 09:54:59 -0700 | [diff] [blame] | 1962 | icmph = skb_header_pointer(skb, skb_transport_offset(skb), |
| 1963 | sizeof(_icmph), &_icmph); |
| 1964 | if (!icmph) |
| 1965 | goto out; |
| 1966 | |
Jakub Sitnicki | 23aebda | 2017-08-23 09:58:29 +0200 | [diff] [blame] | 1967 | if (icmph->icmp6_type != ICMPV6_DEST_UNREACH && |
| 1968 | icmph->icmp6_type != ICMPV6_PKT_TOOBIG && |
| 1969 | icmph->icmp6_type != ICMPV6_TIME_EXCEED && |
| 1970 | icmph->icmp6_type != ICMPV6_PARAMPROB) |
| 1971 | goto out; |
| 1972 | |
| 1973 | inner_iph = skb_header_pointer(skb, |
| 1974 | skb_transport_offset(skb) + sizeof(*icmph), |
| 1975 | sizeof(_inner_iph), &_inner_iph); |
| 1976 | if (!inner_iph) |
| 1977 | goto out; |
| 1978 | |
| 1979 | key_iph = inner_iph; |
Roopa Prabhu | 5e5d6fe | 2018-02-28 22:43:22 -0500 | [diff] [blame] | 1980 | _flkeys = NULL; |
Jakub Sitnicki | 23aebda | 2017-08-23 09:58:29 +0200 | [diff] [blame] | 1981 | out: |
Roopa Prabhu | 5e5d6fe | 2018-02-28 22:43:22 -0500 | [diff] [blame] | 1982 | if (_flkeys) { |
| 1983 | keys->addrs.v6addrs.src = _flkeys->addrs.v6addrs.src; |
| 1984 | keys->addrs.v6addrs.dst = _flkeys->addrs.v6addrs.dst; |
| 1985 | keys->tags.flow_label = _flkeys->tags.flow_label; |
| 1986 | keys->basic.ip_proto = _flkeys->basic.ip_proto; |
| 1987 | } else { |
| 1988 | keys->addrs.v6addrs.src = key_iph->saddr; |
| 1989 | keys->addrs.v6addrs.dst = key_iph->daddr; |
Michal Kubecek | fa1be7e | 2018-06-04 11:36:05 +0200 | [diff] [blame] | 1990 | keys->tags.flow_label = ip6_flowlabel(key_iph); |
Roopa Prabhu | 5e5d6fe | 2018-02-28 22:43:22 -0500 | [diff] [blame] | 1991 | keys->basic.ip_proto = key_iph->nexthdr; |
| 1992 | } |
Jakub Sitnicki | 23aebda | 2017-08-23 09:58:29 +0200 | [diff] [blame] | 1993 | } |
| 1994 | |
| 1995 | /* 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] | 1996 | u32 rt6_multipath_hash(const struct net *net, const struct flowi6 *fl6, |
| 1997 | const struct sk_buff *skb, struct flow_keys *flkeys) |
Jakub Sitnicki | 23aebda | 2017-08-23 09:58:29 +0200 | [diff] [blame] | 1998 | { |
| 1999 | struct flow_keys hash_keys; |
David Ahern | 9a2a537 | 2018-03-02 08:32:15 -0800 | [diff] [blame] | 2000 | u32 mhash; |
Jakub Sitnicki | 23aebda | 2017-08-23 09:58:29 +0200 | [diff] [blame] | 2001 | |
David S. Miller | bbfa047 | 2018-03-12 11:09:33 -0400 | [diff] [blame] | 2002 | switch (ip6_multipath_hash_policy(net)) { |
David Ahern | b4bac17 | 2018-03-02 08:32:18 -0800 | [diff] [blame] | 2003 | case 0: |
| 2004 | memset(&hash_keys, 0, sizeof(hash_keys)); |
| 2005 | hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; |
| 2006 | if (skb) { |
| 2007 | ip6_multipath_l3_keys(skb, &hash_keys, flkeys); |
| 2008 | } else { |
| 2009 | hash_keys.addrs.v6addrs.src = fl6->saddr; |
| 2010 | hash_keys.addrs.v6addrs.dst = fl6->daddr; |
Michal Kubecek | fa1be7e | 2018-06-04 11:36:05 +0200 | [diff] [blame] | 2011 | hash_keys.tags.flow_label = (__force u32)flowi6_get_flowlabel(fl6); |
David Ahern | b4bac17 | 2018-03-02 08:32:18 -0800 | [diff] [blame] | 2012 | hash_keys.basic.ip_proto = fl6->flowi6_proto; |
| 2013 | } |
| 2014 | break; |
| 2015 | case 1: |
| 2016 | if (skb) { |
| 2017 | unsigned int flag = FLOW_DISSECTOR_F_STOP_AT_ENCAP; |
| 2018 | struct flow_keys keys; |
| 2019 | |
| 2020 | /* short-circuit if we already have L4 hash present */ |
| 2021 | if (skb->l4_hash) |
| 2022 | return skb_get_hash_raw(skb) >> 1; |
| 2023 | |
| 2024 | memset(&hash_keys, 0, sizeof(hash_keys)); |
| 2025 | |
| 2026 | if (!flkeys) { |
| 2027 | skb_flow_dissect_flow_keys(skb, &keys, flag); |
| 2028 | flkeys = &keys; |
| 2029 | } |
| 2030 | hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; |
| 2031 | hash_keys.addrs.v6addrs.src = flkeys->addrs.v6addrs.src; |
| 2032 | hash_keys.addrs.v6addrs.dst = flkeys->addrs.v6addrs.dst; |
| 2033 | hash_keys.ports.src = flkeys->ports.src; |
| 2034 | hash_keys.ports.dst = flkeys->ports.dst; |
| 2035 | hash_keys.basic.ip_proto = flkeys->basic.ip_proto; |
| 2036 | } else { |
| 2037 | memset(&hash_keys, 0, sizeof(hash_keys)); |
| 2038 | hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; |
| 2039 | hash_keys.addrs.v6addrs.src = fl6->saddr; |
| 2040 | hash_keys.addrs.v6addrs.dst = fl6->daddr; |
| 2041 | hash_keys.ports.src = fl6->fl6_sport; |
| 2042 | hash_keys.ports.dst = fl6->fl6_dport; |
| 2043 | hash_keys.basic.ip_proto = fl6->flowi6_proto; |
| 2044 | } |
| 2045 | break; |
Jakub Sitnicki | 23aebda | 2017-08-23 09:58:29 +0200 | [diff] [blame] | 2046 | } |
David Ahern | 9a2a537 | 2018-03-02 08:32:15 -0800 | [diff] [blame] | 2047 | mhash = flow_hash_from_keys(&hash_keys); |
Jakub Sitnicki | 23aebda | 2017-08-23 09:58:29 +0200 | [diff] [blame] | 2048 | |
David Ahern | 9a2a537 | 2018-03-02 08:32:15 -0800 | [diff] [blame] | 2049 | return mhash >> 1; |
Jakub Sitnicki | 23aebda | 2017-08-23 09:58:29 +0200 | [diff] [blame] | 2050 | } |
| 2051 | |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 2052 | void ip6_route_input(struct sk_buff *skb) |
| 2053 | { |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 2054 | const struct ipv6hdr *iph = ipv6_hdr(skb); |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 2055 | struct net *net = dev_net(skb->dev); |
Thomas Graf | adaa70b | 2006-10-13 15:01:03 -0700 | [diff] [blame] | 2056 | int flags = RT6_LOOKUP_F_HAS_SADDR; |
Jiri Benc | 904af04 | 2015-08-20 13:56:31 +0200 | [diff] [blame] | 2057 | struct ip_tunnel_info *tun_info; |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 2058 | struct flowi6 fl6 = { |
David Ahern | e0d56fd | 2016-09-10 12:09:57 -0700 | [diff] [blame] | 2059 | .flowi6_iif = skb->dev->ifindex, |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 2060 | .daddr = iph->daddr, |
| 2061 | .saddr = iph->saddr, |
YOSHIFUJI Hideaki / 吉藤英明 | 6502ca5 | 2013-01-13 05:01:51 +0000 | [diff] [blame] | 2062 | .flowlabel = ip6_flowinfo(iph), |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 2063 | .flowi6_mark = skb->mark, |
| 2064 | .flowi6_proto = iph->nexthdr, |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 2065 | }; |
Roopa Prabhu | 5e5d6fe | 2018-02-28 22:43:22 -0500 | [diff] [blame] | 2066 | struct flow_keys *flkeys = NULL, _flkeys; |
Thomas Graf | adaa70b | 2006-10-13 15:01:03 -0700 | [diff] [blame] | 2067 | |
Jiri Benc | 904af04 | 2015-08-20 13:56:31 +0200 | [diff] [blame] | 2068 | tun_info = skb_tunnel_info(skb); |
Jiri Benc | 46fa062 | 2015-08-28 20:48:19 +0200 | [diff] [blame] | 2069 | if (tun_info && !(tun_info->mode & IP_TUNNEL_INFO_TX)) |
Jiri Benc | 904af04 | 2015-08-20 13:56:31 +0200 | [diff] [blame] | 2070 | fl6.flowi6_tun_key.tun_id = tun_info->key.tun_id; |
Roopa Prabhu | 5e5d6fe | 2018-02-28 22:43:22 -0500 | [diff] [blame] | 2071 | |
| 2072 | if (fib6_rules_early_flow_dissect(net, skb, &fl6, &_flkeys)) |
| 2073 | flkeys = &_flkeys; |
| 2074 | |
Jakub Sitnicki | 23aebda | 2017-08-23 09:58:29 +0200 | [diff] [blame] | 2075 | if (unlikely(fl6.flowi6_proto == IPPROTO_ICMPV6)) |
David Ahern | b4bac17 | 2018-03-02 08:32:18 -0800 | [diff] [blame] | 2076 | fl6.mp_hash = rt6_multipath_hash(net, &fl6, skb, flkeys); |
Jiri Benc | 06e9d04 | 2015-08-20 13:56:26 +0200 | [diff] [blame] | 2077 | skb_dst_drop(skb); |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 2078 | skb_dst_set(skb, |
| 2079 | ip6_route_input_lookup(net, skb->dev, &fl6, skb, flags)); |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 2080 | } |
| 2081 | |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 2082 | static struct rt6_info *ip6_pol_route_output(struct net *net, |
| 2083 | struct fib6_table *table, |
| 2084 | struct flowi6 *fl6, |
| 2085 | const struct sk_buff *skb, |
| 2086 | int flags) |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 2087 | { |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 2088 | return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, skb, flags); |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 2089 | } |
| 2090 | |
Paolo Abeni | 6f21c96 | 2016-01-29 12:30:19 +0100 | [diff] [blame] | 2091 | struct dst_entry *ip6_route_output_flags(struct net *net, const struct sock *sk, |
| 2092 | struct flowi6 *fl6, int flags) |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 2093 | { |
David Ahern | d46a9d6 | 2015-10-21 08:42:22 -0700 | [diff] [blame] | 2094 | bool any_src; |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 2095 | |
Robert Shearman | 3ede0bb | 2018-09-19 13:56:53 +0100 | [diff] [blame] | 2096 | if (ipv6_addr_type(&fl6->daddr) & |
| 2097 | (IPV6_ADDR_MULTICAST | IPV6_ADDR_LINKLOCAL)) { |
David Ahern | 4c1feac | 2016-09-10 12:09:56 -0700 | [diff] [blame] | 2098 | struct dst_entry *dst; |
| 2099 | |
| 2100 | dst = l3mdev_link_scope_lookup(net, fl6); |
| 2101 | if (dst) |
| 2102 | return dst; |
| 2103 | } |
David Ahern | ca25449 | 2015-10-12 11:47:10 -0700 | [diff] [blame] | 2104 | |
Pavel Emelyanov | 1fb9489 | 2012-08-08 21:53:36 +0000 | [diff] [blame] | 2105 | fl6->flowi6_iif = LOOPBACK_IFINDEX; |
David McCullough | 4dc27d1c | 2012-06-25 15:42:26 +0000 | [diff] [blame] | 2106 | |
David Ahern | d46a9d6 | 2015-10-21 08:42:22 -0700 | [diff] [blame] | 2107 | any_src = ipv6_addr_any(&fl6->saddr); |
David Ahern | 741a11d | 2015-09-28 10:12:13 -0700 | [diff] [blame] | 2108 | if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr) || |
David Ahern | d46a9d6 | 2015-10-21 08:42:22 -0700 | [diff] [blame] | 2109 | (fl6->flowi6_oif && any_src)) |
YOSHIFUJI Hideaki | 77d16f4 | 2006-08-23 17:25:05 -0700 | [diff] [blame] | 2110 | flags |= RT6_LOOKUP_F_IFACE; |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 2111 | |
David Ahern | d46a9d6 | 2015-10-21 08:42:22 -0700 | [diff] [blame] | 2112 | if (!any_src) |
Thomas Graf | adaa70b | 2006-10-13 15:01:03 -0700 | [diff] [blame] | 2113 | flags |= RT6_LOOKUP_F_HAS_SADDR; |
YOSHIFUJI Hideaki / 吉藤英明 | 0c9a2ac | 2010-03-07 00:14:44 +0000 | [diff] [blame] | 2114 | else if (sk) |
| 2115 | flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs); |
Thomas Graf | adaa70b | 2006-10-13 15:01:03 -0700 | [diff] [blame] | 2116 | |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 2117 | return fib6_rule_lookup(net, fl6, NULL, flags, ip6_pol_route_output); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2118 | } |
Paolo Abeni | 6f21c96 | 2016-01-29 12:30:19 +0100 | [diff] [blame] | 2119 | EXPORT_SYMBOL_GPL(ip6_route_output_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2120 | |
David S. Miller | 2774c13 | 2011-03-01 14:59:04 -0800 | [diff] [blame] | 2121 | 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] | 2122 | { |
David S. Miller | 5c1e6aa | 2011-04-28 14:13:38 -0700 | [diff] [blame] | 2123 | struct rt6_info *rt, *ort = (struct rt6_info *) dst_orig; |
Wei Wang | 1dbe3252 | 2017-06-17 10:42:26 -0700 | [diff] [blame] | 2124 | struct net_device *loopback_dev = net->loopback_dev; |
David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 2125 | struct dst_entry *new = NULL; |
| 2126 | |
Wei Wang | 1dbe3252 | 2017-06-17 10:42:26 -0700 | [diff] [blame] | 2127 | rt = dst_alloc(&ip6_dst_blackhole_ops, loopback_dev, 1, |
Steffen Klassert | 62cf27e | 2017-10-09 08:39:43 +0200 | [diff] [blame] | 2128 | DST_OBSOLETE_DEAD, 0); |
David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 2129 | if (rt) { |
Martin KaFai Lau | 0a1f596 | 2015-10-15 16:39:58 -0700 | [diff] [blame] | 2130 | rt6_info_init(rt); |
Wei Wang | 81eb844 | 2017-10-06 12:06:11 -0700 | [diff] [blame] | 2131 | atomic_inc(&net->ipv6.rt6_stats->fib_rt_alloc); |
Martin KaFai Lau | 0a1f596 | 2015-10-15 16:39:58 -0700 | [diff] [blame] | 2132 | |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 2133 | new = &rt->dst; |
David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 2134 | new->__use = 1; |
Herbert Xu | 352e512 | 2007-11-13 21:34:06 -0800 | [diff] [blame] | 2135 | new->input = dst_discard; |
Eric W. Biederman | ede2059 | 2015-10-07 16:48:47 -0500 | [diff] [blame] | 2136 | new->output = dst_discard_out; |
David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 2137 | |
Martin KaFai Lau | 0a1f596 | 2015-10-15 16:39:58 -0700 | [diff] [blame] | 2138 | dst_copy_metrics(new, &ort->dst); |
David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 2139 | |
Wei Wang | 1dbe3252 | 2017-06-17 10:42:26 -0700 | [diff] [blame] | 2140 | rt->rt6i_idev = in6_dev_get(loopback_dev); |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 2141 | rt->rt6i_gateway = ort->rt6i_gateway; |
Martin KaFai Lau | 0a1f596 | 2015-10-15 16:39:58 -0700 | [diff] [blame] | 2142 | rt->rt6i_flags = ort->rt6i_flags & ~RTF_PCPU; |
David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 2143 | |
| 2144 | memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key)); |
| 2145 | #ifdef CONFIG_IPV6_SUBTREES |
| 2146 | memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key)); |
| 2147 | #endif |
David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 2148 | } |
| 2149 | |
David S. Miller | 69ead7a | 2011-03-01 14:45:33 -0800 | [diff] [blame] | 2150 | dst_release(dst_orig); |
| 2151 | return new ? new : ERR_PTR(-ENOMEM); |
David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 2152 | } |
David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 2153 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2154 | /* |
| 2155 | * Destination cache support functions |
| 2156 | */ |
| 2157 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 2158 | static bool fib6_check(struct fib6_info *f6i, u32 cookie) |
Martin KaFai Lau | 3da59bd | 2015-05-22 20:56:03 -0700 | [diff] [blame] | 2159 | { |
Steffen Klassert | 3614364 | 2017-08-25 09:05:42 +0200 | [diff] [blame] | 2160 | u32 rt_cookie = 0; |
Wei Wang | c5cff85 | 2017-08-21 09:47:10 -0700 | [diff] [blame] | 2161 | |
David Ahern | 8ae8697 | 2018-04-20 15:38:03 -0700 | [diff] [blame] | 2162 | if (!fib6_get_cookie_safe(f6i, &rt_cookie) || rt_cookie != cookie) |
David Ahern | 93531c6 | 2018-04-17 17:33:25 -0700 | [diff] [blame] | 2163 | return false; |
| 2164 | |
| 2165 | if (fib6_check_expired(f6i)) |
| 2166 | return false; |
| 2167 | |
| 2168 | return true; |
| 2169 | } |
| 2170 | |
David Ahern | a68886a | 2018-04-20 15:38:02 -0700 | [diff] [blame] | 2171 | static struct dst_entry *rt6_check(struct rt6_info *rt, |
| 2172 | struct fib6_info *from, |
| 2173 | u32 cookie) |
Martin KaFai Lau | 3da59bd | 2015-05-22 20:56:03 -0700 | [diff] [blame] | 2174 | { |
Wei Wang | c5cff85 | 2017-08-21 09:47:10 -0700 | [diff] [blame] | 2175 | u32 rt_cookie = 0; |
| 2176 | |
David Ahern | a68886a | 2018-04-20 15:38:02 -0700 | [diff] [blame] | 2177 | if ((from && !fib6_get_cookie_safe(from, &rt_cookie)) || |
David Ahern | 93531c6 | 2018-04-17 17:33:25 -0700 | [diff] [blame] | 2178 | rt_cookie != cookie) |
Martin KaFai Lau | 3da59bd | 2015-05-22 20:56:03 -0700 | [diff] [blame] | 2179 | return NULL; |
| 2180 | |
| 2181 | if (rt6_check_expired(rt)) |
| 2182 | return NULL; |
| 2183 | |
| 2184 | return &rt->dst; |
| 2185 | } |
| 2186 | |
David Ahern | a68886a | 2018-04-20 15:38:02 -0700 | [diff] [blame] | 2187 | static struct dst_entry *rt6_dst_from_check(struct rt6_info *rt, |
| 2188 | struct fib6_info *from, |
| 2189 | u32 cookie) |
Martin KaFai Lau | 3da59bd | 2015-05-22 20:56:03 -0700 | [diff] [blame] | 2190 | { |
Martin KaFai Lau | 5973fb1 | 2015-11-11 11:51:07 -0800 | [diff] [blame] | 2191 | if (!__rt6_check_expired(rt) && |
| 2192 | rt->dst.obsolete == DST_OBSOLETE_FORCE_CHK && |
David Ahern | a68886a | 2018-04-20 15:38:02 -0700 | [diff] [blame] | 2193 | fib6_check(from, cookie)) |
Martin KaFai Lau | 3da59bd | 2015-05-22 20:56:03 -0700 | [diff] [blame] | 2194 | return &rt->dst; |
| 2195 | else |
| 2196 | return NULL; |
| 2197 | } |
| 2198 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2199 | static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie) |
| 2200 | { |
David Ahern | a87b7dc | 2018-04-20 15:38:00 -0700 | [diff] [blame] | 2201 | struct dst_entry *dst_ret; |
David Ahern | a68886a | 2018-04-20 15:38:02 -0700 | [diff] [blame] | 2202 | struct fib6_info *from; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2203 | struct rt6_info *rt; |
| 2204 | |
David Ahern | a87b7dc | 2018-04-20 15:38:00 -0700 | [diff] [blame] | 2205 | rt = container_of(dst, struct rt6_info, dst); |
| 2206 | |
| 2207 | rcu_read_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2208 | |
Nicolas Dichtel | 6f3118b | 2012-09-10 22:09:46 +0000 | [diff] [blame] | 2209 | /* All IPV6 dsts are created with ->obsolete set to the value |
| 2210 | * DST_OBSOLETE_FORCE_CHK which forces validation calls down |
| 2211 | * into this function always. |
| 2212 | */ |
Hannes Frederic Sowa | e3bc10b | 2013-10-24 07:48:24 +0200 | [diff] [blame] | 2213 | |
David Ahern | a68886a | 2018-04-20 15:38:02 -0700 | [diff] [blame] | 2214 | from = rcu_dereference(rt->from); |
Martin KaFai Lau | 4b32b5a | 2015-04-28 13:03:06 -0700 | [diff] [blame] | 2215 | |
David Ahern | a68886a | 2018-04-20 15:38:02 -0700 | [diff] [blame] | 2216 | if (from && (rt->rt6i_flags & RTF_PCPU || |
| 2217 | unlikely(!list_empty(&rt->rt6i_uncached)))) |
| 2218 | dst_ret = rt6_dst_from_check(rt, from, cookie); |
Martin KaFai Lau | 3da59bd | 2015-05-22 20:56:03 -0700 | [diff] [blame] | 2219 | else |
David Ahern | a68886a | 2018-04-20 15:38:02 -0700 | [diff] [blame] | 2220 | dst_ret = rt6_check(rt, from, cookie); |
David Ahern | a87b7dc | 2018-04-20 15:38:00 -0700 | [diff] [blame] | 2221 | |
| 2222 | rcu_read_unlock(); |
| 2223 | |
| 2224 | return dst_ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2225 | } |
| 2226 | |
| 2227 | static struct dst_entry *ip6_negative_advice(struct dst_entry *dst) |
| 2228 | { |
| 2229 | struct rt6_info *rt = (struct rt6_info *) dst; |
| 2230 | |
| 2231 | if (rt) { |
YOSHIFUJI Hideaki / 吉藤英明 | 54c1a85 | 2010-03-28 07:15:45 +0000 | [diff] [blame] | 2232 | if (rt->rt6i_flags & RTF_CACHE) { |
David Ahern | c3c14da | 2018-04-23 11:32:06 -0700 | [diff] [blame] | 2233 | rcu_read_lock(); |
YOSHIFUJI Hideaki / 吉藤英明 | 54c1a85 | 2010-03-28 07:15:45 +0000 | [diff] [blame] | 2234 | if (rt6_check_expired(rt)) { |
David Ahern | 93531c6 | 2018-04-17 17:33:25 -0700 | [diff] [blame] | 2235 | rt6_remove_exception_rt(rt); |
YOSHIFUJI Hideaki / 吉藤英明 | 54c1a85 | 2010-03-28 07:15:45 +0000 | [diff] [blame] | 2236 | dst = NULL; |
| 2237 | } |
David Ahern | c3c14da | 2018-04-23 11:32:06 -0700 | [diff] [blame] | 2238 | rcu_read_unlock(); |
YOSHIFUJI Hideaki / 吉藤英明 | 54c1a85 | 2010-03-28 07:15:45 +0000 | [diff] [blame] | 2239 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2240 | dst_release(dst); |
YOSHIFUJI Hideaki / 吉藤英明 | 54c1a85 | 2010-03-28 07:15:45 +0000 | [diff] [blame] | 2241 | dst = NULL; |
| 2242 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2243 | } |
YOSHIFUJI Hideaki / 吉藤英明 | 54c1a85 | 2010-03-28 07:15:45 +0000 | [diff] [blame] | 2244 | return dst; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2245 | } |
| 2246 | |
| 2247 | static void ip6_link_failure(struct sk_buff *skb) |
| 2248 | { |
| 2249 | struct rt6_info *rt; |
| 2250 | |
Alexey Dobriyan | 3ffe533 | 2010-02-18 08:25:24 +0000 | [diff] [blame] | 2251 | icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2252 | |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 2253 | rt = (struct rt6_info *) skb_dst(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2254 | if (rt) { |
David Ahern | 8a14e46 | 2018-04-23 11:32:07 -0700 | [diff] [blame] | 2255 | rcu_read_lock(); |
Hannes Frederic Sowa | 1eb4f75 | 2013-07-10 23:00:57 +0200 | [diff] [blame] | 2256 | if (rt->rt6i_flags & RTF_CACHE) { |
Xin Long | 761f602 | 2018-11-14 00:48:28 +0800 | [diff] [blame] | 2257 | rt6_remove_exception_rt(rt); |
Wei Wang | c5cff85 | 2017-08-21 09:47:10 -0700 | [diff] [blame] | 2258 | } else { |
David Ahern | a68886a | 2018-04-20 15:38:02 -0700 | [diff] [blame] | 2259 | struct fib6_info *from; |
Wei Wang | c5cff85 | 2017-08-21 09:47:10 -0700 | [diff] [blame] | 2260 | struct fib6_node *fn; |
| 2261 | |
David Ahern | a68886a | 2018-04-20 15:38:02 -0700 | [diff] [blame] | 2262 | from = rcu_dereference(rt->from); |
| 2263 | if (from) { |
| 2264 | fn = rcu_dereference(from->fib6_node); |
| 2265 | if (fn && (rt->rt6i_flags & RTF_DEFAULT)) |
| 2266 | fn->fn_sernum = -1; |
| 2267 | } |
Hannes Frederic Sowa | 1eb4f75 | 2013-07-10 23:00:57 +0200 | [diff] [blame] | 2268 | } |
David Ahern | 8a14e46 | 2018-04-23 11:32:07 -0700 | [diff] [blame] | 2269 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2270 | } |
| 2271 | } |
| 2272 | |
David Ahern | 6a3e030 | 2018-04-20 15:37:57 -0700 | [diff] [blame] | 2273 | static void rt6_update_expires(struct rt6_info *rt0, int timeout) |
| 2274 | { |
David Ahern | a68886a | 2018-04-20 15:38:02 -0700 | [diff] [blame] | 2275 | if (!(rt0->rt6i_flags & RTF_EXPIRES)) { |
| 2276 | struct fib6_info *from; |
| 2277 | |
| 2278 | rcu_read_lock(); |
| 2279 | from = rcu_dereference(rt0->from); |
| 2280 | if (from) |
| 2281 | rt0->dst.expires = from->expires; |
| 2282 | rcu_read_unlock(); |
| 2283 | } |
David Ahern | 6a3e030 | 2018-04-20 15:37:57 -0700 | [diff] [blame] | 2284 | |
| 2285 | dst_set_expires(&rt0->dst, timeout); |
| 2286 | rt0->rt6i_flags |= RTF_EXPIRES; |
| 2287 | } |
| 2288 | |
Martin KaFai Lau | 45e4fd2 | 2015-05-22 20:56:00 -0700 | [diff] [blame] | 2289 | static void rt6_do_update_pmtu(struct rt6_info *rt, u32 mtu) |
| 2290 | { |
| 2291 | struct net *net = dev_net(rt->dst.dev); |
| 2292 | |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 2293 | dst_metric_set(&rt->dst, RTAX_MTU, mtu); |
Martin KaFai Lau | 45e4fd2 | 2015-05-22 20:56:00 -0700 | [diff] [blame] | 2294 | rt->rt6i_flags |= RTF_MODIFIED; |
Martin KaFai Lau | 45e4fd2 | 2015-05-22 20:56:00 -0700 | [diff] [blame] | 2295 | rt6_update_expires(rt, net->ipv6.sysctl.ip6_rt_mtu_expires); |
| 2296 | } |
| 2297 | |
Martin KaFai Lau | 0d3f6d2 | 2015-11-11 11:51:06 -0800 | [diff] [blame] | 2298 | static bool rt6_cache_allowed_for_pmtu(const struct rt6_info *rt) |
| 2299 | { |
| 2300 | return !(rt->rt6i_flags & RTF_CACHE) && |
Paolo Abeni | 1490ed2 | 2019-02-15 18:15:37 +0100 | [diff] [blame] | 2301 | (rt->rt6i_flags & RTF_PCPU || rcu_access_pointer(rt->from)); |
Martin KaFai Lau | 0d3f6d2 | 2015-11-11 11:51:06 -0800 | [diff] [blame] | 2302 | } |
| 2303 | |
Martin KaFai Lau | 45e4fd2 | 2015-05-22 20:56:00 -0700 | [diff] [blame] | 2304 | static void __ip6_rt_update_pmtu(struct dst_entry *dst, const struct sock *sk, |
| 2305 | const struct ipv6hdr *iph, u32 mtu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2306 | { |
Julian Anastasov | 0dec879 | 2017-02-06 23:14:16 +0200 | [diff] [blame] | 2307 | const struct in6_addr *daddr, *saddr; |
Ian Morris | 67ba415 | 2014-08-24 21:53:10 +0100 | [diff] [blame] | 2308 | struct rt6_info *rt6 = (struct rt6_info *)dst; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2309 | |
Xin Long | 19bda36 | 2016-10-28 18:18:01 +0800 | [diff] [blame] | 2310 | if (dst_metric_locked(dst, RTAX_MTU)) |
| 2311 | return; |
| 2312 | |
Julian Anastasov | 0dec879 | 2017-02-06 23:14:16 +0200 | [diff] [blame] | 2313 | if (iph) { |
| 2314 | daddr = &iph->daddr; |
| 2315 | saddr = &iph->saddr; |
| 2316 | } else if (sk) { |
| 2317 | daddr = &sk->sk_v6_daddr; |
| 2318 | saddr = &inet6_sk(sk)->saddr; |
| 2319 | } else { |
| 2320 | daddr = NULL; |
| 2321 | saddr = NULL; |
| 2322 | } |
| 2323 | dst_confirm_neigh(dst, daddr); |
Martin KaFai Lau | 45e4fd2 | 2015-05-22 20:56:00 -0700 | [diff] [blame] | 2324 | mtu = max_t(u32, mtu, IPV6_MIN_MTU); |
| 2325 | if (mtu >= dst_mtu(dst)) |
| 2326 | return; |
David S. Miller | 81aded2 | 2012-06-15 14:54:11 -0700 | [diff] [blame] | 2327 | |
Martin KaFai Lau | 0d3f6d2 | 2015-11-11 11:51:06 -0800 | [diff] [blame] | 2328 | if (!rt6_cache_allowed_for_pmtu(rt6)) { |
Martin KaFai Lau | 45e4fd2 | 2015-05-22 20:56:00 -0700 | [diff] [blame] | 2329 | rt6_do_update_pmtu(rt6, mtu); |
Wei Wang | 2b760fc | 2017-10-06 12:06:03 -0700 | [diff] [blame] | 2330 | /* update rt6_ex->stamp for cache */ |
| 2331 | if (rt6->rt6i_flags & RTF_CACHE) |
| 2332 | rt6_update_exception_stamp_rt(rt6); |
Julian Anastasov | 0dec879 | 2017-02-06 23:14:16 +0200 | [diff] [blame] | 2333 | } else if (daddr) { |
David Ahern | 85bd05d | 2019-04-16 14:36:01 -0700 | [diff] [blame^] | 2334 | struct fib6_result res = {}; |
Martin KaFai Lau | 45e4fd2 | 2015-05-22 20:56:00 -0700 | [diff] [blame] | 2335 | struct rt6_info *nrt6; |
Hagen Paul Pfeifer | 9d28971 | 2015-01-15 22:34:25 +0100 | [diff] [blame] | 2336 | |
David Ahern | 4d85cd0 | 2018-04-20 15:37:59 -0700 | [diff] [blame] | 2337 | rcu_read_lock(); |
David Ahern | 85bd05d | 2019-04-16 14:36:01 -0700 | [diff] [blame^] | 2338 | res.f6i = rcu_dereference(rt6->from); |
| 2339 | if (!res.f6i) { |
Jonathan Lemon | 9c69a13 | 2019-04-14 14:21:29 -0700 | [diff] [blame] | 2340 | rcu_read_unlock(); |
| 2341 | return; |
| 2342 | } |
David Ahern | 85bd05d | 2019-04-16 14:36:01 -0700 | [diff] [blame^] | 2343 | res.nh = &res.f6i->fib6_nh; |
| 2344 | nrt6 = ip6_rt_cache_alloc(&res, daddr, saddr); |
Martin KaFai Lau | 45e4fd2 | 2015-05-22 20:56:00 -0700 | [diff] [blame] | 2345 | if (nrt6) { |
| 2346 | rt6_do_update_pmtu(nrt6, mtu); |
David Ahern | 85bd05d | 2019-04-16 14:36:01 -0700 | [diff] [blame^] | 2347 | if (rt6_insert_exception(nrt6, res.f6i)) |
Wei Wang | 2b760fc | 2017-10-06 12:06:03 -0700 | [diff] [blame] | 2348 | dst_release_immediate(&nrt6->dst); |
Martin KaFai Lau | 45e4fd2 | 2015-05-22 20:56:00 -0700 | [diff] [blame] | 2349 | } |
David Ahern | a68886a | 2018-04-20 15:38:02 -0700 | [diff] [blame] | 2350 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2351 | } |
| 2352 | } |
| 2353 | |
Martin KaFai Lau | 45e4fd2 | 2015-05-22 20:56:00 -0700 | [diff] [blame] | 2354 | static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk, |
| 2355 | struct sk_buff *skb, u32 mtu) |
| 2356 | { |
| 2357 | __ip6_rt_update_pmtu(dst, sk, skb ? ipv6_hdr(skb) : NULL, mtu); |
| 2358 | } |
| 2359 | |
David S. Miller | 42ae66c | 2012-06-15 20:01:57 -0700 | [diff] [blame] | 2360 | 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] | 2361 | int oif, u32 mark, kuid_t uid) |
David S. Miller | 81aded2 | 2012-06-15 14:54:11 -0700 | [diff] [blame] | 2362 | { |
| 2363 | const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data; |
| 2364 | struct dst_entry *dst; |
Maciej Żenczykowski | dc92095 | 2018-09-29 23:44:51 -0700 | [diff] [blame] | 2365 | struct flowi6 fl6 = { |
| 2366 | .flowi6_oif = oif, |
| 2367 | .flowi6_mark = mark ? mark : IP6_REPLY_MARK(net, skb->mark), |
| 2368 | .daddr = iph->daddr, |
| 2369 | .saddr = iph->saddr, |
| 2370 | .flowlabel = ip6_flowinfo(iph), |
| 2371 | .flowi6_uid = uid, |
| 2372 | }; |
David S. Miller | 81aded2 | 2012-06-15 14:54:11 -0700 | [diff] [blame] | 2373 | |
| 2374 | dst = ip6_route_output(net, NULL, &fl6); |
| 2375 | if (!dst->error) |
Martin KaFai Lau | 45e4fd2 | 2015-05-22 20:56:00 -0700 | [diff] [blame] | 2376 | __ip6_rt_update_pmtu(dst, NULL, iph, ntohl(mtu)); |
David S. Miller | 81aded2 | 2012-06-15 14:54:11 -0700 | [diff] [blame] | 2377 | dst_release(dst); |
| 2378 | } |
| 2379 | EXPORT_SYMBOL_GPL(ip6_update_pmtu); |
| 2380 | |
| 2381 | void ip6_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, __be32 mtu) |
| 2382 | { |
David Ahern | 7ddacfa | 2018-11-18 10:45:30 -0800 | [diff] [blame] | 2383 | int oif = sk->sk_bound_dev_if; |
Martin KaFai Lau | 33c162a | 2016-04-11 15:29:36 -0700 | [diff] [blame] | 2384 | struct dst_entry *dst; |
| 2385 | |
David Ahern | 7ddacfa | 2018-11-18 10:45:30 -0800 | [diff] [blame] | 2386 | if (!oif && skb->dev) |
| 2387 | oif = l3mdev_master_ifindex(skb->dev); |
| 2388 | |
| 2389 | 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] | 2390 | |
| 2391 | dst = __sk_dst_get(sk); |
| 2392 | if (!dst || !dst->obsolete || |
| 2393 | dst->ops->check(dst, inet6_sk(sk)->dst_cookie)) |
| 2394 | return; |
| 2395 | |
| 2396 | bh_lock_sock(sk); |
| 2397 | if (!sock_owned_by_user(sk) && !ipv6_addr_v4mapped(&sk->sk_v6_daddr)) |
| 2398 | ip6_datagram_dst_update(sk, false); |
| 2399 | bh_unlock_sock(sk); |
David S. Miller | 81aded2 | 2012-06-15 14:54:11 -0700 | [diff] [blame] | 2400 | } |
| 2401 | EXPORT_SYMBOL_GPL(ip6_sk_update_pmtu); |
| 2402 | |
Alexey Kodanev | 7d6850f | 2018-04-03 15:00:07 +0300 | [diff] [blame] | 2403 | void ip6_sk_dst_store_flow(struct sock *sk, struct dst_entry *dst, |
| 2404 | const struct flowi6 *fl6) |
| 2405 | { |
| 2406 | #ifdef CONFIG_IPV6_SUBTREES |
| 2407 | struct ipv6_pinfo *np = inet6_sk(sk); |
| 2408 | #endif |
| 2409 | |
| 2410 | ip6_dst_store(sk, dst, |
| 2411 | ipv6_addr_equal(&fl6->daddr, &sk->sk_v6_daddr) ? |
| 2412 | &sk->sk_v6_daddr : NULL, |
| 2413 | #ifdef CONFIG_IPV6_SUBTREES |
| 2414 | ipv6_addr_equal(&fl6->saddr, &np->saddr) ? |
| 2415 | &np->saddr : |
| 2416 | #endif |
| 2417 | NULL); |
| 2418 | } |
| 2419 | |
David Ahern | 0b34eb0 | 2019-04-09 14:41:19 -0700 | [diff] [blame] | 2420 | static bool ip6_redirect_nh_match(struct fib6_info *f6i, |
| 2421 | struct fib6_nh *nh, |
| 2422 | struct flowi6 *fl6, |
| 2423 | const struct in6_addr *gw, |
| 2424 | struct rt6_info **ret) |
| 2425 | { |
| 2426 | if (nh->fib_nh_flags & RTNH_F_DEAD || !nh->fib_nh_gw_family || |
| 2427 | fl6->flowi6_oif != nh->fib_nh_dev->ifindex) |
| 2428 | return false; |
| 2429 | |
| 2430 | /* rt_cache's gateway might be different from its 'parent' |
| 2431 | * in the case of an ip redirect. |
| 2432 | * So we keep searching in the exception table if the gateway |
| 2433 | * is different. |
| 2434 | */ |
| 2435 | if (!ipv6_addr_equal(gw, &nh->fib_nh_gw6)) { |
David Ahern | 7e4b512 | 2019-04-16 14:36:00 -0700 | [diff] [blame] | 2436 | struct fib6_result res = { |
| 2437 | .f6i = f6i, |
| 2438 | }; |
David Ahern | 0b34eb0 | 2019-04-09 14:41:19 -0700 | [diff] [blame] | 2439 | struct rt6_info *rt_cache; |
| 2440 | |
David Ahern | 7e4b512 | 2019-04-16 14:36:00 -0700 | [diff] [blame] | 2441 | rt_cache = rt6_find_cached_rt(&res, &fl6->daddr, &fl6->saddr); |
David Ahern | 0b34eb0 | 2019-04-09 14:41:19 -0700 | [diff] [blame] | 2442 | if (rt_cache && |
| 2443 | ipv6_addr_equal(gw, &rt_cache->rt6i_gateway)) { |
| 2444 | *ret = rt_cache; |
| 2445 | return true; |
| 2446 | } |
| 2447 | return false; |
| 2448 | } |
| 2449 | return true; |
| 2450 | } |
| 2451 | |
Duan Jiong | b55b76b | 2013-09-04 19:44:21 +0800 | [diff] [blame] | 2452 | /* Handle redirects */ |
| 2453 | struct ip6rd_flowi { |
| 2454 | struct flowi6 fl6; |
| 2455 | struct in6_addr gateway; |
| 2456 | }; |
| 2457 | |
| 2458 | static struct rt6_info *__ip6_route_redirect(struct net *net, |
| 2459 | struct fib6_table *table, |
| 2460 | struct flowi6 *fl6, |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 2461 | const struct sk_buff *skb, |
Duan Jiong | b55b76b | 2013-09-04 19:44:21 +0800 | [diff] [blame] | 2462 | int flags) |
| 2463 | { |
| 2464 | struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl6; |
David Ahern | 0b34eb0 | 2019-04-09 14:41:19 -0700 | [diff] [blame] | 2465 | struct rt6_info *ret = NULL; |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 2466 | struct fib6_info *rt; |
Duan Jiong | b55b76b | 2013-09-04 19:44:21 +0800 | [diff] [blame] | 2467 | struct fib6_node *fn; |
| 2468 | |
| 2469 | /* Get the "current" route for this destination and |
Alexander Alemayhu | 67c408c | 2017-01-07 23:53:00 +0100 | [diff] [blame] | 2470 | * check if the redirect has come from appropriate router. |
Duan Jiong | b55b76b | 2013-09-04 19:44:21 +0800 | [diff] [blame] | 2471 | * |
| 2472 | * RFC 4861 specifies that redirects should only be |
| 2473 | * accepted if they come from the nexthop to the target. |
| 2474 | * Due to the way the routes are chosen, this notion |
| 2475 | * is a bit fuzzy and one might need to check all possible |
| 2476 | * routes. |
| 2477 | */ |
| 2478 | |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 2479 | rcu_read_lock(); |
David Ahern | 6454743 | 2018-05-09 20:34:19 -0700 | [diff] [blame] | 2480 | fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr); |
Duan Jiong | b55b76b | 2013-09-04 19:44:21 +0800 | [diff] [blame] | 2481 | restart: |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 2482 | for_each_fib6_node_rt_rcu(fn) { |
David Ahern | 1489568 | 2018-04-17 17:33:17 -0700 | [diff] [blame] | 2483 | if (fib6_check_expired(rt)) |
Duan Jiong | b55b76b | 2013-09-04 19:44:21 +0800 | [diff] [blame] | 2484 | continue; |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 2485 | if (rt->fib6_flags & RTF_REJECT) |
Duan Jiong | b55b76b | 2013-09-04 19:44:21 +0800 | [diff] [blame] | 2486 | break; |
David Ahern | 0b34eb0 | 2019-04-09 14:41:19 -0700 | [diff] [blame] | 2487 | if (ip6_redirect_nh_match(rt, &rt->fib6_nh, fl6, |
| 2488 | &rdfl->gateway, &ret)) |
| 2489 | goto out; |
Duan Jiong | b55b76b | 2013-09-04 19:44:21 +0800 | [diff] [blame] | 2490 | } |
| 2491 | |
| 2492 | if (!rt) |
David Ahern | 421842e | 2018-04-17 17:33:18 -0700 | [diff] [blame] | 2493 | rt = net->ipv6.fib6_null_entry; |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 2494 | else if (rt->fib6_flags & RTF_REJECT) { |
David Ahern | 23fb93a | 2018-04-17 17:33:23 -0700 | [diff] [blame] | 2495 | ret = net->ipv6.ip6_null_entry; |
Martin KaFai Lau | b0a1ba5 | 2015-01-20 19:16:02 -0800 | [diff] [blame] | 2496 | goto out; |
| 2497 | } |
| 2498 | |
David Ahern | 421842e | 2018-04-17 17:33:18 -0700 | [diff] [blame] | 2499 | if (rt == net->ipv6.fib6_null_entry) { |
Martin KaFai Lau | a3c00e4 | 2014-10-20 13:42:43 -0700 | [diff] [blame] | 2500 | fn = fib6_backtrack(fn, &fl6->saddr); |
| 2501 | if (fn) |
| 2502 | goto restart; |
Duan Jiong | b55b76b | 2013-09-04 19:44:21 +0800 | [diff] [blame] | 2503 | } |
Martin KaFai Lau | a3c00e4 | 2014-10-20 13:42:43 -0700 | [diff] [blame] | 2504 | |
Martin KaFai Lau | b0a1ba5 | 2015-01-20 19:16:02 -0800 | [diff] [blame] | 2505 | out: |
David Ahern | 23fb93a | 2018-04-17 17:33:23 -0700 | [diff] [blame] | 2506 | if (ret) |
David Ahern | 10585b4 | 2019-03-20 09:24:50 -0700 | [diff] [blame] | 2507 | ip6_hold_safe(net, &ret); |
David Ahern | 23fb93a | 2018-04-17 17:33:23 -0700 | [diff] [blame] | 2508 | else |
| 2509 | ret = ip6_create_rt_rcu(rt); |
Duan Jiong | b55b76b | 2013-09-04 19:44:21 +0800 | [diff] [blame] | 2510 | |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 2511 | rcu_read_unlock(); |
Duan Jiong | b55b76b | 2013-09-04 19:44:21 +0800 | [diff] [blame] | 2512 | |
Paolo Abeni | b65f164 | 2017-10-19 09:31:43 +0200 | [diff] [blame] | 2513 | trace_fib6_table_lookup(net, rt, table, fl6); |
David Ahern | 23fb93a | 2018-04-17 17:33:23 -0700 | [diff] [blame] | 2514 | return ret; |
Duan Jiong | b55b76b | 2013-09-04 19:44:21 +0800 | [diff] [blame] | 2515 | }; |
| 2516 | |
| 2517 | static struct dst_entry *ip6_route_redirect(struct net *net, |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 2518 | const struct flowi6 *fl6, |
| 2519 | const struct sk_buff *skb, |
| 2520 | const struct in6_addr *gateway) |
Duan Jiong | b55b76b | 2013-09-04 19:44:21 +0800 | [diff] [blame] | 2521 | { |
| 2522 | int flags = RT6_LOOKUP_F_HAS_SADDR; |
| 2523 | struct ip6rd_flowi rdfl; |
| 2524 | |
| 2525 | rdfl.fl6 = *fl6; |
| 2526 | rdfl.gateway = *gateway; |
| 2527 | |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 2528 | return fib6_rule_lookup(net, &rdfl.fl6, skb, |
Duan Jiong | b55b76b | 2013-09-04 19:44:21 +0800 | [diff] [blame] | 2529 | flags, __ip6_route_redirect); |
| 2530 | } |
| 2531 | |
Lorenzo Colitti | e2d118a | 2016-11-04 02:23:43 +0900 | [diff] [blame] | 2532 | void ip6_redirect(struct sk_buff *skb, struct net *net, int oif, u32 mark, |
| 2533 | kuid_t uid) |
David S. Miller | 3a5ad2e | 2012-07-12 00:08:07 -0700 | [diff] [blame] | 2534 | { |
| 2535 | const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data; |
| 2536 | struct dst_entry *dst; |
Maciej Żenczykowski | 1f7f10a | 2018-09-29 23:44:48 -0700 | [diff] [blame] | 2537 | struct flowi6 fl6 = { |
| 2538 | .flowi6_iif = LOOPBACK_IFINDEX, |
| 2539 | .flowi6_oif = oif, |
| 2540 | .flowi6_mark = mark, |
| 2541 | .daddr = iph->daddr, |
| 2542 | .saddr = iph->saddr, |
| 2543 | .flowlabel = ip6_flowinfo(iph), |
| 2544 | .flowi6_uid = uid, |
| 2545 | }; |
David S. Miller | 3a5ad2e | 2012-07-12 00:08:07 -0700 | [diff] [blame] | 2546 | |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 2547 | dst = ip6_route_redirect(net, &fl6, skb, &ipv6_hdr(skb)->saddr); |
Duan Jiong | b55b76b | 2013-09-04 19:44:21 +0800 | [diff] [blame] | 2548 | rt6_do_redirect(dst, NULL, skb); |
David S. Miller | 3a5ad2e | 2012-07-12 00:08:07 -0700 | [diff] [blame] | 2549 | dst_release(dst); |
| 2550 | } |
| 2551 | EXPORT_SYMBOL_GPL(ip6_redirect); |
| 2552 | |
Maciej Żenczykowski | d456336 | 2018-09-29 23:44:50 -0700 | [diff] [blame] | 2553 | 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] | 2554 | { |
| 2555 | const struct ipv6hdr *iph = ipv6_hdr(skb); |
| 2556 | const struct rd_msg *msg = (struct rd_msg *)icmp6_hdr(skb); |
| 2557 | struct dst_entry *dst; |
Maciej Żenczykowski | 0b26fb1 | 2018-09-29 23:44:49 -0700 | [diff] [blame] | 2558 | struct flowi6 fl6 = { |
| 2559 | .flowi6_iif = LOOPBACK_IFINDEX, |
| 2560 | .flowi6_oif = oif, |
Maciej Żenczykowski | 0b26fb1 | 2018-09-29 23:44:49 -0700 | [diff] [blame] | 2561 | .daddr = msg->dest, |
| 2562 | .saddr = iph->daddr, |
| 2563 | .flowi6_uid = sock_net_uid(net, NULL), |
| 2564 | }; |
Duan Jiong | c92a59e | 2013-08-22 12:07:35 +0800 | [diff] [blame] | 2565 | |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 2566 | dst = ip6_route_redirect(net, &fl6, skb, &iph->saddr); |
Duan Jiong | b55b76b | 2013-09-04 19:44:21 +0800 | [diff] [blame] | 2567 | rt6_do_redirect(dst, NULL, skb); |
Duan Jiong | c92a59e | 2013-08-22 12:07:35 +0800 | [diff] [blame] | 2568 | dst_release(dst); |
| 2569 | } |
| 2570 | |
David S. Miller | 3a5ad2e | 2012-07-12 00:08:07 -0700 | [diff] [blame] | 2571 | void ip6_sk_redirect(struct sk_buff *skb, struct sock *sk) |
| 2572 | { |
Lorenzo Colitti | e2d118a | 2016-11-04 02:23:43 +0900 | [diff] [blame] | 2573 | ip6_redirect(skb, sock_net(sk), sk->sk_bound_dev_if, sk->sk_mark, |
| 2574 | sk->sk_uid); |
David S. Miller | 3a5ad2e | 2012-07-12 00:08:07 -0700 | [diff] [blame] | 2575 | } |
| 2576 | EXPORT_SYMBOL_GPL(ip6_sk_redirect); |
| 2577 | |
David S. Miller | 0dbaee3 | 2010-12-13 12:52:14 -0800 | [diff] [blame] | 2578 | static unsigned int ip6_default_advmss(const struct dst_entry *dst) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2579 | { |
David S. Miller | 0dbaee3 | 2010-12-13 12:52:14 -0800 | [diff] [blame] | 2580 | struct net_device *dev = dst->dev; |
| 2581 | unsigned int mtu = dst_mtu(dst); |
| 2582 | struct net *net = dev_net(dev); |
| 2583 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2584 | mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr); |
| 2585 | |
Daniel Lezcano | 5578689 | 2008-03-04 13:47:47 -0800 | [diff] [blame] | 2586 | if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss) |
| 2587 | mtu = net->ipv6.sysctl.ip6_rt_min_advmss; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2588 | |
| 2589 | /* |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 2590 | * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and |
| 2591 | * corresponding MSS is IPV6_MAXPLEN - tcp_header_size. |
| 2592 | * IPV6_MAXPLEN is also valid and means: "any MSS, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2593 | * rely only on pmtu discovery" |
| 2594 | */ |
| 2595 | if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr)) |
| 2596 | mtu = IPV6_MAXPLEN; |
| 2597 | return mtu; |
| 2598 | } |
| 2599 | |
Steffen Klassert | ebb762f | 2011-11-23 02:12:51 +0000 | [diff] [blame] | 2600 | static unsigned int ip6_mtu(const struct dst_entry *dst) |
David S. Miller | d33e455 | 2010-12-14 13:01:14 -0800 | [diff] [blame] | 2601 | { |
David S. Miller | d33e455 | 2010-12-14 13:01:14 -0800 | [diff] [blame] | 2602 | struct inet6_dev *idev; |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 2603 | unsigned int mtu; |
Steffen Klassert | 618f9bc | 2011-11-23 02:13:31 +0000 | [diff] [blame] | 2604 | |
Martin KaFai Lau | 4b32b5a | 2015-04-28 13:03:06 -0700 | [diff] [blame] | 2605 | mtu = dst_metric_raw(dst, RTAX_MTU); |
| 2606 | if (mtu) |
| 2607 | goto out; |
| 2608 | |
Steffen Klassert | 618f9bc | 2011-11-23 02:13:31 +0000 | [diff] [blame] | 2609 | mtu = IPV6_MIN_MTU; |
David S. Miller | d33e455 | 2010-12-14 13:01:14 -0800 | [diff] [blame] | 2610 | |
| 2611 | rcu_read_lock(); |
| 2612 | idev = __in6_dev_get(dst->dev); |
| 2613 | if (idev) |
| 2614 | mtu = idev->cnf.mtu6; |
| 2615 | rcu_read_unlock(); |
| 2616 | |
Eric Dumazet | 30f78d8 | 2014-04-10 21:23:36 -0700 | [diff] [blame] | 2617 | out: |
Roopa Prabhu | 14972cb | 2016-08-24 20:10:43 -0700 | [diff] [blame] | 2618 | mtu = min_t(unsigned int, mtu, IP6_MAX_MTU); |
| 2619 | |
| 2620 | return mtu - lwtunnel_headroom(dst->lwtstate, mtu); |
David S. Miller | d33e455 | 2010-12-14 13:01:14 -0800 | [diff] [blame] | 2621 | } |
| 2622 | |
David Ahern | 901731b | 2018-05-21 09:08:14 -0700 | [diff] [blame] | 2623 | /* MTU selection: |
| 2624 | * 1. mtu on route is locked - use it |
| 2625 | * 2. mtu from nexthop exception |
| 2626 | * 3. mtu from egress device |
| 2627 | * |
| 2628 | * based on ip6_dst_mtu_forward and exception logic of |
| 2629 | * rt6_find_cached_rt; called with rcu_read_lock |
| 2630 | */ |
| 2631 | u32 ip6_mtu_from_fib6(struct fib6_info *f6i, struct in6_addr *daddr, |
| 2632 | struct in6_addr *saddr) |
| 2633 | { |
| 2634 | struct rt6_exception_bucket *bucket; |
| 2635 | struct rt6_exception *rt6_ex; |
| 2636 | struct in6_addr *src_key; |
| 2637 | struct inet6_dev *idev; |
| 2638 | u32 mtu = 0; |
| 2639 | |
| 2640 | if (unlikely(fib6_metric_locked(f6i, RTAX_MTU))) { |
| 2641 | mtu = f6i->fib6_pmtu; |
| 2642 | if (mtu) |
| 2643 | goto out; |
| 2644 | } |
| 2645 | |
| 2646 | src_key = NULL; |
| 2647 | #ifdef CONFIG_IPV6_SUBTREES |
| 2648 | if (f6i->fib6_src.plen) |
| 2649 | src_key = saddr; |
| 2650 | #endif |
| 2651 | |
| 2652 | bucket = rcu_dereference(f6i->rt6i_exception_bucket); |
| 2653 | rt6_ex = __rt6_find_exception_rcu(&bucket, daddr, src_key); |
| 2654 | if (rt6_ex && !rt6_check_expired(rt6_ex->rt6i)) |
| 2655 | mtu = dst_metric_raw(&rt6_ex->rt6i->dst, RTAX_MTU); |
| 2656 | |
| 2657 | if (likely(!mtu)) { |
| 2658 | struct net_device *dev = fib6_info_nh_dev(f6i); |
| 2659 | |
| 2660 | mtu = IPV6_MIN_MTU; |
| 2661 | idev = __in6_dev_get(dev); |
| 2662 | if (idev && idev->cnf.mtu6 > mtu) |
| 2663 | mtu = idev->cnf.mtu6; |
| 2664 | } |
| 2665 | |
| 2666 | mtu = min_t(unsigned int, mtu, IP6_MAX_MTU); |
| 2667 | out: |
| 2668 | return mtu - lwtunnel_headroom(fib6_info_nh_lwt(f6i), mtu); |
| 2669 | } |
| 2670 | |
YOSHIFUJI Hideaki | 3b00944 | 2007-12-06 16:11:48 -0800 | [diff] [blame] | 2671 | struct dst_entry *icmp6_dst_alloc(struct net_device *dev, |
David S. Miller | 87a1157 | 2011-12-06 17:04:13 -0500 | [diff] [blame] | 2672 | struct flowi6 *fl6) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2673 | { |
David S. Miller | 87a1157 | 2011-12-06 17:04:13 -0500 | [diff] [blame] | 2674 | struct dst_entry *dst; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2675 | struct rt6_info *rt; |
| 2676 | struct inet6_dev *idev = in6_dev_get(dev); |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 2677 | struct net *net = dev_net(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2678 | |
David S. Miller | 3830847 | 2011-12-03 18:02:47 -0500 | [diff] [blame] | 2679 | if (unlikely(!idev)) |
Eric Dumazet | 122bdf6 | 2012-03-14 21:13:11 +0000 | [diff] [blame] | 2680 | return ERR_PTR(-ENODEV); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2681 | |
Martin KaFai Lau | ad70686 | 2015-08-14 11:05:52 -0700 | [diff] [blame] | 2682 | rt = ip6_dst_alloc(net, dev, 0); |
David S. Miller | 3830847 | 2011-12-03 18:02:47 -0500 | [diff] [blame] | 2683 | if (unlikely(!rt)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2684 | in6_dev_put(idev); |
David S. Miller | 87a1157 | 2011-12-06 17:04:13 -0500 | [diff] [blame] | 2685 | dst = ERR_PTR(-ENOMEM); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2686 | goto out; |
| 2687 | } |
| 2688 | |
Yan, Zheng | 8e2ec63 | 2011-09-05 21:34:30 +0000 | [diff] [blame] | 2689 | rt->dst.flags |= DST_HOST; |
Brendan McGrath | 588753f | 2017-12-13 22:14:57 +1100 | [diff] [blame] | 2690 | rt->dst.input = ip6_input; |
Yan, Zheng | 8e2ec63 | 2011-09-05 21:34:30 +0000 | [diff] [blame] | 2691 | rt->dst.output = ip6_output; |
Julian Anastasov | 550bab4 | 2013-10-20 15:43:04 +0300 | [diff] [blame] | 2692 | rt->rt6i_gateway = fl6->daddr; |
David S. Miller | 87a1157 | 2011-12-06 17:04:13 -0500 | [diff] [blame] | 2693 | rt->rt6i_dst.addr = fl6->daddr; |
Yan, Zheng | 8e2ec63 | 2011-09-05 21:34:30 +0000 | [diff] [blame] | 2694 | rt->rt6i_dst.plen = 128; |
| 2695 | rt->rt6i_idev = idev; |
Li RongQing | 14edd87 | 2012-10-24 14:01:18 +0800 | [diff] [blame] | 2696 | dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2697 | |
Ido Schimmel | 4c981e2 | 2018-01-07 12:45:04 +0200 | [diff] [blame] | 2698 | /* Add this dst into uncached_list so that rt6_disable_ip() can |
Wei Wang | 587fea7 | 2017-06-17 10:42:36 -0700 | [diff] [blame] | 2699 | * do proper release of the net_device |
| 2700 | */ |
| 2701 | rt6_uncached_list_add(rt); |
Wei Wang | 81eb844 | 2017-10-06 12:06:11 -0700 | [diff] [blame] | 2702 | atomic_inc(&net->ipv6.rt6_stats->fib_rt_uncache); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2703 | |
David S. Miller | 87a1157 | 2011-12-06 17:04:13 -0500 | [diff] [blame] | 2704 | dst = xfrm_lookup(net, &rt->dst, flowi6_to_flowi(fl6), NULL, 0); |
| 2705 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2706 | out: |
David S. Miller | 87a1157 | 2011-12-06 17:04:13 -0500 | [diff] [blame] | 2707 | return dst; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2708 | } |
| 2709 | |
Daniel Lezcano | 569d364 | 2008-01-18 03:56:57 -0800 | [diff] [blame] | 2710 | static int ip6_dst_gc(struct dst_ops *ops) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2711 | { |
Alexey Dobriyan | 86393e5 | 2009-08-29 01:34:49 +0000 | [diff] [blame] | 2712 | struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops); |
Daniel Lezcano | 7019b78 | 2008-03-04 13:50:14 -0800 | [diff] [blame] | 2713 | int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval; |
| 2714 | int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size; |
| 2715 | int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity; |
| 2716 | int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout; |
| 2717 | unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc; |
Eric Dumazet | fc66f95 | 2010-10-08 06:37:34 +0000 | [diff] [blame] | 2718 | int entries; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2719 | |
Eric Dumazet | fc66f95 | 2010-10-08 06:37:34 +0000 | [diff] [blame] | 2720 | entries = dst_entries_get_fast(ops); |
Michal Kubeček | 49a18d8 | 2013-08-01 10:04:24 +0200 | [diff] [blame] | 2721 | if (time_after(rt_last_gc + rt_min_interval, jiffies) && |
Eric Dumazet | fc66f95 | 2010-10-08 06:37:34 +0000 | [diff] [blame] | 2722 | entries <= rt_max_size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2723 | goto out; |
| 2724 | |
Benjamin Thery | 6891a34 | 2008-03-04 13:49:47 -0800 | [diff] [blame] | 2725 | net->ipv6.ip6_rt_gc_expire++; |
Li RongQing | 1495664 | 2014-05-19 17:30:28 +0800 | [diff] [blame] | 2726 | fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net, true); |
Eric Dumazet | fc66f95 | 2010-10-08 06:37:34 +0000 | [diff] [blame] | 2727 | entries = dst_entries_get_slow(ops); |
| 2728 | if (entries < ops->gc_thresh) |
Daniel Lezcano | 7019b78 | 2008-03-04 13:50:14 -0800 | [diff] [blame] | 2729 | net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2730 | out: |
Daniel Lezcano | 7019b78 | 2008-03-04 13:50:14 -0800 | [diff] [blame] | 2731 | 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] | 2732 | return entries > rt_max_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2733 | } |
| 2734 | |
David Ahern | 8c14586 | 2016-04-24 21:26:04 -0700 | [diff] [blame] | 2735 | static struct rt6_info *ip6_nh_lookup_table(struct net *net, |
| 2736 | struct fib6_config *cfg, |
David Ahern | f4797b3 | 2018-01-25 16:55:08 -0800 | [diff] [blame] | 2737 | const struct in6_addr *gw_addr, |
| 2738 | u32 tbid, int flags) |
David Ahern | 8c14586 | 2016-04-24 21:26:04 -0700 | [diff] [blame] | 2739 | { |
| 2740 | struct flowi6 fl6 = { |
| 2741 | .flowi6_oif = cfg->fc_ifindex, |
| 2742 | .daddr = *gw_addr, |
| 2743 | .saddr = cfg->fc_prefsrc, |
| 2744 | }; |
| 2745 | struct fib6_table *table; |
| 2746 | struct rt6_info *rt; |
David Ahern | 8c14586 | 2016-04-24 21:26:04 -0700 | [diff] [blame] | 2747 | |
David Ahern | f4797b3 | 2018-01-25 16:55:08 -0800 | [diff] [blame] | 2748 | table = fib6_get_table(net, tbid); |
David Ahern | 8c14586 | 2016-04-24 21:26:04 -0700 | [diff] [blame] | 2749 | if (!table) |
| 2750 | return NULL; |
| 2751 | |
| 2752 | if (!ipv6_addr_any(&cfg->fc_prefsrc)) |
| 2753 | flags |= RT6_LOOKUP_F_HAS_SADDR; |
| 2754 | |
David Ahern | f4797b3 | 2018-01-25 16:55:08 -0800 | [diff] [blame] | 2755 | flags |= RT6_LOOKUP_F_IGNORE_LINKSTATE; |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 2756 | rt = ip6_pol_route(net, table, cfg->fc_ifindex, &fl6, NULL, flags); |
David Ahern | 8c14586 | 2016-04-24 21:26:04 -0700 | [diff] [blame] | 2757 | |
| 2758 | /* if table lookup failed, fall back to full lookup */ |
| 2759 | if (rt == net->ipv6.ip6_null_entry) { |
| 2760 | ip6_rt_put(rt); |
| 2761 | rt = NULL; |
| 2762 | } |
| 2763 | |
| 2764 | return rt; |
| 2765 | } |
| 2766 | |
David Ahern | fc1e64e | 2018-01-25 16:55:09 -0800 | [diff] [blame] | 2767 | static int ip6_route_check_nh_onlink(struct net *net, |
| 2768 | struct fib6_config *cfg, |
David Ahern | 9fbb704 | 2018-03-13 08:29:36 -0700 | [diff] [blame] | 2769 | const struct net_device *dev, |
David Ahern | fc1e64e | 2018-01-25 16:55:09 -0800 | [diff] [blame] | 2770 | struct netlink_ext_ack *extack) |
| 2771 | { |
David Ahern | 44750f8 | 2018-02-06 13:17:06 -0800 | [diff] [blame] | 2772 | u32 tbid = l3mdev_fib_table(dev) ? : RT_TABLE_MAIN; |
David Ahern | fc1e64e | 2018-01-25 16:55:09 -0800 | [diff] [blame] | 2773 | const struct in6_addr *gw_addr = &cfg->fc_gateway; |
| 2774 | u32 flags = RTF_LOCAL | RTF_ANYCAST | RTF_REJECT; |
Paolo Abeni | bf1dc8b | 2019-02-21 11:19:42 +0100 | [diff] [blame] | 2775 | struct fib6_info *from; |
David Ahern | fc1e64e | 2018-01-25 16:55:09 -0800 | [diff] [blame] | 2776 | struct rt6_info *grt; |
| 2777 | int err; |
| 2778 | |
| 2779 | err = 0; |
| 2780 | grt = ip6_nh_lookup_table(net, cfg, gw_addr, tbid, 0); |
| 2781 | if (grt) { |
Paolo Abeni | bf1dc8b | 2019-02-21 11:19:42 +0100 | [diff] [blame] | 2782 | rcu_read_lock(); |
| 2783 | from = rcu_dereference(grt->from); |
David Ahern | 58e354c | 2018-02-06 12:14:12 -0800 | [diff] [blame] | 2784 | if (!grt->dst.error && |
David Ahern | 4ed591c | 2018-10-24 13:58:39 -0700 | [diff] [blame] | 2785 | /* ignore match if it is the default route */ |
Paolo Abeni | bf1dc8b | 2019-02-21 11:19:42 +0100 | [diff] [blame] | 2786 | from && !ipv6_addr_any(&from->fib6_dst.addr) && |
David Ahern | 58e354c | 2018-02-06 12:14:12 -0800 | [diff] [blame] | 2787 | (grt->rt6i_flags & flags || dev != grt->dst.dev)) { |
David Ahern | 44750f8 | 2018-02-06 13:17:06 -0800 | [diff] [blame] | 2788 | NL_SET_ERR_MSG(extack, |
| 2789 | "Nexthop has invalid gateway or device mismatch"); |
David Ahern | fc1e64e | 2018-01-25 16:55:09 -0800 | [diff] [blame] | 2790 | err = -EINVAL; |
| 2791 | } |
Paolo Abeni | bf1dc8b | 2019-02-21 11:19:42 +0100 | [diff] [blame] | 2792 | rcu_read_unlock(); |
David Ahern | fc1e64e | 2018-01-25 16:55:09 -0800 | [diff] [blame] | 2793 | |
| 2794 | ip6_rt_put(grt); |
| 2795 | } |
| 2796 | |
| 2797 | return err; |
| 2798 | } |
| 2799 | |
David Ahern | 1edce99 | 2018-01-25 16:55:07 -0800 | [diff] [blame] | 2800 | static int ip6_route_check_nh(struct net *net, |
| 2801 | struct fib6_config *cfg, |
| 2802 | struct net_device **_dev, |
| 2803 | struct inet6_dev **idev) |
| 2804 | { |
| 2805 | const struct in6_addr *gw_addr = &cfg->fc_gateway; |
| 2806 | struct net_device *dev = _dev ? *_dev : NULL; |
| 2807 | struct rt6_info *grt = NULL; |
| 2808 | int err = -EHOSTUNREACH; |
| 2809 | |
| 2810 | if (cfg->fc_table) { |
David Ahern | f4797b3 | 2018-01-25 16:55:08 -0800 | [diff] [blame] | 2811 | int flags = RT6_LOOKUP_F_IFACE; |
| 2812 | |
| 2813 | grt = ip6_nh_lookup_table(net, cfg, gw_addr, |
| 2814 | cfg->fc_table, flags); |
David Ahern | 1edce99 | 2018-01-25 16:55:07 -0800 | [diff] [blame] | 2815 | if (grt) { |
| 2816 | if (grt->rt6i_flags & RTF_GATEWAY || |
| 2817 | (dev && dev != grt->dst.dev)) { |
| 2818 | ip6_rt_put(grt); |
| 2819 | grt = NULL; |
| 2820 | } |
| 2821 | } |
| 2822 | } |
| 2823 | |
| 2824 | if (!grt) |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 2825 | grt = rt6_lookup(net, gw_addr, NULL, cfg->fc_ifindex, NULL, 1); |
David Ahern | 1edce99 | 2018-01-25 16:55:07 -0800 | [diff] [blame] | 2826 | |
| 2827 | if (!grt) |
| 2828 | goto out; |
| 2829 | |
| 2830 | if (dev) { |
| 2831 | if (dev != grt->dst.dev) { |
| 2832 | ip6_rt_put(grt); |
| 2833 | goto out; |
| 2834 | } |
| 2835 | } else { |
| 2836 | *_dev = dev = grt->dst.dev; |
| 2837 | *idev = grt->rt6i_idev; |
| 2838 | dev_hold(dev); |
| 2839 | in6_dev_hold(grt->rt6i_idev); |
| 2840 | } |
| 2841 | |
| 2842 | if (!(grt->rt6i_flags & RTF_GATEWAY)) |
| 2843 | err = 0; |
| 2844 | |
| 2845 | ip6_rt_put(grt); |
| 2846 | |
| 2847 | out: |
| 2848 | return err; |
| 2849 | } |
| 2850 | |
David Ahern | 9fbb704 | 2018-03-13 08:29:36 -0700 | [diff] [blame] | 2851 | static int ip6_validate_gw(struct net *net, struct fib6_config *cfg, |
| 2852 | struct net_device **_dev, struct inet6_dev **idev, |
| 2853 | struct netlink_ext_ack *extack) |
| 2854 | { |
| 2855 | const struct in6_addr *gw_addr = &cfg->fc_gateway; |
| 2856 | int gwa_type = ipv6_addr_type(gw_addr); |
David Ahern | 232378e | 2018-03-13 08:29:37 -0700 | [diff] [blame] | 2857 | bool skip_dev = gwa_type & IPV6_ADDR_LINKLOCAL ? false : true; |
David Ahern | 9fbb704 | 2018-03-13 08:29:36 -0700 | [diff] [blame] | 2858 | const struct net_device *dev = *_dev; |
David Ahern | 232378e | 2018-03-13 08:29:37 -0700 | [diff] [blame] | 2859 | bool need_addr_check = !dev; |
David Ahern | 9fbb704 | 2018-03-13 08:29:36 -0700 | [diff] [blame] | 2860 | int err = -EINVAL; |
| 2861 | |
| 2862 | /* if gw_addr is local we will fail to detect this in case |
| 2863 | * address is still TENTATIVE (DAD in progress). rt6_lookup() |
| 2864 | * will return already-added prefix route via interface that |
| 2865 | * prefix route was assigned to, which might be non-loopback. |
| 2866 | */ |
David Ahern | 232378e | 2018-03-13 08:29:37 -0700 | [diff] [blame] | 2867 | if (dev && |
| 2868 | ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) { |
| 2869 | NL_SET_ERR_MSG(extack, "Gateway can not be a local address"); |
David Ahern | 9fbb704 | 2018-03-13 08:29:36 -0700 | [diff] [blame] | 2870 | goto out; |
| 2871 | } |
| 2872 | |
| 2873 | if (gwa_type != (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_UNICAST)) { |
| 2874 | /* IPv6 strictly inhibits using not link-local |
| 2875 | * addresses as nexthop address. |
| 2876 | * Otherwise, router will not able to send redirects. |
| 2877 | * It is very good, but in some (rare!) circumstances |
| 2878 | * (SIT, PtP, NBMA NOARP links) it is handy to allow |
| 2879 | * some exceptions. --ANK |
| 2880 | * We allow IPv4-mapped nexthops to support RFC4798-type |
| 2881 | * addressing |
| 2882 | */ |
| 2883 | if (!(gwa_type & (IPV6_ADDR_UNICAST | IPV6_ADDR_MAPPED))) { |
| 2884 | NL_SET_ERR_MSG(extack, "Invalid gateway address"); |
| 2885 | goto out; |
| 2886 | } |
| 2887 | |
| 2888 | if (cfg->fc_flags & RTNH_F_ONLINK) |
| 2889 | err = ip6_route_check_nh_onlink(net, cfg, dev, extack); |
| 2890 | else |
| 2891 | err = ip6_route_check_nh(net, cfg, _dev, idev); |
| 2892 | |
| 2893 | if (err) |
| 2894 | goto out; |
| 2895 | } |
| 2896 | |
| 2897 | /* reload in case device was changed */ |
| 2898 | dev = *_dev; |
| 2899 | |
| 2900 | err = -EINVAL; |
| 2901 | if (!dev) { |
| 2902 | NL_SET_ERR_MSG(extack, "Egress device not specified"); |
| 2903 | goto out; |
| 2904 | } else if (dev->flags & IFF_LOOPBACK) { |
| 2905 | NL_SET_ERR_MSG(extack, |
| 2906 | "Egress device can not be loopback device for this route"); |
| 2907 | goto out; |
| 2908 | } |
David Ahern | 232378e | 2018-03-13 08:29:37 -0700 | [diff] [blame] | 2909 | |
| 2910 | /* if we did not check gw_addr above, do so now that the |
| 2911 | * egress device has been resolved. |
| 2912 | */ |
| 2913 | if (need_addr_check && |
| 2914 | ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) { |
| 2915 | NL_SET_ERR_MSG(extack, "Gateway can not be a local address"); |
| 2916 | goto out; |
| 2917 | } |
| 2918 | |
David Ahern | 9fbb704 | 2018-03-13 08:29:36 -0700 | [diff] [blame] | 2919 | err = 0; |
| 2920 | out: |
| 2921 | return err; |
| 2922 | } |
| 2923 | |
David Ahern | 83c44251 | 2019-03-27 20:53:50 -0700 | [diff] [blame] | 2924 | static bool fib6_is_reject(u32 flags, struct net_device *dev, int addr_type) |
| 2925 | { |
| 2926 | if ((flags & RTF_REJECT) || |
| 2927 | (dev && (dev->flags & IFF_LOOPBACK) && |
| 2928 | !(addr_type & IPV6_ADDR_LOOPBACK) && |
| 2929 | !(flags & RTF_LOCAL))) |
| 2930 | return true; |
| 2931 | |
| 2932 | return false; |
| 2933 | } |
| 2934 | |
| 2935 | int fib6_nh_init(struct net *net, struct fib6_nh *fib6_nh, |
| 2936 | struct fib6_config *cfg, gfp_t gfp_flags, |
| 2937 | struct netlink_ext_ack *extack) |
| 2938 | { |
| 2939 | struct net_device *dev = NULL; |
| 2940 | struct inet6_dev *idev = NULL; |
| 2941 | int addr_type; |
| 2942 | int err; |
| 2943 | |
David Ahern | f174173 | 2019-03-27 20:53:57 -0700 | [diff] [blame] | 2944 | fib6_nh->fib_nh_family = AF_INET6; |
| 2945 | |
David Ahern | 83c44251 | 2019-03-27 20:53:50 -0700 | [diff] [blame] | 2946 | err = -ENODEV; |
| 2947 | if (cfg->fc_ifindex) { |
| 2948 | dev = dev_get_by_index(net, cfg->fc_ifindex); |
| 2949 | if (!dev) |
| 2950 | goto out; |
| 2951 | idev = in6_dev_get(dev); |
| 2952 | if (!idev) |
| 2953 | goto out; |
| 2954 | } |
| 2955 | |
| 2956 | if (cfg->fc_flags & RTNH_F_ONLINK) { |
| 2957 | if (!dev) { |
| 2958 | NL_SET_ERR_MSG(extack, |
| 2959 | "Nexthop device required for onlink"); |
| 2960 | goto out; |
| 2961 | } |
| 2962 | |
| 2963 | if (!(dev->flags & IFF_UP)) { |
| 2964 | NL_SET_ERR_MSG(extack, "Nexthop device is not up"); |
| 2965 | err = -ENETDOWN; |
| 2966 | goto out; |
| 2967 | } |
| 2968 | |
David Ahern | ad1601a | 2019-03-27 20:53:56 -0700 | [diff] [blame] | 2969 | fib6_nh->fib_nh_flags |= RTNH_F_ONLINK; |
David Ahern | 83c44251 | 2019-03-27 20:53:50 -0700 | [diff] [blame] | 2970 | } |
| 2971 | |
David Ahern | ad1601a | 2019-03-27 20:53:56 -0700 | [diff] [blame] | 2972 | fib6_nh->fib_nh_weight = 1; |
David Ahern | 83c44251 | 2019-03-27 20:53:50 -0700 | [diff] [blame] | 2973 | |
| 2974 | /* We cannot add true routes via loopback here, |
| 2975 | * they would result in kernel looping; promote them to reject routes |
| 2976 | */ |
| 2977 | addr_type = ipv6_addr_type(&cfg->fc_dst); |
| 2978 | if (fib6_is_reject(cfg->fc_flags, dev, addr_type)) { |
| 2979 | /* hold loopback dev/idev if we haven't done so. */ |
| 2980 | if (dev != net->loopback_dev) { |
| 2981 | if (dev) { |
| 2982 | dev_put(dev); |
| 2983 | in6_dev_put(idev); |
| 2984 | } |
| 2985 | dev = net->loopback_dev; |
| 2986 | dev_hold(dev); |
| 2987 | idev = in6_dev_get(dev); |
| 2988 | if (!idev) { |
| 2989 | err = -ENODEV; |
| 2990 | goto out; |
| 2991 | } |
| 2992 | } |
| 2993 | goto set_dev; |
| 2994 | } |
| 2995 | |
| 2996 | if (cfg->fc_flags & RTF_GATEWAY) { |
| 2997 | err = ip6_validate_gw(net, cfg, &dev, &idev, extack); |
| 2998 | if (err) |
| 2999 | goto out; |
| 3000 | |
David Ahern | ad1601a | 2019-03-27 20:53:56 -0700 | [diff] [blame] | 3001 | fib6_nh->fib_nh_gw6 = cfg->fc_gateway; |
David Ahern | bdf0046 | 2019-04-05 16:30:26 -0700 | [diff] [blame] | 3002 | fib6_nh->fib_nh_gw_family = AF_INET6; |
David Ahern | 83c44251 | 2019-03-27 20:53:50 -0700 | [diff] [blame] | 3003 | } |
| 3004 | |
| 3005 | err = -ENODEV; |
| 3006 | if (!dev) |
| 3007 | goto out; |
| 3008 | |
| 3009 | if (idev->cnf.disable_ipv6) { |
| 3010 | NL_SET_ERR_MSG(extack, "IPv6 is disabled on nexthop device"); |
| 3011 | err = -EACCES; |
| 3012 | goto out; |
| 3013 | } |
| 3014 | |
| 3015 | if (!(dev->flags & IFF_UP) && !cfg->fc_ignore_dev_down) { |
| 3016 | NL_SET_ERR_MSG(extack, "Nexthop device is not up"); |
| 3017 | err = -ENETDOWN; |
| 3018 | goto out; |
| 3019 | } |
| 3020 | |
| 3021 | if (!(cfg->fc_flags & (RTF_LOCAL | RTF_ANYCAST)) && |
| 3022 | !netif_carrier_ok(dev)) |
David Ahern | ad1601a | 2019-03-27 20:53:56 -0700 | [diff] [blame] | 3023 | fib6_nh->fib_nh_flags |= RTNH_F_LINKDOWN; |
David Ahern | 83c44251 | 2019-03-27 20:53:50 -0700 | [diff] [blame] | 3024 | |
David Ahern | 979e276 | 2019-03-27 20:53:58 -0700 | [diff] [blame] | 3025 | err = fib_nh_common_init(&fib6_nh->nh_common, cfg->fc_encap, |
| 3026 | cfg->fc_encap_type, cfg, gfp_flags, extack); |
| 3027 | if (err) |
| 3028 | goto out; |
David Ahern | 83c44251 | 2019-03-27 20:53:50 -0700 | [diff] [blame] | 3029 | set_dev: |
David Ahern | ad1601a | 2019-03-27 20:53:56 -0700 | [diff] [blame] | 3030 | fib6_nh->fib_nh_dev = dev; |
David Ahern | f174173 | 2019-03-27 20:53:57 -0700 | [diff] [blame] | 3031 | fib6_nh->fib_nh_oif = dev->ifindex; |
David Ahern | 83c44251 | 2019-03-27 20:53:50 -0700 | [diff] [blame] | 3032 | err = 0; |
| 3033 | out: |
| 3034 | if (idev) |
| 3035 | in6_dev_put(idev); |
| 3036 | |
| 3037 | if (err) { |
David Ahern | ad1601a | 2019-03-27 20:53:56 -0700 | [diff] [blame] | 3038 | lwtstate_put(fib6_nh->fib_nh_lws); |
| 3039 | fib6_nh->fib_nh_lws = NULL; |
David Ahern | 83c44251 | 2019-03-27 20:53:50 -0700 | [diff] [blame] | 3040 | if (dev) |
| 3041 | dev_put(dev); |
| 3042 | } |
| 3043 | |
| 3044 | return err; |
| 3045 | } |
| 3046 | |
David Ahern | dac7d0f | 2019-03-27 20:53:51 -0700 | [diff] [blame] | 3047 | void fib6_nh_release(struct fib6_nh *fib6_nh) |
| 3048 | { |
David Ahern | 979e276 | 2019-03-27 20:53:58 -0700 | [diff] [blame] | 3049 | fib_nh_common_release(&fib6_nh->nh_common); |
David Ahern | dac7d0f | 2019-03-27 20:53:51 -0700 | [diff] [blame] | 3050 | } |
| 3051 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 3052 | static struct fib6_info *ip6_route_info_create(struct fib6_config *cfg, |
David Ahern | acb54e3 | 2018-04-17 17:33:22 -0700 | [diff] [blame] | 3053 | gfp_t gfp_flags, |
David Ahern | 333c430 | 2017-05-21 10:12:04 -0600 | [diff] [blame] | 3054 | struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3055 | { |
Daniel Lezcano | 5578689 | 2008-03-04 13:47:47 -0800 | [diff] [blame] | 3056 | struct net *net = cfg->fc_nlinfo.nl_net; |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 3057 | struct fib6_info *rt = NULL; |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 3058 | struct fib6_table *table; |
Roopa Prabhu | 8c5b83f | 2015-10-10 08:26:36 -0700 | [diff] [blame] | 3059 | int err = -EINVAL; |
David Ahern | 83c44251 | 2019-03-27 20:53:50 -0700 | [diff] [blame] | 3060 | int addr_type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3061 | |
David Ahern | 557c44b | 2017-04-19 14:19:43 -0700 | [diff] [blame] | 3062 | /* RTF_PCPU is an internal flag; can not be set by userspace */ |
David Ahern | d5d531c | 2017-05-21 10:12:05 -0600 | [diff] [blame] | 3063 | if (cfg->fc_flags & RTF_PCPU) { |
| 3064 | NL_SET_ERR_MSG(extack, "Userspace can not set RTF_PCPU"); |
David Ahern | 557c44b | 2017-04-19 14:19:43 -0700 | [diff] [blame] | 3065 | goto out; |
David Ahern | d5d531c | 2017-05-21 10:12:05 -0600 | [diff] [blame] | 3066 | } |
David Ahern | 557c44b | 2017-04-19 14:19:43 -0700 | [diff] [blame] | 3067 | |
Wei Wang | 2ea2352 | 2017-10-27 17:30:12 -0700 | [diff] [blame] | 3068 | /* RTF_CACHE is an internal flag; can not be set by userspace */ |
| 3069 | if (cfg->fc_flags & RTF_CACHE) { |
| 3070 | NL_SET_ERR_MSG(extack, "Userspace can not set RTF_CACHE"); |
| 3071 | goto out; |
| 3072 | } |
| 3073 | |
David Ahern | e8478e8 | 2018-04-17 17:33:13 -0700 | [diff] [blame] | 3074 | if (cfg->fc_type > RTN_MAX) { |
| 3075 | NL_SET_ERR_MSG(extack, "Invalid route type"); |
| 3076 | goto out; |
| 3077 | } |
| 3078 | |
David Ahern | d5d531c | 2017-05-21 10:12:05 -0600 | [diff] [blame] | 3079 | if (cfg->fc_dst_len > 128) { |
| 3080 | NL_SET_ERR_MSG(extack, "Invalid prefix length"); |
Roopa Prabhu | 8c5b83f | 2015-10-10 08:26:36 -0700 | [diff] [blame] | 3081 | goto out; |
David Ahern | d5d531c | 2017-05-21 10:12:05 -0600 | [diff] [blame] | 3082 | } |
| 3083 | if (cfg->fc_src_len > 128) { |
| 3084 | NL_SET_ERR_MSG(extack, "Invalid source address length"); |
| 3085 | goto out; |
| 3086 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3087 | #ifndef CONFIG_IPV6_SUBTREES |
David Ahern | d5d531c | 2017-05-21 10:12:05 -0600 | [diff] [blame] | 3088 | if (cfg->fc_src_len) { |
| 3089 | NL_SET_ERR_MSG(extack, |
| 3090 | "Specifying source address requires IPV6_SUBTREES to be enabled"); |
Roopa Prabhu | 8c5b83f | 2015-10-10 08:26:36 -0700 | [diff] [blame] | 3091 | goto out; |
David Ahern | d5d531c | 2017-05-21 10:12:05 -0600 | [diff] [blame] | 3092 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3093 | #endif |
David Ahern | fc1e64e | 2018-01-25 16:55:09 -0800 | [diff] [blame] | 3094 | |
Matti Vaittinen | d71314b | 2011-11-14 00:14:49 +0000 | [diff] [blame] | 3095 | err = -ENOBUFS; |
David S. Miller | 3830847 | 2011-12-03 18:02:47 -0500 | [diff] [blame] | 3096 | if (cfg->fc_nlinfo.nlh && |
| 3097 | !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) { |
Matti Vaittinen | d71314b | 2011-11-14 00:14:49 +0000 | [diff] [blame] | 3098 | table = fib6_get_table(net, cfg->fc_table); |
David S. Miller | 3830847 | 2011-12-03 18:02:47 -0500 | [diff] [blame] | 3099 | if (!table) { |
Joe Perches | f321383 | 2012-05-15 14:11:53 +0000 | [diff] [blame] | 3100 | 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] | 3101 | table = fib6_new_table(net, cfg->fc_table); |
| 3102 | } |
| 3103 | } else { |
| 3104 | table = fib6_new_table(net, cfg->fc_table); |
| 3105 | } |
David S. Miller | 3830847 | 2011-12-03 18:02:47 -0500 | [diff] [blame] | 3106 | |
| 3107 | if (!table) |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 3108 | goto out; |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 3109 | |
David Ahern | 93531c6 | 2018-04-17 17:33:25 -0700 | [diff] [blame] | 3110 | err = -ENOMEM; |
| 3111 | rt = fib6_info_alloc(gfp_flags); |
| 3112 | if (!rt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3113 | goto out; |
David Ahern | 93531c6 | 2018-04-17 17:33:25 -0700 | [diff] [blame] | 3114 | |
David Ahern | d7e774f | 2018-11-06 12:51:15 -0800 | [diff] [blame] | 3115 | rt->fib6_metrics = ip_fib_metrics_init(net, cfg->fc_mx, cfg->fc_mx_len, |
| 3116 | extack); |
David Ahern | 767a221 | 2018-10-04 20:07:51 -0700 | [diff] [blame] | 3117 | if (IS_ERR(rt->fib6_metrics)) { |
| 3118 | err = PTR_ERR(rt->fib6_metrics); |
Eric Dumazet | fda21d4 | 2018-10-05 09:17:50 -0700 | [diff] [blame] | 3119 | /* Do not leave garbage there. */ |
| 3120 | rt->fib6_metrics = (struct dst_metrics *)&dst_default_metrics; |
David Ahern | 767a221 | 2018-10-04 20:07:51 -0700 | [diff] [blame] | 3121 | goto out; |
| 3122 | } |
| 3123 | |
David Ahern | 93531c6 | 2018-04-17 17:33:25 -0700 | [diff] [blame] | 3124 | if (cfg->fc_flags & RTF_ADDRCONF) |
| 3125 | rt->dst_nocount = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3126 | |
Gao feng | 1716a96 | 2012-04-06 00:13:10 +0000 | [diff] [blame] | 3127 | if (cfg->fc_flags & RTF_EXPIRES) |
David Ahern | 1489568 | 2018-04-17 17:33:17 -0700 | [diff] [blame] | 3128 | fib6_set_expires(rt, jiffies + |
Gao feng | 1716a96 | 2012-04-06 00:13:10 +0000 | [diff] [blame] | 3129 | clock_t_to_jiffies(cfg->fc_expires)); |
| 3130 | else |
David Ahern | 1489568 | 2018-04-17 17:33:17 -0700 | [diff] [blame] | 3131 | fib6_clean_expires(rt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3132 | |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 3133 | if (cfg->fc_protocol == RTPROT_UNSPEC) |
| 3134 | cfg->fc_protocol = RTPROT_BOOT; |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 3135 | rt->fib6_protocol = cfg->fc_protocol; |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 3136 | |
David Ahern | 83c44251 | 2019-03-27 20:53:50 -0700 | [diff] [blame] | 3137 | rt->fib6_table = table; |
| 3138 | rt->fib6_metric = cfg->fc_metric; |
| 3139 | rt->fib6_type = cfg->fc_type; |
David Ahern | 2b2450c | 2019-03-27 20:53:52 -0700 | [diff] [blame] | 3140 | rt->fib6_flags = cfg->fc_flags & ~RTF_GATEWAY; |
Roopa Prabhu | 19e42e4 | 2015-07-21 10:43:48 +0200 | [diff] [blame] | 3141 | |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 3142 | ipv6_addr_prefix(&rt->fib6_dst.addr, &cfg->fc_dst, cfg->fc_dst_len); |
| 3143 | rt->fib6_dst.plen = cfg->fc_dst_len; |
| 3144 | if (rt->fib6_dst.plen == 128) |
David Ahern | 3b6761d | 2018-04-17 17:33:20 -0700 | [diff] [blame] | 3145 | rt->dst_host = true; |
Michal Kubeček | e5fd387 | 2014-03-27 13:04:08 +0100 | [diff] [blame] | 3146 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3147 | #ifdef CONFIG_IPV6_SUBTREES |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 3148 | ipv6_addr_prefix(&rt->fib6_src.addr, &cfg->fc_src, cfg->fc_src_len); |
| 3149 | rt->fib6_src.plen = cfg->fc_src_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3150 | #endif |
David Ahern | 83c44251 | 2019-03-27 20:53:50 -0700 | [diff] [blame] | 3151 | err = fib6_nh_init(net, &rt->fib6_nh, cfg, gfp_flags, extack); |
| 3152 | if (err) |
| 3153 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3154 | |
| 3155 | /* We cannot add true routes via loopback here, |
David Ahern | 83c44251 | 2019-03-27 20:53:50 -0700 | [diff] [blame] | 3156 | * they would result in kernel looping; promote them to reject routes |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3157 | */ |
David Ahern | 83c44251 | 2019-03-27 20:53:50 -0700 | [diff] [blame] | 3158 | addr_type = ipv6_addr_type(&cfg->fc_dst); |
David Ahern | ad1601a | 2019-03-27 20:53:56 -0700 | [diff] [blame] | 3159 | if (fib6_is_reject(cfg->fc_flags, rt->fib6_nh.fib_nh_dev, addr_type)) |
David Ahern | 83c44251 | 2019-03-27 20:53:50 -0700 | [diff] [blame] | 3160 | rt->fib6_flags = RTF_REJECT | RTF_NONEXTHOP; |
David Ahern | 955ec4c | 2018-01-24 19:45:29 -0800 | [diff] [blame] | 3161 | |
Daniel Walter | c3968a8 | 2011-04-13 21:10:57 +0000 | [diff] [blame] | 3162 | if (!ipv6_addr_any(&cfg->fc_prefsrc)) { |
David Ahern | 83c44251 | 2019-03-27 20:53:50 -0700 | [diff] [blame] | 3163 | struct net_device *dev = fib6_info_nh_dev(rt); |
| 3164 | |
Daniel Walter | c3968a8 | 2011-04-13 21:10:57 +0000 | [diff] [blame] | 3165 | if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) { |
David Ahern | d5d531c | 2017-05-21 10:12:05 -0600 | [diff] [blame] | 3166 | NL_SET_ERR_MSG(extack, "Invalid source address"); |
Daniel Walter | c3968a8 | 2011-04-13 21:10:57 +0000 | [diff] [blame] | 3167 | err = -EINVAL; |
| 3168 | goto out; |
| 3169 | } |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 3170 | rt->fib6_prefsrc.addr = cfg->fc_prefsrc; |
| 3171 | rt->fib6_prefsrc.plen = 128; |
Daniel Walter | c3968a8 | 2011-04-13 21:10:57 +0000 | [diff] [blame] | 3172 | } else |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 3173 | rt->fib6_prefsrc.plen = 0; |
Daniel Walter | c3968a8 | 2011-04-13 21:10:57 +0000 | [diff] [blame] | 3174 | |
Roopa Prabhu | 8c5b83f | 2015-10-10 08:26:36 -0700 | [diff] [blame] | 3175 | return rt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3176 | out: |
David Ahern | 93531c6 | 2018-04-17 17:33:25 -0700 | [diff] [blame] | 3177 | fib6_info_release(rt); |
Roopa Prabhu | 8c5b83f | 2015-10-10 08:26:36 -0700 | [diff] [blame] | 3178 | return ERR_PTR(err); |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 3179 | } |
| 3180 | |
David Ahern | acb54e3 | 2018-04-17 17:33:22 -0700 | [diff] [blame] | 3181 | int ip6_route_add(struct fib6_config *cfg, gfp_t gfp_flags, |
David Ahern | 333c430 | 2017-05-21 10:12:04 -0600 | [diff] [blame] | 3182 | struct netlink_ext_ack *extack) |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 3183 | { |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 3184 | struct fib6_info *rt; |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 3185 | int err; |
| 3186 | |
David Ahern | acb54e3 | 2018-04-17 17:33:22 -0700 | [diff] [blame] | 3187 | rt = ip6_route_info_create(cfg, gfp_flags, extack); |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 3188 | if (IS_ERR(rt)) |
| 3189 | return PTR_ERR(rt); |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 3190 | |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 3191 | err = __ip6_ins_rt(rt, &cfg->fc_nlinfo, extack); |
David Ahern | 93531c6 | 2018-04-17 17:33:25 -0700 | [diff] [blame] | 3192 | fib6_info_release(rt); |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 3193 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3194 | return err; |
| 3195 | } |
| 3196 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 3197 | 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] | 3198 | { |
David Ahern | afb1d4b5 | 2018-04-17 17:33:11 -0700 | [diff] [blame] | 3199 | struct net *net = info->nl_net; |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 3200 | struct fib6_table *table; |
David Ahern | afb1d4b5 | 2018-04-17 17:33:11 -0700 | [diff] [blame] | 3201 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3202 | |
David Ahern | 421842e | 2018-04-17 17:33:18 -0700 | [diff] [blame] | 3203 | if (rt == net->ipv6.fib6_null_entry) { |
Gao feng | 6825a26 | 2012-09-19 19:25:34 +0000 | [diff] [blame] | 3204 | err = -ENOENT; |
| 3205 | goto out; |
| 3206 | } |
Patrick McHardy | 6c813a7 | 2006-08-06 22:22:47 -0700 | [diff] [blame] | 3207 | |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 3208 | table = rt->fib6_table; |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 3209 | spin_lock_bh(&table->tb6_lock); |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 3210 | err = fib6_del(rt, info); |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 3211 | spin_unlock_bh(&table->tb6_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3212 | |
Gao feng | 6825a26 | 2012-09-19 19:25:34 +0000 | [diff] [blame] | 3213 | out: |
David Ahern | 93531c6 | 2018-04-17 17:33:25 -0700 | [diff] [blame] | 3214 | fib6_info_release(rt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3215 | return err; |
| 3216 | } |
| 3217 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 3218 | int ip6_del_rt(struct net *net, struct fib6_info *rt) |
Thomas Graf | e0a1ad73 | 2006-08-22 00:00:21 -0700 | [diff] [blame] | 3219 | { |
David Ahern | afb1d4b5 | 2018-04-17 17:33:11 -0700 | [diff] [blame] | 3220 | struct nl_info info = { .nl_net = net }; |
| 3221 | |
Denis V. Lunev | 528c4ce | 2007-12-13 09:45:12 -0800 | [diff] [blame] | 3222 | return __ip6_del_rt(rt, &info); |
Thomas Graf | e0a1ad73 | 2006-08-22 00:00:21 -0700 | [diff] [blame] | 3223 | } |
| 3224 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 3225 | 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] | 3226 | { |
| 3227 | struct nl_info *info = &cfg->fc_nlinfo; |
WANG Cong | e333003 | 2017-02-27 16:07:43 -0800 | [diff] [blame] | 3228 | struct net *net = info->nl_net; |
David Ahern | 16a16cd | 2017-02-02 12:37:11 -0800 | [diff] [blame] | 3229 | struct sk_buff *skb = NULL; |
David Ahern | 0ae8133 | 2017-02-02 12:37:08 -0800 | [diff] [blame] | 3230 | struct fib6_table *table; |
WANG Cong | e333003 | 2017-02-27 16:07:43 -0800 | [diff] [blame] | 3231 | int err = -ENOENT; |
David Ahern | 0ae8133 | 2017-02-02 12:37:08 -0800 | [diff] [blame] | 3232 | |
David Ahern | 421842e | 2018-04-17 17:33:18 -0700 | [diff] [blame] | 3233 | if (rt == net->ipv6.fib6_null_entry) |
WANG Cong | e333003 | 2017-02-27 16:07:43 -0800 | [diff] [blame] | 3234 | goto out_put; |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 3235 | table = rt->fib6_table; |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 3236 | spin_lock_bh(&table->tb6_lock); |
David Ahern | 0ae8133 | 2017-02-02 12:37:08 -0800 | [diff] [blame] | 3237 | |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 3238 | if (rt->fib6_nsiblings && cfg->fc_delete_all_nh) { |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 3239 | struct fib6_info *sibling, *next_sibling; |
David Ahern | 0ae8133 | 2017-02-02 12:37:08 -0800 | [diff] [blame] | 3240 | |
David Ahern | 16a16cd | 2017-02-02 12:37:11 -0800 | [diff] [blame] | 3241 | /* prefer to send a single notification with all hops */ |
| 3242 | skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any()); |
| 3243 | if (skb) { |
| 3244 | u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0; |
| 3245 | |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 3246 | if (rt6_fill_node(net, skb, rt, NULL, |
David Ahern | 16a16cd | 2017-02-02 12:37:11 -0800 | [diff] [blame] | 3247 | NULL, NULL, 0, RTM_DELROUTE, |
| 3248 | info->portid, seq, 0) < 0) { |
| 3249 | kfree_skb(skb); |
| 3250 | skb = NULL; |
| 3251 | } else |
| 3252 | info->skip_notify = 1; |
| 3253 | } |
| 3254 | |
David Ahern | 0ae8133 | 2017-02-02 12:37:08 -0800 | [diff] [blame] | 3255 | list_for_each_entry_safe(sibling, next_sibling, |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 3256 | &rt->fib6_siblings, |
| 3257 | fib6_siblings) { |
David Ahern | 0ae8133 | 2017-02-02 12:37:08 -0800 | [diff] [blame] | 3258 | err = fib6_del(sibling, info); |
| 3259 | if (err) |
WANG Cong | e333003 | 2017-02-27 16:07:43 -0800 | [diff] [blame] | 3260 | goto out_unlock; |
David Ahern | 0ae8133 | 2017-02-02 12:37:08 -0800 | [diff] [blame] | 3261 | } |
| 3262 | } |
| 3263 | |
| 3264 | err = fib6_del(rt, info); |
WANG Cong | e333003 | 2017-02-27 16:07:43 -0800 | [diff] [blame] | 3265 | out_unlock: |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 3266 | spin_unlock_bh(&table->tb6_lock); |
WANG Cong | e333003 | 2017-02-27 16:07:43 -0800 | [diff] [blame] | 3267 | out_put: |
David Ahern | 93531c6 | 2018-04-17 17:33:25 -0700 | [diff] [blame] | 3268 | fib6_info_release(rt); |
David Ahern | 16a16cd | 2017-02-02 12:37:11 -0800 | [diff] [blame] | 3269 | |
| 3270 | if (skb) { |
WANG Cong | e333003 | 2017-02-27 16:07:43 -0800 | [diff] [blame] | 3271 | rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE, |
David Ahern | 16a16cd | 2017-02-02 12:37:11 -0800 | [diff] [blame] | 3272 | info->nlh, gfp_any()); |
| 3273 | } |
David Ahern | 0ae8133 | 2017-02-02 12:37:08 -0800 | [diff] [blame] | 3274 | return err; |
| 3275 | } |
| 3276 | |
David Ahern | 23fb93a | 2018-04-17 17:33:23 -0700 | [diff] [blame] | 3277 | static int ip6_del_cached_rt(struct rt6_info *rt, struct fib6_config *cfg) |
| 3278 | { |
| 3279 | int rc = -ESRCH; |
| 3280 | |
| 3281 | if (cfg->fc_ifindex && rt->dst.dev->ifindex != cfg->fc_ifindex) |
| 3282 | goto out; |
| 3283 | |
| 3284 | if (cfg->fc_flags & RTF_GATEWAY && |
| 3285 | !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway)) |
| 3286 | goto out; |
Xin Long | 761f602 | 2018-11-14 00:48:28 +0800 | [diff] [blame] | 3287 | |
| 3288 | rc = rt6_remove_exception_rt(rt); |
David Ahern | 23fb93a | 2018-04-17 17:33:23 -0700 | [diff] [blame] | 3289 | out: |
| 3290 | return rc; |
| 3291 | } |
| 3292 | |
David Ahern | 333c430 | 2017-05-21 10:12:04 -0600 | [diff] [blame] | 3293 | static int ip6_route_del(struct fib6_config *cfg, |
| 3294 | struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3295 | { |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 3296 | struct rt6_info *rt_cache; |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 3297 | struct fib6_table *table; |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 3298 | struct fib6_info *rt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3299 | struct fib6_node *fn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3300 | int err = -ESRCH; |
| 3301 | |
Daniel Lezcano | 5578689 | 2008-03-04 13:47:47 -0800 | [diff] [blame] | 3302 | table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table); |
David Ahern | d5d531c | 2017-05-21 10:12:05 -0600 | [diff] [blame] | 3303 | if (!table) { |
| 3304 | NL_SET_ERR_MSG(extack, "FIB table does not exist"); |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 3305 | return err; |
David Ahern | d5d531c | 2017-05-21 10:12:05 -0600 | [diff] [blame] | 3306 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3307 | |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 3308 | rcu_read_lock(); |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 3309 | |
| 3310 | fn = fib6_locate(&table->tb6_root, |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 3311 | &cfg->fc_dst, cfg->fc_dst_len, |
Wei Wang | 38fbeee | 2017-10-06 12:06:02 -0700 | [diff] [blame] | 3312 | &cfg->fc_src, cfg->fc_src_len, |
Wei Wang | 2b760fc | 2017-10-06 12:06:03 -0700 | [diff] [blame] | 3313 | !(cfg->fc_flags & RTF_CACHE)); |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 3314 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3315 | if (fn) { |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 3316 | for_each_fib6_node_rt_rcu(fn) { |
David Ahern | ad1601a | 2019-03-27 20:53:56 -0700 | [diff] [blame] | 3317 | struct fib6_nh *nh; |
| 3318 | |
Wei Wang | 2b760fc | 2017-10-06 12:06:03 -0700 | [diff] [blame] | 3319 | if (cfg->fc_flags & RTF_CACHE) { |
David Ahern | 7e4b512 | 2019-04-16 14:36:00 -0700 | [diff] [blame] | 3320 | struct fib6_result res = { |
| 3321 | .f6i = rt, |
| 3322 | }; |
David Ahern | 23fb93a | 2018-04-17 17:33:23 -0700 | [diff] [blame] | 3323 | int rc; |
| 3324 | |
David Ahern | 7e4b512 | 2019-04-16 14:36:00 -0700 | [diff] [blame] | 3325 | rt_cache = rt6_find_cached_rt(&res, |
| 3326 | &cfg->fc_dst, |
Wei Wang | 2b760fc | 2017-10-06 12:06:03 -0700 | [diff] [blame] | 3327 | &cfg->fc_src); |
David Ahern | 23fb93a | 2018-04-17 17:33:23 -0700 | [diff] [blame] | 3328 | if (rt_cache) { |
| 3329 | rc = ip6_del_cached_rt(rt_cache, cfg); |
Eric Dumazet | 9e57501 | 2018-05-09 10:05:46 -0700 | [diff] [blame] | 3330 | if (rc != -ESRCH) { |
| 3331 | rcu_read_unlock(); |
David Ahern | 23fb93a | 2018-04-17 17:33:23 -0700 | [diff] [blame] | 3332 | return rc; |
Eric Dumazet | 9e57501 | 2018-05-09 10:05:46 -0700 | [diff] [blame] | 3333 | } |
David Ahern | 23fb93a | 2018-04-17 17:33:23 -0700 | [diff] [blame] | 3334 | } |
| 3335 | continue; |
Wei Wang | 2b760fc | 2017-10-06 12:06:03 -0700 | [diff] [blame] | 3336 | } |
David Ahern | ad1601a | 2019-03-27 20:53:56 -0700 | [diff] [blame] | 3337 | |
| 3338 | nh = &rt->fib6_nh; |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 3339 | if (cfg->fc_ifindex && |
David Ahern | ad1601a | 2019-03-27 20:53:56 -0700 | [diff] [blame] | 3340 | (!nh->fib_nh_dev || |
| 3341 | nh->fib_nh_dev->ifindex != cfg->fc_ifindex)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3342 | continue; |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 3343 | if (cfg->fc_flags & RTF_GATEWAY && |
David Ahern | ad1601a | 2019-03-27 20:53:56 -0700 | [diff] [blame] | 3344 | !ipv6_addr_equal(&cfg->fc_gateway, &nh->fib_nh_gw6)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3345 | continue; |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 3346 | if (cfg->fc_metric && cfg->fc_metric != rt->fib6_metric) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3347 | continue; |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 3348 | if (cfg->fc_protocol && cfg->fc_protocol != rt->fib6_protocol) |
Mantas M | c2ed188 | 2016-12-16 10:30:59 +0200 | [diff] [blame] | 3349 | continue; |
Wei Wang | e873e4b | 2018-07-21 20:56:32 -0700 | [diff] [blame] | 3350 | if (!fib6_info_hold_safe(rt)) |
| 3351 | continue; |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 3352 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3353 | |
David Ahern | 0ae8133 | 2017-02-02 12:37:08 -0800 | [diff] [blame] | 3354 | /* if gateway was specified only delete the one hop */ |
| 3355 | if (cfg->fc_flags & RTF_GATEWAY) |
| 3356 | return __ip6_del_rt(rt, &cfg->fc_nlinfo); |
| 3357 | |
| 3358 | return __ip6_del_rt_siblings(rt, cfg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3359 | } |
| 3360 | } |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 3361 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3362 | |
| 3363 | return err; |
| 3364 | } |
| 3365 | |
David S. Miller | 6700c27 | 2012-07-17 03:29:28 -0700 | [diff] [blame] | 3366 | 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] | 3367 | { |
YOSHIFUJI Hideaki | a627945 | 2006-08-23 17:18:26 -0700 | [diff] [blame] | 3368 | struct netevent_redirect netevent; |
David S. Miller | e8599ff | 2012-07-11 23:43:53 -0700 | [diff] [blame] | 3369 | struct rt6_info *rt, *nrt = NULL; |
David Ahern | 85bd05d | 2019-04-16 14:36:01 -0700 | [diff] [blame^] | 3370 | struct fib6_result res = {}; |
David S. Miller | e8599ff | 2012-07-11 23:43:53 -0700 | [diff] [blame] | 3371 | struct ndisc_options ndopts; |
| 3372 | struct inet6_dev *in6_dev; |
| 3373 | struct neighbour *neigh; |
YOSHIFUJI Hideaki / 吉藤英明 | 71bcdba | 2013-01-05 16:34:51 +0000 | [diff] [blame] | 3374 | struct rd_msg *msg; |
David S. Miller | 6e157b6 | 2012-07-12 00:05:02 -0700 | [diff] [blame] | 3375 | int optlen, on_link; |
| 3376 | u8 *lladdr; |
David S. Miller | e8599ff | 2012-07-11 23:43:53 -0700 | [diff] [blame] | 3377 | |
Simon Horman | 29a3cad | 2013-05-28 20:34:26 +0000 | [diff] [blame] | 3378 | optlen = skb_tail_pointer(skb) - skb_transport_header(skb); |
YOSHIFUJI Hideaki / 吉藤英明 | 71bcdba | 2013-01-05 16:34:51 +0000 | [diff] [blame] | 3379 | optlen -= sizeof(*msg); |
David S. Miller | e8599ff | 2012-07-11 23:43:53 -0700 | [diff] [blame] | 3380 | |
| 3381 | if (optlen < 0) { |
David S. Miller | 6e157b6 | 2012-07-12 00:05:02 -0700 | [diff] [blame] | 3382 | net_dbg_ratelimited("rt6_do_redirect: packet too short\n"); |
David S. Miller | e8599ff | 2012-07-11 23:43:53 -0700 | [diff] [blame] | 3383 | return; |
| 3384 | } |
| 3385 | |
YOSHIFUJI Hideaki / 吉藤英明 | 71bcdba | 2013-01-05 16:34:51 +0000 | [diff] [blame] | 3386 | msg = (struct rd_msg *)icmp6_hdr(skb); |
David S. Miller | e8599ff | 2012-07-11 23:43:53 -0700 | [diff] [blame] | 3387 | |
YOSHIFUJI Hideaki / 吉藤英明 | 71bcdba | 2013-01-05 16:34:51 +0000 | [diff] [blame] | 3388 | if (ipv6_addr_is_multicast(&msg->dest)) { |
David S. Miller | 6e157b6 | 2012-07-12 00:05:02 -0700 | [diff] [blame] | 3389 | net_dbg_ratelimited("rt6_do_redirect: destination address is multicast\n"); |
David S. Miller | e8599ff | 2012-07-11 23:43:53 -0700 | [diff] [blame] | 3390 | return; |
| 3391 | } |
| 3392 | |
David S. Miller | 6e157b6 | 2012-07-12 00:05:02 -0700 | [diff] [blame] | 3393 | on_link = 0; |
YOSHIFUJI Hideaki / 吉藤英明 | 71bcdba | 2013-01-05 16:34:51 +0000 | [diff] [blame] | 3394 | if (ipv6_addr_equal(&msg->dest, &msg->target)) { |
David S. Miller | e8599ff | 2012-07-11 23:43:53 -0700 | [diff] [blame] | 3395 | on_link = 1; |
YOSHIFUJI Hideaki / 吉藤英明 | 71bcdba | 2013-01-05 16:34:51 +0000 | [diff] [blame] | 3396 | } else if (ipv6_addr_type(&msg->target) != |
David S. Miller | e8599ff | 2012-07-11 23:43:53 -0700 | [diff] [blame] | 3397 | (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) { |
David S. Miller | 6e157b6 | 2012-07-12 00:05:02 -0700 | [diff] [blame] | 3398 | 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] | 3399 | return; |
| 3400 | } |
| 3401 | |
| 3402 | in6_dev = __in6_dev_get(skb->dev); |
| 3403 | if (!in6_dev) |
| 3404 | return; |
| 3405 | if (in6_dev->cnf.forwarding || !in6_dev->cnf.accept_redirects) |
| 3406 | return; |
| 3407 | |
| 3408 | /* RFC2461 8.1: |
| 3409 | * The IP source address of the Redirect MUST be the same as the current |
| 3410 | * first-hop router for the specified ICMP Destination Address. |
| 3411 | */ |
| 3412 | |
Alexander Aring | f997c55 | 2016-06-15 21:20:23 +0200 | [diff] [blame] | 3413 | if (!ndisc_parse_options(skb->dev, msg->opt, optlen, &ndopts)) { |
David S. Miller | e8599ff | 2012-07-11 23:43:53 -0700 | [diff] [blame] | 3414 | net_dbg_ratelimited("rt6_redirect: invalid ND options\n"); |
| 3415 | return; |
| 3416 | } |
David S. Miller | 6e157b6 | 2012-07-12 00:05:02 -0700 | [diff] [blame] | 3417 | |
| 3418 | lladdr = NULL; |
David S. Miller | e8599ff | 2012-07-11 23:43:53 -0700 | [diff] [blame] | 3419 | if (ndopts.nd_opts_tgt_lladdr) { |
| 3420 | lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr, |
| 3421 | skb->dev); |
| 3422 | if (!lladdr) { |
| 3423 | net_dbg_ratelimited("rt6_redirect: invalid link-layer address length\n"); |
| 3424 | return; |
| 3425 | } |
| 3426 | } |
| 3427 | |
David S. Miller | 6e157b6 | 2012-07-12 00:05:02 -0700 | [diff] [blame] | 3428 | rt = (struct rt6_info *) dst; |
Matthias Schiffer | ec13ad1 | 2015-11-02 01:24:38 +0100 | [diff] [blame] | 3429 | if (rt->rt6i_flags & RTF_REJECT) { |
David S. Miller | 6e157b6 | 2012-07-12 00:05:02 -0700 | [diff] [blame] | 3430 | net_dbg_ratelimited("rt6_redirect: source isn't a valid nexthop for redirect target\n"); |
| 3431 | return; |
| 3432 | } |
| 3433 | |
| 3434 | /* Redirect received -> path was valid. |
| 3435 | * Look, redirects are sent only in response to data packets, |
| 3436 | * so that this nexthop apparently is reachable. --ANK |
| 3437 | */ |
Julian Anastasov | 0dec879 | 2017-02-06 23:14:16 +0200 | [diff] [blame] | 3438 | dst_confirm_neigh(&rt->dst, &ipv6_hdr(skb)->saddr); |
David S. Miller | 6e157b6 | 2012-07-12 00:05:02 -0700 | [diff] [blame] | 3439 | |
YOSHIFUJI Hideaki / 吉藤英明 | 71bcdba | 2013-01-05 16:34:51 +0000 | [diff] [blame] | 3440 | neigh = __neigh_lookup(&nd_tbl, &msg->target, skb->dev, 1); |
David S. Miller | e8599ff | 2012-07-11 23:43:53 -0700 | [diff] [blame] | 3441 | if (!neigh) |
| 3442 | return; |
| 3443 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3444 | /* |
| 3445 | * We have finally decided to accept it. |
| 3446 | */ |
| 3447 | |
Alexander Aring | f997c55 | 2016-06-15 21:20:23 +0200 | [diff] [blame] | 3448 | ndisc_update(skb->dev, neigh, lladdr, NUD_STALE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3449 | NEIGH_UPDATE_F_WEAK_OVERRIDE| |
| 3450 | NEIGH_UPDATE_F_OVERRIDE| |
| 3451 | (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER| |
Alexander Aring | f997c55 | 2016-06-15 21:20:23 +0200 | [diff] [blame] | 3452 | NEIGH_UPDATE_F_ISROUTER)), |
| 3453 | NDISC_REDIRECT, &ndopts); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3454 | |
David Ahern | 4d85cd0 | 2018-04-20 15:37:59 -0700 | [diff] [blame] | 3455 | rcu_read_lock(); |
David Ahern | 85bd05d | 2019-04-16 14:36:01 -0700 | [diff] [blame^] | 3456 | res.f6i = rcu_dereference(rt->from); |
Wei Wang | e873e4b | 2018-07-21 20:56:32 -0700 | [diff] [blame] | 3457 | /* This fib6_info_hold() is safe here because we hold reference to rt |
| 3458 | * and rt already holds reference to fib6_info. |
| 3459 | */ |
David Ahern | 85bd05d | 2019-04-16 14:36:01 -0700 | [diff] [blame^] | 3460 | fib6_info_hold(res.f6i); |
David Ahern | 4d85cd0 | 2018-04-20 15:37:59 -0700 | [diff] [blame] | 3461 | rcu_read_unlock(); |
David Ahern | 8a14e46 | 2018-04-23 11:32:07 -0700 | [diff] [blame] | 3462 | |
David Ahern | 85bd05d | 2019-04-16 14:36:01 -0700 | [diff] [blame^] | 3463 | res.nh = &res.f6i->fib6_nh; |
| 3464 | nrt = ip6_rt_cache_alloc(&res, &msg->dest, NULL); |
David S. Miller | 3830847 | 2011-12-03 18:02:47 -0500 | [diff] [blame] | 3465 | if (!nrt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3466 | goto out; |
| 3467 | |
| 3468 | nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE; |
| 3469 | if (on_link) |
| 3470 | nrt->rt6i_flags &= ~RTF_GATEWAY; |
| 3471 | |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 3472 | nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3473 | |
Wei Wang | 2b760fc | 2017-10-06 12:06:03 -0700 | [diff] [blame] | 3474 | /* No need to remove rt from the exception table if rt is |
| 3475 | * a cached route because rt6_insert_exception() will |
| 3476 | * takes care of it |
| 3477 | */ |
David Ahern | 85bd05d | 2019-04-16 14:36:01 -0700 | [diff] [blame^] | 3478 | if (rt6_insert_exception(nrt, res.f6i)) { |
Wei Wang | 2b760fc | 2017-10-06 12:06:03 -0700 | [diff] [blame] | 3479 | dst_release_immediate(&nrt->dst); |
| 3480 | goto out; |
| 3481 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3482 | |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 3483 | netevent.old = &rt->dst; |
| 3484 | netevent.new = &nrt->dst; |
YOSHIFUJI Hideaki / 吉藤英明 | 71bcdba | 2013-01-05 16:34:51 +0000 | [diff] [blame] | 3485 | netevent.daddr = &msg->dest; |
YOSHIFUJI Hideaki / 吉藤英明 | 6059283 | 2013-01-14 09:28:27 +0000 | [diff] [blame] | 3486 | netevent.neigh = neigh; |
Tom Tucker | 8d71740 | 2006-07-30 20:43:36 -0700 | [diff] [blame] | 3487 | call_netevent_notifiers(NETEVENT_REDIRECT, &netevent); |
| 3488 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3489 | out: |
David Ahern | 85bd05d | 2019-04-16 14:36:01 -0700 | [diff] [blame^] | 3490 | fib6_info_release(res.f6i); |
David S. Miller | e8599ff | 2012-07-11 23:43:53 -0700 | [diff] [blame] | 3491 | neigh_release(neigh); |
David S. Miller | 6e157b6 | 2012-07-12 00:05:02 -0700 | [diff] [blame] | 3492 | } |
| 3493 | |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 3494 | #ifdef CONFIG_IPV6_ROUTE_INFO |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 3495 | static struct fib6_info *rt6_get_route_info(struct net *net, |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 3496 | const struct in6_addr *prefix, int prefixlen, |
David Ahern | 830218c | 2016-10-24 10:52:35 -0700 | [diff] [blame] | 3497 | const struct in6_addr *gwaddr, |
| 3498 | struct net_device *dev) |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 3499 | { |
David Ahern | 830218c | 2016-10-24 10:52:35 -0700 | [diff] [blame] | 3500 | u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO; |
| 3501 | int ifindex = dev->ifindex; |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 3502 | struct fib6_node *fn; |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 3503 | struct fib6_info *rt = NULL; |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 3504 | struct fib6_table *table; |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 3505 | |
David Ahern | 830218c | 2016-10-24 10:52:35 -0700 | [diff] [blame] | 3506 | table = fib6_get_table(net, tb_id); |
David S. Miller | 3830847 | 2011-12-03 18:02:47 -0500 | [diff] [blame] | 3507 | if (!table) |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 3508 | return NULL; |
| 3509 | |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 3510 | rcu_read_lock(); |
Wei Wang | 38fbeee | 2017-10-06 12:06:02 -0700 | [diff] [blame] | 3511 | fn = fib6_locate(&table->tb6_root, prefix, prefixlen, NULL, 0, true); |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 3512 | if (!fn) |
| 3513 | goto out; |
| 3514 | |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 3515 | for_each_fib6_node_rt_rcu(fn) { |
David Ahern | ad1601a | 2019-03-27 20:53:56 -0700 | [diff] [blame] | 3516 | if (rt->fib6_nh.fib_nh_dev->ifindex != ifindex) |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 3517 | continue; |
David Ahern | 2b2450c | 2019-03-27 20:53:52 -0700 | [diff] [blame] | 3518 | if (!(rt->fib6_flags & RTF_ROUTEINFO) || |
David Ahern | bdf0046 | 2019-04-05 16:30:26 -0700 | [diff] [blame] | 3519 | !rt->fib6_nh.fib_nh_gw_family) |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 3520 | continue; |
David Ahern | ad1601a | 2019-03-27 20:53:56 -0700 | [diff] [blame] | 3521 | if (!ipv6_addr_equal(&rt->fib6_nh.fib_nh_gw6, gwaddr)) |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 3522 | continue; |
Wei Wang | e873e4b | 2018-07-21 20:56:32 -0700 | [diff] [blame] | 3523 | if (!fib6_info_hold_safe(rt)) |
| 3524 | continue; |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 3525 | break; |
| 3526 | } |
| 3527 | out: |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 3528 | rcu_read_unlock(); |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 3529 | return rt; |
| 3530 | } |
| 3531 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 3532 | static struct fib6_info *rt6_add_route_info(struct net *net, |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 3533 | const struct in6_addr *prefix, int prefixlen, |
David Ahern | 830218c | 2016-10-24 10:52:35 -0700 | [diff] [blame] | 3534 | const struct in6_addr *gwaddr, |
| 3535 | struct net_device *dev, |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 3536 | unsigned int pref) |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 3537 | { |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 3538 | struct fib6_config cfg = { |
Rami Rosen | 238fc7e | 2008-02-09 23:43:11 -0800 | [diff] [blame] | 3539 | .fc_metric = IP6_RT_PRIO_USER, |
David Ahern | 830218c | 2016-10-24 10:52:35 -0700 | [diff] [blame] | 3540 | .fc_ifindex = dev->ifindex, |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 3541 | .fc_dst_len = prefixlen, |
| 3542 | .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO | |
| 3543 | RTF_UP | RTF_PREF(pref), |
Xin Long | b91d532 | 2017-08-03 14:13:46 +0800 | [diff] [blame] | 3544 | .fc_protocol = RTPROT_RA, |
David Ahern | e8478e8 | 2018-04-17 17:33:13 -0700 | [diff] [blame] | 3545 | .fc_type = RTN_UNICAST, |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 3546 | .fc_nlinfo.portid = 0, |
Daniel Lezcano | efa2cea | 2008-03-04 13:46:48 -0800 | [diff] [blame] | 3547 | .fc_nlinfo.nlh = NULL, |
| 3548 | .fc_nlinfo.nl_net = net, |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 3549 | }; |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 3550 | |
David Ahern | 830218c | 2016-10-24 10:52:35 -0700 | [diff] [blame] | 3551 | cfg.fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO, |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 3552 | cfg.fc_dst = *prefix; |
| 3553 | cfg.fc_gateway = *gwaddr; |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 3554 | |
YOSHIFUJI Hideaki | e317da9 | 2006-03-20 17:06:42 -0800 | [diff] [blame] | 3555 | /* We should treat it as a default route if prefix length is 0. */ |
| 3556 | if (!prefixlen) |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 3557 | cfg.fc_flags |= RTF_DEFAULT; |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 3558 | |
David Ahern | acb54e3 | 2018-04-17 17:33:22 -0700 | [diff] [blame] | 3559 | ip6_route_add(&cfg, GFP_ATOMIC, NULL); |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 3560 | |
David Ahern | 830218c | 2016-10-24 10:52:35 -0700 | [diff] [blame] | 3561 | return rt6_get_route_info(net, prefix, prefixlen, gwaddr, dev); |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 3562 | } |
| 3563 | #endif |
| 3564 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 3565 | struct fib6_info *rt6_get_dflt_router(struct net *net, |
David Ahern | afb1d4b5 | 2018-04-17 17:33:11 -0700 | [diff] [blame] | 3566 | const struct in6_addr *addr, |
| 3567 | struct net_device *dev) |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 3568 | { |
David Ahern | 830218c | 2016-10-24 10:52:35 -0700 | [diff] [blame] | 3569 | u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT; |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 3570 | struct fib6_info *rt; |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 3571 | struct fib6_table *table; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3572 | |
David Ahern | afb1d4b5 | 2018-04-17 17:33:11 -0700 | [diff] [blame] | 3573 | table = fib6_get_table(net, tb_id); |
David S. Miller | 3830847 | 2011-12-03 18:02:47 -0500 | [diff] [blame] | 3574 | if (!table) |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 3575 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3576 | |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 3577 | rcu_read_lock(); |
| 3578 | for_each_fib6_node_rt_rcu(&table->tb6_root) { |
David Ahern | ad1601a | 2019-03-27 20:53:56 -0700 | [diff] [blame] | 3579 | struct fib6_nh *nh = &rt->fib6_nh; |
| 3580 | |
| 3581 | if (dev == nh->fib_nh_dev && |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 3582 | ((rt->fib6_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) && |
David Ahern | ad1601a | 2019-03-27 20:53:56 -0700 | [diff] [blame] | 3583 | ipv6_addr_equal(&nh->fib_nh_gw6, addr)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3584 | break; |
| 3585 | } |
Wei Wang | e873e4b | 2018-07-21 20:56:32 -0700 | [diff] [blame] | 3586 | if (rt && !fib6_info_hold_safe(rt)) |
| 3587 | rt = NULL; |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 3588 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3589 | return rt; |
| 3590 | } |
| 3591 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 3592 | struct fib6_info *rt6_add_dflt_router(struct net *net, |
David Ahern | afb1d4b5 | 2018-04-17 17:33:11 -0700 | [diff] [blame] | 3593 | const struct in6_addr *gwaddr, |
YOSHIFUJI Hideaki | ebacaaa | 2006-03-20 17:04:53 -0800 | [diff] [blame] | 3594 | struct net_device *dev, |
| 3595 | unsigned int pref) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3596 | { |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 3597 | struct fib6_config cfg = { |
David Ahern | ca25449 | 2015-10-12 11:47:10 -0700 | [diff] [blame] | 3598 | .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT, |
Rami Rosen | 238fc7e | 2008-02-09 23:43:11 -0800 | [diff] [blame] | 3599 | .fc_metric = IP6_RT_PRIO_USER, |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 3600 | .fc_ifindex = dev->ifindex, |
| 3601 | .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT | |
| 3602 | RTF_UP | RTF_EXPIRES | RTF_PREF(pref), |
Xin Long | b91d532 | 2017-08-03 14:13:46 +0800 | [diff] [blame] | 3603 | .fc_protocol = RTPROT_RA, |
David Ahern | e8478e8 | 2018-04-17 17:33:13 -0700 | [diff] [blame] | 3604 | .fc_type = RTN_UNICAST, |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 3605 | .fc_nlinfo.portid = 0, |
Daniel Lezcano | 5578689 | 2008-03-04 13:47:47 -0800 | [diff] [blame] | 3606 | .fc_nlinfo.nlh = NULL, |
David Ahern | afb1d4b5 | 2018-04-17 17:33:11 -0700 | [diff] [blame] | 3607 | .fc_nlinfo.nl_net = net, |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 3608 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3609 | |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 3610 | cfg.fc_gateway = *gwaddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3611 | |
David Ahern | acb54e3 | 2018-04-17 17:33:22 -0700 | [diff] [blame] | 3612 | if (!ip6_route_add(&cfg, GFP_ATOMIC, NULL)) { |
David Ahern | 830218c | 2016-10-24 10:52:35 -0700 | [diff] [blame] | 3613 | struct fib6_table *table; |
| 3614 | |
| 3615 | table = fib6_get_table(dev_net(dev), cfg.fc_table); |
| 3616 | if (table) |
| 3617 | table->flags |= RT6_TABLE_HAS_DFLT_ROUTER; |
| 3618 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3619 | |
David Ahern | afb1d4b5 | 2018-04-17 17:33:11 -0700 | [diff] [blame] | 3620 | return rt6_get_dflt_router(net, gwaddr, dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3621 | } |
| 3622 | |
David Ahern | afb1d4b5 | 2018-04-17 17:33:11 -0700 | [diff] [blame] | 3623 | static void __rt6_purge_dflt_routers(struct net *net, |
| 3624 | struct fib6_table *table) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3625 | { |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 3626 | struct fib6_info *rt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3627 | |
| 3628 | restart: |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 3629 | rcu_read_lock(); |
| 3630 | for_each_fib6_node_rt_rcu(&table->tb6_root) { |
David Ahern | dcd1f57 | 2018-04-18 15:39:05 -0700 | [diff] [blame] | 3631 | struct net_device *dev = fib6_info_nh_dev(rt); |
| 3632 | struct inet6_dev *idev = dev ? __in6_dev_get(dev) : NULL; |
| 3633 | |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 3634 | if (rt->fib6_flags & (RTF_DEFAULT | RTF_ADDRCONF) && |
Wei Wang | e873e4b | 2018-07-21 20:56:32 -0700 | [diff] [blame] | 3635 | (!idev || idev->cnf.accept_ra != 2) && |
| 3636 | fib6_info_hold_safe(rt)) { |
David Ahern | 93531c6 | 2018-04-17 17:33:25 -0700 | [diff] [blame] | 3637 | rcu_read_unlock(); |
| 3638 | ip6_del_rt(net, rt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3639 | goto restart; |
| 3640 | } |
| 3641 | } |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 3642 | rcu_read_unlock(); |
David Ahern | 830218c | 2016-10-24 10:52:35 -0700 | [diff] [blame] | 3643 | |
| 3644 | table->flags &= ~RT6_TABLE_HAS_DFLT_ROUTER; |
| 3645 | } |
| 3646 | |
| 3647 | void rt6_purge_dflt_routers(struct net *net) |
| 3648 | { |
| 3649 | struct fib6_table *table; |
| 3650 | struct hlist_head *head; |
| 3651 | unsigned int h; |
| 3652 | |
| 3653 | rcu_read_lock(); |
| 3654 | |
| 3655 | for (h = 0; h < FIB6_TABLE_HASHSZ; h++) { |
| 3656 | head = &net->ipv6.fib_table_hash[h]; |
| 3657 | hlist_for_each_entry_rcu(table, head, tb6_hlist) { |
| 3658 | if (table->flags & RT6_TABLE_HAS_DFLT_ROUTER) |
David Ahern | afb1d4b5 | 2018-04-17 17:33:11 -0700 | [diff] [blame] | 3659 | __rt6_purge_dflt_routers(net, table); |
David Ahern | 830218c | 2016-10-24 10:52:35 -0700 | [diff] [blame] | 3660 | } |
| 3661 | } |
| 3662 | |
| 3663 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3664 | } |
| 3665 | |
Daniel Lezcano | 5578689 | 2008-03-04 13:47:47 -0800 | [diff] [blame] | 3666 | static void rtmsg_to_fib6_config(struct net *net, |
| 3667 | struct in6_rtmsg *rtmsg, |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 3668 | struct fib6_config *cfg) |
| 3669 | { |
Maciej Żenczykowski | 8823a3a | 2018-09-29 23:44:52 -0700 | [diff] [blame] | 3670 | *cfg = (struct fib6_config){ |
| 3671 | .fc_table = l3mdev_fib_table_by_index(net, rtmsg->rtmsg_ifindex) ? |
| 3672 | : RT6_TABLE_MAIN, |
| 3673 | .fc_ifindex = rtmsg->rtmsg_ifindex, |
David Ahern | 67f6951 | 2019-03-21 05:21:34 -0700 | [diff] [blame] | 3674 | .fc_metric = rtmsg->rtmsg_metric ? : IP6_RT_PRIO_USER, |
Maciej Żenczykowski | 8823a3a | 2018-09-29 23:44:52 -0700 | [diff] [blame] | 3675 | .fc_expires = rtmsg->rtmsg_info, |
| 3676 | .fc_dst_len = rtmsg->rtmsg_dst_len, |
| 3677 | .fc_src_len = rtmsg->rtmsg_src_len, |
| 3678 | .fc_flags = rtmsg->rtmsg_flags, |
| 3679 | .fc_type = rtmsg->rtmsg_type, |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 3680 | |
Maciej Żenczykowski | 8823a3a | 2018-09-29 23:44:52 -0700 | [diff] [blame] | 3681 | .fc_nlinfo.nl_net = net, |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 3682 | |
Maciej Żenczykowski | 8823a3a | 2018-09-29 23:44:52 -0700 | [diff] [blame] | 3683 | .fc_dst = rtmsg->rtmsg_dst, |
| 3684 | .fc_src = rtmsg->rtmsg_src, |
| 3685 | .fc_gateway = rtmsg->rtmsg_gateway, |
| 3686 | }; |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 3687 | } |
| 3688 | |
Daniel Lezcano | 5578689 | 2008-03-04 13:47:47 -0800 | [diff] [blame] | 3689 | 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] | 3690 | { |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 3691 | struct fib6_config cfg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3692 | struct in6_rtmsg rtmsg; |
| 3693 | int err; |
| 3694 | |
Ian Morris | 67ba415 | 2014-08-24 21:53:10 +0100 | [diff] [blame] | 3695 | switch (cmd) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3696 | case SIOCADDRT: /* Add a route */ |
| 3697 | case SIOCDELRT: /* Delete a route */ |
Eric W. Biederman | af31f41 | 2012-11-16 03:03:06 +0000 | [diff] [blame] | 3698 | if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3699 | return -EPERM; |
| 3700 | err = copy_from_user(&rtmsg, arg, |
| 3701 | sizeof(struct in6_rtmsg)); |
| 3702 | if (err) |
| 3703 | return -EFAULT; |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 3704 | |
Daniel Lezcano | 5578689 | 2008-03-04 13:47:47 -0800 | [diff] [blame] | 3705 | rtmsg_to_fib6_config(net, &rtmsg, &cfg); |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 3706 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3707 | rtnl_lock(); |
| 3708 | switch (cmd) { |
| 3709 | case SIOCADDRT: |
David Ahern | acb54e3 | 2018-04-17 17:33:22 -0700 | [diff] [blame] | 3710 | err = ip6_route_add(&cfg, GFP_KERNEL, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3711 | break; |
| 3712 | case SIOCDELRT: |
David Ahern | 333c430 | 2017-05-21 10:12:04 -0600 | [diff] [blame] | 3713 | err = ip6_route_del(&cfg, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3714 | break; |
| 3715 | default: |
| 3716 | err = -EINVAL; |
| 3717 | } |
| 3718 | rtnl_unlock(); |
| 3719 | |
| 3720 | return err; |
Stephen Hemminger | 3ff50b7 | 2007-04-20 17:09:22 -0700 | [diff] [blame] | 3721 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3722 | |
| 3723 | return -EINVAL; |
| 3724 | } |
| 3725 | |
| 3726 | /* |
| 3727 | * Drop the packet on the floor |
| 3728 | */ |
| 3729 | |
Brian Haley | d5fdd6b | 2009-06-23 04:31:07 -0700 | [diff] [blame] | 3730 | 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] | 3731 | { |
YOSHIFUJI Hideaki | 612f09e | 2007-04-13 16:18:02 -0700 | [diff] [blame] | 3732 | int type; |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 3733 | struct dst_entry *dst = skb_dst(skb); |
YOSHIFUJI Hideaki | 612f09e | 2007-04-13 16:18:02 -0700 | [diff] [blame] | 3734 | switch (ipstats_mib_noroutes) { |
| 3735 | case IPSTATS_MIB_INNOROUTES: |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 3736 | type = ipv6_addr_type(&ipv6_hdr(skb)->daddr); |
Ulrich Weber | 45bb006 | 2010-02-25 23:28:58 +0000 | [diff] [blame] | 3737 | if (type == IPV6_ADDR_ANY) { |
Stephen Suryaputra | bdb7cc6 | 2018-04-16 13:42:16 -0400 | [diff] [blame] | 3738 | IP6_INC_STATS(dev_net(dst->dev), |
| 3739 | __in6_dev_get_safely(skb->dev), |
Denis V. Lunev | 3bd653c | 2008-10-08 10:54:51 -0700 | [diff] [blame] | 3740 | IPSTATS_MIB_INADDRERRORS); |
YOSHIFUJI Hideaki | 612f09e | 2007-04-13 16:18:02 -0700 | [diff] [blame] | 3741 | break; |
| 3742 | } |
| 3743 | /* FALLTHROUGH */ |
| 3744 | case IPSTATS_MIB_OUTNOROUTES: |
Denis V. Lunev | 3bd653c | 2008-10-08 10:54:51 -0700 | [diff] [blame] | 3745 | IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst), |
| 3746 | ipstats_mib_noroutes); |
YOSHIFUJI Hideaki | 612f09e | 2007-04-13 16:18:02 -0700 | [diff] [blame] | 3747 | break; |
| 3748 | } |
Alexey Dobriyan | 3ffe533 | 2010-02-18 08:25:24 +0000 | [diff] [blame] | 3749 | icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3750 | kfree_skb(skb); |
| 3751 | return 0; |
| 3752 | } |
| 3753 | |
Thomas Graf | 9ce8ade | 2006-10-18 20:46:54 -0700 | [diff] [blame] | 3754 | static int ip6_pkt_discard(struct sk_buff *skb) |
| 3755 | { |
YOSHIFUJI Hideaki | 612f09e | 2007-04-13 16:18:02 -0700 | [diff] [blame] | 3756 | return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES); |
Thomas Graf | 9ce8ade | 2006-10-18 20:46:54 -0700 | [diff] [blame] | 3757 | } |
| 3758 | |
Eric W. Biederman | ede2059 | 2015-10-07 16:48:47 -0500 | [diff] [blame] | 3759 | 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] | 3760 | { |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 3761 | skb->dev = skb_dst(skb)->dev; |
YOSHIFUJI Hideaki | 612f09e | 2007-04-13 16:18:02 -0700 | [diff] [blame] | 3762 | return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3763 | } |
| 3764 | |
Thomas Graf | 9ce8ade | 2006-10-18 20:46:54 -0700 | [diff] [blame] | 3765 | static int ip6_pkt_prohibit(struct sk_buff *skb) |
| 3766 | { |
YOSHIFUJI Hideaki | 612f09e | 2007-04-13 16:18:02 -0700 | [diff] [blame] | 3767 | return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES); |
Thomas Graf | 9ce8ade | 2006-10-18 20:46:54 -0700 | [diff] [blame] | 3768 | } |
| 3769 | |
Eric W. Biederman | ede2059 | 2015-10-07 16:48:47 -0500 | [diff] [blame] | 3770 | 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] | 3771 | { |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 3772 | skb->dev = skb_dst(skb)->dev; |
YOSHIFUJI Hideaki | 612f09e | 2007-04-13 16:18:02 -0700 | [diff] [blame] | 3773 | return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES); |
Thomas Graf | 9ce8ade | 2006-10-18 20:46:54 -0700 | [diff] [blame] | 3774 | } |
| 3775 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3776 | /* |
| 3777 | * Allocate a dst for local (unicast / anycast) address. |
| 3778 | */ |
| 3779 | |
David Ahern | 360a988 | 2018-04-18 15:39:00 -0700 | [diff] [blame] | 3780 | struct fib6_info *addrconf_f6i_alloc(struct net *net, |
| 3781 | struct inet6_dev *idev, |
| 3782 | const struct in6_addr *addr, |
| 3783 | bool anycast, gfp_t gfp_flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3784 | { |
David Ahern | c7a1ce3 | 2019-03-21 05:21:35 -0700 | [diff] [blame] | 3785 | struct fib6_config cfg = { |
| 3786 | .fc_table = l3mdev_fib_table(idev->dev) ? : RT6_TABLE_LOCAL, |
| 3787 | .fc_ifindex = idev->dev->ifindex, |
| 3788 | .fc_flags = RTF_UP | RTF_ADDRCONF | RTF_NONEXTHOP, |
| 3789 | .fc_dst = *addr, |
| 3790 | .fc_dst_len = 128, |
| 3791 | .fc_protocol = RTPROT_KERNEL, |
| 3792 | .fc_nlinfo.nl_net = net, |
| 3793 | .fc_ignore_dev_down = true, |
| 3794 | }; |
David Ahern | 5f02ce24 | 2016-09-10 12:09:54 -0700 | [diff] [blame] | 3795 | |
David Ahern | e8478e8 | 2018-04-17 17:33:13 -0700 | [diff] [blame] | 3796 | if (anycast) { |
David Ahern | c7a1ce3 | 2019-03-21 05:21:35 -0700 | [diff] [blame] | 3797 | cfg.fc_type = RTN_ANYCAST; |
| 3798 | cfg.fc_flags |= RTF_ANYCAST; |
David Ahern | e8478e8 | 2018-04-17 17:33:13 -0700 | [diff] [blame] | 3799 | } else { |
David Ahern | c7a1ce3 | 2019-03-21 05:21:35 -0700 | [diff] [blame] | 3800 | cfg.fc_type = RTN_LOCAL; |
| 3801 | cfg.fc_flags |= RTF_LOCAL; |
David Ahern | e8478e8 | 2018-04-17 17:33:13 -0700 | [diff] [blame] | 3802 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3803 | |
David Ahern | c7a1ce3 | 2019-03-21 05:21:35 -0700 | [diff] [blame] | 3804 | return ip6_route_info_create(&cfg, gfp_flags, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3805 | } |
| 3806 | |
Daniel Walter | c3968a8 | 2011-04-13 21:10:57 +0000 | [diff] [blame] | 3807 | /* remove deleted ip from prefsrc entries */ |
| 3808 | struct arg_dev_net_ip { |
| 3809 | struct net_device *dev; |
| 3810 | struct net *net; |
| 3811 | struct in6_addr *addr; |
| 3812 | }; |
| 3813 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 3814 | static int fib6_remove_prefsrc(struct fib6_info *rt, void *arg) |
Daniel Walter | c3968a8 | 2011-04-13 21:10:57 +0000 | [diff] [blame] | 3815 | { |
| 3816 | struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev; |
| 3817 | struct net *net = ((struct arg_dev_net_ip *)arg)->net; |
| 3818 | struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr; |
| 3819 | |
David Ahern | ad1601a | 2019-03-27 20:53:56 -0700 | [diff] [blame] | 3820 | if (((void *)rt->fib6_nh.fib_nh_dev == dev || !dev) && |
David Ahern | 421842e | 2018-04-17 17:33:18 -0700 | [diff] [blame] | 3821 | rt != net->ipv6.fib6_null_entry && |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 3822 | ipv6_addr_equal(addr, &rt->fib6_prefsrc.addr)) { |
Wei Wang | 60006a4 | 2017-10-06 12:05:58 -0700 | [diff] [blame] | 3823 | spin_lock_bh(&rt6_exception_lock); |
Daniel Walter | c3968a8 | 2011-04-13 21:10:57 +0000 | [diff] [blame] | 3824 | /* remove prefsrc entry */ |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 3825 | rt->fib6_prefsrc.plen = 0; |
Wei Wang | 60006a4 | 2017-10-06 12:05:58 -0700 | [diff] [blame] | 3826 | spin_unlock_bh(&rt6_exception_lock); |
Daniel Walter | c3968a8 | 2011-04-13 21:10:57 +0000 | [diff] [blame] | 3827 | } |
| 3828 | return 0; |
| 3829 | } |
| 3830 | |
| 3831 | void rt6_remove_prefsrc(struct inet6_ifaddr *ifp) |
| 3832 | { |
| 3833 | struct net *net = dev_net(ifp->idev->dev); |
| 3834 | struct arg_dev_net_ip adni = { |
| 3835 | .dev = ifp->idev->dev, |
| 3836 | .net = net, |
| 3837 | .addr = &ifp->addr, |
| 3838 | }; |
Li RongQing | 0c3584d | 2013-12-27 16:32:38 +0800 | [diff] [blame] | 3839 | fib6_clean_all(net, fib6_remove_prefsrc, &adni); |
Daniel Walter | c3968a8 | 2011-04-13 21:10:57 +0000 | [diff] [blame] | 3840 | } |
| 3841 | |
David Ahern | 2b2450c | 2019-03-27 20:53:52 -0700 | [diff] [blame] | 3842 | #define RTF_RA_ROUTER (RTF_ADDRCONF | RTF_DEFAULT) |
Duan Jiong | be7a010 | 2014-05-15 15:56:14 +0800 | [diff] [blame] | 3843 | |
| 3844 | /* Remove routers and update dst entries when gateway turn into host. */ |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 3845 | static int fib6_clean_tohost(struct fib6_info *rt, void *arg) |
Duan Jiong | be7a010 | 2014-05-15 15:56:14 +0800 | [diff] [blame] | 3846 | { |
| 3847 | struct in6_addr *gateway = (struct in6_addr *)arg; |
| 3848 | |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 3849 | if (((rt->fib6_flags & RTF_RA_ROUTER) == RTF_RA_ROUTER) && |
David Ahern | bdf0046 | 2019-04-05 16:30:26 -0700 | [diff] [blame] | 3850 | rt->fib6_nh.fib_nh_gw_family && |
David Ahern | ad1601a | 2019-03-27 20:53:56 -0700 | [diff] [blame] | 3851 | ipv6_addr_equal(gateway, &rt->fib6_nh.fib_nh_gw6)) { |
Duan Jiong | be7a010 | 2014-05-15 15:56:14 +0800 | [diff] [blame] | 3852 | return -1; |
| 3853 | } |
Wei Wang | b16cb45 | 2017-10-06 12:06:00 -0700 | [diff] [blame] | 3854 | |
| 3855 | /* Further clean up cached routes in exception table. |
| 3856 | * This is needed because cached route may have a different |
| 3857 | * gateway than its 'parent' in the case of an ip redirect. |
| 3858 | */ |
| 3859 | rt6_exceptions_clean_tohost(rt, gateway); |
| 3860 | |
Duan Jiong | be7a010 | 2014-05-15 15:56:14 +0800 | [diff] [blame] | 3861 | return 0; |
| 3862 | } |
| 3863 | |
| 3864 | void rt6_clean_tohost(struct net *net, struct in6_addr *gateway) |
| 3865 | { |
| 3866 | fib6_clean_all(net, fib6_clean_tohost, gateway); |
| 3867 | } |
| 3868 | |
Ido Schimmel | 2127d95 | 2018-01-07 12:45:03 +0200 | [diff] [blame] | 3869 | struct arg_netdev_event { |
| 3870 | const struct net_device *dev; |
Ido Schimmel | 4c981e2 | 2018-01-07 12:45:04 +0200 | [diff] [blame] | 3871 | union { |
| 3872 | unsigned int nh_flags; |
| 3873 | unsigned long event; |
| 3874 | }; |
Ido Schimmel | 2127d95 | 2018-01-07 12:45:03 +0200 | [diff] [blame] | 3875 | }; |
| 3876 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 3877 | 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] | 3878 | { |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 3879 | struct fib6_info *iter; |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 3880 | struct fib6_node *fn; |
| 3881 | |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 3882 | fn = rcu_dereference_protected(rt->fib6_node, |
| 3883 | lockdep_is_held(&rt->fib6_table->tb6_lock)); |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 3884 | iter = rcu_dereference_protected(fn->leaf, |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 3885 | lockdep_is_held(&rt->fib6_table->tb6_lock)); |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 3886 | while (iter) { |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 3887 | if (iter->fib6_metric == rt->fib6_metric && |
David Ahern | 33bd5ac | 2018-07-03 14:36:21 -0700 | [diff] [blame] | 3888 | rt6_qualify_for_ecmp(iter)) |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 3889 | return iter; |
David Ahern | 8fb11a9 | 2018-05-04 13:54:24 -0700 | [diff] [blame] | 3890 | iter = rcu_dereference_protected(iter->fib6_next, |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 3891 | lockdep_is_held(&rt->fib6_table->tb6_lock)); |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 3892 | } |
| 3893 | |
| 3894 | return NULL; |
| 3895 | } |
| 3896 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 3897 | static bool rt6_is_dead(const struct fib6_info *rt) |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 3898 | { |
David Ahern | ad1601a | 2019-03-27 20:53:56 -0700 | [diff] [blame] | 3899 | if (rt->fib6_nh.fib_nh_flags & RTNH_F_DEAD || |
| 3900 | (rt->fib6_nh.fib_nh_flags & RTNH_F_LINKDOWN && |
| 3901 | ip6_ignore_linkdown(rt->fib6_nh.fib_nh_dev))) |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 3902 | return true; |
| 3903 | |
| 3904 | return false; |
| 3905 | } |
| 3906 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 3907 | static int rt6_multipath_total_weight(const struct fib6_info *rt) |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 3908 | { |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 3909 | struct fib6_info *iter; |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 3910 | int total = 0; |
| 3911 | |
| 3912 | if (!rt6_is_dead(rt)) |
David Ahern | ad1601a | 2019-03-27 20:53:56 -0700 | [diff] [blame] | 3913 | total += rt->fib6_nh.fib_nh_weight; |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 3914 | |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 3915 | list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) { |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 3916 | if (!rt6_is_dead(iter)) |
David Ahern | ad1601a | 2019-03-27 20:53:56 -0700 | [diff] [blame] | 3917 | total += iter->fib6_nh.fib_nh_weight; |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 3918 | } |
| 3919 | |
| 3920 | return total; |
| 3921 | } |
| 3922 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 3923 | 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] | 3924 | { |
| 3925 | int upper_bound = -1; |
| 3926 | |
| 3927 | if (!rt6_is_dead(rt)) { |
David Ahern | ad1601a | 2019-03-27 20:53:56 -0700 | [diff] [blame] | 3928 | *weight += rt->fib6_nh.fib_nh_weight; |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 3929 | upper_bound = DIV_ROUND_CLOSEST_ULL((u64) (*weight) << 31, |
| 3930 | total) - 1; |
| 3931 | } |
David Ahern | ad1601a | 2019-03-27 20:53:56 -0700 | [diff] [blame] | 3932 | atomic_set(&rt->fib6_nh.fib_nh_upper_bound, upper_bound); |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 3933 | } |
| 3934 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 3935 | 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] | 3936 | { |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 3937 | struct fib6_info *iter; |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 3938 | int weight = 0; |
| 3939 | |
| 3940 | rt6_upper_bound_set(rt, &weight, total); |
| 3941 | |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 3942 | list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 3943 | rt6_upper_bound_set(iter, &weight, total); |
| 3944 | } |
| 3945 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 3946 | void rt6_multipath_rebalance(struct fib6_info *rt) |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 3947 | { |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 3948 | struct fib6_info *first; |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 3949 | int total; |
| 3950 | |
| 3951 | /* In case the entire multipath route was marked for flushing, |
| 3952 | * then there is no need to rebalance upon the removal of every |
| 3953 | * sibling route. |
| 3954 | */ |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 3955 | if (!rt->fib6_nsiblings || rt->should_flush) |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 3956 | return; |
| 3957 | |
| 3958 | /* During lookup routes are evaluated in order, so we need to |
| 3959 | * make sure upper bounds are assigned from the first sibling |
| 3960 | * onwards. |
| 3961 | */ |
| 3962 | first = rt6_multipath_first_sibling(rt); |
| 3963 | if (WARN_ON_ONCE(!first)) |
| 3964 | return; |
| 3965 | |
| 3966 | total = rt6_multipath_total_weight(first); |
| 3967 | rt6_multipath_upper_bound_set(first, total); |
| 3968 | } |
| 3969 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 3970 | static int fib6_ifup(struct fib6_info *rt, void *p_arg) |
Ido Schimmel | 2127d95 | 2018-01-07 12:45:03 +0200 | [diff] [blame] | 3971 | { |
| 3972 | const struct arg_netdev_event *arg = p_arg; |
David Ahern | 7aef685 | 2018-04-17 17:33:10 -0700 | [diff] [blame] | 3973 | struct net *net = dev_net(arg->dev); |
Ido Schimmel | 2127d95 | 2018-01-07 12:45:03 +0200 | [diff] [blame] | 3974 | |
David Ahern | ad1601a | 2019-03-27 20:53:56 -0700 | [diff] [blame] | 3975 | if (rt != net->ipv6.fib6_null_entry && |
| 3976 | rt->fib6_nh.fib_nh_dev == arg->dev) { |
| 3977 | rt->fib6_nh.fib_nh_flags &= ~arg->nh_flags; |
David Ahern | 7aef685 | 2018-04-17 17:33:10 -0700 | [diff] [blame] | 3978 | fib6_update_sernum_upto_root(net, rt); |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 3979 | rt6_multipath_rebalance(rt); |
Ido Schimmel | 1de178e | 2018-01-07 12:45:15 +0200 | [diff] [blame] | 3980 | } |
Ido Schimmel | 2127d95 | 2018-01-07 12:45:03 +0200 | [diff] [blame] | 3981 | |
| 3982 | return 0; |
| 3983 | } |
| 3984 | |
| 3985 | void rt6_sync_up(struct net_device *dev, unsigned int nh_flags) |
| 3986 | { |
| 3987 | struct arg_netdev_event arg = { |
| 3988 | .dev = dev, |
Ido Schimmel | 6802f3a | 2018-01-12 22:07:36 +0200 | [diff] [blame] | 3989 | { |
| 3990 | .nh_flags = nh_flags, |
| 3991 | }, |
Ido Schimmel | 2127d95 | 2018-01-07 12:45:03 +0200 | [diff] [blame] | 3992 | }; |
| 3993 | |
| 3994 | if (nh_flags & RTNH_F_DEAD && netif_carrier_ok(dev)) |
| 3995 | arg.nh_flags |= RTNH_F_LINKDOWN; |
| 3996 | |
| 3997 | fib6_clean_all(dev_net(dev), fib6_ifup, &arg); |
| 3998 | } |
| 3999 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4000 | static bool rt6_multipath_uses_dev(const struct fib6_info *rt, |
Ido Schimmel | 1de178e | 2018-01-07 12:45:15 +0200 | [diff] [blame] | 4001 | const struct net_device *dev) |
| 4002 | { |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4003 | struct fib6_info *iter; |
Ido Schimmel | 1de178e | 2018-01-07 12:45:15 +0200 | [diff] [blame] | 4004 | |
David Ahern | ad1601a | 2019-03-27 20:53:56 -0700 | [diff] [blame] | 4005 | if (rt->fib6_nh.fib_nh_dev == dev) |
Ido Schimmel | 1de178e | 2018-01-07 12:45:15 +0200 | [diff] [blame] | 4006 | return true; |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 4007 | list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) |
David Ahern | ad1601a | 2019-03-27 20:53:56 -0700 | [diff] [blame] | 4008 | if (iter->fib6_nh.fib_nh_dev == dev) |
Ido Schimmel | 1de178e | 2018-01-07 12:45:15 +0200 | [diff] [blame] | 4009 | return true; |
| 4010 | |
| 4011 | return false; |
| 4012 | } |
| 4013 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4014 | static void rt6_multipath_flush(struct fib6_info *rt) |
Ido Schimmel | 1de178e | 2018-01-07 12:45:15 +0200 | [diff] [blame] | 4015 | { |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4016 | struct fib6_info *iter; |
Ido Schimmel | 1de178e | 2018-01-07 12:45:15 +0200 | [diff] [blame] | 4017 | |
| 4018 | rt->should_flush = 1; |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 4019 | list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) |
Ido Schimmel | 1de178e | 2018-01-07 12:45:15 +0200 | [diff] [blame] | 4020 | iter->should_flush = 1; |
| 4021 | } |
| 4022 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4023 | static unsigned int rt6_multipath_dead_count(const struct fib6_info *rt, |
Ido Schimmel | 1de178e | 2018-01-07 12:45:15 +0200 | [diff] [blame] | 4024 | const struct net_device *down_dev) |
| 4025 | { |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4026 | struct fib6_info *iter; |
Ido Schimmel | 1de178e | 2018-01-07 12:45:15 +0200 | [diff] [blame] | 4027 | unsigned int dead = 0; |
| 4028 | |
David Ahern | ad1601a | 2019-03-27 20:53:56 -0700 | [diff] [blame] | 4029 | if (rt->fib6_nh.fib_nh_dev == down_dev || |
| 4030 | rt->fib6_nh.fib_nh_flags & RTNH_F_DEAD) |
Ido Schimmel | 1de178e | 2018-01-07 12:45:15 +0200 | [diff] [blame] | 4031 | dead++; |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 4032 | list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) |
David Ahern | ad1601a | 2019-03-27 20:53:56 -0700 | [diff] [blame] | 4033 | if (iter->fib6_nh.fib_nh_dev == down_dev || |
| 4034 | iter->fib6_nh.fib_nh_flags & RTNH_F_DEAD) |
Ido Schimmel | 1de178e | 2018-01-07 12:45:15 +0200 | [diff] [blame] | 4035 | dead++; |
| 4036 | |
| 4037 | return dead; |
| 4038 | } |
| 4039 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4040 | static void rt6_multipath_nh_flags_set(struct fib6_info *rt, |
Ido Schimmel | 1de178e | 2018-01-07 12:45:15 +0200 | [diff] [blame] | 4041 | const struct net_device *dev, |
| 4042 | unsigned int nh_flags) |
| 4043 | { |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4044 | struct fib6_info *iter; |
Ido Schimmel | 1de178e | 2018-01-07 12:45:15 +0200 | [diff] [blame] | 4045 | |
David Ahern | ad1601a | 2019-03-27 20:53:56 -0700 | [diff] [blame] | 4046 | if (rt->fib6_nh.fib_nh_dev == dev) |
| 4047 | rt->fib6_nh.fib_nh_flags |= nh_flags; |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 4048 | list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) |
David Ahern | ad1601a | 2019-03-27 20:53:56 -0700 | [diff] [blame] | 4049 | if (iter->fib6_nh.fib_nh_dev == dev) |
| 4050 | iter->fib6_nh.fib_nh_flags |= nh_flags; |
Ido Schimmel | 1de178e | 2018-01-07 12:45:15 +0200 | [diff] [blame] | 4051 | } |
| 4052 | |
David Ahern | a1a22c1 | 2017-01-18 07:40:36 -0800 | [diff] [blame] | 4053 | /* called with write lock held for table with rt */ |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4054 | static int fib6_ifdown(struct fib6_info *rt, void *p_arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4055 | { |
Ido Schimmel | 4c981e2 | 2018-01-07 12:45:04 +0200 | [diff] [blame] | 4056 | const struct arg_netdev_event *arg = p_arg; |
| 4057 | const struct net_device *dev = arg->dev; |
David Ahern | 7aef685 | 2018-04-17 17:33:10 -0700 | [diff] [blame] | 4058 | struct net *net = dev_net(dev); |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 4059 | |
David Ahern | 421842e | 2018-04-17 17:33:18 -0700 | [diff] [blame] | 4060 | if (rt == net->ipv6.fib6_null_entry) |
Ido Schimmel | 27c6fa7 | 2018-01-07 12:45:05 +0200 | [diff] [blame] | 4061 | return 0; |
| 4062 | |
| 4063 | switch (arg->event) { |
| 4064 | case NETDEV_UNREGISTER: |
David Ahern | ad1601a | 2019-03-27 20:53:56 -0700 | [diff] [blame] | 4065 | return rt->fib6_nh.fib_nh_dev == dev ? -1 : 0; |
Ido Schimmel | 27c6fa7 | 2018-01-07 12:45:05 +0200 | [diff] [blame] | 4066 | case NETDEV_DOWN: |
Ido Schimmel | 1de178e | 2018-01-07 12:45:15 +0200 | [diff] [blame] | 4067 | if (rt->should_flush) |
Ido Schimmel | 27c6fa7 | 2018-01-07 12:45:05 +0200 | [diff] [blame] | 4068 | return -1; |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 4069 | if (!rt->fib6_nsiblings) |
David Ahern | ad1601a | 2019-03-27 20:53:56 -0700 | [diff] [blame] | 4070 | return rt->fib6_nh.fib_nh_dev == dev ? -1 : 0; |
Ido Schimmel | 1de178e | 2018-01-07 12:45:15 +0200 | [diff] [blame] | 4071 | if (rt6_multipath_uses_dev(rt, dev)) { |
| 4072 | unsigned int count; |
| 4073 | |
| 4074 | count = rt6_multipath_dead_count(rt, dev); |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 4075 | if (rt->fib6_nsiblings + 1 == count) { |
Ido Schimmel | 1de178e | 2018-01-07 12:45:15 +0200 | [diff] [blame] | 4076 | rt6_multipath_flush(rt); |
| 4077 | return -1; |
| 4078 | } |
| 4079 | rt6_multipath_nh_flags_set(rt, dev, RTNH_F_DEAD | |
| 4080 | RTNH_F_LINKDOWN); |
David Ahern | 7aef685 | 2018-04-17 17:33:10 -0700 | [diff] [blame] | 4081 | fib6_update_sernum(net, rt); |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 4082 | rt6_multipath_rebalance(rt); |
Ido Schimmel | 1de178e | 2018-01-07 12:45:15 +0200 | [diff] [blame] | 4083 | } |
| 4084 | return -2; |
Ido Schimmel | 27c6fa7 | 2018-01-07 12:45:05 +0200 | [diff] [blame] | 4085 | case NETDEV_CHANGE: |
David Ahern | ad1601a | 2019-03-27 20:53:56 -0700 | [diff] [blame] | 4086 | if (rt->fib6_nh.fib_nh_dev != dev || |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 4087 | rt->fib6_flags & (RTF_LOCAL | RTF_ANYCAST)) |
Ido Schimmel | 27c6fa7 | 2018-01-07 12:45:05 +0200 | [diff] [blame] | 4088 | break; |
David Ahern | ad1601a | 2019-03-27 20:53:56 -0700 | [diff] [blame] | 4089 | rt->fib6_nh.fib_nh_flags |= RTNH_F_LINKDOWN; |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 4090 | rt6_multipath_rebalance(rt); |
Ido Schimmel | 27c6fa7 | 2018-01-07 12:45:05 +0200 | [diff] [blame] | 4091 | break; |
Ido Schimmel | 2b24136 | 2018-01-07 12:45:02 +0200 | [diff] [blame] | 4092 | } |
David S. Miller | c159d30 | 2011-12-26 15:24:36 -0500 | [diff] [blame] | 4093 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4094 | return 0; |
| 4095 | } |
| 4096 | |
Ido Schimmel | 27c6fa7 | 2018-01-07 12:45:05 +0200 | [diff] [blame] | 4097 | void rt6_sync_down_dev(struct net_device *dev, unsigned long event) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4098 | { |
Ido Schimmel | 4c981e2 | 2018-01-07 12:45:04 +0200 | [diff] [blame] | 4099 | struct arg_netdev_event arg = { |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 4100 | .dev = dev, |
Ido Schimmel | 6802f3a | 2018-01-12 22:07:36 +0200 | [diff] [blame] | 4101 | { |
| 4102 | .event = event, |
| 4103 | }, |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 4104 | }; |
David Ahern | 7c6bb7d | 2018-10-11 20:17:21 -0700 | [diff] [blame] | 4105 | struct net *net = dev_net(dev); |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 4106 | |
David Ahern | 7c6bb7d | 2018-10-11 20:17:21 -0700 | [diff] [blame] | 4107 | if (net->ipv6.sysctl.skip_notify_on_dev_down) |
| 4108 | fib6_clean_all_skip_notify(net, fib6_ifdown, &arg); |
| 4109 | else |
| 4110 | fib6_clean_all(net, fib6_ifdown, &arg); |
Ido Schimmel | 4c981e2 | 2018-01-07 12:45:04 +0200 | [diff] [blame] | 4111 | } |
| 4112 | |
| 4113 | void rt6_disable_ip(struct net_device *dev, unsigned long event) |
| 4114 | { |
| 4115 | rt6_sync_down_dev(dev, event); |
| 4116 | rt6_uncached_list_flush_dev(dev_net(dev), dev); |
| 4117 | neigh_ifdown(&nd_tbl, dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4118 | } |
| 4119 | |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 4120 | struct rt6_mtu_change_arg { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4121 | struct net_device *dev; |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 4122 | unsigned int mtu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4123 | }; |
| 4124 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4125 | static int rt6_mtu_change_route(struct fib6_info *rt, void *p_arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4126 | { |
| 4127 | struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg; |
| 4128 | struct inet6_dev *idev; |
| 4129 | |
| 4130 | /* In IPv6 pmtu discovery is not optional, |
| 4131 | so that RTAX_MTU lock cannot disable it. |
| 4132 | We still use this lock to block changes |
| 4133 | caused by addrconf/ndisc. |
| 4134 | */ |
| 4135 | |
| 4136 | idev = __in6_dev_get(arg->dev); |
David S. Miller | 3830847 | 2011-12-03 18:02:47 -0500 | [diff] [blame] | 4137 | if (!idev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4138 | return 0; |
| 4139 | |
| 4140 | /* For administrative MTU increase, there is no way to discover |
| 4141 | IPv6 PMTU increase, so PMTU increase should be updated here. |
| 4142 | Since RFC 1981 doesn't include administrative MTU increase |
| 4143 | update PMTU increase is a MUST. (i.e. jumbo frame) |
| 4144 | */ |
David Ahern | ad1601a | 2019-03-27 20:53:56 -0700 | [diff] [blame] | 4145 | if (rt->fib6_nh.fib_nh_dev == arg->dev && |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 4146 | !fib6_metric_locked(rt, RTAX_MTU)) { |
| 4147 | u32 mtu = rt->fib6_pmtu; |
| 4148 | |
| 4149 | if (mtu >= arg->mtu || |
| 4150 | (mtu < arg->mtu && mtu == idev->cnf.mtu6)) |
| 4151 | fib6_metric_set(rt, RTAX_MTU, arg->mtu); |
| 4152 | |
Wei Wang | f5bbe7e | 2017-10-06 12:05:59 -0700 | [diff] [blame] | 4153 | spin_lock_bh(&rt6_exception_lock); |
Stefano Brivio | e9fa149 | 2018-03-06 11:10:19 +0100 | [diff] [blame] | 4154 | rt6_exceptions_update_pmtu(idev, rt, arg->mtu); |
Wei Wang | f5bbe7e | 2017-10-06 12:05:59 -0700 | [diff] [blame] | 4155 | spin_unlock_bh(&rt6_exception_lock); |
Simon Arlott | 566cfd8 | 2007-07-26 00:09:55 -0700 | [diff] [blame] | 4156 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4157 | return 0; |
| 4158 | } |
| 4159 | |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 4160 | void rt6_mtu_change(struct net_device *dev, unsigned int mtu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4161 | { |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 4162 | struct rt6_mtu_change_arg arg = { |
| 4163 | .dev = dev, |
| 4164 | .mtu = mtu, |
| 4165 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4166 | |
Li RongQing | 0c3584d | 2013-12-27 16:32:38 +0800 | [diff] [blame] | 4167 | fib6_clean_all(dev_net(dev), rt6_mtu_change_route, &arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4168 | } |
| 4169 | |
Patrick McHardy | ef7c79e | 2007-06-05 12:38:30 -0700 | [diff] [blame] | 4170 | static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = { |
Thomas Graf | 5176f91 | 2006-08-26 20:13:18 -0700 | [diff] [blame] | 4171 | [RTA_GATEWAY] = { .len = sizeof(struct in6_addr) }, |
Eric Dumazet | aa8f877 | 2018-04-22 18:29:23 -0700 | [diff] [blame] | 4172 | [RTA_PREFSRC] = { .len = sizeof(struct in6_addr) }, |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4173 | [RTA_OIF] = { .type = NLA_U32 }, |
Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 4174 | [RTA_IIF] = { .type = NLA_U32 }, |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4175 | [RTA_PRIORITY] = { .type = NLA_U32 }, |
| 4176 | [RTA_METRICS] = { .type = NLA_NESTED }, |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 4177 | [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) }, |
Lubomir Rintel | c78ba6d | 2015-03-11 15:39:21 +0100 | [diff] [blame] | 4178 | [RTA_PREF] = { .type = NLA_U8 }, |
Roopa Prabhu | 19e42e4 | 2015-07-21 10:43:48 +0200 | [diff] [blame] | 4179 | [RTA_ENCAP_TYPE] = { .type = NLA_U16 }, |
| 4180 | [RTA_ENCAP] = { .type = NLA_NESTED }, |
Xin Long | 32bc201 | 2015-12-16 17:50:11 +0800 | [diff] [blame] | 4181 | [RTA_EXPIRES] = { .type = NLA_U32 }, |
Lorenzo Colitti | 622ec2c | 2016-11-04 02:23:42 +0900 | [diff] [blame] | 4182 | [RTA_UID] = { .type = NLA_U32 }, |
Liping Zhang | 3b45a41 | 2017-02-27 20:59:39 +0800 | [diff] [blame] | 4183 | [RTA_MARK] = { .type = NLA_U32 }, |
Eric Dumazet | aa8f877 | 2018-04-22 18:29:23 -0700 | [diff] [blame] | 4184 | [RTA_TABLE] = { .type = NLA_U32 }, |
Roopa Prabhu | eacb938 | 2018-05-22 14:03:28 -0700 | [diff] [blame] | 4185 | [RTA_IP_PROTO] = { .type = NLA_U8 }, |
| 4186 | [RTA_SPORT] = { .type = NLA_U16 }, |
| 4187 | [RTA_DPORT] = { .type = NLA_U16 }, |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4188 | }; |
| 4189 | |
| 4190 | 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] | 4191 | struct fib6_config *cfg, |
| 4192 | struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4193 | { |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4194 | struct rtmsg *rtm; |
| 4195 | struct nlattr *tb[RTA_MAX+1]; |
Lubomir Rintel | c78ba6d | 2015-03-11 15:39:21 +0100 | [diff] [blame] | 4196 | unsigned int pref; |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4197 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4198 | |
Johannes Berg | fceb643 | 2017-04-12 14:34:07 +0200 | [diff] [blame] | 4199 | err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy, |
David Ahern | dac9c97 | 2018-10-07 20:16:24 -0700 | [diff] [blame] | 4200 | extack); |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4201 | if (err < 0) |
| 4202 | goto errout; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4203 | |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4204 | err = -EINVAL; |
| 4205 | rtm = nlmsg_data(nlh); |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4206 | |
Maciej Żenczykowski | 84db840 | 2018-09-29 23:44:53 -0700 | [diff] [blame] | 4207 | *cfg = (struct fib6_config){ |
| 4208 | .fc_table = rtm->rtm_table, |
| 4209 | .fc_dst_len = rtm->rtm_dst_len, |
| 4210 | .fc_src_len = rtm->rtm_src_len, |
| 4211 | .fc_flags = RTF_UP, |
| 4212 | .fc_protocol = rtm->rtm_protocol, |
| 4213 | .fc_type = rtm->rtm_type, |
| 4214 | |
| 4215 | .fc_nlinfo.portid = NETLINK_CB(skb).portid, |
| 4216 | .fc_nlinfo.nlh = nlh, |
| 4217 | .fc_nlinfo.nl_net = sock_net(skb->sk), |
| 4218 | }; |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4219 | |
Nicolas Dichtel | ef2c7d7 | 2012-09-05 02:12:42 +0000 | [diff] [blame] | 4220 | if (rtm->rtm_type == RTN_UNREACHABLE || |
| 4221 | rtm->rtm_type == RTN_BLACKHOLE || |
Nicolas Dichtel | b4949ab | 2012-09-06 05:53:35 +0000 | [diff] [blame] | 4222 | rtm->rtm_type == RTN_PROHIBIT || |
| 4223 | rtm->rtm_type == RTN_THROW) |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4224 | cfg->fc_flags |= RTF_REJECT; |
| 4225 | |
Maciej Żenczykowski | ab79ad1 | 2010-09-27 00:07:02 +0000 | [diff] [blame] | 4226 | if (rtm->rtm_type == RTN_LOCAL) |
| 4227 | cfg->fc_flags |= RTF_LOCAL; |
| 4228 | |
Martin KaFai Lau | 1f56a01f | 2015-04-28 13:03:03 -0700 | [diff] [blame] | 4229 | if (rtm->rtm_flags & RTM_F_CLONED) |
| 4230 | cfg->fc_flags |= RTF_CACHE; |
| 4231 | |
David Ahern | fc1e64e | 2018-01-25 16:55:09 -0800 | [diff] [blame] | 4232 | cfg->fc_flags |= (rtm->rtm_flags & RTNH_F_ONLINK); |
| 4233 | |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4234 | if (tb[RTA_GATEWAY]) { |
Jiri Benc | 67b61f6 | 2015-03-29 16:59:26 +0200 | [diff] [blame] | 4235 | cfg->fc_gateway = nla_get_in6_addr(tb[RTA_GATEWAY]); |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4236 | cfg->fc_flags |= RTF_GATEWAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4237 | } |
David Ahern | e381854 | 2019-02-26 09:00:03 -0800 | [diff] [blame] | 4238 | if (tb[RTA_VIA]) { |
| 4239 | NL_SET_ERR_MSG(extack, "IPv6 does not support RTA_VIA attribute"); |
| 4240 | goto errout; |
| 4241 | } |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4242 | |
| 4243 | if (tb[RTA_DST]) { |
| 4244 | int plen = (rtm->rtm_dst_len + 7) >> 3; |
| 4245 | |
| 4246 | if (nla_len(tb[RTA_DST]) < plen) |
| 4247 | goto errout; |
| 4248 | |
| 4249 | nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4250 | } |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4251 | |
| 4252 | if (tb[RTA_SRC]) { |
| 4253 | int plen = (rtm->rtm_src_len + 7) >> 3; |
| 4254 | |
| 4255 | if (nla_len(tb[RTA_SRC]) < plen) |
| 4256 | goto errout; |
| 4257 | |
| 4258 | nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4259 | } |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4260 | |
Daniel Walter | c3968a8 | 2011-04-13 21:10:57 +0000 | [diff] [blame] | 4261 | if (tb[RTA_PREFSRC]) |
Jiri Benc | 67b61f6 | 2015-03-29 16:59:26 +0200 | [diff] [blame] | 4262 | cfg->fc_prefsrc = nla_get_in6_addr(tb[RTA_PREFSRC]); |
Daniel Walter | c3968a8 | 2011-04-13 21:10:57 +0000 | [diff] [blame] | 4263 | |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4264 | if (tb[RTA_OIF]) |
| 4265 | cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]); |
| 4266 | |
| 4267 | if (tb[RTA_PRIORITY]) |
| 4268 | cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]); |
| 4269 | |
| 4270 | if (tb[RTA_METRICS]) { |
| 4271 | cfg->fc_mx = nla_data(tb[RTA_METRICS]); |
| 4272 | cfg->fc_mx_len = nla_len(tb[RTA_METRICS]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4273 | } |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4274 | |
| 4275 | if (tb[RTA_TABLE]) |
| 4276 | cfg->fc_table = nla_get_u32(tb[RTA_TABLE]); |
| 4277 | |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 4278 | if (tb[RTA_MULTIPATH]) { |
| 4279 | cfg->fc_mp = nla_data(tb[RTA_MULTIPATH]); |
| 4280 | cfg->fc_mp_len = nla_len(tb[RTA_MULTIPATH]); |
David Ahern | 9ed5959 | 2017-01-17 14:57:36 -0800 | [diff] [blame] | 4281 | |
| 4282 | err = lwtunnel_valid_encap_type_attr(cfg->fc_mp, |
David Ahern | c255bd6 | 2017-05-27 16:19:27 -0600 | [diff] [blame] | 4283 | cfg->fc_mp_len, extack); |
David Ahern | 9ed5959 | 2017-01-17 14:57:36 -0800 | [diff] [blame] | 4284 | if (err < 0) |
| 4285 | goto errout; |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 4286 | } |
| 4287 | |
Lubomir Rintel | c78ba6d | 2015-03-11 15:39:21 +0100 | [diff] [blame] | 4288 | if (tb[RTA_PREF]) { |
| 4289 | pref = nla_get_u8(tb[RTA_PREF]); |
| 4290 | if (pref != ICMPV6_ROUTER_PREF_LOW && |
| 4291 | pref != ICMPV6_ROUTER_PREF_HIGH) |
| 4292 | pref = ICMPV6_ROUTER_PREF_MEDIUM; |
| 4293 | cfg->fc_flags |= RTF_PREF(pref); |
| 4294 | } |
| 4295 | |
Roopa Prabhu | 19e42e4 | 2015-07-21 10:43:48 +0200 | [diff] [blame] | 4296 | if (tb[RTA_ENCAP]) |
| 4297 | cfg->fc_encap = tb[RTA_ENCAP]; |
| 4298 | |
David Ahern | 9ed5959 | 2017-01-17 14:57:36 -0800 | [diff] [blame] | 4299 | if (tb[RTA_ENCAP_TYPE]) { |
Roopa Prabhu | 19e42e4 | 2015-07-21 10:43:48 +0200 | [diff] [blame] | 4300 | cfg->fc_encap_type = nla_get_u16(tb[RTA_ENCAP_TYPE]); |
| 4301 | |
David Ahern | c255bd6 | 2017-05-27 16:19:27 -0600 | [diff] [blame] | 4302 | err = lwtunnel_valid_encap_type(cfg->fc_encap_type, extack); |
David Ahern | 9ed5959 | 2017-01-17 14:57:36 -0800 | [diff] [blame] | 4303 | if (err < 0) |
| 4304 | goto errout; |
| 4305 | } |
| 4306 | |
Xin Long | 32bc201 | 2015-12-16 17:50:11 +0800 | [diff] [blame] | 4307 | if (tb[RTA_EXPIRES]) { |
| 4308 | unsigned long timeout = addrconf_timeout_fixup(nla_get_u32(tb[RTA_EXPIRES]), HZ); |
| 4309 | |
| 4310 | if (addrconf_finite_timeout(timeout)) { |
| 4311 | cfg->fc_expires = jiffies_to_clock_t(timeout * HZ); |
| 4312 | cfg->fc_flags |= RTF_EXPIRES; |
| 4313 | } |
| 4314 | } |
| 4315 | |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4316 | err = 0; |
| 4317 | errout: |
| 4318 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4319 | } |
| 4320 | |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 4321 | struct rt6_nh { |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4322 | struct fib6_info *fib6_info; |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 4323 | struct fib6_config r_cfg; |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 4324 | struct list_head next; |
| 4325 | }; |
| 4326 | |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 4327 | static int ip6_route_info_append(struct net *net, |
| 4328 | struct list_head *rt6_nh_list, |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4329 | struct fib6_info *rt, |
| 4330 | struct fib6_config *r_cfg) |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 4331 | { |
| 4332 | struct rt6_nh *nh; |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 4333 | int err = -EEXIST; |
| 4334 | |
| 4335 | list_for_each_entry(nh, rt6_nh_list, next) { |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4336 | /* check if fib6_info already exists */ |
| 4337 | if (rt6_duplicate_nexthop(nh->fib6_info, rt)) |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 4338 | return err; |
| 4339 | } |
| 4340 | |
| 4341 | nh = kzalloc(sizeof(*nh), GFP_KERNEL); |
| 4342 | if (!nh) |
| 4343 | return -ENOMEM; |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4344 | nh->fib6_info = rt; |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 4345 | memcpy(&nh->r_cfg, r_cfg, sizeof(*r_cfg)); |
| 4346 | list_add_tail(&nh->next, rt6_nh_list); |
| 4347 | |
| 4348 | return 0; |
| 4349 | } |
| 4350 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4351 | static void ip6_route_mpath_notify(struct fib6_info *rt, |
| 4352 | struct fib6_info *rt_last, |
David Ahern | 3b1137f | 2017-02-02 12:37:10 -0800 | [diff] [blame] | 4353 | struct nl_info *info, |
| 4354 | __u16 nlflags) |
| 4355 | { |
| 4356 | /* if this is an APPEND route, then rt points to the first route |
| 4357 | * inserted and rt_last points to last route inserted. Userspace |
| 4358 | * wants a consistent dump of the route which starts at the first |
| 4359 | * nexthop. Since sibling routes are always added at the end of |
| 4360 | * the list, find the first sibling of the last route appended |
| 4361 | */ |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 4362 | if ((nlflags & NLM_F_APPEND) && rt_last && rt_last->fib6_nsiblings) { |
| 4363 | rt = list_first_entry(&rt_last->fib6_siblings, |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4364 | struct fib6_info, |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 4365 | fib6_siblings); |
David Ahern | 3b1137f | 2017-02-02 12:37:10 -0800 | [diff] [blame] | 4366 | } |
| 4367 | |
| 4368 | if (rt) |
| 4369 | inet6_rt_notify(RTM_NEWROUTE, rt, info, nlflags); |
| 4370 | } |
| 4371 | |
David Ahern | 333c430 | 2017-05-21 10:12:04 -0600 | [diff] [blame] | 4372 | static int ip6_route_multipath_add(struct fib6_config *cfg, |
| 4373 | struct netlink_ext_ack *extack) |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 4374 | { |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4375 | struct fib6_info *rt_notif = NULL, *rt_last = NULL; |
David Ahern | 3b1137f | 2017-02-02 12:37:10 -0800 | [diff] [blame] | 4376 | struct nl_info *info = &cfg->fc_nlinfo; |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 4377 | struct fib6_config r_cfg; |
| 4378 | struct rtnexthop *rtnh; |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4379 | struct fib6_info *rt; |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 4380 | struct rt6_nh *err_nh; |
| 4381 | struct rt6_nh *nh, *nh_safe; |
David Ahern | 3b1137f | 2017-02-02 12:37:10 -0800 | [diff] [blame] | 4382 | __u16 nlflags; |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 4383 | int remaining; |
| 4384 | int attrlen; |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 4385 | int err = 1; |
| 4386 | int nhn = 0; |
| 4387 | int replace = (cfg->fc_nlinfo.nlh && |
| 4388 | (cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_REPLACE)); |
| 4389 | LIST_HEAD(rt6_nh_list); |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 4390 | |
David Ahern | 3b1137f | 2017-02-02 12:37:10 -0800 | [diff] [blame] | 4391 | nlflags = replace ? NLM_F_REPLACE : NLM_F_CREATE; |
| 4392 | if (info->nlh && info->nlh->nlmsg_flags & NLM_F_APPEND) |
| 4393 | nlflags |= NLM_F_APPEND; |
| 4394 | |
Michal Kubeček | 35f1b4e | 2015-05-18 20:53:55 +0200 | [diff] [blame] | 4395 | remaining = cfg->fc_mp_len; |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 4396 | rtnh = (struct rtnexthop *)cfg->fc_mp; |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 4397 | |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 4398 | /* Parse a Multipath Entry and build a list (rt6_nh_list) of |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4399 | * fib6_info structs per nexthop |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 4400 | */ |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 4401 | while (rtnh_ok(rtnh, remaining)) { |
| 4402 | memcpy(&r_cfg, cfg, sizeof(*cfg)); |
| 4403 | if (rtnh->rtnh_ifindex) |
| 4404 | r_cfg.fc_ifindex = rtnh->rtnh_ifindex; |
| 4405 | |
| 4406 | attrlen = rtnh_attrlen(rtnh); |
| 4407 | if (attrlen > 0) { |
| 4408 | struct nlattr *nla, *attrs = rtnh_attrs(rtnh); |
| 4409 | |
| 4410 | nla = nla_find(attrs, attrlen, RTA_GATEWAY); |
| 4411 | if (nla) { |
Jiri Benc | 67b61f6 | 2015-03-29 16:59:26 +0200 | [diff] [blame] | 4412 | r_cfg.fc_gateway = nla_get_in6_addr(nla); |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 4413 | r_cfg.fc_flags |= RTF_GATEWAY; |
| 4414 | } |
Roopa Prabhu | 19e42e4 | 2015-07-21 10:43:48 +0200 | [diff] [blame] | 4415 | r_cfg.fc_encap = nla_find(attrs, attrlen, RTA_ENCAP); |
| 4416 | nla = nla_find(attrs, attrlen, RTA_ENCAP_TYPE); |
| 4417 | if (nla) |
| 4418 | r_cfg.fc_encap_type = nla_get_u16(nla); |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 4419 | } |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 4420 | |
David Ahern | 68e2ffd | 2018-03-20 10:06:59 -0700 | [diff] [blame] | 4421 | r_cfg.fc_flags |= (rtnh->rtnh_flags & RTNH_F_ONLINK); |
David Ahern | acb54e3 | 2018-04-17 17:33:22 -0700 | [diff] [blame] | 4422 | rt = ip6_route_info_create(&r_cfg, GFP_KERNEL, extack); |
Roopa Prabhu | 8c5b83f | 2015-10-10 08:26:36 -0700 | [diff] [blame] | 4423 | if (IS_ERR(rt)) { |
| 4424 | err = PTR_ERR(rt); |
| 4425 | rt = NULL; |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 4426 | goto cleanup; |
Roopa Prabhu | 8c5b83f | 2015-10-10 08:26:36 -0700 | [diff] [blame] | 4427 | } |
David Ahern | b5d2d75 | 2018-07-15 09:35:19 -0700 | [diff] [blame] | 4428 | if (!rt6_qualify_for_ecmp(rt)) { |
| 4429 | err = -EINVAL; |
| 4430 | NL_SET_ERR_MSG(extack, |
| 4431 | "Device only routes can not be added for IPv6 using the multipath API."); |
| 4432 | fib6_info_release(rt); |
| 4433 | goto cleanup; |
| 4434 | } |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 4435 | |
David Ahern | ad1601a | 2019-03-27 20:53:56 -0700 | [diff] [blame] | 4436 | rt->fib6_nh.fib_nh_weight = rtnh->rtnh_hops + 1; |
Ido Schimmel | 398958a | 2018-01-09 16:40:28 +0200 | [diff] [blame] | 4437 | |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 4438 | err = ip6_route_info_append(info->nl_net, &rt6_nh_list, |
| 4439 | rt, &r_cfg); |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 4440 | if (err) { |
David Ahern | 93531c6 | 2018-04-17 17:33:25 -0700 | [diff] [blame] | 4441 | fib6_info_release(rt); |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 4442 | goto cleanup; |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 4443 | } |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 4444 | |
| 4445 | rtnh = rtnh_next(rtnh, &remaining); |
| 4446 | } |
| 4447 | |
David Ahern | 3b1137f | 2017-02-02 12:37:10 -0800 | [diff] [blame] | 4448 | /* for add and replace send one notification with all nexthops. |
| 4449 | * Skip the notification in fib6_add_rt2node and send one with |
| 4450 | * the full route when done |
| 4451 | */ |
| 4452 | info->skip_notify = 1; |
| 4453 | |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 4454 | err_nh = NULL; |
| 4455 | list_for_each_entry(nh, &rt6_nh_list, next) { |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4456 | err = __ip6_ins_rt(nh->fib6_info, info, extack); |
| 4457 | fib6_info_release(nh->fib6_info); |
David Ahern | 3b1137f | 2017-02-02 12:37:10 -0800 | [diff] [blame] | 4458 | |
David Ahern | f722517 | 2018-06-04 13:41:42 -0700 | [diff] [blame] | 4459 | if (!err) { |
| 4460 | /* save reference to last route successfully inserted */ |
| 4461 | rt_last = nh->fib6_info; |
| 4462 | |
| 4463 | /* save reference to first route for notification */ |
| 4464 | if (!rt_notif) |
| 4465 | rt_notif = nh->fib6_info; |
| 4466 | } |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 4467 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4468 | /* nh->fib6_info is used or freed at this point, reset to NULL*/ |
| 4469 | nh->fib6_info = NULL; |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 4470 | if (err) { |
| 4471 | if (replace && nhn) |
Jakub Kicinski | a5a82d8 | 2019-01-14 10:52:45 -0800 | [diff] [blame] | 4472 | NL_SET_ERR_MSG_MOD(extack, |
| 4473 | "multipath route replace failed (check consistency of installed routes)"); |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 4474 | err_nh = nh; |
| 4475 | goto add_errout; |
| 4476 | } |
| 4477 | |
Nicolas Dichtel | 1a72418 | 2012-11-01 22:58:22 +0000 | [diff] [blame] | 4478 | /* Because each route is added like a single route we remove |
Michal Kubeček | 2759647 | 2015-05-18 20:54:00 +0200 | [diff] [blame] | 4479 | * these flags after the first nexthop: if there is a collision, |
| 4480 | * we have already failed to add the first nexthop: |
| 4481 | * fib6_add_rt2node() has rejected it; when replacing, old |
| 4482 | * nexthops have been replaced by first new, the rest should |
| 4483 | * be added to it. |
Nicolas Dichtel | 1a72418 | 2012-11-01 22:58:22 +0000 | [diff] [blame] | 4484 | */ |
Michal Kubeček | 2759647 | 2015-05-18 20:54:00 +0200 | [diff] [blame] | 4485 | cfg->fc_nlinfo.nlh->nlmsg_flags &= ~(NLM_F_EXCL | |
| 4486 | NLM_F_REPLACE); |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 4487 | nhn++; |
| 4488 | } |
| 4489 | |
David Ahern | 3b1137f | 2017-02-02 12:37:10 -0800 | [diff] [blame] | 4490 | /* success ... tell user about new route */ |
| 4491 | ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags); |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 4492 | goto cleanup; |
| 4493 | |
| 4494 | add_errout: |
David Ahern | 3b1137f | 2017-02-02 12:37:10 -0800 | [diff] [blame] | 4495 | /* send notification for routes that were added so that |
| 4496 | * the delete notifications sent by ip6_route_del are |
| 4497 | * coherent |
| 4498 | */ |
| 4499 | if (rt_notif) |
| 4500 | ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags); |
| 4501 | |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 4502 | /* Delete routes that were already added */ |
| 4503 | list_for_each_entry(nh, &rt6_nh_list, next) { |
| 4504 | if (err_nh == nh) |
| 4505 | break; |
David Ahern | 333c430 | 2017-05-21 10:12:04 -0600 | [diff] [blame] | 4506 | ip6_route_del(&nh->r_cfg, extack); |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 4507 | } |
| 4508 | |
| 4509 | cleanup: |
| 4510 | list_for_each_entry_safe(nh, nh_safe, &rt6_nh_list, next) { |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4511 | if (nh->fib6_info) |
| 4512 | fib6_info_release(nh->fib6_info); |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 4513 | list_del(&nh->next); |
| 4514 | kfree(nh); |
| 4515 | } |
| 4516 | |
| 4517 | return err; |
| 4518 | } |
| 4519 | |
David Ahern | 333c430 | 2017-05-21 10:12:04 -0600 | [diff] [blame] | 4520 | static int ip6_route_multipath_del(struct fib6_config *cfg, |
| 4521 | struct netlink_ext_ack *extack) |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 4522 | { |
| 4523 | struct fib6_config r_cfg; |
| 4524 | struct rtnexthop *rtnh; |
| 4525 | int remaining; |
| 4526 | int attrlen; |
| 4527 | int err = 1, last_err = 0; |
| 4528 | |
| 4529 | remaining = cfg->fc_mp_len; |
| 4530 | rtnh = (struct rtnexthop *)cfg->fc_mp; |
| 4531 | |
| 4532 | /* Parse a Multipath Entry */ |
| 4533 | while (rtnh_ok(rtnh, remaining)) { |
| 4534 | memcpy(&r_cfg, cfg, sizeof(*cfg)); |
| 4535 | if (rtnh->rtnh_ifindex) |
| 4536 | r_cfg.fc_ifindex = rtnh->rtnh_ifindex; |
| 4537 | |
| 4538 | attrlen = rtnh_attrlen(rtnh); |
| 4539 | if (attrlen > 0) { |
| 4540 | struct nlattr *nla, *attrs = rtnh_attrs(rtnh); |
| 4541 | |
| 4542 | nla = nla_find(attrs, attrlen, RTA_GATEWAY); |
| 4543 | if (nla) { |
| 4544 | nla_memcpy(&r_cfg.fc_gateway, nla, 16); |
| 4545 | r_cfg.fc_flags |= RTF_GATEWAY; |
| 4546 | } |
| 4547 | } |
David Ahern | 333c430 | 2017-05-21 10:12:04 -0600 | [diff] [blame] | 4548 | err = ip6_route_del(&r_cfg, extack); |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 4549 | if (err) |
| 4550 | last_err = err; |
| 4551 | |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 4552 | rtnh = rtnh_next(rtnh, &remaining); |
| 4553 | } |
| 4554 | |
| 4555 | return last_err; |
| 4556 | } |
| 4557 | |
David Ahern | c21ef3e | 2017-04-16 09:48:24 -0700 | [diff] [blame] | 4558 | static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh, |
| 4559 | struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4560 | { |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4561 | struct fib6_config cfg; |
| 4562 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4563 | |
David Ahern | 333c430 | 2017-05-21 10:12:04 -0600 | [diff] [blame] | 4564 | err = rtm_to_fib6_config(skb, nlh, &cfg, extack); |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4565 | if (err < 0) |
| 4566 | return err; |
| 4567 | |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 4568 | if (cfg.fc_mp) |
David Ahern | 333c430 | 2017-05-21 10:12:04 -0600 | [diff] [blame] | 4569 | return ip6_route_multipath_del(&cfg, extack); |
David Ahern | 0ae8133 | 2017-02-02 12:37:08 -0800 | [diff] [blame] | 4570 | else { |
| 4571 | cfg.fc_delete_all_nh = 1; |
David Ahern | 333c430 | 2017-05-21 10:12:04 -0600 | [diff] [blame] | 4572 | return ip6_route_del(&cfg, extack); |
David Ahern | 0ae8133 | 2017-02-02 12:37:08 -0800 | [diff] [blame] | 4573 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4574 | } |
| 4575 | |
David Ahern | c21ef3e | 2017-04-16 09:48:24 -0700 | [diff] [blame] | 4576 | static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh, |
| 4577 | struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4578 | { |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4579 | struct fib6_config cfg; |
| 4580 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4581 | |
David Ahern | 333c430 | 2017-05-21 10:12:04 -0600 | [diff] [blame] | 4582 | err = rtm_to_fib6_config(skb, nlh, &cfg, extack); |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4583 | if (err < 0) |
| 4584 | return err; |
| 4585 | |
David Ahern | 67f6951 | 2019-03-21 05:21:34 -0700 | [diff] [blame] | 4586 | if (cfg.fc_metric == 0) |
| 4587 | cfg.fc_metric = IP6_RT_PRIO_USER; |
| 4588 | |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 4589 | if (cfg.fc_mp) |
David Ahern | 333c430 | 2017-05-21 10:12:04 -0600 | [diff] [blame] | 4590 | return ip6_route_multipath_add(&cfg, extack); |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 4591 | else |
David Ahern | acb54e3 | 2018-04-17 17:33:22 -0700 | [diff] [blame] | 4592 | return ip6_route_add(&cfg, GFP_KERNEL, extack); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4593 | } |
| 4594 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4595 | static size_t rt6_nlmsg_size(struct fib6_info *rt) |
Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 4596 | { |
David Ahern | beb1afac5 | 2017-02-02 12:37:09 -0800 | [diff] [blame] | 4597 | int nexthop_len = 0; |
| 4598 | |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 4599 | if (rt->fib6_nsiblings) { |
David Ahern | beb1afac5 | 2017-02-02 12:37:09 -0800 | [diff] [blame] | 4600 | nexthop_len = nla_total_size(0) /* RTA_MULTIPATH */ |
| 4601 | + NLA_ALIGN(sizeof(struct rtnexthop)) |
| 4602 | + nla_total_size(16) /* RTA_GATEWAY */ |
David Ahern | ad1601a | 2019-03-27 20:53:56 -0700 | [diff] [blame] | 4603 | + lwtunnel_get_encap_size(rt->fib6_nh.fib_nh_lws); |
David Ahern | beb1afac5 | 2017-02-02 12:37:09 -0800 | [diff] [blame] | 4604 | |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 4605 | nexthop_len *= rt->fib6_nsiblings; |
David Ahern | beb1afac5 | 2017-02-02 12:37:09 -0800 | [diff] [blame] | 4606 | } |
| 4607 | |
Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 4608 | return NLMSG_ALIGN(sizeof(struct rtmsg)) |
| 4609 | + nla_total_size(16) /* RTA_SRC */ |
| 4610 | + nla_total_size(16) /* RTA_DST */ |
| 4611 | + nla_total_size(16) /* RTA_GATEWAY */ |
| 4612 | + nla_total_size(16) /* RTA_PREFSRC */ |
| 4613 | + nla_total_size(4) /* RTA_TABLE */ |
| 4614 | + nla_total_size(4) /* RTA_IIF */ |
| 4615 | + nla_total_size(4) /* RTA_OIF */ |
| 4616 | + nla_total_size(4) /* RTA_PRIORITY */ |
Noriaki TAKAMIYA | 6a2b9ce | 2007-01-23 22:09:41 -0800 | [diff] [blame] | 4617 | + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */ |
Daniel Borkmann | ea69763 | 2015-01-05 23:57:47 +0100 | [diff] [blame] | 4618 | + nla_total_size(sizeof(struct rta_cacheinfo)) |
Lubomir Rintel | c78ba6d | 2015-03-11 15:39:21 +0100 | [diff] [blame] | 4619 | + nla_total_size(TCP_CA_NAME_MAX) /* RTAX_CC_ALGO */ |
Roopa Prabhu | 19e42e4 | 2015-07-21 10:43:48 +0200 | [diff] [blame] | 4620 | + nla_total_size(1) /* RTA_PREF */ |
David Ahern | ad1601a | 2019-03-27 20:53:56 -0700 | [diff] [blame] | 4621 | + lwtunnel_get_encap_size(rt->fib6_nh.fib_nh_lws) |
David Ahern | beb1afac5 | 2017-02-02 12:37:09 -0800 | [diff] [blame] | 4622 | + nexthop_len; |
| 4623 | } |
| 4624 | |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 4625 | static int rt6_fill_node(struct net *net, struct sk_buff *skb, |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4626 | struct fib6_info *rt, struct dst_entry *dst, |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 4627 | struct in6_addr *dest, struct in6_addr *src, |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 4628 | int iif, int type, u32 portid, u32 seq, |
David Ahern | f8cfe2c | 2017-01-17 15:51:08 -0800 | [diff] [blame] | 4629 | unsigned int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4630 | { |
Xin Long | 22d0bd8 | 2018-09-11 14:33:58 +0800 | [diff] [blame] | 4631 | struct rt6_info *rt6 = (struct rt6_info *)dst; |
| 4632 | struct rt6key *rt6_dst, *rt6_src; |
| 4633 | u32 *pmetrics, table, rt6_flags; |
Thomas Graf | 2d7202b | 2006-08-22 00:01:27 -0700 | [diff] [blame] | 4634 | struct nlmsghdr *nlh; |
Xin Long | 22d0bd8 | 2018-09-11 14:33:58 +0800 | [diff] [blame] | 4635 | struct rtmsg *rtm; |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 4636 | long expires = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4637 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 4638 | nlh = nlmsg_put(skb, portid, seq, type, sizeof(*rtm), flags); |
David S. Miller | 3830847 | 2011-12-03 18:02:47 -0500 | [diff] [blame] | 4639 | if (!nlh) |
Patrick McHardy | 2693256 | 2007-01-31 23:16:40 -0800 | [diff] [blame] | 4640 | return -EMSGSIZE; |
Thomas Graf | 2d7202b | 2006-08-22 00:01:27 -0700 | [diff] [blame] | 4641 | |
Xin Long | 22d0bd8 | 2018-09-11 14:33:58 +0800 | [diff] [blame] | 4642 | if (rt6) { |
| 4643 | rt6_dst = &rt6->rt6i_dst; |
| 4644 | rt6_src = &rt6->rt6i_src; |
| 4645 | rt6_flags = rt6->rt6i_flags; |
| 4646 | } else { |
| 4647 | rt6_dst = &rt->fib6_dst; |
| 4648 | rt6_src = &rt->fib6_src; |
| 4649 | rt6_flags = rt->fib6_flags; |
| 4650 | } |
| 4651 | |
Thomas Graf | 2d7202b | 2006-08-22 00:01:27 -0700 | [diff] [blame] | 4652 | rtm = nlmsg_data(nlh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4653 | rtm->rtm_family = AF_INET6; |
Xin Long | 22d0bd8 | 2018-09-11 14:33:58 +0800 | [diff] [blame] | 4654 | rtm->rtm_dst_len = rt6_dst->plen; |
| 4655 | rtm->rtm_src_len = rt6_src->plen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4656 | rtm->rtm_tos = 0; |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 4657 | if (rt->fib6_table) |
| 4658 | table = rt->fib6_table->tb6_id; |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 4659 | else |
Patrick McHardy | 9e762a4 | 2006-08-10 23:09:48 -0700 | [diff] [blame] | 4660 | table = RT6_TABLE_UNSPEC; |
Kalash Nainwal | 97f0082 | 2019-02-20 16:23:04 -0800 | [diff] [blame] | 4661 | rtm->rtm_table = table < 256 ? table : RT_TABLE_COMPAT; |
David S. Miller | c78679e | 2012-04-01 20:27:33 -0400 | [diff] [blame] | 4662 | if (nla_put_u32(skb, RTA_TABLE, table)) |
| 4663 | goto nla_put_failure; |
David Ahern | e8478e8 | 2018-04-17 17:33:13 -0700 | [diff] [blame] | 4664 | |
| 4665 | rtm->rtm_type = rt->fib6_type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4666 | rtm->rtm_flags = 0; |
| 4667 | rtm->rtm_scope = RT_SCOPE_UNIVERSE; |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 4668 | rtm->rtm_protocol = rt->fib6_protocol; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4669 | |
Xin Long | 22d0bd8 | 2018-09-11 14:33:58 +0800 | [diff] [blame] | 4670 | if (rt6_flags & RTF_CACHE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4671 | rtm->rtm_flags |= RTM_F_CLONED; |
| 4672 | |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 4673 | if (dest) { |
| 4674 | if (nla_put_in6_addr(skb, RTA_DST, dest)) |
David S. Miller | c78679e | 2012-04-01 20:27:33 -0400 | [diff] [blame] | 4675 | goto nla_put_failure; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 4676 | rtm->rtm_dst_len = 128; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4677 | } else if (rtm->rtm_dst_len) |
Xin Long | 22d0bd8 | 2018-09-11 14:33:58 +0800 | [diff] [blame] | 4678 | if (nla_put_in6_addr(skb, RTA_DST, &rt6_dst->addr)) |
David S. Miller | c78679e | 2012-04-01 20:27:33 -0400 | [diff] [blame] | 4679 | goto nla_put_failure; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4680 | #ifdef CONFIG_IPV6_SUBTREES |
| 4681 | if (src) { |
Jiri Benc | 930345e | 2015-03-29 16:59:25 +0200 | [diff] [blame] | 4682 | if (nla_put_in6_addr(skb, RTA_SRC, src)) |
David S. Miller | c78679e | 2012-04-01 20:27:33 -0400 | [diff] [blame] | 4683 | goto nla_put_failure; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 4684 | rtm->rtm_src_len = 128; |
David S. Miller | c78679e | 2012-04-01 20:27:33 -0400 | [diff] [blame] | 4685 | } else if (rtm->rtm_src_len && |
Xin Long | 22d0bd8 | 2018-09-11 14:33:58 +0800 | [diff] [blame] | 4686 | nla_put_in6_addr(skb, RTA_SRC, &rt6_src->addr)) |
David S. Miller | c78679e | 2012-04-01 20:27:33 -0400 | [diff] [blame] | 4687 | goto nla_put_failure; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4688 | #endif |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 4689 | if (iif) { |
| 4690 | #ifdef CONFIG_IPV6_MROUTE |
Xin Long | 22d0bd8 | 2018-09-11 14:33:58 +0800 | [diff] [blame] | 4691 | if (ipv6_addr_is_multicast(&rt6_dst->addr)) { |
David Ahern | fd61c6b | 2017-01-17 15:51:07 -0800 | [diff] [blame] | 4692 | int err = ip6mr_get_route(net, skb, rtm, portid); |
Nikolay Aleksandrov | 2cf7507 | 2016-09-25 23:08:31 +0200 | [diff] [blame] | 4693 | |
David Ahern | fd61c6b | 2017-01-17 15:51:07 -0800 | [diff] [blame] | 4694 | if (err == 0) |
| 4695 | return 0; |
| 4696 | if (err < 0) |
| 4697 | goto nla_put_failure; |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 4698 | } else |
| 4699 | #endif |
David S. Miller | c78679e | 2012-04-01 20:27:33 -0400 | [diff] [blame] | 4700 | if (nla_put_u32(skb, RTA_IIF, iif)) |
| 4701 | goto nla_put_failure; |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 4702 | } else if (dest) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4703 | struct in6_addr saddr_buf; |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 4704 | if (ip6_route_get_saddr(net, rt, dest, 0, &saddr_buf) == 0 && |
Jiri Benc | 930345e | 2015-03-29 16:59:25 +0200 | [diff] [blame] | 4705 | nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf)) |
David S. Miller | c78679e | 2012-04-01 20:27:33 -0400 | [diff] [blame] | 4706 | goto nla_put_failure; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4707 | } |
Thomas Graf | 2d7202b | 2006-08-22 00:01:27 -0700 | [diff] [blame] | 4708 | |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 4709 | if (rt->fib6_prefsrc.plen) { |
Daniel Walter | c3968a8 | 2011-04-13 21:10:57 +0000 | [diff] [blame] | 4710 | struct in6_addr saddr_buf; |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 4711 | saddr_buf = rt->fib6_prefsrc.addr; |
Jiri Benc | 930345e | 2015-03-29 16:59:25 +0200 | [diff] [blame] | 4712 | if (nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf)) |
David S. Miller | c78679e | 2012-04-01 20:27:33 -0400 | [diff] [blame] | 4713 | goto nla_put_failure; |
Daniel Walter | c3968a8 | 2011-04-13 21:10:57 +0000 | [diff] [blame] | 4714 | } |
| 4715 | |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 4716 | pmetrics = dst ? dst_metrics_ptr(dst) : rt->fib6_metrics->metrics; |
| 4717 | if (rtnetlink_put_metrics(skb, pmetrics) < 0) |
Thomas Graf | 2d7202b | 2006-08-22 00:01:27 -0700 | [diff] [blame] | 4718 | goto nla_put_failure; |
| 4719 | |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 4720 | if (nla_put_u32(skb, RTA_PRIORITY, rt->fib6_metric)) |
David S. Miller | c78679e | 2012-04-01 20:27:33 -0400 | [diff] [blame] | 4721 | goto nla_put_failure; |
Li Wei | 8253947 | 2012-07-29 16:01:30 +0000 | [diff] [blame] | 4722 | |
David Ahern | beb1afac5 | 2017-02-02 12:37:09 -0800 | [diff] [blame] | 4723 | /* For multipath routes, walk the siblings list and add |
| 4724 | * each as a nexthop within RTA_MULTIPATH. |
| 4725 | */ |
Xin Long | 22d0bd8 | 2018-09-11 14:33:58 +0800 | [diff] [blame] | 4726 | if (rt6) { |
| 4727 | if (rt6_flags & RTF_GATEWAY && |
| 4728 | nla_put_in6_addr(skb, RTA_GATEWAY, &rt6->rt6i_gateway)) |
| 4729 | goto nla_put_failure; |
| 4730 | |
| 4731 | if (dst->dev && nla_put_u32(skb, RTA_OIF, dst->dev->ifindex)) |
| 4732 | goto nla_put_failure; |
| 4733 | } else if (rt->fib6_nsiblings) { |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4734 | struct fib6_info *sibling, *next_sibling; |
David Ahern | beb1afac5 | 2017-02-02 12:37:09 -0800 | [diff] [blame] | 4735 | struct nlattr *mp; |
| 4736 | |
| 4737 | mp = nla_nest_start(skb, RTA_MULTIPATH); |
| 4738 | if (!mp) |
| 4739 | goto nla_put_failure; |
| 4740 | |
David Ahern | c0a7207 | 2019-04-02 14:11:58 -0700 | [diff] [blame] | 4741 | if (fib_add_nexthop(skb, &rt->fib6_nh.nh_common, |
| 4742 | rt->fib6_nh.fib_nh_weight) < 0) |
David Ahern | beb1afac5 | 2017-02-02 12:37:09 -0800 | [diff] [blame] | 4743 | goto nla_put_failure; |
| 4744 | |
| 4745 | list_for_each_entry_safe(sibling, next_sibling, |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 4746 | &rt->fib6_siblings, fib6_siblings) { |
David Ahern | c0a7207 | 2019-04-02 14:11:58 -0700 | [diff] [blame] | 4747 | if (fib_add_nexthop(skb, &sibling->fib6_nh.nh_common, |
| 4748 | sibling->fib6_nh.fib_nh_weight) < 0) |
David Ahern | beb1afac5 | 2017-02-02 12:37:09 -0800 | [diff] [blame] | 4749 | goto nla_put_failure; |
| 4750 | } |
| 4751 | |
| 4752 | nla_nest_end(skb, mp); |
| 4753 | } else { |
David Ahern | c0a7207 | 2019-04-02 14:11:58 -0700 | [diff] [blame] | 4754 | if (fib_nexthop_info(skb, &rt->fib6_nh.nh_common, |
| 4755 | &rtm->rtm_flags, false) < 0) |
David Ahern | beb1afac5 | 2017-02-02 12:37:09 -0800 | [diff] [blame] | 4756 | goto nla_put_failure; |
| 4757 | } |
| 4758 | |
Xin Long | 22d0bd8 | 2018-09-11 14:33:58 +0800 | [diff] [blame] | 4759 | if (rt6_flags & RTF_EXPIRES) { |
David Ahern | 1489568 | 2018-04-17 17:33:17 -0700 | [diff] [blame] | 4760 | expires = dst ? dst->expires : rt->expires; |
| 4761 | expires -= jiffies; |
| 4762 | } |
YOSHIFUJI Hideaki | 69cdf8f | 2008-05-19 16:55:13 -0700 | [diff] [blame] | 4763 | |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 4764 | 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] | 4765 | goto nla_put_failure; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4766 | |
Xin Long | 22d0bd8 | 2018-09-11 14:33:58 +0800 | [diff] [blame] | 4767 | if (nla_put_u8(skb, RTA_PREF, IPV6_EXTRACT_PREF(rt6_flags))) |
Lubomir Rintel | c78ba6d | 2015-03-11 15:39:21 +0100 | [diff] [blame] | 4768 | goto nla_put_failure; |
| 4769 | |
Roopa Prabhu | 19e42e4 | 2015-07-21 10:43:48 +0200 | [diff] [blame] | 4770 | |
Johannes Berg | 053c095 | 2015-01-16 22:09:00 +0100 | [diff] [blame] | 4771 | nlmsg_end(skb, nlh); |
| 4772 | return 0; |
Thomas Graf | 2d7202b | 2006-08-22 00:01:27 -0700 | [diff] [blame] | 4773 | |
| 4774 | nla_put_failure: |
Patrick McHardy | 2693256 | 2007-01-31 23:16:40 -0800 | [diff] [blame] | 4775 | nlmsg_cancel(skb, nlh); |
| 4776 | return -EMSGSIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4777 | } |
| 4778 | |
David Ahern | 13e3890 | 2018-10-15 18:56:44 -0700 | [diff] [blame] | 4779 | static bool fib6_info_uses_dev(const struct fib6_info *f6i, |
| 4780 | const struct net_device *dev) |
| 4781 | { |
David Ahern | ad1601a | 2019-03-27 20:53:56 -0700 | [diff] [blame] | 4782 | if (f6i->fib6_nh.fib_nh_dev == dev) |
David Ahern | 13e3890 | 2018-10-15 18:56:44 -0700 | [diff] [blame] | 4783 | return true; |
| 4784 | |
| 4785 | if (f6i->fib6_nsiblings) { |
| 4786 | struct fib6_info *sibling, *next_sibling; |
| 4787 | |
| 4788 | list_for_each_entry_safe(sibling, next_sibling, |
| 4789 | &f6i->fib6_siblings, fib6_siblings) { |
David Ahern | ad1601a | 2019-03-27 20:53:56 -0700 | [diff] [blame] | 4790 | if (sibling->fib6_nh.fib_nh_dev == dev) |
David Ahern | 13e3890 | 2018-10-15 18:56:44 -0700 | [diff] [blame] | 4791 | return true; |
| 4792 | } |
| 4793 | } |
| 4794 | |
| 4795 | return false; |
| 4796 | } |
| 4797 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4798 | int rt6_dump_route(struct fib6_info *rt, void *p_arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4799 | { |
| 4800 | 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] | 4801 | struct fib_dump_filter *filter = &arg->filter; |
| 4802 | unsigned int flags = NLM_F_MULTI; |
David Ahern | 1f17e2f | 2017-01-26 13:54:08 -0800 | [diff] [blame] | 4803 | struct net *net = arg->net; |
| 4804 | |
David Ahern | 421842e | 2018-04-17 17:33:18 -0700 | [diff] [blame] | 4805 | if (rt == net->ipv6.fib6_null_entry) |
David Ahern | 1f17e2f | 2017-01-26 13:54:08 -0800 | [diff] [blame] | 4806 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4807 | |
David Ahern | 13e3890 | 2018-10-15 18:56:44 -0700 | [diff] [blame] | 4808 | if ((filter->flags & RTM_F_PREFIX) && |
| 4809 | !(rt->fib6_flags & RTF_PREFIX_RT)) { |
| 4810 | /* success since this is not a prefix route */ |
| 4811 | return 1; |
| 4812 | } |
| 4813 | if (filter->filter_set) { |
| 4814 | if ((filter->rt_type && rt->fib6_type != filter->rt_type) || |
| 4815 | (filter->dev && !fib6_info_uses_dev(rt, filter->dev)) || |
| 4816 | (filter->protocol && rt->fib6_protocol != filter->protocol)) { |
David Ahern | f8cfe2c | 2017-01-17 15:51:08 -0800 | [diff] [blame] | 4817 | return 1; |
| 4818 | } |
David Ahern | 13e3890 | 2018-10-15 18:56:44 -0700 | [diff] [blame] | 4819 | flags |= NLM_F_DUMP_FILTERED; |
David Ahern | f8cfe2c | 2017-01-17 15:51:08 -0800 | [diff] [blame] | 4820 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4821 | |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 4822 | return rt6_fill_node(net, arg->skb, rt, NULL, NULL, NULL, 0, |
| 4823 | RTM_NEWROUTE, NETLINK_CB(arg->cb->skb).portid, |
David Ahern | 13e3890 | 2018-10-15 18:56:44 -0700 | [diff] [blame] | 4824 | arg->cb->nlh->nlmsg_seq, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4825 | } |
| 4826 | |
Jakub Kicinski | 0eff0a2 | 2019-01-18 10:46:24 -0800 | [diff] [blame] | 4827 | static int inet6_rtm_valid_getroute_req(struct sk_buff *skb, |
| 4828 | const struct nlmsghdr *nlh, |
| 4829 | struct nlattr **tb, |
| 4830 | struct netlink_ext_ack *extack) |
| 4831 | { |
| 4832 | struct rtmsg *rtm; |
| 4833 | int i, err; |
| 4834 | |
| 4835 | if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*rtm))) { |
| 4836 | NL_SET_ERR_MSG_MOD(extack, |
| 4837 | "Invalid header for get route request"); |
| 4838 | return -EINVAL; |
| 4839 | } |
| 4840 | |
| 4841 | if (!netlink_strict_get_check(skb)) |
| 4842 | return nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, |
| 4843 | rtm_ipv6_policy, extack); |
| 4844 | |
| 4845 | rtm = nlmsg_data(nlh); |
| 4846 | if ((rtm->rtm_src_len && rtm->rtm_src_len != 128) || |
| 4847 | (rtm->rtm_dst_len && rtm->rtm_dst_len != 128) || |
| 4848 | rtm->rtm_table || rtm->rtm_protocol || rtm->rtm_scope || |
| 4849 | rtm->rtm_type) { |
| 4850 | NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for get route request"); |
| 4851 | return -EINVAL; |
| 4852 | } |
| 4853 | if (rtm->rtm_flags & ~RTM_F_FIB_MATCH) { |
| 4854 | NL_SET_ERR_MSG_MOD(extack, |
| 4855 | "Invalid flags for get route request"); |
| 4856 | return -EINVAL; |
| 4857 | } |
| 4858 | |
| 4859 | err = nlmsg_parse_strict(nlh, sizeof(*rtm), tb, RTA_MAX, |
| 4860 | rtm_ipv6_policy, extack); |
| 4861 | if (err) |
| 4862 | return err; |
| 4863 | |
| 4864 | if ((tb[RTA_SRC] && !rtm->rtm_src_len) || |
| 4865 | (tb[RTA_DST] && !rtm->rtm_dst_len)) { |
| 4866 | NL_SET_ERR_MSG_MOD(extack, "rtm_src_len and rtm_dst_len must be 128 for IPv6"); |
| 4867 | return -EINVAL; |
| 4868 | } |
| 4869 | |
| 4870 | for (i = 0; i <= RTA_MAX; i++) { |
| 4871 | if (!tb[i]) |
| 4872 | continue; |
| 4873 | |
| 4874 | switch (i) { |
| 4875 | case RTA_SRC: |
| 4876 | case RTA_DST: |
| 4877 | case RTA_IIF: |
| 4878 | case RTA_OIF: |
| 4879 | case RTA_MARK: |
| 4880 | case RTA_UID: |
| 4881 | case RTA_SPORT: |
| 4882 | case RTA_DPORT: |
| 4883 | case RTA_IP_PROTO: |
| 4884 | break; |
| 4885 | default: |
| 4886 | NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in get route request"); |
| 4887 | return -EINVAL; |
| 4888 | } |
| 4889 | } |
| 4890 | |
| 4891 | return 0; |
| 4892 | } |
| 4893 | |
David Ahern | c21ef3e | 2017-04-16 09:48:24 -0700 | [diff] [blame] | 4894 | static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh, |
| 4895 | struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4896 | { |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 4897 | struct net *net = sock_net(in_skb->sk); |
Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 4898 | struct nlattr *tb[RTA_MAX+1]; |
Roopa Prabhu | 18c3a61 | 2017-05-25 10:42:40 -0700 | [diff] [blame] | 4899 | int err, iif = 0, oif = 0; |
David Ahern | a68886a | 2018-04-20 15:38:02 -0700 | [diff] [blame] | 4900 | struct fib6_info *from; |
Roopa Prabhu | 18c3a61 | 2017-05-25 10:42:40 -0700 | [diff] [blame] | 4901 | struct dst_entry *dst; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4902 | struct rt6_info *rt; |
Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 4903 | struct sk_buff *skb; |
| 4904 | struct rtmsg *rtm; |
Maciej Żenczykowski | 744486d | 2018-09-29 23:44:54 -0700 | [diff] [blame] | 4905 | struct flowi6 fl6 = {}; |
Roopa Prabhu | 18c3a61 | 2017-05-25 10:42:40 -0700 | [diff] [blame] | 4906 | bool fibmatch; |
Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 4907 | |
Jakub Kicinski | 0eff0a2 | 2019-01-18 10:46:24 -0800 | [diff] [blame] | 4908 | err = inet6_rtm_valid_getroute_req(in_skb, nlh, tb, extack); |
Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 4909 | if (err < 0) |
| 4910 | goto errout; |
| 4911 | |
| 4912 | err = -EINVAL; |
Hannes Frederic Sowa | 38b7097 | 2016-06-11 20:08:19 +0200 | [diff] [blame] | 4913 | rtm = nlmsg_data(nlh); |
| 4914 | fl6.flowlabel = ip6_make_flowinfo(rtm->rtm_tos, 0); |
Roopa Prabhu | 18c3a61 | 2017-05-25 10:42:40 -0700 | [diff] [blame] | 4915 | fibmatch = !!(rtm->rtm_flags & RTM_F_FIB_MATCH); |
Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 4916 | |
| 4917 | if (tb[RTA_SRC]) { |
| 4918 | if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr)) |
| 4919 | goto errout; |
| 4920 | |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 4921 | fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]); |
Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 4922 | } |
| 4923 | |
| 4924 | if (tb[RTA_DST]) { |
| 4925 | if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr)) |
| 4926 | goto errout; |
| 4927 | |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 4928 | fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]); |
Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 4929 | } |
| 4930 | |
| 4931 | if (tb[RTA_IIF]) |
| 4932 | iif = nla_get_u32(tb[RTA_IIF]); |
| 4933 | |
| 4934 | if (tb[RTA_OIF]) |
Shmulik Ladkani | 72331bc | 2012-04-01 04:03:45 +0000 | [diff] [blame] | 4935 | oif = nla_get_u32(tb[RTA_OIF]); |
Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 4936 | |
Lorenzo Colitti | 2e47b29 | 2014-05-15 16:38:41 -0700 | [diff] [blame] | 4937 | if (tb[RTA_MARK]) |
| 4938 | fl6.flowi6_mark = nla_get_u32(tb[RTA_MARK]); |
| 4939 | |
Lorenzo Colitti | 622ec2c | 2016-11-04 02:23:42 +0900 | [diff] [blame] | 4940 | if (tb[RTA_UID]) |
| 4941 | fl6.flowi6_uid = make_kuid(current_user_ns(), |
| 4942 | nla_get_u32(tb[RTA_UID])); |
| 4943 | else |
| 4944 | fl6.flowi6_uid = iif ? INVALID_UID : current_uid(); |
| 4945 | |
Roopa Prabhu | eacb938 | 2018-05-22 14:03:28 -0700 | [diff] [blame] | 4946 | if (tb[RTA_SPORT]) |
| 4947 | fl6.fl6_sport = nla_get_be16(tb[RTA_SPORT]); |
| 4948 | |
| 4949 | if (tb[RTA_DPORT]) |
| 4950 | fl6.fl6_dport = nla_get_be16(tb[RTA_DPORT]); |
| 4951 | |
| 4952 | if (tb[RTA_IP_PROTO]) { |
| 4953 | err = rtm_getroute_parse_ip_proto(tb[RTA_IP_PROTO], |
Hangbin Liu | 5e1a99e | 2019-02-27 16:15:29 +0800 | [diff] [blame] | 4954 | &fl6.flowi6_proto, AF_INET6, |
| 4955 | extack); |
Roopa Prabhu | eacb938 | 2018-05-22 14:03:28 -0700 | [diff] [blame] | 4956 | if (err) |
| 4957 | goto errout; |
| 4958 | } |
| 4959 | |
Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 4960 | if (iif) { |
| 4961 | struct net_device *dev; |
Shmulik Ladkani | 72331bc | 2012-04-01 04:03:45 +0000 | [diff] [blame] | 4962 | int flags = 0; |
| 4963 | |
Florian Westphal | 121622d | 2017-08-15 16:34:42 +0200 | [diff] [blame] | 4964 | rcu_read_lock(); |
| 4965 | |
| 4966 | dev = dev_get_by_index_rcu(net, iif); |
Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 4967 | if (!dev) { |
Florian Westphal | 121622d | 2017-08-15 16:34:42 +0200 | [diff] [blame] | 4968 | rcu_read_unlock(); |
Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 4969 | err = -ENODEV; |
| 4970 | goto errout; |
| 4971 | } |
Shmulik Ladkani | 72331bc | 2012-04-01 04:03:45 +0000 | [diff] [blame] | 4972 | |
| 4973 | fl6.flowi6_iif = iif; |
| 4974 | |
| 4975 | if (!ipv6_addr_any(&fl6.saddr)) |
| 4976 | flags |= RT6_LOOKUP_F_HAS_SADDR; |
| 4977 | |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 4978 | dst = ip6_route_input_lookup(net, dev, &fl6, NULL, flags); |
Florian Westphal | 121622d | 2017-08-15 16:34:42 +0200 | [diff] [blame] | 4979 | |
| 4980 | rcu_read_unlock(); |
Shmulik Ladkani | 72331bc | 2012-04-01 04:03:45 +0000 | [diff] [blame] | 4981 | } else { |
| 4982 | fl6.flowi6_oif = oif; |
| 4983 | |
Ido Schimmel | 58acfd7 | 2017-12-20 12:28:25 +0200 | [diff] [blame] | 4984 | dst = ip6_route_output(net, NULL, &fl6); |
Roopa Prabhu | 18c3a61 | 2017-05-25 10:42:40 -0700 | [diff] [blame] | 4985 | } |
| 4986 | |
Roopa Prabhu | 18c3a61 | 2017-05-25 10:42:40 -0700 | [diff] [blame] | 4987 | |
| 4988 | rt = container_of(dst, struct rt6_info, dst); |
| 4989 | if (rt->dst.error) { |
| 4990 | err = rt->dst.error; |
| 4991 | ip6_rt_put(rt); |
| 4992 | goto errout; |
Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 4993 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4994 | |
WANG Cong | 9d6acb3 | 2017-03-01 20:48:39 -0800 | [diff] [blame] | 4995 | if (rt == net->ipv6.ip6_null_entry) { |
| 4996 | err = rt->dst.error; |
| 4997 | ip6_rt_put(rt); |
| 4998 | goto errout; |
| 4999 | } |
| 5000 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5001 | skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); |
David S. Miller | 3830847 | 2011-12-03 18:02:47 -0500 | [diff] [blame] | 5002 | if (!skb) { |
Amerigo Wang | 94e187c | 2012-10-29 00:13:19 +0000 | [diff] [blame] | 5003 | ip6_rt_put(rt); |
Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 5004 | err = -ENOBUFS; |
| 5005 | goto errout; |
| 5006 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5007 | |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 5008 | skb_dst_set(skb, &rt->dst); |
David Ahern | a68886a | 2018-04-20 15:38:02 -0700 | [diff] [blame] | 5009 | |
| 5010 | rcu_read_lock(); |
| 5011 | from = rcu_dereference(rt->from); |
| 5012 | |
Roopa Prabhu | 18c3a61 | 2017-05-25 10:42:40 -0700 | [diff] [blame] | 5013 | if (fibmatch) |
David Ahern | a68886a | 2018-04-20 15:38:02 -0700 | [diff] [blame] | 5014 | err = rt6_fill_node(net, skb, from, NULL, NULL, NULL, iif, |
Roopa Prabhu | 18c3a61 | 2017-05-25 10:42:40 -0700 | [diff] [blame] | 5015 | RTM_NEWROUTE, NETLINK_CB(in_skb).portid, |
| 5016 | nlh->nlmsg_seq, 0); |
| 5017 | else |
David Ahern | a68886a | 2018-04-20 15:38:02 -0700 | [diff] [blame] | 5018 | err = rt6_fill_node(net, skb, from, dst, &fl6.daddr, |
| 5019 | &fl6.saddr, iif, RTM_NEWROUTE, |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 5020 | NETLINK_CB(in_skb).portid, nlh->nlmsg_seq, |
| 5021 | 0); |
David Ahern | a68886a | 2018-04-20 15:38:02 -0700 | [diff] [blame] | 5022 | rcu_read_unlock(); |
| 5023 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5024 | if (err < 0) { |
Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 5025 | kfree_skb(skb); |
| 5026 | goto errout; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5027 | } |
| 5028 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 5029 | err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid); |
Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 5030 | errout: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5031 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5032 | } |
| 5033 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 5034 | 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] | 5035 | unsigned int nlm_flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5036 | { |
| 5037 | struct sk_buff *skb; |
Daniel Lezcano | 5578689 | 2008-03-04 13:47:47 -0800 | [diff] [blame] | 5038 | struct net *net = info->nl_net; |
Denis V. Lunev | 528c4ce | 2007-12-13 09:45:12 -0800 | [diff] [blame] | 5039 | u32 seq; |
| 5040 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5041 | |
Denis V. Lunev | 528c4ce | 2007-12-13 09:45:12 -0800 | [diff] [blame] | 5042 | err = -ENOBUFS; |
David S. Miller | 3830847 | 2011-12-03 18:02:47 -0500 | [diff] [blame] | 5043 | seq = info->nlh ? info->nlh->nlmsg_seq : 0; |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 5044 | |
Roopa Prabhu | 19e42e4 | 2015-07-21 10:43:48 +0200 | [diff] [blame] | 5045 | skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any()); |
David S. Miller | 3830847 | 2011-12-03 18:02:47 -0500 | [diff] [blame] | 5046 | if (!skb) |
Thomas Graf | 21713eb | 2006-08-15 00:35:24 -0700 | [diff] [blame] | 5047 | goto errout; |
| 5048 | |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 5049 | err = rt6_fill_node(net, skb, rt, NULL, NULL, NULL, 0, |
| 5050 | event, info->portid, seq, nlm_flags); |
Patrick McHardy | 2693256 | 2007-01-31 23:16:40 -0800 | [diff] [blame] | 5051 | if (err < 0) { |
| 5052 | /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */ |
| 5053 | WARN_ON(err == -EMSGSIZE); |
| 5054 | kfree_skb(skb); |
| 5055 | goto errout; |
| 5056 | } |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 5057 | rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE, |
Pablo Neira Ayuso | 1ce85fe | 2009-02-24 23:18:28 -0800 | [diff] [blame] | 5058 | info->nlh, gfp_any()); |
| 5059 | return; |
Thomas Graf | 21713eb | 2006-08-15 00:35:24 -0700 | [diff] [blame] | 5060 | errout: |
| 5061 | if (err < 0) |
Daniel Lezcano | 5578689 | 2008-03-04 13:47:47 -0800 | [diff] [blame] | 5062 | rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5063 | } |
| 5064 | |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 5065 | static int ip6_route_dev_notify(struct notifier_block *this, |
Jiri Pirko | 351638e | 2013-05-28 01:30:21 +0000 | [diff] [blame] | 5066 | unsigned long event, void *ptr) |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 5067 | { |
Jiri Pirko | 351638e | 2013-05-28 01:30:21 +0000 | [diff] [blame] | 5068 | struct net_device *dev = netdev_notifier_info_to_dev(ptr); |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 5069 | struct net *net = dev_net(dev); |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 5070 | |
WANG Cong | 242d3a4 | 2017-05-08 10:12:13 -0700 | [diff] [blame] | 5071 | if (!(dev->flags & IFF_LOOPBACK)) |
| 5072 | return NOTIFY_OK; |
| 5073 | |
| 5074 | if (event == NETDEV_REGISTER) { |
David Ahern | ad1601a | 2019-03-27 20:53:56 -0700 | [diff] [blame] | 5075 | net->ipv6.fib6_null_entry->fib6_nh.fib_nh_dev = dev; |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 5076 | net->ipv6.ip6_null_entry->dst.dev = dev; |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 5077 | net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev); |
| 5078 | #ifdef CONFIG_IPV6_MULTIPLE_TABLES |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 5079 | net->ipv6.ip6_prohibit_entry->dst.dev = dev; |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 5080 | net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev); |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 5081 | net->ipv6.ip6_blk_hole_entry->dst.dev = dev; |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 5082 | net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev); |
| 5083 | #endif |
WANG Cong | 76da070 | 2017-06-20 11:42:27 -0700 | [diff] [blame] | 5084 | } else if (event == NETDEV_UNREGISTER && |
| 5085 | dev->reg_state != NETREG_UNREGISTERED) { |
| 5086 | /* NETDEV_UNREGISTER could be fired for multiple times by |
| 5087 | * netdev_wait_allrefs(). Make sure we only call this once. |
| 5088 | */ |
Eric Dumazet | 12d94a8 | 2017-08-15 04:09:51 -0700 | [diff] [blame] | 5089 | in6_dev_put_clear(&net->ipv6.ip6_null_entry->rt6i_idev); |
WANG Cong | 242d3a4 | 2017-05-08 10:12:13 -0700 | [diff] [blame] | 5090 | #ifdef CONFIG_IPV6_MULTIPLE_TABLES |
Eric Dumazet | 12d94a8 | 2017-08-15 04:09:51 -0700 | [diff] [blame] | 5091 | in6_dev_put_clear(&net->ipv6.ip6_prohibit_entry->rt6i_idev); |
| 5092 | in6_dev_put_clear(&net->ipv6.ip6_blk_hole_entry->rt6i_idev); |
WANG Cong | 242d3a4 | 2017-05-08 10:12:13 -0700 | [diff] [blame] | 5093 | #endif |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 5094 | } |
| 5095 | |
| 5096 | return NOTIFY_OK; |
| 5097 | } |
| 5098 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5099 | /* |
| 5100 | * /proc |
| 5101 | */ |
| 5102 | |
| 5103 | #ifdef CONFIG_PROC_FS |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5104 | static int rt6_stats_seq_show(struct seq_file *seq, void *v) |
| 5105 | { |
Daniel Lezcano | 69ddb80 | 2008-03-04 13:46:23 -0800 | [diff] [blame] | 5106 | struct net *net = (struct net *)seq->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5107 | seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n", |
Daniel Lezcano | 69ddb80 | 2008-03-04 13:46:23 -0800 | [diff] [blame] | 5108 | net->ipv6.rt6_stats->fib_nodes, |
| 5109 | net->ipv6.rt6_stats->fib_route_nodes, |
Wei Wang | 81eb844 | 2017-10-06 12:06:11 -0700 | [diff] [blame] | 5110 | atomic_read(&net->ipv6.rt6_stats->fib_rt_alloc), |
Daniel Lezcano | 69ddb80 | 2008-03-04 13:46:23 -0800 | [diff] [blame] | 5111 | net->ipv6.rt6_stats->fib_rt_entries, |
| 5112 | net->ipv6.rt6_stats->fib_rt_cache, |
Eric Dumazet | fc66f95 | 2010-10-08 06:37:34 +0000 | [diff] [blame] | 5113 | dst_entries_get_slow(&net->ipv6.ip6_dst_ops), |
Daniel Lezcano | 69ddb80 | 2008-03-04 13:46:23 -0800 | [diff] [blame] | 5114 | net->ipv6.rt6_stats->fib_discarded_routes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5115 | |
| 5116 | return 0; |
| 5117 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5118 | #endif /* CONFIG_PROC_FS */ |
| 5119 | |
| 5120 | #ifdef CONFIG_SYSCTL |
| 5121 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5122 | static |
Joe Perches | fe2c633 | 2013-06-11 23:04:25 -0700 | [diff] [blame] | 5123 | int ipv6_sysctl_rtcache_flush(struct ctl_table *ctl, int write, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5124 | void __user *buffer, size_t *lenp, loff_t *ppos) |
| 5125 | { |
Lucian Adrian Grijincu | c486da3 | 2011-02-24 19:48:03 +0000 | [diff] [blame] | 5126 | struct net *net; |
| 5127 | int delay; |
Aditya Pakki | f0fb9b2 | 2018-12-24 10:30:17 -0600 | [diff] [blame] | 5128 | int ret; |
Lucian Adrian Grijincu | c486da3 | 2011-02-24 19:48:03 +0000 | [diff] [blame] | 5129 | if (!write) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5130 | return -EINVAL; |
Lucian Adrian Grijincu | c486da3 | 2011-02-24 19:48:03 +0000 | [diff] [blame] | 5131 | |
| 5132 | net = (struct net *)ctl->extra1; |
| 5133 | delay = net->ipv6.sysctl.flush_delay; |
Aditya Pakki | f0fb9b2 | 2018-12-24 10:30:17 -0600 | [diff] [blame] | 5134 | ret = proc_dointvec(ctl, write, buffer, lenp, ppos); |
| 5135 | if (ret) |
| 5136 | return ret; |
| 5137 | |
Michal Kubeček | 2ac3ac8 | 2013-08-01 10:04:14 +0200 | [diff] [blame] | 5138 | 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] | 5139 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5140 | } |
| 5141 | |
David Ahern | 7c6bb7d | 2018-10-11 20:17:21 -0700 | [diff] [blame] | 5142 | static int zero; |
| 5143 | static int one = 1; |
| 5144 | |
David Ahern | ed792e2 | 2018-10-08 14:06:34 -0700 | [diff] [blame] | 5145 | static struct ctl_table ipv6_route_table_template[] = { |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 5146 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5147 | .procname = "flush", |
Daniel Lezcano | 4990509 | 2008-01-10 03:01:01 -0800 | [diff] [blame] | 5148 | .data = &init_net.ipv6.sysctl.flush_delay, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5149 | .maxlen = sizeof(int), |
Dave Jones | 89c8b3a1 | 2005-04-28 12:11:49 -0700 | [diff] [blame] | 5150 | .mode = 0200, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 5151 | .proc_handler = ipv6_sysctl_rtcache_flush |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5152 | }, |
| 5153 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5154 | .procname = "gc_thresh", |
Daniel Lezcano | 9a7ec3a | 2008-03-04 13:48:53 -0800 | [diff] [blame] | 5155 | .data = &ip6_dst_ops_template.gc_thresh, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5156 | .maxlen = sizeof(int), |
| 5157 | .mode = 0644, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 5158 | .proc_handler = proc_dointvec, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5159 | }, |
| 5160 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5161 | .procname = "max_size", |
Daniel Lezcano | 4990509 | 2008-01-10 03:01:01 -0800 | [diff] [blame] | 5162 | .data = &init_net.ipv6.sysctl.ip6_rt_max_size, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5163 | .maxlen = sizeof(int), |
| 5164 | .mode = 0644, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 5165 | .proc_handler = proc_dointvec, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5166 | }, |
| 5167 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5168 | .procname = "gc_min_interval", |
Daniel Lezcano | 4990509 | 2008-01-10 03:01:01 -0800 | [diff] [blame] | 5169 | .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5170 | .maxlen = sizeof(int), |
| 5171 | .mode = 0644, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 5172 | .proc_handler = proc_dointvec_jiffies, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5173 | }, |
| 5174 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5175 | .procname = "gc_timeout", |
Daniel Lezcano | 4990509 | 2008-01-10 03:01:01 -0800 | [diff] [blame] | 5176 | .data = &init_net.ipv6.sysctl.ip6_rt_gc_timeout, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5177 | .maxlen = sizeof(int), |
| 5178 | .mode = 0644, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 5179 | .proc_handler = proc_dointvec_jiffies, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5180 | }, |
| 5181 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5182 | .procname = "gc_interval", |
Daniel Lezcano | 4990509 | 2008-01-10 03:01:01 -0800 | [diff] [blame] | 5183 | .data = &init_net.ipv6.sysctl.ip6_rt_gc_interval, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5184 | .maxlen = sizeof(int), |
| 5185 | .mode = 0644, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 5186 | .proc_handler = proc_dointvec_jiffies, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5187 | }, |
| 5188 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5189 | .procname = "gc_elasticity", |
Daniel Lezcano | 4990509 | 2008-01-10 03:01:01 -0800 | [diff] [blame] | 5190 | .data = &init_net.ipv6.sysctl.ip6_rt_gc_elasticity, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5191 | .maxlen = sizeof(int), |
| 5192 | .mode = 0644, |
Min Zhang | f3d3f61 | 2010-08-14 22:42:51 -0700 | [diff] [blame] | 5193 | .proc_handler = proc_dointvec, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5194 | }, |
| 5195 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5196 | .procname = "mtu_expires", |
Daniel Lezcano | 4990509 | 2008-01-10 03:01:01 -0800 | [diff] [blame] | 5197 | .data = &init_net.ipv6.sysctl.ip6_rt_mtu_expires, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5198 | .maxlen = sizeof(int), |
| 5199 | .mode = 0644, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 5200 | .proc_handler = proc_dointvec_jiffies, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5201 | }, |
| 5202 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5203 | .procname = "min_adv_mss", |
Daniel Lezcano | 4990509 | 2008-01-10 03:01:01 -0800 | [diff] [blame] | 5204 | .data = &init_net.ipv6.sysctl.ip6_rt_min_advmss, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5205 | .maxlen = sizeof(int), |
| 5206 | .mode = 0644, |
Min Zhang | f3d3f61 | 2010-08-14 22:42:51 -0700 | [diff] [blame] | 5207 | .proc_handler = proc_dointvec, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5208 | }, |
| 5209 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5210 | .procname = "gc_min_interval_ms", |
Daniel Lezcano | 4990509 | 2008-01-10 03:01:01 -0800 | [diff] [blame] | 5211 | .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5212 | .maxlen = sizeof(int), |
| 5213 | .mode = 0644, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 5214 | .proc_handler = proc_dointvec_ms_jiffies, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5215 | }, |
David Ahern | 7c6bb7d | 2018-10-11 20:17:21 -0700 | [diff] [blame] | 5216 | { |
| 5217 | .procname = "skip_notify_on_dev_down", |
| 5218 | .data = &init_net.ipv6.sysctl.skip_notify_on_dev_down, |
| 5219 | .maxlen = sizeof(int), |
| 5220 | .mode = 0644, |
| 5221 | .proc_handler = proc_dointvec, |
| 5222 | .extra1 = &zero, |
| 5223 | .extra2 = &one, |
| 5224 | }, |
Eric W. Biederman | f8572d8 | 2009-11-05 13:32:03 -0800 | [diff] [blame] | 5225 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5226 | }; |
| 5227 | |
Alexey Dobriyan | 2c8c1e7 | 2010-01-17 03:35:32 +0000 | [diff] [blame] | 5228 | struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net) |
Daniel Lezcano | 760f2d0 | 2008-01-10 02:53:43 -0800 | [diff] [blame] | 5229 | { |
| 5230 | struct ctl_table *table; |
| 5231 | |
| 5232 | table = kmemdup(ipv6_route_table_template, |
| 5233 | sizeof(ipv6_route_table_template), |
| 5234 | GFP_KERNEL); |
YOSHIFUJI Hideaki | 5ee0910 | 2008-02-28 00:24:28 +0900 | [diff] [blame] | 5235 | |
| 5236 | if (table) { |
| 5237 | table[0].data = &net->ipv6.sysctl.flush_delay; |
Lucian Adrian Grijincu | c486da3 | 2011-02-24 19:48:03 +0000 | [diff] [blame] | 5238 | table[0].extra1 = net; |
Alexey Dobriyan | 86393e5 | 2009-08-29 01:34:49 +0000 | [diff] [blame] | 5239 | table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh; |
YOSHIFUJI Hideaki | 5ee0910 | 2008-02-28 00:24:28 +0900 | [diff] [blame] | 5240 | table[2].data = &net->ipv6.sysctl.ip6_rt_max_size; |
| 5241 | table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval; |
| 5242 | table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout; |
| 5243 | table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval; |
| 5244 | table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity; |
| 5245 | table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires; |
| 5246 | table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss; |
Alexey Dobriyan | 9c69fab | 2009-12-18 20:11:03 -0800 | [diff] [blame] | 5247 | table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval; |
David Ahern | 7c6bb7d | 2018-10-11 20:17:21 -0700 | [diff] [blame] | 5248 | table[10].data = &net->ipv6.sysctl.skip_notify_on_dev_down; |
Eric W. Biederman | 464dc80 | 2012-11-16 03:02:59 +0000 | [diff] [blame] | 5249 | |
| 5250 | /* Don't export sysctls to unprivileged users */ |
| 5251 | if (net->user_ns != &init_user_ns) |
| 5252 | table[0].procname = NULL; |
YOSHIFUJI Hideaki | 5ee0910 | 2008-02-28 00:24:28 +0900 | [diff] [blame] | 5253 | } |
| 5254 | |
Daniel Lezcano | 760f2d0 | 2008-01-10 02:53:43 -0800 | [diff] [blame] | 5255 | return table; |
| 5256 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5257 | #endif |
| 5258 | |
Alexey Dobriyan | 2c8c1e7 | 2010-01-17 03:35:32 +0000 | [diff] [blame] | 5259 | static int __net_init ip6_route_net_init(struct net *net) |
Daniel Lezcano | cdb1876 | 2008-03-04 13:45:33 -0800 | [diff] [blame] | 5260 | { |
Pavel Emelyanov | 633d424b | 2008-04-21 14:25:23 -0700 | [diff] [blame] | 5261 | int ret = -ENOMEM; |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 5262 | |
Alexey Dobriyan | 86393e5 | 2009-08-29 01:34:49 +0000 | [diff] [blame] | 5263 | memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template, |
| 5264 | sizeof(net->ipv6.ip6_dst_ops)); |
Benjamin Thery | f2fc6a5 | 2008-03-04 13:49:23 -0800 | [diff] [blame] | 5265 | |
Eric Dumazet | fc66f95 | 2010-10-08 06:37:34 +0000 | [diff] [blame] | 5266 | if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0) |
| 5267 | goto out_ip6_dst_ops; |
| 5268 | |
David Ahern | 421842e | 2018-04-17 17:33:18 -0700 | [diff] [blame] | 5269 | net->ipv6.fib6_null_entry = kmemdup(&fib6_null_entry_template, |
| 5270 | sizeof(*net->ipv6.fib6_null_entry), |
| 5271 | GFP_KERNEL); |
| 5272 | if (!net->ipv6.fib6_null_entry) |
| 5273 | goto out_ip6_dst_entries; |
| 5274 | |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 5275 | net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template, |
| 5276 | sizeof(*net->ipv6.ip6_null_entry), |
| 5277 | GFP_KERNEL); |
| 5278 | if (!net->ipv6.ip6_null_entry) |
David Ahern | 421842e | 2018-04-17 17:33:18 -0700 | [diff] [blame] | 5279 | goto out_fib6_null_entry; |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 5280 | 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] | 5281 | dst_init_metrics(&net->ipv6.ip6_null_entry->dst, |
| 5282 | ip6_template_metrics, true); |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 5283 | |
| 5284 | #ifdef CONFIG_IPV6_MULTIPLE_TABLES |
Vincent Bernat | feca7d8 | 2017-08-08 20:23:49 +0200 | [diff] [blame] | 5285 | net->ipv6.fib6_has_custom_rules = false; |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 5286 | net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template, |
| 5287 | sizeof(*net->ipv6.ip6_prohibit_entry), |
| 5288 | GFP_KERNEL); |
Peter Zijlstra | 68fffc6 | 2008-10-07 14:12:10 -0700 | [diff] [blame] | 5289 | if (!net->ipv6.ip6_prohibit_entry) |
| 5290 | goto out_ip6_null_entry; |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 5291 | 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] | 5292 | dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst, |
| 5293 | ip6_template_metrics, true); |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 5294 | |
| 5295 | net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template, |
| 5296 | sizeof(*net->ipv6.ip6_blk_hole_entry), |
| 5297 | GFP_KERNEL); |
Peter Zijlstra | 68fffc6 | 2008-10-07 14:12:10 -0700 | [diff] [blame] | 5298 | if (!net->ipv6.ip6_blk_hole_entry) |
| 5299 | goto out_ip6_prohibit_entry; |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 5300 | 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] | 5301 | dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst, |
| 5302 | ip6_template_metrics, true); |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 5303 | #endif |
| 5304 | |
Peter Zijlstra | b339a47c | 2008-10-07 14:15:00 -0700 | [diff] [blame] | 5305 | net->ipv6.sysctl.flush_delay = 0; |
| 5306 | net->ipv6.sysctl.ip6_rt_max_size = 4096; |
| 5307 | net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2; |
| 5308 | net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ; |
| 5309 | net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ; |
| 5310 | net->ipv6.sysctl.ip6_rt_gc_elasticity = 9; |
| 5311 | net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ; |
| 5312 | net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40; |
David Ahern | 7c6bb7d | 2018-10-11 20:17:21 -0700 | [diff] [blame] | 5313 | net->ipv6.sysctl.skip_notify_on_dev_down = 0; |
Peter Zijlstra | b339a47c | 2008-10-07 14:15:00 -0700 | [diff] [blame] | 5314 | |
Benjamin Thery | 6891a34 | 2008-03-04 13:49:47 -0800 | [diff] [blame] | 5315 | net->ipv6.ip6_rt_gc_expire = 30*HZ; |
| 5316 | |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 5317 | ret = 0; |
| 5318 | out: |
| 5319 | return ret; |
Benjamin Thery | f2fc6a5 | 2008-03-04 13:49:23 -0800 | [diff] [blame] | 5320 | |
Peter Zijlstra | 68fffc6 | 2008-10-07 14:12:10 -0700 | [diff] [blame] | 5321 | #ifdef CONFIG_IPV6_MULTIPLE_TABLES |
| 5322 | out_ip6_prohibit_entry: |
| 5323 | kfree(net->ipv6.ip6_prohibit_entry); |
| 5324 | out_ip6_null_entry: |
| 5325 | kfree(net->ipv6.ip6_null_entry); |
| 5326 | #endif |
David Ahern | 421842e | 2018-04-17 17:33:18 -0700 | [diff] [blame] | 5327 | out_fib6_null_entry: |
| 5328 | kfree(net->ipv6.fib6_null_entry); |
Eric Dumazet | fc66f95 | 2010-10-08 06:37:34 +0000 | [diff] [blame] | 5329 | out_ip6_dst_entries: |
| 5330 | dst_entries_destroy(&net->ipv6.ip6_dst_ops); |
Benjamin Thery | f2fc6a5 | 2008-03-04 13:49:23 -0800 | [diff] [blame] | 5331 | out_ip6_dst_ops: |
Benjamin Thery | f2fc6a5 | 2008-03-04 13:49:23 -0800 | [diff] [blame] | 5332 | goto out; |
Daniel Lezcano | cdb1876 | 2008-03-04 13:45:33 -0800 | [diff] [blame] | 5333 | } |
| 5334 | |
Alexey Dobriyan | 2c8c1e7 | 2010-01-17 03:35:32 +0000 | [diff] [blame] | 5335 | static void __net_exit ip6_route_net_exit(struct net *net) |
Daniel Lezcano | cdb1876 | 2008-03-04 13:45:33 -0800 | [diff] [blame] | 5336 | { |
David Ahern | 421842e | 2018-04-17 17:33:18 -0700 | [diff] [blame] | 5337 | kfree(net->ipv6.fib6_null_entry); |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 5338 | kfree(net->ipv6.ip6_null_entry); |
| 5339 | #ifdef CONFIG_IPV6_MULTIPLE_TABLES |
| 5340 | kfree(net->ipv6.ip6_prohibit_entry); |
| 5341 | kfree(net->ipv6.ip6_blk_hole_entry); |
| 5342 | #endif |
Xiaotian Feng | 41bb78b | 2010-11-02 16:11:05 +0000 | [diff] [blame] | 5343 | dst_entries_destroy(&net->ipv6.ip6_dst_ops); |
Daniel Lezcano | cdb1876 | 2008-03-04 13:45:33 -0800 | [diff] [blame] | 5344 | } |
| 5345 | |
Thomas Graf | d189634 | 2012-06-18 12:08:33 +0000 | [diff] [blame] | 5346 | static int __net_init ip6_route_net_init_late(struct net *net) |
| 5347 | { |
| 5348 | #ifdef CONFIG_PROC_FS |
Christoph Hellwig | c350637 | 2018-04-10 19:42:55 +0200 | [diff] [blame] | 5349 | proc_create_net("ipv6_route", 0, net->proc_net, &ipv6_route_seq_ops, |
| 5350 | sizeof(struct ipv6_route_iter)); |
Christoph Hellwig | 3617d94 | 2018-04-13 20:38:35 +0200 | [diff] [blame] | 5351 | proc_create_net_single("rt6_stats", 0444, net->proc_net, |
| 5352 | rt6_stats_seq_show, NULL); |
Thomas Graf | d189634 | 2012-06-18 12:08:33 +0000 | [diff] [blame] | 5353 | #endif |
| 5354 | return 0; |
| 5355 | } |
| 5356 | |
| 5357 | static void __net_exit ip6_route_net_exit_late(struct net *net) |
| 5358 | { |
| 5359 | #ifdef CONFIG_PROC_FS |
Gao feng | ece31ff | 2013-02-18 01:34:56 +0000 | [diff] [blame] | 5360 | remove_proc_entry("ipv6_route", net->proc_net); |
| 5361 | remove_proc_entry("rt6_stats", net->proc_net); |
Thomas Graf | d189634 | 2012-06-18 12:08:33 +0000 | [diff] [blame] | 5362 | #endif |
| 5363 | } |
| 5364 | |
Daniel Lezcano | cdb1876 | 2008-03-04 13:45:33 -0800 | [diff] [blame] | 5365 | static struct pernet_operations ip6_route_net_ops = { |
| 5366 | .init = ip6_route_net_init, |
| 5367 | .exit = ip6_route_net_exit, |
| 5368 | }; |
| 5369 | |
David S. Miller | c3426b4 | 2012-06-09 16:27:05 -0700 | [diff] [blame] | 5370 | static int __net_init ipv6_inetpeer_init(struct net *net) |
| 5371 | { |
| 5372 | struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL); |
| 5373 | |
| 5374 | if (!bp) |
| 5375 | return -ENOMEM; |
| 5376 | inet_peer_base_init(bp); |
| 5377 | net->ipv6.peers = bp; |
| 5378 | return 0; |
| 5379 | } |
| 5380 | |
| 5381 | static void __net_exit ipv6_inetpeer_exit(struct net *net) |
| 5382 | { |
| 5383 | struct inet_peer_base *bp = net->ipv6.peers; |
| 5384 | |
| 5385 | net->ipv6.peers = NULL; |
David S. Miller | 56a6b24 | 2012-06-09 16:32:41 -0700 | [diff] [blame] | 5386 | inetpeer_invalidate_tree(bp); |
David S. Miller | c3426b4 | 2012-06-09 16:27:05 -0700 | [diff] [blame] | 5387 | kfree(bp); |
| 5388 | } |
| 5389 | |
David S. Miller | 2b823f7 | 2012-06-09 19:00:16 -0700 | [diff] [blame] | 5390 | static struct pernet_operations ipv6_inetpeer_ops = { |
David S. Miller | c3426b4 | 2012-06-09 16:27:05 -0700 | [diff] [blame] | 5391 | .init = ipv6_inetpeer_init, |
| 5392 | .exit = ipv6_inetpeer_exit, |
| 5393 | }; |
| 5394 | |
Thomas Graf | d189634 | 2012-06-18 12:08:33 +0000 | [diff] [blame] | 5395 | static struct pernet_operations ip6_route_net_late_ops = { |
| 5396 | .init = ip6_route_net_init_late, |
| 5397 | .exit = ip6_route_net_exit_late, |
| 5398 | }; |
| 5399 | |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 5400 | static struct notifier_block ip6_route_dev_notifier = { |
| 5401 | .notifier_call = ip6_route_dev_notify, |
WANG Cong | 242d3a4 | 2017-05-08 10:12:13 -0700 | [diff] [blame] | 5402 | .priority = ADDRCONF_NOTIFY_PRIORITY - 10, |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 5403 | }; |
| 5404 | |
WANG Cong | 2f46093 | 2017-05-03 22:07:31 -0700 | [diff] [blame] | 5405 | void __init ip6_route_init_special_entries(void) |
| 5406 | { |
| 5407 | /* Registering of the loopback is done before this portion of code, |
| 5408 | * the loopback reference in rt6_info will not be taken, do it |
| 5409 | * manually for init_net */ |
David Ahern | ad1601a | 2019-03-27 20:53:56 -0700 | [diff] [blame] | 5410 | 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] | 5411 | init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev; |
| 5412 | init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); |
| 5413 | #ifdef CONFIG_IPV6_MULTIPLE_TABLES |
| 5414 | init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev; |
| 5415 | init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); |
| 5416 | init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev; |
| 5417 | init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); |
| 5418 | #endif |
| 5419 | } |
| 5420 | |
Daniel Lezcano | 433d49c | 2007-12-07 00:43:48 -0800 | [diff] [blame] | 5421 | int __init ip6_route_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5422 | { |
Daniel Lezcano | 433d49c | 2007-12-07 00:43:48 -0800 | [diff] [blame] | 5423 | int ret; |
Martin KaFai Lau | 8d0b94a | 2015-05-22 20:56:04 -0700 | [diff] [blame] | 5424 | int cpu; |
Daniel Lezcano | 433d49c | 2007-12-07 00:43:48 -0800 | [diff] [blame] | 5425 | |
Daniel Lezcano | 9a7ec3a | 2008-03-04 13:48:53 -0800 | [diff] [blame] | 5426 | ret = -ENOMEM; |
| 5427 | ip6_dst_ops_template.kmem_cachep = |
| 5428 | kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0, |
| 5429 | SLAB_HWCACHE_ALIGN, NULL); |
| 5430 | if (!ip6_dst_ops_template.kmem_cachep) |
Fernando Carrijo | c19a28e | 2009-01-07 18:09:08 -0800 | [diff] [blame] | 5431 | goto out; |
David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 5432 | |
Eric Dumazet | fc66f95 | 2010-10-08 06:37:34 +0000 | [diff] [blame] | 5433 | ret = dst_entries_init(&ip6_dst_blackhole_ops); |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 5434 | if (ret) |
Daniel Lezcano | bdb3289 | 2008-03-04 13:48:10 -0800 | [diff] [blame] | 5435 | goto out_kmem_cache; |
Daniel Lezcano | bdb3289 | 2008-03-04 13:48:10 -0800 | [diff] [blame] | 5436 | |
David S. Miller | c3426b4 | 2012-06-09 16:27:05 -0700 | [diff] [blame] | 5437 | ret = register_pernet_subsys(&ipv6_inetpeer_ops); |
| 5438 | if (ret) |
David S. Miller | e8803b6 | 2012-06-16 01:12:19 -0700 | [diff] [blame] | 5439 | goto out_dst_entries; |
Thomas Graf | 2a0c451 | 2012-06-14 23:00:17 +0000 | [diff] [blame] | 5440 | |
David S. Miller | 7e52b33 | 2012-06-15 15:51:55 -0700 | [diff] [blame] | 5441 | ret = register_pernet_subsys(&ip6_route_net_ops); |
| 5442 | if (ret) |
| 5443 | goto out_register_inetpeer; |
David S. Miller | c3426b4 | 2012-06-09 16:27:05 -0700 | [diff] [blame] | 5444 | |
Arnaud Ebalard | 5dc121e | 2008-10-01 02:37:56 -0700 | [diff] [blame] | 5445 | ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep; |
| 5446 | |
David S. Miller | e8803b6 | 2012-06-16 01:12:19 -0700 | [diff] [blame] | 5447 | ret = fib6_init(); |
Daniel Lezcano | 433d49c | 2007-12-07 00:43:48 -0800 | [diff] [blame] | 5448 | if (ret) |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 5449 | goto out_register_subsys; |
Daniel Lezcano | 433d49c | 2007-12-07 00:43:48 -0800 | [diff] [blame] | 5450 | |
Daniel Lezcano | 433d49c | 2007-12-07 00:43:48 -0800 | [diff] [blame] | 5451 | ret = xfrm6_init(); |
| 5452 | if (ret) |
David S. Miller | e8803b6 | 2012-06-16 01:12:19 -0700 | [diff] [blame] | 5453 | goto out_fib6_init; |
Daniel Lezcano | c35b7e7 | 2007-12-08 00:14:11 -0800 | [diff] [blame] | 5454 | |
Daniel Lezcano | 433d49c | 2007-12-07 00:43:48 -0800 | [diff] [blame] | 5455 | ret = fib6_rules_init(); |
| 5456 | if (ret) |
| 5457 | goto xfrm6_init; |
Daniel Lezcano | 7e5449c | 2007-12-08 00:14:54 -0800 | [diff] [blame] | 5458 | |
Thomas Graf | d189634 | 2012-06-18 12:08:33 +0000 | [diff] [blame] | 5459 | ret = register_pernet_subsys(&ip6_route_net_late_ops); |
| 5460 | if (ret) |
| 5461 | goto fib6_rules_init; |
| 5462 | |
Florian Westphal | 16feebc | 2017-12-02 21:44:08 +0100 | [diff] [blame] | 5463 | ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_NEWROUTE, |
| 5464 | inet6_rtm_newroute, NULL, 0); |
| 5465 | if (ret < 0) |
| 5466 | goto out_register_late_subsys; |
| 5467 | |
| 5468 | ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_DELROUTE, |
| 5469 | inet6_rtm_delroute, NULL, 0); |
| 5470 | if (ret < 0) |
| 5471 | goto out_register_late_subsys; |
| 5472 | |
| 5473 | ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETROUTE, |
| 5474 | inet6_rtm_getroute, NULL, |
| 5475 | RTNL_FLAG_DOIT_UNLOCKED); |
| 5476 | if (ret < 0) |
Thomas Graf | d189634 | 2012-06-18 12:08:33 +0000 | [diff] [blame] | 5477 | goto out_register_late_subsys; |
Daniel Lezcano | 433d49c | 2007-12-07 00:43:48 -0800 | [diff] [blame] | 5478 | |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 5479 | ret = register_netdevice_notifier(&ip6_route_dev_notifier); |
Daniel Lezcano | cdb1876 | 2008-03-04 13:45:33 -0800 | [diff] [blame] | 5480 | if (ret) |
Thomas Graf | d189634 | 2012-06-18 12:08:33 +0000 | [diff] [blame] | 5481 | goto out_register_late_subsys; |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 5482 | |
Martin KaFai Lau | 8d0b94a | 2015-05-22 20:56:04 -0700 | [diff] [blame] | 5483 | for_each_possible_cpu(cpu) { |
| 5484 | struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu); |
| 5485 | |
| 5486 | INIT_LIST_HEAD(&ul->head); |
| 5487 | spin_lock_init(&ul->lock); |
| 5488 | } |
| 5489 | |
Daniel Lezcano | 433d49c | 2007-12-07 00:43:48 -0800 | [diff] [blame] | 5490 | out: |
| 5491 | return ret; |
| 5492 | |
Thomas Graf | d189634 | 2012-06-18 12:08:33 +0000 | [diff] [blame] | 5493 | out_register_late_subsys: |
Florian Westphal | 16feebc | 2017-12-02 21:44:08 +0100 | [diff] [blame] | 5494 | rtnl_unregister_all(PF_INET6); |
Thomas Graf | d189634 | 2012-06-18 12:08:33 +0000 | [diff] [blame] | 5495 | unregister_pernet_subsys(&ip6_route_net_late_ops); |
Daniel Lezcano | 433d49c | 2007-12-07 00:43:48 -0800 | [diff] [blame] | 5496 | fib6_rules_init: |
Daniel Lezcano | 433d49c | 2007-12-07 00:43:48 -0800 | [diff] [blame] | 5497 | fib6_rules_cleanup(); |
| 5498 | xfrm6_init: |
Daniel Lezcano | 433d49c | 2007-12-07 00:43:48 -0800 | [diff] [blame] | 5499 | xfrm6_fini(); |
Thomas Graf | 2a0c451 | 2012-06-14 23:00:17 +0000 | [diff] [blame] | 5500 | out_fib6_init: |
| 5501 | fib6_gc_cleanup(); |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 5502 | out_register_subsys: |
| 5503 | unregister_pernet_subsys(&ip6_route_net_ops); |
David S. Miller | 7e52b33 | 2012-06-15 15:51:55 -0700 | [diff] [blame] | 5504 | out_register_inetpeer: |
| 5505 | unregister_pernet_subsys(&ipv6_inetpeer_ops); |
Eric Dumazet | fc66f95 | 2010-10-08 06:37:34 +0000 | [diff] [blame] | 5506 | out_dst_entries: |
| 5507 | dst_entries_destroy(&ip6_dst_blackhole_ops); |
Daniel Lezcano | 433d49c | 2007-12-07 00:43:48 -0800 | [diff] [blame] | 5508 | out_kmem_cache: |
Benjamin Thery | f2fc6a5 | 2008-03-04 13:49:23 -0800 | [diff] [blame] | 5509 | kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep); |
Daniel Lezcano | 433d49c | 2007-12-07 00:43:48 -0800 | [diff] [blame] | 5510 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5511 | } |
| 5512 | |
| 5513 | void ip6_route_cleanup(void) |
| 5514 | { |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 5515 | unregister_netdevice_notifier(&ip6_route_dev_notifier); |
Thomas Graf | d189634 | 2012-06-18 12:08:33 +0000 | [diff] [blame] | 5516 | unregister_pernet_subsys(&ip6_route_net_late_ops); |
Thomas Graf | 101367c | 2006-08-04 03:39:02 -0700 | [diff] [blame] | 5517 | fib6_rules_cleanup(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5518 | xfrm6_fini(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5519 | fib6_gc_cleanup(); |
David S. Miller | c3426b4 | 2012-06-09 16:27:05 -0700 | [diff] [blame] | 5520 | unregister_pernet_subsys(&ipv6_inetpeer_ops); |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 5521 | unregister_pernet_subsys(&ip6_route_net_ops); |
Xiaotian Feng | 41bb78b | 2010-11-02 16:11:05 +0000 | [diff] [blame] | 5522 | dst_entries_destroy(&ip6_dst_blackhole_ops); |
Benjamin Thery | f2fc6a5 | 2008-03-04 13:49:23 -0800 | [diff] [blame] | 5523 | kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5524 | } |