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); |
Nicolas Dichtel | 52bd4c0 | 2013-06-28 17:35:48 +0200 | [diff] [blame] | 99 | static int rt6_score_route(struct rt6_info *rt, int oif, int strict); |
David Ahern | 16a16cd | 2017-02-02 12:37:11 -0800 | [diff] [blame] | 100 | static size_t rt6_nlmsg_size(struct rt6_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, |
| 102 | struct rt6_info *rt, struct dst_entry *dst, |
| 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); |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 106 | static struct rt6_info *rt6_find_cached_rt(struct rt6_info *rt, |
| 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 |
Daniel Lezcano | efa2cea | 2008-03-04 13:46:48 -0800 | [diff] [blame] | 111 | static struct rt6_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); |
Daniel Lezcano | efa2cea | 2008-03-04 13:46:48 -0800 | [diff] [blame] | 116 | static struct rt6_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 S. Miller | f894cbf | 2012-07-02 21:52:24 -0700 | [diff] [blame] | 185 | static inline const void *choose_neigh_daddr(struct rt6_info *rt, |
| 186 | struct sk_buff *skb, |
| 187 | const void *daddr) |
David S. Miller | 3923297 | 2012-01-26 15:22:32 -0500 | [diff] [blame] | 188 | { |
| 189 | struct in6_addr *p = &rt->rt6i_gateway; |
| 190 | |
David S. Miller | a7563f3 | 2012-01-26 16:29:16 -0500 | [diff] [blame] | 191 | if (!ipv6_addr_any(p)) |
David S. Miller | 3923297 | 2012-01-26 15:22:32 -0500 | [diff] [blame] | 192 | return (const void *) p; |
David S. Miller | f894cbf | 2012-07-02 21:52:24 -0700 | [diff] [blame] | 193 | else if (skb) |
| 194 | return &ipv6_hdr(skb)->daddr; |
David S. Miller | 3923297 | 2012-01-26 15:22:32 -0500 | [diff] [blame] | 195 | return daddr; |
| 196 | } |
| 197 | |
David S. Miller | f894cbf | 2012-07-02 21:52:24 -0700 | [diff] [blame] | 198 | static struct neighbour *ip6_neigh_lookup(const struct dst_entry *dst, |
| 199 | struct sk_buff *skb, |
| 200 | const void *daddr) |
David S. Miller | d3aaeb3 | 2011-07-18 00:40:17 -0700 | [diff] [blame] | 201 | { |
David S. Miller | 3923297 | 2012-01-26 15:22:32 -0500 | [diff] [blame] | 202 | struct rt6_info *rt = (struct rt6_info *) dst; |
| 203 | struct neighbour *n; |
| 204 | |
David S. Miller | f894cbf | 2012-07-02 21:52:24 -0700 | [diff] [blame] | 205 | daddr = choose_neigh_daddr(rt, skb, daddr); |
YOSHIFUJI Hideaki / 吉藤英明 | 8e022ee | 2013-01-17 12:53:09 +0000 | [diff] [blame] | 206 | n = __ipv6_neigh_lookup(dst->dev, daddr); |
David S. Miller | f83c779 | 2011-12-28 15:41:23 -0500 | [diff] [blame] | 207 | if (n) |
| 208 | return n; |
| 209 | return neigh_create(&nd_tbl, daddr, dst->dev); |
| 210 | } |
| 211 | |
Julian Anastasov | 63fca65 | 2017-02-06 23:14:15 +0200 | [diff] [blame] | 212 | static void ip6_confirm_neigh(const struct dst_entry *dst, const void *daddr) |
| 213 | { |
| 214 | struct net_device *dev = dst->dev; |
| 215 | struct rt6_info *rt = (struct rt6_info *)dst; |
| 216 | |
| 217 | daddr = choose_neigh_daddr(rt, NULL, daddr); |
| 218 | if (!daddr) |
| 219 | return; |
| 220 | if (dev->flags & (IFF_NOARP | IFF_LOOPBACK)) |
| 221 | return; |
| 222 | if (ipv6_addr_is_multicast((const struct in6_addr *)daddr)) |
| 223 | return; |
| 224 | __ipv6_confirm_neigh(dev, daddr); |
| 225 | } |
| 226 | |
Daniel Lezcano | 9a7ec3a | 2008-03-04 13:48:53 -0800 | [diff] [blame] | 227 | static struct dst_ops ip6_dst_ops_template = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | .family = AF_INET6, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | .gc = ip6_dst_gc, |
| 230 | .gc_thresh = 1024, |
| 231 | .check = ip6_dst_check, |
David S. Miller | 0dbaee3 | 2010-12-13 12:52:14 -0800 | [diff] [blame] | 232 | .default_advmss = ip6_default_advmss, |
Steffen Klassert | ebb762f | 2011-11-23 02:12:51 +0000 | [diff] [blame] | 233 | .mtu = ip6_mtu, |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 234 | .cow_metrics = dst_cow_metrics_generic, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | .destroy = ip6_dst_destroy, |
| 236 | .ifdown = ip6_dst_ifdown, |
| 237 | .negative_advice = ip6_negative_advice, |
| 238 | .link_failure = ip6_link_failure, |
| 239 | .update_pmtu = ip6_rt_update_pmtu, |
David S. Miller | 6e157b6 | 2012-07-12 00:05:02 -0700 | [diff] [blame] | 240 | .redirect = rt6_do_redirect, |
Eric W. Biederman | 9f8955c | 2015-10-07 16:48:39 -0500 | [diff] [blame] | 241 | .local_out = __ip6_local_out, |
David S. Miller | d3aaeb3 | 2011-07-18 00:40:17 -0700 | [diff] [blame] | 242 | .neigh_lookup = ip6_neigh_lookup, |
Julian Anastasov | 63fca65 | 2017-02-06 23:14:15 +0200 | [diff] [blame] | 243 | .confirm_neigh = ip6_confirm_neigh, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | }; |
| 245 | |
Steffen Klassert | ebb762f | 2011-11-23 02:12:51 +0000 | [diff] [blame] | 246 | static unsigned int ip6_blackhole_mtu(const struct dst_entry *dst) |
Roland Dreier | ec831ea | 2011-01-31 13:16:00 -0800 | [diff] [blame] | 247 | { |
Steffen Klassert | 618f9bc | 2011-11-23 02:13:31 +0000 | [diff] [blame] | 248 | unsigned int mtu = dst_metric_raw(dst, RTAX_MTU); |
| 249 | |
| 250 | return mtu ? : dst->dev->mtu; |
Roland Dreier | ec831ea | 2011-01-31 13:16:00 -0800 | [diff] [blame] | 251 | } |
| 252 | |
David S. Miller | 6700c27 | 2012-07-17 03:29:28 -0700 | [diff] [blame] | 253 | static void ip6_rt_blackhole_update_pmtu(struct dst_entry *dst, struct sock *sk, |
| 254 | struct sk_buff *skb, u32 mtu) |
David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 255 | { |
| 256 | } |
| 257 | |
David S. Miller | 6700c27 | 2012-07-17 03:29:28 -0700 | [diff] [blame] | 258 | static void ip6_rt_blackhole_redirect(struct dst_entry *dst, struct sock *sk, |
| 259 | struct sk_buff *skb) |
David S. Miller | b587ee3 | 2012-07-12 00:39:24 -0700 | [diff] [blame] | 260 | { |
| 261 | } |
| 262 | |
David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 263 | static struct dst_ops ip6_dst_blackhole_ops = { |
| 264 | .family = AF_INET6, |
David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 265 | .destroy = ip6_dst_destroy, |
| 266 | .check = ip6_dst_check, |
Steffen Klassert | ebb762f | 2011-11-23 02:12:51 +0000 | [diff] [blame] | 267 | .mtu = ip6_blackhole_mtu, |
Eric Dumazet | 214f45c | 2011-02-18 11:39:01 -0800 | [diff] [blame] | 268 | .default_advmss = ip6_default_advmss, |
David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 269 | .update_pmtu = ip6_rt_blackhole_update_pmtu, |
David S. Miller | b587ee3 | 2012-07-12 00:39:24 -0700 | [diff] [blame] | 270 | .redirect = ip6_rt_blackhole_redirect, |
Martin KaFai Lau | 0a1f596 | 2015-10-15 16:39:58 -0700 | [diff] [blame] | 271 | .cow_metrics = dst_cow_metrics_generic, |
David S. Miller | d3aaeb3 | 2011-07-18 00:40:17 -0700 | [diff] [blame] | 272 | .neigh_lookup = ip6_neigh_lookup, |
David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 273 | }; |
| 274 | |
David S. Miller | 62fa8a8 | 2011-01-26 20:51:05 -0800 | [diff] [blame] | 275 | static const u32 ip6_template_metrics[RTAX_MAX] = { |
Li RongQing | 14edd87 | 2012-10-24 14:01:18 +0800 | [diff] [blame] | 276 | [RTAX_HOPLIMIT - 1] = 0, |
David S. Miller | 62fa8a8 | 2011-01-26 20:51:05 -0800 | [diff] [blame] | 277 | }; |
| 278 | |
Eric Dumazet | fb0af4c | 2012-09-11 21:47:51 +0000 | [diff] [blame] | 279 | static const struct rt6_info ip6_null_entry_template = { |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 280 | .dst = { |
| 281 | .__refcnt = ATOMIC_INIT(1), |
| 282 | .__use = 1, |
Nicolas Dichtel | 2c20cbd | 2012-09-10 22:09:47 +0000 | [diff] [blame] | 283 | .obsolete = DST_OBSOLETE_FORCE_CHK, |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 284 | .error = -ENETUNREACH, |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 285 | .input = ip6_pkt_discard, |
| 286 | .output = ip6_pkt_discard_out, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | }, |
| 288 | .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP), |
Jean-Mickael Guerin | 4f72427 | 2009-05-20 17:38:59 -0700 | [diff] [blame] | 289 | .rt6i_protocol = RTPROT_KERNEL, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | .rt6i_metric = ~(u32) 0, |
| 291 | .rt6i_ref = ATOMIC_INIT(1), |
David Ahern | e8478e8 | 2018-04-17 17:33:13 -0700 | [diff] [blame] | 292 | .fib6_type = RTN_UNREACHABLE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | }; |
| 294 | |
Thomas Graf | 101367c | 2006-08-04 03:39:02 -0700 | [diff] [blame] | 295 | #ifdef CONFIG_IPV6_MULTIPLE_TABLES |
| 296 | |
Eric Dumazet | fb0af4c | 2012-09-11 21:47:51 +0000 | [diff] [blame] | 297 | static const struct rt6_info ip6_prohibit_entry_template = { |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 298 | .dst = { |
| 299 | .__refcnt = ATOMIC_INIT(1), |
| 300 | .__use = 1, |
Nicolas Dichtel | 2c20cbd | 2012-09-10 22:09:47 +0000 | [diff] [blame] | 301 | .obsolete = DST_OBSOLETE_FORCE_CHK, |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 302 | .error = -EACCES, |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 303 | .input = ip6_pkt_prohibit, |
| 304 | .output = ip6_pkt_prohibit_out, |
Thomas Graf | 101367c | 2006-08-04 03:39:02 -0700 | [diff] [blame] | 305 | }, |
| 306 | .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP), |
Jean-Mickael Guerin | 4f72427 | 2009-05-20 17:38:59 -0700 | [diff] [blame] | 307 | .rt6i_protocol = RTPROT_KERNEL, |
Thomas Graf | 101367c | 2006-08-04 03:39:02 -0700 | [diff] [blame] | 308 | .rt6i_metric = ~(u32) 0, |
| 309 | .rt6i_ref = ATOMIC_INIT(1), |
David Ahern | e8478e8 | 2018-04-17 17:33:13 -0700 | [diff] [blame] | 310 | .fib6_type = RTN_PROHIBIT, |
Thomas Graf | 101367c | 2006-08-04 03:39:02 -0700 | [diff] [blame] | 311 | }; |
| 312 | |
Eric Dumazet | fb0af4c | 2012-09-11 21:47:51 +0000 | [diff] [blame] | 313 | static const struct rt6_info ip6_blk_hole_entry_template = { |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 314 | .dst = { |
| 315 | .__refcnt = ATOMIC_INIT(1), |
| 316 | .__use = 1, |
Nicolas Dichtel | 2c20cbd | 2012-09-10 22:09:47 +0000 | [diff] [blame] | 317 | .obsolete = DST_OBSOLETE_FORCE_CHK, |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 318 | .error = -EINVAL, |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 319 | .input = dst_discard, |
Eric W. Biederman | ede2059 | 2015-10-07 16:48:47 -0500 | [diff] [blame] | 320 | .output = dst_discard_out, |
Thomas Graf | 101367c | 2006-08-04 03:39:02 -0700 | [diff] [blame] | 321 | }, |
| 322 | .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP), |
Jean-Mickael Guerin | 4f72427 | 2009-05-20 17:38:59 -0700 | [diff] [blame] | 323 | .rt6i_protocol = RTPROT_KERNEL, |
Thomas Graf | 101367c | 2006-08-04 03:39:02 -0700 | [diff] [blame] | 324 | .rt6i_metric = ~(u32) 0, |
| 325 | .rt6i_ref = ATOMIC_INIT(1), |
David Ahern | e8478e8 | 2018-04-17 17:33:13 -0700 | [diff] [blame] | 326 | .fib6_type = RTN_BLACKHOLE, |
Thomas Graf | 101367c | 2006-08-04 03:39:02 -0700 | [diff] [blame] | 327 | }; |
| 328 | |
| 329 | #endif |
| 330 | |
Martin KaFai Lau | ebfa45f | 2015-10-15 16:39:57 -0700 | [diff] [blame] | 331 | static void rt6_info_init(struct rt6_info *rt) |
| 332 | { |
| 333 | struct dst_entry *dst = &rt->dst; |
| 334 | |
| 335 | memset(dst + 1, 0, sizeof(*rt) - sizeof(*dst)); |
| 336 | INIT_LIST_HEAD(&rt->rt6i_siblings); |
| 337 | INIT_LIST_HEAD(&rt->rt6i_uncached); |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 338 | rt->fib6_metrics = (struct dst_metrics *)&dst_default_metrics; |
Martin KaFai Lau | ebfa45f | 2015-10-15 16:39:57 -0700 | [diff] [blame] | 339 | } |
| 340 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | /* allocate dst with ip6_dst_ops */ |
Martin KaFai Lau | d52d399 | 2015-05-22 20:56:06 -0700 | [diff] [blame] | 342 | static struct rt6_info *__ip6_dst_alloc(struct net *net, |
| 343 | struct net_device *dev, |
Martin KaFai Lau | ad70686 | 2015-08-14 11:05:52 -0700 | [diff] [blame] | 344 | int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | { |
David S. Miller | 97bab73 | 2012-06-09 22:36:36 -0700 | [diff] [blame] | 346 | struct rt6_info *rt = dst_alloc(&net->ipv6.ip6_dst_ops, dev, |
Wei Wang | b2a9c0e | 2017-06-17 10:42:41 -0700 | [diff] [blame] | 347 | 1, DST_OBSOLETE_FORCE_CHK, flags); |
David S. Miller | cf91166 | 2011-04-28 14:31:47 -0700 | [diff] [blame] | 348 | |
Wei Wang | 81eb844 | 2017-10-06 12:06:11 -0700 | [diff] [blame] | 349 | if (rt) { |
Martin KaFai Lau | ebfa45f | 2015-10-15 16:39:57 -0700 | [diff] [blame] | 350 | rt6_info_init(rt); |
Wei Wang | 81eb844 | 2017-10-06 12:06:11 -0700 | [diff] [blame] | 351 | atomic_inc(&net->ipv6.rt6_stats->fib_rt_alloc); |
| 352 | } |
Steffen Klassert | 8104891 | 2012-07-05 23:37:09 +0000 | [diff] [blame] | 353 | |
David S. Miller | cf91166 | 2011-04-28 14:31:47 -0700 | [diff] [blame] | 354 | return rt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | } |
| 356 | |
David Ahern | 9ab179d | 2016-04-07 11:10:06 -0700 | [diff] [blame] | 357 | struct rt6_info *ip6_dst_alloc(struct net *net, |
| 358 | struct net_device *dev, |
| 359 | int flags) |
Martin KaFai Lau | d52d399 | 2015-05-22 20:56:06 -0700 | [diff] [blame] | 360 | { |
Martin KaFai Lau | ad70686 | 2015-08-14 11:05:52 -0700 | [diff] [blame] | 361 | struct rt6_info *rt = __ip6_dst_alloc(net, dev, flags); |
Martin KaFai Lau | d52d399 | 2015-05-22 20:56:06 -0700 | [diff] [blame] | 362 | |
| 363 | if (rt) { |
| 364 | rt->rt6i_pcpu = alloc_percpu_gfp(struct rt6_info *, GFP_ATOMIC); |
Eric Dumazet | bfd8e5a | 2017-10-09 06:01:37 -0700 | [diff] [blame] | 365 | if (!rt->rt6i_pcpu) { |
Wei Wang | 587fea7 | 2017-06-17 10:42:36 -0700 | [diff] [blame] | 366 | dst_release_immediate(&rt->dst); |
Martin KaFai Lau | d52d399 | 2015-05-22 20:56:06 -0700 | [diff] [blame] | 367 | return NULL; |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | return rt; |
| 372 | } |
David Ahern | 9ab179d | 2016-04-07 11:10:06 -0700 | [diff] [blame] | 373 | EXPORT_SYMBOL(ip6_dst_alloc); |
Martin KaFai Lau | d52d399 | 2015-05-22 20:56:06 -0700 | [diff] [blame] | 374 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | static void ip6_dst_destroy(struct dst_entry *dst) |
| 376 | { |
| 377 | struct rt6_info *rt = (struct rt6_info *)dst; |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 378 | struct rt6_exception_bucket *bucket; |
David Miller | 3a2232e | 2017-11-28 15:40:40 -0500 | [diff] [blame] | 379 | struct rt6_info *from = rt->from; |
Martin KaFai Lau | 8d0b94a | 2015-05-22 20:56:04 -0700 | [diff] [blame] | 380 | struct inet6_dev *idev; |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 381 | struct dst_metrics *m; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | |
Martin KaFai Lau | 4b32b5a | 2015-04-28 13:03:06 -0700 | [diff] [blame] | 383 | dst_destroy_metrics_generic(dst); |
Markus Elfring | 8777531 | 2015-07-02 16:30:24 +0200 | [diff] [blame] | 384 | free_percpu(rt->rt6i_pcpu); |
Martin KaFai Lau | 8d0b94a | 2015-05-22 20:56:04 -0700 | [diff] [blame] | 385 | rt6_uncached_list_del(rt); |
| 386 | |
| 387 | idev = rt->rt6i_idev; |
David S. Miller | 3830847 | 2011-12-03 18:02:47 -0500 | [diff] [blame] | 388 | if (idev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | rt->rt6i_idev = NULL; |
| 390 | in6_dev_put(idev); |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 391 | } |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 392 | bucket = rcu_dereference_protected(rt->rt6i_exception_bucket, 1); |
| 393 | if (bucket) { |
| 394 | rt->rt6i_exception_bucket = NULL; |
| 395 | kfree(bucket); |
| 396 | } |
Gao feng | 1716a96 | 2012-04-06 00:13:10 +0000 | [diff] [blame] | 397 | |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 398 | m = rt->fib6_metrics; |
| 399 | if (m != &dst_default_metrics && refcount_dec_and_test(&m->refcnt)) |
| 400 | kfree(m); |
| 401 | |
David Miller | 3a2232e | 2017-11-28 15:40:40 -0500 | [diff] [blame] | 402 | rt->from = NULL; |
| 403 | dst_release(&from->dst); |
David S. Miller | b341936 | 2010-11-30 12:27:11 -0800 | [diff] [blame] | 404 | } |
| 405 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev, |
| 407 | int how) |
| 408 | { |
| 409 | struct rt6_info *rt = (struct rt6_info *)dst; |
| 410 | struct inet6_dev *idev = rt->rt6i_idev; |
Denis V. Lunev | 5a3e55d | 2007-12-07 00:38:10 -0800 | [diff] [blame] | 411 | struct net_device *loopback_dev = |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 412 | dev_net(dev)->loopback_dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | |
Wei Wang | e5645f5 | 2017-08-14 10:44:59 -0700 | [diff] [blame] | 414 | if (idev && idev->dev != loopback_dev) { |
| 415 | struct inet6_dev *loopback_idev = in6_dev_get(loopback_dev); |
| 416 | if (loopback_idev) { |
| 417 | rt->rt6i_idev = loopback_idev; |
| 418 | in6_dev_put(idev); |
David S. Miller | 97cac08 | 2012-07-02 22:43:47 -0700 | [diff] [blame] | 419 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | } |
| 421 | } |
| 422 | |
Martin KaFai Lau | 5973fb1 | 2015-11-11 11:51:07 -0800 | [diff] [blame] | 423 | static bool __rt6_check_expired(const struct rt6_info *rt) |
| 424 | { |
| 425 | if (rt->rt6i_flags & RTF_EXPIRES) |
| 426 | return time_after(jiffies, rt->dst.expires); |
| 427 | else |
| 428 | return false; |
| 429 | } |
| 430 | |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 431 | static bool rt6_check_expired(const struct rt6_info *rt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | { |
Gao feng | 1716a96 | 2012-04-06 00:13:10 +0000 | [diff] [blame] | 433 | if (rt->rt6i_flags & RTF_EXPIRES) { |
| 434 | if (time_after(jiffies, rt->dst.expires)) |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 435 | return true; |
David Miller | 3a2232e | 2017-11-28 15:40:40 -0500 | [diff] [blame] | 436 | } else if (rt->from) { |
Xin Long | 1e2ea8a | 2017-08-26 20:10:10 +0800 | [diff] [blame] | 437 | return rt->dst.obsolete != DST_OBSOLETE_FORCE_CHK || |
David Ahern | 1489568 | 2018-04-17 17:33:17 -0700 | [diff] [blame^] | 438 | fib6_check_expired(rt->from); |
Gao feng | 1716a96 | 2012-04-06 00:13:10 +0000 | [diff] [blame] | 439 | } |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 440 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | } |
| 442 | |
David Ahern | b4bac17 | 2018-03-02 08:32:18 -0800 | [diff] [blame] | 443 | static struct rt6_info *rt6_multipath_select(const struct net *net, |
| 444 | struct rt6_info *match, |
Nicolas Dichtel | 52bd4c0 | 2013-06-28 17:35:48 +0200 | [diff] [blame] | 445 | struct flowi6 *fl6, int oif, |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 446 | const struct sk_buff *skb, |
Nicolas Dichtel | 52bd4c0 | 2013-06-28 17:35:48 +0200 | [diff] [blame] | 447 | int strict) |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 448 | { |
| 449 | struct rt6_info *sibling, *next_sibling; |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 450 | |
Jakub Sitnicki | b673d6c | 2017-08-23 09:58:31 +0200 | [diff] [blame] | 451 | /* We might have already computed the hash for ICMPv6 errors. In such |
| 452 | * case it will always be non-zero. Otherwise now is the time to do it. |
| 453 | */ |
| 454 | if (!fl6->mp_hash) |
David Ahern | b4bac17 | 2018-03-02 08:32:18 -0800 | [diff] [blame] | 455 | fl6->mp_hash = rt6_multipath_hash(net, fl6, skb, NULL); |
Jakub Sitnicki | b673d6c | 2017-08-23 09:58:31 +0200 | [diff] [blame] | 456 | |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 457 | if (fl6->mp_hash <= atomic_read(&match->fib6_nh.nh_upper_bound)) |
Ido Schimmel | 3d709f6 | 2018-01-09 16:40:27 +0200 | [diff] [blame] | 458 | return match; |
Ido Schimmel | bbfcd77 | 2017-11-21 09:50:12 +0200 | [diff] [blame] | 459 | |
Ido Schimmel | 3d709f6 | 2018-01-09 16:40:27 +0200 | [diff] [blame] | 460 | list_for_each_entry_safe(sibling, next_sibling, &match->rt6i_siblings, |
| 461 | rt6i_siblings) { |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 462 | int nh_upper_bound; |
| 463 | |
| 464 | nh_upper_bound = atomic_read(&sibling->fib6_nh.nh_upper_bound); |
| 465 | if (fl6->mp_hash > nh_upper_bound) |
Ido Schimmel | 3d709f6 | 2018-01-09 16:40:27 +0200 | [diff] [blame] | 466 | continue; |
| 467 | if (rt6_score_route(sibling, oif, strict) < 0) |
| 468 | break; |
| 469 | match = sibling; |
| 470 | break; |
| 471 | } |
| 472 | |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 473 | return match; |
| 474 | } |
| 475 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | /* |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 477 | * Route lookup. rcu_read_lock() should be held. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | */ |
| 479 | |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 480 | static inline struct rt6_info *rt6_device_match(struct net *net, |
| 481 | struct rt6_info *rt, |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 482 | const struct in6_addr *saddr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | int oif, |
YOSHIFUJI Hideaki | d420895 | 2008-06-27 20:14:54 -0700 | [diff] [blame] | 484 | int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | { |
| 486 | struct rt6_info *local = NULL; |
| 487 | struct rt6_info *sprt; |
| 488 | |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 489 | if (!oif && ipv6_addr_any(saddr) && |
| 490 | !(rt->fib6_nh.nh_flags & RTNH_F_DEAD)) |
Ido Schimmel | 8067bb8 | 2018-01-07 12:45:09 +0200 | [diff] [blame] | 491 | return rt; |
YOSHIFUJI Hideaki | dd3abc4 | 2008-07-02 18:30:18 +0900 | [diff] [blame] | 492 | |
David Miller | 071fb37 | 2017-11-28 15:40:15 -0500 | [diff] [blame] | 493 | for (sprt = rt; sprt; sprt = rcu_dereference(sprt->rt6_next)) { |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 494 | const struct net_device *dev = sprt->fib6_nh.nh_dev; |
YOSHIFUJI Hideaki | dd3abc4 | 2008-07-02 18:30:18 +0900 | [diff] [blame] | 495 | |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 496 | if (sprt->fib6_nh.nh_flags & RTNH_F_DEAD) |
Ido Schimmel | 8067bb8 | 2018-01-07 12:45:09 +0200 | [diff] [blame] | 497 | continue; |
| 498 | |
YOSHIFUJI Hideaki | dd3abc4 | 2008-07-02 18:30:18 +0900 | [diff] [blame] | 499 | if (oif) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | if (dev->ifindex == oif) |
| 501 | return sprt; |
| 502 | if (dev->flags & IFF_LOOPBACK) { |
David S. Miller | 3830847 | 2011-12-03 18:02:47 -0500 | [diff] [blame] | 503 | if (!sprt->rt6i_idev || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | sprt->rt6i_idev->dev->ifindex != oif) { |
David Ahern | 17fb0b2 | 2015-09-25 15:22:54 -0600 | [diff] [blame] | 505 | if (flags & RT6_LOOKUP_F_IFACE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | continue; |
David Ahern | 17fb0b2 | 2015-09-25 15:22:54 -0600 | [diff] [blame] | 507 | if (local && |
| 508 | local->rt6i_idev->dev->ifindex == oif) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | continue; |
| 510 | } |
| 511 | local = sprt; |
| 512 | } |
YOSHIFUJI Hideaki | dd3abc4 | 2008-07-02 18:30:18 +0900 | [diff] [blame] | 513 | } else { |
| 514 | if (ipv6_chk_addr(net, saddr, dev, |
| 515 | flags & RT6_LOOKUP_F_IFACE)) |
| 516 | return sprt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | } |
YOSHIFUJI Hideaki | dd3abc4 | 2008-07-02 18:30:18 +0900 | [diff] [blame] | 518 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | |
YOSHIFUJI Hideaki | dd3abc4 | 2008-07-02 18:30:18 +0900 | [diff] [blame] | 520 | if (oif) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | if (local) |
| 522 | return local; |
| 523 | |
YOSHIFUJI Hideaki | d420895 | 2008-06-27 20:14:54 -0700 | [diff] [blame] | 524 | if (flags & RT6_LOOKUP_F_IFACE) |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 525 | return net->ipv6.ip6_null_entry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | } |
Ido Schimmel | 8067bb8 | 2018-01-07 12:45:09 +0200 | [diff] [blame] | 527 | |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 528 | return rt->fib6_nh.nh_flags & RTNH_F_DEAD ? net->ipv6.ip6_null_entry : rt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | } |
| 530 | |
YOSHIFUJI Hideaki | 2709725 | 2006-03-20 17:05:13 -0800 | [diff] [blame] | 531 | #ifdef CONFIG_IPV6_ROUTER_PREF |
Hannes Frederic Sowa | c2f17e8 | 2013-10-21 06:17:15 +0200 | [diff] [blame] | 532 | struct __rt6_probe_work { |
| 533 | struct work_struct work; |
| 534 | struct in6_addr target; |
| 535 | struct net_device *dev; |
| 536 | }; |
| 537 | |
| 538 | static void rt6_probe_deferred(struct work_struct *w) |
| 539 | { |
| 540 | struct in6_addr mcaddr; |
| 541 | struct __rt6_probe_work *work = |
| 542 | container_of(w, struct __rt6_probe_work, work); |
| 543 | |
| 544 | addrconf_addr_solict_mult(&work->target, &mcaddr); |
Erik Nordmark | adc176c | 2016-12-02 14:00:08 -0800 | [diff] [blame] | 545 | ndisc_send_ns(work->dev, &work->target, &mcaddr, NULL, 0); |
Hannes Frederic Sowa | c2f17e8 | 2013-10-21 06:17:15 +0200 | [diff] [blame] | 546 | dev_put(work->dev); |
Michael Büsch | 662f553 | 2015-02-08 10:14:07 +0100 | [diff] [blame] | 547 | kfree(work); |
Hannes Frederic Sowa | c2f17e8 | 2013-10-21 06:17:15 +0200 | [diff] [blame] | 548 | } |
| 549 | |
YOSHIFUJI Hideaki | 2709725 | 2006-03-20 17:05:13 -0800 | [diff] [blame] | 550 | static void rt6_probe(struct rt6_info *rt) |
| 551 | { |
Martin KaFai Lau | 990edb4 | 2015-07-24 09:57:42 -0700 | [diff] [blame] | 552 | struct __rt6_probe_work *work; |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 553 | const struct in6_addr *nh_gw; |
Eric Dumazet | f2c31e3 | 2011-07-29 19:00:53 +0000 | [diff] [blame] | 554 | struct neighbour *neigh; |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 555 | struct net_device *dev; |
| 556 | |
YOSHIFUJI Hideaki | 2709725 | 2006-03-20 17:05:13 -0800 | [diff] [blame] | 557 | /* |
| 558 | * Okay, this does not seem to be appropriate |
| 559 | * for now, however, we need to check if it |
| 560 | * is really so; aka Router Reachability Probing. |
| 561 | * |
| 562 | * Router Reachability Probe MUST be rate-limited |
| 563 | * to no more than one per minute. |
| 564 | */ |
YOSHIFUJI Hideaki / 吉藤英明 | 2152cae | 2013-01-17 12:53:43 +0000 | [diff] [blame] | 565 | if (!rt || !(rt->rt6i_flags & RTF_GATEWAY)) |
Amerigo Wang | fdd6681 | 2012-09-10 02:48:44 +0000 | [diff] [blame] | 566 | return; |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 567 | |
| 568 | nh_gw = &rt->fib6_nh.nh_gw; |
| 569 | dev = rt->fib6_nh.nh_dev; |
YOSHIFUJI Hideaki / 吉藤英明 | 2152cae | 2013-01-17 12:53:43 +0000 | [diff] [blame] | 570 | rcu_read_lock_bh(); |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 571 | neigh = __ipv6_neigh_lookup_noref(dev, nh_gw); |
YOSHIFUJI Hideaki / 吉藤英明 | 2152cae | 2013-01-17 12:53:43 +0000 | [diff] [blame] | 572 | if (neigh) { |
Martin KaFai Lau | 8d6c31b | 2015-07-24 09:57:43 -0700 | [diff] [blame] | 573 | if (neigh->nud_state & NUD_VALID) |
| 574 | goto out; |
| 575 | |
Martin KaFai Lau | 990edb4 | 2015-07-24 09:57:42 -0700 | [diff] [blame] | 576 | work = NULL; |
YOSHIFUJI Hideaki / 吉藤英明 | 2152cae | 2013-01-17 12:53:43 +0000 | [diff] [blame] | 577 | write_lock(&neigh->lock); |
Martin KaFai Lau | 990edb4 | 2015-07-24 09:57:42 -0700 | [diff] [blame] | 578 | if (!(neigh->nud_state & NUD_VALID) && |
| 579 | time_after(jiffies, |
| 580 | neigh->updated + |
| 581 | rt->rt6i_idev->cnf.rtr_probe_interval)) { |
| 582 | work = kmalloc(sizeof(*work), GFP_ATOMIC); |
| 583 | if (work) |
| 584 | __neigh_set_probe_once(neigh); |
Hannes Frederic Sowa | c2f17e8 | 2013-10-21 06:17:15 +0200 | [diff] [blame] | 585 | } |
YOSHIFUJI Hideaki / 吉藤英明 | 2152cae | 2013-01-17 12:53:43 +0000 | [diff] [blame] | 586 | write_unlock(&neigh->lock); |
Martin KaFai Lau | 990edb4 | 2015-07-24 09:57:42 -0700 | [diff] [blame] | 587 | } else { |
| 588 | work = kmalloc(sizeof(*work), GFP_ATOMIC); |
Eric Dumazet | f2c31e3 | 2011-07-29 19:00:53 +0000 | [diff] [blame] | 589 | } |
Martin KaFai Lau | 990edb4 | 2015-07-24 09:57:42 -0700 | [diff] [blame] | 590 | |
| 591 | if (work) { |
| 592 | INIT_WORK(&work->work, rt6_probe_deferred); |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 593 | work->target = *nh_gw; |
| 594 | dev_hold(dev); |
| 595 | work->dev = dev; |
Martin KaFai Lau | 990edb4 | 2015-07-24 09:57:42 -0700 | [diff] [blame] | 596 | schedule_work(&work->work); |
| 597 | } |
| 598 | |
Martin KaFai Lau | 8d6c31b | 2015-07-24 09:57:43 -0700 | [diff] [blame] | 599 | out: |
YOSHIFUJI Hideaki / 吉藤英明 | 2152cae | 2013-01-17 12:53:43 +0000 | [diff] [blame] | 600 | rcu_read_unlock_bh(); |
YOSHIFUJI Hideaki | 2709725 | 2006-03-20 17:05:13 -0800 | [diff] [blame] | 601 | } |
| 602 | #else |
| 603 | static inline void rt6_probe(struct rt6_info *rt) |
| 604 | { |
YOSHIFUJI Hideaki | 2709725 | 2006-03-20 17:05:13 -0800 | [diff] [blame] | 605 | } |
| 606 | #endif |
| 607 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 608 | /* |
YOSHIFUJI Hideaki | 554cfb7 | 2006-03-20 17:00:26 -0800 | [diff] [blame] | 609 | * Default Router Selection (RFC 2461 6.3.6) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | */ |
Dave Jones | b6f99a2 | 2007-03-22 12:27:49 -0700 | [diff] [blame] | 611 | static inline int rt6_check_dev(struct rt6_info *rt, int oif) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | { |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 613 | const struct net_device *dev = rt->fib6_nh.nh_dev; |
| 614 | |
David S. Miller | 161980f | 2007-04-06 11:42:27 -0700 | [diff] [blame] | 615 | if (!oif || dev->ifindex == oif) |
YOSHIFUJI Hideaki | 554cfb7 | 2006-03-20 17:00:26 -0800 | [diff] [blame] | 616 | return 2; |
David S. Miller | 161980f | 2007-04-06 11:42:27 -0700 | [diff] [blame] | 617 | if ((dev->flags & IFF_LOOPBACK) && |
| 618 | rt->rt6i_idev && rt->rt6i_idev->dev->ifindex == oif) |
| 619 | return 1; |
| 620 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | } |
| 622 | |
Hannes Frederic Sowa | afc154e | 2013-07-11 12:43:42 +0200 | [diff] [blame] | 623 | static inline enum rt6_nud_state rt6_check_neigh(struct rt6_info *rt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 624 | { |
Hannes Frederic Sowa | afc154e | 2013-07-11 12:43:42 +0200 | [diff] [blame] | 625 | enum rt6_nud_state ret = RT6_NUD_FAIL_HARD; |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 626 | struct neighbour *neigh; |
Eric Dumazet | f2c31e3 | 2011-07-29 19:00:53 +0000 | [diff] [blame] | 627 | |
YOSHIFUJI Hideaki | 4d0c591 | 2006-05-26 13:23:41 -0700 | [diff] [blame] | 628 | if (rt->rt6i_flags & RTF_NONEXTHOP || |
| 629 | !(rt->rt6i_flags & RTF_GATEWAY)) |
Hannes Frederic Sowa | afc154e | 2013-07-11 12:43:42 +0200 | [diff] [blame] | 630 | return RT6_NUD_SUCCEED; |
YOSHIFUJI Hideaki / 吉藤英明 | 145a362 | 2013-01-17 12:53:38 +0000 | [diff] [blame] | 631 | |
| 632 | rcu_read_lock_bh(); |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 633 | neigh = __ipv6_neigh_lookup_noref(rt->fib6_nh.nh_dev, |
| 634 | &rt->fib6_nh.nh_gw); |
YOSHIFUJI Hideaki / 吉藤英明 | 145a362 | 2013-01-17 12:53:38 +0000 | [diff] [blame] | 635 | if (neigh) { |
| 636 | read_lock(&neigh->lock); |
YOSHIFUJI Hideaki | 554cfb7 | 2006-03-20 17:00:26 -0800 | [diff] [blame] | 637 | if (neigh->nud_state & NUD_VALID) |
Hannes Frederic Sowa | afc154e | 2013-07-11 12:43:42 +0200 | [diff] [blame] | 638 | ret = RT6_NUD_SUCCEED; |
YOSHIFUJI Hideaki | 398bcbe | 2008-01-19 00:35:16 -0800 | [diff] [blame] | 639 | #ifdef CONFIG_IPV6_ROUTER_PREF |
Paul Marks | a5a81f0 | 2012-12-03 10:26:54 +0000 | [diff] [blame] | 640 | else if (!(neigh->nud_state & NUD_FAILED)) |
Hannes Frederic Sowa | afc154e | 2013-07-11 12:43:42 +0200 | [diff] [blame] | 641 | ret = RT6_NUD_SUCCEED; |
Jiri Benc | 7e98056 | 2013-12-11 13:48:20 +0100 | [diff] [blame] | 642 | else |
| 643 | ret = RT6_NUD_FAIL_PROBE; |
YOSHIFUJI Hideaki | 398bcbe | 2008-01-19 00:35:16 -0800 | [diff] [blame] | 644 | #endif |
YOSHIFUJI Hideaki / 吉藤英明 | 145a362 | 2013-01-17 12:53:38 +0000 | [diff] [blame] | 645 | read_unlock(&neigh->lock); |
Hannes Frederic Sowa | afc154e | 2013-07-11 12:43:42 +0200 | [diff] [blame] | 646 | } else { |
| 647 | ret = IS_ENABLED(CONFIG_IPV6_ROUTER_PREF) ? |
Jiri Benc | 7e98056 | 2013-12-11 13:48:20 +0100 | [diff] [blame] | 648 | RT6_NUD_SUCCEED : RT6_NUD_FAIL_DO_RR; |
Paul Marks | a5a81f0 | 2012-12-03 10:26:54 +0000 | [diff] [blame] | 649 | } |
YOSHIFUJI Hideaki / 吉藤英明 | 145a362 | 2013-01-17 12:53:38 +0000 | [diff] [blame] | 650 | rcu_read_unlock_bh(); |
| 651 | |
Paul Marks | a5a81f0 | 2012-12-03 10:26:54 +0000 | [diff] [blame] | 652 | return ret; |
YOSHIFUJI Hideaki | 554cfb7 | 2006-03-20 17:00:26 -0800 | [diff] [blame] | 653 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 654 | |
YOSHIFUJI Hideaki | 554cfb7 | 2006-03-20 17:00:26 -0800 | [diff] [blame] | 655 | static int rt6_score_route(struct rt6_info *rt, int oif, |
| 656 | int strict) |
| 657 | { |
Paul Marks | a5a81f0 | 2012-12-03 10:26:54 +0000 | [diff] [blame] | 658 | int m; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 659 | |
YOSHIFUJI Hideaki | 4d0c591 | 2006-05-26 13:23:41 -0700 | [diff] [blame] | 660 | m = rt6_check_dev(rt, oif); |
YOSHIFUJI Hideaki | 77d16f4 | 2006-08-23 17:25:05 -0700 | [diff] [blame] | 661 | if (!m && (strict & RT6_LOOKUP_F_IFACE)) |
Hannes Frederic Sowa | afc154e | 2013-07-11 12:43:42 +0200 | [diff] [blame] | 662 | return RT6_NUD_FAIL_HARD; |
YOSHIFUJI Hideaki | ebacaaa | 2006-03-20 17:04:53 -0800 | [diff] [blame] | 663 | #ifdef CONFIG_IPV6_ROUTER_PREF |
| 664 | m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(rt->rt6i_flags)) << 2; |
| 665 | #endif |
Hannes Frederic Sowa | afc154e | 2013-07-11 12:43:42 +0200 | [diff] [blame] | 666 | if (strict & RT6_LOOKUP_F_REACHABLE) { |
| 667 | int n = rt6_check_neigh(rt); |
| 668 | if (n < 0) |
| 669 | return n; |
| 670 | } |
YOSHIFUJI Hideaki | 554cfb7 | 2006-03-20 17:00:26 -0800 | [diff] [blame] | 671 | return m; |
| 672 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 673 | |
David S. Miller | f11e665 | 2007-03-24 20:36:25 -0700 | [diff] [blame] | 674 | static struct rt6_info *find_match(struct rt6_info *rt, int oif, int strict, |
Hannes Frederic Sowa | afc154e | 2013-07-11 12:43:42 +0200 | [diff] [blame] | 675 | int *mpri, struct rt6_info *match, |
| 676 | bool *do_rr) |
YOSHIFUJI Hideaki | 554cfb7 | 2006-03-20 17:00:26 -0800 | [diff] [blame] | 677 | { |
David S. Miller | f11e665 | 2007-03-24 20:36:25 -0700 | [diff] [blame] | 678 | int m; |
Hannes Frederic Sowa | afc154e | 2013-07-11 12:43:42 +0200 | [diff] [blame] | 679 | bool match_do_rr = false; |
Andy Gospodarek | 35103d1 | 2015-08-13 10:39:01 -0400 | [diff] [blame] | 680 | struct inet6_dev *idev = rt->rt6i_idev; |
Andy Gospodarek | 35103d1 | 2015-08-13 10:39:01 -0400 | [diff] [blame] | 681 | |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 682 | if (rt->fib6_nh.nh_flags & RTNH_F_DEAD) |
Ido Schimmel | 8067bb8 | 2018-01-07 12:45:09 +0200 | [diff] [blame] | 683 | goto out; |
| 684 | |
Ido Schimmel | 14c5206 | 2018-01-07 12:45:07 +0200 | [diff] [blame] | 685 | if (idev->cnf.ignore_routes_with_linkdown && |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 686 | rt->fib6_nh.nh_flags & RTNH_F_LINKDOWN && |
David Ahern | d5d32e4 | 2016-10-24 12:27:23 -0700 | [diff] [blame] | 687 | !(strict & RT6_LOOKUP_F_IGNORE_LINKSTATE)) |
Andy Gospodarek | 35103d1 | 2015-08-13 10:39:01 -0400 | [diff] [blame] | 688 | goto out; |
David S. Miller | f11e665 | 2007-03-24 20:36:25 -0700 | [diff] [blame] | 689 | |
David Ahern | 1489568 | 2018-04-17 17:33:17 -0700 | [diff] [blame^] | 690 | if (fib6_check_expired(rt)) |
David S. Miller | f11e665 | 2007-03-24 20:36:25 -0700 | [diff] [blame] | 691 | goto out; |
| 692 | |
| 693 | m = rt6_score_route(rt, oif, strict); |
Jiri Benc | 7e98056 | 2013-12-11 13:48:20 +0100 | [diff] [blame] | 694 | if (m == RT6_NUD_FAIL_DO_RR) { |
Hannes Frederic Sowa | afc154e | 2013-07-11 12:43:42 +0200 | [diff] [blame] | 695 | match_do_rr = true; |
| 696 | m = 0; /* lowest valid score */ |
Jiri Benc | 7e98056 | 2013-12-11 13:48:20 +0100 | [diff] [blame] | 697 | } else if (m == RT6_NUD_FAIL_HARD) { |
David S. Miller | f11e665 | 2007-03-24 20:36:25 -0700 | [diff] [blame] | 698 | goto out; |
David S. Miller | f11e665 | 2007-03-24 20:36:25 -0700 | [diff] [blame] | 699 | } |
| 700 | |
Hannes Frederic Sowa | afc154e | 2013-07-11 12:43:42 +0200 | [diff] [blame] | 701 | if (strict & RT6_LOOKUP_F_REACHABLE) |
| 702 | rt6_probe(rt); |
| 703 | |
Jiri Benc | 7e98056 | 2013-12-11 13:48:20 +0100 | [diff] [blame] | 704 | /* 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] | 705 | if (m > *mpri) { |
| 706 | *do_rr = match_do_rr; |
| 707 | *mpri = m; |
| 708 | match = rt; |
| 709 | } |
David S. Miller | f11e665 | 2007-03-24 20:36:25 -0700 | [diff] [blame] | 710 | out: |
| 711 | return match; |
| 712 | } |
| 713 | |
| 714 | static struct rt6_info *find_rr_leaf(struct fib6_node *fn, |
Wei Wang | 8d1040e | 2017-10-06 12:06:08 -0700 | [diff] [blame] | 715 | struct rt6_info *leaf, |
David S. Miller | f11e665 | 2007-03-24 20:36:25 -0700 | [diff] [blame] | 716 | struct rt6_info *rr_head, |
Hannes Frederic Sowa | afc154e | 2013-07-11 12:43:42 +0200 | [diff] [blame] | 717 | u32 metric, int oif, int strict, |
| 718 | bool *do_rr) |
David S. Miller | f11e665 | 2007-03-24 20:36:25 -0700 | [diff] [blame] | 719 | { |
Steffen Klassert | 9fbdcfa | 2015-04-28 13:03:04 -0700 | [diff] [blame] | 720 | struct rt6_info *rt, *match, *cont; |
YOSHIFUJI Hideaki | 554cfb7 | 2006-03-20 17:00:26 -0800 | [diff] [blame] | 721 | int mpri = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 722 | |
David S. Miller | f11e665 | 2007-03-24 20:36:25 -0700 | [diff] [blame] | 723 | match = NULL; |
Steffen Klassert | 9fbdcfa | 2015-04-28 13:03:04 -0700 | [diff] [blame] | 724 | cont = NULL; |
David Miller | 071fb37 | 2017-11-28 15:40:15 -0500 | [diff] [blame] | 725 | for (rt = rr_head; rt; rt = rcu_dereference(rt->rt6_next)) { |
Steffen Klassert | 9fbdcfa | 2015-04-28 13:03:04 -0700 | [diff] [blame] | 726 | if (rt->rt6i_metric != metric) { |
| 727 | cont = rt; |
| 728 | break; |
| 729 | } |
| 730 | |
Hannes Frederic Sowa | afc154e | 2013-07-11 12:43:42 +0200 | [diff] [blame] | 731 | match = find_match(rt, oif, strict, &mpri, match, do_rr); |
Steffen Klassert | 9fbdcfa | 2015-04-28 13:03:04 -0700 | [diff] [blame] | 732 | } |
| 733 | |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 734 | for (rt = leaf; rt && rt != rr_head; |
David Miller | 071fb37 | 2017-11-28 15:40:15 -0500 | [diff] [blame] | 735 | rt = rcu_dereference(rt->rt6_next)) { |
Steffen Klassert | 9fbdcfa | 2015-04-28 13:03:04 -0700 | [diff] [blame] | 736 | if (rt->rt6i_metric != metric) { |
| 737 | cont = rt; |
| 738 | break; |
| 739 | } |
| 740 | |
| 741 | match = find_match(rt, oif, strict, &mpri, match, do_rr); |
| 742 | } |
| 743 | |
| 744 | if (match || !cont) |
| 745 | return match; |
| 746 | |
David Miller | 071fb37 | 2017-11-28 15:40:15 -0500 | [diff] [blame] | 747 | for (rt = cont; rt; rt = rcu_dereference(rt->rt6_next)) |
Hannes Frederic Sowa | afc154e | 2013-07-11 12:43:42 +0200 | [diff] [blame] | 748 | match = find_match(rt, oif, strict, &mpri, match, do_rr); |
YOSHIFUJI Hideaki | 554cfb7 | 2006-03-20 17:00:26 -0800 | [diff] [blame] | 749 | |
David S. Miller | f11e665 | 2007-03-24 20:36:25 -0700 | [diff] [blame] | 750 | return match; |
| 751 | } |
YOSHIFUJI Hideaki | 554cfb7 | 2006-03-20 17:00:26 -0800 | [diff] [blame] | 752 | |
Wei Wang | 8d1040e | 2017-10-06 12:06:08 -0700 | [diff] [blame] | 753 | static struct rt6_info *rt6_select(struct net *net, struct fib6_node *fn, |
| 754 | int oif, int strict) |
David S. Miller | f11e665 | 2007-03-24 20:36:25 -0700 | [diff] [blame] | 755 | { |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 756 | struct rt6_info *leaf = rcu_dereference(fn->leaf); |
David S. Miller | f11e665 | 2007-03-24 20:36:25 -0700 | [diff] [blame] | 757 | struct rt6_info *match, *rt0; |
Hannes Frederic Sowa | afc154e | 2013-07-11 12:43:42 +0200 | [diff] [blame] | 758 | bool do_rr = false; |
Wei Wang | 17ecf59 | 2017-10-06 12:06:09 -0700 | [diff] [blame] | 759 | int key_plen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 760 | |
Wei Wang | 87b1af8 | 2017-10-23 14:59:35 -0700 | [diff] [blame] | 761 | if (!leaf || leaf == net->ipv6.ip6_null_entry) |
Wei Wang | 8d1040e | 2017-10-06 12:06:08 -0700 | [diff] [blame] | 762 | return net->ipv6.ip6_null_entry; |
| 763 | |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 764 | rt0 = rcu_dereference(fn->rr_ptr); |
David S. Miller | f11e665 | 2007-03-24 20:36:25 -0700 | [diff] [blame] | 765 | if (!rt0) |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 766 | rt0 = leaf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 767 | |
Wei Wang | 17ecf59 | 2017-10-06 12:06:09 -0700 | [diff] [blame] | 768 | /* Double check to make sure fn is not an intermediate node |
| 769 | * and fn->leaf does not points to its child's leaf |
| 770 | * (This might happen if all routes under fn are deleted from |
| 771 | * the tree and fib6_repair_tree() is called on the node.) |
| 772 | */ |
| 773 | key_plen = rt0->rt6i_dst.plen; |
| 774 | #ifdef CONFIG_IPV6_SUBTREES |
| 775 | if (rt0->rt6i_src.plen) |
| 776 | key_plen = rt0->rt6i_src.plen; |
| 777 | #endif |
| 778 | if (fn->fn_bit != key_plen) |
| 779 | return net->ipv6.ip6_null_entry; |
| 780 | |
Wei Wang | 8d1040e | 2017-10-06 12:06:08 -0700 | [diff] [blame] | 781 | match = find_rr_leaf(fn, leaf, rt0, rt0->rt6i_metric, oif, strict, |
Hannes Frederic Sowa | afc154e | 2013-07-11 12:43:42 +0200 | [diff] [blame] | 782 | &do_rr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 783 | |
Hannes Frederic Sowa | afc154e | 2013-07-11 12:43:42 +0200 | [diff] [blame] | 784 | if (do_rr) { |
David Miller | 071fb37 | 2017-11-28 15:40:15 -0500 | [diff] [blame] | 785 | struct rt6_info *next = rcu_dereference(rt0->rt6_next); |
David S. Miller | f11e665 | 2007-03-24 20:36:25 -0700 | [diff] [blame] | 786 | |
YOSHIFUJI Hideaki | 554cfb7 | 2006-03-20 17:00:26 -0800 | [diff] [blame] | 787 | /* no entries matched; do round-robin */ |
David S. Miller | f11e665 | 2007-03-24 20:36:25 -0700 | [diff] [blame] | 788 | if (!next || next->rt6i_metric != rt0->rt6i_metric) |
Wei Wang | 8d1040e | 2017-10-06 12:06:08 -0700 | [diff] [blame] | 789 | next = leaf; |
David S. Miller | f11e665 | 2007-03-24 20:36:25 -0700 | [diff] [blame] | 790 | |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 791 | if (next != rt0) { |
| 792 | spin_lock_bh(&leaf->rt6i_table->tb6_lock); |
| 793 | /* make sure next is not being deleted from the tree */ |
| 794 | if (next->rt6i_node) |
| 795 | rcu_assign_pointer(fn->rr_ptr, next); |
| 796 | spin_unlock_bh(&leaf->rt6i_table->tb6_lock); |
| 797 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 798 | } |
| 799 | |
Eric Dumazet | a02cec2 | 2010-09-22 20:43:57 +0000 | [diff] [blame] | 800 | return match ? match : net->ipv6.ip6_null_entry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 801 | } |
| 802 | |
Martin KaFai Lau | 8b9df26 | 2015-05-22 20:55:59 -0700 | [diff] [blame] | 803 | static bool rt6_is_gw_or_nonexthop(const struct rt6_info *rt) |
| 804 | { |
| 805 | return (rt->rt6i_flags & (RTF_NONEXTHOP | RTF_GATEWAY)); |
| 806 | } |
| 807 | |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 808 | #ifdef CONFIG_IPV6_ROUTE_INFO |
| 809 | int rt6_route_rcv(struct net_device *dev, u8 *opt, int len, |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 810 | const struct in6_addr *gwaddr) |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 811 | { |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 812 | struct net *net = dev_net(dev); |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 813 | struct route_info *rinfo = (struct route_info *) opt; |
| 814 | struct in6_addr prefix_buf, *prefix; |
| 815 | unsigned int pref; |
YOSHIFUJI Hideaki | 4bed72e | 2008-05-27 17:37:49 +0900 | [diff] [blame] | 816 | unsigned long lifetime; |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 817 | struct rt6_info *rt; |
| 818 | |
| 819 | if (len < sizeof(struct route_info)) { |
| 820 | return -EINVAL; |
| 821 | } |
| 822 | |
| 823 | /* Sanity check for prefix_len and length */ |
| 824 | if (rinfo->length > 3) { |
| 825 | return -EINVAL; |
| 826 | } else if (rinfo->prefix_len > 128) { |
| 827 | return -EINVAL; |
| 828 | } else if (rinfo->prefix_len > 64) { |
| 829 | if (rinfo->length < 2) { |
| 830 | return -EINVAL; |
| 831 | } |
| 832 | } else if (rinfo->prefix_len > 0) { |
| 833 | if (rinfo->length < 1) { |
| 834 | return -EINVAL; |
| 835 | } |
| 836 | } |
| 837 | |
| 838 | pref = rinfo->route_pref; |
| 839 | if (pref == ICMPV6_ROUTER_PREF_INVALID) |
Jens Rosenboom | 3933fc9 | 2009-09-10 06:25:11 +0000 | [diff] [blame] | 840 | return -EINVAL; |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 841 | |
YOSHIFUJI Hideaki | 4bed72e | 2008-05-27 17:37:49 +0900 | [diff] [blame] | 842 | lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ); |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 843 | |
| 844 | if (rinfo->length == 3) |
| 845 | prefix = (struct in6_addr *)rinfo->prefix; |
| 846 | else { |
| 847 | /* this function is safe */ |
| 848 | ipv6_addr_prefix(&prefix_buf, |
| 849 | (struct in6_addr *)rinfo->prefix, |
| 850 | rinfo->prefix_len); |
| 851 | prefix = &prefix_buf; |
| 852 | } |
| 853 | |
Duan Jiong | f104a56 | 2013-11-08 09:56:53 +0800 | [diff] [blame] | 854 | if (rinfo->prefix_len == 0) |
David Ahern | afb1d4b5 | 2018-04-17 17:33:11 -0700 | [diff] [blame] | 855 | rt = rt6_get_dflt_router(net, gwaddr, dev); |
Duan Jiong | f104a56 | 2013-11-08 09:56:53 +0800 | [diff] [blame] | 856 | else |
| 857 | rt = rt6_get_route_info(net, prefix, rinfo->prefix_len, |
David Ahern | 830218c | 2016-10-24 10:52:35 -0700 | [diff] [blame] | 858 | gwaddr, dev); |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 859 | |
| 860 | if (rt && !lifetime) { |
David Ahern | afb1d4b5 | 2018-04-17 17:33:11 -0700 | [diff] [blame] | 861 | ip6_del_rt(net, rt); |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 862 | rt = NULL; |
| 863 | } |
| 864 | |
| 865 | if (!rt && lifetime) |
David Ahern | 830218c | 2016-10-24 10:52:35 -0700 | [diff] [blame] | 866 | rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr, |
| 867 | dev, pref); |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 868 | else if (rt) |
| 869 | rt->rt6i_flags = RTF_ROUTEINFO | |
| 870 | (rt->rt6i_flags & ~RTF_PREF_MASK) | RTF_PREF(pref); |
| 871 | |
| 872 | if (rt) { |
Gao feng | 1716a96 | 2012-04-06 00:13:10 +0000 | [diff] [blame] | 873 | if (!addrconf_finite_timeout(lifetime)) |
David Ahern | 1489568 | 2018-04-17 17:33:17 -0700 | [diff] [blame^] | 874 | fib6_clean_expires(rt); |
Gao feng | 1716a96 | 2012-04-06 00:13:10 +0000 | [diff] [blame] | 875 | else |
David Ahern | 1489568 | 2018-04-17 17:33:17 -0700 | [diff] [blame^] | 876 | fib6_set_expires(rt, jiffies + HZ * lifetime); |
Gao feng | 1716a96 | 2012-04-06 00:13:10 +0000 | [diff] [blame] | 877 | |
Amerigo Wang | 94e187c | 2012-10-29 00:13:19 +0000 | [diff] [blame] | 878 | ip6_rt_put(rt); |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 879 | } |
| 880 | return 0; |
| 881 | } |
| 882 | #endif |
| 883 | |
David Ahern | ae90d86 | 2018-04-17 17:33:12 -0700 | [diff] [blame] | 884 | /* |
| 885 | * Misc support functions |
| 886 | */ |
| 887 | |
| 888 | /* called with rcu_lock held */ |
| 889 | static struct net_device *ip6_rt_get_dev_rcu(struct rt6_info *rt) |
| 890 | { |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 891 | struct net_device *dev = rt->fib6_nh.nh_dev; |
David Ahern | ae90d86 | 2018-04-17 17:33:12 -0700 | [diff] [blame] | 892 | |
| 893 | if (rt->rt6i_flags & (RTF_LOCAL | RTF_ANYCAST)) { |
| 894 | /* for copies of local routes, dst->dev needs to be the |
| 895 | * device if it is a master device, the master device if |
| 896 | * device is enslaved, and the loopback as the default |
| 897 | */ |
| 898 | if (netif_is_l3_slave(dev) && |
| 899 | !rt6_need_strict(&rt->rt6i_dst.addr)) |
| 900 | dev = l3mdev_master_dev_rcu(dev); |
| 901 | else if (!netif_is_l3_master(dev)) |
| 902 | dev = dev_net(dev)->loopback_dev; |
| 903 | /* last case is netif_is_l3_master(dev) is true in which |
| 904 | * case we want dev returned to be dev |
| 905 | */ |
| 906 | } |
| 907 | |
| 908 | return dev; |
| 909 | } |
| 910 | |
David Ahern | 6edb3c9 | 2018-04-17 17:33:15 -0700 | [diff] [blame] | 911 | static const int fib6_prop[RTN_MAX + 1] = { |
| 912 | [RTN_UNSPEC] = 0, |
| 913 | [RTN_UNICAST] = 0, |
| 914 | [RTN_LOCAL] = 0, |
| 915 | [RTN_BROADCAST] = 0, |
| 916 | [RTN_ANYCAST] = 0, |
| 917 | [RTN_MULTICAST] = 0, |
| 918 | [RTN_BLACKHOLE] = -EINVAL, |
| 919 | [RTN_UNREACHABLE] = -EHOSTUNREACH, |
| 920 | [RTN_PROHIBIT] = -EACCES, |
| 921 | [RTN_THROW] = -EAGAIN, |
| 922 | [RTN_NAT] = -EINVAL, |
| 923 | [RTN_XRESOLVE] = -EINVAL, |
| 924 | }; |
| 925 | |
| 926 | static int ip6_rt_type_to_error(u8 fib6_type) |
| 927 | { |
| 928 | return fib6_prop[fib6_type]; |
| 929 | } |
| 930 | |
| 931 | static void ip6_rt_init_dst_reject(struct rt6_info *rt, struct rt6_info *ort) |
| 932 | { |
| 933 | rt->dst.error = ip6_rt_type_to_error(ort->fib6_type); |
| 934 | |
| 935 | switch (ort->fib6_type) { |
| 936 | case RTN_BLACKHOLE: |
| 937 | rt->dst.output = dst_discard_out; |
| 938 | rt->dst.input = dst_discard; |
| 939 | break; |
| 940 | case RTN_PROHIBIT: |
| 941 | rt->dst.output = ip6_pkt_prohibit_out; |
| 942 | rt->dst.input = ip6_pkt_prohibit; |
| 943 | break; |
| 944 | case RTN_THROW: |
| 945 | case RTN_UNREACHABLE: |
| 946 | default: |
| 947 | rt->dst.output = ip6_pkt_discard_out; |
| 948 | rt->dst.input = ip6_pkt_discard; |
| 949 | break; |
| 950 | } |
| 951 | } |
| 952 | |
| 953 | static void ip6_rt_init_dst(struct rt6_info *rt, struct rt6_info *ort) |
| 954 | { |
| 955 | if (ort->rt6i_flags & RTF_REJECT) { |
| 956 | ip6_rt_init_dst_reject(rt, ort); |
| 957 | return; |
| 958 | } |
| 959 | |
| 960 | rt->dst.error = 0; |
| 961 | rt->dst.output = ip6_output; |
| 962 | |
| 963 | if (ort->fib6_type == RTN_LOCAL) { |
| 964 | rt->dst.flags |= DST_HOST; |
| 965 | rt->dst.input = ip6_input; |
| 966 | } else if (ipv6_addr_type(&ort->rt6i_dst.addr) & IPV6_ADDR_MULTICAST) { |
| 967 | rt->dst.input = ip6_mc_input; |
| 968 | } else { |
| 969 | rt->dst.input = ip6_forward; |
| 970 | } |
| 971 | |
| 972 | if (ort->fib6_nh.nh_lwtstate) { |
| 973 | rt->dst.lwtstate = lwtstate_get(ort->fib6_nh.nh_lwtstate); |
| 974 | lwtunnel_set_redirect(&rt->dst); |
| 975 | } |
| 976 | |
| 977 | rt->dst.lastuse = jiffies; |
| 978 | } |
| 979 | |
David Ahern | ae90d86 | 2018-04-17 17:33:12 -0700 | [diff] [blame] | 980 | static void rt6_set_from(struct rt6_info *rt, struct rt6_info *from) |
| 981 | { |
| 982 | BUG_ON(from->from); |
| 983 | |
| 984 | rt->rt6i_flags &= ~RTF_EXPIRES; |
| 985 | dst_hold(&from->dst); |
| 986 | rt->from = from; |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 987 | dst_init_metrics(&rt->dst, from->fib6_metrics->metrics, true); |
| 988 | if (from->fib6_metrics != &dst_default_metrics) { |
| 989 | rt->dst._metrics |= DST_METRICS_REFCOUNTED; |
| 990 | refcount_inc(&from->fib6_metrics->refcnt); |
| 991 | } |
David Ahern | ae90d86 | 2018-04-17 17:33:12 -0700 | [diff] [blame] | 992 | } |
| 993 | |
| 994 | static void ip6_rt_copy_init(struct rt6_info *rt, struct rt6_info *ort) |
| 995 | { |
David Ahern | 6edb3c9 | 2018-04-17 17:33:15 -0700 | [diff] [blame] | 996 | ip6_rt_init_dst(rt, ort); |
| 997 | |
David Ahern | ae90d86 | 2018-04-17 17:33:12 -0700 | [diff] [blame] | 998 | rt->rt6i_dst = ort->rt6i_dst; |
David Ahern | ae90d86 | 2018-04-17 17:33:12 -0700 | [diff] [blame] | 999 | rt->rt6i_idev = ort->rt6i_idev; |
| 1000 | if (rt->rt6i_idev) |
| 1001 | in6_dev_hold(rt->rt6i_idev); |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 1002 | rt->rt6i_gateway = ort->fib6_nh.nh_gw; |
David Ahern | ae90d86 | 2018-04-17 17:33:12 -0700 | [diff] [blame] | 1003 | rt->rt6i_flags = ort->rt6i_flags; |
| 1004 | rt6_set_from(rt, ort); |
| 1005 | rt->rt6i_metric = ort->rt6i_metric; |
| 1006 | #ifdef CONFIG_IPV6_SUBTREES |
| 1007 | rt->rt6i_src = ort->rt6i_src; |
| 1008 | #endif |
| 1009 | rt->rt6i_prefsrc = ort->rt6i_prefsrc; |
| 1010 | rt->rt6i_table = ort->rt6i_table; |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 1011 | rt->dst.lwtstate = lwtstate_get(ort->fib6_nh.nh_lwtstate); |
David Ahern | ae90d86 | 2018-04-17 17:33:12 -0700 | [diff] [blame] | 1012 | } |
| 1013 | |
Martin KaFai Lau | a3c00e4 | 2014-10-20 13:42:43 -0700 | [diff] [blame] | 1014 | static struct fib6_node* fib6_backtrack(struct fib6_node *fn, |
| 1015 | struct in6_addr *saddr) |
| 1016 | { |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 1017 | struct fib6_node *pn, *sn; |
Martin KaFai Lau | a3c00e4 | 2014-10-20 13:42:43 -0700 | [diff] [blame] | 1018 | while (1) { |
| 1019 | if (fn->fn_flags & RTN_TL_ROOT) |
| 1020 | return NULL; |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 1021 | pn = rcu_dereference(fn->parent); |
| 1022 | sn = FIB6_SUBTREE(pn); |
| 1023 | if (sn && sn != fn) |
| 1024 | fn = fib6_lookup(sn, NULL, saddr); |
Martin KaFai Lau | a3c00e4 | 2014-10-20 13:42:43 -0700 | [diff] [blame] | 1025 | else |
| 1026 | fn = pn; |
| 1027 | if (fn->fn_flags & RTN_RTINFO) |
| 1028 | return fn; |
| 1029 | } |
| 1030 | } |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1031 | |
Wei Wang | d3843fe | 2017-10-06 12:06:06 -0700 | [diff] [blame] | 1032 | static bool ip6_hold_safe(struct net *net, struct rt6_info **prt, |
| 1033 | bool null_fallback) |
| 1034 | { |
| 1035 | struct rt6_info *rt = *prt; |
| 1036 | |
| 1037 | if (dst_hold_safe(&rt->dst)) |
| 1038 | return true; |
| 1039 | if (null_fallback) { |
| 1040 | rt = net->ipv6.ip6_null_entry; |
| 1041 | dst_hold(&rt->dst); |
| 1042 | } else { |
| 1043 | rt = NULL; |
| 1044 | } |
| 1045 | *prt = rt; |
| 1046 | return false; |
| 1047 | } |
| 1048 | |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 1049 | static struct rt6_info *ip6_pol_route_lookup(struct net *net, |
| 1050 | struct fib6_table *table, |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 1051 | struct flowi6 *fl6, |
| 1052 | const struct sk_buff *skb, |
| 1053 | int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1054 | { |
Wei Wang | 2b760fc | 2017-10-06 12:06:03 -0700 | [diff] [blame] | 1055 | struct rt6_info *rt, *rt_cache; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1056 | struct fib6_node *fn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1057 | |
David Ahern | b6cdbc8 | 2018-03-29 17:44:57 -0700 | [diff] [blame] | 1058 | if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF) |
| 1059 | flags &= ~RT6_LOOKUP_F_IFACE; |
| 1060 | |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 1061 | rcu_read_lock(); |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 1062 | fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr); |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1063 | restart: |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 1064 | rt = rcu_dereference(fn->leaf); |
| 1065 | if (!rt) { |
| 1066 | rt = net->ipv6.ip6_null_entry; |
| 1067 | } else { |
| 1068 | rt = rt6_device_match(net, rt, &fl6->saddr, |
| 1069 | fl6->flowi6_oif, flags); |
| 1070 | if (rt->rt6i_nsiblings && fl6->flowi6_oif == 0) |
David Ahern | b4bac17 | 2018-03-02 08:32:18 -0800 | [diff] [blame] | 1071 | rt = rt6_multipath_select(net, rt, fl6, fl6->flowi6_oif, |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 1072 | skb, flags); |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 1073 | } |
Martin KaFai Lau | a3c00e4 | 2014-10-20 13:42:43 -0700 | [diff] [blame] | 1074 | if (rt == net->ipv6.ip6_null_entry) { |
| 1075 | fn = fib6_backtrack(fn, &fl6->saddr); |
| 1076 | if (fn) |
| 1077 | goto restart; |
| 1078 | } |
Wei Wang | 2b760fc | 2017-10-06 12:06:03 -0700 | [diff] [blame] | 1079 | /* Search through exception table */ |
| 1080 | rt_cache = rt6_find_cached_rt(rt, &fl6->daddr, &fl6->saddr); |
| 1081 | if (rt_cache) |
| 1082 | rt = rt_cache; |
| 1083 | |
Wei Wang | d3843fe | 2017-10-06 12:06:06 -0700 | [diff] [blame] | 1084 | if (ip6_hold_safe(net, &rt, true)) |
| 1085 | dst_use_noref(&rt->dst, jiffies); |
| 1086 | |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 1087 | rcu_read_unlock(); |
David Ahern | b811580 | 2015-11-19 12:24:22 -0800 | [diff] [blame] | 1088 | |
Paolo Abeni | b65f164 | 2017-10-19 09:31:43 +0200 | [diff] [blame] | 1089 | trace_fib6_table_lookup(net, rt, table, fl6); |
David Ahern | b811580 | 2015-11-19 12:24:22 -0800 | [diff] [blame] | 1090 | |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1091 | return rt; |
| 1092 | |
| 1093 | } |
| 1094 | |
Ian Morris | 67ba415 | 2014-08-24 21:53:10 +0100 | [diff] [blame] | 1095 | struct dst_entry *ip6_route_lookup(struct net *net, struct flowi6 *fl6, |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 1096 | const struct sk_buff *skb, int flags) |
Florian Westphal | ea6e574 | 2011-09-05 16:05:44 +0200 | [diff] [blame] | 1097 | { |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 1098 | return fib6_rule_lookup(net, fl6, skb, flags, ip6_pol_route_lookup); |
Florian Westphal | ea6e574 | 2011-09-05 16:05:44 +0200 | [diff] [blame] | 1099 | } |
| 1100 | EXPORT_SYMBOL_GPL(ip6_route_lookup); |
| 1101 | |
YOSHIFUJI Hideaki | 9acd9f3 | 2008-04-10 15:42:10 +0900 | [diff] [blame] | 1102 | 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] | 1103 | const struct in6_addr *saddr, int oif, |
| 1104 | const struct sk_buff *skb, int strict) |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1105 | { |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 1106 | struct flowi6 fl6 = { |
| 1107 | .flowi6_oif = oif, |
| 1108 | .daddr = *daddr, |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1109 | }; |
| 1110 | struct dst_entry *dst; |
YOSHIFUJI Hideaki | 77d16f4 | 2006-08-23 17:25:05 -0700 | [diff] [blame] | 1111 | int flags = strict ? RT6_LOOKUP_F_IFACE : 0; |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1112 | |
Thomas Graf | adaa70b | 2006-10-13 15:01:03 -0700 | [diff] [blame] | 1113 | if (saddr) { |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 1114 | memcpy(&fl6.saddr, saddr, sizeof(*saddr)); |
Thomas Graf | adaa70b | 2006-10-13 15:01:03 -0700 | [diff] [blame] | 1115 | flags |= RT6_LOOKUP_F_HAS_SADDR; |
| 1116 | } |
| 1117 | |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 1118 | dst = fib6_rule_lookup(net, &fl6, skb, flags, ip6_pol_route_lookup); |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1119 | if (dst->error == 0) |
| 1120 | return (struct rt6_info *) dst; |
| 1121 | |
| 1122 | dst_release(dst); |
| 1123 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1124 | return NULL; |
| 1125 | } |
YOSHIFUJI Hideaki | 7159039 | 2007-02-22 22:05:40 +0900 | [diff] [blame] | 1126 | EXPORT_SYMBOL(rt6_lookup); |
| 1127 | |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1128 | /* ip6_ins_rt is called with FREE table->tb6_lock. |
Wei Wang | 1cfb71e | 2017-06-17 10:42:33 -0700 | [diff] [blame] | 1129 | * It takes new route entry, the addition fails by any reason the |
| 1130 | * route is released. |
| 1131 | * Caller must hold dst before calling it. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1132 | */ |
| 1133 | |
Michal Kubeček | e5fd387 | 2014-03-27 13:04:08 +0100 | [diff] [blame] | 1134 | static int __ip6_ins_rt(struct rt6_info *rt, struct nl_info *info, |
David Ahern | 333c430 | 2017-05-21 10:12:04 -0600 | [diff] [blame] | 1135 | struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1136 | { |
| 1137 | int err; |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1138 | struct fib6_table *table; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1139 | |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1140 | table = rt->rt6i_table; |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 1141 | spin_lock_bh(&table->tb6_lock); |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 1142 | err = fib6_add(&table->tb6_root, rt, info, extack); |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 1143 | spin_unlock_bh(&table->tb6_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1144 | |
| 1145 | return err; |
| 1146 | } |
| 1147 | |
David Ahern | afb1d4b5 | 2018-04-17 17:33:11 -0700 | [diff] [blame] | 1148 | int ip6_ins_rt(struct net *net, struct rt6_info *rt) |
Thomas Graf | 40e22e8 | 2006-08-22 00:00:45 -0700 | [diff] [blame] | 1149 | { |
David Ahern | afb1d4b5 | 2018-04-17 17:33:11 -0700 | [diff] [blame] | 1150 | struct nl_info info = { .nl_net = net, }; |
Florian Westphal | e715b6d | 2015-01-05 23:57:44 +0100 | [diff] [blame] | 1151 | |
Wei Wang | 1cfb71e | 2017-06-17 10:42:33 -0700 | [diff] [blame] | 1152 | /* Hold dst to account for the reference from the fib6 tree */ |
| 1153 | dst_hold(&rt->dst); |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 1154 | return __ip6_ins_rt(rt, &info, NULL); |
Thomas Graf | 40e22e8 | 2006-08-22 00:00:45 -0700 | [diff] [blame] | 1155 | } |
| 1156 | |
Martin KaFai Lau | 8b9df26 | 2015-05-22 20:55:59 -0700 | [diff] [blame] | 1157 | static struct rt6_info *ip6_rt_cache_alloc(struct rt6_info *ort, |
| 1158 | const struct in6_addr *daddr, |
| 1159 | const struct in6_addr *saddr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1160 | { |
David Ahern | 4832c30 | 2017-08-17 12:17:20 -0700 | [diff] [blame] | 1161 | struct net_device *dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1162 | struct rt6_info *rt; |
| 1163 | |
| 1164 | /* |
| 1165 | * Clone the route. |
| 1166 | */ |
| 1167 | |
Martin KaFai Lau | d52d399 | 2015-05-22 20:56:06 -0700 | [diff] [blame] | 1168 | if (ort->rt6i_flags & (RTF_CACHE | RTF_PCPU)) |
David Miller | 3a2232e | 2017-11-28 15:40:40 -0500 | [diff] [blame] | 1169 | ort = ort->from; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1170 | |
David Ahern | 4832c30 | 2017-08-17 12:17:20 -0700 | [diff] [blame] | 1171 | rcu_read_lock(); |
| 1172 | dev = ip6_rt_get_dev_rcu(ort); |
| 1173 | rt = __ip6_dst_alloc(dev_net(dev), dev, 0); |
| 1174 | rcu_read_unlock(); |
Martin KaFai Lau | 83a09ab | 2015-05-22 20:56:05 -0700 | [diff] [blame] | 1175 | if (!rt) |
| 1176 | return NULL; |
| 1177 | |
| 1178 | ip6_rt_copy_init(rt, ort); |
| 1179 | rt->rt6i_flags |= RTF_CACHE; |
| 1180 | rt->rt6i_metric = 0; |
| 1181 | rt->dst.flags |= DST_HOST; |
| 1182 | rt->rt6i_dst.addr = *daddr; |
| 1183 | rt->rt6i_dst.plen = 128; |
| 1184 | |
| 1185 | if (!rt6_is_gw_or_nonexthop(ort)) { |
| 1186 | if (ort->rt6i_dst.plen != 128 && |
| 1187 | ipv6_addr_equal(&ort->rt6i_dst.addr, daddr)) |
| 1188 | rt->rt6i_flags |= RTF_ANYCAST; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1189 | #ifdef CONFIG_IPV6_SUBTREES |
Martin KaFai Lau | 83a09ab | 2015-05-22 20:56:05 -0700 | [diff] [blame] | 1190 | if (rt->rt6i_src.plen && saddr) { |
| 1191 | rt->rt6i_src.addr = *saddr; |
| 1192 | rt->rt6i_src.plen = 128; |
Martin KaFai Lau | 8b9df26 | 2015-05-22 20:55:59 -0700 | [diff] [blame] | 1193 | } |
Martin KaFai Lau | 83a09ab | 2015-05-22 20:56:05 -0700 | [diff] [blame] | 1194 | #endif |
YOSHIFUJI Hideaki | 95a9a5b | 2006-03-20 16:55:51 -0800 | [diff] [blame] | 1195 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1196 | |
YOSHIFUJI Hideaki | 95a9a5b | 2006-03-20 16:55:51 -0800 | [diff] [blame] | 1197 | return rt; |
| 1198 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1199 | |
Martin KaFai Lau | d52d399 | 2015-05-22 20:56:06 -0700 | [diff] [blame] | 1200 | static struct rt6_info *ip6_rt_pcpu_alloc(struct rt6_info *rt) |
| 1201 | { |
David Ahern | 4832c30 | 2017-08-17 12:17:20 -0700 | [diff] [blame] | 1202 | struct net_device *dev; |
Martin KaFai Lau | d52d399 | 2015-05-22 20:56:06 -0700 | [diff] [blame] | 1203 | struct rt6_info *pcpu_rt; |
| 1204 | |
David Ahern | 4832c30 | 2017-08-17 12:17:20 -0700 | [diff] [blame] | 1205 | rcu_read_lock(); |
| 1206 | dev = ip6_rt_get_dev_rcu(rt); |
| 1207 | pcpu_rt = __ip6_dst_alloc(dev_net(dev), dev, rt->dst.flags); |
| 1208 | rcu_read_unlock(); |
Martin KaFai Lau | d52d399 | 2015-05-22 20:56:06 -0700 | [diff] [blame] | 1209 | if (!pcpu_rt) |
| 1210 | return NULL; |
| 1211 | ip6_rt_copy_init(pcpu_rt, rt); |
| 1212 | pcpu_rt->rt6i_protocol = rt->rt6i_protocol; |
| 1213 | pcpu_rt->rt6i_flags |= RTF_PCPU; |
| 1214 | return pcpu_rt; |
| 1215 | } |
| 1216 | |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 1217 | /* It should be called with rcu_read_lock() acquired */ |
Martin KaFai Lau | d52d399 | 2015-05-22 20:56:06 -0700 | [diff] [blame] | 1218 | static struct rt6_info *rt6_get_pcpu_route(struct rt6_info *rt) |
| 1219 | { |
Martin KaFai Lau | a73e419 | 2015-08-14 11:05:53 -0700 | [diff] [blame] | 1220 | struct rt6_info *pcpu_rt, **p; |
Martin KaFai Lau | d52d399 | 2015-05-22 20:56:06 -0700 | [diff] [blame] | 1221 | |
| 1222 | p = this_cpu_ptr(rt->rt6i_pcpu); |
| 1223 | pcpu_rt = *p; |
| 1224 | |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 1225 | if (pcpu_rt) |
| 1226 | ip6_hold_safe(NULL, &pcpu_rt, false); |
Wei Wang | d3843fe | 2017-10-06 12:06:06 -0700 | [diff] [blame] | 1227 | |
Martin KaFai Lau | a73e419 | 2015-08-14 11:05:53 -0700 | [diff] [blame] | 1228 | return pcpu_rt; |
| 1229 | } |
| 1230 | |
David Ahern | afb1d4b5 | 2018-04-17 17:33:11 -0700 | [diff] [blame] | 1231 | static struct rt6_info *rt6_make_pcpu_route(struct net *net, |
| 1232 | struct rt6_info *rt) |
Martin KaFai Lau | a73e419 | 2015-08-14 11:05:53 -0700 | [diff] [blame] | 1233 | { |
| 1234 | struct rt6_info *pcpu_rt, *prev, **p; |
Martin KaFai Lau | d52d399 | 2015-05-22 20:56:06 -0700 | [diff] [blame] | 1235 | |
| 1236 | pcpu_rt = ip6_rt_pcpu_alloc(rt); |
| 1237 | if (!pcpu_rt) { |
Martin KaFai Lau | 9c7370a | 2015-08-14 11:05:54 -0700 | [diff] [blame] | 1238 | dst_hold(&net->ipv6.ip6_null_entry->dst); |
| 1239 | return net->ipv6.ip6_null_entry; |
Martin KaFai Lau | d52d399 | 2015-05-22 20:56:06 -0700 | [diff] [blame] | 1240 | } |
| 1241 | |
Martin KaFai Lau | d52d399 | 2015-05-22 20:56:06 -0700 | [diff] [blame] | 1242 | dst_hold(&pcpu_rt->dst); |
Wei Wang | a94b936 | 2017-10-06 12:06:04 -0700 | [diff] [blame] | 1243 | p = this_cpu_ptr(rt->rt6i_pcpu); |
| 1244 | prev = cmpxchg(p, NULL, pcpu_rt); |
Eric Dumazet | 951f788 | 2017-10-08 21:07:18 -0700 | [diff] [blame] | 1245 | BUG_ON(prev); |
Wei Wang | a94b936 | 2017-10-06 12:06:04 -0700 | [diff] [blame] | 1246 | |
Martin KaFai Lau | d52d399 | 2015-05-22 20:56:06 -0700 | [diff] [blame] | 1247 | return pcpu_rt; |
| 1248 | } |
| 1249 | |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1250 | /* exception hash table implementation |
| 1251 | */ |
| 1252 | static DEFINE_SPINLOCK(rt6_exception_lock); |
| 1253 | |
| 1254 | /* Remove rt6_ex from hash table and free the memory |
| 1255 | * Caller must hold rt6_exception_lock |
| 1256 | */ |
| 1257 | static void rt6_remove_exception(struct rt6_exception_bucket *bucket, |
| 1258 | struct rt6_exception *rt6_ex) |
| 1259 | { |
Colin Ian King | b2427e6 | 2017-10-10 18:01:16 +0100 | [diff] [blame] | 1260 | struct net *net; |
Wei Wang | 81eb844 | 2017-10-06 12:06:11 -0700 | [diff] [blame] | 1261 | |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1262 | if (!bucket || !rt6_ex) |
| 1263 | return; |
Colin Ian King | b2427e6 | 2017-10-10 18:01:16 +0100 | [diff] [blame] | 1264 | |
| 1265 | net = dev_net(rt6_ex->rt6i->dst.dev); |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1266 | rt6_ex->rt6i->rt6i_node = NULL; |
| 1267 | hlist_del_rcu(&rt6_ex->hlist); |
| 1268 | rt6_release(rt6_ex->rt6i); |
| 1269 | kfree_rcu(rt6_ex, rcu); |
| 1270 | WARN_ON_ONCE(!bucket->depth); |
| 1271 | bucket->depth--; |
Wei Wang | 81eb844 | 2017-10-06 12:06:11 -0700 | [diff] [blame] | 1272 | net->ipv6.rt6_stats->fib_rt_cache--; |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1273 | } |
| 1274 | |
| 1275 | /* Remove oldest rt6_ex in bucket and free the memory |
| 1276 | * Caller must hold rt6_exception_lock |
| 1277 | */ |
| 1278 | static void rt6_exception_remove_oldest(struct rt6_exception_bucket *bucket) |
| 1279 | { |
| 1280 | struct rt6_exception *rt6_ex, *oldest = NULL; |
| 1281 | |
| 1282 | if (!bucket) |
| 1283 | return; |
| 1284 | |
| 1285 | hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) { |
| 1286 | if (!oldest || time_before(rt6_ex->stamp, oldest->stamp)) |
| 1287 | oldest = rt6_ex; |
| 1288 | } |
| 1289 | rt6_remove_exception(bucket, oldest); |
| 1290 | } |
| 1291 | |
| 1292 | static u32 rt6_exception_hash(const struct in6_addr *dst, |
| 1293 | const struct in6_addr *src) |
| 1294 | { |
| 1295 | static u32 seed __read_mostly; |
| 1296 | u32 val; |
| 1297 | |
| 1298 | net_get_random_once(&seed, sizeof(seed)); |
| 1299 | val = jhash(dst, sizeof(*dst), seed); |
| 1300 | |
| 1301 | #ifdef CONFIG_IPV6_SUBTREES |
| 1302 | if (src) |
| 1303 | val = jhash(src, sizeof(*src), val); |
| 1304 | #endif |
| 1305 | return hash_32(val, FIB6_EXCEPTION_BUCKET_SIZE_SHIFT); |
| 1306 | } |
| 1307 | |
| 1308 | /* Helper function to find the cached rt in the hash table |
| 1309 | * and update bucket pointer to point to the bucket for this |
| 1310 | * (daddr, saddr) pair |
| 1311 | * Caller must hold rt6_exception_lock |
| 1312 | */ |
| 1313 | static struct rt6_exception * |
| 1314 | __rt6_find_exception_spinlock(struct rt6_exception_bucket **bucket, |
| 1315 | const struct in6_addr *daddr, |
| 1316 | const struct in6_addr *saddr) |
| 1317 | { |
| 1318 | struct rt6_exception *rt6_ex; |
| 1319 | u32 hval; |
| 1320 | |
| 1321 | if (!(*bucket) || !daddr) |
| 1322 | return NULL; |
| 1323 | |
| 1324 | hval = rt6_exception_hash(daddr, saddr); |
| 1325 | *bucket += hval; |
| 1326 | |
| 1327 | hlist_for_each_entry(rt6_ex, &(*bucket)->chain, hlist) { |
| 1328 | struct rt6_info *rt6 = rt6_ex->rt6i; |
| 1329 | bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr); |
| 1330 | |
| 1331 | #ifdef CONFIG_IPV6_SUBTREES |
| 1332 | if (matched && saddr) |
| 1333 | matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr); |
| 1334 | #endif |
| 1335 | if (matched) |
| 1336 | return rt6_ex; |
| 1337 | } |
| 1338 | return NULL; |
| 1339 | } |
| 1340 | |
| 1341 | /* Helper function to find the cached rt in the hash table |
| 1342 | * and update bucket pointer to point to the bucket for this |
| 1343 | * (daddr, saddr) pair |
| 1344 | * Caller must hold rcu_read_lock() |
| 1345 | */ |
| 1346 | static struct rt6_exception * |
| 1347 | __rt6_find_exception_rcu(struct rt6_exception_bucket **bucket, |
| 1348 | const struct in6_addr *daddr, |
| 1349 | const struct in6_addr *saddr) |
| 1350 | { |
| 1351 | struct rt6_exception *rt6_ex; |
| 1352 | u32 hval; |
| 1353 | |
| 1354 | WARN_ON_ONCE(!rcu_read_lock_held()); |
| 1355 | |
| 1356 | if (!(*bucket) || !daddr) |
| 1357 | return NULL; |
| 1358 | |
| 1359 | hval = rt6_exception_hash(daddr, saddr); |
| 1360 | *bucket += hval; |
| 1361 | |
| 1362 | hlist_for_each_entry_rcu(rt6_ex, &(*bucket)->chain, hlist) { |
| 1363 | struct rt6_info *rt6 = rt6_ex->rt6i; |
| 1364 | bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr); |
| 1365 | |
| 1366 | #ifdef CONFIG_IPV6_SUBTREES |
| 1367 | if (matched && saddr) |
| 1368 | matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr); |
| 1369 | #endif |
| 1370 | if (matched) |
| 1371 | return rt6_ex; |
| 1372 | } |
| 1373 | return NULL; |
| 1374 | } |
| 1375 | |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 1376 | static unsigned int fib6_mtu(const struct rt6_info *rt) |
| 1377 | { |
| 1378 | unsigned int mtu; |
| 1379 | |
| 1380 | mtu = rt->fib6_pmtu ? : rt->rt6i_idev->cnf.mtu6; |
| 1381 | mtu = min_t(unsigned int, mtu, IP6_MAX_MTU); |
| 1382 | |
| 1383 | return mtu - lwtunnel_headroom(rt->fib6_nh.nh_lwtstate, mtu); |
| 1384 | } |
| 1385 | |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1386 | static int rt6_insert_exception(struct rt6_info *nrt, |
| 1387 | struct rt6_info *ort) |
| 1388 | { |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 1389 | struct net *net = dev_net(nrt->dst.dev); |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1390 | struct rt6_exception_bucket *bucket; |
| 1391 | struct in6_addr *src_key = NULL; |
| 1392 | struct rt6_exception *rt6_ex; |
| 1393 | int err = 0; |
| 1394 | |
| 1395 | /* ort can't be a cache or pcpu route */ |
| 1396 | if (ort->rt6i_flags & (RTF_CACHE | RTF_PCPU)) |
David Miller | 3a2232e | 2017-11-28 15:40:40 -0500 | [diff] [blame] | 1397 | ort = ort->from; |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1398 | WARN_ON_ONCE(ort->rt6i_flags & (RTF_CACHE | RTF_PCPU)); |
| 1399 | |
| 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 | */ |
| 1426 | if (ort->rt6i_src.plen) |
| 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 | */ |
| 1433 | nrt->rt6i_prefsrc = ort->rt6i_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; |
| 1455 | atomic_inc(&nrt->rt6i_ref); |
| 1456 | nrt->rt6i_node = ort->rt6i_node; |
| 1457 | hlist_add_head_rcu(&rt6_ex->hlist, &bucket->chain); |
| 1458 | bucket->depth++; |
Wei Wang | 81eb844 | 2017-10-06 12:06:11 -0700 | [diff] [blame] | 1459 | net->ipv6.rt6_stats->fib_rt_cache++; |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1460 | |
| 1461 | if (bucket->depth > FIB6_MAX_DEPTH) |
| 1462 | rt6_exception_remove_oldest(bucket); |
| 1463 | |
| 1464 | out: |
| 1465 | spin_unlock_bh(&rt6_exception_lock); |
| 1466 | |
| 1467 | /* Update fn->fn_sernum to invalidate all cached dst */ |
Paolo Abeni | b886d5f | 2017-10-19 16:07:10 +0200 | [diff] [blame] | 1468 | if (!err) { |
Ido Schimmel | 922c2ac | 2018-01-07 12:45:14 +0200 | [diff] [blame] | 1469 | spin_lock_bh(&ort->rt6i_table->tb6_lock); |
David Ahern | 7aef685 | 2018-04-17 17:33:10 -0700 | [diff] [blame] | 1470 | fib6_update_sernum(net, ort); |
Ido Schimmel | 922c2ac | 2018-01-07 12:45:14 +0200 | [diff] [blame] | 1471 | spin_unlock_bh(&ort->rt6i_table->tb6_lock); |
Paolo Abeni | b886d5f | 2017-10-19 16:07:10 +0200 | [diff] [blame] | 1472 | fib6_force_start_gc(net); |
| 1473 | } |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1474 | |
| 1475 | return err; |
| 1476 | } |
| 1477 | |
| 1478 | void rt6_flush_exceptions(struct rt6_info *rt) |
| 1479 | { |
| 1480 | struct rt6_exception_bucket *bucket; |
| 1481 | struct rt6_exception *rt6_ex; |
| 1482 | struct hlist_node *tmp; |
| 1483 | int i; |
| 1484 | |
| 1485 | spin_lock_bh(&rt6_exception_lock); |
| 1486 | /* Prevent rt6_insert_exception() to recreate the bucket list */ |
| 1487 | rt->exception_bucket_flushed = 1; |
| 1488 | |
| 1489 | bucket = rcu_dereference_protected(rt->rt6i_exception_bucket, |
| 1490 | lockdep_is_held(&rt6_exception_lock)); |
| 1491 | if (!bucket) |
| 1492 | goto out; |
| 1493 | |
| 1494 | for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) { |
| 1495 | hlist_for_each_entry_safe(rt6_ex, tmp, &bucket->chain, hlist) |
| 1496 | rt6_remove_exception(bucket, rt6_ex); |
| 1497 | WARN_ON_ONCE(bucket->depth); |
| 1498 | bucket++; |
| 1499 | } |
| 1500 | |
| 1501 | out: |
| 1502 | spin_unlock_bh(&rt6_exception_lock); |
| 1503 | } |
| 1504 | |
| 1505 | /* Find cached rt in the hash table inside passed in rt |
| 1506 | * Caller has to hold rcu_read_lock() |
| 1507 | */ |
| 1508 | static struct rt6_info *rt6_find_cached_rt(struct rt6_info *rt, |
| 1509 | struct in6_addr *daddr, |
| 1510 | struct in6_addr *saddr) |
| 1511 | { |
| 1512 | struct rt6_exception_bucket *bucket; |
| 1513 | struct in6_addr *src_key = NULL; |
| 1514 | struct rt6_exception *rt6_ex; |
| 1515 | struct rt6_info *res = NULL; |
| 1516 | |
| 1517 | bucket = rcu_dereference(rt->rt6i_exception_bucket); |
| 1518 | |
| 1519 | #ifdef CONFIG_IPV6_SUBTREES |
| 1520 | /* rt6i_src.plen != 0 indicates rt is in subtree |
| 1521 | * and exception table is indexed by a hash of |
| 1522 | * both rt6i_dst and rt6i_src. |
| 1523 | * Otherwise, the exception table is indexed by |
| 1524 | * a hash of only rt6i_dst. |
| 1525 | */ |
| 1526 | if (rt->rt6i_src.plen) |
| 1527 | src_key = saddr; |
| 1528 | #endif |
| 1529 | rt6_ex = __rt6_find_exception_rcu(&bucket, daddr, src_key); |
| 1530 | |
| 1531 | if (rt6_ex && !rt6_check_expired(rt6_ex->rt6i)) |
| 1532 | res = rt6_ex->rt6i; |
| 1533 | |
| 1534 | return res; |
| 1535 | } |
| 1536 | |
| 1537 | /* Remove the passed in cached rt from the hash table that contains it */ |
| 1538 | int rt6_remove_exception_rt(struct rt6_info *rt) |
| 1539 | { |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1540 | struct rt6_exception_bucket *bucket; |
David Miller | 3a2232e | 2017-11-28 15:40:40 -0500 | [diff] [blame] | 1541 | struct rt6_info *from = rt->from; |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1542 | struct in6_addr *src_key = NULL; |
| 1543 | struct rt6_exception *rt6_ex; |
| 1544 | int err; |
| 1545 | |
| 1546 | if (!from || |
Colin Ian King | 442d713 | 2017-10-10 19:10:30 +0100 | [diff] [blame] | 1547 | !(rt->rt6i_flags & RTF_CACHE)) |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1548 | return -EINVAL; |
| 1549 | |
| 1550 | if (!rcu_access_pointer(from->rt6i_exception_bucket)) |
| 1551 | return -ENOENT; |
| 1552 | |
| 1553 | spin_lock_bh(&rt6_exception_lock); |
| 1554 | bucket = rcu_dereference_protected(from->rt6i_exception_bucket, |
| 1555 | lockdep_is_held(&rt6_exception_lock)); |
| 1556 | #ifdef CONFIG_IPV6_SUBTREES |
| 1557 | /* rt6i_src.plen != 0 indicates 'from' is in subtree |
| 1558 | * and exception table is indexed by a hash of |
| 1559 | * both rt6i_dst and rt6i_src. |
| 1560 | * Otherwise, the exception table is indexed by |
| 1561 | * a hash of only rt6i_dst. |
| 1562 | */ |
| 1563 | if (from->rt6i_src.plen) |
| 1564 | src_key = &rt->rt6i_src.addr; |
| 1565 | #endif |
| 1566 | rt6_ex = __rt6_find_exception_spinlock(&bucket, |
| 1567 | &rt->rt6i_dst.addr, |
| 1568 | src_key); |
| 1569 | if (rt6_ex) { |
| 1570 | rt6_remove_exception(bucket, rt6_ex); |
| 1571 | err = 0; |
| 1572 | } else { |
| 1573 | err = -ENOENT; |
| 1574 | } |
| 1575 | |
| 1576 | spin_unlock_bh(&rt6_exception_lock); |
| 1577 | return err; |
| 1578 | } |
| 1579 | |
| 1580 | /* Find rt6_ex which contains the passed in rt cache and |
| 1581 | * refresh its stamp |
| 1582 | */ |
| 1583 | static void rt6_update_exception_stamp_rt(struct rt6_info *rt) |
| 1584 | { |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1585 | struct rt6_exception_bucket *bucket; |
David Miller | 3a2232e | 2017-11-28 15:40:40 -0500 | [diff] [blame] | 1586 | struct rt6_info *from = rt->from; |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1587 | struct in6_addr *src_key = NULL; |
| 1588 | struct rt6_exception *rt6_ex; |
| 1589 | |
| 1590 | if (!from || |
Colin Ian King | 442d713 | 2017-10-10 19:10:30 +0100 | [diff] [blame] | 1591 | !(rt->rt6i_flags & RTF_CACHE)) |
Wei Wang | 35732d0 | 2017-10-06 12:05:57 -0700 | [diff] [blame] | 1592 | return; |
| 1593 | |
| 1594 | rcu_read_lock(); |
| 1595 | bucket = rcu_dereference(from->rt6i_exception_bucket); |
| 1596 | |
| 1597 | #ifdef CONFIG_IPV6_SUBTREES |
| 1598 | /* rt6i_src.plen != 0 indicates 'from' is in subtree |
| 1599 | * and exception table is indexed by a hash of |
| 1600 | * both rt6i_dst and rt6i_src. |
| 1601 | * Otherwise, the exception table is indexed by |
| 1602 | * a hash of only rt6i_dst. |
| 1603 | */ |
| 1604 | if (from->rt6i_src.plen) |
| 1605 | src_key = &rt->rt6i_src.addr; |
| 1606 | #endif |
| 1607 | rt6_ex = __rt6_find_exception_rcu(&bucket, |
| 1608 | &rt->rt6i_dst.addr, |
| 1609 | src_key); |
| 1610 | if (rt6_ex) |
| 1611 | rt6_ex->stamp = jiffies; |
| 1612 | |
| 1613 | rcu_read_unlock(); |
| 1614 | } |
| 1615 | |
Wei Wang | 60006a4 | 2017-10-06 12:05:58 -0700 | [diff] [blame] | 1616 | static void rt6_exceptions_remove_prefsrc(struct rt6_info *rt) |
| 1617 | { |
| 1618 | struct rt6_exception_bucket *bucket; |
| 1619 | struct rt6_exception *rt6_ex; |
| 1620 | int i; |
| 1621 | |
| 1622 | bucket = rcu_dereference_protected(rt->rt6i_exception_bucket, |
| 1623 | lockdep_is_held(&rt6_exception_lock)); |
| 1624 | |
| 1625 | if (bucket) { |
| 1626 | for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) { |
| 1627 | hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) { |
| 1628 | rt6_ex->rt6i->rt6i_prefsrc.plen = 0; |
| 1629 | } |
| 1630 | bucket++; |
| 1631 | } |
| 1632 | } |
| 1633 | } |
| 1634 | |
Stefano Brivio | e9fa149 | 2018-03-06 11:10:19 +0100 | [diff] [blame] | 1635 | static bool rt6_mtu_change_route_allowed(struct inet6_dev *idev, |
| 1636 | struct rt6_info *rt, int mtu) |
| 1637 | { |
| 1638 | /* If the new MTU is lower than the route PMTU, this new MTU will be the |
| 1639 | * lowest MTU in the path: always allow updating the route PMTU to |
| 1640 | * reflect PMTU decreases. |
| 1641 | * |
| 1642 | * If the new MTU is higher, and the route PMTU is equal to the local |
| 1643 | * MTU, this means the old MTU is the lowest in the path, so allow |
| 1644 | * updating it: if other nodes now have lower MTUs, PMTU discovery will |
| 1645 | * handle this. |
| 1646 | */ |
| 1647 | |
| 1648 | if (dst_mtu(&rt->dst) >= mtu) |
| 1649 | return true; |
| 1650 | |
| 1651 | if (dst_mtu(&rt->dst) == idev->cnf.mtu6) |
| 1652 | return true; |
| 1653 | |
| 1654 | return false; |
| 1655 | } |
| 1656 | |
| 1657 | static void rt6_exceptions_update_pmtu(struct inet6_dev *idev, |
| 1658 | struct rt6_info *rt, int mtu) |
Wei Wang | f5bbe7e | 2017-10-06 12:05:59 -0700 | [diff] [blame] | 1659 | { |
| 1660 | struct rt6_exception_bucket *bucket; |
| 1661 | struct rt6_exception *rt6_ex; |
| 1662 | int i; |
| 1663 | |
| 1664 | bucket = rcu_dereference_protected(rt->rt6i_exception_bucket, |
| 1665 | lockdep_is_held(&rt6_exception_lock)); |
| 1666 | |
Stefano Brivio | e9fa149 | 2018-03-06 11:10:19 +0100 | [diff] [blame] | 1667 | if (!bucket) |
| 1668 | return; |
| 1669 | |
| 1670 | for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) { |
| 1671 | hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) { |
| 1672 | struct rt6_info *entry = rt6_ex->rt6i; |
| 1673 | |
| 1674 | /* For RTF_CACHE with rt6i_pmtu == 0 (i.e. a redirected |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 1675 | * route), the metrics of its rt->from have already |
Stefano Brivio | e9fa149 | 2018-03-06 11:10:19 +0100 | [diff] [blame] | 1676 | * been updated. |
| 1677 | */ |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 1678 | if (dst_metric_raw(&entry->dst, RTAX_MTU) && |
Stefano Brivio | e9fa149 | 2018-03-06 11:10:19 +0100 | [diff] [blame] | 1679 | rt6_mtu_change_route_allowed(idev, entry, mtu)) |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 1680 | dst_metric_set(&entry->dst, RTAX_MTU, mtu); |
Wei Wang | f5bbe7e | 2017-10-06 12:05:59 -0700 | [diff] [blame] | 1681 | } |
Stefano Brivio | e9fa149 | 2018-03-06 11:10:19 +0100 | [diff] [blame] | 1682 | bucket++; |
Wei Wang | f5bbe7e | 2017-10-06 12:05:59 -0700 | [diff] [blame] | 1683 | } |
| 1684 | } |
| 1685 | |
Wei Wang | b16cb45 | 2017-10-06 12:06:00 -0700 | [diff] [blame] | 1686 | #define RTF_CACHE_GATEWAY (RTF_GATEWAY | RTF_CACHE) |
| 1687 | |
| 1688 | static void rt6_exceptions_clean_tohost(struct rt6_info *rt, |
| 1689 | struct in6_addr *gateway) |
| 1690 | { |
| 1691 | struct rt6_exception_bucket *bucket; |
| 1692 | struct rt6_exception *rt6_ex; |
| 1693 | struct hlist_node *tmp; |
| 1694 | int i; |
| 1695 | |
| 1696 | if (!rcu_access_pointer(rt->rt6i_exception_bucket)) |
| 1697 | return; |
| 1698 | |
| 1699 | spin_lock_bh(&rt6_exception_lock); |
| 1700 | bucket = rcu_dereference_protected(rt->rt6i_exception_bucket, |
| 1701 | lockdep_is_held(&rt6_exception_lock)); |
| 1702 | |
| 1703 | if (bucket) { |
| 1704 | for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) { |
| 1705 | hlist_for_each_entry_safe(rt6_ex, tmp, |
| 1706 | &bucket->chain, hlist) { |
| 1707 | struct rt6_info *entry = rt6_ex->rt6i; |
| 1708 | |
| 1709 | if ((entry->rt6i_flags & RTF_CACHE_GATEWAY) == |
| 1710 | RTF_CACHE_GATEWAY && |
| 1711 | ipv6_addr_equal(gateway, |
| 1712 | &entry->rt6i_gateway)) { |
| 1713 | rt6_remove_exception(bucket, rt6_ex); |
| 1714 | } |
| 1715 | } |
| 1716 | bucket++; |
| 1717 | } |
| 1718 | } |
| 1719 | |
| 1720 | spin_unlock_bh(&rt6_exception_lock); |
| 1721 | } |
| 1722 | |
Wei Wang | c757faa | 2017-10-06 12:06:01 -0700 | [diff] [blame] | 1723 | static void rt6_age_examine_exception(struct rt6_exception_bucket *bucket, |
| 1724 | struct rt6_exception *rt6_ex, |
| 1725 | struct fib6_gc_args *gc_args, |
| 1726 | unsigned long now) |
| 1727 | { |
| 1728 | struct rt6_info *rt = rt6_ex->rt6i; |
| 1729 | |
Paolo Abeni | 1859bac | 2017-10-19 16:07:11 +0200 | [diff] [blame] | 1730 | /* we are pruning and obsoleting aged-out and non gateway exceptions |
| 1731 | * even if others have still references to them, so that on next |
| 1732 | * dst_check() such references can be dropped. |
| 1733 | * EXPIRES exceptions - e.g. pmtu-generated ones are pruned when |
| 1734 | * expired, independently from their aging, as per RFC 8201 section 4 |
| 1735 | */ |
Wei Wang | 31afeb4 | 2018-01-26 11:40:17 -0800 | [diff] [blame] | 1736 | if (!(rt->rt6i_flags & RTF_EXPIRES)) { |
| 1737 | if (time_after_eq(now, rt->dst.lastuse + gc_args->timeout)) { |
| 1738 | RT6_TRACE("aging clone %p\n", rt); |
| 1739 | rt6_remove_exception(bucket, rt6_ex); |
| 1740 | return; |
| 1741 | } |
| 1742 | } else if (time_after(jiffies, rt->dst.expires)) { |
| 1743 | RT6_TRACE("purging expired route %p\n", rt); |
Wei Wang | c757faa | 2017-10-06 12:06:01 -0700 | [diff] [blame] | 1744 | rt6_remove_exception(bucket, rt6_ex); |
| 1745 | return; |
Wei Wang | 31afeb4 | 2018-01-26 11:40:17 -0800 | [diff] [blame] | 1746 | } |
| 1747 | |
| 1748 | if (rt->rt6i_flags & RTF_GATEWAY) { |
Wei Wang | c757faa | 2017-10-06 12:06:01 -0700 | [diff] [blame] | 1749 | struct neighbour *neigh; |
| 1750 | __u8 neigh_flags = 0; |
| 1751 | |
Eric Dumazet | 1bfa26f | 2018-03-23 07:56:58 -0700 | [diff] [blame] | 1752 | neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway); |
| 1753 | if (neigh) |
Wei Wang | c757faa | 2017-10-06 12:06:01 -0700 | [diff] [blame] | 1754 | neigh_flags = neigh->flags; |
Eric Dumazet | 1bfa26f | 2018-03-23 07:56:58 -0700 | [diff] [blame] | 1755 | |
Wei Wang | c757faa | 2017-10-06 12:06:01 -0700 | [diff] [blame] | 1756 | if (!(neigh_flags & NTF_ROUTER)) { |
| 1757 | RT6_TRACE("purging route %p via non-router but gateway\n", |
| 1758 | rt); |
| 1759 | rt6_remove_exception(bucket, rt6_ex); |
| 1760 | return; |
| 1761 | } |
| 1762 | } |
Wei Wang | 31afeb4 | 2018-01-26 11:40:17 -0800 | [diff] [blame] | 1763 | |
Wei Wang | c757faa | 2017-10-06 12:06:01 -0700 | [diff] [blame] | 1764 | gc_args->more++; |
| 1765 | } |
| 1766 | |
| 1767 | void rt6_age_exceptions(struct rt6_info *rt, |
| 1768 | struct fib6_gc_args *gc_args, |
| 1769 | unsigned long now) |
| 1770 | { |
| 1771 | struct rt6_exception_bucket *bucket; |
| 1772 | struct rt6_exception *rt6_ex; |
| 1773 | struct hlist_node *tmp; |
| 1774 | int i; |
| 1775 | |
| 1776 | if (!rcu_access_pointer(rt->rt6i_exception_bucket)) |
| 1777 | return; |
| 1778 | |
Eric Dumazet | 1bfa26f | 2018-03-23 07:56:58 -0700 | [diff] [blame] | 1779 | rcu_read_lock_bh(); |
| 1780 | spin_lock(&rt6_exception_lock); |
Wei Wang | c757faa | 2017-10-06 12:06:01 -0700 | [diff] [blame] | 1781 | bucket = rcu_dereference_protected(rt->rt6i_exception_bucket, |
| 1782 | lockdep_is_held(&rt6_exception_lock)); |
| 1783 | |
| 1784 | if (bucket) { |
| 1785 | for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) { |
| 1786 | hlist_for_each_entry_safe(rt6_ex, tmp, |
| 1787 | &bucket->chain, hlist) { |
| 1788 | rt6_age_examine_exception(bucket, rt6_ex, |
| 1789 | gc_args, now); |
| 1790 | } |
| 1791 | bucket++; |
| 1792 | } |
| 1793 | } |
Eric Dumazet | 1bfa26f | 2018-03-23 07:56:58 -0700 | [diff] [blame] | 1794 | spin_unlock(&rt6_exception_lock); |
| 1795 | rcu_read_unlock_bh(); |
Wei Wang | c757faa | 2017-10-06 12:06:01 -0700 | [diff] [blame] | 1796 | } |
| 1797 | |
David Ahern | 9ff7438 | 2016-06-13 13:44:19 -0700 | [diff] [blame] | 1798 | 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] | 1799 | int oif, struct flowi6 *fl6, |
| 1800 | const struct sk_buff *skb, int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1801 | { |
Martin KaFai Lau | 367efcb | 2014-10-20 13:42:45 -0700 | [diff] [blame] | 1802 | struct fib6_node *fn, *saved_fn; |
Wei Wang | 2b760fc | 2017-10-06 12:06:03 -0700 | [diff] [blame] | 1803 | struct rt6_info *rt, *rt_cache; |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1804 | int strict = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1805 | |
YOSHIFUJI Hideaki | 77d16f4 | 2006-08-23 17:25:05 -0700 | [diff] [blame] | 1806 | strict |= flags & RT6_LOOKUP_F_IFACE; |
David Ahern | d5d32e4 | 2016-10-24 12:27:23 -0700 | [diff] [blame] | 1807 | strict |= flags & RT6_LOOKUP_F_IGNORE_LINKSTATE; |
Martin KaFai Lau | 367efcb | 2014-10-20 13:42:45 -0700 | [diff] [blame] | 1808 | if (net->ipv6.devconf_all->forwarding == 0) |
| 1809 | strict |= RT6_LOOKUP_F_REACHABLE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1810 | |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 1811 | rcu_read_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1812 | |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 1813 | fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr); |
Martin KaFai Lau | 367efcb | 2014-10-20 13:42:45 -0700 | [diff] [blame] | 1814 | saved_fn = fn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1815 | |
David Ahern | ca25449 | 2015-10-12 11:47:10 -0700 | [diff] [blame] | 1816 | if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF) |
| 1817 | oif = 0; |
| 1818 | |
Martin KaFai Lau | a3c00e4 | 2014-10-20 13:42:43 -0700 | [diff] [blame] | 1819 | redo_rt6_select: |
Wei Wang | 8d1040e | 2017-10-06 12:06:08 -0700 | [diff] [blame] | 1820 | rt = rt6_select(net, fn, oif, strict); |
Nicolas Dichtel | 52bd4c0 | 2013-06-28 17:35:48 +0200 | [diff] [blame] | 1821 | if (rt->rt6i_nsiblings) |
David Ahern | b4bac17 | 2018-03-02 08:32:18 -0800 | [diff] [blame] | 1822 | rt = rt6_multipath_select(net, rt, fl6, oif, skb, strict); |
Martin KaFai Lau | a3c00e4 | 2014-10-20 13:42:43 -0700 | [diff] [blame] | 1823 | if (rt == net->ipv6.ip6_null_entry) { |
| 1824 | fn = fib6_backtrack(fn, &fl6->saddr); |
| 1825 | if (fn) |
| 1826 | goto redo_rt6_select; |
Martin KaFai Lau | 367efcb | 2014-10-20 13:42:45 -0700 | [diff] [blame] | 1827 | else if (strict & RT6_LOOKUP_F_REACHABLE) { |
| 1828 | /* also consider unreachable route */ |
| 1829 | strict &= ~RT6_LOOKUP_F_REACHABLE; |
| 1830 | fn = saved_fn; |
| 1831 | goto redo_rt6_select; |
Martin KaFai Lau | 367efcb | 2014-10-20 13:42:45 -0700 | [diff] [blame] | 1832 | } |
Martin KaFai Lau | a3c00e4 | 2014-10-20 13:42:43 -0700 | [diff] [blame] | 1833 | } |
| 1834 | |
Wei Wang | 2b760fc | 2017-10-06 12:06:03 -0700 | [diff] [blame] | 1835 | /*Search through exception table */ |
| 1836 | rt_cache = rt6_find_cached_rt(rt, &fl6->daddr, &fl6->saddr); |
| 1837 | if (rt_cache) |
| 1838 | rt = rt_cache; |
YOSHIFUJI Hideaki | fb9de91 | 2006-03-20 16:59:08 -0800 | [diff] [blame] | 1839 | |
Wei Wang | d3843fe | 2017-10-06 12:06:06 -0700 | [diff] [blame] | 1840 | if (rt == net->ipv6.ip6_null_entry) { |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 1841 | rcu_read_unlock(); |
Wei Wang | d3843fe | 2017-10-06 12:06:06 -0700 | [diff] [blame] | 1842 | dst_hold(&rt->dst); |
Paolo Abeni | b65f164 | 2017-10-19 09:31:43 +0200 | [diff] [blame] | 1843 | trace_fib6_table_lookup(net, rt, table, fl6); |
Wei Wang | d3843fe | 2017-10-06 12:06:06 -0700 | [diff] [blame] | 1844 | return rt; |
| 1845 | } else if (rt->rt6i_flags & RTF_CACHE) { |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 1846 | if (ip6_hold_safe(net, &rt, true)) |
Wei Wang | d3843fe | 2017-10-06 12:06:06 -0700 | [diff] [blame] | 1847 | dst_use_noref(&rt->dst, jiffies); |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 1848 | |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 1849 | rcu_read_unlock(); |
Paolo Abeni | b65f164 | 2017-10-19 09:31:43 +0200 | [diff] [blame] | 1850 | trace_fib6_table_lookup(net, rt, table, fl6); |
Martin KaFai Lau | d52d399 | 2015-05-22 20:56:06 -0700 | [diff] [blame] | 1851 | return rt; |
Martin KaFai Lau | 3da59bd | 2015-05-22 20:56:03 -0700 | [diff] [blame] | 1852 | } else if (unlikely((fl6->flowi6_flags & FLOWI_FLAG_KNOWN_NH) && |
| 1853 | !(rt->rt6i_flags & RTF_GATEWAY))) { |
| 1854 | /* Create a RTF_CACHE clone which will not be |
| 1855 | * owned by the fib6 tree. It is for the special case where |
| 1856 | * the daddr in the skb during the neighbor look-up is different |
| 1857 | * from the fl6->daddr used to look-up route here. |
| 1858 | */ |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1859 | |
Martin KaFai Lau | 3da59bd | 2015-05-22 20:56:03 -0700 | [diff] [blame] | 1860 | struct rt6_info *uncached_rt; |
| 1861 | |
Wei Wang | d3843fe | 2017-10-06 12:06:06 -0700 | [diff] [blame] | 1862 | if (ip6_hold_safe(net, &rt, true)) { |
| 1863 | dst_use_noref(&rt->dst, jiffies); |
| 1864 | } else { |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 1865 | rcu_read_unlock(); |
Wei Wang | d3843fe | 2017-10-06 12:06:06 -0700 | [diff] [blame] | 1866 | uncached_rt = rt; |
| 1867 | goto uncached_rt_out; |
| 1868 | } |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 1869 | rcu_read_unlock(); |
Martin KaFai Lau | d52d399 | 2015-05-22 20:56:06 -0700 | [diff] [blame] | 1870 | |
Martin KaFai Lau | 3da59bd | 2015-05-22 20:56:03 -0700 | [diff] [blame] | 1871 | uncached_rt = ip6_rt_cache_alloc(rt, &fl6->daddr, NULL); |
| 1872 | dst_release(&rt->dst); |
| 1873 | |
Wei Wang | 1cfb71e | 2017-06-17 10:42:33 -0700 | [diff] [blame] | 1874 | if (uncached_rt) { |
| 1875 | /* Uncached_rt's refcnt is taken during ip6_rt_cache_alloc() |
| 1876 | * No need for another dst_hold() |
| 1877 | */ |
Martin KaFai Lau | 8d0b94a | 2015-05-22 20:56:04 -0700 | [diff] [blame] | 1878 | rt6_uncached_list_add(uncached_rt); |
Wei Wang | 81eb844 | 2017-10-06 12:06:11 -0700 | [diff] [blame] | 1879 | atomic_inc(&net->ipv6.rt6_stats->fib_rt_uncache); |
Wei Wang | 1cfb71e | 2017-06-17 10:42:33 -0700 | [diff] [blame] | 1880 | } else { |
Martin KaFai Lau | 3da59bd | 2015-05-22 20:56:03 -0700 | [diff] [blame] | 1881 | uncached_rt = net->ipv6.ip6_null_entry; |
Wei Wang | 1cfb71e | 2017-06-17 10:42:33 -0700 | [diff] [blame] | 1882 | dst_hold(&uncached_rt->dst); |
| 1883 | } |
David Ahern | b811580 | 2015-11-19 12:24:22 -0800 | [diff] [blame] | 1884 | |
Wei Wang | d3843fe | 2017-10-06 12:06:06 -0700 | [diff] [blame] | 1885 | uncached_rt_out: |
Paolo Abeni | b65f164 | 2017-10-19 09:31:43 +0200 | [diff] [blame] | 1886 | trace_fib6_table_lookup(net, uncached_rt, table, fl6); |
Martin KaFai Lau | 3da59bd | 2015-05-22 20:56:03 -0700 | [diff] [blame] | 1887 | return uncached_rt; |
Martin KaFai Lau | 3da59bd | 2015-05-22 20:56:03 -0700 | [diff] [blame] | 1888 | |
Martin KaFai Lau | d52d399 | 2015-05-22 20:56:06 -0700 | [diff] [blame] | 1889 | } else { |
| 1890 | /* Get a percpu copy */ |
| 1891 | |
| 1892 | struct rt6_info *pcpu_rt; |
| 1893 | |
Wei Wang | d3843fe | 2017-10-06 12:06:06 -0700 | [diff] [blame] | 1894 | dst_use_noref(&rt->dst, jiffies); |
Eric Dumazet | 951f788 | 2017-10-08 21:07:18 -0700 | [diff] [blame] | 1895 | local_bh_disable(); |
Martin KaFai Lau | d52d399 | 2015-05-22 20:56:06 -0700 | [diff] [blame] | 1896 | pcpu_rt = rt6_get_pcpu_route(rt); |
Martin KaFai Lau | d52d399 | 2015-05-22 20:56:06 -0700 | [diff] [blame] | 1897 | |
Eric Dumazet | 951f788 | 2017-10-08 21:07:18 -0700 | [diff] [blame] | 1898 | if (!pcpu_rt) { |
Wei Wang | a94b936 | 2017-10-06 12:06:04 -0700 | [diff] [blame] | 1899 | /* atomic_inc_not_zero() is needed when using rcu */ |
| 1900 | if (atomic_inc_not_zero(&rt->rt6i_ref)) { |
Eric Dumazet | 951f788 | 2017-10-08 21:07:18 -0700 | [diff] [blame] | 1901 | /* No dst_hold() on rt is needed because grabbing |
Wei Wang | a94b936 | 2017-10-06 12:06:04 -0700 | [diff] [blame] | 1902 | * rt->rt6i_ref makes sure rt can't be released. |
| 1903 | */ |
David Ahern | afb1d4b5 | 2018-04-17 17:33:11 -0700 | [diff] [blame] | 1904 | pcpu_rt = rt6_make_pcpu_route(net, rt); |
Wei Wang | a94b936 | 2017-10-06 12:06:04 -0700 | [diff] [blame] | 1905 | rt6_release(rt); |
| 1906 | } else { |
| 1907 | /* rt is already removed from tree */ |
Wei Wang | a94b936 | 2017-10-06 12:06:04 -0700 | [diff] [blame] | 1908 | pcpu_rt = net->ipv6.ip6_null_entry; |
| 1909 | dst_hold(&pcpu_rt->dst); |
| 1910 | } |
Martin KaFai Lau | 9c7370a | 2015-08-14 11:05:54 -0700 | [diff] [blame] | 1911 | } |
Eric Dumazet | 951f788 | 2017-10-08 21:07:18 -0700 | [diff] [blame] | 1912 | local_bh_enable(); |
| 1913 | rcu_read_unlock(); |
Paolo Abeni | b65f164 | 2017-10-19 09:31:43 +0200 | [diff] [blame] | 1914 | trace_fib6_table_lookup(net, pcpu_rt, table, fl6); |
Martin KaFai Lau | d52d399 | 2015-05-22 20:56:06 -0700 | [diff] [blame] | 1915 | return pcpu_rt; |
| 1916 | } |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1917 | } |
David Ahern | 9ff7438 | 2016-06-13 13:44:19 -0700 | [diff] [blame] | 1918 | EXPORT_SYMBOL_GPL(ip6_pol_route); |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1919 | |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 1920 | static struct rt6_info *ip6_pol_route_input(struct net *net, |
| 1921 | struct fib6_table *table, |
| 1922 | struct flowi6 *fl6, |
| 1923 | const struct sk_buff *skb, |
| 1924 | int flags) |
Pavel Emelyanov | 4acad72 | 2007-10-15 13:02:51 -0700 | [diff] [blame] | 1925 | { |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 1926 | return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, skb, flags); |
Pavel Emelyanov | 4acad72 | 2007-10-15 13:02:51 -0700 | [diff] [blame] | 1927 | } |
| 1928 | |
Mahesh Bandewar | d409b84 | 2016-09-16 12:59:08 -0700 | [diff] [blame] | 1929 | struct dst_entry *ip6_route_input_lookup(struct net *net, |
| 1930 | struct net_device *dev, |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 1931 | struct flowi6 *fl6, |
| 1932 | const struct sk_buff *skb, |
| 1933 | int flags) |
Shmulik Ladkani | 72331bc | 2012-04-01 04:03:45 +0000 | [diff] [blame] | 1934 | { |
| 1935 | if (rt6_need_strict(&fl6->daddr) && dev->type != ARPHRD_PIMREG) |
| 1936 | flags |= RT6_LOOKUP_F_IFACE; |
| 1937 | |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 1938 | return fib6_rule_lookup(net, fl6, skb, flags, ip6_pol_route_input); |
Shmulik Ladkani | 72331bc | 2012-04-01 04:03:45 +0000 | [diff] [blame] | 1939 | } |
Mahesh Bandewar | d409b84 | 2016-09-16 12:59:08 -0700 | [diff] [blame] | 1940 | EXPORT_SYMBOL_GPL(ip6_route_input_lookup); |
Shmulik Ladkani | 72331bc | 2012-04-01 04:03:45 +0000 | [diff] [blame] | 1941 | |
Jakub Sitnicki | 23aebda | 2017-08-23 09:58:29 +0200 | [diff] [blame] | 1942 | static void ip6_multipath_l3_keys(const struct sk_buff *skb, |
Roopa Prabhu | 5e5d6fe | 2018-02-28 22:43:22 -0500 | [diff] [blame] | 1943 | struct flow_keys *keys, |
| 1944 | struct flow_keys *flkeys) |
Jakub Sitnicki | 23aebda | 2017-08-23 09:58:29 +0200 | [diff] [blame] | 1945 | { |
| 1946 | const struct ipv6hdr *outer_iph = ipv6_hdr(skb); |
| 1947 | const struct ipv6hdr *key_iph = outer_iph; |
Roopa Prabhu | 5e5d6fe | 2018-02-28 22:43:22 -0500 | [diff] [blame] | 1948 | struct flow_keys *_flkeys = flkeys; |
Jakub Sitnicki | 23aebda | 2017-08-23 09:58:29 +0200 | [diff] [blame] | 1949 | const struct ipv6hdr *inner_iph; |
| 1950 | const struct icmp6hdr *icmph; |
| 1951 | struct ipv6hdr _inner_iph; |
| 1952 | |
| 1953 | if (likely(outer_iph->nexthdr != IPPROTO_ICMPV6)) |
| 1954 | goto out; |
| 1955 | |
| 1956 | icmph = icmp6_hdr(skb); |
| 1957 | if (icmph->icmp6_type != ICMPV6_DEST_UNREACH && |
| 1958 | icmph->icmp6_type != ICMPV6_PKT_TOOBIG && |
| 1959 | icmph->icmp6_type != ICMPV6_TIME_EXCEED && |
| 1960 | icmph->icmp6_type != ICMPV6_PARAMPROB) |
| 1961 | goto out; |
| 1962 | |
| 1963 | inner_iph = skb_header_pointer(skb, |
| 1964 | skb_transport_offset(skb) + sizeof(*icmph), |
| 1965 | sizeof(_inner_iph), &_inner_iph); |
| 1966 | if (!inner_iph) |
| 1967 | goto out; |
| 1968 | |
| 1969 | key_iph = inner_iph; |
Roopa Prabhu | 5e5d6fe | 2018-02-28 22:43:22 -0500 | [diff] [blame] | 1970 | _flkeys = NULL; |
Jakub Sitnicki | 23aebda | 2017-08-23 09:58:29 +0200 | [diff] [blame] | 1971 | out: |
Roopa Prabhu | 5e5d6fe | 2018-02-28 22:43:22 -0500 | [diff] [blame] | 1972 | if (_flkeys) { |
| 1973 | keys->addrs.v6addrs.src = _flkeys->addrs.v6addrs.src; |
| 1974 | keys->addrs.v6addrs.dst = _flkeys->addrs.v6addrs.dst; |
| 1975 | keys->tags.flow_label = _flkeys->tags.flow_label; |
| 1976 | keys->basic.ip_proto = _flkeys->basic.ip_proto; |
| 1977 | } else { |
| 1978 | keys->addrs.v6addrs.src = key_iph->saddr; |
| 1979 | keys->addrs.v6addrs.dst = key_iph->daddr; |
| 1980 | keys->tags.flow_label = ip6_flowinfo(key_iph); |
| 1981 | keys->basic.ip_proto = key_iph->nexthdr; |
| 1982 | } |
Jakub Sitnicki | 23aebda | 2017-08-23 09:58:29 +0200 | [diff] [blame] | 1983 | } |
| 1984 | |
| 1985 | /* 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] | 1986 | u32 rt6_multipath_hash(const struct net *net, const struct flowi6 *fl6, |
| 1987 | const struct sk_buff *skb, struct flow_keys *flkeys) |
Jakub Sitnicki | 23aebda | 2017-08-23 09:58:29 +0200 | [diff] [blame] | 1988 | { |
| 1989 | struct flow_keys hash_keys; |
David Ahern | 9a2a537 | 2018-03-02 08:32:15 -0800 | [diff] [blame] | 1990 | u32 mhash; |
Jakub Sitnicki | 23aebda | 2017-08-23 09:58:29 +0200 | [diff] [blame] | 1991 | |
David S. Miller | bbfa047 | 2018-03-12 11:09:33 -0400 | [diff] [blame] | 1992 | switch (ip6_multipath_hash_policy(net)) { |
David Ahern | b4bac17 | 2018-03-02 08:32:18 -0800 | [diff] [blame] | 1993 | case 0: |
| 1994 | memset(&hash_keys, 0, sizeof(hash_keys)); |
| 1995 | hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; |
| 1996 | if (skb) { |
| 1997 | ip6_multipath_l3_keys(skb, &hash_keys, flkeys); |
| 1998 | } else { |
| 1999 | hash_keys.addrs.v6addrs.src = fl6->saddr; |
| 2000 | hash_keys.addrs.v6addrs.dst = fl6->daddr; |
| 2001 | hash_keys.tags.flow_label = (__force u32)fl6->flowlabel; |
| 2002 | hash_keys.basic.ip_proto = fl6->flowi6_proto; |
| 2003 | } |
| 2004 | break; |
| 2005 | case 1: |
| 2006 | if (skb) { |
| 2007 | unsigned int flag = FLOW_DISSECTOR_F_STOP_AT_ENCAP; |
| 2008 | struct flow_keys keys; |
| 2009 | |
| 2010 | /* short-circuit if we already have L4 hash present */ |
| 2011 | if (skb->l4_hash) |
| 2012 | return skb_get_hash_raw(skb) >> 1; |
| 2013 | |
| 2014 | memset(&hash_keys, 0, sizeof(hash_keys)); |
| 2015 | |
| 2016 | if (!flkeys) { |
| 2017 | skb_flow_dissect_flow_keys(skb, &keys, flag); |
| 2018 | flkeys = &keys; |
| 2019 | } |
| 2020 | hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; |
| 2021 | hash_keys.addrs.v6addrs.src = flkeys->addrs.v6addrs.src; |
| 2022 | hash_keys.addrs.v6addrs.dst = flkeys->addrs.v6addrs.dst; |
| 2023 | hash_keys.ports.src = flkeys->ports.src; |
| 2024 | hash_keys.ports.dst = flkeys->ports.dst; |
| 2025 | hash_keys.basic.ip_proto = flkeys->basic.ip_proto; |
| 2026 | } else { |
| 2027 | memset(&hash_keys, 0, sizeof(hash_keys)); |
| 2028 | hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; |
| 2029 | hash_keys.addrs.v6addrs.src = fl6->saddr; |
| 2030 | hash_keys.addrs.v6addrs.dst = fl6->daddr; |
| 2031 | hash_keys.ports.src = fl6->fl6_sport; |
| 2032 | hash_keys.ports.dst = fl6->fl6_dport; |
| 2033 | hash_keys.basic.ip_proto = fl6->flowi6_proto; |
| 2034 | } |
| 2035 | break; |
Jakub Sitnicki | 23aebda | 2017-08-23 09:58:29 +0200 | [diff] [blame] | 2036 | } |
David Ahern | 9a2a537 | 2018-03-02 08:32:15 -0800 | [diff] [blame] | 2037 | mhash = flow_hash_from_keys(&hash_keys); |
Jakub Sitnicki | 23aebda | 2017-08-23 09:58:29 +0200 | [diff] [blame] | 2038 | |
David Ahern | 9a2a537 | 2018-03-02 08:32:15 -0800 | [diff] [blame] | 2039 | return mhash >> 1; |
Jakub Sitnicki | 23aebda | 2017-08-23 09:58:29 +0200 | [diff] [blame] | 2040 | } |
| 2041 | |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 2042 | void ip6_route_input(struct sk_buff *skb) |
| 2043 | { |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 2044 | const struct ipv6hdr *iph = ipv6_hdr(skb); |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 2045 | struct net *net = dev_net(skb->dev); |
Thomas Graf | adaa70b | 2006-10-13 15:01:03 -0700 | [diff] [blame] | 2046 | int flags = RT6_LOOKUP_F_HAS_SADDR; |
Jiri Benc | 904af04 | 2015-08-20 13:56:31 +0200 | [diff] [blame] | 2047 | struct ip_tunnel_info *tun_info; |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 2048 | struct flowi6 fl6 = { |
David Ahern | e0d56fd | 2016-09-10 12:09:57 -0700 | [diff] [blame] | 2049 | .flowi6_iif = skb->dev->ifindex, |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 2050 | .daddr = iph->daddr, |
| 2051 | .saddr = iph->saddr, |
YOSHIFUJI Hideaki / 吉藤英明 | 6502ca5 | 2013-01-13 05:01:51 +0000 | [diff] [blame] | 2052 | .flowlabel = ip6_flowinfo(iph), |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 2053 | .flowi6_mark = skb->mark, |
| 2054 | .flowi6_proto = iph->nexthdr, |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 2055 | }; |
Roopa Prabhu | 5e5d6fe | 2018-02-28 22:43:22 -0500 | [diff] [blame] | 2056 | struct flow_keys *flkeys = NULL, _flkeys; |
Thomas Graf | adaa70b | 2006-10-13 15:01:03 -0700 | [diff] [blame] | 2057 | |
Jiri Benc | 904af04 | 2015-08-20 13:56:31 +0200 | [diff] [blame] | 2058 | tun_info = skb_tunnel_info(skb); |
Jiri Benc | 46fa062 | 2015-08-28 20:48:19 +0200 | [diff] [blame] | 2059 | if (tun_info && !(tun_info->mode & IP_TUNNEL_INFO_TX)) |
Jiri Benc | 904af04 | 2015-08-20 13:56:31 +0200 | [diff] [blame] | 2060 | fl6.flowi6_tun_key.tun_id = tun_info->key.tun_id; |
Roopa Prabhu | 5e5d6fe | 2018-02-28 22:43:22 -0500 | [diff] [blame] | 2061 | |
| 2062 | if (fib6_rules_early_flow_dissect(net, skb, &fl6, &_flkeys)) |
| 2063 | flkeys = &_flkeys; |
| 2064 | |
Jakub Sitnicki | 23aebda | 2017-08-23 09:58:29 +0200 | [diff] [blame] | 2065 | if (unlikely(fl6.flowi6_proto == IPPROTO_ICMPV6)) |
David Ahern | b4bac17 | 2018-03-02 08:32:18 -0800 | [diff] [blame] | 2066 | fl6.mp_hash = rt6_multipath_hash(net, &fl6, skb, flkeys); |
Jiri Benc | 06e9d04 | 2015-08-20 13:56:26 +0200 | [diff] [blame] | 2067 | skb_dst_drop(skb); |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 2068 | skb_dst_set(skb, |
| 2069 | ip6_route_input_lookup(net, skb->dev, &fl6, skb, flags)); |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 2070 | } |
| 2071 | |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 2072 | static struct rt6_info *ip6_pol_route_output(struct net *net, |
| 2073 | struct fib6_table *table, |
| 2074 | struct flowi6 *fl6, |
| 2075 | const struct sk_buff *skb, |
| 2076 | int flags) |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 2077 | { |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 2078 | return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, skb, flags); |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 2079 | } |
| 2080 | |
Paolo Abeni | 6f21c96 | 2016-01-29 12:30:19 +0100 | [diff] [blame] | 2081 | struct dst_entry *ip6_route_output_flags(struct net *net, const struct sock *sk, |
| 2082 | struct flowi6 *fl6, int flags) |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 2083 | { |
David Ahern | d46a9d6 | 2015-10-21 08:42:22 -0700 | [diff] [blame] | 2084 | bool any_src; |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 2085 | |
David Ahern | 4c1feac | 2016-09-10 12:09:56 -0700 | [diff] [blame] | 2086 | if (rt6_need_strict(&fl6->daddr)) { |
| 2087 | struct dst_entry *dst; |
| 2088 | |
| 2089 | dst = l3mdev_link_scope_lookup(net, fl6); |
| 2090 | if (dst) |
| 2091 | return dst; |
| 2092 | } |
David Ahern | ca25449 | 2015-10-12 11:47:10 -0700 | [diff] [blame] | 2093 | |
Pavel Emelyanov | 1fb9489 | 2012-08-08 21:53:36 +0000 | [diff] [blame] | 2094 | fl6->flowi6_iif = LOOPBACK_IFINDEX; |
David McCullough | 4dc27d1c | 2012-06-25 15:42:26 +0000 | [diff] [blame] | 2095 | |
David Ahern | d46a9d6 | 2015-10-21 08:42:22 -0700 | [diff] [blame] | 2096 | any_src = ipv6_addr_any(&fl6->saddr); |
David Ahern | 741a11d | 2015-09-28 10:12:13 -0700 | [diff] [blame] | 2097 | if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr) || |
David Ahern | d46a9d6 | 2015-10-21 08:42:22 -0700 | [diff] [blame] | 2098 | (fl6->flowi6_oif && any_src)) |
YOSHIFUJI Hideaki | 77d16f4 | 2006-08-23 17:25:05 -0700 | [diff] [blame] | 2099 | flags |= RT6_LOOKUP_F_IFACE; |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 2100 | |
David Ahern | d46a9d6 | 2015-10-21 08:42:22 -0700 | [diff] [blame] | 2101 | if (!any_src) |
Thomas Graf | adaa70b | 2006-10-13 15:01:03 -0700 | [diff] [blame] | 2102 | flags |= RT6_LOOKUP_F_HAS_SADDR; |
YOSHIFUJI Hideaki / 吉藤英明 | 0c9a2ac | 2010-03-07 00:14:44 +0000 | [diff] [blame] | 2103 | else if (sk) |
| 2104 | flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs); |
Thomas Graf | adaa70b | 2006-10-13 15:01:03 -0700 | [diff] [blame] | 2105 | |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 2106 | return fib6_rule_lookup(net, fl6, NULL, flags, ip6_pol_route_output); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2107 | } |
Paolo Abeni | 6f21c96 | 2016-01-29 12:30:19 +0100 | [diff] [blame] | 2108 | EXPORT_SYMBOL_GPL(ip6_route_output_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2109 | |
David S. Miller | 2774c13 | 2011-03-01 14:59:04 -0800 | [diff] [blame] | 2110 | 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] | 2111 | { |
David S. Miller | 5c1e6aa | 2011-04-28 14:13:38 -0700 | [diff] [blame] | 2112 | struct rt6_info *rt, *ort = (struct rt6_info *) dst_orig; |
Wei Wang | 1dbe3252 | 2017-06-17 10:42:26 -0700 | [diff] [blame] | 2113 | struct net_device *loopback_dev = net->loopback_dev; |
David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 2114 | struct dst_entry *new = NULL; |
| 2115 | |
Wei Wang | 1dbe3252 | 2017-06-17 10:42:26 -0700 | [diff] [blame] | 2116 | rt = dst_alloc(&ip6_dst_blackhole_ops, loopback_dev, 1, |
Steffen Klassert | 62cf27e | 2017-10-09 08:39:43 +0200 | [diff] [blame] | 2117 | DST_OBSOLETE_DEAD, 0); |
David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 2118 | if (rt) { |
Martin KaFai Lau | 0a1f596 | 2015-10-15 16:39:58 -0700 | [diff] [blame] | 2119 | rt6_info_init(rt); |
Wei Wang | 81eb844 | 2017-10-06 12:06:11 -0700 | [diff] [blame] | 2120 | atomic_inc(&net->ipv6.rt6_stats->fib_rt_alloc); |
Martin KaFai Lau | 0a1f596 | 2015-10-15 16:39:58 -0700 | [diff] [blame] | 2121 | |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 2122 | new = &rt->dst; |
David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 2123 | new->__use = 1; |
Herbert Xu | 352e512 | 2007-11-13 21:34:06 -0800 | [diff] [blame] | 2124 | new->input = dst_discard; |
Eric W. Biederman | ede2059 | 2015-10-07 16:48:47 -0500 | [diff] [blame] | 2125 | new->output = dst_discard_out; |
David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 2126 | |
Martin KaFai Lau | 0a1f596 | 2015-10-15 16:39:58 -0700 | [diff] [blame] | 2127 | dst_copy_metrics(new, &ort->dst); |
David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 2128 | |
Wei Wang | 1dbe3252 | 2017-06-17 10:42:26 -0700 | [diff] [blame] | 2129 | rt->rt6i_idev = in6_dev_get(loopback_dev); |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 2130 | rt->rt6i_gateway = ort->rt6i_gateway; |
Martin KaFai Lau | 0a1f596 | 2015-10-15 16:39:58 -0700 | [diff] [blame] | 2131 | rt->rt6i_flags = ort->rt6i_flags & ~RTF_PCPU; |
David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 2132 | rt->rt6i_metric = 0; |
| 2133 | |
| 2134 | memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key)); |
| 2135 | #ifdef CONFIG_IPV6_SUBTREES |
| 2136 | memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key)); |
| 2137 | #endif |
David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 2138 | } |
| 2139 | |
David S. Miller | 69ead7a | 2011-03-01 14:45:33 -0800 | [diff] [blame] | 2140 | dst_release(dst_orig); |
| 2141 | return new ? new : ERR_PTR(-ENOMEM); |
David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 2142 | } |
David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 2143 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2144 | /* |
| 2145 | * Destination cache support functions |
| 2146 | */ |
| 2147 | |
Martin KaFai Lau | 3da59bd | 2015-05-22 20:56:03 -0700 | [diff] [blame] | 2148 | static struct dst_entry *rt6_check(struct rt6_info *rt, u32 cookie) |
| 2149 | { |
Steffen Klassert | 3614364 | 2017-08-25 09:05:42 +0200 | [diff] [blame] | 2150 | u32 rt_cookie = 0; |
Wei Wang | c5cff85 | 2017-08-21 09:47:10 -0700 | [diff] [blame] | 2151 | |
| 2152 | if (!rt6_get_cookie_safe(rt, &rt_cookie) || rt_cookie != cookie) |
Martin KaFai Lau | 3da59bd | 2015-05-22 20:56:03 -0700 | [diff] [blame] | 2153 | return NULL; |
| 2154 | |
| 2155 | if (rt6_check_expired(rt)) |
| 2156 | return NULL; |
| 2157 | |
| 2158 | return &rt->dst; |
| 2159 | } |
| 2160 | |
| 2161 | static struct dst_entry *rt6_dst_from_check(struct rt6_info *rt, u32 cookie) |
| 2162 | { |
Martin KaFai Lau | 5973fb1 | 2015-11-11 11:51:07 -0800 | [diff] [blame] | 2163 | if (!__rt6_check_expired(rt) && |
| 2164 | rt->dst.obsolete == DST_OBSOLETE_FORCE_CHK && |
David Miller | 3a2232e | 2017-11-28 15:40:40 -0500 | [diff] [blame] | 2165 | rt6_check(rt->from, cookie)) |
Martin KaFai Lau | 3da59bd | 2015-05-22 20:56:03 -0700 | [diff] [blame] | 2166 | return &rt->dst; |
| 2167 | else |
| 2168 | return NULL; |
| 2169 | } |
| 2170 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2171 | static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie) |
| 2172 | { |
| 2173 | struct rt6_info *rt; |
| 2174 | |
| 2175 | rt = (struct rt6_info *) dst; |
| 2176 | |
Nicolas Dichtel | 6f3118b | 2012-09-10 22:09:46 +0000 | [diff] [blame] | 2177 | /* All IPV6 dsts are created with ->obsolete set to the value |
| 2178 | * DST_OBSOLETE_FORCE_CHK which forces validation calls down |
| 2179 | * into this function always. |
| 2180 | */ |
Hannes Frederic Sowa | e3bc10b | 2013-10-24 07:48:24 +0200 | [diff] [blame] | 2181 | |
Martin KaFai Lau | 02bcf4e | 2015-11-11 11:51:08 -0800 | [diff] [blame] | 2182 | if (rt->rt6i_flags & RTF_PCPU || |
David Miller | 3a2232e | 2017-11-28 15:40:40 -0500 | [diff] [blame] | 2183 | (unlikely(!list_empty(&rt->rt6i_uncached)) && rt->from)) |
Martin KaFai Lau | 3da59bd | 2015-05-22 20:56:03 -0700 | [diff] [blame] | 2184 | return rt6_dst_from_check(rt, cookie); |
| 2185 | else |
| 2186 | return rt6_check(rt, cookie); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2187 | } |
| 2188 | |
| 2189 | static struct dst_entry *ip6_negative_advice(struct dst_entry *dst) |
| 2190 | { |
| 2191 | struct rt6_info *rt = (struct rt6_info *) dst; |
| 2192 | |
| 2193 | if (rt) { |
YOSHIFUJI Hideaki / 吉藤英明 | 54c1a85 | 2010-03-28 07:15:45 +0000 | [diff] [blame] | 2194 | if (rt->rt6i_flags & RTF_CACHE) { |
| 2195 | if (rt6_check_expired(rt)) { |
David Ahern | afb1d4b5 | 2018-04-17 17:33:11 -0700 | [diff] [blame] | 2196 | ip6_del_rt(dev_net(dst->dev), rt); |
YOSHIFUJI Hideaki / 吉藤英明 | 54c1a85 | 2010-03-28 07:15:45 +0000 | [diff] [blame] | 2197 | dst = NULL; |
| 2198 | } |
| 2199 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2200 | dst_release(dst); |
YOSHIFUJI Hideaki / 吉藤英明 | 54c1a85 | 2010-03-28 07:15:45 +0000 | [diff] [blame] | 2201 | dst = NULL; |
| 2202 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2203 | } |
YOSHIFUJI Hideaki / 吉藤英明 | 54c1a85 | 2010-03-28 07:15:45 +0000 | [diff] [blame] | 2204 | return dst; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2205 | } |
| 2206 | |
| 2207 | static void ip6_link_failure(struct sk_buff *skb) |
| 2208 | { |
| 2209 | struct rt6_info *rt; |
| 2210 | |
Alexey Dobriyan | 3ffe533 | 2010-02-18 08:25:24 +0000 | [diff] [blame] | 2211 | icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2212 | |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 2213 | rt = (struct rt6_info *) skb_dst(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2214 | if (rt) { |
Hannes Frederic Sowa | 1eb4f75 | 2013-07-10 23:00:57 +0200 | [diff] [blame] | 2215 | if (rt->rt6i_flags & RTF_CACHE) { |
Wei Wang | ad65a2f | 2017-06-17 10:42:35 -0700 | [diff] [blame] | 2216 | if (dst_hold_safe(&rt->dst)) |
David Ahern | afb1d4b5 | 2018-04-17 17:33:11 -0700 | [diff] [blame] | 2217 | ip6_del_rt(dev_net(rt->dst.dev), rt); |
Wei Wang | c5cff85 | 2017-08-21 09:47:10 -0700 | [diff] [blame] | 2218 | } else { |
| 2219 | struct fib6_node *fn; |
| 2220 | |
| 2221 | rcu_read_lock(); |
| 2222 | fn = rcu_dereference(rt->rt6i_node); |
| 2223 | if (fn && (rt->rt6i_flags & RTF_DEFAULT)) |
| 2224 | fn->fn_sernum = -1; |
| 2225 | rcu_read_unlock(); |
Hannes Frederic Sowa | 1eb4f75 | 2013-07-10 23:00:57 +0200 | [diff] [blame] | 2226 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2227 | } |
| 2228 | } |
| 2229 | |
Martin KaFai Lau | 45e4fd2 | 2015-05-22 20:56:00 -0700 | [diff] [blame] | 2230 | static void rt6_do_update_pmtu(struct rt6_info *rt, u32 mtu) |
| 2231 | { |
| 2232 | struct net *net = dev_net(rt->dst.dev); |
| 2233 | |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 2234 | dst_metric_set(&rt->dst, RTAX_MTU, mtu); |
Martin KaFai Lau | 45e4fd2 | 2015-05-22 20:56:00 -0700 | [diff] [blame] | 2235 | rt->rt6i_flags |= RTF_MODIFIED; |
Martin KaFai Lau | 45e4fd2 | 2015-05-22 20:56:00 -0700 | [diff] [blame] | 2236 | rt6_update_expires(rt, net->ipv6.sysctl.ip6_rt_mtu_expires); |
| 2237 | } |
| 2238 | |
Martin KaFai Lau | 0d3f6d2 | 2015-11-11 11:51:06 -0800 | [diff] [blame] | 2239 | static bool rt6_cache_allowed_for_pmtu(const struct rt6_info *rt) |
| 2240 | { |
| 2241 | return !(rt->rt6i_flags & RTF_CACHE) && |
Wei Wang | 4e587ea | 2017-08-25 15:03:10 -0700 | [diff] [blame] | 2242 | (rt->rt6i_flags & RTF_PCPU || |
| 2243 | rcu_access_pointer(rt->rt6i_node)); |
Martin KaFai Lau | 0d3f6d2 | 2015-11-11 11:51:06 -0800 | [diff] [blame] | 2244 | } |
| 2245 | |
Martin KaFai Lau | 45e4fd2 | 2015-05-22 20:56:00 -0700 | [diff] [blame] | 2246 | static void __ip6_rt_update_pmtu(struct dst_entry *dst, const struct sock *sk, |
| 2247 | const struct ipv6hdr *iph, u32 mtu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2248 | { |
Julian Anastasov | 0dec879 | 2017-02-06 23:14:16 +0200 | [diff] [blame] | 2249 | const struct in6_addr *daddr, *saddr; |
Ian Morris | 67ba415 | 2014-08-24 21:53:10 +0100 | [diff] [blame] | 2250 | struct rt6_info *rt6 = (struct rt6_info *)dst; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2251 | |
Martin KaFai Lau | 45e4fd2 | 2015-05-22 20:56:00 -0700 | [diff] [blame] | 2252 | if (rt6->rt6i_flags & RTF_LOCAL) |
| 2253 | return; |
| 2254 | |
Xin Long | 19bda36 | 2016-10-28 18:18:01 +0800 | [diff] [blame] | 2255 | if (dst_metric_locked(dst, RTAX_MTU)) |
| 2256 | return; |
| 2257 | |
Julian Anastasov | 0dec879 | 2017-02-06 23:14:16 +0200 | [diff] [blame] | 2258 | if (iph) { |
| 2259 | daddr = &iph->daddr; |
| 2260 | saddr = &iph->saddr; |
| 2261 | } else if (sk) { |
| 2262 | daddr = &sk->sk_v6_daddr; |
| 2263 | saddr = &inet6_sk(sk)->saddr; |
| 2264 | } else { |
| 2265 | daddr = NULL; |
| 2266 | saddr = NULL; |
| 2267 | } |
| 2268 | dst_confirm_neigh(dst, daddr); |
Martin KaFai Lau | 45e4fd2 | 2015-05-22 20:56:00 -0700 | [diff] [blame] | 2269 | mtu = max_t(u32, mtu, IPV6_MIN_MTU); |
| 2270 | if (mtu >= dst_mtu(dst)) |
| 2271 | return; |
David S. Miller | 81aded2 | 2012-06-15 14:54:11 -0700 | [diff] [blame] | 2272 | |
Martin KaFai Lau | 0d3f6d2 | 2015-11-11 11:51:06 -0800 | [diff] [blame] | 2273 | if (!rt6_cache_allowed_for_pmtu(rt6)) { |
Martin KaFai Lau | 45e4fd2 | 2015-05-22 20:56:00 -0700 | [diff] [blame] | 2274 | rt6_do_update_pmtu(rt6, mtu); |
Wei Wang | 2b760fc | 2017-10-06 12:06:03 -0700 | [diff] [blame] | 2275 | /* update rt6_ex->stamp for cache */ |
| 2276 | if (rt6->rt6i_flags & RTF_CACHE) |
| 2277 | rt6_update_exception_stamp_rt(rt6); |
Julian Anastasov | 0dec879 | 2017-02-06 23:14:16 +0200 | [diff] [blame] | 2278 | } else if (daddr) { |
Martin KaFai Lau | 45e4fd2 | 2015-05-22 20:56:00 -0700 | [diff] [blame] | 2279 | struct rt6_info *nrt6; |
Hagen Paul Pfeifer | 9d28971 | 2015-01-15 22:34:25 +0100 | [diff] [blame] | 2280 | |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 2281 | nrt6 = ip6_rt_cache_alloc(rt6->from, daddr, saddr); |
Martin KaFai Lau | 45e4fd2 | 2015-05-22 20:56:00 -0700 | [diff] [blame] | 2282 | if (nrt6) { |
| 2283 | rt6_do_update_pmtu(nrt6, mtu); |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 2284 | if (rt6_insert_exception(nrt6, rt6->from)) |
Wei Wang | 2b760fc | 2017-10-06 12:06:03 -0700 | [diff] [blame] | 2285 | dst_release_immediate(&nrt6->dst); |
Martin KaFai Lau | 45e4fd2 | 2015-05-22 20:56:00 -0700 | [diff] [blame] | 2286 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2287 | } |
| 2288 | } |
| 2289 | |
Martin KaFai Lau | 45e4fd2 | 2015-05-22 20:56:00 -0700 | [diff] [blame] | 2290 | static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk, |
| 2291 | struct sk_buff *skb, u32 mtu) |
| 2292 | { |
| 2293 | __ip6_rt_update_pmtu(dst, sk, skb ? ipv6_hdr(skb) : NULL, mtu); |
| 2294 | } |
| 2295 | |
David S. Miller | 42ae66c | 2012-06-15 20:01:57 -0700 | [diff] [blame] | 2296 | 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] | 2297 | int oif, u32 mark, kuid_t uid) |
David S. Miller | 81aded2 | 2012-06-15 14:54:11 -0700 | [diff] [blame] | 2298 | { |
| 2299 | const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data; |
| 2300 | struct dst_entry *dst; |
| 2301 | struct flowi6 fl6; |
| 2302 | |
| 2303 | memset(&fl6, 0, sizeof(fl6)); |
| 2304 | fl6.flowi6_oif = oif; |
Lorenzo Colitti | 1b3c61d | 2014-05-13 10:17:34 -0700 | [diff] [blame] | 2305 | fl6.flowi6_mark = mark ? mark : IP6_REPLY_MARK(net, skb->mark); |
David S. Miller | 81aded2 | 2012-06-15 14:54:11 -0700 | [diff] [blame] | 2306 | fl6.daddr = iph->daddr; |
| 2307 | fl6.saddr = iph->saddr; |
YOSHIFUJI Hideaki / 吉藤英明 | 6502ca5 | 2013-01-13 05:01:51 +0000 | [diff] [blame] | 2308 | fl6.flowlabel = ip6_flowinfo(iph); |
Lorenzo Colitti | e2d118a | 2016-11-04 02:23:43 +0900 | [diff] [blame] | 2309 | fl6.flowi6_uid = uid; |
David S. Miller | 81aded2 | 2012-06-15 14:54:11 -0700 | [diff] [blame] | 2310 | |
| 2311 | dst = ip6_route_output(net, NULL, &fl6); |
| 2312 | if (!dst->error) |
Martin KaFai Lau | 45e4fd2 | 2015-05-22 20:56:00 -0700 | [diff] [blame] | 2313 | __ip6_rt_update_pmtu(dst, NULL, iph, ntohl(mtu)); |
David S. Miller | 81aded2 | 2012-06-15 14:54:11 -0700 | [diff] [blame] | 2314 | dst_release(dst); |
| 2315 | } |
| 2316 | EXPORT_SYMBOL_GPL(ip6_update_pmtu); |
| 2317 | |
| 2318 | void ip6_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, __be32 mtu) |
| 2319 | { |
Martin KaFai Lau | 33c162a | 2016-04-11 15:29:36 -0700 | [diff] [blame] | 2320 | struct dst_entry *dst; |
| 2321 | |
David S. Miller | 81aded2 | 2012-06-15 14:54:11 -0700 | [diff] [blame] | 2322 | ip6_update_pmtu(skb, sock_net(sk), mtu, |
Lorenzo Colitti | e2d118a | 2016-11-04 02:23:43 +0900 | [diff] [blame] | 2323 | sk->sk_bound_dev_if, sk->sk_mark, sk->sk_uid); |
Martin KaFai Lau | 33c162a | 2016-04-11 15:29:36 -0700 | [diff] [blame] | 2324 | |
| 2325 | dst = __sk_dst_get(sk); |
| 2326 | if (!dst || !dst->obsolete || |
| 2327 | dst->ops->check(dst, inet6_sk(sk)->dst_cookie)) |
| 2328 | return; |
| 2329 | |
| 2330 | bh_lock_sock(sk); |
| 2331 | if (!sock_owned_by_user(sk) && !ipv6_addr_v4mapped(&sk->sk_v6_daddr)) |
| 2332 | ip6_datagram_dst_update(sk, false); |
| 2333 | bh_unlock_sock(sk); |
David S. Miller | 81aded2 | 2012-06-15 14:54:11 -0700 | [diff] [blame] | 2334 | } |
| 2335 | EXPORT_SYMBOL_GPL(ip6_sk_update_pmtu); |
| 2336 | |
Alexey Kodanev | 7d6850f | 2018-04-03 15:00:07 +0300 | [diff] [blame] | 2337 | void ip6_sk_dst_store_flow(struct sock *sk, struct dst_entry *dst, |
| 2338 | const struct flowi6 *fl6) |
| 2339 | { |
| 2340 | #ifdef CONFIG_IPV6_SUBTREES |
| 2341 | struct ipv6_pinfo *np = inet6_sk(sk); |
| 2342 | #endif |
| 2343 | |
| 2344 | ip6_dst_store(sk, dst, |
| 2345 | ipv6_addr_equal(&fl6->daddr, &sk->sk_v6_daddr) ? |
| 2346 | &sk->sk_v6_daddr : NULL, |
| 2347 | #ifdef CONFIG_IPV6_SUBTREES |
| 2348 | ipv6_addr_equal(&fl6->saddr, &np->saddr) ? |
| 2349 | &np->saddr : |
| 2350 | #endif |
| 2351 | NULL); |
| 2352 | } |
| 2353 | |
Duan Jiong | b55b76b | 2013-09-04 19:44:21 +0800 | [diff] [blame] | 2354 | /* Handle redirects */ |
| 2355 | struct ip6rd_flowi { |
| 2356 | struct flowi6 fl6; |
| 2357 | struct in6_addr gateway; |
| 2358 | }; |
| 2359 | |
| 2360 | static struct rt6_info *__ip6_route_redirect(struct net *net, |
| 2361 | struct fib6_table *table, |
| 2362 | struct flowi6 *fl6, |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 2363 | const struct sk_buff *skb, |
Duan Jiong | b55b76b | 2013-09-04 19:44:21 +0800 | [diff] [blame] | 2364 | int flags) |
| 2365 | { |
| 2366 | struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl6; |
Wei Wang | 2b760fc | 2017-10-06 12:06:03 -0700 | [diff] [blame] | 2367 | struct rt6_info *rt, *rt_cache; |
Duan Jiong | b55b76b | 2013-09-04 19:44:21 +0800 | [diff] [blame] | 2368 | struct fib6_node *fn; |
| 2369 | |
| 2370 | /* Get the "current" route for this destination and |
Alexander Alemayhu | 67c408c | 2017-01-07 23:53:00 +0100 | [diff] [blame] | 2371 | * check if the redirect has come from appropriate router. |
Duan Jiong | b55b76b | 2013-09-04 19:44:21 +0800 | [diff] [blame] | 2372 | * |
| 2373 | * RFC 4861 specifies that redirects should only be |
| 2374 | * accepted if they come from the nexthop to the target. |
| 2375 | * Due to the way the routes are chosen, this notion |
| 2376 | * is a bit fuzzy and one might need to check all possible |
| 2377 | * routes. |
| 2378 | */ |
| 2379 | |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 2380 | rcu_read_lock(); |
Duan Jiong | b55b76b | 2013-09-04 19:44:21 +0800 | [diff] [blame] | 2381 | fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr); |
| 2382 | restart: |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 2383 | for_each_fib6_node_rt_rcu(fn) { |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 2384 | if (rt->fib6_nh.nh_flags & RTNH_F_DEAD) |
Ido Schimmel | 8067bb8 | 2018-01-07 12:45:09 +0200 | [diff] [blame] | 2385 | continue; |
David Ahern | 1489568 | 2018-04-17 17:33:17 -0700 | [diff] [blame^] | 2386 | if (fib6_check_expired(rt)) |
Duan Jiong | b55b76b | 2013-09-04 19:44:21 +0800 | [diff] [blame] | 2387 | continue; |
David Ahern | 6edb3c9 | 2018-04-17 17:33:15 -0700 | [diff] [blame] | 2388 | if (rt->rt6i_flags & RTF_REJECT) |
Duan Jiong | b55b76b | 2013-09-04 19:44:21 +0800 | [diff] [blame] | 2389 | break; |
| 2390 | if (!(rt->rt6i_flags & RTF_GATEWAY)) |
| 2391 | continue; |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 2392 | if (fl6->flowi6_oif != rt->fib6_nh.nh_dev->ifindex) |
Duan Jiong | b55b76b | 2013-09-04 19:44:21 +0800 | [diff] [blame] | 2393 | continue; |
Wei Wang | 2b760fc | 2017-10-06 12:06:03 -0700 | [diff] [blame] | 2394 | /* rt_cache's gateway might be different from its 'parent' |
| 2395 | * in the case of an ip redirect. |
| 2396 | * So we keep searching in the exception table if the gateway |
| 2397 | * is different. |
| 2398 | */ |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 2399 | if (!ipv6_addr_equal(&rdfl->gateway, &rt->fib6_nh.nh_gw)) { |
Wei Wang | 2b760fc | 2017-10-06 12:06:03 -0700 | [diff] [blame] | 2400 | rt_cache = rt6_find_cached_rt(rt, |
| 2401 | &fl6->daddr, |
| 2402 | &fl6->saddr); |
| 2403 | if (rt_cache && |
| 2404 | ipv6_addr_equal(&rdfl->gateway, |
| 2405 | &rt_cache->rt6i_gateway)) { |
| 2406 | rt = rt_cache; |
| 2407 | break; |
| 2408 | } |
Duan Jiong | b55b76b | 2013-09-04 19:44:21 +0800 | [diff] [blame] | 2409 | continue; |
Wei Wang | 2b760fc | 2017-10-06 12:06:03 -0700 | [diff] [blame] | 2410 | } |
Duan Jiong | b55b76b | 2013-09-04 19:44:21 +0800 | [diff] [blame] | 2411 | break; |
| 2412 | } |
| 2413 | |
| 2414 | if (!rt) |
| 2415 | rt = net->ipv6.ip6_null_entry; |
David Ahern | 6edb3c9 | 2018-04-17 17:33:15 -0700 | [diff] [blame] | 2416 | else if (rt->rt6i_flags & RTF_REJECT) { |
Duan Jiong | b55b76b | 2013-09-04 19:44:21 +0800 | [diff] [blame] | 2417 | rt = net->ipv6.ip6_null_entry; |
Martin KaFai Lau | b0a1ba5 | 2015-01-20 19:16:02 -0800 | [diff] [blame] | 2418 | goto out; |
| 2419 | } |
| 2420 | |
| 2421 | if (rt == net->ipv6.ip6_null_entry) { |
Martin KaFai Lau | a3c00e4 | 2014-10-20 13:42:43 -0700 | [diff] [blame] | 2422 | fn = fib6_backtrack(fn, &fl6->saddr); |
| 2423 | if (fn) |
| 2424 | goto restart; |
Duan Jiong | b55b76b | 2013-09-04 19:44:21 +0800 | [diff] [blame] | 2425 | } |
Martin KaFai Lau | a3c00e4 | 2014-10-20 13:42:43 -0700 | [diff] [blame] | 2426 | |
Martin KaFai Lau | b0a1ba5 | 2015-01-20 19:16:02 -0800 | [diff] [blame] | 2427 | out: |
Wei Wang | d3843fe | 2017-10-06 12:06:06 -0700 | [diff] [blame] | 2428 | ip6_hold_safe(net, &rt, true); |
Duan Jiong | b55b76b | 2013-09-04 19:44:21 +0800 | [diff] [blame] | 2429 | |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 2430 | rcu_read_unlock(); |
Duan Jiong | b55b76b | 2013-09-04 19:44:21 +0800 | [diff] [blame] | 2431 | |
Paolo Abeni | b65f164 | 2017-10-19 09:31:43 +0200 | [diff] [blame] | 2432 | trace_fib6_table_lookup(net, rt, table, fl6); |
Duan Jiong | b55b76b | 2013-09-04 19:44:21 +0800 | [diff] [blame] | 2433 | return rt; |
| 2434 | }; |
| 2435 | |
| 2436 | static struct dst_entry *ip6_route_redirect(struct net *net, |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 2437 | const struct flowi6 *fl6, |
| 2438 | const struct sk_buff *skb, |
| 2439 | const struct in6_addr *gateway) |
Duan Jiong | b55b76b | 2013-09-04 19:44:21 +0800 | [diff] [blame] | 2440 | { |
| 2441 | int flags = RT6_LOOKUP_F_HAS_SADDR; |
| 2442 | struct ip6rd_flowi rdfl; |
| 2443 | |
| 2444 | rdfl.fl6 = *fl6; |
| 2445 | rdfl.gateway = *gateway; |
| 2446 | |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 2447 | return fib6_rule_lookup(net, &rdfl.fl6, skb, |
Duan Jiong | b55b76b | 2013-09-04 19:44:21 +0800 | [diff] [blame] | 2448 | flags, __ip6_route_redirect); |
| 2449 | } |
| 2450 | |
Lorenzo Colitti | e2d118a | 2016-11-04 02:23:43 +0900 | [diff] [blame] | 2451 | void ip6_redirect(struct sk_buff *skb, struct net *net, int oif, u32 mark, |
| 2452 | kuid_t uid) |
David S. Miller | 3a5ad2e | 2012-07-12 00:08:07 -0700 | [diff] [blame] | 2453 | { |
| 2454 | const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data; |
| 2455 | struct dst_entry *dst; |
| 2456 | struct flowi6 fl6; |
| 2457 | |
| 2458 | memset(&fl6, 0, sizeof(fl6)); |
Julian Anastasov | e374c61 | 2014-04-28 10:51:56 +0300 | [diff] [blame] | 2459 | fl6.flowi6_iif = LOOPBACK_IFINDEX; |
David S. Miller | 3a5ad2e | 2012-07-12 00:08:07 -0700 | [diff] [blame] | 2460 | fl6.flowi6_oif = oif; |
| 2461 | fl6.flowi6_mark = mark; |
David S. Miller | 3a5ad2e | 2012-07-12 00:08:07 -0700 | [diff] [blame] | 2462 | fl6.daddr = iph->daddr; |
| 2463 | fl6.saddr = iph->saddr; |
YOSHIFUJI Hideaki / 吉藤英明 | 6502ca5 | 2013-01-13 05:01:51 +0000 | [diff] [blame] | 2464 | fl6.flowlabel = ip6_flowinfo(iph); |
Lorenzo Colitti | e2d118a | 2016-11-04 02:23:43 +0900 | [diff] [blame] | 2465 | fl6.flowi6_uid = uid; |
David S. Miller | 3a5ad2e | 2012-07-12 00:08:07 -0700 | [diff] [blame] | 2466 | |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 2467 | dst = ip6_route_redirect(net, &fl6, skb, &ipv6_hdr(skb)->saddr); |
Duan Jiong | b55b76b | 2013-09-04 19:44:21 +0800 | [diff] [blame] | 2468 | rt6_do_redirect(dst, NULL, skb); |
David S. Miller | 3a5ad2e | 2012-07-12 00:08:07 -0700 | [diff] [blame] | 2469 | dst_release(dst); |
| 2470 | } |
| 2471 | EXPORT_SYMBOL_GPL(ip6_redirect); |
| 2472 | |
Duan Jiong | c92a59e | 2013-08-22 12:07:35 +0800 | [diff] [blame] | 2473 | void ip6_redirect_no_header(struct sk_buff *skb, struct net *net, int oif, |
| 2474 | u32 mark) |
| 2475 | { |
| 2476 | const struct ipv6hdr *iph = ipv6_hdr(skb); |
| 2477 | const struct rd_msg *msg = (struct rd_msg *)icmp6_hdr(skb); |
| 2478 | struct dst_entry *dst; |
| 2479 | struct flowi6 fl6; |
| 2480 | |
| 2481 | memset(&fl6, 0, sizeof(fl6)); |
Julian Anastasov | e374c61 | 2014-04-28 10:51:56 +0300 | [diff] [blame] | 2482 | fl6.flowi6_iif = LOOPBACK_IFINDEX; |
Duan Jiong | c92a59e | 2013-08-22 12:07:35 +0800 | [diff] [blame] | 2483 | fl6.flowi6_oif = oif; |
| 2484 | fl6.flowi6_mark = mark; |
Duan Jiong | c92a59e | 2013-08-22 12:07:35 +0800 | [diff] [blame] | 2485 | fl6.daddr = msg->dest; |
| 2486 | fl6.saddr = iph->daddr; |
Lorenzo Colitti | e2d118a | 2016-11-04 02:23:43 +0900 | [diff] [blame] | 2487 | fl6.flowi6_uid = sock_net_uid(net, NULL); |
Duan Jiong | c92a59e | 2013-08-22 12:07:35 +0800 | [diff] [blame] | 2488 | |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 2489 | dst = ip6_route_redirect(net, &fl6, skb, &iph->saddr); |
Duan Jiong | b55b76b | 2013-09-04 19:44:21 +0800 | [diff] [blame] | 2490 | rt6_do_redirect(dst, NULL, skb); |
Duan Jiong | c92a59e | 2013-08-22 12:07:35 +0800 | [diff] [blame] | 2491 | dst_release(dst); |
| 2492 | } |
| 2493 | |
David S. Miller | 3a5ad2e | 2012-07-12 00:08:07 -0700 | [diff] [blame] | 2494 | void ip6_sk_redirect(struct sk_buff *skb, struct sock *sk) |
| 2495 | { |
Lorenzo Colitti | e2d118a | 2016-11-04 02:23:43 +0900 | [diff] [blame] | 2496 | ip6_redirect(skb, sock_net(sk), sk->sk_bound_dev_if, sk->sk_mark, |
| 2497 | sk->sk_uid); |
David S. Miller | 3a5ad2e | 2012-07-12 00:08:07 -0700 | [diff] [blame] | 2498 | } |
| 2499 | EXPORT_SYMBOL_GPL(ip6_sk_redirect); |
| 2500 | |
David S. Miller | 0dbaee3 | 2010-12-13 12:52:14 -0800 | [diff] [blame] | 2501 | static unsigned int ip6_default_advmss(const struct dst_entry *dst) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2502 | { |
David S. Miller | 0dbaee3 | 2010-12-13 12:52:14 -0800 | [diff] [blame] | 2503 | struct net_device *dev = dst->dev; |
| 2504 | unsigned int mtu = dst_mtu(dst); |
| 2505 | struct net *net = dev_net(dev); |
| 2506 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2507 | mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr); |
| 2508 | |
Daniel Lezcano | 5578689 | 2008-03-04 13:47:47 -0800 | [diff] [blame] | 2509 | if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss) |
| 2510 | mtu = net->ipv6.sysctl.ip6_rt_min_advmss; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2511 | |
| 2512 | /* |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 2513 | * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and |
| 2514 | * corresponding MSS is IPV6_MAXPLEN - tcp_header_size. |
| 2515 | * IPV6_MAXPLEN is also valid and means: "any MSS, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2516 | * rely only on pmtu discovery" |
| 2517 | */ |
| 2518 | if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr)) |
| 2519 | mtu = IPV6_MAXPLEN; |
| 2520 | return mtu; |
| 2521 | } |
| 2522 | |
Steffen Klassert | ebb762f | 2011-11-23 02:12:51 +0000 | [diff] [blame] | 2523 | static unsigned int ip6_mtu(const struct dst_entry *dst) |
David S. Miller | d33e455 | 2010-12-14 13:01:14 -0800 | [diff] [blame] | 2524 | { |
David S. Miller | d33e455 | 2010-12-14 13:01:14 -0800 | [diff] [blame] | 2525 | struct inet6_dev *idev; |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 2526 | unsigned int mtu; |
Steffen Klassert | 618f9bc | 2011-11-23 02:13:31 +0000 | [diff] [blame] | 2527 | |
Martin KaFai Lau | 4b32b5a | 2015-04-28 13:03:06 -0700 | [diff] [blame] | 2528 | mtu = dst_metric_raw(dst, RTAX_MTU); |
| 2529 | if (mtu) |
| 2530 | goto out; |
| 2531 | |
Steffen Klassert | 618f9bc | 2011-11-23 02:13:31 +0000 | [diff] [blame] | 2532 | mtu = IPV6_MIN_MTU; |
David S. Miller | d33e455 | 2010-12-14 13:01:14 -0800 | [diff] [blame] | 2533 | |
| 2534 | rcu_read_lock(); |
| 2535 | idev = __in6_dev_get(dst->dev); |
| 2536 | if (idev) |
| 2537 | mtu = idev->cnf.mtu6; |
| 2538 | rcu_read_unlock(); |
| 2539 | |
Eric Dumazet | 30f78d8 | 2014-04-10 21:23:36 -0700 | [diff] [blame] | 2540 | out: |
Roopa Prabhu | 14972cb | 2016-08-24 20:10:43 -0700 | [diff] [blame] | 2541 | mtu = min_t(unsigned int, mtu, IP6_MAX_MTU); |
| 2542 | |
| 2543 | return mtu - lwtunnel_headroom(dst->lwtstate, mtu); |
David S. Miller | d33e455 | 2010-12-14 13:01:14 -0800 | [diff] [blame] | 2544 | } |
| 2545 | |
YOSHIFUJI Hideaki | 3b00944 | 2007-12-06 16:11:48 -0800 | [diff] [blame] | 2546 | struct dst_entry *icmp6_dst_alloc(struct net_device *dev, |
David S. Miller | 87a1157 | 2011-12-06 17:04:13 -0500 | [diff] [blame] | 2547 | struct flowi6 *fl6) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2548 | { |
David S. Miller | 87a1157 | 2011-12-06 17:04:13 -0500 | [diff] [blame] | 2549 | struct dst_entry *dst; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2550 | struct rt6_info *rt; |
| 2551 | struct inet6_dev *idev = in6_dev_get(dev); |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 2552 | struct net *net = dev_net(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2553 | |
David S. Miller | 3830847 | 2011-12-03 18:02:47 -0500 | [diff] [blame] | 2554 | if (unlikely(!idev)) |
Eric Dumazet | 122bdf6 | 2012-03-14 21:13:11 +0000 | [diff] [blame] | 2555 | return ERR_PTR(-ENODEV); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2556 | |
Martin KaFai Lau | ad70686 | 2015-08-14 11:05:52 -0700 | [diff] [blame] | 2557 | rt = ip6_dst_alloc(net, dev, 0); |
David S. Miller | 3830847 | 2011-12-03 18:02:47 -0500 | [diff] [blame] | 2558 | if (unlikely(!rt)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2559 | in6_dev_put(idev); |
David S. Miller | 87a1157 | 2011-12-06 17:04:13 -0500 | [diff] [blame] | 2560 | dst = ERR_PTR(-ENOMEM); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2561 | goto out; |
| 2562 | } |
| 2563 | |
Yan, Zheng | 8e2ec63 | 2011-09-05 21:34:30 +0000 | [diff] [blame] | 2564 | rt->dst.flags |= DST_HOST; |
Brendan McGrath | 588753f | 2017-12-13 22:14:57 +1100 | [diff] [blame] | 2565 | rt->dst.input = ip6_input; |
Yan, Zheng | 8e2ec63 | 2011-09-05 21:34:30 +0000 | [diff] [blame] | 2566 | rt->dst.output = ip6_output; |
Julian Anastasov | 550bab4 | 2013-10-20 15:43:04 +0300 | [diff] [blame] | 2567 | rt->rt6i_gateway = fl6->daddr; |
David S. Miller | 87a1157 | 2011-12-06 17:04:13 -0500 | [diff] [blame] | 2568 | rt->rt6i_dst.addr = fl6->daddr; |
Yan, Zheng | 8e2ec63 | 2011-09-05 21:34:30 +0000 | [diff] [blame] | 2569 | rt->rt6i_dst.plen = 128; |
| 2570 | rt->rt6i_idev = idev; |
Li RongQing | 14edd87 | 2012-10-24 14:01:18 +0800 | [diff] [blame] | 2571 | dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2572 | |
Ido Schimmel | 4c981e2 | 2018-01-07 12:45:04 +0200 | [diff] [blame] | 2573 | /* Add this dst into uncached_list so that rt6_disable_ip() can |
Wei Wang | 587fea7 | 2017-06-17 10:42:36 -0700 | [diff] [blame] | 2574 | * do proper release of the net_device |
| 2575 | */ |
| 2576 | rt6_uncached_list_add(rt); |
Wei Wang | 81eb844 | 2017-10-06 12:06:11 -0700 | [diff] [blame] | 2577 | atomic_inc(&net->ipv6.rt6_stats->fib_rt_uncache); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2578 | |
David S. Miller | 87a1157 | 2011-12-06 17:04:13 -0500 | [diff] [blame] | 2579 | dst = xfrm_lookup(net, &rt->dst, flowi6_to_flowi(fl6), NULL, 0); |
| 2580 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2581 | out: |
David S. Miller | 87a1157 | 2011-12-06 17:04:13 -0500 | [diff] [blame] | 2582 | return dst; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2583 | } |
| 2584 | |
Daniel Lezcano | 569d364 | 2008-01-18 03:56:57 -0800 | [diff] [blame] | 2585 | static int ip6_dst_gc(struct dst_ops *ops) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2586 | { |
Alexey Dobriyan | 86393e5 | 2009-08-29 01:34:49 +0000 | [diff] [blame] | 2587 | struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops); |
Daniel Lezcano | 7019b78 | 2008-03-04 13:50:14 -0800 | [diff] [blame] | 2588 | int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval; |
| 2589 | int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size; |
| 2590 | int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity; |
| 2591 | int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout; |
| 2592 | unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc; |
Eric Dumazet | fc66f95 | 2010-10-08 06:37:34 +0000 | [diff] [blame] | 2593 | int entries; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2594 | |
Eric Dumazet | fc66f95 | 2010-10-08 06:37:34 +0000 | [diff] [blame] | 2595 | entries = dst_entries_get_fast(ops); |
Michal Kubeček | 49a18d8 | 2013-08-01 10:04:24 +0200 | [diff] [blame] | 2596 | if (time_after(rt_last_gc + rt_min_interval, jiffies) && |
Eric Dumazet | fc66f95 | 2010-10-08 06:37:34 +0000 | [diff] [blame] | 2597 | entries <= rt_max_size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2598 | goto out; |
| 2599 | |
Benjamin Thery | 6891a34 | 2008-03-04 13:49:47 -0800 | [diff] [blame] | 2600 | net->ipv6.ip6_rt_gc_expire++; |
Li RongQing | 1495664 | 2014-05-19 17:30:28 +0800 | [diff] [blame] | 2601 | fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net, true); |
Eric Dumazet | fc66f95 | 2010-10-08 06:37:34 +0000 | [diff] [blame] | 2602 | entries = dst_entries_get_slow(ops); |
| 2603 | if (entries < ops->gc_thresh) |
Daniel Lezcano | 7019b78 | 2008-03-04 13:50:14 -0800 | [diff] [blame] | 2604 | net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2605 | out: |
Daniel Lezcano | 7019b78 | 2008-03-04 13:50:14 -0800 | [diff] [blame] | 2606 | 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] | 2607 | return entries > rt_max_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2608 | } |
| 2609 | |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 2610 | static int ip6_convert_metrics(struct net *net, struct rt6_info *rt, |
| 2611 | struct fib6_config *cfg) |
Florian Westphal | e715b6d | 2015-01-05 23:57:44 +0100 | [diff] [blame] | 2612 | { |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 2613 | int err = 0; |
Florian Westphal | e715b6d | 2015-01-05 23:57:44 +0100 | [diff] [blame] | 2614 | |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 2615 | if (cfg->fc_mx) { |
| 2616 | rt->fib6_metrics = kzalloc(sizeof(*rt->fib6_metrics), |
| 2617 | GFP_KERNEL); |
| 2618 | if (unlikely(!rt->fib6_metrics)) |
| 2619 | return -ENOMEM; |
Florian Westphal | e715b6d | 2015-01-05 23:57:44 +0100 | [diff] [blame] | 2620 | |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 2621 | refcount_set(&rt->fib6_metrics->refcnt, 1); |
Florian Westphal | e715b6d | 2015-01-05 23:57:44 +0100 | [diff] [blame] | 2622 | |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 2623 | err = ip_metrics_convert(net, cfg->fc_mx, cfg->fc_mx_len, |
| 2624 | rt->fib6_metrics->metrics); |
Florian Westphal | e715b6d | 2015-01-05 23:57:44 +0100 | [diff] [blame] | 2625 | } |
| 2626 | |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 2627 | return err; |
Florian Westphal | e715b6d | 2015-01-05 23:57:44 +0100 | [diff] [blame] | 2628 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2629 | |
David Ahern | 8c14586 | 2016-04-24 21:26:04 -0700 | [diff] [blame] | 2630 | static struct rt6_info *ip6_nh_lookup_table(struct net *net, |
| 2631 | struct fib6_config *cfg, |
David Ahern | f4797b3 | 2018-01-25 16:55:08 -0800 | [diff] [blame] | 2632 | const struct in6_addr *gw_addr, |
| 2633 | u32 tbid, int flags) |
David Ahern | 8c14586 | 2016-04-24 21:26:04 -0700 | [diff] [blame] | 2634 | { |
| 2635 | struct flowi6 fl6 = { |
| 2636 | .flowi6_oif = cfg->fc_ifindex, |
| 2637 | .daddr = *gw_addr, |
| 2638 | .saddr = cfg->fc_prefsrc, |
| 2639 | }; |
| 2640 | struct fib6_table *table; |
| 2641 | struct rt6_info *rt; |
David Ahern | 8c14586 | 2016-04-24 21:26:04 -0700 | [diff] [blame] | 2642 | |
David Ahern | f4797b3 | 2018-01-25 16:55:08 -0800 | [diff] [blame] | 2643 | table = fib6_get_table(net, tbid); |
David Ahern | 8c14586 | 2016-04-24 21:26:04 -0700 | [diff] [blame] | 2644 | if (!table) |
| 2645 | return NULL; |
| 2646 | |
| 2647 | if (!ipv6_addr_any(&cfg->fc_prefsrc)) |
| 2648 | flags |= RT6_LOOKUP_F_HAS_SADDR; |
| 2649 | |
David Ahern | f4797b3 | 2018-01-25 16:55:08 -0800 | [diff] [blame] | 2650 | flags |= RT6_LOOKUP_F_IGNORE_LINKSTATE; |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 2651 | rt = ip6_pol_route(net, table, cfg->fc_ifindex, &fl6, NULL, flags); |
David Ahern | 8c14586 | 2016-04-24 21:26:04 -0700 | [diff] [blame] | 2652 | |
| 2653 | /* if table lookup failed, fall back to full lookup */ |
| 2654 | if (rt == net->ipv6.ip6_null_entry) { |
| 2655 | ip6_rt_put(rt); |
| 2656 | rt = NULL; |
| 2657 | } |
| 2658 | |
| 2659 | return rt; |
| 2660 | } |
| 2661 | |
David Ahern | fc1e64e | 2018-01-25 16:55:09 -0800 | [diff] [blame] | 2662 | static int ip6_route_check_nh_onlink(struct net *net, |
| 2663 | struct fib6_config *cfg, |
David Ahern | 9fbb704 | 2018-03-13 08:29:36 -0700 | [diff] [blame] | 2664 | const struct net_device *dev, |
David Ahern | fc1e64e | 2018-01-25 16:55:09 -0800 | [diff] [blame] | 2665 | struct netlink_ext_ack *extack) |
| 2666 | { |
David Ahern | 44750f8 | 2018-02-06 13:17:06 -0800 | [diff] [blame] | 2667 | u32 tbid = l3mdev_fib_table(dev) ? : RT_TABLE_MAIN; |
David Ahern | fc1e64e | 2018-01-25 16:55:09 -0800 | [diff] [blame] | 2668 | const struct in6_addr *gw_addr = &cfg->fc_gateway; |
| 2669 | u32 flags = RTF_LOCAL | RTF_ANYCAST | RTF_REJECT; |
| 2670 | struct rt6_info *grt; |
| 2671 | int err; |
| 2672 | |
| 2673 | err = 0; |
| 2674 | grt = ip6_nh_lookup_table(net, cfg, gw_addr, tbid, 0); |
| 2675 | if (grt) { |
David Ahern | 58e354c | 2018-02-06 12:14:12 -0800 | [diff] [blame] | 2676 | if (!grt->dst.error && |
| 2677 | (grt->rt6i_flags & flags || dev != grt->dst.dev)) { |
David Ahern | 44750f8 | 2018-02-06 13:17:06 -0800 | [diff] [blame] | 2678 | NL_SET_ERR_MSG(extack, |
| 2679 | "Nexthop has invalid gateway or device mismatch"); |
David Ahern | fc1e64e | 2018-01-25 16:55:09 -0800 | [diff] [blame] | 2680 | err = -EINVAL; |
| 2681 | } |
| 2682 | |
| 2683 | ip6_rt_put(grt); |
| 2684 | } |
| 2685 | |
| 2686 | return err; |
| 2687 | } |
| 2688 | |
David Ahern | 1edce99 | 2018-01-25 16:55:07 -0800 | [diff] [blame] | 2689 | static int ip6_route_check_nh(struct net *net, |
| 2690 | struct fib6_config *cfg, |
| 2691 | struct net_device **_dev, |
| 2692 | struct inet6_dev **idev) |
| 2693 | { |
| 2694 | const struct in6_addr *gw_addr = &cfg->fc_gateway; |
| 2695 | struct net_device *dev = _dev ? *_dev : NULL; |
| 2696 | struct rt6_info *grt = NULL; |
| 2697 | int err = -EHOSTUNREACH; |
| 2698 | |
| 2699 | if (cfg->fc_table) { |
David Ahern | f4797b3 | 2018-01-25 16:55:08 -0800 | [diff] [blame] | 2700 | int flags = RT6_LOOKUP_F_IFACE; |
| 2701 | |
| 2702 | grt = ip6_nh_lookup_table(net, cfg, gw_addr, |
| 2703 | cfg->fc_table, flags); |
David Ahern | 1edce99 | 2018-01-25 16:55:07 -0800 | [diff] [blame] | 2704 | if (grt) { |
| 2705 | if (grt->rt6i_flags & RTF_GATEWAY || |
| 2706 | (dev && dev != grt->dst.dev)) { |
| 2707 | ip6_rt_put(grt); |
| 2708 | grt = NULL; |
| 2709 | } |
| 2710 | } |
| 2711 | } |
| 2712 | |
| 2713 | if (!grt) |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 2714 | grt = rt6_lookup(net, gw_addr, NULL, cfg->fc_ifindex, NULL, 1); |
David Ahern | 1edce99 | 2018-01-25 16:55:07 -0800 | [diff] [blame] | 2715 | |
| 2716 | if (!grt) |
| 2717 | goto out; |
| 2718 | |
| 2719 | if (dev) { |
| 2720 | if (dev != grt->dst.dev) { |
| 2721 | ip6_rt_put(grt); |
| 2722 | goto out; |
| 2723 | } |
| 2724 | } else { |
| 2725 | *_dev = dev = grt->dst.dev; |
| 2726 | *idev = grt->rt6i_idev; |
| 2727 | dev_hold(dev); |
| 2728 | in6_dev_hold(grt->rt6i_idev); |
| 2729 | } |
| 2730 | |
| 2731 | if (!(grt->rt6i_flags & RTF_GATEWAY)) |
| 2732 | err = 0; |
| 2733 | |
| 2734 | ip6_rt_put(grt); |
| 2735 | |
| 2736 | out: |
| 2737 | return err; |
| 2738 | } |
| 2739 | |
David Ahern | 9fbb704 | 2018-03-13 08:29:36 -0700 | [diff] [blame] | 2740 | static int ip6_validate_gw(struct net *net, struct fib6_config *cfg, |
| 2741 | struct net_device **_dev, struct inet6_dev **idev, |
| 2742 | struct netlink_ext_ack *extack) |
| 2743 | { |
| 2744 | const struct in6_addr *gw_addr = &cfg->fc_gateway; |
| 2745 | int gwa_type = ipv6_addr_type(gw_addr); |
David Ahern | 232378e | 2018-03-13 08:29:37 -0700 | [diff] [blame] | 2746 | bool skip_dev = gwa_type & IPV6_ADDR_LINKLOCAL ? false : true; |
David Ahern | 9fbb704 | 2018-03-13 08:29:36 -0700 | [diff] [blame] | 2747 | const struct net_device *dev = *_dev; |
David Ahern | 232378e | 2018-03-13 08:29:37 -0700 | [diff] [blame] | 2748 | bool need_addr_check = !dev; |
David Ahern | 9fbb704 | 2018-03-13 08:29:36 -0700 | [diff] [blame] | 2749 | int err = -EINVAL; |
| 2750 | |
| 2751 | /* if gw_addr is local we will fail to detect this in case |
| 2752 | * address is still TENTATIVE (DAD in progress). rt6_lookup() |
| 2753 | * will return already-added prefix route via interface that |
| 2754 | * prefix route was assigned to, which might be non-loopback. |
| 2755 | */ |
David Ahern | 232378e | 2018-03-13 08:29:37 -0700 | [diff] [blame] | 2756 | if (dev && |
| 2757 | ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) { |
| 2758 | NL_SET_ERR_MSG(extack, "Gateway can not be a local address"); |
David Ahern | 9fbb704 | 2018-03-13 08:29:36 -0700 | [diff] [blame] | 2759 | goto out; |
| 2760 | } |
| 2761 | |
| 2762 | if (gwa_type != (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_UNICAST)) { |
| 2763 | /* IPv6 strictly inhibits using not link-local |
| 2764 | * addresses as nexthop address. |
| 2765 | * Otherwise, router will not able to send redirects. |
| 2766 | * It is very good, but in some (rare!) circumstances |
| 2767 | * (SIT, PtP, NBMA NOARP links) it is handy to allow |
| 2768 | * some exceptions. --ANK |
| 2769 | * We allow IPv4-mapped nexthops to support RFC4798-type |
| 2770 | * addressing |
| 2771 | */ |
| 2772 | if (!(gwa_type & (IPV6_ADDR_UNICAST | IPV6_ADDR_MAPPED))) { |
| 2773 | NL_SET_ERR_MSG(extack, "Invalid gateway address"); |
| 2774 | goto out; |
| 2775 | } |
| 2776 | |
| 2777 | if (cfg->fc_flags & RTNH_F_ONLINK) |
| 2778 | err = ip6_route_check_nh_onlink(net, cfg, dev, extack); |
| 2779 | else |
| 2780 | err = ip6_route_check_nh(net, cfg, _dev, idev); |
| 2781 | |
| 2782 | if (err) |
| 2783 | goto out; |
| 2784 | } |
| 2785 | |
| 2786 | /* reload in case device was changed */ |
| 2787 | dev = *_dev; |
| 2788 | |
| 2789 | err = -EINVAL; |
| 2790 | if (!dev) { |
| 2791 | NL_SET_ERR_MSG(extack, "Egress device not specified"); |
| 2792 | goto out; |
| 2793 | } else if (dev->flags & IFF_LOOPBACK) { |
| 2794 | NL_SET_ERR_MSG(extack, |
| 2795 | "Egress device can not be loopback device for this route"); |
| 2796 | goto out; |
| 2797 | } |
David Ahern | 232378e | 2018-03-13 08:29:37 -0700 | [diff] [blame] | 2798 | |
| 2799 | /* if we did not check gw_addr above, do so now that the |
| 2800 | * egress device has been resolved. |
| 2801 | */ |
| 2802 | if (need_addr_check && |
| 2803 | ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) { |
| 2804 | NL_SET_ERR_MSG(extack, "Gateway can not be a local address"); |
| 2805 | goto out; |
| 2806 | } |
| 2807 | |
David Ahern | 9fbb704 | 2018-03-13 08:29:36 -0700 | [diff] [blame] | 2808 | err = 0; |
| 2809 | out: |
| 2810 | return err; |
| 2811 | } |
| 2812 | |
David Ahern | 333c430 | 2017-05-21 10:12:04 -0600 | [diff] [blame] | 2813 | static struct rt6_info *ip6_route_info_create(struct fib6_config *cfg, |
| 2814 | struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2815 | { |
Daniel Lezcano | 5578689 | 2008-03-04 13:47:47 -0800 | [diff] [blame] | 2816 | struct net *net = cfg->fc_nlinfo.nl_net; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2817 | struct rt6_info *rt = NULL; |
| 2818 | struct net_device *dev = NULL; |
| 2819 | struct inet6_dev *idev = NULL; |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 2820 | struct fib6_table *table; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2821 | int addr_type; |
Roopa Prabhu | 8c5b83f | 2015-10-10 08:26:36 -0700 | [diff] [blame] | 2822 | int err = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2823 | |
David Ahern | 557c44b | 2017-04-19 14:19:43 -0700 | [diff] [blame] | 2824 | /* RTF_PCPU is an internal flag; can not be set by userspace */ |
David Ahern | d5d531c | 2017-05-21 10:12:05 -0600 | [diff] [blame] | 2825 | if (cfg->fc_flags & RTF_PCPU) { |
| 2826 | NL_SET_ERR_MSG(extack, "Userspace can not set RTF_PCPU"); |
David Ahern | 557c44b | 2017-04-19 14:19:43 -0700 | [diff] [blame] | 2827 | goto out; |
David Ahern | d5d531c | 2017-05-21 10:12:05 -0600 | [diff] [blame] | 2828 | } |
David Ahern | 557c44b | 2017-04-19 14:19:43 -0700 | [diff] [blame] | 2829 | |
Wei Wang | 2ea2352 | 2017-10-27 17:30:12 -0700 | [diff] [blame] | 2830 | /* RTF_CACHE is an internal flag; can not be set by userspace */ |
| 2831 | if (cfg->fc_flags & RTF_CACHE) { |
| 2832 | NL_SET_ERR_MSG(extack, "Userspace can not set RTF_CACHE"); |
| 2833 | goto out; |
| 2834 | } |
| 2835 | |
David Ahern | e8478e8 | 2018-04-17 17:33:13 -0700 | [diff] [blame] | 2836 | if (cfg->fc_type > RTN_MAX) { |
| 2837 | NL_SET_ERR_MSG(extack, "Invalid route type"); |
| 2838 | goto out; |
| 2839 | } |
| 2840 | |
David Ahern | d5d531c | 2017-05-21 10:12:05 -0600 | [diff] [blame] | 2841 | if (cfg->fc_dst_len > 128) { |
| 2842 | NL_SET_ERR_MSG(extack, "Invalid prefix length"); |
Roopa Prabhu | 8c5b83f | 2015-10-10 08:26:36 -0700 | [diff] [blame] | 2843 | goto out; |
David Ahern | d5d531c | 2017-05-21 10:12:05 -0600 | [diff] [blame] | 2844 | } |
| 2845 | if (cfg->fc_src_len > 128) { |
| 2846 | NL_SET_ERR_MSG(extack, "Invalid source address length"); |
| 2847 | goto out; |
| 2848 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2849 | #ifndef CONFIG_IPV6_SUBTREES |
David Ahern | d5d531c | 2017-05-21 10:12:05 -0600 | [diff] [blame] | 2850 | if (cfg->fc_src_len) { |
| 2851 | NL_SET_ERR_MSG(extack, |
| 2852 | "Specifying source address requires IPV6_SUBTREES to be enabled"); |
Roopa Prabhu | 8c5b83f | 2015-10-10 08:26:36 -0700 | [diff] [blame] | 2853 | goto out; |
David Ahern | d5d531c | 2017-05-21 10:12:05 -0600 | [diff] [blame] | 2854 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2855 | #endif |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 2856 | if (cfg->fc_ifindex) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2857 | err = -ENODEV; |
Daniel Lezcano | 5578689 | 2008-03-04 13:47:47 -0800 | [diff] [blame] | 2858 | dev = dev_get_by_index(net, cfg->fc_ifindex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2859 | if (!dev) |
| 2860 | goto out; |
| 2861 | idev = in6_dev_get(dev); |
| 2862 | if (!idev) |
| 2863 | goto out; |
| 2864 | } |
| 2865 | |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 2866 | if (cfg->fc_metric == 0) |
| 2867 | cfg->fc_metric = IP6_RT_PRIO_USER; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2868 | |
David Ahern | fc1e64e | 2018-01-25 16:55:09 -0800 | [diff] [blame] | 2869 | if (cfg->fc_flags & RTNH_F_ONLINK) { |
| 2870 | if (!dev) { |
| 2871 | NL_SET_ERR_MSG(extack, |
| 2872 | "Nexthop device required for onlink"); |
| 2873 | err = -ENODEV; |
| 2874 | goto out; |
| 2875 | } |
| 2876 | |
| 2877 | if (!(dev->flags & IFF_UP)) { |
| 2878 | NL_SET_ERR_MSG(extack, "Nexthop device is not up"); |
| 2879 | err = -ENETDOWN; |
| 2880 | goto out; |
| 2881 | } |
| 2882 | } |
| 2883 | |
Matti Vaittinen | d71314b | 2011-11-14 00:14:49 +0000 | [diff] [blame] | 2884 | err = -ENOBUFS; |
David S. Miller | 3830847 | 2011-12-03 18:02:47 -0500 | [diff] [blame] | 2885 | if (cfg->fc_nlinfo.nlh && |
| 2886 | !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) { |
Matti Vaittinen | d71314b | 2011-11-14 00:14:49 +0000 | [diff] [blame] | 2887 | table = fib6_get_table(net, cfg->fc_table); |
David S. Miller | 3830847 | 2011-12-03 18:02:47 -0500 | [diff] [blame] | 2888 | if (!table) { |
Joe Perches | f321383 | 2012-05-15 14:11:53 +0000 | [diff] [blame] | 2889 | 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] | 2890 | table = fib6_new_table(net, cfg->fc_table); |
| 2891 | } |
| 2892 | } else { |
| 2893 | table = fib6_new_table(net, cfg->fc_table); |
| 2894 | } |
David S. Miller | 3830847 | 2011-12-03 18:02:47 -0500 | [diff] [blame] | 2895 | |
| 2896 | if (!table) |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 2897 | goto out; |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 2898 | |
Martin KaFai Lau | ad70686 | 2015-08-14 11:05:52 -0700 | [diff] [blame] | 2899 | rt = ip6_dst_alloc(net, NULL, |
| 2900 | (cfg->fc_flags & RTF_ADDRCONF) ? 0 : DST_NOCOUNT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2901 | |
David S. Miller | 3830847 | 2011-12-03 18:02:47 -0500 | [diff] [blame] | 2902 | if (!rt) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2903 | err = -ENOMEM; |
| 2904 | goto out; |
| 2905 | } |
| 2906 | |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 2907 | err = ip6_convert_metrics(net, rt, cfg); |
| 2908 | if (err < 0) |
| 2909 | goto out; |
| 2910 | |
Gao feng | 1716a96 | 2012-04-06 00:13:10 +0000 | [diff] [blame] | 2911 | if (cfg->fc_flags & RTF_EXPIRES) |
David Ahern | 1489568 | 2018-04-17 17:33:17 -0700 | [diff] [blame^] | 2912 | fib6_set_expires(rt, jiffies + |
Gao feng | 1716a96 | 2012-04-06 00:13:10 +0000 | [diff] [blame] | 2913 | clock_t_to_jiffies(cfg->fc_expires)); |
| 2914 | else |
David Ahern | 1489568 | 2018-04-17 17:33:17 -0700 | [diff] [blame^] | 2915 | fib6_clean_expires(rt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2916 | |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 2917 | if (cfg->fc_protocol == RTPROT_UNSPEC) |
| 2918 | cfg->fc_protocol = RTPROT_BOOT; |
| 2919 | rt->rt6i_protocol = cfg->fc_protocol; |
| 2920 | |
| 2921 | addr_type = ipv6_addr_type(&cfg->fc_dst); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2922 | |
Roopa Prabhu | 19e42e4 | 2015-07-21 10:43:48 +0200 | [diff] [blame] | 2923 | if (cfg->fc_encap) { |
| 2924 | struct lwtunnel_state *lwtstate; |
| 2925 | |
David Ahern | 30357d7 | 2017-01-30 12:07:37 -0800 | [diff] [blame] | 2926 | err = lwtunnel_build_state(cfg->fc_encap_type, |
Tom Herbert | 127eb7c | 2015-08-24 09:45:41 -0700 | [diff] [blame] | 2927 | cfg->fc_encap, AF_INET6, cfg, |
David Ahern | 9ae2872 | 2017-05-27 16:19:28 -0600 | [diff] [blame] | 2928 | &lwtstate, extack); |
Roopa Prabhu | 19e42e4 | 2015-07-21 10:43:48 +0200 | [diff] [blame] | 2929 | if (err) |
| 2930 | goto out; |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 2931 | rt->fib6_nh.nh_lwtstate = lwtstate_get(lwtstate); |
Roopa Prabhu | 19e42e4 | 2015-07-21 10:43:48 +0200 | [diff] [blame] | 2932 | } |
| 2933 | |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 2934 | ipv6_addr_prefix(&rt->rt6i_dst.addr, &cfg->fc_dst, cfg->fc_dst_len); |
| 2935 | rt->rt6i_dst.plen = cfg->fc_dst_len; |
Martin KaFai Lau | afc4eef | 2015-04-28 13:03:07 -0700 | [diff] [blame] | 2936 | if (rt->rt6i_dst.plen == 128) |
Michal Kubeček | e5fd387 | 2014-03-27 13:04:08 +0100 | [diff] [blame] | 2937 | rt->dst.flags |= DST_HOST; |
Michal Kubeček | e5fd387 | 2014-03-27 13:04:08 +0100 | [diff] [blame] | 2938 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2939 | #ifdef CONFIG_IPV6_SUBTREES |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 2940 | ipv6_addr_prefix(&rt->rt6i_src.addr, &cfg->fc_src, cfg->fc_src_len); |
| 2941 | rt->rt6i_src.plen = cfg->fc_src_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2942 | #endif |
| 2943 | |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 2944 | rt->rt6i_metric = cfg->fc_metric; |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 2945 | rt->fib6_nh.nh_weight = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2946 | |
David Ahern | e8478e8 | 2018-04-17 17:33:13 -0700 | [diff] [blame] | 2947 | rt->fib6_type = cfg->fc_type; |
| 2948 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2949 | /* We cannot add true routes via loopback here, |
| 2950 | they would result in kernel looping; promote them to reject routes |
| 2951 | */ |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 2952 | if ((cfg->fc_flags & RTF_REJECT) || |
David S. Miller | 3830847 | 2011-12-03 18:02:47 -0500 | [diff] [blame] | 2953 | (dev && (dev->flags & IFF_LOOPBACK) && |
| 2954 | !(addr_type & IPV6_ADDR_LOOPBACK) && |
| 2955 | !(cfg->fc_flags & RTF_LOCAL))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2956 | /* hold loopback dev/idev if we haven't done so. */ |
Daniel Lezcano | 5578689 | 2008-03-04 13:47:47 -0800 | [diff] [blame] | 2957 | if (dev != net->loopback_dev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2958 | if (dev) { |
| 2959 | dev_put(dev); |
| 2960 | in6_dev_put(idev); |
| 2961 | } |
Daniel Lezcano | 5578689 | 2008-03-04 13:47:47 -0800 | [diff] [blame] | 2962 | dev = net->loopback_dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2963 | dev_hold(dev); |
| 2964 | idev = in6_dev_get(dev); |
| 2965 | if (!idev) { |
| 2966 | err = -ENODEV; |
| 2967 | goto out; |
| 2968 | } |
| 2969 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2970 | rt->rt6i_flags = RTF_REJECT|RTF_NONEXTHOP; |
| 2971 | goto install_route; |
| 2972 | } |
| 2973 | |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 2974 | if (cfg->fc_flags & RTF_GATEWAY) { |
David Ahern | 9fbb704 | 2018-03-13 08:29:36 -0700 | [diff] [blame] | 2975 | err = ip6_validate_gw(net, cfg, &dev, &idev, extack); |
| 2976 | if (err) |
Florian Westphal | 48ed7b2 | 2015-05-21 00:25:41 +0200 | [diff] [blame] | 2977 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2978 | |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 2979 | rt->fib6_nh.nh_gw = rt->rt6i_gateway = cfg->fc_gateway; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2980 | } |
| 2981 | |
| 2982 | err = -ENODEV; |
David S. Miller | 3830847 | 2011-12-03 18:02:47 -0500 | [diff] [blame] | 2983 | if (!dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2984 | goto out; |
| 2985 | |
Lorenzo Bianconi | 428604f | 2018-03-29 11:02:24 +0200 | [diff] [blame] | 2986 | if (idev->cnf.disable_ipv6) { |
| 2987 | NL_SET_ERR_MSG(extack, "IPv6 is disabled on nexthop device"); |
| 2988 | err = -EACCES; |
| 2989 | goto out; |
| 2990 | } |
| 2991 | |
David Ahern | 955ec4c | 2018-01-24 19:45:29 -0800 | [diff] [blame] | 2992 | if (!(dev->flags & IFF_UP)) { |
| 2993 | NL_SET_ERR_MSG(extack, "Nexthop device is not up"); |
| 2994 | err = -ENETDOWN; |
| 2995 | goto out; |
| 2996 | } |
| 2997 | |
Daniel Walter | c3968a8 | 2011-04-13 21:10:57 +0000 | [diff] [blame] | 2998 | if (!ipv6_addr_any(&cfg->fc_prefsrc)) { |
| 2999 | if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) { |
David Ahern | d5d531c | 2017-05-21 10:12:05 -0600 | [diff] [blame] | 3000 | NL_SET_ERR_MSG(extack, "Invalid source address"); |
Daniel Walter | c3968a8 | 2011-04-13 21:10:57 +0000 | [diff] [blame] | 3001 | err = -EINVAL; |
| 3002 | goto out; |
| 3003 | } |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 3004 | rt->rt6i_prefsrc.addr = cfg->fc_prefsrc; |
Daniel Walter | c3968a8 | 2011-04-13 21:10:57 +0000 | [diff] [blame] | 3005 | rt->rt6i_prefsrc.plen = 128; |
| 3006 | } else |
| 3007 | rt->rt6i_prefsrc.plen = 0; |
| 3008 | |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 3009 | rt->rt6i_flags = cfg->fc_flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3010 | |
| 3011 | install_route: |
Ido Schimmel | 5609b80 | 2018-01-07 12:45:06 +0200 | [diff] [blame] | 3012 | if (!(rt->rt6i_flags & (RTF_LOCAL | RTF_ANYCAST)) && |
| 3013 | !netif_carrier_ok(dev)) |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 3014 | rt->fib6_nh.nh_flags |= RTNH_F_LINKDOWN; |
| 3015 | rt->fib6_nh.nh_flags |= (cfg->fc_flags & RTNH_F_ONLINK); |
| 3016 | rt->fib6_nh.nh_dev = rt->dst.dev = dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3017 | rt->rt6i_idev = idev; |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 3018 | rt->rt6i_table = table; |
Daniel Lezcano | 63152fc | 2008-03-03 23:31:11 -0800 | [diff] [blame] | 3019 | |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 3020 | cfg->fc_nlinfo.nl_net = dev_net(dev); |
Daniel Lezcano | 63152fc | 2008-03-03 23:31:11 -0800 | [diff] [blame] | 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); |
Wei Wang | 587fea7 | 2017-06-17 10:42:36 -0700 | [diff] [blame] | 3028 | if (rt) |
| 3029 | dst_release_immediate(&rt->dst); |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 3030 | |
Roopa Prabhu | 8c5b83f | 2015-10-10 08:26:36 -0700 | [diff] [blame] | 3031 | return ERR_PTR(err); |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 3032 | } |
| 3033 | |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 3034 | int ip6_route_add(struct fib6_config *cfg, struct netlink_ext_ack *extack) |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 3035 | { |
Roopa Prabhu | 8c5b83f | 2015-10-10 08:26:36 -0700 | [diff] [blame] | 3036 | struct rt6_info *rt; |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 3037 | int err; |
| 3038 | |
David Ahern | 333c430 | 2017-05-21 10:12:04 -0600 | [diff] [blame] | 3039 | rt = ip6_route_info_create(cfg, 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); |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 3044 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3045 | return err; |
| 3046 | } |
| 3047 | |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 3048 | static int __ip6_del_rt(struct rt6_info *rt, struct nl_info *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3049 | { |
David Ahern | afb1d4b5 | 2018-04-17 17:33:11 -0700 | [diff] [blame] | 3050 | struct net *net = info->nl_net; |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 3051 | struct fib6_table *table; |
David Ahern | afb1d4b5 | 2018-04-17 17:33:11 -0700 | [diff] [blame] | 3052 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3053 | |
Wei Wang | a4c2fd7 | 2017-06-17 10:42:42 -0700 | [diff] [blame] | 3054 | if (rt == net->ipv6.ip6_null_entry) { |
Gao feng | 6825a26 | 2012-09-19 19:25:34 +0000 | [diff] [blame] | 3055 | err = -ENOENT; |
| 3056 | goto out; |
| 3057 | } |
Patrick McHardy | 6c813a7 | 2006-08-06 22:22:47 -0700 | [diff] [blame] | 3058 | |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 3059 | table = rt->rt6i_table; |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 3060 | spin_lock_bh(&table->tb6_lock); |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 3061 | err = fib6_del(rt, info); |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 3062 | spin_unlock_bh(&table->tb6_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3063 | |
Gao feng | 6825a26 | 2012-09-19 19:25:34 +0000 | [diff] [blame] | 3064 | out: |
Amerigo Wang | 94e187c | 2012-10-29 00:13:19 +0000 | [diff] [blame] | 3065 | ip6_rt_put(rt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3066 | return err; |
| 3067 | } |
| 3068 | |
David Ahern | afb1d4b5 | 2018-04-17 17:33:11 -0700 | [diff] [blame] | 3069 | int ip6_del_rt(struct net *net, struct rt6_info *rt) |
Thomas Graf | e0a1ad73 | 2006-08-22 00:00:21 -0700 | [diff] [blame] | 3070 | { |
David Ahern | afb1d4b5 | 2018-04-17 17:33:11 -0700 | [diff] [blame] | 3071 | struct nl_info info = { .nl_net = net }; |
| 3072 | |
Denis V. Lunev | 528c4ce | 2007-12-13 09:45:12 -0800 | [diff] [blame] | 3073 | return __ip6_del_rt(rt, &info); |
Thomas Graf | e0a1ad73 | 2006-08-22 00:00:21 -0700 | [diff] [blame] | 3074 | } |
| 3075 | |
David Ahern | 0ae8133 | 2017-02-02 12:37:08 -0800 | [diff] [blame] | 3076 | static int __ip6_del_rt_siblings(struct rt6_info *rt, struct fib6_config *cfg) |
| 3077 | { |
| 3078 | struct nl_info *info = &cfg->fc_nlinfo; |
WANG Cong | e333003 | 2017-02-27 16:07:43 -0800 | [diff] [blame] | 3079 | struct net *net = info->nl_net; |
David Ahern | 16a16cd | 2017-02-02 12:37:11 -0800 | [diff] [blame] | 3080 | struct sk_buff *skb = NULL; |
David Ahern | 0ae8133 | 2017-02-02 12:37:08 -0800 | [diff] [blame] | 3081 | struct fib6_table *table; |
WANG Cong | e333003 | 2017-02-27 16:07:43 -0800 | [diff] [blame] | 3082 | int err = -ENOENT; |
David Ahern | 0ae8133 | 2017-02-02 12:37:08 -0800 | [diff] [blame] | 3083 | |
WANG Cong | e333003 | 2017-02-27 16:07:43 -0800 | [diff] [blame] | 3084 | if (rt == net->ipv6.ip6_null_entry) |
| 3085 | goto out_put; |
David Ahern | 0ae8133 | 2017-02-02 12:37:08 -0800 | [diff] [blame] | 3086 | table = rt->rt6i_table; |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 3087 | spin_lock_bh(&table->tb6_lock); |
David Ahern | 0ae8133 | 2017-02-02 12:37:08 -0800 | [diff] [blame] | 3088 | |
| 3089 | if (rt->rt6i_nsiblings && cfg->fc_delete_all_nh) { |
| 3090 | struct rt6_info *sibling, *next_sibling; |
| 3091 | |
David Ahern | 16a16cd | 2017-02-02 12:37:11 -0800 | [diff] [blame] | 3092 | /* prefer to send a single notification with all hops */ |
| 3093 | skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any()); |
| 3094 | if (skb) { |
| 3095 | u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0; |
| 3096 | |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 3097 | if (rt6_fill_node(net, skb, rt, NULL, |
David Ahern | 16a16cd | 2017-02-02 12:37:11 -0800 | [diff] [blame] | 3098 | NULL, NULL, 0, RTM_DELROUTE, |
| 3099 | info->portid, seq, 0) < 0) { |
| 3100 | kfree_skb(skb); |
| 3101 | skb = NULL; |
| 3102 | } else |
| 3103 | info->skip_notify = 1; |
| 3104 | } |
| 3105 | |
David Ahern | 0ae8133 | 2017-02-02 12:37:08 -0800 | [diff] [blame] | 3106 | list_for_each_entry_safe(sibling, next_sibling, |
| 3107 | &rt->rt6i_siblings, |
| 3108 | rt6i_siblings) { |
| 3109 | err = fib6_del(sibling, info); |
| 3110 | if (err) |
WANG Cong | e333003 | 2017-02-27 16:07:43 -0800 | [diff] [blame] | 3111 | goto out_unlock; |
David Ahern | 0ae8133 | 2017-02-02 12:37:08 -0800 | [diff] [blame] | 3112 | } |
| 3113 | } |
| 3114 | |
| 3115 | err = fib6_del(rt, info); |
WANG Cong | e333003 | 2017-02-27 16:07:43 -0800 | [diff] [blame] | 3116 | out_unlock: |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 3117 | spin_unlock_bh(&table->tb6_lock); |
WANG Cong | e333003 | 2017-02-27 16:07:43 -0800 | [diff] [blame] | 3118 | out_put: |
David Ahern | 0ae8133 | 2017-02-02 12:37:08 -0800 | [diff] [blame] | 3119 | ip6_rt_put(rt); |
David Ahern | 16a16cd | 2017-02-02 12:37:11 -0800 | [diff] [blame] | 3120 | |
| 3121 | if (skb) { |
WANG Cong | e333003 | 2017-02-27 16:07:43 -0800 | [diff] [blame] | 3122 | rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE, |
David Ahern | 16a16cd | 2017-02-02 12:37:11 -0800 | [diff] [blame] | 3123 | info->nlh, gfp_any()); |
| 3124 | } |
David Ahern | 0ae8133 | 2017-02-02 12:37:08 -0800 | [diff] [blame] | 3125 | return err; |
| 3126 | } |
| 3127 | |
David Ahern | 333c430 | 2017-05-21 10:12:04 -0600 | [diff] [blame] | 3128 | static int ip6_route_del(struct fib6_config *cfg, |
| 3129 | struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3130 | { |
Wei Wang | 2b760fc | 2017-10-06 12:06:03 -0700 | [diff] [blame] | 3131 | struct rt6_info *rt, *rt_cache; |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 3132 | struct fib6_table *table; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3133 | struct fib6_node *fn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3134 | int err = -ESRCH; |
| 3135 | |
Daniel Lezcano | 5578689 | 2008-03-04 13:47:47 -0800 | [diff] [blame] | 3136 | table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table); |
David Ahern | d5d531c | 2017-05-21 10:12:05 -0600 | [diff] [blame] | 3137 | if (!table) { |
| 3138 | NL_SET_ERR_MSG(extack, "FIB table does not exist"); |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 3139 | return err; |
David Ahern | d5d531c | 2017-05-21 10:12:05 -0600 | [diff] [blame] | 3140 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3141 | |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 3142 | rcu_read_lock(); |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 3143 | |
| 3144 | fn = fib6_locate(&table->tb6_root, |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 3145 | &cfg->fc_dst, cfg->fc_dst_len, |
Wei Wang | 38fbeee | 2017-10-06 12:06:02 -0700 | [diff] [blame] | 3146 | &cfg->fc_src, cfg->fc_src_len, |
Wei Wang | 2b760fc | 2017-10-06 12:06:03 -0700 | [diff] [blame] | 3147 | !(cfg->fc_flags & RTF_CACHE)); |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 3148 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3149 | if (fn) { |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 3150 | for_each_fib6_node_rt_rcu(fn) { |
Wei Wang | 2b760fc | 2017-10-06 12:06:03 -0700 | [diff] [blame] | 3151 | if (cfg->fc_flags & RTF_CACHE) { |
| 3152 | rt_cache = rt6_find_cached_rt(rt, &cfg->fc_dst, |
| 3153 | &cfg->fc_src); |
| 3154 | if (!rt_cache) |
| 3155 | continue; |
| 3156 | rt = rt_cache; |
| 3157 | } |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 3158 | if (cfg->fc_ifindex && |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 3159 | (!rt->fib6_nh.nh_dev || |
| 3160 | rt->fib6_nh.nh_dev->ifindex != cfg->fc_ifindex)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3161 | continue; |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 3162 | if (cfg->fc_flags & RTF_GATEWAY && |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 3163 | !ipv6_addr_equal(&cfg->fc_gateway, &rt->fib6_nh.nh_gw)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3164 | continue; |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 3165 | if (cfg->fc_metric && cfg->fc_metric != rt->rt6i_metric) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3166 | continue; |
Mantas M | c2ed188 | 2016-12-16 10:30:59 +0200 | [diff] [blame] | 3167 | if (cfg->fc_protocol && cfg->fc_protocol != rt->rt6i_protocol) |
| 3168 | continue; |
Wei Wang | d3843fe | 2017-10-06 12:06:06 -0700 | [diff] [blame] | 3169 | if (!dst_hold_safe(&rt->dst)) |
| 3170 | break; |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 3171 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3172 | |
David Ahern | 0ae8133 | 2017-02-02 12:37:08 -0800 | [diff] [blame] | 3173 | /* if gateway was specified only delete the one hop */ |
| 3174 | if (cfg->fc_flags & RTF_GATEWAY) |
| 3175 | return __ip6_del_rt(rt, &cfg->fc_nlinfo); |
| 3176 | |
| 3177 | return __ip6_del_rt_siblings(rt, cfg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3178 | } |
| 3179 | } |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 3180 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3181 | |
| 3182 | return err; |
| 3183 | } |
| 3184 | |
David S. Miller | 6700c27 | 2012-07-17 03:29:28 -0700 | [diff] [blame] | 3185 | 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] | 3186 | { |
YOSHIFUJI Hideaki | a627945 | 2006-08-23 17:18:26 -0700 | [diff] [blame] | 3187 | struct netevent_redirect netevent; |
David S. Miller | e8599ff | 2012-07-11 23:43:53 -0700 | [diff] [blame] | 3188 | struct rt6_info *rt, *nrt = NULL; |
David S. Miller | e8599ff | 2012-07-11 23:43:53 -0700 | [diff] [blame] | 3189 | struct ndisc_options ndopts; |
| 3190 | struct inet6_dev *in6_dev; |
| 3191 | struct neighbour *neigh; |
YOSHIFUJI Hideaki / 吉藤英明 | 71bcdba | 2013-01-05 16:34:51 +0000 | [diff] [blame] | 3192 | struct rd_msg *msg; |
David S. Miller | 6e157b6 | 2012-07-12 00:05:02 -0700 | [diff] [blame] | 3193 | int optlen, on_link; |
| 3194 | u8 *lladdr; |
David S. Miller | e8599ff | 2012-07-11 23:43:53 -0700 | [diff] [blame] | 3195 | |
Simon Horman | 29a3cad | 2013-05-28 20:34:26 +0000 | [diff] [blame] | 3196 | optlen = skb_tail_pointer(skb) - skb_transport_header(skb); |
YOSHIFUJI Hideaki / 吉藤英明 | 71bcdba | 2013-01-05 16:34:51 +0000 | [diff] [blame] | 3197 | optlen -= sizeof(*msg); |
David S. Miller | e8599ff | 2012-07-11 23:43:53 -0700 | [diff] [blame] | 3198 | |
| 3199 | if (optlen < 0) { |
David S. Miller | 6e157b6 | 2012-07-12 00:05:02 -0700 | [diff] [blame] | 3200 | net_dbg_ratelimited("rt6_do_redirect: packet too short\n"); |
David S. Miller | e8599ff | 2012-07-11 23:43:53 -0700 | [diff] [blame] | 3201 | return; |
| 3202 | } |
| 3203 | |
YOSHIFUJI Hideaki / 吉藤英明 | 71bcdba | 2013-01-05 16:34:51 +0000 | [diff] [blame] | 3204 | msg = (struct rd_msg *)icmp6_hdr(skb); |
David S. Miller | e8599ff | 2012-07-11 23:43:53 -0700 | [diff] [blame] | 3205 | |
YOSHIFUJI Hideaki / 吉藤英明 | 71bcdba | 2013-01-05 16:34:51 +0000 | [diff] [blame] | 3206 | if (ipv6_addr_is_multicast(&msg->dest)) { |
David S. Miller | 6e157b6 | 2012-07-12 00:05:02 -0700 | [diff] [blame] | 3207 | net_dbg_ratelimited("rt6_do_redirect: destination address is multicast\n"); |
David S. Miller | e8599ff | 2012-07-11 23:43:53 -0700 | [diff] [blame] | 3208 | return; |
| 3209 | } |
| 3210 | |
David S. Miller | 6e157b6 | 2012-07-12 00:05:02 -0700 | [diff] [blame] | 3211 | on_link = 0; |
YOSHIFUJI Hideaki / 吉藤英明 | 71bcdba | 2013-01-05 16:34:51 +0000 | [diff] [blame] | 3212 | if (ipv6_addr_equal(&msg->dest, &msg->target)) { |
David S. Miller | e8599ff | 2012-07-11 23:43:53 -0700 | [diff] [blame] | 3213 | on_link = 1; |
YOSHIFUJI Hideaki / 吉藤英明 | 71bcdba | 2013-01-05 16:34:51 +0000 | [diff] [blame] | 3214 | } else if (ipv6_addr_type(&msg->target) != |
David S. Miller | e8599ff | 2012-07-11 23:43:53 -0700 | [diff] [blame] | 3215 | (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) { |
David S. Miller | 6e157b6 | 2012-07-12 00:05:02 -0700 | [diff] [blame] | 3216 | 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] | 3217 | return; |
| 3218 | } |
| 3219 | |
| 3220 | in6_dev = __in6_dev_get(skb->dev); |
| 3221 | if (!in6_dev) |
| 3222 | return; |
| 3223 | if (in6_dev->cnf.forwarding || !in6_dev->cnf.accept_redirects) |
| 3224 | return; |
| 3225 | |
| 3226 | /* RFC2461 8.1: |
| 3227 | * The IP source address of the Redirect MUST be the same as the current |
| 3228 | * first-hop router for the specified ICMP Destination Address. |
| 3229 | */ |
| 3230 | |
Alexander Aring | f997c55 | 2016-06-15 21:20:23 +0200 | [diff] [blame] | 3231 | if (!ndisc_parse_options(skb->dev, msg->opt, optlen, &ndopts)) { |
David S. Miller | e8599ff | 2012-07-11 23:43:53 -0700 | [diff] [blame] | 3232 | net_dbg_ratelimited("rt6_redirect: invalid ND options\n"); |
| 3233 | return; |
| 3234 | } |
David S. Miller | 6e157b6 | 2012-07-12 00:05:02 -0700 | [diff] [blame] | 3235 | |
| 3236 | lladdr = NULL; |
David S. Miller | e8599ff | 2012-07-11 23:43:53 -0700 | [diff] [blame] | 3237 | if (ndopts.nd_opts_tgt_lladdr) { |
| 3238 | lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr, |
| 3239 | skb->dev); |
| 3240 | if (!lladdr) { |
| 3241 | net_dbg_ratelimited("rt6_redirect: invalid link-layer address length\n"); |
| 3242 | return; |
| 3243 | } |
| 3244 | } |
| 3245 | |
David S. Miller | 6e157b6 | 2012-07-12 00:05:02 -0700 | [diff] [blame] | 3246 | rt = (struct rt6_info *) dst; |
Matthias Schiffer | ec13ad1 | 2015-11-02 01:24:38 +0100 | [diff] [blame] | 3247 | if (rt->rt6i_flags & RTF_REJECT) { |
David S. Miller | 6e157b6 | 2012-07-12 00:05:02 -0700 | [diff] [blame] | 3248 | net_dbg_ratelimited("rt6_redirect: source isn't a valid nexthop for redirect target\n"); |
| 3249 | return; |
| 3250 | } |
| 3251 | |
| 3252 | /* Redirect received -> path was valid. |
| 3253 | * Look, redirects are sent only in response to data packets, |
| 3254 | * so that this nexthop apparently is reachable. --ANK |
| 3255 | */ |
Julian Anastasov | 0dec879 | 2017-02-06 23:14:16 +0200 | [diff] [blame] | 3256 | dst_confirm_neigh(&rt->dst, &ipv6_hdr(skb)->saddr); |
David S. Miller | 6e157b6 | 2012-07-12 00:05:02 -0700 | [diff] [blame] | 3257 | |
YOSHIFUJI Hideaki / 吉藤英明 | 71bcdba | 2013-01-05 16:34:51 +0000 | [diff] [blame] | 3258 | neigh = __neigh_lookup(&nd_tbl, &msg->target, skb->dev, 1); |
David S. Miller | e8599ff | 2012-07-11 23:43:53 -0700 | [diff] [blame] | 3259 | if (!neigh) |
| 3260 | return; |
| 3261 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3262 | /* |
| 3263 | * We have finally decided to accept it. |
| 3264 | */ |
| 3265 | |
Alexander Aring | f997c55 | 2016-06-15 21:20:23 +0200 | [diff] [blame] | 3266 | ndisc_update(skb->dev, neigh, lladdr, NUD_STALE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3267 | NEIGH_UPDATE_F_WEAK_OVERRIDE| |
| 3268 | NEIGH_UPDATE_F_OVERRIDE| |
| 3269 | (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER| |
Alexander Aring | f997c55 | 2016-06-15 21:20:23 +0200 | [diff] [blame] | 3270 | NEIGH_UPDATE_F_ISROUTER)), |
| 3271 | NDISC_REDIRECT, &ndopts); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3272 | |
Martin KaFai Lau | 83a09ab | 2015-05-22 20:56:05 -0700 | [diff] [blame] | 3273 | nrt = ip6_rt_cache_alloc(rt, &msg->dest, NULL); |
David S. Miller | 3830847 | 2011-12-03 18:02:47 -0500 | [diff] [blame] | 3274 | if (!nrt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3275 | goto out; |
| 3276 | |
| 3277 | nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE; |
| 3278 | if (on_link) |
| 3279 | nrt->rt6i_flags &= ~RTF_GATEWAY; |
| 3280 | |
Xin Long | b91d532 | 2017-08-03 14:13:46 +0800 | [diff] [blame] | 3281 | nrt->rt6i_protocol = RTPROT_REDIRECT; |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 3282 | nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3283 | |
Wei Wang | 2b760fc | 2017-10-06 12:06:03 -0700 | [diff] [blame] | 3284 | /* No need to remove rt from the exception table if rt is |
| 3285 | * a cached route because rt6_insert_exception() will |
| 3286 | * takes care of it |
| 3287 | */ |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 3288 | if (rt6_insert_exception(nrt, rt->from)) { |
Wei Wang | 2b760fc | 2017-10-06 12:06:03 -0700 | [diff] [blame] | 3289 | dst_release_immediate(&nrt->dst); |
| 3290 | goto out; |
| 3291 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3292 | |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 3293 | netevent.old = &rt->dst; |
| 3294 | netevent.new = &nrt->dst; |
YOSHIFUJI Hideaki / 吉藤英明 | 71bcdba | 2013-01-05 16:34:51 +0000 | [diff] [blame] | 3295 | netevent.daddr = &msg->dest; |
YOSHIFUJI Hideaki / 吉藤英明 | 6059283 | 2013-01-14 09:28:27 +0000 | [diff] [blame] | 3296 | netevent.neigh = neigh; |
Tom Tucker | 8d71740 | 2006-07-30 20:43:36 -0700 | [diff] [blame] | 3297 | call_netevent_notifiers(NETEVENT_REDIRECT, &netevent); |
| 3298 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3299 | out: |
David S. Miller | e8599ff | 2012-07-11 23:43:53 -0700 | [diff] [blame] | 3300 | neigh_release(neigh); |
David S. Miller | 6e157b6 | 2012-07-12 00:05:02 -0700 | [diff] [blame] | 3301 | } |
| 3302 | |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 3303 | #ifdef CONFIG_IPV6_ROUTE_INFO |
Daniel Lezcano | efa2cea | 2008-03-04 13:46:48 -0800 | [diff] [blame] | 3304 | static struct rt6_info *rt6_get_route_info(struct net *net, |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 3305 | const struct in6_addr *prefix, int prefixlen, |
David Ahern | 830218c | 2016-10-24 10:52:35 -0700 | [diff] [blame] | 3306 | const struct in6_addr *gwaddr, |
| 3307 | struct net_device *dev) |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 3308 | { |
David Ahern | 830218c | 2016-10-24 10:52:35 -0700 | [diff] [blame] | 3309 | u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO; |
| 3310 | int ifindex = dev->ifindex; |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 3311 | struct fib6_node *fn; |
| 3312 | struct rt6_info *rt = NULL; |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 3313 | struct fib6_table *table; |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 3314 | |
David Ahern | 830218c | 2016-10-24 10:52:35 -0700 | [diff] [blame] | 3315 | table = fib6_get_table(net, tb_id); |
David S. Miller | 3830847 | 2011-12-03 18:02:47 -0500 | [diff] [blame] | 3316 | if (!table) |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 3317 | return NULL; |
| 3318 | |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 3319 | rcu_read_lock(); |
Wei Wang | 38fbeee | 2017-10-06 12:06:02 -0700 | [diff] [blame] | 3320 | fn = fib6_locate(&table->tb6_root, prefix, prefixlen, NULL, 0, true); |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 3321 | if (!fn) |
| 3322 | goto out; |
| 3323 | |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 3324 | for_each_fib6_node_rt_rcu(fn) { |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 3325 | if (rt->fib6_nh.nh_dev->ifindex != ifindex) |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 3326 | continue; |
| 3327 | if ((rt->rt6i_flags & (RTF_ROUTEINFO|RTF_GATEWAY)) != (RTF_ROUTEINFO|RTF_GATEWAY)) |
| 3328 | continue; |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 3329 | if (!ipv6_addr_equal(&rt->fib6_nh.nh_gw, gwaddr)) |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 3330 | continue; |
Wei Wang | d3843fe | 2017-10-06 12:06:06 -0700 | [diff] [blame] | 3331 | ip6_hold_safe(NULL, &rt, false); |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 3332 | break; |
| 3333 | } |
| 3334 | out: |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 3335 | rcu_read_unlock(); |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 3336 | return rt; |
| 3337 | } |
| 3338 | |
Daniel Lezcano | efa2cea | 2008-03-04 13:46:48 -0800 | [diff] [blame] | 3339 | static struct rt6_info *rt6_add_route_info(struct net *net, |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 3340 | const struct in6_addr *prefix, int prefixlen, |
David Ahern | 830218c | 2016-10-24 10:52:35 -0700 | [diff] [blame] | 3341 | const struct in6_addr *gwaddr, |
| 3342 | struct net_device *dev, |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 3343 | unsigned int pref) |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 3344 | { |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 3345 | struct fib6_config cfg = { |
Rami Rosen | 238fc7e | 2008-02-09 23:43:11 -0800 | [diff] [blame] | 3346 | .fc_metric = IP6_RT_PRIO_USER, |
David Ahern | 830218c | 2016-10-24 10:52:35 -0700 | [diff] [blame] | 3347 | .fc_ifindex = dev->ifindex, |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 3348 | .fc_dst_len = prefixlen, |
| 3349 | .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO | |
| 3350 | RTF_UP | RTF_PREF(pref), |
Xin Long | b91d532 | 2017-08-03 14:13:46 +0800 | [diff] [blame] | 3351 | .fc_protocol = RTPROT_RA, |
David Ahern | e8478e8 | 2018-04-17 17:33:13 -0700 | [diff] [blame] | 3352 | .fc_type = RTN_UNICAST, |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 3353 | .fc_nlinfo.portid = 0, |
Daniel Lezcano | efa2cea | 2008-03-04 13:46:48 -0800 | [diff] [blame] | 3354 | .fc_nlinfo.nlh = NULL, |
| 3355 | .fc_nlinfo.nl_net = net, |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 3356 | }; |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 3357 | |
David Ahern | 830218c | 2016-10-24 10:52:35 -0700 | [diff] [blame] | 3358 | cfg.fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO, |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 3359 | cfg.fc_dst = *prefix; |
| 3360 | cfg.fc_gateway = *gwaddr; |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 3361 | |
YOSHIFUJI Hideaki | e317da9 | 2006-03-20 17:06:42 -0800 | [diff] [blame] | 3362 | /* We should treat it as a default route if prefix length is 0. */ |
| 3363 | if (!prefixlen) |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 3364 | cfg.fc_flags |= RTF_DEFAULT; |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 3365 | |
David Ahern | 333c430 | 2017-05-21 10:12:04 -0600 | [diff] [blame] | 3366 | ip6_route_add(&cfg, NULL); |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 3367 | |
David Ahern | 830218c | 2016-10-24 10:52:35 -0700 | [diff] [blame] | 3368 | return rt6_get_route_info(net, prefix, prefixlen, gwaddr, dev); |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 3369 | } |
| 3370 | #endif |
| 3371 | |
David Ahern | afb1d4b5 | 2018-04-17 17:33:11 -0700 | [diff] [blame] | 3372 | struct rt6_info *rt6_get_dflt_router(struct net *net, |
| 3373 | const struct in6_addr *addr, |
| 3374 | struct net_device *dev) |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 3375 | { |
David Ahern | 830218c | 2016-10-24 10:52:35 -0700 | [diff] [blame] | 3376 | u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3377 | struct rt6_info *rt; |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 3378 | struct fib6_table *table; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3379 | |
David Ahern | afb1d4b5 | 2018-04-17 17:33:11 -0700 | [diff] [blame] | 3380 | table = fib6_get_table(net, tb_id); |
David S. Miller | 3830847 | 2011-12-03 18:02:47 -0500 | [diff] [blame] | 3381 | if (!table) |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 3382 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3383 | |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 3384 | rcu_read_lock(); |
| 3385 | for_each_fib6_node_rt_rcu(&table->tb6_root) { |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 3386 | if (dev == rt->fib6_nh.nh_dev && |
YOSHIFUJI Hideaki | 045927f | 2006-03-20 17:00:48 -0800 | [diff] [blame] | 3387 | ((rt->rt6i_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) && |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 3388 | ipv6_addr_equal(&rt->fib6_nh.nh_gw, addr)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3389 | break; |
| 3390 | } |
| 3391 | if (rt) |
Wei Wang | d3843fe | 2017-10-06 12:06:06 -0700 | [diff] [blame] | 3392 | ip6_hold_safe(NULL, &rt, false); |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 3393 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3394 | return rt; |
| 3395 | } |
| 3396 | |
David Ahern | afb1d4b5 | 2018-04-17 17:33:11 -0700 | [diff] [blame] | 3397 | struct rt6_info *rt6_add_dflt_router(struct net *net, |
| 3398 | const struct in6_addr *gwaddr, |
YOSHIFUJI Hideaki | ebacaaa | 2006-03-20 17:04:53 -0800 | [diff] [blame] | 3399 | struct net_device *dev, |
| 3400 | unsigned int pref) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3401 | { |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 3402 | struct fib6_config cfg = { |
David Ahern | ca25449 | 2015-10-12 11:47:10 -0700 | [diff] [blame] | 3403 | .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT, |
Rami Rosen | 238fc7e | 2008-02-09 23:43:11 -0800 | [diff] [blame] | 3404 | .fc_metric = IP6_RT_PRIO_USER, |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 3405 | .fc_ifindex = dev->ifindex, |
| 3406 | .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT | |
| 3407 | RTF_UP | RTF_EXPIRES | RTF_PREF(pref), |
Xin Long | b91d532 | 2017-08-03 14:13:46 +0800 | [diff] [blame] | 3408 | .fc_protocol = RTPROT_RA, |
David Ahern | e8478e8 | 2018-04-17 17:33:13 -0700 | [diff] [blame] | 3409 | .fc_type = RTN_UNICAST, |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 3410 | .fc_nlinfo.portid = 0, |
Daniel Lezcano | 5578689 | 2008-03-04 13:47:47 -0800 | [diff] [blame] | 3411 | .fc_nlinfo.nlh = NULL, |
David Ahern | afb1d4b5 | 2018-04-17 17:33:11 -0700 | [diff] [blame] | 3412 | .fc_nlinfo.nl_net = net, |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 3413 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3414 | |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 3415 | cfg.fc_gateway = *gwaddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3416 | |
David Ahern | 333c430 | 2017-05-21 10:12:04 -0600 | [diff] [blame] | 3417 | if (!ip6_route_add(&cfg, NULL)) { |
David Ahern | 830218c | 2016-10-24 10:52:35 -0700 | [diff] [blame] | 3418 | struct fib6_table *table; |
| 3419 | |
| 3420 | table = fib6_get_table(dev_net(dev), cfg.fc_table); |
| 3421 | if (table) |
| 3422 | table->flags |= RT6_TABLE_HAS_DFLT_ROUTER; |
| 3423 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3424 | |
David Ahern | afb1d4b5 | 2018-04-17 17:33:11 -0700 | [diff] [blame] | 3425 | return rt6_get_dflt_router(net, gwaddr, dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3426 | } |
| 3427 | |
David Ahern | afb1d4b5 | 2018-04-17 17:33:11 -0700 | [diff] [blame] | 3428 | static void __rt6_purge_dflt_routers(struct net *net, |
| 3429 | struct fib6_table *table) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3430 | { |
| 3431 | struct rt6_info *rt; |
| 3432 | |
| 3433 | restart: |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 3434 | rcu_read_lock(); |
| 3435 | for_each_fib6_node_rt_rcu(&table->tb6_root) { |
Lorenzo Colitti | 3e8b0ac | 2013-03-03 20:46:46 +0000 | [diff] [blame] | 3436 | if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF) && |
| 3437 | (!rt->rt6i_idev || rt->rt6i_idev->cnf.accept_ra != 2)) { |
Wei Wang | d3843fe | 2017-10-06 12:06:06 -0700 | [diff] [blame] | 3438 | if (dst_hold_safe(&rt->dst)) { |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 3439 | rcu_read_unlock(); |
David Ahern | afb1d4b5 | 2018-04-17 17:33:11 -0700 | [diff] [blame] | 3440 | ip6_del_rt(net, rt); |
Wei Wang | d3843fe | 2017-10-06 12:06:06 -0700 | [diff] [blame] | 3441 | } else { |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 3442 | rcu_read_unlock(); |
Wei Wang | d3843fe | 2017-10-06 12:06:06 -0700 | [diff] [blame] | 3443 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3444 | goto restart; |
| 3445 | } |
| 3446 | } |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 3447 | rcu_read_unlock(); |
David Ahern | 830218c | 2016-10-24 10:52:35 -0700 | [diff] [blame] | 3448 | |
| 3449 | table->flags &= ~RT6_TABLE_HAS_DFLT_ROUTER; |
| 3450 | } |
| 3451 | |
| 3452 | void rt6_purge_dflt_routers(struct net *net) |
| 3453 | { |
| 3454 | struct fib6_table *table; |
| 3455 | struct hlist_head *head; |
| 3456 | unsigned int h; |
| 3457 | |
| 3458 | rcu_read_lock(); |
| 3459 | |
| 3460 | for (h = 0; h < FIB6_TABLE_HASHSZ; h++) { |
| 3461 | head = &net->ipv6.fib_table_hash[h]; |
| 3462 | hlist_for_each_entry_rcu(table, head, tb6_hlist) { |
| 3463 | if (table->flags & RT6_TABLE_HAS_DFLT_ROUTER) |
David Ahern | afb1d4b5 | 2018-04-17 17:33:11 -0700 | [diff] [blame] | 3464 | __rt6_purge_dflt_routers(net, table); |
David Ahern | 830218c | 2016-10-24 10:52:35 -0700 | [diff] [blame] | 3465 | } |
| 3466 | } |
| 3467 | |
| 3468 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3469 | } |
| 3470 | |
Daniel Lezcano | 5578689 | 2008-03-04 13:47:47 -0800 | [diff] [blame] | 3471 | static void rtmsg_to_fib6_config(struct net *net, |
| 3472 | struct in6_rtmsg *rtmsg, |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 3473 | struct fib6_config *cfg) |
| 3474 | { |
| 3475 | memset(cfg, 0, sizeof(*cfg)); |
| 3476 | |
David Ahern | ca25449 | 2015-10-12 11:47:10 -0700 | [diff] [blame] | 3477 | cfg->fc_table = l3mdev_fib_table_by_index(net, rtmsg->rtmsg_ifindex) ? |
| 3478 | : RT6_TABLE_MAIN; |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 3479 | cfg->fc_ifindex = rtmsg->rtmsg_ifindex; |
| 3480 | cfg->fc_metric = rtmsg->rtmsg_metric; |
| 3481 | cfg->fc_expires = rtmsg->rtmsg_info; |
| 3482 | cfg->fc_dst_len = rtmsg->rtmsg_dst_len; |
| 3483 | cfg->fc_src_len = rtmsg->rtmsg_src_len; |
| 3484 | cfg->fc_flags = rtmsg->rtmsg_flags; |
David Ahern | e8478e8 | 2018-04-17 17:33:13 -0700 | [diff] [blame] | 3485 | cfg->fc_type = rtmsg->rtmsg_type; |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 3486 | |
Daniel Lezcano | 5578689 | 2008-03-04 13:47:47 -0800 | [diff] [blame] | 3487 | cfg->fc_nlinfo.nl_net = net; |
Benjamin Thery | f1243c2 | 2008-02-26 18:10:03 -0800 | [diff] [blame] | 3488 | |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 3489 | cfg->fc_dst = rtmsg->rtmsg_dst; |
| 3490 | cfg->fc_src = rtmsg->rtmsg_src; |
| 3491 | cfg->fc_gateway = rtmsg->rtmsg_gateway; |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 3492 | } |
| 3493 | |
Daniel Lezcano | 5578689 | 2008-03-04 13:47:47 -0800 | [diff] [blame] | 3494 | 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] | 3495 | { |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 3496 | struct fib6_config cfg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3497 | struct in6_rtmsg rtmsg; |
| 3498 | int err; |
| 3499 | |
Ian Morris | 67ba415 | 2014-08-24 21:53:10 +0100 | [diff] [blame] | 3500 | switch (cmd) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3501 | case SIOCADDRT: /* Add a route */ |
| 3502 | case SIOCDELRT: /* Delete a route */ |
Eric W. Biederman | af31f41 | 2012-11-16 03:03:06 +0000 | [diff] [blame] | 3503 | if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3504 | return -EPERM; |
| 3505 | err = copy_from_user(&rtmsg, arg, |
| 3506 | sizeof(struct in6_rtmsg)); |
| 3507 | if (err) |
| 3508 | return -EFAULT; |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 3509 | |
Daniel Lezcano | 5578689 | 2008-03-04 13:47:47 -0800 | [diff] [blame] | 3510 | rtmsg_to_fib6_config(net, &rtmsg, &cfg); |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 3511 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3512 | rtnl_lock(); |
| 3513 | switch (cmd) { |
| 3514 | case SIOCADDRT: |
David Ahern | 333c430 | 2017-05-21 10:12:04 -0600 | [diff] [blame] | 3515 | err = ip6_route_add(&cfg, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3516 | break; |
| 3517 | case SIOCDELRT: |
David Ahern | 333c430 | 2017-05-21 10:12:04 -0600 | [diff] [blame] | 3518 | err = ip6_route_del(&cfg, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3519 | break; |
| 3520 | default: |
| 3521 | err = -EINVAL; |
| 3522 | } |
| 3523 | rtnl_unlock(); |
| 3524 | |
| 3525 | return err; |
Stephen Hemminger | 3ff50b7 | 2007-04-20 17:09:22 -0700 | [diff] [blame] | 3526 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3527 | |
| 3528 | return -EINVAL; |
| 3529 | } |
| 3530 | |
| 3531 | /* |
| 3532 | * Drop the packet on the floor |
| 3533 | */ |
| 3534 | |
Brian Haley | d5fdd6b | 2009-06-23 04:31:07 -0700 | [diff] [blame] | 3535 | 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] | 3536 | { |
YOSHIFUJI Hideaki | 612f09e | 2007-04-13 16:18:02 -0700 | [diff] [blame] | 3537 | int type; |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 3538 | struct dst_entry *dst = skb_dst(skb); |
YOSHIFUJI Hideaki | 612f09e | 2007-04-13 16:18:02 -0700 | [diff] [blame] | 3539 | switch (ipstats_mib_noroutes) { |
| 3540 | case IPSTATS_MIB_INNOROUTES: |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 3541 | type = ipv6_addr_type(&ipv6_hdr(skb)->daddr); |
Ulrich Weber | 45bb006 | 2010-02-25 23:28:58 +0000 | [diff] [blame] | 3542 | if (type == IPV6_ADDR_ANY) { |
Stephen Suryaputra | bdb7cc6 | 2018-04-16 13:42:16 -0400 | [diff] [blame] | 3543 | IP6_INC_STATS(dev_net(dst->dev), |
| 3544 | __in6_dev_get_safely(skb->dev), |
Denis V. Lunev | 3bd653c | 2008-10-08 10:54:51 -0700 | [diff] [blame] | 3545 | IPSTATS_MIB_INADDRERRORS); |
YOSHIFUJI Hideaki | 612f09e | 2007-04-13 16:18:02 -0700 | [diff] [blame] | 3546 | break; |
| 3547 | } |
| 3548 | /* FALLTHROUGH */ |
| 3549 | case IPSTATS_MIB_OUTNOROUTES: |
Denis V. Lunev | 3bd653c | 2008-10-08 10:54:51 -0700 | [diff] [blame] | 3550 | IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst), |
| 3551 | ipstats_mib_noroutes); |
YOSHIFUJI Hideaki | 612f09e | 2007-04-13 16:18:02 -0700 | [diff] [blame] | 3552 | break; |
| 3553 | } |
Alexey Dobriyan | 3ffe533 | 2010-02-18 08:25:24 +0000 | [diff] [blame] | 3554 | icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3555 | kfree_skb(skb); |
| 3556 | return 0; |
| 3557 | } |
| 3558 | |
Thomas Graf | 9ce8ade | 2006-10-18 20:46:54 -0700 | [diff] [blame] | 3559 | static int ip6_pkt_discard(struct sk_buff *skb) |
| 3560 | { |
YOSHIFUJI Hideaki | 612f09e | 2007-04-13 16:18:02 -0700 | [diff] [blame] | 3561 | return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES); |
Thomas Graf | 9ce8ade | 2006-10-18 20:46:54 -0700 | [diff] [blame] | 3562 | } |
| 3563 | |
Eric W. Biederman | ede2059 | 2015-10-07 16:48:47 -0500 | [diff] [blame] | 3564 | 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] | 3565 | { |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 3566 | skb->dev = skb_dst(skb)->dev; |
YOSHIFUJI Hideaki | 612f09e | 2007-04-13 16:18:02 -0700 | [diff] [blame] | 3567 | return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3568 | } |
| 3569 | |
Thomas Graf | 9ce8ade | 2006-10-18 20:46:54 -0700 | [diff] [blame] | 3570 | static int ip6_pkt_prohibit(struct sk_buff *skb) |
| 3571 | { |
YOSHIFUJI Hideaki | 612f09e | 2007-04-13 16:18:02 -0700 | [diff] [blame] | 3572 | return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES); |
Thomas Graf | 9ce8ade | 2006-10-18 20:46:54 -0700 | [diff] [blame] | 3573 | } |
| 3574 | |
Eric W. Biederman | ede2059 | 2015-10-07 16:48:47 -0500 | [diff] [blame] | 3575 | 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] | 3576 | { |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 3577 | skb->dev = skb_dst(skb)->dev; |
YOSHIFUJI Hideaki | 612f09e | 2007-04-13 16:18:02 -0700 | [diff] [blame] | 3578 | return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES); |
Thomas Graf | 9ce8ade | 2006-10-18 20:46:54 -0700 | [diff] [blame] | 3579 | } |
| 3580 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3581 | /* |
| 3582 | * Allocate a dst for local (unicast / anycast) address. |
| 3583 | */ |
| 3584 | |
David Ahern | afb1d4b5 | 2018-04-17 17:33:11 -0700 | [diff] [blame] | 3585 | struct rt6_info *addrconf_dst_alloc(struct net *net, |
| 3586 | struct inet6_dev *idev, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3587 | const struct in6_addr *addr, |
David S. Miller | 8f03151 | 2011-12-06 16:48:14 -0500 | [diff] [blame] | 3588 | bool anycast) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3589 | { |
David Ahern | ca25449 | 2015-10-12 11:47:10 -0700 | [diff] [blame] | 3590 | u32 tb_id; |
David Ahern | 4832c30 | 2017-08-17 12:17:20 -0700 | [diff] [blame] | 3591 | struct net_device *dev = idev->dev; |
David Ahern | 5f02ce24 | 2016-09-10 12:09:54 -0700 | [diff] [blame] | 3592 | struct rt6_info *rt; |
| 3593 | |
David Ahern | 5f02ce24 | 2016-09-10 12:09:54 -0700 | [diff] [blame] | 3594 | rt = ip6_dst_alloc(net, dev, DST_NOCOUNT); |
Hannes Frederic Sowa | a3300ef | 2013-12-07 03:33:45 +0100 | [diff] [blame] | 3595 | if (!rt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3596 | return ERR_PTR(-ENOMEM); |
| 3597 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3598 | in6_dev_hold(idev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3599 | rt->rt6i_idev = idev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3600 | |
David Ahern | 6edb3c9 | 2018-04-17 17:33:15 -0700 | [diff] [blame] | 3601 | rt->dst.flags |= DST_HOST; |
David Ahern | 94b5e0f | 2017-02-02 08:52:21 -0800 | [diff] [blame] | 3602 | rt->rt6i_protocol = RTPROT_KERNEL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3603 | rt->rt6i_flags = RTF_UP | RTF_NONEXTHOP; |
David Ahern | e8478e8 | 2018-04-17 17:33:13 -0700 | [diff] [blame] | 3604 | if (anycast) { |
| 3605 | rt->fib6_type = RTN_ANYCAST; |
YOSHIFUJI Hideaki | 58c4fb8 | 2005-12-21 22:56:42 +0900 | [diff] [blame] | 3606 | rt->rt6i_flags |= RTF_ANYCAST; |
David Ahern | e8478e8 | 2018-04-17 17:33:13 -0700 | [diff] [blame] | 3607 | } else { |
| 3608 | rt->fib6_type = RTN_LOCAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3609 | rt->rt6i_flags |= RTF_LOCAL; |
David Ahern | e8478e8 | 2018-04-17 17:33:13 -0700 | [diff] [blame] | 3610 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3611 | |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 3612 | rt->fib6_nh.nh_gw = *addr; |
| 3613 | rt->fib6_nh.nh_dev = dev; |
Julian Anastasov | 550bab4 | 2013-10-20 15:43:04 +0300 | [diff] [blame] | 3614 | rt->rt6i_gateway = *addr; |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 3615 | rt->rt6i_dst.addr = *addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3616 | rt->rt6i_dst.plen = 128; |
David Ahern | ca25449 | 2015-10-12 11:47:10 -0700 | [diff] [blame] | 3617 | tb_id = l3mdev_fib_table(idev->dev) ? : RT6_TABLE_LOCAL; |
| 3618 | rt->rt6i_table = fib6_get_table(net, tb_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3619 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3620 | return rt; |
| 3621 | } |
| 3622 | |
Daniel Walter | c3968a8 | 2011-04-13 21:10:57 +0000 | [diff] [blame] | 3623 | /* remove deleted ip from prefsrc entries */ |
| 3624 | struct arg_dev_net_ip { |
| 3625 | struct net_device *dev; |
| 3626 | struct net *net; |
| 3627 | struct in6_addr *addr; |
| 3628 | }; |
| 3629 | |
| 3630 | static int fib6_remove_prefsrc(struct rt6_info *rt, void *arg) |
| 3631 | { |
| 3632 | struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev; |
| 3633 | struct net *net = ((struct arg_dev_net_ip *)arg)->net; |
| 3634 | struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr; |
| 3635 | |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 3636 | if (((void *)rt->fib6_nh.nh_dev == dev || !dev) && |
Daniel Walter | c3968a8 | 2011-04-13 21:10:57 +0000 | [diff] [blame] | 3637 | rt != net->ipv6.ip6_null_entry && |
| 3638 | ipv6_addr_equal(addr, &rt->rt6i_prefsrc.addr)) { |
Wei Wang | 60006a4 | 2017-10-06 12:05:58 -0700 | [diff] [blame] | 3639 | spin_lock_bh(&rt6_exception_lock); |
Daniel Walter | c3968a8 | 2011-04-13 21:10:57 +0000 | [diff] [blame] | 3640 | /* remove prefsrc entry */ |
| 3641 | rt->rt6i_prefsrc.plen = 0; |
Wei Wang | 60006a4 | 2017-10-06 12:05:58 -0700 | [diff] [blame] | 3642 | /* need to update cache as well */ |
| 3643 | rt6_exceptions_remove_prefsrc(rt); |
| 3644 | spin_unlock_bh(&rt6_exception_lock); |
Daniel Walter | c3968a8 | 2011-04-13 21:10:57 +0000 | [diff] [blame] | 3645 | } |
| 3646 | return 0; |
| 3647 | } |
| 3648 | |
| 3649 | void rt6_remove_prefsrc(struct inet6_ifaddr *ifp) |
| 3650 | { |
| 3651 | struct net *net = dev_net(ifp->idev->dev); |
| 3652 | struct arg_dev_net_ip adni = { |
| 3653 | .dev = ifp->idev->dev, |
| 3654 | .net = net, |
| 3655 | .addr = &ifp->addr, |
| 3656 | }; |
Li RongQing | 0c3584d | 2013-12-27 16:32:38 +0800 | [diff] [blame] | 3657 | fib6_clean_all(net, fib6_remove_prefsrc, &adni); |
Daniel Walter | c3968a8 | 2011-04-13 21:10:57 +0000 | [diff] [blame] | 3658 | } |
| 3659 | |
Duan Jiong | be7a010 | 2014-05-15 15:56:14 +0800 | [diff] [blame] | 3660 | #define RTF_RA_ROUTER (RTF_ADDRCONF | RTF_DEFAULT | RTF_GATEWAY) |
Duan Jiong | be7a010 | 2014-05-15 15:56:14 +0800 | [diff] [blame] | 3661 | |
| 3662 | /* Remove routers and update dst entries when gateway turn into host. */ |
| 3663 | static int fib6_clean_tohost(struct rt6_info *rt, void *arg) |
| 3664 | { |
| 3665 | struct in6_addr *gateway = (struct in6_addr *)arg; |
| 3666 | |
Wei Wang | 2b760fc | 2017-10-06 12:06:03 -0700 | [diff] [blame] | 3667 | if (((rt->rt6i_flags & RTF_RA_ROUTER) == RTF_RA_ROUTER) && |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 3668 | ipv6_addr_equal(gateway, &rt->fib6_nh.nh_gw)) { |
Duan Jiong | be7a010 | 2014-05-15 15:56:14 +0800 | [diff] [blame] | 3669 | return -1; |
| 3670 | } |
Wei Wang | b16cb45 | 2017-10-06 12:06:00 -0700 | [diff] [blame] | 3671 | |
| 3672 | /* Further clean up cached routes in exception table. |
| 3673 | * This is needed because cached route may have a different |
| 3674 | * gateway than its 'parent' in the case of an ip redirect. |
| 3675 | */ |
| 3676 | rt6_exceptions_clean_tohost(rt, gateway); |
| 3677 | |
Duan Jiong | be7a010 | 2014-05-15 15:56:14 +0800 | [diff] [blame] | 3678 | return 0; |
| 3679 | } |
| 3680 | |
| 3681 | void rt6_clean_tohost(struct net *net, struct in6_addr *gateway) |
| 3682 | { |
| 3683 | fib6_clean_all(net, fib6_clean_tohost, gateway); |
| 3684 | } |
| 3685 | |
Ido Schimmel | 2127d95 | 2018-01-07 12:45:03 +0200 | [diff] [blame] | 3686 | struct arg_netdev_event { |
| 3687 | const struct net_device *dev; |
Ido Schimmel | 4c981e2 | 2018-01-07 12:45:04 +0200 | [diff] [blame] | 3688 | union { |
| 3689 | unsigned int nh_flags; |
| 3690 | unsigned long event; |
| 3691 | }; |
Ido Schimmel | 2127d95 | 2018-01-07 12:45:03 +0200 | [diff] [blame] | 3692 | }; |
| 3693 | |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 3694 | static struct rt6_info *rt6_multipath_first_sibling(const struct rt6_info *rt) |
| 3695 | { |
| 3696 | struct rt6_info *iter; |
| 3697 | struct fib6_node *fn; |
| 3698 | |
| 3699 | fn = rcu_dereference_protected(rt->rt6i_node, |
| 3700 | lockdep_is_held(&rt->rt6i_table->tb6_lock)); |
| 3701 | iter = rcu_dereference_protected(fn->leaf, |
| 3702 | lockdep_is_held(&rt->rt6i_table->tb6_lock)); |
| 3703 | while (iter) { |
| 3704 | if (iter->rt6i_metric == rt->rt6i_metric && |
| 3705 | rt6_qualify_for_ecmp(iter)) |
| 3706 | return iter; |
| 3707 | iter = rcu_dereference_protected(iter->rt6_next, |
| 3708 | lockdep_is_held(&rt->rt6i_table->tb6_lock)); |
| 3709 | } |
| 3710 | |
| 3711 | return NULL; |
| 3712 | } |
| 3713 | |
| 3714 | static bool rt6_is_dead(const struct rt6_info *rt) |
| 3715 | { |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 3716 | if (rt->fib6_nh.nh_flags & RTNH_F_DEAD || |
| 3717 | (rt->fib6_nh.nh_flags & RTNH_F_LINKDOWN && |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 3718 | rt->rt6i_idev->cnf.ignore_routes_with_linkdown)) |
| 3719 | return true; |
| 3720 | |
| 3721 | return false; |
| 3722 | } |
| 3723 | |
| 3724 | static int rt6_multipath_total_weight(const struct rt6_info *rt) |
| 3725 | { |
| 3726 | struct rt6_info *iter; |
| 3727 | int total = 0; |
| 3728 | |
| 3729 | if (!rt6_is_dead(rt)) |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 3730 | total += rt->fib6_nh.nh_weight; |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 3731 | |
| 3732 | list_for_each_entry(iter, &rt->rt6i_siblings, rt6i_siblings) { |
| 3733 | if (!rt6_is_dead(iter)) |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 3734 | total += iter->fib6_nh.nh_weight; |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 3735 | } |
| 3736 | |
| 3737 | return total; |
| 3738 | } |
| 3739 | |
| 3740 | static void rt6_upper_bound_set(struct rt6_info *rt, int *weight, int total) |
| 3741 | { |
| 3742 | int upper_bound = -1; |
| 3743 | |
| 3744 | if (!rt6_is_dead(rt)) { |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 3745 | *weight += rt->fib6_nh.nh_weight; |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 3746 | upper_bound = DIV_ROUND_CLOSEST_ULL((u64) (*weight) << 31, |
| 3747 | total) - 1; |
| 3748 | } |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 3749 | atomic_set(&rt->fib6_nh.nh_upper_bound, upper_bound); |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 3750 | } |
| 3751 | |
| 3752 | static void rt6_multipath_upper_bound_set(struct rt6_info *rt, int total) |
| 3753 | { |
| 3754 | struct rt6_info *iter; |
| 3755 | int weight = 0; |
| 3756 | |
| 3757 | rt6_upper_bound_set(rt, &weight, total); |
| 3758 | |
| 3759 | list_for_each_entry(iter, &rt->rt6i_siblings, rt6i_siblings) |
| 3760 | rt6_upper_bound_set(iter, &weight, total); |
| 3761 | } |
| 3762 | |
| 3763 | void rt6_multipath_rebalance(struct rt6_info *rt) |
| 3764 | { |
| 3765 | struct rt6_info *first; |
| 3766 | int total; |
| 3767 | |
| 3768 | /* In case the entire multipath route was marked for flushing, |
| 3769 | * then there is no need to rebalance upon the removal of every |
| 3770 | * sibling route. |
| 3771 | */ |
| 3772 | if (!rt->rt6i_nsiblings || rt->should_flush) |
| 3773 | return; |
| 3774 | |
| 3775 | /* During lookup routes are evaluated in order, so we need to |
| 3776 | * make sure upper bounds are assigned from the first sibling |
| 3777 | * onwards. |
| 3778 | */ |
| 3779 | first = rt6_multipath_first_sibling(rt); |
| 3780 | if (WARN_ON_ONCE(!first)) |
| 3781 | return; |
| 3782 | |
| 3783 | total = rt6_multipath_total_weight(first); |
| 3784 | rt6_multipath_upper_bound_set(first, total); |
| 3785 | } |
| 3786 | |
Ido Schimmel | 2127d95 | 2018-01-07 12:45:03 +0200 | [diff] [blame] | 3787 | static int fib6_ifup(struct rt6_info *rt, void *p_arg) |
| 3788 | { |
| 3789 | const struct arg_netdev_event *arg = p_arg; |
David Ahern | 7aef685 | 2018-04-17 17:33:10 -0700 | [diff] [blame] | 3790 | struct net *net = dev_net(arg->dev); |
Ido Schimmel | 2127d95 | 2018-01-07 12:45:03 +0200 | [diff] [blame] | 3791 | |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 3792 | if (rt != net->ipv6.ip6_null_entry && rt->fib6_nh.nh_dev == arg->dev) { |
| 3793 | rt->fib6_nh.nh_flags &= ~arg->nh_flags; |
David Ahern | 7aef685 | 2018-04-17 17:33:10 -0700 | [diff] [blame] | 3794 | fib6_update_sernum_upto_root(net, rt); |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 3795 | rt6_multipath_rebalance(rt); |
Ido Schimmel | 1de178e | 2018-01-07 12:45:15 +0200 | [diff] [blame] | 3796 | } |
Ido Schimmel | 2127d95 | 2018-01-07 12:45:03 +0200 | [diff] [blame] | 3797 | |
| 3798 | return 0; |
| 3799 | } |
| 3800 | |
| 3801 | void rt6_sync_up(struct net_device *dev, unsigned int nh_flags) |
| 3802 | { |
| 3803 | struct arg_netdev_event arg = { |
| 3804 | .dev = dev, |
Ido Schimmel | 6802f3a | 2018-01-12 22:07:36 +0200 | [diff] [blame] | 3805 | { |
| 3806 | .nh_flags = nh_flags, |
| 3807 | }, |
Ido Schimmel | 2127d95 | 2018-01-07 12:45:03 +0200 | [diff] [blame] | 3808 | }; |
| 3809 | |
| 3810 | if (nh_flags & RTNH_F_DEAD && netif_carrier_ok(dev)) |
| 3811 | arg.nh_flags |= RTNH_F_LINKDOWN; |
| 3812 | |
| 3813 | fib6_clean_all(dev_net(dev), fib6_ifup, &arg); |
| 3814 | } |
| 3815 | |
Ido Schimmel | 1de178e | 2018-01-07 12:45:15 +0200 | [diff] [blame] | 3816 | static bool rt6_multipath_uses_dev(const struct rt6_info *rt, |
| 3817 | const struct net_device *dev) |
| 3818 | { |
| 3819 | struct rt6_info *iter; |
| 3820 | |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 3821 | if (rt->fib6_nh.nh_dev == dev) |
Ido Schimmel | 1de178e | 2018-01-07 12:45:15 +0200 | [diff] [blame] | 3822 | return true; |
| 3823 | list_for_each_entry(iter, &rt->rt6i_siblings, rt6i_siblings) |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 3824 | if (iter->fib6_nh.nh_dev == dev) |
Ido Schimmel | 1de178e | 2018-01-07 12:45:15 +0200 | [diff] [blame] | 3825 | return true; |
| 3826 | |
| 3827 | return false; |
| 3828 | } |
| 3829 | |
| 3830 | static void rt6_multipath_flush(struct rt6_info *rt) |
| 3831 | { |
| 3832 | struct rt6_info *iter; |
| 3833 | |
| 3834 | rt->should_flush = 1; |
| 3835 | list_for_each_entry(iter, &rt->rt6i_siblings, rt6i_siblings) |
| 3836 | iter->should_flush = 1; |
| 3837 | } |
| 3838 | |
| 3839 | static unsigned int rt6_multipath_dead_count(const struct rt6_info *rt, |
| 3840 | const struct net_device *down_dev) |
| 3841 | { |
| 3842 | struct rt6_info *iter; |
| 3843 | unsigned int dead = 0; |
| 3844 | |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 3845 | if (rt->fib6_nh.nh_dev == down_dev || |
| 3846 | rt->fib6_nh.nh_flags & RTNH_F_DEAD) |
Ido Schimmel | 1de178e | 2018-01-07 12:45:15 +0200 | [diff] [blame] | 3847 | dead++; |
| 3848 | list_for_each_entry(iter, &rt->rt6i_siblings, rt6i_siblings) |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 3849 | if (iter->fib6_nh.nh_dev == down_dev || |
| 3850 | iter->fib6_nh.nh_flags & RTNH_F_DEAD) |
Ido Schimmel | 1de178e | 2018-01-07 12:45:15 +0200 | [diff] [blame] | 3851 | dead++; |
| 3852 | |
| 3853 | return dead; |
| 3854 | } |
| 3855 | |
| 3856 | static void rt6_multipath_nh_flags_set(struct rt6_info *rt, |
| 3857 | const struct net_device *dev, |
| 3858 | unsigned int nh_flags) |
| 3859 | { |
| 3860 | struct rt6_info *iter; |
| 3861 | |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 3862 | if (rt->fib6_nh.nh_dev == dev) |
| 3863 | rt->fib6_nh.nh_flags |= nh_flags; |
Ido Schimmel | 1de178e | 2018-01-07 12:45:15 +0200 | [diff] [blame] | 3864 | list_for_each_entry(iter, &rt->rt6i_siblings, rt6i_siblings) |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 3865 | if (iter->fib6_nh.nh_dev == dev) |
| 3866 | iter->fib6_nh.nh_flags |= nh_flags; |
Ido Schimmel | 1de178e | 2018-01-07 12:45:15 +0200 | [diff] [blame] | 3867 | } |
| 3868 | |
David Ahern | a1a22c1 | 2017-01-18 07:40:36 -0800 | [diff] [blame] | 3869 | /* called with write lock held for table with rt */ |
Ido Schimmel | 4c981e2 | 2018-01-07 12:45:04 +0200 | [diff] [blame] | 3870 | static int fib6_ifdown(struct rt6_info *rt, void *p_arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3871 | { |
Ido Schimmel | 4c981e2 | 2018-01-07 12:45:04 +0200 | [diff] [blame] | 3872 | const struct arg_netdev_event *arg = p_arg; |
| 3873 | const struct net_device *dev = arg->dev; |
David Ahern | 7aef685 | 2018-04-17 17:33:10 -0700 | [diff] [blame] | 3874 | struct net *net = dev_net(dev); |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 3875 | |
Ido Schimmel | 1de178e | 2018-01-07 12:45:15 +0200 | [diff] [blame] | 3876 | if (rt == net->ipv6.ip6_null_entry) |
Ido Schimmel | 27c6fa7 | 2018-01-07 12:45:05 +0200 | [diff] [blame] | 3877 | return 0; |
| 3878 | |
| 3879 | switch (arg->event) { |
| 3880 | case NETDEV_UNREGISTER: |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 3881 | return rt->fib6_nh.nh_dev == dev ? -1 : 0; |
Ido Schimmel | 27c6fa7 | 2018-01-07 12:45:05 +0200 | [diff] [blame] | 3882 | case NETDEV_DOWN: |
Ido Schimmel | 1de178e | 2018-01-07 12:45:15 +0200 | [diff] [blame] | 3883 | if (rt->should_flush) |
Ido Schimmel | 27c6fa7 | 2018-01-07 12:45:05 +0200 | [diff] [blame] | 3884 | return -1; |
Ido Schimmel | 1de178e | 2018-01-07 12:45:15 +0200 | [diff] [blame] | 3885 | if (!rt->rt6i_nsiblings) |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 3886 | return rt->fib6_nh.nh_dev == dev ? -1 : 0; |
Ido Schimmel | 1de178e | 2018-01-07 12:45:15 +0200 | [diff] [blame] | 3887 | if (rt6_multipath_uses_dev(rt, dev)) { |
| 3888 | unsigned int count; |
| 3889 | |
| 3890 | count = rt6_multipath_dead_count(rt, dev); |
| 3891 | if (rt->rt6i_nsiblings + 1 == count) { |
| 3892 | rt6_multipath_flush(rt); |
| 3893 | return -1; |
| 3894 | } |
| 3895 | rt6_multipath_nh_flags_set(rt, dev, RTNH_F_DEAD | |
| 3896 | RTNH_F_LINKDOWN); |
David Ahern | 7aef685 | 2018-04-17 17:33:10 -0700 | [diff] [blame] | 3897 | fib6_update_sernum(net, rt); |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 3898 | rt6_multipath_rebalance(rt); |
Ido Schimmel | 1de178e | 2018-01-07 12:45:15 +0200 | [diff] [blame] | 3899 | } |
| 3900 | return -2; |
Ido Schimmel | 27c6fa7 | 2018-01-07 12:45:05 +0200 | [diff] [blame] | 3901 | case NETDEV_CHANGE: |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 3902 | if (rt->fib6_nh.nh_dev != dev || |
Ido Schimmel | 1de178e | 2018-01-07 12:45:15 +0200 | [diff] [blame] | 3903 | rt->rt6i_flags & (RTF_LOCAL | RTF_ANYCAST)) |
Ido Schimmel | 27c6fa7 | 2018-01-07 12:45:05 +0200 | [diff] [blame] | 3904 | break; |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 3905 | rt->fib6_nh.nh_flags |= RTNH_F_LINKDOWN; |
Ido Schimmel | d7dedee | 2018-01-09 16:40:25 +0200 | [diff] [blame] | 3906 | rt6_multipath_rebalance(rt); |
Ido Schimmel | 27c6fa7 | 2018-01-07 12:45:05 +0200 | [diff] [blame] | 3907 | break; |
Ido Schimmel | 2b24136 | 2018-01-07 12:45:02 +0200 | [diff] [blame] | 3908 | } |
David S. Miller | c159d30 | 2011-12-26 15:24:36 -0500 | [diff] [blame] | 3909 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3910 | return 0; |
| 3911 | } |
| 3912 | |
Ido Schimmel | 27c6fa7 | 2018-01-07 12:45:05 +0200 | [diff] [blame] | 3913 | void rt6_sync_down_dev(struct net_device *dev, unsigned long event) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3914 | { |
Ido Schimmel | 4c981e2 | 2018-01-07 12:45:04 +0200 | [diff] [blame] | 3915 | struct arg_netdev_event arg = { |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 3916 | .dev = dev, |
Ido Schimmel | 6802f3a | 2018-01-12 22:07:36 +0200 | [diff] [blame] | 3917 | { |
| 3918 | .event = event, |
| 3919 | }, |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 3920 | }; |
| 3921 | |
Ido Schimmel | 4c981e2 | 2018-01-07 12:45:04 +0200 | [diff] [blame] | 3922 | fib6_clean_all(dev_net(dev), fib6_ifdown, &arg); |
| 3923 | } |
| 3924 | |
| 3925 | void rt6_disable_ip(struct net_device *dev, unsigned long event) |
| 3926 | { |
| 3927 | rt6_sync_down_dev(dev, event); |
| 3928 | rt6_uncached_list_flush_dev(dev_net(dev), dev); |
| 3929 | neigh_ifdown(&nd_tbl, dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3930 | } |
| 3931 | |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 3932 | struct rt6_mtu_change_arg { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3933 | struct net_device *dev; |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 3934 | unsigned int mtu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3935 | }; |
| 3936 | |
| 3937 | static int rt6_mtu_change_route(struct rt6_info *rt, void *p_arg) |
| 3938 | { |
| 3939 | struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg; |
| 3940 | struct inet6_dev *idev; |
| 3941 | |
| 3942 | /* In IPv6 pmtu discovery is not optional, |
| 3943 | so that RTAX_MTU lock cannot disable it. |
| 3944 | We still use this lock to block changes |
| 3945 | caused by addrconf/ndisc. |
| 3946 | */ |
| 3947 | |
| 3948 | idev = __in6_dev_get(arg->dev); |
David S. Miller | 3830847 | 2011-12-03 18:02:47 -0500 | [diff] [blame] | 3949 | if (!idev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3950 | return 0; |
| 3951 | |
| 3952 | /* For administrative MTU increase, there is no way to discover |
| 3953 | IPv6 PMTU increase, so PMTU increase should be updated here. |
| 3954 | Since RFC 1981 doesn't include administrative MTU increase |
| 3955 | update PMTU increase is a MUST. (i.e. jumbo frame) |
| 3956 | */ |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 3957 | if (rt->fib6_nh.nh_dev == arg->dev && |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 3958 | !fib6_metric_locked(rt, RTAX_MTU)) { |
| 3959 | u32 mtu = rt->fib6_pmtu; |
| 3960 | |
| 3961 | if (mtu >= arg->mtu || |
| 3962 | (mtu < arg->mtu && mtu == idev->cnf.mtu6)) |
| 3963 | fib6_metric_set(rt, RTAX_MTU, arg->mtu); |
| 3964 | |
Wei Wang | f5bbe7e | 2017-10-06 12:05:59 -0700 | [diff] [blame] | 3965 | spin_lock_bh(&rt6_exception_lock); |
Stefano Brivio | e9fa149 | 2018-03-06 11:10:19 +0100 | [diff] [blame] | 3966 | rt6_exceptions_update_pmtu(idev, rt, arg->mtu); |
Wei Wang | f5bbe7e | 2017-10-06 12:05:59 -0700 | [diff] [blame] | 3967 | spin_unlock_bh(&rt6_exception_lock); |
Simon Arlott | 566cfd8 | 2007-07-26 00:09:55 -0700 | [diff] [blame] | 3968 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3969 | return 0; |
| 3970 | } |
| 3971 | |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 3972 | void rt6_mtu_change(struct net_device *dev, unsigned int mtu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3973 | { |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 3974 | struct rt6_mtu_change_arg arg = { |
| 3975 | .dev = dev, |
| 3976 | .mtu = mtu, |
| 3977 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3978 | |
Li RongQing | 0c3584d | 2013-12-27 16:32:38 +0800 | [diff] [blame] | 3979 | fib6_clean_all(dev_net(dev), rt6_mtu_change_route, &arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3980 | } |
| 3981 | |
Patrick McHardy | ef7c79e | 2007-06-05 12:38:30 -0700 | [diff] [blame] | 3982 | static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = { |
Thomas Graf | 5176f91 | 2006-08-26 20:13:18 -0700 | [diff] [blame] | 3983 | [RTA_GATEWAY] = { .len = sizeof(struct in6_addr) }, |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 3984 | [RTA_OIF] = { .type = NLA_U32 }, |
Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 3985 | [RTA_IIF] = { .type = NLA_U32 }, |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 3986 | [RTA_PRIORITY] = { .type = NLA_U32 }, |
| 3987 | [RTA_METRICS] = { .type = NLA_NESTED }, |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 3988 | [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) }, |
Lubomir Rintel | c78ba6d | 2015-03-11 15:39:21 +0100 | [diff] [blame] | 3989 | [RTA_PREF] = { .type = NLA_U8 }, |
Roopa Prabhu | 19e42e4 | 2015-07-21 10:43:48 +0200 | [diff] [blame] | 3990 | [RTA_ENCAP_TYPE] = { .type = NLA_U16 }, |
| 3991 | [RTA_ENCAP] = { .type = NLA_NESTED }, |
Xin Long | 32bc201 | 2015-12-16 17:50:11 +0800 | [diff] [blame] | 3992 | [RTA_EXPIRES] = { .type = NLA_U32 }, |
Lorenzo Colitti | 622ec2c | 2016-11-04 02:23:42 +0900 | [diff] [blame] | 3993 | [RTA_UID] = { .type = NLA_U32 }, |
Liping Zhang | 3b45a41 | 2017-02-27 20:59:39 +0800 | [diff] [blame] | 3994 | [RTA_MARK] = { .type = NLA_U32 }, |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 3995 | }; |
| 3996 | |
| 3997 | 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] | 3998 | struct fib6_config *cfg, |
| 3999 | struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4000 | { |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4001 | struct rtmsg *rtm; |
| 4002 | struct nlattr *tb[RTA_MAX+1]; |
Lubomir Rintel | c78ba6d | 2015-03-11 15:39:21 +0100 | [diff] [blame] | 4003 | unsigned int pref; |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4004 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4005 | |
Johannes Berg | fceb643 | 2017-04-12 14:34:07 +0200 | [diff] [blame] | 4006 | err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy, |
| 4007 | NULL); |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4008 | if (err < 0) |
| 4009 | goto errout; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4010 | |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4011 | err = -EINVAL; |
| 4012 | rtm = nlmsg_data(nlh); |
| 4013 | memset(cfg, 0, sizeof(*cfg)); |
| 4014 | |
| 4015 | cfg->fc_table = rtm->rtm_table; |
| 4016 | cfg->fc_dst_len = rtm->rtm_dst_len; |
| 4017 | cfg->fc_src_len = rtm->rtm_src_len; |
| 4018 | cfg->fc_flags = RTF_UP; |
| 4019 | cfg->fc_protocol = rtm->rtm_protocol; |
Nicolas Dichtel | ef2c7d7 | 2012-09-05 02:12:42 +0000 | [diff] [blame] | 4020 | cfg->fc_type = rtm->rtm_type; |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4021 | |
Nicolas Dichtel | ef2c7d7 | 2012-09-05 02:12:42 +0000 | [diff] [blame] | 4022 | if (rtm->rtm_type == RTN_UNREACHABLE || |
| 4023 | rtm->rtm_type == RTN_BLACKHOLE || |
Nicolas Dichtel | b4949ab | 2012-09-06 05:53:35 +0000 | [diff] [blame] | 4024 | rtm->rtm_type == RTN_PROHIBIT || |
| 4025 | rtm->rtm_type == RTN_THROW) |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4026 | cfg->fc_flags |= RTF_REJECT; |
| 4027 | |
Maciej Żenczykowski | ab79ad1 | 2010-09-27 00:07:02 +0000 | [diff] [blame] | 4028 | if (rtm->rtm_type == RTN_LOCAL) |
| 4029 | cfg->fc_flags |= RTF_LOCAL; |
| 4030 | |
Martin KaFai Lau | 1f56a01f | 2015-04-28 13:03:03 -0700 | [diff] [blame] | 4031 | if (rtm->rtm_flags & RTM_F_CLONED) |
| 4032 | cfg->fc_flags |= RTF_CACHE; |
| 4033 | |
David Ahern | fc1e64e | 2018-01-25 16:55:09 -0800 | [diff] [blame] | 4034 | cfg->fc_flags |= (rtm->rtm_flags & RTNH_F_ONLINK); |
| 4035 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 4036 | cfg->fc_nlinfo.portid = NETLINK_CB(skb).portid; |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4037 | cfg->fc_nlinfo.nlh = nlh; |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 4038 | cfg->fc_nlinfo.nl_net = sock_net(skb->sk); |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4039 | |
| 4040 | if (tb[RTA_GATEWAY]) { |
Jiri Benc | 67b61f6 | 2015-03-29 16:59:26 +0200 | [diff] [blame] | 4041 | cfg->fc_gateway = nla_get_in6_addr(tb[RTA_GATEWAY]); |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4042 | cfg->fc_flags |= RTF_GATEWAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4043 | } |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4044 | |
| 4045 | if (tb[RTA_DST]) { |
| 4046 | int plen = (rtm->rtm_dst_len + 7) >> 3; |
| 4047 | |
| 4048 | if (nla_len(tb[RTA_DST]) < plen) |
| 4049 | goto errout; |
| 4050 | |
| 4051 | nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4052 | } |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4053 | |
| 4054 | if (tb[RTA_SRC]) { |
| 4055 | int plen = (rtm->rtm_src_len + 7) >> 3; |
| 4056 | |
| 4057 | if (nla_len(tb[RTA_SRC]) < plen) |
| 4058 | goto errout; |
| 4059 | |
| 4060 | nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4061 | } |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4062 | |
Daniel Walter | c3968a8 | 2011-04-13 21:10:57 +0000 | [diff] [blame] | 4063 | if (tb[RTA_PREFSRC]) |
Jiri Benc | 67b61f6 | 2015-03-29 16:59:26 +0200 | [diff] [blame] | 4064 | cfg->fc_prefsrc = nla_get_in6_addr(tb[RTA_PREFSRC]); |
Daniel Walter | c3968a8 | 2011-04-13 21:10:57 +0000 | [diff] [blame] | 4065 | |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4066 | if (tb[RTA_OIF]) |
| 4067 | cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]); |
| 4068 | |
| 4069 | if (tb[RTA_PRIORITY]) |
| 4070 | cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]); |
| 4071 | |
| 4072 | if (tb[RTA_METRICS]) { |
| 4073 | cfg->fc_mx = nla_data(tb[RTA_METRICS]); |
| 4074 | cfg->fc_mx_len = nla_len(tb[RTA_METRICS]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4075 | } |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4076 | |
| 4077 | if (tb[RTA_TABLE]) |
| 4078 | cfg->fc_table = nla_get_u32(tb[RTA_TABLE]); |
| 4079 | |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 4080 | if (tb[RTA_MULTIPATH]) { |
| 4081 | cfg->fc_mp = nla_data(tb[RTA_MULTIPATH]); |
| 4082 | cfg->fc_mp_len = nla_len(tb[RTA_MULTIPATH]); |
David Ahern | 9ed5959 | 2017-01-17 14:57:36 -0800 | [diff] [blame] | 4083 | |
| 4084 | err = lwtunnel_valid_encap_type_attr(cfg->fc_mp, |
David Ahern | c255bd6 | 2017-05-27 16:19:27 -0600 | [diff] [blame] | 4085 | cfg->fc_mp_len, extack); |
David Ahern | 9ed5959 | 2017-01-17 14:57:36 -0800 | [diff] [blame] | 4086 | if (err < 0) |
| 4087 | goto errout; |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 4088 | } |
| 4089 | |
Lubomir Rintel | c78ba6d | 2015-03-11 15:39:21 +0100 | [diff] [blame] | 4090 | if (tb[RTA_PREF]) { |
| 4091 | pref = nla_get_u8(tb[RTA_PREF]); |
| 4092 | if (pref != ICMPV6_ROUTER_PREF_LOW && |
| 4093 | pref != ICMPV6_ROUTER_PREF_HIGH) |
| 4094 | pref = ICMPV6_ROUTER_PREF_MEDIUM; |
| 4095 | cfg->fc_flags |= RTF_PREF(pref); |
| 4096 | } |
| 4097 | |
Roopa Prabhu | 19e42e4 | 2015-07-21 10:43:48 +0200 | [diff] [blame] | 4098 | if (tb[RTA_ENCAP]) |
| 4099 | cfg->fc_encap = tb[RTA_ENCAP]; |
| 4100 | |
David Ahern | 9ed5959 | 2017-01-17 14:57:36 -0800 | [diff] [blame] | 4101 | if (tb[RTA_ENCAP_TYPE]) { |
Roopa Prabhu | 19e42e4 | 2015-07-21 10:43:48 +0200 | [diff] [blame] | 4102 | cfg->fc_encap_type = nla_get_u16(tb[RTA_ENCAP_TYPE]); |
| 4103 | |
David Ahern | c255bd6 | 2017-05-27 16:19:27 -0600 | [diff] [blame] | 4104 | err = lwtunnel_valid_encap_type(cfg->fc_encap_type, extack); |
David Ahern | 9ed5959 | 2017-01-17 14:57:36 -0800 | [diff] [blame] | 4105 | if (err < 0) |
| 4106 | goto errout; |
| 4107 | } |
| 4108 | |
Xin Long | 32bc201 | 2015-12-16 17:50:11 +0800 | [diff] [blame] | 4109 | if (tb[RTA_EXPIRES]) { |
| 4110 | unsigned long timeout = addrconf_timeout_fixup(nla_get_u32(tb[RTA_EXPIRES]), HZ); |
| 4111 | |
| 4112 | if (addrconf_finite_timeout(timeout)) { |
| 4113 | cfg->fc_expires = jiffies_to_clock_t(timeout * HZ); |
| 4114 | cfg->fc_flags |= RTF_EXPIRES; |
| 4115 | } |
| 4116 | } |
| 4117 | |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4118 | err = 0; |
| 4119 | errout: |
| 4120 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4121 | } |
| 4122 | |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 4123 | struct rt6_nh { |
| 4124 | struct rt6_info *rt6_info; |
| 4125 | struct fib6_config r_cfg; |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 4126 | struct list_head next; |
| 4127 | }; |
| 4128 | |
| 4129 | static void ip6_print_replace_route_err(struct list_head *rt6_nh_list) |
| 4130 | { |
| 4131 | struct rt6_nh *nh; |
| 4132 | |
| 4133 | list_for_each_entry(nh, rt6_nh_list, next) { |
David Ahern | 7d4d506 | 2017-02-02 12:37:12 -0800 | [diff] [blame] | 4134 | 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] | 4135 | &nh->r_cfg.fc_dst, &nh->r_cfg.fc_gateway, |
| 4136 | nh->r_cfg.fc_ifindex); |
| 4137 | } |
| 4138 | } |
| 4139 | |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 4140 | static int ip6_route_info_append(struct net *net, |
| 4141 | struct list_head *rt6_nh_list, |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 4142 | struct rt6_info *rt, struct fib6_config *r_cfg) |
| 4143 | { |
| 4144 | struct rt6_nh *nh; |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 4145 | int err = -EEXIST; |
| 4146 | |
| 4147 | list_for_each_entry(nh, rt6_nh_list, next) { |
| 4148 | /* check if rt6_info already exists */ |
David Ahern | f06b754 | 2017-07-05 14:41:46 -0600 | [diff] [blame] | 4149 | if (rt6_duplicate_nexthop(nh->rt6_info, rt)) |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 4150 | return err; |
| 4151 | } |
| 4152 | |
| 4153 | nh = kzalloc(sizeof(*nh), GFP_KERNEL); |
| 4154 | if (!nh) |
| 4155 | return -ENOMEM; |
| 4156 | nh->rt6_info = rt; |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 4157 | err = ip6_convert_metrics(net, rt, r_cfg); |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 4158 | if (err) { |
| 4159 | kfree(nh); |
| 4160 | return err; |
| 4161 | } |
| 4162 | memcpy(&nh->r_cfg, r_cfg, sizeof(*r_cfg)); |
| 4163 | list_add_tail(&nh->next, rt6_nh_list); |
| 4164 | |
| 4165 | return 0; |
| 4166 | } |
| 4167 | |
David Ahern | 3b1137f | 2017-02-02 12:37:10 -0800 | [diff] [blame] | 4168 | static void ip6_route_mpath_notify(struct rt6_info *rt, |
| 4169 | struct rt6_info *rt_last, |
| 4170 | struct nl_info *info, |
| 4171 | __u16 nlflags) |
| 4172 | { |
| 4173 | /* if this is an APPEND route, then rt points to the first route |
| 4174 | * inserted and rt_last points to last route inserted. Userspace |
| 4175 | * wants a consistent dump of the route which starts at the first |
| 4176 | * nexthop. Since sibling routes are always added at the end of |
| 4177 | * the list, find the first sibling of the last route appended |
| 4178 | */ |
| 4179 | if ((nlflags & NLM_F_APPEND) && rt_last && rt_last->rt6i_nsiblings) { |
| 4180 | rt = list_first_entry(&rt_last->rt6i_siblings, |
| 4181 | struct rt6_info, |
| 4182 | rt6i_siblings); |
| 4183 | } |
| 4184 | |
| 4185 | if (rt) |
| 4186 | inet6_rt_notify(RTM_NEWROUTE, rt, info, nlflags); |
| 4187 | } |
| 4188 | |
David Ahern | 333c430 | 2017-05-21 10:12:04 -0600 | [diff] [blame] | 4189 | static int ip6_route_multipath_add(struct fib6_config *cfg, |
| 4190 | struct netlink_ext_ack *extack) |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 4191 | { |
David Ahern | 3b1137f | 2017-02-02 12:37:10 -0800 | [diff] [blame] | 4192 | struct rt6_info *rt_notif = NULL, *rt_last = NULL; |
| 4193 | struct nl_info *info = &cfg->fc_nlinfo; |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 4194 | struct fib6_config r_cfg; |
| 4195 | struct rtnexthop *rtnh; |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 4196 | struct rt6_info *rt; |
| 4197 | struct rt6_nh *err_nh; |
| 4198 | struct rt6_nh *nh, *nh_safe; |
David Ahern | 3b1137f | 2017-02-02 12:37:10 -0800 | [diff] [blame] | 4199 | __u16 nlflags; |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 4200 | int remaining; |
| 4201 | int attrlen; |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 4202 | int err = 1; |
| 4203 | int nhn = 0; |
| 4204 | int replace = (cfg->fc_nlinfo.nlh && |
| 4205 | (cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_REPLACE)); |
| 4206 | LIST_HEAD(rt6_nh_list); |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 4207 | |
David Ahern | 3b1137f | 2017-02-02 12:37:10 -0800 | [diff] [blame] | 4208 | nlflags = replace ? NLM_F_REPLACE : NLM_F_CREATE; |
| 4209 | if (info->nlh && info->nlh->nlmsg_flags & NLM_F_APPEND) |
| 4210 | nlflags |= NLM_F_APPEND; |
| 4211 | |
Michal Kubeček | 35f1b4e | 2015-05-18 20:53:55 +0200 | [diff] [blame] | 4212 | remaining = cfg->fc_mp_len; |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 4213 | rtnh = (struct rtnexthop *)cfg->fc_mp; |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 4214 | |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 4215 | /* Parse a Multipath Entry and build a list (rt6_nh_list) of |
| 4216 | * rt6_info structs per nexthop |
| 4217 | */ |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 4218 | while (rtnh_ok(rtnh, remaining)) { |
| 4219 | memcpy(&r_cfg, cfg, sizeof(*cfg)); |
| 4220 | if (rtnh->rtnh_ifindex) |
| 4221 | r_cfg.fc_ifindex = rtnh->rtnh_ifindex; |
| 4222 | |
| 4223 | attrlen = rtnh_attrlen(rtnh); |
| 4224 | if (attrlen > 0) { |
| 4225 | struct nlattr *nla, *attrs = rtnh_attrs(rtnh); |
| 4226 | |
| 4227 | nla = nla_find(attrs, attrlen, RTA_GATEWAY); |
| 4228 | if (nla) { |
Jiri Benc | 67b61f6 | 2015-03-29 16:59:26 +0200 | [diff] [blame] | 4229 | r_cfg.fc_gateway = nla_get_in6_addr(nla); |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 4230 | r_cfg.fc_flags |= RTF_GATEWAY; |
| 4231 | } |
Roopa Prabhu | 19e42e4 | 2015-07-21 10:43:48 +0200 | [diff] [blame] | 4232 | r_cfg.fc_encap = nla_find(attrs, attrlen, RTA_ENCAP); |
| 4233 | nla = nla_find(attrs, attrlen, RTA_ENCAP_TYPE); |
| 4234 | if (nla) |
| 4235 | r_cfg.fc_encap_type = nla_get_u16(nla); |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 4236 | } |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 4237 | |
David Ahern | 68e2ffd | 2018-03-20 10:06:59 -0700 | [diff] [blame] | 4238 | r_cfg.fc_flags |= (rtnh->rtnh_flags & RTNH_F_ONLINK); |
David Ahern | 333c430 | 2017-05-21 10:12:04 -0600 | [diff] [blame] | 4239 | rt = ip6_route_info_create(&r_cfg, extack); |
Roopa Prabhu | 8c5b83f | 2015-10-10 08:26:36 -0700 | [diff] [blame] | 4240 | if (IS_ERR(rt)) { |
| 4241 | err = PTR_ERR(rt); |
| 4242 | rt = NULL; |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 4243 | goto cleanup; |
Roopa Prabhu | 8c5b83f | 2015-10-10 08:26:36 -0700 | [diff] [blame] | 4244 | } |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 4245 | |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 4246 | rt->fib6_nh.nh_weight = rtnh->rtnh_hops + 1; |
Ido Schimmel | 398958a | 2018-01-09 16:40:28 +0200 | [diff] [blame] | 4247 | |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 4248 | err = ip6_route_info_append(info->nl_net, &rt6_nh_list, |
| 4249 | rt, &r_cfg); |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 4250 | if (err) { |
Wei Wang | 587fea7 | 2017-06-17 10:42:36 -0700 | [diff] [blame] | 4251 | dst_release_immediate(&rt->dst); |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 4252 | goto cleanup; |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 4253 | } |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 4254 | |
| 4255 | rtnh = rtnh_next(rtnh, &remaining); |
| 4256 | } |
| 4257 | |
David Ahern | 3b1137f | 2017-02-02 12:37:10 -0800 | [diff] [blame] | 4258 | /* for add and replace send one notification with all nexthops. |
| 4259 | * Skip the notification in fib6_add_rt2node and send one with |
| 4260 | * the full route when done |
| 4261 | */ |
| 4262 | info->skip_notify = 1; |
| 4263 | |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 4264 | err_nh = NULL; |
| 4265 | list_for_each_entry(nh, &rt6_nh_list, next) { |
David Ahern | 3b1137f | 2017-02-02 12:37:10 -0800 | [diff] [blame] | 4266 | rt_last = nh->rt6_info; |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 4267 | err = __ip6_ins_rt(nh->rt6_info, info, extack); |
David Ahern | 3b1137f | 2017-02-02 12:37:10 -0800 | [diff] [blame] | 4268 | /* save reference to first route for notification */ |
| 4269 | if (!rt_notif && !err) |
| 4270 | rt_notif = nh->rt6_info; |
| 4271 | |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 4272 | /* nh->rt6_info is used or freed at this point, reset to NULL*/ |
| 4273 | nh->rt6_info = NULL; |
| 4274 | if (err) { |
| 4275 | if (replace && nhn) |
| 4276 | ip6_print_replace_route_err(&rt6_nh_list); |
| 4277 | err_nh = nh; |
| 4278 | goto add_errout; |
| 4279 | } |
| 4280 | |
Nicolas Dichtel | 1a72418 | 2012-11-01 22:58:22 +0000 | [diff] [blame] | 4281 | /* Because each route is added like a single route we remove |
Michal Kubeček | 2759647 | 2015-05-18 20:54:00 +0200 | [diff] [blame] | 4282 | * these flags after the first nexthop: if there is a collision, |
| 4283 | * we have already failed to add the first nexthop: |
| 4284 | * fib6_add_rt2node() has rejected it; when replacing, old |
| 4285 | * nexthops have been replaced by first new, the rest should |
| 4286 | * be added to it. |
Nicolas Dichtel | 1a72418 | 2012-11-01 22:58:22 +0000 | [diff] [blame] | 4287 | */ |
Michal Kubeček | 2759647 | 2015-05-18 20:54:00 +0200 | [diff] [blame] | 4288 | cfg->fc_nlinfo.nlh->nlmsg_flags &= ~(NLM_F_EXCL | |
| 4289 | NLM_F_REPLACE); |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 4290 | nhn++; |
| 4291 | } |
| 4292 | |
David Ahern | 3b1137f | 2017-02-02 12:37:10 -0800 | [diff] [blame] | 4293 | /* success ... tell user about new route */ |
| 4294 | ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags); |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 4295 | goto cleanup; |
| 4296 | |
| 4297 | add_errout: |
David Ahern | 3b1137f | 2017-02-02 12:37:10 -0800 | [diff] [blame] | 4298 | /* send notification for routes that were added so that |
| 4299 | * the delete notifications sent by ip6_route_del are |
| 4300 | * coherent |
| 4301 | */ |
| 4302 | if (rt_notif) |
| 4303 | ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags); |
| 4304 | |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 4305 | /* Delete routes that were already added */ |
| 4306 | list_for_each_entry(nh, &rt6_nh_list, next) { |
| 4307 | if (err_nh == nh) |
| 4308 | break; |
David Ahern | 333c430 | 2017-05-21 10:12:04 -0600 | [diff] [blame] | 4309 | ip6_route_del(&nh->r_cfg, extack); |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 4310 | } |
| 4311 | |
| 4312 | cleanup: |
| 4313 | list_for_each_entry_safe(nh, nh_safe, &rt6_nh_list, next) { |
Wei Wang | 587fea7 | 2017-06-17 10:42:36 -0700 | [diff] [blame] | 4314 | if (nh->rt6_info) |
| 4315 | dst_release_immediate(&nh->rt6_info->dst); |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 4316 | list_del(&nh->next); |
| 4317 | kfree(nh); |
| 4318 | } |
| 4319 | |
| 4320 | return err; |
| 4321 | } |
| 4322 | |
David Ahern | 333c430 | 2017-05-21 10:12:04 -0600 | [diff] [blame] | 4323 | static int ip6_route_multipath_del(struct fib6_config *cfg, |
| 4324 | struct netlink_ext_ack *extack) |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 4325 | { |
| 4326 | struct fib6_config r_cfg; |
| 4327 | struct rtnexthop *rtnh; |
| 4328 | int remaining; |
| 4329 | int attrlen; |
| 4330 | int err = 1, last_err = 0; |
| 4331 | |
| 4332 | remaining = cfg->fc_mp_len; |
| 4333 | rtnh = (struct rtnexthop *)cfg->fc_mp; |
| 4334 | |
| 4335 | /* Parse a Multipath Entry */ |
| 4336 | while (rtnh_ok(rtnh, remaining)) { |
| 4337 | memcpy(&r_cfg, cfg, sizeof(*cfg)); |
| 4338 | if (rtnh->rtnh_ifindex) |
| 4339 | r_cfg.fc_ifindex = rtnh->rtnh_ifindex; |
| 4340 | |
| 4341 | attrlen = rtnh_attrlen(rtnh); |
| 4342 | if (attrlen > 0) { |
| 4343 | struct nlattr *nla, *attrs = rtnh_attrs(rtnh); |
| 4344 | |
| 4345 | nla = nla_find(attrs, attrlen, RTA_GATEWAY); |
| 4346 | if (nla) { |
| 4347 | nla_memcpy(&r_cfg.fc_gateway, nla, 16); |
| 4348 | r_cfg.fc_flags |= RTF_GATEWAY; |
| 4349 | } |
| 4350 | } |
David Ahern | 333c430 | 2017-05-21 10:12:04 -0600 | [diff] [blame] | 4351 | err = ip6_route_del(&r_cfg, extack); |
Roopa Prabhu | 6b9ea5a | 2015-09-08 10:53:04 -0700 | [diff] [blame] | 4352 | if (err) |
| 4353 | last_err = err; |
| 4354 | |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 4355 | rtnh = rtnh_next(rtnh, &remaining); |
| 4356 | } |
| 4357 | |
| 4358 | return last_err; |
| 4359 | } |
| 4360 | |
David Ahern | c21ef3e | 2017-04-16 09:48:24 -0700 | [diff] [blame] | 4361 | static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh, |
| 4362 | struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4363 | { |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4364 | struct fib6_config cfg; |
| 4365 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4366 | |
David Ahern | 333c430 | 2017-05-21 10:12:04 -0600 | [diff] [blame] | 4367 | err = rtm_to_fib6_config(skb, nlh, &cfg, extack); |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4368 | if (err < 0) |
| 4369 | return err; |
| 4370 | |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 4371 | if (cfg.fc_mp) |
David Ahern | 333c430 | 2017-05-21 10:12:04 -0600 | [diff] [blame] | 4372 | return ip6_route_multipath_del(&cfg, extack); |
David Ahern | 0ae8133 | 2017-02-02 12:37:08 -0800 | [diff] [blame] | 4373 | else { |
| 4374 | cfg.fc_delete_all_nh = 1; |
David Ahern | 333c430 | 2017-05-21 10:12:04 -0600 | [diff] [blame] | 4375 | return ip6_route_del(&cfg, extack); |
David Ahern | 0ae8133 | 2017-02-02 12:37:08 -0800 | [diff] [blame] | 4376 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4377 | } |
| 4378 | |
David Ahern | c21ef3e | 2017-04-16 09:48:24 -0700 | [diff] [blame] | 4379 | static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh, |
| 4380 | struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4381 | { |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4382 | struct fib6_config cfg; |
| 4383 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4384 | |
David Ahern | 333c430 | 2017-05-21 10:12:04 -0600 | [diff] [blame] | 4385 | err = rtm_to_fib6_config(skb, nlh, &cfg, extack); |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4386 | if (err < 0) |
| 4387 | return err; |
| 4388 | |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 4389 | if (cfg.fc_mp) |
David Ahern | 333c430 | 2017-05-21 10:12:04 -0600 | [diff] [blame] | 4390 | return ip6_route_multipath_add(&cfg, extack); |
Nicolas Dichtel | 51ebd31 | 2012-10-22 03:42:09 +0000 | [diff] [blame] | 4391 | else |
David Ahern | 333c430 | 2017-05-21 10:12:04 -0600 | [diff] [blame] | 4392 | return ip6_route_add(&cfg, extack); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4393 | } |
| 4394 | |
David Ahern | beb1afac5 | 2017-02-02 12:37:09 -0800 | [diff] [blame] | 4395 | static size_t rt6_nlmsg_size(struct rt6_info *rt) |
Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 4396 | { |
David Ahern | beb1afac5 | 2017-02-02 12:37:09 -0800 | [diff] [blame] | 4397 | int nexthop_len = 0; |
| 4398 | |
| 4399 | if (rt->rt6i_nsiblings) { |
| 4400 | nexthop_len = nla_total_size(0) /* RTA_MULTIPATH */ |
| 4401 | + NLA_ALIGN(sizeof(struct rtnexthop)) |
| 4402 | + nla_total_size(16) /* RTA_GATEWAY */ |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 4403 | + lwtunnel_get_encap_size(rt->fib6_nh.nh_lwtstate); |
David Ahern | beb1afac5 | 2017-02-02 12:37:09 -0800 | [diff] [blame] | 4404 | |
| 4405 | nexthop_len *= rt->rt6i_nsiblings; |
| 4406 | } |
| 4407 | |
Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 4408 | return NLMSG_ALIGN(sizeof(struct rtmsg)) |
| 4409 | + nla_total_size(16) /* RTA_SRC */ |
| 4410 | + nla_total_size(16) /* RTA_DST */ |
| 4411 | + nla_total_size(16) /* RTA_GATEWAY */ |
| 4412 | + nla_total_size(16) /* RTA_PREFSRC */ |
| 4413 | + nla_total_size(4) /* RTA_TABLE */ |
| 4414 | + nla_total_size(4) /* RTA_IIF */ |
| 4415 | + nla_total_size(4) /* RTA_OIF */ |
| 4416 | + nla_total_size(4) /* RTA_PRIORITY */ |
Noriaki TAKAMIYA | 6a2b9ce | 2007-01-23 22:09:41 -0800 | [diff] [blame] | 4417 | + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */ |
Daniel Borkmann | ea69763 | 2015-01-05 23:57:47 +0100 | [diff] [blame] | 4418 | + nla_total_size(sizeof(struct rta_cacheinfo)) |
Lubomir Rintel | c78ba6d | 2015-03-11 15:39:21 +0100 | [diff] [blame] | 4419 | + nla_total_size(TCP_CA_NAME_MAX) /* RTAX_CC_ALGO */ |
Roopa Prabhu | 19e42e4 | 2015-07-21 10:43:48 +0200 | [diff] [blame] | 4420 | + nla_total_size(1) /* RTA_PREF */ |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 4421 | + lwtunnel_get_encap_size(rt->fib6_nh.nh_lwtstate) |
David Ahern | beb1afac5 | 2017-02-02 12:37:09 -0800 | [diff] [blame] | 4422 | + nexthop_len; |
| 4423 | } |
| 4424 | |
| 4425 | static int rt6_nexthop_info(struct sk_buff *skb, struct rt6_info *rt, |
David Ahern | 5be083c | 2017-03-06 15:57:31 -0800 | [diff] [blame] | 4426 | unsigned int *flags, bool skip_oif) |
David Ahern | beb1afac5 | 2017-02-02 12:37:09 -0800 | [diff] [blame] | 4427 | { |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 4428 | if (rt->fib6_nh.nh_flags & RTNH_F_DEAD) |
Ido Schimmel | f9d882e | 2018-01-07 12:45:10 +0200 | [diff] [blame] | 4429 | *flags |= RTNH_F_DEAD; |
| 4430 | |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 4431 | if (rt->fib6_nh.nh_flags & RTNH_F_LINKDOWN) { |
David Ahern | beb1afac5 | 2017-02-02 12:37:09 -0800 | [diff] [blame] | 4432 | *flags |= RTNH_F_LINKDOWN; |
| 4433 | if (rt->rt6i_idev->cnf.ignore_routes_with_linkdown) |
| 4434 | *flags |= RTNH_F_DEAD; |
| 4435 | } |
| 4436 | |
| 4437 | if (rt->rt6i_flags & RTF_GATEWAY) { |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 4438 | 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] | 4439 | goto nla_put_failure; |
| 4440 | } |
| 4441 | |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 4442 | *flags |= (rt->fib6_nh.nh_flags & RTNH_F_ONLINK); |
| 4443 | if (rt->fib6_nh.nh_flags & RTNH_F_OFFLOAD) |
Ido Schimmel | 61e4d01 | 2017-08-03 13:28:20 +0200 | [diff] [blame] | 4444 | *flags |= RTNH_F_OFFLOAD; |
| 4445 | |
David Ahern | 5be083c | 2017-03-06 15:57:31 -0800 | [diff] [blame] | 4446 | /* not needed for multipath encoding b/c it has a rtnexthop struct */ |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 4447 | if (!skip_oif && rt->fib6_nh.nh_dev && |
| 4448 | nla_put_u32(skb, RTA_OIF, rt->fib6_nh.nh_dev->ifindex)) |
David Ahern | beb1afac5 | 2017-02-02 12:37:09 -0800 | [diff] [blame] | 4449 | goto nla_put_failure; |
| 4450 | |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 4451 | if (rt->fib6_nh.nh_lwtstate && |
| 4452 | lwtunnel_fill_encap(skb, rt->fib6_nh.nh_lwtstate) < 0) |
David Ahern | beb1afac5 | 2017-02-02 12:37:09 -0800 | [diff] [blame] | 4453 | goto nla_put_failure; |
| 4454 | |
| 4455 | return 0; |
| 4456 | |
| 4457 | nla_put_failure: |
| 4458 | return -EMSGSIZE; |
| 4459 | } |
| 4460 | |
David Ahern | 5be083c | 2017-03-06 15:57:31 -0800 | [diff] [blame] | 4461 | /* add multipath next hop */ |
David Ahern | beb1afac5 | 2017-02-02 12:37:09 -0800 | [diff] [blame] | 4462 | static int rt6_add_nexthop(struct sk_buff *skb, struct rt6_info *rt) |
| 4463 | { |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 4464 | const struct net_device *dev = rt->fib6_nh.nh_dev; |
David Ahern | beb1afac5 | 2017-02-02 12:37:09 -0800 | [diff] [blame] | 4465 | struct rtnexthop *rtnh; |
| 4466 | unsigned int flags = 0; |
| 4467 | |
| 4468 | rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh)); |
| 4469 | if (!rtnh) |
| 4470 | goto nla_put_failure; |
| 4471 | |
David Ahern | 5e670d8 | 2018-04-17 17:33:14 -0700 | [diff] [blame] | 4472 | rtnh->rtnh_hops = rt->fib6_nh.nh_weight - 1; |
| 4473 | rtnh->rtnh_ifindex = dev ? dev->ifindex : 0; |
David Ahern | beb1afac5 | 2017-02-02 12:37:09 -0800 | [diff] [blame] | 4474 | |
David Ahern | 5be083c | 2017-03-06 15:57:31 -0800 | [diff] [blame] | 4475 | if (rt6_nexthop_info(skb, rt, &flags, true) < 0) |
David Ahern | beb1afac5 | 2017-02-02 12:37:09 -0800 | [diff] [blame] | 4476 | goto nla_put_failure; |
| 4477 | |
| 4478 | rtnh->rtnh_flags = flags; |
| 4479 | |
| 4480 | /* length of rtnetlink header + attributes */ |
| 4481 | rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *)rtnh; |
| 4482 | |
| 4483 | return 0; |
| 4484 | |
| 4485 | nla_put_failure: |
| 4486 | return -EMSGSIZE; |
Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 4487 | } |
| 4488 | |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 4489 | static int rt6_fill_node(struct net *net, struct sk_buff *skb, |
| 4490 | struct rt6_info *rt, struct dst_entry *dst, |
| 4491 | struct in6_addr *dest, struct in6_addr *src, |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 4492 | int iif, int type, u32 portid, u32 seq, |
David Ahern | f8cfe2c | 2017-01-17 15:51:08 -0800 | [diff] [blame] | 4493 | unsigned int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4494 | { |
| 4495 | struct rtmsg *rtm; |
Thomas Graf | 2d7202b | 2006-08-22 00:01:27 -0700 | [diff] [blame] | 4496 | struct nlmsghdr *nlh; |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 4497 | long expires = 0; |
| 4498 | u32 *pmetrics; |
Patrick McHardy | 9e762a4 | 2006-08-10 23:09:48 -0700 | [diff] [blame] | 4499 | u32 table; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4500 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 4501 | nlh = nlmsg_put(skb, portid, seq, type, sizeof(*rtm), flags); |
David S. Miller | 3830847 | 2011-12-03 18:02:47 -0500 | [diff] [blame] | 4502 | if (!nlh) |
Patrick McHardy | 2693256 | 2007-01-31 23:16:40 -0800 | [diff] [blame] | 4503 | return -EMSGSIZE; |
Thomas Graf | 2d7202b | 2006-08-22 00:01:27 -0700 | [diff] [blame] | 4504 | |
| 4505 | rtm = nlmsg_data(nlh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4506 | rtm->rtm_family = AF_INET6; |
| 4507 | rtm->rtm_dst_len = rt->rt6i_dst.plen; |
| 4508 | rtm->rtm_src_len = rt->rt6i_src.plen; |
| 4509 | rtm->rtm_tos = 0; |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 4510 | if (rt->rt6i_table) |
Patrick McHardy | 9e762a4 | 2006-08-10 23:09:48 -0700 | [diff] [blame] | 4511 | table = rt->rt6i_table->tb6_id; |
Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 4512 | else |
Patrick McHardy | 9e762a4 | 2006-08-10 23:09:48 -0700 | [diff] [blame] | 4513 | table = RT6_TABLE_UNSPEC; |
| 4514 | rtm->rtm_table = table; |
David S. Miller | c78679e | 2012-04-01 20:27:33 -0400 | [diff] [blame] | 4515 | if (nla_put_u32(skb, RTA_TABLE, table)) |
| 4516 | goto nla_put_failure; |
David Ahern | e8478e8 | 2018-04-17 17:33:13 -0700 | [diff] [blame] | 4517 | |
| 4518 | rtm->rtm_type = rt->fib6_type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4519 | rtm->rtm_flags = 0; |
| 4520 | rtm->rtm_scope = RT_SCOPE_UNIVERSE; |
| 4521 | rtm->rtm_protocol = rt->rt6i_protocol; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4522 | |
David S. Miller | 3830847 | 2011-12-03 18:02:47 -0500 | [diff] [blame] | 4523 | if (rt->rt6i_flags & RTF_CACHE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4524 | rtm->rtm_flags |= RTM_F_CLONED; |
| 4525 | |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 4526 | if (dest) { |
| 4527 | if (nla_put_in6_addr(skb, RTA_DST, dest)) |
David S. Miller | c78679e | 2012-04-01 20:27:33 -0400 | [diff] [blame] | 4528 | goto nla_put_failure; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 4529 | rtm->rtm_dst_len = 128; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4530 | } else if (rtm->rtm_dst_len) |
Jiri Benc | 930345e | 2015-03-29 16:59:25 +0200 | [diff] [blame] | 4531 | if (nla_put_in6_addr(skb, RTA_DST, &rt->rt6i_dst.addr)) |
David S. Miller | c78679e | 2012-04-01 20:27:33 -0400 | [diff] [blame] | 4532 | goto nla_put_failure; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4533 | #ifdef CONFIG_IPV6_SUBTREES |
| 4534 | if (src) { |
Jiri Benc | 930345e | 2015-03-29 16:59:25 +0200 | [diff] [blame] | 4535 | if (nla_put_in6_addr(skb, RTA_SRC, src)) |
David S. Miller | c78679e | 2012-04-01 20:27:33 -0400 | [diff] [blame] | 4536 | goto nla_put_failure; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 4537 | rtm->rtm_src_len = 128; |
David S. Miller | c78679e | 2012-04-01 20:27:33 -0400 | [diff] [blame] | 4538 | } else if (rtm->rtm_src_len && |
Jiri Benc | 930345e | 2015-03-29 16:59:25 +0200 | [diff] [blame] | 4539 | nla_put_in6_addr(skb, RTA_SRC, &rt->rt6i_src.addr)) |
David S. Miller | c78679e | 2012-04-01 20:27:33 -0400 | [diff] [blame] | 4540 | goto nla_put_failure; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4541 | #endif |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 4542 | if (iif) { |
| 4543 | #ifdef CONFIG_IPV6_MROUTE |
| 4544 | if (ipv6_addr_is_multicast(&rt->rt6i_dst.addr)) { |
David Ahern | fd61c6b | 2017-01-17 15:51:07 -0800 | [diff] [blame] | 4545 | int err = ip6mr_get_route(net, skb, rtm, portid); |
Nikolay Aleksandrov | 2cf7507 | 2016-09-25 23:08:31 +0200 | [diff] [blame] | 4546 | |
David Ahern | fd61c6b | 2017-01-17 15:51:07 -0800 | [diff] [blame] | 4547 | if (err == 0) |
| 4548 | return 0; |
| 4549 | if (err < 0) |
| 4550 | goto nla_put_failure; |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 4551 | } else |
| 4552 | #endif |
David S. Miller | c78679e | 2012-04-01 20:27:33 -0400 | [diff] [blame] | 4553 | if (nla_put_u32(skb, RTA_IIF, iif)) |
| 4554 | goto nla_put_failure; |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 4555 | } else if (dest) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4556 | struct in6_addr saddr_buf; |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 4557 | if (ip6_route_get_saddr(net, rt, dest, 0, &saddr_buf) == 0 && |
Jiri Benc | 930345e | 2015-03-29 16:59:25 +0200 | [diff] [blame] | 4558 | nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf)) |
David S. Miller | c78679e | 2012-04-01 20:27:33 -0400 | [diff] [blame] | 4559 | goto nla_put_failure; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4560 | } |
Thomas Graf | 2d7202b | 2006-08-22 00:01:27 -0700 | [diff] [blame] | 4561 | |
Daniel Walter | c3968a8 | 2011-04-13 21:10:57 +0000 | [diff] [blame] | 4562 | if (rt->rt6i_prefsrc.plen) { |
| 4563 | struct in6_addr saddr_buf; |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 4564 | saddr_buf = rt->rt6i_prefsrc.addr; |
Jiri Benc | 930345e | 2015-03-29 16:59:25 +0200 | [diff] [blame] | 4565 | if (nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf)) |
David S. Miller | c78679e | 2012-04-01 20:27:33 -0400 | [diff] [blame] | 4566 | goto nla_put_failure; |
Daniel Walter | c3968a8 | 2011-04-13 21:10:57 +0000 | [diff] [blame] | 4567 | } |
| 4568 | |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 4569 | pmetrics = dst ? dst_metrics_ptr(dst) : rt->fib6_metrics->metrics; |
| 4570 | if (rtnetlink_put_metrics(skb, pmetrics) < 0) |
Thomas Graf | 2d7202b | 2006-08-22 00:01:27 -0700 | [diff] [blame] | 4571 | goto nla_put_failure; |
| 4572 | |
David S. Miller | c78679e | 2012-04-01 20:27:33 -0400 | [diff] [blame] | 4573 | if (nla_put_u32(skb, RTA_PRIORITY, rt->rt6i_metric)) |
| 4574 | goto nla_put_failure; |
Li Wei | 8253947 | 2012-07-29 16:01:30 +0000 | [diff] [blame] | 4575 | |
David Ahern | beb1afac5 | 2017-02-02 12:37:09 -0800 | [diff] [blame] | 4576 | /* For multipath routes, walk the siblings list and add |
| 4577 | * each as a nexthop within RTA_MULTIPATH. |
| 4578 | */ |
| 4579 | if (rt->rt6i_nsiblings) { |
| 4580 | struct rt6_info *sibling, *next_sibling; |
| 4581 | struct nlattr *mp; |
| 4582 | |
| 4583 | mp = nla_nest_start(skb, RTA_MULTIPATH); |
| 4584 | if (!mp) |
| 4585 | goto nla_put_failure; |
| 4586 | |
| 4587 | if (rt6_add_nexthop(skb, rt) < 0) |
| 4588 | goto nla_put_failure; |
| 4589 | |
| 4590 | list_for_each_entry_safe(sibling, next_sibling, |
| 4591 | &rt->rt6i_siblings, rt6i_siblings) { |
| 4592 | if (rt6_add_nexthop(skb, sibling) < 0) |
| 4593 | goto nla_put_failure; |
| 4594 | } |
| 4595 | |
| 4596 | nla_nest_end(skb, mp); |
| 4597 | } else { |
David Ahern | 5be083c | 2017-03-06 15:57:31 -0800 | [diff] [blame] | 4598 | if (rt6_nexthop_info(skb, rt, &rtm->rtm_flags, false) < 0) |
David Ahern | beb1afac5 | 2017-02-02 12:37:09 -0800 | [diff] [blame] | 4599 | goto nla_put_failure; |
| 4600 | } |
| 4601 | |
David Ahern | 1489568 | 2018-04-17 17:33:17 -0700 | [diff] [blame^] | 4602 | if (rt->rt6i_flags & RTF_EXPIRES) { |
| 4603 | expires = dst ? dst->expires : rt->expires; |
| 4604 | expires -= jiffies; |
| 4605 | } |
YOSHIFUJI Hideaki | 69cdf8f | 2008-05-19 16:55:13 -0700 | [diff] [blame] | 4606 | |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 4607 | 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] | 4608 | goto nla_put_failure; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4609 | |
Lubomir Rintel | c78ba6d | 2015-03-11 15:39:21 +0100 | [diff] [blame] | 4610 | if (nla_put_u8(skb, RTA_PREF, IPV6_EXTRACT_PREF(rt->rt6i_flags))) |
| 4611 | goto nla_put_failure; |
| 4612 | |
Roopa Prabhu | 19e42e4 | 2015-07-21 10:43:48 +0200 | [diff] [blame] | 4613 | |
Johannes Berg | 053c095 | 2015-01-16 22:09:00 +0100 | [diff] [blame] | 4614 | nlmsg_end(skb, nlh); |
| 4615 | return 0; |
Thomas Graf | 2d7202b | 2006-08-22 00:01:27 -0700 | [diff] [blame] | 4616 | |
| 4617 | nla_put_failure: |
Patrick McHardy | 2693256 | 2007-01-31 23:16:40 -0800 | [diff] [blame] | 4618 | nlmsg_cancel(skb, nlh); |
| 4619 | return -EMSGSIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4620 | } |
| 4621 | |
Patrick McHardy | 1b43af5 | 2006-08-10 23:11:17 -0700 | [diff] [blame] | 4622 | int rt6_dump_route(struct rt6_info *rt, void *p_arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4623 | { |
| 4624 | 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] | 4625 | struct net *net = arg->net; |
| 4626 | |
| 4627 | if (rt == net->ipv6.ip6_null_entry) |
| 4628 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4629 | |
Thomas Graf | 2d7202b | 2006-08-22 00:01:27 -0700 | [diff] [blame] | 4630 | if (nlmsg_len(arg->cb->nlh) >= sizeof(struct rtmsg)) { |
| 4631 | struct rtmsg *rtm = nlmsg_data(arg->cb->nlh); |
David Ahern | f8cfe2c | 2017-01-17 15:51:08 -0800 | [diff] [blame] | 4632 | |
| 4633 | /* user wants prefix routes only */ |
| 4634 | if (rtm->rtm_flags & RTM_F_PREFIX && |
| 4635 | !(rt->rt6i_flags & RTF_PREFIX_RT)) { |
| 4636 | /* success since this is not a prefix route */ |
| 4637 | return 1; |
| 4638 | } |
| 4639 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4640 | |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 4641 | return rt6_fill_node(net, arg->skb, rt, NULL, NULL, NULL, 0, |
| 4642 | RTM_NEWROUTE, NETLINK_CB(arg->cb->skb).portid, |
| 4643 | arg->cb->nlh->nlmsg_seq, NLM_F_MULTI); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4644 | } |
| 4645 | |
David Ahern | c21ef3e | 2017-04-16 09:48:24 -0700 | [diff] [blame] | 4646 | static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh, |
| 4647 | struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4648 | { |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 4649 | struct net *net = sock_net(in_skb->sk); |
Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 4650 | struct nlattr *tb[RTA_MAX+1]; |
Roopa Prabhu | 18c3a61 | 2017-05-25 10:42:40 -0700 | [diff] [blame] | 4651 | int err, iif = 0, oif = 0; |
| 4652 | struct dst_entry *dst; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4653 | struct rt6_info *rt; |
Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 4654 | struct sk_buff *skb; |
| 4655 | struct rtmsg *rtm; |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 4656 | struct flowi6 fl6; |
Roopa Prabhu | 18c3a61 | 2017-05-25 10:42:40 -0700 | [diff] [blame] | 4657 | bool fibmatch; |
Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 4658 | |
Johannes Berg | fceb643 | 2017-04-12 14:34:07 +0200 | [diff] [blame] | 4659 | err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy, |
David Ahern | c21ef3e | 2017-04-16 09:48:24 -0700 | [diff] [blame] | 4660 | extack); |
Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 4661 | if (err < 0) |
| 4662 | goto errout; |
| 4663 | |
| 4664 | err = -EINVAL; |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 4665 | memset(&fl6, 0, sizeof(fl6)); |
Hannes Frederic Sowa | 38b7097 | 2016-06-11 20:08:19 +0200 | [diff] [blame] | 4666 | rtm = nlmsg_data(nlh); |
| 4667 | fl6.flowlabel = ip6_make_flowinfo(rtm->rtm_tos, 0); |
Roopa Prabhu | 18c3a61 | 2017-05-25 10:42:40 -0700 | [diff] [blame] | 4668 | fibmatch = !!(rtm->rtm_flags & RTM_F_FIB_MATCH); |
Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 4669 | |
| 4670 | if (tb[RTA_SRC]) { |
| 4671 | if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr)) |
| 4672 | goto errout; |
| 4673 | |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 4674 | fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]); |
Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 4675 | } |
| 4676 | |
| 4677 | if (tb[RTA_DST]) { |
| 4678 | if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr)) |
| 4679 | goto errout; |
| 4680 | |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 4681 | fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]); |
Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 4682 | } |
| 4683 | |
| 4684 | if (tb[RTA_IIF]) |
| 4685 | iif = nla_get_u32(tb[RTA_IIF]); |
| 4686 | |
| 4687 | if (tb[RTA_OIF]) |
Shmulik Ladkani | 72331bc | 2012-04-01 04:03:45 +0000 | [diff] [blame] | 4688 | oif = nla_get_u32(tb[RTA_OIF]); |
Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 4689 | |
Lorenzo Colitti | 2e47b29 | 2014-05-15 16:38:41 -0700 | [diff] [blame] | 4690 | if (tb[RTA_MARK]) |
| 4691 | fl6.flowi6_mark = nla_get_u32(tb[RTA_MARK]); |
| 4692 | |
Lorenzo Colitti | 622ec2c | 2016-11-04 02:23:42 +0900 | [diff] [blame] | 4693 | if (tb[RTA_UID]) |
| 4694 | fl6.flowi6_uid = make_kuid(current_user_ns(), |
| 4695 | nla_get_u32(tb[RTA_UID])); |
| 4696 | else |
| 4697 | fl6.flowi6_uid = iif ? INVALID_UID : current_uid(); |
| 4698 | |
Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 4699 | if (iif) { |
| 4700 | struct net_device *dev; |
Shmulik Ladkani | 72331bc | 2012-04-01 04:03:45 +0000 | [diff] [blame] | 4701 | int flags = 0; |
| 4702 | |
Florian Westphal | 121622d | 2017-08-15 16:34:42 +0200 | [diff] [blame] | 4703 | rcu_read_lock(); |
| 4704 | |
| 4705 | dev = dev_get_by_index_rcu(net, iif); |
Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 4706 | if (!dev) { |
Florian Westphal | 121622d | 2017-08-15 16:34:42 +0200 | [diff] [blame] | 4707 | rcu_read_unlock(); |
Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 4708 | err = -ENODEV; |
| 4709 | goto errout; |
| 4710 | } |
Shmulik Ladkani | 72331bc | 2012-04-01 04:03:45 +0000 | [diff] [blame] | 4711 | |
| 4712 | fl6.flowi6_iif = iif; |
| 4713 | |
| 4714 | if (!ipv6_addr_any(&fl6.saddr)) |
| 4715 | flags |= RT6_LOOKUP_F_HAS_SADDR; |
| 4716 | |
David Ahern | b75cc8f | 2018-03-02 08:32:17 -0800 | [diff] [blame] | 4717 | dst = ip6_route_input_lookup(net, dev, &fl6, NULL, flags); |
Florian Westphal | 121622d | 2017-08-15 16:34:42 +0200 | [diff] [blame] | 4718 | |
| 4719 | rcu_read_unlock(); |
Shmulik Ladkani | 72331bc | 2012-04-01 04:03:45 +0000 | [diff] [blame] | 4720 | } else { |
| 4721 | fl6.flowi6_oif = oif; |
| 4722 | |
Ido Schimmel | 58acfd7 | 2017-12-20 12:28:25 +0200 | [diff] [blame] | 4723 | dst = ip6_route_output(net, NULL, &fl6); |
Roopa Prabhu | 18c3a61 | 2017-05-25 10:42:40 -0700 | [diff] [blame] | 4724 | } |
| 4725 | |
Roopa Prabhu | 18c3a61 | 2017-05-25 10:42:40 -0700 | [diff] [blame] | 4726 | |
| 4727 | rt = container_of(dst, struct rt6_info, dst); |
| 4728 | if (rt->dst.error) { |
| 4729 | err = rt->dst.error; |
| 4730 | ip6_rt_put(rt); |
| 4731 | goto errout; |
Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 4732 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4733 | |
WANG Cong | 9d6acb3 | 2017-03-01 20:48:39 -0800 | [diff] [blame] | 4734 | if (rt == net->ipv6.ip6_null_entry) { |
| 4735 | err = rt->dst.error; |
| 4736 | ip6_rt_put(rt); |
| 4737 | goto errout; |
| 4738 | } |
| 4739 | |
David S. Miller | fba961a | 2017-12-22 11:16:31 -0500 | [diff] [blame] | 4740 | if (fibmatch && rt->from) { |
| 4741 | struct rt6_info *ort = rt->from; |
Ido Schimmel | 58acfd7 | 2017-12-20 12:28:25 +0200 | [diff] [blame] | 4742 | |
| 4743 | dst_hold(&ort->dst); |
| 4744 | ip6_rt_put(rt); |
| 4745 | rt = ort; |
| 4746 | } |
| 4747 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4748 | skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); |
David S. Miller | 3830847 | 2011-12-03 18:02:47 -0500 | [diff] [blame] | 4749 | if (!skb) { |
Amerigo Wang | 94e187c | 2012-10-29 00:13:19 +0000 | [diff] [blame] | 4750 | ip6_rt_put(rt); |
Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 4751 | err = -ENOBUFS; |
| 4752 | goto errout; |
| 4753 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4754 | |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 4755 | skb_dst_set(skb, &rt->dst); |
Roopa Prabhu | 18c3a61 | 2017-05-25 10:42:40 -0700 | [diff] [blame] | 4756 | if (fibmatch) |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 4757 | err = rt6_fill_node(net, skb, rt, NULL, NULL, NULL, iif, |
Roopa Prabhu | 18c3a61 | 2017-05-25 10:42:40 -0700 | [diff] [blame] | 4758 | RTM_NEWROUTE, NETLINK_CB(in_skb).portid, |
| 4759 | nlh->nlmsg_seq, 0); |
| 4760 | else |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 4761 | err = rt6_fill_node(net, skb, rt, dst, &fl6.daddr, &fl6.saddr, |
| 4762 | iif, RTM_NEWROUTE, |
| 4763 | NETLINK_CB(in_skb).portid, nlh->nlmsg_seq, |
| 4764 | 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4765 | if (err < 0) { |
Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 4766 | kfree_skb(skb); |
| 4767 | goto errout; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4768 | } |
| 4769 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 4770 | err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid); |
Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 4771 | errout: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4772 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4773 | } |
| 4774 | |
Roopa Prabhu | 37a1d36 | 2015-09-13 10:18:33 -0700 | [diff] [blame] | 4775 | void inet6_rt_notify(int event, struct rt6_info *rt, struct nl_info *info, |
| 4776 | unsigned int nlm_flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4777 | { |
| 4778 | struct sk_buff *skb; |
Daniel Lezcano | 5578689 | 2008-03-04 13:47:47 -0800 | [diff] [blame] | 4779 | struct net *net = info->nl_net; |
Denis V. Lunev | 528c4ce | 2007-12-13 09:45:12 -0800 | [diff] [blame] | 4780 | u32 seq; |
| 4781 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4782 | |
Denis V. Lunev | 528c4ce | 2007-12-13 09:45:12 -0800 | [diff] [blame] | 4783 | err = -ENOBUFS; |
David S. Miller | 3830847 | 2011-12-03 18:02:47 -0500 | [diff] [blame] | 4784 | seq = info->nlh ? info->nlh->nlmsg_seq : 0; |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 4785 | |
Roopa Prabhu | 19e42e4 | 2015-07-21 10:43:48 +0200 | [diff] [blame] | 4786 | skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any()); |
David S. Miller | 3830847 | 2011-12-03 18:02:47 -0500 | [diff] [blame] | 4787 | if (!skb) |
Thomas Graf | 21713eb | 2006-08-15 00:35:24 -0700 | [diff] [blame] | 4788 | goto errout; |
| 4789 | |
David Ahern | d4ead6b | 2018-04-17 17:33:16 -0700 | [diff] [blame] | 4790 | err = rt6_fill_node(net, skb, rt, NULL, NULL, NULL, 0, |
| 4791 | event, info->portid, seq, nlm_flags); |
Patrick McHardy | 2693256 | 2007-01-31 23:16:40 -0800 | [diff] [blame] | 4792 | if (err < 0) { |
| 4793 | /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */ |
| 4794 | WARN_ON(err == -EMSGSIZE); |
| 4795 | kfree_skb(skb); |
| 4796 | goto errout; |
| 4797 | } |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 4798 | rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE, |
Pablo Neira Ayuso | 1ce85fe | 2009-02-24 23:18:28 -0800 | [diff] [blame] | 4799 | info->nlh, gfp_any()); |
| 4800 | return; |
Thomas Graf | 21713eb | 2006-08-15 00:35:24 -0700 | [diff] [blame] | 4801 | errout: |
| 4802 | if (err < 0) |
Daniel Lezcano | 5578689 | 2008-03-04 13:47:47 -0800 | [diff] [blame] | 4803 | rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4804 | } |
| 4805 | |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 4806 | static int ip6_route_dev_notify(struct notifier_block *this, |
Jiri Pirko | 351638e | 2013-05-28 01:30:21 +0000 | [diff] [blame] | 4807 | unsigned long event, void *ptr) |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 4808 | { |
Jiri Pirko | 351638e | 2013-05-28 01:30:21 +0000 | [diff] [blame] | 4809 | struct net_device *dev = netdev_notifier_info_to_dev(ptr); |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 4810 | struct net *net = dev_net(dev); |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 4811 | |
WANG Cong | 242d3a4 | 2017-05-08 10:12:13 -0700 | [diff] [blame] | 4812 | if (!(dev->flags & IFF_LOOPBACK)) |
| 4813 | return NOTIFY_OK; |
| 4814 | |
| 4815 | if (event == NETDEV_REGISTER) { |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 4816 | net->ipv6.ip6_null_entry->dst.dev = dev; |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 4817 | net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev); |
| 4818 | #ifdef CONFIG_IPV6_MULTIPLE_TABLES |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 4819 | net->ipv6.ip6_prohibit_entry->dst.dev = dev; |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 4820 | net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev); |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 4821 | net->ipv6.ip6_blk_hole_entry->dst.dev = dev; |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 4822 | net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev); |
| 4823 | #endif |
WANG Cong | 76da070 | 2017-06-20 11:42:27 -0700 | [diff] [blame] | 4824 | } else if (event == NETDEV_UNREGISTER && |
| 4825 | dev->reg_state != NETREG_UNREGISTERED) { |
| 4826 | /* NETDEV_UNREGISTER could be fired for multiple times by |
| 4827 | * netdev_wait_allrefs(). Make sure we only call this once. |
| 4828 | */ |
Eric Dumazet | 12d94a8 | 2017-08-15 04:09:51 -0700 | [diff] [blame] | 4829 | in6_dev_put_clear(&net->ipv6.ip6_null_entry->rt6i_idev); |
WANG Cong | 242d3a4 | 2017-05-08 10:12:13 -0700 | [diff] [blame] | 4830 | #ifdef CONFIG_IPV6_MULTIPLE_TABLES |
Eric Dumazet | 12d94a8 | 2017-08-15 04:09:51 -0700 | [diff] [blame] | 4831 | in6_dev_put_clear(&net->ipv6.ip6_prohibit_entry->rt6i_idev); |
| 4832 | in6_dev_put_clear(&net->ipv6.ip6_blk_hole_entry->rt6i_idev); |
WANG Cong | 242d3a4 | 2017-05-08 10:12:13 -0700 | [diff] [blame] | 4833 | #endif |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 4834 | } |
| 4835 | |
| 4836 | return NOTIFY_OK; |
| 4837 | } |
| 4838 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4839 | /* |
| 4840 | * /proc |
| 4841 | */ |
| 4842 | |
| 4843 | #ifdef CONFIG_PROC_FS |
| 4844 | |
Alexey Dobriyan | 33120b3 | 2007-11-06 05:27:11 -0800 | [diff] [blame] | 4845 | static const struct file_operations ipv6_route_proc_fops = { |
Alexey Dobriyan | 33120b3 | 2007-11-06 05:27:11 -0800 | [diff] [blame] | 4846 | .open = ipv6_route_open, |
| 4847 | .read = seq_read, |
| 4848 | .llseek = seq_lseek, |
Hannes Frederic Sowa | 8d2ca1d | 2013-09-21 16:55:59 +0200 | [diff] [blame] | 4849 | .release = seq_release_net, |
Alexey Dobriyan | 33120b3 | 2007-11-06 05:27:11 -0800 | [diff] [blame] | 4850 | }; |
| 4851 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4852 | static int rt6_stats_seq_show(struct seq_file *seq, void *v) |
| 4853 | { |
Daniel Lezcano | 69ddb80 | 2008-03-04 13:46:23 -0800 | [diff] [blame] | 4854 | struct net *net = (struct net *)seq->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4855 | seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n", |
Daniel Lezcano | 69ddb80 | 2008-03-04 13:46:23 -0800 | [diff] [blame] | 4856 | net->ipv6.rt6_stats->fib_nodes, |
| 4857 | net->ipv6.rt6_stats->fib_route_nodes, |
Wei Wang | 81eb844 | 2017-10-06 12:06:11 -0700 | [diff] [blame] | 4858 | atomic_read(&net->ipv6.rt6_stats->fib_rt_alloc), |
Daniel Lezcano | 69ddb80 | 2008-03-04 13:46:23 -0800 | [diff] [blame] | 4859 | net->ipv6.rt6_stats->fib_rt_entries, |
| 4860 | net->ipv6.rt6_stats->fib_rt_cache, |
Eric Dumazet | fc66f95 | 2010-10-08 06:37:34 +0000 | [diff] [blame] | 4861 | dst_entries_get_slow(&net->ipv6.ip6_dst_ops), |
Daniel Lezcano | 69ddb80 | 2008-03-04 13:46:23 -0800 | [diff] [blame] | 4862 | net->ipv6.rt6_stats->fib_discarded_routes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4863 | |
| 4864 | return 0; |
| 4865 | } |
| 4866 | |
| 4867 | static int rt6_stats_seq_open(struct inode *inode, struct file *file) |
| 4868 | { |
Pavel Emelyanov | de05c55 | 2008-07-18 04:07:21 -0700 | [diff] [blame] | 4869 | return single_open_net(inode, file, rt6_stats_seq_show); |
Daniel Lezcano | 69ddb80 | 2008-03-04 13:46:23 -0800 | [diff] [blame] | 4870 | } |
| 4871 | |
Arjan van de Ven | 9a32144 | 2007-02-12 00:55:35 -0800 | [diff] [blame] | 4872 | static const struct file_operations rt6_stats_seq_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4873 | .open = rt6_stats_seq_open, |
| 4874 | .read = seq_read, |
| 4875 | .llseek = seq_lseek, |
Pavel Emelyanov | b6fcbdb | 2008-07-18 04:07:44 -0700 | [diff] [blame] | 4876 | .release = single_release_net, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4877 | }; |
| 4878 | #endif /* CONFIG_PROC_FS */ |
| 4879 | |
| 4880 | #ifdef CONFIG_SYSCTL |
| 4881 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4882 | static |
Joe Perches | fe2c633 | 2013-06-11 23:04:25 -0700 | [diff] [blame] | 4883 | int ipv6_sysctl_rtcache_flush(struct ctl_table *ctl, int write, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4884 | void __user *buffer, size_t *lenp, loff_t *ppos) |
| 4885 | { |
Lucian Adrian Grijincu | c486da3 | 2011-02-24 19:48:03 +0000 | [diff] [blame] | 4886 | struct net *net; |
| 4887 | int delay; |
| 4888 | if (!write) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4889 | return -EINVAL; |
Lucian Adrian Grijincu | c486da3 | 2011-02-24 19:48:03 +0000 | [diff] [blame] | 4890 | |
| 4891 | net = (struct net *)ctl->extra1; |
| 4892 | delay = net->ipv6.sysctl.flush_delay; |
| 4893 | proc_dointvec(ctl, write, buffer, lenp, ppos); |
Michal Kubeček | 2ac3ac8 | 2013-08-01 10:04:14 +0200 | [diff] [blame] | 4894 | 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] | 4895 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4896 | } |
| 4897 | |
Joe Perches | fe2c633 | 2013-06-11 23:04:25 -0700 | [diff] [blame] | 4898 | struct ctl_table ipv6_route_table_template[] = { |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 4899 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4900 | .procname = "flush", |
Daniel Lezcano | 4990509 | 2008-01-10 03:01:01 -0800 | [diff] [blame] | 4901 | .data = &init_net.ipv6.sysctl.flush_delay, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4902 | .maxlen = sizeof(int), |
Dave Jones | 89c8b3a1 | 2005-04-28 12:11:49 -0700 | [diff] [blame] | 4903 | .mode = 0200, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 4904 | .proc_handler = ipv6_sysctl_rtcache_flush |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4905 | }, |
| 4906 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4907 | .procname = "gc_thresh", |
Daniel Lezcano | 9a7ec3a | 2008-03-04 13:48:53 -0800 | [diff] [blame] | 4908 | .data = &ip6_dst_ops_template.gc_thresh, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4909 | .maxlen = sizeof(int), |
| 4910 | .mode = 0644, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 4911 | .proc_handler = proc_dointvec, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4912 | }, |
| 4913 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4914 | .procname = "max_size", |
Daniel Lezcano | 4990509 | 2008-01-10 03:01:01 -0800 | [diff] [blame] | 4915 | .data = &init_net.ipv6.sysctl.ip6_rt_max_size, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4916 | .maxlen = sizeof(int), |
| 4917 | .mode = 0644, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 4918 | .proc_handler = proc_dointvec, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4919 | }, |
| 4920 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4921 | .procname = "gc_min_interval", |
Daniel Lezcano | 4990509 | 2008-01-10 03:01:01 -0800 | [diff] [blame] | 4922 | .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4923 | .maxlen = sizeof(int), |
| 4924 | .mode = 0644, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 4925 | .proc_handler = proc_dointvec_jiffies, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4926 | }, |
| 4927 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4928 | .procname = "gc_timeout", |
Daniel Lezcano | 4990509 | 2008-01-10 03:01:01 -0800 | [diff] [blame] | 4929 | .data = &init_net.ipv6.sysctl.ip6_rt_gc_timeout, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4930 | .maxlen = sizeof(int), |
| 4931 | .mode = 0644, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 4932 | .proc_handler = proc_dointvec_jiffies, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4933 | }, |
| 4934 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4935 | .procname = "gc_interval", |
Daniel Lezcano | 4990509 | 2008-01-10 03:01:01 -0800 | [diff] [blame] | 4936 | .data = &init_net.ipv6.sysctl.ip6_rt_gc_interval, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4937 | .maxlen = sizeof(int), |
| 4938 | .mode = 0644, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 4939 | .proc_handler = proc_dointvec_jiffies, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4940 | }, |
| 4941 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4942 | .procname = "gc_elasticity", |
Daniel Lezcano | 4990509 | 2008-01-10 03:01:01 -0800 | [diff] [blame] | 4943 | .data = &init_net.ipv6.sysctl.ip6_rt_gc_elasticity, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4944 | .maxlen = sizeof(int), |
| 4945 | .mode = 0644, |
Min Zhang | f3d3f61 | 2010-08-14 22:42:51 -0700 | [diff] [blame] | 4946 | .proc_handler = proc_dointvec, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4947 | }, |
| 4948 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4949 | .procname = "mtu_expires", |
Daniel Lezcano | 4990509 | 2008-01-10 03:01:01 -0800 | [diff] [blame] | 4950 | .data = &init_net.ipv6.sysctl.ip6_rt_mtu_expires, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4951 | .maxlen = sizeof(int), |
| 4952 | .mode = 0644, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 4953 | .proc_handler = proc_dointvec_jiffies, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4954 | }, |
| 4955 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4956 | .procname = "min_adv_mss", |
Daniel Lezcano | 4990509 | 2008-01-10 03:01:01 -0800 | [diff] [blame] | 4957 | .data = &init_net.ipv6.sysctl.ip6_rt_min_advmss, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4958 | .maxlen = sizeof(int), |
| 4959 | .mode = 0644, |
Min Zhang | f3d3f61 | 2010-08-14 22:42:51 -0700 | [diff] [blame] | 4960 | .proc_handler = proc_dointvec, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4961 | }, |
| 4962 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4963 | .procname = "gc_min_interval_ms", |
Daniel Lezcano | 4990509 | 2008-01-10 03:01:01 -0800 | [diff] [blame] | 4964 | .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4965 | .maxlen = sizeof(int), |
| 4966 | .mode = 0644, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 4967 | .proc_handler = proc_dointvec_ms_jiffies, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4968 | }, |
Eric W. Biederman | f8572d8 | 2009-11-05 13:32:03 -0800 | [diff] [blame] | 4969 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4970 | }; |
| 4971 | |
Alexey Dobriyan | 2c8c1e7 | 2010-01-17 03:35:32 +0000 | [diff] [blame] | 4972 | struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net) |
Daniel Lezcano | 760f2d0 | 2008-01-10 02:53:43 -0800 | [diff] [blame] | 4973 | { |
| 4974 | struct ctl_table *table; |
| 4975 | |
| 4976 | table = kmemdup(ipv6_route_table_template, |
| 4977 | sizeof(ipv6_route_table_template), |
| 4978 | GFP_KERNEL); |
YOSHIFUJI Hideaki | 5ee0910 | 2008-02-28 00:24:28 +0900 | [diff] [blame] | 4979 | |
| 4980 | if (table) { |
| 4981 | table[0].data = &net->ipv6.sysctl.flush_delay; |
Lucian Adrian Grijincu | c486da3 | 2011-02-24 19:48:03 +0000 | [diff] [blame] | 4982 | table[0].extra1 = net; |
Alexey Dobriyan | 86393e5 | 2009-08-29 01:34:49 +0000 | [diff] [blame] | 4983 | table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh; |
YOSHIFUJI Hideaki | 5ee0910 | 2008-02-28 00:24:28 +0900 | [diff] [blame] | 4984 | table[2].data = &net->ipv6.sysctl.ip6_rt_max_size; |
| 4985 | table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval; |
| 4986 | table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout; |
| 4987 | table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval; |
| 4988 | table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity; |
| 4989 | table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires; |
| 4990 | table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss; |
Alexey Dobriyan | 9c69fab | 2009-12-18 20:11:03 -0800 | [diff] [blame] | 4991 | table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval; |
Eric W. Biederman | 464dc80 | 2012-11-16 03:02:59 +0000 | [diff] [blame] | 4992 | |
| 4993 | /* Don't export sysctls to unprivileged users */ |
| 4994 | if (net->user_ns != &init_user_ns) |
| 4995 | table[0].procname = NULL; |
YOSHIFUJI Hideaki | 5ee0910 | 2008-02-28 00:24:28 +0900 | [diff] [blame] | 4996 | } |
| 4997 | |
Daniel Lezcano | 760f2d0 | 2008-01-10 02:53:43 -0800 | [diff] [blame] | 4998 | return table; |
| 4999 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5000 | #endif |
| 5001 | |
Alexey Dobriyan | 2c8c1e7 | 2010-01-17 03:35:32 +0000 | [diff] [blame] | 5002 | static int __net_init ip6_route_net_init(struct net *net) |
Daniel Lezcano | cdb1876 | 2008-03-04 13:45:33 -0800 | [diff] [blame] | 5003 | { |
Pavel Emelyanov | 633d424b | 2008-04-21 14:25:23 -0700 | [diff] [blame] | 5004 | int ret = -ENOMEM; |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 5005 | |
Alexey Dobriyan | 86393e5 | 2009-08-29 01:34:49 +0000 | [diff] [blame] | 5006 | memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template, |
| 5007 | sizeof(net->ipv6.ip6_dst_ops)); |
Benjamin Thery | f2fc6a5 | 2008-03-04 13:49:23 -0800 | [diff] [blame] | 5008 | |
Eric Dumazet | fc66f95 | 2010-10-08 06:37:34 +0000 | [diff] [blame] | 5009 | if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0) |
| 5010 | goto out_ip6_dst_ops; |
| 5011 | |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 5012 | net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template, |
| 5013 | sizeof(*net->ipv6.ip6_null_entry), |
| 5014 | GFP_KERNEL); |
| 5015 | if (!net->ipv6.ip6_null_entry) |
Eric Dumazet | fc66f95 | 2010-10-08 06:37:34 +0000 | [diff] [blame] | 5016 | goto out_ip6_dst_entries; |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 5017 | 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] | 5018 | dst_init_metrics(&net->ipv6.ip6_null_entry->dst, |
| 5019 | ip6_template_metrics, true); |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 5020 | |
| 5021 | #ifdef CONFIG_IPV6_MULTIPLE_TABLES |
Vincent Bernat | feca7d8 | 2017-08-08 20:23:49 +0200 | [diff] [blame] | 5022 | net->ipv6.fib6_has_custom_rules = false; |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 5023 | net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template, |
| 5024 | sizeof(*net->ipv6.ip6_prohibit_entry), |
| 5025 | GFP_KERNEL); |
Peter Zijlstra | 68fffc6 | 2008-10-07 14:12:10 -0700 | [diff] [blame] | 5026 | if (!net->ipv6.ip6_prohibit_entry) |
| 5027 | goto out_ip6_null_entry; |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 5028 | 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] | 5029 | dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst, |
| 5030 | ip6_template_metrics, true); |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 5031 | |
| 5032 | net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template, |
| 5033 | sizeof(*net->ipv6.ip6_blk_hole_entry), |
| 5034 | GFP_KERNEL); |
Peter Zijlstra | 68fffc6 | 2008-10-07 14:12:10 -0700 | [diff] [blame] | 5035 | if (!net->ipv6.ip6_blk_hole_entry) |
| 5036 | goto out_ip6_prohibit_entry; |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 5037 | 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] | 5038 | dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst, |
| 5039 | ip6_template_metrics, true); |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 5040 | #endif |
| 5041 | |
Peter Zijlstra | b339a47c | 2008-10-07 14:15:00 -0700 | [diff] [blame] | 5042 | net->ipv6.sysctl.flush_delay = 0; |
| 5043 | net->ipv6.sysctl.ip6_rt_max_size = 4096; |
| 5044 | net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2; |
| 5045 | net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ; |
| 5046 | net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ; |
| 5047 | net->ipv6.sysctl.ip6_rt_gc_elasticity = 9; |
| 5048 | net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ; |
| 5049 | net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40; |
| 5050 | |
Benjamin Thery | 6891a34 | 2008-03-04 13:49:47 -0800 | [diff] [blame] | 5051 | net->ipv6.ip6_rt_gc_expire = 30*HZ; |
| 5052 | |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 5053 | ret = 0; |
| 5054 | out: |
| 5055 | return ret; |
Benjamin Thery | f2fc6a5 | 2008-03-04 13:49:23 -0800 | [diff] [blame] | 5056 | |
Peter Zijlstra | 68fffc6 | 2008-10-07 14:12:10 -0700 | [diff] [blame] | 5057 | #ifdef CONFIG_IPV6_MULTIPLE_TABLES |
| 5058 | out_ip6_prohibit_entry: |
| 5059 | kfree(net->ipv6.ip6_prohibit_entry); |
| 5060 | out_ip6_null_entry: |
| 5061 | kfree(net->ipv6.ip6_null_entry); |
| 5062 | #endif |
Eric Dumazet | fc66f95 | 2010-10-08 06:37:34 +0000 | [diff] [blame] | 5063 | out_ip6_dst_entries: |
| 5064 | dst_entries_destroy(&net->ipv6.ip6_dst_ops); |
Benjamin Thery | f2fc6a5 | 2008-03-04 13:49:23 -0800 | [diff] [blame] | 5065 | out_ip6_dst_ops: |
Benjamin Thery | f2fc6a5 | 2008-03-04 13:49:23 -0800 | [diff] [blame] | 5066 | goto out; |
Daniel Lezcano | cdb1876 | 2008-03-04 13:45:33 -0800 | [diff] [blame] | 5067 | } |
| 5068 | |
Alexey Dobriyan | 2c8c1e7 | 2010-01-17 03:35:32 +0000 | [diff] [blame] | 5069 | static void __net_exit ip6_route_net_exit(struct net *net) |
Daniel Lezcano | cdb1876 | 2008-03-04 13:45:33 -0800 | [diff] [blame] | 5070 | { |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 5071 | kfree(net->ipv6.ip6_null_entry); |
| 5072 | #ifdef CONFIG_IPV6_MULTIPLE_TABLES |
| 5073 | kfree(net->ipv6.ip6_prohibit_entry); |
| 5074 | kfree(net->ipv6.ip6_blk_hole_entry); |
| 5075 | #endif |
Xiaotian Feng | 41bb78b | 2010-11-02 16:11:05 +0000 | [diff] [blame] | 5076 | dst_entries_destroy(&net->ipv6.ip6_dst_ops); |
Daniel Lezcano | cdb1876 | 2008-03-04 13:45:33 -0800 | [diff] [blame] | 5077 | } |
| 5078 | |
Thomas Graf | d189634 | 2012-06-18 12:08:33 +0000 | [diff] [blame] | 5079 | static int __net_init ip6_route_net_init_late(struct net *net) |
| 5080 | { |
| 5081 | #ifdef CONFIG_PROC_FS |
Gao feng | d4beaa6 | 2013-02-18 01:34:54 +0000 | [diff] [blame] | 5082 | proc_create("ipv6_route", 0, net->proc_net, &ipv6_route_proc_fops); |
Joe Perches | d644406 | 2018-03-23 15:54:38 -0700 | [diff] [blame] | 5083 | proc_create("rt6_stats", 0444, net->proc_net, &rt6_stats_seq_fops); |
Thomas Graf | d189634 | 2012-06-18 12:08:33 +0000 | [diff] [blame] | 5084 | #endif |
| 5085 | return 0; |
| 5086 | } |
| 5087 | |
| 5088 | static void __net_exit ip6_route_net_exit_late(struct net *net) |
| 5089 | { |
| 5090 | #ifdef CONFIG_PROC_FS |
Gao feng | ece31ff | 2013-02-18 01:34:56 +0000 | [diff] [blame] | 5091 | remove_proc_entry("ipv6_route", net->proc_net); |
| 5092 | remove_proc_entry("rt6_stats", net->proc_net); |
Thomas Graf | d189634 | 2012-06-18 12:08:33 +0000 | [diff] [blame] | 5093 | #endif |
| 5094 | } |
| 5095 | |
Daniel Lezcano | cdb1876 | 2008-03-04 13:45:33 -0800 | [diff] [blame] | 5096 | static struct pernet_operations ip6_route_net_ops = { |
| 5097 | .init = ip6_route_net_init, |
| 5098 | .exit = ip6_route_net_exit, |
| 5099 | }; |
| 5100 | |
David S. Miller | c3426b4 | 2012-06-09 16:27:05 -0700 | [diff] [blame] | 5101 | static int __net_init ipv6_inetpeer_init(struct net *net) |
| 5102 | { |
| 5103 | struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL); |
| 5104 | |
| 5105 | if (!bp) |
| 5106 | return -ENOMEM; |
| 5107 | inet_peer_base_init(bp); |
| 5108 | net->ipv6.peers = bp; |
| 5109 | return 0; |
| 5110 | } |
| 5111 | |
| 5112 | static void __net_exit ipv6_inetpeer_exit(struct net *net) |
| 5113 | { |
| 5114 | struct inet_peer_base *bp = net->ipv6.peers; |
| 5115 | |
| 5116 | net->ipv6.peers = NULL; |
David S. Miller | 56a6b24 | 2012-06-09 16:32:41 -0700 | [diff] [blame] | 5117 | inetpeer_invalidate_tree(bp); |
David S. Miller | c3426b4 | 2012-06-09 16:27:05 -0700 | [diff] [blame] | 5118 | kfree(bp); |
| 5119 | } |
| 5120 | |
David S. Miller | 2b823f7 | 2012-06-09 19:00:16 -0700 | [diff] [blame] | 5121 | static struct pernet_operations ipv6_inetpeer_ops = { |
David S. Miller | c3426b4 | 2012-06-09 16:27:05 -0700 | [diff] [blame] | 5122 | .init = ipv6_inetpeer_init, |
| 5123 | .exit = ipv6_inetpeer_exit, |
| 5124 | }; |
| 5125 | |
Thomas Graf | d189634 | 2012-06-18 12:08:33 +0000 | [diff] [blame] | 5126 | static struct pernet_operations ip6_route_net_late_ops = { |
| 5127 | .init = ip6_route_net_init_late, |
| 5128 | .exit = ip6_route_net_exit_late, |
| 5129 | }; |
| 5130 | |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 5131 | static struct notifier_block ip6_route_dev_notifier = { |
| 5132 | .notifier_call = ip6_route_dev_notify, |
WANG Cong | 242d3a4 | 2017-05-08 10:12:13 -0700 | [diff] [blame] | 5133 | .priority = ADDRCONF_NOTIFY_PRIORITY - 10, |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 5134 | }; |
| 5135 | |
WANG Cong | 2f46093 | 2017-05-03 22:07:31 -0700 | [diff] [blame] | 5136 | void __init ip6_route_init_special_entries(void) |
| 5137 | { |
| 5138 | /* Registering of the loopback is done before this portion of code, |
| 5139 | * the loopback reference in rt6_info will not be taken, do it |
| 5140 | * manually for init_net */ |
| 5141 | init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev; |
| 5142 | init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); |
| 5143 | #ifdef CONFIG_IPV6_MULTIPLE_TABLES |
| 5144 | init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev; |
| 5145 | init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); |
| 5146 | init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev; |
| 5147 | init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); |
| 5148 | #endif |
| 5149 | } |
| 5150 | |
Daniel Lezcano | 433d49c | 2007-12-07 00:43:48 -0800 | [diff] [blame] | 5151 | int __init ip6_route_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5152 | { |
Daniel Lezcano | 433d49c | 2007-12-07 00:43:48 -0800 | [diff] [blame] | 5153 | int ret; |
Martin KaFai Lau | 8d0b94a | 2015-05-22 20:56:04 -0700 | [diff] [blame] | 5154 | int cpu; |
Daniel Lezcano | 433d49c | 2007-12-07 00:43:48 -0800 | [diff] [blame] | 5155 | |
Daniel Lezcano | 9a7ec3a | 2008-03-04 13:48:53 -0800 | [diff] [blame] | 5156 | ret = -ENOMEM; |
| 5157 | ip6_dst_ops_template.kmem_cachep = |
| 5158 | kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0, |
| 5159 | SLAB_HWCACHE_ALIGN, NULL); |
| 5160 | if (!ip6_dst_ops_template.kmem_cachep) |
Fernando Carrijo | c19a28e | 2009-01-07 18:09:08 -0800 | [diff] [blame] | 5161 | goto out; |
David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 5162 | |
Eric Dumazet | fc66f95 | 2010-10-08 06:37:34 +0000 | [diff] [blame] | 5163 | ret = dst_entries_init(&ip6_dst_blackhole_ops); |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 5164 | if (ret) |
Daniel Lezcano | bdb3289 | 2008-03-04 13:48:10 -0800 | [diff] [blame] | 5165 | goto out_kmem_cache; |
Daniel Lezcano | bdb3289 | 2008-03-04 13:48:10 -0800 | [diff] [blame] | 5166 | |
David S. Miller | c3426b4 | 2012-06-09 16:27:05 -0700 | [diff] [blame] | 5167 | ret = register_pernet_subsys(&ipv6_inetpeer_ops); |
| 5168 | if (ret) |
David S. Miller | e8803b6 | 2012-06-16 01:12:19 -0700 | [diff] [blame] | 5169 | goto out_dst_entries; |
Thomas Graf | 2a0c451 | 2012-06-14 23:00:17 +0000 | [diff] [blame] | 5170 | |
David S. Miller | 7e52b33 | 2012-06-15 15:51:55 -0700 | [diff] [blame] | 5171 | ret = register_pernet_subsys(&ip6_route_net_ops); |
| 5172 | if (ret) |
| 5173 | goto out_register_inetpeer; |
David S. Miller | c3426b4 | 2012-06-09 16:27:05 -0700 | [diff] [blame] | 5174 | |
Arnaud Ebalard | 5dc121e | 2008-10-01 02:37:56 -0700 | [diff] [blame] | 5175 | ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep; |
| 5176 | |
David S. Miller | e8803b6 | 2012-06-16 01:12:19 -0700 | [diff] [blame] | 5177 | ret = fib6_init(); |
Daniel Lezcano | 433d49c | 2007-12-07 00:43:48 -0800 | [diff] [blame] | 5178 | if (ret) |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 5179 | goto out_register_subsys; |
Daniel Lezcano | 433d49c | 2007-12-07 00:43:48 -0800 | [diff] [blame] | 5180 | |
Daniel Lezcano | 433d49c | 2007-12-07 00:43:48 -0800 | [diff] [blame] | 5181 | ret = xfrm6_init(); |
| 5182 | if (ret) |
David S. Miller | e8803b6 | 2012-06-16 01:12:19 -0700 | [diff] [blame] | 5183 | goto out_fib6_init; |
Daniel Lezcano | c35b7e7 | 2007-12-08 00:14:11 -0800 | [diff] [blame] | 5184 | |
Daniel Lezcano | 433d49c | 2007-12-07 00:43:48 -0800 | [diff] [blame] | 5185 | ret = fib6_rules_init(); |
| 5186 | if (ret) |
| 5187 | goto xfrm6_init; |
Daniel Lezcano | 7e5449c | 2007-12-08 00:14:54 -0800 | [diff] [blame] | 5188 | |
Thomas Graf | d189634 | 2012-06-18 12:08:33 +0000 | [diff] [blame] | 5189 | ret = register_pernet_subsys(&ip6_route_net_late_ops); |
| 5190 | if (ret) |
| 5191 | goto fib6_rules_init; |
| 5192 | |
Florian Westphal | 16feebc | 2017-12-02 21:44:08 +0100 | [diff] [blame] | 5193 | ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_NEWROUTE, |
| 5194 | inet6_rtm_newroute, NULL, 0); |
| 5195 | if (ret < 0) |
| 5196 | goto out_register_late_subsys; |
| 5197 | |
| 5198 | ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_DELROUTE, |
| 5199 | inet6_rtm_delroute, NULL, 0); |
| 5200 | if (ret < 0) |
| 5201 | goto out_register_late_subsys; |
| 5202 | |
| 5203 | ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETROUTE, |
| 5204 | inet6_rtm_getroute, NULL, |
| 5205 | RTNL_FLAG_DOIT_UNLOCKED); |
| 5206 | if (ret < 0) |
Thomas Graf | d189634 | 2012-06-18 12:08:33 +0000 | [diff] [blame] | 5207 | goto out_register_late_subsys; |
Daniel Lezcano | 433d49c | 2007-12-07 00:43:48 -0800 | [diff] [blame] | 5208 | |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 5209 | ret = register_netdevice_notifier(&ip6_route_dev_notifier); |
Daniel Lezcano | cdb1876 | 2008-03-04 13:45:33 -0800 | [diff] [blame] | 5210 | if (ret) |
Thomas Graf | d189634 | 2012-06-18 12:08:33 +0000 | [diff] [blame] | 5211 | goto out_register_late_subsys; |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 5212 | |
Martin KaFai Lau | 8d0b94a | 2015-05-22 20:56:04 -0700 | [diff] [blame] | 5213 | for_each_possible_cpu(cpu) { |
| 5214 | struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu); |
| 5215 | |
| 5216 | INIT_LIST_HEAD(&ul->head); |
| 5217 | spin_lock_init(&ul->lock); |
| 5218 | } |
| 5219 | |
Daniel Lezcano | 433d49c | 2007-12-07 00:43:48 -0800 | [diff] [blame] | 5220 | out: |
| 5221 | return ret; |
| 5222 | |
Thomas Graf | d189634 | 2012-06-18 12:08:33 +0000 | [diff] [blame] | 5223 | out_register_late_subsys: |
Florian Westphal | 16feebc | 2017-12-02 21:44:08 +0100 | [diff] [blame] | 5224 | rtnl_unregister_all(PF_INET6); |
Thomas Graf | d189634 | 2012-06-18 12:08:33 +0000 | [diff] [blame] | 5225 | unregister_pernet_subsys(&ip6_route_net_late_ops); |
Daniel Lezcano | 433d49c | 2007-12-07 00:43:48 -0800 | [diff] [blame] | 5226 | fib6_rules_init: |
Daniel Lezcano | 433d49c | 2007-12-07 00:43:48 -0800 | [diff] [blame] | 5227 | fib6_rules_cleanup(); |
| 5228 | xfrm6_init: |
Daniel Lezcano | 433d49c | 2007-12-07 00:43:48 -0800 | [diff] [blame] | 5229 | xfrm6_fini(); |
Thomas Graf | 2a0c451 | 2012-06-14 23:00:17 +0000 | [diff] [blame] | 5230 | out_fib6_init: |
| 5231 | fib6_gc_cleanup(); |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 5232 | out_register_subsys: |
| 5233 | unregister_pernet_subsys(&ip6_route_net_ops); |
David S. Miller | 7e52b33 | 2012-06-15 15:51:55 -0700 | [diff] [blame] | 5234 | out_register_inetpeer: |
| 5235 | unregister_pernet_subsys(&ipv6_inetpeer_ops); |
Eric Dumazet | fc66f95 | 2010-10-08 06:37:34 +0000 | [diff] [blame] | 5236 | out_dst_entries: |
| 5237 | dst_entries_destroy(&ip6_dst_blackhole_ops); |
Daniel Lezcano | 433d49c | 2007-12-07 00:43:48 -0800 | [diff] [blame] | 5238 | out_kmem_cache: |
Benjamin Thery | f2fc6a5 | 2008-03-04 13:49:23 -0800 | [diff] [blame] | 5239 | kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep); |
Daniel Lezcano | 433d49c | 2007-12-07 00:43:48 -0800 | [diff] [blame] | 5240 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5241 | } |
| 5242 | |
| 5243 | void ip6_route_cleanup(void) |
| 5244 | { |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 5245 | unregister_netdevice_notifier(&ip6_route_dev_notifier); |
Thomas Graf | d189634 | 2012-06-18 12:08:33 +0000 | [diff] [blame] | 5246 | unregister_pernet_subsys(&ip6_route_net_late_ops); |
Thomas Graf | 101367c | 2006-08-04 03:39:02 -0700 | [diff] [blame] | 5247 | fib6_rules_cleanup(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5248 | xfrm6_fini(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5249 | fib6_gc_cleanup(); |
David S. Miller | c3426b4 | 2012-06-09 16:27:05 -0700 | [diff] [blame] | 5250 | unregister_pernet_subsys(&ipv6_inetpeer_ops); |
Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 5251 | unregister_pernet_subsys(&ip6_route_net_ops); |
Xiaotian Feng | 41bb78b | 2010-11-02 16:11:05 +0000 | [diff] [blame] | 5252 | dst_entries_destroy(&ip6_dst_blackhole_ops); |
Benjamin Thery | f2fc6a5 | 2008-03-04 13:49:23 -0800 | [diff] [blame] | 5253 | kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5254 | } |