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> |
David Ahern | b811580 | 2015-11-19 12:24:22 -0800 | [diff] [blame] | 66 | #include <trace/events/fib6.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 68 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | |
| 70 | #ifdef CONFIG_SYSCTL |
| 71 | #include <linux/sysctl.h> |
| 72 | #endif |
| 73 | |
Hannes Frederic Sowa | afc154e | 2013-07-11 12:43:42 +0200 | [diff] [blame] | 74 | enum rt6_nud_state { |
Jiri Benc | 7e98056 | 2013-12-11 13:48:20 +0100 | [diff] [blame] | 75 | RT6_NUD_FAIL_HARD = -3, |
| 76 | RT6_NUD_FAIL_PROBE = -2, |
| 77 | RT6_NUD_FAIL_DO_RR = -1, |
Hannes Frederic Sowa | afc154e | 2013-07-11 12:43:42 +0200 | [diff] [blame] | 78 | RT6_NUD_SUCCEED = 1 |
| 79 | }; |
| 80 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | 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] | 82 | static unsigned int ip6_default_advmss(const struct dst_entry *dst); |
Steffen Klassert | ebb762f | 2011-11-23 02:12:51 +0000 | [diff] [blame] | 83 | static unsigned int ip6_mtu(const struct dst_entry *dst); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | static struct dst_entry *ip6_negative_advice(struct dst_entry *); |
| 85 | static void ip6_dst_destroy(struct dst_entry *); |
| 86 | static void ip6_dst_ifdown(struct dst_entry *, |
| 87 | struct net_device *dev, int how); |
Daniel Lezcano | 569d364 | 2008-01-18 03:56:57 -0800 | [diff] [blame] | 88 | static int ip6_dst_gc(struct dst_ops *ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | |
| 90 | static int ip6_pkt_discard(struct sk_buff *skb); |
Eric W. Biederman | ede2059 | 2015-10-07 16:48:47 -0500 | [diff] [blame] | 91 | 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] | 92 | static int ip6_pkt_prohibit(struct sk_buff *skb); |
Eric W. Biederman | ede2059 | 2015-10-07 16:48:47 -0500 | [diff] [blame] | 93 | 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] | 94 | static void ip6_link_failure(struct sk_buff *skb); |
David S. Miller | 6700c27 | 2012-07-17 03:29:28 -0700 | [diff] [blame] | 95 | static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk, |
| 96 | struct sk_buff *skb, u32 mtu); |
| 97 | static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, |
| 98 | struct sk_buff *skb); |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 99 | static int rt6_score_route(struct fib6_info *rt, int oif, int strict); |
| 100 | static size_t rt6_nlmsg_size(struct fib6_info *rt); |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 101 | static int rt6_fill_node(struct net *net, struct sk_buff *skb, |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 102 | struct fib6_info *rt, struct dst_entry *dst, |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 103 | struct in6_addr *dest, struct in6_addr *src, |
David Ahern | 16a16cd | 2017-02-02 12:37:11 -0800 | [diff] [blame] | 104 | int iif, int type, u32 portid, u32 seq, |
| 105 | unsigned int flags); |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 106 | static struct rt6_info *rt6_find_cached_rt(struct fib6_info *rt, |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 107 | struct in6_addr *daddr, |
| 108 | struct in6_addr *saddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 110 | #ifdef CONFIG_IPV6_ROUTE_INFO |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 111 | static struct fib6_info *rt6_add_route_info(struct net *net, |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 112 | const struct in6_addr *prefix, int prefixlen, |
David Ahern | 830218c | 2016-10-24 10:52:35 -0700 | [diff] [blame] | 113 | const struct in6_addr *gwaddr, |
| 114 | struct net_device *dev, |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 115 | unsigned int pref); |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 116 | static struct fib6_info *rt6_get_route_info(struct net *net, |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 117 | const struct in6_addr *prefix, int prefixlen, |
David Ahern | 830218c | 2016-10-24 10:52:35 -0700 | [diff] [blame] | 118 | const struct in6_addr *gwaddr, |
| 119 | struct net_device *dev); |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 120 | #endif |
| 121 | |
Martin KaFai Lau | 8d0b94a | 2015-05-22 20:56:04 -0700 | [diff] [blame] | 122 | struct uncached_list { |
| 123 | spinlock_t lock; |
| 124 | struct list_head head; |
| 125 | }; |
| 126 | |
| 127 | static DEFINE_PER_CPU_ALIGNED(struct uncached_list, rt6_uncached_list); |
| 128 | |
Xin Long | 510c321 | 2018-02-14 19:06:02 +0800 | [diff] [blame] | 129 | void rt6_uncached_list_add(struct rt6_info *rt) |
Martin KaFai Lau | 8d0b94a | 2015-05-22 20:56:04 -0700 | [diff] [blame] | 130 | { |
| 131 | struct uncached_list *ul = raw_cpu_ptr(&rt6_uncached_list); |
| 132 | |
Martin KaFai Lau | 8d0b94a | 2015-05-22 20:56:04 -0700 | [diff] [blame] | 133 | rt->rt6i_uncached_list = ul; |
| 134 | |
| 135 | spin_lock_bh(&ul->lock); |
| 136 | list_add_tail(&rt->rt6i_uncached, &ul->head); |
| 137 | spin_unlock_bh(&ul->lock); |
| 138 | } |
| 139 | |
Xin Long | 510c321 | 2018-02-14 19:06:02 +0800 | [diff] [blame] | 140 | void rt6_uncached_list_del(struct rt6_info *rt) |
Martin KaFai Lau | 8d0b94a | 2015-05-22 20:56:04 -0700 | [diff] [blame] | 141 | { |
| 142 | if (!list_empty(&rt->rt6i_uncached)) { |
| 143 | struct uncached_list *ul = rt->rt6i_uncached_list; |
Wei Wang | 81eb844 | 2017-10-06 12:06:11 -0700 | [diff] [blame] | 144 | struct net *net = dev_net(rt->dst.dev); |
Martin KaFai Lau | 8d0b94a | 2015-05-22 20:56:04 -0700 | [diff] [blame] | 145 | |
| 146 | spin_lock_bh(&ul->lock); |
| 147 | list_del(&rt->rt6i_uncached); |
Wei Wang | 81eb844 | 2017-10-06 12:06:11 -0700 | [diff] [blame] | 148 | atomic_dec(&net->ipv6.rt6_stats->fib_rt_uncache); |
Martin KaFai Lau | 8d0b94a | 2015-05-22 20:56:04 -0700 | [diff] [blame] | 149 | spin_unlock_bh(&ul->lock); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | static void rt6_uncached_list_flush_dev(struct net *net, struct net_device *dev) |
| 154 | { |
| 155 | struct net_device *loopback_dev = net->loopback_dev; |
| 156 | int cpu; |
| 157 | |
Eric W. Biederman | e332bc6 | 2015-10-12 11:02:08 -0500 | [diff] [blame] | 158 | if (dev == loopback_dev) |
| 159 | return; |
| 160 | |
Martin KaFai Lau | 8d0b94a | 2015-05-22 20:56:04 -0700 | [diff] [blame] | 161 | for_each_possible_cpu(cpu) { |
| 162 | struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu); |
| 163 | struct rt6_info *rt; |
| 164 | |
| 165 | spin_lock_bh(&ul->lock); |
| 166 | list_for_each_entry(rt, &ul->head, rt6i_uncached) { |
| 167 | struct inet6_dev *rt_idev = rt->rt6i_idev; |
| 168 | struct net_device *rt_dev = rt->dst.dev; |
| 169 | |
Eric W. Biederman | e332bc6 | 2015-10-12 11:02:08 -0500 | [diff] [blame] | 170 | if (rt_idev->dev == dev) { |
Martin KaFai Lau | 8d0b94a | 2015-05-22 20:56:04 -0700 | [diff] [blame] | 171 | rt->rt6i_idev = in6_dev_get(loopback_dev); |
| 172 | in6_dev_put(rt_idev); |
| 173 | } |
| 174 | |
Eric W. Biederman | e332bc6 | 2015-10-12 11:02:08 -0500 | [diff] [blame] | 175 | if (rt_dev == dev) { |
Martin KaFai Lau | 8d0b94a | 2015-05-22 20:56:04 -0700 | [diff] [blame] | 176 | rt->dst.dev = loopback_dev; |
| 177 | dev_hold(rt->dst.dev); |
| 178 | dev_put(rt_dev); |
| 179 | } |
| 180 | } |
| 181 | spin_unlock_bh(&ul->lock); |
| 182 | } |
| 183 | } |
| 184 | |
David Ahern | f8a1b43 | 2018-04-17 17:33:21 -0700 | [diff] [blame] | 185 | 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] | 186 | struct sk_buff *skb, |
| 187 | const void *daddr) |
David S. Miller | 3923297 | 2012-01-26 15:22:32 -0500 | [diff] [blame] | 188 | { |
David S. Miller | a7563f3 | 2012-01-26 16:29:16 -0500 | [diff] [blame] | 189 | if (!ipv6_addr_any(p)) |
David S. Miller | 3923297 | 2012-01-26 15:22:32 -0500 | [diff] [blame] | 190 | return (const void *) p; |
David S. Miller | f894cbf | 2012-07-02 21:52:24 -0700 | [diff] [blame] | 191 | else if (skb) |
| 192 | return &ipv6_hdr(skb)->daddr; |
David S. Miller | 3923297 | 2012-01-26 15:22:32 -0500 | [diff] [blame] | 193 | return daddr; |
| 194 | } |
| 195 | |
David Ahern | f8a1b43 | 2018-04-17 17:33:21 -0700 | [diff] [blame] | 196 | struct neighbour *ip6_neigh_lookup(const struct in6_addr *gw, |
| 197 | struct net_device *dev, |
| 198 | struct sk_buff *skb, |
| 199 | const void *daddr) |
David S. Miller | d3aaeb3 | 2011-07-18 00:40:17 -0700 | [diff] [blame] | 200 | { |
David S. Miller | 3923297 | 2012-01-26 15:22:32 -0500 | [diff] [blame] | 201 | struct neighbour *n; |
| 202 | |
David Ahern | f8a1b43 | 2018-04-17 17:33:21 -0700 | [diff] [blame] | 203 | daddr = choose_neigh_daddr(gw, skb, daddr); |
| 204 | n = __ipv6_neigh_lookup(dev, daddr); |
David S. Miller | f83c779 | 2011-12-28 15:41:23 -0500 | [diff] [blame] | 205 | if (n) |
| 206 | return n; |
David Ahern | f8a1b43 | 2018-04-17 17:33:21 -0700 | [diff] [blame] | 207 | return neigh_create(&nd_tbl, daddr, dev); |
| 208 | } |
| 209 | |
| 210 | static struct neighbour *ip6_dst_neigh_lookup(const struct dst_entry *dst, |
| 211 | struct sk_buff *skb, |
| 212 | const void *daddr) |
| 213 | { |
| 214 | const struct rt6_info *rt = container_of(dst, struct rt6_info, dst); |
| 215 | |
| 216 | return ip6_neigh_lookup(&rt->rt6i_gateway, dst->dev, skb, daddr); |
David S. Miller | f83c779 | 2011-12-28 15:41:23 -0500 | [diff] [blame] | 217 | } |
| 218 | |
Julian Anastasov | 63fca65 | 2017-02-06 23:14:15 +0200 | [diff] [blame] | 219 | static void ip6_confirm_neigh(const struct dst_entry *dst, const void *daddr) |
| 220 | { |
| 221 | struct net_device *dev = dst->dev; |
| 222 | struct rt6_info *rt = (struct rt6_info *)dst; |
| 223 | |
David Ahern | f8a1b43 | 2018-04-17 17:33:21 -0700 | [diff] [blame] | 224 | daddr = choose_neigh_daddr(&rt->rt6i_gateway, NULL, daddr); |
Julian Anastasov | 63fca65 | 2017-02-06 23:14:15 +0200 | [diff] [blame] | 225 | if (!daddr) |
| 226 | return; |
| 227 | if (dev->flags & (IFF_NOARP | IFF_LOOPBACK)) |
| 228 | return; |
| 229 | if (ipv6_addr_is_multicast((const struct in6_addr *)daddr)) |
| 230 | return; |
| 231 | __ipv6_confirm_neigh(dev, daddr); |
| 232 | } |
| 233 | |
Daniel Lezcano | 9a7ec3a | 2008-03-04 13:48:53 -0800 | [diff] [blame] | 234 | static struct dst_ops ip6_dst_ops_template = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | .family = AF_INET6, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | .gc = ip6_dst_gc, |
| 237 | .gc_thresh = 1024, |
| 238 | .check = ip6_dst_check, |
David S. Miller | 0dbaee3 | 2010-12-13 12:52:14 -0800 | [diff] [blame] | 239 | .default_advmss = ip6_default_advmss, |
Steffen Klassert | ebb762f | 2011-11-23 02:12:51 +0000 | [diff] [blame] | 240 | .mtu = ip6_mtu, |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 241 | .cow_metrics = dst_cow_metrics_generic, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | .destroy = ip6_dst_destroy, |
| 243 | .ifdown = ip6_dst_ifdown, |
| 244 | .negative_advice = ip6_negative_advice, |
| 245 | .link_failure = ip6_link_failure, |
| 246 | .update_pmtu = ip6_rt_update_pmtu, |
David S. Miller | 6e157b6 | 2012-07-12 00:05:02 -0700 | [diff] [blame] | 247 | .redirect = rt6_do_redirect, |
Eric W. Biederman | 9f8955c | 2015-10-07 16:48:39 -0500 | [diff] [blame] | 248 | .local_out = __ip6_local_out, |
David Ahern | f8a1b43 | 2018-04-17 17:33:21 -0700 | [diff] [blame] | 249 | .neigh_lookup = ip6_dst_neigh_lookup, |
Julian Anastasov | 63fca65 | 2017-02-06 23:14:15 +0200 | [diff] [blame] | 250 | .confirm_neigh = ip6_confirm_neigh, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | }; |
| 252 | |
Steffen Klassert | ebb762f | 2011-11-23 02:12:51 +0000 | [diff] [blame] | 253 | static unsigned int ip6_blackhole_mtu(const struct dst_entry *dst) |
Roland Dreier | ec831ea | 2011-01-31 13:16:00 -0800 | [diff] [blame] | 254 | { |
Steffen Klassert | 618f9bc | 2011-11-23 02:13:31 +0000 | [diff] [blame] | 255 | unsigned int mtu = dst_metric_raw(dst, RTAX_MTU); |
| 256 | |
| 257 | return mtu ? : dst->dev->mtu; |
Roland Dreier | ec831ea | 2011-01-31 13:16:00 -0800 | [diff] [blame] | 258 | } |
| 259 | |
David S. Miller | 6700c27 | 2012-07-17 03:29:28 -0700 | [diff] [blame] | 260 | static void ip6_rt_blackhole_update_pmtu(struct dst_entry *dst, struct sock *sk, |
| 261 | struct sk_buff *skb, u32 mtu) |
David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 262 | { |
| 263 | } |
| 264 | |
David S. Miller | 6700c27 | 2012-07-17 03:29:28 -0700 | [diff] [blame] | 265 | static void ip6_rt_blackhole_redirect(struct dst_entry *dst, struct sock *sk, |
| 266 | struct sk_buff *skb) |
David S. Miller | b587ee3 | 2012-07-12 00:39:24 -0700 | [diff] [blame] | 267 | { |
| 268 | } |
| 269 | |
David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 270 | static struct dst_ops ip6_dst_blackhole_ops = { |
| 271 | .family = AF_INET6, |
David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 272 | .destroy = ip6_dst_destroy, |
| 273 | .check = ip6_dst_check, |
Steffen Klassert | ebb762f | 2011-11-23 02:12:51 +0000 | [diff] [blame] | 274 | .mtu = ip6_blackhole_mtu, |
Eric Dumazet | 214f45c | 2011-02-18 11:39:01 -0800 | [diff] [blame] | 275 | .default_advmss = ip6_default_advmss, |
David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 276 | .update_pmtu = ip6_rt_blackhole_update_pmtu, |
David S. Miller | b587ee3 | 2012-07-12 00:39:24 -0700 | [diff] [blame] | 277 | .redirect = ip6_rt_blackhole_redirect, |
Martin KaFai Lau | 0a1f596 | 2015-10-15 16:39:58 -0700 | [diff] [blame] | 278 | .cow_metrics = dst_cow_metrics_generic, |
David Ahern | f8a1b43 | 2018-04-17 17:33:21 -0700 | [diff] [blame] | 279 | .neigh_lookup = ip6_dst_neigh_lookup, |
David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 280 | }; |
| 281 | |
David S. Miller | 62fa8a8 | 2011-01-26 20:51:05 -0800 | [diff] [blame] | 282 | static const u32 ip6_template_metrics[RTAX_MAX] = { |
Li RongQing | 14edd87 | 2012-10-24 14:01:18 +0800 | [diff] [blame] | 283 | [RTAX_HOPLIMIT - 1] = 0, |
David S. Miller | 62fa8a8 | 2011-01-26 20:51:05 -0800 | [diff] [blame] | 284 | }; |
| 285 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 286 | static const struct fib6_info fib6_null_entry_template = { |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 287 | .fib6_flags = (RTF_REJECT | RTF_NONEXTHOP), |
| 288 | .fib6_protocol = RTPROT_KERNEL, |
| 289 | .fib6_metric = ~(u32)0, |
| 290 | .fib6_ref = ATOMIC_INIT(1), |
David Ahern | 421842e | 2018-04-17 17:33:18 -0700 | [diff] [blame] | 291 | .fib6_type = RTN_UNREACHABLE, |
| 292 | .fib6_metrics = (struct dst_metrics *)&dst_default_metrics, |
| 293 | }; |
| 294 | |
Eric Dumazet | fb0af4c | 2012-09-11 21:47:51 +0000 | [diff] [blame] | 295 | static const struct rt6_info ip6_null_entry_template = { |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 296 | .dst = { |
| 297 | .__refcnt = ATOMIC_INIT(1), |
| 298 | .__use = 1, |
Nicolas Dichtel | 2c20cbd | 2012-09-10 22:09:47 +0000 | [diff] [blame] | 299 | .obsolete = DST_OBSOLETE_FORCE_CHK, |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 300 | .error = -ENETUNREACH, |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 301 | .input = ip6_pkt_discard, |
| 302 | .output = ip6_pkt_discard_out, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | }, |
| 304 | .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | }; |
| 306 | |
Thomas Graf | 101367c | 2006-08-04 03:39:02 -0700 | [diff] [blame] | 307 | #ifdef CONFIG_IPV6_MULTIPLE_TABLES |
| 308 | |
Eric Dumazet | fb0af4c | 2012-09-11 21:47:51 +0000 | [diff] [blame] | 309 | static const struct rt6_info ip6_prohibit_entry_template = { |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 310 | .dst = { |
| 311 | .__refcnt = ATOMIC_INIT(1), |
| 312 | .__use = 1, |
Nicolas Dichtel | 2c20cbd | 2012-09-10 22:09:47 +0000 | [diff] [blame] | 313 | .obsolete = DST_OBSOLETE_FORCE_CHK, |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 314 | .error = -EACCES, |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 315 | .input = ip6_pkt_prohibit, |
| 316 | .output = ip6_pkt_prohibit_out, |
Thomas Graf | 101367c | 2006-08-04 03:39:02 -0700 | [diff] [blame] | 317 | }, |
| 318 | .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP), |
Thomas Graf | 101367c | 2006-08-04 03:39:02 -0700 | [diff] [blame] | 319 | }; |
| 320 | |
Eric Dumazet | fb0af4c | 2012-09-11 21:47:51 +0000 | [diff] [blame] | 321 | static const struct rt6_info ip6_blk_hole_entry_template = { |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 322 | .dst = { |
| 323 | .__refcnt = ATOMIC_INIT(1), |
| 324 | .__use = 1, |
Nicolas Dichtel | 2c20cbd | 2012-09-10 22:09:47 +0000 | [diff] [blame] | 325 | .obsolete = DST_OBSOLETE_FORCE_CHK, |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 326 | .error = -EINVAL, |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 327 | .input = dst_discard, |
Eric W. Biederman | ede2059 | 2015-10-07 16:48:47 -0500 | [diff] [blame] | 328 | .output = dst_discard_out, |
Thomas Graf | 101367c | 2006-08-04 03:39:02 -0700 | [diff] [blame] | 329 | }, |
| 330 | .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP), |
Thomas Graf | 101367c | 2006-08-04 03:39:02 -0700 | [diff] [blame] | 331 | }; |
| 332 | |
| 333 | #endif |
| 334 | |
Martin KaFai Lau | ebfa45f | 2015-10-15 16:39:57 -0700 | [diff] [blame] | 335 | static void rt6_info_init(struct rt6_info *rt) |
| 336 | { |
| 337 | struct dst_entry *dst = &rt->dst; |
| 338 | |
| 339 | memset(dst + 1, 0, sizeof(*rt) - sizeof(*dst)); |
Martin KaFai Lau | ebfa45f | 2015-10-15 16:39:57 -0700 | [diff] [blame] | 340 | INIT_LIST_HEAD(&rt->rt6i_uncached); |
| 341 | } |
| 342 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | /* allocate dst with ip6_dst_ops */ |
David Ahern | 93531c6 | 2018-04-17 17:33:25 -0700 | [diff] [blame] | 344 | struct rt6_info *ip6_dst_alloc(struct net *net, struct net_device *dev, |
| 345 | int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | { |
David S. Miller | 97bab73 | 2012-06-09 22:36:36 -0700 | [diff] [blame] | 347 | struct rt6_info *rt = dst_alloc(&net->ipv6.ip6_dst_ops, dev, |
Wei Wang | b2a9c0e | 2017-06-17 10:42:41 -0700 | [diff] [blame] | 348 | 1, DST_OBSOLETE_FORCE_CHK, flags); |
David S. Miller | cf91166 | 2011-04-28 14:31:47 -0700 | [diff] [blame] | 349 | |
Wei Wang | 81eb844 | 2017-10-06 12:06:11 -0700 | [diff] [blame] | 350 | if (rt) { |
Martin KaFai Lau | ebfa45f | 2015-10-15 16:39:57 -0700 | [diff] [blame] | 351 | rt6_info_init(rt); |
Wei Wang | 81eb844 | 2017-10-06 12:06:11 -0700 | [diff] [blame] | 352 | atomic_inc(&net->ipv6.rt6_stats->fib_rt_alloc); |
| 353 | } |
Steffen Klassert | 8104891 | 2012-07-05 23:37:09 +0000 | [diff] [blame] | 354 | |
David S. Miller | cf91166 | 2011-04-28 14:31:47 -0700 | [diff] [blame] | 355 | return rt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | } |
David Ahern | 9ab179d | 2016-04-07 11:10:06 -0700 | [diff] [blame] | 357 | EXPORT_SYMBOL(ip6_dst_alloc); |
Martin KaFai Lau | d52d399 | 2015-05-22 20:56:06 -0700 | [diff] [blame] | 358 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | static void ip6_dst_destroy(struct dst_entry *dst) |
| 360 | { |
| 361 | struct rt6_info *rt = (struct rt6_info *)dst; |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 362 | struct fib6_info *from = rt->from; |
Martin KaFai Lau | 8d0b94a | 2015-05-22 20:56:04 -0700 | [diff] [blame] | 363 | struct inet6_dev *idev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | |
Martin KaFai Lau | 4b32b5a | 2015-04-28 13:03:06 -0700 | [diff] [blame] | 365 | dst_destroy_metrics_generic(dst); |
Martin KaFai Lau | 8d0b94a | 2015-05-22 20:56:04 -0700 | [diff] [blame] | 366 | rt6_uncached_list_del(rt); |
| 367 | |
| 368 | idev = rt->rt6i_idev; |
David S. Miller | 3830847 | 2011-12-03 18:02:47 -0500 | [diff] [blame] | 369 | if (idev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | rt->rt6i_idev = NULL; |
| 371 | in6_dev_put(idev); |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 372 | } |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 373 | |
David Miller | 3a2232e | 2017-11-28 15:40:40 -0500 | [diff] [blame] | 374 | rt->from = NULL; |
David Ahern | 93531c6 | 2018-04-17 17:33:25 -0700 | [diff] [blame] | 375 | fib6_info_release(from); |
David S. Miller | b341936 | 2010-11-30 12:27:11 -0800 | [diff] [blame] | 376 | } |
| 377 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev, |
| 379 | int how) |
| 380 | { |
| 381 | struct rt6_info *rt = (struct rt6_info *)dst; |
| 382 | struct inet6_dev *idev = rt->rt6i_idev; |
Denis V. Lunev | 5a3e55d | 2007-12-07 00:38:10 -0800 | [diff] [blame] | 383 | struct net_device *loopback_dev = |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 384 | dev_net(dev)->loopback_dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | |
Wei Wang | e5645f5 | 2017-08-14 10:44:59 -0700 | [diff] [blame] | 386 | if (idev && idev->dev != loopback_dev) { |
| 387 | struct inet6_dev *loopback_idev = in6_dev_get(loopback_dev); |
| 388 | if (loopback_idev) { |
| 389 | rt->rt6i_idev = loopback_idev; |
| 390 | in6_dev_put(idev); |
David S. Miller | 97cac08 | 2012-07-02 22:43:47 -0700 | [diff] [blame] | 391 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | } |
| 393 | } |
| 394 | |
Martin KaFai Lau | 5973fb1 | 2015-11-11 11:51:07 -0800 | [diff] [blame] | 395 | static bool __rt6_check_expired(const struct rt6_info *rt) |
| 396 | { |
| 397 | if (rt->rt6i_flags & RTF_EXPIRES) |
| 398 | return time_after(jiffies, rt->dst.expires); |
| 399 | else |
| 400 | return false; |
| 401 | } |
| 402 | |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 403 | static bool rt6_check_expired(const struct rt6_info *rt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | { |
Gao feng | 1716a96 | 2012-04-06 00:13:10 +0000 | [diff] [blame] | 405 | if (rt->rt6i_flags & RTF_EXPIRES) { |
| 406 | if (time_after(jiffies, rt->dst.expires)) |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 407 | return true; |
David Miller | 3a2232e | 2017-11-28 15:40:40 -0500 | [diff] [blame] | 408 | } else if (rt->from) { |
Xin Long | 1e2ea8a | 2017-08-26 20:10:10 +0800 | [diff] [blame] | 409 | return rt->dst.obsolete != DST_OBSOLETE_FORCE_CHK || |
David Ahern | 1489568 | 2018-04-17 17:33:17 -0700 | [diff] [blame] | 410 | fib6_check_expired(rt->from); |
Gao feng | 1716a96 | 2012-04-06 00:13:10 +0000 | [diff] [blame] | 411 | } |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 412 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | } |
| 414 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 415 | static struct fib6_info *rt6_multipath_select(const struct net *net, |
| 416 | struct fib6_info *match, |
Nicolas Dichtel | 52bd4c0 | 2013-06-28 17:35:48 +0200 | [diff] [blame] | 417 | struct flowi6 *fl6, int oif, |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 418 | const struct sk_buff *skb, |
Nicolas Dichtel | 52bd4c0 | 2013-06-28 17:35:48 +0200 | [diff] [blame] | 419 | int strict) |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 420 | { |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 421 | struct fib6_info *sibling, *next_sibling; |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 422 | |
Jakub Sitnicki | b673d6c | 2017-08-23 09:58:31 +0200 | [diff] [blame] | 423 | /* We might have already computed the hash for ICMPv6 errors. In such |
| 424 | * case it will always be non-zero. Otherwise now is the time to do it. |
| 425 | */ |
| 426 | if (!fl6->mp_hash) |
David Ahern | b4bac17 | 2018-03-02 08:32:18 -0800 | [diff] [blame] | 427 | fl6->mp_hash = rt6_multipath_hash(net, fl6, skb, NULL); |
Jakub Sitnicki | b673d6c | 2017-08-23 09:58:31 +0200 | [diff] [blame] | 428 | |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 429 | if (fl6->mp_hash <= atomic_read(&match->fib6_nh.nh_upper_bound)) |
Ido Schimmel | 3d709f6 | 2018-01-09 16:40:27 +0200 | [diff] [blame] | 430 | return match; |
Ido Schimmel | bbfcd77 | 2017-11-21 09:50:12 +0200 | [diff] [blame] | 431 | |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 432 | list_for_each_entry_safe(sibling, next_sibling, &match->fib6_siblings, |
| 433 | fib6_siblings) { |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 434 | int nh_upper_bound; |
| 435 | |
| 436 | nh_upper_bound = atomic_read(&sibling->fib6_nh.nh_upper_bound); |
| 437 | if (fl6->mp_hash > nh_upper_bound) |
Ido Schimmel | 3d709f6 | 2018-01-09 16:40:27 +0200 | [diff] [blame] | 438 | continue; |
| 439 | if (rt6_score_route(sibling, oif, strict) < 0) |
| 440 | break; |
| 441 | match = sibling; |
| 442 | break; |
| 443 | } |
| 444 | |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 445 | return match; |
| 446 | } |
| 447 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | /* |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 449 | * Route lookup. rcu_read_lock() should be held. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | */ |
| 451 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 452 | static inline struct fib6_info *rt6_device_match(struct net *net, |
| 453 | struct fib6_info *rt, |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 454 | const struct in6_addr *saddr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | int oif, |
YOSHIFUJI Hideaki | d420895 | 2008-06-27 20:14:54 -0700 | [diff] [blame] | 456 | int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | { |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 458 | struct fib6_info *sprt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 460 | if (!oif && ipv6_addr_any(saddr) && |
| 461 | !(rt->fib6_nh.nh_flags & RTNH_F_DEAD)) |
Ido Schimmel | 8067bb8 | 2018-01-07 12:45:09 +0200 | [diff] [blame] | 462 | return rt; |
YOSHIFUJI Hideaki | dd3abc4 | 2008-07-02 18:30:18 +0900 | [diff] [blame] | 463 | |
David Miller | 071fb37 | 2017-11-28 15:40:15 -0500 | [diff] [blame] | 464 | for (sprt = rt; sprt; sprt = rcu_dereference(sprt->rt6_next)) { |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 465 | const struct net_device *dev = sprt->fib6_nh.nh_dev; |
YOSHIFUJI Hideaki | dd3abc4 | 2008-07-02 18:30:18 +0900 | [diff] [blame] | 466 | |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 467 | if (sprt->fib6_nh.nh_flags & RTNH_F_DEAD) |
Ido Schimmel | 8067bb8 | 2018-01-07 12:45:09 +0200 | [diff] [blame] | 468 | continue; |
| 469 | |
YOSHIFUJI Hideaki | dd3abc4 | 2008-07-02 18:30:18 +0900 | [diff] [blame] | 470 | if (oif) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | if (dev->ifindex == oif) |
| 472 | return sprt; |
YOSHIFUJI Hideaki | dd3abc4 | 2008-07-02 18:30:18 +0900 | [diff] [blame] | 473 | } else { |
| 474 | if (ipv6_chk_addr(net, saddr, dev, |
| 475 | flags & RT6_LOOKUP_F_IFACE)) |
| 476 | return sprt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | } |
YOSHIFUJI Hideaki | dd3abc4 | 2008-07-02 18:30:18 +0900 | [diff] [blame] | 478 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | |
David Ahern | eea68cd | 2018-04-18 15:39:02 -0700 | [diff] [blame] | 480 | if (oif && flags & RT6_LOOKUP_F_IFACE) |
| 481 | return net->ipv6.fib6_null_entry; |
Ido Schimmel | 8067bb8 | 2018-01-07 12:45:09 +0200 | [diff] [blame] | 482 | |
David Ahern | 421842e | 2018-04-17 17:33:18 -0700 | [diff] [blame] | 483 | return rt->fib6_nh.nh_flags & RTNH_F_DEAD ? net->ipv6.fib6_null_entry : rt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | } |
| 485 | |
YOSHIFUJI Hideaki | 2709725 | 2006-03-20 17:05:13 -0800 | [diff] [blame] | 486 | #ifdef CONFIG_IPV6_ROUTER_PREF |
Hannes Frederic Sowa | c2f17e8 | 2013-10-21 06:17:15 +0200 | [diff] [blame] | 487 | struct __rt6_probe_work { |
| 488 | struct work_struct work; |
| 489 | struct in6_addr target; |
| 490 | struct net_device *dev; |
| 491 | }; |
| 492 | |
| 493 | static void rt6_probe_deferred(struct work_struct *w) |
| 494 | { |
| 495 | struct in6_addr mcaddr; |
| 496 | struct __rt6_probe_work *work = |
| 497 | container_of(w, struct __rt6_probe_work, work); |
| 498 | |
| 499 | addrconf_addr_solict_mult(&work->target, &mcaddr); |
Erik Nordmark | adc176c | 2016-12-02 14:00:08 -0800 | [diff] [blame] | 500 | ndisc_send_ns(work->dev, &work->target, &mcaddr, NULL, 0); |
Hannes Frederic Sowa | c2f17e8 | 2013-10-21 06:17:15 +0200 | [diff] [blame] | 501 | dev_put(work->dev); |
Michael Büsch | 662f553 | 2015-02-08 10:14:07 +0100 | [diff] [blame] | 502 | kfree(work); |
Hannes Frederic Sowa | c2f17e8 | 2013-10-21 06:17:15 +0200 | [diff] [blame] | 503 | } |
| 504 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 505 | static void rt6_probe(struct fib6_info *rt) |
YOSHIFUJI Hideaki | 2709725 | 2006-03-20 17:05:13 -0800 | [diff] [blame] | 506 | { |
Martin KaFai Lau | 990edb4 | 2015-07-24 09:57:42 -0700 | [diff] [blame] | 507 | struct __rt6_probe_work *work; |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 508 | const struct in6_addr *nh_gw; |
Eric Dumazet | f2c31e3 | 2011-07-29 19:00:53 +0000 | [diff] [blame] | 509 | struct neighbour *neigh; |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 510 | struct net_device *dev; |
| 511 | |
YOSHIFUJI Hideaki | 2709725 | 2006-03-20 17:05:13 -0800 | [diff] [blame] | 512 | /* |
| 513 | * Okay, this does not seem to be appropriate |
| 514 | * for now, however, we need to check if it |
| 515 | * is really so; aka Router Reachability Probing. |
| 516 | * |
| 517 | * Router Reachability Probe MUST be rate-limited |
| 518 | * to no more than one per minute. |
| 519 | */ |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 520 | if (!rt || !(rt->fib6_flags & RTF_GATEWAY)) |
Amerigo Wang | fdd6681 | 2012-09-10 02:48:44 +0000 | [diff] [blame] | 521 | return; |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 522 | |
| 523 | nh_gw = &rt->fib6_nh.nh_gw; |
| 524 | dev = rt->fib6_nh.nh_dev; |
YOSHIFUJI Hideaki / 吉藤英明 | 2152cae | 2013-01-17 12:53:43 +0000 | [diff] [blame] | 525 | rcu_read_lock_bh(); |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 526 | neigh = __ipv6_neigh_lookup_noref(dev, nh_gw); |
YOSHIFUJI Hideaki / 吉藤英明 | 2152cae | 2013-01-17 12:53:43 +0000 | [diff] [blame] | 527 | if (neigh) { |
David Ahern | dcd1f57 | 2018-04-18 15:39:05 -0700 | [diff] [blame^] | 528 | struct inet6_dev *idev; |
| 529 | |
Martin KaFai Lau | 8d6c31b | 2015-07-24 09:57:43 -0700 | [diff] [blame] | 530 | if (neigh->nud_state & NUD_VALID) |
| 531 | goto out; |
| 532 | |
David Ahern | dcd1f57 | 2018-04-18 15:39:05 -0700 | [diff] [blame^] | 533 | idev = __in6_dev_get(dev); |
Martin KaFai Lau | 990edb4 | 2015-07-24 09:57:42 -0700 | [diff] [blame] | 534 | work = NULL; |
YOSHIFUJI Hideaki / 吉藤英明 | 2152cae | 2013-01-17 12:53:43 +0000 | [diff] [blame] | 535 | write_lock(&neigh->lock); |
Martin KaFai Lau | 990edb4 | 2015-07-24 09:57:42 -0700 | [diff] [blame] | 536 | if (!(neigh->nud_state & NUD_VALID) && |
| 537 | time_after(jiffies, |
David Ahern | dcd1f57 | 2018-04-18 15:39:05 -0700 | [diff] [blame^] | 538 | neigh->updated + idev->cnf.rtr_probe_interval)) { |
Martin KaFai Lau | 990edb4 | 2015-07-24 09:57:42 -0700 | [diff] [blame] | 539 | work = kmalloc(sizeof(*work), GFP_ATOMIC); |
| 540 | if (work) |
| 541 | __neigh_set_probe_once(neigh); |
Hannes Frederic Sowa | c2f17e8 | 2013-10-21 06:17:15 +0200 | [diff] [blame] | 542 | } |
YOSHIFUJI Hideaki / 吉藤英明 | 2152cae | 2013-01-17 12:53:43 +0000 | [diff] [blame] | 543 | write_unlock(&neigh->lock); |
Martin KaFai Lau | 990edb4 | 2015-07-24 09:57:42 -0700 | [diff] [blame] | 544 | } else { |
| 545 | work = kmalloc(sizeof(*work), GFP_ATOMIC); |
Eric Dumazet | f2c31e3 | 2011-07-29 19:00:53 +0000 | [diff] [blame] | 546 | } |
Martin KaFai Lau | 990edb4 | 2015-07-24 09:57:42 -0700 | [diff] [blame] | 547 | |
| 548 | if (work) { |
| 549 | INIT_WORK(&work->work, rt6_probe_deferred); |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 550 | work->target = *nh_gw; |
| 551 | dev_hold(dev); |
| 552 | work->dev = dev; |
Martin KaFai Lau | 990edb4 | 2015-07-24 09:57:42 -0700 | [diff] [blame] | 553 | schedule_work(&work->work); |
| 554 | } |
| 555 | |
Martin KaFai Lau | 8d6c31b | 2015-07-24 09:57:43 -0700 | [diff] [blame] | 556 | out: |
YOSHIFUJI Hideaki / 吉藤英明 | 2152cae | 2013-01-17 12:53:43 +0000 | [diff] [blame] | 557 | rcu_read_unlock_bh(); |
YOSHIFUJI Hideaki | 2709725 | 2006-03-20 17:05:13 -0800 | [diff] [blame] | 558 | } |
| 559 | #else |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 560 | static inline void rt6_probe(struct fib6_info *rt) |
YOSHIFUJI Hideaki | 2709725 | 2006-03-20 17:05:13 -0800 | [diff] [blame] | 561 | { |
YOSHIFUJI Hideaki | 2709725 | 2006-03-20 17:05:13 -0800 | [diff] [blame] | 562 | } |
| 563 | #endif |
| 564 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 | /* |
YOSHIFUJI Hideaki | 554cfb7 | 2006-03-20 17:00:26 -0800 | [diff] [blame] | 566 | * Default Router Selection (RFC 2461 6.3.6) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | */ |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 568 | static inline int rt6_check_dev(struct fib6_info *rt, int oif) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | { |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 570 | const struct net_device *dev = rt->fib6_nh.nh_dev; |
| 571 | |
David S. Miller | 161980f | 2007-04-06 11:42:27 -0700 | [diff] [blame] | 572 | if (!oif || dev->ifindex == oif) |
YOSHIFUJI Hideaki | 554cfb7 | 2006-03-20 17:00:26 -0800 | [diff] [blame] | 573 | return 2; |
David S. Miller | 161980f | 2007-04-06 11:42:27 -0700 | [diff] [blame] | 574 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 575 | } |
| 576 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 577 | static inline enum rt6_nud_state rt6_check_neigh(struct fib6_info *rt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | { |
Hannes Frederic Sowa | afc154e | 2013-07-11 12:43:42 +0200 | [diff] [blame] | 579 | enum rt6_nud_state ret = RT6_NUD_FAIL_HARD; |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 580 | struct neighbour *neigh; |
Eric Dumazet | f2c31e3 | 2011-07-29 19:00:53 +0000 | [diff] [blame] | 581 | |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 582 | if (rt->fib6_flags & RTF_NONEXTHOP || |
| 583 | !(rt->fib6_flags & RTF_GATEWAY)) |
Hannes Frederic Sowa | afc154e | 2013-07-11 12:43:42 +0200 | [diff] [blame] | 584 | return RT6_NUD_SUCCEED; |
YOSHIFUJI Hideaki / 吉藤英明 | 145a362 | 2013-01-17 12:53:38 +0000 | [diff] [blame] | 585 | |
| 586 | rcu_read_lock_bh(); |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 587 | neigh = __ipv6_neigh_lookup_noref(rt->fib6_nh.nh_dev, |
| 588 | &rt->fib6_nh.nh_gw); |
YOSHIFUJI Hideaki / 吉藤英明 | 145a362 | 2013-01-17 12:53:38 +0000 | [diff] [blame] | 589 | if (neigh) { |
| 590 | read_lock(&neigh->lock); |
YOSHIFUJI Hideaki | 554cfb7 | 2006-03-20 17:00:26 -0800 | [diff] [blame] | 591 | if (neigh->nud_state & NUD_VALID) |
Hannes Frederic Sowa | afc154e | 2013-07-11 12:43:42 +0200 | [diff] [blame] | 592 | ret = RT6_NUD_SUCCEED; |
YOSHIFUJI Hideaki | 398bcbe | 2008-01-19 00:35:16 -0800 | [diff] [blame] | 593 | #ifdef CONFIG_IPV6_ROUTER_PREF |
Paul Marks | a5a81f0 | 2012-12-03 10:26:54 +0000 | [diff] [blame] | 594 | else if (!(neigh->nud_state & NUD_FAILED)) |
Hannes Frederic Sowa | afc154e | 2013-07-11 12:43:42 +0200 | [diff] [blame] | 595 | ret = RT6_NUD_SUCCEED; |
Jiri Benc | 7e98056 | 2013-12-11 13:48:20 +0100 | [diff] [blame] | 596 | else |
| 597 | ret = RT6_NUD_FAIL_PROBE; |
YOSHIFUJI Hideaki | 398bcbe | 2008-01-19 00:35:16 -0800 | [diff] [blame] | 598 | #endif |
YOSHIFUJI Hideaki / 吉藤英明 | 145a362 | 2013-01-17 12:53:38 +0000 | [diff] [blame] | 599 | read_unlock(&neigh->lock); |
Hannes Frederic Sowa | afc154e | 2013-07-11 12:43:42 +0200 | [diff] [blame] | 600 | } else { |
| 601 | ret = IS_ENABLED(CONFIG_IPV6_ROUTER_PREF) ? |
Jiri Benc | 7e98056 | 2013-12-11 13:48:20 +0100 | [diff] [blame] | 602 | RT6_NUD_SUCCEED : RT6_NUD_FAIL_DO_RR; |
Paul Marks | a5a81f0 | 2012-12-03 10:26:54 +0000 | [diff] [blame] | 603 | } |
YOSHIFUJI Hideaki / 吉藤英明 | 145a362 | 2013-01-17 12:53:38 +0000 | [diff] [blame] | 604 | rcu_read_unlock_bh(); |
| 605 | |
Paul Marks | a5a81f0 | 2012-12-03 10:26:54 +0000 | [diff] [blame] | 606 | return ret; |
YOSHIFUJI Hideaki | 554cfb7 | 2006-03-20 17:00:26 -0800 | [diff] [blame] | 607 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 608 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 609 | static int rt6_score_route(struct fib6_info *rt, int oif, int strict) |
YOSHIFUJI Hideaki | 554cfb7 | 2006-03-20 17:00:26 -0800 | [diff] [blame] | 610 | { |
Paul Marks | a5a81f0 | 2012-12-03 10:26:54 +0000 | [diff] [blame] | 611 | int m; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 612 | |
YOSHIFUJI Hideaki | 4d0c591 | 2006-05-26 13:23:41 -0700 | [diff] [blame] | 613 | m = rt6_check_dev(rt, oif); |
YOSHIFUJI Hideaki | 77d16f4 | 2006-08-23 17:25:05 -0700 | [diff] [blame] | 614 | if (!m && (strict & RT6_LOOKUP_F_IFACE)) |
Hannes Frederic Sowa | afc154e | 2013-07-11 12:43:42 +0200 | [diff] [blame] | 615 | return RT6_NUD_FAIL_HARD; |
YOSHIFUJI Hideaki | ebacaaa | 2006-03-20 17:04:53 -0800 | [diff] [blame] | 616 | #ifdef CONFIG_IPV6_ROUTER_PREF |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 617 | m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(rt->fib6_flags)) << 2; |
YOSHIFUJI Hideaki | ebacaaa | 2006-03-20 17:04:53 -0800 | [diff] [blame] | 618 | #endif |
Hannes Frederic Sowa | afc154e | 2013-07-11 12:43:42 +0200 | [diff] [blame] | 619 | if (strict & RT6_LOOKUP_F_REACHABLE) { |
| 620 | int n = rt6_check_neigh(rt); |
| 621 | if (n < 0) |
| 622 | return n; |
| 623 | } |
YOSHIFUJI Hideaki | 554cfb7 | 2006-03-20 17:00:26 -0800 | [diff] [blame] | 624 | return m; |
| 625 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | |
David Ahern | dcd1f57 | 2018-04-18 15:39:05 -0700 | [diff] [blame^] | 627 | /* called with rc_read_lock held */ |
| 628 | static inline bool fib6_ignore_linkdown(const struct fib6_info *f6i) |
| 629 | { |
| 630 | const struct net_device *dev = fib6_info_nh_dev(f6i); |
| 631 | bool rc = false; |
| 632 | |
| 633 | if (dev) { |
| 634 | const struct inet6_dev *idev = __in6_dev_get(dev); |
| 635 | |
| 636 | rc = !!idev->cnf.ignore_routes_with_linkdown; |
| 637 | } |
| 638 | |
| 639 | return rc; |
| 640 | } |
| 641 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 642 | static struct fib6_info *find_match(struct fib6_info *rt, int oif, int strict, |
| 643 | int *mpri, struct fib6_info *match, |
Hannes Frederic Sowa | afc154e | 2013-07-11 12:43:42 +0200 | [diff] [blame] | 644 | bool *do_rr) |
YOSHIFUJI Hideaki | 554cfb7 | 2006-03-20 17:00:26 -0800 | [diff] [blame] | 645 | { |
David S. Miller | f11e665 | 2007-03-24 20:36:25 -0700 | [diff] [blame] | 646 | int m; |
Hannes Frederic Sowa | afc154e | 2013-07-11 12:43:42 +0200 | [diff] [blame] | 647 | bool match_do_rr = false; |
Andy Gospodarek | 35103d1 | 2015-08-13 10:39:01 -0400 | [diff] [blame] | 648 | |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 649 | if (rt->fib6_nh.nh_flags & RTNH_F_DEAD) |
Ido Schimmel | 8067bb8 | 2018-01-07 12:45:09 +0200 | [diff] [blame] | 650 | goto out; |
| 651 | |
David Ahern | dcd1f57 | 2018-04-18 15:39:05 -0700 | [diff] [blame^] | 652 | if (fib6_ignore_linkdown(rt) && |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 653 | rt->fib6_nh.nh_flags & RTNH_F_LINKDOWN && |
David Ahern | d5d32e4 | 2016-10-24 12:27:23 -0700 | [diff] [blame] | 654 | !(strict & RT6_LOOKUP_F_IGNORE_LINKSTATE)) |
Andy Gospodarek | 35103d1 | 2015-08-13 10:39:01 -0400 | [diff] [blame] | 655 | goto out; |
David S. Miller | f11e665 | 2007-03-24 20:36:25 -0700 | [diff] [blame] | 656 | |
David Ahern | 1489568 | 2018-04-17 17:33:17 -0700 | [diff] [blame] | 657 | if (fib6_check_expired(rt)) |
David S. Miller | f11e665 | 2007-03-24 20:36:25 -0700 | [diff] [blame] | 658 | goto out; |
| 659 | |
| 660 | m = rt6_score_route(rt, oif, strict); |
Jiri Benc | 7e98056 | 2013-12-11 13:48:20 +0100 | [diff] [blame] | 661 | if (m == RT6_NUD_FAIL_DO_RR) { |
Hannes Frederic Sowa | afc154e | 2013-07-11 12:43:42 +0200 | [diff] [blame] | 662 | match_do_rr = true; |
| 663 | m = 0; /* lowest valid score */ |
Jiri Benc | 7e98056 | 2013-12-11 13:48:20 +0100 | [diff] [blame] | 664 | } else if (m == RT6_NUD_FAIL_HARD) { |
David S. Miller | f11e665 | 2007-03-24 20:36:25 -0700 | [diff] [blame] | 665 | goto out; |
David S. Miller | f11e665 | 2007-03-24 20:36:25 -0700 | [diff] [blame] | 666 | } |
| 667 | |
Hannes Frederic Sowa | afc154e | 2013-07-11 12:43:42 +0200 | [diff] [blame] | 668 | if (strict & RT6_LOOKUP_F_REACHABLE) |
| 669 | rt6_probe(rt); |
| 670 | |
Jiri Benc | 7e98056 | 2013-12-11 13:48:20 +0100 | [diff] [blame] | 671 | /* 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] | 672 | if (m > *mpri) { |
| 673 | *do_rr = match_do_rr; |
| 674 | *mpri = m; |
| 675 | match = rt; |
| 676 | } |
David S. Miller | f11e665 | 2007-03-24 20:36:25 -0700 | [diff] [blame] | 677 | out: |
| 678 | return match; |
| 679 | } |
| 680 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 681 | static struct fib6_info *find_rr_leaf(struct fib6_node *fn, |
| 682 | struct fib6_info *leaf, |
| 683 | struct fib6_info *rr_head, |
Hannes Frederic Sowa | afc154e | 2013-07-11 12:43:42 +0200 | [diff] [blame] | 684 | u32 metric, int oif, int strict, |
| 685 | bool *do_rr) |
David S. Miller | f11e665 | 2007-03-24 20:36:25 -0700 | [diff] [blame] | 686 | { |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 687 | struct fib6_info *rt, *match, *cont; |
YOSHIFUJI Hideaki | 554cfb7 | 2006-03-20 17:00:26 -0800 | [diff] [blame] | 688 | int mpri = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | |
David S. Miller | f11e665 | 2007-03-24 20:36:25 -0700 | [diff] [blame] | 690 | match = NULL; |
Steffen Klassert | 9fbdcfa | 2015-04-28 13:03:04 -0700 | [diff] [blame] | 691 | cont = NULL; |
David Miller | 071fb37 | 2017-11-28 15:40:15 -0500 | [diff] [blame] | 692 | for (rt = rr_head; rt; rt = rcu_dereference(rt->rt6_next)) { |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 693 | if (rt->fib6_metric != metric) { |
Steffen Klassert | 9fbdcfa | 2015-04-28 13:03:04 -0700 | [diff] [blame] | 694 | cont = rt; |
| 695 | break; |
| 696 | } |
| 697 | |
Hannes Frederic Sowa | afc154e | 2013-07-11 12:43:42 +0200 | [diff] [blame] | 698 | match = find_match(rt, oif, strict, &mpri, match, do_rr); |
Steffen Klassert | 9fbdcfa | 2015-04-28 13:03:04 -0700 | [diff] [blame] | 699 | } |
| 700 | |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 701 | for (rt = leaf; rt && rt != rr_head; |
David Miller | 071fb37 | 2017-11-28 15:40:15 -0500 | [diff] [blame] | 702 | rt = rcu_dereference(rt->rt6_next)) { |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 703 | if (rt->fib6_metric != metric) { |
Steffen Klassert | 9fbdcfa | 2015-04-28 13:03:04 -0700 | [diff] [blame] | 704 | cont = rt; |
| 705 | break; |
| 706 | } |
| 707 | |
| 708 | match = find_match(rt, oif, strict, &mpri, match, do_rr); |
| 709 | } |
| 710 | |
| 711 | if (match || !cont) |
| 712 | return match; |
| 713 | |
David Miller | 071fb37 | 2017-11-28 15:40:15 -0500 | [diff] [blame] | 714 | for (rt = cont; rt; rt = rcu_dereference(rt->rt6_next)) |
Hannes Frederic Sowa | afc154e | 2013-07-11 12:43:42 +0200 | [diff] [blame] | 715 | match = find_match(rt, oif, strict, &mpri, match, do_rr); |
YOSHIFUJI Hideaki | 554cfb7 | 2006-03-20 17:00:26 -0800 | [diff] [blame] | 716 | |
David S. Miller | f11e665 | 2007-03-24 20:36:25 -0700 | [diff] [blame] | 717 | return match; |
| 718 | } |
YOSHIFUJI Hideaki | 554cfb7 | 2006-03-20 17:00:26 -0800 | [diff] [blame] | 719 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 720 | 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] | 721 | int oif, int strict) |
David S. Miller | f11e665 | 2007-03-24 20:36:25 -0700 | [diff] [blame] | 722 | { |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 723 | struct fib6_info *leaf = rcu_dereference(fn->leaf); |
| 724 | struct fib6_info *match, *rt0; |
Hannes Frederic Sowa | afc154e | 2013-07-11 12:43:42 +0200 | [diff] [blame] | 725 | bool do_rr = false; |
Wei Wang | 17ecf59 | 2017-10-06 12:06:09 -0700 | [diff] [blame] | 726 | int key_plen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 727 | |
David Ahern | 421842e | 2018-04-17 17:33:18 -0700 | [diff] [blame] | 728 | if (!leaf || leaf == net->ipv6.fib6_null_entry) |
| 729 | return net->ipv6.fib6_null_entry; |
Wei Wang | 8d1040e | 2017-10-06 12:06:08 -0700 | [diff] [blame] | 730 | |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 731 | rt0 = rcu_dereference(fn->rr_ptr); |
David S. Miller | f11e665 | 2007-03-24 20:36:25 -0700 | [diff] [blame] | 732 | if (!rt0) |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 733 | rt0 = leaf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 734 | |
Wei Wang | 17ecf59 | 2017-10-06 12:06:09 -0700 | [diff] [blame] | 735 | /* Double check to make sure fn is not an intermediate node |
| 736 | * and fn->leaf does not points to its child's leaf |
| 737 | * (This might happen if all routes under fn are deleted from |
| 738 | * the tree and fib6_repair_tree() is called on the node.) |
| 739 | */ |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 740 | key_plen = rt0->fib6_dst.plen; |
Wei Wang | 17ecf59 | 2017-10-06 12:06:09 -0700 | [diff] [blame] | 741 | #ifdef CONFIG_IPV6_SUBTREES |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 742 | if (rt0->fib6_src.plen) |
| 743 | key_plen = rt0->fib6_src.plen; |
Wei Wang | 17ecf59 | 2017-10-06 12:06:09 -0700 | [diff] [blame] | 744 | #endif |
| 745 | if (fn->fn_bit != key_plen) |
David Ahern | 421842e | 2018-04-17 17:33:18 -0700 | [diff] [blame] | 746 | return net->ipv6.fib6_null_entry; |
Wei Wang | 17ecf59 | 2017-10-06 12:06:09 -0700 | [diff] [blame] | 747 | |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 748 | 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] | 749 | &do_rr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 750 | |
Hannes Frederic Sowa | afc154e | 2013-07-11 12:43:42 +0200 | [diff] [blame] | 751 | if (do_rr) { |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 752 | struct fib6_info *next = rcu_dereference(rt0->rt6_next); |
David S. Miller | f11e665 | 2007-03-24 20:36:25 -0700 | [diff] [blame] | 753 | |
YOSHIFUJI Hideaki | 554cfb7 | 2006-03-20 17:00:26 -0800 | [diff] [blame] | 754 | /* no entries matched; do round-robin */ |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 755 | if (!next || next->fib6_metric != rt0->fib6_metric) |
Wei Wang | 8d1040e | 2017-10-06 12:06:08 -0700 | [diff] [blame] | 756 | next = leaf; |
David S. Miller | f11e665 | 2007-03-24 20:36:25 -0700 | [diff] [blame] | 757 | |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 758 | if (next != rt0) { |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 759 | spin_lock_bh(&leaf->fib6_table->tb6_lock); |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 760 | /* make sure next is not being deleted from the tree */ |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 761 | if (next->fib6_node) |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 762 | rcu_assign_pointer(fn->rr_ptr, next); |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 763 | spin_unlock_bh(&leaf->fib6_table->tb6_lock); |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 764 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 765 | } |
| 766 | |
David Ahern | 421842e | 2018-04-17 17:33:18 -0700 | [diff] [blame] | 767 | return match ? match : net->ipv6.fib6_null_entry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 768 | } |
| 769 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 770 | static bool rt6_is_gw_or_nonexthop(const struct fib6_info *rt) |
Martin KaFai Lau | 8b9df26 | 2015-05-22 20:55:59 -0700 | [diff] [blame] | 771 | { |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 772 | return (rt->fib6_flags & (RTF_NONEXTHOP | RTF_GATEWAY)); |
Martin KaFai Lau | 8b9df26 | 2015-05-22 20:55:59 -0700 | [diff] [blame] | 773 | } |
| 774 | |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 775 | #ifdef CONFIG_IPV6_ROUTE_INFO |
| 776 | int rt6_route_rcv(struct net_device *dev, u8 *opt, int len, |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 777 | const struct in6_addr *gwaddr) |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 778 | { |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 779 | struct net *net = dev_net(dev); |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 780 | struct route_info *rinfo = (struct route_info *) opt; |
| 781 | struct in6_addr prefix_buf, *prefix; |
| 782 | unsigned int pref; |
YOSHIFUJI Hideaki | 4bed72e | 2008-05-27 17:37:49 +0900 | [diff] [blame] | 783 | unsigned long lifetime; |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 784 | struct fib6_info *rt; |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 785 | |
| 786 | if (len < sizeof(struct route_info)) { |
| 787 | return -EINVAL; |
| 788 | } |
| 789 | |
| 790 | /* Sanity check for prefix_len and length */ |
| 791 | if (rinfo->length > 3) { |
| 792 | return -EINVAL; |
| 793 | } else if (rinfo->prefix_len > 128) { |
| 794 | return -EINVAL; |
| 795 | } else if (rinfo->prefix_len > 64) { |
| 796 | if (rinfo->length < 2) { |
| 797 | return -EINVAL; |
| 798 | } |
| 799 | } else if (rinfo->prefix_len > 0) { |
| 800 | if (rinfo->length < 1) { |
| 801 | return -EINVAL; |
| 802 | } |
| 803 | } |
| 804 | |
| 805 | pref = rinfo->route_pref; |
| 806 | if (pref == ICMPV6_ROUTER_PREF_INVALID) |
Jens Rosenboom | 3933fc9 | 2009-09-10 06:25:11 +0000 | [diff] [blame] | 807 | return -EINVAL; |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 808 | |
YOSHIFUJI Hideaki | 4bed72e | 2008-05-27 17:37:49 +0900 | [diff] [blame] | 809 | lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ); |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 810 | |
| 811 | if (rinfo->length == 3) |
| 812 | prefix = (struct in6_addr *)rinfo->prefix; |
| 813 | else { |
| 814 | /* this function is safe */ |
| 815 | ipv6_addr_prefix(&prefix_buf, |
| 816 | (struct in6_addr *)rinfo->prefix, |
| 817 | rinfo->prefix_len); |
| 818 | prefix = &prefix_buf; |
| 819 | } |
| 820 | |
Duan Jiong | f104a56 | 2013-11-08 09:56:53 +0800 | [diff] [blame] | 821 | if (rinfo->prefix_len == 0) |
David Ahern | afb1d4b5 | 2018-04-17 17:33:11 -0700 | [diff] [blame] | 822 | rt = rt6_get_dflt_router(net, gwaddr, dev); |
Duan Jiong | f104a56 | 2013-11-08 09:56:53 +0800 | [diff] [blame] | 823 | else |
| 824 | rt = rt6_get_route_info(net, prefix, rinfo->prefix_len, |
David Ahern | 830218c | 2016-10-24 10:52:35 -0700 | [diff] [blame] | 825 | gwaddr, dev); |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 826 | |
| 827 | if (rt && !lifetime) { |
David Ahern | afb1d4b5 | 2018-04-17 17:33:11 -0700 | [diff] [blame] | 828 | ip6_del_rt(net, rt); |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 829 | rt = NULL; |
| 830 | } |
| 831 | |
| 832 | if (!rt && lifetime) |
David Ahern | 830218c | 2016-10-24 10:52:35 -0700 | [diff] [blame] | 833 | rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr, |
| 834 | dev, pref); |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 835 | else if (rt) |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 836 | rt->fib6_flags = RTF_ROUTEINFO | |
| 837 | (rt->fib6_flags & ~RTF_PREF_MASK) | RTF_PREF(pref); |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 838 | |
| 839 | if (rt) { |
Gao feng | 1716a96 | 2012-04-06 00:13:10 +0000 | [diff] [blame] | 840 | if (!addrconf_finite_timeout(lifetime)) |
David Ahern | 1489568 | 2018-04-17 17:33:17 -0700 | [diff] [blame] | 841 | fib6_clean_expires(rt); |
Gao feng | 1716a96 | 2012-04-06 00:13:10 +0000 | [diff] [blame] | 842 | else |
David Ahern | 1489568 | 2018-04-17 17:33:17 -0700 | [diff] [blame] | 843 | fib6_set_expires(rt, jiffies + HZ * lifetime); |
Gao feng | 1716a96 | 2012-04-06 00:13:10 +0000 | [diff] [blame] | 844 | |
David Ahern | 93531c6 | 2018-04-17 17:33:25 -0700 | [diff] [blame] | 845 | fib6_info_release(rt); |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 846 | } |
| 847 | return 0; |
| 848 | } |
| 849 | #endif |
| 850 | |
David Ahern | ae90d86 | 2018-04-17 17:33:12 -0700 | [diff] [blame] | 851 | /* |
| 852 | * Misc support functions |
| 853 | */ |
| 854 | |
| 855 | /* called with rcu_lock held */ |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 856 | 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] | 857 | { |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 858 | struct net_device *dev = rt->fib6_nh.nh_dev; |
David Ahern | ae90d86 | 2018-04-17 17:33:12 -0700 | [diff] [blame] | 859 | |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 860 | if (rt->fib6_flags & (RTF_LOCAL | RTF_ANYCAST)) { |
David Ahern | ae90d86 | 2018-04-17 17:33:12 -0700 | [diff] [blame] | 861 | /* for copies of local routes, dst->dev needs to be the |
| 862 | * device if it is a master device, the master device if |
| 863 | * device is enslaved, and the loopback as the default |
| 864 | */ |
| 865 | if (netif_is_l3_slave(dev) && |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 866 | !rt6_need_strict(&rt->fib6_dst.addr)) |
David Ahern | ae90d86 | 2018-04-17 17:33:12 -0700 | [diff] [blame] | 867 | dev = l3mdev_master_dev_rcu(dev); |
| 868 | else if (!netif_is_l3_master(dev)) |
| 869 | dev = dev_net(dev)->loopback_dev; |
| 870 | /* last case is netif_is_l3_master(dev) is true in which |
| 871 | * case we want dev returned to be dev |
| 872 | */ |
| 873 | } |
| 874 | |
| 875 | return dev; |
| 876 | } |
| 877 | |
David Ahern | 6edb3c9 | 2018-04-17 17:33:15 -0700 | [diff] [blame] | 878 | static const int fib6_prop[RTN_MAX + 1] = { |
| 879 | [RTN_UNSPEC] = 0, |
| 880 | [RTN_UNICAST] = 0, |
| 881 | [RTN_LOCAL] = 0, |
| 882 | [RTN_BROADCAST] = 0, |
| 883 | [RTN_ANYCAST] = 0, |
| 884 | [RTN_MULTICAST] = 0, |
| 885 | [RTN_BLACKHOLE] = -EINVAL, |
| 886 | [RTN_UNREACHABLE] = -EHOSTUNREACH, |
| 887 | [RTN_PROHIBIT] = -EACCES, |
| 888 | [RTN_THROW] = -EAGAIN, |
| 889 | [RTN_NAT] = -EINVAL, |
| 890 | [RTN_XRESOLVE] = -EINVAL, |
| 891 | }; |
| 892 | |
| 893 | static int ip6_rt_type_to_error(u8 fib6_type) |
| 894 | { |
| 895 | return fib6_prop[fib6_type]; |
| 896 | } |
| 897 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 898 | static unsigned short fib6_info_dst_flags(struct fib6_info *rt) |
David Ahern | 3b6761d | 2018-04-17 17:33:20 -0700 | [diff] [blame] | 899 | { |
| 900 | unsigned short flags = 0; |
| 901 | |
| 902 | if (rt->dst_nocount) |
| 903 | flags |= DST_NOCOUNT; |
| 904 | if (rt->dst_nopolicy) |
| 905 | flags |= DST_NOPOLICY; |
| 906 | if (rt->dst_host) |
| 907 | flags |= DST_HOST; |
| 908 | |
| 909 | return flags; |
| 910 | } |
| 911 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 912 | 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] | 913 | { |
| 914 | rt->dst.error = ip6_rt_type_to_error(ort->fib6_type); |
| 915 | |
| 916 | switch (ort->fib6_type) { |
| 917 | case RTN_BLACKHOLE: |
| 918 | rt->dst.output = dst_discard_out; |
| 919 | rt->dst.input = dst_discard; |
| 920 | break; |
| 921 | case RTN_PROHIBIT: |
| 922 | rt->dst.output = ip6_pkt_prohibit_out; |
| 923 | rt->dst.input = ip6_pkt_prohibit; |
| 924 | break; |
| 925 | case RTN_THROW: |
| 926 | case RTN_UNREACHABLE: |
| 927 | default: |
| 928 | rt->dst.output = ip6_pkt_discard_out; |
| 929 | rt->dst.input = ip6_pkt_discard; |
| 930 | break; |
| 931 | } |
| 932 | } |
| 933 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 934 | 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] | 935 | { |
David Ahern | 3b6761d | 2018-04-17 17:33:20 -0700 | [diff] [blame] | 936 | rt->dst.flags |= fib6_info_dst_flags(ort); |
| 937 | |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 938 | if (ort->fib6_flags & RTF_REJECT) { |
David Ahern | 6edb3c9 | 2018-04-17 17:33:15 -0700 | [diff] [blame] | 939 | ip6_rt_init_dst_reject(rt, ort); |
| 940 | return; |
| 941 | } |
| 942 | |
| 943 | rt->dst.error = 0; |
| 944 | rt->dst.output = ip6_output; |
| 945 | |
| 946 | if (ort->fib6_type == RTN_LOCAL) { |
David Ahern | 6edb3c9 | 2018-04-17 17:33:15 -0700 | [diff] [blame] | 947 | rt->dst.input = ip6_input; |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 948 | } else if (ipv6_addr_type(&ort->fib6_dst.addr) & IPV6_ADDR_MULTICAST) { |
David Ahern | 6edb3c9 | 2018-04-17 17:33:15 -0700 | [diff] [blame] | 949 | rt->dst.input = ip6_mc_input; |
| 950 | } else { |
| 951 | rt->dst.input = ip6_forward; |
| 952 | } |
| 953 | |
| 954 | if (ort->fib6_nh.nh_lwtstate) { |
| 955 | rt->dst.lwtstate = lwtstate_get(ort->fib6_nh.nh_lwtstate); |
| 956 | lwtunnel_set_redirect(&rt->dst); |
| 957 | } |
| 958 | |
| 959 | rt->dst.lastuse = jiffies; |
| 960 | } |
| 961 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 962 | 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] | 963 | { |
David Ahern | ae90d86 | 2018-04-17 17:33:12 -0700 | [diff] [blame] | 964 | rt->rt6i_flags &= ~RTF_EXPIRES; |
David Ahern | 93531c6 | 2018-04-17 17:33:25 -0700 | [diff] [blame] | 965 | fib6_info_hold(from); |
| 966 | rt->from = from; |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 967 | dst_init_metrics(&rt->dst, from->fib6_metrics->metrics, true); |
| 968 | if (from->fib6_metrics != &dst_default_metrics) { |
| 969 | rt->dst._metrics |= DST_METRICS_REFCOUNTED; |
| 970 | refcount_inc(&from->fib6_metrics->refcnt); |
| 971 | } |
David Ahern | ae90d86 | 2018-04-17 17:33:12 -0700 | [diff] [blame] | 972 | } |
| 973 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 974 | 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] | 975 | { |
David Ahern | dcd1f57 | 2018-04-18 15:39:05 -0700 | [diff] [blame^] | 976 | struct net_device *dev = fib6_info_nh_dev(ort); |
| 977 | |
David Ahern | 6edb3c9 | 2018-04-17 17:33:15 -0700 | [diff] [blame] | 978 | ip6_rt_init_dst(rt, ort); |
| 979 | |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 980 | rt->rt6i_dst = ort->fib6_dst; |
David Ahern | dcd1f57 | 2018-04-18 15:39:05 -0700 | [diff] [blame^] | 981 | rt->rt6i_idev = dev ? in6_dev_get(dev) : NULL; |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 982 | rt->rt6i_gateway = ort->fib6_nh.nh_gw; |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 983 | rt->rt6i_flags = ort->fib6_flags; |
David Ahern | ae90d86 | 2018-04-17 17:33:12 -0700 | [diff] [blame] | 984 | rt6_set_from(rt, ort); |
David Ahern | ae90d86 | 2018-04-17 17:33:12 -0700 | [diff] [blame] | 985 | #ifdef CONFIG_IPV6_SUBTREES |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 986 | rt->rt6i_src = ort->fib6_src; |
David Ahern | ae90d86 | 2018-04-17 17:33:12 -0700 | [diff] [blame] | 987 | #endif |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 988 | rt->rt6i_prefsrc = ort->fib6_prefsrc; |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 989 | rt->dst.lwtstate = lwtstate_get(ort->fib6_nh.nh_lwtstate); |
David Ahern | ae90d86 | 2018-04-17 17:33:12 -0700 | [diff] [blame] | 990 | } |
| 991 | |
Martin KaFai Lau | a3c00e4 | 2014-10-20 13:42:43 -0700 | [diff] [blame] | 992 | static struct fib6_node* fib6_backtrack(struct fib6_node *fn, |
| 993 | struct in6_addr *saddr) |
| 994 | { |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 995 | struct fib6_node *pn, *sn; |
Martin KaFai Lau | a3c00e4 | 2014-10-20 13:42:43 -0700 | [diff] [blame] | 996 | while (1) { |
| 997 | if (fn->fn_flags & RTN_TL_ROOT) |
| 998 | return NULL; |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 999 | pn = rcu_dereference(fn->parent); |
| 1000 | sn = FIB6_SUBTREE(pn); |
| 1001 | if (sn && sn != fn) |
| 1002 | fn = fib6_lookup(sn, NULL, saddr); |
Martin KaFai Lau | a3c00e4 | 2014-10-20 13:42:43 -0700 | [diff] [blame] | 1003 | else |
| 1004 | fn = pn; |
| 1005 | if (fn->fn_flags & RTN_RTINFO) |
| 1006 | return fn; |
| 1007 | } |
| 1008 | } |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1009 | |
Wei Wang | d3843fe | 2017-10-06 12:06:06 -0700 | [diff] [blame] | 1010 | static bool ip6_hold_safe(struct net *net, struct rt6_info **prt, |
| 1011 | bool null_fallback) |
| 1012 | { |
| 1013 | struct rt6_info *rt = *prt; |
| 1014 | |
| 1015 | if (dst_hold_safe(&rt->dst)) |
| 1016 | return true; |
| 1017 | if (null_fallback) { |
| 1018 | rt = net->ipv6.ip6_null_entry; |
| 1019 | dst_hold(&rt->dst); |
| 1020 | } else { |
| 1021 | rt = NULL; |
| 1022 | } |
| 1023 | *prt = rt; |
| 1024 | return false; |
| 1025 | } |
| 1026 | |
David Ahern | dec9b0e | 2018-04-17 17:33:19 -0700 | [diff] [blame] | 1027 | /* called with rcu_lock held */ |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 1028 | static struct rt6_info *ip6_create_rt_rcu(struct fib6_info *rt) |
David Ahern | dec9b0e | 2018-04-17 17:33:19 -0700 | [diff] [blame] | 1029 | { |
David Ahern | 3b6761d | 2018-04-17 17:33:20 -0700 | [diff] [blame] | 1030 | unsigned short flags = fib6_info_dst_flags(rt); |
David Ahern | dec9b0e | 2018-04-17 17:33:19 -0700 | [diff] [blame] | 1031 | struct net_device *dev = rt->fib6_nh.nh_dev; |
| 1032 | struct rt6_info *nrt; |
| 1033 | |
David Ahern | 93531c6 | 2018-04-17 17:33:25 -0700 | [diff] [blame] | 1034 | nrt = ip6_dst_alloc(dev_net(dev), dev, flags); |
David Ahern | dec9b0e | 2018-04-17 17:33:19 -0700 | [diff] [blame] | 1035 | if (nrt) |
| 1036 | ip6_rt_copy_init(nrt, rt); |
| 1037 | |
| 1038 | return nrt; |
| 1039 | } |
| 1040 | |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 1041 | static struct rt6_info *ip6_pol_route_lookup(struct net *net, |
| 1042 | struct fib6_table *table, |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 1043 | struct flowi6 *fl6, |
| 1044 | const struct sk_buff *skb, |
| 1045 | int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1046 | { |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 1047 | struct fib6_info *f6i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1048 | struct fib6_node *fn; |
David Ahern | 23fb93a | 2018-04-17 17:33:23 -0700 | [diff] [blame] | 1049 | struct rt6_info *rt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1050 | |
David Ahern | b6cdbc8 | 2018-03-29 17:44:57 -0700 | [diff] [blame] | 1051 | if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF) |
| 1052 | flags &= ~RT6_LOOKUP_F_IFACE; |
| 1053 | |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 1054 | rcu_read_lock(); |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 1055 | fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr); |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1056 | restart: |
David Ahern | 23fb93a | 2018-04-17 17:33:23 -0700 | [diff] [blame] | 1057 | f6i = rcu_dereference(fn->leaf); |
| 1058 | if (!f6i) { |
| 1059 | f6i = net->ipv6.fib6_null_entry; |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 1060 | } else { |
David Ahern | 23fb93a | 2018-04-17 17:33:23 -0700 | [diff] [blame] | 1061 | f6i = rt6_device_match(net, f6i, &fl6->saddr, |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 1062 | fl6->flowi6_oif, flags); |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 1063 | if (f6i->fib6_nsiblings && fl6->flowi6_oif == 0) |
David Ahern | 23fb93a | 2018-04-17 17:33:23 -0700 | [diff] [blame] | 1064 | f6i = rt6_multipath_select(net, f6i, fl6, |
| 1065 | fl6->flowi6_oif, skb, flags); |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 1066 | } |
David Ahern | 23fb93a | 2018-04-17 17:33:23 -0700 | [diff] [blame] | 1067 | if (f6i == net->ipv6.fib6_null_entry) { |
Martin KaFai Lau | a3c00e4 | 2014-10-20 13:42:43 -0700 | [diff] [blame] | 1068 | fn = fib6_backtrack(fn, &fl6->saddr); |
| 1069 | if (fn) |
| 1070 | goto restart; |
| 1071 | } |
David Ahern | 23fb93a | 2018-04-17 17:33:23 -0700 | [diff] [blame] | 1072 | |
Wei Wang | 2b760fc | 2017-10-06 12:06:03 -0700 | [diff] [blame] | 1073 | /* Search through exception table */ |
David Ahern | 23fb93a | 2018-04-17 17:33:23 -0700 | [diff] [blame] | 1074 | rt = rt6_find_cached_rt(f6i, &fl6->daddr, &fl6->saddr); |
| 1075 | if (rt) { |
David Ahern | dec9b0e | 2018-04-17 17:33:19 -0700 | [diff] [blame] | 1076 | if (ip6_hold_safe(net, &rt, true)) |
| 1077 | dst_use_noref(&rt->dst, jiffies); |
David Ahern | 23fb93a | 2018-04-17 17:33:23 -0700 | [diff] [blame] | 1078 | } else if (f6i == net->ipv6.fib6_null_entry) { |
David Ahern | dec9b0e | 2018-04-17 17:33:19 -0700 | [diff] [blame] | 1079 | rt = net->ipv6.ip6_null_entry; |
| 1080 | dst_hold(&rt->dst); |
David Ahern | 23fb93a | 2018-04-17 17:33:23 -0700 | [diff] [blame] | 1081 | } else { |
| 1082 | rt = ip6_create_rt_rcu(f6i); |
| 1083 | if (!rt) { |
| 1084 | rt = net->ipv6.ip6_null_entry; |
| 1085 | dst_hold(&rt->dst); |
| 1086 | } |
David Ahern | dec9b0e | 2018-04-17 17:33:19 -0700 | [diff] [blame] | 1087 | } |
Wei Wang | d3843fe | 2017-10-06 12:06:06 -0700 | [diff] [blame] | 1088 | |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 1089 | rcu_read_unlock(); |
David Ahern | b811580 | 2015-11-19 12:24:22 -0800 | [diff] [blame] | 1090 | |
Paolo Abeni | b65f164 | 2017-10-19 09:31:43 +0200 | [diff] [blame] | 1091 | trace_fib6_table_lookup(net, rt, table, fl6); |
David Ahern | b811580 | 2015-11-19 12:24:22 -0800 | [diff] [blame] | 1092 | |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1093 | return rt; |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1094 | } |
| 1095 | |
Ian Morris | 67ba415 | 2014-08-24 21:53:10 +0100 | [diff] [blame] | 1096 | struct dst_entry *ip6_route_lookup(struct net *net, struct flowi6 *fl6, |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 1097 | const struct sk_buff *skb, int flags) |
Florian Westphal | ea6e574 | 2011-09-05 16:05:44 +0200 | [diff] [blame] | 1098 | { |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 1099 | return fib6_rule_lookup(net, fl6, skb, flags, ip6_pol_route_lookup); |
Florian Westphal | ea6e574 | 2011-09-05 16:05:44 +0200 | [diff] [blame] | 1100 | } |
| 1101 | EXPORT_SYMBOL_GPL(ip6_route_lookup); |
| 1102 | |
YOSHIFUJI Hideaki | 9acd9f3 | 2008-04-10 15:42:10 +0900 | [diff] [blame] | 1103 | 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] | 1104 | const struct in6_addr *saddr, int oif, |
| 1105 | const struct sk_buff *skb, int strict) |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1106 | { |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 1107 | struct flowi6 fl6 = { |
| 1108 | .flowi6_oif = oif, |
| 1109 | .daddr = *daddr, |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1110 | }; |
| 1111 | struct dst_entry *dst; |
YOSHIFUJI Hideaki | 77d16f4 | 2006-08-23 17:25:05 -0700 | [diff] [blame] | 1112 | int flags = strict ? RT6_LOOKUP_F_IFACE : 0; |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1113 | |
Thomas Graf | adaa70b | 2006-10-13 15:01:03 -0700 | [diff] [blame] | 1114 | if (saddr) { |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 1115 | memcpy(&fl6.saddr, saddr, sizeof(*saddr)); |
Thomas Graf | adaa70b | 2006-10-13 15:01:03 -0700 | [diff] [blame] | 1116 | flags |= RT6_LOOKUP_F_HAS_SADDR; |
| 1117 | } |
| 1118 | |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 1119 | dst = fib6_rule_lookup(net, &fl6, skb, flags, ip6_pol_route_lookup); |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1120 | if (dst->error == 0) |
| 1121 | return (struct rt6_info *) dst; |
| 1122 | |
| 1123 | dst_release(dst); |
| 1124 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1125 | return NULL; |
| 1126 | } |
YOSHIFUJI Hideaki | 7159039 | 2007-02-22 22:05:40 +0900 | [diff] [blame] | 1127 | EXPORT_SYMBOL(rt6_lookup); |
| 1128 | |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1129 | /* ip6_ins_rt is called with FREE table->tb6_lock. |
Wei Wang | 1cfb71e | 2017-06-17 10:42:33 -0700 | [diff] [blame] | 1130 | * It takes new route entry, the addition fails by any reason the |
| 1131 | * route is released. |
| 1132 | * Caller must hold dst before calling it. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1133 | */ |
| 1134 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 1135 | 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] | 1136 | struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1137 | { |
| 1138 | int err; |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1139 | struct fib6_table *table; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1140 | |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 1141 | table = rt->fib6_table; |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 1142 | spin_lock_bh(&table->tb6_lock); |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 1143 | err = fib6_add(&table->tb6_root, rt, info, extack); |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 1144 | spin_unlock_bh(&table->tb6_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1145 | |
| 1146 | return err; |
| 1147 | } |
| 1148 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 1149 | int ip6_ins_rt(struct net *net, struct fib6_info *rt) |
Thomas Graf | 40e22e8 | 2006-08-22 00:00:45 -0700 | [diff] [blame] | 1150 | { |
David Ahern | afb1d4b5 | 2018-04-17 17:33:11 -0700 | [diff] [blame] | 1151 | struct nl_info info = { .nl_net = net, }; |
Florian Westphal | e715b6d | 2015-01-05 23:57:44 +0100 | [diff] [blame] | 1152 | |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 1153 | return __ip6_ins_rt(rt, &info, NULL); |
Thomas Graf | 40e22e8 | 2006-08-22 00:00:45 -0700 | [diff] [blame] | 1154 | } |
| 1155 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 1156 | static struct rt6_info *ip6_rt_cache_alloc(struct fib6_info *ort, |
Martin KaFai Lau | 8b9df26 | 2015-05-22 20:55:59 -0700 | [diff] [blame] | 1157 | const struct in6_addr *daddr, |
| 1158 | const struct in6_addr *saddr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1159 | { |
David Ahern | 4832c30 | 2017-08-17 12:17:20 -0700 | [diff] [blame] | 1160 | struct net_device *dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1161 | struct rt6_info *rt; |
| 1162 | |
| 1163 | /* |
| 1164 | * Clone the route. |
| 1165 | */ |
| 1166 | |
David Ahern | 4832c30 | 2017-08-17 12:17:20 -0700 | [diff] [blame] | 1167 | rcu_read_lock(); |
| 1168 | dev = ip6_rt_get_dev_rcu(ort); |
David Ahern | 93531c6 | 2018-04-17 17:33:25 -0700 | [diff] [blame] | 1169 | rt = ip6_dst_alloc(dev_net(dev), dev, 0); |
David Ahern | 4832c30 | 2017-08-17 12:17:20 -0700 | [diff] [blame] | 1170 | rcu_read_unlock(); |
Martin KaFai Lau | 83a09ab | 2015-05-22 20:56:05 -0700 | [diff] [blame] | 1171 | if (!rt) |
| 1172 | return NULL; |
| 1173 | |
| 1174 | ip6_rt_copy_init(rt, ort); |
| 1175 | rt->rt6i_flags |= RTF_CACHE; |
Martin KaFai Lau | 83a09ab | 2015-05-22 20:56:05 -0700 | [diff] [blame] | 1176 | rt->dst.flags |= DST_HOST; |
| 1177 | rt->rt6i_dst.addr = *daddr; |
| 1178 | rt->rt6i_dst.plen = 128; |
| 1179 | |
| 1180 | if (!rt6_is_gw_or_nonexthop(ort)) { |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 1181 | if (ort->fib6_dst.plen != 128 && |
| 1182 | ipv6_addr_equal(&ort->fib6_dst.addr, daddr)) |
Martin KaFai Lau | 83a09ab | 2015-05-22 20:56:05 -0700 | [diff] [blame] | 1183 | rt->rt6i_flags |= RTF_ANYCAST; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1184 | #ifdef CONFIG_IPV6_SUBTREES |
Martin KaFai Lau | 83a09ab | 2015-05-22 20:56:05 -0700 | [diff] [blame] | 1185 | if (rt->rt6i_src.plen && saddr) { |
| 1186 | rt->rt6i_src.addr = *saddr; |
| 1187 | rt->rt6i_src.plen = 128; |
Martin KaFai Lau | 8b9df26 | 2015-05-22 20:55:59 -0700 | [diff] [blame] | 1188 | } |
Martin KaFai Lau | 83a09ab | 2015-05-22 20:56:05 -0700 | [diff] [blame] | 1189 | #endif |
YOSHIFUJI Hideaki | 95a9a5b | 2006-03-20 16:55:51 -0800 | [diff] [blame] | 1190 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1191 | |
YOSHIFUJI Hideaki | 95a9a5b | 2006-03-20 16:55:51 -0800 | [diff] [blame] | 1192 | return rt; |
| 1193 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1194 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 1195 | 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] | 1196 | { |
David Ahern | 3b6761d | 2018-04-17 17:33:20 -0700 | [diff] [blame] | 1197 | unsigned short flags = fib6_info_dst_flags(rt); |
David Ahern | 4832c30 | 2017-08-17 12:17:20 -0700 | [diff] [blame] | 1198 | struct net_device *dev; |
Martin KaFai Lau | d52d399 | 2015-05-22 20:56:06 -0700 | [diff] [blame] | 1199 | struct rt6_info *pcpu_rt; |
| 1200 | |
David Ahern | 4832c30 | 2017-08-17 12:17:20 -0700 | [diff] [blame] | 1201 | rcu_read_lock(); |
| 1202 | dev = ip6_rt_get_dev_rcu(rt); |
David Ahern | 93531c6 | 2018-04-17 17:33:25 -0700 | [diff] [blame] | 1203 | pcpu_rt = ip6_dst_alloc(dev_net(dev), dev, flags); |
David Ahern | 4832c30 | 2017-08-17 12:17:20 -0700 | [diff] [blame] | 1204 | rcu_read_unlock(); |
Martin KaFai Lau | d52d399 | 2015-05-22 20:56:06 -0700 | [diff] [blame] | 1205 | if (!pcpu_rt) |
| 1206 | return NULL; |
| 1207 | ip6_rt_copy_init(pcpu_rt, rt); |
Martin KaFai Lau | d52d399 | 2015-05-22 20:56:06 -0700 | [diff] [blame] | 1208 | pcpu_rt->rt6i_flags |= RTF_PCPU; |
| 1209 | return pcpu_rt; |
| 1210 | } |
| 1211 | |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 1212 | /* It should be called with rcu_read_lock() acquired */ |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 1213 | 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] | 1214 | { |
Martin KaFai Lau | a73e419 | 2015-08-14 11:05:53 -0700 | [diff] [blame] | 1215 | struct rt6_info *pcpu_rt, **p; |
Martin KaFai Lau | d52d399 | 2015-05-22 20:56:06 -0700 | [diff] [blame] | 1216 | |
| 1217 | p = this_cpu_ptr(rt->rt6i_pcpu); |
| 1218 | pcpu_rt = *p; |
| 1219 | |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 1220 | if (pcpu_rt) |
| 1221 | ip6_hold_safe(NULL, &pcpu_rt, false); |
Wei Wang | d3843fe | 2017-10-06 12:06:06 -0700 | [diff] [blame] | 1222 | |
Martin KaFai Lau | a73e419 | 2015-08-14 11:05:53 -0700 | [diff] [blame] | 1223 | return pcpu_rt; |
| 1224 | } |
| 1225 | |
David Ahern | afb1d4b5 | 2018-04-17 17:33:11 -0700 | [diff] [blame] | 1226 | static struct rt6_info *rt6_make_pcpu_route(struct net *net, |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 1227 | struct fib6_info *rt) |
Martin KaFai Lau | a73e419 | 2015-08-14 11:05:53 -0700 | [diff] [blame] | 1228 | { |
| 1229 | struct rt6_info *pcpu_rt, *prev, **p; |
Martin KaFai Lau | d52d399 | 2015-05-22 20:56:06 -0700 | [diff] [blame] | 1230 | |
| 1231 | pcpu_rt = ip6_rt_pcpu_alloc(rt); |
| 1232 | if (!pcpu_rt) { |
Martin KaFai Lau | 9c7370a | 2015-08-14 11:05:54 -0700 | [diff] [blame] | 1233 | dst_hold(&net->ipv6.ip6_null_entry->dst); |
| 1234 | return net->ipv6.ip6_null_entry; |
Martin KaFai Lau | d52d399 | 2015-05-22 20:56:06 -0700 | [diff] [blame] | 1235 | } |
| 1236 | |
Martin KaFai Lau | d52d399 | 2015-05-22 20:56:06 -0700 | [diff] [blame] | 1237 | dst_hold(&pcpu_rt->dst); |
Wei Wang | a94b936 | 2017-10-06 12:06:04 -0700 | [diff] [blame] | 1238 | p = this_cpu_ptr(rt->rt6i_pcpu); |
| 1239 | prev = cmpxchg(p, NULL, pcpu_rt); |
Eric Dumazet | 951f788 | 2017-10-08 21:07:18 -0700 | [diff] [blame] | 1240 | BUG_ON(prev); |
Wei Wang | a94b936 | 2017-10-06 12:06:04 -0700 | [diff] [blame] | 1241 | |
Martin KaFai Lau | d52d399 | 2015-05-22 20:56:06 -0700 | [diff] [blame] | 1242 | return pcpu_rt; |
| 1243 | } |
| 1244 | |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1245 | /* exception hash table implementation |
| 1246 | */ |
| 1247 | static DEFINE_SPINLOCK(rt6_exception_lock); |
| 1248 | |
| 1249 | /* Remove rt6_ex from hash table and free the memory |
| 1250 | * Caller must hold rt6_exception_lock |
| 1251 | */ |
| 1252 | static void rt6_remove_exception(struct rt6_exception_bucket *bucket, |
| 1253 | struct rt6_exception *rt6_ex) |
| 1254 | { |
Colin Ian King | b2427e6 | 2017-10-10 18:01:16 +0100 | [diff] [blame] | 1255 | struct net *net; |
Wei Wang | 81eb844 | 2017-10-06 12:06:11 -0700 | [diff] [blame] | 1256 | |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1257 | if (!bucket || !rt6_ex) |
| 1258 | return; |
Colin Ian King | b2427e6 | 2017-10-10 18:01:16 +0100 | [diff] [blame] | 1259 | |
| 1260 | net = dev_net(rt6_ex->rt6i->dst.dev); |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1261 | hlist_del_rcu(&rt6_ex->hlist); |
David Ahern | 77634cc | 2018-04-17 17:33:27 -0700 | [diff] [blame] | 1262 | dst_release(&rt6_ex->rt6i->dst); |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1263 | kfree_rcu(rt6_ex, rcu); |
| 1264 | WARN_ON_ONCE(!bucket->depth); |
| 1265 | bucket->depth--; |
Wei Wang | 81eb844 | 2017-10-06 12:06:11 -0700 | [diff] [blame] | 1266 | net->ipv6.rt6_stats->fib_rt_cache--; |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1267 | } |
| 1268 | |
| 1269 | /* Remove oldest rt6_ex in bucket and free the memory |
| 1270 | * Caller must hold rt6_exception_lock |
| 1271 | */ |
| 1272 | static void rt6_exception_remove_oldest(struct rt6_exception_bucket *bucket) |
| 1273 | { |
| 1274 | struct rt6_exception *rt6_ex, *oldest = NULL; |
| 1275 | |
| 1276 | if (!bucket) |
| 1277 | return; |
| 1278 | |
| 1279 | hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) { |
| 1280 | if (!oldest || time_before(rt6_ex->stamp, oldest->stamp)) |
| 1281 | oldest = rt6_ex; |
| 1282 | } |
| 1283 | rt6_remove_exception(bucket, oldest); |
| 1284 | } |
| 1285 | |
| 1286 | static u32 rt6_exception_hash(const struct in6_addr *dst, |
| 1287 | const struct in6_addr *src) |
| 1288 | { |
| 1289 | static u32 seed __read_mostly; |
| 1290 | u32 val; |
| 1291 | |
| 1292 | net_get_random_once(&seed, sizeof(seed)); |
| 1293 | val = jhash(dst, sizeof(*dst), seed); |
| 1294 | |
| 1295 | #ifdef CONFIG_IPV6_SUBTREES |
| 1296 | if (src) |
| 1297 | val = jhash(src, sizeof(*src), val); |
| 1298 | #endif |
| 1299 | return hash_32(val, FIB6_EXCEPTION_BUCKET_SIZE_SHIFT); |
| 1300 | } |
| 1301 | |
| 1302 | /* Helper function to find the cached rt in the hash table |
| 1303 | * and update bucket pointer to point to the bucket for this |
| 1304 | * (daddr, saddr) pair |
| 1305 | * Caller must hold rt6_exception_lock |
| 1306 | */ |
| 1307 | static struct rt6_exception * |
| 1308 | __rt6_find_exception_spinlock(struct rt6_exception_bucket **bucket, |
| 1309 | const struct in6_addr *daddr, |
| 1310 | const struct in6_addr *saddr) |
| 1311 | { |
| 1312 | struct rt6_exception *rt6_ex; |
| 1313 | u32 hval; |
| 1314 | |
| 1315 | if (!(*bucket) || !daddr) |
| 1316 | return NULL; |
| 1317 | |
| 1318 | hval = rt6_exception_hash(daddr, saddr); |
| 1319 | *bucket += hval; |
| 1320 | |
| 1321 | hlist_for_each_entry(rt6_ex, &(*bucket)->chain, hlist) { |
| 1322 | struct rt6_info *rt6 = rt6_ex->rt6i; |
| 1323 | bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr); |
| 1324 | |
| 1325 | #ifdef CONFIG_IPV6_SUBTREES |
| 1326 | if (matched && saddr) |
| 1327 | matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr); |
| 1328 | #endif |
| 1329 | if (matched) |
| 1330 | return rt6_ex; |
| 1331 | } |
| 1332 | return NULL; |
| 1333 | } |
| 1334 | |
| 1335 | /* Helper function to find the cached rt in the hash table |
| 1336 | * and update bucket pointer to point to the bucket for this |
| 1337 | * (daddr, saddr) pair |
| 1338 | * Caller must hold rcu_read_lock() |
| 1339 | */ |
| 1340 | static struct rt6_exception * |
| 1341 | __rt6_find_exception_rcu(struct rt6_exception_bucket **bucket, |
| 1342 | const struct in6_addr *daddr, |
| 1343 | const struct in6_addr *saddr) |
| 1344 | { |
| 1345 | struct rt6_exception *rt6_ex; |
| 1346 | u32 hval; |
| 1347 | |
| 1348 | WARN_ON_ONCE(!rcu_read_lock_held()); |
| 1349 | |
| 1350 | if (!(*bucket) || !daddr) |
| 1351 | return NULL; |
| 1352 | |
| 1353 | hval = rt6_exception_hash(daddr, saddr); |
| 1354 | *bucket += hval; |
| 1355 | |
| 1356 | hlist_for_each_entry_rcu(rt6_ex, &(*bucket)->chain, hlist) { |
| 1357 | struct rt6_info *rt6 = rt6_ex->rt6i; |
| 1358 | bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr); |
| 1359 | |
| 1360 | #ifdef CONFIG_IPV6_SUBTREES |
| 1361 | if (matched && saddr) |
| 1362 | matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr); |
| 1363 | #endif |
| 1364 | if (matched) |
| 1365 | return rt6_ex; |
| 1366 | } |
| 1367 | return NULL; |
| 1368 | } |
| 1369 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 1370 | static unsigned int fib6_mtu(const struct fib6_info *rt) |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 1371 | { |
| 1372 | unsigned int mtu; |
| 1373 | |
David Ahern | dcd1f57 | 2018-04-18 15:39:05 -0700 | [diff] [blame^] | 1374 | if (rt->fib6_pmtu) { |
| 1375 | mtu = rt->fib6_pmtu; |
| 1376 | } else { |
| 1377 | struct net_device *dev = fib6_info_nh_dev(rt); |
| 1378 | struct inet6_dev *idev; |
| 1379 | |
| 1380 | rcu_read_lock(); |
| 1381 | idev = __in6_dev_get(dev); |
| 1382 | mtu = idev->cnf.mtu6; |
| 1383 | rcu_read_unlock(); |
| 1384 | } |
| 1385 | |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 1386 | mtu = min_t(unsigned int, mtu, IP6_MAX_MTU); |
| 1387 | |
| 1388 | return mtu - lwtunnel_headroom(rt->fib6_nh.nh_lwtstate, mtu); |
| 1389 | } |
| 1390 | |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1391 | static int rt6_insert_exception(struct rt6_info *nrt, |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 1392 | struct fib6_info *ort) |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1393 | { |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 1394 | struct net *net = dev_net(nrt->dst.dev); |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1395 | struct rt6_exception_bucket *bucket; |
| 1396 | struct in6_addr *src_key = NULL; |
| 1397 | struct rt6_exception *rt6_ex; |
| 1398 | int err = 0; |
| 1399 | |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1400 | spin_lock_bh(&rt6_exception_lock); |
| 1401 | |
| 1402 | if (ort->exception_bucket_flushed) { |
| 1403 | err = -EINVAL; |
| 1404 | goto out; |
| 1405 | } |
| 1406 | |
| 1407 | bucket = rcu_dereference_protected(ort->rt6i_exception_bucket, |
| 1408 | lockdep_is_held(&rt6_exception_lock)); |
| 1409 | if (!bucket) { |
| 1410 | bucket = kcalloc(FIB6_EXCEPTION_BUCKET_SIZE, sizeof(*bucket), |
| 1411 | GFP_ATOMIC); |
| 1412 | if (!bucket) { |
| 1413 | err = -ENOMEM; |
| 1414 | goto out; |
| 1415 | } |
| 1416 | rcu_assign_pointer(ort->rt6i_exception_bucket, bucket); |
| 1417 | } |
| 1418 | |
| 1419 | #ifdef CONFIG_IPV6_SUBTREES |
| 1420 | /* rt6i_src.plen != 0 indicates ort is in subtree |
| 1421 | * and exception table is indexed by a hash of |
| 1422 | * both rt6i_dst and rt6i_src. |
| 1423 | * Otherwise, the exception table is indexed by |
| 1424 | * a hash of only rt6i_dst. |
| 1425 | */ |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 1426 | if (ort->fib6_src.plen) |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1427 | src_key = &nrt->rt6i_src.addr; |
| 1428 | #endif |
Wei Wang | 60006a4 | 2017-10-06 12:05:58 -0700 | [diff] [blame] | 1429 | |
| 1430 | /* Update rt6i_prefsrc as it could be changed |
| 1431 | * in rt6_remove_prefsrc() |
| 1432 | */ |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 1433 | nrt->rt6i_prefsrc = ort->fib6_prefsrc; |
Wei Wang | f5bbe7e | 2017-10-06 12:05:59 -0700 | [diff] [blame] | 1434 | /* rt6_mtu_change() might lower mtu on ort. |
| 1435 | * Only insert this exception route if its mtu |
| 1436 | * is less than ort's mtu value. |
| 1437 | */ |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 1438 | if (dst_metric_raw(&nrt->dst, RTAX_MTU) >= fib6_mtu(ort)) { |
Wei Wang | f5bbe7e | 2017-10-06 12:05:59 -0700 | [diff] [blame] | 1439 | err = -EINVAL; |
| 1440 | goto out; |
| 1441 | } |
Wei Wang | 60006a4 | 2017-10-06 12:05:58 -0700 | [diff] [blame] | 1442 | |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1443 | rt6_ex = __rt6_find_exception_spinlock(&bucket, &nrt->rt6i_dst.addr, |
| 1444 | src_key); |
| 1445 | if (rt6_ex) |
| 1446 | rt6_remove_exception(bucket, rt6_ex); |
| 1447 | |
| 1448 | rt6_ex = kzalloc(sizeof(*rt6_ex), GFP_ATOMIC); |
| 1449 | if (!rt6_ex) { |
| 1450 | err = -ENOMEM; |
| 1451 | goto out; |
| 1452 | } |
| 1453 | rt6_ex->rt6i = nrt; |
| 1454 | rt6_ex->stamp = jiffies; |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1455 | hlist_add_head_rcu(&rt6_ex->hlist, &bucket->chain); |
| 1456 | bucket->depth++; |
Wei Wang | 81eb844 | 2017-10-06 12:06:11 -0700 | [diff] [blame] | 1457 | net->ipv6.rt6_stats->fib_rt_cache++; |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1458 | |
| 1459 | if (bucket->depth > FIB6_MAX_DEPTH) |
| 1460 | rt6_exception_remove_oldest(bucket); |
| 1461 | |
| 1462 | out: |
| 1463 | spin_unlock_bh(&rt6_exception_lock); |
| 1464 | |
| 1465 | /* Update fn->fn_sernum to invalidate all cached dst */ |
Paolo Abeni | b886d5f | 2017-10-19 16:07:10 +0200 | [diff] [blame] | 1466 | if (!err) { |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 1467 | spin_lock_bh(&ort->fib6_table->tb6_lock); |
David Ahern | 7aef685 | 2018-04-17 17:33:10 -0700 | [diff] [blame] | 1468 | fib6_update_sernum(net, ort); |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 1469 | spin_unlock_bh(&ort->fib6_table->tb6_lock); |
Paolo Abeni | b886d5f | 2017-10-19 16:07:10 +0200 | [diff] [blame] | 1470 | fib6_force_start_gc(net); |
| 1471 | } |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1472 | |
| 1473 | return err; |
| 1474 | } |
| 1475 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 1476 | void rt6_flush_exceptions(struct fib6_info *rt) |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1477 | { |
| 1478 | struct rt6_exception_bucket *bucket; |
| 1479 | struct rt6_exception *rt6_ex; |
| 1480 | struct hlist_node *tmp; |
| 1481 | int i; |
| 1482 | |
| 1483 | spin_lock_bh(&rt6_exception_lock); |
| 1484 | /* Prevent rt6_insert_exception() to recreate the bucket list */ |
| 1485 | rt->exception_bucket_flushed = 1; |
| 1486 | |
| 1487 | bucket = rcu_dereference_protected(rt->rt6i_exception_bucket, |
| 1488 | lockdep_is_held(&rt6_exception_lock)); |
| 1489 | if (!bucket) |
| 1490 | goto out; |
| 1491 | |
| 1492 | for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) { |
| 1493 | hlist_for_each_entry_safe(rt6_ex, tmp, &bucket->chain, hlist) |
| 1494 | rt6_remove_exception(bucket, rt6_ex); |
| 1495 | WARN_ON_ONCE(bucket->depth); |
| 1496 | bucket++; |
| 1497 | } |
| 1498 | |
| 1499 | out: |
| 1500 | spin_unlock_bh(&rt6_exception_lock); |
| 1501 | } |
| 1502 | |
| 1503 | /* Find cached rt in the hash table inside passed in rt |
| 1504 | * Caller has to hold rcu_read_lock() |
| 1505 | */ |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 1506 | static struct rt6_info *rt6_find_cached_rt(struct fib6_info *rt, |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1507 | struct in6_addr *daddr, |
| 1508 | struct in6_addr *saddr) |
| 1509 | { |
| 1510 | struct rt6_exception_bucket *bucket; |
| 1511 | struct in6_addr *src_key = NULL; |
| 1512 | struct rt6_exception *rt6_ex; |
| 1513 | struct rt6_info *res = NULL; |
| 1514 | |
| 1515 | bucket = rcu_dereference(rt->rt6i_exception_bucket); |
| 1516 | |
| 1517 | #ifdef CONFIG_IPV6_SUBTREES |
| 1518 | /* rt6i_src.plen != 0 indicates rt is in subtree |
| 1519 | * and exception table is indexed by a hash of |
| 1520 | * both rt6i_dst and rt6i_src. |
| 1521 | * Otherwise, the exception table is indexed by |
| 1522 | * a hash of only rt6i_dst. |
| 1523 | */ |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 1524 | if (rt->fib6_src.plen) |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1525 | src_key = saddr; |
| 1526 | #endif |
| 1527 | rt6_ex = __rt6_find_exception_rcu(&bucket, daddr, src_key); |
| 1528 | |
| 1529 | if (rt6_ex && !rt6_check_expired(rt6_ex->rt6i)) |
| 1530 | res = rt6_ex->rt6i; |
| 1531 | |
| 1532 | return res; |
| 1533 | } |
| 1534 | |
| 1535 | /* 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] | 1536 | static int rt6_remove_exception_rt(struct rt6_info *rt) |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1537 | { |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1538 | struct rt6_exception_bucket *bucket; |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 1539 | struct fib6_info *from = rt->from; |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1540 | struct in6_addr *src_key = NULL; |
| 1541 | struct rt6_exception *rt6_ex; |
| 1542 | int err; |
| 1543 | |
| 1544 | if (!from || |
Colin Ian King | 442d713 | 2017-10-10 19:10:30 +0100 | [diff] [blame] | 1545 | !(rt->rt6i_flags & RTF_CACHE)) |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1546 | return -EINVAL; |
| 1547 | |
| 1548 | if (!rcu_access_pointer(from->rt6i_exception_bucket)) |
| 1549 | return -ENOENT; |
| 1550 | |
| 1551 | spin_lock_bh(&rt6_exception_lock); |
| 1552 | bucket = rcu_dereference_protected(from->rt6i_exception_bucket, |
| 1553 | lockdep_is_held(&rt6_exception_lock)); |
| 1554 | #ifdef CONFIG_IPV6_SUBTREES |
| 1555 | /* rt6i_src.plen != 0 indicates 'from' is in subtree |
| 1556 | * and exception table is indexed by a hash of |
| 1557 | * both rt6i_dst and rt6i_src. |
| 1558 | * Otherwise, the exception table is indexed by |
| 1559 | * a hash of only rt6i_dst. |
| 1560 | */ |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 1561 | if (from->fib6_src.plen) |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1562 | src_key = &rt->rt6i_src.addr; |
| 1563 | #endif |
| 1564 | rt6_ex = __rt6_find_exception_spinlock(&bucket, |
| 1565 | &rt->rt6i_dst.addr, |
| 1566 | src_key); |
| 1567 | if (rt6_ex) { |
| 1568 | rt6_remove_exception(bucket, rt6_ex); |
| 1569 | err = 0; |
| 1570 | } else { |
| 1571 | err = -ENOENT; |
| 1572 | } |
| 1573 | |
| 1574 | spin_unlock_bh(&rt6_exception_lock); |
| 1575 | return err; |
| 1576 | } |
| 1577 | |
| 1578 | /* Find rt6_ex which contains the passed in rt cache and |
| 1579 | * refresh its stamp |
| 1580 | */ |
| 1581 | static void rt6_update_exception_stamp_rt(struct rt6_info *rt) |
| 1582 | { |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1583 | struct rt6_exception_bucket *bucket; |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 1584 | struct fib6_info *from = rt->from; |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1585 | struct in6_addr *src_key = NULL; |
| 1586 | struct rt6_exception *rt6_ex; |
| 1587 | |
| 1588 | if (!from || |
Colin Ian King | 442d713 | 2017-10-10 19:10:30 +0100 | [diff] [blame] | 1589 | !(rt->rt6i_flags & RTF_CACHE)) |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1590 | return; |
| 1591 | |
| 1592 | rcu_read_lock(); |
| 1593 | bucket = rcu_dereference(from->rt6i_exception_bucket); |
| 1594 | |
| 1595 | #ifdef CONFIG_IPV6_SUBTREES |
| 1596 | /* rt6i_src.plen != 0 indicates 'from' is in subtree |
| 1597 | * and exception table is indexed by a hash of |
| 1598 | * both rt6i_dst and rt6i_src. |
| 1599 | * Otherwise, the exception table is indexed by |
| 1600 | * a hash of only rt6i_dst. |
| 1601 | */ |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 1602 | if (from->fib6_src.plen) |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1603 | src_key = &rt->rt6i_src.addr; |
| 1604 | #endif |
| 1605 | rt6_ex = __rt6_find_exception_rcu(&bucket, |
| 1606 | &rt->rt6i_dst.addr, |
| 1607 | src_key); |
| 1608 | if (rt6_ex) |
| 1609 | rt6_ex->stamp = jiffies; |
| 1610 | |
| 1611 | rcu_read_unlock(); |
| 1612 | } |
| 1613 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 1614 | static void rt6_exceptions_remove_prefsrc(struct fib6_info *rt) |
Wei Wang | 60006a4 | 2017-10-06 12:05:58 -0700 | [diff] [blame] | 1615 | { |
| 1616 | struct rt6_exception_bucket *bucket; |
| 1617 | struct rt6_exception *rt6_ex; |
| 1618 | int i; |
| 1619 | |
| 1620 | bucket = rcu_dereference_protected(rt->rt6i_exception_bucket, |
| 1621 | lockdep_is_held(&rt6_exception_lock)); |
| 1622 | |
| 1623 | if (bucket) { |
| 1624 | for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) { |
| 1625 | hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) { |
| 1626 | rt6_ex->rt6i->rt6i_prefsrc.plen = 0; |
| 1627 | } |
| 1628 | bucket++; |
| 1629 | } |
| 1630 | } |
| 1631 | } |
| 1632 | |
Stefano Brivio | e9fa149 | 2018-03-06 11:10:19 +0100 | [diff] [blame] | 1633 | static bool rt6_mtu_change_route_allowed(struct inet6_dev *idev, |
| 1634 | struct rt6_info *rt, int mtu) |
| 1635 | { |
| 1636 | /* If the new MTU is lower than the route PMTU, this new MTU will be the |
| 1637 | * lowest MTU in the path: always allow updating the route PMTU to |
| 1638 | * reflect PMTU decreases. |
| 1639 | * |
| 1640 | * If the new MTU is higher, and the route PMTU is equal to the local |
| 1641 | * MTU, this means the old MTU is the lowest in the path, so allow |
| 1642 | * updating it: if other nodes now have lower MTUs, PMTU discovery will |
| 1643 | * handle this. |
| 1644 | */ |
| 1645 | |
| 1646 | if (dst_mtu(&rt->dst) >= mtu) |
| 1647 | return true; |
| 1648 | |
| 1649 | if (dst_mtu(&rt->dst) == idev->cnf.mtu6) |
| 1650 | return true; |
| 1651 | |
| 1652 | return false; |
| 1653 | } |
| 1654 | |
| 1655 | static void rt6_exceptions_update_pmtu(struct inet6_dev *idev, |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 1656 | struct fib6_info *rt, int mtu) |
Wei Wang | f5bbe7e | 2017-10-06 12:05:59 -0700 | [diff] [blame] | 1657 | { |
| 1658 | struct rt6_exception_bucket *bucket; |
| 1659 | struct rt6_exception *rt6_ex; |
| 1660 | int i; |
| 1661 | |
| 1662 | bucket = rcu_dereference_protected(rt->rt6i_exception_bucket, |
| 1663 | lockdep_is_held(&rt6_exception_lock)); |
| 1664 | |
Stefano Brivio | e9fa149 | 2018-03-06 11:10:19 +0100 | [diff] [blame] | 1665 | if (!bucket) |
| 1666 | return; |
| 1667 | |
| 1668 | for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) { |
| 1669 | hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) { |
| 1670 | struct rt6_info *entry = rt6_ex->rt6i; |
| 1671 | |
| 1672 | /* For RTF_CACHE with rt6i_pmtu == 0 (i.e. a redirected |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 1673 | * route), the metrics of its rt->from have already |
Stefano Brivio | e9fa149 | 2018-03-06 11:10:19 +0100 | [diff] [blame] | 1674 | * been updated. |
| 1675 | */ |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 1676 | if (dst_metric_raw(&entry->dst, RTAX_MTU) && |
Stefano Brivio | e9fa149 | 2018-03-06 11:10:19 +0100 | [diff] [blame] | 1677 | rt6_mtu_change_route_allowed(idev, entry, mtu)) |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 1678 | dst_metric_set(&entry->dst, RTAX_MTU, mtu); |
Wei Wang | f5bbe7e | 2017-10-06 12:05:59 -0700 | [diff] [blame] | 1679 | } |
Stefano Brivio | e9fa149 | 2018-03-06 11:10:19 +0100 | [diff] [blame] | 1680 | bucket++; |
Wei Wang | f5bbe7e | 2017-10-06 12:05:59 -0700 | [diff] [blame] | 1681 | } |
| 1682 | } |
| 1683 | |
Wei Wang | b16cb45 | 2017-10-06 12:06:00 -0700 | [diff] [blame] | 1684 | #define RTF_CACHE_GATEWAY (RTF_GATEWAY | RTF_CACHE) |
| 1685 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 1686 | static void rt6_exceptions_clean_tohost(struct fib6_info *rt, |
Wei Wang | b16cb45 | 2017-10-06 12:06:00 -0700 | [diff] [blame] | 1687 | struct in6_addr *gateway) |
| 1688 | { |
| 1689 | struct rt6_exception_bucket *bucket; |
| 1690 | struct rt6_exception *rt6_ex; |
| 1691 | struct hlist_node *tmp; |
| 1692 | int i; |
| 1693 | |
| 1694 | if (!rcu_access_pointer(rt->rt6i_exception_bucket)) |
| 1695 | return; |
| 1696 | |
| 1697 | spin_lock_bh(&rt6_exception_lock); |
| 1698 | bucket = rcu_dereference_protected(rt->rt6i_exception_bucket, |
| 1699 | lockdep_is_held(&rt6_exception_lock)); |
| 1700 | |
| 1701 | if (bucket) { |
| 1702 | for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) { |
| 1703 | hlist_for_each_entry_safe(rt6_ex, tmp, |
| 1704 | &bucket->chain, hlist) { |
| 1705 | struct rt6_info *entry = rt6_ex->rt6i; |
| 1706 | |
| 1707 | if ((entry->rt6i_flags & RTF_CACHE_GATEWAY) == |
| 1708 | RTF_CACHE_GATEWAY && |
| 1709 | ipv6_addr_equal(gateway, |
| 1710 | &entry->rt6i_gateway)) { |
| 1711 | rt6_remove_exception(bucket, rt6_ex); |
| 1712 | } |
| 1713 | } |
| 1714 | bucket++; |
| 1715 | } |
| 1716 | } |
| 1717 | |
| 1718 | spin_unlock_bh(&rt6_exception_lock); |
| 1719 | } |
| 1720 | |
Wei Wang | c757faa | 2017-10-06 12:06:01 -0700 | [diff] [blame] | 1721 | static void rt6_age_examine_exception(struct rt6_exception_bucket *bucket, |
| 1722 | struct rt6_exception *rt6_ex, |
| 1723 | struct fib6_gc_args *gc_args, |
| 1724 | unsigned long now) |
| 1725 | { |
| 1726 | struct rt6_info *rt = rt6_ex->rt6i; |
| 1727 | |
Paolo Abeni | 1859bac | 2017-10-19 16:07:11 +0200 | [diff] [blame] | 1728 | /* we are pruning and obsoleting aged-out and non gateway exceptions |
| 1729 | * even if others have still references to them, so that on next |
| 1730 | * dst_check() such references can be dropped. |
| 1731 | * EXPIRES exceptions - e.g. pmtu-generated ones are pruned when |
| 1732 | * expired, independently from their aging, as per RFC 8201 section 4 |
| 1733 | */ |
Wei Wang | 31afeb4 | 2018-01-26 11:40:17 -0800 | [diff] [blame] | 1734 | if (!(rt->rt6i_flags & RTF_EXPIRES)) { |
| 1735 | if (time_after_eq(now, rt->dst.lastuse + gc_args->timeout)) { |
| 1736 | RT6_TRACE("aging clone %p\n", rt); |
| 1737 | rt6_remove_exception(bucket, rt6_ex); |
| 1738 | return; |
| 1739 | } |
| 1740 | } else if (time_after(jiffies, rt->dst.expires)) { |
| 1741 | RT6_TRACE("purging expired route %p\n", rt); |
Wei Wang | c757faa | 2017-10-06 12:06:01 -0700 | [diff] [blame] | 1742 | rt6_remove_exception(bucket, rt6_ex); |
| 1743 | return; |
Wei Wang | 31afeb4 | 2018-01-26 11:40:17 -0800 | [diff] [blame] | 1744 | } |
| 1745 | |
| 1746 | if (rt->rt6i_flags & RTF_GATEWAY) { |
Wei Wang | c757faa | 2017-10-06 12:06:01 -0700 | [diff] [blame] | 1747 | struct neighbour *neigh; |
| 1748 | __u8 neigh_flags = 0; |
| 1749 | |
Eric Dumazet | 1bfa26f | 2018-03-23 07:56:58 -0700 | [diff] [blame] | 1750 | neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway); |
| 1751 | if (neigh) |
Wei Wang | c757faa | 2017-10-06 12:06:01 -0700 | [diff] [blame] | 1752 | neigh_flags = neigh->flags; |
Eric Dumazet | 1bfa26f | 2018-03-23 07:56:58 -0700 | [diff] [blame] | 1753 | |
Wei Wang | c757faa | 2017-10-06 12:06:01 -0700 | [diff] [blame] | 1754 | if (!(neigh_flags & NTF_ROUTER)) { |
| 1755 | RT6_TRACE("purging route %p via non-router but gateway\n", |
| 1756 | rt); |
| 1757 | rt6_remove_exception(bucket, rt6_ex); |
| 1758 | return; |
| 1759 | } |
| 1760 | } |
Wei Wang | 31afeb4 | 2018-01-26 11:40:17 -0800 | [diff] [blame] | 1761 | |
Wei Wang | c757faa | 2017-10-06 12:06:01 -0700 | [diff] [blame] | 1762 | gc_args->more++; |
| 1763 | } |
| 1764 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 1765 | void rt6_age_exceptions(struct fib6_info *rt, |
Wei Wang | c757faa | 2017-10-06 12:06:01 -0700 | [diff] [blame] | 1766 | struct fib6_gc_args *gc_args, |
| 1767 | unsigned long now) |
| 1768 | { |
| 1769 | struct rt6_exception_bucket *bucket; |
| 1770 | struct rt6_exception *rt6_ex; |
| 1771 | struct hlist_node *tmp; |
| 1772 | int i; |
| 1773 | |
| 1774 | if (!rcu_access_pointer(rt->rt6i_exception_bucket)) |
| 1775 | return; |
| 1776 | |
Eric Dumazet | 1bfa26f | 2018-03-23 07:56:58 -0700 | [diff] [blame] | 1777 | rcu_read_lock_bh(); |
| 1778 | spin_lock(&rt6_exception_lock); |
Wei Wang | c757faa | 2017-10-06 12:06:01 -0700 | [diff] [blame] | 1779 | bucket = rcu_dereference_protected(rt->rt6i_exception_bucket, |
| 1780 | lockdep_is_held(&rt6_exception_lock)); |
| 1781 | |
| 1782 | if (bucket) { |
| 1783 | for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) { |
| 1784 | hlist_for_each_entry_safe(rt6_ex, tmp, |
| 1785 | &bucket->chain, hlist) { |
| 1786 | rt6_age_examine_exception(bucket, rt6_ex, |
| 1787 | gc_args, now); |
| 1788 | } |
| 1789 | bucket++; |
| 1790 | } |
| 1791 | } |
Eric Dumazet | 1bfa26f | 2018-03-23 07:56:58 -0700 | [diff] [blame] | 1792 | spin_unlock(&rt6_exception_lock); |
| 1793 | rcu_read_unlock_bh(); |
Wei Wang | c757faa | 2017-10-06 12:06:01 -0700 | [diff] [blame] | 1794 | } |
| 1795 | |
David Ahern | 9ff7438 | 2016-06-13 13:44:19 -0700 | [diff] [blame] | 1796 | struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table, |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 1797 | int oif, struct flowi6 *fl6, |
| 1798 | const struct sk_buff *skb, int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1799 | { |
Martin KaFai Lau | 367efcb | 2014-10-20 13:42:45 -0700 | [diff] [blame] | 1800 | struct fib6_node *fn, *saved_fn; |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 1801 | struct fib6_info *f6i; |
David Ahern | 23fb93a | 2018-04-17 17:33:23 -0700 | [diff] [blame] | 1802 | struct rt6_info *rt; |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1803 | int strict = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1804 | |
YOSHIFUJI Hideaki | 77d16f4 | 2006-08-23 17:25:05 -0700 | [diff] [blame] | 1805 | strict |= flags & RT6_LOOKUP_F_IFACE; |
David Ahern | d5d32e4 | 2016-10-24 12:27:23 -0700 | [diff] [blame] | 1806 | strict |= flags & RT6_LOOKUP_F_IGNORE_LINKSTATE; |
Martin KaFai Lau | 367efcb | 2014-10-20 13:42:45 -0700 | [diff] [blame] | 1807 | if (net->ipv6.devconf_all->forwarding == 0) |
| 1808 | strict |= RT6_LOOKUP_F_REACHABLE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1809 | |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 1810 | rcu_read_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1811 | |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 1812 | fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr); |
Martin KaFai Lau | 367efcb | 2014-10-20 13:42:45 -0700 | [diff] [blame] | 1813 | saved_fn = fn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1814 | |
David Ahern | ca25449 | 2015-10-12 11:47:10 -0700 | [diff] [blame] | 1815 | if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF) |
| 1816 | oif = 0; |
| 1817 | |
Martin KaFai Lau | a3c00e4 | 2014-10-20 13:42:43 -0700 | [diff] [blame] | 1818 | redo_rt6_select: |
David Ahern | 23fb93a | 2018-04-17 17:33:23 -0700 | [diff] [blame] | 1819 | f6i = rt6_select(net, fn, oif, strict); |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 1820 | if (f6i->fib6_nsiblings) |
David Ahern | 23fb93a | 2018-04-17 17:33:23 -0700 | [diff] [blame] | 1821 | f6i = rt6_multipath_select(net, f6i, fl6, oif, skb, strict); |
| 1822 | if (f6i == net->ipv6.fib6_null_entry) { |
Martin KaFai Lau | a3c00e4 | 2014-10-20 13:42:43 -0700 | [diff] [blame] | 1823 | fn = fib6_backtrack(fn, &fl6->saddr); |
| 1824 | if (fn) |
| 1825 | goto redo_rt6_select; |
Martin KaFai Lau | 367efcb | 2014-10-20 13:42:45 -0700 | [diff] [blame] | 1826 | else if (strict & RT6_LOOKUP_F_REACHABLE) { |
| 1827 | /* also consider unreachable route */ |
| 1828 | strict &= ~RT6_LOOKUP_F_REACHABLE; |
| 1829 | fn = saved_fn; |
| 1830 | goto redo_rt6_select; |
Martin KaFai Lau | 367efcb | 2014-10-20 13:42:45 -0700 | [diff] [blame] | 1831 | } |
Martin KaFai Lau | a3c00e4 | 2014-10-20 13:42:43 -0700 | [diff] [blame] | 1832 | } |
| 1833 | |
David Ahern | 23fb93a | 2018-04-17 17:33:23 -0700 | [diff] [blame] | 1834 | if (f6i == net->ipv6.fib6_null_entry) { |
David Ahern | 421842e | 2018-04-17 17:33:18 -0700 | [diff] [blame] | 1835 | rt = net->ipv6.ip6_null_entry; |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 1836 | rcu_read_unlock(); |
Wei Wang | d3843fe | 2017-10-06 12:06:06 -0700 | [diff] [blame] | 1837 | dst_hold(&rt->dst); |
Paolo Abeni | b65f164 | 2017-10-19 09:31:43 +0200 | [diff] [blame] | 1838 | trace_fib6_table_lookup(net, rt, table, fl6); |
Wei Wang | d3843fe | 2017-10-06 12:06:06 -0700 | [diff] [blame] | 1839 | return rt; |
David Ahern | 23fb93a | 2018-04-17 17:33:23 -0700 | [diff] [blame] | 1840 | } |
| 1841 | |
| 1842 | /*Search through exception table */ |
| 1843 | rt = rt6_find_cached_rt(f6i, &fl6->daddr, &fl6->saddr); |
| 1844 | if (rt) { |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 1845 | if (ip6_hold_safe(net, &rt, true)) |
Wei Wang | d3843fe | 2017-10-06 12:06:06 -0700 | [diff] [blame] | 1846 | dst_use_noref(&rt->dst, jiffies); |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 1847 | |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 1848 | rcu_read_unlock(); |
Paolo Abeni | b65f164 | 2017-10-19 09:31:43 +0200 | [diff] [blame] | 1849 | trace_fib6_table_lookup(net, rt, table, fl6); |
Martin KaFai Lau | d52d399 | 2015-05-22 20:56:06 -0700 | [diff] [blame] | 1850 | return rt; |
Martin KaFai Lau | 3da59bd | 2015-05-22 20:56:03 -0700 | [diff] [blame] | 1851 | } else if (unlikely((fl6->flowi6_flags & FLOWI_FLAG_KNOWN_NH) && |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 1852 | !(f6i->fib6_flags & RTF_GATEWAY))) { |
Martin KaFai Lau | 3da59bd | 2015-05-22 20:56:03 -0700 | [diff] [blame] | 1853 | /* Create a RTF_CACHE clone which will not be |
| 1854 | * owned by the fib6 tree. It is for the special case where |
| 1855 | * the daddr in the skb during the neighbor look-up is different |
| 1856 | * from the fl6->daddr used to look-up route here. |
| 1857 | */ |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1858 | |
Martin KaFai Lau | 3da59bd | 2015-05-22 20:56:03 -0700 | [diff] [blame] | 1859 | struct rt6_info *uncached_rt; |
| 1860 | |
David Ahern | 93531c6 | 2018-04-17 17:33:25 -0700 | [diff] [blame] | 1861 | fib6_info_hold(f6i); |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 1862 | rcu_read_unlock(); |
Martin KaFai Lau | d52d399 | 2015-05-22 20:56:06 -0700 | [diff] [blame] | 1863 | |
David Ahern | 23fb93a | 2018-04-17 17:33:23 -0700 | [diff] [blame] | 1864 | uncached_rt = ip6_rt_cache_alloc(f6i, &fl6->daddr, NULL); |
David Ahern | 93531c6 | 2018-04-17 17:33:25 -0700 | [diff] [blame] | 1865 | fib6_info_release(f6i); |
Martin KaFai Lau | 3da59bd | 2015-05-22 20:56:03 -0700 | [diff] [blame] | 1866 | |
Wei Wang | 1cfb71e | 2017-06-17 10:42:33 -0700 | [diff] [blame] | 1867 | if (uncached_rt) { |
| 1868 | /* Uncached_rt's refcnt is taken during ip6_rt_cache_alloc() |
| 1869 | * No need for another dst_hold() |
| 1870 | */ |
Martin KaFai Lau | 8d0b94a | 2015-05-22 20:56:04 -0700 | [diff] [blame] | 1871 | rt6_uncached_list_add(uncached_rt); |
Wei Wang | 81eb844 | 2017-10-06 12:06:11 -0700 | [diff] [blame] | 1872 | atomic_inc(&net->ipv6.rt6_stats->fib_rt_uncache); |
Wei Wang | 1cfb71e | 2017-06-17 10:42:33 -0700 | [diff] [blame] | 1873 | } else { |
Martin KaFai Lau | 3da59bd | 2015-05-22 20:56:03 -0700 | [diff] [blame] | 1874 | uncached_rt = net->ipv6.ip6_null_entry; |
Wei Wang | 1cfb71e | 2017-06-17 10:42:33 -0700 | [diff] [blame] | 1875 | dst_hold(&uncached_rt->dst); |
| 1876 | } |
David Ahern | b811580 | 2015-11-19 12:24:22 -0800 | [diff] [blame] | 1877 | |
Paolo Abeni | b65f164 | 2017-10-19 09:31:43 +0200 | [diff] [blame] | 1878 | trace_fib6_table_lookup(net, uncached_rt, table, fl6); |
Martin KaFai Lau | 3da59bd | 2015-05-22 20:56:03 -0700 | [diff] [blame] | 1879 | return uncached_rt; |
Martin KaFai Lau | 3da59bd | 2015-05-22 20:56:03 -0700 | [diff] [blame] | 1880 | |
Martin KaFai Lau | d52d399 | 2015-05-22 20:56:06 -0700 | [diff] [blame] | 1881 | } else { |
| 1882 | /* Get a percpu copy */ |
| 1883 | |
| 1884 | struct rt6_info *pcpu_rt; |
| 1885 | |
Eric Dumazet | 951f788 | 2017-10-08 21:07:18 -0700 | [diff] [blame] | 1886 | local_bh_disable(); |
David Ahern | 23fb93a | 2018-04-17 17:33:23 -0700 | [diff] [blame] | 1887 | pcpu_rt = rt6_get_pcpu_route(f6i); |
Martin KaFai Lau | d52d399 | 2015-05-22 20:56:06 -0700 | [diff] [blame] | 1888 | |
David Ahern | 93531c6 | 2018-04-17 17:33:25 -0700 | [diff] [blame] | 1889 | if (!pcpu_rt) |
| 1890 | pcpu_rt = rt6_make_pcpu_route(net, f6i); |
| 1891 | |
Eric Dumazet | 951f788 | 2017-10-08 21:07:18 -0700 | [diff] [blame] | 1892 | local_bh_enable(); |
| 1893 | rcu_read_unlock(); |
Paolo Abeni | b65f164 | 2017-10-19 09:31:43 +0200 | [diff] [blame] | 1894 | trace_fib6_table_lookup(net, pcpu_rt, table, fl6); |
Martin KaFai Lau | d52d399 | 2015-05-22 20:56:06 -0700 | [diff] [blame] | 1895 | return pcpu_rt; |
| 1896 | } |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1897 | } |
David Ahern | 9ff7438 | 2016-06-13 13:44:19 -0700 | [diff] [blame] | 1898 | EXPORT_SYMBOL_GPL(ip6_pol_route); |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1899 | |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 1900 | static struct rt6_info *ip6_pol_route_input(struct net *net, |
| 1901 | struct fib6_table *table, |
| 1902 | struct flowi6 *fl6, |
| 1903 | const struct sk_buff *skb, |
| 1904 | int flags) |
Pavel Emelyanov | 4acad72 | 2007-10-15 13:02:51 -0700 | [diff] [blame] | 1905 | { |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 1906 | return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, skb, flags); |
Pavel Emelyanov | 4acad72 | 2007-10-15 13:02:51 -0700 | [diff] [blame] | 1907 | } |
| 1908 | |
Mahesh Bandewar | d409b84 | 2016-09-16 12:59:08 -0700 | [diff] [blame] | 1909 | struct dst_entry *ip6_route_input_lookup(struct net *net, |
| 1910 | struct net_device *dev, |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 1911 | struct flowi6 *fl6, |
| 1912 | const struct sk_buff *skb, |
| 1913 | int flags) |
Shmulik Ladkani | 72331bc | 2012-04-01 04:03:45 +0000 | [diff] [blame] | 1914 | { |
| 1915 | if (rt6_need_strict(&fl6->daddr) && dev->type != ARPHRD_PIMREG) |
| 1916 | flags |= RT6_LOOKUP_F_IFACE; |
| 1917 | |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 1918 | return fib6_rule_lookup(net, fl6, skb, flags, ip6_pol_route_input); |
Shmulik Ladkani | 72331bc | 2012-04-01 04:03:45 +0000 | [diff] [blame] | 1919 | } |
Mahesh Bandewar | d409b84 | 2016-09-16 12:59:08 -0700 | [diff] [blame] | 1920 | EXPORT_SYMBOL_GPL(ip6_route_input_lookup); |
Shmulik Ladkani | 72331bc | 2012-04-01 04:03:45 +0000 | [diff] [blame] | 1921 | |
Jakub Sitnicki | 23aebda | 2017-08-23 09:58:29 +0200 | [diff] [blame] | 1922 | static void ip6_multipath_l3_keys(const struct sk_buff *skb, |
Roopa Prabhu | 5e5d6fe | 2018-02-28 22:43:22 -0500 | [diff] [blame] | 1923 | struct flow_keys *keys, |
| 1924 | struct flow_keys *flkeys) |
Jakub Sitnicki | 23aebda | 2017-08-23 09:58:29 +0200 | [diff] [blame] | 1925 | { |
| 1926 | const struct ipv6hdr *outer_iph = ipv6_hdr(skb); |
| 1927 | const struct ipv6hdr *key_iph = outer_iph; |
Roopa Prabhu | 5e5d6fe | 2018-02-28 22:43:22 -0500 | [diff] [blame] | 1928 | struct flow_keys *_flkeys = flkeys; |
Jakub Sitnicki | 23aebda | 2017-08-23 09:58:29 +0200 | [diff] [blame] | 1929 | const struct ipv6hdr *inner_iph; |
| 1930 | const struct icmp6hdr *icmph; |
| 1931 | struct ipv6hdr _inner_iph; |
| 1932 | |
| 1933 | if (likely(outer_iph->nexthdr != IPPROTO_ICMPV6)) |
| 1934 | goto out; |
| 1935 | |
| 1936 | icmph = icmp6_hdr(skb); |
| 1937 | if (icmph->icmp6_type != ICMPV6_DEST_UNREACH && |
| 1938 | icmph->icmp6_type != ICMPV6_PKT_TOOBIG && |
| 1939 | icmph->icmp6_type != ICMPV6_TIME_EXCEED && |
| 1940 | icmph->icmp6_type != ICMPV6_PARAMPROB) |
| 1941 | goto out; |
| 1942 | |
| 1943 | inner_iph = skb_header_pointer(skb, |
| 1944 | skb_transport_offset(skb) + sizeof(*icmph), |
| 1945 | sizeof(_inner_iph), &_inner_iph); |
| 1946 | if (!inner_iph) |
| 1947 | goto out; |
| 1948 | |
| 1949 | key_iph = inner_iph; |
Roopa Prabhu | 5e5d6fe | 2018-02-28 22:43:22 -0500 | [diff] [blame] | 1950 | _flkeys = NULL; |
Jakub Sitnicki | 23aebda | 2017-08-23 09:58:29 +0200 | [diff] [blame] | 1951 | out: |
Roopa Prabhu | 5e5d6fe | 2018-02-28 22:43:22 -0500 | [diff] [blame] | 1952 | if (_flkeys) { |
| 1953 | keys->addrs.v6addrs.src = _flkeys->addrs.v6addrs.src; |
| 1954 | keys->addrs.v6addrs.dst = _flkeys->addrs.v6addrs.dst; |
| 1955 | keys->tags.flow_label = _flkeys->tags.flow_label; |
| 1956 | keys->basic.ip_proto = _flkeys->basic.ip_proto; |
| 1957 | } else { |
| 1958 | keys->addrs.v6addrs.src = key_iph->saddr; |
| 1959 | keys->addrs.v6addrs.dst = key_iph->daddr; |
| 1960 | keys->tags.flow_label = ip6_flowinfo(key_iph); |
| 1961 | keys->basic.ip_proto = key_iph->nexthdr; |
| 1962 | } |
Jakub Sitnicki | 23aebda | 2017-08-23 09:58:29 +0200 | [diff] [blame] | 1963 | } |
| 1964 | |
| 1965 | /* 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] | 1966 | u32 rt6_multipath_hash(const struct net *net, const struct flowi6 *fl6, |
| 1967 | const struct sk_buff *skb, struct flow_keys *flkeys) |
Jakub Sitnicki | 23aebda | 2017-08-23 09:58:29 +0200 | [diff] [blame] | 1968 | { |
| 1969 | struct flow_keys hash_keys; |
David Ahern | 9a2a537 | 2018-03-02 08:32:15 -0800 | [diff] [blame] | 1970 | u32 mhash; |
Jakub Sitnicki | 23aebda | 2017-08-23 09:58:29 +0200 | [diff] [blame] | 1971 | |
David S. Miller | bbfa047 | 2018-03-12 11:09:33 -0400 | [diff] [blame] | 1972 | switch (ip6_multipath_hash_policy(net)) { |
David Ahern | b4bac17 | 2018-03-02 08:32:18 -0800 | [diff] [blame] | 1973 | case 0: |
| 1974 | memset(&hash_keys, 0, sizeof(hash_keys)); |
| 1975 | hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; |
| 1976 | if (skb) { |
| 1977 | ip6_multipath_l3_keys(skb, &hash_keys, flkeys); |
| 1978 | } else { |
| 1979 | hash_keys.addrs.v6addrs.src = fl6->saddr; |
| 1980 | hash_keys.addrs.v6addrs.dst = fl6->daddr; |
| 1981 | hash_keys.tags.flow_label = (__force u32)fl6->flowlabel; |
| 1982 | hash_keys.basic.ip_proto = fl6->flowi6_proto; |
| 1983 | } |
| 1984 | break; |
| 1985 | case 1: |
| 1986 | if (skb) { |
| 1987 | unsigned int flag = FLOW_DISSECTOR_F_STOP_AT_ENCAP; |
| 1988 | struct flow_keys keys; |
| 1989 | |
| 1990 | /* short-circuit if we already have L4 hash present */ |
| 1991 | if (skb->l4_hash) |
| 1992 | return skb_get_hash_raw(skb) >> 1; |
| 1993 | |
| 1994 | memset(&hash_keys, 0, sizeof(hash_keys)); |
| 1995 | |
| 1996 | if (!flkeys) { |
| 1997 | skb_flow_dissect_flow_keys(skb, &keys, flag); |
| 1998 | flkeys = &keys; |
| 1999 | } |
| 2000 | hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; |
| 2001 | hash_keys.addrs.v6addrs.src = flkeys->addrs.v6addrs.src; |
| 2002 | hash_keys.addrs.v6addrs.dst = flkeys->addrs.v6addrs.dst; |
| 2003 | hash_keys.ports.src = flkeys->ports.src; |
| 2004 | hash_keys.ports.dst = flkeys->ports.dst; |
| 2005 | hash_keys.basic.ip_proto = flkeys->basic.ip_proto; |
| 2006 | } else { |
| 2007 | memset(&hash_keys, 0, sizeof(hash_keys)); |
| 2008 | hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; |
| 2009 | hash_keys.addrs.v6addrs.src = fl6->saddr; |
| 2010 | hash_keys.addrs.v6addrs.dst = fl6->daddr; |
| 2011 | hash_keys.ports.src = fl6->fl6_sport; |
| 2012 | hash_keys.ports.dst = fl6->fl6_dport; |
| 2013 | hash_keys.basic.ip_proto = fl6->flowi6_proto; |
| 2014 | } |
| 2015 | break; |
Jakub Sitnicki | 23aebda | 2017-08-23 09:58:29 +0200 | [diff] [blame] | 2016 | } |
David Ahern | 9a2a537 | 2018-03-02 08:32:15 -0800 | [diff] [blame] | 2017 | mhash = flow_hash_from_keys(&hash_keys); |
Jakub Sitnicki | 23aebda | 2017-08-23 09:58:29 +0200 | [diff] [blame] | 2018 | |
David Ahern | 9a2a537 | 2018-03-02 08:32:15 -0800 | [diff] [blame] | 2019 | return mhash >> 1; |
Jakub Sitnicki | 23aebda | 2017-08-23 09:58:29 +0200 | [diff] [blame] | 2020 | } |
| 2021 | |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 2022 | void ip6_route_input(struct sk_buff *skb) |
| 2023 | { |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 2024 | const struct ipv6hdr *iph = ipv6_hdr(skb); |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 2025 | struct net *net = dev_net(skb->dev); |
Thomas Graf | adaa70b | 2006-10-13 15:01:03 -0700 | [diff] [blame] | 2026 | int flags = RT6_LOOKUP_F_HAS_SADDR; |
Jiri Benc | 904af04 | 2015-08-20 13:56:31 +0200 | [diff] [blame] | 2027 | struct ip_tunnel_info *tun_info; |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 2028 | struct flowi6 fl6 = { |
David Ahern | e0d56fd | 2016-09-10 12:09:57 -0700 | [diff] [blame] | 2029 | .flowi6_iif = skb->dev->ifindex, |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 2030 | .daddr = iph->daddr, |
| 2031 | .saddr = iph->saddr, |
YOSHIFUJI Hideaki / 吉藤英明 | 6502ca5 | 2013-01-13 05:01:51 +0000 | [diff] [blame] | 2032 | .flowlabel = ip6_flowinfo(iph), |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 2033 | .flowi6_mark = skb->mark, |
| 2034 | .flowi6_proto = iph->nexthdr, |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 2035 | }; |
Roopa Prabhu | 5e5d6fe | 2018-02-28 22:43:22 -0500 | [diff] [blame] | 2036 | struct flow_keys *flkeys = NULL, _flkeys; |
Thomas Graf | adaa70b | 2006-10-13 15:01:03 -0700 | [diff] [blame] | 2037 | |
Jiri Benc | 904af04 | 2015-08-20 13:56:31 +0200 | [diff] [blame] | 2038 | tun_info = skb_tunnel_info(skb); |
Jiri Benc | 46fa062 | 2015-08-28 20:48:19 +0200 | [diff] [blame] | 2039 | if (tun_info && !(tun_info->mode & IP_TUNNEL_INFO_TX)) |
Jiri Benc | 904af04 | 2015-08-20 13:56:31 +0200 | [diff] [blame] | 2040 | fl6.flowi6_tun_key.tun_id = tun_info->key.tun_id; |
Roopa Prabhu | 5e5d6fe | 2018-02-28 22:43:22 -0500 | [diff] [blame] | 2041 | |
| 2042 | if (fib6_rules_early_flow_dissect(net, skb, &fl6, &_flkeys)) |
| 2043 | flkeys = &_flkeys; |
| 2044 | |
Jakub Sitnicki | 23aebda | 2017-08-23 09:58:29 +0200 | [diff] [blame] | 2045 | if (unlikely(fl6.flowi6_proto == IPPROTO_ICMPV6)) |
David Ahern | b4bac17 | 2018-03-02 08:32:18 -0800 | [diff] [blame] | 2046 | fl6.mp_hash = rt6_multipath_hash(net, &fl6, skb, flkeys); |
Jiri Benc | 06e9d04 | 2015-08-20 13:56:26 +0200 | [diff] [blame] | 2047 | skb_dst_drop(skb); |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 2048 | skb_dst_set(skb, |
| 2049 | ip6_route_input_lookup(net, skb->dev, &fl6, skb, flags)); |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 2050 | } |
| 2051 | |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 2052 | static struct rt6_info *ip6_pol_route_output(struct net *net, |
| 2053 | struct fib6_table *table, |
| 2054 | struct flowi6 *fl6, |
| 2055 | const struct sk_buff *skb, |
| 2056 | int flags) |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 2057 | { |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 2058 | return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, skb, flags); |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 2059 | } |
| 2060 | |
Paolo Abeni | 6f21c96 | 2016-01-29 12:30:19 +0100 | [diff] [blame] | 2061 | struct dst_entry *ip6_route_output_flags(struct net *net, const struct sock *sk, |
| 2062 | struct flowi6 *fl6, int flags) |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 2063 | { |
David Ahern | d46a9d6 | 2015-10-21 08:42:22 -0700 | [diff] [blame] | 2064 | bool any_src; |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 2065 | |
David Ahern | 4c1feac | 2016-09-10 12:09:56 -0700 | [diff] [blame] | 2066 | if (rt6_need_strict(&fl6->daddr)) { |
| 2067 | struct dst_entry *dst; |
| 2068 | |
| 2069 | dst = l3mdev_link_scope_lookup(net, fl6); |
| 2070 | if (dst) |
| 2071 | return dst; |
| 2072 | } |
David Ahern | ca25449 | 2015-10-12 11:47:10 -0700 | [diff] [blame] | 2073 | |
Pavel Emelyanov | 1fb9489 | 2012-08-08 21:53:36 +0000 | [diff] [blame] | 2074 | fl6->flowi6_iif = LOOPBACK_IFINDEX; |
David McCullough | 4dc27d1c | 2012-06-25 15:42:26 +0000 | [diff] [blame] | 2075 | |
David Ahern | d46a9d6 | 2015-10-21 08:42:22 -0700 | [diff] [blame] | 2076 | any_src = ipv6_addr_any(&fl6->saddr); |
David Ahern | 741a11d | 2015-09-28 10:12:13 -0700 | [diff] [blame] | 2077 | if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr) || |
David Ahern | d46a9d6 | 2015-10-21 08:42:22 -0700 | [diff] [blame] | 2078 | (fl6->flowi6_oif && any_src)) |
YOSHIFUJI Hideaki | 77d16f4 | 2006-08-23 17:25:05 -0700 | [diff] [blame] | 2079 | flags |= RT6_LOOKUP_F_IFACE; |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 2080 | |
David Ahern | d46a9d6 | 2015-10-21 08:42:22 -0700 | [diff] [blame] | 2081 | if (!any_src) |
Thomas Graf | adaa70b | 2006-10-13 15:01:03 -0700 | [diff] [blame] | 2082 | flags |= RT6_LOOKUP_F_HAS_SADDR; |
YOSHIFUJI Hideaki / 吉藤英明 | 0c9a2ac | 2010-03-07 00:14:44 +0000 | [diff] [blame] | 2083 | else if (sk) |
| 2084 | flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs); |
Thomas Graf | adaa70b | 2006-10-13 15:01:03 -0700 | [diff] [blame] | 2085 | |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 2086 | return fib6_rule_lookup(net, fl6, NULL, flags, ip6_pol_route_output); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2087 | } |
Paolo Abeni | 6f21c96 | 2016-01-29 12:30:19 +0100 | [diff] [blame] | 2088 | EXPORT_SYMBOL_GPL(ip6_route_output_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2089 | |
David S. Miller | 2774c13 | 2011-03-01 14:59:04 -0800 | [diff] [blame] | 2090 | 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] | 2091 | { |
David S. Miller | 5c1e6aa | 2011-04-28 14:13:38 -0700 | [diff] [blame] | 2092 | struct rt6_info *rt, *ort = (struct rt6_info *) dst_orig; |
Wei Wang | 1dbe3252 | 2017-06-17 10:42:26 -0700 | [diff] [blame] | 2093 | struct net_device *loopback_dev = net->loopback_dev; |
David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 2094 | struct dst_entry *new = NULL; |
| 2095 | |
Wei Wang | 1dbe3252 | 2017-06-17 10:42:26 -0700 | [diff] [blame] | 2096 | rt = dst_alloc(&ip6_dst_blackhole_ops, loopback_dev, 1, |
Steffen Klassert | 62cf27e | 2017-10-09 08:39:43 +0200 | [diff] [blame] | 2097 | DST_OBSOLETE_DEAD, 0); |
David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 2098 | if (rt) { |
Martin KaFai Lau | 0a1f596 | 2015-10-15 16:39:58 -0700 | [diff] [blame] | 2099 | rt6_info_init(rt); |
Wei Wang | 81eb844 | 2017-10-06 12:06:11 -0700 | [diff] [blame] | 2100 | atomic_inc(&net->ipv6.rt6_stats->fib_rt_alloc); |
Martin KaFai Lau | 0a1f596 | 2015-10-15 16:39:58 -0700 | [diff] [blame] | 2101 | |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 2102 | new = &rt->dst; |
David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 2103 | new->__use = 1; |
Herbert Xu | 352e512 | 2007-11-13 21:34:06 -0800 | [diff] [blame] | 2104 | new->input = dst_discard; |
Eric W. Biederman | ede2059 | 2015-10-07 16:48:47 -0500 | [diff] [blame] | 2105 | new->output = dst_discard_out; |
David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 2106 | |
Martin KaFai Lau | 0a1f596 | 2015-10-15 16:39:58 -0700 | [diff] [blame] | 2107 | dst_copy_metrics(new, &ort->dst); |
David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 2108 | |
Wei Wang | 1dbe3252 | 2017-06-17 10:42:26 -0700 | [diff] [blame] | 2109 | rt->rt6i_idev = in6_dev_get(loopback_dev); |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 2110 | rt->rt6i_gateway = ort->rt6i_gateway; |
Martin KaFai Lau | 0a1f596 | 2015-10-15 16:39:58 -0700 | [diff] [blame] | 2111 | rt->rt6i_flags = ort->rt6i_flags & ~RTF_PCPU; |
David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 2112 | |
| 2113 | memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key)); |
| 2114 | #ifdef CONFIG_IPV6_SUBTREES |
| 2115 | memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key)); |
| 2116 | #endif |
David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 2117 | } |
| 2118 | |
David S. Miller | 69ead7a | 2011-03-01 14:45:33 -0800 | [diff] [blame] | 2119 | dst_release(dst_orig); |
| 2120 | return new ? new : ERR_PTR(-ENOMEM); |
David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 2121 | } |
David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 2122 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2123 | /* |
| 2124 | * Destination cache support functions |
| 2125 | */ |
| 2126 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 2127 | static bool fib6_check(struct fib6_info *f6i, u32 cookie) |
David Ahern | 93531c6 | 2018-04-17 17:33:25 -0700 | [diff] [blame] | 2128 | { |
| 2129 | u32 rt_cookie = 0; |
| 2130 | |
| 2131 | if ((f6i && !rt6_get_cookie_safe(f6i, &rt_cookie)) || |
| 2132 | rt_cookie != cookie) |
| 2133 | return false; |
| 2134 | |
| 2135 | if (fib6_check_expired(f6i)) |
| 2136 | return false; |
| 2137 | |
| 2138 | return true; |
| 2139 | } |
| 2140 | |
Martin KaFai Lau | 3da59bd | 2015-05-22 20:56:03 -0700 | [diff] [blame] | 2141 | static struct dst_entry *rt6_check(struct rt6_info *rt, u32 cookie) |
| 2142 | { |
Steffen Klassert | 3614364 | 2017-08-25 09:05:42 +0200 | [diff] [blame] | 2143 | u32 rt_cookie = 0; |
Wei Wang | c5cff85 | 2017-08-21 09:47:10 -0700 | [diff] [blame] | 2144 | |
David Ahern | 93531c6 | 2018-04-17 17:33:25 -0700 | [diff] [blame] | 2145 | if ((rt->from && !rt6_get_cookie_safe(rt->from, &rt_cookie)) || |
| 2146 | rt_cookie != cookie) |
Martin KaFai Lau | 3da59bd | 2015-05-22 20:56:03 -0700 | [diff] [blame] | 2147 | return NULL; |
| 2148 | |
| 2149 | if (rt6_check_expired(rt)) |
| 2150 | return NULL; |
| 2151 | |
| 2152 | return &rt->dst; |
| 2153 | } |
| 2154 | |
| 2155 | static struct dst_entry *rt6_dst_from_check(struct rt6_info *rt, u32 cookie) |
| 2156 | { |
Martin KaFai Lau | 5973fb1 | 2015-11-11 11:51:07 -0800 | [diff] [blame] | 2157 | if (!__rt6_check_expired(rt) && |
| 2158 | rt->dst.obsolete == DST_OBSOLETE_FORCE_CHK && |
David Ahern | 93531c6 | 2018-04-17 17:33:25 -0700 | [diff] [blame] | 2159 | fib6_check(rt->from, cookie)) |
Martin KaFai Lau | 3da59bd | 2015-05-22 20:56:03 -0700 | [diff] [blame] | 2160 | return &rt->dst; |
| 2161 | else |
| 2162 | return NULL; |
| 2163 | } |
| 2164 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2165 | static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie) |
| 2166 | { |
| 2167 | struct rt6_info *rt; |
| 2168 | |
| 2169 | rt = (struct rt6_info *) dst; |
| 2170 | |
Nicolas Dichtel | 6f3118b | 2012-09-10 22:09:46 +0000 | [diff] [blame] | 2171 | /* All IPV6 dsts are created with ->obsolete set to the value |
| 2172 | * DST_OBSOLETE_FORCE_CHK which forces validation calls down |
| 2173 | * into this function always. |
| 2174 | */ |
Hannes Frederic Sowa | e3bc10b | 2013-10-24 07:48:24 +0200 | [diff] [blame] | 2175 | |
Martin KaFai Lau | 02bcf4e | 2015-11-11 11:51:08 -0800 | [diff] [blame] | 2176 | if (rt->rt6i_flags & RTF_PCPU || |
David Miller | 3a2232e | 2017-11-28 15:40:40 -0500 | [diff] [blame] | 2177 | (unlikely(!list_empty(&rt->rt6i_uncached)) && rt->from)) |
Martin KaFai Lau | 3da59bd | 2015-05-22 20:56:03 -0700 | [diff] [blame] | 2178 | return rt6_dst_from_check(rt, cookie); |
| 2179 | else |
| 2180 | return rt6_check(rt, cookie); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2181 | } |
| 2182 | |
| 2183 | static struct dst_entry *ip6_negative_advice(struct dst_entry *dst) |
| 2184 | { |
| 2185 | struct rt6_info *rt = (struct rt6_info *) dst; |
| 2186 | |
| 2187 | if (rt) { |
YOSHIFUJI Hideaki / 吉藤英明 | 54c1a85 | 2010-03-28 07:15:45 +0000 | [diff] [blame] | 2188 | if (rt->rt6i_flags & RTF_CACHE) { |
| 2189 | if (rt6_check_expired(rt)) { |
David Ahern | 93531c6 | 2018-04-17 17:33:25 -0700 | [diff] [blame] | 2190 | rt6_remove_exception_rt(rt); |
YOSHIFUJI Hideaki / 吉藤英明 | 54c1a85 | 2010-03-28 07:15:45 +0000 | [diff] [blame] | 2191 | dst = NULL; |
| 2192 | } |
| 2193 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2194 | dst_release(dst); |
YOSHIFUJI Hideaki / 吉藤英明 | 54c1a85 | 2010-03-28 07:15:45 +0000 | [diff] [blame] | 2195 | dst = NULL; |
| 2196 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2197 | } |
YOSHIFUJI Hideaki / 吉藤英明 | 54c1a85 | 2010-03-28 07:15:45 +0000 | [diff] [blame] | 2198 | return dst; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2199 | } |
| 2200 | |
| 2201 | static void ip6_link_failure(struct sk_buff *skb) |
| 2202 | { |
| 2203 | struct rt6_info *rt; |
| 2204 | |
Alexey Dobriyan | 3ffe533 | 2010-02-18 08:25:24 +0000 | [diff] [blame] | 2205 | icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2206 | |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 2207 | rt = (struct rt6_info *) skb_dst(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2208 | if (rt) { |
Hannes Frederic Sowa | 1eb4f75 | 2013-07-10 23:00:57 +0200 | [diff] [blame] | 2209 | if (rt->rt6i_flags & RTF_CACHE) { |
Wei Wang | ad65a2f | 2017-06-17 10:42:35 -0700 | [diff] [blame] | 2210 | if (dst_hold_safe(&rt->dst)) |
David Ahern | 93531c6 | 2018-04-17 17:33:25 -0700 | [diff] [blame] | 2211 | rt6_remove_exception_rt(rt); |
| 2212 | } else if (rt->from) { |
Wei Wang | c5cff85 | 2017-08-21 09:47:10 -0700 | [diff] [blame] | 2213 | struct fib6_node *fn; |
| 2214 | |
| 2215 | rcu_read_lock(); |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 2216 | fn = rcu_dereference(rt->from->fib6_node); |
Wei Wang | c5cff85 | 2017-08-21 09:47:10 -0700 | [diff] [blame] | 2217 | if (fn && (rt->rt6i_flags & RTF_DEFAULT)) |
| 2218 | fn->fn_sernum = -1; |
| 2219 | rcu_read_unlock(); |
Hannes Frederic Sowa | 1eb4f75 | 2013-07-10 23:00:57 +0200 | [diff] [blame] | 2220 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2221 | } |
| 2222 | } |
| 2223 | |
Martin KaFai Lau | 45e4fd2 | 2015-05-22 20:56:00 -0700 | [diff] [blame] | 2224 | static void rt6_do_update_pmtu(struct rt6_info *rt, u32 mtu) |
| 2225 | { |
| 2226 | struct net *net = dev_net(rt->dst.dev); |
| 2227 | |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 2228 | dst_metric_set(&rt->dst, RTAX_MTU, mtu); |
Martin KaFai Lau | 45e4fd2 | 2015-05-22 20:56:00 -0700 | [diff] [blame] | 2229 | rt->rt6i_flags |= RTF_MODIFIED; |
Martin KaFai Lau | 45e4fd2 | 2015-05-22 20:56:00 -0700 | [diff] [blame] | 2230 | rt6_update_expires(rt, net->ipv6.sysctl.ip6_rt_mtu_expires); |
| 2231 | } |
| 2232 | |
Martin KaFai Lau | 0d3f6d2 | 2015-11-11 11:51:06 -0800 | [diff] [blame] | 2233 | static bool rt6_cache_allowed_for_pmtu(const struct rt6_info *rt) |
| 2234 | { |
| 2235 | return !(rt->rt6i_flags & RTF_CACHE) && |
David Ahern | 77634cc | 2018-04-17 17:33:27 -0700 | [diff] [blame] | 2236 | (rt->rt6i_flags & RTF_PCPU || rt->from); |
Martin KaFai Lau | 0d3f6d2 | 2015-11-11 11:51:06 -0800 | [diff] [blame] | 2237 | } |
| 2238 | |
Martin KaFai Lau | 45e4fd2 | 2015-05-22 20:56:00 -0700 | [diff] [blame] | 2239 | static void __ip6_rt_update_pmtu(struct dst_entry *dst, const struct sock *sk, |
| 2240 | const struct ipv6hdr *iph, u32 mtu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2241 | { |
Julian Anastasov | 0dec879 | 2017-02-06 23:14:16 +0200 | [diff] [blame] | 2242 | const struct in6_addr *daddr, *saddr; |
Ian Morris | 67ba415 | 2014-08-24 21:53:10 +0100 | [diff] [blame] | 2243 | struct rt6_info *rt6 = (struct rt6_info *)dst; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2244 | |
Martin KaFai Lau | 45e4fd2 | 2015-05-22 20:56:00 -0700 | [diff] [blame] | 2245 | if (rt6->rt6i_flags & RTF_LOCAL) |
| 2246 | return; |
| 2247 | |
Xin Long | 19bda36 | 2016-10-28 18:18:01 +0800 | [diff] [blame] | 2248 | if (dst_metric_locked(dst, RTAX_MTU)) |
| 2249 | return; |
| 2250 | |
Julian Anastasov | 0dec879 | 2017-02-06 23:14:16 +0200 | [diff] [blame] | 2251 | if (iph) { |
| 2252 | daddr = &iph->daddr; |
| 2253 | saddr = &iph->saddr; |
| 2254 | } else if (sk) { |
| 2255 | daddr = &sk->sk_v6_daddr; |
| 2256 | saddr = &inet6_sk(sk)->saddr; |
| 2257 | } else { |
| 2258 | daddr = NULL; |
| 2259 | saddr = NULL; |
| 2260 | } |
| 2261 | dst_confirm_neigh(dst, daddr); |
Martin KaFai Lau | 45e4fd2 | 2015-05-22 20:56:00 -0700 | [diff] [blame] | 2262 | mtu = max_t(u32, mtu, IPV6_MIN_MTU); |
| 2263 | if (mtu >= dst_mtu(dst)) |
| 2264 | return; |
David S. Miller | 81aded2 | 2012-06-15 14:54:11 -0700 | [diff] [blame] | 2265 | |
Martin KaFai Lau | 0d3f6d2 | 2015-11-11 11:51:06 -0800 | [diff] [blame] | 2266 | if (!rt6_cache_allowed_for_pmtu(rt6)) { |
Martin KaFai Lau | 45e4fd2 | 2015-05-22 20:56:00 -0700 | [diff] [blame] | 2267 | rt6_do_update_pmtu(rt6, mtu); |
Wei Wang | 2b760fc | 2017-10-06 12:06:03 -0700 | [diff] [blame] | 2268 | /* update rt6_ex->stamp for cache */ |
| 2269 | if (rt6->rt6i_flags & RTF_CACHE) |
| 2270 | rt6_update_exception_stamp_rt(rt6); |
Julian Anastasov | 0dec879 | 2017-02-06 23:14:16 +0200 | [diff] [blame] | 2271 | } else if (daddr) { |
Martin KaFai Lau | 45e4fd2 | 2015-05-22 20:56:00 -0700 | [diff] [blame] | 2272 | struct rt6_info *nrt6; |
Hagen Paul Pfeifer | 9d28971 | 2015-01-15 22:34:25 +0100 | [diff] [blame] | 2273 | |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 2274 | nrt6 = ip6_rt_cache_alloc(rt6->from, daddr, saddr); |
Martin KaFai Lau | 45e4fd2 | 2015-05-22 20:56:00 -0700 | [diff] [blame] | 2275 | if (nrt6) { |
| 2276 | rt6_do_update_pmtu(nrt6, mtu); |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 2277 | if (rt6_insert_exception(nrt6, rt6->from)) |
Wei Wang | 2b760fc | 2017-10-06 12:06:03 -0700 | [diff] [blame] | 2278 | dst_release_immediate(&nrt6->dst); |
Martin KaFai Lau | 45e4fd2 | 2015-05-22 20:56:00 -0700 | [diff] [blame] | 2279 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2280 | } |
| 2281 | } |
| 2282 | |
Martin KaFai Lau | 45e4fd2 | 2015-05-22 20:56:00 -0700 | [diff] [blame] | 2283 | static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk, |
| 2284 | struct sk_buff *skb, u32 mtu) |
| 2285 | { |
| 2286 | __ip6_rt_update_pmtu(dst, sk, skb ? ipv6_hdr(skb) : NULL, mtu); |
| 2287 | } |
| 2288 | |
David S. Miller | 42ae66c | 2012-06-15 20:01:57 -0700 | [diff] [blame] | 2289 | 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] | 2290 | int oif, u32 mark, kuid_t uid) |
David S. Miller | 81aded2 | 2012-06-15 14:54:11 -0700 | [diff] [blame] | 2291 | { |
| 2292 | const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data; |
| 2293 | struct dst_entry *dst; |
| 2294 | struct flowi6 fl6; |
| 2295 | |
| 2296 | memset(&fl6, 0, sizeof(fl6)); |
| 2297 | fl6.flowi6_oif = oif; |
Lorenzo Colitti | 1b3c61d | 2014-05-13 10:17:34 -0700 | [diff] [blame] | 2298 | fl6.flowi6_mark = mark ? mark : IP6_REPLY_MARK(net, skb->mark); |
David S. Miller | 81aded2 | 2012-06-15 14:54:11 -0700 | [diff] [blame] | 2299 | fl6.daddr = iph->daddr; |
| 2300 | fl6.saddr = iph->saddr; |
YOSHIFUJI Hideaki / 吉藤英明 | 6502ca5 | 2013-01-13 05:01:51 +0000 | [diff] [blame] | 2301 | fl6.flowlabel = ip6_flowinfo(iph); |
Lorenzo Colitti | e2d118a | 2016-11-04 02:23:43 +0900 | [diff] [blame] | 2302 | fl6.flowi6_uid = uid; |
David S. Miller | 81aded2 | 2012-06-15 14:54:11 -0700 | [diff] [blame] | 2303 | |
| 2304 | dst = ip6_route_output(net, NULL, &fl6); |
| 2305 | if (!dst->error) |
Martin KaFai Lau | 45e4fd2 | 2015-05-22 20:56:00 -0700 | [diff] [blame] | 2306 | __ip6_rt_update_pmtu(dst, NULL, iph, ntohl(mtu)); |
David S. Miller | 81aded2 | 2012-06-15 14:54:11 -0700 | [diff] [blame] | 2307 | dst_release(dst); |
| 2308 | } |
| 2309 | EXPORT_SYMBOL_GPL(ip6_update_pmtu); |
| 2310 | |
| 2311 | void ip6_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, __be32 mtu) |
| 2312 | { |
Martin KaFai Lau | 33c162a | 2016-04-11 15:29:36 -0700 | [diff] [blame] | 2313 | struct dst_entry *dst; |
| 2314 | |
David S. Miller | 81aded2 | 2012-06-15 14:54:11 -0700 | [diff] [blame] | 2315 | ip6_update_pmtu(skb, sock_net(sk), mtu, |
Lorenzo Colitti | e2d118a | 2016-11-04 02:23:43 +0900 | [diff] [blame] | 2316 | sk->sk_bound_dev_if, sk->sk_mark, sk->sk_uid); |
Martin KaFai Lau | 33c162a | 2016-04-11 15:29:36 -0700 | [diff] [blame] | 2317 | |
| 2318 | dst = __sk_dst_get(sk); |
| 2319 | if (!dst || !dst->obsolete || |
| 2320 | dst->ops->check(dst, inet6_sk(sk)->dst_cookie)) |
| 2321 | return; |
| 2322 | |
| 2323 | bh_lock_sock(sk); |
| 2324 | if (!sock_owned_by_user(sk) && !ipv6_addr_v4mapped(&sk->sk_v6_daddr)) |
| 2325 | ip6_datagram_dst_update(sk, false); |
| 2326 | bh_unlock_sock(sk); |
David S. Miller | 81aded2 | 2012-06-15 14:54:11 -0700 | [diff] [blame] | 2327 | } |
| 2328 | EXPORT_SYMBOL_GPL(ip6_sk_update_pmtu); |
| 2329 | |
Alexey Kodanev | 7d6850f | 2018-04-03 15:00:07 +0300 | [diff] [blame] | 2330 | void ip6_sk_dst_store_flow(struct sock *sk, struct dst_entry *dst, |
| 2331 | const struct flowi6 *fl6) |
| 2332 | { |
| 2333 | #ifdef CONFIG_IPV6_SUBTREES |
| 2334 | struct ipv6_pinfo *np = inet6_sk(sk); |
| 2335 | #endif |
| 2336 | |
| 2337 | ip6_dst_store(sk, dst, |
| 2338 | ipv6_addr_equal(&fl6->daddr, &sk->sk_v6_daddr) ? |
| 2339 | &sk->sk_v6_daddr : NULL, |
| 2340 | #ifdef CONFIG_IPV6_SUBTREES |
| 2341 | ipv6_addr_equal(&fl6->saddr, &np->saddr) ? |
| 2342 | &np->saddr : |
| 2343 | #endif |
| 2344 | NULL); |
| 2345 | } |
| 2346 | |
Duan Jiong | b55b76b | 2013-09-04 19:44:21 +0800 | [diff] [blame] | 2347 | /* Handle redirects */ |
| 2348 | struct ip6rd_flowi { |
| 2349 | struct flowi6 fl6; |
| 2350 | struct in6_addr gateway; |
| 2351 | }; |
| 2352 | |
| 2353 | static struct rt6_info *__ip6_route_redirect(struct net *net, |
| 2354 | struct fib6_table *table, |
| 2355 | struct flowi6 *fl6, |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 2356 | const struct sk_buff *skb, |
Duan Jiong | b55b76b | 2013-09-04 19:44:21 +0800 | [diff] [blame] | 2357 | int flags) |
| 2358 | { |
| 2359 | struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl6; |
David Ahern | 23fb93a | 2018-04-17 17:33:23 -0700 | [diff] [blame] | 2360 | struct rt6_info *ret = NULL, *rt_cache; |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 2361 | struct fib6_info *rt; |
Duan Jiong | b55b76b | 2013-09-04 19:44:21 +0800 | [diff] [blame] | 2362 | struct fib6_node *fn; |
| 2363 | |
| 2364 | /* Get the "current" route for this destination and |
Alexander Alemayhu | 67c408c | 2017-01-07 23:53:00 +0100 | [diff] [blame] | 2365 | * check if the redirect has come from appropriate router. |
Duan Jiong | b55b76b | 2013-09-04 19:44:21 +0800 | [diff] [blame] | 2366 | * |
| 2367 | * RFC 4861 specifies that redirects should only be |
| 2368 | * accepted if they come from the nexthop to the target. |
| 2369 | * Due to the way the routes are chosen, this notion |
| 2370 | * is a bit fuzzy and one might need to check all possible |
| 2371 | * routes. |
| 2372 | */ |
| 2373 | |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 2374 | rcu_read_lock(); |
Duan Jiong | b55b76b | 2013-09-04 19:44:21 +0800 | [diff] [blame] | 2375 | fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr); |
| 2376 | restart: |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 2377 | for_each_fib6_node_rt_rcu(fn) { |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 2378 | if (rt->fib6_nh.nh_flags & RTNH_F_DEAD) |
Ido Schimmel | 8067bb8 | 2018-01-07 12:45:09 +0200 | [diff] [blame] | 2379 | continue; |
David Ahern | 1489568 | 2018-04-17 17:33:17 -0700 | [diff] [blame] | 2380 | if (fib6_check_expired(rt)) |
Duan Jiong | b55b76b | 2013-09-04 19:44:21 +0800 | [diff] [blame] | 2381 | continue; |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 2382 | if (rt->fib6_flags & RTF_REJECT) |
Duan Jiong | b55b76b | 2013-09-04 19:44:21 +0800 | [diff] [blame] | 2383 | break; |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 2384 | if (!(rt->fib6_flags & RTF_GATEWAY)) |
Duan Jiong | b55b76b | 2013-09-04 19:44:21 +0800 | [diff] [blame] | 2385 | continue; |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 2386 | if (fl6->flowi6_oif != rt->fib6_nh.nh_dev->ifindex) |
Duan Jiong | b55b76b | 2013-09-04 19:44:21 +0800 | [diff] [blame] | 2387 | continue; |
Wei Wang | 2b760fc | 2017-10-06 12:06:03 -0700 | [diff] [blame] | 2388 | /* rt_cache's gateway might be different from its 'parent' |
| 2389 | * in the case of an ip redirect. |
| 2390 | * So we keep searching in the exception table if the gateway |
| 2391 | * is different. |
| 2392 | */ |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 2393 | if (!ipv6_addr_equal(&rdfl->gateway, &rt->fib6_nh.nh_gw)) { |
Wei Wang | 2b760fc | 2017-10-06 12:06:03 -0700 | [diff] [blame] | 2394 | rt_cache = rt6_find_cached_rt(rt, |
| 2395 | &fl6->daddr, |
| 2396 | &fl6->saddr); |
| 2397 | if (rt_cache && |
| 2398 | ipv6_addr_equal(&rdfl->gateway, |
| 2399 | &rt_cache->rt6i_gateway)) { |
David Ahern | 23fb93a | 2018-04-17 17:33:23 -0700 | [diff] [blame] | 2400 | ret = rt_cache; |
Wei Wang | 2b760fc | 2017-10-06 12:06:03 -0700 | [diff] [blame] | 2401 | break; |
| 2402 | } |
Duan Jiong | b55b76b | 2013-09-04 19:44:21 +0800 | [diff] [blame] | 2403 | continue; |
Wei Wang | 2b760fc | 2017-10-06 12:06:03 -0700 | [diff] [blame] | 2404 | } |
Duan Jiong | b55b76b | 2013-09-04 19:44:21 +0800 | [diff] [blame] | 2405 | break; |
| 2406 | } |
| 2407 | |
| 2408 | if (!rt) |
David Ahern | 421842e | 2018-04-17 17:33:18 -0700 | [diff] [blame] | 2409 | rt = net->ipv6.fib6_null_entry; |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 2410 | else if (rt->fib6_flags & RTF_REJECT) { |
David Ahern | 23fb93a | 2018-04-17 17:33:23 -0700 | [diff] [blame] | 2411 | ret = net->ipv6.ip6_null_entry; |
Martin KaFai Lau | b0a1ba5 | 2015-01-20 19:16:02 -0800 | [diff] [blame] | 2412 | goto out; |
| 2413 | } |
| 2414 | |
David Ahern | 421842e | 2018-04-17 17:33:18 -0700 | [diff] [blame] | 2415 | if (rt == net->ipv6.fib6_null_entry) { |
Martin KaFai Lau | a3c00e4 | 2014-10-20 13:42:43 -0700 | [diff] [blame] | 2416 | fn = fib6_backtrack(fn, &fl6->saddr); |
| 2417 | if (fn) |
| 2418 | goto restart; |
Duan Jiong | b55b76b | 2013-09-04 19:44:21 +0800 | [diff] [blame] | 2419 | } |
Martin KaFai Lau | a3c00e4 | 2014-10-20 13:42:43 -0700 | [diff] [blame] | 2420 | |
Martin KaFai Lau | b0a1ba5 | 2015-01-20 19:16:02 -0800 | [diff] [blame] | 2421 | out: |
David Ahern | 23fb93a | 2018-04-17 17:33:23 -0700 | [diff] [blame] | 2422 | if (ret) |
| 2423 | dst_hold(&ret->dst); |
| 2424 | else |
| 2425 | ret = ip6_create_rt_rcu(rt); |
Duan Jiong | b55b76b | 2013-09-04 19:44:21 +0800 | [diff] [blame] | 2426 | |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 2427 | rcu_read_unlock(); |
Duan Jiong | b55b76b | 2013-09-04 19:44:21 +0800 | [diff] [blame] | 2428 | |
David Ahern | 23fb93a | 2018-04-17 17:33:23 -0700 | [diff] [blame] | 2429 | trace_fib6_table_lookup(net, ret, table, fl6); |
| 2430 | return ret; |
Duan Jiong | b55b76b | 2013-09-04 19:44:21 +0800 | [diff] [blame] | 2431 | }; |
| 2432 | |
| 2433 | static struct dst_entry *ip6_route_redirect(struct net *net, |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 2434 | const struct flowi6 *fl6, |
| 2435 | const struct sk_buff *skb, |
| 2436 | const struct in6_addr *gateway) |
Duan Jiong | b55b76b | 2013-09-04 19:44:21 +0800 | [diff] [blame] | 2437 | { |
| 2438 | int flags = RT6_LOOKUP_F_HAS_SADDR; |
| 2439 | struct ip6rd_flowi rdfl; |
| 2440 | |
| 2441 | rdfl.fl6 = *fl6; |
| 2442 | rdfl.gateway = *gateway; |
| 2443 | |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 2444 | return fib6_rule_lookup(net, &rdfl.fl6, skb, |
Duan Jiong | b55b76b | 2013-09-04 19:44:21 +0800 | [diff] [blame] | 2445 | flags, __ip6_route_redirect); |
| 2446 | } |
| 2447 | |
Lorenzo Colitti | e2d118a | 2016-11-04 02:23:43 +0900 | [diff] [blame] | 2448 | void ip6_redirect(struct sk_buff *skb, struct net *net, int oif, u32 mark, |
| 2449 | kuid_t uid) |
David S. Miller | 3a5ad2e | 2012-07-12 00:08:07 -0700 | [diff] [blame] | 2450 | { |
| 2451 | const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data; |
| 2452 | struct dst_entry *dst; |
| 2453 | struct flowi6 fl6; |
| 2454 | |
| 2455 | memset(&fl6, 0, sizeof(fl6)); |
Julian Anastasov | e374c61 | 2014-04-28 10:51:56 +0300 | [diff] [blame] | 2456 | fl6.flowi6_iif = LOOPBACK_IFINDEX; |
David S. Miller | 3a5ad2e | 2012-07-12 00:08:07 -0700 | [diff] [blame] | 2457 | fl6.flowi6_oif = oif; |
| 2458 | fl6.flowi6_mark = mark; |
David S. Miller | 3a5ad2e | 2012-07-12 00:08:07 -0700 | [diff] [blame] | 2459 | fl6.daddr = iph->daddr; |
| 2460 | fl6.saddr = iph->saddr; |
YOSHIFUJI Hideaki / 吉藤英明 | 6502ca5 | 2013-01-13 05:01:51 +0000 | [diff] [blame] | 2461 | fl6.flowlabel = ip6_flowinfo(iph); |
Lorenzo Colitti | e2d118a | 2016-11-04 02:23:43 +0900 | [diff] [blame] | 2462 | fl6.flowi6_uid = uid; |
David S. Miller | 3a5ad2e | 2012-07-12 00:08:07 -0700 | [diff] [blame] | 2463 | |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 2464 | dst = ip6_route_redirect(net, &fl6, skb, &ipv6_hdr(skb)->saddr); |
Duan Jiong | b55b76b | 2013-09-04 19:44:21 +0800 | [diff] [blame] | 2465 | rt6_do_redirect(dst, NULL, skb); |
David S. Miller | 3a5ad2e | 2012-07-12 00:08:07 -0700 | [diff] [blame] | 2466 | dst_release(dst); |
| 2467 | } |
| 2468 | EXPORT_SYMBOL_GPL(ip6_redirect); |
| 2469 | |
Duan Jiong | c92a59e | 2013-08-22 12:07:35 +0800 | [diff] [blame] | 2470 | void ip6_redirect_no_header(struct sk_buff *skb, struct net *net, int oif, |
| 2471 | u32 mark) |
| 2472 | { |
| 2473 | const struct ipv6hdr *iph = ipv6_hdr(skb); |
| 2474 | const struct rd_msg *msg = (struct rd_msg *)icmp6_hdr(skb); |
| 2475 | struct dst_entry *dst; |
| 2476 | struct flowi6 fl6; |
| 2477 | |
| 2478 | memset(&fl6, 0, sizeof(fl6)); |
Julian Anastasov | e374c61 | 2014-04-28 10:51:56 +0300 | [diff] [blame] | 2479 | fl6.flowi6_iif = LOOPBACK_IFINDEX; |
Duan Jiong | c92a59e | 2013-08-22 12:07:35 +0800 | [diff] [blame] | 2480 | fl6.flowi6_oif = oif; |
| 2481 | fl6.flowi6_mark = mark; |
Duan Jiong | c92a59e | 2013-08-22 12:07:35 +0800 | [diff] [blame] | 2482 | fl6.daddr = msg->dest; |
| 2483 | fl6.saddr = iph->daddr; |
Lorenzo Colitti | e2d118a | 2016-11-04 02:23:43 +0900 | [diff] [blame] | 2484 | fl6.flowi6_uid = sock_net_uid(net, NULL); |
Duan Jiong | c92a59e | 2013-08-22 12:07:35 +0800 | [diff] [blame] | 2485 | |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 2486 | dst = ip6_route_redirect(net, &fl6, skb, &iph->saddr); |
Duan Jiong | b55b76b | 2013-09-04 19:44:21 +0800 | [diff] [blame] | 2487 | rt6_do_redirect(dst, NULL, skb); |
Duan Jiong | c92a59e | 2013-08-22 12:07:35 +0800 | [diff] [blame] | 2488 | dst_release(dst); |
| 2489 | } |
| 2490 | |
David S. Miller | 3a5ad2e | 2012-07-12 00:08:07 -0700 | [diff] [blame] | 2491 | void ip6_sk_redirect(struct sk_buff *skb, struct sock *sk) |
| 2492 | { |
Lorenzo Colitti | e2d118a | 2016-11-04 02:23:43 +0900 | [diff] [blame] | 2493 | ip6_redirect(skb, sock_net(sk), sk->sk_bound_dev_if, sk->sk_mark, |
| 2494 | sk->sk_uid); |
David S. Miller | 3a5ad2e | 2012-07-12 00:08:07 -0700 | [diff] [blame] | 2495 | } |
| 2496 | EXPORT_SYMBOL_GPL(ip6_sk_redirect); |
| 2497 | |
David S. Miller | 0dbaee3 | 2010-12-13 12:52:14 -0800 | [diff] [blame] | 2498 | static unsigned int ip6_default_advmss(const struct dst_entry *dst) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2499 | { |
David S. Miller | 0dbaee3 | 2010-12-13 12:52:14 -0800 | [diff] [blame] | 2500 | struct net_device *dev = dst->dev; |
| 2501 | unsigned int mtu = dst_mtu(dst); |
| 2502 | struct net *net = dev_net(dev); |
| 2503 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2504 | mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr); |
| 2505 | |
Daniel Lezcano | 5578689 | 2008-03-04 13:47:47 -0800 | [diff] [blame] | 2506 | if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss) |
| 2507 | mtu = net->ipv6.sysctl.ip6_rt_min_advmss; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2508 | |
| 2509 | /* |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 2510 | * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and |
| 2511 | * corresponding MSS is IPV6_MAXPLEN - tcp_header_size. |
| 2512 | * IPV6_MAXPLEN is also valid and means: "any MSS, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2513 | * rely only on pmtu discovery" |
| 2514 | */ |
| 2515 | if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr)) |
| 2516 | mtu = IPV6_MAXPLEN; |
| 2517 | return mtu; |
| 2518 | } |
| 2519 | |
Steffen Klassert | ebb762f | 2011-11-23 02:12:51 +0000 | [diff] [blame] | 2520 | static unsigned int ip6_mtu(const struct dst_entry *dst) |
David S. Miller | d33e455 | 2010-12-14 13:01:14 -0800 | [diff] [blame] | 2521 | { |
David S. Miller | d33e455 | 2010-12-14 13:01:14 -0800 | [diff] [blame] | 2522 | struct inet6_dev *idev; |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 2523 | unsigned int mtu; |
Steffen Klassert | 618f9bc | 2011-11-23 02:13:31 +0000 | [diff] [blame] | 2524 | |
Martin KaFai Lau | 4b32b5a | 2015-04-28 13:03:06 -0700 | [diff] [blame] | 2525 | mtu = dst_metric_raw(dst, RTAX_MTU); |
| 2526 | if (mtu) |
| 2527 | goto out; |
| 2528 | |
Steffen Klassert | 618f9bc | 2011-11-23 02:13:31 +0000 | [diff] [blame] | 2529 | mtu = IPV6_MIN_MTU; |
David S. Miller | d33e455 | 2010-12-14 13:01:14 -0800 | [diff] [blame] | 2530 | |
| 2531 | rcu_read_lock(); |
| 2532 | idev = __in6_dev_get(dst->dev); |
| 2533 | if (idev) |
| 2534 | mtu = idev->cnf.mtu6; |
| 2535 | rcu_read_unlock(); |
| 2536 | |
Eric Dumazet | 30f78d8 | 2014-04-10 21:23:36 -0700 | [diff] [blame] | 2537 | out: |
Roopa Prabhu | 14972cb | 2016-08-24 20:10:43 -0700 | [diff] [blame] | 2538 | mtu = min_t(unsigned int, mtu, IP6_MAX_MTU); |
| 2539 | |
| 2540 | return mtu - lwtunnel_headroom(dst->lwtstate, mtu); |
David S. Miller | d33e455 | 2010-12-14 13:01:14 -0800 | [diff] [blame] | 2541 | } |
| 2542 | |
YOSHIFUJI Hideaki | 3b00944 | 2007-12-06 16:11:48 -0800 | [diff] [blame] | 2543 | struct dst_entry *icmp6_dst_alloc(struct net_device *dev, |
David S. Miller | 87a1157 | 2011-12-06 17:04:13 -0500 | [diff] [blame] | 2544 | struct flowi6 *fl6) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2545 | { |
David S. Miller | 87a1157 | 2011-12-06 17:04:13 -0500 | [diff] [blame] | 2546 | struct dst_entry *dst; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2547 | struct rt6_info *rt; |
| 2548 | struct inet6_dev *idev = in6_dev_get(dev); |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 2549 | struct net *net = dev_net(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2550 | |
David S. Miller | 3830847 | 2011-12-03 18:02:47 -0500 | [diff] [blame] | 2551 | if (unlikely(!idev)) |
Eric Dumazet | 122bdf6 | 2012-03-14 21:13:11 +0000 | [diff] [blame] | 2552 | return ERR_PTR(-ENODEV); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2553 | |
Martin KaFai Lau | ad70686 | 2015-08-14 11:05:52 -0700 | [diff] [blame] | 2554 | rt = ip6_dst_alloc(net, dev, 0); |
David S. Miller | 3830847 | 2011-12-03 18:02:47 -0500 | [diff] [blame] | 2555 | if (unlikely(!rt)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2556 | in6_dev_put(idev); |
David S. Miller | 87a1157 | 2011-12-06 17:04:13 -0500 | [diff] [blame] | 2557 | dst = ERR_PTR(-ENOMEM); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2558 | goto out; |
| 2559 | } |
| 2560 | |
Yan, Zheng | 8e2ec63 | 2011-09-05 21:34:30 +0000 | [diff] [blame] | 2561 | rt->dst.flags |= DST_HOST; |
Brendan McGrath | 588753f | 2017-12-13 22:14:57 +1100 | [diff] [blame] | 2562 | rt->dst.input = ip6_input; |
Yan, Zheng | 8e2ec63 | 2011-09-05 21:34:30 +0000 | [diff] [blame] | 2563 | rt->dst.output = ip6_output; |
Julian Anastasov | 550bab4 | 2013-10-20 15:43:04 +0300 | [diff] [blame] | 2564 | rt->rt6i_gateway = fl6->daddr; |
David S. Miller | 87a1157 | 2011-12-06 17:04:13 -0500 | [diff] [blame] | 2565 | rt->rt6i_dst.addr = fl6->daddr; |
Yan, Zheng | 8e2ec63 | 2011-09-05 21:34:30 +0000 | [diff] [blame] | 2566 | rt->rt6i_dst.plen = 128; |
| 2567 | rt->rt6i_idev = idev; |
Li RongQing | 14edd87 | 2012-10-24 14:01:18 +0800 | [diff] [blame] | 2568 | dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2569 | |
Ido Schimmel | 4c981e2 | 2018-01-07 12:45:04 +0200 | [diff] [blame] | 2570 | /* Add this dst into uncached_list so that rt6_disable_ip() can |
Wei Wang | 587fea7 | 2017-06-17 10:42:36 -0700 | [diff] [blame] | 2571 | * do proper release of the net_device |
| 2572 | */ |
| 2573 | rt6_uncached_list_add(rt); |
Wei Wang | 81eb844 | 2017-10-06 12:06:11 -0700 | [diff] [blame] | 2574 | atomic_inc(&net->ipv6.rt6_stats->fib_rt_uncache); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2575 | |
David S. Miller | 87a1157 | 2011-12-06 17:04:13 -0500 | [diff] [blame] | 2576 | dst = xfrm_lookup(net, &rt->dst, flowi6_to_flowi(fl6), NULL, 0); |
| 2577 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2578 | out: |
David S. Miller | 87a1157 | 2011-12-06 17:04:13 -0500 | [diff] [blame] | 2579 | return dst; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2580 | } |
| 2581 | |
Daniel Lezcano | 569d364 | 2008-01-18 03:56:57 -0800 | [diff] [blame] | 2582 | static int ip6_dst_gc(struct dst_ops *ops) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2583 | { |
Alexey Dobriyan | 86393e5 | 2009-08-29 01:34:49 +0000 | [diff] [blame] | 2584 | struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops); |
Daniel Lezcano | 7019b78 | 2008-03-04 13:50:14 -0800 | [diff] [blame] | 2585 | int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval; |
| 2586 | int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size; |
| 2587 | int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity; |
| 2588 | int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout; |
| 2589 | unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc; |
Eric Dumazet | fc66f95 | 2010-10-08 06:37:34 +0000 | [diff] [blame] | 2590 | int entries; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2591 | |
Eric Dumazet | fc66f95 | 2010-10-08 06:37:34 +0000 | [diff] [blame] | 2592 | entries = dst_entries_get_fast(ops); |
Michal Kubeček | 49a18d8 | 2013-08-01 10:04:24 +0200 | [diff] [blame] | 2593 | if (time_after(rt_last_gc + rt_min_interval, jiffies) && |
Eric Dumazet | fc66f95 | 2010-10-08 06:37:34 +0000 | [diff] [blame] | 2594 | entries <= rt_max_size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2595 | goto out; |
| 2596 | |
Benjamin Thery | 6891a34 | 2008-03-04 13:49:47 -0800 | [diff] [blame] | 2597 | net->ipv6.ip6_rt_gc_expire++; |
Li RongQing | 1495664 | 2014-05-19 17:30:28 +0800 | [diff] [blame] | 2598 | fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net, true); |
Eric Dumazet | fc66f95 | 2010-10-08 06:37:34 +0000 | [diff] [blame] | 2599 | entries = dst_entries_get_slow(ops); |
| 2600 | if (entries < ops->gc_thresh) |
Daniel Lezcano | 7019b78 | 2008-03-04 13:50:14 -0800 | [diff] [blame] | 2601 | net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2602 | out: |
Daniel Lezcano | 7019b78 | 2008-03-04 13:50:14 -0800 | [diff] [blame] | 2603 | 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] | 2604 | return entries > rt_max_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2605 | } |
| 2606 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 2607 | static int ip6_convert_metrics(struct net *net, struct fib6_info *rt, |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 2608 | struct fib6_config *cfg) |
Florian Westphal | e715b6d | 2015-01-05 23:57:44 +0100 | [diff] [blame] | 2609 | { |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 2610 | int err = 0; |
Florian Westphal | e715b6d | 2015-01-05 23:57:44 +0100 | [diff] [blame] | 2611 | |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 2612 | if (cfg->fc_mx) { |
| 2613 | rt->fib6_metrics = kzalloc(sizeof(*rt->fib6_metrics), |
| 2614 | GFP_KERNEL); |
| 2615 | if (unlikely(!rt->fib6_metrics)) |
| 2616 | return -ENOMEM; |
Florian Westphal | e715b6d | 2015-01-05 23:57:44 +0100 | [diff] [blame] | 2617 | |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 2618 | refcount_set(&rt->fib6_metrics->refcnt, 1); |
Florian Westphal | e715b6d | 2015-01-05 23:57:44 +0100 | [diff] [blame] | 2619 | |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 2620 | err = ip_metrics_convert(net, cfg->fc_mx, cfg->fc_mx_len, |
| 2621 | rt->fib6_metrics->metrics); |
Florian Westphal | e715b6d | 2015-01-05 23:57:44 +0100 | [diff] [blame] | 2622 | } |
| 2623 | |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 2624 | return err; |
Florian Westphal | e715b6d | 2015-01-05 23:57:44 +0100 | [diff] [blame] | 2625 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2626 | |
David Ahern | 8c14586 | 2016-04-24 21:26:04 -0700 | [diff] [blame] | 2627 | static struct rt6_info *ip6_nh_lookup_table(struct net *net, |
| 2628 | struct fib6_config *cfg, |
David Ahern | f4797b3 | 2018-01-25 16:55:08 -0800 | [diff] [blame] | 2629 | const struct in6_addr *gw_addr, |
| 2630 | u32 tbid, int flags) |
David Ahern | 8c14586 | 2016-04-24 21:26:04 -0700 | [diff] [blame] | 2631 | { |
| 2632 | struct flowi6 fl6 = { |
| 2633 | .flowi6_oif = cfg->fc_ifindex, |
| 2634 | .daddr = *gw_addr, |
| 2635 | .saddr = cfg->fc_prefsrc, |
| 2636 | }; |
| 2637 | struct fib6_table *table; |
| 2638 | struct rt6_info *rt; |
David Ahern | 8c14586 | 2016-04-24 21:26:04 -0700 | [diff] [blame] | 2639 | |
David Ahern | f4797b3 | 2018-01-25 16:55:08 -0800 | [diff] [blame] | 2640 | table = fib6_get_table(net, tbid); |
David Ahern | 8c14586 | 2016-04-24 21:26:04 -0700 | [diff] [blame] | 2641 | if (!table) |
| 2642 | return NULL; |
| 2643 | |
| 2644 | if (!ipv6_addr_any(&cfg->fc_prefsrc)) |
| 2645 | flags |= RT6_LOOKUP_F_HAS_SADDR; |
| 2646 | |
David Ahern | f4797b3 | 2018-01-25 16:55:08 -0800 | [diff] [blame] | 2647 | flags |= RT6_LOOKUP_F_IGNORE_LINKSTATE; |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 2648 | rt = ip6_pol_route(net, table, cfg->fc_ifindex, &fl6, NULL, flags); |
David Ahern | 8c14586 | 2016-04-24 21:26:04 -0700 | [diff] [blame] | 2649 | |
| 2650 | /* if table lookup failed, fall back to full lookup */ |
| 2651 | if (rt == net->ipv6.ip6_null_entry) { |
| 2652 | ip6_rt_put(rt); |
| 2653 | rt = NULL; |
| 2654 | } |
| 2655 | |
| 2656 | return rt; |
| 2657 | } |
| 2658 | |
David Ahern | fc1e64e | 2018-01-25 16:55:09 -0800 | [diff] [blame] | 2659 | static int ip6_route_check_nh_onlink(struct net *net, |
| 2660 | struct fib6_config *cfg, |
David Ahern | 9fbb704 | 2018-03-13 08:29:36 -0700 | [diff] [blame] | 2661 | const struct net_device *dev, |
David Ahern | fc1e64e | 2018-01-25 16:55:09 -0800 | [diff] [blame] | 2662 | struct netlink_ext_ack *extack) |
| 2663 | { |
David Ahern | 44750f8 | 2018-02-06 13:17:06 -0800 | [diff] [blame] | 2664 | u32 tbid = l3mdev_fib_table(dev) ? : RT_TABLE_MAIN; |
David Ahern | fc1e64e | 2018-01-25 16:55:09 -0800 | [diff] [blame] | 2665 | const struct in6_addr *gw_addr = &cfg->fc_gateway; |
| 2666 | u32 flags = RTF_LOCAL | RTF_ANYCAST | RTF_REJECT; |
| 2667 | struct rt6_info *grt; |
| 2668 | int err; |
| 2669 | |
| 2670 | err = 0; |
| 2671 | grt = ip6_nh_lookup_table(net, cfg, gw_addr, tbid, 0); |
| 2672 | if (grt) { |
David Ahern | 58e354c | 2018-02-06 12:14:12 -0800 | [diff] [blame] | 2673 | if (!grt->dst.error && |
| 2674 | (grt->rt6i_flags & flags || dev != grt->dst.dev)) { |
David Ahern | 44750f8 | 2018-02-06 13:17:06 -0800 | [diff] [blame] | 2675 | NL_SET_ERR_MSG(extack, |
| 2676 | "Nexthop has invalid gateway or device mismatch"); |
David Ahern | fc1e64e | 2018-01-25 16:55:09 -0800 | [diff] [blame] | 2677 | err = -EINVAL; |
| 2678 | } |
| 2679 | |
| 2680 | ip6_rt_put(grt); |
| 2681 | } |
| 2682 | |
| 2683 | return err; |
| 2684 | } |
| 2685 | |
David Ahern | 1edce99 | 2018-01-25 16:55:07 -0800 | [diff] [blame] | 2686 | static int ip6_route_check_nh(struct net *net, |
| 2687 | struct fib6_config *cfg, |
| 2688 | struct net_device **_dev, |
| 2689 | struct inet6_dev **idev) |
| 2690 | { |
| 2691 | const struct in6_addr *gw_addr = &cfg->fc_gateway; |
| 2692 | struct net_device *dev = _dev ? *_dev : NULL; |
| 2693 | struct rt6_info *grt = NULL; |
| 2694 | int err = -EHOSTUNREACH; |
| 2695 | |
| 2696 | if (cfg->fc_table) { |
David Ahern | f4797b3 | 2018-01-25 16:55:08 -0800 | [diff] [blame] | 2697 | int flags = RT6_LOOKUP_F_IFACE; |
| 2698 | |
| 2699 | grt = ip6_nh_lookup_table(net, cfg, gw_addr, |
| 2700 | cfg->fc_table, flags); |
David Ahern | 1edce99 | 2018-01-25 16:55:07 -0800 | [diff] [blame] | 2701 | if (grt) { |
| 2702 | if (grt->rt6i_flags & RTF_GATEWAY || |
| 2703 | (dev && dev != grt->dst.dev)) { |
| 2704 | ip6_rt_put(grt); |
| 2705 | grt = NULL; |
| 2706 | } |
| 2707 | } |
| 2708 | } |
| 2709 | |
| 2710 | if (!grt) |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 2711 | grt = rt6_lookup(net, gw_addr, NULL, cfg->fc_ifindex, NULL, 1); |
David Ahern | 1edce99 | 2018-01-25 16:55:07 -0800 | [diff] [blame] | 2712 | |
| 2713 | if (!grt) |
| 2714 | goto out; |
| 2715 | |
| 2716 | if (dev) { |
| 2717 | if (dev != grt->dst.dev) { |
| 2718 | ip6_rt_put(grt); |
| 2719 | goto out; |
| 2720 | } |
| 2721 | } else { |
| 2722 | *_dev = dev = grt->dst.dev; |
| 2723 | *idev = grt->rt6i_idev; |
| 2724 | dev_hold(dev); |
| 2725 | in6_dev_hold(grt->rt6i_idev); |
| 2726 | } |
| 2727 | |
| 2728 | if (!(grt->rt6i_flags & RTF_GATEWAY)) |
| 2729 | err = 0; |
| 2730 | |
| 2731 | ip6_rt_put(grt); |
| 2732 | |
| 2733 | out: |
| 2734 | return err; |
| 2735 | } |
| 2736 | |
David Ahern | 9fbb704 | 2018-03-13 08:29:36 -0700 | [diff] [blame] | 2737 | static int ip6_validate_gw(struct net *net, struct fib6_config *cfg, |
| 2738 | struct net_device **_dev, struct inet6_dev **idev, |
| 2739 | struct netlink_ext_ack *extack) |
| 2740 | { |
| 2741 | const struct in6_addr *gw_addr = &cfg->fc_gateway; |
| 2742 | int gwa_type = ipv6_addr_type(gw_addr); |
David Ahern | 232378e | 2018-03-13 08:29:37 -0700 | [diff] [blame] | 2743 | bool skip_dev = gwa_type & IPV6_ADDR_LINKLOCAL ? false : true; |
David Ahern | 9fbb704 | 2018-03-13 08:29:36 -0700 | [diff] [blame] | 2744 | const struct net_device *dev = *_dev; |
David Ahern | 232378e | 2018-03-13 08:29:37 -0700 | [diff] [blame] | 2745 | bool need_addr_check = !dev; |
David Ahern | 9fbb704 | 2018-03-13 08:29:36 -0700 | [diff] [blame] | 2746 | int err = -EINVAL; |
| 2747 | |
| 2748 | /* if gw_addr is local we will fail to detect this in case |
| 2749 | * address is still TENTATIVE (DAD in progress). rt6_lookup() |
| 2750 | * will return already-added prefix route via interface that |
| 2751 | * prefix route was assigned to, which might be non-loopback. |
| 2752 | */ |
David Ahern | 232378e | 2018-03-13 08:29:37 -0700 | [diff] [blame] | 2753 | if (dev && |
| 2754 | ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) { |
| 2755 | NL_SET_ERR_MSG(extack, "Gateway can not be a local address"); |
David Ahern | 9fbb704 | 2018-03-13 08:29:36 -0700 | [diff] [blame] | 2756 | goto out; |
| 2757 | } |
| 2758 | |
| 2759 | if (gwa_type != (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_UNICAST)) { |
| 2760 | /* IPv6 strictly inhibits using not link-local |
| 2761 | * addresses as nexthop address. |
| 2762 | * Otherwise, router will not able to send redirects. |
| 2763 | * It is very good, but in some (rare!) circumstances |
| 2764 | * (SIT, PtP, NBMA NOARP links) it is handy to allow |
| 2765 | * some exceptions. --ANK |
| 2766 | * We allow IPv4-mapped nexthops to support RFC4798-type |
| 2767 | * addressing |
| 2768 | */ |
| 2769 | if (!(gwa_type & (IPV6_ADDR_UNICAST | IPV6_ADDR_MAPPED))) { |
| 2770 | NL_SET_ERR_MSG(extack, "Invalid gateway address"); |
| 2771 | goto out; |
| 2772 | } |
| 2773 | |
| 2774 | if (cfg->fc_flags & RTNH_F_ONLINK) |
| 2775 | err = ip6_route_check_nh_onlink(net, cfg, dev, extack); |
| 2776 | else |
| 2777 | err = ip6_route_check_nh(net, cfg, _dev, idev); |
| 2778 | |
| 2779 | if (err) |
| 2780 | goto out; |
| 2781 | } |
| 2782 | |
| 2783 | /* reload in case device was changed */ |
| 2784 | dev = *_dev; |
| 2785 | |
| 2786 | err = -EINVAL; |
| 2787 | if (!dev) { |
| 2788 | NL_SET_ERR_MSG(extack, "Egress device not specified"); |
| 2789 | goto out; |
| 2790 | } else if (dev->flags & IFF_LOOPBACK) { |
| 2791 | NL_SET_ERR_MSG(extack, |
| 2792 | "Egress device can not be loopback device for this route"); |
| 2793 | goto out; |
| 2794 | } |
David Ahern | 232378e | 2018-03-13 08:29:37 -0700 | [diff] [blame] | 2795 | |
| 2796 | /* if we did not check gw_addr above, do so now that the |
| 2797 | * egress device has been resolved. |
| 2798 | */ |
| 2799 | if (need_addr_check && |
| 2800 | ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) { |
| 2801 | NL_SET_ERR_MSG(extack, "Gateway can not be a local address"); |
| 2802 | goto out; |
| 2803 | } |
| 2804 | |
David Ahern | 9fbb704 | 2018-03-13 08:29:36 -0700 | [diff] [blame] | 2805 | err = 0; |
| 2806 | out: |
| 2807 | return err; |
| 2808 | } |
| 2809 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 2810 | static struct fib6_info *ip6_route_info_create(struct fib6_config *cfg, |
David Ahern | acb54e3 | 2018-04-17 17:33:22 -0700 | [diff] [blame] | 2811 | gfp_t gfp_flags, |
David Ahern | 333c430 | 2017-05-21 10:12:04 -0600 | [diff] [blame] | 2812 | struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2813 | { |
Daniel Lezcano | 5578689 | 2008-03-04 13:47:47 -0800 | [diff] [blame] | 2814 | struct net *net = cfg->fc_nlinfo.nl_net; |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 2815 | struct fib6_info *rt = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2816 | struct net_device *dev = NULL; |
| 2817 | struct inet6_dev *idev = NULL; |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 2818 | struct fib6_table *table; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2819 | int addr_type; |
Roopa Prabhu | 8c5b83f | 2015-10-10 08:26:36 -0700 | [diff] [blame] | 2820 | int err = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2821 | |
David Ahern | 557c44b | 2017-04-19 14:19:43 -0700 | [diff] [blame] | 2822 | /* RTF_PCPU is an internal flag; can not be set by userspace */ |
David Ahern | d5d531c | 2017-05-21 10:12:05 -0600 | [diff] [blame] | 2823 | if (cfg->fc_flags & RTF_PCPU) { |
| 2824 | NL_SET_ERR_MSG(extack, "Userspace can not set RTF_PCPU"); |
David Ahern | 557c44b | 2017-04-19 14:19:43 -0700 | [diff] [blame] | 2825 | goto out; |
David Ahern | d5d531c | 2017-05-21 10:12:05 -0600 | [diff] [blame] | 2826 | } |
David Ahern | 557c44b | 2017-04-19 14:19:43 -0700 | [diff] [blame] | 2827 | |
Wei Wang | 2ea2352 | 2017-10-27 17:30:12 -0700 | [diff] [blame] | 2828 | /* RTF_CACHE is an internal flag; can not be set by userspace */ |
| 2829 | if (cfg->fc_flags & RTF_CACHE) { |
| 2830 | NL_SET_ERR_MSG(extack, "Userspace can not set RTF_CACHE"); |
| 2831 | goto out; |
| 2832 | } |
| 2833 | |
David Ahern | e8478e8 | 2018-04-17 17:33:13 -0700 | [diff] [blame] | 2834 | if (cfg->fc_type > RTN_MAX) { |
| 2835 | NL_SET_ERR_MSG(extack, "Invalid route type"); |
| 2836 | goto out; |
| 2837 | } |
| 2838 | |
David Ahern | d5d531c | 2017-05-21 10:12:05 -0600 | [diff] [blame] | 2839 | if (cfg->fc_dst_len > 128) { |
| 2840 | NL_SET_ERR_MSG(extack, "Invalid prefix length"); |
Roopa Prabhu | 8c5b83f | 2015-10-10 08:26:36 -0700 | [diff] [blame] | 2841 | goto out; |
David Ahern | d5d531c | 2017-05-21 10:12:05 -0600 | [diff] [blame] | 2842 | } |
| 2843 | if (cfg->fc_src_len > 128) { |
| 2844 | NL_SET_ERR_MSG(extack, "Invalid source address length"); |
| 2845 | goto out; |
| 2846 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2847 | #ifndef CONFIG_IPV6_SUBTREES |
David Ahern | d5d531c | 2017-05-21 10:12:05 -0600 | [diff] [blame] | 2848 | if (cfg->fc_src_len) { |
| 2849 | NL_SET_ERR_MSG(extack, |
| 2850 | "Specifying source address requires IPV6_SUBTREES to be enabled"); |
Roopa Prabhu | 8c5b83f | 2015-10-10 08:26:36 -0700 | [diff] [blame] | 2851 | goto out; |
David Ahern | d5d531c | 2017-05-21 10:12:05 -0600 | [diff] [blame] | 2852 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2853 | #endif |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 2854 | if (cfg->fc_ifindex) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2855 | err = -ENODEV; |
Daniel Lezcano | 5578689 | 2008-03-04 13:47:47 -0800 | [diff] [blame] | 2856 | dev = dev_get_by_index(net, cfg->fc_ifindex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2857 | if (!dev) |
| 2858 | goto out; |
| 2859 | idev = in6_dev_get(dev); |
| 2860 | if (!idev) |
| 2861 | goto out; |
| 2862 | } |
| 2863 | |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 2864 | if (cfg->fc_metric == 0) |
| 2865 | cfg->fc_metric = IP6_RT_PRIO_USER; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2866 | |
David Ahern | fc1e64e | 2018-01-25 16:55:09 -0800 | [diff] [blame] | 2867 | if (cfg->fc_flags & RTNH_F_ONLINK) { |
| 2868 | if (!dev) { |
| 2869 | NL_SET_ERR_MSG(extack, |
| 2870 | "Nexthop device required for onlink"); |
| 2871 | err = -ENODEV; |
| 2872 | goto out; |
| 2873 | } |
| 2874 | |
| 2875 | if (!(dev->flags & IFF_UP)) { |
| 2876 | NL_SET_ERR_MSG(extack, "Nexthop device is not up"); |
| 2877 | err = -ENETDOWN; |
| 2878 | goto out; |
| 2879 | } |
| 2880 | } |
| 2881 | |
Matti Vaittinen | d71314b | 2011-11-14 00:14:49 +0000 | [diff] [blame] | 2882 | err = -ENOBUFS; |
David S. Miller | 3830847 | 2011-12-03 18:02:47 -0500 | [diff] [blame] | 2883 | if (cfg->fc_nlinfo.nlh && |
| 2884 | !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) { |
Matti Vaittinen | d71314b | 2011-11-14 00:14:49 +0000 | [diff] [blame] | 2885 | table = fib6_get_table(net, cfg->fc_table); |
David S. Miller | 3830847 | 2011-12-03 18:02:47 -0500 | [diff] [blame] | 2886 | if (!table) { |
Joe Perches | f321383 | 2012-05-15 14:11:53 +0000 | [diff] [blame] | 2887 | 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] | 2888 | table = fib6_new_table(net, cfg->fc_table); |
| 2889 | } |
| 2890 | } else { |
| 2891 | table = fib6_new_table(net, cfg->fc_table); |
| 2892 | } |
David S. Miller | 3830847 | 2011-12-03 18:02:47 -0500 | [diff] [blame] | 2893 | |
| 2894 | if (!table) |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 2895 | goto out; |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 2896 | |
David Ahern | 93531c6 | 2018-04-17 17:33:25 -0700 | [diff] [blame] | 2897 | err = -ENOMEM; |
| 2898 | rt = fib6_info_alloc(gfp_flags); |
| 2899 | if (!rt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2900 | goto out; |
David Ahern | 93531c6 | 2018-04-17 17:33:25 -0700 | [diff] [blame] | 2901 | |
| 2902 | if (cfg->fc_flags & RTF_ADDRCONF) |
| 2903 | rt->dst_nocount = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2904 | |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 2905 | err = ip6_convert_metrics(net, rt, cfg); |
| 2906 | if (err < 0) |
| 2907 | goto out; |
| 2908 | |
Gao feng | 1716a96 | 2012-04-06 00:13:10 +0000 | [diff] [blame] | 2909 | if (cfg->fc_flags & RTF_EXPIRES) |
David Ahern | 1489568 | 2018-04-17 17:33:17 -0700 | [diff] [blame] | 2910 | fib6_set_expires(rt, jiffies + |
Gao feng | 1716a96 | 2012-04-06 00:13:10 +0000 | [diff] [blame] | 2911 | clock_t_to_jiffies(cfg->fc_expires)); |
| 2912 | else |
David Ahern | 1489568 | 2018-04-17 17:33:17 -0700 | [diff] [blame] | 2913 | fib6_clean_expires(rt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2914 | |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 2915 | if (cfg->fc_protocol == RTPROT_UNSPEC) |
| 2916 | cfg->fc_protocol = RTPROT_BOOT; |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 2917 | rt->fib6_protocol = cfg->fc_protocol; |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 2918 | |
| 2919 | addr_type = ipv6_addr_type(&cfg->fc_dst); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2920 | |
Roopa Prabhu | 19e42e4 | 2015-07-21 10:43:48 +0200 | [diff] [blame] | 2921 | if (cfg->fc_encap) { |
| 2922 | struct lwtunnel_state *lwtstate; |
| 2923 | |
David Ahern | 30357d7 | 2017-01-30 12:07:37 -0800 | [diff] [blame] | 2924 | err = lwtunnel_build_state(cfg->fc_encap_type, |
Tom Herbert | 127eb7c | 2015-08-24 09:45:41 -0700 | [diff] [blame] | 2925 | cfg->fc_encap, AF_INET6, cfg, |
David Ahern | 9ae2872 | 2017-05-27 16:19:28 -0600 | [diff] [blame] | 2926 | &lwtstate, extack); |
Roopa Prabhu | 19e42e4 | 2015-07-21 10:43:48 +0200 | [diff] [blame] | 2927 | if (err) |
| 2928 | goto out; |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 2929 | rt->fib6_nh.nh_lwtstate = lwtstate_get(lwtstate); |
Roopa Prabhu | 19e42e4 | 2015-07-21 10:43:48 +0200 | [diff] [blame] | 2930 | } |
| 2931 | |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 2932 | ipv6_addr_prefix(&rt->fib6_dst.addr, &cfg->fc_dst, cfg->fc_dst_len); |
| 2933 | rt->fib6_dst.plen = cfg->fc_dst_len; |
| 2934 | if (rt->fib6_dst.plen == 128) |
David Ahern | 3b6761d | 2018-04-17 17:33:20 -0700 | [diff] [blame] | 2935 | rt->dst_host = true; |
Michal Kubeček | e5fd387 | 2014-03-27 13:04:08 +0100 | [diff] [blame] | 2936 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2937 | #ifdef CONFIG_IPV6_SUBTREES |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 2938 | ipv6_addr_prefix(&rt->fib6_src.addr, &cfg->fc_src, cfg->fc_src_len); |
| 2939 | rt->fib6_src.plen = cfg->fc_src_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2940 | #endif |
| 2941 | |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 2942 | rt->fib6_metric = cfg->fc_metric; |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 2943 | rt->fib6_nh.nh_weight = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2944 | |
David Ahern | e8478e8 | 2018-04-17 17:33:13 -0700 | [diff] [blame] | 2945 | rt->fib6_type = cfg->fc_type; |
| 2946 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2947 | /* We cannot add true routes via loopback here, |
| 2948 | they would result in kernel looping; promote them to reject routes |
| 2949 | */ |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 2950 | if ((cfg->fc_flags & RTF_REJECT) || |
David S. Miller | 3830847 | 2011-12-03 18:02:47 -0500 | [diff] [blame] | 2951 | (dev && (dev->flags & IFF_LOOPBACK) && |
| 2952 | !(addr_type & IPV6_ADDR_LOOPBACK) && |
| 2953 | !(cfg->fc_flags & RTF_LOCAL))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2954 | /* hold loopback dev/idev if we haven't done so. */ |
Daniel Lezcano | 5578689 | 2008-03-04 13:47:47 -0800 | [diff] [blame] | 2955 | if (dev != net->loopback_dev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2956 | if (dev) { |
| 2957 | dev_put(dev); |
| 2958 | in6_dev_put(idev); |
| 2959 | } |
Daniel Lezcano | 5578689 | 2008-03-04 13:47:47 -0800 | [diff] [blame] | 2960 | dev = net->loopback_dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2961 | dev_hold(dev); |
| 2962 | idev = in6_dev_get(dev); |
| 2963 | if (!idev) { |
| 2964 | err = -ENODEV; |
| 2965 | goto out; |
| 2966 | } |
| 2967 | } |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 2968 | rt->fib6_flags = RTF_REJECT|RTF_NONEXTHOP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2969 | goto install_route; |
| 2970 | } |
| 2971 | |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 2972 | if (cfg->fc_flags & RTF_GATEWAY) { |
David Ahern | 9fbb704 | 2018-03-13 08:29:36 -0700 | [diff] [blame] | 2973 | err = ip6_validate_gw(net, cfg, &dev, &idev, extack); |
| 2974 | if (err) |
Florian Westphal | 48ed7b2 | 2015-05-21 00:25:41 +0200 | [diff] [blame] | 2975 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2976 | |
David Ahern | 93531c6 | 2018-04-17 17:33:25 -0700 | [diff] [blame] | 2977 | rt->fib6_nh.nh_gw = cfg->fc_gateway; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2978 | } |
| 2979 | |
| 2980 | err = -ENODEV; |
David S. Miller | 3830847 | 2011-12-03 18:02:47 -0500 | [diff] [blame] | 2981 | if (!dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2982 | goto out; |
| 2983 | |
Lorenzo Bianconi | 428604f | 2018-03-29 11:02:24 +0200 | [diff] [blame] | 2984 | if (idev->cnf.disable_ipv6) { |
| 2985 | NL_SET_ERR_MSG(extack, "IPv6 is disabled on nexthop device"); |
| 2986 | err = -EACCES; |
| 2987 | goto out; |
| 2988 | } |
| 2989 | |
David Ahern | 955ec4c | 2018-01-24 19:45:29 -0800 | [diff] [blame] | 2990 | if (!(dev->flags & IFF_UP)) { |
| 2991 | NL_SET_ERR_MSG(extack, "Nexthop device is not up"); |
| 2992 | err = -ENETDOWN; |
| 2993 | goto out; |
| 2994 | } |
| 2995 | |
Daniel Walter | c3968a8 | 2011-04-13 21:10:57 +0000 | [diff] [blame] | 2996 | if (!ipv6_addr_any(&cfg->fc_prefsrc)) { |
| 2997 | if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) { |
David Ahern | d5d531c | 2017-05-21 10:12:05 -0600 | [diff] [blame] | 2998 | NL_SET_ERR_MSG(extack, "Invalid source address"); |
Daniel Walter | c3968a8 | 2011-04-13 21:10:57 +0000 | [diff] [blame] | 2999 | err = -EINVAL; |
| 3000 | goto out; |
| 3001 | } |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 3002 | rt->fib6_prefsrc.addr = cfg->fc_prefsrc; |
| 3003 | rt->fib6_prefsrc.plen = 128; |
Daniel Walter | c3968a8 | 2011-04-13 21:10:57 +0000 | [diff] [blame] | 3004 | } else |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 3005 | rt->fib6_prefsrc.plen = 0; |
Daniel Walter | c3968a8 | 2011-04-13 21:10:57 +0000 | [diff] [blame] | 3006 | |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 3007 | rt->fib6_flags = cfg->fc_flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3008 | |
| 3009 | install_route: |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 3010 | if (!(rt->fib6_flags & (RTF_LOCAL | RTF_ANYCAST)) && |
Ido Schimmel | 5609b80 | 2018-01-07 12:45:06 +0200 | [diff] [blame] | 3011 | !netif_carrier_ok(dev)) |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 3012 | rt->fib6_nh.nh_flags |= RTNH_F_LINKDOWN; |
| 3013 | rt->fib6_nh.nh_flags |= (cfg->fc_flags & RTNH_F_ONLINK); |
David Ahern | 93531c6 | 2018-04-17 17:33:25 -0700 | [diff] [blame] | 3014 | rt->fib6_nh.nh_dev = dev; |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 3015 | rt->fib6_table = table; |
Daniel Lezcano | 63152fc | 2008-03-03 23:31:11 -0800 | [diff] [blame] | 3016 | |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 3017 | cfg->fc_nlinfo.nl_net = dev_net(dev); |
Daniel Lezcano | 63152fc | 2008-03-03 23:31:11 -0800 | [diff] [blame] | 3018 | |
David Ahern | dcd1f57 | 2018-04-18 15:39:05 -0700 | [diff] [blame^] | 3019 | if (idev) |
| 3020 | in6_dev_put(idev); |
| 3021 | |
Roopa Prabhu | 8c5b83f | 2015-10-10 08:26:36 -0700 | [diff] [blame] | 3022 | return rt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3023 | out: |
| 3024 | if (dev) |
| 3025 | dev_put(dev); |
| 3026 | if (idev) |
| 3027 | in6_dev_put(idev); |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 3028 | |
David Ahern | 93531c6 | 2018-04-17 17:33:25 -0700 | [diff] [blame] | 3029 | fib6_info_release(rt); |
Roopa Prabhu | 8c5b83f | 2015-10-10 08:26:36 -0700 | [diff] [blame] | 3030 | return ERR_PTR(err); |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 3031 | } |
| 3032 | |
David Ahern | acb54e3 | 2018-04-17 17:33:22 -0700 | [diff] [blame] | 3033 | int ip6_route_add(struct fib6_config *cfg, gfp_t gfp_flags, |
| 3034 | struct netlink_ext_ack *extack) |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 3035 | { |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 3036 | struct fib6_info *rt; |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 3037 | int err; |
| 3038 | |
David Ahern | acb54e3 | 2018-04-17 17:33:22 -0700 | [diff] [blame] | 3039 | rt = ip6_route_info_create(cfg, gfp_flags, extack); |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 3040 | if (IS_ERR(rt)) |
| 3041 | return PTR_ERR(rt); |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 3042 | |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 3043 | err = __ip6_ins_rt(rt, &cfg->fc_nlinfo, extack); |
David Ahern | 93531c6 | 2018-04-17 17:33:25 -0700 | [diff] [blame] | 3044 | fib6_info_release(rt); |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 3045 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3046 | return err; |
| 3047 | } |
| 3048 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 3049 | 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] | 3050 | { |
David Ahern | afb1d4b5 | 2018-04-17 17:33:11 -0700 | [diff] [blame] | 3051 | struct net *net = info->nl_net; |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 3052 | struct fib6_table *table; |
David Ahern | afb1d4b5 | 2018-04-17 17:33:11 -0700 | [diff] [blame] | 3053 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3054 | |
David Ahern | 421842e | 2018-04-17 17:33:18 -0700 | [diff] [blame] | 3055 | if (rt == net->ipv6.fib6_null_entry) { |
Gao feng | 6825a26 | 2012-09-19 19:25:34 +0000 | [diff] [blame] | 3056 | err = -ENOENT; |
| 3057 | goto out; |
| 3058 | } |
Patrick McHardy | 6c813a7 | 2006-08-06 22:22:47 -0700 | [diff] [blame] | 3059 | |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 3060 | table = rt->fib6_table; |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 3061 | spin_lock_bh(&table->tb6_lock); |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 3062 | err = fib6_del(rt, info); |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 3063 | spin_unlock_bh(&table->tb6_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3064 | |
Gao feng | 6825a26 | 2012-09-19 19:25:34 +0000 | [diff] [blame] | 3065 | out: |
David Ahern | 93531c6 | 2018-04-17 17:33:25 -0700 | [diff] [blame] | 3066 | fib6_info_release(rt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3067 | return err; |
| 3068 | } |
| 3069 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 3070 | int ip6_del_rt(struct net *net, struct fib6_info *rt) |
Thomas Graf | e0a1ad73 | 2006-08-22 00:00:21 -0700 | [diff] [blame] | 3071 | { |
David Ahern | afb1d4b5 | 2018-04-17 17:33:11 -0700 | [diff] [blame] | 3072 | struct nl_info info = { .nl_net = net }; |
| 3073 | |
Denis V. Lunev | 528c4ce | 2007-12-13 09:45:12 -0800 | [diff] [blame] | 3074 | return __ip6_del_rt(rt, &info); |
Thomas Graf | e0a1ad73 | 2006-08-22 00:00:21 -0700 | [diff] [blame] | 3075 | } |
| 3076 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 3077 | 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] | 3078 | { |
| 3079 | struct nl_info *info = &cfg->fc_nlinfo; |
WANG Cong | e333003 | 2017-02-27 16:07:43 -0800 | [diff] [blame] | 3080 | struct net *net = info->nl_net; |
David Ahern | 16a16cd | 2017-02-02 12:37:11 -0800 | [diff] [blame] | 3081 | struct sk_buff *skb = NULL; |
David Ahern | 0ae8133 | 2017-02-02 12:37:08 -0800 | [diff] [blame] | 3082 | struct fib6_table *table; |
WANG Cong | e333003 | 2017-02-27 16:07:43 -0800 | [diff] [blame] | 3083 | int err = -ENOENT; |
David Ahern | 0ae8133 | 2017-02-02 12:37:08 -0800 | [diff] [blame] | 3084 | |
David Ahern | 421842e | 2018-04-17 17:33:18 -0700 | [diff] [blame] | 3085 | if (rt == net->ipv6.fib6_null_entry) |
WANG Cong | e333003 | 2017-02-27 16:07:43 -0800 | [diff] [blame] | 3086 | goto out_put; |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 3087 | table = rt->fib6_table; |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 3088 | spin_lock_bh(&table->tb6_lock); |
David Ahern | 0ae8133 | 2017-02-02 12:37:08 -0800 | [diff] [blame] | 3089 | |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 3090 | if (rt->fib6_nsiblings && cfg->fc_delete_all_nh) { |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 3091 | struct fib6_info *sibling, *next_sibling; |
David Ahern | 0ae8133 | 2017-02-02 12:37:08 -0800 | [diff] [blame] | 3092 | |
David Ahern | 16a16cd | 2017-02-02 12:37:11 -0800 | [diff] [blame] | 3093 | /* prefer to send a single notification with all hops */ |
| 3094 | skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any()); |
| 3095 | if (skb) { |
| 3096 | u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0; |
| 3097 | |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 3098 | if (rt6_fill_node(net, skb, rt, NULL, |
David Ahern | 16a16cd | 2017-02-02 12:37:11 -0800 | [diff] [blame] | 3099 | NULL, NULL, 0, RTM_DELROUTE, |
| 3100 | info->portid, seq, 0) < 0) { |
| 3101 | kfree_skb(skb); |
| 3102 | skb = NULL; |
| 3103 | } else |
| 3104 | info->skip_notify = 1; |
| 3105 | } |
| 3106 | |
David Ahern | 0ae8133 | 2017-02-02 12:37:08 -0800 | [diff] [blame] | 3107 | list_for_each_entry_safe(sibling, next_sibling, |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 3108 | &rt->fib6_siblings, |
| 3109 | fib6_siblings) { |
David Ahern | 0ae8133 | 2017-02-02 12:37:08 -0800 | [diff] [blame] | 3110 | err = fib6_del(sibling, info); |
| 3111 | if (err) |
WANG Cong | e333003 | 2017-02-27 16:07:43 -0800 | [diff] [blame] | 3112 | goto out_unlock; |
David Ahern | 0ae8133 | 2017-02-02 12:37:08 -0800 | [diff] [blame] | 3113 | } |
| 3114 | } |
| 3115 | |
| 3116 | err = fib6_del(rt, info); |
WANG Cong | e333003 | 2017-02-27 16:07:43 -0800 | [diff] [blame] | 3117 | out_unlock: |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 3118 | spin_unlock_bh(&table->tb6_lock); |
WANG Cong | e333003 | 2017-02-27 16:07:43 -0800 | [diff] [blame] | 3119 | out_put: |
David Ahern | 93531c6 | 2018-04-17 17:33:25 -0700 | [diff] [blame] | 3120 | fib6_info_release(rt); |
David Ahern | 16a16cd | 2017-02-02 12:37:11 -0800 | [diff] [blame] | 3121 | |
| 3122 | if (skb) { |
WANG Cong | e333003 | 2017-02-27 16:07:43 -0800 | [diff] [blame] | 3123 | rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE, |
David Ahern | 16a16cd | 2017-02-02 12:37:11 -0800 | [diff] [blame] | 3124 | info->nlh, gfp_any()); |
| 3125 | } |
David Ahern | 0ae8133 | 2017-02-02 12:37:08 -0800 | [diff] [blame] | 3126 | return err; |
| 3127 | } |
| 3128 | |
David Ahern | 23fb93a | 2018-04-17 17:33:23 -0700 | [diff] [blame] | 3129 | static int ip6_del_cached_rt(struct rt6_info *rt, struct fib6_config *cfg) |
| 3130 | { |
| 3131 | int rc = -ESRCH; |
| 3132 | |
| 3133 | if (cfg->fc_ifindex && rt->dst.dev->ifindex != cfg->fc_ifindex) |
| 3134 | goto out; |
| 3135 | |
| 3136 | if (cfg->fc_flags & RTF_GATEWAY && |
| 3137 | !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway)) |
| 3138 | goto out; |
| 3139 | if (dst_hold_safe(&rt->dst)) |
| 3140 | rc = rt6_remove_exception_rt(rt); |
| 3141 | out: |
| 3142 | return rc; |
| 3143 | } |
| 3144 | |
David Ahern | 333c430 | 2017-05-21 10:12:04 -0600 | [diff] [blame] | 3145 | static int ip6_route_del(struct fib6_config *cfg, |
| 3146 | struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3147 | { |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 3148 | struct rt6_info *rt_cache; |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 3149 | struct fib6_table *table; |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 3150 | struct fib6_info *rt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3151 | struct fib6_node *fn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3152 | int err = -ESRCH; |
| 3153 | |
Daniel Lezcano | 5578689 | 2008-03-04 13:47:47 -0800 | [diff] [blame] | 3154 | table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table); |
David Ahern | d5d531c | 2017-05-21 10:12:05 -0600 | [diff] [blame] | 3155 | if (!table) { |
| 3156 | NL_SET_ERR_MSG(extack, "FIB table does not exist"); |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 3157 | return err; |
David Ahern | d5d531c | 2017-05-21 10:12:05 -0600 | [diff] [blame] | 3158 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3159 | |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 3160 | rcu_read_lock(); |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 3161 | |
| 3162 | fn = fib6_locate(&table->tb6_root, |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 3163 | &cfg->fc_dst, cfg->fc_dst_len, |
Wei Wang | 38fbeee | 2017-10-06 12:06:02 -0700 | [diff] [blame] | 3164 | &cfg->fc_src, cfg->fc_src_len, |
Wei Wang | 2b760fc | 2017-10-06 12:06:03 -0700 | [diff] [blame] | 3165 | !(cfg->fc_flags & RTF_CACHE)); |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 3166 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3167 | if (fn) { |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 3168 | for_each_fib6_node_rt_rcu(fn) { |
Wei Wang | 2b760fc | 2017-10-06 12:06:03 -0700 | [diff] [blame] | 3169 | if (cfg->fc_flags & RTF_CACHE) { |
David Ahern | 23fb93a | 2018-04-17 17:33:23 -0700 | [diff] [blame] | 3170 | int rc; |
| 3171 | |
Wei Wang | 2b760fc | 2017-10-06 12:06:03 -0700 | [diff] [blame] | 3172 | rt_cache = rt6_find_cached_rt(rt, &cfg->fc_dst, |
| 3173 | &cfg->fc_src); |
David Ahern | 23fb93a | 2018-04-17 17:33:23 -0700 | [diff] [blame] | 3174 | if (rt_cache) { |
| 3175 | rc = ip6_del_cached_rt(rt_cache, cfg); |
| 3176 | if (rc != -ESRCH) |
| 3177 | return rc; |
| 3178 | } |
| 3179 | continue; |
Wei Wang | 2b760fc | 2017-10-06 12:06:03 -0700 | [diff] [blame] | 3180 | } |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 3181 | if (cfg->fc_ifindex && |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 3182 | (!rt->fib6_nh.nh_dev || |
| 3183 | rt->fib6_nh.nh_dev->ifindex != cfg->fc_ifindex)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3184 | continue; |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 3185 | if (cfg->fc_flags & RTF_GATEWAY && |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 3186 | !ipv6_addr_equal(&cfg->fc_gateway, &rt->fib6_nh.nh_gw)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3187 | continue; |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 3188 | if (cfg->fc_metric && cfg->fc_metric != rt->fib6_metric) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3189 | continue; |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 3190 | if (cfg->fc_protocol && cfg->fc_protocol != rt->fib6_protocol) |
Mantas M | c2ed188 | 2016-12-16 10:30:59 +0200 | [diff] [blame] | 3191 | continue; |
David Ahern | 93531c6 | 2018-04-17 17:33:25 -0700 | [diff] [blame] | 3192 | fib6_info_hold(rt); |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 3193 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3194 | |
David Ahern | 0ae8133 | 2017-02-02 12:37:08 -0800 | [diff] [blame] | 3195 | /* if gateway was specified only delete the one hop */ |
| 3196 | if (cfg->fc_flags & RTF_GATEWAY) |
| 3197 | return __ip6_del_rt(rt, &cfg->fc_nlinfo); |
| 3198 | |
| 3199 | return __ip6_del_rt_siblings(rt, cfg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3200 | } |
| 3201 | } |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 3202 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3203 | |
| 3204 | return err; |
| 3205 | } |
| 3206 | |
David S. Miller | 6700c27 | 2012-07-17 03:29:28 -0700 | [diff] [blame] | 3207 | 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] | 3208 | { |
YOSHIFUJI Hideaki | a627945 | 2006-08-23 17:18:26 -0700 | [diff] [blame] | 3209 | struct netevent_redirect netevent; |
David S. Miller | e8599ff | 2012-07-11 23:43:53 -0700 | [diff] [blame] | 3210 | struct rt6_info *rt, *nrt = NULL; |
David S. Miller | e8599ff | 2012-07-11 23:43:53 -0700 | [diff] [blame] | 3211 | struct ndisc_options ndopts; |
| 3212 | struct inet6_dev *in6_dev; |
| 3213 | struct neighbour *neigh; |
YOSHIFUJI Hideaki / 吉藤英明 | 71bcdba | 2013-01-05 16:34:51 +0000 | [diff] [blame] | 3214 | struct rd_msg *msg; |
David S. Miller | 6e157b6 | 2012-07-12 00:05:02 -0700 | [diff] [blame] | 3215 | int optlen, on_link; |
| 3216 | u8 *lladdr; |
David S. Miller | e8599ff | 2012-07-11 23:43:53 -0700 | [diff] [blame] | 3217 | |
Simon Horman | 29a3cad | 2013-05-28 20:34:26 +0000 | [diff] [blame] | 3218 | optlen = skb_tail_pointer(skb) - skb_transport_header(skb); |
YOSHIFUJI Hideaki / 吉藤英明 | 71bcdba | 2013-01-05 16:34:51 +0000 | [diff] [blame] | 3219 | optlen -= sizeof(*msg); |
David S. Miller | e8599ff | 2012-07-11 23:43:53 -0700 | [diff] [blame] | 3220 | |
| 3221 | if (optlen < 0) { |
David S. Miller | 6e157b6 | 2012-07-12 00:05:02 -0700 | [diff] [blame] | 3222 | net_dbg_ratelimited("rt6_do_redirect: packet too short\n"); |
David S. Miller | e8599ff | 2012-07-11 23:43:53 -0700 | [diff] [blame] | 3223 | return; |
| 3224 | } |
| 3225 | |
YOSHIFUJI Hideaki / 吉藤英明 | 71bcdba | 2013-01-05 16:34:51 +0000 | [diff] [blame] | 3226 | msg = (struct rd_msg *)icmp6_hdr(skb); |
David S. Miller | e8599ff | 2012-07-11 23:43:53 -0700 | [diff] [blame] | 3227 | |
YOSHIFUJI Hideaki / 吉藤英明 | 71bcdba | 2013-01-05 16:34:51 +0000 | [diff] [blame] | 3228 | if (ipv6_addr_is_multicast(&msg->dest)) { |
David S. Miller | 6e157b6 | 2012-07-12 00:05:02 -0700 | [diff] [blame] | 3229 | net_dbg_ratelimited("rt6_do_redirect: destination address is multicast\n"); |
David S. Miller | e8599ff | 2012-07-11 23:43:53 -0700 | [diff] [blame] | 3230 | return; |
| 3231 | } |
| 3232 | |
David S. Miller | 6e157b6 | 2012-07-12 00:05:02 -0700 | [diff] [blame] | 3233 | on_link = 0; |
YOSHIFUJI Hideaki / 吉藤英明 | 71bcdba | 2013-01-05 16:34:51 +0000 | [diff] [blame] | 3234 | if (ipv6_addr_equal(&msg->dest, &msg->target)) { |
David S. Miller | e8599ff | 2012-07-11 23:43:53 -0700 | [diff] [blame] | 3235 | on_link = 1; |
YOSHIFUJI Hideaki / 吉藤英明 | 71bcdba | 2013-01-05 16:34:51 +0000 | [diff] [blame] | 3236 | } else if (ipv6_addr_type(&msg->target) != |
David S. Miller | e8599ff | 2012-07-11 23:43:53 -0700 | [diff] [blame] | 3237 | (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) { |
David S. Miller | 6e157b6 | 2012-07-12 00:05:02 -0700 | [diff] [blame] | 3238 | 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] | 3239 | return; |
| 3240 | } |
| 3241 | |
| 3242 | in6_dev = __in6_dev_get(skb->dev); |
| 3243 | if (!in6_dev) |
| 3244 | return; |
| 3245 | if (in6_dev->cnf.forwarding || !in6_dev->cnf.accept_redirects) |
| 3246 | return; |
| 3247 | |
| 3248 | /* RFC2461 8.1: |
| 3249 | * The IP source address of the Redirect MUST be the same as the current |
| 3250 | * first-hop router for the specified ICMP Destination Address. |
| 3251 | */ |
| 3252 | |
Alexander Aring | f997c55 | 2016-06-15 21:20:23 +0200 | [diff] [blame] | 3253 | if (!ndisc_parse_options(skb->dev, msg->opt, optlen, &ndopts)) { |
David S. Miller | e8599ff | 2012-07-11 23:43:53 -0700 | [diff] [blame] | 3254 | net_dbg_ratelimited("rt6_redirect: invalid ND options\n"); |
| 3255 | return; |
| 3256 | } |
David S. Miller | 6e157b6 | 2012-07-12 00:05:02 -0700 | [diff] [blame] | 3257 | |
| 3258 | lladdr = NULL; |
David S. Miller | e8599ff | 2012-07-11 23:43:53 -0700 | [diff] [blame] | 3259 | if (ndopts.nd_opts_tgt_lladdr) { |
| 3260 | lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr, |
| 3261 | skb->dev); |
| 3262 | if (!lladdr) { |
| 3263 | net_dbg_ratelimited("rt6_redirect: invalid link-layer address length\n"); |
| 3264 | return; |
| 3265 | } |
| 3266 | } |
| 3267 | |
David S. Miller | 6e157b6 | 2012-07-12 00:05:02 -0700 | [diff] [blame] | 3268 | rt = (struct rt6_info *) dst; |
Matthias Schiffer | ec13ad1 | 2015-11-02 01:24:38 +0100 | [diff] [blame] | 3269 | if (rt->rt6i_flags & RTF_REJECT) { |
David S. Miller | 6e157b6 | 2012-07-12 00:05:02 -0700 | [diff] [blame] | 3270 | net_dbg_ratelimited("rt6_redirect: source isn't a valid nexthop for redirect target\n"); |
| 3271 | return; |
| 3272 | } |
| 3273 | |
| 3274 | /* Redirect received -> path was valid. |
| 3275 | * Look, redirects are sent only in response to data packets, |
| 3276 | * so that this nexthop apparently is reachable. --ANK |
| 3277 | */ |
Julian Anastasov | 0dec879 | 2017-02-06 23:14:16 +0200 | [diff] [blame] | 3278 | dst_confirm_neigh(&rt->dst, &ipv6_hdr(skb)->saddr); |
David S. Miller | 6e157b6 | 2012-07-12 00:05:02 -0700 | [diff] [blame] | 3279 | |
YOSHIFUJI Hideaki / 吉藤英明 | 71bcdba | 2013-01-05 16:34:51 +0000 | [diff] [blame] | 3280 | neigh = __neigh_lookup(&nd_tbl, &msg->target, skb->dev, 1); |
David S. Miller | e8599ff | 2012-07-11 23:43:53 -0700 | [diff] [blame] | 3281 | if (!neigh) |
| 3282 | return; |
| 3283 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3284 | /* |
| 3285 | * We have finally decided to accept it. |
| 3286 | */ |
| 3287 | |
Alexander Aring | f997c55 | 2016-06-15 21:20:23 +0200 | [diff] [blame] | 3288 | ndisc_update(skb->dev, neigh, lladdr, NUD_STALE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3289 | NEIGH_UPDATE_F_WEAK_OVERRIDE| |
| 3290 | NEIGH_UPDATE_F_OVERRIDE| |
| 3291 | (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER| |
Alexander Aring | f997c55 | 2016-06-15 21:20:23 +0200 | [diff] [blame] | 3292 | NEIGH_UPDATE_F_ISROUTER)), |
| 3293 | NDISC_REDIRECT, &ndopts); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3294 | |
David Ahern | 23fb93a | 2018-04-17 17:33:23 -0700 | [diff] [blame] | 3295 | nrt = ip6_rt_cache_alloc(rt->from, &msg->dest, NULL); |
David S. Miller | 3830847 | 2011-12-03 18:02:47 -0500 | [diff] [blame] | 3296 | if (!nrt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3297 | goto out; |
| 3298 | |
| 3299 | nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE; |
| 3300 | if (on_link) |
| 3301 | nrt->rt6i_flags &= ~RTF_GATEWAY; |
| 3302 | |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 3303 | nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3304 | |
Wei Wang | 2b760fc | 2017-10-06 12:06:03 -0700 | [diff] [blame] | 3305 | /* No need to remove rt from the exception table if rt is |
| 3306 | * a cached route because rt6_insert_exception() will |
| 3307 | * takes care of it |
| 3308 | */ |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 3309 | if (rt6_insert_exception(nrt, rt->from)) { |
Wei Wang | 2b760fc | 2017-10-06 12:06:03 -0700 | [diff] [blame] | 3310 | dst_release_immediate(&nrt->dst); |
| 3311 | goto out; |
| 3312 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3313 | |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 3314 | netevent.old = &rt->dst; |
| 3315 | netevent.new = &nrt->dst; |
YOSHIFUJI Hideaki / 吉藤英明 | 71bcdba | 2013-01-05 16:34:51 +0000 | [diff] [blame] | 3316 | netevent.daddr = &msg->dest; |
YOSHIFUJI Hideaki / 吉藤英明 | 6059283 | 2013-01-14 09:28:27 +0000 | [diff] [blame] | 3317 | netevent.neigh = neigh; |
Tom Tucker | 8d71740 | 2006-07-30 20:43:36 -0700 | [diff] [blame] | 3318 | call_netevent_notifiers(NETEVENT_REDIRECT, &netevent); |
| 3319 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3320 | out: |
David S. Miller | e8599ff | 2012-07-11 23:43:53 -0700 | [diff] [blame] | 3321 | neigh_release(neigh); |
David S. Miller | 6e157b6 | 2012-07-12 00:05:02 -0700 | [diff] [blame] | 3322 | } |
| 3323 | |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 3324 | #ifdef CONFIG_IPV6_ROUTE_INFO |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 3325 | static struct fib6_info *rt6_get_route_info(struct net *net, |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 3326 | const struct in6_addr *prefix, int prefixlen, |
David Ahern | 830218c | 2016-10-24 10:52:35 -0700 | [diff] [blame] | 3327 | const struct in6_addr *gwaddr, |
| 3328 | struct net_device *dev) |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 3329 | { |
David Ahern | 830218c | 2016-10-24 10:52:35 -0700 | [diff] [blame] | 3330 | u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO; |
| 3331 | int ifindex = dev->ifindex; |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 3332 | struct fib6_node *fn; |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 3333 | struct fib6_info *rt = NULL; |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 3334 | struct fib6_table *table; |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 3335 | |
David Ahern | 830218c | 2016-10-24 10:52:35 -0700 | [diff] [blame] | 3336 | table = fib6_get_table(net, tb_id); |
David S. Miller | 3830847 | 2011-12-03 18:02:47 -0500 | [diff] [blame] | 3337 | if (!table) |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 3338 | return NULL; |
| 3339 | |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 3340 | rcu_read_lock(); |
Wei Wang | 38fbeee | 2017-10-06 12:06:02 -0700 | [diff] [blame] | 3341 | fn = fib6_locate(&table->tb6_root, prefix, prefixlen, NULL, 0, true); |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 3342 | if (!fn) |
| 3343 | goto out; |
| 3344 | |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 3345 | for_each_fib6_node_rt_rcu(fn) { |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 3346 | if (rt->fib6_nh.nh_dev->ifindex != ifindex) |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 3347 | continue; |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 3348 | if ((rt->fib6_flags & (RTF_ROUTEINFO|RTF_GATEWAY)) != (RTF_ROUTEINFO|RTF_GATEWAY)) |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 3349 | continue; |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 3350 | if (!ipv6_addr_equal(&rt->fib6_nh.nh_gw, gwaddr)) |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 3351 | continue; |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 3352 | fib6_info_hold(rt); |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 3353 | break; |
| 3354 | } |
| 3355 | out: |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 3356 | rcu_read_unlock(); |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 3357 | return rt; |
| 3358 | } |
| 3359 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 3360 | static struct fib6_info *rt6_add_route_info(struct net *net, |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 3361 | const struct in6_addr *prefix, int prefixlen, |
David Ahern | 830218c | 2016-10-24 10:52:35 -0700 | [diff] [blame] | 3362 | const struct in6_addr *gwaddr, |
| 3363 | struct net_device *dev, |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 3364 | unsigned int pref) |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 3365 | { |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 3366 | struct fib6_config cfg = { |
Rami Rosen | 238fc7e | 2008-02-09 23:43:11 -0800 | [diff] [blame] | 3367 | .fc_metric = IP6_RT_PRIO_USER, |
David Ahern | 830218c | 2016-10-24 10:52:35 -0700 | [diff] [blame] | 3368 | .fc_ifindex = dev->ifindex, |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 3369 | .fc_dst_len = prefixlen, |
| 3370 | .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO | |
| 3371 | RTF_UP | RTF_PREF(pref), |
Xin Long | b91d532 | 2017-08-03 14:13:46 +0800 | [diff] [blame] | 3372 | .fc_protocol = RTPROT_RA, |
David Ahern | e8478e8 | 2018-04-17 17:33:13 -0700 | [diff] [blame] | 3373 | .fc_type = RTN_UNICAST, |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 3374 | .fc_nlinfo.portid = 0, |
Daniel Lezcano | efa2cea | 2008-03-04 13:46:48 -0800 | [diff] [blame] | 3375 | .fc_nlinfo.nlh = NULL, |
| 3376 | .fc_nlinfo.nl_net = net, |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 3377 | }; |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 3378 | |
David Ahern | 830218c | 2016-10-24 10:52:35 -0700 | [diff] [blame] | 3379 | cfg.fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO, |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 3380 | cfg.fc_dst = *prefix; |
| 3381 | cfg.fc_gateway = *gwaddr; |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 3382 | |
YOSHIFUJI Hideaki | e317da9 | 2006-03-20 17:06:42 -0800 | [diff] [blame] | 3383 | /* We should treat it as a default route if prefix length is 0. */ |
| 3384 | if (!prefixlen) |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 3385 | cfg.fc_flags |= RTF_DEFAULT; |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 3386 | |
David Ahern | acb54e3 | 2018-04-17 17:33:22 -0700 | [diff] [blame] | 3387 | ip6_route_add(&cfg, GFP_ATOMIC, NULL); |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 3388 | |
David Ahern | 830218c | 2016-10-24 10:52:35 -0700 | [diff] [blame] | 3389 | return rt6_get_route_info(net, prefix, prefixlen, gwaddr, dev); |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 3390 | } |
| 3391 | #endif |
| 3392 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 3393 | struct fib6_info *rt6_get_dflt_router(struct net *net, |
David Ahern | afb1d4b5 | 2018-04-17 17:33:11 -0700 | [diff] [blame] | 3394 | const struct in6_addr *addr, |
| 3395 | struct net_device *dev) |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 3396 | { |
David Ahern | 830218c | 2016-10-24 10:52:35 -0700 | [diff] [blame] | 3397 | u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT; |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 3398 | struct fib6_info *rt; |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 3399 | struct fib6_table *table; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3400 | |
David Ahern | afb1d4b5 | 2018-04-17 17:33:11 -0700 | [diff] [blame] | 3401 | table = fib6_get_table(net, tb_id); |
David S. Miller | 3830847 | 2011-12-03 18:02:47 -0500 | [diff] [blame] | 3402 | if (!table) |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 3403 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3404 | |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 3405 | rcu_read_lock(); |
| 3406 | for_each_fib6_node_rt_rcu(&table->tb6_root) { |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 3407 | if (dev == rt->fib6_nh.nh_dev && |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 3408 | ((rt->fib6_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) && |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 3409 | ipv6_addr_equal(&rt->fib6_nh.nh_gw, addr)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3410 | break; |
| 3411 | } |
| 3412 | if (rt) |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 3413 | fib6_info_hold(rt); |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 3414 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3415 | return rt; |
| 3416 | } |
| 3417 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 3418 | struct fib6_info *rt6_add_dflt_router(struct net *net, |
David Ahern | afb1d4b5 | 2018-04-17 17:33:11 -0700 | [diff] [blame] | 3419 | const struct in6_addr *gwaddr, |
YOSHIFUJI Hideaki | ebacaaa | 2006-03-20 17:04:53 -0800 | [diff] [blame] | 3420 | struct net_device *dev, |
| 3421 | unsigned int pref) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3422 | { |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 3423 | struct fib6_config cfg = { |
David Ahern | ca25449 | 2015-10-12 11:47:10 -0700 | [diff] [blame] | 3424 | .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT, |
Rami Rosen | 238fc7e | 2008-02-09 23:43:11 -0800 | [diff] [blame] | 3425 | .fc_metric = IP6_RT_PRIO_USER, |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 3426 | .fc_ifindex = dev->ifindex, |
| 3427 | .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT | |
| 3428 | RTF_UP | RTF_EXPIRES | RTF_PREF(pref), |
Xin Long | b91d532 | 2017-08-03 14:13:46 +0800 | [diff] [blame] | 3429 | .fc_protocol = RTPROT_RA, |
David Ahern | e8478e8 | 2018-04-17 17:33:13 -0700 | [diff] [blame] | 3430 | .fc_type = RTN_UNICAST, |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 3431 | .fc_nlinfo.portid = 0, |
Daniel Lezcano | 5578689 | 2008-03-04 13:47:47 -0800 | [diff] [blame] | 3432 | .fc_nlinfo.nlh = NULL, |
David Ahern | afb1d4b5 | 2018-04-17 17:33:11 -0700 | [diff] [blame] | 3433 | .fc_nlinfo.nl_net = net, |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 3434 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3435 | |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 3436 | cfg.fc_gateway = *gwaddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3437 | |
David Ahern | acb54e3 | 2018-04-17 17:33:22 -0700 | [diff] [blame] | 3438 | if (!ip6_route_add(&cfg, GFP_ATOMIC, NULL)) { |
David Ahern | 830218c | 2016-10-24 10:52:35 -0700 | [diff] [blame] | 3439 | struct fib6_table *table; |
| 3440 | |
| 3441 | table = fib6_get_table(dev_net(dev), cfg.fc_table); |
| 3442 | if (table) |
| 3443 | table->flags |= RT6_TABLE_HAS_DFLT_ROUTER; |
| 3444 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3445 | |
David Ahern | afb1d4b5 | 2018-04-17 17:33:11 -0700 | [diff] [blame] | 3446 | return rt6_get_dflt_router(net, gwaddr, dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3447 | } |
| 3448 | |
David Ahern | afb1d4b5 | 2018-04-17 17:33:11 -0700 | [diff] [blame] | 3449 | static void __rt6_purge_dflt_routers(struct net *net, |
| 3450 | struct fib6_table *table) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3451 | { |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 3452 | struct fib6_info *rt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3453 | |
| 3454 | restart: |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 3455 | rcu_read_lock(); |
| 3456 | for_each_fib6_node_rt_rcu(&table->tb6_root) { |
David Ahern | dcd1f57 | 2018-04-18 15:39:05 -0700 | [diff] [blame^] | 3457 | struct net_device *dev = fib6_info_nh_dev(rt); |
| 3458 | struct inet6_dev *idev = dev ? __in6_dev_get(dev) : NULL; |
| 3459 | |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 3460 | if (rt->fib6_flags & (RTF_DEFAULT | RTF_ADDRCONF) && |
David Ahern | dcd1f57 | 2018-04-18 15:39:05 -0700 | [diff] [blame^] | 3461 | (!idev || idev->cnf.accept_ra != 2)) { |
David Ahern | 93531c6 | 2018-04-17 17:33:25 -0700 | [diff] [blame] | 3462 | fib6_info_hold(rt); |
| 3463 | rcu_read_unlock(); |
| 3464 | ip6_del_rt(net, rt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3465 | goto restart; |
| 3466 | } |
| 3467 | } |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 3468 | rcu_read_unlock(); |
David Ahern | 830218c | 2016-10-24 10:52:35 -0700 | [diff] [blame] | 3469 | |
| 3470 | table->flags &= ~RT6_TABLE_HAS_DFLT_ROUTER; |
| 3471 | } |
| 3472 | |
| 3473 | void rt6_purge_dflt_routers(struct net *net) |
| 3474 | { |
| 3475 | struct fib6_table *table; |
| 3476 | struct hlist_head *head; |
| 3477 | unsigned int h; |
| 3478 | |
| 3479 | rcu_read_lock(); |
| 3480 | |
| 3481 | for (h = 0; h < FIB6_TABLE_HASHSZ; h++) { |
| 3482 | head = &net->ipv6.fib_table_hash[h]; |
| 3483 | hlist_for_each_entry_rcu(table, head, tb6_hlist) { |
| 3484 | if (table->flags & RT6_TABLE_HAS_DFLT_ROUTER) |
David Ahern | afb1d4b5 | 2018-04-17 17:33:11 -0700 | [diff] [blame] | 3485 | __rt6_purge_dflt_routers(net, table); |
David Ahern | 830218c | 2016-10-24 10:52:35 -0700 | [diff] [blame] | 3486 | } |
| 3487 | } |
| 3488 | |
| 3489 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3490 | } |
| 3491 | |
Daniel Lezcano | 5578689 | 2008-03-04 13:47:47 -0800 | [diff] [blame] | 3492 | static void rtmsg_to_fib6_config(struct net *net, |
| 3493 | struct in6_rtmsg *rtmsg, |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 3494 | struct fib6_config *cfg) |
| 3495 | { |
| 3496 | memset(cfg, 0, sizeof(*cfg)); |
| 3497 | |
David Ahern | ca25449 | 2015-10-12 11:47:10 -0700 | [diff] [blame] | 3498 | cfg->fc_table = l3mdev_fib_table_by_index(net, rtmsg->rtmsg_ifindex) ? |
| 3499 | : RT6_TABLE_MAIN; |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 3500 | cfg->fc_ifindex = rtmsg->rtmsg_ifindex; |
| 3501 | cfg->fc_metric = rtmsg->rtmsg_metric; |
| 3502 | cfg->fc_expires = rtmsg->rtmsg_info; |
| 3503 | cfg->fc_dst_len = rtmsg->rtmsg_dst_len; |
| 3504 | cfg->fc_src_len = rtmsg->rtmsg_src_len; |
| 3505 | cfg->fc_flags = rtmsg->rtmsg_flags; |
David Ahern | e8478e8 | 2018-04-17 17:33:13 -0700 | [diff] [blame] | 3506 | cfg->fc_type = rtmsg->rtmsg_type; |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 3507 | |
Daniel Lezcano | 5578689 | 2008-03-04 13:47:47 -0800 | [diff] [blame] | 3508 | cfg->fc_nlinfo.nl_net = net; |
Benjamin Thery | f1243c2 | 2008-02-26 18:10:03 -0800 | [diff] [blame] | 3509 | |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 3510 | cfg->fc_dst = rtmsg->rtmsg_dst; |
| 3511 | cfg->fc_src = rtmsg->rtmsg_src; |
| 3512 | cfg->fc_gateway = rtmsg->rtmsg_gateway; |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 3513 | } |
| 3514 | |
Daniel Lezcano | 5578689 | 2008-03-04 13:47:47 -0800 | [diff] [blame] | 3515 | 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] | 3516 | { |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 3517 | struct fib6_config cfg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3518 | struct in6_rtmsg rtmsg; |
| 3519 | int err; |
| 3520 | |
Ian Morris | 67ba415 | 2014-08-24 21:53:10 +0100 | [diff] [blame] | 3521 | switch (cmd) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3522 | case SIOCADDRT: /* Add a route */ |
| 3523 | case SIOCDELRT: /* Delete a route */ |
Eric W. Biederman | af31f41 | 2012-11-16 03:03:06 +0000 | [diff] [blame] | 3524 | if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3525 | return -EPERM; |
| 3526 | err = copy_from_user(&rtmsg, arg, |
| 3527 | sizeof(struct in6_rtmsg)); |
| 3528 | if (err) |
| 3529 | return -EFAULT; |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 3530 | |
Daniel Lezcano | 5578689 | 2008-03-04 13:47:47 -0800 | [diff] [blame] | 3531 | rtmsg_to_fib6_config(net, &rtmsg, &cfg); |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 3532 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3533 | rtnl_lock(); |
| 3534 | switch (cmd) { |
| 3535 | case SIOCADDRT: |
David Ahern | acb54e3 | 2018-04-17 17:33:22 -0700 | [diff] [blame] | 3536 | err = ip6_route_add(&cfg, GFP_KERNEL, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3537 | break; |
| 3538 | case SIOCDELRT: |
David Ahern | 333c430 | 2017-05-21 10:12:04 -0600 | [diff] [blame] | 3539 | err = ip6_route_del(&cfg, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3540 | break; |
| 3541 | default: |
| 3542 | err = -EINVAL; |
| 3543 | } |
| 3544 | rtnl_unlock(); |
| 3545 | |
| 3546 | return err; |
Stephen Hemminger | 3ff50b7 | 2007-04-20 17:09:22 -0700 | [diff] [blame] | 3547 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3548 | |
| 3549 | return -EINVAL; |
| 3550 | } |
| 3551 | |
| 3552 | /* |
| 3553 | * Drop the packet on the floor |
| 3554 | */ |
| 3555 | |
Brian Haley | d5fdd6b | 2009-06-23 04:31:07 -0700 | [diff] [blame] | 3556 | 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] | 3557 | { |
YOSHIFUJI Hideaki | 612f09e | 2007-04-13 16:18:02 -0700 | [diff] [blame] | 3558 | int type; |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 3559 | struct dst_entry *dst = skb_dst(skb); |
YOSHIFUJI Hideaki | 612f09e | 2007-04-13 16:18:02 -0700 | [diff] [blame] | 3560 | switch (ipstats_mib_noroutes) { |
| 3561 | case IPSTATS_MIB_INNOROUTES: |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 3562 | type = ipv6_addr_type(&ipv6_hdr(skb)->daddr); |
Ulrich Weber | 45bb006 | 2010-02-25 23:28:58 +0000 | [diff] [blame] | 3563 | if (type == IPV6_ADDR_ANY) { |
Stephen Suryaputra | bdb7cc6 | 2018-04-16 13:42:16 -0400 | [diff] [blame] | 3564 | IP6_INC_STATS(dev_net(dst->dev), |
| 3565 | __in6_dev_get_safely(skb->dev), |
Denis V. Lunev | 3bd653c | 2008-10-08 10:54:51 -0700 | [diff] [blame] | 3566 | IPSTATS_MIB_INADDRERRORS); |
YOSHIFUJI Hideaki | 612f09e | 2007-04-13 16:18:02 -0700 | [diff] [blame] | 3567 | break; |
| 3568 | } |
| 3569 | /* FALLTHROUGH */ |
| 3570 | case IPSTATS_MIB_OUTNOROUTES: |
Denis V. Lunev | 3bd653c | 2008-10-08 10:54:51 -0700 | [diff] [blame] | 3571 | IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst), |
| 3572 | ipstats_mib_noroutes); |
YOSHIFUJI Hideaki | 612f09e | 2007-04-13 16:18:02 -0700 | [diff] [blame] | 3573 | break; |
| 3574 | } |
Alexey Dobriyan | 3ffe533 | 2010-02-18 08:25:24 +0000 | [diff] [blame] | 3575 | icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3576 | kfree_skb(skb); |
| 3577 | return 0; |
| 3578 | } |
| 3579 | |
Thomas Graf | 9ce8ade | 2006-10-18 20:46:54 -0700 | [diff] [blame] | 3580 | static int ip6_pkt_discard(struct sk_buff *skb) |
| 3581 | { |
YOSHIFUJI Hideaki | 612f09e | 2007-04-13 16:18:02 -0700 | [diff] [blame] | 3582 | return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES); |
Thomas Graf | 9ce8ade | 2006-10-18 20:46:54 -0700 | [diff] [blame] | 3583 | } |
| 3584 | |
Eric W. Biederman | ede2059 | 2015-10-07 16:48:47 -0500 | [diff] [blame] | 3585 | 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] | 3586 | { |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 3587 | skb->dev = skb_dst(skb)->dev; |
YOSHIFUJI Hideaki | 612f09e | 2007-04-13 16:18:02 -0700 | [diff] [blame] | 3588 | return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3589 | } |
| 3590 | |
Thomas Graf | 9ce8ade | 2006-10-18 20:46:54 -0700 | [diff] [blame] | 3591 | static int ip6_pkt_prohibit(struct sk_buff *skb) |
| 3592 | { |
YOSHIFUJI Hideaki | 612f09e | 2007-04-13 16:18:02 -0700 | [diff] [blame] | 3593 | return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES); |
Thomas Graf | 9ce8ade | 2006-10-18 20:46:54 -0700 | [diff] [blame] | 3594 | } |
| 3595 | |
Eric W. Biederman | ede2059 | 2015-10-07 16:48:47 -0500 | [diff] [blame] | 3596 | 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] | 3597 | { |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 3598 | skb->dev = skb_dst(skb)->dev; |
YOSHIFUJI Hideaki | 612f09e | 2007-04-13 16:18:02 -0700 | [diff] [blame] | 3599 | return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES); |
Thomas Graf | 9ce8ade | 2006-10-18 20:46:54 -0700 | [diff] [blame] | 3600 | } |
| 3601 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3602 | /* |
| 3603 | * Allocate a dst for local (unicast / anycast) address. |
| 3604 | */ |
| 3605 | |
David Ahern | 360a988 | 2018-04-18 15:39:00 -0700 | [diff] [blame] | 3606 | struct fib6_info *addrconf_f6i_alloc(struct net *net, |
| 3607 | struct inet6_dev *idev, |
| 3608 | const struct in6_addr *addr, |
| 3609 | bool anycast, gfp_t gfp_flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3610 | { |
David Ahern | ca25449 | 2015-10-12 11:47:10 -0700 | [diff] [blame] | 3611 | u32 tb_id; |
David Ahern | 4832c30 | 2017-08-17 12:17:20 -0700 | [diff] [blame] | 3612 | struct net_device *dev = idev->dev; |
David Ahern | 360a988 | 2018-04-18 15:39:00 -0700 | [diff] [blame] | 3613 | struct fib6_info *f6i; |
David Ahern | 5f02ce24 | 2016-09-10 12:09:54 -0700 | [diff] [blame] | 3614 | |
David Ahern | 360a988 | 2018-04-18 15:39:00 -0700 | [diff] [blame] | 3615 | f6i = fib6_info_alloc(gfp_flags); |
| 3616 | if (!f6i) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3617 | return ERR_PTR(-ENOMEM); |
| 3618 | |
David Ahern | 360a988 | 2018-04-18 15:39:00 -0700 | [diff] [blame] | 3619 | f6i->dst_nocount = true; |
David Ahern | 360a988 | 2018-04-18 15:39:00 -0700 | [diff] [blame] | 3620 | f6i->dst_host = true; |
| 3621 | f6i->fib6_protocol = RTPROT_KERNEL; |
| 3622 | f6i->fib6_flags = RTF_UP | RTF_NONEXTHOP; |
David Ahern | e8478e8 | 2018-04-17 17:33:13 -0700 | [diff] [blame] | 3623 | if (anycast) { |
David Ahern | 360a988 | 2018-04-18 15:39:00 -0700 | [diff] [blame] | 3624 | f6i->fib6_type = RTN_ANYCAST; |
| 3625 | f6i->fib6_flags |= RTF_ANYCAST; |
David Ahern | e8478e8 | 2018-04-17 17:33:13 -0700 | [diff] [blame] | 3626 | } else { |
David Ahern | 360a988 | 2018-04-18 15:39:00 -0700 | [diff] [blame] | 3627 | f6i->fib6_type = RTN_LOCAL; |
| 3628 | f6i->fib6_flags |= RTF_LOCAL; |
David Ahern | e8478e8 | 2018-04-17 17:33:13 -0700 | [diff] [blame] | 3629 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3630 | |
David Ahern | 360a988 | 2018-04-18 15:39:00 -0700 | [diff] [blame] | 3631 | f6i->fib6_nh.nh_gw = *addr; |
David Ahern | 93531c6 | 2018-04-17 17:33:25 -0700 | [diff] [blame] | 3632 | dev_hold(dev); |
David Ahern | 360a988 | 2018-04-18 15:39:00 -0700 | [diff] [blame] | 3633 | f6i->fib6_nh.nh_dev = dev; |
| 3634 | f6i->fib6_dst.addr = *addr; |
| 3635 | f6i->fib6_dst.plen = 128; |
David Ahern | ca25449 | 2015-10-12 11:47:10 -0700 | [diff] [blame] | 3636 | tb_id = l3mdev_fib_table(idev->dev) ? : RT6_TABLE_LOCAL; |
David Ahern | 360a988 | 2018-04-18 15:39:00 -0700 | [diff] [blame] | 3637 | f6i->fib6_table = fib6_get_table(net, tb_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3638 | |
David Ahern | 360a988 | 2018-04-18 15:39:00 -0700 | [diff] [blame] | 3639 | return f6i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3640 | } |
| 3641 | |
Daniel Walter | c3968a8 | 2011-04-13 21:10:57 +0000 | [diff] [blame] | 3642 | /* remove deleted ip from prefsrc entries */ |
| 3643 | struct arg_dev_net_ip { |
| 3644 | struct net_device *dev; |
| 3645 | struct net *net; |
| 3646 | struct in6_addr *addr; |
| 3647 | }; |
| 3648 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 3649 | static int fib6_remove_prefsrc(struct fib6_info *rt, void *arg) |
Daniel Walter | c3968a8 | 2011-04-13 21:10:57 +0000 | [diff] [blame] | 3650 | { |
| 3651 | struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev; |
| 3652 | struct net *net = ((struct arg_dev_net_ip *)arg)->net; |
| 3653 | struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr; |
| 3654 | |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 3655 | if (((void *)rt->fib6_nh.nh_dev == dev || !dev) && |
David Ahern | 421842e | 2018-04-17 17:33:18 -0700 | [diff] [blame] | 3656 | rt != net->ipv6.fib6_null_entry && |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 3657 | ipv6_addr_equal(addr, &rt->fib6_prefsrc.addr)) { |
Wei Wang | 60006a4 | 2017-10-06 12:05:58 -0700 | [diff] [blame] | 3658 | spin_lock_bh(&rt6_exception_lock); |
Daniel Walter | c3968a8 | 2011-04-13 21:10:57 +0000 | [diff] [blame] | 3659 | /* remove prefsrc entry */ |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 3660 | rt->fib6_prefsrc.plen = 0; |
Wei Wang | 60006a4 | 2017-10-06 12:05:58 -0700 | [diff] [blame] | 3661 | /* need to update cache as well */ |
| 3662 | rt6_exceptions_remove_prefsrc(rt); |
| 3663 | spin_unlock_bh(&rt6_exception_lock); |
Daniel Walter | c3968a8 | 2011-04-13 21:10:57 +0000 | [diff] [blame] | 3664 | } |
| 3665 | return 0; |
| 3666 | } |
| 3667 | |
| 3668 | void rt6_remove_prefsrc(struct inet6_ifaddr *ifp) |
| 3669 | { |
| 3670 | struct net *net = dev_net(ifp->idev->dev); |
| 3671 | struct arg_dev_net_ip adni = { |
| 3672 | .dev = ifp->idev->dev, |
| 3673 | .net = net, |
| 3674 | .addr = &ifp->addr, |
| 3675 | }; |
Li RongQing | 0c3584d | 2013-12-27 16:32:38 +0800 | [diff] [blame] | 3676 | fib6_clean_all(net, fib6_remove_prefsrc, &adni); |
Daniel Walter | c3968a8 | 2011-04-13 21:10:57 +0000 | [diff] [blame] | 3677 | } |
| 3678 | |
Duan Jiong | be7a010 | 2014-05-15 15:56:14 +0800 | [diff] [blame] | 3679 | #define RTF_RA_ROUTER (RTF_ADDRCONF | RTF_DEFAULT | RTF_GATEWAY) |
Duan Jiong | be7a010 | 2014-05-15 15:56:14 +0800 | [diff] [blame] | 3680 | |
| 3681 | /* Remove routers and update dst entries when gateway turn into host. */ |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 3682 | static int fib6_clean_tohost(struct fib6_info *rt, void *arg) |
Duan Jiong | be7a010 | 2014-05-15 15:56:14 +0800 | [diff] [blame] | 3683 | { |
| 3684 | struct in6_addr *gateway = (struct in6_addr *)arg; |
| 3685 | |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 3686 | if (((rt->fib6_flags & RTF_RA_ROUTER) == RTF_RA_ROUTER) && |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 3687 | ipv6_addr_equal(gateway, &rt->fib6_nh.nh_gw)) { |
Duan Jiong | be7a010 | 2014-05-15 15:56:14 +0800 | [diff] [blame] | 3688 | return -1; |
| 3689 | } |
Wei Wang | b16cb45 | 2017-10-06 12:06:00 -0700 | [diff] [blame] | 3690 | |
| 3691 | /* Further clean up cached routes in exception table. |
| 3692 | * This is needed because cached route may have a different |
| 3693 | * gateway than its 'parent' in the case of an ip redirect. |
| 3694 | */ |
| 3695 | rt6_exceptions_clean_tohost(rt, gateway); |
| 3696 | |
Duan Jiong | be7a010 | 2014-05-15 15:56:14 +0800 | [diff] [blame] | 3697 | return 0; |
| 3698 | } |
| 3699 | |
| 3700 | void rt6_clean_tohost(struct net *net, struct in6_addr *gateway) |
| 3701 | { |
| 3702 | fib6_clean_all(net, fib6_clean_tohost, gateway); |
| 3703 | } |
| 3704 | |
Ido Schimmel | 2127d95 | 2018-01-07 12:45:03 +0200 | [diff] [blame] | 3705 | struct arg_netdev_event { |
| 3706 | const struct net_device *dev; |
Ido Schimmel | 4c981e2 | 2018-01-07 12:45:04 +0200 | [diff] [blame] | 3707 | union { |
| 3708 | unsigned int nh_flags; |
| 3709 | unsigned long event; |
| 3710 | }; |
Ido Schimmel | 2127d95 | 2018-01-07 12:45:03 +0200 | [diff] [blame] | 3711 | }; |
| 3712 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 3713 | 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] | 3714 | { |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 3715 | struct fib6_info *iter; |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 3716 | struct fib6_node *fn; |
| 3717 | |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 3718 | fn = rcu_dereference_protected(rt->fib6_node, |
| 3719 | lockdep_is_held(&rt->fib6_table->tb6_lock)); |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 3720 | iter = rcu_dereference_protected(fn->leaf, |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 3721 | lockdep_is_held(&rt->fib6_table->tb6_lock)); |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 3722 | while (iter) { |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 3723 | if (iter->fib6_metric == rt->fib6_metric && |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 3724 | rt6_qualify_for_ecmp(iter)) |
| 3725 | return iter; |
| 3726 | iter = rcu_dereference_protected(iter->rt6_next, |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 3727 | lockdep_is_held(&rt->fib6_table->tb6_lock)); |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 3728 | } |
| 3729 | |
| 3730 | return NULL; |
| 3731 | } |
| 3732 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 3733 | static bool rt6_is_dead(const struct fib6_info *rt) |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 3734 | { |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 3735 | if (rt->fib6_nh.nh_flags & RTNH_F_DEAD || |
| 3736 | (rt->fib6_nh.nh_flags & RTNH_F_LINKDOWN && |
David Ahern | dcd1f57 | 2018-04-18 15:39:05 -0700 | [diff] [blame^] | 3737 | fib6_ignore_linkdown(rt))) |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 3738 | return true; |
| 3739 | |
| 3740 | return false; |
| 3741 | } |
| 3742 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 3743 | static int rt6_multipath_total_weight(const struct fib6_info *rt) |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 3744 | { |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 3745 | struct fib6_info *iter; |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 3746 | int total = 0; |
| 3747 | |
| 3748 | if (!rt6_is_dead(rt)) |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 3749 | total += rt->fib6_nh.nh_weight; |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 3750 | |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 3751 | list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) { |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 3752 | if (!rt6_is_dead(iter)) |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 3753 | total += iter->fib6_nh.nh_weight; |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 3754 | } |
| 3755 | |
| 3756 | return total; |
| 3757 | } |
| 3758 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 3759 | 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] | 3760 | { |
| 3761 | int upper_bound = -1; |
| 3762 | |
| 3763 | if (!rt6_is_dead(rt)) { |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 3764 | *weight += rt->fib6_nh.nh_weight; |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 3765 | upper_bound = DIV_ROUND_CLOSEST_ULL((u64) (*weight) << 31, |
| 3766 | total) - 1; |
| 3767 | } |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 3768 | atomic_set(&rt->fib6_nh.nh_upper_bound, upper_bound); |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 3769 | } |
| 3770 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 3771 | 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] | 3772 | { |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 3773 | struct fib6_info *iter; |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 3774 | int weight = 0; |
| 3775 | |
| 3776 | rt6_upper_bound_set(rt, &weight, total); |
| 3777 | |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 3778 | list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 3779 | rt6_upper_bound_set(iter, &weight, total); |
| 3780 | } |
| 3781 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 3782 | void rt6_multipath_rebalance(struct fib6_info *rt) |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 3783 | { |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 3784 | struct fib6_info *first; |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 3785 | int total; |
| 3786 | |
| 3787 | /* In case the entire multipath route was marked for flushing, |
| 3788 | * then there is no need to rebalance upon the removal of every |
| 3789 | * sibling route. |
| 3790 | */ |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 3791 | if (!rt->fib6_nsiblings || rt->should_flush) |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 3792 | return; |
| 3793 | |
| 3794 | /* During lookup routes are evaluated in order, so we need to |
| 3795 | * make sure upper bounds are assigned from the first sibling |
| 3796 | * onwards. |
| 3797 | */ |
| 3798 | first = rt6_multipath_first_sibling(rt); |
| 3799 | if (WARN_ON_ONCE(!first)) |
| 3800 | return; |
| 3801 | |
| 3802 | total = rt6_multipath_total_weight(first); |
| 3803 | rt6_multipath_upper_bound_set(first, total); |
| 3804 | } |
| 3805 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 3806 | static int fib6_ifup(struct fib6_info *rt, void *p_arg) |
Ido Schimmel | 2127d95 | 2018-01-07 12:45:03 +0200 | [diff] [blame] | 3807 | { |
| 3808 | const struct arg_netdev_event *arg = p_arg; |
David Ahern | 7aef685 | 2018-04-17 17:33:10 -0700 | [diff] [blame] | 3809 | struct net *net = dev_net(arg->dev); |
Ido Schimmel | 2127d95 | 2018-01-07 12:45:03 +0200 | [diff] [blame] | 3810 | |
David Ahern | 421842e | 2018-04-17 17:33:18 -0700 | [diff] [blame] | 3811 | if (rt != net->ipv6.fib6_null_entry && rt->fib6_nh.nh_dev == arg->dev) { |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 3812 | rt->fib6_nh.nh_flags &= ~arg->nh_flags; |
David Ahern | 7aef685 | 2018-04-17 17:33:10 -0700 | [diff] [blame] | 3813 | fib6_update_sernum_upto_root(net, rt); |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 3814 | rt6_multipath_rebalance(rt); |
Ido Schimmel | 1de178e | 2018-01-07 12:45:15 +0200 | [diff] [blame] | 3815 | } |
Ido Schimmel | 2127d95 | 2018-01-07 12:45:03 +0200 | [diff] [blame] | 3816 | |
| 3817 | return 0; |
| 3818 | } |
| 3819 | |
| 3820 | void rt6_sync_up(struct net_device *dev, unsigned int nh_flags) |
| 3821 | { |
| 3822 | struct arg_netdev_event arg = { |
| 3823 | .dev = dev, |
Ido Schimmel | 6802f3a | 2018-01-12 22:07:36 +0200 | [diff] [blame] | 3824 | { |
| 3825 | .nh_flags = nh_flags, |
| 3826 | }, |
Ido Schimmel | 2127d95 | 2018-01-07 12:45:03 +0200 | [diff] [blame] | 3827 | }; |
| 3828 | |
| 3829 | if (nh_flags & RTNH_F_DEAD && netif_carrier_ok(dev)) |
| 3830 | arg.nh_flags |= RTNH_F_LINKDOWN; |
| 3831 | |
| 3832 | fib6_clean_all(dev_net(dev), fib6_ifup, &arg); |
| 3833 | } |
| 3834 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 3835 | static bool rt6_multipath_uses_dev(const struct fib6_info *rt, |
Ido Schimmel | 1de178e | 2018-01-07 12:45:15 +0200 | [diff] [blame] | 3836 | const struct net_device *dev) |
| 3837 | { |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 3838 | struct fib6_info *iter; |
Ido Schimmel | 1de178e | 2018-01-07 12:45:15 +0200 | [diff] [blame] | 3839 | |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 3840 | if (rt->fib6_nh.nh_dev == dev) |
Ido Schimmel | 1de178e | 2018-01-07 12:45:15 +0200 | [diff] [blame] | 3841 | return true; |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 3842 | list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 3843 | if (iter->fib6_nh.nh_dev == dev) |
Ido Schimmel | 1de178e | 2018-01-07 12:45:15 +0200 | [diff] [blame] | 3844 | return true; |
| 3845 | |
| 3846 | return false; |
| 3847 | } |
| 3848 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 3849 | static void rt6_multipath_flush(struct fib6_info *rt) |
Ido Schimmel | 1de178e | 2018-01-07 12:45:15 +0200 | [diff] [blame] | 3850 | { |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 3851 | struct fib6_info *iter; |
Ido Schimmel | 1de178e | 2018-01-07 12:45:15 +0200 | [diff] [blame] | 3852 | |
| 3853 | rt->should_flush = 1; |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 3854 | list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) |
Ido Schimmel | 1de178e | 2018-01-07 12:45:15 +0200 | [diff] [blame] | 3855 | iter->should_flush = 1; |
| 3856 | } |
| 3857 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 3858 | static unsigned int rt6_multipath_dead_count(const struct fib6_info *rt, |
Ido Schimmel | 1de178e | 2018-01-07 12:45:15 +0200 | [diff] [blame] | 3859 | const struct net_device *down_dev) |
| 3860 | { |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 3861 | struct fib6_info *iter; |
Ido Schimmel | 1de178e | 2018-01-07 12:45:15 +0200 | [diff] [blame] | 3862 | unsigned int dead = 0; |
| 3863 | |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 3864 | if (rt->fib6_nh.nh_dev == down_dev || |
| 3865 | rt->fib6_nh.nh_flags & RTNH_F_DEAD) |
Ido Schimmel | 1de178e | 2018-01-07 12:45:15 +0200 | [diff] [blame] | 3866 | dead++; |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 3867 | list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 3868 | if (iter->fib6_nh.nh_dev == down_dev || |
| 3869 | iter->fib6_nh.nh_flags & RTNH_F_DEAD) |
Ido Schimmel | 1de178e | 2018-01-07 12:45:15 +0200 | [diff] [blame] | 3870 | dead++; |
| 3871 | |
| 3872 | return dead; |
| 3873 | } |
| 3874 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 3875 | static void rt6_multipath_nh_flags_set(struct fib6_info *rt, |
Ido Schimmel | 1de178e | 2018-01-07 12:45:15 +0200 | [diff] [blame] | 3876 | const struct net_device *dev, |
| 3877 | unsigned int nh_flags) |
| 3878 | { |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 3879 | struct fib6_info *iter; |
Ido Schimmel | 1de178e | 2018-01-07 12:45:15 +0200 | [diff] [blame] | 3880 | |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 3881 | if (rt->fib6_nh.nh_dev == dev) |
| 3882 | rt->fib6_nh.nh_flags |= nh_flags; |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 3883 | list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 3884 | if (iter->fib6_nh.nh_dev == dev) |
| 3885 | iter->fib6_nh.nh_flags |= nh_flags; |
Ido Schimmel | 1de178e | 2018-01-07 12:45:15 +0200 | [diff] [blame] | 3886 | } |
| 3887 | |
David Ahern | a1a22c1 | 2017-01-18 07:40:36 -0800 | [diff] [blame] | 3888 | /* called with write lock held for table with rt */ |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 3889 | static int fib6_ifdown(struct fib6_info *rt, void *p_arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3890 | { |
Ido Schimmel | 4c981e2 | 2018-01-07 12:45:04 +0200 | [diff] [blame] | 3891 | const struct arg_netdev_event *arg = p_arg; |
| 3892 | const struct net_device *dev = arg->dev; |
David Ahern | 7aef685 | 2018-04-17 17:33:10 -0700 | [diff] [blame] | 3893 | struct net *net = dev_net(dev); |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 3894 | |
David Ahern | 421842e | 2018-04-17 17:33:18 -0700 | [diff] [blame] | 3895 | if (rt == net->ipv6.fib6_null_entry) |
Ido Schimmel | 27c6fa7 | 2018-01-07 12:45:05 +0200 | [diff] [blame] | 3896 | return 0; |
| 3897 | |
| 3898 | switch (arg->event) { |
| 3899 | case NETDEV_UNREGISTER: |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 3900 | return rt->fib6_nh.nh_dev == dev ? -1 : 0; |
Ido Schimmel | 27c6fa7 | 2018-01-07 12:45:05 +0200 | [diff] [blame] | 3901 | case NETDEV_DOWN: |
Ido Schimmel | 1de178e | 2018-01-07 12:45:15 +0200 | [diff] [blame] | 3902 | if (rt->should_flush) |
Ido Schimmel | 27c6fa7 | 2018-01-07 12:45:05 +0200 | [diff] [blame] | 3903 | return -1; |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 3904 | if (!rt->fib6_nsiblings) |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 3905 | return rt->fib6_nh.nh_dev == dev ? -1 : 0; |
Ido Schimmel | 1de178e | 2018-01-07 12:45:15 +0200 | [diff] [blame] | 3906 | if (rt6_multipath_uses_dev(rt, dev)) { |
| 3907 | unsigned int count; |
| 3908 | |
| 3909 | count = rt6_multipath_dead_count(rt, dev); |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 3910 | if (rt->fib6_nsiblings + 1 == count) { |
Ido Schimmel | 1de178e | 2018-01-07 12:45:15 +0200 | [diff] [blame] | 3911 | rt6_multipath_flush(rt); |
| 3912 | return -1; |
| 3913 | } |
| 3914 | rt6_multipath_nh_flags_set(rt, dev, RTNH_F_DEAD | |
| 3915 | RTNH_F_LINKDOWN); |
David Ahern | 7aef685 | 2018-04-17 17:33:10 -0700 | [diff] [blame] | 3916 | fib6_update_sernum(net, rt); |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 3917 | rt6_multipath_rebalance(rt); |
Ido Schimmel | 1de178e | 2018-01-07 12:45:15 +0200 | [diff] [blame] | 3918 | } |
| 3919 | return -2; |
Ido Schimmel | 27c6fa7 | 2018-01-07 12:45:05 +0200 | [diff] [blame] | 3920 | case NETDEV_CHANGE: |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 3921 | if (rt->fib6_nh.nh_dev != dev || |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 3922 | rt->fib6_flags & (RTF_LOCAL | RTF_ANYCAST)) |
Ido Schimmel | 27c6fa7 | 2018-01-07 12:45:05 +0200 | [diff] [blame] | 3923 | break; |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 3924 | rt->fib6_nh.nh_flags |= RTNH_F_LINKDOWN; |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 3925 | rt6_multipath_rebalance(rt); |
Ido Schimmel | 27c6fa7 | 2018-01-07 12:45:05 +0200 | [diff] [blame] | 3926 | break; |
Ido Schimmel | 2b24136 | 2018-01-07 12:45:02 +0200 | [diff] [blame] | 3927 | } |
David S. Miller | c159d30 | 2011-12-26 15:24:36 -0500 | [diff] [blame] | 3928 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3929 | return 0; |
| 3930 | } |
| 3931 | |
Ido Schimmel | 27c6fa7 | 2018-01-07 12:45:05 +0200 | [diff] [blame] | 3932 | void rt6_sync_down_dev(struct net_device *dev, unsigned long event) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3933 | { |
Ido Schimmel | 4c981e2 | 2018-01-07 12:45:04 +0200 | [diff] [blame] | 3934 | struct arg_netdev_event arg = { |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 3935 | .dev = dev, |
Ido Schimmel | 6802f3a | 2018-01-12 22:07:36 +0200 | [diff] [blame] | 3936 | { |
| 3937 | .event = event, |
| 3938 | }, |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 3939 | }; |
| 3940 | |
Ido Schimmel | 4c981e2 | 2018-01-07 12:45:04 +0200 | [diff] [blame] | 3941 | fib6_clean_all(dev_net(dev), fib6_ifdown, &arg); |
| 3942 | } |
| 3943 | |
| 3944 | void rt6_disable_ip(struct net_device *dev, unsigned long event) |
| 3945 | { |
| 3946 | rt6_sync_down_dev(dev, event); |
| 3947 | rt6_uncached_list_flush_dev(dev_net(dev), dev); |
| 3948 | neigh_ifdown(&nd_tbl, dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3949 | } |
| 3950 | |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 3951 | struct rt6_mtu_change_arg { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3952 | struct net_device *dev; |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 3953 | unsigned int mtu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3954 | }; |
| 3955 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 3956 | 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] | 3957 | { |
| 3958 | struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg; |
| 3959 | struct inet6_dev *idev; |
| 3960 | |
| 3961 | /* In IPv6 pmtu discovery is not optional, |
| 3962 | so that RTAX_MTU lock cannot disable it. |
| 3963 | We still use this lock to block changes |
| 3964 | caused by addrconf/ndisc. |
| 3965 | */ |
| 3966 | |
| 3967 | idev = __in6_dev_get(arg->dev); |
David S. Miller | 3830847 | 2011-12-03 18:02:47 -0500 | [diff] [blame] | 3968 | if (!idev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3969 | return 0; |
| 3970 | |
| 3971 | /* For administrative MTU increase, there is no way to discover |
| 3972 | IPv6 PMTU increase, so PMTU increase should be updated here. |
| 3973 | Since RFC 1981 doesn't include administrative MTU increase |
| 3974 | update PMTU increase is a MUST. (i.e. jumbo frame) |
| 3975 | */ |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 3976 | if (rt->fib6_nh.nh_dev == arg->dev && |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 3977 | !fib6_metric_locked(rt, RTAX_MTU)) { |
| 3978 | u32 mtu = rt->fib6_pmtu; |
| 3979 | |
| 3980 | if (mtu >= arg->mtu || |
| 3981 | (mtu < arg->mtu && mtu == idev->cnf.mtu6)) |
| 3982 | fib6_metric_set(rt, RTAX_MTU, arg->mtu); |
| 3983 | |
Wei Wang | f5bbe7e | 2017-10-06 12:05:59 -0700 | [diff] [blame] | 3984 | spin_lock_bh(&rt6_exception_lock); |
Stefano Brivio | e9fa149 | 2018-03-06 11:10:19 +0100 | [diff] [blame] | 3985 | rt6_exceptions_update_pmtu(idev, rt, arg->mtu); |
Wei Wang | f5bbe7e | 2017-10-06 12:05:59 -0700 | [diff] [blame] | 3986 | spin_unlock_bh(&rt6_exception_lock); |
Simon Arlott | 566cfd8 | 2007-07-26 00:09:55 -0700 | [diff] [blame] | 3987 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3988 | return 0; |
| 3989 | } |
| 3990 | |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 3991 | void rt6_mtu_change(struct net_device *dev, unsigned int mtu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3992 | { |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 3993 | struct rt6_mtu_change_arg arg = { |
| 3994 | .dev = dev, |
| 3995 | .mtu = mtu, |
| 3996 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3997 | |
Li RongQing | 0c3584d | 2013-12-27 16:32:38 +0800 | [diff] [blame] | 3998 | fib6_clean_all(dev_net(dev), rt6_mtu_change_route, &arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3999 | } |
| 4000 | |
Patrick McHardy | ef7c79e | 2007-06-05 12:38:30 -0700 | [diff] [blame] | 4001 | static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = { |
Thomas Graf | 5176f91 | 2006-08-26 20:13:18 -0700 | [diff] [blame] | 4002 | [RTA_GATEWAY] = { .len = sizeof(struct in6_addr) }, |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4003 | [RTA_OIF] = { .type = NLA_U32 }, |
Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 4004 | [RTA_IIF] = { .type = NLA_U32 }, |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4005 | [RTA_PRIORITY] = { .type = NLA_U32 }, |
| 4006 | [RTA_METRICS] = { .type = NLA_NESTED }, |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 4007 | [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) }, |
Lubomir Rintel | c78ba6d | 2015-03-11 15:39:21 +0100 | [diff] [blame] | 4008 | [RTA_PREF] = { .type = NLA_U8 }, |
Roopa Prabhu | 19e42e4 | 2015-07-21 10:43:48 +0200 | [diff] [blame] | 4009 | [RTA_ENCAP_TYPE] = { .type = NLA_U16 }, |
| 4010 | [RTA_ENCAP] = { .type = NLA_NESTED }, |
Xin Long | 32bc201 | 2015-12-16 17:50:11 +0800 | [diff] [blame] | 4011 | [RTA_EXPIRES] = { .type = NLA_U32 }, |
Lorenzo Colitti | 622ec2c | 2016-11-04 02:23:42 +0900 | [diff] [blame] | 4012 | [RTA_UID] = { .type = NLA_U32 }, |
Liping Zhang | 3b45a41 | 2017-02-27 20:59:39 +0800 | [diff] [blame] | 4013 | [RTA_MARK] = { .type = NLA_U32 }, |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4014 | }; |
| 4015 | |
| 4016 | 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] | 4017 | struct fib6_config *cfg, |
| 4018 | struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4019 | { |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4020 | struct rtmsg *rtm; |
| 4021 | struct nlattr *tb[RTA_MAX+1]; |
Lubomir Rintel | c78ba6d | 2015-03-11 15:39:21 +0100 | [diff] [blame] | 4022 | unsigned int pref; |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4023 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4024 | |
Johannes Berg | fceb643 | 2017-04-12 14:34:07 +0200 | [diff] [blame] | 4025 | err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy, |
| 4026 | NULL); |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4027 | if (err < 0) |
| 4028 | goto errout; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4029 | |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4030 | err = -EINVAL; |
| 4031 | rtm = nlmsg_data(nlh); |
| 4032 | memset(cfg, 0, sizeof(*cfg)); |
| 4033 | |
| 4034 | cfg->fc_table = rtm->rtm_table; |
| 4035 | cfg->fc_dst_len = rtm->rtm_dst_len; |
| 4036 | cfg->fc_src_len = rtm->rtm_src_len; |
| 4037 | cfg->fc_flags = RTF_UP; |
| 4038 | cfg->fc_protocol = rtm->rtm_protocol; |
Nicolas Dichtel | ef2c7d7 | 2012-09-05 02:12:42 +0000 | [diff] [blame] | 4039 | cfg->fc_type = rtm->rtm_type; |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4040 | |
Nicolas Dichtel | ef2c7d7 | 2012-09-05 02:12:42 +0000 | [diff] [blame] | 4041 | if (rtm->rtm_type == RTN_UNREACHABLE || |
| 4042 | rtm->rtm_type == RTN_BLACKHOLE || |
Nicolas Dichtel | b4949ab | 2012-09-06 05:53:35 +0000 | [diff] [blame] | 4043 | rtm->rtm_type == RTN_PROHIBIT || |
| 4044 | rtm->rtm_type == RTN_THROW) |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4045 | cfg->fc_flags |= RTF_REJECT; |
| 4046 | |
Maciej Żenczykowski | ab79ad1 | 2010-09-27 00:07:02 +0000 | [diff] [blame] | 4047 | if (rtm->rtm_type == RTN_LOCAL) |
| 4048 | cfg->fc_flags |= RTF_LOCAL; |
| 4049 | |
Martin KaFai Lau | 1f56a01f | 2015-04-28 13:03:03 -0700 | [diff] [blame] | 4050 | if (rtm->rtm_flags & RTM_F_CLONED) |
| 4051 | cfg->fc_flags |= RTF_CACHE; |
| 4052 | |
David Ahern | fc1e64e | 2018-01-25 16:55:09 -0800 | [diff] [blame] | 4053 | cfg->fc_flags |= (rtm->rtm_flags & RTNH_F_ONLINK); |
| 4054 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 4055 | cfg->fc_nlinfo.portid = NETLINK_CB(skb).portid; |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4056 | cfg->fc_nlinfo.nlh = nlh; |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 4057 | cfg->fc_nlinfo.nl_net = sock_net(skb->sk); |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4058 | |
| 4059 | if (tb[RTA_GATEWAY]) { |
Jiri Benc | 67b61f6 | 2015-03-29 16:59:26 +0200 | [diff] [blame] | 4060 | cfg->fc_gateway = nla_get_in6_addr(tb[RTA_GATEWAY]); |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4061 | cfg->fc_flags |= RTF_GATEWAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4062 | } |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4063 | |
| 4064 | if (tb[RTA_DST]) { |
| 4065 | int plen = (rtm->rtm_dst_len + 7) >> 3; |
| 4066 | |
| 4067 | if (nla_len(tb[RTA_DST]) < plen) |
| 4068 | goto errout; |
| 4069 | |
| 4070 | nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4071 | } |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4072 | |
| 4073 | if (tb[RTA_SRC]) { |
| 4074 | int plen = (rtm->rtm_src_len + 7) >> 3; |
| 4075 | |
| 4076 | if (nla_len(tb[RTA_SRC]) < plen) |
| 4077 | goto errout; |
| 4078 | |
| 4079 | nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4080 | } |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4081 | |
Daniel Walter | c3968a8 | 2011-04-13 21:10:57 +0000 | [diff] [blame] | 4082 | if (tb[RTA_PREFSRC]) |
Jiri Benc | 67b61f6 | 2015-03-29 16:59:26 +0200 | [diff] [blame] | 4083 | cfg->fc_prefsrc = nla_get_in6_addr(tb[RTA_PREFSRC]); |
Daniel Walter | c3968a8 | 2011-04-13 21:10:57 +0000 | [diff] [blame] | 4084 | |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4085 | if (tb[RTA_OIF]) |
| 4086 | cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]); |
| 4087 | |
| 4088 | if (tb[RTA_PRIORITY]) |
| 4089 | cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]); |
| 4090 | |
| 4091 | if (tb[RTA_METRICS]) { |
| 4092 | cfg->fc_mx = nla_data(tb[RTA_METRICS]); |
| 4093 | cfg->fc_mx_len = nla_len(tb[RTA_METRICS]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4094 | } |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4095 | |
| 4096 | if (tb[RTA_TABLE]) |
| 4097 | cfg->fc_table = nla_get_u32(tb[RTA_TABLE]); |
| 4098 | |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 4099 | if (tb[RTA_MULTIPATH]) { |
| 4100 | cfg->fc_mp = nla_data(tb[RTA_MULTIPATH]); |
| 4101 | cfg->fc_mp_len = nla_len(tb[RTA_MULTIPATH]); |
David Ahern | 9ed5959 | 2017-01-17 14:57:36 -0800 | [diff] [blame] | 4102 | |
| 4103 | err = lwtunnel_valid_encap_type_attr(cfg->fc_mp, |
David Ahern | c255bd6 | 2017-05-27 16:19:27 -0600 | [diff] [blame] | 4104 | cfg->fc_mp_len, extack); |
David Ahern | 9ed5959 | 2017-01-17 14:57:36 -0800 | [diff] [blame] | 4105 | if (err < 0) |
| 4106 | goto errout; |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 4107 | } |
| 4108 | |
Lubomir Rintel | c78ba6d | 2015-03-11 15:39:21 +0100 | [diff] [blame] | 4109 | if (tb[RTA_PREF]) { |
| 4110 | pref = nla_get_u8(tb[RTA_PREF]); |
| 4111 | if (pref != ICMPV6_ROUTER_PREF_LOW && |
| 4112 | pref != ICMPV6_ROUTER_PREF_HIGH) |
| 4113 | pref = ICMPV6_ROUTER_PREF_MEDIUM; |
| 4114 | cfg->fc_flags |= RTF_PREF(pref); |
| 4115 | } |
| 4116 | |
Roopa Prabhu | 19e42e4 | 2015-07-21 10:43:48 +0200 | [diff] [blame] | 4117 | if (tb[RTA_ENCAP]) |
| 4118 | cfg->fc_encap = tb[RTA_ENCAP]; |
| 4119 | |
David Ahern | 9ed5959 | 2017-01-17 14:57:36 -0800 | [diff] [blame] | 4120 | if (tb[RTA_ENCAP_TYPE]) { |
Roopa Prabhu | 19e42e4 | 2015-07-21 10:43:48 +0200 | [diff] [blame] | 4121 | cfg->fc_encap_type = nla_get_u16(tb[RTA_ENCAP_TYPE]); |
| 4122 | |
David Ahern | c255bd6 | 2017-05-27 16:19:27 -0600 | [diff] [blame] | 4123 | err = lwtunnel_valid_encap_type(cfg->fc_encap_type, extack); |
David Ahern | 9ed5959 | 2017-01-17 14:57:36 -0800 | [diff] [blame] | 4124 | if (err < 0) |
| 4125 | goto errout; |
| 4126 | } |
| 4127 | |
Xin Long | 32bc201 | 2015-12-16 17:50:11 +0800 | [diff] [blame] | 4128 | if (tb[RTA_EXPIRES]) { |
| 4129 | unsigned long timeout = addrconf_timeout_fixup(nla_get_u32(tb[RTA_EXPIRES]), HZ); |
| 4130 | |
| 4131 | if (addrconf_finite_timeout(timeout)) { |
| 4132 | cfg->fc_expires = jiffies_to_clock_t(timeout * HZ); |
| 4133 | cfg->fc_flags |= RTF_EXPIRES; |
| 4134 | } |
| 4135 | } |
| 4136 | |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4137 | err = 0; |
| 4138 | errout: |
| 4139 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4140 | } |
| 4141 | |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 4142 | struct rt6_nh { |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4143 | struct fib6_info *fib6_info; |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 4144 | struct fib6_config r_cfg; |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 4145 | struct list_head next; |
| 4146 | }; |
| 4147 | |
| 4148 | static void ip6_print_replace_route_err(struct list_head *rt6_nh_list) |
| 4149 | { |
| 4150 | struct rt6_nh *nh; |
| 4151 | |
| 4152 | list_for_each_entry(nh, rt6_nh_list, next) { |
David Ahern | 7d4d506 | 2017-02-02 12:37:12 -0800 | [diff] [blame] | 4153 | pr_warn("IPV6: multipath route replace failed (check consistency of installed routes): %pI6c nexthop %pI6c ifi %d\n", |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 4154 | &nh->r_cfg.fc_dst, &nh->r_cfg.fc_gateway, |
| 4155 | nh->r_cfg.fc_ifindex); |
| 4156 | } |
| 4157 | } |
| 4158 | |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 4159 | static int ip6_route_info_append(struct net *net, |
| 4160 | struct list_head *rt6_nh_list, |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4161 | struct fib6_info *rt, |
| 4162 | struct fib6_config *r_cfg) |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 4163 | { |
| 4164 | struct rt6_nh *nh; |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 4165 | int err = -EEXIST; |
| 4166 | |
| 4167 | list_for_each_entry(nh, rt6_nh_list, next) { |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4168 | /* check if fib6_info already exists */ |
| 4169 | if (rt6_duplicate_nexthop(nh->fib6_info, rt)) |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 4170 | return err; |
| 4171 | } |
| 4172 | |
| 4173 | nh = kzalloc(sizeof(*nh), GFP_KERNEL); |
| 4174 | if (!nh) |
| 4175 | return -ENOMEM; |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4176 | nh->fib6_info = rt; |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 4177 | err = ip6_convert_metrics(net, rt, r_cfg); |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 4178 | if (err) { |
| 4179 | kfree(nh); |
| 4180 | return err; |
| 4181 | } |
| 4182 | memcpy(&nh->r_cfg, r_cfg, sizeof(*r_cfg)); |
| 4183 | list_add_tail(&nh->next, rt6_nh_list); |
| 4184 | |
| 4185 | return 0; |
| 4186 | } |
| 4187 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4188 | static void ip6_route_mpath_notify(struct fib6_info *rt, |
| 4189 | struct fib6_info *rt_last, |
David Ahern | 3b1137f | 2017-02-02 12:37:10 -0800 | [diff] [blame] | 4190 | struct nl_info *info, |
| 4191 | __u16 nlflags) |
| 4192 | { |
| 4193 | /* if this is an APPEND route, then rt points to the first route |
| 4194 | * inserted and rt_last points to last route inserted. Userspace |
| 4195 | * wants a consistent dump of the route which starts at the first |
| 4196 | * nexthop. Since sibling routes are always added at the end of |
| 4197 | * the list, find the first sibling of the last route appended |
| 4198 | */ |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 4199 | if ((nlflags & NLM_F_APPEND) && rt_last && rt_last->fib6_nsiblings) { |
| 4200 | rt = list_first_entry(&rt_last->fib6_siblings, |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4201 | struct fib6_info, |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 4202 | fib6_siblings); |
David Ahern | 3b1137f | 2017-02-02 12:37:10 -0800 | [diff] [blame] | 4203 | } |
| 4204 | |
| 4205 | if (rt) |
| 4206 | inet6_rt_notify(RTM_NEWROUTE, rt, info, nlflags); |
| 4207 | } |
| 4208 | |
David Ahern | 333c430 | 2017-05-21 10:12:04 -0600 | [diff] [blame] | 4209 | static int ip6_route_multipath_add(struct fib6_config *cfg, |
| 4210 | struct netlink_ext_ack *extack) |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 4211 | { |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4212 | struct fib6_info *rt_notif = NULL, *rt_last = NULL; |
David Ahern | 3b1137f | 2017-02-02 12:37:10 -0800 | [diff] [blame] | 4213 | struct nl_info *info = &cfg->fc_nlinfo; |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 4214 | struct fib6_config r_cfg; |
| 4215 | struct rtnexthop *rtnh; |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4216 | struct fib6_info *rt; |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 4217 | struct rt6_nh *err_nh; |
| 4218 | struct rt6_nh *nh, *nh_safe; |
David Ahern | 3b1137f | 2017-02-02 12:37:10 -0800 | [diff] [blame] | 4219 | __u16 nlflags; |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 4220 | int remaining; |
| 4221 | int attrlen; |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 4222 | int err = 1; |
| 4223 | int nhn = 0; |
| 4224 | int replace = (cfg->fc_nlinfo.nlh && |
| 4225 | (cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_REPLACE)); |
| 4226 | LIST_HEAD(rt6_nh_list); |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 4227 | |
David Ahern | 3b1137f | 2017-02-02 12:37:10 -0800 | [diff] [blame] | 4228 | nlflags = replace ? NLM_F_REPLACE : NLM_F_CREATE; |
| 4229 | if (info->nlh && info->nlh->nlmsg_flags & NLM_F_APPEND) |
| 4230 | nlflags |= NLM_F_APPEND; |
| 4231 | |
Michal Kubeček | 35f1b4e | 2015-05-18 20:53:55 +0200 | [diff] [blame] | 4232 | remaining = cfg->fc_mp_len; |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 4233 | rtnh = (struct rtnexthop *)cfg->fc_mp; |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 4234 | |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 4235 | /* Parse a Multipath Entry and build a list (rt6_nh_list) of |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4236 | * fib6_info structs per nexthop |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 4237 | */ |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 4238 | while (rtnh_ok(rtnh, remaining)) { |
| 4239 | memcpy(&r_cfg, cfg, sizeof(*cfg)); |
| 4240 | if (rtnh->rtnh_ifindex) |
| 4241 | r_cfg.fc_ifindex = rtnh->rtnh_ifindex; |
| 4242 | |
| 4243 | attrlen = rtnh_attrlen(rtnh); |
| 4244 | if (attrlen > 0) { |
| 4245 | struct nlattr *nla, *attrs = rtnh_attrs(rtnh); |
| 4246 | |
| 4247 | nla = nla_find(attrs, attrlen, RTA_GATEWAY); |
| 4248 | if (nla) { |
Jiri Benc | 67b61f6 | 2015-03-29 16:59:26 +0200 | [diff] [blame] | 4249 | r_cfg.fc_gateway = nla_get_in6_addr(nla); |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 4250 | r_cfg.fc_flags |= RTF_GATEWAY; |
| 4251 | } |
Roopa Prabhu | 19e42e4 | 2015-07-21 10:43:48 +0200 | [diff] [blame] | 4252 | r_cfg.fc_encap = nla_find(attrs, attrlen, RTA_ENCAP); |
| 4253 | nla = nla_find(attrs, attrlen, RTA_ENCAP_TYPE); |
| 4254 | if (nla) |
| 4255 | r_cfg.fc_encap_type = nla_get_u16(nla); |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 4256 | } |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 4257 | |
David Ahern | 68e2ffd | 2018-03-20 10:06:59 -0700 | [diff] [blame] | 4258 | r_cfg.fc_flags |= (rtnh->rtnh_flags & RTNH_F_ONLINK); |
David Ahern | acb54e3 | 2018-04-17 17:33:22 -0700 | [diff] [blame] | 4259 | rt = ip6_route_info_create(&r_cfg, GFP_KERNEL, extack); |
Roopa Prabhu | 8c5b83f | 2015-10-10 08:26:36 -0700 | [diff] [blame] | 4260 | if (IS_ERR(rt)) { |
| 4261 | err = PTR_ERR(rt); |
| 4262 | rt = NULL; |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 4263 | goto cleanup; |
Roopa Prabhu | 8c5b83f | 2015-10-10 08:26:36 -0700 | [diff] [blame] | 4264 | } |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 4265 | |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 4266 | rt->fib6_nh.nh_weight = rtnh->rtnh_hops + 1; |
Ido Schimmel | 398958a | 2018-01-09 16:40:28 +0200 | [diff] [blame] | 4267 | |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 4268 | err = ip6_route_info_append(info->nl_net, &rt6_nh_list, |
| 4269 | rt, &r_cfg); |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 4270 | if (err) { |
David Ahern | 93531c6 | 2018-04-17 17:33:25 -0700 | [diff] [blame] | 4271 | fib6_info_release(rt); |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 4272 | goto cleanup; |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 4273 | } |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 4274 | |
| 4275 | rtnh = rtnh_next(rtnh, &remaining); |
| 4276 | } |
| 4277 | |
David Ahern | 3b1137f | 2017-02-02 12:37:10 -0800 | [diff] [blame] | 4278 | /* for add and replace send one notification with all nexthops. |
| 4279 | * Skip the notification in fib6_add_rt2node and send one with |
| 4280 | * the full route when done |
| 4281 | */ |
| 4282 | info->skip_notify = 1; |
| 4283 | |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 4284 | err_nh = NULL; |
| 4285 | list_for_each_entry(nh, &rt6_nh_list, next) { |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4286 | rt_last = nh->fib6_info; |
| 4287 | err = __ip6_ins_rt(nh->fib6_info, info, extack); |
| 4288 | fib6_info_release(nh->fib6_info); |
David Ahern | 93531c6 | 2018-04-17 17:33:25 -0700 | [diff] [blame] | 4289 | |
David Ahern | 3b1137f | 2017-02-02 12:37:10 -0800 | [diff] [blame] | 4290 | /* save reference to first route for notification */ |
| 4291 | if (!rt_notif && !err) |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4292 | rt_notif = nh->fib6_info; |
David Ahern | 3b1137f | 2017-02-02 12:37:10 -0800 | [diff] [blame] | 4293 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4294 | /* nh->fib6_info is used or freed at this point, reset to NULL*/ |
| 4295 | nh->fib6_info = NULL; |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 4296 | if (err) { |
| 4297 | if (replace && nhn) |
| 4298 | ip6_print_replace_route_err(&rt6_nh_list); |
| 4299 | err_nh = nh; |
| 4300 | goto add_errout; |
| 4301 | } |
| 4302 | |
Nicolas Dichtel | 1a72418 | 2012-11-01 22:58:22 +0000 | [diff] [blame] | 4303 | /* Because each route is added like a single route we remove |
Michal Kubeček | 2759647 | 2015-05-18 20:54:00 +0200 | [diff] [blame] | 4304 | * these flags after the first nexthop: if there is a collision, |
| 4305 | * we have already failed to add the first nexthop: |
| 4306 | * fib6_add_rt2node() has rejected it; when replacing, old |
| 4307 | * nexthops have been replaced by first new, the rest should |
| 4308 | * be added to it. |
Nicolas Dichtel | 1a72418 | 2012-11-01 22:58:22 +0000 | [diff] [blame] | 4309 | */ |
Michal Kubeček | 2759647 | 2015-05-18 20:54:00 +0200 | [diff] [blame] | 4310 | cfg->fc_nlinfo.nlh->nlmsg_flags &= ~(NLM_F_EXCL | |
| 4311 | NLM_F_REPLACE); |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 4312 | nhn++; |
| 4313 | } |
| 4314 | |
David Ahern | 3b1137f | 2017-02-02 12:37:10 -0800 | [diff] [blame] | 4315 | /* success ... tell user about new route */ |
| 4316 | ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags); |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 4317 | goto cleanup; |
| 4318 | |
| 4319 | add_errout: |
David Ahern | 3b1137f | 2017-02-02 12:37:10 -0800 | [diff] [blame] | 4320 | /* send notification for routes that were added so that |
| 4321 | * the delete notifications sent by ip6_route_del are |
| 4322 | * coherent |
| 4323 | */ |
| 4324 | if (rt_notif) |
| 4325 | ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags); |
| 4326 | |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 4327 | /* Delete routes that were already added */ |
| 4328 | list_for_each_entry(nh, &rt6_nh_list, next) { |
| 4329 | if (err_nh == nh) |
| 4330 | break; |
David Ahern | 333c430 | 2017-05-21 10:12:04 -0600 | [diff] [blame] | 4331 | ip6_route_del(&nh->r_cfg, extack); |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 4332 | } |
| 4333 | |
| 4334 | cleanup: |
| 4335 | list_for_each_entry_safe(nh, nh_safe, &rt6_nh_list, next) { |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4336 | if (nh->fib6_info) |
| 4337 | fib6_info_release(nh->fib6_info); |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 4338 | list_del(&nh->next); |
| 4339 | kfree(nh); |
| 4340 | } |
| 4341 | |
| 4342 | return err; |
| 4343 | } |
| 4344 | |
David Ahern | 333c430 | 2017-05-21 10:12:04 -0600 | [diff] [blame] | 4345 | static int ip6_route_multipath_del(struct fib6_config *cfg, |
| 4346 | struct netlink_ext_ack *extack) |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 4347 | { |
| 4348 | struct fib6_config r_cfg; |
| 4349 | struct rtnexthop *rtnh; |
| 4350 | int remaining; |
| 4351 | int attrlen; |
| 4352 | int err = 1, last_err = 0; |
| 4353 | |
| 4354 | remaining = cfg->fc_mp_len; |
| 4355 | rtnh = (struct rtnexthop *)cfg->fc_mp; |
| 4356 | |
| 4357 | /* Parse a Multipath Entry */ |
| 4358 | while (rtnh_ok(rtnh, remaining)) { |
| 4359 | memcpy(&r_cfg, cfg, sizeof(*cfg)); |
| 4360 | if (rtnh->rtnh_ifindex) |
| 4361 | r_cfg.fc_ifindex = rtnh->rtnh_ifindex; |
| 4362 | |
| 4363 | attrlen = rtnh_attrlen(rtnh); |
| 4364 | if (attrlen > 0) { |
| 4365 | struct nlattr *nla, *attrs = rtnh_attrs(rtnh); |
| 4366 | |
| 4367 | nla = nla_find(attrs, attrlen, RTA_GATEWAY); |
| 4368 | if (nla) { |
| 4369 | nla_memcpy(&r_cfg.fc_gateway, nla, 16); |
| 4370 | r_cfg.fc_flags |= RTF_GATEWAY; |
| 4371 | } |
| 4372 | } |
David Ahern | 333c430 | 2017-05-21 10:12:04 -0600 | [diff] [blame] | 4373 | err = ip6_route_del(&r_cfg, extack); |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 4374 | if (err) |
| 4375 | last_err = err; |
| 4376 | |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 4377 | rtnh = rtnh_next(rtnh, &remaining); |
| 4378 | } |
| 4379 | |
| 4380 | return last_err; |
| 4381 | } |
| 4382 | |
David Ahern | c21ef3e | 2017-04-16 09:48:24 -0700 | [diff] [blame] | 4383 | static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh, |
| 4384 | struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4385 | { |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4386 | struct fib6_config cfg; |
| 4387 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4388 | |
David Ahern | 333c430 | 2017-05-21 10:12:04 -0600 | [diff] [blame] | 4389 | err = rtm_to_fib6_config(skb, nlh, &cfg, extack); |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4390 | if (err < 0) |
| 4391 | return err; |
| 4392 | |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 4393 | if (cfg.fc_mp) |
David Ahern | 333c430 | 2017-05-21 10:12:04 -0600 | [diff] [blame] | 4394 | return ip6_route_multipath_del(&cfg, extack); |
David Ahern | 0ae8133 | 2017-02-02 12:37:08 -0800 | [diff] [blame] | 4395 | else { |
| 4396 | cfg.fc_delete_all_nh = 1; |
David Ahern | 333c430 | 2017-05-21 10:12:04 -0600 | [diff] [blame] | 4397 | return ip6_route_del(&cfg, extack); |
David Ahern | 0ae8133 | 2017-02-02 12:37:08 -0800 | [diff] [blame] | 4398 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4399 | } |
| 4400 | |
David Ahern | c21ef3e | 2017-04-16 09:48:24 -0700 | [diff] [blame] | 4401 | static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh, |
| 4402 | struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4403 | { |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4404 | struct fib6_config cfg; |
| 4405 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4406 | |
David Ahern | 333c430 | 2017-05-21 10:12:04 -0600 | [diff] [blame] | 4407 | err = rtm_to_fib6_config(skb, nlh, &cfg, extack); |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4408 | if (err < 0) |
| 4409 | return err; |
| 4410 | |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 4411 | if (cfg.fc_mp) |
David Ahern | 333c430 | 2017-05-21 10:12:04 -0600 | [diff] [blame] | 4412 | return ip6_route_multipath_add(&cfg, extack); |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 4413 | else |
David Ahern | acb54e3 | 2018-04-17 17:33:22 -0700 | [diff] [blame] | 4414 | return ip6_route_add(&cfg, GFP_KERNEL, extack); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4415 | } |
| 4416 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4417 | static size_t rt6_nlmsg_size(struct fib6_info *rt) |
Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 4418 | { |
David Ahern | beb1afac5 | 2017-02-02 12:37:09 -0800 | [diff] [blame] | 4419 | int nexthop_len = 0; |
| 4420 | |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 4421 | if (rt->fib6_nsiblings) { |
David Ahern | beb1afac5 | 2017-02-02 12:37:09 -0800 | [diff] [blame] | 4422 | nexthop_len = nla_total_size(0) /* RTA_MULTIPATH */ |
| 4423 | + NLA_ALIGN(sizeof(struct rtnexthop)) |
| 4424 | + nla_total_size(16) /* RTA_GATEWAY */ |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 4425 | + lwtunnel_get_encap_size(rt->fib6_nh.nh_lwtstate); |
David Ahern | beb1afac5 | 2017-02-02 12:37:09 -0800 | [diff] [blame] | 4426 | |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 4427 | nexthop_len *= rt->fib6_nsiblings; |
David Ahern | beb1afac5 | 2017-02-02 12:37:09 -0800 | [diff] [blame] | 4428 | } |
| 4429 | |
Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 4430 | return NLMSG_ALIGN(sizeof(struct rtmsg)) |
| 4431 | + nla_total_size(16) /* RTA_SRC */ |
| 4432 | + nla_total_size(16) /* RTA_DST */ |
| 4433 | + nla_total_size(16) /* RTA_GATEWAY */ |
| 4434 | + nla_total_size(16) /* RTA_PREFSRC */ |
| 4435 | + nla_total_size(4) /* RTA_TABLE */ |
| 4436 | + nla_total_size(4) /* RTA_IIF */ |
| 4437 | + nla_total_size(4) /* RTA_OIF */ |
| 4438 | + nla_total_size(4) /* RTA_PRIORITY */ |
Noriaki TAKAMIYA | 6a2b9ce | 2007-01-23 22:09:41 -0800 | [diff] [blame] | 4439 | + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */ |
Daniel Borkmann | ea69763 | 2015-01-05 23:57:47 +0100 | [diff] [blame] | 4440 | + nla_total_size(sizeof(struct rta_cacheinfo)) |
Lubomir Rintel | c78ba6d | 2015-03-11 15:39:21 +0100 | [diff] [blame] | 4441 | + nla_total_size(TCP_CA_NAME_MAX) /* RTAX_CC_ALGO */ |
Roopa Prabhu | 19e42e4 | 2015-07-21 10:43:48 +0200 | [diff] [blame] | 4442 | + nla_total_size(1) /* RTA_PREF */ |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 4443 | + lwtunnel_get_encap_size(rt->fib6_nh.nh_lwtstate) |
David Ahern | beb1afac5 | 2017-02-02 12:37:09 -0800 | [diff] [blame] | 4444 | + nexthop_len; |
| 4445 | } |
| 4446 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4447 | static int rt6_nexthop_info(struct sk_buff *skb, struct fib6_info *rt, |
David Ahern | 5be083c | 2017-03-06 15:57:31 -0800 | [diff] [blame] | 4448 | unsigned int *flags, bool skip_oif) |
David Ahern | beb1afac5 | 2017-02-02 12:37:09 -0800 | [diff] [blame] | 4449 | { |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 4450 | if (rt->fib6_nh.nh_flags & RTNH_F_DEAD) |
Ido Schimmel | f9d882e | 2018-01-07 12:45:10 +0200 | [diff] [blame] | 4451 | *flags |= RTNH_F_DEAD; |
| 4452 | |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 4453 | if (rt->fib6_nh.nh_flags & RTNH_F_LINKDOWN) { |
David Ahern | beb1afac5 | 2017-02-02 12:37:09 -0800 | [diff] [blame] | 4454 | *flags |= RTNH_F_LINKDOWN; |
David Ahern | dcd1f57 | 2018-04-18 15:39:05 -0700 | [diff] [blame^] | 4455 | |
| 4456 | rcu_read_lock(); |
| 4457 | if (fib6_ignore_linkdown(rt)) |
David Ahern | beb1afac5 | 2017-02-02 12:37:09 -0800 | [diff] [blame] | 4458 | *flags |= RTNH_F_DEAD; |
David Ahern | dcd1f57 | 2018-04-18 15:39:05 -0700 | [diff] [blame^] | 4459 | rcu_read_unlock(); |
David Ahern | beb1afac5 | 2017-02-02 12:37:09 -0800 | [diff] [blame] | 4460 | } |
| 4461 | |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 4462 | if (rt->fib6_flags & RTF_GATEWAY) { |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 4463 | if (nla_put_in6_addr(skb, RTA_GATEWAY, &rt->fib6_nh.nh_gw) < 0) |
David Ahern | beb1afac5 | 2017-02-02 12:37:09 -0800 | [diff] [blame] | 4464 | goto nla_put_failure; |
| 4465 | } |
| 4466 | |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 4467 | *flags |= (rt->fib6_nh.nh_flags & RTNH_F_ONLINK); |
| 4468 | if (rt->fib6_nh.nh_flags & RTNH_F_OFFLOAD) |
Ido Schimmel | 61e4d01 | 2017-08-03 13:28:20 +0200 | [diff] [blame] | 4469 | *flags |= RTNH_F_OFFLOAD; |
| 4470 | |
David Ahern | 5be083c | 2017-03-06 15:57:31 -0800 | [diff] [blame] | 4471 | /* not needed for multipath encoding b/c it has a rtnexthop struct */ |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 4472 | if (!skip_oif && rt->fib6_nh.nh_dev && |
| 4473 | nla_put_u32(skb, RTA_OIF, rt->fib6_nh.nh_dev->ifindex)) |
David Ahern | beb1afac5 | 2017-02-02 12:37:09 -0800 | [diff] [blame] | 4474 | goto nla_put_failure; |
| 4475 | |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 4476 | if (rt->fib6_nh.nh_lwtstate && |
| 4477 | lwtunnel_fill_encap(skb, rt->fib6_nh.nh_lwtstate) < 0) |
David Ahern | beb1afac5 | 2017-02-02 12:37:09 -0800 | [diff] [blame] | 4478 | goto nla_put_failure; |
| 4479 | |
| 4480 | return 0; |
| 4481 | |
| 4482 | nla_put_failure: |
| 4483 | return -EMSGSIZE; |
| 4484 | } |
| 4485 | |
David Ahern | 5be083c | 2017-03-06 15:57:31 -0800 | [diff] [blame] | 4486 | /* add multipath next hop */ |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4487 | static int rt6_add_nexthop(struct sk_buff *skb, struct fib6_info *rt) |
David Ahern | beb1afac5 | 2017-02-02 12:37:09 -0800 | [diff] [blame] | 4488 | { |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 4489 | const struct net_device *dev = rt->fib6_nh.nh_dev; |
David Ahern | beb1afac5 | 2017-02-02 12:37:09 -0800 | [diff] [blame] | 4490 | struct rtnexthop *rtnh; |
| 4491 | unsigned int flags = 0; |
| 4492 | |
| 4493 | rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh)); |
| 4494 | if (!rtnh) |
| 4495 | goto nla_put_failure; |
| 4496 | |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 4497 | rtnh->rtnh_hops = rt->fib6_nh.nh_weight - 1; |
| 4498 | rtnh->rtnh_ifindex = dev ? dev->ifindex : 0; |
David Ahern | beb1afac5 | 2017-02-02 12:37:09 -0800 | [diff] [blame] | 4499 | |
David Ahern | 5be083c | 2017-03-06 15:57:31 -0800 | [diff] [blame] | 4500 | if (rt6_nexthop_info(skb, rt, &flags, true) < 0) |
David Ahern | beb1afac5 | 2017-02-02 12:37:09 -0800 | [diff] [blame] | 4501 | goto nla_put_failure; |
| 4502 | |
| 4503 | rtnh->rtnh_flags = flags; |
| 4504 | |
| 4505 | /* length of rtnetlink header + attributes */ |
| 4506 | rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *)rtnh; |
| 4507 | |
| 4508 | return 0; |
| 4509 | |
| 4510 | nla_put_failure: |
| 4511 | return -EMSGSIZE; |
Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 4512 | } |
| 4513 | |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 4514 | static int rt6_fill_node(struct net *net, struct sk_buff *skb, |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4515 | struct fib6_info *rt, struct dst_entry *dst, |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 4516 | struct in6_addr *dest, struct in6_addr *src, |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 4517 | int iif, int type, u32 portid, u32 seq, |
David Ahern | f8cfe2c | 2017-01-17 15:51:08 -0800 | [diff] [blame] | 4518 | unsigned int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4519 | { |
| 4520 | struct rtmsg *rtm; |
Thomas Graf | 2d7202b | 2006-08-22 00:01:27 -0700 | [diff] [blame] | 4521 | struct nlmsghdr *nlh; |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 4522 | long expires = 0; |
| 4523 | u32 *pmetrics; |
Patrick McHardy | 9e762a4 | 2006-08-10 23:09:48 -0700 | [diff] [blame] | 4524 | u32 table; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4525 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 4526 | nlh = nlmsg_put(skb, portid, seq, type, sizeof(*rtm), flags); |
David S. Miller | 3830847 | 2011-12-03 18:02:47 -0500 | [diff] [blame] | 4527 | if (!nlh) |
Patrick McHardy | 2693256 | 2007-01-31 23:16:40 -0800 | [diff] [blame] | 4528 | return -EMSGSIZE; |
Thomas Graf | 2d7202b | 2006-08-22 00:01:27 -0700 | [diff] [blame] | 4529 | |
| 4530 | rtm = nlmsg_data(nlh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4531 | rtm->rtm_family = AF_INET6; |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 4532 | rtm->rtm_dst_len = rt->fib6_dst.plen; |
| 4533 | rtm->rtm_src_len = rt->fib6_src.plen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4534 | rtm->rtm_tos = 0; |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 4535 | if (rt->fib6_table) |
| 4536 | table = rt->fib6_table->tb6_id; |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 4537 | else |
Patrick McHardy | 9e762a4 | 2006-08-10 23:09:48 -0700 | [diff] [blame] | 4538 | table = RT6_TABLE_UNSPEC; |
| 4539 | rtm->rtm_table = table; |
David S. Miller | c78679e | 2012-04-01 20:27:33 -0400 | [diff] [blame] | 4540 | if (nla_put_u32(skb, RTA_TABLE, table)) |
| 4541 | goto nla_put_failure; |
David Ahern | e8478e8 | 2018-04-17 17:33:13 -0700 | [diff] [blame] | 4542 | |
| 4543 | rtm->rtm_type = rt->fib6_type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4544 | rtm->rtm_flags = 0; |
| 4545 | rtm->rtm_scope = RT_SCOPE_UNIVERSE; |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 4546 | rtm->rtm_protocol = rt->fib6_protocol; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4547 | |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 4548 | if (rt->fib6_flags & RTF_CACHE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4549 | rtm->rtm_flags |= RTM_F_CLONED; |
| 4550 | |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 4551 | if (dest) { |
| 4552 | if (nla_put_in6_addr(skb, RTA_DST, dest)) |
David S. Miller | c78679e | 2012-04-01 20:27:33 -0400 | [diff] [blame] | 4553 | goto nla_put_failure; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 4554 | rtm->rtm_dst_len = 128; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4555 | } else if (rtm->rtm_dst_len) |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 4556 | if (nla_put_in6_addr(skb, RTA_DST, &rt->fib6_dst.addr)) |
David S. Miller | c78679e | 2012-04-01 20:27:33 -0400 | [diff] [blame] | 4557 | goto nla_put_failure; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4558 | #ifdef CONFIG_IPV6_SUBTREES |
| 4559 | if (src) { |
Jiri Benc | 930345e | 2015-03-29 16:59:25 +0200 | [diff] [blame] | 4560 | if (nla_put_in6_addr(skb, RTA_SRC, src)) |
David S. Miller | c78679e | 2012-04-01 20:27:33 -0400 | [diff] [blame] | 4561 | goto nla_put_failure; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 4562 | rtm->rtm_src_len = 128; |
David S. Miller | c78679e | 2012-04-01 20:27:33 -0400 | [diff] [blame] | 4563 | } else if (rtm->rtm_src_len && |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 4564 | nla_put_in6_addr(skb, RTA_SRC, &rt->fib6_src.addr)) |
David S. Miller | c78679e | 2012-04-01 20:27:33 -0400 | [diff] [blame] | 4565 | goto nla_put_failure; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4566 | #endif |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 4567 | if (iif) { |
| 4568 | #ifdef CONFIG_IPV6_MROUTE |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 4569 | if (ipv6_addr_is_multicast(&rt->fib6_dst.addr)) { |
David Ahern | fd61c6b | 2017-01-17 15:51:07 -0800 | [diff] [blame] | 4570 | int err = ip6mr_get_route(net, skb, rtm, portid); |
Nikolay Aleksandrov | 2cf7507 | 2016-09-25 23:08:31 +0200 | [diff] [blame] | 4571 | |
David Ahern | fd61c6b | 2017-01-17 15:51:07 -0800 | [diff] [blame] | 4572 | if (err == 0) |
| 4573 | return 0; |
| 4574 | if (err < 0) |
| 4575 | goto nla_put_failure; |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 4576 | } else |
| 4577 | #endif |
David S. Miller | c78679e | 2012-04-01 20:27:33 -0400 | [diff] [blame] | 4578 | if (nla_put_u32(skb, RTA_IIF, iif)) |
| 4579 | goto nla_put_failure; |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 4580 | } else if (dest) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4581 | struct in6_addr saddr_buf; |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 4582 | if (ip6_route_get_saddr(net, rt, dest, 0, &saddr_buf) == 0 && |
Jiri Benc | 930345e | 2015-03-29 16:59:25 +0200 | [diff] [blame] | 4583 | nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf)) |
David S. Miller | c78679e | 2012-04-01 20:27:33 -0400 | [diff] [blame] | 4584 | goto nla_put_failure; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4585 | } |
Thomas Graf | 2d7202b | 2006-08-22 00:01:27 -0700 | [diff] [blame] | 4586 | |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 4587 | if (rt->fib6_prefsrc.plen) { |
Daniel Walter | c3968a8 | 2011-04-13 21:10:57 +0000 | [diff] [blame] | 4588 | struct in6_addr saddr_buf; |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 4589 | saddr_buf = rt->fib6_prefsrc.addr; |
Jiri Benc | 930345e | 2015-03-29 16:59:25 +0200 | [diff] [blame] | 4590 | if (nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf)) |
David S. Miller | c78679e | 2012-04-01 20:27:33 -0400 | [diff] [blame] | 4591 | goto nla_put_failure; |
Daniel Walter | c3968a8 | 2011-04-13 21:10:57 +0000 | [diff] [blame] | 4592 | } |
| 4593 | |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 4594 | pmetrics = dst ? dst_metrics_ptr(dst) : rt->fib6_metrics->metrics; |
| 4595 | if (rtnetlink_put_metrics(skb, pmetrics) < 0) |
Thomas Graf | 2d7202b | 2006-08-22 00:01:27 -0700 | [diff] [blame] | 4596 | goto nla_put_failure; |
| 4597 | |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 4598 | if (nla_put_u32(skb, RTA_PRIORITY, rt->fib6_metric)) |
David S. Miller | c78679e | 2012-04-01 20:27:33 -0400 | [diff] [blame] | 4599 | goto nla_put_failure; |
Li Wei | 8253947 | 2012-07-29 16:01:30 +0000 | [diff] [blame] | 4600 | |
David Ahern | beb1afac5 | 2017-02-02 12:37:09 -0800 | [diff] [blame] | 4601 | /* For multipath routes, walk the siblings list and add |
| 4602 | * each as a nexthop within RTA_MULTIPATH. |
| 4603 | */ |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 4604 | if (rt->fib6_nsiblings) { |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4605 | struct fib6_info *sibling, *next_sibling; |
David Ahern | beb1afac5 | 2017-02-02 12:37:09 -0800 | [diff] [blame] | 4606 | struct nlattr *mp; |
| 4607 | |
| 4608 | mp = nla_nest_start(skb, RTA_MULTIPATH); |
| 4609 | if (!mp) |
| 4610 | goto nla_put_failure; |
| 4611 | |
| 4612 | if (rt6_add_nexthop(skb, rt) < 0) |
| 4613 | goto nla_put_failure; |
| 4614 | |
| 4615 | list_for_each_entry_safe(sibling, next_sibling, |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 4616 | &rt->fib6_siblings, fib6_siblings) { |
David Ahern | beb1afac5 | 2017-02-02 12:37:09 -0800 | [diff] [blame] | 4617 | if (rt6_add_nexthop(skb, sibling) < 0) |
| 4618 | goto nla_put_failure; |
| 4619 | } |
| 4620 | |
| 4621 | nla_nest_end(skb, mp); |
| 4622 | } else { |
David Ahern | 5be083c | 2017-03-06 15:57:31 -0800 | [diff] [blame] | 4623 | if (rt6_nexthop_info(skb, rt, &rtm->rtm_flags, false) < 0) |
David Ahern | beb1afac5 | 2017-02-02 12:37:09 -0800 | [diff] [blame] | 4624 | goto nla_put_failure; |
| 4625 | } |
| 4626 | |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 4627 | if (rt->fib6_flags & RTF_EXPIRES) { |
David Ahern | 1489568 | 2018-04-17 17:33:17 -0700 | [diff] [blame] | 4628 | expires = dst ? dst->expires : rt->expires; |
| 4629 | expires -= jiffies; |
| 4630 | } |
YOSHIFUJI Hideaki | 69cdf8f | 2008-05-19 16:55:13 -0700 | [diff] [blame] | 4631 | |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 4632 | 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] | 4633 | goto nla_put_failure; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4634 | |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 4635 | if (nla_put_u8(skb, RTA_PREF, IPV6_EXTRACT_PREF(rt->fib6_flags))) |
Lubomir Rintel | c78ba6d | 2015-03-11 15:39:21 +0100 | [diff] [blame] | 4636 | goto nla_put_failure; |
| 4637 | |
Roopa Prabhu | 19e42e4 | 2015-07-21 10:43:48 +0200 | [diff] [blame] | 4638 | |
Johannes Berg | 053c095 | 2015-01-16 22:09:00 +0100 | [diff] [blame] | 4639 | nlmsg_end(skb, nlh); |
| 4640 | return 0; |
Thomas Graf | 2d7202b | 2006-08-22 00:01:27 -0700 | [diff] [blame] | 4641 | |
| 4642 | nla_put_failure: |
Patrick McHardy | 2693256 | 2007-01-31 23:16:40 -0800 | [diff] [blame] | 4643 | nlmsg_cancel(skb, nlh); |
| 4644 | return -EMSGSIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4645 | } |
| 4646 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4647 | int rt6_dump_route(struct fib6_info *rt, void *p_arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4648 | { |
| 4649 | struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg; |
David Ahern | 1f17e2f | 2017-01-26 13:54:08 -0800 | [diff] [blame] | 4650 | struct net *net = arg->net; |
| 4651 | |
David Ahern | 421842e | 2018-04-17 17:33:18 -0700 | [diff] [blame] | 4652 | if (rt == net->ipv6.fib6_null_entry) |
David Ahern | 1f17e2f | 2017-01-26 13:54:08 -0800 | [diff] [blame] | 4653 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4654 | |
Thomas Graf | 2d7202b | 2006-08-22 00:01:27 -0700 | [diff] [blame] | 4655 | if (nlmsg_len(arg->cb->nlh) >= sizeof(struct rtmsg)) { |
| 4656 | struct rtmsg *rtm = nlmsg_data(arg->cb->nlh); |
David Ahern | f8cfe2c | 2017-01-17 15:51:08 -0800 | [diff] [blame] | 4657 | |
| 4658 | /* user wants prefix routes only */ |
| 4659 | if (rtm->rtm_flags & RTM_F_PREFIX && |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 4660 | !(rt->fib6_flags & RTF_PREFIX_RT)) { |
David Ahern | f8cfe2c | 2017-01-17 15:51:08 -0800 | [diff] [blame] | 4661 | /* success since this is not a prefix route */ |
| 4662 | return 1; |
| 4663 | } |
| 4664 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4665 | |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 4666 | return rt6_fill_node(net, arg->skb, rt, NULL, NULL, NULL, 0, |
| 4667 | RTM_NEWROUTE, NETLINK_CB(arg->cb->skb).portid, |
| 4668 | arg->cb->nlh->nlmsg_seq, NLM_F_MULTI); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4669 | } |
| 4670 | |
David Ahern | c21ef3e | 2017-04-16 09:48:24 -0700 | [diff] [blame] | 4671 | static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh, |
| 4672 | struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4673 | { |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 4674 | struct net *net = sock_net(in_skb->sk); |
Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 4675 | struct nlattr *tb[RTA_MAX+1]; |
Roopa Prabhu | 18c3a61 | 2017-05-25 10:42:40 -0700 | [diff] [blame] | 4676 | int err, iif = 0, oif = 0; |
| 4677 | struct dst_entry *dst; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4678 | struct rt6_info *rt; |
Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 4679 | struct sk_buff *skb; |
| 4680 | struct rtmsg *rtm; |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 4681 | struct flowi6 fl6; |
Roopa Prabhu | 18c3a61 | 2017-05-25 10:42:40 -0700 | [diff] [blame] | 4682 | bool fibmatch; |
Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 4683 | |
Johannes Berg | fceb643 | 2017-04-12 14:34:07 +0200 | [diff] [blame] | 4684 | err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy, |
David Ahern | c21ef3e | 2017-04-16 09:48:24 -0700 | [diff] [blame] | 4685 | extack); |
Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 4686 | if (err < 0) |
| 4687 | goto errout; |
| 4688 | |
| 4689 | err = -EINVAL; |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 4690 | memset(&fl6, 0, sizeof(fl6)); |
Hannes Frederic Sowa | 38b7097 | 2016-06-11 20:08:19 +0200 | [diff] [blame] | 4691 | rtm = nlmsg_data(nlh); |
| 4692 | fl6.flowlabel = ip6_make_flowinfo(rtm->rtm_tos, 0); |
Roopa Prabhu | 18c3a61 | 2017-05-25 10:42:40 -0700 | [diff] [blame] | 4693 | fibmatch = !!(rtm->rtm_flags & RTM_F_FIB_MATCH); |
Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 4694 | |
| 4695 | if (tb[RTA_SRC]) { |
| 4696 | if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr)) |
| 4697 | goto errout; |
| 4698 | |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 4699 | fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]); |
Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 4700 | } |
| 4701 | |
| 4702 | if (tb[RTA_DST]) { |
| 4703 | if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr)) |
| 4704 | goto errout; |
| 4705 | |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 4706 | fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]); |
Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 4707 | } |
| 4708 | |
| 4709 | if (tb[RTA_IIF]) |
| 4710 | iif = nla_get_u32(tb[RTA_IIF]); |
| 4711 | |
| 4712 | if (tb[RTA_OIF]) |
Shmulik Ladkani | 72331bc | 2012-04-01 04:03:45 +0000 | [diff] [blame] | 4713 | oif = nla_get_u32(tb[RTA_OIF]); |
Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 4714 | |
Lorenzo Colitti | 2e47b29 | 2014-05-15 16:38:41 -0700 | [diff] [blame] | 4715 | if (tb[RTA_MARK]) |
| 4716 | fl6.flowi6_mark = nla_get_u32(tb[RTA_MARK]); |
| 4717 | |
Lorenzo Colitti | 622ec2c | 2016-11-04 02:23:42 +0900 | [diff] [blame] | 4718 | if (tb[RTA_UID]) |
| 4719 | fl6.flowi6_uid = make_kuid(current_user_ns(), |
| 4720 | nla_get_u32(tb[RTA_UID])); |
| 4721 | else |
| 4722 | fl6.flowi6_uid = iif ? INVALID_UID : current_uid(); |
| 4723 | |
Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 4724 | if (iif) { |
| 4725 | struct net_device *dev; |
Shmulik Ladkani | 72331bc | 2012-04-01 04:03:45 +0000 | [diff] [blame] | 4726 | int flags = 0; |
| 4727 | |
Florian Westphal | 121622d | 2017-08-15 16:34:42 +0200 | [diff] [blame] | 4728 | rcu_read_lock(); |
| 4729 | |
| 4730 | dev = dev_get_by_index_rcu(net, iif); |
Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 4731 | if (!dev) { |
Florian Westphal | 121622d | 2017-08-15 16:34:42 +0200 | [diff] [blame] | 4732 | rcu_read_unlock(); |
Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 4733 | err = -ENODEV; |
| 4734 | goto errout; |
| 4735 | } |
Shmulik Ladkani | 72331bc | 2012-04-01 04:03:45 +0000 | [diff] [blame] | 4736 | |
| 4737 | fl6.flowi6_iif = iif; |
| 4738 | |
| 4739 | if (!ipv6_addr_any(&fl6.saddr)) |
| 4740 | flags |= RT6_LOOKUP_F_HAS_SADDR; |
| 4741 | |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 4742 | dst = ip6_route_input_lookup(net, dev, &fl6, NULL, flags); |
Florian Westphal | 121622d | 2017-08-15 16:34:42 +0200 | [diff] [blame] | 4743 | |
| 4744 | rcu_read_unlock(); |
Shmulik Ladkani | 72331bc | 2012-04-01 04:03:45 +0000 | [diff] [blame] | 4745 | } else { |
| 4746 | fl6.flowi6_oif = oif; |
| 4747 | |
Ido Schimmel | 58acfd7 | 2017-12-20 12:28:25 +0200 | [diff] [blame] | 4748 | dst = ip6_route_output(net, NULL, &fl6); |
Roopa Prabhu | 18c3a61 | 2017-05-25 10:42:40 -0700 | [diff] [blame] | 4749 | } |
| 4750 | |
Roopa Prabhu | 18c3a61 | 2017-05-25 10:42:40 -0700 | [diff] [blame] | 4751 | |
| 4752 | rt = container_of(dst, struct rt6_info, dst); |
| 4753 | if (rt->dst.error) { |
| 4754 | err = rt->dst.error; |
| 4755 | ip6_rt_put(rt); |
| 4756 | goto errout; |
Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 4757 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4758 | |
WANG Cong | 9d6acb3 | 2017-03-01 20:48:39 -0800 | [diff] [blame] | 4759 | if (rt == net->ipv6.ip6_null_entry) { |
| 4760 | err = rt->dst.error; |
| 4761 | ip6_rt_put(rt); |
| 4762 | goto errout; |
| 4763 | } |
| 4764 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4765 | skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); |
David S. Miller | 3830847 | 2011-12-03 18:02:47 -0500 | [diff] [blame] | 4766 | if (!skb) { |
Amerigo Wang | 94e187c | 2012-10-29 00:13:19 +0000 | [diff] [blame] | 4767 | ip6_rt_put(rt); |
Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 4768 | err = -ENOBUFS; |
| 4769 | goto errout; |
| 4770 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4771 | |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 4772 | skb_dst_set(skb, &rt->dst); |
Roopa Prabhu | 18c3a61 | 2017-05-25 10:42:40 -0700 | [diff] [blame] | 4773 | if (fibmatch) |
David Ahern | 93531c6 | 2018-04-17 17:33:25 -0700 | [diff] [blame] | 4774 | err = rt6_fill_node(net, skb, rt->from, NULL, NULL, NULL, iif, |
Roopa Prabhu | 18c3a61 | 2017-05-25 10:42:40 -0700 | [diff] [blame] | 4775 | RTM_NEWROUTE, NETLINK_CB(in_skb).portid, |
| 4776 | nlh->nlmsg_seq, 0); |
| 4777 | else |
David Ahern | 93531c6 | 2018-04-17 17:33:25 -0700 | [diff] [blame] | 4778 | err = rt6_fill_node(net, skb, rt->from, dst, |
| 4779 | &fl6.daddr, &fl6.saddr, iif, RTM_NEWROUTE, |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 4780 | NETLINK_CB(in_skb).portid, nlh->nlmsg_seq, |
| 4781 | 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4782 | if (err < 0) { |
Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 4783 | kfree_skb(skb); |
| 4784 | goto errout; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4785 | } |
| 4786 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 4787 | err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid); |
Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 4788 | errout: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4789 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4790 | } |
| 4791 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 4792 | 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] | 4793 | unsigned int nlm_flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4794 | { |
| 4795 | struct sk_buff *skb; |
Daniel Lezcano | 5578689 | 2008-03-04 13:47:47 -0800 | [diff] [blame] | 4796 | struct net *net = info->nl_net; |
Denis V. Lunev | 528c4ce | 2007-12-13 09:45:12 -0800 | [diff] [blame] | 4797 | u32 seq; |
| 4798 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4799 | |
Denis V. Lunev | 528c4ce | 2007-12-13 09:45:12 -0800 | [diff] [blame] | 4800 | err = -ENOBUFS; |
David S. Miller | 3830847 | 2011-12-03 18:02:47 -0500 | [diff] [blame] | 4801 | seq = info->nlh ? info->nlh->nlmsg_seq : 0; |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4802 | |
Roopa Prabhu | 19e42e4 | 2015-07-21 10:43:48 +0200 | [diff] [blame] | 4803 | skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any()); |
David S. Miller | 3830847 | 2011-12-03 18:02:47 -0500 | [diff] [blame] | 4804 | if (!skb) |
Thomas Graf | 21713eb | 2006-08-15 00:35:24 -0700 | [diff] [blame] | 4805 | goto errout; |
| 4806 | |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 4807 | err = rt6_fill_node(net, skb, rt, NULL, NULL, NULL, 0, |
| 4808 | event, info->portid, seq, nlm_flags); |
Patrick McHardy | 2693256 | 2007-01-31 23:16:40 -0800 | [diff] [blame] | 4809 | if (err < 0) { |
| 4810 | /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */ |
| 4811 | WARN_ON(err == -EMSGSIZE); |
| 4812 | kfree_skb(skb); |
| 4813 | goto errout; |
| 4814 | } |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 4815 | rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE, |
Pablo Neira Ayuso | 1ce85fe | 2009-02-24 23:18:28 -0800 | [diff] [blame] | 4816 | info->nlh, gfp_any()); |
| 4817 | return; |
Thomas Graf | 21713eb | 2006-08-15 00:35:24 -0700 | [diff] [blame] | 4818 | errout: |
| 4819 | if (err < 0) |
Daniel Lezcano | 5578689 | 2008-03-04 13:47:47 -0800 | [diff] [blame] | 4820 | rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4821 | } |
| 4822 | |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 4823 | static int ip6_route_dev_notify(struct notifier_block *this, |
Jiri Pirko | 351638e | 2013-05-28 01:30:21 +0000 | [diff] [blame] | 4824 | unsigned long event, void *ptr) |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 4825 | { |
Jiri Pirko | 351638e | 2013-05-28 01:30:21 +0000 | [diff] [blame] | 4826 | struct net_device *dev = netdev_notifier_info_to_dev(ptr); |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 4827 | struct net *net = dev_net(dev); |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 4828 | |
WANG Cong | 242d3a4 | 2017-05-08 10:12:13 -0700 | [diff] [blame] | 4829 | if (!(dev->flags & IFF_LOOPBACK)) |
| 4830 | return NOTIFY_OK; |
| 4831 | |
| 4832 | if (event == NETDEV_REGISTER) { |
David Ahern | 421842e | 2018-04-17 17:33:18 -0700 | [diff] [blame] | 4833 | net->ipv6.fib6_null_entry->fib6_nh.nh_dev = dev; |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 4834 | net->ipv6.ip6_null_entry->dst.dev = dev; |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 4835 | net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev); |
| 4836 | #ifdef CONFIG_IPV6_MULTIPLE_TABLES |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 4837 | net->ipv6.ip6_prohibit_entry->dst.dev = dev; |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 4838 | net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev); |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 4839 | net->ipv6.ip6_blk_hole_entry->dst.dev = dev; |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 4840 | net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev); |
| 4841 | #endif |
WANG Cong | 76da070 | 2017-06-20 11:42:27 -0700 | [diff] [blame] | 4842 | } else if (event == NETDEV_UNREGISTER && |
| 4843 | dev->reg_state != NETREG_UNREGISTERED) { |
| 4844 | /* NETDEV_UNREGISTER could be fired for multiple times by |
| 4845 | * netdev_wait_allrefs(). Make sure we only call this once. |
| 4846 | */ |
Eric Dumazet | 12d94a8 | 2017-08-15 04:09:51 -0700 | [diff] [blame] | 4847 | in6_dev_put_clear(&net->ipv6.ip6_null_entry->rt6i_idev); |
WANG Cong | 242d3a4 | 2017-05-08 10:12:13 -0700 | [diff] [blame] | 4848 | #ifdef CONFIG_IPV6_MULTIPLE_TABLES |
Eric Dumazet | 12d94a8 | 2017-08-15 04:09:51 -0700 | [diff] [blame] | 4849 | in6_dev_put_clear(&net->ipv6.ip6_prohibit_entry->rt6i_idev); |
| 4850 | in6_dev_put_clear(&net->ipv6.ip6_blk_hole_entry->rt6i_idev); |
WANG Cong | 242d3a4 | 2017-05-08 10:12:13 -0700 | [diff] [blame] | 4851 | #endif |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 4852 | } |
| 4853 | |
| 4854 | return NOTIFY_OK; |
| 4855 | } |
| 4856 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4857 | /* |
| 4858 | * /proc |
| 4859 | */ |
| 4860 | |
| 4861 | #ifdef CONFIG_PROC_FS |
| 4862 | |
Alexey Dobriyan | 33120b3 | 2007-11-06 05:27:11 -0800 | [diff] [blame] | 4863 | static const struct file_operations ipv6_route_proc_fops = { |
Alexey Dobriyan | 33120b3 | 2007-11-06 05:27:11 -0800 | [diff] [blame] | 4864 | .open = ipv6_route_open, |
| 4865 | .read = seq_read, |
| 4866 | .llseek = seq_lseek, |
Hannes Frederic Sowa | 8d2ca1d | 2013-09-21 16:55:59 +0200 | [diff] [blame] | 4867 | .release = seq_release_net, |
Alexey Dobriyan | 33120b3 | 2007-11-06 05:27:11 -0800 | [diff] [blame] | 4868 | }; |
| 4869 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4870 | static int rt6_stats_seq_show(struct seq_file *seq, void *v) |
| 4871 | { |
Daniel Lezcano | 69ddb80 | 2008-03-04 13:46:23 -0800 | [diff] [blame] | 4872 | struct net *net = (struct net *)seq->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4873 | seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n", |
Daniel Lezcano | 69ddb80 | 2008-03-04 13:46:23 -0800 | [diff] [blame] | 4874 | net->ipv6.rt6_stats->fib_nodes, |
| 4875 | net->ipv6.rt6_stats->fib_route_nodes, |
Wei Wang | 81eb844 | 2017-10-06 12:06:11 -0700 | [diff] [blame] | 4876 | atomic_read(&net->ipv6.rt6_stats->fib_rt_alloc), |
Daniel Lezcano | 69ddb80 | 2008-03-04 13:46:23 -0800 | [diff] [blame] | 4877 | net->ipv6.rt6_stats->fib_rt_entries, |
| 4878 | net->ipv6.rt6_stats->fib_rt_cache, |
Eric Dumazet | fc66f95 | 2010-10-08 06:37:34 +0000 | [diff] [blame] | 4879 | dst_entries_get_slow(&net->ipv6.ip6_dst_ops), |
Daniel Lezcano | 69ddb80 | 2008-03-04 13:46:23 -0800 | [diff] [blame] | 4880 | net->ipv6.rt6_stats->fib_discarded_routes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4881 | |
| 4882 | return 0; |
| 4883 | } |
| 4884 | |
| 4885 | static int rt6_stats_seq_open(struct inode *inode, struct file *file) |
| 4886 | { |
Pavel Emelyanov | de05c55 | 2008-07-18 04:07:21 -0700 | [diff] [blame] | 4887 | return single_open_net(inode, file, rt6_stats_seq_show); |
Daniel Lezcano | 69ddb80 | 2008-03-04 13:46:23 -0800 | [diff] [blame] | 4888 | } |
| 4889 | |
Arjan van de Ven | 9a32144 | 2007-02-12 00:55:35 -0800 | [diff] [blame] | 4890 | static const struct file_operations rt6_stats_seq_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4891 | .open = rt6_stats_seq_open, |
| 4892 | .read = seq_read, |
| 4893 | .llseek = seq_lseek, |
Pavel Emelyanov | b6fcbdb | 2008-07-18 04:07:44 -0700 | [diff] [blame] | 4894 | .release = single_release_net, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4895 | }; |
| 4896 | #endif /* CONFIG_PROC_FS */ |
| 4897 | |
| 4898 | #ifdef CONFIG_SYSCTL |
| 4899 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4900 | static |
Joe Perches | fe2c633 | 2013-06-11 23:04:25 -0700 | [diff] [blame] | 4901 | int ipv6_sysctl_rtcache_flush(struct ctl_table *ctl, int write, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4902 | void __user *buffer, size_t *lenp, loff_t *ppos) |
| 4903 | { |
Lucian Adrian Grijincu | c486da3 | 2011-02-24 19:48:03 +0000 | [diff] [blame] | 4904 | struct net *net; |
| 4905 | int delay; |
| 4906 | if (!write) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4907 | return -EINVAL; |
Lucian Adrian Grijincu | c486da3 | 2011-02-24 19:48:03 +0000 | [diff] [blame] | 4908 | |
| 4909 | net = (struct net *)ctl->extra1; |
| 4910 | delay = net->ipv6.sysctl.flush_delay; |
| 4911 | proc_dointvec(ctl, write, buffer, lenp, ppos); |
Michal Kubeček | 2ac3ac8 | 2013-08-01 10:04:14 +0200 | [diff] [blame] | 4912 | 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] | 4913 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4914 | } |
| 4915 | |
Joe Perches | fe2c633 | 2013-06-11 23:04:25 -0700 | [diff] [blame] | 4916 | struct ctl_table ipv6_route_table_template[] = { |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 4917 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4918 | .procname = "flush", |
Daniel Lezcano | 4990509 | 2008-01-10 03:01:01 -0800 | [diff] [blame] | 4919 | .data = &init_net.ipv6.sysctl.flush_delay, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4920 | .maxlen = sizeof(int), |
Dave Jones | 89c8b3a1 | 2005-04-28 12:11:49 -0700 | [diff] [blame] | 4921 | .mode = 0200, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 4922 | .proc_handler = ipv6_sysctl_rtcache_flush |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4923 | }, |
| 4924 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4925 | .procname = "gc_thresh", |
Daniel Lezcano | 9a7ec3a | 2008-03-04 13:48:53 -0800 | [diff] [blame] | 4926 | .data = &ip6_dst_ops_template.gc_thresh, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4927 | .maxlen = sizeof(int), |
| 4928 | .mode = 0644, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 4929 | .proc_handler = proc_dointvec, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4930 | }, |
| 4931 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4932 | .procname = "max_size", |
Daniel Lezcano | 4990509 | 2008-01-10 03:01:01 -0800 | [diff] [blame] | 4933 | .data = &init_net.ipv6.sysctl.ip6_rt_max_size, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4934 | .maxlen = sizeof(int), |
| 4935 | .mode = 0644, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 4936 | .proc_handler = proc_dointvec, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4937 | }, |
| 4938 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4939 | .procname = "gc_min_interval", |
Daniel Lezcano | 4990509 | 2008-01-10 03:01:01 -0800 | [diff] [blame] | 4940 | .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4941 | .maxlen = sizeof(int), |
| 4942 | .mode = 0644, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 4943 | .proc_handler = proc_dointvec_jiffies, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4944 | }, |
| 4945 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4946 | .procname = "gc_timeout", |
Daniel Lezcano | 4990509 | 2008-01-10 03:01:01 -0800 | [diff] [blame] | 4947 | .data = &init_net.ipv6.sysctl.ip6_rt_gc_timeout, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4948 | .maxlen = sizeof(int), |
| 4949 | .mode = 0644, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 4950 | .proc_handler = proc_dointvec_jiffies, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4951 | }, |
| 4952 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4953 | .procname = "gc_interval", |
Daniel Lezcano | 4990509 | 2008-01-10 03:01:01 -0800 | [diff] [blame] | 4954 | .data = &init_net.ipv6.sysctl.ip6_rt_gc_interval, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4955 | .maxlen = sizeof(int), |
| 4956 | .mode = 0644, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 4957 | .proc_handler = proc_dointvec_jiffies, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4958 | }, |
| 4959 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4960 | .procname = "gc_elasticity", |
Daniel Lezcano | 4990509 | 2008-01-10 03:01:01 -0800 | [diff] [blame] | 4961 | .data = &init_net.ipv6.sysctl.ip6_rt_gc_elasticity, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4962 | .maxlen = sizeof(int), |
| 4963 | .mode = 0644, |
Min Zhang | f3d3f61 | 2010-08-14 22:42:51 -0700 | [diff] [blame] | 4964 | .proc_handler = proc_dointvec, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4965 | }, |
| 4966 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4967 | .procname = "mtu_expires", |
Daniel Lezcano | 4990509 | 2008-01-10 03:01:01 -0800 | [diff] [blame] | 4968 | .data = &init_net.ipv6.sysctl.ip6_rt_mtu_expires, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4969 | .maxlen = sizeof(int), |
| 4970 | .mode = 0644, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 4971 | .proc_handler = proc_dointvec_jiffies, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4972 | }, |
| 4973 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4974 | .procname = "min_adv_mss", |
Daniel Lezcano | 4990509 | 2008-01-10 03:01:01 -0800 | [diff] [blame] | 4975 | .data = &init_net.ipv6.sysctl.ip6_rt_min_advmss, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4976 | .maxlen = sizeof(int), |
| 4977 | .mode = 0644, |
Min Zhang | f3d3f61 | 2010-08-14 22:42:51 -0700 | [diff] [blame] | 4978 | .proc_handler = proc_dointvec, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4979 | }, |
| 4980 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4981 | .procname = "gc_min_interval_ms", |
Daniel Lezcano | 4990509 | 2008-01-10 03:01:01 -0800 | [diff] [blame] | 4982 | .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4983 | .maxlen = sizeof(int), |
| 4984 | .mode = 0644, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 4985 | .proc_handler = proc_dointvec_ms_jiffies, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4986 | }, |
Eric W. Biederman | f8572d8 | 2009-11-05 13:32:03 -0800 | [diff] [blame] | 4987 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4988 | }; |
| 4989 | |
Alexey Dobriyan | 2c8c1e7 | 2010-01-17 03:35:32 +0000 | [diff] [blame] | 4990 | struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net) |
Daniel Lezcano | 760f2d0 | 2008-01-10 02:53:43 -0800 | [diff] [blame] | 4991 | { |
| 4992 | struct ctl_table *table; |
| 4993 | |
| 4994 | table = kmemdup(ipv6_route_table_template, |
| 4995 | sizeof(ipv6_route_table_template), |
| 4996 | GFP_KERNEL); |
YOSHIFUJI Hideaki | 5ee0910 | 2008-02-28 00:24:28 +0900 | [diff] [blame] | 4997 | |
| 4998 | if (table) { |
| 4999 | table[0].data = &net->ipv6.sysctl.flush_delay; |
Lucian Adrian Grijincu | c486da3 | 2011-02-24 19:48:03 +0000 | [diff] [blame] | 5000 | table[0].extra1 = net; |
Alexey Dobriyan | 86393e5 | 2009-08-29 01:34:49 +0000 | [diff] [blame] | 5001 | table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh; |
YOSHIFUJI Hideaki | 5ee0910 | 2008-02-28 00:24:28 +0900 | [diff] [blame] | 5002 | table[2].data = &net->ipv6.sysctl.ip6_rt_max_size; |
| 5003 | table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval; |
| 5004 | table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout; |
| 5005 | table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval; |
| 5006 | table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity; |
| 5007 | table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires; |
| 5008 | table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss; |
Alexey Dobriyan | 9c69fab | 2009-12-18 20:11:03 -0800 | [diff] [blame] | 5009 | table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval; |
Eric W. Biederman | 464dc80 | 2012-11-16 03:02:59 +0000 | [diff] [blame] | 5010 | |
| 5011 | /* Don't export sysctls to unprivileged users */ |
| 5012 | if (net->user_ns != &init_user_ns) |
| 5013 | table[0].procname = NULL; |
YOSHIFUJI Hideaki | 5ee0910 | 2008-02-28 00:24:28 +0900 | [diff] [blame] | 5014 | } |
| 5015 | |
Daniel Lezcano | 760f2d0 | 2008-01-10 02:53:43 -0800 | [diff] [blame] | 5016 | return table; |
| 5017 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5018 | #endif |
| 5019 | |
Alexey Dobriyan | 2c8c1e7 | 2010-01-17 03:35:32 +0000 | [diff] [blame] | 5020 | static int __net_init ip6_route_net_init(struct net *net) |
Daniel Lezcano | cdb1876 | 2008-03-04 13:45:33 -0800 | [diff] [blame] | 5021 | { |
Pavel Emelyanov | 633d424b | 2008-04-21 14:25:23 -0700 | [diff] [blame] | 5022 | int ret = -ENOMEM; |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 5023 | |
Alexey Dobriyan | 86393e5 | 2009-08-29 01:34:49 +0000 | [diff] [blame] | 5024 | memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template, |
| 5025 | sizeof(net->ipv6.ip6_dst_ops)); |
Benjamin Thery | f2fc6a5 | 2008-03-04 13:49:23 -0800 | [diff] [blame] | 5026 | |
Eric Dumazet | fc66f95 | 2010-10-08 06:37:34 +0000 | [diff] [blame] | 5027 | if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0) |
| 5028 | goto out_ip6_dst_ops; |
| 5029 | |
David Ahern | 421842e | 2018-04-17 17:33:18 -0700 | [diff] [blame] | 5030 | net->ipv6.fib6_null_entry = kmemdup(&fib6_null_entry_template, |
| 5031 | sizeof(*net->ipv6.fib6_null_entry), |
| 5032 | GFP_KERNEL); |
| 5033 | if (!net->ipv6.fib6_null_entry) |
| 5034 | goto out_ip6_dst_entries; |
| 5035 | |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 5036 | net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template, |
| 5037 | sizeof(*net->ipv6.ip6_null_entry), |
| 5038 | GFP_KERNEL); |
| 5039 | if (!net->ipv6.ip6_null_entry) |
David Ahern | 421842e | 2018-04-17 17:33:18 -0700 | [diff] [blame] | 5040 | goto out_fib6_null_entry; |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 5041 | 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] | 5042 | dst_init_metrics(&net->ipv6.ip6_null_entry->dst, |
| 5043 | ip6_template_metrics, true); |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 5044 | |
| 5045 | #ifdef CONFIG_IPV6_MULTIPLE_TABLES |
Vincent Bernat | feca7d8 | 2017-08-08 20:23:49 +0200 | [diff] [blame] | 5046 | net->ipv6.fib6_has_custom_rules = false; |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 5047 | net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template, |
| 5048 | sizeof(*net->ipv6.ip6_prohibit_entry), |
| 5049 | GFP_KERNEL); |
Peter Zijlstra | 68fffc6 | 2008-10-07 14:12:10 -0700 | [diff] [blame] | 5050 | if (!net->ipv6.ip6_prohibit_entry) |
| 5051 | goto out_ip6_null_entry; |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 5052 | 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] | 5053 | dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst, |
| 5054 | ip6_template_metrics, true); |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 5055 | |
| 5056 | net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template, |
| 5057 | sizeof(*net->ipv6.ip6_blk_hole_entry), |
| 5058 | GFP_KERNEL); |
Peter Zijlstra | 68fffc6 | 2008-10-07 14:12:10 -0700 | [diff] [blame] | 5059 | if (!net->ipv6.ip6_blk_hole_entry) |
| 5060 | goto out_ip6_prohibit_entry; |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 5061 | 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] | 5062 | dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst, |
| 5063 | ip6_template_metrics, true); |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 5064 | #endif |
| 5065 | |
Peter Zijlstra | b339a47c | 2008-10-07 14:15:00 -0700 | [diff] [blame] | 5066 | net->ipv6.sysctl.flush_delay = 0; |
| 5067 | net->ipv6.sysctl.ip6_rt_max_size = 4096; |
| 5068 | net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2; |
| 5069 | net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ; |
| 5070 | net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ; |
| 5071 | net->ipv6.sysctl.ip6_rt_gc_elasticity = 9; |
| 5072 | net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ; |
| 5073 | net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40; |
| 5074 | |
Benjamin Thery | 6891a34 | 2008-03-04 13:49:47 -0800 | [diff] [blame] | 5075 | net->ipv6.ip6_rt_gc_expire = 30*HZ; |
| 5076 | |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 5077 | ret = 0; |
| 5078 | out: |
| 5079 | return ret; |
Benjamin Thery | f2fc6a5 | 2008-03-04 13:49:23 -0800 | [diff] [blame] | 5080 | |
Peter Zijlstra | 68fffc6 | 2008-10-07 14:12:10 -0700 | [diff] [blame] | 5081 | #ifdef CONFIG_IPV6_MULTIPLE_TABLES |
| 5082 | out_ip6_prohibit_entry: |
| 5083 | kfree(net->ipv6.ip6_prohibit_entry); |
| 5084 | out_ip6_null_entry: |
| 5085 | kfree(net->ipv6.ip6_null_entry); |
| 5086 | #endif |
David Ahern | 421842e | 2018-04-17 17:33:18 -0700 | [diff] [blame] | 5087 | out_fib6_null_entry: |
| 5088 | kfree(net->ipv6.fib6_null_entry); |
Eric Dumazet | fc66f95 | 2010-10-08 06:37:34 +0000 | [diff] [blame] | 5089 | out_ip6_dst_entries: |
| 5090 | dst_entries_destroy(&net->ipv6.ip6_dst_ops); |
Benjamin Thery | f2fc6a5 | 2008-03-04 13:49:23 -0800 | [diff] [blame] | 5091 | out_ip6_dst_ops: |
Benjamin Thery | f2fc6a5 | 2008-03-04 13:49:23 -0800 | [diff] [blame] | 5092 | goto out; |
Daniel Lezcano | cdb1876 | 2008-03-04 13:45:33 -0800 | [diff] [blame] | 5093 | } |
| 5094 | |
Alexey Dobriyan | 2c8c1e7 | 2010-01-17 03:35:32 +0000 | [diff] [blame] | 5095 | static void __net_exit ip6_route_net_exit(struct net *net) |
Daniel Lezcano | cdb1876 | 2008-03-04 13:45:33 -0800 | [diff] [blame] | 5096 | { |
David Ahern | 421842e | 2018-04-17 17:33:18 -0700 | [diff] [blame] | 5097 | kfree(net->ipv6.fib6_null_entry); |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 5098 | kfree(net->ipv6.ip6_null_entry); |
| 5099 | #ifdef CONFIG_IPV6_MULTIPLE_TABLES |
| 5100 | kfree(net->ipv6.ip6_prohibit_entry); |
| 5101 | kfree(net->ipv6.ip6_blk_hole_entry); |
| 5102 | #endif |
Xiaotian Feng | 41bb78b | 2010-11-02 16:11:05 +0000 | [diff] [blame] | 5103 | dst_entries_destroy(&net->ipv6.ip6_dst_ops); |
Daniel Lezcano | cdb1876 | 2008-03-04 13:45:33 -0800 | [diff] [blame] | 5104 | } |
| 5105 | |
Thomas Graf | d189634 | 2012-06-18 12:08:33 +0000 | [diff] [blame] | 5106 | static int __net_init ip6_route_net_init_late(struct net *net) |
| 5107 | { |
| 5108 | #ifdef CONFIG_PROC_FS |
Gao feng | d4beaa6 | 2013-02-18 01:34:54 +0000 | [diff] [blame] | 5109 | proc_create("ipv6_route", 0, net->proc_net, &ipv6_route_proc_fops); |
Joe Perches | d644406 | 2018-03-23 15:54:38 -0700 | [diff] [blame] | 5110 | proc_create("rt6_stats", 0444, net->proc_net, &rt6_stats_seq_fops); |
Thomas Graf | d189634 | 2012-06-18 12:08:33 +0000 | [diff] [blame] | 5111 | #endif |
| 5112 | return 0; |
| 5113 | } |
| 5114 | |
| 5115 | static void __net_exit ip6_route_net_exit_late(struct net *net) |
| 5116 | { |
| 5117 | #ifdef CONFIG_PROC_FS |
Gao feng | ece31ff | 2013-02-18 01:34:56 +0000 | [diff] [blame] | 5118 | remove_proc_entry("ipv6_route", net->proc_net); |
| 5119 | remove_proc_entry("rt6_stats", net->proc_net); |
Thomas Graf | d189634 | 2012-06-18 12:08:33 +0000 | [diff] [blame] | 5120 | #endif |
| 5121 | } |
| 5122 | |
Daniel Lezcano | cdb1876 | 2008-03-04 13:45:33 -0800 | [diff] [blame] | 5123 | static struct pernet_operations ip6_route_net_ops = { |
| 5124 | .init = ip6_route_net_init, |
| 5125 | .exit = ip6_route_net_exit, |
| 5126 | }; |
| 5127 | |
David S. Miller | c3426b4 | 2012-06-09 16:27:05 -0700 | [diff] [blame] | 5128 | static int __net_init ipv6_inetpeer_init(struct net *net) |
| 5129 | { |
| 5130 | struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL); |
| 5131 | |
| 5132 | if (!bp) |
| 5133 | return -ENOMEM; |
| 5134 | inet_peer_base_init(bp); |
| 5135 | net->ipv6.peers = bp; |
| 5136 | return 0; |
| 5137 | } |
| 5138 | |
| 5139 | static void __net_exit ipv6_inetpeer_exit(struct net *net) |
| 5140 | { |
| 5141 | struct inet_peer_base *bp = net->ipv6.peers; |
| 5142 | |
| 5143 | net->ipv6.peers = NULL; |
David S. Miller | 56a6b24 | 2012-06-09 16:32:41 -0700 | [diff] [blame] | 5144 | inetpeer_invalidate_tree(bp); |
David S. Miller | c3426b4 | 2012-06-09 16:27:05 -0700 | [diff] [blame] | 5145 | kfree(bp); |
| 5146 | } |
| 5147 | |
David S. Miller | 2b823f7 | 2012-06-09 19:00:16 -0700 | [diff] [blame] | 5148 | static struct pernet_operations ipv6_inetpeer_ops = { |
David S. Miller | c3426b4 | 2012-06-09 16:27:05 -0700 | [diff] [blame] | 5149 | .init = ipv6_inetpeer_init, |
| 5150 | .exit = ipv6_inetpeer_exit, |
| 5151 | }; |
| 5152 | |
Thomas Graf | d189634 | 2012-06-18 12:08:33 +0000 | [diff] [blame] | 5153 | static struct pernet_operations ip6_route_net_late_ops = { |
| 5154 | .init = ip6_route_net_init_late, |
| 5155 | .exit = ip6_route_net_exit_late, |
| 5156 | }; |
| 5157 | |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 5158 | static struct notifier_block ip6_route_dev_notifier = { |
| 5159 | .notifier_call = ip6_route_dev_notify, |
WANG Cong | 242d3a4 | 2017-05-08 10:12:13 -0700 | [diff] [blame] | 5160 | .priority = ADDRCONF_NOTIFY_PRIORITY - 10, |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 5161 | }; |
| 5162 | |
WANG Cong | 2f46093 | 2017-05-03 22:07:31 -0700 | [diff] [blame] | 5163 | void __init ip6_route_init_special_entries(void) |
| 5164 | { |
| 5165 | /* Registering of the loopback is done before this portion of code, |
| 5166 | * the loopback reference in rt6_info will not be taken, do it |
| 5167 | * manually for init_net */ |
David Ahern | 421842e | 2018-04-17 17:33:18 -0700 | [diff] [blame] | 5168 | init_net.ipv6.fib6_null_entry->fib6_nh.nh_dev = init_net.loopback_dev; |
WANG Cong | 2f46093 | 2017-05-03 22:07:31 -0700 | [diff] [blame] | 5169 | init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev; |
| 5170 | init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); |
| 5171 | #ifdef CONFIG_IPV6_MULTIPLE_TABLES |
| 5172 | init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev; |
| 5173 | init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); |
| 5174 | init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev; |
| 5175 | init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); |
| 5176 | #endif |
| 5177 | } |
| 5178 | |
Daniel Lezcano | 433d49c | 2007-12-07 00:43:48 -0800 | [diff] [blame] | 5179 | int __init ip6_route_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5180 | { |
Daniel Lezcano | 433d49c | 2007-12-07 00:43:48 -0800 | [diff] [blame] | 5181 | int ret; |
Martin KaFai Lau | 8d0b94a | 2015-05-22 20:56:04 -0700 | [diff] [blame] | 5182 | int cpu; |
Daniel Lezcano | 433d49c | 2007-12-07 00:43:48 -0800 | [diff] [blame] | 5183 | |
Daniel Lezcano | 9a7ec3a | 2008-03-04 13:48:53 -0800 | [diff] [blame] | 5184 | ret = -ENOMEM; |
| 5185 | ip6_dst_ops_template.kmem_cachep = |
| 5186 | kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0, |
| 5187 | SLAB_HWCACHE_ALIGN, NULL); |
| 5188 | if (!ip6_dst_ops_template.kmem_cachep) |
Fernando Carrijo | c19a28e | 2009-01-07 18:09:08 -0800 | [diff] [blame] | 5189 | goto out; |
David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 5190 | |
Eric Dumazet | fc66f95 | 2010-10-08 06:37:34 +0000 | [diff] [blame] | 5191 | ret = dst_entries_init(&ip6_dst_blackhole_ops); |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 5192 | if (ret) |
Daniel Lezcano | bdb3289 | 2008-03-04 13:48:10 -0800 | [diff] [blame] | 5193 | goto out_kmem_cache; |
Daniel Lezcano | bdb3289 | 2008-03-04 13:48:10 -0800 | [diff] [blame] | 5194 | |
David S. Miller | c3426b4 | 2012-06-09 16:27:05 -0700 | [diff] [blame] | 5195 | ret = register_pernet_subsys(&ipv6_inetpeer_ops); |
| 5196 | if (ret) |
David S. Miller | e8803b6 | 2012-06-16 01:12:19 -0700 | [diff] [blame] | 5197 | goto out_dst_entries; |
Thomas Graf | 2a0c451 | 2012-06-14 23:00:17 +0000 | [diff] [blame] | 5198 | |
David S. Miller | 7e52b33 | 2012-06-15 15:51:55 -0700 | [diff] [blame] | 5199 | ret = register_pernet_subsys(&ip6_route_net_ops); |
| 5200 | if (ret) |
| 5201 | goto out_register_inetpeer; |
David S. Miller | c3426b4 | 2012-06-09 16:27:05 -0700 | [diff] [blame] | 5202 | |
Arnaud Ebalard | 5dc121e | 2008-10-01 02:37:56 -0700 | [diff] [blame] | 5203 | ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep; |
| 5204 | |
David S. Miller | e8803b6 | 2012-06-16 01:12:19 -0700 | [diff] [blame] | 5205 | ret = fib6_init(); |
Daniel Lezcano | 433d49c | 2007-12-07 00:43:48 -0800 | [diff] [blame] | 5206 | if (ret) |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 5207 | goto out_register_subsys; |
Daniel Lezcano | 433d49c | 2007-12-07 00:43:48 -0800 | [diff] [blame] | 5208 | |
Daniel Lezcano | 433d49c | 2007-12-07 00:43:48 -0800 | [diff] [blame] | 5209 | ret = xfrm6_init(); |
| 5210 | if (ret) |
David S. Miller | e8803b6 | 2012-06-16 01:12:19 -0700 | [diff] [blame] | 5211 | goto out_fib6_init; |
Daniel Lezcano | c35b7e7 | 2007-12-08 00:14:11 -0800 | [diff] [blame] | 5212 | |
Daniel Lezcano | 433d49c | 2007-12-07 00:43:48 -0800 | [diff] [blame] | 5213 | ret = fib6_rules_init(); |
| 5214 | if (ret) |
| 5215 | goto xfrm6_init; |
Daniel Lezcano | 7e5449c | 2007-12-08 00:14:54 -0800 | [diff] [blame] | 5216 | |
Thomas Graf | d189634 | 2012-06-18 12:08:33 +0000 | [diff] [blame] | 5217 | ret = register_pernet_subsys(&ip6_route_net_late_ops); |
| 5218 | if (ret) |
| 5219 | goto fib6_rules_init; |
| 5220 | |
Florian Westphal | 16feebc | 2017-12-02 21:44:08 +0100 | [diff] [blame] | 5221 | ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_NEWROUTE, |
| 5222 | inet6_rtm_newroute, NULL, 0); |
| 5223 | if (ret < 0) |
| 5224 | goto out_register_late_subsys; |
| 5225 | |
| 5226 | ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_DELROUTE, |
| 5227 | inet6_rtm_delroute, NULL, 0); |
| 5228 | if (ret < 0) |
| 5229 | goto out_register_late_subsys; |
| 5230 | |
| 5231 | ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETROUTE, |
| 5232 | inet6_rtm_getroute, NULL, |
| 5233 | RTNL_FLAG_DOIT_UNLOCKED); |
| 5234 | if (ret < 0) |
Thomas Graf | d189634 | 2012-06-18 12:08:33 +0000 | [diff] [blame] | 5235 | goto out_register_late_subsys; |
Daniel Lezcano | 433d49c | 2007-12-07 00:43:48 -0800 | [diff] [blame] | 5236 | |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 5237 | ret = register_netdevice_notifier(&ip6_route_dev_notifier); |
Daniel Lezcano | cdb1876 | 2008-03-04 13:45:33 -0800 | [diff] [blame] | 5238 | if (ret) |
Thomas Graf | d189634 | 2012-06-18 12:08:33 +0000 | [diff] [blame] | 5239 | goto out_register_late_subsys; |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 5240 | |
Martin KaFai Lau | 8d0b94a | 2015-05-22 20:56:04 -0700 | [diff] [blame] | 5241 | for_each_possible_cpu(cpu) { |
| 5242 | struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu); |
| 5243 | |
| 5244 | INIT_LIST_HEAD(&ul->head); |
| 5245 | spin_lock_init(&ul->lock); |
| 5246 | } |
| 5247 | |
Daniel Lezcano | 433d49c | 2007-12-07 00:43:48 -0800 | [diff] [blame] | 5248 | out: |
| 5249 | return ret; |
| 5250 | |
Thomas Graf | d189634 | 2012-06-18 12:08:33 +0000 | [diff] [blame] | 5251 | out_register_late_subsys: |
Florian Westphal | 16feebc | 2017-12-02 21:44:08 +0100 | [diff] [blame] | 5252 | rtnl_unregister_all(PF_INET6); |
Thomas Graf | d189634 | 2012-06-18 12:08:33 +0000 | [diff] [blame] | 5253 | unregister_pernet_subsys(&ip6_route_net_late_ops); |
Daniel Lezcano | 433d49c | 2007-12-07 00:43:48 -0800 | [diff] [blame] | 5254 | fib6_rules_init: |
Daniel Lezcano | 433d49c | 2007-12-07 00:43:48 -0800 | [diff] [blame] | 5255 | fib6_rules_cleanup(); |
| 5256 | xfrm6_init: |
Daniel Lezcano | 433d49c | 2007-12-07 00:43:48 -0800 | [diff] [blame] | 5257 | xfrm6_fini(); |
Thomas Graf | 2a0c451 | 2012-06-14 23:00:17 +0000 | [diff] [blame] | 5258 | out_fib6_init: |
| 5259 | fib6_gc_cleanup(); |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 5260 | out_register_subsys: |
| 5261 | unregister_pernet_subsys(&ip6_route_net_ops); |
David S. Miller | 7e52b33 | 2012-06-15 15:51:55 -0700 | [diff] [blame] | 5262 | out_register_inetpeer: |
| 5263 | unregister_pernet_subsys(&ipv6_inetpeer_ops); |
Eric Dumazet | fc66f95 | 2010-10-08 06:37:34 +0000 | [diff] [blame] | 5264 | out_dst_entries: |
| 5265 | dst_entries_destroy(&ip6_dst_blackhole_ops); |
Daniel Lezcano | 433d49c | 2007-12-07 00:43:48 -0800 | [diff] [blame] | 5266 | out_kmem_cache: |
Benjamin Thery | f2fc6a5 | 2008-03-04 13:49:23 -0800 | [diff] [blame] | 5267 | kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep); |
Daniel Lezcano | 433d49c | 2007-12-07 00:43:48 -0800 | [diff] [blame] | 5268 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5269 | } |
| 5270 | |
| 5271 | void ip6_route_cleanup(void) |
| 5272 | { |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 5273 | unregister_netdevice_notifier(&ip6_route_dev_notifier); |
Thomas Graf | d189634 | 2012-06-18 12:08:33 +0000 | [diff] [blame] | 5274 | unregister_pernet_subsys(&ip6_route_net_late_ops); |
Thomas Graf | 101367c | 2006-08-04 03:39:02 -0700 | [diff] [blame] | 5275 | fib6_rules_cleanup(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5276 | xfrm6_fini(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5277 | fib6_gc_cleanup(); |
David S. Miller | c3426b4 | 2012-06-09 16:27:05 -0700 | [diff] [blame] | 5278 | unregister_pernet_subsys(&ipv6_inetpeer_ops); |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 5279 | unregister_pernet_subsys(&ip6_route_net_ops); |
Xiaotian Feng | 41bb78b | 2010-11-02 16:11:05 +0000 | [diff] [blame] | 5280 | dst_entries_destroy(&ip6_dst_blackhole_ops); |
Benjamin Thery | f2fc6a5 | 2008-03-04 13:49:23 -0800 | [diff] [blame] | 5281 | kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5282 | } |